sqlite3.c revision 363179
1/******************************************************************************
2** This file is an amalgamation of many separate C source files from SQLite
3** version 3.32.3.  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** For MinGW, check to see if we can include the header file containing its
1004** version information, among other things.  Normally, this internal MinGW
1005** header file would [only] be included automatically by other MinGW header
1006** files; however, the contained version information is now required by this
1007** header file to work around binary compatibility issues (see below) and
1008** this is the only known way to reliably obtain it.  This entire #if block
1009** would be completely unnecessary if there was any other way of detecting
1010** MinGW via their preprocessor (e.g. if they customized their GCC to define
1011** some MinGW-specific macros).  When compiling for MinGW, either the
1012** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
1013** defined; otherwise, detection of conditions specific to MinGW will be
1014** disabled.
1015*/
1016#if defined(_HAVE_MINGW_H)
1017# include "mingw.h"
1018#elif defined(_HAVE__MINGW_H)
1019# include "_mingw.h"
1020#endif
1021
1022/*
1023** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
1024** define is required to maintain binary compatibility with the MSVC runtime
1025** library in use (e.g. for Windows XP).
1026*/
1027#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
1028    defined(_WIN32) && !defined(_WIN64) && \
1029    defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
1030    defined(__MSVCRT__)
1031# define _USE_32BIT_TIME_T
1032#endif
1033
1034/* The public SQLite interface.  The _FILE_OFFSET_BITS macro must appear
1035** first in QNX.  Also, the _USE_32BIT_TIME_T macro must appear first for
1036** MinGW.
1037*/
1038/************** Include sqlite3.h in the middle of sqliteInt.h ***************/
1039/************** Begin file sqlite3.h *****************************************/
1040/*
1041** 2001-09-15
1042**
1043** The author disclaims copyright to this source code.  In place of
1044** a legal notice, here is a blessing:
1045**
1046**    May you do good and not evil.
1047**    May you find forgiveness for yourself and forgive others.
1048**    May you share freely, never taking more than you give.
1049**
1050*************************************************************************
1051** This header file defines the interface that the SQLite library
1052** presents to client programs.  If a C-function, structure, datatype,
1053** or constant definition does not appear in this file, then it is
1054** not a published API of SQLite, is subject to change without
1055** notice, and should not be referenced by programs that use SQLite.
1056**
1057** Some of the definitions that are in this file are marked as
1058** "experimental".  Experimental interfaces are normally new
1059** features recently added to SQLite.  We do not anticipate changes
1060** to experimental interfaces but reserve the right to make minor changes
1061** if experience from use "in the wild" suggest such changes are prudent.
1062**
1063** The official C-language API documentation for SQLite is derived
1064** from comments in this file.  This file is the authoritative source
1065** on how SQLite interfaces are supposed to operate.
1066**
1067** The name of this file under configuration management is "sqlite.h.in".
1068** The makefile makes some minor changes to this file (such as inserting
1069** the version number) and changes its name to "sqlite3.h" as
1070** part of the build process.
1071*/
1072#ifndef SQLITE3_H
1073#define SQLITE3_H
1074#include <stdarg.h>     /* Needed for the definition of va_list */
1075
1076/*
1077** Make sure we can call this stuff from C++.
1078*/
1079#if 0
1080extern "C" {
1081#endif
1082
1083
1084/*
1085** Provide the ability to override linkage features of the interface.
1086*/
1087#ifndef SQLITE_EXTERN
1088# define SQLITE_EXTERN extern
1089#endif
1090#ifndef SQLITE_API
1091# define SQLITE_API
1092#endif
1093#ifndef SQLITE_CDECL
1094# define SQLITE_CDECL
1095#endif
1096#ifndef SQLITE_APICALL
1097# define SQLITE_APICALL
1098#endif
1099#ifndef SQLITE_STDCALL
1100# define SQLITE_STDCALL SQLITE_APICALL
1101#endif
1102#ifndef SQLITE_CALLBACK
1103# define SQLITE_CALLBACK
1104#endif
1105#ifndef SQLITE_SYSAPI
1106# define SQLITE_SYSAPI
1107#endif
1108
1109/*
1110** These no-op macros are used in front of interfaces to mark those
1111** interfaces as either deprecated or experimental.  New applications
1112** should not use deprecated interfaces - they are supported for backwards
1113** compatibility only.  Application writers should be aware that
1114** experimental interfaces are subject to change in point releases.
1115**
1116** These macros used to resolve to various kinds of compiler magic that
1117** would generate warning messages when they were used.  But that
1118** compiler magic ended up generating such a flurry of bug reports
1119** that we have taken it all out and gone back to using simple
1120** noop macros.
1121*/
1122#define SQLITE_DEPRECATED
1123#define SQLITE_EXPERIMENTAL
1124
1125/*
1126** Ensure these symbols were not defined by some previous header file.
1127*/
1128#ifdef SQLITE_VERSION
1129# undef SQLITE_VERSION
1130#endif
1131#ifdef SQLITE_VERSION_NUMBER
1132# undef SQLITE_VERSION_NUMBER
1133#endif
1134
1135/*
1136** CAPI3REF: Compile-Time Library Version Numbers
1137**
1138** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
1139** evaluates to a string literal that is the SQLite version in the
1140** format "X.Y.Z" where X is the major version number (always 3 for
1141** SQLite3) and Y is the minor version number and Z is the release number.)^
1142** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
1143** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
1144** numbers used in [SQLITE_VERSION].)^
1145** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
1146** be larger than the release from which it is derived.  Either Y will
1147** be held constant and Z will be incremented or else Y will be incremented
1148** and Z will be reset to zero.
1149**
1150** Since [version 3.6.18] ([dateof:3.6.18]),
1151** SQLite source code has been stored in the
1152** <a href="http://www.fossil-scm.org/">Fossil configuration management
1153** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
1154** a string which identifies a particular check-in of SQLite
1155** within its configuration management system.  ^The SQLITE_SOURCE_ID
1156** string contains the date and time of the check-in (UTC) and a SHA1
1157** or SHA3-256 hash of the entire source tree.  If the source code has
1158** been edited in any way since it was last checked in, then the last
1159** four hexadecimal digits of the hash may be modified.
1160**
1161** See also: [sqlite3_libversion()],
1162** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1163** [sqlite_version()] and [sqlite_source_id()].
1164*/
1165#define SQLITE_VERSION        "3.32.3"
1166#define SQLITE_VERSION_NUMBER 3032003
1167#define SQLITE_SOURCE_ID      "2020-06-18 14:00:33 7ebdfa80be8e8e73324b8d66b3460222eb74c7e9dfd655b48d6ca7e1933cc8fd"
1168
1169/*
1170** CAPI3REF: Run-Time Library Version Numbers
1171** KEYWORDS: sqlite3_version sqlite3_sourceid
1172**
1173** These interfaces provide the same information as the [SQLITE_VERSION],
1174** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
1175** but are associated with the library instead of the header file.  ^(Cautious
1176** programmers might include assert() statements in their application to
1177** verify that values returned by these interfaces match the macros in
1178** the header, and thus ensure that the application is
1179** compiled with matching library and header files.
1180**
1181** <blockquote><pre>
1182** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
1183** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 );
1184** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
1185** </pre></blockquote>)^
1186**
1187** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
1188** macro.  ^The sqlite3_libversion() function returns a pointer to the
1189** to the sqlite3_version[] string constant.  The sqlite3_libversion()
1190** function is provided for use in DLLs since DLL users usually do not have
1191** direct access to string constants within the DLL.  ^The
1192** sqlite3_libversion_number() function returns an integer equal to
1193** [SQLITE_VERSION_NUMBER].  ^(The sqlite3_sourceid() function returns
1194** a pointer to a string constant whose value is the same as the
1195** [SQLITE_SOURCE_ID] C preprocessor macro.  Except if SQLite is built
1196** using an edited copy of [the amalgamation], then the last four characters
1197** of the hash might be different from [SQLITE_SOURCE_ID].)^
1198**
1199** See also: [sqlite_version()] and [sqlite_source_id()].
1200*/
1201SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
1202SQLITE_API const char *sqlite3_libversion(void);
1203SQLITE_API const char *sqlite3_sourceid(void);
1204SQLITE_API int sqlite3_libversion_number(void);
1205
1206/*
1207** CAPI3REF: Run-Time Library Compilation Options Diagnostics
1208**
1209** ^The sqlite3_compileoption_used() function returns 0 or 1
1210** indicating whether the specified option was defined at
1211** compile time.  ^The SQLITE_ prefix may be omitted from the
1212** option name passed to sqlite3_compileoption_used().
1213**
1214** ^The sqlite3_compileoption_get() function allows iterating
1215** over the list of options that were defined at compile time by
1216** returning the N-th compile time option string.  ^If N is out of range,
1217** sqlite3_compileoption_get() returns a NULL pointer.  ^The SQLITE_
1218** prefix is omitted from any strings returned by
1219** sqlite3_compileoption_get().
1220**
1221** ^Support for the diagnostic functions sqlite3_compileoption_used()
1222** and sqlite3_compileoption_get() may be omitted by specifying the
1223** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
1224**
1225** See also: SQL functions [sqlite_compileoption_used()] and
1226** [sqlite_compileoption_get()] and the [compile_options pragma].
1227*/
1228#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1229SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
1230SQLITE_API const char *sqlite3_compileoption_get(int N);
1231#else
1232# define sqlite3_compileoption_used(X) 0
1233# define sqlite3_compileoption_get(X)  ((void*)0)
1234#endif
1235
1236/*
1237** CAPI3REF: Test To See If The Library Is Threadsafe
1238**
1239** ^The sqlite3_threadsafe() function returns zero if and only if
1240** SQLite was compiled with mutexing code omitted due to the
1241** [SQLITE_THREADSAFE] compile-time option being set to 0.
1242**
1243** SQLite can be compiled with or without mutexes.  When
1244** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
1245** are enabled and SQLite is threadsafe.  When the
1246** [SQLITE_THREADSAFE] macro is 0,
1247** the mutexes are omitted.  Without the mutexes, it is not safe
1248** to use SQLite concurrently from more than one thread.
1249**
1250** Enabling mutexes incurs a measurable performance penalty.
1251** So if speed is of utmost importance, it makes sense to disable
1252** the mutexes.  But for maximum safety, mutexes should be enabled.
1253** ^The default behavior is for mutexes to be enabled.
1254**
1255** This interface can be used by an application to make sure that the
1256** version of SQLite that it is linking against was compiled with
1257** the desired setting of the [SQLITE_THREADSAFE] macro.
1258**
1259** This interface only reports on the compile-time mutex setting
1260** of the [SQLITE_THREADSAFE] flag.  If SQLite is compiled with
1261** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
1262** can be fully or partially disabled using a call to [sqlite3_config()]
1263** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
1264** or [SQLITE_CONFIG_SERIALIZED].  ^(The return value of the
1265** sqlite3_threadsafe() function shows only the compile-time setting of
1266** thread safety, not any run-time changes to that setting made by
1267** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
1268** is unchanged by calls to sqlite3_config().)^
1269**
1270** See the [threading mode] documentation for additional information.
1271*/
1272SQLITE_API int sqlite3_threadsafe(void);
1273
1274/*
1275** CAPI3REF: Database Connection Handle
1276** KEYWORDS: {database connection} {database connections}
1277**
1278** Each open SQLite database is represented by a pointer to an instance of
1279** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
1280** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
1281** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
1282** and [sqlite3_close_v2()] are its destructors.  There are many other
1283** interfaces (such as
1284** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
1285** [sqlite3_busy_timeout()] to name but three) that are methods on an
1286** sqlite3 object.
1287*/
1288typedef struct sqlite3 sqlite3;
1289
1290/*
1291** CAPI3REF: 64-Bit Integer Types
1292** KEYWORDS: sqlite_int64 sqlite_uint64
1293**
1294** Because there is no cross-platform way to specify 64-bit integer types
1295** SQLite includes typedefs for 64-bit signed and unsigned integers.
1296**
1297** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
1298** The sqlite_int64 and sqlite_uint64 types are supported for backwards
1299** compatibility only.
1300**
1301** ^The sqlite3_int64 and sqlite_int64 types can store integer values
1302** between -9223372036854775808 and +9223372036854775807 inclusive.  ^The
1303** sqlite3_uint64 and sqlite_uint64 types can store integer values
1304** between 0 and +18446744073709551615 inclusive.
1305*/
1306#ifdef SQLITE_INT64_TYPE
1307  typedef SQLITE_INT64_TYPE sqlite_int64;
1308# ifdef SQLITE_UINT64_TYPE
1309    typedef SQLITE_UINT64_TYPE sqlite_uint64;
1310# else
1311    typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
1312# endif
1313#elif defined(_MSC_VER) || defined(__BORLANDC__)
1314  typedef __int64 sqlite_int64;
1315  typedef unsigned __int64 sqlite_uint64;
1316#else
1317  typedef long long int sqlite_int64;
1318  typedef unsigned long long int sqlite_uint64;
1319#endif
1320typedef sqlite_int64 sqlite3_int64;
1321typedef sqlite_uint64 sqlite3_uint64;
1322
1323/*
1324** If compiling for a processor that lacks floating point support,
1325** substitute integer for floating-point.
1326*/
1327#ifdef SQLITE_OMIT_FLOATING_POINT
1328# define double sqlite3_int64
1329#endif
1330
1331/*
1332** CAPI3REF: Closing A Database Connection
1333** DESTRUCTOR: sqlite3
1334**
1335** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
1336** for the [sqlite3] object.
1337** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
1338** the [sqlite3] object is successfully destroyed and all associated
1339** resources are deallocated.
1340**
1341** Ideally, applications should [sqlite3_finalize | finalize] all
1342** [prepared statements], [sqlite3_blob_close | close] all [BLOB handles], and
1343** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
1344** with the [sqlite3] object prior to attempting to close the object.
1345** ^If the database connection is associated with unfinalized prepared
1346** statements, BLOB handlers, and/or unfinished sqlite3_backup objects then
1347** sqlite3_close() will leave the database connection open and return
1348** [SQLITE_BUSY]. ^If sqlite3_close_v2() is called with unfinalized prepared
1349** statements, unclosed BLOB handlers, and/or unfinished sqlite3_backups,
1350** it returns [SQLITE_OK] regardless, but instead of deallocating the database
1351** connection immediately, it marks the database connection as an unusable
1352** "zombie" and makes arrangements to automatically deallocate the database
1353** connection after all prepared statements are finalized, all BLOB handles
1354** are closed, and all backups have finished. The sqlite3_close_v2() interface
1355** is intended for use with host languages that are garbage collected, and
1356** where the order in which destructors are called is arbitrary.
1357**
1358** ^If an [sqlite3] object is destroyed while a transaction is open,
1359** the transaction is automatically rolled back.
1360**
1361** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
1362** must be either a NULL
1363** pointer or an [sqlite3] object pointer obtained
1364** from [sqlite3_open()], [sqlite3_open16()], or
1365** [sqlite3_open_v2()], and not previously closed.
1366** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
1367** argument is a harmless no-op.
1368*/
1369SQLITE_API int sqlite3_close(sqlite3*);
1370SQLITE_API int sqlite3_close_v2(sqlite3*);
1371
1372/*
1373** The type for a callback function.
1374** This is legacy and deprecated.  It is included for historical
1375** compatibility and is not documented.
1376*/
1377typedef int (*sqlite3_callback)(void*,int,char**, char**);
1378
1379/*
1380** CAPI3REF: One-Step Query Execution Interface
1381** METHOD: sqlite3
1382**
1383** The sqlite3_exec() interface is a convenience wrapper around
1384** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
1385** that allows an application to run multiple statements of SQL
1386** without having to use a lot of C code.
1387**
1388** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
1389** semicolon-separate SQL statements passed into its 2nd argument,
1390** in the context of the [database connection] passed in as its 1st
1391** argument.  ^If the callback function of the 3rd argument to
1392** sqlite3_exec() is not NULL, then it is invoked for each result row
1393** coming out of the evaluated SQL statements.  ^The 4th argument to
1394** sqlite3_exec() is relayed through to the 1st argument of each
1395** callback invocation.  ^If the callback pointer to sqlite3_exec()
1396** is NULL, then no callback is ever invoked and result rows are
1397** ignored.
1398**
1399** ^If an error occurs while evaluating the SQL statements passed into
1400** sqlite3_exec(), then execution of the current statement stops and
1401** subsequent statements are skipped.  ^If the 5th parameter to sqlite3_exec()
1402** is not NULL then any error message is written into memory obtained
1403** from [sqlite3_malloc()] and passed back through the 5th parameter.
1404** To avoid memory leaks, the application should invoke [sqlite3_free()]
1405** on error message strings returned through the 5th parameter of
1406** sqlite3_exec() after the error message string is no longer needed.
1407** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
1408** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
1409** NULL before returning.
1410**
1411** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
1412** routine returns SQLITE_ABORT without invoking the callback again and
1413** without running any subsequent SQL statements.
1414**
1415** ^The 2nd argument to the sqlite3_exec() callback function is the
1416** number of columns in the result.  ^The 3rd argument to the sqlite3_exec()
1417** callback is an array of pointers to strings obtained as if from
1418** [sqlite3_column_text()], one for each column.  ^If an element of a
1419** result row is NULL then the corresponding string pointer for the
1420** sqlite3_exec() callback is a NULL pointer.  ^The 4th argument to the
1421** sqlite3_exec() callback is an array of pointers to strings where each
1422** entry represents the name of corresponding result column as obtained
1423** from [sqlite3_column_name()].
1424**
1425** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
1426** to an empty string, or a pointer that contains only whitespace and/or
1427** SQL comments, then no SQL statements are evaluated and the database
1428** is not changed.
1429**
1430** Restrictions:
1431**
1432** <ul>
1433** <li> The application must ensure that the 1st parameter to sqlite3_exec()
1434**      is a valid and open [database connection].
1435** <li> The application must not close the [database connection] specified by
1436**      the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
1437** <li> The application must not modify the SQL statement text passed into
1438**      the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
1439** </ul>
1440*/
1441SQLITE_API int sqlite3_exec(
1442  sqlite3*,                                  /* An open database */
1443  const char *sql,                           /* SQL to be evaluated */
1444  int (*callback)(void*,int,char**,char**),  /* Callback function */
1445  void *,                                    /* 1st argument to callback */
1446  char **errmsg                              /* Error msg written here */
1447);
1448
1449/*
1450** CAPI3REF: Result Codes
1451** KEYWORDS: {result code definitions}
1452**
1453** Many SQLite functions return an integer result code from the set shown
1454** here in order to indicate success or failure.
1455**
1456** New error codes may be added in future versions of SQLite.
1457**
1458** See also: [extended result code definitions]
1459*/
1460#define SQLITE_OK           0   /* Successful result */
1461/* beginning-of-error-codes */
1462#define SQLITE_ERROR        1   /* Generic error */
1463#define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
1464#define SQLITE_PERM         3   /* Access permission denied */
1465#define SQLITE_ABORT        4   /* Callback routine requested an abort */
1466#define SQLITE_BUSY         5   /* The database file is locked */
1467#define SQLITE_LOCKED       6   /* A table in the database is locked */
1468#define SQLITE_NOMEM        7   /* A malloc() failed */
1469#define SQLITE_READONLY     8   /* Attempt to write a readonly database */
1470#define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
1471#define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
1472#define SQLITE_CORRUPT     11   /* The database disk image is malformed */
1473#define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
1474#define SQLITE_FULL        13   /* Insertion failed because database is full */
1475#define SQLITE_CANTOPEN    14   /* Unable to open the database file */
1476#define SQLITE_PROTOCOL    15   /* Database lock protocol error */
1477#define SQLITE_EMPTY       16   /* Internal use only */
1478#define SQLITE_SCHEMA      17   /* The database schema changed */
1479#define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
1480#define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
1481#define SQLITE_MISMATCH    20   /* Data type mismatch */
1482#define SQLITE_MISUSE      21   /* Library used incorrectly */
1483#define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
1484#define SQLITE_AUTH        23   /* Authorization denied */
1485#define SQLITE_FORMAT      24   /* Not used */
1486#define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
1487#define SQLITE_NOTADB      26   /* File opened that is not a database file */
1488#define SQLITE_NOTICE      27   /* Notifications from sqlite3_log() */
1489#define SQLITE_WARNING     28   /* Warnings from sqlite3_log() */
1490#define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
1491#define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
1492/* end-of-error-codes */
1493
1494/*
1495** CAPI3REF: Extended Result Codes
1496** KEYWORDS: {extended result code definitions}
1497**
1498** In its default configuration, SQLite API routines return one of 30 integer
1499** [result codes].  However, experience has shown that many of
1500** these result codes are too coarse-grained.  They do not provide as
1501** much information about problems as programmers might like.  In an effort to
1502** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8]
1503** and later) include
1504** support for additional result codes that provide more detailed information
1505** about errors. These [extended result codes] are enabled or disabled
1506** on a per database connection basis using the
1507** [sqlite3_extended_result_codes()] API.  Or, the extended code for
1508** the most recent error can be obtained using
1509** [sqlite3_extended_errcode()].
1510*/
1511#define SQLITE_ERROR_MISSING_COLLSEQ   (SQLITE_ERROR | (1<<8))
1512#define SQLITE_ERROR_RETRY             (SQLITE_ERROR | (2<<8))
1513#define SQLITE_ERROR_SNAPSHOT          (SQLITE_ERROR | (3<<8))
1514#define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
1515#define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
1516#define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
1517#define SQLITE_IOERR_FSYNC             (SQLITE_IOERR | (4<<8))
1518#define SQLITE_IOERR_DIR_FSYNC         (SQLITE_IOERR | (5<<8))
1519#define SQLITE_IOERR_TRUNCATE          (SQLITE_IOERR | (6<<8))
1520#define SQLITE_IOERR_FSTAT             (SQLITE_IOERR | (7<<8))
1521#define SQLITE_IOERR_UNLOCK            (SQLITE_IOERR | (8<<8))
1522#define SQLITE_IOERR_RDLOCK            (SQLITE_IOERR | (9<<8))
1523#define SQLITE_IOERR_DELETE            (SQLITE_IOERR | (10<<8))
1524#define SQLITE_IOERR_BLOCKED           (SQLITE_IOERR | (11<<8))
1525#define SQLITE_IOERR_NOMEM             (SQLITE_IOERR | (12<<8))
1526#define SQLITE_IOERR_ACCESS            (SQLITE_IOERR | (13<<8))
1527#define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
1528#define SQLITE_IOERR_LOCK              (SQLITE_IOERR | (15<<8))
1529#define SQLITE_IOERR_CLOSE             (SQLITE_IOERR | (16<<8))
1530#define SQLITE_IOERR_DIR_CLOSE         (SQLITE_IOERR | (17<<8))
1531#define SQLITE_IOERR_SHMOPEN           (SQLITE_IOERR | (18<<8))
1532#define SQLITE_IOERR_SHMSIZE           (SQLITE_IOERR | (19<<8))
1533#define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
1534#define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
1535#define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
1536#define SQLITE_IOERR_DELETE_NOENT      (SQLITE_IOERR | (23<<8))
1537#define SQLITE_IOERR_MMAP              (SQLITE_IOERR | (24<<8))
1538#define SQLITE_IOERR_GETTEMPPATH       (SQLITE_IOERR | (25<<8))
1539#define SQLITE_IOERR_CONVPATH          (SQLITE_IOERR | (26<<8))
1540#define SQLITE_IOERR_VNODE             (SQLITE_IOERR | (27<<8))
1541#define SQLITE_IOERR_AUTH              (SQLITE_IOERR | (28<<8))
1542#define SQLITE_IOERR_BEGIN_ATOMIC      (SQLITE_IOERR | (29<<8))
1543#define SQLITE_IOERR_COMMIT_ATOMIC     (SQLITE_IOERR | (30<<8))
1544#define SQLITE_IOERR_ROLLBACK_ATOMIC   (SQLITE_IOERR | (31<<8))
1545#define SQLITE_IOERR_DATA              (SQLITE_IOERR | (32<<8))
1546#define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
1547#define SQLITE_LOCKED_VTAB             (SQLITE_LOCKED |  (2<<8))
1548#define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
1549#define SQLITE_BUSY_SNAPSHOT           (SQLITE_BUSY   |  (2<<8))
1550#define SQLITE_BUSY_TIMEOUT            (SQLITE_BUSY   |  (3<<8))
1551#define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
1552#define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
1553#define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
1554#define SQLITE_CANTOPEN_CONVPATH       (SQLITE_CANTOPEN | (4<<8))
1555#define SQLITE_CANTOPEN_DIRTYWAL       (SQLITE_CANTOPEN | (5<<8)) /* Not Used */
1556#define SQLITE_CANTOPEN_SYMLINK        (SQLITE_CANTOPEN | (6<<8))
1557#define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
1558#define SQLITE_CORRUPT_SEQUENCE        (SQLITE_CORRUPT | (2<<8))
1559#define SQLITE_CORRUPT_INDEX           (SQLITE_CORRUPT | (3<<8))
1560#define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
1561#define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
1562#define SQLITE_READONLY_ROLLBACK       (SQLITE_READONLY | (3<<8))
1563#define SQLITE_READONLY_DBMOVED        (SQLITE_READONLY | (4<<8))
1564#define SQLITE_READONLY_CANTINIT       (SQLITE_READONLY | (5<<8))
1565#define SQLITE_READONLY_DIRECTORY      (SQLITE_READONLY | (6<<8))
1566#define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
1567#define SQLITE_CONSTRAINT_CHECK        (SQLITE_CONSTRAINT | (1<<8))
1568#define SQLITE_CONSTRAINT_COMMITHOOK   (SQLITE_CONSTRAINT | (2<<8))
1569#define SQLITE_CONSTRAINT_FOREIGNKEY   (SQLITE_CONSTRAINT | (3<<8))
1570#define SQLITE_CONSTRAINT_FUNCTION     (SQLITE_CONSTRAINT | (4<<8))
1571#define SQLITE_CONSTRAINT_NOTNULL      (SQLITE_CONSTRAINT | (5<<8))
1572#define SQLITE_CONSTRAINT_PRIMARYKEY   (SQLITE_CONSTRAINT | (6<<8))
1573#define SQLITE_CONSTRAINT_TRIGGER      (SQLITE_CONSTRAINT | (7<<8))
1574#define SQLITE_CONSTRAINT_UNIQUE       (SQLITE_CONSTRAINT | (8<<8))
1575#define SQLITE_CONSTRAINT_VTAB         (SQLITE_CONSTRAINT | (9<<8))
1576#define SQLITE_CONSTRAINT_ROWID        (SQLITE_CONSTRAINT |(10<<8))
1577#define SQLITE_CONSTRAINT_PINNED       (SQLITE_CONSTRAINT |(11<<8))
1578#define SQLITE_NOTICE_RECOVER_WAL      (SQLITE_NOTICE | (1<<8))
1579#define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
1580#define SQLITE_WARNING_AUTOINDEX       (SQLITE_WARNING | (1<<8))
1581#define SQLITE_AUTH_USER               (SQLITE_AUTH | (1<<8))
1582#define SQLITE_OK_LOAD_PERMANENTLY     (SQLITE_OK | (1<<8))
1583#define SQLITE_OK_SYMLINK              (SQLITE_OK | (2<<8))
1584
1585/*
1586** CAPI3REF: Flags For File Open Operations
1587**
1588** These bit values are intended for use in the
1589** 3rd parameter to the [sqlite3_open_v2()] interface and
1590** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
1591*/
1592#define SQLITE_OPEN_READONLY         0x00000001  /* Ok for sqlite3_open_v2() */
1593#define SQLITE_OPEN_READWRITE        0x00000002  /* Ok for sqlite3_open_v2() */
1594#define SQLITE_OPEN_CREATE           0x00000004  /* Ok for sqlite3_open_v2() */
1595#define SQLITE_OPEN_DELETEONCLOSE    0x00000008  /* VFS only */
1596#define SQLITE_OPEN_EXCLUSIVE        0x00000010  /* VFS only */
1597#define SQLITE_OPEN_AUTOPROXY        0x00000020  /* VFS only */
1598#define SQLITE_OPEN_URI              0x00000040  /* Ok for sqlite3_open_v2() */
1599#define SQLITE_OPEN_MEMORY           0x00000080  /* Ok for sqlite3_open_v2() */
1600#define SQLITE_OPEN_MAIN_DB          0x00000100  /* VFS only */
1601#define SQLITE_OPEN_TEMP_DB          0x00000200  /* VFS only */
1602#define SQLITE_OPEN_TRANSIENT_DB     0x00000400  /* VFS only */
1603#define SQLITE_OPEN_MAIN_JOURNAL     0x00000800  /* VFS only */
1604#define SQLITE_OPEN_TEMP_JOURNAL     0x00001000  /* VFS only */
1605#define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
1606#define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
1607#define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
1608#define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
1609#define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
1610#define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
1611#define SQLITE_OPEN_WAL              0x00080000  /* VFS only */
1612#define SQLITE_OPEN_NOFOLLOW         0x01000000  /* Ok for sqlite3_open_v2() */
1613
1614/* Reserved:                         0x00F00000 */
1615
1616/*
1617** CAPI3REF: Device Characteristics
1618**
1619** The xDeviceCharacteristics method of the [sqlite3_io_methods]
1620** object returns an integer which is a vector of these
1621** bit values expressing I/O characteristics of the mass storage
1622** device that holds the file that the [sqlite3_io_methods]
1623** refers to.
1624**
1625** The SQLITE_IOCAP_ATOMIC property means that all writes of
1626** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
1627** mean that writes of blocks that are nnn bytes in size and
1628** are aligned to an address which is an integer multiple of
1629** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
1630** that when data is appended to a file, the data is appended
1631** first then the size of the file is extended, never the other
1632** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
1633** information is written to disk in the same order as calls
1634** to xWrite().  The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
1635** after reboot following a crash or power loss, the only bytes in a
1636** file that were written at the application level might have changed
1637** and that adjacent bytes, even bytes within the same sector are
1638** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
1639** flag indicates that a file cannot be deleted when open.  The
1640** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
1641** read-only media and cannot be changed even by processes with
1642** elevated privileges.
1643**
1644** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
1645** filesystem supports doing multiple write operations atomically when those
1646** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
1647** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
1648*/
1649#define SQLITE_IOCAP_ATOMIC                 0x00000001
1650#define SQLITE_IOCAP_ATOMIC512              0x00000002
1651#define SQLITE_IOCAP_ATOMIC1K               0x00000004
1652#define SQLITE_IOCAP_ATOMIC2K               0x00000008
1653#define SQLITE_IOCAP_ATOMIC4K               0x00000010
1654#define SQLITE_IOCAP_ATOMIC8K               0x00000020
1655#define SQLITE_IOCAP_ATOMIC16K              0x00000040
1656#define SQLITE_IOCAP_ATOMIC32K              0x00000080
1657#define SQLITE_IOCAP_ATOMIC64K              0x00000100
1658#define SQLITE_IOCAP_SAFE_APPEND            0x00000200
1659#define SQLITE_IOCAP_SEQUENTIAL             0x00000400
1660#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
1661#define SQLITE_IOCAP_POWERSAFE_OVERWRITE    0x00001000
1662#define SQLITE_IOCAP_IMMUTABLE              0x00002000
1663#define SQLITE_IOCAP_BATCH_ATOMIC           0x00004000
1664
1665/*
1666** CAPI3REF: File Locking Levels
1667**
1668** SQLite uses one of these integer values as the second
1669** argument to calls it makes to the xLock() and xUnlock() methods
1670** of an [sqlite3_io_methods] object.
1671*/
1672#define SQLITE_LOCK_NONE          0
1673#define SQLITE_LOCK_SHARED        1
1674#define SQLITE_LOCK_RESERVED      2
1675#define SQLITE_LOCK_PENDING       3
1676#define SQLITE_LOCK_EXCLUSIVE     4
1677
1678/*
1679** CAPI3REF: Synchronization Type Flags
1680**
1681** When SQLite invokes the xSync() method of an
1682** [sqlite3_io_methods] object it uses a combination of
1683** these integer values as the second argument.
1684**
1685** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
1686** sync operation only needs to flush data to mass storage.  Inode
1687** information need not be flushed. If the lower four bits of the flag
1688** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
1689** If the lower four bits equal SQLITE_SYNC_FULL, that means
1690** to use Mac OS X style fullsync instead of fsync().
1691**
1692** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
1693** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
1694** settings.  The [synchronous pragma] determines when calls to the
1695** xSync VFS method occur and applies uniformly across all platforms.
1696** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
1697** energetic or rigorous or forceful the sync operations are and
1698** only make a difference on Mac OSX for the default SQLite code.
1699** (Third-party VFS implementations might also make the distinction
1700** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
1701** operating systems natively supported by SQLite, only Mac OSX
1702** cares about the difference.)
1703*/
1704#define SQLITE_SYNC_NORMAL        0x00002
1705#define SQLITE_SYNC_FULL          0x00003
1706#define SQLITE_SYNC_DATAONLY      0x00010
1707
1708/*
1709** CAPI3REF: OS Interface Open File Handle
1710**
1711** An [sqlite3_file] object represents an open file in the
1712** [sqlite3_vfs | OS interface layer].  Individual OS interface
1713** implementations will
1714** want to subclass this object by appending additional fields
1715** for their own use.  The pMethods entry is a pointer to an
1716** [sqlite3_io_methods] object that defines methods for performing
1717** I/O operations on the open file.
1718*/
1719typedef struct sqlite3_file sqlite3_file;
1720struct sqlite3_file {
1721  const struct sqlite3_io_methods *pMethods;  /* Methods for an open file */
1722};
1723
1724/*
1725** CAPI3REF: OS Interface File Virtual Methods Object
1726**
1727** Every file opened by the [sqlite3_vfs.xOpen] method populates an
1728** [sqlite3_file] object (or, more commonly, a subclass of the
1729** [sqlite3_file] object) with a pointer to an instance of this object.
1730** This object defines the methods used to perform various operations
1731** against the open file represented by the [sqlite3_file] object.
1732**
1733** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
1734** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1735** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed.  The
1736** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
1737** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
1738** to NULL.
1739**
1740** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1741** [SQLITE_SYNC_FULL].  The first choice is the normal fsync().
1742** The second choice is a Mac OS X style fullsync.  The [SQLITE_SYNC_DATAONLY]
1743** flag may be ORed in to indicate that only the data of the file
1744** and not its inode needs to be synced.
1745**
1746** The integer values to xLock() and xUnlock() are one of
1747** <ul>
1748** <li> [SQLITE_LOCK_NONE],
1749** <li> [SQLITE_LOCK_SHARED],
1750** <li> [SQLITE_LOCK_RESERVED],
1751** <li> [SQLITE_LOCK_PENDING], or
1752** <li> [SQLITE_LOCK_EXCLUSIVE].
1753** </ul>
1754** xLock() increases the lock. xUnlock() decreases the lock.
1755** The xCheckReservedLock() method checks whether any database connection,
1756** either in this process or in some other process, is holding a RESERVED,
1757** PENDING, or EXCLUSIVE lock on the file.  It returns true
1758** if such a lock exists and false otherwise.
1759**
1760** The xFileControl() method is a generic interface that allows custom
1761** VFS implementations to directly control an open file using the
1762** [sqlite3_file_control()] interface.  The second "op" argument is an
1763** integer opcode.  The third argument is a generic pointer intended to
1764** point to a structure that may contain arguments or space in which to
1765** write return values.  Potential uses for xFileControl() might be
1766** functions to enable blocking locks with timeouts, to change the
1767** locking strategy (for example to use dot-file locks), to inquire
1768** about the status of a lock, or to break stale locks.  The SQLite
1769** core reserves all opcodes less than 100 for its own use.
1770** A [file control opcodes | list of opcodes] less than 100 is available.
1771** Applications that define a custom xFileControl method should use opcodes
1772** greater than 100 to avoid conflicts.  VFS implementations should
1773** return [SQLITE_NOTFOUND] for file control opcodes that they do not
1774** recognize.
1775**
1776** The xSectorSize() method returns the sector size of the
1777** device that underlies the file.  The sector size is the
1778** minimum write that can be performed without disturbing
1779** other bytes in the file.  The xDeviceCharacteristics()
1780** method returns a bit vector describing behaviors of the
1781** underlying device:
1782**
1783** <ul>
1784** <li> [SQLITE_IOCAP_ATOMIC]
1785** <li> [SQLITE_IOCAP_ATOMIC512]
1786** <li> [SQLITE_IOCAP_ATOMIC1K]
1787** <li> [SQLITE_IOCAP_ATOMIC2K]
1788** <li> [SQLITE_IOCAP_ATOMIC4K]
1789** <li> [SQLITE_IOCAP_ATOMIC8K]
1790** <li> [SQLITE_IOCAP_ATOMIC16K]
1791** <li> [SQLITE_IOCAP_ATOMIC32K]
1792** <li> [SQLITE_IOCAP_ATOMIC64K]
1793** <li> [SQLITE_IOCAP_SAFE_APPEND]
1794** <li> [SQLITE_IOCAP_SEQUENTIAL]
1795** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
1796** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
1797** <li> [SQLITE_IOCAP_IMMUTABLE]
1798** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
1799** </ul>
1800**
1801** The SQLITE_IOCAP_ATOMIC property means that all writes of
1802** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
1803** mean that writes of blocks that are nnn bytes in size and
1804** are aligned to an address which is an integer multiple of
1805** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
1806** that when data is appended to a file, the data is appended
1807** first then the size of the file is extended, never the other
1808** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
1809** information is written to disk in the same order as calls
1810** to xWrite().
1811**
1812** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
1813** in the unread portions of the buffer with zeros.  A VFS that
1814** fails to zero-fill short reads might seem to work.  However,
1815** failure to zero-fill short reads will eventually lead to
1816** database corruption.
1817*/
1818typedef struct sqlite3_io_methods sqlite3_io_methods;
1819struct sqlite3_io_methods {
1820  int iVersion;
1821  int (*xClose)(sqlite3_file*);
1822  int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
1823  int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
1824  int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
1825  int (*xSync)(sqlite3_file*, int flags);
1826  int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
1827  int (*xLock)(sqlite3_file*, int);
1828  int (*xUnlock)(sqlite3_file*, int);
1829  int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
1830  int (*xFileControl)(sqlite3_file*, int op, void *pArg);
1831  int (*xSectorSize)(sqlite3_file*);
1832  int (*xDeviceCharacteristics)(sqlite3_file*);
1833  /* Methods above are valid for version 1 */
1834  int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
1835  int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1836  void (*xShmBarrier)(sqlite3_file*);
1837  int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
1838  /* Methods above are valid for version 2 */
1839  int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
1840  int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
1841  /* Methods above are valid for version 3 */
1842  /* Additional methods may be added in future releases */
1843};
1844
1845/*
1846** CAPI3REF: Standard File Control Opcodes
1847** KEYWORDS: {file control opcodes} {file control opcode}
1848**
1849** These integer constants are opcodes for the xFileControl method
1850** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
1851** interface.
1852**
1853** <ul>
1854** <li>[[SQLITE_FCNTL_LOCKSTATE]]
1855** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
1856** opcode causes the xFileControl method to write the current state of
1857** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1858** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1859** into an integer that the pArg argument points to. This capability
1860** is used during testing and is only available when the SQLITE_TEST
1861** compile-time option is used.
1862**
1863** <li>[[SQLITE_FCNTL_SIZE_HINT]]
1864** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1865** layer a hint of how large the database file will grow to be during the
1866** current transaction.  This hint is not guaranteed to be accurate but it
1867** is often close.  The underlying VFS might choose to preallocate database
1868** file space based on this hint in order to help writes to the database
1869** file run faster.
1870**
1871** <li>[[SQLITE_FCNTL_SIZE_LIMIT]]
1872** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
1873** implements [sqlite3_deserialize()] to set an upper bound on the size
1874** of the in-memory database.  The argument is a pointer to a [sqlite3_int64].
1875** If the integer pointed to is negative, then it is filled in with the
1876** current limit.  Otherwise the limit is set to the larger of the value
1877** of the integer pointed to and the current database size.  The integer
1878** pointed to is set to the new limit.
1879**
1880** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
1881** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
1882** extends and truncates the database file in chunks of a size specified
1883** by the user. The fourth argument to [sqlite3_file_control()] should
1884** point to an integer (type int) containing the new chunk-size to use
1885** for the nominated database. Allocating database file space in large
1886** chunks (say 1MB at a time), may reduce file-system fragmentation and
1887** improve performance on some systems.
1888**
1889** <li>[[SQLITE_FCNTL_FILE_POINTER]]
1890** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1891** to the [sqlite3_file] object associated with a particular database
1892** connection.  See also [SQLITE_FCNTL_JOURNAL_POINTER].
1893**
1894** <li>[[SQLITE_FCNTL_JOURNAL_POINTER]]
1895** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer
1896** to the [sqlite3_file] object associated with the journal file (either
1897** the [rollback journal] or the [write-ahead log]) for a particular database
1898** connection.  See also [SQLITE_FCNTL_FILE_POINTER].
1899**
1900** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
1901** No longer in use.
1902**
1903** <li>[[SQLITE_FCNTL_SYNC]]
1904** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
1905** sent to the VFS immediately before the xSync method is invoked on a
1906** database file descriptor. Or, if the xSync method is not invoked
1907** because the user has configured SQLite with
1908** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place
1909** of the xSync method. In most cases, the pointer argument passed with
1910** this file-control is NULL. However, if the database file is being synced
1911** as part of a multi-database commit, the argument points to a nul-terminated
1912** string containing the transactions master-journal file name. VFSes that
1913** do not need this signal should silently ignore this opcode. Applications
1914** should not call [sqlite3_file_control()] with this opcode as doing so may
1915** disrupt the operation of the specialized VFSes that do require it.
1916**
1917** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
1918** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
1919** and sent to the VFS after a transaction has been committed immediately
1920** but before the database is unlocked. VFSes that do not need this signal
1921** should silently ignore this opcode. Applications should not call
1922** [sqlite3_file_control()] with this opcode as doing so may disrupt the
1923** operation of the specialized VFSes that do require it.
1924**
1925** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
1926** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
1927** retry counts and intervals for certain disk I/O operations for the
1928** windows [VFS] in order to provide robustness in the presence of
1929** anti-virus programs.  By default, the windows VFS will retry file read,
1930** file write, and file delete operations up to 10 times, with a delay
1931** of 25 milliseconds before the first retry and with the delay increasing
1932** by an additional 25 milliseconds with each subsequent retry.  This
1933** opcode allows these two values (10 retries and 25 milliseconds of delay)
1934** to be adjusted.  The values are changed for all database connections
1935** within the same process.  The argument is a pointer to an array of two
1936** integers where the first integer is the new retry count and the second
1937** integer is the delay.  If either integer is negative, then the setting
1938** is not changed but instead the prior value of that setting is written
1939** into the array entry, allowing the current retry settings to be
1940** interrogated.  The zDbName parameter is ignored.
1941**
1942** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
1943** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
1944** persistent [WAL | Write Ahead Log] setting.  By default, the auxiliary
1945** write ahead log ([WAL file]) and shared memory
1946** files used for transaction control
1947** are automatically deleted when the latest connection to the database
1948** closes.  Setting persistent WAL mode causes those files to persist after
1949** close.  Persisting the files is useful when other processes that do not
1950** have write permission on the directory containing the database file want
1951** to read the database file, as the WAL and shared memory files must exist
1952** in order for the database to be readable.  The fourth parameter to
1953** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1954** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1955** WAL mode.  If the integer is -1, then it is overwritten with the current
1956** WAL persistence setting.
1957**
1958** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
1959** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
1960** persistent "powersafe-overwrite" or "PSOW" setting.  The PSOW setting
1961** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
1962** xDeviceCharacteristics methods. The fourth parameter to
1963** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1964** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1965** mode.  If the integer is -1, then it is overwritten with the current
1966** zero-damage mode setting.
1967**
1968** <li>[[SQLITE_FCNTL_OVERWRITE]]
1969** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
1970** a write transaction to indicate that, unless it is rolled back for some
1971** reason, the entire database file will be overwritten by the current
1972** transaction. This is used by VACUUM operations.
1973**
1974** <li>[[SQLITE_FCNTL_VFSNAME]]
1975** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
1976** all [VFSes] in the VFS stack.  The names are of all VFS shims and the
1977** final bottom-level VFS are written into memory obtained from
1978** [sqlite3_malloc()] and the result is stored in the char* variable
1979** that the fourth parameter of [sqlite3_file_control()] points to.
1980** The caller is responsible for freeing the memory when done.  As with
1981** all file-control actions, there is no guarantee that this will actually
1982** do anything.  Callers should initialize the char* variable to a NULL
1983** pointer in case this file-control is not implemented.  This file-control
1984** is intended for diagnostic use only.
1985**
1986** <li>[[SQLITE_FCNTL_VFS_POINTER]]
1987** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1988** [VFSes] currently in use.  ^(The argument X in
1989** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be
1990** of type "[sqlite3_vfs] **".  This opcodes will set *X
1991** to a pointer to the top-level VFS.)^
1992** ^When there are multiple VFS shims in the stack, this opcode finds the
1993** upper-most shim only.
1994**
1995** <li>[[SQLITE_FCNTL_PRAGMA]]
1996** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA]
1997** file control is sent to the open [sqlite3_file] object corresponding
1998** to the database file to which the pragma statement refers. ^The argument
1999** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
2000** pointers to strings (char**) in which the second element of the array
2001** is the name of the pragma and the third element is the argument to the
2002** pragma or NULL if the pragma has no argument.  ^The handler for an
2003** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
2004** of the char** argument point to a string obtained from [sqlite3_mprintf()]
2005** or the equivalent and that string will become the result of the pragma or
2006** the error message if the pragma fails. ^If the
2007** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal
2008** [PRAGMA] processing continues.  ^If the [SQLITE_FCNTL_PRAGMA]
2009** file control returns [SQLITE_OK], then the parser assumes that the
2010** VFS has handled the PRAGMA itself and the parser generates a no-op
2011** prepared statement if result string is NULL, or that returns a copy
2012** of the result string if the string is non-NULL.
2013** ^If the [SQLITE_FCNTL_PRAGMA] file control returns
2014** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
2015** that the VFS encountered an error while handling the [PRAGMA] and the
2016** compilation of the PRAGMA fails with an error.  ^The [SQLITE_FCNTL_PRAGMA]
2017** file control occurs at the beginning of pragma statement analysis and so
2018** it is able to override built-in [PRAGMA] statements.
2019**
2020** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
2021** ^The [SQLITE_FCNTL_BUSYHANDLER]
2022** file-control may be invoked by SQLite on the database file handle
2023** shortly after it is opened in order to provide a custom VFS with access
2024** to the connection's busy-handler callback. The argument is of type (void**)
2025** - an array of two (void *) values. The first (void *) actually points
2026** to a function of type (int (*)(void *)). In order to invoke the connection's
2027** busy-handler, this function should be invoked with the second (void *) in
2028** the array as the only argument. If it returns non-zero, then the operation
2029** should be retried. If it returns zero, the custom VFS should abandon the
2030** current operation.
2031**
2032** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
2033** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
2034** to have SQLite generate a
2035** temporary filename using the same algorithm that is followed to generate
2036** temporary filenames for TEMP tables and other internal uses.  The
2037** argument should be a char** which will be filled with the filename
2038** written into memory obtained from [sqlite3_malloc()].  The caller should
2039** invoke [sqlite3_free()] on the result to avoid a memory leak.
2040**
2041** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
2042** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
2043** maximum number of bytes that will be used for memory-mapped I/O.
2044** The argument is a pointer to a value of type sqlite3_int64 that
2045** is an advisory maximum number of bytes in the file to memory map.  The
2046** pointer is overwritten with the old value.  The limit is not changed if
2047** the value originally pointed to is negative, and so the current limit
2048** can be queried by passing in a pointer to a negative number.  This
2049** file-control is used internally to implement [PRAGMA mmap_size].
2050**
2051** <li>[[SQLITE_FCNTL_TRACE]]
2052** The [SQLITE_FCNTL_TRACE] file control provides advisory information
2053** to the VFS about what the higher layers of the SQLite stack are doing.
2054** This file control is used by some VFS activity tracing [shims].
2055** The argument is a zero-terminated string.  Higher layers in the
2056** SQLite stack may generate instances of this file control if
2057** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
2058**
2059** <li>[[SQLITE_FCNTL_HAS_MOVED]]
2060** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
2061** pointer to an integer and it writes a boolean into that integer depending
2062** on whether or not the file has been renamed, moved, or deleted since it
2063** was first opened.
2064**
2065** <li>[[SQLITE_FCNTL_WIN32_GET_HANDLE]]
2066** The [SQLITE_FCNTL_WIN32_GET_HANDLE] opcode can be used to obtain the
2067** underlying native file handle associated with a file handle.  This file
2068** control interprets its argument as a pointer to a native file handle and
2069** writes the resulting value there.
2070**
2071** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
2072** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging.  This
2073** opcode causes the xFileControl method to swap the file handle with the one
2074** pointed to by the pArg argument.  This capability is used during testing
2075** and only needs to be supported when SQLITE_TEST is defined.
2076**
2077** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
2078** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
2079** be advantageous to block on the next WAL lock if the lock is not immediately
2080** available.  The WAL subsystem issues this signal during rare
2081** circumstances in order to fix a problem with priority inversion.
2082** Applications should <em>not</em> use this file-control.
2083**
2084** <li>[[SQLITE_FCNTL_ZIPVFS]]
2085** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
2086** VFS should return SQLITE_NOTFOUND for this opcode.
2087**
2088** <li>[[SQLITE_FCNTL_RBU]]
2089** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
2090** the RBU extension only.  All other VFS should return SQLITE_NOTFOUND for
2091** this opcode.
2092**
2093** <li>[[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]]
2094** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then
2095** the file descriptor is placed in "batch write mode", which
2096** means all subsequent write operations will be deferred and done
2097** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].  Systems
2098** that do not support batch atomic writes will return SQLITE_NOTFOUND.
2099** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to
2100** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or
2101** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make
2102** no VFS interface calls on the same [sqlite3_file] file descriptor
2103** except for calls to the xWrite method and the xFileControl method
2104** with [SQLITE_FCNTL_SIZE_HINT].
2105**
2106** <li>[[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]]
2107** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write
2108** operations since the previous successful call to
2109** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically.
2110** This file control returns [SQLITE_OK] if and only if the writes were
2111** all performed successfully and have been committed to persistent storage.
2112** ^Regardless of whether or not it is successful, this file control takes
2113** the file descriptor out of batch write mode so that all subsequent
2114** write operations are independent.
2115** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without
2116** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
2117**
2118** <li>[[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]]
2119** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
2120** operations since the previous successful call to
2121** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
2122** ^This file control takes the file descriptor out of batch write mode
2123** so that all subsequent write operations are independent.
2124** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
2125** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
2126**
2127** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
2128** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode is used to configure a VFS
2129** to block for up to M milliseconds before failing when attempting to
2130** obtain a file lock using the xLock or xShmLock methods of the VFS.
2131** The parameter is a pointer to a 32-bit signed integer that contains
2132** the value that M is to be set to. Before returning, the 32-bit signed
2133** integer is overwritten with the previous value of M.
2134**
2135** <li>[[SQLITE_FCNTL_DATA_VERSION]]
2136** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
2137** a database file.  The argument is a pointer to a 32-bit unsigned integer.
2138** The "data version" for the pager is written into the pointer.  The
2139** "data version" changes whenever any change occurs to the corresponding
2140** database file, either through SQL statements on the same database
2141** connection or through transactions committed by separate database
2142** connections possibly in other processes. The [sqlite3_total_changes()]
2143** interface can be used to find if any database on the connection has changed,
2144** but that interface responds to changes on TEMP as well as MAIN and does
2145** not provide a mechanism to detect changes to MAIN only.  Also, the
2146** [sqlite3_total_changes()] interface responds to internal changes only and
2147** omits changes made by other database connections.  The
2148** [PRAGMA data_version] command provides a mechanism to detect changes to
2149** a single attached database that occur due to other database connections,
2150** but omits changes implemented by the database connection on which it is
2151** called.  This file control is the only mechanism to detect changes that
2152** happen either internally or externally and that are associated with
2153** a particular attached database.
2154**
2155** <li>[[SQLITE_FCNTL_CKPT_START]]
2156** The [SQLITE_FCNTL_CKPT_START] opcode is invoked from within a checkpoint
2157** in wal mode before the client starts to copy pages from the wal
2158** file to the database file.
2159**
2160** <li>[[SQLITE_FCNTL_CKPT_DONE]]
2161** The [SQLITE_FCNTL_CKPT_DONE] opcode is invoked from within a checkpoint
2162** in wal mode after the client has finished copying pages from the wal
2163** file to the database file, but before the *-shm file is updated to
2164** record the fact that the pages have been checkpointed.
2165** </ul>
2166*/
2167#define SQLITE_FCNTL_LOCKSTATE               1
2168#define SQLITE_FCNTL_GET_LOCKPROXYFILE       2
2169#define SQLITE_FCNTL_SET_LOCKPROXYFILE       3
2170#define SQLITE_FCNTL_LAST_ERRNO              4
2171#define SQLITE_FCNTL_SIZE_HINT               5
2172#define SQLITE_FCNTL_CHUNK_SIZE              6
2173#define SQLITE_FCNTL_FILE_POINTER            7
2174#define SQLITE_FCNTL_SYNC_OMITTED            8
2175#define SQLITE_FCNTL_WIN32_AV_RETRY          9
2176#define SQLITE_FCNTL_PERSIST_WAL            10
2177#define SQLITE_FCNTL_OVERWRITE              11
2178#define SQLITE_FCNTL_VFSNAME                12
2179#define SQLITE_FCNTL_POWERSAFE_OVERWRITE    13
2180#define SQLITE_FCNTL_PRAGMA                 14
2181#define SQLITE_FCNTL_BUSYHANDLER            15
2182#define SQLITE_FCNTL_TEMPFILENAME           16
2183#define SQLITE_FCNTL_MMAP_SIZE              18
2184#define SQLITE_FCNTL_TRACE                  19
2185#define SQLITE_FCNTL_HAS_MOVED              20
2186#define SQLITE_FCNTL_SYNC                   21
2187#define SQLITE_FCNTL_COMMIT_PHASETWO        22
2188#define SQLITE_FCNTL_WIN32_SET_HANDLE       23
2189#define SQLITE_FCNTL_WAL_BLOCK              24
2190#define SQLITE_FCNTL_ZIPVFS                 25
2191#define SQLITE_FCNTL_RBU                    26
2192#define SQLITE_FCNTL_VFS_POINTER            27
2193#define SQLITE_FCNTL_JOURNAL_POINTER        28
2194#define SQLITE_FCNTL_WIN32_GET_HANDLE       29
2195#define SQLITE_FCNTL_PDB                    30
2196#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE     31
2197#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE    32
2198#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE  33
2199#define SQLITE_FCNTL_LOCK_TIMEOUT           34
2200#define SQLITE_FCNTL_DATA_VERSION           35
2201#define SQLITE_FCNTL_SIZE_LIMIT             36
2202#define SQLITE_FCNTL_CKPT_DONE              37
2203#define SQLITE_FCNTL_RESERVE_BYTES          38
2204#define SQLITE_FCNTL_CKPT_START             39
2205
2206/* deprecated names */
2207#define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
2208#define SQLITE_SET_LOCKPROXYFILE      SQLITE_FCNTL_SET_LOCKPROXYFILE
2209#define SQLITE_LAST_ERRNO             SQLITE_FCNTL_LAST_ERRNO
2210
2211
2212/*
2213** CAPI3REF: Mutex Handle
2214**
2215** The mutex module within SQLite defines [sqlite3_mutex] to be an
2216** abstract type for a mutex object.  The SQLite core never looks
2217** at the internal representation of an [sqlite3_mutex].  It only
2218** deals with pointers to the [sqlite3_mutex] object.
2219**
2220** Mutexes are created using [sqlite3_mutex_alloc()].
2221*/
2222typedef struct sqlite3_mutex sqlite3_mutex;
2223
2224/*
2225** CAPI3REF: Loadable Extension Thunk
2226**
2227** A pointer to the opaque sqlite3_api_routines structure is passed as
2228** the third parameter to entry points of [loadable extensions].  This
2229** structure must be typedefed in order to work around compiler warnings
2230** on some platforms.
2231*/
2232typedef struct sqlite3_api_routines sqlite3_api_routines;
2233
2234/*
2235** CAPI3REF: OS Interface Object
2236**
2237** An instance of the sqlite3_vfs object defines the interface between
2238** the SQLite core and the underlying operating system.  The "vfs"
2239** in the name of the object stands for "virtual file system".  See
2240** the [VFS | VFS documentation] for further information.
2241**
2242** The VFS interface is sometimes extended by adding new methods onto
2243** the end.  Each time such an extension occurs, the iVersion field
2244** is incremented.  The iVersion value started out as 1 in
2245** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2
2246** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased
2247** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6].  Additional fields
2248** may be appended to the sqlite3_vfs object and the iVersion value
2249** may increase again in future versions of SQLite.
2250** Note that due to an oversight, the structure
2251** of the sqlite3_vfs object changed in the transition from
2252** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0]
2253** and yet the iVersion field was not increased.
2254**
2255** The szOsFile field is the size of the subclassed [sqlite3_file]
2256** structure used by this VFS.  mxPathname is the maximum length of
2257** a pathname in this VFS.
2258**
2259** Registered sqlite3_vfs objects are kept on a linked list formed by
2260** the pNext pointer.  The [sqlite3_vfs_register()]
2261** and [sqlite3_vfs_unregister()] interfaces manage this list
2262** in a thread-safe way.  The [sqlite3_vfs_find()] interface
2263** searches the list.  Neither the application code nor the VFS
2264** implementation should use the pNext pointer.
2265**
2266** The pNext field is the only field in the sqlite3_vfs
2267** structure that SQLite will ever modify.  SQLite will only access
2268** or modify this field while holding a particular static mutex.
2269** The application should never modify anything within the sqlite3_vfs
2270** object once the object has been registered.
2271**
2272** The zName field holds the name of the VFS module.  The name must
2273** be unique across all VFS modules.
2274**
2275** [[sqlite3_vfs.xOpen]]
2276** ^SQLite guarantees that the zFilename parameter to xOpen
2277** is either a NULL pointer or string obtained
2278** from xFullPathname() with an optional suffix added.
2279** ^If a suffix is added to the zFilename parameter, it will
2280** consist of a single "-" character followed by no more than
2281** 11 alphanumeric and/or "-" characters.
2282** ^SQLite further guarantees that
2283** the string will be valid and unchanged until xClose() is
2284** called. Because of the previous sentence,
2285** the [sqlite3_file] can safely store a pointer to the
2286** filename if it needs to remember the filename for some reason.
2287** If the zFilename parameter to xOpen is a NULL pointer then xOpen
2288** must invent its own temporary name for the file.  ^Whenever the
2289** xFilename parameter is NULL it will also be the case that the
2290** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
2291**
2292** The flags argument to xOpen() includes all bits set in
2293** the flags argument to [sqlite3_open_v2()].  Or if [sqlite3_open()]
2294** or [sqlite3_open16()] is used, then flags includes at least
2295** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
2296** If xOpen() opens a file read-only then it sets *pOutFlags to
2297** include [SQLITE_OPEN_READONLY].  Other bits in *pOutFlags may be set.
2298**
2299** ^(SQLite will also add one of the following flags to the xOpen()
2300** call, depending on the object being opened:
2301**
2302** <ul>
2303** <li>  [SQLITE_OPEN_MAIN_DB]
2304** <li>  [SQLITE_OPEN_MAIN_JOURNAL]
2305** <li>  [SQLITE_OPEN_TEMP_DB]
2306** <li>  [SQLITE_OPEN_TEMP_JOURNAL]
2307** <li>  [SQLITE_OPEN_TRANSIENT_DB]
2308** <li>  [SQLITE_OPEN_SUBJOURNAL]
2309** <li>  [SQLITE_OPEN_MASTER_JOURNAL]
2310** <li>  [SQLITE_OPEN_WAL]
2311** </ul>)^
2312**
2313** The file I/O implementation can use the object type flags to
2314** change the way it deals with files.  For example, an application
2315** that does not care about crash recovery or rollback might make
2316** the open of a journal file a no-op.  Writes to this journal would
2317** also be no-ops, and any attempt to read the journal would return
2318** SQLITE_IOERR.  Or the implementation might recognize that a database
2319** file will be doing page-aligned sector reads and writes in a random
2320** order and set up its I/O subsystem accordingly.
2321**
2322** SQLite might also add one of the following flags to the xOpen method:
2323**
2324** <ul>
2325** <li> [SQLITE_OPEN_DELETEONCLOSE]
2326** <li> [SQLITE_OPEN_EXCLUSIVE]
2327** </ul>
2328**
2329** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
2330** deleted when it is closed.  ^The [SQLITE_OPEN_DELETEONCLOSE]
2331** will be set for TEMP databases and their journals, transient
2332** databases, and subjournals.
2333**
2334** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
2335** with the [SQLITE_OPEN_CREATE] flag, which are both directly
2336** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
2337** API.  The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
2338** SQLITE_OPEN_CREATE, is used to indicate that file should always
2339** be created, and that it is an error if it already exists.
2340** It is <i>not</i> used to indicate the file should be opened
2341** for exclusive access.
2342**
2343** ^At least szOsFile bytes of memory are allocated by SQLite
2344** to hold the [sqlite3_file] structure passed as the third
2345** argument to xOpen.  The xOpen method does not have to
2346** allocate the structure; it should just fill it in.  Note that
2347** the xOpen method must set the sqlite3_file.pMethods to either
2348** a valid [sqlite3_io_methods] object or to NULL.  xOpen must do
2349** this even if the open fails.  SQLite expects that the sqlite3_file.pMethods
2350** element will be valid after xOpen returns regardless of the success
2351** or failure of the xOpen call.
2352**
2353** [[sqlite3_vfs.xAccess]]
2354** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
2355** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
2356** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
2357** to test whether a file is at least readable.  The SQLITE_ACCESS_READ
2358** flag is never actually used and is not implemented in the built-in
2359** VFSes of SQLite.  The file is named by the second argument and can be a
2360** directory. The xAccess method returns [SQLITE_OK] on success or some
2361** non-zero error code if there is an I/O error or if the name of
2362** the file given in the second argument is illegal.  If SQLITE_OK
2363** is returned, then non-zero or zero is written into *pResOut to indicate
2364** whether or not the file is accessible.
2365**
2366** ^SQLite will always allocate at least mxPathname+1 bytes for the
2367** output buffer xFullPathname.  The exact size of the output buffer
2368** is also passed as a parameter to both  methods. If the output buffer
2369** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
2370** handled as a fatal error by SQLite, vfs implementations should endeavor
2371** to prevent this by setting mxPathname to a sufficiently large value.
2372**
2373** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
2374** interfaces are not strictly a part of the filesystem, but they are
2375** included in the VFS structure for completeness.
2376** The xRandomness() function attempts to return nBytes bytes
2377** of good-quality randomness into zOut.  The return value is
2378** the actual number of bytes of randomness obtained.
2379** The xSleep() method causes the calling thread to sleep for at
2380** least the number of microseconds given.  ^The xCurrentTime()
2381** method returns a Julian Day Number for the current date and time as
2382** a floating point value.
2383** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
2384** Day Number multiplied by 86400000 (the number of milliseconds in
2385** a 24-hour day).
2386** ^SQLite will use the xCurrentTimeInt64() method to get the current
2387** date and time if that method is available (if iVersion is 2 or
2388** greater and the function pointer is not NULL) and will fall back
2389** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
2390**
2391** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
2392** are not used by the SQLite core.  These optional interfaces are provided
2393** by some VFSes to facilitate testing of the VFS code. By overriding
2394** system calls with functions under its control, a test program can
2395** simulate faults and error conditions that would otherwise be difficult
2396** or impossible to induce.  The set of system calls that can be overridden
2397** varies from one VFS to another, and from one version of the same VFS to the
2398** next.  Applications that use these interfaces must be prepared for any
2399** or all of these interfaces to be NULL or for their behavior to change
2400** from one release to the next.  Applications must not attempt to access
2401** any of these methods if the iVersion of the VFS is less than 3.
2402*/
2403typedef struct sqlite3_vfs sqlite3_vfs;
2404typedef void (*sqlite3_syscall_ptr)(void);
2405struct sqlite3_vfs {
2406  int iVersion;            /* Structure version number (currently 3) */
2407  int szOsFile;            /* Size of subclassed sqlite3_file */
2408  int mxPathname;          /* Maximum file pathname length */
2409  sqlite3_vfs *pNext;      /* Next registered VFS */
2410  const char *zName;       /* Name of this virtual file system */
2411  void *pAppData;          /* Pointer to application-specific data */
2412  int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
2413               int flags, int *pOutFlags);
2414  int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
2415  int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
2416  int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
2417  void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
2418  void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
2419  void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
2420  void (*xDlClose)(sqlite3_vfs*, void*);
2421  int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
2422  int (*xSleep)(sqlite3_vfs*, int microseconds);
2423  int (*xCurrentTime)(sqlite3_vfs*, double*);
2424  int (*xGetLastError)(sqlite3_vfs*, int, char *);
2425  /*
2426  ** The methods above are in version 1 of the sqlite_vfs object
2427  ** definition.  Those that follow are added in version 2 or later
2428  */
2429  int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
2430  /*
2431  ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
2432  ** Those below are for version 3 and greater.
2433  */
2434  int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
2435  sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
2436  const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
2437  /*
2438  ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
2439  ** New fields may be appended in future versions.  The iVersion
2440  ** value will increment whenever this happens.
2441  */
2442};
2443
2444/*
2445** CAPI3REF: Flags for the xAccess VFS method
2446**
2447** These integer constants can be used as the third parameter to
2448** the xAccess method of an [sqlite3_vfs] object.  They determine
2449** what kind of permissions the xAccess method is looking for.
2450** With SQLITE_ACCESS_EXISTS, the xAccess method
2451** simply checks whether the file exists.
2452** With SQLITE_ACCESS_READWRITE, the xAccess method
2453** checks whether the named directory is both readable and writable
2454** (in other words, if files can be added, removed, and renamed within
2455** the directory).
2456** The SQLITE_ACCESS_READWRITE constant is currently used only by the
2457** [temp_store_directory pragma], though this could change in a future
2458** release of SQLite.
2459** With SQLITE_ACCESS_READ, the xAccess method
2460** checks whether the file is readable.  The SQLITE_ACCESS_READ constant is
2461** currently unused, though it might be used in a future release of
2462** SQLite.
2463*/
2464#define SQLITE_ACCESS_EXISTS    0
2465#define SQLITE_ACCESS_READWRITE 1   /* Used by PRAGMA temp_store_directory */
2466#define SQLITE_ACCESS_READ      2   /* Unused */
2467
2468/*
2469** CAPI3REF: Flags for the xShmLock VFS method
2470**
2471** These integer constants define the various locking operations
2472** allowed by the xShmLock method of [sqlite3_io_methods].  The
2473** following are the only legal combinations of flags to the
2474** xShmLock method:
2475**
2476** <ul>
2477** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
2478** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
2479** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
2480** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
2481** </ul>
2482**
2483** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
2484** was given on the corresponding lock.
2485**
2486** The xShmLock method can transition between unlocked and SHARED or
2487** between unlocked and EXCLUSIVE.  It cannot transition between SHARED
2488** and EXCLUSIVE.
2489*/
2490#define SQLITE_SHM_UNLOCK       1
2491#define SQLITE_SHM_LOCK         2
2492#define SQLITE_SHM_SHARED       4
2493#define SQLITE_SHM_EXCLUSIVE    8
2494
2495/*
2496** CAPI3REF: Maximum xShmLock index
2497**
2498** The xShmLock method on [sqlite3_io_methods] may use values
2499** between 0 and this upper bound as its "offset" argument.
2500** The SQLite core will never attempt to acquire or release a
2501** lock outside of this range
2502*/
2503#define SQLITE_SHM_NLOCK        8
2504
2505
2506/*
2507** CAPI3REF: Initialize The SQLite Library
2508**
2509** ^The sqlite3_initialize() routine initializes the
2510** SQLite library.  ^The sqlite3_shutdown() routine
2511** deallocates any resources that were allocated by sqlite3_initialize().
2512** These routines are designed to aid in process initialization and
2513** shutdown on embedded systems.  Workstation applications using
2514** SQLite normally do not need to invoke either of these routines.
2515**
2516** A call to sqlite3_initialize() is an "effective" call if it is
2517** the first time sqlite3_initialize() is invoked during the lifetime of
2518** the process, or if it is the first time sqlite3_initialize() is invoked
2519** following a call to sqlite3_shutdown().  ^(Only an effective call
2520** of sqlite3_initialize() does any initialization.  All other calls
2521** are harmless no-ops.)^
2522**
2523** A call to sqlite3_shutdown() is an "effective" call if it is the first
2524** call to sqlite3_shutdown() since the last sqlite3_initialize().  ^(Only
2525** an effective call to sqlite3_shutdown() does any deinitialization.
2526** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
2527**
2528** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
2529** is not.  The sqlite3_shutdown() interface must only be called from a
2530** single thread.  All open [database connections] must be closed and all
2531** other SQLite resources must be deallocated prior to invoking
2532** sqlite3_shutdown().
2533**
2534** Among other things, ^sqlite3_initialize() will invoke
2535** sqlite3_os_init().  Similarly, ^sqlite3_shutdown()
2536** will invoke sqlite3_os_end().
2537**
2538** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
2539** ^If for some reason, sqlite3_initialize() is unable to initialize
2540** the library (perhaps it is unable to allocate a needed resource such
2541** as a mutex) it returns an [error code] other than [SQLITE_OK].
2542**
2543** ^The sqlite3_initialize() routine is called internally by many other
2544** SQLite interfaces so that an application usually does not need to
2545** invoke sqlite3_initialize() directly.  For example, [sqlite3_open()]
2546** calls sqlite3_initialize() so the SQLite library will be automatically
2547** initialized when [sqlite3_open()] is called if it has not be initialized
2548** already.  ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
2549** compile-time option, then the automatic calls to sqlite3_initialize()
2550** are omitted and the application must call sqlite3_initialize() directly
2551** prior to using any other SQLite interface.  For maximum portability,
2552** it is recommended that applications always invoke sqlite3_initialize()
2553** directly prior to using any other SQLite interface.  Future releases
2554** of SQLite may require this.  In other words, the behavior exhibited
2555** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
2556** default behavior in some future release of SQLite.
2557**
2558** The sqlite3_os_init() routine does operating-system specific
2559** initialization of the SQLite library.  The sqlite3_os_end()
2560** routine undoes the effect of sqlite3_os_init().  Typical tasks
2561** performed by these routines include allocation or deallocation
2562** of static resources, initialization of global variables,
2563** setting up a default [sqlite3_vfs] module, or setting up
2564** a default configuration using [sqlite3_config()].
2565**
2566** The application should never invoke either sqlite3_os_init()
2567** or sqlite3_os_end() directly.  The application should only invoke
2568** sqlite3_initialize() and sqlite3_shutdown().  The sqlite3_os_init()
2569** interface is called automatically by sqlite3_initialize() and
2570** sqlite3_os_end() is called by sqlite3_shutdown().  Appropriate
2571** implementations for sqlite3_os_init() and sqlite3_os_end()
2572** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
2573** When [custom builds | built for other platforms]
2574** (using the [SQLITE_OS_OTHER=1] compile-time
2575** option) the application must supply a suitable implementation for
2576** sqlite3_os_init() and sqlite3_os_end().  An application-supplied
2577** implementation of sqlite3_os_init() or sqlite3_os_end()
2578** must return [SQLITE_OK] on success and some other [error code] upon
2579** failure.
2580*/
2581SQLITE_API int sqlite3_initialize(void);
2582SQLITE_API int sqlite3_shutdown(void);
2583SQLITE_API int sqlite3_os_init(void);
2584SQLITE_API int sqlite3_os_end(void);
2585
2586/*
2587** CAPI3REF: Configuring The SQLite Library
2588**
2589** The sqlite3_config() interface is used to make global configuration
2590** changes to SQLite in order to tune SQLite to the specific needs of
2591** the application.  The default configuration is recommended for most
2592** applications and so this routine is usually not necessary.  It is
2593** provided to support rare applications with unusual needs.
2594**
2595** <b>The sqlite3_config() interface is not threadsafe. The application
2596** must ensure that no other SQLite interfaces are invoked by other
2597** threads while sqlite3_config() is running.</b>
2598**
2599** The sqlite3_config() interface
2600** may only be invoked prior to library initialization using
2601** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
2602** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
2603** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
2604** Note, however, that ^sqlite3_config() can be called as part of the
2605** implementation of an application-defined [sqlite3_os_init()].
2606**
2607** The first argument to sqlite3_config() is an integer
2608** [configuration option] that determines
2609** what property of SQLite is to be configured.  Subsequent arguments
2610** vary depending on the [configuration option]
2611** in the first argument.
2612**
2613** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
2614** ^If the option is unknown or SQLite is unable to set the option
2615** then this routine returns a non-zero [error code].
2616*/
2617SQLITE_API int sqlite3_config(int, ...);
2618
2619/*
2620** CAPI3REF: Configure database connections
2621** METHOD: sqlite3
2622**
2623** The sqlite3_db_config() interface is used to make configuration
2624** changes to a [database connection].  The interface is similar to
2625** [sqlite3_config()] except that the changes apply to a single
2626** [database connection] (specified in the first argument).
2627**
2628** The second argument to sqlite3_db_config(D,V,...)  is the
2629** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2630** that indicates what aspect of the [database connection] is being configured.
2631** Subsequent arguments vary depending on the configuration verb.
2632**
2633** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
2634** the call is considered successful.
2635*/
2636SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
2637
2638/*
2639** CAPI3REF: Memory Allocation Routines
2640**
2641** An instance of this object defines the interface between SQLite
2642** and low-level memory allocation routines.
2643**
2644** This object is used in only one place in the SQLite interface.
2645** A pointer to an instance of this object is the argument to
2646** [sqlite3_config()] when the configuration option is
2647** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
2648** By creating an instance of this object
2649** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
2650** during configuration, an application can specify an alternative
2651** memory allocation subsystem for SQLite to use for all of its
2652** dynamic memory needs.
2653**
2654** Note that SQLite comes with several [built-in memory allocators]
2655** that are perfectly adequate for the overwhelming majority of applications
2656** and that this object is only useful to a tiny minority of applications
2657** with specialized memory allocation requirements.  This object is
2658** also used during testing of SQLite in order to specify an alternative
2659** memory allocator that simulates memory out-of-memory conditions in
2660** order to verify that SQLite recovers gracefully from such
2661** conditions.
2662**
2663** The xMalloc, xRealloc, and xFree methods must work like the
2664** malloc(), realloc() and free() functions from the standard C library.
2665** ^SQLite guarantees that the second argument to
2666** xRealloc is always a value returned by a prior call to xRoundup.
2667**
2668** xSize should return the allocated size of a memory allocation
2669** previously obtained from xMalloc or xRealloc.  The allocated size
2670** is always at least as big as the requested size but may be larger.
2671**
2672** The xRoundup method returns what would be the allocated size of
2673** a memory allocation given a particular requested size.  Most memory
2674** allocators round up memory allocations at least to the next multiple
2675** of 8.  Some allocators round up to a larger multiple or to a power of 2.
2676** Every memory allocation request coming in through [sqlite3_malloc()]
2677** or [sqlite3_realloc()] first calls xRoundup.  If xRoundup returns 0,
2678** that causes the corresponding memory allocation to fail.
2679**
2680** The xInit method initializes the memory allocator.  For example,
2681** it might allocate any required mutexes or initialize internal data
2682** structures.  The xShutdown method is invoked (indirectly) by
2683** [sqlite3_shutdown()] and should deallocate any resources acquired
2684** by xInit.  The pAppData pointer is used as the only parameter to
2685** xInit and xShutdown.
2686**
2687** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
2688** the xInit method, so the xInit method need not be threadsafe.  The
2689** xShutdown method is only called from [sqlite3_shutdown()] so it does
2690** not need to be threadsafe either.  For all other methods, SQLite
2691** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
2692** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
2693** it is by default) and so the methods are automatically serialized.
2694** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
2695** methods must be threadsafe or else make their own arrangements for
2696** serialization.
2697**
2698** SQLite will never invoke xInit() more than once without an intervening
2699** call to xShutdown().
2700*/
2701typedef struct sqlite3_mem_methods sqlite3_mem_methods;
2702struct sqlite3_mem_methods {
2703  void *(*xMalloc)(int);         /* Memory allocation function */
2704  void (*xFree)(void*);          /* Free a prior allocation */
2705  void *(*xRealloc)(void*,int);  /* Resize an allocation */
2706  int (*xSize)(void*);           /* Return the size of an allocation */
2707  int (*xRoundup)(int);          /* Round up request size to allocation size */
2708  int (*xInit)(void*);           /* Initialize the memory allocator */
2709  void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
2710  void *pAppData;                /* Argument to xInit() and xShutdown() */
2711};
2712
2713/*
2714** CAPI3REF: Configuration Options
2715** KEYWORDS: {configuration option}
2716**
2717** These constants are the available integer configuration options that
2718** can be passed as the first argument to the [sqlite3_config()] interface.
2719**
2720** New configuration options may be added in future releases of SQLite.
2721** Existing configuration options might be discontinued.  Applications
2722** should check the return code from [sqlite3_config()] to make sure that
2723** the call worked.  The [sqlite3_config()] interface will return a
2724** non-zero [error code] if a discontinued or unsupported configuration option
2725** is invoked.
2726**
2727** <dl>
2728** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
2729** <dd>There are no arguments to this option.  ^This option sets the
2730** [threading mode] to Single-thread.  In other words, it disables
2731** all mutexing and puts SQLite into a mode where it can only be used
2732** by a single thread.   ^If SQLite is compiled with
2733** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2734** it is not possible to change the [threading mode] from its default
2735** value of Single-thread and so [sqlite3_config()] will return
2736** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
2737** configuration option.</dd>
2738**
2739** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
2740** <dd>There are no arguments to this option.  ^This option sets the
2741** [threading mode] to Multi-thread.  In other words, it disables
2742** mutexing on [database connection] and [prepared statement] objects.
2743** The application is responsible for serializing access to
2744** [database connections] and [prepared statements].  But other mutexes
2745** are enabled so that SQLite will be safe to use in a multi-threaded
2746** environment as long as no two threads attempt to use the same
2747** [database connection] at the same time.  ^If SQLite is compiled with
2748** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2749** it is not possible to set the Multi-thread [threading mode] and
2750** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2751** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
2752**
2753** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
2754** <dd>There are no arguments to this option.  ^This option sets the
2755** [threading mode] to Serialized. In other words, this option enables
2756** all mutexes including the recursive
2757** mutexes on [database connection] and [prepared statement] objects.
2758** In this mode (which is the default when SQLite is compiled with
2759** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
2760** to [database connections] and [prepared statements] so that the
2761** application is free to use the same [database connection] or the
2762** same [prepared statement] in different threads at the same time.
2763** ^If SQLite is compiled with
2764** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2765** it is not possible to set the Serialized [threading mode] and
2766** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2767** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
2768**
2769** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
2770** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
2771** a pointer to an instance of the [sqlite3_mem_methods] structure.
2772** The argument specifies
2773** alternative low-level memory allocation routines to be used in place of
2774** the memory allocation routines built into SQLite.)^ ^SQLite makes
2775** its own private copy of the content of the [sqlite3_mem_methods] structure
2776** before the [sqlite3_config()] call returns.</dd>
2777**
2778** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
2779** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
2780** is a pointer to an instance of the [sqlite3_mem_methods] structure.
2781** The [sqlite3_mem_methods]
2782** structure is filled with the currently defined memory allocation routines.)^
2783** This option can be used to overload the default memory allocation
2784** routines with a wrapper that simulations memory allocation failure or
2785** tracks memory usage, for example. </dd>
2786**
2787** [[SQLITE_CONFIG_SMALL_MALLOC]] <dt>SQLITE_CONFIG_SMALL_MALLOC</dt>
2788** <dd> ^The SQLITE_CONFIG_SMALL_MALLOC option takes single argument of
2789** type int, interpreted as a boolean, which if true provides a hint to
2790** SQLite that it should avoid large memory allocations if possible.
2791** SQLite will run faster if it is free to make large memory allocations,
2792** but some application might prefer to run slower in exchange for
2793** guarantees about memory fragmentation that are possible if large
2794** allocations are avoided.  This hint is normally off.
2795** </dd>
2796**
2797** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
2798** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
2799** interpreted as a boolean, which enables or disables the collection of
2800** memory allocation statistics. ^(When memory allocation statistics are
2801** disabled, the following SQLite interfaces become non-operational:
2802**   <ul>
2803**   <li> [sqlite3_hard_heap_limit64()]
2804**   <li> [sqlite3_memory_used()]
2805**   <li> [sqlite3_memory_highwater()]
2806**   <li> [sqlite3_soft_heap_limit64()]
2807**   <li> [sqlite3_status64()]
2808**   </ul>)^
2809** ^Memory allocation statistics are enabled by default unless SQLite is
2810** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
2811** allocation statistics are disabled by default.
2812** </dd>
2813**
2814** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
2815** <dd> The SQLITE_CONFIG_SCRATCH option is no longer used.
2816** </dd>
2817**
2818** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
2819** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool
2820** that SQLite can use for the database page cache with the default page
2821** cache implementation.
2822** This configuration option is a no-op if an application-defined page
2823** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2].
2824** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
2825** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2826** and the number of cache lines (N).
2827** The sz argument should be the size of the largest database page
2828** (a power of two between 512 and 65536) plus some extra bytes for each
2829** page header.  ^The number of extra bytes needed by the page header
2830** can be determined using [SQLITE_CONFIG_PCACHE_HDRSZ].
2831** ^It is harmless, apart from the wasted memory,
2832** for the sz parameter to be larger than necessary.  The pMem
2833** argument must be either a NULL pointer or a pointer to an 8-byte
2834** aligned block of memory of at least sz*N bytes, otherwise
2835** subsequent behavior is undefined.
2836** ^When pMem is not NULL, SQLite will strive to use the memory provided
2837** to satisfy page cache needs, falling back to [sqlite3_malloc()] if
2838** a page cache line is larger than sz bytes or if all of the pMem buffer
2839** is exhausted.
2840** ^If pMem is NULL and N is non-zero, then each database connection
2841** does an initial bulk allocation for page cache memory
2842** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or
2843** of -1024*N bytes if N is negative, . ^If additional
2844** page cache memory is needed beyond what is provided by the initial
2845** allocation, then SQLite goes to [sqlite3_malloc()] separately for each
2846** additional cache line. </dd>
2847**
2848** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
2849** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
2850** that SQLite will use for all of its dynamic memory allocation needs
2851** beyond those provided for by [SQLITE_CONFIG_PAGECACHE].
2852** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
2853** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
2854** [SQLITE_ERROR] if invoked otherwise.
2855** ^There are three arguments to SQLITE_CONFIG_HEAP:
2856** An 8-byte aligned pointer to the memory,
2857** the number of bytes in the memory buffer, and the minimum allocation size.
2858** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
2859** to using its default memory allocator (the system malloc() implementation),
2860** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
2861** memory pointer is not NULL then the alternative memory
2862** allocator is engaged to handle all of SQLites memory allocation needs.
2863** The first pointer (the memory pointer) must be aligned to an 8-byte
2864** boundary or subsequent behavior of SQLite will be undefined.
2865** The minimum allocation size is capped at 2**12. Reasonable values
2866** for the minimum allocation size are 2**5 through 2**8.</dd>
2867**
2868** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
2869** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
2870** pointer to an instance of the [sqlite3_mutex_methods] structure.
2871** The argument specifies alternative low-level mutex routines to be used
2872** in place the mutex routines built into SQLite.)^  ^SQLite makes a copy of
2873** the content of the [sqlite3_mutex_methods] structure before the call to
2874** [sqlite3_config()] returns. ^If SQLite is compiled with
2875** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2876** the entire mutexing subsystem is omitted from the build and hence calls to
2877** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
2878** return [SQLITE_ERROR].</dd>
2879**
2880** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
2881** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
2882** is a pointer to an instance of the [sqlite3_mutex_methods] structure.  The
2883** [sqlite3_mutex_methods]
2884** structure is filled with the currently defined mutex routines.)^
2885** This option can be used to overload the default mutex allocation
2886** routines with a wrapper used to track mutex usage for performance
2887** profiling or testing, for example.   ^If SQLite is compiled with
2888** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2889** the entire mutexing subsystem is omitted from the build and hence calls to
2890** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
2891** return [SQLITE_ERROR].</dd>
2892**
2893** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
2894** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
2895** the default size of lookaside memory on each [database connection].
2896** The first argument is the
2897** size of each lookaside buffer slot and the second is the number of
2898** slots allocated to each database connection.)^  ^(SQLITE_CONFIG_LOOKASIDE
2899** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
2900** option to [sqlite3_db_config()] can be used to change the lookaside
2901** configuration on individual connections.)^ </dd>
2902**
2903** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
2904** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
2905** a pointer to an [sqlite3_pcache_methods2] object.  This object specifies
2906** the interface to a custom page cache implementation.)^
2907** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
2908**
2909** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
2910** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
2911** is a pointer to an [sqlite3_pcache_methods2] object.  SQLite copies of
2912** the current page cache implementation into that object.)^ </dd>
2913**
2914** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
2915** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
2916** global [error log].
2917** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
2918** function with a call signature of void(*)(void*,int,const char*),
2919** and a pointer to void. ^If the function pointer is not NULL, it is
2920** invoked by [sqlite3_log()] to process each logging event.  ^If the
2921** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2922** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
2923** passed through as the first parameter to the application-defined logger
2924** function whenever that function is invoked.  ^The second parameter to
2925** the logger function is a copy of the first parameter to the corresponding
2926** [sqlite3_log()] call and is intended to be a [result code] or an
2927** [extended result code].  ^The third parameter passed to the logger is
2928** log message after formatting via [sqlite3_snprintf()].
2929** The SQLite logging interface is not reentrant; the logger function
2930** supplied by the application must not invoke any SQLite interface.
2931** In a multi-threaded application, the application-defined logger
2932** function must be threadsafe. </dd>
2933**
2934** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
2935** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
2936** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2937** then URI handling is globally disabled.)^ ^If URI handling is globally
2938** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()],
2939** [sqlite3_open16()] or
2940** specified as part of [ATTACH] commands are interpreted as URIs, regardless
2941** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
2942** connection is opened. ^If it is globally disabled, filenames are
2943** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
2944** database connection is opened. ^(By default, URI handling is globally
2945** disabled. The default value may be changed by compiling with the
2946** [SQLITE_USE_URI] symbol defined.)^
2947**
2948** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
2949** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
2950** argument which is interpreted as a boolean in order to enable or disable
2951** the use of covering indices for full table scans in the query optimizer.
2952** ^The default setting is determined
2953** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2954** if that compile-time option is omitted.
2955** The ability to disable the use of covering indices for full table scans
2956** is because some incorrectly coded legacy applications might malfunction
2957** when the optimization is enabled.  Providing the ability to
2958** disable the optimization allows the older, buggy application code to work
2959** without change even with newer versions of SQLite.
2960**
2961** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2962** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
2963** <dd> These options are obsolete and should not be used by new code.
2964** They are retained for backwards compatibility but are now no-ops.
2965** </dd>
2966**
2967** [[SQLITE_CONFIG_SQLLOG]]
2968** <dt>SQLITE_CONFIG_SQLLOG
2969** <dd>This option is only available if sqlite is compiled with the
2970** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2971** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
2972** The second should be of type (void*). The callback is invoked by the library
2973** in three separate circumstances, identified by the value passed as the
2974** fourth parameter. If the fourth parameter is 0, then the database connection
2975** passed as the second argument has just been opened. The third argument
2976** points to a buffer containing the name of the main database file. If the
2977** fourth parameter is 1, then the SQL statement that the third parameter
2978** points to has just been executed. Or, if the fourth parameter is 2, then
2979** the connection being passed as the second parameter is being closed. The
2980** third parameter is passed NULL In this case.  An example of using this
2981** configuration option can be seen in the "test_sqllog.c" source file in
2982** the canonical SQLite source tree.</dd>
2983**
2984** [[SQLITE_CONFIG_MMAP_SIZE]]
2985** <dt>SQLITE_CONFIG_MMAP_SIZE
2986** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2987** that are the default mmap size limit (the default setting for
2988** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
2989** ^The default setting can be overridden by each database connection using
2990** either the [PRAGMA mmap_size] command, or by using the
2991** [SQLITE_FCNTL_MMAP_SIZE] file control.  ^(The maximum allowed mmap size
2992** will be silently truncated if necessary so that it does not exceed the
2993** compile-time maximum mmap size set by the
2994** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2995** ^If either argument to this option is negative, then that argument is
2996** changed to its compile-time default.
2997**
2998** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
2999** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
3000** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
3001** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
3002** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
3003** that specifies the maximum size of the created heap.
3004**
3005** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
3006** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
3007** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
3008** is a pointer to an integer and writes into that integer the number of extra
3009** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE].
3010** The amount of extra space required can change depending on the compiler,
3011** target platform, and SQLite version.
3012**
3013** [[SQLITE_CONFIG_PMASZ]]
3014** <dt>SQLITE_CONFIG_PMASZ
3015** <dd>^The SQLITE_CONFIG_PMASZ option takes a single parameter which
3016** is an unsigned integer and sets the "Minimum PMA Size" for the multithreaded
3017** sorter to that integer.  The default minimum PMA Size is set by the
3018** [SQLITE_SORTER_PMASZ] compile-time option.  New threads are launched
3019** to help with sort operations when multithreaded sorting
3020** is enabled (using the [PRAGMA threads] command) and the amount of content
3021** to be sorted exceeds the page size times the minimum of the
3022** [PRAGMA cache_size] setting and this value.
3023**
3024** [[SQLITE_CONFIG_STMTJRNL_SPILL]]
3025** <dt>SQLITE_CONFIG_STMTJRNL_SPILL
3026** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which
3027** becomes the [statement journal] spill-to-disk threshold.
3028** [Statement journals] are held in memory until their size (in bytes)
3029** exceeds this threshold, at which point they are written to disk.
3030** Or if the threshold is -1, statement journals are always held
3031** exclusively in memory.
3032** Since many statement journals never become large, setting the spill
3033** threshold to a value such as 64KiB can greatly reduce the amount of
3034** I/O required to support statement rollback.
3035** The default value for this setting is controlled by the
3036** [SQLITE_STMTJRNL_SPILL] compile-time option.
3037**
3038** [[SQLITE_CONFIG_SORTERREF_SIZE]]
3039** <dt>SQLITE_CONFIG_SORTERREF_SIZE
3040** <dd>The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter
3041** of type (int) - the new value of the sorter-reference size threshold.
3042** Usually, when SQLite uses an external sort to order records according
3043** to an ORDER BY clause, all fields required by the caller are present in the
3044** sorted records. However, if SQLite determines based on the declared type
3045** of a table column that its values are likely to be very large - larger
3046** than the configured sorter-reference size threshold - then a reference
3047** is stored in each sorted record and the required column values loaded
3048** from the database as records are returned in sorted order. The default
3049** value for this option is to never use this optimization. Specifying a
3050** negative value for this option restores the default behaviour.
3051** This option is only available if SQLite is compiled with the
3052** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
3053**
3054** [[SQLITE_CONFIG_MEMDB_MAXSIZE]]
3055** <dt>SQLITE_CONFIG_MEMDB_MAXSIZE
3056** <dd>The SQLITE_CONFIG_MEMDB_MAXSIZE option accepts a single parameter
3057** [sqlite3_int64] parameter which is the default maximum size for an in-memory
3058** database created using [sqlite3_deserialize()].  This default maximum
3059** size can be adjusted up or down for individual databases using the
3060** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control].  If this
3061** configuration setting is never used, then the default maximum is determined
3062** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option.  If that
3063** compile-time option is not set, then the default maximum is 1073741824.
3064** </dl>
3065*/
3066#define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
3067#define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
3068#define SQLITE_CONFIG_SERIALIZED    3  /* nil */
3069#define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
3070#define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
3071#define SQLITE_CONFIG_SCRATCH       6  /* No longer used */
3072#define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
3073#define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
3074#define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
3075#define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
3076#define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
3077/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
3078#define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
3079#define SQLITE_CONFIG_PCACHE       14  /* no-op */
3080#define SQLITE_CONFIG_GETPCACHE    15  /* no-op */
3081#define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
3082#define SQLITE_CONFIG_URI          17  /* int */
3083#define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
3084#define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
3085#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20  /* int */
3086#define SQLITE_CONFIG_SQLLOG       21  /* xSqllog, void* */
3087#define SQLITE_CONFIG_MMAP_SIZE    22  /* sqlite3_int64, sqlite3_int64 */
3088#define SQLITE_CONFIG_WIN32_HEAPSIZE      23  /* int nByte */
3089#define SQLITE_CONFIG_PCACHE_HDRSZ        24  /* int *psz */
3090#define SQLITE_CONFIG_PMASZ               25  /* unsigned int szPma */
3091#define SQLITE_CONFIG_STMTJRNL_SPILL      26  /* int nByte */
3092#define SQLITE_CONFIG_SMALL_MALLOC        27  /* boolean */
3093#define SQLITE_CONFIG_SORTERREF_SIZE      28  /* int nByte */
3094#define SQLITE_CONFIG_MEMDB_MAXSIZE       29  /* sqlite3_int64 */
3095
3096/*
3097** CAPI3REF: Database Connection Configuration Options
3098**
3099** These constants are the available integer configuration options that
3100** can be passed as the second argument to the [sqlite3_db_config()] interface.
3101**
3102** New configuration options may be added in future releases of SQLite.
3103** Existing configuration options might be discontinued.  Applications
3104** should check the return code from [sqlite3_db_config()] to make sure that
3105** the call worked.  ^The [sqlite3_db_config()] interface will return a
3106** non-zero [error code] if a discontinued or unsupported configuration option
3107** is invoked.
3108**
3109** <dl>
3110** [[SQLITE_DBCONFIG_LOOKASIDE]]
3111** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
3112** <dd> ^This option takes three additional arguments that determine the
3113** [lookaside memory allocator] configuration for the [database connection].
3114** ^The first argument (the third parameter to [sqlite3_db_config()] is a
3115** pointer to a memory buffer to use for lookaside memory.
3116** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
3117** may be NULL in which case SQLite will allocate the
3118** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
3119** size of each lookaside buffer slot.  ^The third argument is the number of
3120** slots.  The size of the buffer in the first argument must be greater than
3121** or equal to the product of the second and third arguments.  The buffer
3122** must be aligned to an 8-byte boundary.  ^If the second argument to
3123** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
3124** rounded down to the next smaller multiple of 8.  ^(The lookaside memory
3125** configuration for a database connection can only be changed when that
3126** connection is not currently using lookaside memory, or in other words
3127** when the "current value" returned by
3128** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
3129** Any attempt to change the lookaside memory configuration when lookaside
3130** memory is in use leaves the configuration unchanged and returns
3131** [SQLITE_BUSY].)^</dd>
3132**
3133** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
3134** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
3135** <dd> ^This option is used to enable or disable the enforcement of
3136** [foreign key constraints].  There should be two additional arguments.
3137** The first argument is an integer which is 0 to disable FK enforcement,
3138** positive to enable FK enforcement or negative to leave FK enforcement
3139** unchanged.  The second parameter is a pointer to an integer into which
3140** is written 0 or 1 to indicate whether FK enforcement is off or on
3141** following this call.  The second parameter may be a NULL pointer, in
3142** which case the FK enforcement setting is not reported back. </dd>
3143**
3144** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
3145** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
3146** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
3147** There should be two additional arguments.
3148** The first argument is an integer which is 0 to disable triggers,
3149** positive to enable triggers or negative to leave the setting unchanged.
3150** The second parameter is a pointer to an integer into which
3151** is written 0 or 1 to indicate whether triggers are disabled or enabled
3152** following this call.  The second parameter may be a NULL pointer, in
3153** which case the trigger setting is not reported back. </dd>
3154**
3155** [[SQLITE_DBCONFIG_ENABLE_VIEW]]
3156** <dt>SQLITE_DBCONFIG_ENABLE_VIEW</dt>
3157** <dd> ^This option is used to enable or disable [CREATE VIEW | views].
3158** There should be two additional arguments.
3159** The first argument is an integer which is 0 to disable views,
3160** positive to enable views or negative to leave the setting unchanged.
3161** The second parameter is a pointer to an integer into which
3162** is written 0 or 1 to indicate whether views are disabled or enabled
3163** following this call.  The second parameter may be a NULL pointer, in
3164** which case the view setting is not reported back. </dd>
3165**
3166** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
3167** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
3168** <dd> ^This option is used to enable or disable the
3169** [fts3_tokenizer()] function which is part of the
3170** [FTS3] full-text search engine extension.
3171** There should be two additional arguments.
3172** The first argument is an integer which is 0 to disable fts3_tokenizer() or
3173** positive to enable fts3_tokenizer() or negative to leave the setting
3174** unchanged.
3175** The second parameter is a pointer to an integer into which
3176** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
3177** following this call.  The second parameter may be a NULL pointer, in
3178** which case the new setting is not reported back. </dd>
3179**
3180** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
3181** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
3182** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
3183** interface independently of the [load_extension()] SQL function.
3184** The [sqlite3_enable_load_extension()] API enables or disables both the
3185** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
3186** There should be two additional arguments.
3187** When the first argument to this interface is 1, then only the C-API is
3188** enabled and the SQL function remains disabled.  If the first argument to
3189** this interface is 0, then both the C-API and the SQL function are disabled.
3190** If the first argument is -1, then no changes are made to state of either the
3191** C-API or the SQL function.
3192** The second parameter is a pointer to an integer into which
3193** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
3194** is disabled or enabled following this call.  The second parameter may
3195** be a NULL pointer, in which case the new setting is not reported back.
3196** </dd>
3197**
3198** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
3199** <dd> ^This option is used to change the name of the "main" database
3200** schema.  ^The sole argument is a pointer to a constant UTF8 string
3201** which will become the new schema name in place of "main".  ^SQLite
3202** does not make a copy of the new main schema name string, so the application
3203** must ensure that the argument passed into this DBCONFIG option is unchanged
3204** until after the database connection closes.
3205** </dd>
3206**
3207** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]]
3208** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
3209** <dd> Usually, when a database in wal mode is closed or detached from a
3210** database handle, SQLite checks if this will mean that there are now no
3211** connections at all to the database. If so, it performs a checkpoint
3212** operation before closing the connection. This option may be used to
3213** override this behaviour. The first parameter passed to this operation
3214** is an integer - positive to disable checkpoints-on-close, or zero (the
3215** default) to enable them, and negative to leave the setting unchanged.
3216** The second parameter is a pointer to an integer
3217** into which is written 0 or 1 to indicate whether checkpoints-on-close
3218** have been disabled - 0 if they are not disabled, 1 if they are.
3219** </dd>
3220**
3221** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
3222** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
3223** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
3224** a single SQL query statement will always use the same algorithm regardless
3225** of values of [bound parameters].)^ The QPSG disables some query optimizations
3226** that look at the values of bound parameters, which can make some queries
3227** slower.  But the QPSG has the advantage of more predictable behavior.  With
3228** the QPSG active, SQLite will always use the same query plan in the field as
3229** was used during testing in the lab.
3230** The first argument to this setting is an integer which is 0 to disable
3231** the QPSG, positive to enable QPSG, or negative to leave the setting
3232** unchanged. The second parameter is a pointer to an integer into which
3233** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
3234** following this call.
3235** </dd>
3236**
3237** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
3238** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not
3239** include output for any operations performed by trigger programs. This
3240** option is used to set or clear (the default) a flag that governs this
3241** behavior. The first parameter passed to this operation is an integer -
3242** positive to enable output for trigger programs, or zero to disable it,
3243** or negative to leave the setting unchanged.
3244** The second parameter is a pointer to an integer into which is written
3245** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
3246** it is not disabled, 1 if it is.
3247** </dd>
3248**
3249** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
3250** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
3251** [VACUUM] in order to reset a database back to an empty database
3252** with no schema and no content. The following process works even for
3253** a badly corrupted database file:
3254** <ol>
3255** <li> If the database connection is newly opened, make sure it has read the
3256**      database schema by preparing then discarding some query against the
3257**      database, or calling sqlite3_table_column_metadata(), ignoring any
3258**      errors.  This step is only necessary if the application desires to keep
3259**      the database in WAL mode after the reset if it was in WAL mode before
3260**      the reset.
3261** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
3262** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
3263** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
3264** </ol>
3265** Because resetting a database is destructive and irreversible, the
3266** process requires the use of this obscure API and multiple steps to help
3267** ensure that it does not happen by accident.
3268**
3269** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt>
3270** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the
3271** "defensive" flag for a database connection.  When the defensive
3272** flag is enabled, language features that allow ordinary SQL to
3273** deliberately corrupt the database file are disabled.  The disabled
3274** features include but are not limited to the following:
3275** <ul>
3276** <li> The [PRAGMA writable_schema=ON] statement.
3277** <li> The [PRAGMA journal_mode=OFF] statement.
3278** <li> Writes to the [sqlite_dbpage] virtual table.
3279** <li> Direct writes to [shadow tables].
3280** </ul>
3281** </dd>
3282**
3283** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]] <dt>SQLITE_DBCONFIG_WRITABLE_SCHEMA</dt>
3284** <dd>The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the
3285** "writable_schema" flag. This has the same effect and is logically equivalent
3286** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF].
3287** The first argument to this setting is an integer which is 0 to disable
3288** the writable_schema, positive to enable writable_schema, or negative to
3289** leave the setting unchanged. The second parameter is a pointer to an
3290** integer into which is written 0 or 1 to indicate whether the writable_schema
3291** is enabled or disabled following this call.
3292** </dd>
3293**
3294** [[SQLITE_DBCONFIG_LEGACY_ALTER_TABLE]]
3295** <dt>SQLITE_DBCONFIG_LEGACY_ALTER_TABLE</dt>
3296** <dd>The SQLITE_DBCONFIG_LEGACY_ALTER_TABLE option activates or deactivates
3297** the legacy behavior of the [ALTER TABLE RENAME] command such it
3298** behaves as it did prior to [version 3.24.0] (2018-06-04).  See the
3299** "Compatibility Notice" on the [ALTER TABLE RENAME documentation] for
3300** additional information. This feature can also be turned on and off
3301** using the [PRAGMA legacy_alter_table] statement.
3302** </dd>
3303**
3304** [[SQLITE_DBCONFIG_DQS_DML]]
3305** <dt>SQLITE_DBCONFIG_DQS_DML</td>
3306** <dd>The SQLITE_DBCONFIG_DQS_DML option activates or deactivates
3307** the legacy [double-quoted string literal] misfeature for DML statements
3308** only, that is DELETE, INSERT, SELECT, and UPDATE statements. The
3309** default value of this setting is determined by the [-DSQLITE_DQS]
3310** compile-time option.
3311** </dd>
3312**
3313** [[SQLITE_DBCONFIG_DQS_DDL]]
3314** <dt>SQLITE_DBCONFIG_DQS_DDL</td>
3315** <dd>The SQLITE_DBCONFIG_DQS option activates or deactivates
3316** the legacy [double-quoted string literal] misfeature for DDL statements,
3317** such as CREATE TABLE and CREATE INDEX. The
3318** default value of this setting is determined by the [-DSQLITE_DQS]
3319** compile-time option.
3320** </dd>
3321**
3322** [[SQLITE_DBCONFIG_TRUSTED_SCHEMA]]
3323** <dt>SQLITE_DBCONFIG_TRUSTED_SCHEMA</td>
3324** <dd>The SQLITE_DBCONFIG_TRUSTED_SCHEMA option tells SQLite to
3325** assume that database schemas (the contents of the [sqlite_master] tables)
3326** are untainted by malicious content.
3327** When the SQLITE_DBCONFIG_TRUSTED_SCHEMA option is disabled, SQLite
3328** takes additional defensive steps to protect the application from harm
3329** including:
3330** <ul>
3331** <li> Prohibit the use of SQL functions inside triggers, views,
3332** CHECK constraints, DEFAULT clauses, expression indexes,
3333** partial indexes, or generated columns
3334** unless those functions are tagged with [SQLITE_INNOCUOUS].
3335** <li> Prohibit the use of virtual tables inside of triggers or views
3336** unless those virtual tables are tagged with [SQLITE_VTAB_INNOCUOUS].
3337** </ul>
3338** This setting defaults to "on" for legacy compatibility, however
3339** all applications are advised to turn it off if possible. This setting
3340** can also be controlled using the [PRAGMA trusted_schema] statement.
3341** </dd>
3342**
3343** [[SQLITE_DBCONFIG_LEGACY_FILE_FORMAT]]
3344** <dt>SQLITE_DBCONFIG_LEGACY_FILE_FORMAT</td>
3345** <dd>The SQLITE_DBCONFIG_LEGACY_FILE_FORMAT option activates or deactivates
3346** the legacy file format flag.  When activated, this flag causes all newly
3347** created database file to have a schema format version number (the 4-byte
3348** integer found at offset 44 into the database header) of 1.  This in turn
3349** means that the resulting database file will be readable and writable by
3350** any SQLite version back to 3.0.0 ([dateof:3.0.0]).  Without this setting,
3351** newly created databases are generally not understandable by SQLite versions
3352** prior to 3.3.0 ([dateof:3.3.0]).  As these words are written, there
3353** is now scarcely any need to generated database files that are compatible
3354** all the way back to version 3.0.0, and so this setting is of little
3355** practical use, but is provided so that SQLite can continue to claim the
3356** ability to generate new database files that are compatible with  version
3357** 3.0.0.
3358** <p>Note that when the SQLITE_DBCONFIG_LEGACY_FILE_FORMAT setting is on,
3359** the [VACUUM] command will fail with an obscure error when attempting to
3360** process a table with generated columns and a descending index.  This is
3361** not considered a bug since SQLite versions 3.3.0 and earlier do not support
3362** either generated columns or decending indexes.
3363** </dd>
3364** </dl>
3365*/
3366#define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
3367#define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
3368#define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
3369#define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
3370#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
3371#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
3372#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
3373#define SQLITE_DBCONFIG_ENABLE_QPSG           1007 /* int int* */
3374#define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
3375#define SQLITE_DBCONFIG_RESET_DATABASE        1009 /* int int* */
3376#define SQLITE_DBCONFIG_DEFENSIVE             1010 /* int int* */
3377#define SQLITE_DBCONFIG_WRITABLE_SCHEMA       1011 /* int int* */
3378#define SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    1012 /* int int* */
3379#define SQLITE_DBCONFIG_DQS_DML               1013 /* int int* */
3380#define SQLITE_DBCONFIG_DQS_DDL               1014 /* int int* */
3381#define SQLITE_DBCONFIG_ENABLE_VIEW           1015 /* int int* */
3382#define SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    1016 /* int int* */
3383#define SQLITE_DBCONFIG_TRUSTED_SCHEMA        1017 /* int int* */
3384#define SQLITE_DBCONFIG_MAX                   1017 /* Largest DBCONFIG */
3385
3386/*
3387** CAPI3REF: Enable Or Disable Extended Result Codes
3388** METHOD: sqlite3
3389**
3390** ^The sqlite3_extended_result_codes() routine enables or disables the
3391** [extended result codes] feature of SQLite. ^The extended result
3392** codes are disabled by default for historical compatibility.
3393*/
3394SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
3395
3396/*
3397** CAPI3REF: Last Insert Rowid
3398** METHOD: sqlite3
3399**
3400** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
3401** has a unique 64-bit signed
3402** integer key called the [ROWID | "rowid"]. ^The rowid is always available
3403** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
3404** names are not also used by explicitly declared columns. ^If
3405** the table has a column of type [INTEGER PRIMARY KEY] then that column
3406** is another alias for the rowid.
3407**
3408** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
3409** the most recent successful [INSERT] into a rowid table or [virtual table]
3410** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
3411** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred
3412** on the database connection D, then sqlite3_last_insert_rowid(D) returns
3413** zero.
3414**
3415** As well as being set automatically as rows are inserted into database
3416** tables, the value returned by this function may be set explicitly by
3417** [sqlite3_set_last_insert_rowid()]
3418**
3419** Some virtual table implementations may INSERT rows into rowid tables as
3420** part of committing a transaction (e.g. to flush data accumulated in memory
3421** to disk). In this case subsequent calls to this function return the rowid
3422** associated with these internal INSERT operations, which leads to
3423** unintuitive results. Virtual table implementations that do write to rowid
3424** tables in this way can avoid this problem by restoring the original
3425** rowid value using [sqlite3_set_last_insert_rowid()] before returning
3426** control to the user.
3427**
3428** ^(If an [INSERT] occurs within a trigger then this routine will
3429** return the [rowid] of the inserted row as long as the trigger is
3430** running. Once the trigger program ends, the value returned
3431** by this routine reverts to what it was before the trigger was fired.)^
3432**
3433** ^An [INSERT] that fails due to a constraint violation is not a
3434** successful [INSERT] and does not change the value returned by this
3435** routine.  ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
3436** and INSERT OR ABORT make no changes to the return value of this
3437** routine when their insertion fails.  ^(When INSERT OR REPLACE
3438** encounters a constraint violation, it does not fail.  The
3439** INSERT continues to completion after deleting rows that caused
3440** the constraint problem so INSERT OR REPLACE will always change
3441** the return value of this interface.)^
3442**
3443** ^For the purposes of this routine, an [INSERT] is considered to
3444** be successful even if it is subsequently rolled back.
3445**
3446** This function is accessible to SQL statements via the
3447** [last_insert_rowid() SQL function].
3448**
3449** If a separate thread performs a new [INSERT] on the same
3450** database connection while the [sqlite3_last_insert_rowid()]
3451** function is running and thus changes the last insert [rowid],
3452** then the value returned by [sqlite3_last_insert_rowid()] is
3453** unpredictable and might not equal either the old or the new
3454** last insert [rowid].
3455*/
3456SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
3457
3458/*
3459** CAPI3REF: Set the Last Insert Rowid value.
3460** METHOD: sqlite3
3461**
3462** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
3463** set the value returned by calling sqlite3_last_insert_rowid(D) to R
3464** without inserting a row into the database.
3465*/
3466SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
3467
3468/*
3469** CAPI3REF: Count The Number Of Rows Modified
3470** METHOD: sqlite3
3471**
3472** ^This function returns the number of rows modified, inserted or
3473** deleted by the most recently completed INSERT, UPDATE or DELETE
3474** statement on the database connection specified by the only parameter.
3475** ^Executing any other type of SQL statement does not modify the value
3476** returned by this function.
3477**
3478** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
3479** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
3480** [foreign key actions] or [REPLACE] constraint resolution are not counted.
3481**
3482** Changes to a view that are intercepted by
3483** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value
3484** returned by sqlite3_changes() immediately after an INSERT, UPDATE or
3485** DELETE statement run on a view is always zero. Only changes made to real
3486** tables are counted.
3487**
3488** Things are more complicated if the sqlite3_changes() function is
3489** executed while a trigger program is running. This may happen if the
3490** program uses the [changes() SQL function], or if some other callback
3491** function invokes sqlite3_changes() directly. Essentially:
3492**
3493** <ul>
3494**   <li> ^(Before entering a trigger program the value returned by
3495**        sqlite3_changes() function is saved. After the trigger program
3496**        has finished, the original value is restored.)^
3497**
3498**   <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
3499**        statement sets the value returned by sqlite3_changes()
3500**        upon completion as normal. Of course, this value will not include
3501**        any changes performed by sub-triggers, as the sqlite3_changes()
3502**        value will be saved and restored after each sub-trigger has run.)^
3503** </ul>
3504**
3505** ^This means that if the changes() SQL function (or similar) is used
3506** by the first INSERT, UPDATE or DELETE statement within a trigger, it
3507** returns the value as set when the calling statement began executing.
3508** ^If it is used by the second or subsequent such statement within a trigger
3509** program, the value returned reflects the number of rows modified by the
3510** previous INSERT, UPDATE or DELETE statement within the same trigger.
3511**
3512** If a separate thread makes changes on the same database connection
3513** while [sqlite3_changes()] is running then the value returned
3514** is unpredictable and not meaningful.
3515**
3516** See also:
3517** <ul>
3518** <li> the [sqlite3_total_changes()] interface
3519** <li> the [count_changes pragma]
3520** <li> the [changes() SQL function]
3521** <li> the [data_version pragma]
3522** </ul>
3523*/
3524SQLITE_API int sqlite3_changes(sqlite3*);
3525
3526/*
3527** CAPI3REF: Total Number Of Rows Modified
3528** METHOD: sqlite3
3529**
3530** ^This function returns the total number of rows inserted, modified or
3531** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
3532** since the database connection was opened, including those executed as
3533** part of trigger programs. ^Executing any other type of SQL statement
3534** does not affect the value returned by sqlite3_total_changes().
3535**
3536** ^Changes made as part of [foreign key actions] are included in the
3537** count, but those made as part of REPLACE constraint resolution are
3538** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
3539** are not counted.
3540**
3541** The [sqlite3_total_changes(D)] interface only reports the number
3542** of rows that changed due to SQL statement run against database
3543** connection D.  Any changes by other database connections are ignored.
3544** To detect changes against a database file from other database
3545** connections use the [PRAGMA data_version] command or the
3546** [SQLITE_FCNTL_DATA_VERSION] [file control].
3547**
3548** If a separate thread makes changes on the same database connection
3549** while [sqlite3_total_changes()] is running then the value
3550** returned is unpredictable and not meaningful.
3551**
3552** See also:
3553** <ul>
3554** <li> the [sqlite3_changes()] interface
3555** <li> the [count_changes pragma]
3556** <li> the [changes() SQL function]
3557** <li> the [data_version pragma]
3558** <li> the [SQLITE_FCNTL_DATA_VERSION] [file control]
3559** </ul>
3560*/
3561SQLITE_API int sqlite3_total_changes(sqlite3*);
3562
3563/*
3564** CAPI3REF: Interrupt A Long-Running Query
3565** METHOD: sqlite3
3566**
3567** ^This function causes any pending database operation to abort and
3568** return at its earliest opportunity. This routine is typically
3569** called in response to a user action such as pressing "Cancel"
3570** or Ctrl-C where the user wants a long query operation to halt
3571** immediately.
3572**
3573** ^It is safe to call this routine from a thread different from the
3574** thread that is currently running the database operation.  But it
3575** is not safe to call this routine with a [database connection] that
3576** is closed or might close before sqlite3_interrupt() returns.
3577**
3578** ^If an SQL operation is very nearly finished at the time when
3579** sqlite3_interrupt() is called, then it might not have an opportunity
3580** to be interrupted and might continue to completion.
3581**
3582** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
3583** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
3584** that is inside an explicit transaction, then the entire transaction
3585** will be rolled back automatically.
3586**
3587** ^The sqlite3_interrupt(D) call is in effect until all currently running
3588** SQL statements on [database connection] D complete.  ^Any new SQL statements
3589** that are started after the sqlite3_interrupt() call and before the
3590** running statement count reaches zero are interrupted as if they had been
3591** running prior to the sqlite3_interrupt() call.  ^New SQL statements
3592** that are started after the running statement count reaches zero are
3593** not effected by the sqlite3_interrupt().
3594** ^A call to sqlite3_interrupt(D) that occurs when there are no running
3595** SQL statements is a no-op and has no effect on SQL statements
3596** that are started after the sqlite3_interrupt() call returns.
3597*/
3598SQLITE_API void sqlite3_interrupt(sqlite3*);
3599
3600/*
3601** CAPI3REF: Determine If An SQL Statement Is Complete
3602**
3603** These routines are useful during command-line input to determine if the
3604** currently entered text seems to form a complete SQL statement or
3605** if additional input is needed before sending the text into
3606** SQLite for parsing.  ^These routines return 1 if the input string
3607** appears to be a complete SQL statement.  ^A statement is judged to be
3608** complete if it ends with a semicolon token and is not a prefix of a
3609** well-formed CREATE TRIGGER statement.  ^Semicolons that are embedded within
3610** string literals or quoted identifier names or comments are not
3611** independent tokens (they are part of the token in which they are
3612** embedded) and thus do not count as a statement terminator.  ^Whitespace
3613** and comments that follow the final semicolon are ignored.
3614**
3615** ^These routines return 0 if the statement is incomplete.  ^If a
3616** memory allocation fails, then SQLITE_NOMEM is returned.
3617**
3618** ^These routines do not parse the SQL statements thus
3619** will not detect syntactically incorrect SQL.
3620**
3621** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
3622** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
3623** automatically by sqlite3_complete16().  If that initialization fails,
3624** then the return value from sqlite3_complete16() will be non-zero
3625** regardless of whether or not the input SQL is complete.)^
3626**
3627** The input to [sqlite3_complete()] must be a zero-terminated
3628** UTF-8 string.
3629**
3630** The input to [sqlite3_complete16()] must be a zero-terminated
3631** UTF-16 string in native byte order.
3632*/
3633SQLITE_API int sqlite3_complete(const char *sql);
3634SQLITE_API int sqlite3_complete16(const void *sql);
3635
3636/*
3637** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
3638** KEYWORDS: {busy-handler callback} {busy handler}
3639** METHOD: sqlite3
3640**
3641** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
3642** that might be invoked with argument P whenever
3643** an attempt is made to access a database table associated with
3644** [database connection] D when another thread
3645** or process has the table locked.
3646** The sqlite3_busy_handler() interface is used to implement
3647** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
3648**
3649** ^If the busy callback is NULL, then [SQLITE_BUSY]
3650** is returned immediately upon encountering the lock.  ^If the busy callback
3651** is not NULL, then the callback might be invoked with two arguments.
3652**
3653** ^The first argument to the busy handler is a copy of the void* pointer which
3654** is the third argument to sqlite3_busy_handler().  ^The second argument to
3655** the busy handler callback is the number of times that the busy handler has
3656** been invoked previously for the same locking event.  ^If the
3657** busy callback returns 0, then no additional attempts are made to
3658** access the database and [SQLITE_BUSY] is returned
3659** to the application.
3660** ^If the callback returns non-zero, then another attempt
3661** is made to access the database and the cycle repeats.
3662**
3663** The presence of a busy handler does not guarantee that it will be invoked
3664** when there is lock contention. ^If SQLite determines that invoking the busy
3665** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
3666** to the application instead of invoking the
3667** busy handler.
3668** Consider a scenario where one process is holding a read lock that
3669** it is trying to promote to a reserved lock and
3670** a second process is holding a reserved lock that it is trying
3671** to promote to an exclusive lock.  The first process cannot proceed
3672** because it is blocked by the second and the second process cannot
3673** proceed because it is blocked by the first.  If both processes
3674** invoke the busy handlers, neither will make any progress.  Therefore,
3675** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
3676** will induce the first process to release its read lock and allow
3677** the second process to proceed.
3678**
3679** ^The default busy callback is NULL.
3680**
3681** ^(There can only be a single busy handler defined for each
3682** [database connection].  Setting a new busy handler clears any
3683** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
3684** or evaluating [PRAGMA busy_timeout=N] will change the
3685** busy handler and thus clear any previously set busy handler.
3686**
3687** The busy callback should not take any actions which modify the
3688** database connection that invoked the busy handler.  In other words,
3689** the busy handler is not reentrant.  Any such actions
3690** result in undefined behavior.
3691**
3692** A busy handler must not close the database connection
3693** or [prepared statement] that invoked the busy handler.
3694*/
3695SQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
3696
3697/*
3698** CAPI3REF: Set A Busy Timeout
3699** METHOD: sqlite3
3700**
3701** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
3702** for a specified amount of time when a table is locked.  ^The handler
3703** will sleep multiple times until at least "ms" milliseconds of sleeping
3704** have accumulated.  ^After at least "ms" milliseconds of sleeping,
3705** the handler returns 0 which causes [sqlite3_step()] to return
3706** [SQLITE_BUSY].
3707**
3708** ^Calling this routine with an argument less than or equal to zero
3709** turns off all busy handlers.
3710**
3711** ^(There can only be a single busy handler for a particular
3712** [database connection] at any given moment.  If another busy handler
3713** was defined  (using [sqlite3_busy_handler()]) prior to calling
3714** this routine, that other busy handler is cleared.)^
3715**
3716** See also:  [PRAGMA busy_timeout]
3717*/
3718SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3719
3720/*
3721** CAPI3REF: Convenience Routines For Running Queries
3722** METHOD: sqlite3
3723**
3724** This is a legacy interface that is preserved for backwards compatibility.
3725** Use of this interface is not recommended.
3726**
3727** Definition: A <b>result table</b> is memory data structure created by the
3728** [sqlite3_get_table()] interface.  A result table records the
3729** complete query results from one or more queries.
3730**
3731** The table conceptually has a number of rows and columns.  But
3732** these numbers are not part of the result table itself.  These
3733** numbers are obtained separately.  Let N be the number of rows
3734** and M be the number of columns.
3735**
3736** A result table is an array of pointers to zero-terminated UTF-8 strings.
3737** There are (N+1)*M elements in the array.  The first M pointers point
3738** to zero-terminated strings that  contain the names of the columns.
3739** The remaining entries all point to query results.  NULL values result
3740** in NULL pointers.  All other values are in their UTF-8 zero-terminated
3741** string representation as returned by [sqlite3_column_text()].
3742**
3743** A result table might consist of one or more memory allocations.
3744** It is not safe to pass a result table directly to [sqlite3_free()].
3745** A result table should be deallocated using [sqlite3_free_table()].
3746**
3747** ^(As an example of the result table format, suppose a query result
3748** is as follows:
3749**
3750** <blockquote><pre>
3751**        Name        | Age
3752**        -----------------------
3753**        Alice       | 43
3754**        Bob         | 28
3755**        Cindy       | 21
3756** </pre></blockquote>
3757**
3758** There are two columns (M==2) and three rows (N==3).  Thus the
3759** result table has 8 entries.  Suppose the result table is stored
3760** in an array named azResult.  Then azResult holds this content:
3761**
3762** <blockquote><pre>
3763**        azResult&#91;0] = "Name";
3764**        azResult&#91;1] = "Age";
3765**        azResult&#91;2] = "Alice";
3766**        azResult&#91;3] = "43";
3767**        azResult&#91;4] = "Bob";
3768**        azResult&#91;5] = "28";
3769**        azResult&#91;6] = "Cindy";
3770**        azResult&#91;7] = "21";
3771** </pre></blockquote>)^
3772**
3773** ^The sqlite3_get_table() function evaluates one or more
3774** semicolon-separated SQL statements in the zero-terminated UTF-8
3775** string of its 2nd parameter and returns a result table to the
3776** pointer given in its 3rd parameter.
3777**
3778** After the application has finished with the result from sqlite3_get_table(),
3779** it must pass the result table pointer to sqlite3_free_table() in order to
3780** release the memory that was malloced.  Because of the way the
3781** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
3782** function must not try to call [sqlite3_free()] directly.  Only
3783** [sqlite3_free_table()] is able to release the memory properly and safely.
3784**
3785** The sqlite3_get_table() interface is implemented as a wrapper around
3786** [sqlite3_exec()].  The sqlite3_get_table() routine does not have access
3787** to any internal data structures of SQLite.  It uses only the public
3788** interface defined here.  As a consequence, errors that occur in the
3789** wrapper layer outside of the internal [sqlite3_exec()] call are not
3790** reflected in subsequent calls to [sqlite3_errcode()] or
3791** [sqlite3_errmsg()].
3792*/
3793SQLITE_API int sqlite3_get_table(
3794  sqlite3 *db,          /* An open database */
3795  const char *zSql,     /* SQL to be evaluated */
3796  char ***pazResult,    /* Results of the query */
3797  int *pnRow,           /* Number of result rows written here */
3798  int *pnColumn,        /* Number of result columns written here */
3799  char **pzErrmsg       /* Error msg written here */
3800);
3801SQLITE_API void sqlite3_free_table(char **result);
3802
3803/*
3804** CAPI3REF: Formatted String Printing Functions
3805**
3806** These routines are work-alikes of the "printf()" family of functions
3807** from the standard C library.
3808** These routines understand most of the common formatting options from
3809** the standard library printf()
3810** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3811** See the [built-in printf()] documentation for details.
3812**
3813** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
3814** results into memory obtained from [sqlite3_malloc64()].
3815** The strings returned by these two routines should be
3816** released by [sqlite3_free()].  ^Both routines return a
3817** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
3818** memory to hold the resulting string.
3819**
3820** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
3821** the standard C library.  The result is written into the
3822** buffer supplied as the second parameter whose size is given by
3823** the first parameter. Note that the order of the
3824** first two parameters is reversed from snprintf().)^  This is an
3825** historical accident that cannot be fixed without breaking
3826** backwards compatibility.  ^(Note also that sqlite3_snprintf()
3827** returns a pointer to its buffer instead of the number of
3828** characters actually written into the buffer.)^  We admit that
3829** the number of characters written would be a more useful return
3830** value but we cannot change the implementation of sqlite3_snprintf()
3831** now without breaking compatibility.
3832**
3833** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
3834** guarantees that the buffer is always zero-terminated.  ^The first
3835** parameter "n" is the total size of the buffer, including space for
3836** the zero terminator.  So the longest string that can be completely
3837** written will be n-1 characters.
3838**
3839** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
3840**
3841** See also:  [built-in printf()], [printf() SQL function]
3842*/
3843SQLITE_API char *sqlite3_mprintf(const char*,...);
3844SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
3845SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
3846SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
3847
3848/*
3849** CAPI3REF: Memory Allocation Subsystem
3850**
3851** The SQLite core uses these three routines for all of its own
3852** internal memory allocation needs. "Core" in the previous sentence
3853** does not include operating-system specific [VFS] implementation.  The
3854** Windows VFS uses native malloc() and free() for some operations.
3855**
3856** ^The sqlite3_malloc() routine returns a pointer to a block
3857** of memory at least N bytes in length, where N is the parameter.
3858** ^If sqlite3_malloc() is unable to obtain sufficient free
3859** memory, it returns a NULL pointer.  ^If the parameter N to
3860** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
3861** a NULL pointer.
3862**
3863** ^The sqlite3_malloc64(N) routine works just like
3864** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3865** of a signed 32-bit integer.
3866**
3867** ^Calling sqlite3_free() with a pointer previously returned
3868** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
3869** that it might be reused.  ^The sqlite3_free() routine is
3870** a no-op if is called with a NULL pointer.  Passing a NULL pointer
3871** to sqlite3_free() is harmless.  After being freed, memory
3872** should neither be read nor written.  Even reading previously freed
3873** memory might result in a segmentation fault or other severe error.
3874** Memory corruption, a segmentation fault, or other severe error
3875** might result if sqlite3_free() is called with a non-NULL pointer that
3876** was not obtained from sqlite3_malloc() or sqlite3_realloc().
3877**
3878** ^The sqlite3_realloc(X,N) interface attempts to resize a
3879** prior memory allocation X to be at least N bytes.
3880** ^If the X parameter to sqlite3_realloc(X,N)
3881** is a NULL pointer then its behavior is identical to calling
3882** sqlite3_malloc(N).
3883** ^If the N parameter to sqlite3_realloc(X,N) is zero or
3884** negative then the behavior is exactly the same as calling
3885** sqlite3_free(X).
3886** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation
3887** of at least N bytes in size or NULL if insufficient memory is available.
3888** ^If M is the size of the prior allocation, then min(N,M) bytes
3889** of the prior allocation are copied into the beginning of buffer returned
3890** by sqlite3_realloc(X,N) and the prior allocation is freed.
3891** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the
3892** prior allocation is not freed.
3893**
3894** ^The sqlite3_realloc64(X,N) interfaces works the same as
3895** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3896** of a 32-bit signed integer.
3897**
3898** ^If X is a memory allocation previously obtained from sqlite3_malloc(),
3899** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then
3900** sqlite3_msize(X) returns the size of that memory allocation in bytes.
3901** ^The value returned by sqlite3_msize(X) might be larger than the number
3902** of bytes requested when X was allocated.  ^If X is a NULL pointer then
3903** sqlite3_msize(X) returns zero.  If X points to something that is not
3904** the beginning of memory allocation, or if it points to a formerly
3905** valid memory allocation that has now been freed, then the behavior
3906** of sqlite3_msize(X) is undefined and possibly harmful.
3907**
3908** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(),
3909** sqlite3_malloc64(), and sqlite3_realloc64()
3910** is always aligned to at least an 8 byte boundary, or to a
3911** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3912** option is used.
3913**
3914** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
3915** must be either NULL or else pointers obtained from a prior
3916** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
3917** not yet been released.
3918**
3919** The application must not read or write any part of
3920** a block of memory after it has been released using
3921** [sqlite3_free()] or [sqlite3_realloc()].
3922*/
3923SQLITE_API void *sqlite3_malloc(int);
3924SQLITE_API void *sqlite3_malloc64(sqlite3_uint64);
3925SQLITE_API void *sqlite3_realloc(void*, int);
3926SQLITE_API void *sqlite3_realloc64(void*, sqlite3_uint64);
3927SQLITE_API void sqlite3_free(void*);
3928SQLITE_API sqlite3_uint64 sqlite3_msize(void*);
3929
3930/*
3931** CAPI3REF: Memory Allocator Statistics
3932**
3933** SQLite provides these two interfaces for reporting on the status
3934** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
3935** routines, which form the built-in memory allocation subsystem.
3936**
3937** ^The [sqlite3_memory_used()] routine returns the number of bytes
3938** of memory currently outstanding (malloced but not freed).
3939** ^The [sqlite3_memory_highwater()] routine returns the maximum
3940** value of [sqlite3_memory_used()] since the high-water mark
3941** was last reset.  ^The values returned by [sqlite3_memory_used()] and
3942** [sqlite3_memory_highwater()] include any overhead
3943** added by SQLite in its implementation of [sqlite3_malloc()],
3944** but not overhead added by the any underlying system library
3945** routines that [sqlite3_malloc()] may call.
3946**
3947** ^The memory high-water mark is reset to the current value of
3948** [sqlite3_memory_used()] if and only if the parameter to
3949** [sqlite3_memory_highwater()] is true.  ^The value returned
3950** by [sqlite3_memory_highwater(1)] is the high-water mark
3951** prior to the reset.
3952*/
3953SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
3954SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
3955
3956/*
3957** CAPI3REF: Pseudo-Random Number Generator
3958**
3959** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3960** select random [ROWID | ROWIDs] when inserting new records into a table that
3961** already uses the largest possible [ROWID].  The PRNG is also used for
3962** the built-in random() and randomblob() SQL functions.  This interface allows
3963** applications to access the same PRNG for other purposes.
3964**
3965** ^A call to this routine stores N bytes of randomness into buffer P.
3966** ^The P parameter can be a NULL pointer.
3967**
3968** ^If this routine has not been previously called or if the previous
3969** call had N less than one or a NULL pointer for P, then the PRNG is
3970** seeded using randomness obtained from the xRandomness method of
3971** the default [sqlite3_vfs] object.
3972** ^If the previous call to this routine had an N of 1 or more and a
3973** non-NULL P then the pseudo-randomness is generated
3974** internally and without recourse to the [sqlite3_vfs] xRandomness
3975** method.
3976*/
3977SQLITE_API void sqlite3_randomness(int N, void *P);
3978
3979/*
3980** CAPI3REF: Compile-Time Authorization Callbacks
3981** METHOD: sqlite3
3982** KEYWORDS: {authorizer callback}
3983**
3984** ^This routine registers an authorizer callback with a particular
3985** [database connection], supplied in the first argument.
3986** ^The authorizer callback is invoked as SQL statements are being compiled
3987** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
3988** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()],
3989** and [sqlite3_prepare16_v3()].  ^At various
3990** points during the compilation process, as logic is being created
3991** to perform various actions, the authorizer callback is invoked to
3992** see if those actions are allowed.  ^The authorizer callback should
3993** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
3994** specific action but allow the SQL statement to continue to be
3995** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
3996** rejected with an error.  ^If the authorizer callback returns
3997** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
3998** then the [sqlite3_prepare_v2()] or equivalent call that triggered
3999** the authorizer will fail with an error message.
4000**
4001** When the callback returns [SQLITE_OK], that means the operation
4002** requested is ok.  ^When the callback returns [SQLITE_DENY], the
4003** [sqlite3_prepare_v2()] or equivalent call that triggered the
4004** authorizer will fail with an error message explaining that
4005** access is denied.
4006**
4007** ^The first parameter to the authorizer callback is a copy of the third
4008** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
4009** to the callback is an integer [SQLITE_COPY | action code] that specifies
4010** the particular action to be authorized. ^The third through sixth parameters
4011** to the callback are either NULL pointers or zero-terminated strings
4012** that contain additional details about the action to be authorized.
4013** Applications must always be prepared to encounter a NULL pointer in any
4014** of the third through the sixth parameters of the authorization callback.
4015**
4016** ^If the action code is [SQLITE_READ]
4017** and the callback returns [SQLITE_IGNORE] then the
4018** [prepared statement] statement is constructed to substitute
4019** a NULL value in place of the table column that would have
4020** been read if [SQLITE_OK] had been returned.  The [SQLITE_IGNORE]
4021** return can be used to deny an untrusted user access to individual
4022** columns of a table.
4023** ^When a table is referenced by a [SELECT] but no column values are
4024** extracted from that table (for example in a query like
4025** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback
4026** is invoked once for that table with a column name that is an empty string.
4027** ^If the action code is [SQLITE_DELETE] and the callback returns
4028** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
4029** [truncate optimization] is disabled and all rows are deleted individually.
4030**
4031** An authorizer is used when [sqlite3_prepare | preparing]
4032** SQL statements from an untrusted source, to ensure that the SQL statements
4033** do not try to access data they are not allowed to see, or that they do not
4034** try to execute malicious statements that damage the database.  For
4035** example, an application may allow a user to enter arbitrary
4036** SQL queries for evaluation by a database.  But the application does
4037** not want the user to be able to make arbitrary changes to the
4038** database.  An authorizer could then be put in place while the
4039** user-entered SQL is being [sqlite3_prepare | prepared] that
4040** disallows everything except [SELECT] statements.
4041**
4042** Applications that need to process SQL from untrusted sources
4043** might also consider lowering resource limits using [sqlite3_limit()]
4044** and limiting database size using the [max_page_count] [PRAGMA]
4045** in addition to using an authorizer.
4046**
4047** ^(Only a single authorizer can be in place on a database connection
4048** at a time.  Each call to sqlite3_set_authorizer overrides the
4049** previous call.)^  ^Disable the authorizer by installing a NULL callback.
4050** The authorizer is disabled by default.
4051**
4052** The authorizer callback must not do anything that will modify
4053** the database connection that invoked the authorizer callback.
4054** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4055** database connections for the meaning of "modify" in this paragraph.
4056**
4057** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
4058** statement might be re-prepared during [sqlite3_step()] due to a
4059** schema change.  Hence, the application should ensure that the
4060** correct authorizer callback remains in place during the [sqlite3_step()].
4061**
4062** ^Note that the authorizer callback is invoked only during
4063** [sqlite3_prepare()] or its variants.  Authorization is not
4064** performed during statement evaluation in [sqlite3_step()], unless
4065** as stated in the previous paragraph, sqlite3_step() invokes
4066** sqlite3_prepare_v2() to reprepare a statement after a schema change.
4067*/
4068SQLITE_API int sqlite3_set_authorizer(
4069  sqlite3*,
4070  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
4071  void *pUserData
4072);
4073
4074/*
4075** CAPI3REF: Authorizer Return Codes
4076**
4077** The [sqlite3_set_authorizer | authorizer callback function] must
4078** return either [SQLITE_OK] or one of these two constants in order
4079** to signal SQLite whether or not the action is permitted.  See the
4080** [sqlite3_set_authorizer | authorizer documentation] for additional
4081** information.
4082**
4083** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
4084** returned from the [sqlite3_vtab_on_conflict()] interface.
4085*/
4086#define SQLITE_DENY   1   /* Abort the SQL statement with an error */
4087#define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
4088
4089/*
4090** CAPI3REF: Authorizer Action Codes
4091**
4092** The [sqlite3_set_authorizer()] interface registers a callback function
4093** that is invoked to authorize certain SQL statement actions.  The
4094** second parameter to the callback is an integer code that specifies
4095** what action is being authorized.  These are the integer action codes that
4096** the authorizer callback may be passed.
4097**
4098** These action code values signify what kind of operation is to be
4099** authorized.  The 3rd and 4th parameters to the authorization
4100** callback function will be parameters or NULL depending on which of these
4101** codes is used as the second parameter.  ^(The 5th parameter to the
4102** authorizer callback is the name of the database ("main", "temp",
4103** etc.) if applicable.)^  ^The 6th parameter to the authorizer callback
4104** is the name of the inner-most trigger or view that is responsible for
4105** the access attempt or NULL if this access attempt is directly from
4106** top-level SQL code.
4107*/
4108/******************************************* 3rd ************ 4th ***********/
4109#define SQLITE_CREATE_INDEX          1   /* Index Name      Table Name      */
4110#define SQLITE_CREATE_TABLE          2   /* Table Name      NULL            */
4111#define SQLITE_CREATE_TEMP_INDEX     3   /* Index Name      Table Name      */
4112#define SQLITE_CREATE_TEMP_TABLE     4   /* Table Name      NULL            */
4113#define SQLITE_CREATE_TEMP_TRIGGER   5   /* Trigger Name    Table Name      */
4114#define SQLITE_CREATE_TEMP_VIEW      6   /* View Name       NULL            */
4115#define SQLITE_CREATE_TRIGGER        7   /* Trigger Name    Table Name      */
4116#define SQLITE_CREATE_VIEW           8   /* View Name       NULL            */
4117#define SQLITE_DELETE                9   /* Table Name      NULL            */
4118#define SQLITE_DROP_INDEX           10   /* Index Name      Table Name      */
4119#define SQLITE_DROP_TABLE           11   /* Table Name      NULL            */
4120#define SQLITE_DROP_TEMP_INDEX      12   /* Index Name      Table Name      */
4121#define SQLITE_DROP_TEMP_TABLE      13   /* Table Name      NULL            */
4122#define SQLITE_DROP_TEMP_TRIGGER    14   /* Trigger Name    Table Name      */
4123#define SQLITE_DROP_TEMP_VIEW       15   /* View Name       NULL            */
4124#define SQLITE_DROP_TRIGGER         16   /* Trigger Name    Table Name      */
4125#define SQLITE_DROP_VIEW            17   /* View Name       NULL            */
4126#define SQLITE_INSERT               18   /* Table Name      NULL            */
4127#define SQLITE_PRAGMA               19   /* Pragma Name     1st arg or NULL */
4128#define SQLITE_READ                 20   /* Table Name      Column Name     */
4129#define SQLITE_SELECT               21   /* NULL            NULL            */
4130#define SQLITE_TRANSACTION          22   /* Operation       NULL            */
4131#define SQLITE_UPDATE               23   /* Table Name      Column Name     */
4132#define SQLITE_ATTACH               24   /* Filename        NULL            */
4133#define SQLITE_DETACH               25   /* Database Name   NULL            */
4134#define SQLITE_ALTER_TABLE          26   /* Database Name   Table Name      */
4135#define SQLITE_REINDEX              27   /* Index Name      NULL            */
4136#define SQLITE_ANALYZE              28   /* Table Name      NULL            */
4137#define SQLITE_CREATE_VTABLE        29   /* Table Name      Module Name     */
4138#define SQLITE_DROP_VTABLE          30   /* Table Name      Module Name     */
4139#define SQLITE_FUNCTION             31   /* NULL            Function Name   */
4140#define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
4141#define SQLITE_COPY                  0   /* No longer used */
4142#define SQLITE_RECURSIVE            33   /* NULL            NULL            */
4143
4144/*
4145** CAPI3REF: Tracing And Profiling Functions
4146** METHOD: sqlite3
4147**
4148** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
4149** instead of the routines described here.
4150**
4151** These routines register callback functions that can be used for
4152** tracing and profiling the execution of SQL statements.
4153**
4154** ^The callback function registered by sqlite3_trace() is invoked at
4155** various times when an SQL statement is being run by [sqlite3_step()].
4156** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
4157** SQL statement text as the statement first begins executing.
4158** ^(Additional sqlite3_trace() callbacks might occur
4159** as each triggered subprogram is entered.  The callbacks for triggers
4160** contain a UTF-8 SQL comment that identifies the trigger.)^
4161**
4162** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
4163** the length of [bound parameter] expansion in the output of sqlite3_trace().
4164**
4165** ^The callback function registered by sqlite3_profile() is invoked
4166** as each SQL statement finishes.  ^The profile callback contains
4167** the original statement text and an estimate of wall-clock time
4168** of how long that statement took to run.  ^The profile callback
4169** time is in units of nanoseconds, however the current implementation
4170** is only capable of millisecond resolution so the six least significant
4171** digits in the time are meaningless.  Future versions of SQLite
4172** might provide greater resolution on the profiler callback.  Invoking
4173** either [sqlite3_trace()] or [sqlite3_trace_v2()] will cancel the
4174** profile callback.
4175*/
4176SQLITE_API SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*,
4177   void(*xTrace)(void*,const char*), void*);
4178SQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
4179   void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
4180
4181/*
4182** CAPI3REF: SQL Trace Event Codes
4183** KEYWORDS: SQLITE_TRACE
4184**
4185** These constants identify classes of events that can be monitored
4186** using the [sqlite3_trace_v2()] tracing logic.  The M argument
4187** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
4188** the following constants.  ^The first argument to the trace callback
4189** is one of the following constants.
4190**
4191** New tracing constants may be added in future releases.
4192**
4193** ^A trace callback has four arguments: xCallback(T,C,P,X).
4194** ^The T argument is one of the integer type codes above.
4195** ^The C argument is a copy of the context pointer passed in as the
4196** fourth argument to [sqlite3_trace_v2()].
4197** The P and X arguments are pointers whose meanings depend on T.
4198**
4199** <dl>
4200** [[SQLITE_TRACE_STMT]] <dt>SQLITE_TRACE_STMT</dt>
4201** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement
4202** first begins running and possibly at other times during the
4203** execution of the prepared statement, such as at the start of each
4204** trigger subprogram. ^The P argument is a pointer to the
4205** [prepared statement]. ^The X argument is a pointer to a string which
4206** is the unexpanded SQL text of the prepared statement or an SQL comment
4207** that indicates the invocation of a trigger.  ^The callback can compute
4208** the same text that would have been returned by the legacy [sqlite3_trace()]
4209** interface by using the X argument when X begins with "--" and invoking
4210** [sqlite3_expanded_sql(P)] otherwise.
4211**
4212** [[SQLITE_TRACE_PROFILE]] <dt>SQLITE_TRACE_PROFILE</dt>
4213** <dd>^An SQLITE_TRACE_PROFILE callback provides approximately the same
4214** information as is provided by the [sqlite3_profile()] callback.
4215** ^The P argument is a pointer to the [prepared statement] and the
4216** X argument points to a 64-bit integer which is the estimated of
4217** the number of nanosecond that the prepared statement took to run.
4218** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes.
4219**
4220** [[SQLITE_TRACE_ROW]] <dt>SQLITE_TRACE_ROW</dt>
4221** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared
4222** statement generates a single row of result.
4223** ^The P argument is a pointer to the [prepared statement] and the
4224** X argument is unused.
4225**
4226** [[SQLITE_TRACE_CLOSE]] <dt>SQLITE_TRACE_CLOSE</dt>
4227** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database
4228** connection closes.
4229** ^The P argument is a pointer to the [database connection] object
4230** and the X argument is unused.
4231** </dl>
4232*/
4233#define SQLITE_TRACE_STMT       0x01
4234#define SQLITE_TRACE_PROFILE    0x02
4235#define SQLITE_TRACE_ROW        0x04
4236#define SQLITE_TRACE_CLOSE      0x08
4237
4238/*
4239** CAPI3REF: SQL Trace Hook
4240** METHOD: sqlite3
4241**
4242** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback
4243** function X against [database connection] D, using property mask M
4244** and context pointer P.  ^If the X callback is
4245** NULL or if the M mask is zero, then tracing is disabled.  The
4246** M argument should be the bitwise OR-ed combination of
4247** zero or more [SQLITE_TRACE] constants.
4248**
4249** ^Each call to either sqlite3_trace() or sqlite3_trace_v2() overrides
4250** (cancels) any prior calls to sqlite3_trace() or sqlite3_trace_v2().
4251**
4252** ^The X callback is invoked whenever any of the events identified by
4253** mask M occur.  ^The integer return value from the callback is currently
4254** ignored, though this may change in future releases.  Callback
4255** implementations should return zero to ensure future compatibility.
4256**
4257** ^A trace callback is invoked with four arguments: callback(T,C,P,X).
4258** ^The T argument is one of the [SQLITE_TRACE]
4259** constants to indicate why the callback was invoked.
4260** ^The C argument is a copy of the context pointer.
4261** The P and X arguments are pointers whose meanings depend on T.
4262**
4263** The sqlite3_trace_v2() interface is intended to replace the legacy
4264** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which
4265** are deprecated.
4266*/
4267SQLITE_API int sqlite3_trace_v2(
4268  sqlite3*,
4269  unsigned uMask,
4270  int(*xCallback)(unsigned,void*,void*,void*),
4271  void *pCtx
4272);
4273
4274/*
4275** CAPI3REF: Query Progress Callbacks
4276** METHOD: sqlite3
4277**
4278** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
4279** function X to be invoked periodically during long running calls to
4280** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
4281** database connection D.  An example use for this
4282** interface is to keep a GUI updated during a large query.
4283**
4284** ^The parameter P is passed through as the only parameter to the
4285** callback function X.  ^The parameter N is the approximate number of
4286** [virtual machine instructions] that are evaluated between successive
4287** invocations of the callback X.  ^If N is less than one then the progress
4288** handler is disabled.
4289**
4290** ^Only a single progress handler may be defined at one time per
4291** [database connection]; setting a new progress handler cancels the
4292** old one.  ^Setting parameter X to NULL disables the progress handler.
4293** ^The progress handler is also disabled by setting N to a value less
4294** than 1.
4295**
4296** ^If the progress callback returns non-zero, the operation is
4297** interrupted.  This feature can be used to implement a
4298** "Cancel" button on a GUI progress dialog box.
4299**
4300** The progress handler callback must not do anything that will modify
4301** the database connection that invoked the progress handler.
4302** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4303** database connections for the meaning of "modify" in this paragraph.
4304**
4305*/
4306SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
4307
4308/*
4309** CAPI3REF: Opening A New Database Connection
4310** CONSTRUCTOR: sqlite3
4311**
4312** ^These routines open an SQLite database file as specified by the
4313** filename argument. ^The filename argument is interpreted as UTF-8 for
4314** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
4315** order for sqlite3_open16(). ^(A [database connection] handle is usually
4316** returned in *ppDb, even if an error occurs.  The only exception is that
4317** if SQLite is unable to allocate memory to hold the [sqlite3] object,
4318** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
4319** object.)^ ^(If the database is opened (and/or created) successfully, then
4320** [SQLITE_OK] is returned.  Otherwise an [error code] is returned.)^ ^The
4321** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
4322** an English language description of the error following a failure of any
4323** of the sqlite3_open() routines.
4324**
4325** ^The default encoding will be UTF-8 for databases created using
4326** sqlite3_open() or sqlite3_open_v2().  ^The default encoding for databases
4327** created using sqlite3_open16() will be UTF-16 in the native byte order.
4328**
4329** Whether or not an error occurs when it is opened, resources
4330** associated with the [database connection] handle should be released by
4331** passing it to [sqlite3_close()] when it is no longer required.
4332**
4333** The sqlite3_open_v2() interface works like sqlite3_open()
4334** except that it accepts two additional parameters for additional control
4335** over the new database connection.  ^(The flags parameter to
4336** sqlite3_open_v2() must include, at a minimum, one of the following
4337** three flag combinations:)^
4338**
4339** <dl>
4340** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
4341** <dd>The database is opened in read-only mode.  If the database does not
4342** already exist, an error is returned.</dd>)^
4343**
4344** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
4345** <dd>The database is opened for reading and writing if possible, or reading
4346** only if the file is write protected by the operating system.  In either
4347** case the database must already exist, otherwise an error is returned.</dd>)^
4348**
4349** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
4350** <dd>The database is opened for reading and writing, and is created if
4351** it does not already exist. This is the behavior that is always used for
4352** sqlite3_open() and sqlite3_open16().</dd>)^
4353** </dl>
4354**
4355** In addition to the required flags, the following optional flags are
4356** also supported:
4357**
4358** <dl>
4359** ^(<dt>[SQLITE_OPEN_URI]</dt>
4360** <dd>The filename can be interpreted as a URI if this flag is set.</dd>)^
4361**
4362** ^(<dt>[SQLITE_OPEN_MEMORY]</dt>
4363** <dd>The database will be opened as an in-memory database.  The database
4364** is named by the "filename" argument for the purposes of cache-sharing,
4365** if shared cache mode is enabled, but the "filename" is otherwise ignored.
4366** </dd>)^
4367**
4368** ^(<dt>[SQLITE_OPEN_NOMUTEX]</dt>
4369** <dd>The new database connection will use the "multi-thread"
4370** [threading mode].)^  This means that separate threads are allowed
4371** to use SQLite at the same time, as long as each thread is using
4372** a different [database connection].
4373**
4374** ^(<dt>[SQLITE_OPEN_FULLMUTEX]</dt>
4375** <dd>The new database connection will use the "serialized"
4376** [threading mode].)^  This means the multiple threads can safely
4377** attempt to use the same database connection at the same time.
4378** (Mutexes will block any actual concurrency, but in this mode
4379** there is no harm in trying.)
4380**
4381** ^(<dt>[SQLITE_OPEN_SHAREDCACHE]</dt>
4382** <dd>The database is opened [shared cache] enabled, overriding
4383** the default shared cache setting provided by
4384** [sqlite3_enable_shared_cache()].)^
4385**
4386** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt>
4387** <dd>The database is opened [shared cache] disabled, overriding
4388** the default shared cache setting provided by
4389** [sqlite3_enable_shared_cache()].)^
4390**
4391** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt>
4392** <dd>The database filename is not allowed to be a symbolic link</dd>
4393** </dl>)^
4394**
4395** If the 3rd parameter to sqlite3_open_v2() is not one of the
4396** required combinations shown above optionally combined with other
4397** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
4398** then the behavior is undefined.
4399**
4400** ^The fourth parameter to sqlite3_open_v2() is the name of the
4401** [sqlite3_vfs] object that defines the operating system interface that
4402** the new database connection should use.  ^If the fourth parameter is
4403** a NULL pointer then the default [sqlite3_vfs] object is used.
4404**
4405** ^If the filename is ":memory:", then a private, temporary in-memory database
4406** is created for the connection.  ^This in-memory database will vanish when
4407** the database connection is closed.  Future versions of SQLite might
4408** make use of additional special filenames that begin with the ":" character.
4409** It is recommended that when a database filename actually does begin with
4410** a ":" character you should prefix the filename with a pathname such as
4411** "./" to avoid ambiguity.
4412**
4413** ^If the filename is an empty string, then a private, temporary
4414** on-disk database will be created.  ^This private database will be
4415** automatically deleted as soon as the database connection is closed.
4416**
4417** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
4418**
4419** ^If [URI filename] interpretation is enabled, and the filename argument
4420** begins with "file:", then the filename is interpreted as a URI. ^URI
4421** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
4422** set in the third argument to sqlite3_open_v2(), or if it has
4423** been enabled globally using the [SQLITE_CONFIG_URI] option with the
4424** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
4425** URI filename interpretation is turned off
4426** by default, but future releases of SQLite might enable URI filename
4427** interpretation by default.  See "[URI filenames]" for additional
4428** information.
4429**
4430** URI filenames are parsed according to RFC 3986. ^If the URI contains an
4431** authority, then it must be either an empty string or the string
4432** "localhost". ^If the authority is not an empty string or "localhost", an
4433** error is returned to the caller. ^The fragment component of a URI, if
4434** present, is ignored.
4435**
4436** ^SQLite uses the path component of the URI as the name of the disk file
4437** which contains the database. ^If the path begins with a '/' character,
4438** then it is interpreted as an absolute path. ^If the path does not begin
4439** with a '/' (meaning that the authority section is omitted from the URI)
4440** then the path is interpreted as a relative path.
4441** ^(On windows, the first component of an absolute path
4442** is a drive specification (e.g. "C:").)^
4443**
4444** [[core URI query parameters]]
4445** The query component of a URI may contain parameters that are interpreted
4446** either by SQLite itself, or by a [VFS | custom VFS implementation].
4447** SQLite and its built-in [VFSes] interpret the
4448** following query parameters:
4449**
4450** <ul>
4451**   <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
4452**     a VFS object that provides the operating system interface that should
4453**     be used to access the database file on disk. ^If this option is set to
4454**     an empty string the default VFS object is used. ^Specifying an unknown
4455**     VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
4456**     present, then the VFS specified by the option takes precedence over
4457**     the value passed as the fourth parameter to sqlite3_open_v2().
4458**
4459**   <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
4460**     "rwc", or "memory". Attempting to set it to any other value is
4461**     an error)^.
4462**     ^If "ro" is specified, then the database is opened for read-only
4463**     access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
4464**     third argument to sqlite3_open_v2(). ^If the mode option is set to
4465**     "rw", then the database is opened for read-write (but not create)
4466**     access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
4467**     been set. ^Value "rwc" is equivalent to setting both
4468**     SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE.  ^If the mode option is
4469**     set to "memory" then a pure [in-memory database] that never reads
4470**     or writes from disk is used. ^It is an error to specify a value for
4471**     the mode parameter that is less restrictive than that specified by
4472**     the flags passed in the third parameter to sqlite3_open_v2().
4473**
4474**   <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
4475**     "private". ^Setting it to "shared" is equivalent to setting the
4476**     SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
4477**     sqlite3_open_v2(). ^Setting the cache parameter to "private" is
4478**     equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
4479**     ^If sqlite3_open_v2() is used and the "cache" parameter is present in
4480**     a URI filename, its value overrides any behavior requested by setting
4481**     SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
4482**
4483**  <li> <b>psow</b>: ^The psow parameter indicates whether or not the
4484**     [powersafe overwrite] property does or does not apply to the
4485**     storage media on which the database file resides.
4486**
4487**  <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
4488**     which if set disables file locking in rollback journal modes.  This
4489**     is useful for accessing a database on a filesystem that does not
4490**     support locking.  Caution:  Database corruption might result if two
4491**     or more processes write to the same database and any one of those
4492**     processes uses nolock=1.
4493**
4494**  <li> <b>immutable</b>: ^The immutable parameter is a boolean query
4495**     parameter that indicates that the database file is stored on
4496**     read-only media.  ^When immutable is set, SQLite assumes that the
4497**     database file cannot be changed, even by a process with higher
4498**     privilege, and so the database is opened read-only and all locking
4499**     and change detection is disabled.  Caution: Setting the immutable
4500**     property on a database file that does in fact change can result
4501**     in incorrect query results and/or [SQLITE_CORRUPT] errors.
4502**     See also: [SQLITE_IOCAP_IMMUTABLE].
4503**
4504** </ul>
4505**
4506** ^Specifying an unknown parameter in the query component of a URI is not an
4507** error.  Future versions of SQLite might understand additional query
4508** parameters.  See "[query parameters with special meaning to SQLite]" for
4509** additional information.
4510**
4511** [[URI filename examples]] <h3>URI filename examples</h3>
4512**
4513** <table border="1" align=center cellpadding=5>
4514** <tr><th> URI filenames <th> Results
4515** <tr><td> file:data.db <td>
4516**          Open the file "data.db" in the current directory.
4517** <tr><td> file:/home/fred/data.db<br>
4518**          file:///home/fred/data.db <br>
4519**          file://localhost/home/fred/data.db <br> <td>
4520**          Open the database file "/home/fred/data.db".
4521** <tr><td> file://darkstar/home/fred/data.db <td>
4522**          An error. "darkstar" is not a recognized authority.
4523** <tr><td style="white-space:nowrap">
4524**          file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
4525**     <td> Windows only: Open the file "data.db" on fred's desktop on drive
4526**          C:. Note that the %20 escaping in this example is not strictly
4527**          necessary - space characters can be used literally
4528**          in URI filenames.
4529** <tr><td> file:data.db?mode=ro&cache=private <td>
4530**          Open file "data.db" in the current directory for read-only access.
4531**          Regardless of whether or not shared-cache mode is enabled by
4532**          default, use a private cache.
4533** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4534**          Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4535**          that uses dot-files in place of posix advisory locking.
4536** <tr><td> file:data.db?mode=readonly <td>
4537**          An error. "readonly" is not a valid option for the "mode" parameter.
4538** </table>
4539**
4540** ^URI hexadecimal escape sequences (%HH) are supported within the path and
4541** query components of a URI. A hexadecimal escape sequence consists of a
4542** percent sign - "%" - followed by exactly two hexadecimal digits
4543** specifying an octet value. ^Before the path or query components of a
4544** URI filename are interpreted, they are encoded using UTF-8 and all
4545** hexadecimal escape sequences replaced by a single byte containing the
4546** corresponding octet. If this process generates an invalid UTF-8 encoding,
4547** the results are undefined.
4548**
4549** <b>Note to Windows users:</b>  The encoding used for the filename argument
4550** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
4551** codepage is currently defined.  Filenames containing international
4552** characters must be converted to UTF-8 prior to passing them into
4553** sqlite3_open() or sqlite3_open_v2().
4554**
4555** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
4556** prior to calling sqlite3_open() or sqlite3_open_v2().  Otherwise, various
4557** features that require the use of temporary files may fail.
4558**
4559** See also: [sqlite3_temp_directory]
4560*/
4561SQLITE_API int sqlite3_open(
4562  const char *filename,   /* Database filename (UTF-8) */
4563  sqlite3 **ppDb          /* OUT: SQLite db handle */
4564);
4565SQLITE_API int sqlite3_open16(
4566  const void *filename,   /* Database filename (UTF-16) */
4567  sqlite3 **ppDb          /* OUT: SQLite db handle */
4568);
4569SQLITE_API int sqlite3_open_v2(
4570  const char *filename,   /* Database filename (UTF-8) */
4571  sqlite3 **ppDb,         /* OUT: SQLite db handle */
4572  int flags,              /* Flags */
4573  const char *zVfs        /* Name of VFS module to use */
4574);
4575
4576/*
4577** CAPI3REF: Obtain Values For URI Parameters
4578**
4579** These are utility routines, useful to [VFS|custom VFS implementations],
4580** that check if a database file was a URI that contained a specific query
4581** parameter, and if so obtains the value of that query parameter.
4582**
4583** The first parameter to these interfaces (hereafter referred to
4584** as F) must be one of:
4585** <ul>
4586** <li> A database filename pointer created by the SQLite core and
4587** passed into the xOpen() method of a VFS implemention, or
4588** <li> A filename obtained from [sqlite3_db_filename()], or
4589** <li> A new filename constructed using [sqlite3_create_filename()].
4590** </ul>
4591** If the F parameter is not one of the above, then the behavior is
4592** undefined and probably undesirable.  Older versions of SQLite were
4593** more tolerant of invalid F parameters than newer versions.
4594**
4595** If F is a suitable filename (as described in the previous paragraph)
4596** and if P is the name of the query parameter, then
4597** sqlite3_uri_parameter(F,P) returns the value of the P
4598** parameter if it exists or a NULL pointer if P does not appear as a
4599** query parameter on F.  If P is a query parameter of F and it
4600** has no explicit value, then sqlite3_uri_parameter(F,P) returns
4601** a pointer to an empty string.
4602**
4603** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
4604** parameter and returns true (1) or false (0) according to the value
4605** of P.  The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
4606** value of query parameter P is one of "yes", "true", or "on" in any
4607** case or if the value begins with a non-zero number.  The
4608** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
4609** query parameter P is one of "no", "false", or "off" in any case or
4610** if the value begins with a numeric zero.  If P is not a query
4611** parameter on F or if the value of P does not match any of the
4612** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
4613**
4614** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
4615** 64-bit signed integer and returns that integer, or D if P does not
4616** exist.  If the value of P is something other than an integer, then
4617** zero is returned.
4618**
4619** The sqlite3_uri_key(F,N) returns a pointer to the name (not
4620** the value) of the N-th query parameter for filename F, or a NULL
4621** pointer if N is less than zero or greater than the number of query
4622** parameters minus 1.  The N value is zero-based so N should be 0 to obtain
4623** the name of the first query parameter, 1 for the second parameter, and
4624** so forth.
4625**
4626** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
4627** sqlite3_uri_boolean(F,P,B) returns B.  If F is not a NULL pointer and
4628** is not a database file pathname pointer that the SQLite core passed
4629** into the xOpen VFS method, then the behavior of this routine is undefined
4630** and probably undesirable.
4631**
4632** Beginning with SQLite [version 3.31.0] ([dateof:3.31.0]) the input F
4633** parameter can also be the name of a rollback journal file or WAL file
4634** in addition to the main database file.  Prior to version 3.31.0, these
4635** routines would only work if F was the name of the main database file.
4636** When the F parameter is the name of the rollback journal or WAL file,
4637** it has access to all the same query parameters as were found on the
4638** main database file.
4639**
4640** See the [URI filename] documentation for additional information.
4641*/
4642SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
4643SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
4644SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
4645SQLITE_API const char *sqlite3_uri_key(const char *zFilename, int N);
4646
4647/*
4648** CAPI3REF:  Translate filenames
4649**
4650** These routines are available to [VFS|custom VFS implementations] for
4651** translating filenames between the main database file, the journal file,
4652** and the WAL file.
4653**
4654** If F is the name of an sqlite database file, journal file, or WAL file
4655** passed by the SQLite core into the VFS, then sqlite3_filename_database(F)
4656** returns the name of the corresponding database file.
4657**
4658** If F is the name of an sqlite database file, journal file, or WAL file
4659** passed by the SQLite core into the VFS, or if F is a database filename
4660** obtained from [sqlite3_db_filename()], then sqlite3_filename_journal(F)
4661** returns the name of the corresponding rollback journal file.
4662**
4663** If F is the name of an sqlite database file, journal file, or WAL file
4664** that was passed by the SQLite core into the VFS, or if F is a database
4665** filename obtained from [sqlite3_db_filename()], then
4666** sqlite3_filename_wal(F) returns the name of the corresponding
4667** WAL file.
4668**
4669** In all of the above, if F is not the name of a database, journal or WAL
4670** filename passed into the VFS from the SQLite core and F is not the
4671** return value from [sqlite3_db_filename()], then the result is
4672** undefined and is likely a memory access violation.
4673*/
4674SQLITE_API const char *sqlite3_filename_database(const char*);
4675SQLITE_API const char *sqlite3_filename_journal(const char*);
4676SQLITE_API const char *sqlite3_filename_wal(const char*);
4677
4678/*
4679** CAPI3REF:  Database File Corresponding To A Journal
4680**
4681** ^If X is the name of a rollback or WAL-mode journal file that is
4682** passed into the xOpen method of [sqlite3_vfs], then
4683** sqlite3_database_file_object(X) returns a pointer to the [sqlite3_file]
4684** object that represents the main database file.
4685**
4686** This routine is intended for use in custom [VFS] implementations
4687** only.  It is not a general-purpose interface.
4688** The argument sqlite3_file_object(X) must be a filename pointer that
4689** has been passed into [sqlite3_vfs].xOpen method where the
4690** flags parameter to xOpen contains one of the bits
4691** [SQLITE_OPEN_MAIN_JOURNAL] or [SQLITE_OPEN_WAL].  Any other use
4692** of this routine results in undefined and probably undesirable
4693** behavior.
4694*/
4695SQLITE_API sqlite3_file *sqlite3_database_file_object(const char*);
4696
4697/*
4698** CAPI3REF: Create and Destroy VFS Filenames
4699**
4700** These interfces are provided for use by [VFS shim] implementations and
4701** are not useful outside of that context.
4702**
4703** The sqlite3_create_filename(D,J,W,N,P) allocates memory to hold a version of
4704** database filename D with corresponding journal file J and WAL file W and
4705** with N URI parameters key/values pairs in the array P.  The result from
4706** sqlite3_create_filename(D,J,W,N,P) is a pointer to a database filename that
4707** is safe to pass to routines like:
4708** <ul>
4709** <li> [sqlite3_uri_parameter()],
4710** <li> [sqlite3_uri_boolean()],
4711** <li> [sqlite3_uri_int64()],
4712** <li> [sqlite3_uri_key()],
4713** <li> [sqlite3_filename_database()],
4714** <li> [sqlite3_filename_journal()], or
4715** <li> [sqlite3_filename_wal()].
4716** </ul>
4717** If a memory allocation error occurs, sqlite3_create_filename() might
4718** return a NULL pointer.  The memory obtained from sqlite3_create_filename(X)
4719** must be released by a corresponding call to sqlite3_free_filename(Y).
4720**
4721** The P parameter in sqlite3_create_filename(D,J,W,N,P) should be an array
4722** of 2*N pointers to strings.  Each pair of pointers in this array corresponds
4723** to a key and value for a query parameter.  The P parameter may be a NULL
4724** pointer if N is zero.  None of the 2*N pointers in the P array may be
4725** NULL pointers and key pointers should not be empty strings.
4726** None of the D, J, or W parameters to sqlite3_create_filename(D,J,W,N,P) may
4727** be NULL pointers, though they can be empty strings.
4728**
4729** The sqlite3_free_filename(Y) routine releases a memory allocation
4730** previously obtained from sqlite3_create_filename().  Invoking
4731** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
4732**
4733** If the Y parameter to sqlite3_free_filename(Y) is anything other
4734** than a NULL pointer or a pointer previously acquired from
4735** sqlite3_create_filename(), then bad things such as heap
4736** corruption or segfaults may occur. The value Y should be
4737** used again after sqlite3_free_filename(Y) has been called.  This means
4738** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y,
4739** then the corresponding [sqlite3_module.xClose() method should also be
4740** invoked prior to calling sqlite3_free_filename(Y).
4741*/
4742SQLITE_API char *sqlite3_create_filename(
4743  const char *zDatabase,
4744  const char *zJournal,
4745  const char *zWal,
4746  int nParam,
4747  const char **azParam
4748);
4749SQLITE_API void sqlite3_free_filename(char*);
4750
4751/*
4752** CAPI3REF: Error Codes And Messages
4753** METHOD: sqlite3
4754**
4755** ^If the most recent sqlite3_* API call associated with
4756** [database connection] D failed, then the sqlite3_errcode(D) interface
4757** returns the numeric [result code] or [extended result code] for that
4758** API call.
4759** ^The sqlite3_extended_errcode()
4760** interface is the same except that it always returns the
4761** [extended result code] even when extended result codes are
4762** disabled.
4763**
4764** The values returned by sqlite3_errcode() and/or
4765** sqlite3_extended_errcode() might change with each API call.
4766** Except, there are some interfaces that are guaranteed to never
4767** change the value of the error code.  The error-code preserving
4768** interfaces are:
4769**
4770** <ul>
4771** <li> sqlite3_errcode()
4772** <li> sqlite3_extended_errcode()
4773** <li> sqlite3_errmsg()
4774** <li> sqlite3_errmsg16()
4775** </ul>
4776**
4777** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4778** text that describes the error, as either UTF-8 or UTF-16 respectively.
4779** ^(Memory to hold the error message string is managed internally.
4780** The application does not need to worry about freeing the result.
4781** However, the error string might be overwritten or deallocated by
4782** subsequent calls to other SQLite interface functions.)^
4783**
4784** ^The sqlite3_errstr() interface returns the English-language text
4785** that describes the [result code], as UTF-8.
4786** ^(Memory to hold the error message string is managed internally
4787** and must not be freed by the application)^.
4788**
4789** When the serialized [threading mode] is in use, it might be the
4790** case that a second error occurs on a separate thread in between
4791** the time of the first error and the call to these interfaces.
4792** When that happens, the second error will be reported since these
4793** interfaces always report the most recent result.  To avoid
4794** this, each thread can obtain exclusive use of the [database connection] D
4795** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
4796** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
4797** all calls to the interfaces listed here are completed.
4798**
4799** If an interface fails with SQLITE_MISUSE, that means the interface
4800** was invoked incorrectly by the application.  In that case, the
4801** error code and message may or may not be set.
4802*/
4803SQLITE_API int sqlite3_errcode(sqlite3 *db);
4804SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
4805SQLITE_API const char *sqlite3_errmsg(sqlite3*);
4806SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
4807SQLITE_API const char *sqlite3_errstr(int);
4808
4809/*
4810** CAPI3REF: Prepared Statement Object
4811** KEYWORDS: {prepared statement} {prepared statements}
4812**
4813** An instance of this object represents a single SQL statement that
4814** has been compiled into binary form and is ready to be evaluated.
4815**
4816** Think of each SQL statement as a separate computer program.  The
4817** original SQL text is source code.  A prepared statement object
4818** is the compiled object code.  All SQL must be converted into a
4819** prepared statement before it can be run.
4820**
4821** The life-cycle of a prepared statement object usually goes like this:
4822**
4823** <ol>
4824** <li> Create the prepared statement object using [sqlite3_prepare_v2()].
4825** <li> Bind values to [parameters] using the sqlite3_bind_*()
4826**      interfaces.
4827** <li> Run the SQL by calling [sqlite3_step()] one or more times.
4828** <li> Reset the prepared statement using [sqlite3_reset()] then go back
4829**      to step 2.  Do this zero or more times.
4830** <li> Destroy the object using [sqlite3_finalize()].
4831** </ol>
4832*/
4833typedef struct sqlite3_stmt sqlite3_stmt;
4834
4835/*
4836** CAPI3REF: Run-time Limits
4837** METHOD: sqlite3
4838**
4839** ^(This interface allows the size of various constructs to be limited
4840** on a connection by connection basis.  The first parameter is the
4841** [database connection] whose limit is to be set or queried.  The
4842** second parameter is one of the [limit categories] that define a
4843** class of constructs to be size limited.  The third parameter is the
4844** new limit for that construct.)^
4845**
4846** ^If the new limit is a negative number, the limit is unchanged.
4847** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
4848** [limits | hard upper bound]
4849** set at compile-time by a C preprocessor macro called
4850** [limits | SQLITE_MAX_<i>NAME</i>].
4851** (The "_LIMIT_" in the name is changed to "_MAX_".))^
4852** ^Attempts to increase a limit above its hard upper bound are
4853** silently truncated to the hard upper bound.
4854**
4855** ^Regardless of whether or not the limit was changed, the
4856** [sqlite3_limit()] interface returns the prior value of the limit.
4857** ^Hence, to find the current value of a limit without changing it,
4858** simply invoke this interface with the third parameter set to -1.
4859**
4860** Run-time limits are intended for use in applications that manage
4861** both their own internal database and also databases that are controlled
4862** by untrusted external sources.  An example application might be a
4863** web browser that has its own databases for storing history and
4864** separate databases controlled by JavaScript applications downloaded
4865** off the Internet.  The internal databases can be given the
4866** large, default limits.  Databases managed by external sources can
4867** be given much smaller limits designed to prevent a denial of service
4868** attack.  Developers might also want to use the [sqlite3_set_authorizer()]
4869** interface to further control untrusted SQL.  The size of the database
4870** created by an untrusted script can be contained using the
4871** [max_page_count] [PRAGMA].
4872**
4873** New run-time limit categories may be added in future releases.
4874*/
4875SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
4876
4877/*
4878** CAPI3REF: Run-Time Limit Categories
4879** KEYWORDS: {limit category} {*limit categories}
4880**
4881** These constants define various performance limits
4882** that can be lowered at run-time using [sqlite3_limit()].
4883** The synopsis of the meanings of the various limits is shown below.
4884** Additional information is available at [limits | Limits in SQLite].
4885**
4886** <dl>
4887** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
4888** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
4889**
4890** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
4891** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
4892**
4893** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
4894** <dd>The maximum number of columns in a table definition or in the
4895** result set of a [SELECT] or the maximum number of columns in an index
4896** or in an ORDER BY or GROUP BY clause.</dd>)^
4897**
4898** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
4899** <dd>The maximum depth of the parse tree on any expression.</dd>)^
4900**
4901** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
4902** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
4903**
4904** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
4905** <dd>The maximum number of instructions in a virtual machine program
4906** used to implement an SQL statement.  If [sqlite3_prepare_v2()] or
4907** the equivalent tries to allocate space for more than this many opcodes
4908** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
4909**
4910** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
4911** <dd>The maximum number of arguments on a function.</dd>)^
4912**
4913** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
4914** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
4915**
4916** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
4917** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
4918** <dd>The maximum length of the pattern argument to the [LIKE] or
4919** [GLOB] operators.</dd>)^
4920**
4921** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
4922** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
4923** <dd>The maximum index number of any [parameter] in an SQL statement.)^
4924**
4925** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
4926** <dd>The maximum depth of recursion for triggers.</dd>)^
4927**
4928** [[SQLITE_LIMIT_WORKER_THREADS]] ^(<dt>SQLITE_LIMIT_WORKER_THREADS</dt>
4929** <dd>The maximum number of auxiliary worker threads that a single
4930** [prepared statement] may start.</dd>)^
4931** </dl>
4932*/
4933#define SQLITE_LIMIT_LENGTH                    0
4934#define SQLITE_LIMIT_SQL_LENGTH                1
4935#define SQLITE_LIMIT_COLUMN                    2
4936#define SQLITE_LIMIT_EXPR_DEPTH                3
4937#define SQLITE_LIMIT_COMPOUND_SELECT           4
4938#define SQLITE_LIMIT_VDBE_OP                   5
4939#define SQLITE_LIMIT_FUNCTION_ARG              6
4940#define SQLITE_LIMIT_ATTACHED                  7
4941#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
4942#define SQLITE_LIMIT_VARIABLE_NUMBER           9
4943#define SQLITE_LIMIT_TRIGGER_DEPTH            10
4944#define SQLITE_LIMIT_WORKER_THREADS           11
4945
4946/*
4947** CAPI3REF: Prepare Flags
4948**
4949** These constants define various flags that can be passed into
4950** "prepFlags" parameter of the [sqlite3_prepare_v3()] and
4951** [sqlite3_prepare16_v3()] interfaces.
4952**
4953** New flags may be added in future releases of SQLite.
4954**
4955** <dl>
4956** [[SQLITE_PREPARE_PERSISTENT]] ^(<dt>SQLITE_PREPARE_PERSISTENT</dt>
4957** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner
4958** that the prepared statement will be retained for a long time and
4959** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
4960** and [sqlite3_prepare16_v3()] assume that the prepared statement will
4961** be used just once or at most a few times and then destroyed using
4962** [sqlite3_finalize()] relatively soon. The current implementation acts
4963** on this hint by avoiding the use of [lookaside memory] so as not to
4964** deplete the limited store of lookaside memory. Future versions of
4965** SQLite may act on this hint differently.
4966**
4967** [[SQLITE_PREPARE_NORMALIZE]] <dt>SQLITE_PREPARE_NORMALIZE</dt>
4968** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
4969** to be required for any prepared statement that wanted to use the
4970** [sqlite3_normalized_sql()] interface.  However, the
4971** [sqlite3_normalized_sql()] interface is now available to all
4972** prepared statements, regardless of whether or not they use this
4973** flag.
4974**
4975** [[SQLITE_PREPARE_NO_VTAB]] <dt>SQLITE_PREPARE_NO_VTAB</dt>
4976** <dd>The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler
4977** to return an error (error code SQLITE_ERROR) if the statement uses
4978** any virtual tables.
4979** </dl>
4980*/
4981#define SQLITE_PREPARE_PERSISTENT              0x01
4982#define SQLITE_PREPARE_NORMALIZE               0x02
4983#define SQLITE_PREPARE_NO_VTAB                 0x04
4984
4985/*
4986** CAPI3REF: Compiling An SQL Statement
4987** KEYWORDS: {SQL statement compiler}
4988** METHOD: sqlite3
4989** CONSTRUCTOR: sqlite3_stmt
4990**
4991** To execute an SQL statement, it must first be compiled into a byte-code
4992** program using one of these routines.  Or, in other words, these routines
4993** are constructors for the [prepared statement] object.
4994**
4995** The preferred routine to use is [sqlite3_prepare_v2()].  The
4996** [sqlite3_prepare()] interface is legacy and should be avoided.
4997** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used
4998** for special purposes.
4999**
5000** The use of the UTF-8 interfaces is preferred, as SQLite currently
5001** does all parsing using UTF-8.  The UTF-16 interfaces are provided
5002** as a convenience.  The UTF-16 interfaces work by converting the
5003** input text into UTF-8, then invoking the corresponding UTF-8 interface.
5004**
5005** The first argument, "db", is a [database connection] obtained from a
5006** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
5007** [sqlite3_open16()].  The database connection must not have been closed.
5008**
5009** The second argument, "zSql", is the statement to be compiled, encoded
5010** as either UTF-8 or UTF-16.  The sqlite3_prepare(), sqlite3_prepare_v2(),
5011** and sqlite3_prepare_v3()
5012** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
5013** and sqlite3_prepare16_v3() use UTF-16.
5014**
5015** ^If the nByte argument is negative, then zSql is read up to the
5016** first zero terminator. ^If nByte is positive, then it is the
5017** number of bytes read from zSql.  ^If nByte is zero, then no prepared
5018** statement is generated.
5019** If the caller knows that the supplied string is nul-terminated, then
5020** there is a small performance advantage to passing an nByte parameter that
5021** is the number of bytes in the input string <i>including</i>
5022** the nul-terminator.
5023**
5024** ^If pzTail is not NULL then *pzTail is made to point to the first byte
5025** past the end of the first SQL statement in zSql.  These routines only
5026** compile the first statement in zSql, so *pzTail is left pointing to
5027** what remains uncompiled.
5028**
5029** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
5030** executed using [sqlite3_step()].  ^If there is an error, *ppStmt is set
5031** to NULL.  ^If the input text contains no SQL (if the input is an empty
5032** string or a comment) then *ppStmt is set to NULL.
5033** The calling procedure is responsible for deleting the compiled
5034** SQL statement using [sqlite3_finalize()] after it has finished with it.
5035** ppStmt may not be NULL.
5036**
5037** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
5038** otherwise an [error code] is returned.
5039**
5040** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(),
5041** and sqlite3_prepare16_v3() interfaces are recommended for all new programs.
5042** The older interfaces (sqlite3_prepare() and sqlite3_prepare16())
5043** are retained for backwards compatibility, but their use is discouraged.
5044** ^In the "vX" interfaces, the prepared statement
5045** that is returned (the [sqlite3_stmt] object) contains a copy of the
5046** original SQL text. This causes the [sqlite3_step()] interface to
5047** behave differently in three ways:
5048**
5049** <ol>
5050** <li>
5051** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
5052** always used to do, [sqlite3_step()] will automatically recompile the SQL
5053** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
5054** retries will occur before sqlite3_step() gives up and returns an error.
5055** </li>
5056**
5057** <li>
5058** ^When an error occurs, [sqlite3_step()] will return one of the detailed
5059** [error codes] or [extended error codes].  ^The legacy behavior was that
5060** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
5061** and the application would have to make a second call to [sqlite3_reset()]
5062** in order to find the underlying cause of the problem. With the "v2" prepare
5063** interfaces, the underlying reason for the error is returned immediately.
5064** </li>
5065**
5066** <li>
5067** ^If the specific value bound to a [parameter | host parameter] in the
5068** WHERE clause might influence the choice of query plan for a statement,
5069** then the statement will be automatically recompiled, as if there had been
5070** a schema change, on the first [sqlite3_step()] call following any change
5071** to the [sqlite3_bind_text | bindings] of that [parameter].
5072** ^The specific value of a WHERE-clause [parameter] might influence the
5073** choice of query plan if the parameter is the left-hand side of a [LIKE]
5074** or [GLOB] operator or if the parameter is compared to an indexed column
5075** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
5076** </li>
5077** </ol>
5078**
5079** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
5080** the extra prepFlags parameter, which is a bit array consisting of zero or
5081** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags.  ^The
5082** sqlite3_prepare_v2() interface works exactly the same as
5083** sqlite3_prepare_v3() with a zero prepFlags parameter.
5084*/
5085SQLITE_API int sqlite3_prepare(
5086  sqlite3 *db,            /* Database handle */
5087  const char *zSql,       /* SQL statement, UTF-8 encoded */
5088  int nByte,              /* Maximum length of zSql in bytes. */
5089  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
5090  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
5091);
5092SQLITE_API int sqlite3_prepare_v2(
5093  sqlite3 *db,            /* Database handle */
5094  const char *zSql,       /* SQL statement, UTF-8 encoded */
5095  int nByte,              /* Maximum length of zSql in bytes. */
5096  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
5097  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
5098);
5099SQLITE_API int sqlite3_prepare_v3(
5100  sqlite3 *db,            /* Database handle */
5101  const char *zSql,       /* SQL statement, UTF-8 encoded */
5102  int nByte,              /* Maximum length of zSql in bytes. */
5103  unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
5104  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
5105  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
5106);
5107SQLITE_API int sqlite3_prepare16(
5108  sqlite3 *db,            /* Database handle */
5109  const void *zSql,       /* SQL statement, UTF-16 encoded */
5110  int nByte,              /* Maximum length of zSql in bytes. */
5111  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
5112  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
5113);
5114SQLITE_API int sqlite3_prepare16_v2(
5115  sqlite3 *db,            /* Database handle */
5116  const void *zSql,       /* SQL statement, UTF-16 encoded */
5117  int nByte,              /* Maximum length of zSql in bytes. */
5118  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
5119  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
5120);
5121SQLITE_API int sqlite3_prepare16_v3(
5122  sqlite3 *db,            /* Database handle */
5123  const void *zSql,       /* SQL statement, UTF-16 encoded */
5124  int nByte,              /* Maximum length of zSql in bytes. */
5125  unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
5126  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
5127  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
5128);
5129
5130/*
5131** CAPI3REF: Retrieving Statement SQL
5132** METHOD: sqlite3_stmt
5133**
5134** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
5135** SQL text used to create [prepared statement] P if P was
5136** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
5137** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
5138** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
5139** string containing the SQL text of prepared statement P with
5140** [bound parameters] expanded.
5141** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
5142** string containing the normalized SQL text of prepared statement P.  The
5143** semantics used to normalize a SQL statement are unspecified and subject
5144** to change.  At a minimum, literal values will be replaced with suitable
5145** placeholders.
5146**
5147** ^(For example, if a prepared statement is created using the SQL
5148** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
5149** and parameter :xyz is unbound, then sqlite3_sql() will return
5150** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
5151** will return "SELECT 2345,NULL".)^
5152**
5153** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory
5154** is available to hold the result, or if the result would exceed the
5155** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
5156**
5157** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
5158** bound parameter expansions.  ^The [SQLITE_OMIT_TRACE] compile-time
5159** option causes sqlite3_expanded_sql() to always return NULL.
5160**
5161** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
5162** are managed by SQLite and are automatically freed when the prepared
5163** statement is finalized.
5164** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
5165** is obtained from [sqlite3_malloc()] and must be free by the application
5166** by passing it to [sqlite3_free()].
5167*/
5168SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
5169SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
5170SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
5171
5172/*
5173** CAPI3REF: Determine If An SQL Statement Writes The Database
5174** METHOD: sqlite3_stmt
5175**
5176** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
5177** and only if the [prepared statement] X makes no direct changes to
5178** the content of the database file.
5179**
5180** Note that [application-defined SQL functions] or
5181** [virtual tables] might change the database indirectly as a side effect.
5182** ^(For example, if an application defines a function "eval()" that
5183** calls [sqlite3_exec()], then the following SQL statement would
5184** change the database file through side-effects:
5185**
5186** <blockquote><pre>
5187**    SELECT eval('DELETE FROM t1') FROM t2;
5188** </pre></blockquote>
5189**
5190** But because the [SELECT] statement does not change the database file
5191** directly, sqlite3_stmt_readonly() would still return true.)^
5192**
5193** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
5194** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
5195** since the statements themselves do not actually modify the database but
5196** rather they control the timing of when other statements modify the
5197** database.  ^The [ATTACH] and [DETACH] statements also cause
5198** sqlite3_stmt_readonly() to return true since, while those statements
5199** change the configuration of a database connection, they do not make
5200** changes to the content of the database files on disk.
5201** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since
5202** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and
5203** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
5204** sqlite3_stmt_readonly() returns false for those commands.
5205*/
5206SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
5207
5208/*
5209** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement
5210** METHOD: sqlite3_stmt
5211**
5212** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the
5213** prepared statement S is an EXPLAIN statement, or 2 if the
5214** statement S is an EXPLAIN QUERY PLAN.
5215** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is
5216** an ordinary statement or a NULL pointer.
5217*/
5218SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
5219
5220/*
5221** CAPI3REF: Determine If A Prepared Statement Has Been Reset
5222** METHOD: sqlite3_stmt
5223**
5224** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
5225** [prepared statement] S has been stepped at least once using
5226** [sqlite3_step(S)] but has neither run to completion (returned
5227** [SQLITE_DONE] from [sqlite3_step(S)]) nor
5228** been reset using [sqlite3_reset(S)].  ^The sqlite3_stmt_busy(S)
5229** interface returns false if S is a NULL pointer.  If S is not a
5230** NULL pointer and is not a pointer to a valid [prepared statement]
5231** object, then the behavior is undefined and probably undesirable.
5232**
5233** This interface can be used in combination [sqlite3_next_stmt()]
5234** to locate all prepared statements associated with a database
5235** connection that are in need of being reset.  This can be used,
5236** for example, in diagnostic routines to search for prepared
5237** statements that are holding a transaction open.
5238*/
5239SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
5240
5241/*
5242** CAPI3REF: Dynamically Typed Value Object
5243** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
5244**
5245** SQLite uses the sqlite3_value object to represent all values
5246** that can be stored in a database table. SQLite uses dynamic typing
5247** for the values it stores.  ^Values stored in sqlite3_value objects
5248** can be integers, floating point values, strings, BLOBs, or NULL.
5249**
5250** An sqlite3_value object may be either "protected" or "unprotected".
5251** Some interfaces require a protected sqlite3_value.  Other interfaces
5252** will accept either a protected or an unprotected sqlite3_value.
5253** Every interface that accepts sqlite3_value arguments specifies
5254** whether or not it requires a protected sqlite3_value.  The
5255** [sqlite3_value_dup()] interface can be used to construct a new
5256** protected sqlite3_value from an unprotected sqlite3_value.
5257**
5258** The terms "protected" and "unprotected" refer to whether or not
5259** a mutex is held.  An internal mutex is held for a protected
5260** sqlite3_value object but no mutex is held for an unprotected
5261** sqlite3_value object.  If SQLite is compiled to be single-threaded
5262** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
5263** or if SQLite is run in one of reduced mutex modes
5264** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
5265** then there is no distinction between protected and unprotected
5266** sqlite3_value objects and they can be used interchangeably.  However,
5267** for maximum code portability it is recommended that applications
5268** still make the distinction between protected and unprotected
5269** sqlite3_value objects even when not strictly required.
5270**
5271** ^The sqlite3_value objects that are passed as parameters into the
5272** implementation of [application-defined SQL functions] are protected.
5273** ^The sqlite3_value object returned by
5274** [sqlite3_column_value()] is unprotected.
5275** Unprotected sqlite3_value objects may only be used as arguments
5276** to [sqlite3_result_value()], [sqlite3_bind_value()], and
5277** [sqlite3_value_dup()].
5278** The [sqlite3_value_blob | sqlite3_value_type()] family of
5279** interfaces require protected sqlite3_value objects.
5280*/
5281typedef struct sqlite3_value sqlite3_value;
5282
5283/*
5284** CAPI3REF: SQL Function Context Object
5285**
5286** The context in which an SQL function executes is stored in an
5287** sqlite3_context object.  ^A pointer to an sqlite3_context object
5288** is always first parameter to [application-defined SQL functions].
5289** The application-defined SQL function implementation will pass this
5290** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
5291** [sqlite3_aggregate_context()], [sqlite3_user_data()],
5292** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
5293** and/or [sqlite3_set_auxdata()].
5294*/
5295typedef struct sqlite3_context sqlite3_context;
5296
5297/*
5298** CAPI3REF: Binding Values To Prepared Statements
5299** KEYWORDS: {host parameter} {host parameters} {host parameter name}
5300** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
5301** METHOD: sqlite3_stmt
5302**
5303** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
5304** literals may be replaced by a [parameter] that matches one of following
5305** templates:
5306**
5307** <ul>
5308** <li>  ?
5309** <li>  ?NNN
5310** <li>  :VVV
5311** <li>  @VVV
5312** <li>  $VVV
5313** </ul>
5314**
5315** In the templates above, NNN represents an integer literal,
5316** and VVV represents an alphanumeric identifier.)^  ^The values of these
5317** parameters (also called "host parameter names" or "SQL parameters")
5318** can be set using the sqlite3_bind_*() routines defined here.
5319**
5320** ^The first argument to the sqlite3_bind_*() routines is always
5321** a pointer to the [sqlite3_stmt] object returned from
5322** [sqlite3_prepare_v2()] or its variants.
5323**
5324** ^The second argument is the index of the SQL parameter to be set.
5325** ^The leftmost SQL parameter has an index of 1.  ^When the same named
5326** SQL parameter is used more than once, second and subsequent
5327** occurrences have the same index as the first occurrence.
5328** ^The index for named parameters can be looked up using the
5329** [sqlite3_bind_parameter_index()] API if desired.  ^The index
5330** for "?NNN" parameters is the value of NNN.
5331** ^The NNN value must be between 1 and the [sqlite3_limit()]
5332** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 32766).
5333**
5334** ^The third argument is the value to bind to the parameter.
5335** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16()
5336** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
5337** is ignored and the end result is the same as sqlite3_bind_null().
5338** ^If the third parameter to sqlite3_bind_text() is not NULL, then
5339** it should be a pointer to well-formed UTF8 text.
5340** ^If the third parameter to sqlite3_bind_text16() is not NULL, then
5341** it should be a pointer to well-formed UTF16 text.
5342** ^If the third parameter to sqlite3_bind_text64() is not NULL, then
5343** it should be a pointer to a well-formed unicode string that is
5344** either UTF8 if the sixth parameter is SQLITE_UTF8, or UTF16
5345** otherwise.
5346**
5347** [[byte-order determination rules]] ^The byte-order of
5348** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
5349** found in first character, which is removed, or in the absence of a BOM
5350** the byte order is the native byte order of the host
5351** machine for sqlite3_bind_text16() or the byte order specified in
5352** the 6th parameter for sqlite3_bind_text64().)^
5353** ^If UTF16 input text contains invalid unicode
5354** characters, then SQLite might change those invalid characters
5355** into the unicode replacement character: U+FFFD.
5356**
5357** ^(In those routines that have a fourth argument, its value is the
5358** number of bytes in the parameter.  To be clear: the value is the
5359** number of <u>bytes</u> in the value, not the number of characters.)^
5360** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
5361** is negative, then the length of the string is
5362** the number of bytes up to the first zero terminator.
5363** If the fourth parameter to sqlite3_bind_blob() is negative, then
5364** the behavior is undefined.
5365** If a non-negative fourth parameter is provided to sqlite3_bind_text()
5366** or sqlite3_bind_text16() or sqlite3_bind_text64() then
5367** that parameter must be the byte offset
5368** where the NUL terminator would occur assuming the string were NUL
5369** terminated.  If any NUL characters occurs at byte offsets less than
5370** the value of the fourth parameter then the resulting string value will
5371** contain embedded NULs.  The result of expressions involving strings
5372** with embedded NULs is undefined.
5373**
5374** ^The fifth argument to the BLOB and string binding interfaces
5375** is a destructor used to dispose of the BLOB or
5376** string after SQLite has finished with it.  ^The destructor is called
5377** to dispose of the BLOB or string even if the call to the bind API fails,
5378** except the destructor is not called if the third parameter is a NULL
5379** pointer or the fourth parameter is negative.
5380** ^If the fifth argument is
5381** the special value [SQLITE_STATIC], then SQLite assumes that the
5382** information is in static, unmanaged space and does not need to be freed.
5383** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
5384** SQLite makes its own private copy of the data immediately, before
5385** the sqlite3_bind_*() routine returns.
5386**
5387** ^The sixth argument to sqlite3_bind_text64() must be one of
5388** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
5389** to specify the encoding of the text in the third parameter.  If
5390** the sixth argument to sqlite3_bind_text64() is not one of the
5391** allowed values shown above, or if the text encoding is different
5392** from the encoding specified by the sixth parameter, then the behavior
5393** is undefined.
5394**
5395** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
5396** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
5397** (just an integer to hold its size) while it is being processed.
5398** Zeroblobs are intended to serve as placeholders for BLOBs whose
5399** content is later written using
5400** [sqlite3_blob_open | incremental BLOB I/O] routines.
5401** ^A negative value for the zeroblob results in a zero-length BLOB.
5402**
5403** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
5404** [prepared statement] S to have an SQL value of NULL, but to also be
5405** associated with the pointer P of type T.  ^D is either a NULL pointer or
5406** a pointer to a destructor function for P. ^SQLite will invoke the
5407** destructor D with a single argument of P when it is finished using
5408** P.  The T parameter should be a static string, preferably a string
5409** literal. The sqlite3_bind_pointer() routine is part of the
5410** [pointer passing interface] added for SQLite 3.20.0.
5411**
5412** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
5413** for the [prepared statement] or with a prepared statement for which
5414** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
5415** then the call will return [SQLITE_MISUSE].  If any sqlite3_bind_()
5416** routine is passed a [prepared statement] that has been finalized, the
5417** result is undefined and probably harmful.
5418**
5419** ^Bindings are not cleared by the [sqlite3_reset()] routine.
5420** ^Unbound parameters are interpreted as NULL.
5421**
5422** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
5423** [error code] if anything goes wrong.
5424** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB
5425** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or
5426** [SQLITE_MAX_LENGTH].
5427** ^[SQLITE_RANGE] is returned if the parameter
5428** index is out of range.  ^[SQLITE_NOMEM] is returned if malloc() fails.
5429**
5430** See also: [sqlite3_bind_parameter_count()],
5431** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
5432*/
5433SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
5434SQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
5435                        void(*)(void*));
5436SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
5437SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
5438SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
5439SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
5440SQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
5441SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
5442SQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
5443                         void(*)(void*), unsigned char encoding);
5444SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
5445SQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
5446SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
5447SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
5448
5449/*
5450** CAPI3REF: Number Of SQL Parameters
5451** METHOD: sqlite3_stmt
5452**
5453** ^This routine can be used to find the number of [SQL parameters]
5454** in a [prepared statement].  SQL parameters are tokens of the
5455** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
5456** placeholders for values that are [sqlite3_bind_blob | bound]
5457** to the parameters at a later time.
5458**
5459** ^(This routine actually returns the index of the largest (rightmost)
5460** parameter. For all forms except ?NNN, this will correspond to the
5461** number of unique parameters.  If parameters of the ?NNN form are used,
5462** there may be gaps in the list.)^
5463**
5464** See also: [sqlite3_bind_blob|sqlite3_bind()],
5465** [sqlite3_bind_parameter_name()], and
5466** [sqlite3_bind_parameter_index()].
5467*/
5468SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
5469
5470/*
5471** CAPI3REF: Name Of A Host Parameter
5472** METHOD: sqlite3_stmt
5473**
5474** ^The sqlite3_bind_parameter_name(P,N) interface returns
5475** the name of the N-th [SQL parameter] in the [prepared statement] P.
5476** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
5477** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
5478** respectively.
5479** In other words, the initial ":" or "$" or "@" or "?"
5480** is included as part of the name.)^
5481** ^Parameters of the form "?" without a following integer have no name
5482** and are referred to as "nameless" or "anonymous parameters".
5483**
5484** ^The first host parameter has an index of 1, not 0.
5485**
5486** ^If the value N is out of range or if the N-th parameter is
5487** nameless, then NULL is returned.  ^The returned string is
5488** always in UTF-8 encoding even if the named parameter was
5489** originally specified as UTF-16 in [sqlite3_prepare16()],
5490** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
5491**
5492** See also: [sqlite3_bind_blob|sqlite3_bind()],
5493** [sqlite3_bind_parameter_count()], and
5494** [sqlite3_bind_parameter_index()].
5495*/
5496SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
5497
5498/*
5499** CAPI3REF: Index Of A Parameter With A Given Name
5500** METHOD: sqlite3_stmt
5501**
5502** ^Return the index of an SQL parameter given its name.  ^The
5503** index value returned is suitable for use as the second
5504** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
5505** is returned if no matching parameter is found.  ^The parameter
5506** name must be given in UTF-8 even if the original statement
5507** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5508** [sqlite3_prepare16_v3()].
5509**
5510** See also: [sqlite3_bind_blob|sqlite3_bind()],
5511** [sqlite3_bind_parameter_count()], and
5512** [sqlite3_bind_parameter_name()].
5513*/
5514SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
5515
5516/*
5517** CAPI3REF: Reset All Bindings On A Prepared Statement
5518** METHOD: sqlite3_stmt
5519**
5520** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
5521** the [sqlite3_bind_blob | bindings] on a [prepared statement].
5522** ^Use this routine to reset all host parameters to NULL.
5523*/
5524SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
5525
5526/*
5527** CAPI3REF: Number Of Columns In A Result Set
5528** METHOD: sqlite3_stmt
5529**
5530** ^Return the number of columns in the result set returned by the
5531** [prepared statement]. ^If this routine returns 0, that means the
5532** [prepared statement] returns no data (for example an [UPDATE]).
5533** ^However, just because this routine returns a positive number does not
5534** mean that one or more rows of data will be returned.  ^A SELECT statement
5535** will always have a positive sqlite3_column_count() but depending on the
5536** WHERE clause constraints and the table content, it might return no rows.
5537**
5538** See also: [sqlite3_data_count()]
5539*/
5540SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
5541
5542/*
5543** CAPI3REF: Column Names In A Result Set
5544** METHOD: sqlite3_stmt
5545**
5546** ^These routines return the name assigned to a particular column
5547** in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
5548** interface returns a pointer to a zero-terminated UTF-8 string
5549** and sqlite3_column_name16() returns a pointer to a zero-terminated
5550** UTF-16 string.  ^The first parameter is the [prepared statement]
5551** that implements the [SELECT] statement. ^The second parameter is the
5552** column number.  ^The leftmost column is number 0.
5553**
5554** ^The returned string pointer is valid until either the [prepared statement]
5555** is destroyed by [sqlite3_finalize()] or until the statement is automatically
5556** reprepared by the first call to [sqlite3_step()] for a particular run
5557** or until the next call to
5558** sqlite3_column_name() or sqlite3_column_name16() on the same column.
5559**
5560** ^If sqlite3_malloc() fails during the processing of either routine
5561** (for example during a conversion from UTF-8 to UTF-16) then a
5562** NULL pointer is returned.
5563**
5564** ^The name of a result column is the value of the "AS" clause for
5565** that column, if there is an AS clause.  If there is no AS clause
5566** then the name of the column is unspecified and may change from
5567** one release of SQLite to the next.
5568*/
5569SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
5570SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
5571
5572/*
5573** CAPI3REF: Source Of Data In A Query Result
5574** METHOD: sqlite3_stmt
5575**
5576** ^These routines provide a means to determine the database, table, and
5577** table column that is the origin of a particular result column in
5578** [SELECT] statement.
5579** ^The name of the database or table or column can be returned as
5580** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
5581** the database name, the _table_ routines return the table name, and
5582** the origin_ routines return the column name.
5583** ^The returned string is valid until the [prepared statement] is destroyed
5584** using [sqlite3_finalize()] or until the statement is automatically
5585** reprepared by the first call to [sqlite3_step()] for a particular run
5586** or until the same information is requested
5587** again in a different encoding.
5588**
5589** ^The names returned are the original un-aliased names of the
5590** database, table, and column.
5591**
5592** ^The first argument to these interfaces is a [prepared statement].
5593** ^These functions return information about the Nth result column returned by
5594** the statement, where N is the second function argument.
5595** ^The left-most column is column 0 for these routines.
5596**
5597** ^If the Nth column returned by the statement is an expression or
5598** subquery and is not a column value, then all of these functions return
5599** NULL.  ^These routines might also return NULL if a memory allocation error
5600** occurs.  ^Otherwise, they return the name of the attached database, table,
5601** or column that query result column was extracted from.
5602**
5603** ^As with all other SQLite APIs, those whose names end with "16" return
5604** UTF-16 encoded strings and the other functions return UTF-8.
5605**
5606** ^These APIs are only available if the library was compiled with the
5607** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5608**
5609** If two or more threads call one or more
5610** [sqlite3_column_database_name | column metadata interfaces]
5611** for the same [prepared statement] and result column
5612** at the same time then the results are undefined.
5613*/
5614SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
5615SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
5616SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
5617SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
5618SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
5619SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
5620
5621/*
5622** CAPI3REF: Declared Datatype Of A Query Result
5623** METHOD: sqlite3_stmt
5624**
5625** ^(The first parameter is a [prepared statement].
5626** If this statement is a [SELECT] statement and the Nth column of the
5627** returned result set of that [SELECT] is a table column (not an
5628** expression or subquery) then the declared type of the table
5629** column is returned.)^  ^If the Nth column of the result set is an
5630** expression or subquery, then a NULL pointer is returned.
5631** ^The returned string is always UTF-8 encoded.
5632**
5633** ^(For example, given the database schema:
5634**
5635** CREATE TABLE t1(c1 VARIANT);
5636**
5637** and the following statement to be compiled:
5638**
5639** SELECT c1 + 1, c1 FROM t1;
5640**
5641** this routine would return the string "VARIANT" for the second result
5642** column (i==1), and a NULL pointer for the first result column (i==0).)^
5643**
5644** ^SQLite uses dynamic run-time typing.  ^So just because a column
5645** is declared to contain a particular type does not mean that the
5646** data stored in that column is of the declared type.  SQLite is
5647** strongly typed, but the typing is dynamic not static.  ^Type
5648** is associated with individual values, not with the containers
5649** used to hold those values.
5650*/
5651SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
5652SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
5653
5654/*
5655** CAPI3REF: Evaluate An SQL Statement
5656** METHOD: sqlite3_stmt
5657**
5658** After a [prepared statement] has been prepared using any of
5659** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()],
5660** or [sqlite3_prepare16_v3()] or one of the legacy
5661** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
5662** must be called one or more times to evaluate the statement.
5663**
5664** The details of the behavior of the sqlite3_step() interface depend
5665** on whether the statement was prepared using the newer "vX" interfaces
5666** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()],
5667** [sqlite3_prepare16_v2()] or the older legacy
5668** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
5669** new "vX" interface is recommended for new applications but the legacy
5670** interface will continue to be supported.
5671**
5672** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
5673** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
5674** ^With the "v2" interface, any of the other [result codes] or
5675** [extended result codes] might be returned as well.
5676**
5677** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
5678** database locks it needs to do its job.  ^If the statement is a [COMMIT]
5679** or occurs outside of an explicit transaction, then you can retry the
5680** statement.  If the statement is not a [COMMIT] and occurs within an
5681** explicit transaction then you should rollback the transaction before
5682** continuing.
5683**
5684** ^[SQLITE_DONE] means that the statement has finished executing
5685** successfully.  sqlite3_step() should not be called again on this virtual
5686** machine without first calling [sqlite3_reset()] to reset the virtual
5687** machine back to its initial state.
5688**
5689** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
5690** is returned each time a new row of data is ready for processing by the
5691** caller. The values may be accessed using the [column access functions].
5692** sqlite3_step() is called again to retrieve the next row of data.
5693**
5694** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5695** violation) has occurred.  sqlite3_step() should not be called again on
5696** the VM. More information may be found by calling [sqlite3_errmsg()].
5697** ^With the legacy interface, a more specific error code (for example,
5698** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
5699** can be obtained by calling [sqlite3_reset()] on the
5700** [prepared statement].  ^In the "v2" interface,
5701** the more specific error code is returned directly by sqlite3_step().
5702**
5703** [SQLITE_MISUSE] means that the this routine was called inappropriately.
5704** Perhaps it was called on a [prepared statement] that has
5705** already been [sqlite3_finalize | finalized] or on one that had
5706** previously returned [SQLITE_ERROR] or [SQLITE_DONE].  Or it could
5707** be the case that the same database connection is being used by two or
5708** more threads at the same moment in time.
5709**
5710** For all versions of SQLite up to and including 3.6.23.1, a call to
5711** [sqlite3_reset()] was required after sqlite3_step() returned anything
5712** other than [SQLITE_ROW] before any subsequent invocation of
5713** sqlite3_step().  Failure to reset the prepared statement using
5714** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
5715** sqlite3_step().  But after [version 3.6.23.1] ([dateof:3.6.23.1],
5716** sqlite3_step() began
5717** calling [sqlite3_reset()] automatically in this circumstance rather
5718** than returning [SQLITE_MISUSE].  This is not considered a compatibility
5719** break because any application that ever receives an SQLITE_MISUSE error
5720** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
5721** can be used to restore the legacy behavior.
5722**
5723** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
5724** API always returns a generic error code, [SQLITE_ERROR], following any
5725** error other than [SQLITE_BUSY] and [SQLITE_MISUSE].  You must call
5726** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
5727** specific [error codes] that better describes the error.
5728** We admit that this is a goofy design.  The problem has been fixed
5729** with the "v2" interface.  If you prepare all of your SQL statements
5730** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()]
5731** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead
5732** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
5733** then the more specific [error codes] are returned directly
5734** by sqlite3_step().  The use of the "vX" interfaces is recommended.
5735*/
5736SQLITE_API int sqlite3_step(sqlite3_stmt*);
5737
5738/*
5739** CAPI3REF: Number of columns in a result set
5740** METHOD: sqlite3_stmt
5741**
5742** ^The sqlite3_data_count(P) interface returns the number of columns in the
5743** current row of the result set of [prepared statement] P.
5744** ^If prepared statement P does not have results ready to return
5745** (via calls to the [sqlite3_column_int | sqlite3_column()] family of
5746** interfaces) then sqlite3_data_count(P) returns 0.
5747** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
5748** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
5749** [sqlite3_step](P) returned [SQLITE_DONE].  ^The sqlite3_data_count(P)
5750** will return non-zero if previous call to [sqlite3_step](P) returned
5751** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
5752** where it always returns zero since each step of that multi-step
5753** pragma returns 0 columns of data.
5754**
5755** See also: [sqlite3_column_count()]
5756*/
5757SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5758
5759/*
5760** CAPI3REF: Fundamental Datatypes
5761** KEYWORDS: SQLITE_TEXT
5762**
5763** ^(Every value in SQLite has one of five fundamental datatypes:
5764**
5765** <ul>
5766** <li> 64-bit signed integer
5767** <li> 64-bit IEEE floating point number
5768** <li> string
5769** <li> BLOB
5770** <li> NULL
5771** </ul>)^
5772**
5773** These constants are codes for each of those types.
5774**
5775** Note that the SQLITE_TEXT constant was also used in SQLite version 2
5776** for a completely different meaning.  Software that links against both
5777** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
5778** SQLITE_TEXT.
5779*/
5780#define SQLITE_INTEGER  1
5781#define SQLITE_FLOAT    2
5782#define SQLITE_BLOB     4
5783#define SQLITE_NULL     5
5784#ifdef SQLITE_TEXT
5785# undef SQLITE_TEXT
5786#else
5787# define SQLITE_TEXT     3
5788#endif
5789#define SQLITE3_TEXT     3
5790
5791/*
5792** CAPI3REF: Result Values From A Query
5793** KEYWORDS: {column access functions}
5794** METHOD: sqlite3_stmt
5795**
5796** <b>Summary:</b>
5797** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5798** <tr><td><b>sqlite3_column_blob</b><td>&rarr;<td>BLOB result
5799** <tr><td><b>sqlite3_column_double</b><td>&rarr;<td>REAL result
5800** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
5801** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
5802** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
5803** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
5804** <tr><td><b>sqlite3_column_value</b><td>&rarr;<td>The result as an
5805** [sqlite3_value|unprotected sqlite3_value] object.
5806** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
5807** <tr><td><b>sqlite3_column_bytes</b><td>&rarr;<td>Size of a BLOB
5808** or a UTF-8 TEXT result in bytes
5809** <tr><td><b>sqlite3_column_bytes16&nbsp;&nbsp;</b>
5810** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5811** TEXT in bytes
5812** <tr><td><b>sqlite3_column_type</b><td>&rarr;<td>Default
5813** datatype of the result
5814** </table></blockquote>
5815**
5816** <b>Details:</b>
5817**
5818** ^These routines return information about a single column of the current
5819** result row of a query.  ^In every case the first argument is a pointer
5820** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
5821** that was returned from [sqlite3_prepare_v2()] or one of its variants)
5822** and the second argument is the index of the column for which information
5823** should be returned. ^The leftmost column of the result set has the index 0.
5824** ^The number of columns in the result can be determined using
5825** [sqlite3_column_count()].
5826**
5827** If the SQL statement does not currently point to a valid row, or if the
5828** column index is out of range, the result is undefined.
5829** These routines may only be called when the most recent call to
5830** [sqlite3_step()] has returned [SQLITE_ROW] and neither
5831** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
5832** If any of these routines are called after [sqlite3_reset()] or
5833** [sqlite3_finalize()] or after [sqlite3_step()] has returned
5834** something other than [SQLITE_ROW], the results are undefined.
5835** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
5836** are called from a different thread while any of these routines
5837** are pending, then the results are undefined.
5838**
5839** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16)
5840** each return the value of a result column in a specific data format.  If
5841** the result column is not initially in the requested format (for example,
5842** if the query returns an integer but the sqlite3_column_text() interface
5843** is used to extract the value) then an automatic type conversion is performed.
5844**
5845** ^The sqlite3_column_type() routine returns the
5846** [SQLITE_INTEGER | datatype code] for the initial data type
5847** of the result column.  ^The returned value is one of [SQLITE_INTEGER],
5848** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].
5849** The return value of sqlite3_column_type() can be used to decide which
5850** of the first six interface should be used to extract the column value.
5851** The value returned by sqlite3_column_type() is only meaningful if no
5852** automatic type conversions have occurred for the value in question.
5853** After a type conversion, the result of calling sqlite3_column_type()
5854** is undefined, though harmless.  Future
5855** versions of SQLite may change the behavior of sqlite3_column_type()
5856** following a type conversion.
5857**
5858** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes()
5859** or sqlite3_column_bytes16() interfaces can be used to determine the size
5860** of that BLOB or string.
5861**
5862** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5863** routine returns the number of bytes in that BLOB or string.
5864** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5865** the string to UTF-8 and then returns the number of bytes.
5866** ^If the result is a numeric value then sqlite3_column_bytes() uses
5867** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5868** the number of bytes in that string.
5869** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
5870**
5871** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5872** routine returns the number of bytes in that BLOB or string.
5873** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5874** the string to UTF-16 and then returns the number of bytes.
5875** ^If the result is a numeric value then sqlite3_column_bytes16() uses
5876** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5877** the number of bytes in that string.
5878** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
5879**
5880** ^The values returned by [sqlite3_column_bytes()] and
5881** [sqlite3_column_bytes16()] do not include the zero terminators at the end
5882** of the string.  ^For clarity: the values returned by
5883** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
5884** bytes in the string, not the number of characters.
5885**
5886** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
5887** even empty strings, are always zero-terminated.  ^The return
5888** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5889**
5890** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
5891** [unprotected sqlite3_value] object.  In a multithreaded environment,
5892** an unprotected sqlite3_value object may only be used safely with
5893** [sqlite3_bind_value()] and [sqlite3_result_value()].
5894** If the [unprotected sqlite3_value] object returned by
5895** [sqlite3_column_value()] is used in any other way, including calls
5896** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
5897** or [sqlite3_value_bytes()], the behavior is not threadsafe.
5898** Hence, the sqlite3_column_value() interface
5899** is normally only useful within the implementation of
5900** [application-defined SQL functions] or [virtual tables], not within
5901** top-level application code.
5902**
5903** The these routines may attempt to convert the datatype of the result.
5904** ^For example, if the internal representation is FLOAT and a text result
5905** is requested, [sqlite3_snprintf()] is used internally to perform the
5906** conversion automatically.  ^(The following table details the conversions
5907** that are applied:
5908**
5909** <blockquote>
5910** <table border="1">
5911** <tr><th> Internal<br>Type <th> Requested<br>Type <th>  Conversion
5912**
5913** <tr><td>  NULL    <td> INTEGER   <td> Result is 0
5914** <tr><td>  NULL    <td>  FLOAT    <td> Result is 0.0
5915** <tr><td>  NULL    <td>   TEXT    <td> Result is a NULL pointer
5916** <tr><td>  NULL    <td>   BLOB    <td> Result is a NULL pointer
5917** <tr><td> INTEGER  <td>  FLOAT    <td> Convert from integer to float
5918** <tr><td> INTEGER  <td>   TEXT    <td> ASCII rendering of the integer
5919** <tr><td> INTEGER  <td>   BLOB    <td> Same as INTEGER->TEXT
5920** <tr><td>  FLOAT   <td> INTEGER   <td> [CAST] to INTEGER
5921** <tr><td>  FLOAT   <td>   TEXT    <td> ASCII rendering of the float
5922** <tr><td>  FLOAT   <td>   BLOB    <td> [CAST] to BLOB
5923** <tr><td>  TEXT    <td> INTEGER   <td> [CAST] to INTEGER
5924** <tr><td>  TEXT    <td>  FLOAT    <td> [CAST] to REAL
5925** <tr><td>  TEXT    <td>   BLOB    <td> No change
5926** <tr><td>  BLOB    <td> INTEGER   <td> [CAST] to INTEGER
5927** <tr><td>  BLOB    <td>  FLOAT    <td> [CAST] to REAL
5928** <tr><td>  BLOB    <td>   TEXT    <td> Add a zero terminator if needed
5929** </table>
5930** </blockquote>)^
5931**
5932** Note that when type conversions occur, pointers returned by prior
5933** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
5934** sqlite3_column_text16() may be invalidated.
5935** Type conversions and pointer invalidations might occur
5936** in the following cases:
5937**
5938** <ul>
5939** <li> The initial content is a BLOB and sqlite3_column_text() or
5940**      sqlite3_column_text16() is called.  A zero-terminator might
5941**      need to be added to the string.</li>
5942** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5943**      sqlite3_column_text16() is called.  The content must be converted
5944**      to UTF-16.</li>
5945** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5946**      sqlite3_column_text() is called.  The content must be converted
5947**      to UTF-8.</li>
5948** </ul>
5949**
5950** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5951** not invalidate a prior pointer, though of course the content of the buffer
5952** that the prior pointer references will have been modified.  Other kinds
5953** of conversion are done in place when it is possible, but sometimes they
5954** are not possible and in those cases prior pointers are invalidated.
5955**
5956** The safest policy is to invoke these routines
5957** in one of the following ways:
5958**
5959** <ul>
5960**  <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
5961**  <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
5962**  <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
5963** </ul>
5964**
5965** In other words, you should call sqlite3_column_text(),
5966** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
5967** into the desired format, then invoke sqlite3_column_bytes() or
5968** sqlite3_column_bytes16() to find the size of the result.  Do not mix calls
5969** to sqlite3_column_text() or sqlite3_column_blob() with calls to
5970** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
5971** with calls to sqlite3_column_bytes().
5972**
5973** ^The pointers returned are valid until a type conversion occurs as
5974** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
5975** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
5976** and BLOBs is freed automatically.  Do not pass the pointers returned
5977** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
5978** [sqlite3_free()].
5979**
5980** As long as the input parameters are correct, these routines will only
5981** fail if an out-of-memory error occurs during a format conversion.
5982** Only the following subset of interfaces are subject to out-of-memory
5983** errors:
5984**
5985** <ul>
5986** <li> sqlite3_column_blob()
5987** <li> sqlite3_column_text()
5988** <li> sqlite3_column_text16()
5989** <li> sqlite3_column_bytes()
5990** <li> sqlite3_column_bytes16()
5991** </ul>
5992**
5993** If an out-of-memory error occurs, then the return value from these
5994** routines is the same as if the column had contained an SQL NULL value.
5995** Valid SQL NULL returns can be distinguished from out-of-memory errors
5996** by invoking the [sqlite3_errcode()] immediately after the suspect
5997** return value is obtained and before any
5998** other SQLite interface is called on the same [database connection].
5999*/
6000SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
6001SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
6002SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
6003SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
6004SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
6005SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
6006SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
6007SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
6008SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
6009SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
6010
6011/*
6012** CAPI3REF: Destroy A Prepared Statement Object
6013** DESTRUCTOR: sqlite3_stmt
6014**
6015** ^The sqlite3_finalize() function is called to delete a [prepared statement].
6016** ^If the most recent evaluation of the statement encountered no errors
6017** or if the statement is never been evaluated, then sqlite3_finalize() returns
6018** SQLITE_OK.  ^If the most recent evaluation of statement S failed, then
6019** sqlite3_finalize(S) returns the appropriate [error code] or
6020** [extended error code].
6021**
6022** ^The sqlite3_finalize(S) routine can be called at any point during
6023** the life cycle of [prepared statement] S:
6024** before statement S is ever evaluated, after
6025** one or more calls to [sqlite3_reset()], or after any call
6026** to [sqlite3_step()] regardless of whether or not the statement has
6027** completed execution.
6028**
6029** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
6030**
6031** The application must finalize every [prepared statement] in order to avoid
6032** resource leaks.  It is a grievous error for the application to try to use
6033** a prepared statement after it has been finalized.  Any use of a prepared
6034** statement after it has been finalized can result in undefined and
6035** undesirable behavior such as segfaults and heap corruption.
6036*/
6037SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
6038
6039/*
6040** CAPI3REF: Reset A Prepared Statement Object
6041** METHOD: sqlite3_stmt
6042**
6043** The sqlite3_reset() function is called to reset a [prepared statement]
6044** object back to its initial state, ready to be re-executed.
6045** ^Any SQL statement variables that had values bound to them using
6046** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
6047** Use [sqlite3_clear_bindings()] to reset the bindings.
6048**
6049** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
6050** back to the beginning of its program.
6051**
6052** ^If the most recent call to [sqlite3_step(S)] for the
6053** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
6054** or if [sqlite3_step(S)] has never before been called on S,
6055** then [sqlite3_reset(S)] returns [SQLITE_OK].
6056**
6057** ^If the most recent call to [sqlite3_step(S)] for the
6058** [prepared statement] S indicated an error, then
6059** [sqlite3_reset(S)] returns an appropriate [error code].
6060**
6061** ^The [sqlite3_reset(S)] interface does not change the values
6062** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
6063*/
6064SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
6065
6066/*
6067** CAPI3REF: Create Or Redefine SQL Functions
6068** KEYWORDS: {function creation routines}
6069** METHOD: sqlite3
6070**
6071** ^These functions (collectively known as "function creation routines")
6072** are used to add SQL functions or aggregates or to redefine the behavior
6073** of existing SQL functions or aggregates. The only differences between
6074** the three "sqlite3_create_function*" routines are the text encoding
6075** expected for the second parameter (the name of the function being
6076** created) and the presence or absence of a destructor callback for
6077** the application data pointer. Function sqlite3_create_window_function()
6078** is similar, but allows the user to supply the extra callback functions
6079** needed by [aggregate window functions].
6080**
6081** ^The first parameter is the [database connection] to which the SQL
6082** function is to be added.  ^If an application uses more than one database
6083** connection then application-defined SQL functions must be added
6084** to each database connection separately.
6085**
6086** ^The second parameter is the name of the SQL function to be created or
6087** redefined.  ^The length of the name is limited to 255 bytes in a UTF-8
6088** representation, exclusive of the zero-terminator.  ^Note that the name
6089** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
6090** ^Any attempt to create a function with a longer name
6091** will result in [SQLITE_MISUSE] being returned.
6092**
6093** ^The third parameter (nArg)
6094** is the number of arguments that the SQL function or
6095** aggregate takes. ^If this parameter is -1, then the SQL function or
6096** aggregate may take any number of arguments between 0 and the limit
6097** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]).  If the third
6098** parameter is less than -1 or greater than 127 then the behavior is
6099** undefined.
6100**
6101** ^The fourth parameter, eTextRep, specifies what
6102** [SQLITE_UTF8 | text encoding] this SQL function prefers for
6103** its parameters.  The application should set this parameter to
6104** [SQLITE_UTF16LE] if the function implementation invokes
6105** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
6106** implementation invokes [sqlite3_value_text16be()] on an input, or
6107** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
6108** otherwise.  ^The same SQL function may be registered multiple times using
6109** different preferred text encodings, with different implementations for
6110** each encoding.
6111** ^When multiple implementations of the same function are available, SQLite
6112** will pick the one that involves the least amount of data conversion.
6113**
6114** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
6115** to signal that the function will always return the same result given
6116** the same inputs within a single SQL statement.  Most SQL functions are
6117** deterministic.  The built-in [random()] SQL function is an example of a
6118** function that is not deterministic.  The SQLite query planner is able to
6119** perform additional optimizations on deterministic functions, so use
6120** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
6121**
6122** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY]
6123** flag, which if present prevents the function from being invoked from
6124** within VIEWs, TRIGGERs, CHECK constraints, generated column expressions,
6125** index expressions, or the WHERE clause of partial indexes.
6126**
6127** <span style="background-color:#ffff90;">
6128** For best security, the [SQLITE_DIRECTONLY] flag is recommended for
6129** all application-defined SQL functions that do not need to be
6130** used inside of triggers, view, CHECK constraints, or other elements of
6131** the database schema.  This flags is especially recommended for SQL
6132** functions that have side effects or reveal internal application state.
6133** Without this flag, an attacker might be able to modify the schema of
6134** a database file to include invocations of the function with parameters
6135** chosen by the attacker, which the application will then execute when
6136** the database file is opened and read.
6137** </span>
6138**
6139** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
6140** function can gain access to this pointer using [sqlite3_user_data()].)^
6141**
6142** ^The sixth, seventh and eighth parameters passed to the three
6143** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are
6144** pointers to C-language functions that implement the SQL function or
6145** aggregate. ^A scalar SQL function requires an implementation of the xFunc
6146** callback only; NULL pointers must be passed as the xStep and xFinal
6147** parameters. ^An aggregate SQL function requires an implementation of xStep
6148** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
6149** SQL function or aggregate, pass NULL pointers for all three function
6150** callbacks.
6151**
6152** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue
6153** and xInverse) passed to sqlite3_create_window_function are pointers to
6154** C-language callbacks that implement the new function. xStep and xFinal
6155** must both be non-NULL. xValue and xInverse may either both be NULL, in
6156** which case a regular aggregate function is created, or must both be
6157** non-NULL, in which case the new function may be used as either an aggregate
6158** or aggregate window function. More details regarding the implementation
6159** of aggregate window functions are
6160** [user-defined window functions|available here].
6161**
6162** ^(If the final parameter to sqlite3_create_function_v2() or
6163** sqlite3_create_window_function() is not NULL, then it is destructor for
6164** the application data pointer. The destructor is invoked when the function
6165** is deleted, either by being overloaded or when the database connection
6166** closes.)^ ^The destructor is also invoked if the call to
6167** sqlite3_create_function_v2() fails.  ^When the destructor callback is
6168** invoked, it is passed a single argument which is a copy of the application
6169** data pointer which was the fifth parameter to sqlite3_create_function_v2().
6170**
6171** ^It is permitted to register multiple implementations of the same
6172** functions with the same name but with either differing numbers of
6173** arguments or differing preferred text encodings.  ^SQLite will use
6174** the implementation that most closely matches the way in which the
6175** SQL function is used.  ^A function implementation with a non-negative
6176** nArg parameter is a better match than a function implementation with
6177** a negative nArg.  ^A function where the preferred text encoding
6178** matches the database encoding is a better
6179** match than a function where the encoding is different.
6180** ^A function where the encoding difference is between UTF16le and UTF16be
6181** is a closer match than a function where the encoding difference is
6182** between UTF8 and UTF16.
6183**
6184** ^Built-in functions may be overloaded by new application-defined functions.
6185**
6186** ^An application-defined function is permitted to call other
6187** SQLite interfaces.  However, such calls must not
6188** close the database connection nor finalize or reset the prepared
6189** statement in which the function is running.
6190*/
6191SQLITE_API int sqlite3_create_function(
6192  sqlite3 *db,
6193  const char *zFunctionName,
6194  int nArg,
6195  int eTextRep,
6196  void *pApp,
6197  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
6198  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
6199  void (*xFinal)(sqlite3_context*)
6200);
6201SQLITE_API int sqlite3_create_function16(
6202  sqlite3 *db,
6203  const void *zFunctionName,
6204  int nArg,
6205  int eTextRep,
6206  void *pApp,
6207  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
6208  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
6209  void (*xFinal)(sqlite3_context*)
6210);
6211SQLITE_API int sqlite3_create_function_v2(
6212  sqlite3 *db,
6213  const char *zFunctionName,
6214  int nArg,
6215  int eTextRep,
6216  void *pApp,
6217  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
6218  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
6219  void (*xFinal)(sqlite3_context*),
6220  void(*xDestroy)(void*)
6221);
6222SQLITE_API int sqlite3_create_window_function(
6223  sqlite3 *db,
6224  const char *zFunctionName,
6225  int nArg,
6226  int eTextRep,
6227  void *pApp,
6228  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
6229  void (*xFinal)(sqlite3_context*),
6230  void (*xValue)(sqlite3_context*),
6231  void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
6232  void(*xDestroy)(void*)
6233);
6234
6235/*
6236** CAPI3REF: Text Encodings
6237**
6238** These constant define integer codes that represent the various
6239** text encodings supported by SQLite.
6240*/
6241#define SQLITE_UTF8           1    /* IMP: R-37514-35566 */
6242#define SQLITE_UTF16LE        2    /* IMP: R-03371-37637 */
6243#define SQLITE_UTF16BE        3    /* IMP: R-51971-34154 */
6244#define SQLITE_UTF16          4    /* Use native byte order */
6245#define SQLITE_ANY            5    /* Deprecated */
6246#define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
6247
6248/*
6249** CAPI3REF: Function Flags
6250**
6251** These constants may be ORed together with the
6252** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
6253** to [sqlite3_create_function()], [sqlite3_create_function16()], or
6254** [sqlite3_create_function_v2()].
6255**
6256** <dl>
6257** [[SQLITE_DETERMINISTIC]] <dt>SQLITE_DETERMINISTIC</dt><dd>
6258** The SQLITE_DETERMINISTIC flag means that the new function always gives
6259** the same output when the input parameters are the same.
6260** The [abs|abs() function] is deterministic, for example, but
6261** [randomblob|randomblob()] is not.  Functions must
6262** be deterministic in order to be used in certain contexts such as
6263** with the WHERE clause of [partial indexes] or in [generated columns].
6264** SQLite might also optimize deterministic functions by factoring them
6265** out of inner loops.
6266** </dd>
6267**
6268** [[SQLITE_DIRECTONLY]] <dt>SQLITE_DIRECTONLY</dt><dd>
6269** The SQLITE_DIRECTONLY flag means that the function may only be invoked
6270** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
6271** schema structures such as [CHECK constraints], [DEFAULT clauses],
6272** [expression indexes], [partial indexes], or [generated columns].
6273** The SQLITE_DIRECTONLY flags is a security feature which is recommended
6274** for all [application-defined SQL functions], and especially for functions
6275** that have side-effects or that could potentially leak sensitive
6276** information.
6277** </dd>
6278**
6279** [[SQLITE_INNOCUOUS]] <dt>SQLITE_INNOCUOUS</dt><dd>
6280** The SQLITE_INNOCUOUS flag means that the function is unlikely
6281** to cause problems even if misused.  An innocuous function should have
6282** no side effects and should not depend on any values other than its
6283** input parameters. The [abs|abs() function] is an example of an
6284** innocuous function.
6285** The [load_extension() SQL function] is not innocuous because of its
6286** side effects.
6287** <p> SQLITE_INNOCUOUS is similar to SQLITE_DETERMINISTIC, but is not
6288** exactly the same.  The [random|random() function] is an example of a
6289** function that is innocuous but not deterministic.
6290** <p>Some heightened security settings
6291** ([SQLITE_DBCONFIG_TRUSTED_SCHEMA] and [PRAGMA trusted_schema=OFF])
6292** disable the use of SQL functions inside views and triggers and in
6293** schema structures such as [CHECK constraints], [DEFAULT clauses],
6294** [expression indexes], [partial indexes], and [generated columns] unless
6295** the function is tagged with SQLITE_INNOCUOUS.  Most built-in functions
6296** are innocuous.  Developers are advised to avoid using the
6297** SQLITE_INNOCUOUS flag for application-defined functions unless the
6298** function has been carefully audited and found to be free of potentially
6299** security-adverse side-effects and information-leaks.
6300** </dd>
6301**
6302** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
6303** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
6304** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
6305** Specifying this flag makes no difference for scalar or aggregate user
6306** functions. However, if it is not specified for a user-defined window
6307** function, then any sub-types belonging to arguments passed to the window
6308** function may be discarded before the window function is called (i.e.
6309** sqlite3_value_subtype() will always return 0).
6310** </dd>
6311** </dl>
6312*/
6313#define SQLITE_DETERMINISTIC    0x000000800
6314#define SQLITE_DIRECTONLY       0x000080000
6315#define SQLITE_SUBTYPE          0x000100000
6316#define SQLITE_INNOCUOUS        0x000200000
6317
6318/*
6319** CAPI3REF: Deprecated Functions
6320** DEPRECATED
6321**
6322** These functions are [deprecated].  In order to maintain
6323** backwards compatibility with older code, these functions continue
6324** to be supported.  However, new applications should avoid
6325** the use of these functions.  To encourage programmers to avoid
6326** these functions, we will not explain what they do.
6327*/
6328#ifndef SQLITE_OMIT_DEPRECATED
6329SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
6330SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
6331SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
6332SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
6333SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
6334SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
6335                      void*,sqlite3_int64);
6336#endif
6337
6338/*
6339** CAPI3REF: Obtaining SQL Values
6340** METHOD: sqlite3_value
6341**
6342** <b>Summary:</b>
6343** <blockquote><table border=0 cellpadding=0 cellspacing=0>
6344** <tr><td><b>sqlite3_value_blob</b><td>&rarr;<td>BLOB value
6345** <tr><td><b>sqlite3_value_double</b><td>&rarr;<td>REAL value
6346** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
6347** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
6348** <tr><td><b>sqlite3_value_pointer</b><td>&rarr;<td>Pointer value
6349** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
6350** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
6351** the native byteorder
6352** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
6353** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
6354** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
6355** <tr><td><b>sqlite3_value_bytes</b><td>&rarr;<td>Size of a BLOB
6356** or a UTF-8 TEXT in bytes
6357** <tr><td><b>sqlite3_value_bytes16&nbsp;&nbsp;</b>
6358** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
6359** TEXT in bytes
6360** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
6361** datatype of the value
6362** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
6363** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
6364** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
6365** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
6366** against a virtual table.
6367** <tr><td><b>sqlite3_value_frombind&nbsp;&nbsp;</b>
6368** <td>&rarr;&nbsp;&nbsp;<td>True if value originated from a [bound parameter]
6369** </table></blockquote>
6370**
6371** <b>Details:</b>
6372**
6373** These routines extract type, size, and content information from
6374** [protected sqlite3_value] objects.  Protected sqlite3_value objects
6375** are used to pass parameter information into the functions that
6376** implement [application-defined SQL functions] and [virtual tables].
6377**
6378** These routines work only with [protected sqlite3_value] objects.
6379** Any attempt to use these routines on an [unprotected sqlite3_value]
6380** is not threadsafe.
6381**
6382** ^These routines work just like the corresponding [column access functions]
6383** except that these routines take a single [protected sqlite3_value] object
6384** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
6385**
6386** ^The sqlite3_value_text16() interface extracts a UTF-16 string
6387** in the native byte-order of the host machine.  ^The
6388** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
6389** extract UTF-16 strings as big-endian and little-endian respectively.
6390**
6391** ^If [sqlite3_value] object V was initialized
6392** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)]
6393** and if X and Y are strings that compare equal according to strcmp(X,Y),
6394** then sqlite3_value_pointer(V,Y) will return the pointer P.  ^Otherwise,
6395** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer()
6396** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
6397**
6398** ^(The sqlite3_value_type(V) interface returns the
6399** [SQLITE_INTEGER | datatype code] for the initial datatype of the
6400** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER],
6401** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^
6402** Other interfaces might change the datatype for an sqlite3_value object.
6403** For example, if the datatype is initially SQLITE_INTEGER and
6404** sqlite3_value_text(V) is called to extract a text value for that
6405** integer, then subsequent calls to sqlite3_value_type(V) might return
6406** SQLITE_TEXT.  Whether or not a persistent internal datatype conversion
6407** occurs is undefined and may change from one release of SQLite to the next.
6408**
6409** ^(The sqlite3_value_numeric_type() interface attempts to apply
6410** numeric affinity to the value.  This means that an attempt is
6411** made to convert the value to an integer or floating point.  If
6412** such a conversion is possible without loss of information (in other
6413** words, if the value is a string that looks like a number)
6414** then the conversion is performed.  Otherwise no conversion occurs.
6415** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
6416**
6417** ^Within the [xUpdate] method of a [virtual table], the
6418** sqlite3_value_nochange(X) interface returns true if and only if
6419** the column corresponding to X is unchanged by the UPDATE operation
6420** that the xUpdate method call was invoked to implement and if
6421** and the prior [xColumn] method call that was invoked to extracted
6422** the value for that column returned without setting a result (probably
6423** because it queried [sqlite3_vtab_nochange()] and found that the column
6424** was unchanging).  ^Within an [xUpdate] method, any value for which
6425** sqlite3_value_nochange(X) is true will in all other respects appear
6426** to be a NULL value.  If sqlite3_value_nochange(X) is invoked anywhere other
6427** than within an [xUpdate] method call for an UPDATE statement, then
6428** the return value is arbitrary and meaningless.
6429**
6430** ^The sqlite3_value_frombind(X) interface returns non-zero if the
6431** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()]
6432** interfaces.  ^If X comes from an SQL literal value, or a table column,
6433** or an expression, then sqlite3_value_frombind(X) returns zero.
6434**
6435** Please pay particular attention to the fact that the pointer returned
6436** from [sqlite3_value_blob()], [sqlite3_value_text()], or
6437** [sqlite3_value_text16()] can be invalidated by a subsequent call to
6438** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
6439** or [sqlite3_value_text16()].
6440**
6441** These routines must be called from the same thread as
6442** the SQL function that supplied the [sqlite3_value*] parameters.
6443**
6444** As long as the input parameter is correct, these routines can only
6445** fail if an out-of-memory error occurs during a format conversion.
6446** Only the following subset of interfaces are subject to out-of-memory
6447** errors:
6448**
6449** <ul>
6450** <li> sqlite3_value_blob()
6451** <li> sqlite3_value_text()
6452** <li> sqlite3_value_text16()
6453** <li> sqlite3_value_text16le()
6454** <li> sqlite3_value_text16be()
6455** <li> sqlite3_value_bytes()
6456** <li> sqlite3_value_bytes16()
6457** </ul>
6458**
6459** If an out-of-memory error occurs, then the return value from these
6460** routines is the same as if the column had contained an SQL NULL value.
6461** Valid SQL NULL returns can be distinguished from out-of-memory errors
6462** by invoking the [sqlite3_errcode()] immediately after the suspect
6463** return value is obtained and before any
6464** other SQLite interface is called on the same [database connection].
6465*/
6466SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
6467SQLITE_API double sqlite3_value_double(sqlite3_value*);
6468SQLITE_API int sqlite3_value_int(sqlite3_value*);
6469SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
6470SQLITE_API void *sqlite3_value_pointer(sqlite3_value*, const char*);
6471SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
6472SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
6473SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
6474SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
6475SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
6476SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
6477SQLITE_API int sqlite3_value_type(sqlite3_value*);
6478SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
6479SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
6480SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
6481
6482/*
6483** CAPI3REF: Finding The Subtype Of SQL Values
6484** METHOD: sqlite3_value
6485**
6486** The sqlite3_value_subtype(V) function returns the subtype for
6487** an [application-defined SQL function] argument V.  The subtype
6488** information can be used to pass a limited amount of context from
6489** one SQL function to another.  Use the [sqlite3_result_subtype()]
6490** routine to set the subtype for the return value of an SQL function.
6491*/
6492SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
6493
6494/*
6495** CAPI3REF: Copy And Free SQL Values
6496** METHOD: sqlite3_value
6497**
6498** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
6499** object D and returns a pointer to that copy.  ^The [sqlite3_value] returned
6500** is a [protected sqlite3_value] object even if the input is not.
6501** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
6502** memory allocation fails.
6503**
6504** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
6505** previously obtained from [sqlite3_value_dup()].  ^If V is a NULL pointer
6506** then sqlite3_value_free(V) is a harmless no-op.
6507*/
6508SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
6509SQLITE_API void sqlite3_value_free(sqlite3_value*);
6510
6511/*
6512** CAPI3REF: Obtain Aggregate Function Context
6513** METHOD: sqlite3_context
6514**
6515** Implementations of aggregate SQL functions use this
6516** routine to allocate memory for storing their state.
6517**
6518** ^The first time the sqlite3_aggregate_context(C,N) routine is called
6519** for a particular aggregate function, SQLite allocates
6520** N bytes of memory, zeroes out that memory, and returns a pointer
6521** to the new memory. ^On second and subsequent calls to
6522** sqlite3_aggregate_context() for the same aggregate function instance,
6523** the same buffer is returned.  Sqlite3_aggregate_context() is normally
6524** called once for each invocation of the xStep callback and then one
6525** last time when the xFinal callback is invoked.  ^(When no rows match
6526** an aggregate query, the xStep() callback of the aggregate function
6527** implementation is never called and xFinal() is called exactly once.
6528** In those cases, sqlite3_aggregate_context() might be called for the
6529** first time from within xFinal().)^
6530**
6531** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
6532** when first called if N is less than or equal to zero or if a memory
6533** allocate error occurs.
6534**
6535** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
6536** determined by the N parameter on first successful call.  Changing the
6537** value of N in any subsequent call to sqlite3_aggregate_context() within
6538** the same aggregate function instance will not resize the memory
6539** allocation.)^  Within the xFinal callback, it is customary to set
6540** N=0 in calls to sqlite3_aggregate_context(C,N) so that no
6541** pointless memory allocations occur.
6542**
6543** ^SQLite automatically frees the memory allocated by
6544** sqlite3_aggregate_context() when the aggregate query concludes.
6545**
6546** The first parameter must be a copy of the
6547** [sqlite3_context | SQL function context] that is the first parameter
6548** to the xStep or xFinal callback routine that implements the aggregate
6549** function.
6550**
6551** This routine must be called from the same thread in which
6552** the aggregate SQL function is running.
6553*/
6554SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
6555
6556/*
6557** CAPI3REF: User Data For Functions
6558** METHOD: sqlite3_context
6559**
6560** ^The sqlite3_user_data() interface returns a copy of
6561** the pointer that was the pUserData parameter (the 5th parameter)
6562** of the [sqlite3_create_function()]
6563** and [sqlite3_create_function16()] routines that originally
6564** registered the application defined function.
6565**
6566** This routine must be called from the same thread in which
6567** the application-defined function is running.
6568*/
6569SQLITE_API void *sqlite3_user_data(sqlite3_context*);
6570
6571/*
6572** CAPI3REF: Database Connection For Functions
6573** METHOD: sqlite3_context
6574**
6575** ^The sqlite3_context_db_handle() interface returns a copy of
6576** the pointer to the [database connection] (the 1st parameter)
6577** of the [sqlite3_create_function()]
6578** and [sqlite3_create_function16()] routines that originally
6579** registered the application defined function.
6580*/
6581SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
6582
6583/*
6584** CAPI3REF: Function Auxiliary Data
6585** METHOD: sqlite3_context
6586**
6587** These functions may be used by (non-aggregate) SQL functions to
6588** associate metadata with argument values. If the same value is passed to
6589** multiple invocations of the same SQL function during query execution, under
6590** some circumstances the associated metadata may be preserved.  An example
6591** of where this might be useful is in a regular-expression matching
6592** function. The compiled version of the regular expression can be stored as
6593** metadata associated with the pattern string.
6594** Then as long as the pattern string remains the same,
6595** the compiled regular expression can be reused on multiple
6596** invocations of the same function.
6597**
6598** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the metadata
6599** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument
6600** value to the application-defined function.  ^N is zero for the left-most
6601** function argument.  ^If there is no metadata
6602** associated with the function argument, the sqlite3_get_auxdata(C,N) interface
6603** returns a NULL pointer.
6604**
6605** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
6606** argument of the application-defined function.  ^Subsequent
6607** calls to sqlite3_get_auxdata(C,N) return P from the most recent
6608** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
6609** NULL if the metadata has been discarded.
6610** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
6611** SQLite will invoke the destructor function X with parameter P exactly
6612** once, when the metadata is discarded.
6613** SQLite is free to discard the metadata at any time, including: <ul>
6614** <li> ^(when the corresponding function parameter changes)^, or
6615** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
6616**      SQL statement)^, or
6617** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
6618**       parameter)^, or
6619** <li> ^(during the original sqlite3_set_auxdata() call when a memory
6620**      allocation error occurs.)^ </ul>
6621**
6622** Note the last bullet in particular.  The destructor X in
6623** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
6624** sqlite3_set_auxdata() interface even returns.  Hence sqlite3_set_auxdata()
6625** should be called near the end of the function implementation and the
6626** function implementation should not make any use of P after
6627** sqlite3_set_auxdata() has been called.
6628**
6629** ^(In practice, metadata is preserved between function calls for
6630** function parameters that are compile-time constants, including literal
6631** values and [parameters] and expressions composed from the same.)^
6632**
6633** The value of the N parameter to these interfaces should be non-negative.
6634** Future enhancements may make use of negative N values to define new
6635** kinds of function caching behavior.
6636**
6637** These routines must be called from the same thread in which
6638** the SQL function is running.
6639*/
6640SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
6641SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
6642
6643
6644/*
6645** CAPI3REF: Constants Defining Special Destructor Behavior
6646**
6647** These are special values for the destructor that is passed in as the
6648** final argument to routines like [sqlite3_result_blob()].  ^If the destructor
6649** argument is SQLITE_STATIC, it means that the content pointer is constant
6650** and will never change.  It does not need to be destroyed.  ^The
6651** SQLITE_TRANSIENT value means that the content will likely change in
6652** the near future and that SQLite should make its own private copy of
6653** the content before returning.
6654**
6655** The typedef is necessary to work around problems in certain
6656** C++ compilers.
6657*/
6658typedef void (*sqlite3_destructor_type)(void*);
6659#define SQLITE_STATIC      ((sqlite3_destructor_type)0)
6660#define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
6661
6662/*
6663** CAPI3REF: Setting The Result Of An SQL Function
6664** METHOD: sqlite3_context
6665**
6666** These routines are used by the xFunc or xFinal callbacks that
6667** implement SQL functions and aggregates.  See
6668** [sqlite3_create_function()] and [sqlite3_create_function16()]
6669** for additional information.
6670**
6671** These functions work very much like the [parameter binding] family of
6672** functions used to bind values to host parameters in prepared statements.
6673** Refer to the [SQL parameter] documentation for additional information.
6674**
6675** ^The sqlite3_result_blob() interface sets the result from
6676** an application-defined function to be the BLOB whose content is pointed
6677** to by the second parameter and which is N bytes long where N is the
6678** third parameter.
6679**
6680** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N)
6681** interfaces set the result of the application-defined function to be
6682** a BLOB containing all zero bytes and N bytes in size.
6683**
6684** ^The sqlite3_result_double() interface sets the result from
6685** an application-defined function to be a floating point value specified
6686** by its 2nd argument.
6687**
6688** ^The sqlite3_result_error() and sqlite3_result_error16() functions
6689** cause the implemented SQL function to throw an exception.
6690** ^SQLite uses the string pointed to by the
6691** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
6692** as the text of an error message.  ^SQLite interprets the error
6693** message string from sqlite3_result_error() as UTF-8. ^SQLite
6694** interprets the string from sqlite3_result_error16() as UTF-16 using
6695** the same [byte-order determination rules] as [sqlite3_bind_text16()].
6696** ^If the third parameter to sqlite3_result_error()
6697** or sqlite3_result_error16() is negative then SQLite takes as the error
6698** message all text up through the first zero character.
6699** ^If the third parameter to sqlite3_result_error() or
6700** sqlite3_result_error16() is non-negative then SQLite takes that many
6701** bytes (not characters) from the 2nd parameter as the error message.
6702** ^The sqlite3_result_error() and sqlite3_result_error16()
6703** routines make a private copy of the error message text before
6704** they return.  Hence, the calling function can deallocate or
6705** modify the text after they return without harm.
6706** ^The sqlite3_result_error_code() function changes the error code
6707** returned by SQLite as a result of an error in a function.  ^By default,
6708** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
6709** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
6710**
6711** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
6712** error indicating that a string or BLOB is too long to represent.
6713**
6714** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
6715** error indicating that a memory allocation failed.
6716**
6717** ^The sqlite3_result_int() interface sets the return value
6718** of the application-defined function to be the 32-bit signed integer
6719** value given in the 2nd argument.
6720** ^The sqlite3_result_int64() interface sets the return value
6721** of the application-defined function to be the 64-bit signed integer
6722** value given in the 2nd argument.
6723**
6724** ^The sqlite3_result_null() interface sets the return value
6725** of the application-defined function to be NULL.
6726**
6727** ^The sqlite3_result_text(), sqlite3_result_text16(),
6728** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
6729** set the return value of the application-defined function to be
6730** a text string which is represented as UTF-8, UTF-16 native byte order,
6731** UTF-16 little endian, or UTF-16 big endian, respectively.
6732** ^The sqlite3_result_text64() interface sets the return value of an
6733** application-defined function to be a text string in an encoding
6734** specified by the fifth (and last) parameter, which must be one
6735** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
6736** ^SQLite takes the text result from the application from
6737** the 2nd parameter of the sqlite3_result_text* interfaces.
6738** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6739** is negative, then SQLite takes result text from the 2nd parameter
6740** through the first zero character.
6741** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6742** is non-negative, then as many bytes (not characters) of the text
6743** pointed to by the 2nd parameter are taken as the application-defined
6744** function result.  If the 3rd parameter is non-negative, then it
6745** must be the byte offset into the string where the NUL terminator would
6746** appear if the string where NUL terminated.  If any NUL characters occur
6747** in the string at a byte offset that is less than the value of the 3rd
6748** parameter, then the resulting string will contain embedded NULs and the
6749** result of expressions operating on strings with embedded NULs is undefined.
6750** ^If the 4th parameter to the sqlite3_result_text* interfaces
6751** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6752** function as the destructor on the text or BLOB result when it has
6753** finished using that result.
6754** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
6755** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
6756** assumes that the text or BLOB result is in constant space and does not
6757** copy the content of the parameter nor call a destructor on the content
6758** when it has finished using that result.
6759** ^If the 4th parameter to the sqlite3_result_text* interfaces
6760** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
6761** then SQLite makes a copy of the result into space obtained
6762** from [sqlite3_malloc()] before it returns.
6763**
6764** ^For the sqlite3_result_text16(), sqlite3_result_text16le(), and
6765** sqlite3_result_text16be() routines, and for sqlite3_result_text64()
6766** when the encoding is not UTF8, if the input UTF16 begins with a
6767** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
6768** string and the rest of the string is interpreted according to the
6769** byte-order specified by the BOM.  ^The byte-order specified by
6770** the BOM at the beginning of the text overrides the byte-order
6771** specified by the interface procedure.  ^So, for example, if
6772** sqlite3_result_text16le() is invoked with text that begins
6773** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
6774** first two bytes of input are skipped and the remaining input
6775** is interpreted as UTF16BE text.
6776**
6777** ^For UTF16 input text to the sqlite3_result_text16(),
6778** sqlite3_result_text16be(), sqlite3_result_text16le(), and
6779** sqlite3_result_text64() routines, if the text contains invalid
6780** UTF16 characters, the invalid characters might be converted
6781** into the unicode replacement character, U+FFFD.
6782**
6783** ^The sqlite3_result_value() interface sets the result of
6784** the application-defined function to be a copy of the
6785** [unprotected sqlite3_value] object specified by the 2nd parameter.  ^The
6786** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
6787** so that the [sqlite3_value] specified in the parameter may change or
6788** be deallocated after sqlite3_result_value() returns without harm.
6789** ^A [protected sqlite3_value] object may always be used where an
6790** [unprotected sqlite3_value] object is required, so either
6791** kind of [sqlite3_value] object can be used with this interface.
6792**
6793** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an
6794** SQL NULL value, just like [sqlite3_result_null(C)], except that it
6795** also associates the host-language pointer P or type T with that
6796** NULL value such that the pointer can be retrieved within an
6797** [application-defined SQL function] using [sqlite3_value_pointer()].
6798** ^If the D parameter is not NULL, then it is a pointer to a destructor
6799** for the P parameter.  ^SQLite invokes D with P as its only argument
6800** when SQLite is finished with P.  The T parameter should be a static
6801** string and preferably a string literal. The sqlite3_result_pointer()
6802** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
6803**
6804** If these routines are called from within the different thread
6805** than the one containing the application-defined function that received
6806** the [sqlite3_context] pointer, the results are undefined.
6807*/
6808SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
6809SQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*,
6810                           sqlite3_uint64,void(*)(void*));
6811SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
6812SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
6813SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
6814SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
6815SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
6816SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
6817SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
6818SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
6819SQLITE_API void sqlite3_result_null(sqlite3_context*);
6820SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6821SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
6822                           void(*)(void*), unsigned char encoding);
6823SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
6824SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
6825SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
6826SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
6827SQLITE_API void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*));
6828SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
6829SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
6830
6831
6832/*
6833** CAPI3REF: Setting The Subtype Of An SQL Function
6834** METHOD: sqlite3_context
6835**
6836** The sqlite3_result_subtype(C,T) function causes the subtype of
6837** the result from the [application-defined SQL function] with
6838** [sqlite3_context] C to be the value T.  Only the lower 8 bits
6839** of the subtype T are preserved in current versions of SQLite;
6840** higher order bits are discarded.
6841** The number of subtype bytes preserved by SQLite might increase
6842** in future releases of SQLite.
6843*/
6844SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6845
6846/*
6847** CAPI3REF: Define New Collating Sequences
6848** METHOD: sqlite3
6849**
6850** ^These functions add, remove, or modify a [collation] associated
6851** with the [database connection] specified as the first argument.
6852**
6853** ^The name of the collation is a UTF-8 string
6854** for sqlite3_create_collation() and sqlite3_create_collation_v2()
6855** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6856** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
6857** considered to be the same name.
6858**
6859** ^(The third argument (eTextRep) must be one of the constants:
6860** <ul>
6861** <li> [SQLITE_UTF8],
6862** <li> [SQLITE_UTF16LE],
6863** <li> [SQLITE_UTF16BE],
6864** <li> [SQLITE_UTF16], or
6865** <li> [SQLITE_UTF16_ALIGNED].
6866** </ul>)^
6867** ^The eTextRep argument determines the encoding of strings passed
6868** to the collating function callback, xCompare.
6869** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
6870** force strings to be UTF16 with native byte order.
6871** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
6872** on an even byte address.
6873**
6874** ^The fourth argument, pArg, is an application data pointer that is passed
6875** through as the first argument to the collating function callback.
6876**
6877** ^The fifth argument, xCompare, is a pointer to the collating function.
6878** ^Multiple collating functions can be registered using the same name but
6879** with different eTextRep parameters and SQLite will use whichever
6880** function requires the least amount of data transformation.
6881** ^If the xCompare argument is NULL then the collating function is
6882** deleted.  ^When all collating functions having the same name are deleted,
6883** that collation is no longer usable.
6884**
6885** ^The collating function callback is invoked with a copy of the pArg
6886** application data pointer and with two strings in the encoding specified
6887** by the eTextRep argument.  The two integer parameters to the collating
6888** function callback are the length of the two strings, in bytes. The collating
6889** function must return an integer that is negative, zero, or positive
6890** if the first string is less than, equal to, or greater than the second,
6891** respectively.  A collating function must always return the same answer
6892** given the same inputs.  If two or more collating functions are registered
6893** to the same collation name (using different eTextRep values) then all
6894** must give an equivalent answer when invoked with equivalent strings.
6895** The collating function must obey the following properties for all
6896** strings A, B, and C:
6897**
6898** <ol>
6899** <li> If A==B then B==A.
6900** <li> If A==B and B==C then A==C.
6901** <li> If A&lt;B THEN B&gt;A.
6902** <li> If A&lt;B and B&lt;C then A&lt;C.
6903** </ol>
6904**
6905** If a collating function fails any of the above constraints and that
6906** collating function is registered and used, then the behavior of SQLite
6907** is undefined.
6908**
6909** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
6910** with the addition that the xDestroy callback is invoked on pArg when
6911** the collating function is deleted.
6912** ^Collating functions are deleted when they are overridden by later
6913** calls to the collation creation functions or when the
6914** [database connection] is closed using [sqlite3_close()].
6915**
6916** ^The xDestroy callback is <u>not</u> called if the
6917** sqlite3_create_collation_v2() function fails.  Applications that invoke
6918** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6919** check the return code and dispose of the application data pointer
6920** themselves rather than expecting SQLite to deal with it for them.
6921** This is different from every other SQLite interface.  The inconsistency
6922** is unfortunate but cannot be changed without breaking backwards
6923** compatibility.
6924**
6925** See also:  [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
6926*/
6927SQLITE_API int sqlite3_create_collation(
6928  sqlite3*,
6929  const char *zName,
6930  int eTextRep,
6931  void *pArg,
6932  int(*xCompare)(void*,int,const void*,int,const void*)
6933);
6934SQLITE_API int sqlite3_create_collation_v2(
6935  sqlite3*,
6936  const char *zName,
6937  int eTextRep,
6938  void *pArg,
6939  int(*xCompare)(void*,int,const void*,int,const void*),
6940  void(*xDestroy)(void*)
6941);
6942SQLITE_API int sqlite3_create_collation16(
6943  sqlite3*,
6944  const void *zName,
6945  int eTextRep,
6946  void *pArg,
6947  int(*xCompare)(void*,int,const void*,int,const void*)
6948);
6949
6950/*
6951** CAPI3REF: Collation Needed Callbacks
6952** METHOD: sqlite3
6953**
6954** ^To avoid having to register all collation sequences before a database
6955** can be used, a single callback function may be registered with the
6956** [database connection] to be invoked whenever an undefined collation
6957** sequence is required.
6958**
6959** ^If the function is registered using the sqlite3_collation_needed() API,
6960** then it is passed the names of undefined collation sequences as strings
6961** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6962** the names are passed as UTF-16 in machine native byte order.
6963** ^A call to either function replaces the existing collation-needed callback.
6964**
6965** ^(When the callback is invoked, the first argument passed is a copy
6966** of the second argument to sqlite3_collation_needed() or
6967** sqlite3_collation_needed16().  The second argument is the database
6968** connection.  The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
6969** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
6970** sequence function required.  The fourth parameter is the name of the
6971** required collation sequence.)^
6972**
6973** The callback function should register the desired collation using
6974** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
6975** [sqlite3_create_collation_v2()].
6976*/
6977SQLITE_API int sqlite3_collation_needed(
6978  sqlite3*,
6979  void*,
6980  void(*)(void*,sqlite3*,int eTextRep,const char*)
6981);
6982SQLITE_API int sqlite3_collation_needed16(
6983  sqlite3*,
6984  void*,
6985  void(*)(void*,sqlite3*,int eTextRep,const void*)
6986);
6987
6988#ifdef SQLITE_ENABLE_CEROD
6989/*
6990** Specify the activation key for a CEROD database.  Unless
6991** activated, none of the CEROD routines will work.
6992*/
6993SQLITE_API void sqlite3_activate_cerod(
6994  const char *zPassPhrase        /* Activation phrase */
6995);
6996#endif
6997
6998/*
6999** CAPI3REF: Suspend Execution For A Short Time
7000**
7001** The sqlite3_sleep() function causes the current thread to suspend execution
7002** for at least a number of milliseconds specified in its parameter.
7003**
7004** If the operating system does not support sleep requests with
7005** millisecond time resolution, then the time will be rounded up to
7006** the nearest second. The number of milliseconds of sleep actually
7007** requested from the operating system is returned.
7008**
7009** ^SQLite implements this interface by calling the xSleep()
7010** method of the default [sqlite3_vfs] object.  If the xSleep() method
7011** of the default VFS is not implemented correctly, or not implemented at
7012** all, then the behavior of sqlite3_sleep() may deviate from the description
7013** in the previous paragraphs.
7014*/
7015SQLITE_API int sqlite3_sleep(int);
7016
7017/*
7018** CAPI3REF: Name Of The Folder Holding Temporary Files
7019**
7020** ^(If this global variable is made to point to a string which is
7021** the name of a folder (a.k.a. directory), then all temporary files
7022** created by SQLite when using a built-in [sqlite3_vfs | VFS]
7023** will be placed in that directory.)^  ^If this variable
7024** is a NULL pointer, then SQLite performs a search for an appropriate
7025** temporary file directory.
7026**
7027** Applications are strongly discouraged from using this global variable.
7028** It is required to set a temporary folder on Windows Runtime (WinRT).
7029** But for all other platforms, it is highly recommended that applications
7030** neither read nor write this variable.  This global variable is a relic
7031** that exists for backwards compatibility of legacy applications and should
7032** be avoided in new projects.
7033**
7034** It is not safe to read or modify this variable in more than one
7035** thread at a time.  It is not safe to read or modify this variable
7036** if a [database connection] is being used at the same time in a separate
7037** thread.
7038** It is intended that this variable be set once
7039** as part of process initialization and before any SQLite interface
7040** routines have been called and that this variable remain unchanged
7041** thereafter.
7042**
7043** ^The [temp_store_directory pragma] may modify this variable and cause
7044** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
7045** the [temp_store_directory pragma] always assumes that any string
7046** that this variable points to is held in memory obtained from
7047** [sqlite3_malloc] and the pragma may attempt to free that memory
7048** using [sqlite3_free].
7049** Hence, if this variable is modified directly, either it should be
7050** made NULL or made to point to memory obtained from [sqlite3_malloc]
7051** or else the use of the [temp_store_directory pragma] should be avoided.
7052** Except when requested by the [temp_store_directory pragma], SQLite
7053** does not free the memory that sqlite3_temp_directory points to.  If
7054** the application wants that memory to be freed, it must do
7055** so itself, taking care to only do so after all [database connection]
7056** objects have been destroyed.
7057**
7058** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
7059** prior to calling [sqlite3_open] or [sqlite3_open_v2].  Otherwise, various
7060** features that require the use of temporary files may fail.  Here is an
7061** example of how to do this using C++ with the Windows Runtime:
7062**
7063** <blockquote><pre>
7064** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
7065** &nbsp;     TemporaryFolder->Path->Data();
7066** char zPathBuf&#91;MAX_PATH + 1&#93;;
7067** memset(zPathBuf, 0, sizeof(zPathBuf));
7068** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
7069** &nbsp;     NULL, NULL);
7070** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
7071** </pre></blockquote>
7072*/
7073SQLITE_API char *sqlite3_temp_directory;
7074
7075/*
7076** CAPI3REF: Name Of The Folder Holding Database Files
7077**
7078** ^(If this global variable is made to point to a string which is
7079** the name of a folder (a.k.a. directory), then all database files
7080** specified with a relative pathname and created or accessed by
7081** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
7082** to be relative to that directory.)^ ^If this variable is a NULL
7083** pointer, then SQLite assumes that all database files specified
7084** with a relative pathname are relative to the current directory
7085** for the process.  Only the windows VFS makes use of this global
7086** variable; it is ignored by the unix VFS.
7087**
7088** Changing the value of this variable while a database connection is
7089** open can result in a corrupt database.
7090**
7091** It is not safe to read or modify this variable in more than one
7092** thread at a time.  It is not safe to read or modify this variable
7093** if a [database connection] is being used at the same time in a separate
7094** thread.
7095** It is intended that this variable be set once
7096** as part of process initialization and before any SQLite interface
7097** routines have been called and that this variable remain unchanged
7098** thereafter.
7099**
7100** ^The [data_store_directory pragma] may modify this variable and cause
7101** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
7102** the [data_store_directory pragma] always assumes that any string
7103** that this variable points to is held in memory obtained from
7104** [sqlite3_malloc] and the pragma may attempt to free that memory
7105** using [sqlite3_free].
7106** Hence, if this variable is modified directly, either it should be
7107** made NULL or made to point to memory obtained from [sqlite3_malloc]
7108** or else the use of the [data_store_directory pragma] should be avoided.
7109*/
7110SQLITE_API char *sqlite3_data_directory;
7111
7112/*
7113** CAPI3REF: Win32 Specific Interface
7114**
7115** These interfaces are available only on Windows.  The
7116** [sqlite3_win32_set_directory] interface is used to set the value associated
7117** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
7118** zValue, depending on the value of the type parameter.  The zValue parameter
7119** should be NULL to cause the previous value to be freed via [sqlite3_free];
7120** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
7121** prior to being used.  The [sqlite3_win32_set_directory] interface returns
7122** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
7123** or [SQLITE_NOMEM] if memory could not be allocated.  The value of the
7124** [sqlite3_data_directory] variable is intended to act as a replacement for
7125** the current directory on the sub-platforms of Win32 where that concept is
7126** not present, e.g. WinRT and UWP.  The [sqlite3_win32_set_directory8] and
7127** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
7128** sqlite3_win32_set_directory interface except the string parameter must be
7129** UTF-8 or UTF-16, respectively.
7130*/
7131SQLITE_API int sqlite3_win32_set_directory(
7132  unsigned long type, /* Identifier for directory being set or reset */
7133  void *zValue        /* New value for directory being set or reset */
7134);
7135SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
7136SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
7137
7138/*
7139** CAPI3REF: Win32 Directory Types
7140**
7141** These macros are only available on Windows.  They define the allowed values
7142** for the type argument to the [sqlite3_win32_set_directory] interface.
7143*/
7144#define SQLITE_WIN32_DATA_DIRECTORY_TYPE  1
7145#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE  2
7146
7147/*
7148** CAPI3REF: Test For Auto-Commit Mode
7149** KEYWORDS: {autocommit mode}
7150** METHOD: sqlite3
7151**
7152** ^The sqlite3_get_autocommit() interface returns non-zero or
7153** zero if the given database connection is or is not in autocommit mode,
7154** respectively.  ^Autocommit mode is on by default.
7155** ^Autocommit mode is disabled by a [BEGIN] statement.
7156** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
7157**
7158** If certain kinds of errors occur on a statement within a multi-statement
7159** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
7160** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
7161** transaction might be rolled back automatically.  The only way to
7162** find out whether SQLite automatically rolled back the transaction after
7163** an error is to use this function.
7164**
7165** If another thread changes the autocommit status of the database
7166** connection while this routine is running, then the return value
7167** is undefined.
7168*/
7169SQLITE_API int sqlite3_get_autocommit(sqlite3*);
7170
7171/*
7172** CAPI3REF: Find The Database Handle Of A Prepared Statement
7173** METHOD: sqlite3_stmt
7174**
7175** ^The sqlite3_db_handle interface returns the [database connection] handle
7176** to which a [prepared statement] belongs.  ^The [database connection]
7177** returned by sqlite3_db_handle is the same [database connection]
7178** that was the first argument
7179** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
7180** create the statement in the first place.
7181*/
7182SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
7183
7184/*
7185** CAPI3REF: Return The Filename For A Database Connection
7186** METHOD: sqlite3
7187**
7188** ^The sqlite3_db_filename(D,N) interface returns a pointer to the filename
7189** associated with database N of connection D.
7190** ^If there is no attached database N on the database
7191** connection D, or if database N is a temporary or in-memory database, then
7192** this function will return either a NULL pointer or an empty string.
7193**
7194** ^The string value returned by this routine is owned and managed by
7195** the database connection.  ^The value will be valid until the database N
7196** is [DETACH]-ed or until the database connection closes.
7197**
7198** ^The filename returned by this function is the output of the
7199** xFullPathname method of the [VFS].  ^In other words, the filename
7200** will be an absolute pathname, even if the filename used
7201** to open the database originally was a URI or relative pathname.
7202**
7203** If the filename pointer returned by this routine is not NULL, then it
7204** can be used as the filename input parameter to these routines:
7205** <ul>
7206** <li> [sqlite3_uri_parameter()]
7207** <li> [sqlite3_uri_boolean()]
7208** <li> [sqlite3_uri_int64()]
7209** <li> [sqlite3_filename_database()]
7210** <li> [sqlite3_filename_journal()]
7211** <li> [sqlite3_filename_wal()]
7212** </ul>
7213*/
7214SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
7215
7216/*
7217** CAPI3REF: Determine if a database is read-only
7218** METHOD: sqlite3
7219**
7220** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
7221** of connection D is read-only, 0 if it is read/write, or -1 if N is not
7222** the name of a database on connection D.
7223*/
7224SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
7225
7226/*
7227** CAPI3REF: Find the next prepared statement
7228** METHOD: sqlite3
7229**
7230** ^This interface returns a pointer to the next [prepared statement] after
7231** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
7232** then this interface returns a pointer to the first prepared statement
7233** associated with the database connection pDb.  ^If no prepared statement
7234** satisfies the conditions of this routine, it returns NULL.
7235**
7236** The [database connection] pointer D in a call to
7237** [sqlite3_next_stmt(D,S)] must refer to an open database
7238** connection and in particular must not be a NULL pointer.
7239*/
7240SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
7241
7242/*
7243** CAPI3REF: Commit And Rollback Notification Callbacks
7244** METHOD: sqlite3
7245**
7246** ^The sqlite3_commit_hook() interface registers a callback
7247** function to be invoked whenever a transaction is [COMMIT | committed].
7248** ^Any callback set by a previous call to sqlite3_commit_hook()
7249** for the same database connection is overridden.
7250** ^The sqlite3_rollback_hook() interface registers a callback
7251** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
7252** ^Any callback set by a previous call to sqlite3_rollback_hook()
7253** for the same database connection is overridden.
7254** ^The pArg argument is passed through to the callback.
7255** ^If the callback on a commit hook function returns non-zero,
7256** then the commit is converted into a rollback.
7257**
7258** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
7259** return the P argument from the previous call of the same function
7260** on the same [database connection] D, or NULL for
7261** the first call for each function on D.
7262**
7263** The commit and rollback hook callbacks are not reentrant.
7264** The callback implementation must not do anything that will modify
7265** the database connection that invoked the callback.  Any actions
7266** to modify the database connection must be deferred until after the
7267** completion of the [sqlite3_step()] call that triggered the commit
7268** or rollback hook in the first place.
7269** Note that running any other SQL statements, including SELECT statements,
7270** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
7271** the database connections for the meaning of "modify" in this paragraph.
7272**
7273** ^Registering a NULL function disables the callback.
7274**
7275** ^When the commit hook callback routine returns zero, the [COMMIT]
7276** operation is allowed to continue normally.  ^If the commit hook
7277** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
7278** ^The rollback hook is invoked on a rollback that results from a commit
7279** hook returning non-zero, just as it would be with any other rollback.
7280**
7281** ^For the purposes of this API, a transaction is said to have been
7282** rolled back if an explicit "ROLLBACK" statement is executed, or
7283** an error or constraint causes an implicit rollback to occur.
7284** ^The rollback callback is not invoked if a transaction is
7285** automatically rolled back because the database connection is closed.
7286**
7287** See also the [sqlite3_update_hook()] interface.
7288*/
7289SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
7290SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
7291
7292/*
7293** CAPI3REF: Data Change Notification Callbacks
7294** METHOD: sqlite3
7295**
7296** ^The sqlite3_update_hook() interface registers a callback function
7297** with the [database connection] identified by the first argument
7298** to be invoked whenever a row is updated, inserted or deleted in
7299** a [rowid table].
7300** ^Any callback set by a previous call to this function
7301** for the same database connection is overridden.
7302**
7303** ^The second argument is a pointer to the function to invoke when a
7304** row is updated, inserted or deleted in a rowid table.
7305** ^The first argument to the callback is a copy of the third argument
7306** to sqlite3_update_hook().
7307** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
7308** or [SQLITE_UPDATE], depending on the operation that caused the callback
7309** to be invoked.
7310** ^The third and fourth arguments to the callback contain pointers to the
7311** database and table name containing the affected row.
7312** ^The final callback parameter is the [rowid] of the row.
7313** ^In the case of an update, this is the [rowid] after the update takes place.
7314**
7315** ^(The update hook is not invoked when internal system tables are
7316** modified (i.e. sqlite_master and sqlite_sequence).)^
7317** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
7318**
7319** ^In the current implementation, the update hook
7320** is not invoked when conflicting rows are deleted because of an
7321** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
7322** invoked when rows are deleted using the [truncate optimization].
7323** The exceptions defined in this paragraph might change in a future
7324** release of SQLite.
7325**
7326** The update hook implementation must not do anything that will modify
7327** the database connection that invoked the update hook.  Any actions
7328** to modify the database connection must be deferred until after the
7329** completion of the [sqlite3_step()] call that triggered the update hook.
7330** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
7331** database connections for the meaning of "modify" in this paragraph.
7332**
7333** ^The sqlite3_update_hook(D,C,P) function
7334** returns the P argument from the previous call
7335** on the same [database connection] D, or NULL for
7336** the first call on D.
7337**
7338** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()],
7339** and [sqlite3_preupdate_hook()] interfaces.
7340*/
7341SQLITE_API void *sqlite3_update_hook(
7342  sqlite3*,
7343  void(*)(void *,int ,char const *,char const *,sqlite3_int64),
7344  void*
7345);
7346
7347/*
7348** CAPI3REF: Enable Or Disable Shared Pager Cache
7349**
7350** ^(This routine enables or disables the sharing of the database cache
7351** and schema data structures between [database connection | connections]
7352** to the same database. Sharing is enabled if the argument is true
7353** and disabled if the argument is false.)^
7354**
7355** ^Cache sharing is enabled and disabled for an entire process.
7356** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
7357** In prior versions of SQLite,
7358** sharing was enabled or disabled for each thread separately.
7359**
7360** ^(The cache sharing mode set by this interface effects all subsequent
7361** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
7362** Existing database connections continue to use the sharing mode
7363** that was in effect at the time they were opened.)^
7364**
7365** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
7366** successfully.  An [error code] is returned otherwise.)^
7367**
7368** ^Shared cache is disabled by default. It is recommended that it stay
7369** that way.  In other words, do not use this routine.  This interface
7370** continues to be provided for historical compatibility, but its use is
7371** discouraged.  Any use of shared cache is discouraged.  If shared cache
7372** must be used, it is recommended that shared cache only be enabled for
7373** individual database connections using the [sqlite3_open_v2()] interface
7374** with the [SQLITE_OPEN_SHAREDCACHE] flag.
7375**
7376** Note: This method is disabled on MacOS X 10.7 and iOS version 5.0
7377** and will always return SQLITE_MISUSE. On those systems,
7378** shared cache mode should be enabled per-database connection via
7379** [sqlite3_open_v2()] with [SQLITE_OPEN_SHAREDCACHE].
7380**
7381** This interface is threadsafe on processors where writing a
7382** 32-bit integer is atomic.
7383**
7384** See Also:  [SQLite Shared-Cache Mode]
7385*/
7386SQLITE_API int sqlite3_enable_shared_cache(int);
7387
7388/*
7389** CAPI3REF: Attempt To Free Heap Memory
7390**
7391** ^The sqlite3_release_memory() interface attempts to free N bytes
7392** of heap memory by deallocating non-essential memory allocations
7393** held by the database library.   Memory used to cache database
7394** pages to improve performance is an example of non-essential memory.
7395** ^sqlite3_release_memory() returns the number of bytes actually freed,
7396** which might be more or less than the amount requested.
7397** ^The sqlite3_release_memory() routine is a no-op returning zero
7398** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
7399**
7400** See also: [sqlite3_db_release_memory()]
7401*/
7402SQLITE_API int sqlite3_release_memory(int);
7403
7404/*
7405** CAPI3REF: Free Memory Used By A Database Connection
7406** METHOD: sqlite3
7407**
7408** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
7409** memory as possible from database connection D. Unlike the
7410** [sqlite3_release_memory()] interface, this interface is in effect even
7411** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
7412** omitted.
7413**
7414** See also: [sqlite3_release_memory()]
7415*/
7416SQLITE_API int sqlite3_db_release_memory(sqlite3*);
7417
7418/*
7419** CAPI3REF: Impose A Limit On Heap Size
7420**
7421** These interfaces impose limits on the amount of heap memory that will be
7422** by all database connections within a single process.
7423**
7424** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
7425** soft limit on the amount of heap memory that may be allocated by SQLite.
7426** ^SQLite strives to keep heap memory utilization below the soft heap
7427** limit by reducing the number of pages held in the page cache
7428** as heap memory usages approaches the limit.
7429** ^The soft heap limit is "soft" because even though SQLite strives to stay
7430** below the limit, it will exceed the limit rather than generate
7431** an [SQLITE_NOMEM] error.  In other words, the soft heap limit
7432** is advisory only.
7433**
7434** ^The sqlite3_hard_heap_limit64(N) interface sets a hard upper bound of
7435** N bytes on the amount of memory that will be allocated.  ^The
7436** sqlite3_hard_heap_limit64(N) interface is similar to
7437** sqlite3_soft_heap_limit64(N) except that memory allocations will fail
7438** when the hard heap limit is reached.
7439**
7440** ^The return value from both sqlite3_soft_heap_limit64() and
7441** sqlite3_hard_heap_limit64() is the size of
7442** the heap limit prior to the call, or negative in the case of an
7443** error.  ^If the argument N is negative
7444** then no change is made to the heap limit.  Hence, the current
7445** size of heap limits can be determined by invoking
7446** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
7447**
7448** ^Setting the heap limits to zero disables the heap limiter mechanism.
7449**
7450** ^The soft heap limit may not be greater than the hard heap limit.
7451** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N)
7452** is invoked with a value of N that is greater than the hard heap limit,
7453** the the soft heap limit is set to the value of the hard heap limit.
7454** ^The soft heap limit is automatically enabled whenever the hard heap
7455** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and
7456** the soft heap limit is outside the range of 1..N, then the soft heap
7457** limit is set to N.  ^Invoking sqlite3_soft_heap_limit64(0) when the
7458** hard heap limit is enabled makes the soft heap limit equal to the
7459** hard heap limit.
7460**
7461** The memory allocation limits can also be adjusted using
7462** [PRAGMA soft_heap_limit] and [PRAGMA hard_heap_limit].
7463**
7464** ^(The heap limits are not enforced in the current implementation
7465** if one or more of following conditions are true:
7466**
7467** <ul>
7468** <li> The limit value is set to zero.
7469** <li> Memory accounting is disabled using a combination of the
7470**      [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7471**      the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7472** <li> An alternative page cache implementation is specified using
7473**      [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
7474** <li> The page cache allocates from its own memory pool supplied
7475**      by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
7476**      from the heap.
7477** </ul>)^
7478**
7479** The circumstances under which SQLite will enforce the heap limits may
7480** changes in future releases of SQLite.
7481*/
7482SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
7483SQLITE_API sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 N);
7484
7485/*
7486** CAPI3REF: Deprecated Soft Heap Limit Interface
7487** DEPRECATED
7488**
7489** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
7490** interface.  This routine is provided for historical compatibility
7491** only.  All new applications should use the
7492** [sqlite3_soft_heap_limit64()] interface rather than this one.
7493*/
7494SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
7495
7496
7497/*
7498** CAPI3REF: Extract Metadata About A Column Of A Table
7499** METHOD: sqlite3
7500**
7501** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns
7502** information about column C of table T in database D
7503** on [database connection] X.)^  ^The sqlite3_table_column_metadata()
7504** interface returns SQLITE_OK and fills in the non-NULL pointers in
7505** the final five arguments with appropriate values if the specified
7506** column exists.  ^The sqlite3_table_column_metadata() interface returns
7507** SQLITE_ERROR if the specified column does not exist.
7508** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7509** NULL pointer, then this routine simply checks for the existence of the
7510** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it
7511** does not.  If the table name parameter T in a call to
7512** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is
7513** undefined behavior.
7514**
7515** ^The column is identified by the second, third and fourth parameters to
7516** this function. ^(The second parameter is either the name of the database
7517** (i.e. "main", "temp", or an attached database) containing the specified
7518** table or NULL.)^ ^If it is NULL, then all attached databases are searched
7519** for the table using the same algorithm used by the database engine to
7520** resolve unqualified table references.
7521**
7522** ^The third and fourth parameters to this function are the table and column
7523** name of the desired column, respectively.
7524**
7525** ^Metadata is returned by writing to the memory locations passed as the 5th
7526** and subsequent parameters to this function. ^Any of these arguments may be
7527** NULL, in which case the corresponding element of metadata is omitted.
7528**
7529** ^(<blockquote>
7530** <table border="1">
7531** <tr><th> Parameter <th> Output<br>Type <th>  Description
7532**
7533** <tr><td> 5th <td> const char* <td> Data type
7534** <tr><td> 6th <td> const char* <td> Name of default collation sequence
7535** <tr><td> 7th <td> int         <td> True if column has a NOT NULL constraint
7536** <tr><td> 8th <td> int         <td> True if column is part of the PRIMARY KEY
7537** <tr><td> 9th <td> int         <td> True if column is [AUTOINCREMENT]
7538** </table>
7539** </blockquote>)^
7540**
7541** ^The memory pointed to by the character pointers returned for the
7542** declaration type and collation sequence is valid until the next
7543** call to any SQLite API function.
7544**
7545** ^If the specified table is actually a view, an [error code] is returned.
7546**
7547** ^If the specified column is "rowid", "oid" or "_rowid_" and the table
7548** is not a [WITHOUT ROWID] table and an
7549** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
7550** parameters are set for the explicitly declared column. ^(If there is no
7551** [INTEGER PRIMARY KEY] column, then the outputs
7552** for the [rowid] are set as follows:
7553**
7554** <pre>
7555**     data type: "INTEGER"
7556**     collation sequence: "BINARY"
7557**     not null: 0
7558**     primary key: 1
7559**     auto increment: 0
7560** </pre>)^
7561**
7562** ^This function causes all database schemas to be read from disk and
7563** parsed, if that has not already been done, and returns an error if
7564** any errors are encountered while loading the schema.
7565*/
7566SQLITE_API int sqlite3_table_column_metadata(
7567  sqlite3 *db,                /* Connection handle */
7568  const char *zDbName,        /* Database name or NULL */
7569  const char *zTableName,     /* Table name */
7570  const char *zColumnName,    /* Column name */
7571  char const **pzDataType,    /* OUTPUT: Declared data type */
7572  char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
7573  int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
7574  int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
7575  int *pAutoinc               /* OUTPUT: True if column is auto-increment */
7576);
7577
7578/*
7579** CAPI3REF: Load An Extension
7580** METHOD: sqlite3
7581**
7582** ^This interface loads an SQLite extension library from the named file.
7583**
7584** ^The sqlite3_load_extension() interface attempts to load an
7585** [SQLite extension] library contained in the file zFile.  If
7586** the file cannot be loaded directly, attempts are made to load
7587** with various operating-system specific extensions added.
7588** So for example, if "samplelib" cannot be loaded, then names like
7589** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
7590** be tried also.
7591**
7592** ^The entry point is zProc.
7593** ^(zProc may be 0, in which case SQLite will try to come up with an
7594** entry point name on its own.  It first tries "sqlite3_extension_init".
7595** If that does not work, it constructs a name "sqlite3_X_init" where the
7596** X is consists of the lower-case equivalent of all ASCII alphabetic
7597** characters in the filename from the last "/" to the first following
7598** "." and omitting any initial "lib".)^
7599** ^The sqlite3_load_extension() interface returns
7600** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
7601** ^If an error occurs and pzErrMsg is not 0, then the
7602** [sqlite3_load_extension()] interface shall attempt to
7603** fill *pzErrMsg with error message text stored in memory
7604** obtained from [sqlite3_malloc()]. The calling function
7605** should free this memory by calling [sqlite3_free()].
7606**
7607** ^Extension loading must be enabled using
7608** [sqlite3_enable_load_extension()] or
7609** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL)
7610** prior to calling this API,
7611** otherwise an error will be returned.
7612**
7613** <b>Security warning:</b> It is recommended that the
7614** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this
7615** interface.  The use of the [sqlite3_enable_load_extension()] interface
7616** should be avoided.  This will keep the SQL function [load_extension()]
7617** disabled and prevent SQL injections from giving attackers
7618** access to extension loading capabilities.
7619**
7620** See also the [load_extension() SQL function].
7621*/
7622SQLITE_API int sqlite3_load_extension(
7623  sqlite3 *db,          /* Load the extension into this database connection */
7624  const char *zFile,    /* Name of the shared library containing extension */
7625  const char *zProc,    /* Entry point.  Derived from zFile if 0 */
7626  char **pzErrMsg       /* Put error message here if not 0 */
7627);
7628
7629/*
7630** CAPI3REF: Enable Or Disable Extension Loading
7631** METHOD: sqlite3
7632**
7633** ^So as not to open security holes in older applications that are
7634** unprepared to deal with [extension loading], and as a means of disabling
7635** [extension loading] while evaluating user-entered SQL, the following API
7636** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
7637**
7638** ^Extension loading is off by default.
7639** ^Call the sqlite3_enable_load_extension() routine with onoff==1
7640** to turn extension loading on and call it with onoff==0 to turn
7641** it back off again.
7642**
7643** ^This interface enables or disables both the C-API
7644** [sqlite3_load_extension()] and the SQL function [load_extension()].
7645** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
7646** to enable or disable only the C-API.)^
7647**
7648** <b>Security warning:</b> It is recommended that extension loading
7649** be enabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method
7650** rather than this interface, so the [load_extension()] SQL function
7651** remains disabled. This will prevent SQL injections from giving attackers
7652** access to extension loading capabilities.
7653*/
7654SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
7655
7656/*
7657** CAPI3REF: Automatically Load Statically Linked Extensions
7658**
7659** ^This interface causes the xEntryPoint() function to be invoked for
7660** each new [database connection] that is created.  The idea here is that
7661** xEntryPoint() is the entry point for a statically linked [SQLite extension]
7662** that is to be automatically loaded into all new database connections.
7663**
7664** ^(Even though the function prototype shows that xEntryPoint() takes
7665** no arguments and returns void, SQLite invokes xEntryPoint() with three
7666** arguments and expects an integer result as if the signature of the
7667** entry point where as follows:
7668**
7669** <blockquote><pre>
7670** &nbsp;  int xEntryPoint(
7671** &nbsp;    sqlite3 *db,
7672** &nbsp;    const char **pzErrMsg,
7673** &nbsp;    const struct sqlite3_api_routines *pThunk
7674** &nbsp;  );
7675** </pre></blockquote>)^
7676**
7677** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
7678** point to an appropriate error message (obtained from [sqlite3_mprintf()])
7679** and return an appropriate [error code].  ^SQLite ensures that *pzErrMsg
7680** is NULL before calling the xEntryPoint().  ^SQLite will invoke
7681** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns.  ^If any
7682** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
7683** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
7684**
7685** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
7686** on the list of automatic extensions is a harmless no-op. ^No entry point
7687** will be called more than once for each database connection that is opened.
7688**
7689** See also: [sqlite3_reset_auto_extension()]
7690** and [sqlite3_cancel_auto_extension()]
7691*/
7692SQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void));
7693
7694/*
7695** CAPI3REF: Cancel Automatic Extension Loading
7696**
7697** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the
7698** initialization routine X that was registered using a prior call to
7699** [sqlite3_auto_extension(X)].  ^The [sqlite3_cancel_auto_extension(X)]
7700** routine returns 1 if initialization routine X was successfully
7701** unregistered and it returns 0 if X was not on the list of initialization
7702** routines.
7703*/
7704SQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void));
7705
7706/*
7707** CAPI3REF: Reset Automatic Extension Loading
7708**
7709** ^This interface disables all automatic extensions previously
7710** registered using [sqlite3_auto_extension()].
7711*/
7712SQLITE_API void sqlite3_reset_auto_extension(void);
7713
7714/*
7715** The interface to the virtual-table mechanism is currently considered
7716** to be experimental.  The interface might change in incompatible ways.
7717** If this is a problem for you, do not use the interface at this time.
7718**
7719** When the virtual-table mechanism stabilizes, we will declare the
7720** interface fixed, support it indefinitely, and remove this comment.
7721*/
7722
7723/*
7724** Structures used by the virtual table interface
7725*/
7726typedef struct sqlite3_vtab sqlite3_vtab;
7727typedef struct sqlite3_index_info sqlite3_index_info;
7728typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
7729typedef struct sqlite3_module sqlite3_module;
7730
7731/*
7732** CAPI3REF: Virtual Table Object
7733** KEYWORDS: sqlite3_module {virtual table module}
7734**
7735** This structure, sometimes called a "virtual table module",
7736** defines the implementation of a [virtual table].
7737** This structure consists mostly of methods for the module.
7738**
7739** ^A virtual table module is created by filling in a persistent
7740** instance of this structure and passing a pointer to that instance
7741** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
7742** ^The registration remains valid until it is replaced by a different
7743** module or until the [database connection] closes.  The content
7744** of this structure must not change while it is registered with
7745** any database connection.
7746*/
7747struct sqlite3_module {
7748  int iVersion;
7749  int (*xCreate)(sqlite3*, void *pAux,
7750               int argc, const char *const*argv,
7751               sqlite3_vtab **ppVTab, char**);
7752  int (*xConnect)(sqlite3*, void *pAux,
7753               int argc, const char *const*argv,
7754               sqlite3_vtab **ppVTab, char**);
7755  int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
7756  int (*xDisconnect)(sqlite3_vtab *pVTab);
7757  int (*xDestroy)(sqlite3_vtab *pVTab);
7758  int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
7759  int (*xClose)(sqlite3_vtab_cursor*);
7760  int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
7761                int argc, sqlite3_value **argv);
7762  int (*xNext)(sqlite3_vtab_cursor*);
7763  int (*xEof)(sqlite3_vtab_cursor*);
7764  int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
7765  int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
7766  int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
7767  int (*xBegin)(sqlite3_vtab *pVTab);
7768  int (*xSync)(sqlite3_vtab *pVTab);
7769  int (*xCommit)(sqlite3_vtab *pVTab);
7770  int (*xRollback)(sqlite3_vtab *pVTab);
7771  int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
7772                       void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
7773                       void **ppArg);
7774  int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
7775  /* The methods above are in version 1 of the sqlite_module object. Those
7776  ** below are for version 2 and greater. */
7777  int (*xSavepoint)(sqlite3_vtab *pVTab, int);
7778  int (*xRelease)(sqlite3_vtab *pVTab, int);
7779  int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
7780  /* The methods above are in versions 1 and 2 of the sqlite_module object.
7781  ** Those below are for version 3 and greater. */
7782  int (*xShadowName)(const char*);
7783};
7784
7785/*
7786** CAPI3REF: Virtual Table Indexing Information
7787** KEYWORDS: sqlite3_index_info
7788**
7789** The sqlite3_index_info structure and its substructures is used as part
7790** of the [virtual table] interface to
7791** pass information into and receive the reply from the [xBestIndex]
7792** method of a [virtual table module].  The fields under **Inputs** are the
7793** inputs to xBestIndex and are read-only.  xBestIndex inserts its
7794** results into the **Outputs** fields.
7795**
7796** ^(The aConstraint[] array records WHERE clause constraints of the form:
7797**
7798** <blockquote>column OP expr</blockquote>
7799**
7800** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^  ^(The particular operator is
7801** stored in aConstraint[].op using one of the
7802** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
7803** ^(The index of the column is stored in
7804** aConstraint[].iColumn.)^  ^(aConstraint[].usable is TRUE if the
7805** expr on the right-hand side can be evaluated (and thus the constraint
7806** is usable) and false if it cannot.)^
7807**
7808** ^The optimizer automatically inverts terms of the form "expr OP column"
7809** and makes other simplifications to the WHERE clause in an attempt to
7810** get as many WHERE clause terms into the form shown above as possible.
7811** ^The aConstraint[] array only reports WHERE clause terms that are
7812** relevant to the particular virtual table being queried.
7813**
7814** ^Information about the ORDER BY clause is stored in aOrderBy[].
7815** ^Each term of aOrderBy records a column of the ORDER BY clause.
7816**
7817** The colUsed field indicates which columns of the virtual table may be
7818** required by the current scan. Virtual table columns are numbered from
7819** zero in the order in which they appear within the CREATE TABLE statement
7820** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7821** the corresponding bit is set within the colUsed mask if the column may be
7822** required by SQLite. If the table has at least 64 columns and any column
7823** to the right of the first 63 is required, then bit 63 of colUsed is also
7824** set. In other words, column iCol may be required if the expression
7825** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to
7826** non-zero.
7827**
7828** The [xBestIndex] method must fill aConstraintUsage[] with information
7829** about what parameters to pass to xFilter.  ^If argvIndex>0 then
7830** the right-hand side of the corresponding aConstraint[] is evaluated
7831** and becomes the argvIndex-th entry in argv.  ^(If aConstraintUsage[].omit
7832** is true, then the constraint is assumed to be fully handled by the
7833** virtual table and might not be checked again by the byte code.)^ ^(The
7834** aConstraintUsage[].omit flag is an optimization hint. When the omit flag
7835** is left in its default setting of false, the constraint will always be
7836** checked separately in byte code.  If the omit flag is change to true, then
7837** the constraint may or may not be checked in byte code.  In other words,
7838** when the omit flag is true there is no guarantee that the constraint will
7839** not be checked again using byte code.)^
7840**
7841** ^The idxNum and idxPtr values are recorded and passed into the
7842** [xFilter] method.
7843** ^[sqlite3_free()] is used to free idxPtr if and only if
7844** needToFreeIdxPtr is true.
7845**
7846** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
7847** the correct order to satisfy the ORDER BY clause so that no separate
7848** sorting step is required.
7849**
7850** ^The estimatedCost value is an estimate of the cost of a particular
7851** strategy. A cost of N indicates that the cost of the strategy is similar
7852** to a linear scan of an SQLite table with N rows. A cost of log(N)
7853** indicates that the expense of the operation is similar to that of a
7854** binary search on a unique indexed field of an SQLite table with N rows.
7855**
7856** ^The estimatedRows value is an estimate of the number of rows that
7857** will be returned by the strategy.
7858**
7859** The xBestIndex method may optionally populate the idxFlags field with a
7860** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7861** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite
7862** assumes that the strategy may visit at most one row.
7863**
7864** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then
7865** SQLite also assumes that if a call to the xUpdate() method is made as
7866** part of the same statement to delete or update a virtual table row and the
7867** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback
7868** any database changes. In other words, if the xUpdate() returns
7869** SQLITE_CONSTRAINT, the database contents must be exactly as they were
7870** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
7871** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
7872** the xUpdate method are automatically rolled back by SQLite.
7873**
7874** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
7875** structure for SQLite [version 3.8.2] ([dateof:3.8.2]).
7876** If a virtual table extension is
7877** used with an SQLite version earlier than 3.8.2, the results of attempting
7878** to read or write the estimatedRows field are undefined (but are likely
7879** to include crashing the application). The estimatedRows field should
7880** therefore only be used if [sqlite3_libversion_number()] returns a
7881** value greater than or equal to 3008002. Similarly, the idxFlags field
7882** was added for [version 3.9.0] ([dateof:3.9.0]).
7883** It may therefore only be used if
7884** sqlite3_libversion_number() returns a value greater than or equal to
7885** 3009000.
7886*/
7887struct sqlite3_index_info {
7888  /* Inputs */
7889  int nConstraint;           /* Number of entries in aConstraint */
7890  struct sqlite3_index_constraint {
7891     int iColumn;              /* Column constrained.  -1 for ROWID */
7892     unsigned char op;         /* Constraint operator */
7893     unsigned char usable;     /* True if this constraint is usable */
7894     int iTermOffset;          /* Used internally - xBestIndex should ignore */
7895  } *aConstraint;            /* Table of WHERE clause constraints */
7896  int nOrderBy;              /* Number of terms in the ORDER BY clause */
7897  struct sqlite3_index_orderby {
7898     int iColumn;              /* Column number */
7899     unsigned char desc;       /* True for DESC.  False for ASC. */
7900  } *aOrderBy;               /* The ORDER BY clause */
7901  /* Outputs */
7902  struct sqlite3_index_constraint_usage {
7903    int argvIndex;           /* if >0, constraint is part of argv to xFilter */
7904    unsigned char omit;      /* Do not code a test for this constraint */
7905  } *aConstraintUsage;
7906  int idxNum;                /* Number used to identify the index */
7907  char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
7908  int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
7909  int orderByConsumed;       /* True if output is already ordered */
7910  double estimatedCost;           /* Estimated cost of using this index */
7911  /* Fields below are only available in SQLite 3.8.2 and later */
7912  sqlite3_int64 estimatedRows;    /* Estimated number of rows returned */
7913  /* Fields below are only available in SQLite 3.9.0 and later */
7914  int idxFlags;              /* Mask of SQLITE_INDEX_SCAN_* flags */
7915  /* Fields below are only available in SQLite 3.10.0 and later */
7916  sqlite3_uint64 colUsed;    /* Input: Mask of columns used by statement */
7917};
7918
7919/*
7920** CAPI3REF: Virtual Table Scan Flags
7921**
7922** Virtual table implementations are allowed to set the
7923** [sqlite3_index_info].idxFlags field to some combination of
7924** these bits.
7925*/
7926#define SQLITE_INDEX_SCAN_UNIQUE      1     /* Scan visits at most 1 row */
7927
7928/*
7929** CAPI3REF: Virtual Table Constraint Operator Codes
7930**
7931** These macros define the allowed values for the
7932** [sqlite3_index_info].aConstraint[].op field.  Each value represents
7933** an operator that is part of a constraint term in the wHERE clause of
7934** a query that uses a [virtual table].
7935*/
7936#define SQLITE_INDEX_CONSTRAINT_EQ         2
7937#define SQLITE_INDEX_CONSTRAINT_GT         4
7938#define SQLITE_INDEX_CONSTRAINT_LE         8
7939#define SQLITE_INDEX_CONSTRAINT_LT        16
7940#define SQLITE_INDEX_CONSTRAINT_GE        32
7941#define SQLITE_INDEX_CONSTRAINT_MATCH     64
7942#define SQLITE_INDEX_CONSTRAINT_LIKE      65
7943#define SQLITE_INDEX_CONSTRAINT_GLOB      66
7944#define SQLITE_INDEX_CONSTRAINT_REGEXP    67
7945#define SQLITE_INDEX_CONSTRAINT_NE        68
7946#define SQLITE_INDEX_CONSTRAINT_ISNOT     69
7947#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
7948#define SQLITE_INDEX_CONSTRAINT_ISNULL    71
7949#define SQLITE_INDEX_CONSTRAINT_IS        72
7950#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
7951
7952/*
7953** CAPI3REF: Register A Virtual Table Implementation
7954** METHOD: sqlite3
7955**
7956** ^These routines are used to register a new [virtual table module] name.
7957** ^Module names must be registered before
7958** creating a new [virtual table] using the module and before using a
7959** preexisting [virtual table] for the module.
7960**
7961** ^The module name is registered on the [database connection] specified
7962** by the first parameter.  ^The name of the module is given by the
7963** second parameter.  ^The third parameter is a pointer to
7964** the implementation of the [virtual table module].   ^The fourth
7965** parameter is an arbitrary client data pointer that is passed through
7966** into the [xCreate] and [xConnect] methods of the virtual table module
7967** when a new virtual table is be being created or reinitialized.
7968**
7969** ^The sqlite3_create_module_v2() interface has a fifth parameter which
7970** is a pointer to a destructor for the pClientData.  ^SQLite will
7971** invoke the destructor function (if it is not NULL) when SQLite
7972** no longer needs the pClientData pointer.  ^The destructor will also
7973** be invoked if the call to sqlite3_create_module_v2() fails.
7974** ^The sqlite3_create_module()
7975** interface is equivalent to sqlite3_create_module_v2() with a NULL
7976** destructor.
7977**
7978** ^If the third parameter (the pointer to the sqlite3_module object) is
7979** NULL then no new module is create and any existing modules with the
7980** same name are dropped.
7981**
7982** See also: [sqlite3_drop_modules()]
7983*/
7984SQLITE_API int sqlite3_create_module(
7985  sqlite3 *db,               /* SQLite connection to register module with */
7986  const char *zName,         /* Name of the module */
7987  const sqlite3_module *p,   /* Methods for the module */
7988  void *pClientData          /* Client data for xCreate/xConnect */
7989);
7990SQLITE_API int sqlite3_create_module_v2(
7991  sqlite3 *db,               /* SQLite connection to register module with */
7992  const char *zName,         /* Name of the module */
7993  const sqlite3_module *p,   /* Methods for the module */
7994  void *pClientData,         /* Client data for xCreate/xConnect */
7995  void(*xDestroy)(void*)     /* Module destructor function */
7996);
7997
7998/*
7999** CAPI3REF: Remove Unnecessary Virtual Table Implementations
8000** METHOD: sqlite3
8001**
8002** ^The sqlite3_drop_modules(D,L) interface removes all virtual
8003** table modules from database connection D except those named on list L.
8004** The L parameter must be either NULL or a pointer to an array of pointers
8005** to strings where the array is terminated by a single NULL pointer.
8006** ^If the L parameter is NULL, then all virtual table modules are removed.
8007**
8008** See also: [sqlite3_create_module()]
8009*/
8010SQLITE_API int sqlite3_drop_modules(
8011  sqlite3 *db,                /* Remove modules from this connection */
8012  const char **azKeep         /* Except, do not remove the ones named here */
8013);
8014
8015/*
8016** CAPI3REF: Virtual Table Instance Object
8017** KEYWORDS: sqlite3_vtab
8018**
8019** Every [virtual table module] implementation uses a subclass
8020** of this object to describe a particular instance
8021** of the [virtual table].  Each subclass will
8022** be tailored to the specific needs of the module implementation.
8023** The purpose of this superclass is to define certain fields that are
8024** common to all module implementations.
8025**
8026** ^Virtual tables methods can set an error message by assigning a
8027** string obtained from [sqlite3_mprintf()] to zErrMsg.  The method should
8028** take care that any prior string is freed by a call to [sqlite3_free()]
8029** prior to assigning a new string to zErrMsg.  ^After the error message
8030** is delivered up to the client application, the string will be automatically
8031** freed by sqlite3_free() and the zErrMsg field will be zeroed.
8032*/
8033struct sqlite3_vtab {
8034  const sqlite3_module *pModule;  /* The module for this virtual table */
8035  int nRef;                       /* Number of open cursors */
8036  char *zErrMsg;                  /* Error message from sqlite3_mprintf() */
8037  /* Virtual table implementations will typically add additional fields */
8038};
8039
8040/*
8041** CAPI3REF: Virtual Table Cursor Object
8042** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
8043**
8044** Every [virtual table module] implementation uses a subclass of the
8045** following structure to describe cursors that point into the
8046** [virtual table] and are used
8047** to loop through the virtual table.  Cursors are created using the
8048** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
8049** by the [sqlite3_module.xClose | xClose] method.  Cursors are used
8050** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
8051** of the module.  Each module implementation will define
8052** the content of a cursor structure to suit its own needs.
8053**
8054** This superclass exists in order to define fields of the cursor that
8055** are common to all implementations.
8056*/
8057struct sqlite3_vtab_cursor {
8058  sqlite3_vtab *pVtab;      /* Virtual table of this cursor */
8059  /* Virtual table implementations will typically add additional fields */
8060};
8061
8062/*
8063** CAPI3REF: Declare The Schema Of A Virtual Table
8064**
8065** ^The [xCreate] and [xConnect] methods of a
8066** [virtual table module] call this interface
8067** to declare the format (the names and datatypes of the columns) of
8068** the virtual tables they implement.
8069*/
8070SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
8071
8072/*
8073** CAPI3REF: Overload A Function For A Virtual Table
8074** METHOD: sqlite3
8075**
8076** ^(Virtual tables can provide alternative implementations of functions
8077** using the [xFindFunction] method of the [virtual table module].
8078** But global versions of those functions
8079** must exist in order to be overloaded.)^
8080**
8081** ^(This API makes sure a global version of a function with a particular
8082** name and number of parameters exists.  If no such function exists
8083** before this API is called, a new function is created.)^  ^The implementation
8084** of the new function always causes an exception to be thrown.  So
8085** the new function is not good for anything by itself.  Its only
8086** purpose is to be a placeholder function that can be overloaded
8087** by a [virtual table].
8088*/
8089SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
8090
8091/*
8092** The interface to the virtual-table mechanism defined above (back up
8093** to a comment remarkably similar to this one) is currently considered
8094** to be experimental.  The interface might change in incompatible ways.
8095** If this is a problem for you, do not use the interface at this time.
8096**
8097** When the virtual-table mechanism stabilizes, we will declare the
8098** interface fixed, support it indefinitely, and remove this comment.
8099*/
8100
8101/*
8102** CAPI3REF: A Handle To An Open BLOB
8103** KEYWORDS: {BLOB handle} {BLOB handles}
8104**
8105** An instance of this object represents an open BLOB on which
8106** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
8107** ^Objects of this type are created by [sqlite3_blob_open()]
8108** and destroyed by [sqlite3_blob_close()].
8109** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
8110** can be used to read or write small subsections of the BLOB.
8111** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
8112*/
8113typedef struct sqlite3_blob sqlite3_blob;
8114
8115/*
8116** CAPI3REF: Open A BLOB For Incremental I/O
8117** METHOD: sqlite3
8118** CONSTRUCTOR: sqlite3_blob
8119**
8120** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
8121** in row iRow, column zColumn, table zTable in database zDb;
8122** in other words, the same BLOB that would be selected by:
8123**
8124** <pre>
8125**     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
8126** </pre>)^
8127**
8128** ^(Parameter zDb is not the filename that contains the database, but
8129** rather the symbolic name of the database. For attached databases, this is
8130** the name that appears after the AS keyword in the [ATTACH] statement.
8131** For the main database file, the database name is "main". For TEMP
8132** tables, the database name is "temp".)^
8133**
8134** ^If the flags parameter is non-zero, then the BLOB is opened for read
8135** and write access. ^If the flags parameter is zero, the BLOB is opened for
8136** read-only access.
8137**
8138** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored
8139** in *ppBlob. Otherwise an [error code] is returned and, unless the error
8140** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided
8141** the API is not misused, it is always safe to call [sqlite3_blob_close()]
8142** on *ppBlob after this function it returns.
8143**
8144** This function fails with SQLITE_ERROR if any of the following are true:
8145** <ul>
8146**   <li> ^(Database zDb does not exist)^,
8147**   <li> ^(Table zTable does not exist within database zDb)^,
8148**   <li> ^(Table zTable is a WITHOUT ROWID table)^,
8149**   <li> ^(Column zColumn does not exist)^,
8150**   <li> ^(Row iRow is not present in the table)^,
8151**   <li> ^(The specified column of row iRow contains a value that is not
8152**         a TEXT or BLOB value)^,
8153**   <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE
8154**         constraint and the blob is being opened for read/write access)^,
8155**   <li> ^([foreign key constraints | Foreign key constraints] are enabled,
8156**         column zColumn is part of a [child key] definition and the blob is
8157**         being opened for read/write access)^.
8158** </ul>
8159**
8160** ^Unless it returns SQLITE_MISUSE, this function sets the
8161** [database connection] error code and message accessible via
8162** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
8163**
8164** A BLOB referenced by sqlite3_blob_open() may be read using the
8165** [sqlite3_blob_read()] interface and modified by using
8166** [sqlite3_blob_write()].  The [BLOB handle] can be moved to a
8167** different row of the same table using the [sqlite3_blob_reopen()]
8168** interface.  However, the column, table, or database of a [BLOB handle]
8169** cannot be changed after the [BLOB handle] is opened.
8170**
8171** ^(If the row that a BLOB handle points to is modified by an
8172** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
8173** then the BLOB handle is marked as "expired".
8174** This is true if any column of the row is changed, even a column
8175** other than the one the BLOB handle is open on.)^
8176** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
8177** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
8178** ^(Changes written into a BLOB prior to the BLOB expiring are not
8179** rolled back by the expiration of the BLOB.  Such changes will eventually
8180** commit if the transaction continues to completion.)^
8181**
8182** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
8183** the opened blob.  ^The size of a blob may not be changed by this
8184** interface.  Use the [UPDATE] SQL command to change the size of a
8185** blob.
8186**
8187** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
8188** and the built-in [zeroblob] SQL function may be used to create a
8189** zero-filled blob to read or write using the incremental-blob interface.
8190**
8191** To avoid a resource leak, every open [BLOB handle] should eventually
8192** be released by a call to [sqlite3_blob_close()].
8193**
8194** See also: [sqlite3_blob_close()],
8195** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
8196** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
8197*/
8198SQLITE_API int sqlite3_blob_open(
8199  sqlite3*,
8200  const char *zDb,
8201  const char *zTable,
8202  const char *zColumn,
8203  sqlite3_int64 iRow,
8204  int flags,
8205  sqlite3_blob **ppBlob
8206);
8207
8208/*
8209** CAPI3REF: Move a BLOB Handle to a New Row
8210** METHOD: sqlite3_blob
8211**
8212** ^This function is used to move an existing [BLOB handle] so that it points
8213** to a different row of the same database table. ^The new row is identified
8214** by the rowid value passed as the second argument. Only the row can be
8215** changed. ^The database, table and column on which the blob handle is open
8216** remain the same. Moving an existing [BLOB handle] to a new row is
8217** faster than closing the existing handle and opening a new one.
8218**
8219** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
8220** it must exist and there must be either a blob or text value stored in
8221** the nominated column.)^ ^If the new row is not present in the table, or if
8222** it does not contain a blob or text value, or if another error occurs, an
8223** SQLite error code is returned and the blob handle is considered aborted.
8224** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
8225** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
8226** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
8227** always returns zero.
8228**
8229** ^This function sets the database handle error code and message.
8230*/
8231SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
8232
8233/*
8234** CAPI3REF: Close A BLOB Handle
8235** DESTRUCTOR: sqlite3_blob
8236**
8237** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
8238** unconditionally.  Even if this routine returns an error code, the
8239** handle is still closed.)^
8240**
8241** ^If the blob handle being closed was opened for read-write access, and if
8242** the database is in auto-commit mode and there are no other open read-write
8243** blob handles or active write statements, the current transaction is
8244** committed. ^If an error occurs while committing the transaction, an error
8245** code is returned and the transaction rolled back.
8246**
8247** Calling this function with an argument that is not a NULL pointer or an
8248** open blob handle results in undefined behaviour. ^Calling this routine
8249** with a null pointer (such as would be returned by a failed call to
8250** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
8251** is passed a valid open blob handle, the values returned by the
8252** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
8253*/
8254SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
8255
8256/*
8257** CAPI3REF: Return The Size Of An Open BLOB
8258** METHOD: sqlite3_blob
8259**
8260** ^Returns the size in bytes of the BLOB accessible via the
8261** successfully opened [BLOB handle] in its only argument.  ^The
8262** incremental blob I/O routines can only read or overwriting existing
8263** blob content; they cannot change the size of a blob.
8264**
8265** This routine only works on a [BLOB handle] which has been created
8266** by a prior successful call to [sqlite3_blob_open()] and which has not
8267** been closed by [sqlite3_blob_close()].  Passing any other pointer in
8268** to this routine results in undefined and probably undesirable behavior.
8269*/
8270SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
8271
8272/*
8273** CAPI3REF: Read Data From A BLOB Incrementally
8274** METHOD: sqlite3_blob
8275**
8276** ^(This function is used to read data from an open [BLOB handle] into a
8277** caller-supplied buffer. N bytes of data are copied into buffer Z
8278** from the open BLOB, starting at offset iOffset.)^
8279**
8280** ^If offset iOffset is less than N bytes from the end of the BLOB,
8281** [SQLITE_ERROR] is returned and no data is read.  ^If N or iOffset is
8282** less than zero, [SQLITE_ERROR] is returned and no data is read.
8283** ^The size of the blob (and hence the maximum value of N+iOffset)
8284** can be determined using the [sqlite3_blob_bytes()] interface.
8285**
8286** ^An attempt to read from an expired [BLOB handle] fails with an
8287** error code of [SQLITE_ABORT].
8288**
8289** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
8290** Otherwise, an [error code] or an [extended error code] is returned.)^
8291**
8292** This routine only works on a [BLOB handle] which has been created
8293** by a prior successful call to [sqlite3_blob_open()] and which has not
8294** been closed by [sqlite3_blob_close()].  Passing any other pointer in
8295** to this routine results in undefined and probably undesirable behavior.
8296**
8297** See also: [sqlite3_blob_write()].
8298*/
8299SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
8300
8301/*
8302** CAPI3REF: Write Data Into A BLOB Incrementally
8303** METHOD: sqlite3_blob
8304**
8305** ^(This function is used to write data into an open [BLOB handle] from a
8306** caller-supplied buffer. N bytes of data are copied from the buffer Z
8307** into the open BLOB, starting at offset iOffset.)^
8308**
8309** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
8310** Otherwise, an  [error code] or an [extended error code] is returned.)^
8311** ^Unless SQLITE_MISUSE is returned, this function sets the
8312** [database connection] error code and message accessible via
8313** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
8314**
8315** ^If the [BLOB handle] passed as the first argument was not opened for
8316** writing (the flags parameter to [sqlite3_blob_open()] was zero),
8317** this function returns [SQLITE_READONLY].
8318**
8319** This function may only modify the contents of the BLOB; it is
8320** not possible to increase the size of a BLOB using this API.
8321** ^If offset iOffset is less than N bytes from the end of the BLOB,
8322** [SQLITE_ERROR] is returned and no data is written. The size of the
8323** BLOB (and hence the maximum value of N+iOffset) can be determined
8324** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less
8325** than zero [SQLITE_ERROR] is returned and no data is written.
8326**
8327** ^An attempt to write to an expired [BLOB handle] fails with an
8328** error code of [SQLITE_ABORT].  ^Writes to the BLOB that occurred
8329** before the [BLOB handle] expired are not rolled back by the
8330** expiration of the handle, though of course those changes might
8331** have been overwritten by the statement that expired the BLOB handle
8332** or by other independent statements.
8333**
8334** This routine only works on a [BLOB handle] which has been created
8335** by a prior successful call to [sqlite3_blob_open()] and which has not
8336** been closed by [sqlite3_blob_close()].  Passing any other pointer in
8337** to this routine results in undefined and probably undesirable behavior.
8338**
8339** See also: [sqlite3_blob_read()].
8340*/
8341SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
8342
8343/*
8344** CAPI3REF: Virtual File System Objects
8345**
8346** A virtual filesystem (VFS) is an [sqlite3_vfs] object
8347** that SQLite uses to interact
8348** with the underlying operating system.  Most SQLite builds come with a
8349** single default VFS that is appropriate for the host computer.
8350** New VFSes can be registered and existing VFSes can be unregistered.
8351** The following interfaces are provided.
8352**
8353** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
8354** ^Names are case sensitive.
8355** ^Names are zero-terminated UTF-8 strings.
8356** ^If there is no match, a NULL pointer is returned.
8357** ^If zVfsName is NULL then the default VFS is returned.
8358**
8359** ^New VFSes are registered with sqlite3_vfs_register().
8360** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
8361** ^The same VFS can be registered multiple times without injury.
8362** ^To make an existing VFS into the default VFS, register it again
8363** with the makeDflt flag set.  If two different VFSes with the
8364** same name are registered, the behavior is undefined.  If a
8365** VFS is registered with a name that is NULL or an empty string,
8366** then the behavior is undefined.
8367**
8368** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
8369** ^(If the default VFS is unregistered, another VFS is chosen as
8370** the default.  The choice for the new VFS is arbitrary.)^
8371*/
8372SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
8373SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
8374SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
8375
8376/*
8377** CAPI3REF: Mutexes
8378**
8379** The SQLite core uses these routines for thread
8380** synchronization. Though they are intended for internal
8381** use by SQLite, code that links against SQLite is
8382** permitted to use any of these routines.
8383**
8384** The SQLite source code contains multiple implementations
8385** of these mutex routines.  An appropriate implementation
8386** is selected automatically at compile-time.  The following
8387** implementations are available in the SQLite core:
8388**
8389** <ul>
8390** <li>   SQLITE_MUTEX_PTHREADS
8391** <li>   SQLITE_MUTEX_W32
8392** <li>   SQLITE_MUTEX_NOOP
8393** </ul>
8394**
8395** The SQLITE_MUTEX_NOOP implementation is a set of routines
8396** that does no real locking and is appropriate for use in
8397** a single-threaded application.  The SQLITE_MUTEX_PTHREADS and
8398** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
8399** and Windows.
8400**
8401** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
8402** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
8403** implementation is included with the library. In this case the
8404** application must supply a custom mutex implementation using the
8405** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
8406** before calling sqlite3_initialize() or any other public sqlite3_
8407** function that calls sqlite3_initialize().
8408**
8409** ^The sqlite3_mutex_alloc() routine allocates a new
8410** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc()
8411** routine returns NULL if it is unable to allocate the requested
8412** mutex.  The argument to sqlite3_mutex_alloc() must one of these
8413** integer constants:
8414**
8415** <ul>
8416** <li>  SQLITE_MUTEX_FAST
8417** <li>  SQLITE_MUTEX_RECURSIVE
8418** <li>  SQLITE_MUTEX_STATIC_MASTER
8419** <li>  SQLITE_MUTEX_STATIC_MEM
8420** <li>  SQLITE_MUTEX_STATIC_OPEN
8421** <li>  SQLITE_MUTEX_STATIC_PRNG
8422** <li>  SQLITE_MUTEX_STATIC_LRU
8423** <li>  SQLITE_MUTEX_STATIC_PMEM
8424** <li>  SQLITE_MUTEX_STATIC_APP1
8425** <li>  SQLITE_MUTEX_STATIC_APP2
8426** <li>  SQLITE_MUTEX_STATIC_APP3
8427** <li>  SQLITE_MUTEX_STATIC_VFS1
8428** <li>  SQLITE_MUTEX_STATIC_VFS2
8429** <li>  SQLITE_MUTEX_STATIC_VFS3
8430** </ul>
8431**
8432** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
8433** cause sqlite3_mutex_alloc() to create
8434** a new mutex.  ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
8435** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
8436** The mutex implementation does not need to make a distinction
8437** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
8438** not want to.  SQLite will only request a recursive mutex in
8439** cases where it really needs one.  If a faster non-recursive mutex
8440** implementation is available on the host platform, the mutex subsystem
8441** might return such a mutex in response to SQLITE_MUTEX_FAST.
8442**
8443** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
8444** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
8445** a pointer to a static preexisting mutex.  ^Nine static mutexes are
8446** used by the current version of SQLite.  Future versions of SQLite
8447** may add additional static mutexes.  Static mutexes are for internal
8448** use by SQLite only.  Applications that use SQLite mutexes should
8449** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
8450** SQLITE_MUTEX_RECURSIVE.
8451**
8452** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
8453** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
8454** returns a different mutex on every call.  ^For the static
8455** mutex types, the same mutex is returned on every call that has
8456** the same type number.
8457**
8458** ^The sqlite3_mutex_free() routine deallocates a previously
8459** allocated dynamic mutex.  Attempting to deallocate a static
8460** mutex results in undefined behavior.
8461**
8462** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
8463** to enter a mutex.  ^If another thread is already within the mutex,
8464** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
8465** SQLITE_BUSY.  ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
8466** upon successful entry.  ^(Mutexes created using
8467** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
8468** In such cases, the
8469** mutex must be exited an equal number of times before another thread
8470** can enter.)^  If the same thread tries to enter any mutex other
8471** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
8472**
8473** ^(Some systems (for example, Windows 95) do not support the operation
8474** implemented by sqlite3_mutex_try().  On those systems, sqlite3_mutex_try()
8475** will always return SQLITE_BUSY. The SQLite core only ever uses
8476** sqlite3_mutex_try() as an optimization so this is acceptable
8477** behavior.)^
8478**
8479** ^The sqlite3_mutex_leave() routine exits a mutex that was
8480** previously entered by the same thread.   The behavior
8481** is undefined if the mutex is not currently entered by the
8482** calling thread or is not currently allocated.
8483**
8484** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
8485** sqlite3_mutex_leave() is a NULL pointer, then all three routines
8486** behave as no-ops.
8487**
8488** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
8489*/
8490SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
8491SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
8492SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
8493SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
8494SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
8495
8496/*
8497** CAPI3REF: Mutex Methods Object
8498**
8499** An instance of this structure defines the low-level routines
8500** used to allocate and use mutexes.
8501**
8502** Usually, the default mutex implementations provided by SQLite are
8503** sufficient, however the application has the option of substituting a custom
8504** implementation for specialized deployments or systems for which SQLite
8505** does not provide a suitable implementation. In this case, the application
8506** creates and populates an instance of this structure to pass
8507** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
8508** Additionally, an instance of this structure can be used as an
8509** output variable when querying the system for the current mutex
8510** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
8511**
8512** ^The xMutexInit method defined by this structure is invoked as
8513** part of system initialization by the sqlite3_initialize() function.
8514** ^The xMutexInit routine is called by SQLite exactly once for each
8515** effective call to [sqlite3_initialize()].
8516**
8517** ^The xMutexEnd method defined by this structure is invoked as
8518** part of system shutdown by the sqlite3_shutdown() function. The
8519** implementation of this method is expected to release all outstanding
8520** resources obtained by the mutex methods implementation, especially
8521** those obtained by the xMutexInit method.  ^The xMutexEnd()
8522** interface is invoked exactly once for each call to [sqlite3_shutdown()].
8523**
8524** ^(The remaining seven methods defined by this structure (xMutexAlloc,
8525** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
8526** xMutexNotheld) implement the following interfaces (respectively):
8527**
8528** <ul>
8529**   <li>  [sqlite3_mutex_alloc()] </li>
8530**   <li>  [sqlite3_mutex_free()] </li>
8531**   <li>  [sqlite3_mutex_enter()] </li>
8532**   <li>  [sqlite3_mutex_try()] </li>
8533**   <li>  [sqlite3_mutex_leave()] </li>
8534**   <li>  [sqlite3_mutex_held()] </li>
8535**   <li>  [sqlite3_mutex_notheld()] </li>
8536** </ul>)^
8537**
8538** The only difference is that the public sqlite3_XXX functions enumerated
8539** above silently ignore any invocations that pass a NULL pointer instead
8540** of a valid mutex handle. The implementations of the methods defined
8541** by this structure are not required to handle this case. The results
8542** of passing a NULL pointer instead of a valid mutex handle are undefined
8543** (i.e. it is acceptable to provide an implementation that segfaults if
8544** it is passed a NULL pointer).
8545**
8546** The xMutexInit() method must be threadsafe.  It must be harmless to
8547** invoke xMutexInit() multiple times within the same process and without
8548** intervening calls to xMutexEnd().  Second and subsequent calls to
8549** xMutexInit() must be no-ops.
8550**
8551** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
8552** and its associates).  Similarly, xMutexAlloc() must not use SQLite memory
8553** allocation for a static mutex.  ^However xMutexAlloc() may use SQLite
8554** memory allocation for a fast or recursive mutex.
8555**
8556** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
8557** called, but only if the prior call to xMutexInit returned SQLITE_OK.
8558** If xMutexInit fails in any way, it is expected to clean up after itself
8559** prior to returning.
8560*/
8561typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
8562struct sqlite3_mutex_methods {
8563  int (*xMutexInit)(void);
8564  int (*xMutexEnd)(void);
8565  sqlite3_mutex *(*xMutexAlloc)(int);
8566  void (*xMutexFree)(sqlite3_mutex *);
8567  void (*xMutexEnter)(sqlite3_mutex *);
8568  int (*xMutexTry)(sqlite3_mutex *);
8569  void (*xMutexLeave)(sqlite3_mutex *);
8570  int (*xMutexHeld)(sqlite3_mutex *);
8571  int (*xMutexNotheld)(sqlite3_mutex *);
8572};
8573
8574/*
8575** CAPI3REF: Mutex Verification Routines
8576**
8577** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
8578** are intended for use inside assert() statements.  The SQLite core
8579** never uses these routines except inside an assert() and applications
8580** are advised to follow the lead of the core.  The SQLite core only
8581** provides implementations for these routines when it is compiled
8582** with the SQLITE_DEBUG flag.  External mutex implementations
8583** are only required to provide these routines if SQLITE_DEBUG is
8584** defined and if NDEBUG is not defined.
8585**
8586** These routines should return true if the mutex in their argument
8587** is held or not held, respectively, by the calling thread.
8588**
8589** The implementation is not required to provide versions of these
8590** routines that actually work. If the implementation does not provide working
8591** versions of these routines, it should at least provide stubs that always
8592** return true so that one does not get spurious assertion failures.
8593**
8594** If the argument to sqlite3_mutex_held() is a NULL pointer then
8595** the routine should return 1.   This seems counter-intuitive since
8596** clearly the mutex cannot be held if it does not exist.  But
8597** the reason the mutex does not exist is because the build is not
8598** using mutexes.  And we do not want the assert() containing the
8599** call to sqlite3_mutex_held() to fail, so a non-zero return is
8600** the appropriate thing to do.  The sqlite3_mutex_notheld()
8601** interface should also return 1 when given a NULL pointer.
8602*/
8603#ifndef NDEBUG
8604SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
8605SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
8606#endif
8607
8608/*
8609** CAPI3REF: Mutex Types
8610**
8611** The [sqlite3_mutex_alloc()] interface takes a single argument
8612** which is one of these integer constants.
8613**
8614** The set of static mutexes may change from one SQLite release to the
8615** next.  Applications that override the built-in mutex logic must be
8616** prepared to accommodate additional static mutexes.
8617*/
8618#define SQLITE_MUTEX_FAST             0
8619#define SQLITE_MUTEX_RECURSIVE        1
8620#define SQLITE_MUTEX_STATIC_MASTER    2
8621#define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
8622#define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
8623#define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
8624#define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_randomness() */
8625#define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
8626#define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
8627#define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
8628#define SQLITE_MUTEX_STATIC_APP1      8  /* For use by application */
8629#define SQLITE_MUTEX_STATIC_APP2      9  /* For use by application */
8630#define SQLITE_MUTEX_STATIC_APP3     10  /* For use by application */
8631#define SQLITE_MUTEX_STATIC_VFS1     11  /* For use by built-in VFS */
8632#define SQLITE_MUTEX_STATIC_VFS2     12  /* For use by extension VFS */
8633#define SQLITE_MUTEX_STATIC_VFS3     13  /* For use by application VFS */
8634
8635/*
8636** CAPI3REF: Retrieve the mutex for a database connection
8637** METHOD: sqlite3
8638**
8639** ^This interface returns a pointer the [sqlite3_mutex] object that
8640** serializes access to the [database connection] given in the argument
8641** when the [threading mode] is Serialized.
8642** ^If the [threading mode] is Single-thread or Multi-thread then this
8643** routine returns a NULL pointer.
8644*/
8645SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
8646
8647/*
8648** CAPI3REF: Low-Level Control Of Database Files
8649** METHOD: sqlite3
8650** KEYWORDS: {file control}
8651**
8652** ^The [sqlite3_file_control()] interface makes a direct call to the
8653** xFileControl method for the [sqlite3_io_methods] object associated
8654** with a particular database identified by the second argument. ^The
8655** name of the database is "main" for the main database or "temp" for the
8656** TEMP database, or the name that appears after the AS keyword for
8657** databases that are added using the [ATTACH] SQL command.
8658** ^A NULL pointer can be used in place of "main" to refer to the
8659** main database file.
8660** ^The third and fourth parameters to this routine
8661** are passed directly through to the second and third parameters of
8662** the xFileControl method.  ^The return value of the xFileControl
8663** method becomes the return value of this routine.
8664**
8665** A few opcodes for [sqlite3_file_control()] are handled directly
8666** by the SQLite core and never invoke the
8667** sqlite3_io_methods.xFileControl method.
8668** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes
8669** a pointer to the underlying [sqlite3_file] object to be written into
8670** the space pointed to by the 4th parameter.  The
8671** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns
8672** the [sqlite3_file] object associated with the journal file instead of
8673** the main database.  The [SQLITE_FCNTL_VFS_POINTER] opcode returns
8674** a pointer to the underlying [sqlite3_vfs] object for the file.
8675** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter
8676** from the pager.
8677**
8678** ^If the second parameter (zDbName) does not match the name of any
8679** open database file, then SQLITE_ERROR is returned.  ^This error
8680** code is not remembered and will not be recalled by [sqlite3_errcode()]
8681** or [sqlite3_errmsg()].  The underlying xFileControl method might
8682** also return SQLITE_ERROR.  There is no way to distinguish between
8683** an incorrect zDbName and an SQLITE_ERROR return from the underlying
8684** xFileControl method.
8685**
8686** See also: [file control opcodes]
8687*/
8688SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
8689
8690/*
8691** CAPI3REF: Testing Interface
8692**
8693** ^The sqlite3_test_control() interface is used to read out internal
8694** state of SQLite and to inject faults into SQLite for testing
8695** purposes.  ^The first parameter is an operation code that determines
8696** the number, meaning, and operation of all subsequent parameters.
8697**
8698** This interface is not for use by applications.  It exists solely
8699** for verifying the correct operation of the SQLite library.  Depending
8700** on how the SQLite library is compiled, this interface might not exist.
8701**
8702** The details of the operation codes, their meanings, the parameters
8703** they take, and what they do are all subject to change without notice.
8704** Unlike most of the SQLite API, this function is not guaranteed to
8705** operate consistently from one release to the next.
8706*/
8707SQLITE_API int sqlite3_test_control(int op, ...);
8708
8709/*
8710** CAPI3REF: Testing Interface Operation Codes
8711**
8712** These constants are the valid operation code parameters used
8713** as the first argument to [sqlite3_test_control()].
8714**
8715** These parameters and their meanings are subject to change
8716** without notice.  These values are for testing purposes only.
8717** Applications should not use any of these parameters or the
8718** [sqlite3_test_control()] interface.
8719*/
8720#define SQLITE_TESTCTRL_FIRST                    5
8721#define SQLITE_TESTCTRL_PRNG_SAVE                5
8722#define SQLITE_TESTCTRL_PRNG_RESTORE             6
8723#define SQLITE_TESTCTRL_PRNG_RESET               7  /* NOT USED */
8724#define SQLITE_TESTCTRL_BITVEC_TEST              8
8725#define SQLITE_TESTCTRL_FAULT_INSTALL            9
8726#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
8727#define SQLITE_TESTCTRL_PENDING_BYTE            11
8728#define SQLITE_TESTCTRL_ASSERT                  12
8729#define SQLITE_TESTCTRL_ALWAYS                  13
8730#define SQLITE_TESTCTRL_RESERVE                 14  /* NOT USED */
8731#define SQLITE_TESTCTRL_OPTIMIZATIONS           15
8732#define SQLITE_TESTCTRL_ISKEYWORD               16  /* NOT USED */
8733#define SQLITE_TESTCTRL_SCRATCHMALLOC           17  /* NOT USED */
8734#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS      17
8735#define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
8736#define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
8737#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
8738#define SQLITE_TESTCTRL_NEVER_CORRUPT           20
8739#define SQLITE_TESTCTRL_VDBE_COVERAGE           21
8740#define SQLITE_TESTCTRL_BYTEORDER               22
8741#define SQLITE_TESTCTRL_ISINIT                  23
8742#define SQLITE_TESTCTRL_SORTER_MMAP             24
8743#define SQLITE_TESTCTRL_IMPOSTER                25
8744#define SQLITE_TESTCTRL_PARSER_COVERAGE         26
8745#define SQLITE_TESTCTRL_RESULT_INTREAL          27
8746#define SQLITE_TESTCTRL_PRNG_SEED               28
8747#define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS     29
8748#define SQLITE_TESTCTRL_LAST                    29  /* Largest TESTCTRL */
8749
8750/*
8751** CAPI3REF: SQL Keyword Checking
8752**
8753** These routines provide access to the set of SQL language keywords
8754** recognized by SQLite.  Applications can uses these routines to determine
8755** whether or not a specific identifier needs to be escaped (for example,
8756** by enclosing in double-quotes) so as not to confuse the parser.
8757**
8758** The sqlite3_keyword_count() interface returns the number of distinct
8759** keywords understood by SQLite.
8760**
8761** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
8762** makes *Z point to that keyword expressed as UTF8 and writes the number
8763** of bytes in the keyword into *L.  The string that *Z points to is not
8764** zero-terminated.  The sqlite3_keyword_name(N,Z,L) routine returns
8765** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z
8766** or L are NULL or invalid pointers then calls to
8767** sqlite3_keyword_name(N,Z,L) result in undefined behavior.
8768**
8769** The sqlite3_keyword_check(Z,L) interface checks to see whether or not
8770** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8771** if it is and zero if not.
8772**
8773** The parser used by SQLite is forgiving.  It is often possible to use
8774** a keyword as an identifier as long as such use does not result in a
8775** parsing ambiguity.  For example, the statement
8776** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and
8777** creates a new table named "BEGIN" with three columns named
8778** "REPLACE", "PRAGMA", and "END".  Nevertheless, best practice is to avoid
8779** using keywords as identifiers.  Common techniques used to avoid keyword
8780** name collisions include:
8781** <ul>
8782** <li> Put all identifier names inside double-quotes.  This is the official
8783**      SQL way to escape identifier names.
8784** <li> Put identifier names inside &#91;...&#93;.  This is not standard SQL,
8785**      but it is what SQL Server does and so lots of programmers use this
8786**      technique.
8787** <li> Begin every identifier with the letter "Z" as no SQL keywords start
8788**      with "Z".
8789** <li> Include a digit somewhere in every identifier name.
8790** </ul>
8791**
8792** Note that the number of keywords understood by SQLite can depend on
8793** compile-time options.  For example, "VACUUM" is not a keyword if
8794** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option.  Also,
8795** new keywords may be added to future releases of SQLite.
8796*/
8797SQLITE_API int sqlite3_keyword_count(void);
8798SQLITE_API int sqlite3_keyword_name(int,const char**,int*);
8799SQLITE_API int sqlite3_keyword_check(const char*,int);
8800
8801/*
8802** CAPI3REF: Dynamic String Object
8803** KEYWORDS: {dynamic string}
8804**
8805** An instance of the sqlite3_str object contains a dynamically-sized
8806** string under construction.
8807**
8808** The lifecycle of an sqlite3_str object is as follows:
8809** <ol>
8810** <li> ^The sqlite3_str object is created using [sqlite3_str_new()].
8811** <li> ^Text is appended to the sqlite3_str object using various
8812** methods, such as [sqlite3_str_appendf()].
8813** <li> ^The sqlite3_str object is destroyed and the string it created
8814** is returned using the [sqlite3_str_finish()] interface.
8815** </ol>
8816*/
8817typedef struct sqlite3_str sqlite3_str;
8818
8819/*
8820** CAPI3REF: Create A New Dynamic String Object
8821** CONSTRUCTOR: sqlite3_str
8822**
8823** ^The [sqlite3_str_new(D)] interface allocates and initializes
8824** a new [sqlite3_str] object.  To avoid memory leaks, the object returned by
8825** [sqlite3_str_new()] must be freed by a subsequent call to
8826** [sqlite3_str_finish(X)].
8827**
8828** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
8829** valid [sqlite3_str] object, though in the event of an out-of-memory
8830** error the returned object might be a special singleton that will
8831** silently reject new text, always return SQLITE_NOMEM from
8832** [sqlite3_str_errcode()], always return 0 for
8833** [sqlite3_str_length()], and always return NULL from
8834** [sqlite3_str_finish(X)].  It is always safe to use the value
8835** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
8836** to any of the other [sqlite3_str] methods.
8837**
8838** The D parameter to [sqlite3_str_new(D)] may be NULL.  If the
8839** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
8840** length of the string contained in the [sqlite3_str] object will be
8841** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
8842** of [SQLITE_MAX_LENGTH].
8843*/
8844SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*);
8845
8846/*
8847** CAPI3REF: Finalize A Dynamic String
8848** DESTRUCTOR: sqlite3_str
8849**
8850** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X
8851** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()]
8852** that contains the constructed string.  The calling application should
8853** pass the returned value to [sqlite3_free()] to avoid a memory leak.
8854** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any
8855** errors were encountered during construction of the string.  ^The
8856** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the
8857** string in [sqlite3_str] object X is zero bytes long.
8858*/
8859SQLITE_API char *sqlite3_str_finish(sqlite3_str*);
8860
8861/*
8862** CAPI3REF: Add Content To A Dynamic String
8863** METHOD: sqlite3_str
8864**
8865** These interfaces add content to an sqlite3_str object previously obtained
8866** from [sqlite3_str_new()].
8867**
8868** ^The [sqlite3_str_appendf(X,F,...)] and
8869** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8870** functionality of SQLite to append formatted text onto the end of
8871** [sqlite3_str] object X.
8872**
8873** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S
8874** onto the end of the [sqlite3_str] object X.  N must be non-negative.
8875** S must contain at least N non-zero bytes of content.  To append a
8876** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8877** method instead.
8878**
8879** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of
8880** zero-terminated string S onto the end of [sqlite3_str] object X.
8881**
8882** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the
8883** single-byte character C onto the end of [sqlite3_str] object X.
8884** ^This method can be used, for example, to add whitespace indentation.
8885**
8886** ^The [sqlite3_str_reset(X)] method resets the string under construction
8887** inside [sqlite3_str] object X back to zero bytes in length.
8888**
8889** These methods do not return a result code.  ^If an error occurs, that fact
8890** is recorded in the [sqlite3_str] object and can be recovered by a
8891** subsequent call to [sqlite3_str_errcode(X)].
8892*/
8893SQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...);
8894SQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list);
8895SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
8896SQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn);
8897SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
8898SQLITE_API void sqlite3_str_reset(sqlite3_str*);
8899
8900/*
8901** CAPI3REF: Status Of A Dynamic String
8902** METHOD: sqlite3_str
8903**
8904** These interfaces return the current status of an [sqlite3_str] object.
8905**
8906** ^If any prior errors have occurred while constructing the dynamic string
8907** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return
8908** an appropriate error code.  ^The [sqlite3_str_errcode(X)] method returns
8909** [SQLITE_NOMEM] following any out-of-memory error, or
8910** [SQLITE_TOOBIG] if the size of the dynamic string exceeds
8911** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors.
8912**
8913** ^The [sqlite3_str_length(X)] method returns the current length, in bytes,
8914** of the dynamic string under construction in [sqlite3_str] object X.
8915** ^The length returned by [sqlite3_str_length(X)] does not include the
8916** zero-termination byte.
8917**
8918** ^The [sqlite3_str_value(X)] method returns a pointer to the current
8919** content of the dynamic string under construction in X.  The value
8920** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X
8921** and might be freed or altered by any subsequent method on the same
8922** [sqlite3_str] object.  Applications must not used the pointer returned
8923** [sqlite3_str_value(X)] after any subsequent method call on the same
8924** object.  ^Applications may change the content of the string returned
8925** by [sqlite3_str_value(X)] as long as they do not write into any bytes
8926** outside the range of 0 to [sqlite3_str_length(X)] and do not read or
8927** write any byte after any subsequent sqlite3_str method call.
8928*/
8929SQLITE_API int sqlite3_str_errcode(sqlite3_str*);
8930SQLITE_API int sqlite3_str_length(sqlite3_str*);
8931SQLITE_API char *sqlite3_str_value(sqlite3_str*);
8932
8933/*
8934** CAPI3REF: SQLite Runtime Status
8935**
8936** ^These interfaces are used to retrieve runtime status information
8937** about the performance of SQLite, and optionally to reset various
8938** highwater marks.  ^The first argument is an integer code for
8939** the specific parameter to measure.  ^(Recognized integer codes
8940** are of the form [status parameters | SQLITE_STATUS_...].)^
8941** ^The current value of the parameter is returned into *pCurrent.
8942** ^The highest recorded value is returned in *pHighwater.  ^If the
8943** resetFlag is true, then the highest record value is reset after
8944** *pHighwater is written.  ^(Some parameters do not record the highest
8945** value.  For those parameters
8946** nothing is written into *pHighwater and the resetFlag is ignored.)^
8947** ^(Other parameters record only the highwater mark and not the current
8948** value.  For these latter parameters nothing is written into *pCurrent.)^
8949**
8950** ^The sqlite3_status() and sqlite3_status64() routines return
8951** SQLITE_OK on success and a non-zero [error code] on failure.
8952**
8953** If either the current value or the highwater mark is too large to
8954** be represented by a 32-bit integer, then the values returned by
8955** sqlite3_status() are undefined.
8956**
8957** See also: [sqlite3_db_status()]
8958*/
8959SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
8960SQLITE_API int sqlite3_status64(
8961  int op,
8962  sqlite3_int64 *pCurrent,
8963  sqlite3_int64 *pHighwater,
8964  int resetFlag
8965);
8966
8967
8968/*
8969** CAPI3REF: Status Parameters
8970** KEYWORDS: {status parameters}
8971**
8972** These integer constants designate various run-time status parameters
8973** that can be returned by [sqlite3_status()].
8974**
8975** <dl>
8976** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
8977** <dd>This parameter is the current amount of memory checked out
8978** using [sqlite3_malloc()], either directly or indirectly.  The
8979** figure includes calls made to [sqlite3_malloc()] by the application
8980** and internal memory usage by the SQLite library.  Auxiliary page-cache
8981** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
8982** this parameter.  The amount returned is the sum of the allocation
8983** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
8984**
8985** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
8986** <dd>This parameter records the largest memory allocation request
8987** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
8988** internal equivalents).  Only the value returned in the
8989** *pHighwater parameter to [sqlite3_status()] is of interest.
8990** The value written into the *pCurrent parameter is undefined.</dd>)^
8991**
8992** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
8993** <dd>This parameter records the number of separate memory allocations
8994** currently checked out.</dd>)^
8995**
8996** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
8997** <dd>This parameter returns the number of pages used out of the
8998** [pagecache memory allocator] that was configured using
8999** [SQLITE_CONFIG_PAGECACHE].  The
9000** value returned is in pages, not in bytes.</dd>)^
9001**
9002** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
9003** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
9004** <dd>This parameter returns the number of bytes of page cache
9005** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
9006** buffer and where forced to overflow to [sqlite3_malloc()].  The
9007** returned value includes allocations that overflowed because they
9008** where too large (they were larger than the "sz" parameter to
9009** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
9010** no space was left in the page cache.</dd>)^
9011**
9012** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
9013** <dd>This parameter records the largest memory allocation request
9014** handed to the [pagecache memory allocator].  Only the value returned in the
9015** *pHighwater parameter to [sqlite3_status()] is of interest.
9016** The value written into the *pCurrent parameter is undefined.</dd>)^
9017**
9018** [[SQLITE_STATUS_SCRATCH_USED]] <dt>SQLITE_STATUS_SCRATCH_USED</dt>
9019** <dd>No longer used.</dd>
9020**
9021** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
9022** <dd>No longer used.</dd>
9023**
9024** [[SQLITE_STATUS_SCRATCH_SIZE]] <dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
9025** <dd>No longer used.</dd>
9026**
9027** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
9028** <dd>The *pHighwater parameter records the deepest parser stack.
9029** The *pCurrent value is undefined.  The *pHighwater value is only
9030** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
9031** </dl>
9032**
9033** New status parameters may be added from time to time.
9034*/
9035#define SQLITE_STATUS_MEMORY_USED          0
9036#define SQLITE_STATUS_PAGECACHE_USED       1
9037#define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
9038#define SQLITE_STATUS_SCRATCH_USED         3  /* NOT USED */
9039#define SQLITE_STATUS_SCRATCH_OVERFLOW     4  /* NOT USED */
9040#define SQLITE_STATUS_MALLOC_SIZE          5
9041#define SQLITE_STATUS_PARSER_STACK         6
9042#define SQLITE_STATUS_PAGECACHE_SIZE       7
9043#define SQLITE_STATUS_SCRATCH_SIZE         8  /* NOT USED */
9044#define SQLITE_STATUS_MALLOC_COUNT         9
9045
9046/*
9047** CAPI3REF: Database Connection Status
9048** METHOD: sqlite3
9049**
9050** ^This interface is used to retrieve runtime status information
9051** about a single [database connection].  ^The first argument is the
9052** database connection object to be interrogated.  ^The second argument
9053** is an integer constant, taken from the set of
9054** [SQLITE_DBSTATUS options], that
9055** determines the parameter to interrogate.  The set of
9056** [SQLITE_DBSTATUS options] is likely
9057** to grow in future releases of SQLite.
9058**
9059** ^The current value of the requested parameter is written into *pCur
9060** and the highest instantaneous value is written into *pHiwtr.  ^If
9061** the resetFlg is true, then the highest instantaneous value is
9062** reset back down to the current value.
9063**
9064** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
9065** non-zero [error code] on failure.
9066**
9067** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
9068*/
9069SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
9070
9071/*
9072** CAPI3REF: Status Parameters for database connections
9073** KEYWORDS: {SQLITE_DBSTATUS options}
9074**
9075** These constants are the available integer "verbs" that can be passed as
9076** the second argument to the [sqlite3_db_status()] interface.
9077**
9078** New verbs may be added in future releases of SQLite. Existing verbs
9079** might be discontinued. Applications should check the return code from
9080** [sqlite3_db_status()] to make sure that the call worked.
9081** The [sqlite3_db_status()] interface will return a non-zero error code
9082** if a discontinued or unsupported verb is invoked.
9083**
9084** <dl>
9085** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
9086** <dd>This parameter returns the number of lookaside memory slots currently
9087** checked out.</dd>)^
9088**
9089** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
9090** <dd>This parameter returns the number of malloc attempts that were
9091** satisfied using lookaside memory. Only the high-water value is meaningful;
9092** the current value is always zero.)^
9093**
9094** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
9095** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
9096** <dd>This parameter returns the number malloc attempts that might have
9097** been satisfied using lookaside memory but failed due to the amount of
9098** memory requested being larger than the lookaside slot size.
9099** Only the high-water value is meaningful;
9100** the current value is always zero.)^
9101**
9102** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
9103** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
9104** <dd>This parameter returns the number malloc attempts that might have
9105** been satisfied using lookaside memory but failed due to all lookaside
9106** memory already being in use.
9107** Only the high-water value is meaningful;
9108** the current value is always zero.)^
9109**
9110** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
9111** <dd>This parameter returns the approximate number of bytes of heap
9112** memory used by all pager caches associated with the database connection.)^
9113** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
9114**
9115** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]]
9116** ^(<dt>SQLITE_DBSTATUS_CACHE_USED_SHARED</dt>
9117** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a
9118** pager cache is shared between two or more connections the bytes of heap
9119** memory used by that pager cache is divided evenly between the attached
9120** connections.)^  In other words, if none of the pager caches associated
9121** with the database connection are shared, this request returns the same
9122** value as DBSTATUS_CACHE_USED. Or, if one or more or the pager caches are
9123** shared, the value returned by this call will be smaller than that returned
9124** by DBSTATUS_CACHE_USED. ^The highwater mark associated with
9125** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0.
9126**
9127** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
9128** <dd>This parameter returns the approximate number of bytes of heap
9129** memory used to store the schema for all databases associated
9130** with the connection - main, temp, and any [ATTACH]-ed databases.)^
9131** ^The full amount of memory used by the schemas is reported, even if the
9132** schema memory is shared with other database connections due to
9133** [shared cache mode] being enabled.
9134** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
9135**
9136** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
9137** <dd>This parameter returns the approximate number of bytes of heap
9138** and lookaside memory used by all prepared statements associated with
9139** the database connection.)^
9140** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
9141** </dd>
9142**
9143** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
9144** <dd>This parameter returns the number of pager cache hits that have
9145** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
9146** is always 0.
9147** </dd>
9148**
9149** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
9150** <dd>This parameter returns the number of pager cache misses that have
9151** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
9152** is always 0.
9153** </dd>
9154**
9155** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
9156** <dd>This parameter returns the number of dirty cache entries that have
9157** been written to disk. Specifically, the number of pages written to the
9158** wal file in wal mode databases, or the number of pages written to the
9159** database file in rollback mode databases. Any pages written as part of
9160** transaction rollback or database recovery operations are not included.
9161** If an IO or other error occurs while writing a page to disk, the effect
9162** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
9163** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
9164** </dd>
9165**
9166** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
9167** <dd>This parameter returns the number of dirty cache entries that have
9168** been written to disk in the middle of a transaction due to the page
9169** cache overflowing. Transactions are more efficient if they are written
9170** to disk all at once. When pages spill mid-transaction, that introduces
9171** additional overhead. This parameter can be used help identify
9172** inefficiencies that can be resolved by increasing the cache size.
9173** </dd>
9174**
9175** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
9176** <dd>This parameter returns zero for the current value if and only if
9177** all foreign key constraints (deferred or immediate) have been
9178** resolved.)^  ^The highwater mark is always 0.
9179** </dd>
9180** </dl>
9181*/
9182#define SQLITE_DBSTATUS_LOOKASIDE_USED       0
9183#define SQLITE_DBSTATUS_CACHE_USED           1
9184#define SQLITE_DBSTATUS_SCHEMA_USED          2
9185#define SQLITE_DBSTATUS_STMT_USED            3
9186#define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
9187#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
9188#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
9189#define SQLITE_DBSTATUS_CACHE_HIT            7
9190#define SQLITE_DBSTATUS_CACHE_MISS           8
9191#define SQLITE_DBSTATUS_CACHE_WRITE          9
9192#define SQLITE_DBSTATUS_DEFERRED_FKS        10
9193#define SQLITE_DBSTATUS_CACHE_USED_SHARED   11
9194#define SQLITE_DBSTATUS_CACHE_SPILL         12
9195#define SQLITE_DBSTATUS_MAX                 12   /* Largest defined DBSTATUS */
9196
9197
9198/*
9199** CAPI3REF: Prepared Statement Status
9200** METHOD: sqlite3_stmt
9201**
9202** ^(Each prepared statement maintains various
9203** [SQLITE_STMTSTATUS counters] that measure the number
9204** of times it has performed specific operations.)^  These counters can
9205** be used to monitor the performance characteristics of the prepared
9206** statements.  For example, if the number of table steps greatly exceeds
9207** the number of table searches or result rows, that would tend to indicate
9208** that the prepared statement is using a full table scan rather than
9209** an index.
9210**
9211** ^(This interface is used to retrieve and reset counter values from
9212** a [prepared statement].  The first argument is the prepared statement
9213** object to be interrogated.  The second argument
9214** is an integer code for a specific [SQLITE_STMTSTATUS counter]
9215** to be interrogated.)^
9216** ^The current value of the requested counter is returned.
9217** ^If the resetFlg is true, then the counter is reset to zero after this
9218** interface call returns.
9219**
9220** See also: [sqlite3_status()] and [sqlite3_db_status()].
9221*/
9222SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
9223
9224/*
9225** CAPI3REF: Status Parameters for prepared statements
9226** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
9227**
9228** These preprocessor macros define integer codes that name counter
9229** values associated with the [sqlite3_stmt_status()] interface.
9230** The meanings of the various counters are as follows:
9231**
9232** <dl>
9233** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
9234** <dd>^This is the number of times that SQLite has stepped forward in
9235** a table as part of a full table scan.  Large numbers for this counter
9236** may indicate opportunities for performance improvement through
9237** careful use of indices.</dd>
9238**
9239** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
9240** <dd>^This is the number of sort operations that have occurred.
9241** A non-zero value in this counter may indicate an opportunity to
9242** improvement performance through careful use of indices.</dd>
9243**
9244** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
9245** <dd>^This is the number of rows inserted into transient indices that
9246** were created automatically in order to help joins run faster.
9247** A non-zero value in this counter may indicate an opportunity to
9248** improvement performance by adding permanent indices that do not
9249** need to be reinitialized each time the statement is run.</dd>
9250**
9251** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
9252** <dd>^This is the number of virtual machine operations executed
9253** by the prepared statement if that number is less than or equal
9254** to 2147483647.  The number of virtual machine operations can be
9255** used as a proxy for the total work done by the prepared statement.
9256** If the number of virtual machine operations exceeds 2147483647
9257** then the value returned by this statement status code is undefined.
9258**
9259** [[SQLITE_STMTSTATUS_REPREPARE]] <dt>SQLITE_STMTSTATUS_REPREPARE</dt>
9260** <dd>^This is the number of times that the prepare statement has been
9261** automatically regenerated due to schema changes or changes to
9262** [bound parameters] that might affect the query plan.
9263**
9264** [[SQLITE_STMTSTATUS_RUN]] <dt>SQLITE_STMTSTATUS_RUN</dt>
9265** <dd>^This is the number of times that the prepared statement has
9266** been run.  A single "run" for the purposes of this counter is one
9267** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()].
9268** The counter is incremented on the first [sqlite3_step()] call of each
9269** cycle.
9270**
9271** [[SQLITE_STMTSTATUS_MEMUSED]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt>
9272** <dd>^This is the approximate number of bytes of heap memory
9273** used to store the prepared statement.  ^This value is not actually
9274** a counter, and so the resetFlg parameter to sqlite3_stmt_status()
9275** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED.
9276** </dd>
9277** </dl>
9278*/
9279#define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
9280#define SQLITE_STMTSTATUS_SORT              2
9281#define SQLITE_STMTSTATUS_AUTOINDEX         3
9282#define SQLITE_STMTSTATUS_VM_STEP           4
9283#define SQLITE_STMTSTATUS_REPREPARE         5
9284#define SQLITE_STMTSTATUS_RUN               6
9285#define SQLITE_STMTSTATUS_MEMUSED           99
9286
9287/*
9288** CAPI3REF: Custom Page Cache Object
9289**
9290** The sqlite3_pcache type is opaque.  It is implemented by
9291** the pluggable module.  The SQLite core has no knowledge of
9292** its size or internal structure and never deals with the
9293** sqlite3_pcache object except by holding and passing pointers
9294** to the object.
9295**
9296** See [sqlite3_pcache_methods2] for additional information.
9297*/
9298typedef struct sqlite3_pcache sqlite3_pcache;
9299
9300/*
9301** CAPI3REF: Custom Page Cache Object
9302**
9303** The sqlite3_pcache_page object represents a single page in the
9304** page cache.  The page cache will allocate instances of this
9305** object.  Various methods of the page cache use pointers to instances
9306** of this object as parameters or as their return value.
9307**
9308** See [sqlite3_pcache_methods2] for additional information.
9309*/
9310typedef struct sqlite3_pcache_page sqlite3_pcache_page;
9311struct sqlite3_pcache_page {
9312  void *pBuf;        /* The content of the page */
9313  void *pExtra;      /* Extra information associated with the page */
9314};
9315
9316/*
9317** CAPI3REF: Application Defined Page Cache.
9318** KEYWORDS: {page cache}
9319**
9320** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
9321** register an alternative page cache implementation by passing in an
9322** instance of the sqlite3_pcache_methods2 structure.)^
9323** In many applications, most of the heap memory allocated by
9324** SQLite is used for the page cache.
9325** By implementing a
9326** custom page cache using this API, an application can better control
9327** the amount of memory consumed by SQLite, the way in which
9328** that memory is allocated and released, and the policies used to
9329** determine exactly which parts of a database file are cached and for
9330** how long.
9331**
9332** The alternative page cache mechanism is an
9333** extreme measure that is only needed by the most demanding applications.
9334** The built-in page cache is recommended for most uses.
9335**
9336** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
9337** internal buffer by SQLite within the call to [sqlite3_config].  Hence
9338** the application may discard the parameter after the call to
9339** [sqlite3_config()] returns.)^
9340**
9341** [[the xInit() page cache method]]
9342** ^(The xInit() method is called once for each effective
9343** call to [sqlite3_initialize()])^
9344** (usually only once during the lifetime of the process). ^(The xInit()
9345** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
9346** The intent of the xInit() method is to set up global data structures
9347** required by the custom page cache implementation.
9348** ^(If the xInit() method is NULL, then the
9349** built-in default page cache is used instead of the application defined
9350** page cache.)^
9351**
9352** [[the xShutdown() page cache method]]
9353** ^The xShutdown() method is called by [sqlite3_shutdown()].
9354** It can be used to clean up
9355** any outstanding resources before process shutdown, if required.
9356** ^The xShutdown() method may be NULL.
9357**
9358** ^SQLite automatically serializes calls to the xInit method,
9359** so the xInit method need not be threadsafe.  ^The
9360** xShutdown method is only called from [sqlite3_shutdown()] so it does
9361** not need to be threadsafe either.  All other methods must be threadsafe
9362** in multithreaded applications.
9363**
9364** ^SQLite will never invoke xInit() more than once without an intervening
9365** call to xShutdown().
9366**
9367** [[the xCreate() page cache methods]]
9368** ^SQLite invokes the xCreate() method to construct a new cache instance.
9369** SQLite will typically create one cache instance for each open database file,
9370** though this is not guaranteed. ^The
9371** first parameter, szPage, is the size in bytes of the pages that must
9372** be allocated by the cache.  ^szPage will always a power of two.  ^The
9373** second parameter szExtra is a number of bytes of extra storage
9374** associated with each page cache entry.  ^The szExtra parameter will
9375** a number less than 250.  SQLite will use the
9376** extra szExtra bytes on each page to store metadata about the underlying
9377** database page on disk.  The value passed into szExtra depends
9378** on the SQLite version, the target platform, and how SQLite was compiled.
9379** ^The third argument to xCreate(), bPurgeable, is true if the cache being
9380** created will be used to cache database pages of a file stored on disk, or
9381** false if it is used for an in-memory database. The cache implementation
9382** does not have to do anything special based with the value of bPurgeable;
9383** it is purely advisory.  ^On a cache where bPurgeable is false, SQLite will
9384** never invoke xUnpin() except to deliberately delete a page.
9385** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
9386** false will always have the "discard" flag set to true.
9387** ^Hence, a cache created with bPurgeable false will
9388** never contain any unpinned pages.
9389**
9390** [[the xCachesize() page cache method]]
9391** ^(The xCachesize() method may be called at any time by SQLite to set the
9392** suggested maximum cache-size (number of pages stored by) the cache
9393** instance passed as the first argument. This is the value configured using
9394** the SQLite "[PRAGMA cache_size]" command.)^  As with the bPurgeable
9395** parameter, the implementation is not required to do anything with this
9396** value; it is advisory only.
9397**
9398** [[the xPagecount() page cache methods]]
9399** The xPagecount() method must return the number of pages currently
9400** stored in the cache, both pinned and unpinned.
9401**
9402** [[the xFetch() page cache methods]]
9403** The xFetch() method locates a page in the cache and returns a pointer to
9404** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
9405** The pBuf element of the returned sqlite3_pcache_page object will be a
9406** pointer to a buffer of szPage bytes used to store the content of a
9407** single database page.  The pExtra element of sqlite3_pcache_page will be
9408** a pointer to the szExtra bytes of extra storage that SQLite has requested
9409** for each entry in the page cache.
9410**
9411** The page to be fetched is determined by the key. ^The minimum key value
9412** is 1.  After it has been retrieved using xFetch, the page is considered
9413** to be "pinned".
9414**
9415** If the requested page is already in the page cache, then the page cache
9416** implementation must return a pointer to the page buffer with its content
9417** intact.  If the requested page is not already in the cache, then the
9418** cache implementation should use the value of the createFlag
9419** parameter to help it determined what action to take:
9420**
9421** <table border=1 width=85% align=center>
9422** <tr><th> createFlag <th> Behavior when page is not already in cache
9423** <tr><td> 0 <td> Do not allocate a new page.  Return NULL.
9424** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
9425**                 Otherwise return NULL.
9426** <tr><td> 2 <td> Make every effort to allocate a new page.  Only return
9427**                 NULL if allocating a new page is effectively impossible.
9428** </table>
9429**
9430** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1.  SQLite
9431** will only use a createFlag of 2 after a prior call with a createFlag of 1
9432** failed.)^  In between the xFetch() calls, SQLite may
9433** attempt to unpin one or more cache pages by spilling the content of
9434** pinned pages to disk and synching the operating system disk cache.
9435**
9436** [[the xUnpin() page cache method]]
9437** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
9438** as its second argument.  If the third parameter, discard, is non-zero,
9439** then the page must be evicted from the cache.
9440** ^If the discard parameter is
9441** zero, then the page may be discarded or retained at the discretion of
9442** page cache implementation. ^The page cache implementation
9443** may choose to evict unpinned pages at any time.
9444**
9445** The cache must not perform any reference counting. A single
9446** call to xUnpin() unpins the page regardless of the number of prior calls
9447** to xFetch().
9448**
9449** [[the xRekey() page cache methods]]
9450** The xRekey() method is used to change the key value associated with the
9451** page passed as the second argument. If the cache
9452** previously contains an entry associated with newKey, it must be
9453** discarded. ^Any prior cache entry associated with newKey is guaranteed not
9454** to be pinned.
9455**
9456** When SQLite calls the xTruncate() method, the cache must discard all
9457** existing cache entries with page numbers (keys) greater than or equal
9458** to the value of the iLimit parameter passed to xTruncate(). If any
9459** of these pages are pinned, they are implicitly unpinned, meaning that
9460** they can be safely discarded.
9461**
9462** [[the xDestroy() page cache method]]
9463** ^The xDestroy() method is used to delete a cache allocated by xCreate().
9464** All resources associated with the specified cache should be freed. ^After
9465** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
9466** handle invalid, and will not use it with any other sqlite3_pcache_methods2
9467** functions.
9468**
9469** [[the xShrink() page cache method]]
9470** ^SQLite invokes the xShrink() method when it wants the page cache to
9471** free up as much of heap memory as possible.  The page cache implementation
9472** is not obligated to free any memory, but well-behaved implementations should
9473** do their best.
9474*/
9475typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
9476struct sqlite3_pcache_methods2 {
9477  int iVersion;
9478  void *pArg;
9479  int (*xInit)(void*);
9480  void (*xShutdown)(void*);
9481  sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
9482  void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9483  int (*xPagecount)(sqlite3_pcache*);
9484  sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9485  void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
9486  void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
9487      unsigned oldKey, unsigned newKey);
9488  void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9489  void (*xDestroy)(sqlite3_pcache*);
9490  void (*xShrink)(sqlite3_pcache*);
9491};
9492
9493/*
9494** This is the obsolete pcache_methods object that has now been replaced
9495** by sqlite3_pcache_methods2.  This object is not used by SQLite.  It is
9496** retained in the header file for backwards compatibility only.
9497*/
9498typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
9499struct sqlite3_pcache_methods {
9500  void *pArg;
9501  int (*xInit)(void*);
9502  void (*xShutdown)(void*);
9503  sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
9504  void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9505  int (*xPagecount)(sqlite3_pcache*);
9506  void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9507  void (*xUnpin)(sqlite3_pcache*, void*, int discard);
9508  void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
9509  void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9510  void (*xDestroy)(sqlite3_pcache*);
9511};
9512
9513
9514/*
9515** CAPI3REF: Online Backup Object
9516**
9517** The sqlite3_backup object records state information about an ongoing
9518** online backup operation.  ^The sqlite3_backup object is created by
9519** a call to [sqlite3_backup_init()] and is destroyed by a call to
9520** [sqlite3_backup_finish()].
9521**
9522** See Also: [Using the SQLite Online Backup API]
9523*/
9524typedef struct sqlite3_backup sqlite3_backup;
9525
9526/*
9527** CAPI3REF: Online Backup API.
9528**
9529** The backup API copies the content of one database into another.
9530** It is useful either for creating backups of databases or
9531** for copying in-memory databases to or from persistent files.
9532**
9533** See Also: [Using the SQLite Online Backup API]
9534**
9535** ^SQLite holds a write transaction open on the destination database file
9536** for the duration of the backup operation.
9537** ^The source database is read-locked only while it is being read;
9538** it is not locked continuously for the entire backup operation.
9539** ^Thus, the backup may be performed on a live source database without
9540** preventing other database connections from
9541** reading or writing to the source database while the backup is underway.
9542**
9543** ^(To perform a backup operation:
9544**   <ol>
9545**     <li><b>sqlite3_backup_init()</b> is called once to initialize the
9546**         backup,
9547**     <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
9548**         the data between the two databases, and finally
9549**     <li><b>sqlite3_backup_finish()</b> is called to release all resources
9550**         associated with the backup operation.
9551**   </ol>)^
9552** There should be exactly one call to sqlite3_backup_finish() for each
9553** successful call to sqlite3_backup_init().
9554**
9555** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
9556**
9557** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
9558** [database connection] associated with the destination database
9559** and the database name, respectively.
9560** ^The database name is "main" for the main database, "temp" for the
9561** temporary database, or the name specified after the AS keyword in
9562** an [ATTACH] statement for an attached database.
9563** ^The S and M arguments passed to
9564** sqlite3_backup_init(D,N,S,M) identify the [database connection]
9565** and database name of the source database, respectively.
9566** ^The source and destination [database connections] (parameters S and D)
9567** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
9568** an error.
9569**
9570** ^A call to sqlite3_backup_init() will fail, returning NULL, if
9571** there is already a read or read-write transaction open on the
9572** destination database.
9573**
9574** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
9575** returned and an error code and error message are stored in the
9576** destination [database connection] D.
9577** ^The error code and message for the failed call to sqlite3_backup_init()
9578** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
9579** [sqlite3_errmsg16()] functions.
9580** ^A successful call to sqlite3_backup_init() returns a pointer to an
9581** [sqlite3_backup] object.
9582** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
9583** sqlite3_backup_finish() functions to perform the specified backup
9584** operation.
9585**
9586** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
9587**
9588** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
9589** the source and destination databases specified by [sqlite3_backup] object B.
9590** ^If N is negative, all remaining source pages are copied.
9591** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
9592** are still more pages to be copied, then the function returns [SQLITE_OK].
9593** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
9594** from source to destination, then it returns [SQLITE_DONE].
9595** ^If an error occurs while running sqlite3_backup_step(B,N),
9596** then an [error code] is returned. ^As well as [SQLITE_OK] and
9597** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
9598** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
9599** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
9600**
9601** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
9602** <ol>
9603** <li> the destination database was opened read-only, or
9604** <li> the destination database is using write-ahead-log journaling
9605** and the destination and source page sizes differ, or
9606** <li> the destination database is an in-memory database and the
9607** destination and source page sizes differ.
9608** </ol>)^
9609**
9610** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9611** the [sqlite3_busy_handler | busy-handler function]
9612** is invoked (if one is specified). ^If the
9613** busy-handler returns non-zero before the lock is available, then
9614** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
9615** sqlite3_backup_step() can be retried later. ^If the source
9616** [database connection]
9617** is being used to write to the source database when sqlite3_backup_step()
9618** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
9619** case the call to sqlite3_backup_step() can be retried later on. ^(If
9620** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
9621** [SQLITE_READONLY] is returned, then
9622** there is no point in retrying the call to sqlite3_backup_step(). These
9623** errors are considered fatal.)^  The application must accept
9624** that the backup operation has failed and pass the backup operation handle
9625** to the sqlite3_backup_finish() to release associated resources.
9626**
9627** ^The first call to sqlite3_backup_step() obtains an exclusive lock
9628** on the destination file. ^The exclusive lock is not released until either
9629** sqlite3_backup_finish() is called or the backup operation is complete
9630** and sqlite3_backup_step() returns [SQLITE_DONE].  ^Every call to
9631** sqlite3_backup_step() obtains a [shared lock] on the source database that
9632** lasts for the duration of the sqlite3_backup_step() call.
9633** ^Because the source database is not locked between calls to
9634** sqlite3_backup_step(), the source database may be modified mid-way
9635** through the backup process.  ^If the source database is modified by an
9636** external process or via a database connection other than the one being
9637** used by the backup operation, then the backup will be automatically
9638** restarted by the next call to sqlite3_backup_step(). ^If the source
9639** database is modified by the using the same database connection as is used
9640** by the backup operation, then the backup database is automatically
9641** updated at the same time.
9642**
9643** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
9644**
9645** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
9646** application wishes to abandon the backup operation, the application
9647** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
9648** ^The sqlite3_backup_finish() interfaces releases all
9649** resources associated with the [sqlite3_backup] object.
9650** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
9651** active write-transaction on the destination database is rolled back.
9652** The [sqlite3_backup] object is invalid
9653** and may not be used following a call to sqlite3_backup_finish().
9654**
9655** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
9656** sqlite3_backup_step() errors occurred, regardless or whether or not
9657** sqlite3_backup_step() completed.
9658** ^If an out-of-memory condition or IO error occurred during any prior
9659** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
9660** sqlite3_backup_finish() returns the corresponding [error code].
9661**
9662** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
9663** is not a permanent error and does not affect the return value of
9664** sqlite3_backup_finish().
9665**
9666** [[sqlite3_backup_remaining()]] [[sqlite3_backup_pagecount()]]
9667** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
9668**
9669** ^The sqlite3_backup_remaining() routine returns the number of pages still
9670** to be backed up at the conclusion of the most recent sqlite3_backup_step().
9671** ^The sqlite3_backup_pagecount() routine returns the total number of pages
9672** in the source database at the conclusion of the most recent
9673** sqlite3_backup_step().
9674** ^(The values returned by these functions are only updated by
9675** sqlite3_backup_step(). If the source database is modified in a way that
9676** changes the size of the source database or the number of pages remaining,
9677** those changes are not reflected in the output of sqlite3_backup_pagecount()
9678** and sqlite3_backup_remaining() until after the next
9679** sqlite3_backup_step().)^
9680**
9681** <b>Concurrent Usage of Database Handles</b>
9682**
9683** ^The source [database connection] may be used by the application for other
9684** purposes while a backup operation is underway or being initialized.
9685** ^If SQLite is compiled and configured to support threadsafe database
9686** connections, then the source database connection may be used concurrently
9687** from within other threads.
9688**
9689** However, the application must guarantee that the destination
9690** [database connection] is not passed to any other API (by any thread) after
9691** sqlite3_backup_init() is called and before the corresponding call to
9692** sqlite3_backup_finish().  SQLite does not currently check to see
9693** if the application incorrectly accesses the destination [database connection]
9694** and so no error code is reported, but the operations may malfunction
9695** nevertheless.  Use of the destination database connection while a
9696** backup is in progress might also also cause a mutex deadlock.
9697**
9698** If running in [shared cache mode], the application must
9699** guarantee that the shared cache used by the destination database
9700** is not accessed while the backup is running. In practice this means
9701** that the application must guarantee that the disk file being
9702** backed up to is not accessed by any connection within the process,
9703** not just the specific connection that was passed to sqlite3_backup_init().
9704**
9705** The [sqlite3_backup] object itself is partially threadsafe. Multiple
9706** threads may safely make multiple concurrent calls to sqlite3_backup_step().
9707** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
9708** APIs are not strictly speaking threadsafe. If they are invoked at the
9709** same time as another thread is invoking sqlite3_backup_step() it is
9710** possible that they return invalid values.
9711*/
9712SQLITE_API sqlite3_backup *sqlite3_backup_init(
9713  sqlite3 *pDest,                        /* Destination database handle */
9714  const char *zDestName,                 /* Destination database name */
9715  sqlite3 *pSource,                      /* Source database handle */
9716  const char *zSourceName                /* Source database name */
9717);
9718SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
9719SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
9720SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
9721SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
9722
9723/*
9724** CAPI3REF: Unlock Notification
9725** METHOD: sqlite3
9726**
9727** ^When running in shared-cache mode, a database operation may fail with
9728** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9729** individual tables within the shared-cache cannot be obtained. See
9730** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9731** ^This API may be used to register a callback that SQLite will invoke
9732** when the connection currently holding the required lock relinquishes it.
9733** ^This API is only available if the library was compiled with the
9734** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9735**
9736** See Also: [Using the SQLite Unlock Notification Feature].
9737**
9738** ^Shared-cache locks are released when a database connection concludes
9739** its current transaction, either by committing it or rolling it back.
9740**
9741** ^When a connection (known as the blocked connection) fails to obtain a
9742** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9743** identity of the database connection (the blocking connection) that
9744** has locked the required resource is stored internally. ^After an
9745** application receives an SQLITE_LOCKED error, it may call the
9746** sqlite3_unlock_notify() method with the blocked connection handle as
9747** the first argument to register for a callback that will be invoked
9748** when the blocking connections current transaction is concluded. ^The
9749** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
9750** call that concludes the blocking connection's transaction.
9751**
9752** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9753** there is a chance that the blocking connection will have already
9754** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
9755** If this happens, then the specified callback is invoked immediately,
9756** from within the call to sqlite3_unlock_notify().)^
9757**
9758** ^If the blocked connection is attempting to obtain a write-lock on a
9759** shared-cache table, and more than one other connection currently holds
9760** a read-lock on the same table, then SQLite arbitrarily selects one of
9761** the other connections to use as the blocking connection.
9762**
9763** ^(There may be at most one unlock-notify callback registered by a
9764** blocked connection. If sqlite3_unlock_notify() is called when the
9765** blocked connection already has a registered unlock-notify callback,
9766** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
9767** called with a NULL pointer as its second argument, then any existing
9768** unlock-notify callback is canceled. ^The blocked connections
9769** unlock-notify callback may also be canceled by closing the blocked
9770** connection using [sqlite3_close()].
9771**
9772** The unlock-notify callback is not reentrant. If an application invokes
9773** any sqlite3_xxx API functions from within an unlock-notify callback, a
9774** crash or deadlock may be the result.
9775**
9776** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
9777** returns SQLITE_OK.
9778**
9779** <b>Callback Invocation Details</b>
9780**
9781** When an unlock-notify callback is registered, the application provides a
9782** single void* pointer that is passed to the callback when it is invoked.
9783** However, the signature of the callback function allows SQLite to pass
9784** it an array of void* context pointers. The first argument passed to
9785** an unlock-notify callback is a pointer to an array of void* pointers,
9786** and the second is the number of entries in the array.
9787**
9788** When a blocking connection's transaction is concluded, there may be
9789** more than one blocked connection that has registered for an unlock-notify
9790** callback. ^If two or more such blocked connections have specified the
9791** same callback function, then instead of invoking the callback function
9792** multiple times, it is invoked once with the set of void* context pointers
9793** specified by the blocked connections bundled together into an array.
9794** This gives the application an opportunity to prioritize any actions
9795** related to the set of unblocked database connections.
9796**
9797** <b>Deadlock Detection</b>
9798**
9799** Assuming that after registering for an unlock-notify callback a
9800** database waits for the callback to be issued before taking any further
9801** action (a reasonable assumption), then using this API may cause the
9802** application to deadlock. For example, if connection X is waiting for
9803** connection Y's transaction to be concluded, and similarly connection
9804** Y is waiting on connection X's transaction, then neither connection
9805** will proceed and the system may remain deadlocked indefinitely.
9806**
9807** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
9808** detection. ^If a given call to sqlite3_unlock_notify() would put the
9809** system in a deadlocked state, then SQLITE_LOCKED is returned and no
9810** unlock-notify callback is registered. The system is said to be in
9811** a deadlocked state if connection A has registered for an unlock-notify
9812** callback on the conclusion of connection B's transaction, and connection
9813** B has itself registered for an unlock-notify callback when connection
9814** A's transaction is concluded. ^Indirect deadlock is also detected, so
9815** the system is also considered to be deadlocked if connection B has
9816** registered for an unlock-notify callback on the conclusion of connection
9817** C's transaction, where connection C is waiting on connection A. ^Any
9818** number of levels of indirection are allowed.
9819**
9820** <b>The "DROP TABLE" Exception</b>
9821**
9822** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
9823** always appropriate to call sqlite3_unlock_notify(). There is however,
9824** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
9825** SQLite checks if there are any currently executing SELECT statements
9826** that belong to the same connection. If there are, SQLITE_LOCKED is
9827** returned. In this case there is no "blocking connection", so invoking
9828** sqlite3_unlock_notify() results in the unlock-notify callback being
9829** invoked immediately. If the application then re-attempts the "DROP TABLE"
9830** or "DROP INDEX" query, an infinite loop might be the result.
9831**
9832** One way around this problem is to check the extended error code returned
9833** by an sqlite3_step() call. ^(If there is a blocking connection, then the
9834** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
9835** the special "DROP TABLE/INDEX" case, the extended error code is just
9836** SQLITE_LOCKED.)^
9837*/
9838SQLITE_API int sqlite3_unlock_notify(
9839  sqlite3 *pBlocked,                          /* Waiting connection */
9840  void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
9841  void *pNotifyArg                            /* Argument to pass to xNotify */
9842);
9843
9844
9845/*
9846** CAPI3REF: String Comparison
9847**
9848** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
9849** and extensions to compare the contents of two buffers containing UTF-8
9850** strings in a case-independent fashion, using the same definition of "case
9851** independence" that SQLite uses internally when comparing identifiers.
9852*/
9853SQLITE_API int sqlite3_stricmp(const char *, const char *);
9854SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
9855
9856/*
9857** CAPI3REF: String Globbing
9858*
9859** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if
9860** string X matches the [GLOB] pattern P.
9861** ^The definition of [GLOB] pattern matching used in
9862** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
9863** SQL dialect understood by SQLite.  ^The [sqlite3_strglob(P,X)] function
9864** is case sensitive.
9865**
9866** Note that this routine returns zero on a match and non-zero if the strings
9867** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9868**
9869** See also: [sqlite3_strlike()].
9870*/
9871SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
9872
9873/*
9874** CAPI3REF: String LIKE Matching
9875*
9876** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if
9877** string X matches the [LIKE] pattern P with escape character E.
9878** ^The definition of [LIKE] pattern matching used in
9879** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E"
9880** operator in the SQL dialect understood by SQLite.  ^For "X LIKE P" without
9881** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0.
9882** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case
9883** insensitive - equivalent upper and lower case ASCII characters match
9884** one another.
9885**
9886** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though
9887** only ASCII characters are case folded.
9888**
9889** Note that this routine returns zero on a match and non-zero if the strings
9890** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9891**
9892** See also: [sqlite3_strglob()].
9893*/
9894SQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc);
9895
9896/*
9897** CAPI3REF: Error Logging Interface
9898**
9899** ^The [sqlite3_log()] interface writes a message into the [error log]
9900** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
9901** ^If logging is enabled, the zFormat string and subsequent arguments are
9902** used with [sqlite3_snprintf()] to generate the final output string.
9903**
9904** The sqlite3_log() interface is intended for use by extensions such as
9905** virtual tables, collating functions, and SQL functions.  While there is
9906** nothing to prevent an application from calling sqlite3_log(), doing so
9907** is considered bad form.
9908**
9909** The zFormat string must not be NULL.
9910**
9911** To avoid deadlocks and other threading problems, the sqlite3_log() routine
9912** will not use dynamically allocated memory.  The log message is stored in
9913** a fixed-length buffer on the stack.  If the log message is longer than
9914** a few hundred characters, it will be truncated to the length of the
9915** buffer.
9916*/
9917SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
9918
9919/*
9920** CAPI3REF: Write-Ahead Log Commit Hook
9921** METHOD: sqlite3
9922**
9923** ^The [sqlite3_wal_hook()] function is used to register a callback that
9924** is invoked each time data is committed to a database in wal mode.
9925**
9926** ^(The callback is invoked by SQLite after the commit has taken place and
9927** the associated write-lock on the database released)^, so the implementation
9928** may read, write or [checkpoint] the database as required.
9929**
9930** ^The first parameter passed to the callback function when it is invoked
9931** is a copy of the third parameter passed to sqlite3_wal_hook() when
9932** registering the callback. ^The second is a copy of the database handle.
9933** ^The third parameter is the name of the database that was written to -
9934** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9935** is the number of pages currently in the write-ahead log file,
9936** including those that were just committed.
9937**
9938** The callback function should normally return [SQLITE_OK].  ^If an error
9939** code is returned, that error will propagate back up through the
9940** SQLite code base to cause the statement that provoked the callback
9941** to report an error, though the commit will have still occurred. If the
9942** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
9943** that does not correspond to any valid SQLite error code, the results
9944** are undefined.
9945**
9946** A single database handle may have at most a single write-ahead log callback
9947** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
9948** previously registered write-ahead log callback. ^Note that the
9949** [sqlite3_wal_autocheckpoint()] interface and the
9950** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
9951** overwrite any prior [sqlite3_wal_hook()] settings.
9952*/
9953SQLITE_API void *sqlite3_wal_hook(
9954  sqlite3*,
9955  int(*)(void *,sqlite3*,const char*,int),
9956  void*
9957);
9958
9959/*
9960** CAPI3REF: Configure an auto-checkpoint
9961** METHOD: sqlite3
9962**
9963** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
9964** [sqlite3_wal_hook()] that causes any database on [database connection] D
9965** to automatically [checkpoint]
9966** after committing a transaction if there are N or
9967** more frames in the [write-ahead log] file.  ^Passing zero or
9968** a negative value as the nFrame parameter disables automatic
9969** checkpoints entirely.
9970**
9971** ^The callback registered by this function replaces any existing callback
9972** registered using [sqlite3_wal_hook()].  ^Likewise, registering a callback
9973** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
9974** configured by this function.
9975**
9976** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
9977** from SQL.
9978**
9979** ^Checkpoints initiated by this mechanism are
9980** [sqlite3_wal_checkpoint_v2|PASSIVE].
9981**
9982** ^Every new [database connection] defaults to having the auto-checkpoint
9983** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
9984** pages.  The use of this interface
9985** is only necessary if the default setting is found to be suboptimal
9986** for a particular application.
9987*/
9988SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
9989
9990/*
9991** CAPI3REF: Checkpoint a database
9992** METHOD: sqlite3
9993**
9994** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to
9995** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^
9996**
9997** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the
9998** [write-ahead log] for database X on [database connection] D to be
9999** transferred into the database file and for the write-ahead log to
10000** be reset.  See the [checkpointing] documentation for addition
10001** information.
10002**
10003** This interface used to be the only way to cause a checkpoint to
10004** occur.  But then the newer and more powerful [sqlite3_wal_checkpoint_v2()]
10005** interface was added.  This interface is retained for backwards
10006** compatibility and as a convenience for applications that need to manually
10007** start a callback but which do not need the full power (and corresponding
10008** complication) of [sqlite3_wal_checkpoint_v2()].
10009*/
10010SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
10011
10012/*
10013** CAPI3REF: Checkpoint a database
10014** METHOD: sqlite3
10015**
10016** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
10017** operation on database X of [database connection] D in mode M.  Status
10018** information is written back into integers pointed to by L and C.)^
10019** ^(The M parameter must be a valid [checkpoint mode]:)^
10020**
10021** <dl>
10022** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
10023**   ^Checkpoint as many frames as possible without waiting for any database
10024**   readers or writers to finish, then sync the database file if all frames
10025**   in the log were checkpointed. ^The [busy-handler callback]
10026**   is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode.
10027**   ^On the other hand, passive mode might leave the checkpoint unfinished
10028**   if there are concurrent readers or writers.
10029**
10030** <dt>SQLITE_CHECKPOINT_FULL<dd>
10031**   ^This mode blocks (it invokes the
10032**   [sqlite3_busy_handler|busy-handler callback]) until there is no
10033**   database writer and all readers are reading from the most recent database
10034**   snapshot. ^It then checkpoints all frames in the log file and syncs the
10035**   database file. ^This mode blocks new database writers while it is pending,
10036**   but new database readers are allowed to continue unimpeded.
10037**
10038** <dt>SQLITE_CHECKPOINT_RESTART<dd>
10039**   ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition
10040**   that after checkpointing the log file it blocks (calls the
10041**   [busy-handler callback])
10042**   until all readers are reading from the database file only. ^This ensures
10043**   that the next writer will restart the log file from the beginning.
10044**   ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new
10045**   database writer attempts while it is pending, but does not impede readers.
10046**
10047** <dt>SQLITE_CHECKPOINT_TRUNCATE<dd>
10048**   ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the
10049**   addition that it also truncates the log file to zero bytes just prior
10050**   to a successful return.
10051** </dl>
10052**
10053** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in
10054** the log file or to -1 if the checkpoint could not run because
10055** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not
10056** NULL,then *pnCkpt is set to the total number of checkpointed frames in the
10057** log file (including any that were already checkpointed before the function
10058** was called) or to -1 if the checkpoint could not run due to an error or
10059** because the database is not in WAL mode. ^Note that upon successful
10060** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been
10061** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero.
10062**
10063** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If
10064** any other process is running a checkpoint operation at the same time, the
10065** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a
10066** busy-handler configured, it will not be invoked in this case.
10067**
10068** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the
10069** exclusive "writer" lock on the database file. ^If the writer lock cannot be
10070** obtained immediately, and a busy-handler is configured, it is invoked and
10071** the writer lock retried until either the busy-handler returns 0 or the lock
10072** is successfully obtained. ^The busy-handler is also invoked while waiting for
10073** database readers as described above. ^If the busy-handler returns 0 before
10074** the writer lock is obtained or while waiting for database readers, the
10075** checkpoint operation proceeds from that point in the same way as
10076** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
10077** without blocking any further. ^SQLITE_BUSY is returned in this case.
10078**
10079** ^If parameter zDb is NULL or points to a zero length string, then the
10080** specified operation is attempted on all WAL databases [attached] to
10081** [database connection] db.  In this case the
10082** values written to output parameters *pnLog and *pnCkpt are undefined. ^If
10083** an SQLITE_BUSY error is encountered when processing one or more of the
10084** attached WAL databases, the operation is still attempted on any remaining
10085** attached databases and SQLITE_BUSY is returned at the end. ^If any other
10086** error occurs while processing an attached database, processing is abandoned
10087** and the error code is returned to the caller immediately. ^If no error
10088** (SQLITE_BUSY or otherwise) is encountered while processing the attached
10089** databases, SQLITE_OK is returned.
10090**
10091** ^If database zDb is the name of an attached database that is not in WAL
10092** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
10093** zDb is not NULL (or a zero length string) and is not the name of any
10094** attached database, SQLITE_ERROR is returned to the caller.
10095**
10096** ^Unless it returns SQLITE_MISUSE,
10097** the sqlite3_wal_checkpoint_v2() interface
10098** sets the error information that is queried by
10099** [sqlite3_errcode()] and [sqlite3_errmsg()].
10100**
10101** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface
10102** from SQL.
10103*/
10104SQLITE_API int sqlite3_wal_checkpoint_v2(
10105  sqlite3 *db,                    /* Database handle */
10106  const char *zDb,                /* Name of attached database (or NULL) */
10107  int eMode,                      /* SQLITE_CHECKPOINT_* value */
10108  int *pnLog,                     /* OUT: Size of WAL log in frames */
10109  int *pnCkpt                     /* OUT: Total number of frames checkpointed */
10110);
10111
10112/*
10113** CAPI3REF: Checkpoint Mode Values
10114** KEYWORDS: {checkpoint mode}
10115**
10116** These constants define all valid values for the "checkpoint mode" passed
10117** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface.
10118** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
10119** meaning of each of these checkpoint modes.
10120*/
10121#define SQLITE_CHECKPOINT_PASSIVE  0  /* Do as much as possible w/o blocking */
10122#define SQLITE_CHECKPOINT_FULL     1  /* Wait for writers, then checkpoint */
10123#define SQLITE_CHECKPOINT_RESTART  2  /* Like FULL but wait for for readers */
10124#define SQLITE_CHECKPOINT_TRUNCATE 3  /* Like RESTART but also truncate WAL */
10125
10126/*
10127** CAPI3REF: Virtual Table Interface Configuration
10128**
10129** This function may be called by either the [xConnect] or [xCreate] method
10130** of a [virtual table] implementation to configure
10131** various facets of the virtual table interface.
10132**
10133** If this interface is invoked outside the context of an xConnect or
10134** xCreate virtual table method then the behavior is undefined.
10135**
10136** In the call sqlite3_vtab_config(D,C,...) the D parameter is the
10137** [database connection] in which the virtual table is being created and
10138** which is passed in as the first argument to the [xConnect] or [xCreate]
10139** method that is invoking sqlite3_vtab_config().  The C parameter is one
10140** of the [virtual table configuration options].  The presence and meaning
10141** of parameters after C depend on which [virtual table configuration option]
10142** is used.
10143*/
10144SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
10145
10146/*
10147** CAPI3REF: Virtual Table Configuration Options
10148** KEYWORDS: {virtual table configuration options}
10149** KEYWORDS: {virtual table configuration option}
10150**
10151** These macros define the various options to the
10152** [sqlite3_vtab_config()] interface that [virtual table] implementations
10153** can use to customize and optimize their behavior.
10154**
10155** <dl>
10156** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
10157** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT</dt>
10158** <dd>Calls of the form
10159** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
10160** where X is an integer.  If X is zero, then the [virtual table] whose
10161** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
10162** support constraints.  In this configuration (which is the default) if
10163** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
10164** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
10165** specified as part of the users SQL statement, regardless of the actual
10166** ON CONFLICT mode specified.
10167**
10168** If X is non-zero, then the virtual table implementation guarantees
10169** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
10170** any modifications to internal or persistent data structures have been made.
10171** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
10172** is able to roll back a statement or database transaction, and abandon
10173** or continue processing the current SQL statement as appropriate.
10174** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
10175** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
10176** had been ABORT.
10177**
10178** Virtual table implementations that are required to handle OR REPLACE
10179** must do so within the [xUpdate] method. If a call to the
10180** [sqlite3_vtab_on_conflict()] function indicates that the current ON
10181** CONFLICT policy is REPLACE, the virtual table implementation should
10182** silently replace the appropriate rows within the xUpdate callback and
10183** return SQLITE_OK. Or, if this is not possible, it may return
10184** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
10185** constraint handling.
10186** </dd>
10187**
10188** [[SQLITE_VTAB_DIRECTONLY]]<dt>SQLITE_VTAB_DIRECTONLY</dt>
10189** <dd>Calls of the form
10190** [sqlite3_vtab_config](db,SQLITE_VTAB_DIRECTONLY) from within the
10191** the [xConnect] or [xCreate] methods of a [virtual table] implmentation
10192** prohibits that virtual table from being used from within triggers and
10193** views.
10194** </dd>
10195**
10196** [[SQLITE_VTAB_INNOCUOUS]]<dt>SQLITE_VTAB_INNOCUOUS</dt>
10197** <dd>Calls of the form
10198** [sqlite3_vtab_config](db,SQLITE_VTAB_INNOCUOUS) from within the
10199** the [xConnect] or [xCreate] methods of a [virtual table] implmentation
10200** identify that virtual table as being safe to use from within triggers
10201** and views.  Conceptually, the SQLITE_VTAB_INNOCUOUS tag means that the
10202** virtual table can do no serious harm even if it is controlled by a
10203** malicious hacker.  Developers should avoid setting the SQLITE_VTAB_INNOCUOUS
10204** flag unless absolutely necessary.
10205** </dd>
10206** </dl>
10207*/
10208#define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
10209#define SQLITE_VTAB_INNOCUOUS          2
10210#define SQLITE_VTAB_DIRECTONLY         3
10211
10212/*
10213** CAPI3REF: Determine The Virtual Table Conflict Policy
10214**
10215** This function may only be called from within a call to the [xUpdate] method
10216** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
10217** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
10218** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
10219** of the SQL statement that triggered the call to the [xUpdate] method of the
10220** [virtual table].
10221*/
10222SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
10223
10224/*
10225** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
10226**
10227** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
10228** method of a [virtual table], then it returns true if and only if the
10229** column is being fetched as part of an UPDATE operation during which the
10230** column value will not change.  Applications might use this to substitute
10231** a return value that is less expensive to compute and that the corresponding
10232** [xUpdate] method understands as a "no-change" value.
10233**
10234** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
10235** the column is not changed by the UPDATE statement, then the xColumn
10236** method can optionally return without setting a result, without calling
10237** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
10238** In that case, [sqlite3_value_nochange(X)] will return true for the
10239** same column in the [xUpdate] method.
10240*/
10241SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
10242
10243/*
10244** CAPI3REF: Determine The Collation For a Virtual Table Constraint
10245**
10246** This function may only be called from within a call to the [xBestIndex]
10247** method of a [virtual table].
10248**
10249** The first argument must be the sqlite3_index_info object that is the
10250** first parameter to the xBestIndex() method. The second argument must be
10251** an index into the aConstraint[] array belonging to the sqlite3_index_info
10252** structure passed to xBestIndex. This function returns a pointer to a buffer
10253** containing the name of the collation sequence for the corresponding
10254** constraint.
10255*/
10256SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
10257
10258/*
10259** CAPI3REF: Conflict resolution modes
10260** KEYWORDS: {conflict resolution mode}
10261**
10262** These constants are returned by [sqlite3_vtab_on_conflict()] to
10263** inform a [virtual table] implementation what the [ON CONFLICT] mode
10264** is for the SQL statement being evaluated.
10265**
10266** Note that the [SQLITE_IGNORE] constant is also used as a potential
10267** return value from the [sqlite3_set_authorizer()] callback and that
10268** [SQLITE_ABORT] is also a [result code].
10269*/
10270#define SQLITE_ROLLBACK 1
10271/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
10272#define SQLITE_FAIL     3
10273/* #define SQLITE_ABORT 4  // Also an error code */
10274#define SQLITE_REPLACE  5
10275
10276/*
10277** CAPI3REF: Prepared Statement Scan Status Opcodes
10278** KEYWORDS: {scanstatus options}
10279**
10280** The following constants can be used for the T parameter to the
10281** [sqlite3_stmt_scanstatus(S,X,T,V)] interface.  Each constant designates a
10282** different metric for sqlite3_stmt_scanstatus() to return.
10283**
10284** When the value returned to V is a string, space to hold that string is
10285** managed by the prepared statement S and will be automatically freed when
10286** S is finalized.
10287**
10288** <dl>
10289** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
10290** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be
10291** set to the total number of times that the X-th loop has run.</dd>
10292**
10293** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
10294** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be set
10295** to the total number of rows examined by all iterations of the X-th loop.</dd>
10296**
10297** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
10298** <dd>^The "double" variable pointed to by the V parameter will be set to the
10299** query planner's estimate for the average number of rows output from each
10300** iteration of the X-th loop.  If the query planner's estimates was accurate,
10301** then this value will approximate the quotient NVISIT/NLOOP and the
10302** product of this value for all prior loops with the same SELECTID will
10303** be the NLOOP value for the current loop.
10304**
10305** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
10306** <dd>^The "const char *" variable pointed to by the V parameter will be set
10307** to a zero-terminated UTF-8 string containing the name of the index or table
10308** used for the X-th loop.
10309**
10310** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
10311** <dd>^The "const char *" variable pointed to by the V parameter will be set
10312** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
10313** description for the X-th loop.
10314**
10315** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt>
10316** <dd>^The "int" variable pointed to by the V parameter will be set to the
10317** "select-id" for the X-th loop.  The select-id identifies which query or
10318** subquery the loop is part of.  The main query has a select-id of zero.
10319** The select-id is the same value as is output in the first column
10320** of an [EXPLAIN QUERY PLAN] query.
10321** </dl>
10322*/
10323#define SQLITE_SCANSTAT_NLOOP    0
10324#define SQLITE_SCANSTAT_NVISIT   1
10325#define SQLITE_SCANSTAT_EST      2
10326#define SQLITE_SCANSTAT_NAME     3
10327#define SQLITE_SCANSTAT_EXPLAIN  4
10328#define SQLITE_SCANSTAT_SELECTID 5
10329
10330/*
10331** CAPI3REF: Prepared Statement Scan Status
10332** METHOD: sqlite3_stmt
10333**
10334** This interface returns information about the predicted and measured
10335** performance for pStmt.  Advanced applications can use this
10336** interface to compare the predicted and the measured performance and
10337** issue warnings and/or rerun [ANALYZE] if discrepancies are found.
10338**
10339** Since this interface is expected to be rarely used, it is only
10340** available if SQLite is compiled using the [SQLITE_ENABLE_STMT_SCANSTATUS]
10341** compile-time option.
10342**
10343** The "iScanStatusOp" parameter determines which status information to return.
10344** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior
10345** of this interface is undefined.
10346** ^The requested measurement is written into a variable pointed to by
10347** the "pOut" parameter.
10348** Parameter "idx" identifies the specific loop to retrieve statistics for.
10349** Loops are numbered starting from zero. ^If idx is out of range - less than
10350** zero or greater than or equal to the total number of loops used to implement
10351** the statement - a non-zero value is returned and the variable that pOut
10352** points to is unchanged.
10353**
10354** ^Statistics might not be available for all loops in all statements. ^In cases
10355** where there exist loops with no available statistics, this function behaves
10356** as if the loop did not exist - it returns non-zero and leave the variable
10357** that pOut points to unchanged.
10358**
10359** See also: [sqlite3_stmt_scanstatus_reset()]
10360*/
10361SQLITE_API int sqlite3_stmt_scanstatus(
10362  sqlite3_stmt *pStmt,      /* Prepared statement for which info desired */
10363  int idx,                  /* Index of loop to report on */
10364  int iScanStatusOp,        /* Information desired.  SQLITE_SCANSTAT_* */
10365  void *pOut                /* Result written here */
10366);
10367
10368/*
10369** CAPI3REF: Zero Scan-Status Counters
10370** METHOD: sqlite3_stmt
10371**
10372** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
10373**
10374** This API is only available if the library is built with pre-processor
10375** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
10376*/
10377SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
10378
10379/*
10380** CAPI3REF: Flush caches to disk mid-transaction
10381**
10382** ^If a write-transaction is open on [database connection] D when the
10383** [sqlite3_db_cacheflush(D)] interface invoked, any dirty
10384** pages in the pager-cache that are not currently in use are written out
10385** to disk. A dirty page may be in use if a database cursor created by an
10386** active SQL statement is reading from it, or if it is page 1 of a database
10387** file (page 1 is always "in use").  ^The [sqlite3_db_cacheflush(D)]
10388** interface flushes caches for all schemas - "main", "temp", and
10389** any [attached] databases.
10390**
10391** ^If this function needs to obtain extra database locks before dirty pages
10392** can be flushed to disk, it does so. ^If those locks cannot be obtained
10393** immediately and there is a busy-handler callback configured, it is invoked
10394** in the usual manner. ^If the required lock still cannot be obtained, then
10395** the database is skipped and an attempt made to flush any dirty pages
10396** belonging to the next (if any) database. ^If any databases are skipped
10397** because locks cannot be obtained, but no other error occurs, this
10398** function returns SQLITE_BUSY.
10399**
10400** ^If any other error occurs while flushing dirty pages to disk (for
10401** example an IO error or out-of-memory condition), then processing is
10402** abandoned and an SQLite [error code] is returned to the caller immediately.
10403**
10404** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK.
10405**
10406** ^This function does not set the database handle error code or message
10407** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions.
10408*/
10409SQLITE_API int sqlite3_db_cacheflush(sqlite3*);
10410
10411/*
10412** CAPI3REF: The pre-update hook.
10413**
10414** ^These interfaces are only available if SQLite is compiled using the
10415** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
10416**
10417** ^The [sqlite3_preupdate_hook()] interface registers a callback function
10418** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
10419** on a database table.
10420** ^At most one preupdate hook may be registered at a time on a single
10421** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
10422** the previous setting.
10423** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
10424** with a NULL pointer as the second parameter.
10425** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
10426** the first parameter to callbacks.
10427**
10428** ^The preupdate hook only fires for changes to real database tables; the
10429** preupdate hook is not invoked for changes to [virtual tables] or to
10430** system tables like sqlite_master or sqlite_stat1.
10431**
10432** ^The second parameter to the preupdate callback is a pointer to
10433** the [database connection] that registered the preupdate hook.
10434** ^The third parameter to the preupdate callback is one of the constants
10435** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
10436** kind of update operation that is about to occur.
10437** ^(The fourth parameter to the preupdate callback is the name of the
10438** database within the database connection that is being modified.  This
10439** will be "main" for the main database or "temp" for TEMP tables or
10440** the name given after the AS keyword in the [ATTACH] statement for attached
10441** databases.)^
10442** ^The fifth parameter to the preupdate callback is the name of the
10443** table that is being modified.
10444**
10445** For an UPDATE or DELETE operation on a [rowid table], the sixth
10446** parameter passed to the preupdate callback is the initial [rowid] of the
10447** row being modified or deleted. For an INSERT operation on a rowid table,
10448** or any operation on a WITHOUT ROWID table, the value of the sixth
10449** parameter is undefined. For an INSERT or UPDATE on a rowid table the
10450** seventh parameter is the final rowid value of the row being inserted
10451** or updated. The value of the seventh parameter passed to the callback
10452** function is not defined for operations on WITHOUT ROWID tables, or for
10453** INSERT operations on rowid tables.
10454**
10455** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
10456** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
10457** provide additional information about a preupdate event. These routines
10458** may only be called from within a preupdate callback.  Invoking any of
10459** these routines from outside of a preupdate callback or with a
10460** [database connection] pointer that is different from the one supplied
10461** to the preupdate callback results in undefined and probably undesirable
10462** behavior.
10463**
10464** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns
10465** in the row that is being inserted, updated, or deleted.
10466**
10467** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to
10468** a [protected sqlite3_value] that contains the value of the Nth column of
10469** the table row before it is updated.  The N parameter must be between 0
10470** and one less than the number of columns or the behavior will be
10471** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE
10472** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the
10473** behavior is undefined.  The [sqlite3_value] that P points to
10474** will be destroyed when the preupdate callback returns.
10475**
10476** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to
10477** a [protected sqlite3_value] that contains the value of the Nth column of
10478** the table row after it is updated.  The N parameter must be between 0
10479** and one less than the number of columns or the behavior will be
10480** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE
10481** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the
10482** behavior is undefined.  The [sqlite3_value] that P points to
10483** will be destroyed when the preupdate callback returns.
10484**
10485** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate
10486** callback was invoked as a result of a direct insert, update, or delete
10487** operation; or 1 for inserts, updates, or deletes invoked by top-level
10488** triggers; or 2 for changes resulting from triggers called by top-level
10489** triggers; and so forth.
10490**
10491** See also:  [sqlite3_update_hook()]
10492*/
10493#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
10494SQLITE_API void *sqlite3_preupdate_hook(
10495  sqlite3 *db,
10496  void(*xPreUpdate)(
10497    void *pCtx,                   /* Copy of third arg to preupdate_hook() */
10498    sqlite3 *db,                  /* Database handle */
10499    int op,                       /* SQLITE_UPDATE, DELETE or INSERT */
10500    char const *zDb,              /* Database name */
10501    char const *zName,            /* Table name */
10502    sqlite3_int64 iKey1,          /* Rowid of row about to be deleted/updated */
10503    sqlite3_int64 iKey2           /* New rowid value (for a rowid UPDATE) */
10504  ),
10505  void*
10506);
10507SQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
10508SQLITE_API int sqlite3_preupdate_count(sqlite3 *);
10509SQLITE_API int sqlite3_preupdate_depth(sqlite3 *);
10510SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
10511#endif
10512
10513/*
10514** CAPI3REF: Low-level system error code
10515**
10516** ^Attempt to return the underlying operating system error code or error
10517** number that caused the most recent I/O error or failure to open a file.
10518** The return value is OS-dependent.  For example, on unix systems, after
10519** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be
10520** called to get back the underlying "errno" that caused the problem, such
10521** as ENOSPC, EAUTH, EISDIR, and so forth.
10522*/
10523SQLITE_API int sqlite3_system_errno(sqlite3*);
10524
10525/*
10526** CAPI3REF: Database Snapshot
10527** KEYWORDS: {snapshot} {sqlite3_snapshot}
10528**
10529** An instance of the snapshot object records the state of a [WAL mode]
10530** database for some specific point in history.
10531**
10532** In [WAL mode], multiple [database connections] that are open on the
10533** same database file can each be reading a different historical version
10534** of the database file.  When a [database connection] begins a read
10535** transaction, that connection sees an unchanging copy of the database
10536** as it existed for the point in time when the transaction first started.
10537** Subsequent changes to the database from other connections are not seen
10538** by the reader until a new read transaction is started.
10539**
10540** The sqlite3_snapshot object records state information about an historical
10541** version of the database file so that it is possible to later open a new read
10542** transaction that sees that historical version of the database rather than
10543** the most recent version.
10544*/
10545typedef struct sqlite3_snapshot {
10546  unsigned char hidden[48];
10547} sqlite3_snapshot;
10548
10549/*
10550** CAPI3REF: Record A Database Snapshot
10551** CONSTRUCTOR: sqlite3_snapshot
10552**
10553** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a
10554** new [sqlite3_snapshot] object that records the current state of
10555** schema S in database connection D.  ^On success, the
10556** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly
10557** created [sqlite3_snapshot] object into *P and returns SQLITE_OK.
10558** If there is not already a read-transaction open on schema S when
10559** this function is called, one is opened automatically.
10560**
10561** The following must be true for this function to succeed. If any of
10562** the following statements are false when sqlite3_snapshot_get() is
10563** called, SQLITE_ERROR is returned. The final value of *P is undefined
10564** in this case.
10565**
10566** <ul>
10567**   <li> The database handle must not be in [autocommit mode].
10568**
10569**   <li> Schema S of [database connection] D must be a [WAL mode] database.
10570**
10571**   <li> There must not be a write transaction open on schema S of database
10572**        connection D.
10573**
10574**   <li> One or more transactions must have been written to the current wal
10575**        file since it was created on disk (by any connection). This means
10576**        that a snapshot cannot be taken on a wal mode database with no wal
10577**        file immediately after it is first opened. At least one transaction
10578**        must be written to it first.
10579** </ul>
10580**
10581** This function may also return SQLITE_NOMEM.  If it is called with the
10582** database handle in autocommit mode but fails for some other reason,
10583** whether or not a read transaction is opened on schema S is undefined.
10584**
10585** The [sqlite3_snapshot] object returned from a successful call to
10586** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()]
10587** to avoid a memory leak.
10588**
10589** The [sqlite3_snapshot_get()] interface is only available when the
10590** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10591*/
10592SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
10593  sqlite3 *db,
10594  const char *zSchema,
10595  sqlite3_snapshot **ppSnapshot
10596);
10597
10598/*
10599** CAPI3REF: Start a read transaction on an historical snapshot
10600** METHOD: sqlite3_snapshot
10601**
10602** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read
10603** transaction or upgrades an existing one for schema S of
10604** [database connection] D such that the read transaction refers to
10605** historical [snapshot] P, rather than the most recent change to the
10606** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK
10607** on success or an appropriate [error code] if it fails.
10608**
10609** ^In order to succeed, the database connection must not be in
10610** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there
10611** is already a read transaction open on schema S, then the database handle
10612** must have no active statements (SELECT statements that have been passed
10613** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()).
10614** SQLITE_ERROR is returned if either of these conditions is violated, or
10615** if schema S does not exist, or if the snapshot object is invalid.
10616**
10617** ^A call to sqlite3_snapshot_open() will fail to open if the specified
10618** snapshot has been overwritten by a [checkpoint]. In this case
10619** SQLITE_ERROR_SNAPSHOT is returned.
10620**
10621** If there is already a read transaction open when this function is
10622** invoked, then the same read transaction remains open (on the same
10623** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT
10624** is returned. If another error code - for example SQLITE_PROTOCOL or an
10625** SQLITE_IOERR error code - is returned, then the final state of the
10626** read transaction is undefined. If SQLITE_OK is returned, then the
10627** read transaction is now open on database snapshot P.
10628**
10629** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
10630** database connection D does not know that the database file for
10631** schema S is in [WAL mode].  A database connection might not know
10632** that the database file is in [WAL mode] if there has been no prior
10633** I/O on that database connection, or if the database entered [WAL mode]
10634** after the most recent I/O on the database connection.)^
10635** (Hint: Run "[PRAGMA application_id]" against a newly opened
10636** database connection in order to make it ready to use snapshots.)
10637**
10638** The [sqlite3_snapshot_open()] interface is only available when the
10639** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10640*/
10641SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
10642  sqlite3 *db,
10643  const char *zSchema,
10644  sqlite3_snapshot *pSnapshot
10645);
10646
10647/*
10648** CAPI3REF: Destroy a snapshot
10649** DESTRUCTOR: sqlite3_snapshot
10650**
10651** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P.
10652** The application must eventually free every [sqlite3_snapshot] object
10653** using this routine to avoid a memory leak.
10654**
10655** The [sqlite3_snapshot_free()] interface is only available when the
10656** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10657*/
10658SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*);
10659
10660/*
10661** CAPI3REF: Compare the ages of two snapshot handles.
10662** METHOD: sqlite3_snapshot
10663**
10664** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages
10665** of two valid snapshot handles.
10666**
10667** If the two snapshot handles are not associated with the same database
10668** file, the result of the comparison is undefined.
10669**
10670** Additionally, the result of the comparison is only valid if both of the
10671** snapshot handles were obtained by calling sqlite3_snapshot_get() since the
10672** last time the wal file was deleted. The wal file is deleted when the
10673** database is changed back to rollback mode or when the number of database
10674** clients drops to zero. If either snapshot handle was obtained before the
10675** wal file was last deleted, the value returned by this function
10676** is undefined.
10677**
10678** Otherwise, this API returns a negative value if P1 refers to an older
10679** snapshot than P2, zero if the two handles refer to the same database
10680** snapshot, and a positive value if P1 is a newer snapshot than P2.
10681**
10682** This interface is only available if SQLite is compiled with the
10683** [SQLITE_ENABLE_SNAPSHOT] option.
10684*/
10685SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
10686  sqlite3_snapshot *p1,
10687  sqlite3_snapshot *p2
10688);
10689
10690/*
10691** CAPI3REF: Recover snapshots from a wal file
10692** METHOD: sqlite3_snapshot
10693**
10694** If a [WAL file] remains on disk after all database connections close
10695** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control]
10696** or because the last process to have the database opened exited without
10697** calling [sqlite3_close()]) and a new connection is subsequently opened
10698** on that database and [WAL file], the [sqlite3_snapshot_open()] interface
10699** will only be able to open the last transaction added to the WAL file
10700** even though the WAL file contains other valid transactions.
10701**
10702** This function attempts to scan the WAL file associated with database zDb
10703** of database handle db and make all valid snapshots available to
10704** sqlite3_snapshot_open(). It is an error if there is already a read
10705** transaction open on the database, or if the database is not a WAL mode
10706** database.
10707**
10708** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
10709**
10710** This interface is only available if SQLite is compiled with the
10711** [SQLITE_ENABLE_SNAPSHOT] option.
10712*/
10713SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
10714
10715/*
10716** CAPI3REF: Serialize a database
10717**
10718** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
10719** that is a serialization of the S database on [database connection] D.
10720** If P is not a NULL pointer, then the size of the database in bytes
10721** is written into *P.
10722**
10723** For an ordinary on-disk database file, the serialization is just a
10724** copy of the disk file.  For an in-memory database or a "TEMP" database,
10725** the serialization is the same sequence of bytes which would be written
10726** to disk if that database where backed up to disk.
10727**
10728** The usual case is that sqlite3_serialize() copies the serialization of
10729** the database into memory obtained from [sqlite3_malloc64()] and returns
10730** a pointer to that memory.  The caller is responsible for freeing the
10731** returned value to avoid a memory leak.  However, if the F argument
10732** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
10733** are made, and the sqlite3_serialize() function will return a pointer
10734** to the contiguous memory representation of the database that SQLite
10735** is currently using for that database, or NULL if the no such contiguous
10736** memory representation of the database exists.  A contiguous memory
10737** representation of the database will usually only exist if there has
10738** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
10739** values of D and S.
10740** The size of the database is written into *P even if the
10741** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
10742** of the database exists.
10743**
10744** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
10745** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
10746** allocation error occurs.
10747**
10748** This interface is only available if SQLite is compiled with the
10749** [SQLITE_ENABLE_DESERIALIZE] option.
10750*/
10751SQLITE_API unsigned char *sqlite3_serialize(
10752  sqlite3 *db,           /* The database connection */
10753  const char *zSchema,   /* Which DB to serialize. ex: "main", "temp", ... */
10754  sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
10755  unsigned int mFlags    /* Zero or more SQLITE_SERIALIZE_* flags */
10756);
10757
10758/*
10759** CAPI3REF: Flags for sqlite3_serialize
10760**
10761** Zero or more of the following constants can be OR-ed together for
10762** the F argument to [sqlite3_serialize(D,S,P,F)].
10763**
10764** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
10765** a pointer to contiguous in-memory database that it is currently using,
10766** without making a copy of the database.  If SQLite is not currently using
10767** a contiguous in-memory database, then this option causes
10768** [sqlite3_serialize()] to return a NULL pointer.  SQLite will only be
10769** using a contiguous in-memory database if it has been initialized by a
10770** prior call to [sqlite3_deserialize()].
10771*/
10772#define SQLITE_SERIALIZE_NOCOPY 0x001   /* Do no memory allocations */
10773
10774/*
10775** CAPI3REF: Deserialize a database
10776**
10777** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
10778** [database connection] D to disconnect from database S and then
10779** reopen S as an in-memory database based on the serialization contained
10780** in P.  The serialized database P is N bytes in size.  M is the size of
10781** the buffer P, which might be larger than N.  If M is larger than N, and
10782** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
10783** permitted to add content to the in-memory database as long as the total
10784** size does not exceed M bytes.
10785**
10786** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
10787** invoke sqlite3_free() on the serialization buffer when the database
10788** connection closes.  If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
10789** SQLite will try to increase the buffer size using sqlite3_realloc64()
10790** if writes on the database cause it to grow larger than M bytes.
10791**
10792** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
10793** database is currently in a read transaction or is involved in a backup
10794** operation.
10795**
10796** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
10797** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
10798** [sqlite3_free()] is invoked on argument P prior to returning.
10799**
10800** This interface is only available if SQLite is compiled with the
10801** [SQLITE_ENABLE_DESERIALIZE] option.
10802*/
10803SQLITE_API int sqlite3_deserialize(
10804  sqlite3 *db,            /* The database connection */
10805  const char *zSchema,    /* Which DB to reopen with the deserialization */
10806  unsigned char *pData,   /* The serialized database content */
10807  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
10808  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
10809  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
10810);
10811
10812/*
10813** CAPI3REF: Flags for sqlite3_deserialize()
10814**
10815** The following are allowed values for 6th argument (the F argument) to
10816** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
10817**
10818** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
10819** in the P argument is held in memory obtained from [sqlite3_malloc64()]
10820** and that SQLite should take ownership of this memory and automatically
10821** free it when it has finished using it.  Without this flag, the caller
10822** is responsible for freeing any dynamically allocated memory.
10823**
10824** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
10825** grow the size of the database using calls to [sqlite3_realloc64()].  This
10826** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
10827** Without this flag, the deserialized database cannot increase in size beyond
10828** the number of bytes specified by the M parameter.
10829**
10830** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
10831** should be treated as read-only.
10832*/
10833#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
10834#define SQLITE_DESERIALIZE_RESIZEABLE  2 /* Resize using sqlite3_realloc64() */
10835#define SQLITE_DESERIALIZE_READONLY    4 /* Database is read-only */
10836
10837/*
10838** Undo the hack that converts floating point types to integer for
10839** builds on processors without floating point support.
10840*/
10841#ifdef SQLITE_OMIT_FLOATING_POINT
10842# undef double
10843#endif
10844
10845#if 0
10846}  /* End of the 'extern "C"' block */
10847#endif
10848#endif /* SQLITE3_H */
10849
10850/******** Begin file sqlite3rtree.h *********/
10851/*
10852** 2010 August 30
10853**
10854** The author disclaims copyright to this source code.  In place of
10855** a legal notice, here is a blessing:
10856**
10857**    May you do good and not evil.
10858**    May you find forgiveness for yourself and forgive others.
10859**    May you share freely, never taking more than you give.
10860**
10861*************************************************************************
10862*/
10863
10864#ifndef _SQLITE3RTREE_H_
10865#define _SQLITE3RTREE_H_
10866
10867
10868#if 0
10869extern "C" {
10870#endif
10871
10872typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
10873typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
10874
10875/* The double-precision datatype used by RTree depends on the
10876** SQLITE_RTREE_INT_ONLY compile-time option.
10877*/
10878#ifdef SQLITE_RTREE_INT_ONLY
10879  typedef sqlite3_int64 sqlite3_rtree_dbl;
10880#else
10881  typedef double sqlite3_rtree_dbl;
10882#endif
10883
10884/*
10885** Register a geometry callback named zGeom that can be used as part of an
10886** R-Tree geometry query as follows:
10887**
10888**   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
10889*/
10890SQLITE_API int sqlite3_rtree_geometry_callback(
10891  sqlite3 *db,
10892  const char *zGeom,
10893  int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
10894  void *pContext
10895);
10896
10897
10898/*
10899** A pointer to a structure of the following type is passed as the first
10900** argument to callbacks registered using rtree_geometry_callback().
10901*/
10902struct sqlite3_rtree_geometry {
10903  void *pContext;                 /* Copy of pContext passed to s_r_g_c() */
10904  int nParam;                     /* Size of array aParam[] */
10905  sqlite3_rtree_dbl *aParam;      /* Parameters passed to SQL geom function */
10906  void *pUser;                    /* Callback implementation user data */
10907  void (*xDelUser)(void *);       /* Called by SQLite to clean up pUser */
10908};
10909
10910/*
10911** Register a 2nd-generation geometry callback named zScore that can be
10912** used as part of an R-Tree geometry query as follows:
10913**
10914**   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
10915*/
10916SQLITE_API int sqlite3_rtree_query_callback(
10917  sqlite3 *db,
10918  const char *zQueryFunc,
10919  int (*xQueryFunc)(sqlite3_rtree_query_info*),
10920  void *pContext,
10921  void (*xDestructor)(void*)
10922);
10923
10924
10925/*
10926** A pointer to a structure of the following type is passed as the
10927** argument to scored geometry callback registered using
10928** sqlite3_rtree_query_callback().
10929**
10930** Note that the first 5 fields of this structure are identical to
10931** sqlite3_rtree_geometry.  This structure is a subclass of
10932** sqlite3_rtree_geometry.
10933*/
10934struct sqlite3_rtree_query_info {
10935  void *pContext;                   /* pContext from when function registered */
10936  int nParam;                       /* Number of function parameters */
10937  sqlite3_rtree_dbl *aParam;        /* value of function parameters */
10938  void *pUser;                      /* callback can use this, if desired */
10939  void (*xDelUser)(void*);          /* function to free pUser */
10940  sqlite3_rtree_dbl *aCoord;        /* Coordinates of node or entry to check */
10941  unsigned int *anQueue;            /* Number of pending entries in the queue */
10942  int nCoord;                       /* Number of coordinates */
10943  int iLevel;                       /* Level of current node or entry */
10944  int mxLevel;                      /* The largest iLevel value in the tree */
10945  sqlite3_int64 iRowid;             /* Rowid for current entry */
10946  sqlite3_rtree_dbl rParentScore;   /* Score of parent node */
10947  int eParentWithin;                /* Visibility of parent node */
10948  int eWithin;                      /* OUT: Visibility */
10949  sqlite3_rtree_dbl rScore;         /* OUT: Write the score here */
10950  /* The following fields are only available in 3.8.11 and later */
10951  sqlite3_value **apSqlParam;       /* Original SQL values of parameters */
10952};
10953
10954/*
10955** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
10956*/
10957#define NOT_WITHIN       0   /* Object completely outside of query region */
10958#define PARTLY_WITHIN    1   /* Object partially overlaps query region */
10959#define FULLY_WITHIN     2   /* Object fully contained within query region */
10960
10961
10962#if 0
10963}  /* end of the 'extern "C"' block */
10964#endif
10965
10966#endif  /* ifndef _SQLITE3RTREE_H_ */
10967
10968/******** End of sqlite3rtree.h *********/
10969/******** Begin file sqlite3session.h *********/
10970
10971#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION)
10972#define __SQLITESESSION_H_ 1
10973
10974/*
10975** Make sure we can call this stuff from C++.
10976*/
10977#if 0
10978extern "C" {
10979#endif
10980
10981
10982/*
10983** CAPI3REF: Session Object Handle
10984**
10985** An instance of this object is a [session] that can be used to
10986** record changes to a database.
10987*/
10988typedef struct sqlite3_session sqlite3_session;
10989
10990/*
10991** CAPI3REF: Changeset Iterator Handle
10992**
10993** An instance of this object acts as a cursor for iterating
10994** over the elements of a [changeset] or [patchset].
10995*/
10996typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
10997
10998/*
10999** CAPI3REF: Create A New Session Object
11000** CONSTRUCTOR: sqlite3_session
11001**
11002** Create a new session object attached to database handle db. If successful,
11003** a pointer to the new object is written to *ppSession and SQLITE_OK is
11004** returned. If an error occurs, *ppSession is set to NULL and an SQLite
11005** error code (e.g. SQLITE_NOMEM) is returned.
11006**
11007** It is possible to create multiple session objects attached to a single
11008** database handle.
11009**
11010** Session objects created using this function should be deleted using the
11011** [sqlite3session_delete()] function before the database handle that they
11012** are attached to is itself closed. If the database handle is closed before
11013** the session object is deleted, then the results of calling any session
11014** module function, including [sqlite3session_delete()] on the session object
11015** are undefined.
11016**
11017** Because the session module uses the [sqlite3_preupdate_hook()] API, it
11018** is not possible for an application to register a pre-update hook on a
11019** database handle that has one or more session objects attached. Nor is
11020** it possible to create a session object attached to a database handle for
11021** which a pre-update hook is already defined. The results of attempting
11022** either of these things are undefined.
11023**
11024** The session object will be used to create changesets for tables in
11025** database zDb, where zDb is either "main", or "temp", or the name of an
11026** attached database. It is not an error if database zDb is not attached
11027** to the database when the session object is created.
11028*/
11029SQLITE_API int sqlite3session_create(
11030  sqlite3 *db,                    /* Database handle */
11031  const char *zDb,                /* Name of db (e.g. "main") */
11032  sqlite3_session **ppSession     /* OUT: New session object */
11033);
11034
11035/*
11036** CAPI3REF: Delete A Session Object
11037** DESTRUCTOR: sqlite3_session
11038**
11039** Delete a session object previously allocated using
11040** [sqlite3session_create()]. Once a session object has been deleted, the
11041** results of attempting to use pSession with any other session module
11042** function are undefined.
11043**
11044** Session objects must be deleted before the database handle to which they
11045** are attached is closed. Refer to the documentation for
11046** [sqlite3session_create()] for details.
11047*/
11048SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
11049
11050
11051/*
11052** CAPI3REF: Enable Or Disable A Session Object
11053** METHOD: sqlite3_session
11054**
11055** Enable or disable the recording of changes by a session object. When
11056** enabled, a session object records changes made to the database. When
11057** disabled - it does not. A newly created session object is enabled.
11058** Refer to the documentation for [sqlite3session_changeset()] for further
11059** details regarding how enabling and disabling a session object affects
11060** the eventual changesets.
11061**
11062** Passing zero to this function disables the session. Passing a value
11063** greater than zero enables it. Passing a value less than zero is a
11064** no-op, and may be used to query the current state of the session.
11065**
11066** The return value indicates the final state of the session object: 0 if
11067** the session is disabled, or 1 if it is enabled.
11068*/
11069SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
11070
11071/*
11072** CAPI3REF: Set Or Clear the Indirect Change Flag
11073** METHOD: sqlite3_session
11074**
11075** Each change recorded by a session object is marked as either direct or
11076** indirect. A change is marked as indirect if either:
11077**
11078** <ul>
11079**   <li> The session object "indirect" flag is set when the change is
11080**        made, or
11081**   <li> The change is made by an SQL trigger or foreign key action
11082**        instead of directly as a result of a users SQL statement.
11083** </ul>
11084**
11085** If a single row is affected by more than one operation within a session,
11086** then the change is considered indirect if all operations meet the criteria
11087** for an indirect change above, or direct otherwise.
11088**
11089** This function is used to set, clear or query the session object indirect
11090** flag.  If the second argument passed to this function is zero, then the
11091** indirect flag is cleared. If it is greater than zero, the indirect flag
11092** is set. Passing a value less than zero does not modify the current value
11093** of the indirect flag, and may be used to query the current state of the
11094** indirect flag for the specified session object.
11095**
11096** The return value indicates the final state of the indirect flag: 0 if
11097** it is clear, or 1 if it is set.
11098*/
11099SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
11100
11101/*
11102** CAPI3REF: Attach A Table To A Session Object
11103** METHOD: sqlite3_session
11104**
11105** If argument zTab is not NULL, then it is the name of a table to attach
11106** to the session object passed as the first argument. All subsequent changes
11107** made to the table while the session object is enabled will be recorded. See
11108** documentation for [sqlite3session_changeset()] for further details.
11109**
11110** Or, if argument zTab is NULL, then changes are recorded for all tables
11111** in the database. If additional tables are added to the database (by
11112** executing "CREATE TABLE" statements) after this call is made, changes for
11113** the new tables are also recorded.
11114**
11115** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
11116** defined as part of their CREATE TABLE statement. It does not matter if the
11117** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
11118** KEY may consist of a single column, or may be a composite key.
11119**
11120** It is not an error if the named table does not exist in the database. Nor
11121** is it an error if the named table does not have a PRIMARY KEY. However,
11122** no changes will be recorded in either of these scenarios.
11123**
11124** Changes are not recorded for individual rows that have NULL values stored
11125** in one or more of their PRIMARY KEY columns.
11126**
11127** SQLITE_OK is returned if the call completes without error. Or, if an error
11128** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
11129**
11130** <h3>Special sqlite_stat1 Handling</h3>
11131**
11132** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to
11133** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
11134**  <pre>
11135**  &nbsp;     CREATE TABLE sqlite_stat1(tbl,idx,stat)
11136**  </pre>
11137**
11138** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are
11139** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes
11140** are recorded for rows for which (idx IS NULL) is true. However, for such
11141** rows a zero-length blob (SQL value X'') is stored in the changeset or
11142** patchset instead of a NULL value. This allows such changesets to be
11143** manipulated by legacy implementations of sqlite3changeset_invert(),
11144** concat() and similar.
11145**
11146** The sqlite3changeset_apply() function automatically converts the
11147** zero-length blob back to a NULL value when updating the sqlite_stat1
11148** table. However, if the application calls sqlite3changeset_new(),
11149** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset
11150** iterator directly (including on a changeset iterator passed to a
11151** conflict-handler callback) then the X'' value is returned. The application
11152** must translate X'' to NULL itself if required.
11153**
11154** Legacy (older than 3.22.0) versions of the sessions module cannot capture
11155** changes made to the sqlite_stat1 table. Legacy versions of the
11156** sqlite3changeset_apply() function silently ignore any modifications to the
11157** sqlite_stat1 table that are part of a changeset or patchset.
11158*/
11159SQLITE_API int sqlite3session_attach(
11160  sqlite3_session *pSession,      /* Session object */
11161  const char *zTab                /* Table name */
11162);
11163
11164/*
11165** CAPI3REF: Set a table filter on a Session Object.
11166** METHOD: sqlite3_session
11167**
11168** The second argument (xFilter) is the "filter callback". For changes to rows
11169** in tables that are not attached to the Session object, the filter is called
11170** to determine whether changes to the table's rows should be tracked or not.
11171** If xFilter returns 0, changes are not tracked. Note that once a table is
11172** attached, xFilter will not be called again.
11173*/
11174SQLITE_API void sqlite3session_table_filter(
11175  sqlite3_session *pSession,      /* Session object */
11176  int(*xFilter)(
11177    void *pCtx,                   /* Copy of third arg to _filter_table() */
11178    const char *zTab              /* Table name */
11179  ),
11180  void *pCtx                      /* First argument passed to xFilter */
11181);
11182
11183/*
11184** CAPI3REF: Generate A Changeset From A Session Object
11185** METHOD: sqlite3_session
11186**
11187** Obtain a changeset containing changes to the tables attached to the
11188** session object passed as the first argument. If successful,
11189** set *ppChangeset to point to a buffer containing the changeset
11190** and *pnChangeset to the size of the changeset in bytes before returning
11191** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
11192** zero and return an SQLite error code.
11193**
11194** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
11195** each representing a change to a single row of an attached table. An INSERT
11196** change contains the values of each field of a new database row. A DELETE
11197** contains the original values of each field of a deleted database row. An
11198** UPDATE change contains the original values of each field of an updated
11199** database row along with the updated values for each updated non-primary-key
11200** column. It is not possible for an UPDATE change to represent a change that
11201** modifies the values of primary key columns. If such a change is made, it
11202** is represented in a changeset as a DELETE followed by an INSERT.
11203**
11204** Changes are not recorded for rows that have NULL values stored in one or
11205** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
11206** no corresponding change is present in the changesets returned by this
11207** function. If an existing row with one or more NULL values stored in
11208** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
11209** only an INSERT is appears in the changeset. Similarly, if an existing row
11210** with non-NULL PRIMARY KEY values is updated so that one or more of its
11211** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
11212** DELETE change only.
11213**
11214** The contents of a changeset may be traversed using an iterator created
11215** using the [sqlite3changeset_start()] API. A changeset may be applied to
11216** a database with a compatible schema using the [sqlite3changeset_apply()]
11217** API.
11218**
11219** Within a changeset generated by this function, all changes related to a
11220** single table are grouped together. In other words, when iterating through
11221** a changeset or when applying a changeset to a database, all changes related
11222** to a single table are processed before moving on to the next table. Tables
11223** are sorted in the same order in which they were attached (or auto-attached)
11224** to the sqlite3_session object. The order in which the changes related to
11225** a single table are stored is undefined.
11226**
11227** Following a successful call to this function, it is the responsibility of
11228** the caller to eventually free the buffer that *ppChangeset points to using
11229** [sqlite3_free()].
11230**
11231** <h3>Changeset Generation</h3>
11232**
11233** Once a table has been attached to a session object, the session object
11234** records the primary key values of all new rows inserted into the table.
11235** It also records the original primary key and other column values of any
11236** deleted or updated rows. For each unique primary key value, data is only
11237** recorded once - the first time a row with said primary key is inserted,
11238** updated or deleted in the lifetime of the session.
11239**
11240** There is one exception to the previous paragraph: when a row is inserted,
11241** updated or deleted, if one or more of its primary key columns contain a
11242** NULL value, no record of the change is made.
11243**
11244** The session object therefore accumulates two types of records - those
11245** that consist of primary key values only (created when the user inserts
11246** a new record) and those that consist of the primary key values and the
11247** original values of other table columns (created when the users deletes
11248** or updates a record).
11249**
11250** When this function is called, the requested changeset is created using
11251** both the accumulated records and the current contents of the database
11252** file. Specifically:
11253**
11254** <ul>
11255**   <li> For each record generated by an insert, the database is queried
11256**        for a row with a matching primary key. If one is found, an INSERT
11257**        change is added to the changeset. If no such row is found, no change
11258**        is added to the changeset.
11259**
11260**   <li> For each record generated by an update or delete, the database is
11261**        queried for a row with a matching primary key. If such a row is
11262**        found and one or more of the non-primary key fields have been
11263**        modified from their original values, an UPDATE change is added to
11264**        the changeset. Or, if no such row is found in the table, a DELETE
11265**        change is added to the changeset. If there is a row with a matching
11266**        primary key in the database, but all fields contain their original
11267**        values, no change is added to the changeset.
11268** </ul>
11269**
11270** This means, amongst other things, that if a row is inserted and then later
11271** deleted while a session object is active, neither the insert nor the delete
11272** will be present in the changeset. Or if a row is deleted and then later a
11273** row with the same primary key values inserted while a session object is
11274** active, the resulting changeset will contain an UPDATE change instead of
11275** a DELETE and an INSERT.
11276**
11277** When a session object is disabled (see the [sqlite3session_enable()] API),
11278** it does not accumulate records when rows are inserted, updated or deleted.
11279** This may appear to have some counter-intuitive effects if a single row
11280** is written to more than once during a session. For example, if a row
11281** is inserted while a session object is enabled, then later deleted while
11282** the same session object is disabled, no INSERT record will appear in the
11283** changeset, even though the delete took place while the session was disabled.
11284** Or, if one field of a row is updated while a session is disabled, and
11285** another field of the same row is updated while the session is enabled, the
11286** resulting changeset will contain an UPDATE change that updates both fields.
11287*/
11288SQLITE_API int sqlite3session_changeset(
11289  sqlite3_session *pSession,      /* Session object */
11290  int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
11291  void **ppChangeset              /* OUT: Buffer containing changeset */
11292);
11293
11294/*
11295** CAPI3REF: Load The Difference Between Tables Into A Session
11296** METHOD: sqlite3_session
11297**
11298** If it is not already attached to the session object passed as the first
11299** argument, this function attaches table zTbl in the same manner as the
11300** [sqlite3session_attach()] function. If zTbl does not exist, or if it
11301** does not have a primary key, this function is a no-op (but does not return
11302** an error).
11303**
11304** Argument zFromDb must be the name of a database ("main", "temp" etc.)
11305** attached to the same database handle as the session object that contains
11306** a table compatible with the table attached to the session by this function.
11307** A table is considered compatible if it:
11308**
11309** <ul>
11310**   <li> Has the same name,
11311**   <li> Has the same set of columns declared in the same order, and
11312**   <li> Has the same PRIMARY KEY definition.
11313** </ul>
11314**
11315** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
11316** are compatible but do not have any PRIMARY KEY columns, it is not an error
11317** but no changes are added to the session object. As with other session
11318** APIs, tables without PRIMARY KEYs are simply ignored.
11319**
11320** This function adds a set of changes to the session object that could be
11321** used to update the table in database zFrom (call this the "from-table")
11322** so that its content is the same as the table attached to the session
11323** object (call this the "to-table"). Specifically:
11324**
11325** <ul>
11326**   <li> For each row (primary key) that exists in the to-table but not in
11327**     the from-table, an INSERT record is added to the session object.
11328**
11329**   <li> For each row (primary key) that exists in the to-table but not in
11330**     the from-table, a DELETE record is added to the session object.
11331**
11332**   <li> For each row (primary key) that exists in both tables, but features
11333**     different non-PK values in each, an UPDATE record is added to the
11334**     session.
11335** </ul>
11336**
11337** To clarify, if this function is called and then a changeset constructed
11338** using [sqlite3session_changeset()], then after applying that changeset to
11339** database zFrom the contents of the two compatible tables would be
11340** identical.
11341**
11342** It an error if database zFrom does not exist or does not contain the
11343** required compatible table.
11344**
11345** If the operation is successful, SQLITE_OK is returned. Otherwise, an SQLite
11346** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
11347** may be set to point to a buffer containing an English language error
11348** message. It is the responsibility of the caller to free this buffer using
11349** sqlite3_free().
11350*/
11351SQLITE_API int sqlite3session_diff(
11352  sqlite3_session *pSession,
11353  const char *zFromDb,
11354  const char *zTbl,
11355  char **pzErrMsg
11356);
11357
11358
11359/*
11360** CAPI3REF: Generate A Patchset From A Session Object
11361** METHOD: sqlite3_session
11362**
11363** The differences between a patchset and a changeset are that:
11364**
11365** <ul>
11366**   <li> DELETE records consist of the primary key fields only. The
11367**        original values of other fields are omitted.
11368**   <li> The original values of any modified fields are omitted from
11369**        UPDATE records.
11370** </ul>
11371**
11372** A patchset blob may be used with up to date versions of all
11373** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(),
11374** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
11375** attempting to use a patchset blob with old versions of the
11376** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error.
11377**
11378** Because the non-primary key "old.*" fields are omitted, no
11379** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
11380** is passed to the sqlite3changeset_apply() API. Other conflict types work
11381** in the same way as for changesets.
11382**
11383** Changes within a patchset are ordered in the same way as for changesets
11384** generated by the sqlite3session_changeset() function (i.e. all changes for
11385** a single table are grouped together, tables appear in the order in which
11386** they were attached to the session object).
11387*/
11388SQLITE_API int sqlite3session_patchset(
11389  sqlite3_session *pSession,      /* Session object */
11390  int *pnPatchset,                /* OUT: Size of buffer at *ppPatchset */
11391  void **ppPatchset               /* OUT: Buffer containing patchset */
11392);
11393
11394/*
11395** CAPI3REF: Test if a changeset has recorded any changes.
11396**
11397** Return non-zero if no changes to attached tables have been recorded by
11398** the session object passed as the first argument. Otherwise, if one or
11399** more changes have been recorded, return zero.
11400**
11401** Even if this function returns zero, it is possible that calling
11402** [sqlite3session_changeset()] on the session handle may still return a
11403** changeset that contains no changes. This can happen when a row in
11404** an attached table is modified and then later on the original values
11405** are restored. However, if this function returns non-zero, then it is
11406** guaranteed that a call to sqlite3session_changeset() will return a
11407** changeset containing zero changes.
11408*/
11409SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
11410
11411/*
11412** CAPI3REF: Create An Iterator To Traverse A Changeset
11413** CONSTRUCTOR: sqlite3_changeset_iter
11414**
11415** Create an iterator used to iterate through the contents of a changeset.
11416** If successful, *pp is set to point to the iterator handle and SQLITE_OK
11417** is returned. Otherwise, if an error occurs, *pp is set to zero and an
11418** SQLite error code is returned.
11419**
11420** The following functions can be used to advance and query a changeset
11421** iterator created by this function:
11422**
11423** <ul>
11424**   <li> [sqlite3changeset_next()]
11425**   <li> [sqlite3changeset_op()]
11426**   <li> [sqlite3changeset_new()]
11427**   <li> [sqlite3changeset_old()]
11428** </ul>
11429**
11430** It is the responsibility of the caller to eventually destroy the iterator
11431** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
11432** changeset (pChangeset) must remain valid until after the iterator is
11433** destroyed.
11434**
11435** Assuming the changeset blob was created by one of the
11436** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
11437** [sqlite3changeset_invert()] functions, all changes within the changeset
11438** that apply to a single table are grouped together. This means that when
11439** an application iterates through a changeset using an iterator created by
11440** this function, all changes that relate to a single table are visited
11441** consecutively. There is no chance that the iterator will visit a change
11442** the applies to table X, then one for table Y, and then later on visit
11443** another change for table X.
11444**
11445** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
11446** may be modified by passing a combination of
11447** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
11448**
11449** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
11450** and therefore subject to change.
11451*/
11452SQLITE_API int sqlite3changeset_start(
11453  sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
11454  int nChangeset,                 /* Size of changeset blob in bytes */
11455  void *pChangeset                /* Pointer to blob containing changeset */
11456);
11457SQLITE_API int sqlite3changeset_start_v2(
11458  sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
11459  int nChangeset,                 /* Size of changeset blob in bytes */
11460  void *pChangeset,               /* Pointer to blob containing changeset */
11461  int flags                       /* SESSION_CHANGESETSTART_* flags */
11462);
11463
11464/*
11465** CAPI3REF: Flags for sqlite3changeset_start_v2
11466**
11467** The following flags may passed via the 4th parameter to
11468** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
11469**
11470** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
11471**   Invert the changeset while iterating through it. This is equivalent to
11472**   inverting a changeset using sqlite3changeset_invert() before applying it.
11473**   It is an error to specify this flag with a patchset.
11474*/
11475#define SQLITE_CHANGESETSTART_INVERT        0x0002
11476
11477
11478/*
11479** CAPI3REF: Advance A Changeset Iterator
11480** METHOD: sqlite3_changeset_iter
11481**
11482** This function may only be used with iterators created by the function
11483** [sqlite3changeset_start()]. If it is called on an iterator passed to
11484** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
11485** is returned and the call has no effect.
11486**
11487** Immediately after an iterator is created by sqlite3changeset_start(), it
11488** does not point to any change in the changeset. Assuming the changeset
11489** is not empty, the first call to this function advances the iterator to
11490** point to the first change in the changeset. Each subsequent call advances
11491** the iterator to point to the next change in the changeset (if any). If
11492** no error occurs and the iterator points to a valid change after a call
11493** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned.
11494** Otherwise, if all changes in the changeset have already been visited,
11495** SQLITE_DONE is returned.
11496**
11497** If an error occurs, an SQLite error code is returned. Possible error
11498** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
11499** SQLITE_NOMEM.
11500*/
11501SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
11502
11503/*
11504** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
11505** METHOD: sqlite3_changeset_iter
11506**
11507** The pIter argument passed to this function may either be an iterator
11508** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11509** created by [sqlite3changeset_start()]. In the latter case, the most recent
11510** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
11511** is not the case, this function returns [SQLITE_MISUSE].
11512**
11513** If argument pzTab is not NULL, then *pzTab is set to point to a
11514** nul-terminated utf-8 encoded string containing the name of the table
11515** affected by the current change. The buffer remains valid until either
11516** sqlite3changeset_next() is called on the iterator or until the
11517** conflict-handler function returns. If pnCol is not NULL, then *pnCol is
11518** set to the number of columns in the table affected by the change. If
11519** pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change
11520** is an indirect change, or false (0) otherwise. See the documentation for
11521** [sqlite3session_indirect()] for a description of direct and indirect
11522** changes. Finally, if pOp is not NULL, then *pOp is set to one of
11523** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the
11524** type of change that the iterator currently points to.
11525**
11526** If no error occurs, SQLITE_OK is returned. If an error does occur, an
11527** SQLite error code is returned. The values of the output variables may not
11528** be trusted in this case.
11529*/
11530SQLITE_API int sqlite3changeset_op(
11531  sqlite3_changeset_iter *pIter,  /* Iterator object */
11532  const char **pzTab,             /* OUT: Pointer to table name */
11533  int *pnCol,                     /* OUT: Number of columns in table */
11534  int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
11535  int *pbIndirect                 /* OUT: True for an 'indirect' change */
11536);
11537
11538/*
11539** CAPI3REF: Obtain The Primary Key Definition Of A Table
11540** METHOD: sqlite3_changeset_iter
11541**
11542** For each modified table, a changeset includes the following:
11543**
11544** <ul>
11545**   <li> The number of columns in the table, and
11546**   <li> Which of those columns make up the tables PRIMARY KEY.
11547** </ul>
11548**
11549** This function is used to find which columns comprise the PRIMARY KEY of
11550** the table modified by the change that iterator pIter currently points to.
11551** If successful, *pabPK is set to point to an array of nCol entries, where
11552** nCol is the number of columns in the table. Elements of *pabPK are set to
11553** 0x01 if the corresponding column is part of the tables primary key, or
11554** 0x00 if it is not.
11555**
11556** If argument pnCol is not NULL, then *pnCol is set to the number of columns
11557** in the table.
11558**
11559** If this function is called when the iterator does not point to a valid
11560** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
11561** SQLITE_OK is returned and the output variables populated as described
11562** above.
11563*/
11564SQLITE_API int sqlite3changeset_pk(
11565  sqlite3_changeset_iter *pIter,  /* Iterator object */
11566  unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
11567  int *pnCol                      /* OUT: Number of entries in output array */
11568);
11569
11570/*
11571** CAPI3REF: Obtain old.* Values From A Changeset Iterator
11572** METHOD: sqlite3_changeset_iter
11573**
11574** The pIter argument passed to this function may either be an iterator
11575** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11576** created by [sqlite3changeset_start()]. In the latter case, the most recent
11577** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
11578** Furthermore, it may only be called if the type of change that the iterator
11579** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
11580** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
11581**
11582** Argument iVal must be greater than or equal to 0, and less than the number
11583** of columns in the table affected by the current change. Otherwise,
11584** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11585**
11586** If successful, this function sets *ppValue to point to a protected
11587** sqlite3_value object containing the iVal'th value from the vector of
11588** original row values stored as part of the UPDATE or DELETE change and
11589** returns SQLITE_OK. The name of the function comes from the fact that this
11590** is similar to the "old.*" columns available to update or delete triggers.
11591**
11592** If some other error occurs (e.g. an OOM condition), an SQLite error code
11593** is returned and *ppValue is set to NULL.
11594*/
11595SQLITE_API int sqlite3changeset_old(
11596  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
11597  int iVal,                       /* Column number */
11598  sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
11599);
11600
11601/*
11602** CAPI3REF: Obtain new.* Values From A Changeset Iterator
11603** METHOD: sqlite3_changeset_iter
11604**
11605** The pIter argument passed to this function may either be an iterator
11606** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11607** created by [sqlite3changeset_start()]. In the latter case, the most recent
11608** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
11609** Furthermore, it may only be called if the type of change that the iterator
11610** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
11611** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
11612**
11613** Argument iVal must be greater than or equal to 0, and less than the number
11614** of columns in the table affected by the current change. Otherwise,
11615** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11616**
11617** If successful, this function sets *ppValue to point to a protected
11618** sqlite3_value object containing the iVal'th value from the vector of
11619** new row values stored as part of the UPDATE or INSERT change and
11620** returns SQLITE_OK. If the change is an UPDATE and does not include
11621** a new value for the requested column, *ppValue is set to NULL and
11622** SQLITE_OK returned. The name of the function comes from the fact that
11623** this is similar to the "new.*" columns available to update or delete
11624** triggers.
11625**
11626** If some other error occurs (e.g. an OOM condition), an SQLite error code
11627** is returned and *ppValue is set to NULL.
11628*/
11629SQLITE_API int sqlite3changeset_new(
11630  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
11631  int iVal,                       /* Column number */
11632  sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
11633);
11634
11635/*
11636** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
11637** METHOD: sqlite3_changeset_iter
11638**
11639** This function should only be used with iterator objects passed to a
11640** conflict-handler callback by [sqlite3changeset_apply()] with either
11641** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
11642** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
11643** is set to NULL.
11644**
11645** Argument iVal must be greater than or equal to 0, and less than the number
11646** of columns in the table affected by the current change. Otherwise,
11647** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11648**
11649** If successful, this function sets *ppValue to point to a protected
11650** sqlite3_value object containing the iVal'th value from the
11651** "conflicting row" associated with the current conflict-handler callback
11652** and returns SQLITE_OK.
11653**
11654** If some other error occurs (e.g. an OOM condition), an SQLite error code
11655** is returned and *ppValue is set to NULL.
11656*/
11657SQLITE_API int sqlite3changeset_conflict(
11658  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
11659  int iVal,                       /* Column number */
11660  sqlite3_value **ppValue         /* OUT: Value from conflicting row */
11661);
11662
11663/*
11664** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
11665** METHOD: sqlite3_changeset_iter
11666**
11667** This function may only be called with an iterator passed to an
11668** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
11669** it sets the output variable to the total number of known foreign key
11670** violations in the destination database and returns SQLITE_OK.
11671**
11672** In all other cases this function returns SQLITE_MISUSE.
11673*/
11674SQLITE_API int sqlite3changeset_fk_conflicts(
11675  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
11676  int *pnOut                      /* OUT: Number of FK violations */
11677);
11678
11679
11680/*
11681** CAPI3REF: Finalize A Changeset Iterator
11682** METHOD: sqlite3_changeset_iter
11683**
11684** This function is used to finalize an iterator allocated with
11685** [sqlite3changeset_start()].
11686**
11687** This function should only be called on iterators created using the
11688** [sqlite3changeset_start()] function. If an application calls this
11689** function with an iterator passed to a conflict-handler by
11690** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
11691** call has no effect.
11692**
11693** If an error was encountered within a call to an sqlite3changeset_xxx()
11694** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
11695** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
11696** to that error is returned by this function. Otherwise, SQLITE_OK is
11697** returned. This is to allow the following pattern (pseudo-code):
11698**
11699** <pre>
11700**   sqlite3changeset_start();
11701**   while( SQLITE_ROW==sqlite3changeset_next() ){
11702**     // Do something with change.
11703**   }
11704**   rc = sqlite3changeset_finalize();
11705**   if( rc!=SQLITE_OK ){
11706**     // An error has occurred
11707**   }
11708** </pre>
11709*/
11710SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
11711
11712/*
11713** CAPI3REF: Invert A Changeset
11714**
11715** This function is used to "invert" a changeset object. Applying an inverted
11716** changeset to a database reverses the effects of applying the uninverted
11717** changeset. Specifically:
11718**
11719** <ul>
11720**   <li> Each DELETE change is changed to an INSERT, and
11721**   <li> Each INSERT change is changed to a DELETE, and
11722**   <li> For each UPDATE change, the old.* and new.* values are exchanged.
11723** </ul>
11724**
11725** This function does not change the order in which changes appear within
11726** the changeset. It merely reverses the sense of each individual change.
11727**
11728** If successful, a pointer to a buffer containing the inverted changeset
11729** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
11730** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
11731** zeroed and an SQLite error code returned.
11732**
11733** It is the responsibility of the caller to eventually call sqlite3_free()
11734** on the *ppOut pointer to free the buffer allocation following a successful
11735** call to this function.
11736**
11737** WARNING/TODO: This function currently assumes that the input is a valid
11738** changeset. If it is not, the results are undefined.
11739*/
11740SQLITE_API int sqlite3changeset_invert(
11741  int nIn, const void *pIn,       /* Input changeset */
11742  int *pnOut, void **ppOut        /* OUT: Inverse of input */
11743);
11744
11745/*
11746** CAPI3REF: Concatenate Two Changeset Objects
11747**
11748** This function is used to concatenate two changesets, A and B, into a
11749** single changeset. The result is a changeset equivalent to applying
11750** changeset A followed by changeset B.
11751**
11752** This function combines the two input changesets using an
11753** sqlite3_changegroup object. Calling it produces similar results as the
11754** following code fragment:
11755**
11756** <pre>
11757**   sqlite3_changegroup *pGrp;
11758**   rc = sqlite3_changegroup_new(&pGrp);
11759**   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
11760**   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
11761**   if( rc==SQLITE_OK ){
11762**     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
11763**   }else{
11764**     *ppOut = 0;
11765**     *pnOut = 0;
11766**   }
11767** </pre>
11768**
11769** Refer to the sqlite3_changegroup documentation below for details.
11770*/
11771SQLITE_API int sqlite3changeset_concat(
11772  int nA,                         /* Number of bytes in buffer pA */
11773  void *pA,                       /* Pointer to buffer containing changeset A */
11774  int nB,                         /* Number of bytes in buffer pB */
11775  void *pB,                       /* Pointer to buffer containing changeset B */
11776  int *pnOut,                     /* OUT: Number of bytes in output changeset */
11777  void **ppOut                    /* OUT: Buffer containing output changeset */
11778);
11779
11780
11781/*
11782** CAPI3REF: Changegroup Handle
11783**
11784** A changegroup is an object used to combine two or more
11785** [changesets] or [patchsets]
11786*/
11787typedef struct sqlite3_changegroup sqlite3_changegroup;
11788
11789/*
11790** CAPI3REF: Create A New Changegroup Object
11791** CONSTRUCTOR: sqlite3_changegroup
11792**
11793** An sqlite3_changegroup object is used to combine two or more changesets
11794** (or patchsets) into a single changeset (or patchset). A single changegroup
11795** object may combine changesets or patchsets, but not both. The output is
11796** always in the same format as the input.
11797**
11798** If successful, this function returns SQLITE_OK and populates (*pp) with
11799** a pointer to a new sqlite3_changegroup object before returning. The caller
11800** should eventually free the returned object using a call to
11801** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
11802** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
11803**
11804** The usual usage pattern for an sqlite3_changegroup object is as follows:
11805**
11806** <ul>
11807**   <li> It is created using a call to sqlite3changegroup_new().
11808**
11809**   <li> Zero or more changesets (or patchsets) are added to the object
11810**        by calling sqlite3changegroup_add().
11811**
11812**   <li> The result of combining all input changesets together is obtained
11813**        by the application via a call to sqlite3changegroup_output().
11814**
11815**   <li> The object is deleted using a call to sqlite3changegroup_delete().
11816** </ul>
11817**
11818** Any number of calls to add() and output() may be made between the calls to
11819** new() and delete(), and in any order.
11820**
11821** As well as the regular sqlite3changegroup_add() and
11822** sqlite3changegroup_output() functions, also available are the streaming
11823** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
11824*/
11825SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
11826
11827/*
11828** CAPI3REF: Add A Changeset To A Changegroup
11829** METHOD: sqlite3_changegroup
11830**
11831** Add all changes within the changeset (or patchset) in buffer pData (size
11832** nData bytes) to the changegroup.
11833**
11834** If the buffer contains a patchset, then all prior calls to this function
11835** on the same changegroup object must also have specified patchsets. Or, if
11836** the buffer contains a changeset, so must have the earlier calls to this
11837** function. Otherwise, SQLITE_ERROR is returned and no changes are added
11838** to the changegroup.
11839**
11840** Rows within the changeset and changegroup are identified by the values in
11841** their PRIMARY KEY columns. A change in the changeset is considered to
11842** apply to the same row as a change already present in the changegroup if
11843** the two rows have the same primary key.
11844**
11845** Changes to rows that do not already appear in the changegroup are
11846** simply copied into it. Or, if both the new changeset and the changegroup
11847** contain changes that apply to a single row, the final contents of the
11848** changegroup depends on the type of each change, as follows:
11849**
11850** <table border=1 style="margin-left:8ex;margin-right:8ex">
11851**   <tr><th style="white-space:pre">Existing Change  </th>
11852**       <th style="white-space:pre">New Change       </th>
11853**       <th>Output Change
11854**   <tr><td>INSERT <td>INSERT <td>
11855**       The new change is ignored. This case does not occur if the new
11856**       changeset was recorded immediately after the changesets already
11857**       added to the changegroup.
11858**   <tr><td>INSERT <td>UPDATE <td>
11859**       The INSERT change remains in the changegroup. The values in the
11860**       INSERT change are modified as if the row was inserted by the
11861**       existing change and then updated according to the new change.
11862**   <tr><td>INSERT <td>DELETE <td>
11863**       The existing INSERT is removed from the changegroup. The DELETE is
11864**       not added.
11865**   <tr><td>UPDATE <td>INSERT <td>
11866**       The new change is ignored. This case does not occur if the new
11867**       changeset was recorded immediately after the changesets already
11868**       added to the changegroup.
11869**   <tr><td>UPDATE <td>UPDATE <td>
11870**       The existing UPDATE remains within the changegroup. It is amended
11871**       so that the accompanying values are as if the row was updated once
11872**       by the existing change and then again by the new change.
11873**   <tr><td>UPDATE <td>DELETE <td>
11874**       The existing UPDATE is replaced by the new DELETE within the
11875**       changegroup.
11876**   <tr><td>DELETE <td>INSERT <td>
11877**       If one or more of the column values in the row inserted by the
11878**       new change differ from those in the row deleted by the existing
11879**       change, the existing DELETE is replaced by an UPDATE within the
11880**       changegroup. Otherwise, if the inserted row is exactly the same
11881**       as the deleted row, the existing DELETE is simply discarded.
11882**   <tr><td>DELETE <td>UPDATE <td>
11883**       The new change is ignored. This case does not occur if the new
11884**       changeset was recorded immediately after the changesets already
11885**       added to the changegroup.
11886**   <tr><td>DELETE <td>DELETE <td>
11887**       The new change is ignored. This case does not occur if the new
11888**       changeset was recorded immediately after the changesets already
11889**       added to the changegroup.
11890** </table>
11891**
11892** If the new changeset contains changes to a table that is already present
11893** in the changegroup, then the number of columns and the position of the
11894** primary key columns for the table must be consistent. If this is not the
11895** case, this function fails with SQLITE_SCHEMA. If the input changeset
11896** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
11897** returned. Or, if an out-of-memory condition occurs during processing, this
11898** function returns SQLITE_NOMEM. In all cases, if an error occurs the state
11899** of the final contents of the changegroup is undefined.
11900**
11901** If no error occurs, SQLITE_OK is returned.
11902*/
11903SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
11904
11905/*
11906** CAPI3REF: Obtain A Composite Changeset From A Changegroup
11907** METHOD: sqlite3_changegroup
11908**
11909** Obtain a buffer containing a changeset (or patchset) representing the
11910** current contents of the changegroup. If the inputs to the changegroup
11911** were themselves changesets, the output is a changeset. Or, if the
11912** inputs were patchsets, the output is also a patchset.
11913**
11914** As with the output of the sqlite3session_changeset() and
11915** sqlite3session_patchset() functions, all changes related to a single
11916** table are grouped together in the output of this function. Tables appear
11917** in the same order as for the very first changeset added to the changegroup.
11918** If the second or subsequent changesets added to the changegroup contain
11919** changes for tables that do not appear in the first changeset, they are
11920** appended onto the end of the output changeset, again in the order in
11921** which they are first encountered.
11922**
11923** If an error occurs, an SQLite error code is returned and the output
11924** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
11925** is returned and the output variables are set to the size of and a
11926** pointer to the output buffer, respectively. In this case it is the
11927** responsibility of the caller to eventually free the buffer using a
11928** call to sqlite3_free().
11929*/
11930SQLITE_API int sqlite3changegroup_output(
11931  sqlite3_changegroup*,
11932  int *pnData,                    /* OUT: Size of output buffer in bytes */
11933  void **ppData                   /* OUT: Pointer to output buffer */
11934);
11935
11936/*
11937** CAPI3REF: Delete A Changegroup Object
11938** DESTRUCTOR: sqlite3_changegroup
11939*/
11940SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
11941
11942/*
11943** CAPI3REF: Apply A Changeset To A Database
11944**
11945** Apply a changeset or patchset to a database. These functions attempt to
11946** update the "main" database attached to handle db with the changes found in
11947** the changeset passed via the second and third arguments.
11948**
11949** The fourth argument (xFilter) passed to these functions is the "filter
11950** callback". If it is not NULL, then for each table affected by at least one
11951** change in the changeset, the filter callback is invoked with
11952** the table name as the second argument, and a copy of the context pointer
11953** passed as the sixth argument as the first. If the "filter callback"
11954** returns zero, then no attempt is made to apply any changes to the table.
11955** Otherwise, if the return value is non-zero or the xFilter argument to
11956** is NULL, all changes related to the table are attempted.
11957**
11958** For each table that is not excluded by the filter callback, this function
11959** tests that the target database contains a compatible table. A table is
11960** considered compatible if all of the following are true:
11961**
11962** <ul>
11963**   <li> The table has the same name as the name recorded in the
11964**        changeset, and
11965**   <li> The table has at least as many columns as recorded in the
11966**        changeset, and
11967**   <li> The table has primary key columns in the same position as
11968**        recorded in the changeset.
11969** </ul>
11970**
11971** If there is no compatible table, it is not an error, but none of the
11972** changes associated with the table are applied. A warning message is issued
11973** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
11974** one such warning is issued for each table in the changeset.
11975**
11976** For each change for which there is a compatible table, an attempt is made
11977** to modify the table contents according to the UPDATE, INSERT or DELETE
11978** change. If a change cannot be applied cleanly, the conflict handler
11979** function passed as the fifth argument to sqlite3changeset_apply() may be
11980** invoked. A description of exactly when the conflict handler is invoked for
11981** each type of change is below.
11982**
11983** Unlike the xFilter argument, xConflict may not be passed NULL. The results
11984** of passing anything other than a valid function pointer as the xConflict
11985** argument are undefined.
11986**
11987** Each time the conflict handler function is invoked, it must return one
11988** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or
11989** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
11990** if the second argument passed to the conflict handler is either
11991** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
11992** returns an illegal value, any changes already made are rolled back and
11993** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different
11994** actions are taken by sqlite3changeset_apply() depending on the value
11995** returned by each invocation of the conflict-handler function. Refer to
11996** the documentation for the three
11997** [SQLITE_CHANGESET_OMIT|available return values] for details.
11998**
11999** <dl>
12000** <dt>DELETE Changes<dd>
12001**   For each DELETE change, the function checks if the target database
12002**   contains a row with the same primary key value (or values) as the
12003**   original row values stored in the changeset. If it does, and the values
12004**   stored in all non-primary key columns also match the values stored in
12005**   the changeset the row is deleted from the target database.
12006**
12007**   If a row with matching primary key values is found, but one or more of
12008**   the non-primary key fields contains a value different from the original
12009**   row value stored in the changeset, the conflict-handler function is
12010**   invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
12011**   database table has more columns than are recorded in the changeset,
12012**   only the values of those non-primary key fields are compared against
12013**   the current database contents - any trailing database table columns
12014**   are ignored.
12015**
12016**   If no row with matching primary key values is found in the database,
12017**   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12018**   passed as the second argument.
12019**
12020**   If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
12021**   (which can only happen if a foreign key constraint is violated), the
12022**   conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
12023**   passed as the second argument. This includes the case where the DELETE
12024**   operation is attempted because an earlier call to the conflict handler
12025**   function returned [SQLITE_CHANGESET_REPLACE].
12026**
12027** <dt>INSERT Changes<dd>
12028**   For each INSERT change, an attempt is made to insert the new row into
12029**   the database. If the changeset row contains fewer fields than the
12030**   database table, the trailing fields are populated with their default
12031**   values.
12032**
12033**   If the attempt to insert the row fails because the database already
12034**   contains a row with the same primary key values, the conflict handler
12035**   function is invoked with the second argument set to
12036**   [SQLITE_CHANGESET_CONFLICT].
12037**
12038**   If the attempt to insert the row fails because of some other constraint
12039**   violation (e.g. NOT NULL or UNIQUE), the conflict handler function is
12040**   invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
12041**   This includes the case where the INSERT operation is re-attempted because
12042**   an earlier call to the conflict handler function returned
12043**   [SQLITE_CHANGESET_REPLACE].
12044**
12045** <dt>UPDATE Changes<dd>
12046**   For each UPDATE change, the function checks if the target database
12047**   contains a row with the same primary key value (or values) as the
12048**   original row values stored in the changeset. If it does, and the values
12049**   stored in all modified non-primary key columns also match the values
12050**   stored in the changeset the row is updated within the target database.
12051**
12052**   If a row with matching primary key values is found, but one or more of
12053**   the modified non-primary key fields contains a value different from an
12054**   original row value stored in the changeset, the conflict-handler function
12055**   is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
12056**   UPDATE changes only contain values for non-primary key fields that are
12057**   to be modified, only those fields need to match the original values to
12058**   avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
12059**
12060**   If no row with matching primary key values is found in the database,
12061**   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12062**   passed as the second argument.
12063**
12064**   If the UPDATE operation is attempted, but SQLite returns
12065**   SQLITE_CONSTRAINT, the conflict-handler function is invoked with
12066**   [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
12067**   This includes the case where the UPDATE operation is attempted after
12068**   an earlier call to the conflict handler function returned
12069**   [SQLITE_CHANGESET_REPLACE].
12070** </dl>
12071**
12072** It is safe to execute SQL statements, including those that write to the
12073** table that the callback related to, from within the xConflict callback.
12074** This can be used to further customize the application's conflict
12075** resolution strategy.
12076**
12077** All changes made by these functions are enclosed in a savepoint transaction.
12078** If any other error (aside from a constraint failure when attempting to
12079** write to the target database) occurs, then the savepoint transaction is
12080** rolled back, restoring the target database to its original state, and an
12081** SQLite error code returned.
12082**
12083** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
12084** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
12085** may set (*ppRebase) to point to a "rebase" that may be used with the
12086** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
12087** is set to the size of the buffer in bytes. It is the responsibility of the
12088** caller to eventually free any such buffer using sqlite3_free(). The buffer
12089** is only allocated and populated if one or more conflicts were encountered
12090** while applying the patchset. See comments surrounding the sqlite3_rebaser
12091** APIs for further details.
12092**
12093** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
12094** may be modified by passing a combination of
12095** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
12096**
12097** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
12098** and therefore subject to change.
12099*/
12100SQLITE_API int sqlite3changeset_apply(
12101  sqlite3 *db,                    /* Apply change to "main" db of this handle */
12102  int nChangeset,                 /* Size of changeset in bytes */
12103  void *pChangeset,               /* Changeset blob */
12104  int(*xFilter)(
12105    void *pCtx,                   /* Copy of sixth arg to _apply() */
12106    const char *zTab              /* Table name */
12107  ),
12108  int(*xConflict)(
12109    void *pCtx,                   /* Copy of sixth arg to _apply() */
12110    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
12111    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
12112  ),
12113  void *pCtx                      /* First argument passed to xConflict */
12114);
12115SQLITE_API int sqlite3changeset_apply_v2(
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  void **ppRebase, int *pnRebase, /* OUT: Rebase data */
12130  int flags                       /* SESSION_CHANGESETAPPLY_* flags */
12131);
12132
12133/*
12134** CAPI3REF: Flags for sqlite3changeset_apply_v2
12135**
12136** The following flags may passed via the 9th parameter to
12137** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
12138**
12139** <dl>
12140** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
12141**   Usually, the sessions module encloses all operations performed by
12142**   a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
12143**   SAVEPOINT is committed if the changeset or patchset is successfully
12144**   applied, or rolled back if an error occurs. Specifying this flag
12145**   causes the sessions module to omit this savepoint. In this case, if the
12146**   caller has an open transaction or savepoint when apply_v2() is called,
12147**   it may revert the partially applied changeset by rolling it back.
12148**
12149** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
12150**   Invert the changeset before applying it. This is equivalent to inverting
12151**   a changeset using sqlite3changeset_invert() before applying it. It is
12152**   an error to specify this flag with a patchset.
12153*/
12154#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT   0x0001
12155#define SQLITE_CHANGESETAPPLY_INVERT        0x0002
12156
12157/*
12158** CAPI3REF: Constants Passed To The Conflict Handler
12159**
12160** Values that may be passed as the second argument to a conflict-handler.
12161**
12162** <dl>
12163** <dt>SQLITE_CHANGESET_DATA<dd>
12164**   The conflict handler is invoked with CHANGESET_DATA as the second argument
12165**   when processing a DELETE or UPDATE change if a row with the required
12166**   PRIMARY KEY fields is present in the database, but one or more other
12167**   (non primary-key) fields modified by the update do not contain the
12168**   expected "before" values.
12169**
12170**   The conflicting row, in this case, is the database row with the matching
12171**   primary key.
12172**
12173** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
12174**   The conflict handler is invoked with CHANGESET_NOTFOUND as the second
12175**   argument when processing a DELETE or UPDATE change if a row with the
12176**   required PRIMARY KEY fields is not present in the database.
12177**
12178**   There is no conflicting row in this case. The results of invoking the
12179**   sqlite3changeset_conflict() API are undefined.
12180**
12181** <dt>SQLITE_CHANGESET_CONFLICT<dd>
12182**   CHANGESET_CONFLICT is passed as the second argument to the conflict
12183**   handler while processing an INSERT change if the operation would result
12184**   in duplicate primary key values.
12185**
12186**   The conflicting row in this case is the database row with the matching
12187**   primary key.
12188**
12189** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
12190**   If foreign key handling is enabled, and applying a changeset leaves the
12191**   database in a state containing foreign key violations, the conflict
12192**   handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
12193**   exactly once before the changeset is committed. If the conflict handler
12194**   returns CHANGESET_OMIT, the changes, including those that caused the
12195**   foreign key constraint violation, are committed. Or, if it returns
12196**   CHANGESET_ABORT, the changeset is rolled back.
12197**
12198**   No current or conflicting row information is provided. The only function
12199**   it is possible to call on the supplied sqlite3_changeset_iter handle
12200**   is sqlite3changeset_fk_conflicts().
12201**
12202** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
12203**   If any other constraint violation occurs while applying a change (i.e.
12204**   a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is
12205**   invoked with CHANGESET_CONSTRAINT as the second argument.
12206**
12207**   There is no conflicting row in this case. The results of invoking the
12208**   sqlite3changeset_conflict() API are undefined.
12209**
12210** </dl>
12211*/
12212#define SQLITE_CHANGESET_DATA        1
12213#define SQLITE_CHANGESET_NOTFOUND    2
12214#define SQLITE_CHANGESET_CONFLICT    3
12215#define SQLITE_CHANGESET_CONSTRAINT  4
12216#define SQLITE_CHANGESET_FOREIGN_KEY 5
12217
12218/*
12219** CAPI3REF: Constants Returned By The Conflict Handler
12220**
12221** A conflict handler callback must return one of the following three values.
12222**
12223** <dl>
12224** <dt>SQLITE_CHANGESET_OMIT<dd>
12225**   If a conflict handler returns this value no special action is taken. The
12226**   change that caused the conflict is not applied. The session module
12227**   continues to the next change in the changeset.
12228**
12229** <dt>SQLITE_CHANGESET_REPLACE<dd>
12230**   This value may only be returned if the second argument to the conflict
12231**   handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
12232**   is not the case, any changes applied so far are rolled back and the
12233**   call to sqlite3changeset_apply() returns SQLITE_MISUSE.
12234**
12235**   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
12236**   handler, then the conflicting row is either updated or deleted, depending
12237**   on the type of change.
12238**
12239**   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
12240**   handler, then the conflicting row is removed from the database and a
12241**   second attempt to apply the change is made. If this second attempt fails,
12242**   the original row is restored to the database before continuing.
12243**
12244** <dt>SQLITE_CHANGESET_ABORT<dd>
12245**   If this value is returned, any changes applied so far are rolled back
12246**   and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
12247** </dl>
12248*/
12249#define SQLITE_CHANGESET_OMIT       0
12250#define SQLITE_CHANGESET_REPLACE    1
12251#define SQLITE_CHANGESET_ABORT      2
12252
12253/*
12254** CAPI3REF: Rebasing changesets
12255** EXPERIMENTAL
12256**
12257** Suppose there is a site hosting a database in state S0. And that
12258** modifications are made that move that database to state S1 and a
12259** changeset recorded (the "local" changeset). Then, a changeset based
12260** on S0 is received from another site (the "remote" changeset) and
12261** applied to the database. The database is then in state
12262** (S1+"remote"), where the exact state depends on any conflict
12263** resolution decisions (OMIT or REPLACE) made while applying "remote".
12264** Rebasing a changeset is to update it to take those conflict
12265** resolution decisions into account, so that the same conflicts
12266** do not have to be resolved elsewhere in the network.
12267**
12268** For example, if both the local and remote changesets contain an
12269** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
12270**
12271**   local:  INSERT INTO t1 VALUES(1, 'v1');
12272**   remote: INSERT INTO t1 VALUES(1, 'v2');
12273**
12274** and the conflict resolution is REPLACE, then the INSERT change is
12275** removed from the local changeset (it was overridden). Or, if the
12276** conflict resolution was "OMIT", then the local changeset is modified
12277** to instead contain:
12278**
12279**           UPDATE t1 SET b = 'v2' WHERE a=1;
12280**
12281** Changes within the local changeset are rebased as follows:
12282**
12283** <dl>
12284** <dt>Local INSERT<dd>
12285**   This may only conflict with a remote INSERT. If the conflict
12286**   resolution was OMIT, then add an UPDATE change to the rebased
12287**   changeset. Or, if the conflict resolution was REPLACE, add
12288**   nothing to the rebased changeset.
12289**
12290** <dt>Local DELETE<dd>
12291**   This may conflict with a remote UPDATE or DELETE. In both cases the
12292**   only possible resolution is OMIT. If the remote operation was a
12293**   DELETE, then add no change to the rebased changeset. If the remote
12294**   operation was an UPDATE, then the old.* fields of change are updated
12295**   to reflect the new.* values in the UPDATE.
12296**
12297** <dt>Local UPDATE<dd>
12298**   This may conflict with a remote UPDATE or DELETE. If it conflicts
12299**   with a DELETE, and the conflict resolution was OMIT, then the update
12300**   is changed into an INSERT. Any undefined values in the new.* record
12301**   from the update change are filled in using the old.* values from
12302**   the conflicting DELETE. Or, if the conflict resolution was REPLACE,
12303**   the UPDATE change is simply omitted from the rebased changeset.
12304**
12305**   If conflict is with a remote UPDATE and the resolution is OMIT, then
12306**   the old.* values are rebased using the new.* values in the remote
12307**   change. Or, if the resolution is REPLACE, then the change is copied
12308**   into the rebased changeset with updates to columns also updated by
12309**   the conflicting remote UPDATE removed. If this means no columns would
12310**   be updated, the change is omitted.
12311** </dl>
12312**
12313** A local change may be rebased against multiple remote changes
12314** simultaneously. If a single key is modified by multiple remote
12315** changesets, they are combined as follows before the local changeset
12316** is rebased:
12317**
12318** <ul>
12319**    <li> If there has been one or more REPLACE resolutions on a
12320**         key, it is rebased according to a REPLACE.
12321**
12322**    <li> If there have been no REPLACE resolutions on a key, then
12323**         the local changeset is rebased according to the most recent
12324**         of the OMIT resolutions.
12325** </ul>
12326**
12327** Note that conflict resolutions from multiple remote changesets are
12328** combined on a per-field basis, not per-row. This means that in the
12329** case of multiple remote UPDATE operations, some fields of a single
12330** local change may be rebased for REPLACE while others are rebased for
12331** OMIT.
12332**
12333** In order to rebase a local changeset, the remote changeset must first
12334** be applied to the local database using sqlite3changeset_apply_v2() and
12335** the buffer of rebase information captured. Then:
12336**
12337** <ol>
12338**   <li> An sqlite3_rebaser object is created by calling
12339**        sqlite3rebaser_create().
12340**   <li> The new object is configured with the rebase buffer obtained from
12341**        sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
12342**        If the local changeset is to be rebased against multiple remote
12343**        changesets, then sqlite3rebaser_configure() should be called
12344**        multiple times, in the same order that the multiple
12345**        sqlite3changeset_apply_v2() calls were made.
12346**   <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
12347**   <li> The sqlite3_rebaser object is deleted by calling
12348**        sqlite3rebaser_delete().
12349** </ol>
12350*/
12351typedef struct sqlite3_rebaser sqlite3_rebaser;
12352
12353/*
12354** CAPI3REF: Create a changeset rebaser object.
12355** EXPERIMENTAL
12356**
12357** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
12358** point to the new object and return SQLITE_OK. Otherwise, if an error
12359** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
12360** to NULL.
12361*/
12362SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
12363
12364/*
12365** CAPI3REF: Configure a changeset rebaser object.
12366** EXPERIMENTAL
12367**
12368** Configure the changeset rebaser object to rebase changesets according
12369** to the conflict resolutions described by buffer pRebase (size nRebase
12370** bytes), which must have been obtained from a previous call to
12371** sqlite3changeset_apply_v2().
12372*/
12373SQLITE_API int sqlite3rebaser_configure(
12374  sqlite3_rebaser*,
12375  int nRebase, const void *pRebase
12376);
12377
12378/*
12379** CAPI3REF: Rebase a changeset
12380** EXPERIMENTAL
12381**
12382** Argument pIn must point to a buffer containing a changeset nIn bytes
12383** in size. This function allocates and populates a buffer with a copy
12384** of the changeset rebased according to the configuration of the
12385** rebaser object passed as the first argument. If successful, (*ppOut)
12386** is set to point to the new buffer containing the rebased changeset and
12387** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
12388** responsibility of the caller to eventually free the new buffer using
12389** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
12390** are set to zero and an SQLite error code returned.
12391*/
12392SQLITE_API int sqlite3rebaser_rebase(
12393  sqlite3_rebaser*,
12394  int nIn, const void *pIn,
12395  int *pnOut, void **ppOut
12396);
12397
12398/*
12399** CAPI3REF: Delete a changeset rebaser object.
12400** EXPERIMENTAL
12401**
12402** Delete the changeset rebaser object and all associated resources. There
12403** should be one call to this function for each successful invocation
12404** of sqlite3rebaser_create().
12405*/
12406SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p);
12407
12408/*
12409** CAPI3REF: Streaming Versions of API functions.
12410**
12411** The six streaming API xxx_strm() functions serve similar purposes to the
12412** corresponding non-streaming API functions:
12413**
12414** <table border=1 style="margin-left:8ex;margin-right:8ex">
12415**   <tr><th>Streaming function<th>Non-streaming equivalent</th>
12416**   <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply]
12417**   <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2]
12418**   <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat]
12419**   <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert]
12420**   <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start]
12421**   <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset]
12422**   <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset]
12423** </table>
12424**
12425** Non-streaming functions that accept changesets (or patchsets) as input
12426** require that the entire changeset be stored in a single buffer in memory.
12427** Similarly, those that return a changeset or patchset do so by returning
12428** a pointer to a single large buffer allocated using sqlite3_malloc().
12429** Normally this is convenient. However, if an application running in a
12430** low-memory environment is required to handle very large changesets, the
12431** large contiguous memory allocations required can become onerous.
12432**
12433** In order to avoid this problem, instead of a single large buffer, input
12434** is passed to a streaming API functions by way of a callback function that
12435** the sessions module invokes to incrementally request input data as it is
12436** required. In all cases, a pair of API function parameters such as
12437**
12438**  <pre>
12439**  &nbsp;     int nChangeset,
12440**  &nbsp;     void *pChangeset,
12441**  </pre>
12442**
12443** Is replaced by:
12444**
12445**  <pre>
12446**  &nbsp;     int (*xInput)(void *pIn, void *pData, int *pnData),
12447**  &nbsp;     void *pIn,
12448**  </pre>
12449**
12450** Each time the xInput callback is invoked by the sessions module, the first
12451** argument passed is a copy of the supplied pIn context pointer. The second
12452** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no
12453** error occurs the xInput method should copy up to (*pnData) bytes of data
12454** into the buffer and set (*pnData) to the actual number of bytes copied
12455** before returning SQLITE_OK. If the input is completely exhausted, (*pnData)
12456** should be set to zero to indicate this. Or, if an error occurs, an SQLite
12457** error code should be returned. In all cases, if an xInput callback returns
12458** an error, all processing is abandoned and the streaming API function
12459** returns a copy of the error code to the caller.
12460**
12461** In the case of sqlite3changeset_start_strm(), the xInput callback may be
12462** invoked by the sessions module at any point during the lifetime of the
12463** iterator. If such an xInput callback returns an error, the iterator enters
12464** an error state, whereby all subsequent calls to iterator functions
12465** immediately fail with the same error code as returned by xInput.
12466**
12467** Similarly, streaming API functions that return changesets (or patchsets)
12468** return them in chunks by way of a callback function instead of via a
12469** pointer to a single large buffer. In this case, a pair of parameters such
12470** as:
12471**
12472**  <pre>
12473**  &nbsp;     int *pnChangeset,
12474**  &nbsp;     void **ppChangeset,
12475**  </pre>
12476**
12477** Is replaced by:
12478**
12479**  <pre>
12480**  &nbsp;     int (*xOutput)(void *pOut, const void *pData, int nData),
12481**  &nbsp;     void *pOut
12482**  </pre>
12483**
12484** The xOutput callback is invoked zero or more times to return data to
12485** the application. The first parameter passed to each call is a copy of the
12486** pOut pointer supplied by the application. The second parameter, pData,
12487** points to a buffer nData bytes in size containing the chunk of output
12488** data being returned. If the xOutput callback successfully processes the
12489** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
12490** it should return some other SQLite error code. In this case processing
12491** is immediately abandoned and the streaming API function returns a copy
12492** of the xOutput error code to the application.
12493**
12494** The sessions module never invokes an xOutput callback with the third
12495** parameter set to a value less than or equal to zero. Other than this,
12496** no guarantees are made as to the size of the chunks of data returned.
12497*/
12498SQLITE_API int sqlite3changeset_apply_strm(
12499  sqlite3 *db,                    /* Apply change to "main" db of this handle */
12500  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
12501  void *pIn,                                          /* First arg for xInput */
12502  int(*xFilter)(
12503    void *pCtx,                   /* Copy of sixth arg to _apply() */
12504    const char *zTab              /* Table name */
12505  ),
12506  int(*xConflict)(
12507    void *pCtx,                   /* Copy of sixth arg to _apply() */
12508    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
12509    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
12510  ),
12511  void *pCtx                      /* First argument passed to xConflict */
12512);
12513SQLITE_API int sqlite3changeset_apply_v2_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  void **ppRebase, int *pnRebase,
12528  int flags
12529);
12530SQLITE_API int sqlite3changeset_concat_strm(
12531  int (*xInputA)(void *pIn, void *pData, int *pnData),
12532  void *pInA,
12533  int (*xInputB)(void *pIn, void *pData, int *pnData),
12534  void *pInB,
12535  int (*xOutput)(void *pOut, const void *pData, int nData),
12536  void *pOut
12537);
12538SQLITE_API int sqlite3changeset_invert_strm(
12539  int (*xInput)(void *pIn, void *pData, int *pnData),
12540  void *pIn,
12541  int (*xOutput)(void *pOut, const void *pData, int nData),
12542  void *pOut
12543);
12544SQLITE_API int sqlite3changeset_start_strm(
12545  sqlite3_changeset_iter **pp,
12546  int (*xInput)(void *pIn, void *pData, int *pnData),
12547  void *pIn
12548);
12549SQLITE_API int sqlite3changeset_start_v2_strm(
12550  sqlite3_changeset_iter **pp,
12551  int (*xInput)(void *pIn, void *pData, int *pnData),
12552  void *pIn,
12553  int flags
12554);
12555SQLITE_API int sqlite3session_changeset_strm(
12556  sqlite3_session *pSession,
12557  int (*xOutput)(void *pOut, const void *pData, int nData),
12558  void *pOut
12559);
12560SQLITE_API int sqlite3session_patchset_strm(
12561  sqlite3_session *pSession,
12562  int (*xOutput)(void *pOut, const void *pData, int nData),
12563  void *pOut
12564);
12565SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*,
12566    int (*xInput)(void *pIn, void *pData, int *pnData),
12567    void *pIn
12568);
12569SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
12570    int (*xOutput)(void *pOut, const void *pData, int nData),
12571    void *pOut
12572);
12573SQLITE_API int sqlite3rebaser_rebase_strm(
12574  sqlite3_rebaser *pRebaser,
12575  int (*xInput)(void *pIn, void *pData, int *pnData),
12576  void *pIn,
12577  int (*xOutput)(void *pOut, const void *pData, int nData),
12578  void *pOut
12579);
12580
12581/*
12582** CAPI3REF: Configure global parameters
12583**
12584** The sqlite3session_config() interface is used to make global configuration
12585** changes to the sessions module in order to tune it to the specific needs
12586** of the application.
12587**
12588** The sqlite3session_config() interface is not threadsafe. If it is invoked
12589** while any other thread is inside any other sessions method then the
12590** results are undefined. Furthermore, if it is invoked after any sessions
12591** related objects have been created, the results are also undefined.
12592**
12593** The first argument to the sqlite3session_config() function must be one
12594** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The
12595** interpretation of the (void*) value passed as the second parameter and
12596** the effect of calling this function depends on the value of the first
12597** parameter.
12598**
12599** <dl>
12600** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
12601**    By default, the sessions module streaming interfaces attempt to input
12602**    and output data in approximately 1 KiB chunks. This operand may be used
12603**    to set and query the value of this configuration setting. The pointer
12604**    passed as the second argument must point to a value of type (int).
12605**    If this value is greater than 0, it is used as the new streaming data
12606**    chunk size for both input and output. Before returning, the (int) value
12607**    pointed to by pArg is set to the final value of the streaming interface
12608**    chunk size.
12609** </dl>
12610**
12611** This function returns SQLITE_OK if successful, or an SQLite error code
12612** otherwise.
12613*/
12614SQLITE_API int sqlite3session_config(int op, void *pArg);
12615
12616/*
12617** CAPI3REF: Values for sqlite3session_config().
12618*/
12619#define SQLITE_SESSION_CONFIG_STRMSIZE 1
12620
12621/*
12622** Make sure we can call this stuff from C++.
12623*/
12624#if 0
12625}
12626#endif
12627
12628#endif  /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
12629
12630/******** End of sqlite3session.h *********/
12631/******** Begin file fts5.h *********/
12632/*
12633** 2014 May 31
12634**
12635** The author disclaims copyright to this source code.  In place of
12636** a legal notice, here is a blessing:
12637**
12638**    May you do good and not evil.
12639**    May you find forgiveness for yourself and forgive others.
12640**    May you share freely, never taking more than you give.
12641**
12642******************************************************************************
12643**
12644** Interfaces to extend FTS5. Using the interfaces defined in this file,
12645** FTS5 may be extended with:
12646**
12647**     * custom tokenizers, and
12648**     * custom auxiliary functions.
12649*/
12650
12651
12652#ifndef _FTS5_H
12653#define _FTS5_H
12654
12655
12656#if 0
12657extern "C" {
12658#endif
12659
12660/*************************************************************************
12661** CUSTOM AUXILIARY FUNCTIONS
12662**
12663** Virtual table implementations may overload SQL functions by implementing
12664** the sqlite3_module.xFindFunction() method.
12665*/
12666
12667typedef struct Fts5ExtensionApi Fts5ExtensionApi;
12668typedef struct Fts5Context Fts5Context;
12669typedef struct Fts5PhraseIter Fts5PhraseIter;
12670
12671typedef void (*fts5_extension_function)(
12672  const Fts5ExtensionApi *pApi,   /* API offered by current FTS version */
12673  Fts5Context *pFts,              /* First arg to pass to pApi functions */
12674  sqlite3_context *pCtx,          /* Context for returning result/error */
12675  int nVal,                       /* Number of values in apVal[] array */
12676  sqlite3_value **apVal           /* Array of trailing arguments */
12677);
12678
12679struct Fts5PhraseIter {
12680  const unsigned char *a;
12681  const unsigned char *b;
12682};
12683
12684/*
12685** EXTENSION API FUNCTIONS
12686**
12687** xUserData(pFts):
12688**   Return a copy of the context pointer the extension function was
12689**   registered with.
12690**
12691** xColumnTotalSize(pFts, iCol, pnToken):
12692**   If parameter iCol is less than zero, set output variable *pnToken
12693**   to the total number of tokens in the FTS5 table. Or, if iCol is
12694**   non-negative but less than the number of columns in the table, return
12695**   the total number of tokens in column iCol, considering all rows in
12696**   the FTS5 table.
12697**
12698**   If parameter iCol is greater than or equal to the number of columns
12699**   in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
12700**   an OOM condition or IO error), an appropriate SQLite error code is
12701**   returned.
12702**
12703** xColumnCount(pFts):
12704**   Return the number of columns in the table.
12705**
12706** xColumnSize(pFts, iCol, pnToken):
12707**   If parameter iCol is less than zero, set output variable *pnToken
12708**   to the total number of tokens in the current row. Or, if iCol is
12709**   non-negative but less than the number of columns in the table, set
12710**   *pnToken to the number of tokens in column iCol of the current row.
12711**
12712**   If parameter iCol is greater than or equal to the number of columns
12713**   in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
12714**   an OOM condition or IO error), an appropriate SQLite error code is
12715**   returned.
12716**
12717**   This function may be quite inefficient if used with an FTS5 table
12718**   created with the "columnsize=0" option.
12719**
12720** xColumnText:
12721**   This function attempts to retrieve the text of column iCol of the
12722**   current document. If successful, (*pz) is set to point to a buffer
12723**   containing the text in utf-8 encoding, (*pn) is set to the size in bytes
12724**   (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
12725**   if an error occurs, an SQLite error code is returned and the final values
12726**   of (*pz) and (*pn) are undefined.
12727**
12728** xPhraseCount:
12729**   Returns the number of phrases in the current query expression.
12730**
12731** xPhraseSize:
12732**   Returns the number of tokens in phrase iPhrase of the query. Phrases
12733**   are numbered starting from zero.
12734**
12735** xInstCount:
12736**   Set *pnInst to the total number of occurrences of all phrases within
12737**   the query within the current row. Return SQLITE_OK if successful, or
12738**   an error code (i.e. SQLITE_NOMEM) if an error occurs.
12739**
12740**   This API can be quite slow if used with an FTS5 table created with the
12741**   "detail=none" or "detail=column" option. If the FTS5 table is created
12742**   with either "detail=none" or "detail=column" and "content=" option
12743**   (i.e. if it is a contentless table), then this API always returns 0.
12744**
12745** xInst:
12746**   Query for the details of phrase match iIdx within the current row.
12747**   Phrase matches are numbered starting from zero, so the iIdx argument
12748**   should be greater than or equal to zero and smaller than the value
12749**   output by xInstCount().
12750**
12751**   Usually, output parameter *piPhrase is set to the phrase number, *piCol
12752**   to the column in which it occurs and *piOff the token offset of the
12753**   first token of the phrase. Returns SQLITE_OK if successful, or an error
12754**   code (i.e. SQLITE_NOMEM) if an error occurs.
12755**
12756**   This API can be quite slow if used with an FTS5 table created with the
12757**   "detail=none" or "detail=column" option.
12758**
12759** xRowid:
12760**   Returns the rowid of the current row.
12761**
12762** xTokenize:
12763**   Tokenize text using the tokenizer belonging to the FTS5 table.
12764**
12765** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
12766**   This API function is used to query the FTS table for phrase iPhrase
12767**   of the current query. Specifically, a query equivalent to:
12768**
12769**       ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
12770**
12771**   with $p set to a phrase equivalent to the phrase iPhrase of the
12772**   current query is executed. Any column filter that applies to
12773**   phrase iPhrase of the current query is included in $p. For each
12774**   row visited, the callback function passed as the fourth argument
12775**   is invoked. The context and API objects passed to the callback
12776**   function may be used to access the properties of each matched row.
12777**   Invoking Api.xUserData() returns a copy of the pointer passed as
12778**   the third argument to pUserData.
12779**
12780**   If the callback function returns any value other than SQLITE_OK, the
12781**   query is abandoned and the xQueryPhrase function returns immediately.
12782**   If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
12783**   Otherwise, the error code is propagated upwards.
12784**
12785**   If the query runs to completion without incident, SQLITE_OK is returned.
12786**   Or, if some error occurs before the query completes or is aborted by
12787**   the callback, an SQLite error code is returned.
12788**
12789**
12790** xSetAuxdata(pFts5, pAux, xDelete)
12791**
12792**   Save the pointer passed as the second argument as the extension function's
12793**   "auxiliary data". The pointer may then be retrieved by the current or any
12794**   future invocation of the same fts5 extension function made as part of
12795**   the same MATCH query using the xGetAuxdata() API.
12796**
12797**   Each extension function is allocated a single auxiliary data slot for
12798**   each FTS query (MATCH expression). If the extension function is invoked
12799**   more than once for a single FTS query, then all invocations share a
12800**   single auxiliary data context.
12801**
12802**   If there is already an auxiliary data pointer when this function is
12803**   invoked, then it is replaced by the new pointer. If an xDelete callback
12804**   was specified along with the original pointer, it is invoked at this
12805**   point.
12806**
12807**   The xDelete callback, if one is specified, is also invoked on the
12808**   auxiliary data pointer after the FTS5 query has finished.
12809**
12810**   If an error (e.g. an OOM condition) occurs within this function,
12811**   the auxiliary data is set to NULL and an error code returned. If the
12812**   xDelete parameter was not NULL, it is invoked on the auxiliary data
12813**   pointer before returning.
12814**
12815**
12816** xGetAuxdata(pFts5, bClear)
12817**
12818**   Returns the current auxiliary data pointer for the fts5 extension
12819**   function. See the xSetAuxdata() method for details.
12820**
12821**   If the bClear argument is non-zero, then the auxiliary data is cleared
12822**   (set to NULL) before this function returns. In this case the xDelete,
12823**   if any, is not invoked.
12824**
12825**
12826** xRowCount(pFts5, pnRow)
12827**
12828**   This function is used to retrieve the total number of rows in the table.
12829**   In other words, the same value that would be returned by:
12830**
12831**        SELECT count(*) FROM ftstable;
12832**
12833** xPhraseFirst()
12834**   This function is used, along with type Fts5PhraseIter and the xPhraseNext
12835**   method, to iterate through all instances of a single query phrase within
12836**   the current row. This is the same information as is accessible via the
12837**   xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
12838**   to use, this API may be faster under some circumstances. To iterate
12839**   through instances of phrase iPhrase, use the following code:
12840**
12841**       Fts5PhraseIter iter;
12842**       int iCol, iOff;
12843**       for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
12844**           iCol>=0;
12845**           pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
12846**       ){
12847**         // An instance of phrase iPhrase at offset iOff of column iCol
12848**       }
12849**
12850**   The Fts5PhraseIter structure is defined above. Applications should not
12851**   modify this structure directly - it should only be used as shown above
12852**   with the xPhraseFirst() and xPhraseNext() API methods (and by
12853**   xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
12854**
12855**   This API can be quite slow if used with an FTS5 table created with the
12856**   "detail=none" or "detail=column" option. If the FTS5 table is created
12857**   with either "detail=none" or "detail=column" and "content=" option
12858**   (i.e. if it is a contentless table), then this API always iterates
12859**   through an empty set (all calls to xPhraseFirst() set iCol to -1).
12860**
12861** xPhraseNext()
12862**   See xPhraseFirst above.
12863**
12864** xPhraseFirstColumn()
12865**   This function and xPhraseNextColumn() are similar to the xPhraseFirst()
12866**   and xPhraseNext() APIs described above. The difference is that instead
12867**   of iterating through all instances of a phrase in the current row, these
12868**   APIs are used to iterate through the set of columns in the current row
12869**   that contain one or more instances of a specified phrase. For example:
12870**
12871**       Fts5PhraseIter iter;
12872**       int iCol;
12873**       for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
12874**           iCol>=0;
12875**           pApi->xPhraseNextColumn(pFts, &iter, &iCol)
12876**       ){
12877**         // Column iCol contains at least one instance of phrase iPhrase
12878**       }
12879**
12880**   This API can be quite slow if used with an FTS5 table created with the
12881**   "detail=none" option. If the FTS5 table is created with either
12882**   "detail=none" "content=" option (i.e. if it is a contentless table),
12883**   then this API always iterates through an empty set (all calls to
12884**   xPhraseFirstColumn() set iCol to -1).
12885**
12886**   The information accessed using this API and its companion
12887**   xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
12888**   (or xInst/xInstCount). The chief advantage of this API is that it is
12889**   significantly more efficient than those alternatives when used with
12890**   "detail=column" tables.
12891**
12892** xPhraseNextColumn()
12893**   See xPhraseFirstColumn above.
12894*/
12895struct Fts5ExtensionApi {
12896  int iVersion;                   /* Currently always set to 3 */
12897
12898  void *(*xUserData)(Fts5Context*);
12899
12900  int (*xColumnCount)(Fts5Context*);
12901  int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
12902  int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
12903
12904  int (*xTokenize)(Fts5Context*,
12905    const char *pText, int nText, /* Text to tokenize */
12906    void *pCtx,                   /* Context passed to xToken() */
12907    int (*xToken)(void*, int, const char*, int, int, int)       /* Callback */
12908  );
12909
12910  int (*xPhraseCount)(Fts5Context*);
12911  int (*xPhraseSize)(Fts5Context*, int iPhrase);
12912
12913  int (*xInstCount)(Fts5Context*, int *pnInst);
12914  int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
12915
12916  sqlite3_int64 (*xRowid)(Fts5Context*);
12917  int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
12918  int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
12919
12920  int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
12921    int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
12922  );
12923  int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
12924  void *(*xGetAuxdata)(Fts5Context*, int bClear);
12925
12926  int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
12927  void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
12928
12929  int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
12930  void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
12931};
12932
12933/*
12934** CUSTOM AUXILIARY FUNCTIONS
12935*************************************************************************/
12936
12937/*************************************************************************
12938** CUSTOM TOKENIZERS
12939**
12940** Applications may also register custom tokenizer types. A tokenizer
12941** is registered by providing fts5 with a populated instance of the
12942** following structure. All structure methods must be defined, setting
12943** any member of the fts5_tokenizer struct to NULL leads to undefined
12944** behaviour. The structure methods are expected to function as follows:
12945**
12946** xCreate:
12947**   This function is used to allocate and initialize a tokenizer instance.
12948**   A tokenizer instance is required to actually tokenize text.
12949**
12950**   The first argument passed to this function is a copy of the (void*)
12951**   pointer provided by the application when the fts5_tokenizer object
12952**   was registered with FTS5 (the third argument to xCreateTokenizer()).
12953**   The second and third arguments are an array of nul-terminated strings
12954**   containing the tokenizer arguments, if any, specified following the
12955**   tokenizer name as part of the CREATE VIRTUAL TABLE statement used
12956**   to create the FTS5 table.
12957**
12958**   The final argument is an output variable. If successful, (*ppOut)
12959**   should be set to point to the new tokenizer handle and SQLITE_OK
12960**   returned. If an error occurs, some value other than SQLITE_OK should
12961**   be returned. In this case, fts5 assumes that the final value of *ppOut
12962**   is undefined.
12963**
12964** xDelete:
12965**   This function is invoked to delete a tokenizer handle previously
12966**   allocated using xCreate(). Fts5 guarantees that this function will
12967**   be invoked exactly once for each successful call to xCreate().
12968**
12969** xTokenize:
12970**   This function is expected to tokenize the nText byte string indicated
12971**   by argument pText. pText may or may not be nul-terminated. The first
12972**   argument passed to this function is a pointer to an Fts5Tokenizer object
12973**   returned by an earlier call to xCreate().
12974**
12975**   The second argument indicates the reason that FTS5 is requesting
12976**   tokenization of the supplied text. This is always one of the following
12977**   four values:
12978**
12979**   <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
12980**            or removed from the FTS table. The tokenizer is being invoked to
12981**            determine the set of tokens to add to (or delete from) the
12982**            FTS index.
12983**
12984**       <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
12985**            against the FTS index. The tokenizer is being called to tokenize
12986**            a bareword or quoted string specified as part of the query.
12987**
12988**       <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
12989**            FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
12990**            followed by a "*" character, indicating that the last token
12991**            returned by the tokenizer will be treated as a token prefix.
12992**
12993**       <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
12994**            satisfy an fts5_api.xTokenize() request made by an auxiliary
12995**            function. Or an fts5_api.xColumnSize() request made by the same
12996**            on a columnsize=0 database.
12997**   </ul>
12998**
12999**   For each token in the input string, the supplied callback xToken() must
13000**   be invoked. The first argument to it should be a copy of the pointer
13001**   passed as the second argument to xTokenize(). The third and fourth
13002**   arguments are a pointer to a buffer containing the token text, and the
13003**   size of the token in bytes. The 4th and 5th arguments are the byte offsets
13004**   of the first byte of and first byte immediately following the text from
13005**   which the token is derived within the input.
13006**
13007**   The second argument passed to the xToken() callback ("tflags") should
13008**   normally be set to 0. The exception is if the tokenizer supports
13009**   synonyms. In this case see the discussion below for details.
13010**
13011**   FTS5 assumes the xToken() callback is invoked for each token in the
13012**   order that they occur within the input text.
13013**
13014**   If an xToken() callback returns any value other than SQLITE_OK, then
13015**   the tokenization should be abandoned and the xTokenize() method should
13016**   immediately return a copy of the xToken() return value. Or, if the
13017**   input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
13018**   if an error occurs with the xTokenize() implementation itself, it
13019**   may abandon the tokenization and return any error code other than
13020**   SQLITE_OK or SQLITE_DONE.
13021**
13022** SYNONYM SUPPORT
13023**
13024**   Custom tokenizers may also support synonyms. Consider a case in which a
13025**   user wishes to query for a phrase such as "first place". Using the
13026**   built-in tokenizers, the FTS5 query 'first + place' will match instances
13027**   of "first place" within the document set, but not alternative forms
13028**   such as "1st place". In some applications, it would be better to match
13029**   all instances of "first place" or "1st place" regardless of which form
13030**   the user specified in the MATCH query text.
13031**
13032**   There are several ways to approach this in FTS5:
13033**
13034**   <ol><li> By mapping all synonyms to a single token. In this case, using
13035**            the above example, this means that the tokenizer returns the
13036**            same token for inputs "first" and "1st". Say that token is in
13037**            fact "first", so that when the user inserts the document "I won
13038**            1st place" entries are added to the index for tokens "i", "won",
13039**            "first" and "place". If the user then queries for '1st + place',
13040**            the tokenizer substitutes "first" for "1st" and the query works
13041**            as expected.
13042**
13043**       <li> By querying the index for all synonyms of each query term
13044**            separately. In this case, when tokenizing query text, the
13045**            tokenizer may provide multiple synonyms for a single term
13046**            within the document. FTS5 then queries the index for each
13047**            synonym individually. For example, faced with the query:
13048**
13049**   <codeblock>
13050**     ... MATCH 'first place'</codeblock>
13051**
13052**            the tokenizer offers both "1st" and "first" as synonyms for the
13053**            first token in the MATCH query and FTS5 effectively runs a query
13054**            similar to:
13055**
13056**   <codeblock>
13057**     ... MATCH '(first OR 1st) place'</codeblock>
13058**
13059**            except that, for the purposes of auxiliary functions, the query
13060**            still appears to contain just two phrases - "(first OR 1st)"
13061**            being treated as a single phrase.
13062**
13063**       <li> By adding multiple synonyms for a single term to the FTS index.
13064**            Using this method, when tokenizing document text, the tokenizer
13065**            provides multiple synonyms for each token. So that when a
13066**            document such as "I won first place" is tokenized, entries are
13067**            added to the FTS index for "i", "won", "first", "1st" and
13068**            "place".
13069**
13070**            This way, even if the tokenizer does not provide synonyms
13071**            when tokenizing query text (it should not - to do so would be
13072**            inefficient), it doesn't matter if the user queries for
13073**            'first + place' or '1st + place', as there are entries in the
13074**            FTS index corresponding to both forms of the first token.
13075**   </ol>
13076**
13077**   Whether it is parsing document or query text, any call to xToken that
13078**   specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
13079**   is considered to supply a synonym for the previous token. For example,
13080**   when parsing the document "I won first place", a tokenizer that supports
13081**   synonyms would call xToken() 5 times, as follows:
13082**
13083**   <codeblock>
13084**       xToken(pCtx, 0, "i",                      1,  0,  1);
13085**       xToken(pCtx, 0, "won",                    3,  2,  5);
13086**       xToken(pCtx, 0, "first",                  5,  6, 11);
13087**       xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3,  6, 11);
13088**       xToken(pCtx, 0, "place",                  5, 12, 17);
13089**</codeblock>
13090**
13091**   It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
13092**   xToken() is called. Multiple synonyms may be specified for a single token
13093**   by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
13094**   There is no limit to the number of synonyms that may be provided for a
13095**   single token.
13096**
13097**   In many cases, method (1) above is the best approach. It does not add
13098**   extra data to the FTS index or require FTS5 to query for multiple terms,
13099**   so it is efficient in terms of disk space and query speed. However, it
13100**   does not support prefix queries very well. If, as suggested above, the
13101**   token "first" is substituted for "1st" by the tokenizer, then the query:
13102**
13103**   <codeblock>
13104**     ... MATCH '1s*'</codeblock>
13105**
13106**   will not match documents that contain the token "1st" (as the tokenizer
13107**   will probably not map "1s" to any prefix of "first").
13108**
13109**   For full prefix support, method (3) may be preferred. In this case,
13110**   because the index contains entries for both "first" and "1st", prefix
13111**   queries such as 'fi*' or '1s*' will match correctly. However, because
13112**   extra entries are added to the FTS index, this method uses more space
13113**   within the database.
13114**
13115**   Method (2) offers a midpoint between (1) and (3). Using this method,
13116**   a query such as '1s*' will match documents that contain the literal
13117**   token "1st", but not "first" (assuming the tokenizer is not able to
13118**   provide synonyms for prefixes). However, a non-prefix query like '1st'
13119**   will match against "1st" and "first". This method does not require
13120**   extra disk space, as no extra entries are added to the FTS index.
13121**   On the other hand, it may require more CPU cycles to run MATCH queries,
13122**   as separate queries of the FTS index are required for each synonym.
13123**
13124**   When using methods (2) or (3), it is important that the tokenizer only
13125**   provide synonyms when tokenizing document text (method (2)) or query
13126**   text (method (3)), not both. Doing so will not cause any errors, but is
13127**   inefficient.
13128*/
13129typedef struct Fts5Tokenizer Fts5Tokenizer;
13130typedef struct fts5_tokenizer fts5_tokenizer;
13131struct fts5_tokenizer {
13132  int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
13133  void (*xDelete)(Fts5Tokenizer*);
13134  int (*xTokenize)(Fts5Tokenizer*,
13135      void *pCtx,
13136      int flags,            /* Mask of FTS5_TOKENIZE_* flags */
13137      const char *pText, int nText,
13138      int (*xToken)(
13139        void *pCtx,         /* Copy of 2nd argument to xTokenize() */
13140        int tflags,         /* Mask of FTS5_TOKEN_* flags */
13141        const char *pToken, /* Pointer to buffer containing token */
13142        int nToken,         /* Size of token in bytes */
13143        int iStart,         /* Byte offset of token within input text */
13144        int iEnd            /* Byte offset of end of token within input text */
13145      )
13146  );
13147};
13148
13149/* Flags that may be passed as the third argument to xTokenize() */
13150#define FTS5_TOKENIZE_QUERY     0x0001
13151#define FTS5_TOKENIZE_PREFIX    0x0002
13152#define FTS5_TOKENIZE_DOCUMENT  0x0004
13153#define FTS5_TOKENIZE_AUX       0x0008
13154
13155/* Flags that may be passed by the tokenizer implementation back to FTS5
13156** as the third argument to the supplied xToken callback. */
13157#define FTS5_TOKEN_COLOCATED    0x0001      /* Same position as prev. token */
13158
13159/*
13160** END OF CUSTOM TOKENIZERS
13161*************************************************************************/
13162
13163/*************************************************************************
13164** FTS5 EXTENSION REGISTRATION API
13165*/
13166typedef struct fts5_api fts5_api;
13167struct fts5_api {
13168  int iVersion;                   /* Currently always set to 2 */
13169
13170  /* Create a new tokenizer */
13171  int (*xCreateTokenizer)(
13172    fts5_api *pApi,
13173    const char *zName,
13174    void *pContext,
13175    fts5_tokenizer *pTokenizer,
13176    void (*xDestroy)(void*)
13177  );
13178
13179  /* Find an existing tokenizer */
13180  int (*xFindTokenizer)(
13181    fts5_api *pApi,
13182    const char *zName,
13183    void **ppContext,
13184    fts5_tokenizer *pTokenizer
13185  );
13186
13187  /* Create a new auxiliary function */
13188  int (*xCreateFunction)(
13189    fts5_api *pApi,
13190    const char *zName,
13191    void *pContext,
13192    fts5_extension_function xFunction,
13193    void (*xDestroy)(void*)
13194  );
13195};
13196
13197/*
13198** END OF REGISTRATION API
13199*************************************************************************/
13200
13201#if 0
13202}  /* end of the 'extern "C"' block */
13203#endif
13204
13205#endif /* _FTS5_H */
13206
13207/******** End of fts5.h *********/
13208
13209/************** End of sqlite3.h *********************************************/
13210/************** Continuing where we left off in sqliteInt.h ******************/
13211
13212/*
13213** Include the configuration header output by 'configure' if we're using the
13214** autoconf-based build
13215*/
13216#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
13217/* #include "config.h" */
13218#define SQLITECONFIG_H 1
13219#endif
13220
13221/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
13222/************** Begin file sqliteLimit.h *************************************/
13223/*
13224** 2007 May 7
13225**
13226** The author disclaims copyright to this source code.  In place of
13227** a legal notice, here is a blessing:
13228**
13229**    May you do good and not evil.
13230**    May you find forgiveness for yourself and forgive others.
13231**    May you share freely, never taking more than you give.
13232**
13233*************************************************************************
13234**
13235** This file defines various limits of what SQLite can process.
13236*/
13237
13238/*
13239** The maximum length of a TEXT or BLOB in bytes.   This also
13240** limits the size of a row in a table or index.
13241**
13242** The hard limit is the ability of a 32-bit signed integer
13243** to count the size: 2^31-1 or 2147483647.
13244*/
13245#ifndef SQLITE_MAX_LENGTH
13246# define SQLITE_MAX_LENGTH 1000000000
13247#endif
13248
13249/*
13250** This is the maximum number of
13251**
13252**    * Columns in a table
13253**    * Columns in an index
13254**    * Columns in a view
13255**    * Terms in the SET clause of an UPDATE statement
13256**    * Terms in the result set of a SELECT statement
13257**    * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
13258**    * Terms in the VALUES clause of an INSERT statement
13259**
13260** The hard upper limit here is 32676.  Most database people will
13261** tell you that in a well-normalized database, you usually should
13262** not have more than a dozen or so columns in any table.  And if
13263** that is the case, there is no point in having more than a few
13264** dozen values in any of the other situations described above.
13265*/
13266#ifndef SQLITE_MAX_COLUMN
13267# define SQLITE_MAX_COLUMN 2000
13268#endif
13269
13270/*
13271** The maximum length of a single SQL statement in bytes.
13272**
13273** It used to be the case that setting this value to zero would
13274** turn the limit off.  That is no longer true.  It is not possible
13275** to turn this limit off.
13276*/
13277#ifndef SQLITE_MAX_SQL_LENGTH
13278# define SQLITE_MAX_SQL_LENGTH 1000000000
13279#endif
13280
13281/*
13282** The maximum depth of an expression tree. This is limited to
13283** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
13284** want to place more severe limits on the complexity of an
13285** expression.
13286**
13287** A value of 0 used to mean that the limit was not enforced.
13288** But that is no longer true.  The limit is now strictly enforced
13289** at all times.
13290*/
13291#ifndef SQLITE_MAX_EXPR_DEPTH
13292# define SQLITE_MAX_EXPR_DEPTH 1000
13293#endif
13294
13295/*
13296** The maximum number of terms in a compound SELECT statement.
13297** The code generator for compound SELECT statements does one
13298** level of recursion for each term.  A stack overflow can result
13299** if the number of terms is too large.  In practice, most SQL
13300** never has more than 3 or 4 terms.  Use a value of 0 to disable
13301** any limit on the number of terms in a compount SELECT.
13302*/
13303#ifndef SQLITE_MAX_COMPOUND_SELECT
13304# define SQLITE_MAX_COMPOUND_SELECT 500
13305#endif
13306
13307/*
13308** The maximum number of opcodes in a VDBE program.
13309** Not currently enforced.
13310*/
13311#ifndef SQLITE_MAX_VDBE_OP
13312# define SQLITE_MAX_VDBE_OP 250000000
13313#endif
13314
13315/*
13316** The maximum number of arguments to an SQL function.
13317*/
13318#ifndef SQLITE_MAX_FUNCTION_ARG
13319# define SQLITE_MAX_FUNCTION_ARG 127
13320#endif
13321
13322/*
13323** The suggested maximum number of in-memory pages to use for
13324** the main database table and for temporary tables.
13325**
13326** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
13327** which means the cache size is limited to 2048000 bytes of memory.
13328** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
13329** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
13330*/
13331#ifndef SQLITE_DEFAULT_CACHE_SIZE
13332# define SQLITE_DEFAULT_CACHE_SIZE  -2000
13333#endif
13334
13335/*
13336** The default number of frames to accumulate in the log file before
13337** checkpointing the database in WAL mode.
13338*/
13339#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
13340# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT  1000
13341#endif
13342
13343/*
13344** The maximum number of attached databases.  This must be between 0
13345** and 125.  The upper bound of 125 is because the attached databases are
13346** counted using a signed 8-bit integer which has a maximum value of 127
13347** and we have to allow 2 extra counts for the "main" and "temp" databases.
13348*/
13349#ifndef SQLITE_MAX_ATTACHED
13350# define SQLITE_MAX_ATTACHED 10
13351#endif
13352
13353
13354/*
13355** The maximum value of a ?nnn wildcard that the parser will accept.
13356** If the value exceeds 32767 then extra space is required for the Expr
13357** structure.  But otherwise, we believe that the number can be as large
13358** as a signed 32-bit integer can hold.
13359*/
13360#ifndef SQLITE_MAX_VARIABLE_NUMBER
13361# define SQLITE_MAX_VARIABLE_NUMBER 32766
13362#endif
13363
13364/* Maximum page size.  The upper bound on this value is 65536.  This a limit
13365** imposed by the use of 16-bit offsets within each page.
13366**
13367** Earlier versions of SQLite allowed the user to change this value at
13368** compile time. This is no longer permitted, on the grounds that it creates
13369** a library that is technically incompatible with an SQLite library
13370** compiled with a different limit. If a process operating on a database
13371** with a page-size of 65536 bytes crashes, then an instance of SQLite
13372** compiled with the default page-size limit will not be able to rollback
13373** the aborted transaction. This could lead to database corruption.
13374*/
13375#ifdef SQLITE_MAX_PAGE_SIZE
13376# undef SQLITE_MAX_PAGE_SIZE
13377#endif
13378#define SQLITE_MAX_PAGE_SIZE 65536
13379
13380
13381/*
13382** The default size of a database page.
13383*/
13384#ifndef SQLITE_DEFAULT_PAGE_SIZE
13385# define SQLITE_DEFAULT_PAGE_SIZE 4096
13386#endif
13387#if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
13388# undef SQLITE_DEFAULT_PAGE_SIZE
13389# define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
13390#endif
13391
13392/*
13393** Ordinarily, if no value is explicitly provided, SQLite creates databases
13394** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
13395** device characteristics (sector-size and atomic write() support),
13396** SQLite may choose a larger value. This constant is the maximum value
13397** SQLite will choose on its own.
13398*/
13399#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
13400# define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
13401#endif
13402#if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
13403# undef SQLITE_MAX_DEFAULT_PAGE_SIZE
13404# define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
13405#endif
13406
13407
13408/*
13409** Maximum number of pages in one database file.
13410**
13411** This is really just the default value for the max_page_count pragma.
13412** This value can be lowered (or raised) at run-time using that the
13413** max_page_count macro.
13414*/
13415#ifndef SQLITE_MAX_PAGE_COUNT
13416# define SQLITE_MAX_PAGE_COUNT 1073741823
13417#endif
13418
13419/*
13420** Maximum length (in bytes) of the pattern in a LIKE or GLOB
13421** operator.
13422*/
13423#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
13424# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
13425#endif
13426
13427/*
13428** Maximum depth of recursion for triggers.
13429**
13430** A value of 1 means that a trigger program will not be able to itself
13431** fire any triggers. A value of 0 means that no trigger programs at all
13432** may be executed.
13433*/
13434#ifndef SQLITE_MAX_TRIGGER_DEPTH
13435# define SQLITE_MAX_TRIGGER_DEPTH 1000
13436#endif
13437
13438/************** End of sqliteLimit.h *****************************************/
13439/************** Continuing where we left off in sqliteInt.h ******************/
13440
13441/* Disable nuisance warnings on Borland compilers */
13442#if defined(__BORLANDC__)
13443#pragma warn -rch /* unreachable code */
13444#pragma warn -ccc /* Condition is always true or false */
13445#pragma warn -aus /* Assigned value is never used */
13446#pragma warn -csu /* Comparing signed and unsigned */
13447#pragma warn -spa /* Suspicious pointer arithmetic */
13448#endif
13449
13450/*
13451** WAL mode depends on atomic aligned 32-bit loads and stores in a few
13452** places.  The following macros try to make this explicit.
13453*/
13454#ifndef __has_feature
13455# define __has_feature(x) 0       /* compatibility with non-clang compilers */
13456#endif
13457#if GCC_VERSION>=4007000 || __has_feature(c_atomic)
13458# define AtomicLoad(PTR)       __atomic_load_n((PTR),__ATOMIC_RELAXED)
13459# define AtomicStore(PTR,VAL)  __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED)
13460#else
13461# define AtomicLoad(PTR)       (*(PTR))
13462# define AtomicStore(PTR,VAL)  (*(PTR) = (VAL))
13463#endif
13464
13465/*
13466** Include standard header files as necessary
13467*/
13468#ifdef HAVE_STDINT_H
13469#include <stdint.h>
13470#endif
13471#ifdef HAVE_INTTYPES_H
13472#include <inttypes.h>
13473#endif
13474
13475/*
13476** The following macros are used to cast pointers to integers and
13477** integers to pointers.  The way you do this varies from one compiler
13478** to the next, so we have developed the following set of #if statements
13479** to generate appropriate macros for a wide range of compilers.
13480**
13481** The correct "ANSI" way to do this is to use the intptr_t type.
13482** Unfortunately, that typedef is not available on all compilers, or
13483** if it is available, it requires an #include of specific headers
13484** that vary from one machine to the next.
13485**
13486** Ticket #3860:  The llvm-gcc-4.2 compiler from Apple chokes on
13487** the ((void*)&((char*)0)[X]) construct.  But MSVC chokes on ((void*)(X)).
13488** So we have to define the macros in different ways depending on the
13489** compiler.
13490*/
13491#if defined(HAVE_STDINT_H)   /* Use this case if we have ANSI headers */
13492# define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
13493# define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
13494#elif defined(__PTRDIFF_TYPE__)  /* This case should work for GCC */
13495# define SQLITE_INT_TO_PTR(X)  ((void*)(__PTRDIFF_TYPE__)(X))
13496# define SQLITE_PTR_TO_INT(X)  ((int)(__PTRDIFF_TYPE__)(X))
13497#elif !defined(__GNUC__)       /* Works for compilers other than LLVM */
13498# define SQLITE_INT_TO_PTR(X)  ((void*)&((char*)0)[X])
13499# define SQLITE_PTR_TO_INT(X)  ((int)(((char*)X)-(char*)0))
13500#else                          /* Generates a warning - but it always works */
13501# define SQLITE_INT_TO_PTR(X)  ((void*)(X))
13502# define SQLITE_PTR_TO_INT(X)  ((int)(X))
13503#endif
13504
13505/*
13506** A macro to hint to the compiler that a function should not be
13507** inlined.
13508*/
13509#if defined(__GNUC__)
13510#  define SQLITE_NOINLINE  __attribute__((noinline))
13511#elif defined(_MSC_VER) && _MSC_VER>=1310
13512#  define SQLITE_NOINLINE  __declspec(noinline)
13513#else
13514#  define SQLITE_NOINLINE
13515#endif
13516
13517/*
13518** Make sure that the compiler intrinsics we desire are enabled when
13519** compiling with an appropriate version of MSVC unless prevented by
13520** the SQLITE_DISABLE_INTRINSIC define.
13521*/
13522#if !defined(SQLITE_DISABLE_INTRINSIC)
13523#  if defined(_MSC_VER) && _MSC_VER>=1400
13524#    if !defined(_WIN32_WCE)
13525#      include <intrin.h>
13526#      pragma intrinsic(_byteswap_ushort)
13527#      pragma intrinsic(_byteswap_ulong)
13528#      pragma intrinsic(_byteswap_uint64)
13529#      pragma intrinsic(_ReadWriteBarrier)
13530#    else
13531#      include <cmnintrin.h>
13532#    endif
13533#  endif
13534#endif
13535
13536/*
13537** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
13538** 0 means mutexes are permanently disable and the library is never
13539** threadsafe.  1 means the library is serialized which is the highest
13540** level of threadsafety.  2 means the library is multithreaded - multiple
13541** threads can use SQLite as long as no two threads try to use the same
13542** database connection at the same time.
13543**
13544** Older versions of SQLite used an optional THREADSAFE macro.
13545** We support that for legacy.
13546**
13547** To ensure that the correct value of "THREADSAFE" is reported when querying
13548** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
13549** logic is partially replicated in ctime.c. If it is updated here, it should
13550** also be updated there.
13551*/
13552#if !defined(SQLITE_THREADSAFE)
13553# if defined(THREADSAFE)
13554#   define SQLITE_THREADSAFE THREADSAFE
13555# else
13556#   define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
13557# endif
13558#endif
13559
13560/*
13561** Powersafe overwrite is on by default.  But can be turned off using
13562** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
13563*/
13564#ifndef SQLITE_POWERSAFE_OVERWRITE
13565# define SQLITE_POWERSAFE_OVERWRITE 1
13566#endif
13567
13568/*
13569** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
13570** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
13571** which case memory allocation statistics are disabled by default.
13572*/
13573#if !defined(SQLITE_DEFAULT_MEMSTATUS)
13574# define SQLITE_DEFAULT_MEMSTATUS 1
13575#endif
13576
13577/*
13578** Exactly one of the following macros must be defined in order to
13579** specify which memory allocation subsystem to use.
13580**
13581**     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
13582**     SQLITE_WIN32_MALLOC           // Use Win32 native heap API
13583**     SQLITE_ZERO_MALLOC            // Use a stub allocator that always fails
13584**     SQLITE_MEMDEBUG               // Debugging version of system malloc()
13585**
13586** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
13587** assert() macro is enabled, each call into the Win32 native heap subsystem
13588** will cause HeapValidate to be called.  If heap validation should fail, an
13589** assertion will be triggered.
13590**
13591** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
13592** the default.
13593*/
13594#if defined(SQLITE_SYSTEM_MALLOC) \
13595  + defined(SQLITE_WIN32_MALLOC) \
13596  + defined(SQLITE_ZERO_MALLOC) \
13597  + defined(SQLITE_MEMDEBUG)>1
13598# error "Two or more of the following compile-time configuration options\
13599 are defined but at most one is allowed:\
13600 SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
13601 SQLITE_ZERO_MALLOC"
13602#endif
13603#if defined(SQLITE_SYSTEM_MALLOC) \
13604  + defined(SQLITE_WIN32_MALLOC) \
13605  + defined(SQLITE_ZERO_MALLOC) \
13606  + defined(SQLITE_MEMDEBUG)==0
13607# define SQLITE_SYSTEM_MALLOC 1
13608#endif
13609
13610/*
13611** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
13612** sizes of memory allocations below this value where possible.
13613*/
13614#if !defined(SQLITE_MALLOC_SOFT_LIMIT)
13615# define SQLITE_MALLOC_SOFT_LIMIT 1024
13616#endif
13617
13618/*
13619** We need to define _XOPEN_SOURCE as follows in order to enable
13620** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
13621** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
13622** it.
13623*/
13624#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)
13625#  define _XOPEN_SOURCE 600
13626#endif
13627
13628/*
13629** NDEBUG and SQLITE_DEBUG are opposites.  It should always be true that
13630** defined(NDEBUG)==!defined(SQLITE_DEBUG).  If this is not currently true,
13631** make it true by defining or undefining NDEBUG.
13632**
13633** Setting NDEBUG makes the code smaller and faster by disabling the
13634** assert() statements in the code.  So we want the default action
13635** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
13636** is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
13637** feature.
13638*/
13639#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
13640# define NDEBUG 1
13641#endif
13642#if defined(NDEBUG) && defined(SQLITE_DEBUG)
13643# undef NDEBUG
13644#endif
13645
13646/*
13647** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
13648*/
13649#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
13650# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
13651#endif
13652
13653/*
13654** The testcase() macro is used to aid in coverage testing.  When
13655** doing coverage testing, the condition inside the argument to
13656** testcase() must be evaluated both true and false in order to
13657** get full branch coverage.  The testcase() macro is inserted
13658** to help ensure adequate test coverage in places where simple
13659** condition/decision coverage is inadequate.  For example, testcase()
13660** can be used to make sure boundary values are tested.  For
13661** bitmask tests, testcase() can be used to make sure each bit
13662** is significant and used at least once.  On switch statements
13663** where multiple cases go to the same block of code, testcase()
13664** can insure that all cases are evaluated.
13665**
13666*/
13667#ifdef SQLITE_COVERAGE_TEST
13668SQLITE_PRIVATE   void sqlite3Coverage(int);
13669# define testcase(X)  if( X ){ sqlite3Coverage(__LINE__); }
13670#else
13671# define testcase(X)
13672#endif
13673
13674/*
13675** The TESTONLY macro is used to enclose variable declarations or
13676** other bits of code that are needed to support the arguments
13677** within testcase() and assert() macros.
13678*/
13679#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
13680# define TESTONLY(X)  X
13681#else
13682# define TESTONLY(X)
13683#endif
13684
13685/*
13686** Sometimes we need a small amount of code such as a variable initialization
13687** to setup for a later assert() statement.  We do not want this code to
13688** appear when assert() is disabled.  The following macro is therefore
13689** used to contain that setup code.  The "VVA" acronym stands for
13690** "Verification, Validation, and Accreditation".  In other words, the
13691** code within VVA_ONLY() will only run during verification processes.
13692*/
13693#ifndef NDEBUG
13694# define VVA_ONLY(X)  X
13695#else
13696# define VVA_ONLY(X)
13697#endif
13698
13699/*
13700** The ALWAYS and NEVER macros surround boolean expressions which
13701** are intended to always be true or false, respectively.  Such
13702** expressions could be omitted from the code completely.  But they
13703** are included in a few cases in order to enhance the resilience
13704** of SQLite to unexpected behavior - to make the code "self-healing"
13705** or "ductile" rather than being "brittle" and crashing at the first
13706** hint of unplanned behavior.
13707**
13708** In other words, ALWAYS and NEVER are added for defensive code.
13709**
13710** When doing coverage testing ALWAYS and NEVER are hard-coded to
13711** be true and false so that the unreachable code they specify will
13712** not be counted as untested code.
13713*/
13714#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
13715# define ALWAYS(X)      (1)
13716# define NEVER(X)       (0)
13717#elif !defined(NDEBUG)
13718# define ALWAYS(X)      ((X)?1:(assert(0),0))
13719# define NEVER(X)       ((X)?(assert(0),1):0)
13720#else
13721# define ALWAYS(X)      (X)
13722# define NEVER(X)       (X)
13723#endif
13724
13725/*
13726** The harmless(X) macro indicates that expression X is usually false
13727** but can be true without causing any problems, but we don't know of
13728** any way to cause X to be true.
13729**
13730** In debugging and testing builds, this macro will abort if X is ever
13731** true.  In this way, developers are alerted to a possible test case
13732** that causes X to be true.  If a harmless macro ever fails, that is
13733** an opportunity to change the macro into a testcase() and add a new
13734** test case to the test suite.
13735**
13736** For normal production builds, harmless(X) is a no-op, since it does
13737** not matter whether expression X is true or false.
13738*/
13739#ifdef SQLITE_DEBUG
13740# define harmless(X)  assert(!(X));
13741#else
13742# define harmless(X)
13743#endif
13744
13745/*
13746** Some conditionals are optimizations only.  In other words, if the
13747** conditionals are replaced with a constant 1 (true) or 0 (false) then
13748** the correct answer is still obtained, though perhaps not as quickly.
13749**
13750** The following macros mark these optimizations conditionals.
13751*/
13752#if defined(SQLITE_MUTATION_TEST)
13753# define OK_IF_ALWAYS_TRUE(X)  (1)
13754# define OK_IF_ALWAYS_FALSE(X) (0)
13755#else
13756# define OK_IF_ALWAYS_TRUE(X)  (X)
13757# define OK_IF_ALWAYS_FALSE(X) (X)
13758#endif
13759
13760/*
13761** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
13762** defined.  We need to defend against those failures when testing with
13763** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
13764** during a normal build.  The following macro can be used to disable tests
13765** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
13766*/
13767#if defined(SQLITE_TEST_REALLOC_STRESS)
13768# define ONLY_IF_REALLOC_STRESS(X)  (X)
13769#elif !defined(NDEBUG)
13770# define ONLY_IF_REALLOC_STRESS(X)  ((X)?(assert(0),1):0)
13771#else
13772# define ONLY_IF_REALLOC_STRESS(X)  (0)
13773#endif
13774
13775/*
13776** Declarations used for tracing the operating system interfaces.
13777*/
13778#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
13779    (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
13780  extern int sqlite3OSTrace;
13781# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
13782# define SQLITE_HAVE_OS_TRACE
13783#else
13784# define OSTRACE(X)
13785# undef  SQLITE_HAVE_OS_TRACE
13786#endif
13787
13788/*
13789** Is the sqlite3ErrName() function needed in the build?  Currently,
13790** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
13791** OSTRACE is enabled), and by several "test*.c" files (which are
13792** compiled using SQLITE_TEST).
13793*/
13794#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
13795    (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
13796# define SQLITE_NEED_ERR_NAME
13797#else
13798# undef  SQLITE_NEED_ERR_NAME
13799#endif
13800
13801/*
13802** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
13803*/
13804#ifdef SQLITE_OMIT_EXPLAIN
13805# undef SQLITE_ENABLE_EXPLAIN_COMMENTS
13806#endif
13807
13808/*
13809** Return true (non-zero) if the input is an integer that is too large
13810** to fit in 32-bits.  This macro is used inside of various testcase()
13811** macros to verify that we have tested SQLite for large-file support.
13812*/
13813#define IS_BIG_INT(X)  (((X)&~(i64)0xffffffff)!=0)
13814
13815/*
13816** The macro unlikely() is a hint that surrounds a boolean
13817** expression that is usually false.  Macro likely() surrounds
13818** a boolean expression that is usually true.  These hints could,
13819** in theory, be used by the compiler to generate better code, but
13820** currently they are just comments for human readers.
13821*/
13822#define likely(X)    (X)
13823#define unlikely(X)  (X)
13824
13825/************** Include hash.h in the middle of sqliteInt.h ******************/
13826/************** Begin file hash.h ********************************************/
13827/*
13828** 2001 September 22
13829**
13830** The author disclaims copyright to this source code.  In place of
13831** a legal notice, here is a blessing:
13832**
13833**    May you do good and not evil.
13834**    May you find forgiveness for yourself and forgive others.
13835**    May you share freely, never taking more than you give.
13836**
13837*************************************************************************
13838** This is the header file for the generic hash-table implementation
13839** used in SQLite.
13840*/
13841#ifndef SQLITE_HASH_H
13842#define SQLITE_HASH_H
13843
13844/* Forward declarations of structures. */
13845typedef struct Hash Hash;
13846typedef struct HashElem HashElem;
13847
13848/* A complete hash table is an instance of the following structure.
13849** The internals of this structure are intended to be opaque -- client
13850** code should not attempt to access or modify the fields of this structure
13851** directly.  Change this structure only by using the routines below.
13852** However, some of the "procedures" and "functions" for modifying and
13853** accessing this structure are really macros, so we can't really make
13854** this structure opaque.
13855**
13856** All elements of the hash table are on a single doubly-linked list.
13857** Hash.first points to the head of this list.
13858**
13859** There are Hash.htsize buckets.  Each bucket points to a spot in
13860** the global doubly-linked list.  The contents of the bucket are the
13861** element pointed to plus the next _ht.count-1 elements in the list.
13862**
13863** Hash.htsize and Hash.ht may be zero.  In that case lookup is done
13864** by a linear search of the global list.  For small tables, the
13865** Hash.ht table is never allocated because if there are few elements
13866** in the table, it is faster to do a linear search than to manage
13867** the hash table.
13868*/
13869struct Hash {
13870  unsigned int htsize;      /* Number of buckets in the hash table */
13871  unsigned int count;       /* Number of entries in this table */
13872  HashElem *first;          /* The first element of the array */
13873  struct _ht {              /* the hash table */
13874    unsigned int count;        /* Number of entries with this hash */
13875    HashElem *chain;           /* Pointer to first entry with this hash */
13876  } *ht;
13877};
13878
13879/* Each element in the hash table is an instance of the following
13880** structure.  All elements are stored on a single doubly-linked list.
13881**
13882** Again, this structure is intended to be opaque, but it can't really
13883** be opaque because it is used by macros.
13884*/
13885struct HashElem {
13886  HashElem *next, *prev;       /* Next and previous elements in the table */
13887  void *data;                  /* Data associated with this element */
13888  const char *pKey;            /* Key associated with this element */
13889};
13890
13891/*
13892** Access routines.  To delete, insert a NULL pointer.
13893*/
13894SQLITE_PRIVATE void sqlite3HashInit(Hash*);
13895SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, void *pData);
13896SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey);
13897SQLITE_PRIVATE void sqlite3HashClear(Hash*);
13898
13899/*
13900** Macros for looping over all elements of a hash table.  The idiom is
13901** like this:
13902**
13903**   Hash h;
13904**   HashElem *p;
13905**   ...
13906**   for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
13907**     SomeStructure *pData = sqliteHashData(p);
13908**     // do something with pData
13909**   }
13910*/
13911#define sqliteHashFirst(H)  ((H)->first)
13912#define sqliteHashNext(E)   ((E)->next)
13913#define sqliteHashData(E)   ((E)->data)
13914/* #define sqliteHashKey(E)    ((E)->pKey) // NOT USED */
13915/* #define sqliteHashKeysize(E) ((E)->nKey)  // NOT USED */
13916
13917/*
13918** Number of entries in a hash table
13919*/
13920/* #define sqliteHashCount(H)  ((H)->count) // NOT USED */
13921
13922#endif /* SQLITE_HASH_H */
13923
13924/************** End of hash.h ************************************************/
13925/************** Continuing where we left off in sqliteInt.h ******************/
13926/************** Include parse.h in the middle of sqliteInt.h *****************/
13927/************** Begin file parse.h *******************************************/
13928#define TK_SEMI                             1
13929#define TK_EXPLAIN                          2
13930#define TK_QUERY                            3
13931#define TK_PLAN                             4
13932#define TK_BEGIN                            5
13933#define TK_TRANSACTION                      6
13934#define TK_DEFERRED                         7
13935#define TK_IMMEDIATE                        8
13936#define TK_EXCLUSIVE                        9
13937#define TK_COMMIT                          10
13938#define TK_END                             11
13939#define TK_ROLLBACK                        12
13940#define TK_SAVEPOINT                       13
13941#define TK_RELEASE                         14
13942#define TK_TO                              15
13943#define TK_TABLE                           16
13944#define TK_CREATE                          17
13945#define TK_IF                              18
13946#define TK_NOT                             19
13947#define TK_EXISTS                          20
13948#define TK_TEMP                            21
13949#define TK_LP                              22
13950#define TK_RP                              23
13951#define TK_AS                              24
13952#define TK_WITHOUT                         25
13953#define TK_COMMA                           26
13954#define TK_ABORT                           27
13955#define TK_ACTION                          28
13956#define TK_AFTER                           29
13957#define TK_ANALYZE                         30
13958#define TK_ASC                             31
13959#define TK_ATTACH                          32
13960#define TK_BEFORE                          33
13961#define TK_BY                              34
13962#define TK_CASCADE                         35
13963#define TK_CAST                            36
13964#define TK_CONFLICT                        37
13965#define TK_DATABASE                        38
13966#define TK_DESC                            39
13967#define TK_DETACH                          40
13968#define TK_EACH                            41
13969#define TK_FAIL                            42
13970#define TK_OR                              43
13971#define TK_AND                             44
13972#define TK_IS                              45
13973#define TK_MATCH                           46
13974#define TK_LIKE_KW                         47
13975#define TK_BETWEEN                         48
13976#define TK_IN                              49
13977#define TK_ISNULL                          50
13978#define TK_NOTNULL                         51
13979#define TK_NE                              52
13980#define TK_EQ                              53
13981#define TK_GT                              54
13982#define TK_LE                              55
13983#define TK_LT                              56
13984#define TK_GE                              57
13985#define TK_ESCAPE                          58
13986#define TK_ID                              59
13987#define TK_COLUMNKW                        60
13988#define TK_DO                              61
13989#define TK_FOR                             62
13990#define TK_IGNORE                          63
13991#define TK_INITIALLY                       64
13992#define TK_INSTEAD                         65
13993#define TK_NO                              66
13994#define TK_KEY                             67
13995#define TK_OF                              68
13996#define TK_OFFSET                          69
13997#define TK_PRAGMA                          70
13998#define TK_RAISE                           71
13999#define TK_RECURSIVE                       72
14000#define TK_REPLACE                         73
14001#define TK_RESTRICT                        74
14002#define TK_ROW                             75
14003#define TK_ROWS                            76
14004#define TK_TRIGGER                         77
14005#define TK_VACUUM                          78
14006#define TK_VIEW                            79
14007#define TK_VIRTUAL                         80
14008#define TK_WITH                            81
14009#define TK_NULLS                           82
14010#define TK_FIRST                           83
14011#define TK_LAST                            84
14012#define TK_CURRENT                         85
14013#define TK_FOLLOWING                       86
14014#define TK_PARTITION                       87
14015#define TK_PRECEDING                       88
14016#define TK_RANGE                           89
14017#define TK_UNBOUNDED                       90
14018#define TK_EXCLUDE                         91
14019#define TK_GROUPS                          92
14020#define TK_OTHERS                          93
14021#define TK_TIES                            94
14022#define TK_GENERATED                       95
14023#define TK_ALWAYS                          96
14024#define TK_REINDEX                         97
14025#define TK_RENAME                          98
14026#define TK_CTIME_KW                        99
14027#define TK_ANY                            100
14028#define TK_BITAND                         101
14029#define TK_BITOR                          102
14030#define TK_LSHIFT                         103
14031#define TK_RSHIFT                         104
14032#define TK_PLUS                           105
14033#define TK_MINUS                          106
14034#define TK_STAR                           107
14035#define TK_SLASH                          108
14036#define TK_REM                            109
14037#define TK_CONCAT                         110
14038#define TK_COLLATE                        111
14039#define TK_BITNOT                         112
14040#define TK_ON                             113
14041#define TK_INDEXED                        114
14042#define TK_STRING                         115
14043#define TK_JOIN_KW                        116
14044#define TK_CONSTRAINT                     117
14045#define TK_DEFAULT                        118
14046#define TK_NULL                           119
14047#define TK_PRIMARY                        120
14048#define TK_UNIQUE                         121
14049#define TK_CHECK                          122
14050#define TK_REFERENCES                     123
14051#define TK_AUTOINCR                       124
14052#define TK_INSERT                         125
14053#define TK_DELETE                         126
14054#define TK_UPDATE                         127
14055#define TK_SET                            128
14056#define TK_DEFERRABLE                     129
14057#define TK_FOREIGN                        130
14058#define TK_DROP                           131
14059#define TK_UNION                          132
14060#define TK_ALL                            133
14061#define TK_EXCEPT                         134
14062#define TK_INTERSECT                      135
14063#define TK_SELECT                         136
14064#define TK_VALUES                         137
14065#define TK_DISTINCT                       138
14066#define TK_DOT                            139
14067#define TK_FROM                           140
14068#define TK_JOIN                           141
14069#define TK_USING                          142
14070#define TK_ORDER                          143
14071#define TK_GROUP                          144
14072#define TK_HAVING                         145
14073#define TK_LIMIT                          146
14074#define TK_WHERE                          147
14075#define TK_INTO                           148
14076#define TK_NOTHING                        149
14077#define TK_FLOAT                          150
14078#define TK_BLOB                           151
14079#define TK_INTEGER                        152
14080#define TK_VARIABLE                       153
14081#define TK_CASE                           154
14082#define TK_WHEN                           155
14083#define TK_THEN                           156
14084#define TK_ELSE                           157
14085#define TK_INDEX                          158
14086#define TK_ALTER                          159
14087#define TK_ADD                            160
14088#define TK_WINDOW                         161
14089#define TK_OVER                           162
14090#define TK_FILTER                         163
14091#define TK_COLUMN                         164
14092#define TK_AGG_FUNCTION                   165
14093#define TK_AGG_COLUMN                     166
14094#define TK_TRUEFALSE                      167
14095#define TK_ISNOT                          168
14096#define TK_FUNCTION                       169
14097#define TK_UMINUS                         170
14098#define TK_UPLUS                          171
14099#define TK_TRUTH                          172
14100#define TK_REGISTER                       173
14101#define TK_VECTOR                         174
14102#define TK_SELECT_COLUMN                  175
14103#define TK_IF_NULL_ROW                    176
14104#define TK_ASTERISK                       177
14105#define TK_SPAN                           178
14106#define TK_SPACE                          179
14107#define TK_ILLEGAL                        180
14108
14109/************** End of parse.h ***********************************************/
14110/************** Continuing where we left off in sqliteInt.h ******************/
14111#include <stdio.h>
14112#include <stdlib.h>
14113#include <string.h>
14114#include <assert.h>
14115#include <stddef.h>
14116
14117/*
14118** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
14119** This allows better measurements of where memcpy() is used when running
14120** cachegrind.  But this macro version of memcpy() is very slow so it
14121** should not be used in production.  This is a performance measurement
14122** hack only.
14123*/
14124#ifdef SQLITE_INLINE_MEMCPY
14125# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
14126                        int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
14127#endif
14128
14129/*
14130** If compiling for a processor that lacks floating point support,
14131** substitute integer for floating-point
14132*/
14133#ifdef SQLITE_OMIT_FLOATING_POINT
14134# define double sqlite_int64
14135# define float sqlite_int64
14136# define LONGDOUBLE_TYPE sqlite_int64
14137# ifndef SQLITE_BIG_DBL
14138#   define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
14139# endif
14140# define SQLITE_OMIT_DATETIME_FUNCS 1
14141# define SQLITE_OMIT_TRACE 1
14142# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
14143# undef SQLITE_HAVE_ISNAN
14144#endif
14145#ifndef SQLITE_BIG_DBL
14146# define SQLITE_BIG_DBL (1e99)
14147#endif
14148
14149/*
14150** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
14151** afterward. Having this macro allows us to cause the C compiler
14152** to omit code used by TEMP tables without messy #ifndef statements.
14153*/
14154#ifdef SQLITE_OMIT_TEMPDB
14155#define OMIT_TEMPDB 1
14156#else
14157#define OMIT_TEMPDB 0
14158#endif
14159
14160/*
14161** The "file format" number is an integer that is incremented whenever
14162** the VDBE-level file format changes.  The following macros define the
14163** the default file format for new databases and the maximum file format
14164** that the library can read.
14165*/
14166#define SQLITE_MAX_FILE_FORMAT 4
14167#ifndef SQLITE_DEFAULT_FILE_FORMAT
14168# define SQLITE_DEFAULT_FILE_FORMAT 4
14169#endif
14170
14171/*
14172** Determine whether triggers are recursive by default.  This can be
14173** changed at run-time using a pragma.
14174*/
14175#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
14176# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
14177#endif
14178
14179/*
14180** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
14181** on the command-line
14182*/
14183#ifndef SQLITE_TEMP_STORE
14184# define SQLITE_TEMP_STORE 1
14185#endif
14186
14187/*
14188** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
14189** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
14190** to zero.
14191*/
14192#if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0
14193# undef SQLITE_MAX_WORKER_THREADS
14194# define SQLITE_MAX_WORKER_THREADS 0
14195#endif
14196#ifndef SQLITE_MAX_WORKER_THREADS
14197# define SQLITE_MAX_WORKER_THREADS 8
14198#endif
14199#ifndef SQLITE_DEFAULT_WORKER_THREADS
14200# define SQLITE_DEFAULT_WORKER_THREADS 0
14201#endif
14202#if SQLITE_DEFAULT_WORKER_THREADS>SQLITE_MAX_WORKER_THREADS
14203# undef SQLITE_MAX_WORKER_THREADS
14204# define SQLITE_MAX_WORKER_THREADS SQLITE_DEFAULT_WORKER_THREADS
14205#endif
14206
14207/*
14208** The default initial allocation for the pagecache when using separate
14209** pagecaches for each database connection.  A positive number is the
14210** number of pages.  A negative number N translations means that a buffer
14211** of -1024*N bytes is allocated and used for as many pages as it will hold.
14212**
14213** The default value of "20" was choosen to minimize the run-time of the
14214** speedtest1 test program with options: --shrink-memory --reprepare
14215*/
14216#ifndef SQLITE_DEFAULT_PCACHE_INITSZ
14217# define SQLITE_DEFAULT_PCACHE_INITSZ 20
14218#endif
14219
14220/*
14221** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.
14222*/
14223#ifndef SQLITE_DEFAULT_SORTERREF_SIZE
14224# define SQLITE_DEFAULT_SORTERREF_SIZE 0x7fffffff
14225#endif
14226
14227/*
14228** The compile-time options SQLITE_MMAP_READWRITE and
14229** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
14230** You must choose one or the other (or neither) but not both.
14231*/
14232#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
14233#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
14234#endif
14235
14236/*
14237** GCC does not define the offsetof() macro so we'll have to do it
14238** ourselves.
14239*/
14240#ifndef offsetof
14241#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
14242#endif
14243
14244/*
14245** Macros to compute minimum and maximum of two numbers.
14246*/
14247#ifndef MIN
14248# define MIN(A,B) ((A)<(B)?(A):(B))
14249#endif
14250#ifndef MAX
14251# define MAX(A,B) ((A)>(B)?(A):(B))
14252#endif
14253
14254/*
14255** Swap two objects of type TYPE.
14256*/
14257#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
14258
14259/*
14260** Check to see if this machine uses EBCDIC.  (Yes, believe it or
14261** not, there are still machines out there that use EBCDIC.)
14262*/
14263#if 'A' == '\301'
14264# define SQLITE_EBCDIC 1
14265#else
14266# define SQLITE_ASCII 1
14267#endif
14268
14269/*
14270** Integers of known sizes.  These typedefs might change for architectures
14271** where the sizes very.  Preprocessor macros are available so that the
14272** types can be conveniently redefined at compile-type.  Like this:
14273**
14274**         cc '-DUINTPTR_TYPE=long long int' ...
14275*/
14276#ifndef UINT32_TYPE
14277# ifdef HAVE_UINT32_T
14278#  define UINT32_TYPE uint32_t
14279# else
14280#  define UINT32_TYPE unsigned int
14281# endif
14282#endif
14283#ifndef UINT16_TYPE
14284# ifdef HAVE_UINT16_T
14285#  define UINT16_TYPE uint16_t
14286# else
14287#  define UINT16_TYPE unsigned short int
14288# endif
14289#endif
14290#ifndef INT16_TYPE
14291# ifdef HAVE_INT16_T
14292#  define INT16_TYPE int16_t
14293# else
14294#  define INT16_TYPE short int
14295# endif
14296#endif
14297#ifndef UINT8_TYPE
14298# ifdef HAVE_UINT8_T
14299#  define UINT8_TYPE uint8_t
14300# else
14301#  define UINT8_TYPE unsigned char
14302# endif
14303#endif
14304#ifndef INT8_TYPE
14305# ifdef HAVE_INT8_T
14306#  define INT8_TYPE int8_t
14307# else
14308#  define INT8_TYPE signed char
14309# endif
14310#endif
14311#ifndef LONGDOUBLE_TYPE
14312# define LONGDOUBLE_TYPE long double
14313#endif
14314typedef sqlite_int64 i64;          /* 8-byte signed integer */
14315typedef sqlite_uint64 u64;         /* 8-byte unsigned integer */
14316typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
14317typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
14318typedef INT16_TYPE i16;            /* 2-byte signed integer */
14319typedef UINT8_TYPE u8;             /* 1-byte unsigned integer */
14320typedef INT8_TYPE i8;              /* 1-byte signed integer */
14321
14322/*
14323** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
14324** that can be stored in a u32 without loss of data.  The value
14325** is 0x00000000ffffffff.  But because of quirks of some compilers, we
14326** have to specify the value in the less intuitive manner shown:
14327*/
14328#define SQLITE_MAX_U32  ((((u64)1)<<32)-1)
14329
14330/*
14331** The datatype used to store estimates of the number of rows in a
14332** table or index.  This is an unsigned integer type.  For 99.9% of
14333** the world, a 32-bit integer is sufficient.  But a 64-bit integer
14334** can be used at compile-time if desired.
14335*/
14336#ifdef SQLITE_64BIT_STATS
14337 typedef u64 tRowcnt;    /* 64-bit only if requested at compile-time */
14338#else
14339 typedef u32 tRowcnt;    /* 32-bit is the default */
14340#endif
14341
14342/*
14343** Estimated quantities used for query planning are stored as 16-bit
14344** logarithms.  For quantity X, the value stored is 10*log2(X).  This
14345** gives a possible range of values of approximately 1.0e986 to 1e-986.
14346** But the allowed values are "grainy".  Not every value is representable.
14347** For example, quantities 16 and 17 are both represented by a LogEst
14348** of 40.  However, since LogEst quantities are suppose to be estimates,
14349** not exact values, this imprecision is not a problem.
14350**
14351** "LogEst" is short for "Logarithmic Estimate".
14352**
14353** Examples:
14354**      1 -> 0              20 -> 43          10000 -> 132
14355**      2 -> 10             25 -> 46          25000 -> 146
14356**      3 -> 16            100 -> 66        1000000 -> 199
14357**      4 -> 20           1000 -> 99        1048576 -> 200
14358**     10 -> 33           1024 -> 100    4294967296 -> 320
14359**
14360** The LogEst can be negative to indicate fractional values.
14361** Examples:
14362**
14363**    0.5 -> -10           0.1 -> -33        0.0625 -> -40
14364*/
14365typedef INT16_TYPE LogEst;
14366
14367/*
14368** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
14369*/
14370#ifndef SQLITE_PTRSIZE
14371# if defined(__SIZEOF_POINTER__)
14372#   define SQLITE_PTRSIZE __SIZEOF_POINTER__
14373# elif defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
14374       defined(_M_ARM)   || defined(__arm__)    || defined(__x86)   ||    \
14375      (defined(__TOS_AIX__) && !defined(__64BIT__))
14376#   define SQLITE_PTRSIZE 4
14377# else
14378#   define SQLITE_PTRSIZE 8
14379# endif
14380#endif
14381
14382/* The uptr type is an unsigned integer large enough to hold a pointer
14383*/
14384#if defined(HAVE_STDINT_H)
14385  typedef uintptr_t uptr;
14386#elif SQLITE_PTRSIZE==4
14387  typedef u32 uptr;
14388#else
14389  typedef u64 uptr;
14390#endif
14391
14392/*
14393** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
14394** something between S (inclusive) and E (exclusive).
14395**
14396** In other words, S is a buffer and E is a pointer to the first byte after
14397** the end of buffer S.  This macro returns true if P points to something
14398** contained within the buffer S.
14399*/
14400#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
14401
14402
14403/*
14404** Macros to determine whether the machine is big or little endian,
14405** and whether or not that determination is run-time or compile-time.
14406**
14407** For best performance, an attempt is made to guess at the byte-order
14408** using C-preprocessor macros.  If that is unsuccessful, or if
14409** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
14410** at run-time.
14411*/
14412#ifndef SQLITE_BYTEORDER
14413# if defined(i386)      || defined(__i386__)      || defined(_M_IX86) ||    \
14414     defined(__x86_64)  || defined(__x86_64__)    || defined(_M_X64)  ||    \
14415     defined(_M_AMD64)  || defined(_M_ARM)        || defined(__x86)   ||    \
14416     defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
14417#   define SQLITE_BYTEORDER    1234
14418# elif defined(sparc)     || defined(__ppc__) || \
14419       defined(__ARMEB__) || defined(__AARCH64EB__)
14420#   define SQLITE_BYTEORDER    4321
14421# else
14422#   define SQLITE_BYTEORDER 0
14423# endif
14424#endif
14425#if SQLITE_BYTEORDER==4321
14426# define SQLITE_BIGENDIAN    1
14427# define SQLITE_LITTLEENDIAN 0
14428# define SQLITE_UTF16NATIVE  SQLITE_UTF16BE
14429#elif SQLITE_BYTEORDER==1234
14430# define SQLITE_BIGENDIAN    0
14431# define SQLITE_LITTLEENDIAN 1
14432# define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
14433#else
14434# ifdef SQLITE_AMALGAMATION
14435  const int sqlite3one = 1;
14436# else
14437  extern const int sqlite3one;
14438# endif
14439# define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
14440# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
14441# define SQLITE_UTF16NATIVE  (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
14442#endif
14443
14444/*
14445** Constants for the largest and smallest possible 64-bit signed integers.
14446** These macros are designed to work correctly on both 32-bit and 64-bit
14447** compilers.
14448*/
14449#define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
14450#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
14451
14452/*
14453** Round up a number to the next larger multiple of 8.  This is used
14454** to force 8-byte alignment on 64-bit architectures.
14455*/
14456#define ROUND8(x)     (((x)+7)&~7)
14457
14458/*
14459** Round down to the nearest multiple of 8
14460*/
14461#define ROUNDDOWN8(x) ((x)&~7)
14462
14463/*
14464** Assert that the pointer X is aligned to an 8-byte boundary.  This
14465** macro is used only within assert() to verify that the code gets
14466** all alignment restrictions correct.
14467**
14468** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
14469** underlying malloc() implementation might return us 4-byte aligned
14470** pointers.  In that case, only verify 4-byte alignment.
14471*/
14472#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
14473# define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&3)==0)
14474#else
14475# define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&7)==0)
14476#endif
14477
14478/*
14479** Disable MMAP on platforms where it is known to not work
14480*/
14481#if defined(__OpenBSD__) || defined(__QNXNTO__)
14482# undef SQLITE_MAX_MMAP_SIZE
14483# define SQLITE_MAX_MMAP_SIZE 0
14484#endif
14485
14486/*
14487** Default maximum size of memory used by memory-mapped I/O in the VFS
14488*/
14489#ifdef __APPLE__
14490# include <TargetConditionals.h>
14491#endif
14492#ifndef SQLITE_MAX_MMAP_SIZE
14493# if defined(__linux__) \
14494  || defined(_WIN32) \
14495  || (defined(__APPLE__) && defined(__MACH__)) \
14496  || defined(__sun) \
14497  || defined(__FreeBSD__) \
14498  || defined(__DragonFly__)
14499#   define SQLITE_MAX_MMAP_SIZE 0x7fff0000  /* 2147418112 */
14500# else
14501#   define SQLITE_MAX_MMAP_SIZE 0
14502# endif
14503#endif
14504
14505/*
14506** The default MMAP_SIZE is zero on all platforms.  Or, even if a larger
14507** default MMAP_SIZE is specified at compile-time, make sure that it does
14508** not exceed the maximum mmap size.
14509*/
14510#ifndef SQLITE_DEFAULT_MMAP_SIZE
14511# define SQLITE_DEFAULT_MMAP_SIZE 0
14512#endif
14513#if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
14514# undef SQLITE_DEFAULT_MMAP_SIZE
14515# define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
14516#endif
14517
14518/*
14519** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not
14520** the Select query generator tracing logic is turned on.
14521*/
14522#if defined(SQLITE_ENABLE_SELECTTRACE)
14523# define SELECTTRACE_ENABLED 1
14524#else
14525# define SELECTTRACE_ENABLED 0
14526#endif
14527
14528/*
14529** An instance of the following structure is used to store the busy-handler
14530** callback for a given sqlite handle.
14531**
14532** The sqlite.busyHandler member of the sqlite struct contains the busy
14533** callback for the database handle. Each pager opened via the sqlite
14534** handle is passed a pointer to sqlite.busyHandler. The busy-handler
14535** callback is currently invoked only from within pager.c.
14536*/
14537typedef struct BusyHandler BusyHandler;
14538struct BusyHandler {
14539  int (*xBusyHandler)(void *,int);  /* The busy callback */
14540  void *pBusyArg;                   /* First arg to busy callback */
14541  int nBusy;                        /* Incremented with each busy call */
14542};
14543
14544/*
14545** Name of the master database table.  The master database table
14546** is a special table that holds the names and attributes of all
14547** user tables and indices.
14548*/
14549#define MASTER_NAME       "sqlite_master"
14550#define TEMP_MASTER_NAME  "sqlite_temp_master"
14551
14552/*
14553** The root-page of the master database table.
14554*/
14555#define MASTER_ROOT       1
14556
14557/*
14558** The name of the schema table.
14559*/
14560#define SCHEMA_TABLE(x)  ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
14561
14562/*
14563** A convenience macro that returns the number of elements in
14564** an array.
14565*/
14566#define ArraySize(X)    ((int)(sizeof(X)/sizeof(X[0])))
14567
14568/*
14569** Determine if the argument is a power of two
14570*/
14571#define IsPowerOfTwo(X) (((X)&((X)-1))==0)
14572
14573/*
14574** The following value as a destructor means to use sqlite3DbFree().
14575** The sqlite3DbFree() routine requires two parameters instead of the
14576** one parameter that destructors normally want.  So we have to introduce
14577** this magic value that the code knows to handle differently.  Any
14578** pointer will work here as long as it is distinct from SQLITE_STATIC
14579** and SQLITE_TRANSIENT.
14580*/
14581#define SQLITE_DYNAMIC   ((sqlite3_destructor_type)sqlite3MallocSize)
14582
14583/*
14584** When SQLITE_OMIT_WSD is defined, it means that the target platform does
14585** not support Writable Static Data (WSD) such as global and static variables.
14586** All variables must either be on the stack or dynamically allocated from
14587** the heap.  When WSD is unsupported, the variable declarations scattered
14588** throughout the SQLite code must become constants instead.  The SQLITE_WSD
14589** macro is used for this purpose.  And instead of referencing the variable
14590** directly, we use its constant as a key to lookup the run-time allocated
14591** buffer that holds real variable.  The constant is also the initializer
14592** for the run-time allocated buffer.
14593**
14594** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
14595** macros become no-ops and have zero performance impact.
14596*/
14597#ifdef SQLITE_OMIT_WSD
14598  #define SQLITE_WSD const
14599  #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
14600  #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
14601SQLITE_API int sqlite3_wsd_init(int N, int J);
14602SQLITE_API void *sqlite3_wsd_find(void *K, int L);
14603#else
14604  #define SQLITE_WSD
14605  #define GLOBAL(t,v) v
14606  #define sqlite3GlobalConfig sqlite3Config
14607#endif
14608
14609/*
14610** The following macros are used to suppress compiler warnings and to
14611** make it clear to human readers when a function parameter is deliberately
14612** left unused within the body of a function. This usually happens when
14613** a function is called via a function pointer. For example the
14614** implementation of an SQL aggregate step callback may not use the
14615** parameter indicating the number of arguments passed to the aggregate,
14616** if it knows that this is enforced elsewhere.
14617**
14618** When a function parameter is not used at all within the body of a function,
14619** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
14620** However, these macros may also be used to suppress warnings related to
14621** parameters that may or may not be used depending on compilation options.
14622** For example those parameters only used in assert() statements. In these
14623** cases the parameters are named as per the usual conventions.
14624*/
14625#define UNUSED_PARAMETER(x) (void)(x)
14626#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
14627
14628/*
14629** Forward references to structures
14630*/
14631typedef struct AggInfo AggInfo;
14632typedef struct AuthContext AuthContext;
14633typedef struct AutoincInfo AutoincInfo;
14634typedef struct Bitvec Bitvec;
14635typedef struct CollSeq CollSeq;
14636typedef struct Column Column;
14637typedef struct Db Db;
14638typedef struct Schema Schema;
14639typedef struct Expr Expr;
14640typedef struct ExprList ExprList;
14641typedef struct FKey FKey;
14642typedef struct FuncDestructor FuncDestructor;
14643typedef struct FuncDef FuncDef;
14644typedef struct FuncDefHash FuncDefHash;
14645typedef struct IdList IdList;
14646typedef struct Index Index;
14647typedef struct IndexSample IndexSample;
14648typedef struct KeyClass KeyClass;
14649typedef struct KeyInfo KeyInfo;
14650typedef struct Lookaside Lookaside;
14651typedef struct LookasideSlot LookasideSlot;
14652typedef struct Module Module;
14653typedef struct NameContext NameContext;
14654typedef struct Parse Parse;
14655typedef struct PreUpdate PreUpdate;
14656typedef struct PrintfArguments PrintfArguments;
14657typedef struct RenameToken RenameToken;
14658typedef struct RowSet RowSet;
14659typedef struct Savepoint Savepoint;
14660typedef struct Select Select;
14661typedef struct SQLiteThread SQLiteThread;
14662typedef struct SelectDest SelectDest;
14663typedef struct SrcList SrcList;
14664typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
14665typedef struct Table Table;
14666typedef struct TableLock TableLock;
14667typedef struct Token Token;
14668typedef struct TreeView TreeView;
14669typedef struct Trigger Trigger;
14670typedef struct TriggerPrg TriggerPrg;
14671typedef struct TriggerStep TriggerStep;
14672typedef struct UnpackedRecord UnpackedRecord;
14673typedef struct Upsert Upsert;
14674typedef struct VTable VTable;
14675typedef struct VtabCtx VtabCtx;
14676typedef struct Walker Walker;
14677typedef struct WhereInfo WhereInfo;
14678typedef struct Window Window;
14679typedef struct With With;
14680
14681
14682/*
14683** The bitmask datatype defined below is used for various optimizations.
14684**
14685** Changing this from a 64-bit to a 32-bit type limits the number of
14686** tables in a join to 32 instead of 64.  But it also reduces the size
14687** of the library by 738 bytes on ix86.
14688*/
14689#ifdef SQLITE_BITMASK_TYPE
14690  typedef SQLITE_BITMASK_TYPE Bitmask;
14691#else
14692  typedef u64 Bitmask;
14693#endif
14694
14695/*
14696** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
14697*/
14698#define BMS  ((int)(sizeof(Bitmask)*8))
14699
14700/*
14701** A bit in a Bitmask
14702*/
14703#define MASKBIT(n)   (((Bitmask)1)<<(n))
14704#define MASKBIT64(n) (((u64)1)<<(n))
14705#define MASKBIT32(n) (((unsigned int)1)<<(n))
14706#define ALLBITS      ((Bitmask)-1)
14707
14708/* A VList object records a mapping between parameters/variables/wildcards
14709** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
14710** variable number associated with that parameter.  See the format description
14711** on the sqlite3VListAdd() routine for more information.  A VList is really
14712** just an array of integers.
14713*/
14714typedef int VList;
14715
14716/*
14717** Defer sourcing vdbe.h and btree.h until after the "u8" and
14718** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
14719** pointer types (i.e. FuncDef) defined above.
14720*/
14721/************** Include btree.h in the middle of sqliteInt.h *****************/
14722/************** Begin file btree.h *******************************************/
14723/*
14724** 2001 September 15
14725**
14726** The author disclaims copyright to this source code.  In place of
14727** a legal notice, here is a blessing:
14728**
14729**    May you do good and not evil.
14730**    May you find forgiveness for yourself and forgive others.
14731**    May you share freely, never taking more than you give.
14732**
14733*************************************************************************
14734** This header file defines the interface that the sqlite B-Tree file
14735** subsystem.  See comments in the source code for a detailed description
14736** of what each interface routine does.
14737*/
14738#ifndef SQLITE_BTREE_H
14739#define SQLITE_BTREE_H
14740
14741/* TODO: This definition is just included so other modules compile. It
14742** needs to be revisited.
14743*/
14744#define SQLITE_N_BTREE_META 16
14745
14746/*
14747** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
14748** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
14749*/
14750#ifndef SQLITE_DEFAULT_AUTOVACUUM
14751  #define SQLITE_DEFAULT_AUTOVACUUM 0
14752#endif
14753
14754#define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
14755#define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
14756#define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
14757
14758/*
14759** Forward declarations of structure
14760*/
14761typedef struct Btree Btree;
14762typedef struct BtCursor BtCursor;
14763typedef struct BtShared BtShared;
14764typedef struct BtreePayload BtreePayload;
14765
14766
14767SQLITE_PRIVATE int sqlite3BtreeOpen(
14768  sqlite3_vfs *pVfs,       /* VFS to use with this b-tree */
14769  const char *zFilename,   /* Name of database file to open */
14770  sqlite3 *db,             /* Associated database connection */
14771  Btree **ppBtree,         /* Return open Btree* here */
14772  int flags,               /* Flags */
14773  int vfsFlags             /* Flags passed through to VFS open */
14774);
14775
14776/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
14777** following values.
14778**
14779** NOTE:  These values must match the corresponding PAGER_ values in
14780** pager.h.
14781*/
14782#define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
14783#define BTREE_MEMORY        2  /* This is an in-memory DB */
14784#define BTREE_SINGLE        4  /* The file contains at most 1 b-tree */
14785#define BTREE_UNORDERED     8  /* Use of a hash implementation is OK */
14786
14787SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
14788SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
14789SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree*,int);
14790#if SQLITE_MAX_MMAP_SIZE>0
14791SQLITE_PRIVATE   int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
14792#endif
14793SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
14794SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
14795SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
14796SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
14797SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
14798SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
14799SQLITE_PRIVATE int sqlite3BtreeGetRequestedReserve(Btree*);
14800SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
14801SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
14802SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
14803SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int,int*);
14804SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
14805SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
14806SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
14807SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int,int);
14808SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
14809SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
14810SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
14811SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
14812SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
14813SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
14814SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
14815#ifndef SQLITE_OMIT_SHARED_CACHE
14816SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
14817#endif
14818SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
14819
14820SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
14821SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
14822SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
14823
14824SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
14825
14826/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
14827** of the flags shown below.
14828**
14829** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
14830** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
14831** is stored in the leaves.  (BTREE_INTKEY is used for SQL tables.)  With
14832** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
14833** anywhere - the key is the content.  (BTREE_BLOBKEY is used for SQL
14834** indices.)
14835*/
14836#define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
14837#define BTREE_BLOBKEY    2    /* Table has keys only - no data */
14838
14839SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
14840SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
14841SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*);
14842SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree*, int, int);
14843
14844SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
14845SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
14846
14847SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
14848
14849/*
14850** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
14851** should be one of the following values. The integer values are assigned
14852** to constants so that the offset of the corresponding field in an
14853** SQLite database header may be found using the following formula:
14854**
14855**   offset = 36 + (idx * 4)
14856**
14857** For example, the free-page-count field is located at byte offset 36 of
14858** the database file header. The incr-vacuum-flag field is located at
14859** byte offset 64 (== 36+4*7).
14860**
14861** The BTREE_DATA_VERSION value is not really a value stored in the header.
14862** It is a read-only number computed by the pager.  But we merge it with
14863** the header value access routines since its access pattern is the same.
14864** Call it a "virtual meta value".
14865*/
14866#define BTREE_FREE_PAGE_COUNT     0
14867#define BTREE_SCHEMA_VERSION      1
14868#define BTREE_FILE_FORMAT         2
14869#define BTREE_DEFAULT_CACHE_SIZE  3
14870#define BTREE_LARGEST_ROOT_PAGE   4
14871#define BTREE_TEXT_ENCODING       5
14872#define BTREE_USER_VERSION        6
14873#define BTREE_INCR_VACUUM         7
14874#define BTREE_APPLICATION_ID      8
14875#define BTREE_DATA_VERSION        15  /* A virtual meta-value */
14876
14877/*
14878** Kinds of hints that can be passed into the sqlite3BtreeCursorHint()
14879** interface.
14880**
14881** BTREE_HINT_RANGE  (arguments: Expr*, Mem*)
14882**
14883**     The first argument is an Expr* (which is guaranteed to be constant for
14884**     the lifetime of the cursor) that defines constraints on which rows
14885**     might be fetched with this cursor.  The Expr* tree may contain
14886**     TK_REGISTER nodes that refer to values stored in the array of registers
14887**     passed as the second parameter.  In other words, if Expr.op==TK_REGISTER
14888**     then the value of the node is the value in Mem[pExpr.iTable].  Any
14889**     TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
14890**     column of the b-tree of the cursor.  The Expr tree will not contain
14891**     any function calls nor subqueries nor references to b-trees other than
14892**     the cursor being hinted.
14893**
14894**     The design of the _RANGE hint is aid b-tree implementations that try
14895**     to prefetch content from remote machines - to provide those
14896**     implementations with limits on what needs to be prefetched and thereby
14897**     reduce network bandwidth.
14898**
14899** Note that BTREE_HINT_FLAGS with BTREE_BULKLOAD is the only hint used by
14900** standard SQLite.  The other hints are provided for extentions that use
14901** the SQLite parser and code generator but substitute their own storage
14902** engine.
14903*/
14904#define BTREE_HINT_RANGE 0       /* Range constraints on queries */
14905
14906/*
14907** Values that may be OR'd together to form the argument to the
14908** BTREE_HINT_FLAGS hint for sqlite3BtreeCursorHint():
14909**
14910** The BTREE_BULKLOAD flag is set on index cursors when the index is going
14911** to be filled with content that is already in sorted order.
14912**
14913** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or
14914** OP_SeekLE opcodes for a range search, but where the range of entries
14915** selected will all have the same key.  In other words, the cursor will
14916** be used only for equality key searches.
14917**
14918*/
14919#define BTREE_BULKLOAD 0x00000001  /* Used to full index in sorted order */
14920#define BTREE_SEEK_EQ  0x00000002  /* EQ seeks only - no range seeks */
14921
14922/*
14923** Flags passed as the third argument to sqlite3BtreeCursor().
14924**
14925** For read-only cursors the wrFlag argument is always zero. For read-write
14926** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just
14927** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will
14928** only be used by SQLite for the following:
14929**
14930**   * to seek to and then delete specific entries, and/or
14931**
14932**   * to read values that will be used to create keys that other
14933**     BTREE_FORDELETE cursors will seek to and delete.
14934**
14935** The BTREE_FORDELETE flag is an optimization hint.  It is not used by
14936** by this, the native b-tree engine of SQLite, but it is available to
14937** alternative storage engines that might be substituted in place of this
14938** b-tree system.  For alternative storage engines in which a delete of
14939** the main table row automatically deletes corresponding index rows,
14940** the FORDELETE flag hint allows those alternative storage engines to
14941** skip a lot of work.  Namely:  FORDELETE cursors may treat all SEEK
14942** and DELETE operations as no-ops, and any READ operation against a
14943** FORDELETE cursor may return a null row: 0x01 0x00.
14944*/
14945#define BTREE_WRCSR     0x00000004     /* read-write cursor */
14946#define BTREE_FORDELETE 0x00000008     /* Cursor is for seek/delete only */
14947
14948SQLITE_PRIVATE int sqlite3BtreeCursor(
14949  Btree*,                              /* BTree containing table to open */
14950  int iTable,                          /* Index of root page */
14951  int wrFlag,                          /* 1 for writing.  0 for read-only */
14952  struct KeyInfo*,                     /* First argument to compare function */
14953  BtCursor *pCursor                    /* Space to write cursor structure */
14954);
14955SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void);
14956SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
14957SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
14958SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned);
14959#ifdef SQLITE_ENABLE_CURSOR_HINTS
14960SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor*, int, ...);
14961#endif
14962
14963SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
14964SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
14965  BtCursor*,
14966  UnpackedRecord *pUnKey,
14967  i64 intKey,
14968  int bias,
14969  int *pRes
14970);
14971SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
14972SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
14973SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
14974
14975/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
14976#define BTREE_SAVEPOSITION 0x02  /* Leave cursor pointing at NEXT or PREV */
14977#define BTREE_AUXDELETE    0x04  /* not the primary delete operation */
14978#define BTREE_APPEND       0x08  /* Insert is likely an append */
14979
14980/* An instance of the BtreePayload object describes the content of a single
14981** entry in either an index or table btree.
14982**
14983** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
14984** an arbitrary key and no data.  These btrees have pKey,nKey set to the
14985** key and the pData,nData,nZero fields are uninitialized.  The aMem,nMem
14986** fields give an array of Mem objects that are a decomposition of the key.
14987** The nMem field might be zero, indicating that no decomposition is available.
14988**
14989** Table btrees (used for rowid tables) contain an integer rowid used as
14990** the key and passed in the nKey field.  The pKey field is zero.
14991** pData,nData hold the content of the new entry.  nZero extra zero bytes
14992** are appended to the end of the content when constructing the entry.
14993** The aMem,nMem fields are uninitialized for table btrees.
14994**
14995** Field usage summary:
14996**
14997**               Table BTrees                   Index Btrees
14998**
14999**   pKey        always NULL                    encoded key
15000**   nKey        the ROWID                      length of pKey
15001**   pData       data                           not used
15002**   aMem        not used                       decomposed key value
15003**   nMem        not used                       entries in aMem
15004**   nData       length of pData                not used
15005**   nZero       extra zeros after pData        not used
15006**
15007** This object is used to pass information into sqlite3BtreeInsert().  The
15008** same information used to be passed as five separate parameters.  But placing
15009** the information into this object helps to keep the interface more
15010** organized and understandable, and it also helps the resulting code to
15011** run a little faster by using fewer registers for parameter passing.
15012*/
15013struct BtreePayload {
15014  const void *pKey;       /* Key content for indexes.  NULL for tables */
15015  sqlite3_int64 nKey;     /* Size of pKey for indexes.  PRIMARY KEY for tabs */
15016  const void *pData;      /* Data for tables. */
15017  sqlite3_value *aMem;    /* First of nMem value in the unpacked pKey */
15018  u16 nMem;               /* Number of aMem[] value.  Might be zero */
15019  int nData;              /* Size of pData.  0 if none. */
15020  int nZero;              /* Extra zero data appended after pData,nData */
15021};
15022
15023SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
15024                       int flags, int seekResult);
15025SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
15026SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
15027SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int flags);
15028SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
15029SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int flags);
15030SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
15031SQLITE_PRIVATE void sqlite3BtreeCursorPin(BtCursor*);
15032SQLITE_PRIVATE void sqlite3BtreeCursorUnpin(BtCursor*);
15033#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
15034SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor*);
15035#endif
15036SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
15037SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
15038SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor*);
15039SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor*);
15040
15041SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(sqlite3*,Btree*,int*aRoot,int nRoot,int,int*);
15042SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
15043SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor*);
15044
15045#ifndef SQLITE_OMIT_INCRBLOB
15046SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*);
15047SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
15048SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
15049#endif
15050SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
15051SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
15052SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
15053SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
15054SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void);
15055
15056#ifndef NDEBUG
15057SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
15058#endif
15059SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor*);
15060
15061SQLITE_PRIVATE int sqlite3BtreeCount(sqlite3*, BtCursor*, i64*);
15062
15063#ifdef SQLITE_TEST
15064SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
15065SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
15066#endif
15067
15068#ifndef SQLITE_OMIT_WAL
15069SQLITE_PRIVATE   int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
15070#endif
15071
15072/*
15073** If we are not using shared cache, then there is no need to
15074** use mutexes to access the BtShared structures.  So make the
15075** Enter and Leave procedures no-ops.
15076*/
15077#ifndef SQLITE_OMIT_SHARED_CACHE
15078SQLITE_PRIVATE   void sqlite3BtreeEnter(Btree*);
15079SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
15080SQLITE_PRIVATE   int sqlite3BtreeSharable(Btree*);
15081SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
15082SQLITE_PRIVATE   int sqlite3BtreeConnectionCount(Btree*);
15083#else
15084# define sqlite3BtreeEnter(X)
15085# define sqlite3BtreeEnterAll(X)
15086# define sqlite3BtreeSharable(X) 0
15087# define sqlite3BtreeEnterCursor(X)
15088# define sqlite3BtreeConnectionCount(X) 1
15089#endif
15090
15091#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
15092SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
15093SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
15094SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
15095#ifndef NDEBUG
15096  /* These routines are used inside assert() statements only. */
15097SQLITE_PRIVATE   int sqlite3BtreeHoldsMutex(Btree*);
15098SQLITE_PRIVATE   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
15099SQLITE_PRIVATE   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
15100#endif
15101#else
15102
15103# define sqlite3BtreeLeave(X)
15104# define sqlite3BtreeLeaveCursor(X)
15105# define sqlite3BtreeLeaveAll(X)
15106
15107# define sqlite3BtreeHoldsMutex(X) 1
15108# define sqlite3BtreeHoldsAllMutexes(X) 1
15109# define sqlite3SchemaMutexHeld(X,Y,Z) 1
15110#endif
15111
15112
15113#endif /* SQLITE_BTREE_H */
15114
15115/************** End of btree.h ***********************************************/
15116/************** Continuing where we left off in sqliteInt.h ******************/
15117/************** Include vdbe.h in the middle of sqliteInt.h ******************/
15118/************** Begin file vdbe.h ********************************************/
15119/*
15120** 2001 September 15
15121**
15122** The author disclaims copyright to this source code.  In place of
15123** a legal notice, here is a blessing:
15124**
15125**    May you do good and not evil.
15126**    May you find forgiveness for yourself and forgive others.
15127**    May you share freely, never taking more than you give.
15128**
15129*************************************************************************
15130** Header file for the Virtual DataBase Engine (VDBE)
15131**
15132** This header defines the interface to the virtual database engine
15133** or VDBE.  The VDBE implements an abstract machine that runs a
15134** simple program to access and modify the underlying database.
15135*/
15136#ifndef SQLITE_VDBE_H
15137#define SQLITE_VDBE_H
15138/* #include <stdio.h> */
15139
15140/*
15141** A single VDBE is an opaque structure named "Vdbe".  Only routines
15142** in the source file sqliteVdbe.c are allowed to see the insides
15143** of this structure.
15144*/
15145typedef struct Vdbe Vdbe;
15146
15147/*
15148** The names of the following types declared in vdbeInt.h are required
15149** for the VdbeOp definition.
15150*/
15151typedef struct sqlite3_value Mem;
15152typedef struct SubProgram SubProgram;
15153
15154/*
15155** A single instruction of the virtual machine has an opcode
15156** and as many as three operands.  The instruction is recorded
15157** as an instance of the following structure:
15158*/
15159struct VdbeOp {
15160  u8 opcode;          /* What operation to perform */
15161  signed char p4type; /* One of the P4_xxx constants for p4 */
15162  u16 p5;             /* Fifth parameter is an unsigned 16-bit integer */
15163  int p1;             /* First operand */
15164  int p2;             /* Second parameter (often the jump destination) */
15165  int p3;             /* The third parameter */
15166  union p4union {     /* fourth parameter */
15167    int i;                 /* Integer value if p4type==P4_INT32 */
15168    void *p;               /* Generic pointer */
15169    char *z;               /* Pointer to data for string (char array) types */
15170    i64 *pI64;             /* Used when p4type is P4_INT64 */
15171    double *pReal;         /* Used when p4type is P4_REAL */
15172    FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
15173    sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
15174    CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
15175    Mem *pMem;             /* Used when p4type is P4_MEM */
15176    VTable *pVtab;         /* Used when p4type is P4_VTAB */
15177    KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
15178    int *ai;               /* Used when p4type is P4_INTARRAY */
15179    SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
15180    Table *pTab;           /* Used when p4type is P4_TABLE */
15181#ifdef SQLITE_ENABLE_CURSOR_HINTS
15182    Expr *pExpr;           /* Used when p4type is P4_EXPR */
15183#endif
15184    int (*xAdvance)(BtCursor *, int);
15185  } p4;
15186#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
15187  char *zComment;          /* Comment to improve readability */
15188#endif
15189#ifdef VDBE_PROFILE
15190  u32 cnt;                 /* Number of times this instruction was executed */
15191  u64 cycles;              /* Total time spent executing this instruction */
15192#endif
15193#ifdef SQLITE_VDBE_COVERAGE
15194  u32 iSrcLine;            /* Source-code line that generated this opcode
15195                           ** with flags in the upper 8 bits */
15196#endif
15197};
15198typedef struct VdbeOp VdbeOp;
15199
15200
15201/*
15202** A sub-routine used to implement a trigger program.
15203*/
15204struct SubProgram {
15205  VdbeOp *aOp;                  /* Array of opcodes for sub-program */
15206  int nOp;                      /* Elements in aOp[] */
15207  int nMem;                     /* Number of memory cells required */
15208  int nCsr;                     /* Number of cursors required */
15209  u8 *aOnce;                    /* Array of OP_Once flags */
15210  void *token;                  /* id that may be used to recursive triggers */
15211  SubProgram *pNext;            /* Next sub-program already visited */
15212};
15213
15214/*
15215** A smaller version of VdbeOp used for the VdbeAddOpList() function because
15216** it takes up less space.
15217*/
15218struct VdbeOpList {
15219  u8 opcode;          /* What operation to perform */
15220  signed char p1;     /* First operand */
15221  signed char p2;     /* Second parameter (often the jump destination) */
15222  signed char p3;     /* Third parameter */
15223};
15224typedef struct VdbeOpList VdbeOpList;
15225
15226/*
15227** Allowed values of VdbeOp.p4type
15228*/
15229#define P4_NOTUSED      0   /* The P4 parameter is not used */
15230#define P4_TRANSIENT    0   /* P4 is a pointer to a transient string */
15231#define P4_STATIC     (-1)  /* Pointer to a static string */
15232#define P4_COLLSEQ    (-2)  /* P4 is a pointer to a CollSeq structure */
15233#define P4_INT32      (-3)  /* P4 is a 32-bit signed integer */
15234#define P4_SUBPROGRAM (-4)  /* P4 is a pointer to a SubProgram structure */
15235#define P4_ADVANCE    (-5)  /* P4 is a pointer to BtreeNext() or BtreePrev() */
15236#define P4_TABLE      (-6)  /* P4 is a pointer to a Table structure */
15237/* Above do not own any resources.  Must free those below */
15238#define P4_FREE_IF_LE (-7)
15239#define P4_DYNAMIC    (-7)  /* Pointer to memory from sqliteMalloc() */
15240#define P4_FUNCDEF    (-8)  /* P4 is a pointer to a FuncDef structure */
15241#define P4_KEYINFO    (-9)  /* P4 is a pointer to a KeyInfo structure */
15242#define P4_EXPR       (-10) /* P4 is a pointer to an Expr tree */
15243#define P4_MEM        (-11) /* P4 is a pointer to a Mem*    structure */
15244#define P4_VTAB       (-12) /* P4 is a pointer to an sqlite3_vtab structure */
15245#define P4_REAL       (-13) /* P4 is a 64-bit floating point value */
15246#define P4_INT64      (-14) /* P4 is a 64-bit signed integer */
15247#define P4_INTARRAY   (-15) /* P4 is a vector of 32-bit integers */
15248#define P4_FUNCCTX    (-16) /* P4 is a pointer to an sqlite3_context object */
15249#define P4_DYNBLOB    (-17) /* Pointer to memory from sqliteMalloc() */
15250
15251/* Error message codes for OP_Halt */
15252#define P5_ConstraintNotNull 1
15253#define P5_ConstraintUnique  2
15254#define P5_ConstraintCheck   3
15255#define P5_ConstraintFK      4
15256
15257/*
15258** The Vdbe.aColName array contains 5n Mem structures, where n is the
15259** number of columns of data returned by the statement.
15260*/
15261#define COLNAME_NAME     0
15262#define COLNAME_DECLTYPE 1
15263#define COLNAME_DATABASE 2
15264#define COLNAME_TABLE    3
15265#define COLNAME_COLUMN   4
15266#ifdef SQLITE_ENABLE_COLUMN_METADATA
15267# define COLNAME_N        5      /* Number of COLNAME_xxx symbols */
15268#else
15269# ifdef SQLITE_OMIT_DECLTYPE
15270#   define COLNAME_N      1      /* Store only the name */
15271# else
15272#   define COLNAME_N      2      /* Store the name and decltype */
15273# endif
15274#endif
15275
15276/*
15277** The following macro converts a label returned by sqlite3VdbeMakeLabel()
15278** into an index into the Parse.aLabel[] array that contains the resolved
15279** address of that label.
15280*/
15281#define ADDR(X)  (~(X))
15282
15283/*
15284** The makefile scans the vdbe.c source file and creates the "opcodes.h"
15285** header file that defines a number for each opcode used by the VDBE.
15286*/
15287/************** Include opcodes.h in the middle of vdbe.h ********************/
15288/************** Begin file opcodes.h *****************************************/
15289/* Automatically generated.  Do not edit */
15290/* See the tool/mkopcodeh.tcl script for details */
15291#define OP_Savepoint       0
15292#define OP_AutoCommit      1
15293#define OP_Transaction     2
15294#define OP_SorterNext      3 /* jump                                       */
15295#define OP_Prev            4 /* jump                                       */
15296#define OP_Next            5 /* jump                                       */
15297#define OP_Checkpoint      6
15298#define OP_JournalMode     7
15299#define OP_Vacuum          8
15300#define OP_VFilter         9 /* jump, synopsis: iplan=r[P3] zplan='P4'     */
15301#define OP_VUpdate        10 /* synopsis: data=r[P3@P2]                    */
15302#define OP_Goto           11 /* jump                                       */
15303#define OP_Gosub          12 /* jump                                       */
15304#define OP_InitCoroutine  13 /* jump                                       */
15305#define OP_Yield          14 /* jump                                       */
15306#define OP_MustBeInt      15 /* jump                                       */
15307#define OP_Jump           16 /* jump                                       */
15308#define OP_Once           17 /* jump                                       */
15309#define OP_If             18 /* jump                                       */
15310#define OP_Not            19 /* same as TK_NOT, synopsis: r[P2]= !r[P1]    */
15311#define OP_IfNot          20 /* jump                                       */
15312#define OP_IfNullRow      21 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
15313#define OP_SeekLT         22 /* jump, synopsis: key=r[P3@P4]               */
15314#define OP_SeekLE         23 /* jump, synopsis: key=r[P3@P4]               */
15315#define OP_SeekGE         24 /* jump, synopsis: key=r[P3@P4]               */
15316#define OP_SeekGT         25 /* jump, synopsis: key=r[P3@P4]               */
15317#define OP_IfNotOpen      26 /* jump, synopsis: if( !csr[P1] ) goto P2     */
15318#define OP_IfNoHope       27 /* jump, synopsis: key=r[P3@P4]               */
15319#define OP_NoConflict     28 /* jump, synopsis: key=r[P3@P4]               */
15320#define OP_NotFound       29 /* jump, synopsis: key=r[P3@P4]               */
15321#define OP_Found          30 /* jump, synopsis: key=r[P3@P4]               */
15322#define OP_SeekRowid      31 /* jump, synopsis: intkey=r[P3]               */
15323#define OP_NotExists      32 /* jump, synopsis: intkey=r[P3]               */
15324#define OP_Last           33 /* jump                                       */
15325#define OP_IfSmaller      34 /* jump                                       */
15326#define OP_SorterSort     35 /* jump                                       */
15327#define OP_Sort           36 /* jump                                       */
15328#define OP_Rewind         37 /* jump                                       */
15329#define OP_IdxLE          38 /* jump, synopsis: key=r[P3@P4]               */
15330#define OP_IdxGT          39 /* jump, synopsis: key=r[P3@P4]               */
15331#define OP_IdxLT          40 /* jump, synopsis: key=r[P3@P4]               */
15332#define OP_IdxGE          41 /* jump, synopsis: key=r[P3@P4]               */
15333#define OP_RowSetRead     42 /* jump, synopsis: r[P3]=rowset(P1)           */
15334#define OP_Or             43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
15335#define OP_And            44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
15336#define OP_RowSetTest     45 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
15337#define OP_Program        46 /* jump                                       */
15338#define OP_FkIfZero       47 /* jump, synopsis: if fkctr[P1]==0 goto P2    */
15339#define OP_IfPos          48 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
15340#define OP_IfNotZero      49 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
15341#define OP_IsNull         50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
15342#define OP_NotNull        51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
15343#define OP_Ne             52 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */
15344#define OP_Eq             53 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */
15345#define OP_Gt             54 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */
15346#define OP_Le             55 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */
15347#define OP_Lt             56 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */
15348#define OP_Ge             57 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */
15349#define OP_ElseNotEq      58 /* jump, same as TK_ESCAPE                    */
15350#define OP_DecrJumpZero   59 /* jump, synopsis: if (--r[P1])==0 goto P2    */
15351#define OP_IncrVacuum     60 /* jump                                       */
15352#define OP_VNext          61 /* jump                                       */
15353#define OP_Init           62 /* jump, synopsis: Start at P2                */
15354#define OP_PureFunc       63 /* synopsis: r[P3]=func(r[P2@NP])             */
15355#define OP_Function       64 /* synopsis: r[P3]=func(r[P2@NP])             */
15356#define OP_Return         65
15357#define OP_EndCoroutine   66
15358#define OP_HaltIfNull     67 /* synopsis: if r[P3]=null halt               */
15359#define OP_Halt           68
15360#define OP_Integer        69 /* synopsis: r[P2]=P1                         */
15361#define OP_Int64          70 /* synopsis: r[P2]=P4                         */
15362#define OP_String         71 /* synopsis: r[P2]='P4' (len=P1)              */
15363#define OP_Null           72 /* synopsis: r[P2..P3]=NULL                   */
15364#define OP_SoftNull       73 /* synopsis: r[P1]=NULL                       */
15365#define OP_Blob           74 /* synopsis: r[P2]=P4 (len=P1)                */
15366#define OP_Variable       75 /* synopsis: r[P2]=parameter(P1,P4)           */
15367#define OP_Move           76 /* synopsis: r[P2@P3]=r[P1@P3]                */
15368#define OP_Copy           77 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
15369#define OP_SCopy          78 /* synopsis: r[P2]=r[P1]                      */
15370#define OP_IntCopy        79 /* synopsis: r[P2]=r[P1]                      */
15371#define OP_ResultRow      80 /* synopsis: output=r[P1@P2]                  */
15372#define OP_CollSeq        81
15373#define OP_AddImm         82 /* synopsis: r[P1]=r[P1]+P2                   */
15374#define OP_RealAffinity   83
15375#define OP_Cast           84 /* synopsis: affinity(r[P1])                  */
15376#define OP_Permutation    85
15377#define OP_Compare        86 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
15378#define OP_IsTrue         87 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
15379#define OP_Offset         88 /* synopsis: r[P3] = sqlite_offset(P1)        */
15380#define OP_Column         89 /* synopsis: r[P3]=PX                         */
15381#define OP_Affinity       90 /* synopsis: affinity(r[P1@P2])               */
15382#define OP_MakeRecord     91 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
15383#define OP_Count          92 /* synopsis: r[P2]=count()                    */
15384#define OP_ReadCookie     93
15385#define OP_SetCookie      94
15386#define OP_ReopenIdx      95 /* synopsis: root=P2 iDb=P3                   */
15387#define OP_OpenRead       96 /* synopsis: root=P2 iDb=P3                   */
15388#define OP_OpenWrite      97 /* synopsis: root=P2 iDb=P3                   */
15389#define OP_OpenDup        98
15390#define OP_OpenAutoindex  99 /* synopsis: nColumn=P2                       */
15391#define OP_OpenEphemeral 100 /* synopsis: nColumn=P2                       */
15392#define OP_BitAnd        101 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
15393#define OP_BitOr         102 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
15394#define OP_ShiftLeft     103 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
15395#define OP_ShiftRight    104 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
15396#define OP_Add           105 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
15397#define OP_Subtract      106 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
15398#define OP_Multiply      107 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
15399#define OP_Divide        108 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
15400#define OP_Remainder     109 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
15401#define OP_Concat        110 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
15402#define OP_SorterOpen    111
15403#define OP_BitNot        112 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
15404#define OP_SequenceTest  113 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
15405#define OP_OpenPseudo    114 /* synopsis: P3 columns in r[P2]              */
15406#define OP_String8       115 /* same as TK_STRING, synopsis: r[P2]='P4'    */
15407#define OP_Close         116
15408#define OP_ColumnsUsed   117
15409#define OP_SeekHit       118 /* synopsis: seekHit=P2                       */
15410#define OP_Sequence      119 /* synopsis: r[P2]=cursor[P1].ctr++           */
15411#define OP_NewRowid      120 /* synopsis: r[P2]=rowid                      */
15412#define OP_Insert        121 /* synopsis: intkey=r[P3] data=r[P2]          */
15413#define OP_Delete        122
15414#define OP_ResetCount    123
15415#define OP_SorterCompare 124 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
15416#define OP_SorterData    125 /* synopsis: r[P2]=data                       */
15417#define OP_RowData       126 /* synopsis: r[P2]=data                       */
15418#define OP_Rowid         127 /* synopsis: r[P2]=rowid                      */
15419#define OP_NullRow       128
15420#define OP_SeekEnd       129
15421#define OP_IdxInsert     130 /* synopsis: key=r[P2]                        */
15422#define OP_SorterInsert  131 /* synopsis: key=r[P2]                        */
15423#define OP_IdxDelete     132 /* synopsis: key=r[P2@P3]                     */
15424#define OP_DeferredSeek  133 /* synopsis: Move P3 to P1.rowid if needed    */
15425#define OP_IdxRowid      134 /* synopsis: r[P2]=rowid                      */
15426#define OP_FinishSeek    135
15427#define OP_Destroy       136
15428#define OP_Clear         137
15429#define OP_ResetSorter   138
15430#define OP_CreateBtree   139 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
15431#define OP_SqlExec       140
15432#define OP_ParseSchema   141
15433#define OP_LoadAnalysis  142
15434#define OP_DropTable     143
15435#define OP_DropIndex     144
15436#define OP_DropTrigger   145
15437#define OP_IntegrityCk   146
15438#define OP_RowSetAdd     147 /* synopsis: rowset(P1)=r[P2]                 */
15439#define OP_Param         148
15440#define OP_FkCounter     149 /* synopsis: fkctr[P1]+=P2                    */
15441#define OP_Real          150 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
15442#define OP_MemMax        151 /* synopsis: r[P1]=max(r[P1],r[P2])           */
15443#define OP_OffsetLimit   152 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
15444#define OP_AggInverse    153 /* synopsis: accum=r[P3] inverse(r[P2@P5])    */
15445#define OP_AggStep       154 /* synopsis: accum=r[P3] step(r[P2@P5])       */
15446#define OP_AggStep1      155 /* synopsis: accum=r[P3] step(r[P2@P5])       */
15447#define OP_AggValue      156 /* synopsis: r[P3]=value N=P2                 */
15448#define OP_AggFinal      157 /* synopsis: accum=r[P1] N=P2                 */
15449#define OP_Expire        158
15450#define OP_CursorLock    159
15451#define OP_CursorUnlock  160
15452#define OP_TableLock     161 /* synopsis: iDb=P1 root=P2 write=P3          */
15453#define OP_VBegin        162
15454#define OP_VCreate       163
15455#define OP_VDestroy      164
15456#define OP_VOpen         165
15457#define OP_VColumn       166 /* synopsis: r[P3]=vcolumn(P2)                */
15458#define OP_VRename       167
15459#define OP_Pagecount     168
15460#define OP_MaxPgcnt      169
15461#define OP_Trace         170
15462#define OP_CursorHint    171
15463#define OP_ReleaseReg    172 /* synopsis: release r[P1@P2] mask P3         */
15464#define OP_Noop          173
15465#define OP_Explain       174
15466#define OP_Abortable     175
15467
15468/* Properties such as "out2" or "jump" that are specified in
15469** comments following the "case" for each opcode in the vdbe.c
15470** are encoded into bitvectors as follows:
15471*/
15472#define OPFLG_JUMP        0x01  /* jump:  P2 holds jmp target */
15473#define OPFLG_IN1         0x02  /* in1:   P1 is an input */
15474#define OPFLG_IN2         0x04  /* in2:   P2 is an input */
15475#define OPFLG_IN3         0x08  /* in3:   P3 is an input */
15476#define OPFLG_OUT2        0x10  /* out2:  P2 is an output */
15477#define OPFLG_OUT3        0x20  /* out3:  P3 is an output */
15478#define OPFLG_INITIALIZER {\
15479/*   0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x10,\
15480/*   8 */ 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03,\
15481/*  16 */ 0x01, 0x01, 0x03, 0x12, 0x03, 0x01, 0x09, 0x09,\
15482/*  24 */ 0x09, 0x09, 0x01, 0x09, 0x09, 0x09, 0x09, 0x09,\
15483/*  32 */ 0x09, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
15484/*  40 */ 0x01, 0x01, 0x23, 0x26, 0x26, 0x0b, 0x01, 0x01,\
15485/*  48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
15486/*  56 */ 0x0b, 0x0b, 0x01, 0x03, 0x01, 0x01, 0x01, 0x00,\
15487/*  64 */ 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10, 0x10,\
15488/*  72 */ 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10, 0x10,\
15489/*  80 */ 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00, 0x12,\
15490/*  88 */ 0x20, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
15491/*  96 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x26, 0x26, 0x26,\
15492/* 104 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\
15493/* 112 */ 0x12, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10,\
15494/* 120 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
15495/* 128 */ 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10, 0x00,\
15496/* 136 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\
15497/* 144 */ 0x00, 0x00, 0x00, 0x06, 0x10, 0x00, 0x10, 0x04,\
15498/* 152 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
15499/* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
15500/* 168 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
15501}
15502
15503/* The sqlite3P2Values() routine is able to run faster if it knows
15504** the value of the largest JUMP opcode.  The smaller the maximum
15505** JUMP opcode the better, so the mkopcodeh.tcl script that
15506** generated this include file strives to group all JUMP opcodes
15507** together near the beginning of the list.
15508*/
15509#define SQLITE_MX_JUMP_OPCODE  62  /* Maximum JUMP opcode */
15510
15511/************** End of opcodes.h *********************************************/
15512/************** Continuing where we left off in vdbe.h ***********************/
15513
15514/*
15515** Additional non-public SQLITE_PREPARE_* flags
15516*/
15517#define SQLITE_PREPARE_SAVESQL  0x80  /* Preserve SQL text */
15518#define SQLITE_PREPARE_MASK     0x0f  /* Mask of public flags */
15519
15520/*
15521** Prototypes for the VDBE interface.  See comments on the implementation
15522** for a description of what each of these routines does.
15523*/
15524SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*);
15525SQLITE_PRIVATE Parse *sqlite3VdbeParser(Vdbe*);
15526SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
15527SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
15528SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
15529SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe*,int);
15530SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe*,int,const char*);
15531SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
15532SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
15533SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
15534SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
15535SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
15536SQLITE_PRIVATE int sqlite3VdbeAddFunctionCall(Parse*,int,int,int,int,const FuncDef*,int);
15537SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe*,int);
15538#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
15539SQLITE_PRIVATE   void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
15540SQLITE_PRIVATE   void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
15541#else
15542# define sqlite3VdbeVerifyNoMallocRequired(A,B)
15543# define sqlite3VdbeVerifyNoResultRow(A)
15544#endif
15545#if defined(SQLITE_DEBUG)
15546SQLITE_PRIVATE   void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
15547#else
15548# define sqlite3VdbeVerifyAbortable(A,B)
15549#endif
15550SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
15551#ifndef SQLITE_OMIT_EXPLAIN
15552SQLITE_PRIVATE   void sqlite3VdbeExplain(Parse*,u8,const char*,...);
15553SQLITE_PRIVATE   void sqlite3VdbeExplainPop(Parse*);
15554SQLITE_PRIVATE   int sqlite3VdbeExplainParent(Parse*);
15555# define ExplainQueryPlan(P)        sqlite3VdbeExplain P
15556# define ExplainQueryPlanPop(P)     sqlite3VdbeExplainPop(P)
15557# define ExplainQueryPlanParent(P)  sqlite3VdbeExplainParent(P)
15558#else
15559# define ExplainQueryPlan(P)
15560# define ExplainQueryPlanPop(P)
15561# define ExplainQueryPlanParent(P) 0
15562# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
15563#endif
15564#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
15565SQLITE_PRIVATE   void sqlite3ExplainBreakpoint(const char*,const char*);
15566#else
15567# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
15568#endif
15569SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
15570SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, int addr, u8);
15571SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
15572SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
15573SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
15574SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
15575SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
15576SQLITE_PRIVATE void sqlite3VdbeJumpHereOrPopInst(Vdbe*, int addr);
15577SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
15578SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
15579#ifdef SQLITE_DEBUG
15580SQLITE_PRIVATE   void sqlite3VdbeReleaseRegisters(Parse*,int addr, int n, u32 mask, int);
15581#else
15582# define sqlite3VdbeReleaseRegisters(P,A,N,M,F)
15583#endif
15584SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
15585SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
15586SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
15587SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
15588SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
15589SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse*);
15590SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
15591SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*);
15592SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
15593SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
15594SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
15595SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
15596SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
15597SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
15598#ifdef SQLITE_DEBUG
15599SQLITE_PRIVATE   int sqlite3VdbeAssertMayAbort(Vdbe *, int);
15600#endif
15601SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
15602SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
15603SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
15604SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
15605SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
15606SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
15607SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
15608SQLITE_PRIVATE u8 sqlite3VdbePrepareFlags(Vdbe*);
15609SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8);
15610#ifdef SQLITE_ENABLE_NORMALIZE
15611SQLITE_PRIVATE void sqlite3VdbeAddDblquoteStr(sqlite3*,Vdbe*,const char*);
15612SQLITE_PRIVATE int sqlite3VdbeUsesDoubleQuotedString(Vdbe*,const char*);
15613#endif
15614SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
15615SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
15616SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
15617SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
15618#ifndef SQLITE_OMIT_TRACE
15619SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
15620#endif
15621SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
15622SQLITE_PRIVATE int sqlite3BlobCompare(const Mem*, const Mem*);
15623
15624SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
15625SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
15626SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
15627SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo*);
15628
15629typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
15630SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
15631
15632SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
15633SQLITE_PRIVATE int sqlite3VdbeHasSubProgram(Vdbe*);
15634
15635SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context*);
15636#ifdef SQLITE_ENABLE_BYTECODE_VTAB
15637SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3*);
15638#endif
15639
15640/* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
15641** each VDBE opcode.
15642**
15643** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
15644** comments in VDBE programs that show key decision points in the code
15645** generator.
15646*/
15647#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
15648SQLITE_PRIVATE   void sqlite3VdbeComment(Vdbe*, const char*, ...);
15649# define VdbeComment(X)  sqlite3VdbeComment X
15650SQLITE_PRIVATE   void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
15651# define VdbeNoopComment(X)  sqlite3VdbeNoopComment X
15652# ifdef SQLITE_ENABLE_MODULE_COMMENTS
15653#   define VdbeModuleComment(X)  sqlite3VdbeNoopComment X
15654# else
15655#   define VdbeModuleComment(X)
15656# endif
15657#else
15658# define VdbeComment(X)
15659# define VdbeNoopComment(X)
15660# define VdbeModuleComment(X)
15661#endif
15662
15663/*
15664** The VdbeCoverage macros are used to set a coverage testing point
15665** for VDBE branch instructions.  The coverage testing points are line
15666** numbers in the sqlite3.c source file.  VDBE branch coverage testing
15667** only works with an amalagmation build.  That's ok since a VDBE branch
15668** coverage build designed for testing the test suite only.  No application
15669** should ever ship with VDBE branch coverage measuring turned on.
15670**
15671**    VdbeCoverage(v)                  // Mark the previously coded instruction
15672**                                     // as a branch
15673**
15674**    VdbeCoverageIf(v, conditional)   // Mark previous if conditional true
15675**
15676**    VdbeCoverageAlwaysTaken(v)       // Previous branch is always taken
15677**
15678**    VdbeCoverageNeverTaken(v)        // Previous branch is never taken
15679**
15680**    VdbeCoverageNeverNull(v)         // Previous three-way branch is only
15681**                                     // taken on the first two ways.  The
15682**                                     // NULL option is not possible
15683**
15684**    VdbeCoverageEqNe(v)              // Previous OP_Jump is only interested
15685**                                     // in distingishing equal and not-equal.
15686**
15687** Every VDBE branch operation must be tagged with one of the macros above.
15688** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
15689** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
15690** routine in vdbe.c, alerting the developer to the missed tag.
15691**
15692** During testing, the test application will invoke
15693** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback
15694** routine that is invoked as each bytecode branch is taken.  The callback
15695** contains the sqlite3.c source line number ov the VdbeCoverage macro and
15696** flags to indicate whether or not the branch was taken.  The test application
15697** is responsible for keeping track of this and reporting byte-code branches
15698** that are never taken.
15699**
15700** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the
15701** vdbe.c source file for additional information.
15702*/
15703#ifdef SQLITE_VDBE_COVERAGE
15704SQLITE_PRIVATE   void sqlite3VdbeSetLineNumber(Vdbe*,int);
15705# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
15706# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
15707# define VdbeCoverageAlwaysTaken(v) \
15708         sqlite3VdbeSetLineNumber(v,__LINE__|0x5000000);
15709# define VdbeCoverageNeverTaken(v) \
15710         sqlite3VdbeSetLineNumber(v,__LINE__|0x6000000);
15711# define VdbeCoverageNeverNull(v) \
15712         sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
15713# define VdbeCoverageNeverNullIf(v,x) \
15714         if(x)sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
15715# define VdbeCoverageEqNe(v) \
15716         sqlite3VdbeSetLineNumber(v,__LINE__|0x8000000);
15717# define VDBE_OFFSET_LINENO(x) (__LINE__+x)
15718#else
15719# define VdbeCoverage(v)
15720# define VdbeCoverageIf(v,x)
15721# define VdbeCoverageAlwaysTaken(v)
15722# define VdbeCoverageNeverTaken(v)
15723# define VdbeCoverageNeverNull(v)
15724# define VdbeCoverageNeverNullIf(v,x)
15725# define VdbeCoverageEqNe(v)
15726# define VDBE_OFFSET_LINENO(x) 0
15727#endif
15728
15729#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
15730SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
15731#else
15732# define sqlite3VdbeScanStatus(a,b,c,d,e)
15733#endif
15734
15735#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
15736SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, VdbeOp*);
15737#endif
15738
15739#endif /* SQLITE_VDBE_H */
15740
15741/************** End of vdbe.h ************************************************/
15742/************** Continuing where we left off in sqliteInt.h ******************/
15743/************** Include pager.h in the middle of sqliteInt.h *****************/
15744/************** Begin file pager.h *******************************************/
15745/*
15746** 2001 September 15
15747**
15748** The author disclaims copyright to this source code.  In place of
15749** a legal notice, here is a blessing:
15750**
15751**    May you do good and not evil.
15752**    May you find forgiveness for yourself and forgive others.
15753**    May you share freely, never taking more than you give.
15754**
15755*************************************************************************
15756** This header file defines the interface that the sqlite page cache
15757** subsystem.  The page cache subsystem reads and writes a file a page
15758** at a time and provides a journal for rollback.
15759*/
15760
15761#ifndef SQLITE_PAGER_H
15762#define SQLITE_PAGER_H
15763
15764/*
15765** Default maximum size for persistent journal files. A negative
15766** value means no limit. This value may be overridden using the
15767** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
15768*/
15769#ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
15770  #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
15771#endif
15772
15773/*
15774** The type used to represent a page number.  The first page in a file
15775** is called page 1.  0 is used to represent "not a page".
15776*/
15777typedef u32 Pgno;
15778
15779/*
15780** Each open file is managed by a separate instance of the "Pager" structure.
15781*/
15782typedef struct Pager Pager;
15783
15784/*
15785** Handle type for pages.
15786*/
15787typedef struct PgHdr DbPage;
15788
15789/*
15790** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
15791** reserved for working around a windows/posix incompatibility). It is
15792** used in the journal to signify that the remainder of the journal file
15793** is devoted to storing a master journal name - there are no more pages to
15794** roll back. See comments for function writeMasterJournal() in pager.c
15795** for details.
15796*/
15797#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
15798
15799/*
15800** Allowed values for the flags parameter to sqlite3PagerOpen().
15801**
15802** NOTE: These values must match the corresponding BTREE_ values in btree.h.
15803*/
15804#define PAGER_OMIT_JOURNAL  0x0001    /* Do not use a rollback journal */
15805#define PAGER_MEMORY        0x0002    /* In-memory database */
15806
15807/*
15808** Valid values for the second argument to sqlite3PagerLockingMode().
15809*/
15810#define PAGER_LOCKINGMODE_QUERY      -1
15811#define PAGER_LOCKINGMODE_NORMAL      0
15812#define PAGER_LOCKINGMODE_EXCLUSIVE   1
15813
15814/*
15815** Numeric constants that encode the journalmode.
15816**
15817** The numeric values encoded here (other than PAGER_JOURNALMODE_QUERY)
15818** are exposed in the API via the "PRAGMA journal_mode" command and
15819** therefore cannot be changed without a compatibility break.
15820*/
15821#define PAGER_JOURNALMODE_QUERY     (-1)  /* Query the value of journalmode */
15822#define PAGER_JOURNALMODE_DELETE      0   /* Commit by deleting journal file */
15823#define PAGER_JOURNALMODE_PERSIST     1   /* Commit by zeroing journal header */
15824#define PAGER_JOURNALMODE_OFF         2   /* Journal omitted.  */
15825#define PAGER_JOURNALMODE_TRUNCATE    3   /* Commit by truncating journal */
15826#define PAGER_JOURNALMODE_MEMORY      4   /* In-memory journal file */
15827#define PAGER_JOURNALMODE_WAL         5   /* Use write-ahead logging */
15828
15829/*
15830** Flags that make up the mask passed to sqlite3PagerGet().
15831*/
15832#define PAGER_GET_NOCONTENT     0x01  /* Do not load data from disk */
15833#define PAGER_GET_READONLY      0x02  /* Read-only page is acceptable */
15834
15835/*
15836** Flags for sqlite3PagerSetFlags()
15837**
15838** Value constraints (enforced via assert()):
15839**    PAGER_FULLFSYNC      == SQLITE_FullFSync
15840**    PAGER_CKPT_FULLFSYNC == SQLITE_CkptFullFSync
15841**    PAGER_CACHE_SPILL    == SQLITE_CacheSpill
15842*/
15843#define PAGER_SYNCHRONOUS_OFF       0x01  /* PRAGMA synchronous=OFF */
15844#define PAGER_SYNCHRONOUS_NORMAL    0x02  /* PRAGMA synchronous=NORMAL */
15845#define PAGER_SYNCHRONOUS_FULL      0x03  /* PRAGMA synchronous=FULL */
15846#define PAGER_SYNCHRONOUS_EXTRA     0x04  /* PRAGMA synchronous=EXTRA */
15847#define PAGER_SYNCHRONOUS_MASK      0x07  /* Mask for four values above */
15848#define PAGER_FULLFSYNC             0x08  /* PRAGMA fullfsync=ON */
15849#define PAGER_CKPT_FULLFSYNC        0x10  /* PRAGMA checkpoint_fullfsync=ON */
15850#define PAGER_CACHESPILL            0x20  /* PRAGMA cache_spill=ON */
15851#define PAGER_FLAGS_MASK            0x38  /* All above except SYNCHRONOUS */
15852
15853/*
15854** The remainder of this file contains the declarations of the functions
15855** that make up the Pager sub-system API. See source code comments for
15856** a detailed description of each routine.
15857*/
15858
15859/* Open and close a Pager connection. */
15860SQLITE_PRIVATE int sqlite3PagerOpen(
15861  sqlite3_vfs*,
15862  Pager **ppPager,
15863  const char*,
15864  int,
15865  int,
15866  int,
15867  void(*)(DbPage*)
15868);
15869SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*);
15870SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
15871
15872/* Functions used to configure a Pager object. */
15873SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
15874SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
15875SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
15876SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
15877SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager*, int);
15878SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
15879SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
15880SQLITE_PRIVATE void sqlite3PagerSetFlags(Pager*,unsigned);
15881SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
15882SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
15883SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
15884SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
15885SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
15886SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
15887SQLITE_PRIVATE int sqlite3PagerFlush(Pager*);
15888
15889/* Functions used to obtain and release page references. */
15890SQLITE_PRIVATE int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
15891SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
15892SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
15893SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
15894SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*);
15895SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage*);
15896
15897/* Operations on page references. */
15898SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
15899SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
15900SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
15901SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
15902SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
15903SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
15904
15905/* Functions used to manage pager transactions and savepoints. */
15906SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
15907SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
15908SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
15909SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
15910SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster);
15911SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
15912SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
15913SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
15914SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
15915SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
15916
15917#ifndef SQLITE_OMIT_WAL
15918SQLITE_PRIVATE   int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
15919SQLITE_PRIVATE   int sqlite3PagerWalSupported(Pager *pPager);
15920SQLITE_PRIVATE   int sqlite3PagerWalCallback(Pager *pPager);
15921SQLITE_PRIVATE   int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
15922SQLITE_PRIVATE   int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
15923# ifdef SQLITE_ENABLE_SNAPSHOT
15924SQLITE_PRIVATE   int sqlite3PagerSnapshotGet(Pager*, sqlite3_snapshot **ppSnapshot);
15925SQLITE_PRIVATE   int sqlite3PagerSnapshotOpen(Pager*, sqlite3_snapshot *pSnapshot);
15926SQLITE_PRIVATE   int sqlite3PagerSnapshotRecover(Pager *pPager);
15927SQLITE_PRIVATE   int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot);
15928SQLITE_PRIVATE   void sqlite3PagerSnapshotUnlock(Pager *pPager);
15929# endif
15930#endif
15931
15932#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_ENABLE_SETLK_TIMEOUT)
15933SQLITE_PRIVATE   int sqlite3PagerWalWriteLock(Pager*, int);
15934SQLITE_PRIVATE   void sqlite3PagerWalDb(Pager*, sqlite3*);
15935#else
15936# define sqlite3PagerWalWriteLock(y,z) SQLITE_OK
15937# define sqlite3PagerWalDb(x,y)
15938#endif
15939
15940#ifdef SQLITE_DIRECT_OVERFLOW_READ
15941SQLITE_PRIVATE   int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno);
15942#endif
15943
15944#ifdef SQLITE_ENABLE_ZIPVFS
15945SQLITE_PRIVATE   int sqlite3PagerWalFramesize(Pager *pPager);
15946#endif
15947
15948/* Functions used to query pager state and configuration. */
15949SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
15950SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager*);
15951#ifdef SQLITE_DEBUG
15952SQLITE_PRIVATE   int sqlite3PagerRefcount(Pager*);
15953#endif
15954SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
15955SQLITE_PRIVATE const char *sqlite3PagerFilename(const Pager*, int);
15956SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager*);
15957SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
15958SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
15959SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
15960SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
15961SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
15962SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
15963SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
15964SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
15965
15966/* Functions used to truncate the database file. */
15967SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
15968
15969SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16);
15970
15971/* Functions to support testing and debugging. */
15972#if !defined(NDEBUG) || defined(SQLITE_TEST)
15973SQLITE_PRIVATE   Pgno sqlite3PagerPagenumber(DbPage*);
15974SQLITE_PRIVATE   int sqlite3PagerIswriteable(DbPage*);
15975#endif
15976#ifdef SQLITE_TEST
15977SQLITE_PRIVATE   int *sqlite3PagerStats(Pager*);
15978SQLITE_PRIVATE   void sqlite3PagerRefdump(Pager*);
15979  void disable_simulated_io_errors(void);
15980  void enable_simulated_io_errors(void);
15981#else
15982# define disable_simulated_io_errors()
15983# define enable_simulated_io_errors()
15984#endif
15985
15986#endif /* SQLITE_PAGER_H */
15987
15988/************** End of pager.h ***********************************************/
15989/************** Continuing where we left off in sqliteInt.h ******************/
15990/************** Include pcache.h in the middle of sqliteInt.h ****************/
15991/************** Begin file pcache.h ******************************************/
15992/*
15993** 2008 August 05
15994**
15995** The author disclaims copyright to this source code.  In place of
15996** a legal notice, here is a blessing:
15997**
15998**    May you do good and not evil.
15999**    May you find forgiveness for yourself and forgive others.
16000**    May you share freely, never taking more than you give.
16001**
16002*************************************************************************
16003** This header file defines the interface that the sqlite page cache
16004** subsystem.
16005*/
16006
16007#ifndef _PCACHE_H_
16008
16009typedef struct PgHdr PgHdr;
16010typedef struct PCache PCache;
16011
16012/*
16013** Every page in the cache is controlled by an instance of the following
16014** structure.
16015*/
16016struct PgHdr {
16017  sqlite3_pcache_page *pPage;    /* Pcache object page handle */
16018  void *pData;                   /* Page data */
16019  void *pExtra;                  /* Extra content */
16020  PCache *pCache;                /* PRIVATE: Cache that owns this page */
16021  PgHdr *pDirty;                 /* Transient list of dirty sorted by pgno */
16022  Pager *pPager;                 /* The pager this page is part of */
16023  Pgno pgno;                     /* Page number for this page */
16024#ifdef SQLITE_CHECK_PAGES
16025  u32 pageHash;                  /* Hash of page content */
16026#endif
16027  u16 flags;                     /* PGHDR flags defined below */
16028
16029  /**********************************************************************
16030  ** Elements above, except pCache, are public.  All that follow are
16031  ** private to pcache.c and should not be accessed by other modules.
16032  ** pCache is grouped with the public elements for efficiency.
16033  */
16034  i16 nRef;                      /* Number of users of this page */
16035  PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
16036  PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
16037                          /* NB: pDirtyNext and pDirtyPrev are undefined if the
16038                          ** PgHdr object is not dirty */
16039};
16040
16041/* Bit values for PgHdr.flags */
16042#define PGHDR_CLEAN           0x001  /* Page not on the PCache.pDirty list */
16043#define PGHDR_DIRTY           0x002  /* Page is on the PCache.pDirty list */
16044#define PGHDR_WRITEABLE       0x004  /* Journaled and ready to modify */
16045#define PGHDR_NEED_SYNC       0x008  /* Fsync the rollback journal before
16046                                     ** writing this page to the database */
16047#define PGHDR_DONT_WRITE      0x010  /* Do not write content to disk */
16048#define PGHDR_MMAP            0x020  /* This is an mmap page object */
16049
16050#define PGHDR_WAL_APPEND      0x040  /* Appended to wal file */
16051
16052/* Initialize and shutdown the page cache subsystem */
16053SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
16054SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
16055
16056/* Page cache buffer management:
16057** These routines implement SQLITE_CONFIG_PAGECACHE.
16058*/
16059SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
16060
16061/* Create a new pager cache.
16062** Under memory stress, invoke xStress to try to make pages clean.
16063** Only clean and unpinned pages can be reclaimed.
16064*/
16065SQLITE_PRIVATE int sqlite3PcacheOpen(
16066  int szPage,                    /* Size of every page */
16067  int szExtra,                   /* Extra space associated with each page */
16068  int bPurgeable,                /* True if pages are on backing store */
16069  int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
16070  void *pStress,                 /* Argument to xStress */
16071  PCache *pToInit                /* Preallocated space for the PCache */
16072);
16073
16074/* Modify the page-size after the cache has been created. */
16075SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *, int);
16076
16077/* Return the size in bytes of a PCache object.  Used to preallocate
16078** storage space.
16079*/
16080SQLITE_PRIVATE int sqlite3PcacheSize(void);
16081
16082/* One release per successful fetch.  Page is pinned until released.
16083** Reference counted.
16084*/
16085SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
16086SQLITE_PRIVATE int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
16087SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
16088SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
16089
16090SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
16091SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
16092SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
16093SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
16094SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache*);
16095
16096/* Change a page number.  Used by incr-vacuum. */
16097SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
16098
16099/* Remove all pages with pgno>x.  Reset the cache if x==0 */
16100SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
16101
16102/* Get a list of all dirty pages in the cache, sorted by page number */
16103SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
16104
16105/* Reset and close the cache object */
16106SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
16107
16108/* Clear flags from pages of the page cache */
16109SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
16110
16111/* Discard the contents of the cache */
16112SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
16113
16114/* Return the total number of outstanding page references */
16115SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
16116
16117/* Increment the reference count of an existing page */
16118SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
16119
16120SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
16121
16122/* Return the total number of pages stored in the cache */
16123SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
16124
16125#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
16126/* Iterate through all dirty pages currently stored in the cache. This
16127** interface is only available if SQLITE_CHECK_PAGES is defined when the
16128** library is built.
16129*/
16130SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
16131#endif
16132
16133#if defined(SQLITE_DEBUG)
16134/* Check invariants on a PgHdr object */
16135SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr*);
16136#endif
16137
16138/* Set and get the suggested cache-size for the specified pager-cache.
16139**
16140** If no global maximum is configured, then the system attempts to limit
16141** the total number of pages cached by purgeable pager-caches to the sum
16142** of the suggested cache-sizes.
16143*/
16144SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
16145#ifdef SQLITE_TEST
16146SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
16147#endif
16148
16149/* Set or get the suggested spill-size for the specified pager-cache.
16150**
16151** The spill-size is the minimum number of pages in cache before the cache
16152** will attempt to spill dirty pages by calling xStress.
16153*/
16154SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *, int);
16155
16156/* Free up as much memory as possible from the page cache */
16157SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
16158
16159#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
16160/* Try to return memory used by the pcache module to the main memory heap */
16161SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
16162#endif
16163
16164#ifdef SQLITE_TEST
16165SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
16166#endif
16167
16168SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
16169
16170/* Return the header size */
16171SQLITE_PRIVATE int sqlite3HeaderSizePcache(void);
16172SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
16173
16174/* Number of dirty pages as a percentage of the configured cache size */
16175SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*);
16176
16177#ifdef SQLITE_DIRECT_OVERFLOW_READ
16178SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache);
16179#endif
16180
16181#endif /* _PCACHE_H_ */
16182
16183/************** End of pcache.h **********************************************/
16184/************** Continuing where we left off in sqliteInt.h ******************/
16185/************** Include os.h in the middle of sqliteInt.h ********************/
16186/************** Begin file os.h **********************************************/
16187/*
16188** 2001 September 16
16189**
16190** The author disclaims copyright to this source code.  In place of
16191** a legal notice, here is a blessing:
16192**
16193**    May you do good and not evil.
16194**    May you find forgiveness for yourself and forgive others.
16195**    May you share freely, never taking more than you give.
16196**
16197******************************************************************************
16198**
16199** This header file (together with is companion C source-code file
16200** "os.c") attempt to abstract the underlying operating system so that
16201** the SQLite library will work on both POSIX and windows systems.
16202**
16203** This header file is #include-ed by sqliteInt.h and thus ends up
16204** being included by every source file.
16205*/
16206#ifndef _SQLITE_OS_H_
16207#define _SQLITE_OS_H_
16208
16209/*
16210** Attempt to automatically detect the operating system and setup the
16211** necessary pre-processor macros for it.
16212*/
16213/************** Include os_setup.h in the middle of os.h *********************/
16214/************** Begin file os_setup.h ****************************************/
16215/*
16216** 2013 November 25
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 file contains pre-processor directives related to operating system
16228** detection and/or setup.
16229*/
16230#ifndef SQLITE_OS_SETUP_H
16231#define SQLITE_OS_SETUP_H
16232
16233/*
16234** Figure out if we are dealing with Unix, Windows, or some other operating
16235** system.
16236**
16237** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
16238** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0.  One of
16239** the three will be 1.  The other two will be 0.
16240*/
16241#if defined(SQLITE_OS_OTHER)
16242#  if SQLITE_OS_OTHER==1
16243#    undef SQLITE_OS_UNIX
16244#    define SQLITE_OS_UNIX 0
16245#    undef SQLITE_OS_WIN
16246#    define SQLITE_OS_WIN 0
16247#  else
16248#    undef SQLITE_OS_OTHER
16249#  endif
16250#endif
16251#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
16252#  define SQLITE_OS_OTHER 0
16253#  ifndef SQLITE_OS_WIN
16254#    if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
16255        defined(__MINGW32__) || defined(__BORLANDC__)
16256#      define SQLITE_OS_WIN 1
16257#      define SQLITE_OS_UNIX 0
16258#    else
16259#      define SQLITE_OS_WIN 0
16260#      define SQLITE_OS_UNIX 1
16261#    endif
16262#  else
16263#    define SQLITE_OS_UNIX 0
16264#  endif
16265#else
16266#  ifndef SQLITE_OS_WIN
16267#    define SQLITE_OS_WIN 0
16268#  endif
16269#endif
16270
16271#endif /* SQLITE_OS_SETUP_H */
16272
16273/************** End of os_setup.h ********************************************/
16274/************** Continuing where we left off in os.h *************************/
16275
16276/* If the SET_FULLSYNC macro is not defined above, then make it
16277** a no-op
16278*/
16279#ifndef SET_FULLSYNC
16280# define SET_FULLSYNC(x,y)
16281#endif
16282
16283/*
16284** The default size of a disk sector
16285*/
16286#ifndef SQLITE_DEFAULT_SECTOR_SIZE
16287# define SQLITE_DEFAULT_SECTOR_SIZE 4096
16288#endif
16289
16290/*
16291** Temporary files are named starting with this prefix followed by 16 random
16292** alphanumeric characters, and no file extension. They are stored in the
16293** OS's standard temporary file directory, and are deleted prior to exit.
16294** If sqlite is being embedded in another program, you may wish to change the
16295** prefix to reflect your program's name, so that if your program exits
16296** prematurely, old temporary files can be easily identified. This can be done
16297** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
16298**
16299** 2006-10-31:  The default prefix used to be "sqlite_".  But then
16300** Mcafee started using SQLite in their anti-virus product and it
16301** started putting files with the "sqlite" name in the c:/temp folder.
16302** This annoyed many windows users.  Those users would then do a
16303** Google search for "sqlite", find the telephone numbers of the
16304** developers and call to wake them up at night and complain.
16305** For this reason, the default name prefix is changed to be "sqlite"
16306** spelled backwards.  So the temp files are still identified, but
16307** anybody smart enough to figure out the code is also likely smart
16308** enough to know that calling the developer will not help get rid
16309** of the file.
16310*/
16311#ifndef SQLITE_TEMP_FILE_PREFIX
16312# define SQLITE_TEMP_FILE_PREFIX "etilqs_"
16313#endif
16314
16315/*
16316** The following values may be passed as the second argument to
16317** sqlite3OsLock(). The various locks exhibit the following semantics:
16318**
16319** SHARED:    Any number of processes may hold a SHARED lock simultaneously.
16320** RESERVED:  A single process may hold a RESERVED lock on a file at
16321**            any time. Other processes may hold and obtain new SHARED locks.
16322** PENDING:   A single process may hold a PENDING lock on a file at
16323**            any one time. Existing SHARED locks may persist, but no new
16324**            SHARED locks may be obtained by other processes.
16325** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
16326**
16327** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
16328** process that requests an EXCLUSIVE lock may actually obtain a PENDING
16329** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
16330** sqlite3OsLock().
16331*/
16332#define NO_LOCK         0
16333#define SHARED_LOCK     1
16334#define RESERVED_LOCK   2
16335#define PENDING_LOCK    3
16336#define EXCLUSIVE_LOCK  4
16337
16338/*
16339** File Locking Notes:  (Mostly about windows but also some info for Unix)
16340**
16341** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
16342** those functions are not available.  So we use only LockFile() and
16343** UnlockFile().
16344**
16345** LockFile() prevents not just writing but also reading by other processes.
16346** A SHARED_LOCK is obtained by locking a single randomly-chosen
16347** byte out of a specific range of bytes. The lock byte is obtained at
16348** random so two separate readers can probably access the file at the
16349** same time, unless they are unlucky and choose the same lock byte.
16350** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
16351** There can only be one writer.  A RESERVED_LOCK is obtained by locking
16352** a single byte of the file that is designated as the reserved lock byte.
16353** A PENDING_LOCK is obtained by locking a designated byte different from
16354** the RESERVED_LOCK byte.
16355**
16356** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
16357** which means we can use reader/writer locks.  When reader/writer locks
16358** are used, the lock is placed on the same range of bytes that is used
16359** for probabilistic locking in Win95/98/ME.  Hence, the locking scheme
16360** will support two or more Win95 readers or two or more WinNT readers.
16361** But a single Win95 reader will lock out all WinNT readers and a single
16362** WinNT reader will lock out all other Win95 readers.
16363**
16364** The following #defines specify the range of bytes used for locking.
16365** SHARED_SIZE is the number of bytes available in the pool from which
16366** a random byte is selected for a shared lock.  The pool of bytes for
16367** shared locks begins at SHARED_FIRST.
16368**
16369** The same locking strategy and
16370** byte ranges are used for Unix.  This leaves open the possibility of having
16371** clients on win95, winNT, and unix all talking to the same shared file
16372** and all locking correctly.  To do so would require that samba (or whatever
16373** tool is being used for file sharing) implements locks correctly between
16374** windows and unix.  I'm guessing that isn't likely to happen, but by
16375** using the same locking range we are at least open to the possibility.
16376**
16377** Locking in windows is manditory.  For this reason, we cannot store
16378** actual data in the bytes used for locking.  The pager never allocates
16379** the pages involved in locking therefore.  SHARED_SIZE is selected so
16380** that all locks will fit on a single page even at the minimum page size.
16381** PENDING_BYTE defines the beginning of the locks.  By default PENDING_BYTE
16382** is set high so that we don't have to allocate an unused page except
16383** for very large databases.  But one should test the page skipping logic
16384** by setting PENDING_BYTE low and running the entire regression suite.
16385**
16386** Changing the value of PENDING_BYTE results in a subtly incompatible
16387** file format.  Depending on how it is changed, you might not notice
16388** the incompatibility right away, even running a full regression test.
16389** The default location of PENDING_BYTE is the first byte past the
16390** 1GB boundary.
16391**
16392*/
16393#ifdef SQLITE_OMIT_WSD
16394# define PENDING_BYTE     (0x40000000)
16395#else
16396# define PENDING_BYTE      sqlite3PendingByte
16397#endif
16398#define RESERVED_BYTE     (PENDING_BYTE+1)
16399#define SHARED_FIRST      (PENDING_BYTE+2)
16400#define SHARED_SIZE       510
16401
16402/*
16403** Wrapper around OS specific sqlite3_os_init() function.
16404*/
16405SQLITE_PRIVATE int sqlite3OsInit(void);
16406
16407/*
16408** Functions for accessing sqlite3_file methods
16409*/
16410SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*);
16411SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
16412SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
16413SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
16414SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
16415SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
16416SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
16417SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
16418SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
16419SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
16420SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
16421#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
16422SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
16423SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
16424#ifndef SQLITE_OMIT_WAL
16425SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
16426SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
16427SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
16428SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
16429#endif /* SQLITE_OMIT_WAL */
16430SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
16431SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
16432
16433
16434/*
16435** Functions for accessing sqlite3_vfs methods
16436*/
16437SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
16438SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
16439SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
16440SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
16441#ifndef SQLITE_OMIT_LOAD_EXTENSION
16442SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
16443SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
16444SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
16445SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
16446#endif /* SQLITE_OMIT_LOAD_EXTENSION */
16447SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
16448SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
16449SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*);
16450SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
16451
16452/*
16453** Convenience functions for opening and closing files using
16454** sqlite3_malloc() to obtain space for the file-handle structure.
16455*/
16456SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
16457SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *);
16458
16459#endif /* _SQLITE_OS_H_ */
16460
16461/************** End of os.h **************************************************/
16462/************** Continuing where we left off in sqliteInt.h ******************/
16463/************** Include mutex.h in the middle of sqliteInt.h *****************/
16464/************** Begin file mutex.h *******************************************/
16465/*
16466** 2007 August 28
16467**
16468** The author disclaims copyright to this source code.  In place of
16469** a legal notice, here is a blessing:
16470**
16471**    May you do good and not evil.
16472**    May you find forgiveness for yourself and forgive others.
16473**    May you share freely, never taking more than you give.
16474**
16475*************************************************************************
16476**
16477** This file contains the common header for all mutex implementations.
16478** The sqliteInt.h header #includes this file so that it is available
16479** to all source files.  We break it out in an effort to keep the code
16480** better organized.
16481**
16482** NOTE:  source files should *not* #include this header file directly.
16483** Source files should #include the sqliteInt.h file and let that file
16484** include this one indirectly.
16485*/
16486
16487
16488/*
16489** Figure out what version of the code to use.  The choices are
16490**
16491**   SQLITE_MUTEX_OMIT         No mutex logic.  Not even stubs.  The
16492**                             mutexes implementation cannot be overridden
16493**                             at start-time.
16494**
16495**   SQLITE_MUTEX_NOOP         For single-threaded applications.  No
16496**                             mutual exclusion is provided.  But this
16497**                             implementation can be overridden at
16498**                             start-time.
16499**
16500**   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
16501**
16502**   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
16503*/
16504#if !SQLITE_THREADSAFE
16505# define SQLITE_MUTEX_OMIT
16506#endif
16507#if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
16508#  if SQLITE_OS_UNIX
16509#    define SQLITE_MUTEX_PTHREADS
16510#  elif SQLITE_OS_WIN
16511#    define SQLITE_MUTEX_W32
16512#  else
16513#    define SQLITE_MUTEX_NOOP
16514#  endif
16515#endif
16516
16517#ifdef SQLITE_MUTEX_OMIT
16518/*
16519** If this is a no-op implementation, implement everything as macros.
16520*/
16521#define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
16522#define sqlite3_mutex_free(X)
16523#define sqlite3_mutex_enter(X)
16524#define sqlite3_mutex_try(X)      SQLITE_OK
16525#define sqlite3_mutex_leave(X)
16526#define sqlite3_mutex_held(X)     ((void)(X),1)
16527#define sqlite3_mutex_notheld(X)  ((void)(X),1)
16528#define sqlite3MutexAlloc(X)      ((sqlite3_mutex*)8)
16529#define sqlite3MutexInit()        SQLITE_OK
16530#define sqlite3MutexEnd()
16531#define MUTEX_LOGIC(X)
16532#else
16533#define MUTEX_LOGIC(X)            X
16534SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
16535#endif /* defined(SQLITE_MUTEX_OMIT) */
16536
16537/************** End of mutex.h ***********************************************/
16538/************** Continuing where we left off in sqliteInt.h ******************/
16539
16540/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
16541** synchronous setting to EXTRA.  It is no longer supported.
16542*/
16543#ifdef SQLITE_EXTRA_DURABLE
16544# warning Use SQLITE_DEFAULT_SYNCHRONOUS=3 instead of SQLITE_EXTRA_DURABLE
16545# define SQLITE_DEFAULT_SYNCHRONOUS 3
16546#endif
16547
16548/*
16549** Default synchronous levels.
16550**
16551** Note that (for historcal reasons) the PAGER_SYNCHRONOUS_* macros differ
16552** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
16553**
16554**           PAGER_SYNCHRONOUS       DEFAULT_SYNCHRONOUS
16555**   OFF           1                         0
16556**   NORMAL        2                         1
16557**   FULL          3                         2
16558**   EXTRA         4                         3
16559**
16560** The "PRAGMA synchronous" statement also uses the zero-based numbers.
16561** In other words, the zero-based numbers are used for all external interfaces
16562** and the one-based values are used internally.
16563*/
16564#ifndef SQLITE_DEFAULT_SYNCHRONOUS
16565# define SQLITE_DEFAULT_SYNCHRONOUS 2
16566#endif
16567#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS
16568# define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS
16569#endif
16570
16571/*
16572** Each database file to be accessed by the system is an instance
16573** of the following structure.  There are normally two of these structures
16574** in the sqlite.aDb[] array.  aDb[0] is the main database file and
16575** aDb[1] is the database file used to hold temporary tables.  Additional
16576** databases may be attached.
16577*/
16578struct Db {
16579  char *zDbSName;      /* Name of this database. (schema name, not filename) */
16580  Btree *pBt;          /* The B*Tree structure for this database file */
16581  u8 safety_level;     /* How aggressive at syncing data to disk */
16582  u8 bSyncSet;         /* True if "PRAGMA synchronous=N" has been run */
16583  Schema *pSchema;     /* Pointer to database schema (possibly shared) */
16584};
16585
16586/*
16587** An instance of the following structure stores a database schema.
16588**
16589** Most Schema objects are associated with a Btree.  The exception is
16590** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
16591** In shared cache mode, a single Schema object can be shared by multiple
16592** Btrees that refer to the same underlying BtShared object.
16593**
16594** Schema objects are automatically deallocated when the last Btree that
16595** references them is destroyed.   The TEMP Schema is manually freed by
16596** sqlite3_close().
16597*
16598** A thread must be holding a mutex on the corresponding Btree in order
16599** to access Schema content.  This implies that the thread must also be
16600** holding a mutex on the sqlite3 connection pointer that owns the Btree.
16601** For a TEMP Schema, only the connection mutex is required.
16602*/
16603struct Schema {
16604  int schema_cookie;   /* Database schema version number for this file */
16605  int iGeneration;     /* Generation counter.  Incremented with each change */
16606  Hash tblHash;        /* All tables indexed by name */
16607  Hash idxHash;        /* All (named) indices indexed by name */
16608  Hash trigHash;       /* All triggers indexed by name */
16609  Hash fkeyHash;       /* All foreign keys by referenced table name */
16610  Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
16611  u8 file_format;      /* Schema format version for this file */
16612  u8 enc;              /* Text encoding used by this database */
16613  u16 schemaFlags;     /* Flags associated with this schema */
16614  int cache_size;      /* Number of pages to use in the cache */
16615};
16616
16617/*
16618** These macros can be used to test, set, or clear bits in the
16619** Db.pSchema->flags field.
16620*/
16621#define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
16622#define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
16623#define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->schemaFlags|=(P)
16624#define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->schemaFlags&=~(P)
16625
16626/*
16627** Allowed values for the DB.pSchema->flags field.
16628**
16629** The DB_SchemaLoaded flag is set after the database schema has been
16630** read into internal hash tables.
16631**
16632** DB_UnresetViews means that one or more views have column names that
16633** have been filled out.  If the schema changes, these column names might
16634** changes and so the view will need to be reset.
16635*/
16636#define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
16637#define DB_UnresetViews    0x0002  /* Some views have defined column names */
16638#define DB_ResetWanted     0x0008  /* Reset the schema when nSchemaLock==0 */
16639
16640/*
16641** The number of different kinds of things that can be limited
16642** using the sqlite3_limit() interface.
16643*/
16644#define SQLITE_N_LIMIT (SQLITE_LIMIT_WORKER_THREADS+1)
16645
16646/*
16647** Lookaside malloc is a set of fixed-size buffers that can be used
16648** to satisfy small transient memory allocation requests for objects
16649** associated with a particular database connection.  The use of
16650** lookaside malloc provides a significant performance enhancement
16651** (approx 10%) by avoiding numerous malloc/free requests while parsing
16652** SQL statements.
16653**
16654** The Lookaside structure holds configuration information about the
16655** lookaside malloc subsystem.  Each available memory allocation in
16656** the lookaside subsystem is stored on a linked list of LookasideSlot
16657** objects.
16658**
16659** Lookaside allocations are only allowed for objects that are associated
16660** with a particular database connection.  Hence, schema information cannot
16661** be stored in lookaside because in shared cache mode the schema information
16662** is shared by multiple database connections.  Therefore, while parsing
16663** schema information, the Lookaside.bEnabled flag is cleared so that
16664** lookaside allocations are not used to construct the schema objects.
16665**
16666** New lookaside allocations are only allowed if bDisable==0.  When
16667** bDisable is greater than zero, sz is set to zero which effectively
16668** disables lookaside without adding a new test for the bDisable flag
16669** in a performance-critical path.  sz should be set by to szTrue whenever
16670** bDisable changes back to zero.
16671**
16672** Lookaside buffers are initially held on the pInit list.  As they are
16673** used and freed, they are added back to the pFree list.  New allocations
16674** come off of pFree first, then pInit as a fallback.  This dual-list
16675** allows use to compute a high-water mark - the maximum number of allocations
16676** outstanding at any point in the past - by subtracting the number of
16677** allocations on the pInit list from the total number of allocations.
16678**
16679** Enhancement on 2019-12-12:  Two-size-lookaside
16680** The default lookaside configuration is 100 slots of 1200 bytes each.
16681** The larger slot sizes are important for performance, but they waste
16682** a lot of space, as most lookaside allocations are less than 128 bytes.
16683** The two-size-lookaside enhancement breaks up the lookaside allocation
16684** into two pools:  One of 128-byte slots and the other of the default size
16685** (1200-byte) slots.   Allocations are filled from the small-pool first,
16686** failing over to the full-size pool if that does not work.  Thus more
16687** lookaside slots are available while also using less memory.
16688** This enhancement can be omitted by compiling with
16689** SQLITE_OMIT_TWOSIZE_LOOKASIDE.
16690*/
16691struct Lookaside {
16692  u32 bDisable;           /* Only operate the lookaside when zero */
16693  u16 sz;                 /* Size of each buffer in bytes */
16694  u16 szTrue;             /* True value of sz, even if disabled */
16695  u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
16696  u32 nSlot;              /* Number of lookaside slots allocated */
16697  u32 anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
16698  LookasideSlot *pInit;   /* List of buffers not previously used */
16699  LookasideSlot *pFree;   /* List of available buffers */
16700#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
16701  LookasideSlot *pSmallInit; /* List of small buffers not prediously used */
16702  LookasideSlot *pSmallFree; /* List of available small buffers */
16703  void *pMiddle;          /* First byte past end of full-size buffers and
16704                          ** the first byte of LOOKASIDE_SMALL buffers */
16705#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
16706  void *pStart;           /* First byte of available memory space */
16707  void *pEnd;             /* First byte past end of available space */
16708};
16709struct LookasideSlot {
16710  LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
16711};
16712
16713#define DisableLookaside  db->lookaside.bDisable++;db->lookaside.sz=0
16714#define EnableLookaside   db->lookaside.bDisable--;\
16715   db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
16716
16717/* Size of the smaller allocations in two-size lookside */
16718#ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
16719#  define LOOKASIDE_SMALL           0
16720#else
16721#  define LOOKASIDE_SMALL         128
16722#endif
16723
16724/*
16725** A hash table for built-in function definitions.  (Application-defined
16726** functions use a regular table table from hash.h.)
16727**
16728** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
16729** Collisions are on the FuncDef.u.pHash chain.  Use the SQLITE_FUNC_HASH()
16730** macro to compute a hash on the function name.
16731*/
16732#define SQLITE_FUNC_HASH_SZ 23
16733struct FuncDefHash {
16734  FuncDef *a[SQLITE_FUNC_HASH_SZ];       /* Hash table for functions */
16735};
16736#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
16737
16738#ifdef SQLITE_USER_AUTHENTICATION
16739/*
16740** Information held in the "sqlite3" database connection object and used
16741** to manage user authentication.
16742*/
16743typedef struct sqlite3_userauth sqlite3_userauth;
16744struct sqlite3_userauth {
16745  u8 authLevel;                 /* Current authentication level */
16746  int nAuthPW;                  /* Size of the zAuthPW in bytes */
16747  char *zAuthPW;                /* Password used to authenticate */
16748  char *zAuthUser;              /* User name used to authenticate */
16749};
16750
16751/* Allowed values for sqlite3_userauth.authLevel */
16752#define UAUTH_Unknown     0     /* Authentication not yet checked */
16753#define UAUTH_Fail        1     /* User authentication failed */
16754#define UAUTH_User        2     /* Authenticated as a normal user */
16755#define UAUTH_Admin       3     /* Authenticated as an administrator */
16756
16757/* Functions used only by user authorization logic */
16758SQLITE_PRIVATE int sqlite3UserAuthTable(const char*);
16759SQLITE_PRIVATE int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*);
16760SQLITE_PRIVATE void sqlite3UserAuthInit(sqlite3*);
16761SQLITE_PRIVATE void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
16762
16763#endif /* SQLITE_USER_AUTHENTICATION */
16764
16765/*
16766** typedef for the authorization callback function.
16767*/
16768#ifdef SQLITE_USER_AUTHENTICATION
16769  typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
16770                               const char*, const char*);
16771#else
16772  typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
16773                               const char*);
16774#endif
16775
16776#ifndef SQLITE_OMIT_DEPRECATED
16777/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
16778** in the style of sqlite3_trace()
16779*/
16780#define SQLITE_TRACE_LEGACY          0x40     /* Use the legacy xTrace */
16781#define SQLITE_TRACE_XPROFILE        0x80     /* Use the legacy xProfile */
16782#else
16783#define SQLITE_TRACE_LEGACY          0
16784#define SQLITE_TRACE_XPROFILE        0
16785#endif /* SQLITE_OMIT_DEPRECATED */
16786#define SQLITE_TRACE_NONLEGACY_MASK  0x0f     /* Normal flags */
16787
16788
16789/*
16790** Each database connection is an instance of the following structure.
16791*/
16792struct sqlite3 {
16793  sqlite3_vfs *pVfs;            /* OS Interface */
16794  struct Vdbe *pVdbe;           /* List of active virtual machines */
16795  CollSeq *pDfltColl;           /* BINARY collseq for the database encoding */
16796  sqlite3_mutex *mutex;         /* Connection mutex */
16797  Db *aDb;                      /* All backends */
16798  int nDb;                      /* Number of backends currently in use */
16799  u32 mDbFlags;                 /* flags recording internal state */
16800  u64 flags;                    /* flags settable by pragmas. See below */
16801  i64 lastRowid;                /* ROWID of most recent insert (see above) */
16802  i64 szMmap;                   /* Default mmap_size setting */
16803  u32 nSchemaLock;              /* Do not reset the schema when non-zero */
16804  unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
16805  int errCode;                  /* Most recent error code (SQLITE_*) */
16806  int errMask;                  /* & result codes with this before returning */
16807  int iSysErrno;                /* Errno value from last system error */
16808  u16 dbOptFlags;               /* Flags to enable/disable optimizations */
16809  u8 enc;                       /* Text encoding */
16810  u8 autoCommit;                /* The auto-commit flag. */
16811  u8 temp_store;                /* 1: file 2: memory 0: default */
16812  u8 mallocFailed;              /* True if we have seen a malloc failure */
16813  u8 bBenignMalloc;             /* Do not require OOMs if true */
16814  u8 dfltLockMode;              /* Default locking-mode for attached dbs */
16815  signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
16816  u8 suppressErr;               /* Do not issue error messages if true */
16817  u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
16818  u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
16819  u8 mTrace;                    /* zero or more SQLITE_TRACE flags */
16820  u8 noSharedCache;             /* True if no shared-cache backends */
16821  u8 nSqlExec;                  /* Number of pending OP_SqlExec opcodes */
16822  int nextPagesize;             /* Pagesize after VACUUM if >0 */
16823  u32 magic;                    /* Magic number for detect library misuse */
16824  int nChange;                  /* Value returned by sqlite3_changes() */
16825  int nTotalChange;             /* Value returned by sqlite3_total_changes() */
16826  int aLimit[SQLITE_N_LIMIT];   /* Limits */
16827  int nMaxSorterMmap;           /* Maximum size of regions mapped by sorter */
16828  struct sqlite3InitInfo {      /* Information used during initialization */
16829    int newTnum;                /* Rootpage of table being initialized */
16830    u8 iDb;                     /* Which db file is being initialized */
16831    u8 busy;                    /* TRUE if currently initializing */
16832    unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
16833    unsigned imposterTable : 1; /* Building an imposter table */
16834    unsigned reopenMemdb : 1;   /* ATTACH is really a reopen using MemDB */
16835    char **azInit;              /* "type", "name", and "tbl_name" columns */
16836  } init;
16837  int nVdbeActive;              /* Number of VDBEs currently running */
16838  int nVdbeRead;                /* Number of active VDBEs that read or write */
16839  int nVdbeWrite;               /* Number of active VDBEs that read and write */
16840  int nVdbeExec;                /* Number of nested calls to VdbeExec() */
16841  int nVDestroy;                /* Number of active OP_VDestroy operations */
16842  int nExtension;               /* Number of loaded extensions */
16843  void **aExtension;            /* Array of shared library handles */
16844  int (*xTrace)(u32,void*,void*,void*);     /* Trace function */
16845  void *pTraceArg;                          /* Argument to the trace function */
16846#ifndef SQLITE_OMIT_DEPRECATED
16847  void (*xProfile)(void*,const char*,u64);  /* Profiling function */
16848  void *pProfileArg;                        /* Argument to profile function */
16849#endif
16850  void *pCommitArg;                 /* Argument to xCommitCallback() */
16851  int (*xCommitCallback)(void*);    /* Invoked at every commit. */
16852  void *pRollbackArg;               /* Argument to xRollbackCallback() */
16853  void (*xRollbackCallback)(void*); /* Invoked at every commit. */
16854  void *pUpdateArg;
16855  void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
16856  Parse *pParse;                /* Current parse */
16857#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
16858  void *pPreUpdateArg;          /* First argument to xPreUpdateCallback */
16859  void (*xPreUpdateCallback)(   /* Registered using sqlite3_preupdate_hook() */
16860    void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
16861  );
16862  PreUpdate *pPreUpdate;        /* Context for active pre-update callback */
16863#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
16864#ifndef SQLITE_OMIT_WAL
16865  int (*xWalCallback)(void *, sqlite3 *, const char *, int);
16866  void *pWalArg;
16867#endif
16868  void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
16869  void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
16870  void *pCollNeededArg;
16871  sqlite3_value *pErr;          /* Most recent error message */
16872  union {
16873    volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
16874    double notUsed1;            /* Spacer */
16875  } u1;
16876  Lookaside lookaside;          /* Lookaside malloc configuration */
16877#ifndef SQLITE_OMIT_AUTHORIZATION
16878  sqlite3_xauth xAuth;          /* Access authorization function */
16879  void *pAuthArg;               /* 1st argument to the access auth function */
16880#endif
16881#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
16882  int (*xProgress)(void *);     /* The progress callback */
16883  void *pProgressArg;           /* Argument to the progress callback */
16884  unsigned nProgressOps;        /* Number of opcodes for progress callback */
16885#endif
16886#ifndef SQLITE_OMIT_VIRTUALTABLE
16887  int nVTrans;                  /* Allocated size of aVTrans */
16888  Hash aModule;                 /* populated by sqlite3_create_module() */
16889  VtabCtx *pVtabCtx;            /* Context for active vtab connect/create */
16890  VTable **aVTrans;             /* Virtual tables with open transactions */
16891  VTable *pDisconnect;          /* Disconnect these in next sqlite3_prepare() */
16892#endif
16893  Hash aFunc;                   /* Hash table of connection functions */
16894  Hash aCollSeq;                /* All collating sequences */
16895  BusyHandler busyHandler;      /* Busy callback */
16896  Db aDbStatic[2];              /* Static space for the 2 default backends */
16897  Savepoint *pSavepoint;        /* List of active savepoints */
16898  int nAnalysisLimit;           /* Number of index rows to ANALYZE */
16899  int busyTimeout;              /* Busy handler timeout, in msec */
16900  int nSavepoint;               /* Number of non-transaction savepoints */
16901  int nStatement;               /* Number of nested statement-transactions  */
16902  i64 nDeferredCons;            /* Net deferred constraints this transaction. */
16903  i64 nDeferredImmCons;         /* Net deferred immediate constraints */
16904  int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */
16905#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
16906  /* The following variables are all protected by the STATIC_MASTER
16907  ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
16908  **
16909  ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
16910  ** unlock so that it can proceed.
16911  **
16912  ** When X.pBlockingConnection==Y, that means that something that X tried
16913  ** tried to do recently failed with an SQLITE_LOCKED error due to locks
16914  ** held by Y.
16915  */
16916  sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
16917  sqlite3 *pUnlockConnection;           /* Connection to watch for unlock */
16918  void *pUnlockArg;                     /* Argument to xUnlockNotify */
16919  void (*xUnlockNotify)(void **, int);  /* Unlock notify callback */
16920  sqlite3 *pNextBlocked;        /* Next in list of all blocked connections */
16921#endif
16922#ifdef SQLITE_USER_AUTHENTICATION
16923  sqlite3_userauth auth;        /* User authentication information */
16924#endif
16925};
16926
16927/*
16928** A macro to discover the encoding of a database.
16929*/
16930#define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
16931#define ENC(db)        ((db)->enc)
16932
16933/*
16934** A u64 constant where the lower 32 bits are all zeros.  Only the
16935** upper 32 bits are included in the argument.  Necessary because some
16936** C-compilers still do not accept LL integer literals.
16937*/
16938#define HI(X)  ((u64)(X)<<32)
16939
16940/*
16941** Possible values for the sqlite3.flags.
16942**
16943** Value constraints (enforced via assert()):
16944**      SQLITE_FullFSync     == PAGER_FULLFSYNC
16945**      SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
16946**      SQLITE_CacheSpill    == PAGER_CACHE_SPILL
16947*/
16948#define SQLITE_WriteSchema    0x00000001  /* OK to update SQLITE_MASTER */
16949#define SQLITE_LegacyFileFmt  0x00000002  /* Create new databases in format 1 */
16950#define SQLITE_FullColNames   0x00000004  /* Show full column names on SELECT */
16951#define SQLITE_FullFSync      0x00000008  /* Use full fsync on the backend */
16952#define SQLITE_CkptFullFSync  0x00000010  /* Use full fsync for checkpoint */
16953#define SQLITE_CacheSpill     0x00000020  /* OK to spill pager cache */
16954#define SQLITE_ShortColNames  0x00000040  /* Show short columns names */
16955#define SQLITE_TrustedSchema  0x00000080  /* Allow unsafe functions and
16956                                          ** vtabs in the schema definition */
16957#define SQLITE_NullCallback   0x00000100  /* Invoke the callback once if the */
16958                                          /*   result set is empty */
16959#define SQLITE_IgnoreChecks   0x00000200  /* Do not enforce check constraints */
16960#define SQLITE_ReadUncommit   0x00000400  /* READ UNCOMMITTED in shared-cache */
16961#define SQLITE_NoCkptOnClose  0x00000800  /* No checkpoint on close()/DETACH */
16962#define SQLITE_ReverseOrder   0x00001000  /* Reverse unordered SELECTs */
16963#define SQLITE_RecTriggers    0x00002000  /* Enable recursive triggers */
16964#define SQLITE_ForeignKeys    0x00004000  /* Enforce foreign key constraints  */
16965#define SQLITE_AutoIndex      0x00008000  /* Enable automatic indexes */
16966#define SQLITE_LoadExtension  0x00010000  /* Enable load_extension */
16967#define SQLITE_LoadExtFunc    0x00020000  /* Enable load_extension() SQL func */
16968#define SQLITE_EnableTrigger  0x00040000  /* True to enable triggers */
16969#define SQLITE_DeferFKs       0x00080000  /* Defer all FK constraints */
16970#define SQLITE_QueryOnly      0x00100000  /* Disable database changes */
16971#define SQLITE_CellSizeCk     0x00200000  /* Check btree cell sizes on load */
16972#define SQLITE_Fts3Tokenizer  0x00400000  /* Enable fts3_tokenizer(2) */
16973#define SQLITE_EnableQPSG     0x00800000  /* Query Planner Stability Guarantee*/
16974#define SQLITE_TriggerEQP     0x01000000  /* Show trigger EXPLAIN QUERY PLAN */
16975#define SQLITE_ResetDatabase  0x02000000  /* Reset the database */
16976#define SQLITE_LegacyAlter    0x04000000  /* Legacy ALTER TABLE behaviour */
16977#define SQLITE_NoSchemaError  0x08000000  /* Do not report schema parse errors*/
16978#define SQLITE_Defensive      0x10000000  /* Input SQL is likely hostile */
16979#define SQLITE_DqsDDL         0x20000000  /* dbl-quoted strings allowed in DDL*/
16980#define SQLITE_DqsDML         0x40000000  /* dbl-quoted strings allowed in DML*/
16981#define SQLITE_EnableView     0x80000000  /* Enable the use of views */
16982#define SQLITE_CountRows      HI(0x00001) /* Count rows changed by INSERT, */
16983                                          /*   DELETE, or UPDATE and return */
16984                                          /*   the count using a callback. */
16985
16986/* Flags used only if debugging */
16987#ifdef SQLITE_DEBUG
16988#define SQLITE_SqlTrace       HI(0x0100000) /* Debug print SQL as it executes */
16989#define SQLITE_VdbeListing    HI(0x0200000) /* Debug listings of VDBE progs */
16990#define SQLITE_VdbeTrace      HI(0x0400000) /* True to trace VDBE execution */
16991#define SQLITE_VdbeAddopTrace HI(0x0800000) /* Trace sqlite3VdbeAddOp() calls */
16992#define SQLITE_VdbeEQP        HI(0x1000000) /* Debug EXPLAIN QUERY PLAN */
16993#define SQLITE_ParserTrace    HI(0x2000000) /* PRAGMA parser_trace=ON */
16994#endif
16995
16996/*
16997** Allowed values for sqlite3.mDbFlags
16998*/
16999#define DBFLAG_SchemaChange   0x0001  /* Uncommitted Hash table changes */
17000#define DBFLAG_PreferBuiltin  0x0002  /* Preference to built-in funcs */
17001#define DBFLAG_Vacuum         0x0004  /* Currently in a VACUUM */
17002#define DBFLAG_VacuumInto     0x0008  /* Currently running VACUUM INTO */
17003#define DBFLAG_SchemaKnownOk  0x0010  /* Schema is known to be valid */
17004#define DBFLAG_InternalFunc   0x0020  /* Allow use of internal functions */
17005#define DBFLAG_EncodingFixed  0x0040  /* No longer possible to change enc. */
17006
17007/*
17008** Bits of the sqlite3.dbOptFlags field that are used by the
17009** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
17010** selectively disable various optimizations.
17011*/
17012#define SQLITE_QueryFlattener 0x0001   /* Query flattening */
17013#define SQLITE_WindowFunc     0x0002   /* Use xInverse for window functions */
17014#define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
17015#define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
17016#define SQLITE_DistinctOpt    0x0010   /* DISTINCT using indexes */
17017#define SQLITE_CoverIdxScan   0x0020   /* Covering index scans */
17018#define SQLITE_OrderByIdxJoin 0x0040   /* ORDER BY of joins via index */
17019#define SQLITE_Transitive     0x0080   /* Transitive constraints */
17020#define SQLITE_OmitNoopJoin   0x0100   /* Omit unused tables in joins */
17021#define SQLITE_CountOfView    0x0200   /* The count-of-view optimization */
17022#define SQLITE_CursorHints    0x0400   /* Add OP_CursorHint opcodes */
17023#define SQLITE_Stat4          0x0800   /* Use STAT4 data */
17024   /* TH3 expects the Stat4   ^^^^^^ value to be 0x0800.  Don't change it */
17025#define SQLITE_PushDown       0x1000   /* The push-down optimization */
17026#define SQLITE_SimplifyJoin   0x2000   /* Convert LEFT JOIN to JOIN */
17027#define SQLITE_SkipScan       0x4000   /* Skip-scans */
17028#define SQLITE_PropagateConst 0x8000   /* The constant propagation opt */
17029#define SQLITE_AllOpts        0xffff   /* All optimizations */
17030
17031/*
17032** Macros for testing whether or not optimizations are enabled or disabled.
17033*/
17034#define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
17035#define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
17036
17037/*
17038** Return true if it OK to factor constant expressions into the initialization
17039** code. The argument is a Parse object for the code generator.
17040*/
17041#define ConstFactorOk(P) ((P)->okConstFactor)
17042
17043/*
17044** Possible values for the sqlite.magic field.
17045** The numbers are obtained at random and have no special meaning, other
17046** than being distinct from one another.
17047*/
17048#define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
17049#define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
17050#define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
17051#define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
17052#define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
17053#define SQLITE_MAGIC_ZOMBIE   0x64cffc7f  /* Close with last statement close */
17054
17055/*
17056** Each SQL function is defined by an instance of the following
17057** structure.  For global built-in functions (ex: substr(), max(), count())
17058** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
17059** For per-connection application-defined functions, a pointer to this
17060** structure is held in the db->aHash hash table.
17061**
17062** The u.pHash field is used by the global built-ins.  The u.pDestructor
17063** field is used by per-connection app-def functions.
17064*/
17065struct FuncDef {
17066  i8 nArg;             /* Number of arguments.  -1 means unlimited */
17067  u32 funcFlags;       /* Some combination of SQLITE_FUNC_* */
17068  void *pUserData;     /* User data parameter */
17069  FuncDef *pNext;      /* Next function with same name */
17070  void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
17071  void (*xFinalize)(sqlite3_context*);                  /* Agg finalizer */
17072  void (*xValue)(sqlite3_context*);                     /* Current agg value */
17073  void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
17074  const char *zName;   /* SQL name of the function. */
17075  union {
17076    FuncDef *pHash;      /* Next with a different name but the same hash */
17077    FuncDestructor *pDestructor;   /* Reference counted destructor function */
17078  } u;
17079};
17080
17081/*
17082** This structure encapsulates a user-function destructor callback (as
17083** configured using create_function_v2()) and a reference counter. When
17084** create_function_v2() is called to create a function with a destructor,
17085** a single object of this type is allocated. FuncDestructor.nRef is set to
17086** the number of FuncDef objects created (either 1 or 3, depending on whether
17087** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
17088** member of each of the new FuncDef objects is set to point to the allocated
17089** FuncDestructor.
17090**
17091** Thereafter, when one of the FuncDef objects is deleted, the reference
17092** count on this object is decremented. When it reaches 0, the destructor
17093** is invoked and the FuncDestructor structure freed.
17094*/
17095struct FuncDestructor {
17096  int nRef;
17097  void (*xDestroy)(void *);
17098  void *pUserData;
17099};
17100
17101/*
17102** Possible values for FuncDef.flags.  Note that the _LENGTH and _TYPEOF
17103** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG.  And
17104** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC.  There
17105** are assert() statements in the code to verify this.
17106**
17107** Value constraints (enforced via assert()):
17108**     SQLITE_FUNC_MINMAX    ==  NC_MinMaxAgg      == SF_MinMaxAgg
17109**     SQLITE_FUNC_LENGTH    ==  OPFLAG_LENGTHARG
17110**     SQLITE_FUNC_TYPEOF    ==  OPFLAG_TYPEOFARG
17111**     SQLITE_FUNC_CONSTANT  ==  SQLITE_DETERMINISTIC from the API
17112**     SQLITE_FUNC_DIRECT    ==  SQLITE_DIRECTONLY from the API
17113**     SQLITE_FUNC_UNSAFE    ==  SQLITE_INNOCUOUS
17114**     SQLITE_FUNC_ENCMASK   depends on SQLITE_UTF* macros in the API
17115*/
17116#define SQLITE_FUNC_ENCMASK  0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
17117#define SQLITE_FUNC_LIKE     0x0004 /* Candidate for the LIKE optimization */
17118#define SQLITE_FUNC_CASE     0x0008 /* Case-sensitive LIKE-type function */
17119#define SQLITE_FUNC_EPHEM    0x0010 /* Ephemeral.  Delete with VDBE */
17120#define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
17121#define SQLITE_FUNC_LENGTH   0x0040 /* Built-in length() function */
17122#define SQLITE_FUNC_TYPEOF   0x0080 /* Built-in typeof() function */
17123#define SQLITE_FUNC_COUNT    0x0100 /* Built-in count(*) aggregate */
17124/*                           0x0200 -- available for reuse */
17125#define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
17126#define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
17127#define SQLITE_FUNC_MINMAX   0x1000 /* True for min() and max() aggregates */
17128#define SQLITE_FUNC_SLOCHNG  0x2000 /* "Slow Change". Value constant during a
17129                                    ** single query - might change over time */
17130#define SQLITE_FUNC_TEST     0x4000 /* Built-in testing functions */
17131#define SQLITE_FUNC_OFFSET   0x8000 /* Built-in sqlite_offset() function */
17132#define SQLITE_FUNC_WINDOW   0x00010000 /* Built-in window-only function */
17133#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
17134#define SQLITE_FUNC_DIRECT   0x00080000 /* Not for use in TRIGGERs or VIEWs */
17135#define SQLITE_FUNC_SUBTYPE  0x00100000 /* Result likely to have sub-type */
17136#define SQLITE_FUNC_UNSAFE   0x00200000 /* Function has side effects */
17137#define SQLITE_FUNC_INLINE   0x00400000 /* Functions implemented in-line */
17138
17139/* Identifier numbers for each in-line function */
17140#define INLINEFUNC_coalesce             0
17141#define INLINEFUNC_implies_nonnull_row  1
17142#define INLINEFUNC_expr_implies_expr    2
17143#define INLINEFUNC_expr_compare         3
17144#define INLINEFUNC_affinity             4
17145#define INLINEFUNC_iif                  5
17146#define INLINEFUNC_unlikely            99  /* Default case */
17147
17148/*
17149** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
17150** used to create the initializers for the FuncDef structures.
17151**
17152**   FUNCTION(zName, nArg, iArg, bNC, xFunc)
17153**     Used to create a scalar function definition of a function zName
17154**     implemented by C function xFunc that accepts nArg arguments. The
17155**     value passed as iArg is cast to a (void*) and made available
17156**     as the user-data (sqlite3_user_data()) for the function. If
17157**     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
17158**
17159**   VFUNCTION(zName, nArg, iArg, bNC, xFunc)
17160**     Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
17161**
17162**   SFUNCTION(zName, nArg, iArg, bNC, xFunc)
17163**     Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
17164**     adds the SQLITE_DIRECTONLY flag.
17165**
17166**   INLINE_FUNC(zName, nArg, iFuncId, mFlags)
17167**     zName is the name of a function that is implemented by in-line
17168**     byte code rather than by the usual callbacks. The iFuncId
17169**     parameter determines the function id.  The mFlags parameter is
17170**     optional SQLITE_FUNC_ flags for this function.
17171**
17172**   TEST_FUNC(zName, nArg, iFuncId, mFlags)
17173**     zName is the name of a test-only function implemented by in-line
17174**     byte code rather than by the usual callbacks. The iFuncId
17175**     parameter determines the function id.  The mFlags parameter is
17176**     optional SQLITE_FUNC_ flags for this function.
17177**
17178**   DFUNCTION(zName, nArg, iArg, bNC, xFunc)
17179**     Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
17180**     adds the SQLITE_FUNC_SLOCHNG flag.  Used for date & time functions
17181**     and functions like sqlite_version() that can change, but not during
17182**     a single query.  The iArg is ignored.  The user-data is always set
17183**     to a NULL pointer.  The bNC parameter is not used.
17184**
17185**   PURE_DATE(zName, nArg, iArg, bNC, xFunc)
17186**     Used for "pure" date/time functions, this macro is like DFUNCTION
17187**     except that it does set the SQLITE_FUNC_CONSTANT flags.  iArg is
17188**     ignored and the user-data for these functions is set to an
17189**     arbitrary non-NULL pointer.  The bNC parameter is not used.
17190**
17191**   AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
17192**     Used to create an aggregate function definition implemented by
17193**     the C functions xStep and xFinal. The first four parameters
17194**     are interpreted in the same way as the first 4 parameters to
17195**     FUNCTION().
17196**
17197**   WFUNCTION(zName, nArg, iArg, xStep, xFinal, xValue, xInverse)
17198**     Used to create an aggregate function definition implemented by
17199**     the C functions xStep and xFinal. The first four parameters
17200**     are interpreted in the same way as the first 4 parameters to
17201**     FUNCTION().
17202**
17203**   LIKEFUNC(zName, nArg, pArg, flags)
17204**     Used to create a scalar function definition of a function zName
17205**     that accepts nArg arguments and is implemented by a call to C
17206**     function likeFunc. Argument pArg is cast to a (void *) and made
17207**     available as the function user-data (sqlite3_user_data()). The
17208**     FuncDef.flags variable is set to the value passed as the flags
17209**     parameter.
17210*/
17211#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
17212  {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
17213   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17214#define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
17215  {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
17216   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17217#define SFUNCTION(zName, nArg, iArg, bNC, xFunc) \
17218  {nArg, SQLITE_UTF8|SQLITE_DIRECTONLY|SQLITE_FUNC_UNSAFE, \
17219   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17220#define INLINE_FUNC(zName, nArg, iArg, mFlags) \
17221  {nArg, SQLITE_UTF8|SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
17222   SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
17223#define TEST_FUNC(zName, nArg, iArg, mFlags) \
17224  {nArg, SQLITE_UTF8|SQLITE_FUNC_INTERNAL|SQLITE_FUNC_TEST| \
17225         SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
17226   SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
17227#define DFUNCTION(zName, nArg, iArg, bNC, xFunc) \
17228  {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8, \
17229   0, 0, xFunc, 0, 0, 0, #zName, {0} }
17230#define PURE_DATE(zName, nArg, iArg, bNC, xFunc) \
17231  {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
17232   (void*)&sqlite3Config, 0, xFunc, 0, 0, 0, #zName, {0} }
17233#define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
17234  {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
17235   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17236#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
17237  {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
17238   pArg, 0, xFunc, 0, 0, 0, #zName, }
17239#define LIKEFUNC(zName, nArg, arg, flags) \
17240  {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
17241   (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
17242#define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f) \
17243  {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|f, \
17244   SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,xInverse,#zName, {0}}
17245#define INTERNAL_FUNCTION(zName, nArg, xFunc) \
17246  {nArg, SQLITE_FUNC_INTERNAL|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
17247   0, 0, xFunc, 0, 0, 0, #zName, {0} }
17248
17249
17250/*
17251** All current savepoints are stored in a linked list starting at
17252** sqlite3.pSavepoint. The first element in the list is the most recently
17253** opened savepoint. Savepoints are added to the list by the vdbe
17254** OP_Savepoint instruction.
17255*/
17256struct Savepoint {
17257  char *zName;                        /* Savepoint name (nul-terminated) */
17258  i64 nDeferredCons;                  /* Number of deferred fk violations */
17259  i64 nDeferredImmCons;               /* Number of deferred imm fk. */
17260  Savepoint *pNext;                   /* Parent savepoint (if any) */
17261};
17262
17263/*
17264** The following are used as the second parameter to sqlite3Savepoint(),
17265** and as the P1 argument to the OP_Savepoint instruction.
17266*/
17267#define SAVEPOINT_BEGIN      0
17268#define SAVEPOINT_RELEASE    1
17269#define SAVEPOINT_ROLLBACK   2
17270
17271
17272/*
17273** Each SQLite module (virtual table definition) is defined by an
17274** instance of the following structure, stored in the sqlite3.aModule
17275** hash table.
17276*/
17277struct Module {
17278  const sqlite3_module *pModule;       /* Callback pointers */
17279  const char *zName;                   /* Name passed to create_module() */
17280  int nRefModule;                      /* Number of pointers to this object */
17281  void *pAux;                          /* pAux passed to create_module() */
17282  void (*xDestroy)(void *);            /* Module destructor function */
17283  Table *pEpoTab;                      /* Eponymous table for this module */
17284};
17285
17286/*
17287** Information about each column of an SQL table is held in an instance
17288** of the Column structure, in the Table.aCol[] array.
17289**
17290** Definitions:
17291**
17292**   "table column index"     This is the index of the column in the
17293**                            Table.aCol[] array, and also the index of
17294**                            the column in the original CREATE TABLE stmt.
17295**
17296**   "storage column index"   This is the index of the column in the
17297**                            record BLOB generated by the OP_MakeRecord
17298**                            opcode.  The storage column index is less than
17299**                            or equal to the table column index.  It is
17300**                            equal if and only if there are no VIRTUAL
17301**                            columns to the left.
17302*/
17303struct Column {
17304  char *zName;     /* Name of this column, \000, then the type */
17305  Expr *pDflt;     /* Default value or GENERATED ALWAYS AS value */
17306  char *zColl;     /* Collating sequence.  If NULL, use the default */
17307  u8 notNull;      /* An OE_ code for handling a NOT NULL constraint */
17308  char affinity;   /* One of the SQLITE_AFF_... values */
17309  u8 szEst;        /* Estimated size of value in this column. sizeof(INT)==1 */
17310  u8 hName;        /* Column name hash for faster lookup */
17311  u16 colFlags;    /* Boolean properties.  See COLFLAG_ defines below */
17312};
17313
17314/* Allowed values for Column.colFlags:
17315*/
17316#define COLFLAG_PRIMKEY   0x0001   /* Column is part of the primary key */
17317#define COLFLAG_HIDDEN    0x0002   /* A hidden column in a virtual table */
17318#define COLFLAG_HASTYPE   0x0004   /* Type name follows column name */
17319#define COLFLAG_UNIQUE    0x0008   /* Column def contains "UNIQUE" or "PK" */
17320#define COLFLAG_SORTERREF 0x0010   /* Use sorter-refs with this column */
17321#define COLFLAG_VIRTUAL   0x0020   /* GENERATED ALWAYS AS ... VIRTUAL */
17322#define COLFLAG_STORED    0x0040   /* GENERATED ALWAYS AS ... STORED */
17323#define COLFLAG_NOTAVAIL  0x0080   /* STORED column not yet calculated */
17324#define COLFLAG_BUSY      0x0100   /* Blocks recursion on GENERATED columns */
17325#define COLFLAG_GENERATED 0x0060   /* Combo: _STORED, _VIRTUAL */
17326#define COLFLAG_NOINSERT  0x0062   /* Combo: _HIDDEN, _STORED, _VIRTUAL */
17327
17328/*
17329** A "Collating Sequence" is defined by an instance of the following
17330** structure. Conceptually, a collating sequence consists of a name and
17331** a comparison routine that defines the order of that sequence.
17332**
17333** If CollSeq.xCmp is NULL, it means that the
17334** collating sequence is undefined.  Indices built on an undefined
17335** collating sequence may not be read or written.
17336*/
17337struct CollSeq {
17338  char *zName;          /* Name of the collating sequence, UTF-8 encoded */
17339  u8 enc;               /* Text encoding handled by xCmp() */
17340  void *pUser;          /* First argument to xCmp() */
17341  int (*xCmp)(void*,int, const void*, int, const void*);
17342  void (*xDel)(void*);  /* Destructor for pUser */
17343};
17344
17345/*
17346** A sort order can be either ASC or DESC.
17347*/
17348#define SQLITE_SO_ASC       0  /* Sort in ascending order */
17349#define SQLITE_SO_DESC      1  /* Sort in ascending order */
17350#define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
17351
17352/*
17353** Column affinity types.
17354**
17355** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
17356** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
17357** the speed a little by numbering the values consecutively.
17358**
17359** But rather than start with 0 or 1, we begin with 'A'.  That way,
17360** when multiple affinity types are concatenated into a string and
17361** used as the P4 operand, they will be more readable.
17362**
17363** Note also that the numeric types are grouped together so that testing
17364** for a numeric type is a single comparison.  And the BLOB type is first.
17365*/
17366#define SQLITE_AFF_NONE     0x40  /* '@' */
17367#define SQLITE_AFF_BLOB     0x41  /* 'A' */
17368#define SQLITE_AFF_TEXT     0x42  /* 'B' */
17369#define SQLITE_AFF_NUMERIC  0x43  /* 'C' */
17370#define SQLITE_AFF_INTEGER  0x44  /* 'D' */
17371#define SQLITE_AFF_REAL     0x45  /* 'E' */
17372
17373#define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
17374
17375/*
17376** The SQLITE_AFF_MASK values masks off the significant bits of an
17377** affinity value.
17378*/
17379#define SQLITE_AFF_MASK     0x47
17380
17381/*
17382** Additional bit values that can be ORed with an affinity without
17383** changing the affinity.
17384**
17385** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
17386** It causes an assert() to fire if either operand to a comparison
17387** operator is NULL.  It is added to certain comparison operators to
17388** prove that the operands are always NOT NULL.
17389*/
17390#define SQLITE_KEEPNULL     0x08  /* Used by vector == or <> */
17391#define SQLITE_JUMPIFNULL   0x10  /* jumps if either operand is NULL */
17392#define SQLITE_STOREP2      0x20  /* Store result in reg[P2] rather than jump */
17393#define SQLITE_NULLEQ       0x80  /* NULL=NULL */
17394#define SQLITE_NOTNULL      0x90  /* Assert that operands are never NULL */
17395
17396/*
17397** An object of this type is created for each virtual table present in
17398** the database schema.
17399**
17400** If the database schema is shared, then there is one instance of this
17401** structure for each database connection (sqlite3*) that uses the shared
17402** schema. This is because each database connection requires its own unique
17403** instance of the sqlite3_vtab* handle used to access the virtual table
17404** implementation. sqlite3_vtab* handles can not be shared between
17405** database connections, even when the rest of the in-memory database
17406** schema is shared, as the implementation often stores the database
17407** connection handle passed to it via the xConnect() or xCreate() method
17408** during initialization internally. This database connection handle may
17409** then be used by the virtual table implementation to access real tables
17410** within the database. So that they appear as part of the callers
17411** transaction, these accesses need to be made via the same database
17412** connection as that used to execute SQL operations on the virtual table.
17413**
17414** All VTable objects that correspond to a single table in a shared
17415** database schema are initially stored in a linked-list pointed to by
17416** the Table.pVTable member variable of the corresponding Table object.
17417** When an sqlite3_prepare() operation is required to access the virtual
17418** table, it searches the list for the VTable that corresponds to the
17419** database connection doing the preparing so as to use the correct
17420** sqlite3_vtab* handle in the compiled query.
17421**
17422** When an in-memory Table object is deleted (for example when the
17423** schema is being reloaded for some reason), the VTable objects are not
17424** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
17425** immediately. Instead, they are moved from the Table.pVTable list to
17426** another linked list headed by the sqlite3.pDisconnect member of the
17427** corresponding sqlite3 structure. They are then deleted/xDisconnected
17428** next time a statement is prepared using said sqlite3*. This is done
17429** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
17430** Refer to comments above function sqlite3VtabUnlockList() for an
17431** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
17432** list without holding the corresponding sqlite3.mutex mutex.
17433**
17434** The memory for objects of this type is always allocated by
17435** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
17436** the first argument.
17437*/
17438struct VTable {
17439  sqlite3 *db;              /* Database connection associated with this table */
17440  Module *pMod;             /* Pointer to module implementation */
17441  sqlite3_vtab *pVtab;      /* Pointer to vtab instance */
17442  int nRef;                 /* Number of pointers to this structure */
17443  u8 bConstraint;           /* True if constraints are supported */
17444  u8 eVtabRisk;             /* Riskiness of allowing hacker access */
17445  int iSavepoint;           /* Depth of the SAVEPOINT stack */
17446  VTable *pNext;            /* Next in linked list (see above) */
17447};
17448
17449/* Allowed values for VTable.eVtabRisk
17450*/
17451#define SQLITE_VTABRISK_Low          0
17452#define SQLITE_VTABRISK_Normal       1
17453#define SQLITE_VTABRISK_High         2
17454
17455/*
17456** The schema for each SQL table and view is represented in memory
17457** by an instance of the following structure.
17458*/
17459struct Table {
17460  char *zName;         /* Name of the table or view */
17461  Column *aCol;        /* Information about each column */
17462  Index *pIndex;       /* List of SQL indexes on this table. */
17463  Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
17464  FKey *pFKey;         /* Linked list of all foreign keys in this table */
17465  char *zColAff;       /* String defining the affinity of each column */
17466  ExprList *pCheck;    /* All CHECK constraints */
17467                       /*   ... also used as column name list in a VIEW */
17468  int tnum;            /* Root BTree page for this table */
17469  u32 nTabRef;         /* Number of pointers to this Table */
17470  u32 tabFlags;        /* Mask of TF_* values */
17471  i16 iPKey;           /* If not negative, use aCol[iPKey] as the rowid */
17472  i16 nCol;            /* Number of columns in this table */
17473  i16 nNVCol;          /* Number of columns that are not VIRTUAL */
17474  LogEst nRowLogEst;   /* Estimated rows in table - from sqlite_stat1 table */
17475  LogEst szTabRow;     /* Estimated size of each table row in bytes */
17476#ifdef SQLITE_ENABLE_COSTMULT
17477  LogEst costMult;     /* Cost multiplier for using this table */
17478#endif
17479  u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
17480#ifndef SQLITE_OMIT_ALTERTABLE
17481  int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
17482#endif
17483#ifndef SQLITE_OMIT_VIRTUALTABLE
17484  int nModuleArg;      /* Number of arguments to the module */
17485  char **azModuleArg;  /* 0: module 1: schema 2: vtab name 3...: args */
17486  VTable *pVTable;     /* List of VTable objects. */
17487#endif
17488  Trigger *pTrigger;   /* List of triggers stored in pSchema */
17489  Schema *pSchema;     /* Schema that contains this table */
17490  Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
17491};
17492
17493/*
17494** Allowed values for Table.tabFlags.
17495**
17496** TF_OOOHidden applies to tables or view that have hidden columns that are
17497** followed by non-hidden columns.  Example:  "CREATE VIRTUAL TABLE x USING
17498** vtab1(a HIDDEN, b);".  Since "b" is a non-hidden column but "a" is hidden,
17499** the TF_OOOHidden attribute would apply in this case.  Such tables require
17500** special handling during INSERT processing. The "OOO" means "Out Of Order".
17501**
17502** Constraints:
17503**
17504**         TF_HasVirtual == COLFLAG_Virtual
17505**         TF_HasStored  == COLFLAG_Stored
17506*/
17507#define TF_Readonly        0x0001    /* Read-only system table */
17508#define TF_Ephemeral       0x0002    /* An ephemeral table */
17509#define TF_HasPrimaryKey   0x0004    /* Table has a primary key */
17510#define TF_Autoincrement   0x0008    /* Integer primary key is autoincrement */
17511#define TF_HasStat1        0x0010    /* nRowLogEst set from sqlite_stat1 */
17512#define TF_HasVirtual      0x0020    /* Has one or more VIRTUAL columns */
17513#define TF_HasStored       0x0040    /* Has one or more STORED columns */
17514#define TF_HasGenerated    0x0060    /* Combo: HasVirtual + HasStored */
17515#define TF_WithoutRowid    0x0080    /* No rowid.  PRIMARY KEY is the key */
17516#define TF_StatsUsed       0x0100    /* Query planner decisions affected by
17517                                     ** Index.aiRowLogEst[] values */
17518#define TF_NoVisibleRowid  0x0200    /* No user-visible "rowid" column */
17519#define TF_OOOHidden       0x0400    /* Out-of-Order hidden columns */
17520#define TF_HasNotNull      0x0800    /* Contains NOT NULL constraints */
17521#define TF_Shadow          0x1000    /* True for a shadow table */
17522
17523/*
17524** Test to see whether or not a table is a virtual table.  This is
17525** done as a macro so that it will be optimized out when virtual
17526** table support is omitted from the build.
17527*/
17528#ifndef SQLITE_OMIT_VIRTUALTABLE
17529#  define IsVirtual(X)      ((X)->nModuleArg)
17530#  define ExprIsVtab(X)  \
17531              ((X)->op==TK_COLUMN && (X)->y.pTab!=0 && (X)->y.pTab->nModuleArg)
17532#else
17533#  define IsVirtual(X)      0
17534#  define ExprIsVtab(X)     0
17535#endif
17536
17537/*
17538** Macros to determine if a column is hidden.  IsOrdinaryHiddenColumn()
17539** only works for non-virtual tables (ordinary tables and views) and is
17540** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined.  The
17541** IsHiddenColumn() macro is general purpose.
17542*/
17543#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS)
17544#  define IsHiddenColumn(X)         (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17545#  define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17546#elif !defined(SQLITE_OMIT_VIRTUALTABLE)
17547#  define IsHiddenColumn(X)         (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17548#  define IsOrdinaryHiddenColumn(X) 0
17549#else
17550#  define IsHiddenColumn(X)         0
17551#  define IsOrdinaryHiddenColumn(X) 0
17552#endif
17553
17554
17555/* Does the table have a rowid */
17556#define HasRowid(X)     (((X)->tabFlags & TF_WithoutRowid)==0)
17557#define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
17558
17559/*
17560** Each foreign key constraint is an instance of the following structure.
17561**
17562** A foreign key is associated with two tables.  The "from" table is
17563** the table that contains the REFERENCES clause that creates the foreign
17564** key.  The "to" table is the table that is named in the REFERENCES clause.
17565** Consider this example:
17566**
17567**     CREATE TABLE ex1(
17568**       a INTEGER PRIMARY KEY,
17569**       b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
17570**     );
17571**
17572** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
17573** Equivalent names:
17574**
17575**     from-table == child-table
17576**       to-table == parent-table
17577**
17578** Each REFERENCES clause generates an instance of the following structure
17579** which is attached to the from-table.  The to-table need not exist when
17580** the from-table is created.  The existence of the to-table is not checked.
17581**
17582** The list of all parents for child Table X is held at X.pFKey.
17583**
17584** A list of all children for a table named Z (which might not even exist)
17585** is held in Schema.fkeyHash with a hash key of Z.
17586*/
17587struct FKey {
17588  Table *pFrom;     /* Table containing the REFERENCES clause (aka: Child) */
17589  FKey *pNextFrom;  /* Next FKey with the same in pFrom. Next parent of pFrom */
17590  char *zTo;        /* Name of table that the key points to (aka: Parent) */
17591  FKey *pNextTo;    /* Next with the same zTo. Next child of zTo. */
17592  FKey *pPrevTo;    /* Previous with the same zTo */
17593  int nCol;         /* Number of columns in this key */
17594  /* EV: R-30323-21917 */
17595  u8 isDeferred;       /* True if constraint checking is deferred till COMMIT */
17596  u8 aAction[2];        /* ON DELETE and ON UPDATE actions, respectively */
17597  Trigger *apTrigger[2];/* Triggers for aAction[] actions */
17598  struct sColMap {      /* Mapping of columns in pFrom to columns in zTo */
17599    int iFrom;            /* Index of column in pFrom */
17600    char *zCol;           /* Name of column in zTo.  If NULL use PRIMARY KEY */
17601  } aCol[1];            /* One entry for each of nCol columns */
17602};
17603
17604/*
17605** SQLite supports many different ways to resolve a constraint
17606** error.  ROLLBACK processing means that a constraint violation
17607** causes the operation in process to fail and for the current transaction
17608** to be rolled back.  ABORT processing means the operation in process
17609** fails and any prior changes from that one operation are backed out,
17610** but the transaction is not rolled back.  FAIL processing means that
17611** the operation in progress stops and returns an error code.  But prior
17612** changes due to the same operation are not backed out and no rollback
17613** occurs.  IGNORE means that the particular row that caused the constraint
17614** error is not inserted or updated.  Processing continues and no error
17615** is returned.  REPLACE means that preexisting database rows that caused
17616** a UNIQUE constraint violation are removed so that the new insert or
17617** update can proceed.  Processing continues and no error is reported.
17618**
17619** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
17620** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
17621** same as ROLLBACK for DEFERRED keys.  SETNULL means that the foreign
17622** key is set to NULL.  CASCADE means that a DELETE or UPDATE of the
17623** referenced table row is propagated into the row that holds the
17624** foreign key.
17625**
17626** The following symbolic values are used to record which type
17627** of action to take.
17628*/
17629#define OE_None     0   /* There is no constraint to check */
17630#define OE_Rollback 1   /* Fail the operation and rollback the transaction */
17631#define OE_Abort    2   /* Back out changes but do no rollback transaction */
17632#define OE_Fail     3   /* Stop the operation but leave all prior changes */
17633#define OE_Ignore   4   /* Ignore the error. Do not do the INSERT or UPDATE */
17634#define OE_Replace  5   /* Delete existing record, then do INSERT or UPDATE */
17635#define OE_Update   6   /* Process as a DO UPDATE in an upsert */
17636#define OE_Restrict 7   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
17637#define OE_SetNull  8   /* Set the foreign key value to NULL */
17638#define OE_SetDflt  9   /* Set the foreign key value to its default */
17639#define OE_Cascade  10  /* Cascade the changes */
17640#define OE_Default  11  /* Do whatever the default action is */
17641
17642
17643/*
17644** An instance of the following structure is passed as the first
17645** argument to sqlite3VdbeKeyCompare and is used to control the
17646** comparison of the two index keys.
17647**
17648** Note that aSortOrder[] and aColl[] have nField+1 slots.  There
17649** are nField slots for the columns of an index then one extra slot
17650** for the rowid at the end.
17651*/
17652struct KeyInfo {
17653  u32 nRef;           /* Number of references to this KeyInfo object */
17654  u8 enc;             /* Text encoding - one of the SQLITE_UTF* values */
17655  u16 nKeyField;      /* Number of key columns in the index */
17656  u16 nAllField;      /* Total columns, including key plus others */
17657  sqlite3 *db;        /* The database connection */
17658  u8 *aSortFlags;     /* Sort order for each column. */
17659  CollSeq *aColl[1];  /* Collating sequence for each term of the key */
17660};
17661
17662/*
17663** Allowed bit values for entries in the KeyInfo.aSortFlags[] array.
17664*/
17665#define KEYINFO_ORDER_DESC    0x01    /* DESC sort order */
17666#define KEYINFO_ORDER_BIGNULL 0x02    /* NULL is larger than any other value */
17667
17668/*
17669** This object holds a record which has been parsed out into individual
17670** fields, for the purposes of doing a comparison.
17671**
17672** A record is an object that contains one or more fields of data.
17673** Records are used to store the content of a table row and to store
17674** the key of an index.  A blob encoding of a record is created by
17675** the OP_MakeRecord opcode of the VDBE and is disassembled by the
17676** OP_Column opcode.
17677**
17678** An instance of this object serves as a "key" for doing a search on
17679** an index b+tree. The goal of the search is to find the entry that
17680** is closed to the key described by this object.  This object might hold
17681** just a prefix of the key.  The number of fields is given by
17682** pKeyInfo->nField.
17683**
17684** The r1 and r2 fields are the values to return if this key is less than
17685** or greater than a key in the btree, respectively.  These are normally
17686** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
17687** is in DESC order.
17688**
17689** The key comparison functions actually return default_rc when they find
17690** an equals comparison.  default_rc can be -1, 0, or +1.  If there are
17691** multiple entries in the b-tree with the same key (when only looking
17692** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
17693** cause the search to find the last match, or +1 to cause the search to
17694** find the first match.
17695**
17696** The key comparison functions will set eqSeen to true if they ever
17697** get and equal results when comparing this structure to a b-tree record.
17698** When default_rc!=0, the search might end up on the record immediately
17699** before the first match or immediately after the last match.  The
17700** eqSeen field will indicate whether or not an exact match exists in the
17701** b-tree.
17702*/
17703struct UnpackedRecord {
17704  KeyInfo *pKeyInfo;  /* Collation and sort-order information */
17705  Mem *aMem;          /* Values */
17706  u16 nField;         /* Number of entries in apMem[] */
17707  i8 default_rc;      /* Comparison result if keys are equal */
17708  u8 errCode;         /* Error detected by xRecordCompare (CORRUPT or NOMEM) */
17709  i8 r1;              /* Value to return if (lhs < rhs) */
17710  i8 r2;              /* Value to return if (lhs > rhs) */
17711  u8 eqSeen;          /* True if an equality comparison has been seen */
17712};
17713
17714
17715/*
17716** Each SQL index is represented in memory by an
17717** instance of the following structure.
17718**
17719** The columns of the table that are to be indexed are described
17720** by the aiColumn[] field of this structure.  For example, suppose
17721** we have the following table and index:
17722**
17723**     CREATE TABLE Ex1(c1 int, c2 int, c3 text);
17724**     CREATE INDEX Ex2 ON Ex1(c3,c1);
17725**
17726** In the Table structure describing Ex1, nCol==3 because there are
17727** three columns in the table.  In the Index structure describing
17728** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
17729** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the
17730** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
17731** The second column to be indexed (c1) has an index of 0 in
17732** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
17733**
17734** The Index.onError field determines whether or not the indexed columns
17735** must be unique and what to do if they are not.  When Index.onError=OE_None,
17736** it means this is not a unique index.  Otherwise it is a unique index
17737** and the value of Index.onError indicate the which conflict resolution
17738** algorithm to employ whenever an attempt is made to insert a non-unique
17739** element.
17740**
17741** While parsing a CREATE TABLE or CREATE INDEX statement in order to
17742** generate VDBE code (as opposed to parsing one read from an sqlite_master
17743** table as part of parsing an existing database schema), transient instances
17744** of this structure may be created. In this case the Index.tnum variable is
17745** used to store the address of a VDBE instruction, not a database page
17746** number (it cannot - the database page is not allocated until the VDBE
17747** program is executed). See convertToWithoutRowidTable() for details.
17748*/
17749struct Index {
17750  char *zName;             /* Name of this index */
17751  i16 *aiColumn;           /* Which columns are used by this index.  1st is 0 */
17752  LogEst *aiRowLogEst;     /* From ANALYZE: Est. rows selected by each column */
17753  Table *pTable;           /* The SQL table being indexed */
17754  char *zColAff;           /* String defining the affinity of each column */
17755  Index *pNext;            /* The next index associated with the same table */
17756  Schema *pSchema;         /* Schema containing this index */
17757  u8 *aSortOrder;          /* for each column: True==DESC, False==ASC */
17758  const char **azColl;     /* Array of collation sequence names for index */
17759  Expr *pPartIdxWhere;     /* WHERE clause for partial indices */
17760  ExprList *aColExpr;      /* Column expressions */
17761  int tnum;                /* DB Page containing root of this index */
17762  LogEst szIdxRow;         /* Estimated average row size in bytes */
17763  u16 nKeyCol;             /* Number of columns forming the key */
17764  u16 nColumn;             /* Number of columns stored in the index */
17765  u8 onError;              /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
17766  unsigned idxType:2;      /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */
17767  unsigned bUnordered:1;   /* Use this index for == or IN queries only */
17768  unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
17769  unsigned isResized:1;    /* True if resizeIndexObject() has been called */
17770  unsigned isCovering:1;   /* True if this is a covering index */
17771  unsigned noSkipScan:1;   /* Do not try to use skip-scan if true */
17772  unsigned hasStat1:1;     /* aiRowLogEst values come from sqlite_stat1 */
17773  unsigned bNoQuery:1;     /* Do not use this index to optimize queries */
17774  unsigned bAscKeyBug:1;   /* True if the bba7b69f9849b5bf bug applies */
17775  unsigned bHasVCol:1;     /* Index references one or more VIRTUAL columns */
17776#ifdef SQLITE_ENABLE_STAT4
17777  int nSample;             /* Number of elements in aSample[] */
17778  int nSampleCol;          /* Size of IndexSample.anEq[] and so on */
17779  tRowcnt *aAvgEq;         /* Average nEq values for keys not in aSample */
17780  IndexSample *aSample;    /* Samples of the left-most key */
17781  tRowcnt *aiRowEst;       /* Non-logarithmic stat1 data for this index */
17782  tRowcnt nRowEst0;        /* Non-logarithmic number of rows in the index */
17783#endif
17784  Bitmask colNotIdxed;     /* 0 for unindexed columns in pTab */
17785};
17786
17787/*
17788** Allowed values for Index.idxType
17789*/
17790#define SQLITE_IDXTYPE_APPDEF      0   /* Created using CREATE INDEX */
17791#define SQLITE_IDXTYPE_UNIQUE      1   /* Implements a UNIQUE constraint */
17792#define SQLITE_IDXTYPE_PRIMARYKEY  2   /* Is the PRIMARY KEY for the table */
17793#define SQLITE_IDXTYPE_IPK         3   /* INTEGER PRIMARY KEY index */
17794
17795/* Return true if index X is a PRIMARY KEY index */
17796#define IsPrimaryKeyIndex(X)  ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
17797
17798/* Return true if index X is a UNIQUE index */
17799#define IsUniqueIndex(X)      ((X)->onError!=OE_None)
17800
17801/* The Index.aiColumn[] values are normally positive integer.  But
17802** there are some negative values that have special meaning:
17803*/
17804#define XN_ROWID     (-1)     /* Indexed column is the rowid */
17805#define XN_EXPR      (-2)     /* Indexed column is an expression */
17806
17807/*
17808** Each sample stored in the sqlite_stat4 table is represented in memory
17809** using a structure of this type.  See documentation at the top of the
17810** analyze.c source file for additional information.
17811*/
17812struct IndexSample {
17813  void *p;          /* Pointer to sampled record */
17814  int n;            /* Size of record in bytes */
17815  tRowcnt *anEq;    /* Est. number of rows where the key equals this sample */
17816  tRowcnt *anLt;    /* Est. number of rows where key is less than this sample */
17817  tRowcnt *anDLt;   /* Est. number of distinct keys less than this sample */
17818};
17819
17820/*
17821** Possible values to use within the flags argument to sqlite3GetToken().
17822*/
17823#define SQLITE_TOKEN_QUOTED    0x1 /* Token is a quoted identifier. */
17824#define SQLITE_TOKEN_KEYWORD   0x2 /* Token is a keyword. */
17825
17826/*
17827** Each token coming out of the lexer is an instance of
17828** this structure.  Tokens are also used as part of an expression.
17829**
17830** The memory that "z" points to is owned by other objects.  Take care
17831** that the owner of the "z" string does not deallocate the string before
17832** the Token goes out of scope!  Very often, the "z" points to some place
17833** in the middle of the Parse.zSql text.  But it might also point to a
17834** static string.
17835*/
17836struct Token {
17837  const char *z;     /* Text of the token.  Not NULL-terminated! */
17838  unsigned int n;    /* Number of characters in this token */
17839};
17840
17841/*
17842** An instance of this structure contains information needed to generate
17843** code for a SELECT that contains aggregate functions.
17844**
17845** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
17846** pointer to this structure.  The Expr.iAgg field is the index in
17847** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
17848** code for that node.
17849**
17850** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
17851** original Select structure that describes the SELECT statement.  These
17852** fields do not need to be freed when deallocating the AggInfo structure.
17853*/
17854struct AggInfo {
17855  u8 directMode;          /* Direct rendering mode means take data directly
17856                          ** from source tables rather than from accumulators */
17857  u8 useSortingIdx;       /* In direct mode, reference the sorting index rather
17858                          ** than the source table */
17859  int sortingIdx;         /* Cursor number of the sorting index */
17860  int sortingIdxPTab;     /* Cursor number of pseudo-table */
17861  int nSortingColumn;     /* Number of columns in the sorting index */
17862  int mnReg, mxReg;       /* Range of registers allocated for aCol and aFunc */
17863  ExprList *pGroupBy;     /* The group by clause */
17864  struct AggInfo_col {    /* For each column used in source tables */
17865    Table *pTab;             /* Source table */
17866    Expr *pExpr;             /* The original expression */
17867    int iTable;              /* Cursor number of the source table */
17868    int iMem;                /* Memory location that acts as accumulator */
17869    i16 iColumn;             /* Column number within the source table */
17870    i16 iSorterColumn;       /* Column number in the sorting index */
17871  } *aCol;
17872  int nColumn;            /* Number of used entries in aCol[] */
17873  int nAccumulator;       /* Number of columns that show through to the output.
17874                          ** Additional columns are used only as parameters to
17875                          ** aggregate functions */
17876  struct AggInfo_func {   /* For each aggregate function */
17877    Expr *pExpr;             /* Expression encoding the function */
17878    FuncDef *pFunc;          /* The aggregate function implementation */
17879    int iMem;                /* Memory location that acts as accumulator */
17880    int iDistinct;           /* Ephemeral table used to enforce DISTINCT */
17881  } *aFunc;
17882  int nFunc;              /* Number of entries in aFunc[] */
17883#ifdef SQLITE_DEBUG
17884  int iAggMagic;          /* Magic number when valid */
17885#endif
17886  AggInfo *pNext;         /* Next in list of them all */
17887};
17888
17889/*
17890** Value for AggInfo.iAggMagic when the structure is valid
17891*/
17892#define AggInfoMagic  0x2059e99e
17893
17894/*
17895** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
17896** Usually it is 16-bits.  But if SQLITE_MAX_VARIABLE_NUMBER is greater
17897** than 32767 we have to make it 32-bit.  16-bit is preferred because
17898** it uses less memory in the Expr object, which is a big memory user
17899** in systems with lots of prepared statements.  And few applications
17900** need more than about 10 or 20 variables.  But some extreme users want
17901** to have prepared statements with over 32766 variables, and for them
17902** the option is available (at compile-time).
17903*/
17904#if SQLITE_MAX_VARIABLE_NUMBER<32767
17905typedef i16 ynVar;
17906#else
17907typedef int ynVar;
17908#endif
17909
17910/*
17911** Each node of an expression in the parse tree is an instance
17912** of this structure.
17913**
17914** Expr.op is the opcode. The integer parser token codes are reused
17915** as opcodes here. For example, the parser defines TK_GE to be an integer
17916** code representing the ">=" operator. This same integer code is reused
17917** to represent the greater-than-or-equal-to operator in the expression
17918** tree.
17919**
17920** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
17921** or TK_STRING), then Expr.token contains the text of the SQL literal. If
17922** the expression is a variable (TK_VARIABLE), then Expr.token contains the
17923** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
17924** then Expr.token contains the name of the function.
17925**
17926** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
17927** binary operator. Either or both may be NULL.
17928**
17929** Expr.x.pList is a list of arguments if the expression is an SQL function,
17930** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
17931** Expr.x.pSelect is used if the expression is a sub-select or an expression of
17932** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
17933** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
17934** valid.
17935**
17936** An expression of the form ID or ID.ID refers to a column in a table.
17937** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
17938** the integer cursor number of a VDBE cursor pointing to that table and
17939** Expr.iColumn is the column number for the specific column.  If the
17940** expression is used as a result in an aggregate SELECT, then the
17941** value is also stored in the Expr.iAgg column in the aggregate so that
17942** it can be accessed after all aggregates are computed.
17943**
17944** If the expression is an unbound variable marker (a question mark
17945** character '?' in the original SQL) then the Expr.iTable holds the index
17946** number for that variable.
17947**
17948** If the expression is a subquery then Expr.iColumn holds an integer
17949** register number containing the result of the subquery.  If the
17950** subquery gives a constant result, then iTable is -1.  If the subquery
17951** gives a different answer at different times during statement processing
17952** then iTable is the address of a subroutine that computes the subquery.
17953**
17954** If the Expr is of type OP_Column, and the table it is selecting from
17955** is a disk table or the "old.*" pseudo-table, then pTab points to the
17956** corresponding table definition.
17957**
17958** ALLOCATION NOTES:
17959**
17960** Expr objects can use a lot of memory space in database schema.  To
17961** help reduce memory requirements, sometimes an Expr object will be
17962** truncated.  And to reduce the number of memory allocations, sometimes
17963** two or more Expr objects will be stored in a single memory allocation,
17964** together with Expr.zToken strings.
17965**
17966** If the EP_Reduced and EP_TokenOnly flags are set when
17967** an Expr object is truncated.  When EP_Reduced is set, then all
17968** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
17969** are contained within the same memory allocation.  Note, however, that
17970** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
17971** allocated, regardless of whether or not EP_Reduced is set.
17972*/
17973struct Expr {
17974  u8 op;                 /* Operation performed by this node */
17975  char affExpr;          /* affinity, or RAISE type */
17976  u8 op2;                /* TK_REGISTER/TK_TRUTH: original value of Expr.op
17977                         ** TK_COLUMN: the value of p5 for OP_Column
17978                         ** TK_AGG_FUNCTION: nesting depth
17979                         ** TK_FUNCTION: NC_SelfRef flag if needs OP_PureFunc */
17980#ifdef SQLITE_DEBUG
17981  u8 vvaFlags;           /* Verification flags. */
17982#endif
17983  u32 flags;             /* Various flags.  EP_* See below */
17984  union {
17985    char *zToken;          /* Token value. Zero terminated and dequoted */
17986    int iValue;            /* Non-negative integer value if EP_IntValue */
17987  } u;
17988
17989  /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
17990  ** space is allocated for the fields below this point. An attempt to
17991  ** access them will result in a segfault or malfunction.
17992  *********************************************************************/
17993
17994  Expr *pLeft;           /* Left subnode */
17995  Expr *pRight;          /* Right subnode */
17996  union {
17997    ExprList *pList;     /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
17998    Select *pSelect;     /* EP_xIsSelect and op = IN, EXISTS, SELECT */
17999  } x;
18000
18001  /* If the EP_Reduced flag is set in the Expr.flags mask, then no
18002  ** space is allocated for the fields below this point. An attempt to
18003  ** access them will result in a segfault or malfunction.
18004  *********************************************************************/
18005
18006#if SQLITE_MAX_EXPR_DEPTH>0
18007  int nHeight;           /* Height of the tree headed by this node */
18008#endif
18009  int iTable;            /* TK_COLUMN: cursor number of table holding column
18010                         ** TK_REGISTER: register number
18011                         ** TK_TRIGGER: 1 -> new, 0 -> old
18012                         ** EP_Unlikely:  134217728 times likelihood
18013                         ** TK_IN: ephemerial table holding RHS
18014                         ** TK_SELECT_COLUMN: Number of columns on the LHS
18015                         ** TK_SELECT: 1st register of result vector */
18016  ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
18017                         ** TK_VARIABLE: variable number (always >= 1).
18018                         ** TK_SELECT_COLUMN: column of the result vector */
18019  i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
18020  i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
18021  AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
18022  union {
18023    Table *pTab;           /* TK_COLUMN: Table containing column. Can be NULL
18024                           ** for a column of an index on an expression */
18025    Window *pWin;          /* EP_WinFunc: Window/Filter defn for a function */
18026    struct {               /* TK_IN, TK_SELECT, and TK_EXISTS */
18027      int iAddr;             /* Subroutine entry address */
18028      int regReturn;         /* Register used to hold return address */
18029    } sub;
18030  } y;
18031};
18032
18033/*
18034** The following are the meanings of bits in the Expr.flags field.
18035** Value restrictions:
18036**
18037**          EP_Agg == NC_HasAgg == SF_HasAgg
18038**          EP_Win == NC_HasWin
18039*/
18040#define EP_FromJoin   0x000001 /* Originates in ON/USING clause of outer join */
18041#define EP_Distinct   0x000002 /* Aggregate function with DISTINCT keyword */
18042#define EP_HasFunc    0x000004 /* Contains one or more functions of any kind */
18043#define EP_FixedCol   0x000008 /* TK_Column with a known fixed value */
18044#define EP_Agg        0x000010 /* Contains one or more aggregate functions */
18045#define EP_VarSelect  0x000020 /* pSelect is correlated, not constant */
18046#define EP_DblQuoted  0x000040 /* token.z was originally in "..." */
18047#define EP_InfixFunc  0x000080 /* True for an infix function: LIKE, GLOB, etc */
18048#define EP_Collate    0x000100 /* Tree contains a TK_COLLATE operator */
18049#define EP_Commuted   0x000200 /* Comparison operator has been commuted */
18050#define EP_IntValue   0x000400 /* Integer value contained in u.iValue */
18051#define EP_xIsSelect  0x000800 /* x.pSelect is valid (otherwise x.pList is) */
18052#define EP_Skip       0x001000 /* Operator does not contribute to affinity */
18053#define EP_Reduced    0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
18054#define EP_TokenOnly  0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
18055#define EP_Win        0x008000 /* Contains window functions */
18056#define EP_MemToken   0x010000 /* Need to sqlite3DbFree() Expr.zToken */
18057                  /*  0x020000 // available for reuse */
18058#define EP_Unlikely   0x040000 /* unlikely() or likelihood() function */
18059#define EP_ConstFunc  0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
18060#define EP_CanBeNull  0x100000 /* Can be null despite NOT NULL constraint */
18061#define EP_Subquery   0x200000 /* Tree contains a TK_SELECT operator */
18062#define EP_Alias      0x400000 /* Is an alias for a result set column */
18063#define EP_Leaf       0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
18064#define EP_WinFunc   0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
18065#define EP_Subrtn    0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
18066#define EP_Quoted    0x4000000 /* TK_ID was originally quoted */
18067#define EP_Static    0x8000000 /* Held in memory not obtained from malloc() */
18068#define EP_IsTrue   0x10000000 /* Always has boolean value of TRUE */
18069#define EP_IsFalse  0x20000000 /* Always has boolean value of FALSE */
18070#define EP_FromDDL  0x40000000 /* Originates from sqlite_master */
18071               /*   0x80000000 // Available */
18072
18073/*
18074** The EP_Propagate mask is a set of properties that automatically propagate
18075** upwards into parent nodes.
18076*/
18077#define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
18078
18079/*
18080** These macros can be used to test, set, or clear bits in the
18081** Expr.flags field.
18082*/
18083#define ExprHasProperty(E,P)     (((E)->flags&(P))!=0)
18084#define ExprHasAllProperty(E,P)  (((E)->flags&(P))==(P))
18085#define ExprSetProperty(E,P)     (E)->flags|=(P)
18086#define ExprClearProperty(E,P)   (E)->flags&=~(P)
18087#define ExprAlwaysTrue(E)   (((E)->flags&(EP_FromJoin|EP_IsTrue))==EP_IsTrue)
18088#define ExprAlwaysFalse(E)  (((E)->flags&(EP_FromJoin|EP_IsFalse))==EP_IsFalse)
18089
18090
18091/* Flags for use with Expr.vvaFlags
18092*/
18093#define EP_NoReduce   0x01  /* Cannot EXPRDUP_REDUCE this Expr */
18094#define EP_Immutable  0x02  /* Do not change this Expr node */
18095
18096/* The ExprSetVVAProperty() macro is used for Verification, Validation,
18097** and Accreditation only.  It works like ExprSetProperty() during VVA
18098** processes but is a no-op for delivery.
18099*/
18100#ifdef SQLITE_DEBUG
18101# define ExprSetVVAProperty(E,P)   (E)->vvaFlags|=(P)
18102# define ExprHasVVAProperty(E,P)   (((E)->vvaFlags&(P))!=0)
18103# define ExprClearVVAProperties(E) (E)->vvaFlags = 0
18104#else
18105# define ExprSetVVAProperty(E,P)
18106# define ExprHasVVAProperty(E,P)   0
18107# define ExprClearVVAProperties(E)
18108#endif
18109
18110/*
18111** Macros to determine the number of bytes required by a normal Expr
18112** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
18113** and an Expr struct with the EP_TokenOnly flag set.
18114*/
18115#define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
18116#define EXPR_REDUCEDSIZE        offsetof(Expr,iTable)  /* Common features */
18117#define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
18118
18119/*
18120** Flags passed to the sqlite3ExprDup() function. See the header comment
18121** above sqlite3ExprDup() for details.
18122*/
18123#define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
18124
18125/*
18126** True if the expression passed as an argument was a function with
18127** an OVER() clause (a window function).
18128*/
18129#ifdef SQLITE_OMIT_WINDOWFUNC
18130# define IsWindowFunc(p) 0
18131#else
18132# define IsWindowFunc(p) ( \
18133    ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
18134 )
18135#endif
18136
18137/*
18138** A list of expressions.  Each expression may optionally have a
18139** name.  An expr/name combination can be used in several ways, such
18140** as the list of "expr AS ID" fields following a "SELECT" or in the
18141** list of "ID = expr" items in an UPDATE.  A list of expressions can
18142** also be used as the argument to a function, in which case the a.zName
18143** field is not used.
18144**
18145** In order to try to keep memory usage down, the Expr.a.zEName field
18146** is used for multiple purposes:
18147**
18148**     eEName          Usage
18149**    ----------       -------------------------
18150**    ENAME_NAME       (1) the AS of result set column
18151**                     (2) COLUMN= of an UPDATE
18152**
18153**    ENAME_TAB        DB.TABLE.NAME used to resolve names
18154**                     of subqueries
18155**
18156**    ENAME_SPAN       Text of the original result set
18157**                     expression.
18158*/
18159struct ExprList {
18160  int nExpr;             /* Number of expressions on the list */
18161  struct ExprList_item { /* For each expression in the list */
18162    Expr *pExpr;            /* The parse tree for this expression */
18163    char *zEName;           /* Token associated with this expression */
18164    u8 sortFlags;           /* Mask of KEYINFO_ORDER_* flags */
18165    unsigned eEName :2;     /* Meaning of zEName */
18166    unsigned done :1;       /* A flag to indicate when processing is finished */
18167    unsigned reusable :1;   /* Constant expression is reusable */
18168    unsigned bSorterRef :1; /* Defer evaluation until after sorting */
18169    unsigned bNulls: 1;     /* True if explicit "NULLS FIRST/LAST" */
18170    union {
18171      struct {
18172        u16 iOrderByCol;      /* For ORDER BY, column number in result set */
18173        u16 iAlias;           /* Index into Parse.aAlias[] for zName */
18174      } x;
18175      int iConstExprReg;      /* Register in which Expr value is cached */
18176    } u;
18177  } a[1];                  /* One slot for each expression in the list */
18178};
18179
18180/*
18181** Allowed values for Expr.a.eEName
18182*/
18183#define ENAME_NAME  0       /* The AS clause of a result set */
18184#define ENAME_SPAN  1       /* Complete text of the result set expression */
18185#define ENAME_TAB   2       /* "DB.TABLE.NAME" for the result set */
18186
18187/*
18188** An instance of this structure can hold a simple list of identifiers,
18189** such as the list "a,b,c" in the following statements:
18190**
18191**      INSERT INTO t(a,b,c) VALUES ...;
18192**      CREATE INDEX idx ON t(a,b,c);
18193**      CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
18194**
18195** The IdList.a.idx field is used when the IdList represents the list of
18196** column names after a table name in an INSERT statement.  In the statement
18197**
18198**     INSERT INTO t(a,b,c) ...
18199**
18200** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
18201*/
18202struct IdList {
18203  struct IdList_item {
18204    char *zName;      /* Name of the identifier */
18205    int idx;          /* Index in some Table.aCol[] of a column named zName */
18206  } *a;
18207  int nId;         /* Number of identifiers on the list */
18208};
18209
18210/*
18211** The following structure describes the FROM clause of a SELECT statement.
18212** Each table or subquery in the FROM clause is a separate element of
18213** the SrcList.a[] array.
18214**
18215** With the addition of multiple database support, the following structure
18216** can also be used to describe a particular table such as the table that
18217** is modified by an INSERT, DELETE, or UPDATE statement.  In standard SQL,
18218** such a table must be a simple name: ID.  But in SQLite, the table can
18219** now be identified by a database name, a dot, then the table name: ID.ID.
18220**
18221** The jointype starts out showing the join type between the current table
18222** and the next table on the list.  The parser builds the list this way.
18223** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
18224** jointype expresses the join between the table and the previous table.
18225**
18226** In the colUsed field, the high-order bit (bit 63) is set if the table
18227** contains more than 63 columns and the 64-th or later column is used.
18228*/
18229struct SrcList {
18230  int nSrc;        /* Number of tables or subqueries in the FROM clause */
18231  u32 nAlloc;      /* Number of entries allocated in a[] below */
18232  struct SrcList_item {
18233    Schema *pSchema;  /* Schema to which this item is fixed */
18234    char *zDatabase;  /* Name of database holding this table */
18235    char *zName;      /* Name of the table */
18236    char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
18237    Table *pTab;      /* An SQL table corresponding to zName */
18238    Select *pSelect;  /* A SELECT statement used in place of a table name */
18239    int addrFillSub;  /* Address of subroutine to manifest a subquery */
18240    int regReturn;    /* Register holding return address of addrFillSub */
18241    int regResult;    /* Registers holding results of a co-routine */
18242    struct {
18243      u8 jointype;      /* Type of join between this table and the previous */
18244      unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
18245      unsigned isIndexedBy :1;   /* True if there is an INDEXED BY clause */
18246      unsigned isTabFunc :1;     /* True if table-valued-function syntax */
18247      unsigned isCorrelated :1;  /* True if sub-query is correlated */
18248      unsigned viaCoroutine :1;  /* Implemented as a co-routine */
18249      unsigned isRecursive :1;   /* True for recursive reference in WITH */
18250      unsigned fromDDL :1;       /* Comes from sqlite_master */
18251    } fg;
18252    int iCursor;      /* The VDBE cursor number used to access this table */
18253    Expr *pOn;        /* The ON clause of a join */
18254    IdList *pUsing;   /* The USING clause of a join */
18255    Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
18256    union {
18257      char *zIndexedBy;    /* Identifier from "INDEXED BY <zIndex>" clause */
18258      ExprList *pFuncArg;  /* Arguments to table-valued-function */
18259    } u1;
18260    Index *pIBIndex;  /* Index structure corresponding to u1.zIndexedBy */
18261  } a[1];             /* One entry for each identifier on the list */
18262};
18263
18264/*
18265** Permitted values of the SrcList.a.jointype field
18266*/
18267#define JT_INNER     0x0001    /* Any kind of inner or cross join */
18268#define JT_CROSS     0x0002    /* Explicit use of the CROSS keyword */
18269#define JT_NATURAL   0x0004    /* True for a "natural" join */
18270#define JT_LEFT      0x0008    /* Left outer join */
18271#define JT_RIGHT     0x0010    /* Right outer join */
18272#define JT_OUTER     0x0020    /* The "OUTER" keyword is present */
18273#define JT_ERROR     0x0040    /* unknown or unsupported join type */
18274
18275
18276/*
18277** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
18278** and the WhereInfo.wctrlFlags member.
18279**
18280** Value constraints (enforced via assert()):
18281**     WHERE_USE_LIMIT  == SF_FixedLimit
18282*/
18283#define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
18284#define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
18285#define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
18286#define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
18287#define WHERE_ONEPASS_MULTIROW 0x0008 /* ONEPASS is ok with multiple rows */
18288#define WHERE_DUPLICATES_OK    0x0010 /* Ok to return a row more than once */
18289#define WHERE_OR_SUBCLAUSE     0x0020 /* Processing a sub-WHERE as part of
18290                                      ** the OR optimization  */
18291#define WHERE_GROUPBY          0x0040 /* pOrderBy is really a GROUP BY */
18292#define WHERE_DISTINCTBY       0x0080 /* pOrderby is really a DISTINCT clause */
18293#define WHERE_WANT_DISTINCT    0x0100 /* All output needs to be distinct */
18294#define WHERE_SORTBYGROUP      0x0200 /* Support sqlite3WhereIsSorted() */
18295#define WHERE_SEEK_TABLE       0x0400 /* Do not defer seeks on main table */
18296#define WHERE_ORDERBY_LIMIT    0x0800 /* ORDERBY+LIMIT on the inner loop */
18297#define WHERE_SEEK_UNIQ_TABLE  0x1000 /* Do not defer seeks if unique */
18298                        /*     0x2000    not currently used */
18299#define WHERE_USE_LIMIT        0x4000 /* Use the LIMIT in cost estimates */
18300                        /*     0x8000    not currently used */
18301
18302/* Allowed return values from sqlite3WhereIsDistinct()
18303*/
18304#define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
18305#define WHERE_DISTINCT_UNIQUE    1  /* No duplicates */
18306#define WHERE_DISTINCT_ORDERED   2  /* All duplicates are adjacent */
18307#define WHERE_DISTINCT_UNORDERED 3  /* Duplicates are scattered */
18308
18309/*
18310** A NameContext defines a context in which to resolve table and column
18311** names.  The context consists of a list of tables (the pSrcList) field and
18312** a list of named expression (pEList).  The named expression list may
18313** be NULL.  The pSrc corresponds to the FROM clause of a SELECT or
18314** to the table being operated on by INSERT, UPDATE, or DELETE.  The
18315** pEList corresponds to the result set of a SELECT and is NULL for
18316** other statements.
18317**
18318** NameContexts can be nested.  When resolving names, the inner-most
18319** context is searched first.  If no match is found, the next outer
18320** context is checked.  If there is still no match, the next context
18321** is checked.  This process continues until either a match is found
18322** or all contexts are check.  When a match is found, the nRef member of
18323** the context containing the match is incremented.
18324**
18325** Each subquery gets a new NameContext.  The pNext field points to the
18326** NameContext in the parent query.  Thus the process of scanning the
18327** NameContext list corresponds to searching through successively outer
18328** subqueries looking for a match.
18329*/
18330struct NameContext {
18331  Parse *pParse;       /* The parser */
18332  SrcList *pSrcList;   /* One or more tables used to resolve names */
18333  union {
18334    ExprList *pEList;    /* Optional list of result-set columns */
18335    AggInfo *pAggInfo;   /* Information about aggregates at this level */
18336    Upsert *pUpsert;     /* ON CONFLICT clause information from an upsert */
18337  } uNC;
18338  NameContext *pNext;  /* Next outer name context.  NULL for outermost */
18339  int nRef;            /* Number of names resolved by this context */
18340  int nErr;            /* Number of errors encountered while resolving names */
18341  int ncFlags;         /* Zero or more NC_* flags defined below */
18342  Select *pWinSelect;  /* SELECT statement for any window functions */
18343};
18344
18345/*
18346** Allowed values for the NameContext, ncFlags field.
18347**
18348** Value constraints (all checked via assert()):
18349**    NC_HasAgg    == SF_HasAgg    == EP_Agg
18350**    NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
18351**    NC_HasWin    == EP_Win
18352**
18353*/
18354#define NC_AllowAgg  0x00001  /* Aggregate functions are allowed here */
18355#define NC_PartIdx   0x00002  /* True if resolving a partial index WHERE */
18356#define NC_IsCheck   0x00004  /* True if resolving a CHECK constraint */
18357#define NC_GenCol    0x00008  /* True for a GENERATED ALWAYS AS clause */
18358#define NC_HasAgg    0x00010  /* One or more aggregate functions seen */
18359#define NC_IdxExpr   0x00020  /* True if resolving columns of CREATE INDEX */
18360#define NC_SelfRef   0x0002e  /* Combo: PartIdx, isCheck, GenCol, and IdxExpr */
18361#define NC_VarSelect 0x00040  /* A correlated subquery has been seen */
18362#define NC_UEList    0x00080  /* True if uNC.pEList is used */
18363#define NC_UAggInfo  0x00100  /* True if uNC.pAggInfo is used */
18364#define NC_UUpsert   0x00200  /* True if uNC.pUpsert is used */
18365#define NC_MinMaxAgg 0x01000  /* min/max aggregates seen.  See note above */
18366#define NC_Complex   0x02000  /* True if a function or subquery seen */
18367#define NC_AllowWin  0x04000  /* Window functions are allowed here */
18368#define NC_HasWin    0x08000  /* One or more window functions seen */
18369#define NC_IsDDL     0x10000  /* Resolving names in a CREATE statement */
18370#define NC_InAggFunc 0x20000  /* True if analyzing arguments to an agg func */
18371#define NC_FromDDL   0x40000  /* SQL text comes from sqlite_master */
18372
18373/*
18374** An instance of the following object describes a single ON CONFLICT
18375** clause in an upsert.
18376**
18377** The pUpsertTarget field is only set if the ON CONFLICT clause includes
18378** conflict-target clause.  (In "ON CONFLICT(a,b)" the "(a,b)" is the
18379** conflict-target clause.)  The pUpsertTargetWhere is the optional
18380** WHERE clause used to identify partial unique indexes.
18381**
18382** pUpsertSet is the list of column=expr terms of the UPDATE statement.
18383** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING.  The
18384** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the
18385** WHERE clause is omitted.
18386*/
18387struct Upsert {
18388  ExprList *pUpsertTarget;  /* Optional description of conflicting index */
18389  Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */
18390  ExprList *pUpsertSet;     /* The SET clause from an ON CONFLICT UPDATE */
18391  Expr *pUpsertWhere;       /* WHERE clause for the ON CONFLICT UPDATE */
18392  /* The fields above comprise the parse tree for the upsert clause.
18393  ** The fields below are used to transfer information from the INSERT
18394  ** processing down into the UPDATE processing while generating code.
18395  ** Upsert owns the memory allocated above, but not the memory below. */
18396  Index *pUpsertIdx;        /* Constraint that pUpsertTarget identifies */
18397  SrcList *pUpsertSrc;      /* Table to be updated */
18398  int regData;              /* First register holding array of VALUES */
18399  int iDataCur;             /* Index of the data cursor */
18400  int iIdxCur;              /* Index of the first index cursor */
18401};
18402
18403/*
18404** An instance of the following structure contains all information
18405** needed to generate code for a single SELECT statement.
18406**
18407** See the header comment on the computeLimitRegisters() routine for a
18408** detailed description of the meaning of the iLimit and iOffset fields.
18409**
18410** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
18411** These addresses must be stored so that we can go back and fill in
18412** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
18413** the number of columns in P2 can be computed at the same time
18414** as the OP_OpenEphm instruction is coded because not
18415** enough information about the compound query is known at that point.
18416** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
18417** for the result set.  The KeyInfo for addrOpenEphm[2] contains collating
18418** sequences for the ORDER BY clause.
18419*/
18420struct Select {
18421  u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
18422  LogEst nSelectRow;     /* Estimated number of result rows */
18423  u32 selFlags;          /* Various SF_* values */
18424  int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
18425  u32 selId;             /* Unique identifier number for this SELECT */
18426  int addrOpenEphm[2];   /* OP_OpenEphem opcodes related to this select */
18427  ExprList *pEList;      /* The fields of the result */
18428  SrcList *pSrc;         /* The FROM clause */
18429  Expr *pWhere;          /* The WHERE clause */
18430  ExprList *pGroupBy;    /* The GROUP BY clause */
18431  Expr *pHaving;         /* The HAVING clause */
18432  ExprList *pOrderBy;    /* The ORDER BY clause */
18433  Select *pPrior;        /* Prior select in a compound select statement */
18434  Select *pNext;         /* Next select to the left in a compound */
18435  Expr *pLimit;          /* LIMIT expression. NULL means not used. */
18436  With *pWith;           /* WITH clause attached to this select. Or NULL. */
18437#ifndef SQLITE_OMIT_WINDOWFUNC
18438  Window *pWin;          /* List of window functions */
18439  Window *pWinDefn;      /* List of named window definitions */
18440#endif
18441};
18442
18443/*
18444** Allowed values for Select.selFlags.  The "SF" prefix stands for
18445** "Select Flag".
18446**
18447** Value constraints (all checked via assert())
18448**     SF_HasAgg     == NC_HasAgg
18449**     SF_MinMaxAgg  == NC_MinMaxAgg     == SQLITE_FUNC_MINMAX
18450**     SF_FixedLimit == WHERE_USE_LIMIT
18451*/
18452#define SF_Distinct      0x0000001 /* Output should be DISTINCT */
18453#define SF_All           0x0000002 /* Includes the ALL keyword */
18454#define SF_Resolved      0x0000004 /* Identifiers have been resolved */
18455#define SF_Aggregate     0x0000008 /* Contains agg functions or a GROUP BY */
18456#define SF_HasAgg        0x0000010 /* Contains aggregate functions */
18457#define SF_UsesEphemeral 0x0000020 /* Uses the OpenEphemeral opcode */
18458#define SF_Expanded      0x0000040 /* sqlite3SelectExpand() called on this */
18459#define SF_HasTypeInfo   0x0000080 /* FROM subqueries have Table metadata */
18460#define SF_Compound      0x0000100 /* Part of a compound query */
18461#define SF_Values        0x0000200 /* Synthesized from VALUES clause */
18462#define SF_MultiValue    0x0000400 /* Single VALUES term with multiple rows */
18463#define SF_NestedFrom    0x0000800 /* Part of a parenthesized FROM clause */
18464#define SF_MinMaxAgg     0x0001000 /* Aggregate containing min() or max() */
18465#define SF_Recursive     0x0002000 /* The recursive part of a recursive CTE */
18466#define SF_FixedLimit    0x0004000 /* nSelectRow set by a constant LIMIT */
18467#define SF_MaybeConvert  0x0008000 /* Need convertCompoundSelectToSubquery() */
18468#define SF_Converted     0x0010000 /* By convertCompoundSelectToSubquery() */
18469#define SF_IncludeHidden 0x0020000 /* Include hidden columns in output */
18470#define SF_ComplexResult 0x0040000 /* Result contains subquery or function */
18471#define SF_WhereBegin    0x0080000 /* Really a WhereBegin() call.  Debug Only */
18472#define SF_WinRewrite    0x0100000 /* Window function rewrite accomplished */
18473#define SF_View          0x0200000 /* SELECT statement is a view */
18474#define SF_NoopOrderBy   0x0400000 /* ORDER BY is ignored for this query */
18475
18476/*
18477** The results of a SELECT can be distributed in several ways, as defined
18478** by one of the following macros.  The "SRT" prefix means "SELECT Result
18479** Type".
18480**
18481**     SRT_Union       Store results as a key in a temporary index
18482**                     identified by pDest->iSDParm.
18483**
18484**     SRT_Except      Remove results from the temporary index pDest->iSDParm.
18485**
18486**     SRT_Exists      Store a 1 in memory cell pDest->iSDParm if the result
18487**                     set is not empty.
18488**
18489**     SRT_Discard     Throw the results away.  This is used by SELECT
18490**                     statements within triggers whose only purpose is
18491**                     the side-effects of functions.
18492**
18493** All of the above are free to ignore their ORDER BY clause. Those that
18494** follow must honor the ORDER BY clause.
18495**
18496**     SRT_Output      Generate a row of output (using the OP_ResultRow
18497**                     opcode) for each row in the result set.
18498**
18499**     SRT_Mem         Only valid if the result is a single column.
18500**                     Store the first column of the first result row
18501**                     in register pDest->iSDParm then abandon the rest
18502**                     of the query.  This destination implies "LIMIT 1".
18503**
18504**     SRT_Set         The result must be a single column.  Store each
18505**                     row of result as the key in table pDest->iSDParm.
18506**                     Apply the affinity pDest->affSdst before storing
18507**                     results.  Used to implement "IN (SELECT ...)".
18508**
18509**     SRT_EphemTab    Create an temporary table pDest->iSDParm and store
18510**                     the result there. The cursor is left open after
18511**                     returning.  This is like SRT_Table except that
18512**                     this destination uses OP_OpenEphemeral to create
18513**                     the table first.
18514**
18515**     SRT_Coroutine   Generate a co-routine that returns a new row of
18516**                     results each time it is invoked.  The entry point
18517**                     of the co-routine is stored in register pDest->iSDParm
18518**                     and the result row is stored in pDest->nDest registers
18519**                     starting with pDest->iSdst.
18520**
18521**     SRT_Table       Store results in temporary table pDest->iSDParm.
18522**     SRT_Fifo        This is like SRT_EphemTab except that the table
18523**                     is assumed to already be open.  SRT_Fifo has
18524**                     the additional property of being able to ignore
18525**                     the ORDER BY clause.
18526**
18527**     SRT_DistFifo    Store results in a temporary table pDest->iSDParm.
18528**                     But also use temporary table pDest->iSDParm+1 as
18529**                     a record of all prior results and ignore any duplicate
18530**                     rows.  Name means:  "Distinct Fifo".
18531**
18532**     SRT_Queue       Store results in priority queue pDest->iSDParm (really
18533**                     an index).  Append a sequence number so that all entries
18534**                     are distinct.
18535**
18536**     SRT_DistQueue   Store results in priority queue pDest->iSDParm only if
18537**                     the same record has never been stored before.  The
18538**                     index at pDest->iSDParm+1 hold all prior stores.
18539*/
18540#define SRT_Union        1  /* Store result as keys in an index */
18541#define SRT_Except       2  /* Remove result from a UNION index */
18542#define SRT_Exists       3  /* Store 1 if the result is not empty */
18543#define SRT_Discard      4  /* Do not save the results anywhere */
18544#define SRT_Fifo         5  /* Store result as data with an automatic rowid */
18545#define SRT_DistFifo     6  /* Like SRT_Fifo, but unique results only */
18546#define SRT_Queue        7  /* Store result in an queue */
18547#define SRT_DistQueue    8  /* Like SRT_Queue, but unique results only */
18548
18549/* The ORDER BY clause is ignored for all of the above */
18550#define IgnorableOrderby(X) ((X->eDest)<=SRT_DistQueue)
18551
18552#define SRT_Output       9  /* Output each row of result */
18553#define SRT_Mem         10  /* Store result in a memory cell */
18554#define SRT_Set         11  /* Store results as keys in an index */
18555#define SRT_EphemTab    12  /* Create transient tab and store like SRT_Table */
18556#define SRT_Coroutine   13  /* Generate a single row of result */
18557#define SRT_Table       14  /* Store result as data with an automatic rowid */
18558
18559/*
18560** An instance of this object describes where to put of the results of
18561** a SELECT statement.
18562*/
18563struct SelectDest {
18564  u8 eDest;            /* How to dispose of the results.  On of SRT_* above. */
18565  int iSDParm;         /* A parameter used by the eDest disposal method */
18566  int iSdst;           /* Base register where results are written */
18567  int nSdst;           /* Number of registers allocated */
18568  char *zAffSdst;      /* Affinity used when eDest==SRT_Set */
18569  ExprList *pOrderBy;  /* Key columns for SRT_Queue and SRT_DistQueue */
18570};
18571
18572/*
18573** During code generation of statements that do inserts into AUTOINCREMENT
18574** tables, the following information is attached to the Table.u.autoInc.p
18575** pointer of each autoincrement table to record some side information that
18576** the code generator needs.  We have to keep per-table autoincrement
18577** information in case inserts are done within triggers.  Triggers do not
18578** normally coordinate their activities, but we do need to coordinate the
18579** loading and saving of autoincrement information.
18580*/
18581struct AutoincInfo {
18582  AutoincInfo *pNext;   /* Next info block in a list of them all */
18583  Table *pTab;          /* Table this info block refers to */
18584  int iDb;              /* Index in sqlite3.aDb[] of database holding pTab */
18585  int regCtr;           /* Memory register holding the rowid counter */
18586};
18587
18588/*
18589** At least one instance of the following structure is created for each
18590** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
18591** statement. All such objects are stored in the linked list headed at
18592** Parse.pTriggerPrg and deleted once statement compilation has been
18593** completed.
18594**
18595** A Vdbe sub-program that implements the body and WHEN clause of trigger
18596** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
18597** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
18598** The Parse.pTriggerPrg list never contains two entries with the same
18599** values for both pTrigger and orconf.
18600**
18601** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
18602** accessed (or set to 0 for triggers fired as a result of INSERT
18603** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
18604** a mask of new.* columns used by the program.
18605*/
18606struct TriggerPrg {
18607  Trigger *pTrigger;      /* Trigger this program was coded from */
18608  TriggerPrg *pNext;      /* Next entry in Parse.pTriggerPrg list */
18609  SubProgram *pProgram;   /* Program implementing pTrigger/orconf */
18610  int orconf;             /* Default ON CONFLICT policy */
18611  u32 aColmask[2];        /* Masks of old.*, new.* columns accessed */
18612};
18613
18614/*
18615** The yDbMask datatype for the bitmask of all attached databases.
18616*/
18617#if SQLITE_MAX_ATTACHED>30
18618  typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8];
18619# define DbMaskTest(M,I)    (((M)[(I)/8]&(1<<((I)&7)))!=0)
18620# define DbMaskZero(M)      memset((M),0,sizeof(M))
18621# define DbMaskSet(M,I)     (M)[(I)/8]|=(1<<((I)&7))
18622# define DbMaskAllZero(M)   sqlite3DbMaskAllZero(M)
18623# define DbMaskNonZero(M)   (sqlite3DbMaskAllZero(M)==0)
18624#else
18625  typedef unsigned int yDbMask;
18626# define DbMaskTest(M,I)    (((M)&(((yDbMask)1)<<(I)))!=0)
18627# define DbMaskZero(M)      (M)=0
18628# define DbMaskSet(M,I)     (M)|=(((yDbMask)1)<<(I))
18629# define DbMaskAllZero(M)   (M)==0
18630# define DbMaskNonZero(M)   (M)!=0
18631#endif
18632
18633/*
18634** An SQL parser context.  A copy of this structure is passed through
18635** the parser and down into all the parser action routine in order to
18636** carry around information that is global to the entire parse.
18637**
18638** The structure is divided into two parts.  When the parser and code
18639** generate call themselves recursively, the first part of the structure
18640** is constant but the second part is reset at the beginning and end of
18641** each recursion.
18642**
18643** The nTableLock and aTableLock variables are only used if the shared-cache
18644** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
18645** used to store the set of table-locks required by the statement being
18646** compiled. Function sqlite3TableLock() is used to add entries to the
18647** list.
18648*/
18649struct Parse {
18650  sqlite3 *db;         /* The main database structure */
18651  char *zErrMsg;       /* An error message */
18652  Vdbe *pVdbe;         /* An engine for executing database bytecode */
18653  int rc;              /* Return code from execution */
18654  u8 colNamesSet;      /* TRUE after OP_ColumnName has been issued to pVdbe */
18655  u8 checkSchema;      /* Causes schema cookie check after an error */
18656  u8 nested;           /* Number of nested calls to the parser/code generator */
18657  u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
18658  u8 isMultiWrite;     /* True if statement may modify/insert multiple rows */
18659  u8 mayAbort;         /* True if statement may throw an ABORT exception */
18660  u8 hasCompound;      /* Need to invoke convertCompoundSelectToSubquery() */
18661  u8 okConstFactor;    /* OK to factor out constants */
18662  u8 disableLookaside; /* Number of times lookaside has been disabled */
18663  u8 disableVtab;      /* Disable all virtual tables for this parse */
18664  int nRangeReg;       /* Size of the temporary register block */
18665  int iRangeReg;       /* First register in temporary register block */
18666  int nErr;            /* Number of errors seen */
18667  int nTab;            /* Number of previously allocated VDBE cursors */
18668  int nMem;            /* Number of memory cells used so far */
18669  int szOpAlloc;       /* Bytes of memory space allocated for Vdbe.aOp[] */
18670  int iSelfTab;        /* Table associated with an index on expr, or negative
18671                       ** of the base register during check-constraint eval */
18672  int nLabel;          /* The *negative* of the number of labels used */
18673  int nLabelAlloc;     /* Number of slots in aLabel */
18674  int *aLabel;         /* Space to hold the labels */
18675  ExprList *pConstExpr;/* Constant expressions */
18676  Token constraintName;/* Name of the constraint currently being parsed */
18677  yDbMask writeMask;   /* Start a write transaction on these databases */
18678  yDbMask cookieMask;  /* Bitmask of schema verified databases */
18679  int regRowid;        /* Register holding rowid of CREATE TABLE entry */
18680  int regRoot;         /* Register holding root page number for new objects */
18681  int nMaxArg;         /* Max args passed to user function by sub-program */
18682  int nSelect;         /* Number of SELECT stmts. Counter for Select.selId */
18683#ifndef SQLITE_OMIT_SHARED_CACHE
18684  int nTableLock;        /* Number of locks in aTableLock */
18685  TableLock *aTableLock; /* Required table locks for shared-cache mode */
18686#endif
18687  AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
18688  Parse *pToplevel;    /* Parse structure for main program (or NULL) */
18689  Table *pTriggerTab;  /* Table triggers are being coded for */
18690  Parse *pParentParse; /* Parent parser if this parser is nested */
18691  AggInfo *pAggList;   /* List of all AggInfo objects */
18692  int addrCrTab;       /* Address of OP_CreateBtree opcode on CREATE TABLE */
18693  u32 nQueryLoop;      /* Est number of iterations of a query (10*log2(N)) */
18694  u32 oldmask;         /* Mask of old.* columns referenced */
18695  u32 newmask;         /* Mask of new.* columns referenced */
18696  u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
18697  u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
18698  u8 disableTriggers;  /* True to disable triggers */
18699
18700  /**************************************************************************
18701  ** Fields above must be initialized to zero.  The fields that follow,
18702  ** down to the beginning of the recursive section, do not need to be
18703  ** initialized as they will be set before being used.  The boundary is
18704  ** determined by offsetof(Parse,aTempReg).
18705  **************************************************************************/
18706
18707  int aTempReg[8];        /* Holding area for temporary registers */
18708  Token sNameToken;       /* Token with unqualified schema object name */
18709
18710  /************************************************************************
18711  ** Above is constant between recursions.  Below is reset before and after
18712  ** each recursion.  The boundary between these two regions is determined
18713  ** using offsetof(Parse,sLastToken) so the sLastToken field must be the
18714  ** first field in the recursive region.
18715  ************************************************************************/
18716
18717  Token sLastToken;       /* The last token parsed */
18718  ynVar nVar;               /* Number of '?' variables seen in the SQL so far */
18719  u8 iPkSortOrder;          /* ASC or DESC for INTEGER PRIMARY KEY */
18720  u8 explain;               /* True if the EXPLAIN flag is found on the query */
18721#if !(defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE))
18722  u8 eParseMode;            /* PARSE_MODE_XXX constant */
18723#endif
18724#ifndef SQLITE_OMIT_VIRTUALTABLE
18725  int nVtabLock;            /* Number of virtual tables to lock */
18726#endif
18727  int nHeight;              /* Expression tree height of current sub-select */
18728#ifndef SQLITE_OMIT_EXPLAIN
18729  int addrExplain;          /* Address of current OP_Explain opcode */
18730#endif
18731  VList *pVList;            /* Mapping between variable names and numbers */
18732  Vdbe *pReprepare;         /* VM being reprepared (sqlite3Reprepare()) */
18733  const char *zTail;        /* All SQL text past the last semicolon parsed */
18734  Table *pNewTable;         /* A table being constructed by CREATE TABLE */
18735  Index *pNewIndex;         /* An index being constructed by CREATE INDEX.
18736                            ** Also used to hold redundant UNIQUE constraints
18737                            ** during a RENAME COLUMN */
18738  Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
18739  const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
18740#ifndef SQLITE_OMIT_VIRTUALTABLE
18741  Token sArg;               /* Complete text of a module argument */
18742  Table **apVtabLock;       /* Pointer to virtual tables needing locking */
18743#endif
18744  Table *pZombieTab;        /* List of Table objects to delete after code gen */
18745  TriggerPrg *pTriggerPrg;  /* Linked list of coded triggers */
18746  With *pWith;              /* Current WITH clause, or NULL */
18747  With *pWithToFree;        /* Free this WITH object at the end of the parse */
18748#ifndef SQLITE_OMIT_ALTERTABLE
18749  RenameToken *pRename;     /* Tokens subject to renaming by ALTER TABLE */
18750#endif
18751};
18752
18753#define PARSE_MODE_NORMAL        0
18754#define PARSE_MODE_DECLARE_VTAB  1
18755#define PARSE_MODE_RENAME        2
18756#define PARSE_MODE_UNMAP         3
18757
18758/*
18759** Sizes and pointers of various parts of the Parse object.
18760*/
18761#define PARSE_HDR_SZ offsetof(Parse,aTempReg) /* Recursive part w/o aColCache*/
18762#define PARSE_RECURSE_SZ offsetof(Parse,sLastToken)    /* Recursive part */
18763#define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
18764#define PARSE_TAIL(X) (((char*)(X))+PARSE_RECURSE_SZ)  /* Pointer to tail */
18765
18766/*
18767** Return true if currently inside an sqlite3_declare_vtab() call.
18768*/
18769#ifdef SQLITE_OMIT_VIRTUALTABLE
18770  #define IN_DECLARE_VTAB 0
18771#else
18772  #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
18773#endif
18774
18775#if defined(SQLITE_OMIT_ALTERTABLE)
18776  #define IN_RENAME_OBJECT 0
18777#else
18778  #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME)
18779#endif
18780
18781#if defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE)
18782  #define IN_SPECIAL_PARSE 0
18783#else
18784  #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
18785#endif
18786
18787/*
18788** An instance of the following structure can be declared on a stack and used
18789** to save the Parse.zAuthContext value so that it can be restored later.
18790*/
18791struct AuthContext {
18792  const char *zAuthContext;   /* Put saved Parse.zAuthContext here */
18793  Parse *pParse;              /* The Parse structure */
18794};
18795
18796/*
18797** Bitfield flags for P5 value in various opcodes.
18798**
18799** Value constraints (enforced via assert()):
18800**    OPFLAG_LENGTHARG    == SQLITE_FUNC_LENGTH
18801**    OPFLAG_TYPEOFARG    == SQLITE_FUNC_TYPEOF
18802**    OPFLAG_BULKCSR      == BTREE_BULKLOAD
18803**    OPFLAG_SEEKEQ       == BTREE_SEEK_EQ
18804**    OPFLAG_FORDELETE    == BTREE_FORDELETE
18805**    OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
18806**    OPFLAG_AUXDELETE    == BTREE_AUXDELETE
18807*/
18808#define OPFLAG_NCHANGE       0x01    /* OP_Insert: Set to update db->nChange */
18809                                     /* Also used in P2 (not P5) of OP_Delete */
18810#define OPFLAG_NOCHNG        0x01    /* OP_VColumn nochange for UPDATE */
18811#define OPFLAG_EPHEM         0x01    /* OP_Column: Ephemeral output is ok */
18812#define OPFLAG_LASTROWID     0x20    /* Set to update db->lastRowid */
18813#define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
18814#define OPFLAG_APPEND        0x08    /* This is likely to be an append */
18815#define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
18816#define OPFLAG_ISNOOP        0x40    /* OP_Delete does pre-update-hook only */
18817#define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
18818#define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
18819#define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
18820#define OPFLAG_SEEKEQ        0x02    /* OP_Open** cursor uses EQ seek only */
18821#define OPFLAG_FORDELETE     0x08    /* OP_Open should use BTREE_FORDELETE */
18822#define OPFLAG_P2ISREG       0x10    /* P2 to OP_Open** is a register number */
18823#define OPFLAG_PERMUTE       0x01    /* OP_Compare: use the permutation */
18824#define OPFLAG_SAVEPOSITION  0x02    /* OP_Delete/Insert: save cursor pos */
18825#define OPFLAG_AUXDELETE     0x04    /* OP_Delete: index in a DELETE op */
18826#define OPFLAG_NOCHNG_MAGIC  0x6d    /* OP_MakeRecord: serialtype 10 is ok */
18827
18828/*
18829 * Each trigger present in the database schema is stored as an instance of
18830 * struct Trigger.
18831 *
18832 * Pointers to instances of struct Trigger are stored in two ways.
18833 * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
18834 *    database). This allows Trigger structures to be retrieved by name.
18835 * 2. All triggers associated with a single table form a linked list, using the
18836 *    pNext member of struct Trigger. A pointer to the first element of the
18837 *    linked list is stored as the "pTrigger" member of the associated
18838 *    struct Table.
18839 *
18840 * The "step_list" member points to the first element of a linked list
18841 * containing the SQL statements specified as the trigger program.
18842 */
18843struct Trigger {
18844  char *zName;            /* The name of the trigger                        */
18845  char *table;            /* The table or view to which the trigger applies */
18846  u8 op;                  /* One of TK_DELETE, TK_UPDATE, TK_INSERT         */
18847  u8 tr_tm;               /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
18848  Expr *pWhen;            /* The WHEN clause of the expression (may be NULL) */
18849  IdList *pColumns;       /* If this is an UPDATE OF <column-list> trigger,
18850                             the <column-list> is stored here */
18851  Schema *pSchema;        /* Schema containing the trigger */
18852  Schema *pTabSchema;     /* Schema containing the table */
18853  TriggerStep *step_list; /* Link list of trigger program steps             */
18854  Trigger *pNext;         /* Next trigger associated with the table */
18855};
18856
18857/*
18858** A trigger is either a BEFORE or an AFTER trigger.  The following constants
18859** determine which.
18860**
18861** If there are multiple triggers, you might of some BEFORE and some AFTER.
18862** In that cases, the constants below can be ORed together.
18863*/
18864#define TRIGGER_BEFORE  1
18865#define TRIGGER_AFTER   2
18866
18867/*
18868 * An instance of struct TriggerStep is used to store a single SQL statement
18869 * that is a part of a trigger-program.
18870 *
18871 * Instances of struct TriggerStep are stored in a singly linked list (linked
18872 * using the "pNext" member) referenced by the "step_list" member of the
18873 * associated struct Trigger instance. The first element of the linked list is
18874 * the first step of the trigger-program.
18875 *
18876 * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
18877 * "SELECT" statement. The meanings of the other members is determined by the
18878 * value of "op" as follows:
18879 *
18880 * (op == TK_INSERT)
18881 * orconf    -> stores the ON CONFLICT algorithm
18882 * pSelect   -> If this is an INSERT INTO ... SELECT ... statement, then
18883 *              this stores a pointer to the SELECT statement. Otherwise NULL.
18884 * zTarget   -> Dequoted name of the table to insert into.
18885 * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
18886 *              this stores values to be inserted. Otherwise NULL.
18887 * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ...
18888 *              statement, then this stores the column-names to be
18889 *              inserted into.
18890 *
18891 * (op == TK_DELETE)
18892 * zTarget   -> Dequoted name of the table to delete from.
18893 * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
18894 *              Otherwise NULL.
18895 *
18896 * (op == TK_UPDATE)
18897 * zTarget   -> Dequoted name of the table to update.
18898 * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
18899 *              Otherwise NULL.
18900 * pExprList -> A list of the columns to update and the expressions to update
18901 *              them to. See sqlite3Update() documentation of "pChanges"
18902 *              argument.
18903 *
18904 */
18905struct TriggerStep {
18906  u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
18907  u8 orconf;           /* OE_Rollback etc. */
18908  Trigger *pTrig;      /* The trigger that this step is a part of */
18909  Select *pSelect;     /* SELECT statement or RHS of INSERT INTO SELECT ... */
18910  char *zTarget;       /* Target table for DELETE, UPDATE, INSERT */
18911  Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
18912  ExprList *pExprList; /* SET clause for UPDATE */
18913  IdList *pIdList;     /* Column names for INSERT */
18914  Upsert *pUpsert;     /* Upsert clauses on an INSERT */
18915  char *zSpan;         /* Original SQL text of this command */
18916  TriggerStep *pNext;  /* Next in the link-list */
18917  TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
18918};
18919
18920/*
18921** The following structure contains information used by the sqliteFix...
18922** routines as they walk the parse tree to make database references
18923** explicit.
18924*/
18925typedef struct DbFixer DbFixer;
18926struct DbFixer {
18927  Parse *pParse;      /* The parsing context.  Error messages written here */
18928  Schema *pSchema;    /* Fix items to this schema */
18929  u8 bTemp;           /* True for TEMP schema entries */
18930  const char *zDb;    /* Make sure all objects are contained in this database */
18931  const char *zType;  /* Type of the container - used for error messages */
18932  const Token *pName; /* Name of the container - used for error messages */
18933};
18934
18935/*
18936** An objected used to accumulate the text of a string where we
18937** do not necessarily know how big the string will be in the end.
18938*/
18939struct sqlite3_str {
18940  sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
18941  char *zText;         /* The string collected so far */
18942  u32  nAlloc;         /* Amount of space allocated in zText */
18943  u32  mxAlloc;        /* Maximum allowed allocation.  0 for no malloc usage */
18944  u32  nChar;          /* Length of the string so far */
18945  u8   accError;       /* SQLITE_NOMEM or SQLITE_TOOBIG */
18946  u8   printfFlags;    /* SQLITE_PRINTF flags below */
18947};
18948#define SQLITE_PRINTF_INTERNAL 0x01  /* Internal-use-only converters allowed */
18949#define SQLITE_PRINTF_SQLFUNC  0x02  /* SQL function arguments to VXPrintf */
18950#define SQLITE_PRINTF_MALLOCED 0x04  /* True if xText is allocated space */
18951
18952#define isMalloced(X)  (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
18953
18954
18955/*
18956** A pointer to this structure is used to communicate information
18957** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
18958*/
18959typedef struct {
18960  sqlite3 *db;        /* The database being initialized */
18961  char **pzErrMsg;    /* Error message stored here */
18962  int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
18963  int rc;             /* Result code stored here */
18964  u32 mInitFlags;     /* Flags controlling error messages */
18965  u32 nInitRow;       /* Number of rows processed */
18966} InitData;
18967
18968/*
18969** Allowed values for mInitFlags
18970*/
18971#define INITFLAG_AlterTable   0x0001  /* This is a reparse after ALTER TABLE */
18972
18973/*
18974** Structure containing global configuration data for the SQLite library.
18975**
18976** This structure also contains some state information.
18977*/
18978struct Sqlite3Config {
18979  int bMemstat;                     /* True to enable memory status */
18980  u8 bCoreMutex;                    /* True to enable core mutexing */
18981  u8 bFullMutex;                    /* True to enable full mutexing */
18982  u8 bOpenUri;                      /* True to interpret filenames as URIs */
18983  u8 bUseCis;                       /* Use covering indices for full-scans */
18984  u8 bSmallMalloc;                  /* Avoid large memory allocations if true */
18985  u8 bExtraSchemaChecks;            /* Verify type,name,tbl_name in schema */
18986  int mxStrlen;                     /* Maximum string length */
18987  int neverCorrupt;                 /* Database is always well-formed */
18988  int szLookaside;                  /* Default lookaside buffer size */
18989  int nLookaside;                   /* Default lookaside buffer count */
18990  int nStmtSpill;                   /* Stmt-journal spill-to-disk threshold */
18991  sqlite3_mem_methods m;            /* Low-level memory allocation interface */
18992  sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
18993  sqlite3_pcache_methods2 pcache2;  /* Low-level page-cache interface */
18994  void *pHeap;                      /* Heap storage space */
18995  int nHeap;                        /* Size of pHeap[] */
18996  int mnReq, mxReq;                 /* Min and max heap requests sizes */
18997  sqlite3_int64 szMmap;             /* mmap() space per open file */
18998  sqlite3_int64 mxMmap;             /* Maximum value for szMmap */
18999  void *pPage;                      /* Page cache memory */
19000  int szPage;                       /* Size of each page in pPage[] */
19001  int nPage;                        /* Number of pages in pPage[] */
19002  int mxParserStack;                /* maximum depth of the parser stack */
19003  int sharedCacheEnabled;           /* true if shared-cache mode enabled */
19004  u32 szPma;                        /* Maximum Sorter PMA size */
19005  /* The above might be initialized to non-zero.  The following need to always
19006  ** initially be zero, however. */
19007  int isInit;                       /* True after initialization has finished */
19008  int inProgress;                   /* True while initialization in progress */
19009  int isMutexInit;                  /* True after mutexes are initialized */
19010  int isMallocInit;                 /* True after malloc is initialized */
19011  int isPCacheInit;                 /* True after malloc is initialized */
19012  int nRefInitMutex;                /* Number of users of pInitMutex */
19013  sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
19014  void (*xLog)(void*,int,const char*); /* Function for logging */
19015  void *pLogArg;                       /* First argument to xLog() */
19016#ifdef SQLITE_ENABLE_SQLLOG
19017  void(*xSqllog)(void*,sqlite3*,const char*, int);
19018  void *pSqllogArg;
19019#endif
19020#ifdef SQLITE_VDBE_COVERAGE
19021  /* The following callback (if not NULL) is invoked on every VDBE branch
19022  ** operation.  Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
19023  */
19024  void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx);  /* Callback */
19025  void *pVdbeBranchArg;                                     /* 1st argument */
19026#endif
19027#ifdef SQLITE_ENABLE_DESERIALIZE
19028  sqlite3_int64 mxMemdbSize;        /* Default max memdb size */
19029#endif
19030#ifndef SQLITE_UNTESTABLE
19031  int (*xTestCallback)(int);        /* Invoked by sqlite3FaultSim() */
19032#endif
19033  int bLocaltimeFault;              /* True to fail localtime() calls */
19034  int iOnceResetThreshold;          /* When to reset OP_Once counters */
19035  u32 szSorterRef;                  /* Min size in bytes to use sorter-refs */
19036  unsigned int iPrngSeed;           /* Alternative fixed seed for the PRNG */
19037};
19038
19039/*
19040** This macro is used inside of assert() statements to indicate that
19041** the assert is only valid on a well-formed database.  Instead of:
19042**
19043**     assert( X );
19044**
19045** One writes:
19046**
19047**     assert( X || CORRUPT_DB );
19048**
19049** CORRUPT_DB is true during normal operation.  CORRUPT_DB does not indicate
19050** that the database is definitely corrupt, only that it might be corrupt.
19051** For most test cases, CORRUPT_DB is set to false using a special
19052** sqlite3_test_control().  This enables assert() statements to prove
19053** things that are always true for well-formed databases.
19054*/
19055#define CORRUPT_DB  (sqlite3Config.neverCorrupt==0)
19056
19057/*
19058** Context pointer passed down through the tree-walk.
19059*/
19060struct Walker {
19061  Parse *pParse;                            /* Parser context.  */
19062  int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
19063  int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
19064  void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
19065  int walkerDepth;                          /* Number of subqueries */
19066  u16 eCode;                                /* A small processing code */
19067  union {                                   /* Extra data for callback */
19068    NameContext *pNC;                         /* Naming context */
19069    int n;                                    /* A counter */
19070    int iCur;                                 /* A cursor number */
19071    SrcList *pSrcList;                        /* FROM clause */
19072    struct SrcCount *pSrcCount;               /* Counting column references */
19073    struct CCurHint *pCCurHint;               /* Used by codeCursorHint() */
19074    int *aiCol;                               /* array of column indexes */
19075    struct IdxCover *pIdxCover;               /* Check for index coverage */
19076    struct IdxExprTrans *pIdxTrans;           /* Convert idxed expr to column */
19077    ExprList *pGroupBy;                       /* GROUP BY clause */
19078    Select *pSelect;                          /* HAVING to WHERE clause ctx */
19079    struct WindowRewrite *pRewrite;           /* Window rewrite context */
19080    struct WhereConst *pConst;                /* WHERE clause constants */
19081    struct RenameCtx *pRename;                /* RENAME COLUMN context */
19082    struct Table *pTab;                       /* Table of generated column */
19083    struct SrcList_item *pSrcItem;            /* A single FROM clause item */
19084  } u;
19085};
19086
19087/* Forward declarations */
19088SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
19089SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
19090SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
19091SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
19092SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
19093SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker*, Expr*);
19094SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker*, Select*);
19095SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker*, Select*);
19096SQLITE_PRIVATE int sqlite3WalkerDepthIncrease(Walker*,Select*);
19097SQLITE_PRIVATE void sqlite3WalkerDepthDecrease(Walker*,Select*);
19098
19099#ifdef SQLITE_DEBUG
19100SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker*, Select*);
19101#endif
19102
19103/*
19104** Return code from the parse-tree walking primitives and their
19105** callbacks.
19106*/
19107#define WRC_Continue    0   /* Continue down into children */
19108#define WRC_Prune       1   /* Omit children but continue walking siblings */
19109#define WRC_Abort       2   /* Abandon the tree walk */
19110
19111/*
19112** An instance of this structure represents a set of one or more CTEs
19113** (common table expressions) created by a single WITH clause.
19114*/
19115struct With {
19116  int nCte;                       /* Number of CTEs in the WITH clause */
19117  With *pOuter;                   /* Containing WITH clause, or NULL */
19118  struct Cte {                    /* For each CTE in the WITH clause.... */
19119    char *zName;                    /* Name of this CTE */
19120    ExprList *pCols;                /* List of explicit column names, or NULL */
19121    Select *pSelect;                /* The definition of this CTE */
19122    const char *zCteErr;            /* Error message for circular references */
19123  } a[1];
19124};
19125
19126#ifdef SQLITE_DEBUG
19127/*
19128** An instance of the TreeView object is used for printing the content of
19129** data structures on sqlite3DebugPrintf() using a tree-like view.
19130*/
19131struct TreeView {
19132  int iLevel;             /* Which level of the tree we are on */
19133  u8  bLine[100];         /* Draw vertical in column i if bLine[i] is true */
19134};
19135#endif /* SQLITE_DEBUG */
19136
19137/*
19138** This object is used in various ways, most (but not all) related to window
19139** functions.
19140**
19141**   (1) A single instance of this structure is attached to the
19142**       the Expr.y.pWin field for each window function in an expression tree.
19143**       This object holds the information contained in the OVER clause,
19144**       plus additional fields used during code generation.
19145**
19146**   (2) All window functions in a single SELECT form a linked-list
19147**       attached to Select.pWin.  The Window.pFunc and Window.pExpr
19148**       fields point back to the expression that is the window function.
19149**
19150**   (3) The terms of the WINDOW clause of a SELECT are instances of this
19151**       object on a linked list attached to Select.pWinDefn.
19152**
19153**   (4) For an aggregate function with a FILTER clause, an instance
19154**       of this object is stored in Expr.y.pWin with eFrmType set to
19155**       TK_FILTER. In this case the only field used is Window.pFilter.
19156**
19157** The uses (1) and (2) are really the same Window object that just happens
19158** to be accessible in two different ways.  Use case (3) are separate objects.
19159*/
19160struct Window {
19161  char *zName;            /* Name of window (may be NULL) */
19162  char *zBase;            /* Name of base window for chaining (may be NULL) */
19163  ExprList *pPartition;   /* PARTITION BY clause */
19164  ExprList *pOrderBy;     /* ORDER BY clause */
19165  u8 eFrmType;            /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
19166  u8 eStart;              /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
19167  u8 eEnd;                /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
19168  u8 bImplicitFrame;      /* True if frame was implicitly specified */
19169  u8 eExclude;            /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
19170  Expr *pStart;           /* Expression for "<expr> PRECEDING" */
19171  Expr *pEnd;             /* Expression for "<expr> FOLLOWING" */
19172  Window **ppThis;        /* Pointer to this object in Select.pWin list */
19173  Window *pNextWin;       /* Next window function belonging to this SELECT */
19174  Expr *pFilter;          /* The FILTER expression */
19175  FuncDef *pFunc;         /* The function */
19176  int iEphCsr;            /* Partition buffer or Peer buffer */
19177  int regAccum;           /* Accumulator */
19178  int regResult;          /* Interim result */
19179  int csrApp;             /* Function cursor (used by min/max) */
19180  int regApp;             /* Function register (also used by min/max) */
19181  int regPart;            /* Array of registers for PARTITION BY values */
19182  Expr *pOwner;           /* Expression object this window is attached to */
19183  int nBufferCol;         /* Number of columns in buffer table */
19184  int iArgCol;            /* Offset of first argument for this function */
19185  int regOne;             /* Register containing constant value 1 */
19186  int regStartRowid;
19187  int regEndRowid;
19188  u8 bExprArgs;           /* Defer evaluation of window function arguments
19189                          ** due to the SQLITE_SUBTYPE flag */
19190};
19191
19192#ifndef SQLITE_OMIT_WINDOWFUNC
19193SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3*, Window*);
19194SQLITE_PRIVATE void sqlite3WindowUnlinkFromSelect(Window*);
19195SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p);
19196SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
19197SQLITE_PRIVATE void sqlite3WindowAttach(Parse*, Expr*, Window*);
19198SQLITE_PRIVATE void sqlite3WindowLink(Select *pSel, Window *pWin);
19199SQLITE_PRIVATE int sqlite3WindowCompare(Parse*, Window*, Window*, int);
19200SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse*, Select*);
19201SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
19202SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*);
19203SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
19204SQLITE_PRIVATE void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
19205SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
19206SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
19207SQLITE_PRIVATE void sqlite3WindowFunctions(void);
19208SQLITE_PRIVATE void sqlite3WindowChain(Parse*, Window*, Window*);
19209SQLITE_PRIVATE Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
19210#else
19211# define sqlite3WindowDelete(a,b)
19212# define sqlite3WindowFunctions()
19213# define sqlite3WindowAttach(a,b,c)
19214#endif
19215
19216/*
19217** Assuming zIn points to the first byte of a UTF-8 character,
19218** advance zIn to point to the first byte of the next UTF-8 character.
19219*/
19220#define SQLITE_SKIP_UTF8(zIn) {                        \
19221  if( (*(zIn++))>=0xc0 ){                              \
19222    while( (*zIn & 0xc0)==0x80 ){ zIn++; }             \
19223  }                                                    \
19224}
19225
19226/*
19227** The SQLITE_*_BKPT macros are substitutes for the error codes with
19228** the same name but without the _BKPT suffix.  These macros invoke
19229** routines that report the line-number on which the error originated
19230** using sqlite3_log().  The routines also provide a convenient place
19231** to set a debugger breakpoint.
19232*/
19233SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType);
19234SQLITE_PRIVATE int sqlite3CorruptError(int);
19235SQLITE_PRIVATE int sqlite3MisuseError(int);
19236SQLITE_PRIVATE int sqlite3CantopenError(int);
19237#define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
19238#define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
19239#define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
19240#ifdef SQLITE_DEBUG
19241SQLITE_PRIVATE   int sqlite3NomemError(int);
19242SQLITE_PRIVATE   int sqlite3IoerrnomemError(int);
19243# define SQLITE_NOMEM_BKPT sqlite3NomemError(__LINE__)
19244# define SQLITE_IOERR_NOMEM_BKPT sqlite3IoerrnomemError(__LINE__)
19245#else
19246# define SQLITE_NOMEM_BKPT SQLITE_NOMEM
19247# define SQLITE_IOERR_NOMEM_BKPT SQLITE_IOERR_NOMEM
19248#endif
19249#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
19250SQLITE_PRIVATE   int sqlite3CorruptPgnoError(int,Pgno);
19251# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptPgnoError(__LINE__,(P))
19252#else
19253# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptError(__LINE__)
19254#endif
19255
19256/*
19257** FTS3 and FTS4 both require virtual table support
19258*/
19259#if defined(SQLITE_OMIT_VIRTUALTABLE)
19260# undef SQLITE_ENABLE_FTS3
19261# undef SQLITE_ENABLE_FTS4
19262#endif
19263
19264/*
19265** FTS4 is really an extension for FTS3.  It is enabled using the
19266** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also call
19267** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
19268*/
19269#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
19270# define SQLITE_ENABLE_FTS3 1
19271#endif
19272
19273/*
19274** The ctype.h header is needed for non-ASCII systems.  It is also
19275** needed by FTS3 when FTS3 is included in the amalgamation.
19276*/
19277#if !defined(SQLITE_ASCII) || \
19278    (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
19279# include <ctype.h>
19280#endif
19281
19282/*
19283** The following macros mimic the standard library functions toupper(),
19284** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
19285** sqlite versions only work for ASCII characters, regardless of locale.
19286*/
19287#ifdef SQLITE_ASCII
19288# define sqlite3Toupper(x)  ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
19289# define sqlite3Isspace(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
19290# define sqlite3Isalnum(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
19291# define sqlite3Isalpha(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
19292# define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
19293# define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
19294# define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
19295# define sqlite3Isquote(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
19296#else
19297# define sqlite3Toupper(x)   toupper((unsigned char)(x))
19298# define sqlite3Isspace(x)   isspace((unsigned char)(x))
19299# define sqlite3Isalnum(x)   isalnum((unsigned char)(x))
19300# define sqlite3Isalpha(x)   isalpha((unsigned char)(x))
19301# define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
19302# define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
19303# define sqlite3Tolower(x)   tolower((unsigned char)(x))
19304# define sqlite3Isquote(x)   ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`')
19305#endif
19306SQLITE_PRIVATE int sqlite3IsIdChar(u8);
19307
19308/*
19309** Internal function prototypes
19310*/
19311SQLITE_PRIVATE int sqlite3StrICmp(const char*,const char*);
19312SQLITE_PRIVATE int sqlite3Strlen30(const char*);
19313#define sqlite3Strlen30NN(C) (strlen(C)&0x3fffffff)
19314SQLITE_PRIVATE char *sqlite3ColumnType(Column*,char*);
19315#define sqlite3StrNICmp sqlite3_strnicmp
19316
19317SQLITE_PRIVATE int sqlite3MallocInit(void);
19318SQLITE_PRIVATE void sqlite3MallocEnd(void);
19319SQLITE_PRIVATE void *sqlite3Malloc(u64);
19320SQLITE_PRIVATE void *sqlite3MallocZero(u64);
19321SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, u64);
19322SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, u64);
19323SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3*, u64);
19324SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
19325SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
19326SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
19327SQLITE_PRIVATE void *sqlite3Realloc(void*, u64);
19328SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
19329SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64);
19330SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
19331SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3*, void*);
19332SQLITE_PRIVATE int sqlite3MallocSize(void*);
19333SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
19334SQLITE_PRIVATE void *sqlite3PageMalloc(int);
19335SQLITE_PRIVATE void sqlite3PageFree(void*);
19336SQLITE_PRIVATE void sqlite3MemSetDefault(void);
19337#ifndef SQLITE_UNTESTABLE
19338SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
19339#endif
19340SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
19341
19342/*
19343** On systems with ample stack space and that support alloca(), make
19344** use of alloca() to obtain space for large automatic objects.  By default,
19345** obtain space from malloc().
19346**
19347** The alloca() routine never returns NULL.  This will cause code paths
19348** that deal with sqlite3StackAlloc() failures to be unreachable.
19349*/
19350#ifdef SQLITE_USE_ALLOCA
19351# define sqlite3StackAllocRaw(D,N)   alloca(N)
19352# define sqlite3StackAllocZero(D,N)  memset(alloca(N), 0, N)
19353# define sqlite3StackFree(D,P)
19354#else
19355# define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
19356# define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
19357# define sqlite3StackFree(D,P)       sqlite3DbFree(D,P)
19358#endif
19359
19360/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together.  If they
19361** are, disable MEMSYS3
19362*/
19363#ifdef SQLITE_ENABLE_MEMSYS5
19364SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
19365#undef SQLITE_ENABLE_MEMSYS3
19366#endif
19367#ifdef SQLITE_ENABLE_MEMSYS3
19368SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
19369#endif
19370
19371
19372#ifndef SQLITE_MUTEX_OMIT
19373SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
19374SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3NoopMutex(void);
19375SQLITE_PRIVATE   sqlite3_mutex *sqlite3MutexAlloc(int);
19376SQLITE_PRIVATE   int sqlite3MutexInit(void);
19377SQLITE_PRIVATE   int sqlite3MutexEnd(void);
19378#endif
19379#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP)
19380SQLITE_PRIVATE   void sqlite3MemoryBarrier(void);
19381#else
19382# define sqlite3MemoryBarrier()
19383#endif
19384
19385SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int);
19386SQLITE_PRIVATE void sqlite3StatusUp(int, int);
19387SQLITE_PRIVATE void sqlite3StatusDown(int, int);
19388SQLITE_PRIVATE void sqlite3StatusHighwater(int, int);
19389SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3*,int*);
19390
19391/* Access to mutexes used by sqlite3_status() */
19392SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void);
19393SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void);
19394
19395#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT)
19396SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex*);
19397#else
19398# define sqlite3MutexWarnOnContention(x)
19399#endif
19400
19401#ifndef SQLITE_OMIT_FLOATING_POINT
19402# define EXP754 (((u64)0x7ff)<<52)
19403# define MAN754 ((((u64)1)<<52)-1)
19404# define IsNaN(X) (((X)&EXP754)==EXP754 && ((X)&MAN754)!=0)
19405SQLITE_PRIVATE   int sqlite3IsNaN(double);
19406#else
19407# define IsNaN(X)         0
19408# define sqlite3IsNaN(X)  0
19409#endif
19410
19411/*
19412** An instance of the following structure holds information about SQL
19413** functions arguments that are the parameters to the printf() function.
19414*/
19415struct PrintfArguments {
19416  int nArg;                /* Total number of arguments */
19417  int nUsed;               /* Number of arguments used so far */
19418  sqlite3_value **apArg;   /* The argument values */
19419};
19420
19421SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
19422SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
19423#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
19424SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
19425#endif
19426#if defined(SQLITE_TEST)
19427SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
19428#endif
19429
19430#if defined(SQLITE_DEBUG)
19431SQLITE_PRIVATE   void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
19432SQLITE_PRIVATE   void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
19433SQLITE_PRIVATE   void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
19434SQLITE_PRIVATE   void sqlite3TreeViewSrcList(TreeView*, const SrcList*);
19435SQLITE_PRIVATE   void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
19436SQLITE_PRIVATE   void sqlite3TreeViewWith(TreeView*, const With*, u8);
19437#ifndef SQLITE_OMIT_WINDOWFUNC
19438SQLITE_PRIVATE   void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
19439SQLITE_PRIVATE   void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
19440#endif
19441#endif
19442
19443
19444SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
19445SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
19446SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3*,int);
19447SQLITE_PRIVATE void sqlite3Dequote(char*);
19448SQLITE_PRIVATE void sqlite3DequoteExpr(Expr*);
19449SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
19450SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
19451SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
19452SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
19453SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
19454SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
19455SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
19456SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
19457SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
19458#ifdef SQLITE_DEBUG
19459SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse*,int,int);
19460#endif
19461SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
19462SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
19463SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
19464SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
19465SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
19466SQLITE_PRIVATE Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*);
19467SQLITE_PRIVATE Expr *sqlite3ExprSimplifiedAndOr(Expr*);
19468SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*, int);
19469SQLITE_PRIVATE void sqlite3ExprFunctionUsable(Parse*,Expr*,FuncDef*);
19470SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
19471SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
19472SQLITE_PRIVATE void sqlite3ExprUnmapAndDelete(Parse*, Expr*);
19473SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
19474SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
19475SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList*,int,int);
19476SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
19477SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
19478SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
19479SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList*);
19480SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index*);
19481SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
19482SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
19483SQLITE_PRIVATE int sqlite3InitOne(sqlite3*, int, char**, u32);
19484SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
19485#ifndef SQLITE_OMIT_VIRTUALTABLE
19486SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
19487#endif
19488SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3*);
19489SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int);
19490SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*);
19491SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
19492SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3*,Table*);
19493SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
19494SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*,char);
19495SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
19496SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
19497SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
19498SQLITE_PRIVATE i16 sqlite3TableColumnToIndex(Index*, i16);
19499#ifdef SQLITE_OMIT_GENERATED_COLUMNS
19500# define sqlite3TableColumnToStorage(T,X) (X)  /* No-op pass-through */
19501# define sqlite3StorageColumnToTable(T,X) (X)  /* No-op pass-through */
19502#else
19503SQLITE_PRIVATE   i16 sqlite3TableColumnToStorage(Table*, i16);
19504SQLITE_PRIVATE   i16 sqlite3StorageColumnToTable(Table*, i16);
19505#endif
19506SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
19507#if SQLITE_ENABLE_HIDDEN_COLUMNS
19508SQLITE_PRIVATE   void sqlite3ColumnPropertiesFromName(Table*, Column*);
19509#else
19510# define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
19511#endif
19512SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*,Token*);
19513SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
19514SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
19515SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
19516SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
19517SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
19518SQLITE_PRIVATE void sqlite3AddGenerated(Parse*,Expr*,Token*);
19519SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
19520SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
19521                    sqlite3_vfs**,char**,char **);
19522#define sqlite3CodecQueryParameters(A,B,C) 0
19523SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
19524
19525#ifdef SQLITE_UNTESTABLE
19526# define sqlite3FaultSim(X) SQLITE_OK
19527#else
19528SQLITE_PRIVATE   int sqlite3FaultSim(int);
19529#endif
19530
19531SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
19532SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
19533SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec*, u32);
19534SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
19535SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
19536SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
19537SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
19538#ifndef SQLITE_UNTESTABLE
19539SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
19540#endif
19541
19542SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*);
19543SQLITE_PRIVATE void sqlite3RowSetDelete(void*);
19544SQLITE_PRIVATE void sqlite3RowSetClear(void*);
19545SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
19546SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64);
19547SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
19548
19549SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int);
19550
19551#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
19552SQLITE_PRIVATE   int sqlite3ViewGetColumnNames(Parse*,Table*);
19553#else
19554# define sqlite3ViewGetColumnNames(A,B) 0
19555#endif
19556
19557#if SQLITE_MAX_ATTACHED>30
19558SQLITE_PRIVATE   int sqlite3DbMaskAllZero(yDbMask);
19559#endif
19560SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
19561SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
19562SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
19563SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3*, Index*);
19564#ifndef SQLITE_OMIT_AUTOINCREMENT
19565SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
19566SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
19567#else
19568# define sqlite3AutoincrementBegin(X)
19569# define sqlite3AutoincrementEnd(X)
19570#endif
19571SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
19572#ifndef SQLITE_OMIT_GENERATED_COLUMNS
19573SQLITE_PRIVATE   void sqlite3ComputeGeneratedColumns(Parse*, int, Table*);
19574#endif
19575SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
19576SQLITE_PRIVATE IdList *sqlite3IdListAppend(Parse*, IdList*, Token*);
19577SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
19578SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(Parse*, SrcList*, int, int);
19579SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(Parse*, SrcList*, Token*, Token*);
19580SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
19581                                      Token*, Select*, Expr*, IdList*);
19582SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
19583SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*);
19584SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
19585SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
19586SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
19587SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
19588SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
19589SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
19590SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
19591                          Expr*, int, int, u8);
19592SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
19593SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
19594SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
19595                         Expr*,ExprList*,u32,Expr*);
19596SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
19597SQLITE_PRIVATE void sqlite3SelectReset(Parse*, Select*);
19598SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
19599SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
19600SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
19601#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
19602SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
19603#endif
19604SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
19605SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
19606                   Upsert*);
19607SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
19608SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
19609SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
19610SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
19611SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
19612SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
19613SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
19614SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
19615SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
19616SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
19617#define ONEPASS_OFF      0        /* Use of ONEPASS not allowed */
19618#define ONEPASS_SINGLE   1        /* ONEPASS valid for a single row update */
19619#define ONEPASS_MULTI    2        /* ONEPASS is valid for multiple rows */
19620SQLITE_PRIVATE int sqlite3WhereUsesDeferredSeek(WhereInfo*);
19621SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
19622SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
19623SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
19624SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
19625SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
19626#ifndef SQLITE_OMIT_GENERATED_COLUMNS
19627SQLITE_PRIVATE void sqlite3ExprCodeGeneratedColumn(Parse*, Column*, int);
19628#endif
19629SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
19630SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
19631SQLITE_PRIVATE int sqlite3ExprCodeRunJustOnce(Parse*, Expr*, int);
19632SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
19633SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
19634SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
19635#define SQLITE_ECEL_DUP      0x01  /* Deep, not shallow copies */
19636#define SQLITE_ECEL_FACTOR   0x02  /* Factor out constant terms */
19637#define SQLITE_ECEL_REF      0x04  /* Use ExprList.u.x.iOrderByCol */
19638#define SQLITE_ECEL_OMITREF  0x08  /* Omit if ExprList.u.x.iOrderByCol */
19639SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
19640SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
19641SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
19642SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
19643#define LOCATE_VIEW    0x01
19644#define LOCATE_NOERR   0x02
19645SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
19646SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,u32 flags,struct SrcList_item *);
19647SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
19648SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
19649SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
19650SQLITE_PRIVATE void sqlite3Vacuum(Parse*,Token*,Expr*);
19651SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*, int, sqlite3_value*);
19652SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
19653SQLITE_PRIVATE int sqlite3ExprCompare(Parse*,Expr*, Expr*, int);
19654SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr*, Expr*, int);
19655SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
19656SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Parse*,Expr*, Expr*, int);
19657SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr*,int);
19658SQLITE_PRIVATE void sqlite3AggInfoPersistWalkerInit(Walker*,Parse*);
19659SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
19660SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
19661SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
19662SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
19663SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
19664#ifndef SQLITE_UNTESTABLE
19665SQLITE_PRIVATE void sqlite3PrngSaveState(void);
19666SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
19667#endif
19668SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
19669SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
19670SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
19671SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
19672SQLITE_PRIVATE void sqlite3EndTransaction(Parse*,int);
19673SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
19674SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
19675SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
19676SQLITE_PRIVATE u32 sqlite3IsTrueOrFalse(const char*);
19677SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr*);
19678SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr*);
19679SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
19680SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
19681SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
19682SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
19683SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
19684#ifdef SQLITE_ENABLE_CURSOR_HINTS
19685SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
19686#endif
19687SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
19688SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
19689SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
19690SQLITE_PRIVATE int sqlite3IsRowid(const char*);
19691SQLITE_PRIVATE void sqlite3GenerateRowDelete(
19692    Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
19693SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
19694SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
19695SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
19696SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
19697SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
19698                                     u8,u8,int,int*,int*,Upsert*);
19699#ifdef SQLITE_ENABLE_NULL_TRIM
19700SQLITE_PRIVATE   void sqlite3SetMakeRecordP5(Vdbe*,Table*);
19701#else
19702# define sqlite3SetMakeRecordP5(A,B)
19703#endif
19704SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
19705SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
19706SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
19707SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
19708SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
19709SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
19710SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
19711SQLITE_PRIVATE void sqlite3RowidConstraint(Parse*, int, Table*);
19712SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
19713SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
19714SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
19715SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
19716SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
19717SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(int,const char*);
19718SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
19719SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
19720SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
19721SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
19722SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
19723SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
19724SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
19725SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
19726
19727#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
19728SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
19729#endif
19730
19731#ifndef SQLITE_OMIT_TRIGGER
19732SQLITE_PRIVATE   void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
19733                           Expr*,int, int);
19734SQLITE_PRIVATE   void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
19735SQLITE_PRIVATE   void sqlite3DropTrigger(Parse*, SrcList*, int);
19736SQLITE_PRIVATE   void sqlite3DropTriggerPtr(Parse*, Trigger*);
19737SQLITE_PRIVATE   Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
19738SQLITE_PRIVATE   Trigger *sqlite3TriggerList(Parse *, Table *);
19739SQLITE_PRIVATE   void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
19740                            int, int, int);
19741SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
19742  void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
19743SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
19744SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
19745                                        const char*,const char*);
19746SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*,
19747                                        Select*,u8,Upsert*,
19748                                        const char*,const char*);
19749SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,ExprList*, Expr*, u8,
19750                                        const char*,const char*);
19751SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*,
19752                                        const char*,const char*);
19753SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
19754SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
19755SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
19756# define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
19757# define sqlite3IsToplevel(p) ((p)->pToplevel==0)
19758#else
19759# define sqlite3TriggersExist(B,C,D,E,F) 0
19760# define sqlite3DeleteTrigger(A,B)
19761# define sqlite3DropTriggerPtr(A,B)
19762# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
19763# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
19764# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
19765# define sqlite3TriggerList(X, Y) 0
19766# define sqlite3ParseToplevel(p) p
19767# define sqlite3IsToplevel(p) 1
19768# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
19769#endif
19770
19771SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
19772SQLITE_PRIVATE void sqlite3SetJoinExpr(Expr*,int);
19773SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
19774SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
19775#ifndef SQLITE_OMIT_AUTHORIZATION
19776SQLITE_PRIVATE   void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
19777SQLITE_PRIVATE   int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
19778SQLITE_PRIVATE   void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
19779SQLITE_PRIVATE   void sqlite3AuthContextPop(AuthContext*);
19780SQLITE_PRIVATE   int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
19781#else
19782# define sqlite3AuthRead(a,b,c,d)
19783# define sqlite3AuthCheck(a,b,c,d,e)    SQLITE_OK
19784# define sqlite3AuthContextPush(a,b,c)
19785# define sqlite3AuthContextPop(a)  ((void)(a))
19786#endif
19787SQLITE_PRIVATE int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName);
19788SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
19789SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
19790SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
19791SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
19792SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
19793SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
19794SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
19795SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
19796SQLITE_PRIVATE int sqlite3RealSameAsInt(double,sqlite3_int64);
19797SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
19798SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
19799SQLITE_PRIVATE int sqlite3Atoi(const char*);
19800#ifndef SQLITE_OMIT_UTF16
19801SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
19802#endif
19803SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
19804SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
19805SQLITE_PRIVATE LogEst sqlite3LogEst(u64);
19806SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst);
19807#ifndef SQLITE_OMIT_VIRTUALTABLE
19808SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double);
19809#endif
19810#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
19811    defined(SQLITE_ENABLE_STAT4) || \
19812    defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
19813SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst);
19814#endif
19815SQLITE_PRIVATE VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
19816SQLITE_PRIVATE const char *sqlite3VListNumToName(VList*,int);
19817SQLITE_PRIVATE int sqlite3VListNameToNum(VList*,const char*,int);
19818
19819/*
19820** Routines to read and write variable-length integers.  These used to
19821** be defined locally, but now we use the varint routines in the util.c
19822** file.
19823*/
19824SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
19825SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
19826SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
19827SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
19828
19829/*
19830** The common case is for a varint to be a single byte.  They following
19831** macros handle the common case without a procedure call, but then call
19832** the procedure for larger varints.
19833*/
19834#define getVarint32(A,B)  \
19835  (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
19836#define getVarint32NR(A,B) \
19837  B=(u32)*(A);if(B>=0x80)sqlite3GetVarint32((A),(u32*)&(B))
19838#define putVarint32(A,B)  \
19839  (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
19840  sqlite3PutVarint((A),(B)))
19841#define getVarint    sqlite3GetVarint
19842#define putVarint    sqlite3PutVarint
19843
19844
19845SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
19846SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
19847SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2);
19848SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);
19849SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table*,int);
19850SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr);
19851SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
19852SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
19853SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
19854SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
19855SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int);
19856SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
19857SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
19858SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
19859
19860#if defined(SQLITE_NEED_ERR_NAME)
19861SQLITE_PRIVATE const char *sqlite3ErrName(int);
19862#endif
19863
19864#ifdef SQLITE_ENABLE_DESERIALIZE
19865SQLITE_PRIVATE int sqlite3MemdbInit(void);
19866#endif
19867
19868SQLITE_PRIVATE const char *sqlite3ErrStr(int);
19869SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
19870SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
19871SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq*);
19872SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
19873SQLITE_PRIVATE void sqlite3SetTextEncoding(sqlite3 *db, u8);
19874SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr);
19875SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr);
19876SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,const Expr*,const Expr*);
19877SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
19878SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
19879SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
19880SQLITE_PRIVATE Expr *sqlite3ExprSkipCollateAndLikely(Expr*);
19881SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
19882SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3*);
19883SQLITE_PRIVATE int sqlite3CheckObjectName(Parse*, const char*,const char*,const char*);
19884SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
19885SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
19886SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
19887SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
19888SQLITE_PRIVATE int sqlite3AbsInt32(int);
19889#ifdef SQLITE_ENABLE_8_3_NAMES
19890SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
19891#else
19892# define sqlite3FileSuffix3(X,Y)
19893#endif
19894SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,u8);
19895
19896SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
19897SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
19898SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
19899                        void(*)(void*));
19900SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*);
19901SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
19902#ifndef SQLITE_UNTESTABLE
19903SQLITE_PRIVATE void sqlite3ResultIntReal(sqlite3_context*);
19904#endif
19905SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
19906#ifndef SQLITE_OMIT_UTF16
19907SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
19908#endif
19909SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
19910SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
19911#ifndef SQLITE_AMALGAMATION
19912SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
19913SQLITE_PRIVATE const char sqlite3StrBINARY[];
19914SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
19915SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
19916SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
19917SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
19918#ifndef SQLITE_OMIT_WSD
19919SQLITE_PRIVATE int sqlite3PendingByte;
19920#endif
19921#endif
19922#ifdef VDBE_PROFILE
19923SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt;
19924#endif
19925SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
19926SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
19927SQLITE_PRIVATE void sqlite3AlterFunctions(void);
19928SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
19929SQLITE_PRIVATE void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
19930SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
19931SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
19932SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
19933SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
19934SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr*);
19935SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
19936SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
19937SQLITE_PRIVATE int sqlite3MatchEName(
19938  const struct ExprList_item*,
19939  const char*,
19940  const char*,
19941  const char*
19942);
19943SQLITE_PRIVATE Bitmask sqlite3ExprColUsed(Expr*);
19944SQLITE_PRIVATE u8 sqlite3StrIHash(const char*);
19945SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
19946SQLITE_PRIVATE int sqlite3ResolveExprListNames(NameContext*, ExprList*);
19947SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
19948SQLITE_PRIVATE int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
19949SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
19950SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
19951SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
19952SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
19953SQLITE_PRIVATE void *sqlite3RenameTokenMap(Parse*, void*, Token*);
19954SQLITE_PRIVATE void sqlite3RenameTokenRemap(Parse*, void *pTo, void *pFrom);
19955SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse*, Expr*);
19956SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse*, ExprList*);
19957SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
19958SQLITE_PRIVATE char sqlite3AffinityType(const char*, Column*);
19959SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
19960SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
19961SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
19962SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
19963SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
19964SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
19965SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
19966SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
19967SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
19968SQLITE_PRIVATE void sqlite3SchemaClear(void *);
19969SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
19970SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
19971SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
19972SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo*);
19973SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
19974SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
19975SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
19976SQLITE_PRIVATE int sqlite3HasExplicitNulls(Parse*, ExprList*);
19977
19978#ifdef SQLITE_DEBUG
19979SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*);
19980#endif
19981SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
19982  void (*)(sqlite3_context*,int,sqlite3_value **),
19983  void (*)(sqlite3_context*,int,sqlite3_value **),
19984  void (*)(sqlite3_context*),
19985  void (*)(sqlite3_context*),
19986  void (*)(sqlite3_context*,int,sqlite3_value **),
19987  FuncDestructor *pDestructor
19988);
19989SQLITE_PRIVATE void sqlite3NoopDestructor(void*);
19990SQLITE_PRIVATE void sqlite3OomFault(sqlite3*);
19991SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
19992SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
19993SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
19994
19995SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
19996SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
19997SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
19998SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
19999
20000SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
20001SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
20002
20003#ifndef SQLITE_OMIT_SUBQUERY
20004SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse*, Expr*);
20005#else
20006# define sqlite3ExprCheckIN(x,y) SQLITE_OK
20007#endif
20008
20009#ifdef SQLITE_ENABLE_STAT4
20010SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
20011    Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
20012SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
20013SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
20014SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
20015SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
20016#endif
20017
20018/*
20019** The interface to the LEMON-generated parser
20020*/
20021#ifndef SQLITE_AMALGAMATION
20022SQLITE_PRIVATE   void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
20023SQLITE_PRIVATE   void sqlite3ParserFree(void*, void(*)(void*));
20024#endif
20025SQLITE_PRIVATE void sqlite3Parser(void*, int, Token);
20026SQLITE_PRIVATE int sqlite3ParserFallback(int);
20027#ifdef YYTRACKMAXSTACKDEPTH
20028SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
20029#endif
20030
20031SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
20032#ifndef SQLITE_OMIT_LOAD_EXTENSION
20033SQLITE_PRIVATE   void sqlite3CloseExtensions(sqlite3*);
20034#else
20035# define sqlite3CloseExtensions(X)
20036#endif
20037
20038#ifndef SQLITE_OMIT_SHARED_CACHE
20039SQLITE_PRIVATE   void sqlite3TableLock(Parse *, int, int, u8, const char *);
20040#else
20041  #define sqlite3TableLock(v,w,x,y,z)
20042#endif
20043
20044#ifdef SQLITE_TEST
20045SQLITE_PRIVATE   int sqlite3Utf8To8(unsigned char*);
20046#endif
20047
20048#ifdef SQLITE_OMIT_VIRTUALTABLE
20049#  define sqlite3VtabClear(Y)
20050#  define sqlite3VtabSync(X,Y) SQLITE_OK
20051#  define sqlite3VtabRollback(X)
20052#  define sqlite3VtabCommit(X)
20053#  define sqlite3VtabInSync(db) 0
20054#  define sqlite3VtabLock(X)
20055#  define sqlite3VtabUnlock(X)
20056#  define sqlite3VtabModuleUnref(D,X)
20057#  define sqlite3VtabUnlockList(X)
20058#  define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
20059#  define sqlite3GetVTable(X,Y)  ((VTable*)0)
20060#else
20061SQLITE_PRIVATE    void sqlite3VtabClear(sqlite3 *db, Table*);
20062SQLITE_PRIVATE    void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
20063SQLITE_PRIVATE    int sqlite3VtabSync(sqlite3 *db, Vdbe*);
20064SQLITE_PRIVATE    int sqlite3VtabRollback(sqlite3 *db);
20065SQLITE_PRIVATE    int sqlite3VtabCommit(sqlite3 *db);
20066SQLITE_PRIVATE    void sqlite3VtabLock(VTable *);
20067SQLITE_PRIVATE    void sqlite3VtabUnlock(VTable *);
20068SQLITE_PRIVATE    void sqlite3VtabModuleUnref(sqlite3*,Module*);
20069SQLITE_PRIVATE    void sqlite3VtabUnlockList(sqlite3*);
20070SQLITE_PRIVATE    int sqlite3VtabSavepoint(sqlite3 *, int, int);
20071SQLITE_PRIVATE    void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
20072SQLITE_PRIVATE    VTable *sqlite3GetVTable(sqlite3*, Table*);
20073SQLITE_PRIVATE    Module *sqlite3VtabCreateModule(
20074     sqlite3*,
20075     const char*,
20076     const sqlite3_module*,
20077     void*,
20078     void(*)(void*)
20079   );
20080#  define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
20081#endif
20082SQLITE_PRIVATE int sqlite3ReadOnlyShadowTables(sqlite3 *db);
20083#ifndef SQLITE_OMIT_VIRTUALTABLE
20084SQLITE_PRIVATE   int sqlite3ShadowTableName(sqlite3 *db, const char *zName);
20085SQLITE_PRIVATE   int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*);
20086#else
20087# define sqlite3ShadowTableName(A,B) 0
20088# define sqlite3IsShadowTableOf(A,B,C) 0
20089#endif
20090SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse*,Module*);
20091SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3*,Module*);
20092SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
20093SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
20094SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
20095SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
20096SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
20097SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
20098SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
20099SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
20100SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
20101SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
20102SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
20103SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
20104SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
20105SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
20106#ifdef SQLITE_ENABLE_NORMALIZE
20107SQLITE_PRIVATE char *sqlite3Normalize(Vdbe*, const char*);
20108#endif
20109SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
20110SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
20111SQLITE_PRIVATE CollSeq *sqlite3ExprCompareCollSeq(Parse*,const Expr*);
20112SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, const Expr*, const Expr*);
20113SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
20114SQLITE_PRIVATE const char *sqlite3JournalModename(int);
20115#ifndef SQLITE_OMIT_WAL
20116SQLITE_PRIVATE   int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
20117SQLITE_PRIVATE   int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
20118#endif
20119#ifndef SQLITE_OMIT_CTE
20120SQLITE_PRIVATE   With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
20121SQLITE_PRIVATE   void sqlite3WithDelete(sqlite3*,With*);
20122SQLITE_PRIVATE   void sqlite3WithPush(Parse*, With*, u8);
20123#else
20124#define sqlite3WithPush(x,y,z)
20125#define sqlite3WithDelete(x,y)
20126#endif
20127#ifndef SQLITE_OMIT_UPSERT
20128SQLITE_PRIVATE   Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*);
20129SQLITE_PRIVATE   void sqlite3UpsertDelete(sqlite3*,Upsert*);
20130SQLITE_PRIVATE   Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
20131SQLITE_PRIVATE   int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
20132SQLITE_PRIVATE   void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
20133#else
20134#define sqlite3UpsertNew(v,w,x,y,z) ((Upsert*)0)
20135#define sqlite3UpsertDelete(x,y)
20136#define sqlite3UpsertDup(x,y)       ((Upsert*)0)
20137#endif
20138
20139
20140/* Declarations for functions in fkey.c. All of these are replaced by
20141** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
20142** key functionality is available. If OMIT_TRIGGER is defined but
20143** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
20144** this case foreign keys are parsed, but no other functionality is
20145** provided (enforcement of FK constraints requires the triggers sub-system).
20146*/
20147#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
20148SQLITE_PRIVATE   void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
20149SQLITE_PRIVATE   void sqlite3FkDropTable(Parse*, SrcList *, Table*);
20150SQLITE_PRIVATE   void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
20151SQLITE_PRIVATE   int sqlite3FkRequired(Parse*, Table*, int*, int);
20152SQLITE_PRIVATE   u32 sqlite3FkOldmask(Parse*, Table*);
20153SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
20154#else
20155  #define sqlite3FkActions(a,b,c,d,e,f)
20156  #define sqlite3FkCheck(a,b,c,d,e,f)
20157  #define sqlite3FkDropTable(a,b,c)
20158  #define sqlite3FkOldmask(a,b)         0
20159  #define sqlite3FkRequired(a,b,c,d)    0
20160  #define sqlite3FkReferences(a)        0
20161#endif
20162#ifndef SQLITE_OMIT_FOREIGN_KEY
20163SQLITE_PRIVATE   void sqlite3FkDelete(sqlite3 *, Table*);
20164SQLITE_PRIVATE   int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
20165#else
20166  #define sqlite3FkDelete(a,b)
20167  #define sqlite3FkLocateIndex(a,b,c,d,e)
20168#endif
20169
20170
20171/*
20172** Available fault injectors.  Should be numbered beginning with 0.
20173*/
20174#define SQLITE_FAULTINJECTOR_MALLOC     0
20175#define SQLITE_FAULTINJECTOR_COUNT      1
20176
20177/*
20178** The interface to the code in fault.c used for identifying "benign"
20179** malloc failures. This is only present if SQLITE_UNTESTABLE
20180** is not defined.
20181*/
20182#ifndef SQLITE_UNTESTABLE
20183SQLITE_PRIVATE   void sqlite3BeginBenignMalloc(void);
20184SQLITE_PRIVATE   void sqlite3EndBenignMalloc(void);
20185#else
20186  #define sqlite3BeginBenignMalloc()
20187  #define sqlite3EndBenignMalloc()
20188#endif
20189
20190/*
20191** Allowed return values from sqlite3FindInIndex()
20192*/
20193#define IN_INDEX_ROWID        1   /* Search the rowid of the table */
20194#define IN_INDEX_EPH          2   /* Search an ephemeral b-tree */
20195#define IN_INDEX_INDEX_ASC    3   /* Existing index ASCENDING */
20196#define IN_INDEX_INDEX_DESC   4   /* Existing index DESCENDING */
20197#define IN_INDEX_NOOP         5   /* No table available. Use comparisons */
20198/*
20199** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
20200*/
20201#define IN_INDEX_NOOP_OK     0x0001  /* OK to return IN_INDEX_NOOP */
20202#define IN_INDEX_MEMBERSHIP  0x0002  /* IN operator used for membership test */
20203#define IN_INDEX_LOOP        0x0004  /* IN operator used as a loop */
20204SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*, int*);
20205
20206SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
20207SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
20208#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
20209 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
20210SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
20211#endif
20212
20213SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p);
20214SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
20215
20216SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
20217#if SQLITE_MAX_EXPR_DEPTH>0
20218SQLITE_PRIVATE   int sqlite3SelectExprHeight(Select *);
20219SQLITE_PRIVATE   int sqlite3ExprCheckHeight(Parse*, int);
20220#else
20221  #define sqlite3SelectExprHeight(x) 0
20222  #define sqlite3ExprCheckHeight(x,y)
20223#endif
20224
20225SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
20226SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
20227
20228#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
20229SQLITE_PRIVATE   void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
20230SQLITE_PRIVATE   void sqlite3ConnectionUnlocked(sqlite3 *db);
20231SQLITE_PRIVATE   void sqlite3ConnectionClosed(sqlite3 *db);
20232#else
20233  #define sqlite3ConnectionBlocked(x,y)
20234  #define sqlite3ConnectionUnlocked(x)
20235  #define sqlite3ConnectionClosed(x)
20236#endif
20237
20238#ifdef SQLITE_DEBUG
20239SQLITE_PRIVATE   void sqlite3ParserTrace(FILE*, char *);
20240#endif
20241#if defined(YYCOVERAGE)
20242SQLITE_PRIVATE   int sqlite3ParserCoverage(FILE*);
20243#endif
20244
20245/*
20246** If the SQLITE_ENABLE IOTRACE exists then the global variable
20247** sqlite3IoTrace is a pointer to a printf-like routine used to
20248** print I/O tracing messages.
20249*/
20250#ifdef SQLITE_ENABLE_IOTRACE
20251# define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
20252SQLITE_PRIVATE   void sqlite3VdbeIOTraceSql(Vdbe*);
20253SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
20254#else
20255# define IOTRACE(A)
20256# define sqlite3VdbeIOTraceSql(X)
20257#endif
20258
20259/*
20260** These routines are available for the mem2.c debugging memory allocator
20261** only.  They are used to verify that different "types" of memory
20262** allocations are properly tracked by the system.
20263**
20264** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
20265** the MEMTYPE_* macros defined below.  The type must be a bitmask with
20266** a single bit set.
20267**
20268** sqlite3MemdebugHasType() returns true if any of the bits in its second
20269** argument match the type set by the previous sqlite3MemdebugSetType().
20270** sqlite3MemdebugHasType() is intended for use inside assert() statements.
20271**
20272** sqlite3MemdebugNoType() returns true if none of the bits in its second
20273** argument match the type set by the previous sqlite3MemdebugSetType().
20274**
20275** Perhaps the most important point is the difference between MEMTYPE_HEAP
20276** and MEMTYPE_LOOKASIDE.  If an allocation is MEMTYPE_LOOKASIDE, that means
20277** it might have been allocated by lookaside, except the allocation was
20278** too large or lookaside was already full.  It is important to verify
20279** that allocations that might have been satisfied by lookaside are not
20280** passed back to non-lookaside free() routines.  Asserts such as the
20281** example above are placed on the non-lookaside free() routines to verify
20282** this constraint.
20283**
20284** All of this is no-op for a production build.  It only comes into
20285** play when the SQLITE_MEMDEBUG compile-time option is used.
20286*/
20287#ifdef SQLITE_MEMDEBUG
20288SQLITE_PRIVATE   void sqlite3MemdebugSetType(void*,u8);
20289SQLITE_PRIVATE   int sqlite3MemdebugHasType(void*,u8);
20290SQLITE_PRIVATE   int sqlite3MemdebugNoType(void*,u8);
20291#else
20292# define sqlite3MemdebugSetType(X,Y)  /* no-op */
20293# define sqlite3MemdebugHasType(X,Y)  1
20294# define sqlite3MemdebugNoType(X,Y)   1
20295#endif
20296#define MEMTYPE_HEAP       0x01  /* General heap allocations */
20297#define MEMTYPE_LOOKASIDE  0x02  /* Heap that might have been lookaside */
20298#define MEMTYPE_PCACHE     0x04  /* Page cache allocations */
20299
20300/*
20301** Threading interface
20302*/
20303#if SQLITE_MAX_WORKER_THREADS>0
20304SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
20305SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**);
20306#endif
20307
20308#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)
20309SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3*);
20310#endif
20311#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
20312SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*);
20313#endif
20314
20315SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr);
20316SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr);
20317SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr*, int);
20318SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(Parse*,Expr*,int);
20319SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse*, Expr*);
20320
20321#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
20322SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt);
20323#endif
20324
20325#endif /* SQLITEINT_H */
20326
20327/************** End of sqliteInt.h *******************************************/
20328/************** Begin file global.c ******************************************/
20329/*
20330** 2008 June 13
20331**
20332** The author disclaims copyright to this source code.  In place of
20333** a legal notice, here is a blessing:
20334**
20335**    May you do good and not evil.
20336**    May you find forgiveness for yourself and forgive others.
20337**    May you share freely, never taking more than you give.
20338**
20339*************************************************************************
20340**
20341** This file contains definitions of global variables and constants.
20342*/
20343/* #include "sqliteInt.h" */
20344
20345/* An array to map all upper-case characters into their corresponding
20346** lower-case character.
20347**
20348** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not
20349** handle case conversions for the UTF character set since the tables
20350** involved are nearly as big or bigger than SQLite itself.
20351*/
20352SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
20353#ifdef SQLITE_ASCII
20354      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
20355     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
20356     36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
20357     54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
20358    104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
20359    122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
20360    108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
20361    126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
20362    144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
20363    162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
20364    180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
20365    198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
20366    216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
20367    234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
20368    252,253,254,255
20369#endif
20370#ifdef SQLITE_EBCDIC
20371      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
20372     16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
20373     32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
20374     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
20375     64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
20376     80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
20377     96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, /* 6x */
20378    112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, /* 7x */
20379    128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
20380    144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, /* 9x */
20381    160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
20382    176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
20383    192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
20384    208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
20385    224,225,162,163,164,165,166,167,168,169,234,235,236,237,238,239, /* Ex */
20386    240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, /* Fx */
20387#endif
20388};
20389
20390/*
20391** The following 256 byte lookup table is used to support SQLites built-in
20392** equivalents to the following standard library functions:
20393**
20394**   isspace()                        0x01
20395**   isalpha()                        0x02
20396**   isdigit()                        0x04
20397**   isalnum()                        0x06
20398**   isxdigit()                       0x08
20399**   toupper()                        0x20
20400**   SQLite identifier character      0x40
20401**   Quote character                  0x80
20402**
20403** Bit 0x20 is set if the mapped character requires translation to upper
20404** case. i.e. if the character is a lower-case ASCII character.
20405** If x is a lower-case ASCII character, then its upper-case equivalent
20406** is (x - 0x20). Therefore toupper() can be implemented as:
20407**
20408**   (x & ~(map[x]&0x20))
20409**
20410** The equivalent of tolower() is implemented using the sqlite3UpperToLower[]
20411** array. tolower() is used more often than toupper() by SQLite.
20412**
20413** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
20414** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
20415** non-ASCII UTF character. Hence the test for whether or not a character is
20416** part of an identifier is 0x46.
20417*/
20418SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
20419  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
20420  0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
20421  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
20422  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
20423  0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80,  /* 20..27     !"#$%&' */
20424  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
20425  0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
20426  0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
20427
20428  0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
20429  0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
20430  0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
20431  0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
20432  0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
20433  0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
20434  0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
20435  0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
20436
20437  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 80..87    ........ */
20438  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 88..8f    ........ */
20439  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 90..97    ........ */
20440  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 98..9f    ........ */
20441  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a0..a7    ........ */
20442  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a8..af    ........ */
20443  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b0..b7    ........ */
20444  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b8..bf    ........ */
20445
20446  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c0..c7    ........ */
20447  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c8..cf    ........ */
20448  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d0..d7    ........ */
20449  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d8..df    ........ */
20450  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
20451  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
20452  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
20453  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
20454};
20455
20456/* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
20457** compatibility for legacy applications, the URI filename capability is
20458** disabled by default.
20459**
20460** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
20461** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
20462**
20463** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
20464** disabled. The default value may be changed by compiling with the
20465** SQLITE_USE_URI symbol defined.
20466*/
20467#ifndef SQLITE_USE_URI
20468# define SQLITE_USE_URI 0
20469#endif
20470
20471/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
20472** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
20473** that compile-time option is omitted.
20474*/
20475#if !defined(SQLITE_ALLOW_COVERING_INDEX_SCAN)
20476# define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
20477#else
20478# if !SQLITE_ALLOW_COVERING_INDEX_SCAN
20479#   error "Compile-time disabling of covering index scan using the\
20480 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
20481 Contact SQLite developers if this is a problem for you, and\
20482 delete this #error macro to continue with your build."
20483# endif
20484#endif
20485
20486/* The minimum PMA size is set to this value multiplied by the database
20487** page size in bytes.
20488*/
20489#ifndef SQLITE_SORTER_PMASZ
20490# define SQLITE_SORTER_PMASZ 250
20491#endif
20492
20493/* Statement journals spill to disk when their size exceeds the following
20494** threshold (in bytes). 0 means that statement journals are created and
20495** written to disk immediately (the default behavior for SQLite versions
20496** before 3.12.0).  -1 means always keep the entire statement journal in
20497** memory.  (The statement journal is also always held entirely in memory
20498** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
20499** setting.)
20500*/
20501#ifndef SQLITE_STMTJRNL_SPILL
20502# define SQLITE_STMTJRNL_SPILL (64*1024)
20503#endif
20504
20505/*
20506** The default lookaside-configuration, the format "SZ,N".  SZ is the
20507** number of bytes in each lookaside slot (should be a multiple of 8)
20508** and N is the number of slots.  The lookaside-configuration can be
20509** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
20510** or at run-time for an individual database connection using
20511** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
20512**
20513** With the two-size-lookaside enhancement, less lookaside is required.
20514** The default configuration of 1200,40 actually provides 30 1200-byte slots
20515** and 93 128-byte slots, which is more lookaside than is available
20516** using the older 1200,100 configuration without two-size-lookaside.
20517*/
20518#ifndef SQLITE_DEFAULT_LOOKASIDE
20519# ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
20520#   define SQLITE_DEFAULT_LOOKASIDE 1200,100  /* 120KB of memory */
20521# else
20522#   define SQLITE_DEFAULT_LOOKASIDE 1200,40   /* 48KB of memory */
20523# endif
20524#endif
20525
20526
20527/* The default maximum size of an in-memory database created using
20528** sqlite3_deserialize()
20529*/
20530#ifndef SQLITE_MEMDB_DEFAULT_MAXSIZE
20531# define SQLITE_MEMDB_DEFAULT_MAXSIZE 1073741824
20532#endif
20533
20534/*
20535** The following singleton contains the global configuration for
20536** the SQLite library.
20537*/
20538SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
20539   SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
20540   1,                         /* bCoreMutex */
20541   SQLITE_THREADSAFE==1,      /* bFullMutex */
20542   SQLITE_USE_URI,            /* bOpenUri */
20543   SQLITE_ALLOW_COVERING_INDEX_SCAN,   /* bUseCis */
20544   0,                         /* bSmallMalloc */
20545   1,                         /* bExtraSchemaChecks */
20546   0x7ffffffe,                /* mxStrlen */
20547   0,                         /* neverCorrupt */
20548   SQLITE_DEFAULT_LOOKASIDE,  /* szLookaside, nLookaside */
20549   SQLITE_STMTJRNL_SPILL,     /* nStmtSpill */
20550   {0,0,0,0,0,0,0,0},         /* m */
20551   {0,0,0,0,0,0,0,0,0},       /* mutex */
20552   {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
20553   (void*)0,                  /* pHeap */
20554   0,                         /* nHeap */
20555   0, 0,                      /* mnHeap, mxHeap */
20556   SQLITE_DEFAULT_MMAP_SIZE,  /* szMmap */
20557   SQLITE_MAX_MMAP_SIZE,      /* mxMmap */
20558   (void*)0,                  /* pPage */
20559   0,                         /* szPage */
20560   SQLITE_DEFAULT_PCACHE_INITSZ, /* nPage */
20561   0,                         /* mxParserStack */
20562   0,                         /* sharedCacheEnabled */
20563   SQLITE_SORTER_PMASZ,       /* szPma */
20564   /* All the rest should always be initialized to zero */
20565   0,                         /* isInit */
20566   0,                         /* inProgress */
20567   0,                         /* isMutexInit */
20568   0,                         /* isMallocInit */
20569   0,                         /* isPCacheInit */
20570   0,                         /* nRefInitMutex */
20571   0,                         /* pInitMutex */
20572   0,                         /* xLog */
20573   0,                         /* pLogArg */
20574#ifdef SQLITE_ENABLE_SQLLOG
20575   0,                         /* xSqllog */
20576   0,                         /* pSqllogArg */
20577#endif
20578#ifdef SQLITE_VDBE_COVERAGE
20579   0,                         /* xVdbeBranch */
20580   0,                         /* pVbeBranchArg */
20581#endif
20582#ifdef SQLITE_ENABLE_DESERIALIZE
20583   SQLITE_MEMDB_DEFAULT_MAXSIZE,   /* mxMemdbSize */
20584#endif
20585#ifndef SQLITE_UNTESTABLE
20586   0,                         /* xTestCallback */
20587#endif
20588   0,                         /* bLocaltimeFault */
20589   0x7ffffffe,                /* iOnceResetThreshold */
20590   SQLITE_DEFAULT_SORTERREF_SIZE,   /* szSorterRef */
20591   0,                         /* iPrngSeed */
20592};
20593
20594/*
20595** Hash table for global functions - functions common to all
20596** database connections.  After initialization, this table is
20597** read-only.
20598*/
20599SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
20600
20601#ifdef VDBE_PROFILE
20602/*
20603** The following performance counter can be used in place of
20604** sqlite3Hwtime() for profiling.  This is a no-op on standard builds.
20605*/
20606SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt = 0;
20607#endif
20608
20609/*
20610** The value of the "pending" byte must be 0x40000000 (1 byte past the
20611** 1-gibabyte boundary) in a compatible database.  SQLite never uses
20612** the database page that contains the pending byte.  It never attempts
20613** to read or write that page.  The pending byte page is set aside
20614** for use by the VFS layers as space for managing file locks.
20615**
20616** During testing, it is often desirable to move the pending byte to
20617** a different position in the file.  This allows code that has to
20618** deal with the pending byte to run on files that are much smaller
20619** than 1 GiB.  The sqlite3_test_control() interface can be used to
20620** move the pending byte.
20621**
20622** IMPORTANT:  Changing the pending byte to any value other than
20623** 0x40000000 results in an incompatible database file format!
20624** Changing the pending byte during operation will result in undefined
20625** and incorrect behavior.
20626*/
20627#ifndef SQLITE_OMIT_WSD
20628SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
20629#endif
20630
20631/* #include "opcodes.h" */
20632/*
20633** Properties of opcodes.  The OPFLG_INITIALIZER macro is
20634** created by mkopcodeh.awk during compilation.  Data is obtained
20635** from the comments following the "case OP_xxxx:" statements in
20636** the vdbe.c file.
20637*/
20638SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
20639
20640/*
20641** Name of the default collating sequence
20642*/
20643SQLITE_PRIVATE const char sqlite3StrBINARY[] = "BINARY";
20644
20645/************** End of global.c **********************************************/
20646/************** Begin file status.c ******************************************/
20647/*
20648** 2008 June 18
20649**
20650** The author disclaims copyright to this source code.  In place of
20651** a legal notice, here is a blessing:
20652**
20653**    May you do good and not evil.
20654**    May you find forgiveness for yourself and forgive others.
20655**    May you share freely, never taking more than you give.
20656**
20657*************************************************************************
20658**
20659** This module implements the sqlite3_status() interface and related
20660** functionality.
20661*/
20662/* #include "sqliteInt.h" */
20663/************** Include vdbeInt.h in the middle of status.c ******************/
20664/************** Begin file vdbeInt.h *****************************************/
20665/*
20666** 2003 September 6
20667**
20668** The author disclaims copyright to this source code.  In place of
20669** a legal notice, here is a blessing:
20670**
20671**    May you do good and not evil.
20672**    May you find forgiveness for yourself and forgive others.
20673**    May you share freely, never taking more than you give.
20674**
20675*************************************************************************
20676** This is the header file for information that is private to the
20677** VDBE.  This information used to all be at the top of the single
20678** source code file "vdbe.c".  When that file became too big (over
20679** 6000 lines long) it was split up into several smaller files and
20680** this header information was factored out.
20681*/
20682#ifndef SQLITE_VDBEINT_H
20683#define SQLITE_VDBEINT_H
20684
20685/*
20686** The maximum number of times that a statement will try to reparse
20687** itself before giving up and returning SQLITE_SCHEMA.
20688*/
20689#ifndef SQLITE_MAX_SCHEMA_RETRY
20690# define SQLITE_MAX_SCHEMA_RETRY 50
20691#endif
20692
20693/*
20694** VDBE_DISPLAY_P4 is true or false depending on whether or not the
20695** "explain" P4 display logic is enabled.
20696*/
20697#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
20698     || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) \
20699     || defined(SQLITE_ENABLE_BYTECODE_VTAB)
20700# define VDBE_DISPLAY_P4 1
20701#else
20702# define VDBE_DISPLAY_P4 0
20703#endif
20704
20705/*
20706** SQL is translated into a sequence of instructions to be
20707** executed by a virtual machine.  Each instruction is an instance
20708** of the following structure.
20709*/
20710typedef struct VdbeOp Op;
20711
20712/*
20713** Boolean values
20714*/
20715typedef unsigned Bool;
20716
20717/* Opaque type used by code in vdbesort.c */
20718typedef struct VdbeSorter VdbeSorter;
20719
20720/* Elements of the linked list at Vdbe.pAuxData */
20721typedef struct AuxData AuxData;
20722
20723/* Types of VDBE cursors */
20724#define CURTYPE_BTREE       0
20725#define CURTYPE_SORTER      1
20726#define CURTYPE_VTAB        2
20727#define CURTYPE_PSEUDO      3
20728
20729/*
20730** A VdbeCursor is an superclass (a wrapper) for various cursor objects:
20731**
20732**      * A b-tree cursor
20733**          -  In the main database or in an ephemeral database
20734**          -  On either an index or a table
20735**      * A sorter
20736**      * A virtual table
20737**      * A one-row "pseudotable" stored in a single register
20738*/
20739typedef struct VdbeCursor VdbeCursor;
20740struct VdbeCursor {
20741  u8 eCurType;            /* One of the CURTYPE_* values above */
20742  i8 iDb;                 /* Index of cursor database in db->aDb[] (or -1) */
20743  u8 nullRow;             /* True if pointing to a row with no data */
20744  u8 deferredMoveto;      /* A call to sqlite3BtreeMoveto() is needed */
20745  u8 isTable;             /* True for rowid tables.  False for indexes */
20746#ifdef SQLITE_DEBUG
20747  u8 seekOp;              /* Most recent seek operation on this cursor */
20748  u8 wrFlag;              /* The wrFlag argument to sqlite3BtreeCursor() */
20749#endif
20750  Bool isEphemeral:1;     /* True for an ephemeral table */
20751  Bool useRandomRowid:1;  /* Generate new record numbers semi-randomly */
20752  Bool isOrdered:1;       /* True if the table is not BTREE_UNORDERED */
20753  Bool seekHit:1;         /* See the OP_SeekHit and OP_IfNoHope opcodes */
20754  Btree *pBtx;            /* Separate file holding temporary table */
20755  i64 seqCount;           /* Sequence counter */
20756  int *aAltMap;           /* Mapping from table to index column numbers */
20757
20758  /* Cached OP_Column parse information is only valid if cacheStatus matches
20759  ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
20760  ** CACHE_STALE (0) and so setting cacheStatus=CACHE_STALE guarantees that
20761  ** the cache is out of date. */
20762  u32 cacheStatus;        /* Cache is valid if this matches Vdbe.cacheCtr */
20763  int seekResult;         /* Result of previous sqlite3BtreeMoveto() or 0
20764                          ** if there have been no prior seeks on the cursor. */
20765  /* seekResult does not distinguish between "no seeks have ever occurred
20766  ** on this cursor" and "the most recent seek was an exact match".
20767  ** For CURTYPE_PSEUDO, seekResult is the register holding the record */
20768
20769  /* When a new VdbeCursor is allocated, only the fields above are zeroed.
20770  ** The fields that follow are uninitialized, and must be individually
20771  ** initialized prior to first use. */
20772  VdbeCursor *pAltCursor; /* Associated index cursor from which to read */
20773  union {
20774    BtCursor *pCursor;          /* CURTYPE_BTREE or _PSEUDO.  Btree cursor */
20775    sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB.              Vtab cursor */
20776    VdbeSorter *pSorter;        /* CURTYPE_SORTER.            Sorter object */
20777  } uc;
20778  KeyInfo *pKeyInfo;      /* Info about index keys needed by index cursors */
20779  u32 iHdrOffset;         /* Offset to next unparsed byte of the header */
20780  Pgno pgnoRoot;          /* Root page of the open btree cursor */
20781  i16 nField;             /* Number of fields in the header */
20782  u16 nHdrParsed;         /* Number of header fields parsed so far */
20783  i64 movetoTarget;       /* Argument to the deferred sqlite3BtreeMoveto() */
20784  u32 *aOffset;           /* Pointer to aType[nField] */
20785  const u8 *aRow;         /* Data for the current row, if all on one page */
20786  u32 payloadSize;        /* Total number of bytes in the record */
20787  u32 szRow;              /* Byte available in aRow */
20788#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
20789  u64 maskUsed;           /* Mask of columns used by this cursor */
20790#endif
20791
20792  /* 2*nField extra array elements allocated for aType[], beyond the one
20793  ** static element declared in the structure.  nField total array slots for
20794  ** aType[] and nField+1 array slots for aOffset[] */
20795  u32 aType[1];           /* Type values record decode.  MUST BE LAST */
20796};
20797
20798
20799/*
20800** A value for VdbeCursor.cacheStatus that means the cache is always invalid.
20801*/
20802#define CACHE_STALE 0
20803
20804/*
20805** When a sub-program is executed (OP_Program), a structure of this type
20806** is allocated to store the current value of the program counter, as
20807** well as the current memory cell array and various other frame specific
20808** values stored in the Vdbe struct. When the sub-program is finished,
20809** these values are copied back to the Vdbe from the VdbeFrame structure,
20810** restoring the state of the VM to as it was before the sub-program
20811** began executing.
20812**
20813** The memory for a VdbeFrame object is allocated and managed by a memory
20814** cell in the parent (calling) frame. When the memory cell is deleted or
20815** overwritten, the VdbeFrame object is not freed immediately. Instead, it
20816** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
20817** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
20818** this instead of deleting the VdbeFrame immediately is to avoid recursive
20819** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
20820** child frame are released.
20821**
20822** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
20823** set to NULL if the currently executing frame is the main program.
20824*/
20825typedef struct VdbeFrame VdbeFrame;
20826struct VdbeFrame {
20827  Vdbe *v;                /* VM this frame belongs to */
20828  VdbeFrame *pParent;     /* Parent of this frame, or NULL if parent is main */
20829  Op *aOp;                /* Program instructions for parent frame */
20830  i64 *anExec;            /* Event counters from parent frame */
20831  Mem *aMem;              /* Array of memory cells for parent frame */
20832  VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
20833  u8 *aOnce;              /* Bitmask used by OP_Once */
20834  void *token;            /* Copy of SubProgram.token */
20835  i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
20836  AuxData *pAuxData;      /* Linked list of auxdata allocations */
20837#if SQLITE_DEBUG
20838  u32 iFrameMagic;        /* magic number for sanity checking */
20839#endif
20840  int nCursor;            /* Number of entries in apCsr */
20841  int pc;                 /* Program Counter in parent (calling) frame */
20842  int nOp;                /* Size of aOp array */
20843  int nMem;               /* Number of entries in aMem */
20844  int nChildMem;          /* Number of memory cells for child frame */
20845  int nChildCsr;          /* Number of cursors for child frame */
20846  int nChange;            /* Statement changes (Vdbe.nChange)     */
20847  int nDbChange;          /* Value of db->nChange */
20848};
20849
20850/* Magic number for sanity checking on VdbeFrame objects */
20851#define SQLITE_FRAME_MAGIC 0x879fb71e
20852
20853/*
20854** Return a pointer to the array of registers allocated for use
20855** by a VdbeFrame.
20856*/
20857#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
20858
20859/*
20860** Internally, the vdbe manipulates nearly all SQL values as Mem
20861** structures. Each Mem struct may cache multiple representations (string,
20862** integer etc.) of the same value.
20863*/
20864struct sqlite3_value {
20865  union MemValue {
20866    double r;           /* Real value used when MEM_Real is set in flags */
20867    i64 i;              /* Integer value used when MEM_Int is set in flags */
20868    int nZero;          /* Extra zero bytes when MEM_Zero and MEM_Blob set */
20869    const char *zPType; /* Pointer type when MEM_Term|MEM_Subtype|MEM_Null */
20870    FuncDef *pDef;      /* Used only when flags==MEM_Agg */
20871  } u;
20872  u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
20873  u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
20874  u8  eSubtype;       /* Subtype for this value */
20875  int n;              /* Number of characters in string value, excluding '\0' */
20876  char *z;            /* String or BLOB value */
20877  /* ShallowCopy only needs to copy the information above */
20878  char *zMalloc;      /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */
20879  int szMalloc;       /* Size of the zMalloc allocation */
20880  u32 uTemp;          /* Transient storage for serial_type in OP_MakeRecord */
20881  sqlite3 *db;        /* The associated database connection */
20882  void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
20883#ifdef SQLITE_DEBUG
20884  Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
20885  u16 mScopyFlags;    /* flags value immediately after the shallow copy */
20886#endif
20887};
20888
20889/*
20890** Size of struct Mem not including the Mem.zMalloc member or anything that
20891** follows.
20892*/
20893#define MEMCELLSIZE offsetof(Mem,zMalloc)
20894
20895/* One or more of the following flags are set to indicate the validOK
20896** representations of the value stored in the Mem struct.
20897**
20898** If the MEM_Null flag is set, then the value is an SQL NULL value.
20899** For a pointer type created using sqlite3_bind_pointer() or
20900** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
20901**
20902** If the MEM_Str flag is set then Mem.z points at a string representation.
20903** Usually this is encoded in the same unicode encoding as the main
20904** database (see below for exceptions). If the MEM_Term flag is also
20905** set, then the string is nul terminated. The MEM_Int and MEM_Real
20906** flags may coexist with the MEM_Str flag.
20907*/
20908#define MEM_Null      0x0001   /* Value is NULL (or a pointer) */
20909#define MEM_Str       0x0002   /* Value is a string */
20910#define MEM_Int       0x0004   /* Value is an integer */
20911#define MEM_Real      0x0008   /* Value is a real number */
20912#define MEM_Blob      0x0010   /* Value is a BLOB */
20913#define MEM_IntReal   0x0020   /* MEM_Int that stringifies like MEM_Real */
20914#define MEM_AffMask   0x003f   /* Mask of affinity bits */
20915#define MEM_FromBind  0x0040   /* Value originates from sqlite3_bind() */
20916#define MEM_Undefined 0x0080   /* Value is undefined */
20917#define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
20918#define MEM_TypeMask  0xc1bf   /* Mask of type bits */
20919
20920
20921/* Whenever Mem contains a valid string or blob representation, one of
20922** the following flags must be set to determine the memory management
20923** policy for Mem.z.  The MEM_Term flag tells us whether or not the
20924** string is \000 or \u0000 terminated
20925*/
20926#define MEM_Term      0x0200   /* String in Mem.z is zero terminated */
20927#define MEM_Dyn       0x0400   /* Need to call Mem.xDel() on Mem.z */
20928#define MEM_Static    0x0800   /* Mem.z points to a static string */
20929#define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
20930#define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
20931#define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
20932#define MEM_Subtype   0x8000   /* Mem.eSubtype is valid */
20933#ifdef SQLITE_OMIT_INCRBLOB
20934  #undef MEM_Zero
20935  #define MEM_Zero 0x0000
20936#endif
20937
20938/* Return TRUE if Mem X contains dynamically allocated content - anything
20939** that needs to be deallocated to avoid a leak.
20940*/
20941#define VdbeMemDynamic(X)  \
20942  (((X)->flags&(MEM_Agg|MEM_Dyn))!=0)
20943
20944/*
20945** Clear any existing type flags from a Mem and replace them with f
20946*/
20947#define MemSetTypeFlag(p, f) \
20948   ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
20949
20950/*
20951** True if Mem X is a NULL-nochng type.
20952*/
20953#define MemNullNochng(X) \
20954  (((X)->flags&MEM_TypeMask)==(MEM_Null|MEM_Zero) \
20955    && (X)->n==0 && (X)->u.nZero==0)
20956
20957/*
20958** Return true if a memory cell is not marked as invalid.  This macro
20959** is for use inside assert() statements only.
20960*/
20961#ifdef SQLITE_DEBUG
20962#define memIsValid(M)  ((M)->flags & MEM_Undefined)==0
20963#endif
20964
20965/*
20966** Each auxiliary data pointer stored by a user defined function
20967** implementation calling sqlite3_set_auxdata() is stored in an instance
20968** of this structure. All such structures associated with a single VM
20969** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
20970** when the VM is halted (if not before).
20971*/
20972struct AuxData {
20973  int iAuxOp;                     /* Instruction number of OP_Function opcode */
20974  int iAuxArg;                    /* Index of function argument. */
20975  void *pAux;                     /* Aux data pointer */
20976  void (*xDeleteAux)(void*);      /* Destructor for the aux data */
20977  AuxData *pNextAux;              /* Next element in list */
20978};
20979
20980/*
20981** The "context" argument for an installable function.  A pointer to an
20982** instance of this structure is the first argument to the routines used
20983** implement the SQL functions.
20984**
20985** There is a typedef for this structure in sqlite.h.  So all routines,
20986** even the public interface to SQLite, can use a pointer to this structure.
20987** But this file is the only place where the internal details of this
20988** structure are known.
20989**
20990** This structure is defined inside of vdbeInt.h because it uses substructures
20991** (Mem) which are only defined there.
20992*/
20993struct sqlite3_context {
20994  Mem *pOut;              /* The return value is stored here */
20995  FuncDef *pFunc;         /* Pointer to function information */
20996  Mem *pMem;              /* Memory cell used to store aggregate context */
20997  Vdbe *pVdbe;            /* The VM that owns this context */
20998  int iOp;                /* Instruction number of OP_Function */
20999  int isError;            /* Error code returned by the function. */
21000  u8 skipFlag;            /* Skip accumulator loading if true */
21001  u8 argc;                /* Number of arguments */
21002  sqlite3_value *argv[1]; /* Argument set */
21003};
21004
21005/* A bitfield type for use inside of structures.  Always follow with :N where
21006** N is the number of bits.
21007*/
21008typedef unsigned bft;  /* Bit Field Type */
21009
21010/* The ScanStatus object holds a single value for the
21011** sqlite3_stmt_scanstatus() interface.
21012*/
21013typedef struct ScanStatus ScanStatus;
21014struct ScanStatus {
21015  int addrExplain;                /* OP_Explain for loop */
21016  int addrLoop;                   /* Address of "loops" counter */
21017  int addrVisit;                  /* Address of "rows visited" counter */
21018  int iSelectID;                  /* The "Select-ID" for this loop */
21019  LogEst nEst;                    /* Estimated output rows per loop */
21020  char *zName;                    /* Name of table or index */
21021};
21022
21023/* The DblquoteStr object holds the text of a double-quoted
21024** string for a prepared statement.  A linked list of these objects
21025** is constructed during statement parsing and is held on Vdbe.pDblStr.
21026** When computing a normalized SQL statement for an SQL statement, that
21027** list is consulted for each double-quoted identifier to see if the
21028** identifier should really be a string literal.
21029*/
21030typedef struct DblquoteStr DblquoteStr;
21031struct DblquoteStr {
21032  DblquoteStr *pNextStr;   /* Next string literal in the list */
21033  char z[8];               /* Dequoted value for the string */
21034};
21035
21036/*
21037** An instance of the virtual machine.  This structure contains the complete
21038** state of the virtual machine.
21039**
21040** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
21041** is really a pointer to an instance of this structure.
21042*/
21043struct Vdbe {
21044  sqlite3 *db;            /* The database connection that owns this statement */
21045  Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
21046  Parse *pParse;          /* Parsing context used to create this Vdbe */
21047  ynVar nVar;             /* Number of entries in aVar[] */
21048  u32 magic;              /* Magic number for sanity checking */
21049  int nMem;               /* Number of memory locations currently allocated */
21050  int nCursor;            /* Number of slots in apCsr[] */
21051  u32 cacheCtr;           /* VdbeCursor row cache generation counter */
21052  int pc;                 /* The program counter */
21053  int rc;                 /* Value to return */
21054  int nChange;            /* Number of db changes made since last reset */
21055  int iStatement;         /* Statement number (or 0 if has no opened stmt) */
21056  i64 iCurrentTime;       /* Value of julianday('now') for this statement */
21057  i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
21058  i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
21059  i64 nStmtDefImmCons;    /* Number of def. imm constraints when stmt started */
21060  Mem *aMem;              /* The memory locations */
21061  Mem **apArg;            /* Arguments to currently executing user function */
21062  VdbeCursor **apCsr;     /* One element of this array for each open cursor */
21063  Mem *aVar;              /* Values for the OP_Variable opcode. */
21064
21065  /* When allocating a new Vdbe object, all of the fields below should be
21066  ** initialized to zero or NULL */
21067
21068  Op *aOp;                /* Space to hold the virtual machine's program */
21069  int nOp;                /* Number of instructions in the program */
21070  int nOpAlloc;           /* Slots allocated for aOp[] */
21071  Mem *aColName;          /* Column names to return */
21072  Mem *pResultSet;        /* Pointer to an array of results */
21073  char *zErrMsg;          /* Error message written here */
21074  VList *pVList;          /* Name of variables */
21075#ifndef SQLITE_OMIT_TRACE
21076  i64 startTime;          /* Time when query started - used for profiling */
21077#endif
21078#ifdef SQLITE_DEBUG
21079  int rcApp;              /* errcode set by sqlite3_result_error_code() */
21080  u32 nWrite;             /* Number of write operations that have occurred */
21081#endif
21082  u16 nResColumn;         /* Number of columns in one row of the result set */
21083  u8 errorAction;         /* Recovery action to do in case of an error */
21084  u8 minWriteFileFormat;  /* Minimum file format for writable database files */
21085  u8 prepFlags;           /* SQLITE_PREPARE_* flags */
21086  u8 doingRerun;          /* True if rerunning after an auto-reprepare */
21087  bft expired:2;          /* 1: recompile VM immediately  2: when convenient */
21088  bft explain:2;          /* True if EXPLAIN present on SQL command */
21089  bft changeCntOn:1;      /* True to update the change-counter */
21090  bft runOnlyOnce:1;      /* Automatically expire on reset */
21091  bft usesStmtJournal:1;  /* True if uses a statement journal */
21092  bft readOnly:1;         /* True for statements that do not write */
21093  bft bIsReader:1;        /* True for statements that read */
21094  yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
21095  yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
21096  u32 aCounter[7];        /* Counters used by sqlite3_stmt_status() */
21097  char *zSql;             /* Text of the SQL statement that generated this */
21098#ifdef SQLITE_ENABLE_NORMALIZE
21099  char *zNormSql;         /* Normalization of the associated SQL statement */
21100  DblquoteStr *pDblStr;   /* List of double-quoted string literals */
21101#endif
21102  void *pFree;            /* Free this when deleting the vdbe */
21103  VdbeFrame *pFrame;      /* Parent frame */
21104  VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
21105  int nFrame;             /* Number of frames in pFrame list */
21106  u32 expmask;            /* Binding to these vars invalidates VM */
21107  SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
21108  AuxData *pAuxData;      /* Linked list of auxdata allocations */
21109#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
21110  i64 *anExec;            /* Number of times each op has been executed */
21111  int nScan;              /* Entries in aScan[] */
21112  ScanStatus *aScan;      /* Scan definitions for sqlite3_stmt_scanstatus() */
21113#endif
21114};
21115
21116/*
21117** The following are allowed values for Vdbe.magic
21118*/
21119#define VDBE_MAGIC_INIT     0x16bceaa5    /* Building a VDBE program */
21120#define VDBE_MAGIC_RUN      0x2df20da3    /* VDBE is ready to execute */
21121#define VDBE_MAGIC_HALT     0x319c2973    /* VDBE has completed execution */
21122#define VDBE_MAGIC_RESET    0x48fa9f76    /* Reset and ready to run again */
21123#define VDBE_MAGIC_DEAD     0x5606c3c8    /* The VDBE has been deallocated */
21124
21125/*
21126** Structure used to store the context required by the
21127** sqlite3_preupdate_*() API functions.
21128*/
21129struct PreUpdate {
21130  Vdbe *v;
21131  VdbeCursor *pCsr;               /* Cursor to read old values from */
21132  int op;                         /* One of SQLITE_INSERT, UPDATE, DELETE */
21133  u8 *aRecord;                    /* old.* database record */
21134  KeyInfo keyinfo;
21135  UnpackedRecord *pUnpacked;      /* Unpacked version of aRecord[] */
21136  UnpackedRecord *pNewUnpacked;   /* Unpacked version of new.* record */
21137  int iNewReg;                    /* Register for new.* values */
21138  i64 iKey1;                      /* First key value passed to hook */
21139  i64 iKey2;                      /* Second key value passed to hook */
21140  Mem *aNew;                      /* Array of new.* values */
21141  Table *pTab;                    /* Schema object being upated */
21142  Index *pPk;                     /* PK index if pTab is WITHOUT ROWID */
21143};
21144
21145/*
21146** Function prototypes
21147*/
21148SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
21149SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
21150void sqliteVdbePopStack(Vdbe*,int);
21151SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeFinishMoveto(VdbeCursor*);
21152SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor**, int*);
21153SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
21154SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
21155SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8);
21156SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
21157SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
21158SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
21159
21160int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
21161SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
21162SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
21163SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
21164#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB)
21165SQLITE_PRIVATE int sqlite3VdbeNextOpcode(Vdbe*,Mem*,int,int*,int*,Op**);
21166SQLITE_PRIVATE char *sqlite3VdbeDisplayP4(sqlite3*,Op*);
21167#endif
21168#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
21169SQLITE_PRIVATE char *sqlite3VdbeDisplayComment(sqlite3*,const Op*,const char*);
21170#endif
21171#if !defined(SQLITE_OMIT_EXPLAIN)
21172SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
21173#endif
21174SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
21175SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
21176SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
21177SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
21178SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
21179SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
21180SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
21181SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
21182SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
21183#ifdef SQLITE_OMIT_FLOATING_POINT
21184# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
21185#else
21186SQLITE_PRIVATE   void sqlite3VdbeMemSetDouble(Mem*, double);
21187#endif
21188SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(Mem*, void*, const char*, void(*)(void*));
21189SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem*,sqlite3*,u16);
21190SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
21191SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
21192#ifdef SQLITE_DEBUG
21193SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem*);
21194#endif
21195SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem*);
21196SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
21197SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
21198SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
21199SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
21200SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
21201SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
21202SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
21203SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
21204SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
21205SQLITE_PRIVATE int sqlite3VdbeMemCast(Mem*,u8,u8);
21206SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
21207SQLITE_PRIVATE int sqlite3VdbeMemFromBtreeZeroOffset(BtCursor*,u32,Mem*);
21208SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
21209SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
21210#ifndef SQLITE_OMIT_WINDOWFUNC
21211SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*);
21212#endif
21213#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB)
21214SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
21215#endif
21216SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
21217SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
21218SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
21219#ifdef SQLITE_DEBUG
21220SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame*);
21221#endif
21222SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void*);      /* Destructor on Mem */
21223SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); /* Actually deletes the Frame */
21224SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
21225#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
21226SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int);
21227#endif
21228SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
21229
21230SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *);
21231SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
21232SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
21233SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
21234SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
21235SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
21236SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
21237SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
21238
21239#ifdef SQLITE_DEBUG
21240SQLITE_PRIVATE   void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*);
21241SQLITE_PRIVATE   void sqlite3VdbeAssertAbortable(Vdbe*);
21242#else
21243# define sqlite3VdbeIncrWriteCounter(V,C)
21244# define sqlite3VdbeAssertAbortable(V)
21245#endif
21246
21247#if !defined(SQLITE_OMIT_SHARED_CACHE)
21248SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
21249#else
21250# define sqlite3VdbeEnter(X)
21251#endif
21252
21253#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
21254SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
21255#else
21256# define sqlite3VdbeLeave(X)
21257#endif
21258
21259#ifdef SQLITE_DEBUG
21260SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
21261SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
21262#endif
21263
21264#ifndef SQLITE_OMIT_FOREIGN_KEY
21265SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
21266#else
21267# define sqlite3VdbeCheckFk(p,i) 0
21268#endif
21269
21270#ifdef SQLITE_DEBUG
21271SQLITE_PRIVATE   void sqlite3VdbePrintSql(Vdbe*);
21272SQLITE_PRIVATE   void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr);
21273#endif
21274#ifndef SQLITE_OMIT_UTF16
21275SQLITE_PRIVATE   int sqlite3VdbeMemTranslate(Mem*, u8);
21276SQLITE_PRIVATE   int sqlite3VdbeMemHandleBom(Mem *pMem);
21277#endif
21278
21279#ifndef SQLITE_OMIT_INCRBLOB
21280SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
21281  #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
21282#else
21283  #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
21284  #define ExpandBlob(P) SQLITE_OK
21285#endif
21286
21287#endif /* !defined(SQLITE_VDBEINT_H) */
21288
21289/************** End of vdbeInt.h *********************************************/
21290/************** Continuing where we left off in status.c *********************/
21291
21292/*
21293** Variables in which to record status information.
21294*/
21295#if SQLITE_PTRSIZE>4
21296typedef sqlite3_int64 sqlite3StatValueType;
21297#else
21298typedef u32 sqlite3StatValueType;
21299#endif
21300typedef struct sqlite3StatType sqlite3StatType;
21301static SQLITE_WSD struct sqlite3StatType {
21302  sqlite3StatValueType nowValue[10];  /* Current value */
21303  sqlite3StatValueType mxValue[10];   /* Maximum value */
21304} sqlite3Stat = { {0,}, {0,} };
21305
21306/*
21307** Elements of sqlite3Stat[] are protected by either the memory allocator
21308** mutex, or by the pcache1 mutex.  The following array determines which.
21309*/
21310static const char statMutex[] = {
21311  0,  /* SQLITE_STATUS_MEMORY_USED */
21312  1,  /* SQLITE_STATUS_PAGECACHE_USED */
21313  1,  /* SQLITE_STATUS_PAGECACHE_OVERFLOW */
21314  0,  /* SQLITE_STATUS_SCRATCH_USED */
21315  0,  /* SQLITE_STATUS_SCRATCH_OVERFLOW */
21316  0,  /* SQLITE_STATUS_MALLOC_SIZE */
21317  0,  /* SQLITE_STATUS_PARSER_STACK */
21318  1,  /* SQLITE_STATUS_PAGECACHE_SIZE */
21319  0,  /* SQLITE_STATUS_SCRATCH_SIZE */
21320  0,  /* SQLITE_STATUS_MALLOC_COUNT */
21321};
21322
21323
21324/* The "wsdStat" macro will resolve to the status information
21325** state vector.  If writable static data is unsupported on the target,
21326** we have to locate the state vector at run-time.  In the more common
21327** case where writable static data is supported, wsdStat can refer directly
21328** to the "sqlite3Stat" state vector declared above.
21329*/
21330#ifdef SQLITE_OMIT_WSD
21331# define wsdStatInit  sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
21332# define wsdStat x[0]
21333#else
21334# define wsdStatInit
21335# define wsdStat sqlite3Stat
21336#endif
21337
21338/*
21339** Return the current value of a status parameter.  The caller must
21340** be holding the appropriate mutex.
21341*/
21342SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int op){
21343  wsdStatInit;
21344  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
21345  assert( op>=0 && op<ArraySize(statMutex) );
21346  assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
21347                                           : sqlite3MallocMutex()) );
21348  return wsdStat.nowValue[op];
21349}
21350
21351/*
21352** Add N to the value of a status record.  The caller must hold the
21353** appropriate mutex.  (Locking is checked by assert()).
21354**
21355** The StatusUp() routine can accept positive or negative values for N.
21356** The value of N is added to the current status value and the high-water
21357** mark is adjusted if necessary.
21358**
21359** The StatusDown() routine lowers the current value by N.  The highwater
21360** mark is unchanged.  N must be non-negative for StatusDown().
21361*/
21362SQLITE_PRIVATE void sqlite3StatusUp(int op, int N){
21363  wsdStatInit;
21364  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
21365  assert( op>=0 && op<ArraySize(statMutex) );
21366  assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
21367                                           : sqlite3MallocMutex()) );
21368  wsdStat.nowValue[op] += N;
21369  if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
21370    wsdStat.mxValue[op] = wsdStat.nowValue[op];
21371  }
21372}
21373SQLITE_PRIVATE void sqlite3StatusDown(int op, int N){
21374  wsdStatInit;
21375  assert( N>=0 );
21376  assert( op>=0 && op<ArraySize(statMutex) );
21377  assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
21378                                           : sqlite3MallocMutex()) );
21379  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
21380  wsdStat.nowValue[op] -= N;
21381}
21382
21383/*
21384** Adjust the highwater mark if necessary.
21385** The caller must hold the appropriate mutex.
21386*/
21387SQLITE_PRIVATE void sqlite3StatusHighwater(int op, int X){
21388  sqlite3StatValueType newValue;
21389  wsdStatInit;
21390  assert( X>=0 );
21391  newValue = (sqlite3StatValueType)X;
21392  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
21393  assert( op>=0 && op<ArraySize(statMutex) );
21394  assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
21395                                           : sqlite3MallocMutex()) );
21396  assert( op==SQLITE_STATUS_MALLOC_SIZE
21397          || op==SQLITE_STATUS_PAGECACHE_SIZE
21398          || op==SQLITE_STATUS_PARSER_STACK );
21399  if( newValue>wsdStat.mxValue[op] ){
21400    wsdStat.mxValue[op] = newValue;
21401  }
21402}
21403
21404/*
21405** Query status information.
21406*/
21407SQLITE_API int sqlite3_status64(
21408  int op,
21409  sqlite3_int64 *pCurrent,
21410  sqlite3_int64 *pHighwater,
21411  int resetFlag
21412){
21413  sqlite3_mutex *pMutex;
21414  wsdStatInit;
21415  if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
21416    return SQLITE_MISUSE_BKPT;
21417  }
21418#ifdef SQLITE_ENABLE_API_ARMOR
21419  if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
21420#endif
21421  pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
21422  sqlite3_mutex_enter(pMutex);
21423  *pCurrent = wsdStat.nowValue[op];
21424  *pHighwater = wsdStat.mxValue[op];
21425  if( resetFlag ){
21426    wsdStat.mxValue[op] = wsdStat.nowValue[op];
21427  }
21428  sqlite3_mutex_leave(pMutex);
21429  (void)pMutex;  /* Prevent warning when SQLITE_THREADSAFE=0 */
21430  return SQLITE_OK;
21431}
21432SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
21433  sqlite3_int64 iCur = 0, iHwtr = 0;
21434  int rc;
21435#ifdef SQLITE_ENABLE_API_ARMOR
21436  if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
21437#endif
21438  rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
21439  if( rc==0 ){
21440    *pCurrent = (int)iCur;
21441    *pHighwater = (int)iHwtr;
21442  }
21443  return rc;
21444}
21445
21446/*
21447** Return the number of LookasideSlot elements on the linked list
21448*/
21449static u32 countLookasideSlots(LookasideSlot *p){
21450  u32 cnt = 0;
21451  while( p ){
21452    p = p->pNext;
21453    cnt++;
21454  }
21455  return cnt;
21456}
21457
21458/*
21459** Count the number of slots of lookaside memory that are outstanding
21460*/
21461SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){
21462  u32 nInit = countLookasideSlots(db->lookaside.pInit);
21463  u32 nFree = countLookasideSlots(db->lookaside.pFree);
21464#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
21465  nInit += countLookasideSlots(db->lookaside.pSmallInit);
21466  nFree += countLookasideSlots(db->lookaside.pSmallFree);
21467#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
21468  if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
21469  return db->lookaside.nSlot - (nInit+nFree);
21470}
21471
21472/*
21473** Query status information for a single database connection
21474*/
21475SQLITE_API int sqlite3_db_status(
21476  sqlite3 *db,          /* The database connection whose status is desired */
21477  int op,               /* Status verb */
21478  int *pCurrent,        /* Write current value here */
21479  int *pHighwater,      /* Write high-water mark here */
21480  int resetFlag         /* Reset high-water mark if true */
21481){
21482  int rc = SQLITE_OK;   /* Return code */
21483#ifdef SQLITE_ENABLE_API_ARMOR
21484  if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
21485    return SQLITE_MISUSE_BKPT;
21486  }
21487#endif
21488  sqlite3_mutex_enter(db->mutex);
21489  switch( op ){
21490    case SQLITE_DBSTATUS_LOOKASIDE_USED: {
21491      *pCurrent = sqlite3LookasideUsed(db, pHighwater);
21492      if( resetFlag ){
21493        LookasideSlot *p = db->lookaside.pFree;
21494        if( p ){
21495          while( p->pNext ) p = p->pNext;
21496          p->pNext = db->lookaside.pInit;
21497          db->lookaside.pInit = db->lookaside.pFree;
21498          db->lookaside.pFree = 0;
21499        }
21500#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
21501        p = db->lookaside.pSmallFree;
21502        if( p ){
21503          while( p->pNext ) p = p->pNext;
21504          p->pNext = db->lookaside.pSmallInit;
21505          db->lookaside.pSmallInit = db->lookaside.pSmallFree;
21506          db->lookaside.pSmallFree = 0;
21507        }
21508#endif
21509      }
21510      break;
21511    }
21512
21513    case SQLITE_DBSTATUS_LOOKASIDE_HIT:
21514    case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
21515    case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
21516      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
21517      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
21518      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
21519      assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
21520      assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
21521      *pCurrent = 0;
21522      *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
21523      if( resetFlag ){
21524        db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
21525      }
21526      break;
21527    }
21528
21529    /*
21530    ** Return an approximation for the amount of memory currently used
21531    ** by all pagers associated with the given database connection.  The
21532    ** highwater mark is meaningless and is returned as zero.
21533    */
21534    case SQLITE_DBSTATUS_CACHE_USED_SHARED:
21535    case SQLITE_DBSTATUS_CACHE_USED: {
21536      int totalUsed = 0;
21537      int i;
21538      sqlite3BtreeEnterAll(db);
21539      for(i=0; i<db->nDb; i++){
21540        Btree *pBt = db->aDb[i].pBt;
21541        if( pBt ){
21542          Pager *pPager = sqlite3BtreePager(pBt);
21543          int nByte = sqlite3PagerMemUsed(pPager);
21544          if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED ){
21545            nByte = nByte / sqlite3BtreeConnectionCount(pBt);
21546          }
21547          totalUsed += nByte;
21548        }
21549      }
21550      sqlite3BtreeLeaveAll(db);
21551      *pCurrent = totalUsed;
21552      *pHighwater = 0;
21553      break;
21554    }
21555
21556    /*
21557    ** *pCurrent gets an accurate estimate of the amount of memory used
21558    ** to store the schema for all databases (main, temp, and any ATTACHed
21559    ** databases.  *pHighwater is set to zero.
21560    */
21561    case SQLITE_DBSTATUS_SCHEMA_USED: {
21562      int i;                      /* Used to iterate through schemas */
21563      int nByte = 0;              /* Used to accumulate return value */
21564
21565      sqlite3BtreeEnterAll(db);
21566      db->pnBytesFreed = &nByte;
21567      for(i=0; i<db->nDb; i++){
21568        Schema *pSchema = db->aDb[i].pSchema;
21569        if( ALWAYS(pSchema!=0) ){
21570          HashElem *p;
21571
21572          nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
21573              pSchema->tblHash.count
21574            + pSchema->trigHash.count
21575            + pSchema->idxHash.count
21576            + pSchema->fkeyHash.count
21577          );
21578          nByte += sqlite3_msize(pSchema->tblHash.ht);
21579          nByte += sqlite3_msize(pSchema->trigHash.ht);
21580          nByte += sqlite3_msize(pSchema->idxHash.ht);
21581          nByte += sqlite3_msize(pSchema->fkeyHash.ht);
21582
21583          for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
21584            sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
21585          }
21586          for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
21587            sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
21588          }
21589        }
21590      }
21591      db->pnBytesFreed = 0;
21592      sqlite3BtreeLeaveAll(db);
21593
21594      *pHighwater = 0;
21595      *pCurrent = nByte;
21596      break;
21597    }
21598
21599    /*
21600    ** *pCurrent gets an accurate estimate of the amount of memory used
21601    ** to store all prepared statements.
21602    ** *pHighwater is set to zero.
21603    */
21604    case SQLITE_DBSTATUS_STMT_USED: {
21605      struct Vdbe *pVdbe;         /* Used to iterate through VMs */
21606      int nByte = 0;              /* Used to accumulate return value */
21607
21608      db->pnBytesFreed = &nByte;
21609      for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
21610        sqlite3VdbeClearObject(db, pVdbe);
21611        sqlite3DbFree(db, pVdbe);
21612      }
21613      db->pnBytesFreed = 0;
21614
21615      *pHighwater = 0;  /* IMP: R-64479-57858 */
21616      *pCurrent = nByte;
21617
21618      break;
21619    }
21620
21621    /*
21622    ** Set *pCurrent to the total cache hits or misses encountered by all
21623    ** pagers the database handle is connected to. *pHighwater is always set
21624    ** to zero.
21625    */
21626    case SQLITE_DBSTATUS_CACHE_SPILL:
21627      op = SQLITE_DBSTATUS_CACHE_WRITE+1;
21628      /* Fall through into the next case */
21629    case SQLITE_DBSTATUS_CACHE_HIT:
21630    case SQLITE_DBSTATUS_CACHE_MISS:
21631    case SQLITE_DBSTATUS_CACHE_WRITE:{
21632      int i;
21633      int nRet = 0;
21634      assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
21635      assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
21636
21637      for(i=0; i<db->nDb; i++){
21638        if( db->aDb[i].pBt ){
21639          Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
21640          sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
21641        }
21642      }
21643      *pHighwater = 0; /* IMP: R-42420-56072 */
21644                       /* IMP: R-54100-20147 */
21645                       /* IMP: R-29431-39229 */
21646      *pCurrent = nRet;
21647      break;
21648    }
21649
21650    /* Set *pCurrent to non-zero if there are unresolved deferred foreign
21651    ** key constraints.  Set *pCurrent to zero if all foreign key constraints
21652    ** have been satisfied.  The *pHighwater is always set to zero.
21653    */
21654    case SQLITE_DBSTATUS_DEFERRED_FKS: {
21655      *pHighwater = 0;  /* IMP: R-11967-56545 */
21656      *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
21657      break;
21658    }
21659
21660    default: {
21661      rc = SQLITE_ERROR;
21662    }
21663  }
21664  sqlite3_mutex_leave(db->mutex);
21665  return rc;
21666}
21667
21668/************** End of status.c **********************************************/
21669/************** Begin file date.c ********************************************/
21670/*
21671** 2003 October 31
21672**
21673** The author disclaims copyright to this source code.  In place of
21674** a legal notice, here is a blessing:
21675**
21676**    May you do good and not evil.
21677**    May you find forgiveness for yourself and forgive others.
21678**    May you share freely, never taking more than you give.
21679**
21680*************************************************************************
21681** This file contains the C functions that implement date and time
21682** functions for SQLite.
21683**
21684** There is only one exported symbol in this file - the function
21685** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
21686** All other code has file scope.
21687**
21688** SQLite processes all times and dates as julian day numbers.  The
21689** dates and times are stored as the number of days since noon
21690** in Greenwich on November 24, 4714 B.C. according to the Gregorian
21691** calendar system.
21692**
21693** 1970-01-01 00:00:00 is JD 2440587.5
21694** 2000-01-01 00:00:00 is JD 2451544.5
21695**
21696** This implementation requires years to be expressed as a 4-digit number
21697** which means that only dates between 0000-01-01 and 9999-12-31 can
21698** be represented, even though julian day numbers allow a much wider
21699** range of dates.
21700**
21701** The Gregorian calendar system is used for all dates and times,
21702** even those that predate the Gregorian calendar.  Historians usually
21703** use the julian calendar for dates prior to 1582-10-15 and for some
21704** dates afterwards, depending on locale.  Beware of this difference.
21705**
21706** The conversion algorithms are implemented based on descriptions
21707** in the following text:
21708**
21709**      Jean Meeus
21710**      Astronomical Algorithms, 2nd Edition, 1998
21711**      ISBN 0-943396-61-1
21712**      Willmann-Bell, Inc
21713**      Richmond, Virginia (USA)
21714*/
21715/* #include "sqliteInt.h" */
21716/* #include <stdlib.h> */
21717/* #include <assert.h> */
21718#include <time.h>
21719
21720#ifndef SQLITE_OMIT_DATETIME_FUNCS
21721
21722/*
21723** The MSVC CRT on Windows CE may not have a localtime() function.
21724** So declare a substitute.  The substitute function itself is
21725** defined in "os_win.c".
21726*/
21727#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
21728    (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
21729struct tm *__cdecl localtime(const time_t *);
21730#endif
21731
21732/*
21733** A structure for holding a single date and time.
21734*/
21735typedef struct DateTime DateTime;
21736struct DateTime {
21737  sqlite3_int64 iJD;  /* The julian day number times 86400000 */
21738  int Y, M, D;        /* Year, month, and day */
21739  int h, m;           /* Hour and minutes */
21740  int tz;             /* Timezone offset in minutes */
21741  double s;           /* Seconds */
21742  char validJD;       /* True (1) if iJD is valid */
21743  char rawS;          /* Raw numeric value stored in s */
21744  char validYMD;      /* True (1) if Y,M,D are valid */
21745  char validHMS;      /* True (1) if h,m,s are valid */
21746  char validTZ;       /* True (1) if tz is valid */
21747  char tzSet;         /* Timezone was set explicitly */
21748  char isError;       /* An overflow has occurred */
21749};
21750
21751
21752/*
21753** Convert zDate into one or more integers according to the conversion
21754** specifier zFormat.
21755**
21756** zFormat[] contains 4 characters for each integer converted, except for
21757** the last integer which is specified by three characters.  The meaning
21758** of a four-character format specifiers ABCD is:
21759**
21760**    A:   number of digits to convert.  Always "2" or "4".
21761**    B:   minimum value.  Always "0" or "1".
21762**    C:   maximum value, decoded as:
21763**           a:  12
21764**           b:  14
21765**           c:  24
21766**           d:  31
21767**           e:  59
21768**           f:  9999
21769**    D:   the separator character, or \000 to indicate this is the
21770**         last number to convert.
21771**
21772** Example:  To translate an ISO-8601 date YYYY-MM-DD, the format would
21773** be "40f-21a-20c".  The "40f-" indicates the 4-digit year followed by "-".
21774** The "21a-" indicates the 2-digit month followed by "-".  The "20c" indicates
21775** the 2-digit day which is the last integer in the set.
21776**
21777** The function returns the number of successful conversions.
21778*/
21779static int getDigits(const char *zDate, const char *zFormat, ...){
21780  /* The aMx[] array translates the 3rd character of each format
21781  ** spec into a max size:    a   b   c   d   e     f */
21782  static const u16 aMx[] = { 12, 14, 24, 31, 59, 9999 };
21783  va_list ap;
21784  int cnt = 0;
21785  char nextC;
21786  va_start(ap, zFormat);
21787  do{
21788    char N = zFormat[0] - '0';
21789    char min = zFormat[1] - '0';
21790    int val = 0;
21791    u16 max;
21792
21793    assert( zFormat[2]>='a' && zFormat[2]<='f' );
21794    max = aMx[zFormat[2] - 'a'];
21795    nextC = zFormat[3];
21796    val = 0;
21797    while( N-- ){
21798      if( !sqlite3Isdigit(*zDate) ){
21799        goto end_getDigits;
21800      }
21801      val = val*10 + *zDate - '0';
21802      zDate++;
21803    }
21804    if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){
21805      goto end_getDigits;
21806    }
21807    *va_arg(ap,int*) = val;
21808    zDate++;
21809    cnt++;
21810    zFormat += 4;
21811  }while( nextC );
21812end_getDigits:
21813  va_end(ap);
21814  return cnt;
21815}
21816
21817/*
21818** Parse a timezone extension on the end of a date-time.
21819** The extension is of the form:
21820**
21821**        (+/-)HH:MM
21822**
21823** Or the "zulu" notation:
21824**
21825**        Z
21826**
21827** If the parse is successful, write the number of minutes
21828** of change in p->tz and return 0.  If a parser error occurs,
21829** return non-zero.
21830**
21831** A missing specifier is not considered an error.
21832*/
21833static int parseTimezone(const char *zDate, DateTime *p){
21834  int sgn = 0;
21835  int nHr, nMn;
21836  int c;
21837  while( sqlite3Isspace(*zDate) ){ zDate++; }
21838  p->tz = 0;
21839  c = *zDate;
21840  if( c=='-' ){
21841    sgn = -1;
21842  }else if( c=='+' ){
21843    sgn = +1;
21844  }else if( c=='Z' || c=='z' ){
21845    zDate++;
21846    goto zulu_time;
21847  }else{
21848    return c!=0;
21849  }
21850  zDate++;
21851  if( getDigits(zDate, "20b:20e", &nHr, &nMn)!=2 ){
21852    return 1;
21853  }
21854  zDate += 5;
21855  p->tz = sgn*(nMn + nHr*60);
21856zulu_time:
21857  while( sqlite3Isspace(*zDate) ){ zDate++; }
21858  p->tzSet = 1;
21859  return *zDate!=0;
21860}
21861
21862/*
21863** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
21864** The HH, MM, and SS must each be exactly 2 digits.  The
21865** fractional seconds FFFF can be one or more digits.
21866**
21867** Return 1 if there is a parsing error and 0 on success.
21868*/
21869static int parseHhMmSs(const char *zDate, DateTime *p){
21870  int h, m, s;
21871  double ms = 0.0;
21872  if( getDigits(zDate, "20c:20e", &h, &m)!=2 ){
21873    return 1;
21874  }
21875  zDate += 5;
21876  if( *zDate==':' ){
21877    zDate++;
21878    if( getDigits(zDate, "20e", &s)!=1 ){
21879      return 1;
21880    }
21881    zDate += 2;
21882    if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
21883      double rScale = 1.0;
21884      zDate++;
21885      while( sqlite3Isdigit(*zDate) ){
21886        ms = ms*10.0 + *zDate - '0';
21887        rScale *= 10.0;
21888        zDate++;
21889      }
21890      ms /= rScale;
21891    }
21892  }else{
21893    s = 0;
21894  }
21895  p->validJD = 0;
21896  p->rawS = 0;
21897  p->validHMS = 1;
21898  p->h = h;
21899  p->m = m;
21900  p->s = s + ms;
21901  if( parseTimezone(zDate, p) ) return 1;
21902  p->validTZ = (p->tz!=0)?1:0;
21903  return 0;
21904}
21905
21906/*
21907** Put the DateTime object into its error state.
21908*/
21909static void datetimeError(DateTime *p){
21910  memset(p, 0, sizeof(*p));
21911  p->isError = 1;
21912}
21913
21914/*
21915** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
21916** that the YYYY-MM-DD is according to the Gregorian calendar.
21917**
21918** Reference:  Meeus page 61
21919*/
21920static void computeJD(DateTime *p){
21921  int Y, M, D, A, B, X1, X2;
21922
21923  if( p->validJD ) return;
21924  if( p->validYMD ){
21925    Y = p->Y;
21926    M = p->M;
21927    D = p->D;
21928  }else{
21929    Y = 2000;  /* If no YMD specified, assume 2000-Jan-01 */
21930    M = 1;
21931    D = 1;
21932  }
21933  if( Y<-4713 || Y>9999 || p->rawS ){
21934    datetimeError(p);
21935    return;
21936  }
21937  if( M<=2 ){
21938    Y--;
21939    M += 12;
21940  }
21941  A = Y/100;
21942  B = 2 - A + (A/4);
21943  X1 = 36525*(Y+4716)/100;
21944  X2 = 306001*(M+1)/10000;
21945  p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
21946  p->validJD = 1;
21947  if( p->validHMS ){
21948    p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
21949    if( p->validTZ ){
21950      p->iJD -= p->tz*60000;
21951      p->validYMD = 0;
21952      p->validHMS = 0;
21953      p->validTZ = 0;
21954    }
21955  }
21956}
21957
21958/*
21959** Parse dates of the form
21960**
21961**     YYYY-MM-DD HH:MM:SS.FFF
21962**     YYYY-MM-DD HH:MM:SS
21963**     YYYY-MM-DD HH:MM
21964**     YYYY-MM-DD
21965**
21966** Write the result into the DateTime structure and return 0
21967** on success and 1 if the input string is not a well-formed
21968** date.
21969*/
21970static int parseYyyyMmDd(const char *zDate, DateTime *p){
21971  int Y, M, D, neg;
21972
21973  if( zDate[0]=='-' ){
21974    zDate++;
21975    neg = 1;
21976  }else{
21977    neg = 0;
21978  }
21979  if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
21980    return 1;
21981  }
21982  zDate += 10;
21983  while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
21984  if( parseHhMmSs(zDate, p)==0 ){
21985    /* We got the time */
21986  }else if( *zDate==0 ){
21987    p->validHMS = 0;
21988  }else{
21989    return 1;
21990  }
21991  p->validJD = 0;
21992  p->validYMD = 1;
21993  p->Y = neg ? -Y : Y;
21994  p->M = M;
21995  p->D = D;
21996  if( p->validTZ ){
21997    computeJD(p);
21998  }
21999  return 0;
22000}
22001
22002/*
22003** Set the time to the current time reported by the VFS.
22004**
22005** Return the number of errors.
22006*/
22007static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
22008  p->iJD = sqlite3StmtCurrentTime(context);
22009  if( p->iJD>0 ){
22010    p->validJD = 1;
22011    return 0;
22012  }else{
22013    return 1;
22014  }
22015}
22016
22017/*
22018** Input "r" is a numeric quantity which might be a julian day number,
22019** or the number of seconds since 1970.  If the value if r is within
22020** range of a julian day number, install it as such and set validJD.
22021** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
22022*/
22023static void setRawDateNumber(DateTime *p, double r){
22024  p->s = r;
22025  p->rawS = 1;
22026  if( r>=0.0 && r<5373484.5 ){
22027    p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
22028    p->validJD = 1;
22029  }
22030}
22031
22032/*
22033** Attempt to parse the given string into a julian day number.  Return
22034** the number of errors.
22035**
22036** The following are acceptable forms for the input string:
22037**
22038**      YYYY-MM-DD HH:MM:SS.FFF  +/-HH:MM
22039**      DDDD.DD
22040**      now
22041**
22042** In the first form, the +/-HH:MM is always optional.  The fractional
22043** seconds extension (the ".FFF") is optional.  The seconds portion
22044** (":SS.FFF") is option.  The year and date can be omitted as long
22045** as there is a time string.  The time string can be omitted as long
22046** as there is a year and date.
22047*/
22048static int parseDateOrTime(
22049  sqlite3_context *context,
22050  const char *zDate,
22051  DateTime *p
22052){
22053  double r;
22054  if( parseYyyyMmDd(zDate,p)==0 ){
22055    return 0;
22056  }else if( parseHhMmSs(zDate, p)==0 ){
22057    return 0;
22058  }else if( sqlite3StrICmp(zDate,"now")==0 && sqlite3NotPureFunc(context) ){
22059    return setDateTimeToCurrent(context, p);
22060  }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8)>0 ){
22061    setRawDateNumber(p, r);
22062    return 0;
22063  }
22064  return 1;
22065}
22066
22067/* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
22068** Multiplying this by 86400000 gives 464269060799999 as the maximum value
22069** for DateTime.iJD.
22070**
22071** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with
22072** such a large integer literal, so we have to encode it.
22073*/
22074#define INT_464269060799999  ((((i64)0x1a640)<<32)|0x1072fdff)
22075
22076/*
22077** Return TRUE if the given julian day number is within range.
22078**
22079** The input is the JulianDay times 86400000.
22080*/
22081static int validJulianDay(sqlite3_int64 iJD){
22082  return iJD>=0 && iJD<=INT_464269060799999;
22083}
22084
22085/*
22086** Compute the Year, Month, and Day from the julian day number.
22087*/
22088static void computeYMD(DateTime *p){
22089  int Z, A, B, C, D, E, X1;
22090  if( p->validYMD ) return;
22091  if( !p->validJD ){
22092    p->Y = 2000;
22093    p->M = 1;
22094    p->D = 1;
22095  }else if( !validJulianDay(p->iJD) ){
22096    datetimeError(p);
22097    return;
22098  }else{
22099    Z = (int)((p->iJD + 43200000)/86400000);
22100    A = (int)((Z - 1867216.25)/36524.25);
22101    A = Z + 1 + A - (A/4);
22102    B = A + 1524;
22103    C = (int)((B - 122.1)/365.25);
22104    D = (36525*(C&32767))/100;
22105    E = (int)((B-D)/30.6001);
22106    X1 = (int)(30.6001*E);
22107    p->D = B - D - X1;
22108    p->M = E<14 ? E-1 : E-13;
22109    p->Y = p->M>2 ? C - 4716 : C - 4715;
22110  }
22111  p->validYMD = 1;
22112}
22113
22114/*
22115** Compute the Hour, Minute, and Seconds from the julian day number.
22116*/
22117static void computeHMS(DateTime *p){
22118  int s;
22119  if( p->validHMS ) return;
22120  computeJD(p);
22121  s = (int)((p->iJD + 43200000) % 86400000);
22122  p->s = s/1000.0;
22123  s = (int)p->s;
22124  p->s -= s;
22125  p->h = s/3600;
22126  s -= p->h*3600;
22127  p->m = s/60;
22128  p->s += s - p->m*60;
22129  p->rawS = 0;
22130  p->validHMS = 1;
22131}
22132
22133/*
22134** Compute both YMD and HMS
22135*/
22136static void computeYMD_HMS(DateTime *p){
22137  computeYMD(p);
22138  computeHMS(p);
22139}
22140
22141/*
22142** Clear the YMD and HMS and the TZ
22143*/
22144static void clearYMD_HMS_TZ(DateTime *p){
22145  p->validYMD = 0;
22146  p->validHMS = 0;
22147  p->validTZ = 0;
22148}
22149
22150#ifndef SQLITE_OMIT_LOCALTIME
22151/*
22152** On recent Windows platforms, the localtime_s() function is available
22153** as part of the "Secure CRT". It is essentially equivalent to
22154** localtime_r() available under most POSIX platforms, except that the
22155** order of the parameters is reversed.
22156**
22157** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
22158**
22159** If the user has not indicated to use localtime_r() or localtime_s()
22160** already, check for an MSVC build environment that provides
22161** localtime_s().
22162*/
22163#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S \
22164    && defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
22165#undef  HAVE_LOCALTIME_S
22166#define HAVE_LOCALTIME_S 1
22167#endif
22168
22169/*
22170** The following routine implements the rough equivalent of localtime_r()
22171** using whatever operating-system specific localtime facility that
22172** is available.  This routine returns 0 on success and
22173** non-zero on any kind of error.
22174**
22175** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
22176** routine will always fail.
22177**
22178** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
22179** library function localtime_r() is used to assist in the calculation of
22180** local time.
22181*/
22182static int osLocaltime(time_t *t, struct tm *pTm){
22183  int rc;
22184#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S
22185  struct tm *pX;
22186#if SQLITE_THREADSAFE>0
22187  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
22188#endif
22189  sqlite3_mutex_enter(mutex);
22190  pX = localtime(t);
22191#ifndef SQLITE_UNTESTABLE
22192  if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
22193#endif
22194  if( pX ) *pTm = *pX;
22195  sqlite3_mutex_leave(mutex);
22196  rc = pX==0;
22197#else
22198#ifndef SQLITE_UNTESTABLE
22199  if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
22200#endif
22201#if HAVE_LOCALTIME_R
22202  rc = localtime_r(t, pTm)==0;
22203#else
22204  rc = localtime_s(pTm, t);
22205#endif /* HAVE_LOCALTIME_R */
22206#endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
22207  return rc;
22208}
22209#endif /* SQLITE_OMIT_LOCALTIME */
22210
22211
22212#ifndef SQLITE_OMIT_LOCALTIME
22213/*
22214** Compute the difference (in milliseconds) between localtime and UTC
22215** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
22216** return this value and set *pRc to SQLITE_OK.
22217**
22218** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
22219** is undefined in this case.
22220*/
22221static sqlite3_int64 localtimeOffset(
22222  DateTime *p,                    /* Date at which to calculate offset */
22223  sqlite3_context *pCtx,          /* Write error here if one occurs */
22224  int *pRc                        /* OUT: Error code. SQLITE_OK or ERROR */
22225){
22226  DateTime x, y;
22227  time_t t;
22228  struct tm sLocal;
22229
22230  /* Initialize the contents of sLocal to avoid a compiler warning. */
22231  memset(&sLocal, 0, sizeof(sLocal));
22232
22233  x = *p;
22234  computeYMD_HMS(&x);
22235  if( x.Y<1971 || x.Y>=2038 ){
22236    /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
22237    ** works for years between 1970 and 2037. For dates outside this range,
22238    ** SQLite attempts to map the year into an equivalent year within this
22239    ** range, do the calculation, then map the year back.
22240    */
22241    x.Y = 2000;
22242    x.M = 1;
22243    x.D = 1;
22244    x.h = 0;
22245    x.m = 0;
22246    x.s = 0.0;
22247  } else {
22248    int s = (int)(x.s + 0.5);
22249    x.s = s;
22250  }
22251  x.tz = 0;
22252  x.validJD = 0;
22253  computeJD(&x);
22254  t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
22255  if( osLocaltime(&t, &sLocal) ){
22256    sqlite3_result_error(pCtx, "local time unavailable", -1);
22257    *pRc = SQLITE_ERROR;
22258    return 0;
22259  }
22260  y.Y = sLocal.tm_year + 1900;
22261  y.M = sLocal.tm_mon + 1;
22262  y.D = sLocal.tm_mday;
22263  y.h = sLocal.tm_hour;
22264  y.m = sLocal.tm_min;
22265  y.s = sLocal.tm_sec;
22266  y.validYMD = 1;
22267  y.validHMS = 1;
22268  y.validJD = 0;
22269  y.rawS = 0;
22270  y.validTZ = 0;
22271  y.isError = 0;
22272  computeJD(&y);
22273  *pRc = SQLITE_OK;
22274  return y.iJD - x.iJD;
22275}
22276#endif /* SQLITE_OMIT_LOCALTIME */
22277
22278/*
22279** The following table defines various date transformations of the form
22280**
22281**            'NNN days'
22282**
22283** Where NNN is an arbitrary floating-point number and "days" can be one
22284** of several units of time.
22285*/
22286static const struct {
22287  u8 eType;           /* Transformation type code */
22288  u8 nName;           /* Length of th name */
22289  char *zName;        /* Name of the transformation */
22290  double rLimit;      /* Maximum NNN value for this transform */
22291  double rXform;      /* Constant used for this transform */
22292} aXformType[] = {
22293  { 0, 6, "second", 464269060800.0, 1000.0         },
22294  { 0, 6, "minute", 7737817680.0,   60000.0        },
22295  { 0, 4, "hour",   128963628.0,    3600000.0      },
22296  { 0, 3, "day",    5373485.0,      86400000.0     },
22297  { 1, 5, "month",  176546.0,       2592000000.0   },
22298  { 2, 4, "year",   14713.0,        31536000000.0  },
22299};
22300
22301/*
22302** Process a modifier to a date-time stamp.  The modifiers are
22303** as follows:
22304**
22305**     NNN days
22306**     NNN hours
22307**     NNN minutes
22308**     NNN.NNNN seconds
22309**     NNN months
22310**     NNN years
22311**     start of month
22312**     start of year
22313**     start of week
22314**     start of day
22315**     weekday N
22316**     unixepoch
22317**     localtime
22318**     utc
22319**
22320** Return 0 on success and 1 if there is any kind of error. If the error
22321** is in a system call (i.e. localtime()), then an error message is written
22322** to context pCtx. If the error is an unrecognized modifier, no error is
22323** written to pCtx.
22324*/
22325static int parseModifier(
22326  sqlite3_context *pCtx,      /* Function context */
22327  const char *z,              /* The text of the modifier */
22328  int n,                      /* Length of zMod in bytes */
22329  DateTime *p                 /* The date/time value to be modified */
22330){
22331  int rc = 1;
22332  double r;
22333  switch(sqlite3UpperToLower[(u8)z[0]] ){
22334#ifndef SQLITE_OMIT_LOCALTIME
22335    case 'l': {
22336      /*    localtime
22337      **
22338      ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
22339      ** show local time.
22340      */
22341      if( sqlite3_stricmp(z, "localtime")==0 && sqlite3NotPureFunc(pCtx) ){
22342        computeJD(p);
22343        p->iJD += localtimeOffset(p, pCtx, &rc);
22344        clearYMD_HMS_TZ(p);
22345      }
22346      break;
22347    }
22348#endif
22349    case 'u': {
22350      /*
22351      **    unixepoch
22352      **
22353      ** Treat the current value of p->s as the number of
22354      ** seconds since 1970.  Convert to a real julian day number.
22355      */
22356      if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
22357        r = p->s*1000.0 + 210866760000000.0;
22358        if( r>=0.0 && r<464269060800000.0 ){
22359          clearYMD_HMS_TZ(p);
22360          p->iJD = (sqlite3_int64)(r + 0.5);
22361          p->validJD = 1;
22362          p->rawS = 0;
22363          rc = 0;
22364        }
22365      }
22366#ifndef SQLITE_OMIT_LOCALTIME
22367      else if( sqlite3_stricmp(z, "utc")==0 && sqlite3NotPureFunc(pCtx) ){
22368        if( p->tzSet==0 ){
22369          sqlite3_int64 c1;
22370          computeJD(p);
22371          c1 = localtimeOffset(p, pCtx, &rc);
22372          if( rc==SQLITE_OK ){
22373            p->iJD -= c1;
22374            clearYMD_HMS_TZ(p);
22375            p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
22376          }
22377          p->tzSet = 1;
22378        }else{
22379          rc = SQLITE_OK;
22380        }
22381      }
22382#endif
22383      break;
22384    }
22385    case 'w': {
22386      /*
22387      **    weekday N
22388      **
22389      ** Move the date to the same time on the next occurrence of
22390      ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
22391      ** date is already on the appropriate weekday, this is a no-op.
22392      */
22393      if( sqlite3_strnicmp(z, "weekday ", 8)==0
22394               && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)>0
22395               && (n=(int)r)==r && n>=0 && r<7 ){
22396        sqlite3_int64 Z;
22397        computeYMD_HMS(p);
22398        p->validTZ = 0;
22399        p->validJD = 0;
22400        computeJD(p);
22401        Z = ((p->iJD + 129600000)/86400000) % 7;
22402        if( Z>n ) Z -= 7;
22403        p->iJD += (n - Z)*86400000;
22404        clearYMD_HMS_TZ(p);
22405        rc = 0;
22406      }
22407      break;
22408    }
22409    case 's': {
22410      /*
22411      **    start of TTTTT
22412      **
22413      ** Move the date backwards to the beginning of the current day,
22414      ** or month or year.
22415      */
22416      if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break;
22417      if( !p->validJD && !p->validYMD && !p->validHMS ) break;
22418      z += 9;
22419      computeYMD(p);
22420      p->validHMS = 1;
22421      p->h = p->m = 0;
22422      p->s = 0.0;
22423      p->rawS = 0;
22424      p->validTZ = 0;
22425      p->validJD = 0;
22426      if( sqlite3_stricmp(z,"month")==0 ){
22427        p->D = 1;
22428        rc = 0;
22429      }else if( sqlite3_stricmp(z,"year")==0 ){
22430        p->M = 1;
22431        p->D = 1;
22432        rc = 0;
22433      }else if( sqlite3_stricmp(z,"day")==0 ){
22434        rc = 0;
22435      }
22436      break;
22437    }
22438    case '+':
22439    case '-':
22440    case '0':
22441    case '1':
22442    case '2':
22443    case '3':
22444    case '4':
22445    case '5':
22446    case '6':
22447    case '7':
22448    case '8':
22449    case '9': {
22450      double rRounder;
22451      int i;
22452      for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
22453      if( sqlite3AtoF(z, &r, n, SQLITE_UTF8)<=0 ){
22454        rc = 1;
22455        break;
22456      }
22457      if( z[n]==':' ){
22458        /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
22459        ** specified number of hours, minutes, seconds, and fractional seconds
22460        ** to the time.  The ".FFF" may be omitted.  The ":SS.FFF" may be
22461        ** omitted.
22462        */
22463        const char *z2 = z;
22464        DateTime tx;
22465        sqlite3_int64 day;
22466        if( !sqlite3Isdigit(*z2) ) z2++;
22467        memset(&tx, 0, sizeof(tx));
22468        if( parseHhMmSs(z2, &tx) ) break;
22469        computeJD(&tx);
22470        tx.iJD -= 43200000;
22471        day = tx.iJD/86400000;
22472        tx.iJD -= day*86400000;
22473        if( z[0]=='-' ) tx.iJD = -tx.iJD;
22474        computeJD(p);
22475        clearYMD_HMS_TZ(p);
22476        p->iJD += tx.iJD;
22477        rc = 0;
22478        break;
22479      }
22480
22481      /* If control reaches this point, it means the transformation is
22482      ** one of the forms like "+NNN days".  */
22483      z += n;
22484      while( sqlite3Isspace(*z) ) z++;
22485      n = sqlite3Strlen30(z);
22486      if( n>10 || n<3 ) break;
22487      if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
22488      computeJD(p);
22489      rc = 1;
22490      rRounder = r<0 ? -0.5 : +0.5;
22491      for(i=0; i<ArraySize(aXformType); i++){
22492        if( aXformType[i].nName==n
22493         && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
22494         && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
22495        ){
22496          switch( aXformType[i].eType ){
22497            case 1: { /* Special processing to add months */
22498              int x;
22499              computeYMD_HMS(p);
22500              p->M += (int)r;
22501              x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
22502              p->Y += x;
22503              p->M -= x*12;
22504              p->validJD = 0;
22505              r -= (int)r;
22506              break;
22507            }
22508            case 2: { /* Special processing to add years */
22509              int y = (int)r;
22510              computeYMD_HMS(p);
22511              p->Y += y;
22512              p->validJD = 0;
22513              r -= (int)r;
22514              break;
22515            }
22516          }
22517          computeJD(p);
22518          p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder);
22519          rc = 0;
22520          break;
22521        }
22522      }
22523      clearYMD_HMS_TZ(p);
22524      break;
22525    }
22526    default: {
22527      break;
22528    }
22529  }
22530  return rc;
22531}
22532
22533/*
22534** Process time function arguments.  argv[0] is a date-time stamp.
22535** argv[1] and following are modifiers.  Parse them all and write
22536** the resulting time into the DateTime structure p.  Return 0
22537** on success and 1 if there are any errors.
22538**
22539** If there are zero parameters (if even argv[0] is undefined)
22540** then assume a default value of "now" for argv[0].
22541*/
22542static int isDate(
22543  sqlite3_context *context,
22544  int argc,
22545  sqlite3_value **argv,
22546  DateTime *p
22547){
22548  int i, n;
22549  const unsigned char *z;
22550  int eType;
22551  memset(p, 0, sizeof(*p));
22552  if( argc==0 ){
22553    return setDateTimeToCurrent(context, p);
22554  }
22555  if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
22556                   || eType==SQLITE_INTEGER ){
22557    setRawDateNumber(p, sqlite3_value_double(argv[0]));
22558  }else{
22559    z = sqlite3_value_text(argv[0]);
22560    if( !z || parseDateOrTime(context, (char*)z, p) ){
22561      return 1;
22562    }
22563  }
22564  for(i=1; i<argc; i++){
22565    z = sqlite3_value_text(argv[i]);
22566    n = sqlite3_value_bytes(argv[i]);
22567    if( z==0 || parseModifier(context, (char*)z, n, p) ) return 1;
22568  }
22569  computeJD(p);
22570  if( p->isError || !validJulianDay(p->iJD) ) return 1;
22571  return 0;
22572}
22573
22574
22575/*
22576** The following routines implement the various date and time functions
22577** of SQLite.
22578*/
22579
22580/*
22581**    julianday( TIMESTRING, MOD, MOD, ...)
22582**
22583** Return the julian day number of the date specified in the arguments
22584*/
22585static void juliandayFunc(
22586  sqlite3_context *context,
22587  int argc,
22588  sqlite3_value **argv
22589){
22590  DateTime x;
22591  if( isDate(context, argc, argv, &x)==0 ){
22592    computeJD(&x);
22593    sqlite3_result_double(context, x.iJD/86400000.0);
22594  }
22595}
22596
22597/*
22598**    datetime( TIMESTRING, MOD, MOD, ...)
22599**
22600** Return YYYY-MM-DD HH:MM:SS
22601*/
22602static void datetimeFunc(
22603  sqlite3_context *context,
22604  int argc,
22605  sqlite3_value **argv
22606){
22607  DateTime x;
22608  if( isDate(context, argc, argv, &x)==0 ){
22609    char zBuf[100];
22610    computeYMD_HMS(&x);
22611    sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
22612                     x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
22613    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
22614  }
22615}
22616
22617/*
22618**    time( TIMESTRING, MOD, MOD, ...)
22619**
22620** Return HH:MM:SS
22621*/
22622static void timeFunc(
22623  sqlite3_context *context,
22624  int argc,
22625  sqlite3_value **argv
22626){
22627  DateTime x;
22628  if( isDate(context, argc, argv, &x)==0 ){
22629    char zBuf[100];
22630    computeHMS(&x);
22631    sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
22632    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
22633  }
22634}
22635
22636/*
22637**    date( TIMESTRING, MOD, MOD, ...)
22638**
22639** Return YYYY-MM-DD
22640*/
22641static void dateFunc(
22642  sqlite3_context *context,
22643  int argc,
22644  sqlite3_value **argv
22645){
22646  DateTime x;
22647  if( isDate(context, argc, argv, &x)==0 ){
22648    char zBuf[100];
22649    computeYMD(&x);
22650    sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
22651    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
22652  }
22653}
22654
22655/*
22656**    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
22657**
22658** Return a string described by FORMAT.  Conversions as follows:
22659**
22660**   %d  day of month
22661**   %f  ** fractional seconds  SS.SSS
22662**   %H  hour 00-24
22663**   %j  day of year 000-366
22664**   %J  ** julian day number
22665**   %m  month 01-12
22666**   %M  minute 00-59
22667**   %s  seconds since 1970-01-01
22668**   %S  seconds 00-59
22669**   %w  day of week 0-6  sunday==0
22670**   %W  week of year 00-53
22671**   %Y  year 0000-9999
22672**   %%  %
22673*/
22674static void strftimeFunc(
22675  sqlite3_context *context,
22676  int argc,
22677  sqlite3_value **argv
22678){
22679  DateTime x;
22680  u64 n;
22681  size_t i,j;
22682  char *z;
22683  sqlite3 *db;
22684  const char *zFmt;
22685  char zBuf[100];
22686  if( argc==0 ) return;
22687  zFmt = (const char*)sqlite3_value_text(argv[0]);
22688  if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
22689  db = sqlite3_context_db_handle(context);
22690  for(i=0, n=1; zFmt[i]; i++, n++){
22691    if( zFmt[i]=='%' ){
22692      switch( zFmt[i+1] ){
22693        case 'd':
22694        case 'H':
22695        case 'm':
22696        case 'M':
22697        case 'S':
22698        case 'W':
22699          n++;
22700          /* fall thru */
22701        case 'w':
22702        case '%':
22703          break;
22704        case 'f':
22705          n += 8;
22706          break;
22707        case 'j':
22708          n += 3;
22709          break;
22710        case 'Y':
22711          n += 8;
22712          break;
22713        case 's':
22714        case 'J':
22715          n += 50;
22716          break;
22717        default:
22718          return;  /* ERROR.  return a NULL */
22719      }
22720      i++;
22721    }
22722  }
22723  testcase( n==sizeof(zBuf)-1 );
22724  testcase( n==sizeof(zBuf) );
22725  testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
22726  testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
22727  if( n<sizeof(zBuf) ){
22728    z = zBuf;
22729  }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
22730    sqlite3_result_error_toobig(context);
22731    return;
22732  }else{
22733    z = sqlite3DbMallocRawNN(db, (int)n);
22734    if( z==0 ){
22735      sqlite3_result_error_nomem(context);
22736      return;
22737    }
22738  }
22739  computeJD(&x);
22740  computeYMD_HMS(&x);
22741  for(i=j=0; zFmt[i]; i++){
22742    if( zFmt[i]!='%' ){
22743      z[j++] = zFmt[i];
22744    }else{
22745      i++;
22746      switch( zFmt[i] ){
22747        case 'd':  sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
22748        case 'f': {
22749          double s = x.s;
22750          if( s>59.999 ) s = 59.999;
22751          sqlite3_snprintf(7, &z[j],"%06.3f", s);
22752          j += sqlite3Strlen30(&z[j]);
22753          break;
22754        }
22755        case 'H':  sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
22756        case 'W': /* Fall thru */
22757        case 'j': {
22758          int nDay;             /* Number of days since 1st day of year */
22759          DateTime y = x;
22760          y.validJD = 0;
22761          y.M = 1;
22762          y.D = 1;
22763          computeJD(&y);
22764          nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
22765          if( zFmt[i]=='W' ){
22766            int wd;   /* 0=Monday, 1=Tuesday, ... 6=Sunday */
22767            wd = (int)(((x.iJD+43200000)/86400000)%7);
22768            sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
22769            j += 2;
22770          }else{
22771            sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
22772            j += 3;
22773          }
22774          break;
22775        }
22776        case 'J': {
22777          sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
22778          j+=sqlite3Strlen30(&z[j]);
22779          break;
22780        }
22781        case 'm':  sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
22782        case 'M':  sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
22783        case 's': {
22784          sqlite3_snprintf(30,&z[j],"%lld",
22785                           (i64)(x.iJD/1000 - 21086676*(i64)10000));
22786          j += sqlite3Strlen30(&z[j]);
22787          break;
22788        }
22789        case 'S':  sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
22790        case 'w': {
22791          z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
22792          break;
22793        }
22794        case 'Y': {
22795          sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
22796          break;
22797        }
22798        default:   z[j++] = '%'; break;
22799      }
22800    }
22801  }
22802  z[j] = 0;
22803  sqlite3_result_text(context, z, -1,
22804                      z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
22805}
22806
22807/*
22808** current_time()
22809**
22810** This function returns the same value as time('now').
22811*/
22812static void ctimeFunc(
22813  sqlite3_context *context,
22814  int NotUsed,
22815  sqlite3_value **NotUsed2
22816){
22817  UNUSED_PARAMETER2(NotUsed, NotUsed2);
22818  timeFunc(context, 0, 0);
22819}
22820
22821/*
22822** current_date()
22823**
22824** This function returns the same value as date('now').
22825*/
22826static void cdateFunc(
22827  sqlite3_context *context,
22828  int NotUsed,
22829  sqlite3_value **NotUsed2
22830){
22831  UNUSED_PARAMETER2(NotUsed, NotUsed2);
22832  dateFunc(context, 0, 0);
22833}
22834
22835/*
22836** current_timestamp()
22837**
22838** This function returns the same value as datetime('now').
22839*/
22840static void ctimestampFunc(
22841  sqlite3_context *context,
22842  int NotUsed,
22843  sqlite3_value **NotUsed2
22844){
22845  UNUSED_PARAMETER2(NotUsed, NotUsed2);
22846  datetimeFunc(context, 0, 0);
22847}
22848#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
22849
22850#ifdef SQLITE_OMIT_DATETIME_FUNCS
22851/*
22852** If the library is compiled to omit the full-scale date and time
22853** handling (to get a smaller binary), the following minimal version
22854** of the functions current_time(), current_date() and current_timestamp()
22855** are included instead. This is to support column declarations that
22856** include "DEFAULT CURRENT_TIME" etc.
22857**
22858** This function uses the C-library functions time(), gmtime()
22859** and strftime(). The format string to pass to strftime() is supplied
22860** as the user-data for the function.
22861*/
22862static void currentTimeFunc(
22863  sqlite3_context *context,
22864  int argc,
22865  sqlite3_value **argv
22866){
22867  time_t t;
22868  char *zFormat = (char *)sqlite3_user_data(context);
22869  sqlite3_int64 iT;
22870  struct tm *pTm;
22871  struct tm sNow;
22872  char zBuf[20];
22873
22874  UNUSED_PARAMETER(argc);
22875  UNUSED_PARAMETER(argv);
22876
22877  iT = sqlite3StmtCurrentTime(context);
22878  if( iT<=0 ) return;
22879  t = iT/1000 - 10000*(sqlite3_int64)21086676;
22880#if HAVE_GMTIME_R
22881  pTm = gmtime_r(&t, &sNow);
22882#else
22883  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
22884  pTm = gmtime(&t);
22885  if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
22886  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
22887#endif
22888  if( pTm ){
22889    strftime(zBuf, 20, zFormat, &sNow);
22890    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
22891  }
22892}
22893#endif
22894
22895/*
22896** This function registered all of the above C functions as SQL
22897** functions.  This should be the only routine in this file with
22898** external linkage.
22899*/
22900SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
22901  static FuncDef aDateTimeFuncs[] = {
22902#ifndef SQLITE_OMIT_DATETIME_FUNCS
22903    PURE_DATE(julianday,        -1, 0, 0, juliandayFunc ),
22904    PURE_DATE(date,             -1, 0, 0, dateFunc      ),
22905    PURE_DATE(time,             -1, 0, 0, timeFunc      ),
22906    PURE_DATE(datetime,         -1, 0, 0, datetimeFunc  ),
22907    PURE_DATE(strftime,         -1, 0, 0, strftimeFunc  ),
22908    DFUNCTION(current_time,      0, 0, 0, ctimeFunc     ),
22909    DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
22910    DFUNCTION(current_date,      0, 0, 0, cdateFunc     ),
22911#else
22912    STR_FUNCTION(current_time,      0, "%H:%M:%S",          0, currentTimeFunc),
22913    STR_FUNCTION(current_date,      0, "%Y-%m-%d",          0, currentTimeFunc),
22914    STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
22915#endif
22916  };
22917  sqlite3InsertBuiltinFuncs(aDateTimeFuncs, ArraySize(aDateTimeFuncs));
22918}
22919
22920/************** End of date.c ************************************************/
22921/************** Begin file os.c **********************************************/
22922/*
22923** 2005 November 29
22924**
22925** The author disclaims copyright to this source code.  In place of
22926** a legal notice, here is a blessing:
22927**
22928**    May you do good and not evil.
22929**    May you find forgiveness for yourself and forgive others.
22930**    May you share freely, never taking more than you give.
22931**
22932******************************************************************************
22933**
22934** This file contains OS interface code that is common to all
22935** architectures.
22936*/
22937/* #include "sqliteInt.h" */
22938
22939/*
22940** If we compile with the SQLITE_TEST macro set, then the following block
22941** of code will give us the ability to simulate a disk I/O error.  This
22942** is used for testing the I/O recovery logic.
22943*/
22944#if defined(SQLITE_TEST)
22945SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
22946SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
22947SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
22948SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
22949SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
22950SQLITE_API int sqlite3_diskfull_pending = 0;
22951SQLITE_API int sqlite3_diskfull = 0;
22952#endif /* defined(SQLITE_TEST) */
22953
22954/*
22955** When testing, also keep a count of the number of open files.
22956*/
22957#if defined(SQLITE_TEST)
22958SQLITE_API int sqlite3_open_file_count = 0;
22959#endif /* defined(SQLITE_TEST) */
22960
22961/*
22962** The default SQLite sqlite3_vfs implementations do not allocate
22963** memory (actually, os_unix.c allocates a small amount of memory
22964** from within OsOpen()), but some third-party implementations may.
22965** So we test the effects of a malloc() failing and the sqlite3OsXXX()
22966** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
22967**
22968** The following functions are instrumented for malloc() failure
22969** testing:
22970**
22971**     sqlite3OsRead()
22972**     sqlite3OsWrite()
22973**     sqlite3OsSync()
22974**     sqlite3OsFileSize()
22975**     sqlite3OsLock()
22976**     sqlite3OsCheckReservedLock()
22977**     sqlite3OsFileControl()
22978**     sqlite3OsShmMap()
22979**     sqlite3OsOpen()
22980**     sqlite3OsDelete()
22981**     sqlite3OsAccess()
22982**     sqlite3OsFullPathname()
22983**
22984*/
22985#if defined(SQLITE_TEST)
22986SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
22987  #define DO_OS_MALLOC_TEST(x)                                       \
22988  if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
22989    void *pTstAlloc = sqlite3Malloc(10);                             \
22990    if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT;                  \
22991    sqlite3_free(pTstAlloc);                                         \
22992  }
22993#else
22994  #define DO_OS_MALLOC_TEST(x)
22995#endif
22996
22997/*
22998** The following routines are convenience wrappers around methods
22999** of the sqlite3_file object.  This is mostly just syntactic sugar. All
23000** of this would be completely automatic if SQLite were coded using
23001** C++ instead of plain old C.
23002*/
23003SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file *pId){
23004  if( pId->pMethods ){
23005    pId->pMethods->xClose(pId);
23006    pId->pMethods = 0;
23007  }
23008}
23009SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
23010  DO_OS_MALLOC_TEST(id);
23011  return id->pMethods->xRead(id, pBuf, amt, offset);
23012}
23013SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
23014  DO_OS_MALLOC_TEST(id);
23015  return id->pMethods->xWrite(id, pBuf, amt, offset);
23016}
23017SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
23018  return id->pMethods->xTruncate(id, size);
23019}
23020SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
23021  DO_OS_MALLOC_TEST(id);
23022  return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK;
23023}
23024SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
23025  DO_OS_MALLOC_TEST(id);
23026  return id->pMethods->xFileSize(id, pSize);
23027}
23028SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
23029  DO_OS_MALLOC_TEST(id);
23030  return id->pMethods->xLock(id, lockType);
23031}
23032SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
23033  return id->pMethods->xUnlock(id, lockType);
23034}
23035SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
23036  DO_OS_MALLOC_TEST(id);
23037  return id->pMethods->xCheckReservedLock(id, pResOut);
23038}
23039
23040/*
23041** Use sqlite3OsFileControl() when we are doing something that might fail
23042** and we need to know about the failures.  Use sqlite3OsFileControlHint()
23043** when simply tossing information over the wall to the VFS and we do not
23044** really care if the VFS receives and understands the information since it
23045** is only a hint and can be safely ignored.  The sqlite3OsFileControlHint()
23046** routine has no return value since the return value would be meaningless.
23047*/
23048SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
23049  if( id->pMethods==0 ) return SQLITE_NOTFOUND;
23050#ifdef SQLITE_TEST
23051  if( op!=SQLITE_FCNTL_COMMIT_PHASETWO
23052   && op!=SQLITE_FCNTL_LOCK_TIMEOUT
23053  ){
23054    /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
23055    ** is using a regular VFS, it is called after the corresponding
23056    ** transaction has been committed. Injecting a fault at this point
23057    ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
23058    ** but the transaction is committed anyway.
23059    **
23060    ** The core must call OsFileControl() though, not OsFileControlHint(),
23061    ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
23062    ** means the commit really has failed and an error should be returned
23063    ** to the user.  */
23064    DO_OS_MALLOC_TEST(id);
23065  }
23066#endif
23067  return id->pMethods->xFileControl(id, op, pArg);
23068}
23069SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
23070  if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
23071}
23072
23073SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
23074  int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
23075  return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
23076}
23077SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
23078  return id->pMethods->xDeviceCharacteristics(id);
23079}
23080#ifndef SQLITE_OMIT_WAL
23081SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
23082  return id->pMethods->xShmLock(id, offset, n, flags);
23083}
23084SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
23085  id->pMethods->xShmBarrier(id);
23086}
23087SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
23088  return id->pMethods->xShmUnmap(id, deleteFlag);
23089}
23090SQLITE_PRIVATE int sqlite3OsShmMap(
23091  sqlite3_file *id,               /* Database file handle */
23092  int iPage,
23093  int pgsz,
23094  int bExtend,                    /* True to extend file if necessary */
23095  void volatile **pp              /* OUT: Pointer to mapping */
23096){
23097  DO_OS_MALLOC_TEST(id);
23098  return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
23099}
23100#endif /* SQLITE_OMIT_WAL */
23101
23102#if SQLITE_MAX_MMAP_SIZE>0
23103/* The real implementation of xFetch and xUnfetch */
23104SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
23105  DO_OS_MALLOC_TEST(id);
23106  return id->pMethods->xFetch(id, iOff, iAmt, pp);
23107}
23108SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
23109  return id->pMethods->xUnfetch(id, iOff, p);
23110}
23111#else
23112/* No-op stubs to use when memory-mapped I/O is disabled */
23113SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
23114  *pp = 0;
23115  return SQLITE_OK;
23116}
23117SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
23118  return SQLITE_OK;
23119}
23120#endif
23121
23122/*
23123** The next group of routines are convenience wrappers around the
23124** VFS methods.
23125*/
23126SQLITE_PRIVATE int sqlite3OsOpen(
23127  sqlite3_vfs *pVfs,
23128  const char *zPath,
23129  sqlite3_file *pFile,
23130  int flags,
23131  int *pFlagsOut
23132){
23133  int rc;
23134  DO_OS_MALLOC_TEST(0);
23135  /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
23136  ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
23137  ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
23138  ** reaching the VFS. */
23139  rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut);
23140  assert( rc==SQLITE_OK || pFile->pMethods==0 );
23141  return rc;
23142}
23143SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
23144  DO_OS_MALLOC_TEST(0);
23145  assert( dirSync==0 || dirSync==1 );
23146  return pVfs->xDelete(pVfs, zPath, dirSync);
23147}
23148SQLITE_PRIVATE int sqlite3OsAccess(
23149  sqlite3_vfs *pVfs,
23150  const char *zPath,
23151  int flags,
23152  int *pResOut
23153){
23154  DO_OS_MALLOC_TEST(0);
23155  return pVfs->xAccess(pVfs, zPath, flags, pResOut);
23156}
23157SQLITE_PRIVATE int sqlite3OsFullPathname(
23158  sqlite3_vfs *pVfs,
23159  const char *zPath,
23160  int nPathOut,
23161  char *zPathOut
23162){
23163  DO_OS_MALLOC_TEST(0);
23164  zPathOut[0] = 0;
23165  return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
23166}
23167#ifndef SQLITE_OMIT_LOAD_EXTENSION
23168SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
23169  return pVfs->xDlOpen(pVfs, zPath);
23170}
23171SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
23172  pVfs->xDlError(pVfs, nByte, zBufOut);
23173}
23174SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
23175  return pVfs->xDlSym(pVfs, pHdle, zSym);
23176}
23177SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
23178  pVfs->xDlClose(pVfs, pHandle);
23179}
23180#endif /* SQLITE_OMIT_LOAD_EXTENSION */
23181SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
23182  if( sqlite3Config.iPrngSeed ){
23183    memset(zBufOut, 0, nByte);
23184    if( ALWAYS(nByte>(signed)sizeof(unsigned)) ) nByte = sizeof(unsigned int);
23185    memcpy(zBufOut, &sqlite3Config.iPrngSeed, nByte);
23186    return SQLITE_OK;
23187  }else{
23188    return pVfs->xRandomness(pVfs, nByte, zBufOut);
23189  }
23190
23191}
23192SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
23193  return pVfs->xSleep(pVfs, nMicro);
23194}
23195SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
23196  return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
23197}
23198SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
23199  int rc;
23200  /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
23201  ** method to get the current date and time if that method is available
23202  ** (if iVersion is 2 or greater and the function pointer is not NULL) and
23203  ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
23204  ** unavailable.
23205  */
23206  if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
23207    rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
23208  }else{
23209    double r;
23210    rc = pVfs->xCurrentTime(pVfs, &r);
23211    *pTimeOut = (sqlite3_int64)(r*86400000.0);
23212  }
23213  return rc;
23214}
23215
23216SQLITE_PRIVATE int sqlite3OsOpenMalloc(
23217  sqlite3_vfs *pVfs,
23218  const char *zFile,
23219  sqlite3_file **ppFile,
23220  int flags,
23221  int *pOutFlags
23222){
23223  int rc;
23224  sqlite3_file *pFile;
23225  pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
23226  if( pFile ){
23227    rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
23228    if( rc!=SQLITE_OK ){
23229      sqlite3_free(pFile);
23230    }else{
23231      *ppFile = pFile;
23232    }
23233  }else{
23234    rc = SQLITE_NOMEM_BKPT;
23235  }
23236  return rc;
23237}
23238SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *pFile){
23239  assert( pFile );
23240  sqlite3OsClose(pFile);
23241  sqlite3_free(pFile);
23242}
23243
23244/*
23245** This function is a wrapper around the OS specific implementation of
23246** sqlite3_os_init(). The purpose of the wrapper is to provide the
23247** ability to simulate a malloc failure, so that the handling of an
23248** error in sqlite3_os_init() by the upper layers can be tested.
23249*/
23250SQLITE_PRIVATE int sqlite3OsInit(void){
23251  void *p = sqlite3_malloc(10);
23252  if( p==0 ) return SQLITE_NOMEM_BKPT;
23253  sqlite3_free(p);
23254  return sqlite3_os_init();
23255}
23256
23257/*
23258** The list of all registered VFS implementations.
23259*/
23260static sqlite3_vfs * SQLITE_WSD vfsList = 0;
23261#define vfsList GLOBAL(sqlite3_vfs *, vfsList)
23262
23263/*
23264** Locate a VFS by name.  If no name is given, simply return the
23265** first VFS on the list.
23266*/
23267SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
23268  sqlite3_vfs *pVfs = 0;
23269#if SQLITE_THREADSAFE
23270  sqlite3_mutex *mutex;
23271#endif
23272#ifndef SQLITE_OMIT_AUTOINIT
23273  int rc = sqlite3_initialize();
23274  if( rc ) return 0;
23275#endif
23276#if SQLITE_THREADSAFE
23277  mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
23278#endif
23279  sqlite3_mutex_enter(mutex);
23280  for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
23281    if( zVfs==0 ) break;
23282    if( strcmp(zVfs, pVfs->zName)==0 ) break;
23283  }
23284  sqlite3_mutex_leave(mutex);
23285  return pVfs;
23286}
23287
23288/*
23289** Unlink a VFS from the linked list
23290*/
23291static void vfsUnlink(sqlite3_vfs *pVfs){
23292  assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
23293  if( pVfs==0 ){
23294    /* No-op */
23295  }else if( vfsList==pVfs ){
23296    vfsList = pVfs->pNext;
23297  }else if( vfsList ){
23298    sqlite3_vfs *p = vfsList;
23299    while( p->pNext && p->pNext!=pVfs ){
23300      p = p->pNext;
23301    }
23302    if( p->pNext==pVfs ){
23303      p->pNext = pVfs->pNext;
23304    }
23305  }
23306}
23307
23308/*
23309** Register a VFS with the system.  It is harmless to register the same
23310** VFS multiple times.  The new VFS becomes the default if makeDflt is
23311** true.
23312*/
23313SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
23314  MUTEX_LOGIC(sqlite3_mutex *mutex;)
23315#ifndef SQLITE_OMIT_AUTOINIT
23316  int rc = sqlite3_initialize();
23317  if( rc ) return rc;
23318#endif
23319#ifdef SQLITE_ENABLE_API_ARMOR
23320  if( pVfs==0 ) return SQLITE_MISUSE_BKPT;
23321#endif
23322
23323  MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
23324  sqlite3_mutex_enter(mutex);
23325  vfsUnlink(pVfs);
23326  if( makeDflt || vfsList==0 ){
23327    pVfs->pNext = vfsList;
23328    vfsList = pVfs;
23329  }else{
23330    pVfs->pNext = vfsList->pNext;
23331    vfsList->pNext = pVfs;
23332  }
23333  assert(vfsList);
23334  sqlite3_mutex_leave(mutex);
23335  return SQLITE_OK;
23336}
23337
23338/*
23339** Unregister a VFS so that it is no longer accessible.
23340*/
23341SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
23342  MUTEX_LOGIC(sqlite3_mutex *mutex;)
23343#ifndef SQLITE_OMIT_AUTOINIT
23344  int rc = sqlite3_initialize();
23345  if( rc ) return rc;
23346#endif
23347  MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
23348  sqlite3_mutex_enter(mutex);
23349  vfsUnlink(pVfs);
23350  sqlite3_mutex_leave(mutex);
23351  return SQLITE_OK;
23352}
23353
23354/************** End of os.c **************************************************/
23355/************** Begin file fault.c *******************************************/
23356/*
23357** 2008 Jan 22
23358**
23359** The author disclaims copyright to this source code.  In place of
23360** a legal notice, here is a blessing:
23361**
23362**    May you do good and not evil.
23363**    May you find forgiveness for yourself and forgive others.
23364**    May you share freely, never taking more than you give.
23365**
23366*************************************************************************
23367**
23368** This file contains code to support the concept of "benign"
23369** malloc failures (when the xMalloc() or xRealloc() method of the
23370** sqlite3_mem_methods structure fails to allocate a block of memory
23371** and returns 0).
23372**
23373** Most malloc failures are non-benign. After they occur, SQLite
23374** abandons the current operation and returns an error code (usually
23375** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
23376** fatal. For example, if a malloc fails while resizing a hash table, this
23377** is completely recoverable simply by not carrying out the resize. The
23378** hash table will continue to function normally.  So a malloc failure
23379** during a hash table resize is a benign fault.
23380*/
23381
23382/* #include "sqliteInt.h" */
23383
23384#ifndef SQLITE_UNTESTABLE
23385
23386/*
23387** Global variables.
23388*/
23389typedef struct BenignMallocHooks BenignMallocHooks;
23390static SQLITE_WSD struct BenignMallocHooks {
23391  void (*xBenignBegin)(void);
23392  void (*xBenignEnd)(void);
23393} sqlite3Hooks = { 0, 0 };
23394
23395/* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
23396** structure.  If writable static data is unsupported on the target,
23397** we have to locate the state vector at run-time.  In the more common
23398** case where writable static data is supported, wsdHooks can refer directly
23399** to the "sqlite3Hooks" state vector declared above.
23400*/
23401#ifdef SQLITE_OMIT_WSD
23402# define wsdHooksInit \
23403  BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
23404# define wsdHooks x[0]
23405#else
23406# define wsdHooksInit
23407# define wsdHooks sqlite3Hooks
23408#endif
23409
23410
23411/*
23412** Register hooks to call when sqlite3BeginBenignMalloc() and
23413** sqlite3EndBenignMalloc() are called, respectively.
23414*/
23415SQLITE_PRIVATE void sqlite3BenignMallocHooks(
23416  void (*xBenignBegin)(void),
23417  void (*xBenignEnd)(void)
23418){
23419  wsdHooksInit;
23420  wsdHooks.xBenignBegin = xBenignBegin;
23421  wsdHooks.xBenignEnd = xBenignEnd;
23422}
23423
23424/*
23425** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
23426** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
23427** indicates that subsequent malloc failures are non-benign.
23428*/
23429SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
23430  wsdHooksInit;
23431  if( wsdHooks.xBenignBegin ){
23432    wsdHooks.xBenignBegin();
23433  }
23434}
23435SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
23436  wsdHooksInit;
23437  if( wsdHooks.xBenignEnd ){
23438    wsdHooks.xBenignEnd();
23439  }
23440}
23441
23442#endif   /* #ifndef SQLITE_UNTESTABLE */
23443
23444/************** End of fault.c ***********************************************/
23445/************** Begin file mem0.c ********************************************/
23446/*
23447** 2008 October 28
23448**
23449** The author disclaims copyright to this source code.  In place of
23450** a legal notice, here is a blessing:
23451**
23452**    May you do good and not evil.
23453**    May you find forgiveness for yourself and forgive others.
23454**    May you share freely, never taking more than you give.
23455**
23456*************************************************************************
23457**
23458** This file contains a no-op memory allocation drivers for use when
23459** SQLITE_ZERO_MALLOC is defined.  The allocation drivers implemented
23460** here always fail.  SQLite will not operate with these drivers.  These
23461** are merely placeholders.  Real drivers must be substituted using
23462** sqlite3_config() before SQLite will operate.
23463*/
23464/* #include "sqliteInt.h" */
23465
23466/*
23467** This version of the memory allocator is the default.  It is
23468** used when no other memory allocator is specified using compile-time
23469** macros.
23470*/
23471#ifdef SQLITE_ZERO_MALLOC
23472
23473/*
23474** No-op versions of all memory allocation routines
23475*/
23476static void *sqlite3MemMalloc(int nByte){ return 0; }
23477static void sqlite3MemFree(void *pPrior){ return; }
23478static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
23479static int sqlite3MemSize(void *pPrior){ return 0; }
23480static int sqlite3MemRoundup(int n){ return n; }
23481static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
23482static void sqlite3MemShutdown(void *NotUsed){ return; }
23483
23484/*
23485** This routine is the only routine in this file with external linkage.
23486**
23487** Populate the low-level memory allocation function pointers in
23488** sqlite3GlobalConfig.m with pointers to the routines in this file.
23489*/
23490SQLITE_PRIVATE void sqlite3MemSetDefault(void){
23491  static const sqlite3_mem_methods defaultMethods = {
23492     sqlite3MemMalloc,
23493     sqlite3MemFree,
23494     sqlite3MemRealloc,
23495     sqlite3MemSize,
23496     sqlite3MemRoundup,
23497     sqlite3MemInit,
23498     sqlite3MemShutdown,
23499     0
23500  };
23501  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
23502}
23503
23504#endif /* SQLITE_ZERO_MALLOC */
23505
23506/************** End of mem0.c ************************************************/
23507/************** Begin file mem1.c ********************************************/
23508/*
23509** 2007 August 14
23510**
23511** The author disclaims copyright to this source code.  In place of
23512** a legal notice, here is a blessing:
23513**
23514**    May you do good and not evil.
23515**    May you find forgiveness for yourself and forgive others.
23516**    May you share freely, never taking more than you give.
23517**
23518*************************************************************************
23519**
23520** This file contains low-level memory allocation drivers for when
23521** SQLite will use the standard C-library malloc/realloc/free interface
23522** to obtain the memory it needs.
23523**
23524** This file contains implementations of the low-level memory allocation
23525** routines specified in the sqlite3_mem_methods object.  The content of
23526** this file is only used if SQLITE_SYSTEM_MALLOC is defined.  The
23527** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
23528** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined.  The
23529** default configuration is to use memory allocation routines in this
23530** file.
23531**
23532** C-preprocessor macro summary:
23533**
23534**    HAVE_MALLOC_USABLE_SIZE     The configure script sets this symbol if
23535**                                the malloc_usable_size() interface exists
23536**                                on the target platform.  Or, this symbol
23537**                                can be set manually, if desired.
23538**                                If an equivalent interface exists by
23539**                                a different name, using a separate -D
23540**                                option to rename it.
23541**
23542**    SQLITE_WITHOUT_ZONEMALLOC   Some older macs lack support for the zone
23543**                                memory allocator.  Set this symbol to enable
23544**                                building on older macs.
23545**
23546**    SQLITE_WITHOUT_MSIZE        Set this symbol to disable the use of
23547**                                _msize() on windows systems.  This might
23548**                                be necessary when compiling for Delphi,
23549**                                for example.
23550*/
23551/* #include "sqliteInt.h" */
23552
23553/*
23554** This version of the memory allocator is the default.  It is
23555** used when no other memory allocator is specified using compile-time
23556** macros.
23557*/
23558#ifdef SQLITE_SYSTEM_MALLOC
23559#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
23560
23561/*
23562** Use the zone allocator available on apple products unless the
23563** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
23564*/
23565#include <sys/sysctl.h>
23566#include <malloc/malloc.h>
23567#ifdef SQLITE_MIGHT_BE_SINGLE_CORE
23568#include <libkern/OSAtomic.h>
23569#endif /* SQLITE_MIGHT_BE_SINGLE_CORE */
23570static malloc_zone_t* _sqliteZone_;
23571#define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
23572#define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
23573#define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
23574#define SQLITE_MALLOCSIZE(x) \
23575        (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
23576
23577#else /* if not __APPLE__ */
23578
23579/*
23580** Use standard C library malloc and free on non-Apple systems.
23581** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
23582*/
23583#define SQLITE_MALLOC(x)             malloc(x)
23584#define SQLITE_FREE(x)               free(x)
23585#define SQLITE_REALLOC(x,y)          realloc((x),(y))
23586
23587/*
23588** The malloc.h header file is needed for malloc_usable_size() function
23589** on some systems (e.g. Linux).
23590*/
23591#if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
23592#  define SQLITE_USE_MALLOC_H 1
23593#  define SQLITE_USE_MALLOC_USABLE_SIZE 1
23594/*
23595** The MSVCRT has malloc_usable_size(), but it is called _msize().  The
23596** use of _msize() is automatic, but can be disabled by compiling with
23597** -DSQLITE_WITHOUT_MSIZE.  Using the _msize() function also requires
23598** the malloc.h header file.
23599*/
23600#elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
23601#  define SQLITE_USE_MALLOC_H
23602#  define SQLITE_USE_MSIZE
23603#endif
23604
23605/*
23606** Include the malloc.h header file, if necessary.  Also set define macro
23607** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
23608** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
23609** The memory size function can always be overridden manually by defining
23610** the macro SQLITE_MALLOCSIZE to the desired function name.
23611*/
23612#if defined(SQLITE_USE_MALLOC_H)
23613#  include <malloc.h>
23614#  if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
23615#    if !defined(SQLITE_MALLOCSIZE)
23616#      define SQLITE_MALLOCSIZE(x)   malloc_usable_size(x)
23617#    endif
23618#  elif defined(SQLITE_USE_MSIZE)
23619#    if !defined(SQLITE_MALLOCSIZE)
23620#      define SQLITE_MALLOCSIZE      _msize
23621#    endif
23622#  endif
23623#endif /* defined(SQLITE_USE_MALLOC_H) */
23624
23625#endif /* __APPLE__ or not __APPLE__ */
23626
23627/*
23628** Like malloc(), but remember the size of the allocation
23629** so that we can find it later using sqlite3MemSize().
23630**
23631** For this low-level routine, we are guaranteed that nByte>0 because
23632** cases of nByte<=0 will be intercepted and dealt with by higher level
23633** routines.
23634*/
23635static void *sqlite3MemMalloc(int nByte){
23636#ifdef SQLITE_MALLOCSIZE
23637  void *p;
23638  testcase( ROUND8(nByte)==nByte );
23639  p = SQLITE_MALLOC( nByte );
23640  if( p==0 ){
23641    testcase( sqlite3GlobalConfig.xLog!=0 );
23642    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
23643  }
23644  return p;
23645#else
23646  sqlite3_int64 *p;
23647  assert( nByte>0 );
23648  testcase( ROUND8(nByte)!=nByte );
23649  p = SQLITE_MALLOC( nByte+8 );
23650  if( p ){
23651    p[0] = nByte;
23652    p++;
23653  }else{
23654    testcase( sqlite3GlobalConfig.xLog!=0 );
23655    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
23656  }
23657  return (void *)p;
23658#endif
23659}
23660
23661/*
23662** Like free() but works for allocations obtained from sqlite3MemMalloc()
23663** or sqlite3MemRealloc().
23664**
23665** For this low-level routine, we already know that pPrior!=0 since
23666** cases where pPrior==0 will have been intecepted and dealt with
23667** by higher-level routines.
23668*/
23669static void sqlite3MemFree(void *pPrior){
23670#ifdef SQLITE_MALLOCSIZE
23671  SQLITE_FREE(pPrior);
23672#else
23673  sqlite3_int64 *p = (sqlite3_int64*)pPrior;
23674  assert( pPrior!=0 );
23675  p--;
23676  SQLITE_FREE(p);
23677#endif
23678}
23679
23680/*
23681** Report the allocated size of a prior return from xMalloc()
23682** or xRealloc().
23683*/
23684static int sqlite3MemSize(void *pPrior){
23685#ifdef SQLITE_MALLOCSIZE
23686  assert( pPrior!=0 );
23687  return (int)SQLITE_MALLOCSIZE(pPrior);
23688#else
23689  sqlite3_int64 *p;
23690  assert( pPrior!=0 );
23691  p = (sqlite3_int64*)pPrior;
23692  p--;
23693  return (int)p[0];
23694#endif
23695}
23696
23697/*
23698** Like realloc().  Resize an allocation previously obtained from
23699** sqlite3MemMalloc().
23700**
23701** For this low-level interface, we know that pPrior!=0.  Cases where
23702** pPrior==0 while have been intercepted by higher-level routine and
23703** redirected to xMalloc.  Similarly, we know that nByte>0 because
23704** cases where nByte<=0 will have been intercepted by higher-level
23705** routines and redirected to xFree.
23706*/
23707static void *sqlite3MemRealloc(void *pPrior, int nByte){
23708#ifdef SQLITE_MALLOCSIZE
23709  void *p = SQLITE_REALLOC(pPrior, nByte);
23710  if( p==0 ){
23711    testcase( sqlite3GlobalConfig.xLog!=0 );
23712    sqlite3_log(SQLITE_NOMEM,
23713      "failed memory resize %u to %u bytes",
23714      SQLITE_MALLOCSIZE(pPrior), nByte);
23715  }
23716  return p;
23717#else
23718  sqlite3_int64 *p = (sqlite3_int64*)pPrior;
23719  assert( pPrior!=0 && nByte>0 );
23720  assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
23721  p--;
23722  p = SQLITE_REALLOC(p, nByte+8 );
23723  if( p ){
23724    p[0] = nByte;
23725    p++;
23726  }else{
23727    testcase( sqlite3GlobalConfig.xLog!=0 );
23728    sqlite3_log(SQLITE_NOMEM,
23729      "failed memory resize %u to %u bytes",
23730      sqlite3MemSize(pPrior), nByte);
23731  }
23732  return (void*)p;
23733#endif
23734}
23735
23736/*
23737** Round up a request size to the next valid allocation size.
23738*/
23739static int sqlite3MemRoundup(int n){
23740  return ROUND8(n);
23741}
23742
23743/*
23744** Initialize this module.
23745*/
23746static int sqlite3MemInit(void *NotUsed){
23747#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
23748  int cpuCount;
23749  size_t len;
23750  if( _sqliteZone_ ){
23751    return SQLITE_OK;
23752  }
23753  len = sizeof(cpuCount);
23754  /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
23755  sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
23756  if( cpuCount>1 ){
23757    /* defer MT decisions to system malloc */
23758    _sqliteZone_ = malloc_default_zone();
23759  }else{
23760    /* only 1 core, use our own zone to contention over global locks,
23761    ** e.g. we have our own dedicated locks */
23762    _sqliteZone_ = malloc_create_zone(4096, 0);
23763    malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap");
23764  }
23765#endif /*  defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */
23766  UNUSED_PARAMETER(NotUsed);
23767  return SQLITE_OK;
23768}
23769
23770/*
23771** Deinitialize this module.
23772*/
23773static void sqlite3MemShutdown(void *NotUsed){
23774  UNUSED_PARAMETER(NotUsed);
23775  return;
23776}
23777
23778/*
23779** This routine is the only routine in this file with external linkage.
23780**
23781** Populate the low-level memory allocation function pointers in
23782** sqlite3GlobalConfig.m with pointers to the routines in this file.
23783*/
23784SQLITE_PRIVATE void sqlite3MemSetDefault(void){
23785  static const sqlite3_mem_methods defaultMethods = {
23786     sqlite3MemMalloc,
23787     sqlite3MemFree,
23788     sqlite3MemRealloc,
23789     sqlite3MemSize,
23790     sqlite3MemRoundup,
23791     sqlite3MemInit,
23792     sqlite3MemShutdown,
23793     0
23794  };
23795  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
23796}
23797
23798#endif /* SQLITE_SYSTEM_MALLOC */
23799
23800/************** End of mem1.c ************************************************/
23801/************** Begin file mem2.c ********************************************/
23802/*
23803** 2007 August 15
23804**
23805** The author disclaims copyright to this source code.  In place of
23806** a legal notice, here is a blessing:
23807**
23808**    May you do good and not evil.
23809**    May you find forgiveness for yourself and forgive others.
23810**    May you share freely, never taking more than you give.
23811**
23812*************************************************************************
23813**
23814** This file contains low-level memory allocation drivers for when
23815** SQLite will use the standard C-library malloc/realloc/free interface
23816** to obtain the memory it needs while adding lots of additional debugging
23817** information to each allocation in order to help detect and fix memory
23818** leaks and memory usage errors.
23819**
23820** This file contains implementations of the low-level memory allocation
23821** routines specified in the sqlite3_mem_methods object.
23822*/
23823/* #include "sqliteInt.h" */
23824
23825/*
23826** This version of the memory allocator is used only if the
23827** SQLITE_MEMDEBUG macro is defined
23828*/
23829#ifdef SQLITE_MEMDEBUG
23830
23831/*
23832** The backtrace functionality is only available with GLIBC
23833*/
23834#ifdef __GLIBC__
23835  extern int backtrace(void**,int);
23836  extern void backtrace_symbols_fd(void*const*,int,int);
23837#else
23838# define backtrace(A,B) 1
23839# define backtrace_symbols_fd(A,B,C)
23840#endif
23841/* #include <stdio.h> */
23842
23843/*
23844** Each memory allocation looks like this:
23845**
23846**  ------------------------------------------------------------------------
23847**  | Title |  backtrace pointers |  MemBlockHdr |  allocation |  EndGuard |
23848**  ------------------------------------------------------------------------
23849**
23850** The application code sees only a pointer to the allocation.  We have
23851** to back up from the allocation pointer to find the MemBlockHdr.  The
23852** MemBlockHdr tells us the size of the allocation and the number of
23853** backtrace pointers.  There is also a guard word at the end of the
23854** MemBlockHdr.
23855*/
23856struct MemBlockHdr {
23857  i64 iSize;                          /* Size of this allocation */
23858  struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
23859  char nBacktrace;                    /* Number of backtraces on this alloc */
23860  char nBacktraceSlots;               /* Available backtrace slots */
23861  u8 nTitle;                          /* Bytes of title; includes '\0' */
23862  u8 eType;                           /* Allocation type code */
23863  int iForeGuard;                     /* Guard word for sanity */
23864};
23865
23866/*
23867** Guard words
23868*/
23869#define FOREGUARD 0x80F5E153
23870#define REARGUARD 0xE4676B53
23871
23872/*
23873** Number of malloc size increments to track.
23874*/
23875#define NCSIZE  1000
23876
23877/*
23878** All of the static variables used by this module are collected
23879** into a single structure named "mem".  This is to keep the
23880** static variables organized and to reduce namespace pollution
23881** when this module is combined with other in the amalgamation.
23882*/
23883static struct {
23884
23885  /*
23886  ** Mutex to control access to the memory allocation subsystem.
23887  */
23888  sqlite3_mutex *mutex;
23889
23890  /*
23891  ** Head and tail of a linked list of all outstanding allocations
23892  */
23893  struct MemBlockHdr *pFirst;
23894  struct MemBlockHdr *pLast;
23895
23896  /*
23897  ** The number of levels of backtrace to save in new allocations.
23898  */
23899  int nBacktrace;
23900  void (*xBacktrace)(int, int, void **);
23901
23902  /*
23903  ** Title text to insert in front of each block
23904  */
23905  int nTitle;        /* Bytes of zTitle to save.  Includes '\0' and padding */
23906  char zTitle[100];  /* The title text */
23907
23908  /*
23909  ** sqlite3MallocDisallow() increments the following counter.
23910  ** sqlite3MallocAllow() decrements it.
23911  */
23912  int disallow; /* Do not allow memory allocation */
23913
23914  /*
23915  ** Gather statistics on the sizes of memory allocations.
23916  ** nAlloc[i] is the number of allocation attempts of i*8
23917  ** bytes.  i==NCSIZE is the number of allocation attempts for
23918  ** sizes more than NCSIZE*8 bytes.
23919  */
23920  int nAlloc[NCSIZE];      /* Total number of allocations */
23921  int nCurrent[NCSIZE];    /* Current number of allocations */
23922  int mxCurrent[NCSIZE];   /* Highwater mark for nCurrent */
23923
23924} mem;
23925
23926
23927/*
23928** Adjust memory usage statistics
23929*/
23930static void adjustStats(int iSize, int increment){
23931  int i = ROUND8(iSize)/8;
23932  if( i>NCSIZE-1 ){
23933    i = NCSIZE - 1;
23934  }
23935  if( increment>0 ){
23936    mem.nAlloc[i]++;
23937    mem.nCurrent[i]++;
23938    if( mem.nCurrent[i]>mem.mxCurrent[i] ){
23939      mem.mxCurrent[i] = mem.nCurrent[i];
23940    }
23941  }else{
23942    mem.nCurrent[i]--;
23943    assert( mem.nCurrent[i]>=0 );
23944  }
23945}
23946
23947/*
23948** Given an allocation, find the MemBlockHdr for that allocation.
23949**
23950** This routine checks the guards at either end of the allocation and
23951** if they are incorrect it asserts.
23952*/
23953static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
23954  struct MemBlockHdr *p;
23955  int *pInt;
23956  u8 *pU8;
23957  int nReserve;
23958
23959  p = (struct MemBlockHdr*)pAllocation;
23960  p--;
23961  assert( p->iForeGuard==(int)FOREGUARD );
23962  nReserve = ROUND8(p->iSize);
23963  pInt = (int*)pAllocation;
23964  pU8 = (u8*)pAllocation;
23965  assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
23966  /* This checks any of the "extra" bytes allocated due
23967  ** to rounding up to an 8 byte boundary to ensure
23968  ** they haven't been overwritten.
23969  */
23970  while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
23971  return p;
23972}
23973
23974/*
23975** Return the number of bytes currently allocated at address p.
23976*/
23977static int sqlite3MemSize(void *p){
23978  struct MemBlockHdr *pHdr;
23979  if( !p ){
23980    return 0;
23981  }
23982  pHdr = sqlite3MemsysGetHeader(p);
23983  return (int)pHdr->iSize;
23984}
23985
23986/*
23987** Initialize the memory allocation subsystem.
23988*/
23989static int sqlite3MemInit(void *NotUsed){
23990  UNUSED_PARAMETER(NotUsed);
23991  assert( (sizeof(struct MemBlockHdr)&7) == 0 );
23992  if( !sqlite3GlobalConfig.bMemstat ){
23993    /* If memory status is enabled, then the malloc.c wrapper will already
23994    ** hold the STATIC_MEM mutex when the routines here are invoked. */
23995    mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
23996  }
23997  return SQLITE_OK;
23998}
23999
24000/*
24001** Deinitialize the memory allocation subsystem.
24002*/
24003static void sqlite3MemShutdown(void *NotUsed){
24004  UNUSED_PARAMETER(NotUsed);
24005  mem.mutex = 0;
24006}
24007
24008/*
24009** Round up a request size to the next valid allocation size.
24010*/
24011static int sqlite3MemRoundup(int n){
24012  return ROUND8(n);
24013}
24014
24015/*
24016** Fill a buffer with pseudo-random bytes.  This is used to preset
24017** the content of a new memory allocation to unpredictable values and
24018** to clear the content of a freed allocation to unpredictable values.
24019*/
24020static void randomFill(char *pBuf, int nByte){
24021  unsigned int x, y, r;
24022  x = SQLITE_PTR_TO_INT(pBuf);
24023  y = nByte | 1;
24024  while( nByte >= 4 ){
24025    x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
24026    y = y*1103515245 + 12345;
24027    r = x ^ y;
24028    *(int*)pBuf = r;
24029    pBuf += 4;
24030    nByte -= 4;
24031  }
24032  while( nByte-- > 0 ){
24033    x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
24034    y = y*1103515245 + 12345;
24035    r = x ^ y;
24036    *(pBuf++) = r & 0xff;
24037  }
24038}
24039
24040/*
24041** Allocate nByte bytes of memory.
24042*/
24043static void *sqlite3MemMalloc(int nByte){
24044  struct MemBlockHdr *pHdr;
24045  void **pBt;
24046  char *z;
24047  int *pInt;
24048  void *p = 0;
24049  int totalSize;
24050  int nReserve;
24051  sqlite3_mutex_enter(mem.mutex);
24052  assert( mem.disallow==0 );
24053  nReserve = ROUND8(nByte);
24054  totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
24055               mem.nBacktrace*sizeof(void*) + mem.nTitle;
24056  p = malloc(totalSize);
24057  if( p ){
24058    z = p;
24059    pBt = (void**)&z[mem.nTitle];
24060    pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
24061    pHdr->pNext = 0;
24062    pHdr->pPrev = mem.pLast;
24063    if( mem.pLast ){
24064      mem.pLast->pNext = pHdr;
24065    }else{
24066      mem.pFirst = pHdr;
24067    }
24068    mem.pLast = pHdr;
24069    pHdr->iForeGuard = FOREGUARD;
24070    pHdr->eType = MEMTYPE_HEAP;
24071    pHdr->nBacktraceSlots = mem.nBacktrace;
24072    pHdr->nTitle = mem.nTitle;
24073    if( mem.nBacktrace ){
24074      void *aAddr[40];
24075      pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
24076      memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
24077      assert(pBt[0]);
24078      if( mem.xBacktrace ){
24079        mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
24080      }
24081    }else{
24082      pHdr->nBacktrace = 0;
24083    }
24084    if( mem.nTitle ){
24085      memcpy(z, mem.zTitle, mem.nTitle);
24086    }
24087    pHdr->iSize = nByte;
24088    adjustStats(nByte, +1);
24089    pInt = (int*)&pHdr[1];
24090    pInt[nReserve/sizeof(int)] = REARGUARD;
24091    randomFill((char*)pInt, nByte);
24092    memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
24093    p = (void*)pInt;
24094  }
24095  sqlite3_mutex_leave(mem.mutex);
24096  return p;
24097}
24098
24099/*
24100** Free memory.
24101*/
24102static void sqlite3MemFree(void *pPrior){
24103  struct MemBlockHdr *pHdr;
24104  void **pBt;
24105  char *z;
24106  assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0
24107       || mem.mutex!=0 );
24108  pHdr = sqlite3MemsysGetHeader(pPrior);
24109  pBt = (void**)pHdr;
24110  pBt -= pHdr->nBacktraceSlots;
24111  sqlite3_mutex_enter(mem.mutex);
24112  if( pHdr->pPrev ){
24113    assert( pHdr->pPrev->pNext==pHdr );
24114    pHdr->pPrev->pNext = pHdr->pNext;
24115  }else{
24116    assert( mem.pFirst==pHdr );
24117    mem.pFirst = pHdr->pNext;
24118  }
24119  if( pHdr->pNext ){
24120    assert( pHdr->pNext->pPrev==pHdr );
24121    pHdr->pNext->pPrev = pHdr->pPrev;
24122  }else{
24123    assert( mem.pLast==pHdr );
24124    mem.pLast = pHdr->pPrev;
24125  }
24126  z = (char*)pBt;
24127  z -= pHdr->nTitle;
24128  adjustStats((int)pHdr->iSize, -1);
24129  randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
24130                (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
24131  free(z);
24132  sqlite3_mutex_leave(mem.mutex);
24133}
24134
24135/*
24136** Change the size of an existing memory allocation.
24137**
24138** For this debugging implementation, we *always* make a copy of the
24139** allocation into a new place in memory.  In this way, if the
24140** higher level code is using pointer to the old allocation, it is
24141** much more likely to break and we are much more liking to find
24142** the error.
24143*/
24144static void *sqlite3MemRealloc(void *pPrior, int nByte){
24145  struct MemBlockHdr *pOldHdr;
24146  void *pNew;
24147  assert( mem.disallow==0 );
24148  assert( (nByte & 7)==0 );     /* EV: R-46199-30249 */
24149  pOldHdr = sqlite3MemsysGetHeader(pPrior);
24150  pNew = sqlite3MemMalloc(nByte);
24151  if( pNew ){
24152    memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
24153    if( nByte>pOldHdr->iSize ){
24154      randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
24155    }
24156    sqlite3MemFree(pPrior);
24157  }
24158  return pNew;
24159}
24160
24161/*
24162** Populate the low-level memory allocation function pointers in
24163** sqlite3GlobalConfig.m with pointers to the routines in this file.
24164*/
24165SQLITE_PRIVATE void sqlite3MemSetDefault(void){
24166  static const sqlite3_mem_methods defaultMethods = {
24167     sqlite3MemMalloc,
24168     sqlite3MemFree,
24169     sqlite3MemRealloc,
24170     sqlite3MemSize,
24171     sqlite3MemRoundup,
24172     sqlite3MemInit,
24173     sqlite3MemShutdown,
24174     0
24175  };
24176  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
24177}
24178
24179/*
24180** Set the "type" of an allocation.
24181*/
24182SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
24183  if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
24184    struct MemBlockHdr *pHdr;
24185    pHdr = sqlite3MemsysGetHeader(p);
24186    assert( pHdr->iForeGuard==FOREGUARD );
24187    pHdr->eType = eType;
24188  }
24189}
24190
24191/*
24192** Return TRUE if the mask of type in eType matches the type of the
24193** allocation p.  Also return true if p==NULL.
24194**
24195** This routine is designed for use within an assert() statement, to
24196** verify the type of an allocation.  For example:
24197**
24198**     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
24199*/
24200SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
24201  int rc = 1;
24202  if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
24203    struct MemBlockHdr *pHdr;
24204    pHdr = sqlite3MemsysGetHeader(p);
24205    assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
24206    if( (pHdr->eType&eType)==0 ){
24207      rc = 0;
24208    }
24209  }
24210  return rc;
24211}
24212
24213/*
24214** Return TRUE if the mask of type in eType matches no bits of the type of the
24215** allocation p.  Also return true if p==NULL.
24216**
24217** This routine is designed for use within an assert() statement, to
24218** verify the type of an allocation.  For example:
24219**
24220**     assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
24221*/
24222SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
24223  int rc = 1;
24224  if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
24225    struct MemBlockHdr *pHdr;
24226    pHdr = sqlite3MemsysGetHeader(p);
24227    assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
24228    if( (pHdr->eType&eType)!=0 ){
24229      rc = 0;
24230    }
24231  }
24232  return rc;
24233}
24234
24235/*
24236** Set the number of backtrace levels kept for each allocation.
24237** A value of zero turns off backtracing.  The number is always rounded
24238** up to a multiple of 2.
24239*/
24240SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
24241  if( depth<0 ){ depth = 0; }
24242  if( depth>20 ){ depth = 20; }
24243  depth = (depth+1)&0xfe;
24244  mem.nBacktrace = depth;
24245}
24246
24247SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
24248  mem.xBacktrace = xBacktrace;
24249}
24250
24251/*
24252** Set the title string for subsequent allocations.
24253*/
24254SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
24255  unsigned int n = sqlite3Strlen30(zTitle) + 1;
24256  sqlite3_mutex_enter(mem.mutex);
24257  if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
24258  memcpy(mem.zTitle, zTitle, n);
24259  mem.zTitle[n] = 0;
24260  mem.nTitle = ROUND8(n);
24261  sqlite3_mutex_leave(mem.mutex);
24262}
24263
24264SQLITE_PRIVATE void sqlite3MemdebugSync(){
24265  struct MemBlockHdr *pHdr;
24266  for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
24267    void **pBt = (void**)pHdr;
24268    pBt -= pHdr->nBacktraceSlots;
24269    mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
24270  }
24271}
24272
24273/*
24274** Open the file indicated and write a log of all unfreed memory
24275** allocations into that log.
24276*/
24277SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
24278  FILE *out;
24279  struct MemBlockHdr *pHdr;
24280  void **pBt;
24281  int i;
24282  out = fopen(zFilename, "w");
24283  if( out==0 ){
24284    fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
24285                    zFilename);
24286    return;
24287  }
24288  for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
24289    char *z = (char*)pHdr;
24290    z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
24291    fprintf(out, "**** %lld bytes at %p from %s ****\n",
24292            pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
24293    if( pHdr->nBacktrace ){
24294      fflush(out);
24295      pBt = (void**)pHdr;
24296      pBt -= pHdr->nBacktraceSlots;
24297      backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
24298      fprintf(out, "\n");
24299    }
24300  }
24301  fprintf(out, "COUNTS:\n");
24302  for(i=0; i<NCSIZE-1; i++){
24303    if( mem.nAlloc[i] ){
24304      fprintf(out, "   %5d: %10d %10d %10d\n",
24305            i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
24306    }
24307  }
24308  if( mem.nAlloc[NCSIZE-1] ){
24309    fprintf(out, "   %5d: %10d %10d %10d\n",
24310             NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
24311             mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
24312  }
24313  fclose(out);
24314}
24315
24316/*
24317** Return the number of times sqlite3MemMalloc() has been called.
24318*/
24319SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
24320  int i;
24321  int nTotal = 0;
24322  for(i=0; i<NCSIZE; i++){
24323    nTotal += mem.nAlloc[i];
24324  }
24325  return nTotal;
24326}
24327
24328
24329#endif /* SQLITE_MEMDEBUG */
24330
24331/************** End of mem2.c ************************************************/
24332/************** Begin file mem3.c ********************************************/
24333/*
24334** 2007 October 14
24335**
24336** The author disclaims copyright to this source code.  In place of
24337** a legal notice, here is a blessing:
24338**
24339**    May you do good and not evil.
24340**    May you find forgiveness for yourself and forgive others.
24341**    May you share freely, never taking more than you give.
24342**
24343*************************************************************************
24344** This file contains the C functions that implement a memory
24345** allocation subsystem for use by SQLite.
24346**
24347** This version of the memory allocation subsystem omits all
24348** use of malloc(). The SQLite user supplies a block of memory
24349** before calling sqlite3_initialize() from which allocations
24350** are made and returned by the xMalloc() and xRealloc()
24351** implementations. Once sqlite3_initialize() has been called,
24352** the amount of memory available to SQLite is fixed and cannot
24353** be changed.
24354**
24355** This version of the memory allocation subsystem is included
24356** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
24357*/
24358/* #include "sqliteInt.h" */
24359
24360/*
24361** This version of the memory allocator is only built into the library
24362** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
24363** mean that the library will use a memory-pool by default, just that
24364** it is available. The mempool allocator is activated by calling
24365** sqlite3_config().
24366*/
24367#ifdef SQLITE_ENABLE_MEMSYS3
24368
24369/*
24370** Maximum size (in Mem3Blocks) of a "small" chunk.
24371*/
24372#define MX_SMALL 10
24373
24374
24375/*
24376** Number of freelist hash slots
24377*/
24378#define N_HASH  61
24379
24380/*
24381** A memory allocation (also called a "chunk") consists of two or
24382** more blocks where each block is 8 bytes.  The first 8 bytes are
24383** a header that is not returned to the user.
24384**
24385** A chunk is two or more blocks that is either checked out or
24386** free.  The first block has format u.hdr.  u.hdr.size4x is 4 times the
24387** size of the allocation in blocks if the allocation is free.
24388** The u.hdr.size4x&1 bit is true if the chunk is checked out and
24389** false if the chunk is on the freelist.  The u.hdr.size4x&2 bit
24390** is true if the previous chunk is checked out and false if the
24391** previous chunk is free.  The u.hdr.prevSize field is the size of
24392** the previous chunk in blocks if the previous chunk is on the
24393** freelist. If the previous chunk is checked out, then
24394** u.hdr.prevSize can be part of the data for that chunk and should
24395** not be read or written.
24396**
24397** We often identify a chunk by its index in mem3.aPool[].  When
24398** this is done, the chunk index refers to the second block of
24399** the chunk.  In this way, the first chunk has an index of 1.
24400** A chunk index of 0 means "no such chunk" and is the equivalent
24401** of a NULL pointer.
24402**
24403** The second block of free chunks is of the form u.list.  The
24404** two fields form a double-linked list of chunks of related sizes.
24405** Pointers to the head of the list are stored in mem3.aiSmall[]
24406** for smaller chunks and mem3.aiHash[] for larger chunks.
24407**
24408** The second block of a chunk is user data if the chunk is checked
24409** out.  If a chunk is checked out, the user data may extend into
24410** the u.hdr.prevSize value of the following chunk.
24411*/
24412typedef struct Mem3Block Mem3Block;
24413struct Mem3Block {
24414  union {
24415    struct {
24416      u32 prevSize;   /* Size of previous chunk in Mem3Block elements */
24417      u32 size4x;     /* 4x the size of current chunk in Mem3Block elements */
24418    } hdr;
24419    struct {
24420      u32 next;       /* Index in mem3.aPool[] of next free chunk */
24421      u32 prev;       /* Index in mem3.aPool[] of previous free chunk */
24422    } list;
24423  } u;
24424};
24425
24426/*
24427** All of the static variables used by this module are collected
24428** into a single structure named "mem3".  This is to keep the
24429** static variables organized and to reduce namespace pollution
24430** when this module is combined with other in the amalgamation.
24431*/
24432static SQLITE_WSD struct Mem3Global {
24433  /*
24434  ** Memory available for allocation. nPool is the size of the array
24435  ** (in Mem3Blocks) pointed to by aPool less 2.
24436  */
24437  u32 nPool;
24438  Mem3Block *aPool;
24439
24440  /*
24441  ** True if we are evaluating an out-of-memory callback.
24442  */
24443  int alarmBusy;
24444
24445  /*
24446  ** Mutex to control access to the memory allocation subsystem.
24447  */
24448  sqlite3_mutex *mutex;
24449
24450  /*
24451  ** The minimum amount of free space that we have seen.
24452  */
24453  u32 mnMaster;
24454
24455  /*
24456  ** iMaster is the index of the master chunk.  Most new allocations
24457  ** occur off of this chunk.  szMaster is the size (in Mem3Blocks)
24458  ** of the current master.  iMaster is 0 if there is not master chunk.
24459  ** The master chunk is not in either the aiHash[] or aiSmall[].
24460  */
24461  u32 iMaster;
24462  u32 szMaster;
24463
24464  /*
24465  ** Array of lists of free blocks according to the block size
24466  ** for smaller chunks, or a hash on the block size for larger
24467  ** chunks.
24468  */
24469  u32 aiSmall[MX_SMALL-1];   /* For sizes 2 through MX_SMALL, inclusive */
24470  u32 aiHash[N_HASH];        /* For sizes MX_SMALL+1 and larger */
24471} mem3 = { 97535575 };
24472
24473#define mem3 GLOBAL(struct Mem3Global, mem3)
24474
24475/*
24476** Unlink the chunk at mem3.aPool[i] from list it is currently
24477** on.  *pRoot is the list that i is a member of.
24478*/
24479static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
24480  u32 next = mem3.aPool[i].u.list.next;
24481  u32 prev = mem3.aPool[i].u.list.prev;
24482  assert( sqlite3_mutex_held(mem3.mutex) );
24483  if( prev==0 ){
24484    *pRoot = next;
24485  }else{
24486    mem3.aPool[prev].u.list.next = next;
24487  }
24488  if( next ){
24489    mem3.aPool[next].u.list.prev = prev;
24490  }
24491  mem3.aPool[i].u.list.next = 0;
24492  mem3.aPool[i].u.list.prev = 0;
24493}
24494
24495/*
24496** Unlink the chunk at index i from
24497** whatever list is currently a member of.
24498*/
24499static void memsys3Unlink(u32 i){
24500  u32 size, hash;
24501  assert( sqlite3_mutex_held(mem3.mutex) );
24502  assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
24503  assert( i>=1 );
24504  size = mem3.aPool[i-1].u.hdr.size4x/4;
24505  assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
24506  assert( size>=2 );
24507  if( size <= MX_SMALL ){
24508    memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
24509  }else{
24510    hash = size % N_HASH;
24511    memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
24512  }
24513}
24514
24515/*
24516** Link the chunk at mem3.aPool[i] so that is on the list rooted
24517** at *pRoot.
24518*/
24519static void memsys3LinkIntoList(u32 i, u32 *pRoot){
24520  assert( sqlite3_mutex_held(mem3.mutex) );
24521  mem3.aPool[i].u.list.next = *pRoot;
24522  mem3.aPool[i].u.list.prev = 0;
24523  if( *pRoot ){
24524    mem3.aPool[*pRoot].u.list.prev = i;
24525  }
24526  *pRoot = i;
24527}
24528
24529/*
24530** Link the chunk at index i into either the appropriate
24531** small chunk list, or into the large chunk hash table.
24532*/
24533static void memsys3Link(u32 i){
24534  u32 size, hash;
24535  assert( sqlite3_mutex_held(mem3.mutex) );
24536  assert( i>=1 );
24537  assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
24538  size = mem3.aPool[i-1].u.hdr.size4x/4;
24539  assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
24540  assert( size>=2 );
24541  if( size <= MX_SMALL ){
24542    memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
24543  }else{
24544    hash = size % N_HASH;
24545    memsys3LinkIntoList(i, &mem3.aiHash[hash]);
24546  }
24547}
24548
24549/*
24550** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
24551** will already be held (obtained by code in malloc.c) if
24552** sqlite3GlobalConfig.bMemStat is true.
24553*/
24554static void memsys3Enter(void){
24555  if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
24556    mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
24557  }
24558  sqlite3_mutex_enter(mem3.mutex);
24559}
24560static void memsys3Leave(void){
24561  sqlite3_mutex_leave(mem3.mutex);
24562}
24563
24564/*
24565** Called when we are unable to satisfy an allocation of nBytes.
24566*/
24567static void memsys3OutOfMemory(int nByte){
24568  if( !mem3.alarmBusy ){
24569    mem3.alarmBusy = 1;
24570    assert( sqlite3_mutex_held(mem3.mutex) );
24571    sqlite3_mutex_leave(mem3.mutex);
24572    sqlite3_release_memory(nByte);
24573    sqlite3_mutex_enter(mem3.mutex);
24574    mem3.alarmBusy = 0;
24575  }
24576}
24577
24578
24579/*
24580** Chunk i is a free chunk that has been unlinked.  Adjust its
24581** size parameters for check-out and return a pointer to the
24582** user portion of the chunk.
24583*/
24584static void *memsys3Checkout(u32 i, u32 nBlock){
24585  u32 x;
24586  assert( sqlite3_mutex_held(mem3.mutex) );
24587  assert( i>=1 );
24588  assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
24589  assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
24590  x = mem3.aPool[i-1].u.hdr.size4x;
24591  mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
24592  mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
24593  mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
24594  return &mem3.aPool[i];
24595}
24596
24597/*
24598** Carve a piece off of the end of the mem3.iMaster free chunk.
24599** Return a pointer to the new allocation.  Or, if the master chunk
24600** is not large enough, return 0.
24601*/
24602static void *memsys3FromMaster(u32 nBlock){
24603  assert( sqlite3_mutex_held(mem3.mutex) );
24604  assert( mem3.szMaster>=nBlock );
24605  if( nBlock>=mem3.szMaster-1 ){
24606    /* Use the entire master */
24607    void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
24608    mem3.iMaster = 0;
24609    mem3.szMaster = 0;
24610    mem3.mnMaster = 0;
24611    return p;
24612  }else{
24613    /* Split the master block.  Return the tail. */
24614    u32 newi, x;
24615    newi = mem3.iMaster + mem3.szMaster - nBlock;
24616    assert( newi > mem3.iMaster+1 );
24617    mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
24618    mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
24619    mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
24620    mem3.szMaster -= nBlock;
24621    mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
24622    x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
24623    mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
24624    if( mem3.szMaster < mem3.mnMaster ){
24625      mem3.mnMaster = mem3.szMaster;
24626    }
24627    return (void*)&mem3.aPool[newi];
24628  }
24629}
24630
24631/*
24632** *pRoot is the head of a list of free chunks of the same size
24633** or same size hash.  In other words, *pRoot is an entry in either
24634** mem3.aiSmall[] or mem3.aiHash[].
24635**
24636** This routine examines all entries on the given list and tries
24637** to coalesce each entries with adjacent free chunks.
24638**
24639** If it sees a chunk that is larger than mem3.iMaster, it replaces
24640** the current mem3.iMaster with the new larger chunk.  In order for
24641** this mem3.iMaster replacement to work, the master chunk must be
24642** linked into the hash tables.  That is not the normal state of
24643** affairs, of course.  The calling routine must link the master
24644** chunk before invoking this routine, then must unlink the (possibly
24645** changed) master chunk once this routine has finished.
24646*/
24647static void memsys3Merge(u32 *pRoot){
24648  u32 iNext, prev, size, i, x;
24649
24650  assert( sqlite3_mutex_held(mem3.mutex) );
24651  for(i=*pRoot; i>0; i=iNext){
24652    iNext = mem3.aPool[i].u.list.next;
24653    size = mem3.aPool[i-1].u.hdr.size4x;
24654    assert( (size&1)==0 );
24655    if( (size&2)==0 ){
24656      memsys3UnlinkFromList(i, pRoot);
24657      assert( i > mem3.aPool[i-1].u.hdr.prevSize );
24658      prev = i - mem3.aPool[i-1].u.hdr.prevSize;
24659      if( prev==iNext ){
24660        iNext = mem3.aPool[prev].u.list.next;
24661      }
24662      memsys3Unlink(prev);
24663      size = i + size/4 - prev;
24664      x = mem3.aPool[prev-1].u.hdr.size4x & 2;
24665      mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
24666      mem3.aPool[prev+size-1].u.hdr.prevSize = size;
24667      memsys3Link(prev);
24668      i = prev;
24669    }else{
24670      size /= 4;
24671    }
24672    if( size>mem3.szMaster ){
24673      mem3.iMaster = i;
24674      mem3.szMaster = size;
24675    }
24676  }
24677}
24678
24679/*
24680** Return a block of memory of at least nBytes in size.
24681** Return NULL if unable.
24682**
24683** This function assumes that the necessary mutexes, if any, are
24684** already held by the caller. Hence "Unsafe".
24685*/
24686static void *memsys3MallocUnsafe(int nByte){
24687  u32 i;
24688  u32 nBlock;
24689  u32 toFree;
24690
24691  assert( sqlite3_mutex_held(mem3.mutex) );
24692  assert( sizeof(Mem3Block)==8 );
24693  if( nByte<=12 ){
24694    nBlock = 2;
24695  }else{
24696    nBlock = (nByte + 11)/8;
24697  }
24698  assert( nBlock>=2 );
24699
24700  /* STEP 1:
24701  ** Look for an entry of the correct size in either the small
24702  ** chunk table or in the large chunk hash table.  This is
24703  ** successful most of the time (about 9 times out of 10).
24704  */
24705  if( nBlock <= MX_SMALL ){
24706    i = mem3.aiSmall[nBlock-2];
24707    if( i>0 ){
24708      memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
24709      return memsys3Checkout(i, nBlock);
24710    }
24711  }else{
24712    int hash = nBlock % N_HASH;
24713    for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
24714      if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
24715        memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
24716        return memsys3Checkout(i, nBlock);
24717      }
24718    }
24719  }
24720
24721  /* STEP 2:
24722  ** Try to satisfy the allocation by carving a piece off of the end
24723  ** of the master chunk.  This step usually works if step 1 fails.
24724  */
24725  if( mem3.szMaster>=nBlock ){
24726    return memsys3FromMaster(nBlock);
24727  }
24728
24729
24730  /* STEP 3:
24731  ** Loop through the entire memory pool.  Coalesce adjacent free
24732  ** chunks.  Recompute the master chunk as the largest free chunk.
24733  ** Then try again to satisfy the allocation by carving a piece off
24734  ** of the end of the master chunk.  This step happens very
24735  ** rarely (we hope!)
24736  */
24737  for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
24738    memsys3OutOfMemory(toFree);
24739    if( mem3.iMaster ){
24740      memsys3Link(mem3.iMaster);
24741      mem3.iMaster = 0;
24742      mem3.szMaster = 0;
24743    }
24744    for(i=0; i<N_HASH; i++){
24745      memsys3Merge(&mem3.aiHash[i]);
24746    }
24747    for(i=0; i<MX_SMALL-1; i++){
24748      memsys3Merge(&mem3.aiSmall[i]);
24749    }
24750    if( mem3.szMaster ){
24751      memsys3Unlink(mem3.iMaster);
24752      if( mem3.szMaster>=nBlock ){
24753        return memsys3FromMaster(nBlock);
24754      }
24755    }
24756  }
24757
24758  /* If none of the above worked, then we fail. */
24759  return 0;
24760}
24761
24762/*
24763** Free an outstanding memory allocation.
24764**
24765** This function assumes that the necessary mutexes, if any, are
24766** already held by the caller. Hence "Unsafe".
24767*/
24768static void memsys3FreeUnsafe(void *pOld){
24769  Mem3Block *p = (Mem3Block*)pOld;
24770  int i;
24771  u32 size, x;
24772  assert( sqlite3_mutex_held(mem3.mutex) );
24773  assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
24774  i = p - mem3.aPool;
24775  assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
24776  size = mem3.aPool[i-1].u.hdr.size4x/4;
24777  assert( i+size<=mem3.nPool+1 );
24778  mem3.aPool[i-1].u.hdr.size4x &= ~1;
24779  mem3.aPool[i+size-1].u.hdr.prevSize = size;
24780  mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
24781  memsys3Link(i);
24782
24783  /* Try to expand the master using the newly freed chunk */
24784  if( mem3.iMaster ){
24785    while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
24786      size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
24787      mem3.iMaster -= size;
24788      mem3.szMaster += size;
24789      memsys3Unlink(mem3.iMaster);
24790      x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
24791      mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
24792      mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
24793    }
24794    x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
24795    while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
24796      memsys3Unlink(mem3.iMaster+mem3.szMaster);
24797      mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
24798      mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
24799      mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
24800    }
24801  }
24802}
24803
24804/*
24805** Return the size of an outstanding allocation, in bytes.  The
24806** size returned omits the 8-byte header overhead.  This only
24807** works for chunks that are currently checked out.
24808*/
24809static int memsys3Size(void *p){
24810  Mem3Block *pBlock;
24811  assert( p!=0 );
24812  pBlock = (Mem3Block*)p;
24813  assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
24814  return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
24815}
24816
24817/*
24818** Round up a request size to the next valid allocation size.
24819*/
24820static int memsys3Roundup(int n){
24821  if( n<=12 ){
24822    return 12;
24823  }else{
24824    return ((n+11)&~7) - 4;
24825  }
24826}
24827
24828/*
24829** Allocate nBytes of memory.
24830*/
24831static void *memsys3Malloc(int nBytes){
24832  sqlite3_int64 *p;
24833  assert( nBytes>0 );          /* malloc.c filters out 0 byte requests */
24834  memsys3Enter();
24835  p = memsys3MallocUnsafe(nBytes);
24836  memsys3Leave();
24837  return (void*)p;
24838}
24839
24840/*
24841** Free memory.
24842*/
24843static void memsys3Free(void *pPrior){
24844  assert( pPrior );
24845  memsys3Enter();
24846  memsys3FreeUnsafe(pPrior);
24847  memsys3Leave();
24848}
24849
24850/*
24851** Change the size of an existing memory allocation
24852*/
24853static void *memsys3Realloc(void *pPrior, int nBytes){
24854  int nOld;
24855  void *p;
24856  if( pPrior==0 ){
24857    return sqlite3_malloc(nBytes);
24858  }
24859  if( nBytes<=0 ){
24860    sqlite3_free(pPrior);
24861    return 0;
24862  }
24863  nOld = memsys3Size(pPrior);
24864  if( nBytes<=nOld && nBytes>=nOld-128 ){
24865    return pPrior;
24866  }
24867  memsys3Enter();
24868  p = memsys3MallocUnsafe(nBytes);
24869  if( p ){
24870    if( nOld<nBytes ){
24871      memcpy(p, pPrior, nOld);
24872    }else{
24873      memcpy(p, pPrior, nBytes);
24874    }
24875    memsys3FreeUnsafe(pPrior);
24876  }
24877  memsys3Leave();
24878  return p;
24879}
24880
24881/*
24882** Initialize this module.
24883*/
24884static int memsys3Init(void *NotUsed){
24885  UNUSED_PARAMETER(NotUsed);
24886  if( !sqlite3GlobalConfig.pHeap ){
24887    return SQLITE_ERROR;
24888  }
24889
24890  /* Store a pointer to the memory block in global structure mem3. */
24891  assert( sizeof(Mem3Block)==8 );
24892  mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
24893  mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
24894
24895  /* Initialize the master block. */
24896  mem3.szMaster = mem3.nPool;
24897  mem3.mnMaster = mem3.szMaster;
24898  mem3.iMaster = 1;
24899  mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
24900  mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
24901  mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
24902
24903  return SQLITE_OK;
24904}
24905
24906/*
24907** Deinitialize this module.
24908*/
24909static void memsys3Shutdown(void *NotUsed){
24910  UNUSED_PARAMETER(NotUsed);
24911  mem3.mutex = 0;
24912  return;
24913}
24914
24915
24916
24917/*
24918** Open the file indicated and write a log of all unfreed memory
24919** allocations into that log.
24920*/
24921SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
24922#ifdef SQLITE_DEBUG
24923  FILE *out;
24924  u32 i, j;
24925  u32 size;
24926  if( zFilename==0 || zFilename[0]==0 ){
24927    out = stdout;
24928  }else{
24929    out = fopen(zFilename, "w");
24930    if( out==0 ){
24931      fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
24932                      zFilename);
24933      return;
24934    }
24935  }
24936  memsys3Enter();
24937  fprintf(out, "CHUNKS:\n");
24938  for(i=1; i<=mem3.nPool; i+=size/4){
24939    size = mem3.aPool[i-1].u.hdr.size4x;
24940    if( size/4<=1 ){
24941      fprintf(out, "%p size error\n", &mem3.aPool[i]);
24942      assert( 0 );
24943      break;
24944    }
24945    if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
24946      fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
24947      assert( 0 );
24948      break;
24949    }
24950    if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
24951      fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
24952      assert( 0 );
24953      break;
24954    }
24955    if( size&1 ){
24956      fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
24957    }else{
24958      fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
24959                  i==mem3.iMaster ? " **master**" : "");
24960    }
24961  }
24962  for(i=0; i<MX_SMALL-1; i++){
24963    if( mem3.aiSmall[i]==0 ) continue;
24964    fprintf(out, "small(%2d):", i);
24965    for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
24966      fprintf(out, " %p(%d)", &mem3.aPool[j],
24967              (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
24968    }
24969    fprintf(out, "\n");
24970  }
24971  for(i=0; i<N_HASH; i++){
24972    if( mem3.aiHash[i]==0 ) continue;
24973    fprintf(out, "hash(%2d):", i);
24974    for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
24975      fprintf(out, " %p(%d)", &mem3.aPool[j],
24976              (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
24977    }
24978    fprintf(out, "\n");
24979  }
24980  fprintf(out, "master=%d\n", mem3.iMaster);
24981  fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
24982  fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
24983  sqlite3_mutex_leave(mem3.mutex);
24984  if( out==stdout ){
24985    fflush(stdout);
24986  }else{
24987    fclose(out);
24988  }
24989#else
24990  UNUSED_PARAMETER(zFilename);
24991#endif
24992}
24993
24994/*
24995** This routine is the only routine in this file with external
24996** linkage.
24997**
24998** Populate the low-level memory allocation function pointers in
24999** sqlite3GlobalConfig.m with pointers to the routines in this file. The
25000** arguments specify the block of memory to manage.
25001**
25002** This routine is only called by sqlite3_config(), and therefore
25003** is not required to be threadsafe (it is not).
25004*/
25005SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
25006  static const sqlite3_mem_methods mempoolMethods = {
25007     memsys3Malloc,
25008     memsys3Free,
25009     memsys3Realloc,
25010     memsys3Size,
25011     memsys3Roundup,
25012     memsys3Init,
25013     memsys3Shutdown,
25014     0
25015  };
25016  return &mempoolMethods;
25017}
25018
25019#endif /* SQLITE_ENABLE_MEMSYS3 */
25020
25021/************** End of mem3.c ************************************************/
25022/************** Begin file mem5.c ********************************************/
25023/*
25024** 2007 October 14
25025**
25026** The author disclaims copyright to this source code.  In place of
25027** a legal notice, here is a blessing:
25028**
25029**    May you do good and not evil.
25030**    May you find forgiveness for yourself and forgive others.
25031**    May you share freely, never taking more than you give.
25032**
25033*************************************************************************
25034** This file contains the C functions that implement a memory
25035** allocation subsystem for use by SQLite.
25036**
25037** This version of the memory allocation subsystem omits all
25038** use of malloc(). The application gives SQLite a block of memory
25039** before calling sqlite3_initialize() from which allocations
25040** are made and returned by the xMalloc() and xRealloc()
25041** implementations. Once sqlite3_initialize() has been called,
25042** the amount of memory available to SQLite is fixed and cannot
25043** be changed.
25044**
25045** This version of the memory allocation subsystem is included
25046** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
25047**
25048** This memory allocator uses the following algorithm:
25049**
25050**   1.  All memory allocation sizes are rounded up to a power of 2.
25051**
25052**   2.  If two adjacent free blocks are the halves of a larger block,
25053**       then the two blocks are coalesced into the single larger block.
25054**
25055**   3.  New memory is allocated from the first available free block.
25056**
25057** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
25058** Concerning Dynamic Storage Allocation". Journal of the Association for
25059** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
25060**
25061** Let n be the size of the largest allocation divided by the minimum
25062** allocation size (after rounding all sizes up to a power of 2.)  Let M
25063** be the maximum amount of memory ever outstanding at one time.  Let
25064** N be the total amount of memory available for allocation.  Robson
25065** proved that this memory allocator will never breakdown due to
25066** fragmentation as long as the following constraint holds:
25067**
25068**      N >=  M*(1 + log2(n)/2) - n + 1
25069**
25070** The sqlite3_status() logic tracks the maximum values of n and M so
25071** that an application can, at any time, verify this constraint.
25072*/
25073/* #include "sqliteInt.h" */
25074
25075/*
25076** This version of the memory allocator is used only when
25077** SQLITE_ENABLE_MEMSYS5 is defined.
25078*/
25079#ifdef SQLITE_ENABLE_MEMSYS5
25080
25081/*
25082** A minimum allocation is an instance of the following structure.
25083** Larger allocations are an array of these structures where the
25084** size of the array is a power of 2.
25085**
25086** The size of this object must be a power of two.  That fact is
25087** verified in memsys5Init().
25088*/
25089typedef struct Mem5Link Mem5Link;
25090struct Mem5Link {
25091  int next;       /* Index of next free chunk */
25092  int prev;       /* Index of previous free chunk */
25093};
25094
25095/*
25096** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
25097** mem5.szAtom is always at least 8 and 32-bit integers are used,
25098** it is not actually possible to reach this limit.
25099*/
25100#define LOGMAX 30
25101
25102/*
25103** Masks used for mem5.aCtrl[] elements.
25104*/
25105#define CTRL_LOGSIZE  0x1f    /* Log2 Size of this block */
25106#define CTRL_FREE     0x20    /* True if not checked out */
25107
25108/*
25109** All of the static variables used by this module are collected
25110** into a single structure named "mem5".  This is to keep the
25111** static variables organized and to reduce namespace pollution
25112** when this module is combined with other in the amalgamation.
25113*/
25114static SQLITE_WSD struct Mem5Global {
25115  /*
25116  ** Memory available for allocation
25117  */
25118  int szAtom;      /* Smallest possible allocation in bytes */
25119  int nBlock;      /* Number of szAtom sized blocks in zPool */
25120  u8 *zPool;       /* Memory available to be allocated */
25121
25122  /*
25123  ** Mutex to control access to the memory allocation subsystem.
25124  */
25125  sqlite3_mutex *mutex;
25126
25127#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
25128  /*
25129  ** Performance statistics
25130  */
25131  u64 nAlloc;         /* Total number of calls to malloc */
25132  u64 totalAlloc;     /* Total of all malloc calls - includes internal frag */
25133  u64 totalExcess;    /* Total internal fragmentation */
25134  u32 currentOut;     /* Current checkout, including internal fragmentation */
25135  u32 currentCount;   /* Current number of distinct checkouts */
25136  u32 maxOut;         /* Maximum instantaneous currentOut */
25137  u32 maxCount;       /* Maximum instantaneous currentCount */
25138  u32 maxRequest;     /* Largest allocation (exclusive of internal frag) */
25139#endif
25140
25141  /*
25142  ** Lists of free blocks.  aiFreelist[0] is a list of free blocks of
25143  ** size mem5.szAtom.  aiFreelist[1] holds blocks of size szAtom*2.
25144  ** aiFreelist[2] holds free blocks of size szAtom*4.  And so forth.
25145  */
25146  int aiFreelist[LOGMAX+1];
25147
25148  /*
25149  ** Space for tracking which blocks are checked out and the size
25150  ** of each block.  One byte per block.
25151  */
25152  u8 *aCtrl;
25153
25154} mem5;
25155
25156/*
25157** Access the static variable through a macro for SQLITE_OMIT_WSD.
25158*/
25159#define mem5 GLOBAL(struct Mem5Global, mem5)
25160
25161/*
25162** Assuming mem5.zPool is divided up into an array of Mem5Link
25163** structures, return a pointer to the idx-th such link.
25164*/
25165#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
25166
25167/*
25168** Unlink the chunk at mem5.aPool[i] from list it is currently
25169** on.  It should be found on mem5.aiFreelist[iLogsize].
25170*/
25171static void memsys5Unlink(int i, int iLogsize){
25172  int next, prev;
25173  assert( i>=0 && i<mem5.nBlock );
25174  assert( iLogsize>=0 && iLogsize<=LOGMAX );
25175  assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
25176
25177  next = MEM5LINK(i)->next;
25178  prev = MEM5LINK(i)->prev;
25179  if( prev<0 ){
25180    mem5.aiFreelist[iLogsize] = next;
25181  }else{
25182    MEM5LINK(prev)->next = next;
25183  }
25184  if( next>=0 ){
25185    MEM5LINK(next)->prev = prev;
25186  }
25187}
25188
25189/*
25190** Link the chunk at mem5.aPool[i] so that is on the iLogsize
25191** free list.
25192*/
25193static void memsys5Link(int i, int iLogsize){
25194  int x;
25195  assert( sqlite3_mutex_held(mem5.mutex) );
25196  assert( i>=0 && i<mem5.nBlock );
25197  assert( iLogsize>=0 && iLogsize<=LOGMAX );
25198  assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
25199
25200  x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
25201  MEM5LINK(i)->prev = -1;
25202  if( x>=0 ){
25203    assert( x<mem5.nBlock );
25204    MEM5LINK(x)->prev = i;
25205  }
25206  mem5.aiFreelist[iLogsize] = i;
25207}
25208
25209/*
25210** Obtain or release the mutex needed to access global data structures.
25211*/
25212static void memsys5Enter(void){
25213  sqlite3_mutex_enter(mem5.mutex);
25214}
25215static void memsys5Leave(void){
25216  sqlite3_mutex_leave(mem5.mutex);
25217}
25218
25219/*
25220** Return the size of an outstanding allocation, in bytes.
25221** This only works for chunks that are currently checked out.
25222*/
25223static int memsys5Size(void *p){
25224  int iSize, i;
25225  assert( p!=0 );
25226  i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
25227  assert( i>=0 && i<mem5.nBlock );
25228  iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
25229  return iSize;
25230}
25231
25232/*
25233** Return a block of memory of at least nBytes in size.
25234** Return NULL if unable.  Return NULL if nBytes==0.
25235**
25236** The caller guarantees that nByte is positive.
25237**
25238** The caller has obtained a mutex prior to invoking this
25239** routine so there is never any chance that two or more
25240** threads can be in this routine at the same time.
25241*/
25242static void *memsys5MallocUnsafe(int nByte){
25243  int i;           /* Index of a mem5.aPool[] slot */
25244  int iBin;        /* Index into mem5.aiFreelist[] */
25245  int iFullSz;     /* Size of allocation rounded up to power of 2 */
25246  int iLogsize;    /* Log2 of iFullSz/POW2_MIN */
25247
25248  /* nByte must be a positive */
25249  assert( nByte>0 );
25250
25251  /* No more than 1GiB per allocation */
25252  if( nByte > 0x40000000 ) return 0;
25253
25254#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
25255  /* Keep track of the maximum allocation request.  Even unfulfilled
25256  ** requests are counted */
25257  if( (u32)nByte>mem5.maxRequest ){
25258    mem5.maxRequest = nByte;
25259  }
25260#endif
25261
25262
25263  /* Round nByte up to the next valid power of two */
25264  for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz<nByte; iFullSz*=2,iLogsize++){}
25265
25266  /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
25267  ** block.  If not, then split a block of the next larger power of
25268  ** two in order to create a new free block of size iLogsize.
25269  */
25270  for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
25271  if( iBin>LOGMAX ){
25272    testcase( sqlite3GlobalConfig.xLog!=0 );
25273    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
25274    return 0;
25275  }
25276  i = mem5.aiFreelist[iBin];
25277  memsys5Unlink(i, iBin);
25278  while( iBin>iLogsize ){
25279    int newSize;
25280
25281    iBin--;
25282    newSize = 1 << iBin;
25283    mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
25284    memsys5Link(i+newSize, iBin);
25285  }
25286  mem5.aCtrl[i] = iLogsize;
25287
25288#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
25289  /* Update allocator performance statistics. */
25290  mem5.nAlloc++;
25291  mem5.totalAlloc += iFullSz;
25292  mem5.totalExcess += iFullSz - nByte;
25293  mem5.currentCount++;
25294  mem5.currentOut += iFullSz;
25295  if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
25296  if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
25297#endif
25298
25299#ifdef SQLITE_DEBUG
25300  /* Make sure the allocated memory does not assume that it is set to zero
25301  ** or retains a value from a previous allocation */
25302  memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
25303#endif
25304
25305  /* Return a pointer to the allocated memory. */
25306  return (void*)&mem5.zPool[i*mem5.szAtom];
25307}
25308
25309/*
25310** Free an outstanding memory allocation.
25311*/
25312static void memsys5FreeUnsafe(void *pOld){
25313  u32 size, iLogsize;
25314  int iBlock;
25315
25316  /* Set iBlock to the index of the block pointed to by pOld in
25317  ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
25318  */
25319  iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
25320
25321  /* Check that the pointer pOld points to a valid, non-free block. */
25322  assert( iBlock>=0 && iBlock<mem5.nBlock );
25323  assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
25324  assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
25325
25326  iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
25327  size = 1<<iLogsize;
25328  assert( iBlock+size-1<(u32)mem5.nBlock );
25329
25330  mem5.aCtrl[iBlock] |= CTRL_FREE;
25331  mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
25332
25333#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
25334  assert( mem5.currentCount>0 );
25335  assert( mem5.currentOut>=(size*mem5.szAtom) );
25336  mem5.currentCount--;
25337  mem5.currentOut -= size*mem5.szAtom;
25338  assert( mem5.currentOut>0 || mem5.currentCount==0 );
25339  assert( mem5.currentCount>0 || mem5.currentOut==0 );
25340#endif
25341
25342  mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
25343  while( ALWAYS(iLogsize<LOGMAX) ){
25344    int iBuddy;
25345    if( (iBlock>>iLogsize) & 1 ){
25346      iBuddy = iBlock - size;
25347      assert( iBuddy>=0 );
25348    }else{
25349      iBuddy = iBlock + size;
25350      if( iBuddy>=mem5.nBlock ) break;
25351    }
25352    if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
25353    memsys5Unlink(iBuddy, iLogsize);
25354    iLogsize++;
25355    if( iBuddy<iBlock ){
25356      mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
25357      mem5.aCtrl[iBlock] = 0;
25358      iBlock = iBuddy;
25359    }else{
25360      mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
25361      mem5.aCtrl[iBuddy] = 0;
25362    }
25363    size *= 2;
25364  }
25365
25366#ifdef SQLITE_DEBUG
25367  /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
25368  ** not used after being freed */
25369  memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
25370#endif
25371
25372  memsys5Link(iBlock, iLogsize);
25373}
25374
25375/*
25376** Allocate nBytes of memory.
25377*/
25378static void *memsys5Malloc(int nBytes){
25379  sqlite3_int64 *p = 0;
25380  if( nBytes>0 ){
25381    memsys5Enter();
25382    p = memsys5MallocUnsafe(nBytes);
25383    memsys5Leave();
25384  }
25385  return (void*)p;
25386}
25387
25388/*
25389** Free memory.
25390**
25391** The outer layer memory allocator prevents this routine from
25392** being called with pPrior==0.
25393*/
25394static void memsys5Free(void *pPrior){
25395  assert( pPrior!=0 );
25396  memsys5Enter();
25397  memsys5FreeUnsafe(pPrior);
25398  memsys5Leave();
25399}
25400
25401/*
25402** Change the size of an existing memory allocation.
25403**
25404** The outer layer memory allocator prevents this routine from
25405** being called with pPrior==0.
25406**
25407** nBytes is always a value obtained from a prior call to
25408** memsys5Round().  Hence nBytes is always a non-negative power
25409** of two.  If nBytes==0 that means that an oversize allocation
25410** (an allocation larger than 0x40000000) was requested and this
25411** routine should return 0 without freeing pPrior.
25412*/
25413static void *memsys5Realloc(void *pPrior, int nBytes){
25414  int nOld;
25415  void *p;
25416  assert( pPrior!=0 );
25417  assert( (nBytes&(nBytes-1))==0 );  /* EV: R-46199-30249 */
25418  assert( nBytes>=0 );
25419  if( nBytes==0 ){
25420    return 0;
25421  }
25422  nOld = memsys5Size(pPrior);
25423  if( nBytes<=nOld ){
25424    return pPrior;
25425  }
25426  p = memsys5Malloc(nBytes);
25427  if( p ){
25428    memcpy(p, pPrior, nOld);
25429    memsys5Free(pPrior);
25430  }
25431  return p;
25432}
25433
25434/*
25435** Round up a request size to the next valid allocation size.  If
25436** the allocation is too large to be handled by this allocation system,
25437** return 0.
25438**
25439** All allocations must be a power of two and must be expressed by a
25440** 32-bit signed integer.  Hence the largest allocation is 0x40000000
25441** or 1073741824 bytes.
25442*/
25443static int memsys5Roundup(int n){
25444  int iFullSz;
25445  if( n > 0x40000000 ) return 0;
25446  for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
25447  return iFullSz;
25448}
25449
25450/*
25451** Return the ceiling of the logarithm base 2 of iValue.
25452**
25453** Examples:   memsys5Log(1) -> 0
25454**             memsys5Log(2) -> 1
25455**             memsys5Log(4) -> 2
25456**             memsys5Log(5) -> 3
25457**             memsys5Log(8) -> 3
25458**             memsys5Log(9) -> 4
25459*/
25460static int memsys5Log(int iValue){
25461  int iLog;
25462  for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
25463  return iLog;
25464}
25465
25466/*
25467** Initialize the memory allocator.
25468**
25469** This routine is not threadsafe.  The caller must be holding a mutex
25470** to prevent multiple threads from entering at the same time.
25471*/
25472static int memsys5Init(void *NotUsed){
25473  int ii;            /* Loop counter */
25474  int nByte;         /* Number of bytes of memory available to this allocator */
25475  u8 *zByte;         /* Memory usable by this allocator */
25476  int nMinLog;       /* Log base 2 of minimum allocation size in bytes */
25477  int iOffset;       /* An offset into mem5.aCtrl[] */
25478
25479  UNUSED_PARAMETER(NotUsed);
25480
25481  /* For the purposes of this routine, disable the mutex */
25482  mem5.mutex = 0;
25483
25484  /* The size of a Mem5Link object must be a power of two.  Verify that
25485  ** this is case.
25486  */
25487  assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
25488
25489  nByte = sqlite3GlobalConfig.nHeap;
25490  zByte = (u8*)sqlite3GlobalConfig.pHeap;
25491  assert( zByte!=0 );  /* sqlite3_config() does not allow otherwise */
25492
25493  /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
25494  nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
25495  mem5.szAtom = (1<<nMinLog);
25496  while( (int)sizeof(Mem5Link)>mem5.szAtom ){
25497    mem5.szAtom = mem5.szAtom << 1;
25498  }
25499
25500  mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
25501  mem5.zPool = zByte;
25502  mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
25503
25504  for(ii=0; ii<=LOGMAX; ii++){
25505    mem5.aiFreelist[ii] = -1;
25506  }
25507
25508  iOffset = 0;
25509  for(ii=LOGMAX; ii>=0; ii--){
25510    int nAlloc = (1<<ii);
25511    if( (iOffset+nAlloc)<=mem5.nBlock ){
25512      mem5.aCtrl[iOffset] = ii | CTRL_FREE;
25513      memsys5Link(iOffset, ii);
25514      iOffset += nAlloc;
25515    }
25516    assert((iOffset+nAlloc)>mem5.nBlock);
25517  }
25518
25519  /* If a mutex is required for normal operation, allocate one */
25520  if( sqlite3GlobalConfig.bMemstat==0 ){
25521    mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
25522  }
25523
25524  return SQLITE_OK;
25525}
25526
25527/*
25528** Deinitialize this module.
25529*/
25530static void memsys5Shutdown(void *NotUsed){
25531  UNUSED_PARAMETER(NotUsed);
25532  mem5.mutex = 0;
25533  return;
25534}
25535
25536#ifdef SQLITE_TEST
25537/*
25538** Open the file indicated and write a log of all unfreed memory
25539** allocations into that log.
25540*/
25541SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
25542  FILE *out;
25543  int i, j, n;
25544  int nMinLog;
25545
25546  if( zFilename==0 || zFilename[0]==0 ){
25547    out = stdout;
25548  }else{
25549    out = fopen(zFilename, "w");
25550    if( out==0 ){
25551      fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
25552                      zFilename);
25553      return;
25554    }
25555  }
25556  memsys5Enter();
25557  nMinLog = memsys5Log(mem5.szAtom);
25558  for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
25559    for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
25560    fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
25561  }
25562  fprintf(out, "mem5.nAlloc       = %llu\n", mem5.nAlloc);
25563  fprintf(out, "mem5.totalAlloc   = %llu\n", mem5.totalAlloc);
25564  fprintf(out, "mem5.totalExcess  = %llu\n", mem5.totalExcess);
25565  fprintf(out, "mem5.currentOut   = %u\n", mem5.currentOut);
25566  fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
25567  fprintf(out, "mem5.maxOut       = %u\n", mem5.maxOut);
25568  fprintf(out, "mem5.maxCount     = %u\n", mem5.maxCount);
25569  fprintf(out, "mem5.maxRequest   = %u\n", mem5.maxRequest);
25570  memsys5Leave();
25571  if( out==stdout ){
25572    fflush(stdout);
25573  }else{
25574    fclose(out);
25575  }
25576}
25577#endif
25578
25579/*
25580** This routine is the only routine in this file with external
25581** linkage. It returns a pointer to a static sqlite3_mem_methods
25582** struct populated with the memsys5 methods.
25583*/
25584SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
25585  static const sqlite3_mem_methods memsys5Methods = {
25586     memsys5Malloc,
25587     memsys5Free,
25588     memsys5Realloc,
25589     memsys5Size,
25590     memsys5Roundup,
25591     memsys5Init,
25592     memsys5Shutdown,
25593     0
25594  };
25595  return &memsys5Methods;
25596}
25597
25598#endif /* SQLITE_ENABLE_MEMSYS5 */
25599
25600/************** End of mem5.c ************************************************/
25601/************** Begin file mutex.c *******************************************/
25602/*
25603** 2007 August 14
25604**
25605** The author disclaims copyright to this source code.  In place of
25606** a legal notice, here is a blessing:
25607**
25608**    May you do good and not evil.
25609**    May you find forgiveness for yourself and forgive others.
25610**    May you share freely, never taking more than you give.
25611**
25612*************************************************************************
25613** This file contains the C functions that implement mutexes.
25614**
25615** This file contains code that is common across all mutex implementations.
25616*/
25617/* #include "sqliteInt.h" */
25618
25619#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
25620/*
25621** For debugging purposes, record when the mutex subsystem is initialized
25622** and uninitialized so that we can assert() if there is an attempt to
25623** allocate a mutex while the system is uninitialized.
25624*/
25625static SQLITE_WSD int mutexIsInit = 0;
25626#endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
25627
25628
25629#ifndef SQLITE_MUTEX_OMIT
25630
25631#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
25632/*
25633** This block (enclosed by SQLITE_ENABLE_MULTITHREADED_CHECKS) contains
25634** the implementation of a wrapper around the system default mutex
25635** implementation (sqlite3DefaultMutex()).
25636**
25637** Most calls are passed directly through to the underlying default
25638** mutex implementation. Except, if a mutex is configured by calling
25639** sqlite3MutexWarnOnContention() on it, then if contention is ever
25640** encountered within xMutexEnter() a warning is emitted via sqlite3_log().
25641**
25642** This type of mutex is used as the database handle mutex when testing
25643** apps that usually use SQLITE_CONFIG_MULTITHREAD mode.
25644*/
25645
25646/*
25647** Type for all mutexes used when SQLITE_ENABLE_MULTITHREADED_CHECKS
25648** is defined. Variable CheckMutex.mutex is a pointer to the real mutex
25649** allocated by the system mutex implementation. Variable iType is usually set
25650** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
25651** or one of the static mutex identifiers. Or, if this is a recursive mutex
25652** that has been configured using sqlite3MutexWarnOnContention(), it is
25653** set to SQLITE_MUTEX_WARNONCONTENTION.
25654*/
25655typedef struct CheckMutex CheckMutex;
25656struct CheckMutex {
25657  int iType;
25658  sqlite3_mutex *mutex;
25659};
25660
25661#define SQLITE_MUTEX_WARNONCONTENTION  (-1)
25662
25663/*
25664** Pointer to real mutex methods object used by the CheckMutex
25665** implementation. Set by checkMutexInit().
25666*/
25667static SQLITE_WSD const sqlite3_mutex_methods *pGlobalMutexMethods;
25668
25669#ifdef SQLITE_DEBUG
25670static int checkMutexHeld(sqlite3_mutex *p){
25671  return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex);
25672}
25673static int checkMutexNotheld(sqlite3_mutex *p){
25674  return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex);
25675}
25676#endif
25677
25678/*
25679** Initialize and deinitialize the mutex subsystem.
25680*/
25681static int checkMutexInit(void){
25682  pGlobalMutexMethods = sqlite3DefaultMutex();
25683  return SQLITE_OK;
25684}
25685static int checkMutexEnd(void){
25686  pGlobalMutexMethods = 0;
25687  return SQLITE_OK;
25688}
25689
25690/*
25691** Allocate a mutex.
25692*/
25693static sqlite3_mutex *checkMutexAlloc(int iType){
25694  static CheckMutex staticMutexes[] = {
25695    {2, 0}, {3, 0}, {4, 0}, {5, 0},
25696    {6, 0}, {7, 0}, {8, 0}, {9, 0},
25697    {10, 0}, {11, 0}, {12, 0}, {13, 0}
25698  };
25699  CheckMutex *p = 0;
25700
25701  assert( SQLITE_MUTEX_RECURSIVE==1 && SQLITE_MUTEX_FAST==0 );
25702  if( iType<2 ){
25703    p = sqlite3MallocZero(sizeof(CheckMutex));
25704    if( p==0 ) return 0;
25705    p->iType = iType;
25706  }else{
25707#ifdef SQLITE_ENABLE_API_ARMOR
25708    if( iType-2>=ArraySize(staticMutexes) ){
25709      (void)SQLITE_MISUSE_BKPT;
25710      return 0;
25711    }
25712#endif
25713    p = &staticMutexes[iType-2];
25714  }
25715
25716  if( p->mutex==0 ){
25717    p->mutex = pGlobalMutexMethods->xMutexAlloc(iType);
25718    if( p->mutex==0 ){
25719      if( iType<2 ){
25720        sqlite3_free(p);
25721      }
25722      p = 0;
25723    }
25724  }
25725
25726  return (sqlite3_mutex*)p;
25727}
25728
25729/*
25730** Free a mutex.
25731*/
25732static void checkMutexFree(sqlite3_mutex *p){
25733  assert( SQLITE_MUTEX_RECURSIVE<2 );
25734  assert( SQLITE_MUTEX_FAST<2 );
25735  assert( SQLITE_MUTEX_WARNONCONTENTION<2 );
25736
25737#if SQLITE_ENABLE_API_ARMOR
25738  if( ((CheckMutex*)p)->iType<2 )
25739#endif
25740  {
25741    CheckMutex *pCheck = (CheckMutex*)p;
25742    pGlobalMutexMethods->xMutexFree(pCheck->mutex);
25743    sqlite3_free(pCheck);
25744  }
25745#ifdef SQLITE_ENABLE_API_ARMOR
25746  else{
25747    (void)SQLITE_MISUSE_BKPT;
25748  }
25749#endif
25750}
25751
25752/*
25753** Enter the mutex.
25754*/
25755static void checkMutexEnter(sqlite3_mutex *p){
25756  CheckMutex *pCheck = (CheckMutex*)p;
25757  if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){
25758    if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){
25759      return;
25760    }
25761    sqlite3_log(SQLITE_MISUSE,
25762        "illegal multi-threaded access to database connection"
25763    );
25764  }
25765  pGlobalMutexMethods->xMutexEnter(pCheck->mutex);
25766}
25767
25768/*
25769** Enter the mutex (do not block).
25770*/
25771static int checkMutexTry(sqlite3_mutex *p){
25772  CheckMutex *pCheck = (CheckMutex*)p;
25773  return pGlobalMutexMethods->xMutexTry(pCheck->mutex);
25774}
25775
25776/*
25777** Leave the mutex.
25778*/
25779static void checkMutexLeave(sqlite3_mutex *p){
25780  CheckMutex *pCheck = (CheckMutex*)p;
25781  pGlobalMutexMethods->xMutexLeave(pCheck->mutex);
25782}
25783
25784sqlite3_mutex_methods const *multiThreadedCheckMutex(void){
25785  static const sqlite3_mutex_methods sMutex = {
25786    checkMutexInit,
25787    checkMutexEnd,
25788    checkMutexAlloc,
25789    checkMutexFree,
25790    checkMutexEnter,
25791    checkMutexTry,
25792    checkMutexLeave,
25793#ifdef SQLITE_DEBUG
25794    checkMutexHeld,
25795    checkMutexNotheld
25796#else
25797    0,
25798    0
25799#endif
25800  };
25801  return &sMutex;
25802}
25803
25804/*
25805** Mark the SQLITE_MUTEX_RECURSIVE mutex passed as the only argument as
25806** one on which there should be no contention.
25807*/
25808SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex *p){
25809  if( sqlite3GlobalConfig.mutex.xMutexAlloc==checkMutexAlloc ){
25810    CheckMutex *pCheck = (CheckMutex*)p;
25811    assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE );
25812    pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION;
25813  }
25814}
25815#endif   /* ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS */
25816
25817/*
25818** Initialize the mutex system.
25819*/
25820SQLITE_PRIVATE int sqlite3MutexInit(void){
25821  int rc = SQLITE_OK;
25822  if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
25823    /* If the xMutexAlloc method has not been set, then the user did not
25824    ** install a mutex implementation via sqlite3_config() prior to
25825    ** sqlite3_initialize() being called. This block copies pointers to
25826    ** the default implementation into the sqlite3GlobalConfig structure.
25827    */
25828    sqlite3_mutex_methods const *pFrom;
25829    sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
25830
25831    if( sqlite3GlobalConfig.bCoreMutex ){
25832#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
25833      pFrom = multiThreadedCheckMutex();
25834#else
25835      pFrom = sqlite3DefaultMutex();
25836#endif
25837    }else{
25838      pFrom = sqlite3NoopMutex();
25839    }
25840    pTo->xMutexInit = pFrom->xMutexInit;
25841    pTo->xMutexEnd = pFrom->xMutexEnd;
25842    pTo->xMutexFree = pFrom->xMutexFree;
25843    pTo->xMutexEnter = pFrom->xMutexEnter;
25844    pTo->xMutexTry = pFrom->xMutexTry;
25845    pTo->xMutexLeave = pFrom->xMutexLeave;
25846    pTo->xMutexHeld = pFrom->xMutexHeld;
25847    pTo->xMutexNotheld = pFrom->xMutexNotheld;
25848    sqlite3MemoryBarrier();
25849    pTo->xMutexAlloc = pFrom->xMutexAlloc;
25850  }
25851  assert( sqlite3GlobalConfig.mutex.xMutexInit );
25852  rc = sqlite3GlobalConfig.mutex.xMutexInit();
25853
25854#ifdef SQLITE_DEBUG
25855  GLOBAL(int, mutexIsInit) = 1;
25856#endif
25857
25858  sqlite3MemoryBarrier();
25859  return rc;
25860}
25861
25862/*
25863** Shutdown the mutex system. This call frees resources allocated by
25864** sqlite3MutexInit().
25865*/
25866SQLITE_PRIVATE int sqlite3MutexEnd(void){
25867  int rc = SQLITE_OK;
25868  if( sqlite3GlobalConfig.mutex.xMutexEnd ){
25869    rc = sqlite3GlobalConfig.mutex.xMutexEnd();
25870  }
25871
25872#ifdef SQLITE_DEBUG
25873  GLOBAL(int, mutexIsInit) = 0;
25874#endif
25875
25876  return rc;
25877}
25878
25879/*
25880** Retrieve a pointer to a static mutex or allocate a new dynamic one.
25881*/
25882SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
25883#ifndef SQLITE_OMIT_AUTOINIT
25884  if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
25885  if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0;
25886#endif
25887  assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
25888  return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
25889}
25890
25891SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
25892  if( !sqlite3GlobalConfig.bCoreMutex ){
25893    return 0;
25894  }
25895  assert( GLOBAL(int, mutexIsInit) );
25896  assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
25897  return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
25898}
25899
25900/*
25901** Free a dynamic mutex.
25902*/
25903SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
25904  if( p ){
25905    assert( sqlite3GlobalConfig.mutex.xMutexFree );
25906    sqlite3GlobalConfig.mutex.xMutexFree(p);
25907  }
25908}
25909
25910/*
25911** Obtain the mutex p. If some other thread already has the mutex, block
25912** until it can be obtained.
25913*/
25914SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
25915  if( p ){
25916    assert( sqlite3GlobalConfig.mutex.xMutexEnter );
25917    sqlite3GlobalConfig.mutex.xMutexEnter(p);
25918  }
25919}
25920
25921/*
25922** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
25923** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
25924*/
25925SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
25926  int rc = SQLITE_OK;
25927  if( p ){
25928    assert( sqlite3GlobalConfig.mutex.xMutexTry );
25929    return sqlite3GlobalConfig.mutex.xMutexTry(p);
25930  }
25931  return rc;
25932}
25933
25934/*
25935** The sqlite3_mutex_leave() routine exits a mutex that was previously
25936** entered by the same thread.  The behavior is undefined if the mutex
25937** is not currently entered. If a NULL pointer is passed as an argument
25938** this function is a no-op.
25939*/
25940SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
25941  if( p ){
25942    assert( sqlite3GlobalConfig.mutex.xMutexLeave );
25943    sqlite3GlobalConfig.mutex.xMutexLeave(p);
25944  }
25945}
25946
25947#ifndef NDEBUG
25948/*
25949** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
25950** intended for use inside assert() statements.
25951*/
25952SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
25953  assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld );
25954  return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
25955}
25956SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
25957  assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld );
25958  return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
25959}
25960#endif
25961
25962#endif /* !defined(SQLITE_MUTEX_OMIT) */
25963
25964/************** End of mutex.c ***********************************************/
25965/************** Begin file mutex_noop.c **************************************/
25966/*
25967** 2008 October 07
25968**
25969** The author disclaims copyright to this source code.  In place of
25970** a legal notice, here is a blessing:
25971**
25972**    May you do good and not evil.
25973**    May you find forgiveness for yourself and forgive others.
25974**    May you share freely, never taking more than you give.
25975**
25976*************************************************************************
25977** This file contains the C functions that implement mutexes.
25978**
25979** This implementation in this file does not provide any mutual
25980** exclusion and is thus suitable for use only in applications
25981** that use SQLite in a single thread.  The routines defined
25982** here are place-holders.  Applications can substitute working
25983** mutex routines at start-time using the
25984**
25985**     sqlite3_config(SQLITE_CONFIG_MUTEX,...)
25986**
25987** interface.
25988**
25989** If compiled with SQLITE_DEBUG, then additional logic is inserted
25990** that does error checking on mutexes to make sure they are being
25991** called correctly.
25992*/
25993/* #include "sqliteInt.h" */
25994
25995#ifndef SQLITE_MUTEX_OMIT
25996
25997#ifndef SQLITE_DEBUG
25998/*
25999** Stub routines for all mutex methods.
26000**
26001** This routines provide no mutual exclusion or error checking.
26002*/
26003static int noopMutexInit(void){ return SQLITE_OK; }
26004static int noopMutexEnd(void){ return SQLITE_OK; }
26005static sqlite3_mutex *noopMutexAlloc(int id){
26006  UNUSED_PARAMETER(id);
26007  return (sqlite3_mutex*)8;
26008}
26009static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
26010static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
26011static int noopMutexTry(sqlite3_mutex *p){
26012  UNUSED_PARAMETER(p);
26013  return SQLITE_OK;
26014}
26015static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
26016
26017SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
26018  static const sqlite3_mutex_methods sMutex = {
26019    noopMutexInit,
26020    noopMutexEnd,
26021    noopMutexAlloc,
26022    noopMutexFree,
26023    noopMutexEnter,
26024    noopMutexTry,
26025    noopMutexLeave,
26026
26027    0,
26028    0,
26029  };
26030
26031  return &sMutex;
26032}
26033#endif /* !SQLITE_DEBUG */
26034
26035#ifdef SQLITE_DEBUG
26036/*
26037** In this implementation, error checking is provided for testing
26038** and debugging purposes.  The mutexes still do not provide any
26039** mutual exclusion.
26040*/
26041
26042/*
26043** The mutex object
26044*/
26045typedef struct sqlite3_debug_mutex {
26046  int id;     /* The mutex type */
26047  int cnt;    /* Number of entries without a matching leave */
26048} sqlite3_debug_mutex;
26049
26050/*
26051** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
26052** intended for use inside assert() statements.
26053*/
26054static int debugMutexHeld(sqlite3_mutex *pX){
26055  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
26056  return p==0 || p->cnt>0;
26057}
26058static int debugMutexNotheld(sqlite3_mutex *pX){
26059  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
26060  return p==0 || p->cnt==0;
26061}
26062
26063/*
26064** Initialize and deinitialize the mutex subsystem.
26065*/
26066static int debugMutexInit(void){ return SQLITE_OK; }
26067static int debugMutexEnd(void){ return SQLITE_OK; }
26068
26069/*
26070** The sqlite3_mutex_alloc() routine allocates a new
26071** mutex and returns a pointer to it.  If it returns NULL
26072** that means that a mutex could not be allocated.
26073*/
26074static sqlite3_mutex *debugMutexAlloc(int id){
26075  static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1];
26076  sqlite3_debug_mutex *pNew = 0;
26077  switch( id ){
26078    case SQLITE_MUTEX_FAST:
26079    case SQLITE_MUTEX_RECURSIVE: {
26080      pNew = sqlite3Malloc(sizeof(*pNew));
26081      if( pNew ){
26082        pNew->id = id;
26083        pNew->cnt = 0;
26084      }
26085      break;
26086    }
26087    default: {
26088#ifdef SQLITE_ENABLE_API_ARMOR
26089      if( id-2<0 || id-2>=ArraySize(aStatic) ){
26090        (void)SQLITE_MISUSE_BKPT;
26091        return 0;
26092      }
26093#endif
26094      pNew = &aStatic[id-2];
26095      pNew->id = id;
26096      break;
26097    }
26098  }
26099  return (sqlite3_mutex*)pNew;
26100}
26101
26102/*
26103** This routine deallocates a previously allocated mutex.
26104*/
26105static void debugMutexFree(sqlite3_mutex *pX){
26106  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
26107  assert( p->cnt==0 );
26108  if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){
26109    sqlite3_free(p);
26110  }else{
26111#ifdef SQLITE_ENABLE_API_ARMOR
26112    (void)SQLITE_MISUSE_BKPT;
26113#endif
26114  }
26115}
26116
26117/*
26118** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
26119** to enter a mutex.  If another thread is already within the mutex,
26120** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
26121** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
26122** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
26123** be entered multiple times by the same thread.  In such cases the,
26124** mutex must be exited an equal number of times before another thread
26125** can enter.  If the same thread tries to enter any other kind of mutex
26126** more than once, the behavior is undefined.
26127*/
26128static void debugMutexEnter(sqlite3_mutex *pX){
26129  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
26130  assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
26131  p->cnt++;
26132}
26133static int debugMutexTry(sqlite3_mutex *pX){
26134  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
26135  assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
26136  p->cnt++;
26137  return SQLITE_OK;
26138}
26139
26140/*
26141** The sqlite3_mutex_leave() routine exits a mutex that was
26142** previously entered by the same thread.  The behavior
26143** is undefined if the mutex is not currently entered or
26144** is not currently allocated.  SQLite will never do either.
26145*/
26146static void debugMutexLeave(sqlite3_mutex *pX){
26147  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
26148  assert( debugMutexHeld(pX) );
26149  p->cnt--;
26150  assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
26151}
26152
26153SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
26154  static const sqlite3_mutex_methods sMutex = {
26155    debugMutexInit,
26156    debugMutexEnd,
26157    debugMutexAlloc,
26158    debugMutexFree,
26159    debugMutexEnter,
26160    debugMutexTry,
26161    debugMutexLeave,
26162
26163    debugMutexHeld,
26164    debugMutexNotheld
26165  };
26166
26167  return &sMutex;
26168}
26169#endif /* SQLITE_DEBUG */
26170
26171/*
26172** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
26173** is used regardless of the run-time threadsafety setting.
26174*/
26175#ifdef SQLITE_MUTEX_NOOP
26176SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
26177  return sqlite3NoopMutex();
26178}
26179#endif /* defined(SQLITE_MUTEX_NOOP) */
26180#endif /* !defined(SQLITE_MUTEX_OMIT) */
26181
26182/************** End of mutex_noop.c ******************************************/
26183/************** Begin file mutex_unix.c **************************************/
26184/*
26185** 2007 August 28
26186**
26187** The author disclaims copyright to this source code.  In place of
26188** a legal notice, here is a blessing:
26189**
26190**    May you do good and not evil.
26191**    May you find forgiveness for yourself and forgive others.
26192**    May you share freely, never taking more than you give.
26193**
26194*************************************************************************
26195** This file contains the C functions that implement mutexes for pthreads
26196*/
26197/* #include "sqliteInt.h" */
26198
26199/*
26200** The code in this file is only used if we are compiling threadsafe
26201** under unix with pthreads.
26202**
26203** Note that this implementation requires a version of pthreads that
26204** supports recursive mutexes.
26205*/
26206#ifdef SQLITE_MUTEX_PTHREADS
26207
26208#include <pthread.h>
26209
26210/*
26211** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
26212** are necessary under two condidtions:  (1) Debug builds and (2) using
26213** home-grown mutexes.  Encapsulate these conditions into a single #define.
26214*/
26215#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
26216# define SQLITE_MUTEX_NREF 1
26217#else
26218# define SQLITE_MUTEX_NREF 0
26219#endif
26220
26221/*
26222** Each recursive mutex is an instance of the following structure.
26223*/
26224struct sqlite3_mutex {
26225  pthread_mutex_t mutex;     /* Mutex controlling the lock */
26226#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
26227  int id;                    /* Mutex type */
26228#endif
26229#if SQLITE_MUTEX_NREF
26230  volatile int nRef;         /* Number of entrances */
26231  volatile pthread_t owner;  /* Thread that is within this mutex */
26232  int trace;                 /* True to trace changes */
26233#endif
26234};
26235#if SQLITE_MUTEX_NREF
26236# define SQLITE3_MUTEX_INITIALIZER(id) \
26237     {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0}
26238#elif defined(SQLITE_ENABLE_API_ARMOR)
26239# define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id }
26240#else
26241#define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER }
26242#endif
26243
26244/*
26245** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
26246** intended for use only inside assert() statements.  On some platforms,
26247** there might be race conditions that can cause these routines to
26248** deliver incorrect results.  In particular, if pthread_equal() is
26249** not an atomic operation, then these routines might delivery
26250** incorrect results.  On most platforms, pthread_equal() is a
26251** comparison of two integers and is therefore atomic.  But we are
26252** told that HPUX is not such a platform.  If so, then these routines
26253** will not always work correctly on HPUX.
26254**
26255** On those platforms where pthread_equal() is not atomic, SQLite
26256** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
26257** make sure no assert() statements are evaluated and hence these
26258** routines are never called.
26259*/
26260#if !defined(NDEBUG) || defined(SQLITE_DEBUG)
26261static int pthreadMutexHeld(sqlite3_mutex *p){
26262  return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
26263}
26264static int pthreadMutexNotheld(sqlite3_mutex *p){
26265  return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
26266}
26267#endif
26268
26269/*
26270** Try to provide a memory barrier operation, needed for initialization
26271** and also for the implementation of xShmBarrier in the VFS in cases
26272** where SQLite is compiled without mutexes.
26273*/
26274SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
26275#if defined(SQLITE_MEMORY_BARRIER)
26276  SQLITE_MEMORY_BARRIER;
26277#elif defined(__GNUC__) && GCC_VERSION>=4001000
26278  __sync_synchronize();
26279#endif
26280}
26281
26282/*
26283** Initialize and deinitialize the mutex subsystem.
26284*/
26285static int pthreadMutexInit(void){ return SQLITE_OK; }
26286static int pthreadMutexEnd(void){ return SQLITE_OK; }
26287
26288/*
26289** The sqlite3_mutex_alloc() routine allocates a new
26290** mutex and returns a pointer to it.  If it returns NULL
26291** that means that a mutex could not be allocated.  SQLite
26292** will unwind its stack and return an error.  The argument
26293** to sqlite3_mutex_alloc() is one of these integer constants:
26294**
26295** <ul>
26296** <li>  SQLITE_MUTEX_FAST
26297** <li>  SQLITE_MUTEX_RECURSIVE
26298** <li>  SQLITE_MUTEX_STATIC_MASTER
26299** <li>  SQLITE_MUTEX_STATIC_MEM
26300** <li>  SQLITE_MUTEX_STATIC_OPEN
26301** <li>  SQLITE_MUTEX_STATIC_PRNG
26302** <li>  SQLITE_MUTEX_STATIC_LRU
26303** <li>  SQLITE_MUTEX_STATIC_PMEM
26304** <li>  SQLITE_MUTEX_STATIC_APP1
26305** <li>  SQLITE_MUTEX_STATIC_APP2
26306** <li>  SQLITE_MUTEX_STATIC_APP3
26307** <li>  SQLITE_MUTEX_STATIC_VFS1
26308** <li>  SQLITE_MUTEX_STATIC_VFS2
26309** <li>  SQLITE_MUTEX_STATIC_VFS3
26310** </ul>
26311**
26312** The first two constants cause sqlite3_mutex_alloc() to create
26313** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
26314** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
26315** The mutex implementation does not need to make a distinction
26316** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
26317** not want to.  But SQLite will only request a recursive mutex in
26318** cases where it really needs one.  If a faster non-recursive mutex
26319** implementation is available on the host platform, the mutex subsystem
26320** might return such a mutex in response to SQLITE_MUTEX_FAST.
26321**
26322** The other allowed parameters to sqlite3_mutex_alloc() each return
26323** a pointer to a static preexisting mutex.  Six static mutexes are
26324** used by the current version of SQLite.  Future versions of SQLite
26325** may add additional static mutexes.  Static mutexes are for internal
26326** use by SQLite only.  Applications that use SQLite mutexes should
26327** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
26328** SQLITE_MUTEX_RECURSIVE.
26329**
26330** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
26331** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
26332** returns a different mutex on every call.  But for the static
26333** mutex types, the same mutex is returned on every call that has
26334** the same type number.
26335*/
26336static sqlite3_mutex *pthreadMutexAlloc(int iType){
26337  static sqlite3_mutex staticMutexes[] = {
26338    SQLITE3_MUTEX_INITIALIZER(2),
26339    SQLITE3_MUTEX_INITIALIZER(3),
26340    SQLITE3_MUTEX_INITIALIZER(4),
26341    SQLITE3_MUTEX_INITIALIZER(5),
26342    SQLITE3_MUTEX_INITIALIZER(6),
26343    SQLITE3_MUTEX_INITIALIZER(7),
26344    SQLITE3_MUTEX_INITIALIZER(8),
26345    SQLITE3_MUTEX_INITIALIZER(9),
26346    SQLITE3_MUTEX_INITIALIZER(10),
26347    SQLITE3_MUTEX_INITIALIZER(11),
26348    SQLITE3_MUTEX_INITIALIZER(12),
26349    SQLITE3_MUTEX_INITIALIZER(13)
26350  };
26351  sqlite3_mutex *p;
26352  switch( iType ){
26353    case SQLITE_MUTEX_RECURSIVE: {
26354      p = sqlite3MallocZero( sizeof(*p) );
26355      if( p ){
26356#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
26357        /* If recursive mutexes are not available, we will have to
26358        ** build our own.  See below. */
26359        pthread_mutex_init(&p->mutex, 0);
26360#else
26361        /* Use a recursive mutex if it is available */
26362        pthread_mutexattr_t recursiveAttr;
26363        pthread_mutexattr_init(&recursiveAttr);
26364        pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
26365        pthread_mutex_init(&p->mutex, &recursiveAttr);
26366        pthread_mutexattr_destroy(&recursiveAttr);
26367#endif
26368#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
26369        p->id = SQLITE_MUTEX_RECURSIVE;
26370#endif
26371      }
26372      break;
26373    }
26374    case SQLITE_MUTEX_FAST: {
26375      p = sqlite3MallocZero( sizeof(*p) );
26376      if( p ){
26377        pthread_mutex_init(&p->mutex, 0);
26378#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
26379        p->id = SQLITE_MUTEX_FAST;
26380#endif
26381      }
26382      break;
26383    }
26384    default: {
26385#ifdef SQLITE_ENABLE_API_ARMOR
26386      if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
26387        (void)SQLITE_MISUSE_BKPT;
26388        return 0;
26389      }
26390#endif
26391      p = &staticMutexes[iType-2];
26392      break;
26393    }
26394  }
26395#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
26396  assert( p==0 || p->id==iType );
26397#endif
26398  return p;
26399}
26400
26401
26402/*
26403** This routine deallocates a previously
26404** allocated mutex.  SQLite is careful to deallocate every
26405** mutex that it allocates.
26406*/
26407static void pthreadMutexFree(sqlite3_mutex *p){
26408  assert( p->nRef==0 );
26409#if SQLITE_ENABLE_API_ARMOR
26410  if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE )
26411#endif
26412  {
26413    pthread_mutex_destroy(&p->mutex);
26414    sqlite3_free(p);
26415  }
26416#ifdef SQLITE_ENABLE_API_ARMOR
26417  else{
26418    (void)SQLITE_MISUSE_BKPT;
26419  }
26420#endif
26421}
26422
26423/*
26424** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
26425** to enter a mutex.  If another thread is already within the mutex,
26426** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
26427** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
26428** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
26429** be entered multiple times by the same thread.  In such cases the,
26430** mutex must be exited an equal number of times before another thread
26431** can enter.  If the same thread tries to enter any other kind of mutex
26432** more than once, the behavior is undefined.
26433*/
26434static void pthreadMutexEnter(sqlite3_mutex *p){
26435  assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
26436
26437#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
26438  /* If recursive mutexes are not available, then we have to grow
26439  ** our own.  This implementation assumes that pthread_equal()
26440  ** is atomic - that it cannot be deceived into thinking self
26441  ** and p->owner are equal if p->owner changes between two values
26442  ** that are not equal to self while the comparison is taking place.
26443  ** This implementation also assumes a coherent cache - that
26444  ** separate processes cannot read different values from the same
26445  ** address at the same time.  If either of these two conditions
26446  ** are not met, then the mutexes will fail and problems will result.
26447  */
26448  {
26449    pthread_t self = pthread_self();
26450    if( p->nRef>0 && pthread_equal(p->owner, self) ){
26451      p->nRef++;
26452    }else{
26453      pthread_mutex_lock(&p->mutex);
26454      assert( p->nRef==0 );
26455      p->owner = self;
26456      p->nRef = 1;
26457    }
26458  }
26459#else
26460  /* Use the built-in recursive mutexes if they are available.
26461  */
26462  pthread_mutex_lock(&p->mutex);
26463#if SQLITE_MUTEX_NREF
26464  assert( p->nRef>0 || p->owner==0 );
26465  p->owner = pthread_self();
26466  p->nRef++;
26467#endif
26468#endif
26469
26470#ifdef SQLITE_DEBUG
26471  if( p->trace ){
26472    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
26473  }
26474#endif
26475}
26476static int pthreadMutexTry(sqlite3_mutex *p){
26477  int rc;
26478  assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
26479
26480#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
26481  /* If recursive mutexes are not available, then we have to grow
26482  ** our own.  This implementation assumes that pthread_equal()
26483  ** is atomic - that it cannot be deceived into thinking self
26484  ** and p->owner are equal if p->owner changes between two values
26485  ** that are not equal to self while the comparison is taking place.
26486  ** This implementation also assumes a coherent cache - that
26487  ** separate processes cannot read different values from the same
26488  ** address at the same time.  If either of these two conditions
26489  ** are not met, then the mutexes will fail and problems will result.
26490  */
26491  {
26492    pthread_t self = pthread_self();
26493    if( p->nRef>0 && pthread_equal(p->owner, self) ){
26494      p->nRef++;
26495      rc = SQLITE_OK;
26496    }else if( pthread_mutex_trylock(&p->mutex)==0 ){
26497      assert( p->nRef==0 );
26498      p->owner = self;
26499      p->nRef = 1;
26500      rc = SQLITE_OK;
26501    }else{
26502      rc = SQLITE_BUSY;
26503    }
26504  }
26505#else
26506  /* Use the built-in recursive mutexes if they are available.
26507  */
26508  if( pthread_mutex_trylock(&p->mutex)==0 ){
26509#if SQLITE_MUTEX_NREF
26510    p->owner = pthread_self();
26511    p->nRef++;
26512#endif
26513    rc = SQLITE_OK;
26514  }else{
26515    rc = SQLITE_BUSY;
26516  }
26517#endif
26518
26519#ifdef SQLITE_DEBUG
26520  if( rc==SQLITE_OK && p->trace ){
26521    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
26522  }
26523#endif
26524  return rc;
26525}
26526
26527/*
26528** The sqlite3_mutex_leave() routine exits a mutex that was
26529** previously entered by the same thread.  The behavior
26530** is undefined if the mutex is not currently entered or
26531** is not currently allocated.  SQLite will never do either.
26532*/
26533static void pthreadMutexLeave(sqlite3_mutex *p){
26534  assert( pthreadMutexHeld(p) );
26535#if SQLITE_MUTEX_NREF
26536  p->nRef--;
26537  if( p->nRef==0 ) p->owner = 0;
26538#endif
26539  assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
26540
26541#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
26542  if( p->nRef==0 ){
26543    pthread_mutex_unlock(&p->mutex);
26544  }
26545#else
26546  pthread_mutex_unlock(&p->mutex);
26547#endif
26548
26549#ifdef SQLITE_DEBUG
26550  if( p->trace ){
26551    printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
26552  }
26553#endif
26554}
26555
26556SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
26557  static const sqlite3_mutex_methods sMutex = {
26558    pthreadMutexInit,
26559    pthreadMutexEnd,
26560    pthreadMutexAlloc,
26561    pthreadMutexFree,
26562    pthreadMutexEnter,
26563    pthreadMutexTry,
26564    pthreadMutexLeave,
26565#ifdef SQLITE_DEBUG
26566    pthreadMutexHeld,
26567    pthreadMutexNotheld
26568#else
26569    0,
26570    0
26571#endif
26572  };
26573
26574  return &sMutex;
26575}
26576
26577#endif /* SQLITE_MUTEX_PTHREADS */
26578
26579/************** End of mutex_unix.c ******************************************/
26580/************** Begin file mutex_w32.c ***************************************/
26581/*
26582** 2007 August 14
26583**
26584** The author disclaims copyright to this source code.  In place of
26585** a legal notice, here is a blessing:
26586**
26587**    May you do good and not evil.
26588**    May you find forgiveness for yourself and forgive others.
26589**    May you share freely, never taking more than you give.
26590**
26591*************************************************************************
26592** This file contains the C functions that implement mutexes for Win32.
26593*/
26594/* #include "sqliteInt.h" */
26595
26596#if SQLITE_OS_WIN
26597/*
26598** Include code that is common to all os_*.c files
26599*/
26600/************** Include os_common.h in the middle of mutex_w32.c *************/
26601/************** Begin file os_common.h ***************************************/
26602/*
26603** 2004 May 22
26604**
26605** The author disclaims copyright to this source code.  In place of
26606** a legal notice, here is a blessing:
26607**
26608**    May you do good and not evil.
26609**    May you find forgiveness for yourself and forgive others.
26610**    May you share freely, never taking more than you give.
26611**
26612******************************************************************************
26613**
26614** This file contains macros and a little bit of code that is common to
26615** all of the platform-specific files (os_*.c) and is #included into those
26616** files.
26617**
26618** This file should be #included by the os_*.c files only.  It is not a
26619** general purpose header file.
26620*/
26621#ifndef _OS_COMMON_H_
26622#define _OS_COMMON_H_
26623
26624/*
26625** At least two bugs have slipped in because we changed the MEMORY_DEBUG
26626** macro to SQLITE_DEBUG and some older makefiles have not yet made the
26627** switch.  The following code should catch this problem at compile-time.
26628*/
26629#ifdef MEMORY_DEBUG
26630# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
26631#endif
26632
26633/*
26634** Macros for performance tracing.  Normally turned off.  Only works
26635** on i486 hardware.
26636*/
26637#ifdef SQLITE_PERFORMANCE_TRACE
26638
26639/*
26640** hwtime.h contains inline assembler code for implementing
26641** high-performance timing routines.
26642*/
26643/************** Include hwtime.h in the middle of os_common.h ****************/
26644/************** Begin file hwtime.h ******************************************/
26645/*
26646** 2008 May 27
26647**
26648** The author disclaims copyright to this source code.  In place of
26649** a legal notice, here is a blessing:
26650**
26651**    May you do good and not evil.
26652**    May you find forgiveness for yourself and forgive others.
26653**    May you share freely, never taking more than you give.
26654**
26655******************************************************************************
26656**
26657** This file contains inline asm code for retrieving "high-performance"
26658** counters for x86 and x86_64 class CPUs.
26659*/
26660#ifndef SQLITE_HWTIME_H
26661#define SQLITE_HWTIME_H
26662
26663/*
26664** The following routine only works on pentium-class (or newer) processors.
26665** It uses the RDTSC opcode to read the cycle count value out of the
26666** processor and returns that value.  This can be used for high-res
26667** profiling.
26668*/
26669#if !defined(__STRICT_ANSI__) && \
26670    (defined(__GNUC__) || defined(_MSC_VER)) && \
26671    (defined(i386) || defined(__i386__) || defined(_M_IX86))
26672
26673  #if defined(__GNUC__)
26674
26675  __inline__ sqlite_uint64 sqlite3Hwtime(void){
26676     unsigned int lo, hi;
26677     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
26678     return (sqlite_uint64)hi << 32 | lo;
26679  }
26680
26681  #elif defined(_MSC_VER)
26682
26683  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
26684     __asm {
26685        rdtsc
26686        ret       ; return value at EDX:EAX
26687     }
26688  }
26689
26690  #endif
26691
26692#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__x86_64__))
26693
26694  __inline__ sqlite_uint64 sqlite3Hwtime(void){
26695      unsigned long val;
26696      __asm__ __volatile__ ("rdtsc" : "=A" (val));
26697      return val;
26698  }
26699
26700#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__ppc__))
26701
26702  __inline__ sqlite_uint64 sqlite3Hwtime(void){
26703      unsigned long long retval;
26704      unsigned long junk;
26705      __asm__ __volatile__ ("\n\
26706          1:      mftbu   %1\n\
26707                  mftb    %L0\n\
26708                  mftbu   %0\n\
26709                  cmpw    %0,%1\n\
26710                  bne     1b"
26711                  : "=r" (retval), "=r" (junk));
26712      return retval;
26713  }
26714
26715#else
26716
26717  /*
26718  ** asm() is needed for hardware timing support.  Without asm(),
26719  ** disable the sqlite3Hwtime() routine.
26720  **
26721  ** sqlite3Hwtime() is only used for some obscure debugging
26722  ** and analysis configurations, not in any deliverable, so this
26723  ** should not be a great loss.
26724  */
26725SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
26726
26727#endif
26728
26729#endif /* !defined(SQLITE_HWTIME_H) */
26730
26731/************** End of hwtime.h **********************************************/
26732/************** Continuing where we left off in os_common.h ******************/
26733
26734static sqlite_uint64 g_start;
26735static sqlite_uint64 g_elapsed;
26736#define TIMER_START       g_start=sqlite3Hwtime()
26737#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
26738#define TIMER_ELAPSED     g_elapsed
26739#else
26740#define TIMER_START
26741#define TIMER_END
26742#define TIMER_ELAPSED     ((sqlite_uint64)0)
26743#endif
26744
26745/*
26746** If we compile with the SQLITE_TEST macro set, then the following block
26747** of code will give us the ability to simulate a disk I/O error.  This
26748** is used for testing the I/O recovery logic.
26749*/
26750#if defined(SQLITE_TEST)
26751SQLITE_API extern int sqlite3_io_error_hit;
26752SQLITE_API extern int sqlite3_io_error_hardhit;
26753SQLITE_API extern int sqlite3_io_error_pending;
26754SQLITE_API extern int sqlite3_io_error_persist;
26755SQLITE_API extern int sqlite3_io_error_benign;
26756SQLITE_API extern int sqlite3_diskfull_pending;
26757SQLITE_API extern int sqlite3_diskfull;
26758#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
26759#define SimulateIOError(CODE)  \
26760  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
26761       || sqlite3_io_error_pending-- == 1 )  \
26762              { local_ioerr(); CODE; }
26763static void local_ioerr(){
26764  IOTRACE(("IOERR\n"));
26765  sqlite3_io_error_hit++;
26766  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
26767}
26768#define SimulateDiskfullError(CODE) \
26769   if( sqlite3_diskfull_pending ){ \
26770     if( sqlite3_diskfull_pending == 1 ){ \
26771       local_ioerr(); \
26772       sqlite3_diskfull = 1; \
26773       sqlite3_io_error_hit = 1; \
26774       CODE; \
26775     }else{ \
26776       sqlite3_diskfull_pending--; \
26777     } \
26778   }
26779#else
26780#define SimulateIOErrorBenign(X)
26781#define SimulateIOError(A)
26782#define SimulateDiskfullError(A)
26783#endif /* defined(SQLITE_TEST) */
26784
26785/*
26786** When testing, keep a count of the number of open files.
26787*/
26788#if defined(SQLITE_TEST)
26789SQLITE_API extern int sqlite3_open_file_count;
26790#define OpenCounter(X)  sqlite3_open_file_count+=(X)
26791#else
26792#define OpenCounter(X)
26793#endif /* defined(SQLITE_TEST) */
26794
26795#endif /* !defined(_OS_COMMON_H_) */
26796
26797/************** End of os_common.h *******************************************/
26798/************** Continuing where we left off in mutex_w32.c ******************/
26799
26800/*
26801** Include the header file for the Windows VFS.
26802*/
26803/************** Include os_win.h in the middle of mutex_w32.c ****************/
26804/************** Begin file os_win.h ******************************************/
26805/*
26806** 2013 November 25
26807**
26808** The author disclaims copyright to this source code.  In place of
26809** a legal notice, here is a blessing:
26810**
26811**    May you do good and not evil.
26812**    May you find forgiveness for yourself and forgive others.
26813**    May you share freely, never taking more than you give.
26814**
26815******************************************************************************
26816**
26817** This file contains code that is specific to Windows.
26818*/
26819#ifndef SQLITE_OS_WIN_H
26820#define SQLITE_OS_WIN_H
26821
26822/*
26823** Include the primary Windows SDK header file.
26824*/
26825#include "windows.h"
26826
26827#ifdef __CYGWIN__
26828# include <sys/cygwin.h>
26829# include <errno.h> /* amalgamator: dontcache */
26830#endif
26831
26832/*
26833** Determine if we are dealing with Windows NT.
26834**
26835** We ought to be able to determine if we are compiling for Windows 9x or
26836** Windows NT using the _WIN32_WINNT macro as follows:
26837**
26838** #if defined(_WIN32_WINNT)
26839** # define SQLITE_OS_WINNT 1
26840** #else
26841** # define SQLITE_OS_WINNT 0
26842** #endif
26843**
26844** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
26845** it ought to, so the above test does not work.  We'll just assume that
26846** everything is Windows NT unless the programmer explicitly says otherwise
26847** by setting SQLITE_OS_WINNT to 0.
26848*/
26849#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
26850# define SQLITE_OS_WINNT 1
26851#endif
26852
26853/*
26854** Determine if we are dealing with Windows CE - which has a much reduced
26855** API.
26856*/
26857#if defined(_WIN32_WCE)
26858# define SQLITE_OS_WINCE 1
26859#else
26860# define SQLITE_OS_WINCE 0
26861#endif
26862
26863/*
26864** Determine if we are dealing with WinRT, which provides only a subset of
26865** the full Win32 API.
26866*/
26867#if !defined(SQLITE_OS_WINRT)
26868# define SQLITE_OS_WINRT 0
26869#endif
26870
26871/*
26872** For WinCE, some API function parameters do not appear to be declared as
26873** volatile.
26874*/
26875#if SQLITE_OS_WINCE
26876# define SQLITE_WIN32_VOLATILE
26877#else
26878# define SQLITE_WIN32_VOLATILE volatile
26879#endif
26880
26881/*
26882** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
26883** functions are not available (e.g. those not using MSVC, Cygwin, etc).
26884*/
26885#if SQLITE_OS_WIN && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
26886    SQLITE_THREADSAFE>0 && !defined(__CYGWIN__)
26887# define SQLITE_OS_WIN_THREADS 1
26888#else
26889# define SQLITE_OS_WIN_THREADS 0
26890#endif
26891
26892#endif /* SQLITE_OS_WIN_H */
26893
26894/************** End of os_win.h **********************************************/
26895/************** Continuing where we left off in mutex_w32.c ******************/
26896#endif
26897
26898/*
26899** The code in this file is only used if we are compiling multithreaded
26900** on a Win32 system.
26901*/
26902#ifdef SQLITE_MUTEX_W32
26903
26904/*
26905** Each recursive mutex is an instance of the following structure.
26906*/
26907struct sqlite3_mutex {
26908  CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
26909  int id;                    /* Mutex type */
26910#ifdef SQLITE_DEBUG
26911  volatile int nRef;         /* Number of enterances */
26912  volatile DWORD owner;      /* Thread holding this mutex */
26913  volatile LONG trace;       /* True to trace changes */
26914#endif
26915};
26916
26917/*
26918** These are the initializer values used when declaring a "static" mutex
26919** on Win32.  It should be noted that all mutexes require initialization
26920** on the Win32 platform.
26921*/
26922#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
26923
26924#ifdef SQLITE_DEBUG
26925#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id, \
26926                                    0L, (DWORD)0, 0 }
26927#else
26928#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id }
26929#endif
26930
26931#ifdef SQLITE_DEBUG
26932/*
26933** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
26934** intended for use only inside assert() statements.
26935*/
26936static int winMutexHeld(sqlite3_mutex *p){
26937  return p->nRef!=0 && p->owner==GetCurrentThreadId();
26938}
26939
26940static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
26941  return p->nRef==0 || p->owner!=tid;
26942}
26943
26944static int winMutexNotheld(sqlite3_mutex *p){
26945  DWORD tid = GetCurrentThreadId();
26946  return winMutexNotheld2(p, tid);
26947}
26948#endif
26949
26950/*
26951** Try to provide a memory barrier operation, needed for initialization
26952** and also for the xShmBarrier method of the VFS in cases when SQLite is
26953** compiled without mutexes (SQLITE_THREADSAFE=0).
26954*/
26955SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
26956#if defined(SQLITE_MEMORY_BARRIER)
26957  SQLITE_MEMORY_BARRIER;
26958#elif defined(__GNUC__)
26959  __sync_synchronize();
26960#elif MSVC_VERSION>=1300
26961  _ReadWriteBarrier();
26962#elif defined(MemoryBarrier)
26963  MemoryBarrier();
26964#endif
26965}
26966
26967/*
26968** Initialize and deinitialize the mutex subsystem.
26969*/
26970static sqlite3_mutex winMutex_staticMutexes[] = {
26971  SQLITE3_MUTEX_INITIALIZER(2),
26972  SQLITE3_MUTEX_INITIALIZER(3),
26973  SQLITE3_MUTEX_INITIALIZER(4),
26974  SQLITE3_MUTEX_INITIALIZER(5),
26975  SQLITE3_MUTEX_INITIALIZER(6),
26976  SQLITE3_MUTEX_INITIALIZER(7),
26977  SQLITE3_MUTEX_INITIALIZER(8),
26978  SQLITE3_MUTEX_INITIALIZER(9),
26979  SQLITE3_MUTEX_INITIALIZER(10),
26980  SQLITE3_MUTEX_INITIALIZER(11),
26981  SQLITE3_MUTEX_INITIALIZER(12),
26982  SQLITE3_MUTEX_INITIALIZER(13)
26983};
26984
26985static int winMutex_isInit = 0;
26986static int winMutex_isNt = -1; /* <0 means "need to query" */
26987
26988/* As the winMutexInit() and winMutexEnd() functions are called as part
26989** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
26990** "interlocked" magic used here is probably not strictly necessary.
26991*/
26992static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0;
26993
26994SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
26995SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
26996
26997static int winMutexInit(void){
26998  /* The first to increment to 1 does actual initialization */
26999  if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
27000    int i;
27001    for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
27002#if SQLITE_OS_WINRT
27003      InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
27004#else
27005      InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
27006#endif
27007    }
27008    winMutex_isInit = 1;
27009  }else{
27010    /* Another thread is (in the process of) initializing the static
27011    ** mutexes */
27012    while( !winMutex_isInit ){
27013      sqlite3_win32_sleep(1);
27014    }
27015  }
27016  return SQLITE_OK;
27017}
27018
27019static int winMutexEnd(void){
27020  /* The first to decrement to 0 does actual shutdown
27021  ** (which should be the last to shutdown.) */
27022  if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
27023    if( winMutex_isInit==1 ){
27024      int i;
27025      for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
27026        DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
27027      }
27028      winMutex_isInit = 0;
27029    }
27030  }
27031  return SQLITE_OK;
27032}
27033
27034/*
27035** The sqlite3_mutex_alloc() routine allocates a new
27036** mutex and returns a pointer to it.  If it returns NULL
27037** that means that a mutex could not be allocated.  SQLite
27038** will unwind its stack and return an error.  The argument
27039** to sqlite3_mutex_alloc() is one of these integer constants:
27040**
27041** <ul>
27042** <li>  SQLITE_MUTEX_FAST
27043** <li>  SQLITE_MUTEX_RECURSIVE
27044** <li>  SQLITE_MUTEX_STATIC_MASTER
27045** <li>  SQLITE_MUTEX_STATIC_MEM
27046** <li>  SQLITE_MUTEX_STATIC_OPEN
27047** <li>  SQLITE_MUTEX_STATIC_PRNG
27048** <li>  SQLITE_MUTEX_STATIC_LRU
27049** <li>  SQLITE_MUTEX_STATIC_PMEM
27050** <li>  SQLITE_MUTEX_STATIC_APP1
27051** <li>  SQLITE_MUTEX_STATIC_APP2
27052** <li>  SQLITE_MUTEX_STATIC_APP3
27053** <li>  SQLITE_MUTEX_STATIC_VFS1
27054** <li>  SQLITE_MUTEX_STATIC_VFS2
27055** <li>  SQLITE_MUTEX_STATIC_VFS3
27056** </ul>
27057**
27058** The first two constants cause sqlite3_mutex_alloc() to create
27059** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
27060** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
27061** The mutex implementation does not need to make a distinction
27062** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
27063** not want to.  But SQLite will only request a recursive mutex in
27064** cases where it really needs one.  If a faster non-recursive mutex
27065** implementation is available on the host platform, the mutex subsystem
27066** might return such a mutex in response to SQLITE_MUTEX_FAST.
27067**
27068** The other allowed parameters to sqlite3_mutex_alloc() each return
27069** a pointer to a static preexisting mutex.  Six static mutexes are
27070** used by the current version of SQLite.  Future versions of SQLite
27071** may add additional static mutexes.  Static mutexes are for internal
27072** use by SQLite only.  Applications that use SQLite mutexes should
27073** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
27074** SQLITE_MUTEX_RECURSIVE.
27075**
27076** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
27077** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
27078** returns a different mutex on every call.  But for the static
27079** mutex types, the same mutex is returned on every call that has
27080** the same type number.
27081*/
27082static sqlite3_mutex *winMutexAlloc(int iType){
27083  sqlite3_mutex *p;
27084
27085  switch( iType ){
27086    case SQLITE_MUTEX_FAST:
27087    case SQLITE_MUTEX_RECURSIVE: {
27088      p = sqlite3MallocZero( sizeof(*p) );
27089      if( p ){
27090        p->id = iType;
27091#ifdef SQLITE_DEBUG
27092#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
27093        p->trace = 1;
27094#endif
27095#endif
27096#if SQLITE_OS_WINRT
27097        InitializeCriticalSectionEx(&p->mutex, 0, 0);
27098#else
27099        InitializeCriticalSection(&p->mutex);
27100#endif
27101      }
27102      break;
27103    }
27104    default: {
27105#ifdef SQLITE_ENABLE_API_ARMOR
27106      if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
27107        (void)SQLITE_MISUSE_BKPT;
27108        return 0;
27109      }
27110#endif
27111      p = &winMutex_staticMutexes[iType-2];
27112#ifdef SQLITE_DEBUG
27113#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
27114      InterlockedCompareExchange(&p->trace, 1, 0);
27115#endif
27116#endif
27117      break;
27118    }
27119  }
27120  assert( p==0 || p->id==iType );
27121  return p;
27122}
27123
27124
27125/*
27126** This routine deallocates a previously
27127** allocated mutex.  SQLite is careful to deallocate every
27128** mutex that it allocates.
27129*/
27130static void winMutexFree(sqlite3_mutex *p){
27131  assert( p );
27132  assert( p->nRef==0 && p->owner==0 );
27133  if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){
27134    DeleteCriticalSection(&p->mutex);
27135    sqlite3_free(p);
27136  }else{
27137#ifdef SQLITE_ENABLE_API_ARMOR
27138    (void)SQLITE_MISUSE_BKPT;
27139#endif
27140  }
27141}
27142
27143/*
27144** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
27145** to enter a mutex.  If another thread is already within the mutex,
27146** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
27147** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
27148** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
27149** be entered multiple times by the same thread.  In such cases the,
27150** mutex must be exited an equal number of times before another thread
27151** can enter.  If the same thread tries to enter any other kind of mutex
27152** more than once, the behavior is undefined.
27153*/
27154static void winMutexEnter(sqlite3_mutex *p){
27155#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
27156  DWORD tid = GetCurrentThreadId();
27157#endif
27158#ifdef SQLITE_DEBUG
27159  assert( p );
27160  assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
27161#else
27162  assert( p );
27163#endif
27164  assert( winMutex_isInit==1 );
27165  EnterCriticalSection(&p->mutex);
27166#ifdef SQLITE_DEBUG
27167  assert( p->nRef>0 || p->owner==0 );
27168  p->owner = tid;
27169  p->nRef++;
27170  if( p->trace ){
27171    OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
27172             tid, p->id, p, p->trace, p->nRef));
27173  }
27174#endif
27175}
27176
27177static int winMutexTry(sqlite3_mutex *p){
27178#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
27179  DWORD tid = GetCurrentThreadId();
27180#endif
27181  int rc = SQLITE_BUSY;
27182  assert( p );
27183  assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
27184  /*
27185  ** The sqlite3_mutex_try() routine is very rarely used, and when it
27186  ** is used it is merely an optimization.  So it is OK for it to always
27187  ** fail.
27188  **
27189  ** The TryEnterCriticalSection() interface is only available on WinNT.
27190  ** And some windows compilers complain if you try to use it without
27191  ** first doing some #defines that prevent SQLite from building on Win98.
27192  ** For that reason, we will omit this optimization for now.  See
27193  ** ticket #2685.
27194  */
27195#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
27196  assert( winMutex_isInit==1 );
27197  assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
27198  if( winMutex_isNt<0 ){
27199    winMutex_isNt = sqlite3_win32_is_nt();
27200  }
27201  assert( winMutex_isNt==0 || winMutex_isNt==1 );
27202  if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
27203#ifdef SQLITE_DEBUG
27204    p->owner = tid;
27205    p->nRef++;
27206#endif
27207    rc = SQLITE_OK;
27208  }
27209#else
27210  UNUSED_PARAMETER(p);
27211#endif
27212#ifdef SQLITE_DEBUG
27213  if( p->trace ){
27214    OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
27215             tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
27216  }
27217#endif
27218  return rc;
27219}
27220
27221/*
27222** The sqlite3_mutex_leave() routine exits a mutex that was
27223** previously entered by the same thread.  The behavior
27224** is undefined if the mutex is not currently entered or
27225** is not currently allocated.  SQLite will never do either.
27226*/
27227static void winMutexLeave(sqlite3_mutex *p){
27228#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
27229  DWORD tid = GetCurrentThreadId();
27230#endif
27231  assert( p );
27232#ifdef SQLITE_DEBUG
27233  assert( p->nRef>0 );
27234  assert( p->owner==tid );
27235  p->nRef--;
27236  if( p->nRef==0 ) p->owner = 0;
27237  assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
27238#endif
27239  assert( winMutex_isInit==1 );
27240  LeaveCriticalSection(&p->mutex);
27241#ifdef SQLITE_DEBUG
27242  if( p->trace ){
27243    OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
27244             tid, p->id, p, p->trace, p->nRef));
27245  }
27246#endif
27247}
27248
27249SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
27250  static const sqlite3_mutex_methods sMutex = {
27251    winMutexInit,
27252    winMutexEnd,
27253    winMutexAlloc,
27254    winMutexFree,
27255    winMutexEnter,
27256    winMutexTry,
27257    winMutexLeave,
27258#ifdef SQLITE_DEBUG
27259    winMutexHeld,
27260    winMutexNotheld
27261#else
27262    0,
27263    0
27264#endif
27265  };
27266  return &sMutex;
27267}
27268
27269#endif /* SQLITE_MUTEX_W32 */
27270
27271/************** End of mutex_w32.c *******************************************/
27272/************** Begin file malloc.c ******************************************/
27273/*
27274** 2001 September 15
27275**
27276** The author disclaims copyright to this source code.  In place of
27277** a legal notice, here is a blessing:
27278**
27279**    May you do good and not evil.
27280**    May you find forgiveness for yourself and forgive others.
27281**    May you share freely, never taking more than you give.
27282**
27283*************************************************************************
27284**
27285** Memory allocation functions used throughout sqlite.
27286*/
27287/* #include "sqliteInt.h" */
27288/* #include <stdarg.h> */
27289
27290/*
27291** Attempt to release up to n bytes of non-essential memory currently
27292** held by SQLite. An example of non-essential memory is memory used to
27293** cache database pages that are not currently in use.
27294*/
27295SQLITE_API int sqlite3_release_memory(int n){
27296#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
27297  return sqlite3PcacheReleaseMemory(n);
27298#else
27299  /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
27300  ** is a no-op returning zero if SQLite is not compiled with
27301  ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
27302  UNUSED_PARAMETER(n);
27303  return 0;
27304#endif
27305}
27306
27307/*
27308** Default value of the hard heap limit.  0 means "no limit".
27309*/
27310#ifndef SQLITE_MAX_MEMORY
27311# define SQLITE_MAX_MEMORY 0
27312#endif
27313
27314/*
27315** State information local to the memory allocation subsystem.
27316*/
27317static SQLITE_WSD struct Mem0Global {
27318  sqlite3_mutex *mutex;         /* Mutex to serialize access */
27319  sqlite3_int64 alarmThreshold; /* The soft heap limit */
27320  sqlite3_int64 hardLimit;      /* The hard upper bound on memory */
27321
27322  /*
27323  ** True if heap is nearly "full" where "full" is defined by the
27324  ** sqlite3_soft_heap_limit() setting.
27325  */
27326  int nearlyFull;
27327} mem0 = { 0, SQLITE_MAX_MEMORY, SQLITE_MAX_MEMORY, 0 };
27328
27329#define mem0 GLOBAL(struct Mem0Global, mem0)
27330
27331/*
27332** Return the memory allocator mutex. sqlite3_status() needs it.
27333*/
27334SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void){
27335  return mem0.mutex;
27336}
27337
27338#ifndef SQLITE_OMIT_DEPRECATED
27339/*
27340** Deprecated external interface.  It used to set an alarm callback
27341** that was invoked when memory usage grew too large.  Now it is a
27342** no-op.
27343*/
27344SQLITE_API int sqlite3_memory_alarm(
27345  void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
27346  void *pArg,
27347  sqlite3_int64 iThreshold
27348){
27349  (void)xCallback;
27350  (void)pArg;
27351  (void)iThreshold;
27352  return SQLITE_OK;
27353}
27354#endif
27355
27356/*
27357** Set the soft heap-size limit for the library.  An argument of
27358** zero disables the limit.  A negative argument is a no-op used to
27359** obtain the return value.
27360**
27361** The return value is the value of the heap limit just before this
27362** interface was called.
27363**
27364** If the hard heap limit is enabled, then the soft heap limit cannot
27365** be disabled nor raised above the hard heap limit.
27366*/
27367SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
27368  sqlite3_int64 priorLimit;
27369  sqlite3_int64 excess;
27370  sqlite3_int64 nUsed;
27371#ifndef SQLITE_OMIT_AUTOINIT
27372  int rc = sqlite3_initialize();
27373  if( rc ) return -1;
27374#endif
27375  sqlite3_mutex_enter(mem0.mutex);
27376  priorLimit = mem0.alarmThreshold;
27377  if( n<0 ){
27378    sqlite3_mutex_leave(mem0.mutex);
27379    return priorLimit;
27380  }
27381  if( mem0.hardLimit>0 && (n>mem0.hardLimit || n==0) ){
27382    n = mem0.hardLimit;
27383  }
27384  mem0.alarmThreshold = n;
27385  nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
27386  AtomicStore(&mem0.nearlyFull, n>0 && n<=nUsed);
27387  sqlite3_mutex_leave(mem0.mutex);
27388  excess = sqlite3_memory_used() - n;
27389  if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
27390  return priorLimit;
27391}
27392SQLITE_API void sqlite3_soft_heap_limit(int n){
27393  if( n<0 ) n = 0;
27394  sqlite3_soft_heap_limit64(n);
27395}
27396
27397/*
27398** Set the hard heap-size limit for the library. An argument of zero
27399** disables the hard heap limit.  A negative argument is a no-op used
27400** to obtain the return value without affecting the hard heap limit.
27401**
27402** The return value is the value of the hard heap limit just prior to
27403** calling this interface.
27404**
27405** Setting the hard heap limit will also activate the soft heap limit
27406** and constrain the soft heap limit to be no more than the hard heap
27407** limit.
27408*/
27409SQLITE_API sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 n){
27410  sqlite3_int64 priorLimit;
27411#ifndef SQLITE_OMIT_AUTOINIT
27412  int rc = sqlite3_initialize();
27413  if( rc ) return -1;
27414#endif
27415  sqlite3_mutex_enter(mem0.mutex);
27416  priorLimit = mem0.hardLimit;
27417  if( n>=0 ){
27418    mem0.hardLimit = n;
27419    if( n<mem0.alarmThreshold || mem0.alarmThreshold==0 ){
27420      mem0.alarmThreshold = n;
27421    }
27422  }
27423  sqlite3_mutex_leave(mem0.mutex);
27424  return priorLimit;
27425}
27426
27427
27428/*
27429** Initialize the memory allocation subsystem.
27430*/
27431SQLITE_PRIVATE int sqlite3MallocInit(void){
27432  int rc;
27433  if( sqlite3GlobalConfig.m.xMalloc==0 ){
27434    sqlite3MemSetDefault();
27435  }
27436  memset(&mem0, 0, sizeof(mem0));
27437  mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
27438  if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
27439      || sqlite3GlobalConfig.nPage<=0 ){
27440    sqlite3GlobalConfig.pPage = 0;
27441    sqlite3GlobalConfig.szPage = 0;
27442  }
27443  rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
27444  if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
27445  return rc;
27446}
27447
27448/*
27449** Return true if the heap is currently under memory pressure - in other
27450** words if the amount of heap used is close to the limit set by
27451** sqlite3_soft_heap_limit().
27452*/
27453SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
27454  return AtomicLoad(&mem0.nearlyFull);
27455}
27456
27457/*
27458** Deinitialize the memory allocation subsystem.
27459*/
27460SQLITE_PRIVATE void sqlite3MallocEnd(void){
27461  if( sqlite3GlobalConfig.m.xShutdown ){
27462    sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
27463  }
27464  memset(&mem0, 0, sizeof(mem0));
27465}
27466
27467/*
27468** Return the amount of memory currently checked out.
27469*/
27470SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
27471  sqlite3_int64 res, mx;
27472  sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
27473  return res;
27474}
27475
27476/*
27477** Return the maximum amount of memory that has ever been
27478** checked out since either the beginning of this process
27479** or since the most recent reset.
27480*/
27481SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
27482  sqlite3_int64 res, mx;
27483  sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
27484  return mx;
27485}
27486
27487/*
27488** Trigger the alarm
27489*/
27490static void sqlite3MallocAlarm(int nByte){
27491  if( mem0.alarmThreshold<=0 ) return;
27492  sqlite3_mutex_leave(mem0.mutex);
27493  sqlite3_release_memory(nByte);
27494  sqlite3_mutex_enter(mem0.mutex);
27495}
27496
27497/*
27498** Do a memory allocation with statistics and alarms.  Assume the
27499** lock is already held.
27500*/
27501static void mallocWithAlarm(int n, void **pp){
27502  void *p;
27503  int nFull;
27504  assert( sqlite3_mutex_held(mem0.mutex) );
27505  assert( n>0 );
27506
27507  /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
27508  ** implementation of malloc_good_size(), which must be called in debug
27509  ** mode and specifically when the DMD "Dark Matter Detector" is enabled
27510  ** or else a crash results.  Hence, do not attempt to optimize out the
27511  ** following xRoundup() call. */
27512  nFull = sqlite3GlobalConfig.m.xRoundup(n);
27513
27514  sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
27515  if( mem0.alarmThreshold>0 ){
27516    sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
27517    if( nUsed >= mem0.alarmThreshold - nFull ){
27518      AtomicStore(&mem0.nearlyFull, 1);
27519      sqlite3MallocAlarm(nFull);
27520      if( mem0.hardLimit ){
27521        nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
27522        if( nUsed >= mem0.hardLimit - nFull ){
27523          *pp = 0;
27524          return;
27525        }
27526      }
27527    }else{
27528      AtomicStore(&mem0.nearlyFull, 0);
27529    }
27530  }
27531  p = sqlite3GlobalConfig.m.xMalloc(nFull);
27532#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
27533  if( p==0 && mem0.alarmThreshold>0 ){
27534    sqlite3MallocAlarm(nFull);
27535    p = sqlite3GlobalConfig.m.xMalloc(nFull);
27536  }
27537#endif
27538  if( p ){
27539    nFull = sqlite3MallocSize(p);
27540    sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
27541    sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
27542  }
27543  *pp = p;
27544}
27545
27546/*
27547** Allocate memory.  This routine is like sqlite3_malloc() except that it
27548** assumes the memory subsystem has already been initialized.
27549*/
27550SQLITE_PRIVATE void *sqlite3Malloc(u64 n){
27551  void *p;
27552  if( n==0 || n>=0x7fffff00 ){
27553    /* A memory allocation of a number of bytes which is near the maximum
27554    ** signed integer value might cause an integer overflow inside of the
27555    ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
27556    ** 255 bytes of overhead.  SQLite itself will never use anything near
27557    ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
27558    p = 0;
27559  }else if( sqlite3GlobalConfig.bMemstat ){
27560    sqlite3_mutex_enter(mem0.mutex);
27561    mallocWithAlarm((int)n, &p);
27562    sqlite3_mutex_leave(mem0.mutex);
27563  }else{
27564    p = sqlite3GlobalConfig.m.xMalloc((int)n);
27565  }
27566  assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-11148-40995 */
27567  return p;
27568}
27569
27570/*
27571** This version of the memory allocation is for use by the application.
27572** First make sure the memory subsystem is initialized, then do the
27573** allocation.
27574*/
27575SQLITE_API void *sqlite3_malloc(int n){
27576#ifndef SQLITE_OMIT_AUTOINIT
27577  if( sqlite3_initialize() ) return 0;
27578#endif
27579  return n<=0 ? 0 : sqlite3Malloc(n);
27580}
27581SQLITE_API void *sqlite3_malloc64(sqlite3_uint64 n){
27582#ifndef SQLITE_OMIT_AUTOINIT
27583  if( sqlite3_initialize() ) return 0;
27584#endif
27585  return sqlite3Malloc(n);
27586}
27587
27588/*
27589** TRUE if p is a lookaside memory allocation from db
27590*/
27591#ifndef SQLITE_OMIT_LOOKASIDE
27592static int isLookaside(sqlite3 *db, void *p){
27593  return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
27594}
27595#else
27596#define isLookaside(A,B) 0
27597#endif
27598
27599/*
27600** Return the size of a memory allocation previously obtained from
27601** sqlite3Malloc() or sqlite3_malloc().
27602*/
27603SQLITE_PRIVATE int sqlite3MallocSize(void *p){
27604  assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
27605  return sqlite3GlobalConfig.m.xSize(p);
27606}
27607static int lookasideMallocSize(sqlite3 *db, void *p){
27608#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
27609  return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL;
27610#else
27611  return db->lookaside.szTrue;
27612#endif
27613}
27614SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
27615  assert( p!=0 );
27616#ifdef SQLITE_DEBUG
27617  if( db==0 || !isLookaside(db,p) ){
27618    if( db==0 ){
27619      assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
27620      assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
27621    }else{
27622      assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
27623      assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
27624    }
27625  }
27626#endif
27627  if( db ){
27628    if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
27629#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
27630      if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
27631        assert( sqlite3_mutex_held(db->mutex) );
27632        return LOOKASIDE_SMALL;
27633      }
27634#endif
27635      if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
27636        assert( sqlite3_mutex_held(db->mutex) );
27637        return db->lookaside.szTrue;
27638      }
27639    }
27640  }
27641  return sqlite3GlobalConfig.m.xSize(p);
27642}
27643SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){
27644  assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
27645  assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
27646  return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
27647}
27648
27649/*
27650** Free memory previously obtained from sqlite3Malloc().
27651*/
27652SQLITE_API void sqlite3_free(void *p){
27653  if( p==0 ) return;  /* IMP: R-49053-54554 */
27654  assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
27655  assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
27656  if( sqlite3GlobalConfig.bMemstat ){
27657    sqlite3_mutex_enter(mem0.mutex);
27658    sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
27659    sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
27660    sqlite3GlobalConfig.m.xFree(p);
27661    sqlite3_mutex_leave(mem0.mutex);
27662  }else{
27663    sqlite3GlobalConfig.m.xFree(p);
27664  }
27665}
27666
27667/*
27668** Add the size of memory allocation "p" to the count in
27669** *db->pnBytesFreed.
27670*/
27671static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
27672  *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
27673}
27674
27675/*
27676** Free memory that might be associated with a particular database
27677** connection.  Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
27678** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
27679*/
27680SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3 *db, void *p){
27681  assert( db==0 || sqlite3_mutex_held(db->mutex) );
27682  assert( p!=0 );
27683  if( db ){
27684    if( db->pnBytesFreed ){
27685      measureAllocationSize(db, p);
27686      return;
27687    }
27688    if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
27689#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
27690      if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
27691        LookasideSlot *pBuf = (LookasideSlot*)p;
27692#ifdef SQLITE_DEBUG
27693        memset(p, 0xaa, LOOKASIDE_SMALL);  /* Trash freed content */
27694#endif
27695        pBuf->pNext = db->lookaside.pSmallFree;
27696        db->lookaside.pSmallFree = pBuf;
27697        return;
27698      }
27699#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
27700      if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
27701        LookasideSlot *pBuf = (LookasideSlot*)p;
27702#ifdef SQLITE_DEBUG
27703        memset(p, 0xaa, db->lookaside.szTrue);  /* Trash freed content */
27704#endif
27705        pBuf->pNext = db->lookaside.pFree;
27706        db->lookaside.pFree = pBuf;
27707        return;
27708      }
27709    }
27710  }
27711  assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
27712  assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
27713  assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
27714  sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
27715  sqlite3_free(p);
27716}
27717SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
27718  assert( db==0 || sqlite3_mutex_held(db->mutex) );
27719  if( p ) sqlite3DbFreeNN(db, p);
27720}
27721
27722/*
27723** Change the size of an existing memory allocation
27724*/
27725SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
27726  int nOld, nNew, nDiff;
27727  void *pNew;
27728  assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
27729  assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
27730  if( pOld==0 ){
27731    return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
27732  }
27733  if( nBytes==0 ){
27734    sqlite3_free(pOld); /* IMP: R-26507-47431 */
27735    return 0;
27736  }
27737  if( nBytes>=0x7fffff00 ){
27738    /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
27739    return 0;
27740  }
27741  nOld = sqlite3MallocSize(pOld);
27742  /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
27743  ** argument to xRealloc is always a value returned by a prior call to
27744  ** xRoundup. */
27745  nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
27746  if( nOld==nNew ){
27747    pNew = pOld;
27748  }else if( sqlite3GlobalConfig.bMemstat ){
27749    sqlite3_mutex_enter(mem0.mutex);
27750    sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
27751    nDiff = nNew - nOld;
27752    if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
27753          mem0.alarmThreshold-nDiff ){
27754      sqlite3MallocAlarm(nDiff);
27755    }
27756    pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
27757#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
27758    if( pNew==0 && mem0.alarmThreshold>0 ){
27759      sqlite3MallocAlarm((int)nBytes);
27760      pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
27761    }
27762#endif
27763    if( pNew ){
27764      nNew = sqlite3MallocSize(pNew);
27765      sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
27766    }
27767    sqlite3_mutex_leave(mem0.mutex);
27768  }else{
27769    pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
27770  }
27771  assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
27772  return pNew;
27773}
27774
27775/*
27776** The public interface to sqlite3Realloc.  Make sure that the memory
27777** subsystem is initialized prior to invoking sqliteRealloc.
27778*/
27779SQLITE_API void *sqlite3_realloc(void *pOld, int n){
27780#ifndef SQLITE_OMIT_AUTOINIT
27781  if( sqlite3_initialize() ) return 0;
27782#endif
27783  if( n<0 ) n = 0;  /* IMP: R-26507-47431 */
27784  return sqlite3Realloc(pOld, n);
27785}
27786SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
27787#ifndef SQLITE_OMIT_AUTOINIT
27788  if( sqlite3_initialize() ) return 0;
27789#endif
27790  return sqlite3Realloc(pOld, n);
27791}
27792
27793
27794/*
27795** Allocate and zero memory.
27796*/
27797SQLITE_PRIVATE void *sqlite3MallocZero(u64 n){
27798  void *p = sqlite3Malloc(n);
27799  if( p ){
27800    memset(p, 0, (size_t)n);
27801  }
27802  return p;
27803}
27804
27805/*
27806** Allocate and zero memory.  If the allocation fails, make
27807** the mallocFailed flag in the connection pointer.
27808*/
27809SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
27810  void *p;
27811  testcase( db==0 );
27812  p = sqlite3DbMallocRaw(db, n);
27813  if( p ) memset(p, 0, (size_t)n);
27814  return p;
27815}
27816
27817
27818/* Finish the work of sqlite3DbMallocRawNN for the unusual and
27819** slower case when the allocation cannot be fulfilled using lookaside.
27820*/
27821static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
27822  void *p;
27823  assert( db!=0 );
27824  p = sqlite3Malloc(n);
27825  if( !p ) sqlite3OomFault(db);
27826  sqlite3MemdebugSetType(p,
27827         (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
27828  return p;
27829}
27830
27831/*
27832** Allocate memory, either lookaside (if possible) or heap.
27833** If the allocation fails, set the mallocFailed flag in
27834** the connection pointer.
27835**
27836** If db!=0 and db->mallocFailed is true (indicating a prior malloc
27837** failure on the same database connection) then always return 0.
27838** Hence for a particular database connection, once malloc starts
27839** failing, it fails consistently until mallocFailed is reset.
27840** This is an important assumption.  There are many places in the
27841** code that do things like this:
27842**
27843**         int *a = (int*)sqlite3DbMallocRaw(db, 100);
27844**         int *b = (int*)sqlite3DbMallocRaw(db, 200);
27845**         if( b ) a[10] = 9;
27846**
27847** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
27848** that all prior mallocs (ex: "a") worked too.
27849**
27850** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
27851** not a NULL pointer.
27852*/
27853SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
27854  void *p;
27855  if( db ) return sqlite3DbMallocRawNN(db, n);
27856  p = sqlite3Malloc(n);
27857  sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
27858  return p;
27859}
27860SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
27861#ifndef SQLITE_OMIT_LOOKASIDE
27862  LookasideSlot *pBuf;
27863  assert( db!=0 );
27864  assert( sqlite3_mutex_held(db->mutex) );
27865  assert( db->pnBytesFreed==0 );
27866  if( n>db->lookaside.sz ){
27867    if( !db->lookaside.bDisable ){
27868      db->lookaside.anStat[1]++;
27869    }else if( db->mallocFailed ){
27870      return 0;
27871    }
27872    return dbMallocRawFinish(db, n);
27873  }
27874#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
27875  if( n<=LOOKASIDE_SMALL ){
27876    if( (pBuf = db->lookaside.pSmallFree)!=0 ){
27877      db->lookaside.pSmallFree = pBuf->pNext;
27878      db->lookaside.anStat[0]++;
27879      return (void*)pBuf;
27880    }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
27881      db->lookaside.pSmallInit = pBuf->pNext;
27882      db->lookaside.anStat[0]++;
27883      return (void*)pBuf;
27884    }
27885  }
27886#endif
27887  if( (pBuf = db->lookaside.pFree)!=0 ){
27888    db->lookaside.pFree = pBuf->pNext;
27889    db->lookaside.anStat[0]++;
27890    return (void*)pBuf;
27891  }else if( (pBuf = db->lookaside.pInit)!=0 ){
27892    db->lookaside.pInit = pBuf->pNext;
27893    db->lookaside.anStat[0]++;
27894    return (void*)pBuf;
27895  }else{
27896    db->lookaside.anStat[2]++;
27897  }
27898#else
27899  assert( db!=0 );
27900  assert( sqlite3_mutex_held(db->mutex) );
27901  assert( db->pnBytesFreed==0 );
27902  if( db->mallocFailed ){
27903    return 0;
27904  }
27905#endif
27906  return dbMallocRawFinish(db, n);
27907}
27908
27909/* Forward declaration */
27910static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
27911
27912/*
27913** Resize the block of memory pointed to by p to n bytes. If the
27914** resize fails, set the mallocFailed flag in the connection object.
27915*/
27916SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
27917  assert( db!=0 );
27918  if( p==0 ) return sqlite3DbMallocRawNN(db, n);
27919  assert( sqlite3_mutex_held(db->mutex) );
27920  if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
27921#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
27922    if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
27923      if( n<=LOOKASIDE_SMALL ) return p;
27924    }else
27925#endif
27926    if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
27927      if( n<=db->lookaside.szTrue ) return p;
27928    }
27929  }
27930  return dbReallocFinish(db, p, n);
27931}
27932static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
27933  void *pNew = 0;
27934  assert( db!=0 );
27935  assert( p!=0 );
27936  if( db->mallocFailed==0 ){
27937    if( isLookaside(db, p) ){
27938      pNew = sqlite3DbMallocRawNN(db, n);
27939      if( pNew ){
27940        memcpy(pNew, p, lookasideMallocSize(db, p));
27941        sqlite3DbFree(db, p);
27942      }
27943    }else{
27944      assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
27945      assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
27946      sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
27947      pNew = sqlite3Realloc(p, n);
27948      if( !pNew ){
27949        sqlite3OomFault(db);
27950      }
27951      sqlite3MemdebugSetType(pNew,
27952            (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
27953    }
27954  }
27955  return pNew;
27956}
27957
27958/*
27959** Attempt to reallocate p.  If the reallocation fails, then free p
27960** and set the mallocFailed flag in the database connection.
27961*/
27962SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
27963  void *pNew;
27964  pNew = sqlite3DbRealloc(db, p, n);
27965  if( !pNew ){
27966    sqlite3DbFree(db, p);
27967  }
27968  return pNew;
27969}
27970
27971/*
27972** Make a copy of a string in memory obtained from sqliteMalloc(). These
27973** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
27974** is because when memory debugging is turned on, these two functions are
27975** called via macros that record the current file and line number in the
27976** ThreadData structure.
27977*/
27978SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
27979  char *zNew;
27980  size_t n;
27981  if( z==0 ){
27982    return 0;
27983  }
27984  n = strlen(z) + 1;
27985  zNew = sqlite3DbMallocRaw(db, n);
27986  if( zNew ){
27987    memcpy(zNew, z, n);
27988  }
27989  return zNew;
27990}
27991SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
27992  char *zNew;
27993  assert( db!=0 );
27994  if( z==0 ){
27995    return 0;
27996  }
27997  assert( (n&0x7fffffff)==n );
27998  zNew = sqlite3DbMallocRawNN(db, n+1);
27999  if( zNew ){
28000    memcpy(zNew, z, (size_t)n);
28001    zNew[n] = 0;
28002  }
28003  return zNew;
28004}
28005
28006/*
28007** The text between zStart and zEnd represents a phrase within a larger
28008** SQL statement.  Make a copy of this phrase in space obtained form
28009** sqlite3DbMalloc().  Omit leading and trailing whitespace.
28010*/
28011SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
28012  int n;
28013  while( sqlite3Isspace(zStart[0]) ) zStart++;
28014  n = (int)(zEnd - zStart);
28015  while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
28016  return sqlite3DbStrNDup(db, zStart, n);
28017}
28018
28019/*
28020** Free any prior content in *pz and replace it with a copy of zNew.
28021*/
28022SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
28023  sqlite3DbFree(db, *pz);
28024  *pz = sqlite3DbStrDup(db, zNew);
28025}
28026
28027/*
28028** Call this routine to record the fact that an OOM (out-of-memory) error
28029** has happened.  This routine will set db->mallocFailed, and also
28030** temporarily disable the lookaside memory allocator and interrupt
28031** any running VDBEs.
28032*/
28033SQLITE_PRIVATE void sqlite3OomFault(sqlite3 *db){
28034  if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
28035    db->mallocFailed = 1;
28036    if( db->nVdbeExec>0 ){
28037      AtomicStore(&db->u1.isInterrupted, 1);
28038    }
28039    DisableLookaside;
28040    if( db->pParse ){
28041      db->pParse->rc = SQLITE_NOMEM_BKPT;
28042    }
28043  }
28044}
28045
28046/*
28047** This routine reactivates the memory allocator and clears the
28048** db->mallocFailed flag as necessary.
28049**
28050** The memory allocator is not restarted if there are running
28051** VDBEs.
28052*/
28053SQLITE_PRIVATE void sqlite3OomClear(sqlite3 *db){
28054  if( db->mallocFailed && db->nVdbeExec==0 ){
28055    db->mallocFailed = 0;
28056    AtomicStore(&db->u1.isInterrupted, 0);
28057    assert( db->lookaside.bDisable>0 );
28058    EnableLookaside;
28059  }
28060}
28061
28062/*
28063** Take actions at the end of an API call to indicate an OOM error
28064*/
28065static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
28066  sqlite3OomClear(db);
28067  sqlite3Error(db, SQLITE_NOMEM);
28068  return SQLITE_NOMEM_BKPT;
28069}
28070
28071/*
28072** This function must be called before exiting any API function (i.e.
28073** returning control to the user) that has called sqlite3_malloc or
28074** sqlite3_realloc.
28075**
28076** The returned value is normally a copy of the second argument to this
28077** function. However, if a malloc() failure has occurred since the previous
28078** invocation SQLITE_NOMEM is returned instead.
28079**
28080** If an OOM as occurred, then the connection error-code (the value
28081** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
28082*/
28083SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
28084  /* If the db handle must hold the connection handle mutex here.
28085  ** Otherwise the read (and possible write) of db->mallocFailed
28086  ** is unsafe, as is the call to sqlite3Error().
28087  */
28088  assert( db!=0 );
28089  assert( sqlite3_mutex_held(db->mutex) );
28090  if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
28091    return apiOomError(db);
28092  }
28093  return rc & db->errMask;
28094}
28095
28096/************** End of malloc.c **********************************************/
28097/************** Begin file printf.c ******************************************/
28098/*
28099** The "printf" code that follows dates from the 1980's.  It is in
28100** the public domain.
28101**
28102**************************************************************************
28103**
28104** This file contains code for a set of "printf"-like routines.  These
28105** routines format strings much like the printf() from the standard C
28106** library, though the implementation here has enhancements to support
28107** SQLite.
28108*/
28109/* #include "sqliteInt.h" */
28110
28111/*
28112** Conversion types fall into various categories as defined by the
28113** following enumeration.
28114*/
28115#define etRADIX       0 /* non-decimal integer types.  %x %o */
28116#define etFLOAT       1 /* Floating point.  %f */
28117#define etEXP         2 /* Exponentional notation. %e and %E */
28118#define etGENERIC     3 /* Floating or exponential, depending on exponent. %g */
28119#define etSIZE        4 /* Return number of characters processed so far. %n */
28120#define etSTRING      5 /* Strings. %s */
28121#define etDYNSTRING   6 /* Dynamically allocated strings. %z */
28122#define etPERCENT     7 /* Percent symbol. %% */
28123#define etCHARX       8 /* Characters. %c */
28124/* The rest are extensions, not normally found in printf() */
28125#define etSQLESCAPE   9 /* Strings with '\'' doubled.  %q */
28126#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
28127                          NULL pointers replaced by SQL NULL.  %Q */
28128#define etTOKEN      11 /* a pointer to a Token structure */
28129#define etSRCLIST    12 /* a pointer to a SrcList */
28130#define etPOINTER    13 /* The %p conversion */
28131#define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
28132#define etORDINAL    15 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
28133#define etDECIMAL    16 /* %d or %u, but not %x, %o */
28134
28135#define etINVALID    17 /* Any unrecognized conversion type */
28136
28137
28138/*
28139** An "etByte" is an 8-bit unsigned value.
28140*/
28141typedef unsigned char etByte;
28142
28143/*
28144** Each builtin conversion character (ex: the 'd' in "%d") is described
28145** by an instance of the following structure
28146*/
28147typedef struct et_info {   /* Information about each format field */
28148  char fmttype;            /* The format field code letter */
28149  etByte base;             /* The base for radix conversion */
28150  etByte flags;            /* One or more of FLAG_ constants below */
28151  etByte type;             /* Conversion paradigm */
28152  etByte charset;          /* Offset into aDigits[] of the digits string */
28153  etByte prefix;           /* Offset into aPrefix[] of the prefix string */
28154} et_info;
28155
28156/*
28157** Allowed values for et_info.flags
28158*/
28159#define FLAG_SIGNED    1     /* True if the value to convert is signed */
28160#define FLAG_STRING    4     /* Allow infinite precision */
28161
28162
28163/*
28164** The following table is searched linearly, so it is good to put the
28165** most frequently used conversion types first.
28166*/
28167static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
28168static const char aPrefix[] = "-x0\000X0";
28169static const et_info fmtinfo[] = {
28170  {  'd', 10, 1, etDECIMAL,    0,  0 },
28171  {  's',  0, 4, etSTRING,     0,  0 },
28172  {  'g',  0, 1, etGENERIC,    30, 0 },
28173  {  'z',  0, 4, etDYNSTRING,  0,  0 },
28174  {  'q',  0, 4, etSQLESCAPE,  0,  0 },
28175  {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
28176  {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
28177  {  'c',  0, 0, etCHARX,      0,  0 },
28178  {  'o',  8, 0, etRADIX,      0,  2 },
28179  {  'u', 10, 0, etDECIMAL,    0,  0 },
28180  {  'x', 16, 0, etRADIX,      16, 1 },
28181  {  'X', 16, 0, etRADIX,      0,  4 },
28182#ifndef SQLITE_OMIT_FLOATING_POINT
28183  {  'f',  0, 1, etFLOAT,      0,  0 },
28184  {  'e',  0, 1, etEXP,        30, 0 },
28185  {  'E',  0, 1, etEXP,        14, 0 },
28186  {  'G',  0, 1, etGENERIC,    14, 0 },
28187#endif
28188  {  'i', 10, 1, etDECIMAL,    0,  0 },
28189  {  'n',  0, 0, etSIZE,       0,  0 },
28190  {  '%',  0, 0, etPERCENT,    0,  0 },
28191  {  'p', 16, 0, etPOINTER,    0,  1 },
28192
28193  /* All the rest are undocumented and are for internal use only */
28194  {  'T',  0, 0, etTOKEN,      0,  0 },
28195  {  'S',  0, 0, etSRCLIST,    0,  0 },
28196  {  'r', 10, 1, etORDINAL,    0,  0 },
28197};
28198
28199/* Floating point constants used for rounding */
28200static const double arRound[] = {
28201  5.0e-01, 5.0e-02, 5.0e-03, 5.0e-04, 5.0e-05,
28202  5.0e-06, 5.0e-07, 5.0e-08, 5.0e-09, 5.0e-10,
28203};
28204
28205/*
28206** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
28207** conversions will work.
28208*/
28209#ifndef SQLITE_OMIT_FLOATING_POINT
28210/*
28211** "*val" is a double such that 0.1 <= *val < 10.0
28212** Return the ascii code for the leading digit of *val, then
28213** multiply "*val" by 10.0 to renormalize.
28214**
28215** Example:
28216**     input:     *val = 3.14159
28217**     output:    *val = 1.4159    function return = '3'
28218**
28219** The counter *cnt is incremented each time.  After counter exceeds
28220** 16 (the number of significant digits in a 64-bit float) '0' is
28221** always returned.
28222*/
28223static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
28224  int digit;
28225  LONGDOUBLE_TYPE d;
28226  if( (*cnt)<=0 ) return '0';
28227  (*cnt)--;
28228  digit = (int)*val;
28229  d = digit;
28230  digit += '0';
28231  *val = (*val - d)*10.0;
28232  return (char)digit;
28233}
28234#endif /* SQLITE_OMIT_FLOATING_POINT */
28235
28236/*
28237** Set the StrAccum object to an error mode.
28238*/
28239static void setStrAccumError(StrAccum *p, u8 eError){
28240  assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
28241  p->accError = eError;
28242  if( p->mxAlloc ) sqlite3_str_reset(p);
28243  if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError);
28244}
28245
28246/*
28247** Extra argument values from a PrintfArguments object
28248*/
28249static sqlite3_int64 getIntArg(PrintfArguments *p){
28250  if( p->nArg<=p->nUsed ) return 0;
28251  return sqlite3_value_int64(p->apArg[p->nUsed++]);
28252}
28253static double getDoubleArg(PrintfArguments *p){
28254  if( p->nArg<=p->nUsed ) return 0.0;
28255  return sqlite3_value_double(p->apArg[p->nUsed++]);
28256}
28257static char *getTextArg(PrintfArguments *p){
28258  if( p->nArg<=p->nUsed ) return 0;
28259  return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
28260}
28261
28262/*
28263** Allocate memory for a temporary buffer needed for printf rendering.
28264**
28265** If the requested size of the temp buffer is larger than the size
28266** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error.
28267** Do the size check before the memory allocation to prevent rogue
28268** SQL from requesting large allocations using the precision or width
28269** field of the printf() function.
28270*/
28271static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
28272  char *z;
28273  if( pAccum->accError ) return 0;
28274  if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
28275    setStrAccumError(pAccum, SQLITE_TOOBIG);
28276    return 0;
28277  }
28278  z = sqlite3DbMallocRaw(pAccum->db, n);
28279  if( z==0 ){
28280    setStrAccumError(pAccum, SQLITE_NOMEM);
28281  }
28282  return z;
28283}
28284
28285/*
28286** On machines with a small stack size, you can redefine the
28287** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
28288*/
28289#ifndef SQLITE_PRINT_BUF_SIZE
28290# define SQLITE_PRINT_BUF_SIZE 70
28291#endif
28292#define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
28293
28294/*
28295** Hard limit on the precision of floating-point conversions.
28296*/
28297#ifndef SQLITE_PRINTF_PRECISION_LIMIT
28298# define SQLITE_FP_PRECISION_LIMIT 100000000
28299#endif
28300
28301/*
28302** Render a string given by "fmt" into the StrAccum object.
28303*/
28304SQLITE_API void sqlite3_str_vappendf(
28305  sqlite3_str *pAccum,       /* Accumulate results here */
28306  const char *fmt,           /* Format string */
28307  va_list ap                 /* arguments */
28308){
28309  int c;                     /* Next character in the format string */
28310  char *bufpt;               /* Pointer to the conversion buffer */
28311  int precision;             /* Precision of the current field */
28312  int length;                /* Length of the field */
28313  int idx;                   /* A general purpose loop counter */
28314  int width;                 /* Width of the current field */
28315  etByte flag_leftjustify;   /* True if "-" flag is present */
28316  etByte flag_prefix;        /* '+' or ' ' or 0 for prefix */
28317  etByte flag_alternateform; /* True if "#" flag is present */
28318  etByte flag_altform2;      /* True if "!" flag is present */
28319  etByte flag_zeropad;       /* True if field width constant starts with zero */
28320  etByte flag_long;          /* 1 for the "l" flag, 2 for "ll", 0 by default */
28321  etByte done;               /* Loop termination flag */
28322  etByte cThousand;          /* Thousands separator for %d and %u */
28323  etByte xtype = etINVALID;  /* Conversion paradigm */
28324  u8 bArgList;               /* True for SQLITE_PRINTF_SQLFUNC */
28325  char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
28326  sqlite_uint64 longvalue;   /* Value for integer types */
28327  LONGDOUBLE_TYPE realvalue; /* Value for real types */
28328  const et_info *infop;      /* Pointer to the appropriate info structure */
28329  char *zOut;                /* Rendering buffer */
28330  int nOut;                  /* Size of the rendering buffer */
28331  char *zExtra = 0;          /* Malloced memory used by some conversion */
28332#ifndef SQLITE_OMIT_FLOATING_POINT
28333  int  exp, e2;              /* exponent of real numbers */
28334  int nsd;                   /* Number of significant digits returned */
28335  double rounder;            /* Used for rounding floating point values */
28336  etByte flag_dp;            /* True if decimal point should be shown */
28337  etByte flag_rtz;           /* True if trailing zeros should be removed */
28338#endif
28339  PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
28340  char buf[etBUFSIZE];       /* Conversion buffer */
28341
28342  /* pAccum never starts out with an empty buffer that was obtained from
28343  ** malloc().  This precondition is required by the mprintf("%z...")
28344  ** optimization. */
28345  assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
28346
28347  bufpt = 0;
28348  if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
28349    pArgList = va_arg(ap, PrintfArguments*);
28350    bArgList = 1;
28351  }else{
28352    bArgList = 0;
28353  }
28354  for(; (c=(*fmt))!=0; ++fmt){
28355    if( c!='%' ){
28356      bufpt = (char *)fmt;
28357#if HAVE_STRCHRNUL
28358      fmt = strchrnul(fmt, '%');
28359#else
28360      do{ fmt++; }while( *fmt && *fmt != '%' );
28361#endif
28362      sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
28363      if( *fmt==0 ) break;
28364    }
28365    if( (c=(*++fmt))==0 ){
28366      sqlite3_str_append(pAccum, "%", 1);
28367      break;
28368    }
28369    /* Find out what flags are present */
28370    flag_leftjustify = flag_prefix = cThousand =
28371     flag_alternateform = flag_altform2 = flag_zeropad = 0;
28372    done = 0;
28373    width = 0;
28374    flag_long = 0;
28375    precision = -1;
28376    do{
28377      switch( c ){
28378        case '-':   flag_leftjustify = 1;     break;
28379        case '+':   flag_prefix = '+';        break;
28380        case ' ':   flag_prefix = ' ';        break;
28381        case '#':   flag_alternateform = 1;   break;
28382        case '!':   flag_altform2 = 1;        break;
28383        case '0':   flag_zeropad = 1;         break;
28384        case ',':   cThousand = ',';          break;
28385        default:    done = 1;                 break;
28386        case 'l': {
28387          flag_long = 1;
28388          c = *++fmt;
28389          if( c=='l' ){
28390            c = *++fmt;
28391            flag_long = 2;
28392          }
28393          done = 1;
28394          break;
28395        }
28396        case '1': case '2': case '3': case '4': case '5':
28397        case '6': case '7': case '8': case '9': {
28398          unsigned wx = c - '0';
28399          while( (c = *++fmt)>='0' && c<='9' ){
28400            wx = wx*10 + c - '0';
28401          }
28402          testcase( wx>0x7fffffff );
28403          width = wx & 0x7fffffff;
28404#ifdef SQLITE_PRINTF_PRECISION_LIMIT
28405          if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
28406            width = SQLITE_PRINTF_PRECISION_LIMIT;
28407          }
28408#endif
28409          if( c!='.' && c!='l' ){
28410            done = 1;
28411          }else{
28412            fmt--;
28413          }
28414          break;
28415        }
28416        case '*': {
28417          if( bArgList ){
28418            width = (int)getIntArg(pArgList);
28419          }else{
28420            width = va_arg(ap,int);
28421          }
28422          if( width<0 ){
28423            flag_leftjustify = 1;
28424            width = width >= -2147483647 ? -width : 0;
28425          }
28426#ifdef SQLITE_PRINTF_PRECISION_LIMIT
28427          if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
28428            width = SQLITE_PRINTF_PRECISION_LIMIT;
28429          }
28430#endif
28431          if( (c = fmt[1])!='.' && c!='l' ){
28432            c = *++fmt;
28433            done = 1;
28434          }
28435          break;
28436        }
28437        case '.': {
28438          c = *++fmt;
28439          if( c=='*' ){
28440            if( bArgList ){
28441              precision = (int)getIntArg(pArgList);
28442            }else{
28443              precision = va_arg(ap,int);
28444            }
28445            if( precision<0 ){
28446              precision = precision >= -2147483647 ? -precision : -1;
28447            }
28448            c = *++fmt;
28449          }else{
28450            unsigned px = 0;
28451            while( c>='0' && c<='9' ){
28452              px = px*10 + c - '0';
28453              c = *++fmt;
28454            }
28455            testcase( px>0x7fffffff );
28456            precision = px & 0x7fffffff;
28457          }
28458#ifdef SQLITE_PRINTF_PRECISION_LIMIT
28459          if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){
28460            precision = SQLITE_PRINTF_PRECISION_LIMIT;
28461          }
28462#endif
28463          if( c=='l' ){
28464            --fmt;
28465          }else{
28466            done = 1;
28467          }
28468          break;
28469        }
28470      }
28471    }while( !done && (c=(*++fmt))!=0 );
28472
28473    /* Fetch the info entry for the field */
28474    infop = &fmtinfo[0];
28475    xtype = etINVALID;
28476    for(idx=0; idx<ArraySize(fmtinfo); idx++){
28477      if( c==fmtinfo[idx].fmttype ){
28478        infop = &fmtinfo[idx];
28479        xtype = infop->type;
28480        break;
28481      }
28482    }
28483
28484    /*
28485    ** At this point, variables are initialized as follows:
28486    **
28487    **   flag_alternateform          TRUE if a '#' is present.
28488    **   flag_altform2               TRUE if a '!' is present.
28489    **   flag_prefix                 '+' or ' ' or zero
28490    **   flag_leftjustify            TRUE if a '-' is present or if the
28491    **                               field width was negative.
28492    **   flag_zeropad                TRUE if the width began with 0.
28493    **   flag_long                   1 for "l", 2 for "ll"
28494    **   width                       The specified field width.  This is
28495    **                               always non-negative.  Zero is the default.
28496    **   precision                   The specified precision.  The default
28497    **                               is -1.
28498    **   xtype                       The class of the conversion.
28499    **   infop                       Pointer to the appropriate info struct.
28500    */
28501    assert( width>=0 );
28502    assert( precision>=(-1) );
28503    switch( xtype ){
28504      case etPOINTER:
28505        flag_long = sizeof(char*)==sizeof(i64) ? 2 :
28506                     sizeof(char*)==sizeof(long int) ? 1 : 0;
28507        /* Fall through into the next case */
28508      case etORDINAL:
28509      case etRADIX:
28510        cThousand = 0;
28511        /* Fall through into the next case */
28512      case etDECIMAL:
28513        if( infop->flags & FLAG_SIGNED ){
28514          i64 v;
28515          if( bArgList ){
28516            v = getIntArg(pArgList);
28517          }else if( flag_long ){
28518            if( flag_long==2 ){
28519              v = va_arg(ap,i64) ;
28520            }else{
28521              v = va_arg(ap,long int);
28522            }
28523          }else{
28524            v = va_arg(ap,int);
28525          }
28526          if( v<0 ){
28527            if( v==SMALLEST_INT64 ){
28528              longvalue = ((u64)1)<<63;
28529            }else{
28530              longvalue = -v;
28531            }
28532            prefix = '-';
28533          }else{
28534            longvalue = v;
28535            prefix = flag_prefix;
28536          }
28537        }else{
28538          if( bArgList ){
28539            longvalue = (u64)getIntArg(pArgList);
28540          }else if( flag_long ){
28541            if( flag_long==2 ){
28542              longvalue = va_arg(ap,u64);
28543            }else{
28544              longvalue = va_arg(ap,unsigned long int);
28545            }
28546          }else{
28547            longvalue = va_arg(ap,unsigned int);
28548          }
28549          prefix = 0;
28550        }
28551        if( longvalue==0 ) flag_alternateform = 0;
28552        if( flag_zeropad && precision<width-(prefix!=0) ){
28553          precision = width-(prefix!=0);
28554        }
28555        if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
28556          nOut = etBUFSIZE;
28557          zOut = buf;
28558        }else{
28559          u64 n;
28560          n = (u64)precision + 10;
28561          if( cThousand ) n += precision/3;
28562          zOut = zExtra = printfTempBuf(pAccum, n);
28563          if( zOut==0 ) return;
28564          nOut = (int)n;
28565        }
28566        bufpt = &zOut[nOut-1];
28567        if( xtype==etORDINAL ){
28568          static const char zOrd[] = "thstndrd";
28569          int x = (int)(longvalue % 10);
28570          if( x>=4 || (longvalue/10)%10==1 ){
28571            x = 0;
28572          }
28573          *(--bufpt) = zOrd[x*2+1];
28574          *(--bufpt) = zOrd[x*2];
28575        }
28576        {
28577          const char *cset = &aDigits[infop->charset];
28578          u8 base = infop->base;
28579          do{                                           /* Convert to ascii */
28580            *(--bufpt) = cset[longvalue%base];
28581            longvalue = longvalue/base;
28582          }while( longvalue>0 );
28583        }
28584        length = (int)(&zOut[nOut-1]-bufpt);
28585        while( precision>length ){
28586          *(--bufpt) = '0';                             /* Zero pad */
28587          length++;
28588        }
28589        if( cThousand ){
28590          int nn = (length - 1)/3;  /* Number of "," to insert */
28591          int ix = (length - 1)%3 + 1;
28592          bufpt -= nn;
28593          for(idx=0; nn>0; idx++){
28594            bufpt[idx] = bufpt[idx+nn];
28595            ix--;
28596            if( ix==0 ){
28597              bufpt[++idx] = cThousand;
28598              nn--;
28599              ix = 3;
28600            }
28601          }
28602        }
28603        if( prefix ) *(--bufpt) = prefix;               /* Add sign */
28604        if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
28605          const char *pre;
28606          char x;
28607          pre = &aPrefix[infop->prefix];
28608          for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
28609        }
28610        length = (int)(&zOut[nOut-1]-bufpt);
28611        break;
28612      case etFLOAT:
28613      case etEXP:
28614      case etGENERIC:
28615        if( bArgList ){
28616          realvalue = getDoubleArg(pArgList);
28617        }else{
28618          realvalue = va_arg(ap,double);
28619        }
28620#ifdef SQLITE_OMIT_FLOATING_POINT
28621        length = 0;
28622#else
28623        if( precision<0 ) precision = 6;         /* Set default precision */
28624#ifdef SQLITE_FP_PRECISION_LIMIT
28625        if( precision>SQLITE_FP_PRECISION_LIMIT ){
28626          precision = SQLITE_FP_PRECISION_LIMIT;
28627        }
28628#endif
28629        if( realvalue<0.0 ){
28630          realvalue = -realvalue;
28631          prefix = '-';
28632        }else{
28633          prefix = flag_prefix;
28634        }
28635        if( xtype==etGENERIC && precision>0 ) precision--;
28636        testcase( precision>0xfff );
28637        idx = precision & 0xfff;
28638        rounder = arRound[idx%10];
28639        while( idx>=10 ){ rounder *= 1.0e-10; idx -= 10; }
28640        if( xtype==etFLOAT ){
28641          double rx = (double)realvalue;
28642          sqlite3_uint64 u;
28643          int ex;
28644          memcpy(&u, &rx, sizeof(u));
28645          ex = -1023 + (int)((u>>52)&0x7ff);
28646          if( precision+(ex/3) < 15 ) rounder += realvalue*3e-16;
28647          realvalue += rounder;
28648        }
28649        /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
28650        exp = 0;
28651        if( sqlite3IsNaN((double)realvalue) ){
28652          bufpt = "NaN";
28653          length = 3;
28654          break;
28655        }
28656        if( realvalue>0.0 ){
28657          LONGDOUBLE_TYPE scale = 1.0;
28658          while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
28659          while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; }
28660          while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
28661          realvalue /= scale;
28662          while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
28663          while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
28664          if( exp>350 ){
28665            bufpt = buf;
28666            buf[0] = prefix;
28667            memcpy(buf+(prefix!=0),"Inf",4);
28668            length = 3+(prefix!=0);
28669            break;
28670          }
28671        }
28672        bufpt = buf;
28673        /*
28674        ** If the field type is etGENERIC, then convert to either etEXP
28675        ** or etFLOAT, as appropriate.
28676        */
28677        if( xtype!=etFLOAT ){
28678          realvalue += rounder;
28679          if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
28680        }
28681        if( xtype==etGENERIC ){
28682          flag_rtz = !flag_alternateform;
28683          if( exp<-4 || exp>precision ){
28684            xtype = etEXP;
28685          }else{
28686            precision = precision - exp;
28687            xtype = etFLOAT;
28688          }
28689        }else{
28690          flag_rtz = flag_altform2;
28691        }
28692        if( xtype==etEXP ){
28693          e2 = 0;
28694        }else{
28695          e2 = exp;
28696        }
28697        {
28698          i64 szBufNeeded;           /* Size of a temporary buffer needed */
28699          szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15;
28700          if( szBufNeeded > etBUFSIZE ){
28701            bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded);
28702            if( bufpt==0 ) return;
28703          }
28704        }
28705        zOut = bufpt;
28706        nsd = 16 + flag_altform2*10;
28707        flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
28708        /* The sign in front of the number */
28709        if( prefix ){
28710          *(bufpt++) = prefix;
28711        }
28712        /* Digits prior to the decimal point */
28713        if( e2<0 ){
28714          *(bufpt++) = '0';
28715        }else{
28716          for(; e2>=0; e2--){
28717            *(bufpt++) = et_getdigit(&realvalue,&nsd);
28718          }
28719        }
28720        /* The decimal point */
28721        if( flag_dp ){
28722          *(bufpt++) = '.';
28723        }
28724        /* "0" digits after the decimal point but before the first
28725        ** significant digit of the number */
28726        for(e2++; e2<0; precision--, e2++){
28727          assert( precision>0 );
28728          *(bufpt++) = '0';
28729        }
28730        /* Significant digits after the decimal point */
28731        while( (precision--)>0 ){
28732          *(bufpt++) = et_getdigit(&realvalue,&nsd);
28733        }
28734        /* Remove trailing zeros and the "." if no digits follow the "." */
28735        if( flag_rtz && flag_dp ){
28736          while( bufpt[-1]=='0' ) *(--bufpt) = 0;
28737          assert( bufpt>zOut );
28738          if( bufpt[-1]=='.' ){
28739            if( flag_altform2 ){
28740              *(bufpt++) = '0';
28741            }else{
28742              *(--bufpt) = 0;
28743            }
28744          }
28745        }
28746        /* Add the "eNNN" suffix */
28747        if( xtype==etEXP ){
28748          *(bufpt++) = aDigits[infop->charset];
28749          if( exp<0 ){
28750            *(bufpt++) = '-'; exp = -exp;
28751          }else{
28752            *(bufpt++) = '+';
28753          }
28754          if( exp>=100 ){
28755            *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
28756            exp %= 100;
28757          }
28758          *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
28759          *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
28760        }
28761        *bufpt = 0;
28762
28763        /* The converted number is in buf[] and zero terminated. Output it.
28764        ** Note that the number is in the usual order, not reversed as with
28765        ** integer conversions. */
28766        length = (int)(bufpt-zOut);
28767        bufpt = zOut;
28768
28769        /* Special case:  Add leading zeros if the flag_zeropad flag is
28770        ** set and we are not left justified */
28771        if( flag_zeropad && !flag_leftjustify && length < width){
28772          int i;
28773          int nPad = width - length;
28774          for(i=width; i>=nPad; i--){
28775            bufpt[i] = bufpt[i-nPad];
28776          }
28777          i = prefix!=0;
28778          while( nPad-- ) bufpt[i++] = '0';
28779          length = width;
28780        }
28781#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
28782        break;
28783      case etSIZE:
28784        if( !bArgList ){
28785          *(va_arg(ap,int*)) = pAccum->nChar;
28786        }
28787        length = width = 0;
28788        break;
28789      case etPERCENT:
28790        buf[0] = '%';
28791        bufpt = buf;
28792        length = 1;
28793        break;
28794      case etCHARX:
28795        if( bArgList ){
28796          bufpt = getTextArg(pArgList);
28797          length = 1;
28798          if( bufpt ){
28799            buf[0] = c = *(bufpt++);
28800            if( (c&0xc0)==0xc0 ){
28801              while( length<4 && (bufpt[0]&0xc0)==0x80 ){
28802                buf[length++] = *(bufpt++);
28803              }
28804            }
28805          }else{
28806            buf[0] = 0;
28807          }
28808        }else{
28809          unsigned int ch = va_arg(ap,unsigned int);
28810          if( ch<0x00080 ){
28811            buf[0] = ch & 0xff;
28812            length = 1;
28813          }else if( ch<0x00800 ){
28814            buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
28815            buf[1] = 0x80 + (u8)(ch & 0x3f);
28816            length = 2;
28817          }else if( ch<0x10000 ){
28818            buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
28819            buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
28820            buf[2] = 0x80 + (u8)(ch & 0x3f);
28821            length = 3;
28822          }else{
28823            buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
28824            buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
28825            buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
28826            buf[3] = 0x80 + (u8)(ch & 0x3f);
28827            length = 4;
28828          }
28829        }
28830        if( precision>1 ){
28831          width -= precision-1;
28832          if( width>1 && !flag_leftjustify ){
28833            sqlite3_str_appendchar(pAccum, width-1, ' ');
28834            width = 0;
28835          }
28836          while( precision-- > 1 ){
28837            sqlite3_str_append(pAccum, buf, length);
28838          }
28839        }
28840        bufpt = buf;
28841        flag_altform2 = 1;
28842        goto adjust_width_for_utf8;
28843      case etSTRING:
28844      case etDYNSTRING:
28845        if( bArgList ){
28846          bufpt = getTextArg(pArgList);
28847          xtype = etSTRING;
28848        }else{
28849          bufpt = va_arg(ap,char*);
28850        }
28851        if( bufpt==0 ){
28852          bufpt = "";
28853        }else if( xtype==etDYNSTRING ){
28854          if( pAccum->nChar==0
28855           && pAccum->mxAlloc
28856           && width==0
28857           && precision<0
28858           && pAccum->accError==0
28859          ){
28860            /* Special optimization for sqlite3_mprintf("%z..."):
28861            ** Extend an existing memory allocation rather than creating
28862            ** a new one. */
28863            assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
28864            pAccum->zText = bufpt;
28865            pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
28866            pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
28867            pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
28868            length = 0;
28869            break;
28870          }
28871          zExtra = bufpt;
28872        }
28873        if( precision>=0 ){
28874          if( flag_altform2 ){
28875            /* Set length to the number of bytes needed in order to display
28876            ** precision characters */
28877            unsigned char *z = (unsigned char*)bufpt;
28878            while( precision-- > 0 && z[0] ){
28879              SQLITE_SKIP_UTF8(z);
28880            }
28881            length = (int)(z - (unsigned char*)bufpt);
28882          }else{
28883            for(length=0; length<precision && bufpt[length]; length++){}
28884          }
28885        }else{
28886          length = 0x7fffffff & (int)strlen(bufpt);
28887        }
28888      adjust_width_for_utf8:
28889        if( flag_altform2 && width>0 ){
28890          /* Adjust width to account for extra bytes in UTF-8 characters */
28891          int ii = length - 1;
28892          while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
28893        }
28894        break;
28895      case etSQLESCAPE:           /* %q: Escape ' characters */
28896      case etSQLESCAPE2:          /* %Q: Escape ' and enclose in '...' */
28897      case etSQLESCAPE3: {        /* %w: Escape " characters */
28898        int i, j, k, n, isnull;
28899        int needQuote;
28900        char ch;
28901        char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
28902        char *escarg;
28903
28904        if( bArgList ){
28905          escarg = getTextArg(pArgList);
28906        }else{
28907          escarg = va_arg(ap,char*);
28908        }
28909        isnull = escarg==0;
28910        if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
28911        /* For %q, %Q, and %w, the precision is the number of bytes (or
28912        ** characters if the ! flags is present) to use from the input.
28913        ** Because of the extra quoting characters inserted, the number
28914        ** of output characters may be larger than the precision.
28915        */
28916        k = precision;
28917        for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
28918          if( ch==q )  n++;
28919          if( flag_altform2 && (ch&0xc0)==0xc0 ){
28920            while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
28921          }
28922        }
28923        needQuote = !isnull && xtype==etSQLESCAPE2;
28924        n += i + 3;
28925        if( n>etBUFSIZE ){
28926          bufpt = zExtra = printfTempBuf(pAccum, n);
28927          if( bufpt==0 ) return;
28928        }else{
28929          bufpt = buf;
28930        }
28931        j = 0;
28932        if( needQuote ) bufpt[j++] = q;
28933        k = i;
28934        for(i=0; i<k; i++){
28935          bufpt[j++] = ch = escarg[i];
28936          if( ch==q ) bufpt[j++] = ch;
28937        }
28938        if( needQuote ) bufpt[j++] = q;
28939        bufpt[j] = 0;
28940        length = j;
28941        goto adjust_width_for_utf8;
28942      }
28943      case etTOKEN: {
28944        Token *pToken;
28945        if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
28946        pToken = va_arg(ap, Token*);
28947        assert( bArgList==0 );
28948        if( pToken && pToken->n ){
28949          sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
28950        }
28951        length = width = 0;
28952        break;
28953      }
28954      case etSRCLIST: {
28955        SrcList *pSrc;
28956        int k;
28957        struct SrcList_item *pItem;
28958        if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
28959        pSrc = va_arg(ap, SrcList*);
28960        k = va_arg(ap, int);
28961        pItem = &pSrc->a[k];
28962        assert( bArgList==0 );
28963        assert( k>=0 && k<pSrc->nSrc );
28964        if( pItem->zDatabase ){
28965          sqlite3_str_appendall(pAccum, pItem->zDatabase);
28966          sqlite3_str_append(pAccum, ".", 1);
28967        }
28968        sqlite3_str_appendall(pAccum, pItem->zName);
28969        length = width = 0;
28970        break;
28971      }
28972      default: {
28973        assert( xtype==etINVALID );
28974        return;
28975      }
28976    }/* End switch over the format type */
28977    /*
28978    ** The text of the conversion is pointed to by "bufpt" and is
28979    ** "length" characters long.  The field width is "width".  Do
28980    ** the output.  Both length and width are in bytes, not characters,
28981    ** at this point.  If the "!" flag was present on string conversions
28982    ** indicating that width and precision should be expressed in characters,
28983    ** then the values have been translated prior to reaching this point.
28984    */
28985    width -= length;
28986    if( width>0 ){
28987      if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
28988      sqlite3_str_append(pAccum, bufpt, length);
28989      if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
28990    }else{
28991      sqlite3_str_append(pAccum, bufpt, length);
28992    }
28993
28994    if( zExtra ){
28995      sqlite3DbFree(pAccum->db, zExtra);
28996      zExtra = 0;
28997    }
28998  }/* End for loop over the format string */
28999} /* End of function */
29000
29001/*
29002** Enlarge the memory allocation on a StrAccum object so that it is
29003** able to accept at least N more bytes of text.
29004**
29005** Return the number of bytes of text that StrAccum is able to accept
29006** after the attempted enlargement.  The value returned might be zero.
29007*/
29008static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
29009  char *zNew;
29010  assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
29011  if( p->accError ){
29012    testcase(p->accError==SQLITE_TOOBIG);
29013    testcase(p->accError==SQLITE_NOMEM);
29014    return 0;
29015  }
29016  if( p->mxAlloc==0 ){
29017    setStrAccumError(p, SQLITE_TOOBIG);
29018    return p->nAlloc - p->nChar - 1;
29019  }else{
29020    char *zOld = isMalloced(p) ? p->zText : 0;
29021    i64 szNew = p->nChar;
29022    szNew += N + 1;
29023    if( szNew+p->nChar<=p->mxAlloc ){
29024      /* Force exponential buffer size growth as long as it does not overflow,
29025      ** to avoid having to call this routine too often */
29026      szNew += p->nChar;
29027    }
29028    if( szNew > p->mxAlloc ){
29029      sqlite3_str_reset(p);
29030      setStrAccumError(p, SQLITE_TOOBIG);
29031      return 0;
29032    }else{
29033      p->nAlloc = (int)szNew;
29034    }
29035    if( p->db ){
29036      zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
29037    }else{
29038      zNew = sqlite3Realloc(zOld, p->nAlloc);
29039    }
29040    if( zNew ){
29041      assert( p->zText!=0 || p->nChar==0 );
29042      if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
29043      p->zText = zNew;
29044      p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
29045      p->printfFlags |= SQLITE_PRINTF_MALLOCED;
29046    }else{
29047      sqlite3_str_reset(p);
29048      setStrAccumError(p, SQLITE_NOMEM);
29049      return 0;
29050    }
29051  }
29052  return N;
29053}
29054
29055/*
29056** Append N copies of character c to the given string buffer.
29057*/
29058SQLITE_API void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
29059  testcase( p->nChar + (i64)N > 0x7fffffff );
29060  if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
29061    return;
29062  }
29063  while( (N--)>0 ) p->zText[p->nChar++] = c;
29064}
29065
29066/*
29067** The StrAccum "p" is not large enough to accept N new bytes of z[].
29068** So enlarge if first, then do the append.
29069**
29070** This is a helper routine to sqlite3_str_append() that does special-case
29071** work (enlarging the buffer) using tail recursion, so that the
29072** sqlite3_str_append() routine can use fast calling semantics.
29073*/
29074static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
29075  N = sqlite3StrAccumEnlarge(p, N);
29076  if( N>0 ){
29077    memcpy(&p->zText[p->nChar], z, N);
29078    p->nChar += N;
29079  }
29080}
29081
29082/*
29083** Append N bytes of text from z to the StrAccum object.  Increase the
29084** size of the memory allocation for StrAccum if necessary.
29085*/
29086SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
29087  assert( z!=0 || N==0 );
29088  assert( p->zText!=0 || p->nChar==0 || p->accError );
29089  assert( N>=0 );
29090  assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
29091  if( p->nChar+N >= p->nAlloc ){
29092    enlargeAndAppend(p,z,N);
29093  }else if( N ){
29094    assert( p->zText );
29095    p->nChar += N;
29096    memcpy(&p->zText[p->nChar-N], z, N);
29097  }
29098}
29099
29100/*
29101** Append the complete text of zero-terminated string z[] to the p string.
29102*/
29103SQLITE_API void sqlite3_str_appendall(sqlite3_str *p, const char *z){
29104  sqlite3_str_append(p, z, sqlite3Strlen30(z));
29105}
29106
29107
29108/*
29109** Finish off a string by making sure it is zero-terminated.
29110** Return a pointer to the resulting string.  Return a NULL
29111** pointer if any kind of error was encountered.
29112*/
29113static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){
29114  char *zText;
29115  assert( p->mxAlloc>0 && !isMalloced(p) );
29116  zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
29117  if( zText ){
29118    memcpy(zText, p->zText, p->nChar+1);
29119    p->printfFlags |= SQLITE_PRINTF_MALLOCED;
29120  }else{
29121    setStrAccumError(p, SQLITE_NOMEM);
29122  }
29123  p->zText = zText;
29124  return zText;
29125}
29126SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
29127  if( p->zText ){
29128    p->zText[p->nChar] = 0;
29129    if( p->mxAlloc>0 && !isMalloced(p) ){
29130      return strAccumFinishRealloc(p);
29131    }
29132  }
29133  return p->zText;
29134}
29135
29136/*
29137** This singleton is an sqlite3_str object that is returned if
29138** sqlite3_malloc() fails to provide space for a real one.  This
29139** sqlite3_str object accepts no new text and always returns
29140** an SQLITE_NOMEM error.
29141*/
29142static sqlite3_str sqlite3OomStr = {
29143   0, 0, 0, 0, 0, SQLITE_NOMEM, 0
29144};
29145
29146/* Finalize a string created using sqlite3_str_new().
29147*/
29148SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){
29149  char *z;
29150  if( p!=0 && p!=&sqlite3OomStr ){
29151    z = sqlite3StrAccumFinish(p);
29152    sqlite3_free(p);
29153  }else{
29154    z = 0;
29155  }
29156  return z;
29157}
29158
29159/* Return any error code associated with p */
29160SQLITE_API int sqlite3_str_errcode(sqlite3_str *p){
29161  return p ? p->accError : SQLITE_NOMEM;
29162}
29163
29164/* Return the current length of p in bytes */
29165SQLITE_API int sqlite3_str_length(sqlite3_str *p){
29166  return p ? p->nChar : 0;
29167}
29168
29169/* Return the current value for p */
29170SQLITE_API char *sqlite3_str_value(sqlite3_str *p){
29171  if( p==0 || p->nChar==0 ) return 0;
29172  p->zText[p->nChar] = 0;
29173  return p->zText;
29174}
29175
29176/*
29177** Reset an StrAccum string.  Reclaim all malloced memory.
29178*/
29179SQLITE_API void sqlite3_str_reset(StrAccum *p){
29180  if( isMalloced(p) ){
29181    sqlite3DbFree(p->db, p->zText);
29182    p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
29183  }
29184  p->nAlloc = 0;
29185  p->nChar = 0;
29186  p->zText = 0;
29187}
29188
29189/*
29190** Initialize a string accumulator.
29191**
29192** p:     The accumulator to be initialized.
29193** db:    Pointer to a database connection.  May be NULL.  Lookaside
29194**        memory is used if not NULL. db->mallocFailed is set appropriately
29195**        when not NULL.
29196** zBase: An initial buffer.  May be NULL in which case the initial buffer
29197**        is malloced.
29198** n:     Size of zBase in bytes.  If total space requirements never exceed
29199**        n then no memory allocations ever occur.
29200** mx:    Maximum number of bytes to accumulate.  If mx==0 then no memory
29201**        allocations will ever occur.
29202*/
29203SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
29204  p->zText = zBase;
29205  p->db = db;
29206  p->nAlloc = n;
29207  p->mxAlloc = mx;
29208  p->nChar = 0;
29209  p->accError = 0;
29210  p->printfFlags = 0;
29211}
29212
29213/* Allocate and initialize a new dynamic string object */
29214SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){
29215  sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
29216  if( p ){
29217    sqlite3StrAccumInit(p, 0, 0, 0,
29218            db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
29219  }else{
29220    p = &sqlite3OomStr;
29221  }
29222  return p;
29223}
29224
29225/*
29226** Print into memory obtained from sqliteMalloc().  Use the internal
29227** %-conversion extensions.
29228*/
29229SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
29230  char *z;
29231  char zBase[SQLITE_PRINT_BUF_SIZE];
29232  StrAccum acc;
29233  assert( db!=0 );
29234  sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
29235                      db->aLimit[SQLITE_LIMIT_LENGTH]);
29236  acc.printfFlags = SQLITE_PRINTF_INTERNAL;
29237  sqlite3_str_vappendf(&acc, zFormat, ap);
29238  z = sqlite3StrAccumFinish(&acc);
29239  if( acc.accError==SQLITE_NOMEM ){
29240    sqlite3OomFault(db);
29241  }
29242  return z;
29243}
29244
29245/*
29246** Print into memory obtained from sqliteMalloc().  Use the internal
29247** %-conversion extensions.
29248*/
29249SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
29250  va_list ap;
29251  char *z;
29252  va_start(ap, zFormat);
29253  z = sqlite3VMPrintf(db, zFormat, ap);
29254  va_end(ap);
29255  return z;
29256}
29257
29258/*
29259** Print into memory obtained from sqlite3_malloc().  Omit the internal
29260** %-conversion extensions.
29261*/
29262SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
29263  char *z;
29264  char zBase[SQLITE_PRINT_BUF_SIZE];
29265  StrAccum acc;
29266
29267#ifdef SQLITE_ENABLE_API_ARMOR
29268  if( zFormat==0 ){
29269    (void)SQLITE_MISUSE_BKPT;
29270    return 0;
29271  }
29272#endif
29273#ifndef SQLITE_OMIT_AUTOINIT
29274  if( sqlite3_initialize() ) return 0;
29275#endif
29276  sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
29277  sqlite3_str_vappendf(&acc, zFormat, ap);
29278  z = sqlite3StrAccumFinish(&acc);
29279  return z;
29280}
29281
29282/*
29283** Print into memory obtained from sqlite3_malloc()().  Omit the internal
29284** %-conversion extensions.
29285*/
29286SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
29287  va_list ap;
29288  char *z;
29289#ifndef SQLITE_OMIT_AUTOINIT
29290  if( sqlite3_initialize() ) return 0;
29291#endif
29292  va_start(ap, zFormat);
29293  z = sqlite3_vmprintf(zFormat, ap);
29294  va_end(ap);
29295  return z;
29296}
29297
29298/*
29299** sqlite3_snprintf() works like snprintf() except that it ignores the
29300** current locale settings.  This is important for SQLite because we
29301** are not able to use a "," as the decimal point in place of "." as
29302** specified by some locales.
29303**
29304** Oops:  The first two arguments of sqlite3_snprintf() are backwards
29305** from the snprintf() standard.  Unfortunately, it is too late to change
29306** this without breaking compatibility, so we just have to live with the
29307** mistake.
29308**
29309** sqlite3_vsnprintf() is the varargs version.
29310*/
29311SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
29312  StrAccum acc;
29313  if( n<=0 ) return zBuf;
29314#ifdef SQLITE_ENABLE_API_ARMOR
29315  if( zBuf==0 || zFormat==0 ) {
29316    (void)SQLITE_MISUSE_BKPT;
29317    if( zBuf ) zBuf[0] = 0;
29318    return zBuf;
29319  }
29320#endif
29321  sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
29322  sqlite3_str_vappendf(&acc, zFormat, ap);
29323  zBuf[acc.nChar] = 0;
29324  return zBuf;
29325}
29326SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
29327  char *z;
29328  va_list ap;
29329  va_start(ap,zFormat);
29330  z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
29331  va_end(ap);
29332  return z;
29333}
29334
29335/*
29336** This is the routine that actually formats the sqlite3_log() message.
29337** We house it in a separate routine from sqlite3_log() to avoid using
29338** stack space on small-stack systems when logging is disabled.
29339**
29340** sqlite3_log() must render into a static buffer.  It cannot dynamically
29341** allocate memory because it might be called while the memory allocator
29342** mutex is held.
29343**
29344** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
29345** certain format characters (%q) or for very large precisions or widths.
29346** Care must be taken that any sqlite3_log() calls that occur while the
29347** memory mutex is held do not use these mechanisms.
29348*/
29349static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
29350  StrAccum acc;                          /* String accumulator */
29351  char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
29352
29353  sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
29354  sqlite3_str_vappendf(&acc, zFormat, ap);
29355  sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
29356                           sqlite3StrAccumFinish(&acc));
29357}
29358
29359/*
29360** Format and write a message to the log if logging is enabled.
29361*/
29362SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
29363  va_list ap;                             /* Vararg list */
29364  if( sqlite3GlobalConfig.xLog ){
29365    va_start(ap, zFormat);
29366    renderLogMsg(iErrCode, zFormat, ap);
29367    va_end(ap);
29368  }
29369}
29370
29371#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
29372/*
29373** A version of printf() that understands %lld.  Used for debugging.
29374** The printf() built into some versions of windows does not understand %lld
29375** and segfaults if you give it a long long int.
29376*/
29377SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
29378  va_list ap;
29379  StrAccum acc;
29380  char zBuf[SQLITE_PRINT_BUF_SIZE*10];
29381  sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
29382  va_start(ap,zFormat);
29383  sqlite3_str_vappendf(&acc, zFormat, ap);
29384  va_end(ap);
29385  sqlite3StrAccumFinish(&acc);
29386#ifdef SQLITE_OS_TRACE_PROC
29387  {
29388    extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
29389    SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
29390  }
29391#else
29392  fprintf(stdout,"%s", zBuf);
29393  fflush(stdout);
29394#endif
29395}
29396#endif
29397
29398
29399/*
29400** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
29401** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
29402*/
29403SQLITE_API void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
29404  va_list ap;
29405  va_start(ap,zFormat);
29406  sqlite3_str_vappendf(p, zFormat, ap);
29407  va_end(ap);
29408}
29409
29410/************** End of printf.c **********************************************/
29411/************** Begin file treeview.c ****************************************/
29412/*
29413** 2015-06-08
29414**
29415** The author disclaims copyright to this source code.  In place of
29416** a legal notice, here is a blessing:
29417**
29418**    May you do good and not evil.
29419**    May you find forgiveness for yourself and forgive others.
29420**    May you share freely, never taking more than you give.
29421**
29422*************************************************************************
29423**
29424** This file contains C code to implement the TreeView debugging routines.
29425** These routines print a parse tree to standard output for debugging and
29426** analysis.
29427**
29428** The interfaces in this file is only available when compiling
29429** with SQLITE_DEBUG.
29430*/
29431/* #include "sqliteInt.h" */
29432#ifdef SQLITE_DEBUG
29433
29434/*
29435** Add a new subitem to the tree.  The moreToFollow flag indicates that this
29436** is not the last item in the tree.
29437*/
29438static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
29439  if( p==0 ){
29440    p = sqlite3_malloc64( sizeof(*p) );
29441    if( p==0 ) return 0;
29442    memset(p, 0, sizeof(*p));
29443  }else{
29444    p->iLevel++;
29445  }
29446  assert( moreToFollow==0 || moreToFollow==1 );
29447  if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
29448  return p;
29449}
29450
29451/*
29452** Finished with one layer of the tree
29453*/
29454static void sqlite3TreeViewPop(TreeView *p){
29455  if( p==0 ) return;
29456  p->iLevel--;
29457  if( p->iLevel<0 ) sqlite3_free(p);
29458}
29459
29460/*
29461** Generate a single line of output for the tree, with a prefix that contains
29462** all the appropriate tree lines
29463*/
29464static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
29465  va_list ap;
29466  int i;
29467  StrAccum acc;
29468  char zBuf[500];
29469  sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
29470  if( p ){
29471    for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
29472      sqlite3_str_append(&acc, p->bLine[i] ? "|   " : "    ", 4);
29473    }
29474    sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
29475  }
29476  if( zFormat!=0 ){
29477    va_start(ap, zFormat);
29478    sqlite3_str_vappendf(&acc, zFormat, ap);
29479    va_end(ap);
29480    assert( acc.nChar>0 || acc.accError );
29481    sqlite3_str_append(&acc, "\n", 1);
29482  }
29483  sqlite3StrAccumFinish(&acc);
29484  fprintf(stdout,"%s", zBuf);
29485  fflush(stdout);
29486}
29487
29488/*
29489** Shorthand for starting a new tree item that consists of a single label
29490*/
29491static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
29492  p = sqlite3TreeViewPush(p, moreFollows);
29493  sqlite3TreeViewLine(p, "%s", zLabel);
29494}
29495
29496/*
29497** Generate a human-readable description of a WITH clause.
29498*/
29499SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){
29500  int i;
29501  if( pWith==0 ) return;
29502  if( pWith->nCte==0 ) return;
29503  if( pWith->pOuter ){
29504    sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
29505  }else{
29506    sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
29507  }
29508  if( pWith->nCte>0 ){
29509    pView = sqlite3TreeViewPush(pView, 1);
29510    for(i=0; i<pWith->nCte; i++){
29511      StrAccum x;
29512      char zLine[1000];
29513      const struct Cte *pCte = &pWith->a[i];
29514      sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
29515      sqlite3_str_appendf(&x, "%s", pCte->zName);
29516      if( pCte->pCols && pCte->pCols->nExpr>0 ){
29517        char cSep = '(';
29518        int j;
29519        for(j=0; j<pCte->pCols->nExpr; j++){
29520          sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName);
29521          cSep = ',';
29522        }
29523        sqlite3_str_appendf(&x, ")");
29524      }
29525      sqlite3_str_appendf(&x, " AS");
29526      sqlite3StrAccumFinish(&x);
29527      sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
29528      sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
29529      sqlite3TreeViewPop(pView);
29530    }
29531    sqlite3TreeViewPop(pView);
29532  }
29533}
29534
29535/*
29536** Generate a human-readable description of a SrcList object.
29537*/
29538SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){
29539  int i;
29540  for(i=0; i<pSrc->nSrc; i++){
29541    const struct SrcList_item *pItem = &pSrc->a[i];
29542    StrAccum x;
29543    char zLine[100];
29544    sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
29545    sqlite3_str_appendf(&x, "{%d:*}", pItem->iCursor);
29546    if( pItem->zDatabase ){
29547      sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
29548    }else if( pItem->zName ){
29549      sqlite3_str_appendf(&x, " %s", pItem->zName);
29550    }
29551    if( pItem->pTab ){
29552      sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx",
29553           pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed);
29554    }
29555    if( pItem->zAlias ){
29556      sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
29557    }
29558    if( pItem->fg.jointype & JT_LEFT ){
29559      sqlite3_str_appendf(&x, " LEFT-JOIN");
29560    }
29561    if( pItem->fg.fromDDL ){
29562      sqlite3_str_appendf(&x, " DDL");
29563    }
29564    sqlite3StrAccumFinish(&x);
29565    sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
29566    if( pItem->pSelect ){
29567      sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
29568    }
29569    if( pItem->fg.isTabFunc ){
29570      sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
29571    }
29572    sqlite3TreeViewPop(pView);
29573  }
29574}
29575
29576/*
29577** Generate a human-readable description of a Select object.
29578*/
29579SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
29580  int n = 0;
29581  int cnt = 0;
29582  if( p==0 ){
29583    sqlite3TreeViewLine(pView, "nil-SELECT");
29584    return;
29585  }
29586  pView = sqlite3TreeViewPush(pView, moreToFollow);
29587  if( p->pWith ){
29588    sqlite3TreeViewWith(pView, p->pWith, 1);
29589    cnt = 1;
29590    sqlite3TreeViewPush(pView, 1);
29591  }
29592  do{
29593    if( p->selFlags & SF_WhereBegin ){
29594      sqlite3TreeViewLine(pView, "sqlite3WhereBegin()");
29595    }else{
29596      sqlite3TreeViewLine(pView,
29597        "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
29598        ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
29599        ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
29600        p->selId, p, p->selFlags,
29601        (int)p->nSelectRow
29602      );
29603    }
29604    if( cnt++ ) sqlite3TreeViewPop(pView);
29605    if( p->pPrior ){
29606      n = 1000;
29607    }else{
29608      n = 0;
29609      if( p->pSrc && p->pSrc->nSrc ) n++;
29610      if( p->pWhere ) n++;
29611      if( p->pGroupBy ) n++;
29612      if( p->pHaving ) n++;
29613      if( p->pOrderBy ) n++;
29614      if( p->pLimit ) n++;
29615#ifndef SQLITE_OMIT_WINDOWFUNC
29616      if( p->pWin ) n++;
29617      if( p->pWinDefn ) n++;
29618#endif
29619    }
29620    if( p->pEList ){
29621      sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
29622    }
29623    n--;
29624#ifndef SQLITE_OMIT_WINDOWFUNC
29625    if( p->pWin ){
29626      Window *pX;
29627      pView = sqlite3TreeViewPush(pView, (n--)>0);
29628      sqlite3TreeViewLine(pView, "window-functions");
29629      for(pX=p->pWin; pX; pX=pX->pNextWin){
29630        sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
29631      }
29632      sqlite3TreeViewPop(pView);
29633    }
29634#endif
29635    if( p->pSrc && p->pSrc->nSrc ){
29636      pView = sqlite3TreeViewPush(pView, (n--)>0);
29637      sqlite3TreeViewLine(pView, "FROM");
29638      sqlite3TreeViewSrcList(pView, p->pSrc);
29639      sqlite3TreeViewPop(pView);
29640    }
29641    if( p->pWhere ){
29642      sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
29643      sqlite3TreeViewExpr(pView, p->pWhere, 0);
29644      sqlite3TreeViewPop(pView);
29645    }
29646    if( p->pGroupBy ){
29647      sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
29648    }
29649    if( p->pHaving ){
29650      sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
29651      sqlite3TreeViewExpr(pView, p->pHaving, 0);
29652      sqlite3TreeViewPop(pView);
29653    }
29654#ifndef SQLITE_OMIT_WINDOWFUNC
29655    if( p->pWinDefn ){
29656      Window *pX;
29657      sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
29658      for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
29659        sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
29660      }
29661      sqlite3TreeViewPop(pView);
29662    }
29663#endif
29664    if( p->pOrderBy ){
29665      sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
29666    }
29667    if( p->pLimit ){
29668      sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
29669      sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
29670      if( p->pLimit->pRight ){
29671        sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
29672        sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
29673        sqlite3TreeViewPop(pView);
29674      }
29675      sqlite3TreeViewPop(pView);
29676    }
29677    if( p->pPrior ){
29678      const char *zOp = "UNION";
29679      switch( p->op ){
29680        case TK_ALL:         zOp = "UNION ALL";  break;
29681        case TK_INTERSECT:   zOp = "INTERSECT";  break;
29682        case TK_EXCEPT:      zOp = "EXCEPT";     break;
29683      }
29684      sqlite3TreeViewItem(pView, zOp, 1);
29685    }
29686    p = p->pPrior;
29687  }while( p!=0 );
29688  sqlite3TreeViewPop(pView);
29689}
29690
29691#ifndef SQLITE_OMIT_WINDOWFUNC
29692/*
29693** Generate a description of starting or stopping bounds
29694*/
29695SQLITE_PRIVATE void sqlite3TreeViewBound(
29696  TreeView *pView,        /* View context */
29697  u8 eBound,              /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */
29698  Expr *pExpr,            /* Value for PRECEDING or FOLLOWING */
29699  u8 moreToFollow         /* True if more to follow */
29700){
29701  switch( eBound ){
29702    case TK_UNBOUNDED: {
29703      sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow);
29704      sqlite3TreeViewPop(pView);
29705      break;
29706    }
29707    case TK_CURRENT: {
29708      sqlite3TreeViewItem(pView, "CURRENT", moreToFollow);
29709      sqlite3TreeViewPop(pView);
29710      break;
29711    }
29712    case TK_PRECEDING: {
29713      sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow);
29714      sqlite3TreeViewExpr(pView, pExpr, 0);
29715      sqlite3TreeViewPop(pView);
29716      break;
29717    }
29718    case TK_FOLLOWING: {
29719      sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow);
29720      sqlite3TreeViewExpr(pView, pExpr, 0);
29721      sqlite3TreeViewPop(pView);
29722      break;
29723    }
29724  }
29725}
29726#endif /* SQLITE_OMIT_WINDOWFUNC */
29727
29728#ifndef SQLITE_OMIT_WINDOWFUNC
29729/*
29730** Generate a human-readable explanation for a Window object
29731*/
29732SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
29733  int nElement = 0;
29734  if( pWin->pFilter ){
29735    sqlite3TreeViewItem(pView, "FILTER", 1);
29736    sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
29737    sqlite3TreeViewPop(pView);
29738  }
29739  pView = sqlite3TreeViewPush(pView, more);
29740  if( pWin->zName ){
29741    sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
29742  }else{
29743    sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
29744  }
29745  if( pWin->zBase )    nElement++;
29746  if( pWin->pOrderBy ) nElement++;
29747  if( pWin->eFrmType ) nElement++;
29748  if( pWin->eExclude ) nElement++;
29749  if( pWin->zBase ){
29750    sqlite3TreeViewPush(pView, (--nElement)>0);
29751    sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
29752    sqlite3TreeViewPop(pView);
29753  }
29754  if( pWin->pPartition ){
29755    sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
29756  }
29757  if( pWin->pOrderBy ){
29758    sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
29759  }
29760  if( pWin->eFrmType ){
29761    char zBuf[30];
29762    const char *zFrmType = "ROWS";
29763    if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
29764    if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
29765    sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
29766        pWin->bImplicitFrame ? " (implied)" : "");
29767    sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
29768    sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
29769    sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
29770    sqlite3TreeViewPop(pView);
29771  }
29772  if( pWin->eExclude ){
29773    char zBuf[30];
29774    const char *zExclude;
29775    switch( pWin->eExclude ){
29776      case TK_NO:      zExclude = "NO OTHERS";   break;
29777      case TK_CURRENT: zExclude = "CURRENT ROW"; break;
29778      case TK_GROUP:   zExclude = "GROUP";       break;
29779      case TK_TIES:    zExclude = "TIES";        break;
29780      default:
29781        sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
29782        zExclude = zBuf;
29783        break;
29784    }
29785    sqlite3TreeViewPush(pView, 0);
29786    sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
29787    sqlite3TreeViewPop(pView);
29788  }
29789  sqlite3TreeViewPop(pView);
29790}
29791#endif /* SQLITE_OMIT_WINDOWFUNC */
29792
29793#ifndef SQLITE_OMIT_WINDOWFUNC
29794/*
29795** Generate a human-readable explanation for a Window Function object
29796*/
29797SQLITE_PRIVATE void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){
29798  pView = sqlite3TreeViewPush(pView, more);
29799  sqlite3TreeViewLine(pView, "WINFUNC %s(%d)",
29800                       pWin->pFunc->zName, pWin->pFunc->nArg);
29801  sqlite3TreeViewWindow(pView, pWin, 0);
29802  sqlite3TreeViewPop(pView);
29803}
29804#endif /* SQLITE_OMIT_WINDOWFUNC */
29805
29806/*
29807** Generate a human-readable explanation of an expression tree.
29808*/
29809SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
29810  const char *zBinOp = 0;   /* Binary operator */
29811  const char *zUniOp = 0;   /* Unary operator */
29812  char zFlgs[200];
29813  pView = sqlite3TreeViewPush(pView, moreToFollow);
29814  if( pExpr==0 ){
29815    sqlite3TreeViewLine(pView, "nil");
29816    sqlite3TreeViewPop(pView);
29817    return;
29818  }
29819  if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){
29820    StrAccum x;
29821    sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
29822    sqlite3_str_appendf(&x, " fg.af=%x.%c",
29823      pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
29824    if( ExprHasProperty(pExpr, EP_FromJoin) ){
29825      sqlite3_str_appendf(&x, " iRJT=%d", pExpr->iRightJoinTable);
29826    }
29827    if( ExprHasProperty(pExpr, EP_FromDDL) ){
29828      sqlite3_str_appendf(&x, " DDL");
29829    }
29830    if( ExprHasVVAProperty(pExpr, EP_Immutable) ){
29831      sqlite3_str_appendf(&x, " IMMUTABLE");
29832    }
29833    sqlite3StrAccumFinish(&x);
29834  }else{
29835    zFlgs[0] = 0;
29836  }
29837  switch( pExpr->op ){
29838    case TK_AGG_COLUMN: {
29839      sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
29840            pExpr->iTable, pExpr->iColumn, zFlgs);
29841      break;
29842    }
29843    case TK_COLUMN: {
29844      if( pExpr->iTable<0 ){
29845        /* This only happens when coding check constraints */
29846        char zOp2[16];
29847        if( pExpr->op2 ){
29848          sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2);
29849        }else{
29850          zOp2[0] = 0;
29851        }
29852        sqlite3TreeViewLine(pView, "COLUMN(%d)%s%s",
29853                                    pExpr->iColumn, zFlgs, zOp2);
29854      }else{
29855        sqlite3TreeViewLine(pView, "{%d:%d} pTab=%p%s",
29856                        pExpr->iTable, pExpr->iColumn,
29857                        pExpr->y.pTab, zFlgs);
29858      }
29859      if( ExprHasProperty(pExpr, EP_FixedCol) ){
29860        sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
29861      }
29862      break;
29863    }
29864    case TK_INTEGER: {
29865      if( pExpr->flags & EP_IntValue ){
29866        sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
29867      }else{
29868        sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
29869      }
29870      break;
29871    }
29872#ifndef SQLITE_OMIT_FLOATING_POINT
29873    case TK_FLOAT: {
29874      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
29875      break;
29876    }
29877#endif
29878    case TK_STRING: {
29879      sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
29880      break;
29881    }
29882    case TK_NULL: {
29883      sqlite3TreeViewLine(pView,"NULL");
29884      break;
29885    }
29886    case TK_TRUEFALSE: {
29887      sqlite3TreeViewLine(pView,
29888         sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
29889      break;
29890    }
29891#ifndef SQLITE_OMIT_BLOB_LITERAL
29892    case TK_BLOB: {
29893      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
29894      break;
29895    }
29896#endif
29897    case TK_VARIABLE: {
29898      sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
29899                          pExpr->u.zToken, pExpr->iColumn);
29900      break;
29901    }
29902    case TK_REGISTER: {
29903      sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
29904      break;
29905    }
29906    case TK_ID: {
29907      sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
29908      break;
29909    }
29910#ifndef SQLITE_OMIT_CAST
29911    case TK_CAST: {
29912      /* Expressions of the form:   CAST(pLeft AS token) */
29913      sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
29914      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
29915      break;
29916    }
29917#endif /* SQLITE_OMIT_CAST */
29918    case TK_LT:      zBinOp = "LT";     break;
29919    case TK_LE:      zBinOp = "LE";     break;
29920    case TK_GT:      zBinOp = "GT";     break;
29921    case TK_GE:      zBinOp = "GE";     break;
29922    case TK_NE:      zBinOp = "NE";     break;
29923    case TK_EQ:      zBinOp = "EQ";     break;
29924    case TK_IS:      zBinOp = "IS";     break;
29925    case TK_ISNOT:   zBinOp = "ISNOT";  break;
29926    case TK_AND:     zBinOp = "AND";    break;
29927    case TK_OR:      zBinOp = "OR";     break;
29928    case TK_PLUS:    zBinOp = "ADD";    break;
29929    case TK_STAR:    zBinOp = "MUL";    break;
29930    case TK_MINUS:   zBinOp = "SUB";    break;
29931    case TK_REM:     zBinOp = "REM";    break;
29932    case TK_BITAND:  zBinOp = "BITAND"; break;
29933    case TK_BITOR:   zBinOp = "BITOR";  break;
29934    case TK_SLASH:   zBinOp = "DIV";    break;
29935    case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
29936    case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
29937    case TK_CONCAT:  zBinOp = "CONCAT"; break;
29938    case TK_DOT:     zBinOp = "DOT";    break;
29939    case TK_LIMIT:   zBinOp = "LIMIT";  break;
29940
29941    case TK_UMINUS:  zUniOp = "UMINUS"; break;
29942    case TK_UPLUS:   zUniOp = "UPLUS";  break;
29943    case TK_BITNOT:  zUniOp = "BITNOT"; break;
29944    case TK_NOT:     zUniOp = "NOT";    break;
29945    case TK_ISNULL:  zUniOp = "ISNULL"; break;
29946    case TK_NOTNULL: zUniOp = "NOTNULL"; break;
29947
29948    case TK_TRUTH: {
29949      int x;
29950      const char *azOp[] = {
29951         "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
29952      };
29953      assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
29954      assert( pExpr->pRight );
29955      assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE );
29956      x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
29957      zUniOp = azOp[x];
29958      break;
29959    }
29960
29961    case TK_SPAN: {
29962      sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
29963      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
29964      break;
29965    }
29966
29967    case TK_COLLATE: {
29968      /* COLLATE operators without the EP_Collate flag are intended to
29969      ** emulate collation associated with a table column.  These show
29970      ** up in the treeview output as "SOFT-COLLATE".  Explicit COLLATE
29971      ** operators that appear in the original SQL always have the
29972      ** EP_Collate bit set and appear in treeview output as just "COLLATE" */
29973      sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s",
29974        !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "",
29975        pExpr->u.zToken, zFlgs);
29976      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
29977      break;
29978    }
29979
29980    case TK_AGG_FUNCTION:
29981    case TK_FUNCTION: {
29982      ExprList *pFarg;       /* List of function arguments */
29983      Window *pWin;
29984      if( ExprHasProperty(pExpr, EP_TokenOnly) ){
29985        pFarg = 0;
29986        pWin = 0;
29987      }else{
29988        pFarg = pExpr->x.pList;
29989#ifndef SQLITE_OMIT_WINDOWFUNC
29990        pWin = ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0;
29991#else
29992        pWin = 0;
29993#endif
29994      }
29995      if( pExpr->op==TK_AGG_FUNCTION ){
29996        sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s iAgg=%d agg=%p",
29997                             pExpr->op2, pExpr->u.zToken, zFlgs,
29998                             pExpr->iAgg, pExpr->pAggInfo);
29999      }else if( pExpr->op2!=0 ){
30000        const char *zOp2;
30001        char zBuf[8];
30002        sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
30003        zOp2 = zBuf;
30004        if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck";
30005        if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr";
30006        if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx";
30007        if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol";
30008        sqlite3TreeViewLine(pView, "FUNCTION %Q%s op2=%s",
30009                            pExpr->u.zToken, zFlgs, zOp2);
30010      }else{
30011        sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
30012      }
30013      if( pFarg ){
30014        sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0);
30015      }
30016#ifndef SQLITE_OMIT_WINDOWFUNC
30017      if( pWin ){
30018        sqlite3TreeViewWindow(pView, pWin, 0);
30019      }
30020#endif
30021      break;
30022    }
30023#ifndef SQLITE_OMIT_SUBQUERY
30024    case TK_EXISTS: {
30025      sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
30026      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
30027      break;
30028    }
30029    case TK_SELECT: {
30030      sqlite3TreeViewLine(pView, "SELECT-expr flags=0x%x", pExpr->flags);
30031      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
30032      break;
30033    }
30034    case TK_IN: {
30035      sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags);
30036      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
30037      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
30038        sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
30039      }else{
30040        sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
30041      }
30042      break;
30043    }
30044#endif /* SQLITE_OMIT_SUBQUERY */
30045
30046    /*
30047    **    x BETWEEN y AND z
30048    **
30049    ** This is equivalent to
30050    **
30051    **    x>=y AND x<=z
30052    **
30053    ** X is stored in pExpr->pLeft.
30054    ** Y is stored in pExpr->pList->a[0].pExpr.
30055    ** Z is stored in pExpr->pList->a[1].pExpr.
30056    */
30057    case TK_BETWEEN: {
30058      Expr *pX = pExpr->pLeft;
30059      Expr *pY = pExpr->x.pList->a[0].pExpr;
30060      Expr *pZ = pExpr->x.pList->a[1].pExpr;
30061      sqlite3TreeViewLine(pView, "BETWEEN");
30062      sqlite3TreeViewExpr(pView, pX, 1);
30063      sqlite3TreeViewExpr(pView, pY, 1);
30064      sqlite3TreeViewExpr(pView, pZ, 0);
30065      break;
30066    }
30067    case TK_TRIGGER: {
30068      /* If the opcode is TK_TRIGGER, then the expression is a reference
30069      ** to a column in the new.* or old.* pseudo-tables available to
30070      ** trigger programs. In this case Expr.iTable is set to 1 for the
30071      ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
30072      ** is set to the column of the pseudo-table to read, or to -1 to
30073      ** read the rowid field.
30074      */
30075      sqlite3TreeViewLine(pView, "%s(%d)",
30076          pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
30077      break;
30078    }
30079    case TK_CASE: {
30080      sqlite3TreeViewLine(pView, "CASE");
30081      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
30082      sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
30083      break;
30084    }
30085#ifndef SQLITE_OMIT_TRIGGER
30086    case TK_RAISE: {
30087      const char *zType = "unk";
30088      switch( pExpr->affExpr ){
30089        case OE_Rollback:   zType = "rollback";  break;
30090        case OE_Abort:      zType = "abort";     break;
30091        case OE_Fail:       zType = "fail";      break;
30092        case OE_Ignore:     zType = "ignore";    break;
30093      }
30094      sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
30095      break;
30096    }
30097#endif
30098    case TK_MATCH: {
30099      sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
30100                          pExpr->iTable, pExpr->iColumn, zFlgs);
30101      sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
30102      break;
30103    }
30104    case TK_VECTOR: {
30105      char *z = sqlite3_mprintf("VECTOR%s",zFlgs);
30106      sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
30107      sqlite3_free(z);
30108      break;
30109    }
30110    case TK_SELECT_COLUMN: {
30111      sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn);
30112      sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
30113      break;
30114    }
30115    case TK_IF_NULL_ROW: {
30116      sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
30117      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30118      break;
30119    }
30120    default: {
30121      sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
30122      break;
30123    }
30124  }
30125  if( zBinOp ){
30126    sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
30127    sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
30128    sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
30129  }else if( zUniOp ){
30130    sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
30131   sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30132  }
30133  sqlite3TreeViewPop(pView);
30134}
30135
30136
30137/*
30138** Generate a human-readable explanation of an expression list.
30139*/
30140SQLITE_PRIVATE void sqlite3TreeViewBareExprList(
30141  TreeView *pView,
30142  const ExprList *pList,
30143  const char *zLabel
30144){
30145  if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
30146  if( pList==0 ){
30147    sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
30148  }else{
30149    int i;
30150    sqlite3TreeViewLine(pView, "%s", zLabel);
30151    for(i=0; i<pList->nExpr; i++){
30152      int j = pList->a[i].u.x.iOrderByCol;
30153      char *zName = pList->a[i].zEName;
30154      int moreToFollow = i<pList->nExpr - 1;
30155      if( pList->a[i].eEName!=ENAME_NAME ) zName = 0;
30156      if( j || zName ){
30157        sqlite3TreeViewPush(pView, moreToFollow);
30158        moreToFollow = 0;
30159        sqlite3TreeViewLine(pView, 0);
30160        if( zName ){
30161          fprintf(stdout, "AS %s ", zName);
30162        }
30163        if( j ){
30164          fprintf(stdout, "iOrderByCol=%d", j);
30165        }
30166        fprintf(stdout, "\n");
30167        fflush(stdout);
30168      }
30169      sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
30170      if( j || zName ){
30171        sqlite3TreeViewPop(pView);
30172      }
30173    }
30174  }
30175}
30176SQLITE_PRIVATE void sqlite3TreeViewExprList(
30177  TreeView *pView,
30178  const ExprList *pList,
30179  u8 moreToFollow,
30180  const char *zLabel
30181){
30182  pView = sqlite3TreeViewPush(pView, moreToFollow);
30183  sqlite3TreeViewBareExprList(pView, pList, zLabel);
30184  sqlite3TreeViewPop(pView);
30185}
30186
30187#endif /* SQLITE_DEBUG */
30188
30189/************** End of treeview.c ********************************************/
30190/************** Begin file random.c ******************************************/
30191/*
30192** 2001 September 15
30193**
30194** The author disclaims copyright to this source code.  In place of
30195** a legal notice, here is a blessing:
30196**
30197**    May you do good and not evil.
30198**    May you find forgiveness for yourself and forgive others.
30199**    May you share freely, never taking more than you give.
30200**
30201*************************************************************************
30202** This file contains code to implement a pseudo-random number
30203** generator (PRNG) for SQLite.
30204**
30205** Random numbers are used by some of the database backends in order
30206** to generate random integer keys for tables or random filenames.
30207*/
30208/* #include "sqliteInt.h" */
30209
30210
30211/* All threads share a single random number generator.
30212** This structure is the current state of the generator.
30213*/
30214static SQLITE_WSD struct sqlite3PrngType {
30215  unsigned char isInit;          /* True if initialized */
30216  unsigned char i, j;            /* State variables */
30217  unsigned char s[256];          /* State variables */
30218} sqlite3Prng;
30219
30220/*
30221** Return N random bytes.
30222*/
30223SQLITE_API void sqlite3_randomness(int N, void *pBuf){
30224  unsigned char t;
30225  unsigned char *zBuf = pBuf;
30226
30227  /* The "wsdPrng" macro will resolve to the pseudo-random number generator
30228  ** state vector.  If writable static data is unsupported on the target,
30229  ** we have to locate the state vector at run-time.  In the more common
30230  ** case where writable static data is supported, wsdPrng can refer directly
30231  ** to the "sqlite3Prng" state vector declared above.
30232  */
30233#ifdef SQLITE_OMIT_WSD
30234  struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
30235# define wsdPrng p[0]
30236#else
30237# define wsdPrng sqlite3Prng
30238#endif
30239
30240#if SQLITE_THREADSAFE
30241  sqlite3_mutex *mutex;
30242#endif
30243
30244#ifndef SQLITE_OMIT_AUTOINIT
30245  if( sqlite3_initialize() ) return;
30246#endif
30247
30248#if SQLITE_THREADSAFE
30249  mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
30250#endif
30251
30252  sqlite3_mutex_enter(mutex);
30253  if( N<=0 || pBuf==0 ){
30254    wsdPrng.isInit = 0;
30255    sqlite3_mutex_leave(mutex);
30256    return;
30257  }
30258
30259  /* Initialize the state of the random number generator once,
30260  ** the first time this routine is called.  The seed value does
30261  ** not need to contain a lot of randomness since we are not
30262  ** trying to do secure encryption or anything like that...
30263  **
30264  ** Nothing in this file or anywhere else in SQLite does any kind of
30265  ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
30266  ** number generator) not as an encryption device.
30267  */
30268  if( !wsdPrng.isInit ){
30269    int i;
30270    char k[256];
30271    wsdPrng.j = 0;
30272    wsdPrng.i = 0;
30273    sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
30274    for(i=0; i<256; i++){
30275      wsdPrng.s[i] = (u8)i;
30276    }
30277    for(i=0; i<256; i++){
30278      wsdPrng.j += wsdPrng.s[i] + k[i];
30279      t = wsdPrng.s[wsdPrng.j];
30280      wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
30281      wsdPrng.s[i] = t;
30282    }
30283    wsdPrng.isInit = 1;
30284  }
30285
30286  assert( N>0 );
30287  do{
30288    wsdPrng.i++;
30289    t = wsdPrng.s[wsdPrng.i];
30290    wsdPrng.j += t;
30291    wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
30292    wsdPrng.s[wsdPrng.j] = t;
30293    t += wsdPrng.s[wsdPrng.i];
30294    *(zBuf++) = wsdPrng.s[t];
30295  }while( --N );
30296  sqlite3_mutex_leave(mutex);
30297}
30298
30299#ifndef SQLITE_UNTESTABLE
30300/*
30301** For testing purposes, we sometimes want to preserve the state of
30302** PRNG and restore the PRNG to its saved state at a later time, or
30303** to reset the PRNG to its initial state.  These routines accomplish
30304** those tasks.
30305**
30306** The sqlite3_test_control() interface calls these routines to
30307** control the PRNG.
30308*/
30309static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
30310SQLITE_PRIVATE void sqlite3PrngSaveState(void){
30311  memcpy(
30312    &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
30313    &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
30314    sizeof(sqlite3Prng)
30315  );
30316}
30317SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
30318  memcpy(
30319    &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
30320    &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
30321    sizeof(sqlite3Prng)
30322  );
30323}
30324#endif /* SQLITE_UNTESTABLE */
30325
30326/************** End of random.c **********************************************/
30327/************** Begin file threads.c *****************************************/
30328/*
30329** 2012 July 21
30330**
30331** The author disclaims copyright to this source code.  In place of
30332** a legal notice, here is a blessing:
30333**
30334**    May you do good and not evil.
30335**    May you find forgiveness for yourself and forgive others.
30336**    May you share freely, never taking more than you give.
30337**
30338******************************************************************************
30339**
30340** This file presents a simple cross-platform threading interface for
30341** use internally by SQLite.
30342**
30343** A "thread" can be created using sqlite3ThreadCreate().  This thread
30344** runs independently of its creator until it is joined using
30345** sqlite3ThreadJoin(), at which point it terminates.
30346**
30347** Threads do not have to be real.  It could be that the work of the
30348** "thread" is done by the main thread at either the sqlite3ThreadCreate()
30349** or sqlite3ThreadJoin() call.  This is, in fact, what happens in
30350** single threaded systems.  Nothing in SQLite requires multiple threads.
30351** This interface exists so that applications that want to take advantage
30352** of multiple cores can do so, while also allowing applications to stay
30353** single-threaded if desired.
30354*/
30355/* #include "sqliteInt.h" */
30356#if SQLITE_OS_WIN
30357/* #  include "os_win.h" */
30358#endif
30359
30360#if SQLITE_MAX_WORKER_THREADS>0
30361
30362/********************************* Unix Pthreads ****************************/
30363#if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE>0
30364
30365#define SQLITE_THREADS_IMPLEMENTED 1  /* Prevent the single-thread code below */
30366/* #include <pthread.h> */
30367
30368/* A running thread */
30369struct SQLiteThread {
30370  pthread_t tid;                 /* Thread ID */
30371  int done;                      /* Set to true when thread finishes */
30372  void *pOut;                    /* Result returned by the thread */
30373  void *(*xTask)(void*);         /* The thread routine */
30374  void *pIn;                     /* Argument to the thread */
30375};
30376
30377/* Create a new thread */
30378SQLITE_PRIVATE int sqlite3ThreadCreate(
30379  SQLiteThread **ppThread,  /* OUT: Write the thread object here */
30380  void *(*xTask)(void*),    /* Routine to run in a separate thread */
30381  void *pIn                 /* Argument passed into xTask() */
30382){
30383  SQLiteThread *p;
30384  int rc;
30385
30386  assert( ppThread!=0 );
30387  assert( xTask!=0 );
30388  /* This routine is never used in single-threaded mode */
30389  assert( sqlite3GlobalConfig.bCoreMutex!=0 );
30390
30391  *ppThread = 0;
30392  p = sqlite3Malloc(sizeof(*p));
30393  if( p==0 ) return SQLITE_NOMEM_BKPT;
30394  memset(p, 0, sizeof(*p));
30395  p->xTask = xTask;
30396  p->pIn = pIn;
30397  /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
30398  ** function that returns SQLITE_ERROR when passed the argument 200, that
30399  ** forces worker threads to run sequentially and deterministically
30400  ** for testing purposes. */
30401  if( sqlite3FaultSim(200) ){
30402    rc = 1;
30403  }else{
30404    rc = pthread_create(&p->tid, 0, xTask, pIn);
30405  }
30406  if( rc ){
30407    p->done = 1;
30408    p->pOut = xTask(pIn);
30409  }
30410  *ppThread = p;
30411  return SQLITE_OK;
30412}
30413
30414/* Get the results of the thread */
30415SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
30416  int rc;
30417
30418  assert( ppOut!=0 );
30419  if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
30420  if( p->done ){
30421    *ppOut = p->pOut;
30422    rc = SQLITE_OK;
30423  }else{
30424    rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK;
30425  }
30426  sqlite3_free(p);
30427  return rc;
30428}
30429
30430#endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */
30431/******************************** End Unix Pthreads *************************/
30432
30433
30434/********************************* Win32 Threads ****************************/
30435#if SQLITE_OS_WIN_THREADS
30436
30437#define SQLITE_THREADS_IMPLEMENTED 1  /* Prevent the single-thread code below */
30438#include <process.h>
30439
30440/* A running thread */
30441struct SQLiteThread {
30442  void *tid;               /* The thread handle */
30443  unsigned id;             /* The thread identifier */
30444  void *(*xTask)(void*);   /* The routine to run as a thread */
30445  void *pIn;               /* Argument to xTask */
30446  void *pResult;           /* Result of xTask */
30447};
30448
30449/* Thread procedure Win32 compatibility shim */
30450static unsigned __stdcall sqlite3ThreadProc(
30451  void *pArg  /* IN: Pointer to the SQLiteThread structure */
30452){
30453  SQLiteThread *p = (SQLiteThread *)pArg;
30454
30455  assert( p!=0 );
30456#if 0
30457  /*
30458  ** This assert appears to trigger spuriously on certain
30459  ** versions of Windows, possibly due to _beginthreadex()
30460  ** and/or CreateThread() not fully setting their thread
30461  ** ID parameter before starting the thread.
30462  */
30463  assert( p->id==GetCurrentThreadId() );
30464#endif
30465  assert( p->xTask!=0 );
30466  p->pResult = p->xTask(p->pIn);
30467
30468  _endthreadex(0);
30469  return 0; /* NOT REACHED */
30470}
30471
30472/* Create a new thread */
30473SQLITE_PRIVATE int sqlite3ThreadCreate(
30474  SQLiteThread **ppThread,  /* OUT: Write the thread object here */
30475  void *(*xTask)(void*),    /* Routine to run in a separate thread */
30476  void *pIn                 /* Argument passed into xTask() */
30477){
30478  SQLiteThread *p;
30479
30480  assert( ppThread!=0 );
30481  assert( xTask!=0 );
30482  *ppThread = 0;
30483  p = sqlite3Malloc(sizeof(*p));
30484  if( p==0 ) return SQLITE_NOMEM_BKPT;
30485  /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
30486  ** function that returns SQLITE_ERROR when passed the argument 200, that
30487  ** forces worker threads to run sequentially and deterministically
30488  ** (via the sqlite3FaultSim() term of the conditional) for testing
30489  ** purposes. */
30490  if( sqlite3GlobalConfig.bCoreMutex==0 || sqlite3FaultSim(200) ){
30491    memset(p, 0, sizeof(*p));
30492  }else{
30493    p->xTask = xTask;
30494    p->pIn = pIn;
30495    p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
30496    if( p->tid==0 ){
30497      memset(p, 0, sizeof(*p));
30498    }
30499  }
30500  if( p->xTask==0 ){
30501    p->id = GetCurrentThreadId();
30502    p->pResult = xTask(pIn);
30503  }
30504  *ppThread = p;
30505  return SQLITE_OK;
30506}
30507
30508SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject); /* os_win.c */
30509
30510/* Get the results of the thread */
30511SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
30512  DWORD rc;
30513  BOOL bRc;
30514
30515  assert( ppOut!=0 );
30516  if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
30517  if( p->xTask==0 ){
30518    /* assert( p->id==GetCurrentThreadId() ); */
30519    rc = WAIT_OBJECT_0;
30520    assert( p->tid==0 );
30521  }else{
30522    assert( p->id!=0 && p->id!=GetCurrentThreadId() );
30523    rc = sqlite3Win32Wait((HANDLE)p->tid);
30524    assert( rc!=WAIT_IO_COMPLETION );
30525    bRc = CloseHandle((HANDLE)p->tid);
30526    assert( bRc );
30527  }
30528  if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
30529  sqlite3_free(p);
30530  return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR;
30531}
30532
30533#endif /* SQLITE_OS_WIN_THREADS */
30534/******************************** End Win32 Threads *************************/
30535
30536
30537/********************************* Single-Threaded **************************/
30538#ifndef SQLITE_THREADS_IMPLEMENTED
30539/*
30540** This implementation does not actually create a new thread.  It does the
30541** work of the thread in the main thread, when either the thread is created
30542** or when it is joined
30543*/
30544
30545/* A running thread */
30546struct SQLiteThread {
30547  void *(*xTask)(void*);   /* The routine to run as a thread */
30548  void *pIn;               /* Argument to xTask */
30549  void *pResult;           /* Result of xTask */
30550};
30551
30552/* Create a new thread */
30553SQLITE_PRIVATE int sqlite3ThreadCreate(
30554  SQLiteThread **ppThread,  /* OUT: Write the thread object here */
30555  void *(*xTask)(void*),    /* Routine to run in a separate thread */
30556  void *pIn                 /* Argument passed into xTask() */
30557){
30558  SQLiteThread *p;
30559
30560  assert( ppThread!=0 );
30561  assert( xTask!=0 );
30562  *ppThread = 0;
30563  p = sqlite3Malloc(sizeof(*p));
30564  if( p==0 ) return SQLITE_NOMEM_BKPT;
30565  if( (SQLITE_PTR_TO_INT(p)/17)&1 ){
30566    p->xTask = xTask;
30567    p->pIn = pIn;
30568  }else{
30569    p->xTask = 0;
30570    p->pResult = xTask(pIn);
30571  }
30572  *ppThread = p;
30573  return SQLITE_OK;
30574}
30575
30576/* Get the results of the thread */
30577SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
30578
30579  assert( ppOut!=0 );
30580  if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
30581  if( p->xTask ){
30582    *ppOut = p->xTask(p->pIn);
30583  }else{
30584    *ppOut = p->pResult;
30585  }
30586  sqlite3_free(p);
30587
30588#if defined(SQLITE_TEST)
30589  {
30590    void *pTstAlloc = sqlite3Malloc(10);
30591    if (!pTstAlloc) return SQLITE_NOMEM_BKPT;
30592    sqlite3_free(pTstAlloc);
30593  }
30594#endif
30595
30596  return SQLITE_OK;
30597}
30598
30599#endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */
30600/****************************** End Single-Threaded *************************/
30601#endif /* SQLITE_MAX_WORKER_THREADS>0 */
30602
30603/************** End of threads.c *********************************************/
30604/************** Begin file utf.c *********************************************/
30605/*
30606** 2004 April 13
30607**
30608** The author disclaims copyright to this source code.  In place of
30609** a legal notice, here is a blessing:
30610**
30611**    May you do good and not evil.
30612**    May you find forgiveness for yourself and forgive others.
30613**    May you share freely, never taking more than you give.
30614**
30615*************************************************************************
30616** This file contains routines used to translate between UTF-8,
30617** UTF-16, UTF-16BE, and UTF-16LE.
30618**
30619** Notes on UTF-8:
30620**
30621**   Byte-0    Byte-1    Byte-2    Byte-3    Value
30622**  0xxxxxxx                                 00000000 00000000 0xxxxxxx
30623**  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
30624**  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
30625**  11110uuu  10uuzzzz  10yyyyyy  10xxxxxx   000uuuuu zzzzyyyy yyxxxxxx
30626**
30627**
30628** Notes on UTF-16:  (with wwww+1==uuuuu)
30629**
30630**      Word-0               Word-1          Value
30631**  110110ww wwzzzzyy   110111yy yyxxxxxx    000uuuuu zzzzyyyy yyxxxxxx
30632**  zzzzyyyy yyxxxxxx                        00000000 zzzzyyyy yyxxxxxx
30633**
30634**
30635** BOM or Byte Order Mark:
30636**     0xff 0xfe   little-endian utf-16 follows
30637**     0xfe 0xff   big-endian utf-16 follows
30638**
30639*/
30640/* #include "sqliteInt.h" */
30641/* #include <assert.h> */
30642/* #include "vdbeInt.h" */
30643
30644#if !defined(SQLITE_AMALGAMATION) && SQLITE_BYTEORDER==0
30645/*
30646** The following constant value is used by the SQLITE_BIGENDIAN and
30647** SQLITE_LITTLEENDIAN macros.
30648*/
30649SQLITE_PRIVATE const int sqlite3one = 1;
30650#endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */
30651
30652/*
30653** This lookup table is used to help decode the first byte of
30654** a multi-byte UTF8 character.
30655*/
30656static const unsigned char sqlite3Utf8Trans1[] = {
30657  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
30658  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
30659  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
30660  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
30661  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
30662  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
30663  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
30664  0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
30665};
30666
30667
30668#define WRITE_UTF8(zOut, c) {                          \
30669  if( c<0x00080 ){                                     \
30670    *zOut++ = (u8)(c&0xFF);                            \
30671  }                                                    \
30672  else if( c<0x00800 ){                                \
30673    *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
30674    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
30675  }                                                    \
30676  else if( c<0x10000 ){                                \
30677    *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
30678    *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
30679    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
30680  }else{                                               \
30681    *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
30682    *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
30683    *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
30684    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
30685  }                                                    \
30686}
30687
30688#define WRITE_UTF16LE(zOut, c) {                                    \
30689  if( c<=0xFFFF ){                                                  \
30690    *zOut++ = (u8)(c&0x00FF);                                       \
30691    *zOut++ = (u8)((c>>8)&0x00FF);                                  \
30692  }else{                                                            \
30693    *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
30694    *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
30695    *zOut++ = (u8)(c&0x00FF);                                       \
30696    *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
30697  }                                                                 \
30698}
30699
30700#define WRITE_UTF16BE(zOut, c) {                                    \
30701  if( c<=0xFFFF ){                                                  \
30702    *zOut++ = (u8)((c>>8)&0x00FF);                                  \
30703    *zOut++ = (u8)(c&0x00FF);                                       \
30704  }else{                                                            \
30705    *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
30706    *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
30707    *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
30708    *zOut++ = (u8)(c&0x00FF);                                       \
30709  }                                                                 \
30710}
30711
30712/*
30713** Translate a single UTF-8 character.  Return the unicode value.
30714**
30715** During translation, assume that the byte that zTerm points
30716** is a 0x00.
30717**
30718** Write a pointer to the next unread byte back into *pzNext.
30719**
30720** Notes On Invalid UTF-8:
30721**
30722**  *  This routine never allows a 7-bit character (0x00 through 0x7f) to
30723**     be encoded as a multi-byte character.  Any multi-byte character that
30724**     attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
30725**
30726**  *  This routine never allows a UTF16 surrogate value to be encoded.
30727**     If a multi-byte character attempts to encode a value between
30728**     0xd800 and 0xe000 then it is rendered as 0xfffd.
30729**
30730**  *  Bytes in the range of 0x80 through 0xbf which occur as the first
30731**     byte of a character are interpreted as single-byte characters
30732**     and rendered as themselves even though they are technically
30733**     invalid characters.
30734**
30735**  *  This routine accepts over-length UTF8 encodings
30736**     for unicode values 0x80 and greater.  It does not change over-length
30737**     encodings to 0xfffd as some systems recommend.
30738*/
30739#define READ_UTF8(zIn, zTerm, c)                           \
30740  c = *(zIn++);                                            \
30741  if( c>=0xc0 ){                                           \
30742    c = sqlite3Utf8Trans1[c-0xc0];                         \
30743    while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
30744      c = (c<<6) + (0x3f & *(zIn++));                      \
30745    }                                                      \
30746    if( c<0x80                                             \
30747        || (c&0xFFFFF800)==0xD800                          \
30748        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
30749  }
30750SQLITE_PRIVATE u32 sqlite3Utf8Read(
30751  const unsigned char **pz    /* Pointer to string from which to read char */
30752){
30753  unsigned int c;
30754
30755  /* Same as READ_UTF8() above but without the zTerm parameter.
30756  ** For this routine, we assume the UTF8 string is always zero-terminated.
30757  */
30758  c = *((*pz)++);
30759  if( c>=0xc0 ){
30760    c = sqlite3Utf8Trans1[c-0xc0];
30761    while( (*(*pz) & 0xc0)==0x80 ){
30762      c = (c<<6) + (0x3f & *((*pz)++));
30763    }
30764    if( c<0x80
30765        || (c&0xFFFFF800)==0xD800
30766        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
30767  }
30768  return c;
30769}
30770
30771
30772
30773
30774/*
30775** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
30776** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
30777*/
30778/* #define TRANSLATE_TRACE 1 */
30779
30780#ifndef SQLITE_OMIT_UTF16
30781/*
30782** This routine transforms the internal text encoding used by pMem to
30783** desiredEnc. It is an error if the string is already of the desired
30784** encoding, or if *pMem does not contain a string value.
30785*/
30786SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
30787  sqlite3_int64 len;          /* Maximum length of output string in bytes */
30788  unsigned char *zOut;        /* Output buffer */
30789  unsigned char *zIn;         /* Input iterator */
30790  unsigned char *zTerm;       /* End of input */
30791  unsigned char *z;           /* Output iterator */
30792  unsigned int c;
30793
30794  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
30795  assert( pMem->flags&MEM_Str );
30796  assert( pMem->enc!=desiredEnc );
30797  assert( pMem->enc!=0 );
30798  assert( pMem->n>=0 );
30799
30800#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
30801  {
30802    StrAccum acc;
30803    char zBuf[1000];
30804    sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
30805    sqlite3VdbeMemPrettyPrint(pMem, &acc);
30806    fprintf(stderr, "INPUT:  %s\n", sqlite3StrAccumFinish(&acc));
30807  }
30808#endif
30809
30810  /* If the translation is between UTF-16 little and big endian, then
30811  ** all that is required is to swap the byte order. This case is handled
30812  ** differently from the others.
30813  */
30814  if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
30815    u8 temp;
30816    int rc;
30817    rc = sqlite3VdbeMemMakeWriteable(pMem);
30818    if( rc!=SQLITE_OK ){
30819      assert( rc==SQLITE_NOMEM );
30820      return SQLITE_NOMEM_BKPT;
30821    }
30822    zIn = (u8*)pMem->z;
30823    zTerm = &zIn[pMem->n&~1];
30824    while( zIn<zTerm ){
30825      temp = *zIn;
30826      *zIn = *(zIn+1);
30827      zIn++;
30828      *zIn++ = temp;
30829    }
30830    pMem->enc = desiredEnc;
30831    goto translate_out;
30832  }
30833
30834  /* Set len to the maximum number of bytes required in the output buffer. */
30835  if( desiredEnc==SQLITE_UTF8 ){
30836    /* When converting from UTF-16, the maximum growth results from
30837    ** translating a 2-byte character to a 4-byte UTF-8 character.
30838    ** A single byte is required for the output string
30839    ** nul-terminator.
30840    */
30841    pMem->n &= ~1;
30842    len = 2 * (sqlite3_int64)pMem->n + 1;
30843  }else{
30844    /* When converting from UTF-8 to UTF-16 the maximum growth is caused
30845    ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
30846    ** character. Two bytes are required in the output buffer for the
30847    ** nul-terminator.
30848    */
30849    len = 2 * (sqlite3_int64)pMem->n + 2;
30850  }
30851
30852  /* Set zIn to point at the start of the input buffer and zTerm to point 1
30853  ** byte past the end.
30854  **
30855  ** Variable zOut is set to point at the output buffer, space obtained
30856  ** from sqlite3_malloc().
30857  */
30858  zIn = (u8*)pMem->z;
30859  zTerm = &zIn[pMem->n];
30860  zOut = sqlite3DbMallocRaw(pMem->db, len);
30861  if( !zOut ){
30862    return SQLITE_NOMEM_BKPT;
30863  }
30864  z = zOut;
30865
30866  if( pMem->enc==SQLITE_UTF8 ){
30867    if( desiredEnc==SQLITE_UTF16LE ){
30868      /* UTF-8 -> UTF-16 Little-endian */
30869      while( zIn<zTerm ){
30870        READ_UTF8(zIn, zTerm, c);
30871        WRITE_UTF16LE(z, c);
30872      }
30873    }else{
30874      assert( desiredEnc==SQLITE_UTF16BE );
30875      /* UTF-8 -> UTF-16 Big-endian */
30876      while( zIn<zTerm ){
30877        READ_UTF8(zIn, zTerm, c);
30878        WRITE_UTF16BE(z, c);
30879      }
30880    }
30881    pMem->n = (int)(z - zOut);
30882    *z++ = 0;
30883  }else{
30884    assert( desiredEnc==SQLITE_UTF8 );
30885    if( pMem->enc==SQLITE_UTF16LE ){
30886      /* UTF-16 Little-endian -> UTF-8 */
30887      while( zIn<zTerm ){
30888        c = *(zIn++);
30889        c += (*(zIn++))<<8;
30890        if( c>=0xd800 && c<0xe000 ){
30891#ifdef SQLITE_REPLACE_INVALID_UTF
30892          if( c>=0xdc00 || zIn>=zTerm ){
30893            c = 0xfffd;
30894          }else{
30895            int c2 = *(zIn++);
30896            c2 += (*(zIn++))<<8;
30897            if( c2<0xdc00 || c2>=0xe000 ){
30898              zIn -= 2;
30899              c = 0xfffd;
30900            }else{
30901              c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
30902            }
30903          }
30904#else
30905          if( zIn<zTerm ){
30906            int c2 = (*zIn++);
30907            c2 += ((*zIn++)<<8);
30908            c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
30909          }
30910#endif
30911        }
30912        WRITE_UTF8(z, c);
30913      }
30914    }else{
30915      /* UTF-16 Big-endian -> UTF-8 */
30916      while( zIn<zTerm ){
30917        c = (*(zIn++))<<8;
30918        c += *(zIn++);
30919        if( c>=0xd800 && c<0xe000 ){
30920#ifdef SQLITE_REPLACE_INVALID_UTF
30921          if( c>=0xdc00 || zIn>=zTerm ){
30922            c = 0xfffd;
30923          }else{
30924            int c2 = (*(zIn++))<<8;
30925            c2 += *(zIn++);
30926            if( c2<0xdc00 || c2>=0xe000 ){
30927              zIn -= 2;
30928              c = 0xfffd;
30929            }else{
30930              c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
30931            }
30932          }
30933#else
30934          if( zIn<zTerm ){
30935            int c2 = ((*zIn++)<<8);
30936            c2 += (*zIn++);
30937            c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
30938          }
30939#endif
30940        }
30941        WRITE_UTF8(z, c);
30942      }
30943    }
30944    pMem->n = (int)(z - zOut);
30945  }
30946  *z = 0;
30947  assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
30948
30949  c = pMem->flags;
30950  sqlite3VdbeMemRelease(pMem);
30951  pMem->flags = MEM_Str|MEM_Term|(c&(MEM_AffMask|MEM_Subtype));
30952  pMem->enc = desiredEnc;
30953  pMem->z = (char*)zOut;
30954  pMem->zMalloc = pMem->z;
30955  pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
30956
30957translate_out:
30958#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
30959  {
30960    StrAccum acc;
30961    char zBuf[1000];
30962    sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
30963    sqlite3VdbeMemPrettyPrint(pMem, &acc);
30964    fprintf(stderr, "OUTPUT: %s\n", sqlite3StrAccumFinish(&acc));
30965  }
30966#endif
30967  return SQLITE_OK;
30968}
30969#endif /* SQLITE_OMIT_UTF16 */
30970
30971#ifndef SQLITE_OMIT_UTF16
30972/*
30973** This routine checks for a byte-order mark at the beginning of the
30974** UTF-16 string stored in *pMem. If one is present, it is removed and
30975** the encoding of the Mem adjusted. This routine does not do any
30976** byte-swapping, it just sets Mem.enc appropriately.
30977**
30978** The allocation (static, dynamic etc.) and encoding of the Mem may be
30979** changed by this function.
30980*/
30981SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
30982  int rc = SQLITE_OK;
30983  u8 bom = 0;
30984
30985  assert( pMem->n>=0 );
30986  if( pMem->n>1 ){
30987    u8 b1 = *(u8 *)pMem->z;
30988    u8 b2 = *(((u8 *)pMem->z) + 1);
30989    if( b1==0xFE && b2==0xFF ){
30990      bom = SQLITE_UTF16BE;
30991    }
30992    if( b1==0xFF && b2==0xFE ){
30993      bom = SQLITE_UTF16LE;
30994    }
30995  }
30996
30997  if( bom ){
30998    rc = sqlite3VdbeMemMakeWriteable(pMem);
30999    if( rc==SQLITE_OK ){
31000      pMem->n -= 2;
31001      memmove(pMem->z, &pMem->z[2], pMem->n);
31002      pMem->z[pMem->n] = '\0';
31003      pMem->z[pMem->n+1] = '\0';
31004      pMem->flags |= MEM_Term;
31005      pMem->enc = bom;
31006    }
31007  }
31008  return rc;
31009}
31010#endif /* SQLITE_OMIT_UTF16 */
31011
31012/*
31013** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
31014** return the number of unicode characters in pZ up to (but not including)
31015** the first 0x00 byte. If nByte is not less than zero, return the
31016** number of unicode characters in the first nByte of pZ (or up to
31017** the first 0x00, whichever comes first).
31018*/
31019SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
31020  int r = 0;
31021  const u8 *z = (const u8*)zIn;
31022  const u8 *zTerm;
31023  if( nByte>=0 ){
31024    zTerm = &z[nByte];
31025  }else{
31026    zTerm = (const u8*)(-1);
31027  }
31028  assert( z<=zTerm );
31029  while( *z!=0 && z<zTerm ){
31030    SQLITE_SKIP_UTF8(z);
31031    r++;
31032  }
31033  return r;
31034}
31035
31036/* This test function is not currently used by the automated test-suite.
31037** Hence it is only available in debug builds.
31038*/
31039#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
31040/*
31041** Translate UTF-8 to UTF-8.
31042**
31043** This has the effect of making sure that the string is well-formed
31044** UTF-8.  Miscoded characters are removed.
31045**
31046** The translation is done in-place and aborted if the output
31047** overruns the input.
31048*/
31049SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
31050  unsigned char *zOut = zIn;
31051  unsigned char *zStart = zIn;
31052  u32 c;
31053
31054  while( zIn[0] && zOut<=zIn ){
31055    c = sqlite3Utf8Read((const u8**)&zIn);
31056    if( c!=0xfffd ){
31057      WRITE_UTF8(zOut, c);
31058    }
31059  }
31060  *zOut = 0;
31061  return (int)(zOut - zStart);
31062}
31063#endif
31064
31065#ifndef SQLITE_OMIT_UTF16
31066/*
31067** Convert a UTF-16 string in the native encoding into a UTF-8 string.
31068** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
31069** be freed by the calling function.
31070**
31071** NULL is returned if there is an allocation error.
31072*/
31073SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
31074  Mem m;
31075  memset(&m, 0, sizeof(m));
31076  m.db = db;
31077  sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
31078  sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
31079  if( db->mallocFailed ){
31080    sqlite3VdbeMemRelease(&m);
31081    m.z = 0;
31082  }
31083  assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
31084  assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
31085  assert( m.z || db->mallocFailed );
31086  return m.z;
31087}
31088
31089/*
31090** zIn is a UTF-16 encoded unicode string at least nChar characters long.
31091** Return the number of bytes in the first nChar unicode characters
31092** in pZ.  nChar must be non-negative.
31093*/
31094SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
31095  int c;
31096  unsigned char const *z = zIn;
31097  int n = 0;
31098
31099  if( SQLITE_UTF16NATIVE==SQLITE_UTF16LE ) z++;
31100  while( n<nChar ){
31101    c = z[0];
31102    z += 2;
31103    if( c>=0xd8 && c<0xdc && z[0]>=0xdc && z[0]<0xe0 ) z += 2;
31104    n++;
31105  }
31106  return (int)(z-(unsigned char const *)zIn)
31107              - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE);
31108}
31109
31110#if defined(SQLITE_TEST)
31111/*
31112** This routine is called from the TCL test function "translate_selftest".
31113** It checks that the primitives for serializing and deserializing
31114** characters in each encoding are inverses of each other.
31115*/
31116SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
31117  unsigned int i, t;
31118  unsigned char zBuf[20];
31119  unsigned char *z;
31120  int n;
31121  unsigned int c;
31122
31123  for(i=0; i<0x00110000; i++){
31124    z = zBuf;
31125    WRITE_UTF8(z, i);
31126    n = (int)(z-zBuf);
31127    assert( n>0 && n<=4 );
31128    z[0] = 0;
31129    z = zBuf;
31130    c = sqlite3Utf8Read((const u8**)&z);
31131    t = i;
31132    if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
31133    if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
31134    assert( c==t );
31135    assert( (z-zBuf)==n );
31136  }
31137}
31138#endif /* SQLITE_TEST */
31139#endif /* SQLITE_OMIT_UTF16 */
31140
31141/************** End of utf.c *************************************************/
31142/************** Begin file util.c ********************************************/
31143/*
31144** 2001 September 15
31145**
31146** The author disclaims copyright to this source code.  In place of
31147** a legal notice, here is a blessing:
31148**
31149**    May you do good and not evil.
31150**    May you find forgiveness for yourself and forgive others.
31151**    May you share freely, never taking more than you give.
31152**
31153*************************************************************************
31154** Utility functions used throughout sqlite.
31155**
31156** This file contains functions for allocating memory, comparing
31157** strings, and stuff like that.
31158**
31159*/
31160/* #include "sqliteInt.h" */
31161/* #include <stdarg.h> */
31162#ifndef SQLITE_OMIT_FLOATING_POINT
31163#include <math.h>
31164#endif
31165
31166/*
31167** Routine needed to support the testcase() macro.
31168*/
31169#ifdef SQLITE_COVERAGE_TEST
31170SQLITE_PRIVATE void sqlite3Coverage(int x){
31171  static unsigned dummy = 0;
31172  dummy += (unsigned)x;
31173}
31174#endif
31175
31176/*
31177** Calls to sqlite3FaultSim() are used to simulate a failure during testing,
31178** or to bypass normal error detection during testing in order to let
31179** execute proceed futher downstream.
31180**
31181** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0).  The
31182** sqlite3FaultSim() function only returns non-zero during testing.
31183**
31184** During testing, if the test harness has set a fault-sim callback using
31185** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then
31186** each call to sqlite3FaultSim() is relayed to that application-supplied
31187** callback and the integer return value form the application-supplied
31188** callback is returned by sqlite3FaultSim().
31189**
31190** The integer argument to sqlite3FaultSim() is a code to identify which
31191** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim()
31192** should have a unique code.  To prevent legacy testing applications from
31193** breaking, the codes should not be changed or reused.
31194*/
31195#ifndef SQLITE_UNTESTABLE
31196SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
31197  int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
31198  return xCallback ? xCallback(iTest) : SQLITE_OK;
31199}
31200#endif
31201
31202#ifndef SQLITE_OMIT_FLOATING_POINT
31203/*
31204** Return true if the floating point value is Not a Number (NaN).
31205*/
31206SQLITE_PRIVATE int sqlite3IsNaN(double x){
31207  u64 y;
31208  memcpy(&y,&x,sizeof(y));
31209  return IsNaN(y);
31210}
31211#endif /* SQLITE_OMIT_FLOATING_POINT */
31212
31213/*
31214** Compute a string length that is limited to what can be stored in
31215** lower 30 bits of a 32-bit signed integer.
31216**
31217** The value returned will never be negative.  Nor will it ever be greater
31218** than the actual length of the string.  For very long strings (greater
31219** than 1GiB) the value returned might be less than the true string length.
31220*/
31221SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
31222  if( z==0 ) return 0;
31223  return 0x3fffffff & (int)strlen(z);
31224}
31225
31226/*
31227** Return the declared type of a column.  Or return zDflt if the column
31228** has no declared type.
31229**
31230** The column type is an extra string stored after the zero-terminator on
31231** the column name if and only if the COLFLAG_HASTYPE flag is set.
31232*/
31233SQLITE_PRIVATE char *sqlite3ColumnType(Column *pCol, char *zDflt){
31234  if( (pCol->colFlags & COLFLAG_HASTYPE)==0 ) return zDflt;
31235  return pCol->zName + strlen(pCol->zName) + 1;
31236}
31237
31238/*
31239** Helper function for sqlite3Error() - called rarely.  Broken out into
31240** a separate routine to avoid unnecessary register saves on entry to
31241** sqlite3Error().
31242*/
31243static SQLITE_NOINLINE void  sqlite3ErrorFinish(sqlite3 *db, int err_code){
31244  if( db->pErr ) sqlite3ValueSetNull(db->pErr);
31245  sqlite3SystemError(db, err_code);
31246}
31247
31248/*
31249** Set the current error code to err_code and clear any prior error message.
31250** Also set iSysErrno (by calling sqlite3System) if the err_code indicates
31251** that would be appropriate.
31252*/
31253SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){
31254  assert( db!=0 );
31255  db->errCode = err_code;
31256  if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code);
31257}
31258
31259/*
31260** Load the sqlite3.iSysErrno field if that is an appropriate thing
31261** to do based on the SQLite error code in rc.
31262*/
31263SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){
31264  if( rc==SQLITE_IOERR_NOMEM ) return;
31265  rc &= 0xff;
31266  if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){
31267    db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
31268  }
31269}
31270
31271/*
31272** Set the most recent error code and error string for the sqlite
31273** handle "db". The error code is set to "err_code".
31274**
31275** If it is not NULL, string zFormat specifies the format of the
31276** error string in the style of the printf functions: The following
31277** format characters are allowed:
31278**
31279**      %s      Insert a string
31280**      %z      A string that should be freed after use
31281**      %d      Insert an integer
31282**      %T      Insert a token
31283**      %S      Insert the first element of a SrcList
31284**
31285** zFormat and any string tokens that follow it are assumed to be
31286** encoded in UTF-8.
31287**
31288** To clear the most recent error for sqlite handle "db", sqlite3Error
31289** should be called with err_code set to SQLITE_OK and zFormat set
31290** to NULL.
31291*/
31292SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
31293  assert( db!=0 );
31294  db->errCode = err_code;
31295  sqlite3SystemError(db, err_code);
31296  if( zFormat==0 ){
31297    sqlite3Error(db, err_code);
31298  }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
31299    char *z;
31300    va_list ap;
31301    va_start(ap, zFormat);
31302    z = sqlite3VMPrintf(db, zFormat, ap);
31303    va_end(ap);
31304    sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
31305  }
31306}
31307
31308/*
31309** Add an error message to pParse->zErrMsg and increment pParse->nErr.
31310** The following formatting characters are allowed:
31311**
31312**      %s      Insert a string
31313**      %z      A string that should be freed after use
31314**      %d      Insert an integer
31315**      %T      Insert a token
31316**      %S      Insert the first element of a SrcList
31317**
31318** This function should be used to report any error that occurs while
31319** compiling an SQL statement (i.e. within sqlite3_prepare()). The
31320** last thing the sqlite3_prepare() function does is copy the error
31321** stored by this function into the database handle using sqlite3Error().
31322** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used
31323** during statement execution (sqlite3_step() etc.).
31324*/
31325SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
31326  char *zMsg;
31327  va_list ap;
31328  sqlite3 *db = pParse->db;
31329  va_start(ap, zFormat);
31330  zMsg = sqlite3VMPrintf(db, zFormat, ap);
31331  va_end(ap);
31332  if( db->suppressErr ){
31333    sqlite3DbFree(db, zMsg);
31334  }else{
31335    pParse->nErr++;
31336    sqlite3DbFree(db, pParse->zErrMsg);
31337    pParse->zErrMsg = zMsg;
31338    pParse->rc = SQLITE_ERROR;
31339    pParse->pWith = 0;
31340  }
31341}
31342
31343/*
31344** If database connection db is currently parsing SQL, then transfer
31345** error code errCode to that parser if the parser has not already
31346** encountered some other kind of error.
31347*/
31348SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3 *db, int errCode){
31349  Parse *pParse;
31350  if( db==0 || (pParse = db->pParse)==0 ) return errCode;
31351  pParse->rc = errCode;
31352  pParse->nErr++;
31353  return errCode;
31354}
31355
31356/*
31357** Convert an SQL-style quoted string into a normal string by removing
31358** the quote characters.  The conversion is done in-place.  If the
31359** input does not begin with a quote character, then this routine
31360** is a no-op.
31361**
31362** The input string must be zero-terminated.  A new zero-terminator
31363** is added to the dequoted string.
31364**
31365** The return value is -1 if no dequoting occurs or the length of the
31366** dequoted string, exclusive of the zero terminator, if dequoting does
31367** occur.
31368**
31369** 2002-02-14: This routine is extended to remove MS-Access style
31370** brackets from around identifiers.  For example:  "[a-b-c]" becomes
31371** "a-b-c".
31372*/
31373SQLITE_PRIVATE void sqlite3Dequote(char *z){
31374  char quote;
31375  int i, j;
31376  if( z==0 ) return;
31377  quote = z[0];
31378  if( !sqlite3Isquote(quote) ) return;
31379  if( quote=='[' ) quote = ']';
31380  for(i=1, j=0;; i++){
31381    assert( z[i] );
31382    if( z[i]==quote ){
31383      if( z[i+1]==quote ){
31384        z[j++] = quote;
31385        i++;
31386      }else{
31387        break;
31388      }
31389    }else{
31390      z[j++] = z[i];
31391    }
31392  }
31393  z[j] = 0;
31394}
31395SQLITE_PRIVATE void sqlite3DequoteExpr(Expr *p){
31396  assert( sqlite3Isquote(p->u.zToken[0]) );
31397  p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted;
31398  sqlite3Dequote(p->u.zToken);
31399}
31400
31401/*
31402** Generate a Token object from a string
31403*/
31404SQLITE_PRIVATE void sqlite3TokenInit(Token *p, char *z){
31405  p->z = z;
31406  p->n = sqlite3Strlen30(z);
31407}
31408
31409/* Convenient short-hand */
31410#define UpperToLower sqlite3UpperToLower
31411
31412/*
31413** Some systems have stricmp().  Others have strcasecmp().  Because
31414** there is no consistency, we will define our own.
31415**
31416** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
31417** sqlite3_strnicmp() APIs allow applications and extensions to compare
31418** the contents of two buffers containing UTF-8 strings in a
31419** case-independent fashion, using the same definition of "case
31420** independence" that SQLite uses internally when comparing identifiers.
31421*/
31422SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
31423  if( zLeft==0 ){
31424    return zRight ? -1 : 0;
31425  }else if( zRight==0 ){
31426    return 1;
31427  }
31428  return sqlite3StrICmp(zLeft, zRight);
31429}
31430SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
31431  unsigned char *a, *b;
31432  int c, x;
31433  a = (unsigned char *)zLeft;
31434  b = (unsigned char *)zRight;
31435  for(;;){
31436    c = *a;
31437    x = *b;
31438    if( c==x ){
31439      if( c==0 ) break;
31440    }else{
31441      c = (int)UpperToLower[c] - (int)UpperToLower[x];
31442      if( c ) break;
31443    }
31444    a++;
31445    b++;
31446  }
31447  return c;
31448}
31449SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
31450  register unsigned char *a, *b;
31451  if( zLeft==0 ){
31452    return zRight ? -1 : 0;
31453  }else if( zRight==0 ){
31454    return 1;
31455  }
31456  a = (unsigned char *)zLeft;
31457  b = (unsigned char *)zRight;
31458  while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
31459  return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
31460}
31461
31462/*
31463** Compute an 8-bit hash on a string that is insensitive to case differences
31464*/
31465SQLITE_PRIVATE u8 sqlite3StrIHash(const char *z){
31466  u8 h = 0;
31467  if( z==0 ) return 0;
31468  while( z[0] ){
31469    h += UpperToLower[(unsigned char)z[0]];
31470    z++;
31471  }
31472  return h;
31473}
31474
31475/*
31476** Compute 10 to the E-th power.  Examples:  E==1 results in 10.
31477** E==2 results in 100.  E==50 results in 1.0e50.
31478**
31479** This routine only works for values of E between 1 and 341.
31480*/
31481static LONGDOUBLE_TYPE sqlite3Pow10(int E){
31482#if defined(_MSC_VER)
31483  static const LONGDOUBLE_TYPE x[] = {
31484    1.0e+001L,
31485    1.0e+002L,
31486    1.0e+004L,
31487    1.0e+008L,
31488    1.0e+016L,
31489    1.0e+032L,
31490    1.0e+064L,
31491    1.0e+128L,
31492    1.0e+256L
31493  };
31494  LONGDOUBLE_TYPE r = 1.0;
31495  int i;
31496  assert( E>=0 && E<=307 );
31497  for(i=0; E!=0; i++, E >>=1){
31498    if( E & 1 ) r *= x[i];
31499  }
31500  return r;
31501#else
31502  LONGDOUBLE_TYPE x = 10.0;
31503  LONGDOUBLE_TYPE r = 1.0;
31504  while(1){
31505    if( E & 1 ) r *= x;
31506    E >>= 1;
31507    if( E==0 ) break;
31508    x *= x;
31509  }
31510  return r;
31511#endif
31512}
31513
31514/*
31515** The string z[] is an text representation of a real number.
31516** Convert this string to a double and write it into *pResult.
31517**
31518** The string z[] is length bytes in length (bytes, not characters) and
31519** uses the encoding enc.  The string is not necessarily zero-terminated.
31520**
31521** Return TRUE if the result is a valid real number (or integer) and FALSE
31522** if the string is empty or contains extraneous text.  More specifically
31523** return
31524**      1          =>  The input string is a pure integer
31525**      2 or more  =>  The input has a decimal point or eNNN clause
31526**      0 or less  =>  The input string is not a valid number
31527**     -1          =>  Not a valid number, but has a valid prefix which
31528**                     includes a decimal point and/or an eNNN clause
31529**
31530** Valid numbers are in one of these formats:
31531**
31532**    [+-]digits[E[+-]digits]
31533**    [+-]digits.[digits][E[+-]digits]
31534**    [+-].digits[E[+-]digits]
31535**
31536** Leading and trailing whitespace is ignored for the purpose of determining
31537** validity.
31538**
31539** If some prefix of the input string is a valid number, this routine
31540** returns FALSE but it still converts the prefix and writes the result
31541** into *pResult.
31542*/
31543#if defined(_MSC_VER)
31544#pragma warning(disable : 4756)
31545#endif
31546SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
31547#ifndef SQLITE_OMIT_FLOATING_POINT
31548  int incr;
31549  const char *zEnd;
31550  /* sign * significand * (10 ^ (esign * exponent)) */
31551  int sign = 1;    /* sign of significand */
31552  i64 s = 0;       /* significand */
31553  int d = 0;       /* adjust exponent for shifting decimal point */
31554  int esign = 1;   /* sign of exponent */
31555  int e = 0;       /* exponent */
31556  int eValid = 1;  /* True exponent is either not used or is well-formed */
31557  double result;
31558  int nDigit = 0;  /* Number of digits processed */
31559  int eType = 1;   /* 1: pure integer,  2+: fractional  -1 or less: bad UTF16 */
31560
31561  assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
31562  *pResult = 0.0;   /* Default return value, in case of an error */
31563  if( length==0 ) return 0;
31564
31565  if( enc==SQLITE_UTF8 ){
31566    incr = 1;
31567    zEnd = z + length;
31568  }else{
31569    int i;
31570    incr = 2;
31571    length &= ~1;
31572    assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
31573    testcase( enc==SQLITE_UTF16LE );
31574    testcase( enc==SQLITE_UTF16BE );
31575    for(i=3-enc; i<length && z[i]==0; i+=2){}
31576    if( i<length ) eType = -100;
31577    zEnd = &z[i^1];
31578    z += (enc&1);
31579  }
31580
31581  /* skip leading spaces */
31582  while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
31583  if( z>=zEnd ) return 0;
31584
31585  /* get sign of significand */
31586  if( *z=='-' ){
31587    sign = -1;
31588    z+=incr;
31589  }else if( *z=='+' ){
31590    z+=incr;
31591  }
31592
31593  /* copy max significant digits to significand */
31594  while( z<zEnd && sqlite3Isdigit(*z) ){
31595    s = s*10 + (*z - '0');
31596    z+=incr; nDigit++;
31597    if( s>=((LARGEST_INT64-9)/10) ){
31598      /* skip non-significant significand digits
31599      ** (increase exponent by d to shift decimal left) */
31600      while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; d++; }
31601    }
31602  }
31603  if( z>=zEnd ) goto do_atof_calc;
31604
31605  /* if decimal point is present */
31606  if( *z=='.' ){
31607    z+=incr;
31608    eType++;
31609    /* copy digits from after decimal to significand
31610    ** (decrease exponent by d to shift decimal right) */
31611    while( z<zEnd && sqlite3Isdigit(*z) ){
31612      if( s<((LARGEST_INT64-9)/10) ){
31613        s = s*10 + (*z - '0');
31614        d--;
31615        nDigit++;
31616      }
31617      z+=incr;
31618    }
31619  }
31620  if( z>=zEnd ) goto do_atof_calc;
31621
31622  /* if exponent is present */
31623  if( *z=='e' || *z=='E' ){
31624    z+=incr;
31625    eValid = 0;
31626    eType++;
31627
31628    /* This branch is needed to avoid a (harmless) buffer overread.  The
31629    ** special comment alerts the mutation tester that the correct answer
31630    ** is obtained even if the branch is omitted */
31631    if( z>=zEnd ) goto do_atof_calc;              /*PREVENTS-HARMLESS-OVERREAD*/
31632
31633    /* get sign of exponent */
31634    if( *z=='-' ){
31635      esign = -1;
31636      z+=incr;
31637    }else if( *z=='+' ){
31638      z+=incr;
31639    }
31640    /* copy digits to exponent */
31641    while( z<zEnd && sqlite3Isdigit(*z) ){
31642      e = e<10000 ? (e*10 + (*z - '0')) : 10000;
31643      z+=incr;
31644      eValid = 1;
31645    }
31646  }
31647
31648  /* skip trailing spaces */
31649  while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
31650
31651do_atof_calc:
31652  /* adjust exponent by d, and update sign */
31653  e = (e*esign) + d;
31654  if( e<0 ) {
31655    esign = -1;
31656    e *= -1;
31657  } else {
31658    esign = 1;
31659  }
31660
31661  if( s==0 ) {
31662    /* In the IEEE 754 standard, zero is signed. */
31663    result = sign<0 ? -(double)0 : (double)0;
31664  } else {
31665    /* Attempt to reduce exponent.
31666    **
31667    ** Branches that are not required for the correct answer but which only
31668    ** help to obtain the correct answer faster are marked with special
31669    ** comments, as a hint to the mutation tester.
31670    */
31671    while( e>0 ){                                       /*OPTIMIZATION-IF-TRUE*/
31672      if( esign>0 ){
31673        if( s>=(LARGEST_INT64/10) ) break;             /*OPTIMIZATION-IF-FALSE*/
31674        s *= 10;
31675      }else{
31676        if( s%10!=0 ) break;                           /*OPTIMIZATION-IF-FALSE*/
31677        s /= 10;
31678      }
31679      e--;
31680    }
31681
31682    /* adjust the sign of significand */
31683    s = sign<0 ? -s : s;
31684
31685    if( e==0 ){                                         /*OPTIMIZATION-IF-TRUE*/
31686      result = (double)s;
31687    }else{
31688      /* attempt to handle extremely small/large numbers better */
31689      if( e>307 ){                                      /*OPTIMIZATION-IF-TRUE*/
31690        if( e<342 ){                                    /*OPTIMIZATION-IF-TRUE*/
31691          LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308);
31692          if( esign<0 ){
31693            result = s / scale;
31694            result /= 1.0e+308;
31695          }else{
31696            result = s * scale;
31697            result *= 1.0e+308;
31698          }
31699        }else{ assert( e>=342 );
31700          if( esign<0 ){
31701            result = 0.0*s;
31702          }else{
31703#ifdef INFINITY
31704            result = INFINITY*s;
31705#else
31706            result = 1e308*1e308*s;  /* Infinity */
31707#endif
31708          }
31709        }
31710      }else{
31711        LONGDOUBLE_TYPE scale = sqlite3Pow10(e);
31712        if( esign<0 ){
31713          result = s / scale;
31714        }else{
31715          result = s * scale;
31716        }
31717      }
31718    }
31719  }
31720
31721  /* store the result */
31722  *pResult = result;
31723
31724  /* return true if number and no extra non-whitespace chracters after */
31725  if( z==zEnd && nDigit>0 && eValid && eType>0 ){
31726    return eType;
31727  }else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){
31728    return -1;
31729  }else{
31730    return 0;
31731  }
31732#else
31733  return !sqlite3Atoi64(z, pResult, length, enc);
31734#endif /* SQLITE_OMIT_FLOATING_POINT */
31735}
31736#if defined(_MSC_VER)
31737#pragma warning(default : 4756)
31738#endif
31739
31740/*
31741** Compare the 19-character string zNum against the text representation
31742** value 2^63:  9223372036854775808.  Return negative, zero, or positive
31743** if zNum is less than, equal to, or greater than the string.
31744** Note that zNum must contain exactly 19 characters.
31745**
31746** Unlike memcmp() this routine is guaranteed to return the difference
31747** in the values of the last digit if the only difference is in the
31748** last digit.  So, for example,
31749**
31750**      compare2pow63("9223372036854775800", 1)
31751**
31752** will return -8.
31753*/
31754static int compare2pow63(const char *zNum, int incr){
31755  int c = 0;
31756  int i;
31757                    /* 012345678901234567 */
31758  const char *pow63 = "922337203685477580";
31759  for(i=0; c==0 && i<18; i++){
31760    c = (zNum[i*incr]-pow63[i])*10;
31761  }
31762  if( c==0 ){
31763    c = zNum[18*incr] - '8';
31764    testcase( c==(-1) );
31765    testcase( c==0 );
31766    testcase( c==(+1) );
31767  }
31768  return c;
31769}
31770
31771/*
31772** Convert zNum to a 64-bit signed integer.  zNum must be decimal. This
31773** routine does *not* accept hexadecimal notation.
31774**
31775** Returns:
31776**
31777**    -1    Not even a prefix of the input text looks like an integer
31778**     0    Successful transformation.  Fits in a 64-bit signed integer.
31779**     1    Excess non-space text after the integer value
31780**     2    Integer too large for a 64-bit signed integer or is malformed
31781**     3    Special case of 9223372036854775808
31782**
31783** length is the number of bytes in the string (bytes, not characters).
31784** The string is not necessarily zero-terminated.  The encoding is
31785** given by enc.
31786*/
31787SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
31788  int incr;
31789  u64 u = 0;
31790  int neg = 0; /* assume positive */
31791  int i;
31792  int c = 0;
31793  int nonNum = 0;  /* True if input contains UTF16 with high byte non-zero */
31794  int rc;          /* Baseline return code */
31795  const char *zStart;
31796  const char *zEnd = zNum + length;
31797  assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
31798  if( enc==SQLITE_UTF8 ){
31799    incr = 1;
31800  }else{
31801    incr = 2;
31802    assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
31803    for(i=3-enc; i<length && zNum[i]==0; i+=2){}
31804    nonNum = i<length;
31805    zEnd = &zNum[i^1];
31806    zNum += (enc&1);
31807  }
31808  while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
31809  if( zNum<zEnd ){
31810    if( *zNum=='-' ){
31811      neg = 1;
31812      zNum+=incr;
31813    }else if( *zNum=='+' ){
31814      zNum+=incr;
31815    }
31816  }
31817  zStart = zNum;
31818  while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
31819  for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
31820    u = u*10 + c - '0';
31821  }
31822  testcase( i==18*incr );
31823  testcase( i==19*incr );
31824  testcase( i==20*incr );
31825  if( u>LARGEST_INT64 ){
31826    /* This test and assignment is needed only to suppress UB warnings
31827    ** from clang and -fsanitize=undefined.  This test and assignment make
31828    ** the code a little larger and slower, and no harm comes from omitting
31829    ** them, but we must appaise the undefined-behavior pharisees. */
31830    *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
31831  }else if( neg ){
31832    *pNum = -(i64)u;
31833  }else{
31834    *pNum = (i64)u;
31835  }
31836  rc = 0;
31837  if( i==0 && zStart==zNum ){    /* No digits */
31838    rc = -1;
31839  }else if( nonNum ){            /* UTF16 with high-order bytes non-zero */
31840    rc = 1;
31841  }else if( &zNum[i]<zEnd ){     /* Extra bytes at the end */
31842    int jj = i;
31843    do{
31844      if( !sqlite3Isspace(zNum[jj]) ){
31845        rc = 1;          /* Extra non-space text after the integer */
31846        break;
31847      }
31848      jj += incr;
31849    }while( &zNum[jj]<zEnd );
31850  }
31851  if( i<19*incr ){
31852    /* Less than 19 digits, so we know that it fits in 64 bits */
31853    assert( u<=LARGEST_INT64 );
31854    return rc;
31855  }else{
31856    /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
31857    c = i>19*incr ? 1 : compare2pow63(zNum, incr);
31858    if( c<0 ){
31859      /* zNum is less than 9223372036854775808 so it fits */
31860      assert( u<=LARGEST_INT64 );
31861      return rc;
31862    }else{
31863      *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
31864      if( c>0 ){
31865        /* zNum is greater than 9223372036854775808 so it overflows */
31866        return 2;
31867      }else{
31868        /* zNum is exactly 9223372036854775808.  Fits if negative.  The
31869        ** special case 2 overflow if positive */
31870        assert( u-1==LARGEST_INT64 );
31871        return neg ? rc : 3;
31872      }
31873    }
31874  }
31875}
31876
31877/*
31878** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
31879** into a 64-bit signed integer.  This routine accepts hexadecimal literals,
31880** whereas sqlite3Atoi64() does not.
31881**
31882** Returns:
31883**
31884**     0    Successful transformation.  Fits in a 64-bit signed integer.
31885**     1    Excess text after the integer value
31886**     2    Integer too large for a 64-bit signed integer or is malformed
31887**     3    Special case of 9223372036854775808
31888*/
31889SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
31890#ifndef SQLITE_OMIT_HEX_INTEGER
31891  if( z[0]=='0'
31892   && (z[1]=='x' || z[1]=='X')
31893  ){
31894    u64 u = 0;
31895    int i, k;
31896    for(i=2; z[i]=='0'; i++){}
31897    for(k=i; sqlite3Isxdigit(z[k]); k++){
31898      u = u*16 + sqlite3HexToInt(z[k]);
31899    }
31900    memcpy(pOut, &u, 8);
31901    return (z[k]==0 && k-i<=16) ? 0 : 2;
31902  }else
31903#endif /* SQLITE_OMIT_HEX_INTEGER */
31904  {
31905    return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8);
31906  }
31907}
31908
31909/*
31910** If zNum represents an integer that will fit in 32-bits, then set
31911** *pValue to that integer and return true.  Otherwise return false.
31912**
31913** This routine accepts both decimal and hexadecimal notation for integers.
31914**
31915** Any non-numeric characters that following zNum are ignored.
31916** This is different from sqlite3Atoi64() which requires the
31917** input number to be zero-terminated.
31918*/
31919SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
31920  sqlite_int64 v = 0;
31921  int i, c;
31922  int neg = 0;
31923  if( zNum[0]=='-' ){
31924    neg = 1;
31925    zNum++;
31926  }else if( zNum[0]=='+' ){
31927    zNum++;
31928  }
31929#ifndef SQLITE_OMIT_HEX_INTEGER
31930  else if( zNum[0]=='0'
31931        && (zNum[1]=='x' || zNum[1]=='X')
31932        && sqlite3Isxdigit(zNum[2])
31933  ){
31934    u32 u = 0;
31935    zNum += 2;
31936    while( zNum[0]=='0' ) zNum++;
31937    for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){
31938      u = u*16 + sqlite3HexToInt(zNum[i]);
31939    }
31940    if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){
31941      memcpy(pValue, &u, 4);
31942      return 1;
31943    }else{
31944      return 0;
31945    }
31946  }
31947#endif
31948  if( !sqlite3Isdigit(zNum[0]) ) return 0;
31949  while( zNum[0]=='0' ) zNum++;
31950  for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
31951    v = v*10 + c;
31952  }
31953
31954  /* The longest decimal representation of a 32 bit integer is 10 digits:
31955  **
31956  **             1234567890
31957  **     2^31 -> 2147483648
31958  */
31959  testcase( i==10 );
31960  if( i>10 ){
31961    return 0;
31962  }
31963  testcase( v-neg==2147483647 );
31964  if( v-neg>2147483647 ){
31965    return 0;
31966  }
31967  if( neg ){
31968    v = -v;
31969  }
31970  *pValue = (int)v;
31971  return 1;
31972}
31973
31974/*
31975** Return a 32-bit integer value extracted from a string.  If the
31976** string is not an integer, just return 0.
31977*/
31978SQLITE_PRIVATE int sqlite3Atoi(const char *z){
31979  int x = 0;
31980  if( z ) sqlite3GetInt32(z, &x);
31981  return x;
31982}
31983
31984/*
31985** The variable-length integer encoding is as follows:
31986**
31987** KEY:
31988**         A = 0xxxxxxx    7 bits of data and one flag bit
31989**         B = 1xxxxxxx    7 bits of data and one flag bit
31990**         C = xxxxxxxx    8 bits of data
31991**
31992**  7 bits - A
31993** 14 bits - BA
31994** 21 bits - BBA
31995** 28 bits - BBBA
31996** 35 bits - BBBBA
31997** 42 bits - BBBBBA
31998** 49 bits - BBBBBBA
31999** 56 bits - BBBBBBBA
32000** 64 bits - BBBBBBBBC
32001*/
32002
32003/*
32004** Write a 64-bit variable-length integer to memory starting at p[0].
32005** The length of data write will be between 1 and 9 bytes.  The number
32006** of bytes written is returned.
32007**
32008** A variable-length integer consists of the lower 7 bits of each byte
32009** for all bytes that have the 8th bit set and one byte with the 8th
32010** bit clear.  Except, if we get to the 9th byte, it stores the full
32011** 8 bits and is the last byte.
32012*/
32013static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){
32014  int i, j, n;
32015  u8 buf[10];
32016  if( v & (((u64)0xff000000)<<32) ){
32017    p[8] = (u8)v;
32018    v >>= 8;
32019    for(i=7; i>=0; i--){
32020      p[i] = (u8)((v & 0x7f) | 0x80);
32021      v >>= 7;
32022    }
32023    return 9;
32024  }
32025  n = 0;
32026  do{
32027    buf[n++] = (u8)((v & 0x7f) | 0x80);
32028    v >>= 7;
32029  }while( v!=0 );
32030  buf[0] &= 0x7f;
32031  assert( n<=9 );
32032  for(i=0, j=n-1; j>=0; j--, i++){
32033    p[i] = buf[j];
32034  }
32035  return n;
32036}
32037SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
32038  if( v<=0x7f ){
32039    p[0] = v&0x7f;
32040    return 1;
32041  }
32042  if( v<=0x3fff ){
32043    p[0] = ((v>>7)&0x7f)|0x80;
32044    p[1] = v&0x7f;
32045    return 2;
32046  }
32047  return putVarint64(p,v);
32048}
32049
32050/*
32051** Bitmasks used by sqlite3GetVarint().  These precomputed constants
32052** are defined here rather than simply putting the constant expressions
32053** inline in order to work around bugs in the RVT compiler.
32054**
32055** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
32056**
32057** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
32058*/
32059#define SLOT_2_0     0x001fc07f
32060#define SLOT_4_2_0   0xf01fc07f
32061
32062
32063/*
32064** Read a 64-bit variable-length integer from memory starting at p[0].
32065** Return the number of bytes read.  The value is stored in *v.
32066*/
32067SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
32068  u32 a,b,s;
32069
32070  if( ((signed char*)p)[0]>=0 ){
32071    *v = *p;
32072    return 1;
32073  }
32074  if( ((signed char*)p)[1]>=0 ){
32075    *v = ((u32)(p[0]&0x7f)<<7) | p[1];
32076    return 2;
32077  }
32078
32079  /* Verify that constants are precomputed correctly */
32080  assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
32081  assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
32082
32083  a = ((u32)p[0])<<14;
32084  b = p[1];
32085  p += 2;
32086  a |= *p;
32087  /* a: p0<<14 | p2 (unmasked) */
32088  if (!(a&0x80))
32089  {
32090    a &= SLOT_2_0;
32091    b &= 0x7f;
32092    b = b<<7;
32093    a |= b;
32094    *v = a;
32095    return 3;
32096  }
32097
32098  /* CSE1 from below */
32099  a &= SLOT_2_0;
32100  p++;
32101  b = b<<14;
32102  b |= *p;
32103  /* b: p1<<14 | p3 (unmasked) */
32104  if (!(b&0x80))
32105  {
32106    b &= SLOT_2_0;
32107    /* moved CSE1 up */
32108    /* a &= (0x7f<<14)|(0x7f); */
32109    a = a<<7;
32110    a |= b;
32111    *v = a;
32112    return 4;
32113  }
32114
32115  /* a: p0<<14 | p2 (masked) */
32116  /* b: p1<<14 | p3 (unmasked) */
32117  /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
32118  /* moved CSE1 up */
32119  /* a &= (0x7f<<14)|(0x7f); */
32120  b &= SLOT_2_0;
32121  s = a;
32122  /* s: p0<<14 | p2 (masked) */
32123
32124  p++;
32125  a = a<<14;
32126  a |= *p;
32127  /* a: p0<<28 | p2<<14 | p4 (unmasked) */
32128  if (!(a&0x80))
32129  {
32130    /* we can skip these cause they were (effectively) done above
32131    ** while calculating s */
32132    /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
32133    /* b &= (0x7f<<14)|(0x7f); */
32134    b = b<<7;
32135    a |= b;
32136    s = s>>18;
32137    *v = ((u64)s)<<32 | a;
32138    return 5;
32139  }
32140
32141  /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
32142  s = s<<7;
32143  s |= b;
32144  /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
32145
32146  p++;
32147  b = b<<14;
32148  b |= *p;
32149  /* b: p1<<28 | p3<<14 | p5 (unmasked) */
32150  if (!(b&0x80))
32151  {
32152    /* we can skip this cause it was (effectively) done above in calc'ing s */
32153    /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
32154    a &= SLOT_2_0;
32155    a = a<<7;
32156    a |= b;
32157    s = s>>18;
32158    *v = ((u64)s)<<32 | a;
32159    return 6;
32160  }
32161
32162  p++;
32163  a = a<<14;
32164  a |= *p;
32165  /* a: p2<<28 | p4<<14 | p6 (unmasked) */
32166  if (!(a&0x80))
32167  {
32168    a &= SLOT_4_2_0;
32169    b &= SLOT_2_0;
32170    b = b<<7;
32171    a |= b;
32172    s = s>>11;
32173    *v = ((u64)s)<<32 | a;
32174    return 7;
32175  }
32176
32177  /* CSE2 from below */
32178  a &= SLOT_2_0;
32179  p++;
32180  b = b<<14;
32181  b |= *p;
32182  /* b: p3<<28 | p5<<14 | p7 (unmasked) */
32183  if (!(b&0x80))
32184  {
32185    b &= SLOT_4_2_0;
32186    /* moved CSE2 up */
32187    /* a &= (0x7f<<14)|(0x7f); */
32188    a = a<<7;
32189    a |= b;
32190    s = s>>4;
32191    *v = ((u64)s)<<32 | a;
32192    return 8;
32193  }
32194
32195  p++;
32196  a = a<<15;
32197  a |= *p;
32198  /* a: p4<<29 | p6<<15 | p8 (unmasked) */
32199
32200  /* moved CSE2 up */
32201  /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
32202  b &= SLOT_2_0;
32203  b = b<<8;
32204  a |= b;
32205
32206  s = s<<4;
32207  b = p[-4];
32208  b &= 0x7f;
32209  b = b>>3;
32210  s |= b;
32211
32212  *v = ((u64)s)<<32 | a;
32213
32214  return 9;
32215}
32216
32217/*
32218** Read a 32-bit variable-length integer from memory starting at p[0].
32219** Return the number of bytes read.  The value is stored in *v.
32220**
32221** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
32222** integer, then set *v to 0xffffffff.
32223**
32224** A MACRO version, getVarint32, is provided which inlines the
32225** single-byte case.  All code should use the MACRO version as
32226** this function assumes the single-byte case has already been handled.
32227*/
32228SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
32229  u32 a,b;
32230
32231  /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
32232  ** by the getVarin32() macro */
32233  a = *p;
32234  /* a: p0 (unmasked) */
32235#ifndef getVarint32
32236  if (!(a&0x80))
32237  {
32238    /* Values between 0 and 127 */
32239    *v = a;
32240    return 1;
32241  }
32242#endif
32243
32244  /* The 2-byte case */
32245  p++;
32246  b = *p;
32247  /* b: p1 (unmasked) */
32248  if (!(b&0x80))
32249  {
32250    /* Values between 128 and 16383 */
32251    a &= 0x7f;
32252    a = a<<7;
32253    *v = a | b;
32254    return 2;
32255  }
32256
32257  /* The 3-byte case */
32258  p++;
32259  a = a<<14;
32260  a |= *p;
32261  /* a: p0<<14 | p2 (unmasked) */
32262  if (!(a&0x80))
32263  {
32264    /* Values between 16384 and 2097151 */
32265    a &= (0x7f<<14)|(0x7f);
32266    b &= 0x7f;
32267    b = b<<7;
32268    *v = a | b;
32269    return 3;
32270  }
32271
32272  /* A 32-bit varint is used to store size information in btrees.
32273  ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
32274  ** A 3-byte varint is sufficient, for example, to record the size
32275  ** of a 1048569-byte BLOB or string.
32276  **
32277  ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
32278  ** rare larger cases can be handled by the slower 64-bit varint
32279  ** routine.
32280  */
32281#if 1
32282  {
32283    u64 v64;
32284    u8 n;
32285
32286    p -= 2;
32287    n = sqlite3GetVarint(p, &v64);
32288    assert( n>3 && n<=9 );
32289    if( (v64 & SQLITE_MAX_U32)!=v64 ){
32290      *v = 0xffffffff;
32291    }else{
32292      *v = (u32)v64;
32293    }
32294    return n;
32295  }
32296
32297#else
32298  /* For following code (kept for historical record only) shows an
32299  ** unrolling for the 3- and 4-byte varint cases.  This code is
32300  ** slightly faster, but it is also larger and much harder to test.
32301  */
32302  p++;
32303  b = b<<14;
32304  b |= *p;
32305  /* b: p1<<14 | p3 (unmasked) */
32306  if (!(b&0x80))
32307  {
32308    /* Values between 2097152 and 268435455 */
32309    b &= (0x7f<<14)|(0x7f);
32310    a &= (0x7f<<14)|(0x7f);
32311    a = a<<7;
32312    *v = a | b;
32313    return 4;
32314  }
32315
32316  p++;
32317  a = a<<14;
32318  a |= *p;
32319  /* a: p0<<28 | p2<<14 | p4 (unmasked) */
32320  if (!(a&0x80))
32321  {
32322    /* Values  between 268435456 and 34359738367 */
32323    a &= SLOT_4_2_0;
32324    b &= SLOT_4_2_0;
32325    b = b<<7;
32326    *v = a | b;
32327    return 5;
32328  }
32329
32330  /* We can only reach this point when reading a corrupt database
32331  ** file.  In that case we are not in any hurry.  Use the (relatively
32332  ** slow) general-purpose sqlite3GetVarint() routine to extract the
32333  ** value. */
32334  {
32335    u64 v64;
32336    u8 n;
32337
32338    p -= 4;
32339    n = sqlite3GetVarint(p, &v64);
32340    assert( n>5 && n<=9 );
32341    *v = (u32)v64;
32342    return n;
32343  }
32344#endif
32345}
32346
32347/*
32348** Return the number of bytes that will be needed to store the given
32349** 64-bit integer.
32350*/
32351SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
32352  int i;
32353  for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); }
32354  return i;
32355}
32356
32357
32358/*
32359** Read or write a four-byte big-endian integer value.
32360*/
32361SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
32362#if SQLITE_BYTEORDER==4321
32363  u32 x;
32364  memcpy(&x,p,4);
32365  return x;
32366#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
32367  u32 x;
32368  memcpy(&x,p,4);
32369  return __builtin_bswap32(x);
32370#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
32371  u32 x;
32372  memcpy(&x,p,4);
32373  return _byteswap_ulong(x);
32374#else
32375  testcase( p[0]&0x80 );
32376  return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
32377#endif
32378}
32379SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
32380#if SQLITE_BYTEORDER==4321
32381  memcpy(p,&v,4);
32382#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
32383  u32 x = __builtin_bswap32(v);
32384  memcpy(p,&x,4);
32385#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
32386  u32 x = _byteswap_ulong(v);
32387  memcpy(p,&x,4);
32388#else
32389  p[0] = (u8)(v>>24);
32390  p[1] = (u8)(v>>16);
32391  p[2] = (u8)(v>>8);
32392  p[3] = (u8)v;
32393#endif
32394}
32395
32396
32397
32398/*
32399** Translate a single byte of Hex into an integer.
32400** This routine only works if h really is a valid hexadecimal
32401** character:  0..9a..fA..F
32402*/
32403SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
32404  assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
32405#ifdef SQLITE_ASCII
32406  h += 9*(1&(h>>6));
32407#endif
32408#ifdef SQLITE_EBCDIC
32409  h += 9*(1&~(h>>4));
32410#endif
32411  return (u8)(h & 0xf);
32412}
32413
32414#if !defined(SQLITE_OMIT_BLOB_LITERAL)
32415/*
32416** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
32417** value.  Return a pointer to its binary value.  Space to hold the
32418** binary value has been obtained from malloc and must be freed by
32419** the calling routine.
32420*/
32421SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
32422  char *zBlob;
32423  int i;
32424
32425  zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1);
32426  n--;
32427  if( zBlob ){
32428    for(i=0; i<n; i+=2){
32429      zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
32430    }
32431    zBlob[i/2] = 0;
32432  }
32433  return zBlob;
32434}
32435#endif /* !SQLITE_OMIT_BLOB_LITERAL */
32436
32437/*
32438** Log an error that is an API call on a connection pointer that should
32439** not have been used.  The "type" of connection pointer is given as the
32440** argument.  The zType is a word like "NULL" or "closed" or "invalid".
32441*/
32442static void logBadConnection(const char *zType){
32443  sqlite3_log(SQLITE_MISUSE,
32444     "API call with %s database connection pointer",
32445     zType
32446  );
32447}
32448
32449/*
32450** Check to make sure we have a valid db pointer.  This test is not
32451** foolproof but it does provide some measure of protection against
32452** misuse of the interface such as passing in db pointers that are
32453** NULL or which have been previously closed.  If this routine returns
32454** 1 it means that the db pointer is valid and 0 if it should not be
32455** dereferenced for any reason.  The calling function should invoke
32456** SQLITE_MISUSE immediately.
32457**
32458** sqlite3SafetyCheckOk() requires that the db pointer be valid for
32459** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
32460** open properly and is not fit for general use but which can be
32461** used as an argument to sqlite3_errmsg() or sqlite3_close().
32462*/
32463SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
32464  u32 magic;
32465  if( db==0 ){
32466    logBadConnection("NULL");
32467    return 0;
32468  }
32469  magic = db->magic;
32470  if( magic!=SQLITE_MAGIC_OPEN ){
32471    if( sqlite3SafetyCheckSickOrOk(db) ){
32472      testcase( sqlite3GlobalConfig.xLog!=0 );
32473      logBadConnection("unopened");
32474    }
32475    return 0;
32476  }else{
32477    return 1;
32478  }
32479}
32480SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
32481  u32 magic;
32482  magic = db->magic;
32483  if( magic!=SQLITE_MAGIC_SICK &&
32484      magic!=SQLITE_MAGIC_OPEN &&
32485      magic!=SQLITE_MAGIC_BUSY ){
32486    testcase( sqlite3GlobalConfig.xLog!=0 );
32487    logBadConnection("invalid");
32488    return 0;
32489  }else{
32490    return 1;
32491  }
32492}
32493
32494/*
32495** Attempt to add, substract, or multiply the 64-bit signed value iB against
32496** the other 64-bit signed integer at *pA and store the result in *pA.
32497** Return 0 on success.  Or if the operation would have resulted in an
32498** overflow, leave *pA unchanged and return 1.
32499*/
32500SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
32501#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
32502  return __builtin_add_overflow(*pA, iB, pA);
32503#else
32504  i64 iA = *pA;
32505  testcase( iA==0 ); testcase( iA==1 );
32506  testcase( iB==-1 ); testcase( iB==0 );
32507  if( iB>=0 ){
32508    testcase( iA>0 && LARGEST_INT64 - iA == iB );
32509    testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
32510    if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
32511  }else{
32512    testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
32513    testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
32514    if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
32515  }
32516  *pA += iB;
32517  return 0;
32518#endif
32519}
32520SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
32521#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
32522  return __builtin_sub_overflow(*pA, iB, pA);
32523#else
32524  testcase( iB==SMALLEST_INT64+1 );
32525  if( iB==SMALLEST_INT64 ){
32526    testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
32527    if( (*pA)>=0 ) return 1;
32528    *pA -= iB;
32529    return 0;
32530  }else{
32531    return sqlite3AddInt64(pA, -iB);
32532  }
32533#endif
32534}
32535SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
32536#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
32537  return __builtin_mul_overflow(*pA, iB, pA);
32538#else
32539  i64 iA = *pA;
32540  if( iB>0 ){
32541    if( iA>LARGEST_INT64/iB ) return 1;
32542    if( iA<SMALLEST_INT64/iB ) return 1;
32543  }else if( iB<0 ){
32544    if( iA>0 ){
32545      if( iB<SMALLEST_INT64/iA ) return 1;
32546    }else if( iA<0 ){
32547      if( iB==SMALLEST_INT64 ) return 1;
32548      if( iA==SMALLEST_INT64 ) return 1;
32549      if( -iA>LARGEST_INT64/-iB ) return 1;
32550    }
32551  }
32552  *pA = iA*iB;
32553  return 0;
32554#endif
32555}
32556
32557/*
32558** Compute the absolute value of a 32-bit signed integer, of possible.  Or
32559** if the integer has a value of -2147483648, return +2147483647
32560*/
32561SQLITE_PRIVATE int sqlite3AbsInt32(int x){
32562  if( x>=0 ) return x;
32563  if( x==(int)0x80000000 ) return 0x7fffffff;
32564  return -x;
32565}
32566
32567#ifdef SQLITE_ENABLE_8_3_NAMES
32568/*
32569** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
32570** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
32571** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
32572** three characters, then shorten the suffix on z[] to be the last three
32573** characters of the original suffix.
32574**
32575** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
32576** do the suffix shortening regardless of URI parameter.
32577**
32578** Examples:
32579**
32580**     test.db-journal    =>   test.nal
32581**     test.db-wal        =>   test.wal
32582**     test.db-shm        =>   test.shm
32583**     test.db-mj7f3319fa =>   test.9fa
32584*/
32585SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
32586#if SQLITE_ENABLE_8_3_NAMES<2
32587  if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
32588#endif
32589  {
32590    int i, sz;
32591    sz = sqlite3Strlen30(z);
32592    for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
32593    if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
32594  }
32595}
32596#endif
32597
32598/*
32599** Find (an approximate) sum of two LogEst values.  This computation is
32600** not a simple "+" operator because LogEst is stored as a logarithmic
32601** value.
32602**
32603*/
32604SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
32605  static const unsigned char x[] = {
32606     10, 10,                         /* 0,1 */
32607      9, 9,                          /* 2,3 */
32608      8, 8,                          /* 4,5 */
32609      7, 7, 7,                       /* 6,7,8 */
32610      6, 6, 6,                       /* 9,10,11 */
32611      5, 5, 5,                       /* 12-14 */
32612      4, 4, 4, 4,                    /* 15-18 */
32613      3, 3, 3, 3, 3, 3,              /* 19-24 */
32614      2, 2, 2, 2, 2, 2, 2,           /* 25-31 */
32615  };
32616  if( a>=b ){
32617    if( a>b+49 ) return a;
32618    if( a>b+31 ) return a+1;
32619    return a+x[a-b];
32620  }else{
32621    if( b>a+49 ) return b;
32622    if( b>a+31 ) return b+1;
32623    return b+x[b-a];
32624  }
32625}
32626
32627/*
32628** Convert an integer into a LogEst.  In other words, compute an
32629** approximation for 10*log2(x).
32630*/
32631SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
32632  static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
32633  LogEst y = 40;
32634  if( x<8 ){
32635    if( x<2 ) return 0;
32636    while( x<8 ){  y -= 10; x <<= 1; }
32637  }else{
32638#if GCC_VERSION>=5004000
32639    int i = 60 - __builtin_clzll(x);
32640    y += i*10;
32641    x >>= i;
32642#else
32643    while( x>255 ){ y += 40; x >>= 4; }  /*OPTIMIZATION-IF-TRUE*/
32644    while( x>15 ){  y += 10; x >>= 1; }
32645#endif
32646  }
32647  return a[x&7] + y - 10;
32648}
32649
32650#ifndef SQLITE_OMIT_VIRTUALTABLE
32651/*
32652** Convert a double into a LogEst
32653** In other words, compute an approximation for 10*log2(x).
32654*/
32655SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){
32656  u64 a;
32657  LogEst e;
32658  assert( sizeof(x)==8 && sizeof(a)==8 );
32659  if( x<=1 ) return 0;
32660  if( x<=2000000000 ) return sqlite3LogEst((u64)x);
32661  memcpy(&a, &x, 8);
32662  e = (a>>52) - 1022;
32663  return e*10;
32664}
32665#endif /* SQLITE_OMIT_VIRTUALTABLE */
32666
32667#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
32668    defined(SQLITE_ENABLE_STAT4) || \
32669    defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
32670/*
32671** Convert a LogEst into an integer.
32672**
32673** Note that this routine is only used when one or more of various
32674** non-standard compile-time options is enabled.
32675*/
32676SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
32677  u64 n;
32678  n = x%10;
32679  x /= 10;
32680  if( n>=5 ) n -= 2;
32681  else if( n>=1 ) n -= 1;
32682#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
32683    defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
32684  if( x>60 ) return (u64)LARGEST_INT64;
32685#else
32686  /* If only SQLITE_ENABLE_STAT4 is on, then the largest input
32687  ** possible to this routine is 310, resulting in a maximum x of 31 */
32688  assert( x<=60 );
32689#endif
32690  return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
32691}
32692#endif /* defined SCANSTAT or STAT4 or ESTIMATED_ROWS */
32693
32694/*
32695** Add a new name/number pair to a VList.  This might require that the
32696** VList object be reallocated, so return the new VList.  If an OOM
32697** error occurs, the original VList returned and the
32698** db->mallocFailed flag is set.
32699**
32700** A VList is really just an array of integers.  To destroy a VList,
32701** simply pass it to sqlite3DbFree().
32702**
32703** The first integer is the number of integers allocated for the whole
32704** VList.  The second integer is the number of integers actually used.
32705** Each name/number pair is encoded by subsequent groups of 3 or more
32706** integers.
32707**
32708** Each name/number pair starts with two integers which are the numeric
32709** value for the pair and the size of the name/number pair, respectively.
32710** The text name overlays one or more following integers.  The text name
32711** is always zero-terminated.
32712**
32713** Conceptually:
32714**
32715**    struct VList {
32716**      int nAlloc;   // Number of allocated slots
32717**      int nUsed;    // Number of used slots
32718**      struct VListEntry {
32719**        int iValue;    // Value for this entry
32720**        int nSlot;     // Slots used by this entry
32721**        // ... variable name goes here
32722**      } a[0];
32723**    }
32724**
32725** During code generation, pointers to the variable names within the
32726** VList are taken.  When that happens, nAlloc is set to zero as an
32727** indication that the VList may never again be enlarged, since the
32728** accompanying realloc() would invalidate the pointers.
32729*/
32730SQLITE_PRIVATE VList *sqlite3VListAdd(
32731  sqlite3 *db,           /* The database connection used for malloc() */
32732  VList *pIn,            /* The input VList.  Might be NULL */
32733  const char *zName,     /* Name of symbol to add */
32734  int nName,             /* Bytes of text in zName */
32735  int iVal               /* Value to associate with zName */
32736){
32737  int nInt;              /* number of sizeof(int) objects needed for zName */
32738  char *z;               /* Pointer to where zName will be stored */
32739  int i;                 /* Index in pIn[] where zName is stored */
32740
32741  nInt = nName/4 + 3;
32742  assert( pIn==0 || pIn[0]>=3 );  /* Verify ok to add new elements */
32743  if( pIn==0 || pIn[1]+nInt > pIn[0] ){
32744    /* Enlarge the allocation */
32745    sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt;
32746    VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
32747    if( pOut==0 ) return pIn;
32748    if( pIn==0 ) pOut[1] = 2;
32749    pIn = pOut;
32750    pIn[0] = nAlloc;
32751  }
32752  i = pIn[1];
32753  pIn[i] = iVal;
32754  pIn[i+1] = nInt;
32755  z = (char*)&pIn[i+2];
32756  pIn[1] = i+nInt;
32757  assert( pIn[1]<=pIn[0] );
32758  memcpy(z, zName, nName);
32759  z[nName] = 0;
32760  return pIn;
32761}
32762
32763/*
32764** Return a pointer to the name of a variable in the given VList that
32765** has the value iVal.  Or return a NULL if there is no such variable in
32766** the list
32767*/
32768SQLITE_PRIVATE const char *sqlite3VListNumToName(VList *pIn, int iVal){
32769  int i, mx;
32770  if( pIn==0 ) return 0;
32771  mx = pIn[1];
32772  i = 2;
32773  do{
32774    if( pIn[i]==iVal ) return (char*)&pIn[i+2];
32775    i += pIn[i+1];
32776  }while( i<mx );
32777  return 0;
32778}
32779
32780/*
32781** Return the number of the variable named zName, if it is in VList.
32782** or return 0 if there is no such variable.
32783*/
32784SQLITE_PRIVATE int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){
32785  int i, mx;
32786  if( pIn==0 ) return 0;
32787  mx = pIn[1];
32788  i = 2;
32789  do{
32790    const char *z = (const char*)&pIn[i+2];
32791    if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i];
32792    i += pIn[i+1];
32793  }while( i<mx );
32794  return 0;
32795}
32796
32797/************** End of util.c ************************************************/
32798/************** Begin file hash.c ********************************************/
32799/*
32800** 2001 September 22
32801**
32802** The author disclaims copyright to this source code.  In place of
32803** a legal notice, here is a blessing:
32804**
32805**    May you do good and not evil.
32806**    May you find forgiveness for yourself and forgive others.
32807**    May you share freely, never taking more than you give.
32808**
32809*************************************************************************
32810** This is the implementation of generic hash-tables
32811** used in SQLite.
32812*/
32813/* #include "sqliteInt.h" */
32814/* #include <assert.h> */
32815
32816/* Turn bulk memory into a hash table object by initializing the
32817** fields of the Hash structure.
32818**
32819** "pNew" is a pointer to the hash table that is to be initialized.
32820*/
32821SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
32822  assert( pNew!=0 );
32823  pNew->first = 0;
32824  pNew->count = 0;
32825  pNew->htsize = 0;
32826  pNew->ht = 0;
32827}
32828
32829/* Remove all entries from a hash table.  Reclaim all memory.
32830** Call this routine to delete a hash table or to reset a hash table
32831** to the empty state.
32832*/
32833SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
32834  HashElem *elem;         /* For looping over all elements of the table */
32835
32836  assert( pH!=0 );
32837  elem = pH->first;
32838  pH->first = 0;
32839  sqlite3_free(pH->ht);
32840  pH->ht = 0;
32841  pH->htsize = 0;
32842  while( elem ){
32843    HashElem *next_elem = elem->next;
32844    sqlite3_free(elem);
32845    elem = next_elem;
32846  }
32847  pH->count = 0;
32848}
32849
32850/*
32851** The hashing function.
32852*/
32853static unsigned int strHash(const char *z){
32854  unsigned int h = 0;
32855  unsigned char c;
32856  while( (c = (unsigned char)*z++)!=0 ){     /*OPTIMIZATION-IF-TRUE*/
32857    /* Knuth multiplicative hashing.  (Sorting & Searching, p. 510).
32858    ** 0x9e3779b1 is 2654435761 which is the closest prime number to
32859    ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
32860    h += sqlite3UpperToLower[c];
32861    h *= 0x9e3779b1;
32862  }
32863  return h;
32864}
32865
32866
32867/* Link pNew element into the hash table pH.  If pEntry!=0 then also
32868** insert pNew into the pEntry hash bucket.
32869*/
32870static void insertElement(
32871  Hash *pH,              /* The complete hash table */
32872  struct _ht *pEntry,    /* The entry into which pNew is inserted */
32873  HashElem *pNew         /* The element to be inserted */
32874){
32875  HashElem *pHead;       /* First element already in pEntry */
32876  if( pEntry ){
32877    pHead = pEntry->count ? pEntry->chain : 0;
32878    pEntry->count++;
32879    pEntry->chain = pNew;
32880  }else{
32881    pHead = 0;
32882  }
32883  if( pHead ){
32884    pNew->next = pHead;
32885    pNew->prev = pHead->prev;
32886    if( pHead->prev ){ pHead->prev->next = pNew; }
32887    else             { pH->first = pNew; }
32888    pHead->prev = pNew;
32889  }else{
32890    pNew->next = pH->first;
32891    if( pH->first ){ pH->first->prev = pNew; }
32892    pNew->prev = 0;
32893    pH->first = pNew;
32894  }
32895}
32896
32897
32898/* Resize the hash table so that it cantains "new_size" buckets.
32899**
32900** The hash table might fail to resize if sqlite3_malloc() fails or
32901** if the new size is the same as the prior size.
32902** Return TRUE if the resize occurs and false if not.
32903*/
32904static int rehash(Hash *pH, unsigned int new_size){
32905  struct _ht *new_ht;            /* The new hash table */
32906  HashElem *elem, *next_elem;    /* For looping over existing elements */
32907
32908#if SQLITE_MALLOC_SOFT_LIMIT>0
32909  if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
32910    new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
32911  }
32912  if( new_size==pH->htsize ) return 0;
32913#endif
32914
32915  /* The inability to allocates space for a larger hash table is
32916  ** a performance hit but it is not a fatal error.  So mark the
32917  ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
32918  ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
32919  ** only zeroes the requested number of bytes whereas this module will
32920  ** use the actual amount of space allocated for the hash table (which
32921  ** may be larger than the requested amount).
32922  */
32923  sqlite3BeginBenignMalloc();
32924  new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
32925  sqlite3EndBenignMalloc();
32926
32927  if( new_ht==0 ) return 0;
32928  sqlite3_free(pH->ht);
32929  pH->ht = new_ht;
32930  pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
32931  memset(new_ht, 0, new_size*sizeof(struct _ht));
32932  for(elem=pH->first, pH->first=0; elem; elem = next_elem){
32933    unsigned int h = strHash(elem->pKey) % new_size;
32934    next_elem = elem->next;
32935    insertElement(pH, &new_ht[h], elem);
32936  }
32937  return 1;
32938}
32939
32940/* This function (for internal use only) locates an element in an
32941** hash table that matches the given key.  If no element is found,
32942** a pointer to a static null element with HashElem.data==0 is returned.
32943** If pH is not NULL, then the hash for this key is written to *pH.
32944*/
32945static HashElem *findElementWithHash(
32946  const Hash *pH,     /* The pH to be searched */
32947  const char *pKey,   /* The key we are searching for */
32948  unsigned int *pHash /* Write the hash value here */
32949){
32950  HashElem *elem;                /* Used to loop thru the element list */
32951  unsigned int count;            /* Number of elements left to test */
32952  unsigned int h;                /* The computed hash */
32953  static HashElem nullElement = { 0, 0, 0, 0 };
32954
32955  if( pH->ht ){   /*OPTIMIZATION-IF-TRUE*/
32956    struct _ht *pEntry;
32957    h = strHash(pKey) % pH->htsize;
32958    pEntry = &pH->ht[h];
32959    elem = pEntry->chain;
32960    count = pEntry->count;
32961  }else{
32962    h = 0;
32963    elem = pH->first;
32964    count = pH->count;
32965  }
32966  if( pHash ) *pHash = h;
32967  while( count-- ){
32968    assert( elem!=0 );
32969    if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
32970      return elem;
32971    }
32972    elem = elem->next;
32973  }
32974  return &nullElement;
32975}
32976
32977/* Remove a single entry from the hash table given a pointer to that
32978** element and a hash on the element's key.
32979*/
32980static void removeElementGivenHash(
32981  Hash *pH,         /* The pH containing "elem" */
32982  HashElem* elem,   /* The element to be removed from the pH */
32983  unsigned int h    /* Hash value for the element */
32984){
32985  struct _ht *pEntry;
32986  if( elem->prev ){
32987    elem->prev->next = elem->next;
32988  }else{
32989    pH->first = elem->next;
32990  }
32991  if( elem->next ){
32992    elem->next->prev = elem->prev;
32993  }
32994  if( pH->ht ){
32995    pEntry = &pH->ht[h];
32996    if( pEntry->chain==elem ){
32997      pEntry->chain = elem->next;
32998    }
32999    assert( pEntry->count>0 );
33000    pEntry->count--;
33001  }
33002  sqlite3_free( elem );
33003  pH->count--;
33004  if( pH->count==0 ){
33005    assert( pH->first==0 );
33006    assert( pH->count==0 );
33007    sqlite3HashClear(pH);
33008  }
33009}
33010
33011/* Attempt to locate an element of the hash table pH with a key
33012** that matches pKey.  Return the data for this element if it is
33013** found, or NULL if there is no match.
33014*/
33015SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){
33016  assert( pH!=0 );
33017  assert( pKey!=0 );
33018  return findElementWithHash(pH, pKey, 0)->data;
33019}
33020
33021/* Insert an element into the hash table pH.  The key is pKey
33022** and the data is "data".
33023**
33024** If no element exists with a matching key, then a new
33025** element is created and NULL is returned.
33026**
33027** If another element already exists with the same key, then the
33028** new data replaces the old data and the old data is returned.
33029** The key is not copied in this instance.  If a malloc fails, then
33030** the new data is returned and the hash table is unchanged.
33031**
33032** If the "data" parameter to this function is NULL, then the
33033** element corresponding to "key" is removed from the hash table.
33034*/
33035SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
33036  unsigned int h;       /* the hash of the key modulo hash table size */
33037  HashElem *elem;       /* Used to loop thru the element list */
33038  HashElem *new_elem;   /* New element added to the pH */
33039
33040  assert( pH!=0 );
33041  assert( pKey!=0 );
33042  elem = findElementWithHash(pH,pKey,&h);
33043  if( elem->data ){
33044    void *old_data = elem->data;
33045    if( data==0 ){
33046      removeElementGivenHash(pH,elem,h);
33047    }else{
33048      elem->data = data;
33049      elem->pKey = pKey;
33050    }
33051    return old_data;
33052  }
33053  if( data==0 ) return 0;
33054  new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
33055  if( new_elem==0 ) return data;
33056  new_elem->pKey = pKey;
33057  new_elem->data = data;
33058  pH->count++;
33059  if( pH->count>=10 && pH->count > 2*pH->htsize ){
33060    if( rehash(pH, pH->count*2) ){
33061      assert( pH->htsize>0 );
33062      h = strHash(pKey) % pH->htsize;
33063    }
33064  }
33065  insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
33066  return 0;
33067}
33068
33069/************** End of hash.c ************************************************/
33070/************** Begin file opcodes.c *****************************************/
33071/* Automatically generated.  Do not edit */
33072/* See the tool/mkopcodec.tcl script for details. */
33073#if !defined(SQLITE_OMIT_EXPLAIN) \
33074 || defined(VDBE_PROFILE) \
33075 || defined(SQLITE_DEBUG)
33076#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
33077# define OpHelp(X) "\0" X
33078#else
33079# define OpHelp(X)
33080#endif
33081SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
33082 static const char *const azName[] = {
33083    /*   0 */ "Savepoint"        OpHelp(""),
33084    /*   1 */ "AutoCommit"       OpHelp(""),
33085    /*   2 */ "Transaction"      OpHelp(""),
33086    /*   3 */ "SorterNext"       OpHelp(""),
33087    /*   4 */ "Prev"             OpHelp(""),
33088    /*   5 */ "Next"             OpHelp(""),
33089    /*   6 */ "Checkpoint"       OpHelp(""),
33090    /*   7 */ "JournalMode"      OpHelp(""),
33091    /*   8 */ "Vacuum"           OpHelp(""),
33092    /*   9 */ "VFilter"          OpHelp("iplan=r[P3] zplan='P4'"),
33093    /*  10 */ "VUpdate"          OpHelp("data=r[P3@P2]"),
33094    /*  11 */ "Goto"             OpHelp(""),
33095    /*  12 */ "Gosub"            OpHelp(""),
33096    /*  13 */ "InitCoroutine"    OpHelp(""),
33097    /*  14 */ "Yield"            OpHelp(""),
33098    /*  15 */ "MustBeInt"        OpHelp(""),
33099    /*  16 */ "Jump"             OpHelp(""),
33100    /*  17 */ "Once"             OpHelp(""),
33101    /*  18 */ "If"               OpHelp(""),
33102    /*  19 */ "Not"              OpHelp("r[P2]= !r[P1]"),
33103    /*  20 */ "IfNot"            OpHelp(""),
33104    /*  21 */ "IfNullRow"        OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
33105    /*  22 */ "SeekLT"           OpHelp("key=r[P3@P4]"),
33106    /*  23 */ "SeekLE"           OpHelp("key=r[P3@P4]"),
33107    /*  24 */ "SeekGE"           OpHelp("key=r[P3@P4]"),
33108    /*  25 */ "SeekGT"           OpHelp("key=r[P3@P4]"),
33109    /*  26 */ "IfNotOpen"        OpHelp("if( !csr[P1] ) goto P2"),
33110    /*  27 */ "IfNoHope"         OpHelp("key=r[P3@P4]"),
33111    /*  28 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
33112    /*  29 */ "NotFound"         OpHelp("key=r[P3@P4]"),
33113    /*  30 */ "Found"            OpHelp("key=r[P3@P4]"),
33114    /*  31 */ "SeekRowid"        OpHelp("intkey=r[P3]"),
33115    /*  32 */ "NotExists"        OpHelp("intkey=r[P3]"),
33116    /*  33 */ "Last"             OpHelp(""),
33117    /*  34 */ "IfSmaller"        OpHelp(""),
33118    /*  35 */ "SorterSort"       OpHelp(""),
33119    /*  36 */ "Sort"             OpHelp(""),
33120    /*  37 */ "Rewind"           OpHelp(""),
33121    /*  38 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
33122    /*  39 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
33123    /*  40 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
33124    /*  41 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
33125    /*  42 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
33126    /*  43 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
33127    /*  44 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
33128    /*  45 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
33129    /*  46 */ "Program"          OpHelp(""),
33130    /*  47 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
33131    /*  48 */ "IfPos"            OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
33132    /*  49 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
33133    /*  50 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
33134    /*  51 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
33135    /*  52 */ "Ne"               OpHelp("IF r[P3]!=r[P1]"),
33136    /*  53 */ "Eq"               OpHelp("IF r[P3]==r[P1]"),
33137    /*  54 */ "Gt"               OpHelp("IF r[P3]>r[P1]"),
33138    /*  55 */ "Le"               OpHelp("IF r[P3]<=r[P1]"),
33139    /*  56 */ "Lt"               OpHelp("IF r[P3]<r[P1]"),
33140    /*  57 */ "Ge"               OpHelp("IF r[P3]>=r[P1]"),
33141    /*  58 */ "ElseNotEq"        OpHelp(""),
33142    /*  59 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
33143    /*  60 */ "IncrVacuum"       OpHelp(""),
33144    /*  61 */ "VNext"            OpHelp(""),
33145    /*  62 */ "Init"             OpHelp("Start at P2"),
33146    /*  63 */ "PureFunc"         OpHelp("r[P3]=func(r[P2@NP])"),
33147    /*  64 */ "Function"         OpHelp("r[P3]=func(r[P2@NP])"),
33148    /*  65 */ "Return"           OpHelp(""),
33149    /*  66 */ "EndCoroutine"     OpHelp(""),
33150    /*  67 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
33151    /*  68 */ "Halt"             OpHelp(""),
33152    /*  69 */ "Integer"          OpHelp("r[P2]=P1"),
33153    /*  70 */ "Int64"            OpHelp("r[P2]=P4"),
33154    /*  71 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
33155    /*  72 */ "Null"             OpHelp("r[P2..P3]=NULL"),
33156    /*  73 */ "SoftNull"         OpHelp("r[P1]=NULL"),
33157    /*  74 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
33158    /*  75 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
33159    /*  76 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
33160    /*  77 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
33161    /*  78 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
33162    /*  79 */ "IntCopy"          OpHelp("r[P2]=r[P1]"),
33163    /*  80 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
33164    /*  81 */ "CollSeq"          OpHelp(""),
33165    /*  82 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
33166    /*  83 */ "RealAffinity"     OpHelp(""),
33167    /*  84 */ "Cast"             OpHelp("affinity(r[P1])"),
33168    /*  85 */ "Permutation"      OpHelp(""),
33169    /*  86 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
33170    /*  87 */ "IsTrue"           OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
33171    /*  88 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
33172    /*  89 */ "Column"           OpHelp("r[P3]=PX"),
33173    /*  90 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
33174    /*  91 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
33175    /*  92 */ "Count"            OpHelp("r[P2]=count()"),
33176    /*  93 */ "ReadCookie"       OpHelp(""),
33177    /*  94 */ "SetCookie"        OpHelp(""),
33178    /*  95 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
33179    /*  96 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
33180    /*  97 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
33181    /*  98 */ "OpenDup"          OpHelp(""),
33182    /*  99 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
33183    /* 100 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
33184    /* 101 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
33185    /* 102 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
33186    /* 103 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
33187    /* 104 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
33188    /* 105 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
33189    /* 106 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
33190    /* 107 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
33191    /* 108 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
33192    /* 109 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
33193    /* 110 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
33194    /* 111 */ "SorterOpen"       OpHelp(""),
33195    /* 112 */ "BitNot"           OpHelp("r[P2]= ~r[P1]"),
33196    /* 113 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
33197    /* 114 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
33198    /* 115 */ "String8"          OpHelp("r[P2]='P4'"),
33199    /* 116 */ "Close"            OpHelp(""),
33200    /* 117 */ "ColumnsUsed"      OpHelp(""),
33201    /* 118 */ "SeekHit"          OpHelp("seekHit=P2"),
33202    /* 119 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
33203    /* 120 */ "NewRowid"         OpHelp("r[P2]=rowid"),
33204    /* 121 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
33205    /* 122 */ "Delete"           OpHelp(""),
33206    /* 123 */ "ResetCount"       OpHelp(""),
33207    /* 124 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
33208    /* 125 */ "SorterData"       OpHelp("r[P2]=data"),
33209    /* 126 */ "RowData"          OpHelp("r[P2]=data"),
33210    /* 127 */ "Rowid"            OpHelp("r[P2]=rowid"),
33211    /* 128 */ "NullRow"          OpHelp(""),
33212    /* 129 */ "SeekEnd"          OpHelp(""),
33213    /* 130 */ "IdxInsert"        OpHelp("key=r[P2]"),
33214    /* 131 */ "SorterInsert"     OpHelp("key=r[P2]"),
33215    /* 132 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
33216    /* 133 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
33217    /* 134 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
33218    /* 135 */ "FinishSeek"       OpHelp(""),
33219    /* 136 */ "Destroy"          OpHelp(""),
33220    /* 137 */ "Clear"            OpHelp(""),
33221    /* 138 */ "ResetSorter"      OpHelp(""),
33222    /* 139 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
33223    /* 140 */ "SqlExec"          OpHelp(""),
33224    /* 141 */ "ParseSchema"      OpHelp(""),
33225    /* 142 */ "LoadAnalysis"     OpHelp(""),
33226    /* 143 */ "DropTable"        OpHelp(""),
33227    /* 144 */ "DropIndex"        OpHelp(""),
33228    /* 145 */ "DropTrigger"      OpHelp(""),
33229    /* 146 */ "IntegrityCk"      OpHelp(""),
33230    /* 147 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
33231    /* 148 */ "Param"            OpHelp(""),
33232    /* 149 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
33233    /* 150 */ "Real"             OpHelp("r[P2]=P4"),
33234    /* 151 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
33235    /* 152 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
33236    /* 153 */ "AggInverse"       OpHelp("accum=r[P3] inverse(r[P2@P5])"),
33237    /* 154 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
33238    /* 155 */ "AggStep1"         OpHelp("accum=r[P3] step(r[P2@P5])"),
33239    /* 156 */ "AggValue"         OpHelp("r[P3]=value N=P2"),
33240    /* 157 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
33241    /* 158 */ "Expire"           OpHelp(""),
33242    /* 159 */ "CursorLock"       OpHelp(""),
33243    /* 160 */ "CursorUnlock"     OpHelp(""),
33244    /* 161 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
33245    /* 162 */ "VBegin"           OpHelp(""),
33246    /* 163 */ "VCreate"          OpHelp(""),
33247    /* 164 */ "VDestroy"         OpHelp(""),
33248    /* 165 */ "VOpen"            OpHelp(""),
33249    /* 166 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
33250    /* 167 */ "VRename"          OpHelp(""),
33251    /* 168 */ "Pagecount"        OpHelp(""),
33252    /* 169 */ "MaxPgcnt"         OpHelp(""),
33253    /* 170 */ "Trace"            OpHelp(""),
33254    /* 171 */ "CursorHint"       OpHelp(""),
33255    /* 172 */ "ReleaseReg"       OpHelp("release r[P1@P2] mask P3"),
33256    /* 173 */ "Noop"             OpHelp(""),
33257    /* 174 */ "Explain"          OpHelp(""),
33258    /* 175 */ "Abortable"        OpHelp(""),
33259  };
33260  return azName[i];
33261}
33262#endif
33263
33264/************** End of opcodes.c *********************************************/
33265/************** Begin file os_unix.c *****************************************/
33266/*
33267** 2004 May 22
33268**
33269** The author disclaims copyright to this source code.  In place of
33270** a legal notice, here is a blessing:
33271**
33272**    May you do good and not evil.
33273**    May you find forgiveness for yourself and forgive others.
33274**    May you share freely, never taking more than you give.
33275**
33276******************************************************************************
33277**
33278** This file contains the VFS implementation for unix-like operating systems
33279** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
33280**
33281** There are actually several different VFS implementations in this file.
33282** The differences are in the way that file locking is done.  The default
33283** implementation uses Posix Advisory Locks.  Alternative implementations
33284** use flock(), dot-files, various proprietary locking schemas, or simply
33285** skip locking all together.
33286**
33287** This source file is organized into divisions where the logic for various
33288** subfunctions is contained within the appropriate division.  PLEASE
33289** KEEP THE STRUCTURE OF THIS FILE INTACT.  New code should be placed
33290** in the correct division and should be clearly labeled.
33291**
33292** The layout of divisions is as follows:
33293**
33294**   *  General-purpose declarations and utility functions.
33295**   *  Unique file ID logic used by VxWorks.
33296**   *  Various locking primitive implementations (all except proxy locking):
33297**      + for Posix Advisory Locks
33298**      + for no-op locks
33299**      + for dot-file locks
33300**      + for flock() locking
33301**      + for named semaphore locks (VxWorks only)
33302**      + for AFP filesystem locks (MacOSX only)
33303**   *  sqlite3_file methods not associated with locking.
33304**   *  Definitions of sqlite3_io_methods objects for all locking
33305**      methods plus "finder" functions for each locking method.
33306**   *  sqlite3_vfs method implementations.
33307**   *  Locking primitives for the proxy uber-locking-method. (MacOSX only)
33308**   *  Definitions of sqlite3_vfs objects for all locking methods
33309**      plus implementations of sqlite3_os_init() and sqlite3_os_end().
33310*/
33311/* #include "sqliteInt.h" */
33312#if SQLITE_OS_UNIX              /* This file is used on unix only */
33313
33314/*
33315** There are various methods for file locking used for concurrency
33316** control:
33317**
33318**   1. POSIX locking (the default),
33319**   2. No locking,
33320**   3. Dot-file locking,
33321**   4. flock() locking,
33322**   5. AFP locking (OSX only),
33323**   6. Named POSIX semaphores (VXWorks only),
33324**   7. proxy locking. (OSX only)
33325**
33326** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
33327** is defined to 1.  The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
33328** selection of the appropriate locking style based on the filesystem
33329** where the database is located.
33330*/
33331#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
33332#  if defined(__APPLE__)
33333#    define SQLITE_ENABLE_LOCKING_STYLE 1
33334#  else
33335#    define SQLITE_ENABLE_LOCKING_STYLE 0
33336#  endif
33337#endif
33338
33339/* Use pread() and pwrite() if they are available */
33340#if defined(__APPLE__)
33341# define HAVE_PREAD 1
33342# define HAVE_PWRITE 1
33343#endif
33344#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
33345# undef USE_PREAD
33346# define USE_PREAD64 1
33347#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE)
33348# undef USE_PREAD64
33349# define USE_PREAD 1
33350#endif
33351
33352/*
33353** standard include files.
33354*/
33355#include <sys/types.h>
33356#include <sys/stat.h>
33357#include <fcntl.h>
33358#include <sys/ioctl.h>
33359#include <unistd.h>
33360/* #include <time.h> */
33361#include <sys/time.h>
33362#include <errno.h>
33363#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
33364# include <sys/mman.h>
33365#endif
33366
33367#if SQLITE_ENABLE_LOCKING_STYLE
33368/* # include <sys/ioctl.h> */
33369# include <sys/file.h>
33370# include <sys/param.h>
33371#endif /* SQLITE_ENABLE_LOCKING_STYLE */
33372
33373/*
33374** Try to determine if gethostuuid() is available based on standard
33375** macros.  This might sometimes compute the wrong value for some
33376** obscure platforms.  For those cases, simply compile with one of
33377** the following:
33378**
33379**    -DHAVE_GETHOSTUUID=0
33380**    -DHAVE_GETHOSTUUID=1
33381**
33382** None if this matters except when building on Apple products with
33383** -DSQLITE_ENABLE_LOCKING_STYLE.
33384*/
33385#ifndef HAVE_GETHOSTUUID
33386# define HAVE_GETHOSTUUID 0
33387# if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
33388                            (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
33389#    if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
33390         && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))
33391#      undef HAVE_GETHOSTUUID
33392#      define HAVE_GETHOSTUUID 1
33393#    else
33394#      warning "gethostuuid() is disabled."
33395#    endif
33396#  endif
33397#endif
33398
33399
33400#if OS_VXWORKS
33401/* # include <sys/ioctl.h> */
33402# include <semaphore.h>
33403# include <limits.h>
33404#endif /* OS_VXWORKS */
33405
33406#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
33407# include <sys/mount.h>
33408#endif
33409
33410#ifdef HAVE_UTIME
33411# include <utime.h>
33412#endif
33413
33414/*
33415** Allowed values of unixFile.fsFlags
33416*/
33417#define SQLITE_FSFLAGS_IS_MSDOS     0x1
33418
33419/*
33420** If we are to be thread-safe, include the pthreads header.
33421*/
33422#if SQLITE_THREADSAFE
33423/* # include <pthread.h> */
33424#endif
33425
33426/*
33427** Default permissions when creating a new file
33428*/
33429#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
33430# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
33431#endif
33432
33433/*
33434** Default permissions when creating auto proxy dir
33435*/
33436#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
33437# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
33438#endif
33439
33440/*
33441** Maximum supported path-length.
33442*/
33443#define MAX_PATHNAME 512
33444
33445/*
33446** Maximum supported symbolic links
33447*/
33448#define SQLITE_MAX_SYMLINKS 100
33449
33450/* Always cast the getpid() return type for compatibility with
33451** kernel modules in VxWorks. */
33452#define osGetpid(X) (pid_t)getpid()
33453
33454/*
33455** Only set the lastErrno if the error code is a real error and not
33456** a normal expected return code of SQLITE_BUSY or SQLITE_OK
33457*/
33458#define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))
33459
33460/* Forward references */
33461typedef struct unixShm unixShm;               /* Connection shared memory */
33462typedef struct unixShmNode unixShmNode;       /* Shared memory instance */
33463typedef struct unixInodeInfo unixInodeInfo;   /* An i-node */
33464typedef struct UnixUnusedFd UnixUnusedFd;     /* An unused file descriptor */
33465
33466/*
33467** Sometimes, after a file handle is closed by SQLite, the file descriptor
33468** cannot be closed immediately. In these cases, instances of the following
33469** structure are used to store the file descriptor while waiting for an
33470** opportunity to either close or reuse it.
33471*/
33472struct UnixUnusedFd {
33473  int fd;                   /* File descriptor to close */
33474  int flags;                /* Flags this file descriptor was opened with */
33475  UnixUnusedFd *pNext;      /* Next unused file descriptor on same file */
33476};
33477
33478/*
33479** The unixFile structure is subclass of sqlite3_file specific to the unix
33480** VFS implementations.
33481*/
33482typedef struct unixFile unixFile;
33483struct unixFile {
33484  sqlite3_io_methods const *pMethod;  /* Always the first entry */
33485  sqlite3_vfs *pVfs;                  /* The VFS that created this unixFile */
33486  unixInodeInfo *pInode;              /* Info about locks on this inode */
33487  int h;                              /* The file descriptor */
33488  unsigned char eFileLock;            /* The type of lock held on this fd */
33489  unsigned short int ctrlFlags;       /* Behavioral bits.  UNIXFILE_* flags */
33490  int lastErrno;                      /* The unix errno from last I/O error */
33491  void *lockingContext;               /* Locking style specific state */
33492  UnixUnusedFd *pPreallocatedUnused;  /* Pre-allocated UnixUnusedFd */
33493  const char *zPath;                  /* Name of the file */
33494  unixShm *pShm;                      /* Shared memory segment information */
33495  int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
33496#if SQLITE_MAX_MMAP_SIZE>0
33497  int nFetchOut;                      /* Number of outstanding xFetch refs */
33498  sqlite3_int64 mmapSize;             /* Usable size of mapping at pMapRegion */
33499  sqlite3_int64 mmapSizeActual;       /* Actual size of mapping at pMapRegion */
33500  sqlite3_int64 mmapSizeMax;          /* Configured FCNTL_MMAP_SIZE value */
33501  void *pMapRegion;                   /* Memory mapped region */
33502#endif
33503  int sectorSize;                     /* Device sector size */
33504  int deviceCharacteristics;          /* Precomputed device characteristics */
33505#if SQLITE_ENABLE_LOCKING_STYLE
33506  int openFlags;                      /* The flags specified at open() */
33507#endif
33508#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
33509  unsigned fsFlags;                   /* cached details from statfs() */
33510#endif
33511#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
33512  unsigned iBusyTimeout;              /* Wait this many millisec on locks */
33513#endif
33514#if OS_VXWORKS
33515  struct vxworksFileId *pId;          /* Unique file ID */
33516#endif
33517#ifdef SQLITE_DEBUG
33518  /* The next group of variables are used to track whether or not the
33519  ** transaction counter in bytes 24-27 of database files are updated
33520  ** whenever any part of the database changes.  An assertion fault will
33521  ** occur if a file is updated without also updating the transaction
33522  ** counter.  This test is made to avoid new problems similar to the
33523  ** one described by ticket #3584.
33524  */
33525  unsigned char transCntrChng;   /* True if the transaction counter changed */
33526  unsigned char dbUpdate;        /* True if any part of database file changed */
33527  unsigned char inNormalWrite;   /* True if in a normal write operation */
33528
33529#endif
33530
33531#ifdef SQLITE_TEST
33532  /* In test mode, increase the size of this structure a bit so that
33533  ** it is larger than the struct CrashFile defined in test6.c.
33534  */
33535  char aPadding[32];
33536#endif
33537};
33538
33539/* This variable holds the process id (pid) from when the xRandomness()
33540** method was called.  If xOpen() is called from a different process id,
33541** indicating that a fork() has occurred, the PRNG will be reset.
33542*/
33543static pid_t randomnessPid = 0;
33544
33545/*
33546** Allowed values for the unixFile.ctrlFlags bitmask:
33547*/
33548#define UNIXFILE_EXCL        0x01     /* Connections from one process only */
33549#define UNIXFILE_RDONLY      0x02     /* Connection is read only */
33550#define UNIXFILE_PERSIST_WAL 0x04     /* Persistent WAL mode */
33551#ifndef SQLITE_DISABLE_DIRSYNC
33552# define UNIXFILE_DIRSYNC    0x08     /* Directory sync needed */
33553#else
33554# define UNIXFILE_DIRSYNC    0x00
33555#endif
33556#define UNIXFILE_PSOW        0x10     /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
33557#define UNIXFILE_DELETE      0x20     /* Delete on close */
33558#define UNIXFILE_URI         0x40     /* Filename might have query parameters */
33559#define UNIXFILE_NOLOCK      0x80     /* Do no file locking */
33560
33561/*
33562** Include code that is common to all os_*.c files
33563*/
33564/************** Include os_common.h in the middle of os_unix.c ***************/
33565/************** Begin file os_common.h ***************************************/
33566/*
33567** 2004 May 22
33568**
33569** The author disclaims copyright to this source code.  In place of
33570** a legal notice, here is a blessing:
33571**
33572**    May you do good and not evil.
33573**    May you find forgiveness for yourself and forgive others.
33574**    May you share freely, never taking more than you give.
33575**
33576******************************************************************************
33577**
33578** This file contains macros and a little bit of code that is common to
33579** all of the platform-specific files (os_*.c) and is #included into those
33580** files.
33581**
33582** This file should be #included by the os_*.c files only.  It is not a
33583** general purpose header file.
33584*/
33585#ifndef _OS_COMMON_H_
33586#define _OS_COMMON_H_
33587
33588/*
33589** At least two bugs have slipped in because we changed the MEMORY_DEBUG
33590** macro to SQLITE_DEBUG and some older makefiles have not yet made the
33591** switch.  The following code should catch this problem at compile-time.
33592*/
33593#ifdef MEMORY_DEBUG
33594# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
33595#endif
33596
33597/*
33598** Macros for performance tracing.  Normally turned off.  Only works
33599** on i486 hardware.
33600*/
33601#ifdef SQLITE_PERFORMANCE_TRACE
33602
33603/*
33604** hwtime.h contains inline assembler code for implementing
33605** high-performance timing routines.
33606*/
33607/************** Include hwtime.h in the middle of os_common.h ****************/
33608/************** Begin file hwtime.h ******************************************/
33609/*
33610** 2008 May 27
33611**
33612** The author disclaims copyright to this source code.  In place of
33613** a legal notice, here is a blessing:
33614**
33615**    May you do good and not evil.
33616**    May you find forgiveness for yourself and forgive others.
33617**    May you share freely, never taking more than you give.
33618**
33619******************************************************************************
33620**
33621** This file contains inline asm code for retrieving "high-performance"
33622** counters for x86 and x86_64 class CPUs.
33623*/
33624#ifndef SQLITE_HWTIME_H
33625#define SQLITE_HWTIME_H
33626
33627/*
33628** The following routine only works on pentium-class (or newer) processors.
33629** It uses the RDTSC opcode to read the cycle count value out of the
33630** processor and returns that value.  This can be used for high-res
33631** profiling.
33632*/
33633#if !defined(__STRICT_ANSI__) && \
33634    (defined(__GNUC__) || defined(_MSC_VER)) && \
33635    (defined(i386) || defined(__i386__) || defined(_M_IX86))
33636
33637  #if defined(__GNUC__)
33638
33639  __inline__ sqlite_uint64 sqlite3Hwtime(void){
33640     unsigned int lo, hi;
33641     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
33642     return (sqlite_uint64)hi << 32 | lo;
33643  }
33644
33645  #elif defined(_MSC_VER)
33646
33647  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
33648     __asm {
33649        rdtsc
33650        ret       ; return value at EDX:EAX
33651     }
33652  }
33653
33654  #endif
33655
33656#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__x86_64__))
33657
33658  __inline__ sqlite_uint64 sqlite3Hwtime(void){
33659      unsigned long val;
33660      __asm__ __volatile__ ("rdtsc" : "=A" (val));
33661      return val;
33662  }
33663
33664#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__ppc__))
33665
33666  __inline__ sqlite_uint64 sqlite3Hwtime(void){
33667      unsigned long long retval;
33668      unsigned long junk;
33669      __asm__ __volatile__ ("\n\
33670          1:      mftbu   %1\n\
33671                  mftb    %L0\n\
33672                  mftbu   %0\n\
33673                  cmpw    %0,%1\n\
33674                  bne     1b"
33675                  : "=r" (retval), "=r" (junk));
33676      return retval;
33677  }
33678
33679#else
33680
33681  /*
33682  ** asm() is needed for hardware timing support.  Without asm(),
33683  ** disable the sqlite3Hwtime() routine.
33684  **
33685  ** sqlite3Hwtime() is only used for some obscure debugging
33686  ** and analysis configurations, not in any deliverable, so this
33687  ** should not be a great loss.
33688  */
33689SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
33690
33691#endif
33692
33693#endif /* !defined(SQLITE_HWTIME_H) */
33694
33695/************** End of hwtime.h **********************************************/
33696/************** Continuing where we left off in os_common.h ******************/
33697
33698static sqlite_uint64 g_start;
33699static sqlite_uint64 g_elapsed;
33700#define TIMER_START       g_start=sqlite3Hwtime()
33701#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
33702#define TIMER_ELAPSED     g_elapsed
33703#else
33704#define TIMER_START
33705#define TIMER_END
33706#define TIMER_ELAPSED     ((sqlite_uint64)0)
33707#endif
33708
33709/*
33710** If we compile with the SQLITE_TEST macro set, then the following block
33711** of code will give us the ability to simulate a disk I/O error.  This
33712** is used for testing the I/O recovery logic.
33713*/
33714#if defined(SQLITE_TEST)
33715SQLITE_API extern int sqlite3_io_error_hit;
33716SQLITE_API extern int sqlite3_io_error_hardhit;
33717SQLITE_API extern int sqlite3_io_error_pending;
33718SQLITE_API extern int sqlite3_io_error_persist;
33719SQLITE_API extern int sqlite3_io_error_benign;
33720SQLITE_API extern int sqlite3_diskfull_pending;
33721SQLITE_API extern int sqlite3_diskfull;
33722#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
33723#define SimulateIOError(CODE)  \
33724  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
33725       || sqlite3_io_error_pending-- == 1 )  \
33726              { local_ioerr(); CODE; }
33727static void local_ioerr(){
33728  IOTRACE(("IOERR\n"));
33729  sqlite3_io_error_hit++;
33730  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
33731}
33732#define SimulateDiskfullError(CODE) \
33733   if( sqlite3_diskfull_pending ){ \
33734     if( sqlite3_diskfull_pending == 1 ){ \
33735       local_ioerr(); \
33736       sqlite3_diskfull = 1; \
33737       sqlite3_io_error_hit = 1; \
33738       CODE; \
33739     }else{ \
33740       sqlite3_diskfull_pending--; \
33741     } \
33742   }
33743#else
33744#define SimulateIOErrorBenign(X)
33745#define SimulateIOError(A)
33746#define SimulateDiskfullError(A)
33747#endif /* defined(SQLITE_TEST) */
33748
33749/*
33750** When testing, keep a count of the number of open files.
33751*/
33752#if defined(SQLITE_TEST)
33753SQLITE_API extern int sqlite3_open_file_count;
33754#define OpenCounter(X)  sqlite3_open_file_count+=(X)
33755#else
33756#define OpenCounter(X)
33757#endif /* defined(SQLITE_TEST) */
33758
33759#endif /* !defined(_OS_COMMON_H_) */
33760
33761/************** End of os_common.h *******************************************/
33762/************** Continuing where we left off in os_unix.c ********************/
33763
33764/*
33765** Define various macros that are missing from some systems.
33766*/
33767#ifndef O_LARGEFILE
33768# define O_LARGEFILE 0
33769#endif
33770#ifdef SQLITE_DISABLE_LFS
33771# undef O_LARGEFILE
33772# define O_LARGEFILE 0
33773#endif
33774#ifndef O_NOFOLLOW
33775# define O_NOFOLLOW 0
33776#endif
33777#ifndef O_BINARY
33778# define O_BINARY 0
33779#endif
33780
33781/*
33782** The threadid macro resolves to the thread-id or to 0.  Used for
33783** testing and debugging only.
33784*/
33785#if SQLITE_THREADSAFE
33786#define threadid pthread_self()
33787#else
33788#define threadid 0
33789#endif
33790
33791/*
33792** HAVE_MREMAP defaults to true on Linux and false everywhere else.
33793*/
33794#if !defined(HAVE_MREMAP)
33795# if defined(__linux__) && defined(_GNU_SOURCE)
33796#  define HAVE_MREMAP 1
33797# else
33798#  define HAVE_MREMAP 0
33799# endif
33800#endif
33801
33802/*
33803** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
33804** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
33805*/
33806#ifdef __ANDROID__
33807# define lseek lseek64
33808#endif
33809
33810#ifdef __linux__
33811/*
33812** Linux-specific IOCTL magic numbers used for controlling F2FS
33813*/
33814#define F2FS_IOCTL_MAGIC        0xf5
33815#define F2FS_IOC_START_ATOMIC_WRITE     _IO(F2FS_IOCTL_MAGIC, 1)
33816#define F2FS_IOC_COMMIT_ATOMIC_WRITE    _IO(F2FS_IOCTL_MAGIC, 2)
33817#define F2FS_IOC_START_VOLATILE_WRITE   _IO(F2FS_IOCTL_MAGIC, 3)
33818#define F2FS_IOC_ABORT_VOLATILE_WRITE   _IO(F2FS_IOCTL_MAGIC, 5)
33819#define F2FS_IOC_GET_FEATURES           _IOR(F2FS_IOCTL_MAGIC, 12, u32)
33820#define F2FS_FEATURE_ATOMIC_WRITE 0x0004
33821#endif /* __linux__ */
33822
33823
33824/*
33825** Different Unix systems declare open() in different ways.  Same use
33826** open(const char*,int,mode_t).  Others use open(const char*,int,...).
33827** The difference is important when using a pointer to the function.
33828**
33829** The safest way to deal with the problem is to always use this wrapper
33830** which always has the same well-defined interface.
33831*/
33832static int posixOpen(const char *zFile, int flags, int mode){
33833  return open(zFile, flags, mode);
33834}
33835
33836/* Forward reference */
33837static int openDirectory(const char*, int*);
33838static int unixGetpagesize(void);
33839
33840/*
33841** Many system calls are accessed through pointer-to-functions so that
33842** they may be overridden at runtime to facilitate fault injection during
33843** testing and sandboxing.  The following array holds the names and pointers
33844** to all overrideable system calls.
33845*/
33846static struct unix_syscall {
33847  const char *zName;            /* Name of the system call */
33848  sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
33849  sqlite3_syscall_ptr pDefault; /* Default value */
33850} aSyscall[] = {
33851  { "open",         (sqlite3_syscall_ptr)posixOpen,  0  },
33852#define osOpen      ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
33853
33854  { "close",        (sqlite3_syscall_ptr)close,      0  },
33855#define osClose     ((int(*)(int))aSyscall[1].pCurrent)
33856
33857  { "access",       (sqlite3_syscall_ptr)access,     0  },
33858#define osAccess    ((int(*)(const char*,int))aSyscall[2].pCurrent)
33859
33860  { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
33861#define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
33862
33863  { "stat",         (sqlite3_syscall_ptr)stat,       0  },
33864#define osStat      ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
33865
33866/*
33867** The DJGPP compiler environment looks mostly like Unix, but it
33868** lacks the fcntl() system call.  So redefine fcntl() to be something
33869** that always succeeds.  This means that locking does not occur under
33870** DJGPP.  But it is DOS - what did you expect?
33871*/
33872#ifdef __DJGPP__
33873  { "fstat",        0,                 0  },
33874#define osFstat(a,b,c)    0
33875#else
33876  { "fstat",        (sqlite3_syscall_ptr)fstat,      0  },
33877#define osFstat     ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
33878#endif
33879
33880  { "ftruncate",    (sqlite3_syscall_ptr)ftruncate,  0  },
33881#define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
33882
33883  { "fcntl",        (sqlite3_syscall_ptr)fcntl,      0  },
33884#define osFcntl     ((int(*)(int,int,...))aSyscall[7].pCurrent)
33885
33886  { "read",         (sqlite3_syscall_ptr)read,       0  },
33887#define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
33888
33889#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
33890  { "pread",        (sqlite3_syscall_ptr)pread,      0  },
33891#else
33892  { "pread",        (sqlite3_syscall_ptr)0,          0  },
33893#endif
33894#define osPread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
33895
33896#if defined(USE_PREAD64)
33897  { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
33898#else
33899  { "pread64",      (sqlite3_syscall_ptr)0,          0  },
33900#endif
33901#define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
33902
33903  { "write",        (sqlite3_syscall_ptr)write,      0  },
33904#define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
33905
33906#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
33907  { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
33908#else
33909  { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
33910#endif
33911#define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
33912                    aSyscall[12].pCurrent)
33913
33914#if defined(USE_PREAD64)
33915  { "pwrite64",     (sqlite3_syscall_ptr)pwrite64,   0  },
33916#else
33917  { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
33918#endif
33919#define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off64_t))\
33920                    aSyscall[13].pCurrent)
33921
33922  { "fchmod",       (sqlite3_syscall_ptr)fchmod,          0  },
33923#define osFchmod    ((int(*)(int,mode_t))aSyscall[14].pCurrent)
33924
33925#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
33926  { "fallocate",    (sqlite3_syscall_ptr)posix_fallocate,  0 },
33927#else
33928  { "fallocate",    (sqlite3_syscall_ptr)0,                0 },
33929#endif
33930#define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
33931
33932  { "unlink",       (sqlite3_syscall_ptr)unlink,           0 },
33933#define osUnlink    ((int(*)(const char*))aSyscall[16].pCurrent)
33934
33935  { "openDirectory",    (sqlite3_syscall_ptr)openDirectory,      0 },
33936#define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
33937
33938  { "mkdir",        (sqlite3_syscall_ptr)mkdir,           0 },
33939#define osMkdir     ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
33940
33941  { "rmdir",        (sqlite3_syscall_ptr)rmdir,           0 },
33942#define osRmdir     ((int(*)(const char*))aSyscall[19].pCurrent)
33943
33944#if defined(HAVE_FCHOWN)
33945  { "fchown",       (sqlite3_syscall_ptr)fchown,          0 },
33946#else
33947  { "fchown",       (sqlite3_syscall_ptr)0,               0 },
33948#endif
33949#define osFchown    ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
33950
33951#if defined(HAVE_FCHOWN)
33952  { "geteuid",      (sqlite3_syscall_ptr)geteuid,         0 },
33953#else
33954  { "geteuid",      (sqlite3_syscall_ptr)0,               0 },
33955#endif
33956#define osGeteuid   ((uid_t(*)(void))aSyscall[21].pCurrent)
33957
33958#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
33959  { "mmap",         (sqlite3_syscall_ptr)mmap,            0 },
33960#else
33961  { "mmap",         (sqlite3_syscall_ptr)0,               0 },
33962#endif
33963#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent)
33964
33965#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
33966  { "munmap",       (sqlite3_syscall_ptr)munmap,          0 },
33967#else
33968  { "munmap",       (sqlite3_syscall_ptr)0,               0 },
33969#endif
33970#define osMunmap ((int(*)(void*,size_t))aSyscall[23].pCurrent)
33971
33972#if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
33973  { "mremap",       (sqlite3_syscall_ptr)mremap,          0 },
33974#else
33975  { "mremap",       (sqlite3_syscall_ptr)0,               0 },
33976#endif
33977#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)
33978
33979#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
33980  { "getpagesize",  (sqlite3_syscall_ptr)unixGetpagesize, 0 },
33981#else
33982  { "getpagesize",  (sqlite3_syscall_ptr)0,               0 },
33983#endif
33984#define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent)
33985
33986#if defined(HAVE_READLINK)
33987  { "readlink",     (sqlite3_syscall_ptr)readlink,        0 },
33988#else
33989  { "readlink",     (sqlite3_syscall_ptr)0,               0 },
33990#endif
33991#define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
33992
33993#if defined(HAVE_LSTAT)
33994  { "lstat",         (sqlite3_syscall_ptr)lstat,          0 },
33995#else
33996  { "lstat",         (sqlite3_syscall_ptr)0,              0 },
33997#endif
33998#define osLstat      ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
33999
34000#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
34001# ifdef __ANDROID__
34002  { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 },
34003#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
34004# else
34005  { "ioctl",         (sqlite3_syscall_ptr)ioctl,          0 },
34006#define osIoctl ((int(*)(int,unsigned long,...))aSyscall[28].pCurrent)
34007# endif
34008#else
34009  { "ioctl",         (sqlite3_syscall_ptr)0,              0 },
34010#endif
34011
34012}; /* End of the overrideable system calls */
34013
34014
34015/*
34016** On some systems, calls to fchown() will trigger a message in a security
34017** log if they come from non-root processes.  So avoid calling fchown() if
34018** we are not running as root.
34019*/
34020static int robustFchown(int fd, uid_t uid, gid_t gid){
34021#if defined(HAVE_FCHOWN)
34022  return osGeteuid() ? 0 : osFchown(fd,uid,gid);
34023#else
34024  return 0;
34025#endif
34026}
34027
34028/*
34029** This is the xSetSystemCall() method of sqlite3_vfs for all of the
34030** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
34031** system call pointer, or SQLITE_NOTFOUND if there is no configurable
34032** system call named zName.
34033*/
34034static int unixSetSystemCall(
34035  sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
34036  const char *zName,            /* Name of system call to override */
34037  sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
34038){
34039  unsigned int i;
34040  int rc = SQLITE_NOTFOUND;
34041
34042  UNUSED_PARAMETER(pNotUsed);
34043  if( zName==0 ){
34044    /* If no zName is given, restore all system calls to their default
34045    ** settings and return NULL
34046    */
34047    rc = SQLITE_OK;
34048    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
34049      if( aSyscall[i].pDefault ){
34050        aSyscall[i].pCurrent = aSyscall[i].pDefault;
34051      }
34052    }
34053  }else{
34054    /* If zName is specified, operate on only the one system call
34055    ** specified.
34056    */
34057    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
34058      if( strcmp(zName, aSyscall[i].zName)==0 ){
34059        if( aSyscall[i].pDefault==0 ){
34060          aSyscall[i].pDefault = aSyscall[i].pCurrent;
34061        }
34062        rc = SQLITE_OK;
34063        if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
34064        aSyscall[i].pCurrent = pNewFunc;
34065        break;
34066      }
34067    }
34068  }
34069  return rc;
34070}
34071
34072/*
34073** Return the value of a system call.  Return NULL if zName is not a
34074** recognized system call name.  NULL is also returned if the system call
34075** is currently undefined.
34076*/
34077static sqlite3_syscall_ptr unixGetSystemCall(
34078  sqlite3_vfs *pNotUsed,
34079  const char *zName
34080){
34081  unsigned int i;
34082
34083  UNUSED_PARAMETER(pNotUsed);
34084  for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
34085    if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
34086  }
34087  return 0;
34088}
34089
34090/*
34091** Return the name of the first system call after zName.  If zName==NULL
34092** then return the name of the first system call.  Return NULL if zName
34093** is the last system call or if zName is not the name of a valid
34094** system call.
34095*/
34096static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
34097  int i = -1;
34098
34099  UNUSED_PARAMETER(p);
34100  if( zName ){
34101    for(i=0; i<ArraySize(aSyscall)-1; i++){
34102      if( strcmp(zName, aSyscall[i].zName)==0 ) break;
34103    }
34104  }
34105  for(i++; i<ArraySize(aSyscall); i++){
34106    if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
34107  }
34108  return 0;
34109}
34110
34111/*
34112** Do not accept any file descriptor less than this value, in order to avoid
34113** opening database file using file descriptors that are commonly used for
34114** standard input, output, and error.
34115*/
34116#ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
34117# define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
34118#endif
34119
34120/*
34121** Invoke open().  Do so multiple times, until it either succeeds or
34122** fails for some reason other than EINTR.
34123**
34124** If the file creation mode "m" is 0 then set it to the default for
34125** SQLite.  The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
34126** 0644) as modified by the system umask.  If m is not 0, then
34127** make the file creation mode be exactly m ignoring the umask.
34128**
34129** The m parameter will be non-zero only when creating -wal, -journal,
34130** and -shm files.  We want those files to have *exactly* the same
34131** permissions as their original database, unadulterated by the umask.
34132** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
34133** transaction crashes and leaves behind hot journals, then any
34134** process that is able to write to the database will also be able to
34135** recover the hot journals.
34136*/
34137static int robust_open(const char *z, int f, mode_t m){
34138  int fd;
34139  mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
34140  while(1){
34141#if defined(O_CLOEXEC)
34142    fd = osOpen(z,f|O_CLOEXEC,m2);
34143#else
34144    fd = osOpen(z,f,m2);
34145#endif
34146    if( fd<0 ){
34147      if( errno==EINTR ) continue;
34148      break;
34149    }
34150    if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
34151    osClose(fd);
34152    sqlite3_log(SQLITE_WARNING,
34153                "attempt to open \"%s\" as file descriptor %d", z, fd);
34154    fd = -1;
34155    if( osOpen("/dev/null", O_RDONLY, m)<0 ) break;
34156  }
34157  if( fd>=0 ){
34158    if( m!=0 ){
34159      struct stat statbuf;
34160      if( osFstat(fd, &statbuf)==0
34161       && statbuf.st_size==0
34162       && (statbuf.st_mode&0777)!=m
34163      ){
34164        osFchmod(fd, m);
34165      }
34166    }
34167#if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
34168    osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
34169#endif
34170  }
34171  return fd;
34172}
34173
34174/*
34175** Helper functions to obtain and relinquish the global mutex. The
34176** global mutex is used to protect the unixInodeInfo and
34177** vxworksFileId objects used by this file, all of which may be
34178** shared by multiple threads.
34179**
34180** Function unixMutexHeld() is used to assert() that the global mutex
34181** is held when required. This function is only used as part of assert()
34182** statements. e.g.
34183**
34184**   unixEnterMutex()
34185**     assert( unixMutexHeld() );
34186**   unixEnterLeave()
34187**
34188** To prevent deadlock, the global unixBigLock must must be acquired
34189** before the unixInodeInfo.pLockMutex mutex, if both are held.  It is
34190** OK to get the pLockMutex without holding unixBigLock first, but if
34191** that happens, the unixBigLock mutex must not be acquired until after
34192** pLockMutex is released.
34193**
34194**      OK:     enter(unixBigLock),  enter(pLockInfo)
34195**      OK:     enter(unixBigLock)
34196**      OK:     enter(pLockInfo)
34197**   ERROR:     enter(pLockInfo), enter(unixBigLock)
34198*/
34199static sqlite3_mutex *unixBigLock = 0;
34200static void unixEnterMutex(void){
34201  assert( sqlite3_mutex_notheld(unixBigLock) );  /* Not a recursive mutex */
34202  sqlite3_mutex_enter(unixBigLock);
34203}
34204static void unixLeaveMutex(void){
34205  assert( sqlite3_mutex_held(unixBigLock) );
34206  sqlite3_mutex_leave(unixBigLock);
34207}
34208#ifdef SQLITE_DEBUG
34209static int unixMutexHeld(void) {
34210  return sqlite3_mutex_held(unixBigLock);
34211}
34212#endif
34213
34214
34215#ifdef SQLITE_HAVE_OS_TRACE
34216/*
34217** Helper function for printing out trace information from debugging
34218** binaries. This returns the string representation of the supplied
34219** integer lock-type.
34220*/
34221static const char *azFileLock(int eFileLock){
34222  switch( eFileLock ){
34223    case NO_LOCK: return "NONE";
34224    case SHARED_LOCK: return "SHARED";
34225    case RESERVED_LOCK: return "RESERVED";
34226    case PENDING_LOCK: return "PENDING";
34227    case EXCLUSIVE_LOCK: return "EXCLUSIVE";
34228  }
34229  return "ERROR";
34230}
34231#endif
34232
34233#ifdef SQLITE_LOCK_TRACE
34234/*
34235** Print out information about all locking operations.
34236**
34237** This routine is used for troubleshooting locks on multithreaded
34238** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
34239** command-line option on the compiler.  This code is normally
34240** turned off.
34241*/
34242static int lockTrace(int fd, int op, struct flock *p){
34243  char *zOpName, *zType;
34244  int s;
34245  int savedErrno;
34246  if( op==F_GETLK ){
34247    zOpName = "GETLK";
34248  }else if( op==F_SETLK ){
34249    zOpName = "SETLK";
34250  }else{
34251    s = osFcntl(fd, op, p);
34252    sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
34253    return s;
34254  }
34255  if( p->l_type==F_RDLCK ){
34256    zType = "RDLCK";
34257  }else if( p->l_type==F_WRLCK ){
34258    zType = "WRLCK";
34259  }else if( p->l_type==F_UNLCK ){
34260    zType = "UNLCK";
34261  }else{
34262    assert( 0 );
34263  }
34264  assert( p->l_whence==SEEK_SET );
34265  s = osFcntl(fd, op, p);
34266  savedErrno = errno;
34267  sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
34268     threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
34269     (int)p->l_pid, s);
34270  if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
34271    struct flock l2;
34272    l2 = *p;
34273    osFcntl(fd, F_GETLK, &l2);
34274    if( l2.l_type==F_RDLCK ){
34275      zType = "RDLCK";
34276    }else if( l2.l_type==F_WRLCK ){
34277      zType = "WRLCK";
34278    }else if( l2.l_type==F_UNLCK ){
34279      zType = "UNLCK";
34280    }else{
34281      assert( 0 );
34282    }
34283    sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
34284       zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
34285  }
34286  errno = savedErrno;
34287  return s;
34288}
34289#undef osFcntl
34290#define osFcntl lockTrace
34291#endif /* SQLITE_LOCK_TRACE */
34292
34293/*
34294** Retry ftruncate() calls that fail due to EINTR
34295**
34296** All calls to ftruncate() within this file should be made through
34297** this wrapper.  On the Android platform, bypassing the logic below
34298** could lead to a corrupt database.
34299*/
34300static int robust_ftruncate(int h, sqlite3_int64 sz){
34301  int rc;
34302#ifdef __ANDROID__
34303  /* On Android, ftruncate() always uses 32-bit offsets, even if
34304  ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to
34305  ** truncate a file to any size larger than 2GiB. Silently ignore any
34306  ** such attempts.  */
34307  if( sz>(sqlite3_int64)0x7FFFFFFF ){
34308    rc = SQLITE_OK;
34309  }else
34310#endif
34311  do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
34312  return rc;
34313}
34314
34315/*
34316** This routine translates a standard POSIX errno code into something
34317** useful to the clients of the sqlite3 functions.  Specifically, it is
34318** intended to translate a variety of "try again" errors into SQLITE_BUSY
34319** and a variety of "please close the file descriptor NOW" errors into
34320** SQLITE_IOERR
34321**
34322** Errors during initialization of locks, or file system support for locks,
34323** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
34324*/
34325static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
34326  assert( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
34327          (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
34328          (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
34329          (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) );
34330  switch (posixError) {
34331  case EACCES:
34332  case EAGAIN:
34333  case ETIMEDOUT:
34334  case EBUSY:
34335  case EINTR:
34336  case ENOLCK:
34337    /* random NFS retry error, unless during file system support
34338     * introspection, in which it actually means what it says */
34339    return SQLITE_BUSY;
34340
34341  case EPERM:
34342    return SQLITE_PERM;
34343
34344  default:
34345    return sqliteIOErr;
34346  }
34347}
34348
34349
34350/******************************************************************************
34351****************** Begin Unique File ID Utility Used By VxWorks ***************
34352**
34353** On most versions of unix, we can get a unique ID for a file by concatenating
34354** the device number and the inode number.  But this does not work on VxWorks.
34355** On VxWorks, a unique file id must be based on the canonical filename.
34356**
34357** A pointer to an instance of the following structure can be used as a
34358** unique file ID in VxWorks.  Each instance of this structure contains
34359** a copy of the canonical filename.  There is also a reference count.
34360** The structure is reclaimed when the number of pointers to it drops to
34361** zero.
34362**
34363** There are never very many files open at one time and lookups are not
34364** a performance-critical path, so it is sufficient to put these
34365** structures on a linked list.
34366*/
34367struct vxworksFileId {
34368  struct vxworksFileId *pNext;  /* Next in a list of them all */
34369  int nRef;                     /* Number of references to this one */
34370  int nName;                    /* Length of the zCanonicalName[] string */
34371  char *zCanonicalName;         /* Canonical filename */
34372};
34373
34374#if OS_VXWORKS
34375/*
34376** All unique filenames are held on a linked list headed by this
34377** variable:
34378*/
34379static struct vxworksFileId *vxworksFileList = 0;
34380
34381/*
34382** Simplify a filename into its canonical form
34383** by making the following changes:
34384**
34385**  * removing any trailing and duplicate /
34386**  * convert /./ into just /
34387**  * convert /A/../ where A is any simple name into just /
34388**
34389** Changes are made in-place.  Return the new name length.
34390**
34391** The original filename is in z[0..n-1].  Return the number of
34392** characters in the simplified name.
34393*/
34394static int vxworksSimplifyName(char *z, int n){
34395  int i, j;
34396  while( n>1 && z[n-1]=='/' ){ n--; }
34397  for(i=j=0; i<n; i++){
34398    if( z[i]=='/' ){
34399      if( z[i+1]=='/' ) continue;
34400      if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
34401        i += 1;
34402        continue;
34403      }
34404      if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
34405        while( j>0 && z[j-1]!='/' ){ j--; }
34406        if( j>0 ){ j--; }
34407        i += 2;
34408        continue;
34409      }
34410    }
34411    z[j++] = z[i];
34412  }
34413  z[j] = 0;
34414  return j;
34415}
34416
34417/*
34418** Find a unique file ID for the given absolute pathname.  Return
34419** a pointer to the vxworksFileId object.  This pointer is the unique
34420** file ID.
34421**
34422** The nRef field of the vxworksFileId object is incremented before
34423** the object is returned.  A new vxworksFileId object is created
34424** and added to the global list if necessary.
34425**
34426** If a memory allocation error occurs, return NULL.
34427*/
34428static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
34429  struct vxworksFileId *pNew;         /* search key and new file ID */
34430  struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */
34431  int n;                              /* Length of zAbsoluteName string */
34432
34433  assert( zAbsoluteName[0]=='/' );
34434  n = (int)strlen(zAbsoluteName);
34435  pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) );
34436  if( pNew==0 ) return 0;
34437  pNew->zCanonicalName = (char*)&pNew[1];
34438  memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
34439  n = vxworksSimplifyName(pNew->zCanonicalName, n);
34440
34441  /* Search for an existing entry that matching the canonical name.
34442  ** If found, increment the reference count and return a pointer to
34443  ** the existing file ID.
34444  */
34445  unixEnterMutex();
34446  for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
34447    if( pCandidate->nName==n
34448     && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
34449    ){
34450       sqlite3_free(pNew);
34451       pCandidate->nRef++;
34452       unixLeaveMutex();
34453       return pCandidate;
34454    }
34455  }
34456
34457  /* No match was found.  We will make a new file ID */
34458  pNew->nRef = 1;
34459  pNew->nName = n;
34460  pNew->pNext = vxworksFileList;
34461  vxworksFileList = pNew;
34462  unixLeaveMutex();
34463  return pNew;
34464}
34465
34466/*
34467** Decrement the reference count on a vxworksFileId object.  Free
34468** the object when the reference count reaches zero.
34469*/
34470static void vxworksReleaseFileId(struct vxworksFileId *pId){
34471  unixEnterMutex();
34472  assert( pId->nRef>0 );
34473  pId->nRef--;
34474  if( pId->nRef==0 ){
34475    struct vxworksFileId **pp;
34476    for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
34477    assert( *pp==pId );
34478    *pp = pId->pNext;
34479    sqlite3_free(pId);
34480  }
34481  unixLeaveMutex();
34482}
34483#endif /* OS_VXWORKS */
34484/*************** End of Unique File ID Utility Used By VxWorks ****************
34485******************************************************************************/
34486
34487
34488/******************************************************************************
34489*************************** Posix Advisory Locking ****************************
34490**
34491** POSIX advisory locks are broken by design.  ANSI STD 1003.1 (1996)
34492** section 6.5.2.2 lines 483 through 490 specify that when a process
34493** sets or clears a lock, that operation overrides any prior locks set
34494** by the same process.  It does not explicitly say so, but this implies
34495** that it overrides locks set by the same process using a different
34496** file descriptor.  Consider this test case:
34497**
34498**       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
34499**       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
34500**
34501** Suppose ./file1 and ./file2 are really the same file (because
34502** one is a hard or symbolic link to the other) then if you set
34503** an exclusive lock on fd1, then try to get an exclusive lock
34504** on fd2, it works.  I would have expected the second lock to
34505** fail since there was already a lock on the file due to fd1.
34506** But not so.  Since both locks came from the same process, the
34507** second overrides the first, even though they were on different
34508** file descriptors opened on different file names.
34509**
34510** This means that we cannot use POSIX locks to synchronize file access
34511** among competing threads of the same process.  POSIX locks will work fine
34512** to synchronize access for threads in separate processes, but not
34513** threads within the same process.
34514**
34515** To work around the problem, SQLite has to manage file locks internally
34516** on its own.  Whenever a new database is opened, we have to find the
34517** specific inode of the database file (the inode is determined by the
34518** st_dev and st_ino fields of the stat structure that fstat() fills in)
34519** and check for locks already existing on that inode.  When locks are
34520** created or removed, we have to look at our own internal record of the
34521** locks to see if another thread has previously set a lock on that same
34522** inode.
34523**
34524** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
34525** For VxWorks, we have to use the alternative unique ID system based on
34526** canonical filename and implemented in the previous division.)
34527**
34528** The sqlite3_file structure for POSIX is no longer just an integer file
34529** descriptor.  It is now a structure that holds the integer file
34530** descriptor and a pointer to a structure that describes the internal
34531** locks on the corresponding inode.  There is one locking structure
34532** per inode, so if the same inode is opened twice, both unixFile structures
34533** point to the same locking structure.  The locking structure keeps
34534** a reference count (so we will know when to delete it) and a "cnt"
34535** field that tells us its internal lock status.  cnt==0 means the
34536** file is unlocked.  cnt==-1 means the file has an exclusive lock.
34537** cnt>0 means there are cnt shared locks on the file.
34538**
34539** Any attempt to lock or unlock a file first checks the locking
34540** structure.  The fcntl() system call is only invoked to set a
34541** POSIX lock if the internal lock structure transitions between
34542** a locked and an unlocked state.
34543**
34544** But wait:  there are yet more problems with POSIX advisory locks.
34545**
34546** If you close a file descriptor that points to a file that has locks,
34547** all locks on that file that are owned by the current process are
34548** released.  To work around this problem, each unixInodeInfo object
34549** maintains a count of the number of pending locks on tha inode.
34550** When an attempt is made to close an unixFile, if there are
34551** other unixFile open on the same inode that are holding locks, the call
34552** to close() the file descriptor is deferred until all of the locks clear.
34553** The unixInodeInfo structure keeps a list of file descriptors that need to
34554** be closed and that list is walked (and cleared) when the last lock
34555** clears.
34556**
34557** Yet another problem:  LinuxThreads do not play well with posix locks.
34558**
34559** Many older versions of linux use the LinuxThreads library which is
34560** not posix compliant.  Under LinuxThreads, a lock created by thread
34561** A cannot be modified or overridden by a different thread B.
34562** Only thread A can modify the lock.  Locking behavior is correct
34563** if the appliation uses the newer Native Posix Thread Library (NPTL)
34564** on linux - with NPTL a lock created by thread A can override locks
34565** in thread B.  But there is no way to know at compile-time which
34566** threading library is being used.  So there is no way to know at
34567** compile-time whether or not thread A can override locks on thread B.
34568** One has to do a run-time check to discover the behavior of the
34569** current process.
34570**
34571** SQLite used to support LinuxThreads.  But support for LinuxThreads
34572** was dropped beginning with version 3.7.0.  SQLite will still work with
34573** LinuxThreads provided that (1) there is no more than one connection
34574** per database file in the same process and (2) database connections
34575** do not move across threads.
34576*/
34577
34578/*
34579** An instance of the following structure serves as the key used
34580** to locate a particular unixInodeInfo object.
34581*/
34582struct unixFileId {
34583  dev_t dev;                  /* Device number */
34584#if OS_VXWORKS
34585  struct vxworksFileId *pId;  /* Unique file ID for vxworks. */
34586#else
34587  /* We are told that some versions of Android contain a bug that
34588  ** sizes ino_t at only 32-bits instead of 64-bits. (See
34589  ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
34590  ** To work around this, always allocate 64-bits for the inode number.
34591  ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
34592  ** but that should not be a big deal. */
34593  /* WAS:  ino_t ino;   */
34594  u64 ino;                   /* Inode number */
34595#endif
34596};
34597
34598/*
34599** An instance of the following structure is allocated for each open
34600** inode.
34601**
34602** A single inode can have multiple file descriptors, so each unixFile
34603** structure contains a pointer to an instance of this object and this
34604** object keeps a count of the number of unixFile pointing to it.
34605**
34606** Mutex rules:
34607**
34608**  (1) Only the pLockMutex mutex must be held in order to read or write
34609**      any of the locking fields:
34610**          nShared, nLock, eFileLock, bProcessLock, pUnused
34611**
34612**  (2) When nRef>0, then the following fields are unchanging and can
34613**      be read (but not written) without holding any mutex:
34614**          fileId, pLockMutex
34615**
34616**  (3) With the exceptions above, all the fields may only be read
34617**      or written while holding the global unixBigLock mutex.
34618**
34619** Deadlock prevention:  The global unixBigLock mutex may not
34620** be acquired while holding the pLockMutex mutex.  If both unixBigLock
34621** and pLockMutex are needed, then unixBigLock must be acquired first.
34622*/
34623struct unixInodeInfo {
34624  struct unixFileId fileId;       /* The lookup key */
34625  sqlite3_mutex *pLockMutex;      /* Hold this mutex for... */
34626  int nShared;                      /* Number of SHARED locks held */
34627  int nLock;                        /* Number of outstanding file locks */
34628  unsigned char eFileLock;          /* One of SHARED_LOCK, RESERVED_LOCK etc. */
34629  unsigned char bProcessLock;       /* An exclusive process lock is held */
34630  UnixUnusedFd *pUnused;            /* Unused file descriptors to close */
34631  int nRef;                       /* Number of pointers to this structure */
34632  unixShmNode *pShmNode;          /* Shared memory associated with this inode */
34633  unixInodeInfo *pNext;           /* List of all unixInodeInfo objects */
34634  unixInodeInfo *pPrev;           /*    .... doubly linked */
34635#if SQLITE_ENABLE_LOCKING_STYLE
34636  unsigned long long sharedByte;  /* for AFP simulated shared lock */
34637#endif
34638#if OS_VXWORKS
34639  sem_t *pSem;                    /* Named POSIX semaphore */
34640  char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
34641#endif
34642};
34643
34644/*
34645** A lists of all unixInodeInfo objects.
34646**
34647** Must hold unixBigLock in order to read or write this variable.
34648*/
34649static unixInodeInfo *inodeList = 0;  /* All unixInodeInfo objects */
34650
34651#ifdef SQLITE_DEBUG
34652/*
34653** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not.
34654** This routine is used only within assert() to help verify correct mutex
34655** usage.
34656*/
34657int unixFileMutexHeld(unixFile *pFile){
34658  assert( pFile->pInode );
34659  return sqlite3_mutex_held(pFile->pInode->pLockMutex);
34660}
34661int unixFileMutexNotheld(unixFile *pFile){
34662  assert( pFile->pInode );
34663  return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
34664}
34665#endif
34666
34667/*
34668**
34669** This function - unixLogErrorAtLine(), is only ever called via the macro
34670** unixLogError().
34671**
34672** It is invoked after an error occurs in an OS function and errno has been
34673** set. It logs a message using sqlite3_log() containing the current value of
34674** errno and, if possible, the human-readable equivalent from strerror() or
34675** strerror_r().
34676**
34677** The first argument passed to the macro should be the error code that
34678** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
34679** The two subsequent arguments should be the name of the OS function that
34680** failed (e.g. "unlink", "open") and the associated file-system path,
34681** if any.
34682*/
34683#define unixLogError(a,b,c)     unixLogErrorAtLine(a,b,c,__LINE__)
34684static int unixLogErrorAtLine(
34685  int errcode,                    /* SQLite error code */
34686  const char *zFunc,              /* Name of OS function that failed */
34687  const char *zPath,              /* File path associated with error */
34688  int iLine                       /* Source line number where error occurred */
34689){
34690  char *zErr;                     /* Message from strerror() or equivalent */
34691  int iErrno = errno;             /* Saved syscall error number */
34692
34693  /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
34694  ** the strerror() function to obtain the human-readable error message
34695  ** equivalent to errno. Otherwise, use strerror_r().
34696  */
34697#if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
34698  char aErr[80];
34699  memset(aErr, 0, sizeof(aErr));
34700  zErr = aErr;
34701
34702  /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
34703  ** assume that the system provides the GNU version of strerror_r() that
34704  ** returns a pointer to a buffer containing the error message. That pointer
34705  ** may point to aErr[], or it may point to some static storage somewhere.
34706  ** Otherwise, assume that the system provides the POSIX version of
34707  ** strerror_r(), which always writes an error message into aErr[].
34708  **
34709  ** If the code incorrectly assumes that it is the POSIX version that is
34710  ** available, the error message will often be an empty string. Not a
34711  ** huge problem. Incorrectly concluding that the GNU version is available
34712  ** could lead to a segfault though.
34713  */
34714#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
34715  zErr =
34716# endif
34717  strerror_r(iErrno, aErr, sizeof(aErr)-1);
34718
34719#elif SQLITE_THREADSAFE
34720  /* This is a threadsafe build, but strerror_r() is not available. */
34721  zErr = "";
34722#else
34723  /* Non-threadsafe build, use strerror(). */
34724  zErr = strerror(iErrno);
34725#endif
34726
34727  if( zPath==0 ) zPath = "";
34728  sqlite3_log(errcode,
34729      "os_unix.c:%d: (%d) %s(%s) - %s",
34730      iLine, iErrno, zFunc, zPath, zErr
34731  );
34732
34733  return errcode;
34734}
34735
34736/*
34737** Close a file descriptor.
34738**
34739** We assume that close() almost always works, since it is only in a
34740** very sick application or on a very sick platform that it might fail.
34741** If it does fail, simply leak the file descriptor, but do log the
34742** error.
34743**
34744** Note that it is not safe to retry close() after EINTR since the
34745** file descriptor might have already been reused by another thread.
34746** So we don't even try to recover from an EINTR.  Just log the error
34747** and move on.
34748*/
34749static void robust_close(unixFile *pFile, int h, int lineno){
34750  if( osClose(h) ){
34751    unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
34752                       pFile ? pFile->zPath : 0, lineno);
34753  }
34754}
34755
34756/*
34757** Set the pFile->lastErrno.  Do this in a subroutine as that provides
34758** a convenient place to set a breakpoint.
34759*/
34760static void storeLastErrno(unixFile *pFile, int error){
34761  pFile->lastErrno = error;
34762}
34763
34764/*
34765** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
34766*/
34767static void closePendingFds(unixFile *pFile){
34768  unixInodeInfo *pInode = pFile->pInode;
34769  UnixUnusedFd *p;
34770  UnixUnusedFd *pNext;
34771  assert( unixFileMutexHeld(pFile) );
34772  for(p=pInode->pUnused; p; p=pNext){
34773    pNext = p->pNext;
34774    robust_close(pFile, p->fd, __LINE__);
34775    sqlite3_free(p);
34776  }
34777  pInode->pUnused = 0;
34778}
34779
34780/*
34781** Release a unixInodeInfo structure previously allocated by findInodeInfo().
34782**
34783** The global mutex must be held when this routine is called, but the mutex
34784** on the inode being deleted must NOT be held.
34785*/
34786static void releaseInodeInfo(unixFile *pFile){
34787  unixInodeInfo *pInode = pFile->pInode;
34788  assert( unixMutexHeld() );
34789  assert( unixFileMutexNotheld(pFile) );
34790  if( ALWAYS(pInode) ){
34791    pInode->nRef--;
34792    if( pInode->nRef==0 ){
34793      assert( pInode->pShmNode==0 );
34794      sqlite3_mutex_enter(pInode->pLockMutex);
34795      closePendingFds(pFile);
34796      sqlite3_mutex_leave(pInode->pLockMutex);
34797      if( pInode->pPrev ){
34798        assert( pInode->pPrev->pNext==pInode );
34799        pInode->pPrev->pNext = pInode->pNext;
34800      }else{
34801        assert( inodeList==pInode );
34802        inodeList = pInode->pNext;
34803      }
34804      if( pInode->pNext ){
34805        assert( pInode->pNext->pPrev==pInode );
34806        pInode->pNext->pPrev = pInode->pPrev;
34807      }
34808      sqlite3_mutex_free(pInode->pLockMutex);
34809      sqlite3_free(pInode);
34810    }
34811  }
34812}
34813
34814/*
34815** Given a file descriptor, locate the unixInodeInfo object that
34816** describes that file descriptor.  Create a new one if necessary.  The
34817** return value might be uninitialized if an error occurs.
34818**
34819** The global mutex must held when calling this routine.
34820**
34821** Return an appropriate error code.
34822*/
34823static int findInodeInfo(
34824  unixFile *pFile,               /* Unix file with file desc used in the key */
34825  unixInodeInfo **ppInode        /* Return the unixInodeInfo object here */
34826){
34827  int rc;                        /* System call return code */
34828  int fd;                        /* The file descriptor for pFile */
34829  struct unixFileId fileId;      /* Lookup key for the unixInodeInfo */
34830  struct stat statbuf;           /* Low-level file information */
34831  unixInodeInfo *pInode = 0;     /* Candidate unixInodeInfo object */
34832
34833  assert( unixMutexHeld() );
34834
34835  /* Get low-level information about the file that we can used to
34836  ** create a unique name for the file.
34837  */
34838  fd = pFile->h;
34839  rc = osFstat(fd, &statbuf);
34840  if( rc!=0 ){
34841    storeLastErrno(pFile, errno);
34842#if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS)
34843    if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
34844#endif
34845    return SQLITE_IOERR;
34846  }
34847
34848#ifdef __APPLE__
34849  /* On OS X on an msdos filesystem, the inode number is reported
34850  ** incorrectly for zero-size files.  See ticket #3260.  To work
34851  ** around this problem (we consider it a bug in OS X, not SQLite)
34852  ** we always increase the file size to 1 by writing a single byte
34853  ** prior to accessing the inode number.  The one byte written is
34854  ** an ASCII 'S' character which also happens to be the first byte
34855  ** in the header of every SQLite database.  In this way, if there
34856  ** is a race condition such that another thread has already populated
34857  ** the first page of the database, no damage is done.
34858  */
34859  if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
34860    do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
34861    if( rc!=1 ){
34862      storeLastErrno(pFile, errno);
34863      return SQLITE_IOERR;
34864    }
34865    rc = osFstat(fd, &statbuf);
34866    if( rc!=0 ){
34867      storeLastErrno(pFile, errno);
34868      return SQLITE_IOERR;
34869    }
34870  }
34871#endif
34872
34873  memset(&fileId, 0, sizeof(fileId));
34874  fileId.dev = statbuf.st_dev;
34875#if OS_VXWORKS
34876  fileId.pId = pFile->pId;
34877#else
34878  fileId.ino = (u64)statbuf.st_ino;
34879#endif
34880  assert( unixMutexHeld() );
34881  pInode = inodeList;
34882  while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
34883    pInode = pInode->pNext;
34884  }
34885  if( pInode==0 ){
34886    pInode = sqlite3_malloc64( sizeof(*pInode) );
34887    if( pInode==0 ){
34888      return SQLITE_NOMEM_BKPT;
34889    }
34890    memset(pInode, 0, sizeof(*pInode));
34891    memcpy(&pInode->fileId, &fileId, sizeof(fileId));
34892    if( sqlite3GlobalConfig.bCoreMutex ){
34893      pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
34894      if( pInode->pLockMutex==0 ){
34895        sqlite3_free(pInode);
34896        return SQLITE_NOMEM_BKPT;
34897      }
34898    }
34899    pInode->nRef = 1;
34900    assert( unixMutexHeld() );
34901    pInode->pNext = inodeList;
34902    pInode->pPrev = 0;
34903    if( inodeList ) inodeList->pPrev = pInode;
34904    inodeList = pInode;
34905  }else{
34906    pInode->nRef++;
34907  }
34908  *ppInode = pInode;
34909  return SQLITE_OK;
34910}
34911
34912/*
34913** Return TRUE if pFile has been renamed or unlinked since it was first opened.
34914*/
34915static int fileHasMoved(unixFile *pFile){
34916#if OS_VXWORKS
34917  return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
34918#else
34919  struct stat buf;
34920  return pFile->pInode!=0 &&
34921      (osStat(pFile->zPath, &buf)!=0
34922         || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
34923#endif
34924}
34925
34926
34927/*
34928** Check a unixFile that is a database.  Verify the following:
34929**
34930** (1) There is exactly one hard link on the file
34931** (2) The file is not a symbolic link
34932** (3) The file has not been renamed or unlinked
34933**
34934** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
34935*/
34936static void verifyDbFile(unixFile *pFile){
34937  struct stat buf;
34938  int rc;
34939
34940  /* These verifications occurs for the main database only */
34941  if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
34942
34943  rc = osFstat(pFile->h, &buf);
34944  if( rc!=0 ){
34945    sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
34946    return;
34947  }
34948  if( buf.st_nlink==0 ){
34949    sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
34950    return;
34951  }
34952  if( buf.st_nlink>1 ){
34953    sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
34954    return;
34955  }
34956  if( fileHasMoved(pFile) ){
34957    sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
34958    return;
34959  }
34960}
34961
34962
34963/*
34964** This routine checks if there is a RESERVED lock held on the specified
34965** file by this or any other process. If such a lock is held, set *pResOut
34966** to a non-zero value otherwise *pResOut is set to zero.  The return value
34967** is set to SQLITE_OK unless an I/O error occurs during lock checking.
34968*/
34969static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
34970  int rc = SQLITE_OK;
34971  int reserved = 0;
34972  unixFile *pFile = (unixFile*)id;
34973
34974  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
34975
34976  assert( pFile );
34977  assert( pFile->eFileLock<=SHARED_LOCK );
34978  sqlite3_mutex_enter(pFile->pInode->pLockMutex);
34979
34980  /* Check if a thread in this process holds such a lock */
34981  if( pFile->pInode->eFileLock>SHARED_LOCK ){
34982    reserved = 1;
34983  }
34984
34985  /* Otherwise see if some other process holds it.
34986  */
34987#ifndef __DJGPP__
34988  if( !reserved && !pFile->pInode->bProcessLock ){
34989    struct flock lock;
34990    lock.l_whence = SEEK_SET;
34991    lock.l_start = RESERVED_BYTE;
34992    lock.l_len = 1;
34993    lock.l_type = F_WRLCK;
34994    if( osFcntl(pFile->h, F_GETLK, &lock) ){
34995      rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
34996      storeLastErrno(pFile, errno);
34997    } else if( lock.l_type!=F_UNLCK ){
34998      reserved = 1;
34999    }
35000  }
35001#endif
35002
35003  sqlite3_mutex_leave(pFile->pInode->pLockMutex);
35004  OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
35005
35006  *pResOut = reserved;
35007  return rc;
35008}
35009
35010/*
35011** Set a posix-advisory-lock.
35012**
35013** There are two versions of this routine.  If compiled with
35014** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
35015** which is a pointer to a unixFile.  If the unixFile->iBusyTimeout
35016** value is set, then it is the number of milliseconds to wait before
35017** failing the lock.  The iBusyTimeout value is always reset back to
35018** zero on each call.
35019**
35020** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
35021** attempt to set the lock.
35022*/
35023#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
35024# define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x)
35025#else
35026static int osSetPosixAdvisoryLock(
35027  int h,                /* The file descriptor on which to take the lock */
35028  struct flock *pLock,  /* The description of the lock */
35029  unixFile *pFile       /* Structure holding timeout value */
35030){
35031  int tm = pFile->iBusyTimeout;
35032  int rc = osFcntl(h,F_SETLK,pLock);
35033  while( rc<0 && tm>0 ){
35034    /* On systems that support some kind of blocking file lock with a timeout,
35035    ** make appropriate changes here to invoke that blocking file lock.  On
35036    ** generic posix, however, there is no such API.  So we simply try the
35037    ** lock once every millisecond until either the timeout expires, or until
35038    ** the lock is obtained. */
35039    usleep(1000);
35040    rc = osFcntl(h,F_SETLK,pLock);
35041    tm--;
35042  }
35043  return rc;
35044}
35045#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
35046
35047
35048/*
35049** Attempt to set a system-lock on the file pFile.  The lock is
35050** described by pLock.
35051**
35052** If the pFile was opened read/write from unix-excl, then the only lock
35053** ever obtained is an exclusive lock, and it is obtained exactly once
35054** the first time any lock is attempted.  All subsequent system locking
35055** operations become no-ops.  Locking operations still happen internally,
35056** in order to coordinate access between separate database connections
35057** within this process, but all of that is handled in memory and the
35058** operating system does not participate.
35059**
35060** This function is a pass-through to fcntl(F_SETLK) if pFile is using
35061** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
35062** and is read-only.
35063**
35064** Zero is returned if the call completes successfully, or -1 if a call
35065** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
35066*/
35067static int unixFileLock(unixFile *pFile, struct flock *pLock){
35068  int rc;
35069  unixInodeInfo *pInode = pFile->pInode;
35070  assert( pInode!=0 );
35071  assert( sqlite3_mutex_held(pInode->pLockMutex) );
35072  if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
35073    if( pInode->bProcessLock==0 ){
35074      struct flock lock;
35075      assert( pInode->nLock==0 );
35076      lock.l_whence = SEEK_SET;
35077      lock.l_start = SHARED_FIRST;
35078      lock.l_len = SHARED_SIZE;
35079      lock.l_type = F_WRLCK;
35080      rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
35081      if( rc<0 ) return rc;
35082      pInode->bProcessLock = 1;
35083      pInode->nLock++;
35084    }else{
35085      rc = 0;
35086    }
35087  }else{
35088    rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
35089  }
35090  return rc;
35091}
35092
35093/*
35094** Lock the file with the lock specified by parameter eFileLock - one
35095** of the following:
35096**
35097**     (1) SHARED_LOCK
35098**     (2) RESERVED_LOCK
35099**     (3) PENDING_LOCK
35100**     (4) EXCLUSIVE_LOCK
35101**
35102** Sometimes when requesting one lock state, additional lock states
35103** are inserted in between.  The locking might fail on one of the later
35104** transitions leaving the lock state different from what it started but
35105** still short of its goal.  The following chart shows the allowed
35106** transitions and the inserted intermediate states:
35107**
35108**    UNLOCKED -> SHARED
35109**    SHARED -> RESERVED
35110**    SHARED -> (PENDING) -> EXCLUSIVE
35111**    RESERVED -> (PENDING) -> EXCLUSIVE
35112**    PENDING -> EXCLUSIVE
35113**
35114** This routine will only increase a lock.  Use the sqlite3OsUnlock()
35115** routine to lower a locking level.
35116*/
35117static int unixLock(sqlite3_file *id, int eFileLock){
35118  /* The following describes the implementation of the various locks and
35119  ** lock transitions in terms of the POSIX advisory shared and exclusive
35120  ** lock primitives (called read-locks and write-locks below, to avoid
35121  ** confusion with SQLite lock names). The algorithms are complicated
35122  ** slightly in order to be compatible with Windows95 systems simultaneously
35123  ** accessing the same database file, in case that is ever required.
35124  **
35125  ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
35126  ** byte', each single bytes at well known offsets, and the 'shared byte
35127  ** range', a range of 510 bytes at a well known offset.
35128  **
35129  ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
35130  ** byte'.  If this is successful, 'shared byte range' is read-locked
35131  ** and the lock on the 'pending byte' released.  (Legacy note:  When
35132  ** SQLite was first developed, Windows95 systems were still very common,
35133  ** and Widnows95 lacks a shared-lock capability.  So on Windows95, a
35134  ** single randomly selected by from the 'shared byte range' is locked.
35135  ** Windows95 is now pretty much extinct, but this work-around for the
35136  ** lack of shared-locks on Windows95 lives on, for backwards
35137  ** compatibility.)
35138  **
35139  ** A process may only obtain a RESERVED lock after it has a SHARED lock.
35140  ** A RESERVED lock is implemented by grabbing a write-lock on the
35141  ** 'reserved byte'.
35142  **
35143  ** A process may only obtain a PENDING lock after it has obtained a
35144  ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
35145  ** on the 'pending byte'. This ensures that no new SHARED locks can be
35146  ** obtained, but existing SHARED locks are allowed to persist. A process
35147  ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
35148  ** This property is used by the algorithm for rolling back a journal file
35149  ** after a crash.
35150  **
35151  ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
35152  ** implemented by obtaining a write-lock on the entire 'shared byte
35153  ** range'. Since all other locks require a read-lock on one of the bytes
35154  ** within this range, this ensures that no other locks are held on the
35155  ** database.
35156  */
35157  int rc = SQLITE_OK;
35158  unixFile *pFile = (unixFile*)id;
35159  unixInodeInfo *pInode;
35160  struct flock lock;
35161  int tErrno = 0;
35162
35163  assert( pFile );
35164  OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
35165      azFileLock(eFileLock), azFileLock(pFile->eFileLock),
35166      azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
35167      osGetpid(0)));
35168
35169  /* If there is already a lock of this type or more restrictive on the
35170  ** unixFile, do nothing. Don't use the end_lock: exit path, as
35171  ** unixEnterMutex() hasn't been called yet.
35172  */
35173  if( pFile->eFileLock>=eFileLock ){
35174    OSTRACE(("LOCK    %d %s ok (already held) (unix)\n", pFile->h,
35175            azFileLock(eFileLock)));
35176    return SQLITE_OK;
35177  }
35178
35179  /* Make sure the locking sequence is correct.
35180  **  (1) We never move from unlocked to anything higher than shared lock.
35181  **  (2) SQLite never explicitly requests a pendig lock.
35182  **  (3) A shared lock is always held when a reserve lock is requested.
35183  */
35184  assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
35185  assert( eFileLock!=PENDING_LOCK );
35186  assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
35187
35188  /* This mutex is needed because pFile->pInode is shared across threads
35189  */
35190  pInode = pFile->pInode;
35191  sqlite3_mutex_enter(pInode->pLockMutex);
35192
35193  /* If some thread using this PID has a lock via a different unixFile*
35194  ** handle that precludes the requested lock, return BUSY.
35195  */
35196  if( (pFile->eFileLock!=pInode->eFileLock &&
35197          (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
35198  ){
35199    rc = SQLITE_BUSY;
35200    goto end_lock;
35201  }
35202
35203  /* If a SHARED lock is requested, and some thread using this PID already
35204  ** has a SHARED or RESERVED lock, then increment reference counts and
35205  ** return SQLITE_OK.
35206  */
35207  if( eFileLock==SHARED_LOCK &&
35208      (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
35209    assert( eFileLock==SHARED_LOCK );
35210    assert( pFile->eFileLock==0 );
35211    assert( pInode->nShared>0 );
35212    pFile->eFileLock = SHARED_LOCK;
35213    pInode->nShared++;
35214    pInode->nLock++;
35215    goto end_lock;
35216  }
35217
35218
35219  /* A PENDING lock is needed before acquiring a SHARED lock and before
35220  ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
35221  ** be released.
35222  */
35223  lock.l_len = 1L;
35224  lock.l_whence = SEEK_SET;
35225  if( eFileLock==SHARED_LOCK
35226      || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
35227  ){
35228    lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
35229    lock.l_start = PENDING_BYTE;
35230    if( unixFileLock(pFile, &lock) ){
35231      tErrno = errno;
35232      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
35233      if( rc!=SQLITE_BUSY ){
35234        storeLastErrno(pFile, tErrno);
35235      }
35236      goto end_lock;
35237    }
35238  }
35239
35240
35241  /* If control gets to this point, then actually go ahead and make
35242  ** operating system calls for the specified lock.
35243  */
35244  if( eFileLock==SHARED_LOCK ){
35245    assert( pInode->nShared==0 );
35246    assert( pInode->eFileLock==0 );
35247    assert( rc==SQLITE_OK );
35248
35249    /* Now get the read-lock */
35250    lock.l_start = SHARED_FIRST;
35251    lock.l_len = SHARED_SIZE;
35252    if( unixFileLock(pFile, &lock) ){
35253      tErrno = errno;
35254      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
35255    }
35256
35257    /* Drop the temporary PENDING lock */
35258    lock.l_start = PENDING_BYTE;
35259    lock.l_len = 1L;
35260    lock.l_type = F_UNLCK;
35261    if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
35262      /* This could happen with a network mount */
35263      tErrno = errno;
35264      rc = SQLITE_IOERR_UNLOCK;
35265    }
35266
35267    if( rc ){
35268      if( rc!=SQLITE_BUSY ){
35269        storeLastErrno(pFile, tErrno);
35270      }
35271      goto end_lock;
35272    }else{
35273      pFile->eFileLock = SHARED_LOCK;
35274      pInode->nLock++;
35275      pInode->nShared = 1;
35276    }
35277  }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
35278    /* We are trying for an exclusive lock but another thread in this
35279    ** same process is still holding a shared lock. */
35280    rc = SQLITE_BUSY;
35281  }else{
35282    /* The request was for a RESERVED or EXCLUSIVE lock.  It is
35283    ** assumed that there is a SHARED or greater lock on the file
35284    ** already.
35285    */
35286    assert( 0!=pFile->eFileLock );
35287    lock.l_type = F_WRLCK;
35288
35289    assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
35290    if( eFileLock==RESERVED_LOCK ){
35291      lock.l_start = RESERVED_BYTE;
35292      lock.l_len = 1L;
35293    }else{
35294      lock.l_start = SHARED_FIRST;
35295      lock.l_len = SHARED_SIZE;
35296    }
35297
35298    if( unixFileLock(pFile, &lock) ){
35299      tErrno = errno;
35300      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
35301      if( rc!=SQLITE_BUSY ){
35302        storeLastErrno(pFile, tErrno);
35303      }
35304    }
35305  }
35306
35307
35308#ifdef SQLITE_DEBUG
35309  /* Set up the transaction-counter change checking flags when
35310  ** transitioning from a SHARED to a RESERVED lock.  The change
35311  ** from SHARED to RESERVED marks the beginning of a normal
35312  ** write operation (not a hot journal rollback).
35313  */
35314  if( rc==SQLITE_OK
35315   && pFile->eFileLock<=SHARED_LOCK
35316   && eFileLock==RESERVED_LOCK
35317  ){
35318    pFile->transCntrChng = 0;
35319    pFile->dbUpdate = 0;
35320    pFile->inNormalWrite = 1;
35321  }
35322#endif
35323
35324
35325  if( rc==SQLITE_OK ){
35326    pFile->eFileLock = eFileLock;
35327    pInode->eFileLock = eFileLock;
35328  }else if( eFileLock==EXCLUSIVE_LOCK ){
35329    pFile->eFileLock = PENDING_LOCK;
35330    pInode->eFileLock = PENDING_LOCK;
35331  }
35332
35333end_lock:
35334  sqlite3_mutex_leave(pInode->pLockMutex);
35335  OSTRACE(("LOCK    %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
35336      rc==SQLITE_OK ? "ok" : "failed"));
35337  return rc;
35338}
35339
35340/*
35341** Add the file descriptor used by file handle pFile to the corresponding
35342** pUnused list.
35343*/
35344static void setPendingFd(unixFile *pFile){
35345  unixInodeInfo *pInode = pFile->pInode;
35346  UnixUnusedFd *p = pFile->pPreallocatedUnused;
35347  assert( unixFileMutexHeld(pFile) );
35348  p->pNext = pInode->pUnused;
35349  pInode->pUnused = p;
35350  pFile->h = -1;
35351  pFile->pPreallocatedUnused = 0;
35352}
35353
35354/*
35355** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
35356** must be either NO_LOCK or SHARED_LOCK.
35357**
35358** If the locking level of the file descriptor is already at or below
35359** the requested locking level, this routine is a no-op.
35360**
35361** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
35362** the byte range is divided into 2 parts and the first part is unlocked then
35363** set to a read lock, then the other part is simply unlocked.  This works
35364** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
35365** remove the write lock on a region when a read lock is set.
35366*/
35367static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
35368  unixFile *pFile = (unixFile*)id;
35369  unixInodeInfo *pInode;
35370  struct flock lock;
35371  int rc = SQLITE_OK;
35372
35373  assert( pFile );
35374  OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
35375      pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
35376      osGetpid(0)));
35377
35378  assert( eFileLock<=SHARED_LOCK );
35379  if( pFile->eFileLock<=eFileLock ){
35380    return SQLITE_OK;
35381  }
35382  pInode = pFile->pInode;
35383  sqlite3_mutex_enter(pInode->pLockMutex);
35384  assert( pInode->nShared!=0 );
35385  if( pFile->eFileLock>SHARED_LOCK ){
35386    assert( pInode->eFileLock==pFile->eFileLock );
35387
35388#ifdef SQLITE_DEBUG
35389    /* When reducing a lock such that other processes can start
35390    ** reading the database file again, make sure that the
35391    ** transaction counter was updated if any part of the database
35392    ** file changed.  If the transaction counter is not updated,
35393    ** other connections to the same file might not realize that
35394    ** the file has changed and hence might not know to flush their
35395    ** cache.  The use of a stale cache can lead to database corruption.
35396    */
35397    pFile->inNormalWrite = 0;
35398#endif
35399
35400    /* downgrading to a shared lock on NFS involves clearing the write lock
35401    ** before establishing the readlock - to avoid a race condition we downgrade
35402    ** the lock in 2 blocks, so that part of the range will be covered by a
35403    ** write lock until the rest is covered by a read lock:
35404    **  1:   [WWWWW]
35405    **  2:   [....W]
35406    **  3:   [RRRRW]
35407    **  4:   [RRRR.]
35408    */
35409    if( eFileLock==SHARED_LOCK ){
35410#if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
35411      (void)handleNFSUnlock;
35412      assert( handleNFSUnlock==0 );
35413#endif
35414#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
35415      if( handleNFSUnlock ){
35416        int tErrno;               /* Error code from system call errors */
35417        off_t divSize = SHARED_SIZE - 1;
35418
35419        lock.l_type = F_UNLCK;
35420        lock.l_whence = SEEK_SET;
35421        lock.l_start = SHARED_FIRST;
35422        lock.l_len = divSize;
35423        if( unixFileLock(pFile, &lock)==(-1) ){
35424          tErrno = errno;
35425          rc = SQLITE_IOERR_UNLOCK;
35426          storeLastErrno(pFile, tErrno);
35427          goto end_unlock;
35428        }
35429        lock.l_type = F_RDLCK;
35430        lock.l_whence = SEEK_SET;
35431        lock.l_start = SHARED_FIRST;
35432        lock.l_len = divSize;
35433        if( unixFileLock(pFile, &lock)==(-1) ){
35434          tErrno = errno;
35435          rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
35436          if( IS_LOCK_ERROR(rc) ){
35437            storeLastErrno(pFile, tErrno);
35438          }
35439          goto end_unlock;
35440        }
35441        lock.l_type = F_UNLCK;
35442        lock.l_whence = SEEK_SET;
35443        lock.l_start = SHARED_FIRST+divSize;
35444        lock.l_len = SHARED_SIZE-divSize;
35445        if( unixFileLock(pFile, &lock)==(-1) ){
35446          tErrno = errno;
35447          rc = SQLITE_IOERR_UNLOCK;
35448          storeLastErrno(pFile, tErrno);
35449          goto end_unlock;
35450        }
35451      }else
35452#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
35453      {
35454        lock.l_type = F_RDLCK;
35455        lock.l_whence = SEEK_SET;
35456        lock.l_start = SHARED_FIRST;
35457        lock.l_len = SHARED_SIZE;
35458        if( unixFileLock(pFile, &lock) ){
35459          /* In theory, the call to unixFileLock() cannot fail because another
35460          ** process is holding an incompatible lock. If it does, this
35461          ** indicates that the other process is not following the locking
35462          ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
35463          ** SQLITE_BUSY would confuse the upper layer (in practice it causes
35464          ** an assert to fail). */
35465          rc = SQLITE_IOERR_RDLOCK;
35466          storeLastErrno(pFile, errno);
35467          goto end_unlock;
35468        }
35469      }
35470    }
35471    lock.l_type = F_UNLCK;
35472    lock.l_whence = SEEK_SET;
35473    lock.l_start = PENDING_BYTE;
35474    lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
35475    if( unixFileLock(pFile, &lock)==0 ){
35476      pInode->eFileLock = SHARED_LOCK;
35477    }else{
35478      rc = SQLITE_IOERR_UNLOCK;
35479      storeLastErrno(pFile, errno);
35480      goto end_unlock;
35481    }
35482  }
35483  if( eFileLock==NO_LOCK ){
35484    /* Decrement the shared lock counter.  Release the lock using an
35485    ** OS call only when all threads in this same process have released
35486    ** the lock.
35487    */
35488    pInode->nShared--;
35489    if( pInode->nShared==0 ){
35490      lock.l_type = F_UNLCK;
35491      lock.l_whence = SEEK_SET;
35492      lock.l_start = lock.l_len = 0L;
35493      if( unixFileLock(pFile, &lock)==0 ){
35494        pInode->eFileLock = NO_LOCK;
35495      }else{
35496        rc = SQLITE_IOERR_UNLOCK;
35497        storeLastErrno(pFile, errno);
35498        pInode->eFileLock = NO_LOCK;
35499        pFile->eFileLock = NO_LOCK;
35500      }
35501    }
35502
35503    /* Decrement the count of locks against this same file.  When the
35504    ** count reaches zero, close any other file descriptors whose close
35505    ** was deferred because of outstanding locks.
35506    */
35507    pInode->nLock--;
35508    assert( pInode->nLock>=0 );
35509    if( pInode->nLock==0 ) closePendingFds(pFile);
35510  }
35511
35512end_unlock:
35513  sqlite3_mutex_leave(pInode->pLockMutex);
35514  if( rc==SQLITE_OK ){
35515    pFile->eFileLock = eFileLock;
35516  }
35517  return rc;
35518}
35519
35520/*
35521** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
35522** must be either NO_LOCK or SHARED_LOCK.
35523**
35524** If the locking level of the file descriptor is already at or below
35525** the requested locking level, this routine is a no-op.
35526*/
35527static int unixUnlock(sqlite3_file *id, int eFileLock){
35528#if SQLITE_MAX_MMAP_SIZE>0
35529  assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
35530#endif
35531  return posixUnlock(id, eFileLock, 0);
35532}
35533
35534#if SQLITE_MAX_MMAP_SIZE>0
35535static int unixMapfile(unixFile *pFd, i64 nByte);
35536static void unixUnmapfile(unixFile *pFd);
35537#endif
35538
35539/*
35540** This function performs the parts of the "close file" operation
35541** common to all locking schemes. It closes the directory and file
35542** handles, if they are valid, and sets all fields of the unixFile
35543** structure to 0.
35544**
35545** It is *not* necessary to hold the mutex when this routine is called,
35546** even on VxWorks.  A mutex will be acquired on VxWorks by the
35547** vxworksReleaseFileId() routine.
35548*/
35549static int closeUnixFile(sqlite3_file *id){
35550  unixFile *pFile = (unixFile*)id;
35551#if SQLITE_MAX_MMAP_SIZE>0
35552  unixUnmapfile(pFile);
35553#endif
35554  if( pFile->h>=0 ){
35555    robust_close(pFile, pFile->h, __LINE__);
35556    pFile->h = -1;
35557  }
35558#if OS_VXWORKS
35559  if( pFile->pId ){
35560    if( pFile->ctrlFlags & UNIXFILE_DELETE ){
35561      osUnlink(pFile->pId->zCanonicalName);
35562    }
35563    vxworksReleaseFileId(pFile->pId);
35564    pFile->pId = 0;
35565  }
35566#endif
35567#ifdef SQLITE_UNLINK_AFTER_CLOSE
35568  if( pFile->ctrlFlags & UNIXFILE_DELETE ){
35569    osUnlink(pFile->zPath);
35570    sqlite3_free(*(char**)&pFile->zPath);
35571    pFile->zPath = 0;
35572  }
35573#endif
35574  OSTRACE(("CLOSE   %-3d\n", pFile->h));
35575  OpenCounter(-1);
35576  sqlite3_free(pFile->pPreallocatedUnused);
35577  memset(pFile, 0, sizeof(unixFile));
35578  return SQLITE_OK;
35579}
35580
35581/*
35582** Close a file.
35583*/
35584static int unixClose(sqlite3_file *id){
35585  int rc = SQLITE_OK;
35586  unixFile *pFile = (unixFile *)id;
35587  unixInodeInfo *pInode = pFile->pInode;
35588
35589  assert( pInode!=0 );
35590  verifyDbFile(pFile);
35591  unixUnlock(id, NO_LOCK);
35592  assert( unixFileMutexNotheld(pFile) );
35593  unixEnterMutex();
35594
35595  /* unixFile.pInode is always valid here. Otherwise, a different close
35596  ** routine (e.g. nolockClose()) would be called instead.
35597  */
35598  assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
35599  sqlite3_mutex_enter(pInode->pLockMutex);
35600  if( pInode->nLock ){
35601    /* If there are outstanding locks, do not actually close the file just
35602    ** yet because that would clear those locks.  Instead, add the file
35603    ** descriptor to pInode->pUnused list.  It will be automatically closed
35604    ** when the last lock is cleared.
35605    */
35606    setPendingFd(pFile);
35607  }
35608  sqlite3_mutex_leave(pInode->pLockMutex);
35609  releaseInodeInfo(pFile);
35610  rc = closeUnixFile(id);
35611  unixLeaveMutex();
35612  return rc;
35613}
35614
35615/************** End of the posix advisory lock implementation *****************
35616******************************************************************************/
35617
35618/******************************************************************************
35619****************************** No-op Locking **********************************
35620**
35621** Of the various locking implementations available, this is by far the
35622** simplest:  locking is ignored.  No attempt is made to lock the database
35623** file for reading or writing.
35624**
35625** This locking mode is appropriate for use on read-only databases
35626** (ex: databases that are burned into CD-ROM, for example.)  It can
35627** also be used if the application employs some external mechanism to
35628** prevent simultaneous access of the same database by two or more
35629** database connections.  But there is a serious risk of database
35630** corruption if this locking mode is used in situations where multiple
35631** database connections are accessing the same database file at the same
35632** time and one or more of those connections are writing.
35633*/
35634
35635static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
35636  UNUSED_PARAMETER(NotUsed);
35637  *pResOut = 0;
35638  return SQLITE_OK;
35639}
35640static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
35641  UNUSED_PARAMETER2(NotUsed, NotUsed2);
35642  return SQLITE_OK;
35643}
35644static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
35645  UNUSED_PARAMETER2(NotUsed, NotUsed2);
35646  return SQLITE_OK;
35647}
35648
35649/*
35650** Close the file.
35651*/
35652static int nolockClose(sqlite3_file *id) {
35653  return closeUnixFile(id);
35654}
35655
35656/******************* End of the no-op lock implementation *********************
35657******************************************************************************/
35658
35659/******************************************************************************
35660************************* Begin dot-file Locking ******************************
35661**
35662** The dotfile locking implementation uses the existence of separate lock
35663** files (really a directory) to control access to the database.  This works
35664** on just about every filesystem imaginable.  But there are serious downsides:
35665**
35666**    (1)  There is zero concurrency.  A single reader blocks all other
35667**         connections from reading or writing the database.
35668**
35669**    (2)  An application crash or power loss can leave stale lock files
35670**         sitting around that need to be cleared manually.
35671**
35672** Nevertheless, a dotlock is an appropriate locking mode for use if no
35673** other locking strategy is available.
35674**
35675** Dotfile locking works by creating a subdirectory in the same directory as
35676** the database and with the same name but with a ".lock" extension added.
35677** The existence of a lock directory implies an EXCLUSIVE lock.  All other
35678** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
35679*/
35680
35681/*
35682** The file suffix added to the data base filename in order to create the
35683** lock directory.
35684*/
35685#define DOTLOCK_SUFFIX ".lock"
35686
35687/*
35688** This routine checks if there is a RESERVED lock held on the specified
35689** file by this or any other process. If such a lock is held, set *pResOut
35690** to a non-zero value otherwise *pResOut is set to zero.  The return value
35691** is set to SQLITE_OK unless an I/O error occurs during lock checking.
35692**
35693** In dotfile locking, either a lock exists or it does not.  So in this
35694** variation of CheckReservedLock(), *pResOut is set to true if any lock
35695** is held on the file and false if the file is unlocked.
35696*/
35697static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
35698  int rc = SQLITE_OK;
35699  int reserved = 0;
35700  unixFile *pFile = (unixFile*)id;
35701
35702  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
35703
35704  assert( pFile );
35705  reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
35706  OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
35707  *pResOut = reserved;
35708  return rc;
35709}
35710
35711/*
35712** Lock the file with the lock specified by parameter eFileLock - one
35713** of the following:
35714**
35715**     (1) SHARED_LOCK
35716**     (2) RESERVED_LOCK
35717**     (3) PENDING_LOCK
35718**     (4) EXCLUSIVE_LOCK
35719**
35720** Sometimes when requesting one lock state, additional lock states
35721** are inserted in between.  The locking might fail on one of the later
35722** transitions leaving the lock state different from what it started but
35723** still short of its goal.  The following chart shows the allowed
35724** transitions and the inserted intermediate states:
35725**
35726**    UNLOCKED -> SHARED
35727**    SHARED -> RESERVED
35728**    SHARED -> (PENDING) -> EXCLUSIVE
35729**    RESERVED -> (PENDING) -> EXCLUSIVE
35730**    PENDING -> EXCLUSIVE
35731**
35732** This routine will only increase a lock.  Use the sqlite3OsUnlock()
35733** routine to lower a locking level.
35734**
35735** With dotfile locking, we really only support state (4): EXCLUSIVE.
35736** But we track the other locking levels internally.
35737*/
35738static int dotlockLock(sqlite3_file *id, int eFileLock) {
35739  unixFile *pFile = (unixFile*)id;
35740  char *zLockFile = (char *)pFile->lockingContext;
35741  int rc = SQLITE_OK;
35742
35743
35744  /* If we have any lock, then the lock file already exists.  All we have
35745  ** to do is adjust our internal record of the lock level.
35746  */
35747  if( pFile->eFileLock > NO_LOCK ){
35748    pFile->eFileLock = eFileLock;
35749    /* Always update the timestamp on the old file */
35750#ifdef HAVE_UTIME
35751    utime(zLockFile, NULL);
35752#else
35753    utimes(zLockFile, NULL);
35754#endif
35755    return SQLITE_OK;
35756  }
35757
35758  /* grab an exclusive lock */
35759  rc = osMkdir(zLockFile, 0777);
35760  if( rc<0 ){
35761    /* failed to open/create the lock directory */
35762    int tErrno = errno;
35763    if( EEXIST == tErrno ){
35764      rc = SQLITE_BUSY;
35765    } else {
35766      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
35767      if( rc!=SQLITE_BUSY ){
35768        storeLastErrno(pFile, tErrno);
35769      }
35770    }
35771    return rc;
35772  }
35773
35774  /* got it, set the type and return ok */
35775  pFile->eFileLock = eFileLock;
35776  return rc;
35777}
35778
35779/*
35780** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
35781** must be either NO_LOCK or SHARED_LOCK.
35782**
35783** If the locking level of the file descriptor is already at or below
35784** the requested locking level, this routine is a no-op.
35785**
35786** When the locking level reaches NO_LOCK, delete the lock file.
35787*/
35788static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
35789  unixFile *pFile = (unixFile*)id;
35790  char *zLockFile = (char *)pFile->lockingContext;
35791  int rc;
35792
35793  assert( pFile );
35794  OSTRACE(("UNLOCK  %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
35795           pFile->eFileLock, osGetpid(0)));
35796  assert( eFileLock<=SHARED_LOCK );
35797
35798  /* no-op if possible */
35799  if( pFile->eFileLock==eFileLock ){
35800    return SQLITE_OK;
35801  }
35802
35803  /* To downgrade to shared, simply update our internal notion of the
35804  ** lock state.  No need to mess with the file on disk.
35805  */
35806  if( eFileLock==SHARED_LOCK ){
35807    pFile->eFileLock = SHARED_LOCK;
35808    return SQLITE_OK;
35809  }
35810
35811  /* To fully unlock the database, delete the lock file */
35812  assert( eFileLock==NO_LOCK );
35813  rc = osRmdir(zLockFile);
35814  if( rc<0 ){
35815    int tErrno = errno;
35816    if( tErrno==ENOENT ){
35817      rc = SQLITE_OK;
35818    }else{
35819      rc = SQLITE_IOERR_UNLOCK;
35820      storeLastErrno(pFile, tErrno);
35821    }
35822    return rc;
35823  }
35824  pFile->eFileLock = NO_LOCK;
35825  return SQLITE_OK;
35826}
35827
35828/*
35829** Close a file.  Make sure the lock has been released before closing.
35830*/
35831static int dotlockClose(sqlite3_file *id) {
35832  unixFile *pFile = (unixFile*)id;
35833  assert( id!=0 );
35834  dotlockUnlock(id, NO_LOCK);
35835  sqlite3_free(pFile->lockingContext);
35836  return closeUnixFile(id);
35837}
35838/****************** End of the dot-file lock implementation *******************
35839******************************************************************************/
35840
35841/******************************************************************************
35842************************** Begin flock Locking ********************************
35843**
35844** Use the flock() system call to do file locking.
35845**
35846** flock() locking is like dot-file locking in that the various
35847** fine-grain locking levels supported by SQLite are collapsed into
35848** a single exclusive lock.  In other words, SHARED, RESERVED, and
35849** PENDING locks are the same thing as an EXCLUSIVE lock.  SQLite
35850** still works when you do this, but concurrency is reduced since
35851** only a single process can be reading the database at a time.
35852**
35853** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off
35854*/
35855#if SQLITE_ENABLE_LOCKING_STYLE
35856
35857/*
35858** Retry flock() calls that fail with EINTR
35859*/
35860#ifdef EINTR
35861static int robust_flock(int fd, int op){
35862  int rc;
35863  do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
35864  return rc;
35865}
35866#else
35867# define robust_flock(a,b) flock(a,b)
35868#endif
35869
35870
35871/*
35872** This routine checks if there is a RESERVED lock held on the specified
35873** file by this or any other process. If such a lock is held, set *pResOut
35874** to a non-zero value otherwise *pResOut is set to zero.  The return value
35875** is set to SQLITE_OK unless an I/O error occurs during lock checking.
35876*/
35877static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
35878  int rc = SQLITE_OK;
35879  int reserved = 0;
35880  unixFile *pFile = (unixFile*)id;
35881
35882  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
35883
35884  assert( pFile );
35885
35886  /* Check if a thread in this process holds such a lock */
35887  if( pFile->eFileLock>SHARED_LOCK ){
35888    reserved = 1;
35889  }
35890
35891  /* Otherwise see if some other process holds it. */
35892  if( !reserved ){
35893    /* attempt to get the lock */
35894    int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
35895    if( !lrc ){
35896      /* got the lock, unlock it */
35897      lrc = robust_flock(pFile->h, LOCK_UN);
35898      if ( lrc ) {
35899        int tErrno = errno;
35900        /* unlock failed with an error */
35901        lrc = SQLITE_IOERR_UNLOCK;
35902        storeLastErrno(pFile, tErrno);
35903        rc = lrc;
35904      }
35905    } else {
35906      int tErrno = errno;
35907      reserved = 1;
35908      /* someone else might have it reserved */
35909      lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
35910      if( IS_LOCK_ERROR(lrc) ){
35911        storeLastErrno(pFile, tErrno);
35912        rc = lrc;
35913      }
35914    }
35915  }
35916  OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
35917
35918#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
35919  if( (rc & 0xff) == SQLITE_IOERR ){
35920    rc = SQLITE_OK;
35921    reserved=1;
35922  }
35923#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
35924  *pResOut = reserved;
35925  return rc;
35926}
35927
35928/*
35929** Lock the file with the lock specified by parameter eFileLock - one
35930** of the following:
35931**
35932**     (1) SHARED_LOCK
35933**     (2) RESERVED_LOCK
35934**     (3) PENDING_LOCK
35935**     (4) EXCLUSIVE_LOCK
35936**
35937** Sometimes when requesting one lock state, additional lock states
35938** are inserted in between.  The locking might fail on one of the later
35939** transitions leaving the lock state different from what it started but
35940** still short of its goal.  The following chart shows the allowed
35941** transitions and the inserted intermediate states:
35942**
35943**    UNLOCKED -> SHARED
35944**    SHARED -> RESERVED
35945**    SHARED -> (PENDING) -> EXCLUSIVE
35946**    RESERVED -> (PENDING) -> EXCLUSIVE
35947**    PENDING -> EXCLUSIVE
35948**
35949** flock() only really support EXCLUSIVE locks.  We track intermediate
35950** lock states in the sqlite3_file structure, but all locks SHARED or
35951** above are really EXCLUSIVE locks and exclude all other processes from
35952** access the file.
35953**
35954** This routine will only increase a lock.  Use the sqlite3OsUnlock()
35955** routine to lower a locking level.
35956*/
35957static int flockLock(sqlite3_file *id, int eFileLock) {
35958  int rc = SQLITE_OK;
35959  unixFile *pFile = (unixFile*)id;
35960
35961  assert( pFile );
35962
35963  /* if we already have a lock, it is exclusive.
35964  ** Just adjust level and punt on outta here. */
35965  if (pFile->eFileLock > NO_LOCK) {
35966    pFile->eFileLock = eFileLock;
35967    return SQLITE_OK;
35968  }
35969
35970  /* grab an exclusive lock */
35971
35972  if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
35973    int tErrno = errno;
35974    /* didn't get, must be busy */
35975    rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
35976    if( IS_LOCK_ERROR(rc) ){
35977      storeLastErrno(pFile, tErrno);
35978    }
35979  } else {
35980    /* got it, set the type and return ok */
35981    pFile->eFileLock = eFileLock;
35982  }
35983  OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
35984           rc==SQLITE_OK ? "ok" : "failed"));
35985#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
35986  if( (rc & 0xff) == SQLITE_IOERR ){
35987    rc = SQLITE_BUSY;
35988  }
35989#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
35990  return rc;
35991}
35992
35993
35994/*
35995** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
35996** must be either NO_LOCK or SHARED_LOCK.
35997**
35998** If the locking level of the file descriptor is already at or below
35999** the requested locking level, this routine is a no-op.
36000*/
36001static int flockUnlock(sqlite3_file *id, int eFileLock) {
36002  unixFile *pFile = (unixFile*)id;
36003
36004  assert( pFile );
36005  OSTRACE(("UNLOCK  %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
36006           pFile->eFileLock, osGetpid(0)));
36007  assert( eFileLock<=SHARED_LOCK );
36008
36009  /* no-op if possible */
36010  if( pFile->eFileLock==eFileLock ){
36011    return SQLITE_OK;
36012  }
36013
36014  /* shared can just be set because we always have an exclusive */
36015  if (eFileLock==SHARED_LOCK) {
36016    pFile->eFileLock = eFileLock;
36017    return SQLITE_OK;
36018  }
36019
36020  /* no, really, unlock. */
36021  if( robust_flock(pFile->h, LOCK_UN) ){
36022#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
36023    return SQLITE_OK;
36024#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
36025    return SQLITE_IOERR_UNLOCK;
36026  }else{
36027    pFile->eFileLock = NO_LOCK;
36028    return SQLITE_OK;
36029  }
36030}
36031
36032/*
36033** Close a file.
36034*/
36035static int flockClose(sqlite3_file *id) {
36036  assert( id!=0 );
36037  flockUnlock(id, NO_LOCK);
36038  return closeUnixFile(id);
36039}
36040
36041#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
36042
36043/******************* End of the flock lock implementation *********************
36044******************************************************************************/
36045
36046/******************************************************************************
36047************************ Begin Named Semaphore Locking ************************
36048**
36049** Named semaphore locking is only supported on VxWorks.
36050**
36051** Semaphore locking is like dot-lock and flock in that it really only
36052** supports EXCLUSIVE locking.  Only a single process can read or write
36053** the database file at a time.  This reduces potential concurrency, but
36054** makes the lock implementation much easier.
36055*/
36056#if OS_VXWORKS
36057
36058/*
36059** This routine checks if there is a RESERVED lock held on the specified
36060** file by this or any other process. If such a lock is held, set *pResOut
36061** to a non-zero value otherwise *pResOut is set to zero.  The return value
36062** is set to SQLITE_OK unless an I/O error occurs during lock checking.
36063*/
36064static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) {
36065  int rc = SQLITE_OK;
36066  int reserved = 0;
36067  unixFile *pFile = (unixFile*)id;
36068
36069  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
36070
36071  assert( pFile );
36072
36073  /* Check if a thread in this process holds such a lock */
36074  if( pFile->eFileLock>SHARED_LOCK ){
36075    reserved = 1;
36076  }
36077
36078  /* Otherwise see if some other process holds it. */
36079  if( !reserved ){
36080    sem_t *pSem = pFile->pInode->pSem;
36081
36082    if( sem_trywait(pSem)==-1 ){
36083      int tErrno = errno;
36084      if( EAGAIN != tErrno ){
36085        rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
36086        storeLastErrno(pFile, tErrno);
36087      } else {
36088        /* someone else has the lock when we are in NO_LOCK */
36089        reserved = (pFile->eFileLock < SHARED_LOCK);
36090      }
36091    }else{
36092      /* we could have it if we want it */
36093      sem_post(pSem);
36094    }
36095  }
36096  OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
36097
36098  *pResOut = reserved;
36099  return rc;
36100}
36101
36102/*
36103** Lock the file with the lock specified by parameter eFileLock - one
36104** of the following:
36105**
36106**     (1) SHARED_LOCK
36107**     (2) RESERVED_LOCK
36108**     (3) PENDING_LOCK
36109**     (4) EXCLUSIVE_LOCK
36110**
36111** Sometimes when requesting one lock state, additional lock states
36112** are inserted in between.  The locking might fail on one of the later
36113** transitions leaving the lock state different from what it started but
36114** still short of its goal.  The following chart shows the allowed
36115** transitions and the inserted intermediate states:
36116**
36117**    UNLOCKED -> SHARED
36118**    SHARED -> RESERVED
36119**    SHARED -> (PENDING) -> EXCLUSIVE
36120**    RESERVED -> (PENDING) -> EXCLUSIVE
36121**    PENDING -> EXCLUSIVE
36122**
36123** Semaphore locks only really support EXCLUSIVE locks.  We track intermediate
36124** lock states in the sqlite3_file structure, but all locks SHARED or
36125** above are really EXCLUSIVE locks and exclude all other processes from
36126** access the file.
36127**
36128** This routine will only increase a lock.  Use the sqlite3OsUnlock()
36129** routine to lower a locking level.
36130*/
36131static int semXLock(sqlite3_file *id, int eFileLock) {
36132  unixFile *pFile = (unixFile*)id;
36133  sem_t *pSem = pFile->pInode->pSem;
36134  int rc = SQLITE_OK;
36135
36136  /* if we already have a lock, it is exclusive.
36137  ** Just adjust level and punt on outta here. */
36138  if (pFile->eFileLock > NO_LOCK) {
36139    pFile->eFileLock = eFileLock;
36140    rc = SQLITE_OK;
36141    goto sem_end_lock;
36142  }
36143
36144  /* lock semaphore now but bail out when already locked. */
36145  if( sem_trywait(pSem)==-1 ){
36146    rc = SQLITE_BUSY;
36147    goto sem_end_lock;
36148  }
36149
36150  /* got it, set the type and return ok */
36151  pFile->eFileLock = eFileLock;
36152
36153 sem_end_lock:
36154  return rc;
36155}
36156
36157/*
36158** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
36159** must be either NO_LOCK or SHARED_LOCK.
36160**
36161** If the locking level of the file descriptor is already at or below
36162** the requested locking level, this routine is a no-op.
36163*/
36164static int semXUnlock(sqlite3_file *id, int eFileLock) {
36165  unixFile *pFile = (unixFile*)id;
36166  sem_t *pSem = pFile->pInode->pSem;
36167
36168  assert( pFile );
36169  assert( pSem );
36170  OSTRACE(("UNLOCK  %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
36171           pFile->eFileLock, osGetpid(0)));
36172  assert( eFileLock<=SHARED_LOCK );
36173
36174  /* no-op if possible */
36175  if( pFile->eFileLock==eFileLock ){
36176    return SQLITE_OK;
36177  }
36178
36179  /* shared can just be set because we always have an exclusive */
36180  if (eFileLock==SHARED_LOCK) {
36181    pFile->eFileLock = eFileLock;
36182    return SQLITE_OK;
36183  }
36184
36185  /* no, really unlock. */
36186  if ( sem_post(pSem)==-1 ) {
36187    int rc, tErrno = errno;
36188    rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
36189    if( IS_LOCK_ERROR(rc) ){
36190      storeLastErrno(pFile, tErrno);
36191    }
36192    return rc;
36193  }
36194  pFile->eFileLock = NO_LOCK;
36195  return SQLITE_OK;
36196}
36197
36198/*
36199 ** Close a file.
36200 */
36201static int semXClose(sqlite3_file *id) {
36202  if( id ){
36203    unixFile *pFile = (unixFile*)id;
36204    semXUnlock(id, NO_LOCK);
36205    assert( pFile );
36206    assert( unixFileMutexNotheld(pFile) );
36207    unixEnterMutex();
36208    releaseInodeInfo(pFile);
36209    unixLeaveMutex();
36210    closeUnixFile(id);
36211  }
36212  return SQLITE_OK;
36213}
36214
36215#endif /* OS_VXWORKS */
36216/*
36217** Named semaphore locking is only available on VxWorks.
36218**
36219*************** End of the named semaphore lock implementation ****************
36220******************************************************************************/
36221
36222
36223/******************************************************************************
36224*************************** Begin AFP Locking *********************************
36225**
36226** AFP is the Apple Filing Protocol.  AFP is a network filesystem found
36227** on Apple Macintosh computers - both OS9 and OSX.
36228**
36229** Third-party implementations of AFP are available.  But this code here
36230** only works on OSX.
36231*/
36232
36233#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
36234/*
36235** The afpLockingContext structure contains all afp lock specific state
36236*/
36237typedef struct afpLockingContext afpLockingContext;
36238struct afpLockingContext {
36239  int reserved;
36240  const char *dbPath;             /* Name of the open file */
36241};
36242
36243struct ByteRangeLockPB2
36244{
36245  unsigned long long offset;        /* offset to first byte to lock */
36246  unsigned long long length;        /* nbr of bytes to lock */
36247  unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
36248  unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */
36249  unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
36250  int fd;                           /* file desc to assoc this lock with */
36251};
36252
36253#define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
36254
36255/*
36256** This is a utility for setting or clearing a bit-range lock on an
36257** AFP filesystem.
36258**
36259** Return SQLITE_OK on success, SQLITE_BUSY on failure.
36260*/
36261static int afpSetLock(
36262  const char *path,              /* Name of the file to be locked or unlocked */
36263  unixFile *pFile,               /* Open file descriptor on path */
36264  unsigned long long offset,     /* First byte to be locked */
36265  unsigned long long length,     /* Number of bytes to lock */
36266  int setLockFlag                /* True to set lock.  False to clear lock */
36267){
36268  struct ByteRangeLockPB2 pb;
36269  int err;
36270
36271  pb.unLockFlag = setLockFlag ? 0 : 1;
36272  pb.startEndFlag = 0;
36273  pb.offset = offset;
36274  pb.length = length;
36275  pb.fd = pFile->h;
36276
36277  OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
36278    (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
36279    offset, length));
36280  err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
36281  if ( err==-1 ) {
36282    int rc;
36283    int tErrno = errno;
36284    OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
36285             path, tErrno, strerror(tErrno)));
36286#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
36287    rc = SQLITE_BUSY;
36288#else
36289    rc = sqliteErrorFromPosixError(tErrno,
36290                    setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
36291#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
36292    if( IS_LOCK_ERROR(rc) ){
36293      storeLastErrno(pFile, tErrno);
36294    }
36295    return rc;
36296  } else {
36297    return SQLITE_OK;
36298  }
36299}
36300
36301/*
36302** This routine checks if there is a RESERVED lock held on the specified
36303** file by this or any other process. If such a lock is held, set *pResOut
36304** to a non-zero value otherwise *pResOut is set to zero.  The return value
36305** is set to SQLITE_OK unless an I/O error occurs during lock checking.
36306*/
36307static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
36308  int rc = SQLITE_OK;
36309  int reserved = 0;
36310  unixFile *pFile = (unixFile*)id;
36311  afpLockingContext *context;
36312
36313  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
36314
36315  assert( pFile );
36316  context = (afpLockingContext *) pFile->lockingContext;
36317  if( context->reserved ){
36318    *pResOut = 1;
36319    return SQLITE_OK;
36320  }
36321  sqlite3_mutex_enter(pFile->pInode->pLockMutex);
36322  /* Check if a thread in this process holds such a lock */
36323  if( pFile->pInode->eFileLock>SHARED_LOCK ){
36324    reserved = 1;
36325  }
36326
36327  /* Otherwise see if some other process holds it.
36328   */
36329  if( !reserved ){
36330    /* lock the RESERVED byte */
36331    int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
36332    if( SQLITE_OK==lrc ){
36333      /* if we succeeded in taking the reserved lock, unlock it to restore
36334      ** the original state */
36335      lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
36336    } else {
36337      /* if we failed to get the lock then someone else must have it */
36338      reserved = 1;
36339    }
36340    if( IS_LOCK_ERROR(lrc) ){
36341      rc=lrc;
36342    }
36343  }
36344
36345  sqlite3_mutex_leave(pFile->pInode->pLockMutex);
36346  OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
36347
36348  *pResOut = reserved;
36349  return rc;
36350}
36351
36352/*
36353** Lock the file with the lock specified by parameter eFileLock - one
36354** of the following:
36355**
36356**     (1) SHARED_LOCK
36357**     (2) RESERVED_LOCK
36358**     (3) PENDING_LOCK
36359**     (4) EXCLUSIVE_LOCK
36360**
36361** Sometimes when requesting one lock state, additional lock states
36362** are inserted in between.  The locking might fail on one of the later
36363** transitions leaving the lock state different from what it started but
36364** still short of its goal.  The following chart shows the allowed
36365** transitions and the inserted intermediate states:
36366**
36367**    UNLOCKED -> SHARED
36368**    SHARED -> RESERVED
36369**    SHARED -> (PENDING) -> EXCLUSIVE
36370**    RESERVED -> (PENDING) -> EXCLUSIVE
36371**    PENDING -> EXCLUSIVE
36372**
36373** This routine will only increase a lock.  Use the sqlite3OsUnlock()
36374** routine to lower a locking level.
36375*/
36376static int afpLock(sqlite3_file *id, int eFileLock){
36377  int rc = SQLITE_OK;
36378  unixFile *pFile = (unixFile*)id;
36379  unixInodeInfo *pInode = pFile->pInode;
36380  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
36381
36382  assert( pFile );
36383  OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
36384           azFileLock(eFileLock), azFileLock(pFile->eFileLock),
36385           azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
36386
36387  /* If there is already a lock of this type or more restrictive on the
36388  ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
36389  ** unixEnterMutex() hasn't been called yet.
36390  */
36391  if( pFile->eFileLock>=eFileLock ){
36392    OSTRACE(("LOCK    %d %s ok (already held) (afp)\n", pFile->h,
36393           azFileLock(eFileLock)));
36394    return SQLITE_OK;
36395  }
36396
36397  /* Make sure the locking sequence is correct
36398  **  (1) We never move from unlocked to anything higher than shared lock.
36399  **  (2) SQLite never explicitly requests a pendig lock.
36400  **  (3) A shared lock is always held when a reserve lock is requested.
36401  */
36402  assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
36403  assert( eFileLock!=PENDING_LOCK );
36404  assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
36405
36406  /* This mutex is needed because pFile->pInode is shared across threads
36407  */
36408  pInode = pFile->pInode;
36409  sqlite3_mutex_enter(pInode->pLockMutex);
36410
36411  /* If some thread using this PID has a lock via a different unixFile*
36412  ** handle that precludes the requested lock, return BUSY.
36413  */
36414  if( (pFile->eFileLock!=pInode->eFileLock &&
36415       (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
36416     ){
36417    rc = SQLITE_BUSY;
36418    goto afp_end_lock;
36419  }
36420
36421  /* If a SHARED lock is requested, and some thread using this PID already
36422  ** has a SHARED or RESERVED lock, then increment reference counts and
36423  ** return SQLITE_OK.
36424  */
36425  if( eFileLock==SHARED_LOCK &&
36426     (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
36427    assert( eFileLock==SHARED_LOCK );
36428    assert( pFile->eFileLock==0 );
36429    assert( pInode->nShared>0 );
36430    pFile->eFileLock = SHARED_LOCK;
36431    pInode->nShared++;
36432    pInode->nLock++;
36433    goto afp_end_lock;
36434  }
36435
36436  /* A PENDING lock is needed before acquiring a SHARED lock and before
36437  ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
36438  ** be released.
36439  */
36440  if( eFileLock==SHARED_LOCK
36441      || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
36442  ){
36443    int failed;
36444    failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
36445    if (failed) {
36446      rc = failed;
36447      goto afp_end_lock;
36448    }
36449  }
36450
36451  /* If control gets to this point, then actually go ahead and make
36452  ** operating system calls for the specified lock.
36453  */
36454  if( eFileLock==SHARED_LOCK ){
36455    int lrc1, lrc2, lrc1Errno = 0;
36456    long lk, mask;
36457
36458    assert( pInode->nShared==0 );
36459    assert( pInode->eFileLock==0 );
36460
36461    mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
36462    /* Now get the read-lock SHARED_LOCK */
36463    /* note that the quality of the randomness doesn't matter that much */
36464    lk = random();
36465    pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
36466    lrc1 = afpSetLock(context->dbPath, pFile,
36467          SHARED_FIRST+pInode->sharedByte, 1, 1);
36468    if( IS_LOCK_ERROR(lrc1) ){
36469      lrc1Errno = pFile->lastErrno;
36470    }
36471    /* Drop the temporary PENDING lock */
36472    lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
36473
36474    if( IS_LOCK_ERROR(lrc1) ) {
36475      storeLastErrno(pFile, lrc1Errno);
36476      rc = lrc1;
36477      goto afp_end_lock;
36478    } else if( IS_LOCK_ERROR(lrc2) ){
36479      rc = lrc2;
36480      goto afp_end_lock;
36481    } else if( lrc1 != SQLITE_OK ) {
36482      rc = lrc1;
36483    } else {
36484      pFile->eFileLock = SHARED_LOCK;
36485      pInode->nLock++;
36486      pInode->nShared = 1;
36487    }
36488  }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
36489    /* We are trying for an exclusive lock but another thread in this
36490     ** same process is still holding a shared lock. */
36491    rc = SQLITE_BUSY;
36492  }else{
36493    /* The request was for a RESERVED or EXCLUSIVE lock.  It is
36494    ** assumed that there is a SHARED or greater lock on the file
36495    ** already.
36496    */
36497    int failed = 0;
36498    assert( 0!=pFile->eFileLock );
36499    if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
36500        /* Acquire a RESERVED lock */
36501        failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
36502      if( !failed ){
36503        context->reserved = 1;
36504      }
36505    }
36506    if (!failed && eFileLock == EXCLUSIVE_LOCK) {
36507      /* Acquire an EXCLUSIVE lock */
36508
36509      /* Remove the shared lock before trying the range.  we'll need to
36510      ** reestablish the shared lock if we can't get the  afpUnlock
36511      */
36512      if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
36513                         pInode->sharedByte, 1, 0)) ){
36514        int failed2 = SQLITE_OK;
36515        /* now attemmpt to get the exclusive lock range */
36516        failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
36517                               SHARED_SIZE, 1);
36518        if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
36519                       SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
36520          /* Can't reestablish the shared lock.  Sqlite can't deal, this is
36521          ** a critical I/O error
36522          */
36523          rc = ((failed & 0xff) == SQLITE_IOERR) ? failed2 :
36524               SQLITE_IOERR_LOCK;
36525          goto afp_end_lock;
36526        }
36527      }else{
36528        rc = failed;
36529      }
36530    }
36531    if( failed ){
36532      rc = failed;
36533    }
36534  }
36535
36536  if( rc==SQLITE_OK ){
36537    pFile->eFileLock = eFileLock;
36538    pInode->eFileLock = eFileLock;
36539  }else if( eFileLock==EXCLUSIVE_LOCK ){
36540    pFile->eFileLock = PENDING_LOCK;
36541    pInode->eFileLock = PENDING_LOCK;
36542  }
36543
36544afp_end_lock:
36545  sqlite3_mutex_leave(pInode->pLockMutex);
36546  OSTRACE(("LOCK    %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
36547         rc==SQLITE_OK ? "ok" : "failed"));
36548  return rc;
36549}
36550
36551/*
36552** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
36553** must be either NO_LOCK or SHARED_LOCK.
36554**
36555** If the locking level of the file descriptor is already at or below
36556** the requested locking level, this routine is a no-op.
36557*/
36558static int afpUnlock(sqlite3_file *id, int eFileLock) {
36559  int rc = SQLITE_OK;
36560  unixFile *pFile = (unixFile*)id;
36561  unixInodeInfo *pInode;
36562  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
36563  int skipShared = 0;
36564#ifdef SQLITE_TEST
36565  int h = pFile->h;
36566#endif
36567
36568  assert( pFile );
36569  OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
36570           pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
36571           osGetpid(0)));
36572
36573  assert( eFileLock<=SHARED_LOCK );
36574  if( pFile->eFileLock<=eFileLock ){
36575    return SQLITE_OK;
36576  }
36577  pInode = pFile->pInode;
36578  sqlite3_mutex_enter(pInode->pLockMutex);
36579  assert( pInode->nShared!=0 );
36580  if( pFile->eFileLock>SHARED_LOCK ){
36581    assert( pInode->eFileLock==pFile->eFileLock );
36582    SimulateIOErrorBenign(1);
36583    SimulateIOError( h=(-1) )
36584    SimulateIOErrorBenign(0);
36585
36586#ifdef SQLITE_DEBUG
36587    /* When reducing a lock such that other processes can start
36588    ** reading the database file again, make sure that the
36589    ** transaction counter was updated if any part of the database
36590    ** file changed.  If the transaction counter is not updated,
36591    ** other connections to the same file might not realize that
36592    ** the file has changed and hence might not know to flush their
36593    ** cache.  The use of a stale cache can lead to database corruption.
36594    */
36595    assert( pFile->inNormalWrite==0
36596           || pFile->dbUpdate==0
36597           || pFile->transCntrChng==1 );
36598    pFile->inNormalWrite = 0;
36599#endif
36600
36601    if( pFile->eFileLock==EXCLUSIVE_LOCK ){
36602      rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
36603      if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
36604        /* only re-establish the shared lock if necessary */
36605        int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
36606        rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
36607      } else {
36608        skipShared = 1;
36609      }
36610    }
36611    if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
36612      rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
36613    }
36614    if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
36615      rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
36616      if( !rc ){
36617        context->reserved = 0;
36618      }
36619    }
36620    if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
36621      pInode->eFileLock = SHARED_LOCK;
36622    }
36623  }
36624  if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
36625
36626    /* Decrement the shared lock counter.  Release the lock using an
36627    ** OS call only when all threads in this same process have released
36628    ** the lock.
36629    */
36630    unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
36631    pInode->nShared--;
36632    if( pInode->nShared==0 ){
36633      SimulateIOErrorBenign(1);
36634      SimulateIOError( h=(-1) )
36635      SimulateIOErrorBenign(0);
36636      if( !skipShared ){
36637        rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
36638      }
36639      if( !rc ){
36640        pInode->eFileLock = NO_LOCK;
36641        pFile->eFileLock = NO_LOCK;
36642      }
36643    }
36644    if( rc==SQLITE_OK ){
36645      pInode->nLock--;
36646      assert( pInode->nLock>=0 );
36647      if( pInode->nLock==0 ) closePendingFds(pFile);
36648    }
36649  }
36650
36651  sqlite3_mutex_leave(pInode->pLockMutex);
36652  if( rc==SQLITE_OK ){
36653    pFile->eFileLock = eFileLock;
36654  }
36655  return rc;
36656}
36657
36658/*
36659** Close a file & cleanup AFP specific locking context
36660*/
36661static int afpClose(sqlite3_file *id) {
36662  int rc = SQLITE_OK;
36663  unixFile *pFile = (unixFile*)id;
36664  assert( id!=0 );
36665  afpUnlock(id, NO_LOCK);
36666  assert( unixFileMutexNotheld(pFile) );
36667  unixEnterMutex();
36668  if( pFile->pInode ){
36669    unixInodeInfo *pInode = pFile->pInode;
36670    sqlite3_mutex_enter(pInode->pLockMutex);
36671    if( pInode->nLock ){
36672      /* If there are outstanding locks, do not actually close the file just
36673      ** yet because that would clear those locks.  Instead, add the file
36674      ** descriptor to pInode->aPending.  It will be automatically closed when
36675      ** the last lock is cleared.
36676      */
36677      setPendingFd(pFile);
36678    }
36679    sqlite3_mutex_leave(pInode->pLockMutex);
36680  }
36681  releaseInodeInfo(pFile);
36682  sqlite3_free(pFile->lockingContext);
36683  rc = closeUnixFile(id);
36684  unixLeaveMutex();
36685  return rc;
36686}
36687
36688#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
36689/*
36690** The code above is the AFP lock implementation.  The code is specific
36691** to MacOSX and does not work on other unix platforms.  No alternative
36692** is available.  If you don't compile for a mac, then the "unix-afp"
36693** VFS is not available.
36694**
36695********************* End of the AFP lock implementation **********************
36696******************************************************************************/
36697
36698/******************************************************************************
36699*************************** Begin NFS Locking ********************************/
36700
36701#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
36702/*
36703 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
36704 ** must be either NO_LOCK or SHARED_LOCK.
36705 **
36706 ** If the locking level of the file descriptor is already at or below
36707 ** the requested locking level, this routine is a no-op.
36708 */
36709static int nfsUnlock(sqlite3_file *id, int eFileLock){
36710  return posixUnlock(id, eFileLock, 1);
36711}
36712
36713#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
36714/*
36715** The code above is the NFS lock implementation.  The code is specific
36716** to MacOSX and does not work on other unix platforms.  No alternative
36717** is available.
36718**
36719********************* End of the NFS lock implementation **********************
36720******************************************************************************/
36721
36722/******************************************************************************
36723**************** Non-locking sqlite3_file methods *****************************
36724**
36725** The next division contains implementations for all methods of the
36726** sqlite3_file object other than the locking methods.  The locking
36727** methods were defined in divisions above (one locking method per
36728** division).  Those methods that are common to all locking modes
36729** are gather together into this division.
36730*/
36731
36732/*
36733** Seek to the offset passed as the second argument, then read cnt
36734** bytes into pBuf. Return the number of bytes actually read.
36735**
36736** NB:  If you define USE_PREAD or USE_PREAD64, then it might also
36737** be necessary to define _XOPEN_SOURCE to be 500.  This varies from
36738** one system to another.  Since SQLite does not define USE_PREAD
36739** in any form by default, we will not attempt to define _XOPEN_SOURCE.
36740** See tickets #2741 and #2681.
36741**
36742** To avoid stomping the errno value on a failed read the lastErrno value
36743** is set before returning.
36744*/
36745static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
36746  int got;
36747  int prior = 0;
36748#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
36749  i64 newOffset;
36750#endif
36751  TIMER_START;
36752  assert( cnt==(cnt&0x1ffff) );
36753  assert( id->h>2 );
36754  do{
36755#if defined(USE_PREAD)
36756    got = osPread(id->h, pBuf, cnt, offset);
36757    SimulateIOError( got = -1 );
36758#elif defined(USE_PREAD64)
36759    got = osPread64(id->h, pBuf, cnt, offset);
36760    SimulateIOError( got = -1 );
36761#else
36762    newOffset = lseek(id->h, offset, SEEK_SET);
36763    SimulateIOError( newOffset = -1 );
36764    if( newOffset<0 ){
36765      storeLastErrno((unixFile*)id, errno);
36766      return -1;
36767    }
36768    got = osRead(id->h, pBuf, cnt);
36769#endif
36770    if( got==cnt ) break;
36771    if( got<0 ){
36772      if( errno==EINTR ){ got = 1; continue; }
36773      prior = 0;
36774      storeLastErrno((unixFile*)id,  errno);
36775      break;
36776    }else if( got>0 ){
36777      cnt -= got;
36778      offset += got;
36779      prior += got;
36780      pBuf = (void*)(got + (char*)pBuf);
36781    }
36782  }while( got>0 );
36783  TIMER_END;
36784  OSTRACE(("READ    %-3d %5d %7lld %llu\n",
36785            id->h, got+prior, offset-prior, TIMER_ELAPSED));
36786  return got+prior;
36787}
36788
36789/*
36790** Read data from a file into a buffer.  Return SQLITE_OK if all
36791** bytes were read successfully and SQLITE_IOERR if anything goes
36792** wrong.
36793*/
36794static int unixRead(
36795  sqlite3_file *id,
36796  void *pBuf,
36797  int amt,
36798  sqlite3_int64 offset
36799){
36800  unixFile *pFile = (unixFile *)id;
36801  int got;
36802  assert( id );
36803  assert( offset>=0 );
36804  assert( amt>0 );
36805
36806  /* If this is a database file (not a journal, master-journal or temp
36807  ** file), the bytes in the locking range should never be read or written. */
36808#if 0
36809  assert( pFile->pPreallocatedUnused==0
36810       || offset>=PENDING_BYTE+512
36811       || offset+amt<=PENDING_BYTE
36812  );
36813#endif
36814
36815#if SQLITE_MAX_MMAP_SIZE>0
36816  /* Deal with as much of this read request as possible by transfering
36817  ** data from the memory mapping using memcpy().  */
36818  if( offset<pFile->mmapSize ){
36819    if( offset+amt <= pFile->mmapSize ){
36820      memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
36821      return SQLITE_OK;
36822    }else{
36823      int nCopy = pFile->mmapSize - offset;
36824      memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
36825      pBuf = &((u8 *)pBuf)[nCopy];
36826      amt -= nCopy;
36827      offset += nCopy;
36828    }
36829  }
36830#endif
36831
36832  got = seekAndRead(pFile, offset, pBuf, amt);
36833  if( got==amt ){
36834    return SQLITE_OK;
36835  }else if( got<0 ){
36836    /* lastErrno set by seekAndRead */
36837    return SQLITE_IOERR_READ;
36838  }else{
36839    storeLastErrno(pFile, 0);   /* not a system error */
36840    /* Unread parts of the buffer must be zero-filled */
36841    memset(&((char*)pBuf)[got], 0, amt-got);
36842    return SQLITE_IOERR_SHORT_READ;
36843  }
36844}
36845
36846/*
36847** Attempt to seek the file-descriptor passed as the first argument to
36848** absolute offset iOff, then attempt to write nBuf bytes of data from
36849** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
36850** return the actual number of bytes written (which may be less than
36851** nBuf).
36852*/
36853static int seekAndWriteFd(
36854  int fd,                         /* File descriptor to write to */
36855  i64 iOff,                       /* File offset to begin writing at */
36856  const void *pBuf,               /* Copy data from this buffer to the file */
36857  int nBuf,                       /* Size of buffer pBuf in bytes */
36858  int *piErrno                    /* OUT: Error number if error occurs */
36859){
36860  int rc = 0;                     /* Value returned by system call */
36861
36862  assert( nBuf==(nBuf&0x1ffff) );
36863  assert( fd>2 );
36864  assert( piErrno!=0 );
36865  nBuf &= 0x1ffff;
36866  TIMER_START;
36867
36868#if defined(USE_PREAD)
36869  do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
36870#elif defined(USE_PREAD64)
36871  do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
36872#else
36873  do{
36874    i64 iSeek = lseek(fd, iOff, SEEK_SET);
36875    SimulateIOError( iSeek = -1 );
36876    if( iSeek<0 ){
36877      rc = -1;
36878      break;
36879    }
36880    rc = osWrite(fd, pBuf, nBuf);
36881  }while( rc<0 && errno==EINTR );
36882#endif
36883
36884  TIMER_END;
36885  OSTRACE(("WRITE   %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
36886
36887  if( rc<0 ) *piErrno = errno;
36888  return rc;
36889}
36890
36891
36892/*
36893** Seek to the offset in id->offset then read cnt bytes into pBuf.
36894** Return the number of bytes actually read.  Update the offset.
36895**
36896** To avoid stomping the errno value on a failed write the lastErrno value
36897** is set before returning.
36898*/
36899static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
36900  return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
36901}
36902
36903
36904/*
36905** Write data from a buffer into a file.  Return SQLITE_OK on success
36906** or some other error code on failure.
36907*/
36908static int unixWrite(
36909  sqlite3_file *id,
36910  const void *pBuf,
36911  int amt,
36912  sqlite3_int64 offset
36913){
36914  unixFile *pFile = (unixFile*)id;
36915  int wrote = 0;
36916  assert( id );
36917  assert( amt>0 );
36918
36919  /* If this is a database file (not a journal, master-journal or temp
36920  ** file), the bytes in the locking range should never be read or written. */
36921#if 0
36922  assert( pFile->pPreallocatedUnused==0
36923       || offset>=PENDING_BYTE+512
36924       || offset+amt<=PENDING_BYTE
36925  );
36926#endif
36927
36928#ifdef SQLITE_DEBUG
36929  /* If we are doing a normal write to a database file (as opposed to
36930  ** doing a hot-journal rollback or a write to some file other than a
36931  ** normal database file) then record the fact that the database
36932  ** has changed.  If the transaction counter is modified, record that
36933  ** fact too.
36934  */
36935  if( pFile->inNormalWrite ){
36936    pFile->dbUpdate = 1;  /* The database has been modified */
36937    if( offset<=24 && offset+amt>=27 ){
36938      int rc;
36939      char oldCntr[4];
36940      SimulateIOErrorBenign(1);
36941      rc = seekAndRead(pFile, 24, oldCntr, 4);
36942      SimulateIOErrorBenign(0);
36943      if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
36944        pFile->transCntrChng = 1;  /* The transaction counter has changed */
36945      }
36946    }
36947  }
36948#endif
36949
36950#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
36951  /* Deal with as much of this write request as possible by transfering
36952  ** data from the memory mapping using memcpy().  */
36953  if( offset<pFile->mmapSize ){
36954    if( offset+amt <= pFile->mmapSize ){
36955      memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
36956      return SQLITE_OK;
36957    }else{
36958      int nCopy = pFile->mmapSize - offset;
36959      memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
36960      pBuf = &((u8 *)pBuf)[nCopy];
36961      amt -= nCopy;
36962      offset += nCopy;
36963    }
36964  }
36965#endif
36966
36967  while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){
36968    amt -= wrote;
36969    offset += wrote;
36970    pBuf = &((char*)pBuf)[wrote];
36971  }
36972  SimulateIOError(( wrote=(-1), amt=1 ));
36973  SimulateDiskfullError(( wrote=0, amt=1 ));
36974
36975  if( amt>wrote ){
36976    if( wrote<0 && pFile->lastErrno!=ENOSPC ){
36977      /* lastErrno set by seekAndWrite */
36978      return SQLITE_IOERR_WRITE;
36979    }else{
36980      storeLastErrno(pFile, 0); /* not a system error */
36981      return SQLITE_FULL;
36982    }
36983  }
36984
36985  return SQLITE_OK;
36986}
36987
36988#ifdef SQLITE_TEST
36989/*
36990** Count the number of fullsyncs and normal syncs.  This is used to test
36991** that syncs and fullsyncs are occurring at the right times.
36992*/
36993SQLITE_API int sqlite3_sync_count = 0;
36994SQLITE_API int sqlite3_fullsync_count = 0;
36995#endif
36996
36997/*
36998** We do not trust systems to provide a working fdatasync().  Some do.
36999** Others do no.  To be safe, we will stick with the (slightly slower)
37000** fsync(). If you know that your system does support fdatasync() correctly,
37001** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
37002*/
37003#if !defined(fdatasync) && !HAVE_FDATASYNC
37004# define fdatasync fsync
37005#endif
37006
37007/*
37008** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
37009** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
37010** only available on Mac OS X.  But that could change.
37011*/
37012#ifdef F_FULLFSYNC
37013# define HAVE_FULLFSYNC 1
37014#else
37015# define HAVE_FULLFSYNC 0
37016#endif
37017
37018
37019/*
37020** The fsync() system call does not work as advertised on many
37021** unix systems.  The following procedure is an attempt to make
37022** it work better.
37023**
37024** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful
37025** for testing when we want to run through the test suite quickly.
37026** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
37027** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
37028** or power failure will likely corrupt the database file.
37029**
37030** SQLite sets the dataOnly flag if the size of the file is unchanged.
37031** The idea behind dataOnly is that it should only write the file content
37032** to disk, not the inode.  We only set dataOnly if the file size is
37033** unchanged since the file size is part of the inode.  However,
37034** Ted Ts'o tells us that fdatasync() will also write the inode if the
37035** file size has changed.  The only real difference between fdatasync()
37036** and fsync(), Ted tells us, is that fdatasync() will not flush the
37037** inode if the mtime or owner or other inode attributes have changed.
37038** We only care about the file size, not the other file attributes, so
37039** as far as SQLite is concerned, an fdatasync() is always adequate.
37040** So, we always use fdatasync() if it is available, regardless of
37041** the value of the dataOnly flag.
37042*/
37043static int full_fsync(int fd, int fullSync, int dataOnly){
37044  int rc;
37045
37046  /* The following "ifdef/elif/else/" block has the same structure as
37047  ** the one below. It is replicated here solely to avoid cluttering
37048  ** up the real code with the UNUSED_PARAMETER() macros.
37049  */
37050#ifdef SQLITE_NO_SYNC
37051  UNUSED_PARAMETER(fd);
37052  UNUSED_PARAMETER(fullSync);
37053  UNUSED_PARAMETER(dataOnly);
37054#elif HAVE_FULLFSYNC
37055  UNUSED_PARAMETER(dataOnly);
37056#else
37057  UNUSED_PARAMETER(fullSync);
37058  UNUSED_PARAMETER(dataOnly);
37059#endif
37060
37061  /* Record the number of times that we do a normal fsync() and
37062  ** FULLSYNC.  This is used during testing to verify that this procedure
37063  ** gets called with the correct arguments.
37064  */
37065#ifdef SQLITE_TEST
37066  if( fullSync ) sqlite3_fullsync_count++;
37067  sqlite3_sync_count++;
37068#endif
37069
37070  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
37071  ** no-op.  But go ahead and call fstat() to validate the file
37072  ** descriptor as we need a method to provoke a failure during
37073  ** coverate testing.
37074  */
37075#ifdef SQLITE_NO_SYNC
37076  {
37077    struct stat buf;
37078    rc = osFstat(fd, &buf);
37079  }
37080#elif HAVE_FULLFSYNC
37081  if( fullSync ){
37082    rc = osFcntl(fd, F_FULLFSYNC, 0);
37083  }else{
37084    rc = 1;
37085  }
37086  /* If the FULLFSYNC failed, fall back to attempting an fsync().
37087  ** It shouldn't be possible for fullfsync to fail on the local
37088  ** file system (on OSX), so failure indicates that FULLFSYNC
37089  ** isn't supported for this file system. So, attempt an fsync
37090  ** and (for now) ignore the overhead of a superfluous fcntl call.
37091  ** It'd be better to detect fullfsync support once and avoid
37092  ** the fcntl call every time sync is called.
37093  */
37094  if( rc ) rc = fsync(fd);
37095
37096#elif defined(__APPLE__)
37097  /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
37098  ** so currently we default to the macro that redefines fdatasync to fsync
37099  */
37100  rc = fsync(fd);
37101#else
37102  rc = fdatasync(fd);
37103#if OS_VXWORKS
37104  if( rc==-1 && errno==ENOTSUP ){
37105    rc = fsync(fd);
37106  }
37107#endif /* OS_VXWORKS */
37108#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
37109
37110  if( OS_VXWORKS && rc!= -1 ){
37111    rc = 0;
37112  }
37113  return rc;
37114}
37115
37116/*
37117** Open a file descriptor to the directory containing file zFilename.
37118** If successful, *pFd is set to the opened file descriptor and
37119** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
37120** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
37121** value.
37122**
37123** The directory file descriptor is used for only one thing - to
37124** fsync() a directory to make sure file creation and deletion events
37125** are flushed to disk.  Such fsyncs are not needed on newer
37126** journaling filesystems, but are required on older filesystems.
37127**
37128** This routine can be overridden using the xSetSysCall interface.
37129** The ability to override this routine was added in support of the
37130** chromium sandbox.  Opening a directory is a security risk (we are
37131** told) so making it overrideable allows the chromium sandbox to
37132** replace this routine with a harmless no-op.  To make this routine
37133** a no-op, replace it with a stub that returns SQLITE_OK but leaves
37134** *pFd set to a negative number.
37135**
37136** If SQLITE_OK is returned, the caller is responsible for closing
37137** the file descriptor *pFd using close().
37138*/
37139static int openDirectory(const char *zFilename, int *pFd){
37140  int ii;
37141  int fd = -1;
37142  char zDirname[MAX_PATHNAME+1];
37143
37144  sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
37145  for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
37146  if( ii>0 ){
37147    zDirname[ii] = '\0';
37148  }else{
37149    if( zDirname[0]!='/' ) zDirname[0] = '.';
37150    zDirname[1] = 0;
37151  }
37152  fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
37153  if( fd>=0 ){
37154    OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
37155  }
37156  *pFd = fd;
37157  if( fd>=0 ) return SQLITE_OK;
37158  return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname);
37159}
37160
37161/*
37162** Make sure all writes to a particular file are committed to disk.
37163**
37164** If dataOnly==0 then both the file itself and its metadata (file
37165** size, access time, etc) are synced.  If dataOnly!=0 then only the
37166** file data is synced.
37167**
37168** Under Unix, also make sure that the directory entry for the file
37169** has been created by fsync-ing the directory that contains the file.
37170** If we do not do this and we encounter a power failure, the directory
37171** entry for the journal might not exist after we reboot.  The next
37172** SQLite to access the file will not know that the journal exists (because
37173** the directory entry for the journal was never created) and the transaction
37174** will not roll back - possibly leading to database corruption.
37175*/
37176static int unixSync(sqlite3_file *id, int flags){
37177  int rc;
37178  unixFile *pFile = (unixFile*)id;
37179
37180  int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
37181  int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
37182
37183  /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
37184  assert((flags&0x0F)==SQLITE_SYNC_NORMAL
37185      || (flags&0x0F)==SQLITE_SYNC_FULL
37186  );
37187
37188  /* Unix cannot, but some systems may return SQLITE_FULL from here. This
37189  ** line is to test that doing so does not cause any problems.
37190  */
37191  SimulateDiskfullError( return SQLITE_FULL );
37192
37193  assert( pFile );
37194  OSTRACE(("SYNC    %-3d\n", pFile->h));
37195  rc = full_fsync(pFile->h, isFullsync, isDataOnly);
37196  SimulateIOError( rc=1 );
37197  if( rc ){
37198    storeLastErrno(pFile, errno);
37199    return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
37200  }
37201
37202  /* Also fsync the directory containing the file if the DIRSYNC flag
37203  ** is set.  This is a one-time occurrence.  Many systems (examples: AIX)
37204  ** are unable to fsync a directory, so ignore errors on the fsync.
37205  */
37206  if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
37207    int dirfd;
37208    OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
37209            HAVE_FULLFSYNC, isFullsync));
37210    rc = osOpenDirectory(pFile->zPath, &dirfd);
37211    if( rc==SQLITE_OK ){
37212      full_fsync(dirfd, 0, 0);
37213      robust_close(pFile, dirfd, __LINE__);
37214    }else{
37215      assert( rc==SQLITE_CANTOPEN );
37216      rc = SQLITE_OK;
37217    }
37218    pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
37219  }
37220  return rc;
37221}
37222
37223/*
37224** Truncate an open file to a specified size
37225*/
37226static int unixTruncate(sqlite3_file *id, i64 nByte){
37227  unixFile *pFile = (unixFile *)id;
37228  int rc;
37229  assert( pFile );
37230  SimulateIOError( return SQLITE_IOERR_TRUNCATE );
37231
37232  /* If the user has configured a chunk-size for this file, truncate the
37233  ** file so that it consists of an integer number of chunks (i.e. the
37234  ** actual file size after the operation may be larger than the requested
37235  ** size).
37236  */
37237  if( pFile->szChunk>0 ){
37238    nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
37239  }
37240
37241  rc = robust_ftruncate(pFile->h, nByte);
37242  if( rc ){
37243    storeLastErrno(pFile, errno);
37244    return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
37245  }else{
37246#ifdef SQLITE_DEBUG
37247    /* If we are doing a normal write to a database file (as opposed to
37248    ** doing a hot-journal rollback or a write to some file other than a
37249    ** normal database file) and we truncate the file to zero length,
37250    ** that effectively updates the change counter.  This might happen
37251    ** when restoring a database using the backup API from a zero-length
37252    ** source.
37253    */
37254    if( pFile->inNormalWrite && nByte==0 ){
37255      pFile->transCntrChng = 1;
37256    }
37257#endif
37258
37259#if SQLITE_MAX_MMAP_SIZE>0
37260    /* If the file was just truncated to a size smaller than the currently
37261    ** mapped region, reduce the effective mapping size as well. SQLite will
37262    ** use read() and write() to access data beyond this point from now on.
37263    */
37264    if( nByte<pFile->mmapSize ){
37265      pFile->mmapSize = nByte;
37266    }
37267#endif
37268
37269    return SQLITE_OK;
37270  }
37271}
37272
37273/*
37274** Determine the current size of a file in bytes
37275*/
37276static int unixFileSize(sqlite3_file *id, i64 *pSize){
37277  int rc;
37278  struct stat buf;
37279  assert( id );
37280  rc = osFstat(((unixFile*)id)->h, &buf);
37281  SimulateIOError( rc=1 );
37282  if( rc!=0 ){
37283    storeLastErrno((unixFile*)id, errno);
37284    return SQLITE_IOERR_FSTAT;
37285  }
37286  *pSize = buf.st_size;
37287
37288  /* When opening a zero-size database, the findInodeInfo() procedure
37289  ** writes a single byte into that file in order to work around a bug
37290  ** in the OS-X msdos filesystem.  In order to avoid problems with upper
37291  ** layers, we need to report this file size as zero even though it is
37292  ** really 1.   Ticket #3260.
37293  */
37294  if( *pSize==1 ) *pSize = 0;
37295
37296
37297  return SQLITE_OK;
37298}
37299
37300#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
37301/*
37302** Handler for proxy-locking file-control verbs.  Defined below in the
37303** proxying locking division.
37304*/
37305static int proxyFileControl(sqlite3_file*,int,void*);
37306#endif
37307
37308/*
37309** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
37310** file-control operation.  Enlarge the database to nBytes in size
37311** (rounded up to the next chunk-size).  If the database is already
37312** nBytes or larger, this routine is a no-op.
37313*/
37314static int fcntlSizeHint(unixFile *pFile, i64 nByte){
37315  if( pFile->szChunk>0 ){
37316    i64 nSize;                    /* Required file size */
37317    struct stat buf;              /* Used to hold return values of fstat() */
37318
37319    if( osFstat(pFile->h, &buf) ){
37320      return SQLITE_IOERR_FSTAT;
37321    }
37322
37323    nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
37324    if( nSize>(i64)buf.st_size ){
37325
37326#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
37327      /* The code below is handling the return value of osFallocate()
37328      ** correctly. posix_fallocate() is defined to "returns zero on success,
37329      ** or an error number on  failure". See the manpage for details. */
37330      int err;
37331      do{
37332        err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
37333      }while( err==EINTR );
37334      if( err && err!=EINVAL ) return SQLITE_IOERR_WRITE;
37335#else
37336      /* If the OS does not have posix_fallocate(), fake it. Write a
37337      ** single byte to the last byte in each block that falls entirely
37338      ** within the extended region. Then, if required, a single byte
37339      ** at offset (nSize-1), to set the size of the file correctly.
37340      ** This is a similar technique to that used by glibc on systems
37341      ** that do not have a real fallocate() call.
37342      */
37343      int nBlk = buf.st_blksize;  /* File-system block size */
37344      int nWrite = 0;             /* Number of bytes written by seekAndWrite */
37345      i64 iWrite;                 /* Next offset to write to */
37346
37347      iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
37348      assert( iWrite>=buf.st_size );
37349      assert( ((iWrite+1)%nBlk)==0 );
37350      for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
37351        if( iWrite>=nSize ) iWrite = nSize - 1;
37352        nWrite = seekAndWrite(pFile, iWrite, "", 1);
37353        if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
37354      }
37355#endif
37356    }
37357  }
37358
37359#if SQLITE_MAX_MMAP_SIZE>0
37360  if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
37361    int rc;
37362    if( pFile->szChunk<=0 ){
37363      if( robust_ftruncate(pFile->h, nByte) ){
37364        storeLastErrno(pFile, errno);
37365        return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
37366      }
37367    }
37368
37369    rc = unixMapfile(pFile, nByte);
37370    return rc;
37371  }
37372#endif
37373
37374  return SQLITE_OK;
37375}
37376
37377/*
37378** If *pArg is initially negative then this is a query.  Set *pArg to
37379** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
37380**
37381** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
37382*/
37383static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
37384  if( *pArg<0 ){
37385    *pArg = (pFile->ctrlFlags & mask)!=0;
37386  }else if( (*pArg)==0 ){
37387    pFile->ctrlFlags &= ~mask;
37388  }else{
37389    pFile->ctrlFlags |= mask;
37390  }
37391}
37392
37393/* Forward declaration */
37394static int unixGetTempname(int nBuf, char *zBuf);
37395
37396/*
37397** Information and control of an open file handle.
37398*/
37399static int unixFileControl(sqlite3_file *id, int op, void *pArg){
37400  unixFile *pFile = (unixFile*)id;
37401  switch( op ){
37402#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
37403    case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: {
37404      int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
37405      return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK;
37406    }
37407    case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: {
37408      int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
37409      return rc ? SQLITE_IOERR_COMMIT_ATOMIC : SQLITE_OK;
37410    }
37411    case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: {
37412      int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
37413      return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK;
37414    }
37415#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
37416
37417    case SQLITE_FCNTL_LOCKSTATE: {
37418      *(int*)pArg = pFile->eFileLock;
37419      return SQLITE_OK;
37420    }
37421    case SQLITE_FCNTL_LAST_ERRNO: {
37422      *(int*)pArg = pFile->lastErrno;
37423      return SQLITE_OK;
37424    }
37425    case SQLITE_FCNTL_CHUNK_SIZE: {
37426      pFile->szChunk = *(int *)pArg;
37427      return SQLITE_OK;
37428    }
37429    case SQLITE_FCNTL_SIZE_HINT: {
37430      int rc;
37431      SimulateIOErrorBenign(1);
37432      rc = fcntlSizeHint(pFile, *(i64 *)pArg);
37433      SimulateIOErrorBenign(0);
37434      return rc;
37435    }
37436    case SQLITE_FCNTL_PERSIST_WAL: {
37437      unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
37438      return SQLITE_OK;
37439    }
37440    case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
37441      unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
37442      return SQLITE_OK;
37443    }
37444    case SQLITE_FCNTL_VFSNAME: {
37445      *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
37446      return SQLITE_OK;
37447    }
37448    case SQLITE_FCNTL_TEMPFILENAME: {
37449      char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
37450      if( zTFile ){
37451        unixGetTempname(pFile->pVfs->mxPathname, zTFile);
37452        *(char**)pArg = zTFile;
37453      }
37454      return SQLITE_OK;
37455    }
37456    case SQLITE_FCNTL_HAS_MOVED: {
37457      *(int*)pArg = fileHasMoved(pFile);
37458      return SQLITE_OK;
37459    }
37460#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
37461    case SQLITE_FCNTL_LOCK_TIMEOUT: {
37462      int iOld = pFile->iBusyTimeout;
37463      pFile->iBusyTimeout = *(int*)pArg;
37464      *(int*)pArg = iOld;
37465      return SQLITE_OK;
37466    }
37467#endif
37468#if SQLITE_MAX_MMAP_SIZE>0
37469    case SQLITE_FCNTL_MMAP_SIZE: {
37470      i64 newLimit = *(i64*)pArg;
37471      int rc = SQLITE_OK;
37472      if( newLimit>sqlite3GlobalConfig.mxMmap ){
37473        newLimit = sqlite3GlobalConfig.mxMmap;
37474      }
37475
37476      /* The value of newLimit may be eventually cast to (size_t) and passed
37477      ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a
37478      ** 64-bit type. */
37479      if( newLimit>0 && sizeof(size_t)<8 ){
37480        newLimit = (newLimit & 0x7FFFFFFF);
37481      }
37482
37483      *(i64*)pArg = pFile->mmapSizeMax;
37484      if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
37485        pFile->mmapSizeMax = newLimit;
37486        if( pFile->mmapSize>0 ){
37487          unixUnmapfile(pFile);
37488          rc = unixMapfile(pFile, -1);
37489        }
37490      }
37491      return rc;
37492    }
37493#endif
37494#ifdef SQLITE_DEBUG
37495    /* The pager calls this method to signal that it has done
37496    ** a rollback and that the database is therefore unchanged and
37497    ** it hence it is OK for the transaction change counter to be
37498    ** unchanged.
37499    */
37500    case SQLITE_FCNTL_DB_UNCHANGED: {
37501      ((unixFile*)id)->dbUpdate = 0;
37502      return SQLITE_OK;
37503    }
37504#endif
37505#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
37506    case SQLITE_FCNTL_SET_LOCKPROXYFILE:
37507    case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
37508      return proxyFileControl(id,op,pArg);
37509    }
37510#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
37511  }
37512  return SQLITE_NOTFOUND;
37513}
37514
37515/*
37516** If pFd->sectorSize is non-zero when this function is called, it is a
37517** no-op. Otherwise, the values of pFd->sectorSize and
37518** pFd->deviceCharacteristics are set according to the file-system
37519** characteristics.
37520**
37521** There are two versions of this function. One for QNX and one for all
37522** other systems.
37523*/
37524#ifndef __QNXNTO__
37525static void setDeviceCharacteristics(unixFile *pFd){
37526  assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
37527  if( pFd->sectorSize==0 ){
37528#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
37529    int res;
37530    u32 f = 0;
37531
37532    /* Check for support for F2FS atomic batch writes. */
37533    res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
37534    if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE) ){
37535      pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
37536    }
37537#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
37538
37539    /* Set the POWERSAFE_OVERWRITE flag if requested. */
37540    if( pFd->ctrlFlags & UNIXFILE_PSOW ){
37541      pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
37542    }
37543
37544    pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
37545  }
37546}
37547#else
37548#include <sys/dcmd_blk.h>
37549#include <sys/statvfs.h>
37550static void setDeviceCharacteristics(unixFile *pFile){
37551  if( pFile->sectorSize == 0 ){
37552    struct statvfs fsInfo;
37553
37554    /* Set defaults for non-supported filesystems */
37555    pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
37556    pFile->deviceCharacteristics = 0;
37557    if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
37558      return;
37559    }
37560
37561    if( !strcmp(fsInfo.f_basetype, "tmp") ) {
37562      pFile->sectorSize = fsInfo.f_bsize;
37563      pFile->deviceCharacteristics =
37564        SQLITE_IOCAP_ATOMIC4K |       /* All ram filesystem writes are atomic */
37565        SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
37566                                      ** the write succeeds */
37567        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
37568                                      ** so it is ordered */
37569        0;
37570    }else if( strstr(fsInfo.f_basetype, "etfs") ){
37571      pFile->sectorSize = fsInfo.f_bsize;
37572      pFile->deviceCharacteristics =
37573        /* etfs cluster size writes are atomic */
37574        (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
37575        SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
37576                                      ** the write succeeds */
37577        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
37578                                      ** so it is ordered */
37579        0;
37580    }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
37581      pFile->sectorSize = fsInfo.f_bsize;
37582      pFile->deviceCharacteristics =
37583        SQLITE_IOCAP_ATOMIC |         /* All filesystem writes are atomic */
37584        SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
37585                                      ** the write succeeds */
37586        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
37587                                      ** so it is ordered */
37588        0;
37589    }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
37590      pFile->sectorSize = fsInfo.f_bsize;
37591      pFile->deviceCharacteristics =
37592        /* full bitset of atomics from max sector size and smaller */
37593        ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
37594        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
37595                                      ** so it is ordered */
37596        0;
37597    }else if( strstr(fsInfo.f_basetype, "dos") ){
37598      pFile->sectorSize = fsInfo.f_bsize;
37599      pFile->deviceCharacteristics =
37600        /* full bitset of atomics from max sector size and smaller */
37601        ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
37602        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
37603                                      ** so it is ordered */
37604        0;
37605    }else{
37606      pFile->deviceCharacteristics =
37607        SQLITE_IOCAP_ATOMIC512 |      /* blocks are atomic */
37608        SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
37609                                      ** the write succeeds */
37610        0;
37611    }
37612  }
37613  /* Last chance verification.  If the sector size isn't a multiple of 512
37614  ** then it isn't valid.*/
37615  if( pFile->sectorSize % 512 != 0 ){
37616    pFile->deviceCharacteristics = 0;
37617    pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
37618  }
37619}
37620#endif
37621
37622/*
37623** Return the sector size in bytes of the underlying block device for
37624** the specified file. This is almost always 512 bytes, but may be
37625** larger for some devices.
37626**
37627** SQLite code assumes this function cannot fail. It also assumes that
37628** if two files are created in the same file-system directory (i.e.
37629** a database and its journal file) that the sector size will be the
37630** same for both.
37631*/
37632static int unixSectorSize(sqlite3_file *id){
37633  unixFile *pFd = (unixFile*)id;
37634  setDeviceCharacteristics(pFd);
37635  return pFd->sectorSize;
37636}
37637
37638/*
37639** Return the device characteristics for the file.
37640**
37641** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
37642** However, that choice is controversial since technically the underlying
37643** file system does not always provide powersafe overwrites.  (In other
37644** words, after a power-loss event, parts of the file that were never
37645** written might end up being altered.)  However, non-PSOW behavior is very,
37646** very rare.  And asserting PSOW makes a large reduction in the amount
37647** of required I/O for journaling, since a lot of padding is eliminated.
37648**  Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
37649** available to turn it off and URI query parameter available to turn it off.
37650*/
37651static int unixDeviceCharacteristics(sqlite3_file *id){
37652  unixFile *pFd = (unixFile*)id;
37653  setDeviceCharacteristics(pFd);
37654  return pFd->deviceCharacteristics;
37655}
37656
37657#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
37658
37659/*
37660** Return the system page size.
37661**
37662** This function should not be called directly by other code in this file.
37663** Instead, it should be called via macro osGetpagesize().
37664*/
37665static int unixGetpagesize(void){
37666#if OS_VXWORKS
37667  return 1024;
37668#elif defined(_BSD_SOURCE)
37669  return getpagesize();
37670#else
37671  return (int)sysconf(_SC_PAGESIZE);
37672#endif
37673}
37674
37675#endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
37676
37677#ifndef SQLITE_OMIT_WAL
37678
37679/*
37680** Object used to represent an shared memory buffer.
37681**
37682** When multiple threads all reference the same wal-index, each thread
37683** has its own unixShm object, but they all point to a single instance
37684** of this unixShmNode object.  In other words, each wal-index is opened
37685** only once per process.
37686**
37687** Each unixShmNode object is connected to a single unixInodeInfo object.
37688** We could coalesce this object into unixInodeInfo, but that would mean
37689** every open file that does not use shared memory (in other words, most
37690** open files) would have to carry around this extra information.  So
37691** the unixInodeInfo object contains a pointer to this unixShmNode object
37692** and the unixShmNode object is created only when needed.
37693**
37694** unixMutexHeld() must be true when creating or destroying
37695** this object or while reading or writing the following fields:
37696**
37697**      nRef
37698**
37699** The following fields are read-only after the object is created:
37700**
37701**      hShm
37702**      zFilename
37703**
37704** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and
37705** unixMutexHeld() is true when reading or writing any other field
37706** in this structure.
37707*/
37708struct unixShmNode {
37709  unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
37710  sqlite3_mutex *pShmMutex;  /* Mutex to access this object */
37711  char *zFilename;           /* Name of the mmapped file */
37712  int hShm;                  /* Open file descriptor */
37713  int szRegion;              /* Size of shared-memory regions */
37714  u16 nRegion;               /* Size of array apRegion */
37715  u8 isReadonly;             /* True if read-only */
37716  u8 isUnlocked;             /* True if no DMS lock held */
37717  char **apRegion;           /* Array of mapped shared-memory regions */
37718  int nRef;                  /* Number of unixShm objects pointing to this */
37719  unixShm *pFirst;           /* All unixShm objects pointing to this */
37720#ifdef SQLITE_DEBUG
37721  u8 exclMask;               /* Mask of exclusive locks held */
37722  u8 sharedMask;             /* Mask of shared locks held */
37723  u8 nextShmId;              /* Next available unixShm.id value */
37724#endif
37725};
37726
37727/*
37728** Structure used internally by this VFS to record the state of an
37729** open shared memory connection.
37730**
37731** The following fields are initialized when this object is created and
37732** are read-only thereafter:
37733**
37734**    unixShm.pShmNode
37735**    unixShm.id
37736**
37737** All other fields are read/write.  The unixShm.pShmNode->pShmMutex must
37738** be held while accessing any read/write fields.
37739*/
37740struct unixShm {
37741  unixShmNode *pShmNode;     /* The underlying unixShmNode object */
37742  unixShm *pNext;            /* Next unixShm with the same unixShmNode */
37743  u8 hasMutex;               /* True if holding the unixShmNode->pShmMutex */
37744  u8 id;                     /* Id of this connection within its unixShmNode */
37745  u16 sharedMask;            /* Mask of shared locks held */
37746  u16 exclMask;              /* Mask of exclusive locks held */
37747};
37748
37749/*
37750** Constants used for locking
37751*/
37752#define UNIX_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)         /* first lock byte */
37753#define UNIX_SHM_DMS    (UNIX_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
37754
37755/*
37756** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
37757**
37758** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
37759** otherwise.
37760*/
37761static int unixShmSystemLock(
37762  unixFile *pFile,       /* Open connection to the WAL file */
37763  int lockType,          /* F_UNLCK, F_RDLCK, or F_WRLCK */
37764  int ofst,              /* First byte of the locking range */
37765  int n                  /* Number of bytes to lock */
37766){
37767  unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
37768  struct flock f;        /* The posix advisory locking structure */
37769  int rc = SQLITE_OK;    /* Result code form fcntl() */
37770
37771  /* Access to the unixShmNode object is serialized by the caller */
37772  pShmNode = pFile->pInode->pShmNode;
37773  assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
37774  assert( pShmNode->nRef>0 || unixMutexHeld() );
37775
37776  /* Shared locks never span more than one byte */
37777  assert( n==1 || lockType!=F_RDLCK );
37778
37779  /* Locks are within range */
37780  assert( n>=1 && n<=SQLITE_SHM_NLOCK );
37781
37782  if( pShmNode->hShm>=0 ){
37783    int res;
37784    /* Initialize the locking parameters */
37785    f.l_type = lockType;
37786    f.l_whence = SEEK_SET;
37787    f.l_start = ofst;
37788    f.l_len = n;
37789    res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
37790    if( res==-1 ){
37791#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
37792      rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY);
37793#else
37794      rc = SQLITE_BUSY;
37795#endif
37796    }
37797  }
37798
37799  /* Update the global lock state and do debug tracing */
37800#ifdef SQLITE_DEBUG
37801  { u16 mask;
37802  OSTRACE(("SHM-LOCK "));
37803  mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
37804  if( rc==SQLITE_OK ){
37805    if( lockType==F_UNLCK ){
37806      OSTRACE(("unlock %d ok", ofst));
37807      pShmNode->exclMask &= ~mask;
37808      pShmNode->sharedMask &= ~mask;
37809    }else if( lockType==F_RDLCK ){
37810      OSTRACE(("read-lock %d ok", ofst));
37811      pShmNode->exclMask &= ~mask;
37812      pShmNode->sharedMask |= mask;
37813    }else{
37814      assert( lockType==F_WRLCK );
37815      OSTRACE(("write-lock %d ok", ofst));
37816      pShmNode->exclMask |= mask;
37817      pShmNode->sharedMask &= ~mask;
37818    }
37819  }else{
37820    if( lockType==F_UNLCK ){
37821      OSTRACE(("unlock %d failed", ofst));
37822    }else if( lockType==F_RDLCK ){
37823      OSTRACE(("read-lock failed"));
37824    }else{
37825      assert( lockType==F_WRLCK );
37826      OSTRACE(("write-lock %d failed", ofst));
37827    }
37828  }
37829  OSTRACE((" - afterwards %03x,%03x\n",
37830           pShmNode->sharedMask, pShmNode->exclMask));
37831  }
37832#endif
37833
37834  return rc;
37835}
37836
37837/*
37838** Return the minimum number of 32KB shm regions that should be mapped at
37839** a time, assuming that each mapping must be an integer multiple of the
37840** current system page-size.
37841**
37842** Usually, this is 1. The exception seems to be systems that are configured
37843** to use 64KB pages - in this case each mapping must cover at least two
37844** shm regions.
37845*/
37846static int unixShmRegionPerMap(void){
37847  int shmsz = 32*1024;            /* SHM region size */
37848  int pgsz = osGetpagesize();   /* System page size */
37849  assert( ((pgsz-1)&pgsz)==0 );   /* Page size must be a power of 2 */
37850  if( pgsz<shmsz ) return 1;
37851  return pgsz/shmsz;
37852}
37853
37854/*
37855** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
37856**
37857** This is not a VFS shared-memory method; it is a utility function called
37858** by VFS shared-memory methods.
37859*/
37860static void unixShmPurge(unixFile *pFd){
37861  unixShmNode *p = pFd->pInode->pShmNode;
37862  assert( unixMutexHeld() );
37863  if( p && ALWAYS(p->nRef==0) ){
37864    int nShmPerMap = unixShmRegionPerMap();
37865    int i;
37866    assert( p->pInode==pFd->pInode );
37867    sqlite3_mutex_free(p->pShmMutex);
37868    for(i=0; i<p->nRegion; i+=nShmPerMap){
37869      if( p->hShm>=0 ){
37870        osMunmap(p->apRegion[i], p->szRegion);
37871      }else{
37872        sqlite3_free(p->apRegion[i]);
37873      }
37874    }
37875    sqlite3_free(p->apRegion);
37876    if( p->hShm>=0 ){
37877      robust_close(pFd, p->hShm, __LINE__);
37878      p->hShm = -1;
37879    }
37880    p->pInode->pShmNode = 0;
37881    sqlite3_free(p);
37882  }
37883}
37884
37885/*
37886** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
37887** take it now. Return SQLITE_OK if successful, or an SQLite error
37888** code otherwise.
37889**
37890** If the DMS cannot be locked because this is a readonly_shm=1
37891** connection and no other process already holds a lock, return
37892** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
37893*/
37894static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){
37895  struct flock lock;
37896  int rc = SQLITE_OK;
37897
37898  /* Use F_GETLK to determine the locks other processes are holding
37899  ** on the DMS byte. If it indicates that another process is holding
37900  ** a SHARED lock, then this process may also take a SHARED lock
37901  ** and proceed with opening the *-shm file.
37902  **
37903  ** Or, if no other process is holding any lock, then this process
37904  ** is the first to open it. In this case take an EXCLUSIVE lock on the
37905  ** DMS byte and truncate the *-shm file to zero bytes in size. Then
37906  ** downgrade to a SHARED lock on the DMS byte.
37907  **
37908  ** If another process is holding an EXCLUSIVE lock on the DMS byte,
37909  ** return SQLITE_BUSY to the caller (it will try again). An earlier
37910  ** version of this code attempted the SHARED lock at this point. But
37911  ** this introduced a subtle race condition: if the process holding
37912  ** EXCLUSIVE failed just before truncating the *-shm file, then this
37913  ** process might open and use the *-shm file without truncating it.
37914  ** And if the *-shm file has been corrupted by a power failure or
37915  ** system crash, the database itself may also become corrupt.  */
37916  lock.l_whence = SEEK_SET;
37917  lock.l_start = UNIX_SHM_DMS;
37918  lock.l_len = 1;
37919  lock.l_type = F_WRLCK;
37920  if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
37921    rc = SQLITE_IOERR_LOCK;
37922  }else if( lock.l_type==F_UNLCK ){
37923    if( pShmNode->isReadonly ){
37924      pShmNode->isUnlocked = 1;
37925      rc = SQLITE_READONLY_CANTINIT;
37926    }else{
37927      rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1);
37928      /* The first connection to attach must truncate the -shm file.  We
37929      ** truncate to 3 bytes (an arbitrary small number, less than the
37930      ** -shm header size) rather than 0 as a system debugging aid, to
37931      ** help detect if a -shm file truncation is legitimate or is the work
37932      ** or a rogue process. */
37933      if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){
37934        rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
37935      }
37936    }
37937  }else if( lock.l_type==F_WRLCK ){
37938    rc = SQLITE_BUSY;
37939  }
37940
37941  if( rc==SQLITE_OK ){
37942    assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK );
37943    rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1);
37944  }
37945  return rc;
37946}
37947
37948/*
37949** Open a shared-memory area associated with open database file pDbFd.
37950** This particular implementation uses mmapped files.
37951**
37952** The file used to implement shared-memory is in the same directory
37953** as the open database file and has the same name as the open database
37954** file with the "-shm" suffix added.  For example, if the database file
37955** is "/home/user1/config.db" then the file that is created and mmapped
37956** for shared memory will be called "/home/user1/config.db-shm".
37957**
37958** Another approach to is to use files in /dev/shm or /dev/tmp or an
37959** some other tmpfs mount. But if a file in a different directory
37960** from the database file is used, then differing access permissions
37961** or a chroot() might cause two different processes on the same
37962** database to end up using different files for shared memory -
37963** meaning that their memory would not really be shared - resulting
37964** in database corruption.  Nevertheless, this tmpfs file usage
37965** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
37966** or the equivalent.  The use of the SQLITE_SHM_DIRECTORY compile-time
37967** option results in an incompatible build of SQLite;  builds of SQLite
37968** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
37969** same database file at the same time, database corruption will likely
37970** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
37971** "unsupported" and may go away in a future SQLite release.
37972**
37973** When opening a new shared-memory file, if no other instances of that
37974** file are currently open, in this process or in other processes, then
37975** the file must be truncated to zero length or have its header cleared.
37976**
37977** If the original database file (pDbFd) is using the "unix-excl" VFS
37978** that means that an exclusive lock is held on the database file and
37979** that no other processes are able to read or write the database.  In
37980** that case, we do not really need shared memory.  No shared memory
37981** file is created.  The shared memory will be simulated with heap memory.
37982*/
37983static int unixOpenSharedMemory(unixFile *pDbFd){
37984  struct unixShm *p = 0;          /* The connection to be opened */
37985  struct unixShmNode *pShmNode;   /* The underlying mmapped file */
37986  int rc = SQLITE_OK;             /* Result code */
37987  unixInodeInfo *pInode;          /* The inode of fd */
37988  char *zShm;             /* Name of the file used for SHM */
37989  int nShmFilename;               /* Size of the SHM filename in bytes */
37990
37991  /* Allocate space for the new unixShm object. */
37992  p = sqlite3_malloc64( sizeof(*p) );
37993  if( p==0 ) return SQLITE_NOMEM_BKPT;
37994  memset(p, 0, sizeof(*p));
37995  assert( pDbFd->pShm==0 );
37996
37997  /* Check to see if a unixShmNode object already exists. Reuse an existing
37998  ** one if present. Create a new one if necessary.
37999  */
38000  assert( unixFileMutexNotheld(pDbFd) );
38001  unixEnterMutex();
38002  pInode = pDbFd->pInode;
38003  pShmNode = pInode->pShmNode;
38004  if( pShmNode==0 ){
38005    struct stat sStat;                 /* fstat() info for database file */
38006#ifndef SQLITE_SHM_DIRECTORY
38007    const char *zBasePath = pDbFd->zPath;
38008#endif
38009
38010    /* Call fstat() to figure out the permissions on the database file. If
38011    ** a new *-shm file is created, an attempt will be made to create it
38012    ** with the same permissions.
38013    */
38014    if( osFstat(pDbFd->h, &sStat) ){
38015      rc = SQLITE_IOERR_FSTAT;
38016      goto shm_open_err;
38017    }
38018
38019#ifdef SQLITE_SHM_DIRECTORY
38020    nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
38021#else
38022    nShmFilename = 6 + (int)strlen(zBasePath);
38023#endif
38024    pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
38025    if( pShmNode==0 ){
38026      rc = SQLITE_NOMEM_BKPT;
38027      goto shm_open_err;
38028    }
38029    memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
38030    zShm = pShmNode->zFilename = (char*)&pShmNode[1];
38031#ifdef SQLITE_SHM_DIRECTORY
38032    sqlite3_snprintf(nShmFilename, zShm,
38033                     SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
38034                     (u32)sStat.st_ino, (u32)sStat.st_dev);
38035#else
38036    sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
38037    sqlite3FileSuffix3(pDbFd->zPath, zShm);
38038#endif
38039    pShmNode->hShm = -1;
38040    pDbFd->pInode->pShmNode = pShmNode;
38041    pShmNode->pInode = pDbFd->pInode;
38042    if( sqlite3GlobalConfig.bCoreMutex ){
38043      pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
38044      if( pShmNode->pShmMutex==0 ){
38045        rc = SQLITE_NOMEM_BKPT;
38046        goto shm_open_err;
38047      }
38048    }
38049
38050    if( pInode->bProcessLock==0 ){
38051      if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
38052        pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT|O_NOFOLLOW,
38053                                     (sStat.st_mode&0777));
38054      }
38055      if( pShmNode->hShm<0 ){
38056        pShmNode->hShm = robust_open(zShm, O_RDONLY|O_NOFOLLOW,
38057                                     (sStat.st_mode&0777));
38058        if( pShmNode->hShm<0 ){
38059          rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm);
38060          goto shm_open_err;
38061        }
38062        pShmNode->isReadonly = 1;
38063      }
38064
38065      /* If this process is running as root, make sure that the SHM file
38066      ** is owned by the same user that owns the original database.  Otherwise,
38067      ** the original owner will not be able to connect.
38068      */
38069      robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
38070
38071      rc = unixLockSharedMemory(pDbFd, pShmNode);
38072      if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
38073    }
38074  }
38075
38076  /* Make the new connection a child of the unixShmNode */
38077  p->pShmNode = pShmNode;
38078#ifdef SQLITE_DEBUG
38079  p->id = pShmNode->nextShmId++;
38080#endif
38081  pShmNode->nRef++;
38082  pDbFd->pShm = p;
38083  unixLeaveMutex();
38084
38085  /* The reference count on pShmNode has already been incremented under
38086  ** the cover of the unixEnterMutex() mutex and the pointer from the
38087  ** new (struct unixShm) object to the pShmNode has been set. All that is
38088  ** left to do is to link the new object into the linked list starting
38089  ** at pShmNode->pFirst. This must be done while holding the
38090  ** pShmNode->pShmMutex.
38091  */
38092  sqlite3_mutex_enter(pShmNode->pShmMutex);
38093  p->pNext = pShmNode->pFirst;
38094  pShmNode->pFirst = p;
38095  sqlite3_mutex_leave(pShmNode->pShmMutex);
38096  return rc;
38097
38098  /* Jump here on any error */
38099shm_open_err:
38100  unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
38101  sqlite3_free(p);
38102  unixLeaveMutex();
38103  return rc;
38104}
38105
38106/*
38107** This function is called to obtain a pointer to region iRegion of the
38108** shared-memory associated with the database file fd. Shared-memory regions
38109** are numbered starting from zero. Each shared-memory region is szRegion
38110** bytes in size.
38111**
38112** If an error occurs, an error code is returned and *pp is set to NULL.
38113**
38114** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
38115** region has not been allocated (by any client, including one running in a
38116** separate process), then *pp is set to NULL and SQLITE_OK returned. If
38117** bExtend is non-zero and the requested shared-memory region has not yet
38118** been allocated, it is allocated by this function.
38119**
38120** If the shared-memory region has already been allocated or is allocated by
38121** this call as described above, then it is mapped into this processes
38122** address space (if it is not already), *pp is set to point to the mapped
38123** memory and SQLITE_OK returned.
38124*/
38125static int unixShmMap(
38126  sqlite3_file *fd,               /* Handle open on database file */
38127  int iRegion,                    /* Region to retrieve */
38128  int szRegion,                   /* Size of regions */
38129  int bExtend,                    /* True to extend file if necessary */
38130  void volatile **pp              /* OUT: Mapped memory */
38131){
38132  unixFile *pDbFd = (unixFile*)fd;
38133  unixShm *p;
38134  unixShmNode *pShmNode;
38135  int rc = SQLITE_OK;
38136  int nShmPerMap = unixShmRegionPerMap();
38137  int nReqRegion;
38138
38139  /* If the shared-memory file has not yet been opened, open it now. */
38140  if( pDbFd->pShm==0 ){
38141    rc = unixOpenSharedMemory(pDbFd);
38142    if( rc!=SQLITE_OK ) return rc;
38143  }
38144
38145  p = pDbFd->pShm;
38146  pShmNode = p->pShmNode;
38147  sqlite3_mutex_enter(pShmNode->pShmMutex);
38148  if( pShmNode->isUnlocked ){
38149    rc = unixLockSharedMemory(pDbFd, pShmNode);
38150    if( rc!=SQLITE_OK ) goto shmpage_out;
38151    pShmNode->isUnlocked = 0;
38152  }
38153  assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
38154  assert( pShmNode->pInode==pDbFd->pInode );
38155  assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
38156  assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
38157
38158  /* Minimum number of regions required to be mapped. */
38159  nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
38160
38161  if( pShmNode->nRegion<nReqRegion ){
38162    char **apNew;                      /* New apRegion[] array */
38163    int nByte = nReqRegion*szRegion;   /* Minimum required file size */
38164    struct stat sStat;                 /* Used by fstat() */
38165
38166    pShmNode->szRegion = szRegion;
38167
38168    if( pShmNode->hShm>=0 ){
38169      /* The requested region is not mapped into this processes address space.
38170      ** Check to see if it has been allocated (i.e. if the wal-index file is
38171      ** large enough to contain the requested region).
38172      */
38173      if( osFstat(pShmNode->hShm, &sStat) ){
38174        rc = SQLITE_IOERR_SHMSIZE;
38175        goto shmpage_out;
38176      }
38177
38178      if( sStat.st_size<nByte ){
38179        /* The requested memory region does not exist. If bExtend is set to
38180        ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
38181        */
38182        if( !bExtend ){
38183          goto shmpage_out;
38184        }
38185
38186        /* Alternatively, if bExtend is true, extend the file. Do this by
38187        ** writing a single byte to the end of each (OS) page being
38188        ** allocated or extended. Technically, we need only write to the
38189        ** last page in order to extend the file. But writing to all new
38190        ** pages forces the OS to allocate them immediately, which reduces
38191        ** the chances of SIGBUS while accessing the mapped region later on.
38192        */
38193        else{
38194          static const int pgsz = 4096;
38195          int iPg;
38196
38197          /* Write to the last byte of each newly allocated or extended page */
38198          assert( (nByte % pgsz)==0 );
38199          for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
38200            int x = 0;
38201            if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
38202              const char *zFile = pShmNode->zFilename;
38203              rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
38204              goto shmpage_out;
38205            }
38206          }
38207        }
38208      }
38209    }
38210
38211    /* Map the requested memory region into this processes address space. */
38212    apNew = (char **)sqlite3_realloc(
38213        pShmNode->apRegion, nReqRegion*sizeof(char *)
38214    );
38215    if( !apNew ){
38216      rc = SQLITE_IOERR_NOMEM_BKPT;
38217      goto shmpage_out;
38218    }
38219    pShmNode->apRegion = apNew;
38220    while( pShmNode->nRegion<nReqRegion ){
38221      int nMap = szRegion*nShmPerMap;
38222      int i;
38223      void *pMem;
38224      if( pShmNode->hShm>=0 ){
38225        pMem = osMmap(0, nMap,
38226            pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
38227            MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
38228        );
38229        if( pMem==MAP_FAILED ){
38230          rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
38231          goto shmpage_out;
38232        }
38233      }else{
38234        pMem = sqlite3_malloc64(nMap);
38235        if( pMem==0 ){
38236          rc = SQLITE_NOMEM_BKPT;
38237          goto shmpage_out;
38238        }
38239        memset(pMem, 0, nMap);
38240      }
38241
38242      for(i=0; i<nShmPerMap; i++){
38243        pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
38244      }
38245      pShmNode->nRegion += nShmPerMap;
38246    }
38247  }
38248
38249shmpage_out:
38250  if( pShmNode->nRegion>iRegion ){
38251    *pp = pShmNode->apRegion[iRegion];
38252  }else{
38253    *pp = 0;
38254  }
38255  if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
38256  sqlite3_mutex_leave(pShmNode->pShmMutex);
38257  return rc;
38258}
38259
38260/*
38261** Change the lock state for a shared-memory segment.
38262**
38263** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
38264** different here than in posix.  In xShmLock(), one can go from unlocked
38265** to shared and back or from unlocked to exclusive and back.  But one may
38266** not go from shared to exclusive or from exclusive to shared.
38267*/
38268static int unixShmLock(
38269  sqlite3_file *fd,          /* Database file holding the shared memory */
38270  int ofst,                  /* First lock to acquire or release */
38271  int n,                     /* Number of locks to acquire or release */
38272  int flags                  /* What to do with the lock */
38273){
38274  unixFile *pDbFd = (unixFile*)fd;      /* Connection holding shared memory */
38275  unixShm *p = pDbFd->pShm;             /* The shared memory being locked */
38276  unixShm *pX;                          /* For looping over all siblings */
38277  unixShmNode *pShmNode = p->pShmNode;  /* The underlying file iNode */
38278  int rc = SQLITE_OK;                   /* Result code */
38279  u16 mask;                             /* Mask of locks to take or release */
38280
38281  assert( pShmNode==pDbFd->pInode->pShmNode );
38282  assert( pShmNode->pInode==pDbFd->pInode );
38283  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
38284  assert( n>=1 );
38285  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
38286       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
38287       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
38288       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
38289  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
38290  assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
38291  assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
38292
38293  /* Check that, if this to be a blocking lock, no locks that occur later
38294  ** in the following list than the lock being obtained are already held:
38295  **
38296  **   1. Checkpointer lock (ofst==1).
38297  **   2. Write lock (ofst==0).
38298  **   3. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK).
38299  **
38300  ** In other words, if this is a blocking lock, none of the locks that
38301  ** occur later in the above list than the lock being obtained may be
38302  ** held.  */
38303#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
38304  assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
38305         (ofst!=2)                                   /* not RECOVER */
38306      && (ofst!=1 || (p->exclMask|p->sharedMask)==0)
38307      && (ofst!=0 || (p->exclMask|p->sharedMask)<3)
38308      && (ofst<3  || (p->exclMask|p->sharedMask)<(1<<ofst))
38309  ));
38310#endif
38311
38312  mask = (1<<(ofst+n)) - (1<<ofst);
38313  assert( n>1 || mask==(1<<ofst) );
38314  sqlite3_mutex_enter(pShmNode->pShmMutex);
38315  if( flags & SQLITE_SHM_UNLOCK ){
38316    u16 allMask = 0; /* Mask of locks held by siblings */
38317
38318    /* See if any siblings hold this same lock */
38319    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
38320      if( pX==p ) continue;
38321      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
38322      allMask |= pX->sharedMask;
38323    }
38324
38325    /* Unlock the system-level locks */
38326    if( (mask & allMask)==0 ){
38327      rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n);
38328    }else{
38329      rc = SQLITE_OK;
38330    }
38331
38332    /* Undo the local locks */
38333    if( rc==SQLITE_OK ){
38334      p->exclMask &= ~mask;
38335      p->sharedMask &= ~mask;
38336    }
38337  }else if( flags & SQLITE_SHM_SHARED ){
38338    u16 allShared = 0;  /* Union of locks held by connections other than "p" */
38339
38340    /* Find out which shared locks are already held by sibling connections.
38341    ** If any sibling already holds an exclusive lock, go ahead and return
38342    ** SQLITE_BUSY.
38343    */
38344    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
38345      if( (pX->exclMask & mask)!=0 ){
38346        rc = SQLITE_BUSY;
38347        break;
38348      }
38349      allShared |= pX->sharedMask;
38350    }
38351
38352    /* Get shared locks at the system level, if necessary */
38353    if( rc==SQLITE_OK ){
38354      if( (allShared & mask)==0 ){
38355        rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n);
38356      }else{
38357        rc = SQLITE_OK;
38358      }
38359    }
38360
38361    /* Get the local shared locks */
38362    if( rc==SQLITE_OK ){
38363      p->sharedMask |= mask;
38364    }
38365  }else{
38366    /* Make sure no sibling connections hold locks that will block this
38367    ** lock.  If any do, return SQLITE_BUSY right away.
38368    */
38369    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
38370      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
38371        rc = SQLITE_BUSY;
38372        break;
38373      }
38374    }
38375
38376    /* Get the exclusive locks at the system level.  Then if successful
38377    ** also mark the local connection as being locked.
38378    */
38379    if( rc==SQLITE_OK ){
38380      rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n);
38381      if( rc==SQLITE_OK ){
38382        assert( (p->sharedMask & mask)==0 );
38383        p->exclMask |= mask;
38384      }
38385    }
38386  }
38387  sqlite3_mutex_leave(pShmNode->pShmMutex);
38388  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
38389           p->id, osGetpid(0), p->sharedMask, p->exclMask));
38390  return rc;
38391}
38392
38393/*
38394** Implement a memory barrier or memory fence on shared memory.
38395**
38396** All loads and stores begun before the barrier must complete before
38397** any load or store begun after the barrier.
38398*/
38399static void unixShmBarrier(
38400  sqlite3_file *fd                /* Database file holding the shared memory */
38401){
38402  UNUSED_PARAMETER(fd);
38403  sqlite3MemoryBarrier();         /* compiler-defined memory barrier */
38404  assert( fd->pMethods->xLock==nolockLock
38405       || unixFileMutexNotheld((unixFile*)fd)
38406  );
38407  unixEnterMutex();               /* Also mutex, for redundancy */
38408  unixLeaveMutex();
38409}
38410
38411/*
38412** Close a connection to shared-memory.  Delete the underlying
38413** storage if deleteFlag is true.
38414**
38415** If there is no shared memory associated with the connection then this
38416** routine is a harmless no-op.
38417*/
38418static int unixShmUnmap(
38419  sqlite3_file *fd,               /* The underlying database file */
38420  int deleteFlag                  /* Delete shared-memory if true */
38421){
38422  unixShm *p;                     /* The connection to be closed */
38423  unixShmNode *pShmNode;          /* The underlying shared-memory file */
38424  unixShm **pp;                   /* For looping over sibling connections */
38425  unixFile *pDbFd;                /* The underlying database file */
38426
38427  pDbFd = (unixFile*)fd;
38428  p = pDbFd->pShm;
38429  if( p==0 ) return SQLITE_OK;
38430  pShmNode = p->pShmNode;
38431
38432  assert( pShmNode==pDbFd->pInode->pShmNode );
38433  assert( pShmNode->pInode==pDbFd->pInode );
38434
38435  /* Remove connection p from the set of connections associated
38436  ** with pShmNode */
38437  sqlite3_mutex_enter(pShmNode->pShmMutex);
38438  for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
38439  *pp = p->pNext;
38440
38441  /* Free the connection p */
38442  sqlite3_free(p);
38443  pDbFd->pShm = 0;
38444  sqlite3_mutex_leave(pShmNode->pShmMutex);
38445
38446  /* If pShmNode->nRef has reached 0, then close the underlying
38447  ** shared-memory file, too */
38448  assert( unixFileMutexNotheld(pDbFd) );
38449  unixEnterMutex();
38450  assert( pShmNode->nRef>0 );
38451  pShmNode->nRef--;
38452  if( pShmNode->nRef==0 ){
38453    if( deleteFlag && pShmNode->hShm>=0 ){
38454      osUnlink(pShmNode->zFilename);
38455    }
38456    unixShmPurge(pDbFd);
38457  }
38458  unixLeaveMutex();
38459
38460  return SQLITE_OK;
38461}
38462
38463
38464#else
38465# define unixShmMap     0
38466# define unixShmLock    0
38467# define unixShmBarrier 0
38468# define unixShmUnmap   0
38469#endif /* #ifndef SQLITE_OMIT_WAL */
38470
38471#if SQLITE_MAX_MMAP_SIZE>0
38472/*
38473** If it is currently memory mapped, unmap file pFd.
38474*/
38475static void unixUnmapfile(unixFile *pFd){
38476  assert( pFd->nFetchOut==0 );
38477  if( pFd->pMapRegion ){
38478    osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
38479    pFd->pMapRegion = 0;
38480    pFd->mmapSize = 0;
38481    pFd->mmapSizeActual = 0;
38482  }
38483}
38484
38485/*
38486** Attempt to set the size of the memory mapping maintained by file
38487** descriptor pFd to nNew bytes. Any existing mapping is discarded.
38488**
38489** If successful, this function sets the following variables:
38490**
38491**       unixFile.pMapRegion
38492**       unixFile.mmapSize
38493**       unixFile.mmapSizeActual
38494**
38495** If unsuccessful, an error message is logged via sqlite3_log() and
38496** the three variables above are zeroed. In this case SQLite should
38497** continue accessing the database using the xRead() and xWrite()
38498** methods.
38499*/
38500static void unixRemapfile(
38501  unixFile *pFd,                  /* File descriptor object */
38502  i64 nNew                        /* Required mapping size */
38503){
38504  const char *zErr = "mmap";
38505  int h = pFd->h;                      /* File descriptor open on db file */
38506  u8 *pOrig = (u8 *)pFd->pMapRegion;   /* Pointer to current file mapping */
38507  i64 nOrig = pFd->mmapSizeActual;     /* Size of pOrig region in bytes */
38508  u8 *pNew = 0;                        /* Location of new mapping */
38509  int flags = PROT_READ;               /* Flags to pass to mmap() */
38510
38511  assert( pFd->nFetchOut==0 );
38512  assert( nNew>pFd->mmapSize );
38513  assert( nNew<=pFd->mmapSizeMax );
38514  assert( nNew>0 );
38515  assert( pFd->mmapSizeActual>=pFd->mmapSize );
38516  assert( MAP_FAILED!=0 );
38517
38518#ifdef SQLITE_MMAP_READWRITE
38519  if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
38520#endif
38521
38522  if( pOrig ){
38523#if HAVE_MREMAP
38524    i64 nReuse = pFd->mmapSize;
38525#else
38526    const int szSyspage = osGetpagesize();
38527    i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
38528#endif
38529    u8 *pReq = &pOrig[nReuse];
38530
38531    /* Unmap any pages of the existing mapping that cannot be reused. */
38532    if( nReuse!=nOrig ){
38533      osMunmap(pReq, nOrig-nReuse);
38534    }
38535
38536#if HAVE_MREMAP
38537    pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
38538    zErr = "mremap";
38539#else
38540    pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
38541    if( pNew!=MAP_FAILED ){
38542      if( pNew!=pReq ){
38543        osMunmap(pNew, nNew - nReuse);
38544        pNew = 0;
38545      }else{
38546        pNew = pOrig;
38547      }
38548    }
38549#endif
38550
38551    /* The attempt to extend the existing mapping failed. Free it. */
38552    if( pNew==MAP_FAILED || pNew==0 ){
38553      osMunmap(pOrig, nReuse);
38554    }
38555  }
38556
38557  /* If pNew is still NULL, try to create an entirely new mapping. */
38558  if( pNew==0 ){
38559    pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
38560  }
38561
38562  if( pNew==MAP_FAILED ){
38563    pNew = 0;
38564    nNew = 0;
38565    unixLogError(SQLITE_OK, zErr, pFd->zPath);
38566
38567    /* If the mmap() above failed, assume that all subsequent mmap() calls
38568    ** will probably fail too. Fall back to using xRead/xWrite exclusively
38569    ** in this case.  */
38570    pFd->mmapSizeMax = 0;
38571  }
38572  pFd->pMapRegion = (void *)pNew;
38573  pFd->mmapSize = pFd->mmapSizeActual = nNew;
38574}
38575
38576/*
38577** Memory map or remap the file opened by file-descriptor pFd (if the file
38578** is already mapped, the existing mapping is replaced by the new). Or, if
38579** there already exists a mapping for this file, and there are still
38580** outstanding xFetch() references to it, this function is a no-op.
38581**
38582** If parameter nByte is non-negative, then it is the requested size of
38583** the mapping to create. Otherwise, if nByte is less than zero, then the
38584** requested size is the size of the file on disk. The actual size of the
38585** created mapping is either the requested size or the value configured
38586** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
38587**
38588** SQLITE_OK is returned if no error occurs (even if the mapping is not
38589** recreated as a result of outstanding references) or an SQLite error
38590** code otherwise.
38591*/
38592static int unixMapfile(unixFile *pFd, i64 nMap){
38593  assert( nMap>=0 || pFd->nFetchOut==0 );
38594  assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
38595  if( pFd->nFetchOut>0 ) return SQLITE_OK;
38596
38597  if( nMap<0 ){
38598    struct stat statbuf;          /* Low-level file information */
38599    if( osFstat(pFd->h, &statbuf) ){
38600      return SQLITE_IOERR_FSTAT;
38601    }
38602    nMap = statbuf.st_size;
38603  }
38604  if( nMap>pFd->mmapSizeMax ){
38605    nMap = pFd->mmapSizeMax;
38606  }
38607
38608  assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
38609  if( nMap!=pFd->mmapSize ){
38610    unixRemapfile(pFd, nMap);
38611  }
38612
38613  return SQLITE_OK;
38614}
38615#endif /* SQLITE_MAX_MMAP_SIZE>0 */
38616
38617/*
38618** If possible, return a pointer to a mapping of file fd starting at offset
38619** iOff. The mapping must be valid for at least nAmt bytes.
38620**
38621** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
38622** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
38623** Finally, if an error does occur, return an SQLite error code. The final
38624** value of *pp is undefined in this case.
38625**
38626** If this function does return a pointer, the caller must eventually
38627** release the reference by calling unixUnfetch().
38628*/
38629static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
38630#if SQLITE_MAX_MMAP_SIZE>0
38631  unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
38632#endif
38633  *pp = 0;
38634
38635#if SQLITE_MAX_MMAP_SIZE>0
38636  if( pFd->mmapSizeMax>0 ){
38637    if( pFd->pMapRegion==0 ){
38638      int rc = unixMapfile(pFd, -1);
38639      if( rc!=SQLITE_OK ) return rc;
38640    }
38641    if( pFd->mmapSize >= iOff+nAmt ){
38642      *pp = &((u8 *)pFd->pMapRegion)[iOff];
38643      pFd->nFetchOut++;
38644    }
38645  }
38646#endif
38647  return SQLITE_OK;
38648}
38649
38650/*
38651** If the third argument is non-NULL, then this function releases a
38652** reference obtained by an earlier call to unixFetch(). The second
38653** argument passed to this function must be the same as the corresponding
38654** argument that was passed to the unixFetch() invocation.
38655**
38656** Or, if the third argument is NULL, then this function is being called
38657** to inform the VFS layer that, according to POSIX, any existing mapping
38658** may now be invalid and should be unmapped.
38659*/
38660static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
38661#if SQLITE_MAX_MMAP_SIZE>0
38662  unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
38663  UNUSED_PARAMETER(iOff);
38664
38665  /* If p==0 (unmap the entire file) then there must be no outstanding
38666  ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
38667  ** then there must be at least one outstanding.  */
38668  assert( (p==0)==(pFd->nFetchOut==0) );
38669
38670  /* If p!=0, it must match the iOff value. */
38671  assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
38672
38673  if( p ){
38674    pFd->nFetchOut--;
38675  }else{
38676    unixUnmapfile(pFd);
38677  }
38678
38679  assert( pFd->nFetchOut>=0 );
38680#else
38681  UNUSED_PARAMETER(fd);
38682  UNUSED_PARAMETER(p);
38683  UNUSED_PARAMETER(iOff);
38684#endif
38685  return SQLITE_OK;
38686}
38687
38688/*
38689** Here ends the implementation of all sqlite3_file methods.
38690**
38691********************** End sqlite3_file Methods *******************************
38692******************************************************************************/
38693
38694/*
38695** This division contains definitions of sqlite3_io_methods objects that
38696** implement various file locking strategies.  It also contains definitions
38697** of "finder" functions.  A finder-function is used to locate the appropriate
38698** sqlite3_io_methods object for a particular database file.  The pAppData
38699** field of the sqlite3_vfs VFS objects are initialized to be pointers to
38700** the correct finder-function for that VFS.
38701**
38702** Most finder functions return a pointer to a fixed sqlite3_io_methods
38703** object.  The only interesting finder-function is autolockIoFinder, which
38704** looks at the filesystem type and tries to guess the best locking
38705** strategy from that.
38706**
38707** For finder-function F, two objects are created:
38708**
38709**    (1) The real finder-function named "FImpt()".
38710**
38711**    (2) A constant pointer to this function named just "F".
38712**
38713**
38714** A pointer to the F pointer is used as the pAppData value for VFS
38715** objects.  We have to do this instead of letting pAppData point
38716** directly at the finder-function since C90 rules prevent a void*
38717** from be cast into a function pointer.
38718**
38719**
38720** Each instance of this macro generates two objects:
38721**
38722**   *  A constant sqlite3_io_methods object call METHOD that has locking
38723**      methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
38724**
38725**   *  An I/O method finder function called FINDER that returns a pointer
38726**      to the METHOD object in the previous bullet.
38727*/
38728#define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP)     \
38729static const sqlite3_io_methods METHOD = {                                   \
38730   VERSION,                    /* iVersion */                                \
38731   CLOSE,                      /* xClose */                                  \
38732   unixRead,                   /* xRead */                                   \
38733   unixWrite,                  /* xWrite */                                  \
38734   unixTruncate,               /* xTruncate */                               \
38735   unixSync,                   /* xSync */                                   \
38736   unixFileSize,               /* xFileSize */                               \
38737   LOCK,                       /* xLock */                                   \
38738   UNLOCK,                     /* xUnlock */                                 \
38739   CKLOCK,                     /* xCheckReservedLock */                      \
38740   unixFileControl,            /* xFileControl */                            \
38741   unixSectorSize,             /* xSectorSize */                             \
38742   unixDeviceCharacteristics,  /* xDeviceCapabilities */                     \
38743   SHMMAP,                     /* xShmMap */                                 \
38744   unixShmLock,                /* xShmLock */                                \
38745   unixShmBarrier,             /* xShmBarrier */                             \
38746   unixShmUnmap,               /* xShmUnmap */                               \
38747   unixFetch,                  /* xFetch */                                  \
38748   unixUnfetch,                /* xUnfetch */                                \
38749};                                                                           \
38750static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
38751  UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
38752  return &METHOD;                                                            \
38753}                                                                            \
38754static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
38755    = FINDER##Impl;
38756
38757/*
38758** Here are all of the sqlite3_io_methods objects for each of the
38759** locking strategies.  Functions that return pointers to these methods
38760** are also created.
38761*/
38762IOMETHODS(
38763  posixIoFinder,            /* Finder function name */
38764  posixIoMethods,           /* sqlite3_io_methods object name */
38765  3,                        /* shared memory and mmap are enabled */
38766  unixClose,                /* xClose method */
38767  unixLock,                 /* xLock method */
38768  unixUnlock,               /* xUnlock method */
38769  unixCheckReservedLock,    /* xCheckReservedLock method */
38770  unixShmMap                /* xShmMap method */
38771)
38772IOMETHODS(
38773  nolockIoFinder,           /* Finder function name */
38774  nolockIoMethods,          /* sqlite3_io_methods object name */
38775  3,                        /* shared memory and mmap are enabled */
38776  nolockClose,              /* xClose method */
38777  nolockLock,               /* xLock method */
38778  nolockUnlock,             /* xUnlock method */
38779  nolockCheckReservedLock,  /* xCheckReservedLock method */
38780  0                         /* xShmMap method */
38781)
38782IOMETHODS(
38783  dotlockIoFinder,          /* Finder function name */
38784  dotlockIoMethods,         /* sqlite3_io_methods object name */
38785  1,                        /* shared memory is disabled */
38786  dotlockClose,             /* xClose method */
38787  dotlockLock,              /* xLock method */
38788  dotlockUnlock,            /* xUnlock method */
38789  dotlockCheckReservedLock, /* xCheckReservedLock method */
38790  0                         /* xShmMap method */
38791)
38792
38793#if SQLITE_ENABLE_LOCKING_STYLE
38794IOMETHODS(
38795  flockIoFinder,            /* Finder function name */
38796  flockIoMethods,           /* sqlite3_io_methods object name */
38797  1,                        /* shared memory is disabled */
38798  flockClose,               /* xClose method */
38799  flockLock,                /* xLock method */
38800  flockUnlock,              /* xUnlock method */
38801  flockCheckReservedLock,   /* xCheckReservedLock method */
38802  0                         /* xShmMap method */
38803)
38804#endif
38805
38806#if OS_VXWORKS
38807IOMETHODS(
38808  semIoFinder,              /* Finder function name */
38809  semIoMethods,             /* sqlite3_io_methods object name */
38810  1,                        /* shared memory is disabled */
38811  semXClose,                /* xClose method */
38812  semXLock,                 /* xLock method */
38813  semXUnlock,               /* xUnlock method */
38814  semXCheckReservedLock,    /* xCheckReservedLock method */
38815  0                         /* xShmMap method */
38816)
38817#endif
38818
38819#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
38820IOMETHODS(
38821  afpIoFinder,              /* Finder function name */
38822  afpIoMethods,             /* sqlite3_io_methods object name */
38823  1,                        /* shared memory is disabled */
38824  afpClose,                 /* xClose method */
38825  afpLock,                  /* xLock method */
38826  afpUnlock,                /* xUnlock method */
38827  afpCheckReservedLock,     /* xCheckReservedLock method */
38828  0                         /* xShmMap method */
38829)
38830#endif
38831
38832/*
38833** The proxy locking method is a "super-method" in the sense that it
38834** opens secondary file descriptors for the conch and lock files and
38835** it uses proxy, dot-file, AFP, and flock() locking methods on those
38836** secondary files.  For this reason, the division that implements
38837** proxy locking is located much further down in the file.  But we need
38838** to go ahead and define the sqlite3_io_methods and finder function
38839** for proxy locking here.  So we forward declare the I/O methods.
38840*/
38841#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
38842static int proxyClose(sqlite3_file*);
38843static int proxyLock(sqlite3_file*, int);
38844static int proxyUnlock(sqlite3_file*, int);
38845static int proxyCheckReservedLock(sqlite3_file*, int*);
38846IOMETHODS(
38847  proxyIoFinder,            /* Finder function name */
38848  proxyIoMethods,           /* sqlite3_io_methods object name */
38849  1,                        /* shared memory is disabled */
38850  proxyClose,               /* xClose method */
38851  proxyLock,                /* xLock method */
38852  proxyUnlock,              /* xUnlock method */
38853  proxyCheckReservedLock,   /* xCheckReservedLock method */
38854  0                         /* xShmMap method */
38855)
38856#endif
38857
38858/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
38859#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
38860IOMETHODS(
38861  nfsIoFinder,               /* Finder function name */
38862  nfsIoMethods,              /* sqlite3_io_methods object name */
38863  1,                         /* shared memory is disabled */
38864  unixClose,                 /* xClose method */
38865  unixLock,                  /* xLock method */
38866  nfsUnlock,                 /* xUnlock method */
38867  unixCheckReservedLock,     /* xCheckReservedLock method */
38868  0                          /* xShmMap method */
38869)
38870#endif
38871
38872#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
38873/*
38874** This "finder" function attempts to determine the best locking strategy
38875** for the database file "filePath".  It then returns the sqlite3_io_methods
38876** object that implements that strategy.
38877**
38878** This is for MacOSX only.
38879*/
38880static const sqlite3_io_methods *autolockIoFinderImpl(
38881  const char *filePath,    /* name of the database file */
38882  unixFile *pNew           /* open file object for the database file */
38883){
38884  static const struct Mapping {
38885    const char *zFilesystem;              /* Filesystem type name */
38886    const sqlite3_io_methods *pMethods;   /* Appropriate locking method */
38887  } aMap[] = {
38888    { "hfs",    &posixIoMethods },
38889    { "ufs",    &posixIoMethods },
38890    { "afpfs",  &afpIoMethods },
38891    { "smbfs",  &afpIoMethods },
38892    { "webdav", &nolockIoMethods },
38893    { 0, 0 }
38894  };
38895  int i;
38896  struct statfs fsInfo;
38897  struct flock lockInfo;
38898
38899  if( !filePath ){
38900    /* If filePath==NULL that means we are dealing with a transient file
38901    ** that does not need to be locked. */
38902    return &nolockIoMethods;
38903  }
38904  if( statfs(filePath, &fsInfo) != -1 ){
38905    if( fsInfo.f_flags & MNT_RDONLY ){
38906      return &nolockIoMethods;
38907    }
38908    for(i=0; aMap[i].zFilesystem; i++){
38909      if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
38910        return aMap[i].pMethods;
38911      }
38912    }
38913  }
38914
38915  /* Default case. Handles, amongst others, "nfs".
38916  ** Test byte-range lock using fcntl(). If the call succeeds,
38917  ** assume that the file-system supports POSIX style locks.
38918  */
38919  lockInfo.l_len = 1;
38920  lockInfo.l_start = 0;
38921  lockInfo.l_whence = SEEK_SET;
38922  lockInfo.l_type = F_RDLCK;
38923  if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
38924    if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
38925      return &nfsIoMethods;
38926    } else {
38927      return &posixIoMethods;
38928    }
38929  }else{
38930    return &dotlockIoMethods;
38931  }
38932}
38933static const sqlite3_io_methods
38934  *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
38935
38936#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
38937
38938#if OS_VXWORKS
38939/*
38940** This "finder" function for VxWorks checks to see if posix advisory
38941** locking works.  If it does, then that is what is used.  If it does not
38942** work, then fallback to named semaphore locking.
38943*/
38944static const sqlite3_io_methods *vxworksIoFinderImpl(
38945  const char *filePath,    /* name of the database file */
38946  unixFile *pNew           /* the open file object */
38947){
38948  struct flock lockInfo;
38949
38950  if( !filePath ){
38951    /* If filePath==NULL that means we are dealing with a transient file
38952    ** that does not need to be locked. */
38953    return &nolockIoMethods;
38954  }
38955
38956  /* Test if fcntl() is supported and use POSIX style locks.
38957  ** Otherwise fall back to the named semaphore method.
38958  */
38959  lockInfo.l_len = 1;
38960  lockInfo.l_start = 0;
38961  lockInfo.l_whence = SEEK_SET;
38962  lockInfo.l_type = F_RDLCK;
38963  if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
38964    return &posixIoMethods;
38965  }else{
38966    return &semIoMethods;
38967  }
38968}
38969static const sqlite3_io_methods
38970  *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl;
38971
38972#endif /* OS_VXWORKS */
38973
38974/*
38975** An abstract type for a pointer to an IO method finder function:
38976*/
38977typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
38978
38979
38980/****************************************************************************
38981**************************** sqlite3_vfs methods ****************************
38982**
38983** This division contains the implementation of methods on the
38984** sqlite3_vfs object.
38985*/
38986
38987/*
38988** Initialize the contents of the unixFile structure pointed to by pId.
38989*/
38990static int fillInUnixFile(
38991  sqlite3_vfs *pVfs,      /* Pointer to vfs object */
38992  int h,                  /* Open file descriptor of file being opened */
38993  sqlite3_file *pId,      /* Write to the unixFile structure here */
38994  const char *zFilename,  /* Name of the file being opened */
38995  int ctrlFlags           /* Zero or more UNIXFILE_* values */
38996){
38997  const sqlite3_io_methods *pLockingStyle;
38998  unixFile *pNew = (unixFile *)pId;
38999  int rc = SQLITE_OK;
39000
39001  assert( pNew->pInode==NULL );
39002
39003  /* No locking occurs in temporary files */
39004  assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
39005
39006  OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
39007  pNew->h = h;
39008  pNew->pVfs = pVfs;
39009  pNew->zPath = zFilename;
39010  pNew->ctrlFlags = (u8)ctrlFlags;
39011#if SQLITE_MAX_MMAP_SIZE>0
39012  pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
39013#endif
39014  if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
39015                           "psow", SQLITE_POWERSAFE_OVERWRITE) ){
39016    pNew->ctrlFlags |= UNIXFILE_PSOW;
39017  }
39018  if( strcmp(pVfs->zName,"unix-excl")==0 ){
39019    pNew->ctrlFlags |= UNIXFILE_EXCL;
39020  }
39021
39022#if OS_VXWORKS
39023  pNew->pId = vxworksFindFileId(zFilename);
39024  if( pNew->pId==0 ){
39025    ctrlFlags |= UNIXFILE_NOLOCK;
39026    rc = SQLITE_NOMEM_BKPT;
39027  }
39028#endif
39029
39030  if( ctrlFlags & UNIXFILE_NOLOCK ){
39031    pLockingStyle = &nolockIoMethods;
39032  }else{
39033    pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
39034#if SQLITE_ENABLE_LOCKING_STYLE
39035    /* Cache zFilename in the locking context (AFP and dotlock override) for
39036    ** proxyLock activation is possible (remote proxy is based on db name)
39037    ** zFilename remains valid until file is closed, to support */
39038    pNew->lockingContext = (void*)zFilename;
39039#endif
39040  }
39041
39042  if( pLockingStyle == &posixIoMethods
39043#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
39044    || pLockingStyle == &nfsIoMethods
39045#endif
39046  ){
39047    unixEnterMutex();
39048    rc = findInodeInfo(pNew, &pNew->pInode);
39049    if( rc!=SQLITE_OK ){
39050      /* If an error occurred in findInodeInfo(), close the file descriptor
39051      ** immediately, before releasing the mutex. findInodeInfo() may fail
39052      ** in two scenarios:
39053      **
39054      **   (a) A call to fstat() failed.
39055      **   (b) A malloc failed.
39056      **
39057      ** Scenario (b) may only occur if the process is holding no other
39058      ** file descriptors open on the same file. If there were other file
39059      ** descriptors on this file, then no malloc would be required by
39060      ** findInodeInfo(). If this is the case, it is quite safe to close
39061      ** handle h - as it is guaranteed that no posix locks will be released
39062      ** by doing so.
39063      **
39064      ** If scenario (a) caused the error then things are not so safe. The
39065      ** implicit assumption here is that if fstat() fails, things are in
39066      ** such bad shape that dropping a lock or two doesn't matter much.
39067      */
39068      robust_close(pNew, h, __LINE__);
39069      h = -1;
39070    }
39071    unixLeaveMutex();
39072  }
39073
39074#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
39075  else if( pLockingStyle == &afpIoMethods ){
39076    /* AFP locking uses the file path so it needs to be included in
39077    ** the afpLockingContext.
39078    */
39079    afpLockingContext *pCtx;
39080    pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
39081    if( pCtx==0 ){
39082      rc = SQLITE_NOMEM_BKPT;
39083    }else{
39084      /* NB: zFilename exists and remains valid until the file is closed
39085      ** according to requirement F11141.  So we do not need to make a
39086      ** copy of the filename. */
39087      pCtx->dbPath = zFilename;
39088      pCtx->reserved = 0;
39089      srandomdev();
39090      unixEnterMutex();
39091      rc = findInodeInfo(pNew, &pNew->pInode);
39092      if( rc!=SQLITE_OK ){
39093        sqlite3_free(pNew->lockingContext);
39094        robust_close(pNew, h, __LINE__);
39095        h = -1;
39096      }
39097      unixLeaveMutex();
39098    }
39099  }
39100#endif
39101
39102  else if( pLockingStyle == &dotlockIoMethods ){
39103    /* Dotfile locking uses the file path so it needs to be included in
39104    ** the dotlockLockingContext
39105    */
39106    char *zLockFile;
39107    int nFilename;
39108    assert( zFilename!=0 );
39109    nFilename = (int)strlen(zFilename) + 6;
39110    zLockFile = (char *)sqlite3_malloc64(nFilename);
39111    if( zLockFile==0 ){
39112      rc = SQLITE_NOMEM_BKPT;
39113    }else{
39114      sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
39115    }
39116    pNew->lockingContext = zLockFile;
39117  }
39118
39119#if OS_VXWORKS
39120  else if( pLockingStyle == &semIoMethods ){
39121    /* Named semaphore locking uses the file path so it needs to be
39122    ** included in the semLockingContext
39123    */
39124    unixEnterMutex();
39125    rc = findInodeInfo(pNew, &pNew->pInode);
39126    if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
39127      char *zSemName = pNew->pInode->aSemName;
39128      int n;
39129      sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
39130                       pNew->pId->zCanonicalName);
39131      for( n=1; zSemName[n]; n++ )
39132        if( zSemName[n]=='/' ) zSemName[n] = '_';
39133      pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
39134      if( pNew->pInode->pSem == SEM_FAILED ){
39135        rc = SQLITE_NOMEM_BKPT;
39136        pNew->pInode->aSemName[0] = '\0';
39137      }
39138    }
39139    unixLeaveMutex();
39140  }
39141#endif
39142
39143  storeLastErrno(pNew, 0);
39144#if OS_VXWORKS
39145  if( rc!=SQLITE_OK ){
39146    if( h>=0 ) robust_close(pNew, h, __LINE__);
39147    h = -1;
39148    osUnlink(zFilename);
39149    pNew->ctrlFlags |= UNIXFILE_DELETE;
39150  }
39151#endif
39152  if( rc!=SQLITE_OK ){
39153    if( h>=0 ) robust_close(pNew, h, __LINE__);
39154  }else{
39155    pNew->pMethod = pLockingStyle;
39156    OpenCounter(+1);
39157    verifyDbFile(pNew);
39158  }
39159  return rc;
39160}
39161
39162/*
39163** Return the name of a directory in which to put temporary files.
39164** If no suitable temporary file directory can be found, return NULL.
39165*/
39166static const char *unixTempFileDir(void){
39167  static const char *azDirs[] = {
39168     0,
39169     0,
39170     "/var/tmp",
39171     "/usr/tmp",
39172     "/tmp",
39173     "."
39174  };
39175  unsigned int i = 0;
39176  struct stat buf;
39177  const char *zDir = sqlite3_temp_directory;
39178
39179  if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
39180  if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
39181  while(1){
39182    if( zDir!=0
39183     && osStat(zDir, &buf)==0
39184     && S_ISDIR(buf.st_mode)
39185     && osAccess(zDir, 03)==0
39186    ){
39187      return zDir;
39188    }
39189    if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break;
39190    zDir = azDirs[i++];
39191  }
39192  return 0;
39193}
39194
39195/*
39196** Create a temporary file name in zBuf.  zBuf must be allocated
39197** by the calling process and must be big enough to hold at least
39198** pVfs->mxPathname bytes.
39199*/
39200static int unixGetTempname(int nBuf, char *zBuf){
39201  const char *zDir;
39202  int iLimit = 0;
39203
39204  /* It's odd to simulate an io-error here, but really this is just
39205  ** using the io-error infrastructure to test that SQLite handles this
39206  ** function failing.
39207  */
39208  zBuf[0] = 0;
39209  SimulateIOError( return SQLITE_IOERR );
39210
39211  zDir = unixTempFileDir();
39212  if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH;
39213  do{
39214    u64 r;
39215    sqlite3_randomness(sizeof(r), &r);
39216    assert( nBuf>2 );
39217    zBuf[nBuf-2] = 0;
39218    sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
39219                     zDir, r, 0);
39220    if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
39221  }while( osAccess(zBuf,0)==0 );
39222  return SQLITE_OK;
39223}
39224
39225#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
39226/*
39227** Routine to transform a unixFile into a proxy-locking unixFile.
39228** Implementation in the proxy-lock division, but used by unixOpen()
39229** if SQLITE_PREFER_PROXY_LOCKING is defined.
39230*/
39231static int proxyTransformUnixFile(unixFile*, const char*);
39232#endif
39233
39234/*
39235** Search for an unused file descriptor that was opened on the database
39236** file (not a journal or master-journal file) identified by pathname
39237** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
39238** argument to this function.
39239**
39240** Such a file descriptor may exist if a database connection was closed
39241** but the associated file descriptor could not be closed because some
39242** other file descriptor open on the same file is holding a file-lock.
39243** Refer to comments in the unixClose() function and the lengthy comment
39244** describing "Posix Advisory Locking" at the start of this file for
39245** further details. Also, ticket #4018.
39246**
39247** If a suitable file descriptor is found, then it is returned. If no
39248** such file descriptor is located, -1 is returned.
39249*/
39250static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
39251  UnixUnusedFd *pUnused = 0;
39252
39253  /* Do not search for an unused file descriptor on vxworks. Not because
39254  ** vxworks would not benefit from the change (it might, we're not sure),
39255  ** but because no way to test it is currently available. It is better
39256  ** not to risk breaking vxworks support for the sake of such an obscure
39257  ** feature.  */
39258#if !OS_VXWORKS
39259  struct stat sStat;                   /* Results of stat() call */
39260
39261  unixEnterMutex();
39262
39263  /* A stat() call may fail for various reasons. If this happens, it is
39264  ** almost certain that an open() call on the same path will also fail.
39265  ** For this reason, if an error occurs in the stat() call here, it is
39266  ** ignored and -1 is returned. The caller will try to open a new file
39267  ** descriptor on the same path, fail, and return an error to SQLite.
39268  **
39269  ** Even if a subsequent open() call does succeed, the consequences of
39270  ** not searching for a reusable file descriptor are not dire.  */
39271  if( inodeList!=0 && 0==osStat(zPath, &sStat) ){
39272    unixInodeInfo *pInode;
39273
39274    pInode = inodeList;
39275    while( pInode && (pInode->fileId.dev!=sStat.st_dev
39276                     || pInode->fileId.ino!=(u64)sStat.st_ino) ){
39277       pInode = pInode->pNext;
39278    }
39279    if( pInode ){
39280      UnixUnusedFd **pp;
39281      assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
39282      sqlite3_mutex_enter(pInode->pLockMutex);
39283      flags &= (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE);
39284      for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
39285      pUnused = *pp;
39286      if( pUnused ){
39287        *pp = pUnused->pNext;
39288      }
39289      sqlite3_mutex_leave(pInode->pLockMutex);
39290    }
39291  }
39292  unixLeaveMutex();
39293#endif    /* if !OS_VXWORKS */
39294  return pUnused;
39295}
39296
39297/*
39298** Find the mode, uid and gid of file zFile.
39299*/
39300static int getFileMode(
39301  const char *zFile,              /* File name */
39302  mode_t *pMode,                  /* OUT: Permissions of zFile */
39303  uid_t *pUid,                    /* OUT: uid of zFile. */
39304  gid_t *pGid                     /* OUT: gid of zFile. */
39305){
39306  struct stat sStat;              /* Output of stat() on database file */
39307  int rc = SQLITE_OK;
39308  if( 0==osStat(zFile, &sStat) ){
39309    *pMode = sStat.st_mode & 0777;
39310    *pUid = sStat.st_uid;
39311    *pGid = sStat.st_gid;
39312  }else{
39313    rc = SQLITE_IOERR_FSTAT;
39314  }
39315  return rc;
39316}
39317
39318/*
39319** This function is called by unixOpen() to determine the unix permissions
39320** to create new files with. If no error occurs, then SQLITE_OK is returned
39321** and a value suitable for passing as the third argument to open(2) is
39322** written to *pMode. If an IO error occurs, an SQLite error code is
39323** returned and the value of *pMode is not modified.
39324**
39325** In most cases, this routine sets *pMode to 0, which will become
39326** an indication to robust_open() to create the file using
39327** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
39328** But if the file being opened is a WAL or regular journal file, then
39329** this function queries the file-system for the permissions on the
39330** corresponding database file and sets *pMode to this value. Whenever
39331** possible, WAL and journal files are created using the same permissions
39332** as the associated database file.
39333**
39334** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
39335** original filename is unavailable.  But 8_3_NAMES is only used for
39336** FAT filesystems and permissions do not matter there, so just use
39337** the default permissions.  In 8_3_NAMES mode, leave *pMode set to zero.
39338*/
39339static int findCreateFileMode(
39340  const char *zPath,              /* Path of file (possibly) being created */
39341  int flags,                      /* Flags passed as 4th argument to xOpen() */
39342  mode_t *pMode,                  /* OUT: Permissions to open file with */
39343  uid_t *pUid,                    /* OUT: uid to set on the file */
39344  gid_t *pGid                     /* OUT: gid to set on the file */
39345){
39346  int rc = SQLITE_OK;             /* Return Code */
39347  *pMode = 0;
39348  *pUid = 0;
39349  *pGid = 0;
39350  if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
39351    char zDb[MAX_PATHNAME+1];     /* Database file path */
39352    int nDb;                      /* Number of valid bytes in zDb */
39353
39354    /* zPath is a path to a WAL or journal file. The following block derives
39355    ** the path to the associated database file from zPath. This block handles
39356    ** the following naming conventions:
39357    **
39358    **   "<path to db>-journal"
39359    **   "<path to db>-wal"
39360    **   "<path to db>-journalNN"
39361    **   "<path to db>-walNN"
39362    **
39363    ** where NN is a decimal number. The NN naming schemes are
39364    ** used by the test_multiplex.c module.
39365    */
39366    nDb = sqlite3Strlen30(zPath) - 1;
39367    while( zPath[nDb]!='-' ){
39368      /* In normal operation, the journal file name will always contain
39369      ** a '-' character.  However in 8+3 filename mode, or if a corrupt
39370      ** rollback journal specifies a master journal with a goofy name, then
39371      ** the '-' might be missing. */
39372      if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK;
39373      nDb--;
39374    }
39375    memcpy(zDb, zPath, nDb);
39376    zDb[nDb] = '\0';
39377
39378    rc = getFileMode(zDb, pMode, pUid, pGid);
39379  }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
39380    *pMode = 0600;
39381  }else if( flags & SQLITE_OPEN_URI ){
39382    /* If this is a main database file and the file was opened using a URI
39383    ** filename, check for the "modeof" parameter. If present, interpret
39384    ** its value as a filename and try to copy the mode, uid and gid from
39385    ** that file.  */
39386    const char *z = sqlite3_uri_parameter(zPath, "modeof");
39387    if( z ){
39388      rc = getFileMode(z, pMode, pUid, pGid);
39389    }
39390  }
39391  return rc;
39392}
39393
39394/*
39395** Open the file zPath.
39396**
39397** Previously, the SQLite OS layer used three functions in place of this
39398** one:
39399**
39400**     sqlite3OsOpenReadWrite();
39401**     sqlite3OsOpenReadOnly();
39402**     sqlite3OsOpenExclusive();
39403**
39404** These calls correspond to the following combinations of flags:
39405**
39406**     ReadWrite() ->     (READWRITE | CREATE)
39407**     ReadOnly()  ->     (READONLY)
39408**     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
39409**
39410** The old OpenExclusive() accepted a boolean argument - "delFlag". If
39411** true, the file was configured to be automatically deleted when the
39412** file handle closed. To achieve the same effect using this new
39413** interface, add the DELETEONCLOSE flag to those specified above for
39414** OpenExclusive().
39415*/
39416static int unixOpen(
39417  sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
39418  const char *zPath,           /* Pathname of file to be opened */
39419  sqlite3_file *pFile,         /* The file descriptor to be filled in */
39420  int flags,                   /* Input flags to control the opening */
39421  int *pOutFlags               /* Output flags returned to SQLite core */
39422){
39423  unixFile *p = (unixFile *)pFile;
39424  int fd = -1;                   /* File descriptor returned by open() */
39425  int openFlags = 0;             /* Flags to pass to open() */
39426  int eType = flags&0x0FFF00;  /* Type of file to open */
39427  int noLock;                    /* True to omit locking primitives */
39428  int rc = SQLITE_OK;            /* Function Return Code */
39429  int ctrlFlags = 0;             /* UNIXFILE_* flags */
39430
39431  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
39432  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
39433  int isCreate     = (flags & SQLITE_OPEN_CREATE);
39434  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
39435  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
39436#if SQLITE_ENABLE_LOCKING_STYLE
39437  int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
39438#endif
39439#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
39440  struct statfs fsInfo;
39441#endif
39442
39443  /* If creating a master or main-file journal, this function will open
39444  ** a file-descriptor on the directory too. The first time unixSync()
39445  ** is called the directory file descriptor will be fsync()ed and close()d.
39446  */
39447  int isNewJrnl = (isCreate && (
39448        eType==SQLITE_OPEN_MASTER_JOURNAL
39449     || eType==SQLITE_OPEN_MAIN_JOURNAL
39450     || eType==SQLITE_OPEN_WAL
39451  ));
39452
39453  /* If argument zPath is a NULL pointer, this function is required to open
39454  ** a temporary file. Use this buffer to store the file name in.
39455  */
39456  char zTmpname[MAX_PATHNAME+2];
39457  const char *zName = zPath;
39458
39459  /* Check the following statements are true:
39460  **
39461  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
39462  **   (b) if CREATE is set, then READWRITE must also be set, and
39463  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
39464  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
39465  */
39466  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
39467  assert(isCreate==0 || isReadWrite);
39468  assert(isExclusive==0 || isCreate);
39469  assert(isDelete==0 || isCreate);
39470
39471  /* The main DB, main journal, WAL file and master journal are never
39472  ** automatically deleted. Nor are they ever temporary files.  */
39473  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
39474  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
39475  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
39476  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
39477
39478  /* Assert that the upper layer has set one of the "file-type" flags. */
39479  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
39480       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
39481       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
39482       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
39483  );
39484
39485  /* Detect a pid change and reset the PRNG.  There is a race condition
39486  ** here such that two or more threads all trying to open databases at
39487  ** the same instant might all reset the PRNG.  But multiple resets
39488  ** are harmless.
39489  */
39490  if( randomnessPid!=osGetpid(0) ){
39491    randomnessPid = osGetpid(0);
39492    sqlite3_randomness(0,0);
39493  }
39494  memset(p, 0, sizeof(unixFile));
39495
39496  if( eType==SQLITE_OPEN_MAIN_DB ){
39497    UnixUnusedFd *pUnused;
39498    pUnused = findReusableFd(zName, flags);
39499    if( pUnused ){
39500      fd = pUnused->fd;
39501    }else{
39502      pUnused = sqlite3_malloc64(sizeof(*pUnused));
39503      if( !pUnused ){
39504        return SQLITE_NOMEM_BKPT;
39505      }
39506    }
39507    p->pPreallocatedUnused = pUnused;
39508
39509    /* Database filenames are double-zero terminated if they are not
39510    ** URIs with parameters.  Hence, they can always be passed into
39511    ** sqlite3_uri_parameter(). */
39512    assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
39513
39514  }else if( !zName ){
39515    /* If zName is NULL, the upper layer is requesting a temp file. */
39516    assert(isDelete && !isNewJrnl);
39517    rc = unixGetTempname(pVfs->mxPathname, zTmpname);
39518    if( rc!=SQLITE_OK ){
39519      return rc;
39520    }
39521    zName = zTmpname;
39522
39523    /* Generated temporary filenames are always double-zero terminated
39524    ** for use by sqlite3_uri_parameter(). */
39525    assert( zName[strlen(zName)+1]==0 );
39526  }
39527
39528  /* Determine the value of the flags parameter passed to POSIX function
39529  ** open(). These must be calculated even if open() is not called, as
39530  ** they may be stored as part of the file handle and used by the
39531  ** 'conch file' locking functions later on.  */
39532  if( isReadonly )  openFlags |= O_RDONLY;
39533  if( isReadWrite ) openFlags |= O_RDWR;
39534  if( isCreate )    openFlags |= O_CREAT;
39535  if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
39536  openFlags |= (O_LARGEFILE|O_BINARY|O_NOFOLLOW);
39537
39538  if( fd<0 ){
39539    mode_t openMode;              /* Permissions to create file with */
39540    uid_t uid;                    /* Userid for the file */
39541    gid_t gid;                    /* Groupid for the file */
39542    rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
39543    if( rc!=SQLITE_OK ){
39544      assert( !p->pPreallocatedUnused );
39545      assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
39546      return rc;
39547    }
39548    fd = robust_open(zName, openFlags, openMode);
39549    OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
39550    assert( !isExclusive || (openFlags & O_CREAT)!=0 );
39551    if( fd<0 ){
39552      if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){
39553        /* If unable to create a journal because the directory is not
39554        ** writable, change the error code to indicate that. */
39555        rc = SQLITE_READONLY_DIRECTORY;
39556      }else if( errno!=EISDIR && isReadWrite ){
39557        /* Failed to open the file for read/write access. Try read-only. */
39558        flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
39559        openFlags &= ~(O_RDWR|O_CREAT);
39560        flags |= SQLITE_OPEN_READONLY;
39561        openFlags |= O_RDONLY;
39562        isReadonly = 1;
39563        fd = robust_open(zName, openFlags, openMode);
39564      }
39565    }
39566    if( fd<0 ){
39567      int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
39568      if( rc==SQLITE_OK ) rc = rc2;
39569      goto open_finished;
39570    }
39571
39572    /* The owner of the rollback journal or WAL file should always be the
39573    ** same as the owner of the database file.  Try to ensure that this is
39574    ** the case.  The chown() system call will be a no-op if the current
39575    ** process lacks root privileges, be we should at least try.  Without
39576    ** this step, if a root process opens a database file, it can leave
39577    ** behinds a journal/WAL that is owned by root and hence make the
39578    ** database inaccessible to unprivileged processes.
39579    **
39580    ** If openMode==0, then that means uid and gid are not set correctly
39581    ** (probably because SQLite is configured to use 8+3 filename mode) and
39582    ** in that case we do not want to attempt the chown().
39583    */
39584    if( openMode && (flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL))!=0 ){
39585      robustFchown(fd, uid, gid);
39586    }
39587  }
39588  assert( fd>=0 );
39589  if( pOutFlags ){
39590    *pOutFlags = flags;
39591  }
39592
39593  if( p->pPreallocatedUnused ){
39594    p->pPreallocatedUnused->fd = fd;
39595    p->pPreallocatedUnused->flags =
39596                          flags & (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE);
39597  }
39598
39599  if( isDelete ){
39600#if OS_VXWORKS
39601    zPath = zName;
39602#elif defined(SQLITE_UNLINK_AFTER_CLOSE)
39603    zPath = sqlite3_mprintf("%s", zName);
39604    if( zPath==0 ){
39605      robust_close(p, fd, __LINE__);
39606      return SQLITE_NOMEM_BKPT;
39607    }
39608#else
39609    osUnlink(zName);
39610#endif
39611  }
39612#if SQLITE_ENABLE_LOCKING_STYLE
39613  else{
39614    p->openFlags = openFlags;
39615  }
39616#endif
39617
39618#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
39619  if( fstatfs(fd, &fsInfo) == -1 ){
39620    storeLastErrno(p, errno);
39621    robust_close(p, fd, __LINE__);
39622    return SQLITE_IOERR_ACCESS;
39623  }
39624  if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
39625    ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
39626  }
39627  if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
39628    ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
39629  }
39630#endif
39631
39632  /* Set up appropriate ctrlFlags */
39633  if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
39634  if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
39635  noLock = eType!=SQLITE_OPEN_MAIN_DB;
39636  if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
39637  if( isNewJrnl )               ctrlFlags |= UNIXFILE_DIRSYNC;
39638  if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
39639
39640#if SQLITE_ENABLE_LOCKING_STYLE
39641#if SQLITE_PREFER_PROXY_LOCKING
39642  isAutoProxy = 1;
39643#endif
39644  if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
39645    char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
39646    int useProxy = 0;
39647
39648    /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
39649    ** never use proxy, NULL means use proxy for non-local files only.  */
39650    if( envforce!=NULL ){
39651      useProxy = atoi(envforce)>0;
39652    }else{
39653      useProxy = !(fsInfo.f_flags&MNT_LOCAL);
39654    }
39655    if( useProxy ){
39656      rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
39657      if( rc==SQLITE_OK ){
39658        rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
39659        if( rc!=SQLITE_OK ){
39660          /* Use unixClose to clean up the resources added in fillInUnixFile
39661          ** and clear all the structure's references.  Specifically,
39662          ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
39663          */
39664          unixClose(pFile);
39665          return rc;
39666        }
39667      }
39668      goto open_finished;
39669    }
39670  }
39671#endif
39672
39673  assert( zPath==0 || zPath[0]=='/'
39674      || eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL
39675  );
39676  rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
39677
39678open_finished:
39679  if( rc!=SQLITE_OK ){
39680    sqlite3_free(p->pPreallocatedUnused);
39681  }
39682  return rc;
39683}
39684
39685
39686/*
39687** Delete the file at zPath. If the dirSync argument is true, fsync()
39688** the directory after deleting the file.
39689*/
39690static int unixDelete(
39691  sqlite3_vfs *NotUsed,     /* VFS containing this as the xDelete method */
39692  const char *zPath,        /* Name of file to be deleted */
39693  int dirSync               /* If true, fsync() directory after deleting file */
39694){
39695  int rc = SQLITE_OK;
39696  UNUSED_PARAMETER(NotUsed);
39697  SimulateIOError(return SQLITE_IOERR_DELETE);
39698  if( osUnlink(zPath)==(-1) ){
39699    if( errno==ENOENT
39700#if OS_VXWORKS
39701        || osAccess(zPath,0)!=0
39702#endif
39703    ){
39704      rc = SQLITE_IOERR_DELETE_NOENT;
39705    }else{
39706      rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
39707    }
39708    return rc;
39709  }
39710#ifndef SQLITE_DISABLE_DIRSYNC
39711  if( (dirSync & 1)!=0 ){
39712    int fd;
39713    rc = osOpenDirectory(zPath, &fd);
39714    if( rc==SQLITE_OK ){
39715      if( full_fsync(fd,0,0) ){
39716        rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
39717      }
39718      robust_close(0, fd, __LINE__);
39719    }else{
39720      assert( rc==SQLITE_CANTOPEN );
39721      rc = SQLITE_OK;
39722    }
39723  }
39724#endif
39725  return rc;
39726}
39727
39728/*
39729** Test the existence of or access permissions of file zPath. The
39730** test performed depends on the value of flags:
39731**
39732**     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
39733**     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
39734**     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
39735**
39736** Otherwise return 0.
39737*/
39738static int unixAccess(
39739  sqlite3_vfs *NotUsed,   /* The VFS containing this xAccess method */
39740  const char *zPath,      /* Path of the file to examine */
39741  int flags,              /* What do we want to learn about the zPath file? */
39742  int *pResOut            /* Write result boolean here */
39743){
39744  UNUSED_PARAMETER(NotUsed);
39745  SimulateIOError( return SQLITE_IOERR_ACCESS; );
39746  assert( pResOut!=0 );
39747
39748  /* The spec says there are three possible values for flags.  But only
39749  ** two of them are actually used */
39750  assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE );
39751
39752  if( flags==SQLITE_ACCESS_EXISTS ){
39753    struct stat buf;
39754    *pResOut = 0==osStat(zPath, &buf) &&
39755                (!S_ISREG(buf.st_mode) || buf.st_size>0);
39756  }else{
39757    *pResOut = osAccess(zPath, W_OK|R_OK)==0;
39758  }
39759  return SQLITE_OK;
39760}
39761
39762/*
39763**
39764*/
39765static int mkFullPathname(
39766  const char *zPath,              /* Input path */
39767  char *zOut,                     /* Output buffer */
39768  int nOut                        /* Allocated size of buffer zOut */
39769){
39770  int nPath = sqlite3Strlen30(zPath);
39771  int iOff = 0;
39772  if( zPath[0]!='/' ){
39773    if( osGetcwd(zOut, nOut-2)==0 ){
39774      return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
39775    }
39776    iOff = sqlite3Strlen30(zOut);
39777    zOut[iOff++] = '/';
39778  }
39779  if( (iOff+nPath+1)>nOut ){
39780    /* SQLite assumes that xFullPathname() nul-terminates the output buffer
39781    ** even if it returns an error.  */
39782    zOut[iOff] = '\0';
39783    return SQLITE_CANTOPEN_BKPT;
39784  }
39785  sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
39786  return SQLITE_OK;
39787}
39788
39789/*
39790** Turn a relative pathname into a full pathname. The relative path
39791** is stored as a nul-terminated string in the buffer pointed to by
39792** zPath.
39793**
39794** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
39795** (in this case, MAX_PATHNAME bytes). The full-path is written to
39796** this buffer before returning.
39797*/
39798static int unixFullPathname(
39799  sqlite3_vfs *pVfs,            /* Pointer to vfs object */
39800  const char *zPath,            /* Possibly relative input path */
39801  int nOut,                     /* Size of output buffer in bytes */
39802  char *zOut                    /* Output buffer */
39803){
39804#if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT)
39805  return mkFullPathname(zPath, zOut, nOut);
39806#else
39807  int rc = SQLITE_OK;
39808  int nByte;
39809  int nLink = 0;                /* Number of symbolic links followed so far */
39810  const char *zIn = zPath;      /* Input path for each iteration of loop */
39811  char *zDel = 0;
39812
39813  assert( pVfs->mxPathname==MAX_PATHNAME );
39814  UNUSED_PARAMETER(pVfs);
39815
39816  /* It's odd to simulate an io-error here, but really this is just
39817  ** using the io-error infrastructure to test that SQLite handles this
39818  ** function failing. This function could fail if, for example, the
39819  ** current working directory has been unlinked.
39820  */
39821  SimulateIOError( return SQLITE_ERROR );
39822
39823  do {
39824
39825    /* Call stat() on path zIn. Set bLink to true if the path is a symbolic
39826    ** link, or false otherwise.  */
39827    int bLink = 0;
39828    struct stat buf;
39829    if( osLstat(zIn, &buf)!=0 ){
39830      if( errno!=ENOENT ){
39831        rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
39832      }
39833    }else{
39834      bLink = S_ISLNK(buf.st_mode);
39835    }
39836
39837    if( bLink ){
39838      nLink++;
39839      if( zDel==0 ){
39840        zDel = sqlite3_malloc(nOut);
39841        if( zDel==0 ) rc = SQLITE_NOMEM_BKPT;
39842      }else if( nLink>=SQLITE_MAX_SYMLINKS ){
39843        rc = SQLITE_CANTOPEN_BKPT;
39844      }
39845
39846      if( rc==SQLITE_OK ){
39847        nByte = osReadlink(zIn, zDel, nOut-1);
39848        if( nByte<0 ){
39849          rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
39850        }else{
39851          if( zDel[0]!='/' ){
39852            int n;
39853            for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
39854            if( nByte+n+1>nOut ){
39855              rc = SQLITE_CANTOPEN_BKPT;
39856            }else{
39857              memmove(&zDel[n], zDel, nByte+1);
39858              memcpy(zDel, zIn, n);
39859              nByte += n;
39860            }
39861          }
39862          zDel[nByte] = '\0';
39863        }
39864      }
39865
39866      zIn = zDel;
39867    }
39868
39869    assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' );
39870    if( rc==SQLITE_OK && zIn!=zOut ){
39871      rc = mkFullPathname(zIn, zOut, nOut);
39872    }
39873    if( bLink==0 ) break;
39874    zIn = zOut;
39875  }while( rc==SQLITE_OK );
39876
39877  sqlite3_free(zDel);
39878  if( rc==SQLITE_OK && nLink ) rc = SQLITE_OK_SYMLINK;
39879  return rc;
39880#endif   /* HAVE_READLINK && HAVE_LSTAT */
39881}
39882
39883
39884#ifndef SQLITE_OMIT_LOAD_EXTENSION
39885/*
39886** Interfaces for opening a shared library, finding entry points
39887** within the shared library, and closing the shared library.
39888*/
39889#include <dlfcn.h>
39890static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
39891  UNUSED_PARAMETER(NotUsed);
39892  return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
39893}
39894
39895/*
39896** SQLite calls this function immediately after a call to unixDlSym() or
39897** unixDlOpen() fails (returns a null pointer). If a more detailed error
39898** message is available, it is written to zBufOut. If no error message
39899** is available, zBufOut is left unmodified and SQLite uses a default
39900** error message.
39901*/
39902static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
39903  const char *zErr;
39904  UNUSED_PARAMETER(NotUsed);
39905  unixEnterMutex();
39906  zErr = dlerror();
39907  if( zErr ){
39908    sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
39909  }
39910  unixLeaveMutex();
39911}
39912static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
39913  /*
39914  ** GCC with -pedantic-errors says that C90 does not allow a void* to be
39915  ** cast into a pointer to a function.  And yet the library dlsym() routine
39916  ** returns a void* which is really a pointer to a function.  So how do we
39917  ** use dlsym() with -pedantic-errors?
39918  **
39919  ** Variable x below is defined to be a pointer to a function taking
39920  ** parameters void* and const char* and returning a pointer to a function.
39921  ** We initialize x by assigning it a pointer to the dlsym() function.
39922  ** (That assignment requires a cast.)  Then we call the function that
39923  ** x points to.
39924  **
39925  ** This work-around is unlikely to work correctly on any system where
39926  ** you really cannot cast a function pointer into void*.  But then, on the
39927  ** other hand, dlsym() will not work on such a system either, so we have
39928  ** not really lost anything.
39929  */
39930  void (*(*x)(void*,const char*))(void);
39931  UNUSED_PARAMETER(NotUsed);
39932  x = (void(*(*)(void*,const char*))(void))dlsym;
39933  return (*x)(p, zSym);
39934}
39935static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
39936  UNUSED_PARAMETER(NotUsed);
39937  dlclose(pHandle);
39938}
39939#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
39940  #define unixDlOpen  0
39941  #define unixDlError 0
39942  #define unixDlSym   0
39943  #define unixDlClose 0
39944#endif
39945
39946/*
39947** Write nBuf bytes of random data to the supplied buffer zBuf.
39948*/
39949static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
39950  UNUSED_PARAMETER(NotUsed);
39951  assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
39952
39953  /* We have to initialize zBuf to prevent valgrind from reporting
39954  ** errors.  The reports issued by valgrind are incorrect - we would
39955  ** prefer that the randomness be increased by making use of the
39956  ** uninitialized space in zBuf - but valgrind errors tend to worry
39957  ** some users.  Rather than argue, it seems easier just to initialize
39958  ** the whole array and silence valgrind, even if that means less randomness
39959  ** in the random seed.
39960  **
39961  ** When testing, initializing zBuf[] to zero is all we do.  That means
39962  ** that we always use the same random number sequence.  This makes the
39963  ** tests repeatable.
39964  */
39965  memset(zBuf, 0, nBuf);
39966  randomnessPid = osGetpid(0);
39967#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
39968  {
39969    int fd, got;
39970    fd = robust_open("/dev/urandom", O_RDONLY, 0);
39971    if( fd<0 ){
39972      time_t t;
39973      time(&t);
39974      memcpy(zBuf, &t, sizeof(t));
39975      memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
39976      assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
39977      nBuf = sizeof(t) + sizeof(randomnessPid);
39978    }else{
39979      do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
39980      robust_close(0, fd, __LINE__);
39981    }
39982  }
39983#endif
39984  return nBuf;
39985}
39986
39987
39988/*
39989** Sleep for a little while.  Return the amount of time slept.
39990** The argument is the number of microseconds we want to sleep.
39991** The return value is the number of microseconds of sleep actually
39992** requested from the underlying operating system, a number which
39993** might be greater than or equal to the argument, but not less
39994** than the argument.
39995*/
39996static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
39997#if OS_VXWORKS
39998  struct timespec sp;
39999
40000  sp.tv_sec = microseconds / 1000000;
40001  sp.tv_nsec = (microseconds % 1000000) * 1000;
40002  nanosleep(&sp, NULL);
40003  UNUSED_PARAMETER(NotUsed);
40004  return microseconds;
40005#elif defined(HAVE_USLEEP) && HAVE_USLEEP
40006  usleep(microseconds);
40007  UNUSED_PARAMETER(NotUsed);
40008  return microseconds;
40009#else
40010  int seconds = (microseconds+999999)/1000000;
40011  sleep(seconds);
40012  UNUSED_PARAMETER(NotUsed);
40013  return seconds*1000000;
40014#endif
40015}
40016
40017/*
40018** The following variable, if set to a non-zero value, is interpreted as
40019** the number of seconds since 1970 and is used to set the result of
40020** sqlite3OsCurrentTime() during testing.
40021*/
40022#ifdef SQLITE_TEST
40023SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
40024#endif
40025
40026/*
40027** Find the current time (in Universal Coordinated Time).  Write into *piNow
40028** the current time and date as a Julian Day number times 86_400_000.  In
40029** other words, write into *piNow the number of milliseconds since the Julian
40030** epoch of noon in Greenwich on November 24, 4714 B.C according to the
40031** proleptic Gregorian calendar.
40032**
40033** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date
40034** cannot be found.
40035*/
40036static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
40037  static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
40038  int rc = SQLITE_OK;
40039#if defined(NO_GETTOD)
40040  time_t t;
40041  time(&t);
40042  *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
40043#elif OS_VXWORKS
40044  struct timespec sNow;
40045  clock_gettime(CLOCK_REALTIME, &sNow);
40046  *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
40047#else
40048  struct timeval sNow;
40049  (void)gettimeofday(&sNow, 0);  /* Cannot fail given valid arguments */
40050  *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
40051#endif
40052
40053#ifdef SQLITE_TEST
40054  if( sqlite3_current_time ){
40055    *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
40056  }
40057#endif
40058  UNUSED_PARAMETER(NotUsed);
40059  return rc;
40060}
40061
40062#ifndef SQLITE_OMIT_DEPRECATED
40063/*
40064** Find the current time (in Universal Coordinated Time).  Write the
40065** current time and date as a Julian Day number into *prNow and
40066** return 0.  Return 1 if the time and date cannot be found.
40067*/
40068static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
40069  sqlite3_int64 i = 0;
40070  int rc;
40071  UNUSED_PARAMETER(NotUsed);
40072  rc = unixCurrentTimeInt64(0, &i);
40073  *prNow = i/86400000.0;
40074  return rc;
40075}
40076#else
40077# define unixCurrentTime 0
40078#endif
40079
40080/*
40081** The xGetLastError() method is designed to return a better
40082** low-level error message when operating-system problems come up
40083** during SQLite operation.  Only the integer return code is currently
40084** used.
40085*/
40086static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
40087  UNUSED_PARAMETER(NotUsed);
40088  UNUSED_PARAMETER(NotUsed2);
40089  UNUSED_PARAMETER(NotUsed3);
40090  return errno;
40091}
40092
40093
40094/*
40095************************ End of sqlite3_vfs methods ***************************
40096******************************************************************************/
40097
40098/******************************************************************************
40099************************** Begin Proxy Locking ********************************
40100**
40101** Proxy locking is a "uber-locking-method" in this sense:  It uses the
40102** other locking methods on secondary lock files.  Proxy locking is a
40103** meta-layer over top of the primitive locking implemented above.  For
40104** this reason, the division that implements of proxy locking is deferred
40105** until late in the file (here) after all of the other I/O methods have
40106** been defined - so that the primitive locking methods are available
40107** as services to help with the implementation of proxy locking.
40108**
40109****
40110**
40111** The default locking schemes in SQLite use byte-range locks on the
40112** database file to coordinate safe, concurrent access by multiple readers
40113** and writers [http://sqlite.org/lockingv3.html].  The five file locking
40114** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
40115** as POSIX read & write locks over fixed set of locations (via fsctl),
40116** on AFP and SMB only exclusive byte-range locks are available via fsctl
40117** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
40118** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
40119** address in the shared range is taken for a SHARED lock, the entire
40120** shared range is taken for an EXCLUSIVE lock):
40121**
40122**      PENDING_BYTE        0x40000000
40123**      RESERVED_BYTE       0x40000001
40124**      SHARED_RANGE        0x40000002 -> 0x40000200
40125**
40126** This works well on the local file system, but shows a nearly 100x
40127** slowdown in read performance on AFP because the AFP client disables
40128** the read cache when byte-range locks are present.  Enabling the read
40129** cache exposes a cache coherency problem that is present on all OS X
40130** supported network file systems.  NFS and AFP both observe the
40131** close-to-open semantics for ensuring cache coherency
40132** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
40133** address the requirements for concurrent database access by multiple
40134** readers and writers
40135** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
40136**
40137** To address the performance and cache coherency issues, proxy file locking
40138** changes the way database access is controlled by limiting access to a
40139** single host at a time and moving file locks off of the database file
40140** and onto a proxy file on the local file system.
40141**
40142**
40143** Using proxy locks
40144** -----------------
40145**
40146** C APIs
40147**
40148**  sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE,
40149**                       <proxy_path> | ":auto:");
40150**  sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE,
40151**                       &<proxy_path>);
40152**
40153**
40154** SQL pragmas
40155**
40156**  PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
40157**  PRAGMA [database.]lock_proxy_file
40158**
40159** Specifying ":auto:" means that if there is a conch file with a matching
40160** host ID in it, the proxy path in the conch file will be used, otherwise
40161** a proxy path based on the user's temp dir
40162** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
40163** actual proxy file name is generated from the name and path of the
40164** database file.  For example:
40165**
40166**       For database path "/Users/me/foo.db"
40167**       The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
40168**
40169** Once a lock proxy is configured for a database connection, it can not
40170** be removed, however it may be switched to a different proxy path via
40171** the above APIs (assuming the conch file is not being held by another
40172** connection or process).
40173**
40174**
40175** How proxy locking works
40176** -----------------------
40177**
40178** Proxy file locking relies primarily on two new supporting files:
40179**
40180**   *  conch file to limit access to the database file to a single host
40181**      at a time
40182**
40183**   *  proxy file to act as a proxy for the advisory locks normally
40184**      taken on the database
40185**
40186** The conch file - to use a proxy file, sqlite must first "hold the conch"
40187** by taking an sqlite-style shared lock on the conch file, reading the
40188** contents and comparing the host's unique host ID (see below) and lock
40189** proxy path against the values stored in the conch.  The conch file is
40190** stored in the same directory as the database file and the file name
40191** is patterned after the database file name as ".<databasename>-conch".
40192** If the conch file does not exist, or its contents do not match the
40193** host ID and/or proxy path, then the lock is escalated to an exclusive
40194** lock and the conch file contents is updated with the host ID and proxy
40195** path and the lock is downgraded to a shared lock again.  If the conch
40196** is held by another process (with a shared lock), the exclusive lock
40197** will fail and SQLITE_BUSY is returned.
40198**
40199** The proxy file - a single-byte file used for all advisory file locks
40200** normally taken on the database file.   This allows for safe sharing
40201** of the database file for multiple readers and writers on the same
40202** host (the conch ensures that they all use the same local lock file).
40203**
40204** Requesting the lock proxy does not immediately take the conch, it is
40205** only taken when the first request to lock database file is made.
40206** This matches the semantics of the traditional locking behavior, where
40207** opening a connection to a database file does not take a lock on it.
40208** The shared lock and an open file descriptor are maintained until
40209** the connection to the database is closed.
40210**
40211** The proxy file and the lock file are never deleted so they only need
40212** to be created the first time they are used.
40213**
40214** Configuration options
40215** ---------------------
40216**
40217**  SQLITE_PREFER_PROXY_LOCKING
40218**
40219**       Database files accessed on non-local file systems are
40220**       automatically configured for proxy locking, lock files are
40221**       named automatically using the same logic as
40222**       PRAGMA lock_proxy_file=":auto:"
40223**
40224**  SQLITE_PROXY_DEBUG
40225**
40226**       Enables the logging of error messages during host id file
40227**       retrieval and creation
40228**
40229**  LOCKPROXYDIR
40230**
40231**       Overrides the default directory used for lock proxy files that
40232**       are named automatically via the ":auto:" setting
40233**
40234**  SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
40235**
40236**       Permissions to use when creating a directory for storing the
40237**       lock proxy files, only used when LOCKPROXYDIR is not set.
40238**
40239**
40240** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
40241** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
40242** force proxy locking to be used for every database file opened, and 0
40243** will force automatic proxy locking to be disabled for all database
40244** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or
40245** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
40246*/
40247
40248/*
40249** Proxy locking is only available on MacOSX
40250*/
40251#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
40252
40253/*
40254** The proxyLockingContext has the path and file structures for the remote
40255** and local proxy files in it
40256*/
40257typedef struct proxyLockingContext proxyLockingContext;
40258struct proxyLockingContext {
40259  unixFile *conchFile;         /* Open conch file */
40260  char *conchFilePath;         /* Name of the conch file */
40261  unixFile *lockProxy;         /* Open proxy lock file */
40262  char *lockProxyPath;         /* Name of the proxy lock file */
40263  char *dbPath;                /* Name of the open file */
40264  int conchHeld;               /* 1 if the conch is held, -1 if lockless */
40265  int nFails;                  /* Number of conch taking failures */
40266  void *oldLockingContext;     /* Original lockingcontext to restore on close */
40267  sqlite3_io_methods const *pOldMethod;     /* Original I/O methods for close */
40268};
40269
40270/*
40271** The proxy lock file path for the database at dbPath is written into lPath,
40272** which must point to valid, writable memory large enough for a maxLen length
40273** file path.
40274*/
40275static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
40276  int len;
40277  int dbLen;
40278  int i;
40279
40280#ifdef LOCKPROXYDIR
40281  len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
40282#else
40283# ifdef _CS_DARWIN_USER_TEMP_DIR
40284  {
40285    if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
40286      OSTRACE(("GETLOCKPATH  failed %s errno=%d pid=%d\n",
40287               lPath, errno, osGetpid(0)));
40288      return SQLITE_IOERR_LOCK;
40289    }
40290    len = strlcat(lPath, "sqliteplocks", maxLen);
40291  }
40292# else
40293  len = strlcpy(lPath, "/tmp/", maxLen);
40294# endif
40295#endif
40296
40297  if( lPath[len-1]!='/' ){
40298    len = strlcat(lPath, "/", maxLen);
40299  }
40300
40301  /* transform the db path to a unique cache name */
40302  dbLen = (int)strlen(dbPath);
40303  for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
40304    char c = dbPath[i];
40305    lPath[i+len] = (c=='/')?'_':c;
40306  }
40307  lPath[i+len]='\0';
40308  strlcat(lPath, ":auto:", maxLen);
40309  OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
40310  return SQLITE_OK;
40311}
40312
40313/*
40314 ** Creates the lock file and any missing directories in lockPath
40315 */
40316static int proxyCreateLockPath(const char *lockPath){
40317  int i, len;
40318  char buf[MAXPATHLEN];
40319  int start = 0;
40320
40321  assert(lockPath!=NULL);
40322  /* try to create all the intermediate directories */
40323  len = (int)strlen(lockPath);
40324  buf[0] = lockPath[0];
40325  for( i=1; i<len; i++ ){
40326    if( lockPath[i] == '/' && (i - start > 0) ){
40327      /* only mkdir if leaf dir != "." or "/" or ".." */
40328      if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
40329         || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
40330        buf[i]='\0';
40331        if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
40332          int err=errno;
40333          if( err!=EEXIST ) {
40334            OSTRACE(("CREATELOCKPATH  FAILED creating %s, "
40335                     "'%s' proxy lock path=%s pid=%d\n",
40336                     buf, strerror(err), lockPath, osGetpid(0)));
40337            return err;
40338          }
40339        }
40340      }
40341      start=i+1;
40342    }
40343    buf[i] = lockPath[i];
40344  }
40345  OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n",lockPath,osGetpid(0)));
40346  return 0;
40347}
40348
40349/*
40350** Create a new VFS file descriptor (stored in memory obtained from
40351** sqlite3_malloc) and open the file named "path" in the file descriptor.
40352**
40353** The caller is responsible not only for closing the file descriptor
40354** but also for freeing the memory associated with the file descriptor.
40355*/
40356static int proxyCreateUnixFile(
40357    const char *path,        /* path for the new unixFile */
40358    unixFile **ppFile,       /* unixFile created and returned by ref */
40359    int islockfile           /* if non zero missing dirs will be created */
40360) {
40361  int fd = -1;
40362  unixFile *pNew;
40363  int rc = SQLITE_OK;
40364  int openFlags = O_RDWR | O_CREAT | O_NOFOLLOW;
40365  sqlite3_vfs dummyVfs;
40366  int terrno = 0;
40367  UnixUnusedFd *pUnused = NULL;
40368
40369  /* 1. first try to open/create the file
40370  ** 2. if that fails, and this is a lock file (not-conch), try creating
40371  ** the parent directories and then try again.
40372  ** 3. if that fails, try to open the file read-only
40373  ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
40374  */
40375  pUnused = findReusableFd(path, openFlags);
40376  if( pUnused ){
40377    fd = pUnused->fd;
40378  }else{
40379    pUnused = sqlite3_malloc64(sizeof(*pUnused));
40380    if( !pUnused ){
40381      return SQLITE_NOMEM_BKPT;
40382    }
40383  }
40384  if( fd<0 ){
40385    fd = robust_open(path, openFlags, 0);
40386    terrno = errno;
40387    if( fd<0 && errno==ENOENT && islockfile ){
40388      if( proxyCreateLockPath(path) == SQLITE_OK ){
40389        fd = robust_open(path, openFlags, 0);
40390      }
40391    }
40392  }
40393  if( fd<0 ){
40394    openFlags = O_RDONLY | O_NOFOLLOW;
40395    fd = robust_open(path, openFlags, 0);
40396    terrno = errno;
40397  }
40398  if( fd<0 ){
40399    if( islockfile ){
40400      return SQLITE_BUSY;
40401    }
40402    switch (terrno) {
40403      case EACCES:
40404        return SQLITE_PERM;
40405      case EIO:
40406        return SQLITE_IOERR_LOCK; /* even though it is the conch */
40407      default:
40408        return SQLITE_CANTOPEN_BKPT;
40409    }
40410  }
40411
40412  pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
40413  if( pNew==NULL ){
40414    rc = SQLITE_NOMEM_BKPT;
40415    goto end_create_proxy;
40416  }
40417  memset(pNew, 0, sizeof(unixFile));
40418  pNew->openFlags = openFlags;
40419  memset(&dummyVfs, 0, sizeof(dummyVfs));
40420  dummyVfs.pAppData = (void*)&autolockIoFinder;
40421  dummyVfs.zName = "dummy";
40422  pUnused->fd = fd;
40423  pUnused->flags = openFlags;
40424  pNew->pPreallocatedUnused = pUnused;
40425
40426  rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
40427  if( rc==SQLITE_OK ){
40428    *ppFile = pNew;
40429    return SQLITE_OK;
40430  }
40431end_create_proxy:
40432  robust_close(pNew, fd, __LINE__);
40433  sqlite3_free(pNew);
40434  sqlite3_free(pUnused);
40435  return rc;
40436}
40437
40438#ifdef SQLITE_TEST
40439/* simulate multiple hosts by creating unique hostid file paths */
40440SQLITE_API int sqlite3_hostid_num = 0;
40441#endif
40442
40443#define PROXY_HOSTIDLEN    16  /* conch file host id length */
40444
40445#if HAVE_GETHOSTUUID
40446/* Not always defined in the headers as it ought to be */
40447extern int gethostuuid(uuid_t id, const struct timespec *wait);
40448#endif
40449
40450/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
40451** bytes of writable memory.
40452*/
40453static int proxyGetHostID(unsigned char *pHostID, int *pError){
40454  assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
40455  memset(pHostID, 0, PROXY_HOSTIDLEN);
40456#if HAVE_GETHOSTUUID
40457  {
40458    struct timespec timeout = {1, 0}; /* 1 sec timeout */
40459    if( gethostuuid(pHostID, &timeout) ){
40460      int err = errno;
40461      if( pError ){
40462        *pError = err;
40463      }
40464      return SQLITE_IOERR;
40465    }
40466  }
40467#else
40468  UNUSED_PARAMETER(pError);
40469#endif
40470#ifdef SQLITE_TEST
40471  /* simulate multiple hosts by creating unique hostid file paths */
40472  if( sqlite3_hostid_num != 0){
40473    pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
40474  }
40475#endif
40476
40477  return SQLITE_OK;
40478}
40479
40480/* The conch file contains the header, host id and lock file path
40481 */
40482#define PROXY_CONCHVERSION 2   /* 1-byte header, 16-byte host id, path */
40483#define PROXY_HEADERLEN    1   /* conch file header length */
40484#define PROXY_PATHINDEX    (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
40485#define PROXY_MAXCONCHLEN  (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
40486
40487/*
40488** Takes an open conch file, copies the contents to a new path and then moves
40489** it back.  The newly created file's file descriptor is assigned to the
40490** conch file structure and finally the original conch file descriptor is
40491** closed.  Returns zero if successful.
40492*/
40493static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
40494  proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
40495  unixFile *conchFile = pCtx->conchFile;
40496  char tPath[MAXPATHLEN];
40497  char buf[PROXY_MAXCONCHLEN];
40498  char *cPath = pCtx->conchFilePath;
40499  size_t readLen = 0;
40500  size_t pathLen = 0;
40501  char errmsg[64] = "";
40502  int fd = -1;
40503  int rc = -1;
40504  UNUSED_PARAMETER(myHostID);
40505
40506  /* create a new path by replace the trailing '-conch' with '-break' */
40507  pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
40508  if( pathLen>MAXPATHLEN || pathLen<6 ||
40509     (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
40510    sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
40511    goto end_breaklock;
40512  }
40513  /* read the conch content */
40514  readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
40515  if( readLen<PROXY_PATHINDEX ){
40516    sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
40517    goto end_breaklock;
40518  }
40519  /* write it out to the temporary break file */
40520  fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW), 0);
40521  if( fd<0 ){
40522    sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
40523    goto end_breaklock;
40524  }
40525  if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
40526    sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
40527    goto end_breaklock;
40528  }
40529  if( rename(tPath, cPath) ){
40530    sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
40531    goto end_breaklock;
40532  }
40533  rc = 0;
40534  fprintf(stderr, "broke stale lock on %s\n", cPath);
40535  robust_close(pFile, conchFile->h, __LINE__);
40536  conchFile->h = fd;
40537  conchFile->openFlags = O_RDWR | O_CREAT;
40538
40539end_breaklock:
40540  if( rc ){
40541    if( fd>=0 ){
40542      osUnlink(tPath);
40543      robust_close(pFile, fd, __LINE__);
40544    }
40545    fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
40546  }
40547  return rc;
40548}
40549
40550/* Take the requested lock on the conch file and break a stale lock if the
40551** host id matches.
40552*/
40553static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
40554  proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
40555  unixFile *conchFile = pCtx->conchFile;
40556  int rc = SQLITE_OK;
40557  int nTries = 0;
40558  struct timespec conchModTime;
40559
40560  memset(&conchModTime, 0, sizeof(conchModTime));
40561  do {
40562    rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
40563    nTries ++;
40564    if( rc==SQLITE_BUSY ){
40565      /* If the lock failed (busy):
40566       * 1st try: get the mod time of the conch, wait 0.5s and try again.
40567       * 2nd try: fail if the mod time changed or host id is different, wait
40568       *           10 sec and try again
40569       * 3rd try: break the lock unless the mod time has changed.
40570       */
40571      struct stat buf;
40572      if( osFstat(conchFile->h, &buf) ){
40573        storeLastErrno(pFile, errno);
40574        return SQLITE_IOERR_LOCK;
40575      }
40576
40577      if( nTries==1 ){
40578        conchModTime = buf.st_mtimespec;
40579        usleep(500000); /* wait 0.5 sec and try the lock again*/
40580        continue;
40581      }
40582
40583      assert( nTries>1 );
40584      if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
40585         conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
40586        return SQLITE_BUSY;
40587      }
40588
40589      if( nTries==2 ){
40590        char tBuf[PROXY_MAXCONCHLEN];
40591        int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
40592        if( len<0 ){
40593          storeLastErrno(pFile, errno);
40594          return SQLITE_IOERR_LOCK;
40595        }
40596        if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
40597          /* don't break the lock if the host id doesn't match */
40598          if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
40599            return SQLITE_BUSY;
40600          }
40601        }else{
40602          /* don't break the lock on short read or a version mismatch */
40603          return SQLITE_BUSY;
40604        }
40605        usleep(10000000); /* wait 10 sec and try the lock again */
40606        continue;
40607      }
40608
40609      assert( nTries==3 );
40610      if( 0==proxyBreakConchLock(pFile, myHostID) ){
40611        rc = SQLITE_OK;
40612        if( lockType==EXCLUSIVE_LOCK ){
40613          rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
40614        }
40615        if( !rc ){
40616          rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
40617        }
40618      }
40619    }
40620  } while( rc==SQLITE_BUSY && nTries<3 );
40621
40622  return rc;
40623}
40624
40625/* Takes the conch by taking a shared lock and read the contents conch, if
40626** lockPath is non-NULL, the host ID and lock file path must match.  A NULL
40627** lockPath means that the lockPath in the conch file will be used if the
40628** host IDs match, or a new lock path will be generated automatically
40629** and written to the conch file.
40630*/
40631static int proxyTakeConch(unixFile *pFile){
40632  proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
40633
40634  if( pCtx->conchHeld!=0 ){
40635    return SQLITE_OK;
40636  }else{
40637    unixFile *conchFile = pCtx->conchFile;
40638    uuid_t myHostID;
40639    int pError = 0;
40640    char readBuf[PROXY_MAXCONCHLEN];
40641    char lockPath[MAXPATHLEN];
40642    char *tempLockPath = NULL;
40643    int rc = SQLITE_OK;
40644    int createConch = 0;
40645    int hostIdMatch = 0;
40646    int readLen = 0;
40647    int tryOldLockPath = 0;
40648    int forceNewLockPath = 0;
40649
40650    OSTRACE(("TAKECONCH  %d for %s pid=%d\n", conchFile->h,
40651             (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
40652             osGetpid(0)));
40653
40654    rc = proxyGetHostID(myHostID, &pError);
40655    if( (rc&0xff)==SQLITE_IOERR ){
40656      storeLastErrno(pFile, pError);
40657      goto end_takeconch;
40658    }
40659    rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
40660    if( rc!=SQLITE_OK ){
40661      goto end_takeconch;
40662    }
40663    /* read the existing conch file */
40664    readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
40665    if( readLen<0 ){
40666      /* I/O error: lastErrno set by seekAndRead */
40667      storeLastErrno(pFile, conchFile->lastErrno);
40668      rc = SQLITE_IOERR_READ;
40669      goto end_takeconch;
40670    }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
40671             readBuf[0]!=(char)PROXY_CONCHVERSION ){
40672      /* a short read or version format mismatch means we need to create a new
40673      ** conch file.
40674      */
40675      createConch = 1;
40676    }
40677    /* if the host id matches and the lock path already exists in the conch
40678    ** we'll try to use the path there, if we can't open that path, we'll
40679    ** retry with a new auto-generated path
40680    */
40681    do { /* in case we need to try again for an :auto: named lock file */
40682
40683      if( !createConch && !forceNewLockPath ){
40684        hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
40685                                  PROXY_HOSTIDLEN);
40686        /* if the conch has data compare the contents */
40687        if( !pCtx->lockProxyPath ){
40688          /* for auto-named local lock file, just check the host ID and we'll
40689           ** use the local lock file path that's already in there
40690           */
40691          if( hostIdMatch ){
40692            size_t pathLen = (readLen - PROXY_PATHINDEX);
40693
40694            if( pathLen>=MAXPATHLEN ){
40695              pathLen=MAXPATHLEN-1;
40696            }
40697            memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
40698            lockPath[pathLen] = 0;
40699            tempLockPath = lockPath;
40700            tryOldLockPath = 1;
40701            /* create a copy of the lock path if the conch is taken */
40702            goto end_takeconch;
40703          }
40704        }else if( hostIdMatch
40705               && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
40706                           readLen-PROXY_PATHINDEX)
40707        ){
40708          /* conch host and lock path match */
40709          goto end_takeconch;
40710        }
40711      }
40712
40713      /* if the conch isn't writable and doesn't match, we can't take it */
40714      if( (conchFile->openFlags&O_RDWR) == 0 ){
40715        rc = SQLITE_BUSY;
40716        goto end_takeconch;
40717      }
40718
40719      /* either the conch didn't match or we need to create a new one */
40720      if( !pCtx->lockProxyPath ){
40721        proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
40722        tempLockPath = lockPath;
40723        /* create a copy of the lock path _only_ if the conch is taken */
40724      }
40725
40726      /* update conch with host and path (this will fail if other process
40727      ** has a shared lock already), if the host id matches, use the big
40728      ** stick.
40729      */
40730      futimes(conchFile->h, NULL);
40731      if( hostIdMatch && !createConch ){
40732        if( conchFile->pInode && conchFile->pInode->nShared>1 ){
40733          /* We are trying for an exclusive lock but another thread in this
40734           ** same process is still holding a shared lock. */
40735          rc = SQLITE_BUSY;
40736        } else {
40737          rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
40738        }
40739      }else{
40740        rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
40741      }
40742      if( rc==SQLITE_OK ){
40743        char writeBuffer[PROXY_MAXCONCHLEN];
40744        int writeSize = 0;
40745
40746        writeBuffer[0] = (char)PROXY_CONCHVERSION;
40747        memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
40748        if( pCtx->lockProxyPath!=NULL ){
40749          strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
40750                  MAXPATHLEN);
40751        }else{
40752          strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
40753        }
40754        writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
40755        robust_ftruncate(conchFile->h, writeSize);
40756        rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
40757        full_fsync(conchFile->h,0,0);
40758        /* If we created a new conch file (not just updated the contents of a
40759         ** valid conch file), try to match the permissions of the database
40760         */
40761        if( rc==SQLITE_OK && createConch ){
40762          struct stat buf;
40763          int err = osFstat(pFile->h, &buf);
40764          if( err==0 ){
40765            mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
40766                                        S_IROTH|S_IWOTH);
40767            /* try to match the database file R/W permissions, ignore failure */
40768#ifndef SQLITE_PROXY_DEBUG
40769            osFchmod(conchFile->h, cmode);
40770#else
40771            do{
40772              rc = osFchmod(conchFile->h, cmode);
40773            }while( rc==(-1) && errno==EINTR );
40774            if( rc!=0 ){
40775              int code = errno;
40776              fprintf(stderr, "fchmod %o FAILED with %d %s\n",
40777                      cmode, code, strerror(code));
40778            } else {
40779              fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
40780            }
40781          }else{
40782            int code = errno;
40783            fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
40784                    err, code, strerror(code));
40785#endif
40786          }
40787        }
40788      }
40789      conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
40790
40791    end_takeconch:
40792      OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
40793      if( rc==SQLITE_OK && pFile->openFlags ){
40794        int fd;
40795        if( pFile->h>=0 ){
40796          robust_close(pFile, pFile->h, __LINE__);
40797        }
40798        pFile->h = -1;
40799        fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
40800        OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
40801        if( fd>=0 ){
40802          pFile->h = fd;
40803        }else{
40804          rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
40805           during locking */
40806        }
40807      }
40808      if( rc==SQLITE_OK && !pCtx->lockProxy ){
40809        char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
40810        rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
40811        if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
40812          /* we couldn't create the proxy lock file with the old lock file path
40813           ** so try again via auto-naming
40814           */
40815          forceNewLockPath = 1;
40816          tryOldLockPath = 0;
40817          continue; /* go back to the do {} while start point, try again */
40818        }
40819      }
40820      if( rc==SQLITE_OK ){
40821        /* Need to make a copy of path if we extracted the value
40822         ** from the conch file or the path was allocated on the stack
40823         */
40824        if( tempLockPath ){
40825          pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
40826          if( !pCtx->lockProxyPath ){
40827            rc = SQLITE_NOMEM_BKPT;
40828          }
40829        }
40830      }
40831      if( rc==SQLITE_OK ){
40832        pCtx->conchHeld = 1;
40833
40834        if( pCtx->lockProxy->pMethod == &afpIoMethods ){
40835          afpLockingContext *afpCtx;
40836          afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
40837          afpCtx->dbPath = pCtx->lockProxyPath;
40838        }
40839      } else {
40840        conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
40841      }
40842      OSTRACE(("TAKECONCH  %d %s\n", conchFile->h,
40843               rc==SQLITE_OK?"ok":"failed"));
40844      return rc;
40845    } while (1); /* in case we need to retry the :auto: lock file -
40846                 ** we should never get here except via the 'continue' call. */
40847  }
40848}
40849
40850/*
40851** If pFile holds a lock on a conch file, then release that lock.
40852*/
40853static int proxyReleaseConch(unixFile *pFile){
40854  int rc = SQLITE_OK;         /* Subroutine return code */
40855  proxyLockingContext *pCtx;  /* The locking context for the proxy lock */
40856  unixFile *conchFile;        /* Name of the conch file */
40857
40858  pCtx = (proxyLockingContext *)pFile->lockingContext;
40859  conchFile = pCtx->conchFile;
40860  OSTRACE(("RELEASECONCH  %d for %s pid=%d\n", conchFile->h,
40861           (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
40862           osGetpid(0)));
40863  if( pCtx->conchHeld>0 ){
40864    rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
40865  }
40866  pCtx->conchHeld = 0;
40867  OSTRACE(("RELEASECONCH  %d %s\n", conchFile->h,
40868           (rc==SQLITE_OK ? "ok" : "failed")));
40869  return rc;
40870}
40871
40872/*
40873** Given the name of a database file, compute the name of its conch file.
40874** Store the conch filename in memory obtained from sqlite3_malloc64().
40875** Make *pConchPath point to the new name.  Return SQLITE_OK on success
40876** or SQLITE_NOMEM if unable to obtain memory.
40877**
40878** The caller is responsible for ensuring that the allocated memory
40879** space is eventually freed.
40880**
40881** *pConchPath is set to NULL if a memory allocation error occurs.
40882*/
40883static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
40884  int i;                        /* Loop counter */
40885  int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
40886  char *conchPath;              /* buffer in which to construct conch name */
40887
40888  /* Allocate space for the conch filename and initialize the name to
40889  ** the name of the original database file. */
40890  *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
40891  if( conchPath==0 ){
40892    return SQLITE_NOMEM_BKPT;
40893  }
40894  memcpy(conchPath, dbPath, len+1);
40895
40896  /* now insert a "." before the last / character */
40897  for( i=(len-1); i>=0; i-- ){
40898    if( conchPath[i]=='/' ){
40899      i++;
40900      break;
40901    }
40902  }
40903  conchPath[i]='.';
40904  while ( i<len ){
40905    conchPath[i+1]=dbPath[i];
40906    i++;
40907  }
40908
40909  /* append the "-conch" suffix to the file */
40910  memcpy(&conchPath[i+1], "-conch", 7);
40911  assert( (int)strlen(conchPath) == len+7 );
40912
40913  return SQLITE_OK;
40914}
40915
40916
40917/* Takes a fully configured proxy locking-style unix file and switches
40918** the local lock file path
40919*/
40920static int switchLockProxyPath(unixFile *pFile, const char *path) {
40921  proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
40922  char *oldPath = pCtx->lockProxyPath;
40923  int rc = SQLITE_OK;
40924
40925  if( pFile->eFileLock!=NO_LOCK ){
40926    return SQLITE_BUSY;
40927  }
40928
40929  /* nothing to do if the path is NULL, :auto: or matches the existing path */
40930  if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
40931    (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
40932    return SQLITE_OK;
40933  }else{
40934    unixFile *lockProxy = pCtx->lockProxy;
40935    pCtx->lockProxy=NULL;
40936    pCtx->conchHeld = 0;
40937    if( lockProxy!=NULL ){
40938      rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
40939      if( rc ) return rc;
40940      sqlite3_free(lockProxy);
40941    }
40942    sqlite3_free(oldPath);
40943    pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
40944  }
40945
40946  return rc;
40947}
40948
40949/*
40950** pFile is a file that has been opened by a prior xOpen call.  dbPath
40951** is a string buffer at least MAXPATHLEN+1 characters in size.
40952**
40953** This routine find the filename associated with pFile and writes it
40954** int dbPath.
40955*/
40956static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
40957#if defined(__APPLE__)
40958  if( pFile->pMethod == &afpIoMethods ){
40959    /* afp style keeps a reference to the db path in the filePath field
40960    ** of the struct */
40961    assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
40962    strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
40963            MAXPATHLEN);
40964  } else
40965#endif
40966  if( pFile->pMethod == &dotlockIoMethods ){
40967    /* dot lock style uses the locking context to store the dot lock
40968    ** file path */
40969    int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
40970    memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
40971  }else{
40972    /* all other styles use the locking context to store the db file path */
40973    assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
40974    strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
40975  }
40976  return SQLITE_OK;
40977}
40978
40979/*
40980** Takes an already filled in unix file and alters it so all file locking
40981** will be performed on the local proxy lock file.  The following fields
40982** are preserved in the locking context so that they can be restored and
40983** the unix structure properly cleaned up at close time:
40984**  ->lockingContext
40985**  ->pMethod
40986*/
40987static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
40988  proxyLockingContext *pCtx;
40989  char dbPath[MAXPATHLEN+1];       /* Name of the database file */
40990  char *lockPath=NULL;
40991  int rc = SQLITE_OK;
40992
40993  if( pFile->eFileLock!=NO_LOCK ){
40994    return SQLITE_BUSY;
40995  }
40996  proxyGetDbPathForUnixFile(pFile, dbPath);
40997  if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
40998    lockPath=NULL;
40999  }else{
41000    lockPath=(char *)path;
41001  }
41002
41003  OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
41004           (lockPath ? lockPath : ":auto:"), osGetpid(0)));
41005
41006  pCtx = sqlite3_malloc64( sizeof(*pCtx) );
41007  if( pCtx==0 ){
41008    return SQLITE_NOMEM_BKPT;
41009  }
41010  memset(pCtx, 0, sizeof(*pCtx));
41011
41012  rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
41013  if( rc==SQLITE_OK ){
41014    rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
41015    if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
41016      /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
41017      ** (c) the file system is read-only, then enable no-locking access.
41018      ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
41019      ** that openFlags will have only one of O_RDONLY or O_RDWR.
41020      */
41021      struct statfs fsInfo;
41022      struct stat conchInfo;
41023      int goLockless = 0;
41024
41025      if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
41026        int err = errno;
41027        if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
41028          goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
41029        }
41030      }
41031      if( goLockless ){
41032        pCtx->conchHeld = -1; /* read only FS/ lockless */
41033        rc = SQLITE_OK;
41034      }
41035    }
41036  }
41037  if( rc==SQLITE_OK && lockPath ){
41038    pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
41039  }
41040
41041  if( rc==SQLITE_OK ){
41042    pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
41043    if( pCtx->dbPath==NULL ){
41044      rc = SQLITE_NOMEM_BKPT;
41045    }
41046  }
41047  if( rc==SQLITE_OK ){
41048    /* all memory is allocated, proxys are created and assigned,
41049    ** switch the locking context and pMethod then return.
41050    */
41051    pCtx->oldLockingContext = pFile->lockingContext;
41052    pFile->lockingContext = pCtx;
41053    pCtx->pOldMethod = pFile->pMethod;
41054    pFile->pMethod = &proxyIoMethods;
41055  }else{
41056    if( pCtx->conchFile ){
41057      pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
41058      sqlite3_free(pCtx->conchFile);
41059    }
41060    sqlite3DbFree(0, pCtx->lockProxyPath);
41061    sqlite3_free(pCtx->conchFilePath);
41062    sqlite3_free(pCtx);
41063  }
41064  OSTRACE(("TRANSPROXY  %d %s\n", pFile->h,
41065           (rc==SQLITE_OK ? "ok" : "failed")));
41066  return rc;
41067}
41068
41069
41070/*
41071** This routine handles sqlite3_file_control() calls that are specific
41072** to proxy locking.
41073*/
41074static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
41075  switch( op ){
41076    case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
41077      unixFile *pFile = (unixFile*)id;
41078      if( pFile->pMethod == &proxyIoMethods ){
41079        proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
41080        proxyTakeConch(pFile);
41081        if( pCtx->lockProxyPath ){
41082          *(const char **)pArg = pCtx->lockProxyPath;
41083        }else{
41084          *(const char **)pArg = ":auto: (not held)";
41085        }
41086      } else {
41087        *(const char **)pArg = NULL;
41088      }
41089      return SQLITE_OK;
41090    }
41091    case SQLITE_FCNTL_SET_LOCKPROXYFILE: {
41092      unixFile *pFile = (unixFile*)id;
41093      int rc = SQLITE_OK;
41094      int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
41095      if( pArg==NULL || (const char *)pArg==0 ){
41096        if( isProxyStyle ){
41097          /* turn off proxy locking - not supported.  If support is added for
41098          ** switching proxy locking mode off then it will need to fail if
41099          ** the journal mode is WAL mode.
41100          */
41101          rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
41102        }else{
41103          /* turn off proxy locking - already off - NOOP */
41104          rc = SQLITE_OK;
41105        }
41106      }else{
41107        const char *proxyPath = (const char *)pArg;
41108        if( isProxyStyle ){
41109          proxyLockingContext *pCtx =
41110            (proxyLockingContext*)pFile->lockingContext;
41111          if( !strcmp(pArg, ":auto:")
41112           || (pCtx->lockProxyPath &&
41113               !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
41114          ){
41115            rc = SQLITE_OK;
41116          }else{
41117            rc = switchLockProxyPath(pFile, proxyPath);
41118          }
41119        }else{
41120          /* turn on proxy file locking */
41121          rc = proxyTransformUnixFile(pFile, proxyPath);
41122        }
41123      }
41124      return rc;
41125    }
41126    default: {
41127      assert( 0 );  /* The call assures that only valid opcodes are sent */
41128    }
41129  }
41130  /*NOTREACHED*/ assert(0);
41131  return SQLITE_ERROR;
41132}
41133
41134/*
41135** Within this division (the proxying locking implementation) the procedures
41136** above this point are all utilities.  The lock-related methods of the
41137** proxy-locking sqlite3_io_method object follow.
41138*/
41139
41140
41141/*
41142** This routine checks if there is a RESERVED lock held on the specified
41143** file by this or any other process. If such a lock is held, set *pResOut
41144** to a non-zero value otherwise *pResOut is set to zero.  The return value
41145** is set to SQLITE_OK unless an I/O error occurs during lock checking.
41146*/
41147static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
41148  unixFile *pFile = (unixFile*)id;
41149  int rc = proxyTakeConch(pFile);
41150  if( rc==SQLITE_OK ){
41151    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41152    if( pCtx->conchHeld>0 ){
41153      unixFile *proxy = pCtx->lockProxy;
41154      return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
41155    }else{ /* conchHeld < 0 is lockless */
41156      pResOut=0;
41157    }
41158  }
41159  return rc;
41160}
41161
41162/*
41163** Lock the file with the lock specified by parameter eFileLock - one
41164** of the following:
41165**
41166**     (1) SHARED_LOCK
41167**     (2) RESERVED_LOCK
41168**     (3) PENDING_LOCK
41169**     (4) EXCLUSIVE_LOCK
41170**
41171** Sometimes when requesting one lock state, additional lock states
41172** are inserted in between.  The locking might fail on one of the later
41173** transitions leaving the lock state different from what it started but
41174** still short of its goal.  The following chart shows the allowed
41175** transitions and the inserted intermediate states:
41176**
41177**    UNLOCKED -> SHARED
41178**    SHARED -> RESERVED
41179**    SHARED -> (PENDING) -> EXCLUSIVE
41180**    RESERVED -> (PENDING) -> EXCLUSIVE
41181**    PENDING -> EXCLUSIVE
41182**
41183** This routine will only increase a lock.  Use the sqlite3OsUnlock()
41184** routine to lower a locking level.
41185*/
41186static int proxyLock(sqlite3_file *id, int eFileLock) {
41187  unixFile *pFile = (unixFile*)id;
41188  int rc = proxyTakeConch(pFile);
41189  if( rc==SQLITE_OK ){
41190    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41191    if( pCtx->conchHeld>0 ){
41192      unixFile *proxy = pCtx->lockProxy;
41193      rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
41194      pFile->eFileLock = proxy->eFileLock;
41195    }else{
41196      /* conchHeld < 0 is lockless */
41197    }
41198  }
41199  return rc;
41200}
41201
41202
41203/*
41204** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
41205** must be either NO_LOCK or SHARED_LOCK.
41206**
41207** If the locking level of the file descriptor is already at or below
41208** the requested locking level, this routine is a no-op.
41209*/
41210static int proxyUnlock(sqlite3_file *id, int eFileLock) {
41211  unixFile *pFile = (unixFile*)id;
41212  int rc = proxyTakeConch(pFile);
41213  if( rc==SQLITE_OK ){
41214    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41215    if( pCtx->conchHeld>0 ){
41216      unixFile *proxy = pCtx->lockProxy;
41217      rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
41218      pFile->eFileLock = proxy->eFileLock;
41219    }else{
41220      /* conchHeld < 0 is lockless */
41221    }
41222  }
41223  return rc;
41224}
41225
41226/*
41227** Close a file that uses proxy locks.
41228*/
41229static int proxyClose(sqlite3_file *id) {
41230  if( ALWAYS(id) ){
41231    unixFile *pFile = (unixFile*)id;
41232    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41233    unixFile *lockProxy = pCtx->lockProxy;
41234    unixFile *conchFile = pCtx->conchFile;
41235    int rc = SQLITE_OK;
41236
41237    if( lockProxy ){
41238      rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
41239      if( rc ) return rc;
41240      rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
41241      if( rc ) return rc;
41242      sqlite3_free(lockProxy);
41243      pCtx->lockProxy = 0;
41244    }
41245    if( conchFile ){
41246      if( pCtx->conchHeld ){
41247        rc = proxyReleaseConch(pFile);
41248        if( rc ) return rc;
41249      }
41250      rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
41251      if( rc ) return rc;
41252      sqlite3_free(conchFile);
41253    }
41254    sqlite3DbFree(0, pCtx->lockProxyPath);
41255    sqlite3_free(pCtx->conchFilePath);
41256    sqlite3DbFree(0, pCtx->dbPath);
41257    /* restore the original locking context and pMethod then close it */
41258    pFile->lockingContext = pCtx->oldLockingContext;
41259    pFile->pMethod = pCtx->pOldMethod;
41260    sqlite3_free(pCtx);
41261    return pFile->pMethod->xClose(id);
41262  }
41263  return SQLITE_OK;
41264}
41265
41266
41267
41268#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
41269/*
41270** The proxy locking style is intended for use with AFP filesystems.
41271** And since AFP is only supported on MacOSX, the proxy locking is also
41272** restricted to MacOSX.
41273**
41274**
41275******************* End of the proxy lock implementation **********************
41276******************************************************************************/
41277
41278/*
41279** Initialize the operating system interface.
41280**
41281** This routine registers all VFS implementations for unix-like operating
41282** systems.  This routine, and the sqlite3_os_end() routine that follows,
41283** should be the only routines in this file that are visible from other
41284** files.
41285**
41286** This routine is called once during SQLite initialization and by a
41287** single thread.  The memory allocation and mutex subsystems have not
41288** necessarily been initialized when this routine is called, and so they
41289** should not be used.
41290*/
41291SQLITE_API int sqlite3_os_init(void){
41292  /*
41293  ** The following macro defines an initializer for an sqlite3_vfs object.
41294  ** The name of the VFS is NAME.  The pAppData is a pointer to a pointer
41295  ** to the "finder" function.  (pAppData is a pointer to a pointer because
41296  ** silly C90 rules prohibit a void* from being cast to a function pointer
41297  ** and so we have to go through the intermediate pointer to avoid problems
41298  ** when compiling with -pedantic-errors on GCC.)
41299  **
41300  ** The FINDER parameter to this macro is the name of the pointer to the
41301  ** finder-function.  The finder-function returns a pointer to the
41302  ** sqlite_io_methods object that implements the desired locking
41303  ** behaviors.  See the division above that contains the IOMETHODS
41304  ** macro for addition information on finder-functions.
41305  **
41306  ** Most finders simply return a pointer to a fixed sqlite3_io_methods
41307  ** object.  But the "autolockIoFinder" available on MacOSX does a little
41308  ** more than that; it looks at the filesystem type that hosts the
41309  ** database file and tries to choose an locking method appropriate for
41310  ** that filesystem time.
41311  */
41312  #define UNIXVFS(VFSNAME, FINDER) {                        \
41313    3,                    /* iVersion */                    \
41314    sizeof(unixFile),     /* szOsFile */                    \
41315    MAX_PATHNAME,         /* mxPathname */                  \
41316    0,                    /* pNext */                       \
41317    VFSNAME,              /* zName */                       \
41318    (void*)&FINDER,       /* pAppData */                    \
41319    unixOpen,             /* xOpen */                       \
41320    unixDelete,           /* xDelete */                     \
41321    unixAccess,           /* xAccess */                     \
41322    unixFullPathname,     /* xFullPathname */               \
41323    unixDlOpen,           /* xDlOpen */                     \
41324    unixDlError,          /* xDlError */                    \
41325    unixDlSym,            /* xDlSym */                      \
41326    unixDlClose,          /* xDlClose */                    \
41327    unixRandomness,       /* xRandomness */                 \
41328    unixSleep,            /* xSleep */                      \
41329    unixCurrentTime,      /* xCurrentTime */                \
41330    unixGetLastError,     /* xGetLastError */               \
41331    unixCurrentTimeInt64, /* xCurrentTimeInt64 */           \
41332    unixSetSystemCall,    /* xSetSystemCall */              \
41333    unixGetSystemCall,    /* xGetSystemCall */              \
41334    unixNextSystemCall,   /* xNextSystemCall */             \
41335  }
41336
41337  /*
41338  ** All default VFSes for unix are contained in the following array.
41339  **
41340  ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
41341  ** by the SQLite core when the VFS is registered.  So the following
41342  ** array cannot be const.
41343  */
41344  static sqlite3_vfs aVfs[] = {
41345#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
41346    UNIXVFS("unix",          autolockIoFinder ),
41347#elif OS_VXWORKS
41348    UNIXVFS("unix",          vxworksIoFinder ),
41349#else
41350    UNIXVFS("unix",          posixIoFinder ),
41351#endif
41352    UNIXVFS("unix-none",     nolockIoFinder ),
41353    UNIXVFS("unix-dotfile",  dotlockIoFinder ),
41354    UNIXVFS("unix-excl",     posixIoFinder ),
41355#if OS_VXWORKS
41356    UNIXVFS("unix-namedsem", semIoFinder ),
41357#endif
41358#if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
41359    UNIXVFS("unix-posix",    posixIoFinder ),
41360#endif
41361#if SQLITE_ENABLE_LOCKING_STYLE
41362    UNIXVFS("unix-flock",    flockIoFinder ),
41363#endif
41364#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
41365    UNIXVFS("unix-afp",      afpIoFinder ),
41366    UNIXVFS("unix-nfs",      nfsIoFinder ),
41367    UNIXVFS("unix-proxy",    proxyIoFinder ),
41368#endif
41369  };
41370  unsigned int i;          /* Loop counter */
41371
41372  /* Double-check that the aSyscall[] array has been constructed
41373  ** correctly.  See ticket [bb3a86e890c8e96ab] */
41374  assert( ArraySize(aSyscall)==29 );
41375
41376  /* Register all VFSes defined in the aVfs[] array */
41377  for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
41378    sqlite3_vfs_register(&aVfs[i], i==0);
41379  }
41380  unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
41381  return SQLITE_OK;
41382}
41383
41384/*
41385** Shutdown the operating system interface.
41386**
41387** Some operating systems might need to do some cleanup in this routine,
41388** to release dynamically allocated objects.  But not on unix.
41389** This routine is a no-op for unix.
41390*/
41391SQLITE_API int sqlite3_os_end(void){
41392  unixBigLock = 0;
41393  return SQLITE_OK;
41394}
41395
41396#endif /* SQLITE_OS_UNIX */
41397
41398/************** End of os_unix.c *********************************************/
41399/************** Begin file os_win.c ******************************************/
41400/*
41401** 2004 May 22
41402**
41403** The author disclaims copyright to this source code.  In place of
41404** a legal notice, here is a blessing:
41405**
41406**    May you do good and not evil.
41407**    May you find forgiveness for yourself and forgive others.
41408**    May you share freely, never taking more than you give.
41409**
41410******************************************************************************
41411**
41412** This file contains code that is specific to Windows.
41413*/
41414/* #include "sqliteInt.h" */
41415#if SQLITE_OS_WIN               /* This file is used for Windows only */
41416
41417/*
41418** Include code that is common to all os_*.c files
41419*/
41420/************** Include os_common.h in the middle of os_win.c ****************/
41421/************** Begin file os_common.h ***************************************/
41422/*
41423** 2004 May 22
41424**
41425** The author disclaims copyright to this source code.  In place of
41426** a legal notice, here is a blessing:
41427**
41428**    May you do good and not evil.
41429**    May you find forgiveness for yourself and forgive others.
41430**    May you share freely, never taking more than you give.
41431**
41432******************************************************************************
41433**
41434** This file contains macros and a little bit of code that is common to
41435** all of the platform-specific files (os_*.c) and is #included into those
41436** files.
41437**
41438** This file should be #included by the os_*.c files only.  It is not a
41439** general purpose header file.
41440*/
41441#ifndef _OS_COMMON_H_
41442#define _OS_COMMON_H_
41443
41444/*
41445** At least two bugs have slipped in because we changed the MEMORY_DEBUG
41446** macro to SQLITE_DEBUG and some older makefiles have not yet made the
41447** switch.  The following code should catch this problem at compile-time.
41448*/
41449#ifdef MEMORY_DEBUG
41450# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
41451#endif
41452
41453/*
41454** Macros for performance tracing.  Normally turned off.  Only works
41455** on i486 hardware.
41456*/
41457#ifdef SQLITE_PERFORMANCE_TRACE
41458
41459/*
41460** hwtime.h contains inline assembler code for implementing
41461** high-performance timing routines.
41462*/
41463/************** Include hwtime.h in the middle of os_common.h ****************/
41464/************** Begin file hwtime.h ******************************************/
41465/*
41466** 2008 May 27
41467**
41468** The author disclaims copyright to this source code.  In place of
41469** a legal notice, here is a blessing:
41470**
41471**    May you do good and not evil.
41472**    May you find forgiveness for yourself and forgive others.
41473**    May you share freely, never taking more than you give.
41474**
41475******************************************************************************
41476**
41477** This file contains inline asm code for retrieving "high-performance"
41478** counters for x86 and x86_64 class CPUs.
41479*/
41480#ifndef SQLITE_HWTIME_H
41481#define SQLITE_HWTIME_H
41482
41483/*
41484** The following routine only works on pentium-class (or newer) processors.
41485** It uses the RDTSC opcode to read the cycle count value out of the
41486** processor and returns that value.  This can be used for high-res
41487** profiling.
41488*/
41489#if !defined(__STRICT_ANSI__) && \
41490    (defined(__GNUC__) || defined(_MSC_VER)) && \
41491    (defined(i386) || defined(__i386__) || defined(_M_IX86))
41492
41493  #if defined(__GNUC__)
41494
41495  __inline__ sqlite_uint64 sqlite3Hwtime(void){
41496     unsigned int lo, hi;
41497     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
41498     return (sqlite_uint64)hi << 32 | lo;
41499  }
41500
41501  #elif defined(_MSC_VER)
41502
41503  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
41504     __asm {
41505        rdtsc
41506        ret       ; return value at EDX:EAX
41507     }
41508  }
41509
41510  #endif
41511
41512#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__x86_64__))
41513
41514  __inline__ sqlite_uint64 sqlite3Hwtime(void){
41515      unsigned long val;
41516      __asm__ __volatile__ ("rdtsc" : "=A" (val));
41517      return val;
41518  }
41519
41520#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__ppc__))
41521
41522  __inline__ sqlite_uint64 sqlite3Hwtime(void){
41523      unsigned long long retval;
41524      unsigned long junk;
41525      __asm__ __volatile__ ("\n\
41526          1:      mftbu   %1\n\
41527                  mftb    %L0\n\
41528                  mftbu   %0\n\
41529                  cmpw    %0,%1\n\
41530                  bne     1b"
41531                  : "=r" (retval), "=r" (junk));
41532      return retval;
41533  }
41534
41535#else
41536
41537  /*
41538  ** asm() is needed for hardware timing support.  Without asm(),
41539  ** disable the sqlite3Hwtime() routine.
41540  **
41541  ** sqlite3Hwtime() is only used for some obscure debugging
41542  ** and analysis configurations, not in any deliverable, so this
41543  ** should not be a great loss.
41544  */
41545SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
41546
41547#endif
41548
41549#endif /* !defined(SQLITE_HWTIME_H) */
41550
41551/************** End of hwtime.h **********************************************/
41552/************** Continuing where we left off in os_common.h ******************/
41553
41554static sqlite_uint64 g_start;
41555static sqlite_uint64 g_elapsed;
41556#define TIMER_START       g_start=sqlite3Hwtime()
41557#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
41558#define TIMER_ELAPSED     g_elapsed
41559#else
41560#define TIMER_START
41561#define TIMER_END
41562#define TIMER_ELAPSED     ((sqlite_uint64)0)
41563#endif
41564
41565/*
41566** If we compile with the SQLITE_TEST macro set, then the following block
41567** of code will give us the ability to simulate a disk I/O error.  This
41568** is used for testing the I/O recovery logic.
41569*/
41570#if defined(SQLITE_TEST)
41571SQLITE_API extern int sqlite3_io_error_hit;
41572SQLITE_API extern int sqlite3_io_error_hardhit;
41573SQLITE_API extern int sqlite3_io_error_pending;
41574SQLITE_API extern int sqlite3_io_error_persist;
41575SQLITE_API extern int sqlite3_io_error_benign;
41576SQLITE_API extern int sqlite3_diskfull_pending;
41577SQLITE_API extern int sqlite3_diskfull;
41578#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
41579#define SimulateIOError(CODE)  \
41580  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
41581       || sqlite3_io_error_pending-- == 1 )  \
41582              { local_ioerr(); CODE; }
41583static void local_ioerr(){
41584  IOTRACE(("IOERR\n"));
41585  sqlite3_io_error_hit++;
41586  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
41587}
41588#define SimulateDiskfullError(CODE) \
41589   if( sqlite3_diskfull_pending ){ \
41590     if( sqlite3_diskfull_pending == 1 ){ \
41591       local_ioerr(); \
41592       sqlite3_diskfull = 1; \
41593       sqlite3_io_error_hit = 1; \
41594       CODE; \
41595     }else{ \
41596       sqlite3_diskfull_pending--; \
41597     } \
41598   }
41599#else
41600#define SimulateIOErrorBenign(X)
41601#define SimulateIOError(A)
41602#define SimulateDiskfullError(A)
41603#endif /* defined(SQLITE_TEST) */
41604
41605/*
41606** When testing, keep a count of the number of open files.
41607*/
41608#if defined(SQLITE_TEST)
41609SQLITE_API extern int sqlite3_open_file_count;
41610#define OpenCounter(X)  sqlite3_open_file_count+=(X)
41611#else
41612#define OpenCounter(X)
41613#endif /* defined(SQLITE_TEST) */
41614
41615#endif /* !defined(_OS_COMMON_H_) */
41616
41617/************** End of os_common.h *******************************************/
41618/************** Continuing where we left off in os_win.c *********************/
41619
41620/*
41621** Include the header file for the Windows VFS.
41622*/
41623/* #include "os_win.h" */
41624
41625/*
41626** Compiling and using WAL mode requires several APIs that are only
41627** available in Windows platforms based on the NT kernel.
41628*/
41629#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
41630#  error "WAL mode requires support from the Windows NT kernel, compile\
41631 with SQLITE_OMIT_WAL."
41632#endif
41633
41634#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0
41635#  error "Memory mapped files require support from the Windows NT kernel,\
41636 compile with SQLITE_MAX_MMAP_SIZE=0."
41637#endif
41638
41639/*
41640** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
41641** based on the sub-platform)?
41642*/
41643#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
41644#  define SQLITE_WIN32_HAS_ANSI
41645#endif
41646
41647/*
41648** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
41649** based on the sub-platform)?
41650*/
41651#if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
41652    !defined(SQLITE_WIN32_NO_WIDE)
41653#  define SQLITE_WIN32_HAS_WIDE
41654#endif
41655
41656/*
41657** Make sure at least one set of Win32 APIs is available.
41658*/
41659#if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
41660#  error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
41661 must be defined."
41662#endif
41663
41664/*
41665** Define the required Windows SDK version constants if they are not
41666** already available.
41667*/
41668#ifndef NTDDI_WIN8
41669#  define NTDDI_WIN8                        0x06020000
41670#endif
41671
41672#ifndef NTDDI_WINBLUE
41673#  define NTDDI_WINBLUE                     0x06030000
41674#endif
41675
41676#ifndef NTDDI_WINTHRESHOLD
41677#  define NTDDI_WINTHRESHOLD                0x06040000
41678#endif
41679
41680/*
41681** Check to see if the GetVersionEx[AW] functions are deprecated on the
41682** target system.  GetVersionEx was first deprecated in Win8.1.
41683*/
41684#ifndef SQLITE_WIN32_GETVERSIONEX
41685#  if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
41686#    define SQLITE_WIN32_GETVERSIONEX   0   /* GetVersionEx() is deprecated */
41687#  else
41688#    define SQLITE_WIN32_GETVERSIONEX   1   /* GetVersionEx() is current */
41689#  endif
41690#endif
41691
41692/*
41693** Check to see if the CreateFileMappingA function is supported on the
41694** target system.  It is unavailable when using "mincore.lib" on Win10.
41695** When compiling for Windows 10, always assume "mincore.lib" is in use.
41696*/
41697#ifndef SQLITE_WIN32_CREATEFILEMAPPINGA
41698#  if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD
41699#    define SQLITE_WIN32_CREATEFILEMAPPINGA   0
41700#  else
41701#    define SQLITE_WIN32_CREATEFILEMAPPINGA   1
41702#  endif
41703#endif
41704
41705/*
41706** This constant should already be defined (in the "WinDef.h" SDK file).
41707*/
41708#ifndef MAX_PATH
41709#  define MAX_PATH                      (260)
41710#endif
41711
41712/*
41713** Maximum pathname length (in chars) for Win32.  This should normally be
41714** MAX_PATH.
41715*/
41716#ifndef SQLITE_WIN32_MAX_PATH_CHARS
41717#  define SQLITE_WIN32_MAX_PATH_CHARS   (MAX_PATH)
41718#endif
41719
41720/*
41721** This constant should already be defined (in the "WinNT.h" SDK file).
41722*/
41723#ifndef UNICODE_STRING_MAX_CHARS
41724#  define UNICODE_STRING_MAX_CHARS      (32767)
41725#endif
41726
41727/*
41728** Maximum pathname length (in chars) for WinNT.  This should normally be
41729** UNICODE_STRING_MAX_CHARS.
41730*/
41731#ifndef SQLITE_WINNT_MAX_PATH_CHARS
41732#  define SQLITE_WINNT_MAX_PATH_CHARS   (UNICODE_STRING_MAX_CHARS)
41733#endif
41734
41735/*
41736** Maximum pathname length (in bytes) for Win32.  The MAX_PATH macro is in
41737** characters, so we allocate 4 bytes per character assuming worst-case of
41738** 4-bytes-per-character for UTF8.
41739*/
41740#ifndef SQLITE_WIN32_MAX_PATH_BYTES
41741#  define SQLITE_WIN32_MAX_PATH_BYTES   (SQLITE_WIN32_MAX_PATH_CHARS*4)
41742#endif
41743
41744/*
41745** Maximum pathname length (in bytes) for WinNT.  This should normally be
41746** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
41747*/
41748#ifndef SQLITE_WINNT_MAX_PATH_BYTES
41749#  define SQLITE_WINNT_MAX_PATH_BYTES   \
41750                            (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
41751#endif
41752
41753/*
41754** Maximum error message length (in chars) for WinRT.
41755*/
41756#ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
41757#  define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
41758#endif
41759
41760/*
41761** Returns non-zero if the character should be treated as a directory
41762** separator.
41763*/
41764#ifndef winIsDirSep
41765#  define winIsDirSep(a)                (((a) == '/') || ((a) == '\\'))
41766#endif
41767
41768/*
41769** This macro is used when a local variable is set to a value that is
41770** [sometimes] not used by the code (e.g. via conditional compilation).
41771*/
41772#ifndef UNUSED_VARIABLE_VALUE
41773#  define UNUSED_VARIABLE_VALUE(x)      (void)(x)
41774#endif
41775
41776/*
41777** Returns the character that should be used as the directory separator.
41778*/
41779#ifndef winGetDirSep
41780#  define winGetDirSep()                '\\'
41781#endif
41782
41783/*
41784** Do we need to manually define the Win32 file mapping APIs for use with WAL
41785** mode or memory mapped files (e.g. these APIs are available in the Windows
41786** CE SDK; however, they are not present in the header file)?
41787*/
41788#if SQLITE_WIN32_FILEMAPPING_API && \
41789        (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
41790/*
41791** Two of the file mapping APIs are different under WinRT.  Figure out which
41792** set we need.
41793*/
41794#if SQLITE_OS_WINRT
41795WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
41796        LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
41797
41798WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
41799#else
41800#if defined(SQLITE_WIN32_HAS_ANSI)
41801WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
41802        DWORD, DWORD, DWORD, LPCSTR);
41803#endif /* defined(SQLITE_WIN32_HAS_ANSI) */
41804
41805#if defined(SQLITE_WIN32_HAS_WIDE)
41806WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
41807        DWORD, DWORD, DWORD, LPCWSTR);
41808#endif /* defined(SQLITE_WIN32_HAS_WIDE) */
41809
41810WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
41811#endif /* SQLITE_OS_WINRT */
41812
41813/*
41814** These file mapping APIs are common to both Win32 and WinRT.
41815*/
41816
41817WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T);
41818WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
41819#endif /* SQLITE_WIN32_FILEMAPPING_API */
41820
41821/*
41822** Some Microsoft compilers lack this definition.
41823*/
41824#ifndef INVALID_FILE_ATTRIBUTES
41825# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
41826#endif
41827
41828#ifndef FILE_FLAG_MASK
41829# define FILE_FLAG_MASK          (0xFF3C0000)
41830#endif
41831
41832#ifndef FILE_ATTRIBUTE_MASK
41833# define FILE_ATTRIBUTE_MASK     (0x0003FFF7)
41834#endif
41835
41836#ifndef SQLITE_OMIT_WAL
41837/* Forward references to structures used for WAL */
41838typedef struct winShm winShm;           /* A connection to shared-memory */
41839typedef struct winShmNode winShmNode;   /* A region of shared-memory */
41840#endif
41841
41842/*
41843** WinCE lacks native support for file locking so we have to fake it
41844** with some code of our own.
41845*/
41846#if SQLITE_OS_WINCE
41847typedef struct winceLock {
41848  int nReaders;       /* Number of reader locks obtained */
41849  BOOL bPending;      /* Indicates a pending lock has been obtained */
41850  BOOL bReserved;     /* Indicates a reserved lock has been obtained */
41851  BOOL bExclusive;    /* Indicates an exclusive lock has been obtained */
41852} winceLock;
41853#endif
41854
41855/*
41856** The winFile structure is a subclass of sqlite3_file* specific to the win32
41857** portability layer.
41858*/
41859typedef struct winFile winFile;
41860struct winFile {
41861  const sqlite3_io_methods *pMethod; /*** Must be first ***/
41862  sqlite3_vfs *pVfs;      /* The VFS used to open this file */
41863  HANDLE h;               /* Handle for accessing the file */
41864  u8 locktype;            /* Type of lock currently held on this file */
41865  short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
41866  u8 ctrlFlags;           /* Flags.  See WINFILE_* below */
41867  DWORD lastErrno;        /* The Windows errno from the last I/O error */
41868#ifndef SQLITE_OMIT_WAL
41869  winShm *pShm;           /* Instance of shared memory on this file */
41870#endif
41871  const char *zPath;      /* Full pathname of this file */
41872  int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
41873#if SQLITE_OS_WINCE
41874  LPWSTR zDeleteOnClose;  /* Name of file to delete when closing */
41875  HANDLE hMutex;          /* Mutex used to control access to shared lock */
41876  HANDLE hShared;         /* Shared memory segment used for locking */
41877  winceLock local;        /* Locks obtained by this instance of winFile */
41878  winceLock *shared;      /* Global shared lock memory for the file  */
41879#endif
41880#if SQLITE_MAX_MMAP_SIZE>0
41881  int nFetchOut;                /* Number of outstanding xFetch references */
41882  HANDLE hMap;                  /* Handle for accessing memory mapping */
41883  void *pMapRegion;             /* Area memory mapped */
41884  sqlite3_int64 mmapSize;       /* Size of mapped region */
41885  sqlite3_int64 mmapSizeMax;    /* Configured FCNTL_MMAP_SIZE value */
41886#endif
41887};
41888
41889/*
41890** The winVfsAppData structure is used for the pAppData member for all of the
41891** Win32 VFS variants.
41892*/
41893typedef struct winVfsAppData winVfsAppData;
41894struct winVfsAppData {
41895  const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */
41896  void *pAppData;                    /* The extra pAppData, if any. */
41897  BOOL bNoLock;                      /* Non-zero if locking is disabled. */
41898};
41899
41900/*
41901** Allowed values for winFile.ctrlFlags
41902*/
41903#define WINFILE_RDONLY          0x02   /* Connection is read only */
41904#define WINFILE_PERSIST_WAL     0x04   /* Persistent WAL mode */
41905#define WINFILE_PSOW            0x10   /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
41906
41907/*
41908 * The size of the buffer used by sqlite3_win32_write_debug().
41909 */
41910#ifndef SQLITE_WIN32_DBG_BUF_SIZE
41911#  define SQLITE_WIN32_DBG_BUF_SIZE   ((int)(4096-sizeof(DWORD)))
41912#endif
41913
41914/*
41915 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
41916 * various Win32 API heap functions instead of our own.
41917 */
41918#ifdef SQLITE_WIN32_MALLOC
41919
41920/*
41921 * If this is non-zero, an isolated heap will be created by the native Win32
41922 * allocator subsystem; otherwise, the default process heap will be used.  This
41923 * setting has no effect when compiling for WinRT.  By default, this is enabled
41924 * and an isolated heap will be created to store all allocated data.
41925 *
41926 ******************************************************************************
41927 * WARNING: It is important to note that when this setting is non-zero and the
41928 *          winMemShutdown function is called (e.g. by the sqlite3_shutdown
41929 *          function), all data that was allocated using the isolated heap will
41930 *          be freed immediately and any attempt to access any of that freed
41931 *          data will almost certainly result in an immediate access violation.
41932 ******************************************************************************
41933 */
41934#ifndef SQLITE_WIN32_HEAP_CREATE
41935#  define SQLITE_WIN32_HEAP_CREATE        (TRUE)
41936#endif
41937
41938/*
41939 * This is the maximum possible initial size of the Win32-specific heap, in
41940 * bytes.
41941 */
41942#ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE
41943#  define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U)
41944#endif
41945
41946/*
41947 * This is the extra space for the initial size of the Win32-specific heap,
41948 * in bytes.  This value may be zero.
41949 */
41950#ifndef SQLITE_WIN32_HEAP_INIT_EXTRA
41951#  define SQLITE_WIN32_HEAP_INIT_EXTRA  (4194304)
41952#endif
41953
41954/*
41955 * Calculate the maximum legal cache size, in pages, based on the maximum
41956 * possible initial heap size and the default page size, setting aside the
41957 * needed extra space.
41958 */
41959#ifndef SQLITE_WIN32_MAX_CACHE_SIZE
41960#  define SQLITE_WIN32_MAX_CACHE_SIZE   (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
41961                                          (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \
41962                                         (SQLITE_DEFAULT_PAGE_SIZE))
41963#endif
41964
41965/*
41966 * This is cache size used in the calculation of the initial size of the
41967 * Win32-specific heap.  It cannot be negative.
41968 */
41969#ifndef SQLITE_WIN32_CACHE_SIZE
41970#  if SQLITE_DEFAULT_CACHE_SIZE>=0
41971#    define SQLITE_WIN32_CACHE_SIZE     (SQLITE_DEFAULT_CACHE_SIZE)
41972#  else
41973#    define SQLITE_WIN32_CACHE_SIZE     (-(SQLITE_DEFAULT_CACHE_SIZE))
41974#  endif
41975#endif
41976
41977/*
41978 * Make sure that the calculated cache size, in pages, cannot cause the
41979 * initial size of the Win32-specific heap to exceed the maximum amount
41980 * of memory that can be specified in the call to HeapCreate.
41981 */
41982#if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE
41983#  undef SQLITE_WIN32_CACHE_SIZE
41984#  define SQLITE_WIN32_CACHE_SIZE       (2000)
41985#endif
41986
41987/*
41988 * The initial size of the Win32-specific heap.  This value may be zero.
41989 */
41990#ifndef SQLITE_WIN32_HEAP_INIT_SIZE
41991#  define SQLITE_WIN32_HEAP_INIT_SIZE   ((SQLITE_WIN32_CACHE_SIZE) * \
41992                                         (SQLITE_DEFAULT_PAGE_SIZE) + \
41993                                         (SQLITE_WIN32_HEAP_INIT_EXTRA))
41994#endif
41995
41996/*
41997 * The maximum size of the Win32-specific heap.  This value may be zero.
41998 */
41999#ifndef SQLITE_WIN32_HEAP_MAX_SIZE
42000#  define SQLITE_WIN32_HEAP_MAX_SIZE    (0)
42001#endif
42002
42003/*
42004 * The extra flags to use in calls to the Win32 heap APIs.  This value may be
42005 * zero for the default behavior.
42006 */
42007#ifndef SQLITE_WIN32_HEAP_FLAGS
42008#  define SQLITE_WIN32_HEAP_FLAGS       (0)
42009#endif
42010
42011
42012/*
42013** The winMemData structure stores information required by the Win32-specific
42014** sqlite3_mem_methods implementation.
42015*/
42016typedef struct winMemData winMemData;
42017struct winMemData {
42018#ifndef NDEBUG
42019  u32 magic1;   /* Magic number to detect structure corruption. */
42020#endif
42021  HANDLE hHeap; /* The handle to our heap. */
42022  BOOL bOwned;  /* Do we own the heap (i.e. destroy it on shutdown)? */
42023#ifndef NDEBUG
42024  u32 magic2;   /* Magic number to detect structure corruption. */
42025#endif
42026};
42027
42028#ifndef NDEBUG
42029#define WINMEM_MAGIC1     0x42b2830b
42030#define WINMEM_MAGIC2     0xbd4d7cf4
42031#endif
42032
42033static struct winMemData win_mem_data = {
42034#ifndef NDEBUG
42035  WINMEM_MAGIC1,
42036#endif
42037  NULL, FALSE
42038#ifndef NDEBUG
42039  ,WINMEM_MAGIC2
42040#endif
42041};
42042
42043#ifndef NDEBUG
42044#define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )
42045#define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )
42046#define winMemAssertMagic()  winMemAssertMagic1(); winMemAssertMagic2();
42047#else
42048#define winMemAssertMagic()
42049#endif
42050
42051#define winMemGetDataPtr()  &win_mem_data
42052#define winMemGetHeap()     win_mem_data.hHeap
42053#define winMemGetOwned()    win_mem_data.bOwned
42054
42055static void *winMemMalloc(int nBytes);
42056static void winMemFree(void *pPrior);
42057static void *winMemRealloc(void *pPrior, int nBytes);
42058static int winMemSize(void *p);
42059static int winMemRoundup(int n);
42060static int winMemInit(void *pAppData);
42061static void winMemShutdown(void *pAppData);
42062
42063SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
42064#endif /* SQLITE_WIN32_MALLOC */
42065
42066/*
42067** The following variable is (normally) set once and never changes
42068** thereafter.  It records whether the operating system is Win9x
42069** or WinNT.
42070**
42071** 0:   Operating system unknown.
42072** 1:   Operating system is Win9x.
42073** 2:   Operating system is WinNT.
42074**
42075** In order to facilitate testing on a WinNT system, the test fixture
42076** can manually set this value to 1 to emulate Win98 behavior.
42077*/
42078#ifdef SQLITE_TEST
42079SQLITE_API LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
42080#else
42081static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
42082#endif
42083
42084#ifndef SYSCALL
42085#  define SYSCALL sqlite3_syscall_ptr
42086#endif
42087
42088/*
42089** This function is not available on Windows CE or WinRT.
42090 */
42091
42092#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
42093#  define osAreFileApisANSI()       1
42094#endif
42095
42096/*
42097** Many system calls are accessed through pointer-to-functions so that
42098** they may be overridden at runtime to facilitate fault injection during
42099** testing and sandboxing.  The following array holds the names and pointers
42100** to all overrideable system calls.
42101*/
42102static struct win_syscall {
42103  const char *zName;            /* Name of the system call */
42104  sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
42105  sqlite3_syscall_ptr pDefault; /* Default value */
42106} aSyscall[] = {
42107#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
42108  { "AreFileApisANSI",         (SYSCALL)AreFileApisANSI,         0 },
42109#else
42110  { "AreFileApisANSI",         (SYSCALL)0,                       0 },
42111#endif
42112
42113#ifndef osAreFileApisANSI
42114#define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
42115#endif
42116
42117#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
42118  { "CharLowerW",              (SYSCALL)CharLowerW,              0 },
42119#else
42120  { "CharLowerW",              (SYSCALL)0,                       0 },
42121#endif
42122
42123#define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
42124
42125#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
42126  { "CharUpperW",              (SYSCALL)CharUpperW,              0 },
42127#else
42128  { "CharUpperW",              (SYSCALL)0,                       0 },
42129#endif
42130
42131#define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
42132
42133  { "CloseHandle",             (SYSCALL)CloseHandle,             0 },
42134
42135#define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
42136
42137#if defined(SQLITE_WIN32_HAS_ANSI)
42138  { "CreateFileA",             (SYSCALL)CreateFileA,             0 },
42139#else
42140  { "CreateFileA",             (SYSCALL)0,                       0 },
42141#endif
42142
42143#define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
42144        LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
42145
42146#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
42147  { "CreateFileW",             (SYSCALL)CreateFileW,             0 },
42148#else
42149  { "CreateFileW",             (SYSCALL)0,                       0 },
42150#endif
42151
42152#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
42153        LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
42154
42155#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
42156        (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) && \
42157        SQLITE_WIN32_CREATEFILEMAPPINGA
42158  { "CreateFileMappingA",      (SYSCALL)CreateFileMappingA,      0 },
42159#else
42160  { "CreateFileMappingA",      (SYSCALL)0,                       0 },
42161#endif
42162
42163#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
42164        DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
42165
42166#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
42167        (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
42168  { "CreateFileMappingW",      (SYSCALL)CreateFileMappingW,      0 },
42169#else
42170  { "CreateFileMappingW",      (SYSCALL)0,                       0 },
42171#endif
42172
42173#define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
42174        DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
42175
42176#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
42177  { "CreateMutexW",            (SYSCALL)CreateMutexW,            0 },
42178#else
42179  { "CreateMutexW",            (SYSCALL)0,                       0 },
42180#endif
42181
42182#define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
42183        LPCWSTR))aSyscall[8].pCurrent)
42184
42185#if defined(SQLITE_WIN32_HAS_ANSI)
42186  { "DeleteFileA",             (SYSCALL)DeleteFileA,             0 },
42187#else
42188  { "DeleteFileA",             (SYSCALL)0,                       0 },
42189#endif
42190
42191#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
42192
42193#if defined(SQLITE_WIN32_HAS_WIDE)
42194  { "DeleteFileW",             (SYSCALL)DeleteFileW,             0 },
42195#else
42196  { "DeleteFileW",             (SYSCALL)0,                       0 },
42197#endif
42198
42199#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
42200
42201#if SQLITE_OS_WINCE
42202  { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
42203#else
42204  { "FileTimeToLocalFileTime", (SYSCALL)0,                       0 },
42205#endif
42206
42207#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
42208        LPFILETIME))aSyscall[11].pCurrent)
42209
42210#if SQLITE_OS_WINCE
42211  { "FileTimeToSystemTime",    (SYSCALL)FileTimeToSystemTime,    0 },
42212#else
42213  { "FileTimeToSystemTime",    (SYSCALL)0,                       0 },
42214#endif
42215
42216#define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
42217        LPSYSTEMTIME))aSyscall[12].pCurrent)
42218
42219  { "FlushFileBuffers",        (SYSCALL)FlushFileBuffers,        0 },
42220
42221#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
42222
42223#if defined(SQLITE_WIN32_HAS_ANSI)
42224  { "FormatMessageA",          (SYSCALL)FormatMessageA,          0 },
42225#else
42226  { "FormatMessageA",          (SYSCALL)0,                       0 },
42227#endif
42228
42229#define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
42230        DWORD,va_list*))aSyscall[14].pCurrent)
42231
42232#if defined(SQLITE_WIN32_HAS_WIDE)
42233  { "FormatMessageW",          (SYSCALL)FormatMessageW,          0 },
42234#else
42235  { "FormatMessageW",          (SYSCALL)0,                       0 },
42236#endif
42237
42238#define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
42239        DWORD,va_list*))aSyscall[15].pCurrent)
42240
42241#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
42242  { "FreeLibrary",             (SYSCALL)FreeLibrary,             0 },
42243#else
42244  { "FreeLibrary",             (SYSCALL)0,                       0 },
42245#endif
42246
42247#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
42248
42249  { "GetCurrentProcessId",     (SYSCALL)GetCurrentProcessId,     0 },
42250
42251#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
42252
42253#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
42254  { "GetDiskFreeSpaceA",       (SYSCALL)GetDiskFreeSpaceA,       0 },
42255#else
42256  { "GetDiskFreeSpaceA",       (SYSCALL)0,                       0 },
42257#endif
42258
42259#define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
42260        LPDWORD))aSyscall[18].pCurrent)
42261
42262#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
42263  { "GetDiskFreeSpaceW",       (SYSCALL)GetDiskFreeSpaceW,       0 },
42264#else
42265  { "GetDiskFreeSpaceW",       (SYSCALL)0,                       0 },
42266#endif
42267
42268#define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
42269        LPDWORD))aSyscall[19].pCurrent)
42270
42271#if defined(SQLITE_WIN32_HAS_ANSI)
42272  { "GetFileAttributesA",      (SYSCALL)GetFileAttributesA,      0 },
42273#else
42274  { "GetFileAttributesA",      (SYSCALL)0,                       0 },
42275#endif
42276
42277#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
42278
42279#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
42280  { "GetFileAttributesW",      (SYSCALL)GetFileAttributesW,      0 },
42281#else
42282  { "GetFileAttributesW",      (SYSCALL)0,                       0 },
42283#endif
42284
42285#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
42286
42287#if defined(SQLITE_WIN32_HAS_WIDE)
42288  { "GetFileAttributesExW",    (SYSCALL)GetFileAttributesExW,    0 },
42289#else
42290  { "GetFileAttributesExW",    (SYSCALL)0,                       0 },
42291#endif
42292
42293#define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
42294        LPVOID))aSyscall[22].pCurrent)
42295
42296#if !SQLITE_OS_WINRT
42297  { "GetFileSize",             (SYSCALL)GetFileSize,             0 },
42298#else
42299  { "GetFileSize",             (SYSCALL)0,                       0 },
42300#endif
42301
42302#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
42303
42304#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
42305  { "GetFullPathNameA",        (SYSCALL)GetFullPathNameA,        0 },
42306#else
42307  { "GetFullPathNameA",        (SYSCALL)0,                       0 },
42308#endif
42309
42310#define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
42311        LPSTR*))aSyscall[24].pCurrent)
42312
42313#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
42314  { "GetFullPathNameW",        (SYSCALL)GetFullPathNameW,        0 },
42315#else
42316  { "GetFullPathNameW",        (SYSCALL)0,                       0 },
42317#endif
42318
42319#define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
42320        LPWSTR*))aSyscall[25].pCurrent)
42321
42322  { "GetLastError",            (SYSCALL)GetLastError,            0 },
42323
42324#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
42325
42326#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
42327#if SQLITE_OS_WINCE
42328  /* The GetProcAddressA() routine is only available on Windows CE. */
42329  { "GetProcAddressA",         (SYSCALL)GetProcAddressA,         0 },
42330#else
42331  /* All other Windows platforms expect GetProcAddress() to take
42332  ** an ANSI string regardless of the _UNICODE setting */
42333  { "GetProcAddressA",         (SYSCALL)GetProcAddress,          0 },
42334#endif
42335#else
42336  { "GetProcAddressA",         (SYSCALL)0,                       0 },
42337#endif
42338
42339#define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
42340        LPCSTR))aSyscall[27].pCurrent)
42341
42342#if !SQLITE_OS_WINRT
42343  { "GetSystemInfo",           (SYSCALL)GetSystemInfo,           0 },
42344#else
42345  { "GetSystemInfo",           (SYSCALL)0,                       0 },
42346#endif
42347
42348#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
42349
42350  { "GetSystemTime",           (SYSCALL)GetSystemTime,           0 },
42351
42352#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
42353
42354#if !SQLITE_OS_WINCE
42355  { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
42356#else
42357  { "GetSystemTimeAsFileTime", (SYSCALL)0,                       0 },
42358#endif
42359
42360#define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
42361        LPFILETIME))aSyscall[30].pCurrent)
42362
42363#if defined(SQLITE_WIN32_HAS_ANSI)
42364  { "GetTempPathA",            (SYSCALL)GetTempPathA,            0 },
42365#else
42366  { "GetTempPathA",            (SYSCALL)0,                       0 },
42367#endif
42368
42369#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
42370
42371#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
42372  { "GetTempPathW",            (SYSCALL)GetTempPathW,            0 },
42373#else
42374  { "GetTempPathW",            (SYSCALL)0,                       0 },
42375#endif
42376
42377#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
42378
42379#if !SQLITE_OS_WINRT
42380  { "GetTickCount",            (SYSCALL)GetTickCount,            0 },
42381#else
42382  { "GetTickCount",            (SYSCALL)0,                       0 },
42383#endif
42384
42385#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
42386
42387#if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX
42388  { "GetVersionExA",           (SYSCALL)GetVersionExA,           0 },
42389#else
42390  { "GetVersionExA",           (SYSCALL)0,                       0 },
42391#endif
42392
42393#define osGetVersionExA ((BOOL(WINAPI*)( \
42394        LPOSVERSIONINFOA))aSyscall[34].pCurrent)
42395
42396#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
42397        SQLITE_WIN32_GETVERSIONEX
42398  { "GetVersionExW",           (SYSCALL)GetVersionExW,           0 },
42399#else
42400  { "GetVersionExW",           (SYSCALL)0,                       0 },
42401#endif
42402
42403#define osGetVersionExW ((BOOL(WINAPI*)( \
42404        LPOSVERSIONINFOW))aSyscall[35].pCurrent)
42405
42406  { "HeapAlloc",               (SYSCALL)HeapAlloc,               0 },
42407
42408#define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
42409        SIZE_T))aSyscall[36].pCurrent)
42410
42411#if !SQLITE_OS_WINRT
42412  { "HeapCreate",              (SYSCALL)HeapCreate,              0 },
42413#else
42414  { "HeapCreate",              (SYSCALL)0,                       0 },
42415#endif
42416
42417#define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
42418        SIZE_T))aSyscall[37].pCurrent)
42419
42420#if !SQLITE_OS_WINRT
42421  { "HeapDestroy",             (SYSCALL)HeapDestroy,             0 },
42422#else
42423  { "HeapDestroy",             (SYSCALL)0,                       0 },
42424#endif
42425
42426#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
42427
42428  { "HeapFree",                (SYSCALL)HeapFree,                0 },
42429
42430#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
42431
42432  { "HeapReAlloc",             (SYSCALL)HeapReAlloc,             0 },
42433
42434#define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
42435        SIZE_T))aSyscall[40].pCurrent)
42436
42437  { "HeapSize",                (SYSCALL)HeapSize,                0 },
42438
42439#define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
42440        LPCVOID))aSyscall[41].pCurrent)
42441
42442#if !SQLITE_OS_WINRT
42443  { "HeapValidate",            (SYSCALL)HeapValidate,            0 },
42444#else
42445  { "HeapValidate",            (SYSCALL)0,                       0 },
42446#endif
42447
42448#define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
42449        LPCVOID))aSyscall[42].pCurrent)
42450
42451#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
42452  { "HeapCompact",             (SYSCALL)HeapCompact,             0 },
42453#else
42454  { "HeapCompact",             (SYSCALL)0,                       0 },
42455#endif
42456
42457#define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
42458
42459#if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
42460  { "LoadLibraryA",            (SYSCALL)LoadLibraryA,            0 },
42461#else
42462  { "LoadLibraryA",            (SYSCALL)0,                       0 },
42463#endif
42464
42465#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
42466
42467#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
42468        !defined(SQLITE_OMIT_LOAD_EXTENSION)
42469  { "LoadLibraryW",            (SYSCALL)LoadLibraryW,            0 },
42470#else
42471  { "LoadLibraryW",            (SYSCALL)0,                       0 },
42472#endif
42473
42474#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
42475
42476#if !SQLITE_OS_WINRT
42477  { "LocalFree",               (SYSCALL)LocalFree,               0 },
42478#else
42479  { "LocalFree",               (SYSCALL)0,                       0 },
42480#endif
42481
42482#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
42483
42484#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
42485  { "LockFile",                (SYSCALL)LockFile,                0 },
42486#else
42487  { "LockFile",                (SYSCALL)0,                       0 },
42488#endif
42489
42490#ifndef osLockFile
42491#define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
42492        DWORD))aSyscall[47].pCurrent)
42493#endif
42494
42495#if !SQLITE_OS_WINCE
42496  { "LockFileEx",              (SYSCALL)LockFileEx,              0 },
42497#else
42498  { "LockFileEx",              (SYSCALL)0,                       0 },
42499#endif
42500
42501#ifndef osLockFileEx
42502#define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
42503        LPOVERLAPPED))aSyscall[48].pCurrent)
42504#endif
42505
42506#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
42507        (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
42508  { "MapViewOfFile",           (SYSCALL)MapViewOfFile,           0 },
42509#else
42510  { "MapViewOfFile",           (SYSCALL)0,                       0 },
42511#endif
42512
42513#define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
42514        SIZE_T))aSyscall[49].pCurrent)
42515
42516  { "MultiByteToWideChar",     (SYSCALL)MultiByteToWideChar,     0 },
42517
42518#define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
42519        int))aSyscall[50].pCurrent)
42520
42521  { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
42522
42523#define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
42524        LARGE_INTEGER*))aSyscall[51].pCurrent)
42525
42526  { "ReadFile",                (SYSCALL)ReadFile,                0 },
42527
42528#define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
42529        LPOVERLAPPED))aSyscall[52].pCurrent)
42530
42531  { "SetEndOfFile",            (SYSCALL)SetEndOfFile,            0 },
42532
42533#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
42534
42535#if !SQLITE_OS_WINRT
42536  { "SetFilePointer",          (SYSCALL)SetFilePointer,          0 },
42537#else
42538  { "SetFilePointer",          (SYSCALL)0,                       0 },
42539#endif
42540
42541#define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
42542        DWORD))aSyscall[54].pCurrent)
42543
42544#if !SQLITE_OS_WINRT
42545  { "Sleep",                   (SYSCALL)Sleep,                   0 },
42546#else
42547  { "Sleep",                   (SYSCALL)0,                       0 },
42548#endif
42549
42550#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
42551
42552  { "SystemTimeToFileTime",    (SYSCALL)SystemTimeToFileTime,    0 },
42553
42554#define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
42555        LPFILETIME))aSyscall[56].pCurrent)
42556
42557#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
42558  { "UnlockFile",              (SYSCALL)UnlockFile,              0 },
42559#else
42560  { "UnlockFile",              (SYSCALL)0,                       0 },
42561#endif
42562
42563#ifndef osUnlockFile
42564#define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
42565        DWORD))aSyscall[57].pCurrent)
42566#endif
42567
42568#if !SQLITE_OS_WINCE
42569  { "UnlockFileEx",            (SYSCALL)UnlockFileEx,            0 },
42570#else
42571  { "UnlockFileEx",            (SYSCALL)0,                       0 },
42572#endif
42573
42574#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
42575        LPOVERLAPPED))aSyscall[58].pCurrent)
42576
42577#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
42578  { "UnmapViewOfFile",         (SYSCALL)UnmapViewOfFile,         0 },
42579#else
42580  { "UnmapViewOfFile",         (SYSCALL)0,                       0 },
42581#endif
42582
42583#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
42584
42585  { "WideCharToMultiByte",     (SYSCALL)WideCharToMultiByte,     0 },
42586
42587#define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
42588        LPCSTR,LPBOOL))aSyscall[60].pCurrent)
42589
42590  { "WriteFile",               (SYSCALL)WriteFile,               0 },
42591
42592#define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
42593        LPOVERLAPPED))aSyscall[61].pCurrent)
42594
42595#if SQLITE_OS_WINRT
42596  { "CreateEventExW",          (SYSCALL)CreateEventExW,          0 },
42597#else
42598  { "CreateEventExW",          (SYSCALL)0,                       0 },
42599#endif
42600
42601#define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
42602        DWORD,DWORD))aSyscall[62].pCurrent)
42603
42604#if !SQLITE_OS_WINRT
42605  { "WaitForSingleObject",     (SYSCALL)WaitForSingleObject,     0 },
42606#else
42607  { "WaitForSingleObject",     (SYSCALL)0,                       0 },
42608#endif
42609
42610#define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
42611        DWORD))aSyscall[63].pCurrent)
42612
42613#if !SQLITE_OS_WINCE
42614  { "WaitForSingleObjectEx",   (SYSCALL)WaitForSingleObjectEx,   0 },
42615#else
42616  { "WaitForSingleObjectEx",   (SYSCALL)0,                       0 },
42617#endif
42618
42619#define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
42620        BOOL))aSyscall[64].pCurrent)
42621
42622#if SQLITE_OS_WINRT
42623  { "SetFilePointerEx",        (SYSCALL)SetFilePointerEx,        0 },
42624#else
42625  { "SetFilePointerEx",        (SYSCALL)0,                       0 },
42626#endif
42627
42628#define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
42629        PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
42630
42631#if SQLITE_OS_WINRT
42632  { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
42633#else
42634  { "GetFileInformationByHandleEx", (SYSCALL)0,                  0 },
42635#endif
42636
42637#define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
42638        FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
42639
42640#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
42641  { "MapViewOfFileFromApp",    (SYSCALL)MapViewOfFileFromApp,    0 },
42642#else
42643  { "MapViewOfFileFromApp",    (SYSCALL)0,                       0 },
42644#endif
42645
42646#define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
42647        SIZE_T))aSyscall[67].pCurrent)
42648
42649#if SQLITE_OS_WINRT
42650  { "CreateFile2",             (SYSCALL)CreateFile2,             0 },
42651#else
42652  { "CreateFile2",             (SYSCALL)0,                       0 },
42653#endif
42654
42655#define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
42656        LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
42657
42658#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
42659  { "LoadPackagedLibrary",     (SYSCALL)LoadPackagedLibrary,     0 },
42660#else
42661  { "LoadPackagedLibrary",     (SYSCALL)0,                       0 },
42662#endif
42663
42664#define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
42665        DWORD))aSyscall[69].pCurrent)
42666
42667#if SQLITE_OS_WINRT
42668  { "GetTickCount64",          (SYSCALL)GetTickCount64,          0 },
42669#else
42670  { "GetTickCount64",          (SYSCALL)0,                       0 },
42671#endif
42672
42673#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
42674
42675#if SQLITE_OS_WINRT
42676  { "GetNativeSystemInfo",     (SYSCALL)GetNativeSystemInfo,     0 },
42677#else
42678  { "GetNativeSystemInfo",     (SYSCALL)0,                       0 },
42679#endif
42680
42681#define osGetNativeSystemInfo ((VOID(WINAPI*)( \
42682        LPSYSTEM_INFO))aSyscall[71].pCurrent)
42683
42684#if defined(SQLITE_WIN32_HAS_ANSI)
42685  { "OutputDebugStringA",      (SYSCALL)OutputDebugStringA,      0 },
42686#else
42687  { "OutputDebugStringA",      (SYSCALL)0,                       0 },
42688#endif
42689
42690#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
42691
42692#if defined(SQLITE_WIN32_HAS_WIDE)
42693  { "OutputDebugStringW",      (SYSCALL)OutputDebugStringW,      0 },
42694#else
42695  { "OutputDebugStringW",      (SYSCALL)0,                       0 },
42696#endif
42697
42698#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
42699
42700  { "GetProcessHeap",          (SYSCALL)GetProcessHeap,          0 },
42701
42702#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
42703
42704#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
42705  { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
42706#else
42707  { "CreateFileMappingFromApp", (SYSCALL)0,                      0 },
42708#endif
42709
42710#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
42711        LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
42712
42713/*
42714** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
42715**       is really just a macro that uses a compiler intrinsic (e.g. x64).
42716**       So do not try to make this is into a redefinable interface.
42717*/
42718#if defined(InterlockedCompareExchange)
42719  { "InterlockedCompareExchange", (SYSCALL)0,                    0 },
42720
42721#define osInterlockedCompareExchange InterlockedCompareExchange
42722#else
42723  { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
42724
42725#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \
42726        SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent)
42727#endif /* defined(InterlockedCompareExchange) */
42728
42729#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
42730  { "UuidCreate",               (SYSCALL)UuidCreate,             0 },
42731#else
42732  { "UuidCreate",               (SYSCALL)0,                      0 },
42733#endif
42734
42735#define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent)
42736
42737#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
42738  { "UuidCreateSequential",     (SYSCALL)UuidCreateSequential,   0 },
42739#else
42740  { "UuidCreateSequential",     (SYSCALL)0,                      0 },
42741#endif
42742
42743#define osUuidCreateSequential \
42744        ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent)
42745
42746#if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE>0
42747  { "FlushViewOfFile",          (SYSCALL)FlushViewOfFile,        0 },
42748#else
42749  { "FlushViewOfFile",          (SYSCALL)0,                      0 },
42750#endif
42751
42752#define osFlushViewOfFile \
42753        ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
42754
42755}; /* End of the overrideable system calls */
42756
42757/*
42758** This is the xSetSystemCall() method of sqlite3_vfs for all of the
42759** "win32" VFSes.  Return SQLITE_OK opon successfully updating the
42760** system call pointer, or SQLITE_NOTFOUND if there is no configurable
42761** system call named zName.
42762*/
42763static int winSetSystemCall(
42764  sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
42765  const char *zName,            /* Name of system call to override */
42766  sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
42767){
42768  unsigned int i;
42769  int rc = SQLITE_NOTFOUND;
42770
42771  UNUSED_PARAMETER(pNotUsed);
42772  if( zName==0 ){
42773    /* If no zName is given, restore all system calls to their default
42774    ** settings and return NULL
42775    */
42776    rc = SQLITE_OK;
42777    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
42778      if( aSyscall[i].pDefault ){
42779        aSyscall[i].pCurrent = aSyscall[i].pDefault;
42780      }
42781    }
42782  }else{
42783    /* If zName is specified, operate on only the one system call
42784    ** specified.
42785    */
42786    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
42787      if( strcmp(zName, aSyscall[i].zName)==0 ){
42788        if( aSyscall[i].pDefault==0 ){
42789          aSyscall[i].pDefault = aSyscall[i].pCurrent;
42790        }
42791        rc = SQLITE_OK;
42792        if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
42793        aSyscall[i].pCurrent = pNewFunc;
42794        break;
42795      }
42796    }
42797  }
42798  return rc;
42799}
42800
42801/*
42802** Return the value of a system call.  Return NULL if zName is not a
42803** recognized system call name.  NULL is also returned if the system call
42804** is currently undefined.
42805*/
42806static sqlite3_syscall_ptr winGetSystemCall(
42807  sqlite3_vfs *pNotUsed,
42808  const char *zName
42809){
42810  unsigned int i;
42811
42812  UNUSED_PARAMETER(pNotUsed);
42813  for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
42814    if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
42815  }
42816  return 0;
42817}
42818
42819/*
42820** Return the name of the first system call after zName.  If zName==NULL
42821** then return the name of the first system call.  Return NULL if zName
42822** is the last system call or if zName is not the name of a valid
42823** system call.
42824*/
42825static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
42826  int i = -1;
42827
42828  UNUSED_PARAMETER(p);
42829  if( zName ){
42830    for(i=0; i<ArraySize(aSyscall)-1; i++){
42831      if( strcmp(zName, aSyscall[i].zName)==0 ) break;
42832    }
42833  }
42834  for(i++; i<ArraySize(aSyscall); i++){
42835    if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
42836  }
42837  return 0;
42838}
42839
42840#ifdef SQLITE_WIN32_MALLOC
42841/*
42842** If a Win32 native heap has been configured, this function will attempt to
42843** compact it.  Upon success, SQLITE_OK will be returned.  Upon failure, one
42844** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned.  The
42845** "pnLargest" argument, if non-zero, will be used to return the size of the
42846** largest committed free block in the heap, in bytes.
42847*/
42848SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
42849  int rc = SQLITE_OK;
42850  UINT nLargest = 0;
42851  HANDLE hHeap;
42852
42853  winMemAssertMagic();
42854  hHeap = winMemGetHeap();
42855  assert( hHeap!=0 );
42856  assert( hHeap!=INVALID_HANDLE_VALUE );
42857#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
42858  assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
42859#endif
42860#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
42861  if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
42862    DWORD lastErrno = osGetLastError();
42863    if( lastErrno==NO_ERROR ){
42864      sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
42865                  (void*)hHeap);
42866      rc = SQLITE_NOMEM_BKPT;
42867    }else{
42868      sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
42869                  osGetLastError(), (void*)hHeap);
42870      rc = SQLITE_ERROR;
42871    }
42872  }
42873#else
42874  sqlite3_log(SQLITE_NOTFOUND, "failed to HeapCompact, heap=%p",
42875              (void*)hHeap);
42876  rc = SQLITE_NOTFOUND;
42877#endif
42878  if( pnLargest ) *pnLargest = nLargest;
42879  return rc;
42880}
42881
42882/*
42883** If a Win32 native heap has been configured, this function will attempt to
42884** destroy and recreate it.  If the Win32 native heap is not isolated and/or
42885** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
42886** be returned and no changes will be made to the Win32 native heap.
42887*/
42888SQLITE_API int sqlite3_win32_reset_heap(){
42889  int rc;
42890  MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
42891  MUTEX_LOGIC( sqlite3_mutex *pMem; )    /* The memsys static mutex */
42892  MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
42893  MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )
42894  sqlite3_mutex_enter(pMaster);
42895  sqlite3_mutex_enter(pMem);
42896  winMemAssertMagic();
42897  if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){
42898    /*
42899    ** At this point, there should be no outstanding memory allocations on
42900    ** the heap.  Also, since both the master and memsys locks are currently
42901    ** being held by us, no other function (i.e. from another thread) should
42902    ** be able to even access the heap.  Attempt to destroy and recreate our
42903    ** isolated Win32 native heap now.
42904    */
42905    assert( winMemGetHeap()!=NULL );
42906    assert( winMemGetOwned() );
42907    assert( sqlite3_memory_used()==0 );
42908    winMemShutdown(winMemGetDataPtr());
42909    assert( winMemGetHeap()==NULL );
42910    assert( !winMemGetOwned() );
42911    assert( sqlite3_memory_used()==0 );
42912    rc = winMemInit(winMemGetDataPtr());
42913    assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL );
42914    assert( rc!=SQLITE_OK || winMemGetOwned() );
42915    assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 );
42916  }else{
42917    /*
42918    ** The Win32 native heap cannot be modified because it may be in use.
42919    */
42920    rc = SQLITE_BUSY;
42921  }
42922  sqlite3_mutex_leave(pMem);
42923  sqlite3_mutex_leave(pMaster);
42924  return rc;
42925}
42926#endif /* SQLITE_WIN32_MALLOC */
42927
42928/*
42929** This function outputs the specified (ANSI) string to the Win32 debugger
42930** (if available).
42931*/
42932
42933SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
42934  char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
42935  int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
42936  if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
42937  assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
42938#ifdef SQLITE_ENABLE_API_ARMOR
42939  if( !zBuf ){
42940    (void)SQLITE_MISUSE_BKPT;
42941    return;
42942  }
42943#endif
42944#if defined(SQLITE_WIN32_HAS_ANSI)
42945  if( nMin>0 ){
42946    memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
42947    memcpy(zDbgBuf, zBuf, nMin);
42948    osOutputDebugStringA(zDbgBuf);
42949  }else{
42950    osOutputDebugStringA(zBuf);
42951  }
42952#elif defined(SQLITE_WIN32_HAS_WIDE)
42953  memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
42954  if ( osMultiByteToWideChar(
42955          osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
42956          nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
42957    return;
42958  }
42959  osOutputDebugStringW((LPCWSTR)zDbgBuf);
42960#else
42961  if( nMin>0 ){
42962    memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
42963    memcpy(zDbgBuf, zBuf, nMin);
42964    fprintf(stderr, "%s", zDbgBuf);
42965  }else{
42966    fprintf(stderr, "%s", zBuf);
42967  }
42968#endif
42969}
42970
42971/*
42972** The following routine suspends the current thread for at least ms
42973** milliseconds.  This is equivalent to the Win32 Sleep() interface.
42974*/
42975#if SQLITE_OS_WINRT
42976static HANDLE sleepObj = NULL;
42977#endif
42978
42979SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
42980#if SQLITE_OS_WINRT
42981  if ( sleepObj==NULL ){
42982    sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET,
42983                                SYNCHRONIZE);
42984  }
42985  assert( sleepObj!=NULL );
42986  osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
42987#else
42988  osSleep(milliseconds);
42989#endif
42990}
42991
42992#if SQLITE_MAX_WORKER_THREADS>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
42993        SQLITE_THREADSAFE>0
42994SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject){
42995  DWORD rc;
42996  while( (rc = osWaitForSingleObjectEx(hObject, INFINITE,
42997                                       TRUE))==WAIT_IO_COMPLETION ){}
42998  return rc;
42999}
43000#endif
43001
43002/*
43003** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
43004** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
43005**
43006** Here is an interesting observation:  Win95, Win98, and WinME lack
43007** the LockFileEx() API.  But we can still statically link against that
43008** API as long as we don't call it when running Win95/98/ME.  A call to
43009** this routine is used to determine if the host is Win95/98/ME or
43010** WinNT/2K/XP so that we will know whether or not we can safely call
43011** the LockFileEx() API.
43012*/
43013
43014#if !SQLITE_WIN32_GETVERSIONEX
43015# define osIsNT()  (1)
43016#elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
43017# define osIsNT()  (1)
43018#elif !defined(SQLITE_WIN32_HAS_WIDE)
43019# define osIsNT()  (0)
43020#else
43021# define osIsNT()  ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
43022#endif
43023
43024/*
43025** This function determines if the machine is running a version of Windows
43026** based on the NT kernel.
43027*/
43028SQLITE_API int sqlite3_win32_is_nt(void){
43029#if SQLITE_OS_WINRT
43030  /*
43031  ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
43032  **       kernel.
43033  */
43034  return 1;
43035#elif SQLITE_WIN32_GETVERSIONEX
43036  if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
43037#if defined(SQLITE_WIN32_HAS_ANSI)
43038    OSVERSIONINFOA sInfo;
43039    sInfo.dwOSVersionInfoSize = sizeof(sInfo);
43040    osGetVersionExA(&sInfo);
43041    osInterlockedCompareExchange(&sqlite3_os_type,
43042        (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
43043#elif defined(SQLITE_WIN32_HAS_WIDE)
43044    OSVERSIONINFOW sInfo;
43045    sInfo.dwOSVersionInfoSize = sizeof(sInfo);
43046    osGetVersionExW(&sInfo);
43047    osInterlockedCompareExchange(&sqlite3_os_type,
43048        (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
43049#endif
43050  }
43051  return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
43052#elif SQLITE_TEST
43053  return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
43054#else
43055  /*
43056  ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
43057  **       deprecated are always assumed to be based on the NT kernel.
43058  */
43059  return 1;
43060#endif
43061}
43062
43063#ifdef SQLITE_WIN32_MALLOC
43064/*
43065** Allocate nBytes of memory.
43066*/
43067static void *winMemMalloc(int nBytes){
43068  HANDLE hHeap;
43069  void *p;
43070
43071  winMemAssertMagic();
43072  hHeap = winMemGetHeap();
43073  assert( hHeap!=0 );
43074  assert( hHeap!=INVALID_HANDLE_VALUE );
43075#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43076  assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
43077#endif
43078  assert( nBytes>=0 );
43079  p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
43080  if( !p ){
43081    sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p",
43082                nBytes, osGetLastError(), (void*)hHeap);
43083  }
43084  return p;
43085}
43086
43087/*
43088** Free memory.
43089*/
43090static void winMemFree(void *pPrior){
43091  HANDLE hHeap;
43092
43093  winMemAssertMagic();
43094  hHeap = winMemGetHeap();
43095  assert( hHeap!=0 );
43096  assert( hHeap!=INVALID_HANDLE_VALUE );
43097#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43098  assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
43099#endif
43100  if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
43101  if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
43102    sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p",
43103                pPrior, osGetLastError(), (void*)hHeap);
43104  }
43105}
43106
43107/*
43108** Change the size of an existing memory allocation
43109*/
43110static void *winMemRealloc(void *pPrior, int nBytes){
43111  HANDLE hHeap;
43112  void *p;
43113
43114  winMemAssertMagic();
43115  hHeap = winMemGetHeap();
43116  assert( hHeap!=0 );
43117  assert( hHeap!=INVALID_HANDLE_VALUE );
43118#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43119  assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
43120#endif
43121  assert( nBytes>=0 );
43122  if( !pPrior ){
43123    p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
43124  }else{
43125    p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
43126  }
43127  if( !p ){
43128    sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%lu), heap=%p",
43129                pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
43130                (void*)hHeap);
43131  }
43132  return p;
43133}
43134
43135/*
43136** Return the size of an outstanding allocation, in bytes.
43137*/
43138static int winMemSize(void *p){
43139  HANDLE hHeap;
43140  SIZE_T n;
43141
43142  winMemAssertMagic();
43143  hHeap = winMemGetHeap();
43144  assert( hHeap!=0 );
43145  assert( hHeap!=INVALID_HANDLE_VALUE );
43146#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43147  assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) );
43148#endif
43149  if( !p ) return 0;
43150  n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
43151  if( n==(SIZE_T)-1 ){
43152    sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p",
43153                p, osGetLastError(), (void*)hHeap);
43154    return 0;
43155  }
43156  return (int)n;
43157}
43158
43159/*
43160** Round up a request size to the next valid allocation size.
43161*/
43162static int winMemRoundup(int n){
43163  return n;
43164}
43165
43166/*
43167** Initialize this module.
43168*/
43169static int winMemInit(void *pAppData){
43170  winMemData *pWinMemData = (winMemData *)pAppData;
43171
43172  if( !pWinMemData ) return SQLITE_ERROR;
43173  assert( pWinMemData->magic1==WINMEM_MAGIC1 );
43174  assert( pWinMemData->magic2==WINMEM_MAGIC2 );
43175
43176#if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
43177  if( !pWinMemData->hHeap ){
43178    DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
43179    DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap;
43180    if( dwMaximumSize==0 ){
43181      dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
43182    }else if( dwInitialSize>dwMaximumSize ){
43183      dwInitialSize = dwMaximumSize;
43184    }
43185    pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
43186                                      dwInitialSize, dwMaximumSize);
43187    if( !pWinMemData->hHeap ){
43188      sqlite3_log(SQLITE_NOMEM,
43189          "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
43190          osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
43191          dwMaximumSize);
43192      return SQLITE_NOMEM_BKPT;
43193    }
43194    pWinMemData->bOwned = TRUE;
43195    assert( pWinMemData->bOwned );
43196  }
43197#else
43198  pWinMemData->hHeap = osGetProcessHeap();
43199  if( !pWinMemData->hHeap ){
43200    sqlite3_log(SQLITE_NOMEM,
43201        "failed to GetProcessHeap (%lu)", osGetLastError());
43202    return SQLITE_NOMEM_BKPT;
43203  }
43204  pWinMemData->bOwned = FALSE;
43205  assert( !pWinMemData->bOwned );
43206#endif
43207  assert( pWinMemData->hHeap!=0 );
43208  assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
43209#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43210  assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
43211#endif
43212  return SQLITE_OK;
43213}
43214
43215/*
43216** Deinitialize this module.
43217*/
43218static void winMemShutdown(void *pAppData){
43219  winMemData *pWinMemData = (winMemData *)pAppData;
43220
43221  if( !pWinMemData ) return;
43222  assert( pWinMemData->magic1==WINMEM_MAGIC1 );
43223  assert( pWinMemData->magic2==WINMEM_MAGIC2 );
43224
43225  if( pWinMemData->hHeap ){
43226    assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
43227#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43228    assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
43229#endif
43230    if( pWinMemData->bOwned ){
43231      if( !osHeapDestroy(pWinMemData->hHeap) ){
43232        sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%lu), heap=%p",
43233                    osGetLastError(), (void*)pWinMemData->hHeap);
43234      }
43235      pWinMemData->bOwned = FALSE;
43236    }
43237    pWinMemData->hHeap = NULL;
43238  }
43239}
43240
43241/*
43242** Populate the low-level memory allocation function pointers in
43243** sqlite3GlobalConfig.m with pointers to the routines in this file. The
43244** arguments specify the block of memory to manage.
43245**
43246** This routine is only called by sqlite3_config(), and therefore
43247** is not required to be threadsafe (it is not).
43248*/
43249SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
43250  static const sqlite3_mem_methods winMemMethods = {
43251    winMemMalloc,
43252    winMemFree,
43253    winMemRealloc,
43254    winMemSize,
43255    winMemRoundup,
43256    winMemInit,
43257    winMemShutdown,
43258    &win_mem_data
43259  };
43260  return &winMemMethods;
43261}
43262
43263SQLITE_PRIVATE void sqlite3MemSetDefault(void){
43264  sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
43265}
43266#endif /* SQLITE_WIN32_MALLOC */
43267
43268/*
43269** Convert a UTF-8 string to Microsoft Unicode.
43270**
43271** Space to hold the returned string is obtained from sqlite3_malloc().
43272*/
43273static LPWSTR winUtf8ToUnicode(const char *zText){
43274  int nChar;
43275  LPWSTR zWideText;
43276
43277  nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
43278  if( nChar==0 ){
43279    return 0;
43280  }
43281  zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
43282  if( zWideText==0 ){
43283    return 0;
43284  }
43285  nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
43286                                nChar);
43287  if( nChar==0 ){
43288    sqlite3_free(zWideText);
43289    zWideText = 0;
43290  }
43291  return zWideText;
43292}
43293
43294/*
43295** Convert a Microsoft Unicode string to UTF-8.
43296**
43297** Space to hold the returned string is obtained from sqlite3_malloc().
43298*/
43299static char *winUnicodeToUtf8(LPCWSTR zWideText){
43300  int nByte;
43301  char *zText;
43302
43303  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
43304  if( nByte == 0 ){
43305    return 0;
43306  }
43307  zText = sqlite3MallocZero( nByte );
43308  if( zText==0 ){
43309    return 0;
43310  }
43311  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
43312                                0, 0);
43313  if( nByte == 0 ){
43314    sqlite3_free(zText);
43315    zText = 0;
43316  }
43317  return zText;
43318}
43319
43320/*
43321** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
43322** code page.
43323**
43324** Space to hold the returned string is obtained from sqlite3_malloc().
43325*/
43326static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
43327  int nByte;
43328  LPWSTR zMbcsText;
43329  int codepage = useAnsi ? CP_ACP : CP_OEMCP;
43330
43331  nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
43332                                0)*sizeof(WCHAR);
43333  if( nByte==0 ){
43334    return 0;
43335  }
43336  zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
43337  if( zMbcsText==0 ){
43338    return 0;
43339  }
43340  nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
43341                                nByte);
43342  if( nByte==0 ){
43343    sqlite3_free(zMbcsText);
43344    zMbcsText = 0;
43345  }
43346  return zMbcsText;
43347}
43348
43349/*
43350** Convert a Microsoft Unicode string to a multi-byte character string,
43351** using the ANSI or OEM code page.
43352**
43353** Space to hold the returned string is obtained from sqlite3_malloc().
43354*/
43355static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
43356  int nByte;
43357  char *zText;
43358  int codepage = useAnsi ? CP_ACP : CP_OEMCP;
43359
43360  nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
43361  if( nByte == 0 ){
43362    return 0;
43363  }
43364  zText = sqlite3MallocZero( nByte );
43365  if( zText==0 ){
43366    return 0;
43367  }
43368  nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
43369                                nByte, 0, 0);
43370  if( nByte == 0 ){
43371    sqlite3_free(zText);
43372    zText = 0;
43373  }
43374  return zText;
43375}
43376
43377/*
43378** Convert a multi-byte character string to UTF-8.
43379**
43380** Space to hold the returned string is obtained from sqlite3_malloc().
43381*/
43382static char *winMbcsToUtf8(const char *zText, int useAnsi){
43383  char *zTextUtf8;
43384  LPWSTR zTmpWide;
43385
43386  zTmpWide = winMbcsToUnicode(zText, useAnsi);
43387  if( zTmpWide==0 ){
43388    return 0;
43389  }
43390  zTextUtf8 = winUnicodeToUtf8(zTmpWide);
43391  sqlite3_free(zTmpWide);
43392  return zTextUtf8;
43393}
43394
43395/*
43396** Convert a UTF-8 string to a multi-byte character string.
43397**
43398** Space to hold the returned string is obtained from sqlite3_malloc().
43399*/
43400static char *winUtf8ToMbcs(const char *zText, int useAnsi){
43401  char *zTextMbcs;
43402  LPWSTR zTmpWide;
43403
43404  zTmpWide = winUtf8ToUnicode(zText);
43405  if( zTmpWide==0 ){
43406    return 0;
43407  }
43408  zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
43409  sqlite3_free(zTmpWide);
43410  return zTextMbcs;
43411}
43412
43413/*
43414** This is a public wrapper for the winUtf8ToUnicode() function.
43415*/
43416SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){
43417#ifdef SQLITE_ENABLE_API_ARMOR
43418  if( !zText ){
43419    (void)SQLITE_MISUSE_BKPT;
43420    return 0;
43421  }
43422#endif
43423#ifndef SQLITE_OMIT_AUTOINIT
43424  if( sqlite3_initialize() ) return 0;
43425#endif
43426  return winUtf8ToUnicode(zText);
43427}
43428
43429/*
43430** This is a public wrapper for the winUnicodeToUtf8() function.
43431*/
43432SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
43433#ifdef SQLITE_ENABLE_API_ARMOR
43434  if( !zWideText ){
43435    (void)SQLITE_MISUSE_BKPT;
43436    return 0;
43437  }
43438#endif
43439#ifndef SQLITE_OMIT_AUTOINIT
43440  if( sqlite3_initialize() ) return 0;
43441#endif
43442  return winUnicodeToUtf8(zWideText);
43443}
43444
43445/*
43446** This is a public wrapper for the winMbcsToUtf8() function.
43447*/
43448SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zText){
43449#ifdef SQLITE_ENABLE_API_ARMOR
43450  if( !zText ){
43451    (void)SQLITE_MISUSE_BKPT;
43452    return 0;
43453  }
43454#endif
43455#ifndef SQLITE_OMIT_AUTOINIT
43456  if( sqlite3_initialize() ) return 0;
43457#endif
43458  return winMbcsToUtf8(zText, osAreFileApisANSI());
43459}
43460
43461/*
43462** This is a public wrapper for the winMbcsToUtf8() function.
43463*/
43464SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
43465#ifdef SQLITE_ENABLE_API_ARMOR
43466  if( !zText ){
43467    (void)SQLITE_MISUSE_BKPT;
43468    return 0;
43469  }
43470#endif
43471#ifndef SQLITE_OMIT_AUTOINIT
43472  if( sqlite3_initialize() ) return 0;
43473#endif
43474  return winMbcsToUtf8(zText, useAnsi);
43475}
43476
43477/*
43478** This is a public wrapper for the winUtf8ToMbcs() function.
43479*/
43480SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zText){
43481#ifdef SQLITE_ENABLE_API_ARMOR
43482  if( !zText ){
43483    (void)SQLITE_MISUSE_BKPT;
43484    return 0;
43485  }
43486#endif
43487#ifndef SQLITE_OMIT_AUTOINIT
43488  if( sqlite3_initialize() ) return 0;
43489#endif
43490  return winUtf8ToMbcs(zText, osAreFileApisANSI());
43491}
43492
43493/*
43494** This is a public wrapper for the winUtf8ToMbcs() function.
43495*/
43496SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
43497#ifdef SQLITE_ENABLE_API_ARMOR
43498  if( !zText ){
43499    (void)SQLITE_MISUSE_BKPT;
43500    return 0;
43501  }
43502#endif
43503#ifndef SQLITE_OMIT_AUTOINIT
43504  if( sqlite3_initialize() ) return 0;
43505#endif
43506  return winUtf8ToMbcs(zText, useAnsi);
43507}
43508
43509/*
43510** This function is the same as sqlite3_win32_set_directory (below); however,
43511** it accepts a UTF-8 string.
43512*/
43513SQLITE_API int sqlite3_win32_set_directory8(
43514  unsigned long type, /* Identifier for directory being set or reset */
43515  const char *zValue  /* New value for directory being set or reset */
43516){
43517  char **ppDirectory = 0;
43518#ifndef SQLITE_OMIT_AUTOINIT
43519  int rc = sqlite3_initialize();
43520  if( rc ) return rc;
43521#endif
43522  if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
43523    ppDirectory = &sqlite3_data_directory;
43524  }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
43525    ppDirectory = &sqlite3_temp_directory;
43526  }
43527  assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
43528          || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
43529  );
43530  assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
43531  if( ppDirectory ){
43532    char *zCopy = 0;
43533    if( zValue && zValue[0] ){
43534      zCopy = sqlite3_mprintf("%s", zValue);
43535      if ( zCopy==0 ){
43536        return SQLITE_NOMEM_BKPT;
43537      }
43538    }
43539    sqlite3_free(*ppDirectory);
43540    *ppDirectory = zCopy;
43541    return SQLITE_OK;
43542  }
43543  return SQLITE_ERROR;
43544}
43545
43546/*
43547** This function is the same as sqlite3_win32_set_directory (below); however,
43548** it accepts a UTF-16 string.
43549*/
43550SQLITE_API int sqlite3_win32_set_directory16(
43551  unsigned long type, /* Identifier for directory being set or reset */
43552  const void *zValue  /* New value for directory being set or reset */
43553){
43554  int rc;
43555  char *zUtf8 = 0;
43556  if( zValue ){
43557    zUtf8 = sqlite3_win32_unicode_to_utf8(zValue);
43558    if( zUtf8==0 ) return SQLITE_NOMEM_BKPT;
43559  }
43560  rc = sqlite3_win32_set_directory8(type, zUtf8);
43561  if( zUtf8 ) sqlite3_free(zUtf8);
43562  return rc;
43563}
43564
43565/*
43566** This function sets the data directory or the temporary directory based on
43567** the provided arguments.  The type argument must be 1 in order to set the
43568** data directory or 2 in order to set the temporary directory.  The zValue
43569** argument is the name of the directory to use.  The return value will be
43570** SQLITE_OK if successful.
43571*/
43572SQLITE_API int sqlite3_win32_set_directory(
43573  unsigned long type, /* Identifier for directory being set or reset */
43574  void *zValue        /* New value for directory being set or reset */
43575){
43576  return sqlite3_win32_set_directory16(type, zValue);
43577}
43578
43579/*
43580** The return value of winGetLastErrorMsg
43581** is zero if the error message fits in the buffer, or non-zero
43582** otherwise (if the message was truncated).
43583*/
43584static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
43585  /* FormatMessage returns 0 on failure.  Otherwise it
43586  ** returns the number of TCHARs written to the output
43587  ** buffer, excluding the terminating null char.
43588  */
43589  DWORD dwLen = 0;
43590  char *zOut = 0;
43591
43592  if( osIsNT() ){
43593#if SQLITE_OS_WINRT
43594    WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
43595    dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
43596                             FORMAT_MESSAGE_IGNORE_INSERTS,
43597                             NULL,
43598                             lastErrno,
43599                             0,
43600                             zTempWide,
43601                             SQLITE_WIN32_MAX_ERRMSG_CHARS,
43602                             0);
43603#else
43604    LPWSTR zTempWide = NULL;
43605    dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
43606                             FORMAT_MESSAGE_FROM_SYSTEM |
43607                             FORMAT_MESSAGE_IGNORE_INSERTS,
43608                             NULL,
43609                             lastErrno,
43610                             0,
43611                             (LPWSTR) &zTempWide,
43612                             0,
43613                             0);
43614#endif
43615    if( dwLen > 0 ){
43616      /* allocate a buffer and convert to UTF8 */
43617      sqlite3BeginBenignMalloc();
43618      zOut = winUnicodeToUtf8(zTempWide);
43619      sqlite3EndBenignMalloc();
43620#if !SQLITE_OS_WINRT
43621      /* free the system buffer allocated by FormatMessage */
43622      osLocalFree(zTempWide);
43623#endif
43624    }
43625  }
43626#ifdef SQLITE_WIN32_HAS_ANSI
43627  else{
43628    char *zTemp = NULL;
43629    dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
43630                             FORMAT_MESSAGE_FROM_SYSTEM |
43631                             FORMAT_MESSAGE_IGNORE_INSERTS,
43632                             NULL,
43633                             lastErrno,
43634                             0,
43635                             (LPSTR) &zTemp,
43636                             0,
43637                             0);
43638    if( dwLen > 0 ){
43639      /* allocate a buffer and convert to UTF8 */
43640      sqlite3BeginBenignMalloc();
43641      zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
43642      sqlite3EndBenignMalloc();
43643      /* free the system buffer allocated by FormatMessage */
43644      osLocalFree(zTemp);
43645    }
43646  }
43647#endif
43648  if( 0 == dwLen ){
43649    sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
43650  }else{
43651    /* copy a maximum of nBuf chars to output buffer */
43652    sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
43653    /* free the UTF8 buffer */
43654    sqlite3_free(zOut);
43655  }
43656  return 0;
43657}
43658
43659/*
43660**
43661** This function - winLogErrorAtLine() - is only ever called via the macro
43662** winLogError().
43663**
43664** This routine is invoked after an error occurs in an OS function.
43665** It logs a message using sqlite3_log() containing the current value of
43666** error code and, if possible, the human-readable equivalent from
43667** FormatMessage.
43668**
43669** The first argument passed to the macro should be the error code that
43670** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
43671** The two subsequent arguments should be the name of the OS function that
43672** failed and the associated file-system path, if any.
43673*/
43674#define winLogError(a,b,c,d)   winLogErrorAtLine(a,b,c,d,__LINE__)
43675static int winLogErrorAtLine(
43676  int errcode,                    /* SQLite error code */
43677  DWORD lastErrno,                /* Win32 last error */
43678  const char *zFunc,              /* Name of OS function that failed */
43679  const char *zPath,              /* File path associated with error */
43680  int iLine                       /* Source line number where error occurred */
43681){
43682  char zMsg[500];                 /* Human readable error text */
43683  int i;                          /* Loop counter */
43684
43685  zMsg[0] = 0;
43686  winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
43687  assert( errcode!=SQLITE_OK );
43688  if( zPath==0 ) zPath = "";
43689  for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
43690  zMsg[i] = 0;
43691  sqlite3_log(errcode,
43692      "os_win.c:%d: (%lu) %s(%s) - %s",
43693      iLine, lastErrno, zFunc, zPath, zMsg
43694  );
43695
43696  return errcode;
43697}
43698
43699/*
43700** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
43701** will be retried following a locking error - probably caused by
43702** antivirus software.  Also the initial delay before the first retry.
43703** The delay increases linearly with each retry.
43704*/
43705#ifndef SQLITE_WIN32_IOERR_RETRY
43706# define SQLITE_WIN32_IOERR_RETRY 10
43707#endif
43708#ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
43709# define SQLITE_WIN32_IOERR_RETRY_DELAY 25
43710#endif
43711static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
43712static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
43713
43714/*
43715** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
43716** error code obtained via GetLastError() is eligible to be retried.  It
43717** must accept the error code DWORD as its only argument and should return
43718** non-zero if the error code is transient in nature and the operation
43719** responsible for generating the original error might succeed upon being
43720** retried.  The argument to this macro should be a variable.
43721**
43722** Additionally, a macro named "winIoerrCanRetry2" may be defined.  If it
43723** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
43724** returns zero.  The "winIoerrCanRetry2" macro is completely optional and
43725** may be used to include additional error codes in the set that should
43726** result in the failing I/O operation being retried by the caller.  If
43727** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
43728** identical to those of the "winIoerrCanRetry1" macro.
43729*/
43730#if !defined(winIoerrCanRetry1)
43731#define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED)        || \
43732                              ((a)==ERROR_SHARING_VIOLATION)    || \
43733                              ((a)==ERROR_LOCK_VIOLATION)       || \
43734                              ((a)==ERROR_DEV_NOT_EXIST)        || \
43735                              ((a)==ERROR_NETNAME_DELETED)      || \
43736                              ((a)==ERROR_SEM_TIMEOUT)          || \
43737                              ((a)==ERROR_NETWORK_UNREACHABLE))
43738#endif
43739
43740/*
43741** If a ReadFile() or WriteFile() error occurs, invoke this routine
43742** to see if it should be retried.  Return TRUE to retry.  Return FALSE
43743** to give up with an error.
43744*/
43745static int winRetryIoerr(int *pnRetry, DWORD *pError){
43746  DWORD e = osGetLastError();
43747  if( *pnRetry>=winIoerrRetry ){
43748    if( pError ){
43749      *pError = e;
43750    }
43751    return 0;
43752  }
43753  if( winIoerrCanRetry1(e) ){
43754    sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
43755    ++*pnRetry;
43756    return 1;
43757  }
43758#if defined(winIoerrCanRetry2)
43759  else if( winIoerrCanRetry2(e) ){
43760    sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
43761    ++*pnRetry;
43762    return 1;
43763  }
43764#endif
43765  if( pError ){
43766    *pError = e;
43767  }
43768  return 0;
43769}
43770
43771/*
43772** Log a I/O error retry episode.
43773*/
43774static void winLogIoerr(int nRetry, int lineno){
43775  if( nRetry ){
43776    sqlite3_log(SQLITE_NOTICE,
43777      "delayed %dms for lock/sharing conflict at line %d",
43778      winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno
43779    );
43780  }
43781}
43782
43783/*
43784** This #if does not rely on the SQLITE_OS_WINCE define because the
43785** corresponding section in "date.c" cannot use it.
43786*/
43787#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
43788    (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
43789/*
43790** The MSVC CRT on Windows CE may not have a localtime() function.
43791** So define a substitute.
43792*/
43793/* #  include <time.h> */
43794struct tm *__cdecl localtime(const time_t *t)
43795{
43796  static struct tm y;
43797  FILETIME uTm, lTm;
43798  SYSTEMTIME pTm;
43799  sqlite3_int64 t64;
43800  t64 = *t;
43801  t64 = (t64 + 11644473600)*10000000;
43802  uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
43803  uTm.dwHighDateTime= (DWORD)(t64 >> 32);
43804  osFileTimeToLocalFileTime(&uTm,&lTm);
43805  osFileTimeToSystemTime(&lTm,&pTm);
43806  y.tm_year = pTm.wYear - 1900;
43807  y.tm_mon = pTm.wMonth - 1;
43808  y.tm_wday = pTm.wDayOfWeek;
43809  y.tm_mday = pTm.wDay;
43810  y.tm_hour = pTm.wHour;
43811  y.tm_min = pTm.wMinute;
43812  y.tm_sec = pTm.wSecond;
43813  return &y;
43814}
43815#endif
43816
43817#if SQLITE_OS_WINCE
43818/*************************************************************************
43819** This section contains code for WinCE only.
43820*/
43821#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
43822
43823/*
43824** Acquire a lock on the handle h
43825*/
43826static void winceMutexAcquire(HANDLE h){
43827   DWORD dwErr;
43828   do {
43829     dwErr = osWaitForSingleObject(h, INFINITE);
43830   } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
43831}
43832/*
43833** Release a lock acquired by winceMutexAcquire()
43834*/
43835#define winceMutexRelease(h) ReleaseMutex(h)
43836
43837/*
43838** Create the mutex and shared memory used for locking in the file
43839** descriptor pFile
43840*/
43841static int winceCreateLock(const char *zFilename, winFile *pFile){
43842  LPWSTR zTok;
43843  LPWSTR zName;
43844  DWORD lastErrno;
43845  BOOL bLogged = FALSE;
43846  BOOL bInit = TRUE;
43847
43848  zName = winUtf8ToUnicode(zFilename);
43849  if( zName==0 ){
43850    /* out of memory */
43851    return SQLITE_IOERR_NOMEM_BKPT;
43852  }
43853
43854  /* Initialize the local lockdata */
43855  memset(&pFile->local, 0, sizeof(pFile->local));
43856
43857  /* Replace the backslashes from the filename and lowercase it
43858  ** to derive a mutex name. */
43859  zTok = osCharLowerW(zName);
43860  for (;*zTok;zTok++){
43861    if (*zTok == '\\') *zTok = '_';
43862  }
43863
43864  /* Create/open the named mutex */
43865  pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
43866  if (!pFile->hMutex){
43867    pFile->lastErrno = osGetLastError();
43868    sqlite3_free(zName);
43869    return winLogError(SQLITE_IOERR, pFile->lastErrno,
43870                       "winceCreateLock1", zFilename);
43871  }
43872
43873  /* Acquire the mutex before continuing */
43874  winceMutexAcquire(pFile->hMutex);
43875
43876  /* Since the names of named mutexes, semaphores, file mappings etc are
43877  ** case-sensitive, take advantage of that by uppercasing the mutex name
43878  ** and using that as the shared filemapping name.
43879  */
43880  osCharUpperW(zName);
43881  pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
43882                                        PAGE_READWRITE, 0, sizeof(winceLock),
43883                                        zName);
43884
43885  /* Set a flag that indicates we're the first to create the memory so it
43886  ** must be zero-initialized */
43887  lastErrno = osGetLastError();
43888  if (lastErrno == ERROR_ALREADY_EXISTS){
43889    bInit = FALSE;
43890  }
43891
43892  sqlite3_free(zName);
43893
43894  /* If we succeeded in making the shared memory handle, map it. */
43895  if( pFile->hShared ){
43896    pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
43897             FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
43898    /* If mapping failed, close the shared memory handle and erase it */
43899    if( !pFile->shared ){
43900      pFile->lastErrno = osGetLastError();
43901      winLogError(SQLITE_IOERR, pFile->lastErrno,
43902                  "winceCreateLock2", zFilename);
43903      bLogged = TRUE;
43904      osCloseHandle(pFile->hShared);
43905      pFile->hShared = NULL;
43906    }
43907  }
43908
43909  /* If shared memory could not be created, then close the mutex and fail */
43910  if( pFile->hShared==NULL ){
43911    if( !bLogged ){
43912      pFile->lastErrno = lastErrno;
43913      winLogError(SQLITE_IOERR, pFile->lastErrno,
43914                  "winceCreateLock3", zFilename);
43915      bLogged = TRUE;
43916    }
43917    winceMutexRelease(pFile->hMutex);
43918    osCloseHandle(pFile->hMutex);
43919    pFile->hMutex = NULL;
43920    return SQLITE_IOERR;
43921  }
43922
43923  /* Initialize the shared memory if we're supposed to */
43924  if( bInit ){
43925    memset(pFile->shared, 0, sizeof(winceLock));
43926  }
43927
43928  winceMutexRelease(pFile->hMutex);
43929  return SQLITE_OK;
43930}
43931
43932/*
43933** Destroy the part of winFile that deals with wince locks
43934*/
43935static void winceDestroyLock(winFile *pFile){
43936  if (pFile->hMutex){
43937    /* Acquire the mutex */
43938    winceMutexAcquire(pFile->hMutex);
43939
43940    /* The following blocks should probably assert in debug mode, but they
43941       are to cleanup in case any locks remained open */
43942    if (pFile->local.nReaders){
43943      pFile->shared->nReaders --;
43944    }
43945    if (pFile->local.bReserved){
43946      pFile->shared->bReserved = FALSE;
43947    }
43948    if (pFile->local.bPending){
43949      pFile->shared->bPending = FALSE;
43950    }
43951    if (pFile->local.bExclusive){
43952      pFile->shared->bExclusive = FALSE;
43953    }
43954
43955    /* De-reference and close our copy of the shared memory handle */
43956    osUnmapViewOfFile(pFile->shared);
43957    osCloseHandle(pFile->hShared);
43958
43959    /* Done with the mutex */
43960    winceMutexRelease(pFile->hMutex);
43961    osCloseHandle(pFile->hMutex);
43962    pFile->hMutex = NULL;
43963  }
43964}
43965
43966/*
43967** An implementation of the LockFile() API of Windows for CE
43968*/
43969static BOOL winceLockFile(
43970  LPHANDLE phFile,
43971  DWORD dwFileOffsetLow,
43972  DWORD dwFileOffsetHigh,
43973  DWORD nNumberOfBytesToLockLow,
43974  DWORD nNumberOfBytesToLockHigh
43975){
43976  winFile *pFile = HANDLE_TO_WINFILE(phFile);
43977  BOOL bReturn = FALSE;
43978
43979  UNUSED_PARAMETER(dwFileOffsetHigh);
43980  UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
43981
43982  if (!pFile->hMutex) return TRUE;
43983  winceMutexAcquire(pFile->hMutex);
43984
43985  /* Wanting an exclusive lock? */
43986  if (dwFileOffsetLow == (DWORD)SHARED_FIRST
43987       && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
43988    if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
43989       pFile->shared->bExclusive = TRUE;
43990       pFile->local.bExclusive = TRUE;
43991       bReturn = TRUE;
43992    }
43993  }
43994
43995  /* Want a read-only lock? */
43996  else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
43997           nNumberOfBytesToLockLow == 1){
43998    if (pFile->shared->bExclusive == 0){
43999      pFile->local.nReaders ++;
44000      if (pFile->local.nReaders == 1){
44001        pFile->shared->nReaders ++;
44002      }
44003      bReturn = TRUE;
44004    }
44005  }
44006
44007  /* Want a pending lock? */
44008  else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
44009           && nNumberOfBytesToLockLow == 1){
44010    /* If no pending lock has been acquired, then acquire it */
44011    if (pFile->shared->bPending == 0) {
44012      pFile->shared->bPending = TRUE;
44013      pFile->local.bPending = TRUE;
44014      bReturn = TRUE;
44015    }
44016  }
44017
44018  /* Want a reserved lock? */
44019  else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
44020           && nNumberOfBytesToLockLow == 1){
44021    if (pFile->shared->bReserved == 0) {
44022      pFile->shared->bReserved = TRUE;
44023      pFile->local.bReserved = TRUE;
44024      bReturn = TRUE;
44025    }
44026  }
44027
44028  winceMutexRelease(pFile->hMutex);
44029  return bReturn;
44030}
44031
44032/*
44033** An implementation of the UnlockFile API of Windows for CE
44034*/
44035static BOOL winceUnlockFile(
44036  LPHANDLE phFile,
44037  DWORD dwFileOffsetLow,
44038  DWORD dwFileOffsetHigh,
44039  DWORD nNumberOfBytesToUnlockLow,
44040  DWORD nNumberOfBytesToUnlockHigh
44041){
44042  winFile *pFile = HANDLE_TO_WINFILE(phFile);
44043  BOOL bReturn = FALSE;
44044
44045  UNUSED_PARAMETER(dwFileOffsetHigh);
44046  UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
44047
44048  if (!pFile->hMutex) return TRUE;
44049  winceMutexAcquire(pFile->hMutex);
44050
44051  /* Releasing a reader lock or an exclusive lock */
44052  if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
44053    /* Did we have an exclusive lock? */
44054    if (pFile->local.bExclusive){
44055      assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
44056      pFile->local.bExclusive = FALSE;
44057      pFile->shared->bExclusive = FALSE;
44058      bReturn = TRUE;
44059    }
44060
44061    /* Did we just have a reader lock? */
44062    else if (pFile->local.nReaders){
44063      assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE
44064             || nNumberOfBytesToUnlockLow == 1);
44065      pFile->local.nReaders --;
44066      if (pFile->local.nReaders == 0)
44067      {
44068        pFile->shared->nReaders --;
44069      }
44070      bReturn = TRUE;
44071    }
44072  }
44073
44074  /* Releasing a pending lock */
44075  else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
44076           && nNumberOfBytesToUnlockLow == 1){
44077    if (pFile->local.bPending){
44078      pFile->local.bPending = FALSE;
44079      pFile->shared->bPending = FALSE;
44080      bReturn = TRUE;
44081    }
44082  }
44083  /* Releasing a reserved lock */
44084  else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
44085           && nNumberOfBytesToUnlockLow == 1){
44086    if (pFile->local.bReserved) {
44087      pFile->local.bReserved = FALSE;
44088      pFile->shared->bReserved = FALSE;
44089      bReturn = TRUE;
44090    }
44091  }
44092
44093  winceMutexRelease(pFile->hMutex);
44094  return bReturn;
44095}
44096/*
44097** End of the special code for wince
44098*****************************************************************************/
44099#endif /* SQLITE_OS_WINCE */
44100
44101/*
44102** Lock a file region.
44103*/
44104static BOOL winLockFile(
44105  LPHANDLE phFile,
44106  DWORD flags,
44107  DWORD offsetLow,
44108  DWORD offsetHigh,
44109  DWORD numBytesLow,
44110  DWORD numBytesHigh
44111){
44112#if SQLITE_OS_WINCE
44113  /*
44114  ** NOTE: Windows CE is handled differently here due its lack of the Win32
44115  **       API LockFile.
44116  */
44117  return winceLockFile(phFile, offsetLow, offsetHigh,
44118                       numBytesLow, numBytesHigh);
44119#else
44120  if( osIsNT() ){
44121    OVERLAPPED ovlp;
44122    memset(&ovlp, 0, sizeof(OVERLAPPED));
44123    ovlp.Offset = offsetLow;
44124    ovlp.OffsetHigh = offsetHigh;
44125    return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
44126  }else{
44127    return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
44128                      numBytesHigh);
44129  }
44130#endif
44131}
44132
44133/*
44134** Unlock a file region.
44135 */
44136static BOOL winUnlockFile(
44137  LPHANDLE phFile,
44138  DWORD offsetLow,
44139  DWORD offsetHigh,
44140  DWORD numBytesLow,
44141  DWORD numBytesHigh
44142){
44143#if SQLITE_OS_WINCE
44144  /*
44145  ** NOTE: Windows CE is handled differently here due its lack of the Win32
44146  **       API UnlockFile.
44147  */
44148  return winceUnlockFile(phFile, offsetLow, offsetHigh,
44149                         numBytesLow, numBytesHigh);
44150#else
44151  if( osIsNT() ){
44152    OVERLAPPED ovlp;
44153    memset(&ovlp, 0, sizeof(OVERLAPPED));
44154    ovlp.Offset = offsetLow;
44155    ovlp.OffsetHigh = offsetHigh;
44156    return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
44157  }else{
44158    return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
44159                        numBytesHigh);
44160  }
44161#endif
44162}
44163
44164/*****************************************************************************
44165** The next group of routines implement the I/O methods specified
44166** by the sqlite3_io_methods object.
44167******************************************************************************/
44168
44169/*
44170** Some Microsoft compilers lack this definition.
44171*/
44172#ifndef INVALID_SET_FILE_POINTER
44173# define INVALID_SET_FILE_POINTER ((DWORD)-1)
44174#endif
44175
44176/*
44177** Move the current position of the file handle passed as the first
44178** argument to offset iOffset within the file. If successful, return 0.
44179** Otherwise, set pFile->lastErrno and return non-zero.
44180*/
44181static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
44182#if !SQLITE_OS_WINRT
44183  LONG upperBits;                 /* Most sig. 32 bits of new offset */
44184  LONG lowerBits;                 /* Least sig. 32 bits of new offset */
44185  DWORD dwRet;                    /* Value returned by SetFilePointer() */
44186  DWORD lastErrno;                /* Value returned by GetLastError() */
44187
44188  OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
44189
44190  upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
44191  lowerBits = (LONG)(iOffset & 0xffffffff);
44192
44193  /* API oddity: If successful, SetFilePointer() returns a dword
44194  ** containing the lower 32-bits of the new file-offset. Or, if it fails,
44195  ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
44196  ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
44197  ** whether an error has actually occurred, it is also necessary to call
44198  ** GetLastError().
44199  */
44200  dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
44201
44202  if( (dwRet==INVALID_SET_FILE_POINTER
44203      && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
44204    pFile->lastErrno = lastErrno;
44205    winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
44206                "winSeekFile", pFile->zPath);
44207    OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
44208    return 1;
44209  }
44210
44211  OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
44212  return 0;
44213#else
44214  /*
44215  ** Same as above, except that this implementation works for WinRT.
44216  */
44217
44218  LARGE_INTEGER x;                /* The new offset */
44219  BOOL bRet;                      /* Value returned by SetFilePointerEx() */
44220
44221  x.QuadPart = iOffset;
44222  bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
44223
44224  if(!bRet){
44225    pFile->lastErrno = osGetLastError();
44226    winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
44227                "winSeekFile", pFile->zPath);
44228    OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
44229    return 1;
44230  }
44231
44232  OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
44233  return 0;
44234#endif
44235}
44236
44237#if SQLITE_MAX_MMAP_SIZE>0
44238/* Forward references to VFS helper methods used for memory mapped files */
44239static int winMapfile(winFile*, sqlite3_int64);
44240static int winUnmapfile(winFile*);
44241#endif
44242
44243/*
44244** Close a file.
44245**
44246** It is reported that an attempt to close a handle might sometimes
44247** fail.  This is a very unreasonable result, but Windows is notorious
44248** for being unreasonable so I do not doubt that it might happen.  If
44249** the close fails, we pause for 100 milliseconds and try again.  As
44250** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
44251** giving up and returning an error.
44252*/
44253#define MX_CLOSE_ATTEMPT 3
44254static int winClose(sqlite3_file *id){
44255  int rc, cnt = 0;
44256  winFile *pFile = (winFile*)id;
44257
44258  assert( id!=0 );
44259#ifndef SQLITE_OMIT_WAL
44260  assert( pFile->pShm==0 );
44261#endif
44262  assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
44263  OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n",
44264           osGetCurrentProcessId(), pFile, pFile->h));
44265
44266#if SQLITE_MAX_MMAP_SIZE>0
44267  winUnmapfile(pFile);
44268#endif
44269
44270  do{
44271    rc = osCloseHandle(pFile->h);
44272    /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
44273  }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
44274#if SQLITE_OS_WINCE
44275#define WINCE_DELETION_ATTEMPTS 3
44276  {
44277    winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
44278    if( pAppData==NULL || !pAppData->bNoLock ){
44279      winceDestroyLock(pFile);
44280    }
44281  }
44282  if( pFile->zDeleteOnClose ){
44283    int cnt = 0;
44284    while(
44285           osDeleteFileW(pFile->zDeleteOnClose)==0
44286        && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
44287        && cnt++ < WINCE_DELETION_ATTEMPTS
44288    ){
44289       sqlite3_win32_sleep(100);  /* Wait a little before trying again */
44290    }
44291    sqlite3_free(pFile->zDeleteOnClose);
44292  }
44293#endif
44294  if( rc ){
44295    pFile->h = NULL;
44296  }
44297  OpenCounter(-1);
44298  OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n",
44299           osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
44300  return rc ? SQLITE_OK
44301            : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
44302                          "winClose", pFile->zPath);
44303}
44304
44305/*
44306** Read data from a file into a buffer.  Return SQLITE_OK if all
44307** bytes were read successfully and SQLITE_IOERR if anything goes
44308** wrong.
44309*/
44310static int winRead(
44311  sqlite3_file *id,          /* File to read from */
44312  void *pBuf,                /* Write content into this buffer */
44313  int amt,                   /* Number of bytes to read */
44314  sqlite3_int64 offset       /* Begin reading at this offset */
44315){
44316#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
44317  OVERLAPPED overlapped;          /* The offset for ReadFile. */
44318#endif
44319  winFile *pFile = (winFile*)id;  /* file handle */
44320  DWORD nRead;                    /* Number of bytes actually read from file */
44321  int nRetry = 0;                 /* Number of retrys */
44322
44323  assert( id!=0 );
44324  assert( amt>0 );
44325  assert( offset>=0 );
44326  SimulateIOError(return SQLITE_IOERR_READ);
44327  OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
44328           "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
44329           pFile->h, pBuf, amt, offset, pFile->locktype));
44330
44331#if SQLITE_MAX_MMAP_SIZE>0
44332  /* Deal with as much of this read request as possible by transfering
44333  ** data from the memory mapping using memcpy().  */
44334  if( offset<pFile->mmapSize ){
44335    if( offset+amt <= pFile->mmapSize ){
44336      memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
44337      OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
44338               osGetCurrentProcessId(), pFile, pFile->h));
44339      return SQLITE_OK;
44340    }else{
44341      int nCopy = (int)(pFile->mmapSize - offset);
44342      memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
44343      pBuf = &((u8 *)pBuf)[nCopy];
44344      amt -= nCopy;
44345      offset += nCopy;
44346    }
44347  }
44348#endif
44349
44350#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
44351  if( winSeekFile(pFile, offset) ){
44352    OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
44353             osGetCurrentProcessId(), pFile, pFile->h));
44354    return SQLITE_FULL;
44355  }
44356  while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
44357#else
44358  memset(&overlapped, 0, sizeof(OVERLAPPED));
44359  overlapped.Offset = (LONG)(offset & 0xffffffff);
44360  overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
44361  while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
44362         osGetLastError()!=ERROR_HANDLE_EOF ){
44363#endif
44364    DWORD lastErrno;
44365    if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
44366    pFile->lastErrno = lastErrno;
44367    OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n",
44368             osGetCurrentProcessId(), pFile, pFile->h));
44369    return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
44370                       "winRead", pFile->zPath);
44371  }
44372  winLogIoerr(nRetry, __LINE__);
44373  if( nRead<(DWORD)amt ){
44374    /* Unread parts of the buffer must be zero-filled */
44375    memset(&((char*)pBuf)[nRead], 0, amt-nRead);
44376    OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n",
44377             osGetCurrentProcessId(), pFile, pFile->h));
44378    return SQLITE_IOERR_SHORT_READ;
44379  }
44380
44381  OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
44382           osGetCurrentProcessId(), pFile, pFile->h));
44383  return SQLITE_OK;
44384}
44385
44386/*
44387** Write data from a buffer into a file.  Return SQLITE_OK on success
44388** or some other error code on failure.
44389*/
44390static int winWrite(
44391  sqlite3_file *id,               /* File to write into */
44392  const void *pBuf,               /* The bytes to be written */
44393  int amt,                        /* Number of bytes to write */
44394  sqlite3_int64 offset            /* Offset into the file to begin writing at */
44395){
44396  int rc = 0;                     /* True if error has occurred, else false */
44397  winFile *pFile = (winFile*)id;  /* File handle */
44398  int nRetry = 0;                 /* Number of retries */
44399
44400  assert( amt>0 );
44401  assert( pFile );
44402  SimulateIOError(return SQLITE_IOERR_WRITE);
44403  SimulateDiskfullError(return SQLITE_FULL);
44404
44405  OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
44406           "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
44407           pFile->h, pBuf, amt, offset, pFile->locktype));
44408
44409#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
44410  /* Deal with as much of this write request as possible by transfering
44411  ** data from the memory mapping using memcpy().  */
44412  if( offset<pFile->mmapSize ){
44413    if( offset+amt <= pFile->mmapSize ){
44414      memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
44415      OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
44416               osGetCurrentProcessId(), pFile, pFile->h));
44417      return SQLITE_OK;
44418    }else{
44419      int nCopy = (int)(pFile->mmapSize - offset);
44420      memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
44421      pBuf = &((u8 *)pBuf)[nCopy];
44422      amt -= nCopy;
44423      offset += nCopy;
44424    }
44425  }
44426#endif
44427
44428#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
44429  rc = winSeekFile(pFile, offset);
44430  if( rc==0 ){
44431#else
44432  {
44433#endif
44434#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
44435    OVERLAPPED overlapped;        /* The offset for WriteFile. */
44436#endif
44437    u8 *aRem = (u8 *)pBuf;        /* Data yet to be written */
44438    int nRem = amt;               /* Number of bytes yet to be written */
44439    DWORD nWrite;                 /* Bytes written by each WriteFile() call */
44440    DWORD lastErrno = NO_ERROR;   /* Value returned by GetLastError() */
44441
44442#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
44443    memset(&overlapped, 0, sizeof(OVERLAPPED));
44444    overlapped.Offset = (LONG)(offset & 0xffffffff);
44445    overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
44446#endif
44447
44448    while( nRem>0 ){
44449#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
44450      if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
44451#else
44452      if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
44453#endif
44454        if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
44455        break;
44456      }
44457      assert( nWrite==0 || nWrite<=(DWORD)nRem );
44458      if( nWrite==0 || nWrite>(DWORD)nRem ){
44459        lastErrno = osGetLastError();
44460        break;
44461      }
44462#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
44463      offset += nWrite;
44464      overlapped.Offset = (LONG)(offset & 0xffffffff);
44465      overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
44466#endif
44467      aRem += nWrite;
44468      nRem -= nWrite;
44469    }
44470    if( nRem>0 ){
44471      pFile->lastErrno = lastErrno;
44472      rc = 1;
44473    }
44474  }
44475
44476  if( rc ){
44477    if(   ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
44478       || ( pFile->lastErrno==ERROR_DISK_FULL )){
44479      OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
44480               osGetCurrentProcessId(), pFile, pFile->h));
44481      return winLogError(SQLITE_FULL, pFile->lastErrno,
44482                         "winWrite1", pFile->zPath);
44483    }
44484    OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n",
44485             osGetCurrentProcessId(), pFile, pFile->h));
44486    return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
44487                       "winWrite2", pFile->zPath);
44488  }else{
44489    winLogIoerr(nRetry, __LINE__);
44490  }
44491  OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
44492           osGetCurrentProcessId(), pFile, pFile->h));
44493  return SQLITE_OK;
44494}
44495
44496/*
44497** Truncate an open file to a specified size
44498*/
44499static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
44500  winFile *pFile = (winFile*)id;  /* File handle object */
44501  int rc = SQLITE_OK;             /* Return code for this function */
44502  DWORD lastErrno;
44503#if SQLITE_MAX_MMAP_SIZE>0
44504  sqlite3_int64 oldMmapSize;
44505  if( pFile->nFetchOut>0 ){
44506    /* File truncation is a no-op if there are outstanding memory mapped
44507    ** pages.  This is because truncating the file means temporarily unmapping
44508    ** the file, and that might delete memory out from under existing cursors.
44509    **
44510    ** This can result in incremental vacuum not truncating the file,
44511    ** if there is an active read cursor when the incremental vacuum occurs.
44512    ** No real harm comes of this - the database file is not corrupted,
44513    ** though some folks might complain that the file is bigger than it
44514    ** needs to be.
44515    **
44516    ** The only feasible work-around is to defer the truncation until after
44517    ** all references to memory-mapped content are closed.  That is doable,
44518    ** but involves adding a few branches in the common write code path which
44519    ** could slow down normal operations slightly.  Hence, we have decided for
44520    ** now to simply make trancations a no-op if there are pending reads.  We
44521    ** can maybe revisit this decision in the future.
44522    */
44523    return SQLITE_OK;
44524  }
44525#endif
44526
44527  assert( pFile );
44528  SimulateIOError(return SQLITE_IOERR_TRUNCATE);
44529  OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
44530           osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
44531
44532  /* If the user has configured a chunk-size for this file, truncate the
44533  ** file so that it consists of an integer number of chunks (i.e. the
44534  ** actual file size after the operation may be larger than the requested
44535  ** size).
44536  */
44537  if( pFile->szChunk>0 ){
44538    nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
44539  }
44540
44541#if SQLITE_MAX_MMAP_SIZE>0
44542  if( pFile->pMapRegion ){
44543    oldMmapSize = pFile->mmapSize;
44544  }else{
44545    oldMmapSize = 0;
44546  }
44547  winUnmapfile(pFile);
44548#endif
44549
44550  /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
44551  if( winSeekFile(pFile, nByte) ){
44552    rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
44553                     "winTruncate1", pFile->zPath);
44554  }else if( 0==osSetEndOfFile(pFile->h) &&
44555            ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
44556    pFile->lastErrno = lastErrno;
44557    rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
44558                     "winTruncate2", pFile->zPath);
44559  }
44560
44561#if SQLITE_MAX_MMAP_SIZE>0
44562  if( rc==SQLITE_OK && oldMmapSize>0 ){
44563    if( oldMmapSize>nByte ){
44564      winMapfile(pFile, -1);
44565    }else{
44566      winMapfile(pFile, oldMmapSize);
44567    }
44568  }
44569#endif
44570
44571  OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n",
44572           osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
44573  return rc;
44574}
44575
44576#ifdef SQLITE_TEST
44577/*
44578** Count the number of fullsyncs and normal syncs.  This is used to test
44579** that syncs and fullsyncs are occuring at the right times.
44580*/
44581SQLITE_API int sqlite3_sync_count = 0;
44582SQLITE_API int sqlite3_fullsync_count = 0;
44583#endif
44584
44585/*
44586** Make sure all writes to a particular file are committed to disk.
44587*/
44588static int winSync(sqlite3_file *id, int flags){
44589#ifndef SQLITE_NO_SYNC
44590  /*
44591  ** Used only when SQLITE_NO_SYNC is not defined.
44592   */
44593  BOOL rc;
44594#endif
44595#if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
44596    defined(SQLITE_HAVE_OS_TRACE)
44597  /*
44598  ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
44599  ** OSTRACE() macros.
44600   */
44601  winFile *pFile = (winFile*)id;
44602#else
44603  UNUSED_PARAMETER(id);
44604#endif
44605
44606  assert( pFile );
44607  /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
44608  assert((flags&0x0F)==SQLITE_SYNC_NORMAL
44609      || (flags&0x0F)==SQLITE_SYNC_FULL
44610  );
44611
44612  /* Unix cannot, but some systems may return SQLITE_FULL from here. This
44613  ** line is to test that doing so does not cause any problems.
44614  */
44615  SimulateDiskfullError( return SQLITE_FULL );
44616
44617  OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n",
44618           osGetCurrentProcessId(), pFile, pFile->h, flags,
44619           pFile->locktype));
44620
44621#ifndef SQLITE_TEST
44622  UNUSED_PARAMETER(flags);
44623#else
44624  if( (flags&0x0F)==SQLITE_SYNC_FULL ){
44625    sqlite3_fullsync_count++;
44626  }
44627  sqlite3_sync_count++;
44628#endif
44629
44630  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
44631  ** no-op
44632  */
44633#ifdef SQLITE_NO_SYNC
44634  OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
44635           osGetCurrentProcessId(), pFile, pFile->h));
44636  return SQLITE_OK;
44637#else
44638#if SQLITE_MAX_MMAP_SIZE>0
44639  if( pFile->pMapRegion ){
44640    if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
44641      OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
44642               "rc=SQLITE_OK\n", osGetCurrentProcessId(),
44643               pFile, pFile->pMapRegion));
44644    }else{
44645      pFile->lastErrno = osGetLastError();
44646      OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
44647               "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(),
44648               pFile, pFile->pMapRegion));
44649      return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
44650                         "winSync1", pFile->zPath);
44651    }
44652  }
44653#endif
44654  rc = osFlushFileBuffers(pFile->h);
44655  SimulateIOError( rc=FALSE );
44656  if( rc ){
44657    OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
44658             osGetCurrentProcessId(), pFile, pFile->h));
44659    return SQLITE_OK;
44660  }else{
44661    pFile->lastErrno = osGetLastError();
44662    OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n",
44663             osGetCurrentProcessId(), pFile, pFile->h));
44664    return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
44665                       "winSync2", pFile->zPath);
44666  }
44667#endif
44668}
44669
44670/*
44671** Determine the current size of a file in bytes
44672*/
44673static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
44674  winFile *pFile = (winFile*)id;
44675  int rc = SQLITE_OK;
44676
44677  assert( id!=0 );
44678  assert( pSize!=0 );
44679  SimulateIOError(return SQLITE_IOERR_FSTAT);
44680  OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
44681
44682#if SQLITE_OS_WINRT
44683  {
44684    FILE_STANDARD_INFO info;
44685    if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
44686                                     &info, sizeof(info)) ){
44687      *pSize = info.EndOfFile.QuadPart;
44688    }else{
44689      pFile->lastErrno = osGetLastError();
44690      rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
44691                       "winFileSize", pFile->zPath);
44692    }
44693  }
44694#else
44695  {
44696    DWORD upperBits;
44697    DWORD lowerBits;
44698    DWORD lastErrno;
44699
44700    lowerBits = osGetFileSize(pFile->h, &upperBits);
44701    *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
44702    if(   (lowerBits == INVALID_FILE_SIZE)
44703       && ((lastErrno = osGetLastError())!=NO_ERROR) ){
44704      pFile->lastErrno = lastErrno;
44705      rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
44706                       "winFileSize", pFile->zPath);
44707    }
44708  }
44709#endif
44710  OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
44711           pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
44712  return rc;
44713}
44714
44715/*
44716** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
44717*/
44718#ifndef LOCKFILE_FAIL_IMMEDIATELY
44719# define LOCKFILE_FAIL_IMMEDIATELY 1
44720#endif
44721
44722#ifndef LOCKFILE_EXCLUSIVE_LOCK
44723# define LOCKFILE_EXCLUSIVE_LOCK 2
44724#endif
44725
44726/*
44727** Historically, SQLite has used both the LockFile and LockFileEx functions.
44728** When the LockFile function was used, it was always expected to fail
44729** immediately if the lock could not be obtained.  Also, it always expected to
44730** obtain an exclusive lock.  These flags are used with the LockFileEx function
44731** and reflect those expectations; therefore, they should not be changed.
44732*/
44733#ifndef SQLITE_LOCKFILE_FLAGS
44734# define SQLITE_LOCKFILE_FLAGS   (LOCKFILE_FAIL_IMMEDIATELY | \
44735                                  LOCKFILE_EXCLUSIVE_LOCK)
44736#endif
44737
44738/*
44739** Currently, SQLite never calls the LockFileEx function without wanting the
44740** call to fail immediately if the lock cannot be obtained.
44741*/
44742#ifndef SQLITE_LOCKFILEEX_FLAGS
44743# define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
44744#endif
44745
44746/*
44747** Acquire a reader lock.
44748** Different API routines are called depending on whether or not this
44749** is Win9x or WinNT.
44750*/
44751static int winGetReadLock(winFile *pFile){
44752  int res;
44753  OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
44754  if( osIsNT() ){
44755#if SQLITE_OS_WINCE
44756    /*
44757    ** NOTE: Windows CE is handled differently here due its lack of the Win32
44758    **       API LockFileEx.
44759    */
44760    res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
44761#else
44762    res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
44763                      SHARED_SIZE, 0);
44764#endif
44765  }
44766#ifdef SQLITE_WIN32_HAS_ANSI
44767  else{
44768    int lk;
44769    sqlite3_randomness(sizeof(lk), &lk);
44770    pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
44771    res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
44772                      SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
44773  }
44774#endif
44775  if( res == 0 ){
44776    pFile->lastErrno = osGetLastError();
44777    /* No need to log a failure to lock */
44778  }
44779  OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
44780  return res;
44781}
44782
44783/*
44784** Undo a readlock
44785*/
44786static int winUnlockReadLock(winFile *pFile){
44787  int res;
44788  DWORD lastErrno;
44789  OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
44790  if( osIsNT() ){
44791    res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
44792  }
44793#ifdef SQLITE_WIN32_HAS_ANSI
44794  else{
44795    res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
44796  }
44797#endif
44798  if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
44799    pFile->lastErrno = lastErrno;
44800    winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
44801                "winUnlockReadLock", pFile->zPath);
44802  }
44803  OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
44804  return res;
44805}
44806
44807/*
44808** Lock the file with the lock specified by parameter locktype - one
44809** of the following:
44810**
44811**     (1) SHARED_LOCK
44812**     (2) RESERVED_LOCK
44813**     (3) PENDING_LOCK
44814**     (4) EXCLUSIVE_LOCK
44815**
44816** Sometimes when requesting one lock state, additional lock states
44817** are inserted in between.  The locking might fail on one of the later
44818** transitions leaving the lock state different from what it started but
44819** still short of its goal.  The following chart shows the allowed
44820** transitions and the inserted intermediate states:
44821**
44822**    UNLOCKED -> SHARED
44823**    SHARED -> RESERVED
44824**    SHARED -> (PENDING) -> EXCLUSIVE
44825**    RESERVED -> (PENDING) -> EXCLUSIVE
44826**    PENDING -> EXCLUSIVE
44827**
44828** This routine will only increase a lock.  The winUnlock() routine
44829** erases all locks at once and returns us immediately to locking level 0.
44830** It is not possible to lower the locking level one step at a time.  You
44831** must go straight to locking level 0.
44832*/
44833static int winLock(sqlite3_file *id, int locktype){
44834  int rc = SQLITE_OK;    /* Return code from subroutines */
44835  int res = 1;           /* Result of a Windows lock call */
44836  int newLocktype;       /* Set pFile->locktype to this value before exiting */
44837  int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
44838  winFile *pFile = (winFile*)id;
44839  DWORD lastErrno = NO_ERROR;
44840
44841  assert( id!=0 );
44842  OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
44843           pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
44844
44845  /* If there is already a lock of this type or more restrictive on the
44846  ** OsFile, do nothing. Don't use the end_lock: exit path, as
44847  ** sqlite3OsEnterMutex() hasn't been called yet.
44848  */
44849  if( pFile->locktype>=locktype ){
44850    OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
44851    return SQLITE_OK;
44852  }
44853
44854  /* Do not allow any kind of write-lock on a read-only database
44855  */
44856  if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
44857    return SQLITE_IOERR_LOCK;
44858  }
44859
44860  /* Make sure the locking sequence is correct
44861  */
44862  assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
44863  assert( locktype!=PENDING_LOCK );
44864  assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
44865
44866  /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
44867  ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
44868  ** the PENDING_LOCK byte is temporary.
44869  */
44870  newLocktype = pFile->locktype;
44871  if( pFile->locktype==NO_LOCK
44872   || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
44873  ){
44874    int cnt = 3;
44875    while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
44876                                         PENDING_BYTE, 0, 1, 0))==0 ){
44877      /* Try 3 times to get the pending lock.  This is needed to work
44878      ** around problems caused by indexing and/or anti-virus software on
44879      ** Windows systems.
44880      ** If you are using this code as a model for alternative VFSes, do not
44881      ** copy this retry logic.  It is a hack intended for Windows only.
44882      */
44883      lastErrno = osGetLastError();
44884      OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
44885               pFile->h, cnt, res));
44886      if( lastErrno==ERROR_INVALID_HANDLE ){
44887        pFile->lastErrno = lastErrno;
44888        rc = SQLITE_IOERR_LOCK;
44889        OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
44890                 pFile->h, cnt, sqlite3ErrName(rc)));
44891        return rc;
44892      }
44893      if( cnt ) sqlite3_win32_sleep(1);
44894    }
44895    gotPendingLock = res;
44896    if( !res ){
44897      lastErrno = osGetLastError();
44898    }
44899  }
44900
44901  /* Acquire a shared lock
44902  */
44903  if( locktype==SHARED_LOCK && res ){
44904    assert( pFile->locktype==NO_LOCK );
44905    res = winGetReadLock(pFile);
44906    if( res ){
44907      newLocktype = SHARED_LOCK;
44908    }else{
44909      lastErrno = osGetLastError();
44910    }
44911  }
44912
44913  /* Acquire a RESERVED lock
44914  */
44915  if( locktype==RESERVED_LOCK && res ){
44916    assert( pFile->locktype==SHARED_LOCK );
44917    res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
44918    if( res ){
44919      newLocktype = RESERVED_LOCK;
44920    }else{
44921      lastErrno = osGetLastError();
44922    }
44923  }
44924
44925  /* Acquire a PENDING lock
44926  */
44927  if( locktype==EXCLUSIVE_LOCK && res ){
44928    newLocktype = PENDING_LOCK;
44929    gotPendingLock = 0;
44930  }
44931
44932  /* Acquire an EXCLUSIVE lock
44933  */
44934  if( locktype==EXCLUSIVE_LOCK && res ){
44935    assert( pFile->locktype>=SHARED_LOCK );
44936    res = winUnlockReadLock(pFile);
44937    res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
44938                      SHARED_SIZE, 0);
44939    if( res ){
44940      newLocktype = EXCLUSIVE_LOCK;
44941    }else{
44942      lastErrno = osGetLastError();
44943      winGetReadLock(pFile);
44944    }
44945  }
44946
44947  /* If we are holding a PENDING lock that ought to be released, then
44948  ** release it now.
44949  */
44950  if( gotPendingLock && locktype==SHARED_LOCK ){
44951    winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
44952  }
44953
44954  /* Update the state of the lock has held in the file descriptor then
44955  ** return the appropriate result code.
44956  */
44957  if( res ){
44958    rc = SQLITE_OK;
44959  }else{
44960    pFile->lastErrno = lastErrno;
44961    rc = SQLITE_BUSY;
44962    OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
44963             pFile->h, locktype, newLocktype));
44964  }
44965  pFile->locktype = (u8)newLocktype;
44966  OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
44967           pFile->h, pFile->locktype, sqlite3ErrName(rc)));
44968  return rc;
44969}
44970
44971/*
44972** This routine checks if there is a RESERVED lock held on the specified
44973** file by this or any other process. If such a lock is held, return
44974** non-zero, otherwise zero.
44975*/
44976static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
44977  int res;
44978  winFile *pFile = (winFile*)id;
44979
44980  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
44981  OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
44982
44983  assert( id!=0 );
44984  if( pFile->locktype>=RESERVED_LOCK ){
44985    res = 1;
44986    OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
44987  }else{
44988    res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
44989    if( res ){
44990      winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
44991    }
44992    res = !res;
44993    OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
44994  }
44995  *pResOut = res;
44996  OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
44997           pFile->h, pResOut, *pResOut));
44998  return SQLITE_OK;
44999}
45000
45001/*
45002** Lower the locking level on file descriptor id to locktype.  locktype
45003** must be either NO_LOCK or SHARED_LOCK.
45004**
45005** If the locking level of the file descriptor is already at or below
45006** the requested locking level, this routine is a no-op.
45007**
45008** It is not possible for this routine to fail if the second argument
45009** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
45010** might return SQLITE_IOERR;
45011*/
45012static int winUnlock(sqlite3_file *id, int locktype){
45013  int type;
45014  winFile *pFile = (winFile*)id;
45015  int rc = SQLITE_OK;
45016  assert( pFile!=0 );
45017  assert( locktype<=SHARED_LOCK );
45018  OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
45019           pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
45020  type = pFile->locktype;
45021  if( type>=EXCLUSIVE_LOCK ){
45022    winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
45023    if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
45024      /* This should never happen.  We should always be able to
45025      ** reacquire the read lock */
45026      rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
45027                       "winUnlock", pFile->zPath);
45028    }
45029  }
45030  if( type>=RESERVED_LOCK ){
45031    winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
45032  }
45033  if( locktype==NO_LOCK && type>=SHARED_LOCK ){
45034    winUnlockReadLock(pFile);
45035  }
45036  if( type>=PENDING_LOCK ){
45037    winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
45038  }
45039  pFile->locktype = (u8)locktype;
45040  OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
45041           pFile->h, pFile->locktype, sqlite3ErrName(rc)));
45042  return rc;
45043}
45044
45045/******************************************************************************
45046****************************** No-op Locking **********************************
45047**
45048** Of the various locking implementations available, this is by far the
45049** simplest:  locking is ignored.  No attempt is made to lock the database
45050** file for reading or writing.
45051**
45052** This locking mode is appropriate for use on read-only databases
45053** (ex: databases that are burned into CD-ROM, for example.)  It can
45054** also be used if the application employs some external mechanism to
45055** prevent simultaneous access of the same database by two or more
45056** database connections.  But there is a serious risk of database
45057** corruption if this locking mode is used in situations where multiple
45058** database connections are accessing the same database file at the same
45059** time and one or more of those connections are writing.
45060*/
45061
45062static int winNolockLock(sqlite3_file *id, int locktype){
45063  UNUSED_PARAMETER(id);
45064  UNUSED_PARAMETER(locktype);
45065  return SQLITE_OK;
45066}
45067
45068static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
45069  UNUSED_PARAMETER(id);
45070  UNUSED_PARAMETER(pResOut);
45071  return SQLITE_OK;
45072}
45073
45074static int winNolockUnlock(sqlite3_file *id, int locktype){
45075  UNUSED_PARAMETER(id);
45076  UNUSED_PARAMETER(locktype);
45077  return SQLITE_OK;
45078}
45079
45080/******************* End of the no-op lock implementation *********************
45081******************************************************************************/
45082
45083/*
45084** If *pArg is initially negative then this is a query.  Set *pArg to
45085** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
45086**
45087** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
45088*/
45089static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
45090  if( *pArg<0 ){
45091    *pArg = (pFile->ctrlFlags & mask)!=0;
45092  }else if( (*pArg)==0 ){
45093    pFile->ctrlFlags &= ~mask;
45094  }else{
45095    pFile->ctrlFlags |= mask;
45096  }
45097}
45098
45099/* Forward references to VFS helper methods used for temporary files */
45100static int winGetTempname(sqlite3_vfs *, char **);
45101static int winIsDir(const void *);
45102static BOOL winIsLongPathPrefix(const char *);
45103static BOOL winIsDriveLetterAndColon(const char *);
45104
45105/*
45106** Control and query of the open file handle.
45107*/
45108static int winFileControl(sqlite3_file *id, int op, void *pArg){
45109  winFile *pFile = (winFile*)id;
45110  OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
45111  switch( op ){
45112    case SQLITE_FCNTL_LOCKSTATE: {
45113      *(int*)pArg = pFile->locktype;
45114      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45115      return SQLITE_OK;
45116    }
45117    case SQLITE_FCNTL_LAST_ERRNO: {
45118      *(int*)pArg = (int)pFile->lastErrno;
45119      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45120      return SQLITE_OK;
45121    }
45122    case SQLITE_FCNTL_CHUNK_SIZE: {
45123      pFile->szChunk = *(int *)pArg;
45124      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45125      return SQLITE_OK;
45126    }
45127    case SQLITE_FCNTL_SIZE_HINT: {
45128      if( pFile->szChunk>0 ){
45129        sqlite3_int64 oldSz;
45130        int rc = winFileSize(id, &oldSz);
45131        if( rc==SQLITE_OK ){
45132          sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
45133          if( newSz>oldSz ){
45134            SimulateIOErrorBenign(1);
45135            rc = winTruncate(id, newSz);
45136            SimulateIOErrorBenign(0);
45137          }
45138        }
45139        OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
45140        return rc;
45141      }
45142      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45143      return SQLITE_OK;
45144    }
45145    case SQLITE_FCNTL_PERSIST_WAL: {
45146      winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
45147      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45148      return SQLITE_OK;
45149    }
45150    case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
45151      winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
45152      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45153      return SQLITE_OK;
45154    }
45155    case SQLITE_FCNTL_VFSNAME: {
45156      *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
45157      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45158      return SQLITE_OK;
45159    }
45160    case SQLITE_FCNTL_WIN32_AV_RETRY: {
45161      int *a = (int*)pArg;
45162      if( a[0]>0 ){
45163        winIoerrRetry = a[0];
45164      }else{
45165        a[0] = winIoerrRetry;
45166      }
45167      if( a[1]>0 ){
45168        winIoerrRetryDelay = a[1];
45169      }else{
45170        a[1] = winIoerrRetryDelay;
45171      }
45172      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45173      return SQLITE_OK;
45174    }
45175    case SQLITE_FCNTL_WIN32_GET_HANDLE: {
45176      LPHANDLE phFile = (LPHANDLE)pArg;
45177      *phFile = pFile->h;
45178      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45179      return SQLITE_OK;
45180    }
45181#ifdef SQLITE_TEST
45182    case SQLITE_FCNTL_WIN32_SET_HANDLE: {
45183      LPHANDLE phFile = (LPHANDLE)pArg;
45184      HANDLE hOldFile = pFile->h;
45185      pFile->h = *phFile;
45186      *phFile = hOldFile;
45187      OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
45188               hOldFile, pFile->h));
45189      return SQLITE_OK;
45190    }
45191#endif
45192    case SQLITE_FCNTL_TEMPFILENAME: {
45193      char *zTFile = 0;
45194      int rc = winGetTempname(pFile->pVfs, &zTFile);
45195      if( rc==SQLITE_OK ){
45196        *(char**)pArg = zTFile;
45197      }
45198      OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
45199      return rc;
45200    }
45201#if SQLITE_MAX_MMAP_SIZE>0
45202    case SQLITE_FCNTL_MMAP_SIZE: {
45203      i64 newLimit = *(i64*)pArg;
45204      int rc = SQLITE_OK;
45205      if( newLimit>sqlite3GlobalConfig.mxMmap ){
45206        newLimit = sqlite3GlobalConfig.mxMmap;
45207      }
45208
45209      /* The value of newLimit may be eventually cast to (SIZE_T) and passed
45210      ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at
45211      ** least a 64-bit type. */
45212      if( newLimit>0 && sizeof(SIZE_T)<8 ){
45213        newLimit = (newLimit & 0x7FFFFFFF);
45214      }
45215
45216      *(i64*)pArg = pFile->mmapSizeMax;
45217      if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
45218        pFile->mmapSizeMax = newLimit;
45219        if( pFile->mmapSize>0 ){
45220          winUnmapfile(pFile);
45221          rc = winMapfile(pFile, -1);
45222        }
45223      }
45224      OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
45225      return rc;
45226    }
45227#endif
45228  }
45229  OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
45230  return SQLITE_NOTFOUND;
45231}
45232
45233/*
45234** Return the sector size in bytes of the underlying block device for
45235** the specified file. This is almost always 512 bytes, but may be
45236** larger for some devices.
45237**
45238** SQLite code assumes this function cannot fail. It also assumes that
45239** if two files are created in the same file-system directory (i.e.
45240** a database and its journal file) that the sector size will be the
45241** same for both.
45242*/
45243static int winSectorSize(sqlite3_file *id){
45244  (void)id;
45245  return SQLITE_DEFAULT_SECTOR_SIZE;
45246}
45247
45248/*
45249** Return a vector of device characteristics.
45250*/
45251static int winDeviceCharacteristics(sqlite3_file *id){
45252  winFile *p = (winFile*)id;
45253  return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
45254         ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
45255}
45256
45257/*
45258** Windows will only let you create file view mappings
45259** on allocation size granularity boundaries.
45260** During sqlite3_os_init() we do a GetSystemInfo()
45261** to get the granularity size.
45262*/
45263static SYSTEM_INFO winSysInfo;
45264
45265#ifndef SQLITE_OMIT_WAL
45266
45267/*
45268** Helper functions to obtain and relinquish the global mutex. The
45269** global mutex is used to protect the winLockInfo objects used by
45270** this file, all of which may be shared by multiple threads.
45271**
45272** Function winShmMutexHeld() is used to assert() that the global mutex
45273** is held when required. This function is only used as part of assert()
45274** statements. e.g.
45275**
45276**   winShmEnterMutex()
45277**     assert( winShmMutexHeld() );
45278**   winShmLeaveMutex()
45279*/
45280static sqlite3_mutex *winBigLock = 0;
45281static void winShmEnterMutex(void){
45282  sqlite3_mutex_enter(winBigLock);
45283}
45284static void winShmLeaveMutex(void){
45285  sqlite3_mutex_leave(winBigLock);
45286}
45287#ifndef NDEBUG
45288static int winShmMutexHeld(void) {
45289  return sqlite3_mutex_held(winBigLock);
45290}
45291#endif
45292
45293/*
45294** Object used to represent a single file opened and mmapped to provide
45295** shared memory.  When multiple threads all reference the same
45296** log-summary, each thread has its own winFile object, but they all
45297** point to a single instance of this object.  In other words, each
45298** log-summary is opened only once per process.
45299**
45300** winShmMutexHeld() must be true when creating or destroying
45301** this object or while reading or writing the following fields:
45302**
45303**      nRef
45304**      pNext
45305**
45306** The following fields are read-only after the object is created:
45307**
45308**      fid
45309**      zFilename
45310**
45311** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
45312** winShmMutexHeld() is true when reading or writing any other field
45313** in this structure.
45314**
45315*/
45316struct winShmNode {
45317  sqlite3_mutex *mutex;      /* Mutex to access this object */
45318  char *zFilename;           /* Name of the file */
45319  winFile hFile;             /* File handle from winOpen */
45320
45321  int szRegion;              /* Size of shared-memory regions */
45322  int nRegion;               /* Size of array apRegion */
45323  u8 isReadonly;             /* True if read-only */
45324  u8 isUnlocked;             /* True if no DMS lock held */
45325
45326  struct ShmRegion {
45327    HANDLE hMap;             /* File handle from CreateFileMapping */
45328    void *pMap;
45329  } *aRegion;
45330  DWORD lastErrno;           /* The Windows errno from the last I/O error */
45331
45332  int nRef;                  /* Number of winShm objects pointing to this */
45333  winShm *pFirst;            /* All winShm objects pointing to this */
45334  winShmNode *pNext;         /* Next in list of all winShmNode objects */
45335#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
45336  u8 nextShmId;              /* Next available winShm.id value */
45337#endif
45338};
45339
45340/*
45341** A global array of all winShmNode objects.
45342**
45343** The winShmMutexHeld() must be true while reading or writing this list.
45344*/
45345static winShmNode *winShmNodeList = 0;
45346
45347/*
45348** Structure used internally by this VFS to record the state of an
45349** open shared memory connection.
45350**
45351** The following fields are initialized when this object is created and
45352** are read-only thereafter:
45353**
45354**    winShm.pShmNode
45355**    winShm.id
45356**
45357** All other fields are read/write.  The winShm.pShmNode->mutex must be held
45358** while accessing any read/write fields.
45359*/
45360struct winShm {
45361  winShmNode *pShmNode;      /* The underlying winShmNode object */
45362  winShm *pNext;             /* Next winShm with the same winShmNode */
45363  u8 hasMutex;               /* True if holding the winShmNode mutex */
45364  u16 sharedMask;            /* Mask of shared locks held */
45365  u16 exclMask;              /* Mask of exclusive locks held */
45366#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
45367  u8 id;                     /* Id of this connection with its winShmNode */
45368#endif
45369};
45370
45371/*
45372** Constants used for locking
45373*/
45374#define WIN_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
45375#define WIN_SHM_DMS    (WIN_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
45376
45377/*
45378** Apply advisory locks for all n bytes beginning at ofst.
45379*/
45380#define WINSHM_UNLCK  1
45381#define WINSHM_RDLCK  2
45382#define WINSHM_WRLCK  3
45383static int winShmSystemLock(
45384  winShmNode *pFile,    /* Apply locks to this open shared-memory segment */
45385  int lockType,         /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
45386  int ofst,             /* Offset to first byte to be locked/unlocked */
45387  int nByte             /* Number of bytes to lock or unlock */
45388){
45389  int rc = 0;           /* Result code form Lock/UnlockFileEx() */
45390
45391  /* Access to the winShmNode object is serialized by the caller */
45392  assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
45393
45394  OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
45395           pFile->hFile.h, lockType, ofst, nByte));
45396
45397  /* Release/Acquire the system-level lock */
45398  if( lockType==WINSHM_UNLCK ){
45399    rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
45400  }else{
45401    /* Initialize the locking parameters */
45402    DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
45403    if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
45404    rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
45405  }
45406
45407  if( rc!= 0 ){
45408    rc = SQLITE_OK;
45409  }else{
45410    pFile->lastErrno =  osGetLastError();
45411    rc = SQLITE_BUSY;
45412  }
45413
45414  OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
45415           pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
45416           "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
45417
45418  return rc;
45419}
45420
45421/* Forward references to VFS methods */
45422static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
45423static int winDelete(sqlite3_vfs *,const char*,int);
45424
45425/*
45426** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
45427**
45428** This is not a VFS shared-memory method; it is a utility function called
45429** by VFS shared-memory methods.
45430*/
45431static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
45432  winShmNode **pp;
45433  winShmNode *p;
45434  assert( winShmMutexHeld() );
45435  OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
45436           osGetCurrentProcessId(), deleteFlag));
45437  pp = &winShmNodeList;
45438  while( (p = *pp)!=0 ){
45439    if( p->nRef==0 ){
45440      int i;
45441      if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
45442      for(i=0; i<p->nRegion; i++){
45443        BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
45444        OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
45445                 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
45446        UNUSED_VARIABLE_VALUE(bRc);
45447        bRc = osCloseHandle(p->aRegion[i].hMap);
45448        OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
45449                 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
45450        UNUSED_VARIABLE_VALUE(bRc);
45451      }
45452      if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
45453        SimulateIOErrorBenign(1);
45454        winClose((sqlite3_file *)&p->hFile);
45455        SimulateIOErrorBenign(0);
45456      }
45457      if( deleteFlag ){
45458        SimulateIOErrorBenign(1);
45459        sqlite3BeginBenignMalloc();
45460        winDelete(pVfs, p->zFilename, 0);
45461        sqlite3EndBenignMalloc();
45462        SimulateIOErrorBenign(0);
45463      }
45464      *pp = p->pNext;
45465      sqlite3_free(p->aRegion);
45466      sqlite3_free(p);
45467    }else{
45468      pp = &p->pNext;
45469    }
45470  }
45471}
45472
45473/*
45474** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
45475** take it now. Return SQLITE_OK if successful, or an SQLite error
45476** code otherwise.
45477**
45478** If the DMS cannot be locked because this is a readonly_shm=1
45479** connection and no other process already holds a lock, return
45480** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
45481*/
45482static int winLockSharedMemory(winShmNode *pShmNode){
45483  int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1);
45484
45485  if( rc==SQLITE_OK ){
45486    if( pShmNode->isReadonly ){
45487      pShmNode->isUnlocked = 1;
45488      winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
45489      return SQLITE_READONLY_CANTINIT;
45490    }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
45491      winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
45492      return winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
45493                         "winLockSharedMemory", pShmNode->zFilename);
45494    }
45495  }
45496
45497  if( rc==SQLITE_OK ){
45498    winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
45499  }
45500
45501  return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
45502}
45503
45504/*
45505** Open the shared-memory area associated with database file pDbFd.
45506**
45507** When opening a new shared-memory file, if no other instances of that
45508** file are currently open, in this process or in other processes, then
45509** the file must be truncated to zero length or have its header cleared.
45510*/
45511static int winOpenSharedMemory(winFile *pDbFd){
45512  struct winShm *p;                  /* The connection to be opened */
45513  winShmNode *pShmNode = 0;          /* The underlying mmapped file */
45514  int rc = SQLITE_OK;                /* Result code */
45515  winShmNode *pNew;                  /* Newly allocated winShmNode */
45516  int nName;                         /* Size of zName in bytes */
45517
45518  assert( pDbFd->pShm==0 );    /* Not previously opened */
45519
45520  /* Allocate space for the new sqlite3_shm object.  Also speculatively
45521  ** allocate space for a new winShmNode and filename.
45522  */
45523  p = sqlite3MallocZero( sizeof(*p) );
45524  if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT;
45525  nName = sqlite3Strlen30(pDbFd->zPath);
45526  pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
45527  if( pNew==0 ){
45528    sqlite3_free(p);
45529    return SQLITE_IOERR_NOMEM_BKPT;
45530  }
45531  pNew->zFilename = (char*)&pNew[1];
45532  sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
45533  sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
45534
45535  /* Look to see if there is an existing winShmNode that can be used.
45536  ** If no matching winShmNode currently exists, create a new one.
45537  */
45538  winShmEnterMutex();
45539  for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
45540    /* TBD need to come up with better match here.  Perhaps
45541    ** use FILE_ID_BOTH_DIR_INFO Structure.
45542    */
45543    if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
45544  }
45545  if( pShmNode ){
45546    sqlite3_free(pNew);
45547  }else{
45548    int inFlags = SQLITE_OPEN_WAL;
45549    int outFlags = 0;
45550
45551    pShmNode = pNew;
45552    pNew = 0;
45553    ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
45554    pShmNode->pNext = winShmNodeList;
45555    winShmNodeList = pShmNode;
45556
45557    if( sqlite3GlobalConfig.bCoreMutex ){
45558      pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
45559      if( pShmNode->mutex==0 ){
45560        rc = SQLITE_IOERR_NOMEM_BKPT;
45561        goto shm_open_err;
45562      }
45563    }
45564
45565    if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
45566      inFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
45567    }else{
45568      inFlags |= SQLITE_OPEN_READONLY;
45569    }
45570    rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
45571                 (sqlite3_file*)&pShmNode->hFile,
45572                 inFlags, &outFlags);
45573    if( rc!=SQLITE_OK ){
45574      rc = winLogError(rc, osGetLastError(), "winOpenShm",
45575                       pShmNode->zFilename);
45576      goto shm_open_err;
45577    }
45578    if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
45579
45580    rc = winLockSharedMemory(pShmNode);
45581    if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
45582  }
45583
45584  /* Make the new connection a child of the winShmNode */
45585  p->pShmNode = pShmNode;
45586#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
45587  p->id = pShmNode->nextShmId++;
45588#endif
45589  pShmNode->nRef++;
45590  pDbFd->pShm = p;
45591  winShmLeaveMutex();
45592
45593  /* The reference count on pShmNode has already been incremented under
45594  ** the cover of the winShmEnterMutex() mutex and the pointer from the
45595  ** new (struct winShm) object to the pShmNode has been set. All that is
45596  ** left to do is to link the new object into the linked list starting
45597  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
45598  ** mutex.
45599  */
45600  sqlite3_mutex_enter(pShmNode->mutex);
45601  p->pNext = pShmNode->pFirst;
45602  pShmNode->pFirst = p;
45603  sqlite3_mutex_leave(pShmNode->mutex);
45604  return rc;
45605
45606  /* Jump here on any error */
45607shm_open_err:
45608  winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
45609  winShmPurge(pDbFd->pVfs, 0);      /* This call frees pShmNode if required */
45610  sqlite3_free(p);
45611  sqlite3_free(pNew);
45612  winShmLeaveMutex();
45613  return rc;
45614}
45615
45616/*
45617** Close a connection to shared-memory.  Delete the underlying
45618** storage if deleteFlag is true.
45619*/
45620static int winShmUnmap(
45621  sqlite3_file *fd,          /* Database holding shared memory */
45622  int deleteFlag             /* Delete after closing if true */
45623){
45624  winFile *pDbFd;       /* Database holding shared-memory */
45625  winShm *p;            /* The connection to be closed */
45626  winShmNode *pShmNode; /* The underlying shared-memory file */
45627  winShm **pp;          /* For looping over sibling connections */
45628
45629  pDbFd = (winFile*)fd;
45630  p = pDbFd->pShm;
45631  if( p==0 ) return SQLITE_OK;
45632  pShmNode = p->pShmNode;
45633
45634  /* Remove connection p from the set of connections associated
45635  ** with pShmNode */
45636  sqlite3_mutex_enter(pShmNode->mutex);
45637  for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
45638  *pp = p->pNext;
45639
45640  /* Free the connection p */
45641  sqlite3_free(p);
45642  pDbFd->pShm = 0;
45643  sqlite3_mutex_leave(pShmNode->mutex);
45644
45645  /* If pShmNode->nRef has reached 0, then close the underlying
45646  ** shared-memory file, too */
45647  winShmEnterMutex();
45648  assert( pShmNode->nRef>0 );
45649  pShmNode->nRef--;
45650  if( pShmNode->nRef==0 ){
45651    winShmPurge(pDbFd->pVfs, deleteFlag);
45652  }
45653  winShmLeaveMutex();
45654
45655  return SQLITE_OK;
45656}
45657
45658/*
45659** Change the lock state for a shared-memory segment.
45660*/
45661static int winShmLock(
45662  sqlite3_file *fd,          /* Database file holding the shared memory */
45663  int ofst,                  /* First lock to acquire or release */
45664  int n,                     /* Number of locks to acquire or release */
45665  int flags                  /* What to do with the lock */
45666){
45667  winFile *pDbFd = (winFile*)fd;        /* Connection holding shared memory */
45668  winShm *p = pDbFd->pShm;              /* The shared memory being locked */
45669  winShm *pX;                           /* For looping over all siblings */
45670  winShmNode *pShmNode = p->pShmNode;
45671  int rc = SQLITE_OK;                   /* Result code */
45672  u16 mask;                             /* Mask of locks to take or release */
45673
45674  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
45675  assert( n>=1 );
45676  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
45677       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
45678       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
45679       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
45680  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
45681
45682  mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
45683  assert( n>1 || mask==(1<<ofst) );
45684  sqlite3_mutex_enter(pShmNode->mutex);
45685  if( flags & SQLITE_SHM_UNLOCK ){
45686    u16 allMask = 0; /* Mask of locks held by siblings */
45687
45688    /* See if any siblings hold this same lock */
45689    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
45690      if( pX==p ) continue;
45691      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
45692      allMask |= pX->sharedMask;
45693    }
45694
45695    /* Unlock the system-level locks */
45696    if( (mask & allMask)==0 ){
45697      rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
45698    }else{
45699      rc = SQLITE_OK;
45700    }
45701
45702    /* Undo the local locks */
45703    if( rc==SQLITE_OK ){
45704      p->exclMask &= ~mask;
45705      p->sharedMask &= ~mask;
45706    }
45707  }else if( flags & SQLITE_SHM_SHARED ){
45708    u16 allShared = 0;  /* Union of locks held by connections other than "p" */
45709
45710    /* Find out which shared locks are already held by sibling connections.
45711    ** If any sibling already holds an exclusive lock, go ahead and return
45712    ** SQLITE_BUSY.
45713    */
45714    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
45715      if( (pX->exclMask & mask)!=0 ){
45716        rc = SQLITE_BUSY;
45717        break;
45718      }
45719      allShared |= pX->sharedMask;
45720    }
45721
45722    /* Get shared locks at the system level, if necessary */
45723    if( rc==SQLITE_OK ){
45724      if( (allShared & mask)==0 ){
45725        rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
45726      }else{
45727        rc = SQLITE_OK;
45728      }
45729    }
45730
45731    /* Get the local shared locks */
45732    if( rc==SQLITE_OK ){
45733      p->sharedMask |= mask;
45734    }
45735  }else{
45736    /* Make sure no sibling connections hold locks that will block this
45737    ** lock.  If any do, return SQLITE_BUSY right away.
45738    */
45739    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
45740      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
45741        rc = SQLITE_BUSY;
45742        break;
45743      }
45744    }
45745
45746    /* Get the exclusive locks at the system level.  Then if successful
45747    ** also mark the local connection as being locked.
45748    */
45749    if( rc==SQLITE_OK ){
45750      rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
45751      if( rc==SQLITE_OK ){
45752        assert( (p->sharedMask & mask)==0 );
45753        p->exclMask |= mask;
45754      }
45755    }
45756  }
45757  sqlite3_mutex_leave(pShmNode->mutex);
45758  OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
45759           osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
45760           sqlite3ErrName(rc)));
45761  return rc;
45762}
45763
45764/*
45765** Implement a memory barrier or memory fence on shared memory.
45766**
45767** All loads and stores begun before the barrier must complete before
45768** any load or store begun after the barrier.
45769*/
45770static void winShmBarrier(
45771  sqlite3_file *fd          /* Database holding the shared memory */
45772){
45773  UNUSED_PARAMETER(fd);
45774  sqlite3MemoryBarrier();   /* compiler-defined memory barrier */
45775  winShmEnterMutex();       /* Also mutex, for redundancy */
45776  winShmLeaveMutex();
45777}
45778
45779/*
45780** This function is called to obtain a pointer to region iRegion of the
45781** shared-memory associated with the database file fd. Shared-memory regions
45782** are numbered starting from zero. Each shared-memory region is szRegion
45783** bytes in size.
45784**
45785** If an error occurs, an error code is returned and *pp is set to NULL.
45786**
45787** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
45788** region has not been allocated (by any client, including one running in a
45789** separate process), then *pp is set to NULL and SQLITE_OK returned. If
45790** isWrite is non-zero and the requested shared-memory region has not yet
45791** been allocated, it is allocated by this function.
45792**
45793** If the shared-memory region has already been allocated or is allocated by
45794** this call as described above, then it is mapped into this processes
45795** address space (if it is not already), *pp is set to point to the mapped
45796** memory and SQLITE_OK returned.
45797*/
45798static int winShmMap(
45799  sqlite3_file *fd,               /* Handle open on database file */
45800  int iRegion,                    /* Region to retrieve */
45801  int szRegion,                   /* Size of regions */
45802  int isWrite,                    /* True to extend file if necessary */
45803  void volatile **pp              /* OUT: Mapped memory */
45804){
45805  winFile *pDbFd = (winFile*)fd;
45806  winShm *pShm = pDbFd->pShm;
45807  winShmNode *pShmNode;
45808  DWORD protect = PAGE_READWRITE;
45809  DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ;
45810  int rc = SQLITE_OK;
45811
45812  if( !pShm ){
45813    rc = winOpenSharedMemory(pDbFd);
45814    if( rc!=SQLITE_OK ) return rc;
45815    pShm = pDbFd->pShm;
45816    assert( pShm!=0 );
45817  }
45818  pShmNode = pShm->pShmNode;
45819
45820  sqlite3_mutex_enter(pShmNode->mutex);
45821  if( pShmNode->isUnlocked ){
45822    rc = winLockSharedMemory(pShmNode);
45823    if( rc!=SQLITE_OK ) goto shmpage_out;
45824    pShmNode->isUnlocked = 0;
45825  }
45826  assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
45827
45828  if( pShmNode->nRegion<=iRegion ){
45829    struct ShmRegion *apNew;           /* New aRegion[] array */
45830    int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
45831    sqlite3_int64 sz;                  /* Current size of wal-index file */
45832
45833    pShmNode->szRegion = szRegion;
45834
45835    /* The requested region is not mapped into this processes address space.
45836    ** Check to see if it has been allocated (i.e. if the wal-index file is
45837    ** large enough to contain the requested region).
45838    */
45839    rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
45840    if( rc!=SQLITE_OK ){
45841      rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
45842                       "winShmMap1", pDbFd->zPath);
45843      goto shmpage_out;
45844    }
45845
45846    if( sz<nByte ){
45847      /* The requested memory region does not exist. If isWrite is set to
45848      ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
45849      **
45850      ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
45851      ** the requested memory region.
45852      */
45853      if( !isWrite ) goto shmpage_out;
45854      rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
45855      if( rc!=SQLITE_OK ){
45856        rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
45857                         "winShmMap2", pDbFd->zPath);
45858        goto shmpage_out;
45859      }
45860    }
45861
45862    /* Map the requested memory region into this processes address space. */
45863    apNew = (struct ShmRegion *)sqlite3_realloc64(
45864        pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
45865    );
45866    if( !apNew ){
45867      rc = SQLITE_IOERR_NOMEM_BKPT;
45868      goto shmpage_out;
45869    }
45870    pShmNode->aRegion = apNew;
45871
45872    if( pShmNode->isReadonly ){
45873      protect = PAGE_READONLY;
45874      flags = FILE_MAP_READ;
45875    }
45876
45877    while( pShmNode->nRegion<=iRegion ){
45878      HANDLE hMap = NULL;         /* file-mapping handle */
45879      void *pMap = 0;             /* Mapped memory region */
45880
45881#if SQLITE_OS_WINRT
45882      hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
45883          NULL, protect, nByte, NULL
45884      );
45885#elif defined(SQLITE_WIN32_HAS_WIDE)
45886      hMap = osCreateFileMappingW(pShmNode->hFile.h,
45887          NULL, protect, 0, nByte, NULL
45888      );
45889#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
45890      hMap = osCreateFileMappingA(pShmNode->hFile.h,
45891          NULL, protect, 0, nByte, NULL
45892      );
45893#endif
45894      OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
45895               osGetCurrentProcessId(), pShmNode->nRegion, nByte,
45896               hMap ? "ok" : "failed"));
45897      if( hMap ){
45898        int iOffset = pShmNode->nRegion*szRegion;
45899        int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
45900#if SQLITE_OS_WINRT
45901        pMap = osMapViewOfFileFromApp(hMap, flags,
45902            iOffset - iOffsetShift, szRegion + iOffsetShift
45903        );
45904#else
45905        pMap = osMapViewOfFile(hMap, flags,
45906            0, iOffset - iOffsetShift, szRegion + iOffsetShift
45907        );
45908#endif
45909        OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
45910                 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
45911                 szRegion, pMap ? "ok" : "failed"));
45912      }
45913      if( !pMap ){
45914        pShmNode->lastErrno = osGetLastError();
45915        rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
45916                         "winShmMap3", pDbFd->zPath);
45917        if( hMap ) osCloseHandle(hMap);
45918        goto shmpage_out;
45919      }
45920
45921      pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
45922      pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
45923      pShmNode->nRegion++;
45924    }
45925  }
45926
45927shmpage_out:
45928  if( pShmNode->nRegion>iRegion ){
45929    int iOffset = iRegion*szRegion;
45930    int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
45931    char *p = (char *)pShmNode->aRegion[iRegion].pMap;
45932    *pp = (void *)&p[iOffsetShift];
45933  }else{
45934    *pp = 0;
45935  }
45936  if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
45937  sqlite3_mutex_leave(pShmNode->mutex);
45938  return rc;
45939}
45940
45941#else
45942# define winShmMap     0
45943# define winShmLock    0
45944# define winShmBarrier 0
45945# define winShmUnmap   0
45946#endif /* #ifndef SQLITE_OMIT_WAL */
45947
45948/*
45949** Cleans up the mapped region of the specified file, if any.
45950*/
45951#if SQLITE_MAX_MMAP_SIZE>0
45952static int winUnmapfile(winFile *pFile){
45953  assert( pFile!=0 );
45954  OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
45955           "mmapSize=%lld, mmapSizeMax=%lld\n",
45956           osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
45957           pFile->mmapSize, pFile->mmapSizeMax));
45958  if( pFile->pMapRegion ){
45959    if( !osUnmapViewOfFile(pFile->pMapRegion) ){
45960      pFile->lastErrno = osGetLastError();
45961      OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
45962               "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
45963               pFile->pMapRegion));
45964      return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
45965                         "winUnmapfile1", pFile->zPath);
45966    }
45967    pFile->pMapRegion = 0;
45968    pFile->mmapSize = 0;
45969  }
45970  if( pFile->hMap!=NULL ){
45971    if( !osCloseHandle(pFile->hMap) ){
45972      pFile->lastErrno = osGetLastError();
45973      OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
45974               osGetCurrentProcessId(), pFile, pFile->hMap));
45975      return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
45976                         "winUnmapfile2", pFile->zPath);
45977    }
45978    pFile->hMap = NULL;
45979  }
45980  OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
45981           osGetCurrentProcessId(), pFile));
45982  return SQLITE_OK;
45983}
45984
45985/*
45986** Memory map or remap the file opened by file-descriptor pFd (if the file
45987** is already mapped, the existing mapping is replaced by the new). Or, if
45988** there already exists a mapping for this file, and there are still
45989** outstanding xFetch() references to it, this function is a no-op.
45990**
45991** If parameter nByte is non-negative, then it is the requested size of
45992** the mapping to create. Otherwise, if nByte is less than zero, then the
45993** requested size is the size of the file on disk. The actual size of the
45994** created mapping is either the requested size or the value configured
45995** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
45996**
45997** SQLITE_OK is returned if no error occurs (even if the mapping is not
45998** recreated as a result of outstanding references) or an SQLite error
45999** code otherwise.
46000*/
46001static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
46002  sqlite3_int64 nMap = nByte;
46003  int rc;
46004
46005  assert( nMap>=0 || pFd->nFetchOut==0 );
46006  OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
46007           osGetCurrentProcessId(), pFd, nByte));
46008
46009  if( pFd->nFetchOut>0 ) return SQLITE_OK;
46010
46011  if( nMap<0 ){
46012    rc = winFileSize((sqlite3_file*)pFd, &nMap);
46013    if( rc ){
46014      OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
46015               osGetCurrentProcessId(), pFd));
46016      return SQLITE_IOERR_FSTAT;
46017    }
46018  }
46019  if( nMap>pFd->mmapSizeMax ){
46020    nMap = pFd->mmapSizeMax;
46021  }
46022  nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
46023
46024  if( nMap==0 && pFd->mmapSize>0 ){
46025    winUnmapfile(pFd);
46026  }
46027  if( nMap!=pFd->mmapSize ){
46028    void *pNew = 0;
46029    DWORD protect = PAGE_READONLY;
46030    DWORD flags = FILE_MAP_READ;
46031
46032    winUnmapfile(pFd);
46033#ifdef SQLITE_MMAP_READWRITE
46034    if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
46035      protect = PAGE_READWRITE;
46036      flags |= FILE_MAP_WRITE;
46037    }
46038#endif
46039#if SQLITE_OS_WINRT
46040    pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
46041#elif defined(SQLITE_WIN32_HAS_WIDE)
46042    pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
46043                                (DWORD)((nMap>>32) & 0xffffffff),
46044                                (DWORD)(nMap & 0xffffffff), NULL);
46045#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
46046    pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
46047                                (DWORD)((nMap>>32) & 0xffffffff),
46048                                (DWORD)(nMap & 0xffffffff), NULL);
46049#endif
46050    if( pFd->hMap==NULL ){
46051      pFd->lastErrno = osGetLastError();
46052      rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
46053                       "winMapfile1", pFd->zPath);
46054      /* Log the error, but continue normal operation using xRead/xWrite */
46055      OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
46056               osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
46057      return SQLITE_OK;
46058    }
46059    assert( (nMap % winSysInfo.dwPageSize)==0 );
46060    assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
46061#if SQLITE_OS_WINRT
46062    pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
46063#else
46064    pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
46065#endif
46066    if( pNew==NULL ){
46067      osCloseHandle(pFd->hMap);
46068      pFd->hMap = NULL;
46069      pFd->lastErrno = osGetLastError();
46070      rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
46071                       "winMapfile2", pFd->zPath);
46072      /* Log the error, but continue normal operation using xRead/xWrite */
46073      OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
46074               osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
46075      return SQLITE_OK;
46076    }
46077    pFd->pMapRegion = pNew;
46078    pFd->mmapSize = nMap;
46079  }
46080
46081  OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
46082           osGetCurrentProcessId(), pFd));
46083  return SQLITE_OK;
46084}
46085#endif /* SQLITE_MAX_MMAP_SIZE>0 */
46086
46087/*
46088** If possible, return a pointer to a mapping of file fd starting at offset
46089** iOff. The mapping must be valid for at least nAmt bytes.
46090**
46091** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
46092** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
46093** Finally, if an error does occur, return an SQLite error code. The final
46094** value of *pp is undefined in this case.
46095**
46096** If this function does return a pointer, the caller must eventually
46097** release the reference by calling winUnfetch().
46098*/
46099static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
46100#if SQLITE_MAX_MMAP_SIZE>0
46101  winFile *pFd = (winFile*)fd;   /* The underlying database file */
46102#endif
46103  *pp = 0;
46104
46105  OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
46106           osGetCurrentProcessId(), fd, iOff, nAmt, pp));
46107
46108#if SQLITE_MAX_MMAP_SIZE>0
46109  if( pFd->mmapSizeMax>0 ){
46110    if( pFd->pMapRegion==0 ){
46111      int rc = winMapfile(pFd, -1);
46112      if( rc!=SQLITE_OK ){
46113        OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
46114                 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
46115        return rc;
46116      }
46117    }
46118    if( pFd->mmapSize >= iOff+nAmt ){
46119      assert( pFd->pMapRegion!=0 );
46120      *pp = &((u8 *)pFd->pMapRegion)[iOff];
46121      pFd->nFetchOut++;
46122    }
46123  }
46124#endif
46125
46126  OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
46127           osGetCurrentProcessId(), fd, pp, *pp));
46128  return SQLITE_OK;
46129}
46130
46131/*
46132** If the third argument is non-NULL, then this function releases a
46133** reference obtained by an earlier call to winFetch(). The second
46134** argument passed to this function must be the same as the corresponding
46135** argument that was passed to the winFetch() invocation.
46136**
46137** Or, if the third argument is NULL, then this function is being called
46138** to inform the VFS layer that, according to POSIX, any existing mapping
46139** may now be invalid and should be unmapped.
46140*/
46141static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
46142#if SQLITE_MAX_MMAP_SIZE>0
46143  winFile *pFd = (winFile*)fd;   /* The underlying database file */
46144
46145  /* If p==0 (unmap the entire file) then there must be no outstanding
46146  ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
46147  ** then there must be at least one outstanding.  */
46148  assert( (p==0)==(pFd->nFetchOut==0) );
46149
46150  /* If p!=0, it must match the iOff value. */
46151  assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
46152
46153  OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
46154           osGetCurrentProcessId(), pFd, iOff, p));
46155
46156  if( p ){
46157    pFd->nFetchOut--;
46158  }else{
46159    /* FIXME:  If Windows truly always prevents truncating or deleting a
46160    ** file while a mapping is held, then the following winUnmapfile() call
46161    ** is unnecessary can be omitted - potentially improving
46162    ** performance.  */
46163    winUnmapfile(pFd);
46164  }
46165
46166  assert( pFd->nFetchOut>=0 );
46167#endif
46168
46169  OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
46170           osGetCurrentProcessId(), fd));
46171  return SQLITE_OK;
46172}
46173
46174/*
46175** Here ends the implementation of all sqlite3_file methods.
46176**
46177********************** End sqlite3_file Methods *******************************
46178******************************************************************************/
46179
46180/*
46181** This vector defines all the methods that can operate on an
46182** sqlite3_file for win32.
46183*/
46184static const sqlite3_io_methods winIoMethod = {
46185  3,                              /* iVersion */
46186  winClose,                       /* xClose */
46187  winRead,                        /* xRead */
46188  winWrite,                       /* xWrite */
46189  winTruncate,                    /* xTruncate */
46190  winSync,                        /* xSync */
46191  winFileSize,                    /* xFileSize */
46192  winLock,                        /* xLock */
46193  winUnlock,                      /* xUnlock */
46194  winCheckReservedLock,           /* xCheckReservedLock */
46195  winFileControl,                 /* xFileControl */
46196  winSectorSize,                  /* xSectorSize */
46197  winDeviceCharacteristics,       /* xDeviceCharacteristics */
46198  winShmMap,                      /* xShmMap */
46199  winShmLock,                     /* xShmLock */
46200  winShmBarrier,                  /* xShmBarrier */
46201  winShmUnmap,                    /* xShmUnmap */
46202  winFetch,                       /* xFetch */
46203  winUnfetch                      /* xUnfetch */
46204};
46205
46206/*
46207** This vector defines all the methods that can operate on an
46208** sqlite3_file for win32 without performing any locking.
46209*/
46210static const sqlite3_io_methods winIoNolockMethod = {
46211  3,                              /* iVersion */
46212  winClose,                       /* xClose */
46213  winRead,                        /* xRead */
46214  winWrite,                       /* xWrite */
46215  winTruncate,                    /* xTruncate */
46216  winSync,                        /* xSync */
46217  winFileSize,                    /* xFileSize */
46218  winNolockLock,                  /* xLock */
46219  winNolockUnlock,                /* xUnlock */
46220  winNolockCheckReservedLock,     /* xCheckReservedLock */
46221  winFileControl,                 /* xFileControl */
46222  winSectorSize,                  /* xSectorSize */
46223  winDeviceCharacteristics,       /* xDeviceCharacteristics */
46224  winShmMap,                      /* xShmMap */
46225  winShmLock,                     /* xShmLock */
46226  winShmBarrier,                  /* xShmBarrier */
46227  winShmUnmap,                    /* xShmUnmap */
46228  winFetch,                       /* xFetch */
46229  winUnfetch                      /* xUnfetch */
46230};
46231
46232static winVfsAppData winAppData = {
46233  &winIoMethod,       /* pMethod */
46234  0,                  /* pAppData */
46235  0                   /* bNoLock */
46236};
46237
46238static winVfsAppData winNolockAppData = {
46239  &winIoNolockMethod, /* pMethod */
46240  0,                  /* pAppData */
46241  1                   /* bNoLock */
46242};
46243
46244/****************************************************************************
46245**************************** sqlite3_vfs methods ****************************
46246**
46247** This division contains the implementation of methods on the
46248** sqlite3_vfs object.
46249*/
46250
46251#if defined(__CYGWIN__)
46252/*
46253** Convert a filename from whatever the underlying operating system
46254** supports for filenames into UTF-8.  Space to hold the result is
46255** obtained from malloc and must be freed by the calling function.
46256*/
46257static char *winConvertToUtf8Filename(const void *zFilename){
46258  char *zConverted = 0;
46259  if( osIsNT() ){
46260    zConverted = winUnicodeToUtf8(zFilename);
46261  }
46262#ifdef SQLITE_WIN32_HAS_ANSI
46263  else{
46264    zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
46265  }
46266#endif
46267  /* caller will handle out of memory */
46268  return zConverted;
46269}
46270#endif
46271
46272/*
46273** Convert a UTF-8 filename into whatever form the underlying
46274** operating system wants filenames in.  Space to hold the result
46275** is obtained from malloc and must be freed by the calling
46276** function.
46277*/
46278static void *winConvertFromUtf8Filename(const char *zFilename){
46279  void *zConverted = 0;
46280  if( osIsNT() ){
46281    zConverted = winUtf8ToUnicode(zFilename);
46282  }
46283#ifdef SQLITE_WIN32_HAS_ANSI
46284  else{
46285    zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
46286  }
46287#endif
46288  /* caller will handle out of memory */
46289  return zConverted;
46290}
46291
46292/*
46293** This function returns non-zero if the specified UTF-8 string buffer
46294** ends with a directory separator character or one was successfully
46295** added to it.
46296*/
46297static int winMakeEndInDirSep(int nBuf, char *zBuf){
46298  if( zBuf ){
46299    int nLen = sqlite3Strlen30(zBuf);
46300    if( nLen>0 ){
46301      if( winIsDirSep(zBuf[nLen-1]) ){
46302        return 1;
46303      }else if( nLen+1<nBuf ){
46304        zBuf[nLen] = winGetDirSep();
46305        zBuf[nLen+1] = '\0';
46306        return 1;
46307      }
46308    }
46309  }
46310  return 0;
46311}
46312
46313/*
46314** Create a temporary file name and store the resulting pointer into pzBuf.
46315** The pointer returned in pzBuf must be freed via sqlite3_free().
46316*/
46317static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
46318  static char zChars[] =
46319    "abcdefghijklmnopqrstuvwxyz"
46320    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
46321    "0123456789";
46322  size_t i, j;
46323  int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
46324  int nMax, nBuf, nDir, nLen;
46325  char *zBuf;
46326
46327  /* It's odd to simulate an io-error here, but really this is just
46328  ** using the io-error infrastructure to test that SQLite handles this
46329  ** function failing.
46330  */
46331  SimulateIOError( return SQLITE_IOERR );
46332
46333  /* Allocate a temporary buffer to store the fully qualified file
46334  ** name for the temporary file.  If this fails, we cannot continue.
46335  */
46336  nMax = pVfs->mxPathname; nBuf = nMax + 2;
46337  zBuf = sqlite3MallocZero( nBuf );
46338  if( !zBuf ){
46339    OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46340    return SQLITE_IOERR_NOMEM_BKPT;
46341  }
46342
46343  /* Figure out the effective temporary directory.  First, check if one
46344  ** has been explicitly set by the application; otherwise, use the one
46345  ** configured by the operating system.
46346  */
46347  nDir = nMax - (nPre + 15);
46348  assert( nDir>0 );
46349  if( sqlite3_temp_directory ){
46350    int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
46351    if( nDirLen>0 ){
46352      if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
46353        nDirLen++;
46354      }
46355      if( nDirLen>nDir ){
46356        sqlite3_free(zBuf);
46357        OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
46358        return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
46359      }
46360      sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
46361    }
46362  }
46363#if defined(__CYGWIN__)
46364  else{
46365    static const char *azDirs[] = {
46366       0, /* getenv("SQLITE_TMPDIR") */
46367       0, /* getenv("TMPDIR") */
46368       0, /* getenv("TMP") */
46369       0, /* getenv("TEMP") */
46370       0, /* getenv("USERPROFILE") */
46371       "/var/tmp",
46372       "/usr/tmp",
46373       "/tmp",
46374       ".",
46375       0        /* List terminator */
46376    };
46377    unsigned int i;
46378    const char *zDir = 0;
46379
46380    if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
46381    if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
46382    if( !azDirs[2] ) azDirs[2] = getenv("TMP");
46383    if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
46384    if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
46385    for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
46386      void *zConverted;
46387      if( zDir==0 ) continue;
46388      /* If the path starts with a drive letter followed by the colon
46389      ** character, assume it is already a native Win32 path; otherwise,
46390      ** it must be converted to a native Win32 path via the Cygwin API
46391      ** prior to using it.
46392      */
46393      if( winIsDriveLetterAndColon(zDir) ){
46394        zConverted = winConvertFromUtf8Filename(zDir);
46395        if( !zConverted ){
46396          sqlite3_free(zBuf);
46397          OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46398          return SQLITE_IOERR_NOMEM_BKPT;
46399        }
46400        if( winIsDir(zConverted) ){
46401          sqlite3_snprintf(nMax, zBuf, "%s", zDir);
46402          sqlite3_free(zConverted);
46403          break;
46404        }
46405        sqlite3_free(zConverted);
46406      }else{
46407        zConverted = sqlite3MallocZero( nMax+1 );
46408        if( !zConverted ){
46409          sqlite3_free(zBuf);
46410          OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46411          return SQLITE_IOERR_NOMEM_BKPT;
46412        }
46413        if( cygwin_conv_path(
46414                osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
46415                zConverted, nMax+1)<0 ){
46416          sqlite3_free(zConverted);
46417          sqlite3_free(zBuf);
46418          OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
46419          return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
46420                             "winGetTempname2", zDir);
46421        }
46422        if( winIsDir(zConverted) ){
46423          /* At this point, we know the candidate directory exists and should
46424          ** be used.  However, we may need to convert the string containing
46425          ** its name into UTF-8 (i.e. if it is UTF-16 right now).
46426          */
46427          char *zUtf8 = winConvertToUtf8Filename(zConverted);
46428          if( !zUtf8 ){
46429            sqlite3_free(zConverted);
46430            sqlite3_free(zBuf);
46431            OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46432            return SQLITE_IOERR_NOMEM_BKPT;
46433          }
46434          sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
46435          sqlite3_free(zUtf8);
46436          sqlite3_free(zConverted);
46437          break;
46438        }
46439        sqlite3_free(zConverted);
46440      }
46441    }
46442  }
46443#elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
46444  else if( osIsNT() ){
46445    char *zMulti;
46446    LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
46447    if( !zWidePath ){
46448      sqlite3_free(zBuf);
46449      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46450      return SQLITE_IOERR_NOMEM_BKPT;
46451    }
46452    if( osGetTempPathW(nMax, zWidePath)==0 ){
46453      sqlite3_free(zWidePath);
46454      sqlite3_free(zBuf);
46455      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
46456      return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
46457                         "winGetTempname2", 0);
46458    }
46459    zMulti = winUnicodeToUtf8(zWidePath);
46460    if( zMulti ){
46461      sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
46462      sqlite3_free(zMulti);
46463      sqlite3_free(zWidePath);
46464    }else{
46465      sqlite3_free(zWidePath);
46466      sqlite3_free(zBuf);
46467      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46468      return SQLITE_IOERR_NOMEM_BKPT;
46469    }
46470  }
46471#ifdef SQLITE_WIN32_HAS_ANSI
46472  else{
46473    char *zUtf8;
46474    char *zMbcsPath = sqlite3MallocZero( nMax );
46475    if( !zMbcsPath ){
46476      sqlite3_free(zBuf);
46477      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46478      return SQLITE_IOERR_NOMEM_BKPT;
46479    }
46480    if( osGetTempPathA(nMax, zMbcsPath)==0 ){
46481      sqlite3_free(zBuf);
46482      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
46483      return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
46484                         "winGetTempname3", 0);
46485    }
46486    zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
46487    if( zUtf8 ){
46488      sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
46489      sqlite3_free(zUtf8);
46490    }else{
46491      sqlite3_free(zBuf);
46492      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46493      return SQLITE_IOERR_NOMEM_BKPT;
46494    }
46495  }
46496#endif /* SQLITE_WIN32_HAS_ANSI */
46497#endif /* !SQLITE_OS_WINRT */
46498
46499  /*
46500  ** Check to make sure the temporary directory ends with an appropriate
46501  ** separator.  If it does not and there is not enough space left to add
46502  ** one, fail.
46503  */
46504  if( !winMakeEndInDirSep(nDir+1, zBuf) ){
46505    sqlite3_free(zBuf);
46506    OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
46507    return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
46508  }
46509
46510  /*
46511  ** Check that the output buffer is large enough for the temporary file
46512  ** name in the following format:
46513  **
46514  **   "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
46515  **
46516  ** If not, return SQLITE_ERROR.  The number 17 is used here in order to
46517  ** account for the space used by the 15 character random suffix and the
46518  ** two trailing NUL characters.  The final directory separator character
46519  ** has already added if it was not already present.
46520  */
46521  nLen = sqlite3Strlen30(zBuf);
46522  if( (nLen + nPre + 17) > nBuf ){
46523    sqlite3_free(zBuf);
46524    OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
46525    return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0);
46526  }
46527
46528  sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
46529
46530  j = sqlite3Strlen30(zBuf);
46531  sqlite3_randomness(15, &zBuf[j]);
46532  for(i=0; i<15; i++, j++){
46533    zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
46534  }
46535  zBuf[j] = 0;
46536  zBuf[j+1] = 0;
46537  *pzBuf = zBuf;
46538
46539  OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
46540  return SQLITE_OK;
46541}
46542
46543/*
46544** Return TRUE if the named file is really a directory.  Return false if
46545** it is something other than a directory, or if there is any kind of memory
46546** allocation failure.
46547*/
46548static int winIsDir(const void *zConverted){
46549  DWORD attr;
46550  int rc = 0;
46551  DWORD lastErrno;
46552
46553  if( osIsNT() ){
46554    int cnt = 0;
46555    WIN32_FILE_ATTRIBUTE_DATA sAttrData;
46556    memset(&sAttrData, 0, sizeof(sAttrData));
46557    while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
46558                             GetFileExInfoStandard,
46559                             &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
46560    if( !rc ){
46561      return 0; /* Invalid name? */
46562    }
46563    attr = sAttrData.dwFileAttributes;
46564#if SQLITE_OS_WINCE==0
46565  }else{
46566    attr = osGetFileAttributesA((char*)zConverted);
46567#endif
46568  }
46569  return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
46570}
46571
46572/* forward reference */
46573static int winAccess(
46574  sqlite3_vfs *pVfs,         /* Not used on win32 */
46575  const char *zFilename,     /* Name of file to check */
46576  int flags,                 /* Type of test to make on this file */
46577  int *pResOut               /* OUT: Result */
46578);
46579
46580/*
46581** Open a file.
46582*/
46583static int winOpen(
46584  sqlite3_vfs *pVfs,        /* Used to get maximum path length and AppData */
46585  const char *zName,        /* Name of the file (UTF-8) */
46586  sqlite3_file *id,         /* Write the SQLite file handle here */
46587  int flags,                /* Open mode flags */
46588  int *pOutFlags            /* Status return flags */
46589){
46590  HANDLE h;
46591  DWORD lastErrno = 0;
46592  DWORD dwDesiredAccess;
46593  DWORD dwShareMode;
46594  DWORD dwCreationDisposition;
46595  DWORD dwFlagsAndAttributes = 0;
46596#if SQLITE_OS_WINCE
46597  int isTemp = 0;
46598#endif
46599  winVfsAppData *pAppData;
46600  winFile *pFile = (winFile*)id;
46601  void *zConverted;              /* Filename in OS encoding */
46602  const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
46603  int cnt = 0;
46604
46605  /* If argument zPath is a NULL pointer, this function is required to open
46606  ** a temporary file. Use this buffer to store the file name in.
46607  */
46608  char *zTmpname = 0; /* For temporary filename, if necessary. */
46609
46610  int rc = SQLITE_OK;            /* Function Return Code */
46611#if !defined(NDEBUG) || SQLITE_OS_WINCE
46612  int eType = flags&0xFFFFFF00;  /* Type of file to open */
46613#endif
46614
46615  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
46616  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
46617  int isCreate     = (flags & SQLITE_OPEN_CREATE);
46618  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
46619  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
46620
46621#ifndef NDEBUG
46622  int isOpenJournal = (isCreate && (
46623        eType==SQLITE_OPEN_MASTER_JOURNAL
46624     || eType==SQLITE_OPEN_MAIN_JOURNAL
46625     || eType==SQLITE_OPEN_WAL
46626  ));
46627#endif
46628
46629  OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
46630           zUtf8Name, id, flags, pOutFlags));
46631
46632  /* Check the following statements are true:
46633  **
46634  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
46635  **   (b) if CREATE is set, then READWRITE must also be set, and
46636  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
46637  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
46638  */
46639  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
46640  assert(isCreate==0 || isReadWrite);
46641  assert(isExclusive==0 || isCreate);
46642  assert(isDelete==0 || isCreate);
46643
46644  /* The main DB, main journal, WAL file and master journal are never
46645  ** automatically deleted. Nor are they ever temporary files.  */
46646  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
46647  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
46648  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
46649  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
46650
46651  /* Assert that the upper layer has set one of the "file-type" flags. */
46652  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
46653       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
46654       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
46655       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
46656  );
46657
46658  assert( pFile!=0 );
46659  memset(pFile, 0, sizeof(winFile));
46660  pFile->h = INVALID_HANDLE_VALUE;
46661
46662#if SQLITE_OS_WINRT
46663  if( !zUtf8Name && !sqlite3_temp_directory ){
46664    sqlite3_log(SQLITE_ERROR,
46665        "sqlite3_temp_directory variable should be set for WinRT");
46666  }
46667#endif
46668
46669  /* If the second argument to this function is NULL, generate a
46670  ** temporary file name to use
46671  */
46672  if( !zUtf8Name ){
46673    assert( isDelete && !isOpenJournal );
46674    rc = winGetTempname(pVfs, &zTmpname);
46675    if( rc!=SQLITE_OK ){
46676      OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
46677      return rc;
46678    }
46679    zUtf8Name = zTmpname;
46680  }
46681
46682  /* Database filenames are double-zero terminated if they are not
46683  ** URIs with parameters.  Hence, they can always be passed into
46684  ** sqlite3_uri_parameter().
46685  */
46686  assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
46687       zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
46688
46689  /* Convert the filename to the system encoding. */
46690  zConverted = winConvertFromUtf8Filename(zUtf8Name);
46691  if( zConverted==0 ){
46692    sqlite3_free(zTmpname);
46693    OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
46694    return SQLITE_IOERR_NOMEM_BKPT;
46695  }
46696
46697  if( winIsDir(zConverted) ){
46698    sqlite3_free(zConverted);
46699    sqlite3_free(zTmpname);
46700    OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
46701    return SQLITE_CANTOPEN_ISDIR;
46702  }
46703
46704  if( isReadWrite ){
46705    dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
46706  }else{
46707    dwDesiredAccess = GENERIC_READ;
46708  }
46709
46710  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
46711  ** created. SQLite doesn't use it to indicate "exclusive access"
46712  ** as it is usually understood.
46713  */
46714  if( isExclusive ){
46715    /* Creates a new file, only if it does not already exist. */
46716    /* If the file exists, it fails. */
46717    dwCreationDisposition = CREATE_NEW;
46718  }else if( isCreate ){
46719    /* Open existing file, or create if it doesn't exist */
46720    dwCreationDisposition = OPEN_ALWAYS;
46721  }else{
46722    /* Opens a file, only if it exists. */
46723    dwCreationDisposition = OPEN_EXISTING;
46724  }
46725
46726  dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
46727
46728  if( isDelete ){
46729#if SQLITE_OS_WINCE
46730    dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
46731    isTemp = 1;
46732#else
46733    dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
46734                               | FILE_ATTRIBUTE_HIDDEN
46735                               | FILE_FLAG_DELETE_ON_CLOSE;
46736#endif
46737  }else{
46738    dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
46739  }
46740  /* Reports from the internet are that performance is always
46741  ** better if FILE_FLAG_RANDOM_ACCESS is used.  Ticket #2699. */
46742#if SQLITE_OS_WINCE
46743  dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
46744#endif
46745
46746  if( osIsNT() ){
46747#if SQLITE_OS_WINRT
46748    CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
46749    extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
46750    extendedParameters.dwFileAttributes =
46751            dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
46752    extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
46753    extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
46754    extendedParameters.lpSecurityAttributes = NULL;
46755    extendedParameters.hTemplateFile = NULL;
46756    do{
46757      h = osCreateFile2((LPCWSTR)zConverted,
46758                        dwDesiredAccess,
46759                        dwShareMode,
46760                        dwCreationDisposition,
46761                        &extendedParameters);
46762      if( h!=INVALID_HANDLE_VALUE ) break;
46763      if( isReadWrite ){
46764        int rc2, isRO = 0;
46765        sqlite3BeginBenignMalloc();
46766        rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
46767        sqlite3EndBenignMalloc();
46768        if( rc2==SQLITE_OK && isRO ) break;
46769      }
46770    }while( winRetryIoerr(&cnt, &lastErrno) );
46771#else
46772    do{
46773      h = osCreateFileW((LPCWSTR)zConverted,
46774                        dwDesiredAccess,
46775                        dwShareMode, NULL,
46776                        dwCreationDisposition,
46777                        dwFlagsAndAttributes,
46778                        NULL);
46779      if( h!=INVALID_HANDLE_VALUE ) break;
46780      if( isReadWrite ){
46781        int rc2, isRO = 0;
46782        sqlite3BeginBenignMalloc();
46783        rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
46784        sqlite3EndBenignMalloc();
46785        if( rc2==SQLITE_OK && isRO ) break;
46786      }
46787    }while( winRetryIoerr(&cnt, &lastErrno) );
46788#endif
46789  }
46790#ifdef SQLITE_WIN32_HAS_ANSI
46791  else{
46792    do{
46793      h = osCreateFileA((LPCSTR)zConverted,
46794                        dwDesiredAccess,
46795                        dwShareMode, NULL,
46796                        dwCreationDisposition,
46797                        dwFlagsAndAttributes,
46798                        NULL);
46799      if( h!=INVALID_HANDLE_VALUE ) break;
46800      if( isReadWrite ){
46801        int rc2, isRO = 0;
46802        sqlite3BeginBenignMalloc();
46803        rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
46804        sqlite3EndBenignMalloc();
46805        if( rc2==SQLITE_OK && isRO ) break;
46806      }
46807    }while( winRetryIoerr(&cnt, &lastErrno) );
46808  }
46809#endif
46810  winLogIoerr(cnt, __LINE__);
46811
46812  OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
46813           dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
46814
46815  if( h==INVALID_HANDLE_VALUE ){
46816    sqlite3_free(zConverted);
46817    sqlite3_free(zTmpname);
46818    if( isReadWrite && !isExclusive ){
46819      return winOpen(pVfs, zName, id,
46820         ((flags|SQLITE_OPEN_READONLY) &
46821                     ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
46822         pOutFlags);
46823    }else{
46824      pFile->lastErrno = lastErrno;
46825      winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
46826      return SQLITE_CANTOPEN_BKPT;
46827    }
46828  }
46829
46830  if( pOutFlags ){
46831    if( isReadWrite ){
46832      *pOutFlags = SQLITE_OPEN_READWRITE;
46833    }else{
46834      *pOutFlags = SQLITE_OPEN_READONLY;
46835    }
46836  }
46837
46838  OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
46839           "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
46840           *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
46841
46842  pAppData = (winVfsAppData*)pVfs->pAppData;
46843
46844#if SQLITE_OS_WINCE
46845  {
46846    if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
46847         && ((pAppData==NULL) || !pAppData->bNoLock)
46848         && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
46849    ){
46850      osCloseHandle(h);
46851      sqlite3_free(zConverted);
46852      sqlite3_free(zTmpname);
46853      OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
46854      return rc;
46855    }
46856  }
46857  if( isTemp ){
46858    pFile->zDeleteOnClose = zConverted;
46859  }else
46860#endif
46861  {
46862    sqlite3_free(zConverted);
46863  }
46864
46865  sqlite3_free(zTmpname);
46866  pFile->pMethod = pAppData ? pAppData->pMethod : &winIoMethod;
46867  pFile->pVfs = pVfs;
46868  pFile->h = h;
46869  if( isReadonly ){
46870    pFile->ctrlFlags |= WINFILE_RDONLY;
46871  }
46872  if( (flags & SQLITE_OPEN_MAIN_DB)
46873   && sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE)
46874  ){
46875    pFile->ctrlFlags |= WINFILE_PSOW;
46876  }
46877  pFile->lastErrno = NO_ERROR;
46878  pFile->zPath = zName;
46879#if SQLITE_MAX_MMAP_SIZE>0
46880  pFile->hMap = NULL;
46881  pFile->pMapRegion = 0;
46882  pFile->mmapSize = 0;
46883  pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
46884#endif
46885
46886  OpenCounter(+1);
46887  return rc;
46888}
46889
46890/*
46891** Delete the named file.
46892**
46893** Note that Windows does not allow a file to be deleted if some other
46894** process has it open.  Sometimes a virus scanner or indexing program
46895** will open a journal file shortly after it is created in order to do
46896** whatever it does.  While this other process is holding the
46897** file open, we will be unable to delete it.  To work around this
46898** problem, we delay 100 milliseconds and try to delete again.  Up
46899** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
46900** up and returning an error.
46901*/
46902static int winDelete(
46903  sqlite3_vfs *pVfs,          /* Not used on win32 */
46904  const char *zFilename,      /* Name of file to delete */
46905  int syncDir                 /* Not used on win32 */
46906){
46907  int cnt = 0;
46908  int rc;
46909  DWORD attr;
46910  DWORD lastErrno = 0;
46911  void *zConverted;
46912  UNUSED_PARAMETER(pVfs);
46913  UNUSED_PARAMETER(syncDir);
46914
46915  SimulateIOError(return SQLITE_IOERR_DELETE);
46916  OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
46917
46918  zConverted = winConvertFromUtf8Filename(zFilename);
46919  if( zConverted==0 ){
46920    OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
46921    return SQLITE_IOERR_NOMEM_BKPT;
46922  }
46923  if( osIsNT() ){
46924    do {
46925#if SQLITE_OS_WINRT
46926      WIN32_FILE_ATTRIBUTE_DATA sAttrData;
46927      memset(&sAttrData, 0, sizeof(sAttrData));
46928      if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
46929                                  &sAttrData) ){
46930        attr = sAttrData.dwFileAttributes;
46931      }else{
46932        lastErrno = osGetLastError();
46933        if( lastErrno==ERROR_FILE_NOT_FOUND
46934         || lastErrno==ERROR_PATH_NOT_FOUND ){
46935          rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
46936        }else{
46937          rc = SQLITE_ERROR;
46938        }
46939        break;
46940      }
46941#else
46942      attr = osGetFileAttributesW(zConverted);
46943#endif
46944      if ( attr==INVALID_FILE_ATTRIBUTES ){
46945        lastErrno = osGetLastError();
46946        if( lastErrno==ERROR_FILE_NOT_FOUND
46947         || lastErrno==ERROR_PATH_NOT_FOUND ){
46948          rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
46949        }else{
46950          rc = SQLITE_ERROR;
46951        }
46952        break;
46953      }
46954      if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
46955        rc = SQLITE_ERROR; /* Files only. */
46956        break;
46957      }
46958      if ( osDeleteFileW(zConverted) ){
46959        rc = SQLITE_OK; /* Deleted OK. */
46960        break;
46961      }
46962      if ( !winRetryIoerr(&cnt, &lastErrno) ){
46963        rc = SQLITE_ERROR; /* No more retries. */
46964        break;
46965      }
46966    } while(1);
46967  }
46968#ifdef SQLITE_WIN32_HAS_ANSI
46969  else{
46970    do {
46971      attr = osGetFileAttributesA(zConverted);
46972      if ( attr==INVALID_FILE_ATTRIBUTES ){
46973        lastErrno = osGetLastError();
46974        if( lastErrno==ERROR_FILE_NOT_FOUND
46975         || lastErrno==ERROR_PATH_NOT_FOUND ){
46976          rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
46977        }else{
46978          rc = SQLITE_ERROR;
46979        }
46980        break;
46981      }
46982      if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
46983        rc = SQLITE_ERROR; /* Files only. */
46984        break;
46985      }
46986      if ( osDeleteFileA(zConverted) ){
46987        rc = SQLITE_OK; /* Deleted OK. */
46988        break;
46989      }
46990      if ( !winRetryIoerr(&cnt, &lastErrno) ){
46991        rc = SQLITE_ERROR; /* No more retries. */
46992        break;
46993      }
46994    } while(1);
46995  }
46996#endif
46997  if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
46998    rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
46999  }else{
47000    winLogIoerr(cnt, __LINE__);
47001  }
47002  sqlite3_free(zConverted);
47003  OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
47004  return rc;
47005}
47006
47007/*
47008** Check the existence and status of a file.
47009*/
47010static int winAccess(
47011  sqlite3_vfs *pVfs,         /* Not used on win32 */
47012  const char *zFilename,     /* Name of file to check */
47013  int flags,                 /* Type of test to make on this file */
47014  int *pResOut               /* OUT: Result */
47015){
47016  DWORD attr;
47017  int rc = 0;
47018  DWORD lastErrno = 0;
47019  void *zConverted;
47020  UNUSED_PARAMETER(pVfs);
47021
47022  SimulateIOError( return SQLITE_IOERR_ACCESS; );
47023  OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
47024           zFilename, flags, pResOut));
47025
47026  zConverted = winConvertFromUtf8Filename(zFilename);
47027  if( zConverted==0 ){
47028    OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
47029    return SQLITE_IOERR_NOMEM_BKPT;
47030  }
47031  if( osIsNT() ){
47032    int cnt = 0;
47033    WIN32_FILE_ATTRIBUTE_DATA sAttrData;
47034    memset(&sAttrData, 0, sizeof(sAttrData));
47035    while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
47036                             GetFileExInfoStandard,
47037                             &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
47038    if( rc ){
47039      /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
47040      ** as if it does not exist.
47041      */
47042      if(    flags==SQLITE_ACCESS_EXISTS
47043          && sAttrData.nFileSizeHigh==0
47044          && sAttrData.nFileSizeLow==0 ){
47045        attr = INVALID_FILE_ATTRIBUTES;
47046      }else{
47047        attr = sAttrData.dwFileAttributes;
47048      }
47049    }else{
47050      winLogIoerr(cnt, __LINE__);
47051      if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
47052        sqlite3_free(zConverted);
47053        return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
47054                           zFilename);
47055      }else{
47056        attr = INVALID_FILE_ATTRIBUTES;
47057      }
47058    }
47059  }
47060#ifdef SQLITE_WIN32_HAS_ANSI
47061  else{
47062    attr = osGetFileAttributesA((char*)zConverted);
47063  }
47064#endif
47065  sqlite3_free(zConverted);
47066  switch( flags ){
47067    case SQLITE_ACCESS_READ:
47068    case SQLITE_ACCESS_EXISTS:
47069      rc = attr!=INVALID_FILE_ATTRIBUTES;
47070      break;
47071    case SQLITE_ACCESS_READWRITE:
47072      rc = attr!=INVALID_FILE_ATTRIBUTES &&
47073             (attr & FILE_ATTRIBUTE_READONLY)==0;
47074      break;
47075    default:
47076      assert(!"Invalid flags argument");
47077  }
47078  *pResOut = rc;
47079  OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
47080           zFilename, pResOut, *pResOut));
47081  return SQLITE_OK;
47082}
47083
47084/*
47085** Returns non-zero if the specified path name starts with the "long path"
47086** prefix.
47087*/
47088static BOOL winIsLongPathPrefix(
47089  const char *zPathname
47090){
47091  return ( zPathname[0]=='\\' && zPathname[1]=='\\'
47092        && zPathname[2]=='?'  && zPathname[3]=='\\' );
47093}
47094
47095/*
47096** Returns non-zero if the specified path name starts with a drive letter
47097** followed by a colon character.
47098*/
47099static BOOL winIsDriveLetterAndColon(
47100  const char *zPathname
47101){
47102  return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' );
47103}
47104
47105/*
47106** Returns non-zero if the specified path name should be used verbatim.  If
47107** non-zero is returned from this function, the calling function must simply
47108** use the provided path name verbatim -OR- resolve it into a full path name
47109** using the GetFullPathName Win32 API function (if available).
47110*/
47111static BOOL winIsVerbatimPathname(
47112  const char *zPathname
47113){
47114  /*
47115  ** If the path name starts with a forward slash or a backslash, it is either
47116  ** a legal UNC name, a volume relative path, or an absolute path name in the
47117  ** "Unix" format on Windows.  There is no easy way to differentiate between
47118  ** the final two cases; therefore, we return the safer return value of TRUE
47119  ** so that callers of this function will simply use it verbatim.
47120  */
47121  if ( winIsDirSep(zPathname[0]) ){
47122    return TRUE;
47123  }
47124
47125  /*
47126  ** If the path name starts with a letter and a colon it is either a volume
47127  ** relative path or an absolute path.  Callers of this function must not
47128  ** attempt to treat it as a relative path name (i.e. they should simply use
47129  ** it verbatim).
47130  */
47131  if ( winIsDriveLetterAndColon(zPathname) ){
47132    return TRUE;
47133  }
47134
47135  /*
47136  ** If we get to this point, the path name should almost certainly be a purely
47137  ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
47138  */
47139  return FALSE;
47140}
47141
47142/*
47143** Turn a relative pathname into a full pathname.  Write the full
47144** pathname into zOut[].  zOut[] will be at least pVfs->mxPathname
47145** bytes in size.
47146*/
47147static int winFullPathname(
47148  sqlite3_vfs *pVfs,            /* Pointer to vfs object */
47149  const char *zRelative,        /* Possibly relative input path */
47150  int nFull,                    /* Size of output buffer in bytes */
47151  char *zFull                   /* Output buffer */
47152){
47153#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
47154  DWORD nByte;
47155  void *zConverted;
47156  char *zOut;
47157#endif
47158
47159  /* If this path name begins with "/X:" or "\\?\", where "X" is any
47160  ** alphabetic character, discard the initial "/" from the pathname.
47161  */
47162  if( zRelative[0]=='/' && (winIsDriveLetterAndColon(zRelative+1)
47163       || winIsLongPathPrefix(zRelative+1)) ){
47164    zRelative++;
47165  }
47166
47167#if defined(__CYGWIN__)
47168  SimulateIOError( return SQLITE_ERROR );
47169  UNUSED_PARAMETER(nFull);
47170  assert( nFull>=pVfs->mxPathname );
47171  if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
47172    /*
47173    ** NOTE: We are dealing with a relative path name and the data
47174    **       directory has been set.  Therefore, use it as the basis
47175    **       for converting the relative path name to an absolute
47176    **       one by prepending the data directory and a slash.
47177    */
47178    char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
47179    if( !zOut ){
47180      return SQLITE_IOERR_NOMEM_BKPT;
47181    }
47182    if( cygwin_conv_path(
47183            (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
47184            CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
47185      sqlite3_free(zOut);
47186      return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
47187                         "winFullPathname1", zRelative);
47188    }else{
47189      char *zUtf8 = winConvertToUtf8Filename(zOut);
47190      if( !zUtf8 ){
47191        sqlite3_free(zOut);
47192        return SQLITE_IOERR_NOMEM_BKPT;
47193      }
47194      sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
47195                       sqlite3_data_directory, winGetDirSep(), zUtf8);
47196      sqlite3_free(zUtf8);
47197      sqlite3_free(zOut);
47198    }
47199  }else{
47200    char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
47201    if( !zOut ){
47202      return SQLITE_IOERR_NOMEM_BKPT;
47203    }
47204    if( cygwin_conv_path(
47205            (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
47206            zRelative, zOut, pVfs->mxPathname+1)<0 ){
47207      sqlite3_free(zOut);
47208      return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
47209                         "winFullPathname2", zRelative);
47210    }else{
47211      char *zUtf8 = winConvertToUtf8Filename(zOut);
47212      if( !zUtf8 ){
47213        sqlite3_free(zOut);
47214        return SQLITE_IOERR_NOMEM_BKPT;
47215      }
47216      sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
47217      sqlite3_free(zUtf8);
47218      sqlite3_free(zOut);
47219    }
47220  }
47221  return SQLITE_OK;
47222#endif
47223
47224#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
47225  SimulateIOError( return SQLITE_ERROR );
47226  /* WinCE has no concept of a relative pathname, or so I am told. */
47227  /* WinRT has no way to convert a relative path to an absolute one. */
47228  if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
47229    /*
47230    ** NOTE: We are dealing with a relative path name and the data
47231    **       directory has been set.  Therefore, use it as the basis
47232    **       for converting the relative path name to an absolute
47233    **       one by prepending the data directory and a backslash.
47234    */
47235    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
47236                     sqlite3_data_directory, winGetDirSep(), zRelative);
47237  }else{
47238    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
47239  }
47240  return SQLITE_OK;
47241#endif
47242
47243#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
47244  /* It's odd to simulate an io-error here, but really this is just
47245  ** using the io-error infrastructure to test that SQLite handles this
47246  ** function failing. This function could fail if, for example, the
47247  ** current working directory has been unlinked.
47248  */
47249  SimulateIOError( return SQLITE_ERROR );
47250  if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
47251    /*
47252    ** NOTE: We are dealing with a relative path name and the data
47253    **       directory has been set.  Therefore, use it as the basis
47254    **       for converting the relative path name to an absolute
47255    **       one by prepending the data directory and a backslash.
47256    */
47257    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
47258                     sqlite3_data_directory, winGetDirSep(), zRelative);
47259    return SQLITE_OK;
47260  }
47261  zConverted = winConvertFromUtf8Filename(zRelative);
47262  if( zConverted==0 ){
47263    return SQLITE_IOERR_NOMEM_BKPT;
47264  }
47265  if( osIsNT() ){
47266    LPWSTR zTemp;
47267    nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
47268    if( nByte==0 ){
47269      sqlite3_free(zConverted);
47270      return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
47271                         "winFullPathname1", zRelative);
47272    }
47273    nByte += 3;
47274    zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
47275    if( zTemp==0 ){
47276      sqlite3_free(zConverted);
47277      return SQLITE_IOERR_NOMEM_BKPT;
47278    }
47279    nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
47280    if( nByte==0 ){
47281      sqlite3_free(zConverted);
47282      sqlite3_free(zTemp);
47283      return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
47284                         "winFullPathname2", zRelative);
47285    }
47286    sqlite3_free(zConverted);
47287    zOut = winUnicodeToUtf8(zTemp);
47288    sqlite3_free(zTemp);
47289  }
47290#ifdef SQLITE_WIN32_HAS_ANSI
47291  else{
47292    char *zTemp;
47293    nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
47294    if( nByte==0 ){
47295      sqlite3_free(zConverted);
47296      return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
47297                         "winFullPathname3", zRelative);
47298    }
47299    nByte += 3;
47300    zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
47301    if( zTemp==0 ){
47302      sqlite3_free(zConverted);
47303      return SQLITE_IOERR_NOMEM_BKPT;
47304    }
47305    nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
47306    if( nByte==0 ){
47307      sqlite3_free(zConverted);
47308      sqlite3_free(zTemp);
47309      return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
47310                         "winFullPathname4", zRelative);
47311    }
47312    sqlite3_free(zConverted);
47313    zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
47314    sqlite3_free(zTemp);
47315  }
47316#endif
47317  if( zOut ){
47318    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
47319    sqlite3_free(zOut);
47320    return SQLITE_OK;
47321  }else{
47322    return SQLITE_IOERR_NOMEM_BKPT;
47323  }
47324#endif
47325}
47326
47327#ifndef SQLITE_OMIT_LOAD_EXTENSION
47328/*
47329** Interfaces for opening a shared library, finding entry points
47330** within the shared library, and closing the shared library.
47331*/
47332static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
47333  HANDLE h;
47334#if defined(__CYGWIN__)
47335  int nFull = pVfs->mxPathname+1;
47336  char *zFull = sqlite3MallocZero( nFull );
47337  void *zConverted = 0;
47338  if( zFull==0 ){
47339    OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
47340    return 0;
47341  }
47342  if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){
47343    sqlite3_free(zFull);
47344    OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
47345    return 0;
47346  }
47347  zConverted = winConvertFromUtf8Filename(zFull);
47348  sqlite3_free(zFull);
47349#else
47350  void *zConverted = winConvertFromUtf8Filename(zFilename);
47351  UNUSED_PARAMETER(pVfs);
47352#endif
47353  if( zConverted==0 ){
47354    OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
47355    return 0;
47356  }
47357  if( osIsNT() ){
47358#if SQLITE_OS_WINRT
47359    h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
47360#else
47361    h = osLoadLibraryW((LPCWSTR)zConverted);
47362#endif
47363  }
47364#ifdef SQLITE_WIN32_HAS_ANSI
47365  else{
47366    h = osLoadLibraryA((char*)zConverted);
47367  }
47368#endif
47369  OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
47370  sqlite3_free(zConverted);
47371  return (void*)h;
47372}
47373static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
47374  UNUSED_PARAMETER(pVfs);
47375  winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
47376}
47377static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
47378  FARPROC proc;
47379  UNUSED_PARAMETER(pVfs);
47380  proc = osGetProcAddressA((HANDLE)pH, zSym);
47381  OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
47382           (void*)pH, zSym, (void*)proc));
47383  return (void(*)(void))proc;
47384}
47385static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
47386  UNUSED_PARAMETER(pVfs);
47387  osFreeLibrary((HANDLE)pHandle);
47388  OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
47389}
47390#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
47391  #define winDlOpen  0
47392  #define winDlError 0
47393  #define winDlSym   0
47394  #define winDlClose 0
47395#endif
47396
47397/* State information for the randomness gatherer. */
47398typedef struct EntropyGatherer EntropyGatherer;
47399struct EntropyGatherer {
47400  unsigned char *a;   /* Gather entropy into this buffer */
47401  int na;             /* Size of a[] in bytes */
47402  int i;              /* XOR next input into a[i] */
47403  int nXor;           /* Number of XOR operations done */
47404};
47405
47406#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
47407/* Mix sz bytes of entropy into p. */
47408static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
47409  int j, k;
47410  for(j=0, k=p->i; j<sz; j++){
47411    p->a[k++] ^= x[j];
47412    if( k>=p->na ) k = 0;
47413  }
47414  p->i = k;
47415  p->nXor += sz;
47416}
47417#endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */
47418
47419/*
47420** Write up to nBuf bytes of randomness into zBuf.
47421*/
47422static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
47423#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
47424  UNUSED_PARAMETER(pVfs);
47425  memset(zBuf, 0, nBuf);
47426  return nBuf;
47427#else
47428  EntropyGatherer e;
47429  UNUSED_PARAMETER(pVfs);
47430  memset(zBuf, 0, nBuf);
47431  e.a = (unsigned char*)zBuf;
47432  e.na = nBuf;
47433  e.nXor = 0;
47434  e.i = 0;
47435  {
47436    SYSTEMTIME x;
47437    osGetSystemTime(&x);
47438    xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME));
47439  }
47440  {
47441    DWORD pid = osGetCurrentProcessId();
47442    xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD));
47443  }
47444#if SQLITE_OS_WINRT
47445  {
47446    ULONGLONG cnt = osGetTickCount64();
47447    xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG));
47448  }
47449#else
47450  {
47451    DWORD cnt = osGetTickCount();
47452    xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD));
47453  }
47454#endif /* SQLITE_OS_WINRT */
47455  {
47456    LARGE_INTEGER i;
47457    osQueryPerformanceCounter(&i);
47458    xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER));
47459  }
47460#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
47461  {
47462    UUID id;
47463    memset(&id, 0, sizeof(UUID));
47464    osUuidCreate(&id);
47465    xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
47466    memset(&id, 0, sizeof(UUID));
47467    osUuidCreateSequential(&id);
47468    xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
47469  }
47470#endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */
47471  return e.nXor>nBuf ? nBuf : e.nXor;
47472#endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */
47473}
47474
47475
47476/*
47477** Sleep for a little while.  Return the amount of time slept.
47478*/
47479static int winSleep(sqlite3_vfs *pVfs, int microsec){
47480  sqlite3_win32_sleep((microsec+999)/1000);
47481  UNUSED_PARAMETER(pVfs);
47482  return ((microsec+999)/1000)*1000;
47483}
47484
47485/*
47486** The following variable, if set to a non-zero value, is interpreted as
47487** the number of seconds since 1970 and is used to set the result of
47488** sqlite3OsCurrentTime() during testing.
47489*/
47490#ifdef SQLITE_TEST
47491SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
47492#endif
47493
47494/*
47495** Find the current time (in Universal Coordinated Time).  Write into *piNow
47496** the current time and date as a Julian Day number times 86_400_000.  In
47497** other words, write into *piNow the number of milliseconds since the Julian
47498** epoch of noon in Greenwich on November 24, 4714 B.C according to the
47499** proleptic Gregorian calendar.
47500**
47501** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date
47502** cannot be found.
47503*/
47504static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
47505  /* FILETIME structure is a 64-bit value representing the number of
47506     100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
47507  */
47508  FILETIME ft;
47509  static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
47510#ifdef SQLITE_TEST
47511  static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
47512#endif
47513  /* 2^32 - to avoid use of LL and warnings in gcc */
47514  static const sqlite3_int64 max32BitValue =
47515      (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
47516      (sqlite3_int64)294967296;
47517
47518#if SQLITE_OS_WINCE
47519  SYSTEMTIME time;
47520  osGetSystemTime(&time);
47521  /* if SystemTimeToFileTime() fails, it returns zero. */
47522  if (!osSystemTimeToFileTime(&time,&ft)){
47523    return SQLITE_ERROR;
47524  }
47525#else
47526  osGetSystemTimeAsFileTime( &ft );
47527#endif
47528
47529  *piNow = winFiletimeEpoch +
47530            ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
47531               (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
47532
47533#ifdef SQLITE_TEST
47534  if( sqlite3_current_time ){
47535    *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
47536  }
47537#endif
47538  UNUSED_PARAMETER(pVfs);
47539  return SQLITE_OK;
47540}
47541
47542/*
47543** Find the current time (in Universal Coordinated Time).  Write the
47544** current time and date as a Julian Day number into *prNow and
47545** return 0.  Return 1 if the time and date cannot be found.
47546*/
47547static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
47548  int rc;
47549  sqlite3_int64 i;
47550  rc = winCurrentTimeInt64(pVfs, &i);
47551  if( !rc ){
47552    *prNow = i/86400000.0;
47553  }
47554  return rc;
47555}
47556
47557/*
47558** The idea is that this function works like a combination of
47559** GetLastError() and FormatMessage() on Windows (or errno and
47560** strerror_r() on Unix). After an error is returned by an OS
47561** function, SQLite calls this function with zBuf pointing to
47562** a buffer of nBuf bytes. The OS layer should populate the
47563** buffer with a nul-terminated UTF-8 encoded error message
47564** describing the last IO error to have occurred within the calling
47565** thread.
47566**
47567** If the error message is too large for the supplied buffer,
47568** it should be truncated. The return value of xGetLastError
47569** is zero if the error message fits in the buffer, or non-zero
47570** otherwise (if the message was truncated). If non-zero is returned,
47571** then it is not necessary to include the nul-terminator character
47572** in the output buffer.
47573**
47574** Not supplying an error message will have no adverse effect
47575** on SQLite. It is fine to have an implementation that never
47576** returns an error message:
47577**
47578**   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
47579**     assert(zBuf[0]=='\0');
47580**     return 0;
47581**   }
47582**
47583** However if an error message is supplied, it will be incorporated
47584** by sqlite into the error message available to the user using
47585** sqlite3_errmsg(), possibly making IO errors easier to debug.
47586*/
47587static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
47588  DWORD e = osGetLastError();
47589  UNUSED_PARAMETER(pVfs);
47590  if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf);
47591  return e;
47592}
47593
47594/*
47595** Initialize and deinitialize the operating system interface.
47596*/
47597SQLITE_API int sqlite3_os_init(void){
47598  static sqlite3_vfs winVfs = {
47599    3,                     /* iVersion */
47600    sizeof(winFile),       /* szOsFile */
47601    SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
47602    0,                     /* pNext */
47603    "win32",               /* zName */
47604    &winAppData,           /* pAppData */
47605    winOpen,               /* xOpen */
47606    winDelete,             /* xDelete */
47607    winAccess,             /* xAccess */
47608    winFullPathname,       /* xFullPathname */
47609    winDlOpen,             /* xDlOpen */
47610    winDlError,            /* xDlError */
47611    winDlSym,              /* xDlSym */
47612    winDlClose,            /* xDlClose */
47613    winRandomness,         /* xRandomness */
47614    winSleep,              /* xSleep */
47615    winCurrentTime,        /* xCurrentTime */
47616    winGetLastError,       /* xGetLastError */
47617    winCurrentTimeInt64,   /* xCurrentTimeInt64 */
47618    winSetSystemCall,      /* xSetSystemCall */
47619    winGetSystemCall,      /* xGetSystemCall */
47620    winNextSystemCall,     /* xNextSystemCall */
47621  };
47622#if defined(SQLITE_WIN32_HAS_WIDE)
47623  static sqlite3_vfs winLongPathVfs = {
47624    3,                     /* iVersion */
47625    sizeof(winFile),       /* szOsFile */
47626    SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
47627    0,                     /* pNext */
47628    "win32-longpath",      /* zName */
47629    &winAppData,           /* pAppData */
47630    winOpen,               /* xOpen */
47631    winDelete,             /* xDelete */
47632    winAccess,             /* xAccess */
47633    winFullPathname,       /* xFullPathname */
47634    winDlOpen,             /* xDlOpen */
47635    winDlError,            /* xDlError */
47636    winDlSym,              /* xDlSym */
47637    winDlClose,            /* xDlClose */
47638    winRandomness,         /* xRandomness */
47639    winSleep,              /* xSleep */
47640    winCurrentTime,        /* xCurrentTime */
47641    winGetLastError,       /* xGetLastError */
47642    winCurrentTimeInt64,   /* xCurrentTimeInt64 */
47643    winSetSystemCall,      /* xSetSystemCall */
47644    winGetSystemCall,      /* xGetSystemCall */
47645    winNextSystemCall,     /* xNextSystemCall */
47646  };
47647#endif
47648  static sqlite3_vfs winNolockVfs = {
47649    3,                     /* iVersion */
47650    sizeof(winFile),       /* szOsFile */
47651    SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
47652    0,                     /* pNext */
47653    "win32-none",          /* zName */
47654    &winNolockAppData,     /* pAppData */
47655    winOpen,               /* xOpen */
47656    winDelete,             /* xDelete */
47657    winAccess,             /* xAccess */
47658    winFullPathname,       /* xFullPathname */
47659    winDlOpen,             /* xDlOpen */
47660    winDlError,            /* xDlError */
47661    winDlSym,              /* xDlSym */
47662    winDlClose,            /* xDlClose */
47663    winRandomness,         /* xRandomness */
47664    winSleep,              /* xSleep */
47665    winCurrentTime,        /* xCurrentTime */
47666    winGetLastError,       /* xGetLastError */
47667    winCurrentTimeInt64,   /* xCurrentTimeInt64 */
47668    winSetSystemCall,      /* xSetSystemCall */
47669    winGetSystemCall,      /* xGetSystemCall */
47670    winNextSystemCall,     /* xNextSystemCall */
47671  };
47672#if defined(SQLITE_WIN32_HAS_WIDE)
47673  static sqlite3_vfs winLongPathNolockVfs = {
47674    3,                     /* iVersion */
47675    sizeof(winFile),       /* szOsFile */
47676    SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
47677    0,                     /* pNext */
47678    "win32-longpath-none", /* zName */
47679    &winNolockAppData,     /* pAppData */
47680    winOpen,               /* xOpen */
47681    winDelete,             /* xDelete */
47682    winAccess,             /* xAccess */
47683    winFullPathname,       /* xFullPathname */
47684    winDlOpen,             /* xDlOpen */
47685    winDlError,            /* xDlError */
47686    winDlSym,              /* xDlSym */
47687    winDlClose,            /* xDlClose */
47688    winRandomness,         /* xRandomness */
47689    winSleep,              /* xSleep */
47690    winCurrentTime,        /* xCurrentTime */
47691    winGetLastError,       /* xGetLastError */
47692    winCurrentTimeInt64,   /* xCurrentTimeInt64 */
47693    winSetSystemCall,      /* xSetSystemCall */
47694    winGetSystemCall,      /* xGetSystemCall */
47695    winNextSystemCall,     /* xNextSystemCall */
47696  };
47697#endif
47698
47699  /* Double-check that the aSyscall[] array has been constructed
47700  ** correctly.  See ticket [bb3a86e890c8e96ab] */
47701  assert( ArraySize(aSyscall)==80 );
47702
47703  /* get memory map allocation granularity */
47704  memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
47705#if SQLITE_OS_WINRT
47706  osGetNativeSystemInfo(&winSysInfo);
47707#else
47708  osGetSystemInfo(&winSysInfo);
47709#endif
47710  assert( winSysInfo.dwAllocationGranularity>0 );
47711  assert( winSysInfo.dwPageSize>0 );
47712
47713  sqlite3_vfs_register(&winVfs, 1);
47714
47715#if defined(SQLITE_WIN32_HAS_WIDE)
47716  sqlite3_vfs_register(&winLongPathVfs, 0);
47717#endif
47718
47719  sqlite3_vfs_register(&winNolockVfs, 0);
47720
47721#if defined(SQLITE_WIN32_HAS_WIDE)
47722  sqlite3_vfs_register(&winLongPathNolockVfs, 0);
47723#endif
47724
47725#ifndef SQLITE_OMIT_WAL
47726  winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
47727#endif
47728
47729  return SQLITE_OK;
47730}
47731
47732SQLITE_API int sqlite3_os_end(void){
47733#if SQLITE_OS_WINRT
47734  if( sleepObj!=NULL ){
47735    osCloseHandle(sleepObj);
47736    sleepObj = NULL;
47737  }
47738#endif
47739
47740#ifndef SQLITE_OMIT_WAL
47741  winBigLock = 0;
47742#endif
47743
47744  return SQLITE_OK;
47745}
47746
47747#endif /* SQLITE_OS_WIN */
47748
47749/************** End of os_win.c **********************************************/
47750/************** Begin file memdb.c *******************************************/
47751/*
47752** 2016-09-07
47753**
47754** The author disclaims copyright to this source code.  In place of
47755** a legal notice, here is a blessing:
47756**
47757**    May you do good and not evil.
47758**    May you find forgiveness for yourself and forgive others.
47759**    May you share freely, never taking more than you give.
47760**
47761******************************************************************************
47762**
47763** This file implements an in-memory VFS. A database is held as a contiguous
47764** block of memory.
47765**
47766** This file also implements interface sqlite3_serialize() and
47767** sqlite3_deserialize().
47768*/
47769/* #include "sqliteInt.h" */
47770#ifdef SQLITE_ENABLE_DESERIALIZE
47771
47772/*
47773** Forward declaration of objects used by this utility
47774*/
47775typedef struct sqlite3_vfs MemVfs;
47776typedef struct MemFile MemFile;
47777
47778/* Access to a lower-level VFS that (might) implement dynamic loading,
47779** access to randomness, etc.
47780*/
47781#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
47782
47783/* An open file */
47784struct MemFile {
47785  sqlite3_file base;              /* IO methods */
47786  sqlite3_int64 sz;               /* Size of the file */
47787  sqlite3_int64 szAlloc;          /* Space allocated to aData */
47788  sqlite3_int64 szMax;            /* Maximum allowed size of the file */
47789  unsigned char *aData;           /* content of the file */
47790  int nMmap;                      /* Number of memory mapped pages */
47791  unsigned mFlags;                /* Flags */
47792  int eLock;                      /* Most recent lock against this file */
47793};
47794
47795/*
47796** Methods for MemFile
47797*/
47798static int memdbClose(sqlite3_file*);
47799static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
47800static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
47801static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
47802static int memdbSync(sqlite3_file*, int flags);
47803static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
47804static int memdbLock(sqlite3_file*, int);
47805/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
47806static int memdbFileControl(sqlite3_file*, int op, void *pArg);
47807/* static int memdbSectorSize(sqlite3_file*); // not used */
47808static int memdbDeviceCharacteristics(sqlite3_file*);
47809static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
47810static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
47811
47812/*
47813** Methods for MemVfs
47814*/
47815static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
47816/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
47817static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
47818static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
47819static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
47820static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
47821static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
47822static void memdbDlClose(sqlite3_vfs*, void*);
47823static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
47824static int memdbSleep(sqlite3_vfs*, int microseconds);
47825/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
47826static int memdbGetLastError(sqlite3_vfs*, int, char *);
47827static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
47828
47829static sqlite3_vfs memdb_vfs = {
47830  2,                           /* iVersion */
47831  0,                           /* szOsFile (set when registered) */
47832  1024,                        /* mxPathname */
47833  0,                           /* pNext */
47834  "memdb",                     /* zName */
47835  0,                           /* pAppData (set when registered) */
47836  memdbOpen,                   /* xOpen */
47837  0, /* memdbDelete, */        /* xDelete */
47838  memdbAccess,                 /* xAccess */
47839  memdbFullPathname,           /* xFullPathname */
47840  memdbDlOpen,                 /* xDlOpen */
47841  memdbDlError,                /* xDlError */
47842  memdbDlSym,                  /* xDlSym */
47843  memdbDlClose,                /* xDlClose */
47844  memdbRandomness,             /* xRandomness */
47845  memdbSleep,                  /* xSleep */
47846  0, /* memdbCurrentTime, */   /* xCurrentTime */
47847  memdbGetLastError,           /* xGetLastError */
47848  memdbCurrentTimeInt64        /* xCurrentTimeInt64 */
47849};
47850
47851static const sqlite3_io_methods memdb_io_methods = {
47852  3,                              /* iVersion */
47853  memdbClose,                      /* xClose */
47854  memdbRead,                       /* xRead */
47855  memdbWrite,                      /* xWrite */
47856  memdbTruncate,                   /* xTruncate */
47857  memdbSync,                       /* xSync */
47858  memdbFileSize,                   /* xFileSize */
47859  memdbLock,                       /* xLock */
47860  memdbLock,                       /* xUnlock - same as xLock in this case */
47861  0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
47862  memdbFileControl,                /* xFileControl */
47863  0, /* memdbSectorSize,*/         /* xSectorSize */
47864  memdbDeviceCharacteristics,      /* xDeviceCharacteristics */
47865  0,                               /* xShmMap */
47866  0,                               /* xShmLock */
47867  0,                               /* xShmBarrier */
47868  0,                               /* xShmUnmap */
47869  memdbFetch,                      /* xFetch */
47870  memdbUnfetch                     /* xUnfetch */
47871};
47872
47873
47874
47875/*
47876** Close an memdb-file.
47877**
47878** The pData pointer is owned by the application, so there is nothing
47879** to free.
47880*/
47881static int memdbClose(sqlite3_file *pFile){
47882  MemFile *p = (MemFile *)pFile;
47883  if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ) sqlite3_free(p->aData);
47884  return SQLITE_OK;
47885}
47886
47887/*
47888** Read data from an memdb-file.
47889*/
47890static int memdbRead(
47891  sqlite3_file *pFile,
47892  void *zBuf,
47893  int iAmt,
47894  sqlite_int64 iOfst
47895){
47896  MemFile *p = (MemFile *)pFile;
47897  if( iOfst+iAmt>p->sz ){
47898    memset(zBuf, 0, iAmt);
47899    if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
47900    return SQLITE_IOERR_SHORT_READ;
47901  }
47902  memcpy(zBuf, p->aData+iOfst, iAmt);
47903  return SQLITE_OK;
47904}
47905
47906/*
47907** Try to enlarge the memory allocation to hold at least sz bytes
47908*/
47909static int memdbEnlarge(MemFile *p, sqlite3_int64 newSz){
47910  unsigned char *pNew;
47911  if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){
47912    return SQLITE_FULL;
47913  }
47914  if( newSz>p->szMax ){
47915    return SQLITE_FULL;
47916  }
47917  newSz *= 2;
47918  if( newSz>p->szMax ) newSz = p->szMax;
47919  pNew = sqlite3Realloc(p->aData, newSz);
47920  if( pNew==0 ) return SQLITE_NOMEM;
47921  p->aData = pNew;
47922  p->szAlloc = newSz;
47923  return SQLITE_OK;
47924}
47925
47926/*
47927** Write data to an memdb-file.
47928*/
47929static int memdbWrite(
47930  sqlite3_file *pFile,
47931  const void *z,
47932  int iAmt,
47933  sqlite_int64 iOfst
47934){
47935  MemFile *p = (MemFile *)pFile;
47936  if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ) return SQLITE_READONLY;
47937  if( iOfst+iAmt>p->sz ){
47938    int rc;
47939    if( iOfst+iAmt>p->szAlloc
47940     && (rc = memdbEnlarge(p, iOfst+iAmt))!=SQLITE_OK
47941    ){
47942      return rc;
47943    }
47944    if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
47945    p->sz = iOfst+iAmt;
47946  }
47947  memcpy(p->aData+iOfst, z, iAmt);
47948  return SQLITE_OK;
47949}
47950
47951/*
47952** Truncate an memdb-file.
47953**
47954** In rollback mode (which is always the case for memdb, as it does not
47955** support WAL mode) the truncate() method is only used to reduce
47956** the size of a file, never to increase the size.
47957*/
47958static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
47959  MemFile *p = (MemFile *)pFile;
47960  if( NEVER(size>p->sz) ) return SQLITE_FULL;
47961  p->sz = size;
47962  return SQLITE_OK;
47963}
47964
47965/*
47966** Sync an memdb-file.
47967*/
47968static int memdbSync(sqlite3_file *pFile, int flags){
47969  return SQLITE_OK;
47970}
47971
47972/*
47973** Return the current file-size of an memdb-file.
47974*/
47975static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
47976  MemFile *p = (MemFile *)pFile;
47977  *pSize = p->sz;
47978  return SQLITE_OK;
47979}
47980
47981/*
47982** Lock an memdb-file.
47983*/
47984static int memdbLock(sqlite3_file *pFile, int eLock){
47985  MemFile *p = (MemFile *)pFile;
47986  if( eLock>SQLITE_LOCK_SHARED
47987   && (p->mFlags & SQLITE_DESERIALIZE_READONLY)!=0
47988  ){
47989    return SQLITE_READONLY;
47990  }
47991  p->eLock = eLock;
47992  return SQLITE_OK;
47993}
47994
47995#if 0 /* Never used because memdbAccess() always returns false */
47996/*
47997** Check if another file-handle holds a RESERVED lock on an memdb-file.
47998*/
47999static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
48000  *pResOut = 0;
48001  return SQLITE_OK;
48002}
48003#endif
48004
48005/*
48006** File control method. For custom operations on an memdb-file.
48007*/
48008static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
48009  MemFile *p = (MemFile *)pFile;
48010  int rc = SQLITE_NOTFOUND;
48011  if( op==SQLITE_FCNTL_VFSNAME ){
48012    *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
48013    rc = SQLITE_OK;
48014  }
48015  if( op==SQLITE_FCNTL_SIZE_LIMIT ){
48016    sqlite3_int64 iLimit = *(sqlite3_int64*)pArg;
48017    if( iLimit<p->sz ){
48018      if( iLimit<0 ){
48019        iLimit = p->szMax;
48020      }else{
48021        iLimit = p->sz;
48022      }
48023    }
48024    p->szMax = iLimit;
48025    *(sqlite3_int64*)pArg = iLimit;
48026    rc = SQLITE_OK;
48027  }
48028  return rc;
48029}
48030
48031#if 0  /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
48032/*
48033** Return the sector-size in bytes for an memdb-file.
48034*/
48035static int memdbSectorSize(sqlite3_file *pFile){
48036  return 1024;
48037}
48038#endif
48039
48040/*
48041** Return the device characteristic flags supported by an memdb-file.
48042*/
48043static int memdbDeviceCharacteristics(sqlite3_file *pFile){
48044  return SQLITE_IOCAP_ATOMIC |
48045         SQLITE_IOCAP_POWERSAFE_OVERWRITE |
48046         SQLITE_IOCAP_SAFE_APPEND |
48047         SQLITE_IOCAP_SEQUENTIAL;
48048}
48049
48050/* Fetch a page of a memory-mapped file */
48051static int memdbFetch(
48052  sqlite3_file *pFile,
48053  sqlite3_int64 iOfst,
48054  int iAmt,
48055  void **pp
48056){
48057  MemFile *p = (MemFile *)pFile;
48058  if( iOfst+iAmt>p->sz ){
48059    *pp = 0;
48060  }else{
48061    p->nMmap++;
48062    *pp = (void*)(p->aData + iOfst);
48063  }
48064  return SQLITE_OK;
48065}
48066
48067/* Release a memory-mapped page */
48068static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
48069  MemFile *p = (MemFile *)pFile;
48070  p->nMmap--;
48071  return SQLITE_OK;
48072}
48073
48074/*
48075** Open an mem file handle.
48076*/
48077static int memdbOpen(
48078  sqlite3_vfs *pVfs,
48079  const char *zName,
48080  sqlite3_file *pFile,
48081  int flags,
48082  int *pOutFlags
48083){
48084  MemFile *p = (MemFile*)pFile;
48085  if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
48086    return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFile, flags, pOutFlags);
48087  }
48088  memset(p, 0, sizeof(*p));
48089  p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
48090  assert( pOutFlags!=0 );  /* True because flags==SQLITE_OPEN_MAIN_DB */
48091  *pOutFlags = flags | SQLITE_OPEN_MEMORY;
48092  p->base.pMethods = &memdb_io_methods;
48093  p->szMax = sqlite3GlobalConfig.mxMemdbSize;
48094  return SQLITE_OK;
48095}
48096
48097#if 0 /* Only used to delete rollback journals, master journals, and WAL
48098      ** files, none of which exist in memdb.  So this routine is never used */
48099/*
48100** Delete the file located at zPath. If the dirSync argument is true,
48101** ensure the file-system modifications are synced to disk before
48102** returning.
48103*/
48104static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
48105  return SQLITE_IOERR_DELETE;
48106}
48107#endif
48108
48109/*
48110** Test for access permissions. Return true if the requested permission
48111** is available, or false otherwise.
48112**
48113** With memdb, no files ever exist on disk.  So always return false.
48114*/
48115static int memdbAccess(
48116  sqlite3_vfs *pVfs,
48117  const char *zPath,
48118  int flags,
48119  int *pResOut
48120){
48121  *pResOut = 0;
48122  return SQLITE_OK;
48123}
48124
48125/*
48126** Populate buffer zOut with the full canonical pathname corresponding
48127** to the pathname in zPath. zOut is guaranteed to point to a buffer
48128** of at least (INST_MAX_PATHNAME+1) bytes.
48129*/
48130static int memdbFullPathname(
48131  sqlite3_vfs *pVfs,
48132  const char *zPath,
48133  int nOut,
48134  char *zOut
48135){
48136  sqlite3_snprintf(nOut, zOut, "%s", zPath);
48137  return SQLITE_OK;
48138}
48139
48140/*
48141** Open the dynamic library located at zPath and return a handle.
48142*/
48143static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
48144  return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
48145}
48146
48147/*
48148** Populate the buffer zErrMsg (size nByte bytes) with a human readable
48149** utf-8 string describing the most recent error encountered associated
48150** with dynamic libraries.
48151*/
48152static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
48153  ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
48154}
48155
48156/*
48157** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
48158*/
48159static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
48160  return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
48161}
48162
48163/*
48164** Close the dynamic library handle pHandle.
48165*/
48166static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
48167  ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
48168}
48169
48170/*
48171** Populate the buffer pointed to by zBufOut with nByte bytes of
48172** random data.
48173*/
48174static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
48175  return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
48176}
48177
48178/*
48179** Sleep for nMicro microseconds. Return the number of microseconds
48180** actually slept.
48181*/
48182static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
48183  return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
48184}
48185
48186#if 0  /* Never used.  Modern cores only call xCurrentTimeInt64() */
48187/*
48188** Return the current time as a Julian Day number in *pTimeOut.
48189*/
48190static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
48191  return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
48192}
48193#endif
48194
48195static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
48196  return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
48197}
48198static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
48199  return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
48200}
48201
48202/*
48203** Translate a database connection pointer and schema name into a
48204** MemFile pointer.
48205*/
48206static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
48207  MemFile *p = 0;
48208  int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p);
48209  if( rc ) return 0;
48210  if( p->base.pMethods!=&memdb_io_methods ) return 0;
48211  return p;
48212}
48213
48214/*
48215** Return the serialization of a database
48216*/
48217SQLITE_API unsigned char *sqlite3_serialize(
48218  sqlite3 *db,              /* The database connection */
48219  const char *zSchema,      /* Which database within the connection */
48220  sqlite3_int64 *piSize,    /* Write size here, if not NULL */
48221  unsigned int mFlags       /* Maybe SQLITE_SERIALIZE_NOCOPY */
48222){
48223  MemFile *p;
48224  int iDb;
48225  Btree *pBt;
48226  sqlite3_int64 sz;
48227  int szPage = 0;
48228  sqlite3_stmt *pStmt = 0;
48229  unsigned char *pOut;
48230  char *zSql;
48231  int rc;
48232
48233#ifdef SQLITE_ENABLE_API_ARMOR
48234  if( !sqlite3SafetyCheckOk(db) ){
48235    (void)SQLITE_MISUSE_BKPT;
48236    return 0;
48237  }
48238#endif
48239
48240  if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
48241  p = memdbFromDbSchema(db, zSchema);
48242  iDb = sqlite3FindDbName(db, zSchema);
48243  if( piSize ) *piSize = -1;
48244  if( iDb<0 ) return 0;
48245  if( p ){
48246    if( piSize ) *piSize = p->sz;
48247    if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
48248      pOut = p->aData;
48249    }else{
48250      pOut = sqlite3_malloc64( p->sz );
48251      if( pOut ) memcpy(pOut, p->aData, p->sz);
48252    }
48253    return pOut;
48254  }
48255  pBt = db->aDb[iDb].pBt;
48256  if( pBt==0 ) return 0;
48257  szPage = sqlite3BtreeGetPageSize(pBt);
48258  zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema);
48259  rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
48260  sqlite3_free(zSql);
48261  if( rc ) return 0;
48262  rc = sqlite3_step(pStmt);
48263  if( rc!=SQLITE_ROW ){
48264    pOut = 0;
48265  }else{
48266    sz = sqlite3_column_int64(pStmt, 0)*szPage;
48267    if( piSize ) *piSize = sz;
48268    if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
48269      pOut = 0;
48270    }else{
48271      pOut = sqlite3_malloc64( sz );
48272      if( pOut ){
48273        int nPage = sqlite3_column_int(pStmt, 0);
48274        Pager *pPager = sqlite3BtreePager(pBt);
48275        int pgno;
48276        for(pgno=1; pgno<=nPage; pgno++){
48277          DbPage *pPage = 0;
48278          unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
48279          rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
48280          if( rc==SQLITE_OK ){
48281            memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
48282          }else{
48283            memset(pTo, 0, szPage);
48284          }
48285          sqlite3PagerUnref(pPage);
48286        }
48287      }
48288    }
48289  }
48290  sqlite3_finalize(pStmt);
48291  return pOut;
48292}
48293
48294/* Convert zSchema to a MemDB and initialize its content.
48295*/
48296SQLITE_API int sqlite3_deserialize(
48297  sqlite3 *db,            /* The database connection */
48298  const char *zSchema,    /* Which DB to reopen with the deserialization */
48299  unsigned char *pData,   /* The serialized database content */
48300  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
48301  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
48302  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
48303){
48304  MemFile *p;
48305  char *zSql;
48306  sqlite3_stmt *pStmt = 0;
48307  int rc;
48308  int iDb;
48309
48310#ifdef SQLITE_ENABLE_API_ARMOR
48311  if( !sqlite3SafetyCheckOk(db) ){
48312    return SQLITE_MISUSE_BKPT;
48313  }
48314  if( szDb<0 ) return SQLITE_MISUSE_BKPT;
48315  if( szBuf<0 ) return SQLITE_MISUSE_BKPT;
48316#endif
48317
48318  sqlite3_mutex_enter(db->mutex);
48319  if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
48320  iDb = sqlite3FindDbName(db, zSchema);
48321  if( iDb<0 ){
48322    rc = SQLITE_ERROR;
48323    goto end_deserialize;
48324  }
48325  zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema);
48326  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
48327  sqlite3_free(zSql);
48328  if( rc ) goto end_deserialize;
48329  db->init.iDb = (u8)iDb;
48330  db->init.reopenMemdb = 1;
48331  rc = sqlite3_step(pStmt);
48332  db->init.reopenMemdb = 0;
48333  if( rc!=SQLITE_DONE ){
48334    rc = SQLITE_ERROR;
48335    goto end_deserialize;
48336  }
48337  p = memdbFromDbSchema(db, zSchema);
48338  if( p==0 ){
48339    rc = SQLITE_ERROR;
48340  }else{
48341    p->aData = pData;
48342    p->sz = szDb;
48343    p->szAlloc = szBuf;
48344    p->szMax = szBuf;
48345    if( p->szMax<sqlite3GlobalConfig.mxMemdbSize ){
48346      p->szMax = sqlite3GlobalConfig.mxMemdbSize;
48347    }
48348    p->mFlags = mFlags;
48349    rc = SQLITE_OK;
48350  }
48351
48352end_deserialize:
48353  sqlite3_finalize(pStmt);
48354  sqlite3_mutex_leave(db->mutex);
48355  return rc;
48356}
48357
48358/*
48359** This routine is called when the extension is loaded.
48360** Register the new VFS.
48361*/
48362SQLITE_PRIVATE int sqlite3MemdbInit(void){
48363  sqlite3_vfs *pLower = sqlite3_vfs_find(0);
48364  int sz = pLower->szOsFile;
48365  memdb_vfs.pAppData = pLower;
48366  /* The following conditional can only be true when compiled for
48367  ** Windows x86 and SQLITE_MAX_MMAP_SIZE=0.  We always leave
48368  ** it in, to be safe, but it is marked as NO_TEST since there
48369  ** is no way to reach it under most builds. */
48370  if( sz<sizeof(MemFile) ) sz = sizeof(MemFile); /*NO_TEST*/
48371  memdb_vfs.szOsFile = sz;
48372  return sqlite3_vfs_register(&memdb_vfs, 0);
48373}
48374#endif /* SQLITE_ENABLE_DESERIALIZE */
48375
48376/************** End of memdb.c ***********************************************/
48377/************** Begin file bitvec.c ******************************************/
48378/*
48379** 2008 February 16
48380**
48381** The author disclaims copyright to this source code.  In place of
48382** a legal notice, here is a blessing:
48383**
48384**    May you do good and not evil.
48385**    May you find forgiveness for yourself and forgive others.
48386**    May you share freely, never taking more than you give.
48387**
48388*************************************************************************
48389** This file implements an object that represents a fixed-length
48390** bitmap.  Bits are numbered starting with 1.
48391**
48392** A bitmap is used to record which pages of a database file have been
48393** journalled during a transaction, or which pages have the "dont-write"
48394** property.  Usually only a few pages are meet either condition.
48395** So the bitmap is usually sparse and has low cardinality.
48396** But sometimes (for example when during a DROP of a large table) most
48397** or all of the pages in a database can get journalled.  In those cases,
48398** the bitmap becomes dense with high cardinality.  The algorithm needs
48399** to handle both cases well.
48400**
48401** The size of the bitmap is fixed when the object is created.
48402**
48403** All bits are clear when the bitmap is created.  Individual bits
48404** may be set or cleared one at a time.
48405**
48406** Test operations are about 100 times more common that set operations.
48407** Clear operations are exceedingly rare.  There are usually between
48408** 5 and 500 set operations per Bitvec object, though the number of sets can
48409** sometimes grow into tens of thousands or larger.  The size of the
48410** Bitvec object is the number of pages in the database file at the
48411** start of a transaction, and is thus usually less than a few thousand,
48412** but can be as large as 2 billion for a really big database.
48413*/
48414/* #include "sqliteInt.h" */
48415
48416/* Size of the Bitvec structure in bytes. */
48417#define BITVEC_SZ        512
48418
48419/* Round the union size down to the nearest pointer boundary, since that's how
48420** it will be aligned within the Bitvec struct. */
48421#define BITVEC_USIZE \
48422    (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
48423
48424/* Type of the array "element" for the bitmap representation.
48425** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
48426** Setting this to the "natural word" size of your CPU may improve
48427** performance. */
48428#define BITVEC_TELEM     u8
48429/* Size, in bits, of the bitmap element. */
48430#define BITVEC_SZELEM    8
48431/* Number of elements in a bitmap array. */
48432#define BITVEC_NELEM     (BITVEC_USIZE/sizeof(BITVEC_TELEM))
48433/* Number of bits in the bitmap array. */
48434#define BITVEC_NBIT      (BITVEC_NELEM*BITVEC_SZELEM)
48435
48436/* Number of u32 values in hash table. */
48437#define BITVEC_NINT      (BITVEC_USIZE/sizeof(u32))
48438/* Maximum number of entries in hash table before
48439** sub-dividing and re-hashing. */
48440#define BITVEC_MXHASH    (BITVEC_NINT/2)
48441/* Hashing function for the aHash representation.
48442** Empirical testing showed that the *37 multiplier
48443** (an arbitrary prime)in the hash function provided
48444** no fewer collisions than the no-op *1. */
48445#define BITVEC_HASH(X)   (((X)*1)%BITVEC_NINT)
48446
48447#define BITVEC_NPTR      (BITVEC_USIZE/sizeof(Bitvec *))
48448
48449
48450/*
48451** A bitmap is an instance of the following structure.
48452**
48453** This bitmap records the existence of zero or more bits
48454** with values between 1 and iSize, inclusive.
48455**
48456** There are three possible representations of the bitmap.
48457** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
48458** bitmap.  The least significant bit is bit 1.
48459**
48460** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
48461** a hash table that will hold up to BITVEC_MXHASH distinct values.
48462**
48463** Otherwise, the value i is redirected into one of BITVEC_NPTR
48464** sub-bitmaps pointed to by Bitvec.u.apSub[].  Each subbitmap
48465** handles up to iDivisor separate values of i.  apSub[0] holds
48466** values between 1 and iDivisor.  apSub[1] holds values between
48467** iDivisor+1 and 2*iDivisor.  apSub[N] holds values between
48468** N*iDivisor+1 and (N+1)*iDivisor.  Each subbitmap is normalized
48469** to hold deal with values between 1 and iDivisor.
48470*/
48471struct Bitvec {
48472  u32 iSize;      /* Maximum bit index.  Max iSize is 4,294,967,296. */
48473  u32 nSet;       /* Number of bits that are set - only valid for aHash
48474                  ** element.  Max is BITVEC_NINT.  For BITVEC_SZ of 512,
48475                  ** this would be 125. */
48476  u32 iDivisor;   /* Number of bits handled by each apSub[] entry. */
48477                  /* Should >=0 for apSub element. */
48478                  /* Max iDivisor is max(u32) / BITVEC_NPTR + 1.  */
48479                  /* For a BITVEC_SZ of 512, this would be 34,359,739. */
48480  union {
48481    BITVEC_TELEM aBitmap[BITVEC_NELEM];    /* Bitmap representation */
48482    u32 aHash[BITVEC_NINT];      /* Hash table representation */
48483    Bitvec *apSub[BITVEC_NPTR];  /* Recursive representation */
48484  } u;
48485};
48486
48487/*
48488** Create a new bitmap object able to handle bits between 0 and iSize,
48489** inclusive.  Return a pointer to the new object.  Return NULL if
48490** malloc fails.
48491*/
48492SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
48493  Bitvec *p;
48494  assert( sizeof(*p)==BITVEC_SZ );
48495  p = sqlite3MallocZero( sizeof(*p) );
48496  if( p ){
48497    p->iSize = iSize;
48498  }
48499  return p;
48500}
48501
48502/*
48503** Check to see if the i-th bit is set.  Return true or false.
48504** If p is NULL (if the bitmap has not been created) or if
48505** i is out of range, then return false.
48506*/
48507SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
48508  assert( p!=0 );
48509  i--;
48510  if( i>=p->iSize ) return 0;
48511  while( p->iDivisor ){
48512    u32 bin = i/p->iDivisor;
48513    i = i%p->iDivisor;
48514    p = p->u.apSub[bin];
48515    if (!p) {
48516      return 0;
48517    }
48518  }
48519  if( p->iSize<=BITVEC_NBIT ){
48520    return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
48521  } else{
48522    u32 h = BITVEC_HASH(i++);
48523    while( p->u.aHash[h] ){
48524      if( p->u.aHash[h]==i ) return 1;
48525      h = (h+1) % BITVEC_NINT;
48526    }
48527    return 0;
48528  }
48529}
48530SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
48531  return p!=0 && sqlite3BitvecTestNotNull(p,i);
48532}
48533
48534/*
48535** Set the i-th bit.  Return 0 on success and an error code if
48536** anything goes wrong.
48537**
48538** This routine might cause sub-bitmaps to be allocated.  Failing
48539** to get the memory needed to hold the sub-bitmap is the only
48540** that can go wrong with an insert, assuming p and i are valid.
48541**
48542** The calling function must ensure that p is a valid Bitvec object
48543** and that the value for "i" is within range of the Bitvec object.
48544** Otherwise the behavior is undefined.
48545*/
48546SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
48547  u32 h;
48548  if( p==0 ) return SQLITE_OK;
48549  assert( i>0 );
48550  assert( i<=p->iSize );
48551  i--;
48552  while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
48553    u32 bin = i/p->iDivisor;
48554    i = i%p->iDivisor;
48555    if( p->u.apSub[bin]==0 ){
48556      p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
48557      if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
48558    }
48559    p = p->u.apSub[bin];
48560  }
48561  if( p->iSize<=BITVEC_NBIT ){
48562    p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
48563    return SQLITE_OK;
48564  }
48565  h = BITVEC_HASH(i++);
48566  /* if there wasn't a hash collision, and this doesn't */
48567  /* completely fill the hash, then just add it without */
48568  /* worring about sub-dividing and re-hashing. */
48569  if( !p->u.aHash[h] ){
48570    if (p->nSet<(BITVEC_NINT-1)) {
48571      goto bitvec_set_end;
48572    } else {
48573      goto bitvec_set_rehash;
48574    }
48575  }
48576  /* there was a collision, check to see if it's already */
48577  /* in hash, if not, try to find a spot for it */
48578  do {
48579    if( p->u.aHash[h]==i ) return SQLITE_OK;
48580    h++;
48581    if( h>=BITVEC_NINT ) h = 0;
48582  } while( p->u.aHash[h] );
48583  /* we didn't find it in the hash.  h points to the first */
48584  /* available free spot. check to see if this is going to */
48585  /* make our hash too "full".  */
48586bitvec_set_rehash:
48587  if( p->nSet>=BITVEC_MXHASH ){
48588    unsigned int j;
48589    int rc;
48590    u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
48591    if( aiValues==0 ){
48592      return SQLITE_NOMEM_BKPT;
48593    }else{
48594      memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
48595      memset(p->u.apSub, 0, sizeof(p->u.apSub));
48596      p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
48597      rc = sqlite3BitvecSet(p, i);
48598      for(j=0; j<BITVEC_NINT; j++){
48599        if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
48600      }
48601      sqlite3StackFree(0, aiValues);
48602      return rc;
48603    }
48604  }
48605bitvec_set_end:
48606  p->nSet++;
48607  p->u.aHash[h] = i;
48608  return SQLITE_OK;
48609}
48610
48611/*
48612** Clear the i-th bit.
48613**
48614** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
48615** that BitvecClear can use to rebuilt its hash table.
48616*/
48617SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
48618  if( p==0 ) return;
48619  assert( i>0 );
48620  i--;
48621  while( p->iDivisor ){
48622    u32 bin = i/p->iDivisor;
48623    i = i%p->iDivisor;
48624    p = p->u.apSub[bin];
48625    if (!p) {
48626      return;
48627    }
48628  }
48629  if( p->iSize<=BITVEC_NBIT ){
48630    p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
48631  }else{
48632    unsigned int j;
48633    u32 *aiValues = pBuf;
48634    memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
48635    memset(p->u.aHash, 0, sizeof(p->u.aHash));
48636    p->nSet = 0;
48637    for(j=0; j<BITVEC_NINT; j++){
48638      if( aiValues[j] && aiValues[j]!=(i+1) ){
48639        u32 h = BITVEC_HASH(aiValues[j]-1);
48640        p->nSet++;
48641        while( p->u.aHash[h] ){
48642          h++;
48643          if( h>=BITVEC_NINT ) h = 0;
48644        }
48645        p->u.aHash[h] = aiValues[j];
48646      }
48647    }
48648  }
48649}
48650
48651/*
48652** Destroy a bitmap object.  Reclaim all memory used.
48653*/
48654SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
48655  if( p==0 ) return;
48656  if( p->iDivisor ){
48657    unsigned int i;
48658    for(i=0; i<BITVEC_NPTR; i++){
48659      sqlite3BitvecDestroy(p->u.apSub[i]);
48660    }
48661  }
48662  sqlite3_free(p);
48663}
48664
48665/*
48666** Return the value of the iSize parameter specified when Bitvec *p
48667** was created.
48668*/
48669SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
48670  return p->iSize;
48671}
48672
48673#ifndef SQLITE_UNTESTABLE
48674/*
48675** Let V[] be an array of unsigned characters sufficient to hold
48676** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
48677** Then the following macros can be used to set, clear, or test
48678** individual bits within V.
48679*/
48680#define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
48681#define CLEARBIT(V,I)    V[I>>3] &= ~(1<<(I&7))
48682#define TESTBIT(V,I)     (V[I>>3]&(1<<(I&7)))!=0
48683
48684/*
48685** This routine runs an extensive test of the Bitvec code.
48686**
48687** The input is an array of integers that acts as a program
48688** to test the Bitvec.  The integers are opcodes followed
48689** by 0, 1, or 3 operands, depending on the opcode.  Another
48690** opcode follows immediately after the last operand.
48691**
48692** There are 6 opcodes numbered from 0 through 5.  0 is the
48693** "halt" opcode and causes the test to end.
48694**
48695**    0          Halt and return the number of errors
48696**    1 N S X    Set N bits beginning with S and incrementing by X
48697**    2 N S X    Clear N bits beginning with S and incrementing by X
48698**    3 N        Set N randomly chosen bits
48699**    4 N        Clear N randomly chosen bits
48700**    5 N S X    Set N bits from S increment X in array only, not in bitvec
48701**
48702** The opcodes 1 through 4 perform set and clear operations are performed
48703** on both a Bitvec object and on a linear array of bits obtained from malloc.
48704** Opcode 5 works on the linear array only, not on the Bitvec.
48705** Opcode 5 is used to deliberately induce a fault in order to
48706** confirm that error detection works.
48707**
48708** At the conclusion of the test the linear array is compared
48709** against the Bitvec object.  If there are any differences,
48710** an error is returned.  If they are the same, zero is returned.
48711**
48712** If a memory allocation error occurs, return -1.
48713*/
48714SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
48715  Bitvec *pBitvec = 0;
48716  unsigned char *pV = 0;
48717  int rc = -1;
48718  int i, nx, pc, op;
48719  void *pTmpSpace;
48720
48721  /* Allocate the Bitvec to be tested and a linear array of
48722  ** bits to act as the reference */
48723  pBitvec = sqlite3BitvecCreate( sz );
48724  pV = sqlite3MallocZero( (sz+7)/8 + 1 );
48725  pTmpSpace = sqlite3_malloc64(BITVEC_SZ);
48726  if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
48727
48728  /* NULL pBitvec tests */
48729  sqlite3BitvecSet(0, 1);
48730  sqlite3BitvecClear(0, 1, pTmpSpace);
48731
48732  /* Run the program */
48733  pc = 0;
48734  while( (op = aOp[pc])!=0 ){
48735    switch( op ){
48736      case 1:
48737      case 2:
48738      case 5: {
48739        nx = 4;
48740        i = aOp[pc+2] - 1;
48741        aOp[pc+2] += aOp[pc+3];
48742        break;
48743      }
48744      case 3:
48745      case 4:
48746      default: {
48747        nx = 2;
48748        sqlite3_randomness(sizeof(i), &i);
48749        break;
48750      }
48751    }
48752    if( (--aOp[pc+1]) > 0 ) nx = 0;
48753    pc += nx;
48754    i = (i & 0x7fffffff)%sz;
48755    if( (op & 1)!=0 ){
48756      SETBIT(pV, (i+1));
48757      if( op!=5 ){
48758        if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
48759      }
48760    }else{
48761      CLEARBIT(pV, (i+1));
48762      sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
48763    }
48764  }
48765
48766  /* Test to make sure the linear array exactly matches the
48767  ** Bitvec object.  Start with the assumption that they do
48768  ** match (rc==0).  Change rc to non-zero if a discrepancy
48769  ** is found.
48770  */
48771  rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
48772          + sqlite3BitvecTest(pBitvec, 0)
48773          + (sqlite3BitvecSize(pBitvec) - sz);
48774  for(i=1; i<=sz; i++){
48775    if(  (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
48776      rc = i;
48777      break;
48778    }
48779  }
48780
48781  /* Free allocated structure */
48782bitvec_end:
48783  sqlite3_free(pTmpSpace);
48784  sqlite3_free(pV);
48785  sqlite3BitvecDestroy(pBitvec);
48786  return rc;
48787}
48788#endif /* SQLITE_UNTESTABLE */
48789
48790/************** End of bitvec.c **********************************************/
48791/************** Begin file pcache.c ******************************************/
48792/*
48793** 2008 August 05
48794**
48795** The author disclaims copyright to this source code.  In place of
48796** a legal notice, here is a blessing:
48797**
48798**    May you do good and not evil.
48799**    May you find forgiveness for yourself and forgive others.
48800**    May you share freely, never taking more than you give.
48801**
48802*************************************************************************
48803** This file implements that page cache.
48804*/
48805/* #include "sqliteInt.h" */
48806
48807/*
48808** A complete page cache is an instance of this structure.  Every
48809** entry in the cache holds a single page of the database file.  The
48810** btree layer only operates on the cached copy of the database pages.
48811**
48812** A page cache entry is "clean" if it exactly matches what is currently
48813** on disk.  A page is "dirty" if it has been modified and needs to be
48814** persisted to disk.
48815**
48816** pDirty, pDirtyTail, pSynced:
48817**   All dirty pages are linked into the doubly linked list using
48818**   PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
48819**   such that p was added to the list more recently than p->pDirtyNext.
48820**   PCache.pDirty points to the first (newest) element in the list and
48821**   pDirtyTail to the last (oldest).
48822**
48823**   The PCache.pSynced variable is used to optimize searching for a dirty
48824**   page to eject from the cache mid-transaction. It is better to eject
48825**   a page that does not require a journal sync than one that does.
48826**   Therefore, pSynced is maintained so that it *almost* always points
48827**   to either the oldest page in the pDirty/pDirtyTail list that has a
48828**   clear PGHDR_NEED_SYNC flag or to a page that is older than this one
48829**   (so that the right page to eject can be found by following pDirtyPrev
48830**   pointers).
48831*/
48832struct PCache {
48833  PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
48834  PgHdr *pSynced;                     /* Last synced page in dirty page list */
48835  int nRefSum;                        /* Sum of ref counts over all pages */
48836  int szCache;                        /* Configured cache size */
48837  int szSpill;                        /* Size before spilling occurs */
48838  int szPage;                         /* Size of every page in this cache */
48839  int szExtra;                        /* Size of extra space for each page */
48840  u8 bPurgeable;                      /* True if pages are on backing store */
48841  u8 eCreate;                         /* eCreate value for for xFetch() */
48842  int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
48843  void *pStress;                      /* Argument to xStress */
48844  sqlite3_pcache *pCache;             /* Pluggable cache module */
48845};
48846
48847/********************************** Test and Debug Logic **********************/
48848/*
48849** Debug tracing macros.  Enable by by changing the "0" to "1" and
48850** recompiling.
48851**
48852** When sqlite3PcacheTrace is 1, single line trace messages are issued.
48853** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
48854** is displayed for many operations, resulting in a lot of output.
48855*/
48856#if defined(SQLITE_DEBUG) && 0
48857  int sqlite3PcacheTrace = 2;       /* 0: off  1: simple  2: cache dumps */
48858  int sqlite3PcacheMxDump = 9999;   /* Max cache entries for pcacheDump() */
48859# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
48860  void pcacheDump(PCache *pCache){
48861    int N;
48862    int i, j;
48863    sqlite3_pcache_page *pLower;
48864    PgHdr *pPg;
48865    unsigned char *a;
48866
48867    if( sqlite3PcacheTrace<2 ) return;
48868    if( pCache->pCache==0 ) return;
48869    N = sqlite3PcachePagecount(pCache);
48870    if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
48871    for(i=1; i<=N; i++){
48872       pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
48873       if( pLower==0 ) continue;
48874       pPg = (PgHdr*)pLower->pExtra;
48875       printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
48876       a = (unsigned char *)pLower->pBuf;
48877       for(j=0; j<12; j++) printf("%02x", a[j]);
48878       printf("\n");
48879       if( pPg->pPage==0 ){
48880         sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
48881       }
48882    }
48883  }
48884  #else
48885# define pcacheTrace(X)
48886# define pcacheDump(X)
48887#endif
48888
48889/*
48890** Check invariants on a PgHdr entry.  Return true if everything is OK.
48891** Return false if any invariant is violated.
48892**
48893** This routine is for use inside of assert() statements only.  For
48894** example:
48895**
48896**          assert( sqlite3PcachePageSanity(pPg) );
48897*/
48898#ifdef SQLITE_DEBUG
48899SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr *pPg){
48900  PCache *pCache;
48901  assert( pPg!=0 );
48902  assert( pPg->pgno>0 || pPg->pPager==0 );    /* Page number is 1 or more */
48903  pCache = pPg->pCache;
48904  assert( pCache!=0 );      /* Every page has an associated PCache */
48905  if( pPg->flags & PGHDR_CLEAN ){
48906    assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
48907    assert( pCache->pDirty!=pPg );          /* CLEAN pages not on dirty list */
48908    assert( pCache->pDirtyTail!=pPg );
48909  }
48910  /* WRITEABLE pages must also be DIRTY */
48911  if( pPg->flags & PGHDR_WRITEABLE ){
48912    assert( pPg->flags & PGHDR_DIRTY );     /* WRITEABLE implies DIRTY */
48913  }
48914  /* NEED_SYNC can be set independently of WRITEABLE.  This can happen,
48915  ** for example, when using the sqlite3PagerDontWrite() optimization:
48916  **    (1)  Page X is journalled, and gets WRITEABLE and NEED_SEEK.
48917  **    (2)  Page X moved to freelist, WRITEABLE is cleared
48918  **    (3)  Page X reused, WRITEABLE is set again
48919  ** If NEED_SYNC had been cleared in step 2, then it would not be reset
48920  ** in step 3, and page might be written into the database without first
48921  ** syncing the rollback journal, which might cause corruption on a power
48922  ** loss.
48923  **
48924  ** Another example is when the database page size is smaller than the
48925  ** disk sector size.  When any page of a sector is journalled, all pages
48926  ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
48927  ** in case they are later modified, since all pages in the same sector
48928  ** must be journalled and synced before any of those pages can be safely
48929  ** written.
48930  */
48931  return 1;
48932}
48933#endif /* SQLITE_DEBUG */
48934
48935
48936/********************************** Linked List Management ********************/
48937
48938/* Allowed values for second argument to pcacheManageDirtyList() */
48939#define PCACHE_DIRTYLIST_REMOVE   1    /* Remove pPage from dirty list */
48940#define PCACHE_DIRTYLIST_ADD      2    /* Add pPage to the dirty list */
48941#define PCACHE_DIRTYLIST_FRONT    3    /* Move pPage to the front of the list */
48942
48943/*
48944** Manage pPage's participation on the dirty list.  Bits of the addRemove
48945** argument determines what operation to do.  The 0x01 bit means first
48946** remove pPage from the dirty list.  The 0x02 means add pPage back to
48947** the dirty list.  Doing both moves pPage to the front of the dirty list.
48948*/
48949static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
48950  PCache *p = pPage->pCache;
48951
48952  pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
48953                addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
48954                pPage->pgno));
48955  if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
48956    assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
48957    assert( pPage->pDirtyPrev || pPage==p->pDirty );
48958
48959    /* Update the PCache1.pSynced variable if necessary. */
48960    if( p->pSynced==pPage ){
48961      p->pSynced = pPage->pDirtyPrev;
48962    }
48963
48964    if( pPage->pDirtyNext ){
48965      pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
48966    }else{
48967      assert( pPage==p->pDirtyTail );
48968      p->pDirtyTail = pPage->pDirtyPrev;
48969    }
48970    if( pPage->pDirtyPrev ){
48971      pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
48972    }else{
48973      /* If there are now no dirty pages in the cache, set eCreate to 2.
48974      ** This is an optimization that allows sqlite3PcacheFetch() to skip
48975      ** searching for a dirty page to eject from the cache when it might
48976      ** otherwise have to.  */
48977      assert( pPage==p->pDirty );
48978      p->pDirty = pPage->pDirtyNext;
48979      assert( p->bPurgeable || p->eCreate==2 );
48980      if( p->pDirty==0 ){         /*OPTIMIZATION-IF-TRUE*/
48981        assert( p->bPurgeable==0 || p->eCreate==1 );
48982        p->eCreate = 2;
48983      }
48984    }
48985  }
48986  if( addRemove & PCACHE_DIRTYLIST_ADD ){
48987    pPage->pDirtyPrev = 0;
48988    pPage->pDirtyNext = p->pDirty;
48989    if( pPage->pDirtyNext ){
48990      assert( pPage->pDirtyNext->pDirtyPrev==0 );
48991      pPage->pDirtyNext->pDirtyPrev = pPage;
48992    }else{
48993      p->pDirtyTail = pPage;
48994      if( p->bPurgeable ){
48995        assert( p->eCreate==2 );
48996        p->eCreate = 1;
48997      }
48998    }
48999    p->pDirty = pPage;
49000
49001    /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
49002    ** pSynced to point to it. Checking the NEED_SYNC flag is an
49003    ** optimization, as if pSynced points to a page with the NEED_SYNC
49004    ** flag set sqlite3PcacheFetchStress() searches through all newer
49005    ** entries of the dirty-list for a page with NEED_SYNC clear anyway.  */
49006    if( !p->pSynced
49007     && 0==(pPage->flags&PGHDR_NEED_SYNC)   /*OPTIMIZATION-IF-FALSE*/
49008    ){
49009      p->pSynced = pPage;
49010    }
49011  }
49012  pcacheDump(p);
49013}
49014
49015/*
49016** Wrapper around the pluggable caches xUnpin method. If the cache is
49017** being used for an in-memory database, this function is a no-op.
49018*/
49019static void pcacheUnpin(PgHdr *p){
49020  if( p->pCache->bPurgeable ){
49021    pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
49022    sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
49023    pcacheDump(p->pCache);
49024  }
49025}
49026
49027/*
49028** Compute the number of pages of cache requested.   p->szCache is the
49029** cache size requested by the "PRAGMA cache_size" statement.
49030*/
49031static int numberOfCachePages(PCache *p){
49032  if( p->szCache>=0 ){
49033    /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
49034    ** suggested cache size is set to N. */
49035    return p->szCache;
49036  }else{
49037    /* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
49038    ** number of cache pages is adjusted to be a number of pages that would
49039    ** use approximately abs(N*1024) bytes of memory based on the current
49040    ** page size. */
49041    return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
49042  }
49043}
49044
49045/*************************************************** General Interfaces ******
49046**
49047** Initialize and shutdown the page cache subsystem. Neither of these
49048** functions are threadsafe.
49049*/
49050SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
49051  if( sqlite3GlobalConfig.pcache2.xInit==0 ){
49052    /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
49053    ** built-in default page cache is used instead of the application defined
49054    ** page cache. */
49055    sqlite3PCacheSetDefault();
49056    assert( sqlite3GlobalConfig.pcache2.xInit!=0 );
49057  }
49058  return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
49059}
49060SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
49061  if( sqlite3GlobalConfig.pcache2.xShutdown ){
49062    /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
49063    sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
49064  }
49065}
49066
49067/*
49068** Return the size in bytes of a PCache object.
49069*/
49070SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
49071
49072/*
49073** Create a new PCache object. Storage space to hold the object
49074** has already been allocated and is passed in as the p pointer.
49075** The caller discovers how much space needs to be allocated by
49076** calling sqlite3PcacheSize().
49077**
49078** szExtra is some extra space allocated for each page.  The first
49079** 8 bytes of the extra space will be zeroed as the page is allocated,
49080** but remaining content will be uninitialized.  Though it is opaque
49081** to this module, the extra space really ends up being the MemPage
49082** structure in the pager.
49083*/
49084SQLITE_PRIVATE int sqlite3PcacheOpen(
49085  int szPage,                  /* Size of every page */
49086  int szExtra,                 /* Extra space associated with each page */
49087  int bPurgeable,              /* True if pages are on backing store */
49088  int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
49089  void *pStress,               /* Argument to xStress */
49090  PCache *p                    /* Preallocated space for the PCache */
49091){
49092  memset(p, 0, sizeof(PCache));
49093  p->szPage = 1;
49094  p->szExtra = szExtra;
49095  assert( szExtra>=8 );  /* First 8 bytes will be zeroed */
49096  p->bPurgeable = bPurgeable;
49097  p->eCreate = 2;
49098  p->xStress = xStress;
49099  p->pStress = pStress;
49100  p->szCache = 100;
49101  p->szSpill = 1;
49102  pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
49103  return sqlite3PcacheSetPageSize(p, szPage);
49104}
49105
49106/*
49107** Change the page size for PCache object. The caller must ensure that there
49108** are no outstanding page references when this function is called.
49109*/
49110SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
49111  assert( pCache->nRefSum==0 && pCache->pDirty==0 );
49112  if( pCache->szPage ){
49113    sqlite3_pcache *pNew;
49114    pNew = sqlite3GlobalConfig.pcache2.xCreate(
49115                szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
49116                pCache->bPurgeable
49117    );
49118    if( pNew==0 ) return SQLITE_NOMEM_BKPT;
49119    sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
49120    if( pCache->pCache ){
49121      sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
49122    }
49123    pCache->pCache = pNew;
49124    pCache->szPage = szPage;
49125    pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
49126  }
49127  return SQLITE_OK;
49128}
49129
49130/*
49131** Try to obtain a page from the cache.
49132**
49133** This routine returns a pointer to an sqlite3_pcache_page object if
49134** such an object is already in cache, or if a new one is created.
49135** This routine returns a NULL pointer if the object was not in cache
49136** and could not be created.
49137**
49138** The createFlags should be 0 to check for existing pages and should
49139** be 3 (not 1, but 3) to try to create a new page.
49140**
49141** If the createFlag is 0, then NULL is always returned if the page
49142** is not already in the cache.  If createFlag is 1, then a new page
49143** is created only if that can be done without spilling dirty pages
49144** and without exceeding the cache size limit.
49145**
49146** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
49147** initialize the sqlite3_pcache_page object and convert it into a
49148** PgHdr object.  The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
49149** routines are split this way for performance reasons. When separated
49150** they can both (usually) operate without having to push values to
49151** the stack on entry and pop them back off on exit, which saves a
49152** lot of pushing and popping.
49153*/
49154SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(
49155  PCache *pCache,       /* Obtain the page from this cache */
49156  Pgno pgno,            /* Page number to obtain */
49157  int createFlag        /* If true, create page if it does not exist already */
49158){
49159  int eCreate;
49160  sqlite3_pcache_page *pRes;
49161
49162  assert( pCache!=0 );
49163  assert( pCache->pCache!=0 );
49164  assert( createFlag==3 || createFlag==0 );
49165  assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
49166
49167  /* eCreate defines what to do if the page does not exist.
49168  **    0     Do not allocate a new page.  (createFlag==0)
49169  **    1     Allocate a new page if doing so is inexpensive.
49170  **          (createFlag==1 AND bPurgeable AND pDirty)
49171  **    2     Allocate a new page even it doing so is difficult.
49172  **          (createFlag==1 AND !(bPurgeable AND pDirty)
49173  */
49174  eCreate = createFlag & pCache->eCreate;
49175  assert( eCreate==0 || eCreate==1 || eCreate==2 );
49176  assert( createFlag==0 || pCache->eCreate==eCreate );
49177  assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
49178  pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
49179  pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
49180               createFlag?" create":"",pRes));
49181  return pRes;
49182}
49183
49184/*
49185** If the sqlite3PcacheFetch() routine is unable to allocate a new
49186** page because no clean pages are available for reuse and the cache
49187** size limit has been reached, then this routine can be invoked to
49188** try harder to allocate a page.  This routine might invoke the stress
49189** callback to spill dirty pages to the journal.  It will then try to
49190** allocate the new page and will only fail to allocate a new page on
49191** an OOM error.
49192**
49193** This routine should be invoked only after sqlite3PcacheFetch() fails.
49194*/
49195SQLITE_PRIVATE int sqlite3PcacheFetchStress(
49196  PCache *pCache,                 /* Obtain the page from this cache */
49197  Pgno pgno,                      /* Page number to obtain */
49198  sqlite3_pcache_page **ppPage    /* Write result here */
49199){
49200  PgHdr *pPg;
49201  if( pCache->eCreate==2 ) return 0;
49202
49203  if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
49204    /* Find a dirty page to write-out and recycle. First try to find a
49205    ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
49206    ** cleared), but if that is not possible settle for any other
49207    ** unreferenced dirty page.
49208    **
49209    ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
49210    ** flag is currently referenced, then the following may leave pSynced
49211    ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
49212    ** cleared). This is Ok, as pSynced is just an optimization.  */
49213    for(pPg=pCache->pSynced;
49214        pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
49215        pPg=pPg->pDirtyPrev
49216    );
49217    pCache->pSynced = pPg;
49218    if( !pPg ){
49219      for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
49220    }
49221    if( pPg ){
49222      int rc;
49223#ifdef SQLITE_LOG_CACHE_SPILL
49224      sqlite3_log(SQLITE_FULL,
49225                  "spill page %d making room for %d - cache used: %d/%d",
49226                  pPg->pgno, pgno,
49227                  sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
49228                numberOfCachePages(pCache));
49229#endif
49230      pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
49231      rc = pCache->xStress(pCache->pStress, pPg);
49232      pcacheDump(pCache);
49233      if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
49234        return rc;
49235      }
49236    }
49237  }
49238  *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
49239  return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
49240}
49241
49242/*
49243** This is a helper routine for sqlite3PcacheFetchFinish()
49244**
49245** In the uncommon case where the page being fetched has not been
49246** initialized, this routine is invoked to do the initialization.
49247** This routine is broken out into a separate function since it
49248** requires extra stack manipulation that can be avoided in the common
49249** case.
49250*/
49251static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
49252  PCache *pCache,             /* Obtain the page from this cache */
49253  Pgno pgno,                  /* Page number obtained */
49254  sqlite3_pcache_page *pPage  /* Page obtained by prior PcacheFetch() call */
49255){
49256  PgHdr *pPgHdr;
49257  assert( pPage!=0 );
49258  pPgHdr = (PgHdr*)pPage->pExtra;
49259  assert( pPgHdr->pPage==0 );
49260  memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
49261  pPgHdr->pPage = pPage;
49262  pPgHdr->pData = pPage->pBuf;
49263  pPgHdr->pExtra = (void *)&pPgHdr[1];
49264  memset(pPgHdr->pExtra, 0, 8);
49265  pPgHdr->pCache = pCache;
49266  pPgHdr->pgno = pgno;
49267  pPgHdr->flags = PGHDR_CLEAN;
49268  return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
49269}
49270
49271/*
49272** This routine converts the sqlite3_pcache_page object returned by
49273** sqlite3PcacheFetch() into an initialized PgHdr object.  This routine
49274** must be called after sqlite3PcacheFetch() in order to get a usable
49275** result.
49276*/
49277SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(
49278  PCache *pCache,             /* Obtain the page from this cache */
49279  Pgno pgno,                  /* Page number obtained */
49280  sqlite3_pcache_page *pPage  /* Page obtained by prior PcacheFetch() call */
49281){
49282  PgHdr *pPgHdr;
49283
49284  assert( pPage!=0 );
49285  pPgHdr = (PgHdr *)pPage->pExtra;
49286
49287  if( !pPgHdr->pPage ){
49288    return pcacheFetchFinishWithInit(pCache, pgno, pPage);
49289  }
49290  pCache->nRefSum++;
49291  pPgHdr->nRef++;
49292  assert( sqlite3PcachePageSanity(pPgHdr) );
49293  return pPgHdr;
49294}
49295
49296/*
49297** Decrement the reference count on a page. If the page is clean and the
49298** reference count drops to 0, then it is made eligible for recycling.
49299*/
49300SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
49301  assert( p->nRef>0 );
49302  p->pCache->nRefSum--;
49303  if( (--p->nRef)==0 ){
49304    if( p->flags&PGHDR_CLEAN ){
49305      pcacheUnpin(p);
49306    }else{
49307      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
49308    }
49309  }
49310}
49311
49312/*
49313** Increase the reference count of a supplied page by 1.
49314*/
49315SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
49316  assert(p->nRef>0);
49317  assert( sqlite3PcachePageSanity(p) );
49318  p->nRef++;
49319  p->pCache->nRefSum++;
49320}
49321
49322/*
49323** Drop a page from the cache. There must be exactly one reference to the
49324** page. This function deletes that reference, so after it returns the
49325** page pointed to by p is invalid.
49326*/
49327SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
49328  assert( p->nRef==1 );
49329  assert( sqlite3PcachePageSanity(p) );
49330  if( p->flags&PGHDR_DIRTY ){
49331    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
49332  }
49333  p->pCache->nRefSum--;
49334  sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
49335}
49336
49337/*
49338** Make sure the page is marked as dirty. If it isn't dirty already,
49339** make it so.
49340*/
49341SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
49342  assert( p->nRef>0 );
49343  assert( sqlite3PcachePageSanity(p) );
49344  if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){    /*OPTIMIZATION-IF-FALSE*/
49345    p->flags &= ~PGHDR_DONT_WRITE;
49346    if( p->flags & PGHDR_CLEAN ){
49347      p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
49348      pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
49349      assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
49350      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
49351    }
49352    assert( sqlite3PcachePageSanity(p) );
49353  }
49354}
49355
49356/*
49357** Make sure the page is marked as clean. If it isn't clean already,
49358** make it so.
49359*/
49360SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
49361  assert( sqlite3PcachePageSanity(p) );
49362  assert( (p->flags & PGHDR_DIRTY)!=0 );
49363  assert( (p->flags & PGHDR_CLEAN)==0 );
49364  pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
49365  p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
49366  p->flags |= PGHDR_CLEAN;
49367  pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
49368  assert( sqlite3PcachePageSanity(p) );
49369  if( p->nRef==0 ){
49370    pcacheUnpin(p);
49371  }
49372}
49373
49374/*
49375** Make every page in the cache clean.
49376*/
49377SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
49378  PgHdr *p;
49379  pcacheTrace(("%p.CLEAN-ALL\n",pCache));
49380  while( (p = pCache->pDirty)!=0 ){
49381    sqlite3PcacheMakeClean(p);
49382  }
49383}
49384
49385/*
49386** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
49387*/
49388SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache *pCache){
49389  PgHdr *p;
49390  pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
49391  for(p=pCache->pDirty; p; p=p->pDirtyNext){
49392    p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
49393  }
49394  pCache->pSynced = pCache->pDirtyTail;
49395}
49396
49397/*
49398** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
49399*/
49400SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
49401  PgHdr *p;
49402  for(p=pCache->pDirty; p; p=p->pDirtyNext){
49403    p->flags &= ~PGHDR_NEED_SYNC;
49404  }
49405  pCache->pSynced = pCache->pDirtyTail;
49406}
49407
49408/*
49409** Change the page number of page p to newPgno.
49410*/
49411SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
49412  PCache *pCache = p->pCache;
49413  assert( p->nRef>0 );
49414  assert( newPgno>0 );
49415  assert( sqlite3PcachePageSanity(p) );
49416  pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
49417  sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
49418  p->pgno = newPgno;
49419  if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
49420    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
49421  }
49422}
49423
49424/*
49425** Drop every cache entry whose page number is greater than "pgno". The
49426** caller must ensure that there are no outstanding references to any pages
49427** other than page 1 with a page number greater than pgno.
49428**
49429** If there is a reference to page 1 and the pgno parameter passed to this
49430** function is 0, then the data area associated with page 1 is zeroed, but
49431** the page object is not dropped.
49432*/
49433SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
49434  if( pCache->pCache ){
49435    PgHdr *p;
49436    PgHdr *pNext;
49437    pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
49438    for(p=pCache->pDirty; p; p=pNext){
49439      pNext = p->pDirtyNext;
49440      /* This routine never gets call with a positive pgno except right
49441      ** after sqlite3PcacheCleanAll().  So if there are dirty pages,
49442      ** it must be that pgno==0.
49443      */
49444      assert( p->pgno>0 );
49445      if( p->pgno>pgno ){
49446        assert( p->flags&PGHDR_DIRTY );
49447        sqlite3PcacheMakeClean(p);
49448      }
49449    }
49450    if( pgno==0 && pCache->nRefSum ){
49451      sqlite3_pcache_page *pPage1;
49452      pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
49453      if( ALWAYS(pPage1) ){  /* Page 1 is always available in cache, because
49454                             ** pCache->nRefSum>0 */
49455        memset(pPage1->pBuf, 0, pCache->szPage);
49456        pgno = 1;
49457      }
49458    }
49459    sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
49460  }
49461}
49462
49463/*
49464** Close a cache.
49465*/
49466SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
49467  assert( pCache->pCache!=0 );
49468  pcacheTrace(("%p.CLOSE\n",pCache));
49469  sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
49470}
49471
49472/*
49473** Discard the contents of the cache.
49474*/
49475SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
49476  sqlite3PcacheTruncate(pCache, 0);
49477}
49478
49479/*
49480** Merge two lists of pages connected by pDirty and in pgno order.
49481** Do not bother fixing the pDirtyPrev pointers.
49482*/
49483static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
49484  PgHdr result, *pTail;
49485  pTail = &result;
49486  assert( pA!=0 && pB!=0 );
49487  for(;;){
49488    if( pA->pgno<pB->pgno ){
49489      pTail->pDirty = pA;
49490      pTail = pA;
49491      pA = pA->pDirty;
49492      if( pA==0 ){
49493        pTail->pDirty = pB;
49494        break;
49495      }
49496    }else{
49497      pTail->pDirty = pB;
49498      pTail = pB;
49499      pB = pB->pDirty;
49500      if( pB==0 ){
49501        pTail->pDirty = pA;
49502        break;
49503      }
49504    }
49505  }
49506  return result.pDirty;
49507}
49508
49509/*
49510** Sort the list of pages in accending order by pgno.  Pages are
49511** connected by pDirty pointers.  The pDirtyPrev pointers are
49512** corrupted by this sort.
49513**
49514** Since there cannot be more than 2^31 distinct pages in a database,
49515** there cannot be more than 31 buckets required by the merge sorter.
49516** One extra bucket is added to catch overflow in case something
49517** ever changes to make the previous sentence incorrect.
49518*/
49519#define N_SORT_BUCKET  32
49520static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
49521  PgHdr *a[N_SORT_BUCKET], *p;
49522  int i;
49523  memset(a, 0, sizeof(a));
49524  while( pIn ){
49525    p = pIn;
49526    pIn = p->pDirty;
49527    p->pDirty = 0;
49528    for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
49529      if( a[i]==0 ){
49530        a[i] = p;
49531        break;
49532      }else{
49533        p = pcacheMergeDirtyList(a[i], p);
49534        a[i] = 0;
49535      }
49536    }
49537    if( NEVER(i==N_SORT_BUCKET-1) ){
49538      /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
49539      ** the input list.  But that is impossible.
49540      */
49541      a[i] = pcacheMergeDirtyList(a[i], p);
49542    }
49543  }
49544  p = a[0];
49545  for(i=1; i<N_SORT_BUCKET; i++){
49546    if( a[i]==0 ) continue;
49547    p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
49548  }
49549  return p;
49550}
49551
49552/*
49553** Return a list of all dirty pages in the cache, sorted by page number.
49554*/
49555SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
49556  PgHdr *p;
49557  for(p=pCache->pDirty; p; p=p->pDirtyNext){
49558    p->pDirty = p->pDirtyNext;
49559  }
49560  return pcacheSortDirtyList(pCache->pDirty);
49561}
49562
49563/*
49564** Return the total number of references to all pages held by the cache.
49565**
49566** This is not the total number of pages referenced, but the sum of the
49567** reference count for all pages.
49568*/
49569SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
49570  return pCache->nRefSum;
49571}
49572
49573/*
49574** Return the number of references to the page supplied as an argument.
49575*/
49576SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
49577  return p->nRef;
49578}
49579
49580/*
49581** Return the total number of pages in the cache.
49582*/
49583SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
49584  assert( pCache->pCache!=0 );
49585  return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
49586}
49587
49588#ifdef SQLITE_TEST
49589/*
49590** Get the suggested cache-size value.
49591*/
49592SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
49593  return numberOfCachePages(pCache);
49594}
49595#endif
49596
49597/*
49598** Set the suggested cache-size value.
49599*/
49600SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
49601  assert( pCache->pCache!=0 );
49602  pCache->szCache = mxPage;
49603  sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
49604                                         numberOfCachePages(pCache));
49605}
49606
49607/*
49608** Set the suggested cache-spill value.  Make no changes if if the
49609** argument is zero.  Return the effective cache-spill size, which will
49610** be the larger of the szSpill and szCache.
49611*/
49612SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
49613  int res;
49614  assert( p->pCache!=0 );
49615  if( mxPage ){
49616    if( mxPage<0 ){
49617      mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
49618    }
49619    p->szSpill = mxPage;
49620  }
49621  res = numberOfCachePages(p);
49622  if( res<p->szSpill ) res = p->szSpill;
49623  return res;
49624}
49625
49626/*
49627** Free up as much memory as possible from the page cache.
49628*/
49629SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
49630  assert( pCache->pCache!=0 );
49631  sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
49632}
49633
49634/*
49635** Return the size of the header added by this middleware layer
49636** in the page-cache hierarchy.
49637*/
49638SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
49639
49640/*
49641** Return the number of dirty pages currently in the cache, as a percentage
49642** of the configured cache size.
49643*/
49644SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){
49645  PgHdr *pDirty;
49646  int nDirty = 0;
49647  int nCache = numberOfCachePages(pCache);
49648  for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
49649  return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
49650}
49651
49652#ifdef SQLITE_DIRECT_OVERFLOW_READ
49653/*
49654** Return true if there are one or more dirty pages in the cache. Else false.
49655*/
49656SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache){
49657  return (pCache->pDirty!=0);
49658}
49659#endif
49660
49661#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
49662/*
49663** For all dirty pages currently in the cache, invoke the specified
49664** callback. This is only used if the SQLITE_CHECK_PAGES macro is
49665** defined.
49666*/
49667SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
49668  PgHdr *pDirty;
49669  for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
49670    xIter(pDirty);
49671  }
49672}
49673#endif
49674
49675/************** End of pcache.c **********************************************/
49676/************** Begin file pcache1.c *****************************************/
49677/*
49678** 2008 November 05
49679**
49680** The author disclaims copyright to this source code.  In place of
49681** a legal notice, here is a blessing:
49682**
49683**    May you do good and not evil.
49684**    May you find forgiveness for yourself and forgive others.
49685**    May you share freely, never taking more than you give.
49686**
49687*************************************************************************
49688**
49689** This file implements the default page cache implementation (the
49690** sqlite3_pcache interface). It also contains part of the implementation
49691** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
49692** If the default page cache implementation is overridden, then neither of
49693** these two features are available.
49694**
49695** A Page cache line looks like this:
49696**
49697**  -------------------------------------------------------------
49698**  |  database page content   |  PgHdr1  |  MemPage  |  PgHdr  |
49699**  -------------------------------------------------------------
49700**
49701** The database page content is up front (so that buffer overreads tend to
49702** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions).   MemPage
49703** is the extension added by the btree.c module containing information such
49704** as the database page number and how that database page is used.  PgHdr
49705** is added by the pcache.c layer and contains information used to keep track
49706** of which pages are "dirty".  PgHdr1 is an extension added by this
49707** module (pcache1.c).  The PgHdr1 header is a subclass of sqlite3_pcache_page.
49708** PgHdr1 contains information needed to look up a page by its page number.
49709** The superclass sqlite3_pcache_page.pBuf points to the start of the
49710** database page content and sqlite3_pcache_page.pExtra points to PgHdr.
49711**
49712** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at
49713** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size).  The
49714** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
49715** size can vary according to architecture, compile-time options, and
49716** SQLite library version number.
49717**
49718** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained
49719** using a separate memory allocation from the database page content.  This
49720** seeks to overcome the "clownshoe" problem (also called "internal
49721** fragmentation" in academic literature) of allocating a few bytes more
49722** than a power of two with the memory allocator rounding up to the next
49723** power of two, and leaving the rounded-up space unused.
49724**
49725** This module tracks pointers to PgHdr1 objects.  Only pcache.c communicates
49726** with this module.  Information is passed back and forth as PgHdr1 pointers.
49727**
49728** The pcache.c and pager.c modules deal pointers to PgHdr objects.
49729** The btree.c module deals with pointers to MemPage objects.
49730**
49731** SOURCE OF PAGE CACHE MEMORY:
49732**
49733** Memory for a page might come from any of three sources:
49734**
49735**    (1)  The general-purpose memory allocator - sqlite3Malloc()
49736**    (2)  Global page-cache memory provided using sqlite3_config() with
49737**         SQLITE_CONFIG_PAGECACHE.
49738**    (3)  PCache-local bulk allocation.
49739**
49740** The third case is a chunk of heap memory (defaulting to 100 pages worth)
49741** that is allocated when the page cache is created.  The size of the local
49742** bulk allocation can be adjusted using
49743**
49744**     sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
49745**
49746** If N is positive, then N pages worth of memory are allocated using a single
49747** sqlite3Malloc() call and that memory is used for the first N pages allocated.
49748** Or if N is negative, then -1024*N bytes of memory are allocated and used
49749** for as many pages as can be accomodated.
49750**
49751** Only one of (2) or (3) can be used.  Once the memory available to (2) or
49752** (3) is exhausted, subsequent allocations fail over to the general-purpose
49753** memory allocator (1).
49754**
49755** Earlier versions of SQLite used only methods (1) and (2).  But experiments
49756** show that method (3) with N==100 provides about a 5% performance boost for
49757** common workloads.
49758*/
49759/* #include "sqliteInt.h" */
49760
49761typedef struct PCache1 PCache1;
49762typedef struct PgHdr1 PgHdr1;
49763typedef struct PgFreeslot PgFreeslot;
49764typedef struct PGroup PGroup;
49765
49766/*
49767** Each cache entry is represented by an instance of the following
49768** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
49769** PgHdr1.pCache->szPage bytes is allocated directly before this structure
49770** in memory.
49771**
49772** Note: Variables isBulkLocal and isAnchor were once type "u8". That works,
49773** but causes a 2-byte gap in the structure for most architectures (since
49774** pointers must be either 4 or 8-byte aligned). As this structure is located
49775** in memory directly after the associated page data, if the database is
49776** corrupt, code at the b-tree layer may overread the page buffer and
49777** read part of this structure before the corruption is detected. This
49778** can cause a valgrind error if the unitialized gap is accessed. Using u16
49779** ensures there is no such gap, and therefore no bytes of unitialized memory
49780** in the structure.
49781*/
49782struct PgHdr1 {
49783  sqlite3_pcache_page page;      /* Base class. Must be first. pBuf & pExtra */
49784  unsigned int iKey;             /* Key value (page number) */
49785  u16 isBulkLocal;               /* This page from bulk local storage */
49786  u16 isAnchor;                  /* This is the PGroup.lru element */
49787  PgHdr1 *pNext;                 /* Next in hash table chain */
49788  PCache1 *pCache;               /* Cache that currently owns this page */
49789  PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
49790  PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
49791                                 /* NB: pLruPrev is only valid if pLruNext!=0 */
49792};
49793
49794/*
49795** A page is pinned if it is not on the LRU list.  To be "pinned" means
49796** that the page is in active use and must not be deallocated.
49797*/
49798#define PAGE_IS_PINNED(p)    ((p)->pLruNext==0)
49799#define PAGE_IS_UNPINNED(p)  ((p)->pLruNext!=0)
49800
49801/* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set
49802** of one or more PCaches that are able to recycle each other's unpinned
49803** pages when they are under memory pressure.  A PGroup is an instance of
49804** the following object.
49805**
49806** This page cache implementation works in one of two modes:
49807**
49808**   (1)  Every PCache is the sole member of its own PGroup.  There is
49809**        one PGroup per PCache.
49810**
49811**   (2)  There is a single global PGroup that all PCaches are a member
49812**        of.
49813**
49814** Mode 1 uses more memory (since PCache instances are not able to rob
49815** unused pages from other PCaches) but it also operates without a mutex,
49816** and is therefore often faster.  Mode 2 requires a mutex in order to be
49817** threadsafe, but recycles pages more efficiently.
49818**
49819** For mode (1), PGroup.mutex is NULL.  For mode (2) there is only a single
49820** PGroup which is the pcache1.grp global variable and its mutex is
49821** SQLITE_MUTEX_STATIC_LRU.
49822*/
49823struct PGroup {
49824  sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
49825  unsigned int nMaxPage;         /* Sum of nMax for purgeable caches */
49826  unsigned int nMinPage;         /* Sum of nMin for purgeable caches */
49827  unsigned int mxPinned;         /* nMaxpage + 10 - nMinPage */
49828  unsigned int nPurgeable;       /* Number of purgeable pages allocated */
49829  PgHdr1 lru;                    /* The beginning and end of the LRU list */
49830};
49831
49832/* Each page cache is an instance of the following object.  Every
49833** open database file (including each in-memory database and each
49834** temporary or transient database) has a single page cache which
49835** is an instance of this object.
49836**
49837** Pointers to structures of this type are cast and returned as
49838** opaque sqlite3_pcache* handles.
49839*/
49840struct PCache1 {
49841  /* Cache configuration parameters. Page size (szPage) and the purgeable
49842  ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
49843  ** cache is created and are never changed thereafter. nMax may be
49844  ** modified at any time by a call to the pcache1Cachesize() method.
49845  ** The PGroup mutex must be held when accessing nMax.
49846  */
49847  PGroup *pGroup;                     /* PGroup this cache belongs to */
49848  unsigned int *pnPurgeable;          /* Pointer to pGroup->nPurgeable */
49849  int szPage;                         /* Size of database content section */
49850  int szExtra;                        /* sizeof(MemPage)+sizeof(PgHdr) */
49851  int szAlloc;                        /* Total size of one pcache line */
49852  int bPurgeable;                     /* True if cache is purgeable */
49853  unsigned int nMin;                  /* Minimum number of pages reserved */
49854  unsigned int nMax;                  /* Configured "cache_size" value */
49855  unsigned int n90pct;                /* nMax*9/10 */
49856  unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
49857  unsigned int nPurgeableDummy;       /* pnPurgeable points here when not used*/
49858
49859  /* Hash table of all pages. The following variables may only be accessed
49860  ** when the accessor is holding the PGroup mutex.
49861  */
49862  unsigned int nRecyclable;           /* Number of pages in the LRU list */
49863  unsigned int nPage;                 /* Total number of pages in apHash */
49864  unsigned int nHash;                 /* Number of slots in apHash[] */
49865  PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
49866  PgHdr1 *pFree;                      /* List of unused pcache-local pages */
49867  void *pBulk;                        /* Bulk memory used by pcache-local */
49868};
49869
49870/*
49871** Free slots in the allocator used to divide up the global page cache
49872** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
49873*/
49874struct PgFreeslot {
49875  PgFreeslot *pNext;  /* Next free slot */
49876};
49877
49878/*
49879** Global data used by this cache.
49880*/
49881static SQLITE_WSD struct PCacheGlobal {
49882  PGroup grp;                    /* The global PGroup for mode (2) */
49883
49884  /* Variables related to SQLITE_CONFIG_PAGECACHE settings.  The
49885  ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
49886  ** fixed at sqlite3_initialize() time and do not require mutex protection.
49887  ** The nFreeSlot and pFree values do require mutex protection.
49888  */
49889  int isInit;                    /* True if initialized */
49890  int separateCache;             /* Use a new PGroup for each PCache */
49891  int nInitPage;                 /* Initial bulk allocation size */
49892  int szSlot;                    /* Size of each free slot */
49893  int nSlot;                     /* The number of pcache slots */
49894  int nReserve;                  /* Try to keep nFreeSlot above this */
49895  void *pStart, *pEnd;           /* Bounds of global page cache memory */
49896  /* Above requires no mutex.  Use mutex below for variable that follow. */
49897  sqlite3_mutex *mutex;          /* Mutex for accessing the following: */
49898  PgFreeslot *pFree;             /* Free page blocks */
49899  int nFreeSlot;                 /* Number of unused pcache slots */
49900  /* The following value requires a mutex to change.  We skip the mutex on
49901  ** reading because (1) most platforms read a 32-bit integer atomically and
49902  ** (2) even if an incorrect value is read, no great harm is done since this
49903  ** is really just an optimization. */
49904  int bUnderPressure;            /* True if low on PAGECACHE memory */
49905} pcache1_g;
49906
49907/*
49908** All code in this file should access the global structure above via the
49909** alias "pcache1". This ensures that the WSD emulation is used when
49910** compiling for systems that do not support real WSD.
49911*/
49912#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
49913
49914/*
49915** Macros to enter and leave the PCache LRU mutex.
49916*/
49917#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
49918# define pcache1EnterMutex(X)  assert((X)->mutex==0)
49919# define pcache1LeaveMutex(X)  assert((X)->mutex==0)
49920# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
49921#else
49922# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
49923# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
49924# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
49925#endif
49926
49927/******************************************************************************/
49928/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
49929
49930
49931/*
49932** This function is called during initialization if a static buffer is
49933** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
49934** verb to sqlite3_config(). Parameter pBuf points to an allocation large
49935** enough to contain 'n' buffers of 'sz' bytes each.
49936**
49937** This routine is called from sqlite3_initialize() and so it is guaranteed
49938** to be serialized already.  There is no need for further mutexing.
49939*/
49940SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
49941  if( pcache1.isInit ){
49942    PgFreeslot *p;
49943    if( pBuf==0 ) sz = n = 0;
49944    if( n==0 ) sz = 0;
49945    sz = ROUNDDOWN8(sz);
49946    pcache1.szSlot = sz;
49947    pcache1.nSlot = pcache1.nFreeSlot = n;
49948    pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
49949    pcache1.pStart = pBuf;
49950    pcache1.pFree = 0;
49951    pcache1.bUnderPressure = 0;
49952    while( n-- ){
49953      p = (PgFreeslot*)pBuf;
49954      p->pNext = pcache1.pFree;
49955      pcache1.pFree = p;
49956      pBuf = (void*)&((char*)pBuf)[sz];
49957    }
49958    pcache1.pEnd = pBuf;
49959  }
49960}
49961
49962/*
49963** Try to initialize the pCache->pFree and pCache->pBulk fields.  Return
49964** true if pCache->pFree ends up containing one or more free pages.
49965*/
49966static int pcache1InitBulk(PCache1 *pCache){
49967  i64 szBulk;
49968  char *zBulk;
49969  if( pcache1.nInitPage==0 ) return 0;
49970  /* Do not bother with a bulk allocation if the cache size very small */
49971  if( pCache->nMax<3 ) return 0;
49972  sqlite3BeginBenignMalloc();
49973  if( pcache1.nInitPage>0 ){
49974    szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
49975  }else{
49976    szBulk = -1024 * (i64)pcache1.nInitPage;
49977  }
49978  if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
49979    szBulk = pCache->szAlloc*(i64)pCache->nMax;
49980  }
49981  zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
49982  sqlite3EndBenignMalloc();
49983  if( zBulk ){
49984    int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
49985    do{
49986      PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
49987      pX->page.pBuf = zBulk;
49988      pX->page.pExtra = &pX[1];
49989      pX->isBulkLocal = 1;
49990      pX->isAnchor = 0;
49991      pX->pNext = pCache->pFree;
49992      pX->pLruPrev = 0;           /* Initializing this saves a valgrind error */
49993      pCache->pFree = pX;
49994      zBulk += pCache->szAlloc;
49995    }while( --nBulk );
49996  }
49997  return pCache->pFree!=0;
49998}
49999
50000/*
50001** Malloc function used within this file to allocate space from the buffer
50002** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
50003** such buffer exists or there is no space left in it, this function falls
50004** back to sqlite3Malloc().
50005**
50006** Multiple threads can run this routine at the same time.  Global variables
50007** in pcache1 need to be protected via mutex.
50008*/
50009static void *pcache1Alloc(int nByte){
50010  void *p = 0;
50011  assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
50012  if( nByte<=pcache1.szSlot ){
50013    sqlite3_mutex_enter(pcache1.mutex);
50014    p = (PgHdr1 *)pcache1.pFree;
50015    if( p ){
50016      pcache1.pFree = pcache1.pFree->pNext;
50017      pcache1.nFreeSlot--;
50018      pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
50019      assert( pcache1.nFreeSlot>=0 );
50020      sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
50021      sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
50022    }
50023    sqlite3_mutex_leave(pcache1.mutex);
50024  }
50025  if( p==0 ){
50026    /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool.  Get
50027    ** it from sqlite3Malloc instead.
50028    */
50029    p = sqlite3Malloc(nByte);
50030#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
50031    if( p ){
50032      int sz = sqlite3MallocSize(p);
50033      sqlite3_mutex_enter(pcache1.mutex);
50034      sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
50035      sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
50036      sqlite3_mutex_leave(pcache1.mutex);
50037    }
50038#endif
50039    sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
50040  }
50041  return p;
50042}
50043
50044/*
50045** Free an allocated buffer obtained from pcache1Alloc().
50046*/
50047static void pcache1Free(void *p){
50048  if( p==0 ) return;
50049  if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd) ){
50050    PgFreeslot *pSlot;
50051    sqlite3_mutex_enter(pcache1.mutex);
50052    sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1);
50053    pSlot = (PgFreeslot*)p;
50054    pSlot->pNext = pcache1.pFree;
50055    pcache1.pFree = pSlot;
50056    pcache1.nFreeSlot++;
50057    pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
50058    assert( pcache1.nFreeSlot<=pcache1.nSlot );
50059    sqlite3_mutex_leave(pcache1.mutex);
50060  }else{
50061    assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
50062    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
50063#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
50064    {
50065      int nFreed = 0;
50066      nFreed = sqlite3MallocSize(p);
50067      sqlite3_mutex_enter(pcache1.mutex);
50068      sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
50069      sqlite3_mutex_leave(pcache1.mutex);
50070    }
50071#endif
50072    sqlite3_free(p);
50073  }
50074}
50075
50076#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
50077/*
50078** Return the size of a pcache allocation
50079*/
50080static int pcache1MemSize(void *p){
50081  if( p>=pcache1.pStart && p<pcache1.pEnd ){
50082    return pcache1.szSlot;
50083  }else{
50084    int iSize;
50085    assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
50086    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
50087    iSize = sqlite3MallocSize(p);
50088    sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
50089    return iSize;
50090  }
50091}
50092#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
50093
50094/*
50095** Allocate a new page object initially associated with cache pCache.
50096*/
50097static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
50098  PgHdr1 *p = 0;
50099  void *pPg;
50100
50101  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
50102  if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
50103    assert( pCache->pFree!=0 );
50104    p = pCache->pFree;
50105    pCache->pFree = p->pNext;
50106    p->pNext = 0;
50107  }else{
50108#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
50109    /* The group mutex must be released before pcache1Alloc() is called. This
50110    ** is because it might call sqlite3_release_memory(), which assumes that
50111    ** this mutex is not held. */
50112    assert( pcache1.separateCache==0 );
50113    assert( pCache->pGroup==&pcache1.grp );
50114    pcache1LeaveMutex(pCache->pGroup);
50115#endif
50116    if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
50117#ifdef SQLITE_PCACHE_SEPARATE_HEADER
50118    pPg = pcache1Alloc(pCache->szPage);
50119    p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
50120    if( !pPg || !p ){
50121      pcache1Free(pPg);
50122      sqlite3_free(p);
50123      pPg = 0;
50124    }
50125#else
50126    pPg = pcache1Alloc(pCache->szAlloc);
50127#endif
50128    if( benignMalloc ){ sqlite3EndBenignMalloc(); }
50129#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
50130    pcache1EnterMutex(pCache->pGroup);
50131#endif
50132    if( pPg==0 ) return 0;
50133#ifndef SQLITE_PCACHE_SEPARATE_HEADER
50134    p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
50135#endif
50136    p->page.pBuf = pPg;
50137    p->page.pExtra = &p[1];
50138    p->isBulkLocal = 0;
50139    p->isAnchor = 0;
50140  }
50141  (*pCache->pnPurgeable)++;
50142  return p;
50143}
50144
50145/*
50146** Free a page object allocated by pcache1AllocPage().
50147*/
50148static void pcache1FreePage(PgHdr1 *p){
50149  PCache1 *pCache;
50150  assert( p!=0 );
50151  pCache = p->pCache;
50152  assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
50153  if( p->isBulkLocal ){
50154    p->pNext = pCache->pFree;
50155    pCache->pFree = p;
50156  }else{
50157    pcache1Free(p->page.pBuf);
50158#ifdef SQLITE_PCACHE_SEPARATE_HEADER
50159    sqlite3_free(p);
50160#endif
50161  }
50162  (*pCache->pnPurgeable)--;
50163}
50164
50165/*
50166** Malloc function used by SQLite to obtain space from the buffer configured
50167** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
50168** exists, this function falls back to sqlite3Malloc().
50169*/
50170SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
50171  assert( sz<=65536+8 ); /* These allocations are never very large */
50172  return pcache1Alloc(sz);
50173}
50174
50175/*
50176** Free an allocated buffer obtained from sqlite3PageMalloc().
50177*/
50178SQLITE_PRIVATE void sqlite3PageFree(void *p){
50179  pcache1Free(p);
50180}
50181
50182
50183/*
50184** Return true if it desirable to avoid allocating a new page cache
50185** entry.
50186**
50187** If memory was allocated specifically to the page cache using
50188** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
50189** it is desirable to avoid allocating a new page cache entry because
50190** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
50191** for all page cache needs and we should not need to spill the
50192** allocation onto the heap.
50193**
50194** Or, the heap is used for all page cache memory but the heap is
50195** under memory pressure, then again it is desirable to avoid
50196** allocating a new page cache entry in order to avoid stressing
50197** the heap even further.
50198*/
50199static int pcache1UnderMemoryPressure(PCache1 *pCache){
50200  if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
50201    return pcache1.bUnderPressure;
50202  }else{
50203    return sqlite3HeapNearlyFull();
50204  }
50205}
50206
50207/******************************************************************************/
50208/******** General Implementation Functions ************************************/
50209
50210/*
50211** This function is used to resize the hash table used by the cache passed
50212** as the first argument.
50213**
50214** The PCache mutex must be held when this function is called.
50215*/
50216static void pcache1ResizeHash(PCache1 *p){
50217  PgHdr1 **apNew;
50218  unsigned int nNew;
50219  unsigned int i;
50220
50221  assert( sqlite3_mutex_held(p->pGroup->mutex) );
50222
50223  nNew = p->nHash*2;
50224  if( nNew<256 ){
50225    nNew = 256;
50226  }
50227
50228  pcache1LeaveMutex(p->pGroup);
50229  if( p->nHash ){ sqlite3BeginBenignMalloc(); }
50230  apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
50231  if( p->nHash ){ sqlite3EndBenignMalloc(); }
50232  pcache1EnterMutex(p->pGroup);
50233  if( apNew ){
50234    for(i=0; i<p->nHash; i++){
50235      PgHdr1 *pPage;
50236      PgHdr1 *pNext = p->apHash[i];
50237      while( (pPage = pNext)!=0 ){
50238        unsigned int h = pPage->iKey % nNew;
50239        pNext = pPage->pNext;
50240        pPage->pNext = apNew[h];
50241        apNew[h] = pPage;
50242      }
50243    }
50244    sqlite3_free(p->apHash);
50245    p->apHash = apNew;
50246    p->nHash = nNew;
50247  }
50248}
50249
50250/*
50251** This function is used internally to remove the page pPage from the
50252** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
50253** LRU list, then this function is a no-op.
50254**
50255** The PGroup mutex must be held when this function is called.
50256*/
50257static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
50258  assert( pPage!=0 );
50259  assert( PAGE_IS_UNPINNED(pPage) );
50260  assert( pPage->pLruNext );
50261  assert( pPage->pLruPrev );
50262  assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
50263  pPage->pLruPrev->pLruNext = pPage->pLruNext;
50264  pPage->pLruNext->pLruPrev = pPage->pLruPrev;
50265  pPage->pLruNext = 0;
50266  /* pPage->pLruPrev = 0;
50267  ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */
50268  assert( pPage->isAnchor==0 );
50269  assert( pPage->pCache->pGroup->lru.isAnchor==1 );
50270  pPage->pCache->nRecyclable--;
50271  return pPage;
50272}
50273
50274
50275/*
50276** Remove the page supplied as an argument from the hash table
50277** (PCache1.apHash structure) that it is currently stored in.
50278** Also free the page if freePage is true.
50279**
50280** The PGroup mutex must be held when this function is called.
50281*/
50282static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
50283  unsigned int h;
50284  PCache1 *pCache = pPage->pCache;
50285  PgHdr1 **pp;
50286
50287  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
50288  h = pPage->iKey % pCache->nHash;
50289  for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
50290  *pp = (*pp)->pNext;
50291
50292  pCache->nPage--;
50293  if( freeFlag ) pcache1FreePage(pPage);
50294}
50295
50296/*
50297** If there are currently more than nMaxPage pages allocated, try
50298** to recycle pages to reduce the number allocated to nMaxPage.
50299*/
50300static void pcache1EnforceMaxPage(PCache1 *pCache){
50301  PGroup *pGroup = pCache->pGroup;
50302  PgHdr1 *p;
50303  assert( sqlite3_mutex_held(pGroup->mutex) );
50304  while( pGroup->nPurgeable>pGroup->nMaxPage
50305      && (p=pGroup->lru.pLruPrev)->isAnchor==0
50306  ){
50307    assert( p->pCache->pGroup==pGroup );
50308    assert( PAGE_IS_UNPINNED(p) );
50309    pcache1PinPage(p);
50310    pcache1RemoveFromHash(p, 1);
50311  }
50312  if( pCache->nPage==0 && pCache->pBulk ){
50313    sqlite3_free(pCache->pBulk);
50314    pCache->pBulk = pCache->pFree = 0;
50315  }
50316}
50317
50318/*
50319** Discard all pages from cache pCache with a page number (key value)
50320** greater than or equal to iLimit. Any pinned pages that meet this
50321** criteria are unpinned before they are discarded.
50322**
50323** The PCache mutex must be held when this function is called.
50324*/
50325static void pcache1TruncateUnsafe(
50326  PCache1 *pCache,             /* The cache to truncate */
50327  unsigned int iLimit          /* Drop pages with this pgno or larger */
50328){
50329  TESTONLY( int nPage = 0; )  /* To assert pCache->nPage is correct */
50330  unsigned int h, iStop;
50331  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
50332  assert( pCache->iMaxKey >= iLimit );
50333  assert( pCache->nHash > 0 );
50334  if( pCache->iMaxKey - iLimit < pCache->nHash ){
50335    /* If we are just shaving the last few pages off the end of the
50336    ** cache, then there is no point in scanning the entire hash table.
50337    ** Only scan those hash slots that might contain pages that need to
50338    ** be removed. */
50339    h = iLimit % pCache->nHash;
50340    iStop = pCache->iMaxKey % pCache->nHash;
50341    TESTONLY( nPage = -10; )  /* Disable the pCache->nPage validity check */
50342  }else{
50343    /* This is the general case where many pages are being removed.
50344    ** It is necessary to scan the entire hash table */
50345    h = pCache->nHash/2;
50346    iStop = h - 1;
50347  }
50348  for(;;){
50349    PgHdr1 **pp;
50350    PgHdr1 *pPage;
50351    assert( h<pCache->nHash );
50352    pp = &pCache->apHash[h];
50353    while( (pPage = *pp)!=0 ){
50354      if( pPage->iKey>=iLimit ){
50355        pCache->nPage--;
50356        *pp = pPage->pNext;
50357        if( PAGE_IS_UNPINNED(pPage) ) pcache1PinPage(pPage);
50358        pcache1FreePage(pPage);
50359      }else{
50360        pp = &pPage->pNext;
50361        TESTONLY( if( nPage>=0 ) nPage++; )
50362      }
50363    }
50364    if( h==iStop ) break;
50365    h = (h+1) % pCache->nHash;
50366  }
50367  assert( nPage<0 || pCache->nPage==(unsigned)nPage );
50368}
50369
50370/******************************************************************************/
50371/******** sqlite3_pcache Methods **********************************************/
50372
50373/*
50374** Implementation of the sqlite3_pcache.xInit method.
50375*/
50376static int pcache1Init(void *NotUsed){
50377  UNUSED_PARAMETER(NotUsed);
50378  assert( pcache1.isInit==0 );
50379  memset(&pcache1, 0, sizeof(pcache1));
50380
50381
50382  /*
50383  ** The pcache1.separateCache variable is true if each PCache has its own
50384  ** private PGroup (mode-1).  pcache1.separateCache is false if the single
50385  ** PGroup in pcache1.grp is used for all page caches (mode-2).
50386  **
50387  **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
50388  **
50389  **   *  Use a unified cache in single-threaded applications that have
50390  **      configured a start-time buffer for use as page-cache memory using
50391  **      sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
50392  **      pBuf argument.
50393  **
50394  **   *  Otherwise use separate caches (mode-1)
50395  */
50396#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
50397  pcache1.separateCache = 0;
50398#elif SQLITE_THREADSAFE
50399  pcache1.separateCache = sqlite3GlobalConfig.pPage==0
50400                          || sqlite3GlobalConfig.bCoreMutex>0;
50401#else
50402  pcache1.separateCache = sqlite3GlobalConfig.pPage==0;
50403#endif
50404
50405#if SQLITE_THREADSAFE
50406  if( sqlite3GlobalConfig.bCoreMutex ){
50407    pcache1.grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU);
50408    pcache1.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM);
50409  }
50410#endif
50411  if( pcache1.separateCache
50412   && sqlite3GlobalConfig.nPage!=0
50413   && sqlite3GlobalConfig.pPage==0
50414  ){
50415    pcache1.nInitPage = sqlite3GlobalConfig.nPage;
50416  }else{
50417    pcache1.nInitPage = 0;
50418  }
50419  pcache1.grp.mxPinned = 10;
50420  pcache1.isInit = 1;
50421  return SQLITE_OK;
50422}
50423
50424/*
50425** Implementation of the sqlite3_pcache.xShutdown method.
50426** Note that the static mutex allocated in xInit does
50427** not need to be freed.
50428*/
50429static void pcache1Shutdown(void *NotUsed){
50430  UNUSED_PARAMETER(NotUsed);
50431  assert( pcache1.isInit!=0 );
50432  memset(&pcache1, 0, sizeof(pcache1));
50433}
50434
50435/* forward declaration */
50436static void pcache1Destroy(sqlite3_pcache *p);
50437
50438/*
50439** Implementation of the sqlite3_pcache.xCreate method.
50440**
50441** Allocate a new cache.
50442*/
50443static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
50444  PCache1 *pCache;      /* The newly created page cache */
50445  PGroup *pGroup;       /* The group the new page cache will belong to */
50446  int sz;               /* Bytes of memory required to allocate the new cache */
50447
50448  assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
50449  assert( szExtra < 300 );
50450
50451  sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache;
50452  pCache = (PCache1 *)sqlite3MallocZero(sz);
50453  if( pCache ){
50454    if( pcache1.separateCache ){
50455      pGroup = (PGroup*)&pCache[1];
50456      pGroup->mxPinned = 10;
50457    }else{
50458      pGroup = &pcache1.grp;
50459    }
50460    pcache1EnterMutex(pGroup);
50461    if( pGroup->lru.isAnchor==0 ){
50462      pGroup->lru.isAnchor = 1;
50463      pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
50464    }
50465    pCache->pGroup = pGroup;
50466    pCache->szPage = szPage;
50467    pCache->szExtra = szExtra;
50468    pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
50469    pCache->bPurgeable = (bPurgeable ? 1 : 0);
50470    pcache1ResizeHash(pCache);
50471    if( bPurgeable ){
50472      pCache->nMin = 10;
50473      pGroup->nMinPage += pCache->nMin;
50474      pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
50475      pCache->pnPurgeable = &pGroup->nPurgeable;
50476    }else{
50477      pCache->pnPurgeable = &pCache->nPurgeableDummy;
50478    }
50479    pcache1LeaveMutex(pGroup);
50480    if( pCache->nHash==0 ){
50481      pcache1Destroy((sqlite3_pcache*)pCache);
50482      pCache = 0;
50483    }
50484  }
50485  return (sqlite3_pcache *)pCache;
50486}
50487
50488/*
50489** Implementation of the sqlite3_pcache.xCachesize method.
50490**
50491** Configure the cache_size limit for a cache.
50492*/
50493static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
50494  PCache1 *pCache = (PCache1 *)p;
50495  if( pCache->bPurgeable ){
50496    PGroup *pGroup = pCache->pGroup;
50497    pcache1EnterMutex(pGroup);
50498    pGroup->nMaxPage += (nMax - pCache->nMax);
50499    pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
50500    pCache->nMax = nMax;
50501    pCache->n90pct = pCache->nMax*9/10;
50502    pcache1EnforceMaxPage(pCache);
50503    pcache1LeaveMutex(pGroup);
50504  }
50505}
50506
50507/*
50508** Implementation of the sqlite3_pcache.xShrink method.
50509**
50510** Free up as much memory as possible.
50511*/
50512static void pcache1Shrink(sqlite3_pcache *p){
50513  PCache1 *pCache = (PCache1*)p;
50514  if( pCache->bPurgeable ){
50515    PGroup *pGroup = pCache->pGroup;
50516    int savedMaxPage;
50517    pcache1EnterMutex(pGroup);
50518    savedMaxPage = pGroup->nMaxPage;
50519    pGroup->nMaxPage = 0;
50520    pcache1EnforceMaxPage(pCache);
50521    pGroup->nMaxPage = savedMaxPage;
50522    pcache1LeaveMutex(pGroup);
50523  }
50524}
50525
50526/*
50527** Implementation of the sqlite3_pcache.xPagecount method.
50528*/
50529static int pcache1Pagecount(sqlite3_pcache *p){
50530  int n;
50531  PCache1 *pCache = (PCache1*)p;
50532  pcache1EnterMutex(pCache->pGroup);
50533  n = pCache->nPage;
50534  pcache1LeaveMutex(pCache->pGroup);
50535  return n;
50536}
50537
50538
50539/*
50540** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
50541** in the header of the pcache1Fetch() procedure.
50542**
50543** This steps are broken out into a separate procedure because they are
50544** usually not needed, and by avoiding the stack initialization required
50545** for these steps, the main pcache1Fetch() procedure can run faster.
50546*/
50547static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
50548  PCache1 *pCache,
50549  unsigned int iKey,
50550  int createFlag
50551){
50552  unsigned int nPinned;
50553  PGroup *pGroup = pCache->pGroup;
50554  PgHdr1 *pPage = 0;
50555
50556  /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
50557  assert( pCache->nPage >= pCache->nRecyclable );
50558  nPinned = pCache->nPage - pCache->nRecyclable;
50559  assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
50560  assert( pCache->n90pct == pCache->nMax*9/10 );
50561  if( createFlag==1 && (
50562        nPinned>=pGroup->mxPinned
50563     || nPinned>=pCache->n90pct
50564     || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
50565  )){
50566    return 0;
50567  }
50568
50569  if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
50570  assert( pCache->nHash>0 && pCache->apHash );
50571
50572  /* Step 4. Try to recycle a page. */
50573  if( pCache->bPurgeable
50574   && !pGroup->lru.pLruPrev->isAnchor
50575   && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
50576  ){
50577    PCache1 *pOther;
50578    pPage = pGroup->lru.pLruPrev;
50579    assert( PAGE_IS_UNPINNED(pPage) );
50580    pcache1RemoveFromHash(pPage, 0);
50581    pcache1PinPage(pPage);
50582    pOther = pPage->pCache;
50583    if( pOther->szAlloc != pCache->szAlloc ){
50584      pcache1FreePage(pPage);
50585      pPage = 0;
50586    }else{
50587      pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
50588    }
50589  }
50590
50591  /* Step 5. If a usable page buffer has still not been found,
50592  ** attempt to allocate a new one.
50593  */
50594  if( !pPage ){
50595    pPage = pcache1AllocPage(pCache, createFlag==1);
50596  }
50597
50598  if( pPage ){
50599    unsigned int h = iKey % pCache->nHash;
50600    pCache->nPage++;
50601    pPage->iKey = iKey;
50602    pPage->pNext = pCache->apHash[h];
50603    pPage->pCache = pCache;
50604    pPage->pLruNext = 0;
50605    /* pPage->pLruPrev = 0;
50606    ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */
50607    *(void **)pPage->page.pExtra = 0;
50608    pCache->apHash[h] = pPage;
50609    if( iKey>pCache->iMaxKey ){
50610      pCache->iMaxKey = iKey;
50611    }
50612  }
50613  return pPage;
50614}
50615
50616/*
50617** Implementation of the sqlite3_pcache.xFetch method.
50618**
50619** Fetch a page by key value.
50620**
50621** Whether or not a new page may be allocated by this function depends on
50622** the value of the createFlag argument.  0 means do not allocate a new
50623** page.  1 means allocate a new page if space is easily available.  2
50624** means to try really hard to allocate a new page.
50625**
50626** For a non-purgeable cache (a cache used as the storage for an in-memory
50627** database) there is really no difference between createFlag 1 and 2.  So
50628** the calling function (pcache.c) will never have a createFlag of 1 on
50629** a non-purgeable cache.
50630**
50631** There are three different approaches to obtaining space for a page,
50632** depending on the value of parameter createFlag (which may be 0, 1 or 2).
50633**
50634**   1. Regardless of the value of createFlag, the cache is searched for a
50635**      copy of the requested page. If one is found, it is returned.
50636**
50637**   2. If createFlag==0 and the page is not already in the cache, NULL is
50638**      returned.
50639**
50640**   3. If createFlag is 1, and the page is not already in the cache, then
50641**      return NULL (do not allocate a new page) if any of the following
50642**      conditions are true:
50643**
50644**       (a) the number of pages pinned by the cache is greater than
50645**           PCache1.nMax, or
50646**
50647**       (b) the number of pages pinned by the cache is greater than
50648**           the sum of nMax for all purgeable caches, less the sum of
50649**           nMin for all other purgeable caches, or
50650**
50651**   4. If none of the first three conditions apply and the cache is marked
50652**      as purgeable, and if one of the following is true:
50653**
50654**       (a) The number of pages allocated for the cache is already
50655**           PCache1.nMax, or
50656**
50657**       (b) The number of pages allocated for all purgeable caches is
50658**           already equal to or greater than the sum of nMax for all
50659**           purgeable caches,
50660**
50661**       (c) The system is under memory pressure and wants to avoid
50662**           unnecessary pages cache entry allocations
50663**
50664**      then attempt to recycle a page from the LRU list. If it is the right
50665**      size, return the recycled buffer. Otherwise, free the buffer and
50666**      proceed to step 5.
50667**
50668**   5. Otherwise, allocate and return a new page buffer.
50669**
50670** There are two versions of this routine.  pcache1FetchWithMutex() is
50671** the general case.  pcache1FetchNoMutex() is a faster implementation for
50672** the common case where pGroup->mutex is NULL.  The pcache1Fetch() wrapper
50673** invokes the appropriate routine.
50674*/
50675static PgHdr1 *pcache1FetchNoMutex(
50676  sqlite3_pcache *p,
50677  unsigned int iKey,
50678  int createFlag
50679){
50680  PCache1 *pCache = (PCache1 *)p;
50681  PgHdr1 *pPage = 0;
50682
50683  /* Step 1: Search the hash table for an existing entry. */
50684  pPage = pCache->apHash[iKey % pCache->nHash];
50685  while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
50686
50687  /* Step 2: If the page was found in the hash table, then return it.
50688  ** If the page was not in the hash table and createFlag is 0, abort.
50689  ** Otherwise (page not in hash and createFlag!=0) continue with
50690  ** subsequent steps to try to create the page. */
50691  if( pPage ){
50692    if( PAGE_IS_UNPINNED(pPage) ){
50693      return pcache1PinPage(pPage);
50694    }else{
50695      return pPage;
50696    }
50697  }else if( createFlag ){
50698    /* Steps 3, 4, and 5 implemented by this subroutine */
50699    return pcache1FetchStage2(pCache, iKey, createFlag);
50700  }else{
50701    return 0;
50702  }
50703}
50704#if PCACHE1_MIGHT_USE_GROUP_MUTEX
50705static PgHdr1 *pcache1FetchWithMutex(
50706  sqlite3_pcache *p,
50707  unsigned int iKey,
50708  int createFlag
50709){
50710  PCache1 *pCache = (PCache1 *)p;
50711  PgHdr1 *pPage;
50712
50713  pcache1EnterMutex(pCache->pGroup);
50714  pPage = pcache1FetchNoMutex(p, iKey, createFlag);
50715  assert( pPage==0 || pCache->iMaxKey>=iKey );
50716  pcache1LeaveMutex(pCache->pGroup);
50717  return pPage;
50718}
50719#endif
50720static sqlite3_pcache_page *pcache1Fetch(
50721  sqlite3_pcache *p,
50722  unsigned int iKey,
50723  int createFlag
50724){
50725#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
50726  PCache1 *pCache = (PCache1 *)p;
50727#endif
50728
50729  assert( offsetof(PgHdr1,page)==0 );
50730  assert( pCache->bPurgeable || createFlag!=1 );
50731  assert( pCache->bPurgeable || pCache->nMin==0 );
50732  assert( pCache->bPurgeable==0 || pCache->nMin==10 );
50733  assert( pCache->nMin==0 || pCache->bPurgeable );
50734  assert( pCache->nHash>0 );
50735#if PCACHE1_MIGHT_USE_GROUP_MUTEX
50736  if( pCache->pGroup->mutex ){
50737    return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
50738  }else
50739#endif
50740  {
50741    return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
50742  }
50743}
50744
50745
50746/*
50747** Implementation of the sqlite3_pcache.xUnpin method.
50748**
50749** Mark a page as unpinned (eligible for asynchronous recycling).
50750*/
50751static void pcache1Unpin(
50752  sqlite3_pcache *p,
50753  sqlite3_pcache_page *pPg,
50754  int reuseUnlikely
50755){
50756  PCache1 *pCache = (PCache1 *)p;
50757  PgHdr1 *pPage = (PgHdr1 *)pPg;
50758  PGroup *pGroup = pCache->pGroup;
50759
50760  assert( pPage->pCache==pCache );
50761  pcache1EnterMutex(pGroup);
50762
50763  /* It is an error to call this function if the page is already
50764  ** part of the PGroup LRU list.
50765  */
50766  assert( pPage->pLruNext==0 );
50767  assert( PAGE_IS_PINNED(pPage) );
50768
50769  if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
50770    pcache1RemoveFromHash(pPage, 1);
50771  }else{
50772    /* Add the page to the PGroup LRU list. */
50773    PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
50774    pPage->pLruPrev = &pGroup->lru;
50775    (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
50776    *ppFirst = pPage;
50777    pCache->nRecyclable++;
50778  }
50779
50780  pcache1LeaveMutex(pCache->pGroup);
50781}
50782
50783/*
50784** Implementation of the sqlite3_pcache.xRekey method.
50785*/
50786static void pcache1Rekey(
50787  sqlite3_pcache *p,
50788  sqlite3_pcache_page *pPg,
50789  unsigned int iOld,
50790  unsigned int iNew
50791){
50792  PCache1 *pCache = (PCache1 *)p;
50793  PgHdr1 *pPage = (PgHdr1 *)pPg;
50794  PgHdr1 **pp;
50795  unsigned int h;
50796  assert( pPage->iKey==iOld );
50797  assert( pPage->pCache==pCache );
50798
50799  pcache1EnterMutex(pCache->pGroup);
50800
50801  h = iOld%pCache->nHash;
50802  pp = &pCache->apHash[h];
50803  while( (*pp)!=pPage ){
50804    pp = &(*pp)->pNext;
50805  }
50806  *pp = pPage->pNext;
50807
50808  h = iNew%pCache->nHash;
50809  pPage->iKey = iNew;
50810  pPage->pNext = pCache->apHash[h];
50811  pCache->apHash[h] = pPage;
50812  if( iNew>pCache->iMaxKey ){
50813    pCache->iMaxKey = iNew;
50814  }
50815
50816  pcache1LeaveMutex(pCache->pGroup);
50817}
50818
50819/*
50820** Implementation of the sqlite3_pcache.xTruncate method.
50821**
50822** Discard all unpinned pages in the cache with a page number equal to
50823** or greater than parameter iLimit. Any pinned pages with a page number
50824** equal to or greater than iLimit are implicitly unpinned.
50825*/
50826static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
50827  PCache1 *pCache = (PCache1 *)p;
50828  pcache1EnterMutex(pCache->pGroup);
50829  if( iLimit<=pCache->iMaxKey ){
50830    pcache1TruncateUnsafe(pCache, iLimit);
50831    pCache->iMaxKey = iLimit-1;
50832  }
50833  pcache1LeaveMutex(pCache->pGroup);
50834}
50835
50836/*
50837** Implementation of the sqlite3_pcache.xDestroy method.
50838**
50839** Destroy a cache allocated using pcache1Create().
50840*/
50841static void pcache1Destroy(sqlite3_pcache *p){
50842  PCache1 *pCache = (PCache1 *)p;
50843  PGroup *pGroup = pCache->pGroup;
50844  assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
50845  pcache1EnterMutex(pGroup);
50846  if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
50847  assert( pGroup->nMaxPage >= pCache->nMax );
50848  pGroup->nMaxPage -= pCache->nMax;
50849  assert( pGroup->nMinPage >= pCache->nMin );
50850  pGroup->nMinPage -= pCache->nMin;
50851  pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
50852  pcache1EnforceMaxPage(pCache);
50853  pcache1LeaveMutex(pGroup);
50854  sqlite3_free(pCache->pBulk);
50855  sqlite3_free(pCache->apHash);
50856  sqlite3_free(pCache);
50857}
50858
50859/*
50860** This function is called during initialization (sqlite3_initialize()) to
50861** install the default pluggable cache module, assuming the user has not
50862** already provided an alternative.
50863*/
50864SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
50865  static const sqlite3_pcache_methods2 defaultMethods = {
50866    1,                       /* iVersion */
50867    0,                       /* pArg */
50868    pcache1Init,             /* xInit */
50869    pcache1Shutdown,         /* xShutdown */
50870    pcache1Create,           /* xCreate */
50871    pcache1Cachesize,        /* xCachesize */
50872    pcache1Pagecount,        /* xPagecount */
50873    pcache1Fetch,            /* xFetch */
50874    pcache1Unpin,            /* xUnpin */
50875    pcache1Rekey,            /* xRekey */
50876    pcache1Truncate,         /* xTruncate */
50877    pcache1Destroy,          /* xDestroy */
50878    pcache1Shrink            /* xShrink */
50879  };
50880  sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
50881}
50882
50883/*
50884** Return the size of the header on each page of this PCACHE implementation.
50885*/
50886SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
50887
50888/*
50889** Return the global mutex used by this PCACHE implementation.  The
50890** sqlite3_status() routine needs access to this mutex.
50891*/
50892SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void){
50893  return pcache1.mutex;
50894}
50895
50896#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
50897/*
50898** This function is called to free superfluous dynamically allocated memory
50899** held by the pager system. Memory in use by any SQLite pager allocated
50900** by the current thread may be sqlite3_free()ed.
50901**
50902** nReq is the number of bytes of memory required. Once this much has
50903** been released, the function returns. The return value is the total number
50904** of bytes of memory released.
50905*/
50906SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
50907  int nFree = 0;
50908  assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
50909  assert( sqlite3_mutex_notheld(pcache1.mutex) );
50910  if( sqlite3GlobalConfig.pPage==0 ){
50911    PgHdr1 *p;
50912    pcache1EnterMutex(&pcache1.grp);
50913    while( (nReq<0 || nFree<nReq)
50914       &&  (p=pcache1.grp.lru.pLruPrev)!=0
50915       &&  p->isAnchor==0
50916    ){
50917      nFree += pcache1MemSize(p->page.pBuf);
50918#ifdef SQLITE_PCACHE_SEPARATE_HEADER
50919      nFree += sqlite3MemSize(p);
50920#endif
50921      assert( PAGE_IS_UNPINNED(p) );
50922      pcache1PinPage(p);
50923      pcache1RemoveFromHash(p, 1);
50924    }
50925    pcache1LeaveMutex(&pcache1.grp);
50926  }
50927  return nFree;
50928}
50929#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
50930
50931#ifdef SQLITE_TEST
50932/*
50933** This function is used by test procedures to inspect the internal state
50934** of the global cache.
50935*/
50936SQLITE_PRIVATE void sqlite3PcacheStats(
50937  int *pnCurrent,      /* OUT: Total number of pages cached */
50938  int *pnMax,          /* OUT: Global maximum cache size */
50939  int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
50940  int *pnRecyclable    /* OUT: Total number of pages available for recycling */
50941){
50942  PgHdr1 *p;
50943  int nRecyclable = 0;
50944  for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
50945    assert( PAGE_IS_UNPINNED(p) );
50946    nRecyclable++;
50947  }
50948  *pnCurrent = pcache1.grp.nPurgeable;
50949  *pnMax = (int)pcache1.grp.nMaxPage;
50950  *pnMin = (int)pcache1.grp.nMinPage;
50951  *pnRecyclable = nRecyclable;
50952}
50953#endif
50954
50955/************** End of pcache1.c *********************************************/
50956/************** Begin file rowset.c ******************************************/
50957/*
50958** 2008 December 3
50959**
50960** The author disclaims copyright to this source code.  In place of
50961** a legal notice, here is a blessing:
50962**
50963**    May you do good and not evil.
50964**    May you find forgiveness for yourself and forgive others.
50965**    May you share freely, never taking more than you give.
50966**
50967*************************************************************************
50968**
50969** This module implements an object we call a "RowSet".
50970**
50971** The RowSet object is a collection of rowids.  Rowids
50972** are inserted into the RowSet in an arbitrary order.  Inserts
50973** can be intermixed with tests to see if a given rowid has been
50974** previously inserted into the RowSet.
50975**
50976** After all inserts are finished, it is possible to extract the
50977** elements of the RowSet in sorted order.  Once this extraction
50978** process has started, no new elements may be inserted.
50979**
50980** Hence, the primitive operations for a RowSet are:
50981**
50982**    CREATE
50983**    INSERT
50984**    TEST
50985**    SMALLEST
50986**    DESTROY
50987**
50988** The CREATE and DESTROY primitives are the constructor and destructor,
50989** obviously.  The INSERT primitive adds a new element to the RowSet.
50990** TEST checks to see if an element is already in the RowSet.  SMALLEST
50991** extracts the least value from the RowSet.
50992**
50993** The INSERT primitive might allocate additional memory.  Memory is
50994** allocated in chunks so most INSERTs do no allocation.  There is an
50995** upper bound on the size of allocated memory.  No memory is freed
50996** until DESTROY.
50997**
50998** The TEST primitive includes a "batch" number.  The TEST primitive
50999** will only see elements that were inserted before the last change
51000** in the batch number.  In other words, if an INSERT occurs between
51001** two TESTs where the TESTs have the same batch nubmer, then the
51002** value added by the INSERT will not be visible to the second TEST.
51003** The initial batch number is zero, so if the very first TEST contains
51004** a non-zero batch number, it will see all prior INSERTs.
51005**
51006** No INSERTs may occurs after a SMALLEST.  An assertion will fail if
51007** that is attempted.
51008**
51009** The cost of an INSERT is roughly constant.  (Sometimes new memory
51010** has to be allocated on an INSERT.)  The cost of a TEST with a new
51011** batch number is O(NlogN) where N is the number of elements in the RowSet.
51012** The cost of a TEST using the same batch number is O(logN).  The cost
51013** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
51014** primitives are constant time.  The cost of DESTROY is O(N).
51015**
51016** TEST and SMALLEST may not be used by the same RowSet.  This used to
51017** be possible, but the feature was not used, so it was removed in order
51018** to simplify the code.
51019*/
51020/* #include "sqliteInt.h" */
51021
51022
51023/*
51024** Target size for allocation chunks.
51025*/
51026#define ROWSET_ALLOCATION_SIZE 1024
51027
51028/*
51029** The number of rowset entries per allocation chunk.
51030*/
51031#define ROWSET_ENTRY_PER_CHUNK  \
51032                       ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
51033
51034/*
51035** Each entry in a RowSet is an instance of the following object.
51036**
51037** This same object is reused to store a linked list of trees of RowSetEntry
51038** objects.  In that alternative use, pRight points to the next entry
51039** in the list, pLeft points to the tree, and v is unused.  The
51040** RowSet.pForest value points to the head of this forest list.
51041*/
51042struct RowSetEntry {
51043  i64 v;                        /* ROWID value for this entry */
51044  struct RowSetEntry *pRight;   /* Right subtree (larger entries) or list */
51045  struct RowSetEntry *pLeft;    /* Left subtree (smaller entries) */
51046};
51047
51048/*
51049** RowSetEntry objects are allocated in large chunks (instances of the
51050** following structure) to reduce memory allocation overhead.  The
51051** chunks are kept on a linked list so that they can be deallocated
51052** when the RowSet is destroyed.
51053*/
51054struct RowSetChunk {
51055  struct RowSetChunk *pNextChunk;        /* Next chunk on list of them all */
51056  struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
51057};
51058
51059/*
51060** A RowSet in an instance of the following structure.
51061**
51062** A typedef of this structure if found in sqliteInt.h.
51063*/
51064struct RowSet {
51065  struct RowSetChunk *pChunk;    /* List of all chunk allocations */
51066  sqlite3 *db;                   /* The database connection */
51067  struct RowSetEntry *pEntry;    /* List of entries using pRight */
51068  struct RowSetEntry *pLast;     /* Last entry on the pEntry list */
51069  struct RowSetEntry *pFresh;    /* Source of new entry objects */
51070  struct RowSetEntry *pForest;   /* List of binary trees of entries */
51071  u16 nFresh;                    /* Number of objects on pFresh */
51072  u16 rsFlags;                   /* Various flags */
51073  int iBatch;                    /* Current insert batch */
51074};
51075
51076/*
51077** Allowed values for RowSet.rsFlags
51078*/
51079#define ROWSET_SORTED  0x01   /* True if RowSet.pEntry is sorted */
51080#define ROWSET_NEXT    0x02   /* True if sqlite3RowSetNext() has been called */
51081
51082/*
51083** Allocate a RowSet object.  Return NULL if a memory allocation
51084** error occurs.
51085*/
51086SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db){
51087  RowSet *p = sqlite3DbMallocRawNN(db, sizeof(*p));
51088  if( p ){
51089    int N = sqlite3DbMallocSize(db, p);
51090    p->pChunk = 0;
51091    p->db = db;
51092    p->pEntry = 0;
51093    p->pLast = 0;
51094    p->pForest = 0;
51095    p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
51096    p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
51097    p->rsFlags = ROWSET_SORTED;
51098    p->iBatch = 0;
51099  }
51100  return p;
51101}
51102
51103/*
51104** Deallocate all chunks from a RowSet.  This frees all memory that
51105** the RowSet has allocated over its lifetime.  This routine is
51106** the destructor for the RowSet.
51107*/
51108SQLITE_PRIVATE void sqlite3RowSetClear(void *pArg){
51109  RowSet *p = (RowSet*)pArg;
51110  struct RowSetChunk *pChunk, *pNextChunk;
51111  for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
51112    pNextChunk = pChunk->pNextChunk;
51113    sqlite3DbFree(p->db, pChunk);
51114  }
51115  p->pChunk = 0;
51116  p->nFresh = 0;
51117  p->pEntry = 0;
51118  p->pLast = 0;
51119  p->pForest = 0;
51120  p->rsFlags = ROWSET_SORTED;
51121}
51122
51123/*
51124** Deallocate all chunks from a RowSet.  This frees all memory that
51125** the RowSet has allocated over its lifetime.  This routine is
51126** the destructor for the RowSet.
51127*/
51128SQLITE_PRIVATE void sqlite3RowSetDelete(void *pArg){
51129  sqlite3RowSetClear(pArg);
51130  sqlite3DbFree(((RowSet*)pArg)->db, pArg);
51131}
51132
51133/*
51134** Allocate a new RowSetEntry object that is associated with the
51135** given RowSet.  Return a pointer to the new and completely uninitialized
51136** object.
51137**
51138** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
51139** routine returns NULL.
51140*/
51141static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
51142  assert( p!=0 );
51143  if( p->nFresh==0 ){  /*OPTIMIZATION-IF-FALSE*/
51144    /* We could allocate a fresh RowSetEntry each time one is needed, but it
51145    ** is more efficient to pull a preallocated entry from the pool */
51146    struct RowSetChunk *pNew;
51147    pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
51148    if( pNew==0 ){
51149      return 0;
51150    }
51151    pNew->pNextChunk = p->pChunk;
51152    p->pChunk = pNew;
51153    p->pFresh = pNew->aEntry;
51154    p->nFresh = ROWSET_ENTRY_PER_CHUNK;
51155  }
51156  p->nFresh--;
51157  return p->pFresh++;
51158}
51159
51160/*
51161** Insert a new value into a RowSet.
51162**
51163** The mallocFailed flag of the database connection is set if a
51164** memory allocation fails.
51165*/
51166SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
51167  struct RowSetEntry *pEntry;  /* The new entry */
51168  struct RowSetEntry *pLast;   /* The last prior entry */
51169
51170  /* This routine is never called after sqlite3RowSetNext() */
51171  assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
51172
51173  pEntry = rowSetEntryAlloc(p);
51174  if( pEntry==0 ) return;
51175  pEntry->v = rowid;
51176  pEntry->pRight = 0;
51177  pLast = p->pLast;
51178  if( pLast ){
51179    if( rowid<=pLast->v ){  /*OPTIMIZATION-IF-FALSE*/
51180      /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags
51181      ** where possible */
51182      p->rsFlags &= ~ROWSET_SORTED;
51183    }
51184    pLast->pRight = pEntry;
51185  }else{
51186    p->pEntry = pEntry;
51187  }
51188  p->pLast = pEntry;
51189}
51190
51191/*
51192** Merge two lists of RowSetEntry objects.  Remove duplicates.
51193**
51194** The input lists are connected via pRight pointers and are
51195** assumed to each already be in sorted order.
51196*/
51197static struct RowSetEntry *rowSetEntryMerge(
51198  struct RowSetEntry *pA,    /* First sorted list to be merged */
51199  struct RowSetEntry *pB     /* Second sorted list to be merged */
51200){
51201  struct RowSetEntry head;
51202  struct RowSetEntry *pTail;
51203
51204  pTail = &head;
51205  assert( pA!=0 && pB!=0 );
51206  for(;;){
51207    assert( pA->pRight==0 || pA->v<=pA->pRight->v );
51208    assert( pB->pRight==0 || pB->v<=pB->pRight->v );
51209    if( pA->v<=pB->v ){
51210      if( pA->v<pB->v ) pTail = pTail->pRight = pA;
51211      pA = pA->pRight;
51212      if( pA==0 ){
51213        pTail->pRight = pB;
51214        break;
51215      }
51216    }else{
51217      pTail = pTail->pRight = pB;
51218      pB = pB->pRight;
51219      if( pB==0 ){
51220        pTail->pRight = pA;
51221        break;
51222      }
51223    }
51224  }
51225  return head.pRight;
51226}
51227
51228/*
51229** Sort all elements on the list of RowSetEntry objects into order of
51230** increasing v.
51231*/
51232static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
51233  unsigned int i;
51234  struct RowSetEntry *pNext, *aBucket[40];
51235
51236  memset(aBucket, 0, sizeof(aBucket));
51237  while( pIn ){
51238    pNext = pIn->pRight;
51239    pIn->pRight = 0;
51240    for(i=0; aBucket[i]; i++){
51241      pIn = rowSetEntryMerge(aBucket[i], pIn);
51242      aBucket[i] = 0;
51243    }
51244    aBucket[i] = pIn;
51245    pIn = pNext;
51246  }
51247  pIn = aBucket[0];
51248  for(i=1; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
51249    if( aBucket[i]==0 ) continue;
51250    pIn = pIn ? rowSetEntryMerge(pIn, aBucket[i]) : aBucket[i];
51251  }
51252  return pIn;
51253}
51254
51255
51256/*
51257** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
51258** Convert this tree into a linked list connected by the pRight pointers
51259** and return pointers to the first and last elements of the new list.
51260*/
51261static void rowSetTreeToList(
51262  struct RowSetEntry *pIn,         /* Root of the input tree */
51263  struct RowSetEntry **ppFirst,    /* Write head of the output list here */
51264  struct RowSetEntry **ppLast      /* Write tail of the output list here */
51265){
51266  assert( pIn!=0 );
51267  if( pIn->pLeft ){
51268    struct RowSetEntry *p;
51269    rowSetTreeToList(pIn->pLeft, ppFirst, &p);
51270    p->pRight = pIn;
51271  }else{
51272    *ppFirst = pIn;
51273  }
51274  if( pIn->pRight ){
51275    rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
51276  }else{
51277    *ppLast = pIn;
51278  }
51279  assert( (*ppLast)->pRight==0 );
51280}
51281
51282
51283/*
51284** Convert a sorted list of elements (connected by pRight) into a binary
51285** tree with depth of iDepth.  A depth of 1 means the tree contains a single
51286** node taken from the head of *ppList.  A depth of 2 means a tree with
51287** three nodes.  And so forth.
51288**
51289** Use as many entries from the input list as required and update the
51290** *ppList to point to the unused elements of the list.  If the input
51291** list contains too few elements, then construct an incomplete tree
51292** and leave *ppList set to NULL.
51293**
51294** Return a pointer to the root of the constructed binary tree.
51295*/
51296static struct RowSetEntry *rowSetNDeepTree(
51297  struct RowSetEntry **ppList,
51298  int iDepth
51299){
51300  struct RowSetEntry *p;         /* Root of the new tree */
51301  struct RowSetEntry *pLeft;     /* Left subtree */
51302  if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
51303    /* Prevent unnecessary deep recursion when we run out of entries */
51304    return 0;
51305  }
51306  if( iDepth>1 ){   /*OPTIMIZATION-IF-TRUE*/
51307    /* This branch causes a *balanced* tree to be generated.  A valid tree
51308    ** is still generated without this branch, but the tree is wildly
51309    ** unbalanced and inefficient. */
51310    pLeft = rowSetNDeepTree(ppList, iDepth-1);
51311    p = *ppList;
51312    if( p==0 ){     /*OPTIMIZATION-IF-FALSE*/
51313      /* It is safe to always return here, but the resulting tree
51314      ** would be unbalanced */
51315      return pLeft;
51316    }
51317    p->pLeft = pLeft;
51318    *ppList = p->pRight;
51319    p->pRight = rowSetNDeepTree(ppList, iDepth-1);
51320  }else{
51321    p = *ppList;
51322    *ppList = p->pRight;
51323    p->pLeft = p->pRight = 0;
51324  }
51325  return p;
51326}
51327
51328/*
51329** Convert a sorted list of elements into a binary tree. Make the tree
51330** as deep as it needs to be in order to contain the entire list.
51331*/
51332static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
51333  int iDepth;           /* Depth of the tree so far */
51334  struct RowSetEntry *p;       /* Current tree root */
51335  struct RowSetEntry *pLeft;   /* Left subtree */
51336
51337  assert( pList!=0 );
51338  p = pList;
51339  pList = p->pRight;
51340  p->pLeft = p->pRight = 0;
51341  for(iDepth=1; pList; iDepth++){
51342    pLeft = p;
51343    p = pList;
51344    pList = p->pRight;
51345    p->pLeft = pLeft;
51346    p->pRight = rowSetNDeepTree(&pList, iDepth);
51347  }
51348  return p;
51349}
51350
51351/*
51352** Extract the smallest element from the RowSet.
51353** Write the element into *pRowid.  Return 1 on success.  Return
51354** 0 if the RowSet is already empty.
51355**
51356** After this routine has been called, the sqlite3RowSetInsert()
51357** routine may not be called again.
51358**
51359** This routine may not be called after sqlite3RowSetTest() has
51360** been used.  Older versions of RowSet allowed that, but as the
51361** capability was not used by the code generator, it was removed
51362** for code economy.
51363*/
51364SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
51365  assert( p!=0 );
51366  assert( p->pForest==0 );  /* Cannot be used with sqlite3RowSetText() */
51367
51368  /* Merge the forest into a single sorted list on first call */
51369  if( (p->rsFlags & ROWSET_NEXT)==0 ){  /*OPTIMIZATION-IF-FALSE*/
51370    if( (p->rsFlags & ROWSET_SORTED)==0 ){  /*OPTIMIZATION-IF-FALSE*/
51371      p->pEntry = rowSetEntrySort(p->pEntry);
51372    }
51373    p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
51374  }
51375
51376  /* Return the next entry on the list */
51377  if( p->pEntry ){
51378    *pRowid = p->pEntry->v;
51379    p->pEntry = p->pEntry->pRight;
51380    if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
51381      /* Free memory immediately, rather than waiting on sqlite3_finalize() */
51382      sqlite3RowSetClear(p);
51383    }
51384    return 1;
51385  }else{
51386    return 0;
51387  }
51388}
51389
51390/*
51391** Check to see if element iRowid was inserted into the rowset as
51392** part of any insert batch prior to iBatch.  Return 1 or 0.
51393**
51394** If this is the first test of a new batch and if there exist entries
51395** on pRowSet->pEntry, then sort those entries into the forest at
51396** pRowSet->pForest so that they can be tested.
51397*/
51398SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
51399  struct RowSetEntry *p, *pTree;
51400
51401  /* This routine is never called after sqlite3RowSetNext() */
51402  assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
51403
51404  /* Sort entries into the forest on the first test of a new batch.
51405  ** To save unnecessary work, only do this when the batch number changes.
51406  */
51407  if( iBatch!=pRowSet->iBatch ){  /*OPTIMIZATION-IF-FALSE*/
51408    p = pRowSet->pEntry;
51409    if( p ){
51410      struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
51411      if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
51412        /* Only sort the current set of entries if they need it */
51413        p = rowSetEntrySort(p);
51414      }
51415      for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
51416        ppPrevTree = &pTree->pRight;
51417        if( pTree->pLeft==0 ){
51418          pTree->pLeft = rowSetListToTree(p);
51419          break;
51420        }else{
51421          struct RowSetEntry *pAux, *pTail;
51422          rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
51423          pTree->pLeft = 0;
51424          p = rowSetEntryMerge(pAux, p);
51425        }
51426      }
51427      if( pTree==0 ){
51428        *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
51429        if( pTree ){
51430          pTree->v = 0;
51431          pTree->pRight = 0;
51432          pTree->pLeft = rowSetListToTree(p);
51433        }
51434      }
51435      pRowSet->pEntry = 0;
51436      pRowSet->pLast = 0;
51437      pRowSet->rsFlags |= ROWSET_SORTED;
51438    }
51439    pRowSet->iBatch = iBatch;
51440  }
51441
51442  /* Test to see if the iRowid value appears anywhere in the forest.
51443  ** Return 1 if it does and 0 if not.
51444  */
51445  for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
51446    p = pTree->pLeft;
51447    while( p ){
51448      if( p->v<iRowid ){
51449        p = p->pRight;
51450      }else if( p->v>iRowid ){
51451        p = p->pLeft;
51452      }else{
51453        return 1;
51454      }
51455    }
51456  }
51457  return 0;
51458}
51459
51460/************** End of rowset.c **********************************************/
51461/************** Begin file pager.c *******************************************/
51462/*
51463** 2001 September 15
51464**
51465** The author disclaims copyright to this source code.  In place of
51466** a legal notice, here is a blessing:
51467**
51468**    May you do good and not evil.
51469**    May you find forgiveness for yourself and forgive others.
51470**    May you share freely, never taking more than you give.
51471**
51472*************************************************************************
51473** This is the implementation of the page cache subsystem or "pager".
51474**
51475** The pager is used to access a database disk file.  It implements
51476** atomic commit and rollback through the use of a journal file that
51477** is separate from the database file.  The pager also implements file
51478** locking to prevent two processes from writing the same database
51479** file simultaneously, or one process from reading the database while
51480** another is writing.
51481*/
51482#ifndef SQLITE_OMIT_DISKIO
51483/* #include "sqliteInt.h" */
51484/************** Include wal.h in the middle of pager.c ***********************/
51485/************** Begin file wal.h *********************************************/
51486/*
51487** 2010 February 1
51488**
51489** The author disclaims copyright to this source code.  In place of
51490** a legal notice, here is a blessing:
51491**
51492**    May you do good and not evil.
51493**    May you find forgiveness for yourself and forgive others.
51494**    May you share freely, never taking more than you give.
51495**
51496*************************************************************************
51497** This header file defines the interface to the write-ahead logging
51498** system. Refer to the comments below and the header comment attached to
51499** the implementation of each function in log.c for further details.
51500*/
51501
51502#ifndef SQLITE_WAL_H
51503#define SQLITE_WAL_H
51504
51505/* #include "sqliteInt.h" */
51506
51507/* Macros for extracting appropriate sync flags for either transaction
51508** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)):
51509*/
51510#define WAL_SYNC_FLAGS(X)   ((X)&0x03)
51511#define CKPT_SYNC_FLAGS(X)  (((X)>>2)&0x03)
51512
51513#ifdef SQLITE_OMIT_WAL
51514# define sqlite3WalOpen(x,y,z)                   0
51515# define sqlite3WalLimit(x,y)
51516# define sqlite3WalClose(v,w,x,y,z)              0
51517# define sqlite3WalBeginReadTransaction(y,z)     0
51518# define sqlite3WalEndReadTransaction(z)
51519# define sqlite3WalDbsize(y)                     0
51520# define sqlite3WalBeginWriteTransaction(y)      0
51521# define sqlite3WalEndWriteTransaction(x)        0
51522# define sqlite3WalUndo(x,y,z)                   0
51523# define sqlite3WalSavepoint(y,z)
51524# define sqlite3WalSavepointUndo(y,z)            0
51525# define sqlite3WalFrames(u,v,w,x,y,z)           0
51526# define sqlite3WalCheckpoint(q,r,s,t,u,v,w,x,y,z) 0
51527# define sqlite3WalCallback(z)                   0
51528# define sqlite3WalExclusiveMode(y,z)            0
51529# define sqlite3WalHeapMemory(z)                 0
51530# define sqlite3WalFramesize(z)                  0
51531# define sqlite3WalFindFrame(x,y,z)              0
51532# define sqlite3WalFile(x)                       0
51533#else
51534
51535#define WAL_SAVEPOINT_NDATA 4
51536
51537/* Connection to a write-ahead log (WAL) file.
51538** There is one object of this type for each pager.
51539*/
51540typedef struct Wal Wal;
51541
51542/* Open and close a connection to a write-ahead log. */
51543SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
51544SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *);
51545
51546/* Set the limiting size of a WAL file. */
51547SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
51548
51549/* Used by readers to open (lock) and close (unlock) a snapshot.  A
51550** snapshot is like a read-transaction.  It is the state of the database
51551** at an instant in time.  sqlite3WalOpenSnapshot gets a read lock and
51552** preserves the current state even if the other threads or processes
51553** write to or checkpoint the WAL.  sqlite3WalCloseSnapshot() closes the
51554** transaction and releases the lock.
51555*/
51556SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
51557SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
51558
51559/* Read a page from the write-ahead log, if it is present. */
51560SQLITE_PRIVATE int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
51561SQLITE_PRIVATE int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
51562
51563/* If the WAL is not empty, return the size of the database. */
51564SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
51565
51566/* Obtain or release the WRITER lock. */
51567SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
51568SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
51569
51570/* Undo any frames written (but not committed) to the log */
51571SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
51572
51573/* Return an integer that records the current (uncommitted) write
51574** position in the WAL */
51575SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
51576
51577/* Move the write position of the WAL back to iFrame.  Called in
51578** response to a ROLLBACK TO command. */
51579SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
51580
51581/* Write a frame or frames to the log. */
51582SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
51583
51584/* Copy pages from the log to the database file */
51585SQLITE_PRIVATE int sqlite3WalCheckpoint(
51586  Wal *pWal,                      /* Write-ahead log connection */
51587  sqlite3 *db,                    /* Check this handle's interrupt flag */
51588  int eMode,                      /* One of PASSIVE, FULL and RESTART */
51589  int (*xBusy)(void*),            /* Function to call when busy */
51590  void *pBusyArg,                 /* Context argument for xBusyHandler */
51591  int sync_flags,                 /* Flags to sync db file with (or 0) */
51592  int nBuf,                       /* Size of buffer nBuf */
51593  u8 *zBuf,                       /* Temporary buffer to use */
51594  int *pnLog,                     /* OUT: Number of frames in WAL */
51595  int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
51596);
51597
51598/* Return the value to pass to a sqlite3_wal_hook callback, the
51599** number of frames in the WAL at the point of the last commit since
51600** sqlite3WalCallback() was called.  If no commits have occurred since
51601** the last call, then return 0.
51602*/
51603SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
51604
51605/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
51606** by the pager layer on the database file.
51607*/
51608SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
51609
51610/* Return true if the argument is non-NULL and the WAL module is using
51611** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
51612** WAL module is using shared-memory, return false.
51613*/
51614SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
51615
51616#ifdef SQLITE_ENABLE_SNAPSHOT
51617SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot);
51618SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot);
51619SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal);
51620SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot);
51621SQLITE_PRIVATE void sqlite3WalSnapshotUnlock(Wal *pWal);
51622#endif
51623
51624#ifdef SQLITE_ENABLE_ZIPVFS
51625/* If the WAL file is not empty, return the number of bytes of content
51626** stored in each frame (i.e. the db page-size when the WAL was created).
51627*/
51628SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal);
51629#endif
51630
51631/* Return the sqlite3_file object for the WAL file */
51632SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal);
51633
51634#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
51635SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock);
51636SQLITE_PRIVATE void sqlite3WalDb(Wal *pWal, sqlite3 *db);
51637#endif
51638
51639#endif /* ifndef SQLITE_OMIT_WAL */
51640#endif /* SQLITE_WAL_H */
51641
51642/************** End of wal.h *************************************************/
51643/************** Continuing where we left off in pager.c **********************/
51644
51645
51646/******************* NOTES ON THE DESIGN OF THE PAGER ************************
51647**
51648** This comment block describes invariants that hold when using a rollback
51649** journal.  These invariants do not apply for journal_mode=WAL,
51650** journal_mode=MEMORY, or journal_mode=OFF.
51651**
51652** Within this comment block, a page is deemed to have been synced
51653** automatically as soon as it is written when PRAGMA synchronous=OFF.
51654** Otherwise, the page is not synced until the xSync method of the VFS
51655** is called successfully on the file containing the page.
51656**
51657** Definition:  A page of the database file is said to be "overwriteable" if
51658** one or more of the following are true about the page:
51659**
51660**     (a)  The original content of the page as it was at the beginning of
51661**          the transaction has been written into the rollback journal and
51662**          synced.
51663**
51664**     (b)  The page was a freelist leaf page at the start of the transaction.
51665**
51666**     (c)  The page number is greater than the largest page that existed in
51667**          the database file at the start of the transaction.
51668**
51669** (1) A page of the database file is never overwritten unless one of the
51670**     following are true:
51671**
51672**     (a) The page and all other pages on the same sector are overwriteable.
51673**
51674**     (b) The atomic page write optimization is enabled, and the entire
51675**         transaction other than the update of the transaction sequence
51676**         number consists of a single page change.
51677**
51678** (2) The content of a page written into the rollback journal exactly matches
51679**     both the content in the database when the rollback journal was written
51680**     and the content in the database at the beginning of the current
51681**     transaction.
51682**
51683** (3) Writes to the database file are an integer multiple of the page size
51684**     in length and are aligned on a page boundary.
51685**
51686** (4) Reads from the database file are either aligned on a page boundary and
51687**     an integer multiple of the page size in length or are taken from the
51688**     first 100 bytes of the database file.
51689**
51690** (5) All writes to the database file are synced prior to the rollback journal
51691**     being deleted, truncated, or zeroed.
51692**
51693** (6) If a master journal file is used, then all writes to the database file
51694**     are synced prior to the master journal being deleted.
51695**
51696** Definition: Two databases (or the same database at two points it time)
51697** are said to be "logically equivalent" if they give the same answer to
51698** all queries.  Note in particular the content of freelist leaf
51699** pages can be changed arbitrarily without affecting the logical equivalence
51700** of the database.
51701**
51702** (7) At any time, if any subset, including the empty set and the total set,
51703**     of the unsynced changes to a rollback journal are removed and the
51704**     journal is rolled back, the resulting database file will be logically
51705**     equivalent to the database file at the beginning of the transaction.
51706**
51707** (8) When a transaction is rolled back, the xTruncate method of the VFS
51708**     is called to restore the database file to the same size it was at
51709**     the beginning of the transaction.  (In some VFSes, the xTruncate
51710**     method is a no-op, but that does not change the fact the SQLite will
51711**     invoke it.)
51712**
51713** (9) Whenever the database file is modified, at least one bit in the range
51714**     of bytes from 24 through 39 inclusive will be changed prior to releasing
51715**     the EXCLUSIVE lock, thus signaling other connections on the same
51716**     database to flush their caches.
51717**
51718** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
51719**      than one billion transactions.
51720**
51721** (11) A database file is well-formed at the beginning and at the conclusion
51722**      of every transaction.
51723**
51724** (12) An EXCLUSIVE lock is held on the database file when writing to
51725**      the database file.
51726**
51727** (13) A SHARED lock is held on the database file while reading any
51728**      content out of the database file.
51729**
51730******************************************************************************/
51731
51732/*
51733** Macros for troubleshooting.  Normally turned off
51734*/
51735#if 0
51736int sqlite3PagerTrace=1;  /* True to enable tracing */
51737#define sqlite3DebugPrintf printf
51738#define PAGERTRACE(X)     if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
51739#else
51740#define PAGERTRACE(X)
51741#endif
51742
51743/*
51744** The following two macros are used within the PAGERTRACE() macros above
51745** to print out file-descriptors.
51746**
51747** PAGERID() takes a pointer to a Pager struct as its argument. The
51748** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
51749** struct as its argument.
51750*/
51751#define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
51752#define FILEHANDLEID(fd) (SQLITE_PTR_TO_INT(fd))
51753
51754/*
51755** The Pager.eState variable stores the current 'state' of a pager. A
51756** pager may be in any one of the seven states shown in the following
51757** state diagram.
51758**
51759**                            OPEN <------+------+
51760**                              |         |      |
51761**                              V         |      |
51762**               +---------> READER-------+      |
51763**               |              |                |
51764**               |              V                |
51765**               |<-------WRITER_LOCKED------> ERROR
51766**               |              |                ^
51767**               |              V                |
51768**               |<------WRITER_CACHEMOD-------->|
51769**               |              |                |
51770**               |              V                |
51771**               |<-------WRITER_DBMOD---------->|
51772**               |              |                |
51773**               |              V                |
51774**               +<------WRITER_FINISHED-------->+
51775**
51776**
51777** List of state transitions and the C [function] that performs each:
51778**
51779**   OPEN              -> READER              [sqlite3PagerSharedLock]
51780**   READER            -> OPEN                [pager_unlock]
51781**
51782**   READER            -> WRITER_LOCKED       [sqlite3PagerBegin]
51783**   WRITER_LOCKED     -> WRITER_CACHEMOD     [pager_open_journal]
51784**   WRITER_CACHEMOD   -> WRITER_DBMOD        [syncJournal]
51785**   WRITER_DBMOD      -> WRITER_FINISHED     [sqlite3PagerCommitPhaseOne]
51786**   WRITER_***        -> READER              [pager_end_transaction]
51787**
51788**   WRITER_***        -> ERROR               [pager_error]
51789**   ERROR             -> OPEN                [pager_unlock]
51790**
51791**
51792**  OPEN:
51793**
51794**    The pager starts up in this state. Nothing is guaranteed in this
51795**    state - the file may or may not be locked and the database size is
51796**    unknown. The database may not be read or written.
51797**
51798**    * No read or write transaction is active.
51799**    * Any lock, or no lock at all, may be held on the database file.
51800**    * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
51801**
51802**  READER:
51803**
51804**    In this state all the requirements for reading the database in
51805**    rollback (non-WAL) mode are met. Unless the pager is (or recently
51806**    was) in exclusive-locking mode, a user-level read transaction is
51807**    open. The database size is known in this state.
51808**
51809**    A connection running with locking_mode=normal enters this state when
51810**    it opens a read-transaction on the database and returns to state
51811**    OPEN after the read-transaction is completed. However a connection
51812**    running in locking_mode=exclusive (including temp databases) remains in
51813**    this state even after the read-transaction is closed. The only way
51814**    a locking_mode=exclusive connection can transition from READER to OPEN
51815**    is via the ERROR state (see below).
51816**
51817**    * A read transaction may be active (but a write-transaction cannot).
51818**    * A SHARED or greater lock is held on the database file.
51819**    * The dbSize variable may be trusted (even if a user-level read
51820**      transaction is not active). The dbOrigSize and dbFileSize variables
51821**      may not be trusted at this point.
51822**    * If the database is a WAL database, then the WAL connection is open.
51823**    * Even if a read-transaction is not open, it is guaranteed that
51824**      there is no hot-journal in the file-system.
51825**
51826**  WRITER_LOCKED:
51827**
51828**    The pager moves to this state from READER when a write-transaction
51829**    is first opened on the database. In WRITER_LOCKED state, all locks
51830**    required to start a write-transaction are held, but no actual
51831**    modifications to the cache or database have taken place.
51832**
51833**    In rollback mode, a RESERVED or (if the transaction was opened with
51834**    BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
51835**    moving to this state, but the journal file is not written to or opened
51836**    to in this state. If the transaction is committed or rolled back while
51837**    in WRITER_LOCKED state, all that is required is to unlock the database
51838**    file.
51839**
51840**    IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
51841**    If the connection is running with locking_mode=exclusive, an attempt
51842**    is made to obtain an EXCLUSIVE lock on the database file.
51843**
51844**    * A write transaction is active.
51845**    * If the connection is open in rollback-mode, a RESERVED or greater
51846**      lock is held on the database file.
51847**    * If the connection is open in WAL-mode, a WAL write transaction
51848**      is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
51849**      called).
51850**    * The dbSize, dbOrigSize and dbFileSize variables are all valid.
51851**    * The contents of the pager cache have not been modified.
51852**    * The journal file may or may not be open.
51853**    * Nothing (not even the first header) has been written to the journal.
51854**
51855**  WRITER_CACHEMOD:
51856**
51857**    A pager moves from WRITER_LOCKED state to this state when a page is
51858**    first modified by the upper layer. In rollback mode the journal file
51859**    is opened (if it is not already open) and a header written to the
51860**    start of it. The database file on disk has not been modified.
51861**
51862**    * A write transaction is active.
51863**    * A RESERVED or greater lock is held on the database file.
51864**    * The journal file is open and the first header has been written
51865**      to it, but the header has not been synced to disk.
51866**    * The contents of the page cache have been modified.
51867**
51868**  WRITER_DBMOD:
51869**
51870**    The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
51871**    when it modifies the contents of the database file. WAL connections
51872**    never enter this state (since they do not modify the database file,
51873**    just the log file).
51874**
51875**    * A write transaction is active.
51876**    * An EXCLUSIVE or greater lock is held on the database file.
51877**    * The journal file is open and the first header has been written
51878**      and synced to disk.
51879**    * The contents of the page cache have been modified (and possibly
51880**      written to disk).
51881**
51882**  WRITER_FINISHED:
51883**
51884**    It is not possible for a WAL connection to enter this state.
51885**
51886**    A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
51887**    state after the entire transaction has been successfully written into the
51888**    database file. In this state the transaction may be committed simply
51889**    by finalizing the journal file. Once in WRITER_FINISHED state, it is
51890**    not possible to modify the database further. At this point, the upper
51891**    layer must either commit or rollback the transaction.
51892**
51893**    * A write transaction is active.
51894**    * An EXCLUSIVE or greater lock is held on the database file.
51895**    * All writing and syncing of journal and database data has finished.
51896**      If no error occurred, all that remains is to finalize the journal to
51897**      commit the transaction. If an error did occur, the caller will need
51898**      to rollback the transaction.
51899**
51900**  ERROR:
51901**
51902**    The ERROR state is entered when an IO or disk-full error (including
51903**    SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
51904**    difficult to be sure that the in-memory pager state (cache contents,
51905**    db size etc.) are consistent with the contents of the file-system.
51906**
51907**    Temporary pager files may enter the ERROR state, but in-memory pagers
51908**    cannot.
51909**
51910**    For example, if an IO error occurs while performing a rollback,
51911**    the contents of the page-cache may be left in an inconsistent state.
51912**    At this point it would be dangerous to change back to READER state
51913**    (as usually happens after a rollback). Any subsequent readers might
51914**    report database corruption (due to the inconsistent cache), and if
51915**    they upgrade to writers, they may inadvertently corrupt the database
51916**    file. To avoid this hazard, the pager switches into the ERROR state
51917**    instead of READER following such an error.
51918**
51919**    Once it has entered the ERROR state, any attempt to use the pager
51920**    to read or write data returns an error. Eventually, once all
51921**    outstanding transactions have been abandoned, the pager is able to
51922**    transition back to OPEN state, discarding the contents of the
51923**    page-cache and any other in-memory state at the same time. Everything
51924**    is reloaded from disk (and, if necessary, hot-journal rollback peformed)
51925**    when a read-transaction is next opened on the pager (transitioning
51926**    the pager into READER state). At that point the system has recovered
51927**    from the error.
51928**
51929**    Specifically, the pager jumps into the ERROR state if:
51930**
51931**      1. An error occurs while attempting a rollback. This happens in
51932**         function sqlite3PagerRollback().
51933**
51934**      2. An error occurs while attempting to finalize a journal file
51935**         following a commit in function sqlite3PagerCommitPhaseTwo().
51936**
51937**      3. An error occurs while attempting to write to the journal or
51938**         database file in function pagerStress() in order to free up
51939**         memory.
51940**
51941**    In other cases, the error is returned to the b-tree layer. The b-tree
51942**    layer then attempts a rollback operation. If the error condition
51943**    persists, the pager enters the ERROR state via condition (1) above.
51944**
51945**    Condition (3) is necessary because it can be triggered by a read-only
51946**    statement executed within a transaction. In this case, if the error
51947**    code were simply returned to the user, the b-tree layer would not
51948**    automatically attempt a rollback, as it assumes that an error in a
51949**    read-only statement cannot leave the pager in an internally inconsistent
51950**    state.
51951**
51952**    * The Pager.errCode variable is set to something other than SQLITE_OK.
51953**    * There are one or more outstanding references to pages (after the
51954**      last reference is dropped the pager should move back to OPEN state).
51955**    * The pager is not an in-memory pager.
51956**
51957**
51958** Notes:
51959**
51960**   * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
51961**     connection is open in WAL mode. A WAL connection is always in one
51962**     of the first four states.
51963**
51964**   * Normally, a connection open in exclusive mode is never in PAGER_OPEN
51965**     state. There are two exceptions: immediately after exclusive-mode has
51966**     been turned on (and before any read or write transactions are
51967**     executed), and when the pager is leaving the "error state".
51968**
51969**   * See also: assert_pager_state().
51970*/
51971#define PAGER_OPEN                  0
51972#define PAGER_READER                1
51973#define PAGER_WRITER_LOCKED         2
51974#define PAGER_WRITER_CACHEMOD       3
51975#define PAGER_WRITER_DBMOD          4
51976#define PAGER_WRITER_FINISHED       5
51977#define PAGER_ERROR                 6
51978
51979/*
51980** The Pager.eLock variable is almost always set to one of the
51981** following locking-states, according to the lock currently held on
51982** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
51983** This variable is kept up to date as locks are taken and released by
51984** the pagerLockDb() and pagerUnlockDb() wrappers.
51985**
51986** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
51987** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
51988** the operation was successful. In these circumstances pagerLockDb() and
51989** pagerUnlockDb() take a conservative approach - eLock is always updated
51990** when unlocking the file, and only updated when locking the file if the
51991** VFS call is successful. This way, the Pager.eLock variable may be set
51992** to a less exclusive (lower) value than the lock that is actually held
51993** at the system level, but it is never set to a more exclusive value.
51994**
51995** This is usually safe. If an xUnlock fails or appears to fail, there may
51996** be a few redundant xLock() calls or a lock may be held for longer than
51997** required, but nothing really goes wrong.
51998**
51999** The exception is when the database file is unlocked as the pager moves
52000** from ERROR to OPEN state. At this point there may be a hot-journal file
52001** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
52002** transition, by the same pager or any other). If the call to xUnlock()
52003** fails at this point and the pager is left holding an EXCLUSIVE lock, this
52004** can confuse the call to xCheckReservedLock() call made later as part
52005** of hot-journal detection.
52006**
52007** xCheckReservedLock() is defined as returning true "if there is a RESERVED
52008** lock held by this process or any others". So xCheckReservedLock may
52009** return true because the caller itself is holding an EXCLUSIVE lock (but
52010** doesn't know it because of a previous error in xUnlock). If this happens
52011** a hot-journal may be mistaken for a journal being created by an active
52012** transaction in another process, causing SQLite to read from the database
52013** without rolling it back.
52014**
52015** To work around this, if a call to xUnlock() fails when unlocking the
52016** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
52017** is only changed back to a real locking state after a successful call
52018** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
52019** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
52020** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
52021** lock on the database file before attempting to roll it back. See function
52022** PagerSharedLock() for more detail.
52023**
52024** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
52025** PAGER_OPEN state.
52026*/
52027#define UNKNOWN_LOCK                (EXCLUSIVE_LOCK+1)
52028
52029/*
52030** The maximum allowed sector size. 64KiB. If the xSectorsize() method
52031** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
52032** This could conceivably cause corruption following a power failure on
52033** such a system. This is currently an undocumented limit.
52034*/
52035#define MAX_SECTOR_SIZE 0x10000
52036
52037
52038/*
52039** An instance of the following structure is allocated for each active
52040** savepoint and statement transaction in the system. All such structures
52041** are stored in the Pager.aSavepoint[] array, which is allocated and
52042** resized using sqlite3Realloc().
52043**
52044** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
52045** set to 0. If a journal-header is written into the main journal while
52046** the savepoint is active, then iHdrOffset is set to the byte offset
52047** immediately following the last journal record written into the main
52048** journal before the journal-header. This is required during savepoint
52049** rollback (see pagerPlaybackSavepoint()).
52050*/
52051typedef struct PagerSavepoint PagerSavepoint;
52052struct PagerSavepoint {
52053  i64 iOffset;                 /* Starting offset in main journal */
52054  i64 iHdrOffset;              /* See above */
52055  Bitvec *pInSavepoint;        /* Set of pages in this savepoint */
52056  Pgno nOrig;                  /* Original number of pages in file */
52057  Pgno iSubRec;                /* Index of first record in sub-journal */
52058#ifndef SQLITE_OMIT_WAL
52059  u32 aWalData[WAL_SAVEPOINT_NDATA];        /* WAL savepoint context */
52060#endif
52061};
52062
52063/*
52064** Bits of the Pager.doNotSpill flag.  See further description below.
52065*/
52066#define SPILLFLAG_OFF         0x01 /* Never spill cache.  Set via pragma */
52067#define SPILLFLAG_ROLLBACK    0x02 /* Current rolling back, so do not spill */
52068#define SPILLFLAG_NOSYNC      0x04 /* Spill is ok, but do not sync */
52069
52070/*
52071** An open page cache is an instance of struct Pager. A description of
52072** some of the more important member variables follows:
52073**
52074** eState
52075**
52076**   The current 'state' of the pager object. See the comment and state
52077**   diagram above for a description of the pager state.
52078**
52079** eLock
52080**
52081**   For a real on-disk database, the current lock held on the database file -
52082**   NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
52083**
52084**   For a temporary or in-memory database (neither of which require any
52085**   locks), this variable is always set to EXCLUSIVE_LOCK. Since such
52086**   databases always have Pager.exclusiveMode==1, this tricks the pager
52087**   logic into thinking that it already has all the locks it will ever
52088**   need (and no reason to release them).
52089**
52090**   In some (obscure) circumstances, this variable may also be set to
52091**   UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
52092**   details.
52093**
52094** changeCountDone
52095**
52096**   This boolean variable is used to make sure that the change-counter
52097**   (the 4-byte header field at byte offset 24 of the database file) is
52098**   not updated more often than necessary.
52099**
52100**   It is set to true when the change-counter field is updated, which
52101**   can only happen if an exclusive lock is held on the database file.
52102**   It is cleared (set to false) whenever an exclusive lock is
52103**   relinquished on the database file. Each time a transaction is committed,
52104**   The changeCountDone flag is inspected. If it is true, the work of
52105**   updating the change-counter is omitted for the current transaction.
52106**
52107**   This mechanism means that when running in exclusive mode, a connection
52108**   need only update the change-counter once, for the first transaction
52109**   committed.
52110**
52111** setMaster
52112**
52113**   When PagerCommitPhaseOne() is called to commit a transaction, it may
52114**   (or may not) specify a master-journal name to be written into the
52115**   journal file before it is synced to disk.
52116**
52117**   Whether or not a journal file contains a master-journal pointer affects
52118**   the way in which the journal file is finalized after the transaction is
52119**   committed or rolled back when running in "journal_mode=PERSIST" mode.
52120**   If a journal file does not contain a master-journal pointer, it is
52121**   finalized by overwriting the first journal header with zeroes. If
52122**   it does contain a master-journal pointer the journal file is finalized
52123**   by truncating it to zero bytes, just as if the connection were
52124**   running in "journal_mode=truncate" mode.
52125**
52126**   Journal files that contain master journal pointers cannot be finalized
52127**   simply by overwriting the first journal-header with zeroes, as the
52128**   master journal pointer could interfere with hot-journal rollback of any
52129**   subsequently interrupted transaction that reuses the journal file.
52130**
52131**   The flag is cleared as soon as the journal file is finalized (either
52132**   by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
52133**   journal file from being successfully finalized, the setMaster flag
52134**   is cleared anyway (and the pager will move to ERROR state).
52135**
52136** doNotSpill
52137**
52138**   This variables control the behavior of cache-spills  (calls made by
52139**   the pcache module to the pagerStress() routine to write cached data
52140**   to the file-system in order to free up memory).
52141**
52142**   When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
52143**   writing to the database from pagerStress() is disabled altogether.
52144**   The SPILLFLAG_ROLLBACK case is done in a very obscure case that
52145**   comes up during savepoint rollback that requires the pcache module
52146**   to allocate a new page to prevent the journal file from being written
52147**   while it is being traversed by code in pager_playback().  The SPILLFLAG_OFF
52148**   case is a user preference.
52149**
52150**   If the SPILLFLAG_NOSYNC bit is set, writing to the database from
52151**   pagerStress() is permitted, but syncing the journal file is not.
52152**   This flag is set by sqlite3PagerWrite() when the file-system sector-size
52153**   is larger than the database page-size in order to prevent a journal sync
52154**   from happening in between the journalling of two pages on the same sector.
52155**
52156** subjInMemory
52157**
52158**   This is a boolean variable. If true, then any required sub-journal
52159**   is opened as an in-memory journal file. If false, then in-memory
52160**   sub-journals are only used for in-memory pager files.
52161**
52162**   This variable is updated by the upper layer each time a new
52163**   write-transaction is opened.
52164**
52165** dbSize, dbOrigSize, dbFileSize
52166**
52167**   Variable dbSize is set to the number of pages in the database file.
52168**   It is valid in PAGER_READER and higher states (all states except for
52169**   OPEN and ERROR).
52170**
52171**   dbSize is set based on the size of the database file, which may be
52172**   larger than the size of the database (the value stored at offset
52173**   28 of the database header by the btree). If the size of the file
52174**   is not an integer multiple of the page-size, the value stored in
52175**   dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
52176**   Except, any file that is greater than 0 bytes in size is considered
52177**   to have at least one page. (i.e. a 1KB file with 2K page-size leads
52178**   to dbSize==1).
52179**
52180**   During a write-transaction, if pages with page-numbers greater than
52181**   dbSize are modified in the cache, dbSize is updated accordingly.
52182**   Similarly, if the database is truncated using PagerTruncateImage(),
52183**   dbSize is updated.
52184**
52185**   Variables dbOrigSize and dbFileSize are valid in states
52186**   PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
52187**   variable at the start of the transaction. It is used during rollback,
52188**   and to determine whether or not pages need to be journalled before
52189**   being modified.
52190**
52191**   Throughout a write-transaction, dbFileSize contains the size of
52192**   the file on disk in pages. It is set to a copy of dbSize when the
52193**   write-transaction is first opened, and updated when VFS calls are made
52194**   to write or truncate the database file on disk.
52195**
52196**   The only reason the dbFileSize variable is required is to suppress
52197**   unnecessary calls to xTruncate() after committing a transaction. If,
52198**   when a transaction is committed, the dbFileSize variable indicates
52199**   that the database file is larger than the database image (Pager.dbSize),
52200**   pager_truncate() is called. The pager_truncate() call uses xFilesize()
52201**   to measure the database file on disk, and then truncates it if required.
52202**   dbFileSize is not used when rolling back a transaction. In this case
52203**   pager_truncate() is called unconditionally (which means there may be
52204**   a call to xFilesize() that is not strictly required). In either case,
52205**   pager_truncate() may cause the file to become smaller or larger.
52206**
52207** dbHintSize
52208**
52209**   The dbHintSize variable is used to limit the number of calls made to
52210**   the VFS xFileControl(FCNTL_SIZE_HINT) method.
52211**
52212**   dbHintSize is set to a copy of the dbSize variable when a
52213**   write-transaction is opened (at the same time as dbFileSize and
52214**   dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
52215**   dbHintSize is increased to the number of pages that correspond to the
52216**   size-hint passed to the method call. See pager_write_pagelist() for
52217**   details.
52218**
52219** errCode
52220**
52221**   The Pager.errCode variable is only ever used in PAGER_ERROR state. It
52222**   is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
52223**   is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
52224**   sub-codes.
52225**
52226** syncFlags, walSyncFlags
52227**
52228**   syncFlags is either SQLITE_SYNC_NORMAL (0x02) or SQLITE_SYNC_FULL (0x03).
52229**   syncFlags is used for rollback mode.  walSyncFlags is used for WAL mode
52230**   and contains the flags used to sync the checkpoint operations in the
52231**   lower two bits, and sync flags used for transaction commits in the WAL
52232**   file in bits 0x04 and 0x08.  In other words, to get the correct sync flags
52233**   for checkpoint operations, use (walSyncFlags&0x03) and to get the correct
52234**   sync flags for transaction commit, use ((walSyncFlags>>2)&0x03).  Note
52235**   that with synchronous=NORMAL in WAL mode, transaction commit is not synced
52236**   meaning that the 0x04 and 0x08 bits are both zero.
52237*/
52238struct Pager {
52239  sqlite3_vfs *pVfs;          /* OS functions to use for IO */
52240  u8 exclusiveMode;           /* Boolean. True if locking_mode==EXCLUSIVE */
52241  u8 journalMode;             /* One of the PAGER_JOURNALMODE_* values */
52242  u8 useJournal;              /* Use a rollback journal on this file */
52243  u8 noSync;                  /* Do not sync the journal if true */
52244  u8 fullSync;                /* Do extra syncs of the journal for robustness */
52245  u8 extraSync;               /* sync directory after journal delete */
52246  u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
52247  u8 walSyncFlags;            /* See description above */
52248  u8 tempFile;                /* zFilename is a temporary or immutable file */
52249  u8 noLock;                  /* Do not lock (except in WAL mode) */
52250  u8 readOnly;                /* True for a read-only database */
52251  u8 memDb;                   /* True to inhibit all file I/O */
52252
52253  /**************************************************************************
52254  ** The following block contains those class members that change during
52255  ** routine operation.  Class members not in this block are either fixed
52256  ** when the pager is first created or else only change when there is a
52257  ** significant mode change (such as changing the page_size, locking_mode,
52258  ** or the journal_mode).  From another view, these class members describe
52259  ** the "state" of the pager, while other class members describe the
52260  ** "configuration" of the pager.
52261  */
52262  u8 eState;                  /* Pager state (OPEN, READER, WRITER_LOCKED..) */
52263  u8 eLock;                   /* Current lock held on database file */
52264  u8 changeCountDone;         /* Set after incrementing the change-counter */
52265  u8 setMaster;               /* True if a m-j name has been written to jrnl */
52266  u8 doNotSpill;              /* Do not spill the cache when non-zero */
52267  u8 subjInMemory;            /* True to use in-memory sub-journals */
52268  u8 bUseFetch;               /* True to use xFetch() */
52269  u8 hasHeldSharedLock;       /* True if a shared lock has ever been held */
52270  Pgno dbSize;                /* Number of pages in the database */
52271  Pgno dbOrigSize;            /* dbSize before the current transaction */
52272  Pgno dbFileSize;            /* Number of pages in the database file */
52273  Pgno dbHintSize;            /* Value passed to FCNTL_SIZE_HINT call */
52274  int errCode;                /* One of several kinds of errors */
52275  int nRec;                   /* Pages journalled since last j-header written */
52276  u32 cksumInit;              /* Quasi-random value added to every checksum */
52277  u32 nSubRec;                /* Number of records written to sub-journal */
52278  Bitvec *pInJournal;         /* One bit for each page in the database file */
52279  sqlite3_file *fd;           /* File descriptor for database */
52280  sqlite3_file *jfd;          /* File descriptor for main journal */
52281  sqlite3_file *sjfd;         /* File descriptor for sub-journal */
52282  i64 journalOff;             /* Current write offset in the journal file */
52283  i64 journalHdr;             /* Byte offset to previous journal header */
52284  sqlite3_backup *pBackup;    /* Pointer to list of ongoing backup processes */
52285  PagerSavepoint *aSavepoint; /* Array of active savepoints */
52286  int nSavepoint;             /* Number of elements in aSavepoint[] */
52287  u32 iDataVersion;           /* Changes whenever database content changes */
52288  char dbFileVers[16];        /* Changes whenever database file changes */
52289
52290  int nMmapOut;               /* Number of mmap pages currently outstanding */
52291  sqlite3_int64 szMmap;       /* Desired maximum mmap size */
52292  PgHdr *pMmapFreelist;       /* List of free mmap page headers (pDirty) */
52293  /*
52294  ** End of the routinely-changing class members
52295  ***************************************************************************/
52296
52297  u16 nExtra;                 /* Add this many bytes to each in-memory page */
52298  i16 nReserve;               /* Number of unused bytes at end of each page */
52299  u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */
52300  u32 sectorSize;             /* Assumed sector size during rollback */
52301  int pageSize;               /* Number of bytes in a page */
52302  Pgno mxPgno;                /* Maximum allowed size of the database */
52303  i64 journalSizeLimit;       /* Size limit for persistent journal files */
52304  char *zFilename;            /* Name of the database file */
52305  char *zJournal;             /* Name of the journal file */
52306  int (*xBusyHandler)(void*); /* Function to call when busy */
52307  void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
52308  int aStat[4];               /* Total cache hits, misses, writes, spills */
52309#ifdef SQLITE_TEST
52310  int nRead;                  /* Database pages read */
52311#endif
52312  void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
52313  int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
52314  char *pTmpSpace;            /* Pager.pageSize bytes of space for tmp use */
52315  PCache *pPCache;            /* Pointer to page cache object */
52316#ifndef SQLITE_OMIT_WAL
52317  Wal *pWal;                  /* Write-ahead log used by "journal_mode=wal" */
52318  char *zWal;                 /* File name for write-ahead log */
52319#endif
52320};
52321
52322/*
52323** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
52324** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS
52325** or CACHE_WRITE to sqlite3_db_status().
52326*/
52327#define PAGER_STAT_HIT   0
52328#define PAGER_STAT_MISS  1
52329#define PAGER_STAT_WRITE 2
52330#define PAGER_STAT_SPILL 3
52331
52332/*
52333** The following global variables hold counters used for
52334** testing purposes only.  These variables do not exist in
52335** a non-testing build.  These variables are not thread-safe.
52336*/
52337#ifdef SQLITE_TEST
52338SQLITE_API int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
52339SQLITE_API int sqlite3_pager_writedb_count = 0;   /* Number of full pages written to DB */
52340SQLITE_API int sqlite3_pager_writej_count = 0;    /* Number of pages written to journal */
52341# define PAGER_INCR(v)  v++
52342#else
52343# define PAGER_INCR(v)
52344#endif
52345
52346
52347
52348/*
52349** Journal files begin with the following magic string.  The data
52350** was obtained from /dev/random.  It is used only as a sanity check.
52351**
52352** Since version 2.8.0, the journal format contains additional sanity
52353** checking information.  If the power fails while the journal is being
52354** written, semi-random garbage data might appear in the journal
52355** file after power is restored.  If an attempt is then made
52356** to roll the journal back, the database could be corrupted.  The additional
52357** sanity checking data is an attempt to discover the garbage in the
52358** journal and ignore it.
52359**
52360** The sanity checking information for the new journal format consists
52361** of a 32-bit checksum on each page of data.  The checksum covers both
52362** the page number and the pPager->pageSize bytes of data for the page.
52363** This cksum is initialized to a 32-bit random value that appears in the
52364** journal file right after the header.  The random initializer is important,
52365** because garbage data that appears at the end of a journal is likely
52366** data that was once in other files that have now been deleted.  If the
52367** garbage data came from an obsolete journal file, the checksums might
52368** be correct.  But by initializing the checksum to random value which
52369** is different for every journal, we minimize that risk.
52370*/
52371static const unsigned char aJournalMagic[] = {
52372  0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
52373};
52374
52375/*
52376** The size of the of each page record in the journal is given by
52377** the following macro.
52378*/
52379#define JOURNAL_PG_SZ(pPager)  ((pPager->pageSize) + 8)
52380
52381/*
52382** The journal header size for this pager. This is usually the same
52383** size as a single disk sector. See also setSectorSize().
52384*/
52385#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
52386
52387/*
52388** The macro MEMDB is true if we are dealing with an in-memory database.
52389** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
52390** the value of MEMDB will be a constant and the compiler will optimize
52391** out code that would never execute.
52392*/
52393#ifdef SQLITE_OMIT_MEMORYDB
52394# define MEMDB 0
52395#else
52396# define MEMDB pPager->memDb
52397#endif
52398
52399/*
52400** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
52401** interfaces to access the database using memory-mapped I/O.
52402*/
52403#if SQLITE_MAX_MMAP_SIZE>0
52404# define USEFETCH(x) ((x)->bUseFetch)
52405#else
52406# define USEFETCH(x) 0
52407#endif
52408
52409/*
52410** The maximum legal page number is (2^31 - 1).
52411*/
52412#define PAGER_MAX_PGNO 2147483647
52413
52414/*
52415** The argument to this macro is a file descriptor (type sqlite3_file*).
52416** Return 0 if it is not open, or non-zero (but not 1) if it is.
52417**
52418** This is so that expressions can be written as:
52419**
52420**   if( isOpen(pPager->jfd) ){ ...
52421**
52422** instead of
52423**
52424**   if( pPager->jfd->pMethods ){ ...
52425*/
52426#define isOpen(pFd) ((pFd)->pMethods!=0)
52427
52428#ifdef SQLITE_DIRECT_OVERFLOW_READ
52429/*
52430** Return true if page pgno can be read directly from the database file
52431** by the b-tree layer. This is the case if:
52432**
52433**   * the database file is open,
52434**   * there are no dirty pages in the cache, and
52435**   * the desired page is not currently in the wal file.
52436*/
52437SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
52438  if( pPager->fd->pMethods==0 ) return 0;
52439  if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
52440#ifndef SQLITE_OMIT_WAL
52441  if( pPager->pWal ){
52442    u32 iRead = 0;
52443    int rc;
52444    rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
52445    return (rc==SQLITE_OK && iRead==0);
52446  }
52447#endif
52448  return 1;
52449}
52450#endif
52451
52452#ifndef SQLITE_OMIT_WAL
52453# define pagerUseWal(x) ((x)->pWal!=0)
52454#else
52455# define pagerUseWal(x) 0
52456# define pagerRollbackWal(x) 0
52457# define pagerWalFrames(v,w,x,y) 0
52458# define pagerOpenWalIfPresent(z) SQLITE_OK
52459# define pagerBeginReadTransaction(z) SQLITE_OK
52460#endif
52461
52462#ifndef NDEBUG
52463/*
52464** Usage:
52465**
52466**   assert( assert_pager_state(pPager) );
52467**
52468** This function runs many asserts to try to find inconsistencies in
52469** the internal state of the Pager object.
52470*/
52471static int assert_pager_state(Pager *p){
52472  Pager *pPager = p;
52473
52474  /* State must be valid. */
52475  assert( p->eState==PAGER_OPEN
52476       || p->eState==PAGER_READER
52477       || p->eState==PAGER_WRITER_LOCKED
52478       || p->eState==PAGER_WRITER_CACHEMOD
52479       || p->eState==PAGER_WRITER_DBMOD
52480       || p->eState==PAGER_WRITER_FINISHED
52481       || p->eState==PAGER_ERROR
52482  );
52483
52484  /* Regardless of the current state, a temp-file connection always behaves
52485  ** as if it has an exclusive lock on the database file. It never updates
52486  ** the change-counter field, so the changeCountDone flag is always set.
52487  */
52488  assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
52489  assert( p->tempFile==0 || pPager->changeCountDone );
52490
52491  /* If the useJournal flag is clear, the journal-mode must be "OFF".
52492  ** And if the journal-mode is "OFF", the journal file must not be open.
52493  */
52494  assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
52495  assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
52496
52497  /* Check that MEMDB implies noSync. And an in-memory journal. Since
52498  ** this means an in-memory pager performs no IO at all, it cannot encounter
52499  ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
52500  ** a journal file. (although the in-memory journal implementation may
52501  ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
52502  ** is therefore not possible for an in-memory pager to enter the ERROR
52503  ** state.
52504  */
52505  if( MEMDB ){
52506    assert( !isOpen(p->fd) );
52507    assert( p->noSync );
52508    assert( p->journalMode==PAGER_JOURNALMODE_OFF
52509         || p->journalMode==PAGER_JOURNALMODE_MEMORY
52510    );
52511    assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
52512    assert( pagerUseWal(p)==0 );
52513  }
52514
52515  /* If changeCountDone is set, a RESERVED lock or greater must be held
52516  ** on the file.
52517  */
52518  assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
52519  assert( p->eLock!=PENDING_LOCK );
52520
52521  switch( p->eState ){
52522    case PAGER_OPEN:
52523      assert( !MEMDB );
52524      assert( pPager->errCode==SQLITE_OK );
52525      assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
52526      break;
52527
52528    case PAGER_READER:
52529      assert( pPager->errCode==SQLITE_OK );
52530      assert( p->eLock!=UNKNOWN_LOCK );
52531      assert( p->eLock>=SHARED_LOCK );
52532      break;
52533
52534    case PAGER_WRITER_LOCKED:
52535      assert( p->eLock!=UNKNOWN_LOCK );
52536      assert( pPager->errCode==SQLITE_OK );
52537      if( !pagerUseWal(pPager) ){
52538        assert( p->eLock>=RESERVED_LOCK );
52539      }
52540      assert( pPager->dbSize==pPager->dbOrigSize );
52541      assert( pPager->dbOrigSize==pPager->dbFileSize );
52542      assert( pPager->dbOrigSize==pPager->dbHintSize );
52543      assert( pPager->setMaster==0 );
52544      break;
52545
52546    case PAGER_WRITER_CACHEMOD:
52547      assert( p->eLock!=UNKNOWN_LOCK );
52548      assert( pPager->errCode==SQLITE_OK );
52549      if( !pagerUseWal(pPager) ){
52550        /* It is possible that if journal_mode=wal here that neither the
52551        ** journal file nor the WAL file are open. This happens during
52552        ** a rollback transaction that switches from journal_mode=off
52553        ** to journal_mode=wal.
52554        */
52555        assert( p->eLock>=RESERVED_LOCK );
52556        assert( isOpen(p->jfd)
52557             || p->journalMode==PAGER_JOURNALMODE_OFF
52558             || p->journalMode==PAGER_JOURNALMODE_WAL
52559        );
52560      }
52561      assert( pPager->dbOrigSize==pPager->dbFileSize );
52562      assert( pPager->dbOrigSize==pPager->dbHintSize );
52563      break;
52564
52565    case PAGER_WRITER_DBMOD:
52566      assert( p->eLock==EXCLUSIVE_LOCK );
52567      assert( pPager->errCode==SQLITE_OK );
52568      assert( !pagerUseWal(pPager) );
52569      assert( p->eLock>=EXCLUSIVE_LOCK );
52570      assert( isOpen(p->jfd)
52571           || p->journalMode==PAGER_JOURNALMODE_OFF
52572           || p->journalMode==PAGER_JOURNALMODE_WAL
52573           || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
52574      );
52575      assert( pPager->dbOrigSize<=pPager->dbHintSize );
52576      break;
52577
52578    case PAGER_WRITER_FINISHED:
52579      assert( p->eLock==EXCLUSIVE_LOCK );
52580      assert( pPager->errCode==SQLITE_OK );
52581      assert( !pagerUseWal(pPager) );
52582      assert( isOpen(p->jfd)
52583           || p->journalMode==PAGER_JOURNALMODE_OFF
52584           || p->journalMode==PAGER_JOURNALMODE_WAL
52585           || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
52586      );
52587      break;
52588
52589    case PAGER_ERROR:
52590      /* There must be at least one outstanding reference to the pager if
52591      ** in ERROR state. Otherwise the pager should have already dropped
52592      ** back to OPEN state.
52593      */
52594      assert( pPager->errCode!=SQLITE_OK );
52595      assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
52596      break;
52597  }
52598
52599  return 1;
52600}
52601#endif /* ifndef NDEBUG */
52602
52603#ifdef SQLITE_DEBUG
52604/*
52605** Return a pointer to a human readable string in a static buffer
52606** containing the state of the Pager object passed as an argument. This
52607** is intended to be used within debuggers. For example, as an alternative
52608** to "print *pPager" in gdb:
52609**
52610** (gdb) printf "%s", print_pager_state(pPager)
52611**
52612** This routine has external linkage in order to suppress compiler warnings
52613** about an unused function.  It is enclosed within SQLITE_DEBUG and so does
52614** not appear in normal builds.
52615*/
52616char *print_pager_state(Pager *p){
52617  static char zRet[1024];
52618
52619  sqlite3_snprintf(1024, zRet,
52620      "Filename:      %s\n"
52621      "State:         %s errCode=%d\n"
52622      "Lock:          %s\n"
52623      "Locking mode:  locking_mode=%s\n"
52624      "Journal mode:  journal_mode=%s\n"
52625      "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
52626      "Journal:       journalOff=%lld journalHdr=%lld\n"
52627      "Size:          dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
52628      , p->zFilename
52629      , p->eState==PAGER_OPEN            ? "OPEN" :
52630        p->eState==PAGER_READER          ? "READER" :
52631        p->eState==PAGER_WRITER_LOCKED   ? "WRITER_LOCKED" :
52632        p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
52633        p->eState==PAGER_WRITER_DBMOD    ? "WRITER_DBMOD" :
52634        p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
52635        p->eState==PAGER_ERROR           ? "ERROR" : "?error?"
52636      , (int)p->errCode
52637      , p->eLock==NO_LOCK         ? "NO_LOCK" :
52638        p->eLock==RESERVED_LOCK   ? "RESERVED" :
52639        p->eLock==EXCLUSIVE_LOCK  ? "EXCLUSIVE" :
52640        p->eLock==SHARED_LOCK     ? "SHARED" :
52641        p->eLock==UNKNOWN_LOCK    ? "UNKNOWN" : "?error?"
52642      , p->exclusiveMode ? "exclusive" : "normal"
52643      , p->journalMode==PAGER_JOURNALMODE_MEMORY   ? "memory" :
52644        p->journalMode==PAGER_JOURNALMODE_OFF      ? "off" :
52645        p->journalMode==PAGER_JOURNALMODE_DELETE   ? "delete" :
52646        p->journalMode==PAGER_JOURNALMODE_PERSIST  ? "persist" :
52647        p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
52648        p->journalMode==PAGER_JOURNALMODE_WAL      ? "wal" : "?error?"
52649      , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
52650      , p->journalOff, p->journalHdr
52651      , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
52652  );
52653
52654  return zRet;
52655}
52656#endif
52657
52658/* Forward references to the various page getters */
52659static int getPageNormal(Pager*,Pgno,DbPage**,int);
52660static int getPageError(Pager*,Pgno,DbPage**,int);
52661#if SQLITE_MAX_MMAP_SIZE>0
52662static int getPageMMap(Pager*,Pgno,DbPage**,int);
52663#endif
52664
52665/*
52666** Set the Pager.xGet method for the appropriate routine used to fetch
52667** content from the pager.
52668*/
52669static void setGetterMethod(Pager *pPager){
52670  if( pPager->errCode ){
52671    pPager->xGet = getPageError;
52672#if SQLITE_MAX_MMAP_SIZE>0
52673  }else if( USEFETCH(pPager) ){
52674    pPager->xGet = getPageMMap;
52675#endif /* SQLITE_MAX_MMAP_SIZE>0 */
52676  }else{
52677    pPager->xGet = getPageNormal;
52678  }
52679}
52680
52681/*
52682** Return true if it is necessary to write page *pPg into the sub-journal.
52683** A page needs to be written into the sub-journal if there exists one
52684** or more open savepoints for which:
52685**
52686**   * The page-number is less than or equal to PagerSavepoint.nOrig, and
52687**   * The bit corresponding to the page-number is not set in
52688**     PagerSavepoint.pInSavepoint.
52689*/
52690static int subjRequiresPage(PgHdr *pPg){
52691  Pager *pPager = pPg->pPager;
52692  PagerSavepoint *p;
52693  Pgno pgno = pPg->pgno;
52694  int i;
52695  for(i=0; i<pPager->nSavepoint; i++){
52696    p = &pPager->aSavepoint[i];
52697    if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
52698      return 1;
52699    }
52700  }
52701  return 0;
52702}
52703
52704#ifdef SQLITE_DEBUG
52705/*
52706** Return true if the page is already in the journal file.
52707*/
52708static int pageInJournal(Pager *pPager, PgHdr *pPg){
52709  return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
52710}
52711#endif
52712
52713/*
52714** Read a 32-bit integer from the given file descriptor.  Store the integer
52715** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
52716** error code is something goes wrong.
52717**
52718** All values are stored on disk as big-endian.
52719*/
52720static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
52721  unsigned char ac[4];
52722  int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
52723  if( rc==SQLITE_OK ){
52724    *pRes = sqlite3Get4byte(ac);
52725  }
52726  return rc;
52727}
52728
52729/*
52730** Write a 32-bit integer into a string buffer in big-endian byte order.
52731*/
52732#define put32bits(A,B)  sqlite3Put4byte((u8*)A,B)
52733
52734
52735/*
52736** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
52737** on success or an error code is something goes wrong.
52738*/
52739static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
52740  char ac[4];
52741  put32bits(ac, val);
52742  return sqlite3OsWrite(fd, ac, 4, offset);
52743}
52744
52745/*
52746** Unlock the database file to level eLock, which must be either NO_LOCK
52747** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
52748** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
52749**
52750** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
52751** called, do not modify it. See the comment above the #define of
52752** UNKNOWN_LOCK for an explanation of this.
52753*/
52754static int pagerUnlockDb(Pager *pPager, int eLock){
52755  int rc = SQLITE_OK;
52756
52757  assert( !pPager->exclusiveMode || pPager->eLock==eLock );
52758  assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
52759  assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
52760  if( isOpen(pPager->fd) ){
52761    assert( pPager->eLock>=eLock );
52762    rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
52763    if( pPager->eLock!=UNKNOWN_LOCK ){
52764      pPager->eLock = (u8)eLock;
52765    }
52766    IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
52767  }
52768  pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
52769  return rc;
52770}
52771
52772/*
52773** Lock the database file to level eLock, which must be either SHARED_LOCK,
52774** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
52775** Pager.eLock variable to the new locking state.
52776**
52777** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
52778** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
52779** See the comment above the #define of UNKNOWN_LOCK for an explanation
52780** of this.
52781*/
52782static int pagerLockDb(Pager *pPager, int eLock){
52783  int rc = SQLITE_OK;
52784
52785  assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
52786  if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
52787    rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
52788    if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
52789      pPager->eLock = (u8)eLock;
52790      IOTRACE(("LOCK %p %d\n", pPager, eLock))
52791    }
52792  }
52793  return rc;
52794}
52795
52796/*
52797** This function determines whether or not the atomic-write or
52798** atomic-batch-write optimizations can be used with this pager. The
52799** atomic-write optimization can be used if:
52800**
52801**  (a) the value returned by OsDeviceCharacteristics() indicates that
52802**      a database page may be written atomically, and
52803**  (b) the value returned by OsSectorSize() is less than or equal
52804**      to the page size.
52805**
52806** If it can be used, then the value returned is the size of the journal
52807** file when it contains rollback data for exactly one page.
52808**
52809** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
52810** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
52811** returned in this case.
52812**
52813** If neither optimization can be used, 0 is returned.
52814*/
52815static int jrnlBufferSize(Pager *pPager){
52816  assert( !MEMDB );
52817
52818#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
52819 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
52820  int dc;                           /* Device characteristics */
52821
52822  assert( isOpen(pPager->fd) );
52823  dc = sqlite3OsDeviceCharacteristics(pPager->fd);
52824#else
52825  UNUSED_PARAMETER(pPager);
52826#endif
52827
52828#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
52829  if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
52830    return -1;
52831  }
52832#endif
52833
52834#ifdef SQLITE_ENABLE_ATOMIC_WRITE
52835  {
52836    int nSector = pPager->sectorSize;
52837    int szPage = pPager->pageSize;
52838
52839    assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
52840    assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
52841    if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
52842      return 0;
52843    }
52844  }
52845
52846  return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
52847#endif
52848
52849  return 0;
52850}
52851
52852/*
52853** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
52854** on the cache using a hash function.  This is used for testing
52855** and debugging only.
52856*/
52857#ifdef SQLITE_CHECK_PAGES
52858/*
52859** Return a 32-bit hash of the page data for pPage.
52860*/
52861static u32 pager_datahash(int nByte, unsigned char *pData){
52862  u32 hash = 0;
52863  int i;
52864  for(i=0; i<nByte; i++){
52865    hash = (hash*1039) + pData[i];
52866  }
52867  return hash;
52868}
52869static u32 pager_pagehash(PgHdr *pPage){
52870  return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
52871}
52872static void pager_set_pagehash(PgHdr *pPage){
52873  pPage->pageHash = pager_pagehash(pPage);
52874}
52875
52876/*
52877** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
52878** is defined, and NDEBUG is not defined, an assert() statement checks
52879** that the page is either dirty or still matches the calculated page-hash.
52880*/
52881#define CHECK_PAGE(x) checkPage(x)
52882static void checkPage(PgHdr *pPg){
52883  Pager *pPager = pPg->pPager;
52884  assert( pPager->eState!=PAGER_ERROR );
52885  assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
52886}
52887
52888#else
52889#define pager_datahash(X,Y)  0
52890#define pager_pagehash(X)  0
52891#define pager_set_pagehash(X)
52892#define CHECK_PAGE(x)
52893#endif  /* SQLITE_CHECK_PAGES */
52894
52895/*
52896** When this is called the journal file for pager pPager must be open.
52897** This function attempts to read a master journal file name from the
52898** end of the file and, if successful, copies it into memory supplied
52899** by the caller. See comments above writeMasterJournal() for the format
52900** used to store a master journal file name at the end of a journal file.
52901**
52902** zMaster must point to a buffer of at least nMaster bytes allocated by
52903** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
52904** enough space to write the master journal name). If the master journal
52905** name in the journal is longer than nMaster bytes (including a
52906** nul-terminator), then this is handled as if no master journal name
52907** were present in the journal.
52908**
52909** If a master journal file name is present at the end of the journal
52910** file, then it is copied into the buffer pointed to by zMaster. A
52911** nul-terminator byte is appended to the buffer following the master
52912** journal file name.
52913**
52914** If it is determined that no master journal file name is present
52915** zMaster[0] is set to 0 and SQLITE_OK returned.
52916**
52917** If an error occurs while reading from the journal file, an SQLite
52918** error code is returned.
52919*/
52920static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
52921  int rc;                    /* Return code */
52922  u32 len;                   /* Length in bytes of master journal name */
52923  i64 szJ;                   /* Total size in bytes of journal file pJrnl */
52924  u32 cksum;                 /* MJ checksum value read from journal */
52925  u32 u;                     /* Unsigned loop counter */
52926  unsigned char aMagic[8];   /* A buffer to hold the magic header */
52927  zMaster[0] = '\0';
52928
52929  if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
52930   || szJ<16
52931   || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
52932   || len>=nMaster
52933   || len>szJ-16
52934   || len==0
52935   || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
52936   || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
52937   || memcmp(aMagic, aJournalMagic, 8)
52938   || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
52939  ){
52940    return rc;
52941  }
52942
52943  /* See if the checksum matches the master journal name */
52944  for(u=0; u<len; u++){
52945    cksum -= zMaster[u];
52946  }
52947  if( cksum ){
52948    /* If the checksum doesn't add up, then one or more of the disk sectors
52949    ** containing the master journal filename is corrupted. This means
52950    ** definitely roll back, so just return SQLITE_OK and report a (nul)
52951    ** master-journal filename.
52952    */
52953    len = 0;
52954  }
52955  zMaster[len] = '\0';
52956  zMaster[len+1] = '\0';
52957
52958  return SQLITE_OK;
52959}
52960
52961/*
52962** Return the offset of the sector boundary at or immediately
52963** following the value in pPager->journalOff, assuming a sector
52964** size of pPager->sectorSize bytes.
52965**
52966** i.e for a sector size of 512:
52967**
52968**   Pager.journalOff          Return value
52969**   ---------------------------------------
52970**   0                         0
52971**   512                       512
52972**   100                       512
52973**   2000                      2048
52974**
52975*/
52976static i64 journalHdrOffset(Pager *pPager){
52977  i64 offset = 0;
52978  i64 c = pPager->journalOff;
52979  if( c ){
52980    offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
52981  }
52982  assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
52983  assert( offset>=c );
52984  assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
52985  return offset;
52986}
52987
52988/*
52989** The journal file must be open when this function is called.
52990**
52991** This function is a no-op if the journal file has not been written to
52992** within the current transaction (i.e. if Pager.journalOff==0).
52993**
52994** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
52995** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
52996** zero the 28-byte header at the start of the journal file. In either case,
52997** if the pager is not in no-sync mode, sync the journal file immediately
52998** after writing or truncating it.
52999**
53000** If Pager.journalSizeLimit is set to a positive, non-zero value, and
53001** following the truncation or zeroing described above the size of the
53002** journal file in bytes is larger than this value, then truncate the
53003** journal file to Pager.journalSizeLimit bytes. The journal file does
53004** not need to be synced following this operation.
53005**
53006** If an IO error occurs, abandon processing and return the IO error code.
53007** Otherwise, return SQLITE_OK.
53008*/
53009static int zeroJournalHdr(Pager *pPager, int doTruncate){
53010  int rc = SQLITE_OK;                               /* Return code */
53011  assert( isOpen(pPager->jfd) );
53012  assert( !sqlite3JournalIsInMemory(pPager->jfd) );
53013  if( pPager->journalOff ){
53014    const i64 iLimit = pPager->journalSizeLimit;    /* Local cache of jsl */
53015
53016    IOTRACE(("JZEROHDR %p\n", pPager))
53017    if( doTruncate || iLimit==0 ){
53018      rc = sqlite3OsTruncate(pPager->jfd, 0);
53019    }else{
53020      static const char zeroHdr[28] = {0};
53021      rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
53022    }
53023    if( rc==SQLITE_OK && !pPager->noSync ){
53024      rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
53025    }
53026
53027    /* At this point the transaction is committed but the write lock
53028    ** is still held on the file. If there is a size limit configured for
53029    ** the persistent journal and the journal file currently consumes more
53030    ** space than that limit allows for, truncate it now. There is no need
53031    ** to sync the file following this operation.
53032    */
53033    if( rc==SQLITE_OK && iLimit>0 ){
53034      i64 sz;
53035      rc = sqlite3OsFileSize(pPager->jfd, &sz);
53036      if( rc==SQLITE_OK && sz>iLimit ){
53037        rc = sqlite3OsTruncate(pPager->jfd, iLimit);
53038      }
53039    }
53040  }
53041  return rc;
53042}
53043
53044/*
53045** The journal file must be open when this routine is called. A journal
53046** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
53047** current location.
53048**
53049** The format for the journal header is as follows:
53050** - 8 bytes: Magic identifying journal format.
53051** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
53052** - 4 bytes: Random number used for page hash.
53053** - 4 bytes: Initial database page count.
53054** - 4 bytes: Sector size used by the process that wrote this journal.
53055** - 4 bytes: Database page size.
53056**
53057** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
53058*/
53059static int writeJournalHdr(Pager *pPager){
53060  int rc = SQLITE_OK;                 /* Return code */
53061  char *zHeader = pPager->pTmpSpace;  /* Temporary space used to build header */
53062  u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
53063  u32 nWrite;                         /* Bytes of header sector written */
53064  int ii;                             /* Loop counter */
53065
53066  assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
53067
53068  if( nHeader>JOURNAL_HDR_SZ(pPager) ){
53069    nHeader = JOURNAL_HDR_SZ(pPager);
53070  }
53071
53072  /* If there are active savepoints and any of them were created
53073  ** since the most recent journal header was written, update the
53074  ** PagerSavepoint.iHdrOffset fields now.
53075  */
53076  for(ii=0; ii<pPager->nSavepoint; ii++){
53077    if( pPager->aSavepoint[ii].iHdrOffset==0 ){
53078      pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
53079    }
53080  }
53081
53082  pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
53083
53084  /*
53085  ** Write the nRec Field - the number of page records that follow this
53086  ** journal header. Normally, zero is written to this value at this time.
53087  ** After the records are added to the journal (and the journal synced,
53088  ** if in full-sync mode), the zero is overwritten with the true number
53089  ** of records (see syncJournal()).
53090  **
53091  ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
53092  ** reading the journal this value tells SQLite to assume that the
53093  ** rest of the journal file contains valid page records. This assumption
53094  ** is dangerous, as if a failure occurred whilst writing to the journal
53095  ** file it may contain some garbage data. There are two scenarios
53096  ** where this risk can be ignored:
53097  **
53098  **   * When the pager is in no-sync mode. Corruption can follow a
53099  **     power failure in this case anyway.
53100  **
53101  **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
53102  **     that garbage data is never appended to the journal file.
53103  */
53104  assert( isOpen(pPager->fd) || pPager->noSync );
53105  if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
53106   || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
53107  ){
53108    memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
53109    put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
53110  }else{
53111    memset(zHeader, 0, sizeof(aJournalMagic)+4);
53112  }
53113
53114  /* The random check-hash initializer */
53115  sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
53116  put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
53117  /* The initial database size */
53118  put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
53119  /* The assumed sector size for this process */
53120  put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
53121
53122  /* The page size */
53123  put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
53124
53125  /* Initializing the tail of the buffer is not necessary.  Everything
53126  ** works find if the following memset() is omitted.  But initializing
53127  ** the memory prevents valgrind from complaining, so we are willing to
53128  ** take the performance hit.
53129  */
53130  memset(&zHeader[sizeof(aJournalMagic)+20], 0,
53131         nHeader-(sizeof(aJournalMagic)+20));
53132
53133  /* In theory, it is only necessary to write the 28 bytes that the
53134  ** journal header consumes to the journal file here. Then increment the
53135  ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
53136  ** record is written to the following sector (leaving a gap in the file
53137  ** that will be implicitly filled in by the OS).
53138  **
53139  ** However it has been discovered that on some systems this pattern can
53140  ** be significantly slower than contiguously writing data to the file,
53141  ** even if that means explicitly writing data to the block of
53142  ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
53143  ** is done.
53144  **
53145  ** The loop is required here in case the sector-size is larger than the
53146  ** database page size. Since the zHeader buffer is only Pager.pageSize
53147  ** bytes in size, more than one call to sqlite3OsWrite() may be required
53148  ** to populate the entire journal header sector.
53149  */
53150  for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
53151    IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
53152    rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
53153    assert( pPager->journalHdr <= pPager->journalOff );
53154    pPager->journalOff += nHeader;
53155  }
53156
53157  return rc;
53158}
53159
53160/*
53161** The journal file must be open when this is called. A journal header file
53162** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
53163** file. The current location in the journal file is given by
53164** pPager->journalOff. See comments above function writeJournalHdr() for
53165** a description of the journal header format.
53166**
53167** If the header is read successfully, *pNRec is set to the number of
53168** page records following this header and *pDbSize is set to the size of the
53169** database before the transaction began, in pages. Also, pPager->cksumInit
53170** is set to the value read from the journal header. SQLITE_OK is returned
53171** in this case.
53172**
53173** If the journal header file appears to be corrupted, SQLITE_DONE is
53174** returned and *pNRec and *PDbSize are undefined.  If JOURNAL_HDR_SZ bytes
53175** cannot be read from the journal file an error code is returned.
53176*/
53177static int readJournalHdr(
53178  Pager *pPager,               /* Pager object */
53179  int isHot,
53180  i64 journalSize,             /* Size of the open journal file in bytes */
53181  u32 *pNRec,                  /* OUT: Value read from the nRec field */
53182  u32 *pDbSize                 /* OUT: Value of original database size field */
53183){
53184  int rc;                      /* Return code */
53185  unsigned char aMagic[8];     /* A buffer to hold the magic header */
53186  i64 iHdrOff;                 /* Offset of journal header being read */
53187
53188  assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
53189
53190  /* Advance Pager.journalOff to the start of the next sector. If the
53191  ** journal file is too small for there to be a header stored at this
53192  ** point, return SQLITE_DONE.
53193  */
53194  pPager->journalOff = journalHdrOffset(pPager);
53195  if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
53196    return SQLITE_DONE;
53197  }
53198  iHdrOff = pPager->journalOff;
53199
53200  /* Read in the first 8 bytes of the journal header. If they do not match
53201  ** the  magic string found at the start of each journal header, return
53202  ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
53203  ** proceed.
53204  */
53205  if( isHot || iHdrOff!=pPager->journalHdr ){
53206    rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
53207    if( rc ){
53208      return rc;
53209    }
53210    if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
53211      return SQLITE_DONE;
53212    }
53213  }
53214
53215  /* Read the first three 32-bit fields of the journal header: The nRec
53216  ** field, the checksum-initializer and the database size at the start
53217  ** of the transaction. Return an error code if anything goes wrong.
53218  */
53219  if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
53220   || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
53221   || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
53222  ){
53223    return rc;
53224  }
53225
53226  if( pPager->journalOff==0 ){
53227    u32 iPageSize;               /* Page-size field of journal header */
53228    u32 iSectorSize;             /* Sector-size field of journal header */
53229
53230    /* Read the page-size and sector-size journal header fields. */
53231    if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
53232     || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
53233    ){
53234      return rc;
53235    }
53236
53237    /* Versions of SQLite prior to 3.5.8 set the page-size field of the
53238    ** journal header to zero. In this case, assume that the Pager.pageSize
53239    ** variable is already set to the correct page size.
53240    */
53241    if( iPageSize==0 ){
53242      iPageSize = pPager->pageSize;
53243    }
53244
53245    /* Check that the values read from the page-size and sector-size fields
53246    ** are within range. To be 'in range', both values need to be a power
53247    ** of two greater than or equal to 512 or 32, and not greater than their
53248    ** respective compile time maximum limits.
53249    */
53250    if( iPageSize<512                  || iSectorSize<32
53251     || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
53252     || ((iPageSize-1)&iPageSize)!=0   || ((iSectorSize-1)&iSectorSize)!=0
53253    ){
53254      /* If the either the page-size or sector-size in the journal-header is
53255      ** invalid, then the process that wrote the journal-header must have
53256      ** crashed before the header was synced. In this case stop reading
53257      ** the journal file here.
53258      */
53259      return SQLITE_DONE;
53260    }
53261
53262    /* Update the page-size to match the value read from the journal.
53263    ** Use a testcase() macro to make sure that malloc failure within
53264    ** PagerSetPagesize() is tested.
53265    */
53266    rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
53267    testcase( rc!=SQLITE_OK );
53268
53269    /* Update the assumed sector-size to match the value used by
53270    ** the process that created this journal. If this journal was
53271    ** created by a process other than this one, then this routine
53272    ** is being called from within pager_playback(). The local value
53273    ** of Pager.sectorSize is restored at the end of that routine.
53274    */
53275    pPager->sectorSize = iSectorSize;
53276  }
53277
53278  pPager->journalOff += JOURNAL_HDR_SZ(pPager);
53279  return rc;
53280}
53281
53282
53283/*
53284** Write the supplied master journal name into the journal file for pager
53285** pPager at the current location. The master journal name must be the last
53286** thing written to a journal file. If the pager is in full-sync mode, the
53287** journal file descriptor is advanced to the next sector boundary before
53288** anything is written. The format is:
53289**
53290**   + 4 bytes: PAGER_MJ_PGNO.
53291**   + N bytes: Master journal filename in utf-8.
53292**   + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
53293**   + 4 bytes: Master journal name checksum.
53294**   + 8 bytes: aJournalMagic[].
53295**
53296** The master journal page checksum is the sum of the bytes in the master
53297** journal name, where each byte is interpreted as a signed 8-bit integer.
53298**
53299** If zMaster is a NULL pointer (occurs for a single database transaction),
53300** this call is a no-op.
53301*/
53302static int writeMasterJournal(Pager *pPager, const char *zMaster){
53303  int rc;                          /* Return code */
53304  int nMaster;                     /* Length of string zMaster */
53305  i64 iHdrOff;                     /* Offset of header in journal file */
53306  i64 jrnlSize;                    /* Size of journal file on disk */
53307  u32 cksum = 0;                   /* Checksum of string zMaster */
53308
53309  assert( pPager->setMaster==0 );
53310  assert( !pagerUseWal(pPager) );
53311
53312  if( !zMaster
53313   || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
53314   || !isOpen(pPager->jfd)
53315  ){
53316    return SQLITE_OK;
53317  }
53318  pPager->setMaster = 1;
53319  assert( pPager->journalHdr <= pPager->journalOff );
53320
53321  /* Calculate the length in bytes and the checksum of zMaster */
53322  for(nMaster=0; zMaster[nMaster]; nMaster++){
53323    cksum += zMaster[nMaster];
53324  }
53325
53326  /* If in full-sync mode, advance to the next disk sector before writing
53327  ** the master journal name. This is in case the previous page written to
53328  ** the journal has already been synced.
53329  */
53330  if( pPager->fullSync ){
53331    pPager->journalOff = journalHdrOffset(pPager);
53332  }
53333  iHdrOff = pPager->journalOff;
53334
53335  /* Write the master journal data to the end of the journal file. If
53336  ** an error occurs, return the error code to the caller.
53337  */
53338  if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
53339   || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
53340   || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
53341   || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
53342   || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
53343                                 iHdrOff+4+nMaster+8)))
53344  ){
53345    return rc;
53346  }
53347  pPager->journalOff += (nMaster+20);
53348
53349  /* If the pager is in peristent-journal mode, then the physical
53350  ** journal-file may extend past the end of the master-journal name
53351  ** and 8 bytes of magic data just written to the file. This is
53352  ** dangerous because the code to rollback a hot-journal file
53353  ** will not be able to find the master-journal name to determine
53354  ** whether or not the journal is hot.
53355  **
53356  ** Easiest thing to do in this scenario is to truncate the journal
53357  ** file to the required size.
53358  */
53359  if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
53360   && jrnlSize>pPager->journalOff
53361  ){
53362    rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
53363  }
53364  return rc;
53365}
53366
53367/*
53368** Discard the entire contents of the in-memory page-cache.
53369*/
53370static void pager_reset(Pager *pPager){
53371  pPager->iDataVersion++;
53372  sqlite3BackupRestart(pPager->pBackup);
53373  sqlite3PcacheClear(pPager->pPCache);
53374}
53375
53376/*
53377** Return the pPager->iDataVersion value
53378*/
53379SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager *pPager){
53380  return pPager->iDataVersion;
53381}
53382
53383/*
53384** Free all structures in the Pager.aSavepoint[] array and set both
53385** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
53386** if it is open and the pager is not in exclusive mode.
53387*/
53388static void releaseAllSavepoints(Pager *pPager){
53389  int ii;               /* Iterator for looping through Pager.aSavepoint */
53390  for(ii=0; ii<pPager->nSavepoint; ii++){
53391    sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
53392  }
53393  if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
53394    sqlite3OsClose(pPager->sjfd);
53395  }
53396  sqlite3_free(pPager->aSavepoint);
53397  pPager->aSavepoint = 0;
53398  pPager->nSavepoint = 0;
53399  pPager->nSubRec = 0;
53400}
53401
53402/*
53403** Set the bit number pgno in the PagerSavepoint.pInSavepoint
53404** bitvecs of all open savepoints. Return SQLITE_OK if successful
53405** or SQLITE_NOMEM if a malloc failure occurs.
53406*/
53407static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
53408  int ii;                   /* Loop counter */
53409  int rc = SQLITE_OK;       /* Result code */
53410
53411  for(ii=0; ii<pPager->nSavepoint; ii++){
53412    PagerSavepoint *p = &pPager->aSavepoint[ii];
53413    if( pgno<=p->nOrig ){
53414      rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
53415      testcase( rc==SQLITE_NOMEM );
53416      assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
53417    }
53418  }
53419  return rc;
53420}
53421
53422/*
53423** This function is a no-op if the pager is in exclusive mode and not
53424** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
53425** state.
53426**
53427** If the pager is not in exclusive-access mode, the database file is
53428** completely unlocked. If the file is unlocked and the file-system does
53429** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
53430** closed (if it is open).
53431**
53432** If the pager is in ERROR state when this function is called, the
53433** contents of the pager cache are discarded before switching back to
53434** the OPEN state. Regardless of whether the pager is in exclusive-mode
53435** or not, any journal file left in the file-system will be treated
53436** as a hot-journal and rolled back the next time a read-transaction
53437** is opened (by this or by any other connection).
53438*/
53439static void pager_unlock(Pager *pPager){
53440
53441  assert( pPager->eState==PAGER_READER
53442       || pPager->eState==PAGER_OPEN
53443       || pPager->eState==PAGER_ERROR
53444  );
53445
53446  sqlite3BitvecDestroy(pPager->pInJournal);
53447  pPager->pInJournal = 0;
53448  releaseAllSavepoints(pPager);
53449
53450  if( pagerUseWal(pPager) ){
53451    assert( !isOpen(pPager->jfd) );
53452    sqlite3WalEndReadTransaction(pPager->pWal);
53453    pPager->eState = PAGER_OPEN;
53454  }else if( !pPager->exclusiveMode ){
53455    int rc;                       /* Error code returned by pagerUnlockDb() */
53456    int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
53457
53458    /* If the operating system support deletion of open files, then
53459    ** close the journal file when dropping the database lock.  Otherwise
53460    ** another connection with journal_mode=delete might delete the file
53461    ** out from under us.
53462    */
53463    assert( (PAGER_JOURNALMODE_MEMORY   & 5)!=1 );
53464    assert( (PAGER_JOURNALMODE_OFF      & 5)!=1 );
53465    assert( (PAGER_JOURNALMODE_WAL      & 5)!=1 );
53466    assert( (PAGER_JOURNALMODE_DELETE   & 5)!=1 );
53467    assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
53468    assert( (PAGER_JOURNALMODE_PERSIST  & 5)==1 );
53469    if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
53470     || 1!=(pPager->journalMode & 5)
53471    ){
53472      sqlite3OsClose(pPager->jfd);
53473    }
53474
53475    /* If the pager is in the ERROR state and the call to unlock the database
53476    ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
53477    ** above the #define for UNKNOWN_LOCK for an explanation of why this
53478    ** is necessary.
53479    */
53480    rc = pagerUnlockDb(pPager, NO_LOCK);
53481    if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
53482      pPager->eLock = UNKNOWN_LOCK;
53483    }
53484
53485    /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
53486    ** without clearing the error code. This is intentional - the error
53487    ** code is cleared and the cache reset in the block below.
53488    */
53489    assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
53490    pPager->eState = PAGER_OPEN;
53491  }
53492
53493  /* If Pager.errCode is set, the contents of the pager cache cannot be
53494  ** trusted. Now that there are no outstanding references to the pager,
53495  ** it can safely move back to PAGER_OPEN state. This happens in both
53496  ** normal and exclusive-locking mode.
53497  */
53498  assert( pPager->errCode==SQLITE_OK || !MEMDB );
53499  if( pPager->errCode ){
53500    if( pPager->tempFile==0 ){
53501      pager_reset(pPager);
53502      pPager->changeCountDone = 0;
53503      pPager->eState = PAGER_OPEN;
53504    }else{
53505      pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
53506    }
53507    if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
53508    pPager->errCode = SQLITE_OK;
53509    setGetterMethod(pPager);
53510  }
53511
53512  pPager->journalOff = 0;
53513  pPager->journalHdr = 0;
53514  pPager->setMaster = 0;
53515}
53516
53517/*
53518** This function is called whenever an IOERR or FULL error that requires
53519** the pager to transition into the ERROR state may ahve occurred.
53520** The first argument is a pointer to the pager structure, the second
53521** the error-code about to be returned by a pager API function. The
53522** value returned is a copy of the second argument to this function.
53523**
53524** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
53525** IOERR sub-codes, the pager enters the ERROR state and the error code
53526** is stored in Pager.errCode. While the pager remains in the ERROR state,
53527** all major API calls on the Pager will immediately return Pager.errCode.
53528**
53529** The ERROR state indicates that the contents of the pager-cache
53530** cannot be trusted. This state can be cleared by completely discarding
53531** the contents of the pager-cache. If a transaction was active when
53532** the persistent error occurred, then the rollback journal may need
53533** to be replayed to restore the contents of the database file (as if
53534** it were a hot-journal).
53535*/
53536static int pager_error(Pager *pPager, int rc){
53537  int rc2 = rc & 0xff;
53538  assert( rc==SQLITE_OK || !MEMDB );
53539  assert(
53540       pPager->errCode==SQLITE_FULL ||
53541       pPager->errCode==SQLITE_OK ||
53542       (pPager->errCode & 0xff)==SQLITE_IOERR
53543  );
53544  if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
53545    pPager->errCode = rc;
53546    pPager->eState = PAGER_ERROR;
53547    setGetterMethod(pPager);
53548  }
53549  return rc;
53550}
53551
53552static int pager_truncate(Pager *pPager, Pgno nPage);
53553
53554/*
53555** The write transaction open on pPager is being committed (bCommit==1)
53556** or rolled back (bCommit==0).
53557**
53558** Return TRUE if and only if all dirty pages should be flushed to disk.
53559**
53560** Rules:
53561**
53562**   *  For non-TEMP databases, always sync to disk.  This is necessary
53563**      for transactions to be durable.
53564**
53565**   *  Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing
53566**      file has been created already (via a spill on pagerStress()) and
53567**      when the number of dirty pages in memory exceeds 25% of the total
53568**      cache size.
53569*/
53570static int pagerFlushOnCommit(Pager *pPager, int bCommit){
53571  if( pPager->tempFile==0 ) return 1;
53572  if( !bCommit ) return 0;
53573  if( !isOpen(pPager->fd) ) return 0;
53574  return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
53575}
53576
53577/*
53578** This routine ends a transaction. A transaction is usually ended by
53579** either a COMMIT or a ROLLBACK operation. This routine may be called
53580** after rollback of a hot-journal, or if an error occurs while opening
53581** the journal file or writing the very first journal-header of a
53582** database transaction.
53583**
53584** This routine is never called in PAGER_ERROR state. If it is called
53585** in PAGER_NONE or PAGER_SHARED state and the lock held is less
53586** exclusive than a RESERVED lock, it is a no-op.
53587**
53588** Otherwise, any active savepoints are released.
53589**
53590** If the journal file is open, then it is "finalized". Once a journal
53591** file has been finalized it is not possible to use it to roll back a
53592** transaction. Nor will it be considered to be a hot-journal by this
53593** or any other database connection. Exactly how a journal is finalized
53594** depends on whether or not the pager is running in exclusive mode and
53595** the current journal-mode (Pager.journalMode value), as follows:
53596**
53597**   journalMode==MEMORY
53598**     Journal file descriptor is simply closed. This destroys an
53599**     in-memory journal.
53600**
53601**   journalMode==TRUNCATE
53602**     Journal file is truncated to zero bytes in size.
53603**
53604**   journalMode==PERSIST
53605**     The first 28 bytes of the journal file are zeroed. This invalidates
53606**     the first journal header in the file, and hence the entire journal
53607**     file. An invalid journal file cannot be rolled back.
53608**
53609**   journalMode==DELETE
53610**     The journal file is closed and deleted using sqlite3OsDelete().
53611**
53612**     If the pager is running in exclusive mode, this method of finalizing
53613**     the journal file is never used. Instead, if the journalMode is
53614**     DELETE and the pager is in exclusive mode, the method described under
53615**     journalMode==PERSIST is used instead.
53616**
53617** After the journal is finalized, the pager moves to PAGER_READER state.
53618** If running in non-exclusive rollback mode, the lock on the file is
53619** downgraded to a SHARED_LOCK.
53620**
53621** SQLITE_OK is returned if no error occurs. If an error occurs during
53622** any of the IO operations to finalize the journal file or unlock the
53623** database then the IO error code is returned to the user. If the
53624** operation to finalize the journal file fails, then the code still
53625** tries to unlock the database file if not in exclusive mode. If the
53626** unlock operation fails as well, then the first error code related
53627** to the first error encountered (the journal finalization one) is
53628** returned.
53629*/
53630static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){
53631  int rc = SQLITE_OK;      /* Error code from journal finalization operation */
53632  int rc2 = SQLITE_OK;     /* Error code from db file unlock operation */
53633
53634  /* Do nothing if the pager does not have an open write transaction
53635  ** or at least a RESERVED lock. This function may be called when there
53636  ** is no write-transaction active but a RESERVED or greater lock is
53637  ** held under two circumstances:
53638  **
53639  **   1. After a successful hot-journal rollback, it is called with
53640  **      eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
53641  **
53642  **   2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
53643  **      lock switches back to locking_mode=normal and then executes a
53644  **      read-transaction, this function is called with eState==PAGER_READER
53645  **      and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
53646  */
53647  assert( assert_pager_state(pPager) );
53648  assert( pPager->eState!=PAGER_ERROR );
53649  if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
53650    return SQLITE_OK;
53651  }
53652
53653  releaseAllSavepoints(pPager);
53654  assert( isOpen(pPager->jfd) || pPager->pInJournal==0
53655      || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
53656  );
53657  if( isOpen(pPager->jfd) ){
53658    assert( !pagerUseWal(pPager) );
53659
53660    /* Finalize the journal file. */
53661    if( sqlite3JournalIsInMemory(pPager->jfd) ){
53662      /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
53663      sqlite3OsClose(pPager->jfd);
53664    }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
53665      if( pPager->journalOff==0 ){
53666        rc = SQLITE_OK;
53667      }else{
53668        rc = sqlite3OsTruncate(pPager->jfd, 0);
53669        if( rc==SQLITE_OK && pPager->fullSync ){
53670          /* Make sure the new file size is written into the inode right away.
53671          ** Otherwise the journal might resurrect following a power loss and
53672          ** cause the last transaction to roll back.  See
53673          ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773
53674          */
53675          rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
53676        }
53677      }
53678      pPager->journalOff = 0;
53679    }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
53680      || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
53681    ){
53682      rc = zeroJournalHdr(pPager, hasMaster||pPager->tempFile);
53683      pPager->journalOff = 0;
53684    }else{
53685      /* This branch may be executed with Pager.journalMode==MEMORY if
53686      ** a hot-journal was just rolled back. In this case the journal
53687      ** file should be closed and deleted. If this connection writes to
53688      ** the database file, it will do so using an in-memory journal.
53689      */
53690      int bDelete = !pPager->tempFile;
53691      assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
53692      assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
53693           || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
53694           || pPager->journalMode==PAGER_JOURNALMODE_WAL
53695      );
53696      sqlite3OsClose(pPager->jfd);
53697      if( bDelete ){
53698        rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
53699      }
53700    }
53701  }
53702
53703#ifdef SQLITE_CHECK_PAGES
53704  sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
53705  if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
53706    PgHdr *p = sqlite3PagerLookup(pPager, 1);
53707    if( p ){
53708      p->pageHash = 0;
53709      sqlite3PagerUnrefNotNull(p);
53710    }
53711  }
53712#endif
53713
53714  sqlite3BitvecDestroy(pPager->pInJournal);
53715  pPager->pInJournal = 0;
53716  pPager->nRec = 0;
53717  if( rc==SQLITE_OK ){
53718    if( MEMDB || pagerFlushOnCommit(pPager, bCommit) ){
53719      sqlite3PcacheCleanAll(pPager->pPCache);
53720    }else{
53721      sqlite3PcacheClearWritable(pPager->pPCache);
53722    }
53723    sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
53724  }
53725
53726  if( pagerUseWal(pPager) ){
53727    /* Drop the WAL write-lock, if any. Also, if the connection was in
53728    ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
53729    ** lock held on the database file.
53730    */
53731    rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
53732    assert( rc2==SQLITE_OK );
53733  }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
53734    /* This branch is taken when committing a transaction in rollback-journal
53735    ** mode if the database file on disk is larger than the database image.
53736    ** At this point the journal has been finalized and the transaction
53737    ** successfully committed, but the EXCLUSIVE lock is still held on the
53738    ** file. So it is safe to truncate the database file to its minimum
53739    ** required size.  */
53740    assert( pPager->eLock==EXCLUSIVE_LOCK );
53741    rc = pager_truncate(pPager, pPager->dbSize);
53742  }
53743
53744  if( rc==SQLITE_OK && bCommit ){
53745    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
53746    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
53747  }
53748
53749  if( !pPager->exclusiveMode
53750   && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
53751  ){
53752    rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
53753  }
53754  pPager->eState = PAGER_READER;
53755  pPager->setMaster = 0;
53756
53757  return (rc==SQLITE_OK?rc2:rc);
53758}
53759
53760/*
53761** Execute a rollback if a transaction is active and unlock the
53762** database file.
53763**
53764** If the pager has already entered the ERROR state, do not attempt
53765** the rollback at this time. Instead, pager_unlock() is called. The
53766** call to pager_unlock() will discard all in-memory pages, unlock
53767** the database file and move the pager back to OPEN state. If this
53768** means that there is a hot-journal left in the file-system, the next
53769** connection to obtain a shared lock on the pager (which may be this one)
53770** will roll it back.
53771**
53772** If the pager has not already entered the ERROR state, but an IO or
53773** malloc error occurs during a rollback, then this will itself cause
53774** the pager to enter the ERROR state. Which will be cleared by the
53775** call to pager_unlock(), as described above.
53776*/
53777static void pagerUnlockAndRollback(Pager *pPager){
53778  if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
53779    assert( assert_pager_state(pPager) );
53780    if( pPager->eState>=PAGER_WRITER_LOCKED ){
53781      sqlite3BeginBenignMalloc();
53782      sqlite3PagerRollback(pPager);
53783      sqlite3EndBenignMalloc();
53784    }else if( !pPager->exclusiveMode ){
53785      assert( pPager->eState==PAGER_READER );
53786      pager_end_transaction(pPager, 0, 0);
53787    }
53788  }
53789  pager_unlock(pPager);
53790}
53791
53792/*
53793** Parameter aData must point to a buffer of pPager->pageSize bytes
53794** of data. Compute and return a checksum based ont the contents of the
53795** page of data and the current value of pPager->cksumInit.
53796**
53797** This is not a real checksum. It is really just the sum of the
53798** random initial value (pPager->cksumInit) and every 200th byte
53799** of the page data, starting with byte offset (pPager->pageSize%200).
53800** Each byte is interpreted as an 8-bit unsigned integer.
53801**
53802** Changing the formula used to compute this checksum results in an
53803** incompatible journal file format.
53804**
53805** If journal corruption occurs due to a power failure, the most likely
53806** scenario is that one end or the other of the record will be changed.
53807** It is much less likely that the two ends of the journal record will be
53808** correct and the middle be corrupt.  Thus, this "checksum" scheme,
53809** though fast and simple, catches the mostly likely kind of corruption.
53810*/
53811static u32 pager_cksum(Pager *pPager, const u8 *aData){
53812  u32 cksum = pPager->cksumInit;         /* Checksum value to return */
53813  int i = pPager->pageSize-200;          /* Loop counter */
53814  while( i>0 ){
53815    cksum += aData[i];
53816    i -= 200;
53817  }
53818  return cksum;
53819}
53820
53821/*
53822** Read a single page from either the journal file (if isMainJrnl==1) or
53823** from the sub-journal (if isMainJrnl==0) and playback that page.
53824** The page begins at offset *pOffset into the file. The *pOffset
53825** value is increased to the start of the next page in the journal.
53826**
53827** The main rollback journal uses checksums - the statement journal does
53828** not.
53829**
53830** If the page number of the page record read from the (sub-)journal file
53831** is greater than the current value of Pager.dbSize, then playback is
53832** skipped and SQLITE_OK is returned.
53833**
53834** If pDone is not NULL, then it is a record of pages that have already
53835** been played back.  If the page at *pOffset has already been played back
53836** (if the corresponding pDone bit is set) then skip the playback.
53837** Make sure the pDone bit corresponding to the *pOffset page is set
53838** prior to returning.
53839**
53840** If the page record is successfully read from the (sub-)journal file
53841** and played back, then SQLITE_OK is returned. If an IO error occurs
53842** while reading the record from the (sub-)journal file or while writing
53843** to the database file, then the IO error code is returned. If data
53844** is successfully read from the (sub-)journal file but appears to be
53845** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
53846** two circumstances:
53847**
53848**   * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
53849**   * If the record is being rolled back from the main journal file
53850**     and the checksum field does not match the record content.
53851**
53852** Neither of these two scenarios are possible during a savepoint rollback.
53853**
53854** If this is a savepoint rollback, then memory may have to be dynamically
53855** allocated by this function. If this is the case and an allocation fails,
53856** SQLITE_NOMEM is returned.
53857*/
53858static int pager_playback_one_page(
53859  Pager *pPager,                /* The pager being played back */
53860  i64 *pOffset,                 /* Offset of record to playback */
53861  Bitvec *pDone,                /* Bitvec of pages already played back */
53862  int isMainJrnl,               /* 1 -> main journal. 0 -> sub-journal. */
53863  int isSavepnt                 /* True for a savepoint rollback */
53864){
53865  int rc;
53866  PgHdr *pPg;                   /* An existing page in the cache */
53867  Pgno pgno;                    /* The page number of a page in journal */
53868  u32 cksum;                    /* Checksum used for sanity checking */
53869  char *aData;                  /* Temporary storage for the page */
53870  sqlite3_file *jfd;            /* The file descriptor for the journal file */
53871  int isSynced;                 /* True if journal page is synced */
53872
53873  assert( (isMainJrnl&~1)==0 );      /* isMainJrnl is 0 or 1 */
53874  assert( (isSavepnt&~1)==0 );       /* isSavepnt is 0 or 1 */
53875  assert( isMainJrnl || pDone );     /* pDone always used on sub-journals */
53876  assert( isSavepnt || pDone==0 );   /* pDone never used on non-savepoint */
53877
53878  aData = pPager->pTmpSpace;
53879  assert( aData );         /* Temp storage must have already been allocated */
53880  assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
53881
53882  /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
53883  ** or savepoint rollback done at the request of the caller) or this is
53884  ** a hot-journal rollback. If it is a hot-journal rollback, the pager
53885  ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
53886  ** only reads from the main journal, not the sub-journal.
53887  */
53888  assert( pPager->eState>=PAGER_WRITER_CACHEMOD
53889       || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
53890  );
53891  assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
53892
53893  /* Read the page number and page data from the journal or sub-journal
53894  ** file. Return an error code to the caller if an IO error occurs.
53895  */
53896  jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
53897  rc = read32bits(jfd, *pOffset, &pgno);
53898  if( rc!=SQLITE_OK ) return rc;
53899  rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
53900  if( rc!=SQLITE_OK ) return rc;
53901  *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
53902
53903  /* Sanity checking on the page.  This is more important that I originally
53904  ** thought.  If a power failure occurs while the journal is being written,
53905  ** it could cause invalid data to be written into the journal.  We need to
53906  ** detect this invalid data (with high probability) and ignore it.
53907  */
53908  if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
53909    assert( !isSavepnt );
53910    return SQLITE_DONE;
53911  }
53912  if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
53913    return SQLITE_OK;
53914  }
53915  if( isMainJrnl ){
53916    rc = read32bits(jfd, (*pOffset)-4, &cksum);
53917    if( rc ) return rc;
53918    if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
53919      return SQLITE_DONE;
53920    }
53921  }
53922
53923  /* If this page has already been played back before during the current
53924  ** rollback, then don't bother to play it back again.
53925  */
53926  if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
53927    return rc;
53928  }
53929
53930  /* When playing back page 1, restore the nReserve setting
53931  */
53932  if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
53933    pPager->nReserve = ((u8*)aData)[20];
53934  }
53935
53936  /* If the pager is in CACHEMOD state, then there must be a copy of this
53937  ** page in the pager cache. In this case just update the pager cache,
53938  ** not the database file. The page is left marked dirty in this case.
53939  **
53940  ** An exception to the above rule: If the database is in no-sync mode
53941  ** and a page is moved during an incremental vacuum then the page may
53942  ** not be in the pager cache. Later: if a malloc() or IO error occurs
53943  ** during a Movepage() call, then the page may not be in the cache
53944  ** either. So the condition described in the above paragraph is not
53945  ** assert()able.
53946  **
53947  ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
53948  ** pager cache if it exists and the main file. The page is then marked
53949  ** not dirty. Since this code is only executed in PAGER_OPEN state for
53950  ** a hot-journal rollback, it is guaranteed that the page-cache is empty
53951  ** if the pager is in OPEN state.
53952  **
53953  ** Ticket #1171:  The statement journal might contain page content that is
53954  ** different from the page content at the start of the transaction.
53955  ** This occurs when a page is changed prior to the start of a statement
53956  ** then changed again within the statement.  When rolling back such a
53957  ** statement we must not write to the original database unless we know
53958  ** for certain that original page contents are synced into the main rollback
53959  ** journal.  Otherwise, a power loss might leave modified data in the
53960  ** database file without an entry in the rollback journal that can
53961  ** restore the database to its original form.  Two conditions must be
53962  ** met before writing to the database files. (1) the database must be
53963  ** locked.  (2) we know that the original page content is fully synced
53964  ** in the main journal either because the page is not in cache or else
53965  ** the page is marked as needSync==0.
53966  **
53967  ** 2008-04-14:  When attempting to vacuum a corrupt database file, it
53968  ** is possible to fail a statement on a database that does not yet exist.
53969  ** Do not attempt to write if database file has never been opened.
53970  */
53971  if( pagerUseWal(pPager) ){
53972    pPg = 0;
53973  }else{
53974    pPg = sqlite3PagerLookup(pPager, pgno);
53975  }
53976  assert( pPg || !MEMDB );
53977  assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
53978  PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
53979           PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
53980           (isMainJrnl?"main-journal":"sub-journal")
53981  ));
53982  if( isMainJrnl ){
53983    isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
53984  }else{
53985    isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
53986  }
53987  if( isOpen(pPager->fd)
53988   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
53989   && isSynced
53990  ){
53991    i64 ofst = (pgno-1)*(i64)pPager->pageSize;
53992    testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
53993    assert( !pagerUseWal(pPager) );
53994
53995    /* Write the data read from the journal back into the database file.
53996    ** This is usually safe even for an encrypted database - as the data
53997    ** was encrypted before it was written to the journal file. The exception
53998    ** is if the data was just read from an in-memory sub-journal. In that
53999    ** case it must be encrypted here before it is copied into the database
54000    ** file.  */
54001    rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
54002
54003    if( pgno>pPager->dbFileSize ){
54004      pPager->dbFileSize = pgno;
54005    }
54006    if( pPager->pBackup ){
54007      sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
54008    }
54009  }else if( !isMainJrnl && pPg==0 ){
54010    /* If this is a rollback of a savepoint and data was not written to
54011    ** the database and the page is not in-memory, there is a potential
54012    ** problem. When the page is next fetched by the b-tree layer, it
54013    ** will be read from the database file, which may or may not be
54014    ** current.
54015    **
54016    ** There are a couple of different ways this can happen. All are quite
54017    ** obscure. When running in synchronous mode, this can only happen
54018    ** if the page is on the free-list at the start of the transaction, then
54019    ** populated, then moved using sqlite3PagerMovepage().
54020    **
54021    ** The solution is to add an in-memory page to the cache containing
54022    ** the data just read from the sub-journal. Mark the page as dirty
54023    ** and if the pager requires a journal-sync, then mark the page as
54024    ** requiring a journal-sync before it is written.
54025    */
54026    assert( isSavepnt );
54027    assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
54028    pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
54029    rc = sqlite3PagerGet(pPager, pgno, &pPg, 1);
54030    assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
54031    pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
54032    if( rc!=SQLITE_OK ) return rc;
54033    sqlite3PcacheMakeDirty(pPg);
54034  }
54035  if( pPg ){
54036    /* No page should ever be explicitly rolled back that is in use, except
54037    ** for page 1 which is held in use in order to keep the lock on the
54038    ** database active. However such a page may be rolled back as a result
54039    ** of an internal error resulting in an automatic call to
54040    ** sqlite3PagerRollback().
54041    */
54042    void *pData;
54043    pData = pPg->pData;
54044    memcpy(pData, (u8*)aData, pPager->pageSize);
54045    pPager->xReiniter(pPg);
54046    /* It used to be that sqlite3PcacheMakeClean(pPg) was called here.  But
54047    ** that call was dangerous and had no detectable benefit since the cache
54048    ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so
54049    ** has been removed. */
54050    pager_set_pagehash(pPg);
54051
54052    /* If this was page 1, then restore the value of Pager.dbFileVers.
54053    ** Do this before any decoding. */
54054    if( pgno==1 ){
54055      memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
54056    }
54057    sqlite3PcacheRelease(pPg);
54058  }
54059  return rc;
54060}
54061
54062/*
54063** Parameter zMaster is the name of a master journal file. A single journal
54064** file that referred to the master journal file has just been rolled back.
54065** This routine checks if it is possible to delete the master journal file,
54066** and does so if it is.
54067**
54068** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
54069** available for use within this function.
54070**
54071** When a master journal file is created, it is populated with the names
54072** of all of its child journals, one after another, formatted as utf-8
54073** encoded text. The end of each child journal file is marked with a
54074** nul-terminator byte (0x00). i.e. the entire contents of a master journal
54075** file for a transaction involving two databases might be:
54076**
54077**   "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
54078**
54079** A master journal file may only be deleted once all of its child
54080** journals have been rolled back.
54081**
54082** This function reads the contents of the master-journal file into
54083** memory and loops through each of the child journal names. For
54084** each child journal, it checks if:
54085**
54086**   * if the child journal exists, and if so
54087**   * if the child journal contains a reference to master journal
54088**     file zMaster
54089**
54090** If a child journal can be found that matches both of the criteria
54091** above, this function returns without doing anything. Otherwise, if
54092** no such child journal can be found, file zMaster is deleted from
54093** the file-system using sqlite3OsDelete().
54094**
54095** If an IO error within this function, an error code is returned. This
54096** function allocates memory by calling sqlite3Malloc(). If an allocation
54097** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
54098** occur, SQLITE_OK is returned.
54099**
54100** TODO: This function allocates a single block of memory to load
54101** the entire contents of the master journal file. This could be
54102** a couple of kilobytes or so - potentially larger than the page
54103** size.
54104*/
54105static int pager_delmaster(Pager *pPager, const char *zMaster){
54106  sqlite3_vfs *pVfs = pPager->pVfs;
54107  int rc;                   /* Return code */
54108  sqlite3_file *pMaster;    /* Malloc'd master-journal file descriptor */
54109  sqlite3_file *pJournal;   /* Malloc'd child-journal file descriptor */
54110  char *zMasterJournal = 0; /* Contents of master journal file */
54111  i64 nMasterJournal;       /* Size of master journal file */
54112  char *zJournal;           /* Pointer to one journal within MJ file */
54113  char *zMasterPtr;         /* Space to hold MJ filename from a journal file */
54114  int nMasterPtr;           /* Amount of space allocated to zMasterPtr[] */
54115
54116  /* Allocate space for both the pJournal and pMaster file descriptors.
54117  ** If successful, open the master journal file for reading.
54118  */
54119  pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
54120  pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
54121  if( !pMaster ){
54122    rc = SQLITE_NOMEM_BKPT;
54123  }else{
54124    const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
54125    rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
54126  }
54127  if( rc!=SQLITE_OK ) goto delmaster_out;
54128
54129  /* Load the entire master journal file into space obtained from
54130  ** sqlite3_malloc() and pointed to by zMasterJournal.   Also obtain
54131  ** sufficient space (in zMasterPtr) to hold the names of master
54132  ** journal files extracted from regular rollback-journals.
54133  */
54134  rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
54135  if( rc!=SQLITE_OK ) goto delmaster_out;
54136  nMasterPtr = pVfs->mxPathname+1;
54137  zMasterJournal = sqlite3Malloc(nMasterJournal + nMasterPtr + 2);
54138  if( !zMasterJournal ){
54139    rc = SQLITE_NOMEM_BKPT;
54140    goto delmaster_out;
54141  }
54142  zMasterPtr = &zMasterJournal[nMasterJournal+2];
54143  rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
54144  if( rc!=SQLITE_OK ) goto delmaster_out;
54145  zMasterJournal[nMasterJournal] = 0;
54146  zMasterJournal[nMasterJournal+1] = 0;
54147
54148  zJournal = zMasterJournal;
54149  while( (zJournal-zMasterJournal)<nMasterJournal ){
54150    int exists;
54151    rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
54152    if( rc!=SQLITE_OK ){
54153      goto delmaster_out;
54154    }
54155    if( exists ){
54156      /* One of the journals pointed to by the master journal exists.
54157      ** Open it and check if it points at the master journal. If
54158      ** so, return without deleting the master journal file.
54159      ** NB:  zJournal is really a MAIN_JOURNAL.  But call it a
54160      ** MASTER_JOURNAL here so that the VFS will not send the zJournal
54161      ** name into sqlite3_database_file_object().
54162      */
54163      int c;
54164      int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
54165      rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
54166      if( rc!=SQLITE_OK ){
54167        goto delmaster_out;
54168      }
54169
54170      rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
54171      sqlite3OsClose(pJournal);
54172      if( rc!=SQLITE_OK ){
54173        goto delmaster_out;
54174      }
54175
54176      c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
54177      if( c ){
54178        /* We have a match. Do not delete the master journal file. */
54179        goto delmaster_out;
54180      }
54181    }
54182    zJournal += (sqlite3Strlen30(zJournal)+1);
54183  }
54184
54185  sqlite3OsClose(pMaster);
54186  rc = sqlite3OsDelete(pVfs, zMaster, 0);
54187
54188delmaster_out:
54189  sqlite3_free(zMasterJournal);
54190  if( pMaster ){
54191    sqlite3OsClose(pMaster);
54192    assert( !isOpen(pJournal) );
54193    sqlite3_free(pMaster);
54194  }
54195  return rc;
54196}
54197
54198
54199/*
54200** This function is used to change the actual size of the database
54201** file in the file-system. This only happens when committing a transaction,
54202** or rolling back a transaction (including rolling back a hot-journal).
54203**
54204** If the main database file is not open, or the pager is not in either
54205** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
54206** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
54207** If the file on disk is currently larger than nPage pages, then use the VFS
54208** xTruncate() method to truncate it.
54209**
54210** Or, it might be the case that the file on disk is smaller than
54211** nPage pages. Some operating system implementations can get confused if
54212** you try to truncate a file to some size that is larger than it
54213** currently is, so detect this case and write a single zero byte to
54214** the end of the new file instead.
54215**
54216** If successful, return SQLITE_OK. If an IO error occurs while modifying
54217** the database file, return the error code to the caller.
54218*/
54219static int pager_truncate(Pager *pPager, Pgno nPage){
54220  int rc = SQLITE_OK;
54221  assert( pPager->eState!=PAGER_ERROR );
54222  assert( pPager->eState!=PAGER_READER );
54223
54224  if( isOpen(pPager->fd)
54225   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
54226  ){
54227    i64 currentSize, newSize;
54228    int szPage = pPager->pageSize;
54229    assert( pPager->eLock==EXCLUSIVE_LOCK );
54230    /* TODO: Is it safe to use Pager.dbFileSize here? */
54231    rc = sqlite3OsFileSize(pPager->fd, &currentSize);
54232    newSize = szPage*(i64)nPage;
54233    if( rc==SQLITE_OK && currentSize!=newSize ){
54234      if( currentSize>newSize ){
54235        rc = sqlite3OsTruncate(pPager->fd, newSize);
54236      }else if( (currentSize+szPage)<=newSize ){
54237        char *pTmp = pPager->pTmpSpace;
54238        memset(pTmp, 0, szPage);
54239        testcase( (newSize-szPage) == currentSize );
54240        testcase( (newSize-szPage) >  currentSize );
54241        rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
54242      }
54243      if( rc==SQLITE_OK ){
54244        pPager->dbFileSize = nPage;
54245      }
54246    }
54247  }
54248  return rc;
54249}
54250
54251/*
54252** Return a sanitized version of the sector-size of OS file pFile. The
54253** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
54254*/
54255SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){
54256  int iRet = sqlite3OsSectorSize(pFile);
54257  if( iRet<32 ){
54258    iRet = 512;
54259  }else if( iRet>MAX_SECTOR_SIZE ){
54260    assert( MAX_SECTOR_SIZE>=512 );
54261    iRet = MAX_SECTOR_SIZE;
54262  }
54263  return iRet;
54264}
54265
54266/*
54267** Set the value of the Pager.sectorSize variable for the given
54268** pager based on the value returned by the xSectorSize method
54269** of the open database file. The sector size will be used
54270** to determine the size and alignment of journal header and
54271** master journal pointers within created journal files.
54272**
54273** For temporary files the effective sector size is always 512 bytes.
54274**
54275** Otherwise, for non-temporary files, the effective sector size is
54276** the value returned by the xSectorSize() method rounded up to 32 if
54277** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
54278** is greater than MAX_SECTOR_SIZE.
54279**
54280** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
54281** the effective sector size to its minimum value (512).  The purpose of
54282** pPager->sectorSize is to define the "blast radius" of bytes that
54283** might change if a crash occurs while writing to a single byte in
54284** that range.  But with POWERSAFE_OVERWRITE, the blast radius is zero
54285** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
54286** size.  For backwards compatibility of the rollback journal file format,
54287** we cannot reduce the effective sector size below 512.
54288*/
54289static void setSectorSize(Pager *pPager){
54290  assert( isOpen(pPager->fd) || pPager->tempFile );
54291
54292  if( pPager->tempFile
54293   || (sqlite3OsDeviceCharacteristics(pPager->fd) &
54294              SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
54295  ){
54296    /* Sector size doesn't matter for temporary files. Also, the file
54297    ** may not have been opened yet, in which case the OsSectorSize()
54298    ** call will segfault. */
54299    pPager->sectorSize = 512;
54300  }else{
54301    pPager->sectorSize = sqlite3SectorSize(pPager->fd);
54302  }
54303}
54304
54305/*
54306** Playback the journal and thus restore the database file to
54307** the state it was in before we started making changes.
54308**
54309** The journal file format is as follows:
54310**
54311**  (1)  8 byte prefix.  A copy of aJournalMagic[].
54312**  (2)  4 byte big-endian integer which is the number of valid page records
54313**       in the journal.  If this value is 0xffffffff, then compute the
54314**       number of page records from the journal size.
54315**  (3)  4 byte big-endian integer which is the initial value for the
54316**       sanity checksum.
54317**  (4)  4 byte integer which is the number of pages to truncate the
54318**       database to during a rollback.
54319**  (5)  4 byte big-endian integer which is the sector size.  The header
54320**       is this many bytes in size.
54321**  (6)  4 byte big-endian integer which is the page size.
54322**  (7)  zero padding out to the next sector size.
54323**  (8)  Zero or more pages instances, each as follows:
54324**        +  4 byte page number.
54325**        +  pPager->pageSize bytes of data.
54326**        +  4 byte checksum
54327**
54328** When we speak of the journal header, we mean the first 7 items above.
54329** Each entry in the journal is an instance of the 8th item.
54330**
54331** Call the value from the second bullet "nRec".  nRec is the number of
54332** valid page entries in the journal.  In most cases, you can compute the
54333** value of nRec from the size of the journal file.  But if a power
54334** failure occurred while the journal was being written, it could be the
54335** case that the size of the journal file had already been increased but
54336** the extra entries had not yet made it safely to disk.  In such a case,
54337** the value of nRec computed from the file size would be too large.  For
54338** that reason, we always use the nRec value in the header.
54339**
54340** If the nRec value is 0xffffffff it means that nRec should be computed
54341** from the file size.  This value is used when the user selects the
54342** no-sync option for the journal.  A power failure could lead to corruption
54343** in this case.  But for things like temporary table (which will be
54344** deleted when the power is restored) we don't care.
54345**
54346** If the file opened as the journal file is not a well-formed
54347** journal file then all pages up to the first corrupted page are rolled
54348** back (or no pages if the journal header is corrupted). The journal file
54349** is then deleted and SQLITE_OK returned, just as if no corruption had
54350** been encountered.
54351**
54352** If an I/O or malloc() error occurs, the journal-file is not deleted
54353** and an error code is returned.
54354**
54355** The isHot parameter indicates that we are trying to rollback a journal
54356** that might be a hot journal.  Or, it could be that the journal is
54357** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
54358** If the journal really is hot, reset the pager cache prior rolling
54359** back any content.  If the journal is merely persistent, no reset is
54360** needed.
54361*/
54362static int pager_playback(Pager *pPager, int isHot){
54363  sqlite3_vfs *pVfs = pPager->pVfs;
54364  i64 szJ;                 /* Size of the journal file in bytes */
54365  u32 nRec;                /* Number of Records in the journal */
54366  u32 u;                   /* Unsigned loop counter */
54367  Pgno mxPg = 0;           /* Size of the original file in pages */
54368  int rc;                  /* Result code of a subroutine */
54369  int res = 1;             /* Value returned by sqlite3OsAccess() */
54370  char *zMaster = 0;       /* Name of master journal file if any */
54371  int needPagerReset;      /* True to reset page prior to first page rollback */
54372  int nPlayback = 0;       /* Total number of pages restored from journal */
54373  u32 savedPageSize = pPager->pageSize;
54374
54375  /* Figure out how many records are in the journal.  Abort early if
54376  ** the journal is empty.
54377  */
54378  assert( isOpen(pPager->jfd) );
54379  rc = sqlite3OsFileSize(pPager->jfd, &szJ);
54380  if( rc!=SQLITE_OK ){
54381    goto end_playback;
54382  }
54383
54384  /* Read the master journal name from the journal, if it is present.
54385  ** If a master journal file name is specified, but the file is not
54386  ** present on disk, then the journal is not hot and does not need to be
54387  ** played back.
54388  **
54389  ** TODO: Technically the following is an error because it assumes that
54390  ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
54391  ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
54392  ** mxPathname is 512, which is the same as the minimum allowable value
54393  ** for pageSize.
54394  */
54395  zMaster = pPager->pTmpSpace;
54396  rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
54397  if( rc==SQLITE_OK && zMaster[0] ){
54398    rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
54399  }
54400  zMaster = 0;
54401  if( rc!=SQLITE_OK || !res ){
54402    goto end_playback;
54403  }
54404  pPager->journalOff = 0;
54405  needPagerReset = isHot;
54406
54407  /* This loop terminates either when a readJournalHdr() or
54408  ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
54409  ** occurs.
54410  */
54411  while( 1 ){
54412    /* Read the next journal header from the journal file.  If there are
54413    ** not enough bytes left in the journal file for a complete header, or
54414    ** it is corrupted, then a process must have failed while writing it.
54415    ** This indicates nothing more needs to be rolled back.
54416    */
54417    rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
54418    if( rc!=SQLITE_OK ){
54419      if( rc==SQLITE_DONE ){
54420        rc = SQLITE_OK;
54421      }
54422      goto end_playback;
54423    }
54424
54425    /* If nRec is 0xffffffff, then this journal was created by a process
54426    ** working in no-sync mode. This means that the rest of the journal
54427    ** file consists of pages, there are no more journal headers. Compute
54428    ** the value of nRec based on this assumption.
54429    */
54430    if( nRec==0xffffffff ){
54431      assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
54432      nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
54433    }
54434
54435    /* If nRec is 0 and this rollback is of a transaction created by this
54436    ** process and if this is the final header in the journal, then it means
54437    ** that this part of the journal was being filled but has not yet been
54438    ** synced to disk.  Compute the number of pages based on the remaining
54439    ** size of the file.
54440    **
54441    ** The third term of the test was added to fix ticket #2565.
54442    ** When rolling back a hot journal, nRec==0 always means that the next
54443    ** chunk of the journal contains zero pages to be rolled back.  But
54444    ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
54445    ** the journal, it means that the journal might contain additional
54446    ** pages that need to be rolled back and that the number of pages
54447    ** should be computed based on the journal file size.
54448    */
54449    if( nRec==0 && !isHot &&
54450        pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
54451      nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
54452    }
54453
54454    /* If this is the first header read from the journal, truncate the
54455    ** database file back to its original size.
54456    */
54457    if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
54458      rc = pager_truncate(pPager, mxPg);
54459      if( rc!=SQLITE_OK ){
54460        goto end_playback;
54461      }
54462      pPager->dbSize = mxPg;
54463    }
54464
54465    /* Copy original pages out of the journal and back into the
54466    ** database file and/or page cache.
54467    */
54468    for(u=0; u<nRec; u++){
54469      if( needPagerReset ){
54470        pager_reset(pPager);
54471        needPagerReset = 0;
54472      }
54473      rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
54474      if( rc==SQLITE_OK ){
54475        nPlayback++;
54476      }else{
54477        if( rc==SQLITE_DONE ){
54478          pPager->journalOff = szJ;
54479          break;
54480        }else if( rc==SQLITE_IOERR_SHORT_READ ){
54481          /* If the journal has been truncated, simply stop reading and
54482          ** processing the journal. This might happen if the journal was
54483          ** not completely written and synced prior to a crash.  In that
54484          ** case, the database should have never been written in the
54485          ** first place so it is OK to simply abandon the rollback. */
54486          rc = SQLITE_OK;
54487          goto end_playback;
54488        }else{
54489          /* If we are unable to rollback, quit and return the error
54490          ** code.  This will cause the pager to enter the error state
54491          ** so that no further harm will be done.  Perhaps the next
54492          ** process to come along will be able to rollback the database.
54493          */
54494          goto end_playback;
54495        }
54496      }
54497    }
54498  }
54499  /*NOTREACHED*/
54500  assert( 0 );
54501
54502end_playback:
54503  if( rc==SQLITE_OK ){
54504    rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
54505  }
54506  /* Following a rollback, the database file should be back in its original
54507  ** state prior to the start of the transaction, so invoke the
54508  ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
54509  ** assertion that the transaction counter was modified.
54510  */
54511#ifdef SQLITE_DEBUG
54512  sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
54513#endif
54514
54515  /* If this playback is happening automatically as a result of an IO or
54516  ** malloc error that occurred after the change-counter was updated but
54517  ** before the transaction was committed, then the change-counter
54518  ** modification may just have been reverted. If this happens in exclusive
54519  ** mode, then subsequent transactions performed by the connection will not
54520  ** update the change-counter at all. This may lead to cache inconsistency
54521  ** problems for other processes at some point in the future. So, just
54522  ** in case this has happened, clear the changeCountDone flag now.
54523  */
54524  pPager->changeCountDone = pPager->tempFile;
54525
54526  if( rc==SQLITE_OK ){
54527    zMaster = pPager->pTmpSpace;
54528    rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
54529    testcase( rc!=SQLITE_OK );
54530  }
54531  if( rc==SQLITE_OK
54532   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
54533  ){
54534    rc = sqlite3PagerSync(pPager, 0);
54535  }
54536  if( rc==SQLITE_OK ){
54537    rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0);
54538    testcase( rc!=SQLITE_OK );
54539  }
54540  if( rc==SQLITE_OK && zMaster[0] && res ){
54541    /* If there was a master journal and this routine will return success,
54542    ** see if it is possible to delete the master journal.
54543    */
54544    rc = pager_delmaster(pPager, zMaster);
54545    testcase( rc!=SQLITE_OK );
54546  }
54547  if( isHot && nPlayback ){
54548    sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, "recovered %d pages from %s",
54549                nPlayback, pPager->zJournal);
54550  }
54551
54552  /* The Pager.sectorSize variable may have been updated while rolling
54553  ** back a journal created by a process with a different sector size
54554  ** value. Reset it to the correct value for this process.
54555  */
54556  setSectorSize(pPager);
54557  return rc;
54558}
54559
54560
54561/*
54562** Read the content for page pPg out of the database file (or out of
54563** the WAL if that is where the most recent copy if found) into
54564** pPg->pData. A shared lock or greater must be held on the database
54565** file before this function is called.
54566**
54567** If page 1 is read, then the value of Pager.dbFileVers[] is set to
54568** the value read from the database file.
54569**
54570** If an IO error occurs, then the IO error is returned to the caller.
54571** Otherwise, SQLITE_OK is returned.
54572*/
54573static int readDbPage(PgHdr *pPg){
54574  Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
54575  int rc = SQLITE_OK;          /* Return code */
54576
54577#ifndef SQLITE_OMIT_WAL
54578  u32 iFrame = 0;              /* Frame of WAL containing pgno */
54579
54580  assert( pPager->eState>=PAGER_READER && !MEMDB );
54581  assert( isOpen(pPager->fd) );
54582
54583  if( pagerUseWal(pPager) ){
54584    rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
54585    if( rc ) return rc;
54586  }
54587  if( iFrame ){
54588    rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
54589  }else
54590#endif
54591  {
54592    i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
54593    rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
54594    if( rc==SQLITE_IOERR_SHORT_READ ){
54595      rc = SQLITE_OK;
54596    }
54597  }
54598
54599  if( pPg->pgno==1 ){
54600    if( rc ){
54601      /* If the read is unsuccessful, set the dbFileVers[] to something
54602      ** that will never be a valid file version.  dbFileVers[] is a copy
54603      ** of bytes 24..39 of the database.  Bytes 28..31 should always be
54604      ** zero or the size of the database in page. Bytes 32..35 and 35..39
54605      ** should be page numbers which are never 0xffffffff.  So filling
54606      ** pPager->dbFileVers[] with all 0xff bytes should suffice.
54607      **
54608      ** For an encrypted database, the situation is more complex:  bytes
54609      ** 24..39 of the database are white noise.  But the probability of
54610      ** white noise equaling 16 bytes of 0xff is vanishingly small so
54611      ** we should still be ok.
54612      */
54613      memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
54614    }else{
54615      u8 *dbFileVers = &((u8*)pPg->pData)[24];
54616      memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
54617    }
54618  }
54619  PAGER_INCR(sqlite3_pager_readdb_count);
54620  PAGER_INCR(pPager->nRead);
54621  IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
54622  PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
54623               PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
54624
54625  return rc;
54626}
54627
54628/*
54629** Update the value of the change-counter at offsets 24 and 92 in
54630** the header and the sqlite version number at offset 96.
54631**
54632** This is an unconditional update.  See also the pager_incr_changecounter()
54633** routine which only updates the change-counter if the update is actually
54634** needed, as determined by the pPager->changeCountDone state variable.
54635*/
54636static void pager_write_changecounter(PgHdr *pPg){
54637  u32 change_counter;
54638
54639  /* Increment the value just read and write it back to byte 24. */
54640  change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
54641  put32bits(((char*)pPg->pData)+24, change_counter);
54642
54643  /* Also store the SQLite version number in bytes 96..99 and in
54644  ** bytes 92..95 store the change counter for which the version number
54645  ** is valid. */
54646  put32bits(((char*)pPg->pData)+92, change_counter);
54647  put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
54648}
54649
54650#ifndef SQLITE_OMIT_WAL
54651/*
54652** This function is invoked once for each page that has already been
54653** written into the log file when a WAL transaction is rolled back.
54654** Parameter iPg is the page number of said page. The pCtx argument
54655** is actually a pointer to the Pager structure.
54656**
54657** If page iPg is present in the cache, and has no outstanding references,
54658** it is discarded. Otherwise, if there are one or more outstanding
54659** references, the page content is reloaded from the database. If the
54660** attempt to reload content from the database is required and fails,
54661** return an SQLite error code. Otherwise, SQLITE_OK.
54662*/
54663static int pagerUndoCallback(void *pCtx, Pgno iPg){
54664  int rc = SQLITE_OK;
54665  Pager *pPager = (Pager *)pCtx;
54666  PgHdr *pPg;
54667
54668  assert( pagerUseWal(pPager) );
54669  pPg = sqlite3PagerLookup(pPager, iPg);
54670  if( pPg ){
54671    if( sqlite3PcachePageRefcount(pPg)==1 ){
54672      sqlite3PcacheDrop(pPg);
54673    }else{
54674      rc = readDbPage(pPg);
54675      if( rc==SQLITE_OK ){
54676        pPager->xReiniter(pPg);
54677      }
54678      sqlite3PagerUnrefNotNull(pPg);
54679    }
54680  }
54681
54682  /* Normally, if a transaction is rolled back, any backup processes are
54683  ** updated as data is copied out of the rollback journal and into the
54684  ** database. This is not generally possible with a WAL database, as
54685  ** rollback involves simply truncating the log file. Therefore, if one
54686  ** or more frames have already been written to the log (and therefore
54687  ** also copied into the backup databases) as part of this transaction,
54688  ** the backups must be restarted.
54689  */
54690  sqlite3BackupRestart(pPager->pBackup);
54691
54692  return rc;
54693}
54694
54695/*
54696** This function is called to rollback a transaction on a WAL database.
54697*/
54698static int pagerRollbackWal(Pager *pPager){
54699  int rc;                         /* Return Code */
54700  PgHdr *pList;                   /* List of dirty pages to revert */
54701
54702  /* For all pages in the cache that are currently dirty or have already
54703  ** been written (but not committed) to the log file, do one of the
54704  ** following:
54705  **
54706  **   + Discard the cached page (if refcount==0), or
54707  **   + Reload page content from the database (if refcount>0).
54708  */
54709  pPager->dbSize = pPager->dbOrigSize;
54710  rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
54711  pList = sqlite3PcacheDirtyList(pPager->pPCache);
54712  while( pList && rc==SQLITE_OK ){
54713    PgHdr *pNext = pList->pDirty;
54714    rc = pagerUndoCallback((void *)pPager, pList->pgno);
54715    pList = pNext;
54716  }
54717
54718  return rc;
54719}
54720
54721/*
54722** This function is a wrapper around sqlite3WalFrames(). As well as logging
54723** the contents of the list of pages headed by pList (connected by pDirty),
54724** this function notifies any active backup processes that the pages have
54725** changed.
54726**
54727** The list of pages passed into this routine is always sorted by page number.
54728** Hence, if page 1 appears anywhere on the list, it will be the first page.
54729*/
54730static int pagerWalFrames(
54731  Pager *pPager,                  /* Pager object */
54732  PgHdr *pList,                   /* List of frames to log */
54733  Pgno nTruncate,                 /* Database size after this commit */
54734  int isCommit                    /* True if this is a commit */
54735){
54736  int rc;                         /* Return code */
54737  int nList;                      /* Number of pages in pList */
54738  PgHdr *p;                       /* For looping over pages */
54739
54740  assert( pPager->pWal );
54741  assert( pList );
54742#ifdef SQLITE_DEBUG
54743  /* Verify that the page list is in accending order */
54744  for(p=pList; p && p->pDirty; p=p->pDirty){
54745    assert( p->pgno < p->pDirty->pgno );
54746  }
54747#endif
54748
54749  assert( pList->pDirty==0 || isCommit );
54750  if( isCommit ){
54751    /* If a WAL transaction is being committed, there is no point in writing
54752    ** any pages with page numbers greater than nTruncate into the WAL file.
54753    ** They will never be read by any client. So remove them from the pDirty
54754    ** list here. */
54755    PgHdr **ppNext = &pList;
54756    nList = 0;
54757    for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
54758      if( p->pgno<=nTruncate ){
54759        ppNext = &p->pDirty;
54760        nList++;
54761      }
54762    }
54763    assert( pList );
54764  }else{
54765    nList = 1;
54766  }
54767  pPager->aStat[PAGER_STAT_WRITE] += nList;
54768
54769  if( pList->pgno==1 ) pager_write_changecounter(pList);
54770  rc = sqlite3WalFrames(pPager->pWal,
54771      pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
54772  );
54773  if( rc==SQLITE_OK && pPager->pBackup ){
54774    for(p=pList; p; p=p->pDirty){
54775      sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
54776    }
54777  }
54778
54779#ifdef SQLITE_CHECK_PAGES
54780  pList = sqlite3PcacheDirtyList(pPager->pPCache);
54781  for(p=pList; p; p=p->pDirty){
54782    pager_set_pagehash(p);
54783  }
54784#endif
54785
54786  return rc;
54787}
54788
54789/*
54790** Begin a read transaction on the WAL.
54791**
54792** This routine used to be called "pagerOpenSnapshot()" because it essentially
54793** makes a snapshot of the database at the current point in time and preserves
54794** that snapshot for use by the reader in spite of concurrently changes by
54795** other writers or checkpointers.
54796*/
54797static int pagerBeginReadTransaction(Pager *pPager){
54798  int rc;                         /* Return code */
54799  int changed = 0;                /* True if cache must be reset */
54800
54801  assert( pagerUseWal(pPager) );
54802  assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
54803
54804  /* sqlite3WalEndReadTransaction() was not called for the previous
54805  ** transaction in locking_mode=EXCLUSIVE.  So call it now.  If we
54806  ** are in locking_mode=NORMAL and EndRead() was previously called,
54807  ** the duplicate call is harmless.
54808  */
54809  sqlite3WalEndReadTransaction(pPager->pWal);
54810
54811  rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
54812  if( rc!=SQLITE_OK || changed ){
54813    pager_reset(pPager);
54814    if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
54815  }
54816
54817  return rc;
54818}
54819#endif
54820
54821/*
54822** This function is called as part of the transition from PAGER_OPEN
54823** to PAGER_READER state to determine the size of the database file
54824** in pages (assuming the page size currently stored in Pager.pageSize).
54825**
54826** If no error occurs, SQLITE_OK is returned and the size of the database
54827** in pages is stored in *pnPage. Otherwise, an error code (perhaps
54828** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
54829*/
54830static int pagerPagecount(Pager *pPager, Pgno *pnPage){
54831  Pgno nPage;                     /* Value to return via *pnPage */
54832
54833  /* Query the WAL sub-system for the database size. The WalDbsize()
54834  ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
54835  ** if the database size is not available. The database size is not
54836  ** available from the WAL sub-system if the log file is empty or
54837  ** contains no valid committed transactions.
54838  */
54839  assert( pPager->eState==PAGER_OPEN );
54840  assert( pPager->eLock>=SHARED_LOCK );
54841  assert( isOpen(pPager->fd) );
54842  assert( pPager->tempFile==0 );
54843  nPage = sqlite3WalDbsize(pPager->pWal);
54844
54845  /* If the number of pages in the database is not available from the
54846  ** WAL sub-system, determine the page count based on the size of
54847  ** the database file.  If the size of the database file is not an
54848  ** integer multiple of the page-size, round up the result.
54849  */
54850  if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
54851    i64 n = 0;                    /* Size of db file in bytes */
54852    int rc = sqlite3OsFileSize(pPager->fd, &n);
54853    if( rc!=SQLITE_OK ){
54854      return rc;
54855    }
54856    nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
54857  }
54858
54859  /* If the current number of pages in the file is greater than the
54860  ** configured maximum pager number, increase the allowed limit so
54861  ** that the file can be read.
54862  */
54863  if( nPage>pPager->mxPgno ){
54864    pPager->mxPgno = (Pgno)nPage;
54865  }
54866
54867  *pnPage = nPage;
54868  return SQLITE_OK;
54869}
54870
54871#ifndef SQLITE_OMIT_WAL
54872/*
54873** Check if the *-wal file that corresponds to the database opened by pPager
54874** exists if the database is not empy, or verify that the *-wal file does
54875** not exist (by deleting it) if the database file is empty.
54876**
54877** If the database is not empty and the *-wal file exists, open the pager
54878** in WAL mode.  If the database is empty or if no *-wal file exists and
54879** if no error occurs, make sure Pager.journalMode is not set to
54880** PAGER_JOURNALMODE_WAL.
54881**
54882** Return SQLITE_OK or an error code.
54883**
54884** The caller must hold a SHARED lock on the database file to call this
54885** function. Because an EXCLUSIVE lock on the db file is required to delete
54886** a WAL on a none-empty database, this ensures there is no race condition
54887** between the xAccess() below and an xDelete() being executed by some
54888** other connection.
54889*/
54890static int pagerOpenWalIfPresent(Pager *pPager){
54891  int rc = SQLITE_OK;
54892  assert( pPager->eState==PAGER_OPEN );
54893  assert( pPager->eLock>=SHARED_LOCK );
54894
54895  if( !pPager->tempFile ){
54896    int isWal;                    /* True if WAL file exists */
54897    rc = sqlite3OsAccess(
54898        pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
54899    );
54900    if( rc==SQLITE_OK ){
54901      if( isWal ){
54902        Pgno nPage;                   /* Size of the database file */
54903
54904        rc = pagerPagecount(pPager, &nPage);
54905        if( rc ) return rc;
54906        if( nPage==0 ){
54907          rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
54908        }else{
54909          testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
54910          rc = sqlite3PagerOpenWal(pPager, 0);
54911        }
54912      }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
54913        pPager->journalMode = PAGER_JOURNALMODE_DELETE;
54914      }
54915    }
54916  }
54917  return rc;
54918}
54919#endif
54920
54921/*
54922** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
54923** the entire master journal file. The case pSavepoint==NULL occurs when
54924** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
54925** savepoint.
54926**
54927** When pSavepoint is not NULL (meaning a non-transaction savepoint is
54928** being rolled back), then the rollback consists of up to three stages,
54929** performed in the order specified:
54930**
54931**   * Pages are played back from the main journal starting at byte
54932**     offset PagerSavepoint.iOffset and continuing to
54933**     PagerSavepoint.iHdrOffset, or to the end of the main journal
54934**     file if PagerSavepoint.iHdrOffset is zero.
54935**
54936**   * If PagerSavepoint.iHdrOffset is not zero, then pages are played
54937**     back starting from the journal header immediately following
54938**     PagerSavepoint.iHdrOffset to the end of the main journal file.
54939**
54940**   * Pages are then played back from the sub-journal file, starting
54941**     with the PagerSavepoint.iSubRec and continuing to the end of
54942**     the journal file.
54943**
54944** Throughout the rollback process, each time a page is rolled back, the
54945** corresponding bit is set in a bitvec structure (variable pDone in the
54946** implementation below). This is used to ensure that a page is only
54947** rolled back the first time it is encountered in either journal.
54948**
54949** If pSavepoint is NULL, then pages are only played back from the main
54950** journal file. There is no need for a bitvec in this case.
54951**
54952** In either case, before playback commences the Pager.dbSize variable
54953** is reset to the value that it held at the start of the savepoint
54954** (or transaction). No page with a page-number greater than this value
54955** is played back. If one is encountered it is simply skipped.
54956*/
54957static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
54958  i64 szJ;                 /* Effective size of the main journal */
54959  i64 iHdrOff;             /* End of first segment of main-journal records */
54960  int rc = SQLITE_OK;      /* Return code */
54961  Bitvec *pDone = 0;       /* Bitvec to ensure pages played back only once */
54962
54963  assert( pPager->eState!=PAGER_ERROR );
54964  assert( pPager->eState>=PAGER_WRITER_LOCKED );
54965
54966  /* Allocate a bitvec to use to store the set of pages rolled back */
54967  if( pSavepoint ){
54968    pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
54969    if( !pDone ){
54970      return SQLITE_NOMEM_BKPT;
54971    }
54972  }
54973
54974  /* Set the database size back to the value it was before the savepoint
54975  ** being reverted was opened.
54976  */
54977  pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
54978  pPager->changeCountDone = pPager->tempFile;
54979
54980  if( !pSavepoint && pagerUseWal(pPager) ){
54981    return pagerRollbackWal(pPager);
54982  }
54983
54984  /* Use pPager->journalOff as the effective size of the main rollback
54985  ** journal.  The actual file might be larger than this in
54986  ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST.  But anything
54987  ** past pPager->journalOff is off-limits to us.
54988  */
54989  szJ = pPager->journalOff;
54990  assert( pagerUseWal(pPager)==0 || szJ==0 );
54991
54992  /* Begin by rolling back records from the main journal starting at
54993  ** PagerSavepoint.iOffset and continuing to the next journal header.
54994  ** There might be records in the main journal that have a page number
54995  ** greater than the current database size (pPager->dbSize) but those
54996  ** will be skipped automatically.  Pages are added to pDone as they
54997  ** are played back.
54998  */
54999  if( pSavepoint && !pagerUseWal(pPager) ){
55000    iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
55001    pPager->journalOff = pSavepoint->iOffset;
55002    while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
55003      rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
55004    }
55005    assert( rc!=SQLITE_DONE );
55006  }else{
55007    pPager->journalOff = 0;
55008  }
55009
55010  /* Continue rolling back records out of the main journal starting at
55011  ** the first journal header seen and continuing until the effective end
55012  ** of the main journal file.  Continue to skip out-of-range pages and
55013  ** continue adding pages rolled back to pDone.
55014  */
55015  while( rc==SQLITE_OK && pPager->journalOff<szJ ){
55016    u32 ii;            /* Loop counter */
55017    u32 nJRec = 0;     /* Number of Journal Records */
55018    u32 dummy;
55019    rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
55020    assert( rc!=SQLITE_DONE );
55021
55022    /*
55023    ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
55024    ** test is related to ticket #2565.  See the discussion in the
55025    ** pager_playback() function for additional information.
55026    */
55027    if( nJRec==0
55028     && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
55029    ){
55030      nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
55031    }
55032    for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
55033      rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
55034    }
55035    assert( rc!=SQLITE_DONE );
55036  }
55037  assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
55038
55039  /* Finally,  rollback pages from the sub-journal.  Page that were
55040  ** previously rolled back out of the main journal (and are hence in pDone)
55041  ** will be skipped.  Out-of-range pages are also skipped.
55042  */
55043  if( pSavepoint ){
55044    u32 ii;            /* Loop counter */
55045    i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
55046
55047    if( pagerUseWal(pPager) ){
55048      rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
55049    }
55050    for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
55051      assert( offset==(i64)ii*(4+pPager->pageSize) );
55052      rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
55053    }
55054    assert( rc!=SQLITE_DONE );
55055  }
55056
55057  sqlite3BitvecDestroy(pDone);
55058  if( rc==SQLITE_OK ){
55059    pPager->journalOff = szJ;
55060  }
55061
55062  return rc;
55063}
55064
55065/*
55066** Change the maximum number of in-memory pages that are allowed
55067** before attempting to recycle clean and unused pages.
55068*/
55069SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
55070  sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
55071}
55072
55073/*
55074** Change the maximum number of in-memory pages that are allowed
55075** before attempting to spill pages to journal.
55076*/
55077SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager *pPager, int mxPage){
55078  return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
55079}
55080
55081/*
55082** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
55083*/
55084static void pagerFixMaplimit(Pager *pPager){
55085#if SQLITE_MAX_MMAP_SIZE>0
55086  sqlite3_file *fd = pPager->fd;
55087  if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
55088    sqlite3_int64 sz;
55089    sz = pPager->szMmap;
55090    pPager->bUseFetch = (sz>0);
55091    setGetterMethod(pPager);
55092    sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
55093  }
55094#endif
55095}
55096
55097/*
55098** Change the maximum size of any memory mapping made of the database file.
55099*/
55100SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
55101  pPager->szMmap = szMmap;
55102  pagerFixMaplimit(pPager);
55103}
55104
55105/*
55106** Free as much memory as possible from the pager.
55107*/
55108SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
55109  sqlite3PcacheShrink(pPager->pPCache);
55110}
55111
55112/*
55113** Adjust settings of the pager to those specified in the pgFlags parameter.
55114**
55115** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
55116** of the database to damage due to OS crashes or power failures by
55117** changing the number of syncs()s when writing the journals.
55118** There are four levels:
55119**
55120**    OFF       sqlite3OsSync() is never called.  This is the default
55121**              for temporary and transient files.
55122**
55123**    NORMAL    The journal is synced once before writes begin on the
55124**              database.  This is normally adequate protection, but
55125**              it is theoretically possible, though very unlikely,
55126**              that an inopertune power failure could leave the journal
55127**              in a state which would cause damage to the database
55128**              when it is rolled back.
55129**
55130**    FULL      The journal is synced twice before writes begin on the
55131**              database (with some additional information - the nRec field
55132**              of the journal header - being written in between the two
55133**              syncs).  If we assume that writing a
55134**              single disk sector is atomic, then this mode provides
55135**              assurance that the journal will not be corrupted to the
55136**              point of causing damage to the database during rollback.
55137**
55138**    EXTRA     This is like FULL except that is also syncs the directory
55139**              that contains the rollback journal after the rollback
55140**              journal is unlinked.
55141**
55142** The above is for a rollback-journal mode.  For WAL mode, OFF continues
55143** to mean that no syncs ever occur.  NORMAL means that the WAL is synced
55144** prior to the start of checkpoint and that the database file is synced
55145** at the conclusion of the checkpoint if the entire content of the WAL
55146** was written back into the database.  But no sync operations occur for
55147** an ordinary commit in NORMAL mode with WAL.  FULL means that the WAL
55148** file is synced following each commit operation, in addition to the
55149** syncs associated with NORMAL.  There is no difference between FULL
55150** and EXTRA for WAL mode.
55151**
55152** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL.  The
55153** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
55154** using fcntl(F_FULLFSYNC).  SQLITE_SYNC_NORMAL means to do an
55155** ordinary fsync() call.  There is no difference between SQLITE_SYNC_FULL
55156** and SQLITE_SYNC_NORMAL on platforms other than MacOSX.  But the
55157** synchronous=FULL versus synchronous=NORMAL setting determines when
55158** the xSync primitive is called and is relevant to all platforms.
55159**
55160** Numeric values associated with these states are OFF==1, NORMAL=2,
55161** and FULL=3.
55162*/
55163#ifndef SQLITE_OMIT_PAGER_PRAGMAS
55164SQLITE_PRIVATE void sqlite3PagerSetFlags(
55165  Pager *pPager,        /* The pager to set safety level for */
55166  unsigned pgFlags      /* Various flags */
55167){
55168  unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
55169  if( pPager->tempFile ){
55170    pPager->noSync = 1;
55171    pPager->fullSync = 0;
55172    pPager->extraSync = 0;
55173  }else{
55174    pPager->noSync =  level==PAGER_SYNCHRONOUS_OFF ?1:0;
55175    pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
55176    pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
55177  }
55178  if( pPager->noSync ){
55179    pPager->syncFlags = 0;
55180  }else if( pgFlags & PAGER_FULLFSYNC ){
55181    pPager->syncFlags = SQLITE_SYNC_FULL;
55182  }else{
55183    pPager->syncFlags = SQLITE_SYNC_NORMAL;
55184  }
55185  pPager->walSyncFlags = (pPager->syncFlags<<2);
55186  if( pPager->fullSync ){
55187    pPager->walSyncFlags |= pPager->syncFlags;
55188  }
55189  if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
55190    pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
55191  }
55192  if( pgFlags & PAGER_CACHESPILL ){
55193    pPager->doNotSpill &= ~SPILLFLAG_OFF;
55194  }else{
55195    pPager->doNotSpill |= SPILLFLAG_OFF;
55196  }
55197}
55198#endif
55199
55200/*
55201** The following global variable is incremented whenever the library
55202** attempts to open a temporary file.  This information is used for
55203** testing and analysis only.
55204*/
55205#ifdef SQLITE_TEST
55206SQLITE_API int sqlite3_opentemp_count = 0;
55207#endif
55208
55209/*
55210** Open a temporary file.
55211**
55212** Write the file descriptor into *pFile. Return SQLITE_OK on success
55213** or some other error code if we fail. The OS will automatically
55214** delete the temporary file when it is closed.
55215**
55216** The flags passed to the VFS layer xOpen() call are those specified
55217** by parameter vfsFlags ORed with the following:
55218**
55219**     SQLITE_OPEN_READWRITE
55220**     SQLITE_OPEN_CREATE
55221**     SQLITE_OPEN_EXCLUSIVE
55222**     SQLITE_OPEN_DELETEONCLOSE
55223*/
55224static int pagerOpentemp(
55225  Pager *pPager,        /* The pager object */
55226  sqlite3_file *pFile,  /* Write the file descriptor here */
55227  int vfsFlags          /* Flags passed through to the VFS */
55228){
55229  int rc;               /* Return code */
55230
55231#ifdef SQLITE_TEST
55232  sqlite3_opentemp_count++;  /* Used for testing and analysis only */
55233#endif
55234
55235  vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
55236            SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
55237  rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
55238  assert( rc!=SQLITE_OK || isOpen(pFile) );
55239  return rc;
55240}
55241
55242/*
55243** Set the busy handler function.
55244**
55245** The pager invokes the busy-handler if sqlite3OsLock() returns
55246** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
55247** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
55248** lock. It does *not* invoke the busy handler when upgrading from
55249** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
55250** (which occurs during hot-journal rollback). Summary:
55251**
55252**   Transition                        | Invokes xBusyHandler
55253**   --------------------------------------------------------
55254**   NO_LOCK       -> SHARED_LOCK      | Yes
55255**   SHARED_LOCK   -> RESERVED_LOCK    | No
55256**   SHARED_LOCK   -> EXCLUSIVE_LOCK   | No
55257**   RESERVED_LOCK -> EXCLUSIVE_LOCK   | Yes
55258**
55259** If the busy-handler callback returns non-zero, the lock is
55260** retried. If it returns zero, then the SQLITE_BUSY error is
55261** returned to the caller of the pager API function.
55262*/
55263SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(
55264  Pager *pPager,                       /* Pager object */
55265  int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
55266  void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
55267){
55268  void **ap;
55269  pPager->xBusyHandler = xBusyHandler;
55270  pPager->pBusyHandlerArg = pBusyHandlerArg;
55271  ap = (void **)&pPager->xBusyHandler;
55272  assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
55273  assert( ap[1]==pBusyHandlerArg );
55274  sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
55275}
55276
55277/*
55278** Change the page size used by the Pager object. The new page size
55279** is passed in *pPageSize.
55280**
55281** If the pager is in the error state when this function is called, it
55282** is a no-op. The value returned is the error state error code (i.e.
55283** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
55284**
55285** Otherwise, if all of the following are true:
55286**
55287**   * the new page size (value of *pPageSize) is valid (a power
55288**     of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
55289**
55290**   * there are no outstanding page references, and
55291**
55292**   * the database is either not an in-memory database or it is
55293**     an in-memory database that currently consists of zero pages.
55294**
55295** then the pager object page size is set to *pPageSize.
55296**
55297** If the page size is changed, then this function uses sqlite3PagerMalloc()
55298** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
55299** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
55300** In all other cases, SQLITE_OK is returned.
55301**
55302** If the page size is not changed, either because one of the enumerated
55303** conditions above is not true, the pager was in error state when this
55304** function was called, or because the memory allocation attempt failed,
55305** then *pPageSize is set to the old, retained page size before returning.
55306*/
55307SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
55308  int rc = SQLITE_OK;
55309
55310  /* It is not possible to do a full assert_pager_state() here, as this
55311  ** function may be called from within PagerOpen(), before the state
55312  ** of the Pager object is internally consistent.
55313  **
55314  ** At one point this function returned an error if the pager was in
55315  ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
55316  ** there is at least one outstanding page reference, this function
55317  ** is a no-op for that case anyhow.
55318  */
55319
55320  u32 pageSize = *pPageSize;
55321  assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
55322  if( (pPager->memDb==0 || pPager->dbSize==0)
55323   && sqlite3PcacheRefCount(pPager->pPCache)==0
55324   && pageSize && pageSize!=(u32)pPager->pageSize
55325  ){
55326    char *pNew = NULL;             /* New temp space */
55327    i64 nByte = 0;
55328
55329    if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
55330      rc = sqlite3OsFileSize(pPager->fd, &nByte);
55331    }
55332    if( rc==SQLITE_OK ){
55333      /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
55334      * cell header parser will never run off the end of the allocation */
55335      pNew = (char *)sqlite3PageMalloc(pageSize+8);
55336      if( !pNew ){
55337        rc = SQLITE_NOMEM_BKPT;
55338      }else{
55339        memset(pNew+pageSize, 0, 8);
55340      }
55341    }
55342
55343    if( rc==SQLITE_OK ){
55344      pager_reset(pPager);
55345      rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
55346    }
55347    if( rc==SQLITE_OK ){
55348      sqlite3PageFree(pPager->pTmpSpace);
55349      pPager->pTmpSpace = pNew;
55350      pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
55351      pPager->pageSize = pageSize;
55352    }else{
55353      sqlite3PageFree(pNew);
55354    }
55355  }
55356
55357  *pPageSize = pPager->pageSize;
55358  if( rc==SQLITE_OK ){
55359    if( nReserve<0 ) nReserve = pPager->nReserve;
55360    assert( nReserve>=0 && nReserve<1000 );
55361    pPager->nReserve = (i16)nReserve;
55362    pagerFixMaplimit(pPager);
55363  }
55364  return rc;
55365}
55366
55367/*
55368** Return a pointer to the "temporary page" buffer held internally
55369** by the pager.  This is a buffer that is big enough to hold the
55370** entire content of a database page.  This buffer is used internally
55371** during rollback and will be overwritten whenever a rollback
55372** occurs.  But other modules are free to use it too, as long as
55373** no rollbacks are happening.
55374*/
55375SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
55376  return pPager->pTmpSpace;
55377}
55378
55379/*
55380** Attempt to set the maximum database page count if mxPage is positive.
55381** Make no changes if mxPage is zero or negative.  And never reduce the
55382** maximum page count below the current size of the database.
55383**
55384** Regardless of mxPage, return the current maximum page count.
55385*/
55386SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
55387  if( mxPage>0 ){
55388    pPager->mxPgno = mxPage;
55389  }
55390  assert( pPager->eState!=PAGER_OPEN );      /* Called only by OP_MaxPgcnt */
55391  /* assert( pPager->mxPgno>=pPager->dbSize ); */
55392  /* OP_MaxPgcnt ensures that the parameter passed to this function is not
55393  ** less than the total number of valid pages in the database. But this
55394  ** may be less than Pager.dbSize, and so the assert() above is not valid */
55395  return pPager->mxPgno;
55396}
55397
55398/*
55399** The following set of routines are used to disable the simulated
55400** I/O error mechanism.  These routines are used to avoid simulated
55401** errors in places where we do not care about errors.
55402**
55403** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
55404** and generate no code.
55405*/
55406#ifdef SQLITE_TEST
55407SQLITE_API extern int sqlite3_io_error_pending;
55408SQLITE_API extern int sqlite3_io_error_hit;
55409static int saved_cnt;
55410void disable_simulated_io_errors(void){
55411  saved_cnt = sqlite3_io_error_pending;
55412  sqlite3_io_error_pending = -1;
55413}
55414void enable_simulated_io_errors(void){
55415  sqlite3_io_error_pending = saved_cnt;
55416}
55417#else
55418# define disable_simulated_io_errors()
55419# define enable_simulated_io_errors()
55420#endif
55421
55422/*
55423** Read the first N bytes from the beginning of the file into memory
55424** that pDest points to.
55425**
55426** If the pager was opened on a transient file (zFilename==""), or
55427** opened on a file less than N bytes in size, the output buffer is
55428** zeroed and SQLITE_OK returned. The rationale for this is that this
55429** function is used to read database headers, and a new transient or
55430** zero sized database has a header than consists entirely of zeroes.
55431**
55432** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
55433** the error code is returned to the caller and the contents of the
55434** output buffer undefined.
55435*/
55436SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
55437  int rc = SQLITE_OK;
55438  memset(pDest, 0, N);
55439  assert( isOpen(pPager->fd) || pPager->tempFile );
55440
55441  /* This routine is only called by btree immediately after creating
55442  ** the Pager object.  There has not been an opportunity to transition
55443  ** to WAL mode yet.
55444  */
55445  assert( !pagerUseWal(pPager) );
55446
55447  if( isOpen(pPager->fd) ){
55448    IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
55449    rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
55450    if( rc==SQLITE_IOERR_SHORT_READ ){
55451      rc = SQLITE_OK;
55452    }
55453  }
55454  return rc;
55455}
55456
55457/*
55458** This function may only be called when a read-transaction is open on
55459** the pager. It returns the total number of pages in the database.
55460**
55461** However, if the file is between 1 and <page-size> bytes in size, then
55462** this is considered a 1 page file.
55463*/
55464SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
55465  assert( pPager->eState>=PAGER_READER );
55466  assert( pPager->eState!=PAGER_WRITER_FINISHED );
55467  *pnPage = (int)pPager->dbSize;
55468}
55469
55470
55471/*
55472** Try to obtain a lock of type locktype on the database file. If
55473** a similar or greater lock is already held, this function is a no-op
55474** (returning SQLITE_OK immediately).
55475**
55476** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
55477** the busy callback if the lock is currently not available. Repeat
55478** until the busy callback returns false or until the attempt to
55479** obtain the lock succeeds.
55480**
55481** Return SQLITE_OK on success and an error code if we cannot obtain
55482** the lock. If the lock is obtained successfully, set the Pager.state
55483** variable to locktype before returning.
55484*/
55485static int pager_wait_on_lock(Pager *pPager, int locktype){
55486  int rc;                              /* Return code */
55487
55488  /* Check that this is either a no-op (because the requested lock is
55489  ** already held), or one of the transitions that the busy-handler
55490  ** may be invoked during, according to the comment above
55491  ** sqlite3PagerSetBusyhandler().
55492  */
55493  assert( (pPager->eLock>=locktype)
55494       || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
55495       || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
55496  );
55497
55498  do {
55499    rc = pagerLockDb(pPager, locktype);
55500  }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
55501  return rc;
55502}
55503
55504/*
55505** Function assertTruncateConstraint(pPager) checks that one of the
55506** following is true for all dirty pages currently in the page-cache:
55507**
55508**   a) The page number is less than or equal to the size of the
55509**      current database image, in pages, OR
55510**
55511**   b) if the page content were written at this time, it would not
55512**      be necessary to write the current content out to the sub-journal
55513**      (as determined by function subjRequiresPage()).
55514**
55515** If the condition asserted by this function were not true, and the
55516** dirty page were to be discarded from the cache via the pagerStress()
55517** routine, pagerStress() would not write the current page content to
55518** the database file. If a savepoint transaction were rolled back after
55519** this happened, the correct behavior would be to restore the current
55520** content of the page. However, since this content is not present in either
55521** the database file or the portion of the rollback journal and
55522** sub-journal rolled back the content could not be restored and the
55523** database image would become corrupt. It is therefore fortunate that
55524** this circumstance cannot arise.
55525*/
55526#if defined(SQLITE_DEBUG)
55527static void assertTruncateConstraintCb(PgHdr *pPg){
55528  assert( pPg->flags&PGHDR_DIRTY );
55529  assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
55530}
55531static void assertTruncateConstraint(Pager *pPager){
55532  sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
55533}
55534#else
55535# define assertTruncateConstraint(pPager)
55536#endif
55537
55538/*
55539** Truncate the in-memory database file image to nPage pages. This
55540** function does not actually modify the database file on disk. It
55541** just sets the internal state of the pager object so that the
55542** truncation will be done when the current transaction is committed.
55543**
55544** This function is only called right before committing a transaction.
55545** Once this function has been called, the transaction must either be
55546** rolled back or committed. It is not safe to call this function and
55547** then continue writing to the database.
55548*/
55549SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
55550  assert( pPager->dbSize>=nPage );
55551  assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
55552  pPager->dbSize = nPage;
55553
55554  /* At one point the code here called assertTruncateConstraint() to
55555  ** ensure that all pages being truncated away by this operation are,
55556  ** if one or more savepoints are open, present in the savepoint
55557  ** journal so that they can be restored if the savepoint is rolled
55558  ** back. This is no longer necessary as this function is now only
55559  ** called right before committing a transaction. So although the
55560  ** Pager object may still have open savepoints (Pager.nSavepoint!=0),
55561  ** they cannot be rolled back. So the assertTruncateConstraint() call
55562  ** is no longer correct. */
55563}
55564
55565
55566/*
55567** This function is called before attempting a hot-journal rollback. It
55568** syncs the journal file to disk, then sets pPager->journalHdr to the
55569** size of the journal file so that the pager_playback() routine knows
55570** that the entire journal file has been synced.
55571**
55572** Syncing a hot-journal to disk before attempting to roll it back ensures
55573** that if a power-failure occurs during the rollback, the process that
55574** attempts rollback following system recovery sees the same journal
55575** content as this process.
55576**
55577** If everything goes as planned, SQLITE_OK is returned. Otherwise,
55578** an SQLite error code.
55579*/
55580static int pagerSyncHotJournal(Pager *pPager){
55581  int rc = SQLITE_OK;
55582  if( !pPager->noSync ){
55583    rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
55584  }
55585  if( rc==SQLITE_OK ){
55586    rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
55587  }
55588  return rc;
55589}
55590
55591#if SQLITE_MAX_MMAP_SIZE>0
55592/*
55593** Obtain a reference to a memory mapped page object for page number pgno.
55594** The new object will use the pointer pData, obtained from xFetch().
55595** If successful, set *ppPage to point to the new page reference
55596** and return SQLITE_OK. Otherwise, return an SQLite error code and set
55597** *ppPage to zero.
55598**
55599** Page references obtained by calling this function should be released
55600** by calling pagerReleaseMapPage().
55601*/
55602static int pagerAcquireMapPage(
55603  Pager *pPager,                  /* Pager object */
55604  Pgno pgno,                      /* Page number */
55605  void *pData,                    /* xFetch()'d data for this page */
55606  PgHdr **ppPage                  /* OUT: Acquired page object */
55607){
55608  PgHdr *p;                       /* Memory mapped page to return */
55609
55610  if( pPager->pMmapFreelist ){
55611    *ppPage = p = pPager->pMmapFreelist;
55612    pPager->pMmapFreelist = p->pDirty;
55613    p->pDirty = 0;
55614    assert( pPager->nExtra>=8 );
55615    memset(p->pExtra, 0, 8);
55616  }else{
55617    *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
55618    if( p==0 ){
55619      sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
55620      return SQLITE_NOMEM_BKPT;
55621    }
55622    p->pExtra = (void *)&p[1];
55623    p->flags = PGHDR_MMAP;
55624    p->nRef = 1;
55625    p->pPager = pPager;
55626  }
55627
55628  assert( p->pExtra==(void *)&p[1] );
55629  assert( p->pPage==0 );
55630  assert( p->flags==PGHDR_MMAP );
55631  assert( p->pPager==pPager );
55632  assert( p->nRef==1 );
55633
55634  p->pgno = pgno;
55635  p->pData = pData;
55636  pPager->nMmapOut++;
55637
55638  return SQLITE_OK;
55639}
55640#endif
55641
55642/*
55643** Release a reference to page pPg. pPg must have been returned by an
55644** earlier call to pagerAcquireMapPage().
55645*/
55646static void pagerReleaseMapPage(PgHdr *pPg){
55647  Pager *pPager = pPg->pPager;
55648  pPager->nMmapOut--;
55649  pPg->pDirty = pPager->pMmapFreelist;
55650  pPager->pMmapFreelist = pPg;
55651
55652  assert( pPager->fd->pMethods->iVersion>=3 );
55653  sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
55654}
55655
55656/*
55657** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
55658*/
55659static void pagerFreeMapHdrs(Pager *pPager){
55660  PgHdr *p;
55661  PgHdr *pNext;
55662  for(p=pPager->pMmapFreelist; p; p=pNext){
55663    pNext = p->pDirty;
55664    sqlite3_free(p);
55665  }
55666}
55667
55668/* Verify that the database file has not be deleted or renamed out from
55669** under the pager.  Return SQLITE_OK if the database is still where it ought
55670** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
55671** code from sqlite3OsAccess()) if the database has gone missing.
55672*/
55673static int databaseIsUnmoved(Pager *pPager){
55674  int bHasMoved = 0;
55675  int rc;
55676
55677  if( pPager->tempFile ) return SQLITE_OK;
55678  if( pPager->dbSize==0 ) return SQLITE_OK;
55679  assert( pPager->zFilename && pPager->zFilename[0] );
55680  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
55681  if( rc==SQLITE_NOTFOUND ){
55682    /* If the HAS_MOVED file-control is unimplemented, assume that the file
55683    ** has not been moved.  That is the historical behavior of SQLite: prior to
55684    ** version 3.8.3, it never checked */
55685    rc = SQLITE_OK;
55686  }else if( rc==SQLITE_OK && bHasMoved ){
55687    rc = SQLITE_READONLY_DBMOVED;
55688  }
55689  return rc;
55690}
55691
55692
55693/*
55694** Shutdown the page cache.  Free all memory and close all files.
55695**
55696** If a transaction was in progress when this routine is called, that
55697** transaction is rolled back.  All outstanding pages are invalidated
55698** and their memory is freed.  Any attempt to use a page associated
55699** with this page cache after this function returns will likely
55700** result in a coredump.
55701**
55702** This function always succeeds. If a transaction is active an attempt
55703** is made to roll it back. If an error occurs during the rollback
55704** a hot journal may be left in the filesystem but no error is returned
55705** to the caller.
55706*/
55707SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
55708  u8 *pTmp = (u8*)pPager->pTmpSpace;
55709  assert( db || pagerUseWal(pPager)==0 );
55710  assert( assert_pager_state(pPager) );
55711  disable_simulated_io_errors();
55712  sqlite3BeginBenignMalloc();
55713  pagerFreeMapHdrs(pPager);
55714  /* pPager->errCode = 0; */
55715  pPager->exclusiveMode = 0;
55716#ifndef SQLITE_OMIT_WAL
55717  {
55718    u8 *a = 0;
55719    assert( db || pPager->pWal==0 );
55720    if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
55721     && SQLITE_OK==databaseIsUnmoved(pPager)
55722    ){
55723      a = pTmp;
55724    }
55725    sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
55726    pPager->pWal = 0;
55727  }
55728#endif
55729  pager_reset(pPager);
55730  if( MEMDB ){
55731    pager_unlock(pPager);
55732  }else{
55733    /* If it is open, sync the journal file before calling UnlockAndRollback.
55734    ** If this is not done, then an unsynced portion of the open journal
55735    ** file may be played back into the database. If a power failure occurs
55736    ** while this is happening, the database could become corrupt.
55737    **
55738    ** If an error occurs while trying to sync the journal, shift the pager
55739    ** into the ERROR state. This causes UnlockAndRollback to unlock the
55740    ** database and close the journal file without attempting to roll it
55741    ** back or finalize it. The next database user will have to do hot-journal
55742    ** rollback before accessing the database file.
55743    */
55744    if( isOpen(pPager->jfd) ){
55745      pager_error(pPager, pagerSyncHotJournal(pPager));
55746    }
55747    pagerUnlockAndRollback(pPager);
55748  }
55749  sqlite3EndBenignMalloc();
55750  enable_simulated_io_errors();
55751  PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
55752  IOTRACE(("CLOSE %p\n", pPager))
55753  sqlite3OsClose(pPager->jfd);
55754  sqlite3OsClose(pPager->fd);
55755  sqlite3PageFree(pTmp);
55756  sqlite3PcacheClose(pPager->pPCache);
55757  assert( !pPager->aSavepoint && !pPager->pInJournal );
55758  assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
55759
55760  sqlite3_free(pPager);
55761  return SQLITE_OK;
55762}
55763
55764#if !defined(NDEBUG) || defined(SQLITE_TEST)
55765/*
55766** Return the page number for page pPg.
55767*/
55768SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
55769  return pPg->pgno;
55770}
55771#endif
55772
55773/*
55774** Increment the reference count for page pPg.
55775*/
55776SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
55777  sqlite3PcacheRef(pPg);
55778}
55779
55780/*
55781** Sync the journal. In other words, make sure all the pages that have
55782** been written to the journal have actually reached the surface of the
55783** disk and can be restored in the event of a hot-journal rollback.
55784**
55785** If the Pager.noSync flag is set, then this function is a no-op.
55786** Otherwise, the actions required depend on the journal-mode and the
55787** device characteristics of the file-system, as follows:
55788**
55789**   * If the journal file is an in-memory journal file, no action need
55790**     be taken.
55791**
55792**   * Otherwise, if the device does not support the SAFE_APPEND property,
55793**     then the nRec field of the most recently written journal header
55794**     is updated to contain the number of journal records that have
55795**     been written following it. If the pager is operating in full-sync
55796**     mode, then the journal file is synced before this field is updated.
55797**
55798**   * If the device does not support the SEQUENTIAL property, then
55799**     journal file is synced.
55800**
55801** Or, in pseudo-code:
55802**
55803**   if( NOT <in-memory journal> ){
55804**     if( NOT SAFE_APPEND ){
55805**       if( <full-sync mode> ) xSync(<journal file>);
55806**       <update nRec field>
55807**     }
55808**     if( NOT SEQUENTIAL ) xSync(<journal file>);
55809**   }
55810**
55811** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
55812** page currently held in memory before returning SQLITE_OK. If an IO
55813** error is encountered, then the IO error code is returned to the caller.
55814*/
55815static int syncJournal(Pager *pPager, int newHdr){
55816  int rc;                         /* Return code */
55817
55818  assert( pPager->eState==PAGER_WRITER_CACHEMOD
55819       || pPager->eState==PAGER_WRITER_DBMOD
55820  );
55821  assert( assert_pager_state(pPager) );
55822  assert( !pagerUseWal(pPager) );
55823
55824  rc = sqlite3PagerExclusiveLock(pPager);
55825  if( rc!=SQLITE_OK ) return rc;
55826
55827  if( !pPager->noSync ){
55828    assert( !pPager->tempFile );
55829    if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
55830      const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
55831      assert( isOpen(pPager->jfd) );
55832
55833      if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
55834        /* This block deals with an obscure problem. If the last connection
55835        ** that wrote to this database was operating in persistent-journal
55836        ** mode, then the journal file may at this point actually be larger
55837        ** than Pager.journalOff bytes. If the next thing in the journal
55838        ** file happens to be a journal-header (written as part of the
55839        ** previous connection's transaction), and a crash or power-failure
55840        ** occurs after nRec is updated but before this connection writes
55841        ** anything else to the journal file (or commits/rolls back its
55842        ** transaction), then SQLite may become confused when doing the
55843        ** hot-journal rollback following recovery. It may roll back all
55844        ** of this connections data, then proceed to rolling back the old,
55845        ** out-of-date data that follows it. Database corruption.
55846        **
55847        ** To work around this, if the journal file does appear to contain
55848        ** a valid header following Pager.journalOff, then write a 0x00
55849        ** byte to the start of it to prevent it from being recognized.
55850        **
55851        ** Variable iNextHdrOffset is set to the offset at which this
55852        ** problematic header will occur, if it exists. aMagic is used
55853        ** as a temporary buffer to inspect the first couple of bytes of
55854        ** the potential journal header.
55855        */
55856        i64 iNextHdrOffset;
55857        u8 aMagic[8];
55858        u8 zHeader[sizeof(aJournalMagic)+4];
55859
55860        memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
55861        put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
55862
55863        iNextHdrOffset = journalHdrOffset(pPager);
55864        rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
55865        if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
55866          static const u8 zerobyte = 0;
55867          rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
55868        }
55869        if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
55870          return rc;
55871        }
55872
55873        /* Write the nRec value into the journal file header. If in
55874        ** full-synchronous mode, sync the journal first. This ensures that
55875        ** all data has really hit the disk before nRec is updated to mark
55876        ** it as a candidate for rollback.
55877        **
55878        ** This is not required if the persistent media supports the
55879        ** SAFE_APPEND property. Because in this case it is not possible
55880        ** for garbage data to be appended to the file, the nRec field
55881        ** is populated with 0xFFFFFFFF when the journal header is written
55882        ** and never needs to be updated.
55883        */
55884        if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
55885          PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
55886          IOTRACE(("JSYNC %p\n", pPager))
55887          rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
55888          if( rc!=SQLITE_OK ) return rc;
55889        }
55890        IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
55891        rc = sqlite3OsWrite(
55892            pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
55893        );
55894        if( rc!=SQLITE_OK ) return rc;
55895      }
55896      if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
55897        PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
55898        IOTRACE(("JSYNC %p\n", pPager))
55899        rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
55900          (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
55901        );
55902        if( rc!=SQLITE_OK ) return rc;
55903      }
55904
55905      pPager->journalHdr = pPager->journalOff;
55906      if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
55907        pPager->nRec = 0;
55908        rc = writeJournalHdr(pPager);
55909        if( rc!=SQLITE_OK ) return rc;
55910      }
55911    }else{
55912      pPager->journalHdr = pPager->journalOff;
55913    }
55914  }
55915
55916  /* Unless the pager is in noSync mode, the journal file was just
55917  ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
55918  ** all pages.
55919  */
55920  sqlite3PcacheClearSyncFlags(pPager->pPCache);
55921  pPager->eState = PAGER_WRITER_DBMOD;
55922  assert( assert_pager_state(pPager) );
55923  return SQLITE_OK;
55924}
55925
55926/*
55927** The argument is the first in a linked list of dirty pages connected
55928** by the PgHdr.pDirty pointer. This function writes each one of the
55929** in-memory pages in the list to the database file. The argument may
55930** be NULL, representing an empty list. In this case this function is
55931** a no-op.
55932**
55933** The pager must hold at least a RESERVED lock when this function
55934** is called. Before writing anything to the database file, this lock
55935** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
55936** SQLITE_BUSY is returned and no data is written to the database file.
55937**
55938** If the pager is a temp-file pager and the actual file-system file
55939** is not yet open, it is created and opened before any data is
55940** written out.
55941**
55942** Once the lock has been upgraded and, if necessary, the file opened,
55943** the pages are written out to the database file in list order. Writing
55944** a page is skipped if it meets either of the following criteria:
55945**
55946**   * The page number is greater than Pager.dbSize, or
55947**   * The PGHDR_DONT_WRITE flag is set on the page.
55948**
55949** If writing out a page causes the database file to grow, Pager.dbFileSize
55950** is updated accordingly. If page 1 is written out, then the value cached
55951** in Pager.dbFileVers[] is updated to match the new value stored in
55952** the database file.
55953**
55954** If everything is successful, SQLITE_OK is returned. If an IO error
55955** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
55956** be obtained, SQLITE_BUSY is returned.
55957*/
55958static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
55959  int rc = SQLITE_OK;                  /* Return code */
55960
55961  /* This function is only called for rollback pagers in WRITER_DBMOD state. */
55962  assert( !pagerUseWal(pPager) );
55963  assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
55964  assert( pPager->eLock==EXCLUSIVE_LOCK );
55965  assert( isOpen(pPager->fd) || pList->pDirty==0 );
55966
55967  /* If the file is a temp-file has not yet been opened, open it now. It
55968  ** is not possible for rc to be other than SQLITE_OK if this branch
55969  ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
55970  */
55971  if( !isOpen(pPager->fd) ){
55972    assert( pPager->tempFile && rc==SQLITE_OK );
55973    rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
55974  }
55975
55976  /* Before the first write, give the VFS a hint of what the final
55977  ** file size will be.
55978  */
55979  assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
55980  if( rc==SQLITE_OK
55981   && pPager->dbHintSize<pPager->dbSize
55982   && (pList->pDirty || pList->pgno>pPager->dbHintSize)
55983  ){
55984    sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
55985    sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
55986    pPager->dbHintSize = pPager->dbSize;
55987  }
55988
55989  while( rc==SQLITE_OK && pList ){
55990    Pgno pgno = pList->pgno;
55991
55992    /* If there are dirty pages in the page cache with page numbers greater
55993    ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
55994    ** make the file smaller (presumably by auto-vacuum code). Do not write
55995    ** any such pages to the file.
55996    **
55997    ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
55998    ** set (set by sqlite3PagerDontWrite()).
55999    */
56000    if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
56001      i64 offset = (pgno-1)*(i64)pPager->pageSize;   /* Offset to write */
56002      char *pData;                                   /* Data to write */
56003
56004      assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
56005      if( pList->pgno==1 ) pager_write_changecounter(pList);
56006
56007      pData = pList->pData;
56008
56009      /* Write out the page data. */
56010      rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
56011
56012      /* If page 1 was just written, update Pager.dbFileVers to match
56013      ** the value now stored in the database file. If writing this
56014      ** page caused the database file to grow, update dbFileSize.
56015      */
56016      if( pgno==1 ){
56017        memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
56018      }
56019      if( pgno>pPager->dbFileSize ){
56020        pPager->dbFileSize = pgno;
56021      }
56022      pPager->aStat[PAGER_STAT_WRITE]++;
56023
56024      /* Update any backup objects copying the contents of this pager. */
56025      sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
56026
56027      PAGERTRACE(("STORE %d page %d hash(%08x)\n",
56028                   PAGERID(pPager), pgno, pager_pagehash(pList)));
56029      IOTRACE(("PGOUT %p %d\n", pPager, pgno));
56030      PAGER_INCR(sqlite3_pager_writedb_count);
56031    }else{
56032      PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
56033    }
56034    pager_set_pagehash(pList);
56035    pList = pList->pDirty;
56036  }
56037
56038  return rc;
56039}
56040
56041/*
56042** Ensure that the sub-journal file is open. If it is already open, this
56043** function is a no-op.
56044**
56045** SQLITE_OK is returned if everything goes according to plan. An
56046** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
56047** fails.
56048*/
56049static int openSubJournal(Pager *pPager){
56050  int rc = SQLITE_OK;
56051  if( !isOpen(pPager->sjfd) ){
56052    const int flags =  SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_READWRITE
56053      | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE
56054      | SQLITE_OPEN_DELETEONCLOSE;
56055    int nStmtSpill = sqlite3Config.nStmtSpill;
56056    if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
56057      nStmtSpill = -1;
56058    }
56059    rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
56060  }
56061  return rc;
56062}
56063
56064/*
56065** Append a record of the current state of page pPg to the sub-journal.
56066**
56067** If successful, set the bit corresponding to pPg->pgno in the bitvecs
56068** for all open savepoints before returning.
56069**
56070** This function returns SQLITE_OK if everything is successful, an IO
56071** error code if the attempt to write to the sub-journal fails, or
56072** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
56073** bitvec.
56074*/
56075static int subjournalPage(PgHdr *pPg){
56076  int rc = SQLITE_OK;
56077  Pager *pPager = pPg->pPager;
56078  if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
56079
56080    /* Open the sub-journal, if it has not already been opened */
56081    assert( pPager->useJournal );
56082    assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
56083    assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
56084    assert( pagerUseWal(pPager)
56085         || pageInJournal(pPager, pPg)
56086         || pPg->pgno>pPager->dbOrigSize
56087    );
56088    rc = openSubJournal(pPager);
56089
56090    /* If the sub-journal was opened successfully (or was already open),
56091    ** write the journal record into the file.  */
56092    if( rc==SQLITE_OK ){
56093      void *pData = pPg->pData;
56094      i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
56095      char *pData2;
56096      pData2 = pData;
56097      PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
56098      rc = write32bits(pPager->sjfd, offset, pPg->pgno);
56099      if( rc==SQLITE_OK ){
56100        rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
56101      }
56102    }
56103  }
56104  if( rc==SQLITE_OK ){
56105    pPager->nSubRec++;
56106    assert( pPager->nSavepoint>0 );
56107    rc = addToSavepointBitvecs(pPager, pPg->pgno);
56108  }
56109  return rc;
56110}
56111static int subjournalPageIfRequired(PgHdr *pPg){
56112  if( subjRequiresPage(pPg) ){
56113    return subjournalPage(pPg);
56114  }else{
56115    return SQLITE_OK;
56116  }
56117}
56118
56119/*
56120** This function is called by the pcache layer when it has reached some
56121** soft memory limit. The first argument is a pointer to a Pager object
56122** (cast as a void*). The pager is always 'purgeable' (not an in-memory
56123** database). The second argument is a reference to a page that is
56124** currently dirty but has no outstanding references. The page
56125** is always associated with the Pager object passed as the first
56126** argument.
56127**
56128** The job of this function is to make pPg clean by writing its contents
56129** out to the database file, if possible. This may involve syncing the
56130** journal file.
56131**
56132** If successful, sqlite3PcacheMakeClean() is called on the page and
56133** SQLITE_OK returned. If an IO error occurs while trying to make the
56134** page clean, the IO error code is returned. If the page cannot be
56135** made clean for some other reason, but no error occurs, then SQLITE_OK
56136** is returned by sqlite3PcacheMakeClean() is not called.
56137*/
56138static int pagerStress(void *p, PgHdr *pPg){
56139  Pager *pPager = (Pager *)p;
56140  int rc = SQLITE_OK;
56141
56142  assert( pPg->pPager==pPager );
56143  assert( pPg->flags&PGHDR_DIRTY );
56144
56145  /* The doNotSpill NOSYNC bit is set during times when doing a sync of
56146  ** journal (and adding a new header) is not allowed.  This occurs
56147  ** during calls to sqlite3PagerWrite() while trying to journal multiple
56148  ** pages belonging to the same sector.
56149  **
56150  ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
56151  ** regardless of whether or not a sync is required.  This is set during
56152  ** a rollback or by user request, respectively.
56153  **
56154  ** Spilling is also prohibited when in an error state since that could
56155  ** lead to database corruption.   In the current implementation it
56156  ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3
56157  ** while in the error state, hence it is impossible for this routine to
56158  ** be called in the error state.  Nevertheless, we include a NEVER()
56159  ** test for the error state as a safeguard against future changes.
56160  */
56161  if( NEVER(pPager->errCode) ) return SQLITE_OK;
56162  testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
56163  testcase( pPager->doNotSpill & SPILLFLAG_OFF );
56164  testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
56165  if( pPager->doNotSpill
56166   && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
56167      || (pPg->flags & PGHDR_NEED_SYNC)!=0)
56168  ){
56169    return SQLITE_OK;
56170  }
56171
56172  pPager->aStat[PAGER_STAT_SPILL]++;
56173  pPg->pDirty = 0;
56174  if( pagerUseWal(pPager) ){
56175    /* Write a single frame for this page to the log. */
56176    rc = subjournalPageIfRequired(pPg);
56177    if( rc==SQLITE_OK ){
56178      rc = pagerWalFrames(pPager, pPg, 0, 0);
56179    }
56180  }else{
56181
56182#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
56183    if( pPager->tempFile==0 ){
56184      rc = sqlite3JournalCreate(pPager->jfd);
56185      if( rc!=SQLITE_OK ) return pager_error(pPager, rc);
56186    }
56187#endif
56188
56189    /* Sync the journal file if required. */
56190    if( pPg->flags&PGHDR_NEED_SYNC
56191     || pPager->eState==PAGER_WRITER_CACHEMOD
56192    ){
56193      rc = syncJournal(pPager, 1);
56194    }
56195
56196    /* Write the contents of the page out to the database file. */
56197    if( rc==SQLITE_OK ){
56198      assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
56199      rc = pager_write_pagelist(pPager, pPg);
56200    }
56201  }
56202
56203  /* Mark the page as clean. */
56204  if( rc==SQLITE_OK ){
56205    PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
56206    sqlite3PcacheMakeClean(pPg);
56207  }
56208
56209  return pager_error(pPager, rc);
56210}
56211
56212/*
56213** Flush all unreferenced dirty pages to disk.
56214*/
56215SQLITE_PRIVATE int sqlite3PagerFlush(Pager *pPager){
56216  int rc = pPager->errCode;
56217  if( !MEMDB ){
56218    PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
56219    assert( assert_pager_state(pPager) );
56220    while( rc==SQLITE_OK && pList ){
56221      PgHdr *pNext = pList->pDirty;
56222      if( pList->nRef==0 ){
56223        rc = pagerStress((void*)pPager, pList);
56224      }
56225      pList = pNext;
56226    }
56227  }
56228
56229  return rc;
56230}
56231
56232/*
56233** Allocate and initialize a new Pager object and put a pointer to it
56234** in *ppPager. The pager should eventually be freed by passing it
56235** to sqlite3PagerClose().
56236**
56237** The zFilename argument is the path to the database file to open.
56238** If zFilename is NULL then a randomly-named temporary file is created
56239** and used as the file to be cached. Temporary files are be deleted
56240** automatically when they are closed. If zFilename is ":memory:" then
56241** all information is held in cache. It is never written to disk.
56242** This can be used to implement an in-memory database.
56243**
56244** The nExtra parameter specifies the number of bytes of space allocated
56245** along with each page reference. This space is available to the user
56246** via the sqlite3PagerGetExtra() API.  When a new page is allocated, the
56247** first 8 bytes of this space are zeroed but the remainder is uninitialized.
56248** (The extra space is used by btree as the MemPage object.)
56249**
56250** The flags argument is used to specify properties that affect the
56251** operation of the pager. It should be passed some bitwise combination
56252** of the PAGER_* flags.
56253**
56254** The vfsFlags parameter is a bitmask to pass to the flags parameter
56255** of the xOpen() method of the supplied VFS when opening files.
56256**
56257** If the pager object is allocated and the specified file opened
56258** successfully, SQLITE_OK is returned and *ppPager set to point to
56259** the new pager object. If an error occurs, *ppPager is set to NULL
56260** and error code returned. This function may return SQLITE_NOMEM
56261** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
56262** various SQLITE_IO_XXX errors.
56263*/
56264SQLITE_PRIVATE int sqlite3PagerOpen(
56265  sqlite3_vfs *pVfs,       /* The virtual file system to use */
56266  Pager **ppPager,         /* OUT: Return the Pager structure here */
56267  const char *zFilename,   /* Name of the database file to open */
56268  int nExtra,              /* Extra bytes append to each in-memory page */
56269  int flags,               /* flags controlling this file */
56270  int vfsFlags,            /* flags passed through to sqlite3_vfs.xOpen() */
56271  void (*xReinit)(DbPage*) /* Function to reinitialize pages */
56272){
56273  u8 *pPtr;
56274  Pager *pPager = 0;       /* Pager object to allocate and return */
56275  int rc = SQLITE_OK;      /* Return code */
56276  int tempFile = 0;        /* True for temp files (incl. in-memory files) */
56277  int memDb = 0;           /* True if this is an in-memory file */
56278#ifdef SQLITE_ENABLE_DESERIALIZE
56279  int memJM = 0;           /* Memory journal mode */
56280#else
56281# define memJM 0
56282#endif
56283  int readOnly = 0;        /* True if this is a read-only file */
56284  int journalFileSize;     /* Bytes to allocate for each journal fd */
56285  char *zPathname = 0;     /* Full path to database file */
56286  int nPathname = 0;       /* Number of bytes in zPathname */
56287  int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
56288  int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
56289  u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
56290  const char *zUri = 0;    /* URI args to copy */
56291  int nUriByte = 1;        /* Number of bytes of URI args at *zUri */
56292  int nUri = 0;            /* Number of URI parameters */
56293
56294  /* Figure out how much space is required for each journal file-handle
56295  ** (there are two of them, the main journal and the sub-journal).  */
56296  journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
56297
56298  /* Set the output variable to NULL in case an error occurs. */
56299  *ppPager = 0;
56300
56301#ifndef SQLITE_OMIT_MEMORYDB
56302  if( flags & PAGER_MEMORY ){
56303    memDb = 1;
56304    if( zFilename && zFilename[0] ){
56305      zPathname = sqlite3DbStrDup(0, zFilename);
56306      if( zPathname==0  ) return SQLITE_NOMEM_BKPT;
56307      nPathname = sqlite3Strlen30(zPathname);
56308      zFilename = 0;
56309    }
56310  }
56311#endif
56312
56313  /* Compute and store the full pathname in an allocated buffer pointed
56314  ** to by zPathname, length nPathname. Or, if this is a temporary file,
56315  ** leave both nPathname and zPathname set to 0.
56316  */
56317  if( zFilename && zFilename[0] ){
56318    const char *z;
56319    nPathname = pVfs->mxPathname+1;
56320    zPathname = sqlite3DbMallocRaw(0, nPathname*2);
56321    if( zPathname==0 ){
56322      return SQLITE_NOMEM_BKPT;
56323    }
56324    zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
56325    rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
56326    if( rc!=SQLITE_OK ){
56327      if( rc==SQLITE_OK_SYMLINK ){
56328        if( vfsFlags & SQLITE_OPEN_NOFOLLOW ){
56329          rc = SQLITE_CANTOPEN_SYMLINK;
56330        }else{
56331          rc = SQLITE_OK;
56332        }
56333      }
56334    }
56335    nPathname = sqlite3Strlen30(zPathname);
56336    z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
56337    while( *z ){
56338      z += strlen(z)+1;
56339      z += strlen(z)+1;
56340      nUri++;
56341    }
56342    nUriByte = (int)(&z[1] - zUri);
56343    assert( nUriByte>=1 );
56344    if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
56345      /* This branch is taken when the journal path required by
56346      ** the database being opened will be more than pVfs->mxPathname
56347      ** bytes in length. This means the database cannot be opened,
56348      ** as it will not be possible to open the journal file or even
56349      ** check for a hot-journal before reading.
56350      */
56351      rc = SQLITE_CANTOPEN_BKPT;
56352    }
56353    if( rc!=SQLITE_OK ){
56354      sqlite3DbFree(0, zPathname);
56355      return rc;
56356    }
56357  }
56358
56359  /* Allocate memory for the Pager structure, PCache object, the
56360  ** three file descriptors, the database file name and the journal
56361  ** file name. The layout in memory is as follows:
56362  **
56363  **     Pager object                    (sizeof(Pager) bytes)
56364  **     PCache object                   (sqlite3PcacheSize() bytes)
56365  **     Database file handle            (pVfs->szOsFile bytes)
56366  **     Sub-journal file handle         (journalFileSize bytes)
56367  **     Main journal file handle        (journalFileSize bytes)
56368  **     Ptr back to the Pager           (sizeof(Pager*) bytes)
56369  **     \0\0\0\0 database prefix        (4 bytes)
56370  **     Database file name              (nPathname+1 bytes)
56371  **     URI query parameters            (nUriByte bytes)
56372  **     Journal filename                (nPathname+8+1 bytes)
56373  **     WAL filename                    (nPathname+4+1 bytes)
56374  **     \0\0\0 terminator               (3 bytes)
56375  **
56376  ** Some 3rd-party software, over which we have no control, depends on
56377  ** the specific order of the filenames and the \0 separators between them
56378  ** so that it can (for example) find the database filename given the WAL
56379  ** filename without using the sqlite3_filename_database() API.  This is a
56380  ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
56381  ** software is in widespread use, so we try to avoid changing the filename
56382  ** order and formatting if possible.  In particular, the details of the
56383  ** filename format expected by 3rd-party software should be as follows:
56384  **
56385  **   - Main Database Path
56386  **   - \0
56387  **   - Multiple URI components consisting of:
56388  **     - Key
56389  **     - \0
56390  **     - Value
56391  **     - \0
56392  **   - \0
56393  **   - Journal Path
56394  **   - \0
56395  **   - WAL Path (zWALName)
56396  **   - \0
56397  **
56398  ** The sqlite3_create_filename() interface and the databaseFilename() utility
56399  ** that is used by sqlite3_filename_database() and kin also depend on the
56400  ** specific formatting and order of the various filenames, so if the format
56401  ** changes here, be sure to change it there as well.
56402  */
56403  pPtr = (u8 *)sqlite3MallocZero(
56404    ROUND8(sizeof(*pPager)) +            /* Pager structure */
56405    ROUND8(pcacheSize) +                 /* PCache object */
56406    ROUND8(pVfs->szOsFile) +             /* The main db file */
56407    journalFileSize * 2 +                /* The two journal files */
56408    sizeof(pPager) +                     /* Space to hold a pointer */
56409    4 +                                  /* Database prefix */
56410    nPathname + 1 +                      /* database filename */
56411    nUriByte +                           /* query parameters */
56412    nPathname + 8 + 1 +                  /* Journal filename */
56413#ifndef SQLITE_OMIT_WAL
56414    nPathname + 4 + 1 +                  /* WAL filename */
56415#endif
56416    3                                    /* Terminator */
56417  );
56418  assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
56419  if( !pPtr ){
56420    sqlite3DbFree(0, zPathname);
56421    return SQLITE_NOMEM_BKPT;
56422  }
56423  pPager = (Pager*)pPtr;                  pPtr += ROUND8(sizeof(*pPager));
56424  pPager->pPCache = (PCache*)pPtr;        pPtr += ROUND8(pcacheSize);
56425  pPager->fd = (sqlite3_file*)pPtr;       pPtr += ROUND8(pVfs->szOsFile);
56426  pPager->sjfd = (sqlite3_file*)pPtr;     pPtr += journalFileSize;
56427  pPager->jfd =  (sqlite3_file*)pPtr;     pPtr += journalFileSize;
56428  assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
56429  memcpy(pPtr, &pPager, sizeof(pPager));  pPtr += sizeof(pPager);
56430
56431  /* Fill in the Pager.zFilename and pPager.zQueryParam fields */
56432                                          pPtr += 4;  /* Skip zero prefix */
56433  pPager->zFilename = (char*)pPtr;
56434  if( nPathname>0 ){
56435    memcpy(pPtr, zPathname, nPathname);   pPtr += nPathname + 1;
56436    if( zUri ){
56437      memcpy(pPtr, zUri, nUriByte);       pPtr += nUriByte;
56438    }else{
56439                                          pPtr++;
56440    }
56441  }
56442
56443
56444  /* Fill in Pager.zJournal */
56445  if( nPathname>0 ){
56446    pPager->zJournal = (char*)pPtr;
56447    memcpy(pPtr, zPathname, nPathname);   pPtr += nPathname;
56448    memcpy(pPtr, "-journal",8);           pPtr += 8 + 1;
56449#ifdef SQLITE_ENABLE_8_3_NAMES
56450    sqlite3FileSuffix3(zFilename,pPager->zJournal);
56451    pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
56452#endif
56453  }else{
56454    pPager->zJournal = 0;
56455  }
56456
56457#ifndef SQLITE_OMIT_WAL
56458  /* Fill in Pager.zWal */
56459  if( nPathname>0 ){
56460    pPager->zWal = (char*)pPtr;
56461    memcpy(pPtr, zPathname, nPathname);   pPtr += nPathname;
56462    memcpy(pPtr, "-wal", 4);              pPtr += 4 + 1;
56463#ifdef SQLITE_ENABLE_8_3_NAMES
56464    sqlite3FileSuffix3(zFilename, pPager->zWal);
56465    pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
56466#endif
56467  }else{
56468    pPager->zWal = 0;
56469  }
56470#endif
56471
56472  if( nPathname ) sqlite3DbFree(0, zPathname);
56473  pPager->pVfs = pVfs;
56474  pPager->vfsFlags = vfsFlags;
56475
56476  /* Open the pager file.
56477  */
56478  if( zFilename && zFilename[0] ){
56479    int fout = 0;                    /* VFS flags returned by xOpen() */
56480    rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
56481    assert( !memDb );
56482#ifdef SQLITE_ENABLE_DESERIALIZE
56483    memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
56484#endif
56485    readOnly = (fout&SQLITE_OPEN_READONLY)!=0;
56486
56487    /* If the file was successfully opened for read/write access,
56488    ** choose a default page size in case we have to create the
56489    ** database file. The default page size is the maximum of:
56490    **
56491    **    + SQLITE_DEFAULT_PAGE_SIZE,
56492    **    + The value returned by sqlite3OsSectorSize()
56493    **    + The largest page size that can be written atomically.
56494    */
56495    if( rc==SQLITE_OK ){
56496      int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
56497      if( !readOnly ){
56498        setSectorSize(pPager);
56499        assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
56500        if( szPageDflt<pPager->sectorSize ){
56501          if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
56502            szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
56503          }else{
56504            szPageDflt = (u32)pPager->sectorSize;
56505          }
56506        }
56507#ifdef SQLITE_ENABLE_ATOMIC_WRITE
56508        {
56509          int ii;
56510          assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
56511          assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
56512          assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
56513          for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
56514            if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
56515              szPageDflt = ii;
56516            }
56517          }
56518        }
56519#endif
56520      }
56521      pPager->noLock = sqlite3_uri_boolean(pPager->zFilename, "nolock", 0);
56522      if( (iDc & SQLITE_IOCAP_IMMUTABLE)!=0
56523       || sqlite3_uri_boolean(pPager->zFilename, "immutable", 0) ){
56524          vfsFlags |= SQLITE_OPEN_READONLY;
56525          goto act_like_temp_file;
56526      }
56527    }
56528  }else{
56529    /* If a temporary file is requested, it is not opened immediately.
56530    ** In this case we accept the default page size and delay actually
56531    ** opening the file until the first call to OsWrite().
56532    **
56533    ** This branch is also run for an in-memory database. An in-memory
56534    ** database is the same as a temp-file that is never written out to
56535    ** disk and uses an in-memory rollback journal.
56536    **
56537    ** This branch also runs for files marked as immutable.
56538    */
56539act_like_temp_file:
56540    tempFile = 1;
56541    pPager->eState = PAGER_READER;     /* Pretend we already have a lock */
56542    pPager->eLock = EXCLUSIVE_LOCK;    /* Pretend we are in EXCLUSIVE mode */
56543    pPager->noLock = 1;                /* Do no locking */
56544    readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
56545  }
56546
56547  /* The following call to PagerSetPagesize() serves to set the value of
56548  ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
56549  */
56550  if( rc==SQLITE_OK ){
56551    assert( pPager->memDb==0 );
56552    rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
56553    testcase( rc!=SQLITE_OK );
56554  }
56555
56556  /* Initialize the PCache object. */
56557  if( rc==SQLITE_OK ){
56558    nExtra = ROUND8(nExtra);
56559    assert( nExtra>=8 && nExtra<1000 );
56560    rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
56561                       !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
56562  }
56563
56564  /* If an error occurred above, free the  Pager structure and close the file.
56565  */
56566  if( rc!=SQLITE_OK ){
56567    sqlite3OsClose(pPager->fd);
56568    sqlite3PageFree(pPager->pTmpSpace);
56569    sqlite3_free(pPager);
56570    return rc;
56571  }
56572
56573  PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
56574  IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
56575
56576  pPager->useJournal = (u8)useJournal;
56577  /* pPager->stmtOpen = 0; */
56578  /* pPager->stmtInUse = 0; */
56579  /* pPager->nRef = 0; */
56580  /* pPager->stmtSize = 0; */
56581  /* pPager->stmtJSize = 0; */
56582  /* pPager->nPage = 0; */
56583  pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
56584  /* pPager->state = PAGER_UNLOCK; */
56585  /* pPager->errMask = 0; */
56586  pPager->tempFile = (u8)tempFile;
56587  assert( tempFile==PAGER_LOCKINGMODE_NORMAL
56588          || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
56589  assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
56590  pPager->exclusiveMode = (u8)tempFile;
56591  pPager->changeCountDone = pPager->tempFile;
56592  pPager->memDb = (u8)memDb;
56593  pPager->readOnly = (u8)readOnly;
56594  assert( useJournal || pPager->tempFile );
56595  pPager->noSync = pPager->tempFile;
56596  if( pPager->noSync ){
56597    assert( pPager->fullSync==0 );
56598    assert( pPager->extraSync==0 );
56599    assert( pPager->syncFlags==0 );
56600    assert( pPager->walSyncFlags==0 );
56601  }else{
56602    pPager->fullSync = 1;
56603    pPager->extraSync = 0;
56604    pPager->syncFlags = SQLITE_SYNC_NORMAL;
56605    pPager->walSyncFlags = SQLITE_SYNC_NORMAL | (SQLITE_SYNC_NORMAL<<2);
56606  }
56607  /* pPager->pFirst = 0; */
56608  /* pPager->pFirstSynced = 0; */
56609  /* pPager->pLast = 0; */
56610  pPager->nExtra = (u16)nExtra;
56611  pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
56612  assert( isOpen(pPager->fd) || tempFile );
56613  setSectorSize(pPager);
56614  if( !useJournal ){
56615    pPager->journalMode = PAGER_JOURNALMODE_OFF;
56616  }else if( memDb || memJM ){
56617    pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
56618  }
56619  /* pPager->xBusyHandler = 0; */
56620  /* pPager->pBusyHandlerArg = 0; */
56621  pPager->xReiniter = xReinit;
56622  setGetterMethod(pPager);
56623  /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
56624  /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
56625
56626  *ppPager = pPager;
56627  return SQLITE_OK;
56628}
56629
56630/*
56631** Return the sqlite3_file for the main database given the name
56632** of the corresonding WAL or Journal name as passed into
56633** xOpen.
56634*/
56635SQLITE_API sqlite3_file *sqlite3_database_file_object(const char *zName){
56636  Pager *pPager;
56637  while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
56638    zName--;
56639  }
56640  pPager = *(Pager**)(zName - 4 - sizeof(Pager*));
56641  return pPager->fd;
56642}
56643
56644
56645/*
56646** This function is called after transitioning from PAGER_UNLOCK to
56647** PAGER_SHARED state. It tests if there is a hot journal present in
56648** the file-system for the given pager. A hot journal is one that
56649** needs to be played back. According to this function, a hot-journal
56650** file exists if the following criteria are met:
56651**
56652**   * The journal file exists in the file system, and
56653**   * No process holds a RESERVED or greater lock on the database file, and
56654**   * The database file itself is greater than 0 bytes in size, and
56655**   * The first byte of the journal file exists and is not 0x00.
56656**
56657** If the current size of the database file is 0 but a journal file
56658** exists, that is probably an old journal left over from a prior
56659** database with the same name. In this case the journal file is
56660** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
56661** is returned.
56662**
56663** This routine does not check if there is a master journal filename
56664** at the end of the file. If there is, and that master journal file
56665** does not exist, then the journal file is not really hot. In this
56666** case this routine will return a false-positive. The pager_playback()
56667** routine will discover that the journal file is not really hot and
56668** will not roll it back.
56669**
56670** If a hot-journal file is found to exist, *pExists is set to 1 and
56671** SQLITE_OK returned. If no hot-journal file is present, *pExists is
56672** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
56673** to determine whether or not a hot-journal file exists, the IO error
56674** code is returned and the value of *pExists is undefined.
56675*/
56676static int hasHotJournal(Pager *pPager, int *pExists){
56677  sqlite3_vfs * const pVfs = pPager->pVfs;
56678  int rc = SQLITE_OK;           /* Return code */
56679  int exists = 1;               /* True if a journal file is present */
56680  int jrnlOpen = !!isOpen(pPager->jfd);
56681
56682  assert( pPager->useJournal );
56683  assert( isOpen(pPager->fd) );
56684  assert( pPager->eState==PAGER_OPEN );
56685
56686  assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
56687    SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
56688  ));
56689
56690  *pExists = 0;
56691  if( !jrnlOpen ){
56692    rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
56693  }
56694  if( rc==SQLITE_OK && exists ){
56695    int locked = 0;             /* True if some process holds a RESERVED lock */
56696
56697    /* Race condition here:  Another process might have been holding the
56698    ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
56699    ** call above, but then delete the journal and drop the lock before
56700    ** we get to the following sqlite3OsCheckReservedLock() call.  If that
56701    ** is the case, this routine might think there is a hot journal when
56702    ** in fact there is none.  This results in a false-positive which will
56703    ** be dealt with by the playback routine.  Ticket #3883.
56704    */
56705    rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
56706    if( rc==SQLITE_OK && !locked ){
56707      Pgno nPage;                 /* Number of pages in database file */
56708
56709      assert( pPager->tempFile==0 );
56710      rc = pagerPagecount(pPager, &nPage);
56711      if( rc==SQLITE_OK ){
56712        /* If the database is zero pages in size, that means that either (1) the
56713        ** journal is a remnant from a prior database with the same name where
56714        ** the database file but not the journal was deleted, or (2) the initial
56715        ** transaction that populates a new database is being rolled back.
56716        ** In either case, the journal file can be deleted.  However, take care
56717        ** not to delete the journal file if it is already open due to
56718        ** journal_mode=PERSIST.
56719        */
56720        if( nPage==0 && !jrnlOpen ){
56721          sqlite3BeginBenignMalloc();
56722          if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
56723            sqlite3OsDelete(pVfs, pPager->zJournal, 0);
56724            if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
56725          }
56726          sqlite3EndBenignMalloc();
56727        }else{
56728          /* The journal file exists and no other connection has a reserved
56729          ** or greater lock on the database file. Now check that there is
56730          ** at least one non-zero bytes at the start of the journal file.
56731          ** If there is, then we consider this journal to be hot. If not,
56732          ** it can be ignored.
56733          */
56734          if( !jrnlOpen ){
56735            int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
56736            rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
56737          }
56738          if( rc==SQLITE_OK ){
56739            u8 first = 0;
56740            rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
56741            if( rc==SQLITE_IOERR_SHORT_READ ){
56742              rc = SQLITE_OK;
56743            }
56744            if( !jrnlOpen ){
56745              sqlite3OsClose(pPager->jfd);
56746            }
56747            *pExists = (first!=0);
56748          }else if( rc==SQLITE_CANTOPEN ){
56749            /* If we cannot open the rollback journal file in order to see if
56750            ** it has a zero header, that might be due to an I/O error, or
56751            ** it might be due to the race condition described above and in
56752            ** ticket #3883.  Either way, assume that the journal is hot.
56753            ** This might be a false positive.  But if it is, then the
56754            ** automatic journal playback and recovery mechanism will deal
56755            ** with it under an EXCLUSIVE lock where we do not need to
56756            ** worry so much with race conditions.
56757            */
56758            *pExists = 1;
56759            rc = SQLITE_OK;
56760          }
56761        }
56762      }
56763    }
56764  }
56765
56766  return rc;
56767}
56768
56769/*
56770** This function is called to obtain a shared lock on the database file.
56771** It is illegal to call sqlite3PagerGet() until after this function
56772** has been successfully called. If a shared-lock is already held when
56773** this function is called, it is a no-op.
56774**
56775** The following operations are also performed by this function.
56776**
56777**   1) If the pager is currently in PAGER_OPEN state (no lock held
56778**      on the database file), then an attempt is made to obtain a
56779**      SHARED lock on the database file. Immediately after obtaining
56780**      the SHARED lock, the file-system is checked for a hot-journal,
56781**      which is played back if present. Following any hot-journal
56782**      rollback, the contents of the cache are validated by checking
56783**      the 'change-counter' field of the database file header and
56784**      discarded if they are found to be invalid.
56785**
56786**   2) If the pager is running in exclusive-mode, and there are currently
56787**      no outstanding references to any pages, and is in the error state,
56788**      then an attempt is made to clear the error state by discarding
56789**      the contents of the page cache and rolling back any open journal
56790**      file.
56791**
56792** If everything is successful, SQLITE_OK is returned. If an IO error
56793** occurs while locking the database, checking for a hot-journal file or
56794** rolling back a journal file, the IO error code is returned.
56795*/
56796SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
56797  int rc = SQLITE_OK;                /* Return code */
56798
56799  /* This routine is only called from b-tree and only when there are no
56800  ** outstanding pages. This implies that the pager state should either
56801  ** be OPEN or READER. READER is only possible if the pager is or was in
56802  ** exclusive access mode.  */
56803  assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
56804  assert( assert_pager_state(pPager) );
56805  assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
56806  assert( pPager->errCode==SQLITE_OK );
56807
56808  if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
56809    int bHotJournal = 1;          /* True if there exists a hot journal-file */
56810
56811    assert( !MEMDB );
56812    assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
56813
56814    rc = pager_wait_on_lock(pPager, SHARED_LOCK);
56815    if( rc!=SQLITE_OK ){
56816      assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
56817      goto failed;
56818    }
56819
56820    /* If a journal file exists, and there is no RESERVED lock on the
56821    ** database file, then it either needs to be played back or deleted.
56822    */
56823    if( pPager->eLock<=SHARED_LOCK ){
56824      rc = hasHotJournal(pPager, &bHotJournal);
56825    }
56826    if( rc!=SQLITE_OK ){
56827      goto failed;
56828    }
56829    if( bHotJournal ){
56830      if( pPager->readOnly ){
56831        rc = SQLITE_READONLY_ROLLBACK;
56832        goto failed;
56833      }
56834
56835      /* Get an EXCLUSIVE lock on the database file. At this point it is
56836      ** important that a RESERVED lock is not obtained on the way to the
56837      ** EXCLUSIVE lock. If it were, another process might open the
56838      ** database file, detect the RESERVED lock, and conclude that the
56839      ** database is safe to read while this process is still rolling the
56840      ** hot-journal back.
56841      **
56842      ** Because the intermediate RESERVED lock is not requested, any
56843      ** other process attempting to access the database file will get to
56844      ** this point in the code and fail to obtain its own EXCLUSIVE lock
56845      ** on the database file.
56846      **
56847      ** Unless the pager is in locking_mode=exclusive mode, the lock is
56848      ** downgraded to SHARED_LOCK before this function returns.
56849      */
56850      rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
56851      if( rc!=SQLITE_OK ){
56852        goto failed;
56853      }
56854
56855      /* If it is not already open and the file exists on disk, open the
56856      ** journal for read/write access. Write access is required because
56857      ** in exclusive-access mode the file descriptor will be kept open
56858      ** and possibly used for a transaction later on. Also, write-access
56859      ** is usually required to finalize the journal in journal_mode=persist
56860      ** mode (and also for journal_mode=truncate on some systems).
56861      **
56862      ** If the journal does not exist, it usually means that some
56863      ** other connection managed to get in and roll it back before
56864      ** this connection obtained the exclusive lock above. Or, it
56865      ** may mean that the pager was in the error-state when this
56866      ** function was called and the journal file does not exist.
56867      */
56868      if( !isOpen(pPager->jfd) ){
56869        sqlite3_vfs * const pVfs = pPager->pVfs;
56870        int bExists;              /* True if journal file exists */
56871        rc = sqlite3OsAccess(
56872            pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
56873        if( rc==SQLITE_OK && bExists ){
56874          int fout = 0;
56875          int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
56876          assert( !pPager->tempFile );
56877          rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
56878          assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
56879          if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
56880            rc = SQLITE_CANTOPEN_BKPT;
56881            sqlite3OsClose(pPager->jfd);
56882          }
56883        }
56884      }
56885
56886      /* Playback and delete the journal.  Drop the database write
56887      ** lock and reacquire the read lock. Purge the cache before
56888      ** playing back the hot-journal so that we don't end up with
56889      ** an inconsistent cache.  Sync the hot journal before playing
56890      ** it back since the process that crashed and left the hot journal
56891      ** probably did not sync it and we are required to always sync
56892      ** the journal before playing it back.
56893      */
56894      if( isOpen(pPager->jfd) ){
56895        assert( rc==SQLITE_OK );
56896        rc = pagerSyncHotJournal(pPager);
56897        if( rc==SQLITE_OK ){
56898          rc = pager_playback(pPager, !pPager->tempFile);
56899          pPager->eState = PAGER_OPEN;
56900        }
56901      }else if( !pPager->exclusiveMode ){
56902        pagerUnlockDb(pPager, SHARED_LOCK);
56903      }
56904
56905      if( rc!=SQLITE_OK ){
56906        /* This branch is taken if an error occurs while trying to open
56907        ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
56908        ** pager_unlock() routine will be called before returning to unlock
56909        ** the file. If the unlock attempt fails, then Pager.eLock must be
56910        ** set to UNKNOWN_LOCK (see the comment above the #define for
56911        ** UNKNOWN_LOCK above for an explanation).
56912        **
56913        ** In order to get pager_unlock() to do this, set Pager.eState to
56914        ** PAGER_ERROR now. This is not actually counted as a transition
56915        ** to ERROR state in the state diagram at the top of this file,
56916        ** since we know that the same call to pager_unlock() will very
56917        ** shortly transition the pager object to the OPEN state. Calling
56918        ** assert_pager_state() would fail now, as it should not be possible
56919        ** to be in ERROR state when there are zero outstanding page
56920        ** references.
56921        */
56922        pager_error(pPager, rc);
56923        goto failed;
56924      }
56925
56926      assert( pPager->eState==PAGER_OPEN );
56927      assert( (pPager->eLock==SHARED_LOCK)
56928           || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
56929      );
56930    }
56931
56932    if( !pPager->tempFile && pPager->hasHeldSharedLock ){
56933      /* The shared-lock has just been acquired then check to
56934      ** see if the database has been modified.  If the database has changed,
56935      ** flush the cache.  The hasHeldSharedLock flag prevents this from
56936      ** occurring on the very first access to a file, in order to save a
56937      ** single unnecessary sqlite3OsRead() call at the start-up.
56938      **
56939      ** Database changes are detected by looking at 15 bytes beginning
56940      ** at offset 24 into the file.  The first 4 of these 16 bytes are
56941      ** a 32-bit counter that is incremented with each change.  The
56942      ** other bytes change randomly with each file change when
56943      ** a codec is in use.
56944      **
56945      ** There is a vanishingly small chance that a change will not be
56946      ** detected.  The chance of an undetected change is so small that
56947      ** it can be neglected.
56948      */
56949      char dbFileVers[sizeof(pPager->dbFileVers)];
56950
56951      IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
56952      rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
56953      if( rc!=SQLITE_OK ){
56954        if( rc!=SQLITE_IOERR_SHORT_READ ){
56955          goto failed;
56956        }
56957        memset(dbFileVers, 0, sizeof(dbFileVers));
56958      }
56959
56960      if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
56961        pager_reset(pPager);
56962
56963        /* Unmap the database file. It is possible that external processes
56964        ** may have truncated the database file and then extended it back
56965        ** to its original size while this process was not holding a lock.
56966        ** In this case there may exist a Pager.pMap mapping that appears
56967        ** to be the right size but is not actually valid. Avoid this
56968        ** possibility by unmapping the db here. */
56969        if( USEFETCH(pPager) ){
56970          sqlite3OsUnfetch(pPager->fd, 0, 0);
56971        }
56972      }
56973    }
56974
56975    /* If there is a WAL file in the file-system, open this database in WAL
56976    ** mode. Otherwise, the following function call is a no-op.
56977    */
56978    rc = pagerOpenWalIfPresent(pPager);
56979#ifndef SQLITE_OMIT_WAL
56980    assert( pPager->pWal==0 || rc==SQLITE_OK );
56981#endif
56982  }
56983
56984  if( pagerUseWal(pPager) ){
56985    assert( rc==SQLITE_OK );
56986    rc = pagerBeginReadTransaction(pPager);
56987  }
56988
56989  if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
56990    rc = pagerPagecount(pPager, &pPager->dbSize);
56991  }
56992
56993 failed:
56994  if( rc!=SQLITE_OK ){
56995    assert( !MEMDB );
56996    pager_unlock(pPager);
56997    assert( pPager->eState==PAGER_OPEN );
56998  }else{
56999    pPager->eState = PAGER_READER;
57000    pPager->hasHeldSharedLock = 1;
57001  }
57002  return rc;
57003}
57004
57005/*
57006** If the reference count has reached zero, rollback any active
57007** transaction and unlock the pager.
57008**
57009** Except, in locking_mode=EXCLUSIVE when there is nothing to in
57010** the rollback journal, the unlock is not performed and there is
57011** nothing to rollback, so this routine is a no-op.
57012*/
57013static void pagerUnlockIfUnused(Pager *pPager){
57014  if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
57015    assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
57016    pagerUnlockAndRollback(pPager);
57017  }
57018}
57019
57020/*
57021** The page getter methods each try to acquire a reference to a
57022** page with page number pgno. If the requested reference is
57023** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
57024**
57025** There are different implementations of the getter method depending
57026** on the current state of the pager.
57027**
57028**     getPageNormal()         --  The normal getter
57029**     getPageError()          --  Used if the pager is in an error state
57030**     getPageMmap()           --  Used if memory-mapped I/O is enabled
57031**
57032** If the requested page is already in the cache, it is returned.
57033** Otherwise, a new page object is allocated and populated with data
57034** read from the database file. In some cases, the pcache module may
57035** choose not to allocate a new page object and may reuse an existing
57036** object with no outstanding references.
57037**
57038** The extra data appended to a page is always initialized to zeros the
57039** first time a page is loaded into memory. If the page requested is
57040** already in the cache when this function is called, then the extra
57041** data is left as it was when the page object was last used.
57042**
57043** If the database image is smaller than the requested page or if
57044** the flags parameter contains the PAGER_GET_NOCONTENT bit and the
57045** requested page is not already stored in the cache, then no
57046** actual disk read occurs. In this case the memory image of the
57047** page is initialized to all zeros.
57048**
57049** If PAGER_GET_NOCONTENT is true, it means that we do not care about
57050** the contents of the page. This occurs in two scenarios:
57051**
57052**   a) When reading a free-list leaf page from the database, and
57053**
57054**   b) When a savepoint is being rolled back and we need to load
57055**      a new page into the cache to be filled with the data read
57056**      from the savepoint journal.
57057**
57058** If PAGER_GET_NOCONTENT is true, then the data returned is zeroed instead
57059** of being read from the database. Additionally, the bits corresponding
57060** to pgno in Pager.pInJournal (bitvec of pages already written to the
57061** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
57062** savepoints are set. This means if the page is made writable at any
57063** point in the future, using a call to sqlite3PagerWrite(), its contents
57064** will not be journaled. This saves IO.
57065**
57066** The acquisition might fail for several reasons.  In all cases,
57067** an appropriate error code is returned and *ppPage is set to NULL.
57068**
57069** See also sqlite3PagerLookup().  Both this routine and Lookup() attempt
57070** to find a page in the in-memory cache first.  If the page is not already
57071** in memory, this routine goes to disk to read it in whereas Lookup()
57072** just returns 0.  This routine acquires a read-lock the first time it
57073** has to go to disk, and could also playback an old journal if necessary.
57074** Since Lookup() never goes to disk, it never has to deal with locks
57075** or journal files.
57076*/
57077static int getPageNormal(
57078  Pager *pPager,      /* The pager open on the database file */
57079  Pgno pgno,          /* Page number to fetch */
57080  DbPage **ppPage,    /* Write a pointer to the page here */
57081  int flags           /* PAGER_GET_XXX flags */
57082){
57083  int rc = SQLITE_OK;
57084  PgHdr *pPg;
57085  u8 noContent;                   /* True if PAGER_GET_NOCONTENT is set */
57086  sqlite3_pcache_page *pBase;
57087
57088  assert( pPager->errCode==SQLITE_OK );
57089  assert( pPager->eState>=PAGER_READER );
57090  assert( assert_pager_state(pPager) );
57091  assert( pPager->hasHeldSharedLock==1 );
57092
57093  if( pgno==0 ) return SQLITE_CORRUPT_BKPT;
57094  pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
57095  if( pBase==0 ){
57096    pPg = 0;
57097    rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
57098    if( rc!=SQLITE_OK ) goto pager_acquire_err;
57099    if( pBase==0 ){
57100      rc = SQLITE_NOMEM_BKPT;
57101      goto pager_acquire_err;
57102    }
57103  }
57104  pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
57105  assert( pPg==(*ppPage) );
57106  assert( pPg->pgno==pgno );
57107  assert( pPg->pPager==pPager || pPg->pPager==0 );
57108
57109  noContent = (flags & PAGER_GET_NOCONTENT)!=0;
57110  if( pPg->pPager && !noContent ){
57111    /* In this case the pcache already contains an initialized copy of
57112    ** the page. Return without further ado.  */
57113    assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
57114    pPager->aStat[PAGER_STAT_HIT]++;
57115    return SQLITE_OK;
57116
57117  }else{
57118    /* The pager cache has created a new page. Its content needs to
57119    ** be initialized. But first some error checks:
57120    **
57121    ** (1) The maximum page number is 2^31
57122    ** (2) Never try to fetch the locking page
57123    */
57124    if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
57125      rc = SQLITE_CORRUPT_BKPT;
57126      goto pager_acquire_err;
57127    }
57128
57129    pPg->pPager = pPager;
57130
57131    assert( !isOpen(pPager->fd) || !MEMDB );
57132    if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
57133      if( pgno>pPager->mxPgno ){
57134        rc = SQLITE_FULL;
57135        goto pager_acquire_err;
57136      }
57137      if( noContent ){
57138        /* Failure to set the bits in the InJournal bit-vectors is benign.
57139        ** It merely means that we might do some extra work to journal a
57140        ** page that does not need to be journaled.  Nevertheless, be sure
57141        ** to test the case where a malloc error occurs while trying to set
57142        ** a bit in a bit vector.
57143        */
57144        sqlite3BeginBenignMalloc();
57145        if( pgno<=pPager->dbOrigSize ){
57146          TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
57147          testcase( rc==SQLITE_NOMEM );
57148        }
57149        TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
57150        testcase( rc==SQLITE_NOMEM );
57151        sqlite3EndBenignMalloc();
57152      }
57153      memset(pPg->pData, 0, pPager->pageSize);
57154      IOTRACE(("ZERO %p %d\n", pPager, pgno));
57155    }else{
57156      assert( pPg->pPager==pPager );
57157      pPager->aStat[PAGER_STAT_MISS]++;
57158      rc = readDbPage(pPg);
57159      if( rc!=SQLITE_OK ){
57160        goto pager_acquire_err;
57161      }
57162    }
57163    pager_set_pagehash(pPg);
57164  }
57165  return SQLITE_OK;
57166
57167pager_acquire_err:
57168  assert( rc!=SQLITE_OK );
57169  if( pPg ){
57170    sqlite3PcacheDrop(pPg);
57171  }
57172  pagerUnlockIfUnused(pPager);
57173  *ppPage = 0;
57174  return rc;
57175}
57176
57177#if SQLITE_MAX_MMAP_SIZE>0
57178/* The page getter for when memory-mapped I/O is enabled */
57179static int getPageMMap(
57180  Pager *pPager,      /* The pager open on the database file */
57181  Pgno pgno,          /* Page number to fetch */
57182  DbPage **ppPage,    /* Write a pointer to the page here */
57183  int flags           /* PAGER_GET_XXX flags */
57184){
57185  int rc = SQLITE_OK;
57186  PgHdr *pPg = 0;
57187  u32 iFrame = 0;                 /* Frame to read from WAL file */
57188
57189  /* It is acceptable to use a read-only (mmap) page for any page except
57190  ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
57191  ** flag was specified by the caller. And so long as the db is not a
57192  ** temporary or in-memory database.  */
57193  const int bMmapOk = (pgno>1
57194   && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
57195  );
57196
57197  assert( USEFETCH(pPager) );
57198
57199  /* Optimization note:  Adding the "pgno<=1" term before "pgno==0" here
57200  ** allows the compiler optimizer to reuse the results of the "pgno>1"
57201  ** test in the previous statement, and avoid testing pgno==0 in the
57202  ** common case where pgno is large. */
57203  if( pgno<=1 && pgno==0 ){
57204    return SQLITE_CORRUPT_BKPT;
57205  }
57206  assert( pPager->eState>=PAGER_READER );
57207  assert( assert_pager_state(pPager) );
57208  assert( pPager->hasHeldSharedLock==1 );
57209  assert( pPager->errCode==SQLITE_OK );
57210
57211  if( bMmapOk && pagerUseWal(pPager) ){
57212    rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
57213    if( rc!=SQLITE_OK ){
57214      *ppPage = 0;
57215      return rc;
57216    }
57217  }
57218  if( bMmapOk && iFrame==0 ){
57219    void *pData = 0;
57220    rc = sqlite3OsFetch(pPager->fd,
57221        (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
57222    );
57223    if( rc==SQLITE_OK && pData ){
57224      if( pPager->eState>PAGER_READER || pPager->tempFile ){
57225        pPg = sqlite3PagerLookup(pPager, pgno);
57226      }
57227      if( pPg==0 ){
57228        rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
57229      }else{
57230        sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
57231      }
57232      if( pPg ){
57233        assert( rc==SQLITE_OK );
57234        *ppPage = pPg;
57235        return SQLITE_OK;
57236      }
57237    }
57238    if( rc!=SQLITE_OK ){
57239      *ppPage = 0;
57240      return rc;
57241    }
57242  }
57243  return getPageNormal(pPager, pgno, ppPage, flags);
57244}
57245#endif /* SQLITE_MAX_MMAP_SIZE>0 */
57246
57247/* The page getter method for when the pager is an error state */
57248static int getPageError(
57249  Pager *pPager,      /* The pager open on the database file */
57250  Pgno pgno,          /* Page number to fetch */
57251  DbPage **ppPage,    /* Write a pointer to the page here */
57252  int flags           /* PAGER_GET_XXX flags */
57253){
57254  UNUSED_PARAMETER(pgno);
57255  UNUSED_PARAMETER(flags);
57256  assert( pPager->errCode!=SQLITE_OK );
57257  *ppPage = 0;
57258  return pPager->errCode;
57259}
57260
57261
57262/* Dispatch all page fetch requests to the appropriate getter method.
57263*/
57264SQLITE_PRIVATE int sqlite3PagerGet(
57265  Pager *pPager,      /* The pager open on the database file */
57266  Pgno pgno,          /* Page number to fetch */
57267  DbPage **ppPage,    /* Write a pointer to the page here */
57268  int flags           /* PAGER_GET_XXX flags */
57269){
57270  return pPager->xGet(pPager, pgno, ppPage, flags);
57271}
57272
57273/*
57274** Acquire a page if it is already in the in-memory cache.  Do
57275** not read the page from disk.  Return a pointer to the page,
57276** or 0 if the page is not in cache.
57277**
57278** See also sqlite3PagerGet().  The difference between this routine
57279** and sqlite3PagerGet() is that _get() will go to the disk and read
57280** in the page if the page is not already in cache.  This routine
57281** returns NULL if the page is not in cache or if a disk I/O error
57282** has ever happened.
57283*/
57284SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
57285  sqlite3_pcache_page *pPage;
57286  assert( pPager!=0 );
57287  assert( pgno!=0 );
57288  assert( pPager->pPCache!=0 );
57289  pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
57290  assert( pPage==0 || pPager->hasHeldSharedLock );
57291  if( pPage==0 ) return 0;
57292  return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
57293}
57294
57295/*
57296** Release a page reference.
57297**
57298** The sqlite3PagerUnref() and sqlite3PagerUnrefNotNull() may only be
57299** used if we know that the page being released is not the last page.
57300** The btree layer always holds page1 open until the end, so these first
57301** to routines can be used to release any page other than BtShared.pPage1.
57302**
57303** Use sqlite3PagerUnrefPageOne() to release page1.  This latter routine
57304** checks the total number of outstanding pages and if the number of
57305** pages reaches zero it drops the database lock.
57306*/
57307SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage *pPg){
57308  TESTONLY( Pager *pPager = pPg->pPager; )
57309  assert( pPg!=0 );
57310  if( pPg->flags & PGHDR_MMAP ){
57311    assert( pPg->pgno!=1 );  /* Page1 is never memory mapped */
57312    pagerReleaseMapPage(pPg);
57313  }else{
57314    sqlite3PcacheRelease(pPg);
57315  }
57316  /* Do not use this routine to release the last reference to page1 */
57317  assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
57318}
57319SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
57320  if( pPg ) sqlite3PagerUnrefNotNull(pPg);
57321}
57322SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage *pPg){
57323  Pager *pPager;
57324  assert( pPg!=0 );
57325  assert( pPg->pgno==1 );
57326  assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
57327  pPager = pPg->pPager;
57328  sqlite3PcacheRelease(pPg);
57329  pagerUnlockIfUnused(pPager);
57330}
57331
57332/*
57333** This function is called at the start of every write transaction.
57334** There must already be a RESERVED or EXCLUSIVE lock on the database
57335** file when this routine is called.
57336**
57337** Open the journal file for pager pPager and write a journal header
57338** to the start of it. If there are active savepoints, open the sub-journal
57339** as well. This function is only used when the journal file is being
57340** opened to write a rollback log for a transaction. It is not used
57341** when opening a hot journal file to roll it back.
57342**
57343** If the journal file is already open (as it may be in exclusive mode),
57344** then this function just writes a journal header to the start of the
57345** already open file.
57346**
57347** Whether or not the journal file is opened by this function, the
57348** Pager.pInJournal bitvec structure is allocated.
57349**
57350** Return SQLITE_OK if everything is successful. Otherwise, return
57351** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
57352** an IO error code if opening or writing the journal file fails.
57353*/
57354static int pager_open_journal(Pager *pPager){
57355  int rc = SQLITE_OK;                        /* Return code */
57356  sqlite3_vfs * const pVfs = pPager->pVfs;   /* Local cache of vfs pointer */
57357
57358  assert( pPager->eState==PAGER_WRITER_LOCKED );
57359  assert( assert_pager_state(pPager) );
57360  assert( pPager->pInJournal==0 );
57361
57362  /* If already in the error state, this function is a no-op.  But on
57363  ** the other hand, this routine is never called if we are already in
57364  ** an error state. */
57365  if( NEVER(pPager->errCode) ) return pPager->errCode;
57366
57367  if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
57368    pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
57369    if( pPager->pInJournal==0 ){
57370      return SQLITE_NOMEM_BKPT;
57371    }
57372
57373    /* Open the journal file if it is not already open. */
57374    if( !isOpen(pPager->jfd) ){
57375      if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
57376        sqlite3MemJournalOpen(pPager->jfd);
57377      }else{
57378        int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
57379        int nSpill;
57380
57381        if( pPager->tempFile ){
57382          flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
57383          nSpill = sqlite3Config.nStmtSpill;
57384        }else{
57385          flags |= SQLITE_OPEN_MAIN_JOURNAL;
57386          nSpill = jrnlBufferSize(pPager);
57387        }
57388
57389        /* Verify that the database still has the same name as it did when
57390        ** it was originally opened. */
57391        rc = databaseIsUnmoved(pPager);
57392        if( rc==SQLITE_OK ){
57393          rc = sqlite3JournalOpen (
57394              pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
57395          );
57396        }
57397      }
57398      assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
57399    }
57400
57401
57402    /* Write the first journal header to the journal file and open
57403    ** the sub-journal if necessary.
57404    */
57405    if( rc==SQLITE_OK ){
57406      /* TODO: Check if all of these are really required. */
57407      pPager->nRec = 0;
57408      pPager->journalOff = 0;
57409      pPager->setMaster = 0;
57410      pPager->journalHdr = 0;
57411      rc = writeJournalHdr(pPager);
57412    }
57413  }
57414
57415  if( rc!=SQLITE_OK ){
57416    sqlite3BitvecDestroy(pPager->pInJournal);
57417    pPager->pInJournal = 0;
57418  }else{
57419    assert( pPager->eState==PAGER_WRITER_LOCKED );
57420    pPager->eState = PAGER_WRITER_CACHEMOD;
57421  }
57422
57423  return rc;
57424}
57425
57426/*
57427** Begin a write-transaction on the specified pager object. If a
57428** write-transaction has already been opened, this function is a no-op.
57429**
57430** If the exFlag argument is false, then acquire at least a RESERVED
57431** lock on the database file. If exFlag is true, then acquire at least
57432** an EXCLUSIVE lock. If such a lock is already held, no locking
57433** functions need be called.
57434**
57435** If the subjInMemory argument is non-zero, then any sub-journal opened
57436** within this transaction will be opened as an in-memory file. This
57437** has no effect if the sub-journal is already opened (as it may be when
57438** running in exclusive mode) or if the transaction does not require a
57439** sub-journal. If the subjInMemory argument is zero, then any required
57440** sub-journal is implemented in-memory if pPager is an in-memory database,
57441** or using a temporary file otherwise.
57442*/
57443SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
57444  int rc = SQLITE_OK;
57445
57446  if( pPager->errCode ) return pPager->errCode;
57447  assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
57448  pPager->subjInMemory = (u8)subjInMemory;
57449
57450  if( ALWAYS(pPager->eState==PAGER_READER) ){
57451    assert( pPager->pInJournal==0 );
57452
57453    if( pagerUseWal(pPager) ){
57454      /* If the pager is configured to use locking_mode=exclusive, and an
57455      ** exclusive lock on the database is not already held, obtain it now.
57456      */
57457      if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
57458        rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
57459        if( rc!=SQLITE_OK ){
57460          return rc;
57461        }
57462        (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
57463      }
57464
57465      /* Grab the write lock on the log file. If successful, upgrade to
57466      ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
57467      ** The busy-handler is not invoked if another connection already
57468      ** holds the write-lock. If possible, the upper layer will call it.
57469      */
57470      rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
57471    }else{
57472      /* Obtain a RESERVED lock on the database file. If the exFlag parameter
57473      ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
57474      ** busy-handler callback can be used when upgrading to the EXCLUSIVE
57475      ** lock, but not when obtaining the RESERVED lock.
57476      */
57477      rc = pagerLockDb(pPager, RESERVED_LOCK);
57478      if( rc==SQLITE_OK && exFlag ){
57479        rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
57480      }
57481    }
57482
57483    if( rc==SQLITE_OK ){
57484      /* Change to WRITER_LOCKED state.
57485      **
57486      ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
57487      ** when it has an open transaction, but never to DBMOD or FINISHED.
57488      ** This is because in those states the code to roll back savepoint
57489      ** transactions may copy data from the sub-journal into the database
57490      ** file as well as into the page cache. Which would be incorrect in
57491      ** WAL mode.
57492      */
57493      pPager->eState = PAGER_WRITER_LOCKED;
57494      pPager->dbHintSize = pPager->dbSize;
57495      pPager->dbFileSize = pPager->dbSize;
57496      pPager->dbOrigSize = pPager->dbSize;
57497      pPager->journalOff = 0;
57498    }
57499
57500    assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
57501    assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
57502    assert( assert_pager_state(pPager) );
57503  }
57504
57505  PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
57506  return rc;
57507}
57508
57509/*
57510** Write page pPg onto the end of the rollback journal.
57511*/
57512static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){
57513  Pager *pPager = pPg->pPager;
57514  int rc;
57515  u32 cksum;
57516  char *pData2;
57517  i64 iOff = pPager->journalOff;
57518
57519  /* We should never write to the journal file the page that
57520  ** contains the database locks.  The following assert verifies
57521  ** that we do not. */
57522  assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
57523
57524  assert( pPager->journalHdr<=pPager->journalOff );
57525  pData2 = pPg->pData;
57526  cksum = pager_cksum(pPager, (u8*)pData2);
57527
57528  /* Even if an IO or diskfull error occurs while journalling the
57529  ** page in the block above, set the need-sync flag for the page.
57530  ** Otherwise, when the transaction is rolled back, the logic in
57531  ** playback_one_page() will think that the page needs to be restored
57532  ** in the database file. And if an IO error occurs while doing so,
57533  ** then corruption may follow.
57534  */
57535  pPg->flags |= PGHDR_NEED_SYNC;
57536
57537  rc = write32bits(pPager->jfd, iOff, pPg->pgno);
57538  if( rc!=SQLITE_OK ) return rc;
57539  rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
57540  if( rc!=SQLITE_OK ) return rc;
57541  rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
57542  if( rc!=SQLITE_OK ) return rc;
57543
57544  IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
57545           pPager->journalOff, pPager->pageSize));
57546  PAGER_INCR(sqlite3_pager_writej_count);
57547  PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
57548       PAGERID(pPager), pPg->pgno,
57549       ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
57550
57551  pPager->journalOff += 8 + pPager->pageSize;
57552  pPager->nRec++;
57553  assert( pPager->pInJournal!=0 );
57554  rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
57555  testcase( rc==SQLITE_NOMEM );
57556  assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
57557  rc |= addToSavepointBitvecs(pPager, pPg->pgno);
57558  assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
57559  return rc;
57560}
57561
57562/*
57563** Mark a single data page as writeable. The page is written into the
57564** main journal or sub-journal as required. If the page is written into
57565** one of the journals, the corresponding bit is set in the
57566** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
57567** of any open savepoints as appropriate.
57568*/
57569static int pager_write(PgHdr *pPg){
57570  Pager *pPager = pPg->pPager;
57571  int rc = SQLITE_OK;
57572
57573  /* This routine is not called unless a write-transaction has already
57574  ** been started. The journal file may or may not be open at this point.
57575  ** It is never called in the ERROR state.
57576  */
57577  assert( pPager->eState==PAGER_WRITER_LOCKED
57578       || pPager->eState==PAGER_WRITER_CACHEMOD
57579       || pPager->eState==PAGER_WRITER_DBMOD
57580  );
57581  assert( assert_pager_state(pPager) );
57582  assert( pPager->errCode==0 );
57583  assert( pPager->readOnly==0 );
57584  CHECK_PAGE(pPg);
57585
57586  /* The journal file needs to be opened. Higher level routines have already
57587  ** obtained the necessary locks to begin the write-transaction, but the
57588  ** rollback journal might not yet be open. Open it now if this is the case.
57589  **
57590  ** This is done before calling sqlite3PcacheMakeDirty() on the page.
57591  ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
57592  ** an error might occur and the pager would end up in WRITER_LOCKED state
57593  ** with pages marked as dirty in the cache.
57594  */
57595  if( pPager->eState==PAGER_WRITER_LOCKED ){
57596    rc = pager_open_journal(pPager);
57597    if( rc!=SQLITE_OK ) return rc;
57598  }
57599  assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
57600  assert( assert_pager_state(pPager) );
57601
57602  /* Mark the page that is about to be modified as dirty. */
57603  sqlite3PcacheMakeDirty(pPg);
57604
57605  /* If a rollback journal is in use, them make sure the page that is about
57606  ** to change is in the rollback journal, or if the page is a new page off
57607  ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC.
57608  */
57609  assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
57610  if( pPager->pInJournal!=0
57611   && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
57612  ){
57613    assert( pagerUseWal(pPager)==0 );
57614    if( pPg->pgno<=pPager->dbOrigSize ){
57615      rc = pagerAddPageToRollbackJournal(pPg);
57616      if( rc!=SQLITE_OK ){
57617        return rc;
57618      }
57619    }else{
57620      if( pPager->eState!=PAGER_WRITER_DBMOD ){
57621        pPg->flags |= PGHDR_NEED_SYNC;
57622      }
57623      PAGERTRACE(("APPEND %d page %d needSync=%d\n",
57624              PAGERID(pPager), pPg->pgno,
57625             ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
57626    }
57627  }
57628
57629  /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
57630  ** and before writing the page into the rollback journal.  Wait until now,
57631  ** after the page has been successfully journalled, before setting the
57632  ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified.
57633  */
57634  pPg->flags |= PGHDR_WRITEABLE;
57635
57636  /* If the statement journal is open and the page is not in it,
57637  ** then write the page into the statement journal.
57638  */
57639  if( pPager->nSavepoint>0 ){
57640    rc = subjournalPageIfRequired(pPg);
57641  }
57642
57643  /* Update the database size and return. */
57644  if( pPager->dbSize<pPg->pgno ){
57645    pPager->dbSize = pPg->pgno;
57646  }
57647  return rc;
57648}
57649
57650/*
57651** This is a variant of sqlite3PagerWrite() that runs when the sector size
57652** is larger than the page size.  SQLite makes the (reasonable) assumption that
57653** all bytes of a sector are written together by hardware.  Hence, all bytes of
57654** a sector need to be journalled in case of a power loss in the middle of
57655** a write.
57656**
57657** Usually, the sector size is less than or equal to the page size, in which
57658** case pages can be individually written.  This routine only runs in the
57659** exceptional case where the page size is smaller than the sector size.
57660*/
57661static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
57662  int rc = SQLITE_OK;          /* Return code */
57663  Pgno nPageCount;             /* Total number of pages in database file */
57664  Pgno pg1;                    /* First page of the sector pPg is located on. */
57665  int nPage = 0;               /* Number of pages starting at pg1 to journal */
57666  int ii;                      /* Loop counter */
57667  int needSync = 0;            /* True if any page has PGHDR_NEED_SYNC */
57668  Pager *pPager = pPg->pPager; /* The pager that owns pPg */
57669  Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
57670
57671  /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
57672  ** a journal header to be written between the pages journaled by
57673  ** this function.
57674  */
57675  assert( !MEMDB );
57676  assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
57677  pPager->doNotSpill |= SPILLFLAG_NOSYNC;
57678
57679  /* This trick assumes that both the page-size and sector-size are
57680  ** an integer power of 2. It sets variable pg1 to the identifier
57681  ** of the first page of the sector pPg is located on.
57682  */
57683  pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
57684
57685  nPageCount = pPager->dbSize;
57686  if( pPg->pgno>nPageCount ){
57687    nPage = (pPg->pgno - pg1)+1;
57688  }else if( (pg1+nPagePerSector-1)>nPageCount ){
57689    nPage = nPageCount+1-pg1;
57690  }else{
57691    nPage = nPagePerSector;
57692  }
57693  assert(nPage>0);
57694  assert(pg1<=pPg->pgno);
57695  assert((pg1+nPage)>pPg->pgno);
57696
57697  for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
57698    Pgno pg = pg1+ii;
57699    PgHdr *pPage;
57700    if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
57701      if( pg!=PAGER_MJ_PGNO(pPager) ){
57702        rc = sqlite3PagerGet(pPager, pg, &pPage, 0);
57703        if( rc==SQLITE_OK ){
57704          rc = pager_write(pPage);
57705          if( pPage->flags&PGHDR_NEED_SYNC ){
57706            needSync = 1;
57707          }
57708          sqlite3PagerUnrefNotNull(pPage);
57709        }
57710      }
57711    }else if( (pPage = sqlite3PagerLookup(pPager, pg))!=0 ){
57712      if( pPage->flags&PGHDR_NEED_SYNC ){
57713        needSync = 1;
57714      }
57715      sqlite3PagerUnrefNotNull(pPage);
57716    }
57717  }
57718
57719  /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
57720  ** starting at pg1, then it needs to be set for all of them. Because
57721  ** writing to any of these nPage pages may damage the others, the
57722  ** journal file must contain sync()ed copies of all of them
57723  ** before any of them can be written out to the database file.
57724  */
57725  if( rc==SQLITE_OK && needSync ){
57726    assert( !MEMDB );
57727    for(ii=0; ii<nPage; ii++){
57728      PgHdr *pPage = sqlite3PagerLookup(pPager, pg1+ii);
57729      if( pPage ){
57730        pPage->flags |= PGHDR_NEED_SYNC;
57731        sqlite3PagerUnrefNotNull(pPage);
57732      }
57733    }
57734  }
57735
57736  assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
57737  pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
57738  return rc;
57739}
57740
57741/*
57742** Mark a data page as writeable. This routine must be called before
57743** making changes to a page. The caller must check the return value
57744** of this function and be careful not to change any page data unless
57745** this routine returns SQLITE_OK.
57746**
57747** The difference between this function and pager_write() is that this
57748** function also deals with the special case where 2 or more pages
57749** fit on a single disk sector. In this case all co-resident pages
57750** must have been written to the journal file before returning.
57751**
57752** If an error occurs, SQLITE_NOMEM or an IO error code is returned
57753** as appropriate. Otherwise, SQLITE_OK.
57754*/
57755SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){
57756  Pager *pPager = pPg->pPager;
57757  assert( (pPg->flags & PGHDR_MMAP)==0 );
57758  assert( pPager->eState>=PAGER_WRITER_LOCKED );
57759  assert( assert_pager_state(pPager) );
57760  if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
57761    if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
57762    return SQLITE_OK;
57763  }else if( pPager->errCode ){
57764    return pPager->errCode;
57765  }else if( pPager->sectorSize > (u32)pPager->pageSize ){
57766    assert( pPager->tempFile==0 );
57767    return pagerWriteLargeSector(pPg);
57768  }else{
57769    return pager_write(pPg);
57770  }
57771}
57772
57773/*
57774** Return TRUE if the page given in the argument was previously passed
57775** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
57776** to change the content of the page.
57777*/
57778#ifndef NDEBUG
57779SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
57780  return pPg->flags & PGHDR_WRITEABLE;
57781}
57782#endif
57783
57784/*
57785** A call to this routine tells the pager that it is not necessary to
57786** write the information on page pPg back to the disk, even though
57787** that page might be marked as dirty.  This happens, for example, when
57788** the page has been added as a leaf of the freelist and so its
57789** content no longer matters.
57790**
57791** The overlying software layer calls this routine when all of the data
57792** on the given page is unused. The pager marks the page as clean so
57793** that it does not get written to disk.
57794**
57795** Tests show that this optimization can quadruple the speed of large
57796** DELETE operations.
57797**
57798** This optimization cannot be used with a temp-file, as the page may
57799** have been dirty at the start of the transaction. In that case, if
57800** memory pressure forces page pPg out of the cache, the data does need
57801** to be written out to disk so that it may be read back in if the
57802** current transaction is rolled back.
57803*/
57804SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
57805  Pager *pPager = pPg->pPager;
57806  if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
57807    PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
57808    IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
57809    pPg->flags |= PGHDR_DONT_WRITE;
57810    pPg->flags &= ~PGHDR_WRITEABLE;
57811    testcase( pPg->flags & PGHDR_NEED_SYNC );
57812    pager_set_pagehash(pPg);
57813  }
57814}
57815
57816/*
57817** This routine is called to increment the value of the database file
57818** change-counter, stored as a 4-byte big-endian integer starting at
57819** byte offset 24 of the pager file.  The secondary change counter at
57820** 92 is also updated, as is the SQLite version number at offset 96.
57821**
57822** But this only happens if the pPager->changeCountDone flag is false.
57823** To avoid excess churning of page 1, the update only happens once.
57824** See also the pager_write_changecounter() routine that does an
57825** unconditional update of the change counters.
57826**
57827** If the isDirectMode flag is zero, then this is done by calling
57828** sqlite3PagerWrite() on page 1, then modifying the contents of the
57829** page data. In this case the file will be updated when the current
57830** transaction is committed.
57831**
57832** The isDirectMode flag may only be non-zero if the library was compiled
57833** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
57834** if isDirect is non-zero, then the database file is updated directly
57835** by writing an updated version of page 1 using a call to the
57836** sqlite3OsWrite() function.
57837*/
57838static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
57839  int rc = SQLITE_OK;
57840
57841  assert( pPager->eState==PAGER_WRITER_CACHEMOD
57842       || pPager->eState==PAGER_WRITER_DBMOD
57843  );
57844  assert( assert_pager_state(pPager) );
57845
57846  /* Declare and initialize constant integer 'isDirect'. If the
57847  ** atomic-write optimization is enabled in this build, then isDirect
57848  ** is initialized to the value passed as the isDirectMode parameter
57849  ** to this function. Otherwise, it is always set to zero.
57850  **
57851  ** The idea is that if the atomic-write optimization is not
57852  ** enabled at compile time, the compiler can omit the tests of
57853  ** 'isDirect' below, as well as the block enclosed in the
57854  ** "if( isDirect )" condition.
57855  */
57856#ifndef SQLITE_ENABLE_ATOMIC_WRITE
57857# define DIRECT_MODE 0
57858  assert( isDirectMode==0 );
57859  UNUSED_PARAMETER(isDirectMode);
57860#else
57861# define DIRECT_MODE isDirectMode
57862#endif
57863
57864  if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
57865    PgHdr *pPgHdr;                /* Reference to page 1 */
57866
57867    assert( !pPager->tempFile && isOpen(pPager->fd) );
57868
57869    /* Open page 1 of the file for writing. */
57870    rc = sqlite3PagerGet(pPager, 1, &pPgHdr, 0);
57871    assert( pPgHdr==0 || rc==SQLITE_OK );
57872
57873    /* If page one was fetched successfully, and this function is not
57874    ** operating in direct-mode, make page 1 writable.  When not in
57875    ** direct mode, page 1 is always held in cache and hence the PagerGet()
57876    ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
57877    */
57878    if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
57879      rc = sqlite3PagerWrite(pPgHdr);
57880    }
57881
57882    if( rc==SQLITE_OK ){
57883      /* Actually do the update of the change counter */
57884      pager_write_changecounter(pPgHdr);
57885
57886      /* If running in direct mode, write the contents of page 1 to the file. */
57887      if( DIRECT_MODE ){
57888        const void *zBuf;
57889        assert( pPager->dbFileSize>0 );
57890        zBuf = pPgHdr->pData;
57891        if( rc==SQLITE_OK ){
57892          rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
57893          pPager->aStat[PAGER_STAT_WRITE]++;
57894        }
57895        if( rc==SQLITE_OK ){
57896          /* Update the pager's copy of the change-counter. Otherwise, the
57897          ** next time a read transaction is opened the cache will be
57898          ** flushed (as the change-counter values will not match).  */
57899          const void *pCopy = (const void *)&((const char *)zBuf)[24];
57900          memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
57901          pPager->changeCountDone = 1;
57902        }
57903      }else{
57904        pPager->changeCountDone = 1;
57905      }
57906    }
57907
57908    /* Release the page reference. */
57909    sqlite3PagerUnref(pPgHdr);
57910  }
57911  return rc;
57912}
57913
57914/*
57915** Sync the database file to disk. This is a no-op for in-memory databases
57916** or pages with the Pager.noSync flag set.
57917**
57918** If successful, or if called on a pager for which it is a no-op, this
57919** function returns SQLITE_OK. Otherwise, an IO error code is returned.
57920*/
57921SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){
57922  int rc = SQLITE_OK;
57923  void *pArg = (void*)zMaster;
57924  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
57925  if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
57926  if( rc==SQLITE_OK && !pPager->noSync ){
57927    assert( !MEMDB );
57928    rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
57929  }
57930  return rc;
57931}
57932
57933/*
57934** This function may only be called while a write-transaction is active in
57935** rollback. If the connection is in WAL mode, this call is a no-op.
57936** Otherwise, if the connection does not already have an EXCLUSIVE lock on
57937** the database file, an attempt is made to obtain one.
57938**
57939** If the EXCLUSIVE lock is already held or the attempt to obtain it is
57940** successful, or the connection is in WAL mode, SQLITE_OK is returned.
57941** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
57942** returned.
57943*/
57944SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
57945  int rc = pPager->errCode;
57946  assert( assert_pager_state(pPager) );
57947  if( rc==SQLITE_OK ){
57948    assert( pPager->eState==PAGER_WRITER_CACHEMOD
57949         || pPager->eState==PAGER_WRITER_DBMOD
57950         || pPager->eState==PAGER_WRITER_LOCKED
57951    );
57952    assert( assert_pager_state(pPager) );
57953    if( 0==pagerUseWal(pPager) ){
57954      rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
57955    }
57956  }
57957  return rc;
57958}
57959
57960/*
57961** Sync the database file for the pager pPager. zMaster points to the name
57962** of a master journal file that should be written into the individual
57963** journal file. zMaster may be NULL, which is interpreted as no master
57964** journal (a single database transaction).
57965**
57966** This routine ensures that:
57967**
57968**   * The database file change-counter is updated,
57969**   * the journal is synced (unless the atomic-write optimization is used),
57970**   * all dirty pages are written to the database file,
57971**   * the database file is truncated (if required), and
57972**   * the database file synced.
57973**
57974** The only thing that remains to commit the transaction is to finalize
57975** (delete, truncate or zero the first part of) the journal file (or
57976** delete the master journal file if specified).
57977**
57978** Note that if zMaster==NULL, this does not overwrite a previous value
57979** passed to an sqlite3PagerCommitPhaseOne() call.
57980**
57981** If the final parameter - noSync - is true, then the database file itself
57982** is not synced. The caller must call sqlite3PagerSync() directly to
57983** sync the database file before calling CommitPhaseTwo() to delete the
57984** journal file in this case.
57985*/
57986SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
57987  Pager *pPager,                  /* Pager object */
57988  const char *zMaster,            /* If not NULL, the master journal name */
57989  int noSync                      /* True to omit the xSync on the db file */
57990){
57991  int rc = SQLITE_OK;             /* Return code */
57992
57993  assert( pPager->eState==PAGER_WRITER_LOCKED
57994       || pPager->eState==PAGER_WRITER_CACHEMOD
57995       || pPager->eState==PAGER_WRITER_DBMOD
57996       || pPager->eState==PAGER_ERROR
57997  );
57998  assert( assert_pager_state(pPager) );
57999
58000  /* If a prior error occurred, report that error again. */
58001  if( NEVER(pPager->errCode) ) return pPager->errCode;
58002
58003  /* Provide the ability to easily simulate an I/O error during testing */
58004  if( sqlite3FaultSim(400) ) return SQLITE_IOERR;
58005
58006  PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
58007      pPager->zFilename, zMaster, pPager->dbSize));
58008
58009  /* If no database changes have been made, return early. */
58010  if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
58011
58012  assert( MEMDB==0 || pPager->tempFile );
58013  assert( isOpen(pPager->fd) || pPager->tempFile );
58014  if( 0==pagerFlushOnCommit(pPager, 1) ){
58015    /* If this is an in-memory db, or no pages have been written to, or this
58016    ** function has already been called, it is mostly a no-op.  However, any
58017    ** backup in progress needs to be restarted.  */
58018    sqlite3BackupRestart(pPager->pBackup);
58019  }else{
58020    PgHdr *pList;
58021    if( pagerUseWal(pPager) ){
58022      PgHdr *pPageOne = 0;
58023      pList = sqlite3PcacheDirtyList(pPager->pPCache);
58024      if( pList==0 ){
58025        /* Must have at least one page for the WAL commit flag.
58026        ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
58027        rc = sqlite3PagerGet(pPager, 1, &pPageOne, 0);
58028        pList = pPageOne;
58029        pList->pDirty = 0;
58030      }
58031      assert( rc==SQLITE_OK );
58032      if( ALWAYS(pList) ){
58033        rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
58034      }
58035      sqlite3PagerUnref(pPageOne);
58036      if( rc==SQLITE_OK ){
58037        sqlite3PcacheCleanAll(pPager->pPCache);
58038      }
58039    }else{
58040      /* The bBatch boolean is true if the batch-atomic-write commit method
58041      ** should be used.  No rollback journal is created if batch-atomic-write
58042      ** is enabled.
58043      */
58044#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
58045      sqlite3_file *fd = pPager->fd;
58046      int bBatch = zMaster==0    /* An SQLITE_IOCAP_BATCH_ATOMIC commit */
58047        && (sqlite3OsDeviceCharacteristics(fd) & SQLITE_IOCAP_BATCH_ATOMIC)
58048        && !pPager->noSync
58049        && sqlite3JournalIsInMemory(pPager->jfd);
58050#else
58051#     define bBatch 0
58052#endif
58053
58054#ifdef SQLITE_ENABLE_ATOMIC_WRITE
58055      /* The following block updates the change-counter. Exactly how it
58056      ** does this depends on whether or not the atomic-update optimization
58057      ** was enabled at compile time, and if this transaction meets the
58058      ** runtime criteria to use the operation:
58059      **
58060      **    * The file-system supports the atomic-write property for
58061      **      blocks of size page-size, and
58062      **    * This commit is not part of a multi-file transaction, and
58063      **    * Exactly one page has been modified and store in the journal file.
58064      **
58065      ** If the optimization was not enabled at compile time, then the
58066      ** pager_incr_changecounter() function is called to update the change
58067      ** counter in 'indirect-mode'. If the optimization is compiled in but
58068      ** is not applicable to this transaction, call sqlite3JournalCreate()
58069      ** to make sure the journal file has actually been created, then call
58070      ** pager_incr_changecounter() to update the change-counter in indirect
58071      ** mode.
58072      **
58073      ** Otherwise, if the optimization is both enabled and applicable,
58074      ** then call pager_incr_changecounter() to update the change-counter
58075      ** in 'direct' mode. In this case the journal file will never be
58076      ** created for this transaction.
58077      */
58078      if( bBatch==0 ){
58079        PgHdr *pPg;
58080        assert( isOpen(pPager->jfd)
58081            || pPager->journalMode==PAGER_JOURNALMODE_OFF
58082            || pPager->journalMode==PAGER_JOURNALMODE_WAL
58083            );
58084        if( !zMaster && isOpen(pPager->jfd)
58085         && pPager->journalOff==jrnlBufferSize(pPager)
58086         && pPager->dbSize>=pPager->dbOrigSize
58087         && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
58088        ){
58089          /* Update the db file change counter via the direct-write method. The
58090          ** following call will modify the in-memory representation of page 1
58091          ** to include the updated change counter and then write page 1
58092          ** directly to the database file. Because of the atomic-write
58093          ** property of the host file-system, this is safe.
58094          */
58095          rc = pager_incr_changecounter(pPager, 1);
58096        }else{
58097          rc = sqlite3JournalCreate(pPager->jfd);
58098          if( rc==SQLITE_OK ){
58099            rc = pager_incr_changecounter(pPager, 0);
58100          }
58101        }
58102      }
58103#else  /* SQLITE_ENABLE_ATOMIC_WRITE */
58104#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
58105      if( zMaster ){
58106        rc = sqlite3JournalCreate(pPager->jfd);
58107        if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
58108        assert( bBatch==0 );
58109      }
58110#endif
58111      rc = pager_incr_changecounter(pPager, 0);
58112#endif /* !SQLITE_ENABLE_ATOMIC_WRITE */
58113      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
58114
58115      /* Write the master journal name into the journal file. If a master
58116      ** journal file name has already been written to the journal file,
58117      ** or if zMaster is NULL (no master journal), then this call is a no-op.
58118      */
58119      rc = writeMasterJournal(pPager, zMaster);
58120      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
58121
58122      /* Sync the journal file and write all dirty pages to the database.
58123      ** If the atomic-update optimization is being used, this sync will not
58124      ** create the journal file or perform any real IO.
58125      **
58126      ** Because the change-counter page was just modified, unless the
58127      ** atomic-update optimization is used it is almost certain that the
58128      ** journal requires a sync here. However, in locking_mode=exclusive
58129      ** on a system under memory pressure it is just possible that this is
58130      ** not the case. In this case it is likely enough that the redundant
58131      ** xSync() call will be changed to a no-op by the OS anyhow.
58132      */
58133      rc = syncJournal(pPager, 0);
58134      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
58135
58136      pList = sqlite3PcacheDirtyList(pPager->pPCache);
58137#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
58138      if( bBatch ){
58139        rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0);
58140        if( rc==SQLITE_OK ){
58141          rc = pager_write_pagelist(pPager, pList);
58142          if( rc==SQLITE_OK ){
58143            rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
58144          }
58145          if( rc!=SQLITE_OK ){
58146            sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
58147          }
58148        }
58149
58150        if( (rc&0xFF)==SQLITE_IOERR && rc!=SQLITE_IOERR_NOMEM ){
58151          rc = sqlite3JournalCreate(pPager->jfd);
58152          if( rc!=SQLITE_OK ){
58153            sqlite3OsClose(pPager->jfd);
58154            goto commit_phase_one_exit;
58155          }
58156          bBatch = 0;
58157        }else{
58158          sqlite3OsClose(pPager->jfd);
58159        }
58160      }
58161#endif /* SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
58162
58163      if( bBatch==0 ){
58164        rc = pager_write_pagelist(pPager, pList);
58165      }
58166      if( rc!=SQLITE_OK ){
58167        assert( rc!=SQLITE_IOERR_BLOCKED );
58168        goto commit_phase_one_exit;
58169      }
58170      sqlite3PcacheCleanAll(pPager->pPCache);
58171
58172      /* If the file on disk is smaller than the database image, use
58173      ** pager_truncate to grow the file here. This can happen if the database
58174      ** image was extended as part of the current transaction and then the
58175      ** last page in the db image moved to the free-list. In this case the
58176      ** last page is never written out to disk, leaving the database file
58177      ** undersized. Fix this now if it is the case.  */
58178      if( pPager->dbSize>pPager->dbFileSize ){
58179        Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
58180        assert( pPager->eState==PAGER_WRITER_DBMOD );
58181        rc = pager_truncate(pPager, nNew);
58182        if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
58183      }
58184
58185      /* Finally, sync the database file. */
58186      if( !noSync ){
58187        rc = sqlite3PagerSync(pPager, zMaster);
58188      }
58189      IOTRACE(("DBSYNC %p\n", pPager))
58190    }
58191  }
58192
58193commit_phase_one_exit:
58194  if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
58195    pPager->eState = PAGER_WRITER_FINISHED;
58196  }
58197  return rc;
58198}
58199
58200
58201/*
58202** When this function is called, the database file has been completely
58203** updated to reflect the changes made by the current transaction and
58204** synced to disk. The journal file still exists in the file-system
58205** though, and if a failure occurs at this point it will eventually
58206** be used as a hot-journal and the current transaction rolled back.
58207**
58208** This function finalizes the journal file, either by deleting,
58209** truncating or partially zeroing it, so that it cannot be used
58210** for hot-journal rollback. Once this is done the transaction is
58211** irrevocably committed.
58212**
58213** If an error occurs, an IO error code is returned and the pager
58214** moves into the error state. Otherwise, SQLITE_OK is returned.
58215*/
58216SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
58217  int rc = SQLITE_OK;                  /* Return code */
58218
58219  /* This routine should not be called if a prior error has occurred.
58220  ** But if (due to a coding error elsewhere in the system) it does get
58221  ** called, just return the same error code without doing anything. */
58222  if( NEVER(pPager->errCode) ) return pPager->errCode;
58223  pPager->iDataVersion++;
58224
58225  assert( pPager->eState==PAGER_WRITER_LOCKED
58226       || pPager->eState==PAGER_WRITER_FINISHED
58227       || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
58228  );
58229  assert( assert_pager_state(pPager) );
58230
58231  /* An optimization. If the database was not actually modified during
58232  ** this transaction, the pager is running in exclusive-mode and is
58233  ** using persistent journals, then this function is a no-op.
58234  **
58235  ** The start of the journal file currently contains a single journal
58236  ** header with the nRec field set to 0. If such a journal is used as
58237  ** a hot-journal during hot-journal rollback, 0 changes will be made
58238  ** to the database file. So there is no need to zero the journal
58239  ** header. Since the pager is in exclusive mode, there is no need
58240  ** to drop any locks either.
58241  */
58242  if( pPager->eState==PAGER_WRITER_LOCKED
58243   && pPager->exclusiveMode
58244   && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
58245  ){
58246    assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
58247    pPager->eState = PAGER_READER;
58248    return SQLITE_OK;
58249  }
58250
58251  PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
58252  rc = pager_end_transaction(pPager, pPager->setMaster, 1);
58253  return pager_error(pPager, rc);
58254}
58255
58256/*
58257** If a write transaction is open, then all changes made within the
58258** transaction are reverted and the current write-transaction is closed.
58259** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
58260** state if an error occurs.
58261**
58262** If the pager is already in PAGER_ERROR state when this function is called,
58263** it returns Pager.errCode immediately. No work is performed in this case.
58264**
58265** Otherwise, in rollback mode, this function performs two functions:
58266**
58267**   1) It rolls back the journal file, restoring all database file and
58268**      in-memory cache pages to the state they were in when the transaction
58269**      was opened, and
58270**
58271**   2) It finalizes the journal file, so that it is not used for hot
58272**      rollback at any point in the future.
58273**
58274** Finalization of the journal file (task 2) is only performed if the
58275** rollback is successful.
58276**
58277** In WAL mode, all cache-entries containing data modified within the
58278** current transaction are either expelled from the cache or reverted to
58279** their pre-transaction state by re-reading data from the database or
58280** WAL files. The WAL transaction is then closed.
58281*/
58282SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
58283  int rc = SQLITE_OK;                  /* Return code */
58284  PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
58285
58286  /* PagerRollback() is a no-op if called in READER or OPEN state. If
58287  ** the pager is already in the ERROR state, the rollback is not
58288  ** attempted here. Instead, the error code is returned to the caller.
58289  */
58290  assert( assert_pager_state(pPager) );
58291  if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
58292  if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
58293
58294  if( pagerUseWal(pPager) ){
58295    int rc2;
58296    rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
58297    rc2 = pager_end_transaction(pPager, pPager->setMaster, 0);
58298    if( rc==SQLITE_OK ) rc = rc2;
58299  }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
58300    int eState = pPager->eState;
58301    rc = pager_end_transaction(pPager, 0, 0);
58302    if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
58303      /* This can happen using journal_mode=off. Move the pager to the error
58304      ** state to indicate that the contents of the cache may not be trusted.
58305      ** Any active readers will get SQLITE_ABORT.
58306      */
58307      pPager->errCode = SQLITE_ABORT;
58308      pPager->eState = PAGER_ERROR;
58309      setGetterMethod(pPager);
58310      return rc;
58311    }
58312  }else{
58313    rc = pager_playback(pPager, 0);
58314  }
58315
58316  assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
58317  assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
58318          || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR
58319          || rc==SQLITE_CANTOPEN
58320  );
58321
58322  /* If an error occurs during a ROLLBACK, we can no longer trust the pager
58323  ** cache. So call pager_error() on the way out to make any error persistent.
58324  */
58325  return pager_error(pPager, rc);
58326}
58327
58328/*
58329** Return TRUE if the database file is opened read-only.  Return FALSE
58330** if the database is (in theory) writable.
58331*/
58332SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
58333  return pPager->readOnly;
58334}
58335
58336#ifdef SQLITE_DEBUG
58337/*
58338** Return the sum of the reference counts for all pages held by pPager.
58339*/
58340SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
58341  return sqlite3PcacheRefCount(pPager->pPCache);
58342}
58343#endif
58344
58345/*
58346** Return the approximate number of bytes of memory currently
58347** used by the pager and its associated cache.
58348*/
58349SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
58350  int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
58351                                     + 5*sizeof(void*);
58352  return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
58353           + sqlite3MallocSize(pPager)
58354           + pPager->pageSize;
58355}
58356
58357/*
58358** Return the number of references to the specified page.
58359*/
58360SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
58361  return sqlite3PcachePageRefcount(pPage);
58362}
58363
58364#ifdef SQLITE_TEST
58365/*
58366** This routine is used for testing and analysis only.
58367*/
58368SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
58369  static int a[11];
58370  a[0] = sqlite3PcacheRefCount(pPager->pPCache);
58371  a[1] = sqlite3PcachePagecount(pPager->pPCache);
58372  a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
58373  a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
58374  a[4] = pPager->eState;
58375  a[5] = pPager->errCode;
58376  a[6] = pPager->aStat[PAGER_STAT_HIT];
58377  a[7] = pPager->aStat[PAGER_STAT_MISS];
58378  a[8] = 0;  /* Used to be pPager->nOvfl */
58379  a[9] = pPager->nRead;
58380  a[10] = pPager->aStat[PAGER_STAT_WRITE];
58381  return a;
58382}
58383#endif
58384
58385/*
58386** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
58387** or _WRITE+1.  The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
58388** of SQLITE_DBSTATUS_CACHE_SPILL.  The _SPILL case is not contiguous because
58389** it was added later.
58390**
58391** Before returning, *pnVal is incremented by the
58392** current cache hit or miss count, according to the value of eStat. If the
58393** reset parameter is non-zero, the cache hit or miss count is zeroed before
58394** returning.
58395*/
58396SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
58397
58398  assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
58399       || eStat==SQLITE_DBSTATUS_CACHE_MISS
58400       || eStat==SQLITE_DBSTATUS_CACHE_WRITE
58401       || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
58402  );
58403
58404  assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
58405  assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
58406  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1
58407           && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 );
58408
58409  eStat -= SQLITE_DBSTATUS_CACHE_HIT;
58410  *pnVal += pPager->aStat[eStat];
58411  if( reset ){
58412    pPager->aStat[eStat] = 0;
58413  }
58414}
58415
58416/*
58417** Return true if this is an in-memory or temp-file backed pager.
58418*/
58419SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
58420  return pPager->tempFile;
58421}
58422
58423/*
58424** Check that there are at least nSavepoint savepoints open. If there are
58425** currently less than nSavepoints open, then open one or more savepoints
58426** to make up the difference. If the number of savepoints is already
58427** equal to nSavepoint, then this function is a no-op.
58428**
58429** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
58430** occurs while opening the sub-journal file, then an IO error code is
58431** returned. Otherwise, SQLITE_OK.
58432*/
58433static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){
58434  int rc = SQLITE_OK;                       /* Return code */
58435  int nCurrent = pPager->nSavepoint;        /* Current number of savepoints */
58436  int ii;                                   /* Iterator variable */
58437  PagerSavepoint *aNew;                     /* New Pager.aSavepoint array */
58438
58439  assert( pPager->eState>=PAGER_WRITER_LOCKED );
58440  assert( assert_pager_state(pPager) );
58441  assert( nSavepoint>nCurrent && pPager->useJournal );
58442
58443  /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
58444  ** if the allocation fails. Otherwise, zero the new portion in case a
58445  ** malloc failure occurs while populating it in the for(...) loop below.
58446  */
58447  aNew = (PagerSavepoint *)sqlite3Realloc(
58448      pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
58449  );
58450  if( !aNew ){
58451    return SQLITE_NOMEM_BKPT;
58452  }
58453  memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
58454  pPager->aSavepoint = aNew;
58455
58456  /* Populate the PagerSavepoint structures just allocated. */
58457  for(ii=nCurrent; ii<nSavepoint; ii++){
58458    aNew[ii].nOrig = pPager->dbSize;
58459    if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
58460      aNew[ii].iOffset = pPager->journalOff;
58461    }else{
58462      aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
58463    }
58464    aNew[ii].iSubRec = pPager->nSubRec;
58465    aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
58466    if( !aNew[ii].pInSavepoint ){
58467      return SQLITE_NOMEM_BKPT;
58468    }
58469    if( pagerUseWal(pPager) ){
58470      sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
58471    }
58472    pPager->nSavepoint = ii+1;
58473  }
58474  assert( pPager->nSavepoint==nSavepoint );
58475  assertTruncateConstraint(pPager);
58476  return rc;
58477}
58478SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
58479  assert( pPager->eState>=PAGER_WRITER_LOCKED );
58480  assert( assert_pager_state(pPager) );
58481
58482  if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
58483    return pagerOpenSavepoint(pPager, nSavepoint);
58484  }else{
58485    return SQLITE_OK;
58486  }
58487}
58488
58489
58490/*
58491** This function is called to rollback or release (commit) a savepoint.
58492** The savepoint to release or rollback need not be the most recently
58493** created savepoint.
58494**
58495** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
58496** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
58497** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
58498** that have occurred since the specified savepoint was created.
58499**
58500** The savepoint to rollback or release is identified by parameter
58501** iSavepoint. A value of 0 means to operate on the outermost savepoint
58502** (the first created). A value of (Pager.nSavepoint-1) means operate
58503** on the most recently created savepoint. If iSavepoint is greater than
58504** (Pager.nSavepoint-1), then this function is a no-op.
58505**
58506** If a negative value is passed to this function, then the current
58507** transaction is rolled back. This is different to calling
58508** sqlite3PagerRollback() because this function does not terminate
58509** the transaction or unlock the database, it just restores the
58510** contents of the database to its original state.
58511**
58512** In any case, all savepoints with an index greater than iSavepoint
58513** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
58514** then savepoint iSavepoint is also destroyed.
58515**
58516** This function may return SQLITE_NOMEM if a memory allocation fails,
58517** or an IO error code if an IO error occurs while rolling back a
58518** savepoint. If no errors occur, SQLITE_OK is returned.
58519*/
58520SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
58521  int rc = pPager->errCode;
58522
58523#ifdef SQLITE_ENABLE_ZIPVFS
58524  if( op==SAVEPOINT_RELEASE ) rc = SQLITE_OK;
58525#endif
58526
58527  assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
58528  assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
58529
58530  if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
58531    int ii;            /* Iterator variable */
58532    int nNew;          /* Number of remaining savepoints after this op. */
58533
58534    /* Figure out how many savepoints will still be active after this
58535    ** operation. Store this value in nNew. Then free resources associated
58536    ** with any savepoints that are destroyed by this operation.
58537    */
58538    nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
58539    for(ii=nNew; ii<pPager->nSavepoint; ii++){
58540      sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
58541    }
58542    pPager->nSavepoint = nNew;
58543
58544    /* If this is a release of the outermost savepoint, truncate
58545    ** the sub-journal to zero bytes in size. */
58546    if( op==SAVEPOINT_RELEASE ){
58547      if( nNew==0 && isOpen(pPager->sjfd) ){
58548        /* Only truncate if it is an in-memory sub-journal. */
58549        if( sqlite3JournalIsInMemory(pPager->sjfd) ){
58550          rc = sqlite3OsTruncate(pPager->sjfd, 0);
58551          assert( rc==SQLITE_OK );
58552        }
58553        pPager->nSubRec = 0;
58554      }
58555    }
58556    /* Else this is a rollback operation, playback the specified savepoint.
58557    ** If this is a temp-file, it is possible that the journal file has
58558    ** not yet been opened. In this case there have been no changes to
58559    ** the database file, so the playback operation can be skipped.
58560    */
58561    else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
58562      PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
58563      rc = pagerPlaybackSavepoint(pPager, pSavepoint);
58564      assert(rc!=SQLITE_DONE);
58565    }
58566
58567#ifdef SQLITE_ENABLE_ZIPVFS
58568    /* If the cache has been modified but the savepoint cannot be rolled
58569    ** back journal_mode=off, put the pager in the error state. This way,
58570    ** if the VFS used by this pager includes ZipVFS, the entire transaction
58571    ** can be rolled back at the ZipVFS level.  */
58572    else if(
58573        pPager->journalMode==PAGER_JOURNALMODE_OFF
58574     && pPager->eState>=PAGER_WRITER_CACHEMOD
58575    ){
58576      pPager->errCode = SQLITE_ABORT;
58577      pPager->eState = PAGER_ERROR;
58578      setGetterMethod(pPager);
58579    }
58580#endif
58581  }
58582
58583  return rc;
58584}
58585
58586/*
58587** Return the full pathname of the database file.
58588**
58589** Except, if the pager is in-memory only, then return an empty string if
58590** nullIfMemDb is true.  This routine is called with nullIfMemDb==1 when
58591** used to report the filename to the user, for compatibility with legacy
58592** behavior.  But when the Btree needs to know the filename for matching to
58593** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
58594** participate in shared-cache.
58595**
58596** The return value to this routine is always safe to use with
58597** sqlite3_uri_parameter() and sqlite3_filename_database() and friends.
58598*/
58599SQLITE_PRIVATE const char *sqlite3PagerFilename(const Pager *pPager, int nullIfMemDb){
58600  static const char zFake[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
58601  return (nullIfMemDb && pPager->memDb) ? &zFake[4] : pPager->zFilename;
58602}
58603
58604/*
58605** Return the VFS structure for the pager.
58606*/
58607SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
58608  return pPager->pVfs;
58609}
58610
58611/*
58612** Return the file handle for the database file associated
58613** with the pager.  This might return NULL if the file has
58614** not yet been opened.
58615*/
58616SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
58617  return pPager->fd;
58618}
58619
58620/*
58621** Return the file handle for the journal file (if it exists).
58622** This will be either the rollback journal or the WAL file.
58623*/
58624SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
58625#if SQLITE_OMIT_WAL
58626  return pPager->jfd;
58627#else
58628  return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
58629#endif
58630}
58631
58632/*
58633** Return the full pathname of the journal file.
58634*/
58635SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
58636  return pPager->zJournal;
58637}
58638
58639#ifndef SQLITE_OMIT_AUTOVACUUM
58640/*
58641** Move the page pPg to location pgno in the file.
58642**
58643** There must be no references to the page previously located at
58644** pgno (which we call pPgOld) though that page is allowed to be
58645** in cache.  If the page previously located at pgno is not already
58646** in the rollback journal, it is not put there by by this routine.
58647**
58648** References to the page pPg remain valid. Updating any
58649** meta-data associated with pPg (i.e. data stored in the nExtra bytes
58650** allocated along with the page) is the responsibility of the caller.
58651**
58652** A transaction must be active when this routine is called. It used to be
58653** required that a statement transaction was not active, but this restriction
58654** has been removed (CREATE INDEX needs to move a page when a statement
58655** transaction is active).
58656**
58657** If the fourth argument, isCommit, is non-zero, then this page is being
58658** moved as part of a database reorganization just before the transaction
58659** is being committed. In this case, it is guaranteed that the database page
58660** pPg refers to will not be written to again within this transaction.
58661**
58662** This function may return SQLITE_NOMEM or an IO error code if an error
58663** occurs. Otherwise, it returns SQLITE_OK.
58664*/
58665SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
58666  PgHdr *pPgOld;               /* The page being overwritten. */
58667  Pgno needSyncPgno = 0;       /* Old value of pPg->pgno, if sync is required */
58668  int rc;                      /* Return code */
58669  Pgno origPgno;               /* The original page number */
58670
58671  assert( pPg->nRef>0 );
58672  assert( pPager->eState==PAGER_WRITER_CACHEMOD
58673       || pPager->eState==PAGER_WRITER_DBMOD
58674  );
58675  assert( assert_pager_state(pPager) );
58676
58677  /* In order to be able to rollback, an in-memory database must journal
58678  ** the page we are moving from.
58679  */
58680  assert( pPager->tempFile || !MEMDB );
58681  if( pPager->tempFile ){
58682    rc = sqlite3PagerWrite(pPg);
58683    if( rc ) return rc;
58684  }
58685
58686  /* If the page being moved is dirty and has not been saved by the latest
58687  ** savepoint, then save the current contents of the page into the
58688  ** sub-journal now. This is required to handle the following scenario:
58689  **
58690  **   BEGIN;
58691  **     <journal page X, then modify it in memory>
58692  **     SAVEPOINT one;
58693  **       <Move page X to location Y>
58694  **     ROLLBACK TO one;
58695  **
58696  ** If page X were not written to the sub-journal here, it would not
58697  ** be possible to restore its contents when the "ROLLBACK TO one"
58698  ** statement were is processed.
58699  **
58700  ** subjournalPage() may need to allocate space to store pPg->pgno into
58701  ** one or more savepoint bitvecs. This is the reason this function
58702  ** may return SQLITE_NOMEM.
58703  */
58704  if( (pPg->flags & PGHDR_DIRTY)!=0
58705   && SQLITE_OK!=(rc = subjournalPageIfRequired(pPg))
58706  ){
58707    return rc;
58708  }
58709
58710  PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
58711      PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
58712  IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
58713
58714  /* If the journal needs to be sync()ed before page pPg->pgno can
58715  ** be written to, store pPg->pgno in local variable needSyncPgno.
58716  **
58717  ** If the isCommit flag is set, there is no need to remember that
58718  ** the journal needs to be sync()ed before database page pPg->pgno
58719  ** can be written to. The caller has already promised not to write to it.
58720  */
58721  if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
58722    needSyncPgno = pPg->pgno;
58723    assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
58724            pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
58725    assert( pPg->flags&PGHDR_DIRTY );
58726  }
58727
58728  /* If the cache contains a page with page-number pgno, remove it
58729  ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
58730  ** page pgno before the 'move' operation, it needs to be retained
58731  ** for the page moved there.
58732  */
58733  pPg->flags &= ~PGHDR_NEED_SYNC;
58734  pPgOld = sqlite3PagerLookup(pPager, pgno);
58735  assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
58736  if( pPgOld ){
58737    if( pPgOld->nRef>1 ){
58738      sqlite3PagerUnrefNotNull(pPgOld);
58739      return SQLITE_CORRUPT_BKPT;
58740    }
58741    pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
58742    if( pPager->tempFile ){
58743      /* Do not discard pages from an in-memory database since we might
58744      ** need to rollback later.  Just move the page out of the way. */
58745      sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
58746    }else{
58747      sqlite3PcacheDrop(pPgOld);
58748    }
58749  }
58750
58751  origPgno = pPg->pgno;
58752  sqlite3PcacheMove(pPg, pgno);
58753  sqlite3PcacheMakeDirty(pPg);
58754
58755  /* For an in-memory database, make sure the original page continues
58756  ** to exist, in case the transaction needs to roll back.  Use pPgOld
58757  ** as the original page since it has already been allocated.
58758  */
58759  if( pPager->tempFile && pPgOld ){
58760    sqlite3PcacheMove(pPgOld, origPgno);
58761    sqlite3PagerUnrefNotNull(pPgOld);
58762  }
58763
58764  if( needSyncPgno ){
58765    /* If needSyncPgno is non-zero, then the journal file needs to be
58766    ** sync()ed before any data is written to database file page needSyncPgno.
58767    ** Currently, no such page exists in the page-cache and the
58768    ** "is journaled" bitvec flag has been set. This needs to be remedied by
58769    ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
58770    ** flag.
58771    **
58772    ** If the attempt to load the page into the page-cache fails, (due
58773    ** to a malloc() or IO failure), clear the bit in the pInJournal[]
58774    ** array. Otherwise, if the page is loaded and written again in
58775    ** this transaction, it may be written to the database file before
58776    ** it is synced into the journal file. This way, it may end up in
58777    ** the journal file twice, but that is not a problem.
58778    */
58779    PgHdr *pPgHdr;
58780    rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr, 0);
58781    if( rc!=SQLITE_OK ){
58782      if( needSyncPgno<=pPager->dbOrigSize ){
58783        assert( pPager->pTmpSpace!=0 );
58784        sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
58785      }
58786      return rc;
58787    }
58788    pPgHdr->flags |= PGHDR_NEED_SYNC;
58789    sqlite3PcacheMakeDirty(pPgHdr);
58790    sqlite3PagerUnrefNotNull(pPgHdr);
58791  }
58792
58793  return SQLITE_OK;
58794}
58795#endif
58796
58797/*
58798** The page handle passed as the first argument refers to a dirty page
58799** with a page number other than iNew. This function changes the page's
58800** page number to iNew and sets the value of the PgHdr.flags field to
58801** the value passed as the third parameter.
58802*/
58803SQLITE_PRIVATE void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){
58804  assert( pPg->pgno!=iNew );
58805  pPg->flags = flags;
58806  sqlite3PcacheMove(pPg, iNew);
58807}
58808
58809/*
58810** Return a pointer to the data for the specified page.
58811*/
58812SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
58813  assert( pPg->nRef>0 || pPg->pPager->memDb );
58814  return pPg->pData;
58815}
58816
58817/*
58818** Return a pointer to the Pager.nExtra bytes of "extra" space
58819** allocated along with the specified page.
58820*/
58821SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
58822  return pPg->pExtra;
58823}
58824
58825/*
58826** Get/set the locking-mode for this pager. Parameter eMode must be one
58827** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
58828** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
58829** the locking-mode is set to the value specified.
58830**
58831** The returned value is either PAGER_LOCKINGMODE_NORMAL or
58832** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
58833** locking-mode.
58834*/
58835SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
58836  assert( eMode==PAGER_LOCKINGMODE_QUERY
58837            || eMode==PAGER_LOCKINGMODE_NORMAL
58838            || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
58839  assert( PAGER_LOCKINGMODE_QUERY<0 );
58840  assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
58841  assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
58842  if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
58843    pPager->exclusiveMode = (u8)eMode;
58844  }
58845  return (int)pPager->exclusiveMode;
58846}
58847
58848/*
58849** Set the journal-mode for this pager. Parameter eMode must be one of:
58850**
58851**    PAGER_JOURNALMODE_DELETE
58852**    PAGER_JOURNALMODE_TRUNCATE
58853**    PAGER_JOURNALMODE_PERSIST
58854**    PAGER_JOURNALMODE_OFF
58855**    PAGER_JOURNALMODE_MEMORY
58856**    PAGER_JOURNALMODE_WAL
58857**
58858** The journalmode is set to the value specified if the change is allowed.
58859** The change may be disallowed for the following reasons:
58860**
58861**   *  An in-memory database can only have its journal_mode set to _OFF
58862**      or _MEMORY.
58863**
58864**   *  Temporary databases cannot have _WAL journalmode.
58865**
58866** The returned indicate the current (possibly updated) journal-mode.
58867*/
58868SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
58869  u8 eOld = pPager->journalMode;    /* Prior journalmode */
58870
58871  /* The eMode parameter is always valid */
58872  assert(      eMode==PAGER_JOURNALMODE_DELETE
58873            || eMode==PAGER_JOURNALMODE_TRUNCATE
58874            || eMode==PAGER_JOURNALMODE_PERSIST
58875            || eMode==PAGER_JOURNALMODE_OFF
58876            || eMode==PAGER_JOURNALMODE_WAL
58877            || eMode==PAGER_JOURNALMODE_MEMORY );
58878
58879  /* This routine is only called from the OP_JournalMode opcode, and
58880  ** the logic there will never allow a temporary file to be changed
58881  ** to WAL mode.
58882  */
58883  assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
58884
58885  /* Do allow the journalmode of an in-memory database to be set to
58886  ** anything other than MEMORY or OFF
58887  */
58888  if( MEMDB ){
58889    assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
58890    if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
58891      eMode = eOld;
58892    }
58893  }
58894
58895  if( eMode!=eOld ){
58896
58897    /* Change the journal mode. */
58898    assert( pPager->eState!=PAGER_ERROR );
58899    pPager->journalMode = (u8)eMode;
58900
58901    /* When transistioning from TRUNCATE or PERSIST to any other journal
58902    ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
58903    ** delete the journal file.
58904    */
58905    assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
58906    assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
58907    assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
58908    assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
58909    assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
58910    assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
58911
58912    assert( isOpen(pPager->fd) || pPager->exclusiveMode );
58913    if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
58914
58915      /* In this case we would like to delete the journal file. If it is
58916      ** not possible, then that is not a problem. Deleting the journal file
58917      ** here is an optimization only.
58918      **
58919      ** Before deleting the journal file, obtain a RESERVED lock on the
58920      ** database file. This ensures that the journal file is not deleted
58921      ** while it is in use by some other client.
58922      */
58923      sqlite3OsClose(pPager->jfd);
58924      if( pPager->eLock>=RESERVED_LOCK ){
58925        sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
58926      }else{
58927        int rc = SQLITE_OK;
58928        int state = pPager->eState;
58929        assert( state==PAGER_OPEN || state==PAGER_READER );
58930        if( state==PAGER_OPEN ){
58931          rc = sqlite3PagerSharedLock(pPager);
58932        }
58933        if( pPager->eState==PAGER_READER ){
58934          assert( rc==SQLITE_OK );
58935          rc = pagerLockDb(pPager, RESERVED_LOCK);
58936        }
58937        if( rc==SQLITE_OK ){
58938          sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
58939        }
58940        if( rc==SQLITE_OK && state==PAGER_READER ){
58941          pagerUnlockDb(pPager, SHARED_LOCK);
58942        }else if( state==PAGER_OPEN ){
58943          pager_unlock(pPager);
58944        }
58945        assert( state==pPager->eState );
58946      }
58947    }else if( eMode==PAGER_JOURNALMODE_OFF ){
58948      sqlite3OsClose(pPager->jfd);
58949    }
58950  }
58951
58952  /* Return the new journal mode */
58953  return (int)pPager->journalMode;
58954}
58955
58956/*
58957** Return the current journal mode.
58958*/
58959SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
58960  return (int)pPager->journalMode;
58961}
58962
58963/*
58964** Return TRUE if the pager is in a state where it is OK to change the
58965** journalmode.  Journalmode changes can only happen when the database
58966** is unmodified.
58967*/
58968SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
58969  assert( assert_pager_state(pPager) );
58970  if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
58971  if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
58972  return 1;
58973}
58974
58975/*
58976** Get/set the size-limit used for persistent journal files.
58977**
58978** Setting the size limit to -1 means no limit is enforced.
58979** An attempt to set a limit smaller than -1 is a no-op.
58980*/
58981SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
58982  if( iLimit>=-1 ){
58983    pPager->journalSizeLimit = iLimit;
58984    sqlite3WalLimit(pPager->pWal, iLimit);
58985  }
58986  return pPager->journalSizeLimit;
58987}
58988
58989/*
58990** Return a pointer to the pPager->pBackup variable. The backup module
58991** in backup.c maintains the content of this variable. This module
58992** uses it opaquely as an argument to sqlite3BackupRestart() and
58993** sqlite3BackupUpdate() only.
58994*/
58995SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
58996  return &pPager->pBackup;
58997}
58998
58999#ifndef SQLITE_OMIT_VACUUM
59000/*
59001** Unless this is an in-memory or temporary database, clear the pager cache.
59002*/
59003SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
59004  assert( MEMDB==0 || pPager->tempFile );
59005  if( pPager->tempFile==0 ) pager_reset(pPager);
59006}
59007#endif
59008
59009
59010#ifndef SQLITE_OMIT_WAL
59011/*
59012** This function is called when the user invokes "PRAGMA wal_checkpoint",
59013** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
59014** or wal_blocking_checkpoint() API functions.
59015**
59016** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
59017*/
59018SQLITE_PRIVATE int sqlite3PagerCheckpoint(
59019  Pager *pPager,                  /* Checkpoint on this pager */
59020  sqlite3 *db,                    /* Db handle used to check for interrupts */
59021  int eMode,                      /* Type of checkpoint */
59022  int *pnLog,                     /* OUT: Final number of frames in log */
59023  int *pnCkpt                     /* OUT: Final number of checkpointed frames */
59024){
59025  int rc = SQLITE_OK;
59026  if( pPager->pWal ){
59027    rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
59028        (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
59029        pPager->pBusyHandlerArg,
59030        pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
59031        pnLog, pnCkpt
59032    );
59033  }
59034  return rc;
59035}
59036
59037SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
59038  return sqlite3WalCallback(pPager->pWal);
59039}
59040
59041/*
59042** Return true if the underlying VFS for the given pager supports the
59043** primitives necessary for write-ahead logging.
59044*/
59045SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
59046  const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
59047  if( pPager->noLock ) return 0;
59048  return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
59049}
59050
59051/*
59052** Attempt to take an exclusive lock on the database file. If a PENDING lock
59053** is obtained instead, immediately release it.
59054*/
59055static int pagerExclusiveLock(Pager *pPager){
59056  int rc;                         /* Return code */
59057
59058  assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
59059  rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
59060  if( rc!=SQLITE_OK ){
59061    /* If the attempt to grab the exclusive lock failed, release the
59062    ** pending lock that may have been obtained instead.  */
59063    pagerUnlockDb(pPager, SHARED_LOCK);
59064  }
59065
59066  return rc;
59067}
59068
59069/*
59070** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
59071** exclusive-locking mode when this function is called, take an EXCLUSIVE
59072** lock on the database file and use heap-memory to store the wal-index
59073** in. Otherwise, use the normal shared-memory.
59074*/
59075static int pagerOpenWal(Pager *pPager){
59076  int rc = SQLITE_OK;
59077
59078  assert( pPager->pWal==0 && pPager->tempFile==0 );
59079  assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
59080
59081  /* If the pager is already in exclusive-mode, the WAL module will use
59082  ** heap-memory for the wal-index instead of the VFS shared-memory
59083  ** implementation. Take the exclusive lock now, before opening the WAL
59084  ** file, to make sure this is safe.
59085  */
59086  if( pPager->exclusiveMode ){
59087    rc = pagerExclusiveLock(pPager);
59088  }
59089
59090  /* Open the connection to the log file. If this operation fails,
59091  ** (e.g. due to malloc() failure), return an error code.
59092  */
59093  if( rc==SQLITE_OK ){
59094    rc = sqlite3WalOpen(pPager->pVfs,
59095        pPager->fd, pPager->zWal, pPager->exclusiveMode,
59096        pPager->journalSizeLimit, &pPager->pWal
59097    );
59098  }
59099  pagerFixMaplimit(pPager);
59100
59101  return rc;
59102}
59103
59104
59105/*
59106** The caller must be holding a SHARED lock on the database file to call
59107** this function.
59108**
59109** If the pager passed as the first argument is open on a real database
59110** file (not a temp file or an in-memory database), and the WAL file
59111** is not already open, make an attempt to open it now. If successful,
59112** return SQLITE_OK. If an error occurs or the VFS used by the pager does
59113** not support the xShmXXX() methods, return an error code. *pbOpen is
59114** not modified in either case.
59115**
59116** If the pager is open on a temp-file (or in-memory database), or if
59117** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
59118** without doing anything.
59119*/
59120SQLITE_PRIVATE int sqlite3PagerOpenWal(
59121  Pager *pPager,                  /* Pager object */
59122  int *pbOpen                     /* OUT: Set to true if call is a no-op */
59123){
59124  int rc = SQLITE_OK;             /* Return code */
59125
59126  assert( assert_pager_state(pPager) );
59127  assert( pPager->eState==PAGER_OPEN   || pbOpen );
59128  assert( pPager->eState==PAGER_READER || !pbOpen );
59129  assert( pbOpen==0 || *pbOpen==0 );
59130  assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
59131
59132  if( !pPager->tempFile && !pPager->pWal ){
59133    if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
59134
59135    /* Close any rollback journal previously open */
59136    sqlite3OsClose(pPager->jfd);
59137
59138    rc = pagerOpenWal(pPager);
59139    if( rc==SQLITE_OK ){
59140      pPager->journalMode = PAGER_JOURNALMODE_WAL;
59141      pPager->eState = PAGER_OPEN;
59142    }
59143  }else{
59144    *pbOpen = 1;
59145  }
59146
59147  return rc;
59148}
59149
59150/*
59151** This function is called to close the connection to the log file prior
59152** to switching from WAL to rollback mode.
59153**
59154** Before closing the log file, this function attempts to take an
59155** EXCLUSIVE lock on the database file. If this cannot be obtained, an
59156** error (SQLITE_BUSY) is returned and the log connection is not closed.
59157** If successful, the EXCLUSIVE lock is not released before returning.
59158*/
59159SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
59160  int rc = SQLITE_OK;
59161
59162  assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
59163
59164  /* If the log file is not already open, but does exist in the file-system,
59165  ** it may need to be checkpointed before the connection can switch to
59166  ** rollback mode. Open it now so this can happen.
59167  */
59168  if( !pPager->pWal ){
59169    int logexists = 0;
59170    rc = pagerLockDb(pPager, SHARED_LOCK);
59171    if( rc==SQLITE_OK ){
59172      rc = sqlite3OsAccess(
59173          pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
59174      );
59175    }
59176    if( rc==SQLITE_OK && logexists ){
59177      rc = pagerOpenWal(pPager);
59178    }
59179  }
59180
59181  /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
59182  ** the database file, the log and log-summary files will be deleted.
59183  */
59184  if( rc==SQLITE_OK && pPager->pWal ){
59185    rc = pagerExclusiveLock(pPager);
59186    if( rc==SQLITE_OK ){
59187      rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
59188                           pPager->pageSize, (u8*)pPager->pTmpSpace);
59189      pPager->pWal = 0;
59190      pagerFixMaplimit(pPager);
59191      if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
59192    }
59193  }
59194  return rc;
59195}
59196
59197#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
59198/*
59199** If pager pPager is a wal-mode database not in exclusive locking mode,
59200** invoke the sqlite3WalWriteLock() function on the associated Wal object
59201** with the same db and bLock parameters as were passed to this function.
59202** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
59203*/
59204SQLITE_PRIVATE int sqlite3PagerWalWriteLock(Pager *pPager, int bLock){
59205  int rc = SQLITE_OK;
59206  if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){
59207    rc = sqlite3WalWriteLock(pPager->pWal, bLock);
59208  }
59209  return rc;
59210}
59211
59212/*
59213** Set the database handle used by the wal layer to determine if
59214** blocking locks are required.
59215*/
59216SQLITE_PRIVATE void sqlite3PagerWalDb(Pager *pPager, sqlite3 *db){
59217  if( pagerUseWal(pPager) ){
59218    sqlite3WalDb(pPager->pWal, db);
59219  }
59220}
59221#endif
59222
59223#ifdef SQLITE_ENABLE_SNAPSHOT
59224/*
59225** If this is a WAL database, obtain a snapshot handle for the snapshot
59226** currently open. Otherwise, return an error.
59227*/
59228SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){
59229  int rc = SQLITE_ERROR;
59230  if( pPager->pWal ){
59231    rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
59232  }
59233  return rc;
59234}
59235
59236/*
59237** If this is a WAL database, store a pointer to pSnapshot. Next time a
59238** read transaction is opened, attempt to read from the snapshot it
59239** identifies. If this is not a WAL database, return an error.
59240*/
59241SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(
59242  Pager *pPager,
59243  sqlite3_snapshot *pSnapshot
59244){
59245  int rc = SQLITE_OK;
59246  if( pPager->pWal ){
59247    sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
59248  }else{
59249    rc = SQLITE_ERROR;
59250  }
59251  return rc;
59252}
59253
59254/*
59255** If this is a WAL database, call sqlite3WalSnapshotRecover(). If this
59256** is not a WAL database, return an error.
59257*/
59258SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager){
59259  int rc;
59260  if( pPager->pWal ){
59261    rc = sqlite3WalSnapshotRecover(pPager->pWal);
59262  }else{
59263    rc = SQLITE_ERROR;
59264  }
59265  return rc;
59266}
59267
59268/*
59269** The caller currently has a read transaction open on the database.
59270** If this is not a WAL database, SQLITE_ERROR is returned. Otherwise,
59271** this function takes a SHARED lock on the CHECKPOINTER slot and then
59272** checks if the snapshot passed as the second argument is still
59273** available. If so, SQLITE_OK is returned.
59274**
59275** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
59276** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
59277** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
59278** lock is released before returning.
59279*/
59280SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot){
59281  int rc;
59282  if( pPager->pWal ){
59283    rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
59284  }else{
59285    rc = SQLITE_ERROR;
59286  }
59287  return rc;
59288}
59289
59290/*
59291** Release a lock obtained by an earlier successful call to
59292** sqlite3PagerSnapshotCheck().
59293*/
59294SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager){
59295  assert( pPager->pWal );
59296  sqlite3WalSnapshotUnlock(pPager->pWal);
59297}
59298
59299#endif /* SQLITE_ENABLE_SNAPSHOT */
59300#endif /* !SQLITE_OMIT_WAL */
59301
59302#ifdef SQLITE_ENABLE_ZIPVFS
59303/*
59304** A read-lock must be held on the pager when this function is called. If
59305** the pager is in WAL mode and the WAL file currently contains one or more
59306** frames, return the size in bytes of the page images stored within the
59307** WAL frames. Otherwise, if this is not a WAL database or the WAL file
59308** is empty, return 0.
59309*/
59310SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
59311  assert( pPager->eState>=PAGER_READER );
59312  return sqlite3WalFramesize(pPager->pWal);
59313}
59314#endif
59315
59316#endif /* SQLITE_OMIT_DISKIO */
59317
59318/************** End of pager.c ***********************************************/
59319/************** Begin file wal.c *********************************************/
59320/*
59321** 2010 February 1
59322**
59323** The author disclaims copyright to this source code.  In place of
59324** a legal notice, here is a blessing:
59325**
59326**    May you do good and not evil.
59327**    May you find forgiveness for yourself and forgive others.
59328**    May you share freely, never taking more than you give.
59329**
59330*************************************************************************
59331**
59332** This file contains the implementation of a write-ahead log (WAL) used in
59333** "journal_mode=WAL" mode.
59334**
59335** WRITE-AHEAD LOG (WAL) FILE FORMAT
59336**
59337** A WAL file consists of a header followed by zero or more "frames".
59338** Each frame records the revised content of a single page from the
59339** database file.  All changes to the database are recorded by writing
59340** frames into the WAL.  Transactions commit when a frame is written that
59341** contains a commit marker.  A single WAL can and usually does record
59342** multiple transactions.  Periodically, the content of the WAL is
59343** transferred back into the database file in an operation called a
59344** "checkpoint".
59345**
59346** A single WAL file can be used multiple times.  In other words, the
59347** WAL can fill up with frames and then be checkpointed and then new
59348** frames can overwrite the old ones.  A WAL always grows from beginning
59349** toward the end.  Checksums and counters attached to each frame are
59350** used to determine which frames within the WAL are valid and which
59351** are leftovers from prior checkpoints.
59352**
59353** The WAL header is 32 bytes in size and consists of the following eight
59354** big-endian 32-bit unsigned integer values:
59355**
59356**     0: Magic number.  0x377f0682 or 0x377f0683
59357**     4: File format version.  Currently 3007000
59358**     8: Database page size.  Example: 1024
59359**    12: Checkpoint sequence number
59360**    16: Salt-1, random integer incremented with each checkpoint
59361**    20: Salt-2, a different random integer changing with each ckpt
59362**    24: Checksum-1 (first part of checksum for first 24 bytes of header).
59363**    28: Checksum-2 (second part of checksum for first 24 bytes of header).
59364**
59365** Immediately following the wal-header are zero or more frames. Each
59366** frame consists of a 24-byte frame-header followed by a <page-size> bytes
59367** of page data. The frame-header is six big-endian 32-bit unsigned
59368** integer values, as follows:
59369**
59370**     0: Page number.
59371**     4: For commit records, the size of the database image in pages
59372**        after the commit. For all other records, zero.
59373**     8: Salt-1 (copied from the header)
59374**    12: Salt-2 (copied from the header)
59375**    16: Checksum-1.
59376**    20: Checksum-2.
59377**
59378** A frame is considered valid if and only if the following conditions are
59379** true:
59380**
59381**    (1) The salt-1 and salt-2 values in the frame-header match
59382**        salt values in the wal-header
59383**
59384**    (2) The checksum values in the final 8 bytes of the frame-header
59385**        exactly match the checksum computed consecutively on the
59386**        WAL header and the first 8 bytes and the content of all frames
59387**        up to and including the current frame.
59388**
59389** The checksum is computed using 32-bit big-endian integers if the
59390** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
59391** is computed using little-endian if the magic number is 0x377f0682.
59392** The checksum values are always stored in the frame header in a
59393** big-endian format regardless of which byte order is used to compute
59394** the checksum.  The checksum is computed by interpreting the input as
59395** an even number of unsigned 32-bit integers: x[0] through x[N].  The
59396** algorithm used for the checksum is as follows:
59397**
59398**   for i from 0 to n-1 step 2:
59399**     s0 += x[i] + s1;
59400**     s1 += x[i+1] + s0;
59401**   endfor
59402**
59403** Note that s0 and s1 are both weighted checksums using fibonacci weights
59404** in reverse order (the largest fibonacci weight occurs on the first element
59405** of the sequence being summed.)  The s1 value spans all 32-bit
59406** terms of the sequence whereas s0 omits the final term.
59407**
59408** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
59409** WAL is transferred into the database, then the database is VFS.xSync-ed.
59410** The VFS.xSync operations serve as write barriers - all writes launched
59411** before the xSync must complete before any write that launches after the
59412** xSync begins.
59413**
59414** After each checkpoint, the salt-1 value is incremented and the salt-2
59415** value is randomized.  This prevents old and new frames in the WAL from
59416** being considered valid at the same time and being checkpointing together
59417** following a crash.
59418**
59419** READER ALGORITHM
59420**
59421** To read a page from the database (call it page number P), a reader
59422** first checks the WAL to see if it contains page P.  If so, then the
59423** last valid instance of page P that is a followed by a commit frame
59424** or is a commit frame itself becomes the value read.  If the WAL
59425** contains no copies of page P that are valid and which are a commit
59426** frame or are followed by a commit frame, then page P is read from
59427** the database file.
59428**
59429** To start a read transaction, the reader records the index of the last
59430** valid frame in the WAL.  The reader uses this recorded "mxFrame" value
59431** for all subsequent read operations.  New transactions can be appended
59432** to the WAL, but as long as the reader uses its original mxFrame value
59433** and ignores the newly appended content, it will see a consistent snapshot
59434** of the database from a single point in time.  This technique allows
59435** multiple concurrent readers to view different versions of the database
59436** content simultaneously.
59437**
59438** The reader algorithm in the previous paragraphs works correctly, but
59439** because frames for page P can appear anywhere within the WAL, the
59440** reader has to scan the entire WAL looking for page P frames.  If the
59441** WAL is large (multiple megabytes is typical) that scan can be slow,
59442** and read performance suffers.  To overcome this problem, a separate
59443** data structure called the wal-index is maintained to expedite the
59444** search for frames of a particular page.
59445**
59446** WAL-INDEX FORMAT
59447**
59448** Conceptually, the wal-index is shared memory, though VFS implementations
59449** might choose to implement the wal-index using a mmapped file.  Because
59450** the wal-index is shared memory, SQLite does not support journal_mode=WAL
59451** on a network filesystem.  All users of the database must be able to
59452** share memory.
59453**
59454** In the default unix and windows implementation, the wal-index is a mmapped
59455** file whose name is the database name with a "-shm" suffix added.  For that
59456** reason, the wal-index is sometimes called the "shm" file.
59457**
59458** The wal-index is transient.  After a crash, the wal-index can (and should
59459** be) reconstructed from the original WAL file.  In fact, the VFS is required
59460** to either truncate or zero the header of the wal-index when the last
59461** connection to it closes.  Because the wal-index is transient, it can
59462** use an architecture-specific format; it does not have to be cross-platform.
59463** Hence, unlike the database and WAL file formats which store all values
59464** as big endian, the wal-index can store multi-byte values in the native
59465** byte order of the host computer.
59466**
59467** The purpose of the wal-index is to answer this question quickly:  Given
59468** a page number P and a maximum frame index M, return the index of the
59469** last frame in the wal before frame M for page P in the WAL, or return
59470** NULL if there are no frames for page P in the WAL prior to M.
59471**
59472** The wal-index consists of a header region, followed by an one or
59473** more index blocks.
59474**
59475** The wal-index header contains the total number of frames within the WAL
59476** in the mxFrame field.
59477**
59478** Each index block except for the first contains information on
59479** HASHTABLE_NPAGE frames. The first index block contains information on
59480** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
59481** HASHTABLE_NPAGE are selected so that together the wal-index header and
59482** first index block are the same size as all other index blocks in the
59483** wal-index.
59484**
59485** Each index block contains two sections, a page-mapping that contains the
59486** database page number associated with each wal frame, and a hash-table
59487** that allows readers to query an index block for a specific page number.
59488** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
59489** for the first index block) 32-bit page numbers. The first entry in the
59490** first index-block contains the database page number corresponding to the
59491** first frame in the WAL file. The first entry in the second index block
59492** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
59493** the log, and so on.
59494**
59495** The last index block in a wal-index usually contains less than the full
59496** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
59497** depending on the contents of the WAL file. This does not change the
59498** allocated size of the page-mapping array - the page-mapping array merely
59499** contains unused entries.
59500**
59501** Even without using the hash table, the last frame for page P
59502** can be found by scanning the page-mapping sections of each index block
59503** starting with the last index block and moving toward the first, and
59504** within each index block, starting at the end and moving toward the
59505** beginning.  The first entry that equals P corresponds to the frame
59506** holding the content for that page.
59507**
59508** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
59509** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
59510** hash table for each page number in the mapping section, so the hash
59511** table is never more than half full.  The expected number of collisions
59512** prior to finding a match is 1.  Each entry of the hash table is an
59513** 1-based index of an entry in the mapping section of the same
59514** index block.   Let K be the 1-based index of the largest entry in
59515** the mapping section.  (For index blocks other than the last, K will
59516** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
59517** K will be (mxFrame%HASHTABLE_NPAGE).)  Unused slots of the hash table
59518** contain a value of 0.
59519**
59520** To look for page P in the hash table, first compute a hash iKey on
59521** P as follows:
59522**
59523**      iKey = (P * 383) % HASHTABLE_NSLOT
59524**
59525** Then start scanning entries of the hash table, starting with iKey
59526** (wrapping around to the beginning when the end of the hash table is
59527** reached) until an unused hash slot is found. Let the first unused slot
59528** be at index iUnused.  (iUnused might be less than iKey if there was
59529** wrap-around.) Because the hash table is never more than half full,
59530** the search is guaranteed to eventually hit an unused entry.  Let
59531** iMax be the value between iKey and iUnused, closest to iUnused,
59532** where aHash[iMax]==P.  If there is no iMax entry (if there exists
59533** no hash slot such that aHash[i]==p) then page P is not in the
59534** current index block.  Otherwise the iMax-th mapping entry of the
59535** current index block corresponds to the last entry that references
59536** page P.
59537**
59538** A hash search begins with the last index block and moves toward the
59539** first index block, looking for entries corresponding to page P.  On
59540** average, only two or three slots in each index block need to be
59541** examined in order to either find the last entry for page P, or to
59542** establish that no such entry exists in the block.  Each index block
59543** holds over 4000 entries.  So two or three index blocks are sufficient
59544** to cover a typical 10 megabyte WAL file, assuming 1K pages.  8 or 10
59545** comparisons (on average) suffice to either locate a frame in the
59546** WAL or to establish that the frame does not exist in the WAL.  This
59547** is much faster than scanning the entire 10MB WAL.
59548**
59549** Note that entries are added in order of increasing K.  Hence, one
59550** reader might be using some value K0 and a second reader that started
59551** at a later time (after additional transactions were added to the WAL
59552** and to the wal-index) might be using a different value K1, where K1>K0.
59553** Both readers can use the same hash table and mapping section to get
59554** the correct result.  There may be entries in the hash table with
59555** K>K0 but to the first reader, those entries will appear to be unused
59556** slots in the hash table and so the first reader will get an answer as
59557** if no values greater than K0 had ever been inserted into the hash table
59558** in the first place - which is what reader one wants.  Meanwhile, the
59559** second reader using K1 will see additional values that were inserted
59560** later, which is exactly what reader two wants.
59561**
59562** When a rollback occurs, the value of K is decreased. Hash table entries
59563** that correspond to frames greater than the new K value are removed
59564** from the hash table at this point.
59565*/
59566#ifndef SQLITE_OMIT_WAL
59567
59568/* #include "wal.h" */
59569
59570/*
59571** Trace output macros
59572*/
59573#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
59574SQLITE_PRIVATE int sqlite3WalTrace = 0;
59575# define WALTRACE(X)  if(sqlite3WalTrace) sqlite3DebugPrintf X
59576#else
59577# define WALTRACE(X)
59578#endif
59579
59580/*
59581** The maximum (and only) versions of the wal and wal-index formats
59582** that may be interpreted by this version of SQLite.
59583**
59584** If a client begins recovering a WAL file and finds that (a) the checksum
59585** values in the wal-header are correct and (b) the version field is not
59586** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
59587**
59588** Similarly, if a client successfully reads a wal-index header (i.e. the
59589** checksum test is successful) and finds that the version field is not
59590** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
59591** returns SQLITE_CANTOPEN.
59592*/
59593#define WAL_MAX_VERSION      3007000
59594#define WALINDEX_MAX_VERSION 3007000
59595
59596/*
59597** Index numbers for various locking bytes.   WAL_NREADER is the number
59598** of available reader locks and should be at least 3.  The default
59599** is SQLITE_SHM_NLOCK==8 and  WAL_NREADER==5.
59600**
59601** Technically, the various VFSes are free to implement these locks however
59602** they see fit.  However, compatibility is encouraged so that VFSes can
59603** interoperate.  The standard implemention used on both unix and windows
59604** is for the index number to indicate a byte offset into the
59605** WalCkptInfo.aLock[] array in the wal-index header.  In other words, all
59606** locks are on the shm file.  The WALINDEX_LOCK_OFFSET constant (which
59607** should be 120) is the location in the shm file for the first locking
59608** byte.
59609*/
59610#define WAL_WRITE_LOCK         0
59611#define WAL_ALL_BUT_WRITE      1
59612#define WAL_CKPT_LOCK          1
59613#define WAL_RECOVER_LOCK       2
59614#define WAL_READ_LOCK(I)       (3+(I))
59615#define WAL_NREADER            (SQLITE_SHM_NLOCK-3)
59616
59617
59618/* Object declarations */
59619typedef struct WalIndexHdr WalIndexHdr;
59620typedef struct WalIterator WalIterator;
59621typedef struct WalCkptInfo WalCkptInfo;
59622
59623
59624/*
59625** The following object holds a copy of the wal-index header content.
59626**
59627** The actual header in the wal-index consists of two copies of this
59628** object followed by one instance of the WalCkptInfo object.
59629** For all versions of SQLite through 3.10.0 and probably beyond,
59630** the locking bytes (WalCkptInfo.aLock) start at offset 120 and
59631** the total header size is 136 bytes.
59632**
59633** The szPage value can be any power of 2 between 512 and 32768, inclusive.
59634** Or it can be 1 to represent a 65536-byte page.  The latter case was
59635** added in 3.7.1 when support for 64K pages was added.
59636*/
59637struct WalIndexHdr {
59638  u32 iVersion;                   /* Wal-index version */
59639  u32 unused;                     /* Unused (padding) field */
59640  u32 iChange;                    /* Counter incremented each transaction */
59641  u8 isInit;                      /* 1 when initialized */
59642  u8 bigEndCksum;                 /* True if checksums in WAL are big-endian */
59643  u16 szPage;                     /* Database page size in bytes. 1==64K */
59644  u32 mxFrame;                    /* Index of last valid frame in the WAL */
59645  u32 nPage;                      /* Size of database in pages */
59646  u32 aFrameCksum[2];             /* Checksum of last frame in log */
59647  u32 aSalt[2];                   /* Two salt values copied from WAL header */
59648  u32 aCksum[2];                  /* Checksum over all prior fields */
59649};
59650
59651/*
59652** A copy of the following object occurs in the wal-index immediately
59653** following the second copy of the WalIndexHdr.  This object stores
59654** information used by checkpoint.
59655**
59656** nBackfill is the number of frames in the WAL that have been written
59657** back into the database. (We call the act of moving content from WAL to
59658** database "backfilling".)  The nBackfill number is never greater than
59659** WalIndexHdr.mxFrame.  nBackfill can only be increased by threads
59660** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
59661** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
59662** mxFrame back to zero when the WAL is reset.
59663**
59664** nBackfillAttempted is the largest value of nBackfill that a checkpoint
59665** has attempted to achieve.  Normally nBackfill==nBackfillAtempted, however
59666** the nBackfillAttempted is set before any backfilling is done and the
59667** nBackfill is only set after all backfilling completes.  So if a checkpoint
59668** crashes, nBackfillAttempted might be larger than nBackfill.  The
59669** WalIndexHdr.mxFrame must never be less than nBackfillAttempted.
59670**
59671** The aLock[] field is a set of bytes used for locking.  These bytes should
59672** never be read or written.
59673**
59674** There is one entry in aReadMark[] for each reader lock.  If a reader
59675** holds read-lock K, then the value in aReadMark[K] is no greater than
59676** the mxFrame for that reader.  The value READMARK_NOT_USED (0xffffffff)
59677** for any aReadMark[] means that entry is unused.  aReadMark[0] is
59678** a special case; its value is never used and it exists as a place-holder
59679** to avoid having to offset aReadMark[] indexs by one.  Readers holding
59680** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
59681** directly from the database.
59682**
59683** The value of aReadMark[K] may only be changed by a thread that
59684** is holding an exclusive lock on WAL_READ_LOCK(K).  Thus, the value of
59685** aReadMark[K] cannot changed while there is a reader is using that mark
59686** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
59687**
59688** The checkpointer may only transfer frames from WAL to database where
59689** the frame numbers are less than or equal to every aReadMark[] that is
59690** in use (that is, every aReadMark[j] for which there is a corresponding
59691** WAL_READ_LOCK(j)).  New readers (usually) pick the aReadMark[] with the
59692** largest value and will increase an unused aReadMark[] to mxFrame if there
59693** is not already an aReadMark[] equal to mxFrame.  The exception to the
59694** previous sentence is when nBackfill equals mxFrame (meaning that everything
59695** in the WAL has been backfilled into the database) then new readers
59696** will choose aReadMark[0] which has value 0 and hence such reader will
59697** get all their all content directly from the database file and ignore
59698** the WAL.
59699**
59700** Writers normally append new frames to the end of the WAL.  However,
59701** if nBackfill equals mxFrame (meaning that all WAL content has been
59702** written back into the database) and if no readers are using the WAL
59703** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
59704** the writer will first "reset" the WAL back to the beginning and start
59705** writing new content beginning at frame 1.
59706**
59707** We assume that 32-bit loads are atomic and so no locks are needed in
59708** order to read from any aReadMark[] entries.
59709*/
59710struct WalCkptInfo {
59711  u32 nBackfill;                  /* Number of WAL frames backfilled into DB */
59712  u32 aReadMark[WAL_NREADER];     /* Reader marks */
59713  u8 aLock[SQLITE_SHM_NLOCK];     /* Reserved space for locks */
59714  u32 nBackfillAttempted;         /* WAL frames perhaps written, or maybe not */
59715  u32 notUsed0;                   /* Available for future enhancements */
59716};
59717#define READMARK_NOT_USED  0xffffffff
59718
59719
59720/* A block of WALINDEX_LOCK_RESERVED bytes beginning at
59721** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
59722** only support mandatory file-locks, we do not read or write data
59723** from the region of the file on which locks are applied.
59724*/
59725#define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock))
59726#define WALINDEX_HDR_SIZE    (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))
59727
59728/* Size of header before each frame in wal */
59729#define WAL_FRAME_HDRSIZE 24
59730
59731/* Size of write ahead log header, including checksum. */
59732#define WAL_HDRSIZE 32
59733
59734/* WAL magic value. Either this value, or the same value with the least
59735** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
59736** big-endian format in the first 4 bytes of a WAL file.
59737**
59738** If the LSB is set, then the checksums for each frame within the WAL
59739** file are calculated by treating all data as an array of 32-bit
59740** big-endian words. Otherwise, they are calculated by interpreting
59741** all data as 32-bit little-endian words.
59742*/
59743#define WAL_MAGIC 0x377f0682
59744
59745/*
59746** Return the offset of frame iFrame in the write-ahead log file,
59747** assuming a database page size of szPage bytes. The offset returned
59748** is to the start of the write-ahead log frame-header.
59749*/
59750#define walFrameOffset(iFrame, szPage) (                               \
59751  WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE)         \
59752)
59753
59754/*
59755** An open write-ahead log file is represented by an instance of the
59756** following object.
59757*/
59758struct Wal {
59759  sqlite3_vfs *pVfs;         /* The VFS used to create pDbFd */
59760  sqlite3_file *pDbFd;       /* File handle for the database file */
59761  sqlite3_file *pWalFd;      /* File handle for WAL file */
59762  u32 iCallback;             /* Value to pass to log callback (or 0) */
59763  i64 mxWalSize;             /* Truncate WAL to this size upon reset */
59764  int nWiData;               /* Size of array apWiData */
59765  int szFirstBlock;          /* Size of first block written to WAL file */
59766  volatile u32 **apWiData;   /* Pointer to wal-index content in memory */
59767  u32 szPage;                /* Database page size */
59768  i16 readLock;              /* Which read lock is being held.  -1 for none */
59769  u8 syncFlags;              /* Flags to use to sync header writes */
59770  u8 exclusiveMode;          /* Non-zero if connection is in exclusive mode */
59771  u8 writeLock;              /* True if in a write transaction */
59772  u8 ckptLock;               /* True if holding a checkpoint lock */
59773  u8 readOnly;               /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
59774  u8 truncateOnCommit;       /* True to truncate WAL file on commit */
59775  u8 syncHeader;             /* Fsync the WAL header if true */
59776  u8 padToSectorBoundary;    /* Pad transactions out to the next sector */
59777  u8 bShmUnreliable;         /* SHM content is read-only and unreliable */
59778  WalIndexHdr hdr;           /* Wal-index header for current transaction */
59779  u32 minFrame;              /* Ignore wal frames before this one */
59780  u32 iReCksum;              /* On commit, recalculate checksums from here */
59781  const char *zWalName;      /* Name of WAL file */
59782  u32 nCkpt;                 /* Checkpoint sequence counter in the wal-header */
59783#ifdef SQLITE_DEBUG
59784  u8 lockError;              /* True if a locking error has occurred */
59785#endif
59786#ifdef SQLITE_ENABLE_SNAPSHOT
59787  WalIndexHdr *pSnapshot;    /* Start transaction here if not NULL */
59788#endif
59789#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
59790  sqlite3 *db;
59791#endif
59792};
59793
59794/*
59795** Candidate values for Wal.exclusiveMode.
59796*/
59797#define WAL_NORMAL_MODE     0
59798#define WAL_EXCLUSIVE_MODE  1
59799#define WAL_HEAPMEMORY_MODE 2
59800
59801/*
59802** Possible values for WAL.readOnly
59803*/
59804#define WAL_RDWR        0    /* Normal read/write connection */
59805#define WAL_RDONLY      1    /* The WAL file is readonly */
59806#define WAL_SHM_RDONLY  2    /* The SHM file is readonly */
59807
59808/*
59809** Each page of the wal-index mapping contains a hash-table made up of
59810** an array of HASHTABLE_NSLOT elements of the following type.
59811*/
59812typedef u16 ht_slot;
59813
59814/*
59815** This structure is used to implement an iterator that loops through
59816** all frames in the WAL in database page order. Where two or more frames
59817** correspond to the same database page, the iterator visits only the
59818** frame most recently written to the WAL (in other words, the frame with
59819** the largest index).
59820**
59821** The internals of this structure are only accessed by:
59822**
59823**   walIteratorInit() - Create a new iterator,
59824**   walIteratorNext() - Step an iterator,
59825**   walIteratorFree() - Free an iterator.
59826**
59827** This functionality is used by the checkpoint code (see walCheckpoint()).
59828*/
59829struct WalIterator {
59830  int iPrior;                     /* Last result returned from the iterator */
59831  int nSegment;                   /* Number of entries in aSegment[] */
59832  struct WalSegment {
59833    int iNext;                    /* Next slot in aIndex[] not yet returned */
59834    ht_slot *aIndex;              /* i0, i1, i2... such that aPgno[iN] ascend */
59835    u32 *aPgno;                   /* Array of page numbers. */
59836    int nEntry;                   /* Nr. of entries in aPgno[] and aIndex[] */
59837    int iZero;                    /* Frame number associated with aPgno[0] */
59838  } aSegment[1];                  /* One for every 32KB page in the wal-index */
59839};
59840
59841/*
59842** Define the parameters of the hash tables in the wal-index file. There
59843** is a hash-table following every HASHTABLE_NPAGE page numbers in the
59844** wal-index.
59845**
59846** Changing any of these constants will alter the wal-index format and
59847** create incompatibilities.
59848*/
59849#define HASHTABLE_NPAGE      4096                 /* Must be power of 2 */
59850#define HASHTABLE_HASH_1     383                  /* Should be prime */
59851#define HASHTABLE_NSLOT      (HASHTABLE_NPAGE*2)  /* Must be a power of 2 */
59852
59853/*
59854** The block of page numbers associated with the first hash-table in a
59855** wal-index is smaller than usual. This is so that there is a complete
59856** hash-table on each aligned 32KB page of the wal-index.
59857*/
59858#define HASHTABLE_NPAGE_ONE  (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
59859
59860/* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
59861#define WALINDEX_PGSZ   (                                         \
59862    sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
59863)
59864
59865/*
59866** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
59867** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
59868** numbered from zero.
59869**
59870** If the wal-index is currently smaller the iPage pages then the size
59871** of the wal-index might be increased, but only if it is safe to do
59872** so.  It is safe to enlarge the wal-index if pWal->writeLock is true
59873** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
59874**
59875** If this call is successful, *ppPage is set to point to the wal-index
59876** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
59877** then an SQLite error code is returned and *ppPage is set to 0.
59878*/
59879static SQLITE_NOINLINE int walIndexPageRealloc(
59880  Wal *pWal,               /* The WAL context */
59881  int iPage,               /* The page we seek */
59882  volatile u32 **ppPage    /* Write the page pointer here */
59883){
59884  int rc = SQLITE_OK;
59885
59886  /* Enlarge the pWal->apWiData[] array if required */
59887  if( pWal->nWiData<=iPage ){
59888    sqlite3_int64 nByte = sizeof(u32*)*(iPage+1);
59889    volatile u32 **apNew;
59890    apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
59891    if( !apNew ){
59892      *ppPage = 0;
59893      return SQLITE_NOMEM_BKPT;
59894    }
59895    memset((void*)&apNew[pWal->nWiData], 0,
59896           sizeof(u32*)*(iPage+1-pWal->nWiData));
59897    pWal->apWiData = apNew;
59898    pWal->nWiData = iPage+1;
59899  }
59900
59901  /* Request a pointer to the required page from the VFS */
59902  assert( pWal->apWiData[iPage]==0 );
59903  if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
59904    pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
59905    if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
59906  }else{
59907    rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
59908        pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
59909    );
59910    assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
59911    testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
59912    if( (rc&0xff)==SQLITE_READONLY ){
59913      pWal->readOnly |= WAL_SHM_RDONLY;
59914      if( rc==SQLITE_READONLY ){
59915        rc = SQLITE_OK;
59916      }
59917    }
59918  }
59919
59920  *ppPage = pWal->apWiData[iPage];
59921  assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
59922  return rc;
59923}
59924static int walIndexPage(
59925  Wal *pWal,               /* The WAL context */
59926  int iPage,               /* The page we seek */
59927  volatile u32 **ppPage    /* Write the page pointer here */
59928){
59929  if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
59930    return walIndexPageRealloc(pWal, iPage, ppPage);
59931  }
59932  return SQLITE_OK;
59933}
59934
59935/*
59936** Return a pointer to the WalCkptInfo structure in the wal-index.
59937*/
59938static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
59939  assert( pWal->nWiData>0 && pWal->apWiData[0] );
59940  return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
59941}
59942
59943/*
59944** Return a pointer to the WalIndexHdr structure in the wal-index.
59945*/
59946static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
59947  assert( pWal->nWiData>0 && pWal->apWiData[0] );
59948  return (volatile WalIndexHdr*)pWal->apWiData[0];
59949}
59950
59951/*
59952** The argument to this macro must be of type u32. On a little-endian
59953** architecture, it returns the u32 value that results from interpreting
59954** the 4 bytes as a big-endian value. On a big-endian architecture, it
59955** returns the value that would be produced by interpreting the 4 bytes
59956** of the input value as a little-endian integer.
59957*/
59958#define BYTESWAP32(x) ( \
59959    (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8)  \
59960  + (((x)&0x00FF0000)>>8)  + (((x)&0xFF000000)>>24) \
59961)
59962
59963/*
59964** Generate or extend an 8 byte checksum based on the data in
59965** array aByte[] and the initial values of aIn[0] and aIn[1] (or
59966** initial values of 0 and 0 if aIn==NULL).
59967**
59968** The checksum is written back into aOut[] before returning.
59969**
59970** nByte must be a positive multiple of 8.
59971*/
59972static void walChecksumBytes(
59973  int nativeCksum, /* True for native byte-order, false for non-native */
59974  u8 *a,           /* Content to be checksummed */
59975  int nByte,       /* Bytes of content in a[].  Must be a multiple of 8. */
59976  const u32 *aIn,  /* Initial checksum value input */
59977  u32 *aOut        /* OUT: Final checksum value output */
59978){
59979  u32 s1, s2;
59980  u32 *aData = (u32 *)a;
59981  u32 *aEnd = (u32 *)&a[nByte];
59982
59983  if( aIn ){
59984    s1 = aIn[0];
59985    s2 = aIn[1];
59986  }else{
59987    s1 = s2 = 0;
59988  }
59989
59990  assert( nByte>=8 );
59991  assert( (nByte&0x00000007)==0 );
59992  assert( nByte<=65536 );
59993
59994  if( nativeCksum ){
59995    do {
59996      s1 += *aData++ + s2;
59997      s2 += *aData++ + s1;
59998    }while( aData<aEnd );
59999  }else{
60000    do {
60001      s1 += BYTESWAP32(aData[0]) + s2;
60002      s2 += BYTESWAP32(aData[1]) + s1;
60003      aData += 2;
60004    }while( aData<aEnd );
60005  }
60006
60007  aOut[0] = s1;
60008  aOut[1] = s2;
60009}
60010
60011/*
60012** If there is the possibility of concurrent access to the SHM file
60013** from multiple threads and/or processes, then do a memory barrier.
60014*/
60015static void walShmBarrier(Wal *pWal){
60016  if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
60017    sqlite3OsShmBarrier(pWal->pDbFd);
60018  }
60019}
60020
60021/*
60022** Add the SQLITE_NO_TSAN as part of the return-type of a function
60023** definition as a hint that the function contains constructs that
60024** might give false-positive TSAN warnings.
60025**
60026** See tag-20200519-1.
60027*/
60028#if defined(__clang__) && !defined(SQLITE_NO_TSAN)
60029# define SQLITE_NO_TSAN __attribute__((no_sanitize_thread))
60030#else
60031# define SQLITE_NO_TSAN
60032#endif
60033
60034/*
60035** Write the header information in pWal->hdr into the wal-index.
60036**
60037** The checksum on pWal->hdr is updated before it is written.
60038*/
60039static SQLITE_NO_TSAN void walIndexWriteHdr(Wal *pWal){
60040  volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
60041  const int nCksum = offsetof(WalIndexHdr, aCksum);
60042
60043  assert( pWal->writeLock );
60044  pWal->hdr.isInit = 1;
60045  pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
60046  walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
60047  /* Possible TSAN false-positive.  See tag-20200519-1 */
60048  memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
60049  walShmBarrier(pWal);
60050  memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
60051}
60052
60053/*
60054** This function encodes a single frame header and writes it to a buffer
60055** supplied by the caller. A frame-header is made up of a series of
60056** 4-byte big-endian integers, as follows:
60057**
60058**     0: Page number.
60059**     4: For commit records, the size of the database image in pages
60060**        after the commit. For all other records, zero.
60061**     8: Salt-1 (copied from the wal-header)
60062**    12: Salt-2 (copied from the wal-header)
60063**    16: Checksum-1.
60064**    20: Checksum-2.
60065*/
60066static void walEncodeFrame(
60067  Wal *pWal,                      /* The write-ahead log */
60068  u32 iPage,                      /* Database page number for frame */
60069  u32 nTruncate,                  /* New db size (or 0 for non-commit frames) */
60070  u8 *aData,                      /* Pointer to page data */
60071  u8 *aFrame                      /* OUT: Write encoded frame here */
60072){
60073  int nativeCksum;                /* True for native byte-order checksums */
60074  u32 *aCksum = pWal->hdr.aFrameCksum;
60075  assert( WAL_FRAME_HDRSIZE==24 );
60076  sqlite3Put4byte(&aFrame[0], iPage);
60077  sqlite3Put4byte(&aFrame[4], nTruncate);
60078  if( pWal->iReCksum==0 ){
60079    memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
60080
60081    nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
60082    walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
60083    walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
60084
60085    sqlite3Put4byte(&aFrame[16], aCksum[0]);
60086    sqlite3Put4byte(&aFrame[20], aCksum[1]);
60087  }else{
60088    memset(&aFrame[8], 0, 16);
60089  }
60090}
60091
60092/*
60093** Check to see if the frame with header in aFrame[] and content
60094** in aData[] is valid.  If it is a valid frame, fill *piPage and
60095** *pnTruncate and return true.  Return if the frame is not valid.
60096*/
60097static int walDecodeFrame(
60098  Wal *pWal,                      /* The write-ahead log */
60099  u32 *piPage,                    /* OUT: Database page number for frame */
60100  u32 *pnTruncate,                /* OUT: New db size (or 0 if not commit) */
60101  u8 *aData,                      /* Pointer to page data (for checksum) */
60102  u8 *aFrame                      /* Frame data */
60103){
60104  int nativeCksum;                /* True for native byte-order checksums */
60105  u32 *aCksum = pWal->hdr.aFrameCksum;
60106  u32 pgno;                       /* Page number of the frame */
60107  assert( WAL_FRAME_HDRSIZE==24 );
60108
60109  /* A frame is only valid if the salt values in the frame-header
60110  ** match the salt values in the wal-header.
60111  */
60112  if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
60113    return 0;
60114  }
60115
60116  /* A frame is only valid if the page number is creater than zero.
60117  */
60118  pgno = sqlite3Get4byte(&aFrame[0]);
60119  if( pgno==0 ){
60120    return 0;
60121  }
60122
60123  /* A frame is only valid if a checksum of the WAL header,
60124  ** all prior frams, the first 16 bytes of this frame-header,
60125  ** and the frame-data matches the checksum in the last 8
60126  ** bytes of this frame-header.
60127  */
60128  nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
60129  walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
60130  walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
60131  if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
60132   || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
60133  ){
60134    /* Checksum failed. */
60135    return 0;
60136  }
60137
60138  /* If we reach this point, the frame is valid.  Return the page number
60139  ** and the new database size.
60140  */
60141  *piPage = pgno;
60142  *pnTruncate = sqlite3Get4byte(&aFrame[4]);
60143  return 1;
60144}
60145
60146
60147#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
60148/*
60149** Names of locks.  This routine is used to provide debugging output and is not
60150** a part of an ordinary build.
60151*/
60152static const char *walLockName(int lockIdx){
60153  if( lockIdx==WAL_WRITE_LOCK ){
60154    return "WRITE-LOCK";
60155  }else if( lockIdx==WAL_CKPT_LOCK ){
60156    return "CKPT-LOCK";
60157  }else if( lockIdx==WAL_RECOVER_LOCK ){
60158    return "RECOVER-LOCK";
60159  }else{
60160    static char zName[15];
60161    sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
60162                     lockIdx-WAL_READ_LOCK(0));
60163    return zName;
60164  }
60165}
60166#endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
60167
60168
60169/*
60170** Set or release locks on the WAL.  Locks are either shared or exclusive.
60171** A lock cannot be moved directly between shared and exclusive - it must go
60172** through the unlocked state first.
60173**
60174** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
60175*/
60176static int walLockShared(Wal *pWal, int lockIdx){
60177  int rc;
60178  if( pWal->exclusiveMode ) return SQLITE_OK;
60179  rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
60180                        SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
60181  WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
60182            walLockName(lockIdx), rc ? "failed" : "ok"));
60183  VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
60184  return rc;
60185}
60186static void walUnlockShared(Wal *pWal, int lockIdx){
60187  if( pWal->exclusiveMode ) return;
60188  (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
60189                         SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
60190  WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
60191}
60192static int walLockExclusive(Wal *pWal, int lockIdx, int n){
60193  int rc;
60194  if( pWal->exclusiveMode ) return SQLITE_OK;
60195  rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
60196                        SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
60197  WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
60198            walLockName(lockIdx), n, rc ? "failed" : "ok"));
60199  VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
60200  return rc;
60201}
60202static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
60203  if( pWal->exclusiveMode ) return;
60204  (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
60205                         SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
60206  WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
60207             walLockName(lockIdx), n));
60208}
60209
60210/*
60211** Compute a hash on a page number.  The resulting hash value must land
60212** between 0 and (HASHTABLE_NSLOT-1).  The walHashNext() function advances
60213** the hash to the next value in the event of a collision.
60214*/
60215static int walHash(u32 iPage){
60216  assert( iPage>0 );
60217  assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
60218  return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
60219}
60220static int walNextHash(int iPriorHash){
60221  return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
60222}
60223
60224/*
60225** An instance of the WalHashLoc object is used to describe the location
60226** of a page hash table in the wal-index.  This becomes the return value
60227** from walHashGet().
60228*/
60229typedef struct WalHashLoc WalHashLoc;
60230struct WalHashLoc {
60231  volatile ht_slot *aHash;  /* Start of the wal-index hash table */
60232  volatile u32 *aPgno;      /* aPgno[1] is the page of first frame indexed */
60233  u32 iZero;                /* One less than the frame number of first indexed*/
60234};
60235
60236/*
60237** Return pointers to the hash table and page number array stored on
60238** page iHash of the wal-index. The wal-index is broken into 32KB pages
60239** numbered starting from 0.
60240**
60241** Set output variable pLoc->aHash to point to the start of the hash table
60242** in the wal-index file. Set pLoc->iZero to one less than the frame
60243** number of the first frame indexed by this hash table. If a
60244** slot in the hash table is set to N, it refers to frame number
60245** (pLoc->iZero+N) in the log.
60246**
60247** Finally, set pLoc->aPgno so that pLoc->aPgno[1] is the page number of the
60248** first frame indexed by the hash table, frame (pLoc->iZero+1).
60249*/
60250static int walHashGet(
60251  Wal *pWal,                      /* WAL handle */
60252  int iHash,                      /* Find the iHash'th table */
60253  WalHashLoc *pLoc                /* OUT: Hash table location */
60254){
60255  int rc;                         /* Return code */
60256
60257  rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
60258  assert( rc==SQLITE_OK || iHash>0 );
60259
60260  if( rc==SQLITE_OK ){
60261    pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
60262    if( iHash==0 ){
60263      pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
60264      pLoc->iZero = 0;
60265    }else{
60266      pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
60267    }
60268    pLoc->aPgno = &pLoc->aPgno[-1];
60269  }
60270  return rc;
60271}
60272
60273/*
60274** Return the number of the wal-index page that contains the hash-table
60275** and page-number array that contain entries corresponding to WAL frame
60276** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
60277** are numbered starting from 0.
60278*/
60279static int walFramePage(u32 iFrame){
60280  int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
60281  assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
60282       && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
60283       && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
60284       && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
60285       && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
60286  );
60287  return iHash;
60288}
60289
60290/*
60291** Return the page number associated with frame iFrame in this WAL.
60292*/
60293static u32 walFramePgno(Wal *pWal, u32 iFrame){
60294  int iHash = walFramePage(iFrame);
60295  if( iHash==0 ){
60296    return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
60297  }
60298  return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
60299}
60300
60301/*
60302** Remove entries from the hash table that point to WAL slots greater
60303** than pWal->hdr.mxFrame.
60304**
60305** This function is called whenever pWal->hdr.mxFrame is decreased due
60306** to a rollback or savepoint.
60307**
60308** At most only the hash table containing pWal->hdr.mxFrame needs to be
60309** updated.  Any later hash tables will be automatically cleared when
60310** pWal->hdr.mxFrame advances to the point where those hash tables are
60311** actually needed.
60312*/
60313static void walCleanupHash(Wal *pWal){
60314  WalHashLoc sLoc;                /* Hash table location */
60315  int iLimit = 0;                 /* Zero values greater than this */
60316  int nByte;                      /* Number of bytes to zero in aPgno[] */
60317  int i;                          /* Used to iterate through aHash[] */
60318  int rc;                         /* Return code form walHashGet() */
60319
60320  assert( pWal->writeLock );
60321  testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
60322  testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
60323  testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
60324
60325  if( pWal->hdr.mxFrame==0 ) return;
60326
60327  /* Obtain pointers to the hash-table and page-number array containing
60328  ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
60329  ** that the page said hash-table and array reside on is already mapped.(1)
60330  */
60331  assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
60332  assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
60333  rc = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
60334  if( NEVER(rc) ) return; /* Defense-in-depth, in case (1) above is wrong */
60335
60336  /* Zero all hash-table entries that correspond to frame numbers greater
60337  ** than pWal->hdr.mxFrame.
60338  */
60339  iLimit = pWal->hdr.mxFrame - sLoc.iZero;
60340  assert( iLimit>0 );
60341  for(i=0; i<HASHTABLE_NSLOT; i++){
60342    if( sLoc.aHash[i]>iLimit ){
60343      sLoc.aHash[i] = 0;
60344    }
60345  }
60346
60347  /* Zero the entries in the aPgno array that correspond to frames with
60348  ** frame numbers greater than pWal->hdr.mxFrame.
60349  */
60350  nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit+1]);
60351  memset((void *)&sLoc.aPgno[iLimit+1], 0, nByte);
60352
60353#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
60354  /* Verify that the every entry in the mapping region is still reachable
60355  ** via the hash table even after the cleanup.
60356  */
60357  if( iLimit ){
60358    int j;           /* Loop counter */
60359    int iKey;        /* Hash key */
60360    for(j=1; j<=iLimit; j++){
60361      for(iKey=walHash(sLoc.aPgno[j]);sLoc.aHash[iKey];iKey=walNextHash(iKey)){
60362        if( sLoc.aHash[iKey]==j ) break;
60363      }
60364      assert( sLoc.aHash[iKey]==j );
60365    }
60366  }
60367#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
60368}
60369
60370
60371/*
60372** Set an entry in the wal-index that will map database page number
60373** pPage into WAL frame iFrame.
60374*/
60375static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
60376  int rc;                         /* Return code */
60377  WalHashLoc sLoc;                /* Wal-index hash table location */
60378
60379  rc = walHashGet(pWal, walFramePage(iFrame), &sLoc);
60380
60381  /* Assuming the wal-index file was successfully mapped, populate the
60382  ** page number array and hash table entry.
60383  */
60384  if( rc==SQLITE_OK ){
60385    int iKey;                     /* Hash table key */
60386    int idx;                      /* Value to write to hash-table slot */
60387    int nCollide;                 /* Number of hash collisions */
60388
60389    idx = iFrame - sLoc.iZero;
60390    assert( idx <= HASHTABLE_NSLOT/2 + 1 );
60391
60392    /* If this is the first entry to be added to this hash-table, zero the
60393    ** entire hash table and aPgno[] array before proceeding.
60394    */
60395    if( idx==1 ){
60396      int nByte = (int)((u8 *)&sLoc.aHash[HASHTABLE_NSLOT]
60397                               - (u8 *)&sLoc.aPgno[1]);
60398      memset((void*)&sLoc.aPgno[1], 0, nByte);
60399    }
60400
60401    /* If the entry in aPgno[] is already set, then the previous writer
60402    ** must have exited unexpectedly in the middle of a transaction (after
60403    ** writing one or more dirty pages to the WAL to free up memory).
60404    ** Remove the remnants of that writers uncommitted transaction from
60405    ** the hash-table before writing any new entries.
60406    */
60407    if( sLoc.aPgno[idx] ){
60408      walCleanupHash(pWal);
60409      assert( !sLoc.aPgno[idx] );
60410    }
60411
60412    /* Write the aPgno[] array entry and the hash-table slot. */
60413    nCollide = idx;
60414    for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){
60415      if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
60416    }
60417    sLoc.aPgno[idx] = iPage;
60418    sLoc.aHash[iKey] = (ht_slot)idx;
60419
60420#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
60421    /* Verify that the number of entries in the hash table exactly equals
60422    ** the number of entries in the mapping region.
60423    */
60424    {
60425      int i;           /* Loop counter */
60426      int nEntry = 0;  /* Number of entries in the hash table */
60427      for(i=0; i<HASHTABLE_NSLOT; i++){ if( sLoc.aHash[i] ) nEntry++; }
60428      assert( nEntry==idx );
60429    }
60430
60431    /* Verify that the every entry in the mapping region is reachable
60432    ** via the hash table.  This turns out to be a really, really expensive
60433    ** thing to check, so only do this occasionally - not on every
60434    ** iteration.
60435    */
60436    if( (idx&0x3ff)==0 ){
60437      int i;           /* Loop counter */
60438      for(i=1; i<=idx; i++){
60439        for(iKey=walHash(sLoc.aPgno[i]);
60440            sLoc.aHash[iKey];
60441            iKey=walNextHash(iKey)){
60442          if( sLoc.aHash[iKey]==i ) break;
60443        }
60444        assert( sLoc.aHash[iKey]==i );
60445      }
60446    }
60447#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
60448  }
60449
60450
60451  return rc;
60452}
60453
60454
60455/*
60456** Recover the wal-index by reading the write-ahead log file.
60457**
60458** This routine first tries to establish an exclusive lock on the
60459** wal-index to prevent other threads/processes from doing anything
60460** with the WAL or wal-index while recovery is running.  The
60461** WAL_RECOVER_LOCK is also held so that other threads will know
60462** that this thread is running recovery.  If unable to establish
60463** the necessary locks, this routine returns SQLITE_BUSY.
60464*/
60465static int walIndexRecover(Wal *pWal){
60466  int rc;                         /* Return Code */
60467  i64 nSize;                      /* Size of log file */
60468  u32 aFrameCksum[2] = {0, 0};
60469  int iLock;                      /* Lock offset to lock for checkpoint */
60470
60471  /* Obtain an exclusive lock on all byte in the locking range not already
60472  ** locked by the caller. The caller is guaranteed to have locked the
60473  ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
60474  ** If successful, the same bytes that are locked here are unlocked before
60475  ** this function returns.
60476  */
60477  assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
60478  assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
60479  assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
60480  assert( pWal->writeLock );
60481  iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
60482  rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
60483  if( rc==SQLITE_OK ){
60484    rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
60485    if( rc!=SQLITE_OK ){
60486      walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
60487    }
60488  }
60489  if( rc ){
60490    return rc;
60491  }
60492
60493  WALTRACE(("WAL%p: recovery begin...\n", pWal));
60494
60495  memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
60496
60497  rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
60498  if( rc!=SQLITE_OK ){
60499    goto recovery_error;
60500  }
60501
60502  if( nSize>WAL_HDRSIZE ){
60503    u8 aBuf[WAL_HDRSIZE];         /* Buffer to load WAL header into */
60504    u8 *aFrame = 0;               /* Malloc'd buffer to load entire frame */
60505    int szFrame;                  /* Number of bytes in buffer aFrame[] */
60506    u8 *aData;                    /* Pointer to data part of aFrame buffer */
60507    int iFrame;                   /* Index of last frame read */
60508    i64 iOffset;                  /* Next offset to read from log file */
60509    int szPage;                   /* Page size according to the log */
60510    u32 magic;                    /* Magic value read from WAL header */
60511    u32 version;                  /* Magic value read from WAL header */
60512    int isValid;                  /* True if this frame is valid */
60513
60514    /* Read in the WAL header. */
60515    rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
60516    if( rc!=SQLITE_OK ){
60517      goto recovery_error;
60518    }
60519
60520    /* If the database page size is not a power of two, or is greater than
60521    ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
60522    ** data. Similarly, if the 'magic' value is invalid, ignore the whole
60523    ** WAL file.
60524    */
60525    magic = sqlite3Get4byte(&aBuf[0]);
60526    szPage = sqlite3Get4byte(&aBuf[8]);
60527    if( (magic&0xFFFFFFFE)!=WAL_MAGIC
60528     || szPage&(szPage-1)
60529     || szPage>SQLITE_MAX_PAGE_SIZE
60530     || szPage<512
60531    ){
60532      goto finished;
60533    }
60534    pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
60535    pWal->szPage = szPage;
60536    pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
60537    memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
60538
60539    /* Verify that the WAL header checksum is correct */
60540    walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
60541        aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
60542    );
60543    if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
60544     || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
60545    ){
60546      goto finished;
60547    }
60548
60549    /* Verify that the version number on the WAL format is one that
60550    ** are able to understand */
60551    version = sqlite3Get4byte(&aBuf[4]);
60552    if( version!=WAL_MAX_VERSION ){
60553      rc = SQLITE_CANTOPEN_BKPT;
60554      goto finished;
60555    }
60556
60557    /* Malloc a buffer to read frames into. */
60558    szFrame = szPage + WAL_FRAME_HDRSIZE;
60559    aFrame = (u8 *)sqlite3_malloc64(szFrame);
60560    if( !aFrame ){
60561      rc = SQLITE_NOMEM_BKPT;
60562      goto recovery_error;
60563    }
60564    aData = &aFrame[WAL_FRAME_HDRSIZE];
60565
60566    /* Read all frames from the log file. */
60567    iFrame = 0;
60568    for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
60569      u32 pgno;                   /* Database page number for frame */
60570      u32 nTruncate;              /* dbsize field from frame header */
60571
60572      /* Read and decode the next log frame. */
60573      iFrame++;
60574      rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
60575      if( rc!=SQLITE_OK ) break;
60576      isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
60577      if( !isValid ) break;
60578      rc = walIndexAppend(pWal, iFrame, pgno);
60579      if( rc!=SQLITE_OK ) break;
60580
60581      /* If nTruncate is non-zero, this is a commit record. */
60582      if( nTruncate ){
60583        pWal->hdr.mxFrame = iFrame;
60584        pWal->hdr.nPage = nTruncate;
60585        pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
60586        testcase( szPage<=32768 );
60587        testcase( szPage>=65536 );
60588        aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
60589        aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
60590      }
60591    }
60592
60593    sqlite3_free(aFrame);
60594  }
60595
60596finished:
60597  if( rc==SQLITE_OK ){
60598    volatile WalCkptInfo *pInfo;
60599    int i;
60600    pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
60601    pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
60602    walIndexWriteHdr(pWal);
60603
60604    /* Reset the checkpoint-header. This is safe because this thread is
60605    ** currently holding locks that exclude all other readers, writers and
60606    ** checkpointers.
60607    */
60608    pInfo = walCkptInfo(pWal);
60609    pInfo->nBackfill = 0;
60610    pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
60611    pInfo->aReadMark[0] = 0;
60612    for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
60613    if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame;
60614
60615    /* If more than one frame was recovered from the log file, report an
60616    ** event via sqlite3_log(). This is to help with identifying performance
60617    ** problems caused by applications routinely shutting down without
60618    ** checkpointing the log file.
60619    */
60620    if( pWal->hdr.nPage ){
60621      sqlite3_log(SQLITE_NOTICE_RECOVER_WAL,
60622          "recovered %d frames from WAL file %s",
60623          pWal->hdr.mxFrame, pWal->zWalName
60624      );
60625    }
60626  }
60627
60628recovery_error:
60629  WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
60630  walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
60631  walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
60632  return rc;
60633}
60634
60635/*
60636** Close an open wal-index.
60637*/
60638static void walIndexClose(Wal *pWal, int isDelete){
60639  if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
60640    int i;
60641    for(i=0; i<pWal->nWiData; i++){
60642      sqlite3_free((void *)pWal->apWiData[i]);
60643      pWal->apWiData[i] = 0;
60644    }
60645  }
60646  if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
60647    sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
60648  }
60649}
60650
60651/*
60652** Open a connection to the WAL file zWalName. The database file must
60653** already be opened on connection pDbFd. The buffer that zWalName points
60654** to must remain valid for the lifetime of the returned Wal* handle.
60655**
60656** A SHARED lock should be held on the database file when this function
60657** is called. The purpose of this SHARED lock is to prevent any other
60658** client from unlinking the WAL or wal-index file. If another process
60659** were to do this just after this client opened one of these files, the
60660** system would be badly broken.
60661**
60662** If the log file is successfully opened, SQLITE_OK is returned and
60663** *ppWal is set to point to a new WAL handle. If an error occurs,
60664** an SQLite error code is returned and *ppWal is left unmodified.
60665*/
60666SQLITE_PRIVATE int sqlite3WalOpen(
60667  sqlite3_vfs *pVfs,              /* vfs module to open wal and wal-index */
60668  sqlite3_file *pDbFd,            /* The open database file */
60669  const char *zWalName,           /* Name of the WAL file */
60670  int bNoShm,                     /* True to run in heap-memory mode */
60671  i64 mxWalSize,                  /* Truncate WAL to this size on reset */
60672  Wal **ppWal                     /* OUT: Allocated Wal handle */
60673){
60674  int rc;                         /* Return Code */
60675  Wal *pRet;                      /* Object to allocate and return */
60676  int flags;                      /* Flags passed to OsOpen() */
60677
60678  assert( zWalName && zWalName[0] );
60679  assert( pDbFd );
60680
60681  /* In the amalgamation, the os_unix.c and os_win.c source files come before
60682  ** this source file.  Verify that the #defines of the locking byte offsets
60683  ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
60684  ** For that matter, if the lock offset ever changes from its initial design
60685  ** value of 120, we need to know that so there is an assert() to check it.
60686  */
60687  assert( 120==WALINDEX_LOCK_OFFSET );
60688  assert( 136==WALINDEX_HDR_SIZE );
60689#ifdef WIN_SHM_BASE
60690  assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
60691#endif
60692#ifdef UNIX_SHM_BASE
60693  assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
60694#endif
60695
60696
60697  /* Allocate an instance of struct Wal to return. */
60698  *ppWal = 0;
60699  pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
60700  if( !pRet ){
60701    return SQLITE_NOMEM_BKPT;
60702  }
60703
60704  pRet->pVfs = pVfs;
60705  pRet->pWalFd = (sqlite3_file *)&pRet[1];
60706  pRet->pDbFd = pDbFd;
60707  pRet->readLock = -1;
60708  pRet->mxWalSize = mxWalSize;
60709  pRet->zWalName = zWalName;
60710  pRet->syncHeader = 1;
60711  pRet->padToSectorBoundary = 1;
60712  pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
60713
60714  /* Open file handle on the write-ahead log file. */
60715  flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
60716  rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
60717  if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
60718    pRet->readOnly = WAL_RDONLY;
60719  }
60720
60721  if( rc!=SQLITE_OK ){
60722    walIndexClose(pRet, 0);
60723    sqlite3OsClose(pRet->pWalFd);
60724    sqlite3_free(pRet);
60725  }else{
60726    int iDC = sqlite3OsDeviceCharacteristics(pDbFd);
60727    if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
60728    if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
60729      pRet->padToSectorBoundary = 0;
60730    }
60731    *ppWal = pRet;
60732    WALTRACE(("WAL%d: opened\n", pRet));
60733  }
60734  return rc;
60735}
60736
60737/*
60738** Change the size to which the WAL file is trucated on each reset.
60739*/
60740SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){
60741  if( pWal ) pWal->mxWalSize = iLimit;
60742}
60743
60744/*
60745** Find the smallest page number out of all pages held in the WAL that
60746** has not been returned by any prior invocation of this method on the
60747** same WalIterator object.   Write into *piFrame the frame index where
60748** that page was last written into the WAL.  Write into *piPage the page
60749** number.
60750**
60751** Return 0 on success.  If there are no pages in the WAL with a page
60752** number larger than *piPage, then return 1.
60753*/
60754static int walIteratorNext(
60755  WalIterator *p,               /* Iterator */
60756  u32 *piPage,                  /* OUT: The page number of the next page */
60757  u32 *piFrame                  /* OUT: Wal frame index of next page */
60758){
60759  u32 iMin;                     /* Result pgno must be greater than iMin */
60760  u32 iRet = 0xFFFFFFFF;        /* 0xffffffff is never a valid page number */
60761  int i;                        /* For looping through segments */
60762
60763  iMin = p->iPrior;
60764  assert( iMin<0xffffffff );
60765  for(i=p->nSegment-1; i>=0; i--){
60766    struct WalSegment *pSegment = &p->aSegment[i];
60767    while( pSegment->iNext<pSegment->nEntry ){
60768      u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
60769      if( iPg>iMin ){
60770        if( iPg<iRet ){
60771          iRet = iPg;
60772          *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
60773        }
60774        break;
60775      }
60776      pSegment->iNext++;
60777    }
60778  }
60779
60780  *piPage = p->iPrior = iRet;
60781  return (iRet==0xFFFFFFFF);
60782}
60783
60784/*
60785** This function merges two sorted lists into a single sorted list.
60786**
60787** aLeft[] and aRight[] are arrays of indices.  The sort key is
60788** aContent[aLeft[]] and aContent[aRight[]].  Upon entry, the following
60789** is guaranteed for all J<K:
60790**
60791**        aContent[aLeft[J]] < aContent[aLeft[K]]
60792**        aContent[aRight[J]] < aContent[aRight[K]]
60793**
60794** This routine overwrites aRight[] with a new (probably longer) sequence
60795** of indices such that the aRight[] contains every index that appears in
60796** either aLeft[] or the old aRight[] and such that the second condition
60797** above is still met.
60798**
60799** The aContent[aLeft[X]] values will be unique for all X.  And the
60800** aContent[aRight[X]] values will be unique too.  But there might be
60801** one or more combinations of X and Y such that
60802**
60803**      aLeft[X]!=aRight[Y]  &&  aContent[aLeft[X]] == aContent[aRight[Y]]
60804**
60805** When that happens, omit the aLeft[X] and use the aRight[Y] index.
60806*/
60807static void walMerge(
60808  const u32 *aContent,            /* Pages in wal - keys for the sort */
60809  ht_slot *aLeft,                 /* IN: Left hand input list */
60810  int nLeft,                      /* IN: Elements in array *paLeft */
60811  ht_slot **paRight,              /* IN/OUT: Right hand input list */
60812  int *pnRight,                   /* IN/OUT: Elements in *paRight */
60813  ht_slot *aTmp                   /* Temporary buffer */
60814){
60815  int iLeft = 0;                  /* Current index in aLeft */
60816  int iRight = 0;                 /* Current index in aRight */
60817  int iOut = 0;                   /* Current index in output buffer */
60818  int nRight = *pnRight;
60819  ht_slot *aRight = *paRight;
60820
60821  assert( nLeft>0 && nRight>0 );
60822  while( iRight<nRight || iLeft<nLeft ){
60823    ht_slot logpage;
60824    Pgno dbpage;
60825
60826    if( (iLeft<nLeft)
60827     && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
60828    ){
60829      logpage = aLeft[iLeft++];
60830    }else{
60831      logpage = aRight[iRight++];
60832    }
60833    dbpage = aContent[logpage];
60834
60835    aTmp[iOut++] = logpage;
60836    if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
60837
60838    assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
60839    assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
60840  }
60841
60842  *paRight = aLeft;
60843  *pnRight = iOut;
60844  memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
60845}
60846
60847/*
60848** Sort the elements in list aList using aContent[] as the sort key.
60849** Remove elements with duplicate keys, preferring to keep the
60850** larger aList[] values.
60851**
60852** The aList[] entries are indices into aContent[].  The values in
60853** aList[] are to be sorted so that for all J<K:
60854**
60855**      aContent[aList[J]] < aContent[aList[K]]
60856**
60857** For any X and Y such that
60858**
60859**      aContent[aList[X]] == aContent[aList[Y]]
60860**
60861** Keep the larger of the two values aList[X] and aList[Y] and discard
60862** the smaller.
60863*/
60864static void walMergesort(
60865  const u32 *aContent,            /* Pages in wal */
60866  ht_slot *aBuffer,               /* Buffer of at least *pnList items to use */
60867  ht_slot *aList,                 /* IN/OUT: List to sort */
60868  int *pnList                     /* IN/OUT: Number of elements in aList[] */
60869){
60870  struct Sublist {
60871    int nList;                    /* Number of elements in aList */
60872    ht_slot *aList;               /* Pointer to sub-list content */
60873  };
60874
60875  const int nList = *pnList;      /* Size of input list */
60876  int nMerge = 0;                 /* Number of elements in list aMerge */
60877  ht_slot *aMerge = 0;            /* List to be merged */
60878  int iList;                      /* Index into input list */
60879  u32 iSub = 0;                   /* Index into aSub array */
60880  struct Sublist aSub[13];        /* Array of sub-lists */
60881
60882  memset(aSub, 0, sizeof(aSub));
60883  assert( nList<=HASHTABLE_NPAGE && nList>0 );
60884  assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
60885
60886  for(iList=0; iList<nList; iList++){
60887    nMerge = 1;
60888    aMerge = &aList[iList];
60889    for(iSub=0; iList & (1<<iSub); iSub++){
60890      struct Sublist *p;
60891      assert( iSub<ArraySize(aSub) );
60892      p = &aSub[iSub];
60893      assert( p->aList && p->nList<=(1<<iSub) );
60894      assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
60895      walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
60896    }
60897    aSub[iSub].aList = aMerge;
60898    aSub[iSub].nList = nMerge;
60899  }
60900
60901  for(iSub++; iSub<ArraySize(aSub); iSub++){
60902    if( nList & (1<<iSub) ){
60903      struct Sublist *p;
60904      assert( iSub<ArraySize(aSub) );
60905      p = &aSub[iSub];
60906      assert( p->nList<=(1<<iSub) );
60907      assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
60908      walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
60909    }
60910  }
60911  assert( aMerge==aList );
60912  *pnList = nMerge;
60913
60914#ifdef SQLITE_DEBUG
60915  {
60916    int i;
60917    for(i=1; i<*pnList; i++){
60918      assert( aContent[aList[i]] > aContent[aList[i-1]] );
60919    }
60920  }
60921#endif
60922}
60923
60924/*
60925** Free an iterator allocated by walIteratorInit().
60926*/
60927static void walIteratorFree(WalIterator *p){
60928  sqlite3_free(p);
60929}
60930
60931/*
60932** Construct a WalInterator object that can be used to loop over all
60933** pages in the WAL following frame nBackfill in ascending order. Frames
60934** nBackfill or earlier may be included - excluding them is an optimization
60935** only. The caller must hold the checkpoint lock.
60936**
60937** On success, make *pp point to the newly allocated WalInterator object
60938** return SQLITE_OK. Otherwise, return an error code. If this routine
60939** returns an error, the value of *pp is undefined.
60940**
60941** The calling routine should invoke walIteratorFree() to destroy the
60942** WalIterator object when it has finished with it.
60943*/
60944static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
60945  WalIterator *p;                 /* Return value */
60946  int nSegment;                   /* Number of segments to merge */
60947  u32 iLast;                      /* Last frame in log */
60948  sqlite3_int64 nByte;            /* Number of bytes to allocate */
60949  int i;                          /* Iterator variable */
60950  ht_slot *aTmp;                  /* Temp space used by merge-sort */
60951  int rc = SQLITE_OK;             /* Return Code */
60952
60953  /* This routine only runs while holding the checkpoint lock. And
60954  ** it only runs if there is actually content in the log (mxFrame>0).
60955  */
60956  assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
60957  iLast = pWal->hdr.mxFrame;
60958
60959  /* Allocate space for the WalIterator object. */
60960  nSegment = walFramePage(iLast) + 1;
60961  nByte = sizeof(WalIterator)
60962        + (nSegment-1)*sizeof(struct WalSegment)
60963        + iLast*sizeof(ht_slot);
60964  p = (WalIterator *)sqlite3_malloc64(nByte);
60965  if( !p ){
60966    return SQLITE_NOMEM_BKPT;
60967  }
60968  memset(p, 0, nByte);
60969  p->nSegment = nSegment;
60970
60971  /* Allocate temporary space used by the merge-sort routine. This block
60972  ** of memory will be freed before this function returns.
60973  */
60974  aTmp = (ht_slot *)sqlite3_malloc64(
60975      sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
60976  );
60977  if( !aTmp ){
60978    rc = SQLITE_NOMEM_BKPT;
60979  }
60980
60981  for(i=walFramePage(nBackfill+1); rc==SQLITE_OK && i<nSegment; i++){
60982    WalHashLoc sLoc;
60983
60984    rc = walHashGet(pWal, i, &sLoc);
60985    if( rc==SQLITE_OK ){
60986      int j;                      /* Counter variable */
60987      int nEntry;                 /* Number of entries in this segment */
60988      ht_slot *aIndex;            /* Sorted index for this segment */
60989
60990      sLoc.aPgno++;
60991      if( (i+1)==nSegment ){
60992        nEntry = (int)(iLast - sLoc.iZero);
60993      }else{
60994        nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
60995      }
60996      aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
60997      sLoc.iZero++;
60998
60999      for(j=0; j<nEntry; j++){
61000        aIndex[j] = (ht_slot)j;
61001      }
61002      walMergesort((u32 *)sLoc.aPgno, aTmp, aIndex, &nEntry);
61003      p->aSegment[i].iZero = sLoc.iZero;
61004      p->aSegment[i].nEntry = nEntry;
61005      p->aSegment[i].aIndex = aIndex;
61006      p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
61007    }
61008  }
61009  sqlite3_free(aTmp);
61010
61011  if( rc!=SQLITE_OK ){
61012    walIteratorFree(p);
61013    p = 0;
61014  }
61015  *pp = p;
61016  return rc;
61017}
61018
61019#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
61020/*
61021** Attempt to enable blocking locks. Blocking locks are enabled only if (a)
61022** they are supported by the VFS, and (b) the database handle is configured
61023** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
61024** or 0 otherwise.
61025*/
61026static int walEnableBlocking(Wal *pWal){
61027  int res = 0;
61028  if( pWal->db ){
61029    int tmout = pWal->db->busyTimeout;
61030    if( tmout ){
61031      int rc;
61032      rc = sqlite3OsFileControl(
61033          pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout
61034      );
61035      res = (rc==SQLITE_OK);
61036    }
61037  }
61038  return res;
61039}
61040
61041/*
61042** Disable blocking locks.
61043*/
61044static void walDisableBlocking(Wal *pWal){
61045  int tmout = 0;
61046  sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout);
61047}
61048
61049/*
61050** If parameter bLock is true, attempt to enable blocking locks, take
61051** the WRITER lock, and then disable blocking locks. If blocking locks
61052** cannot be enabled, no attempt to obtain the WRITER lock is made. Return
61053** an SQLite error code if an error occurs, or SQLITE_OK otherwise. It is not
61054** an error if blocking locks can not be enabled.
61055**
61056** If the bLock parameter is false and the WRITER lock is held, release it.
61057*/
61058SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock){
61059  int rc = SQLITE_OK;
61060  assert( pWal->readLock<0 || bLock==0 );
61061  if( bLock ){
61062    assert( pWal->db );
61063    if( walEnableBlocking(pWal) ){
61064      rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
61065      if( rc==SQLITE_OK ){
61066        pWal->writeLock = 1;
61067      }
61068      walDisableBlocking(pWal);
61069    }
61070  }else if( pWal->writeLock ){
61071    walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
61072    pWal->writeLock = 0;
61073  }
61074  return rc;
61075}
61076
61077/*
61078** Set the database handle used to determine if blocking locks are required.
61079*/
61080SQLITE_PRIVATE void sqlite3WalDb(Wal *pWal, sqlite3 *db){
61081  pWal->db = db;
61082}
61083
61084/*
61085** Take an exclusive WRITE lock. Blocking if so configured.
61086*/
61087static int walLockWriter(Wal *pWal){
61088  int rc;
61089  walEnableBlocking(pWal);
61090  rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
61091  walDisableBlocking(pWal);
61092  return rc;
61093}
61094#else
61095# define walEnableBlocking(x) 0
61096# define walDisableBlocking(x)
61097# define walLockWriter(pWal) walLockExclusive((pWal), WAL_WRITE_LOCK, 1)
61098# define sqlite3WalDb(pWal, db)
61099#endif   /* ifdef SQLITE_ENABLE_SETLK_TIMEOUT */
61100
61101
61102/*
61103** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
61104** n. If the attempt fails and parameter xBusy is not NULL, then it is a
61105** busy-handler function. Invoke it and retry the lock until either the
61106** lock is successfully obtained or the busy-handler returns 0.
61107*/
61108static int walBusyLock(
61109  Wal *pWal,                      /* WAL connection */
61110  int (*xBusy)(void*),            /* Function to call when busy */
61111  void *pBusyArg,                 /* Context argument for xBusyHandler */
61112  int lockIdx,                    /* Offset of first byte to lock */
61113  int n                           /* Number of bytes to lock */
61114){
61115  int rc;
61116  do {
61117    rc = walLockExclusive(pWal, lockIdx, n);
61118  }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
61119#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
61120  if( rc==SQLITE_BUSY_TIMEOUT ){
61121    walDisableBlocking(pWal);
61122    rc = SQLITE_BUSY;
61123  }
61124#endif
61125  return rc;
61126}
61127
61128/*
61129** The cache of the wal-index header must be valid to call this function.
61130** Return the page-size in bytes used by the database.
61131*/
61132static int walPagesize(Wal *pWal){
61133  return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
61134}
61135
61136/*
61137** The following is guaranteed when this function is called:
61138**
61139**   a) the WRITER lock is held,
61140**   b) the entire log file has been checkpointed, and
61141**   c) any existing readers are reading exclusively from the database
61142**      file - there are no readers that may attempt to read a frame from
61143**      the log file.
61144**
61145** This function updates the shared-memory structures so that the next
61146** client to write to the database (which may be this one) does so by
61147** writing frames into the start of the log file.
61148**
61149** The value of parameter salt1 is used as the aSalt[1] value in the
61150** new wal-index header. It should be passed a pseudo-random value (i.e.
61151** one obtained from sqlite3_randomness()).
61152*/
61153static void walRestartHdr(Wal *pWal, u32 salt1){
61154  volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
61155  int i;                          /* Loop counter */
61156  u32 *aSalt = pWal->hdr.aSalt;   /* Big-endian salt values */
61157  pWal->nCkpt++;
61158  pWal->hdr.mxFrame = 0;
61159  sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
61160  memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
61161  walIndexWriteHdr(pWal);
61162  AtomicStore(&pInfo->nBackfill, 0);
61163  pInfo->nBackfillAttempted = 0;
61164  pInfo->aReadMark[1] = 0;
61165  for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
61166  assert( pInfo->aReadMark[0]==0 );
61167}
61168
61169/*
61170** Copy as much content as we can from the WAL back into the database file
61171** in response to an sqlite3_wal_checkpoint() request or the equivalent.
61172**
61173** The amount of information copies from WAL to database might be limited
61174** by active readers.  This routine will never overwrite a database page
61175** that a concurrent reader might be using.
61176**
61177** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
61178** SQLite is in WAL-mode in synchronous=NORMAL.  That means that if
61179** checkpoints are always run by a background thread or background
61180** process, foreground threads will never block on a lengthy fsync call.
61181**
61182** Fsync is called on the WAL before writing content out of the WAL and
61183** into the database.  This ensures that if the new content is persistent
61184** in the WAL and can be recovered following a power-loss or hard reset.
61185**
61186** Fsync is also called on the database file if (and only if) the entire
61187** WAL content is copied into the database file.  This second fsync makes
61188** it safe to delete the WAL since the new content will persist in the
61189** database file.
61190**
61191** This routine uses and updates the nBackfill field of the wal-index header.
61192** This is the only routine that will increase the value of nBackfill.
61193** (A WAL reset or recovery will revert nBackfill to zero, but not increase
61194** its value.)
61195**
61196** The caller must be holding sufficient locks to ensure that no other
61197** checkpoint is running (in any other thread or process) at the same
61198** time.
61199*/
61200static int walCheckpoint(
61201  Wal *pWal,                      /* Wal connection */
61202  sqlite3 *db,                    /* Check for interrupts on this handle */
61203  int eMode,                      /* One of PASSIVE, FULL or RESTART */
61204  int (*xBusy)(void*),            /* Function to call when busy */
61205  void *pBusyArg,                 /* Context argument for xBusyHandler */
61206  int sync_flags,                 /* Flags for OsSync() (or 0) */
61207  u8 *zBuf                        /* Temporary buffer to use */
61208){
61209  int rc = SQLITE_OK;             /* Return code */
61210  int szPage;                     /* Database page-size */
61211  WalIterator *pIter = 0;         /* Wal iterator context */
61212  u32 iDbpage = 0;                /* Next database page to write */
61213  u32 iFrame = 0;                 /* Wal frame containing data for iDbpage */
61214  u32 mxSafeFrame;                /* Max frame that can be backfilled */
61215  u32 mxPage;                     /* Max database page to write */
61216  int i;                          /* Loop counter */
61217  volatile WalCkptInfo *pInfo;    /* The checkpoint status information */
61218
61219  szPage = walPagesize(pWal);
61220  testcase( szPage<=32768 );
61221  testcase( szPage>=65536 );
61222  pInfo = walCkptInfo(pWal);
61223  if( pInfo->nBackfill<pWal->hdr.mxFrame ){
61224
61225    /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
61226    ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
61227    assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
61228
61229    /* Compute in mxSafeFrame the index of the last frame of the WAL that is
61230    ** safe to write into the database.  Frames beyond mxSafeFrame might
61231    ** overwrite database pages that are in use by active readers and thus
61232    ** cannot be backfilled from the WAL.
61233    */
61234    mxSafeFrame = pWal->hdr.mxFrame;
61235    mxPage = pWal->hdr.nPage;
61236    for(i=1; i<WAL_NREADER; i++){
61237      u32 y = AtomicLoad(pInfo->aReadMark+i);
61238      if( mxSafeFrame>y ){
61239        assert( y<=pWal->hdr.mxFrame );
61240        rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
61241        if( rc==SQLITE_OK ){
61242          u32 iMark = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
61243          AtomicStore(pInfo->aReadMark+i, iMark);
61244          walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
61245        }else if( rc==SQLITE_BUSY ){
61246          mxSafeFrame = y;
61247          xBusy = 0;
61248        }else{
61249          goto walcheckpoint_out;
61250        }
61251      }
61252    }
61253
61254    /* Allocate the iterator */
61255    if( pInfo->nBackfill<mxSafeFrame ){
61256      rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
61257      assert( rc==SQLITE_OK || pIter==0 );
61258    }
61259
61260    if( pIter
61261     && (rc = walBusyLock(pWal,xBusy,pBusyArg,WAL_READ_LOCK(0),1))==SQLITE_OK
61262    ){
61263      u32 nBackfill = pInfo->nBackfill;
61264
61265      pInfo->nBackfillAttempted = mxSafeFrame;
61266
61267      /* Sync the WAL to disk */
61268      rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
61269
61270      /* If the database may grow as a result of this checkpoint, hint
61271      ** about the eventual size of the db file to the VFS layer.
61272      */
61273      if( rc==SQLITE_OK ){
61274        i64 nReq = ((i64)mxPage * szPage);
61275        i64 nSize;                    /* Current size of database file */
61276        sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START, 0);
61277        rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
61278        if( rc==SQLITE_OK && nSize<nReq ){
61279          sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
61280        }
61281      }
61282
61283
61284      /* Iterate through the contents of the WAL, copying data to the db file */
61285      while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
61286        i64 iOffset;
61287        assert( walFramePgno(pWal, iFrame)==iDbpage );
61288        if( AtomicLoad(&db->u1.isInterrupted) ){
61289          rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
61290          break;
61291        }
61292        if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){
61293          continue;
61294        }
61295        iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
61296        /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
61297        rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
61298        if( rc!=SQLITE_OK ) break;
61299        iOffset = (iDbpage-1)*(i64)szPage;
61300        testcase( IS_BIG_INT(iOffset) );
61301        rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
61302        if( rc!=SQLITE_OK ) break;
61303      }
61304      sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0);
61305
61306      /* If work was actually accomplished... */
61307      if( rc==SQLITE_OK ){
61308        if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
61309          i64 szDb = pWal->hdr.nPage*(i64)szPage;
61310          testcase( IS_BIG_INT(szDb) );
61311          rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
61312          if( rc==SQLITE_OK ){
61313            rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
61314          }
61315        }
61316        if( rc==SQLITE_OK ){
61317          AtomicStore(&pInfo->nBackfill, mxSafeFrame);
61318        }
61319      }
61320
61321      /* Release the reader lock held while backfilling */
61322      walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
61323    }
61324
61325    if( rc==SQLITE_BUSY ){
61326      /* Reset the return code so as not to report a checkpoint failure
61327      ** just because there are active readers.  */
61328      rc = SQLITE_OK;
61329    }
61330  }
61331
61332  /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the
61333  ** entire wal file has been copied into the database file, then block
61334  ** until all readers have finished using the wal file. This ensures that
61335  ** the next process to write to the database restarts the wal file.
61336  */
61337  if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
61338    assert( pWal->writeLock );
61339    if( pInfo->nBackfill<pWal->hdr.mxFrame ){
61340      rc = SQLITE_BUSY;
61341    }else if( eMode>=SQLITE_CHECKPOINT_RESTART ){
61342      u32 salt1;
61343      sqlite3_randomness(4, &salt1);
61344      assert( pInfo->nBackfill==pWal->hdr.mxFrame );
61345      rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
61346      if( rc==SQLITE_OK ){
61347        if( eMode==SQLITE_CHECKPOINT_TRUNCATE ){
61348          /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
61349          ** SQLITE_CHECKPOINT_RESTART with the addition that it also
61350          ** truncates the log file to zero bytes just prior to a
61351          ** successful return.
61352          **
61353          ** In theory, it might be safe to do this without updating the
61354          ** wal-index header in shared memory, as all subsequent reader or
61355          ** writer clients should see that the entire log file has been
61356          ** checkpointed and behave accordingly. This seems unsafe though,
61357          ** as it would leave the system in a state where the contents of
61358          ** the wal-index header do not match the contents of the
61359          ** file-system. To avoid this, update the wal-index header to
61360          ** indicate that the log file contains zero valid frames.  */
61361          walRestartHdr(pWal, salt1);
61362          rc = sqlite3OsTruncate(pWal->pWalFd, 0);
61363        }
61364        walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
61365      }
61366    }
61367  }
61368
61369 walcheckpoint_out:
61370  walIteratorFree(pIter);
61371  return rc;
61372}
61373
61374/*
61375** If the WAL file is currently larger than nMax bytes in size, truncate
61376** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
61377*/
61378static void walLimitSize(Wal *pWal, i64 nMax){
61379  i64 sz;
61380  int rx;
61381  sqlite3BeginBenignMalloc();
61382  rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
61383  if( rx==SQLITE_OK && (sz > nMax ) ){
61384    rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
61385  }
61386  sqlite3EndBenignMalloc();
61387  if( rx ){
61388    sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
61389  }
61390}
61391
61392/*
61393** Close a connection to a log file.
61394*/
61395SQLITE_PRIVATE int sqlite3WalClose(
61396  Wal *pWal,                      /* Wal to close */
61397  sqlite3 *db,                    /* For interrupt flag */
61398  int sync_flags,                 /* Flags to pass to OsSync() (or 0) */
61399  int nBuf,
61400  u8 *zBuf                        /* Buffer of at least nBuf bytes */
61401){
61402  int rc = SQLITE_OK;
61403  if( pWal ){
61404    int isDelete = 0;             /* True to unlink wal and wal-index files */
61405
61406    /* If an EXCLUSIVE lock can be obtained on the database file (using the
61407    ** ordinary, rollback-mode locking methods, this guarantees that the
61408    ** connection associated with this log file is the only connection to
61409    ** the database. In this case checkpoint the database and unlink both
61410    ** the wal and wal-index files.
61411    **
61412    ** The EXCLUSIVE lock is not released before returning.
61413    */
61414    if( zBuf!=0
61415     && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
61416    ){
61417      if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
61418        pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
61419      }
61420      rc = sqlite3WalCheckpoint(pWal, db,
61421          SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
61422      );
61423      if( rc==SQLITE_OK ){
61424        int bPersist = -1;
61425        sqlite3OsFileControlHint(
61426            pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
61427        );
61428        if( bPersist!=1 ){
61429          /* Try to delete the WAL file if the checkpoint completed and
61430          ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
61431          ** mode (!bPersist) */
61432          isDelete = 1;
61433        }else if( pWal->mxWalSize>=0 ){
61434          /* Try to truncate the WAL file to zero bytes if the checkpoint
61435          ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
61436          ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
61437          ** non-negative value (pWal->mxWalSize>=0).  Note that we truncate
61438          ** to zero bytes as truncating to the journal_size_limit might
61439          ** leave a corrupt WAL file on disk. */
61440          walLimitSize(pWal, 0);
61441        }
61442      }
61443    }
61444
61445    walIndexClose(pWal, isDelete);
61446    sqlite3OsClose(pWal->pWalFd);
61447    if( isDelete ){
61448      sqlite3BeginBenignMalloc();
61449      sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
61450      sqlite3EndBenignMalloc();
61451    }
61452    WALTRACE(("WAL%p: closed\n", pWal));
61453    sqlite3_free((void *)pWal->apWiData);
61454    sqlite3_free(pWal);
61455  }
61456  return rc;
61457}
61458
61459/*
61460** Try to read the wal-index header.  Return 0 on success and 1 if
61461** there is a problem.
61462**
61463** The wal-index is in shared memory.  Another thread or process might
61464** be writing the header at the same time this procedure is trying to
61465** read it, which might result in inconsistency.  A dirty read is detected
61466** by verifying that both copies of the header are the same and also by
61467** a checksum on the header.
61468**
61469** If and only if the read is consistent and the header is different from
61470** pWal->hdr, then pWal->hdr is updated to the content of the new header
61471** and *pChanged is set to 1.
61472**
61473** If the checksum cannot be verified return non-zero. If the header
61474** is read successfully and the checksum verified, return zero.
61475*/
61476static SQLITE_NO_TSAN int walIndexTryHdr(Wal *pWal, int *pChanged){
61477  u32 aCksum[2];                  /* Checksum on the header content */
61478  WalIndexHdr h1, h2;             /* Two copies of the header content */
61479  WalIndexHdr volatile *aHdr;     /* Header in shared memory */
61480
61481  /* The first page of the wal-index must be mapped at this point. */
61482  assert( pWal->nWiData>0 && pWal->apWiData[0] );
61483
61484  /* Read the header. This might happen concurrently with a write to the
61485  ** same area of shared memory on a different CPU in a SMP,
61486  ** meaning it is possible that an inconsistent snapshot is read
61487  ** from the file. If this happens, return non-zero.
61488  **
61489  ** tag-20200519-1:
61490  ** There are two copies of the header at the beginning of the wal-index.
61491  ** When reading, read [0] first then [1].  Writes are in the reverse order.
61492  ** Memory barriers are used to prevent the compiler or the hardware from
61493  ** reordering the reads and writes.  TSAN and similar tools can sometimes
61494  ** give false-positive warnings about these accesses because the tools do not
61495  ** account for the double-read and the memory barrier. The use of mutexes
61496  ** here would be problematic as the memory being accessed is potentially
61497  ** shared among multiple processes and not all mutex implementions work
61498  ** reliably in that environment.
61499  */
61500  aHdr = walIndexHdr(pWal);
61501  memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
61502  walShmBarrier(pWal);
61503  memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
61504
61505  if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
61506    return 1;   /* Dirty read */
61507  }
61508  if( h1.isInit==0 ){
61509    return 1;   /* Malformed header - probably all zeros */
61510  }
61511  walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
61512  if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
61513    return 1;   /* Checksum does not match */
61514  }
61515
61516  if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
61517    *pChanged = 1;
61518    memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
61519    pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
61520    testcase( pWal->szPage<=32768 );
61521    testcase( pWal->szPage>=65536 );
61522  }
61523
61524  /* The header was successfully read. Return zero. */
61525  return 0;
61526}
61527
61528/*
61529** This is the value that walTryBeginRead returns when it needs to
61530** be retried.
61531*/
61532#define WAL_RETRY  (-1)
61533
61534/*
61535** Read the wal-index header from the wal-index and into pWal->hdr.
61536** If the wal-header appears to be corrupt, try to reconstruct the
61537** wal-index from the WAL before returning.
61538**
61539** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
61540** changed by this operation.  If pWal->hdr is unchanged, set *pChanged
61541** to 0.
61542**
61543** If the wal-index header is successfully read, return SQLITE_OK.
61544** Otherwise an SQLite error code.
61545*/
61546static int walIndexReadHdr(Wal *pWal, int *pChanged){
61547  int rc;                         /* Return code */
61548  int badHdr;                     /* True if a header read failed */
61549  volatile u32 *page0;            /* Chunk of wal-index containing header */
61550
61551  /* Ensure that page 0 of the wal-index (the page that contains the
61552  ** wal-index header) is mapped. Return early if an error occurs here.
61553  */
61554  assert( pChanged );
61555  rc = walIndexPage(pWal, 0, &page0);
61556  if( rc!=SQLITE_OK ){
61557    assert( rc!=SQLITE_READONLY ); /* READONLY changed to OK in walIndexPage */
61558    if( rc==SQLITE_READONLY_CANTINIT ){
61559      /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
61560      ** was openable but is not writable, and this thread is unable to
61561      ** confirm that another write-capable connection has the shared-memory
61562      ** open, and hence the content of the shared-memory is unreliable,
61563      ** since the shared-memory might be inconsistent with the WAL file
61564      ** and there is no writer on hand to fix it. */
61565      assert( page0==0 );
61566      assert( pWal->writeLock==0 );
61567      assert( pWal->readOnly & WAL_SHM_RDONLY );
61568      pWal->bShmUnreliable = 1;
61569      pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
61570      *pChanged = 1;
61571    }else{
61572      return rc; /* Any other non-OK return is just an error */
61573    }
61574  }else{
61575    /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
61576    ** is zero, which prevents the SHM from growing */
61577    testcase( page0!=0 );
61578  }
61579  assert( page0!=0 || pWal->writeLock==0 );
61580
61581  /* If the first page of the wal-index has been mapped, try to read the
61582  ** wal-index header immediately, without holding any lock. This usually
61583  ** works, but may fail if the wal-index header is corrupt or currently
61584  ** being modified by another thread or process.
61585  */
61586  badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
61587
61588  /* If the first attempt failed, it might have been due to a race
61589  ** with a writer.  So get a WRITE lock and try again.
61590  */
61591  if( badHdr ){
61592    if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
61593      if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
61594        walUnlockShared(pWal, WAL_WRITE_LOCK);
61595        rc = SQLITE_READONLY_RECOVERY;
61596      }
61597    }else{
61598      int bWriteLock = pWal->writeLock;
61599      if( bWriteLock || SQLITE_OK==(rc = walLockWriter(pWal)) ){
61600        pWal->writeLock = 1;
61601        if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
61602          badHdr = walIndexTryHdr(pWal, pChanged);
61603          if( badHdr ){
61604            /* If the wal-index header is still malformed even while holding
61605            ** a WRITE lock, it can only mean that the header is corrupted and
61606            ** needs to be reconstructed.  So run recovery to do exactly that.
61607            */
61608            rc = walIndexRecover(pWal);
61609            *pChanged = 1;
61610          }
61611        }
61612        if( bWriteLock==0 ){
61613          pWal->writeLock = 0;
61614          walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
61615        }
61616      }
61617    }
61618  }
61619
61620  /* If the header is read successfully, check the version number to make
61621  ** sure the wal-index was not constructed with some future format that
61622  ** this version of SQLite cannot understand.
61623  */
61624  if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
61625    rc = SQLITE_CANTOPEN_BKPT;
61626  }
61627  if( pWal->bShmUnreliable ){
61628    if( rc!=SQLITE_OK ){
61629      walIndexClose(pWal, 0);
61630      pWal->bShmUnreliable = 0;
61631      assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
61632      /* walIndexRecover() might have returned SHORT_READ if a concurrent
61633      ** writer truncated the WAL out from under it.  If that happens, it
61634      ** indicates that a writer has fixed the SHM file for us, so retry */
61635      if( rc==SQLITE_IOERR_SHORT_READ ) rc = WAL_RETRY;
61636    }
61637    pWal->exclusiveMode = WAL_NORMAL_MODE;
61638  }
61639
61640  return rc;
61641}
61642
61643/*
61644** Open a transaction in a connection where the shared-memory is read-only
61645** and where we cannot verify that there is a separate write-capable connection
61646** on hand to keep the shared-memory up-to-date with the WAL file.
61647**
61648** This can happen, for example, when the shared-memory is implemented by
61649** memory-mapping a *-shm file, where a prior writer has shut down and
61650** left the *-shm file on disk, and now the present connection is trying
61651** to use that database but lacks write permission on the *-shm file.
61652** Other scenarios are also possible, depending on the VFS implementation.
61653**
61654** Precondition:
61655**
61656**    The *-wal file has been read and an appropriate wal-index has been
61657**    constructed in pWal->apWiData[] using heap memory instead of shared
61658**    memory.
61659**
61660** If this function returns SQLITE_OK, then the read transaction has
61661** been successfully opened. In this case output variable (*pChanged)
61662** is set to true before returning if the caller should discard the
61663** contents of the page cache before proceeding. Or, if it returns
61664** WAL_RETRY, then the heap memory wal-index has been discarded and
61665** the caller should retry opening the read transaction from the
61666** beginning (including attempting to map the *-shm file).
61667**
61668** If an error occurs, an SQLite error code is returned.
61669*/
61670static int walBeginShmUnreliable(Wal *pWal, int *pChanged){
61671  i64 szWal;                      /* Size of wal file on disk in bytes */
61672  i64 iOffset;                    /* Current offset when reading wal file */
61673  u8 aBuf[WAL_HDRSIZE];           /* Buffer to load WAL header into */
61674  u8 *aFrame = 0;                 /* Malloc'd buffer to load entire frame */
61675  int szFrame;                    /* Number of bytes in buffer aFrame[] */
61676  u8 *aData;                      /* Pointer to data part of aFrame buffer */
61677  volatile void *pDummy;          /* Dummy argument for xShmMap */
61678  int rc;                         /* Return code */
61679  u32 aSaveCksum[2];              /* Saved copy of pWal->hdr.aFrameCksum */
61680
61681  assert( pWal->bShmUnreliable );
61682  assert( pWal->readOnly & WAL_SHM_RDONLY );
61683  assert( pWal->nWiData>0 && pWal->apWiData[0] );
61684
61685  /* Take WAL_READ_LOCK(0). This has the effect of preventing any
61686  ** writers from running a checkpoint, but does not stop them
61687  ** from running recovery.  */
61688  rc = walLockShared(pWal, WAL_READ_LOCK(0));
61689  if( rc!=SQLITE_OK ){
61690    if( rc==SQLITE_BUSY ) rc = WAL_RETRY;
61691    goto begin_unreliable_shm_out;
61692  }
61693  pWal->readLock = 0;
61694
61695  /* Check to see if a separate writer has attached to the shared-memory area,
61696  ** thus making the shared-memory "reliable" again.  Do this by invoking
61697  ** the xShmMap() routine of the VFS and looking to see if the return
61698  ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT.
61699  **
61700  ** If the shared-memory is now "reliable" return WAL_RETRY, which will
61701  ** cause the heap-memory WAL-index to be discarded and the actual
61702  ** shared memory to be used in its place.
61703  **
61704  ** This step is important because, even though this connection is holding
61705  ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might
61706  ** have already checkpointed the WAL file and, while the current
61707  ** is active, wrap the WAL and start overwriting frames that this
61708  ** process wants to use.
61709  **
61710  ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has
61711  ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY
61712  ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations,
61713  ** even if some external agent does a "chmod" to make the shared-memory
61714  ** writable by us, until sqlite3OsShmUnmap() has been called.
61715  ** This is a requirement on the VFS implementation.
61716   */
61717  rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
61718  assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
61719  if( rc!=SQLITE_READONLY_CANTINIT ){
61720    rc = (rc==SQLITE_READONLY ? WAL_RETRY : rc);
61721    goto begin_unreliable_shm_out;
61722  }
61723
61724  /* We reach this point only if the real shared-memory is still unreliable.
61725  ** Assume the in-memory WAL-index substitute is correct and load it
61726  ** into pWal->hdr.
61727  */
61728  memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
61729
61730  /* Make sure some writer hasn't come in and changed the WAL file out
61731  ** from under us, then disconnected, while we were not looking.
61732  */
61733  rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
61734  if( rc!=SQLITE_OK ){
61735    goto begin_unreliable_shm_out;
61736  }
61737  if( szWal<WAL_HDRSIZE ){
61738    /* If the wal file is too small to contain a wal-header and the
61739    ** wal-index header has mxFrame==0, then it must be safe to proceed
61740    ** reading the database file only. However, the page cache cannot
61741    ** be trusted, as a read/write connection may have connected, written
61742    ** the db, run a checkpoint, truncated the wal file and disconnected
61743    ** since this client's last read transaction.  */
61744    *pChanged = 1;
61745    rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
61746    goto begin_unreliable_shm_out;
61747  }
61748
61749  /* Check the salt keys at the start of the wal file still match. */
61750  rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
61751  if( rc!=SQLITE_OK ){
61752    goto begin_unreliable_shm_out;
61753  }
61754  if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
61755    /* Some writer has wrapped the WAL file while we were not looking.
61756    ** Return WAL_RETRY which will cause the in-memory WAL-index to be
61757    ** rebuilt. */
61758    rc = WAL_RETRY;
61759    goto begin_unreliable_shm_out;
61760  }
61761
61762  /* Allocate a buffer to read frames into */
61763  szFrame = pWal->hdr.szPage + WAL_FRAME_HDRSIZE;
61764  aFrame = (u8 *)sqlite3_malloc64(szFrame);
61765  if( aFrame==0 ){
61766    rc = SQLITE_NOMEM_BKPT;
61767    goto begin_unreliable_shm_out;
61768  }
61769  aData = &aFrame[WAL_FRAME_HDRSIZE];
61770
61771  /* Check to see if a complete transaction has been appended to the
61772  ** wal file since the heap-memory wal-index was created. If so, the
61773  ** heap-memory wal-index is discarded and WAL_RETRY returned to
61774  ** the caller.  */
61775  aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
61776  aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
61777  for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->hdr.szPage);
61778      iOffset+szFrame<=szWal;
61779      iOffset+=szFrame
61780  ){
61781    u32 pgno;                   /* Database page number for frame */
61782    u32 nTruncate;              /* dbsize field from frame header */
61783
61784    /* Read and decode the next log frame. */
61785    rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
61786    if( rc!=SQLITE_OK ) break;
61787    if( !walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame) ) break;
61788
61789    /* If nTruncate is non-zero, then a complete transaction has been
61790    ** appended to this wal file. Set rc to WAL_RETRY and break out of
61791    ** the loop.  */
61792    if( nTruncate ){
61793      rc = WAL_RETRY;
61794      break;
61795    }
61796  }
61797  pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
61798  pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
61799
61800 begin_unreliable_shm_out:
61801  sqlite3_free(aFrame);
61802  if( rc!=SQLITE_OK ){
61803    int i;
61804    for(i=0; i<pWal->nWiData; i++){
61805      sqlite3_free((void*)pWal->apWiData[i]);
61806      pWal->apWiData[i] = 0;
61807    }
61808    pWal->bShmUnreliable = 0;
61809    sqlite3WalEndReadTransaction(pWal);
61810    *pChanged = 1;
61811  }
61812  return rc;
61813}
61814
61815/*
61816** Attempt to start a read transaction.  This might fail due to a race or
61817** other transient condition.  When that happens, it returns WAL_RETRY to
61818** indicate to the caller that it is safe to retry immediately.
61819**
61820** On success return SQLITE_OK.  On a permanent failure (such an
61821** I/O error or an SQLITE_BUSY because another process is running
61822** recovery) return a positive error code.
61823**
61824** The useWal parameter is true to force the use of the WAL and disable
61825** the case where the WAL is bypassed because it has been completely
61826** checkpointed.  If useWal==0 then this routine calls walIndexReadHdr()
61827** to make a copy of the wal-index header into pWal->hdr.  If the
61828** wal-index header has changed, *pChanged is set to 1 (as an indication
61829** to the caller that the local page cache is obsolete and needs to be
61830** flushed.)  When useWal==1, the wal-index header is assumed to already
61831** be loaded and the pChanged parameter is unused.
61832**
61833** The caller must set the cnt parameter to the number of prior calls to
61834** this routine during the current read attempt that returned WAL_RETRY.
61835** This routine will start taking more aggressive measures to clear the
61836** race conditions after multiple WAL_RETRY returns, and after an excessive
61837** number of errors will ultimately return SQLITE_PROTOCOL.  The
61838** SQLITE_PROTOCOL return indicates that some other process has gone rogue
61839** and is not honoring the locking protocol.  There is a vanishingly small
61840** chance that SQLITE_PROTOCOL could be returned because of a run of really
61841** bad luck when there is lots of contention for the wal-index, but that
61842** possibility is so small that it can be safely neglected, we believe.
61843**
61844** On success, this routine obtains a read lock on
61845** WAL_READ_LOCK(pWal->readLock).  The pWal->readLock integer is
61846** in the range 0 <= pWal->readLock < WAL_NREADER.  If pWal->readLock==(-1)
61847** that means the Wal does not hold any read lock.  The reader must not
61848** access any database page that is modified by a WAL frame up to and
61849** including frame number aReadMark[pWal->readLock].  The reader will
61850** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
61851** Or if pWal->readLock==0, then the reader will ignore the WAL
61852** completely and get all content directly from the database file.
61853** If the useWal parameter is 1 then the WAL will never be ignored and
61854** this routine will always set pWal->readLock>0 on success.
61855** When the read transaction is completed, the caller must release the
61856** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
61857**
61858** This routine uses the nBackfill and aReadMark[] fields of the header
61859** to select a particular WAL_READ_LOCK() that strives to let the
61860** checkpoint process do as much work as possible.  This routine might
61861** update values of the aReadMark[] array in the header, but if it does
61862** so it takes care to hold an exclusive lock on the corresponding
61863** WAL_READ_LOCK() while changing values.
61864*/
61865static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
61866  volatile WalCkptInfo *pInfo;    /* Checkpoint information in wal-index */
61867  u32 mxReadMark;                 /* Largest aReadMark[] value */
61868  int mxI;                        /* Index of largest aReadMark[] value */
61869  int i;                          /* Loop counter */
61870  int rc = SQLITE_OK;             /* Return code  */
61871  u32 mxFrame;                    /* Wal frame to lock to */
61872
61873  assert( pWal->readLock<0 );     /* Not currently locked */
61874
61875  /* useWal may only be set for read/write connections */
61876  assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
61877
61878  /* Take steps to avoid spinning forever if there is a protocol error.
61879  **
61880  ** Circumstances that cause a RETRY should only last for the briefest
61881  ** instances of time.  No I/O or other system calls are done while the
61882  ** locks are held, so the locks should not be held for very long. But
61883  ** if we are unlucky, another process that is holding a lock might get
61884  ** paged out or take a page-fault that is time-consuming to resolve,
61885  ** during the few nanoseconds that it is holding the lock.  In that case,
61886  ** it might take longer than normal for the lock to free.
61887  **
61888  ** After 5 RETRYs, we begin calling sqlite3OsSleep().  The first few
61889  ** calls to sqlite3OsSleep() have a delay of 1 microsecond.  Really this
61890  ** is more of a scheduler yield than an actual delay.  But on the 10th
61891  ** an subsequent retries, the delays start becoming longer and longer,
61892  ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
61893  ** The total delay time before giving up is less than 10 seconds.
61894  */
61895  if( cnt>5 ){
61896    int nDelay = 1;                      /* Pause time in microseconds */
61897    if( cnt>100 ){
61898      VVA_ONLY( pWal->lockError = 1; )
61899      return SQLITE_PROTOCOL;
61900    }
61901    if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
61902    sqlite3OsSleep(pWal->pVfs, nDelay);
61903  }
61904
61905  if( !useWal ){
61906    assert( rc==SQLITE_OK );
61907    if( pWal->bShmUnreliable==0 ){
61908      rc = walIndexReadHdr(pWal, pChanged);
61909    }
61910    if( rc==SQLITE_BUSY ){
61911      /* If there is not a recovery running in another thread or process
61912      ** then convert BUSY errors to WAL_RETRY.  If recovery is known to
61913      ** be running, convert BUSY to BUSY_RECOVERY.  There is a race here
61914      ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
61915      ** would be technically correct.  But the race is benign since with
61916      ** WAL_RETRY this routine will be called again and will probably be
61917      ** right on the second iteration.
61918      */
61919      if( pWal->apWiData[0]==0 ){
61920        /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
61921        ** We assume this is a transient condition, so return WAL_RETRY. The
61922        ** xShmMap() implementation used by the default unix and win32 VFS
61923        ** modules may return SQLITE_BUSY due to a race condition in the
61924        ** code that determines whether or not the shared-memory region
61925        ** must be zeroed before the requested page is returned.
61926        */
61927        rc = WAL_RETRY;
61928      }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
61929        walUnlockShared(pWal, WAL_RECOVER_LOCK);
61930        rc = WAL_RETRY;
61931      }else if( rc==SQLITE_BUSY ){
61932        rc = SQLITE_BUSY_RECOVERY;
61933      }
61934    }
61935    if( rc!=SQLITE_OK ){
61936      return rc;
61937    }
61938    else if( pWal->bShmUnreliable ){
61939      return walBeginShmUnreliable(pWal, pChanged);
61940    }
61941  }
61942
61943  assert( pWal->nWiData>0 );
61944  assert( pWal->apWiData[0]!=0 );
61945  pInfo = walCkptInfo(pWal);
61946  if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
61947#ifdef SQLITE_ENABLE_SNAPSHOT
61948   && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
61949#endif
61950  ){
61951    /* The WAL has been completely backfilled (or it is empty).
61952    ** and can be safely ignored.
61953    */
61954    rc = walLockShared(pWal, WAL_READ_LOCK(0));
61955    walShmBarrier(pWal);
61956    if( rc==SQLITE_OK ){
61957      if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
61958        /* It is not safe to allow the reader to continue here if frames
61959        ** may have been appended to the log before READ_LOCK(0) was obtained.
61960        ** When holding READ_LOCK(0), the reader ignores the entire log file,
61961        ** which implies that the database file contains a trustworthy
61962        ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
61963        ** happening, this is usually correct.
61964        **
61965        ** However, if frames have been appended to the log (or if the log
61966        ** is wrapped and written for that matter) before the READ_LOCK(0)
61967        ** is obtained, that is not necessarily true. A checkpointer may
61968        ** have started to backfill the appended frames but crashed before
61969        ** it finished. Leaving a corrupt image in the database file.
61970        */
61971        walUnlockShared(pWal, WAL_READ_LOCK(0));
61972        return WAL_RETRY;
61973      }
61974      pWal->readLock = 0;
61975      return SQLITE_OK;
61976    }else if( rc!=SQLITE_BUSY ){
61977      return rc;
61978    }
61979  }
61980
61981  /* If we get this far, it means that the reader will want to use
61982  ** the WAL to get at content from recent commits.  The job now is
61983  ** to select one of the aReadMark[] entries that is closest to
61984  ** but not exceeding pWal->hdr.mxFrame and lock that entry.
61985  */
61986  mxReadMark = 0;
61987  mxI = 0;
61988  mxFrame = pWal->hdr.mxFrame;
61989#ifdef SQLITE_ENABLE_SNAPSHOT
61990  if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
61991    mxFrame = pWal->pSnapshot->mxFrame;
61992  }
61993#endif
61994  for(i=1; i<WAL_NREADER; i++){
61995    u32 thisMark = AtomicLoad(pInfo->aReadMark+i);
61996    if( mxReadMark<=thisMark && thisMark<=mxFrame ){
61997      assert( thisMark!=READMARK_NOT_USED );
61998      mxReadMark = thisMark;
61999      mxI = i;
62000    }
62001  }
62002  if( (pWal->readOnly & WAL_SHM_RDONLY)==0
62003   && (mxReadMark<mxFrame || mxI==0)
62004  ){
62005    for(i=1; i<WAL_NREADER; i++){
62006      rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
62007      if( rc==SQLITE_OK ){
62008        AtomicStore(pInfo->aReadMark+i,mxFrame);
62009        mxReadMark = mxFrame;
62010        mxI = i;
62011        walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
62012        break;
62013      }else if( rc!=SQLITE_BUSY ){
62014        return rc;
62015      }
62016    }
62017  }
62018  if( mxI==0 ){
62019    assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
62020    return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT;
62021  }
62022
62023  rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
62024  if( rc ){
62025    return rc==SQLITE_BUSY ? WAL_RETRY : rc;
62026  }
62027  /* Now that the read-lock has been obtained, check that neither the
62028  ** value in the aReadMark[] array or the contents of the wal-index
62029  ** header have changed.
62030  **
62031  ** It is necessary to check that the wal-index header did not change
62032  ** between the time it was read and when the shared-lock was obtained
62033  ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
62034  ** that the log file may have been wrapped by a writer, or that frames
62035  ** that occur later in the log than pWal->hdr.mxFrame may have been
62036  ** copied into the database by a checkpointer. If either of these things
62037  ** happened, then reading the database with the current value of
62038  ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
62039  ** instead.
62040  **
62041  ** Before checking that the live wal-index header has not changed
62042  ** since it was read, set Wal.minFrame to the first frame in the wal
62043  ** file that has not yet been checkpointed. This client will not need
62044  ** to read any frames earlier than minFrame from the wal file - they
62045  ** can be safely read directly from the database file.
62046  **
62047  ** Because a ShmBarrier() call is made between taking the copy of
62048  ** nBackfill and checking that the wal-header in shared-memory still
62049  ** matches the one cached in pWal->hdr, it is guaranteed that the
62050  ** checkpointer that set nBackfill was not working with a wal-index
62051  ** header newer than that cached in pWal->hdr. If it were, that could
62052  ** cause a problem. The checkpointer could omit to checkpoint
62053  ** a version of page X that lies before pWal->minFrame (call that version
62054  ** A) on the basis that there is a newer version (version B) of the same
62055  ** page later in the wal file. But if version B happens to like past
62056  ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
62057  ** that it can read version A from the database file. However, since
62058  ** we can guarantee that the checkpointer that set nBackfill could not
62059  ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
62060  */
62061  pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1;
62062  walShmBarrier(pWal);
62063  if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
62064   || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
62065  ){
62066    walUnlockShared(pWal, WAL_READ_LOCK(mxI));
62067    return WAL_RETRY;
62068  }else{
62069    assert( mxReadMark<=pWal->hdr.mxFrame );
62070    pWal->readLock = (i16)mxI;
62071  }
62072  return rc;
62073}
62074
62075#ifdef SQLITE_ENABLE_SNAPSHOT
62076/*
62077** Attempt to reduce the value of the WalCkptInfo.nBackfillAttempted
62078** variable so that older snapshots can be accessed. To do this, loop
62079** through all wal frames from nBackfillAttempted to (nBackfill+1),
62080** comparing their content to the corresponding page with the database
62081** file, if any. Set nBackfillAttempted to the frame number of the
62082** first frame for which the wal file content matches the db file.
62083**
62084** This is only really safe if the file-system is such that any page
62085** writes made by earlier checkpointers were atomic operations, which
62086** is not always true. It is also possible that nBackfillAttempted
62087** may be left set to a value larger than expected, if a wal frame
62088** contains content that duplicate of an earlier version of the same
62089** page.
62090**
62091** SQLITE_OK is returned if successful, or an SQLite error code if an
62092** error occurs. It is not an error if nBackfillAttempted cannot be
62093** decreased at all.
62094*/
62095SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal){
62096  int rc;
62097
62098  assert( pWal->readLock>=0 );
62099  rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
62100  if( rc==SQLITE_OK ){
62101    volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
62102    int szPage = (int)pWal->szPage;
62103    i64 szDb;                   /* Size of db file in bytes */
62104
62105    rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
62106    if( rc==SQLITE_OK ){
62107      void *pBuf1 = sqlite3_malloc(szPage);
62108      void *pBuf2 = sqlite3_malloc(szPage);
62109      if( pBuf1==0 || pBuf2==0 ){
62110        rc = SQLITE_NOMEM;
62111      }else{
62112        u32 i = pInfo->nBackfillAttempted;
62113        for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){
62114          WalHashLoc sLoc;          /* Hash table location */
62115          u32 pgno;                 /* Page number in db file */
62116          i64 iDbOff;               /* Offset of db file entry */
62117          i64 iWalOff;              /* Offset of wal file entry */
62118
62119          rc = walHashGet(pWal, walFramePage(i), &sLoc);
62120          if( rc!=SQLITE_OK ) break;
62121          pgno = sLoc.aPgno[i-sLoc.iZero];
62122          iDbOff = (i64)(pgno-1) * szPage;
62123
62124          if( iDbOff+szPage<=szDb ){
62125            iWalOff = walFrameOffset(i, szPage) + WAL_FRAME_HDRSIZE;
62126            rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
62127
62128            if( rc==SQLITE_OK ){
62129              rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
62130            }
62131
62132            if( rc!=SQLITE_OK || 0==memcmp(pBuf1, pBuf2, szPage) ){
62133              break;
62134            }
62135          }
62136
62137          pInfo->nBackfillAttempted = i-1;
62138        }
62139      }
62140
62141      sqlite3_free(pBuf1);
62142      sqlite3_free(pBuf2);
62143    }
62144    walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
62145  }
62146
62147  return rc;
62148}
62149#endif /* SQLITE_ENABLE_SNAPSHOT */
62150
62151/*
62152** Begin a read transaction on the database.
62153**
62154** This routine used to be called sqlite3OpenSnapshot() and with good reason:
62155** it takes a snapshot of the state of the WAL and wal-index for the current
62156** instant in time.  The current thread will continue to use this snapshot.
62157** Other threads might append new content to the WAL and wal-index but
62158** that extra content is ignored by the current thread.
62159**
62160** If the database contents have changes since the previous read
62161** transaction, then *pChanged is set to 1 before returning.  The
62162** Pager layer will use this to know that its cache is stale and
62163** needs to be flushed.
62164*/
62165SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
62166  int rc;                         /* Return code */
62167  int cnt = 0;                    /* Number of TryBeginRead attempts */
62168#ifdef SQLITE_ENABLE_SNAPSHOT
62169  int bChanged = 0;
62170  WalIndexHdr *pSnapshot = pWal->pSnapshot;
62171#endif
62172
62173  assert( pWal->ckptLock==0 );
62174
62175#ifdef SQLITE_ENABLE_SNAPSHOT
62176  if( pSnapshot ){
62177    if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
62178      bChanged = 1;
62179    }
62180
62181    /* It is possible that there is a checkpointer thread running
62182    ** concurrent with this code. If this is the case, it may be that the
62183    ** checkpointer has already determined that it will checkpoint
62184    ** snapshot X, where X is later in the wal file than pSnapshot, but
62185    ** has not yet set the pInfo->nBackfillAttempted variable to indicate
62186    ** its intent. To avoid the race condition this leads to, ensure that
62187    ** there is no checkpointer process by taking a shared CKPT lock
62188    ** before checking pInfo->nBackfillAttempted.  */
62189    (void)walEnableBlocking(pWal);
62190    rc = walLockShared(pWal, WAL_CKPT_LOCK);
62191    walDisableBlocking(pWal);
62192
62193    if( rc!=SQLITE_OK ){
62194      return rc;
62195    }
62196    pWal->ckptLock = 1;
62197  }
62198#endif
62199
62200  do{
62201    rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
62202  }while( rc==WAL_RETRY );
62203  testcase( (rc&0xff)==SQLITE_BUSY );
62204  testcase( (rc&0xff)==SQLITE_IOERR );
62205  testcase( rc==SQLITE_PROTOCOL );
62206  testcase( rc==SQLITE_OK );
62207
62208#ifdef SQLITE_ENABLE_SNAPSHOT
62209  if( rc==SQLITE_OK ){
62210    if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
62211      /* At this point the client has a lock on an aReadMark[] slot holding
62212      ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
62213      ** is populated with the wal-index header corresponding to the head
62214      ** of the wal file. Verify that pSnapshot is still valid before
62215      ** continuing.  Reasons why pSnapshot might no longer be valid:
62216      **
62217      **    (1)  The WAL file has been reset since the snapshot was taken.
62218      **         In this case, the salt will have changed.
62219      **
62220      **    (2)  A checkpoint as been attempted that wrote frames past
62221      **         pSnapshot->mxFrame into the database file.  Note that the
62222      **         checkpoint need not have completed for this to cause problems.
62223      */
62224      volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
62225
62226      assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
62227      assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
62228
62229      /* Check that the wal file has not been wrapped. Assuming that it has
62230      ** not, also check that no checkpointer has attempted to checkpoint any
62231      ** frames beyond pSnapshot->mxFrame. If either of these conditions are
62232      ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
62233      ** with *pSnapshot and set *pChanged as appropriate for opening the
62234      ** snapshot.  */
62235      if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
62236       && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
62237      ){
62238        assert( pWal->readLock>0 );
62239        memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
62240        *pChanged = bChanged;
62241      }else{
62242        rc = SQLITE_ERROR_SNAPSHOT;
62243      }
62244
62245      /* A client using a non-current snapshot may not ignore any frames
62246      ** from the start of the wal file. This is because, for a system
62247      ** where (minFrame < iSnapshot < maxFrame), a checkpointer may
62248      ** have omitted to checkpoint a frame earlier than minFrame in
62249      ** the file because there exists a frame after iSnapshot that
62250      ** is the same database page.  */
62251      pWal->minFrame = 1;
62252
62253      if( rc!=SQLITE_OK ){
62254        sqlite3WalEndReadTransaction(pWal);
62255      }
62256    }
62257  }
62258
62259  /* Release the shared CKPT lock obtained above. */
62260  if( pWal->ckptLock ){
62261    assert( pSnapshot );
62262    walUnlockShared(pWal, WAL_CKPT_LOCK);
62263    pWal->ckptLock = 0;
62264  }
62265#endif
62266  return rc;
62267}
62268
62269/*
62270** Finish with a read transaction.  All this does is release the
62271** read-lock.
62272*/
62273SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
62274  sqlite3WalEndWriteTransaction(pWal);
62275  if( pWal->readLock>=0 ){
62276    walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
62277    pWal->readLock = -1;
62278  }
62279}
62280
62281/*
62282** Search the wal file for page pgno. If found, set *piRead to the frame that
62283** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
62284** to zero.
62285**
62286** Return SQLITE_OK if successful, or an error code if an error occurs. If an
62287** error does occur, the final value of *piRead is undefined.
62288*/
62289SQLITE_PRIVATE int sqlite3WalFindFrame(
62290  Wal *pWal,                      /* WAL handle */
62291  Pgno pgno,                      /* Database page number to read data for */
62292  u32 *piRead                     /* OUT: Frame number (or zero) */
62293){
62294  u32 iRead = 0;                  /* If !=0, WAL frame to return data from */
62295  u32 iLast = pWal->hdr.mxFrame;  /* Last page in WAL for this reader */
62296  int iHash;                      /* Used to loop through N hash tables */
62297  int iMinHash;
62298
62299  /* This routine is only be called from within a read transaction. */
62300  assert( pWal->readLock>=0 || pWal->lockError );
62301
62302  /* If the "last page" field of the wal-index header snapshot is 0, then
62303  ** no data will be read from the wal under any circumstances. Return early
62304  ** in this case as an optimization.  Likewise, if pWal->readLock==0,
62305  ** then the WAL is ignored by the reader so return early, as if the
62306  ** WAL were empty.
62307  */
62308  if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
62309    *piRead = 0;
62310    return SQLITE_OK;
62311  }
62312
62313  /* Search the hash table or tables for an entry matching page number
62314  ** pgno. Each iteration of the following for() loop searches one
62315  ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
62316  **
62317  ** This code might run concurrently to the code in walIndexAppend()
62318  ** that adds entries to the wal-index (and possibly to this hash
62319  ** table). This means the value just read from the hash
62320  ** slot (aHash[iKey]) may have been added before or after the
62321  ** current read transaction was opened. Values added after the
62322  ** read transaction was opened may have been written incorrectly -
62323  ** i.e. these slots may contain garbage data. However, we assume
62324  ** that any slots written before the current read transaction was
62325  ** opened remain unmodified.
62326  **
62327  ** For the reasons above, the if(...) condition featured in the inner
62328  ** loop of the following block is more stringent that would be required
62329  ** if we had exclusive access to the hash-table:
62330  **
62331  **   (aPgno[iFrame]==pgno):
62332  **     This condition filters out normal hash-table collisions.
62333  **
62334  **   (iFrame<=iLast):
62335  **     This condition filters out entries that were added to the hash
62336  **     table after the current read-transaction had started.
62337  */
62338  iMinHash = walFramePage(pWal->minFrame);
62339  for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
62340    WalHashLoc sLoc;              /* Hash table location */
62341    int iKey;                     /* Hash slot index */
62342    int nCollide;                 /* Number of hash collisions remaining */
62343    int rc;                       /* Error code */
62344    u32 iH;
62345
62346    rc = walHashGet(pWal, iHash, &sLoc);
62347    if( rc!=SQLITE_OK ){
62348      return rc;
62349    }
62350    nCollide = HASHTABLE_NSLOT;
62351    iKey = walHash(pgno);
62352    while( (iH = AtomicLoad(&sLoc.aHash[iKey]))!=0 ){
62353      u32 iFrame = iH + sLoc.iZero;
62354      if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH]==pgno ){
62355        assert( iFrame>iRead || CORRUPT_DB );
62356        iRead = iFrame;
62357      }
62358      if( (nCollide--)==0 ){
62359        return SQLITE_CORRUPT_BKPT;
62360      }
62361      iKey = walNextHash(iKey);
62362    }
62363    if( iRead ) break;
62364  }
62365
62366#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
62367  /* If expensive assert() statements are available, do a linear search
62368  ** of the wal-index file content. Make sure the results agree with the
62369  ** result obtained using the hash indexes above.  */
62370  {
62371    u32 iRead2 = 0;
62372    u32 iTest;
62373    assert( pWal->bShmUnreliable || pWal->minFrame>0 );
62374    for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
62375      if( walFramePgno(pWal, iTest)==pgno ){
62376        iRead2 = iTest;
62377        break;
62378      }
62379    }
62380    assert( iRead==iRead2 );
62381  }
62382#endif
62383
62384  *piRead = iRead;
62385  return SQLITE_OK;
62386}
62387
62388/*
62389** Read the contents of frame iRead from the wal file into buffer pOut
62390** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
62391** error code otherwise.
62392*/
62393SQLITE_PRIVATE int sqlite3WalReadFrame(
62394  Wal *pWal,                      /* WAL handle */
62395  u32 iRead,                      /* Frame to read */
62396  int nOut,                       /* Size of buffer pOut in bytes */
62397  u8 *pOut                        /* Buffer to write page data to */
62398){
62399  int sz;
62400  i64 iOffset;
62401  sz = pWal->hdr.szPage;
62402  sz = (sz&0xfe00) + ((sz&0x0001)<<16);
62403  testcase( sz<=32768 );
62404  testcase( sz>=65536 );
62405  iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
62406  /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
62407  return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
62408}
62409
62410/*
62411** Return the size of the database in pages (or zero, if unknown).
62412*/
62413SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
62414  if( pWal && ALWAYS(pWal->readLock>=0) ){
62415    return pWal->hdr.nPage;
62416  }
62417  return 0;
62418}
62419
62420
62421/*
62422** This function starts a write transaction on the WAL.
62423**
62424** A read transaction must have already been started by a prior call
62425** to sqlite3WalBeginReadTransaction().
62426**
62427** If another thread or process has written into the database since
62428** the read transaction was started, then it is not possible for this
62429** thread to write as doing so would cause a fork.  So this routine
62430** returns SQLITE_BUSY in that case and no write transaction is started.
62431**
62432** There can only be a single writer active at a time.
62433*/
62434SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
62435  int rc;
62436
62437#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
62438  /* If the write-lock is already held, then it was obtained before the
62439  ** read-transaction was even opened, making this call a no-op.
62440  ** Return early. */
62441  if( pWal->writeLock ){
62442    assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
62443    return SQLITE_OK;
62444  }
62445#endif
62446
62447  /* Cannot start a write transaction without first holding a read
62448  ** transaction. */
62449  assert( pWal->readLock>=0 );
62450  assert( pWal->writeLock==0 && pWal->iReCksum==0 );
62451
62452  if( pWal->readOnly ){
62453    return SQLITE_READONLY;
62454  }
62455
62456  /* Only one writer allowed at a time.  Get the write lock.  Return
62457  ** SQLITE_BUSY if unable.
62458  */
62459  rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
62460  if( rc ){
62461    return rc;
62462  }
62463  pWal->writeLock = 1;
62464
62465  /* If another connection has written to the database file since the
62466  ** time the read transaction on this connection was started, then
62467  ** the write is disallowed.
62468  */
62469  if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
62470    walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
62471    pWal->writeLock = 0;
62472    rc = SQLITE_BUSY_SNAPSHOT;
62473  }
62474
62475  return rc;
62476}
62477
62478/*
62479** End a write transaction.  The commit has already been done.  This
62480** routine merely releases the lock.
62481*/
62482SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
62483  if( pWal->writeLock ){
62484    walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
62485    pWal->writeLock = 0;
62486    pWal->iReCksum = 0;
62487    pWal->truncateOnCommit = 0;
62488  }
62489  return SQLITE_OK;
62490}
62491
62492/*
62493** If any data has been written (but not committed) to the log file, this
62494** function moves the write-pointer back to the start of the transaction.
62495**
62496** Additionally, the callback function is invoked for each frame written
62497** to the WAL since the start of the transaction. If the callback returns
62498** other than SQLITE_OK, it is not invoked again and the error code is
62499** returned to the caller.
62500**
62501** Otherwise, if the callback function does not return an error, this
62502** function returns SQLITE_OK.
62503*/
62504SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
62505  int rc = SQLITE_OK;
62506  if( ALWAYS(pWal->writeLock) ){
62507    Pgno iMax = pWal->hdr.mxFrame;
62508    Pgno iFrame;
62509
62510    /* Restore the clients cache of the wal-index header to the state it
62511    ** was in before the client began writing to the database.
62512    */
62513    memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
62514
62515    for(iFrame=pWal->hdr.mxFrame+1;
62516        ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
62517        iFrame++
62518    ){
62519      /* This call cannot fail. Unless the page for which the page number
62520      ** is passed as the second argument is (a) in the cache and
62521      ** (b) has an outstanding reference, then xUndo is either a no-op
62522      ** (if (a) is false) or simply expels the page from the cache (if (b)
62523      ** is false).
62524      **
62525      ** If the upper layer is doing a rollback, it is guaranteed that there
62526      ** are no outstanding references to any page other than page 1. And
62527      ** page 1 is never written to the log until the transaction is
62528      ** committed. As a result, the call to xUndo may not fail.
62529      */
62530      assert( walFramePgno(pWal, iFrame)!=1 );
62531      rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
62532    }
62533    if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
62534  }
62535  return rc;
62536}
62537
62538/*
62539** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
62540** values. This function populates the array with values required to
62541** "rollback" the write position of the WAL handle back to the current
62542** point in the event of a savepoint rollback (via WalSavepointUndo()).
62543*/
62544SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
62545  assert( pWal->writeLock );
62546  aWalData[0] = pWal->hdr.mxFrame;
62547  aWalData[1] = pWal->hdr.aFrameCksum[0];
62548  aWalData[2] = pWal->hdr.aFrameCksum[1];
62549  aWalData[3] = pWal->nCkpt;
62550}
62551
62552/*
62553** Move the write position of the WAL back to the point identified by
62554** the values in the aWalData[] array. aWalData must point to an array
62555** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
62556** by a call to WalSavepoint().
62557*/
62558SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
62559  int rc = SQLITE_OK;
62560
62561  assert( pWal->writeLock );
62562  assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
62563
62564  if( aWalData[3]!=pWal->nCkpt ){
62565    /* This savepoint was opened immediately after the write-transaction
62566    ** was started. Right after that, the writer decided to wrap around
62567    ** to the start of the log. Update the savepoint values to match.
62568    */
62569    aWalData[0] = 0;
62570    aWalData[3] = pWal->nCkpt;
62571  }
62572
62573  if( aWalData[0]<pWal->hdr.mxFrame ){
62574    pWal->hdr.mxFrame = aWalData[0];
62575    pWal->hdr.aFrameCksum[0] = aWalData[1];
62576    pWal->hdr.aFrameCksum[1] = aWalData[2];
62577    walCleanupHash(pWal);
62578  }
62579
62580  return rc;
62581}
62582
62583/*
62584** This function is called just before writing a set of frames to the log
62585** file (see sqlite3WalFrames()). It checks to see if, instead of appending
62586** to the current log file, it is possible to overwrite the start of the
62587** existing log file with the new frames (i.e. "reset" the log). If so,
62588** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
62589** unchanged.
62590**
62591** SQLITE_OK is returned if no error is encountered (regardless of whether
62592** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
62593** if an error occurs.
62594*/
62595static int walRestartLog(Wal *pWal){
62596  int rc = SQLITE_OK;
62597  int cnt;
62598
62599  if( pWal->readLock==0 ){
62600    volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
62601    assert( pInfo->nBackfill==pWal->hdr.mxFrame );
62602    if( pInfo->nBackfill>0 ){
62603      u32 salt1;
62604      sqlite3_randomness(4, &salt1);
62605      rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
62606      if( rc==SQLITE_OK ){
62607        /* If all readers are using WAL_READ_LOCK(0) (in other words if no
62608        ** readers are currently using the WAL), then the transactions
62609        ** frames will overwrite the start of the existing log. Update the
62610        ** wal-index header to reflect this.
62611        **
62612        ** In theory it would be Ok to update the cache of the header only
62613        ** at this point. But updating the actual wal-index header is also
62614        ** safe and means there is no special case for sqlite3WalUndo()
62615        ** to handle if this transaction is rolled back.  */
62616        walRestartHdr(pWal, salt1);
62617        walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
62618      }else if( rc!=SQLITE_BUSY ){
62619        return rc;
62620      }
62621    }
62622    walUnlockShared(pWal, WAL_READ_LOCK(0));
62623    pWal->readLock = -1;
62624    cnt = 0;
62625    do{
62626      int notUsed;
62627      rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
62628    }while( rc==WAL_RETRY );
62629    assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
62630    testcase( (rc&0xff)==SQLITE_IOERR );
62631    testcase( rc==SQLITE_PROTOCOL );
62632    testcase( rc==SQLITE_OK );
62633  }
62634  return rc;
62635}
62636
62637/*
62638** Information about the current state of the WAL file and where
62639** the next fsync should occur - passed from sqlite3WalFrames() into
62640** walWriteToLog().
62641*/
62642typedef struct WalWriter {
62643  Wal *pWal;                   /* The complete WAL information */
62644  sqlite3_file *pFd;           /* The WAL file to which we write */
62645  sqlite3_int64 iSyncPoint;    /* Fsync at this offset */
62646  int syncFlags;               /* Flags for the fsync */
62647  int szPage;                  /* Size of one page */
62648} WalWriter;
62649
62650/*
62651** Write iAmt bytes of content into the WAL file beginning at iOffset.
62652** Do a sync when crossing the p->iSyncPoint boundary.
62653**
62654** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
62655** first write the part before iSyncPoint, then sync, then write the
62656** rest.
62657*/
62658static int walWriteToLog(
62659  WalWriter *p,              /* WAL to write to */
62660  void *pContent,            /* Content to be written */
62661  int iAmt,                  /* Number of bytes to write */
62662  sqlite3_int64 iOffset      /* Start writing at this offset */
62663){
62664  int rc;
62665  if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
62666    int iFirstAmt = (int)(p->iSyncPoint - iOffset);
62667    rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
62668    if( rc ) return rc;
62669    iOffset += iFirstAmt;
62670    iAmt -= iFirstAmt;
62671    pContent = (void*)(iFirstAmt + (char*)pContent);
62672    assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
62673    rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
62674    if( iAmt==0 || rc ) return rc;
62675  }
62676  rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
62677  return rc;
62678}
62679
62680/*
62681** Write out a single frame of the WAL
62682*/
62683static int walWriteOneFrame(
62684  WalWriter *p,               /* Where to write the frame */
62685  PgHdr *pPage,               /* The page of the frame to be written */
62686  int nTruncate,              /* The commit flag.  Usually 0.  >0 for commit */
62687  sqlite3_int64 iOffset       /* Byte offset at which to write */
62688){
62689  int rc;                         /* Result code from subfunctions */
62690  void *pData;                    /* Data actually written */
62691  u8 aFrame[WAL_FRAME_HDRSIZE];   /* Buffer to assemble frame-header in */
62692  pData = pPage->pData;
62693  walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
62694  rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
62695  if( rc ) return rc;
62696  /* Write the page data */
62697  rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
62698  return rc;
62699}
62700
62701/*
62702** This function is called as part of committing a transaction within which
62703** one or more frames have been overwritten. It updates the checksums for
62704** all frames written to the wal file by the current transaction starting
62705** with the earliest to have been overwritten.
62706**
62707** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
62708*/
62709static int walRewriteChecksums(Wal *pWal, u32 iLast){
62710  const int szPage = pWal->szPage;/* Database page size */
62711  int rc = SQLITE_OK;             /* Return code */
62712  u8 *aBuf;                       /* Buffer to load data from wal file into */
62713  u8 aFrame[WAL_FRAME_HDRSIZE];   /* Buffer to assemble frame-headers in */
62714  u32 iRead;                      /* Next frame to read from wal file */
62715  i64 iCksumOff;
62716
62717  aBuf = sqlite3_malloc(szPage + WAL_FRAME_HDRSIZE);
62718  if( aBuf==0 ) return SQLITE_NOMEM_BKPT;
62719
62720  /* Find the checksum values to use as input for the recalculating the
62721  ** first checksum. If the first frame is frame 1 (implying that the current
62722  ** transaction restarted the wal file), these values must be read from the
62723  ** wal-file header. Otherwise, read them from the frame header of the
62724  ** previous frame.  */
62725  assert( pWal->iReCksum>0 );
62726  if( pWal->iReCksum==1 ){
62727    iCksumOff = 24;
62728  }else{
62729    iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
62730  }
62731  rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
62732  pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
62733  pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
62734
62735  iRead = pWal->iReCksum;
62736  pWal->iReCksum = 0;
62737  for(; rc==SQLITE_OK && iRead<=iLast; iRead++){
62738    i64 iOff = walFrameOffset(iRead, szPage);
62739    rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
62740    if( rc==SQLITE_OK ){
62741      u32 iPgno, nDbSize;
62742      iPgno = sqlite3Get4byte(aBuf);
62743      nDbSize = sqlite3Get4byte(&aBuf[4]);
62744
62745      walEncodeFrame(pWal, iPgno, nDbSize, &aBuf[WAL_FRAME_HDRSIZE], aFrame);
62746      rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
62747    }
62748  }
62749
62750  sqlite3_free(aBuf);
62751  return rc;
62752}
62753
62754/*
62755** Write a set of frames to the log. The caller must hold the write-lock
62756** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
62757*/
62758SQLITE_PRIVATE int sqlite3WalFrames(
62759  Wal *pWal,                      /* Wal handle to write to */
62760  int szPage,                     /* Database page-size in bytes */
62761  PgHdr *pList,                   /* List of dirty pages to write */
62762  Pgno nTruncate,                 /* Database size after this commit */
62763  int isCommit,                   /* True if this is a commit */
62764  int sync_flags                  /* Flags to pass to OsSync() (or 0) */
62765){
62766  int rc;                         /* Used to catch return codes */
62767  u32 iFrame;                     /* Next frame address */
62768  PgHdr *p;                       /* Iterator to run through pList with. */
62769  PgHdr *pLast = 0;               /* Last frame in list */
62770  int nExtra = 0;                 /* Number of extra copies of last page */
62771  int szFrame;                    /* The size of a single frame */
62772  i64 iOffset;                    /* Next byte to write in WAL file */
62773  WalWriter w;                    /* The writer */
62774  u32 iFirst = 0;                 /* First frame that may be overwritten */
62775  WalIndexHdr *pLive;             /* Pointer to shared header */
62776
62777  assert( pList );
62778  assert( pWal->writeLock );
62779
62780  /* If this frame set completes a transaction, then nTruncate>0.  If
62781  ** nTruncate==0 then this frame set does not complete the transaction. */
62782  assert( (isCommit!=0)==(nTruncate!=0) );
62783
62784#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
62785  { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
62786    WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
62787              pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
62788  }
62789#endif
62790
62791  pLive = (WalIndexHdr*)walIndexHdr(pWal);
62792  if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
62793    iFirst = pLive->mxFrame+1;
62794  }
62795
62796  /* See if it is possible to write these frames into the start of the
62797  ** log file, instead of appending to it at pWal->hdr.mxFrame.
62798  */
62799  if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
62800    return rc;
62801  }
62802
62803  /* If this is the first frame written into the log, write the WAL
62804  ** header to the start of the WAL file. See comments at the top of
62805  ** this source file for a description of the WAL header format.
62806  */
62807  iFrame = pWal->hdr.mxFrame;
62808  if( iFrame==0 ){
62809    u8 aWalHdr[WAL_HDRSIZE];      /* Buffer to assemble wal-header in */
62810    u32 aCksum[2];                /* Checksum for wal-header */
62811
62812    sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
62813    sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
62814    sqlite3Put4byte(&aWalHdr[8], szPage);
62815    sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
62816    if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
62817    memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
62818    walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
62819    sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
62820    sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
62821
62822    pWal->szPage = szPage;
62823    pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
62824    pWal->hdr.aFrameCksum[0] = aCksum[0];
62825    pWal->hdr.aFrameCksum[1] = aCksum[1];
62826    pWal->truncateOnCommit = 1;
62827
62828    rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
62829    WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
62830    if( rc!=SQLITE_OK ){
62831      return rc;
62832    }
62833
62834    /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
62835    ** all syncing is turned off by PRAGMA synchronous=OFF).  Otherwise
62836    ** an out-of-order write following a WAL restart could result in
62837    ** database corruption.  See the ticket:
62838    **
62839    **     https://sqlite.org/src/info/ff5be73dee
62840    */
62841    if( pWal->syncHeader ){
62842      rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
62843      if( rc ) return rc;
62844    }
62845  }
62846  assert( (int)pWal->szPage==szPage );
62847
62848  /* Setup information needed to write frames into the WAL */
62849  w.pWal = pWal;
62850  w.pFd = pWal->pWalFd;
62851  w.iSyncPoint = 0;
62852  w.syncFlags = sync_flags;
62853  w.szPage = szPage;
62854  iOffset = walFrameOffset(iFrame+1, szPage);
62855  szFrame = szPage + WAL_FRAME_HDRSIZE;
62856
62857  /* Write all frames into the log file exactly once */
62858  for(p=pList; p; p=p->pDirty){
62859    int nDbSize;   /* 0 normally.  Positive == commit flag */
62860
62861    /* Check if this page has already been written into the wal file by
62862    ** the current transaction. If so, overwrite the existing frame and
62863    ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
62864    ** checksums must be recomputed when the transaction is committed.  */
62865    if( iFirst && (p->pDirty || isCommit==0) ){
62866      u32 iWrite = 0;
62867      VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
62868      assert( rc==SQLITE_OK || iWrite==0 );
62869      if( iWrite>=iFirst ){
62870        i64 iOff = walFrameOffset(iWrite, szPage) + WAL_FRAME_HDRSIZE;
62871        void *pData;
62872        if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
62873          pWal->iReCksum = iWrite;
62874        }
62875        pData = p->pData;
62876        rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
62877        if( rc ) return rc;
62878        p->flags &= ~PGHDR_WAL_APPEND;
62879        continue;
62880      }
62881    }
62882
62883    iFrame++;
62884    assert( iOffset==walFrameOffset(iFrame, szPage) );
62885    nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
62886    rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
62887    if( rc ) return rc;
62888    pLast = p;
62889    iOffset += szFrame;
62890    p->flags |= PGHDR_WAL_APPEND;
62891  }
62892
62893  /* Recalculate checksums within the wal file if required. */
62894  if( isCommit && pWal->iReCksum ){
62895    rc = walRewriteChecksums(pWal, iFrame);
62896    if( rc ) return rc;
62897  }
62898
62899  /* If this is the end of a transaction, then we might need to pad
62900  ** the transaction and/or sync the WAL file.
62901  **
62902  ** Padding and syncing only occur if this set of frames complete a
62903  ** transaction and if PRAGMA synchronous=FULL.  If synchronous==NORMAL
62904  ** or synchronous==OFF, then no padding or syncing are needed.
62905  **
62906  ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
62907  ** needed and only the sync is done.  If padding is needed, then the
62908  ** final frame is repeated (with its commit mark) until the next sector
62909  ** boundary is crossed.  Only the part of the WAL prior to the last
62910  ** sector boundary is synced; the part of the last frame that extends
62911  ** past the sector boundary is written after the sync.
62912  */
62913  if( isCommit && WAL_SYNC_FLAGS(sync_flags)!=0 ){
62914    int bSync = 1;
62915    if( pWal->padToSectorBoundary ){
62916      int sectorSize = sqlite3SectorSize(pWal->pWalFd);
62917      w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
62918      bSync = (w.iSyncPoint==iOffset);
62919      testcase( bSync );
62920      while( iOffset<w.iSyncPoint ){
62921        rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
62922        if( rc ) return rc;
62923        iOffset += szFrame;
62924        nExtra++;
62925        assert( pLast!=0 );
62926      }
62927    }
62928    if( bSync ){
62929      assert( rc==SQLITE_OK );
62930      rc = sqlite3OsSync(w.pFd, WAL_SYNC_FLAGS(sync_flags));
62931    }
62932  }
62933
62934  /* If this frame set completes the first transaction in the WAL and
62935  ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
62936  ** journal size limit, if possible.
62937  */
62938  if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
62939    i64 sz = pWal->mxWalSize;
62940    if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
62941      sz = walFrameOffset(iFrame+nExtra+1, szPage);
62942    }
62943    walLimitSize(pWal, sz);
62944    pWal->truncateOnCommit = 0;
62945  }
62946
62947  /* Append data to the wal-index. It is not necessary to lock the
62948  ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
62949  ** guarantees that there are no other writers, and no data that may
62950  ** be in use by existing readers is being overwritten.
62951  */
62952  iFrame = pWal->hdr.mxFrame;
62953  for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
62954    if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
62955    iFrame++;
62956    rc = walIndexAppend(pWal, iFrame, p->pgno);
62957  }
62958  assert( pLast!=0 || nExtra==0 );
62959  while( rc==SQLITE_OK && nExtra>0 ){
62960    iFrame++;
62961    nExtra--;
62962    rc = walIndexAppend(pWal, iFrame, pLast->pgno);
62963  }
62964
62965  if( rc==SQLITE_OK ){
62966    /* Update the private copy of the header. */
62967    pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
62968    testcase( szPage<=32768 );
62969    testcase( szPage>=65536 );
62970    pWal->hdr.mxFrame = iFrame;
62971    if( isCommit ){
62972      pWal->hdr.iChange++;
62973      pWal->hdr.nPage = nTruncate;
62974    }
62975    /* If this is a commit, update the wal-index header too. */
62976    if( isCommit ){
62977      walIndexWriteHdr(pWal);
62978      pWal->iCallback = iFrame;
62979    }
62980  }
62981
62982  WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
62983  return rc;
62984}
62985
62986/*
62987** This routine is called to implement sqlite3_wal_checkpoint() and
62988** related interfaces.
62989**
62990** Obtain a CHECKPOINT lock and then backfill as much information as
62991** we can from WAL into the database.
62992**
62993** If parameter xBusy is not NULL, it is a pointer to a busy-handler
62994** callback. In this case this function runs a blocking checkpoint.
62995*/
62996SQLITE_PRIVATE int sqlite3WalCheckpoint(
62997  Wal *pWal,                      /* Wal connection */
62998  sqlite3 *db,                    /* Check this handle's interrupt flag */
62999  int eMode,                      /* PASSIVE, FULL, RESTART, or TRUNCATE */
63000  int (*xBusy)(void*),            /* Function to call when busy */
63001  void *pBusyArg,                 /* Context argument for xBusyHandler */
63002  int sync_flags,                 /* Flags to sync db file with (or 0) */
63003  int nBuf,                       /* Size of temporary buffer */
63004  u8 *zBuf,                       /* Temporary buffer to use */
63005  int *pnLog,                     /* OUT: Number of frames in WAL */
63006  int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
63007){
63008  int rc;                         /* Return code */
63009  int isChanged = 0;              /* True if a new wal-index header is loaded */
63010  int eMode2 = eMode;             /* Mode to pass to walCheckpoint() */
63011  int (*xBusy2)(void*) = xBusy;   /* Busy handler for eMode2 */
63012
63013  assert( pWal->ckptLock==0 );
63014  assert( pWal->writeLock==0 );
63015
63016  /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
63017  ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
63018  assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
63019
63020  if( pWal->readOnly ) return SQLITE_READONLY;
63021  WALTRACE(("WAL%p: checkpoint begins\n", pWal));
63022
63023  /* Enable blocking locks, if possible. If blocking locks are successfully
63024  ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */
63025  sqlite3WalDb(pWal, db);
63026  (void)walEnableBlocking(pWal);
63027
63028  /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
63029  ** "checkpoint" lock on the database file.
63030  ** EVIDENCE-OF: R-10421-19736 If any other process is running a
63031  ** checkpoint operation at the same time, the lock cannot be obtained and
63032  ** SQLITE_BUSY is returned.
63033  ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
63034  ** it will not be invoked in this case.
63035  */
63036  rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
63037  testcase( rc==SQLITE_BUSY );
63038  testcase( rc!=SQLITE_OK && xBusy2!=0 );
63039  if( rc==SQLITE_OK ){
63040    pWal->ckptLock = 1;
63041
63042    /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
63043    ** TRUNCATE modes also obtain the exclusive "writer" lock on the database
63044    ** file.
63045    **
63046    ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
63047    ** immediately, and a busy-handler is configured, it is invoked and the
63048    ** writer lock retried until either the busy-handler returns 0 or the
63049    ** lock is successfully obtained.
63050    */
63051    if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
63052      rc = walBusyLock(pWal, xBusy2, pBusyArg, WAL_WRITE_LOCK, 1);
63053      if( rc==SQLITE_OK ){
63054        pWal->writeLock = 1;
63055      }else if( rc==SQLITE_BUSY ){
63056        eMode2 = SQLITE_CHECKPOINT_PASSIVE;
63057        xBusy2 = 0;
63058        rc = SQLITE_OK;
63059      }
63060    }
63061  }
63062
63063
63064  /* Read the wal-index header. */
63065  if( rc==SQLITE_OK ){
63066    walDisableBlocking(pWal);
63067    rc = walIndexReadHdr(pWal, &isChanged);
63068    (void)walEnableBlocking(pWal);
63069    if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
63070      sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
63071    }
63072  }
63073
63074  /* Copy data from the log to the database file. */
63075  if( rc==SQLITE_OK ){
63076
63077    if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
63078      rc = SQLITE_CORRUPT_BKPT;
63079    }else{
63080      rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags, zBuf);
63081    }
63082
63083    /* If no error occurred, set the output variables. */
63084    if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
63085      if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
63086      if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
63087    }
63088  }
63089
63090  if( isChanged ){
63091    /* If a new wal-index header was loaded before the checkpoint was
63092    ** performed, then the pager-cache associated with pWal is now
63093    ** out of date. So zero the cached wal-index header to ensure that
63094    ** next time the pager opens a snapshot on this database it knows that
63095    ** the cache needs to be reset.
63096    */
63097    memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
63098  }
63099
63100  walDisableBlocking(pWal);
63101  sqlite3WalDb(pWal, 0);
63102
63103  /* Release the locks. */
63104  sqlite3WalEndWriteTransaction(pWal);
63105  if( pWal->ckptLock ){
63106    walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
63107    pWal->ckptLock = 0;
63108  }
63109  WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
63110#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
63111  if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY;
63112#endif
63113  return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
63114}
63115
63116/* Return the value to pass to a sqlite3_wal_hook callback, the
63117** number of frames in the WAL at the point of the last commit since
63118** sqlite3WalCallback() was called.  If no commits have occurred since
63119** the last call, then return 0.
63120*/
63121SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
63122  u32 ret = 0;
63123  if( pWal ){
63124    ret = pWal->iCallback;
63125    pWal->iCallback = 0;
63126  }
63127  return (int)ret;
63128}
63129
63130/*
63131** This function is called to change the WAL subsystem into or out
63132** of locking_mode=EXCLUSIVE.
63133**
63134** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
63135** into locking_mode=NORMAL.  This means that we must acquire a lock
63136** on the pWal->readLock byte.  If the WAL is already in locking_mode=NORMAL
63137** or if the acquisition of the lock fails, then return 0.  If the
63138** transition out of exclusive-mode is successful, return 1.  This
63139** operation must occur while the pager is still holding the exclusive
63140** lock on the main database file.
63141**
63142** If op is one, then change from locking_mode=NORMAL into
63143** locking_mode=EXCLUSIVE.  This means that the pWal->readLock must
63144** be released.  Return 1 if the transition is made and 0 if the
63145** WAL is already in exclusive-locking mode - meaning that this
63146** routine is a no-op.  The pager must already hold the exclusive lock
63147** on the main database file before invoking this operation.
63148**
63149** If op is negative, then do a dry-run of the op==1 case but do
63150** not actually change anything. The pager uses this to see if it
63151** should acquire the database exclusive lock prior to invoking
63152** the op==1 case.
63153*/
63154SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
63155  int rc;
63156  assert( pWal->writeLock==0 );
63157  assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
63158
63159  /* pWal->readLock is usually set, but might be -1 if there was a
63160  ** prior error while attempting to acquire are read-lock. This cannot
63161  ** happen if the connection is actually in exclusive mode (as no xShmLock
63162  ** locks are taken in this case). Nor should the pager attempt to
63163  ** upgrade to exclusive-mode following such an error.
63164  */
63165  assert( pWal->readLock>=0 || pWal->lockError );
63166  assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
63167
63168  if( op==0 ){
63169    if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
63170      pWal->exclusiveMode = WAL_NORMAL_MODE;
63171      if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
63172        pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
63173      }
63174      rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
63175    }else{
63176      /* Already in locking_mode=NORMAL */
63177      rc = 0;
63178    }
63179  }else if( op>0 ){
63180    assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
63181    assert( pWal->readLock>=0 );
63182    walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
63183    pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
63184    rc = 1;
63185  }else{
63186    rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
63187  }
63188  return rc;
63189}
63190
63191/*
63192** Return true if the argument is non-NULL and the WAL module is using
63193** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
63194** WAL module is using shared-memory, return false.
63195*/
63196SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
63197  return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
63198}
63199
63200#ifdef SQLITE_ENABLE_SNAPSHOT
63201/* Create a snapshot object.  The content of a snapshot is opaque to
63202** every other subsystem, so the WAL module can put whatever it needs
63203** in the object.
63204*/
63205SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){
63206  int rc = SQLITE_OK;
63207  WalIndexHdr *pRet;
63208  static const u32 aZero[4] = { 0, 0, 0, 0 };
63209
63210  assert( pWal->readLock>=0 && pWal->writeLock==0 );
63211
63212  if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
63213    *ppSnapshot = 0;
63214    return SQLITE_ERROR;
63215  }
63216  pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr));
63217  if( pRet==0 ){
63218    rc = SQLITE_NOMEM_BKPT;
63219  }else{
63220    memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
63221    *ppSnapshot = (sqlite3_snapshot*)pRet;
63222  }
63223
63224  return rc;
63225}
63226
63227/* Try to open on pSnapshot when the next read-transaction starts
63228*/
63229SQLITE_PRIVATE void sqlite3WalSnapshotOpen(
63230  Wal *pWal,
63231  sqlite3_snapshot *pSnapshot
63232){
63233  pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
63234}
63235
63236/*
63237** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
63238** p1 is older than p2 and zero if p1 and p2 are the same snapshot.
63239*/
63240SQLITE_API int sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){
63241  WalIndexHdr *pHdr1 = (WalIndexHdr*)p1;
63242  WalIndexHdr *pHdr2 = (WalIndexHdr*)p2;
63243
63244  /* aSalt[0] is a copy of the value stored in the wal file header. It
63245  ** is incremented each time the wal file is restarted.  */
63246  if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
63247  if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
63248  if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
63249  if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
63250  return 0;
63251}
63252
63253/*
63254** The caller currently has a read transaction open on the database.
63255** This function takes a SHARED lock on the CHECKPOINTER slot and then
63256** checks if the snapshot passed as the second argument is still
63257** available. If so, SQLITE_OK is returned.
63258**
63259** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
63260** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
63261** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
63262** lock is released before returning.
63263*/
63264SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){
63265  int rc;
63266  rc = walLockShared(pWal, WAL_CKPT_LOCK);
63267  if( rc==SQLITE_OK ){
63268    WalIndexHdr *pNew = (WalIndexHdr*)pSnapshot;
63269    if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
63270     || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
63271    ){
63272      rc = SQLITE_ERROR_SNAPSHOT;
63273      walUnlockShared(pWal, WAL_CKPT_LOCK);
63274    }
63275  }
63276  return rc;
63277}
63278
63279/*
63280** Release a lock obtained by an earlier successful call to
63281** sqlite3WalSnapshotCheck().
63282*/
63283SQLITE_PRIVATE void sqlite3WalSnapshotUnlock(Wal *pWal){
63284  assert( pWal );
63285  walUnlockShared(pWal, WAL_CKPT_LOCK);
63286}
63287
63288
63289#endif /* SQLITE_ENABLE_SNAPSHOT */
63290
63291#ifdef SQLITE_ENABLE_ZIPVFS
63292/*
63293** If the argument is not NULL, it points to a Wal object that holds a
63294** read-lock. This function returns the database page-size if it is known,
63295** or zero if it is not (or if pWal is NULL).
63296*/
63297SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){
63298  assert( pWal==0 || pWal->readLock>=0 );
63299  return (pWal ? pWal->szPage : 0);
63300}
63301#endif
63302
63303/* Return the sqlite3_file object for the WAL file
63304*/
63305SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal){
63306  return pWal->pWalFd;
63307}
63308
63309#endif /* #ifndef SQLITE_OMIT_WAL */
63310
63311/************** End of wal.c *************************************************/
63312/************** Begin file btmutex.c *****************************************/
63313/*
63314** 2007 August 27
63315**
63316** The author disclaims copyright to this source code.  In place of
63317** a legal notice, here is a blessing:
63318**
63319**    May you do good and not evil.
63320**    May you find forgiveness for yourself and forgive others.
63321**    May you share freely, never taking more than you give.
63322**
63323*************************************************************************
63324**
63325** This file contains code used to implement mutexes on Btree objects.
63326** This code really belongs in btree.c.  But btree.c is getting too
63327** big and we want to break it down some.  This packaged seemed like
63328** a good breakout.
63329*/
63330/************** Include btreeInt.h in the middle of btmutex.c ****************/
63331/************** Begin file btreeInt.h ****************************************/
63332/*
63333** 2004 April 6
63334**
63335** The author disclaims copyright to this source code.  In place of
63336** a legal notice, here is a blessing:
63337**
63338**    May you do good and not evil.
63339**    May you find forgiveness for yourself and forgive others.
63340**    May you share freely, never taking more than you give.
63341**
63342*************************************************************************
63343** This file implements an external (disk-based) database using BTrees.
63344** For a detailed discussion of BTrees, refer to
63345**
63346**     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
63347**     "Sorting And Searching", pages 473-480. Addison-Wesley
63348**     Publishing Company, Reading, Massachusetts.
63349**
63350** The basic idea is that each page of the file contains N database
63351** entries and N+1 pointers to subpages.
63352**
63353**   ----------------------------------------------------------------
63354**   |  Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
63355**   ----------------------------------------------------------------
63356**
63357** All of the keys on the page that Ptr(0) points to have values less
63358** than Key(0).  All of the keys on page Ptr(1) and its subpages have
63359** values greater than Key(0) and less than Key(1).  All of the keys
63360** on Ptr(N) and its subpages have values greater than Key(N-1).  And
63361** so forth.
63362**
63363** Finding a particular key requires reading O(log(M)) pages from the
63364** disk where M is the number of entries in the tree.
63365**
63366** In this implementation, a single file can hold one or more separate
63367** BTrees.  Each BTree is identified by the index of its root page.  The
63368** key and data for any entry are combined to form the "payload".  A
63369** fixed amount of payload can be carried directly on the database
63370** page.  If the payload is larger than the preset amount then surplus
63371** bytes are stored on overflow pages.  The payload for an entry
63372** and the preceding pointer are combined to form a "Cell".  Each
63373** page has a small header which contains the Ptr(N) pointer and other
63374** information such as the size of key and data.
63375**
63376** FORMAT DETAILS
63377**
63378** The file is divided into pages.  The first page is called page 1,
63379** the second is page 2, and so forth.  A page number of zero indicates
63380** "no such page".  The page size can be any power of 2 between 512 and 65536.
63381** Each page can be either a btree page, a freelist page, an overflow
63382** page, or a pointer-map page.
63383**
63384** The first page is always a btree page.  The first 100 bytes of the first
63385** page contain a special header (the "file header") that describes the file.
63386** The format of the file header is as follows:
63387**
63388**   OFFSET   SIZE    DESCRIPTION
63389**      0      16     Header string: "SQLite format 3\000"
63390**     16       2     Page size in bytes.  (1 means 65536)
63391**     18       1     File format write version
63392**     19       1     File format read version
63393**     20       1     Bytes of unused space at the end of each page
63394**     21       1     Max embedded payload fraction (must be 64)
63395**     22       1     Min embedded payload fraction (must be 32)
63396**     23       1     Min leaf payload fraction (must be 32)
63397**     24       4     File change counter
63398**     28       4     Reserved for future use
63399**     32       4     First freelist page
63400**     36       4     Number of freelist pages in the file
63401**     40      60     15 4-byte meta values passed to higher layers
63402**
63403**     40       4     Schema cookie
63404**     44       4     File format of schema layer
63405**     48       4     Size of page cache
63406**     52       4     Largest root-page (auto/incr_vacuum)
63407**     56       4     1=UTF-8 2=UTF16le 3=UTF16be
63408**     60       4     User version
63409**     64       4     Incremental vacuum mode
63410**     68       4     Application-ID
63411**     72      20     unused
63412**     92       4     The version-valid-for number
63413**     96       4     SQLITE_VERSION_NUMBER
63414**
63415** All of the integer values are big-endian (most significant byte first).
63416**
63417** The file change counter is incremented when the database is changed
63418** This counter allows other processes to know when the file has changed
63419** and thus when they need to flush their cache.
63420**
63421** The max embedded payload fraction is the amount of the total usable
63422** space in a page that can be consumed by a single cell for standard
63423** B-tree (non-LEAFDATA) tables.  A value of 255 means 100%.  The default
63424** is to limit the maximum cell size so that at least 4 cells will fit
63425** on one page.  Thus the default max embedded payload fraction is 64.
63426**
63427** If the payload for a cell is larger than the max payload, then extra
63428** payload is spilled to overflow pages.  Once an overflow page is allocated,
63429** as many bytes as possible are moved into the overflow pages without letting
63430** the cell size drop below the min embedded payload fraction.
63431**
63432** The min leaf payload fraction is like the min embedded payload fraction
63433** except that it applies to leaf nodes in a LEAFDATA tree.  The maximum
63434** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
63435** not specified in the header.
63436**
63437** Each btree pages is divided into three sections:  The header, the
63438** cell pointer array, and the cell content area.  Page 1 also has a 100-byte
63439** file header that occurs before the page header.
63440**
63441**      |----------------|
63442**      | file header    |   100 bytes.  Page 1 only.
63443**      |----------------|
63444**      | page header    |   8 bytes for leaves.  12 bytes for interior nodes
63445**      |----------------|
63446**      | cell pointer   |   |  2 bytes per cell.  Sorted order.
63447**      | array          |   |  Grows downward
63448**      |                |   v
63449**      |----------------|
63450**      | unallocated    |
63451**      | space          |
63452**      |----------------|   ^  Grows upwards
63453**      | cell content   |   |  Arbitrary order interspersed with freeblocks.
63454**      | area           |   |  and free space fragments.
63455**      |----------------|
63456**
63457** The page headers looks like this:
63458**
63459**   OFFSET   SIZE     DESCRIPTION
63460**      0       1      Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
63461**      1       2      byte offset to the first freeblock
63462**      3       2      number of cells on this page
63463**      5       2      first byte of the cell content area
63464**      7       1      number of fragmented free bytes
63465**      8       4      Right child (the Ptr(N) value).  Omitted on leaves.
63466**
63467** The flags define the format of this btree page.  The leaf flag means that
63468** this page has no children.  The zerodata flag means that this page carries
63469** only keys and no data.  The intkey flag means that the key is an integer
63470** which is stored in the key size entry of the cell header rather than in
63471** the payload area.
63472**
63473** The cell pointer array begins on the first byte after the page header.
63474** The cell pointer array contains zero or more 2-byte numbers which are
63475** offsets from the beginning of the page to the cell content in the cell
63476** content area.  The cell pointers occur in sorted order.  The system strives
63477** to keep free space after the last cell pointer so that new cells can
63478** be easily added without having to defragment the page.
63479**
63480** Cell content is stored at the very end of the page and grows toward the
63481** beginning of the page.
63482**
63483** Unused space within the cell content area is collected into a linked list of
63484** freeblocks.  Each freeblock is at least 4 bytes in size.  The byte offset
63485** to the first freeblock is given in the header.  Freeblocks occur in
63486** increasing order.  Because a freeblock must be at least 4 bytes in size,
63487** any group of 3 or fewer unused bytes in the cell content area cannot
63488** exist on the freeblock chain.  A group of 3 or fewer free bytes is called
63489** a fragment.  The total number of bytes in all fragments is recorded.
63490** in the page header at offset 7.
63491**
63492**    SIZE    DESCRIPTION
63493**      2     Byte offset of the next freeblock
63494**      2     Bytes in this freeblock
63495**
63496** Cells are of variable length.  Cells are stored in the cell content area at
63497** the end of the page.  Pointers to the cells are in the cell pointer array
63498** that immediately follows the page header.  Cells is not necessarily
63499** contiguous or in order, but cell pointers are contiguous and in order.
63500**
63501** Cell content makes use of variable length integers.  A variable
63502** length integer is 1 to 9 bytes where the lower 7 bits of each
63503** byte are used.  The integer consists of all bytes that have bit 8 set and
63504** the first byte with bit 8 clear.  The most significant byte of the integer
63505** appears first.  A variable-length integer may not be more than 9 bytes long.
63506** As a special case, all 8 bytes of the 9th byte are used as data.  This
63507** allows a 64-bit integer to be encoded in 9 bytes.
63508**
63509**    0x00                      becomes  0x00000000
63510**    0x7f                      becomes  0x0000007f
63511**    0x81 0x00                 becomes  0x00000080
63512**    0x82 0x00                 becomes  0x00000100
63513**    0x80 0x7f                 becomes  0x0000007f
63514**    0x8a 0x91 0xd1 0xac 0x78  becomes  0x12345678
63515**    0x81 0x81 0x81 0x81 0x01  becomes  0x10204081
63516**
63517** Variable length integers are used for rowids and to hold the number of
63518** bytes of key and data in a btree cell.
63519**
63520** The content of a cell looks like this:
63521**
63522**    SIZE    DESCRIPTION
63523**      4     Page number of the left child. Omitted if leaf flag is set.
63524**     var    Number of bytes of data. Omitted if the zerodata flag is set.
63525**     var    Number of bytes of key. Or the key itself if intkey flag is set.
63526**      *     Payload
63527**      4     First page of the overflow chain.  Omitted if no overflow
63528**
63529** Overflow pages form a linked list.  Each page except the last is completely
63530** filled with data (pagesize - 4 bytes).  The last page can have as little
63531** as 1 byte of data.
63532**
63533**    SIZE    DESCRIPTION
63534**      4     Page number of next overflow page
63535**      *     Data
63536**
63537** Freelist pages come in two subtypes: trunk pages and leaf pages.  The
63538** file header points to the first in a linked list of trunk page.  Each trunk
63539** page points to multiple leaf pages.  The content of a leaf page is
63540** unspecified.  A trunk page looks like this:
63541**
63542**    SIZE    DESCRIPTION
63543**      4     Page number of next trunk page
63544**      4     Number of leaf pointers on this page
63545**      *     zero or more pages numbers of leaves
63546*/
63547/* #include "sqliteInt.h" */
63548
63549
63550/* The following value is the maximum cell size assuming a maximum page
63551** size give above.
63552*/
63553#define MX_CELL_SIZE(pBt)  ((int)(pBt->pageSize-8))
63554
63555/* The maximum number of cells on a single page of the database.  This
63556** assumes a minimum cell size of 6 bytes  (4 bytes for the cell itself
63557** plus 2 bytes for the index to the cell in the page header).  Such
63558** small cells will be rare, but they are possible.
63559*/
63560#define MX_CELL(pBt) ((pBt->pageSize-8)/6)
63561
63562/* Forward declarations */
63563typedef struct MemPage MemPage;
63564typedef struct BtLock BtLock;
63565typedef struct CellInfo CellInfo;
63566
63567/*
63568** This is a magic string that appears at the beginning of every
63569** SQLite database in order to identify the file as a real database.
63570**
63571** You can change this value at compile-time by specifying a
63572** -DSQLITE_FILE_HEADER="..." on the compiler command-line.  The
63573** header must be exactly 16 bytes including the zero-terminator so
63574** the string itself should be 15 characters long.  If you change
63575** the header, then your custom library will not be able to read
63576** databases generated by the standard tools and the standard tools
63577** will not be able to read databases created by your custom library.
63578*/
63579#ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
63580#  define SQLITE_FILE_HEADER "SQLite format 3"
63581#endif
63582
63583/*
63584** Page type flags.  An ORed combination of these flags appear as the
63585** first byte of on-disk image of every BTree page.
63586*/
63587#define PTF_INTKEY    0x01
63588#define PTF_ZERODATA  0x02
63589#define PTF_LEAFDATA  0x04
63590#define PTF_LEAF      0x08
63591
63592/*
63593** An instance of this object stores information about each a single database
63594** page that has been loaded into memory.  The information in this object
63595** is derived from the raw on-disk page content.
63596**
63597** As each database page is loaded into memory, the pager allocats an
63598** instance of this object and zeros the first 8 bytes.  (This is the
63599** "extra" information associated with each page of the pager.)
63600**
63601** Access to all fields of this structure is controlled by the mutex
63602** stored in MemPage.pBt->mutex.
63603*/
63604struct MemPage {
63605  u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
63606  u8 bBusy;            /* Prevent endless loops on corrupt database files */
63607  u8 intKey;           /* True if table b-trees.  False for index b-trees */
63608  u8 intKeyLeaf;       /* True if the leaf of an intKey table */
63609  Pgno pgno;           /* Page number for this page */
63610  /* Only the first 8 bytes (above) are zeroed by pager.c when a new page
63611  ** is allocated. All fields that follow must be initialized before use */
63612  u8 leaf;             /* True if a leaf page */
63613  u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
63614  u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
63615  u8 max1bytePayload;  /* min(maxLocal,127) */
63616  u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
63617  u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
63618  u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
63619  u16 cellOffset;      /* Index in aData of first cell pointer */
63620  int nFree;           /* Number of free bytes on the page. -1 for unknown */
63621  u16 nCell;           /* Number of cells on this page, local and ovfl */
63622  u16 maskPage;        /* Mask for page offset */
63623  u16 aiOvfl[4];       /* Insert the i-th overflow cell before the aiOvfl-th
63624                       ** non-overflow cell */
63625  u8 *apOvfl[4];       /* Pointers to the body of overflow cells */
63626  BtShared *pBt;       /* Pointer to BtShared that this page is part of */
63627  u8 *aData;           /* Pointer to disk image of the page data */
63628  u8 *aDataEnd;        /* One byte past the end of usable data */
63629  u8 *aCellIdx;        /* The cell index area */
63630  u8 *aDataOfst;       /* Same as aData for leaves.  aData+4 for interior */
63631  DbPage *pDbPage;     /* Pager page handle */
63632  u16 (*xCellSize)(MemPage*,u8*);             /* cellSizePtr method */
63633  void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
63634};
63635
63636/*
63637** A linked list of the following structures is stored at BtShared.pLock.
63638** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
63639** is opened on the table with root page BtShared.iTable. Locks are removed
63640** from this list when a transaction is committed or rolled back, or when
63641** a btree handle is closed.
63642*/
63643struct BtLock {
63644  Btree *pBtree;        /* Btree handle holding this lock */
63645  Pgno iTable;          /* Root page of table */
63646  u8 eLock;             /* READ_LOCK or WRITE_LOCK */
63647  BtLock *pNext;        /* Next in BtShared.pLock list */
63648};
63649
63650/* Candidate values for BtLock.eLock */
63651#define READ_LOCK     1
63652#define WRITE_LOCK    2
63653
63654/* A Btree handle
63655**
63656** A database connection contains a pointer to an instance of
63657** this object for every database file that it has open.  This structure
63658** is opaque to the database connection.  The database connection cannot
63659** see the internals of this structure and only deals with pointers to
63660** this structure.
63661**
63662** For some database files, the same underlying database cache might be
63663** shared between multiple connections.  In that case, each connection
63664** has it own instance of this object.  But each instance of this object
63665** points to the same BtShared object.  The database cache and the
63666** schema associated with the database file are all contained within
63667** the BtShared object.
63668**
63669** All fields in this structure are accessed under sqlite3.mutex.
63670** The pBt pointer itself may not be changed while there exists cursors
63671** in the referenced BtShared that point back to this Btree since those
63672** cursors have to go through this Btree to find their BtShared and
63673** they often do so without holding sqlite3.mutex.
63674*/
63675struct Btree {
63676  sqlite3 *db;       /* The database connection holding this btree */
63677  BtShared *pBt;     /* Sharable content of this btree */
63678  u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
63679  u8 sharable;       /* True if we can share pBt with another db */
63680  u8 locked;         /* True if db currently has pBt locked */
63681  u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */
63682  int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
63683  int nBackup;       /* Number of backup operations reading this btree */
63684  u32 iDataVersion;  /* Combines with pBt->pPager->iDataVersion */
63685  Btree *pNext;      /* List of other sharable Btrees from the same db */
63686  Btree *pPrev;      /* Back pointer of the same list */
63687#ifndef SQLITE_OMIT_SHARED_CACHE
63688  BtLock lock;       /* Object used to lock page 1 */
63689#endif
63690};
63691
63692/*
63693** Btree.inTrans may take one of the following values.
63694**
63695** If the shared-data extension is enabled, there may be multiple users
63696** of the Btree structure. At most one of these may open a write transaction,
63697** but any number may have active read transactions.
63698*/
63699#define TRANS_NONE  0
63700#define TRANS_READ  1
63701#define TRANS_WRITE 2
63702
63703/*
63704** An instance of this object represents a single database file.
63705**
63706** A single database file can be in use at the same time by two
63707** or more database connections.  When two or more connections are
63708** sharing the same database file, each connection has it own
63709** private Btree object for the file and each of those Btrees points
63710** to this one BtShared object.  BtShared.nRef is the number of
63711** connections currently sharing this database file.
63712**
63713** Fields in this structure are accessed under the BtShared.mutex
63714** mutex, except for nRef and pNext which are accessed under the
63715** global SQLITE_MUTEX_STATIC_MASTER mutex.  The pPager field
63716** may not be modified once it is initially set as long as nRef>0.
63717** The pSchema field may be set once under BtShared.mutex and
63718** thereafter is unchanged as long as nRef>0.
63719**
63720** isPending:
63721**
63722**   If a BtShared client fails to obtain a write-lock on a database
63723**   table (because there exists one or more read-locks on the table),
63724**   the shared-cache enters 'pending-lock' state and isPending is
63725**   set to true.
63726**
63727**   The shared-cache leaves the 'pending lock' state when either of
63728**   the following occur:
63729**
63730**     1) The current writer (BtShared.pWriter) concludes its transaction, OR
63731**     2) The number of locks held by other connections drops to zero.
63732**
63733**   while in the 'pending-lock' state, no connection may start a new
63734**   transaction.
63735**
63736**   This feature is included to help prevent writer-starvation.
63737*/
63738struct BtShared {
63739  Pager *pPager;        /* The page cache */
63740  sqlite3 *db;          /* Database connection currently using this Btree */
63741  BtCursor *pCursor;    /* A list of all open cursors */
63742  MemPage *pPage1;      /* First page of the database */
63743  u8 openFlags;         /* Flags to sqlite3BtreeOpen() */
63744#ifndef SQLITE_OMIT_AUTOVACUUM
63745  u8 autoVacuum;        /* True if auto-vacuum is enabled */
63746  u8 incrVacuum;        /* True if incr-vacuum is enabled */
63747  u8 bDoTruncate;       /* True to truncate db on commit */
63748#endif
63749  u8 inTransaction;     /* Transaction state */
63750  u8 max1bytePayload;   /* Maximum first byte of cell for a 1-byte payload */
63751  u8 nReserveWanted;    /* Desired number of extra bytes per page */
63752  u16 btsFlags;         /* Boolean parameters.  See BTS_* macros below */
63753  u16 maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
63754  u16 minLocal;         /* Minimum local payload in non-LEAFDATA tables */
63755  u16 maxLeaf;          /* Maximum local payload in a LEAFDATA table */
63756  u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
63757  u32 pageSize;         /* Total number of bytes on a page */
63758  u32 usableSize;       /* Number of usable bytes on each page */
63759  int nTransaction;     /* Number of open transactions (read + write) */
63760  u32 nPage;            /* Number of pages in the database */
63761  void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
63762  void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
63763  sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
63764  Bitvec *pHasContent;  /* Set of pages moved to free-list this transaction */
63765#ifndef SQLITE_OMIT_SHARED_CACHE
63766  int nRef;             /* Number of references to this structure */
63767  BtShared *pNext;      /* Next on a list of sharable BtShared structs */
63768  BtLock *pLock;        /* List of locks held on this shared-btree struct */
63769  Btree *pWriter;       /* Btree with currently open write transaction */
63770#endif
63771  u8 *pTmpSpace;        /* Temp space sufficient to hold a single cell */
63772};
63773
63774/*
63775** Allowed values for BtShared.btsFlags
63776*/
63777#define BTS_READ_ONLY        0x0001   /* Underlying file is readonly */
63778#define BTS_PAGESIZE_FIXED   0x0002   /* Page size can no longer be changed */
63779#define BTS_SECURE_DELETE    0x0004   /* PRAGMA secure_delete is enabled */
63780#define BTS_OVERWRITE        0x0008   /* Overwrite deleted content with zeros */
63781#define BTS_FAST_SECURE      0x000c   /* Combination of the previous two */
63782#define BTS_INITIALLY_EMPTY  0x0010   /* Database was empty at trans start */
63783#define BTS_NO_WAL           0x0020   /* Do not open write-ahead-log files */
63784#define BTS_EXCLUSIVE        0x0040   /* pWriter has an exclusive lock */
63785#define BTS_PENDING          0x0080   /* Waiting for read-locks to clear */
63786
63787/*
63788** An instance of the following structure is used to hold information
63789** about a cell.  The parseCellPtr() function fills in this structure
63790** based on information extract from the raw disk page.
63791*/
63792struct CellInfo {
63793  i64 nKey;      /* The key for INTKEY tables, or nPayload otherwise */
63794  u8 *pPayload;  /* Pointer to the start of payload */
63795  u32 nPayload;  /* Bytes of payload */
63796  u16 nLocal;    /* Amount of payload held locally, not on overflow */
63797  u16 nSize;     /* Size of the cell content on the main b-tree page */
63798};
63799
63800/*
63801** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
63802** this will be declared corrupt. This value is calculated based on a
63803** maximum database size of 2^31 pages a minimum fanout of 2 for a
63804** root-node and 3 for all other internal nodes.
63805**
63806** If a tree that appears to be taller than this is encountered, it is
63807** assumed that the database is corrupt.
63808*/
63809#define BTCURSOR_MAX_DEPTH 20
63810
63811/*
63812** A cursor is a pointer to a particular entry within a particular
63813** b-tree within a database file.
63814**
63815** The entry is identified by its MemPage and the index in
63816** MemPage.aCell[] of the entry.
63817**
63818** A single database file can be shared by two more database connections,
63819** but cursors cannot be shared.  Each cursor is associated with a
63820** particular database connection identified BtCursor.pBtree.db.
63821**
63822** Fields in this structure are accessed under the BtShared.mutex
63823** found at self->pBt->mutex.
63824**
63825** skipNext meaning:
63826** The meaning of skipNext depends on the value of eState:
63827**
63828**   eState            Meaning of skipNext
63829**   VALID             skipNext is meaningless and is ignored
63830**   INVALID           skipNext is meaningless and is ignored
63831**   SKIPNEXT          sqlite3BtreeNext() is a no-op if skipNext>0 and
63832**                     sqlite3BtreePrevious() is no-op if skipNext<0.
63833**   REQUIRESEEK       restoreCursorPosition() restores the cursor to
63834**                     eState=SKIPNEXT if skipNext!=0
63835**   FAULT             skipNext holds the cursor fault error code.
63836*/
63837struct BtCursor {
63838  u8 eState;                /* One of the CURSOR_XXX constants (see below) */
63839  u8 curFlags;              /* zero or more BTCF_* flags defined below */
63840  u8 curPagerFlags;         /* Flags to send to sqlite3PagerGet() */
63841  u8 hints;                 /* As configured by CursorSetHints() */
63842  int skipNext;    /* Prev() is noop if negative. Next() is noop if positive.
63843                   ** Error code if eState==CURSOR_FAULT */
63844  Btree *pBtree;            /* The Btree to which this cursor belongs */
63845  Pgno *aOverflow;          /* Cache of overflow page locations */
63846  void *pKey;               /* Saved key that was cursor last known position */
63847  /* All fields above are zeroed when the cursor is allocated.  See
63848  ** sqlite3BtreeCursorZero().  Fields that follow must be manually
63849  ** initialized. */
63850#define BTCURSOR_FIRST_UNINIT pBt   /* Name of first uninitialized field */
63851  BtShared *pBt;            /* The BtShared this cursor points to */
63852  BtCursor *pNext;          /* Forms a linked list of all cursors */
63853  CellInfo info;            /* A parse of the cell we are pointing at */
63854  i64 nKey;                 /* Size of pKey, or last integer key */
63855  Pgno pgnoRoot;            /* The root page of this tree */
63856  i8 iPage;                 /* Index of current page in apPage */
63857  u8 curIntKey;             /* Value of apPage[0]->intKey */
63858  u16 ix;                   /* Current index for apPage[iPage] */
63859  u16 aiIdx[BTCURSOR_MAX_DEPTH-1];     /* Current index in apPage[i] */
63860  struct KeyInfo *pKeyInfo;            /* Arg passed to comparison function */
63861  MemPage *pPage;                        /* Current page */
63862  MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
63863};
63864
63865/*
63866** Legal values for BtCursor.curFlags
63867*/
63868#define BTCF_WriteFlag    0x01   /* True if a write cursor */
63869#define BTCF_ValidNKey    0x02   /* True if info.nKey is valid */
63870#define BTCF_ValidOvfl    0x04   /* True if aOverflow is valid */
63871#define BTCF_AtLast       0x08   /* Cursor is pointing ot the last entry */
63872#define BTCF_Incrblob     0x10   /* True if an incremental I/O handle */
63873#define BTCF_Multiple     0x20   /* Maybe another cursor on the same btree */
63874#define BTCF_Pinned       0x40   /* Cursor is busy and cannot be moved */
63875
63876/*
63877** Potential values for BtCursor.eState.
63878**
63879** CURSOR_INVALID:
63880**   Cursor does not point to a valid entry. This can happen (for example)
63881**   because the table is empty or because BtreeCursorFirst() has not been
63882**   called.
63883**
63884** CURSOR_VALID:
63885**   Cursor points to a valid entry. getPayload() etc. may be called.
63886**
63887** CURSOR_SKIPNEXT:
63888**   Cursor is valid except that the Cursor.skipNext field is non-zero
63889**   indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious()
63890**   operation should be a no-op.
63891**
63892** CURSOR_REQUIRESEEK:
63893**   The table that this cursor was opened on still exists, but has been
63894**   modified since the cursor was last used. The cursor position is saved
63895**   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
63896**   this state, restoreCursorPosition() can be called to attempt to
63897**   seek the cursor to the saved position.
63898**
63899** CURSOR_FAULT:
63900**   An unrecoverable error (an I/O error or a malloc failure) has occurred
63901**   on a different connection that shares the BtShared cache with this
63902**   cursor.  The error has left the cache in an inconsistent state.
63903**   Do nothing else with this cursor.  Any attempt to use the cursor
63904**   should return the error code stored in BtCursor.skipNext
63905*/
63906#define CURSOR_VALID             0
63907#define CURSOR_INVALID           1
63908#define CURSOR_SKIPNEXT          2
63909#define CURSOR_REQUIRESEEK       3
63910#define CURSOR_FAULT             4
63911
63912/*
63913** The database page the PENDING_BYTE occupies. This page is never used.
63914*/
63915# define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
63916
63917/*
63918** These macros define the location of the pointer-map entry for a
63919** database page. The first argument to each is the number of usable
63920** bytes on each page of the database (often 1024). The second is the
63921** page number to look up in the pointer map.
63922**
63923** PTRMAP_PAGENO returns the database page number of the pointer-map
63924** page that stores the required pointer. PTRMAP_PTROFFSET returns
63925** the offset of the requested map entry.
63926**
63927** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
63928** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
63929** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
63930** this test.
63931*/
63932#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
63933#define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
63934#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
63935
63936/*
63937** The pointer map is a lookup table that identifies the parent page for
63938** each child page in the database file.  The parent page is the page that
63939** contains a pointer to the child.  Every page in the database contains
63940** 0 or 1 parent pages.  (In this context 'database page' refers
63941** to any page that is not part of the pointer map itself.)  Each pointer map
63942** entry consists of a single byte 'type' and a 4 byte parent page number.
63943** The PTRMAP_XXX identifiers below are the valid types.
63944**
63945** The purpose of the pointer map is to facility moving pages from one
63946** position in the file to another as part of autovacuum.  When a page
63947** is moved, the pointer in its parent must be updated to point to the
63948** new location.  The pointer map is used to locate the parent page quickly.
63949**
63950** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
63951**                  used in this case.
63952**
63953** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
63954**                  is not used in this case.
63955**
63956** PTRMAP_OVERFLOW1: The database page is the first page in a list of
63957**                   overflow pages. The page number identifies the page that
63958**                   contains the cell with a pointer to this overflow page.
63959**
63960** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
63961**                   overflow pages. The page-number identifies the previous
63962**                   page in the overflow page list.
63963**
63964** PTRMAP_BTREE: The database page is a non-root btree page. The page number
63965**               identifies the parent page in the btree.
63966*/
63967#define PTRMAP_ROOTPAGE 1
63968#define PTRMAP_FREEPAGE 2
63969#define PTRMAP_OVERFLOW1 3
63970#define PTRMAP_OVERFLOW2 4
63971#define PTRMAP_BTREE 5
63972
63973/* A bunch of assert() statements to check the transaction state variables
63974** of handle p (type Btree*) are internally consistent.
63975*/
63976#define btreeIntegrity(p) \
63977  assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
63978  assert( p->pBt->inTransaction>=p->inTrans );
63979
63980
63981/*
63982** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
63983** if the database supports auto-vacuum or not. Because it is used
63984** within an expression that is an argument to another macro
63985** (sqliteMallocRaw), it is not possible to use conditional compilation.
63986** So, this macro is defined instead.
63987*/
63988#ifndef SQLITE_OMIT_AUTOVACUUM
63989#define ISAUTOVACUUM (pBt->autoVacuum)
63990#else
63991#define ISAUTOVACUUM 0
63992#endif
63993
63994
63995/*
63996** This structure is passed around through all the sanity checking routines
63997** in order to keep track of some global state information.
63998**
63999** The aRef[] array is allocated so that there is 1 bit for each page in
64000** the database. As the integrity-check proceeds, for each page used in
64001** the database the corresponding bit is set. This allows integrity-check to
64002** detect pages that are used twice and orphaned pages (both of which
64003** indicate corruption).
64004*/
64005typedef struct IntegrityCk IntegrityCk;
64006struct IntegrityCk {
64007  BtShared *pBt;    /* The tree being checked out */
64008  Pager *pPager;    /* The associated pager.  Also accessible by pBt->pPager */
64009  u8 *aPgRef;       /* 1 bit per page in the db (see above) */
64010  Pgno nPage;       /* Number of pages in the database */
64011  int mxErr;        /* Stop accumulating errors when this reaches zero */
64012  int nErr;         /* Number of messages written to zErrMsg so far */
64013  int mallocFailed; /* A memory allocation error has occurred */
64014  const char *zPfx; /* Error message prefix */
64015  int v1, v2;       /* Values for up to two %d fields in zPfx */
64016  StrAccum errMsg;  /* Accumulate the error message text here */
64017  u32 *heap;        /* Min-heap used for analyzing cell coverage */
64018  sqlite3 *db;      /* Database connection running the check */
64019};
64020
64021/*
64022** Routines to read or write a two- and four-byte big-endian integer values.
64023*/
64024#define get2byte(x)   ((x)[0]<<8 | (x)[1])
64025#define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
64026#define get4byte sqlite3Get4byte
64027#define put4byte sqlite3Put4byte
64028
64029/*
64030** get2byteAligned(), unlike get2byte(), requires that its argument point to a
64031** two-byte aligned address.  get2bytea() is only used for accessing the
64032** cell addresses in a btree header.
64033*/
64034#if SQLITE_BYTEORDER==4321
64035# define get2byteAligned(x)  (*(u16*)(x))
64036#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
64037# define get2byteAligned(x)  __builtin_bswap16(*(u16*)(x))
64038#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
64039# define get2byteAligned(x)  _byteswap_ushort(*(u16*)(x))
64040#else
64041# define get2byteAligned(x)  ((x)[0]<<8 | (x)[1])
64042#endif
64043
64044/************** End of btreeInt.h ********************************************/
64045/************** Continuing where we left off in btmutex.c ********************/
64046#ifndef SQLITE_OMIT_SHARED_CACHE
64047#if SQLITE_THREADSAFE
64048
64049/*
64050** Obtain the BtShared mutex associated with B-Tree handle p. Also,
64051** set BtShared.db to the database handle associated with p and the
64052** p->locked boolean to true.
64053*/
64054static void lockBtreeMutex(Btree *p){
64055  assert( p->locked==0 );
64056  assert( sqlite3_mutex_notheld(p->pBt->mutex) );
64057  assert( sqlite3_mutex_held(p->db->mutex) );
64058
64059  sqlite3_mutex_enter(p->pBt->mutex);
64060  p->pBt->db = p->db;
64061  p->locked = 1;
64062}
64063
64064/*
64065** Release the BtShared mutex associated with B-Tree handle p and
64066** clear the p->locked boolean.
64067*/
64068static void SQLITE_NOINLINE unlockBtreeMutex(Btree *p){
64069  BtShared *pBt = p->pBt;
64070  assert( p->locked==1 );
64071  assert( sqlite3_mutex_held(pBt->mutex) );
64072  assert( sqlite3_mutex_held(p->db->mutex) );
64073  assert( p->db==pBt->db );
64074
64075  sqlite3_mutex_leave(pBt->mutex);
64076  p->locked = 0;
64077}
64078
64079/* Forward reference */
64080static void SQLITE_NOINLINE btreeLockCarefully(Btree *p);
64081
64082/*
64083** Enter a mutex on the given BTree object.
64084**
64085** If the object is not sharable, then no mutex is ever required
64086** and this routine is a no-op.  The underlying mutex is non-recursive.
64087** But we keep a reference count in Btree.wantToLock so the behavior
64088** of this interface is recursive.
64089**
64090** To avoid deadlocks, multiple Btrees are locked in the same order
64091** by all database connections.  The p->pNext is a list of other
64092** Btrees belonging to the same database connection as the p Btree
64093** which need to be locked after p.  If we cannot get a lock on
64094** p, then first unlock all of the others on p->pNext, then wait
64095** for the lock to become available on p, then relock all of the
64096** subsequent Btrees that desire a lock.
64097*/
64098SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
64099  /* Some basic sanity checking on the Btree.  The list of Btrees
64100  ** connected by pNext and pPrev should be in sorted order by
64101  ** Btree.pBt value. All elements of the list should belong to
64102  ** the same connection. Only shared Btrees are on the list. */
64103  assert( p->pNext==0 || p->pNext->pBt>p->pBt );
64104  assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
64105  assert( p->pNext==0 || p->pNext->db==p->db );
64106  assert( p->pPrev==0 || p->pPrev->db==p->db );
64107  assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
64108
64109  /* Check for locking consistency */
64110  assert( !p->locked || p->wantToLock>0 );
64111  assert( p->sharable || p->wantToLock==0 );
64112
64113  /* We should already hold a lock on the database connection */
64114  assert( sqlite3_mutex_held(p->db->mutex) );
64115
64116  /* Unless the database is sharable and unlocked, then BtShared.db
64117  ** should already be set correctly. */
64118  assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
64119
64120  if( !p->sharable ) return;
64121  p->wantToLock++;
64122  if( p->locked ) return;
64123  btreeLockCarefully(p);
64124}
64125
64126/* This is a helper function for sqlite3BtreeLock(). By moving
64127** complex, but seldom used logic, out of sqlite3BtreeLock() and
64128** into this routine, we avoid unnecessary stack pointer changes
64129** and thus help the sqlite3BtreeLock() routine to run much faster
64130** in the common case.
64131*/
64132static void SQLITE_NOINLINE btreeLockCarefully(Btree *p){
64133  Btree *pLater;
64134
64135  /* In most cases, we should be able to acquire the lock we
64136  ** want without having to go through the ascending lock
64137  ** procedure that follows.  Just be sure not to block.
64138  */
64139  if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
64140    p->pBt->db = p->db;
64141    p->locked = 1;
64142    return;
64143  }
64144
64145  /* To avoid deadlock, first release all locks with a larger
64146  ** BtShared address.  Then acquire our lock.  Then reacquire
64147  ** the other BtShared locks that we used to hold in ascending
64148  ** order.
64149  */
64150  for(pLater=p->pNext; pLater; pLater=pLater->pNext){
64151    assert( pLater->sharable );
64152    assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
64153    assert( !pLater->locked || pLater->wantToLock>0 );
64154    if( pLater->locked ){
64155      unlockBtreeMutex(pLater);
64156    }
64157  }
64158  lockBtreeMutex(p);
64159  for(pLater=p->pNext; pLater; pLater=pLater->pNext){
64160    if( pLater->wantToLock ){
64161      lockBtreeMutex(pLater);
64162    }
64163  }
64164}
64165
64166
64167/*
64168** Exit the recursive mutex on a Btree.
64169*/
64170SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
64171  assert( sqlite3_mutex_held(p->db->mutex) );
64172  if( p->sharable ){
64173    assert( p->wantToLock>0 );
64174    p->wantToLock--;
64175    if( p->wantToLock==0 ){
64176      unlockBtreeMutex(p);
64177    }
64178  }
64179}
64180
64181#ifndef NDEBUG
64182/*
64183** Return true if the BtShared mutex is held on the btree, or if the
64184** B-Tree is not marked as sharable.
64185**
64186** This routine is used only from within assert() statements.
64187*/
64188SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
64189  assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
64190  assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
64191  assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
64192  assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
64193
64194  return (p->sharable==0 || p->locked);
64195}
64196#endif
64197
64198
64199/*
64200** Enter the mutex on every Btree associated with a database
64201** connection.  This is needed (for example) prior to parsing
64202** a statement since we will be comparing table and column names
64203** against all schemas and we do not want those schemas being
64204** reset out from under us.
64205**
64206** There is a corresponding leave-all procedures.
64207**
64208** Enter the mutexes in accending order by BtShared pointer address
64209** to avoid the possibility of deadlock when two threads with
64210** two or more btrees in common both try to lock all their btrees
64211** at the same instant.
64212*/
64213static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
64214  int i;
64215  int skipOk = 1;
64216  Btree *p;
64217  assert( sqlite3_mutex_held(db->mutex) );
64218  for(i=0; i<db->nDb; i++){
64219    p = db->aDb[i].pBt;
64220    if( p && p->sharable ){
64221      sqlite3BtreeEnter(p);
64222      skipOk = 0;
64223    }
64224  }
64225  db->noSharedCache = skipOk;
64226}
64227SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
64228  if( db->noSharedCache==0 ) btreeEnterAll(db);
64229}
64230static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){
64231  int i;
64232  Btree *p;
64233  assert( sqlite3_mutex_held(db->mutex) );
64234  for(i=0; i<db->nDb; i++){
64235    p = db->aDb[i].pBt;
64236    if( p ) sqlite3BtreeLeave(p);
64237  }
64238}
64239SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
64240  if( db->noSharedCache==0 ) btreeLeaveAll(db);
64241}
64242
64243#ifndef NDEBUG
64244/*
64245** Return true if the current thread holds the database connection
64246** mutex and all required BtShared mutexes.
64247**
64248** This routine is used inside assert() statements only.
64249*/
64250SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
64251  int i;
64252  if( !sqlite3_mutex_held(db->mutex) ){
64253    return 0;
64254  }
64255  for(i=0; i<db->nDb; i++){
64256    Btree *p;
64257    p = db->aDb[i].pBt;
64258    if( p && p->sharable &&
64259         (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
64260      return 0;
64261    }
64262  }
64263  return 1;
64264}
64265#endif /* NDEBUG */
64266
64267#ifndef NDEBUG
64268/*
64269** Return true if the correct mutexes are held for accessing the
64270** db->aDb[iDb].pSchema structure.  The mutexes required for schema
64271** access are:
64272**
64273**   (1) The mutex on db
64274**   (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
64275**
64276** If pSchema is not NULL, then iDb is computed from pSchema and
64277** db using sqlite3SchemaToIndex().
64278*/
64279SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
64280  Btree *p;
64281  assert( db!=0 );
64282  if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
64283  assert( iDb>=0 && iDb<db->nDb );
64284  if( !sqlite3_mutex_held(db->mutex) ) return 0;
64285  if( iDb==1 ) return 1;
64286  p = db->aDb[iDb].pBt;
64287  assert( p!=0 );
64288  return p->sharable==0 || p->locked==1;
64289}
64290#endif /* NDEBUG */
64291
64292#else /* SQLITE_THREADSAFE>0 above.  SQLITE_THREADSAFE==0 below */
64293/*
64294** The following are special cases for mutex enter routines for use
64295** in single threaded applications that use shared cache.  Except for
64296** these two routines, all mutex operations are no-ops in that case and
64297** are null #defines in btree.h.
64298**
64299** If shared cache is disabled, then all btree mutex routines, including
64300** the ones below, are no-ops and are null #defines in btree.h.
64301*/
64302
64303SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
64304  p->pBt->db = p->db;
64305}
64306SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
64307  int i;
64308  for(i=0; i<db->nDb; i++){
64309    Btree *p = db->aDb[i].pBt;
64310    if( p ){
64311      p->pBt->db = p->db;
64312    }
64313  }
64314}
64315#endif /* if SQLITE_THREADSAFE */
64316
64317#ifndef SQLITE_OMIT_INCRBLOB
64318/*
64319** Enter a mutex on a Btree given a cursor owned by that Btree.
64320**
64321** These entry points are used by incremental I/O only. Enter() is required
64322** any time OMIT_SHARED_CACHE is not defined, regardless of whether or not
64323** the build is threadsafe. Leave() is only required by threadsafe builds.
64324*/
64325SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
64326  sqlite3BtreeEnter(pCur->pBtree);
64327}
64328# if SQLITE_THREADSAFE
64329SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
64330  sqlite3BtreeLeave(pCur->pBtree);
64331}
64332# endif
64333#endif /* ifndef SQLITE_OMIT_INCRBLOB */
64334
64335#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
64336
64337/************** End of btmutex.c *********************************************/
64338/************** Begin file btree.c *******************************************/
64339/*
64340** 2004 April 6
64341**
64342** The author disclaims copyright to this source code.  In place of
64343** a legal notice, here is a blessing:
64344**
64345**    May you do good and not evil.
64346**    May you find forgiveness for yourself and forgive others.
64347**    May you share freely, never taking more than you give.
64348**
64349*************************************************************************
64350** This file implements an external (disk-based) database using BTrees.
64351** See the header comment on "btreeInt.h" for additional information.
64352** Including a description of file format and an overview of operation.
64353*/
64354/* #include "btreeInt.h" */
64355
64356/*
64357** The header string that appears at the beginning of every
64358** SQLite database.
64359*/
64360static const char zMagicHeader[] = SQLITE_FILE_HEADER;
64361
64362/*
64363** Set this global variable to 1 to enable tracing using the TRACE
64364** macro.
64365*/
64366#if 0
64367int sqlite3BtreeTrace=1;  /* True to enable tracing */
64368# define TRACE(X)  if(sqlite3BtreeTrace){printf X;fflush(stdout);}
64369#else
64370# define TRACE(X)
64371#endif
64372
64373/*
64374** Extract a 2-byte big-endian integer from an array of unsigned bytes.
64375** But if the value is zero, make it 65536.
64376**
64377** This routine is used to extract the "offset to cell content area" value
64378** from the header of a btree page.  If the page size is 65536 and the page
64379** is empty, the offset should be 65536, but the 2-byte value stores zero.
64380** This routine makes the necessary adjustment to 65536.
64381*/
64382#define get2byteNotZero(X)  (((((int)get2byte(X))-1)&0xffff)+1)
64383
64384/*
64385** Values passed as the 5th argument to allocateBtreePage()
64386*/
64387#define BTALLOC_ANY   0           /* Allocate any page */
64388#define BTALLOC_EXACT 1           /* Allocate exact page if possible */
64389#define BTALLOC_LE    2           /* Allocate any page <= the parameter */
64390
64391/*
64392** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
64393** defined, or 0 if it is. For example:
64394**
64395**   bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
64396*/
64397#ifndef SQLITE_OMIT_AUTOVACUUM
64398#define IfNotOmitAV(expr) (expr)
64399#else
64400#define IfNotOmitAV(expr) 0
64401#endif
64402
64403#ifndef SQLITE_OMIT_SHARED_CACHE
64404/*
64405** A list of BtShared objects that are eligible for participation
64406** in shared cache.  This variable has file scope during normal builds,
64407** but the test harness needs to access it so we make it global for
64408** test builds.
64409**
64410** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
64411*/
64412#ifdef SQLITE_TEST
64413SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
64414#else
64415static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
64416#endif
64417#endif /* SQLITE_OMIT_SHARED_CACHE */
64418
64419#ifndef SQLITE_OMIT_SHARED_CACHE
64420/*
64421** Enable or disable the shared pager and schema features.
64422**
64423** This routine has no effect on existing database connections.
64424** The shared cache setting effects only future calls to
64425** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
64426*/
64427SQLITE_API int sqlite3_enable_shared_cache(int enable){
64428  sqlite3GlobalConfig.sharedCacheEnabled = enable;
64429  return SQLITE_OK;
64430}
64431#endif
64432
64433
64434
64435#ifdef SQLITE_OMIT_SHARED_CACHE
64436  /*
64437  ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
64438  ** and clearAllSharedCacheTableLocks()
64439  ** manipulate entries in the BtShared.pLock linked list used to store
64440  ** shared-cache table level locks. If the library is compiled with the
64441  ** shared-cache feature disabled, then there is only ever one user
64442  ** of each BtShared structure and so this locking is not necessary.
64443  ** So define the lock related functions as no-ops.
64444  */
64445  #define querySharedCacheTableLock(a,b,c) SQLITE_OK
64446  #define setSharedCacheTableLock(a,b,c) SQLITE_OK
64447  #define clearAllSharedCacheTableLocks(a)
64448  #define downgradeAllSharedCacheTableLocks(a)
64449  #define hasSharedCacheTableLock(a,b,c,d) 1
64450  #define hasReadConflicts(a, b) 0
64451#endif
64452
64453/*
64454** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single
64455** (MemPage*) as an argument. The (MemPage*) must not be NULL.
64456**
64457** If SQLITE_DEBUG is not defined, then this macro is equivalent to
64458** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
64459** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
64460** with the page number and filename associated with the (MemPage*).
64461*/
64462#ifdef SQLITE_DEBUG
64463int corruptPageError(int lineno, MemPage *p){
64464  char *zMsg;
64465  sqlite3BeginBenignMalloc();
64466  zMsg = sqlite3_mprintf("database corruption page %d of %s",
64467      (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
64468  );
64469  sqlite3EndBenignMalloc();
64470  if( zMsg ){
64471    sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
64472  }
64473  sqlite3_free(zMsg);
64474  return SQLITE_CORRUPT_BKPT;
64475}
64476# define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage)
64477#else
64478# define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
64479#endif
64480
64481#ifndef SQLITE_OMIT_SHARED_CACHE
64482
64483#ifdef SQLITE_DEBUG
64484/*
64485**** This function is only used as part of an assert() statement. ***
64486**
64487** Check to see if pBtree holds the required locks to read or write to the
64488** table with root page iRoot.   Return 1 if it does and 0 if not.
64489**
64490** For example, when writing to a table with root-page iRoot via
64491** Btree connection pBtree:
64492**
64493**    assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
64494**
64495** When writing to an index that resides in a sharable database, the
64496** caller should have first obtained a lock specifying the root page of
64497** the corresponding table. This makes things a bit more complicated,
64498** as this module treats each table as a separate structure. To determine
64499** the table corresponding to the index being written, this
64500** function has to search through the database schema.
64501**
64502** Instead of a lock on the table/index rooted at page iRoot, the caller may
64503** hold a write-lock on the schema table (root page 1). This is also
64504** acceptable.
64505*/
64506static int hasSharedCacheTableLock(
64507  Btree *pBtree,         /* Handle that must hold lock */
64508  Pgno iRoot,            /* Root page of b-tree */
64509  int isIndex,           /* True if iRoot is the root of an index b-tree */
64510  int eLockType          /* Required lock type (READ_LOCK or WRITE_LOCK) */
64511){
64512  Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
64513  Pgno iTab = 0;
64514  BtLock *pLock;
64515
64516  /* If this database is not shareable, or if the client is reading
64517  ** and has the read-uncommitted flag set, then no lock is required.
64518  ** Return true immediately.
64519  */
64520  if( (pBtree->sharable==0)
64521   || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
64522  ){
64523    return 1;
64524  }
64525
64526  /* If the client is reading  or writing an index and the schema is
64527  ** not loaded, then it is too difficult to actually check to see if
64528  ** the correct locks are held.  So do not bother - just return true.
64529  ** This case does not come up very often anyhow.
64530  */
64531  if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
64532    return 1;
64533  }
64534
64535  /* Figure out the root-page that the lock should be held on. For table
64536  ** b-trees, this is just the root page of the b-tree being read or
64537  ** written. For index b-trees, it is the root page of the associated
64538  ** table.  */
64539  if( isIndex ){
64540    HashElem *p;
64541    int bSeen = 0;
64542    for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
64543      Index *pIdx = (Index *)sqliteHashData(p);
64544      if( pIdx->tnum==(int)iRoot ){
64545        if( bSeen ){
64546          /* Two or more indexes share the same root page.  There must
64547          ** be imposter tables.  So just return true.  The assert is not
64548          ** useful in that case. */
64549          return 1;
64550        }
64551        iTab = pIdx->pTable->tnum;
64552        bSeen = 1;
64553      }
64554    }
64555  }else{
64556    iTab = iRoot;
64557  }
64558
64559  /* Search for the required lock. Either a write-lock on root-page iTab, a
64560  ** write-lock on the schema table, or (if the client is reading) a
64561  ** read-lock on iTab will suffice. Return 1 if any of these are found.  */
64562  for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
64563    if( pLock->pBtree==pBtree
64564     && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
64565     && pLock->eLock>=eLockType
64566    ){
64567      return 1;
64568    }
64569  }
64570
64571  /* Failed to find the required lock. */
64572  return 0;
64573}
64574#endif /* SQLITE_DEBUG */
64575
64576#ifdef SQLITE_DEBUG
64577/*
64578**** This function may be used as part of assert() statements only. ****
64579**
64580** Return true if it would be illegal for pBtree to write into the
64581** table or index rooted at iRoot because other shared connections are
64582** simultaneously reading that same table or index.
64583**
64584** It is illegal for pBtree to write if some other Btree object that
64585** shares the same BtShared object is currently reading or writing
64586** the iRoot table.  Except, if the other Btree object has the
64587** read-uncommitted flag set, then it is OK for the other object to
64588** have a read cursor.
64589**
64590** For example, before writing to any part of the table or index
64591** rooted at page iRoot, one should call:
64592**
64593**    assert( !hasReadConflicts(pBtree, iRoot) );
64594*/
64595static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
64596  BtCursor *p;
64597  for(p=pBtree->pBt->pCursor; p; p=p->pNext){
64598    if( p->pgnoRoot==iRoot
64599     && p->pBtree!=pBtree
64600     && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
64601    ){
64602      return 1;
64603    }
64604  }
64605  return 0;
64606}
64607#endif    /* #ifdef SQLITE_DEBUG */
64608
64609/*
64610** Query to see if Btree handle p may obtain a lock of type eLock
64611** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
64612** SQLITE_OK if the lock may be obtained (by calling
64613** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
64614*/
64615static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
64616  BtShared *pBt = p->pBt;
64617  BtLock *pIter;
64618
64619  assert( sqlite3BtreeHoldsMutex(p) );
64620  assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
64621  assert( p->db!=0 );
64622  assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
64623
64624  /* If requesting a write-lock, then the Btree must have an open write
64625  ** transaction on this file. And, obviously, for this to be so there
64626  ** must be an open write transaction on the file itself.
64627  */
64628  assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
64629  assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
64630
64631  /* This routine is a no-op if the shared-cache is not enabled */
64632  if( !p->sharable ){
64633    return SQLITE_OK;
64634  }
64635
64636  /* If some other connection is holding an exclusive lock, the
64637  ** requested lock may not be obtained.
64638  */
64639  if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
64640    sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
64641    return SQLITE_LOCKED_SHAREDCACHE;
64642  }
64643
64644  for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
64645    /* The condition (pIter->eLock!=eLock) in the following if(...)
64646    ** statement is a simplification of:
64647    **
64648    **   (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
64649    **
64650    ** since we know that if eLock==WRITE_LOCK, then no other connection
64651    ** may hold a WRITE_LOCK on any table in this file (since there can
64652    ** only be a single writer).
64653    */
64654    assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
64655    assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
64656    if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
64657      sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
64658      if( eLock==WRITE_LOCK ){
64659        assert( p==pBt->pWriter );
64660        pBt->btsFlags |= BTS_PENDING;
64661      }
64662      return SQLITE_LOCKED_SHAREDCACHE;
64663    }
64664  }
64665  return SQLITE_OK;
64666}
64667#endif /* !SQLITE_OMIT_SHARED_CACHE */
64668
64669#ifndef SQLITE_OMIT_SHARED_CACHE
64670/*
64671** Add a lock on the table with root-page iTable to the shared-btree used
64672** by Btree handle p. Parameter eLock must be either READ_LOCK or
64673** WRITE_LOCK.
64674**
64675** This function assumes the following:
64676**
64677**   (a) The specified Btree object p is connected to a sharable
64678**       database (one with the BtShared.sharable flag set), and
64679**
64680**   (b) No other Btree objects hold a lock that conflicts
64681**       with the requested lock (i.e. querySharedCacheTableLock() has
64682**       already been called and returned SQLITE_OK).
64683**
64684** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
64685** is returned if a malloc attempt fails.
64686*/
64687static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
64688  BtShared *pBt = p->pBt;
64689  BtLock *pLock = 0;
64690  BtLock *pIter;
64691
64692  assert( sqlite3BtreeHoldsMutex(p) );
64693  assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
64694  assert( p->db!=0 );
64695
64696  /* A connection with the read-uncommitted flag set will never try to
64697  ** obtain a read-lock using this function. The only read-lock obtained
64698  ** by a connection in read-uncommitted mode is on the sqlite_master
64699  ** table, and that lock is obtained in BtreeBeginTrans().  */
64700  assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
64701
64702  /* This function should only be called on a sharable b-tree after it
64703  ** has been determined that no other b-tree holds a conflicting lock.  */
64704  assert( p->sharable );
64705  assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
64706
64707  /* First search the list for an existing lock on this table. */
64708  for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
64709    if( pIter->iTable==iTable && pIter->pBtree==p ){
64710      pLock = pIter;
64711      break;
64712    }
64713  }
64714
64715  /* If the above search did not find a BtLock struct associating Btree p
64716  ** with table iTable, allocate one and link it into the list.
64717  */
64718  if( !pLock ){
64719    pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
64720    if( !pLock ){
64721      return SQLITE_NOMEM_BKPT;
64722    }
64723    pLock->iTable = iTable;
64724    pLock->pBtree = p;
64725    pLock->pNext = pBt->pLock;
64726    pBt->pLock = pLock;
64727  }
64728
64729  /* Set the BtLock.eLock variable to the maximum of the current lock
64730  ** and the requested lock. This means if a write-lock was already held
64731  ** and a read-lock requested, we don't incorrectly downgrade the lock.
64732  */
64733  assert( WRITE_LOCK>READ_LOCK );
64734  if( eLock>pLock->eLock ){
64735    pLock->eLock = eLock;
64736  }
64737
64738  return SQLITE_OK;
64739}
64740#endif /* !SQLITE_OMIT_SHARED_CACHE */
64741
64742#ifndef SQLITE_OMIT_SHARED_CACHE
64743/*
64744** Release all the table locks (locks obtained via calls to
64745** the setSharedCacheTableLock() procedure) held by Btree object p.
64746**
64747** This function assumes that Btree p has an open read or write
64748** transaction. If it does not, then the BTS_PENDING flag
64749** may be incorrectly cleared.
64750*/
64751static void clearAllSharedCacheTableLocks(Btree *p){
64752  BtShared *pBt = p->pBt;
64753  BtLock **ppIter = &pBt->pLock;
64754
64755  assert( sqlite3BtreeHoldsMutex(p) );
64756  assert( p->sharable || 0==*ppIter );
64757  assert( p->inTrans>0 );
64758
64759  while( *ppIter ){
64760    BtLock *pLock = *ppIter;
64761    assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
64762    assert( pLock->pBtree->inTrans>=pLock->eLock );
64763    if( pLock->pBtree==p ){
64764      *ppIter = pLock->pNext;
64765      assert( pLock->iTable!=1 || pLock==&p->lock );
64766      if( pLock->iTable!=1 ){
64767        sqlite3_free(pLock);
64768      }
64769    }else{
64770      ppIter = &pLock->pNext;
64771    }
64772  }
64773
64774  assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
64775  if( pBt->pWriter==p ){
64776    pBt->pWriter = 0;
64777    pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
64778  }else if( pBt->nTransaction==2 ){
64779    /* This function is called when Btree p is concluding its
64780    ** transaction. If there currently exists a writer, and p is not
64781    ** that writer, then the number of locks held by connections other
64782    ** than the writer must be about to drop to zero. In this case
64783    ** set the BTS_PENDING flag to 0.
64784    **
64785    ** If there is not currently a writer, then BTS_PENDING must
64786    ** be zero already. So this next line is harmless in that case.
64787    */
64788    pBt->btsFlags &= ~BTS_PENDING;
64789  }
64790}
64791
64792/*
64793** This function changes all write-locks held by Btree p into read-locks.
64794*/
64795static void downgradeAllSharedCacheTableLocks(Btree *p){
64796  BtShared *pBt = p->pBt;
64797  if( pBt->pWriter==p ){
64798    BtLock *pLock;
64799    pBt->pWriter = 0;
64800    pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
64801    for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
64802      assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
64803      pLock->eLock = READ_LOCK;
64804    }
64805  }
64806}
64807
64808#endif /* SQLITE_OMIT_SHARED_CACHE */
64809
64810static void releasePage(MemPage *pPage);         /* Forward reference */
64811static void releasePageOne(MemPage *pPage);      /* Forward reference */
64812static void releasePageNotNull(MemPage *pPage);  /* Forward reference */
64813
64814/*
64815***** This routine is used inside of assert() only ****
64816**
64817** Verify that the cursor holds the mutex on its BtShared
64818*/
64819#ifdef SQLITE_DEBUG
64820static int cursorHoldsMutex(BtCursor *p){
64821  return sqlite3_mutex_held(p->pBt->mutex);
64822}
64823
64824/* Verify that the cursor and the BtShared agree about what is the current
64825** database connetion. This is important in shared-cache mode. If the database
64826** connection pointers get out-of-sync, it is possible for routines like
64827** btreeInitPage() to reference an stale connection pointer that references a
64828** a connection that has already closed.  This routine is used inside assert()
64829** statements only and for the purpose of double-checking that the btree code
64830** does keep the database connection pointers up-to-date.
64831*/
64832static int cursorOwnsBtShared(BtCursor *p){
64833  assert( cursorHoldsMutex(p) );
64834  return (p->pBtree->db==p->pBt->db);
64835}
64836#endif
64837
64838/*
64839** Invalidate the overflow cache of the cursor passed as the first argument.
64840** on the shared btree structure pBt.
64841*/
64842#define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
64843
64844/*
64845** Invalidate the overflow page-list cache for all cursors opened
64846** on the shared btree structure pBt.
64847*/
64848static void invalidateAllOverflowCache(BtShared *pBt){
64849  BtCursor *p;
64850  assert( sqlite3_mutex_held(pBt->mutex) );
64851  for(p=pBt->pCursor; p; p=p->pNext){
64852    invalidateOverflowCache(p);
64853  }
64854}
64855
64856#ifndef SQLITE_OMIT_INCRBLOB
64857/*
64858** This function is called before modifying the contents of a table
64859** to invalidate any incrblob cursors that are open on the
64860** row or one of the rows being modified.
64861**
64862** If argument isClearTable is true, then the entire contents of the
64863** table is about to be deleted. In this case invalidate all incrblob
64864** cursors open on any row within the table with root-page pgnoRoot.
64865**
64866** Otherwise, if argument isClearTable is false, then the row with
64867** rowid iRow is being replaced or deleted. In this case invalidate
64868** only those incrblob cursors open on that specific row.
64869*/
64870static void invalidateIncrblobCursors(
64871  Btree *pBtree,          /* The database file to check */
64872  Pgno pgnoRoot,          /* The table that might be changing */
64873  i64 iRow,               /* The rowid that might be changing */
64874  int isClearTable        /* True if all rows are being deleted */
64875){
64876  BtCursor *p;
64877  if( pBtree->hasIncrblobCur==0 ) return;
64878  assert( sqlite3BtreeHoldsMutex(pBtree) );
64879  pBtree->hasIncrblobCur = 0;
64880  for(p=pBtree->pBt->pCursor; p; p=p->pNext){
64881    if( (p->curFlags & BTCF_Incrblob)!=0 ){
64882      pBtree->hasIncrblobCur = 1;
64883      if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
64884        p->eState = CURSOR_INVALID;
64885      }
64886    }
64887  }
64888}
64889
64890#else
64891  /* Stub function when INCRBLOB is omitted */
64892  #define invalidateIncrblobCursors(w,x,y,z)
64893#endif /* SQLITE_OMIT_INCRBLOB */
64894
64895/*
64896** Set bit pgno of the BtShared.pHasContent bitvec. This is called
64897** when a page that previously contained data becomes a free-list leaf
64898** page.
64899**
64900** The BtShared.pHasContent bitvec exists to work around an obscure
64901** bug caused by the interaction of two useful IO optimizations surrounding
64902** free-list leaf pages:
64903**
64904**   1) When all data is deleted from a page and the page becomes
64905**      a free-list leaf page, the page is not written to the database
64906**      (as free-list leaf pages contain no meaningful data). Sometimes
64907**      such a page is not even journalled (as it will not be modified,
64908**      why bother journalling it?).
64909**
64910**   2) When a free-list leaf page is reused, its content is not read
64911**      from the database or written to the journal file (why should it
64912**      be, if it is not at all meaningful?).
64913**
64914** By themselves, these optimizations work fine and provide a handy
64915** performance boost to bulk delete or insert operations. However, if
64916** a page is moved to the free-list and then reused within the same
64917** transaction, a problem comes up. If the page is not journalled when
64918** it is moved to the free-list and it is also not journalled when it
64919** is extracted from the free-list and reused, then the original data
64920** may be lost. In the event of a rollback, it may not be possible
64921** to restore the database to its original configuration.
64922**
64923** The solution is the BtShared.pHasContent bitvec. Whenever a page is
64924** moved to become a free-list leaf page, the corresponding bit is
64925** set in the bitvec. Whenever a leaf page is extracted from the free-list,
64926** optimization 2 above is omitted if the corresponding bit is already
64927** set in BtShared.pHasContent. The contents of the bitvec are cleared
64928** at the end of every transaction.
64929*/
64930static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
64931  int rc = SQLITE_OK;
64932  if( !pBt->pHasContent ){
64933    assert( pgno<=pBt->nPage );
64934    pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
64935    if( !pBt->pHasContent ){
64936      rc = SQLITE_NOMEM_BKPT;
64937    }
64938  }
64939  if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
64940    rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
64941  }
64942  return rc;
64943}
64944
64945/*
64946** Query the BtShared.pHasContent vector.
64947**
64948** This function is called when a free-list leaf page is removed from the
64949** free-list for reuse. It returns false if it is safe to retrieve the
64950** page from the pager layer with the 'no-content' flag set. True otherwise.
64951*/
64952static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
64953  Bitvec *p = pBt->pHasContent;
64954  return p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTestNotNull(p, pgno));
64955}
64956
64957/*
64958** Clear (destroy) the BtShared.pHasContent bitvec. This should be
64959** invoked at the conclusion of each write-transaction.
64960*/
64961static void btreeClearHasContent(BtShared *pBt){
64962  sqlite3BitvecDestroy(pBt->pHasContent);
64963  pBt->pHasContent = 0;
64964}
64965
64966/*
64967** Release all of the apPage[] pages for a cursor.
64968*/
64969static void btreeReleaseAllCursorPages(BtCursor *pCur){
64970  int i;
64971  if( pCur->iPage>=0 ){
64972    for(i=0; i<pCur->iPage; i++){
64973      releasePageNotNull(pCur->apPage[i]);
64974    }
64975    releasePageNotNull(pCur->pPage);
64976    pCur->iPage = -1;
64977  }
64978}
64979
64980/*
64981** The cursor passed as the only argument must point to a valid entry
64982** when this function is called (i.e. have eState==CURSOR_VALID). This
64983** function saves the current cursor key in variables pCur->nKey and
64984** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
64985** code otherwise.
64986**
64987** If the cursor is open on an intkey table, then the integer key
64988** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
64989** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
64990** set to point to a malloced buffer pCur->nKey bytes in size containing
64991** the key.
64992*/
64993static int saveCursorKey(BtCursor *pCur){
64994  int rc = SQLITE_OK;
64995  assert( CURSOR_VALID==pCur->eState );
64996  assert( 0==pCur->pKey );
64997  assert( cursorHoldsMutex(pCur) );
64998
64999  if( pCur->curIntKey ){
65000    /* Only the rowid is required for a table btree */
65001    pCur->nKey = sqlite3BtreeIntegerKey(pCur);
65002  }else{
65003    /* For an index btree, save the complete key content. It is possible
65004    ** that the current key is corrupt. In that case, it is possible that
65005    ** the sqlite3VdbeRecordUnpack() function may overread the buffer by
65006    ** up to the size of 1 varint plus 1 8-byte value when the cursor
65007    ** position is restored. Hence the 17 bytes of padding allocated
65008    ** below. */
65009    void *pKey;
65010    pCur->nKey = sqlite3BtreePayloadSize(pCur);
65011    pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
65012    if( pKey ){
65013      rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
65014      if( rc==SQLITE_OK ){
65015        memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
65016        pCur->pKey = pKey;
65017      }else{
65018        sqlite3_free(pKey);
65019      }
65020    }else{
65021      rc = SQLITE_NOMEM_BKPT;
65022    }
65023  }
65024  assert( !pCur->curIntKey || !pCur->pKey );
65025  return rc;
65026}
65027
65028/*
65029** Save the current cursor position in the variables BtCursor.nKey
65030** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
65031**
65032** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
65033** prior to calling this routine.
65034*/
65035static int saveCursorPosition(BtCursor *pCur){
65036  int rc;
65037
65038  assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
65039  assert( 0==pCur->pKey );
65040  assert( cursorHoldsMutex(pCur) );
65041
65042  if( pCur->curFlags & BTCF_Pinned ){
65043    return SQLITE_CONSTRAINT_PINNED;
65044  }
65045  if( pCur->eState==CURSOR_SKIPNEXT ){
65046    pCur->eState = CURSOR_VALID;
65047  }else{
65048    pCur->skipNext = 0;
65049  }
65050
65051  rc = saveCursorKey(pCur);
65052  if( rc==SQLITE_OK ){
65053    btreeReleaseAllCursorPages(pCur);
65054    pCur->eState = CURSOR_REQUIRESEEK;
65055  }
65056
65057  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
65058  return rc;
65059}
65060
65061/* Forward reference */
65062static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
65063
65064/*
65065** Save the positions of all cursors (except pExcept) that are open on
65066** the table with root-page iRoot.  "Saving the cursor position" means that
65067** the location in the btree is remembered in such a way that it can be
65068** moved back to the same spot after the btree has been modified.  This
65069** routine is called just before cursor pExcept is used to modify the
65070** table, for example in BtreeDelete() or BtreeInsert().
65071**
65072** If there are two or more cursors on the same btree, then all such
65073** cursors should have their BTCF_Multiple flag set.  The btreeCursor()
65074** routine enforces that rule.  This routine only needs to be called in
65075** the uncommon case when pExpect has the BTCF_Multiple flag set.
65076**
65077** If pExpect!=NULL and if no other cursors are found on the same root-page,
65078** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
65079** pointless call to this routine.
65080**
65081** Implementation note:  This routine merely checks to see if any cursors
65082** need to be saved.  It calls out to saveCursorsOnList() in the (unusual)
65083** event that cursors are in need to being saved.
65084*/
65085static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
65086  BtCursor *p;
65087  assert( sqlite3_mutex_held(pBt->mutex) );
65088  assert( pExcept==0 || pExcept->pBt==pBt );
65089  for(p=pBt->pCursor; p; p=p->pNext){
65090    if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
65091  }
65092  if( p ) return saveCursorsOnList(p, iRoot, pExcept);
65093  if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
65094  return SQLITE_OK;
65095}
65096
65097/* This helper routine to saveAllCursors does the actual work of saving
65098** the cursors if and when a cursor is found that actually requires saving.
65099** The common case is that no cursors need to be saved, so this routine is
65100** broken out from its caller to avoid unnecessary stack pointer movement.
65101*/
65102static int SQLITE_NOINLINE saveCursorsOnList(
65103  BtCursor *p,         /* The first cursor that needs saving */
65104  Pgno iRoot,          /* Only save cursor with this iRoot. Save all if zero */
65105  BtCursor *pExcept    /* Do not save this cursor */
65106){
65107  do{
65108    if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
65109      if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
65110        int rc = saveCursorPosition(p);
65111        if( SQLITE_OK!=rc ){
65112          return rc;
65113        }
65114      }else{
65115        testcase( p->iPage>=0 );
65116        btreeReleaseAllCursorPages(p);
65117      }
65118    }
65119    p = p->pNext;
65120  }while( p );
65121  return SQLITE_OK;
65122}
65123
65124/*
65125** Clear the current cursor position.
65126*/
65127SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
65128  assert( cursorHoldsMutex(pCur) );
65129  sqlite3_free(pCur->pKey);
65130  pCur->pKey = 0;
65131  pCur->eState = CURSOR_INVALID;
65132}
65133
65134/*
65135** In this version of BtreeMoveto, pKey is a packed index record
65136** such as is generated by the OP_MakeRecord opcode.  Unpack the
65137** record and then call BtreeMovetoUnpacked() to do the work.
65138*/
65139static int btreeMoveto(
65140  BtCursor *pCur,     /* Cursor open on the btree to be searched */
65141  const void *pKey,   /* Packed key if the btree is an index */
65142  i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
65143  int bias,           /* Bias search to the high end */
65144  int *pRes           /* Write search results here */
65145){
65146  int rc;                    /* Status code */
65147  UnpackedRecord *pIdxKey;   /* Unpacked index key */
65148
65149  if( pKey ){
65150    KeyInfo *pKeyInfo = pCur->pKeyInfo;
65151    assert( nKey==(i64)(int)nKey );
65152    pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
65153    if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT;
65154    sqlite3VdbeRecordUnpack(pKeyInfo, (int)nKey, pKey, pIdxKey);
65155    if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
65156      rc = SQLITE_CORRUPT_BKPT;
65157      goto moveto_done;
65158    }
65159  }else{
65160    pIdxKey = 0;
65161  }
65162  rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
65163moveto_done:
65164  if( pIdxKey ){
65165    sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
65166  }
65167  return rc;
65168}
65169
65170/*
65171** Restore the cursor to the position it was in (or as close to as possible)
65172** when saveCursorPosition() was called. Note that this call deletes the
65173** saved position info stored by saveCursorPosition(), so there can be
65174** at most one effective restoreCursorPosition() call after each
65175** saveCursorPosition().
65176*/
65177static int btreeRestoreCursorPosition(BtCursor *pCur){
65178  int rc;
65179  int skipNext = 0;
65180  assert( cursorOwnsBtShared(pCur) );
65181  assert( pCur->eState>=CURSOR_REQUIRESEEK );
65182  if( pCur->eState==CURSOR_FAULT ){
65183    return pCur->skipNext;
65184  }
65185  pCur->eState = CURSOR_INVALID;
65186  if( sqlite3FaultSim(410) ){
65187    rc = SQLITE_IOERR;
65188  }else{
65189    rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
65190  }
65191  if( rc==SQLITE_OK ){
65192    sqlite3_free(pCur->pKey);
65193    pCur->pKey = 0;
65194    assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
65195    if( skipNext ) pCur->skipNext = skipNext;
65196    if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
65197      pCur->eState = CURSOR_SKIPNEXT;
65198    }
65199  }
65200  return rc;
65201}
65202
65203#define restoreCursorPosition(p) \
65204  (p->eState>=CURSOR_REQUIRESEEK ? \
65205         btreeRestoreCursorPosition(p) : \
65206         SQLITE_OK)
65207
65208/*
65209** Determine whether or not a cursor has moved from the position where
65210** it was last placed, or has been invalidated for any other reason.
65211** Cursors can move when the row they are pointing at is deleted out
65212** from under them, for example.  Cursor might also move if a btree
65213** is rebalanced.
65214**
65215** Calling this routine with a NULL cursor pointer returns false.
65216**
65217** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
65218** back to where it ought to be if this routine returns true.
65219*/
65220SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
65221  assert( EIGHT_BYTE_ALIGNMENT(pCur)
65222       || pCur==sqlite3BtreeFakeValidCursor() );
65223  assert( offsetof(BtCursor, eState)==0 );
65224  assert( sizeof(pCur->eState)==1 );
65225  return CURSOR_VALID != *(u8*)pCur;
65226}
65227
65228/*
65229** Return a pointer to a fake BtCursor object that will always answer
65230** false to the sqlite3BtreeCursorHasMoved() routine above.  The fake
65231** cursor returned must not be used with any other Btree interface.
65232*/
65233SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void){
65234  static u8 fakeCursor = CURSOR_VALID;
65235  assert( offsetof(BtCursor, eState)==0 );
65236  return (BtCursor*)&fakeCursor;
65237}
65238
65239/*
65240** This routine restores a cursor back to its original position after it
65241** has been moved by some outside activity (such as a btree rebalance or
65242** a row having been deleted out from under the cursor).
65243**
65244** On success, the *pDifferentRow parameter is false if the cursor is left
65245** pointing at exactly the same row.  *pDifferntRow is the row the cursor
65246** was pointing to has been deleted, forcing the cursor to point to some
65247** nearby row.
65248**
65249** This routine should only be called for a cursor that just returned
65250** TRUE from sqlite3BtreeCursorHasMoved().
65251*/
65252SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
65253  int rc;
65254
65255  assert( pCur!=0 );
65256  assert( pCur->eState!=CURSOR_VALID );
65257  rc = restoreCursorPosition(pCur);
65258  if( rc ){
65259    *pDifferentRow = 1;
65260    return rc;
65261  }
65262  if( pCur->eState!=CURSOR_VALID ){
65263    *pDifferentRow = 1;
65264  }else{
65265    *pDifferentRow = 0;
65266  }
65267  return SQLITE_OK;
65268}
65269
65270#ifdef SQLITE_ENABLE_CURSOR_HINTS
65271/*
65272** Provide hints to the cursor.  The particular hint given (and the type
65273** and number of the varargs parameters) is determined by the eHintType
65274** parameter.  See the definitions of the BTREE_HINT_* macros for details.
65275*/
65276SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
65277  /* Used only by system that substitute their own storage engine */
65278}
65279#endif
65280
65281/*
65282** Provide flag hints to the cursor.
65283*/
65284SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
65285  assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 );
65286  pCur->hints = x;
65287}
65288
65289
65290#ifndef SQLITE_OMIT_AUTOVACUUM
65291/*
65292** Given a page number of a regular database page, return the page
65293** number for the pointer-map page that contains the entry for the
65294** input page number.
65295**
65296** Return 0 (not a valid page) for pgno==1 since there is
65297** no pointer map associated with page 1.  The integrity_check logic
65298** requires that ptrmapPageno(*,1)!=1.
65299*/
65300static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
65301  int nPagesPerMapPage;
65302  Pgno iPtrMap, ret;
65303  assert( sqlite3_mutex_held(pBt->mutex) );
65304  if( pgno<2 ) return 0;
65305  nPagesPerMapPage = (pBt->usableSize/5)+1;
65306  iPtrMap = (pgno-2)/nPagesPerMapPage;
65307  ret = (iPtrMap*nPagesPerMapPage) + 2;
65308  if( ret==PENDING_BYTE_PAGE(pBt) ){
65309    ret++;
65310  }
65311  return ret;
65312}
65313
65314/*
65315** Write an entry into the pointer map.
65316**
65317** This routine updates the pointer map entry for page number 'key'
65318** so that it maps to type 'eType' and parent page number 'pgno'.
65319**
65320** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
65321** a no-op.  If an error occurs, the appropriate error code is written
65322** into *pRC.
65323*/
65324static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
65325  DbPage *pDbPage;  /* The pointer map page */
65326  u8 *pPtrmap;      /* The pointer map data */
65327  Pgno iPtrmap;     /* The pointer map page number */
65328  int offset;       /* Offset in pointer map page */
65329  int rc;           /* Return code from subfunctions */
65330
65331  if( *pRC ) return;
65332
65333  assert( sqlite3_mutex_held(pBt->mutex) );
65334  /* The master-journal page number must never be used as a pointer map page */
65335  assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
65336
65337  assert( pBt->autoVacuum );
65338  if( key==0 ){
65339    *pRC = SQLITE_CORRUPT_BKPT;
65340    return;
65341  }
65342  iPtrmap = PTRMAP_PAGENO(pBt, key);
65343  rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
65344  if( rc!=SQLITE_OK ){
65345    *pRC = rc;
65346    return;
65347  }
65348  if( ((char*)sqlite3PagerGetExtra(pDbPage))[0]!=0 ){
65349    /* The first byte of the extra data is the MemPage.isInit byte.
65350    ** If that byte is set, it means this page is also being used
65351    ** as a btree page. */
65352    *pRC = SQLITE_CORRUPT_BKPT;
65353    goto ptrmap_exit;
65354  }
65355  offset = PTRMAP_PTROFFSET(iPtrmap, key);
65356  if( offset<0 ){
65357    *pRC = SQLITE_CORRUPT_BKPT;
65358    goto ptrmap_exit;
65359  }
65360  assert( offset <= (int)pBt->usableSize-5 );
65361  pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
65362
65363  if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
65364    TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
65365    *pRC= rc = sqlite3PagerWrite(pDbPage);
65366    if( rc==SQLITE_OK ){
65367      pPtrmap[offset] = eType;
65368      put4byte(&pPtrmap[offset+1], parent);
65369    }
65370  }
65371
65372ptrmap_exit:
65373  sqlite3PagerUnref(pDbPage);
65374}
65375
65376/*
65377** Read an entry from the pointer map.
65378**
65379** This routine retrieves the pointer map entry for page 'key', writing
65380** the type and parent page number to *pEType and *pPgno respectively.
65381** An error code is returned if something goes wrong, otherwise SQLITE_OK.
65382*/
65383static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
65384  DbPage *pDbPage;   /* The pointer map page */
65385  int iPtrmap;       /* Pointer map page index */
65386  u8 *pPtrmap;       /* Pointer map page data */
65387  int offset;        /* Offset of entry in pointer map */
65388  int rc;
65389
65390  assert( sqlite3_mutex_held(pBt->mutex) );
65391
65392  iPtrmap = PTRMAP_PAGENO(pBt, key);
65393  rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
65394  if( rc!=0 ){
65395    return rc;
65396  }
65397  pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
65398
65399  offset = PTRMAP_PTROFFSET(iPtrmap, key);
65400  if( offset<0 ){
65401    sqlite3PagerUnref(pDbPage);
65402    return SQLITE_CORRUPT_BKPT;
65403  }
65404  assert( offset <= (int)pBt->usableSize-5 );
65405  assert( pEType!=0 );
65406  *pEType = pPtrmap[offset];
65407  if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
65408
65409  sqlite3PagerUnref(pDbPage);
65410  if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap);
65411  return SQLITE_OK;
65412}
65413
65414#else /* if defined SQLITE_OMIT_AUTOVACUUM */
65415  #define ptrmapPut(w,x,y,z,rc)
65416  #define ptrmapGet(w,x,y,z) SQLITE_OK
65417  #define ptrmapPutOvflPtr(x, y, z, rc)
65418#endif
65419
65420/*
65421** Given a btree page and a cell index (0 means the first cell on
65422** the page, 1 means the second cell, and so forth) return a pointer
65423** to the cell content.
65424**
65425** findCellPastPtr() does the same except it skips past the initial
65426** 4-byte child pointer found on interior pages, if there is one.
65427**
65428** This routine works only for pages that do not contain overflow cells.
65429*/
65430#define findCell(P,I) \
65431  ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
65432#define findCellPastPtr(P,I) \
65433  ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
65434
65435
65436/*
65437** This is common tail processing for btreeParseCellPtr() and
65438** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
65439** on a single B-tree page.  Make necessary adjustments to the CellInfo
65440** structure.
65441*/
65442static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
65443  MemPage *pPage,         /* Page containing the cell */
65444  u8 *pCell,              /* Pointer to the cell text. */
65445  CellInfo *pInfo         /* Fill in this structure */
65446){
65447  /* If the payload will not fit completely on the local page, we have
65448  ** to decide how much to store locally and how much to spill onto
65449  ** overflow pages.  The strategy is to minimize the amount of unused
65450  ** space on overflow pages while keeping the amount of local storage
65451  ** in between minLocal and maxLocal.
65452  **
65453  ** Warning:  changing the way overflow payload is distributed in any
65454  ** way will result in an incompatible file format.
65455  */
65456  int minLocal;  /* Minimum amount of payload held locally */
65457  int maxLocal;  /* Maximum amount of payload held locally */
65458  int surplus;   /* Overflow payload available for local storage */
65459
65460  minLocal = pPage->minLocal;
65461  maxLocal = pPage->maxLocal;
65462  surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
65463  testcase( surplus==maxLocal );
65464  testcase( surplus==maxLocal+1 );
65465  if( surplus <= maxLocal ){
65466    pInfo->nLocal = (u16)surplus;
65467  }else{
65468    pInfo->nLocal = (u16)minLocal;
65469  }
65470  pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
65471}
65472
65473/*
65474** The following routines are implementations of the MemPage.xParseCell()
65475** method.
65476**
65477** Parse a cell content block and fill in the CellInfo structure.
65478**
65479** btreeParseCellPtr()        =>   table btree leaf nodes
65480** btreeParseCellNoPayload()  =>   table btree internal nodes
65481** btreeParseCellPtrIndex()   =>   index btree nodes
65482**
65483** There is also a wrapper function btreeParseCell() that works for
65484** all MemPage types and that references the cell by index rather than
65485** by pointer.
65486*/
65487static void btreeParseCellPtrNoPayload(
65488  MemPage *pPage,         /* Page containing the cell */
65489  u8 *pCell,              /* Pointer to the cell text. */
65490  CellInfo *pInfo         /* Fill in this structure */
65491){
65492  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
65493  assert( pPage->leaf==0 );
65494  assert( pPage->childPtrSize==4 );
65495#ifndef SQLITE_DEBUG
65496  UNUSED_PARAMETER(pPage);
65497#endif
65498  pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
65499  pInfo->nPayload = 0;
65500  pInfo->nLocal = 0;
65501  pInfo->pPayload = 0;
65502  return;
65503}
65504static void btreeParseCellPtr(
65505  MemPage *pPage,         /* Page containing the cell */
65506  u8 *pCell,              /* Pointer to the cell text. */
65507  CellInfo *pInfo         /* Fill in this structure */
65508){
65509  u8 *pIter;              /* For scanning through pCell */
65510  u32 nPayload;           /* Number of bytes of cell payload */
65511  u64 iKey;               /* Extracted Key value */
65512
65513  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
65514  assert( pPage->leaf==0 || pPage->leaf==1 );
65515  assert( pPage->intKeyLeaf );
65516  assert( pPage->childPtrSize==0 );
65517  pIter = pCell;
65518
65519  /* The next block of code is equivalent to:
65520  **
65521  **     pIter += getVarint32(pIter, nPayload);
65522  **
65523  ** The code is inlined to avoid a function call.
65524  */
65525  nPayload = *pIter;
65526  if( nPayload>=0x80 ){
65527    u8 *pEnd = &pIter[8];
65528    nPayload &= 0x7f;
65529    do{
65530      nPayload = (nPayload<<7) | (*++pIter & 0x7f);
65531    }while( (*pIter)>=0x80 && pIter<pEnd );
65532  }
65533  pIter++;
65534
65535  /* The next block of code is equivalent to:
65536  **
65537  **     pIter += getVarint(pIter, (u64*)&pInfo->nKey);
65538  **
65539  ** The code is inlined to avoid a function call.
65540  */
65541  iKey = *pIter;
65542  if( iKey>=0x80 ){
65543    u8 *pEnd = &pIter[7];
65544    iKey &= 0x7f;
65545    while(1){
65546      iKey = (iKey<<7) | (*++pIter & 0x7f);
65547      if( (*pIter)<0x80 ) break;
65548      if( pIter>=pEnd ){
65549        iKey = (iKey<<8) | *++pIter;
65550        break;
65551      }
65552    }
65553  }
65554  pIter++;
65555
65556  pInfo->nKey = *(i64*)&iKey;
65557  pInfo->nPayload = nPayload;
65558  pInfo->pPayload = pIter;
65559  testcase( nPayload==pPage->maxLocal );
65560  testcase( nPayload==pPage->maxLocal+1 );
65561  if( nPayload<=pPage->maxLocal ){
65562    /* This is the (easy) common case where the entire payload fits
65563    ** on the local page.  No overflow is required.
65564    */
65565    pInfo->nSize = nPayload + (u16)(pIter - pCell);
65566    if( pInfo->nSize<4 ) pInfo->nSize = 4;
65567    pInfo->nLocal = (u16)nPayload;
65568  }else{
65569    btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
65570  }
65571}
65572static void btreeParseCellPtrIndex(
65573  MemPage *pPage,         /* Page containing the cell */
65574  u8 *pCell,              /* Pointer to the cell text. */
65575  CellInfo *pInfo         /* Fill in this structure */
65576){
65577  u8 *pIter;              /* For scanning through pCell */
65578  u32 nPayload;           /* Number of bytes of cell payload */
65579
65580  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
65581  assert( pPage->leaf==0 || pPage->leaf==1 );
65582  assert( pPage->intKeyLeaf==0 );
65583  pIter = pCell + pPage->childPtrSize;
65584  nPayload = *pIter;
65585  if( nPayload>=0x80 ){
65586    u8 *pEnd = &pIter[8];
65587    nPayload &= 0x7f;
65588    do{
65589      nPayload = (nPayload<<7) | (*++pIter & 0x7f);
65590    }while( *(pIter)>=0x80 && pIter<pEnd );
65591  }
65592  pIter++;
65593  pInfo->nKey = nPayload;
65594  pInfo->nPayload = nPayload;
65595  pInfo->pPayload = pIter;
65596  testcase( nPayload==pPage->maxLocal );
65597  testcase( nPayload==pPage->maxLocal+1 );
65598  if( nPayload<=pPage->maxLocal ){
65599    /* This is the (easy) common case where the entire payload fits
65600    ** on the local page.  No overflow is required.
65601    */
65602    pInfo->nSize = nPayload + (u16)(pIter - pCell);
65603    if( pInfo->nSize<4 ) pInfo->nSize = 4;
65604    pInfo->nLocal = (u16)nPayload;
65605  }else{
65606    btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
65607  }
65608}
65609static void btreeParseCell(
65610  MemPage *pPage,         /* Page containing the cell */
65611  int iCell,              /* The cell index.  First cell is 0 */
65612  CellInfo *pInfo         /* Fill in this structure */
65613){
65614  pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
65615}
65616
65617/*
65618** The following routines are implementations of the MemPage.xCellSize
65619** method.
65620**
65621** Compute the total number of bytes that a Cell needs in the cell
65622** data area of the btree-page.  The return number includes the cell
65623** data header and the local payload, but not any overflow page or
65624** the space used by the cell pointer.
65625**
65626** cellSizePtrNoPayload()    =>   table internal nodes
65627** cellSizePtr()             =>   all index nodes & table leaf nodes
65628*/
65629static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
65630  u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
65631  u8 *pEnd;                                /* End mark for a varint */
65632  u32 nSize;                               /* Size value to return */
65633
65634#ifdef SQLITE_DEBUG
65635  /* The value returned by this function should always be the same as
65636  ** the (CellInfo.nSize) value found by doing a full parse of the
65637  ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
65638  ** this function verifies that this invariant is not violated. */
65639  CellInfo debuginfo;
65640  pPage->xParseCell(pPage, pCell, &debuginfo);
65641#endif
65642
65643  nSize = *pIter;
65644  if( nSize>=0x80 ){
65645    pEnd = &pIter[8];
65646    nSize &= 0x7f;
65647    do{
65648      nSize = (nSize<<7) | (*++pIter & 0x7f);
65649    }while( *(pIter)>=0x80 && pIter<pEnd );
65650  }
65651  pIter++;
65652  if( pPage->intKey ){
65653    /* pIter now points at the 64-bit integer key value, a variable length
65654    ** integer. The following block moves pIter to point at the first byte
65655    ** past the end of the key value. */
65656    pEnd = &pIter[9];
65657    while( (*pIter++)&0x80 && pIter<pEnd );
65658  }
65659  testcase( nSize==pPage->maxLocal );
65660  testcase( nSize==pPage->maxLocal+1 );
65661  if( nSize<=pPage->maxLocal ){
65662    nSize += (u32)(pIter - pCell);
65663    if( nSize<4 ) nSize = 4;
65664  }else{
65665    int minLocal = pPage->minLocal;
65666    nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
65667    testcase( nSize==pPage->maxLocal );
65668    testcase( nSize==pPage->maxLocal+1 );
65669    if( nSize>pPage->maxLocal ){
65670      nSize = minLocal;
65671    }
65672    nSize += 4 + (u16)(pIter - pCell);
65673  }
65674  assert( nSize==debuginfo.nSize || CORRUPT_DB );
65675  return (u16)nSize;
65676}
65677static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
65678  u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
65679  u8 *pEnd;              /* End mark for a varint */
65680
65681#ifdef SQLITE_DEBUG
65682  /* The value returned by this function should always be the same as
65683  ** the (CellInfo.nSize) value found by doing a full parse of the
65684  ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
65685  ** this function verifies that this invariant is not violated. */
65686  CellInfo debuginfo;
65687  pPage->xParseCell(pPage, pCell, &debuginfo);
65688#else
65689  UNUSED_PARAMETER(pPage);
65690#endif
65691
65692  assert( pPage->childPtrSize==4 );
65693  pEnd = pIter + 9;
65694  while( (*pIter++)&0x80 && pIter<pEnd );
65695  assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
65696  return (u16)(pIter - pCell);
65697}
65698
65699
65700#ifdef SQLITE_DEBUG
65701/* This variation on cellSizePtr() is used inside of assert() statements
65702** only. */
65703static u16 cellSize(MemPage *pPage, int iCell){
65704  return pPage->xCellSize(pPage, findCell(pPage, iCell));
65705}
65706#endif
65707
65708#ifndef SQLITE_OMIT_AUTOVACUUM
65709/*
65710** The cell pCell is currently part of page pSrc but will ultimately be part
65711** of pPage.  (pSrc and pPager are often the same.)  If pCell contains a
65712** pointer to an overflow page, insert an entry into the pointer-map for
65713** the overflow page that will be valid after pCell has been moved to pPage.
65714*/
65715static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){
65716  CellInfo info;
65717  if( *pRC ) return;
65718  assert( pCell!=0 );
65719  pPage->xParseCell(pPage, pCell, &info);
65720  if( info.nLocal<info.nPayload ){
65721    Pgno ovfl;
65722    if( SQLITE_WITHIN(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
65723      testcase( pSrc!=pPage );
65724      *pRC = SQLITE_CORRUPT_BKPT;
65725      return;
65726    }
65727    ovfl = get4byte(&pCell[info.nSize-4]);
65728    ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
65729  }
65730}
65731#endif
65732
65733
65734/*
65735** Defragment the page given. This routine reorganizes cells within the
65736** page so that there are no free-blocks on the free-block list.
65737**
65738** Parameter nMaxFrag is the maximum amount of fragmented space that may be
65739** present in the page after this routine returns.
65740**
65741** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
65742** b-tree page so that there are no freeblocks or fragment bytes, all
65743** unused bytes are contained in the unallocated space region, and all
65744** cells are packed tightly at the end of the page.
65745*/
65746static int defragmentPage(MemPage *pPage, int nMaxFrag){
65747  int i;                     /* Loop counter */
65748  int pc;                    /* Address of the i-th cell */
65749  int hdr;                   /* Offset to the page header */
65750  int size;                  /* Size of a cell */
65751  int usableSize;            /* Number of usable bytes on a page */
65752  int cellOffset;            /* Offset to the cell pointer array */
65753  int cbrk;                  /* Offset to the cell content area */
65754  int nCell;                 /* Number of cells on the page */
65755  unsigned char *data;       /* The page data */
65756  unsigned char *temp;       /* Temp area for cell content */
65757  unsigned char *src;        /* Source of content */
65758  int iCellFirst;            /* First allowable cell index */
65759  int iCellLast;             /* Last possible cell index */
65760
65761  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
65762  assert( pPage->pBt!=0 );
65763  assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
65764  assert( pPage->nOverflow==0 );
65765  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
65766  temp = 0;
65767  src = data = pPage->aData;
65768  hdr = pPage->hdrOffset;
65769  cellOffset = pPage->cellOffset;
65770  nCell = pPage->nCell;
65771  assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB );
65772  iCellFirst = cellOffset + 2*nCell;
65773  usableSize = pPage->pBt->usableSize;
65774
65775  /* This block handles pages with two or fewer free blocks and nMaxFrag
65776  ** or fewer fragmented bytes. In this case it is faster to move the
65777  ** two (or one) blocks of cells using memmove() and add the required
65778  ** offsets to each pointer in the cell-pointer array than it is to
65779  ** reconstruct the entire page.  */
65780  if( (int)data[hdr+7]<=nMaxFrag ){
65781    int iFree = get2byte(&data[hdr+1]);
65782    if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
65783    if( iFree ){
65784      int iFree2 = get2byte(&data[iFree]);
65785      if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
65786      if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
65787        u8 *pEnd = &data[cellOffset + nCell*2];
65788        u8 *pAddr;
65789        int sz2 = 0;
65790        int sz = get2byte(&data[iFree+2]);
65791        int top = get2byte(&data[hdr+5]);
65792        if( top>=iFree ){
65793          return SQLITE_CORRUPT_PAGE(pPage);
65794        }
65795        if( iFree2 ){
65796          if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage);
65797          sz2 = get2byte(&data[iFree2+2]);
65798          if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage);
65799          memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
65800          sz += sz2;
65801        }else if( NEVER(iFree+sz>usableSize) ){
65802          return SQLITE_CORRUPT_PAGE(pPage);
65803        }
65804
65805        cbrk = top+sz;
65806        assert( cbrk+(iFree-top) <= usableSize );
65807        memmove(&data[cbrk], &data[top], iFree-top);
65808        for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
65809          pc = get2byte(pAddr);
65810          if( pc<iFree ){ put2byte(pAddr, pc+sz); }
65811          else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
65812        }
65813        goto defragment_out;
65814      }
65815    }
65816  }
65817
65818  cbrk = usableSize;
65819  iCellLast = usableSize - 4;
65820  for(i=0; i<nCell; i++){
65821    u8 *pAddr;     /* The i-th cell pointer */
65822    pAddr = &data[cellOffset + i*2];
65823    pc = get2byte(pAddr);
65824    testcase( pc==iCellFirst );
65825    testcase( pc==iCellLast );
65826    /* These conditions have already been verified in btreeInitPage()
65827    ** if PRAGMA cell_size_check=ON.
65828    */
65829    if( pc<iCellFirst || pc>iCellLast ){
65830      return SQLITE_CORRUPT_PAGE(pPage);
65831    }
65832    assert( pc>=iCellFirst && pc<=iCellLast );
65833    size = pPage->xCellSize(pPage, &src[pc]);
65834    cbrk -= size;
65835    if( cbrk<iCellFirst || pc+size>usableSize ){
65836      return SQLITE_CORRUPT_PAGE(pPage);
65837    }
65838    assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
65839    testcase( cbrk+size==usableSize );
65840    testcase( pc+size==usableSize );
65841    put2byte(pAddr, cbrk);
65842    if( temp==0 ){
65843      int x;
65844      if( cbrk==pc ) continue;
65845      temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
65846      x = get2byte(&data[hdr+5]);
65847      memcpy(&temp[x], &data[x], (cbrk+size) - x);
65848      src = temp;
65849    }
65850    memcpy(&data[cbrk], &src[pc], size);
65851  }
65852  data[hdr+7] = 0;
65853
65854 defragment_out:
65855  assert( pPage->nFree>=0 );
65856  if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
65857    return SQLITE_CORRUPT_PAGE(pPage);
65858  }
65859  assert( cbrk>=iCellFirst );
65860  put2byte(&data[hdr+5], cbrk);
65861  data[hdr+1] = 0;
65862  data[hdr+2] = 0;
65863  memset(&data[iCellFirst], 0, cbrk-iCellFirst);
65864  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
65865  return SQLITE_OK;
65866}
65867
65868/*
65869** Search the free-list on page pPg for space to store a cell nByte bytes in
65870** size. If one can be found, return a pointer to the space and remove it
65871** from the free-list.
65872**
65873** If no suitable space can be found on the free-list, return NULL.
65874**
65875** This function may detect corruption within pPg.  If corruption is
65876** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
65877**
65878** Slots on the free list that are between 1 and 3 bytes larger than nByte
65879** will be ignored if adding the extra space to the fragmentation count
65880** causes the fragmentation count to exceed 60.
65881*/
65882static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
65883  const int hdr = pPg->hdrOffset;            /* Offset to page header */
65884  u8 * const aData = pPg->aData;             /* Page data */
65885  int iAddr = hdr + 1;                       /* Address of ptr to pc */
65886  int pc = get2byte(&aData[iAddr]);          /* Address of a free slot */
65887  int x;                                     /* Excess size of the slot */
65888  int maxPC = pPg->pBt->usableSize - nByte;  /* Max address for a usable slot */
65889  int size;                                  /* Size of the free slot */
65890
65891  assert( pc>0 );
65892  while( pc<=maxPC ){
65893    /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
65894    ** freeblock form a big-endian integer which is the size of the freeblock
65895    ** in bytes, including the 4-byte header. */
65896    size = get2byte(&aData[pc+2]);
65897    if( (x = size - nByte)>=0 ){
65898      testcase( x==4 );
65899      testcase( x==3 );
65900      if( x<4 ){
65901        /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
65902        ** number of bytes in fragments may not exceed 60. */
65903        if( aData[hdr+7]>57 ) return 0;
65904
65905        /* Remove the slot from the free-list. Update the number of
65906        ** fragmented bytes within the page. */
65907        memcpy(&aData[iAddr], &aData[pc], 2);
65908        aData[hdr+7] += (u8)x;
65909      }else if( x+pc > maxPC ){
65910        /* This slot extends off the end of the usable part of the page */
65911        *pRc = SQLITE_CORRUPT_PAGE(pPg);
65912        return 0;
65913      }else{
65914        /* The slot remains on the free-list. Reduce its size to account
65915        ** for the portion used by the new allocation. */
65916        put2byte(&aData[pc+2], x);
65917      }
65918      return &aData[pc + x];
65919    }
65920    iAddr = pc;
65921    pc = get2byte(&aData[pc]);
65922    if( pc<=iAddr+size ){
65923      if( pc ){
65924        /* The next slot in the chain is not past the end of the current slot */
65925        *pRc = SQLITE_CORRUPT_PAGE(pPg);
65926      }
65927      return 0;
65928    }
65929  }
65930  if( pc>maxPC+nByte-4 ){
65931    /* The free slot chain extends off the end of the page */
65932    *pRc = SQLITE_CORRUPT_PAGE(pPg);
65933  }
65934  return 0;
65935}
65936
65937/*
65938** Allocate nByte bytes of space from within the B-Tree page passed
65939** as the first argument. Write into *pIdx the index into pPage->aData[]
65940** of the first byte of allocated space. Return either SQLITE_OK or
65941** an error code (usually SQLITE_CORRUPT).
65942**
65943** The caller guarantees that there is sufficient space to make the
65944** allocation.  This routine might need to defragment in order to bring
65945** all the space together, however.  This routine will avoid using
65946** the first two bytes past the cell pointer area since presumably this
65947** allocation is being made in order to insert a new cell, so we will
65948** also end up needing a new cell pointer.
65949*/
65950static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
65951  const int hdr = pPage->hdrOffset;    /* Local cache of pPage->hdrOffset */
65952  u8 * const data = pPage->aData;      /* Local cache of pPage->aData */
65953  int top;                             /* First byte of cell content area */
65954  int rc = SQLITE_OK;                  /* Integer return code */
65955  int gap;        /* First byte of gap between cell pointers and cell content */
65956
65957  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
65958  assert( pPage->pBt );
65959  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
65960  assert( nByte>=0 );  /* Minimum cell size is 4 */
65961  assert( pPage->nFree>=nByte );
65962  assert( pPage->nOverflow==0 );
65963  assert( nByte < (int)(pPage->pBt->usableSize-8) );
65964
65965  assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
65966  gap = pPage->cellOffset + 2*pPage->nCell;
65967  assert( gap<=65536 );
65968  /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
65969  ** and the reserved space is zero (the usual value for reserved space)
65970  ** then the cell content offset of an empty page wants to be 65536.
65971  ** However, that integer is too large to be stored in a 2-byte unsigned
65972  ** integer, so a value of 0 is used in its place. */
65973  top = get2byte(&data[hdr+5]);
65974  assert( top<=(int)pPage->pBt->usableSize ); /* by btreeComputeFreeSpace() */
65975  if( gap>top ){
65976    if( top==0 && pPage->pBt->usableSize==65536 ){
65977      top = 65536;
65978    }else{
65979      return SQLITE_CORRUPT_PAGE(pPage);
65980    }
65981  }
65982
65983  /* If there is enough space between gap and top for one more cell pointer,
65984  ** and if the freelist is not empty, then search the
65985  ** freelist looking for a slot big enough to satisfy the request.
65986  */
65987  testcase( gap+2==top );
65988  testcase( gap+1==top );
65989  testcase( gap==top );
65990  if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
65991    u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
65992    if( pSpace ){
65993      int g2;
65994      assert( pSpace+nByte<=data+pPage->pBt->usableSize );
65995      *pIdx = g2 = (int)(pSpace-data);
65996      if( NEVER(g2<=gap) ){
65997        return SQLITE_CORRUPT_PAGE(pPage);
65998      }else{
65999        return SQLITE_OK;
66000      }
66001    }else if( rc ){
66002      return rc;
66003    }
66004  }
66005
66006  /* The request could not be fulfilled using a freelist slot.  Check
66007  ** to see if defragmentation is necessary.
66008  */
66009  testcase( gap+2+nByte==top );
66010  if( gap+2+nByte>top ){
66011    assert( pPage->nCell>0 || CORRUPT_DB );
66012    assert( pPage->nFree>=0 );
66013    rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
66014    if( rc ) return rc;
66015    top = get2byteNotZero(&data[hdr+5]);
66016    assert( gap+2+nByte<=top );
66017  }
66018
66019
66020  /* Allocate memory from the gap in between the cell pointer array
66021  ** and the cell content area.  The btreeComputeFreeSpace() call has already
66022  ** validated the freelist.  Given that the freelist is valid, there
66023  ** is no way that the allocation can extend off the end of the page.
66024  ** The assert() below verifies the previous sentence.
66025  */
66026  top -= nByte;
66027  put2byte(&data[hdr+5], top);
66028  assert( top+nByte <= (int)pPage->pBt->usableSize );
66029  *pIdx = top;
66030  return SQLITE_OK;
66031}
66032
66033/*
66034** Return a section of the pPage->aData to the freelist.
66035** The first byte of the new free block is pPage->aData[iStart]
66036** and the size of the block is iSize bytes.
66037**
66038** Adjacent freeblocks are coalesced.
66039**
66040** Even though the freeblock list was checked by btreeComputeFreeSpace(),
66041** that routine will not detect overlap between cells or freeblocks.  Nor
66042** does it detect cells or freeblocks that encrouch into the reserved bytes
66043** at the end of the page.  So do additional corruption checks inside this
66044** routine and return SQLITE_CORRUPT if any problems are found.
66045*/
66046static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
66047  u16 iPtr;                             /* Address of ptr to next freeblock */
66048  u16 iFreeBlk;                         /* Address of the next freeblock */
66049  u8 hdr;                               /* Page header size.  0 or 100 */
66050  u8 nFrag = 0;                         /* Reduction in fragmentation */
66051  u16 iOrigSize = iSize;                /* Original value of iSize */
66052  u16 x;                                /* Offset to cell content area */
66053  u32 iEnd = iStart + iSize;            /* First byte past the iStart buffer */
66054  unsigned char *data = pPage->aData;   /* Page content */
66055
66056  assert( pPage->pBt!=0 );
66057  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
66058  assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
66059  assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
66060  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66061  assert( iSize>=4 );   /* Minimum cell size is 4 */
66062  assert( iStart<=pPage->pBt->usableSize-4 );
66063
66064  /* The list of freeblocks must be in ascending order.  Find the
66065  ** spot on the list where iStart should be inserted.
66066  */
66067  hdr = pPage->hdrOffset;
66068  iPtr = hdr + 1;
66069  if( data[iPtr+1]==0 && data[iPtr]==0 ){
66070    iFreeBlk = 0;  /* Shortcut for the case when the freelist is empty */
66071  }else{
66072    while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
66073      if( iFreeBlk<iPtr+4 ){
66074        if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */
66075        return SQLITE_CORRUPT_PAGE(pPage);
66076      }
66077      iPtr = iFreeBlk;
66078    }
66079    if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
66080      return SQLITE_CORRUPT_PAGE(pPage);
66081    }
66082    assert( iFreeBlk>iPtr || iFreeBlk==0 );
66083
66084    /* At this point:
66085    **    iFreeBlk:   First freeblock after iStart, or zero if none
66086    **    iPtr:       The address of a pointer to iFreeBlk
66087    **
66088    ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
66089    */
66090    if( iFreeBlk && iEnd+3>=iFreeBlk ){
66091      nFrag = iFreeBlk - iEnd;
66092      if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
66093      iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
66094      if( iEnd > pPage->pBt->usableSize ){
66095        return SQLITE_CORRUPT_PAGE(pPage);
66096      }
66097      iSize = iEnd - iStart;
66098      iFreeBlk = get2byte(&data[iFreeBlk]);
66099    }
66100
66101    /* If iPtr is another freeblock (that is, if iPtr is not the freelist
66102    ** pointer in the page header) then check to see if iStart should be
66103    ** coalesced onto the end of iPtr.
66104    */
66105    if( iPtr>hdr+1 ){
66106      int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
66107      if( iPtrEnd+3>=iStart ){
66108        if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage);
66109        nFrag += iStart - iPtrEnd;
66110        iSize = iEnd - iPtr;
66111        iStart = iPtr;
66112      }
66113    }
66114    if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
66115    data[hdr+7] -= nFrag;
66116  }
66117  x = get2byte(&data[hdr+5]);
66118  if( iStart<=x ){
66119    /* The new freeblock is at the beginning of the cell content area,
66120    ** so just extend the cell content area rather than create another
66121    ** freelist entry */
66122    if( iStart<x ) return SQLITE_CORRUPT_PAGE(pPage);
66123    if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage);
66124    put2byte(&data[hdr+1], iFreeBlk);
66125    put2byte(&data[hdr+5], iEnd);
66126  }else{
66127    /* Insert the new freeblock into the freelist */
66128    put2byte(&data[iPtr], iStart);
66129  }
66130  if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
66131    /* Overwrite deleted information with zeros when the secure_delete
66132    ** option is enabled */
66133    memset(&data[iStart], 0, iSize);
66134  }
66135  put2byte(&data[iStart], iFreeBlk);
66136  put2byte(&data[iStart+2], iSize);
66137  pPage->nFree += iOrigSize;
66138  return SQLITE_OK;
66139}
66140
66141/*
66142** Decode the flags byte (the first byte of the header) for a page
66143** and initialize fields of the MemPage structure accordingly.
66144**
66145** Only the following combinations are supported.  Anything different
66146** indicates a corrupt database files:
66147**
66148**         PTF_ZERODATA
66149**         PTF_ZERODATA | PTF_LEAF
66150**         PTF_LEAFDATA | PTF_INTKEY
66151**         PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
66152*/
66153static int decodeFlags(MemPage *pPage, int flagByte){
66154  BtShared *pBt;     /* A copy of pPage->pBt */
66155
66156  assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
66157  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66158  pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
66159  flagByte &= ~PTF_LEAF;
66160  pPage->childPtrSize = 4-4*pPage->leaf;
66161  pPage->xCellSize = cellSizePtr;
66162  pBt = pPage->pBt;
66163  if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
66164    /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
66165    ** interior table b-tree page. */
66166    assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
66167    /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
66168    ** leaf table b-tree page. */
66169    assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
66170    pPage->intKey = 1;
66171    if( pPage->leaf ){
66172      pPage->intKeyLeaf = 1;
66173      pPage->xParseCell = btreeParseCellPtr;
66174    }else{
66175      pPage->intKeyLeaf = 0;
66176      pPage->xCellSize = cellSizePtrNoPayload;
66177      pPage->xParseCell = btreeParseCellPtrNoPayload;
66178    }
66179    pPage->maxLocal = pBt->maxLeaf;
66180    pPage->minLocal = pBt->minLeaf;
66181  }else if( flagByte==PTF_ZERODATA ){
66182    /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
66183    ** interior index b-tree page. */
66184    assert( (PTF_ZERODATA)==2 );
66185    /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
66186    ** leaf index b-tree page. */
66187    assert( (PTF_ZERODATA|PTF_LEAF)==10 );
66188    pPage->intKey = 0;
66189    pPage->intKeyLeaf = 0;
66190    pPage->xParseCell = btreeParseCellPtrIndex;
66191    pPage->maxLocal = pBt->maxLocal;
66192    pPage->minLocal = pBt->minLocal;
66193  }else{
66194    /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
66195    ** an error. */
66196    return SQLITE_CORRUPT_PAGE(pPage);
66197  }
66198  pPage->max1bytePayload = pBt->max1bytePayload;
66199  return SQLITE_OK;
66200}
66201
66202/*
66203** Compute the amount of freespace on the page.  In other words, fill
66204** in the pPage->nFree field.
66205*/
66206static int btreeComputeFreeSpace(MemPage *pPage){
66207  int pc;            /* Address of a freeblock within pPage->aData[] */
66208  u8 hdr;            /* Offset to beginning of page header */
66209  u8 *data;          /* Equal to pPage->aData */
66210  int usableSize;    /* Amount of usable space on each page */
66211  int nFree;         /* Number of unused bytes on the page */
66212  int top;           /* First byte of the cell content area */
66213  int iCellFirst;    /* First allowable cell or freeblock offset */
66214  int iCellLast;     /* Last possible cell or freeblock offset */
66215
66216  assert( pPage->pBt!=0 );
66217  assert( pPage->pBt->db!=0 );
66218  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66219  assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
66220  assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
66221  assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
66222  assert( pPage->isInit==1 );
66223  assert( pPage->nFree<0 );
66224
66225  usableSize = pPage->pBt->usableSize;
66226  hdr = pPage->hdrOffset;
66227  data = pPage->aData;
66228  /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
66229  ** the start of the cell content area. A zero value for this integer is
66230  ** interpreted as 65536. */
66231  top = get2byteNotZero(&data[hdr+5]);
66232  iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
66233  iCellLast = usableSize - 4;
66234
66235  /* Compute the total free space on the page
66236  ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
66237  ** start of the first freeblock on the page, or is zero if there are no
66238  ** freeblocks. */
66239  pc = get2byte(&data[hdr+1]);
66240  nFree = data[hdr+7] + top;  /* Init nFree to non-freeblock free space */
66241  if( pc>0 ){
66242    u32 next, size;
66243    if( pc<top ){
66244      /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
66245      ** always be at least one cell before the first freeblock.
66246      */
66247      return SQLITE_CORRUPT_PAGE(pPage);
66248    }
66249    while( 1 ){
66250      if( pc>iCellLast ){
66251        /* Freeblock off the end of the page */
66252        return SQLITE_CORRUPT_PAGE(pPage);
66253      }
66254      next = get2byte(&data[pc]);
66255      size = get2byte(&data[pc+2]);
66256      nFree = nFree + size;
66257      if( next<=pc+size+3 ) break;
66258      pc = next;
66259    }
66260    if( next>0 ){
66261      /* Freeblock not in ascending order */
66262      return SQLITE_CORRUPT_PAGE(pPage);
66263    }
66264    if( pc+size>(unsigned int)usableSize ){
66265      /* Last freeblock extends past page end */
66266      return SQLITE_CORRUPT_PAGE(pPage);
66267    }
66268  }
66269
66270  /* At this point, nFree contains the sum of the offset to the start
66271  ** of the cell-content area plus the number of free bytes within
66272  ** the cell-content area. If this is greater than the usable-size
66273  ** of the page, then the page must be corrupted. This check also
66274  ** serves to verify that the offset to the start of the cell-content
66275  ** area, according to the page header, lies within the page.
66276  */
66277  if( nFree>usableSize || nFree<iCellFirst ){
66278    return SQLITE_CORRUPT_PAGE(pPage);
66279  }
66280  pPage->nFree = (u16)(nFree - iCellFirst);
66281  return SQLITE_OK;
66282}
66283
66284/*
66285** Do additional sanity check after btreeInitPage() if
66286** PRAGMA cell_size_check=ON
66287*/
66288static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){
66289  int iCellFirst;    /* First allowable cell or freeblock offset */
66290  int iCellLast;     /* Last possible cell or freeblock offset */
66291  int i;             /* Index into the cell pointer array */
66292  int sz;            /* Size of a cell */
66293  int pc;            /* Address of a freeblock within pPage->aData[] */
66294  u8 *data;          /* Equal to pPage->aData */
66295  int usableSize;    /* Maximum usable space on the page */
66296  int cellOffset;    /* Start of cell content area */
66297
66298  iCellFirst = pPage->cellOffset + 2*pPage->nCell;
66299  usableSize = pPage->pBt->usableSize;
66300  iCellLast = usableSize - 4;
66301  data = pPage->aData;
66302  cellOffset = pPage->cellOffset;
66303  if( !pPage->leaf ) iCellLast--;
66304  for(i=0; i<pPage->nCell; i++){
66305    pc = get2byteAligned(&data[cellOffset+i*2]);
66306    testcase( pc==iCellFirst );
66307    testcase( pc==iCellLast );
66308    if( pc<iCellFirst || pc>iCellLast ){
66309      return SQLITE_CORRUPT_PAGE(pPage);
66310    }
66311    sz = pPage->xCellSize(pPage, &data[pc]);
66312    testcase( pc+sz==usableSize );
66313    if( pc+sz>usableSize ){
66314      return SQLITE_CORRUPT_PAGE(pPage);
66315    }
66316  }
66317  return SQLITE_OK;
66318}
66319
66320/*
66321** Initialize the auxiliary information for a disk block.
66322**
66323** Return SQLITE_OK on success.  If we see that the page does
66324** not contain a well-formed database page, then return
66325** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
66326** guarantee that the page is well-formed.  It only shows that
66327** we failed to detect any corruption.
66328*/
66329static int btreeInitPage(MemPage *pPage){
66330  u8 *data;          /* Equal to pPage->aData */
66331  BtShared *pBt;        /* The main btree structure */
66332
66333  assert( pPage->pBt!=0 );
66334  assert( pPage->pBt->db!=0 );
66335  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66336  assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
66337  assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
66338  assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
66339  assert( pPage->isInit==0 );
66340
66341  pBt = pPage->pBt;
66342  data = pPage->aData + pPage->hdrOffset;
66343  /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
66344  ** the b-tree page type. */
66345  if( decodeFlags(pPage, data[0]) ){
66346    return SQLITE_CORRUPT_PAGE(pPage);
66347  }
66348  assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
66349  pPage->maskPage = (u16)(pBt->pageSize - 1);
66350  pPage->nOverflow = 0;
66351  pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
66352  pPage->aCellIdx = data + pPage->childPtrSize + 8;
66353  pPage->aDataEnd = pPage->aData + pBt->usableSize;
66354  pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
66355  /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
66356  ** number of cells on the page. */
66357  pPage->nCell = get2byte(&data[3]);
66358  if( pPage->nCell>MX_CELL(pBt) ){
66359    /* To many cells for a single page.  The page must be corrupt */
66360    return SQLITE_CORRUPT_PAGE(pPage);
66361  }
66362  testcase( pPage->nCell==MX_CELL(pBt) );
66363  /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
66364  ** possible for a root page of a table that contains no rows) then the
66365  ** offset to the cell content area will equal the page size minus the
66366  ** bytes of reserved space. */
66367  assert( pPage->nCell>0
66368       || get2byteNotZero(&data[5])==(int)pBt->usableSize
66369       || CORRUPT_DB );
66370  pPage->nFree = -1;  /* Indicate that this value is yet uncomputed */
66371  pPage->isInit = 1;
66372  if( pBt->db->flags & SQLITE_CellSizeCk ){
66373    return btreeCellSizeCheck(pPage);
66374  }
66375  return SQLITE_OK;
66376}
66377
66378/*
66379** Set up a raw page so that it looks like a database page holding
66380** no entries.
66381*/
66382static void zeroPage(MemPage *pPage, int flags){
66383  unsigned char *data = pPage->aData;
66384  BtShared *pBt = pPage->pBt;
66385  u8 hdr = pPage->hdrOffset;
66386  u16 first;
66387
66388  assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
66389  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
66390  assert( sqlite3PagerGetData(pPage->pDbPage) == data );
66391  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
66392  assert( sqlite3_mutex_held(pBt->mutex) );
66393  if( pBt->btsFlags & BTS_FAST_SECURE ){
66394    memset(&data[hdr], 0, pBt->usableSize - hdr);
66395  }
66396  data[hdr] = (char)flags;
66397  first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
66398  memset(&data[hdr+1], 0, 4);
66399  data[hdr+7] = 0;
66400  put2byte(&data[hdr+5], pBt->usableSize);
66401  pPage->nFree = (u16)(pBt->usableSize - first);
66402  decodeFlags(pPage, flags);
66403  pPage->cellOffset = first;
66404  pPage->aDataEnd = &data[pBt->usableSize];
66405  pPage->aCellIdx = &data[first];
66406  pPage->aDataOfst = &data[pPage->childPtrSize];
66407  pPage->nOverflow = 0;
66408  assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
66409  pPage->maskPage = (u16)(pBt->pageSize - 1);
66410  pPage->nCell = 0;
66411  pPage->isInit = 1;
66412}
66413
66414
66415/*
66416** Convert a DbPage obtained from the pager into a MemPage used by
66417** the btree layer.
66418*/
66419static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
66420  MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
66421  if( pgno!=pPage->pgno ){
66422    pPage->aData = sqlite3PagerGetData(pDbPage);
66423    pPage->pDbPage = pDbPage;
66424    pPage->pBt = pBt;
66425    pPage->pgno = pgno;
66426    pPage->hdrOffset = pgno==1 ? 100 : 0;
66427  }
66428  assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
66429  return pPage;
66430}
66431
66432/*
66433** Get a page from the pager.  Initialize the MemPage.pBt and
66434** MemPage.aData elements if needed.  See also: btreeGetUnusedPage().
66435**
66436** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
66437** about the content of the page at this time.  So do not go to the disk
66438** to fetch the content.  Just fill in the content with zeros for now.
66439** If in the future we call sqlite3PagerWrite() on this page, that
66440** means we have started to be concerned about content and the disk
66441** read should occur at that point.
66442*/
66443static int btreeGetPage(
66444  BtShared *pBt,       /* The btree */
66445  Pgno pgno,           /* Number of the page to fetch */
66446  MemPage **ppPage,    /* Return the page in this parameter */
66447  int flags            /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
66448){
66449  int rc;
66450  DbPage *pDbPage;
66451
66452  assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
66453  assert( sqlite3_mutex_held(pBt->mutex) );
66454  rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
66455  if( rc ) return rc;
66456  *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
66457  return SQLITE_OK;
66458}
66459
66460/*
66461** Retrieve a page from the pager cache. If the requested page is not
66462** already in the pager cache return NULL. Initialize the MemPage.pBt and
66463** MemPage.aData elements if needed.
66464*/
66465static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
66466  DbPage *pDbPage;
66467  assert( sqlite3_mutex_held(pBt->mutex) );
66468  pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
66469  if( pDbPage ){
66470    return btreePageFromDbPage(pDbPage, pgno, pBt);
66471  }
66472  return 0;
66473}
66474
66475/*
66476** Return the size of the database file in pages. If there is any kind of
66477** error, return ((unsigned int)-1).
66478*/
66479static Pgno btreePagecount(BtShared *pBt){
66480  assert( (pBt->nPage & 0x80000000)==0 || CORRUPT_DB );
66481  return pBt->nPage;
66482}
66483SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
66484  assert( sqlite3BtreeHoldsMutex(p) );
66485  return btreePagecount(p->pBt) & 0x7fffffff;
66486}
66487
66488/*
66489** Get a page from the pager and initialize it.
66490**
66491** If pCur!=0 then the page is being fetched as part of a moveToChild()
66492** call.  Do additional sanity checking on the page in this case.
66493** And if the fetch fails, this routine must decrement pCur->iPage.
66494**
66495** The page is fetched as read-write unless pCur is not NULL and is
66496** a read-only cursor.
66497**
66498** If an error occurs, then *ppPage is undefined. It
66499** may remain unchanged, or it may be set to an invalid value.
66500*/
66501static int getAndInitPage(
66502  BtShared *pBt,                  /* The database file */
66503  Pgno pgno,                      /* Number of the page to get */
66504  MemPage **ppPage,               /* Write the page pointer here */
66505  BtCursor *pCur,                 /* Cursor to receive the page, or NULL */
66506  int bReadOnly                   /* True for a read-only page */
66507){
66508  int rc;
66509  DbPage *pDbPage;
66510  assert( sqlite3_mutex_held(pBt->mutex) );
66511  assert( pCur==0 || ppPage==&pCur->pPage );
66512  assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
66513  assert( pCur==0 || pCur->iPage>0 );
66514
66515  if( pgno>btreePagecount(pBt) ){
66516    rc = SQLITE_CORRUPT_BKPT;
66517    goto getAndInitPage_error1;
66518  }
66519  rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
66520  if( rc ){
66521    goto getAndInitPage_error1;
66522  }
66523  *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
66524  if( (*ppPage)->isInit==0 ){
66525    btreePageFromDbPage(pDbPage, pgno, pBt);
66526    rc = btreeInitPage(*ppPage);
66527    if( rc!=SQLITE_OK ){
66528      goto getAndInitPage_error2;
66529    }
66530  }
66531  assert( (*ppPage)->pgno==pgno );
66532  assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
66533
66534  /* If obtaining a child page for a cursor, we must verify that the page is
66535  ** compatible with the root page. */
66536  if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
66537    rc = SQLITE_CORRUPT_PGNO(pgno);
66538    goto getAndInitPage_error2;
66539  }
66540  return SQLITE_OK;
66541
66542getAndInitPage_error2:
66543  releasePage(*ppPage);
66544getAndInitPage_error1:
66545  if( pCur ){
66546    pCur->iPage--;
66547    pCur->pPage = pCur->apPage[pCur->iPage];
66548  }
66549  testcase( pgno==0 );
66550  assert( pgno!=0 || rc==SQLITE_CORRUPT );
66551  return rc;
66552}
66553
66554/*
66555** Release a MemPage.  This should be called once for each prior
66556** call to btreeGetPage.
66557**
66558** Page1 is a special case and must be released using releasePageOne().
66559*/
66560static void releasePageNotNull(MemPage *pPage){
66561  assert( pPage->aData );
66562  assert( pPage->pBt );
66563  assert( pPage->pDbPage!=0 );
66564  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
66565  assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
66566  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66567  sqlite3PagerUnrefNotNull(pPage->pDbPage);
66568}
66569static void releasePage(MemPage *pPage){
66570  if( pPage ) releasePageNotNull(pPage);
66571}
66572static void releasePageOne(MemPage *pPage){
66573  assert( pPage!=0 );
66574  assert( pPage->aData );
66575  assert( pPage->pBt );
66576  assert( pPage->pDbPage!=0 );
66577  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
66578  assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
66579  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66580  sqlite3PagerUnrefPageOne(pPage->pDbPage);
66581}
66582
66583/*
66584** Get an unused page.
66585**
66586** This works just like btreeGetPage() with the addition:
66587**
66588**   *  If the page is already in use for some other purpose, immediately
66589**      release it and return an SQLITE_CURRUPT error.
66590**   *  Make sure the isInit flag is clear
66591*/
66592static int btreeGetUnusedPage(
66593  BtShared *pBt,       /* The btree */
66594  Pgno pgno,           /* Number of the page to fetch */
66595  MemPage **ppPage,    /* Return the page in this parameter */
66596  int flags            /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
66597){
66598  int rc = btreeGetPage(pBt, pgno, ppPage, flags);
66599  if( rc==SQLITE_OK ){
66600    if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
66601      releasePage(*ppPage);
66602      *ppPage = 0;
66603      return SQLITE_CORRUPT_BKPT;
66604    }
66605    (*ppPage)->isInit = 0;
66606  }else{
66607    *ppPage = 0;
66608  }
66609  return rc;
66610}
66611
66612
66613/*
66614** During a rollback, when the pager reloads information into the cache
66615** so that the cache is restored to its original state at the start of
66616** the transaction, for each page restored this routine is called.
66617**
66618** This routine needs to reset the extra data section at the end of the
66619** page to agree with the restored data.
66620*/
66621static void pageReinit(DbPage *pData){
66622  MemPage *pPage;
66623  pPage = (MemPage *)sqlite3PagerGetExtra(pData);
66624  assert( sqlite3PagerPageRefcount(pData)>0 );
66625  if( pPage->isInit ){
66626    assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66627    pPage->isInit = 0;
66628    if( sqlite3PagerPageRefcount(pData)>1 ){
66629      /* pPage might not be a btree page;  it might be an overflow page
66630      ** or ptrmap page or a free page.  In those cases, the following
66631      ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
66632      ** But no harm is done by this.  And it is very important that
66633      ** btreeInitPage() be called on every btree page so we make
66634      ** the call for every page that comes in for re-initing. */
66635      btreeInitPage(pPage);
66636    }
66637  }
66638}
66639
66640/*
66641** Invoke the busy handler for a btree.
66642*/
66643static int btreeInvokeBusyHandler(void *pArg){
66644  BtShared *pBt = (BtShared*)pArg;
66645  assert( pBt->db );
66646  assert( sqlite3_mutex_held(pBt->db->mutex) );
66647  return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
66648}
66649
66650/*
66651** Open a database file.
66652**
66653** zFilename is the name of the database file.  If zFilename is NULL
66654** then an ephemeral database is created.  The ephemeral database might
66655** be exclusively in memory, or it might use a disk-based memory cache.
66656** Either way, the ephemeral database will be automatically deleted
66657** when sqlite3BtreeClose() is called.
66658**
66659** If zFilename is ":memory:" then an in-memory database is created
66660** that is automatically destroyed when it is closed.
66661**
66662** The "flags" parameter is a bitmask that might contain bits like
66663** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
66664**
66665** If the database is already opened in the same database connection
66666** and we are in shared cache mode, then the open will fail with an
66667** SQLITE_CONSTRAINT error.  We cannot allow two or more BtShared
66668** objects in the same database connection since doing so will lead
66669** to problems with locking.
66670*/
66671SQLITE_PRIVATE int sqlite3BtreeOpen(
66672  sqlite3_vfs *pVfs,      /* VFS to use for this b-tree */
66673  const char *zFilename,  /* Name of the file containing the BTree database */
66674  sqlite3 *db,            /* Associated database handle */
66675  Btree **ppBtree,        /* Pointer to new Btree object written here */
66676  int flags,              /* Options */
66677  int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */
66678){
66679  BtShared *pBt = 0;             /* Shared part of btree structure */
66680  Btree *p;                      /* Handle to return */
66681  sqlite3_mutex *mutexOpen = 0;  /* Prevents a race condition. Ticket #3537 */
66682  int rc = SQLITE_OK;            /* Result code from this function */
66683  u8 nReserve;                   /* Byte of unused space on each page */
66684  unsigned char zDbHeader[100];  /* Database header content */
66685
66686  /* True if opening an ephemeral, temporary database */
66687  const int isTempDb = zFilename==0 || zFilename[0]==0;
66688
66689  /* Set the variable isMemdb to true for an in-memory database, or
66690  ** false for a file-based database.
66691  */
66692#ifdef SQLITE_OMIT_MEMORYDB
66693  const int isMemdb = 0;
66694#else
66695  const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
66696                       || (isTempDb && sqlite3TempInMemory(db))
66697                       || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
66698#endif
66699
66700  assert( db!=0 );
66701  assert( pVfs!=0 );
66702  assert( sqlite3_mutex_held(db->mutex) );
66703  assert( (flags&0xff)==flags );   /* flags fit in 8 bits */
66704
66705  /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
66706  assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
66707
66708  /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
66709  assert( (flags & BTREE_SINGLE)==0 || isTempDb );
66710
66711  if( isMemdb ){
66712    flags |= BTREE_MEMORY;
66713  }
66714  if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
66715    vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
66716  }
66717  p = sqlite3MallocZero(sizeof(Btree));
66718  if( !p ){
66719    return SQLITE_NOMEM_BKPT;
66720  }
66721  p->inTrans = TRANS_NONE;
66722  p->db = db;
66723#ifndef SQLITE_OMIT_SHARED_CACHE
66724  p->lock.pBtree = p;
66725  p->lock.iTable = 1;
66726#endif
66727
66728#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
66729  /*
66730  ** If this Btree is a candidate for shared cache, try to find an
66731  ** existing BtShared object that we can share with
66732  */
66733  if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
66734    if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
66735      int nFilename = sqlite3Strlen30(zFilename)+1;
66736      int nFullPathname = pVfs->mxPathname+1;
66737      char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename));
66738      MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
66739
66740      p->sharable = 1;
66741      if( !zFullPathname ){
66742        sqlite3_free(p);
66743        return SQLITE_NOMEM_BKPT;
66744      }
66745      if( isMemdb ){
66746        memcpy(zFullPathname, zFilename, nFilename);
66747      }else{
66748        rc = sqlite3OsFullPathname(pVfs, zFilename,
66749                                   nFullPathname, zFullPathname);
66750        if( rc ){
66751          if( rc==SQLITE_OK_SYMLINK ){
66752            rc = SQLITE_OK;
66753          }else{
66754            sqlite3_free(zFullPathname);
66755            sqlite3_free(p);
66756            return rc;
66757          }
66758        }
66759      }
66760#if SQLITE_THREADSAFE
66761      mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
66762      sqlite3_mutex_enter(mutexOpen);
66763      mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
66764      sqlite3_mutex_enter(mutexShared);
66765#endif
66766      for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
66767        assert( pBt->nRef>0 );
66768        if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
66769                 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
66770          int iDb;
66771          for(iDb=db->nDb-1; iDb>=0; iDb--){
66772            Btree *pExisting = db->aDb[iDb].pBt;
66773            if( pExisting && pExisting->pBt==pBt ){
66774              sqlite3_mutex_leave(mutexShared);
66775              sqlite3_mutex_leave(mutexOpen);
66776              sqlite3_free(zFullPathname);
66777              sqlite3_free(p);
66778              return SQLITE_CONSTRAINT;
66779            }
66780          }
66781          p->pBt = pBt;
66782          pBt->nRef++;
66783          break;
66784        }
66785      }
66786      sqlite3_mutex_leave(mutexShared);
66787      sqlite3_free(zFullPathname);
66788    }
66789#ifdef SQLITE_DEBUG
66790    else{
66791      /* In debug mode, we mark all persistent databases as sharable
66792      ** even when they are not.  This exercises the locking code and
66793      ** gives more opportunity for asserts(sqlite3_mutex_held())
66794      ** statements to find locking problems.
66795      */
66796      p->sharable = 1;
66797    }
66798#endif
66799  }
66800#endif
66801  if( pBt==0 ){
66802    /*
66803    ** The following asserts make sure that structures used by the btree are
66804    ** the right size.  This is to guard against size changes that result
66805    ** when compiling on a different architecture.
66806    */
66807    assert( sizeof(i64)==8 );
66808    assert( sizeof(u64)==8 );
66809    assert( sizeof(u32)==4 );
66810    assert( sizeof(u16)==2 );
66811    assert( sizeof(Pgno)==4 );
66812
66813    pBt = sqlite3MallocZero( sizeof(*pBt) );
66814    if( pBt==0 ){
66815      rc = SQLITE_NOMEM_BKPT;
66816      goto btree_open_out;
66817    }
66818    rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
66819                          sizeof(MemPage), flags, vfsFlags, pageReinit);
66820    if( rc==SQLITE_OK ){
66821      sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
66822      rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
66823    }
66824    if( rc!=SQLITE_OK ){
66825      goto btree_open_out;
66826    }
66827    pBt->openFlags = (u8)flags;
66828    pBt->db = db;
66829    sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
66830    p->pBt = pBt;
66831
66832    pBt->pCursor = 0;
66833    pBt->pPage1 = 0;
66834    if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
66835#if defined(SQLITE_SECURE_DELETE)
66836    pBt->btsFlags |= BTS_SECURE_DELETE;
66837#elif defined(SQLITE_FAST_SECURE_DELETE)
66838    pBt->btsFlags |= BTS_OVERWRITE;
66839#endif
66840    /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
66841    ** determined by the 2-byte integer located at an offset of 16 bytes from
66842    ** the beginning of the database file. */
66843    pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
66844    if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
66845         || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
66846      pBt->pageSize = 0;
66847#ifndef SQLITE_OMIT_AUTOVACUUM
66848      /* If the magic name ":memory:" will create an in-memory database, then
66849      ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
66850      ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
66851      ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
66852      ** regular file-name. In this case the auto-vacuum applies as per normal.
66853      */
66854      if( zFilename && !isMemdb ){
66855        pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
66856        pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
66857      }
66858#endif
66859      nReserve = 0;
66860    }else{
66861      /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
66862      ** determined by the one-byte unsigned integer found at an offset of 20
66863      ** into the database file header. */
66864      nReserve = zDbHeader[20];
66865      pBt->btsFlags |= BTS_PAGESIZE_FIXED;
66866#ifndef SQLITE_OMIT_AUTOVACUUM
66867      pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
66868      pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
66869#endif
66870    }
66871    rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
66872    if( rc ) goto btree_open_out;
66873    pBt->usableSize = pBt->pageSize - nReserve;
66874    assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
66875
66876#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
66877    /* Add the new BtShared object to the linked list sharable BtShareds.
66878    */
66879    pBt->nRef = 1;
66880    if( p->sharable ){
66881      MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
66882      MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
66883      if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
66884        pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
66885        if( pBt->mutex==0 ){
66886          rc = SQLITE_NOMEM_BKPT;
66887          goto btree_open_out;
66888        }
66889      }
66890      sqlite3_mutex_enter(mutexShared);
66891      pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
66892      GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
66893      sqlite3_mutex_leave(mutexShared);
66894    }
66895#endif
66896  }
66897
66898#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
66899  /* If the new Btree uses a sharable pBtShared, then link the new
66900  ** Btree into the list of all sharable Btrees for the same connection.
66901  ** The list is kept in ascending order by pBt address.
66902  */
66903  if( p->sharable ){
66904    int i;
66905    Btree *pSib;
66906    for(i=0; i<db->nDb; i++){
66907      if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
66908        while( pSib->pPrev ){ pSib = pSib->pPrev; }
66909        if( (uptr)p->pBt<(uptr)pSib->pBt ){
66910          p->pNext = pSib;
66911          p->pPrev = 0;
66912          pSib->pPrev = p;
66913        }else{
66914          while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
66915            pSib = pSib->pNext;
66916          }
66917          p->pNext = pSib->pNext;
66918          p->pPrev = pSib;
66919          if( p->pNext ){
66920            p->pNext->pPrev = p;
66921          }
66922          pSib->pNext = p;
66923        }
66924        break;
66925      }
66926    }
66927  }
66928#endif
66929  *ppBtree = p;
66930
66931btree_open_out:
66932  if( rc!=SQLITE_OK ){
66933    if( pBt && pBt->pPager ){
66934      sqlite3PagerClose(pBt->pPager, 0);
66935    }
66936    sqlite3_free(pBt);
66937    sqlite3_free(p);
66938    *ppBtree = 0;
66939  }else{
66940    sqlite3_file *pFile;
66941
66942    /* If the B-Tree was successfully opened, set the pager-cache size to the
66943    ** default value. Except, when opening on an existing shared pager-cache,
66944    ** do not change the pager-cache size.
66945    */
66946    if( sqlite3BtreeSchema(p, 0, 0)==0 ){
66947      sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
66948    }
66949
66950    pFile = sqlite3PagerFile(pBt->pPager);
66951    if( pFile->pMethods ){
66952      sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
66953    }
66954  }
66955  if( mutexOpen ){
66956    assert( sqlite3_mutex_held(mutexOpen) );
66957    sqlite3_mutex_leave(mutexOpen);
66958  }
66959  assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 );
66960  return rc;
66961}
66962
66963/*
66964** Decrement the BtShared.nRef counter.  When it reaches zero,
66965** remove the BtShared structure from the sharing list.  Return
66966** true if the BtShared.nRef counter reaches zero and return
66967** false if it is still positive.
66968*/
66969static int removeFromSharingList(BtShared *pBt){
66970#ifndef SQLITE_OMIT_SHARED_CACHE
66971  MUTEX_LOGIC( sqlite3_mutex *pMaster; )
66972  BtShared *pList;
66973  int removed = 0;
66974
66975  assert( sqlite3_mutex_notheld(pBt->mutex) );
66976  MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
66977  sqlite3_mutex_enter(pMaster);
66978  pBt->nRef--;
66979  if( pBt->nRef<=0 ){
66980    if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
66981      GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
66982    }else{
66983      pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
66984      while( ALWAYS(pList) && pList->pNext!=pBt ){
66985        pList=pList->pNext;
66986      }
66987      if( ALWAYS(pList) ){
66988        pList->pNext = pBt->pNext;
66989      }
66990    }
66991    if( SQLITE_THREADSAFE ){
66992      sqlite3_mutex_free(pBt->mutex);
66993    }
66994    removed = 1;
66995  }
66996  sqlite3_mutex_leave(pMaster);
66997  return removed;
66998#else
66999  return 1;
67000#endif
67001}
67002
67003/*
67004** Make sure pBt->pTmpSpace points to an allocation of
67005** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
67006** pointer.
67007*/
67008static void allocateTempSpace(BtShared *pBt){
67009  if( !pBt->pTmpSpace ){
67010    pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
67011
67012    /* One of the uses of pBt->pTmpSpace is to format cells before
67013    ** inserting them into a leaf page (function fillInCell()). If
67014    ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
67015    ** by the various routines that manipulate binary cells. Which
67016    ** can mean that fillInCell() only initializes the first 2 or 3
67017    ** bytes of pTmpSpace, but that the first 4 bytes are copied from
67018    ** it into a database page. This is not actually a problem, but it
67019    ** does cause a valgrind error when the 1 or 2 bytes of unitialized
67020    ** data is passed to system call write(). So to avoid this error,
67021    ** zero the first 4 bytes of temp space here.
67022    **
67023    ** Also:  Provide four bytes of initialized space before the
67024    ** beginning of pTmpSpace as an area available to prepend the
67025    ** left-child pointer to the beginning of a cell.
67026    */
67027    if( pBt->pTmpSpace ){
67028      memset(pBt->pTmpSpace, 0, 8);
67029      pBt->pTmpSpace += 4;
67030    }
67031  }
67032}
67033
67034/*
67035** Free the pBt->pTmpSpace allocation
67036*/
67037static void freeTempSpace(BtShared *pBt){
67038  if( pBt->pTmpSpace ){
67039    pBt->pTmpSpace -= 4;
67040    sqlite3PageFree(pBt->pTmpSpace);
67041    pBt->pTmpSpace = 0;
67042  }
67043}
67044
67045/*
67046** Close an open database and invalidate all cursors.
67047*/
67048SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
67049  BtShared *pBt = p->pBt;
67050  BtCursor *pCur;
67051
67052  /* Close all cursors opened via this handle.  */
67053  assert( sqlite3_mutex_held(p->db->mutex) );
67054  sqlite3BtreeEnter(p);
67055  pCur = pBt->pCursor;
67056  while( pCur ){
67057    BtCursor *pTmp = pCur;
67058    pCur = pCur->pNext;
67059    if( pTmp->pBtree==p ){
67060      sqlite3BtreeCloseCursor(pTmp);
67061    }
67062  }
67063
67064  /* Rollback any active transaction and free the handle structure.
67065  ** The call to sqlite3BtreeRollback() drops any table-locks held by
67066  ** this handle.
67067  */
67068  sqlite3BtreeRollback(p, SQLITE_OK, 0);
67069  sqlite3BtreeLeave(p);
67070
67071  /* If there are still other outstanding references to the shared-btree
67072  ** structure, return now. The remainder of this procedure cleans
67073  ** up the shared-btree.
67074  */
67075  assert( p->wantToLock==0 && p->locked==0 );
67076  if( !p->sharable || removeFromSharingList(pBt) ){
67077    /* The pBt is no longer on the sharing list, so we can access
67078    ** it without having to hold the mutex.
67079    **
67080    ** Clean out and delete the BtShared object.
67081    */
67082    assert( !pBt->pCursor );
67083    sqlite3PagerClose(pBt->pPager, p->db);
67084    if( pBt->xFreeSchema && pBt->pSchema ){
67085      pBt->xFreeSchema(pBt->pSchema);
67086    }
67087    sqlite3DbFree(0, pBt->pSchema);
67088    freeTempSpace(pBt);
67089    sqlite3_free(pBt);
67090  }
67091
67092#ifndef SQLITE_OMIT_SHARED_CACHE
67093  assert( p->wantToLock==0 );
67094  assert( p->locked==0 );
67095  if( p->pPrev ) p->pPrev->pNext = p->pNext;
67096  if( p->pNext ) p->pNext->pPrev = p->pPrev;
67097#endif
67098
67099  sqlite3_free(p);
67100  return SQLITE_OK;
67101}
67102
67103/*
67104** Change the "soft" limit on the number of pages in the cache.
67105** Unused and unmodified pages will be recycled when the number of
67106** pages in the cache exceeds this soft limit.  But the size of the
67107** cache is allowed to grow larger than this limit if it contains
67108** dirty pages or pages still in active use.
67109*/
67110SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
67111  BtShared *pBt = p->pBt;
67112  assert( sqlite3_mutex_held(p->db->mutex) );
67113  sqlite3BtreeEnter(p);
67114  sqlite3PagerSetCachesize(pBt->pPager, mxPage);
67115  sqlite3BtreeLeave(p);
67116  return SQLITE_OK;
67117}
67118
67119/*
67120** Change the "spill" limit on the number of pages in the cache.
67121** If the number of pages exceeds this limit during a write transaction,
67122** the pager might attempt to "spill" pages to the journal early in
67123** order to free up memory.
67124**
67125** The value returned is the current spill size.  If zero is passed
67126** as an argument, no changes are made to the spill size setting, so
67127** using mxPage of 0 is a way to query the current spill size.
67128*/
67129SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
67130  BtShared *pBt = p->pBt;
67131  int res;
67132  assert( sqlite3_mutex_held(p->db->mutex) );
67133  sqlite3BtreeEnter(p);
67134  res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
67135  sqlite3BtreeLeave(p);
67136  return res;
67137}
67138
67139#if SQLITE_MAX_MMAP_SIZE>0
67140/*
67141** Change the limit on the amount of the database file that may be
67142** memory mapped.
67143*/
67144SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
67145  BtShared *pBt = p->pBt;
67146  assert( sqlite3_mutex_held(p->db->mutex) );
67147  sqlite3BtreeEnter(p);
67148  sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
67149  sqlite3BtreeLeave(p);
67150  return SQLITE_OK;
67151}
67152#endif /* SQLITE_MAX_MMAP_SIZE>0 */
67153
67154/*
67155** Change the way data is synced to disk in order to increase or decrease
67156** how well the database resists damage due to OS crashes and power
67157** failures.  Level 1 is the same as asynchronous (no syncs() occur and
67158** there is a high probability of damage)  Level 2 is the default.  There
67159** is a very low but non-zero probability of damage.  Level 3 reduces the
67160** probability of damage to near zero but with a write performance reduction.
67161*/
67162#ifndef SQLITE_OMIT_PAGER_PRAGMAS
67163SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(
67164  Btree *p,              /* The btree to set the safety level on */
67165  unsigned pgFlags       /* Various PAGER_* flags */
67166){
67167  BtShared *pBt = p->pBt;
67168  assert( sqlite3_mutex_held(p->db->mutex) );
67169  sqlite3BtreeEnter(p);
67170  sqlite3PagerSetFlags(pBt->pPager, pgFlags);
67171  sqlite3BtreeLeave(p);
67172  return SQLITE_OK;
67173}
67174#endif
67175
67176/*
67177** Change the default pages size and the number of reserved bytes per page.
67178** Or, if the page size has already been fixed, return SQLITE_READONLY
67179** without changing anything.
67180**
67181** The page size must be a power of 2 between 512 and 65536.  If the page
67182** size supplied does not meet this constraint then the page size is not
67183** changed.
67184**
67185** Page sizes are constrained to be a power of two so that the region
67186** of the database file used for locking (beginning at PENDING_BYTE,
67187** the first byte past the 1GB boundary, 0x40000000) needs to occur
67188** at the beginning of a page.
67189**
67190** If parameter nReserve is less than zero, then the number of reserved
67191** bytes per page is left unchanged.
67192**
67193** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
67194** and autovacuum mode can no longer be changed.
67195*/
67196SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
67197  int rc = SQLITE_OK;
67198  int x;
67199  BtShared *pBt = p->pBt;
67200  assert( nReserve>=0 && nReserve<=255 );
67201  sqlite3BtreeEnter(p);
67202  pBt->nReserveWanted = nReserve;
67203  x = pBt->pageSize - pBt->usableSize;
67204  if( nReserve<x ) nReserve = x;
67205  if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
67206    sqlite3BtreeLeave(p);
67207    return SQLITE_READONLY;
67208  }
67209  assert( nReserve>=0 && nReserve<=255 );
67210  if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
67211        ((pageSize-1)&pageSize)==0 ){
67212    assert( (pageSize & 7)==0 );
67213    assert( !pBt->pCursor );
67214    pBt->pageSize = (u32)pageSize;
67215    freeTempSpace(pBt);
67216  }
67217  rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
67218  pBt->usableSize = pBt->pageSize - (u16)nReserve;
67219  if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
67220  sqlite3BtreeLeave(p);
67221  return rc;
67222}
67223
67224/*
67225** Return the currently defined page size
67226*/
67227SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
67228  return p->pBt->pageSize;
67229}
67230
67231/*
67232** This function is similar to sqlite3BtreeGetReserve(), except that it
67233** may only be called if it is guaranteed that the b-tree mutex is already
67234** held.
67235**
67236** This is useful in one special case in the backup API code where it is
67237** known that the shared b-tree mutex is held, but the mutex on the
67238** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
67239** were to be called, it might collide with some other operation on the
67240** database handle that owns *p, causing undefined behavior.
67241*/
67242SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
67243  int n;
67244  assert( sqlite3_mutex_held(p->pBt->mutex) );
67245  n = p->pBt->pageSize - p->pBt->usableSize;
67246  return n;
67247}
67248
67249/*
67250** Return the number of bytes of space at the end of every page that
67251** are intentually left unused.  This is the "reserved" space that is
67252** sometimes used by extensions.
67253**
67254** The value returned is the larger of the current reserve size and
67255** the latest reserve size requested by SQLITE_FILECTRL_RESERVE_BYTES.
67256** The amount of reserve can only grow - never shrink.
67257*/
67258SQLITE_PRIVATE int sqlite3BtreeGetRequestedReserve(Btree *p){
67259  int n1, n2;
67260  sqlite3BtreeEnter(p);
67261  n1 = (int)p->pBt->nReserveWanted;
67262  n2 = sqlite3BtreeGetReserveNoMutex(p);
67263  sqlite3BtreeLeave(p);
67264  return n1>n2 ? n1 : n2;
67265}
67266
67267
67268/*
67269** Set the maximum page count for a database if mxPage is positive.
67270** No changes are made if mxPage is 0 or negative.
67271** Regardless of the value of mxPage, return the maximum page count.
67272*/
67273SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
67274  int n;
67275  sqlite3BtreeEnter(p);
67276  n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
67277  sqlite3BtreeLeave(p);
67278  return n;
67279}
67280
67281/*
67282** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags:
67283**
67284**    newFlag==0       Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared
67285**    newFlag==1       BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared
67286**    newFlag==2       BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set
67287**    newFlag==(-1)    No changes
67288**
67289** This routine acts as a query if newFlag is less than zero
67290**
67291** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but
67292** freelist leaf pages are not written back to the database.  Thus in-page
67293** deleted content is cleared, but freelist deleted content is not.
67294**
67295** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition
67296** that freelist leaf pages are written back into the database, increasing
67297** the amount of disk I/O.
67298*/
67299SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
67300  int b;
67301  if( p==0 ) return 0;
67302  sqlite3BtreeEnter(p);
67303  assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 );
67304  assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) );
67305  if( newFlag>=0 ){
67306    p->pBt->btsFlags &= ~BTS_FAST_SECURE;
67307    p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
67308  }
67309  b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
67310  sqlite3BtreeLeave(p);
67311  return b;
67312}
67313
67314/*
67315** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
67316** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
67317** is disabled. The default value for the auto-vacuum property is
67318** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
67319*/
67320SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
67321#ifdef SQLITE_OMIT_AUTOVACUUM
67322  return SQLITE_READONLY;
67323#else
67324  BtShared *pBt = p->pBt;
67325  int rc = SQLITE_OK;
67326  u8 av = (u8)autoVacuum;
67327
67328  sqlite3BtreeEnter(p);
67329  if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
67330    rc = SQLITE_READONLY;
67331  }else{
67332    pBt->autoVacuum = av ?1:0;
67333    pBt->incrVacuum = av==2 ?1:0;
67334  }
67335  sqlite3BtreeLeave(p);
67336  return rc;
67337#endif
67338}
67339
67340/*
67341** Return the value of the 'auto-vacuum' property. If auto-vacuum is
67342** enabled 1 is returned. Otherwise 0.
67343*/
67344SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
67345#ifdef SQLITE_OMIT_AUTOVACUUM
67346  return BTREE_AUTOVACUUM_NONE;
67347#else
67348  int rc;
67349  sqlite3BtreeEnter(p);
67350  rc = (
67351    (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
67352    (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
67353    BTREE_AUTOVACUUM_INCR
67354  );
67355  sqlite3BtreeLeave(p);
67356  return rc;
67357#endif
67358}
67359
67360/*
67361** If the user has not set the safety-level for this database connection
67362** using "PRAGMA synchronous", and if the safety-level is not already
67363** set to the value passed to this function as the second parameter,
67364** set it so.
67365*/
67366#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \
67367    && !defined(SQLITE_OMIT_WAL)
67368static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
67369  sqlite3 *db;
67370  Db *pDb;
67371  if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
67372    while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
67373    if( pDb->bSyncSet==0
67374     && pDb->safety_level!=safety_level
67375     && pDb!=&db->aDb[1]
67376    ){
67377      pDb->safety_level = safety_level;
67378      sqlite3PagerSetFlags(pBt->pPager,
67379          pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
67380    }
67381  }
67382}
67383#else
67384# define setDefaultSyncFlag(pBt,safety_level)
67385#endif
67386
67387/* Forward declaration */
67388static int newDatabase(BtShared*);
67389
67390
67391/*
67392** Get a reference to pPage1 of the database file.  This will
67393** also acquire a readlock on that file.
67394**
67395** SQLITE_OK is returned on success.  If the file is not a
67396** well-formed database file, then SQLITE_CORRUPT is returned.
67397** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
67398** is returned if we run out of memory.
67399*/
67400static int lockBtree(BtShared *pBt){
67401  int rc;              /* Result code from subfunctions */
67402  MemPage *pPage1;     /* Page 1 of the database file */
67403  u32 nPage;           /* Number of pages in the database */
67404  u32 nPageFile = 0;   /* Number of pages in the database file */
67405  u32 nPageHeader;     /* Number of pages in the database according to hdr */
67406
67407  assert( sqlite3_mutex_held(pBt->mutex) );
67408  assert( pBt->pPage1==0 );
67409  rc = sqlite3PagerSharedLock(pBt->pPager);
67410  if( rc!=SQLITE_OK ) return rc;
67411  rc = btreeGetPage(pBt, 1, &pPage1, 0);
67412  if( rc!=SQLITE_OK ) return rc;
67413
67414  /* Do some checking to help insure the file we opened really is
67415  ** a valid database file.
67416  */
67417  nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
67418  sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
67419  if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
67420    nPage = nPageFile;
67421  }
67422  if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
67423    nPage = 0;
67424  }
67425  if( nPage>0 ){
67426    u32 pageSize;
67427    u32 usableSize;
67428    u8 *page1 = pPage1->aData;
67429    rc = SQLITE_NOTADB;
67430    /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
67431    ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
67432    ** 61 74 20 33 00. */
67433    if( memcmp(page1, zMagicHeader, 16)!=0 ){
67434      goto page1_init_failed;
67435    }
67436
67437#ifdef SQLITE_OMIT_WAL
67438    if( page1[18]>1 ){
67439      pBt->btsFlags |= BTS_READ_ONLY;
67440    }
67441    if( page1[19]>1 ){
67442      goto page1_init_failed;
67443    }
67444#else
67445    if( page1[18]>2 ){
67446      pBt->btsFlags |= BTS_READ_ONLY;
67447    }
67448    if( page1[19]>2 ){
67449      goto page1_init_failed;
67450    }
67451
67452    /* If the write version is set to 2, this database should be accessed
67453    ** in WAL mode. If the log is not already open, open it now. Then
67454    ** return SQLITE_OK and return without populating BtShared.pPage1.
67455    ** The caller detects this and calls this function again. This is
67456    ** required as the version of page 1 currently in the page1 buffer
67457    ** may not be the latest version - there may be a newer one in the log
67458    ** file.
67459    */
67460    if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
67461      int isOpen = 0;
67462      rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
67463      if( rc!=SQLITE_OK ){
67464        goto page1_init_failed;
67465      }else{
67466        setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
67467        if( isOpen==0 ){
67468          releasePageOne(pPage1);
67469          return SQLITE_OK;
67470        }
67471      }
67472      rc = SQLITE_NOTADB;
67473    }else{
67474      setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
67475    }
67476#endif
67477
67478    /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
67479    ** fractions and the leaf payload fraction values must be 64, 32, and 32.
67480    **
67481    ** The original design allowed these amounts to vary, but as of
67482    ** version 3.6.0, we require them to be fixed.
67483    */
67484    if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
67485      goto page1_init_failed;
67486    }
67487    /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
67488    ** determined by the 2-byte integer located at an offset of 16 bytes from
67489    ** the beginning of the database file. */
67490    pageSize = (page1[16]<<8) | (page1[17]<<16);
67491    /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
67492    ** between 512 and 65536 inclusive. */
67493    if( ((pageSize-1)&pageSize)!=0
67494     || pageSize>SQLITE_MAX_PAGE_SIZE
67495     || pageSize<=256
67496    ){
67497      goto page1_init_failed;
67498    }
67499    pBt->btsFlags |= BTS_PAGESIZE_FIXED;
67500    assert( (pageSize & 7)==0 );
67501    /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
67502    ** integer at offset 20 is the number of bytes of space at the end of
67503    ** each page to reserve for extensions.
67504    **
67505    ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
67506    ** determined by the one-byte unsigned integer found at an offset of 20
67507    ** into the database file header. */
67508    usableSize = pageSize - page1[20];
67509    if( (u32)pageSize!=pBt->pageSize ){
67510      /* After reading the first page of the database assuming a page size
67511      ** of BtShared.pageSize, we have discovered that the page-size is
67512      ** actually pageSize. Unlock the database, leave pBt->pPage1 at
67513      ** zero and return SQLITE_OK. The caller will call this function
67514      ** again with the correct page-size.
67515      */
67516      releasePageOne(pPage1);
67517      pBt->usableSize = usableSize;
67518      pBt->pageSize = pageSize;
67519      freeTempSpace(pBt);
67520      rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
67521                                   pageSize-usableSize);
67522      return rc;
67523    }
67524    if( sqlite3WritableSchema(pBt->db)==0 && nPage>nPageFile ){
67525      rc = SQLITE_CORRUPT_BKPT;
67526      goto page1_init_failed;
67527    }
67528    /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
67529    ** be less than 480. In other words, if the page size is 512, then the
67530    ** reserved space size cannot exceed 32. */
67531    if( usableSize<480 ){
67532      goto page1_init_failed;
67533    }
67534    pBt->pageSize = pageSize;
67535    pBt->usableSize = usableSize;
67536#ifndef SQLITE_OMIT_AUTOVACUUM
67537    pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
67538    pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
67539#endif
67540  }
67541
67542  /* maxLocal is the maximum amount of payload to store locally for
67543  ** a cell.  Make sure it is small enough so that at least minFanout
67544  ** cells can will fit on one page.  We assume a 10-byte page header.
67545  ** Besides the payload, the cell must store:
67546  **     2-byte pointer to the cell
67547  **     4-byte child pointer
67548  **     9-byte nKey value
67549  **     4-byte nData value
67550  **     4-byte overflow page pointer
67551  ** So a cell consists of a 2-byte pointer, a header which is as much as
67552  ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
67553  ** page pointer.
67554  */
67555  pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
67556  pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
67557  pBt->maxLeaf = (u16)(pBt->usableSize - 35);
67558  pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
67559  if( pBt->maxLocal>127 ){
67560    pBt->max1bytePayload = 127;
67561  }else{
67562    pBt->max1bytePayload = (u8)pBt->maxLocal;
67563  }
67564  assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
67565  pBt->pPage1 = pPage1;
67566  pBt->nPage = nPage;
67567  return SQLITE_OK;
67568
67569page1_init_failed:
67570  releasePageOne(pPage1);
67571  pBt->pPage1 = 0;
67572  return rc;
67573}
67574
67575#ifndef NDEBUG
67576/*
67577** Return the number of cursors open on pBt. This is for use
67578** in assert() expressions, so it is only compiled if NDEBUG is not
67579** defined.
67580**
67581** Only write cursors are counted if wrOnly is true.  If wrOnly is
67582** false then all cursors are counted.
67583**
67584** For the purposes of this routine, a cursor is any cursor that
67585** is capable of reading or writing to the database.  Cursors that
67586** have been tripped into the CURSOR_FAULT state are not counted.
67587*/
67588static int countValidCursors(BtShared *pBt, int wrOnly){
67589  BtCursor *pCur;
67590  int r = 0;
67591  for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
67592    if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
67593     && pCur->eState!=CURSOR_FAULT ) r++;
67594  }
67595  return r;
67596}
67597#endif
67598
67599/*
67600** If there are no outstanding cursors and we are not in the middle
67601** of a transaction but there is a read lock on the database, then
67602** this routine unrefs the first page of the database file which
67603** has the effect of releasing the read lock.
67604**
67605** If there is a transaction in progress, this routine is a no-op.
67606*/
67607static void unlockBtreeIfUnused(BtShared *pBt){
67608  assert( sqlite3_mutex_held(pBt->mutex) );
67609  assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
67610  if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
67611    MemPage *pPage1 = pBt->pPage1;
67612    assert( pPage1->aData );
67613    assert( sqlite3PagerRefcount(pBt->pPager)==1 );
67614    pBt->pPage1 = 0;
67615    releasePageOne(pPage1);
67616  }
67617}
67618
67619/*
67620** If pBt points to an empty file then convert that empty file
67621** into a new empty database by initializing the first page of
67622** the database.
67623*/
67624static int newDatabase(BtShared *pBt){
67625  MemPage *pP1;
67626  unsigned char *data;
67627  int rc;
67628
67629  assert( sqlite3_mutex_held(pBt->mutex) );
67630  if( pBt->nPage>0 ){
67631    return SQLITE_OK;
67632  }
67633  pP1 = pBt->pPage1;
67634  assert( pP1!=0 );
67635  data = pP1->aData;
67636  rc = sqlite3PagerWrite(pP1->pDbPage);
67637  if( rc ) return rc;
67638  memcpy(data, zMagicHeader, sizeof(zMagicHeader));
67639  assert( sizeof(zMagicHeader)==16 );
67640  data[16] = (u8)((pBt->pageSize>>8)&0xff);
67641  data[17] = (u8)((pBt->pageSize>>16)&0xff);
67642  data[18] = 1;
67643  data[19] = 1;
67644  assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
67645  data[20] = (u8)(pBt->pageSize - pBt->usableSize);
67646  data[21] = 64;
67647  data[22] = 32;
67648  data[23] = 32;
67649  memset(&data[24], 0, 100-24);
67650  zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
67651  pBt->btsFlags |= BTS_PAGESIZE_FIXED;
67652#ifndef SQLITE_OMIT_AUTOVACUUM
67653  assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
67654  assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
67655  put4byte(&data[36 + 4*4], pBt->autoVacuum);
67656  put4byte(&data[36 + 7*4], pBt->incrVacuum);
67657#endif
67658  pBt->nPage = 1;
67659  data[31] = 1;
67660  return SQLITE_OK;
67661}
67662
67663/*
67664** Initialize the first page of the database file (creating a database
67665** consisting of a single page and no schema objects). Return SQLITE_OK
67666** if successful, or an SQLite error code otherwise.
67667*/
67668SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){
67669  int rc;
67670  sqlite3BtreeEnter(p);
67671  p->pBt->nPage = 0;
67672  rc = newDatabase(p->pBt);
67673  sqlite3BtreeLeave(p);
67674  return rc;
67675}
67676
67677/*
67678** Attempt to start a new transaction. A write-transaction
67679** is started if the second argument is nonzero, otherwise a read-
67680** transaction.  If the second argument is 2 or more and exclusive
67681** transaction is started, meaning that no other process is allowed
67682** to access the database.  A preexisting transaction may not be
67683** upgraded to exclusive by calling this routine a second time - the
67684** exclusivity flag only works for a new transaction.
67685**
67686** A write-transaction must be started before attempting any
67687** changes to the database.  None of the following routines
67688** will work unless a transaction is started first:
67689**
67690**      sqlite3BtreeCreateTable()
67691**      sqlite3BtreeCreateIndex()
67692**      sqlite3BtreeClearTable()
67693**      sqlite3BtreeDropTable()
67694**      sqlite3BtreeInsert()
67695**      sqlite3BtreeDelete()
67696**      sqlite3BtreeUpdateMeta()
67697**
67698** If an initial attempt to acquire the lock fails because of lock contention
67699** and the database was previously unlocked, then invoke the busy handler
67700** if there is one.  But if there was previously a read-lock, do not
67701** invoke the busy handler - just return SQLITE_BUSY.  SQLITE_BUSY is
67702** returned when there is already a read-lock in order to avoid a deadlock.
67703**
67704** Suppose there are two processes A and B.  A has a read lock and B has
67705** a reserved lock.  B tries to promote to exclusive but is blocked because
67706** of A's read lock.  A tries to promote to reserved but is blocked by B.
67707** One or the other of the two processes must give way or there can be
67708** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
67709** when A already has a read lock, we encourage A to give up and let B
67710** proceed.
67711*/
67712SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
67713  BtShared *pBt = p->pBt;
67714  Pager *pPager = pBt->pPager;
67715  int rc = SQLITE_OK;
67716
67717  sqlite3BtreeEnter(p);
67718  btreeIntegrity(p);
67719
67720  /* If the btree is already in a write-transaction, or it
67721  ** is already in a read-transaction and a read-transaction
67722  ** is requested, this is a no-op.
67723  */
67724  if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
67725    goto trans_begun;
67726  }
67727  assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
67728
67729  if( (p->db->flags & SQLITE_ResetDatabase)
67730   && sqlite3PagerIsreadonly(pPager)==0
67731  ){
67732    pBt->btsFlags &= ~BTS_READ_ONLY;
67733  }
67734
67735  /* Write transactions are not possible on a read-only database */
67736  if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
67737    rc = SQLITE_READONLY;
67738    goto trans_begun;
67739  }
67740
67741#ifndef SQLITE_OMIT_SHARED_CACHE
67742  {
67743    sqlite3 *pBlock = 0;
67744    /* If another database handle has already opened a write transaction
67745    ** on this shared-btree structure and a second write transaction is
67746    ** requested, return SQLITE_LOCKED.
67747    */
67748    if( (wrflag && pBt->inTransaction==TRANS_WRITE)
67749     || (pBt->btsFlags & BTS_PENDING)!=0
67750    ){
67751      pBlock = pBt->pWriter->db;
67752    }else if( wrflag>1 ){
67753      BtLock *pIter;
67754      for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
67755        if( pIter->pBtree!=p ){
67756          pBlock = pIter->pBtree->db;
67757          break;
67758        }
67759      }
67760    }
67761    if( pBlock ){
67762      sqlite3ConnectionBlocked(p->db, pBlock);
67763      rc = SQLITE_LOCKED_SHAREDCACHE;
67764      goto trans_begun;
67765    }
67766  }
67767#endif
67768
67769  /* Any read-only or read-write transaction implies a read-lock on
67770  ** page 1. So if some other shared-cache client already has a write-lock
67771  ** on page 1, the transaction cannot be opened. */
67772  rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
67773  if( SQLITE_OK!=rc ) goto trans_begun;
67774
67775  pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
67776  if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
67777  do {
67778    sqlite3PagerWalDb(pPager, p->db);
67779
67780#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
67781    /* If transitioning from no transaction directly to a write transaction,
67782    ** block for the WRITER lock first if possible. */
67783    if( pBt->pPage1==0 && wrflag ){
67784      assert( pBt->inTransaction==TRANS_NONE );
67785      rc = sqlite3PagerWalWriteLock(pPager, 1);
67786      if( rc!=SQLITE_BUSY && rc!=SQLITE_OK ) break;
67787    }
67788#endif
67789
67790    /* Call lockBtree() until either pBt->pPage1 is populated or
67791    ** lockBtree() returns something other than SQLITE_OK. lockBtree()
67792    ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
67793    ** reading page 1 it discovers that the page-size of the database
67794    ** file is not pBt->pageSize. In this case lockBtree() will update
67795    ** pBt->pageSize to the page-size of the file on disk.
67796    */
67797    while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
67798
67799    if( rc==SQLITE_OK && wrflag ){
67800      if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
67801        rc = SQLITE_READONLY;
67802      }else{
67803        rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
67804        if( rc==SQLITE_OK ){
67805          rc = newDatabase(pBt);
67806        }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
67807          /* if there was no transaction opened when this function was
67808          ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
67809          ** code to SQLITE_BUSY. */
67810          rc = SQLITE_BUSY;
67811        }
67812      }
67813    }
67814
67815    if( rc!=SQLITE_OK ){
67816      (void)sqlite3PagerWalWriteLock(pPager, 0);
67817      unlockBtreeIfUnused(pBt);
67818    }
67819  }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
67820          btreeInvokeBusyHandler(pBt) );
67821  sqlite3PagerWalDb(pPager, 0);
67822#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
67823  if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY;
67824#endif
67825
67826  if( rc==SQLITE_OK ){
67827    if( p->inTrans==TRANS_NONE ){
67828      pBt->nTransaction++;
67829#ifndef SQLITE_OMIT_SHARED_CACHE
67830      if( p->sharable ){
67831        assert( p->lock.pBtree==p && p->lock.iTable==1 );
67832        p->lock.eLock = READ_LOCK;
67833        p->lock.pNext = pBt->pLock;
67834        pBt->pLock = &p->lock;
67835      }
67836#endif
67837    }
67838    p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
67839    if( p->inTrans>pBt->inTransaction ){
67840      pBt->inTransaction = p->inTrans;
67841    }
67842    if( wrflag ){
67843      MemPage *pPage1 = pBt->pPage1;
67844#ifndef SQLITE_OMIT_SHARED_CACHE
67845      assert( !pBt->pWriter );
67846      pBt->pWriter = p;
67847      pBt->btsFlags &= ~BTS_EXCLUSIVE;
67848      if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
67849#endif
67850
67851      /* If the db-size header field is incorrect (as it may be if an old
67852      ** client has been writing the database file), update it now. Doing
67853      ** this sooner rather than later means the database size can safely
67854      ** re-read the database size from page 1 if a savepoint or transaction
67855      ** rollback occurs within the transaction.
67856      */
67857      if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
67858        rc = sqlite3PagerWrite(pPage1->pDbPage);
67859        if( rc==SQLITE_OK ){
67860          put4byte(&pPage1->aData[28], pBt->nPage);
67861        }
67862      }
67863    }
67864  }
67865
67866trans_begun:
67867  if( rc==SQLITE_OK ){
67868    if( pSchemaVersion ){
67869      *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
67870    }
67871    if( wrflag ){
67872      /* This call makes sure that the pager has the correct number of
67873      ** open savepoints. If the second parameter is greater than 0 and
67874      ** the sub-journal is not already open, then it will be opened here.
67875      */
67876      rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
67877    }
67878  }
67879
67880  btreeIntegrity(p);
67881  sqlite3BtreeLeave(p);
67882  return rc;
67883}
67884
67885#ifndef SQLITE_OMIT_AUTOVACUUM
67886
67887/*
67888** Set the pointer-map entries for all children of page pPage. Also, if
67889** pPage contains cells that point to overflow pages, set the pointer
67890** map entries for the overflow pages as well.
67891*/
67892static int setChildPtrmaps(MemPage *pPage){
67893  int i;                             /* Counter variable */
67894  int nCell;                         /* Number of cells in page pPage */
67895  int rc;                            /* Return code */
67896  BtShared *pBt = pPage->pBt;
67897  Pgno pgno = pPage->pgno;
67898
67899  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67900  rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
67901  if( rc!=SQLITE_OK ) return rc;
67902  nCell = pPage->nCell;
67903
67904  for(i=0; i<nCell; i++){
67905    u8 *pCell = findCell(pPage, i);
67906
67907    ptrmapPutOvflPtr(pPage, pPage, pCell, &rc);
67908
67909    if( !pPage->leaf ){
67910      Pgno childPgno = get4byte(pCell);
67911      ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
67912    }
67913  }
67914
67915  if( !pPage->leaf ){
67916    Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
67917    ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
67918  }
67919
67920  return rc;
67921}
67922
67923/*
67924** Somewhere on pPage is a pointer to page iFrom.  Modify this pointer so
67925** that it points to iTo. Parameter eType describes the type of pointer to
67926** be modified, as  follows:
67927**
67928** PTRMAP_BTREE:     pPage is a btree-page. The pointer points at a child
67929**                   page of pPage.
67930**
67931** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
67932**                   page pointed to by one of the cells on pPage.
67933**
67934** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
67935**                   overflow page in the list.
67936*/
67937static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
67938  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67939  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
67940  if( eType==PTRMAP_OVERFLOW2 ){
67941    /* The pointer is always the first 4 bytes of the page in this case.  */
67942    if( get4byte(pPage->aData)!=iFrom ){
67943      return SQLITE_CORRUPT_PAGE(pPage);
67944    }
67945    put4byte(pPage->aData, iTo);
67946  }else{
67947    int i;
67948    int nCell;
67949    int rc;
67950
67951    rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
67952    if( rc ) return rc;
67953    nCell = pPage->nCell;
67954
67955    for(i=0; i<nCell; i++){
67956      u8 *pCell = findCell(pPage, i);
67957      if( eType==PTRMAP_OVERFLOW1 ){
67958        CellInfo info;
67959        pPage->xParseCell(pPage, pCell, &info);
67960        if( info.nLocal<info.nPayload ){
67961          if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
67962            return SQLITE_CORRUPT_PAGE(pPage);
67963          }
67964          if( iFrom==get4byte(pCell+info.nSize-4) ){
67965            put4byte(pCell+info.nSize-4, iTo);
67966            break;
67967          }
67968        }
67969      }else{
67970        if( get4byte(pCell)==iFrom ){
67971          put4byte(pCell, iTo);
67972          break;
67973        }
67974      }
67975    }
67976
67977    if( i==nCell ){
67978      if( eType!=PTRMAP_BTREE ||
67979          get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
67980        return SQLITE_CORRUPT_PAGE(pPage);
67981      }
67982      put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
67983    }
67984  }
67985  return SQLITE_OK;
67986}
67987
67988
67989/*
67990** Move the open database page pDbPage to location iFreePage in the
67991** database. The pDbPage reference remains valid.
67992**
67993** The isCommit flag indicates that there is no need to remember that
67994** the journal needs to be sync()ed before database page pDbPage->pgno
67995** can be written to. The caller has already promised not to write to that
67996** page.
67997*/
67998static int relocatePage(
67999  BtShared *pBt,           /* Btree */
68000  MemPage *pDbPage,        /* Open page to move */
68001  u8 eType,                /* Pointer map 'type' entry for pDbPage */
68002  Pgno iPtrPage,           /* Pointer map 'page-no' entry for pDbPage */
68003  Pgno iFreePage,          /* The location to move pDbPage to */
68004  int isCommit             /* isCommit flag passed to sqlite3PagerMovepage */
68005){
68006  MemPage *pPtrPage;   /* The page that contains a pointer to pDbPage */
68007  Pgno iDbPage = pDbPage->pgno;
68008  Pager *pPager = pBt->pPager;
68009  int rc;
68010
68011  assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
68012      eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
68013  assert( sqlite3_mutex_held(pBt->mutex) );
68014  assert( pDbPage->pBt==pBt );
68015  if( iDbPage<3 ) return SQLITE_CORRUPT_BKPT;
68016
68017  /* Move page iDbPage from its current location to page number iFreePage */
68018  TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
68019      iDbPage, iFreePage, iPtrPage, eType));
68020  rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
68021  if( rc!=SQLITE_OK ){
68022    return rc;
68023  }
68024  pDbPage->pgno = iFreePage;
68025
68026  /* If pDbPage was a btree-page, then it may have child pages and/or cells
68027  ** that point to overflow pages. The pointer map entries for all these
68028  ** pages need to be changed.
68029  **
68030  ** If pDbPage is an overflow page, then the first 4 bytes may store a
68031  ** pointer to a subsequent overflow page. If this is the case, then
68032  ** the pointer map needs to be updated for the subsequent overflow page.
68033  */
68034  if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
68035    rc = setChildPtrmaps(pDbPage);
68036    if( rc!=SQLITE_OK ){
68037      return rc;
68038    }
68039  }else{
68040    Pgno nextOvfl = get4byte(pDbPage->aData);
68041    if( nextOvfl!=0 ){
68042      ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
68043      if( rc!=SQLITE_OK ){
68044        return rc;
68045      }
68046    }
68047  }
68048
68049  /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
68050  ** that it points at iFreePage. Also fix the pointer map entry for
68051  ** iPtrPage.
68052  */
68053  if( eType!=PTRMAP_ROOTPAGE ){
68054    rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
68055    if( rc!=SQLITE_OK ){
68056      return rc;
68057    }
68058    rc = sqlite3PagerWrite(pPtrPage->pDbPage);
68059    if( rc!=SQLITE_OK ){
68060      releasePage(pPtrPage);
68061      return rc;
68062    }
68063    rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
68064    releasePage(pPtrPage);
68065    if( rc==SQLITE_OK ){
68066      ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
68067    }
68068  }
68069  return rc;
68070}
68071
68072/* Forward declaration required by incrVacuumStep(). */
68073static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
68074
68075/*
68076** Perform a single step of an incremental-vacuum. If successful, return
68077** SQLITE_OK. If there is no work to do (and therefore no point in
68078** calling this function again), return SQLITE_DONE. Or, if an error
68079** occurs, return some other error code.
68080**
68081** More specifically, this function attempts to re-organize the database so
68082** that the last page of the file currently in use is no longer in use.
68083**
68084** Parameter nFin is the number of pages that this database would contain
68085** were this function called until it returns SQLITE_DONE.
68086**
68087** If the bCommit parameter is non-zero, this function assumes that the
68088** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
68089** or an error. bCommit is passed true for an auto-vacuum-on-commit
68090** operation, or false for an incremental vacuum.
68091*/
68092static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
68093  Pgno nFreeList;           /* Number of pages still on the free-list */
68094  int rc;
68095
68096  assert( sqlite3_mutex_held(pBt->mutex) );
68097  assert( iLastPg>nFin );
68098
68099  if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
68100    u8 eType;
68101    Pgno iPtrPage;
68102
68103    nFreeList = get4byte(&pBt->pPage1->aData[36]);
68104    if( nFreeList==0 ){
68105      return SQLITE_DONE;
68106    }
68107
68108    rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
68109    if( rc!=SQLITE_OK ){
68110      return rc;
68111    }
68112    if( eType==PTRMAP_ROOTPAGE ){
68113      return SQLITE_CORRUPT_BKPT;
68114    }
68115
68116    if( eType==PTRMAP_FREEPAGE ){
68117      if( bCommit==0 ){
68118        /* Remove the page from the files free-list. This is not required
68119        ** if bCommit is non-zero. In that case, the free-list will be
68120        ** truncated to zero after this function returns, so it doesn't
68121        ** matter if it still contains some garbage entries.
68122        */
68123        Pgno iFreePg;
68124        MemPage *pFreePg;
68125        rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT);
68126        if( rc!=SQLITE_OK ){
68127          return rc;
68128        }
68129        assert( iFreePg==iLastPg );
68130        releasePage(pFreePg);
68131      }
68132    } else {
68133      Pgno iFreePg;             /* Index of free page to move pLastPg to */
68134      MemPage *pLastPg;
68135      u8 eMode = BTALLOC_ANY;   /* Mode parameter for allocateBtreePage() */
68136      Pgno iNear = 0;           /* nearby parameter for allocateBtreePage() */
68137
68138      rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
68139      if( rc!=SQLITE_OK ){
68140        return rc;
68141      }
68142
68143      /* If bCommit is zero, this loop runs exactly once and page pLastPg
68144      ** is swapped with the first free page pulled off the free list.
68145      **
68146      ** On the other hand, if bCommit is greater than zero, then keep
68147      ** looping until a free-page located within the first nFin pages
68148      ** of the file is found.
68149      */
68150      if( bCommit==0 ){
68151        eMode = BTALLOC_LE;
68152        iNear = nFin;
68153      }
68154      do {
68155        MemPage *pFreePg;
68156        rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
68157        if( rc!=SQLITE_OK ){
68158          releasePage(pLastPg);
68159          return rc;
68160        }
68161        releasePage(pFreePg);
68162      }while( bCommit && iFreePg>nFin );
68163      assert( iFreePg<iLastPg );
68164
68165      rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
68166      releasePage(pLastPg);
68167      if( rc!=SQLITE_OK ){
68168        return rc;
68169      }
68170    }
68171  }
68172
68173  if( bCommit==0 ){
68174    do {
68175      iLastPg--;
68176    }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) );
68177    pBt->bDoTruncate = 1;
68178    pBt->nPage = iLastPg;
68179  }
68180  return SQLITE_OK;
68181}
68182
68183/*
68184** The database opened by the first argument is an auto-vacuum database
68185** nOrig pages in size containing nFree free pages. Return the expected
68186** size of the database in pages following an auto-vacuum operation.
68187*/
68188static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
68189  int nEntry;                     /* Number of entries on one ptrmap page */
68190  Pgno nPtrmap;                   /* Number of PtrMap pages to be freed */
68191  Pgno nFin;                      /* Return value */
68192
68193  nEntry = pBt->usableSize/5;
68194  nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
68195  nFin = nOrig - nFree - nPtrmap;
68196  if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
68197    nFin--;
68198  }
68199  while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
68200    nFin--;
68201  }
68202
68203  return nFin;
68204}
68205
68206/*
68207** A write-transaction must be opened before calling this function.
68208** It performs a single unit of work towards an incremental vacuum.
68209**
68210** If the incremental vacuum is finished after this function has run,
68211** SQLITE_DONE is returned. If it is not finished, but no error occurred,
68212** SQLITE_OK is returned. Otherwise an SQLite error code.
68213*/
68214SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
68215  int rc;
68216  BtShared *pBt = p->pBt;
68217
68218  sqlite3BtreeEnter(p);
68219  assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
68220  if( !pBt->autoVacuum ){
68221    rc = SQLITE_DONE;
68222  }else{
68223    Pgno nOrig = btreePagecount(pBt);
68224    Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
68225    Pgno nFin = finalDbSize(pBt, nOrig, nFree);
68226
68227    if( nOrig<nFin ){
68228      rc = SQLITE_CORRUPT_BKPT;
68229    }else if( nFree>0 ){
68230      rc = saveAllCursors(pBt, 0, 0);
68231      if( rc==SQLITE_OK ){
68232        invalidateAllOverflowCache(pBt);
68233        rc = incrVacuumStep(pBt, nFin, nOrig, 0);
68234      }
68235      if( rc==SQLITE_OK ){
68236        rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
68237        put4byte(&pBt->pPage1->aData[28], pBt->nPage);
68238      }
68239    }else{
68240      rc = SQLITE_DONE;
68241    }
68242  }
68243  sqlite3BtreeLeave(p);
68244  return rc;
68245}
68246
68247/*
68248** This routine is called prior to sqlite3PagerCommit when a transaction
68249** is committed for an auto-vacuum database.
68250**
68251** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
68252** the database file should be truncated to during the commit process.
68253** i.e. the database has been reorganized so that only the first *pnTrunc
68254** pages are in use.
68255*/
68256static int autoVacuumCommit(BtShared *pBt){
68257  int rc = SQLITE_OK;
68258  Pager *pPager = pBt->pPager;
68259  VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); )
68260
68261  assert( sqlite3_mutex_held(pBt->mutex) );
68262  invalidateAllOverflowCache(pBt);
68263  assert(pBt->autoVacuum);
68264  if( !pBt->incrVacuum ){
68265    Pgno nFin;         /* Number of pages in database after autovacuuming */
68266    Pgno nFree;        /* Number of pages on the freelist initially */
68267    Pgno iFree;        /* The next page to be freed */
68268    Pgno nOrig;        /* Database size before freeing */
68269
68270    nOrig = btreePagecount(pBt);
68271    if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
68272      /* It is not possible to create a database for which the final page
68273      ** is either a pointer-map page or the pending-byte page. If one
68274      ** is encountered, this indicates corruption.
68275      */
68276      return SQLITE_CORRUPT_BKPT;
68277    }
68278
68279    nFree = get4byte(&pBt->pPage1->aData[36]);
68280    nFin = finalDbSize(pBt, nOrig, nFree);
68281    if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
68282    if( nFin<nOrig ){
68283      rc = saveAllCursors(pBt, 0, 0);
68284    }
68285    for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
68286      rc = incrVacuumStep(pBt, nFin, iFree, 1);
68287    }
68288    if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
68289      rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
68290      put4byte(&pBt->pPage1->aData[32], 0);
68291      put4byte(&pBt->pPage1->aData[36], 0);
68292      put4byte(&pBt->pPage1->aData[28], nFin);
68293      pBt->bDoTruncate = 1;
68294      pBt->nPage = nFin;
68295    }
68296    if( rc!=SQLITE_OK ){
68297      sqlite3PagerRollback(pPager);
68298    }
68299  }
68300
68301  assert( nRef>=sqlite3PagerRefcount(pPager) );
68302  return rc;
68303}
68304
68305#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
68306# define setChildPtrmaps(x) SQLITE_OK
68307#endif
68308
68309/*
68310** This routine does the first phase of a two-phase commit.  This routine
68311** causes a rollback journal to be created (if it does not already exist)
68312** and populated with enough information so that if a power loss occurs
68313** the database can be restored to its original state by playing back
68314** the journal.  Then the contents of the journal are flushed out to
68315** the disk.  After the journal is safely on oxide, the changes to the
68316** database are written into the database file and flushed to oxide.
68317** At the end of this call, the rollback journal still exists on the
68318** disk and we are still holding all locks, so the transaction has not
68319** committed.  See sqlite3BtreeCommitPhaseTwo() for the second phase of the
68320** commit process.
68321**
68322** This call is a no-op if no write-transaction is currently active on pBt.
68323**
68324** Otherwise, sync the database file for the btree pBt. zMaster points to
68325** the name of a master journal file that should be written into the
68326** individual journal file, or is NULL, indicating no master journal file
68327** (single database transaction).
68328**
68329** When this is called, the master journal should already have been
68330** created, populated with this journal pointer and synced to disk.
68331**
68332** Once this is routine has returned, the only thing required to commit
68333** the write-transaction for this database file is to delete the journal.
68334*/
68335SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
68336  int rc = SQLITE_OK;
68337  if( p->inTrans==TRANS_WRITE ){
68338    BtShared *pBt = p->pBt;
68339    sqlite3BtreeEnter(p);
68340#ifndef SQLITE_OMIT_AUTOVACUUM
68341    if( pBt->autoVacuum ){
68342      rc = autoVacuumCommit(pBt);
68343      if( rc!=SQLITE_OK ){
68344        sqlite3BtreeLeave(p);
68345        return rc;
68346      }
68347    }
68348    if( pBt->bDoTruncate ){
68349      sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
68350    }
68351#endif
68352    rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
68353    sqlite3BtreeLeave(p);
68354  }
68355  return rc;
68356}
68357
68358/*
68359** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
68360** at the conclusion of a transaction.
68361*/
68362static void btreeEndTransaction(Btree *p){
68363  BtShared *pBt = p->pBt;
68364  sqlite3 *db = p->db;
68365  assert( sqlite3BtreeHoldsMutex(p) );
68366
68367#ifndef SQLITE_OMIT_AUTOVACUUM
68368  pBt->bDoTruncate = 0;
68369#endif
68370  if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
68371    /* If there are other active statements that belong to this database
68372    ** handle, downgrade to a read-only transaction. The other statements
68373    ** may still be reading from the database.  */
68374    downgradeAllSharedCacheTableLocks(p);
68375    p->inTrans = TRANS_READ;
68376  }else{
68377    /* If the handle had any kind of transaction open, decrement the
68378    ** transaction count of the shared btree. If the transaction count
68379    ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
68380    ** call below will unlock the pager.  */
68381    if( p->inTrans!=TRANS_NONE ){
68382      clearAllSharedCacheTableLocks(p);
68383      pBt->nTransaction--;
68384      if( 0==pBt->nTransaction ){
68385        pBt->inTransaction = TRANS_NONE;
68386      }
68387    }
68388
68389    /* Set the current transaction state to TRANS_NONE and unlock the
68390    ** pager if this call closed the only read or write transaction.  */
68391    p->inTrans = TRANS_NONE;
68392    unlockBtreeIfUnused(pBt);
68393  }
68394
68395  btreeIntegrity(p);
68396}
68397
68398/*
68399** Commit the transaction currently in progress.
68400**
68401** This routine implements the second phase of a 2-phase commit.  The
68402** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
68403** be invoked prior to calling this routine.  The sqlite3BtreeCommitPhaseOne()
68404** routine did all the work of writing information out to disk and flushing the
68405** contents so that they are written onto the disk platter.  All this
68406** routine has to do is delete or truncate or zero the header in the
68407** the rollback journal (which causes the transaction to commit) and
68408** drop locks.
68409**
68410** Normally, if an error occurs while the pager layer is attempting to
68411** finalize the underlying journal file, this function returns an error and
68412** the upper layer will attempt a rollback. However, if the second argument
68413** is non-zero then this b-tree transaction is part of a multi-file
68414** transaction. In this case, the transaction has already been committed
68415** (by deleting a master journal file) and the caller will ignore this
68416** functions return code. So, even if an error occurs in the pager layer,
68417** reset the b-tree objects internal state to indicate that the write
68418** transaction has been closed. This is quite safe, as the pager will have
68419** transitioned to the error state.
68420**
68421** This will release the write lock on the database file.  If there
68422** are no active cursors, it also releases the read lock.
68423*/
68424SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
68425
68426  if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
68427  sqlite3BtreeEnter(p);
68428  btreeIntegrity(p);
68429
68430  /* If the handle has a write-transaction open, commit the shared-btrees
68431  ** transaction and set the shared state to TRANS_READ.
68432  */
68433  if( p->inTrans==TRANS_WRITE ){
68434    int rc;
68435    BtShared *pBt = p->pBt;
68436    assert( pBt->inTransaction==TRANS_WRITE );
68437    assert( pBt->nTransaction>0 );
68438    rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
68439    if( rc!=SQLITE_OK && bCleanup==0 ){
68440      sqlite3BtreeLeave(p);
68441      return rc;
68442    }
68443    p->iDataVersion--;  /* Compensate for pPager->iDataVersion++; */
68444    pBt->inTransaction = TRANS_READ;
68445    btreeClearHasContent(pBt);
68446  }
68447
68448  btreeEndTransaction(p);
68449  sqlite3BtreeLeave(p);
68450  return SQLITE_OK;
68451}
68452
68453/*
68454** Do both phases of a commit.
68455*/
68456SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
68457  int rc;
68458  sqlite3BtreeEnter(p);
68459  rc = sqlite3BtreeCommitPhaseOne(p, 0);
68460  if( rc==SQLITE_OK ){
68461    rc = sqlite3BtreeCommitPhaseTwo(p, 0);
68462  }
68463  sqlite3BtreeLeave(p);
68464  return rc;
68465}
68466
68467/*
68468** This routine sets the state to CURSOR_FAULT and the error
68469** code to errCode for every cursor on any BtShared that pBtree
68470** references.  Or if the writeOnly flag is set to 1, then only
68471** trip write cursors and leave read cursors unchanged.
68472**
68473** Every cursor is a candidate to be tripped, including cursors
68474** that belong to other database connections that happen to be
68475** sharing the cache with pBtree.
68476**
68477** This routine gets called when a rollback occurs. If the writeOnly
68478** flag is true, then only write-cursors need be tripped - read-only
68479** cursors save their current positions so that they may continue
68480** following the rollback. Or, if writeOnly is false, all cursors are
68481** tripped. In general, writeOnly is false if the transaction being
68482** rolled back modified the database schema. In this case b-tree root
68483** pages may be moved or deleted from the database altogether, making
68484** it unsafe for read cursors to continue.
68485**
68486** If the writeOnly flag is true and an error is encountered while
68487** saving the current position of a read-only cursor, all cursors,
68488** including all read-cursors are tripped.
68489**
68490** SQLITE_OK is returned if successful, or if an error occurs while
68491** saving a cursor position, an SQLite error code.
68492*/
68493SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
68494  BtCursor *p;
68495  int rc = SQLITE_OK;
68496
68497  assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 );
68498  if( pBtree ){
68499    sqlite3BtreeEnter(pBtree);
68500    for(p=pBtree->pBt->pCursor; p; p=p->pNext){
68501      if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
68502        if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
68503          rc = saveCursorPosition(p);
68504          if( rc!=SQLITE_OK ){
68505            (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
68506            break;
68507          }
68508        }
68509      }else{
68510        sqlite3BtreeClearCursor(p);
68511        p->eState = CURSOR_FAULT;
68512        p->skipNext = errCode;
68513      }
68514      btreeReleaseAllCursorPages(p);
68515    }
68516    sqlite3BtreeLeave(pBtree);
68517  }
68518  return rc;
68519}
68520
68521/*
68522** Set the pBt->nPage field correctly, according to the current
68523** state of the database.  Assume pBt->pPage1 is valid.
68524*/
68525static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){
68526  int nPage = get4byte(&pPage1->aData[28]);
68527  testcase( nPage==0 );
68528  if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
68529  testcase( pBt->nPage!=nPage );
68530  pBt->nPage = nPage;
68531}
68532
68533/*
68534** Rollback the transaction in progress.
68535**
68536** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
68537** Only write cursors are tripped if writeOnly is true but all cursors are
68538** tripped if writeOnly is false.  Any attempt to use
68539** a tripped cursor will result in an error.
68540**
68541** This will release the write lock on the database file.  If there
68542** are no active cursors, it also releases the read lock.
68543*/
68544SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
68545  int rc;
68546  BtShared *pBt = p->pBt;
68547  MemPage *pPage1;
68548
68549  assert( writeOnly==1 || writeOnly==0 );
68550  assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK );
68551  sqlite3BtreeEnter(p);
68552  if( tripCode==SQLITE_OK ){
68553    rc = tripCode = saveAllCursors(pBt, 0, 0);
68554    if( rc ) writeOnly = 0;
68555  }else{
68556    rc = SQLITE_OK;
68557  }
68558  if( tripCode ){
68559    int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
68560    assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) );
68561    if( rc2!=SQLITE_OK ) rc = rc2;
68562  }
68563  btreeIntegrity(p);
68564
68565  if( p->inTrans==TRANS_WRITE ){
68566    int rc2;
68567
68568    assert( TRANS_WRITE==pBt->inTransaction );
68569    rc2 = sqlite3PagerRollback(pBt->pPager);
68570    if( rc2!=SQLITE_OK ){
68571      rc = rc2;
68572    }
68573
68574    /* The rollback may have destroyed the pPage1->aData value.  So
68575    ** call btreeGetPage() on page 1 again to make
68576    ** sure pPage1->aData is set correctly. */
68577    if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
68578      btreeSetNPage(pBt, pPage1);
68579      releasePageOne(pPage1);
68580    }
68581    assert( countValidCursors(pBt, 1)==0 );
68582    pBt->inTransaction = TRANS_READ;
68583    btreeClearHasContent(pBt);
68584  }
68585
68586  btreeEndTransaction(p);
68587  sqlite3BtreeLeave(p);
68588  return rc;
68589}
68590
68591/*
68592** Start a statement subtransaction. The subtransaction can be rolled
68593** back independently of the main transaction. You must start a transaction
68594** before starting a subtransaction. The subtransaction is ended automatically
68595** if the main transaction commits or rolls back.
68596**
68597** Statement subtransactions are used around individual SQL statements
68598** that are contained within a BEGIN...COMMIT block.  If a constraint
68599** error occurs within the statement, the effect of that one statement
68600** can be rolled back without having to rollback the entire transaction.
68601**
68602** A statement sub-transaction is implemented as an anonymous savepoint. The
68603** value passed as the second parameter is the total number of savepoints,
68604** including the new anonymous savepoint, open on the B-Tree. i.e. if there
68605** are no active savepoints and no other statement-transactions open,
68606** iStatement is 1. This anonymous savepoint can be released or rolled back
68607** using the sqlite3BtreeSavepoint() function.
68608*/
68609SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
68610  int rc;
68611  BtShared *pBt = p->pBt;
68612  sqlite3BtreeEnter(p);
68613  assert( p->inTrans==TRANS_WRITE );
68614  assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
68615  assert( iStatement>0 );
68616  assert( iStatement>p->db->nSavepoint );
68617  assert( pBt->inTransaction==TRANS_WRITE );
68618  /* At the pager level, a statement transaction is a savepoint with
68619  ** an index greater than all savepoints created explicitly using
68620  ** SQL statements. It is illegal to open, release or rollback any
68621  ** such savepoints while the statement transaction savepoint is active.
68622  */
68623  rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
68624  sqlite3BtreeLeave(p);
68625  return rc;
68626}
68627
68628/*
68629** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
68630** or SAVEPOINT_RELEASE. This function either releases or rolls back the
68631** savepoint identified by parameter iSavepoint, depending on the value
68632** of op.
68633**
68634** Normally, iSavepoint is greater than or equal to zero. However, if op is
68635** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
68636** contents of the entire transaction are rolled back. This is different
68637** from a normal transaction rollback, as no locks are released and the
68638** transaction remains open.
68639*/
68640SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
68641  int rc = SQLITE_OK;
68642  if( p && p->inTrans==TRANS_WRITE ){
68643    BtShared *pBt = p->pBt;
68644    assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
68645    assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
68646    sqlite3BtreeEnter(p);
68647    if( op==SAVEPOINT_ROLLBACK ){
68648      rc = saveAllCursors(pBt, 0, 0);
68649    }
68650    if( rc==SQLITE_OK ){
68651      rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
68652    }
68653    if( rc==SQLITE_OK ){
68654      if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
68655        pBt->nPage = 0;
68656      }
68657      rc = newDatabase(pBt);
68658      btreeSetNPage(pBt, pBt->pPage1);
68659
68660      /* pBt->nPage might be zero if the database was corrupt when
68661      ** the transaction was started. Otherwise, it must be at least 1.  */
68662      assert( CORRUPT_DB || pBt->nPage>0 );
68663    }
68664    sqlite3BtreeLeave(p);
68665  }
68666  return rc;
68667}
68668
68669/*
68670** Create a new cursor for the BTree whose root is on the page
68671** iTable. If a read-only cursor is requested, it is assumed that
68672** the caller already has at least a read-only transaction open
68673** on the database already. If a write-cursor is requested, then
68674** the caller is assumed to have an open write transaction.
68675**
68676** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
68677** be used for reading.  If the BTREE_WRCSR bit is set, then the cursor
68678** can be used for reading or for writing if other conditions for writing
68679** are also met.  These are the conditions that must be met in order
68680** for writing to be allowed:
68681**
68682** 1:  The cursor must have been opened with wrFlag containing BTREE_WRCSR
68683**
68684** 2:  Other database connections that share the same pager cache
68685**     but which are not in the READ_UNCOMMITTED state may not have
68686**     cursors open with wrFlag==0 on the same table.  Otherwise
68687**     the changes made by this write cursor would be visible to
68688**     the read cursors in the other database connection.
68689**
68690** 3:  The database must be writable (not on read-only media)
68691**
68692** 4:  There must be an active transaction.
68693**
68694** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
68695** is set.  If FORDELETE is set, that is a hint to the implementation that
68696** this cursor will only be used to seek to and delete entries of an index
68697** as part of a larger DELETE statement.  The FORDELETE hint is not used by
68698** this implementation.  But in a hypothetical alternative storage engine
68699** in which index entries are automatically deleted when corresponding table
68700** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
68701** operations on this cursor can be no-ops and all READ operations can
68702** return a null row (2-bytes: 0x01 0x00).
68703**
68704** No checking is done to make sure that page iTable really is the
68705** root page of a b-tree.  If it is not, then the cursor acquired
68706** will not work correctly.
68707**
68708** It is assumed that the sqlite3BtreeCursorZero() has been called
68709** on pCur to initialize the memory space prior to invoking this routine.
68710*/
68711static int btreeCursor(
68712  Btree *p,                              /* The btree */
68713  int iTable,                            /* Root page of table to open */
68714  int wrFlag,                            /* 1 to write. 0 read-only */
68715  struct KeyInfo *pKeyInfo,              /* First arg to comparison function */
68716  BtCursor *pCur                         /* Space for new cursor */
68717){
68718  BtShared *pBt = p->pBt;                /* Shared b-tree handle */
68719  BtCursor *pX;                          /* Looping over other all cursors */
68720
68721  assert( sqlite3BtreeHoldsMutex(p) );
68722  assert( wrFlag==0
68723       || wrFlag==BTREE_WRCSR
68724       || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE)
68725  );
68726
68727  /* The following assert statements verify that if this is a sharable
68728  ** b-tree database, the connection is holding the required table locks,
68729  ** and that no other connection has any open cursor that conflicts with
68730  ** this lock.  The iTable<1 term disables the check for corrupt schemas. */
68731  assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1))
68732          || iTable<1 );
68733  assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
68734
68735  /* Assert that the caller has opened the required transaction. */
68736  assert( p->inTrans>TRANS_NONE );
68737  assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
68738  assert( pBt->pPage1 && pBt->pPage1->aData );
68739  assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
68740
68741  if( wrFlag ){
68742    allocateTempSpace(pBt);
68743    if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT;
68744  }
68745  if( iTable<=1 ){
68746    if( iTable<1 ){
68747      return SQLITE_CORRUPT_BKPT;
68748    }else if( btreePagecount(pBt)==0 ){
68749      assert( wrFlag==0 );
68750      iTable = 0;
68751    }
68752  }
68753
68754  /* Now that no other errors can occur, finish filling in the BtCursor
68755  ** variables and link the cursor into the BtShared list.  */
68756  pCur->pgnoRoot = (Pgno)iTable;
68757  pCur->iPage = -1;
68758  pCur->pKeyInfo = pKeyInfo;
68759  pCur->pBtree = p;
68760  pCur->pBt = pBt;
68761  pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0;
68762  pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
68763  /* If there are two or more cursors on the same btree, then all such
68764  ** cursors *must* have the BTCF_Multiple flag set. */
68765  for(pX=pBt->pCursor; pX; pX=pX->pNext){
68766    if( pX->pgnoRoot==(Pgno)iTable ){
68767      pX->curFlags |= BTCF_Multiple;
68768      pCur->curFlags |= BTCF_Multiple;
68769    }
68770  }
68771  pCur->pNext = pBt->pCursor;
68772  pBt->pCursor = pCur;
68773  pCur->eState = CURSOR_INVALID;
68774  return SQLITE_OK;
68775}
68776static int btreeCursorWithLock(
68777  Btree *p,                              /* The btree */
68778  int iTable,                            /* Root page of table to open */
68779  int wrFlag,                            /* 1 to write. 0 read-only */
68780  struct KeyInfo *pKeyInfo,              /* First arg to comparison function */
68781  BtCursor *pCur                         /* Space for new cursor */
68782){
68783  int rc;
68784  sqlite3BtreeEnter(p);
68785  rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
68786  sqlite3BtreeLeave(p);
68787  return rc;
68788}
68789SQLITE_PRIVATE int sqlite3BtreeCursor(
68790  Btree *p,                                   /* The btree */
68791  int iTable,                                 /* Root page of table to open */
68792  int wrFlag,                                 /* 1 to write. 0 read-only */
68793  struct KeyInfo *pKeyInfo,                   /* First arg to xCompare() */
68794  BtCursor *pCur                              /* Write new cursor here */
68795){
68796  if( p->sharable ){
68797    return btreeCursorWithLock(p, iTable, wrFlag, pKeyInfo, pCur);
68798  }else{
68799    return btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
68800  }
68801}
68802
68803/*
68804** Return the size of a BtCursor object in bytes.
68805**
68806** This interfaces is needed so that users of cursors can preallocate
68807** sufficient storage to hold a cursor.  The BtCursor object is opaque
68808** to users so they cannot do the sizeof() themselves - they must call
68809** this routine.
68810*/
68811SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
68812  return ROUND8(sizeof(BtCursor));
68813}
68814
68815/*
68816** Initialize memory that will be converted into a BtCursor object.
68817**
68818** The simple approach here would be to memset() the entire object
68819** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
68820** do not need to be zeroed and they are large, so we can save a lot
68821** of run-time by skipping the initialization of those elements.
68822*/
68823SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
68824  memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
68825}
68826
68827/*
68828** Close a cursor.  The read lock on the database file is released
68829** when the last cursor is closed.
68830*/
68831SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
68832  Btree *pBtree = pCur->pBtree;
68833  if( pBtree ){
68834    BtShared *pBt = pCur->pBt;
68835    sqlite3BtreeEnter(pBtree);
68836    assert( pBt->pCursor!=0 );
68837    if( pBt->pCursor==pCur ){
68838      pBt->pCursor = pCur->pNext;
68839    }else{
68840      BtCursor *pPrev = pBt->pCursor;
68841      do{
68842        if( pPrev->pNext==pCur ){
68843          pPrev->pNext = pCur->pNext;
68844          break;
68845        }
68846        pPrev = pPrev->pNext;
68847      }while( ALWAYS(pPrev) );
68848    }
68849    btreeReleaseAllCursorPages(pCur);
68850    unlockBtreeIfUnused(pBt);
68851    sqlite3_free(pCur->aOverflow);
68852    sqlite3_free(pCur->pKey);
68853    sqlite3BtreeLeave(pBtree);
68854    pCur->pBtree = 0;
68855  }
68856  return SQLITE_OK;
68857}
68858
68859/*
68860** Make sure the BtCursor* given in the argument has a valid
68861** BtCursor.info structure.  If it is not already valid, call
68862** btreeParseCell() to fill it in.
68863**
68864** BtCursor.info is a cache of the information in the current cell.
68865** Using this cache reduces the number of calls to btreeParseCell().
68866*/
68867#ifndef NDEBUG
68868  static int cellInfoEqual(CellInfo *a, CellInfo *b){
68869    if( a->nKey!=b->nKey ) return 0;
68870    if( a->pPayload!=b->pPayload ) return 0;
68871    if( a->nPayload!=b->nPayload ) return 0;
68872    if( a->nLocal!=b->nLocal ) return 0;
68873    if( a->nSize!=b->nSize ) return 0;
68874    return 1;
68875  }
68876  static void assertCellInfo(BtCursor *pCur){
68877    CellInfo info;
68878    memset(&info, 0, sizeof(info));
68879    btreeParseCell(pCur->pPage, pCur->ix, &info);
68880    assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
68881  }
68882#else
68883  #define assertCellInfo(x)
68884#endif
68885static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
68886  if( pCur->info.nSize==0 ){
68887    pCur->curFlags |= BTCF_ValidNKey;
68888    btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
68889  }else{
68890    assertCellInfo(pCur);
68891  }
68892}
68893
68894#ifndef NDEBUG  /* The next routine used only within assert() statements */
68895/*
68896** Return true if the given BtCursor is valid.  A valid cursor is one
68897** that is currently pointing to a row in a (non-empty) table.
68898** This is a verification routine is used only within assert() statements.
68899*/
68900SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
68901  return pCur && pCur->eState==CURSOR_VALID;
68902}
68903#endif /* NDEBUG */
68904SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
68905  assert( pCur!=0 );
68906  return pCur->eState==CURSOR_VALID;
68907}
68908
68909/*
68910** Return the value of the integer key or "rowid" for a table btree.
68911** This routine is only valid for a cursor that is pointing into a
68912** ordinary table btree.  If the cursor points to an index btree or
68913** is invalid, the result of this routine is undefined.
68914*/
68915SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
68916  assert( cursorHoldsMutex(pCur) );
68917  assert( pCur->eState==CURSOR_VALID );
68918  assert( pCur->curIntKey );
68919  getCellInfo(pCur);
68920  return pCur->info.nKey;
68921}
68922
68923/*
68924** Pin or unpin a cursor.
68925*/
68926SQLITE_PRIVATE void sqlite3BtreeCursorPin(BtCursor *pCur){
68927  assert( (pCur->curFlags & BTCF_Pinned)==0 );
68928  pCur->curFlags |= BTCF_Pinned;
68929}
68930SQLITE_PRIVATE void sqlite3BtreeCursorUnpin(BtCursor *pCur){
68931  assert( (pCur->curFlags & BTCF_Pinned)!=0 );
68932  pCur->curFlags &= ~BTCF_Pinned;
68933}
68934
68935#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
68936/*
68937** Return the offset into the database file for the start of the
68938** payload to which the cursor is pointing.
68939*/
68940SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor *pCur){
68941  assert( cursorHoldsMutex(pCur) );
68942  assert( pCur->eState==CURSOR_VALID );
68943  getCellInfo(pCur);
68944  return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
68945         (i64)(pCur->info.pPayload - pCur->pPage->aData);
68946}
68947#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
68948
68949/*
68950** Return the number of bytes of payload for the entry that pCur is
68951** currently pointing to.  For table btrees, this will be the amount
68952** of data.  For index btrees, this will be the size of the key.
68953**
68954** The caller must guarantee that the cursor is pointing to a non-NULL
68955** valid entry.  In other words, the calling procedure must guarantee
68956** that the cursor has Cursor.eState==CURSOR_VALID.
68957*/
68958SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor *pCur){
68959  assert( cursorHoldsMutex(pCur) );
68960  assert( pCur->eState==CURSOR_VALID );
68961  getCellInfo(pCur);
68962  return pCur->info.nPayload;
68963}
68964
68965/*
68966** Return an upper bound on the size of any record for the table
68967** that the cursor is pointing into.
68968**
68969** This is an optimization.  Everything will still work if this
68970** routine always returns 2147483647 (which is the largest record
68971** that SQLite can handle) or more.  But returning a smaller value might
68972** prevent large memory allocations when trying to interpret a
68973** corrupt datrabase.
68974**
68975** The current implementation merely returns the size of the underlying
68976** database file.
68977*/
68978SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){
68979  assert( cursorHoldsMutex(pCur) );
68980  assert( pCur->eState==CURSOR_VALID );
68981  return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
68982}
68983
68984/*
68985** Given the page number of an overflow page in the database (parameter
68986** ovfl), this function finds the page number of the next page in the
68987** linked list of overflow pages. If possible, it uses the auto-vacuum
68988** pointer-map data instead of reading the content of page ovfl to do so.
68989**
68990** If an error occurs an SQLite error code is returned. Otherwise:
68991**
68992** The page number of the next overflow page in the linked list is
68993** written to *pPgnoNext. If page ovfl is the last page in its linked
68994** list, *pPgnoNext is set to zero.
68995**
68996** If ppPage is not NULL, and a reference to the MemPage object corresponding
68997** to page number pOvfl was obtained, then *ppPage is set to point to that
68998** reference. It is the responsibility of the caller to call releasePage()
68999** on *ppPage to free the reference. In no reference was obtained (because
69000** the pointer-map was used to obtain the value for *pPgnoNext), then
69001** *ppPage is set to zero.
69002*/
69003static int getOverflowPage(
69004  BtShared *pBt,               /* The database file */
69005  Pgno ovfl,                   /* Current overflow page number */
69006  MemPage **ppPage,            /* OUT: MemPage handle (may be NULL) */
69007  Pgno *pPgnoNext              /* OUT: Next overflow page number */
69008){
69009  Pgno next = 0;
69010  MemPage *pPage = 0;
69011  int rc = SQLITE_OK;
69012
69013  assert( sqlite3_mutex_held(pBt->mutex) );
69014  assert(pPgnoNext);
69015
69016#ifndef SQLITE_OMIT_AUTOVACUUM
69017  /* Try to find the next page in the overflow list using the
69018  ** autovacuum pointer-map pages. Guess that the next page in
69019  ** the overflow list is page number (ovfl+1). If that guess turns
69020  ** out to be wrong, fall back to loading the data of page
69021  ** number ovfl to determine the next page number.
69022  */
69023  if( pBt->autoVacuum ){
69024    Pgno pgno;
69025    Pgno iGuess = ovfl+1;
69026    u8 eType;
69027
69028    while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
69029      iGuess++;
69030    }
69031
69032    if( iGuess<=btreePagecount(pBt) ){
69033      rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
69034      if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
69035        next = iGuess;
69036        rc = SQLITE_DONE;
69037      }
69038    }
69039  }
69040#endif
69041
69042  assert( next==0 || rc==SQLITE_DONE );
69043  if( rc==SQLITE_OK ){
69044    rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0);
69045    assert( rc==SQLITE_OK || pPage==0 );
69046    if( rc==SQLITE_OK ){
69047      next = get4byte(pPage->aData);
69048    }
69049  }
69050
69051  *pPgnoNext = next;
69052  if( ppPage ){
69053    *ppPage = pPage;
69054  }else{
69055    releasePage(pPage);
69056  }
69057  return (rc==SQLITE_DONE ? SQLITE_OK : rc);
69058}
69059
69060/*
69061** Copy data from a buffer to a page, or from a page to a buffer.
69062**
69063** pPayload is a pointer to data stored on database page pDbPage.
69064** If argument eOp is false, then nByte bytes of data are copied
69065** from pPayload to the buffer pointed at by pBuf. If eOp is true,
69066** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
69067** of data are copied from the buffer pBuf to pPayload.
69068**
69069** SQLITE_OK is returned on success, otherwise an error code.
69070*/
69071static int copyPayload(
69072  void *pPayload,           /* Pointer to page data */
69073  void *pBuf,               /* Pointer to buffer */
69074  int nByte,                /* Number of bytes to copy */
69075  int eOp,                  /* 0 -> copy from page, 1 -> copy to page */
69076  DbPage *pDbPage           /* Page containing pPayload */
69077){
69078  if( eOp ){
69079    /* Copy data from buffer to page (a write operation) */
69080    int rc = sqlite3PagerWrite(pDbPage);
69081    if( rc!=SQLITE_OK ){
69082      return rc;
69083    }
69084    memcpy(pPayload, pBuf, nByte);
69085  }else{
69086    /* Copy data from page to buffer (a read operation) */
69087    memcpy(pBuf, pPayload, nByte);
69088  }
69089  return SQLITE_OK;
69090}
69091
69092/*
69093** This function is used to read or overwrite payload information
69094** for the entry that the pCur cursor is pointing to. The eOp
69095** argument is interpreted as follows:
69096**
69097**   0: The operation is a read. Populate the overflow cache.
69098**   1: The operation is a write. Populate the overflow cache.
69099**
69100** A total of "amt" bytes are read or written beginning at "offset".
69101** Data is read to or from the buffer pBuf.
69102**
69103** The content being read or written might appear on the main page
69104** or be scattered out on multiple overflow pages.
69105**
69106** If the current cursor entry uses one or more overflow pages
69107** this function may allocate space for and lazily populate
69108** the overflow page-list cache array (BtCursor.aOverflow).
69109** Subsequent calls use this cache to make seeking to the supplied offset
69110** more efficient.
69111**
69112** Once an overflow page-list cache has been allocated, it must be
69113** invalidated if some other cursor writes to the same table, or if
69114** the cursor is moved to a different row. Additionally, in auto-vacuum
69115** mode, the following events may invalidate an overflow page-list cache.
69116**
69117**   * An incremental vacuum,
69118**   * A commit in auto_vacuum="full" mode,
69119**   * Creating a table (may require moving an overflow page).
69120*/
69121static int accessPayload(
69122  BtCursor *pCur,      /* Cursor pointing to entry to read from */
69123  u32 offset,          /* Begin reading this far into payload */
69124  u32 amt,             /* Read this many bytes */
69125  unsigned char *pBuf, /* Write the bytes into this buffer */
69126  int eOp              /* zero to read. non-zero to write. */
69127){
69128  unsigned char *aPayload;
69129  int rc = SQLITE_OK;
69130  int iIdx = 0;
69131  MemPage *pPage = pCur->pPage;               /* Btree page of current entry */
69132  BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
69133#ifdef SQLITE_DIRECT_OVERFLOW_READ
69134  unsigned char * const pBufStart = pBuf;     /* Start of original out buffer */
69135#endif
69136
69137  assert( pPage );
69138  assert( eOp==0 || eOp==1 );
69139  assert( pCur->eState==CURSOR_VALID );
69140  assert( pCur->ix<pPage->nCell );
69141  assert( cursorHoldsMutex(pCur) );
69142
69143  getCellInfo(pCur);
69144  aPayload = pCur->info.pPayload;
69145  assert( offset+amt <= pCur->info.nPayload );
69146
69147  assert( aPayload > pPage->aData );
69148  if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
69149    /* Trying to read or write past the end of the data is an error.  The
69150    ** conditional above is really:
69151    **    &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
69152    ** but is recast into its current form to avoid integer overflow problems
69153    */
69154    return SQLITE_CORRUPT_PAGE(pPage);
69155  }
69156
69157  /* Check if data must be read/written to/from the btree page itself. */
69158  if( offset<pCur->info.nLocal ){
69159    int a = amt;
69160    if( a+offset>pCur->info.nLocal ){
69161      a = pCur->info.nLocal - offset;
69162    }
69163    rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
69164    offset = 0;
69165    pBuf += a;
69166    amt -= a;
69167  }else{
69168    offset -= pCur->info.nLocal;
69169  }
69170
69171
69172  if( rc==SQLITE_OK && amt>0 ){
69173    const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
69174    Pgno nextPage;
69175
69176    nextPage = get4byte(&aPayload[pCur->info.nLocal]);
69177
69178    /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
69179    **
69180    ** The aOverflow[] array is sized at one entry for each overflow page
69181    ** in the overflow chain. The page number of the first overflow page is
69182    ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
69183    ** means "not yet known" (the cache is lazily populated).
69184    */
69185    if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
69186      int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
69187      if( pCur->aOverflow==0
69188       || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
69189      ){
69190        Pgno *aNew = (Pgno*)sqlite3Realloc(
69191            pCur->aOverflow, nOvfl*2*sizeof(Pgno)
69192        );
69193        if( aNew==0 ){
69194          return SQLITE_NOMEM_BKPT;
69195        }else{
69196          pCur->aOverflow = aNew;
69197        }
69198      }
69199      memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
69200      pCur->curFlags |= BTCF_ValidOvfl;
69201    }else{
69202      /* If the overflow page-list cache has been allocated and the
69203      ** entry for the first required overflow page is valid, skip
69204      ** directly to it.
69205      */
69206      if( pCur->aOverflow[offset/ovflSize] ){
69207        iIdx = (offset/ovflSize);
69208        nextPage = pCur->aOverflow[iIdx];
69209        offset = (offset%ovflSize);
69210      }
69211    }
69212
69213    assert( rc==SQLITE_OK && amt>0 );
69214    while( nextPage ){
69215      /* If required, populate the overflow page-list cache. */
69216      assert( pCur->aOverflow[iIdx]==0
69217              || pCur->aOverflow[iIdx]==nextPage
69218              || CORRUPT_DB );
69219      pCur->aOverflow[iIdx] = nextPage;
69220
69221      if( offset>=ovflSize ){
69222        /* The only reason to read this page is to obtain the page
69223        ** number for the next page in the overflow chain. The page
69224        ** data is not required. So first try to lookup the overflow
69225        ** page-list cache, if any, then fall back to the getOverflowPage()
69226        ** function.
69227        */
69228        assert( pCur->curFlags & BTCF_ValidOvfl );
69229        assert( pCur->pBtree->db==pBt->db );
69230        if( pCur->aOverflow[iIdx+1] ){
69231          nextPage = pCur->aOverflow[iIdx+1];
69232        }else{
69233          rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
69234        }
69235        offset -= ovflSize;
69236      }else{
69237        /* Need to read this page properly. It contains some of the
69238        ** range of data that is being read (eOp==0) or written (eOp!=0).
69239        */
69240        int a = amt;
69241        if( a + offset > ovflSize ){
69242          a = ovflSize - offset;
69243        }
69244
69245#ifdef SQLITE_DIRECT_OVERFLOW_READ
69246        /* If all the following are true:
69247        **
69248        **   1) this is a read operation, and
69249        **   2) data is required from the start of this overflow page, and
69250        **   3) there are no dirty pages in the page-cache
69251        **   4) the database is file-backed, and
69252        **   5) the page is not in the WAL file
69253        **   6) at least 4 bytes have already been read into the output buffer
69254        **
69255        ** then data can be read directly from the database file into the
69256        ** output buffer, bypassing the page-cache altogether. This speeds
69257        ** up loading large records that span many overflow pages.
69258        */
69259        if( eOp==0                                             /* (1) */
69260         && offset==0                                          /* (2) */
69261         && sqlite3PagerDirectReadOk(pBt->pPager, nextPage)    /* (3,4,5) */
69262         && &pBuf[-4]>=pBufStart                               /* (6) */
69263        ){
69264          sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
69265          u8 aSave[4];
69266          u8 *aWrite = &pBuf[-4];
69267          assert( aWrite>=pBufStart );                         /* due to (6) */
69268          memcpy(aSave, aWrite, 4);
69269          rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
69270          if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPT;
69271          nextPage = get4byte(aWrite);
69272          memcpy(aWrite, aSave, 4);
69273        }else
69274#endif
69275
69276        {
69277          DbPage *pDbPage;
69278          rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
69279              (eOp==0 ? PAGER_GET_READONLY : 0)
69280          );
69281          if( rc==SQLITE_OK ){
69282            aPayload = sqlite3PagerGetData(pDbPage);
69283            nextPage = get4byte(aPayload);
69284            rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
69285            sqlite3PagerUnref(pDbPage);
69286            offset = 0;
69287          }
69288        }
69289        amt -= a;
69290        if( amt==0 ) return rc;
69291        pBuf += a;
69292      }
69293      if( rc ) break;
69294      iIdx++;
69295    }
69296  }
69297
69298  if( rc==SQLITE_OK && amt>0 ){
69299    /* Overflow chain ends prematurely */
69300    return SQLITE_CORRUPT_PAGE(pPage);
69301  }
69302  return rc;
69303}
69304
69305/*
69306** Read part of the payload for the row at which that cursor pCur is currently
69307** pointing.  "amt" bytes will be transferred into pBuf[].  The transfer
69308** begins at "offset".
69309**
69310** pCur can be pointing to either a table or an index b-tree.
69311** If pointing to a table btree, then the content section is read.  If
69312** pCur is pointing to an index b-tree then the key section is read.
69313**
69314** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing
69315** to a valid row in the table.  For sqlite3BtreePayloadChecked(), the
69316** cursor might be invalid or might need to be restored before being read.
69317**
69318** Return SQLITE_OK on success or an error code if anything goes
69319** wrong.  An error is returned if "offset+amt" is larger than
69320** the available payload.
69321*/
69322SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
69323  assert( cursorHoldsMutex(pCur) );
69324  assert( pCur->eState==CURSOR_VALID );
69325  assert( pCur->iPage>=0 && pCur->pPage );
69326  assert( pCur->ix<pCur->pPage->nCell );
69327  return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
69328}
69329
69330/*
69331** This variant of sqlite3BtreePayload() works even if the cursor has not
69332** in the CURSOR_VALID state.  It is only used by the sqlite3_blob_read()
69333** interface.
69334*/
69335#ifndef SQLITE_OMIT_INCRBLOB
69336static SQLITE_NOINLINE int accessPayloadChecked(
69337  BtCursor *pCur,
69338  u32 offset,
69339  u32 amt,
69340  void *pBuf
69341){
69342  int rc;
69343  if ( pCur->eState==CURSOR_INVALID ){
69344    return SQLITE_ABORT;
69345  }
69346  assert( cursorOwnsBtShared(pCur) );
69347  rc = btreeRestoreCursorPosition(pCur);
69348  return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
69349}
69350SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
69351  if( pCur->eState==CURSOR_VALID ){
69352    assert( cursorOwnsBtShared(pCur) );
69353    return accessPayload(pCur, offset, amt, pBuf, 0);
69354  }else{
69355    return accessPayloadChecked(pCur, offset, amt, pBuf);
69356  }
69357}
69358#endif /* SQLITE_OMIT_INCRBLOB */
69359
69360/*
69361** Return a pointer to payload information from the entry that the
69362** pCur cursor is pointing to.  The pointer is to the beginning of
69363** the key if index btrees (pPage->intKey==0) and is the data for
69364** table btrees (pPage->intKey==1). The number of bytes of available
69365** key/data is written into *pAmt.  If *pAmt==0, then the value
69366** returned will not be a valid pointer.
69367**
69368** This routine is an optimization.  It is common for the entire key
69369** and data to fit on the local page and for there to be no overflow
69370** pages.  When that is so, this routine can be used to access the
69371** key and data without making a copy.  If the key and/or data spills
69372** onto overflow pages, then accessPayload() must be used to reassemble
69373** the key/data and copy it into a preallocated buffer.
69374**
69375** The pointer returned by this routine looks directly into the cached
69376** page of the database.  The data might change or move the next time
69377** any btree routine is called.
69378*/
69379static const void *fetchPayload(
69380  BtCursor *pCur,      /* Cursor pointing to entry to read from */
69381  u32 *pAmt            /* Write the number of available bytes here */
69382){
69383  int amt;
69384  assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
69385  assert( pCur->eState==CURSOR_VALID );
69386  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
69387  assert( cursorOwnsBtShared(pCur) );
69388  assert( pCur->ix<pCur->pPage->nCell );
69389  assert( pCur->info.nSize>0 );
69390  assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
69391  assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
69392  amt = pCur->info.nLocal;
69393  if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
69394    /* There is too little space on the page for the expected amount
69395    ** of local content. Database must be corrupt. */
69396    assert( CORRUPT_DB );
69397    amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
69398  }
69399  *pAmt = (u32)amt;
69400  return (void*)pCur->info.pPayload;
69401}
69402
69403
69404/*
69405** For the entry that cursor pCur is point to, return as
69406** many bytes of the key or data as are available on the local
69407** b-tree page.  Write the number of available bytes into *pAmt.
69408**
69409** The pointer returned is ephemeral.  The key/data may move
69410** or be destroyed on the next call to any Btree routine,
69411** including calls from other threads against the same cache.
69412** Hence, a mutex on the BtShared should be held prior to calling
69413** this routine.
69414**
69415** These routines is used to get quick access to key and data
69416** in the common case where no overflow pages are used.
69417*/
69418SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
69419  return fetchPayload(pCur, pAmt);
69420}
69421
69422
69423/*
69424** Move the cursor down to a new child page.  The newPgno argument is the
69425** page number of the child page to move to.
69426**
69427** This function returns SQLITE_CORRUPT if the page-header flags field of
69428** the new child page does not match the flags field of the parent (i.e.
69429** if an intkey page appears to be the parent of a non-intkey page, or
69430** vice-versa).
69431*/
69432static int moveToChild(BtCursor *pCur, u32 newPgno){
69433  BtShared *pBt = pCur->pBt;
69434
69435  assert( cursorOwnsBtShared(pCur) );
69436  assert( pCur->eState==CURSOR_VALID );
69437  assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
69438  assert( pCur->iPage>=0 );
69439  if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
69440    return SQLITE_CORRUPT_BKPT;
69441  }
69442  pCur->info.nSize = 0;
69443  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
69444  pCur->aiIdx[pCur->iPage] = pCur->ix;
69445  pCur->apPage[pCur->iPage] = pCur->pPage;
69446  pCur->ix = 0;
69447  pCur->iPage++;
69448  return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
69449}
69450
69451#ifdef SQLITE_DEBUG
69452/*
69453** Page pParent is an internal (non-leaf) tree page. This function
69454** asserts that page number iChild is the left-child if the iIdx'th
69455** cell in page pParent. Or, if iIdx is equal to the total number of
69456** cells in pParent, that page number iChild is the right-child of
69457** the page.
69458*/
69459static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
69460  if( CORRUPT_DB ) return;  /* The conditions tested below might not be true
69461                            ** in a corrupt database */
69462  assert( iIdx<=pParent->nCell );
69463  if( iIdx==pParent->nCell ){
69464    assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
69465  }else{
69466    assert( get4byte(findCell(pParent, iIdx))==iChild );
69467  }
69468}
69469#else
69470#  define assertParentIndex(x,y,z)
69471#endif
69472
69473/*
69474** Move the cursor up to the parent page.
69475**
69476** pCur->idx is set to the cell index that contains the pointer
69477** to the page we are coming from.  If we are coming from the
69478** right-most child page then pCur->idx is set to one more than
69479** the largest cell index.
69480*/
69481static void moveToParent(BtCursor *pCur){
69482  MemPage *pLeaf;
69483  assert( cursorOwnsBtShared(pCur) );
69484  assert( pCur->eState==CURSOR_VALID );
69485  assert( pCur->iPage>0 );
69486  assert( pCur->pPage );
69487  assertParentIndex(
69488    pCur->apPage[pCur->iPage-1],
69489    pCur->aiIdx[pCur->iPage-1],
69490    pCur->pPage->pgno
69491  );
69492  testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
69493  pCur->info.nSize = 0;
69494  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
69495  pCur->ix = pCur->aiIdx[pCur->iPage-1];
69496  pLeaf = pCur->pPage;
69497  pCur->pPage = pCur->apPage[--pCur->iPage];
69498  releasePageNotNull(pLeaf);
69499}
69500
69501/*
69502** Move the cursor to point to the root page of its b-tree structure.
69503**
69504** If the table has a virtual root page, then the cursor is moved to point
69505** to the virtual root page instead of the actual root page. A table has a
69506** virtual root page when the actual root page contains no cells and a
69507** single child page. This can only happen with the table rooted at page 1.
69508**
69509** If the b-tree structure is empty, the cursor state is set to
69510** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
69511** the cursor is set to point to the first cell located on the root
69512** (or virtual root) page and the cursor state is set to CURSOR_VALID.
69513**
69514** If this function returns successfully, it may be assumed that the
69515** page-header flags indicate that the [virtual] root-page is the expected
69516** kind of b-tree page (i.e. if when opening the cursor the caller did not
69517** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
69518** indicating a table b-tree, or if the caller did specify a KeyInfo
69519** structure the flags byte is set to 0x02 or 0x0A, indicating an index
69520** b-tree).
69521*/
69522static int moveToRoot(BtCursor *pCur){
69523  MemPage *pRoot;
69524  int rc = SQLITE_OK;
69525
69526  assert( cursorOwnsBtShared(pCur) );
69527  assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
69528  assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
69529  assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
69530  assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
69531  assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
69532
69533  if( pCur->iPage>=0 ){
69534    if( pCur->iPage ){
69535      releasePageNotNull(pCur->pPage);
69536      while( --pCur->iPage ){
69537        releasePageNotNull(pCur->apPage[pCur->iPage]);
69538      }
69539      pCur->pPage = pCur->apPage[0];
69540      goto skip_init;
69541    }
69542  }else if( pCur->pgnoRoot==0 ){
69543    pCur->eState = CURSOR_INVALID;
69544    return SQLITE_EMPTY;
69545  }else{
69546    assert( pCur->iPage==(-1) );
69547    if( pCur->eState>=CURSOR_REQUIRESEEK ){
69548      if( pCur->eState==CURSOR_FAULT ){
69549        assert( pCur->skipNext!=SQLITE_OK );
69550        return pCur->skipNext;
69551      }
69552      sqlite3BtreeClearCursor(pCur);
69553    }
69554    rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
69555                        0, pCur->curPagerFlags);
69556    if( rc!=SQLITE_OK ){
69557      pCur->eState = CURSOR_INVALID;
69558      return rc;
69559    }
69560    pCur->iPage = 0;
69561    pCur->curIntKey = pCur->pPage->intKey;
69562  }
69563  pRoot = pCur->pPage;
69564  assert( pRoot->pgno==pCur->pgnoRoot );
69565
69566  /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
69567  ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
69568  ** NULL, the caller expects a table b-tree. If this is not the case,
69569  ** return an SQLITE_CORRUPT error.
69570  **
69571  ** Earlier versions of SQLite assumed that this test could not fail
69572  ** if the root page was already loaded when this function was called (i.e.
69573  ** if pCur->iPage>=0). But this is not so if the database is corrupted
69574  ** in such a way that page pRoot is linked into a second b-tree table
69575  ** (or the freelist).  */
69576  assert( pRoot->intKey==1 || pRoot->intKey==0 );
69577  if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
69578    return SQLITE_CORRUPT_PAGE(pCur->pPage);
69579  }
69580
69581skip_init:
69582  pCur->ix = 0;
69583  pCur->info.nSize = 0;
69584  pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
69585
69586  pRoot = pCur->pPage;
69587  if( pRoot->nCell>0 ){
69588    pCur->eState = CURSOR_VALID;
69589  }else if( !pRoot->leaf ){
69590    Pgno subpage;
69591    if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
69592    subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
69593    pCur->eState = CURSOR_VALID;
69594    rc = moveToChild(pCur, subpage);
69595  }else{
69596    pCur->eState = CURSOR_INVALID;
69597    rc = SQLITE_EMPTY;
69598  }
69599  return rc;
69600}
69601
69602/*
69603** Move the cursor down to the left-most leaf entry beneath the
69604** entry to which it is currently pointing.
69605**
69606** The left-most leaf is the one with the smallest key - the first
69607** in ascending order.
69608*/
69609static int moveToLeftmost(BtCursor *pCur){
69610  Pgno pgno;
69611  int rc = SQLITE_OK;
69612  MemPage *pPage;
69613
69614  assert( cursorOwnsBtShared(pCur) );
69615  assert( pCur->eState==CURSOR_VALID );
69616  while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
69617    assert( pCur->ix<pPage->nCell );
69618    pgno = get4byte(findCell(pPage, pCur->ix));
69619    rc = moveToChild(pCur, pgno);
69620  }
69621  return rc;
69622}
69623
69624/*
69625** Move the cursor down to the right-most leaf entry beneath the
69626** page to which it is currently pointing.  Notice the difference
69627** between moveToLeftmost() and moveToRightmost().  moveToLeftmost()
69628** finds the left-most entry beneath the *entry* whereas moveToRightmost()
69629** finds the right-most entry beneath the *page*.
69630**
69631** The right-most entry is the one with the largest key - the last
69632** key in ascending order.
69633*/
69634static int moveToRightmost(BtCursor *pCur){
69635  Pgno pgno;
69636  int rc = SQLITE_OK;
69637  MemPage *pPage = 0;
69638
69639  assert( cursorOwnsBtShared(pCur) );
69640  assert( pCur->eState==CURSOR_VALID );
69641  while( !(pPage = pCur->pPage)->leaf ){
69642    pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
69643    pCur->ix = pPage->nCell;
69644    rc = moveToChild(pCur, pgno);
69645    if( rc ) return rc;
69646  }
69647  pCur->ix = pPage->nCell-1;
69648  assert( pCur->info.nSize==0 );
69649  assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
69650  return SQLITE_OK;
69651}
69652
69653/* Move the cursor to the first entry in the table.  Return SQLITE_OK
69654** on success.  Set *pRes to 0 if the cursor actually points to something
69655** or set *pRes to 1 if the table is empty.
69656*/
69657SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
69658  int rc;
69659
69660  assert( cursorOwnsBtShared(pCur) );
69661  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
69662  rc = moveToRoot(pCur);
69663  if( rc==SQLITE_OK ){
69664    assert( pCur->pPage->nCell>0 );
69665    *pRes = 0;
69666    rc = moveToLeftmost(pCur);
69667  }else if( rc==SQLITE_EMPTY ){
69668    assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
69669    *pRes = 1;
69670    rc = SQLITE_OK;
69671  }
69672  return rc;
69673}
69674
69675/* Move the cursor to the last entry in the table.  Return SQLITE_OK
69676** on success.  Set *pRes to 0 if the cursor actually points to something
69677** or set *pRes to 1 if the table is empty.
69678*/
69679SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
69680  int rc;
69681
69682  assert( cursorOwnsBtShared(pCur) );
69683  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
69684
69685  /* If the cursor already points to the last entry, this is a no-op. */
69686  if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
69687#ifdef SQLITE_DEBUG
69688    /* This block serves to assert() that the cursor really does point
69689    ** to the last entry in the b-tree. */
69690    int ii;
69691    for(ii=0; ii<pCur->iPage; ii++){
69692      assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
69693    }
69694    assert( pCur->ix==pCur->pPage->nCell-1 );
69695    assert( pCur->pPage->leaf );
69696#endif
69697    *pRes = 0;
69698    return SQLITE_OK;
69699  }
69700
69701  rc = moveToRoot(pCur);
69702  if( rc==SQLITE_OK ){
69703    assert( pCur->eState==CURSOR_VALID );
69704    *pRes = 0;
69705    rc = moveToRightmost(pCur);
69706    if( rc==SQLITE_OK ){
69707      pCur->curFlags |= BTCF_AtLast;
69708    }else{
69709      pCur->curFlags &= ~BTCF_AtLast;
69710    }
69711  }else if( rc==SQLITE_EMPTY ){
69712    assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
69713    *pRes = 1;
69714    rc = SQLITE_OK;
69715  }
69716  return rc;
69717}
69718
69719/* Move the cursor so that it points to an entry near the key
69720** specified by pIdxKey or intKey.   Return a success code.
69721**
69722** For INTKEY tables, the intKey parameter is used.  pIdxKey
69723** must be NULL.  For index tables, pIdxKey is used and intKey
69724** is ignored.
69725**
69726** If an exact match is not found, then the cursor is always
69727** left pointing at a leaf page which would hold the entry if it
69728** were present.  The cursor might point to an entry that comes
69729** before or after the key.
69730**
69731** An integer is written into *pRes which is the result of
69732** comparing the key with the entry to which the cursor is
69733** pointing.  The meaning of the integer written into
69734** *pRes is as follows:
69735**
69736**     *pRes<0      The cursor is left pointing at an entry that
69737**                  is smaller than intKey/pIdxKey or if the table is empty
69738**                  and the cursor is therefore left point to nothing.
69739**
69740**     *pRes==0     The cursor is left pointing at an entry that
69741**                  exactly matches intKey/pIdxKey.
69742**
69743**     *pRes>0      The cursor is left pointing at an entry that
69744**                  is larger than intKey/pIdxKey.
69745**
69746** For index tables, the pIdxKey->eqSeen field is set to 1 if there
69747** exists an entry in the table that exactly matches pIdxKey.
69748*/
69749SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
69750  BtCursor *pCur,          /* The cursor to be moved */
69751  UnpackedRecord *pIdxKey, /* Unpacked index key */
69752  i64 intKey,              /* The table key */
69753  int biasRight,           /* If true, bias the search to the high end */
69754  int *pRes                /* Write search results here */
69755){
69756  int rc;
69757  RecordCompare xRecordCompare;
69758
69759  assert( cursorOwnsBtShared(pCur) );
69760  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
69761  assert( pRes );
69762  assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
69763  assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) );
69764
69765  /* If the cursor is already positioned at the point we are trying
69766  ** to move to, then just return without doing any work */
69767  if( pIdxKey==0
69768   && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
69769  ){
69770    if( pCur->info.nKey==intKey ){
69771      *pRes = 0;
69772      return SQLITE_OK;
69773    }
69774    if( pCur->info.nKey<intKey ){
69775      if( (pCur->curFlags & BTCF_AtLast)!=0 ){
69776        *pRes = -1;
69777        return SQLITE_OK;
69778      }
69779      /* If the requested key is one more than the previous key, then
69780      ** try to get there using sqlite3BtreeNext() rather than a full
69781      ** binary search.  This is an optimization only.  The correct answer
69782      ** is still obtained without this case, only a little more slowely */
69783      if( pCur->info.nKey+1==intKey ){
69784        *pRes = 0;
69785        rc = sqlite3BtreeNext(pCur, 0);
69786        if( rc==SQLITE_OK ){
69787          getCellInfo(pCur);
69788          if( pCur->info.nKey==intKey ){
69789            return SQLITE_OK;
69790          }
69791        }else if( rc==SQLITE_DONE ){
69792          rc = SQLITE_OK;
69793        }else{
69794          return rc;
69795        }
69796      }
69797    }
69798  }
69799
69800  if( pIdxKey ){
69801    xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
69802    pIdxKey->errCode = 0;
69803    assert( pIdxKey->default_rc==1
69804         || pIdxKey->default_rc==0
69805         || pIdxKey->default_rc==-1
69806    );
69807  }else{
69808    xRecordCompare = 0; /* All keys are integers */
69809  }
69810
69811  rc = moveToRoot(pCur);
69812  if( rc ){
69813    if( rc==SQLITE_EMPTY ){
69814      assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
69815      *pRes = -1;
69816      return SQLITE_OK;
69817    }
69818    return rc;
69819  }
69820  assert( pCur->pPage );
69821  assert( pCur->pPage->isInit );
69822  assert( pCur->eState==CURSOR_VALID );
69823  assert( pCur->pPage->nCell > 0 );
69824  assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
69825  assert( pCur->curIntKey || pIdxKey );
69826  for(;;){
69827    int lwr, upr, idx, c;
69828    Pgno chldPg;
69829    MemPage *pPage = pCur->pPage;
69830    u8 *pCell;                          /* Pointer to current cell in pPage */
69831
69832    /* pPage->nCell must be greater than zero. If this is the root-page
69833    ** the cursor would have been INVALID above and this for(;;) loop
69834    ** not run. If this is not the root-page, then the moveToChild() routine
69835    ** would have already detected db corruption. Similarly, pPage must
69836    ** be the right kind (index or table) of b-tree page. Otherwise
69837    ** a moveToChild() or moveToRoot() call would have detected corruption.  */
69838    assert( pPage->nCell>0 );
69839    assert( pPage->intKey==(pIdxKey==0) );
69840    lwr = 0;
69841    upr = pPage->nCell-1;
69842    assert( biasRight==0 || biasRight==1 );
69843    idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
69844    pCur->ix = (u16)idx;
69845    if( xRecordCompare==0 ){
69846      for(;;){
69847        i64 nCellKey;
69848        pCell = findCellPastPtr(pPage, idx);
69849        if( pPage->intKeyLeaf ){
69850          while( 0x80 <= *(pCell++) ){
69851            if( pCell>=pPage->aDataEnd ){
69852              return SQLITE_CORRUPT_PAGE(pPage);
69853            }
69854          }
69855        }
69856        getVarint(pCell, (u64*)&nCellKey);
69857        if( nCellKey<intKey ){
69858          lwr = idx+1;
69859          if( lwr>upr ){ c = -1; break; }
69860        }else if( nCellKey>intKey ){
69861          upr = idx-1;
69862          if( lwr>upr ){ c = +1; break; }
69863        }else{
69864          assert( nCellKey==intKey );
69865          pCur->ix = (u16)idx;
69866          if( !pPage->leaf ){
69867            lwr = idx;
69868            goto moveto_next_layer;
69869          }else{
69870            pCur->curFlags |= BTCF_ValidNKey;
69871            pCur->info.nKey = nCellKey;
69872            pCur->info.nSize = 0;
69873            *pRes = 0;
69874            return SQLITE_OK;
69875          }
69876        }
69877        assert( lwr+upr>=0 );
69878        idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2; */
69879      }
69880    }else{
69881      for(;;){
69882        int nCell;  /* Size of the pCell cell in bytes */
69883        pCell = findCellPastPtr(pPage, idx);
69884
69885        /* The maximum supported page-size is 65536 bytes. This means that
69886        ** the maximum number of record bytes stored on an index B-Tree
69887        ** page is less than 16384 bytes and may be stored as a 2-byte
69888        ** varint. This information is used to attempt to avoid parsing
69889        ** the entire cell by checking for the cases where the record is
69890        ** stored entirely within the b-tree page by inspecting the first
69891        ** 2 bytes of the cell.
69892        */
69893        nCell = pCell[0];
69894        if( nCell<=pPage->max1bytePayload ){
69895          /* This branch runs if the record-size field of the cell is a
69896          ** single byte varint and the record fits entirely on the main
69897          ** b-tree page.  */
69898          testcase( pCell+nCell+1==pPage->aDataEnd );
69899          c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
69900        }else if( !(pCell[1] & 0x80)
69901          && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
69902        ){
69903          /* The record-size field is a 2 byte varint and the record
69904          ** fits entirely on the main b-tree page.  */
69905          testcase( pCell+nCell+2==pPage->aDataEnd );
69906          c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
69907        }else{
69908          /* The record flows over onto one or more overflow pages. In
69909          ** this case the whole cell needs to be parsed, a buffer allocated
69910          ** and accessPayload() used to retrieve the record into the
69911          ** buffer before VdbeRecordCompare() can be called.
69912          **
69913          ** If the record is corrupt, the xRecordCompare routine may read
69914          ** up to two varints past the end of the buffer. An extra 18
69915          ** bytes of padding is allocated at the end of the buffer in
69916          ** case this happens.  */
69917          void *pCellKey;
69918          u8 * const pCellBody = pCell - pPage->childPtrSize;
69919          const int nOverrun = 18;  /* Size of the overrun padding */
69920          pPage->xParseCell(pPage, pCellBody, &pCur->info);
69921          nCell = (int)pCur->info.nKey;
69922          testcase( nCell<0 );   /* True if key size is 2^32 or more */
69923          testcase( nCell==0 );  /* Invalid key size:  0x80 0x80 0x00 */
69924          testcase( nCell==1 );  /* Invalid key size:  0x80 0x80 0x01 */
69925          testcase( nCell==2 );  /* Minimum legal index key size */
69926          if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
69927            rc = SQLITE_CORRUPT_PAGE(pPage);
69928            goto moveto_finish;
69929          }
69930          pCellKey = sqlite3Malloc( nCell+nOverrun );
69931          if( pCellKey==0 ){
69932            rc = SQLITE_NOMEM_BKPT;
69933            goto moveto_finish;
69934          }
69935          pCur->ix = (u16)idx;
69936          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
69937          memset(((u8*)pCellKey)+nCell,0,nOverrun); /* Fix uninit warnings */
69938          pCur->curFlags &= ~BTCF_ValidOvfl;
69939          if( rc ){
69940            sqlite3_free(pCellKey);
69941            goto moveto_finish;
69942          }
69943          c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
69944          sqlite3_free(pCellKey);
69945        }
69946        assert(
69947            (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
69948         && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
69949        );
69950        if( c<0 ){
69951          lwr = idx+1;
69952        }else if( c>0 ){
69953          upr = idx-1;
69954        }else{
69955          assert( c==0 );
69956          *pRes = 0;
69957          rc = SQLITE_OK;
69958          pCur->ix = (u16)idx;
69959          if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
69960          goto moveto_finish;
69961        }
69962        if( lwr>upr ) break;
69963        assert( lwr+upr>=0 );
69964        idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2 */
69965      }
69966    }
69967    assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
69968    assert( pPage->isInit );
69969    if( pPage->leaf ){
69970      assert( pCur->ix<pCur->pPage->nCell );
69971      pCur->ix = (u16)idx;
69972      *pRes = c;
69973      rc = SQLITE_OK;
69974      goto moveto_finish;
69975    }
69976moveto_next_layer:
69977    if( lwr>=pPage->nCell ){
69978      chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
69979    }else{
69980      chldPg = get4byte(findCell(pPage, lwr));
69981    }
69982    pCur->ix = (u16)lwr;
69983    rc = moveToChild(pCur, chldPg);
69984    if( rc ) break;
69985  }
69986moveto_finish:
69987  pCur->info.nSize = 0;
69988  assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
69989  return rc;
69990}
69991
69992
69993/*
69994** Return TRUE if the cursor is not pointing at an entry of the table.
69995**
69996** TRUE will be returned after a call to sqlite3BtreeNext() moves
69997** past the last entry in the table or sqlite3BtreePrev() moves past
69998** the first entry.  TRUE is also returned if the table is empty.
69999*/
70000SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
70001  /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
70002  ** have been deleted? This API will need to change to return an error code
70003  ** as well as the boolean result value.
70004  */
70005  return (CURSOR_VALID!=pCur->eState);
70006}
70007
70008/*
70009** Return an estimate for the number of rows in the table that pCur is
70010** pointing to.  Return a negative number if no estimate is currently
70011** available.
70012*/
70013SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
70014  i64 n;
70015  u8 i;
70016
70017  assert( cursorOwnsBtShared(pCur) );
70018  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
70019
70020  /* Currently this interface is only called by the OP_IfSmaller
70021  ** opcode, and it that case the cursor will always be valid and
70022  ** will always point to a leaf node. */
70023  if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
70024  if( NEVER(pCur->pPage->leaf==0) ) return -1;
70025
70026  n = pCur->pPage->nCell;
70027  for(i=0; i<pCur->iPage; i++){
70028    n *= pCur->apPage[i]->nCell;
70029  }
70030  return n;
70031}
70032
70033/*
70034** Advance the cursor to the next entry in the database.
70035** Return value:
70036**
70037**    SQLITE_OK        success
70038**    SQLITE_DONE      cursor is already pointing at the last element
70039**    otherwise        some kind of error occurred
70040**
70041** The main entry point is sqlite3BtreeNext().  That routine is optimized
70042** for the common case of merely incrementing the cell counter BtCursor.aiIdx
70043** to the next cell on the current page.  The (slower) btreeNext() helper
70044** routine is called when it is necessary to move to a different page or
70045** to restore the cursor.
70046**
70047** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the
70048** cursor corresponds to an SQL index and this routine could have been
70049** skipped if the SQL index had been a unique index.  The F argument
70050** is a hint to the implement.  SQLite btree implementation does not use
70051** this hint, but COMDB2 does.
70052*/
70053static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){
70054  int rc;
70055  int idx;
70056  MemPage *pPage;
70057
70058  assert( cursorOwnsBtShared(pCur) );
70059  if( pCur->eState!=CURSOR_VALID ){
70060    assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
70061    rc = restoreCursorPosition(pCur);
70062    if( rc!=SQLITE_OK ){
70063      return rc;
70064    }
70065    if( CURSOR_INVALID==pCur->eState ){
70066      return SQLITE_DONE;
70067    }
70068    if( pCur->eState==CURSOR_SKIPNEXT ){
70069      pCur->eState = CURSOR_VALID;
70070      if( pCur->skipNext>0 ) return SQLITE_OK;
70071    }
70072  }
70073
70074  pPage = pCur->pPage;
70075  idx = ++pCur->ix;
70076  if( !pPage->isInit ){
70077    /* The only known way for this to happen is for there to be a
70078    ** recursive SQL function that does a DELETE operation as part of a
70079    ** SELECT which deletes content out from under an active cursor
70080    ** in a corrupt database file where the table being DELETE-ed from
70081    ** has pages in common with the table being queried.  See TH3
70082    ** module cov1/btree78.test testcase 220 (2018-06-08) for an
70083    ** example. */
70084    return SQLITE_CORRUPT_BKPT;
70085  }
70086
70087  /* If the database file is corrupt, it is possible for the value of idx
70088  ** to be invalid here. This can only occur if a second cursor modifies
70089  ** the page while cursor pCur is holding a reference to it. Which can
70090  ** only happen if the database is corrupt in such a way as to link the
70091  ** page into more than one b-tree structure.
70092  **
70093  ** Update 2019-12-23: appears to long longer be possible after the
70094  ** addition of anotherValidCursor() condition on balance_deeper().  */
70095  harmless( idx>pPage->nCell );
70096
70097  if( idx>=pPage->nCell ){
70098    if( !pPage->leaf ){
70099      rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
70100      if( rc ) return rc;
70101      return moveToLeftmost(pCur);
70102    }
70103    do{
70104      if( pCur->iPage==0 ){
70105        pCur->eState = CURSOR_INVALID;
70106        return SQLITE_DONE;
70107      }
70108      moveToParent(pCur);
70109      pPage = pCur->pPage;
70110    }while( pCur->ix>=pPage->nCell );
70111    if( pPage->intKey ){
70112      return sqlite3BtreeNext(pCur, 0);
70113    }else{
70114      return SQLITE_OK;
70115    }
70116  }
70117  if( pPage->leaf ){
70118    return SQLITE_OK;
70119  }else{
70120    return moveToLeftmost(pCur);
70121  }
70122}
70123SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int flags){
70124  MemPage *pPage;
70125  UNUSED_PARAMETER( flags );  /* Used in COMDB2 but not native SQLite */
70126  assert( cursorOwnsBtShared(pCur) );
70127  assert( flags==0 || flags==1 );
70128  pCur->info.nSize = 0;
70129  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
70130  if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
70131  pPage = pCur->pPage;
70132  if( (++pCur->ix)>=pPage->nCell ){
70133    pCur->ix--;
70134    return btreeNext(pCur);
70135  }
70136  if( pPage->leaf ){
70137    return SQLITE_OK;
70138  }else{
70139    return moveToLeftmost(pCur);
70140  }
70141}
70142
70143/*
70144** Step the cursor to the back to the previous entry in the database.
70145** Return values:
70146**
70147**     SQLITE_OK     success
70148**     SQLITE_DONE   the cursor is already on the first element of the table
70149**     otherwise     some kind of error occurred
70150**
70151** The main entry point is sqlite3BtreePrevious().  That routine is optimized
70152** for the common case of merely decrementing the cell counter BtCursor.aiIdx
70153** to the previous cell on the current page.  The (slower) btreePrevious()
70154** helper routine is called when it is necessary to move to a different page
70155** or to restore the cursor.
70156**
70157** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then
70158** the cursor corresponds to an SQL index and this routine could have been
70159** skipped if the SQL index had been a unique index.  The F argument is a
70160** hint to the implement.  The native SQLite btree implementation does not
70161** use this hint, but COMDB2 does.
70162*/
70163static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){
70164  int rc;
70165  MemPage *pPage;
70166
70167  assert( cursorOwnsBtShared(pCur) );
70168  assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
70169  assert( pCur->info.nSize==0 );
70170  if( pCur->eState!=CURSOR_VALID ){
70171    rc = restoreCursorPosition(pCur);
70172    if( rc!=SQLITE_OK ){
70173      return rc;
70174    }
70175    if( CURSOR_INVALID==pCur->eState ){
70176      return SQLITE_DONE;
70177    }
70178    if( CURSOR_SKIPNEXT==pCur->eState ){
70179      pCur->eState = CURSOR_VALID;
70180      if( pCur->skipNext<0 ) return SQLITE_OK;
70181    }
70182  }
70183
70184  pPage = pCur->pPage;
70185  assert( pPage->isInit );
70186  if( !pPage->leaf ){
70187    int idx = pCur->ix;
70188    rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
70189    if( rc ) return rc;
70190    rc = moveToRightmost(pCur);
70191  }else{
70192    while( pCur->ix==0 ){
70193      if( pCur->iPage==0 ){
70194        pCur->eState = CURSOR_INVALID;
70195        return SQLITE_DONE;
70196      }
70197      moveToParent(pCur);
70198    }
70199    assert( pCur->info.nSize==0 );
70200    assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
70201
70202    pCur->ix--;
70203    pPage = pCur->pPage;
70204    if( pPage->intKey && !pPage->leaf ){
70205      rc = sqlite3BtreePrevious(pCur, 0);
70206    }else{
70207      rc = SQLITE_OK;
70208    }
70209  }
70210  return rc;
70211}
70212SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int flags){
70213  assert( cursorOwnsBtShared(pCur) );
70214  assert( flags==0 || flags==1 );
70215  UNUSED_PARAMETER( flags );  /* Used in COMDB2 but not native SQLite */
70216  pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
70217  pCur->info.nSize = 0;
70218  if( pCur->eState!=CURSOR_VALID
70219   || pCur->ix==0
70220   || pCur->pPage->leaf==0
70221  ){
70222    return btreePrevious(pCur);
70223  }
70224  pCur->ix--;
70225  return SQLITE_OK;
70226}
70227
70228/*
70229** Allocate a new page from the database file.
70230**
70231** The new page is marked as dirty.  (In other words, sqlite3PagerWrite()
70232** has already been called on the new page.)  The new page has also
70233** been referenced and the calling routine is responsible for calling
70234** sqlite3PagerUnref() on the new page when it is done.
70235**
70236** SQLITE_OK is returned on success.  Any other return value indicates
70237** an error.  *ppPage is set to NULL in the event of an error.
70238**
70239** If the "nearby" parameter is not 0, then an effort is made to
70240** locate a page close to the page number "nearby".  This can be used in an
70241** attempt to keep related pages close to each other in the database file,
70242** which in turn can make database access faster.
70243**
70244** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
70245** anywhere on the free-list, then it is guaranteed to be returned.  If
70246** eMode is BTALLOC_LT then the page returned will be less than or equal
70247** to nearby if any such page exists.  If eMode is BTALLOC_ANY then there
70248** are no restrictions on which page is returned.
70249*/
70250static int allocateBtreePage(
70251  BtShared *pBt,         /* The btree */
70252  MemPage **ppPage,      /* Store pointer to the allocated page here */
70253  Pgno *pPgno,           /* Store the page number here */
70254  Pgno nearby,           /* Search for a page near this one */
70255  u8 eMode               /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
70256){
70257  MemPage *pPage1;
70258  int rc;
70259  u32 n;     /* Number of pages on the freelist */
70260  u32 k;     /* Number of leaves on the trunk of the freelist */
70261  MemPage *pTrunk = 0;
70262  MemPage *pPrevTrunk = 0;
70263  Pgno mxPage;     /* Total size of the database file */
70264
70265  assert( sqlite3_mutex_held(pBt->mutex) );
70266  assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
70267  pPage1 = pBt->pPage1;
70268  mxPage = btreePagecount(pBt);
70269  /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
70270  ** stores stores the total number of pages on the freelist. */
70271  n = get4byte(&pPage1->aData[36]);
70272  testcase( n==mxPage-1 );
70273  if( n>=mxPage ){
70274    return SQLITE_CORRUPT_BKPT;
70275  }
70276  if( n>0 ){
70277    /* There are pages on the freelist.  Reuse one of those pages. */
70278    Pgno iTrunk;
70279    u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
70280    u32 nSearch = 0;   /* Count of the number of search attempts */
70281
70282    /* If eMode==BTALLOC_EXACT and a query of the pointer-map
70283    ** shows that the page 'nearby' is somewhere on the free-list, then
70284    ** the entire-list will be searched for that page.
70285    */
70286#ifndef SQLITE_OMIT_AUTOVACUUM
70287    if( eMode==BTALLOC_EXACT ){
70288      if( nearby<=mxPage ){
70289        u8 eType;
70290        assert( nearby>0 );
70291        assert( pBt->autoVacuum );
70292        rc = ptrmapGet(pBt, nearby, &eType, 0);
70293        if( rc ) return rc;
70294        if( eType==PTRMAP_FREEPAGE ){
70295          searchList = 1;
70296        }
70297      }
70298    }else if( eMode==BTALLOC_LE ){
70299      searchList = 1;
70300    }
70301#endif
70302
70303    /* Decrement the free-list count by 1. Set iTrunk to the index of the
70304    ** first free-list trunk page. iPrevTrunk is initially 1.
70305    */
70306    rc = sqlite3PagerWrite(pPage1->pDbPage);
70307    if( rc ) return rc;
70308    put4byte(&pPage1->aData[36], n-1);
70309
70310    /* The code within this loop is run only once if the 'searchList' variable
70311    ** is not true. Otherwise, it runs once for each trunk-page on the
70312    ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
70313    ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
70314    */
70315    do {
70316      pPrevTrunk = pTrunk;
70317      if( pPrevTrunk ){
70318        /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
70319        ** is the page number of the next freelist trunk page in the list or
70320        ** zero if this is the last freelist trunk page. */
70321        iTrunk = get4byte(&pPrevTrunk->aData[0]);
70322      }else{
70323        /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
70324        ** stores the page number of the first page of the freelist, or zero if
70325        ** the freelist is empty. */
70326        iTrunk = get4byte(&pPage1->aData[32]);
70327      }
70328      testcase( iTrunk==mxPage );
70329      if( iTrunk>mxPage || nSearch++ > n ){
70330        rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
70331      }else{
70332        rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
70333      }
70334      if( rc ){
70335        pTrunk = 0;
70336        goto end_allocate_page;
70337      }
70338      assert( pTrunk!=0 );
70339      assert( pTrunk->aData!=0 );
70340      /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
70341      ** is the number of leaf page pointers to follow. */
70342      k = get4byte(&pTrunk->aData[4]);
70343      if( k==0 && !searchList ){
70344        /* The trunk has no leaves and the list is not being searched.
70345        ** So extract the trunk page itself and use it as the newly
70346        ** allocated page */
70347        assert( pPrevTrunk==0 );
70348        rc = sqlite3PagerWrite(pTrunk->pDbPage);
70349        if( rc ){
70350          goto end_allocate_page;
70351        }
70352        *pPgno = iTrunk;
70353        memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
70354        *ppPage = pTrunk;
70355        pTrunk = 0;
70356        TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
70357      }else if( k>(u32)(pBt->usableSize/4 - 2) ){
70358        /* Value of k is out of range.  Database corruption */
70359        rc = SQLITE_CORRUPT_PGNO(iTrunk);
70360        goto end_allocate_page;
70361#ifndef SQLITE_OMIT_AUTOVACUUM
70362      }else if( searchList
70363            && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE))
70364      ){
70365        /* The list is being searched and this trunk page is the page
70366        ** to allocate, regardless of whether it has leaves.
70367        */
70368        *pPgno = iTrunk;
70369        *ppPage = pTrunk;
70370        searchList = 0;
70371        rc = sqlite3PagerWrite(pTrunk->pDbPage);
70372        if( rc ){
70373          goto end_allocate_page;
70374        }
70375        if( k==0 ){
70376          if( !pPrevTrunk ){
70377            memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
70378          }else{
70379            rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
70380            if( rc!=SQLITE_OK ){
70381              goto end_allocate_page;
70382            }
70383            memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
70384          }
70385        }else{
70386          /* The trunk page is required by the caller but it contains
70387          ** pointers to free-list leaves. The first leaf becomes a trunk
70388          ** page in this case.
70389          */
70390          MemPage *pNewTrunk;
70391          Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
70392          if( iNewTrunk>mxPage ){
70393            rc = SQLITE_CORRUPT_PGNO(iTrunk);
70394            goto end_allocate_page;
70395          }
70396          testcase( iNewTrunk==mxPage );
70397          rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
70398          if( rc!=SQLITE_OK ){
70399            goto end_allocate_page;
70400          }
70401          rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
70402          if( rc!=SQLITE_OK ){
70403            releasePage(pNewTrunk);
70404            goto end_allocate_page;
70405          }
70406          memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
70407          put4byte(&pNewTrunk->aData[4], k-1);
70408          memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
70409          releasePage(pNewTrunk);
70410          if( !pPrevTrunk ){
70411            assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
70412            put4byte(&pPage1->aData[32], iNewTrunk);
70413          }else{
70414            rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
70415            if( rc ){
70416              goto end_allocate_page;
70417            }
70418            put4byte(&pPrevTrunk->aData[0], iNewTrunk);
70419          }
70420        }
70421        pTrunk = 0;
70422        TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
70423#endif
70424      }else if( k>0 ){
70425        /* Extract a leaf from the trunk */
70426        u32 closest;
70427        Pgno iPage;
70428        unsigned char *aData = pTrunk->aData;
70429        if( nearby>0 ){
70430          u32 i;
70431          closest = 0;
70432          if( eMode==BTALLOC_LE ){
70433            for(i=0; i<k; i++){
70434              iPage = get4byte(&aData[8+i*4]);
70435              if( iPage<=nearby ){
70436                closest = i;
70437                break;
70438              }
70439            }
70440          }else{
70441            int dist;
70442            dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
70443            for(i=1; i<k; i++){
70444              int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
70445              if( d2<dist ){
70446                closest = i;
70447                dist = d2;
70448              }
70449            }
70450          }
70451        }else{
70452          closest = 0;
70453        }
70454
70455        iPage = get4byte(&aData[8+closest*4]);
70456        testcase( iPage==mxPage );
70457        if( iPage>mxPage ){
70458          rc = SQLITE_CORRUPT_PGNO(iTrunk);
70459          goto end_allocate_page;
70460        }
70461        testcase( iPage==mxPage );
70462        if( !searchList
70463         || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE))
70464        ){
70465          int noContent;
70466          *pPgno = iPage;
70467          TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
70468                 ": %d more free pages\n",
70469                 *pPgno, closest+1, k, pTrunk->pgno, n-1));
70470          rc = sqlite3PagerWrite(pTrunk->pDbPage);
70471          if( rc ) goto end_allocate_page;
70472          if( closest<k-1 ){
70473            memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
70474          }
70475          put4byte(&aData[4], k-1);
70476          noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
70477          rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
70478          if( rc==SQLITE_OK ){
70479            rc = sqlite3PagerWrite((*ppPage)->pDbPage);
70480            if( rc!=SQLITE_OK ){
70481              releasePage(*ppPage);
70482              *ppPage = 0;
70483            }
70484          }
70485          searchList = 0;
70486        }
70487      }
70488      releasePage(pPrevTrunk);
70489      pPrevTrunk = 0;
70490    }while( searchList );
70491  }else{
70492    /* There are no pages on the freelist, so append a new page to the
70493    ** database image.
70494    **
70495    ** Normally, new pages allocated by this block can be requested from the
70496    ** pager layer with the 'no-content' flag set. This prevents the pager
70497    ** from trying to read the pages content from disk. However, if the
70498    ** current transaction has already run one or more incremental-vacuum
70499    ** steps, then the page we are about to allocate may contain content
70500    ** that is required in the event of a rollback. In this case, do
70501    ** not set the no-content flag. This causes the pager to load and journal
70502    ** the current page content before overwriting it.
70503    **
70504    ** Note that the pager will not actually attempt to load or journal
70505    ** content for any page that really does lie past the end of the database
70506    ** file on disk. So the effects of disabling the no-content optimization
70507    ** here are confined to those pages that lie between the end of the
70508    ** database image and the end of the database file.
70509    */
70510    int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
70511
70512    rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
70513    if( rc ) return rc;
70514    pBt->nPage++;
70515    if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
70516
70517#ifndef SQLITE_OMIT_AUTOVACUUM
70518    if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
70519      /* If *pPgno refers to a pointer-map page, allocate two new pages
70520      ** at the end of the file instead of one. The first allocated page
70521      ** becomes a new pointer-map page, the second is used by the caller.
70522      */
70523      MemPage *pPg = 0;
70524      TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
70525      assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
70526      rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
70527      if( rc==SQLITE_OK ){
70528        rc = sqlite3PagerWrite(pPg->pDbPage);
70529        releasePage(pPg);
70530      }
70531      if( rc ) return rc;
70532      pBt->nPage++;
70533      if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
70534    }
70535#endif
70536    put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
70537    *pPgno = pBt->nPage;
70538
70539    assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
70540    rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
70541    if( rc ) return rc;
70542    rc = sqlite3PagerWrite((*ppPage)->pDbPage);
70543    if( rc!=SQLITE_OK ){
70544      releasePage(*ppPage);
70545      *ppPage = 0;
70546    }
70547    TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
70548  }
70549
70550  assert( CORRUPT_DB || *pPgno!=PENDING_BYTE_PAGE(pBt) );
70551
70552end_allocate_page:
70553  releasePage(pTrunk);
70554  releasePage(pPrevTrunk);
70555  assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
70556  assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
70557  return rc;
70558}
70559
70560/*
70561** This function is used to add page iPage to the database file free-list.
70562** It is assumed that the page is not already a part of the free-list.
70563**
70564** The value passed as the second argument to this function is optional.
70565** If the caller happens to have a pointer to the MemPage object
70566** corresponding to page iPage handy, it may pass it as the second value.
70567** Otherwise, it may pass NULL.
70568**
70569** If a pointer to a MemPage object is passed as the second argument,
70570** its reference count is not altered by this function.
70571*/
70572static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
70573  MemPage *pTrunk = 0;                /* Free-list trunk page */
70574  Pgno iTrunk = 0;                    /* Page number of free-list trunk page */
70575  MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
70576  MemPage *pPage;                     /* Page being freed. May be NULL. */
70577  int rc;                             /* Return Code */
70578  u32 nFree;                          /* Initial number of pages on free-list */
70579
70580  assert( sqlite3_mutex_held(pBt->mutex) );
70581  assert( CORRUPT_DB || iPage>1 );
70582  assert( !pMemPage || pMemPage->pgno==iPage );
70583
70584  if( iPage<2 || iPage>pBt->nPage ){
70585    return SQLITE_CORRUPT_BKPT;
70586  }
70587  if( pMemPage ){
70588    pPage = pMemPage;
70589    sqlite3PagerRef(pPage->pDbPage);
70590  }else{
70591    pPage = btreePageLookup(pBt, iPage);
70592  }
70593
70594  /* Increment the free page count on pPage1 */
70595  rc = sqlite3PagerWrite(pPage1->pDbPage);
70596  if( rc ) goto freepage_out;
70597  nFree = get4byte(&pPage1->aData[36]);
70598  put4byte(&pPage1->aData[36], nFree+1);
70599
70600  if( pBt->btsFlags & BTS_SECURE_DELETE ){
70601    /* If the secure_delete option is enabled, then
70602    ** always fully overwrite deleted information with zeros.
70603    */
70604    if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
70605     ||            ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
70606    ){
70607      goto freepage_out;
70608    }
70609    memset(pPage->aData, 0, pPage->pBt->pageSize);
70610  }
70611
70612  /* If the database supports auto-vacuum, write an entry in the pointer-map
70613  ** to indicate that the page is free.
70614  */
70615  if( ISAUTOVACUUM ){
70616    ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
70617    if( rc ) goto freepage_out;
70618  }
70619
70620  /* Now manipulate the actual database free-list structure. There are two
70621  ** possibilities. If the free-list is currently empty, or if the first
70622  ** trunk page in the free-list is full, then this page will become a
70623  ** new free-list trunk page. Otherwise, it will become a leaf of the
70624  ** first trunk page in the current free-list. This block tests if it
70625  ** is possible to add the page as a new free-list leaf.
70626  */
70627  if( nFree!=0 ){
70628    u32 nLeaf;                /* Initial number of leaf cells on trunk page */
70629
70630    iTrunk = get4byte(&pPage1->aData[32]);
70631    rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
70632    if( rc!=SQLITE_OK ){
70633      goto freepage_out;
70634    }
70635
70636    nLeaf = get4byte(&pTrunk->aData[4]);
70637    assert( pBt->usableSize>32 );
70638    if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
70639      rc = SQLITE_CORRUPT_BKPT;
70640      goto freepage_out;
70641    }
70642    if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
70643      /* In this case there is room on the trunk page to insert the page
70644      ** being freed as a new leaf.
70645      **
70646      ** Note that the trunk page is not really full until it contains
70647      ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
70648      ** coded.  But due to a coding error in versions of SQLite prior to
70649      ** 3.6.0, databases with freelist trunk pages holding more than
70650      ** usableSize/4 - 8 entries will be reported as corrupt.  In order
70651      ** to maintain backwards compatibility with older versions of SQLite,
70652      ** we will continue to restrict the number of entries to usableSize/4 - 8
70653      ** for now.  At some point in the future (once everyone has upgraded
70654      ** to 3.6.0 or later) we should consider fixing the conditional above
70655      ** to read "usableSize/4-2" instead of "usableSize/4-8".
70656      **
70657      ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
70658      ** avoid using the last six entries in the freelist trunk page array in
70659      ** order that database files created by newer versions of SQLite can be
70660      ** read by older versions of SQLite.
70661      */
70662      rc = sqlite3PagerWrite(pTrunk->pDbPage);
70663      if( rc==SQLITE_OK ){
70664        put4byte(&pTrunk->aData[4], nLeaf+1);
70665        put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
70666        if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
70667          sqlite3PagerDontWrite(pPage->pDbPage);
70668        }
70669        rc = btreeSetHasContent(pBt, iPage);
70670      }
70671      TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
70672      goto freepage_out;
70673    }
70674  }
70675
70676  /* If control flows to this point, then it was not possible to add the
70677  ** the page being freed as a leaf page of the first trunk in the free-list.
70678  ** Possibly because the free-list is empty, or possibly because the
70679  ** first trunk in the free-list is full. Either way, the page being freed
70680  ** will become the new first trunk page in the free-list.
70681  */
70682  if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
70683    goto freepage_out;
70684  }
70685  rc = sqlite3PagerWrite(pPage->pDbPage);
70686  if( rc!=SQLITE_OK ){
70687    goto freepage_out;
70688  }
70689  put4byte(pPage->aData, iTrunk);
70690  put4byte(&pPage->aData[4], 0);
70691  put4byte(&pPage1->aData[32], iPage);
70692  TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
70693
70694freepage_out:
70695  if( pPage ){
70696    pPage->isInit = 0;
70697  }
70698  releasePage(pPage);
70699  releasePage(pTrunk);
70700  return rc;
70701}
70702static void freePage(MemPage *pPage, int *pRC){
70703  if( (*pRC)==SQLITE_OK ){
70704    *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
70705  }
70706}
70707
70708/*
70709** Free any overflow pages associated with the given Cell.  Store
70710** size information about the cell in pInfo.
70711*/
70712static int clearCell(
70713  MemPage *pPage,          /* The page that contains the Cell */
70714  unsigned char *pCell,    /* First byte of the Cell */
70715  CellInfo *pInfo          /* Size information about the cell */
70716){
70717  BtShared *pBt;
70718  Pgno ovflPgno;
70719  int rc;
70720  int nOvfl;
70721  u32 ovflPageSize;
70722
70723  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70724  pPage->xParseCell(pPage, pCell, pInfo);
70725  if( pInfo->nLocal==pInfo->nPayload ){
70726    return SQLITE_OK;  /* No overflow pages. Return without doing anything */
70727  }
70728  testcase( pCell + pInfo->nSize == pPage->aDataEnd );
70729  testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
70730  if( pCell + pInfo->nSize > pPage->aDataEnd ){
70731    /* Cell extends past end of page */
70732    return SQLITE_CORRUPT_PAGE(pPage);
70733  }
70734  ovflPgno = get4byte(pCell + pInfo->nSize - 4);
70735  pBt = pPage->pBt;
70736  assert( pBt->usableSize > 4 );
70737  ovflPageSize = pBt->usableSize - 4;
70738  nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
70739  assert( nOvfl>0 ||
70740    (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
70741  );
70742  while( nOvfl-- ){
70743    Pgno iNext = 0;
70744    MemPage *pOvfl = 0;
70745    if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
70746      /* 0 is not a legal page number and page 1 cannot be an
70747      ** overflow page. Therefore if ovflPgno<2 or past the end of the
70748      ** file the database must be corrupt. */
70749      return SQLITE_CORRUPT_BKPT;
70750    }
70751    if( nOvfl ){
70752      rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
70753      if( rc ) return rc;
70754    }
70755
70756    if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
70757     && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
70758    ){
70759      /* There is no reason any cursor should have an outstanding reference
70760      ** to an overflow page belonging to a cell that is being deleted/updated.
70761      ** So if there exists more than one reference to this page, then it
70762      ** must not really be an overflow page and the database must be corrupt.
70763      ** It is helpful to detect this before calling freePage2(), as
70764      ** freePage2() may zero the page contents if secure-delete mode is
70765      ** enabled. If this 'overflow' page happens to be a page that the
70766      ** caller is iterating through or using in some other way, this
70767      ** can be problematic.
70768      */
70769      rc = SQLITE_CORRUPT_BKPT;
70770    }else{
70771      rc = freePage2(pBt, pOvfl, ovflPgno);
70772    }
70773
70774    if( pOvfl ){
70775      sqlite3PagerUnref(pOvfl->pDbPage);
70776    }
70777    if( rc ) return rc;
70778    ovflPgno = iNext;
70779  }
70780  return SQLITE_OK;
70781}
70782
70783/*
70784** Create the byte sequence used to represent a cell on page pPage
70785** and write that byte sequence into pCell[].  Overflow pages are
70786** allocated and filled in as necessary.  The calling procedure
70787** is responsible for making sure sufficient space has been allocated
70788** for pCell[].
70789**
70790** Note that pCell does not necessary need to point to the pPage->aData
70791** area.  pCell might point to some temporary storage.  The cell will
70792** be constructed in this temporary area then copied into pPage->aData
70793** later.
70794*/
70795static int fillInCell(
70796  MemPage *pPage,                /* The page that contains the cell */
70797  unsigned char *pCell,          /* Complete text of the cell */
70798  const BtreePayload *pX,        /* Payload with which to construct the cell */
70799  int *pnSize                    /* Write cell size here */
70800){
70801  int nPayload;
70802  const u8 *pSrc;
70803  int nSrc, n, rc, mn;
70804  int spaceLeft;
70805  MemPage *pToRelease;
70806  unsigned char *pPrior;
70807  unsigned char *pPayload;
70808  BtShared *pBt;
70809  Pgno pgnoOvfl;
70810  int nHeader;
70811
70812  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70813
70814  /* pPage is not necessarily writeable since pCell might be auxiliary
70815  ** buffer space that is separate from the pPage buffer area */
70816  assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
70817            || sqlite3PagerIswriteable(pPage->pDbPage) );
70818
70819  /* Fill in the header. */
70820  nHeader = pPage->childPtrSize;
70821  if( pPage->intKey ){
70822    nPayload = pX->nData + pX->nZero;
70823    pSrc = pX->pData;
70824    nSrc = pX->nData;
70825    assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
70826    nHeader += putVarint32(&pCell[nHeader], nPayload);
70827    nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
70828  }else{
70829    assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
70830    nSrc = nPayload = (int)pX->nKey;
70831    pSrc = pX->pKey;
70832    nHeader += putVarint32(&pCell[nHeader], nPayload);
70833  }
70834
70835  /* Fill in the payload */
70836  pPayload = &pCell[nHeader];
70837  if( nPayload<=pPage->maxLocal ){
70838    /* This is the common case where everything fits on the btree page
70839    ** and no overflow pages are required. */
70840    n = nHeader + nPayload;
70841    testcase( n==3 );
70842    testcase( n==4 );
70843    if( n<4 ) n = 4;
70844    *pnSize = n;
70845    assert( nSrc<=nPayload );
70846    testcase( nSrc<nPayload );
70847    memcpy(pPayload, pSrc, nSrc);
70848    memset(pPayload+nSrc, 0, nPayload-nSrc);
70849    return SQLITE_OK;
70850  }
70851
70852  /* If we reach this point, it means that some of the content will need
70853  ** to spill onto overflow pages.
70854  */
70855  mn = pPage->minLocal;
70856  n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
70857  testcase( n==pPage->maxLocal );
70858  testcase( n==pPage->maxLocal+1 );
70859  if( n > pPage->maxLocal ) n = mn;
70860  spaceLeft = n;
70861  *pnSize = n + nHeader + 4;
70862  pPrior = &pCell[nHeader+n];
70863  pToRelease = 0;
70864  pgnoOvfl = 0;
70865  pBt = pPage->pBt;
70866
70867  /* At this point variables should be set as follows:
70868  **
70869  **   nPayload           Total payload size in bytes
70870  **   pPayload           Begin writing payload here
70871  **   spaceLeft          Space available at pPayload.  If nPayload>spaceLeft,
70872  **                      that means content must spill into overflow pages.
70873  **   *pnSize            Size of the local cell (not counting overflow pages)
70874  **   pPrior             Where to write the pgno of the first overflow page
70875  **
70876  ** Use a call to btreeParseCellPtr() to verify that the values above
70877  ** were computed correctly.
70878  */
70879#ifdef SQLITE_DEBUG
70880  {
70881    CellInfo info;
70882    pPage->xParseCell(pPage, pCell, &info);
70883    assert( nHeader==(int)(info.pPayload - pCell) );
70884    assert( info.nKey==pX->nKey );
70885    assert( *pnSize == info.nSize );
70886    assert( spaceLeft == info.nLocal );
70887  }
70888#endif
70889
70890  /* Write the payload into the local Cell and any extra into overflow pages */
70891  while( 1 ){
70892    n = nPayload;
70893    if( n>spaceLeft ) n = spaceLeft;
70894
70895    /* If pToRelease is not zero than pPayload points into the data area
70896    ** of pToRelease.  Make sure pToRelease is still writeable. */
70897    assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
70898
70899    /* If pPayload is part of the data area of pPage, then make sure pPage
70900    ** is still writeable */
70901    assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
70902            || sqlite3PagerIswriteable(pPage->pDbPage) );
70903
70904    if( nSrc>=n ){
70905      memcpy(pPayload, pSrc, n);
70906    }else if( nSrc>0 ){
70907      n = nSrc;
70908      memcpy(pPayload, pSrc, n);
70909    }else{
70910      memset(pPayload, 0, n);
70911    }
70912    nPayload -= n;
70913    if( nPayload<=0 ) break;
70914    pPayload += n;
70915    pSrc += n;
70916    nSrc -= n;
70917    spaceLeft -= n;
70918    if( spaceLeft==0 ){
70919      MemPage *pOvfl = 0;
70920#ifndef SQLITE_OMIT_AUTOVACUUM
70921      Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
70922      if( pBt->autoVacuum ){
70923        do{
70924          pgnoOvfl++;
70925        } while(
70926          PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
70927        );
70928      }
70929#endif
70930      rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
70931#ifndef SQLITE_OMIT_AUTOVACUUM
70932      /* If the database supports auto-vacuum, and the second or subsequent
70933      ** overflow page is being allocated, add an entry to the pointer-map
70934      ** for that page now.
70935      **
70936      ** If this is the first overflow page, then write a partial entry
70937      ** to the pointer-map. If we write nothing to this pointer-map slot,
70938      ** then the optimistic overflow chain processing in clearCell()
70939      ** may misinterpret the uninitialized values and delete the
70940      ** wrong pages from the database.
70941      */
70942      if( pBt->autoVacuum && rc==SQLITE_OK ){
70943        u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
70944        ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
70945        if( rc ){
70946          releasePage(pOvfl);
70947        }
70948      }
70949#endif
70950      if( rc ){
70951        releasePage(pToRelease);
70952        return rc;
70953      }
70954
70955      /* If pToRelease is not zero than pPrior points into the data area
70956      ** of pToRelease.  Make sure pToRelease is still writeable. */
70957      assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
70958
70959      /* If pPrior is part of the data area of pPage, then make sure pPage
70960      ** is still writeable */
70961      assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
70962            || sqlite3PagerIswriteable(pPage->pDbPage) );
70963
70964      put4byte(pPrior, pgnoOvfl);
70965      releasePage(pToRelease);
70966      pToRelease = pOvfl;
70967      pPrior = pOvfl->aData;
70968      put4byte(pPrior, 0);
70969      pPayload = &pOvfl->aData[4];
70970      spaceLeft = pBt->usableSize - 4;
70971    }
70972  }
70973  releasePage(pToRelease);
70974  return SQLITE_OK;
70975}
70976
70977/*
70978** Remove the i-th cell from pPage.  This routine effects pPage only.
70979** The cell content is not freed or deallocated.  It is assumed that
70980** the cell content has been copied someplace else.  This routine just
70981** removes the reference to the cell from pPage.
70982**
70983** "sz" must be the number of bytes in the cell.
70984*/
70985static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
70986  u32 pc;         /* Offset to cell content of cell being deleted */
70987  u8 *data;       /* pPage->aData */
70988  u8 *ptr;        /* Used to move bytes around within data[] */
70989  int rc;         /* The return code */
70990  int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
70991
70992  if( *pRC ) return;
70993  assert( idx>=0 && idx<pPage->nCell );
70994  assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
70995  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70996  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70997  assert( pPage->nFree>=0 );
70998  data = pPage->aData;
70999  ptr = &pPage->aCellIdx[2*idx];
71000  pc = get2byte(ptr);
71001  hdr = pPage->hdrOffset;
71002  testcase( pc==get2byte(&data[hdr+5]) );
71003  testcase( pc+sz==pPage->pBt->usableSize );
71004  if( pc+sz > pPage->pBt->usableSize ){
71005    *pRC = SQLITE_CORRUPT_BKPT;
71006    return;
71007  }
71008  rc = freeSpace(pPage, pc, sz);
71009  if( rc ){
71010    *pRC = rc;
71011    return;
71012  }
71013  pPage->nCell--;
71014  if( pPage->nCell==0 ){
71015    memset(&data[hdr+1], 0, 4);
71016    data[hdr+7] = 0;
71017    put2byte(&data[hdr+5], pPage->pBt->usableSize);
71018    pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
71019                       - pPage->childPtrSize - 8;
71020  }else{
71021    memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
71022    put2byte(&data[hdr+3], pPage->nCell);
71023    pPage->nFree += 2;
71024  }
71025}
71026
71027/*
71028** Insert a new cell on pPage at cell index "i".  pCell points to the
71029** content of the cell.
71030**
71031** If the cell content will fit on the page, then put it there.  If it
71032** will not fit, then make a copy of the cell content into pTemp if
71033** pTemp is not null.  Regardless of pTemp, allocate a new entry
71034** in pPage->apOvfl[] and make it point to the cell content (either
71035** in pTemp or the original pCell) and also record its index.
71036** Allocating a new entry in pPage->aCell[] implies that
71037** pPage->nOverflow is incremented.
71038**
71039** *pRC must be SQLITE_OK when this routine is called.
71040*/
71041static void insertCell(
71042  MemPage *pPage,   /* Page into which we are copying */
71043  int i,            /* New cell becomes the i-th cell of the page */
71044  u8 *pCell,        /* Content of the new cell */
71045  int sz,           /* Bytes of content in pCell */
71046  u8 *pTemp,        /* Temp storage space for pCell, if needed */
71047  Pgno iChild,      /* If non-zero, replace first 4 bytes with this value */
71048  int *pRC          /* Read and write return code from here */
71049){
71050  int idx = 0;      /* Where to write new cell content in data[] */
71051  int j;            /* Loop counter */
71052  u8 *data;         /* The content of the whole page */
71053  u8 *pIns;         /* The point in pPage->aCellIdx[] where no cell inserted */
71054
71055  assert( *pRC==SQLITE_OK );
71056  assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
71057  assert( MX_CELL(pPage->pBt)<=10921 );
71058  assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
71059  assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
71060  assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
71061  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71062  assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
71063  assert( pPage->nFree>=0 );
71064  if( pPage->nOverflow || sz+2>pPage->nFree ){
71065    if( pTemp ){
71066      memcpy(pTemp, pCell, sz);
71067      pCell = pTemp;
71068    }
71069    if( iChild ){
71070      put4byte(pCell, iChild);
71071    }
71072    j = pPage->nOverflow++;
71073    /* Comparison against ArraySize-1 since we hold back one extra slot
71074    ** as a contingency.  In other words, never need more than 3 overflow
71075    ** slots but 4 are allocated, just to be safe. */
71076    assert( j < ArraySize(pPage->apOvfl)-1 );
71077    pPage->apOvfl[j] = pCell;
71078    pPage->aiOvfl[j] = (u16)i;
71079
71080    /* When multiple overflows occur, they are always sequential and in
71081    ** sorted order.  This invariants arise because multiple overflows can
71082    ** only occur when inserting divider cells into the parent page during
71083    ** balancing, and the dividers are adjacent and sorted.
71084    */
71085    assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
71086    assert( j==0 || i==pPage->aiOvfl[j-1]+1 );   /* Overflows are sequential */
71087  }else{
71088    int rc = sqlite3PagerWrite(pPage->pDbPage);
71089    if( rc!=SQLITE_OK ){
71090      *pRC = rc;
71091      return;
71092    }
71093    assert( sqlite3PagerIswriteable(pPage->pDbPage) );
71094    data = pPage->aData;
71095    assert( &data[pPage->cellOffset]==pPage->aCellIdx );
71096    rc = allocateSpace(pPage, sz, &idx);
71097    if( rc ){ *pRC = rc; return; }
71098    /* The allocateSpace() routine guarantees the following properties
71099    ** if it returns successfully */
71100    assert( idx >= 0 );
71101    assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
71102    assert( idx+sz <= (int)pPage->pBt->usableSize );
71103    pPage->nFree -= (u16)(2 + sz);
71104    if( iChild ){
71105      /* In a corrupt database where an entry in the cell index section of
71106      ** a btree page has a value of 3 or less, the pCell value might point
71107      ** as many as 4 bytes in front of the start of the aData buffer for
71108      ** the source page.  Make sure this does not cause problems by not
71109      ** reading the first 4 bytes */
71110      memcpy(&data[idx+4], pCell+4, sz-4);
71111      put4byte(&data[idx], iChild);
71112    }else{
71113      memcpy(&data[idx], pCell, sz);
71114    }
71115    pIns = pPage->aCellIdx + i*2;
71116    memmove(pIns+2, pIns, 2*(pPage->nCell - i));
71117    put2byte(pIns, idx);
71118    pPage->nCell++;
71119    /* increment the cell count */
71120    if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
71121    assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
71122#ifndef SQLITE_OMIT_AUTOVACUUM
71123    if( pPage->pBt->autoVacuum ){
71124      /* The cell may contain a pointer to an overflow page. If so, write
71125      ** the entry for the overflow page into the pointer map.
71126      */
71127      ptrmapPutOvflPtr(pPage, pPage, pCell, pRC);
71128    }
71129#endif
71130  }
71131}
71132
71133/*
71134** The following parameters determine how many adjacent pages get involved
71135** in a balancing operation.  NN is the number of neighbors on either side
71136** of the page that participate in the balancing operation.  NB is the
71137** total number of pages that participate, including the target page and
71138** NN neighbors on either side.
71139**
71140** The minimum value of NN is 1 (of course).  Increasing NN above 1
71141** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
71142** in exchange for a larger degradation in INSERT and UPDATE performance.
71143** The value of NN appears to give the best results overall.
71144**
71145** (Later:) The description above makes it seem as if these values are
71146** tunable - as if you could change them and recompile and it would all work.
71147** But that is unlikely.  NB has been 3 since the inception of SQLite and
71148** we have never tested any other value.
71149*/
71150#define NN 1             /* Number of neighbors on either side of pPage */
71151#define NB 3             /* (NN*2+1): Total pages involved in the balance */
71152
71153/*
71154** A CellArray object contains a cache of pointers and sizes for a
71155** consecutive sequence of cells that might be held on multiple pages.
71156**
71157** The cells in this array are the divider cell or cells from the pParent
71158** page plus up to three child pages.  There are a total of nCell cells.
71159**
71160** pRef is a pointer to one of the pages that contributes cells.  This is
71161** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
71162** which should be common to all pages that contribute cells to this array.
71163**
71164** apCell[] and szCell[] hold, respectively, pointers to the start of each
71165** cell and the size of each cell.  Some of the apCell[] pointers might refer
71166** to overflow cells.  In other words, some apCel[] pointers might not point
71167** to content area of the pages.
71168**
71169** A szCell[] of zero means the size of that cell has not yet been computed.
71170**
71171** The cells come from as many as four different pages:
71172**
71173**             -----------
71174**             | Parent  |
71175**             -----------
71176**            /     |     \
71177**           /      |      \
71178**  ---------   ---------   ---------
71179**  |Child-1|   |Child-2|   |Child-3|
71180**  ---------   ---------   ---------
71181**
71182** The order of cells is in the array is for an index btree is:
71183**
71184**       1.  All cells from Child-1 in order
71185**       2.  The first divider cell from Parent
71186**       3.  All cells from Child-2 in order
71187**       4.  The second divider cell from Parent
71188**       5.  All cells from Child-3 in order
71189**
71190** For a table-btree (with rowids) the items 2 and 4 are empty because
71191** content exists only in leaves and there are no divider cells.
71192**
71193** For an index btree, the apEnd[] array holds pointer to the end of page
71194** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
71195** respectively. The ixNx[] array holds the number of cells contained in
71196** each of these 5 stages, and all stages to the left.  Hence:
71197**
71198**    ixNx[0] = Number of cells in Child-1.
71199**    ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
71200**    ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
71201**    ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
71202**    ixNx[4] = Total number of cells.
71203**
71204** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
71205** are used and they point to the leaf pages only, and the ixNx value are:
71206**
71207**    ixNx[0] = Number of cells in Child-1.
71208**    ixNx[1] = Number of cells in Child-1 and Child-2.
71209**    ixNx[2] = Total number of cells.
71210**
71211** Sometimes when deleting, a child page can have zero cells.  In those
71212** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[]
71213** entries, shift down.  The end result is that each ixNx[] entry should
71214** be larger than the previous
71215*/
71216typedef struct CellArray CellArray;
71217struct CellArray {
71218  int nCell;              /* Number of cells in apCell[] */
71219  MemPage *pRef;          /* Reference page */
71220  u8 **apCell;            /* All cells begin balanced */
71221  u16 *szCell;            /* Local size of all cells in apCell[] */
71222  u8 *apEnd[NB*2];        /* MemPage.aDataEnd values */
71223  int ixNx[NB*2];         /* Index of at which we move to the next apEnd[] */
71224};
71225
71226/*
71227** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
71228** computed.
71229*/
71230static void populateCellCache(CellArray *p, int idx, int N){
71231  assert( idx>=0 && idx+N<=p->nCell );
71232  while( N>0 ){
71233    assert( p->apCell[idx]!=0 );
71234    if( p->szCell[idx]==0 ){
71235      p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
71236    }else{
71237      assert( CORRUPT_DB ||
71238              p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
71239    }
71240    idx++;
71241    N--;
71242  }
71243}
71244
71245/*
71246** Return the size of the Nth element of the cell array
71247*/
71248static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
71249  assert( N>=0 && N<p->nCell );
71250  assert( p->szCell[N]==0 );
71251  p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
71252  return p->szCell[N];
71253}
71254static u16 cachedCellSize(CellArray *p, int N){
71255  assert( N>=0 && N<p->nCell );
71256  if( p->szCell[N] ) return p->szCell[N];
71257  return computeCellSize(p, N);
71258}
71259
71260/*
71261** Array apCell[] contains pointers to nCell b-tree page cells. The
71262** szCell[] array contains the size in bytes of each cell. This function
71263** replaces the current contents of page pPg with the contents of the cell
71264** array.
71265**
71266** Some of the cells in apCell[] may currently be stored in pPg. This
71267** function works around problems caused by this by making a copy of any
71268** such cells before overwriting the page data.
71269**
71270** The MemPage.nFree field is invalidated by this function. It is the
71271** responsibility of the caller to set it correctly.
71272*/
71273static int rebuildPage(
71274  CellArray *pCArray,             /* Content to be added to page pPg */
71275  int iFirst,                     /* First cell in pCArray to use */
71276  int nCell,                      /* Final number of cells on page */
71277  MemPage *pPg                    /* The page to be reconstructed */
71278){
71279  const int hdr = pPg->hdrOffset;          /* Offset of header on pPg */
71280  u8 * const aData = pPg->aData;           /* Pointer to data for pPg */
71281  const int usableSize = pPg->pBt->usableSize;
71282  u8 * const pEnd = &aData[usableSize];
71283  int i = iFirst;                 /* Which cell to copy from pCArray*/
71284  u32 j;                          /* Start of cell content area */
71285  int iEnd = i+nCell;             /* Loop terminator */
71286  u8 *pCellptr = pPg->aCellIdx;
71287  u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
71288  u8 *pData;
71289  int k;                          /* Current slot in pCArray->apEnd[] */
71290  u8 *pSrcEnd;                    /* Current pCArray->apEnd[k] value */
71291
71292  assert( i<iEnd );
71293  j = get2byte(&aData[hdr+5]);
71294  if( NEVER(j>(u32)usableSize) ){ j = 0; }
71295  memcpy(&pTmp[j], &aData[j], usableSize - j);
71296
71297  for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
71298  pSrcEnd = pCArray->apEnd[k];
71299
71300  pData = pEnd;
71301  while( 1/*exit by break*/ ){
71302    u8 *pCell = pCArray->apCell[i];
71303    u16 sz = pCArray->szCell[i];
71304    assert( sz>0 );
71305    if( SQLITE_WITHIN(pCell,aData,pEnd) ){
71306      if( ((uptr)(pCell+sz))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPT;
71307      pCell = &pTmp[pCell - aData];
71308    }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd
71309           && (uptr)(pCell)<(uptr)pSrcEnd
71310    ){
71311      return SQLITE_CORRUPT_BKPT;
71312    }
71313
71314    pData -= sz;
71315    put2byte(pCellptr, (pData - aData));
71316    pCellptr += 2;
71317    if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
71318    memcpy(pData, pCell, sz);
71319    assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
71320    testcase( sz!=pPg->xCellSize(pPg,pCell) )
71321    i++;
71322    if( i>=iEnd ) break;
71323    if( pCArray->ixNx[k]<=i ){
71324      k++;
71325      pSrcEnd = pCArray->apEnd[k];
71326    }
71327  }
71328
71329  /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
71330  pPg->nCell = nCell;
71331  pPg->nOverflow = 0;
71332
71333  put2byte(&aData[hdr+1], 0);
71334  put2byte(&aData[hdr+3], pPg->nCell);
71335  put2byte(&aData[hdr+5], pData - aData);
71336  aData[hdr+7] = 0x00;
71337  return SQLITE_OK;
71338}
71339
71340/*
71341** The pCArray objects contains pointers to b-tree cells and the cell sizes.
71342** This function attempts to add the cells stored in the array to page pPg.
71343** If it cannot (because the page needs to be defragmented before the cells
71344** will fit), non-zero is returned. Otherwise, if the cells are added
71345** successfully, zero is returned.
71346**
71347** Argument pCellptr points to the first entry in the cell-pointer array
71348** (part of page pPg) to populate. After cell apCell[0] is written to the
71349** page body, a 16-bit offset is written to pCellptr. And so on, for each
71350** cell in the array. It is the responsibility of the caller to ensure
71351** that it is safe to overwrite this part of the cell-pointer array.
71352**
71353** When this function is called, *ppData points to the start of the
71354** content area on page pPg. If the size of the content area is extended,
71355** *ppData is updated to point to the new start of the content area
71356** before returning.
71357**
71358** Finally, argument pBegin points to the byte immediately following the
71359** end of the space required by this page for the cell-pointer area (for
71360** all cells - not just those inserted by the current call). If the content
71361** area must be extended to before this point in order to accomodate all
71362** cells in apCell[], then the cells do not fit and non-zero is returned.
71363*/
71364static int pageInsertArray(
71365  MemPage *pPg,                   /* Page to add cells to */
71366  u8 *pBegin,                     /* End of cell-pointer array */
71367  u8 **ppData,                    /* IN/OUT: Page content-area pointer */
71368  u8 *pCellptr,                   /* Pointer to cell-pointer area */
71369  int iFirst,                     /* Index of first cell to add */
71370  int nCell,                      /* Number of cells to add to pPg */
71371  CellArray *pCArray              /* Array of cells */
71372){
71373  int i = iFirst;                 /* Loop counter - cell index to insert */
71374  u8 *aData = pPg->aData;         /* Complete page */
71375  u8 *pData = *ppData;            /* Content area.  A subset of aData[] */
71376  int iEnd = iFirst + nCell;      /* End of loop. One past last cell to ins */
71377  int k;                          /* Current slot in pCArray->apEnd[] */
71378  u8 *pEnd;                       /* Maximum extent of cell data */
71379  assert( CORRUPT_DB || pPg->hdrOffset==0 );    /* Never called on page 1 */
71380  if( iEnd<=iFirst ) return 0;
71381  for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
71382  pEnd = pCArray->apEnd[k];
71383  while( 1 /*Exit by break*/ ){
71384    int sz, rc;
71385    u8 *pSlot;
71386    assert( pCArray->szCell[i]!=0 );
71387    sz = pCArray->szCell[i];
71388    if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
71389      if( (pData - pBegin)<sz ) return 1;
71390      pData -= sz;
71391      pSlot = pData;
71392    }
71393    /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
71394    ** database.  But they might for a corrupt database.  Hence use memmove()
71395    ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
71396    assert( (pSlot+sz)<=pCArray->apCell[i]
71397         || pSlot>=(pCArray->apCell[i]+sz)
71398         || CORRUPT_DB );
71399    if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
71400     && (uptr)(pCArray->apCell[i])<(uptr)pEnd
71401    ){
71402      assert( CORRUPT_DB );
71403      (void)SQLITE_CORRUPT_BKPT;
71404      return 1;
71405    }
71406    memmove(pSlot, pCArray->apCell[i], sz);
71407    put2byte(pCellptr, (pSlot - aData));
71408    pCellptr += 2;
71409    i++;
71410    if( i>=iEnd ) break;
71411    if( pCArray->ixNx[k]<=i ){
71412      k++;
71413      pEnd = pCArray->apEnd[k];
71414    }
71415  }
71416  *ppData = pData;
71417  return 0;
71418}
71419
71420/*
71421** The pCArray object contains pointers to b-tree cells and their sizes.
71422**
71423** This function adds the space associated with each cell in the array
71424** that is currently stored within the body of pPg to the pPg free-list.
71425** The cell-pointers and other fields of the page are not updated.
71426**
71427** This function returns the total number of cells added to the free-list.
71428*/
71429static int pageFreeArray(
71430  MemPage *pPg,                   /* Page to edit */
71431  int iFirst,                     /* First cell to delete */
71432  int nCell,                      /* Cells to delete */
71433  CellArray *pCArray              /* Array of cells */
71434){
71435  u8 * const aData = pPg->aData;
71436  u8 * const pEnd = &aData[pPg->pBt->usableSize];
71437  u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
71438  int nRet = 0;
71439  int i;
71440  int iEnd = iFirst + nCell;
71441  u8 *pFree = 0;
71442  int szFree = 0;
71443
71444  for(i=iFirst; i<iEnd; i++){
71445    u8 *pCell = pCArray->apCell[i];
71446    if( SQLITE_WITHIN(pCell, pStart, pEnd) ){
71447      int sz;
71448      /* No need to use cachedCellSize() here.  The sizes of all cells that
71449      ** are to be freed have already been computing while deciding which
71450      ** cells need freeing */
71451      sz = pCArray->szCell[i];  assert( sz>0 );
71452      if( pFree!=(pCell + sz) ){
71453        if( pFree ){
71454          assert( pFree>aData && (pFree - aData)<65536 );
71455          freeSpace(pPg, (u16)(pFree - aData), szFree);
71456        }
71457        pFree = pCell;
71458        szFree = sz;
71459        if( pFree+sz>pEnd ) return 0;
71460      }else{
71461        pFree = pCell;
71462        szFree += sz;
71463      }
71464      nRet++;
71465    }
71466  }
71467  if( pFree ){
71468    assert( pFree>aData && (pFree - aData)<65536 );
71469    freeSpace(pPg, (u16)(pFree - aData), szFree);
71470  }
71471  return nRet;
71472}
71473
71474/*
71475** pCArray contains pointers to and sizes of all cells in the page being
71476** balanced.  The current page, pPg, has pPg->nCell cells starting with
71477** pCArray->apCell[iOld].  After balancing, this page should hold nNew cells
71478** starting at apCell[iNew].
71479**
71480** This routine makes the necessary adjustments to pPg so that it contains
71481** the correct cells after being balanced.
71482**
71483** The pPg->nFree field is invalid when this function returns. It is the
71484** responsibility of the caller to set it correctly.
71485*/
71486static int editPage(
71487  MemPage *pPg,                   /* Edit this page */
71488  int iOld,                       /* Index of first cell currently on page */
71489  int iNew,                       /* Index of new first cell on page */
71490  int nNew,                       /* Final number of cells on page */
71491  CellArray *pCArray              /* Array of cells and sizes */
71492){
71493  u8 * const aData = pPg->aData;
71494  const int hdr = pPg->hdrOffset;
71495  u8 *pBegin = &pPg->aCellIdx[nNew * 2];
71496  int nCell = pPg->nCell;       /* Cells stored on pPg */
71497  u8 *pData;
71498  u8 *pCellptr;
71499  int i;
71500  int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
71501  int iNewEnd = iNew + nNew;
71502
71503#ifdef SQLITE_DEBUG
71504  u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
71505  memcpy(pTmp, aData, pPg->pBt->usableSize);
71506#endif
71507
71508  /* Remove cells from the start and end of the page */
71509  assert( nCell>=0 );
71510  if( iOld<iNew ){
71511    int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
71512    if( NEVER(nShift>nCell) ) return SQLITE_CORRUPT_BKPT;
71513    memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
71514    nCell -= nShift;
71515  }
71516  if( iNewEnd < iOldEnd ){
71517    int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
71518    assert( nCell>=nTail );
71519    nCell -= nTail;
71520  }
71521
71522  pData = &aData[get2byteNotZero(&aData[hdr+5])];
71523  if( pData<pBegin ) goto editpage_fail;
71524
71525  /* Add cells to the start of the page */
71526  if( iNew<iOld ){
71527    int nAdd = MIN(nNew,iOld-iNew);
71528    assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
71529    assert( nAdd>=0 );
71530    pCellptr = pPg->aCellIdx;
71531    memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
71532    if( pageInsertArray(
71533          pPg, pBegin, &pData, pCellptr,
71534          iNew, nAdd, pCArray
71535    ) ) goto editpage_fail;
71536    nCell += nAdd;
71537  }
71538
71539  /* Add any overflow cells */
71540  for(i=0; i<pPg->nOverflow; i++){
71541    int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
71542    if( iCell>=0 && iCell<nNew ){
71543      pCellptr = &pPg->aCellIdx[iCell * 2];
71544      if( nCell>iCell ){
71545        memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
71546      }
71547      nCell++;
71548      cachedCellSize(pCArray, iCell+iNew);
71549      if( pageInsertArray(
71550            pPg, pBegin, &pData, pCellptr,
71551            iCell+iNew, 1, pCArray
71552      ) ) goto editpage_fail;
71553    }
71554  }
71555
71556  /* Append cells to the end of the page */
71557  assert( nCell>=0 );
71558  pCellptr = &pPg->aCellIdx[nCell*2];
71559  if( pageInsertArray(
71560        pPg, pBegin, &pData, pCellptr,
71561        iNew+nCell, nNew-nCell, pCArray
71562  ) ) goto editpage_fail;
71563
71564  pPg->nCell = nNew;
71565  pPg->nOverflow = 0;
71566
71567  put2byte(&aData[hdr+3], pPg->nCell);
71568  put2byte(&aData[hdr+5], pData - aData);
71569
71570#ifdef SQLITE_DEBUG
71571  for(i=0; i<nNew && !CORRUPT_DB; i++){
71572    u8 *pCell = pCArray->apCell[i+iNew];
71573    int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
71574    if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
71575      pCell = &pTmp[pCell - aData];
71576    }
71577    assert( 0==memcmp(pCell, &aData[iOff],
71578            pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
71579  }
71580#endif
71581
71582  return SQLITE_OK;
71583 editpage_fail:
71584  /* Unable to edit this page. Rebuild it from scratch instead. */
71585  populateCellCache(pCArray, iNew, nNew);
71586  return rebuildPage(pCArray, iNew, nNew, pPg);
71587}
71588
71589
71590#ifndef SQLITE_OMIT_QUICKBALANCE
71591/*
71592** This version of balance() handles the common special case where
71593** a new entry is being inserted on the extreme right-end of the
71594** tree, in other words, when the new entry will become the largest
71595** entry in the tree.
71596**
71597** Instead of trying to balance the 3 right-most leaf pages, just add
71598** a new page to the right-hand side and put the one new entry in
71599** that page.  This leaves the right side of the tree somewhat
71600** unbalanced.  But odds are that we will be inserting new entries
71601** at the end soon afterwards so the nearly empty page will quickly
71602** fill up.  On average.
71603**
71604** pPage is the leaf page which is the right-most page in the tree.
71605** pParent is its parent.  pPage must have a single overflow entry
71606** which is also the right-most entry on the page.
71607**
71608** The pSpace buffer is used to store a temporary copy of the divider
71609** cell that will be inserted into pParent. Such a cell consists of a 4
71610** byte page number followed by a variable length integer. In other
71611** words, at most 13 bytes. Hence the pSpace buffer must be at
71612** least 13 bytes in size.
71613*/
71614static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
71615  BtShared *const pBt = pPage->pBt;    /* B-Tree Database */
71616  MemPage *pNew;                       /* Newly allocated page */
71617  int rc;                              /* Return Code */
71618  Pgno pgnoNew;                        /* Page number of pNew */
71619
71620  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71621  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
71622  assert( pPage->nOverflow==1 );
71623
71624  if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT;  /* dbfuzz001.test */
71625  assert( pPage->nFree>=0 );
71626  assert( pParent->nFree>=0 );
71627
71628  /* Allocate a new page. This page will become the right-sibling of
71629  ** pPage. Make the parent page writable, so that the new divider cell
71630  ** may be inserted. If both these operations are successful, proceed.
71631  */
71632  rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
71633
71634  if( rc==SQLITE_OK ){
71635
71636    u8 *pOut = &pSpace[4];
71637    u8 *pCell = pPage->apOvfl[0];
71638    u16 szCell = pPage->xCellSize(pPage, pCell);
71639    u8 *pStop;
71640    CellArray b;
71641
71642    assert( sqlite3PagerIswriteable(pNew->pDbPage) );
71643    assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
71644    zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
71645    b.nCell = 1;
71646    b.pRef = pPage;
71647    b.apCell = &pCell;
71648    b.szCell = &szCell;
71649    b.apEnd[0] = pPage->aDataEnd;
71650    b.ixNx[0] = 2;
71651    rc = rebuildPage(&b, 0, 1, pNew);
71652    if( NEVER(rc) ){
71653      releasePage(pNew);
71654      return rc;
71655    }
71656    pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
71657
71658    /* If this is an auto-vacuum database, update the pointer map
71659    ** with entries for the new page, and any pointer from the
71660    ** cell on the page to an overflow page. If either of these
71661    ** operations fails, the return code is set, but the contents
71662    ** of the parent page are still manipulated by thh code below.
71663    ** That is Ok, at this point the parent page is guaranteed to
71664    ** be marked as dirty. Returning an error code will cause a
71665    ** rollback, undoing any changes made to the parent page.
71666    */
71667    if( ISAUTOVACUUM ){
71668      ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
71669      if( szCell>pNew->minLocal ){
71670        ptrmapPutOvflPtr(pNew, pNew, pCell, &rc);
71671      }
71672    }
71673
71674    /* Create a divider cell to insert into pParent. The divider cell
71675    ** consists of a 4-byte page number (the page number of pPage) and
71676    ** a variable length key value (which must be the same value as the
71677    ** largest key on pPage).
71678    **
71679    ** To find the largest key value on pPage, first find the right-most
71680    ** cell on pPage. The first two fields of this cell are the
71681    ** record-length (a variable length integer at most 32-bits in size)
71682    ** and the key value (a variable length integer, may have any value).
71683    ** The first of the while(...) loops below skips over the record-length
71684    ** field. The second while(...) loop copies the key value from the
71685    ** cell on pPage into the pSpace buffer.
71686    */
71687    pCell = findCell(pPage, pPage->nCell-1);
71688    pStop = &pCell[9];
71689    while( (*(pCell++)&0x80) && pCell<pStop );
71690    pStop = &pCell[9];
71691    while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
71692
71693    /* Insert the new divider cell into pParent. */
71694    if( rc==SQLITE_OK ){
71695      insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
71696                   0, pPage->pgno, &rc);
71697    }
71698
71699    /* Set the right-child pointer of pParent to point to the new page. */
71700    put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
71701
71702    /* Release the reference to the new page. */
71703    releasePage(pNew);
71704  }
71705
71706  return rc;
71707}
71708#endif /* SQLITE_OMIT_QUICKBALANCE */
71709
71710#if 0
71711/*
71712** This function does not contribute anything to the operation of SQLite.
71713** it is sometimes activated temporarily while debugging code responsible
71714** for setting pointer-map entries.
71715*/
71716static int ptrmapCheckPages(MemPage **apPage, int nPage){
71717  int i, j;
71718  for(i=0; i<nPage; i++){
71719    Pgno n;
71720    u8 e;
71721    MemPage *pPage = apPage[i];
71722    BtShared *pBt = pPage->pBt;
71723    assert( pPage->isInit );
71724
71725    for(j=0; j<pPage->nCell; j++){
71726      CellInfo info;
71727      u8 *z;
71728
71729      z = findCell(pPage, j);
71730      pPage->xParseCell(pPage, z, &info);
71731      if( info.nLocal<info.nPayload ){
71732        Pgno ovfl = get4byte(&z[info.nSize-4]);
71733        ptrmapGet(pBt, ovfl, &e, &n);
71734        assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
71735      }
71736      if( !pPage->leaf ){
71737        Pgno child = get4byte(z);
71738        ptrmapGet(pBt, child, &e, &n);
71739        assert( n==pPage->pgno && e==PTRMAP_BTREE );
71740      }
71741    }
71742    if( !pPage->leaf ){
71743      Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
71744      ptrmapGet(pBt, child, &e, &n);
71745      assert( n==pPage->pgno && e==PTRMAP_BTREE );
71746    }
71747  }
71748  return 1;
71749}
71750#endif
71751
71752/*
71753** This function is used to copy the contents of the b-tree node stored
71754** on page pFrom to page pTo. If page pFrom was not a leaf page, then
71755** the pointer-map entries for each child page are updated so that the
71756** parent page stored in the pointer map is page pTo. If pFrom contained
71757** any cells with overflow page pointers, then the corresponding pointer
71758** map entries are also updated so that the parent page is page pTo.
71759**
71760** If pFrom is currently carrying any overflow cells (entries in the
71761** MemPage.apOvfl[] array), they are not copied to pTo.
71762**
71763** Before returning, page pTo is reinitialized using btreeInitPage().
71764**
71765** The performance of this function is not critical. It is only used by
71766** the balance_shallower() and balance_deeper() procedures, neither of
71767** which are called often under normal circumstances.
71768*/
71769static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
71770  if( (*pRC)==SQLITE_OK ){
71771    BtShared * const pBt = pFrom->pBt;
71772    u8 * const aFrom = pFrom->aData;
71773    u8 * const aTo = pTo->aData;
71774    int const iFromHdr = pFrom->hdrOffset;
71775    int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
71776    int rc;
71777    int iData;
71778
71779
71780    assert( pFrom->isInit );
71781    assert( pFrom->nFree>=iToHdr );
71782    assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
71783
71784    /* Copy the b-tree node content from page pFrom to page pTo. */
71785    iData = get2byte(&aFrom[iFromHdr+5]);
71786    memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
71787    memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
71788
71789    /* Reinitialize page pTo so that the contents of the MemPage structure
71790    ** match the new data. The initialization of pTo can actually fail under
71791    ** fairly obscure circumstances, even though it is a copy of initialized
71792    ** page pFrom.
71793    */
71794    pTo->isInit = 0;
71795    rc = btreeInitPage(pTo);
71796    if( rc==SQLITE_OK ) rc = btreeComputeFreeSpace(pTo);
71797    if( rc!=SQLITE_OK ){
71798      *pRC = rc;
71799      return;
71800    }
71801
71802    /* If this is an auto-vacuum database, update the pointer-map entries
71803    ** for any b-tree or overflow pages that pTo now contains the pointers to.
71804    */
71805    if( ISAUTOVACUUM ){
71806      *pRC = setChildPtrmaps(pTo);
71807    }
71808  }
71809}
71810
71811/*
71812** This routine redistributes cells on the iParentIdx'th child of pParent
71813** (hereafter "the page") and up to 2 siblings so that all pages have about the
71814** same amount of free space. Usually a single sibling on either side of the
71815** page are used in the balancing, though both siblings might come from one
71816** side if the page is the first or last child of its parent. If the page
71817** has fewer than 2 siblings (something which can only happen if the page
71818** is a root page or a child of a root page) then all available siblings
71819** participate in the balancing.
71820**
71821** The number of siblings of the page might be increased or decreased by
71822** one or two in an effort to keep pages nearly full but not over full.
71823**
71824** Note that when this routine is called, some of the cells on the page
71825** might not actually be stored in MemPage.aData[]. This can happen
71826** if the page is overfull. This routine ensures that all cells allocated
71827** to the page and its siblings fit into MemPage.aData[] before returning.
71828**
71829** In the course of balancing the page and its siblings, cells may be
71830** inserted into or removed from the parent page (pParent). Doing so
71831** may cause the parent page to become overfull or underfull. If this
71832** happens, it is the responsibility of the caller to invoke the correct
71833** balancing routine to fix this problem (see the balance() routine).
71834**
71835** If this routine fails for any reason, it might leave the database
71836** in a corrupted state. So if this routine fails, the database should
71837** be rolled back.
71838**
71839** The third argument to this function, aOvflSpace, is a pointer to a
71840** buffer big enough to hold one page. If while inserting cells into the parent
71841** page (pParent) the parent page becomes overfull, this buffer is
71842** used to store the parent's overflow cells. Because this function inserts
71843** a maximum of four divider cells into the parent page, and the maximum
71844** size of a cell stored within an internal node is always less than 1/4
71845** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
71846** enough for all overflow cells.
71847**
71848** If aOvflSpace is set to a null pointer, this function returns
71849** SQLITE_NOMEM.
71850*/
71851static int balance_nonroot(
71852  MemPage *pParent,               /* Parent page of siblings being balanced */
71853  int iParentIdx,                 /* Index of "the page" in pParent */
71854  u8 *aOvflSpace,                 /* page-size bytes of space for parent ovfl */
71855  int isRoot,                     /* True if pParent is a root-page */
71856  int bBulk                       /* True if this call is part of a bulk load */
71857){
71858  BtShared *pBt;               /* The whole database */
71859  int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
71860  int nNew = 0;                /* Number of pages in apNew[] */
71861  int nOld;                    /* Number of pages in apOld[] */
71862  int i, j, k;                 /* Loop counters */
71863  int nxDiv;                   /* Next divider slot in pParent->aCell[] */
71864  int rc = SQLITE_OK;          /* The return code */
71865  u16 leafCorrection;          /* 4 if pPage is a leaf.  0 if not */
71866  int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
71867  int usableSpace;             /* Bytes in pPage beyond the header */
71868  int pageFlags;               /* Value of pPage->aData[0] */
71869  int iSpace1 = 0;             /* First unused byte of aSpace1[] */
71870  int iOvflSpace = 0;          /* First unused byte of aOvflSpace[] */
71871  int szScratch;               /* Size of scratch memory requested */
71872  MemPage *apOld[NB];          /* pPage and up to two siblings */
71873  MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
71874  u8 *pRight;                  /* Location in parent of right-sibling pointer */
71875  u8 *apDiv[NB-1];             /* Divider cells in pParent */
71876  int cntNew[NB+2];            /* Index in b.paCell[] of cell after i-th page */
71877  int cntOld[NB+2];            /* Old index in b.apCell[] */
71878  int szNew[NB+2];             /* Combined size of cells placed on i-th page */
71879  u8 *aSpace1;                 /* Space for copies of dividers cells */
71880  Pgno pgno;                   /* Temp var to store a page number in */
71881  u8 abDone[NB+2];             /* True after i'th new page is populated */
71882  Pgno aPgno[NB+2];            /* Page numbers of new pages before shuffling */
71883  Pgno aPgOrder[NB+2];         /* Copy of aPgno[] used for sorting pages */
71884  u16 aPgFlags[NB+2];          /* flags field of new pages before shuffling */
71885  CellArray b;                  /* Parsed information on cells being balanced */
71886
71887  memset(abDone, 0, sizeof(abDone));
71888  b.nCell = 0;
71889  b.apCell = 0;
71890  pBt = pParent->pBt;
71891  assert( sqlite3_mutex_held(pBt->mutex) );
71892  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
71893
71894  /* At this point pParent may have at most one overflow cell. And if
71895  ** this overflow cell is present, it must be the cell with
71896  ** index iParentIdx. This scenario comes about when this function
71897  ** is called (indirectly) from sqlite3BtreeDelete().
71898  */
71899  assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
71900  assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
71901
71902  if( !aOvflSpace ){
71903    return SQLITE_NOMEM_BKPT;
71904  }
71905  assert( pParent->nFree>=0 );
71906
71907  /* Find the sibling pages to balance. Also locate the cells in pParent
71908  ** that divide the siblings. An attempt is made to find NN siblings on
71909  ** either side of pPage. More siblings are taken from one side, however,
71910  ** if there are fewer than NN siblings on the other side. If pParent
71911  ** has NB or fewer children then all children of pParent are taken.
71912  **
71913  ** This loop also drops the divider cells from the parent page. This
71914  ** way, the remainder of the function does not have to deal with any
71915  ** overflow cells in the parent page, since if any existed they will
71916  ** have already been removed.
71917  */
71918  i = pParent->nOverflow + pParent->nCell;
71919  if( i<2 ){
71920    nxDiv = 0;
71921  }else{
71922    assert( bBulk==0 || bBulk==1 );
71923    if( iParentIdx==0 ){
71924      nxDiv = 0;
71925    }else if( iParentIdx==i ){
71926      nxDiv = i-2+bBulk;
71927    }else{
71928      nxDiv = iParentIdx-1;
71929    }
71930    i = 2-bBulk;
71931  }
71932  nOld = i+1;
71933  if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
71934    pRight = &pParent->aData[pParent->hdrOffset+8];
71935  }else{
71936    pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
71937  }
71938  pgno = get4byte(pRight);
71939  while( 1 ){
71940    rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
71941    if( rc ){
71942      memset(apOld, 0, (i+1)*sizeof(MemPage*));
71943      goto balance_cleanup;
71944    }
71945    if( apOld[i]->nFree<0 ){
71946      rc = btreeComputeFreeSpace(apOld[i]);
71947      if( rc ){
71948        memset(apOld, 0, (i)*sizeof(MemPage*));
71949        goto balance_cleanup;
71950      }
71951    }
71952    if( (i--)==0 ) break;
71953
71954    if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
71955      apDiv[i] = pParent->apOvfl[0];
71956      pgno = get4byte(apDiv[i]);
71957      szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
71958      pParent->nOverflow = 0;
71959    }else{
71960      apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
71961      pgno = get4byte(apDiv[i]);
71962      szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
71963
71964      /* Drop the cell from the parent page. apDiv[i] still points to
71965      ** the cell within the parent, even though it has been dropped.
71966      ** This is safe because dropping a cell only overwrites the first
71967      ** four bytes of it, and this function does not need the first
71968      ** four bytes of the divider cell. So the pointer is safe to use
71969      ** later on.
71970      **
71971      ** But not if we are in secure-delete mode. In secure-delete mode,
71972      ** the dropCell() routine will overwrite the entire cell with zeroes.
71973      ** In this case, temporarily copy the cell into the aOvflSpace[]
71974      ** buffer. It will be copied out again as soon as the aSpace[] buffer
71975      ** is allocated.  */
71976      if( pBt->btsFlags & BTS_FAST_SECURE ){
71977        int iOff;
71978
71979        iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
71980        if( (iOff+szNew[i])>(int)pBt->usableSize ){
71981          rc = SQLITE_CORRUPT_BKPT;
71982          memset(apOld, 0, (i+1)*sizeof(MemPage*));
71983          goto balance_cleanup;
71984        }else{
71985          memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
71986          apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
71987        }
71988      }
71989      dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
71990    }
71991  }
71992
71993  /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
71994  ** alignment */
71995  nMaxCells = nOld*(MX_CELL(pBt) + ArraySize(pParent->apOvfl));
71996  nMaxCells = (nMaxCells + 3)&~3;
71997
71998  /*
71999  ** Allocate space for memory structures
72000  */
72001  szScratch =
72002       nMaxCells*sizeof(u8*)                       /* b.apCell */
72003     + nMaxCells*sizeof(u16)                       /* b.szCell */
72004     + pBt->pageSize;                              /* aSpace1 */
72005
72006  assert( szScratch<=7*(int)pBt->pageSize );
72007  b.apCell = sqlite3StackAllocRaw(0, szScratch );
72008  if( b.apCell==0 ){
72009    rc = SQLITE_NOMEM_BKPT;
72010    goto balance_cleanup;
72011  }
72012  b.szCell = (u16*)&b.apCell[nMaxCells];
72013  aSpace1 = (u8*)&b.szCell[nMaxCells];
72014  assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
72015
72016  /*
72017  ** Load pointers to all cells on sibling pages and the divider cells
72018  ** into the local b.apCell[] array.  Make copies of the divider cells
72019  ** into space obtained from aSpace1[]. The divider cells have already
72020  ** been removed from pParent.
72021  **
72022  ** If the siblings are on leaf pages, then the child pointers of the
72023  ** divider cells are stripped from the cells before they are copied
72024  ** into aSpace1[].  In this way, all cells in b.apCell[] are without
72025  ** child pointers.  If siblings are not leaves, then all cell in
72026  ** b.apCell[] include child pointers.  Either way, all cells in b.apCell[]
72027  ** are alike.
72028  **
72029  ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
72030  **       leafData:  1 if pPage holds key+data and pParent holds only keys.
72031  */
72032  b.pRef = apOld[0];
72033  leafCorrection = b.pRef->leaf*4;
72034  leafData = b.pRef->intKeyLeaf;
72035  for(i=0; i<nOld; i++){
72036    MemPage *pOld = apOld[i];
72037    int limit = pOld->nCell;
72038    u8 *aData = pOld->aData;
72039    u16 maskPage = pOld->maskPage;
72040    u8 *piCell = aData + pOld->cellOffset;
72041    u8 *piEnd;
72042    VVA_ONLY( int nCellAtStart = b.nCell; )
72043
72044    /* Verify that all sibling pages are of the same "type" (table-leaf,
72045    ** table-interior, index-leaf, or index-interior).
72046    */
72047    if( pOld->aData[0]!=apOld[0]->aData[0] ){
72048      rc = SQLITE_CORRUPT_BKPT;
72049      goto balance_cleanup;
72050    }
72051
72052    /* Load b.apCell[] with pointers to all cells in pOld.  If pOld
72053    ** contains overflow cells, include them in the b.apCell[] array
72054    ** in the correct spot.
72055    **
72056    ** Note that when there are multiple overflow cells, it is always the
72057    ** case that they are sequential and adjacent.  This invariant arises
72058    ** because multiple overflows can only occurs when inserting divider
72059    ** cells into a parent on a prior balance, and divider cells are always
72060    ** adjacent and are inserted in order.  There is an assert() tagged
72061    ** with "NOTE 1" in the overflow cell insertion loop to prove this
72062    ** invariant.
72063    **
72064    ** This must be done in advance.  Once the balance starts, the cell
72065    ** offset section of the btree page will be overwritten and we will no
72066    ** long be able to find the cells if a pointer to each cell is not saved
72067    ** first.
72068    */
72069    memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
72070    if( pOld->nOverflow>0 ){
72071      if( NEVER(limit<pOld->aiOvfl[0]) ){
72072        rc = SQLITE_CORRUPT_BKPT;
72073        goto balance_cleanup;
72074      }
72075      limit = pOld->aiOvfl[0];
72076      for(j=0; j<limit; j++){
72077        b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
72078        piCell += 2;
72079        b.nCell++;
72080      }
72081      for(k=0; k<pOld->nOverflow; k++){
72082        assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
72083        b.apCell[b.nCell] = pOld->apOvfl[k];
72084        b.nCell++;
72085      }
72086    }
72087    piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
72088    while( piCell<piEnd ){
72089      assert( b.nCell<nMaxCells );
72090      b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
72091      piCell += 2;
72092      b.nCell++;
72093    }
72094    assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
72095
72096    cntOld[i] = b.nCell;
72097    if( i<nOld-1 && !leafData){
72098      u16 sz = (u16)szNew[i];
72099      u8 *pTemp;
72100      assert( b.nCell<nMaxCells );
72101      b.szCell[b.nCell] = sz;
72102      pTemp = &aSpace1[iSpace1];
72103      iSpace1 += sz;
72104      assert( sz<=pBt->maxLocal+23 );
72105      assert( iSpace1 <= (int)pBt->pageSize );
72106      memcpy(pTemp, apDiv[i], sz);
72107      b.apCell[b.nCell] = pTemp+leafCorrection;
72108      assert( leafCorrection==0 || leafCorrection==4 );
72109      b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
72110      if( !pOld->leaf ){
72111        assert( leafCorrection==0 );
72112        assert( pOld->hdrOffset==0 );
72113        /* The right pointer of the child page pOld becomes the left
72114        ** pointer of the divider cell */
72115        memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
72116      }else{
72117        assert( leafCorrection==4 );
72118        while( b.szCell[b.nCell]<4 ){
72119          /* Do not allow any cells smaller than 4 bytes. If a smaller cell
72120          ** does exist, pad it with 0x00 bytes. */
72121          assert( b.szCell[b.nCell]==3 || CORRUPT_DB );
72122          assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
72123          aSpace1[iSpace1++] = 0x00;
72124          b.szCell[b.nCell]++;
72125        }
72126      }
72127      b.nCell++;
72128    }
72129  }
72130
72131  /*
72132  ** Figure out the number of pages needed to hold all b.nCell cells.
72133  ** Store this number in "k".  Also compute szNew[] which is the total
72134  ** size of all cells on the i-th page and cntNew[] which is the index
72135  ** in b.apCell[] of the cell that divides page i from page i+1.
72136  ** cntNew[k] should equal b.nCell.
72137  **
72138  ** Values computed by this block:
72139  **
72140  **           k: The total number of sibling pages
72141  **    szNew[i]: Spaced used on the i-th sibling page.
72142  **   cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
72143  **              the right of the i-th sibling page.
72144  ** usableSpace: Number of bytes of space available on each sibling.
72145  **
72146  */
72147  usableSpace = pBt->usableSize - 12 + leafCorrection;
72148  for(i=k=0; i<nOld; i++, k++){
72149    MemPage *p = apOld[i];
72150    b.apEnd[k] = p->aDataEnd;
72151    b.ixNx[k] = cntOld[i];
72152    if( k && b.ixNx[k]==b.ixNx[k-1] ){
72153      k--;  /* Omit b.ixNx[] entry for child pages with no cells */
72154    }
72155    if( !leafData ){
72156      k++;
72157      b.apEnd[k] = pParent->aDataEnd;
72158      b.ixNx[k] = cntOld[i]+1;
72159    }
72160    assert( p->nFree>=0 );
72161    szNew[i] = usableSpace - p->nFree;
72162    for(j=0; j<p->nOverflow; j++){
72163      szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
72164    }
72165    cntNew[i] = cntOld[i];
72166  }
72167  k = nOld;
72168  for(i=0; i<k; i++){
72169    int sz;
72170    while( szNew[i]>usableSpace ){
72171      if( i+1>=k ){
72172        k = i+2;
72173        if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
72174        szNew[k-1] = 0;
72175        cntNew[k-1] = b.nCell;
72176      }
72177      sz = 2 + cachedCellSize(&b, cntNew[i]-1);
72178      szNew[i] -= sz;
72179      if( !leafData ){
72180        if( cntNew[i]<b.nCell ){
72181          sz = 2 + cachedCellSize(&b, cntNew[i]);
72182        }else{
72183          sz = 0;
72184        }
72185      }
72186      szNew[i+1] += sz;
72187      cntNew[i]--;
72188    }
72189    while( cntNew[i]<b.nCell ){
72190      sz = 2 + cachedCellSize(&b, cntNew[i]);
72191      if( szNew[i]+sz>usableSpace ) break;
72192      szNew[i] += sz;
72193      cntNew[i]++;
72194      if( !leafData ){
72195        if( cntNew[i]<b.nCell ){
72196          sz = 2 + cachedCellSize(&b, cntNew[i]);
72197        }else{
72198          sz = 0;
72199        }
72200      }
72201      szNew[i+1] -= sz;
72202    }
72203    if( cntNew[i]>=b.nCell ){
72204      k = i+1;
72205    }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
72206      rc = SQLITE_CORRUPT_BKPT;
72207      goto balance_cleanup;
72208    }
72209  }
72210
72211  /*
72212  ** The packing computed by the previous block is biased toward the siblings
72213  ** on the left side (siblings with smaller keys). The left siblings are
72214  ** always nearly full, while the right-most sibling might be nearly empty.
72215  ** The next block of code attempts to adjust the packing of siblings to
72216  ** get a better balance.
72217  **
72218  ** This adjustment is more than an optimization.  The packing above might
72219  ** be so out of balance as to be illegal.  For example, the right-most
72220  ** sibling might be completely empty.  This adjustment is not optional.
72221  */
72222  for(i=k-1; i>0; i--){
72223    int szRight = szNew[i];  /* Size of sibling on the right */
72224    int szLeft = szNew[i-1]; /* Size of sibling on the left */
72225    int r;              /* Index of right-most cell in left sibling */
72226    int d;              /* Index of first cell to the left of right sibling */
72227
72228    r = cntNew[i-1] - 1;
72229    d = r + 1 - leafData;
72230    (void)cachedCellSize(&b, d);
72231    do{
72232      assert( d<nMaxCells );
72233      assert( r<nMaxCells );
72234      (void)cachedCellSize(&b, r);
72235      if( szRight!=0
72236       && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
72237        break;
72238      }
72239      szRight += b.szCell[d] + 2;
72240      szLeft -= b.szCell[r] + 2;
72241      cntNew[i-1] = r;
72242      r--;
72243      d--;
72244    }while( r>=0 );
72245    szNew[i] = szRight;
72246    szNew[i-1] = szLeft;
72247    if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
72248      rc = SQLITE_CORRUPT_BKPT;
72249      goto balance_cleanup;
72250    }
72251  }
72252
72253  /* Sanity check:  For a non-corrupt database file one of the follwing
72254  ** must be true:
72255  **    (1) We found one or more cells (cntNew[0])>0), or
72256  **    (2) pPage is a virtual root page.  A virtual root page is when
72257  **        the real root page is page 1 and we are the only child of
72258  **        that page.
72259  */
72260  assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
72261  TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n",
72262    apOld[0]->pgno, apOld[0]->nCell,
72263    nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
72264    nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
72265  ));
72266
72267  /*
72268  ** Allocate k new pages.  Reuse old pages where possible.
72269  */
72270  pageFlags = apOld[0]->aData[0];
72271  for(i=0; i<k; i++){
72272    MemPage *pNew;
72273    if( i<nOld ){
72274      pNew = apNew[i] = apOld[i];
72275      apOld[i] = 0;
72276      rc = sqlite3PagerWrite(pNew->pDbPage);
72277      nNew++;
72278      if( rc ) goto balance_cleanup;
72279    }else{
72280      assert( i>0 );
72281      rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
72282      if( rc ) goto balance_cleanup;
72283      zeroPage(pNew, pageFlags);
72284      apNew[i] = pNew;
72285      nNew++;
72286      cntOld[i] = b.nCell;
72287
72288      /* Set the pointer-map entry for the new sibling page. */
72289      if( ISAUTOVACUUM ){
72290        ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
72291        if( rc!=SQLITE_OK ){
72292          goto balance_cleanup;
72293        }
72294      }
72295    }
72296  }
72297
72298  /*
72299  ** Reassign page numbers so that the new pages are in ascending order.
72300  ** This helps to keep entries in the disk file in order so that a scan
72301  ** of the table is closer to a linear scan through the file. That in turn
72302  ** helps the operating system to deliver pages from the disk more rapidly.
72303  **
72304  ** An O(n^2) insertion sort algorithm is used, but since n is never more
72305  ** than (NB+2) (a small constant), that should not be a problem.
72306  **
72307  ** When NB==3, this one optimization makes the database about 25% faster
72308  ** for large insertions and deletions.
72309  */
72310  for(i=0; i<nNew; i++){
72311    aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
72312    aPgFlags[i] = apNew[i]->pDbPage->flags;
72313    for(j=0; j<i; j++){
72314      if( aPgno[j]==aPgno[i] ){
72315        /* This branch is taken if the set of sibling pages somehow contains
72316        ** duplicate entries. This can happen if the database is corrupt.
72317        ** It would be simpler to detect this as part of the loop below, but
72318        ** we do the detection here in order to avoid populating the pager
72319        ** cache with two separate objects associated with the same
72320        ** page number.  */
72321        assert( CORRUPT_DB );
72322        rc = SQLITE_CORRUPT_BKPT;
72323        goto balance_cleanup;
72324      }
72325    }
72326  }
72327  for(i=0; i<nNew; i++){
72328    int iBest = 0;                /* aPgno[] index of page number to use */
72329    for(j=1; j<nNew; j++){
72330      if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
72331    }
72332    pgno = aPgOrder[iBest];
72333    aPgOrder[iBest] = 0xffffffff;
72334    if( iBest!=i ){
72335      if( iBest>i ){
72336        sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
72337      }
72338      sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
72339      apNew[i]->pgno = pgno;
72340    }
72341  }
72342
72343  TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) "
72344         "%d(%d nc=%d) %d(%d nc=%d)\n",
72345    apNew[0]->pgno, szNew[0], cntNew[0],
72346    nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
72347    nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
72348    nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
72349    nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
72350    nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
72351    nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
72352    nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
72353    nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
72354  ));
72355
72356  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
72357  assert( nNew>=1 && nNew<=ArraySize(apNew) );
72358  assert( apNew[nNew-1]!=0 );
72359  put4byte(pRight, apNew[nNew-1]->pgno);
72360
72361  /* If the sibling pages are not leaves, ensure that the right-child pointer
72362  ** of the right-most new sibling page is set to the value that was
72363  ** originally in the same field of the right-most old sibling page. */
72364  if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){
72365    MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
72366    memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
72367  }
72368
72369  /* Make any required updates to pointer map entries associated with
72370  ** cells stored on sibling pages following the balance operation. Pointer
72371  ** map entries associated with divider cells are set by the insertCell()
72372  ** routine. The associated pointer map entries are:
72373  **
72374  **   a) if the cell contains a reference to an overflow chain, the
72375  **      entry associated with the first page in the overflow chain, and
72376  **
72377  **   b) if the sibling pages are not leaves, the child page associated
72378  **      with the cell.
72379  **
72380  ** If the sibling pages are not leaves, then the pointer map entry
72381  ** associated with the right-child of each sibling may also need to be
72382  ** updated. This happens below, after the sibling pages have been
72383  ** populated, not here.
72384  */
72385  if( ISAUTOVACUUM ){
72386    MemPage *pOld;
72387    MemPage *pNew = pOld = apNew[0];
72388    int cntOldNext = pNew->nCell + pNew->nOverflow;
72389    int iNew = 0;
72390    int iOld = 0;
72391
72392    for(i=0; i<b.nCell; i++){
72393      u8 *pCell = b.apCell[i];
72394      while( i==cntOldNext ){
72395        iOld++;
72396        assert( iOld<nNew || iOld<nOld );
72397        assert( iOld>=0 && iOld<NB );
72398        pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
72399        cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
72400      }
72401      if( i==cntNew[iNew] ){
72402        pNew = apNew[++iNew];
72403        if( !leafData ) continue;
72404      }
72405
72406      /* Cell pCell is destined for new sibling page pNew. Originally, it
72407      ** was either part of sibling page iOld (possibly an overflow cell),
72408      ** or else the divider cell to the left of sibling page iOld. So,
72409      ** if sibling page iOld had the same page number as pNew, and if
72410      ** pCell really was a part of sibling page iOld (not a divider or
72411      ** overflow cell), we can skip updating the pointer map entries.  */
72412      if( iOld>=nNew
72413       || pNew->pgno!=aPgno[iOld]
72414       || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
72415      ){
72416        if( !leafCorrection ){
72417          ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
72418        }
72419        if( cachedCellSize(&b,i)>pNew->minLocal ){
72420          ptrmapPutOvflPtr(pNew, pOld, pCell, &rc);
72421        }
72422        if( rc ) goto balance_cleanup;
72423      }
72424    }
72425  }
72426
72427  /* Insert new divider cells into pParent. */
72428  for(i=0; i<nNew-1; i++){
72429    u8 *pCell;
72430    u8 *pTemp;
72431    int sz;
72432    MemPage *pNew = apNew[i];
72433    j = cntNew[i];
72434
72435    assert( j<nMaxCells );
72436    assert( b.apCell[j]!=0 );
72437    pCell = b.apCell[j];
72438    sz = b.szCell[j] + leafCorrection;
72439    pTemp = &aOvflSpace[iOvflSpace];
72440    if( !pNew->leaf ){
72441      memcpy(&pNew->aData[8], pCell, 4);
72442    }else if( leafData ){
72443      /* If the tree is a leaf-data tree, and the siblings are leaves,
72444      ** then there is no divider cell in b.apCell[]. Instead, the divider
72445      ** cell consists of the integer key for the right-most cell of
72446      ** the sibling-page assembled above only.
72447      */
72448      CellInfo info;
72449      j--;
72450      pNew->xParseCell(pNew, b.apCell[j], &info);
72451      pCell = pTemp;
72452      sz = 4 + putVarint(&pCell[4], info.nKey);
72453      pTemp = 0;
72454    }else{
72455      pCell -= 4;
72456      /* Obscure case for non-leaf-data trees: If the cell at pCell was
72457      ** previously stored on a leaf node, and its reported size was 4
72458      ** bytes, then it may actually be smaller than this
72459      ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
72460      ** any cell). But it is important to pass the correct size to
72461      ** insertCell(), so reparse the cell now.
72462      **
72463      ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
72464      ** and WITHOUT ROWID tables with exactly one column which is the
72465      ** primary key.
72466      */
72467      if( b.szCell[j]==4 ){
72468        assert(leafCorrection==4);
72469        sz = pParent->xCellSize(pParent, pCell);
72470      }
72471    }
72472    iOvflSpace += sz;
72473    assert( sz<=pBt->maxLocal+23 );
72474    assert( iOvflSpace <= (int)pBt->pageSize );
72475    insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
72476    if( rc!=SQLITE_OK ) goto balance_cleanup;
72477    assert( sqlite3PagerIswriteable(pParent->pDbPage) );
72478  }
72479
72480  /* Now update the actual sibling pages. The order in which they are updated
72481  ** is important, as this code needs to avoid disrupting any page from which
72482  ** cells may still to be read. In practice, this means:
72483  **
72484  **  (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
72485  **      then it is not safe to update page apNew[iPg] until after
72486  **      the left-hand sibling apNew[iPg-1] has been updated.
72487  **
72488  **  (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
72489  **      then it is not safe to update page apNew[iPg] until after
72490  **      the right-hand sibling apNew[iPg+1] has been updated.
72491  **
72492  ** If neither of the above apply, the page is safe to update.
72493  **
72494  ** The iPg value in the following loop starts at nNew-1 goes down
72495  ** to 0, then back up to nNew-1 again, thus making two passes over
72496  ** the pages.  On the initial downward pass, only condition (1) above
72497  ** needs to be tested because (2) will always be true from the previous
72498  ** step.  On the upward pass, both conditions are always true, so the
72499  ** upwards pass simply processes pages that were missed on the downward
72500  ** pass.
72501  */
72502  for(i=1-nNew; i<nNew; i++){
72503    int iPg = i<0 ? -i : i;
72504    assert( iPg>=0 && iPg<nNew );
72505    if( abDone[iPg] ) continue;         /* Skip pages already processed */
72506    if( i>=0                            /* On the upwards pass, or... */
72507     || cntOld[iPg-1]>=cntNew[iPg-1]    /* Condition (1) is true */
72508    ){
72509      int iNew;
72510      int iOld;
72511      int nNewCell;
72512
72513      /* Verify condition (1):  If cells are moving left, update iPg
72514      ** only after iPg-1 has already been updated. */
72515      assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
72516
72517      /* Verify condition (2):  If cells are moving right, update iPg
72518      ** only after iPg+1 has already been updated. */
72519      assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] );
72520
72521      if( iPg==0 ){
72522        iNew = iOld = 0;
72523        nNewCell = cntNew[0];
72524      }else{
72525        iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
72526        iNew = cntNew[iPg-1] + !leafData;
72527        nNewCell = cntNew[iPg] - iNew;
72528      }
72529
72530      rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
72531      if( rc ) goto balance_cleanup;
72532      abDone[iPg]++;
72533      apNew[iPg]->nFree = usableSpace-szNew[iPg];
72534      assert( apNew[iPg]->nOverflow==0 );
72535      assert( apNew[iPg]->nCell==nNewCell );
72536    }
72537  }
72538
72539  /* All pages have been processed exactly once */
72540  assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 );
72541
72542  assert( nOld>0 );
72543  assert( nNew>0 );
72544
72545  if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
72546    /* The root page of the b-tree now contains no cells. The only sibling
72547    ** page is the right-child of the parent. Copy the contents of the
72548    ** child page into the parent, decreasing the overall height of the
72549    ** b-tree structure by one. This is described as the "balance-shallower"
72550    ** sub-algorithm in some documentation.
72551    **
72552    ** If this is an auto-vacuum database, the call to copyNodeContent()
72553    ** sets all pointer-map entries corresponding to database image pages
72554    ** for which the pointer is stored within the content being copied.
72555    **
72556    ** It is critical that the child page be defragmented before being
72557    ** copied into the parent, because if the parent is page 1 then it will
72558    ** by smaller than the child due to the database header, and so all the
72559    ** free space needs to be up front.
72560    */
72561    assert( nNew==1 || CORRUPT_DB );
72562    rc = defragmentPage(apNew[0], -1);
72563    testcase( rc!=SQLITE_OK );
72564    assert( apNew[0]->nFree ==
72565        (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
72566          - apNew[0]->nCell*2)
72567      || rc!=SQLITE_OK
72568    );
72569    copyNodeContent(apNew[0], pParent, &rc);
72570    freePage(apNew[0], &rc);
72571  }else if( ISAUTOVACUUM && !leafCorrection ){
72572    /* Fix the pointer map entries associated with the right-child of each
72573    ** sibling page. All other pointer map entries have already been taken
72574    ** care of.  */
72575    for(i=0; i<nNew; i++){
72576      u32 key = get4byte(&apNew[i]->aData[8]);
72577      ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
72578    }
72579  }
72580
72581  assert( pParent->isInit );
72582  TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
72583          nOld, nNew, b.nCell));
72584
72585  /* Free any old pages that were not reused as new pages.
72586  */
72587  for(i=nNew; i<nOld; i++){
72588    freePage(apOld[i], &rc);
72589  }
72590
72591#if 0
72592  if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
72593    /* The ptrmapCheckPages() contains assert() statements that verify that
72594    ** all pointer map pages are set correctly. This is helpful while
72595    ** debugging. This is usually disabled because a corrupt database may
72596    ** cause an assert() statement to fail.  */
72597    ptrmapCheckPages(apNew, nNew);
72598    ptrmapCheckPages(&pParent, 1);
72599  }
72600#endif
72601
72602  /*
72603  ** Cleanup before returning.
72604  */
72605balance_cleanup:
72606  sqlite3StackFree(0, b.apCell);
72607  for(i=0; i<nOld; i++){
72608    releasePage(apOld[i]);
72609  }
72610  for(i=0; i<nNew; i++){
72611    releasePage(apNew[i]);
72612  }
72613
72614  return rc;
72615}
72616
72617
72618/*
72619** This function is called when the root page of a b-tree structure is
72620** overfull (has one or more overflow pages).
72621**
72622** A new child page is allocated and the contents of the current root
72623** page, including overflow cells, are copied into the child. The root
72624** page is then overwritten to make it an empty page with the right-child
72625** pointer pointing to the new page.
72626**
72627** Before returning, all pointer-map entries corresponding to pages
72628** that the new child-page now contains pointers to are updated. The
72629** entry corresponding to the new right-child pointer of the root
72630** page is also updated.
72631**
72632** If successful, *ppChild is set to contain a reference to the child
72633** page and SQLITE_OK is returned. In this case the caller is required
72634** to call releasePage() on *ppChild exactly once. If an error occurs,
72635** an error code is returned and *ppChild is set to 0.
72636*/
72637static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
72638  int rc;                        /* Return value from subprocedures */
72639  MemPage *pChild = 0;           /* Pointer to a new child page */
72640  Pgno pgnoChild = 0;            /* Page number of the new child page */
72641  BtShared *pBt = pRoot->pBt;    /* The BTree */
72642
72643  assert( pRoot->nOverflow>0 );
72644  assert( sqlite3_mutex_held(pBt->mutex) );
72645
72646  /* Make pRoot, the root page of the b-tree, writable. Allocate a new
72647  ** page that will become the new right-child of pPage. Copy the contents
72648  ** of the node stored on pRoot into the new child page.
72649  */
72650  rc = sqlite3PagerWrite(pRoot->pDbPage);
72651  if( rc==SQLITE_OK ){
72652    rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
72653    copyNodeContent(pRoot, pChild, &rc);
72654    if( ISAUTOVACUUM ){
72655      ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
72656    }
72657  }
72658  if( rc ){
72659    *ppChild = 0;
72660    releasePage(pChild);
72661    return rc;
72662  }
72663  assert( sqlite3PagerIswriteable(pChild->pDbPage) );
72664  assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
72665  assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
72666
72667  TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
72668
72669  /* Copy the overflow cells from pRoot to pChild */
72670  memcpy(pChild->aiOvfl, pRoot->aiOvfl,
72671         pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
72672  memcpy(pChild->apOvfl, pRoot->apOvfl,
72673         pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
72674  pChild->nOverflow = pRoot->nOverflow;
72675
72676  /* Zero the contents of pRoot. Then install pChild as the right-child. */
72677  zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
72678  put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
72679
72680  *ppChild = pChild;
72681  return SQLITE_OK;
72682}
72683
72684/*
72685** Return SQLITE_CORRUPT if any cursor other than pCur is currently valid
72686** on the same B-tree as pCur.
72687**
72688** This can if a database is corrupt with two or more SQL tables
72689** pointing to the same b-tree.  If an insert occurs on one SQL table
72690** and causes a BEFORE TRIGGER to do a secondary insert on the other SQL
72691** table linked to the same b-tree.  If the secondary insert causes a
72692** rebalance, that can change content out from under the cursor on the
72693** first SQL table, violating invariants on the first insert.
72694*/
72695static int anotherValidCursor(BtCursor *pCur){
72696  BtCursor *pOther;
72697  for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
72698    if( pOther!=pCur
72699     && pOther->eState==CURSOR_VALID
72700     && pOther->pPage==pCur->pPage
72701    ){
72702      return SQLITE_CORRUPT_BKPT;
72703    }
72704  }
72705  return SQLITE_OK;
72706}
72707
72708/*
72709** The page that pCur currently points to has just been modified in
72710** some way. This function figures out if this modification means the
72711** tree needs to be balanced, and if so calls the appropriate balancing
72712** routine. Balancing routines are:
72713**
72714**   balance_quick()
72715**   balance_deeper()
72716**   balance_nonroot()
72717*/
72718static int balance(BtCursor *pCur){
72719  int rc = SQLITE_OK;
72720  const int nMin = pCur->pBt->usableSize * 2 / 3;
72721  u8 aBalanceQuickSpace[13];
72722  u8 *pFree = 0;
72723
72724  VVA_ONLY( int balance_quick_called = 0 );
72725  VVA_ONLY( int balance_deeper_called = 0 );
72726
72727  do {
72728    int iPage;
72729    MemPage *pPage = pCur->pPage;
72730
72731    if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
72732    if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
72733      break;
72734    }else if( (iPage = pCur->iPage)==0 ){
72735      if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
72736        /* The root page of the b-tree is overfull. In this case call the
72737        ** balance_deeper() function to create a new child for the root-page
72738        ** and copy the current contents of the root-page to it. The
72739        ** next iteration of the do-loop will balance the child page.
72740        */
72741        assert( balance_deeper_called==0 );
72742        VVA_ONLY( balance_deeper_called++ );
72743        rc = balance_deeper(pPage, &pCur->apPage[1]);
72744        if( rc==SQLITE_OK ){
72745          pCur->iPage = 1;
72746          pCur->ix = 0;
72747          pCur->aiIdx[0] = 0;
72748          pCur->apPage[0] = pPage;
72749          pCur->pPage = pCur->apPage[1];
72750          assert( pCur->pPage->nOverflow );
72751        }
72752      }else{
72753        break;
72754      }
72755    }else{
72756      MemPage * const pParent = pCur->apPage[iPage-1];
72757      int const iIdx = pCur->aiIdx[iPage-1];
72758
72759      rc = sqlite3PagerWrite(pParent->pDbPage);
72760      if( rc==SQLITE_OK && pParent->nFree<0 ){
72761        rc = btreeComputeFreeSpace(pParent);
72762      }
72763      if( rc==SQLITE_OK ){
72764#ifndef SQLITE_OMIT_QUICKBALANCE
72765        if( pPage->intKeyLeaf
72766         && pPage->nOverflow==1
72767         && pPage->aiOvfl[0]==pPage->nCell
72768         && pParent->pgno!=1
72769         && pParent->nCell==iIdx
72770        ){
72771          /* Call balance_quick() to create a new sibling of pPage on which
72772          ** to store the overflow cell. balance_quick() inserts a new cell
72773          ** into pParent, which may cause pParent overflow. If this
72774          ** happens, the next iteration of the do-loop will balance pParent
72775          ** use either balance_nonroot() or balance_deeper(). Until this
72776          ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
72777          ** buffer.
72778          **
72779          ** The purpose of the following assert() is to check that only a
72780          ** single call to balance_quick() is made for each call to this
72781          ** function. If this were not verified, a subtle bug involving reuse
72782          ** of the aBalanceQuickSpace[] might sneak in.
72783          */
72784          assert( balance_quick_called==0 );
72785          VVA_ONLY( balance_quick_called++ );
72786          rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
72787        }else
72788#endif
72789        {
72790          /* In this case, call balance_nonroot() to redistribute cells
72791          ** between pPage and up to 2 of its sibling pages. This involves
72792          ** modifying the contents of pParent, which may cause pParent to
72793          ** become overfull or underfull. The next iteration of the do-loop
72794          ** will balance the parent page to correct this.
72795          **
72796          ** If the parent page becomes overfull, the overflow cell or cells
72797          ** are stored in the pSpace buffer allocated immediately below.
72798          ** A subsequent iteration of the do-loop will deal with this by
72799          ** calling balance_nonroot() (balance_deeper() may be called first,
72800          ** but it doesn't deal with overflow cells - just moves them to a
72801          ** different page). Once this subsequent call to balance_nonroot()
72802          ** has completed, it is safe to release the pSpace buffer used by
72803          ** the previous call, as the overflow cell data will have been
72804          ** copied either into the body of a database page or into the new
72805          ** pSpace buffer passed to the latter call to balance_nonroot().
72806          */
72807          u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
72808          rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
72809                               pCur->hints&BTREE_BULKLOAD);
72810          if( pFree ){
72811            /* If pFree is not NULL, it points to the pSpace buffer used
72812            ** by a previous call to balance_nonroot(). Its contents are
72813            ** now stored either on real database pages or within the
72814            ** new pSpace buffer, so it may be safely freed here. */
72815            sqlite3PageFree(pFree);
72816          }
72817
72818          /* The pSpace buffer will be freed after the next call to
72819          ** balance_nonroot(), or just before this function returns, whichever
72820          ** comes first. */
72821          pFree = pSpace;
72822        }
72823      }
72824
72825      pPage->nOverflow = 0;
72826
72827      /* The next iteration of the do-loop balances the parent page. */
72828      releasePage(pPage);
72829      pCur->iPage--;
72830      assert( pCur->iPage>=0 );
72831      pCur->pPage = pCur->apPage[pCur->iPage];
72832    }
72833  }while( rc==SQLITE_OK );
72834
72835  if( pFree ){
72836    sqlite3PageFree(pFree);
72837  }
72838  return rc;
72839}
72840
72841/* Overwrite content from pX into pDest.  Only do the write if the
72842** content is different from what is already there.
72843*/
72844static int btreeOverwriteContent(
72845  MemPage *pPage,           /* MemPage on which writing will occur */
72846  u8 *pDest,                /* Pointer to the place to start writing */
72847  const BtreePayload *pX,   /* Source of data to write */
72848  int iOffset,              /* Offset of first byte to write */
72849  int iAmt                  /* Number of bytes to be written */
72850){
72851  int nData = pX->nData - iOffset;
72852  if( nData<=0 ){
72853    /* Overwritting with zeros */
72854    int i;
72855    for(i=0; i<iAmt && pDest[i]==0; i++){}
72856    if( i<iAmt ){
72857      int rc = sqlite3PagerWrite(pPage->pDbPage);
72858      if( rc ) return rc;
72859      memset(pDest + i, 0, iAmt - i);
72860    }
72861  }else{
72862    if( nData<iAmt ){
72863      /* Mixed read data and zeros at the end.  Make a recursive call
72864      ** to write the zeros then fall through to write the real data */
72865      int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
72866                                 iAmt-nData);
72867      if( rc ) return rc;
72868      iAmt = nData;
72869    }
72870    if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
72871      int rc = sqlite3PagerWrite(pPage->pDbPage);
72872      if( rc ) return rc;
72873      /* In a corrupt database, it is possible for the source and destination
72874      ** buffers to overlap.  This is harmless since the database is already
72875      ** corrupt but it does cause valgrind and ASAN warnings.  So use
72876      ** memmove(). */
72877      memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
72878    }
72879  }
72880  return SQLITE_OK;
72881}
72882
72883/*
72884** Overwrite the cell that cursor pCur is pointing to with fresh content
72885** contained in pX.
72886*/
72887static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
72888  int iOffset;                        /* Next byte of pX->pData to write */
72889  int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
72890  int rc;                             /* Return code */
72891  MemPage *pPage = pCur->pPage;       /* Page being written */
72892  BtShared *pBt;                      /* Btree */
72893  Pgno ovflPgno;                      /* Next overflow page to write */
72894  u32 ovflPageSize;                   /* Size to write on overflow page */
72895
72896  if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
72897   || pCur->info.pPayload < pPage->aData + pPage->cellOffset
72898  ){
72899    return SQLITE_CORRUPT_BKPT;
72900  }
72901  /* Overwrite the local portion first */
72902  rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
72903                             0, pCur->info.nLocal);
72904  if( rc ) return rc;
72905  if( pCur->info.nLocal==nTotal ) return SQLITE_OK;
72906
72907  /* Now overwrite the overflow pages */
72908  iOffset = pCur->info.nLocal;
72909  assert( nTotal>=0 );
72910  assert( iOffset>=0 );
72911  ovflPgno = get4byte(pCur->info.pPayload + iOffset);
72912  pBt = pPage->pBt;
72913  ovflPageSize = pBt->usableSize - 4;
72914  do{
72915    rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
72916    if( rc ) return rc;
72917    if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 ){
72918      rc = SQLITE_CORRUPT_BKPT;
72919    }else{
72920      if( iOffset+ovflPageSize<(u32)nTotal ){
72921        ovflPgno = get4byte(pPage->aData);
72922      }else{
72923        ovflPageSize = nTotal - iOffset;
72924      }
72925      rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
72926                                 iOffset, ovflPageSize);
72927    }
72928    sqlite3PagerUnref(pPage->pDbPage);
72929    if( rc ) return rc;
72930    iOffset += ovflPageSize;
72931  }while( iOffset<nTotal );
72932  return SQLITE_OK;
72933}
72934
72935
72936/*
72937** Insert a new record into the BTree.  The content of the new record
72938** is described by the pX object.  The pCur cursor is used only to
72939** define what table the record should be inserted into, and is left
72940** pointing at a random location.
72941**
72942** For a table btree (used for rowid tables), only the pX.nKey value of
72943** the key is used. The pX.pKey value must be NULL.  The pX.nKey is the
72944** rowid or INTEGER PRIMARY KEY of the row.  The pX.nData,pData,nZero fields
72945** hold the content of the row.
72946**
72947** For an index btree (used for indexes and WITHOUT ROWID tables), the
72948** key is an arbitrary byte sequence stored in pX.pKey,nKey.  The
72949** pX.pData,nData,nZero fields must be zero.
72950**
72951** If the seekResult parameter is non-zero, then a successful call to
72952** MovetoUnpacked() to seek cursor pCur to (pKey,nKey) has already
72953** been performed.  In other words, if seekResult!=0 then the cursor
72954** is currently pointing to a cell that will be adjacent to the cell
72955** to be inserted.  If seekResult<0 then pCur points to a cell that is
72956** smaller then (pKey,nKey).  If seekResult>0 then pCur points to a cell
72957** that is larger than (pKey,nKey).
72958**
72959** If seekResult==0, that means pCur is pointing at some unknown location.
72960** In that case, this routine must seek the cursor to the correct insertion
72961** point for (pKey,nKey) before doing the insertion.  For index btrees,
72962** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
72963** key values and pX->aMem can be used instead of pX->pKey to avoid having
72964** to decode the key.
72965*/
72966SQLITE_PRIVATE int sqlite3BtreeInsert(
72967  BtCursor *pCur,                /* Insert data into the table of this cursor */
72968  const BtreePayload *pX,        /* Content of the row to be inserted */
72969  int flags,                     /* True if this is likely an append */
72970  int seekResult                 /* Result of prior MovetoUnpacked() call */
72971){
72972  int rc;
72973  int loc = seekResult;          /* -1: before desired location  +1: after */
72974  int szNew = 0;
72975  int idx;
72976  MemPage *pPage;
72977  Btree *p = pCur->pBtree;
72978  BtShared *pBt = p->pBt;
72979  unsigned char *oldCell;
72980  unsigned char *newCell = 0;
72981
72982  assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags );
72983
72984  if( pCur->eState==CURSOR_FAULT ){
72985    assert( pCur->skipNext!=SQLITE_OK );
72986    return pCur->skipNext;
72987  }
72988
72989  assert( cursorOwnsBtShared(pCur) );
72990  assert( (pCur->curFlags & BTCF_WriteFlag)!=0
72991              && pBt->inTransaction==TRANS_WRITE
72992              && (pBt->btsFlags & BTS_READ_ONLY)==0 );
72993  assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
72994
72995  /* Assert that the caller has been consistent. If this cursor was opened
72996  ** expecting an index b-tree, then the caller should be inserting blob
72997  ** keys with no associated data. If the cursor was opened expecting an
72998  ** intkey table, the caller should be inserting integer keys with a
72999  ** blob of associated data.  */
73000  assert( (pX->pKey==0)==(pCur->pKeyInfo==0) );
73001
73002  /* Save the positions of any other cursors open on this table.
73003  **
73004  ** In some cases, the call to btreeMoveto() below is a no-op. For
73005  ** example, when inserting data into a table with auto-generated integer
73006  ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
73007  ** integer key to use. It then calls this function to actually insert the
73008  ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
73009  ** that the cursor is already where it needs to be and returns without
73010  ** doing any work. To avoid thwarting these optimizations, it is important
73011  ** not to clear the cursor here.
73012  */
73013  if( pCur->curFlags & BTCF_Multiple ){
73014    rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
73015    if( rc ) return rc;
73016  }
73017
73018  if( pCur->pKeyInfo==0 ){
73019    assert( pX->pKey==0 );
73020    /* If this is an insert into a table b-tree, invalidate any incrblob
73021    ** cursors open on the row being replaced */
73022    invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
73023
73024    /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
73025    ** to a row with the same key as the new entry being inserted.
73026    */
73027#ifdef SQLITE_DEBUG
73028    if( flags & BTREE_SAVEPOSITION ){
73029      assert( pCur->curFlags & BTCF_ValidNKey );
73030      assert( pX->nKey==pCur->info.nKey );
73031      assert( loc==0 );
73032    }
73033#endif
73034
73035    /* On the other hand, BTREE_SAVEPOSITION==0 does not imply
73036    ** that the cursor is not pointing to a row to be overwritten.
73037    ** So do a complete check.
73038    */
73039    if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
73040      /* The cursor is pointing to the entry that is to be
73041      ** overwritten */
73042      assert( pX->nData>=0 && pX->nZero>=0 );
73043      if( pCur->info.nSize!=0
73044       && pCur->info.nPayload==(u32)pX->nData+pX->nZero
73045      ){
73046        /* New entry is the same size as the old.  Do an overwrite */
73047        return btreeOverwriteCell(pCur, pX);
73048      }
73049      assert( loc==0 );
73050    }else if( loc==0 ){
73051      /* The cursor is *not* pointing to the cell to be overwritten, nor
73052      ** to an adjacent cell.  Move the cursor so that it is pointing either
73053      ** to the cell to be overwritten or an adjacent cell.
73054      */
73055      rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
73056      if( rc ) return rc;
73057    }
73058  }else{
73059    /* This is an index or a WITHOUT ROWID table */
73060
73061    /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
73062    ** to a row with the same key as the new entry being inserted.
73063    */
73064    assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 );
73065
73066    /* If the cursor is not already pointing either to the cell to be
73067    ** overwritten, or if a new cell is being inserted, if the cursor is
73068    ** not pointing to an immediately adjacent cell, then move the cursor
73069    ** so that it does.
73070    */
73071    if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
73072      if( pX->nMem ){
73073        UnpackedRecord r;
73074        r.pKeyInfo = pCur->pKeyInfo;
73075        r.aMem = pX->aMem;
73076        r.nField = pX->nMem;
73077        r.default_rc = 0;
73078        r.errCode = 0;
73079        r.r1 = 0;
73080        r.r2 = 0;
73081        r.eqSeen = 0;
73082        rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
73083      }else{
73084        rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
73085      }
73086      if( rc ) return rc;
73087    }
73088
73089    /* If the cursor is currently pointing to an entry to be overwritten
73090    ** and the new content is the same as as the old, then use the
73091    ** overwrite optimization.
73092    */
73093    if( loc==0 ){
73094      getCellInfo(pCur);
73095      if( pCur->info.nKey==pX->nKey ){
73096        BtreePayload x2;
73097        x2.pData = pX->pKey;
73098        x2.nData = pX->nKey;
73099        x2.nZero = 0;
73100        return btreeOverwriteCell(pCur, &x2);
73101      }
73102    }
73103
73104  }
73105  assert( pCur->eState==CURSOR_VALID
73106       || (pCur->eState==CURSOR_INVALID && loc)
73107       || CORRUPT_DB );
73108
73109  pPage = pCur->pPage;
73110  assert( pPage->intKey || pX->nKey>=0 );
73111  assert( pPage->leaf || !pPage->intKey );
73112  if( pPage->nFree<0 ){
73113    rc = btreeComputeFreeSpace(pPage);
73114    if( rc ) return rc;
73115  }
73116
73117  TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
73118          pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
73119          loc==0 ? "overwrite" : "new entry"));
73120  assert( pPage->isInit );
73121  newCell = pBt->pTmpSpace;
73122  assert( newCell!=0 );
73123  rc = fillInCell(pPage, newCell, pX, &szNew);
73124  if( rc ) goto end_insert;
73125  assert( szNew==pPage->xCellSize(pPage, newCell) );
73126  assert( szNew <= MX_CELL_SIZE(pBt) );
73127  idx = pCur->ix;
73128  if( loc==0 ){
73129    CellInfo info;
73130    assert( idx<pPage->nCell );
73131    rc = sqlite3PagerWrite(pPage->pDbPage);
73132    if( rc ){
73133      goto end_insert;
73134    }
73135    oldCell = findCell(pPage, idx);
73136    if( !pPage->leaf ){
73137      memcpy(newCell, oldCell, 4);
73138    }
73139    rc = clearCell(pPage, oldCell, &info);
73140    testcase( pCur->curFlags & BTCF_ValidOvfl );
73141    invalidateOverflowCache(pCur);
73142    if( info.nSize==szNew && info.nLocal==info.nPayload
73143     && (!ISAUTOVACUUM || szNew<pPage->minLocal)
73144    ){
73145      /* Overwrite the old cell with the new if they are the same size.
73146      ** We could also try to do this if the old cell is smaller, then add
73147      ** the leftover space to the free list.  But experiments show that
73148      ** doing that is no faster then skipping this optimization and just
73149      ** calling dropCell() and insertCell().
73150      **
73151      ** This optimization cannot be used on an autovacuum database if the
73152      ** new entry uses overflow pages, as the insertCell() call below is
73153      ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry.  */
73154      assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */
73155      if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
73156        return SQLITE_CORRUPT_BKPT;
73157      }
73158      if( oldCell+szNew > pPage->aDataEnd ){
73159        return SQLITE_CORRUPT_BKPT;
73160      }
73161      memcpy(oldCell, newCell, szNew);
73162      return SQLITE_OK;
73163    }
73164    dropCell(pPage, idx, info.nSize, &rc);
73165    if( rc ) goto end_insert;
73166  }else if( loc<0 && pPage->nCell>0 ){
73167    assert( pPage->leaf );
73168    idx = ++pCur->ix;
73169    pCur->curFlags &= ~BTCF_ValidNKey;
73170  }else{
73171    assert( pPage->leaf );
73172  }
73173  insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
73174  assert( pPage->nOverflow==0 || rc==SQLITE_OK );
73175  assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
73176
73177  /* If no error has occurred and pPage has an overflow cell, call balance()
73178  ** to redistribute the cells within the tree. Since balance() may move
73179  ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
73180  ** variables.
73181  **
73182  ** Previous versions of SQLite called moveToRoot() to move the cursor
73183  ** back to the root page as balance() used to invalidate the contents
73184  ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
73185  ** set the cursor state to "invalid". This makes common insert operations
73186  ** slightly faster.
73187  **
73188  ** There is a subtle but important optimization here too. When inserting
73189  ** multiple records into an intkey b-tree using a single cursor (as can
73190  ** happen while processing an "INSERT INTO ... SELECT" statement), it
73191  ** is advantageous to leave the cursor pointing to the last entry in
73192  ** the b-tree if possible. If the cursor is left pointing to the last
73193  ** entry in the table, and the next row inserted has an integer key
73194  ** larger than the largest existing key, it is possible to insert the
73195  ** row without seeking the cursor. This can be a big performance boost.
73196  */
73197  pCur->info.nSize = 0;
73198  if( pPage->nOverflow ){
73199    assert( rc==SQLITE_OK );
73200    pCur->curFlags &= ~(BTCF_ValidNKey);
73201    rc = balance(pCur);
73202
73203    /* Must make sure nOverflow is reset to zero even if the balance()
73204    ** fails. Internal data structure corruption will result otherwise.
73205    ** Also, set the cursor state to invalid. This stops saveCursorPosition()
73206    ** from trying to save the current position of the cursor.  */
73207    pCur->pPage->nOverflow = 0;
73208    pCur->eState = CURSOR_INVALID;
73209    if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
73210      btreeReleaseAllCursorPages(pCur);
73211      if( pCur->pKeyInfo ){
73212        assert( pCur->pKey==0 );
73213        pCur->pKey = sqlite3Malloc( pX->nKey );
73214        if( pCur->pKey==0 ){
73215          rc = SQLITE_NOMEM;
73216        }else{
73217          memcpy(pCur->pKey, pX->pKey, pX->nKey);
73218        }
73219      }
73220      pCur->eState = CURSOR_REQUIRESEEK;
73221      pCur->nKey = pX->nKey;
73222    }
73223  }
73224  assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
73225
73226end_insert:
73227  return rc;
73228}
73229
73230/*
73231** Delete the entry that the cursor is pointing to.
73232**
73233** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
73234** the cursor is left pointing at an arbitrary location after the delete.
73235** But if that bit is set, then the cursor is left in a state such that
73236** the next call to BtreeNext() or BtreePrev() moves it to the same row
73237** as it would have been on if the call to BtreeDelete() had been omitted.
73238**
73239** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
73240** associated with a single table entry and its indexes.  Only one of those
73241** deletes is considered the "primary" delete.  The primary delete occurs
73242** on a cursor that is not a BTREE_FORDELETE cursor.  All but one delete
73243** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
73244** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
73245** but which might be used by alternative storage engines.
73246*/
73247SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
73248  Btree *p = pCur->pBtree;
73249  BtShared *pBt = p->pBt;
73250  int rc;                              /* Return code */
73251  MemPage *pPage;                      /* Page to delete cell from */
73252  unsigned char *pCell;                /* Pointer to cell to delete */
73253  int iCellIdx;                        /* Index of cell to delete */
73254  int iCellDepth;                      /* Depth of node containing pCell */
73255  CellInfo info;                       /* Size of the cell being deleted */
73256  int bSkipnext = 0;                   /* Leaf cursor in SKIPNEXT state */
73257  u8 bPreserve = flags & BTREE_SAVEPOSITION;  /* Keep cursor valid */
73258
73259  assert( cursorOwnsBtShared(pCur) );
73260  assert( pBt->inTransaction==TRANS_WRITE );
73261  assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
73262  assert( pCur->curFlags & BTCF_WriteFlag );
73263  assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
73264  assert( !hasReadConflicts(p, pCur->pgnoRoot) );
73265  assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
73266  if( pCur->eState==CURSOR_REQUIRESEEK ){
73267    rc = btreeRestoreCursorPosition(pCur);
73268    if( rc ) return rc;
73269  }
73270  assert( pCur->eState==CURSOR_VALID );
73271
73272  iCellDepth = pCur->iPage;
73273  iCellIdx = pCur->ix;
73274  pPage = pCur->pPage;
73275  pCell = findCell(pPage, iCellIdx);
73276  if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ) return SQLITE_CORRUPT;
73277
73278  /* If the bPreserve flag is set to true, then the cursor position must
73279  ** be preserved following this delete operation. If the current delete
73280  ** will cause a b-tree rebalance, then this is done by saving the cursor
73281  ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
73282  ** returning.
73283  **
73284  ** Or, if the current delete will not cause a rebalance, then the cursor
73285  ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
73286  ** before or after the deleted entry. In this case set bSkipnext to true.  */
73287  if( bPreserve ){
73288    if( !pPage->leaf
73289     || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
73290     || pPage->nCell==1  /* See dbfuzz001.test for a test case */
73291    ){
73292      /* A b-tree rebalance will be required after deleting this entry.
73293      ** Save the cursor key.  */
73294      rc = saveCursorKey(pCur);
73295      if( rc ) return rc;
73296    }else{
73297      bSkipnext = 1;
73298    }
73299  }
73300
73301  /* If the page containing the entry to delete is not a leaf page, move
73302  ** the cursor to the largest entry in the tree that is smaller than
73303  ** the entry being deleted. This cell will replace the cell being deleted
73304  ** from the internal node. The 'previous' entry is used for this instead
73305  ** of the 'next' entry, as the previous entry is always a part of the
73306  ** sub-tree headed by the child page of the cell being deleted. This makes
73307  ** balancing the tree following the delete operation easier.  */
73308  if( !pPage->leaf ){
73309    rc = sqlite3BtreePrevious(pCur, 0);
73310    assert( rc!=SQLITE_DONE );
73311    if( rc ) return rc;
73312  }
73313
73314  /* Save the positions of any other cursors open on this table before
73315  ** making any modifications.  */
73316  if( pCur->curFlags & BTCF_Multiple ){
73317    rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
73318    if( rc ) return rc;
73319  }
73320
73321  /* If this is a delete operation to remove a row from a table b-tree,
73322  ** invalidate any incrblob cursors open on the row being deleted.  */
73323  if( pCur->pKeyInfo==0 ){
73324    invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
73325  }
73326
73327  /* Make the page containing the entry to be deleted writable. Then free any
73328  ** overflow pages associated with the entry and finally remove the cell
73329  ** itself from within the page.  */
73330  rc = sqlite3PagerWrite(pPage->pDbPage);
73331  if( rc ) return rc;
73332  rc = clearCell(pPage, pCell, &info);
73333  dropCell(pPage, iCellIdx, info.nSize, &rc);
73334  if( rc ) return rc;
73335
73336  /* If the cell deleted was not located on a leaf page, then the cursor
73337  ** is currently pointing to the largest entry in the sub-tree headed
73338  ** by the child-page of the cell that was just deleted from an internal
73339  ** node. The cell from the leaf node needs to be moved to the internal
73340  ** node to replace the deleted cell.  */
73341  if( !pPage->leaf ){
73342    MemPage *pLeaf = pCur->pPage;
73343    int nCell;
73344    Pgno n;
73345    unsigned char *pTmp;
73346
73347    if( pLeaf->nFree<0 ){
73348      rc = btreeComputeFreeSpace(pLeaf);
73349      if( rc ) return rc;
73350    }
73351    if( iCellDepth<pCur->iPage-1 ){
73352      n = pCur->apPage[iCellDepth+1]->pgno;
73353    }else{
73354      n = pCur->pPage->pgno;
73355    }
73356    pCell = findCell(pLeaf, pLeaf->nCell-1);
73357    if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
73358    nCell = pLeaf->xCellSize(pLeaf, pCell);
73359    assert( MX_CELL_SIZE(pBt) >= nCell );
73360    pTmp = pBt->pTmpSpace;
73361    assert( pTmp!=0 );
73362    rc = sqlite3PagerWrite(pLeaf->pDbPage);
73363    if( rc==SQLITE_OK ){
73364      insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
73365    }
73366    dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
73367    if( rc ) return rc;
73368  }
73369
73370  /* Balance the tree. If the entry deleted was located on a leaf page,
73371  ** then the cursor still points to that page. In this case the first
73372  ** call to balance() repairs the tree, and the if(...) condition is
73373  ** never true.
73374  **
73375  ** Otherwise, if the entry deleted was on an internal node page, then
73376  ** pCur is pointing to the leaf page from which a cell was removed to
73377  ** replace the cell deleted from the internal node. This is slightly
73378  ** tricky as the leaf node may be underfull, and the internal node may
73379  ** be either under or overfull. In this case run the balancing algorithm
73380  ** on the leaf node first. If the balance proceeds far enough up the
73381  ** tree that we can be sure that any problem in the internal node has
73382  ** been corrected, so be it. Otherwise, after balancing the leaf node,
73383  ** walk the cursor up the tree to the internal node and balance it as
73384  ** well.  */
73385  rc = balance(pCur);
73386  if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
73387    releasePageNotNull(pCur->pPage);
73388    pCur->iPage--;
73389    while( pCur->iPage>iCellDepth ){
73390      releasePage(pCur->apPage[pCur->iPage--]);
73391    }
73392    pCur->pPage = pCur->apPage[pCur->iPage];
73393    rc = balance(pCur);
73394  }
73395
73396  if( rc==SQLITE_OK ){
73397    if( bSkipnext ){
73398      assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
73399      assert( pPage==pCur->pPage || CORRUPT_DB );
73400      assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
73401      pCur->eState = CURSOR_SKIPNEXT;
73402      if( iCellIdx>=pPage->nCell ){
73403        pCur->skipNext = -1;
73404        pCur->ix = pPage->nCell-1;
73405      }else{
73406        pCur->skipNext = 1;
73407      }
73408    }else{
73409      rc = moveToRoot(pCur);
73410      if( bPreserve ){
73411        btreeReleaseAllCursorPages(pCur);
73412        pCur->eState = CURSOR_REQUIRESEEK;
73413      }
73414      if( rc==SQLITE_EMPTY ) rc = SQLITE_OK;
73415    }
73416  }
73417  return rc;
73418}
73419
73420/*
73421** Create a new BTree table.  Write into *piTable the page
73422** number for the root page of the new table.
73423**
73424** The type of type is determined by the flags parameter.  Only the
73425** following values of flags are currently in use.  Other values for
73426** flags might not work:
73427**
73428**     BTREE_INTKEY|BTREE_LEAFDATA     Used for SQL tables with rowid keys
73429**     BTREE_ZERODATA                  Used for SQL indices
73430*/
73431static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
73432  BtShared *pBt = p->pBt;
73433  MemPage *pRoot;
73434  Pgno pgnoRoot;
73435  int rc;
73436  int ptfFlags;          /* Page-type flage for the root page of new table */
73437
73438  assert( sqlite3BtreeHoldsMutex(p) );
73439  assert( pBt->inTransaction==TRANS_WRITE );
73440  assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
73441
73442#ifdef SQLITE_OMIT_AUTOVACUUM
73443  rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
73444  if( rc ){
73445    return rc;
73446  }
73447#else
73448  if( pBt->autoVacuum ){
73449    Pgno pgnoMove;      /* Move a page here to make room for the root-page */
73450    MemPage *pPageMove; /* The page to move to. */
73451
73452    /* Creating a new table may probably require moving an existing database
73453    ** to make room for the new tables root page. In case this page turns
73454    ** out to be an overflow page, delete all overflow page-map caches
73455    ** held by open cursors.
73456    */
73457    invalidateAllOverflowCache(pBt);
73458
73459    /* Read the value of meta[3] from the database to determine where the
73460    ** root page of the new table should go. meta[3] is the largest root-page
73461    ** created so far, so the new root-page is (meta[3]+1).
73462    */
73463    sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
73464    pgnoRoot++;
73465
73466    /* The new root-page may not be allocated on a pointer-map page, or the
73467    ** PENDING_BYTE page.
73468    */
73469    while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
73470        pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
73471      pgnoRoot++;
73472    }
73473    assert( pgnoRoot>=3 || CORRUPT_DB );
73474    testcase( pgnoRoot<3 );
73475
73476    /* Allocate a page. The page that currently resides at pgnoRoot will
73477    ** be moved to the allocated page (unless the allocated page happens
73478    ** to reside at pgnoRoot).
73479    */
73480    rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT);
73481    if( rc!=SQLITE_OK ){
73482      return rc;
73483    }
73484
73485    if( pgnoMove!=pgnoRoot ){
73486      /* pgnoRoot is the page that will be used for the root-page of
73487      ** the new table (assuming an error did not occur). But we were
73488      ** allocated pgnoMove. If required (i.e. if it was not allocated
73489      ** by extending the file), the current page at position pgnoMove
73490      ** is already journaled.
73491      */
73492      u8 eType = 0;
73493      Pgno iPtrPage = 0;
73494
73495      /* Save the positions of any open cursors. This is required in
73496      ** case they are holding a reference to an xFetch reference
73497      ** corresponding to page pgnoRoot.  */
73498      rc = saveAllCursors(pBt, 0, 0);
73499      releasePage(pPageMove);
73500      if( rc!=SQLITE_OK ){
73501        return rc;
73502      }
73503
73504      /* Move the page currently at pgnoRoot to pgnoMove. */
73505      rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
73506      if( rc!=SQLITE_OK ){
73507        return rc;
73508      }
73509      rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
73510      if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
73511        rc = SQLITE_CORRUPT_BKPT;
73512      }
73513      if( rc!=SQLITE_OK ){
73514        releasePage(pRoot);
73515        return rc;
73516      }
73517      assert( eType!=PTRMAP_ROOTPAGE );
73518      assert( eType!=PTRMAP_FREEPAGE );
73519      rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
73520      releasePage(pRoot);
73521
73522      /* Obtain the page at pgnoRoot */
73523      if( rc!=SQLITE_OK ){
73524        return rc;
73525      }
73526      rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
73527      if( rc!=SQLITE_OK ){
73528        return rc;
73529      }
73530      rc = sqlite3PagerWrite(pRoot->pDbPage);
73531      if( rc!=SQLITE_OK ){
73532        releasePage(pRoot);
73533        return rc;
73534      }
73535    }else{
73536      pRoot = pPageMove;
73537    }
73538
73539    /* Update the pointer-map and meta-data with the new root-page number. */
73540    ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
73541    if( rc ){
73542      releasePage(pRoot);
73543      return rc;
73544    }
73545
73546    /* When the new root page was allocated, page 1 was made writable in
73547    ** order either to increase the database filesize, or to decrement the
73548    ** freelist count.  Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
73549    */
73550    assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
73551    rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
73552    if( NEVER(rc) ){
73553      releasePage(pRoot);
73554      return rc;
73555    }
73556
73557  }else{
73558    rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
73559    if( rc ) return rc;
73560  }
73561#endif
73562  assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
73563  if( createTabFlags & BTREE_INTKEY ){
73564    ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
73565  }else{
73566    ptfFlags = PTF_ZERODATA | PTF_LEAF;
73567  }
73568  zeroPage(pRoot, ptfFlags);
73569  sqlite3PagerUnref(pRoot->pDbPage);
73570  assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
73571  *piTable = (int)pgnoRoot;
73572  return SQLITE_OK;
73573}
73574SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
73575  int rc;
73576  sqlite3BtreeEnter(p);
73577  rc = btreeCreateTable(p, piTable, flags);
73578  sqlite3BtreeLeave(p);
73579  return rc;
73580}
73581
73582/*
73583** Erase the given database page and all its children.  Return
73584** the page to the freelist.
73585*/
73586static int clearDatabasePage(
73587  BtShared *pBt,           /* The BTree that contains the table */
73588  Pgno pgno,               /* Page number to clear */
73589  int freePageFlag,        /* Deallocate page if true */
73590  int *pnChange            /* Add number of Cells freed to this counter */
73591){
73592  MemPage *pPage;
73593  int rc;
73594  unsigned char *pCell;
73595  int i;
73596  int hdr;
73597  CellInfo info;
73598
73599  assert( sqlite3_mutex_held(pBt->mutex) );
73600  if( pgno>btreePagecount(pBt) ){
73601    return SQLITE_CORRUPT_BKPT;
73602  }
73603  rc = getAndInitPage(pBt, pgno, &pPage, 0, 0);
73604  if( rc ) return rc;
73605  if( pPage->bBusy ){
73606    rc = SQLITE_CORRUPT_BKPT;
73607    goto cleardatabasepage_out;
73608  }
73609  pPage->bBusy = 1;
73610  hdr = pPage->hdrOffset;
73611  for(i=0; i<pPage->nCell; i++){
73612    pCell = findCell(pPage, i);
73613    if( !pPage->leaf ){
73614      rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
73615      if( rc ) goto cleardatabasepage_out;
73616    }
73617    rc = clearCell(pPage, pCell, &info);
73618    if( rc ) goto cleardatabasepage_out;
73619  }
73620  if( !pPage->leaf ){
73621    rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
73622    if( rc ) goto cleardatabasepage_out;
73623  }else if( pnChange ){
73624    assert( pPage->intKey || CORRUPT_DB );
73625    testcase( !pPage->intKey );
73626    *pnChange += pPage->nCell;
73627  }
73628  if( freePageFlag ){
73629    freePage(pPage, &rc);
73630  }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
73631    zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
73632  }
73633
73634cleardatabasepage_out:
73635  pPage->bBusy = 0;
73636  releasePage(pPage);
73637  return rc;
73638}
73639
73640/*
73641** Delete all information from a single table in the database.  iTable is
73642** the page number of the root of the table.  After this routine returns,
73643** the root page is empty, but still exists.
73644**
73645** This routine will fail with SQLITE_LOCKED if there are any open
73646** read cursors on the table.  Open write cursors are moved to the
73647** root of the table.
73648**
73649** If pnChange is not NULL, then table iTable must be an intkey table. The
73650** integer value pointed to by pnChange is incremented by the number of
73651** entries in the table.
73652*/
73653SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
73654  int rc;
73655  BtShared *pBt = p->pBt;
73656  sqlite3BtreeEnter(p);
73657  assert( p->inTrans==TRANS_WRITE );
73658
73659  rc = saveAllCursors(pBt, (Pgno)iTable, 0);
73660
73661  if( SQLITE_OK==rc ){
73662    /* Invalidate all incrblob cursors open on table iTable (assuming iTable
73663    ** is the root of a table b-tree - if it is not, the following call is
73664    ** a no-op).  */
73665    invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1);
73666    rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
73667  }
73668  sqlite3BtreeLeave(p);
73669  return rc;
73670}
73671
73672/*
73673** Delete all information from the single table that pCur is open on.
73674**
73675** This routine only work for pCur on an ephemeral table.
73676*/
73677SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
73678  return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
73679}
73680
73681/*
73682** Erase all information in a table and add the root of the table to
73683** the freelist.  Except, the root of the principle table (the one on
73684** page 1) is never added to the freelist.
73685**
73686** This routine will fail with SQLITE_LOCKED if there are any open
73687** cursors on the table.
73688**
73689** If AUTOVACUUM is enabled and the page at iTable is not the last
73690** root page in the database file, then the last root page
73691** in the database file is moved into the slot formerly occupied by
73692** iTable and that last slot formerly occupied by the last root page
73693** is added to the freelist instead of iTable.  In this say, all
73694** root pages are kept at the beginning of the database file, which
73695** is necessary for AUTOVACUUM to work right.  *piMoved is set to the
73696** page number that used to be the last root page in the file before
73697** the move.  If no page gets moved, *piMoved is set to 0.
73698** The last root page is recorded in meta[3] and the value of
73699** meta[3] is updated by this procedure.
73700*/
73701static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
73702  int rc;
73703  MemPage *pPage = 0;
73704  BtShared *pBt = p->pBt;
73705
73706  assert( sqlite3BtreeHoldsMutex(p) );
73707  assert( p->inTrans==TRANS_WRITE );
73708  assert( iTable>=2 );
73709  if( iTable>btreePagecount(pBt) ){
73710    return SQLITE_CORRUPT_BKPT;
73711  }
73712
73713  rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
73714  if( rc ) return rc;
73715  rc = sqlite3BtreeClearTable(p, iTable, 0);
73716  if( rc ){
73717    releasePage(pPage);
73718    return rc;
73719  }
73720
73721  *piMoved = 0;
73722
73723#ifdef SQLITE_OMIT_AUTOVACUUM
73724  freePage(pPage, &rc);
73725  releasePage(pPage);
73726#else
73727  if( pBt->autoVacuum ){
73728    Pgno maxRootPgno;
73729    sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
73730
73731    if( iTable==maxRootPgno ){
73732      /* If the table being dropped is the table with the largest root-page
73733      ** number in the database, put the root page on the free list.
73734      */
73735      freePage(pPage, &rc);
73736      releasePage(pPage);
73737      if( rc!=SQLITE_OK ){
73738        return rc;
73739      }
73740    }else{
73741      /* The table being dropped does not have the largest root-page
73742      ** number in the database. So move the page that does into the
73743      ** gap left by the deleted root-page.
73744      */
73745      MemPage *pMove;
73746      releasePage(pPage);
73747      rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
73748      if( rc!=SQLITE_OK ){
73749        return rc;
73750      }
73751      rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
73752      releasePage(pMove);
73753      if( rc!=SQLITE_OK ){
73754        return rc;
73755      }
73756      pMove = 0;
73757      rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
73758      freePage(pMove, &rc);
73759      releasePage(pMove);
73760      if( rc!=SQLITE_OK ){
73761        return rc;
73762      }
73763      *piMoved = maxRootPgno;
73764    }
73765
73766    /* Set the new 'max-root-page' value in the database header. This
73767    ** is the old value less one, less one more if that happens to
73768    ** be a root-page number, less one again if that is the
73769    ** PENDING_BYTE_PAGE.
73770    */
73771    maxRootPgno--;
73772    while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
73773           || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
73774      maxRootPgno--;
73775    }
73776    assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
73777
73778    rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
73779  }else{
73780    freePage(pPage, &rc);
73781    releasePage(pPage);
73782  }
73783#endif
73784  return rc;
73785}
73786SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
73787  int rc;
73788  sqlite3BtreeEnter(p);
73789  rc = btreeDropTable(p, iTable, piMoved);
73790  sqlite3BtreeLeave(p);
73791  return rc;
73792}
73793
73794
73795/*
73796** This function may only be called if the b-tree connection already
73797** has a read or write transaction open on the database.
73798**
73799** Read the meta-information out of a database file.  Meta[0]
73800** is the number of free pages currently in the database.  Meta[1]
73801** through meta[15] are available for use by higher layers.  Meta[0]
73802** is read-only, the others are read/write.
73803**
73804** The schema layer numbers meta values differently.  At the schema
73805** layer (and the SetCookie and ReadCookie opcodes) the number of
73806** free pages is not visible.  So Cookie[0] is the same as Meta[1].
73807**
73808** This routine treats Meta[BTREE_DATA_VERSION] as a special case.  Instead
73809** of reading the value out of the header, it instead loads the "DataVersion"
73810** from the pager.  The BTREE_DATA_VERSION value is not actually stored in the
73811** database file.  It is a number computed by the pager.  But its access
73812** pattern is the same as header meta values, and so it is convenient to
73813** read it from this routine.
73814*/
73815SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
73816  BtShared *pBt = p->pBt;
73817
73818  sqlite3BtreeEnter(p);
73819  assert( p->inTrans>TRANS_NONE );
73820  assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
73821  assert( pBt->pPage1 );
73822  assert( idx>=0 && idx<=15 );
73823
73824  if( idx==BTREE_DATA_VERSION ){
73825    *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iDataVersion;
73826  }else{
73827    *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
73828  }
73829
73830  /* If auto-vacuum is disabled in this build and this is an auto-vacuum
73831  ** database, mark the database as read-only.  */
73832#ifdef SQLITE_OMIT_AUTOVACUUM
73833  if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
73834    pBt->btsFlags |= BTS_READ_ONLY;
73835  }
73836#endif
73837
73838  sqlite3BtreeLeave(p);
73839}
73840
73841/*
73842** Write meta-information back into the database.  Meta[0] is
73843** read-only and may not be written.
73844*/
73845SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
73846  BtShared *pBt = p->pBt;
73847  unsigned char *pP1;
73848  int rc;
73849  assert( idx>=1 && idx<=15 );
73850  sqlite3BtreeEnter(p);
73851  assert( p->inTrans==TRANS_WRITE );
73852  assert( pBt->pPage1!=0 );
73853  pP1 = pBt->pPage1->aData;
73854  rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
73855  if( rc==SQLITE_OK ){
73856    put4byte(&pP1[36 + idx*4], iMeta);
73857#ifndef SQLITE_OMIT_AUTOVACUUM
73858    if( idx==BTREE_INCR_VACUUM ){
73859      assert( pBt->autoVacuum || iMeta==0 );
73860      assert( iMeta==0 || iMeta==1 );
73861      pBt->incrVacuum = (u8)iMeta;
73862    }
73863#endif
73864  }
73865  sqlite3BtreeLeave(p);
73866  return rc;
73867}
73868
73869/*
73870** The first argument, pCur, is a cursor opened on some b-tree. Count the
73871** number of entries in the b-tree and write the result to *pnEntry.
73872**
73873** SQLITE_OK is returned if the operation is successfully executed.
73874** Otherwise, if an error is encountered (i.e. an IO error or database
73875** corruption) an SQLite error code is returned.
73876*/
73877SQLITE_PRIVATE int sqlite3BtreeCount(sqlite3 *db, BtCursor *pCur, i64 *pnEntry){
73878  i64 nEntry = 0;                      /* Value to return in *pnEntry */
73879  int rc;                              /* Return code */
73880
73881  rc = moveToRoot(pCur);
73882  if( rc==SQLITE_EMPTY ){
73883    *pnEntry = 0;
73884    return SQLITE_OK;
73885  }
73886
73887  /* Unless an error occurs, the following loop runs one iteration for each
73888  ** page in the B-Tree structure (not including overflow pages).
73889  */
73890  while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){
73891    int iIdx;                          /* Index of child node in parent */
73892    MemPage *pPage;                    /* Current page of the b-tree */
73893
73894    /* If this is a leaf page or the tree is not an int-key tree, then
73895    ** this page contains countable entries. Increment the entry counter
73896    ** accordingly.
73897    */
73898    pPage = pCur->pPage;
73899    if( pPage->leaf || !pPage->intKey ){
73900      nEntry += pPage->nCell;
73901    }
73902
73903    /* pPage is a leaf node. This loop navigates the cursor so that it
73904    ** points to the first interior cell that it points to the parent of
73905    ** the next page in the tree that has not yet been visited. The
73906    ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
73907    ** of the page, or to the number of cells in the page if the next page
73908    ** to visit is the right-child of its parent.
73909    **
73910    ** If all pages in the tree have been visited, return SQLITE_OK to the
73911    ** caller.
73912    */
73913    if( pPage->leaf ){
73914      do {
73915        if( pCur->iPage==0 ){
73916          /* All pages of the b-tree have been visited. Return successfully. */
73917          *pnEntry = nEntry;
73918          return moveToRoot(pCur);
73919        }
73920        moveToParent(pCur);
73921      }while ( pCur->ix>=pCur->pPage->nCell );
73922
73923      pCur->ix++;
73924      pPage = pCur->pPage;
73925    }
73926
73927    /* Descend to the child node of the cell that the cursor currently
73928    ** points at. This is the right-child if (iIdx==pPage->nCell).
73929    */
73930    iIdx = pCur->ix;
73931    if( iIdx==pPage->nCell ){
73932      rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
73933    }else{
73934      rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
73935    }
73936  }
73937
73938  /* An error has occurred. Return an error code. */
73939  return rc;
73940}
73941
73942/*
73943** Return the pager associated with a BTree.  This routine is used for
73944** testing and debugging only.
73945*/
73946SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
73947  return p->pBt->pPager;
73948}
73949
73950#ifndef SQLITE_OMIT_INTEGRITY_CHECK
73951/*
73952** Append a message to the error message string.
73953*/
73954static void checkAppendMsg(
73955  IntegrityCk *pCheck,
73956  const char *zFormat,
73957  ...
73958){
73959  va_list ap;
73960  if( !pCheck->mxErr ) return;
73961  pCheck->mxErr--;
73962  pCheck->nErr++;
73963  va_start(ap, zFormat);
73964  if( pCheck->errMsg.nChar ){
73965    sqlite3_str_append(&pCheck->errMsg, "\n", 1);
73966  }
73967  if( pCheck->zPfx ){
73968    sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
73969  }
73970  sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
73971  va_end(ap);
73972  if( pCheck->errMsg.accError==SQLITE_NOMEM ){
73973    pCheck->mallocFailed = 1;
73974  }
73975}
73976#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
73977
73978#ifndef SQLITE_OMIT_INTEGRITY_CHECK
73979
73980/*
73981** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
73982** corresponds to page iPg is already set.
73983*/
73984static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
73985  assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
73986  return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
73987}
73988
73989/*
73990** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
73991*/
73992static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
73993  assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
73994  pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
73995}
73996
73997
73998/*
73999** Add 1 to the reference count for page iPage.  If this is the second
74000** reference to the page, add an error message to pCheck->zErrMsg.
74001** Return 1 if there are 2 or more references to the page and 0 if
74002** if this is the first reference to the page.
74003**
74004** Also check that the page number is in bounds.
74005*/
74006static int checkRef(IntegrityCk *pCheck, Pgno iPage){
74007  if( iPage>pCheck->nPage || iPage==0 ){
74008    checkAppendMsg(pCheck, "invalid page number %d", iPage);
74009    return 1;
74010  }
74011  if( getPageReferenced(pCheck, iPage) ){
74012    checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
74013    return 1;
74014  }
74015  if( AtomicLoad(&pCheck->db->u1.isInterrupted) ) return 1;
74016  setPageReferenced(pCheck, iPage);
74017  return 0;
74018}
74019
74020#ifndef SQLITE_OMIT_AUTOVACUUM
74021/*
74022** Check that the entry in the pointer-map for page iChild maps to
74023** page iParent, pointer type ptrType. If not, append an error message
74024** to pCheck.
74025*/
74026static void checkPtrmap(
74027  IntegrityCk *pCheck,   /* Integrity check context */
74028  Pgno iChild,           /* Child page number */
74029  u8 eType,              /* Expected pointer map type */
74030  Pgno iParent           /* Expected pointer map parent page number */
74031){
74032  int rc;
74033  u8 ePtrmapType;
74034  Pgno iPtrmapParent;
74035
74036  rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
74037  if( rc!=SQLITE_OK ){
74038    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
74039    checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild);
74040    return;
74041  }
74042
74043  if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
74044    checkAppendMsg(pCheck,
74045      "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
74046      iChild, eType, iParent, ePtrmapType, iPtrmapParent);
74047  }
74048}
74049#endif
74050
74051/*
74052** Check the integrity of the freelist or of an overflow page list.
74053** Verify that the number of pages on the list is N.
74054*/
74055static void checkList(
74056  IntegrityCk *pCheck,  /* Integrity checking context */
74057  int isFreeList,       /* True for a freelist.  False for overflow page list */
74058  int iPage,            /* Page number for first page in the list */
74059  u32 N                 /* Expected number of pages in the list */
74060){
74061  int i;
74062  u32 expected = N;
74063  int nErrAtStart = pCheck->nErr;
74064  while( iPage!=0 && pCheck->mxErr ){
74065    DbPage *pOvflPage;
74066    unsigned char *pOvflData;
74067    if( checkRef(pCheck, iPage) ) break;
74068    N--;
74069    if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
74070      checkAppendMsg(pCheck, "failed to get page %d", iPage);
74071      break;
74072    }
74073    pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
74074    if( isFreeList ){
74075      u32 n = (u32)get4byte(&pOvflData[4]);
74076#ifndef SQLITE_OMIT_AUTOVACUUM
74077      if( pCheck->pBt->autoVacuum ){
74078        checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0);
74079      }
74080#endif
74081      if( n>pCheck->pBt->usableSize/4-2 ){
74082        checkAppendMsg(pCheck,
74083           "freelist leaf count too big on page %d", iPage);
74084        N--;
74085      }else{
74086        for(i=0; i<(int)n; i++){
74087          Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
74088#ifndef SQLITE_OMIT_AUTOVACUUM
74089          if( pCheck->pBt->autoVacuum ){
74090            checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0);
74091          }
74092#endif
74093          checkRef(pCheck, iFreePage);
74094        }
74095        N -= n;
74096      }
74097    }
74098#ifndef SQLITE_OMIT_AUTOVACUUM
74099    else{
74100      /* If this database supports auto-vacuum and iPage is not the last
74101      ** page in this overflow list, check that the pointer-map entry for
74102      ** the following page matches iPage.
74103      */
74104      if( pCheck->pBt->autoVacuum && N>0 ){
74105        i = get4byte(pOvflData);
74106        checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage);
74107      }
74108    }
74109#endif
74110    iPage = get4byte(pOvflData);
74111    sqlite3PagerUnref(pOvflPage);
74112  }
74113  if( N && nErrAtStart==pCheck->nErr ){
74114    checkAppendMsg(pCheck,
74115      "%s is %d but should be %d",
74116      isFreeList ? "size" : "overflow list length",
74117      expected-N, expected);
74118  }
74119}
74120#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
74121
74122/*
74123** An implementation of a min-heap.
74124**
74125** aHeap[0] is the number of elements on the heap.  aHeap[1] is the
74126** root element.  The daughter nodes of aHeap[N] are aHeap[N*2]
74127** and aHeap[N*2+1].
74128**
74129** The heap property is this:  Every node is less than or equal to both
74130** of its daughter nodes.  A consequence of the heap property is that the
74131** root node aHeap[1] is always the minimum value currently in the heap.
74132**
74133** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
74134** the heap, preserving the heap property.  The btreeHeapPull() routine
74135** removes the root element from the heap (the minimum value in the heap)
74136** and then moves other nodes around as necessary to preserve the heap
74137** property.
74138**
74139** This heap is used for cell overlap and coverage testing.  Each u32
74140** entry represents the span of a cell or freeblock on a btree page.
74141** The upper 16 bits are the index of the first byte of a range and the
74142** lower 16 bits are the index of the last byte of that range.
74143*/
74144static void btreeHeapInsert(u32 *aHeap, u32 x){
74145  u32 j, i = ++aHeap[0];
74146  aHeap[i] = x;
74147  while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
74148    x = aHeap[j];
74149    aHeap[j] = aHeap[i];
74150    aHeap[i] = x;
74151    i = j;
74152  }
74153}
74154static int btreeHeapPull(u32 *aHeap, u32 *pOut){
74155  u32 j, i, x;
74156  if( (x = aHeap[0])==0 ) return 0;
74157  *pOut = aHeap[1];
74158  aHeap[1] = aHeap[x];
74159  aHeap[x] = 0xffffffff;
74160  aHeap[0]--;
74161  i = 1;
74162  while( (j = i*2)<=aHeap[0] ){
74163    if( aHeap[j]>aHeap[j+1] ) j++;
74164    if( aHeap[i]<aHeap[j] ) break;
74165    x = aHeap[i];
74166    aHeap[i] = aHeap[j];
74167    aHeap[j] = x;
74168    i = j;
74169  }
74170  return 1;
74171}
74172
74173#ifndef SQLITE_OMIT_INTEGRITY_CHECK
74174/*
74175** Do various sanity checks on a single page of a tree.  Return
74176** the tree depth.  Root pages return 0.  Parents of root pages
74177** return 1, and so forth.
74178**
74179** These checks are done:
74180**
74181**      1.  Make sure that cells and freeblocks do not overlap
74182**          but combine to completely cover the page.
74183**      2.  Make sure integer cell keys are in order.
74184**      3.  Check the integrity of overflow pages.
74185**      4.  Recursively call checkTreePage on all children.
74186**      5.  Verify that the depth of all children is the same.
74187*/
74188static int checkTreePage(
74189  IntegrityCk *pCheck,  /* Context for the sanity check */
74190  int iPage,            /* Page number of the page to check */
74191  i64 *piMinKey,        /* Write minimum integer primary key here */
74192  i64 maxKey            /* Error if integer primary key greater than this */
74193){
74194  MemPage *pPage = 0;      /* The page being analyzed */
74195  int i;                   /* Loop counter */
74196  int rc;                  /* Result code from subroutine call */
74197  int depth = -1, d2;      /* Depth of a subtree */
74198  int pgno;                /* Page number */
74199  int nFrag;               /* Number of fragmented bytes on the page */
74200  int hdr;                 /* Offset to the page header */
74201  int cellStart;           /* Offset to the start of the cell pointer array */
74202  int nCell;               /* Number of cells */
74203  int doCoverageCheck = 1; /* True if cell coverage checking should be done */
74204  int keyCanBeEqual = 1;   /* True if IPK can be equal to maxKey
74205                           ** False if IPK must be strictly less than maxKey */
74206  u8 *data;                /* Page content */
74207  u8 *pCell;               /* Cell content */
74208  u8 *pCellIdx;            /* Next element of the cell pointer array */
74209  BtShared *pBt;           /* The BtShared object that owns pPage */
74210  u32 pc;                  /* Address of a cell */
74211  u32 usableSize;          /* Usable size of the page */
74212  u32 contentOffset;       /* Offset to the start of the cell content area */
74213  u32 *heap = 0;           /* Min-heap used for checking cell coverage */
74214  u32 x, prev = 0;         /* Next and previous entry on the min-heap */
74215  const char *saved_zPfx = pCheck->zPfx;
74216  int saved_v1 = pCheck->v1;
74217  int saved_v2 = pCheck->v2;
74218  u8 savedIsInit = 0;
74219
74220  /* Check that the page exists
74221  */
74222  pBt = pCheck->pBt;
74223  usableSize = pBt->usableSize;
74224  if( iPage==0 ) return 0;
74225  if( checkRef(pCheck, iPage) ) return 0;
74226  pCheck->zPfx = "Page %d: ";
74227  pCheck->v1 = iPage;
74228  if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
74229    checkAppendMsg(pCheck,
74230       "unable to get the page. error code=%d", rc);
74231    goto end_of_check;
74232  }
74233
74234  /* Clear MemPage.isInit to make sure the corruption detection code in
74235  ** btreeInitPage() is executed.  */
74236  savedIsInit = pPage->isInit;
74237  pPage->isInit = 0;
74238  if( (rc = btreeInitPage(pPage))!=0 ){
74239    assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
74240    checkAppendMsg(pCheck,
74241                   "btreeInitPage() returns error code %d", rc);
74242    goto end_of_check;
74243  }
74244  if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
74245    assert( rc==SQLITE_CORRUPT );
74246    checkAppendMsg(pCheck, "free space corruption", rc);
74247    goto end_of_check;
74248  }
74249  data = pPage->aData;
74250  hdr = pPage->hdrOffset;
74251
74252  /* Set up for cell analysis */
74253  pCheck->zPfx = "On tree page %d cell %d: ";
74254  contentOffset = get2byteNotZero(&data[hdr+5]);
74255  assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
74256
74257  /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
74258  ** number of cells on the page. */
74259  nCell = get2byte(&data[hdr+3]);
74260  assert( pPage->nCell==nCell );
74261
74262  /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
74263  ** immediately follows the b-tree page header. */
74264  cellStart = hdr + 12 - 4*pPage->leaf;
74265  assert( pPage->aCellIdx==&data[cellStart] );
74266  pCellIdx = &data[cellStart + 2*(nCell-1)];
74267
74268  if( !pPage->leaf ){
74269    /* Analyze the right-child page of internal pages */
74270    pgno = get4byte(&data[hdr+8]);
74271#ifndef SQLITE_OMIT_AUTOVACUUM
74272    if( pBt->autoVacuum ){
74273      pCheck->zPfx = "On page %d at right child: ";
74274      checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
74275    }
74276#endif
74277    depth = checkTreePage(pCheck, pgno, &maxKey, maxKey);
74278    keyCanBeEqual = 0;
74279  }else{
74280    /* For leaf pages, the coverage check will occur in the same loop
74281    ** as the other cell checks, so initialize the heap.  */
74282    heap = pCheck->heap;
74283    heap[0] = 0;
74284  }
74285
74286  /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
74287  ** integer offsets to the cell contents. */
74288  for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
74289    CellInfo info;
74290
74291    /* Check cell size */
74292    pCheck->v2 = i;
74293    assert( pCellIdx==&data[cellStart + i*2] );
74294    pc = get2byteAligned(pCellIdx);
74295    pCellIdx -= 2;
74296    if( pc<contentOffset || pc>usableSize-4 ){
74297      checkAppendMsg(pCheck, "Offset %d out of range %d..%d",
74298                             pc, contentOffset, usableSize-4);
74299      doCoverageCheck = 0;
74300      continue;
74301    }
74302    pCell = &data[pc];
74303    pPage->xParseCell(pPage, pCell, &info);
74304    if( pc+info.nSize>usableSize ){
74305      checkAppendMsg(pCheck, "Extends off end of page");
74306      doCoverageCheck = 0;
74307      continue;
74308    }
74309
74310    /* Check for integer primary key out of range */
74311    if( pPage->intKey ){
74312      if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
74313        checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey);
74314      }
74315      maxKey = info.nKey;
74316      keyCanBeEqual = 0;     /* Only the first key on the page may ==maxKey */
74317    }
74318
74319    /* Check the content overflow list */
74320    if( info.nPayload>info.nLocal ){
74321      u32 nPage;       /* Number of pages on the overflow chain */
74322      Pgno pgnoOvfl;   /* First page of the overflow chain */
74323      assert( pc + info.nSize - 4 <= usableSize );
74324      nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
74325      pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
74326#ifndef SQLITE_OMIT_AUTOVACUUM
74327      if( pBt->autoVacuum ){
74328        checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
74329      }
74330#endif
74331      checkList(pCheck, 0, pgnoOvfl, nPage);
74332    }
74333
74334    if( !pPage->leaf ){
74335      /* Check sanity of left child page for internal pages */
74336      pgno = get4byte(pCell);
74337#ifndef SQLITE_OMIT_AUTOVACUUM
74338      if( pBt->autoVacuum ){
74339        checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
74340      }
74341#endif
74342      d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
74343      keyCanBeEqual = 0;
74344      if( d2!=depth ){
74345        checkAppendMsg(pCheck, "Child page depth differs");
74346        depth = d2;
74347      }
74348    }else{
74349      /* Populate the coverage-checking heap for leaf pages */
74350      btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
74351    }
74352  }
74353  *piMinKey = maxKey;
74354
74355  /* Check for complete coverage of the page
74356  */
74357  pCheck->zPfx = 0;
74358  if( doCoverageCheck && pCheck->mxErr>0 ){
74359    /* For leaf pages, the min-heap has already been initialized and the
74360    ** cells have already been inserted.  But for internal pages, that has
74361    ** not yet been done, so do it now */
74362    if( !pPage->leaf ){
74363      heap = pCheck->heap;
74364      heap[0] = 0;
74365      for(i=nCell-1; i>=0; i--){
74366        u32 size;
74367        pc = get2byteAligned(&data[cellStart+i*2]);
74368        size = pPage->xCellSize(pPage, &data[pc]);
74369        btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
74370      }
74371    }
74372    /* Add the freeblocks to the min-heap
74373    **
74374    ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
74375    ** is the offset of the first freeblock, or zero if there are no
74376    ** freeblocks on the page.
74377    */
74378    i = get2byte(&data[hdr+1]);
74379    while( i>0 ){
74380      int size, j;
74381      assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
74382      size = get2byte(&data[i+2]);
74383      assert( (u32)(i+size)<=usableSize ); /* due to btreeComputeFreeSpace() */
74384      btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
74385      /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
74386      ** big-endian integer which is the offset in the b-tree page of the next
74387      ** freeblock in the chain, or zero if the freeblock is the last on the
74388      ** chain. */
74389      j = get2byte(&data[i]);
74390      /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
74391      ** increasing offset. */
74392      assert( j==0 || j>i+size );     /* Enforced by btreeComputeFreeSpace() */
74393      assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
74394      i = j;
74395    }
74396    /* Analyze the min-heap looking for overlap between cells and/or
74397    ** freeblocks, and counting the number of untracked bytes in nFrag.
74398    **
74399    ** Each min-heap entry is of the form:    (start_address<<16)|end_address.
74400    ** There is an implied first entry the covers the page header, the cell
74401    ** pointer index, and the gap between the cell pointer index and the start
74402    ** of cell content.
74403    **
74404    ** The loop below pulls entries from the min-heap in order and compares
74405    ** the start_address against the previous end_address.  If there is an
74406    ** overlap, that means bytes are used multiple times.  If there is a gap,
74407    ** that gap is added to the fragmentation count.
74408    */
74409    nFrag = 0;
74410    prev = contentOffset - 1;   /* Implied first min-heap entry */
74411    while( btreeHeapPull(heap,&x) ){
74412      if( (prev&0xffff)>=(x>>16) ){
74413        checkAppendMsg(pCheck,
74414          "Multiple uses for byte %u of page %d", x>>16, iPage);
74415        break;
74416      }else{
74417        nFrag += (x>>16) - (prev&0xffff) - 1;
74418        prev = x;
74419      }
74420    }
74421    nFrag += usableSize - (prev&0xffff) - 1;
74422    /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
74423    ** is stored in the fifth field of the b-tree page header.
74424    ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
74425    ** number of fragmented free bytes within the cell content area.
74426    */
74427    if( heap[0]==0 && nFrag!=data[hdr+7] ){
74428      checkAppendMsg(pCheck,
74429          "Fragmentation of %d bytes reported as %d on page %d",
74430          nFrag, data[hdr+7], iPage);
74431    }
74432  }
74433
74434end_of_check:
74435  if( !doCoverageCheck ) pPage->isInit = savedIsInit;
74436  releasePage(pPage);
74437  pCheck->zPfx = saved_zPfx;
74438  pCheck->v1 = saved_v1;
74439  pCheck->v2 = saved_v2;
74440  return depth+1;
74441}
74442#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
74443
74444#ifndef SQLITE_OMIT_INTEGRITY_CHECK
74445/*
74446** This routine does a complete check of the given BTree file.  aRoot[] is
74447** an array of pages numbers were each page number is the root page of
74448** a table.  nRoot is the number of entries in aRoot.
74449**
74450** A read-only or read-write transaction must be opened before calling
74451** this function.
74452**
74453** Write the number of error seen in *pnErr.  Except for some memory
74454** allocation errors,  an error message held in memory obtained from
74455** malloc is returned if *pnErr is non-zero.  If *pnErr==0 then NULL is
74456** returned.  If a memory allocation error occurs, NULL is returned.
74457*/
74458SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
74459  sqlite3 *db,  /* Database connection that is running the check */
74460  Btree *p,     /* The btree to be checked */
74461  int *aRoot,   /* An array of root pages numbers for individual trees */
74462  int nRoot,    /* Number of entries in aRoot[] */
74463  int mxErr,    /* Stop reporting errors after this many */
74464  int *pnErr    /* Write number of errors seen to this variable */
74465){
74466  Pgno i;
74467  IntegrityCk sCheck;
74468  BtShared *pBt = p->pBt;
74469  u64 savedDbFlags = pBt->db->flags;
74470  char zErr[100];
74471  VVA_ONLY( int nRef );
74472
74473  sqlite3BtreeEnter(p);
74474  assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
74475  VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
74476  assert( nRef>=0 );
74477  sCheck.db = db;
74478  sCheck.pBt = pBt;
74479  sCheck.pPager = pBt->pPager;
74480  sCheck.nPage = btreePagecount(sCheck.pBt);
74481  sCheck.mxErr = mxErr;
74482  sCheck.nErr = 0;
74483  sCheck.mallocFailed = 0;
74484  sCheck.zPfx = 0;
74485  sCheck.v1 = 0;
74486  sCheck.v2 = 0;
74487  sCheck.aPgRef = 0;
74488  sCheck.heap = 0;
74489  sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
74490  sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL;
74491  if( sCheck.nPage==0 ){
74492    goto integrity_ck_cleanup;
74493  }
74494
74495  sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
74496  if( !sCheck.aPgRef ){
74497    sCheck.mallocFailed = 1;
74498    goto integrity_ck_cleanup;
74499  }
74500  sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
74501  if( sCheck.heap==0 ){
74502    sCheck.mallocFailed = 1;
74503    goto integrity_ck_cleanup;
74504  }
74505
74506  i = PENDING_BYTE_PAGE(pBt);
74507  if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
74508
74509  /* Check the integrity of the freelist
74510  */
74511  sCheck.zPfx = "Main freelist: ";
74512  checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
74513            get4byte(&pBt->pPage1->aData[36]));
74514  sCheck.zPfx = 0;
74515
74516  /* Check all the tables.
74517  */
74518#ifndef SQLITE_OMIT_AUTOVACUUM
74519  if( pBt->autoVacuum ){
74520    int mx = 0;
74521    int mxInHdr;
74522    for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
74523    mxInHdr = get4byte(&pBt->pPage1->aData[52]);
74524    if( mx!=mxInHdr ){
74525      checkAppendMsg(&sCheck,
74526        "max rootpage (%d) disagrees with header (%d)",
74527        mx, mxInHdr
74528      );
74529    }
74530  }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
74531    checkAppendMsg(&sCheck,
74532      "incremental_vacuum enabled with a max rootpage of zero"
74533    );
74534  }
74535#endif
74536  testcase( pBt->db->flags & SQLITE_CellSizeCk );
74537  pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
74538  for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
74539    i64 notUsed;
74540    if( aRoot[i]==0 ) continue;
74541#ifndef SQLITE_OMIT_AUTOVACUUM
74542    if( pBt->autoVacuum && aRoot[i]>1 ){
74543      checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
74544    }
74545#endif
74546    checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64);
74547  }
74548  pBt->db->flags = savedDbFlags;
74549
74550  /* Make sure every page in the file is referenced
74551  */
74552  for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
74553#ifdef SQLITE_OMIT_AUTOVACUUM
74554    if( getPageReferenced(&sCheck, i)==0 ){
74555      checkAppendMsg(&sCheck, "Page %d is never used", i);
74556    }
74557#else
74558    /* If the database supports auto-vacuum, make sure no tables contain
74559    ** references to pointer-map pages.
74560    */
74561    if( getPageReferenced(&sCheck, i)==0 &&
74562       (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
74563      checkAppendMsg(&sCheck, "Page %d is never used", i);
74564    }
74565    if( getPageReferenced(&sCheck, i)!=0 &&
74566       (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
74567      checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
74568    }
74569#endif
74570  }
74571
74572  /* Clean  up and report errors.
74573  */
74574integrity_ck_cleanup:
74575  sqlite3PageFree(sCheck.heap);
74576  sqlite3_free(sCheck.aPgRef);
74577  if( sCheck.mallocFailed ){
74578    sqlite3_str_reset(&sCheck.errMsg);
74579    sCheck.nErr++;
74580  }
74581  *pnErr = sCheck.nErr;
74582  if( sCheck.nErr==0 ) sqlite3_str_reset(&sCheck.errMsg);
74583  /* Make sure this analysis did not leave any unref() pages. */
74584  assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
74585  sqlite3BtreeLeave(p);
74586  return sqlite3StrAccumFinish(&sCheck.errMsg);
74587}
74588#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
74589
74590/*
74591** Return the full pathname of the underlying database file.  Return
74592** an empty string if the database is in-memory or a TEMP database.
74593**
74594** The pager filename is invariant as long as the pager is
74595** open so it is safe to access without the BtShared mutex.
74596*/
74597SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
74598  assert( p->pBt->pPager!=0 );
74599  return sqlite3PagerFilename(p->pBt->pPager, 1);
74600}
74601
74602/*
74603** Return the pathname of the journal file for this database. The return
74604** value of this routine is the same regardless of whether the journal file
74605** has been created or not.
74606**
74607** The pager journal filename is invariant as long as the pager is
74608** open so it is safe to access without the BtShared mutex.
74609*/
74610SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
74611  assert( p->pBt->pPager!=0 );
74612  return sqlite3PagerJournalname(p->pBt->pPager);
74613}
74614
74615/*
74616** Return non-zero if a transaction is active.
74617*/
74618SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
74619  assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
74620  return (p && (p->inTrans==TRANS_WRITE));
74621}
74622
74623#ifndef SQLITE_OMIT_WAL
74624/*
74625** Run a checkpoint on the Btree passed as the first argument.
74626**
74627** Return SQLITE_LOCKED if this or any other connection has an open
74628** transaction on the shared-cache the argument Btree is connected to.
74629**
74630** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
74631*/
74632SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
74633  int rc = SQLITE_OK;
74634  if( p ){
74635    BtShared *pBt = p->pBt;
74636    sqlite3BtreeEnter(p);
74637    if( pBt->inTransaction!=TRANS_NONE ){
74638      rc = SQLITE_LOCKED;
74639    }else{
74640      rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
74641    }
74642    sqlite3BtreeLeave(p);
74643  }
74644  return rc;
74645}
74646#endif
74647
74648/*
74649** Return non-zero if a read (or write) transaction is active.
74650*/
74651SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
74652  assert( p );
74653  assert( sqlite3_mutex_held(p->db->mutex) );
74654  return p->inTrans!=TRANS_NONE;
74655}
74656
74657SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
74658  assert( p );
74659  assert( sqlite3_mutex_held(p->db->mutex) );
74660  return p->nBackup!=0;
74661}
74662
74663/*
74664** This function returns a pointer to a blob of memory associated with
74665** a single shared-btree. The memory is used by client code for its own
74666** purposes (for example, to store a high-level schema associated with
74667** the shared-btree). The btree layer manages reference counting issues.
74668**
74669** The first time this is called on a shared-btree, nBytes bytes of memory
74670** are allocated, zeroed, and returned to the caller. For each subsequent
74671** call the nBytes parameter is ignored and a pointer to the same blob
74672** of memory returned.
74673**
74674** If the nBytes parameter is 0 and the blob of memory has not yet been
74675** allocated, a null pointer is returned. If the blob has already been
74676** allocated, it is returned as normal.
74677**
74678** Just before the shared-btree is closed, the function passed as the
74679** xFree argument when the memory allocation was made is invoked on the
74680** blob of allocated memory. The xFree function should not call sqlite3_free()
74681** on the memory, the btree layer does that.
74682*/
74683SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
74684  BtShared *pBt = p->pBt;
74685  sqlite3BtreeEnter(p);
74686  if( !pBt->pSchema && nBytes ){
74687    pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
74688    pBt->xFreeSchema = xFree;
74689  }
74690  sqlite3BtreeLeave(p);
74691  return pBt->pSchema;
74692}
74693
74694/*
74695** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
74696** btree as the argument handle holds an exclusive lock on the
74697** sqlite_master table. Otherwise SQLITE_OK.
74698*/
74699SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
74700  int rc;
74701  assert( sqlite3_mutex_held(p->db->mutex) );
74702  sqlite3BtreeEnter(p);
74703  rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
74704  assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
74705  sqlite3BtreeLeave(p);
74706  return rc;
74707}
74708
74709
74710#ifndef SQLITE_OMIT_SHARED_CACHE
74711/*
74712** Obtain a lock on the table whose root page is iTab.  The
74713** lock is a write lock if isWritelock is true or a read lock
74714** if it is false.
74715*/
74716SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
74717  int rc = SQLITE_OK;
74718  assert( p->inTrans!=TRANS_NONE );
74719  if( p->sharable ){
74720    u8 lockType = READ_LOCK + isWriteLock;
74721    assert( READ_LOCK+1==WRITE_LOCK );
74722    assert( isWriteLock==0 || isWriteLock==1 );
74723
74724    sqlite3BtreeEnter(p);
74725    rc = querySharedCacheTableLock(p, iTab, lockType);
74726    if( rc==SQLITE_OK ){
74727      rc = setSharedCacheTableLock(p, iTab, lockType);
74728    }
74729    sqlite3BtreeLeave(p);
74730  }
74731  return rc;
74732}
74733#endif
74734
74735#ifndef SQLITE_OMIT_INCRBLOB
74736/*
74737** Argument pCsr must be a cursor opened for writing on an
74738** INTKEY table currently pointing at a valid table entry.
74739** This function modifies the data stored as part of that entry.
74740**
74741** Only the data content may only be modified, it is not possible to
74742** change the length of the data stored. If this function is called with
74743** parameters that attempt to write past the end of the existing data,
74744** no modifications are made and SQLITE_CORRUPT is returned.
74745*/
74746SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
74747  int rc;
74748  assert( cursorOwnsBtShared(pCsr) );
74749  assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
74750  assert( pCsr->curFlags & BTCF_Incrblob );
74751
74752  rc = restoreCursorPosition(pCsr);
74753  if( rc!=SQLITE_OK ){
74754    return rc;
74755  }
74756  assert( pCsr->eState!=CURSOR_REQUIRESEEK );
74757  if( pCsr->eState!=CURSOR_VALID ){
74758    return SQLITE_ABORT;
74759  }
74760
74761  /* Save the positions of all other cursors open on this table. This is
74762  ** required in case any of them are holding references to an xFetch
74763  ** version of the b-tree page modified by the accessPayload call below.
74764  **
74765  ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
74766  ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
74767  ** saveAllCursors can only return SQLITE_OK.
74768  */
74769  VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
74770  assert( rc==SQLITE_OK );
74771
74772  /* Check some assumptions:
74773  **   (a) the cursor is open for writing,
74774  **   (b) there is a read/write transaction open,
74775  **   (c) the connection holds a write-lock on the table (if required),
74776  **   (d) there are no conflicting read-locks, and
74777  **   (e) the cursor points at a valid row of an intKey table.
74778  */
74779  if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
74780    return SQLITE_READONLY;
74781  }
74782  assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
74783              && pCsr->pBt->inTransaction==TRANS_WRITE );
74784  assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
74785  assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
74786  assert( pCsr->pPage->intKey );
74787
74788  return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
74789}
74790
74791/*
74792** Mark this cursor as an incremental blob cursor.
74793*/
74794SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
74795  pCur->curFlags |= BTCF_Incrblob;
74796  pCur->pBtree->hasIncrblobCur = 1;
74797}
74798#endif
74799
74800/*
74801** Set both the "read version" (single byte at byte offset 18) and
74802** "write version" (single byte at byte offset 19) fields in the database
74803** header to iVersion.
74804*/
74805SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
74806  BtShared *pBt = pBtree->pBt;
74807  int rc;                         /* Return code */
74808
74809  assert( iVersion==1 || iVersion==2 );
74810
74811  /* If setting the version fields to 1, do not automatically open the
74812  ** WAL connection, even if the version fields are currently set to 2.
74813  */
74814  pBt->btsFlags &= ~BTS_NO_WAL;
74815  if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
74816
74817  rc = sqlite3BtreeBeginTrans(pBtree, 0, 0);
74818  if( rc==SQLITE_OK ){
74819    u8 *aData = pBt->pPage1->aData;
74820    if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
74821      rc = sqlite3BtreeBeginTrans(pBtree, 2, 0);
74822      if( rc==SQLITE_OK ){
74823        rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
74824        if( rc==SQLITE_OK ){
74825          aData[18] = (u8)iVersion;
74826          aData[19] = (u8)iVersion;
74827        }
74828      }
74829    }
74830  }
74831
74832  pBt->btsFlags &= ~BTS_NO_WAL;
74833  return rc;
74834}
74835
74836/*
74837** Return true if the cursor has a hint specified.  This routine is
74838** only used from within assert() statements
74839*/
74840SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
74841  return (pCsr->hints & mask)!=0;
74842}
74843
74844/*
74845** Return true if the given Btree is read-only.
74846*/
74847SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
74848  return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
74849}
74850
74851/*
74852** Return the size of the header added to each page by this module.
74853*/
74854SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
74855
74856#if !defined(SQLITE_OMIT_SHARED_CACHE)
74857/*
74858** Return true if the Btree passed as the only argument is sharable.
74859*/
74860SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
74861  return p->sharable;
74862}
74863
74864/*
74865** Return the number of connections to the BtShared object accessed by
74866** the Btree handle passed as the only argument. For private caches
74867** this is always 1. For shared caches it may be 1 or greater.
74868*/
74869SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree *p){
74870  testcase( p->sharable );
74871  return p->pBt->nRef;
74872}
74873#endif
74874
74875/************** End of btree.c ***********************************************/
74876/************** Begin file backup.c ******************************************/
74877/*
74878** 2009 January 28
74879**
74880** The author disclaims copyright to this source code.  In place of
74881** a legal notice, here is a blessing:
74882**
74883**    May you do good and not evil.
74884**    May you find forgiveness for yourself and forgive others.
74885**    May you share freely, never taking more than you give.
74886**
74887*************************************************************************
74888** This file contains the implementation of the sqlite3_backup_XXX()
74889** API functions and the related features.
74890*/
74891/* #include "sqliteInt.h" */
74892/* #include "btreeInt.h" */
74893
74894/*
74895** Structure allocated for each backup operation.
74896*/
74897struct sqlite3_backup {
74898  sqlite3* pDestDb;        /* Destination database handle */
74899  Btree *pDest;            /* Destination b-tree file */
74900  u32 iDestSchema;         /* Original schema cookie in destination */
74901  int bDestLocked;         /* True once a write-transaction is open on pDest */
74902
74903  Pgno iNext;              /* Page number of the next source page to copy */
74904  sqlite3* pSrcDb;         /* Source database handle */
74905  Btree *pSrc;             /* Source b-tree file */
74906
74907  int rc;                  /* Backup process error code */
74908
74909  /* These two variables are set by every call to backup_step(). They are
74910  ** read by calls to backup_remaining() and backup_pagecount().
74911  */
74912  Pgno nRemaining;         /* Number of pages left to copy */
74913  Pgno nPagecount;         /* Total number of pages to copy */
74914
74915  int isAttached;          /* True once backup has been registered with pager */
74916  sqlite3_backup *pNext;   /* Next backup associated with source pager */
74917};
74918
74919/*
74920** THREAD SAFETY NOTES:
74921**
74922**   Once it has been created using backup_init(), a single sqlite3_backup
74923**   structure may be accessed via two groups of thread-safe entry points:
74924**
74925**     * Via the sqlite3_backup_XXX() API function backup_step() and
74926**       backup_finish(). Both these functions obtain the source database
74927**       handle mutex and the mutex associated with the source BtShared
74928**       structure, in that order.
74929**
74930**     * Via the BackupUpdate() and BackupRestart() functions, which are
74931**       invoked by the pager layer to report various state changes in
74932**       the page cache associated with the source database. The mutex
74933**       associated with the source database BtShared structure will always
74934**       be held when either of these functions are invoked.
74935**
74936**   The other sqlite3_backup_XXX() API functions, backup_remaining() and
74937**   backup_pagecount() are not thread-safe functions. If they are called
74938**   while some other thread is calling backup_step() or backup_finish(),
74939**   the values returned may be invalid. There is no way for a call to
74940**   BackupUpdate() or BackupRestart() to interfere with backup_remaining()
74941**   or backup_pagecount().
74942**
74943**   Depending on the SQLite configuration, the database handles and/or
74944**   the Btree objects may have their own mutexes that require locking.
74945**   Non-sharable Btrees (in-memory databases for example), do not have
74946**   associated mutexes.
74947*/
74948
74949/*
74950** Return a pointer corresponding to database zDb (i.e. "main", "temp")
74951** in connection handle pDb. If such a database cannot be found, return
74952** a NULL pointer and write an error message to pErrorDb.
74953**
74954** If the "temp" database is requested, it may need to be opened by this
74955** function. If an error occurs while doing so, return 0 and write an
74956** error message to pErrorDb.
74957*/
74958static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
74959  int i = sqlite3FindDbName(pDb, zDb);
74960
74961  if( i==1 ){
74962    Parse sParse;
74963    int rc = 0;
74964    memset(&sParse, 0, sizeof(sParse));
74965    sParse.db = pDb;
74966    if( sqlite3OpenTempDatabase(&sParse) ){
74967      sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
74968      rc = SQLITE_ERROR;
74969    }
74970    sqlite3DbFree(pErrorDb, sParse.zErrMsg);
74971    sqlite3ParserReset(&sParse);
74972    if( rc ){
74973      return 0;
74974    }
74975  }
74976
74977  if( i<0 ){
74978    sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
74979    return 0;
74980  }
74981
74982  return pDb->aDb[i].pBt;
74983}
74984
74985/*
74986** Attempt to set the page size of the destination to match the page size
74987** of the source.
74988*/
74989static int setDestPgsz(sqlite3_backup *p){
74990  int rc;
74991  rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
74992  return rc;
74993}
74994
74995/*
74996** Check that there is no open read-transaction on the b-tree passed as the
74997** second argument. If there is not, return SQLITE_OK. Otherwise, if there
74998** is an open read-transaction, return SQLITE_ERROR and leave an error
74999** message in database handle db.
75000*/
75001static int checkReadTransaction(sqlite3 *db, Btree *p){
75002  if( sqlite3BtreeIsInReadTrans(p) ){
75003    sqlite3ErrorWithMsg(db, SQLITE_ERROR, "destination database is in use");
75004    return SQLITE_ERROR;
75005  }
75006  return SQLITE_OK;
75007}
75008
75009/*
75010** Create an sqlite3_backup process to copy the contents of zSrcDb from
75011** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
75012** a pointer to the new sqlite3_backup object.
75013**
75014** If an error occurs, NULL is returned and an error code and error message
75015** stored in database handle pDestDb.
75016*/
75017SQLITE_API sqlite3_backup *sqlite3_backup_init(
75018  sqlite3* pDestDb,                     /* Database to write to */
75019  const char *zDestDb,                  /* Name of database within pDestDb */
75020  sqlite3* pSrcDb,                      /* Database connection to read from */
75021  const char *zSrcDb                    /* Name of database within pSrcDb */
75022){
75023  sqlite3_backup *p;                    /* Value to return */
75024
75025#ifdef SQLITE_ENABLE_API_ARMOR
75026  if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
75027    (void)SQLITE_MISUSE_BKPT;
75028    return 0;
75029  }
75030#endif
75031
75032  /* Lock the source database handle. The destination database
75033  ** handle is not locked in this routine, but it is locked in
75034  ** sqlite3_backup_step(). The user is required to ensure that no
75035  ** other thread accesses the destination handle for the duration
75036  ** of the backup operation.  Any attempt to use the destination
75037  ** database connection while a backup is in progress may cause
75038  ** a malfunction or a deadlock.
75039  */
75040  sqlite3_mutex_enter(pSrcDb->mutex);
75041  sqlite3_mutex_enter(pDestDb->mutex);
75042
75043  if( pSrcDb==pDestDb ){
75044    sqlite3ErrorWithMsg(
75045        pDestDb, SQLITE_ERROR, "source and destination must be distinct"
75046    );
75047    p = 0;
75048  }else {
75049    /* Allocate space for a new sqlite3_backup object...
75050    ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
75051    ** call to sqlite3_backup_init() and is destroyed by a call to
75052    ** sqlite3_backup_finish(). */
75053    p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
75054    if( !p ){
75055      sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT);
75056    }
75057  }
75058
75059  /* If the allocation succeeded, populate the new object. */
75060  if( p ){
75061    p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
75062    p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
75063    p->pDestDb = pDestDb;
75064    p->pSrcDb = pSrcDb;
75065    p->iNext = 1;
75066    p->isAttached = 0;
75067
75068    if( 0==p->pSrc || 0==p->pDest
75069     || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
75070     ){
75071      /* One (or both) of the named databases did not exist or an OOM
75072      ** error was hit. Or there is a transaction open on the destination
75073      ** database. The error has already been written into the pDestDb
75074      ** handle. All that is left to do here is free the sqlite3_backup
75075      ** structure.  */
75076      sqlite3_free(p);
75077      p = 0;
75078    }
75079  }
75080  if( p ){
75081    p->pSrc->nBackup++;
75082  }
75083
75084  sqlite3_mutex_leave(pDestDb->mutex);
75085  sqlite3_mutex_leave(pSrcDb->mutex);
75086  return p;
75087}
75088
75089/*
75090** Argument rc is an SQLite error code. Return true if this error is
75091** considered fatal if encountered during a backup operation. All errors
75092** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
75093*/
75094static int isFatalError(int rc){
75095  return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
75096}
75097
75098/*
75099** Parameter zSrcData points to a buffer containing the data for
75100** page iSrcPg from the source database. Copy this data into the
75101** destination database.
75102*/
75103static int backupOnePage(
75104  sqlite3_backup *p,              /* Backup handle */
75105  Pgno iSrcPg,                    /* Source database page to backup */
75106  const u8 *zSrcData,             /* Source database page data */
75107  int bUpdate                     /* True for an update, false otherwise */
75108){
75109  Pager * const pDestPager = sqlite3BtreePager(p->pDest);
75110  const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
75111  int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
75112  const int nCopy = MIN(nSrcPgsz, nDestPgsz);
75113  const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
75114  int rc = SQLITE_OK;
75115  i64 iOff;
75116
75117  assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
75118  assert( p->bDestLocked );
75119  assert( !isFatalError(p->rc) );
75120  assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
75121  assert( zSrcData );
75122
75123  /* Catch the case where the destination is an in-memory database and the
75124  ** page sizes of the source and destination differ.
75125  */
75126  if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
75127    rc = SQLITE_READONLY;
75128  }
75129
75130  /* This loop runs once for each destination page spanned by the source
75131  ** page. For each iteration, variable iOff is set to the byte offset
75132  ** of the destination page.
75133  */
75134  for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
75135    DbPage *pDestPg = 0;
75136    Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
75137    if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
75138    if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg, 0))
75139     && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
75140    ){
75141      const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
75142      u8 *zDestData = sqlite3PagerGetData(pDestPg);
75143      u8 *zOut = &zDestData[iOff%nDestPgsz];
75144
75145      /* Copy the data from the source page into the destination page.
75146      ** Then clear the Btree layer MemPage.isInit flag. Both this module
75147      ** and the pager code use this trick (clearing the first byte
75148      ** of the page 'extra' space to invalidate the Btree layers
75149      ** cached parse of the page). MemPage.isInit is marked
75150      ** "MUST BE FIRST" for this purpose.
75151      */
75152      memcpy(zOut, zIn, nCopy);
75153      ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
75154      if( iOff==0 && bUpdate==0 ){
75155        sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
75156      }
75157    }
75158    sqlite3PagerUnref(pDestPg);
75159  }
75160
75161  return rc;
75162}
75163
75164/*
75165** If pFile is currently larger than iSize bytes, then truncate it to
75166** exactly iSize bytes. If pFile is not larger than iSize bytes, then
75167** this function is a no-op.
75168**
75169** Return SQLITE_OK if everything is successful, or an SQLite error
75170** code if an error occurs.
75171*/
75172static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
75173  i64 iCurrent;
75174  int rc = sqlite3OsFileSize(pFile, &iCurrent);
75175  if( rc==SQLITE_OK && iCurrent>iSize ){
75176    rc = sqlite3OsTruncate(pFile, iSize);
75177  }
75178  return rc;
75179}
75180
75181/*
75182** Register this backup object with the associated source pager for
75183** callbacks when pages are changed or the cache invalidated.
75184*/
75185static void attachBackupObject(sqlite3_backup *p){
75186  sqlite3_backup **pp;
75187  assert( sqlite3BtreeHoldsMutex(p->pSrc) );
75188  pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
75189  p->pNext = *pp;
75190  *pp = p;
75191  p->isAttached = 1;
75192}
75193
75194/*
75195** Copy nPage pages from the source b-tree to the destination.
75196*/
75197SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
75198  int rc;
75199  int destMode;       /* Destination journal mode */
75200  int pgszSrc = 0;    /* Source page size */
75201  int pgszDest = 0;   /* Destination page size */
75202
75203#ifdef SQLITE_ENABLE_API_ARMOR
75204  if( p==0 ) return SQLITE_MISUSE_BKPT;
75205#endif
75206  sqlite3_mutex_enter(p->pSrcDb->mutex);
75207  sqlite3BtreeEnter(p->pSrc);
75208  if( p->pDestDb ){
75209    sqlite3_mutex_enter(p->pDestDb->mutex);
75210  }
75211
75212  rc = p->rc;
75213  if( !isFatalError(rc) ){
75214    Pager * const pSrcPager = sqlite3BtreePager(p->pSrc);     /* Source pager */
75215    Pager * const pDestPager = sqlite3BtreePager(p->pDest);   /* Dest pager */
75216    int ii;                            /* Iterator variable */
75217    int nSrcPage = -1;                 /* Size of source db in pages */
75218    int bCloseTrans = 0;               /* True if src db requires unlocking */
75219
75220    /* If the source pager is currently in a write-transaction, return
75221    ** SQLITE_BUSY immediately.
75222    */
75223    if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
75224      rc = SQLITE_BUSY;
75225    }else{
75226      rc = SQLITE_OK;
75227    }
75228
75229    /* If there is no open read-transaction on the source database, open
75230    ** one now. If a transaction is opened here, then it will be closed
75231    ** before this function exits.
75232    */
75233    if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
75234      rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
75235      bCloseTrans = 1;
75236    }
75237
75238    /* If the destination database has not yet been locked (i.e. if this
75239    ** is the first call to backup_step() for the current backup operation),
75240    ** try to set its page size to the same as the source database. This
75241    ** is especially important on ZipVFS systems, as in that case it is
75242    ** not possible to create a database file that uses one page size by
75243    ** writing to it with another.  */
75244    if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
75245      rc = SQLITE_NOMEM;
75246    }
75247
75248    /* Lock the destination database, if it is not locked already. */
75249    if( SQLITE_OK==rc && p->bDestLocked==0
75250     && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
75251                                                (int*)&p->iDestSchema))
75252    ){
75253      p->bDestLocked = 1;
75254    }
75255
75256    /* Do not allow backup if the destination database is in WAL mode
75257    ** and the page sizes are different between source and destination */
75258    pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
75259    pgszDest = sqlite3BtreeGetPageSize(p->pDest);
75260    destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
75261    if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
75262      rc = SQLITE_READONLY;
75263    }
75264
75265    /* Now that there is a read-lock on the source database, query the
75266    ** source pager for the number of pages in the database.
75267    */
75268    nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
75269    assert( nSrcPage>=0 );
75270    for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
75271      const Pgno iSrcPg = p->iNext;                 /* Source page number */
75272      if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
75273        DbPage *pSrcPg;                             /* Source page object */
75274        rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg,PAGER_GET_READONLY);
75275        if( rc==SQLITE_OK ){
75276          rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
75277          sqlite3PagerUnref(pSrcPg);
75278        }
75279      }
75280      p->iNext++;
75281    }
75282    if( rc==SQLITE_OK ){
75283      p->nPagecount = nSrcPage;
75284      p->nRemaining = nSrcPage+1-p->iNext;
75285      if( p->iNext>(Pgno)nSrcPage ){
75286        rc = SQLITE_DONE;
75287      }else if( !p->isAttached ){
75288        attachBackupObject(p);
75289      }
75290    }
75291
75292    /* Update the schema version field in the destination database. This
75293    ** is to make sure that the schema-version really does change in
75294    ** the case where the source and destination databases have the
75295    ** same schema version.
75296    */
75297    if( rc==SQLITE_DONE ){
75298      if( nSrcPage==0 ){
75299        rc = sqlite3BtreeNewDb(p->pDest);
75300        nSrcPage = 1;
75301      }
75302      if( rc==SQLITE_OK || rc==SQLITE_DONE ){
75303        rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
75304      }
75305      if( rc==SQLITE_OK ){
75306        if( p->pDestDb ){
75307          sqlite3ResetAllSchemasOfConnection(p->pDestDb);
75308        }
75309        if( destMode==PAGER_JOURNALMODE_WAL ){
75310          rc = sqlite3BtreeSetVersion(p->pDest, 2);
75311        }
75312      }
75313      if( rc==SQLITE_OK ){
75314        int nDestTruncate;
75315        /* Set nDestTruncate to the final number of pages in the destination
75316        ** database. The complication here is that the destination page
75317        ** size may be different to the source page size.
75318        **
75319        ** If the source page size is smaller than the destination page size,
75320        ** round up. In this case the call to sqlite3OsTruncate() below will
75321        ** fix the size of the file. However it is important to call
75322        ** sqlite3PagerTruncateImage() here so that any pages in the
75323        ** destination file that lie beyond the nDestTruncate page mark are
75324        ** journalled by PagerCommitPhaseOne() before they are destroyed
75325        ** by the file truncation.
75326        */
75327        assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
75328        assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
75329        if( pgszSrc<pgszDest ){
75330          int ratio = pgszDest/pgszSrc;
75331          nDestTruncate = (nSrcPage+ratio-1)/ratio;
75332          if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
75333            nDestTruncate--;
75334          }
75335        }else{
75336          nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
75337        }
75338        assert( nDestTruncate>0 );
75339
75340        if( pgszSrc<pgszDest ){
75341          /* If the source page-size is smaller than the destination page-size,
75342          ** two extra things may need to happen:
75343          **
75344          **   * The destination may need to be truncated, and
75345          **
75346          **   * Data stored on the pages immediately following the
75347          **     pending-byte page in the source database may need to be
75348          **     copied into the destination database.
75349          */
75350          const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
75351          sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
75352          Pgno iPg;
75353          int nDstPage;
75354          i64 iOff;
75355          i64 iEnd;
75356
75357          assert( pFile );
75358          assert( nDestTruncate==0
75359              || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
75360                nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
75361             && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
75362          ));
75363
75364          /* This block ensures that all data required to recreate the original
75365          ** database has been stored in the journal for pDestPager and the
75366          ** journal synced to disk. So at this point we may safely modify
75367          ** the database file in any way, knowing that if a power failure
75368          ** occurs, the original database will be reconstructed from the
75369          ** journal file.  */
75370          sqlite3PagerPagecount(pDestPager, &nDstPage);
75371          for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
75372            if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
75373              DbPage *pPg;
75374              rc = sqlite3PagerGet(pDestPager, iPg, &pPg, 0);
75375              if( rc==SQLITE_OK ){
75376                rc = sqlite3PagerWrite(pPg);
75377                sqlite3PagerUnref(pPg);
75378              }
75379            }
75380          }
75381          if( rc==SQLITE_OK ){
75382            rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
75383          }
75384
75385          /* Write the extra pages and truncate the database file as required */
75386          iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
75387          for(
75388            iOff=PENDING_BYTE+pgszSrc;
75389            rc==SQLITE_OK && iOff<iEnd;
75390            iOff+=pgszSrc
75391          ){
75392            PgHdr *pSrcPg = 0;
75393            const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
75394            rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg, 0);
75395            if( rc==SQLITE_OK ){
75396              u8 *zData = sqlite3PagerGetData(pSrcPg);
75397              rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
75398            }
75399            sqlite3PagerUnref(pSrcPg);
75400          }
75401          if( rc==SQLITE_OK ){
75402            rc = backupTruncateFile(pFile, iSize);
75403          }
75404
75405          /* Sync the database file to disk. */
75406          if( rc==SQLITE_OK ){
75407            rc = sqlite3PagerSync(pDestPager, 0);
75408          }
75409        }else{
75410          sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
75411          rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
75412        }
75413
75414        /* Finish committing the transaction to the destination database. */
75415        if( SQLITE_OK==rc
75416         && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
75417        ){
75418          rc = SQLITE_DONE;
75419        }
75420      }
75421    }
75422
75423    /* If bCloseTrans is true, then this function opened a read transaction
75424    ** on the source database. Close the read transaction here. There is
75425    ** no need to check the return values of the btree methods here, as
75426    ** "committing" a read-only transaction cannot fail.
75427    */
75428    if( bCloseTrans ){
75429      TESTONLY( int rc2 );
75430      TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
75431      TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
75432      assert( rc2==SQLITE_OK );
75433    }
75434
75435    if( rc==SQLITE_IOERR_NOMEM ){
75436      rc = SQLITE_NOMEM_BKPT;
75437    }
75438    p->rc = rc;
75439  }
75440  if( p->pDestDb ){
75441    sqlite3_mutex_leave(p->pDestDb->mutex);
75442  }
75443  sqlite3BtreeLeave(p->pSrc);
75444  sqlite3_mutex_leave(p->pSrcDb->mutex);
75445  return rc;
75446}
75447
75448/*
75449** Release all resources associated with an sqlite3_backup* handle.
75450*/
75451SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
75452  sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
75453  sqlite3 *pSrcDb;                     /* Source database connection */
75454  int rc;                              /* Value to return */
75455
75456  /* Enter the mutexes */
75457  if( p==0 ) return SQLITE_OK;
75458  pSrcDb = p->pSrcDb;
75459  sqlite3_mutex_enter(pSrcDb->mutex);
75460  sqlite3BtreeEnter(p->pSrc);
75461  if( p->pDestDb ){
75462    sqlite3_mutex_enter(p->pDestDb->mutex);
75463  }
75464
75465  /* Detach this backup from the source pager. */
75466  if( p->pDestDb ){
75467    p->pSrc->nBackup--;
75468  }
75469  if( p->isAttached ){
75470    pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
75471    assert( pp!=0 );
75472    while( *pp!=p ){
75473      pp = &(*pp)->pNext;
75474      assert( pp!=0 );
75475    }
75476    *pp = p->pNext;
75477  }
75478
75479  /* If a transaction is still open on the Btree, roll it back. */
75480  sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
75481
75482  /* Set the error code of the destination database handle. */
75483  rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
75484  if( p->pDestDb ){
75485    sqlite3Error(p->pDestDb, rc);
75486
75487    /* Exit the mutexes and free the backup context structure. */
75488    sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
75489  }
75490  sqlite3BtreeLeave(p->pSrc);
75491  if( p->pDestDb ){
75492    /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
75493    ** call to sqlite3_backup_init() and is destroyed by a call to
75494    ** sqlite3_backup_finish(). */
75495    sqlite3_free(p);
75496  }
75497  sqlite3LeaveMutexAndCloseZombie(pSrcDb);
75498  return rc;
75499}
75500
75501/*
75502** Return the number of pages still to be backed up as of the most recent
75503** call to sqlite3_backup_step().
75504*/
75505SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
75506#ifdef SQLITE_ENABLE_API_ARMOR
75507  if( p==0 ){
75508    (void)SQLITE_MISUSE_BKPT;
75509    return 0;
75510  }
75511#endif
75512  return p->nRemaining;
75513}
75514
75515/*
75516** Return the total number of pages in the source database as of the most
75517** recent call to sqlite3_backup_step().
75518*/
75519SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
75520#ifdef SQLITE_ENABLE_API_ARMOR
75521  if( p==0 ){
75522    (void)SQLITE_MISUSE_BKPT;
75523    return 0;
75524  }
75525#endif
75526  return p->nPagecount;
75527}
75528
75529/*
75530** This function is called after the contents of page iPage of the
75531** source database have been modified. If page iPage has already been
75532** copied into the destination database, then the data written to the
75533** destination is now invalidated. The destination copy of iPage needs
75534** to be updated with the new data before the backup operation is
75535** complete.
75536**
75537** It is assumed that the mutex associated with the BtShared object
75538** corresponding to the source database is held when this function is
75539** called.
75540*/
75541static SQLITE_NOINLINE void backupUpdate(
75542  sqlite3_backup *p,
75543  Pgno iPage,
75544  const u8 *aData
75545){
75546  assert( p!=0 );
75547  do{
75548    assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
75549    if( !isFatalError(p->rc) && iPage<p->iNext ){
75550      /* The backup process p has already copied page iPage. But now it
75551      ** has been modified by a transaction on the source pager. Copy
75552      ** the new data into the backup.
75553      */
75554      int rc;
75555      assert( p->pDestDb );
75556      sqlite3_mutex_enter(p->pDestDb->mutex);
75557      rc = backupOnePage(p, iPage, aData, 1);
75558      sqlite3_mutex_leave(p->pDestDb->mutex);
75559      assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
75560      if( rc!=SQLITE_OK ){
75561        p->rc = rc;
75562      }
75563    }
75564  }while( (p = p->pNext)!=0 );
75565}
75566SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
75567  if( pBackup ) backupUpdate(pBackup, iPage, aData);
75568}
75569
75570/*
75571** Restart the backup process. This is called when the pager layer
75572** detects that the database has been modified by an external database
75573** connection. In this case there is no way of knowing which of the
75574** pages that have been copied into the destination database are still
75575** valid and which are not, so the entire process needs to be restarted.
75576**
75577** It is assumed that the mutex associated with the BtShared object
75578** corresponding to the source database is held when this function is
75579** called.
75580*/
75581SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
75582  sqlite3_backup *p;                   /* Iterator variable */
75583  for(p=pBackup; p; p=p->pNext){
75584    assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
75585    p->iNext = 1;
75586  }
75587}
75588
75589#ifndef SQLITE_OMIT_VACUUM
75590/*
75591** Copy the complete content of pBtFrom into pBtTo.  A transaction
75592** must be active for both files.
75593**
75594** The size of file pTo may be reduced by this operation. If anything
75595** goes wrong, the transaction on pTo is rolled back. If successful, the
75596** transaction is committed before returning.
75597*/
75598SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
75599  int rc;
75600  sqlite3_file *pFd;              /* File descriptor for database pTo */
75601  sqlite3_backup b;
75602  sqlite3BtreeEnter(pTo);
75603  sqlite3BtreeEnter(pFrom);
75604
75605  assert( sqlite3BtreeIsInTrans(pTo) );
75606  pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
75607  if( pFd->pMethods ){
75608    i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
75609    rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
75610    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
75611    if( rc ) goto copy_finished;
75612  }
75613
75614  /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
75615  ** to 0. This is used by the implementations of sqlite3_backup_step()
75616  ** and sqlite3_backup_finish() to detect that they are being called
75617  ** from this function, not directly by the user.
75618  */
75619  memset(&b, 0, sizeof(b));
75620  b.pSrcDb = pFrom->db;
75621  b.pSrc = pFrom;
75622  b.pDest = pTo;
75623  b.iNext = 1;
75624
75625  /* 0x7FFFFFFF is the hard limit for the number of pages in a database
75626  ** file. By passing this as the number of pages to copy to
75627  ** sqlite3_backup_step(), we can guarantee that the copy finishes
75628  ** within a single call (unless an error occurs). The assert() statement
75629  ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
75630  ** or an error code.  */
75631  sqlite3_backup_step(&b, 0x7FFFFFFF);
75632  assert( b.rc!=SQLITE_OK );
75633
75634  rc = sqlite3_backup_finish(&b);
75635  if( rc==SQLITE_OK ){
75636    pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
75637  }else{
75638    sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
75639  }
75640
75641  assert( sqlite3BtreeIsInTrans(pTo)==0 );
75642copy_finished:
75643  sqlite3BtreeLeave(pFrom);
75644  sqlite3BtreeLeave(pTo);
75645  return rc;
75646}
75647#endif /* SQLITE_OMIT_VACUUM */
75648
75649/************** End of backup.c **********************************************/
75650/************** Begin file vdbemem.c *****************************************/
75651/*
75652** 2004 May 26
75653**
75654** The author disclaims copyright to this source code.  In place of
75655** a legal notice, here is a blessing:
75656**
75657**    May you do good and not evil.
75658**    May you find forgiveness for yourself and forgive others.
75659**    May you share freely, never taking more than you give.
75660**
75661*************************************************************************
75662**
75663** This file contains code use to manipulate "Mem" structure.  A "Mem"
75664** stores a single value in the VDBE.  Mem is an opaque structure visible
75665** only within the VDBE.  Interface routines refer to a Mem using the
75666** name sqlite_value
75667*/
75668/* #include "sqliteInt.h" */
75669/* #include "vdbeInt.h" */
75670
75671/* True if X is a power of two.  0 is considered a power of two here.
75672** In other words, return true if X has at most one bit set.
75673*/
75674#define ISPOWEROF2(X)  (((X)&((X)-1))==0)
75675
75676#ifdef SQLITE_DEBUG
75677/*
75678** Check invariants on a Mem object.
75679**
75680** This routine is intended for use inside of assert() statements, like
75681** this:    assert( sqlite3VdbeCheckMemInvariants(pMem) );
75682*/
75683SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
75684  /* If MEM_Dyn is set then Mem.xDel!=0.
75685  ** Mem.xDel might not be initialized if MEM_Dyn is clear.
75686  */
75687  assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
75688
75689  /* MEM_Dyn may only be set if Mem.szMalloc==0.  In this way we
75690  ** ensure that if Mem.szMalloc>0 then it is safe to do
75691  ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
75692  ** That saves a few cycles in inner loops. */
75693  assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
75694
75695  /* Cannot have more than one of MEM_Int, MEM_Real, or MEM_IntReal */
75696  assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) );
75697
75698  if( p->flags & MEM_Null ){
75699    /* Cannot be both MEM_Null and some other type */
75700    assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
75701
75702    /* If MEM_Null is set, then either the value is a pure NULL (the usual
75703    ** case) or it is a pointer set using sqlite3_bind_pointer() or
75704    ** sqlite3_result_pointer().  If a pointer, then MEM_Term must also be
75705    ** set.
75706    */
75707    if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
75708      /* This is a pointer type.  There may be a flag to indicate what to
75709      ** do with the pointer. */
75710      assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
75711              ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
75712              ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
75713
75714      /* No other bits set */
75715      assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
75716                           |MEM_Dyn|MEM_Ephem|MEM_Static))==0 );
75717    }else{
75718      /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
75719      ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */
75720    }
75721  }else{
75722    /* The MEM_Cleared bit is only allowed on NULLs */
75723    assert( (p->flags & MEM_Cleared)==0 );
75724  }
75725
75726  /* The szMalloc field holds the correct memory allocation size */
75727  assert( p->szMalloc==0
75728       || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
75729
75730  /* If p holds a string or blob, the Mem.z must point to exactly
75731  ** one of the following:
75732  **
75733  **   (1) Memory in Mem.zMalloc and managed by the Mem object
75734  **   (2) Memory to be freed using Mem.xDel
75735  **   (3) An ephemeral string or blob
75736  **   (4) A static string or blob
75737  */
75738  if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
75739    assert(
75740      ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
75741      ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
75742      ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
75743      ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
75744    );
75745  }
75746  return 1;
75747}
75748#endif
75749
75750/*
75751** Render a Mem object which is one of MEM_Int, MEM_Real, or MEM_IntReal
75752** into a buffer.
75753*/
75754static void vdbeMemRenderNum(int sz, char *zBuf, Mem *p){
75755  StrAccum acc;
75756  assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) );
75757  sqlite3StrAccumInit(&acc, 0, zBuf, sz, 0);
75758  if( p->flags & MEM_Int ){
75759    sqlite3_str_appendf(&acc, "%lld", p->u.i);
75760  }else if( p->flags & MEM_IntReal ){
75761    sqlite3_str_appendf(&acc, "%!.15g", (double)p->u.i);
75762  }else{
75763    sqlite3_str_appendf(&acc, "%!.15g", p->u.r);
75764  }
75765  assert( acc.zText==zBuf && acc.mxAlloc<=0 );
75766  zBuf[acc.nChar] = 0; /* Fast version of sqlite3StrAccumFinish(&acc) */
75767}
75768
75769#ifdef SQLITE_DEBUG
75770/*
75771** Validity checks on pMem.  pMem holds a string.
75772**
75773** (1) Check that string value of pMem agrees with its integer or real value.
75774** (2) Check that the string is correctly zero terminated
75775**
75776** A single int or real value always converts to the same strings.  But
75777** many different strings can be converted into the same int or real.
75778** If a table contains a numeric value and an index is based on the
75779** corresponding string value, then it is important that the string be
75780** derived from the numeric value, not the other way around, to ensure
75781** that the index and table are consistent.  See ticket
75782** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
75783** an example.
75784**
75785** This routine looks at pMem to verify that if it has both a numeric
75786** representation and a string representation then the string rep has
75787** been derived from the numeric and not the other way around.  It returns
75788** true if everything is ok and false if there is a problem.
75789**
75790** This routine is for use inside of assert() statements only.
75791*/
75792SQLITE_PRIVATE int sqlite3VdbeMemValidStrRep(Mem *p){
75793  char zBuf[100];
75794  char *z;
75795  int i, j, incr;
75796  if( (p->flags & MEM_Str)==0 ) return 1;
75797  if( p->flags & MEM_Term ){
75798    /* Insure that the string is properly zero-terminated.  Pay particular
75799    ** attention to the case where p->n is odd */
75800    if( p->szMalloc>0 && p->z==p->zMalloc ){
75801      assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 );
75802      assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 );
75803    }
75804    assert( p->z[p->n]==0 );
75805    assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 );
75806    assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 );
75807  }
75808  if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1;
75809  vdbeMemRenderNum(sizeof(zBuf), zBuf, p);
75810  z = p->z;
75811  i = j = 0;
75812  incr = 1;
75813  if( p->enc!=SQLITE_UTF8 ){
75814    incr = 2;
75815    if( p->enc==SQLITE_UTF16BE ) z++;
75816  }
75817  while( zBuf[j] ){
75818    if( zBuf[j++]!=z[i] ) return 0;
75819    i += incr;
75820  }
75821  return 1;
75822}
75823#endif /* SQLITE_DEBUG */
75824
75825/*
75826** If pMem is an object with a valid string representation, this routine
75827** ensures the internal encoding for the string representation is
75828** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
75829**
75830** If pMem is not a string object, or the encoding of the string
75831** representation is already stored using the requested encoding, then this
75832** routine is a no-op.
75833**
75834** SQLITE_OK is returned if the conversion is successful (or not required).
75835** SQLITE_NOMEM may be returned if a malloc() fails during conversion
75836** between formats.
75837*/
75838SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
75839#ifndef SQLITE_OMIT_UTF16
75840  int rc;
75841#endif
75842  assert( !sqlite3VdbeMemIsRowSet(pMem) );
75843  assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
75844           || desiredEnc==SQLITE_UTF16BE );
75845  if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
75846    return SQLITE_OK;
75847  }
75848  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
75849#ifdef SQLITE_OMIT_UTF16
75850  return SQLITE_ERROR;
75851#else
75852
75853  /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
75854  ** then the encoding of the value may not have changed.
75855  */
75856  rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
75857  assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
75858  assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
75859  assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
75860  return rc;
75861#endif
75862}
75863
75864/*
75865** Make sure pMem->z points to a writable allocation of at least n bytes.
75866**
75867** If the bPreserve argument is true, then copy of the content of
75868** pMem->z into the new allocation.  pMem must be either a string or
75869** blob if bPreserve is true.  If bPreserve is false, any prior content
75870** in pMem->z is discarded.
75871*/
75872SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
75873  assert( sqlite3VdbeCheckMemInvariants(pMem) );
75874  assert( !sqlite3VdbeMemIsRowSet(pMem) );
75875  testcase( pMem->db==0 );
75876
75877  /* If the bPreserve flag is set to true, then the memory cell must already
75878  ** contain a valid string or blob value.  */
75879  assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
75880  testcase( bPreserve && pMem->z==0 );
75881
75882  assert( pMem->szMalloc==0
75883       || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
75884  if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
75885    if( pMem->db ){
75886      pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
75887    }else{
75888      pMem->zMalloc = sqlite3Realloc(pMem->z, n);
75889      if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
75890      pMem->z = pMem->zMalloc;
75891    }
75892    bPreserve = 0;
75893  }else{
75894    if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
75895    pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
75896  }
75897  if( pMem->zMalloc==0 ){
75898    sqlite3VdbeMemSetNull(pMem);
75899    pMem->z = 0;
75900    pMem->szMalloc = 0;
75901    return SQLITE_NOMEM_BKPT;
75902  }else{
75903    pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
75904  }
75905
75906  if( bPreserve && pMem->z ){
75907    assert( pMem->z!=pMem->zMalloc );
75908    memcpy(pMem->zMalloc, pMem->z, pMem->n);
75909  }
75910  if( (pMem->flags&MEM_Dyn)!=0 ){
75911    assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
75912    pMem->xDel((void *)(pMem->z));
75913  }
75914
75915  pMem->z = pMem->zMalloc;
75916  pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
75917  return SQLITE_OK;
75918}
75919
75920/*
75921** Change the pMem->zMalloc allocation to be at least szNew bytes.
75922** If pMem->zMalloc already meets or exceeds the requested size, this
75923** routine is a no-op.
75924**
75925** Any prior string or blob content in the pMem object may be discarded.
75926** The pMem->xDel destructor is called, if it exists.  Though MEM_Str
75927** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, MEM_IntReal,
75928** and MEM_Null values are preserved.
75929**
75930** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
75931** if unable to complete the resizing.
75932*/
75933SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
75934  assert( CORRUPT_DB || szNew>0 );
75935  assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
75936  if( pMem->szMalloc<szNew ){
75937    return sqlite3VdbeMemGrow(pMem, szNew, 0);
75938  }
75939  assert( (pMem->flags & MEM_Dyn)==0 );
75940  pMem->z = pMem->zMalloc;
75941  pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal);
75942  return SQLITE_OK;
75943}
75944
75945/*
75946** It is already known that pMem contains an unterminated string.
75947** Add the zero terminator.
75948**
75949** Three bytes of zero are added.  In this way, there is guaranteed
75950** to be a double-zero byte at an even byte boundary in order to
75951** terminate a UTF16 string, even if the initial size of the buffer
75952** is an odd number of bytes.
75953*/
75954static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
75955  if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
75956    return SQLITE_NOMEM_BKPT;
75957  }
75958  pMem->z[pMem->n] = 0;
75959  pMem->z[pMem->n+1] = 0;
75960  pMem->z[pMem->n+2] = 0;
75961  pMem->flags |= MEM_Term;
75962  return SQLITE_OK;
75963}
75964
75965/*
75966** Change pMem so that its MEM_Str or MEM_Blob value is stored in
75967** MEM.zMalloc, where it can be safely written.
75968**
75969** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
75970*/
75971SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
75972  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
75973  assert( !sqlite3VdbeMemIsRowSet(pMem) );
75974  if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
75975    if( ExpandBlob(pMem) ) return SQLITE_NOMEM;
75976    if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
75977      int rc = vdbeMemAddTerminator(pMem);
75978      if( rc ) return rc;
75979    }
75980  }
75981  pMem->flags &= ~MEM_Ephem;
75982#ifdef SQLITE_DEBUG
75983  pMem->pScopyFrom = 0;
75984#endif
75985
75986  return SQLITE_OK;
75987}
75988
75989/*
75990** If the given Mem* has a zero-filled tail, turn it into an ordinary
75991** blob stored in dynamically allocated space.
75992*/
75993#ifndef SQLITE_OMIT_INCRBLOB
75994SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
75995  int nByte;
75996  assert( pMem->flags & MEM_Zero );
75997  assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
75998  testcase( sqlite3_value_nochange(pMem) );
75999  assert( !sqlite3VdbeMemIsRowSet(pMem) );
76000  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76001
76002  /* Set nByte to the number of bytes required to store the expanded blob. */
76003  nByte = pMem->n + pMem->u.nZero;
76004  if( nByte<=0 ){
76005    if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
76006    nByte = 1;
76007  }
76008  if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
76009    return SQLITE_NOMEM_BKPT;
76010  }
76011
76012  memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
76013  pMem->n += pMem->u.nZero;
76014  pMem->flags &= ~(MEM_Zero|MEM_Term);
76015  return SQLITE_OK;
76016}
76017#endif
76018
76019/*
76020** Make sure the given Mem is \u0000 terminated.
76021*/
76022SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
76023  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76024  testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
76025  testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
76026  if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
76027    return SQLITE_OK;   /* Nothing to do */
76028  }else{
76029    return vdbeMemAddTerminator(pMem);
76030  }
76031}
76032
76033/*
76034** Add MEM_Str to the set of representations for the given Mem.  This
76035** routine is only called if pMem is a number of some kind, not a NULL
76036** or a BLOB.
76037**
76038** Existing representations MEM_Int, MEM_Real, or MEM_IntReal are invalidated
76039** if bForce is true but are retained if bForce is false.
76040**
76041** A MEM_Null value will never be passed to this function. This function is
76042** used for converting values to text for returning to the user (i.e. via
76043** sqlite3_value_text()), or for ensuring that values to be used as btree
76044** keys are strings. In the former case a NULL pointer is returned the
76045** user and the latter is an internal programming error.
76046*/
76047SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
76048  const int nByte = 32;
76049
76050  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76051  assert( !(pMem->flags&MEM_Zero) );
76052  assert( !(pMem->flags&(MEM_Str|MEM_Blob)) );
76053  assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) );
76054  assert( !sqlite3VdbeMemIsRowSet(pMem) );
76055  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
76056
76057
76058  if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
76059    pMem->enc = 0;
76060    return SQLITE_NOMEM_BKPT;
76061  }
76062
76063  vdbeMemRenderNum(nByte, pMem->z, pMem);
76064  assert( pMem->z!=0 );
76065  pMem->n = sqlite3Strlen30NN(pMem->z);
76066  pMem->enc = SQLITE_UTF8;
76067  pMem->flags |= MEM_Str|MEM_Term;
76068  if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
76069  sqlite3VdbeChangeEncoding(pMem, enc);
76070  return SQLITE_OK;
76071}
76072
76073/*
76074** Memory cell pMem contains the context of an aggregate function.
76075** This routine calls the finalize method for that function.  The
76076** result of the aggregate is stored back into pMem.
76077**
76078** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
76079** otherwise.
76080*/
76081SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
76082  sqlite3_context ctx;
76083  Mem t;
76084  assert( pFunc!=0 );
76085  assert( pFunc->xFinalize!=0 );
76086  assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
76087  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76088  memset(&ctx, 0, sizeof(ctx));
76089  memset(&t, 0, sizeof(t));
76090  t.flags = MEM_Null;
76091  t.db = pMem->db;
76092  ctx.pOut = &t;
76093  ctx.pMem = pMem;
76094  ctx.pFunc = pFunc;
76095  pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
76096  assert( (pMem->flags & MEM_Dyn)==0 );
76097  if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
76098  memcpy(pMem, &t, sizeof(t));
76099  return ctx.isError;
76100}
76101
76102/*
76103** Memory cell pAccum contains the context of an aggregate function.
76104** This routine calls the xValue method for that function and stores
76105** the results in memory cell pMem.
76106**
76107** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK
76108** otherwise.
76109*/
76110#ifndef SQLITE_OMIT_WINDOWFUNC
76111SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem *pAccum, Mem *pOut, FuncDef *pFunc){
76112  sqlite3_context ctx;
76113  assert( pFunc!=0 );
76114  assert( pFunc->xValue!=0 );
76115  assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
76116  assert( pAccum->db==0 || sqlite3_mutex_held(pAccum->db->mutex) );
76117  memset(&ctx, 0, sizeof(ctx));
76118  sqlite3VdbeMemSetNull(pOut);
76119  ctx.pOut = pOut;
76120  ctx.pMem = pAccum;
76121  ctx.pFunc = pFunc;
76122  pFunc->xValue(&ctx);
76123  return ctx.isError;
76124}
76125#endif /* SQLITE_OMIT_WINDOWFUNC */
76126
76127/*
76128** If the memory cell contains a value that must be freed by
76129** invoking the external callback in Mem.xDel, then this routine
76130** will free that value.  It also sets Mem.flags to MEM_Null.
76131**
76132** This is a helper routine for sqlite3VdbeMemSetNull() and
76133** for sqlite3VdbeMemRelease().  Use those other routines as the
76134** entry point for releasing Mem resources.
76135*/
76136static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){
76137  assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
76138  assert( VdbeMemDynamic(p) );
76139  if( p->flags&MEM_Agg ){
76140    sqlite3VdbeMemFinalize(p, p->u.pDef);
76141    assert( (p->flags & MEM_Agg)==0 );
76142    testcase( p->flags & MEM_Dyn );
76143  }
76144  if( p->flags&MEM_Dyn ){
76145    assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
76146    p->xDel((void *)p->z);
76147  }
76148  p->flags = MEM_Null;
76149}
76150
76151/*
76152** Release memory held by the Mem p, both external memory cleared
76153** by p->xDel and memory in p->zMalloc.
76154**
76155** This is a helper routine invoked by sqlite3VdbeMemRelease() in
76156** the unusual case where there really is memory in p that needs
76157** to be freed.
76158*/
76159static SQLITE_NOINLINE void vdbeMemClear(Mem *p){
76160  if( VdbeMemDynamic(p) ){
76161    vdbeMemClearExternAndSetNull(p);
76162  }
76163  if( p->szMalloc ){
76164    sqlite3DbFreeNN(p->db, p->zMalloc);
76165    p->szMalloc = 0;
76166  }
76167  p->z = 0;
76168}
76169
76170/*
76171** Release any memory resources held by the Mem.  Both the memory that is
76172** free by Mem.xDel and the Mem.zMalloc allocation are freed.
76173**
76174** Use this routine prior to clean up prior to abandoning a Mem, or to
76175** reset a Mem back to its minimum memory utilization.
76176**
76177** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
76178** prior to inserting new content into the Mem.
76179*/
76180SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
76181  assert( sqlite3VdbeCheckMemInvariants(p) );
76182  if( VdbeMemDynamic(p) || p->szMalloc ){
76183    vdbeMemClear(p);
76184  }
76185}
76186
76187/*
76188** Convert a 64-bit IEEE double into a 64-bit signed integer.
76189** If the double is out of range of a 64-bit signed integer then
76190** return the closest available 64-bit signed integer.
76191*/
76192static SQLITE_NOINLINE i64 doubleToInt64(double r){
76193#ifdef SQLITE_OMIT_FLOATING_POINT
76194  /* When floating-point is omitted, double and int64 are the same thing */
76195  return r;
76196#else
76197  /*
76198  ** Many compilers we encounter do not define constants for the
76199  ** minimum and maximum 64-bit integers, or they define them
76200  ** inconsistently.  And many do not understand the "LL" notation.
76201  ** So we define our own static constants here using nothing
76202  ** larger than a 32-bit integer constant.
76203  */
76204  static const i64 maxInt = LARGEST_INT64;
76205  static const i64 minInt = SMALLEST_INT64;
76206
76207  if( r<=(double)minInt ){
76208    return minInt;
76209  }else if( r>=(double)maxInt ){
76210    return maxInt;
76211  }else{
76212    return (i64)r;
76213  }
76214#endif
76215}
76216
76217/*
76218** Return some kind of integer value which is the best we can do
76219** at representing the value that *pMem describes as an integer.
76220** If pMem is an integer, then the value is exact.  If pMem is
76221** a floating-point then the value returned is the integer part.
76222** If pMem is a string or blob, then we make an attempt to convert
76223** it into an integer and return that.  If pMem represents an
76224** an SQL-NULL value, return 0.
76225**
76226** If pMem represents a string value, its encoding might be changed.
76227*/
76228static SQLITE_NOINLINE i64 memIntValue(Mem *pMem){
76229  i64 value = 0;
76230  sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
76231  return value;
76232}
76233SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
76234  int flags;
76235  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76236  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
76237  flags = pMem->flags;
76238  if( flags & (MEM_Int|MEM_IntReal) ){
76239    testcase( flags & MEM_IntReal );
76240    return pMem->u.i;
76241  }else if( flags & MEM_Real ){
76242    return doubleToInt64(pMem->u.r);
76243  }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
76244    return memIntValue(pMem);
76245  }else{
76246    return 0;
76247  }
76248}
76249
76250/*
76251** Return the best representation of pMem that we can get into a
76252** double.  If pMem is already a double or an integer, return its
76253** value.  If it is a string or blob, try to convert it to a double.
76254** If it is a NULL, return 0.0.
76255*/
76256static SQLITE_NOINLINE double memRealValue(Mem *pMem){
76257  /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
76258  double val = (double)0;
76259  sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
76260  return val;
76261}
76262SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
76263  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76264  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
76265  if( pMem->flags & MEM_Real ){
76266    return pMem->u.r;
76267  }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
76268    testcase( pMem->flags & MEM_IntReal );
76269    return (double)pMem->u.i;
76270  }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
76271    return memRealValue(pMem);
76272  }else{
76273    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
76274    return (double)0;
76275  }
76276}
76277
76278/*
76279** Return 1 if pMem represents true, and return 0 if pMem represents false.
76280** Return the value ifNull if pMem is NULL.
76281*/
76282SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
76283  testcase( pMem->flags & MEM_IntReal );
76284  if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0;
76285  if( pMem->flags & MEM_Null ) return ifNull;
76286  return sqlite3VdbeRealValue(pMem)!=0.0;
76287}
76288
76289/*
76290** The MEM structure is already a MEM_Real.  Try to also make it a
76291** MEM_Int if we can.
76292*/
76293SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
76294  i64 ix;
76295  assert( pMem->flags & MEM_Real );
76296  assert( !sqlite3VdbeMemIsRowSet(pMem) );
76297  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76298  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
76299
76300  ix = doubleToInt64(pMem->u.r);
76301
76302  /* Only mark the value as an integer if
76303  **
76304  **    (1) the round-trip conversion real->int->real is a no-op, and
76305  **    (2) The integer is neither the largest nor the smallest
76306  **        possible integer (ticket #3922)
76307  **
76308  ** The second and third terms in the following conditional enforces
76309  ** the second condition under the assumption that addition overflow causes
76310  ** values to wrap around.
76311  */
76312  if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
76313    pMem->u.i = ix;
76314    MemSetTypeFlag(pMem, MEM_Int);
76315  }
76316}
76317
76318/*
76319** Convert pMem to type integer.  Invalidate any prior representations.
76320*/
76321SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
76322  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76323  assert( !sqlite3VdbeMemIsRowSet(pMem) );
76324  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
76325
76326  pMem->u.i = sqlite3VdbeIntValue(pMem);
76327  MemSetTypeFlag(pMem, MEM_Int);
76328  return SQLITE_OK;
76329}
76330
76331/*
76332** Convert pMem so that it is of type MEM_Real.
76333** Invalidate any prior representations.
76334*/
76335SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
76336  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76337  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
76338
76339  pMem->u.r = sqlite3VdbeRealValue(pMem);
76340  MemSetTypeFlag(pMem, MEM_Real);
76341  return SQLITE_OK;
76342}
76343
76344/* Compare a floating point value to an integer.  Return true if the two
76345** values are the same within the precision of the floating point value.
76346**
76347** This function assumes that i was obtained by assignment from r1.
76348**
76349** For some versions of GCC on 32-bit machines, if you do the more obvious
76350** comparison of "r1==(double)i" you sometimes get an answer of false even
76351** though the r1 and (double)i values are bit-for-bit the same.
76352*/
76353SQLITE_PRIVATE int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
76354  double r2 = (double)i;
76355  return r1==0.0
76356      || (memcmp(&r1, &r2, sizeof(r1))==0
76357          && i >= -2251799813685248LL && i < 2251799813685248LL);
76358}
76359
76360/*
76361** Convert pMem so that it has type MEM_Real or MEM_Int.
76362** Invalidate any prior representations.
76363**
76364** Every effort is made to force the conversion, even if the input
76365** is a string that does not look completely like a number.  Convert
76366** as much of the string as we can and ignore the rest.
76367*/
76368SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
76369  testcase( pMem->flags & MEM_Int );
76370  testcase( pMem->flags & MEM_Real );
76371  testcase( pMem->flags & MEM_IntReal );
76372  testcase( pMem->flags & MEM_Null );
76373  if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){
76374    int rc;
76375    sqlite3_int64 ix;
76376    assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
76377    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76378    rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
76379    if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
76380     || sqlite3RealSameAsInt(pMem->u.r, (ix = (i64)pMem->u.r))
76381    ){
76382      pMem->u.i = ix;
76383      MemSetTypeFlag(pMem, MEM_Int);
76384    }else{
76385      MemSetTypeFlag(pMem, MEM_Real);
76386    }
76387  }
76388  assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 );
76389  pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
76390  return SQLITE_OK;
76391}
76392
76393/*
76394** Cast the datatype of the value in pMem according to the affinity
76395** "aff".  Casting is different from applying affinity in that a cast
76396** is forced.  In other words, the value is converted into the desired
76397** affinity even if that results in loss of data.  This routine is
76398** used (for example) to implement the SQL "cast()" operator.
76399*/
76400SQLITE_PRIVATE int sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
76401  if( pMem->flags & MEM_Null ) return SQLITE_OK;
76402  switch( aff ){
76403    case SQLITE_AFF_BLOB: {   /* Really a cast to BLOB */
76404      if( (pMem->flags & MEM_Blob)==0 ){
76405        sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
76406        assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
76407        if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
76408      }else{
76409        pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
76410      }
76411      break;
76412    }
76413    case SQLITE_AFF_NUMERIC: {
76414      sqlite3VdbeMemNumerify(pMem);
76415      break;
76416    }
76417    case SQLITE_AFF_INTEGER: {
76418      sqlite3VdbeMemIntegerify(pMem);
76419      break;
76420    }
76421    case SQLITE_AFF_REAL: {
76422      sqlite3VdbeMemRealify(pMem);
76423      break;
76424    }
76425    default: {
76426      assert( aff==SQLITE_AFF_TEXT );
76427      assert( MEM_Str==(MEM_Blob>>3) );
76428      pMem->flags |= (pMem->flags&MEM_Blob)>>3;
76429      sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
76430      assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
76431      pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
76432      return sqlite3VdbeChangeEncoding(pMem, encoding);
76433    }
76434  }
76435  return SQLITE_OK;
76436}
76437
76438/*
76439** Initialize bulk memory to be a consistent Mem object.
76440**
76441** The minimum amount of initialization feasible is performed.
76442*/
76443SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
76444  assert( (flags & ~MEM_TypeMask)==0 );
76445  pMem->flags = flags;
76446  pMem->db = db;
76447  pMem->szMalloc = 0;
76448}
76449
76450
76451/*
76452** Delete any previous value and set the value stored in *pMem to NULL.
76453**
76454** This routine calls the Mem.xDel destructor to dispose of values that
76455** require the destructor.  But it preserves the Mem.zMalloc memory allocation.
76456** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
76457** routine to invoke the destructor and deallocates Mem.zMalloc.
76458**
76459** Use this routine to reset the Mem prior to insert a new value.
76460**
76461** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
76462*/
76463SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
76464  if( VdbeMemDynamic(pMem) ){
76465    vdbeMemClearExternAndSetNull(pMem);
76466  }else{
76467    pMem->flags = MEM_Null;
76468  }
76469}
76470SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value *p){
76471  sqlite3VdbeMemSetNull((Mem*)p);
76472}
76473
76474/*
76475** Delete any previous value and set the value to be a BLOB of length
76476** n containing all zeros.
76477*/
76478SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
76479  sqlite3VdbeMemRelease(pMem);
76480  pMem->flags = MEM_Blob|MEM_Zero;
76481  pMem->n = 0;
76482  if( n<0 ) n = 0;
76483  pMem->u.nZero = n;
76484  pMem->enc = SQLITE_UTF8;
76485  pMem->z = 0;
76486}
76487
76488/*
76489** The pMem is known to contain content that needs to be destroyed prior
76490** to a value change.  So invoke the destructor, then set the value to
76491** a 64-bit integer.
76492*/
76493static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
76494  sqlite3VdbeMemSetNull(pMem);
76495  pMem->u.i = val;
76496  pMem->flags = MEM_Int;
76497}
76498
76499/*
76500** Delete any previous value and set the value stored in *pMem to val,
76501** manifest type INTEGER.
76502*/
76503SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
76504  if( VdbeMemDynamic(pMem) ){
76505    vdbeReleaseAndSetInt64(pMem, val);
76506  }else{
76507    pMem->u.i = val;
76508    pMem->flags = MEM_Int;
76509  }
76510}
76511
76512/* A no-op destructor */
76513SQLITE_PRIVATE void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
76514
76515/*
76516** Set the value stored in *pMem should already be a NULL.
76517** Also store a pointer to go with it.
76518*/
76519SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(
76520  Mem *pMem,
76521  void *pPtr,
76522  const char *zPType,
76523  void (*xDestructor)(void*)
76524){
76525  assert( pMem->flags==MEM_Null );
76526  pMem->u.zPType = zPType ? zPType : "";
76527  pMem->z = pPtr;
76528  pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
76529  pMem->eSubtype = 'p';
76530  pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
76531}
76532
76533#ifndef SQLITE_OMIT_FLOATING_POINT
76534/*
76535** Delete any previous value and set the value stored in *pMem to val,
76536** manifest type REAL.
76537*/
76538SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
76539  sqlite3VdbeMemSetNull(pMem);
76540  if( !sqlite3IsNaN(val) ){
76541    pMem->u.r = val;
76542    pMem->flags = MEM_Real;
76543  }
76544}
76545#endif
76546
76547#ifdef SQLITE_DEBUG
76548/*
76549** Return true if the Mem holds a RowSet object.  This routine is intended
76550** for use inside of assert() statements.
76551*/
76552SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem *pMem){
76553  return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
76554         && pMem->xDel==sqlite3RowSetDelete;
76555}
76556#endif
76557
76558/*
76559** Delete any previous value and set the value of pMem to be an
76560** empty boolean index.
76561**
76562** Return SQLITE_OK on success and SQLITE_NOMEM if a memory allocation
76563** error occurs.
76564*/
76565SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem *pMem){
76566  sqlite3 *db = pMem->db;
76567  RowSet *p;
76568  assert( db!=0 );
76569  assert( !sqlite3VdbeMemIsRowSet(pMem) );
76570  sqlite3VdbeMemRelease(pMem);
76571  p = sqlite3RowSetInit(db);
76572  if( p==0 ) return SQLITE_NOMEM;
76573  pMem->z = (char*)p;
76574  pMem->flags = MEM_Blob|MEM_Dyn;
76575  pMem->xDel = sqlite3RowSetDelete;
76576  return SQLITE_OK;
76577}
76578
76579/*
76580** Return true if the Mem object contains a TEXT or BLOB that is
76581** too large - whose size exceeds SQLITE_MAX_LENGTH.
76582*/
76583SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
76584  assert( p->db!=0 );
76585  if( p->flags & (MEM_Str|MEM_Blob) ){
76586    int n = p->n;
76587    if( p->flags & MEM_Zero ){
76588      n += p->u.nZero;
76589    }
76590    return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
76591  }
76592  return 0;
76593}
76594
76595#ifdef SQLITE_DEBUG
76596/*
76597** This routine prepares a memory cell for modification by breaking
76598** its link to a shallow copy and by marking any current shallow
76599** copies of this cell as invalid.
76600**
76601** This is used for testing and debugging only - to help ensure that shallow
76602** copies (created by OP_SCopy) are not misused.
76603*/
76604SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
76605  int i;
76606  Mem *pX;
76607  for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
76608    if( pX->pScopyFrom==pMem ){
76609      u16 mFlags;
76610      if( pVdbe->db->flags & SQLITE_VdbeTrace ){
76611        sqlite3DebugPrintf("Invalidate R[%d] due to change in R[%d]\n",
76612          (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
76613      }
76614      /* If pX is marked as a shallow copy of pMem, then try to verify that
76615      ** no significant changes have been made to pX since the OP_SCopy.
76616      ** A significant change would indicated a missed call to this
76617      ** function for pX.  Minor changes, such as adding or removing a
76618      ** dual type, are allowed, as long as the underlying value is the
76619      ** same. */
76620      mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
76621      assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
76622
76623      /* pMem is the register that is changing.  But also mark pX as
76624      ** undefined so that we can quickly detect the shallow-copy error */
76625      pX->flags = MEM_Undefined;
76626      pX->pScopyFrom = 0;
76627    }
76628  }
76629  pMem->pScopyFrom = 0;
76630}
76631#endif /* SQLITE_DEBUG */
76632
76633/*
76634** Make an shallow copy of pFrom into pTo.  Prior contents of
76635** pTo are freed.  The pFrom->z field is not duplicated.  If
76636** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
76637** and flags gets srcType (either MEM_Ephem or MEM_Static).
76638*/
76639static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
76640  vdbeMemClearExternAndSetNull(pTo);
76641  assert( !VdbeMemDynamic(pTo) );
76642  sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
76643}
76644SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
76645  assert( !sqlite3VdbeMemIsRowSet(pFrom) );
76646  assert( pTo->db==pFrom->db );
76647  if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
76648  memcpy(pTo, pFrom, MEMCELLSIZE);
76649  if( (pFrom->flags&MEM_Static)==0 ){
76650    pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
76651    assert( srcType==MEM_Ephem || srcType==MEM_Static );
76652    pTo->flags |= srcType;
76653  }
76654}
76655
76656/*
76657** Make a full copy of pFrom into pTo.  Prior contents of pTo are
76658** freed before the copy is made.
76659*/
76660SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
76661  int rc = SQLITE_OK;
76662
76663  assert( !sqlite3VdbeMemIsRowSet(pFrom) );
76664  if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
76665  memcpy(pTo, pFrom, MEMCELLSIZE);
76666  pTo->flags &= ~MEM_Dyn;
76667  if( pTo->flags&(MEM_Str|MEM_Blob) ){
76668    if( 0==(pFrom->flags&MEM_Static) ){
76669      pTo->flags |= MEM_Ephem;
76670      rc = sqlite3VdbeMemMakeWriteable(pTo);
76671    }
76672  }
76673
76674  return rc;
76675}
76676
76677/*
76678** Transfer the contents of pFrom to pTo. Any existing value in pTo is
76679** freed. If pFrom contains ephemeral data, a copy is made.
76680**
76681** pFrom contains an SQL NULL when this routine returns.
76682*/
76683SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
76684  assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
76685  assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
76686  assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
76687
76688  sqlite3VdbeMemRelease(pTo);
76689  memcpy(pTo, pFrom, sizeof(Mem));
76690  pFrom->flags = MEM_Null;
76691  pFrom->szMalloc = 0;
76692}
76693
76694/*
76695** Change the value of a Mem to be a string or a BLOB.
76696**
76697** The memory management strategy depends on the value of the xDel
76698** parameter. If the value passed is SQLITE_TRANSIENT, then the
76699** string is copied into a (possibly existing) buffer managed by the
76700** Mem structure. Otherwise, any existing buffer is freed and the
76701** pointer copied.
76702**
76703** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
76704** size limit) then no memory allocation occurs.  If the string can be
76705** stored without allocating memory, then it is.  If a memory allocation
76706** is required to store the string, then value of pMem is unchanged.  In
76707** either case, SQLITE_TOOBIG is returned.
76708*/
76709SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
76710  Mem *pMem,          /* Memory cell to set to string value */
76711  const char *z,      /* String pointer */
76712  int n,              /* Bytes in string, or negative */
76713  u8 enc,             /* Encoding of z.  0 for BLOBs */
76714  void (*xDel)(void*) /* Destructor function */
76715){
76716  int nByte = n;      /* New value for pMem->n */
76717  int iLimit;         /* Maximum allowed string or blob size */
76718  u16 flags = 0;      /* New value for pMem->flags */
76719
76720  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76721  assert( !sqlite3VdbeMemIsRowSet(pMem) );
76722
76723  /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
76724  if( !z ){
76725    sqlite3VdbeMemSetNull(pMem);
76726    return SQLITE_OK;
76727  }
76728
76729  if( pMem->db ){
76730    iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
76731  }else{
76732    iLimit = SQLITE_MAX_LENGTH;
76733  }
76734  flags = (enc==0?MEM_Blob:MEM_Str);
76735  if( nByte<0 ){
76736    assert( enc!=0 );
76737    if( enc==SQLITE_UTF8 ){
76738      nByte = 0x7fffffff & (int)strlen(z);
76739    }else{
76740      for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
76741    }
76742    flags |= MEM_Term;
76743  }
76744
76745  /* The following block sets the new values of Mem.z and Mem.xDel. It
76746  ** also sets a flag in local variable "flags" to indicate the memory
76747  ** management (one of MEM_Dyn or MEM_Static).
76748  */
76749  if( xDel==SQLITE_TRANSIENT ){
76750    u32 nAlloc = nByte;
76751    if( flags&MEM_Term ){
76752      nAlloc += (enc==SQLITE_UTF8?1:2);
76753    }
76754    if( nByte>iLimit ){
76755      return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
76756    }
76757    testcase( nAlloc==0 );
76758    testcase( nAlloc==31 );
76759    testcase( nAlloc==32 );
76760    if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)) ){
76761      return SQLITE_NOMEM_BKPT;
76762    }
76763    memcpy(pMem->z, z, nAlloc);
76764  }else{
76765    sqlite3VdbeMemRelease(pMem);
76766    pMem->z = (char *)z;
76767    if( xDel==SQLITE_DYNAMIC ){
76768      pMem->zMalloc = pMem->z;
76769      pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
76770    }else{
76771      pMem->xDel = xDel;
76772      flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
76773    }
76774  }
76775
76776  pMem->n = nByte;
76777  pMem->flags = flags;
76778  if( enc ){
76779    pMem->enc = enc;
76780#ifdef SQLITE_ENABLE_SESSION
76781  }else if( pMem->db==0 ){
76782    pMem->enc = SQLITE_UTF8;
76783#endif
76784  }else{
76785    assert( pMem->db!=0 );
76786    pMem->enc = ENC(pMem->db);
76787  }
76788
76789#ifndef SQLITE_OMIT_UTF16
76790  if( enc>SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
76791    return SQLITE_NOMEM_BKPT;
76792  }
76793#endif
76794
76795  if( nByte>iLimit ){
76796    return SQLITE_TOOBIG;
76797  }
76798
76799  return SQLITE_OK;
76800}
76801
76802/*
76803** Move data out of a btree key or data field and into a Mem structure.
76804** The data is payload from the entry that pCur is currently pointing
76805** to.  offset and amt determine what portion of the data or key to retrieve.
76806** The result is written into the pMem element.
76807**
76808** The pMem object must have been initialized.  This routine will use
76809** pMem->zMalloc to hold the content from the btree, if possible.  New
76810** pMem->zMalloc space will be allocated if necessary.  The calling routine
76811** is responsible for making sure that the pMem object is eventually
76812** destroyed.
76813**
76814** If this routine fails for any reason (malloc returns NULL or unable
76815** to read from the disk) then the pMem is left in an inconsistent state.
76816*/
76817SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
76818  BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
76819  u32 offset,       /* Offset from the start of data to return bytes from. */
76820  u32 amt,          /* Number of bytes to return. */
76821  Mem *pMem         /* OUT: Return data in this Mem structure. */
76822){
76823  int rc;
76824  pMem->flags = MEM_Null;
76825  if( sqlite3BtreeMaxRecordSize(pCur)<offset+amt ){
76826    return SQLITE_CORRUPT_BKPT;
76827  }
76828  if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){
76829    rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
76830    if( rc==SQLITE_OK ){
76831      pMem->z[amt] = 0;   /* Overrun area used when reading malformed records */
76832      pMem->flags = MEM_Blob;
76833      pMem->n = (int)amt;
76834    }else{
76835      sqlite3VdbeMemRelease(pMem);
76836    }
76837  }
76838  return rc;
76839}
76840SQLITE_PRIVATE int sqlite3VdbeMemFromBtreeZeroOffset(
76841  BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
76842  u32 amt,          /* Number of bytes to return. */
76843  Mem *pMem         /* OUT: Return data in this Mem structure. */
76844){
76845  u32 available = 0;  /* Number of bytes available on the local btree page */
76846  int rc = SQLITE_OK; /* Return code */
76847
76848  assert( sqlite3BtreeCursorIsValid(pCur) );
76849  assert( !VdbeMemDynamic(pMem) );
76850
76851  /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
76852  ** that both the BtShared and database handle mutexes are held. */
76853  assert( !sqlite3VdbeMemIsRowSet(pMem) );
76854  pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, &available);
76855  assert( pMem->z!=0 );
76856
76857  if( amt<=available ){
76858    pMem->flags = MEM_Blob|MEM_Ephem;
76859    pMem->n = (int)amt;
76860  }else{
76861    rc = sqlite3VdbeMemFromBtree(pCur, 0, amt, pMem);
76862  }
76863
76864  return rc;
76865}
76866
76867/*
76868** The pVal argument is known to be a value other than NULL.
76869** Convert it into a string with encoding enc and return a pointer
76870** to a zero-terminated version of that string.
76871*/
76872static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
76873  assert( pVal!=0 );
76874  assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
76875  assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
76876  assert( !sqlite3VdbeMemIsRowSet(pVal) );
76877  assert( (pVal->flags & (MEM_Null))==0 );
76878  if( pVal->flags & (MEM_Blob|MEM_Str) ){
76879    if( ExpandBlob(pVal) ) return 0;
76880    pVal->flags |= MEM_Str;
76881    if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
76882      sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
76883    }
76884    if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
76885      assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
76886      if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
76887        return 0;
76888      }
76889    }
76890    sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
76891  }else{
76892    sqlite3VdbeMemStringify(pVal, enc, 0);
76893    assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
76894  }
76895  assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
76896              || pVal->db->mallocFailed );
76897  if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
76898    assert( sqlite3VdbeMemValidStrRep(pVal) );
76899    return pVal->z;
76900  }else{
76901    return 0;
76902  }
76903}
76904
76905/* This function is only available internally, it is not part of the
76906** external API. It works in a similar way to sqlite3_value_text(),
76907** except the data returned is in the encoding specified by the second
76908** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
76909** SQLITE_UTF8.
76910**
76911** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
76912** If that is the case, then the result must be aligned on an even byte
76913** boundary.
76914*/
76915SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
76916  if( !pVal ) return 0;
76917  assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
76918  assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
76919  assert( !sqlite3VdbeMemIsRowSet(pVal) );
76920  if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
76921    assert( sqlite3VdbeMemValidStrRep(pVal) );
76922    return pVal->z;
76923  }
76924  if( pVal->flags&MEM_Null ){
76925    return 0;
76926  }
76927  return valueToText(pVal, enc);
76928}
76929
76930/*
76931** Create a new sqlite3_value object.
76932*/
76933SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
76934  Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
76935  if( p ){
76936    p->flags = MEM_Null;
76937    p->db = db;
76938  }
76939  return p;
76940}
76941
76942/*
76943** Context object passed by sqlite3Stat4ProbeSetValue() through to
76944** valueNew(). See comments above valueNew() for details.
76945*/
76946struct ValueNewStat4Ctx {
76947  Parse *pParse;
76948  Index *pIdx;
76949  UnpackedRecord **ppRec;
76950  int iVal;
76951};
76952
76953/*
76954** Allocate and return a pointer to a new sqlite3_value object. If
76955** the second argument to this function is NULL, the object is allocated
76956** by calling sqlite3ValueNew().
76957**
76958** Otherwise, if the second argument is non-zero, then this function is
76959** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
76960** already been allocated, allocate the UnpackedRecord structure that
76961** that function will return to its caller here. Then return a pointer to
76962** an sqlite3_value within the UnpackedRecord.a[] array.
76963*/
76964static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
76965#ifdef SQLITE_ENABLE_STAT4
76966  if( p ){
76967    UnpackedRecord *pRec = p->ppRec[0];
76968
76969    if( pRec==0 ){
76970      Index *pIdx = p->pIdx;      /* Index being probed */
76971      int nByte;                  /* Bytes of space to allocate */
76972      int i;                      /* Counter variable */
76973      int nCol = pIdx->nColumn;   /* Number of index columns including rowid */
76974
76975      nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord));
76976      pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
76977      if( pRec ){
76978        pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
76979        if( pRec->pKeyInfo ){
76980          assert( pRec->pKeyInfo->nAllField==nCol );
76981          assert( pRec->pKeyInfo->enc==ENC(db) );
76982          pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
76983          for(i=0; i<nCol; i++){
76984            pRec->aMem[i].flags = MEM_Null;
76985            pRec->aMem[i].db = db;
76986          }
76987        }else{
76988          sqlite3DbFreeNN(db, pRec);
76989          pRec = 0;
76990        }
76991      }
76992      if( pRec==0 ) return 0;
76993      p->ppRec[0] = pRec;
76994    }
76995
76996    pRec->nField = p->iVal+1;
76997    return &pRec->aMem[p->iVal];
76998  }
76999#else
77000  UNUSED_PARAMETER(p);
77001#endif /* defined(SQLITE_ENABLE_STAT4) */
77002  return sqlite3ValueNew(db);
77003}
77004
77005/*
77006** The expression object indicated by the second argument is guaranteed
77007** to be a scalar SQL function. If
77008**
77009**   * all function arguments are SQL literals,
77010**   * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and
77011**   * the SQLITE_FUNC_NEEDCOLL function flag is not set,
77012**
77013** then this routine attempts to invoke the SQL function. Assuming no
77014** error occurs, output parameter (*ppVal) is set to point to a value
77015** object containing the result before returning SQLITE_OK.
77016**
77017** Affinity aff is applied to the result of the function before returning.
77018** If the result is a text value, the sqlite3_value object uses encoding
77019** enc.
77020**
77021** If the conditions above are not met, this function returns SQLITE_OK
77022** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
77023** NULL and an SQLite error code returned.
77024*/
77025#ifdef SQLITE_ENABLE_STAT4
77026static int valueFromFunction(
77027  sqlite3 *db,                    /* The database connection */
77028  Expr *p,                        /* The expression to evaluate */
77029  u8 enc,                         /* Encoding to use */
77030  u8 aff,                         /* Affinity to use */
77031  sqlite3_value **ppVal,          /* Write the new value here */
77032  struct ValueNewStat4Ctx *pCtx   /* Second argument for valueNew() */
77033){
77034  sqlite3_context ctx;            /* Context object for function invocation */
77035  sqlite3_value **apVal = 0;      /* Function arguments */
77036  int nVal = 0;                   /* Size of apVal[] array */
77037  FuncDef *pFunc = 0;             /* Function definition */
77038  sqlite3_value *pVal = 0;        /* New value */
77039  int rc = SQLITE_OK;             /* Return code */
77040  ExprList *pList = 0;            /* Function arguments */
77041  int i;                          /* Iterator variable */
77042
77043  assert( pCtx!=0 );
77044  assert( (p->flags & EP_TokenOnly)==0 );
77045  pList = p->x.pList;
77046  if( pList ) nVal = pList->nExpr;
77047  pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
77048  assert( pFunc );
77049  if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
77050   || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
77051  ){
77052    return SQLITE_OK;
77053  }
77054
77055  if( pList ){
77056    apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
77057    if( apVal==0 ){
77058      rc = SQLITE_NOMEM_BKPT;
77059      goto value_from_function_out;
77060    }
77061    for(i=0; i<nVal; i++){
77062      rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
77063      if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out;
77064    }
77065  }
77066
77067  pVal = valueNew(db, pCtx);
77068  if( pVal==0 ){
77069    rc = SQLITE_NOMEM_BKPT;
77070    goto value_from_function_out;
77071  }
77072
77073  assert( pCtx->pParse->rc==SQLITE_OK );
77074  memset(&ctx, 0, sizeof(ctx));
77075  ctx.pOut = pVal;
77076  ctx.pFunc = pFunc;
77077  pFunc->xSFunc(&ctx, nVal, apVal);
77078  if( ctx.isError ){
77079    rc = ctx.isError;
77080    sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
77081  }else{
77082    sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8);
77083    assert( rc==SQLITE_OK );
77084    rc = sqlite3VdbeChangeEncoding(pVal, enc);
77085    if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){
77086      rc = SQLITE_TOOBIG;
77087      pCtx->pParse->nErr++;
77088    }
77089  }
77090  pCtx->pParse->rc = rc;
77091
77092 value_from_function_out:
77093  if( rc!=SQLITE_OK ){
77094    pVal = 0;
77095  }
77096  if( apVal ){
77097    for(i=0; i<nVal; i++){
77098      sqlite3ValueFree(apVal[i]);
77099    }
77100    sqlite3DbFreeNN(db, apVal);
77101  }
77102
77103  *ppVal = pVal;
77104  return rc;
77105}
77106#else
77107# define valueFromFunction(a,b,c,d,e,f) SQLITE_OK
77108#endif /* defined(SQLITE_ENABLE_STAT4) */
77109
77110/*
77111** Extract a value from the supplied expression in the manner described
77112** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
77113** using valueNew().
77114**
77115** If pCtx is NULL and an error occurs after the sqlite3_value object
77116** has been allocated, it is freed before returning. Or, if pCtx is not
77117** NULL, it is assumed that the caller will free any allocated object
77118** in all cases.
77119*/
77120static int valueFromExpr(
77121  sqlite3 *db,                    /* The database connection */
77122  Expr *pExpr,                    /* The expression to evaluate */
77123  u8 enc,                         /* Encoding to use */
77124  u8 affinity,                    /* Affinity to use */
77125  sqlite3_value **ppVal,          /* Write the new value here */
77126  struct ValueNewStat4Ctx *pCtx   /* Second argument for valueNew() */
77127){
77128  int op;
77129  char *zVal = 0;
77130  sqlite3_value *pVal = 0;
77131  int negInt = 1;
77132  const char *zNeg = "";
77133  int rc = SQLITE_OK;
77134
77135  assert( pExpr!=0 );
77136  while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
77137#if defined(SQLITE_ENABLE_STAT4)
77138  if( op==TK_REGISTER ) op = pExpr->op2;
77139#else
77140  if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
77141#endif
77142
77143  /* Compressed expressions only appear when parsing the DEFAULT clause
77144  ** on a table column definition, and hence only when pCtx==0.  This
77145  ** check ensures that an EP_TokenOnly expression is never passed down
77146  ** into valueFromFunction(). */
77147  assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
77148
77149  if( op==TK_CAST ){
77150    u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
77151    rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
77152    testcase( rc!=SQLITE_OK );
77153    if( *ppVal ){
77154      sqlite3VdbeMemCast(*ppVal, aff, SQLITE_UTF8);
77155      sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF8);
77156    }
77157    return rc;
77158  }
77159
77160  /* Handle negative integers in a single step.  This is needed in the
77161  ** case when the value is -9223372036854775808.
77162  */
77163  if( op==TK_UMINUS
77164   && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
77165    pExpr = pExpr->pLeft;
77166    op = pExpr->op;
77167    negInt = -1;
77168    zNeg = "-";
77169  }
77170
77171  if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
77172    pVal = valueNew(db, pCtx);
77173    if( pVal==0 ) goto no_mem;
77174    if( ExprHasProperty(pExpr, EP_IntValue) ){
77175      sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
77176    }else{
77177      zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
77178      if( zVal==0 ) goto no_mem;
77179      sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
77180    }
77181    if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
77182      sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
77183    }else{
77184      sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
77185    }
77186    assert( (pVal->flags & MEM_IntReal)==0 );
77187    if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){
77188      testcase( pVal->flags & MEM_Int );
77189      testcase( pVal->flags & MEM_Real );
77190      pVal->flags &= ~MEM_Str;
77191    }
77192    if( enc!=SQLITE_UTF8 ){
77193      rc = sqlite3VdbeChangeEncoding(pVal, enc);
77194    }
77195  }else if( op==TK_UMINUS ) {
77196    /* This branch happens for multiple negative signs.  Ex: -(-5) */
77197    if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
77198     && pVal!=0
77199    ){
77200      sqlite3VdbeMemNumerify(pVal);
77201      if( pVal->flags & MEM_Real ){
77202        pVal->u.r = -pVal->u.r;
77203      }else if( pVal->u.i==SMALLEST_INT64 ){
77204#ifndef SQLITE_OMIT_FLOATING_POINT
77205        pVal->u.r = -(double)SMALLEST_INT64;
77206#else
77207        pVal->u.r = LARGEST_INT64;
77208#endif
77209        MemSetTypeFlag(pVal, MEM_Real);
77210      }else{
77211        pVal->u.i = -pVal->u.i;
77212      }
77213      sqlite3ValueApplyAffinity(pVal, affinity, enc);
77214    }
77215  }else if( op==TK_NULL ){
77216    pVal = valueNew(db, pCtx);
77217    if( pVal==0 ) goto no_mem;
77218    sqlite3VdbeMemSetNull(pVal);
77219  }
77220#ifndef SQLITE_OMIT_BLOB_LITERAL
77221  else if( op==TK_BLOB ){
77222    int nVal;
77223    assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
77224    assert( pExpr->u.zToken[1]=='\'' );
77225    pVal = valueNew(db, pCtx);
77226    if( !pVal ) goto no_mem;
77227    zVal = &pExpr->u.zToken[2];
77228    nVal = sqlite3Strlen30(zVal)-1;
77229    assert( zVal[nVal]=='\'' );
77230    sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
77231                         0, SQLITE_DYNAMIC);
77232  }
77233#endif
77234#ifdef SQLITE_ENABLE_STAT4
77235  else if( op==TK_FUNCTION && pCtx!=0 ){
77236    rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
77237  }
77238#endif
77239  else if( op==TK_TRUEFALSE ){
77240    pVal = valueNew(db, pCtx);
77241    if( pVal ){
77242      pVal->flags = MEM_Int;
77243      pVal->u.i = pExpr->u.zToken[4]==0;
77244    }
77245  }
77246
77247  *ppVal = pVal;
77248  return rc;
77249
77250no_mem:
77251#ifdef SQLITE_ENABLE_STAT4
77252  if( pCtx==0 || pCtx->pParse->nErr==0 )
77253#endif
77254    sqlite3OomFault(db);
77255  sqlite3DbFree(db, zVal);
77256  assert( *ppVal==0 );
77257#ifdef SQLITE_ENABLE_STAT4
77258  if( pCtx==0 ) sqlite3ValueFree(pVal);
77259#else
77260  assert( pCtx==0 ); sqlite3ValueFree(pVal);
77261#endif
77262  return SQLITE_NOMEM_BKPT;
77263}
77264
77265/*
77266** Create a new sqlite3_value object, containing the value of pExpr.
77267**
77268** This only works for very simple expressions that consist of one constant
77269** token (i.e. "5", "5.1", "'a string'"). If the expression can
77270** be converted directly into a value, then the value is allocated and
77271** a pointer written to *ppVal. The caller is responsible for deallocating
77272** the value by passing it to sqlite3ValueFree() later on. If the expression
77273** cannot be converted to a value, then *ppVal is set to NULL.
77274*/
77275SQLITE_PRIVATE int sqlite3ValueFromExpr(
77276  sqlite3 *db,              /* The database connection */
77277  Expr *pExpr,              /* The expression to evaluate */
77278  u8 enc,                   /* Encoding to use */
77279  u8 affinity,              /* Affinity to use */
77280  sqlite3_value **ppVal     /* Write the new value here */
77281){
77282  return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0;
77283}
77284
77285#ifdef SQLITE_ENABLE_STAT4
77286/*
77287** Attempt to extract a value from pExpr and use it to construct *ppVal.
77288**
77289** If pAlloc is not NULL, then an UnpackedRecord object is created for
77290** pAlloc if one does not exist and the new value is added to the
77291** UnpackedRecord object.
77292**
77293** A value is extracted in the following cases:
77294**
77295**  * (pExpr==0). In this case the value is assumed to be an SQL NULL,
77296**
77297**  * The expression is a bound variable, and this is a reprepare, or
77298**
77299**  * The expression is a literal value.
77300**
77301** On success, *ppVal is made to point to the extracted value.  The caller
77302** is responsible for ensuring that the value is eventually freed.
77303*/
77304static int stat4ValueFromExpr(
77305  Parse *pParse,                  /* Parse context */
77306  Expr *pExpr,                    /* The expression to extract a value from */
77307  u8 affinity,                    /* Affinity to use */
77308  struct ValueNewStat4Ctx *pAlloc,/* How to allocate space.  Or NULL */
77309  sqlite3_value **ppVal           /* OUT: New value object (or NULL) */
77310){
77311  int rc = SQLITE_OK;
77312  sqlite3_value *pVal = 0;
77313  sqlite3 *db = pParse->db;
77314
77315  /* Skip over any TK_COLLATE nodes */
77316  pExpr = sqlite3ExprSkipCollate(pExpr);
77317
77318  assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
77319  if( !pExpr ){
77320    pVal = valueNew(db, pAlloc);
77321    if( pVal ){
77322      sqlite3VdbeMemSetNull((Mem*)pVal);
77323    }
77324  }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
77325    Vdbe *v;
77326    int iBindVar = pExpr->iColumn;
77327    sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
77328    if( (v = pParse->pReprepare)!=0 ){
77329      pVal = valueNew(db, pAlloc);
77330      if( pVal ){
77331        rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
77332        sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
77333        pVal->db = pParse->db;
77334      }
77335    }
77336  }else{
77337    rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, pAlloc);
77338  }
77339
77340  assert( pVal==0 || pVal->db==db );
77341  *ppVal = pVal;
77342  return rc;
77343}
77344
77345/*
77346** This function is used to allocate and populate UnpackedRecord
77347** structures intended to be compared against sample index keys stored
77348** in the sqlite_stat4 table.
77349**
77350** A single call to this function populates zero or more fields of the
77351** record starting with field iVal (fields are numbered from left to
77352** right starting with 0). A single field is populated if:
77353**
77354**  * (pExpr==0). In this case the value is assumed to be an SQL NULL,
77355**
77356**  * The expression is a bound variable, and this is a reprepare, or
77357**
77358**  * The sqlite3ValueFromExpr() function is able to extract a value
77359**    from the expression (i.e. the expression is a literal value).
77360**
77361** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
77362** vector components that match either of the two latter criteria listed
77363** above.
77364**
77365** Before any value is appended to the record, the affinity of the
77366** corresponding column within index pIdx is applied to it. Before
77367** this function returns, output parameter *pnExtract is set to the
77368** number of values appended to the record.
77369**
77370** When this function is called, *ppRec must either point to an object
77371** allocated by an earlier call to this function, or must be NULL. If it
77372** is NULL and a value can be successfully extracted, a new UnpackedRecord
77373** is allocated (and *ppRec set to point to it) before returning.
77374**
77375** Unless an error is encountered, SQLITE_OK is returned. It is not an
77376** error if a value cannot be extracted from pExpr. If an error does
77377** occur, an SQLite error code is returned.
77378*/
77379SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
77380  Parse *pParse,                  /* Parse context */
77381  Index *pIdx,                    /* Index being probed */
77382  UnpackedRecord **ppRec,         /* IN/OUT: Probe record */
77383  Expr *pExpr,                    /* The expression to extract a value from */
77384  int nElem,                      /* Maximum number of values to append */
77385  int iVal,                       /* Array element to populate */
77386  int *pnExtract                  /* OUT: Values appended to the record */
77387){
77388  int rc = SQLITE_OK;
77389  int nExtract = 0;
77390
77391  if( pExpr==0 || pExpr->op!=TK_SELECT ){
77392    int i;
77393    struct ValueNewStat4Ctx alloc;
77394
77395    alloc.pParse = pParse;
77396    alloc.pIdx = pIdx;
77397    alloc.ppRec = ppRec;
77398
77399    for(i=0; i<nElem; i++){
77400      sqlite3_value *pVal = 0;
77401      Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0);
77402      u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
77403      alloc.iVal = iVal+i;
77404      rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal);
77405      if( !pVal ) break;
77406      nExtract++;
77407    }
77408  }
77409
77410  *pnExtract = nExtract;
77411  return rc;
77412}
77413
77414/*
77415** Attempt to extract a value from expression pExpr using the methods
77416** as described for sqlite3Stat4ProbeSetValue() above.
77417**
77418** If successful, set *ppVal to point to a new value object and return
77419** SQLITE_OK. If no value can be extracted, but no other error occurs
77420** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
77421** does occur, return an SQLite error code. The final value of *ppVal
77422** is undefined in this case.
77423*/
77424SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(
77425  Parse *pParse,                  /* Parse context */
77426  Expr *pExpr,                    /* The expression to extract a value from */
77427  u8 affinity,                    /* Affinity to use */
77428  sqlite3_value **ppVal           /* OUT: New value object (or NULL) */
77429){
77430  return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
77431}
77432
77433/*
77434** Extract the iCol-th column from the nRec-byte record in pRec.  Write
77435** the column value into *ppVal.  If *ppVal is initially NULL then a new
77436** sqlite3_value object is allocated.
77437**
77438** If *ppVal is initially NULL then the caller is responsible for
77439** ensuring that the value written into *ppVal is eventually freed.
77440*/
77441SQLITE_PRIVATE int sqlite3Stat4Column(
77442  sqlite3 *db,                    /* Database handle */
77443  const void *pRec,               /* Pointer to buffer containing record */
77444  int nRec,                       /* Size of buffer pRec in bytes */
77445  int iCol,                       /* Column to extract */
77446  sqlite3_value **ppVal           /* OUT: Extracted value */
77447){
77448  u32 t = 0;                      /* a column type code */
77449  int nHdr;                       /* Size of the header in the record */
77450  int iHdr;                       /* Next unread header byte */
77451  int iField;                     /* Next unread data byte */
77452  int szField = 0;                /* Size of the current data field */
77453  int i;                          /* Column index */
77454  u8 *a = (u8*)pRec;              /* Typecast byte array */
77455  Mem *pMem = *ppVal;             /* Write result into this Mem object */
77456
77457  assert( iCol>0 );
77458  iHdr = getVarint32(a, nHdr);
77459  if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT;
77460  iField = nHdr;
77461  for(i=0; i<=iCol; i++){
77462    iHdr += getVarint32(&a[iHdr], t);
77463    testcase( iHdr==nHdr );
77464    testcase( iHdr==nHdr+1 );
77465    if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPT;
77466    szField = sqlite3VdbeSerialTypeLen(t);
77467    iField += szField;
77468  }
77469  testcase( iField==nRec );
77470  testcase( iField==nRec+1 );
77471  if( iField>nRec ) return SQLITE_CORRUPT_BKPT;
77472  if( pMem==0 ){
77473    pMem = *ppVal = sqlite3ValueNew(db);
77474    if( pMem==0 ) return SQLITE_NOMEM_BKPT;
77475  }
77476  sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
77477  pMem->enc = ENC(db);
77478  return SQLITE_OK;
77479}
77480
77481/*
77482** Unless it is NULL, the argument must be an UnpackedRecord object returned
77483** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
77484** the object.
77485*/
77486SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
77487  if( pRec ){
77488    int i;
77489    int nCol = pRec->pKeyInfo->nAllField;
77490    Mem *aMem = pRec->aMem;
77491    sqlite3 *db = aMem[0].db;
77492    for(i=0; i<nCol; i++){
77493      sqlite3VdbeMemRelease(&aMem[i]);
77494    }
77495    sqlite3KeyInfoUnref(pRec->pKeyInfo);
77496    sqlite3DbFreeNN(db, pRec);
77497  }
77498}
77499#endif /* ifdef SQLITE_ENABLE_STAT4 */
77500
77501/*
77502** Change the string value of an sqlite3_value object
77503*/
77504SQLITE_PRIVATE void sqlite3ValueSetStr(
77505  sqlite3_value *v,     /* Value to be set */
77506  int n,                /* Length of string z */
77507  const void *z,        /* Text of the new string */
77508  u8 enc,               /* Encoding to use */
77509  void (*xDel)(void*)   /* Destructor for the string */
77510){
77511  if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
77512}
77513
77514/*
77515** Free an sqlite3_value object
77516*/
77517SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
77518  if( !v ) return;
77519  sqlite3VdbeMemRelease((Mem *)v);
77520  sqlite3DbFreeNN(((Mem*)v)->db, v);
77521}
77522
77523/*
77524** The sqlite3ValueBytes() routine returns the number of bytes in the
77525** sqlite3_value object assuming that it uses the encoding "enc".
77526** The valueBytes() routine is a helper function.
77527*/
77528static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
77529  return valueToText(pVal, enc)!=0 ? pVal->n : 0;
77530}
77531SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
77532  Mem *p = (Mem*)pVal;
77533  assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
77534  if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
77535    return p->n;
77536  }
77537  if( (p->flags & MEM_Blob)!=0 ){
77538    if( p->flags & MEM_Zero ){
77539      return p->n + p->u.nZero;
77540    }else{
77541      return p->n;
77542    }
77543  }
77544  if( p->flags & MEM_Null ) return 0;
77545  return valueBytes(pVal, enc);
77546}
77547
77548/************** End of vdbemem.c *********************************************/
77549/************** Begin file vdbeaux.c *****************************************/
77550/*
77551** 2003 September 6
77552**
77553** The author disclaims copyright to this source code.  In place of
77554** a legal notice, here is a blessing:
77555**
77556**    May you do good and not evil.
77557**    May you find forgiveness for yourself and forgive others.
77558**    May you share freely, never taking more than you give.
77559**
77560*************************************************************************
77561** This file contains code used for creating, destroying, and populating
77562** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
77563*/
77564/* #include "sqliteInt.h" */
77565/* #include "vdbeInt.h" */
77566
77567/* Forward references */
77568static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef);
77569static void vdbeFreeOpArray(sqlite3 *, Op *, int);
77570
77571/*
77572** Create a new virtual database engine.
77573*/
77574SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
77575  sqlite3 *db = pParse->db;
77576  Vdbe *p;
77577  p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
77578  if( p==0 ) return 0;
77579  memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
77580  p->db = db;
77581  if( db->pVdbe ){
77582    db->pVdbe->pPrev = p;
77583  }
77584  p->pNext = db->pVdbe;
77585  p->pPrev = 0;
77586  db->pVdbe = p;
77587  p->magic = VDBE_MAGIC_INIT;
77588  p->pParse = pParse;
77589  pParse->pVdbe = p;
77590  assert( pParse->aLabel==0 );
77591  assert( pParse->nLabel==0 );
77592  assert( p->nOpAlloc==0 );
77593  assert( pParse->szOpAlloc==0 );
77594  sqlite3VdbeAddOp2(p, OP_Init, 0, 1);
77595  return p;
77596}
77597
77598/*
77599** Return the Parse object that owns a Vdbe object.
77600*/
77601SQLITE_PRIVATE Parse *sqlite3VdbeParser(Vdbe *p){
77602  return p->pParse;
77603}
77604
77605/*
77606** Change the error string stored in Vdbe.zErrMsg
77607*/
77608SQLITE_PRIVATE void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
77609  va_list ap;
77610  sqlite3DbFree(p->db, p->zErrMsg);
77611  va_start(ap, zFormat);
77612  p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
77613  va_end(ap);
77614}
77615
77616/*
77617** Remember the SQL string for a prepared statement.
77618*/
77619SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, u8 prepFlags){
77620  if( p==0 ) return;
77621  p->prepFlags = prepFlags;
77622  if( (prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
77623    p->expmask = 0;
77624  }
77625  assert( p->zSql==0 );
77626  p->zSql = sqlite3DbStrNDup(p->db, z, n);
77627}
77628
77629#ifdef SQLITE_ENABLE_NORMALIZE
77630/*
77631** Add a new element to the Vdbe->pDblStr list.
77632*/
77633SQLITE_PRIVATE void sqlite3VdbeAddDblquoteStr(sqlite3 *db, Vdbe *p, const char *z){
77634  if( p ){
77635    int n = sqlite3Strlen30(z);
77636    DblquoteStr *pStr = sqlite3DbMallocRawNN(db,
77637                            sizeof(*pStr)+n+1-sizeof(pStr->z));
77638    if( pStr ){
77639      pStr->pNextStr = p->pDblStr;
77640      p->pDblStr = pStr;
77641      memcpy(pStr->z, z, n+1);
77642    }
77643  }
77644}
77645#endif
77646
77647#ifdef SQLITE_ENABLE_NORMALIZE
77648/*
77649** zId of length nId is a double-quoted identifier.  Check to see if
77650** that identifier is really used as a string literal.
77651*/
77652SQLITE_PRIVATE int sqlite3VdbeUsesDoubleQuotedString(
77653  Vdbe *pVdbe,            /* The prepared statement */
77654  const char *zId         /* The double-quoted identifier, already dequoted */
77655){
77656  DblquoteStr *pStr;
77657  assert( zId!=0 );
77658  if( pVdbe->pDblStr==0 ) return 0;
77659  for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
77660    if( strcmp(zId, pStr->z)==0 ) return 1;
77661  }
77662  return 0;
77663}
77664#endif
77665
77666/*
77667** Swap all content between two VDBE structures.
77668*/
77669SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
77670  Vdbe tmp, *pTmp;
77671  char *zTmp;
77672  assert( pA->db==pB->db );
77673  tmp = *pA;
77674  *pA = *pB;
77675  *pB = tmp;
77676  pTmp = pA->pNext;
77677  pA->pNext = pB->pNext;
77678  pB->pNext = pTmp;
77679  pTmp = pA->pPrev;
77680  pA->pPrev = pB->pPrev;
77681  pB->pPrev = pTmp;
77682  zTmp = pA->zSql;
77683  pA->zSql = pB->zSql;
77684  pB->zSql = zTmp;
77685#ifdef SQLITE_ENABLE_NORMALIZE
77686  zTmp = pA->zNormSql;
77687  pA->zNormSql = pB->zNormSql;
77688  pB->zNormSql = zTmp;
77689#endif
77690  pB->expmask = pA->expmask;
77691  pB->prepFlags = pA->prepFlags;
77692  memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
77693  pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
77694}
77695
77696/*
77697** Resize the Vdbe.aOp array so that it is at least nOp elements larger
77698** than its current size. nOp is guaranteed to be less than or equal
77699** to 1024/sizeof(Op).
77700**
77701** If an out-of-memory error occurs while resizing the array, return
77702** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
77703** unchanged (this is so that any opcodes already allocated can be
77704** correctly deallocated along with the rest of the Vdbe).
77705*/
77706static int growOpArray(Vdbe *v, int nOp){
77707  VdbeOp *pNew;
77708  Parse *p = v->pParse;
77709
77710  /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
77711  ** more frequent reallocs and hence provide more opportunities for
77712  ** simulated OOM faults.  SQLITE_TEST_REALLOC_STRESS is generally used
77713  ** during testing only.  With SQLITE_TEST_REALLOC_STRESS grow the op array
77714  ** by the minimum* amount required until the size reaches 512.  Normal
77715  ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
77716  ** size of the op array or add 1KB of space, whichever is smaller. */
77717#ifdef SQLITE_TEST_REALLOC_STRESS
77718  sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
77719                        : (sqlite3_int64)v->nOpAlloc+nOp);
77720#else
77721  sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
77722                        : (sqlite3_int64)(1024/sizeof(Op)));
77723  UNUSED_PARAMETER(nOp);
77724#endif
77725
77726  /* Ensure that the size of a VDBE does not grow too large */
77727  if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
77728    sqlite3OomFault(p->db);
77729    return SQLITE_NOMEM;
77730  }
77731
77732  assert( nOp<=(1024/sizeof(Op)) );
77733  assert( nNew>=(v->nOpAlloc+nOp) );
77734  pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
77735  if( pNew ){
77736    p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
77737    v->nOpAlloc = p->szOpAlloc/sizeof(Op);
77738    v->aOp = pNew;
77739  }
77740  return (pNew ? SQLITE_OK : SQLITE_NOMEM_BKPT);
77741}
77742
77743#ifdef SQLITE_DEBUG
77744/* This routine is just a convenient place to set a breakpoint that will
77745** fire after each opcode is inserted and displayed using
77746** "PRAGMA vdbe_addoptrace=on".  Parameters "pc" (program counter) and
77747** pOp are available to make the breakpoint conditional.
77748**
77749** Other useful labels for breakpoints include:
77750**   test_trace_breakpoint(pc,pOp)
77751**   sqlite3CorruptError(lineno)
77752**   sqlite3MisuseError(lineno)
77753**   sqlite3CantopenError(lineno)
77754*/
77755static void test_addop_breakpoint(int pc, Op *pOp){
77756  static int n = 0;
77757  n++;
77758}
77759#endif
77760
77761/*
77762** Add a new instruction to the list of instructions current in the
77763** VDBE.  Return the address of the new instruction.
77764**
77765** Parameters:
77766**
77767**    p               Pointer to the VDBE
77768**
77769**    op              The opcode for this instruction
77770**
77771**    p1, p2, p3      Operands
77772**
77773** Use the sqlite3VdbeResolveLabel() function to fix an address and
77774** the sqlite3VdbeChangeP4() function to change the value of the P4
77775** operand.
77776*/
77777static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
77778  assert( p->nOpAlloc<=p->nOp );
77779  if( growOpArray(p, 1) ) return 1;
77780  assert( p->nOpAlloc>p->nOp );
77781  return sqlite3VdbeAddOp3(p, op, p1, p2, p3);
77782}
77783SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
77784  int i;
77785  VdbeOp *pOp;
77786
77787  i = p->nOp;
77788  assert( p->magic==VDBE_MAGIC_INIT );
77789  assert( op>=0 && op<0xff );
77790  if( p->nOpAlloc<=i ){
77791    return growOp3(p, op, p1, p2, p3);
77792  }
77793  p->nOp++;
77794  pOp = &p->aOp[i];
77795  pOp->opcode = (u8)op;
77796  pOp->p5 = 0;
77797  pOp->p1 = p1;
77798  pOp->p2 = p2;
77799  pOp->p3 = p3;
77800  pOp->p4.p = 0;
77801  pOp->p4type = P4_NOTUSED;
77802#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
77803  pOp->zComment = 0;
77804#endif
77805#ifdef SQLITE_DEBUG
77806  if( p->db->flags & SQLITE_VdbeAddopTrace ){
77807    sqlite3VdbePrintOp(0, i, &p->aOp[i]);
77808    test_addop_breakpoint(i, &p->aOp[i]);
77809  }
77810#endif
77811#ifdef VDBE_PROFILE
77812  pOp->cycles = 0;
77813  pOp->cnt = 0;
77814#endif
77815#ifdef SQLITE_VDBE_COVERAGE
77816  pOp->iSrcLine = 0;
77817#endif
77818  return i;
77819}
77820SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
77821  return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
77822}
77823SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
77824  return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
77825}
77826SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
77827  return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
77828}
77829
77830/* Generate code for an unconditional jump to instruction iDest
77831*/
77832SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe *p, int iDest){
77833  return sqlite3VdbeAddOp3(p, OP_Goto, 0, iDest, 0);
77834}
77835
77836/* Generate code to cause the string zStr to be loaded into
77837** register iDest
77838*/
77839SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
77840  return sqlite3VdbeAddOp4(p, OP_String8, 0, iDest, 0, zStr, 0);
77841}
77842
77843/*
77844** Generate code that initializes multiple registers to string or integer
77845** constants.  The registers begin with iDest and increase consecutively.
77846** One register is initialized for each characgter in zTypes[].  For each
77847** "s" character in zTypes[], the register is a string if the argument is
77848** not NULL, or OP_Null if the value is a null pointer.  For each "i" character
77849** in zTypes[], the register is initialized to an integer.
77850**
77851** If the input string does not end with "X" then an OP_ResultRow instruction
77852** is generated for the values inserted.
77853*/
77854SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
77855  va_list ap;
77856  int i;
77857  char c;
77858  va_start(ap, zTypes);
77859  for(i=0; (c = zTypes[i])!=0; i++){
77860    if( c=='s' ){
77861      const char *z = va_arg(ap, const char*);
77862      sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest+i, 0, z, 0);
77863    }else if( c=='i' ){
77864      sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest+i);
77865    }else{
77866      goto skip_op_resultrow;
77867    }
77868  }
77869  sqlite3VdbeAddOp2(p, OP_ResultRow, iDest, i);
77870skip_op_resultrow:
77871  va_end(ap);
77872}
77873
77874/*
77875** Add an opcode that includes the p4 value as a pointer.
77876*/
77877SQLITE_PRIVATE int sqlite3VdbeAddOp4(
77878  Vdbe *p,            /* Add the opcode to this VM */
77879  int op,             /* The new opcode */
77880  int p1,             /* The P1 operand */
77881  int p2,             /* The P2 operand */
77882  int p3,             /* The P3 operand */
77883  const char *zP4,    /* The P4 operand */
77884  int p4type          /* P4 operand type */
77885){
77886  int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
77887  sqlite3VdbeChangeP4(p, addr, zP4, p4type);
77888  return addr;
77889}
77890
77891/*
77892** Add an OP_Function or OP_PureFunc opcode.
77893**
77894** The eCallCtx argument is information (typically taken from Expr.op2)
77895** that describes the calling context of the function.  0 means a general
77896** function call.  NC_IsCheck means called by a check constraint,
77897** NC_IdxExpr means called as part of an index expression.  NC_PartIdx
77898** means in the WHERE clause of a partial index.  NC_GenCol means called
77899** while computing a generated column value.  0 is the usual case.
77900*/
77901SQLITE_PRIVATE int sqlite3VdbeAddFunctionCall(
77902  Parse *pParse,        /* Parsing context */
77903  int p1,               /* Constant argument mask */
77904  int p2,               /* First argument register */
77905  int p3,               /* Register into which results are written */
77906  int nArg,             /* Number of argument */
77907  const FuncDef *pFunc, /* The function to be invoked */
77908  int eCallCtx          /* Calling context */
77909){
77910  Vdbe *v = pParse->pVdbe;
77911  int nByte;
77912  int addr;
77913  sqlite3_context *pCtx;
77914  assert( v );
77915  nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
77916  pCtx = sqlite3DbMallocRawNN(pParse->db, nByte);
77917  if( pCtx==0 ){
77918    assert( pParse->db->mallocFailed );
77919    freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
77920    return 0;
77921  }
77922  pCtx->pOut = 0;
77923  pCtx->pFunc = (FuncDef*)pFunc;
77924  pCtx->pVdbe = 0;
77925  pCtx->isError = 0;
77926  pCtx->argc = nArg;
77927  pCtx->iOp = sqlite3VdbeCurrentAddr(v);
77928  addr = sqlite3VdbeAddOp4(v, eCallCtx ? OP_PureFunc : OP_Function,
77929                           p1, p2, p3, (char*)pCtx, P4_FUNCCTX);
77930  sqlite3VdbeChangeP5(v, eCallCtx & NC_SelfRef);
77931  return addr;
77932}
77933
77934/*
77935** Add an opcode that includes the p4 value with a P4_INT64 or
77936** P4_REAL type.
77937*/
77938SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(
77939  Vdbe *p,            /* Add the opcode to this VM */
77940  int op,             /* The new opcode */
77941  int p1,             /* The P1 operand */
77942  int p2,             /* The P2 operand */
77943  int p3,             /* The P3 operand */
77944  const u8 *zP4,      /* The P4 operand */
77945  int p4type          /* P4 operand type */
77946){
77947  char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
77948  if( p4copy ) memcpy(p4copy, zP4, 8);
77949  return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
77950}
77951
77952#ifndef SQLITE_OMIT_EXPLAIN
77953/*
77954** Return the address of the current EXPLAIN QUERY PLAN baseline.
77955** 0 means "none".
77956*/
77957SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse *pParse){
77958  VdbeOp *pOp;
77959  if( pParse->addrExplain==0 ) return 0;
77960  pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
77961  return pOp->p2;
77962}
77963
77964/*
77965** Set a debugger breakpoint on the following routine in order to
77966** monitor the EXPLAIN QUERY PLAN code generation.
77967*/
77968#if defined(SQLITE_DEBUG)
77969SQLITE_PRIVATE void sqlite3ExplainBreakpoint(const char *z1, const char *z2){
77970  (void)z1;
77971  (void)z2;
77972}
77973#endif
77974
77975/*
77976** Add a new OP_Explain opcode.
77977**
77978** If the bPush flag is true, then make this opcode the parent for
77979** subsequent Explains until sqlite3VdbeExplainPop() is called.
77980*/
77981SQLITE_PRIVATE void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
77982#ifndef SQLITE_DEBUG
77983  /* Always include the OP_Explain opcodes if SQLITE_DEBUG is defined.
77984  ** But omit them (for performance) during production builds */
77985  if( pParse->explain==2 )
77986#endif
77987  {
77988    char *zMsg;
77989    Vdbe *v;
77990    va_list ap;
77991    int iThis;
77992    va_start(ap, zFmt);
77993    zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
77994    va_end(ap);
77995    v = pParse->pVdbe;
77996    iThis = v->nOp;
77997    sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
77998                      zMsg, P4_DYNAMIC);
77999    sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetOp(v,-1)->p4.z);
78000    if( bPush){
78001      pParse->addrExplain = iThis;
78002    }
78003  }
78004}
78005
78006/*
78007** Pop the EXPLAIN QUERY PLAN stack one level.
78008*/
78009SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse *pParse){
78010  sqlite3ExplainBreakpoint("POP", 0);
78011  pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
78012}
78013#endif /* SQLITE_OMIT_EXPLAIN */
78014
78015/*
78016** Add an OP_ParseSchema opcode.  This routine is broken out from
78017** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
78018** as having been used.
78019**
78020** The zWhere string must have been obtained from sqlite3_malloc().
78021** This routine will take ownership of the allocated memory.
78022*/
78023SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
78024  int j;
78025  sqlite3VdbeAddOp4(p, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
78026  for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
78027}
78028
78029/*
78030** Add an opcode that includes the p4 value as an integer.
78031*/
78032SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
78033  Vdbe *p,            /* Add the opcode to this VM */
78034  int op,             /* The new opcode */
78035  int p1,             /* The P1 operand */
78036  int p2,             /* The P2 operand */
78037  int p3,             /* The P3 operand */
78038  int p4              /* The P4 operand as an integer */
78039){
78040  int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
78041  if( p->db->mallocFailed==0 ){
78042    VdbeOp *pOp = &p->aOp[addr];
78043    pOp->p4type = P4_INT32;
78044    pOp->p4.i = p4;
78045  }
78046  return addr;
78047}
78048
78049/* Insert the end of a co-routine
78050*/
78051SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
78052  sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
78053
78054  /* Clear the temporary register cache, thereby ensuring that each
78055  ** co-routine has its own independent set of registers, because co-routines
78056  ** might expect their registers to be preserved across an OP_Yield, and
78057  ** that could cause problems if two or more co-routines are using the same
78058  ** temporary register.
78059  */
78060  v->pParse->nTempReg = 0;
78061  v->pParse->nRangeReg = 0;
78062}
78063
78064/*
78065** Create a new symbolic label for an instruction that has yet to be
78066** coded.  The symbolic label is really just a negative number.  The
78067** label can be used as the P2 value of an operation.  Later, when
78068** the label is resolved to a specific address, the VDBE will scan
78069** through its operation list and change all values of P2 which match
78070** the label into the resolved address.
78071**
78072** The VDBE knows that a P2 value is a label because labels are
78073** always negative and P2 values are suppose to be non-negative.
78074** Hence, a negative P2 value is a label that has yet to be resolved.
78075** (Later:) This is only true for opcodes that have the OPFLG_JUMP
78076** property.
78077**
78078** Variable usage notes:
78079**
78080**     Parse.aLabel[x]     Stores the address that the x-th label resolves
78081**                         into.  For testing (SQLITE_DEBUG), unresolved
78082**                         labels stores -1, but that is not required.
78083**     Parse.nLabelAlloc   Number of slots allocated to Parse.aLabel[]
78084**     Parse.nLabel        The *negative* of the number of labels that have
78085**                         been issued.  The negative is stored because
78086**                         that gives a performance improvement over storing
78087**                         the equivalent positive value.
78088*/
78089SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse *pParse){
78090  return --pParse->nLabel;
78091}
78092
78093/*
78094** Resolve label "x" to be the address of the next instruction to
78095** be inserted.  The parameter "x" must have been obtained from
78096** a prior call to sqlite3VdbeMakeLabel().
78097*/
78098static SQLITE_NOINLINE void resizeResolveLabel(Parse *p, Vdbe *v, int j){
78099  int nNewSize = 10 - p->nLabel;
78100  p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
78101                     nNewSize*sizeof(p->aLabel[0]));
78102  if( p->aLabel==0 ){
78103    p->nLabelAlloc = 0;
78104  }else{
78105#ifdef SQLITE_DEBUG
78106    int i;
78107    for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
78108#endif
78109    p->nLabelAlloc = nNewSize;
78110    p->aLabel[j] = v->nOp;
78111  }
78112}
78113SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
78114  Parse *p = v->pParse;
78115  int j = ADDR(x);
78116  assert( v->magic==VDBE_MAGIC_INIT );
78117  assert( j<-p->nLabel );
78118  assert( j>=0 );
78119#ifdef SQLITE_DEBUG
78120  if( p->db->flags & SQLITE_VdbeAddopTrace ){
78121    printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
78122  }
78123#endif
78124  if( p->nLabelAlloc + p->nLabel < 0 ){
78125    resizeResolveLabel(p,v,j);
78126  }else{
78127    assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
78128    p->aLabel[j] = v->nOp;
78129  }
78130}
78131
78132/*
78133** Mark the VDBE as one that can only be run one time.
78134*/
78135SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
78136  p->runOnlyOnce = 1;
78137}
78138
78139/*
78140** Mark the VDBE as one that can only be run multiple times.
78141*/
78142SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe *p){
78143  p->runOnlyOnce = 0;
78144}
78145
78146#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
78147
78148/*
78149** The following type and function are used to iterate through all opcodes
78150** in a Vdbe main program and each of the sub-programs (triggers) it may
78151** invoke directly or indirectly. It should be used as follows:
78152**
78153**   Op *pOp;
78154**   VdbeOpIter sIter;
78155**
78156**   memset(&sIter, 0, sizeof(sIter));
78157**   sIter.v = v;                            // v is of type Vdbe*
78158**   while( (pOp = opIterNext(&sIter)) ){
78159**     // Do something with pOp
78160**   }
78161**   sqlite3DbFree(v->db, sIter.apSub);
78162**
78163*/
78164typedef struct VdbeOpIter VdbeOpIter;
78165struct VdbeOpIter {
78166  Vdbe *v;                   /* Vdbe to iterate through the opcodes of */
78167  SubProgram **apSub;        /* Array of subprograms */
78168  int nSub;                  /* Number of entries in apSub */
78169  int iAddr;                 /* Address of next instruction to return */
78170  int iSub;                  /* 0 = main program, 1 = first sub-program etc. */
78171};
78172static Op *opIterNext(VdbeOpIter *p){
78173  Vdbe *v = p->v;
78174  Op *pRet = 0;
78175  Op *aOp;
78176  int nOp;
78177
78178  if( p->iSub<=p->nSub ){
78179
78180    if( p->iSub==0 ){
78181      aOp = v->aOp;
78182      nOp = v->nOp;
78183    }else{
78184      aOp = p->apSub[p->iSub-1]->aOp;
78185      nOp = p->apSub[p->iSub-1]->nOp;
78186    }
78187    assert( p->iAddr<nOp );
78188
78189    pRet = &aOp[p->iAddr];
78190    p->iAddr++;
78191    if( p->iAddr==nOp ){
78192      p->iSub++;
78193      p->iAddr = 0;
78194    }
78195
78196    if( pRet->p4type==P4_SUBPROGRAM ){
78197      int nByte = (p->nSub+1)*sizeof(SubProgram*);
78198      int j;
78199      for(j=0; j<p->nSub; j++){
78200        if( p->apSub[j]==pRet->p4.pProgram ) break;
78201      }
78202      if( j==p->nSub ){
78203        p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
78204        if( !p->apSub ){
78205          pRet = 0;
78206        }else{
78207          p->apSub[p->nSub++] = pRet->p4.pProgram;
78208        }
78209      }
78210    }
78211  }
78212
78213  return pRet;
78214}
78215
78216/*
78217** Check if the program stored in the VM associated with pParse may
78218** throw an ABORT exception (causing the statement, but not entire transaction
78219** to be rolled back). This condition is true if the main program or any
78220** sub-programs contains any of the following:
78221**
78222**   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
78223**   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
78224**   *  OP_Destroy
78225**   *  OP_VUpdate
78226**   *  OP_VCreate
78227**   *  OP_VRename
78228**   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
78229**   *  OP_CreateBtree/BTREE_INTKEY and OP_InitCoroutine
78230**      (for CREATE TABLE AS SELECT ...)
78231**
78232** Then check that the value of Parse.mayAbort is true if an
78233** ABORT may be thrown, or false otherwise. Return true if it does
78234** match, or false otherwise. This function is intended to be used as
78235** part of an assert statement in the compiler. Similar to:
78236**
78237**   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
78238*/
78239SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
78240  int hasAbort = 0;
78241  int hasFkCounter = 0;
78242  int hasCreateTable = 0;
78243  int hasCreateIndex = 0;
78244  int hasInitCoroutine = 0;
78245  Op *pOp;
78246  VdbeOpIter sIter;
78247  memset(&sIter, 0, sizeof(sIter));
78248  sIter.v = v;
78249
78250  while( (pOp = opIterNext(&sIter))!=0 ){
78251    int opcode = pOp->opcode;
78252    if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
78253     || opcode==OP_VDestroy
78254     || opcode==OP_VCreate
78255     || (opcode==OP_ParseSchema && pOp->p4.z==0)
78256     || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
78257      && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
78258    ){
78259      hasAbort = 1;
78260      break;
78261    }
78262    if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
78263    if( mayAbort ){
78264      /* hasCreateIndex may also be set for some DELETE statements that use
78265      ** OP_Clear. So this routine may end up returning true in the case
78266      ** where a "DELETE FROM tbl" has a statement-journal but does not
78267      ** require one. This is not so bad - it is an inefficiency, not a bug. */
78268      if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1;
78269      if( opcode==OP_Clear ) hasCreateIndex = 1;
78270    }
78271    if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
78272#ifndef SQLITE_OMIT_FOREIGN_KEY
78273    if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
78274      hasFkCounter = 1;
78275    }
78276#endif
78277  }
78278  sqlite3DbFree(v->db, sIter.apSub);
78279
78280  /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
78281  ** If malloc failed, then the while() loop above may not have iterated
78282  ** through all opcodes and hasAbort may be set incorrectly. Return
78283  ** true for this case to prevent the assert() in the callers frame
78284  ** from failing.  */
78285  return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
78286        || (hasCreateTable && hasInitCoroutine) || hasCreateIndex
78287  );
78288}
78289#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
78290
78291#ifdef SQLITE_DEBUG
78292/*
78293** Increment the nWrite counter in the VDBE if the cursor is not an
78294** ephemeral cursor, or if the cursor argument is NULL.
78295*/
78296SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
78297  if( pC==0
78298   || (pC->eCurType!=CURTYPE_SORTER
78299       && pC->eCurType!=CURTYPE_PSEUDO
78300       && !pC->isEphemeral)
78301  ){
78302    p->nWrite++;
78303  }
78304}
78305#endif
78306
78307#ifdef SQLITE_DEBUG
78308/*
78309** Assert if an Abort at this point in time might result in a corrupt
78310** database.
78311*/
78312SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe *p){
78313  assert( p->nWrite==0 || p->usesStmtJournal );
78314}
78315#endif
78316
78317/*
78318** This routine is called after all opcodes have been inserted.  It loops
78319** through all the opcodes and fixes up some details.
78320**
78321** (1) For each jump instruction with a negative P2 value (a label)
78322**     resolve the P2 value to an actual address.
78323**
78324** (2) Compute the maximum number of arguments used by any SQL function
78325**     and store that value in *pMaxFuncArgs.
78326**
78327** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
78328**     indicate what the prepared statement actually does.
78329**
78330** (4) Initialize the p4.xAdvance pointer on opcodes that use it.
78331**
78332** (5) Reclaim the memory allocated for storing labels.
78333**
78334** This routine will only function correctly if the mkopcodeh.tcl generator
78335** script numbers the opcodes correctly.  Changes to this routine must be
78336** coordinated with changes to mkopcodeh.tcl.
78337*/
78338static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
78339  int nMaxArgs = *pMaxFuncArgs;
78340  Op *pOp;
78341  Parse *pParse = p->pParse;
78342  int *aLabel = pParse->aLabel;
78343  p->readOnly = 1;
78344  p->bIsReader = 0;
78345  pOp = &p->aOp[p->nOp-1];
78346  while(1){
78347
78348    /* Only JUMP opcodes and the short list of special opcodes in the switch
78349    ** below need to be considered.  The mkopcodeh.tcl generator script groups
78350    ** all these opcodes together near the front of the opcode list.  Skip
78351    ** any opcode that does not need processing by virtual of the fact that
78352    ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
78353    */
78354    if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
78355      /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
78356      ** cases from this switch! */
78357      switch( pOp->opcode ){
78358        case OP_Transaction: {
78359          if( pOp->p2!=0 ) p->readOnly = 0;
78360          /* fall thru */
78361        }
78362        case OP_AutoCommit:
78363        case OP_Savepoint: {
78364          p->bIsReader = 1;
78365          break;
78366        }
78367#ifndef SQLITE_OMIT_WAL
78368        case OP_Checkpoint:
78369#endif
78370        case OP_Vacuum:
78371        case OP_JournalMode: {
78372          p->readOnly = 0;
78373          p->bIsReader = 1;
78374          break;
78375        }
78376        case OP_Next:
78377        case OP_SorterNext: {
78378          pOp->p4.xAdvance = sqlite3BtreeNext;
78379          pOp->p4type = P4_ADVANCE;
78380          /* The code generator never codes any of these opcodes as a jump
78381          ** to a label.  They are always coded as a jump backwards to a
78382          ** known address */
78383          assert( pOp->p2>=0 );
78384          break;
78385        }
78386        case OP_Prev: {
78387          pOp->p4.xAdvance = sqlite3BtreePrevious;
78388          pOp->p4type = P4_ADVANCE;
78389          /* The code generator never codes any of these opcodes as a jump
78390          ** to a label.  They are always coded as a jump backwards to a
78391          ** known address */
78392          assert( pOp->p2>=0 );
78393          break;
78394        }
78395#ifndef SQLITE_OMIT_VIRTUALTABLE
78396        case OP_VUpdate: {
78397          if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
78398          break;
78399        }
78400        case OP_VFilter: {
78401          int n;
78402          assert( (pOp - p->aOp) >= 3 );
78403          assert( pOp[-1].opcode==OP_Integer );
78404          n = pOp[-1].p1;
78405          if( n>nMaxArgs ) nMaxArgs = n;
78406          /* Fall through into the default case */
78407        }
78408#endif
78409        default: {
78410          if( pOp->p2<0 ){
78411            /* The mkopcodeh.tcl script has so arranged things that the only
78412            ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
78413            ** have non-negative values for P2. */
78414            assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
78415            assert( ADDR(pOp->p2)<-pParse->nLabel );
78416            pOp->p2 = aLabel[ADDR(pOp->p2)];
78417          }
78418          break;
78419        }
78420      }
78421      /* The mkopcodeh.tcl script has so arranged things that the only
78422      ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
78423      ** have non-negative values for P2. */
78424      assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
78425    }
78426    if( pOp==p->aOp ) break;
78427    pOp--;
78428  }
78429  sqlite3DbFree(p->db, pParse->aLabel);
78430  pParse->aLabel = 0;
78431  pParse->nLabel = 0;
78432  *pMaxFuncArgs = nMaxArgs;
78433  assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
78434}
78435
78436/*
78437** Return the address of the next instruction to be inserted.
78438*/
78439SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
78440  assert( p->magic==VDBE_MAGIC_INIT );
78441  return p->nOp;
78442}
78443
78444/*
78445** Verify that at least N opcode slots are available in p without
78446** having to malloc for more space (except when compiled using
78447** SQLITE_TEST_REALLOC_STRESS).  This interface is used during testing
78448** to verify that certain calls to sqlite3VdbeAddOpList() can never
78449** fail due to a OOM fault and hence that the return value from
78450** sqlite3VdbeAddOpList() will always be non-NULL.
78451*/
78452#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
78453SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
78454  assert( p->nOp + N <= p->nOpAlloc );
78455}
78456#endif
78457
78458/*
78459** Verify that the VM passed as the only argument does not contain
78460** an OP_ResultRow opcode. Fail an assert() if it does. This is used
78461** by code in pragma.c to ensure that the implementation of certain
78462** pragmas comports with the flags specified in the mkpragmatab.tcl
78463** script.
78464*/
78465#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
78466SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p){
78467  int i;
78468  for(i=0; i<p->nOp; i++){
78469    assert( p->aOp[i].opcode!=OP_ResultRow );
78470  }
78471}
78472#endif
78473
78474/*
78475** Generate code (a single OP_Abortable opcode) that will
78476** verify that the VDBE program can safely call Abort in the current
78477** context.
78478*/
78479#if defined(SQLITE_DEBUG)
78480SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
78481  if( onError==OE_Abort ) sqlite3VdbeAddOp0(p, OP_Abortable);
78482}
78483#endif
78484
78485/*
78486** This function returns a pointer to the array of opcodes associated with
78487** the Vdbe passed as the first argument. It is the callers responsibility
78488** to arrange for the returned array to be eventually freed using the
78489** vdbeFreeOpArray() function.
78490**
78491** Before returning, *pnOp is set to the number of entries in the returned
78492** array. Also, *pnMaxArg is set to the larger of its current value and
78493** the number of entries in the Vdbe.apArg[] array required to execute the
78494** returned program.
78495*/
78496SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
78497  VdbeOp *aOp = p->aOp;
78498  assert( aOp && !p->db->mallocFailed );
78499
78500  /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
78501  assert( DbMaskAllZero(p->btreeMask) );
78502
78503  resolveP2Values(p, pnMaxArg);
78504  *pnOp = p->nOp;
78505  p->aOp = 0;
78506  return aOp;
78507}
78508
78509/*
78510** Add a whole list of operations to the operation stack.  Return a
78511** pointer to the first operation inserted.
78512**
78513** Non-zero P2 arguments to jump instructions are automatically adjusted
78514** so that the jump target is relative to the first operation inserted.
78515*/
78516SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(
78517  Vdbe *p,                     /* Add opcodes to the prepared statement */
78518  int nOp,                     /* Number of opcodes to add */
78519  VdbeOpList const *aOp,       /* The opcodes to be added */
78520  int iLineno                  /* Source-file line number of first opcode */
78521){
78522  int i;
78523  VdbeOp *pOut, *pFirst;
78524  assert( nOp>0 );
78525  assert( p->magic==VDBE_MAGIC_INIT );
78526  if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
78527    return 0;
78528  }
78529  pFirst = pOut = &p->aOp[p->nOp];
78530  for(i=0; i<nOp; i++, aOp++, pOut++){
78531    pOut->opcode = aOp->opcode;
78532    pOut->p1 = aOp->p1;
78533    pOut->p2 = aOp->p2;
78534    assert( aOp->p2>=0 );
78535    if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
78536      pOut->p2 += p->nOp;
78537    }
78538    pOut->p3 = aOp->p3;
78539    pOut->p4type = P4_NOTUSED;
78540    pOut->p4.p = 0;
78541    pOut->p5 = 0;
78542#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
78543    pOut->zComment = 0;
78544#endif
78545#ifdef SQLITE_VDBE_COVERAGE
78546    pOut->iSrcLine = iLineno+i;
78547#else
78548    (void)iLineno;
78549#endif
78550#ifdef SQLITE_DEBUG
78551    if( p->db->flags & SQLITE_VdbeAddopTrace ){
78552      sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
78553    }
78554#endif
78555  }
78556  p->nOp += nOp;
78557  return pFirst;
78558}
78559
78560#if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
78561/*
78562** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
78563*/
78564SQLITE_PRIVATE void sqlite3VdbeScanStatus(
78565  Vdbe *p,                        /* VM to add scanstatus() to */
78566  int addrExplain,                /* Address of OP_Explain (or 0) */
78567  int addrLoop,                   /* Address of loop counter */
78568  int addrVisit,                  /* Address of rows visited counter */
78569  LogEst nEst,                    /* Estimated number of output rows */
78570  const char *zName               /* Name of table or index being scanned */
78571){
78572  sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
78573  ScanStatus *aNew;
78574  aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
78575  if( aNew ){
78576    ScanStatus *pNew = &aNew[p->nScan++];
78577    pNew->addrExplain = addrExplain;
78578    pNew->addrLoop = addrLoop;
78579    pNew->addrVisit = addrVisit;
78580    pNew->nEst = nEst;
78581    pNew->zName = sqlite3DbStrDup(p->db, zName);
78582    p->aScan = aNew;
78583  }
78584}
78585#endif
78586
78587
78588/*
78589** Change the value of the opcode, or P1, P2, P3, or P5 operands
78590** for a specific instruction.
78591*/
78592SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe *p, int addr, u8 iNewOpcode){
78593  sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
78594}
78595SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
78596  sqlite3VdbeGetOp(p,addr)->p1 = val;
78597}
78598SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
78599  sqlite3VdbeGetOp(p,addr)->p2 = val;
78600}
78601SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
78602  sqlite3VdbeGetOp(p,addr)->p3 = val;
78603}
78604SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
78605  assert( p->nOp>0 || p->db->mallocFailed );
78606  if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
78607}
78608
78609/*
78610** Change the P2 operand of instruction addr so that it points to
78611** the address of the next instruction to be coded.
78612*/
78613SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
78614  sqlite3VdbeChangeP2(p, addr, p->nOp);
78615}
78616
78617/*
78618** Change the P2 operand of the jump instruction at addr so that
78619** the jump lands on the next opcode.  Or if the jump instruction was
78620** the previous opcode (and is thus a no-op) then simply back up
78621** the next instruction counter by one slot so that the jump is
78622** overwritten by the next inserted opcode.
78623**
78624** This routine is an optimization of sqlite3VdbeJumpHere() that
78625** strives to omit useless byte-code like this:
78626**
78627**        7   Once 0 8 0
78628**        8   ...
78629*/
78630SQLITE_PRIVATE void sqlite3VdbeJumpHereOrPopInst(Vdbe *p, int addr){
78631  if( addr==p->nOp-1 ){
78632    assert( p->aOp[addr].opcode==OP_Once
78633         || p->aOp[addr].opcode==OP_If
78634         || p->aOp[addr].opcode==OP_FkIfZero );
78635    assert( p->aOp[addr].p4type==0 );
78636#ifdef SQLITE_VDBE_COVERAGE
78637    sqlite3VdbeGetOp(p,-1)->iSrcLine = 0;  /* Erase VdbeCoverage() macros */
78638#endif
78639    p->nOp--;
78640  }else{
78641    sqlite3VdbeChangeP2(p, addr, p->nOp);
78642  }
78643}
78644
78645
78646/*
78647** If the input FuncDef structure is ephemeral, then free it.  If
78648** the FuncDef is not ephermal, then do nothing.
78649*/
78650static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
78651  if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
78652    sqlite3DbFreeNN(db, pDef);
78653  }
78654}
78655
78656/*
78657** Delete a P4 value if necessary.
78658*/
78659static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){
78660  if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
78661  sqlite3DbFreeNN(db, p);
78662}
78663static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
78664  freeEphemeralFunction(db, p->pFunc);
78665  sqlite3DbFreeNN(db, p);
78666}
78667static void freeP4(sqlite3 *db, int p4type, void *p4){
78668  assert( db );
78669  switch( p4type ){
78670    case P4_FUNCCTX: {
78671      freeP4FuncCtx(db, (sqlite3_context*)p4);
78672      break;
78673    }
78674    case P4_REAL:
78675    case P4_INT64:
78676    case P4_DYNAMIC:
78677    case P4_DYNBLOB:
78678    case P4_INTARRAY: {
78679      sqlite3DbFree(db, p4);
78680      break;
78681    }
78682    case P4_KEYINFO: {
78683      if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
78684      break;
78685    }
78686#ifdef SQLITE_ENABLE_CURSOR_HINTS
78687    case P4_EXPR: {
78688      sqlite3ExprDelete(db, (Expr*)p4);
78689      break;
78690    }
78691#endif
78692    case P4_FUNCDEF: {
78693      freeEphemeralFunction(db, (FuncDef*)p4);
78694      break;
78695    }
78696    case P4_MEM: {
78697      if( db->pnBytesFreed==0 ){
78698        sqlite3ValueFree((sqlite3_value*)p4);
78699      }else{
78700        freeP4Mem(db, (Mem*)p4);
78701      }
78702      break;
78703    }
78704    case P4_VTAB : {
78705      if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
78706      break;
78707    }
78708  }
78709}
78710
78711/*
78712** Free the space allocated for aOp and any p4 values allocated for the
78713** opcodes contained within. If aOp is not NULL it is assumed to contain
78714** nOp entries.
78715*/
78716static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
78717  if( aOp ){
78718    Op *pOp;
78719    for(pOp=&aOp[nOp-1]; pOp>=aOp; pOp--){
78720      if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
78721#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
78722      sqlite3DbFree(db, pOp->zComment);
78723#endif
78724    }
78725    sqlite3DbFreeNN(db, aOp);
78726  }
78727}
78728
78729/*
78730** Link the SubProgram object passed as the second argument into the linked
78731** list at Vdbe.pSubProgram. This list is used to delete all sub-program
78732** objects when the VM is no longer required.
78733*/
78734SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
78735  p->pNext = pVdbe->pProgram;
78736  pVdbe->pProgram = p;
78737}
78738
78739/*
78740** Return true if the given Vdbe has any SubPrograms.
78741*/
78742SQLITE_PRIVATE int sqlite3VdbeHasSubProgram(Vdbe *pVdbe){
78743  return pVdbe->pProgram!=0;
78744}
78745
78746/*
78747** Change the opcode at addr into OP_Noop
78748*/
78749SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
78750  VdbeOp *pOp;
78751  if( p->db->mallocFailed ) return 0;
78752  assert( addr>=0 && addr<p->nOp );
78753  pOp = &p->aOp[addr];
78754  freeP4(p->db, pOp->p4type, pOp->p4.p);
78755  pOp->p4type = P4_NOTUSED;
78756  pOp->p4.z = 0;
78757  pOp->opcode = OP_Noop;
78758  return 1;
78759}
78760
78761/*
78762** If the last opcode is "op" and it is not a jump destination,
78763** then remove it.  Return true if and only if an opcode was removed.
78764*/
78765SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
78766  if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
78767    return sqlite3VdbeChangeToNoop(p, p->nOp-1);
78768  }else{
78769    return 0;
78770  }
78771}
78772
78773#ifdef SQLITE_DEBUG
78774/*
78775** Generate an OP_ReleaseReg opcode to indicate that a range of
78776** registers, except any identified by mask, are no longer in use.
78777*/
78778SQLITE_PRIVATE void sqlite3VdbeReleaseRegisters(
78779  Parse *pParse,       /* Parsing context */
78780  int iFirst,          /* Index of first register to be released */
78781  int N,               /* Number of registers to release */
78782  u32 mask,            /* Mask of registers to NOT release */
78783  int bUndefine        /* If true, mark registers as undefined */
78784){
78785  if( N==0 ) return;
78786  assert( pParse->pVdbe );
78787  assert( iFirst>=1 );
78788  assert( iFirst+N-1<=pParse->nMem );
78789  if( N<=31 && mask!=0 ){
78790    while( N>0 && (mask&1)!=0 ){
78791      mask >>= 1;
78792      iFirst++;
78793      N--;
78794    }
78795    while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){
78796      mask &= ~MASKBIT32(N-1);
78797      N--;
78798    }
78799  }
78800  if( N>0 ){
78801    sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask);
78802    if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
78803  }
78804}
78805#endif /* SQLITE_DEBUG */
78806
78807
78808/*
78809** Change the value of the P4 operand for a specific instruction.
78810** This routine is useful when a large program is loaded from a
78811** static array using sqlite3VdbeAddOpList but we want to make a
78812** few minor changes to the program.
78813**
78814** If n>=0 then the P4 operand is dynamic, meaning that a copy of
78815** the string is made into memory obtained from sqlite3_malloc().
78816** A value of n==0 means copy bytes of zP4 up to and including the
78817** first null byte.  If n>0 then copy n+1 bytes of zP4.
78818**
78819** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
78820** to a string or structure that is guaranteed to exist for the lifetime of
78821** the Vdbe. In these cases we can just copy the pointer.
78822**
78823** If addr<0 then change P4 on the most recently inserted instruction.
78824*/
78825static void SQLITE_NOINLINE vdbeChangeP4Full(
78826  Vdbe *p,
78827  Op *pOp,
78828  const char *zP4,
78829  int n
78830){
78831  if( pOp->p4type ){
78832    freeP4(p->db, pOp->p4type, pOp->p4.p);
78833    pOp->p4type = 0;
78834    pOp->p4.p = 0;
78835  }
78836  if( n<0 ){
78837    sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
78838  }else{
78839    if( n==0 ) n = sqlite3Strlen30(zP4);
78840    pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
78841    pOp->p4type = P4_DYNAMIC;
78842  }
78843}
78844SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
78845  Op *pOp;
78846  sqlite3 *db;
78847  assert( p!=0 );
78848  db = p->db;
78849  assert( p->magic==VDBE_MAGIC_INIT );
78850  assert( p->aOp!=0 || db->mallocFailed );
78851  if( db->mallocFailed ){
78852    if( n!=P4_VTAB ) freeP4(db, n, (void*)*(char**)&zP4);
78853    return;
78854  }
78855  assert( p->nOp>0 );
78856  assert( addr<p->nOp );
78857  if( addr<0 ){
78858    addr = p->nOp - 1;
78859  }
78860  pOp = &p->aOp[addr];
78861  if( n>=0 || pOp->p4type ){
78862    vdbeChangeP4Full(p, pOp, zP4, n);
78863    return;
78864  }
78865  if( n==P4_INT32 ){
78866    /* Note: this cast is safe, because the origin data point was an int
78867    ** that was cast to a (const char *). */
78868    pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
78869    pOp->p4type = P4_INT32;
78870  }else if( zP4!=0 ){
78871    assert( n<0 );
78872    pOp->p4.p = (void*)zP4;
78873    pOp->p4type = (signed char)n;
78874    if( n==P4_VTAB ) sqlite3VtabLock((VTable*)zP4);
78875  }
78876}
78877
78878/*
78879** Change the P4 operand of the most recently coded instruction
78880** to the value defined by the arguments.  This is a high-speed
78881** version of sqlite3VdbeChangeP4().
78882**
78883** The P4 operand must not have been previously defined.  And the new
78884** P4 must not be P4_INT32.  Use sqlite3VdbeChangeP4() in either of
78885** those cases.
78886*/
78887SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
78888  VdbeOp *pOp;
78889  assert( n!=P4_INT32 && n!=P4_VTAB );
78890  assert( n<=0 );
78891  if( p->db->mallocFailed ){
78892    freeP4(p->db, n, pP4);
78893  }else{
78894    assert( pP4!=0 );
78895    assert( p->nOp>0 );
78896    pOp = &p->aOp[p->nOp-1];
78897    assert( pOp->p4type==P4_NOTUSED );
78898    pOp->p4type = n;
78899    pOp->p4.p = pP4;
78900  }
78901}
78902
78903/*
78904** Set the P4 on the most recently added opcode to the KeyInfo for the
78905** index given.
78906*/
78907SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
78908  Vdbe *v = pParse->pVdbe;
78909  KeyInfo *pKeyInfo;
78910  assert( v!=0 );
78911  assert( pIdx!=0 );
78912  pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx);
78913  if( pKeyInfo ) sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
78914}
78915
78916#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
78917/*
78918** Change the comment on the most recently coded instruction.  Or
78919** insert a No-op and add the comment to that new instruction.  This
78920** makes the code easier to read during debugging.  None of this happens
78921** in a production build.
78922*/
78923static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
78924  assert( p->nOp>0 || p->aOp==0 );
78925  assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed
78926          || p->pParse->nErr>0 );
78927  if( p->nOp ){
78928    assert( p->aOp );
78929    sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
78930    p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
78931  }
78932}
78933SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
78934  va_list ap;
78935  if( p ){
78936    va_start(ap, zFormat);
78937    vdbeVComment(p, zFormat, ap);
78938    va_end(ap);
78939  }
78940}
78941SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
78942  va_list ap;
78943  if( p ){
78944    sqlite3VdbeAddOp0(p, OP_Noop);
78945    va_start(ap, zFormat);
78946    vdbeVComment(p, zFormat, ap);
78947    va_end(ap);
78948  }
78949}
78950#endif  /* NDEBUG */
78951
78952#ifdef SQLITE_VDBE_COVERAGE
78953/*
78954** Set the value if the iSrcLine field for the previously coded instruction.
78955*/
78956SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
78957  sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
78958}
78959#endif /* SQLITE_VDBE_COVERAGE */
78960
78961/*
78962** Return the opcode for a given address.  If the address is -1, then
78963** return the most recently inserted opcode.
78964**
78965** If a memory allocation error has occurred prior to the calling of this
78966** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
78967** is readable but not writable, though it is cast to a writable value.
78968** The return of a dummy opcode allows the call to continue functioning
78969** after an OOM fault without having to check to see if the return from
78970** this routine is a valid pointer.  But because the dummy.opcode is 0,
78971** dummy will never be written to.  This is verified by code inspection and
78972** by running with Valgrind.
78973*/
78974SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
78975  /* C89 specifies that the constant "dummy" will be initialized to all
78976  ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
78977  static VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
78978  assert( p->magic==VDBE_MAGIC_INIT );
78979  if( addr<0 ){
78980    addr = p->nOp - 1;
78981  }
78982  assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
78983  if( p->db->mallocFailed ){
78984    return (VdbeOp*)&dummy;
78985  }else{
78986    return &p->aOp[addr];
78987  }
78988}
78989
78990#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
78991/*
78992** Return an integer value for one of the parameters to the opcode pOp
78993** determined by character c.
78994*/
78995static int translateP(char c, const Op *pOp){
78996  if( c=='1' ) return pOp->p1;
78997  if( c=='2' ) return pOp->p2;
78998  if( c=='3' ) return pOp->p3;
78999  if( c=='4' ) return pOp->p4.i;
79000  return pOp->p5;
79001}
79002
79003/*
79004** Compute a string for the "comment" field of a VDBE opcode listing.
79005**
79006** The Synopsis: field in comments in the vdbe.c source file gets converted
79007** to an extra string that is appended to the sqlite3OpcodeName().  In the
79008** absence of other comments, this synopsis becomes the comment on the opcode.
79009** Some translation occurs:
79010**
79011**       "PX"      ->  "r[X]"
79012**       "PX@PY"   ->  "r[X..X+Y-1]"  or "r[x]" if y is 0 or 1
79013**       "PX@PY+1" ->  "r[X..X+Y]"    or "r[x]" if y is 0
79014**       "PY..PY"  ->  "r[X..Y]"      or "r[x]" if y<=x
79015*/
79016SQLITE_PRIVATE char *sqlite3VdbeDisplayComment(
79017  sqlite3 *db,       /* Optional - Oom error reporting only */
79018  const Op *pOp,     /* The opcode to be commented */
79019  const char *zP4    /* Previously obtained value for P4 */
79020){
79021  const char *zOpName;
79022  const char *zSynopsis;
79023  int nOpName;
79024  int ii;
79025  char zAlt[50];
79026  StrAccum x;
79027
79028  sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH);
79029  zOpName = sqlite3OpcodeName(pOp->opcode);
79030  nOpName = sqlite3Strlen30(zOpName);
79031  if( zOpName[nOpName+1] ){
79032    int seenCom = 0;
79033    char c;
79034    zSynopsis = zOpName += nOpName + 1;
79035    if( strncmp(zSynopsis,"IF ",3)==0 ){
79036      if( pOp->p5 & SQLITE_STOREP2 ){
79037        sqlite3_snprintf(sizeof(zAlt), zAlt, "r[P2] = (%s)", zSynopsis+3);
79038      }else{
79039        sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3);
79040      }
79041      zSynopsis = zAlt;
79042    }
79043    for(ii=0; (c = zSynopsis[ii])!=0; ii++){
79044      if( c=='P' ){
79045        c = zSynopsis[++ii];
79046        if( c=='4' ){
79047          sqlite3_str_appendall(&x, zP4);
79048        }else if( c=='X' ){
79049          sqlite3_str_appendall(&x, pOp->zComment);
79050          seenCom = 1;
79051        }else{
79052          int v1 = translateP(c, pOp);
79053          int v2;
79054          if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
79055            ii += 3;
79056            v2 = translateP(zSynopsis[ii], pOp);
79057            if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
79058              ii += 2;
79059              v2++;
79060            }
79061            if( v2<2 ){
79062              sqlite3_str_appendf(&x, "%d", v1);
79063            }else{
79064              sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
79065            }
79066          }else if( strncmp(zSynopsis+ii+1, "@NP", 3)==0 ){
79067            sqlite3_context *pCtx = pOp->p4.pCtx;
79068            if( pOp->p4type!=P4_FUNCCTX || pCtx->argc==1 ){
79069              sqlite3_str_appendf(&x, "%d", v1);
79070            }else if( pCtx->argc>1 ){
79071              sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
79072            }else{
79073              assert( x.nChar>2 );
79074              x.nChar -= 2;
79075              ii++;
79076            }
79077            ii += 3;
79078          }else{
79079            sqlite3_str_appendf(&x, "%d", v1);
79080            if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
79081              ii += 4;
79082            }
79083          }
79084        }
79085      }else{
79086        sqlite3_str_appendchar(&x, 1, c);
79087      }
79088    }
79089    if( !seenCom && pOp->zComment ){
79090      sqlite3_str_appendf(&x, "; %s", pOp->zComment);
79091    }
79092  }else if( pOp->zComment ){
79093    sqlite3_str_appendall(&x, pOp->zComment);
79094  }
79095  if( (x.accError & SQLITE_NOMEM)!=0 && db!=0 ){
79096    sqlite3OomFault(db);
79097  }
79098  return sqlite3StrAccumFinish(&x);
79099}
79100#endif /* SQLITE_ENABLE_EXPLAIN_COMMENTS */
79101
79102#if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS)
79103/*
79104** Translate the P4.pExpr value for an OP_CursorHint opcode into text
79105** that can be displayed in the P4 column of EXPLAIN output.
79106*/
79107static void displayP4Expr(StrAccum *p, Expr *pExpr){
79108  const char *zOp = 0;
79109  switch( pExpr->op ){
79110    case TK_STRING:
79111      sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
79112      break;
79113    case TK_INTEGER:
79114      sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
79115      break;
79116    case TK_NULL:
79117      sqlite3_str_appendf(p, "NULL");
79118      break;
79119    case TK_REGISTER: {
79120      sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
79121      break;
79122    }
79123    case TK_COLUMN: {
79124      if( pExpr->iColumn<0 ){
79125        sqlite3_str_appendf(p, "rowid");
79126      }else{
79127        sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
79128      }
79129      break;
79130    }
79131    case TK_LT:      zOp = "LT";      break;
79132    case TK_LE:      zOp = "LE";      break;
79133    case TK_GT:      zOp = "GT";      break;
79134    case TK_GE:      zOp = "GE";      break;
79135    case TK_NE:      zOp = "NE";      break;
79136    case TK_EQ:      zOp = "EQ";      break;
79137    case TK_IS:      zOp = "IS";      break;
79138    case TK_ISNOT:   zOp = "ISNOT";   break;
79139    case TK_AND:     zOp = "AND";     break;
79140    case TK_OR:      zOp = "OR";      break;
79141    case TK_PLUS:    zOp = "ADD";     break;
79142    case TK_STAR:    zOp = "MUL";     break;
79143    case TK_MINUS:   zOp = "SUB";     break;
79144    case TK_REM:     zOp = "REM";     break;
79145    case TK_BITAND:  zOp = "BITAND";  break;
79146    case TK_BITOR:   zOp = "BITOR";   break;
79147    case TK_SLASH:   zOp = "DIV";     break;
79148    case TK_LSHIFT:  zOp = "LSHIFT";  break;
79149    case TK_RSHIFT:  zOp = "RSHIFT";  break;
79150    case TK_CONCAT:  zOp = "CONCAT";  break;
79151    case TK_UMINUS:  zOp = "MINUS";   break;
79152    case TK_UPLUS:   zOp = "PLUS";    break;
79153    case TK_BITNOT:  zOp = "BITNOT";  break;
79154    case TK_NOT:     zOp = "NOT";     break;
79155    case TK_ISNULL:  zOp = "ISNULL";  break;
79156    case TK_NOTNULL: zOp = "NOTNULL"; break;
79157
79158    default:
79159      sqlite3_str_appendf(p, "%s", "expr");
79160      break;
79161  }
79162
79163  if( zOp ){
79164    sqlite3_str_appendf(p, "%s(", zOp);
79165    displayP4Expr(p, pExpr->pLeft);
79166    if( pExpr->pRight ){
79167      sqlite3_str_append(p, ",", 1);
79168      displayP4Expr(p, pExpr->pRight);
79169    }
79170    sqlite3_str_append(p, ")", 1);
79171  }
79172}
79173#endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
79174
79175
79176#if VDBE_DISPLAY_P4
79177/*
79178** Compute a string that describes the P4 parameter for an opcode.
79179** Use zTemp for any required temporary buffer space.
79180*/
79181SQLITE_PRIVATE char *sqlite3VdbeDisplayP4(sqlite3 *db, Op *pOp){
79182  char *zP4 = 0;
79183  StrAccum x;
79184
79185  sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH);
79186  switch( pOp->p4type ){
79187    case P4_KEYINFO: {
79188      int j;
79189      KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
79190      assert( pKeyInfo->aSortFlags!=0 );
79191      sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
79192      for(j=0; j<pKeyInfo->nKeyField; j++){
79193        CollSeq *pColl = pKeyInfo->aColl[j];
79194        const char *zColl = pColl ? pColl->zName : "";
79195        if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
79196        sqlite3_str_appendf(&x, ",%s%s%s",
79197               (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "",
79198               (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "",
79199               zColl);
79200      }
79201      sqlite3_str_append(&x, ")", 1);
79202      break;
79203    }
79204#ifdef SQLITE_ENABLE_CURSOR_HINTS
79205    case P4_EXPR: {
79206      displayP4Expr(&x, pOp->p4.pExpr);
79207      break;
79208    }
79209#endif
79210    case P4_COLLSEQ: {
79211      static const char *const encnames[] = {"?", "8", "16LE", "16BE"};
79212      CollSeq *pColl = pOp->p4.pColl;
79213      assert( pColl->enc>=0 && pColl->enc<4 );
79214      sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
79215                          encnames[pColl->enc]);
79216      break;
79217    }
79218    case P4_FUNCDEF: {
79219      FuncDef *pDef = pOp->p4.pFunc;
79220      sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
79221      break;
79222    }
79223    case P4_FUNCCTX: {
79224      FuncDef *pDef = pOp->p4.pCtx->pFunc;
79225      sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
79226      break;
79227    }
79228    case P4_INT64: {
79229      sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
79230      break;
79231    }
79232    case P4_INT32: {
79233      sqlite3_str_appendf(&x, "%d", pOp->p4.i);
79234      break;
79235    }
79236    case P4_REAL: {
79237      sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
79238      break;
79239    }
79240    case P4_MEM: {
79241      Mem *pMem = pOp->p4.pMem;
79242      if( pMem->flags & MEM_Str ){
79243        zP4 = pMem->z;
79244      }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
79245        sqlite3_str_appendf(&x, "%lld", pMem->u.i);
79246      }else if( pMem->flags & MEM_Real ){
79247        sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
79248      }else if( pMem->flags & MEM_Null ){
79249        zP4 = "NULL";
79250      }else{
79251        assert( pMem->flags & MEM_Blob );
79252        zP4 = "(blob)";
79253      }
79254      break;
79255    }
79256#ifndef SQLITE_OMIT_VIRTUALTABLE
79257    case P4_VTAB: {
79258      sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
79259      sqlite3_str_appendf(&x, "vtab:%p", pVtab);
79260      break;
79261    }
79262#endif
79263    case P4_INTARRAY: {
79264      int i;
79265      int *ai = pOp->p4.ai;
79266      int n = ai[0];   /* The first element of an INTARRAY is always the
79267                       ** count of the number of elements to follow */
79268      for(i=1; i<=n; i++){
79269        sqlite3_str_appendf(&x, "%c%d", (i==1 ? '[' : ','), ai[i]);
79270      }
79271      sqlite3_str_append(&x, "]", 1);
79272      break;
79273    }
79274    case P4_SUBPROGRAM: {
79275      zP4 = "program";
79276      break;
79277    }
79278    case P4_DYNBLOB:
79279    case P4_ADVANCE: {
79280      break;
79281    }
79282    case P4_TABLE: {
79283      zP4 = pOp->p4.pTab->zName;
79284      break;
79285    }
79286    default: {
79287      zP4 = pOp->p4.z;
79288    }
79289  }
79290  if( zP4 ) sqlite3_str_appendall(&x, zP4);
79291  if( (x.accError & SQLITE_NOMEM)!=0 ){
79292    sqlite3OomFault(db);
79293  }
79294  return sqlite3StrAccumFinish(&x);
79295}
79296#endif /* VDBE_DISPLAY_P4 */
79297
79298/*
79299** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
79300**
79301** The prepared statements need to know in advance the complete set of
79302** attached databases that will be use.  A mask of these databases
79303** is maintained in p->btreeMask.  The p->lockMask value is the subset of
79304** p->btreeMask of databases that will require a lock.
79305*/
79306SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
79307  assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
79308  assert( i<(int)sizeof(p->btreeMask)*8 );
79309  DbMaskSet(p->btreeMask, i);
79310  if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
79311    DbMaskSet(p->lockMask, i);
79312  }
79313}
79314
79315#if !defined(SQLITE_OMIT_SHARED_CACHE)
79316/*
79317** If SQLite is compiled to support shared-cache mode and to be threadsafe,
79318** this routine obtains the mutex associated with each BtShared structure
79319** that may be accessed by the VM passed as an argument. In doing so it also
79320** sets the BtShared.db member of each of the BtShared structures, ensuring
79321** that the correct busy-handler callback is invoked if required.
79322**
79323** If SQLite is not threadsafe but does support shared-cache mode, then
79324** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
79325** of all of BtShared structures accessible via the database handle
79326** associated with the VM.
79327**
79328** If SQLite is not threadsafe and does not support shared-cache mode, this
79329** function is a no-op.
79330**
79331** The p->btreeMask field is a bitmask of all btrees that the prepared
79332** statement p will ever use.  Let N be the number of bits in p->btreeMask
79333** corresponding to btrees that use shared cache.  Then the runtime of
79334** this routine is N*N.  But as N is rarely more than 1, this should not
79335** be a problem.
79336*/
79337SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
79338  int i;
79339  sqlite3 *db;
79340  Db *aDb;
79341  int nDb;
79342  if( DbMaskAllZero(p->lockMask) ) return;  /* The common case */
79343  db = p->db;
79344  aDb = db->aDb;
79345  nDb = db->nDb;
79346  for(i=0; i<nDb; i++){
79347    if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
79348      sqlite3BtreeEnter(aDb[i].pBt);
79349    }
79350  }
79351}
79352#endif
79353
79354#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
79355/*
79356** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
79357*/
79358static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
79359  int i;
79360  sqlite3 *db;
79361  Db *aDb;
79362  int nDb;
79363  db = p->db;
79364  aDb = db->aDb;
79365  nDb = db->nDb;
79366  for(i=0; i<nDb; i++){
79367    if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
79368      sqlite3BtreeLeave(aDb[i].pBt);
79369    }
79370  }
79371}
79372SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
79373  if( DbMaskAllZero(p->lockMask) ) return;  /* The common case */
79374  vdbeLeave(p);
79375}
79376#endif
79377
79378#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
79379/*
79380** Print a single opcode.  This routine is used for debugging only.
79381*/
79382SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){
79383  char *zP4;
79384  char *zCom;
79385  sqlite3 dummyDb;
79386  static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
79387  if( pOut==0 ) pOut = stdout;
79388  sqlite3BeginBenignMalloc();
79389  dummyDb.mallocFailed = 1;
79390  zP4 = sqlite3VdbeDisplayP4(&dummyDb, pOp);
79391#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
79392  zCom = sqlite3VdbeDisplayComment(0, pOp, zP4);
79393#else
79394  zCom = 0;
79395#endif
79396  /* NB:  The sqlite3OpcodeName() function is implemented by code created
79397  ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
79398  ** information from the vdbe.c source text */
79399  fprintf(pOut, zFormat1, pc,
79400      sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
79401      zP4 ? zP4 : "", pOp->p5,
79402      zCom ? zCom : ""
79403  );
79404  fflush(pOut);
79405  sqlite3_free(zP4);
79406  sqlite3_free(zCom);
79407  sqlite3EndBenignMalloc();
79408}
79409#endif
79410
79411/*
79412** Initialize an array of N Mem element.
79413*/
79414static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
79415  while( (N--)>0 ){
79416    p->db = db;
79417    p->flags = flags;
79418    p->szMalloc = 0;
79419#ifdef SQLITE_DEBUG
79420    p->pScopyFrom = 0;
79421#endif
79422    p++;
79423  }
79424}
79425
79426/*
79427** Release an array of N Mem elements
79428*/
79429static void releaseMemArray(Mem *p, int N){
79430  if( p && N ){
79431    Mem *pEnd = &p[N];
79432    sqlite3 *db = p->db;
79433    if( db->pnBytesFreed ){
79434      do{
79435        if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
79436      }while( (++p)<pEnd );
79437      return;
79438    }
79439    do{
79440      assert( (&p[1])==pEnd || p[0].db==p[1].db );
79441      assert( sqlite3VdbeCheckMemInvariants(p) );
79442
79443      /* This block is really an inlined version of sqlite3VdbeMemRelease()
79444      ** that takes advantage of the fact that the memory cell value is
79445      ** being set to NULL after releasing any dynamic resources.
79446      **
79447      ** The justification for duplicating code is that according to
79448      ** callgrind, this causes a certain test case to hit the CPU 4.7
79449      ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
79450      ** sqlite3MemRelease() were called from here. With -O2, this jumps
79451      ** to 6.6 percent. The test case is inserting 1000 rows into a table
79452      ** with no indexes using a single prepared INSERT statement, bind()
79453      ** and reset(). Inserts are grouped into a transaction.
79454      */
79455      testcase( p->flags & MEM_Agg );
79456      testcase( p->flags & MEM_Dyn );
79457      testcase( p->xDel==sqlite3VdbeFrameMemDel );
79458      if( p->flags&(MEM_Agg|MEM_Dyn) ){
79459        sqlite3VdbeMemRelease(p);
79460      }else if( p->szMalloc ){
79461        sqlite3DbFreeNN(db, p->zMalloc);
79462        p->szMalloc = 0;
79463      }
79464
79465      p->flags = MEM_Undefined;
79466    }while( (++p)<pEnd );
79467  }
79468}
79469
79470#ifdef SQLITE_DEBUG
79471/*
79472** Verify that pFrame is a valid VdbeFrame pointer.  Return true if it is
79473** and false if something is wrong.
79474**
79475** This routine is intended for use inside of assert() statements only.
79476*/
79477SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame *pFrame){
79478  if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
79479  return 1;
79480}
79481#endif
79482
79483
79484/*
79485** This is a destructor on a Mem object (which is really an sqlite3_value)
79486** that deletes the Frame object that is attached to it as a blob.
79487**
79488** This routine does not delete the Frame right away.  It merely adds the
79489** frame to a list of frames to be deleted when the Vdbe halts.
79490*/
79491SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void *pArg){
79492  VdbeFrame *pFrame = (VdbeFrame*)pArg;
79493  assert( sqlite3VdbeFrameIsValid(pFrame) );
79494  pFrame->pParent = pFrame->v->pDelFrame;
79495  pFrame->v->pDelFrame = pFrame;
79496}
79497
79498#if defined(SQLITE_ENABLE_BYTECODE_VTAB) || !defined(SQLITE_OMIT_EXPLAIN)
79499/*
79500** Locate the next opcode to be displayed in EXPLAIN or EXPLAIN
79501** QUERY PLAN output.
79502**
79503** Return SQLITE_ROW on success.  Return SQLITE_DONE if there are no
79504** more opcodes to be displayed.
79505*/
79506SQLITE_PRIVATE int sqlite3VdbeNextOpcode(
79507  Vdbe *p,         /* The statement being explained */
79508  Mem *pSub,       /* Storage for keeping track of subprogram nesting */
79509  int eMode,       /* 0: normal.  1: EQP.  2:  TablesUsed */
79510  int *piPc,       /* IN/OUT: Current rowid.  Overwritten with next rowid */
79511  int *piAddr,     /* OUT: Write index into (*paOp)[] here */
79512  Op **paOp        /* OUT: Write the opcode array here */
79513){
79514  int nRow;                            /* Stop when row count reaches this */
79515  int nSub = 0;                        /* Number of sub-vdbes seen so far */
79516  SubProgram **apSub = 0;              /* Array of sub-vdbes */
79517  int i;                               /* Next instruction address */
79518  int rc = SQLITE_OK;                  /* Result code */
79519  Op *aOp = 0;                         /* Opcode array */
79520  int iPc;                             /* Rowid.  Copy of value in *piPc */
79521
79522  /* When the number of output rows reaches nRow, that means the
79523  ** listing has finished and sqlite3_step() should return SQLITE_DONE.
79524  ** nRow is the sum of the number of rows in the main program, plus
79525  ** the sum of the number of rows in all trigger subprograms encountered
79526  ** so far.  The nRow value will increase as new trigger subprograms are
79527  ** encountered, but p->pc will eventually catch up to nRow.
79528  */
79529  nRow = p->nOp;
79530  if( pSub!=0 ){
79531    if( pSub->flags&MEM_Blob ){
79532      /* pSub is initiallly NULL.  It is initialized to a BLOB by
79533      ** the P4_SUBPROGRAM processing logic below */
79534      nSub = pSub->n/sizeof(Vdbe*);
79535      apSub = (SubProgram **)pSub->z;
79536    }
79537    for(i=0; i<nSub; i++){
79538      nRow += apSub[i]->nOp;
79539    }
79540  }
79541  iPc = *piPc;
79542  while(1){  /* Loop exits via break */
79543    i = iPc++;
79544    if( i>=nRow ){
79545      p->rc = SQLITE_OK;
79546      rc = SQLITE_DONE;
79547      break;
79548    }
79549    if( i<p->nOp ){
79550      /* The rowid is small enough that we are still in the
79551      ** main program. */
79552      aOp = p->aOp;
79553    }else{
79554      /* We are currently listing subprograms.  Figure out which one and
79555      ** pick up the appropriate opcode. */
79556      int j;
79557      i -= p->nOp;
79558      assert( apSub!=0 );
79559      assert( nSub>0 );
79560      for(j=0; i>=apSub[j]->nOp; j++){
79561        i -= apSub[j]->nOp;
79562        assert( i<apSub[j]->nOp || j+1<nSub );
79563      }
79564      aOp = apSub[j]->aOp;
79565    }
79566
79567    /* When an OP_Program opcode is encounter (the only opcode that has
79568    ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
79569    ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
79570    ** has not already been seen.
79571    */
79572    if( pSub!=0 && aOp[i].p4type==P4_SUBPROGRAM ){
79573      int nByte = (nSub+1)*sizeof(SubProgram*);
79574      int j;
79575      for(j=0; j<nSub; j++){
79576        if( apSub[j]==aOp[i].p4.pProgram ) break;
79577      }
79578      if( j==nSub ){
79579        p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
79580        if( p->rc!=SQLITE_OK ){
79581          rc = SQLITE_ERROR;
79582          break;
79583        }
79584        apSub = (SubProgram **)pSub->z;
79585        apSub[nSub++] = aOp[i].p4.pProgram;
79586        MemSetTypeFlag(pSub, MEM_Blob);
79587        pSub->n = nSub*sizeof(SubProgram*);
79588        nRow += aOp[i].p4.pProgram->nOp;
79589      }
79590    }
79591    if( eMode==0 ) break;
79592#ifdef SQLITE_ENABLE_BYTECODE_VTAB
79593    if( eMode==2 ){
79594      Op *pOp = aOp + i;
79595      if( pOp->opcode==OP_OpenRead ) break;
79596      if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break;
79597      if( pOp->opcode==OP_ReopenIdx ) break;
79598    }else
79599#endif
79600    {
79601      assert( eMode==1 );
79602      if( aOp[i].opcode==OP_Explain ) break;
79603      if( aOp[i].opcode==OP_Init && iPc>1 ) break;
79604    }
79605  }
79606  *piPc = iPc;
79607  *piAddr = i;
79608  *paOp = aOp;
79609  return rc;
79610}
79611#endif /* SQLITE_ENABLE_BYTECODE_VTAB || !SQLITE_OMIT_EXPLAIN */
79612
79613
79614/*
79615** Delete a VdbeFrame object and its contents. VdbeFrame objects are
79616** allocated by the OP_Program opcode in sqlite3VdbeExec().
79617*/
79618SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
79619  int i;
79620  Mem *aMem = VdbeFrameMem(p);
79621  VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
79622  assert( sqlite3VdbeFrameIsValid(p) );
79623  for(i=0; i<p->nChildCsr; i++){
79624    sqlite3VdbeFreeCursor(p->v, apCsr[i]);
79625  }
79626  releaseMemArray(aMem, p->nChildMem);
79627  sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
79628  sqlite3DbFree(p->v->db, p);
79629}
79630
79631#ifndef SQLITE_OMIT_EXPLAIN
79632/*
79633** Give a listing of the program in the virtual machine.
79634**
79635** The interface is the same as sqlite3VdbeExec().  But instead of
79636** running the code, it invokes the callback once for each instruction.
79637** This feature is used to implement "EXPLAIN".
79638**
79639** When p->explain==1, each instruction is listed.  When
79640** p->explain==2, only OP_Explain instructions are listed and these
79641** are shown in a different format.  p->explain==2 is used to implement
79642** EXPLAIN QUERY PLAN.
79643** 2018-04-24:  In p->explain==2 mode, the OP_Init opcodes of triggers
79644** are also shown, so that the boundaries between the main program and
79645** each trigger are clear.
79646**
79647** When p->explain==1, first the main program is listed, then each of
79648** the trigger subprograms are listed one by one.
79649*/
79650SQLITE_PRIVATE int sqlite3VdbeList(
79651  Vdbe *p                   /* The VDBE */
79652){
79653  Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
79654  sqlite3 *db = p->db;                 /* The database connection */
79655  int i;                               /* Loop counter */
79656  int rc = SQLITE_OK;                  /* Return code */
79657  Mem *pMem = &p->aMem[1];             /* First Mem of result set */
79658  int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
79659  Op *aOp;                             /* Array of opcodes */
79660  Op *pOp;                             /* Current opcode */
79661
79662  assert( p->explain );
79663  assert( p->magic==VDBE_MAGIC_RUN );
79664  assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
79665
79666  /* Even though this opcode does not use dynamic strings for
79667  ** the result, result columns may become dynamic if the user calls
79668  ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
79669  */
79670  releaseMemArray(pMem, 8);
79671  p->pResultSet = 0;
79672
79673  if( p->rc==SQLITE_NOMEM ){
79674    /* This happens if a malloc() inside a call to sqlite3_column_text() or
79675    ** sqlite3_column_text16() failed.  */
79676    sqlite3OomFault(db);
79677    return SQLITE_ERROR;
79678  }
79679
79680  if( bListSubprogs ){
79681    /* The first 8 memory cells are used for the result set.  So we will
79682    ** commandeer the 9th cell to use as storage for an array of pointers
79683    ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
79684    ** cells.  */
79685    assert( p->nMem>9 );
79686    pSub = &p->aMem[9];
79687  }else{
79688    pSub = 0;
79689  }
79690
79691  /* Figure out which opcode is next to display */
79692  rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp);
79693
79694  if( rc==SQLITE_OK ){
79695    pOp = aOp + i;
79696    if( AtomicLoad(&db->u1.isInterrupted) ){
79697      p->rc = SQLITE_INTERRUPT;
79698      rc = SQLITE_ERROR;
79699      sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
79700    }else{
79701      char *zP4 = sqlite3VdbeDisplayP4(db, pOp);
79702      if( p->explain==2 ){
79703        sqlite3VdbeMemSetInt64(pMem, pOp->p1);
79704        sqlite3VdbeMemSetInt64(pMem+1, pOp->p2);
79705        sqlite3VdbeMemSetInt64(pMem+2, pOp->p3);
79706        sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF8, sqlite3_free);
79707        p->nResColumn = 4;
79708      }else{
79709        sqlite3VdbeMemSetInt64(pMem+0, i);
79710        sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode),
79711                             -1, SQLITE_UTF8, SQLITE_STATIC);
79712        sqlite3VdbeMemSetInt64(pMem+2, pOp->p1);
79713        sqlite3VdbeMemSetInt64(pMem+3, pOp->p2);
79714        sqlite3VdbeMemSetInt64(pMem+4, pOp->p3);
79715        /* pMem+5 for p4 is done last */
79716        sqlite3VdbeMemSetInt64(pMem+6, pOp->p5);
79717#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
79718        {
79719          char *zCom = sqlite3VdbeDisplayComment(db, pOp, zP4);
79720          sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free);
79721        }
79722#else
79723        sqlite3VdbeMemSetNull(pMem+7);
79724#endif
79725        sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF8, sqlite3_free);
79726        p->nResColumn = 8;
79727      }
79728      p->pResultSet = pMem;
79729      if( db->mallocFailed ){
79730        p->rc = SQLITE_NOMEM;
79731        rc = SQLITE_ERROR;
79732      }else{
79733        p->rc = SQLITE_OK;
79734        rc = SQLITE_ROW;
79735      }
79736    }
79737  }
79738  return rc;
79739}
79740#endif /* SQLITE_OMIT_EXPLAIN */
79741
79742#ifdef SQLITE_DEBUG
79743/*
79744** Print the SQL that was used to generate a VDBE program.
79745*/
79746SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
79747  const char *z = 0;
79748  if( p->zSql ){
79749    z = p->zSql;
79750  }else if( p->nOp>=1 ){
79751    const VdbeOp *pOp = &p->aOp[0];
79752    if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
79753      z = pOp->p4.z;
79754      while( sqlite3Isspace(*z) ) z++;
79755    }
79756  }
79757  if( z ) printf("SQL: [%s]\n", z);
79758}
79759#endif
79760
79761#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
79762/*
79763** Print an IOTRACE message showing SQL content.
79764*/
79765SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
79766  int nOp = p->nOp;
79767  VdbeOp *pOp;
79768  if( sqlite3IoTrace==0 ) return;
79769  if( nOp<1 ) return;
79770  pOp = &p->aOp[0];
79771  if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
79772    int i, j;
79773    char z[1000];
79774    sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
79775    for(i=0; sqlite3Isspace(z[i]); i++){}
79776    for(j=0; z[i]; i++){
79777      if( sqlite3Isspace(z[i]) ){
79778        if( z[i-1]!=' ' ){
79779          z[j++] = ' ';
79780        }
79781      }else{
79782        z[j++] = z[i];
79783      }
79784    }
79785    z[j] = 0;
79786    sqlite3IoTrace("SQL %s\n", z);
79787  }
79788}
79789#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
79790
79791/* An instance of this object describes bulk memory available for use
79792** by subcomponents of a prepared statement.  Space is allocated out
79793** of a ReusableSpace object by the allocSpace() routine below.
79794*/
79795struct ReusableSpace {
79796  u8 *pSpace;            /* Available memory */
79797  sqlite3_int64 nFree;   /* Bytes of available memory */
79798  sqlite3_int64 nNeeded; /* Total bytes that could not be allocated */
79799};
79800
79801/* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
79802** from the ReusableSpace object.  Return a pointer to the allocated
79803** memory on success.  If insufficient memory is available in the
79804** ReusableSpace object, increase the ReusableSpace.nNeeded
79805** value by the amount needed and return NULL.
79806**
79807** If pBuf is not initially NULL, that means that the memory has already
79808** been allocated by a prior call to this routine, so just return a copy
79809** of pBuf and leave ReusableSpace unchanged.
79810**
79811** This allocator is employed to repurpose unused slots at the end of the
79812** opcode array of prepared state for other memory needs of the prepared
79813** statement.
79814*/
79815static void *allocSpace(
79816  struct ReusableSpace *p,  /* Bulk memory available for allocation */
79817  void *pBuf,               /* Pointer to a prior allocation */
79818  sqlite3_int64 nByte       /* Bytes of memory needed */
79819){
79820  assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
79821  if( pBuf==0 ){
79822    nByte = ROUND8(nByte);
79823    if( nByte <= p->nFree ){
79824      p->nFree -= nByte;
79825      pBuf = &p->pSpace[p->nFree];
79826    }else{
79827      p->nNeeded += nByte;
79828    }
79829  }
79830  assert( EIGHT_BYTE_ALIGNMENT(pBuf) );
79831  return pBuf;
79832}
79833
79834/*
79835** Rewind the VDBE back to the beginning in preparation for
79836** running it.
79837*/
79838SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
79839#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
79840  int i;
79841#endif
79842  assert( p!=0 );
79843  assert( p->magic==VDBE_MAGIC_INIT || p->magic==VDBE_MAGIC_RESET );
79844
79845  /* There should be at least one opcode.
79846  */
79847  assert( p->nOp>0 );
79848
79849  /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
79850  p->magic = VDBE_MAGIC_RUN;
79851
79852#ifdef SQLITE_DEBUG
79853  for(i=0; i<p->nMem; i++){
79854    assert( p->aMem[i].db==p->db );
79855  }
79856#endif
79857  p->pc = -1;
79858  p->rc = SQLITE_OK;
79859  p->errorAction = OE_Abort;
79860  p->nChange = 0;
79861  p->cacheCtr = 1;
79862  p->minWriteFileFormat = 255;
79863  p->iStatement = 0;
79864  p->nFkConstraint = 0;
79865#ifdef VDBE_PROFILE
79866  for(i=0; i<p->nOp; i++){
79867    p->aOp[i].cnt = 0;
79868    p->aOp[i].cycles = 0;
79869  }
79870#endif
79871}
79872
79873/*
79874** Prepare a virtual machine for execution for the first time after
79875** creating the virtual machine.  This involves things such
79876** as allocating registers and initializing the program counter.
79877** After the VDBE has be prepped, it can be executed by one or more
79878** calls to sqlite3VdbeExec().
79879**
79880** This function may be called exactly once on each virtual machine.
79881** After this routine is called the VM has been "packaged" and is ready
79882** to run.  After this routine is called, further calls to
79883** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
79884** the Vdbe from the Parse object that helped generate it so that the
79885** the Vdbe becomes an independent entity and the Parse object can be
79886** destroyed.
79887**
79888** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
79889** to its initial state after it has been run.
79890*/
79891SQLITE_PRIVATE void sqlite3VdbeMakeReady(
79892  Vdbe *p,                       /* The VDBE */
79893  Parse *pParse                  /* Parsing context */
79894){
79895  sqlite3 *db;                   /* The database connection */
79896  int nVar;                      /* Number of parameters */
79897  int nMem;                      /* Number of VM memory registers */
79898  int nCursor;                   /* Number of cursors required */
79899  int nArg;                      /* Number of arguments in subprograms */
79900  int n;                         /* Loop counter */
79901  struct ReusableSpace x;        /* Reusable bulk memory */
79902
79903  assert( p!=0 );
79904  assert( p->nOp>0 );
79905  assert( pParse!=0 );
79906  assert( p->magic==VDBE_MAGIC_INIT );
79907  assert( pParse==p->pParse );
79908  db = p->db;
79909  assert( db->mallocFailed==0 );
79910  nVar = pParse->nVar;
79911  nMem = pParse->nMem;
79912  nCursor = pParse->nTab;
79913  nArg = pParse->nMaxArg;
79914
79915  /* Each cursor uses a memory cell.  The first cursor (cursor 0) can
79916  ** use aMem[0] which is not otherwise used by the VDBE program.  Allocate
79917  ** space at the end of aMem[] for cursors 1 and greater.
79918  ** See also: allocateCursor().
79919  */
79920  nMem += nCursor;
79921  if( nCursor==0 && nMem>0 ) nMem++;  /* Space for aMem[0] even if not used */
79922
79923  /* Figure out how much reusable memory is available at the end of the
79924  ** opcode array.  This extra memory will be reallocated for other elements
79925  ** of the prepared statement.
79926  */
79927  n = ROUND8(sizeof(Op)*p->nOp);              /* Bytes of opcode memory used */
79928  x.pSpace = &((u8*)p->aOp)[n];               /* Unused opcode memory */
79929  assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) );
79930  x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n);  /* Bytes of unused memory */
79931  assert( x.nFree>=0 );
79932  assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) );
79933
79934  resolveP2Values(p, &nArg);
79935  p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
79936  if( pParse->explain ){
79937    static const char * const azColName[] = {
79938       "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
79939       "id", "parent", "notused", "detail"
79940    };
79941    int iFirst, mx, i;
79942    if( nMem<10 ) nMem = 10;
79943    p->explain = pParse->explain;
79944    if( pParse->explain==2 ){
79945      sqlite3VdbeSetNumCols(p, 4);
79946      iFirst = 8;
79947      mx = 12;
79948    }else{
79949      sqlite3VdbeSetNumCols(p, 8);
79950      iFirst = 0;
79951      mx = 8;
79952    }
79953    for(i=iFirst; i<mx; i++){
79954      sqlite3VdbeSetColName(p, i-iFirst, COLNAME_NAME,
79955                            azColName[i], SQLITE_STATIC);
79956    }
79957  }
79958  p->expired = 0;
79959
79960  /* Memory for registers, parameters, cursor, etc, is allocated in one or two
79961  ** passes.  On the first pass, we try to reuse unused memory at the
79962  ** end of the opcode array.  If we are unable to satisfy all memory
79963  ** requirements by reusing the opcode array tail, then the second
79964  ** pass will fill in the remainder using a fresh memory allocation.
79965  **
79966  ** This two-pass approach that reuses as much memory as possible from
79967  ** the leftover memory at the end of the opcode array.  This can significantly
79968  ** reduce the amount of memory held by a prepared statement.
79969  */
79970  x.nNeeded = 0;
79971  p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
79972  p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
79973  p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
79974  p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
79975#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
79976  p->anExec = allocSpace(&x, 0, p->nOp*sizeof(i64));
79977#endif
79978  if( x.nNeeded ){
79979    x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
79980    x.nFree = x.nNeeded;
79981    if( !db->mallocFailed ){
79982      p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
79983      p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
79984      p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
79985      p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
79986#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
79987      p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
79988#endif
79989    }
79990  }
79991
79992  p->pVList = pParse->pVList;
79993  pParse->pVList =  0;
79994  if( db->mallocFailed ){
79995    p->nVar = 0;
79996    p->nCursor = 0;
79997    p->nMem = 0;
79998  }else{
79999    p->nCursor = nCursor;
80000    p->nVar = (ynVar)nVar;
80001    initMemArray(p->aVar, nVar, db, MEM_Null);
80002    p->nMem = nMem;
80003    initMemArray(p->aMem, nMem, db, MEM_Undefined);
80004    memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
80005#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
80006    memset(p->anExec, 0, p->nOp*sizeof(i64));
80007#endif
80008  }
80009  sqlite3VdbeRewind(p);
80010}
80011
80012/*
80013** Close a VDBE cursor and release all the resources that cursor
80014** happens to hold.
80015*/
80016SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
80017  if( pCx==0 ){
80018    return;
80019  }
80020  assert( pCx->pBtx==0 || pCx->eCurType==CURTYPE_BTREE );
80021  switch( pCx->eCurType ){
80022    case CURTYPE_SORTER: {
80023      sqlite3VdbeSorterClose(p->db, pCx);
80024      break;
80025    }
80026    case CURTYPE_BTREE: {
80027      if( pCx->isEphemeral ){
80028        if( pCx->pBtx ) sqlite3BtreeClose(pCx->pBtx);
80029        /* The pCx->pCursor will be close automatically, if it exists, by
80030        ** the call above. */
80031      }else{
80032        assert( pCx->uc.pCursor!=0 );
80033        sqlite3BtreeCloseCursor(pCx->uc.pCursor);
80034      }
80035      break;
80036    }
80037#ifndef SQLITE_OMIT_VIRTUALTABLE
80038    case CURTYPE_VTAB: {
80039      sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
80040      const sqlite3_module *pModule = pVCur->pVtab->pModule;
80041      assert( pVCur->pVtab->nRef>0 );
80042      pVCur->pVtab->nRef--;
80043      pModule->xClose(pVCur);
80044      break;
80045    }
80046#endif
80047  }
80048}
80049
80050/*
80051** Close all cursors in the current frame.
80052*/
80053static void closeCursorsInFrame(Vdbe *p){
80054  if( p->apCsr ){
80055    int i;
80056    for(i=0; i<p->nCursor; i++){
80057      VdbeCursor *pC = p->apCsr[i];
80058      if( pC ){
80059        sqlite3VdbeFreeCursor(p, pC);
80060        p->apCsr[i] = 0;
80061      }
80062    }
80063  }
80064}
80065
80066/*
80067** Copy the values stored in the VdbeFrame structure to its Vdbe. This
80068** is used, for example, when a trigger sub-program is halted to restore
80069** control to the main program.
80070*/
80071SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
80072  Vdbe *v = pFrame->v;
80073  closeCursorsInFrame(v);
80074#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
80075  v->anExec = pFrame->anExec;
80076#endif
80077  v->aOp = pFrame->aOp;
80078  v->nOp = pFrame->nOp;
80079  v->aMem = pFrame->aMem;
80080  v->nMem = pFrame->nMem;
80081  v->apCsr = pFrame->apCsr;
80082  v->nCursor = pFrame->nCursor;
80083  v->db->lastRowid = pFrame->lastRowid;
80084  v->nChange = pFrame->nChange;
80085  v->db->nChange = pFrame->nDbChange;
80086  sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
80087  v->pAuxData = pFrame->pAuxData;
80088  pFrame->pAuxData = 0;
80089  return pFrame->pc;
80090}
80091
80092/*
80093** Close all cursors.
80094**
80095** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
80096** cell array. This is necessary as the memory cell array may contain
80097** pointers to VdbeFrame objects, which may in turn contain pointers to
80098** open cursors.
80099*/
80100static void closeAllCursors(Vdbe *p){
80101  if( p->pFrame ){
80102    VdbeFrame *pFrame;
80103    for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
80104    sqlite3VdbeFrameRestore(pFrame);
80105    p->pFrame = 0;
80106    p->nFrame = 0;
80107  }
80108  assert( p->nFrame==0 );
80109  closeCursorsInFrame(p);
80110  if( p->aMem ){
80111    releaseMemArray(p->aMem, p->nMem);
80112  }
80113  while( p->pDelFrame ){
80114    VdbeFrame *pDel = p->pDelFrame;
80115    p->pDelFrame = pDel->pParent;
80116    sqlite3VdbeFrameDelete(pDel);
80117  }
80118
80119  /* Delete any auxdata allocations made by the VM */
80120  if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
80121  assert( p->pAuxData==0 );
80122}
80123
80124/*
80125** Set the number of result columns that will be returned by this SQL
80126** statement. This is now set at compile time, rather than during
80127** execution of the vdbe program so that sqlite3_column_count() can
80128** be called on an SQL statement before sqlite3_step().
80129*/
80130SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
80131  int n;
80132  sqlite3 *db = p->db;
80133
80134  if( p->nResColumn ){
80135    releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
80136    sqlite3DbFree(db, p->aColName);
80137  }
80138  n = nResColumn*COLNAME_N;
80139  p->nResColumn = (u16)nResColumn;
80140  p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
80141  if( p->aColName==0 ) return;
80142  initMemArray(p->aColName, n, db, MEM_Null);
80143}
80144
80145/*
80146** Set the name of the idx'th column to be returned by the SQL statement.
80147** zName must be a pointer to a nul terminated string.
80148**
80149** This call must be made after a call to sqlite3VdbeSetNumCols().
80150**
80151** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
80152** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
80153** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
80154*/
80155SQLITE_PRIVATE int sqlite3VdbeSetColName(
80156  Vdbe *p,                         /* Vdbe being configured */
80157  int idx,                         /* Index of column zName applies to */
80158  int var,                         /* One of the COLNAME_* constants */
80159  const char *zName,               /* Pointer to buffer containing name */
80160  void (*xDel)(void*)              /* Memory management strategy for zName */
80161){
80162  int rc;
80163  Mem *pColName;
80164  assert( idx<p->nResColumn );
80165  assert( var<COLNAME_N );
80166  if( p->db->mallocFailed ){
80167    assert( !zName || xDel!=SQLITE_DYNAMIC );
80168    return SQLITE_NOMEM_BKPT;
80169  }
80170  assert( p->aColName!=0 );
80171  pColName = &(p->aColName[idx+var*p->nResColumn]);
80172  rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
80173  assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
80174  return rc;
80175}
80176
80177/*
80178** A read or write transaction may or may not be active on database handle
80179** db. If a transaction is active, commit it. If there is a
80180** write-transaction spanning more than one database file, this routine
80181** takes care of the master journal trickery.
80182*/
80183static int vdbeCommit(sqlite3 *db, Vdbe *p){
80184  int i;
80185  int nTrans = 0;  /* Number of databases with an active write-transaction
80186                   ** that are candidates for a two-phase commit using a
80187                   ** master-journal */
80188  int rc = SQLITE_OK;
80189  int needXcommit = 0;
80190
80191#ifdef SQLITE_OMIT_VIRTUALTABLE
80192  /* With this option, sqlite3VtabSync() is defined to be simply
80193  ** SQLITE_OK so p is not used.
80194  */
80195  UNUSED_PARAMETER(p);
80196#endif
80197
80198  /* Before doing anything else, call the xSync() callback for any
80199  ** virtual module tables written in this transaction. This has to
80200  ** be done before determining whether a master journal file is
80201  ** required, as an xSync() callback may add an attached database
80202  ** to the transaction.
80203  */
80204  rc = sqlite3VtabSync(db, p);
80205
80206  /* This loop determines (a) if the commit hook should be invoked and
80207  ** (b) how many database files have open write transactions, not
80208  ** including the temp database. (b) is important because if more than
80209  ** one database file has an open write transaction, a master journal
80210  ** file is required for an atomic commit.
80211  */
80212  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
80213    Btree *pBt = db->aDb[i].pBt;
80214    if( sqlite3BtreeIsInTrans(pBt) ){
80215      /* Whether or not a database might need a master journal depends upon
80216      ** its journal mode (among other things).  This matrix determines which
80217      ** journal modes use a master journal and which do not */
80218      static const u8 aMJNeeded[] = {
80219        /* DELETE   */  1,
80220        /* PERSIST   */ 1,
80221        /* OFF       */ 0,
80222        /* TRUNCATE  */ 1,
80223        /* MEMORY    */ 0,
80224        /* WAL       */ 0
80225      };
80226      Pager *pPager;   /* Pager associated with pBt */
80227      needXcommit = 1;
80228      sqlite3BtreeEnter(pBt);
80229      pPager = sqlite3BtreePager(pBt);
80230      if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
80231       && aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
80232       && sqlite3PagerIsMemdb(pPager)==0
80233      ){
80234        assert( i!=1 );
80235        nTrans++;
80236      }
80237      rc = sqlite3PagerExclusiveLock(pPager);
80238      sqlite3BtreeLeave(pBt);
80239    }
80240  }
80241  if( rc!=SQLITE_OK ){
80242    return rc;
80243  }
80244
80245  /* If there are any write-transactions at all, invoke the commit hook */
80246  if( needXcommit && db->xCommitCallback ){
80247    rc = db->xCommitCallback(db->pCommitArg);
80248    if( rc ){
80249      return SQLITE_CONSTRAINT_COMMITHOOK;
80250    }
80251  }
80252
80253  /* The simple case - no more than one database file (not counting the
80254  ** TEMP database) has a transaction active.   There is no need for the
80255  ** master-journal.
80256  **
80257  ** If the return value of sqlite3BtreeGetFilename() is a zero length
80258  ** string, it means the main database is :memory: or a temp file.  In
80259  ** that case we do not support atomic multi-file commits, so use the
80260  ** simple case then too.
80261  */
80262  if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
80263   || nTrans<=1
80264  ){
80265    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
80266      Btree *pBt = db->aDb[i].pBt;
80267      if( pBt ){
80268        rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
80269      }
80270    }
80271
80272    /* Do the commit only if all databases successfully complete phase 1.
80273    ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
80274    ** IO error while deleting or truncating a journal file. It is unlikely,
80275    ** but could happen. In this case abandon processing and return the error.
80276    */
80277    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
80278      Btree *pBt = db->aDb[i].pBt;
80279      if( pBt ){
80280        rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
80281      }
80282    }
80283    if( rc==SQLITE_OK ){
80284      sqlite3VtabCommit(db);
80285    }
80286  }
80287
80288  /* The complex case - There is a multi-file write-transaction active.
80289  ** This requires a master journal file to ensure the transaction is
80290  ** committed atomically.
80291  */
80292#ifndef SQLITE_OMIT_DISKIO
80293  else{
80294    sqlite3_vfs *pVfs = db->pVfs;
80295    char *zMaster = 0;   /* File-name for the master journal */
80296    char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
80297    sqlite3_file *pMaster = 0;
80298    i64 offset = 0;
80299    int res;
80300    int retryCount = 0;
80301    int nMainFile;
80302
80303    /* Select a master journal file name */
80304    nMainFile = sqlite3Strlen30(zMainFile);
80305    zMaster = sqlite3MPrintf(db, "%.4c%s%.16c", 0,zMainFile,0);
80306    if( zMaster==0 ) return SQLITE_NOMEM_BKPT;
80307    zMaster += 4;
80308    do {
80309      u32 iRandom;
80310      if( retryCount ){
80311        if( retryCount>100 ){
80312          sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
80313          sqlite3OsDelete(pVfs, zMaster, 0);
80314          break;
80315        }else if( retryCount==1 ){
80316          sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
80317        }
80318      }
80319      retryCount++;
80320      sqlite3_randomness(sizeof(iRandom), &iRandom);
80321      sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
80322                               (iRandom>>8)&0xffffff, iRandom&0xff);
80323      /* The antipenultimate character of the master journal name must
80324      ** be "9" to avoid name collisions when using 8+3 filenames. */
80325      assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
80326      sqlite3FileSuffix3(zMainFile, zMaster);
80327      rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
80328    }while( rc==SQLITE_OK && res );
80329    if( rc==SQLITE_OK ){
80330      /* Open the master journal. */
80331      rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
80332          SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
80333          SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
80334      );
80335    }
80336    if( rc!=SQLITE_OK ){
80337      sqlite3DbFree(db, zMaster-4);
80338      return rc;
80339    }
80340
80341    /* Write the name of each database file in the transaction into the new
80342    ** master journal file. If an error occurs at this point close
80343    ** and delete the master journal file. All the individual journal files
80344    ** still have 'null' as the master journal pointer, so they will roll
80345    ** back independently if a failure occurs.
80346    */
80347    for(i=0; i<db->nDb; i++){
80348      Btree *pBt = db->aDb[i].pBt;
80349      if( sqlite3BtreeIsInTrans(pBt) ){
80350        char const *zFile = sqlite3BtreeGetJournalname(pBt);
80351        if( zFile==0 ){
80352          continue;  /* Ignore TEMP and :memory: databases */
80353        }
80354        assert( zFile[0]!=0 );
80355        rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
80356        offset += sqlite3Strlen30(zFile)+1;
80357        if( rc!=SQLITE_OK ){
80358          sqlite3OsCloseFree(pMaster);
80359          sqlite3OsDelete(pVfs, zMaster, 0);
80360          sqlite3DbFree(db, zMaster-4);
80361          return rc;
80362        }
80363      }
80364    }
80365
80366    /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
80367    ** flag is set this is not required.
80368    */
80369    if( 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
80370     && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
80371    ){
80372      sqlite3OsCloseFree(pMaster);
80373      sqlite3OsDelete(pVfs, zMaster, 0);
80374      sqlite3DbFree(db, zMaster-4);
80375      return rc;
80376    }
80377
80378    /* Sync all the db files involved in the transaction. The same call
80379    ** sets the master journal pointer in each individual journal. If
80380    ** an error occurs here, do not delete the master journal file.
80381    **
80382    ** If the error occurs during the first call to
80383    ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
80384    ** master journal file will be orphaned. But we cannot delete it,
80385    ** in case the master journal file name was written into the journal
80386    ** file before the failure occurred.
80387    */
80388    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
80389      Btree *pBt = db->aDb[i].pBt;
80390      if( pBt ){
80391        rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
80392      }
80393    }
80394    sqlite3OsCloseFree(pMaster);
80395    assert( rc!=SQLITE_BUSY );
80396    if( rc!=SQLITE_OK ){
80397      sqlite3DbFree(db, zMaster-4);
80398      return rc;
80399    }
80400
80401    /* Delete the master journal file. This commits the transaction. After
80402    ** doing this the directory is synced again before any individual
80403    ** transaction files are deleted.
80404    */
80405    rc = sqlite3OsDelete(pVfs, zMaster, 1);
80406    sqlite3DbFree(db, zMaster-4);
80407    zMaster = 0;
80408    if( rc ){
80409      return rc;
80410    }
80411
80412    /* All files and directories have already been synced, so the following
80413    ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
80414    ** deleting or truncating journals. If something goes wrong while
80415    ** this is happening we don't really care. The integrity of the
80416    ** transaction is already guaranteed, but some stray 'cold' journals
80417    ** may be lying around. Returning an error code won't help matters.
80418    */
80419    disable_simulated_io_errors();
80420    sqlite3BeginBenignMalloc();
80421    for(i=0; i<db->nDb; i++){
80422      Btree *pBt = db->aDb[i].pBt;
80423      if( pBt ){
80424        sqlite3BtreeCommitPhaseTwo(pBt, 1);
80425      }
80426    }
80427    sqlite3EndBenignMalloc();
80428    enable_simulated_io_errors();
80429
80430    sqlite3VtabCommit(db);
80431  }
80432#endif
80433
80434  return rc;
80435}
80436
80437/*
80438** This routine checks that the sqlite3.nVdbeActive count variable
80439** matches the number of vdbe's in the list sqlite3.pVdbe that are
80440** currently active. An assertion fails if the two counts do not match.
80441** This is an internal self-check only - it is not an essential processing
80442** step.
80443**
80444** This is a no-op if NDEBUG is defined.
80445*/
80446#ifndef NDEBUG
80447static void checkActiveVdbeCnt(sqlite3 *db){
80448  Vdbe *p;
80449  int cnt = 0;
80450  int nWrite = 0;
80451  int nRead = 0;
80452  p = db->pVdbe;
80453  while( p ){
80454    if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
80455      cnt++;
80456      if( p->readOnly==0 ) nWrite++;
80457      if( p->bIsReader ) nRead++;
80458    }
80459    p = p->pNext;
80460  }
80461  assert( cnt==db->nVdbeActive );
80462  assert( nWrite==db->nVdbeWrite );
80463  assert( nRead==db->nVdbeRead );
80464}
80465#else
80466#define checkActiveVdbeCnt(x)
80467#endif
80468
80469/*
80470** If the Vdbe passed as the first argument opened a statement-transaction,
80471** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
80472** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
80473** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
80474** statement transaction is committed.
80475**
80476** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
80477** Otherwise SQLITE_OK.
80478*/
80479static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){
80480  sqlite3 *const db = p->db;
80481  int rc = SQLITE_OK;
80482  int i;
80483  const int iSavepoint = p->iStatement-1;
80484
80485  assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
80486  assert( db->nStatement>0 );
80487  assert( p->iStatement==(db->nStatement+db->nSavepoint) );
80488
80489  for(i=0; i<db->nDb; i++){
80490    int rc2 = SQLITE_OK;
80491    Btree *pBt = db->aDb[i].pBt;
80492    if( pBt ){
80493      if( eOp==SAVEPOINT_ROLLBACK ){
80494        rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
80495      }
80496      if( rc2==SQLITE_OK ){
80497        rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
80498      }
80499      if( rc==SQLITE_OK ){
80500        rc = rc2;
80501      }
80502    }
80503  }
80504  db->nStatement--;
80505  p->iStatement = 0;
80506
80507  if( rc==SQLITE_OK ){
80508    if( eOp==SAVEPOINT_ROLLBACK ){
80509      rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
80510    }
80511    if( rc==SQLITE_OK ){
80512      rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
80513    }
80514  }
80515
80516  /* If the statement transaction is being rolled back, also restore the
80517  ** database handles deferred constraint counter to the value it had when
80518  ** the statement transaction was opened.  */
80519  if( eOp==SAVEPOINT_ROLLBACK ){
80520    db->nDeferredCons = p->nStmtDefCons;
80521    db->nDeferredImmCons = p->nStmtDefImmCons;
80522  }
80523  return rc;
80524}
80525SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
80526  if( p->db->nStatement && p->iStatement ){
80527    return vdbeCloseStatement(p, eOp);
80528  }
80529  return SQLITE_OK;
80530}
80531
80532
80533/*
80534** This function is called when a transaction opened by the database
80535** handle associated with the VM passed as an argument is about to be
80536** committed. If there are outstanding deferred foreign key constraint
80537** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
80538**
80539** If there are outstanding FK violations and this function returns
80540** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
80541** and write an error message to it. Then return SQLITE_ERROR.
80542*/
80543#ifndef SQLITE_OMIT_FOREIGN_KEY
80544SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
80545  sqlite3 *db = p->db;
80546  if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
80547   || (!deferred && p->nFkConstraint>0)
80548  ){
80549    p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
80550    p->errorAction = OE_Abort;
80551    sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
80552    return SQLITE_ERROR;
80553  }
80554  return SQLITE_OK;
80555}
80556#endif
80557
80558/*
80559** This routine is called the when a VDBE tries to halt.  If the VDBE
80560** has made changes and is in autocommit mode, then commit those
80561** changes.  If a rollback is needed, then do the rollback.
80562**
80563** This routine is the only way to move the state of a VM from
80564** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
80565** call this on a VM that is in the SQLITE_MAGIC_HALT state.
80566**
80567** Return an error code.  If the commit could not complete because of
80568** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
80569** means the close did not happen and needs to be repeated.
80570*/
80571SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
80572  int rc;                         /* Used to store transient return codes */
80573  sqlite3 *db = p->db;
80574
80575  /* This function contains the logic that determines if a statement or
80576  ** transaction will be committed or rolled back as a result of the
80577  ** execution of this virtual machine.
80578  **
80579  ** If any of the following errors occur:
80580  **
80581  **     SQLITE_NOMEM
80582  **     SQLITE_IOERR
80583  **     SQLITE_FULL
80584  **     SQLITE_INTERRUPT
80585  **
80586  ** Then the internal cache might have been left in an inconsistent
80587  ** state.  We need to rollback the statement transaction, if there is
80588  ** one, or the complete transaction if there is no statement transaction.
80589  */
80590
80591  if( p->magic!=VDBE_MAGIC_RUN ){
80592    return SQLITE_OK;
80593  }
80594  if( db->mallocFailed ){
80595    p->rc = SQLITE_NOMEM_BKPT;
80596  }
80597  closeAllCursors(p);
80598  checkActiveVdbeCnt(db);
80599
80600  /* No commit or rollback needed if the program never started or if the
80601  ** SQL statement does not read or write a database file.  */
80602  if( p->pc>=0 && p->bIsReader ){
80603    int mrc;   /* Primary error code from p->rc */
80604    int eStatementOp = 0;
80605    int isSpecialError;            /* Set to true if a 'special' error */
80606
80607    /* Lock all btrees used by the statement */
80608    sqlite3VdbeEnter(p);
80609
80610    /* Check for one of the special errors */
80611    mrc = p->rc & 0xff;
80612    isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
80613                     || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
80614    if( isSpecialError ){
80615      /* If the query was read-only and the error code is SQLITE_INTERRUPT,
80616      ** no rollback is necessary. Otherwise, at least a savepoint
80617      ** transaction must be rolled back to restore the database to a
80618      ** consistent state.
80619      **
80620      ** Even if the statement is read-only, it is important to perform
80621      ** a statement or transaction rollback operation. If the error
80622      ** occurred while writing to the journal, sub-journal or database
80623      ** file as part of an effort to free up cache space (see function
80624      ** pagerStress() in pager.c), the rollback is required to restore
80625      ** the pager to a consistent state.
80626      */
80627      if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
80628        if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
80629          eStatementOp = SAVEPOINT_ROLLBACK;
80630        }else{
80631          /* We are forced to roll back the active transaction. Before doing
80632          ** so, abort any other statements this handle currently has active.
80633          */
80634          sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
80635          sqlite3CloseSavepoints(db);
80636          db->autoCommit = 1;
80637          p->nChange = 0;
80638        }
80639      }
80640    }
80641
80642    /* Check for immediate foreign key violations. */
80643    if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
80644      sqlite3VdbeCheckFk(p, 0);
80645    }
80646
80647    /* If the auto-commit flag is set and this is the only active writer
80648    ** VM, then we do either a commit or rollback of the current transaction.
80649    **
80650    ** Note: This block also runs if one of the special errors handled
80651    ** above has occurred.
80652    */
80653    if( !sqlite3VtabInSync(db)
80654     && db->autoCommit
80655     && db->nVdbeWrite==(p->readOnly==0)
80656    ){
80657      if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
80658        rc = sqlite3VdbeCheckFk(p, 1);
80659        if( rc!=SQLITE_OK ){
80660          if( NEVER(p->readOnly) ){
80661            sqlite3VdbeLeave(p);
80662            return SQLITE_ERROR;
80663          }
80664          rc = SQLITE_CONSTRAINT_FOREIGNKEY;
80665        }else{
80666          /* The auto-commit flag is true, the vdbe program was successful
80667          ** or hit an 'OR FAIL' constraint and there are no deferred foreign
80668          ** key constraints to hold up the transaction. This means a commit
80669          ** is required. */
80670          rc = vdbeCommit(db, p);
80671        }
80672        if( rc==SQLITE_BUSY && p->readOnly ){
80673          sqlite3VdbeLeave(p);
80674          return SQLITE_BUSY;
80675        }else if( rc!=SQLITE_OK ){
80676          p->rc = rc;
80677          sqlite3RollbackAll(db, SQLITE_OK);
80678          p->nChange = 0;
80679        }else{
80680          db->nDeferredCons = 0;
80681          db->nDeferredImmCons = 0;
80682          db->flags &= ~(u64)SQLITE_DeferFKs;
80683          sqlite3CommitInternalChanges(db);
80684        }
80685      }else{
80686        sqlite3RollbackAll(db, SQLITE_OK);
80687        p->nChange = 0;
80688      }
80689      db->nStatement = 0;
80690    }else if( eStatementOp==0 ){
80691      if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
80692        eStatementOp = SAVEPOINT_RELEASE;
80693      }else if( p->errorAction==OE_Abort ){
80694        eStatementOp = SAVEPOINT_ROLLBACK;
80695      }else{
80696        sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
80697        sqlite3CloseSavepoints(db);
80698        db->autoCommit = 1;
80699        p->nChange = 0;
80700      }
80701    }
80702
80703    /* If eStatementOp is non-zero, then a statement transaction needs to
80704    ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
80705    ** do so. If this operation returns an error, and the current statement
80706    ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
80707    ** current statement error code.
80708    */
80709    if( eStatementOp ){
80710      rc = sqlite3VdbeCloseStatement(p, eStatementOp);
80711      if( rc ){
80712        if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
80713          p->rc = rc;
80714          sqlite3DbFree(db, p->zErrMsg);
80715          p->zErrMsg = 0;
80716        }
80717        sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
80718        sqlite3CloseSavepoints(db);
80719        db->autoCommit = 1;
80720        p->nChange = 0;
80721      }
80722    }
80723
80724    /* If this was an INSERT, UPDATE or DELETE and no statement transaction
80725    ** has been rolled back, update the database connection change-counter.
80726    */
80727    if( p->changeCntOn ){
80728      if( eStatementOp!=SAVEPOINT_ROLLBACK ){
80729        sqlite3VdbeSetChanges(db, p->nChange);
80730      }else{
80731        sqlite3VdbeSetChanges(db, 0);
80732      }
80733      p->nChange = 0;
80734    }
80735
80736    /* Release the locks */
80737    sqlite3VdbeLeave(p);
80738  }
80739
80740  /* We have successfully halted and closed the VM.  Record this fact. */
80741  if( p->pc>=0 ){
80742    db->nVdbeActive--;
80743    if( !p->readOnly ) db->nVdbeWrite--;
80744    if( p->bIsReader ) db->nVdbeRead--;
80745    assert( db->nVdbeActive>=db->nVdbeRead );
80746    assert( db->nVdbeRead>=db->nVdbeWrite );
80747    assert( db->nVdbeWrite>=0 );
80748  }
80749  p->magic = VDBE_MAGIC_HALT;
80750  checkActiveVdbeCnt(db);
80751  if( db->mallocFailed ){
80752    p->rc = SQLITE_NOMEM_BKPT;
80753  }
80754
80755  /* If the auto-commit flag is set to true, then any locks that were held
80756  ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
80757  ** to invoke any required unlock-notify callbacks.
80758  */
80759  if( db->autoCommit ){
80760    sqlite3ConnectionUnlocked(db);
80761  }
80762
80763  assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
80764  return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
80765}
80766
80767
80768/*
80769** Each VDBE holds the result of the most recent sqlite3_step() call
80770** in p->rc.  This routine sets that result back to SQLITE_OK.
80771*/
80772SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
80773  p->rc = SQLITE_OK;
80774}
80775
80776/*
80777** Copy the error code and error message belonging to the VDBE passed
80778** as the first argument to its database handle (so that they will be
80779** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
80780**
80781** This function does not clear the VDBE error code or message, just
80782** copies them to the database handle.
80783*/
80784SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
80785  sqlite3 *db = p->db;
80786  int rc = p->rc;
80787  if( p->zErrMsg ){
80788    db->bBenignMalloc++;
80789    sqlite3BeginBenignMalloc();
80790    if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
80791    sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
80792    sqlite3EndBenignMalloc();
80793    db->bBenignMalloc--;
80794  }else if( db->pErr ){
80795    sqlite3ValueSetNull(db->pErr);
80796  }
80797  db->errCode = rc;
80798  return rc;
80799}
80800
80801#ifdef SQLITE_ENABLE_SQLLOG
80802/*
80803** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
80804** invoke it.
80805*/
80806static void vdbeInvokeSqllog(Vdbe *v){
80807  if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
80808    char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
80809    assert( v->db->init.busy==0 );
80810    if( zExpanded ){
80811      sqlite3GlobalConfig.xSqllog(
80812          sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
80813      );
80814      sqlite3DbFree(v->db, zExpanded);
80815    }
80816  }
80817}
80818#else
80819# define vdbeInvokeSqllog(x)
80820#endif
80821
80822/*
80823** Clean up a VDBE after execution but do not delete the VDBE just yet.
80824** Write any error messages into *pzErrMsg.  Return the result code.
80825**
80826** After this routine is run, the VDBE should be ready to be executed
80827** again.
80828**
80829** To look at it another way, this routine resets the state of the
80830** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
80831** VDBE_MAGIC_INIT.
80832*/
80833SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
80834#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
80835  int i;
80836#endif
80837
80838  sqlite3 *db;
80839  db = p->db;
80840
80841  /* If the VM did not run to completion or if it encountered an
80842  ** error, then it might not have been halted properly.  So halt
80843  ** it now.
80844  */
80845  sqlite3VdbeHalt(p);
80846
80847  /* If the VDBE has been run even partially, then transfer the error code
80848  ** and error message from the VDBE into the main database structure.  But
80849  ** if the VDBE has just been set to run but has not actually executed any
80850  ** instructions yet, leave the main database error information unchanged.
80851  */
80852  if( p->pc>=0 ){
80853    vdbeInvokeSqllog(p);
80854    sqlite3VdbeTransferError(p);
80855    if( p->runOnlyOnce ) p->expired = 1;
80856  }else if( p->rc && p->expired ){
80857    /* The expired flag was set on the VDBE before the first call
80858    ** to sqlite3_step(). For consistency (since sqlite3_step() was
80859    ** called), set the database error in this case as well.
80860    */
80861    sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
80862  }
80863
80864  /* Reset register contents and reclaim error message memory.
80865  */
80866#ifdef SQLITE_DEBUG
80867  /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
80868  ** Vdbe.aMem[] arrays have already been cleaned up.  */
80869  if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
80870  if( p->aMem ){
80871    for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
80872  }
80873#endif
80874  sqlite3DbFree(db, p->zErrMsg);
80875  p->zErrMsg = 0;
80876  p->pResultSet = 0;
80877#ifdef SQLITE_DEBUG
80878  p->nWrite = 0;
80879#endif
80880
80881  /* Save profiling information from this VDBE run.
80882  */
80883#ifdef VDBE_PROFILE
80884  {
80885    FILE *out = fopen("vdbe_profile.out", "a");
80886    if( out ){
80887      fprintf(out, "---- ");
80888      for(i=0; i<p->nOp; i++){
80889        fprintf(out, "%02x", p->aOp[i].opcode);
80890      }
80891      fprintf(out, "\n");
80892      if( p->zSql ){
80893        char c, pc = 0;
80894        fprintf(out, "-- ");
80895        for(i=0; (c = p->zSql[i])!=0; i++){
80896          if( pc=='\n' ) fprintf(out, "-- ");
80897          putc(c, out);
80898          pc = c;
80899        }
80900        if( pc!='\n' ) fprintf(out, "\n");
80901      }
80902      for(i=0; i<p->nOp; i++){
80903        char zHdr[100];
80904        sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
80905           p->aOp[i].cnt,
80906           p->aOp[i].cycles,
80907           p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
80908        );
80909        fprintf(out, "%s", zHdr);
80910        sqlite3VdbePrintOp(out, i, &p->aOp[i]);
80911      }
80912      fclose(out);
80913    }
80914  }
80915#endif
80916  p->magic = VDBE_MAGIC_RESET;
80917  return p->rc & db->errMask;
80918}
80919
80920/*
80921** Clean up and delete a VDBE after execution.  Return an integer which is
80922** the result code.  Write any error message text into *pzErrMsg.
80923*/
80924SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
80925  int rc = SQLITE_OK;
80926  if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
80927    rc = sqlite3VdbeReset(p);
80928    assert( (rc & p->db->errMask)==rc );
80929  }
80930  sqlite3VdbeDelete(p);
80931  return rc;
80932}
80933
80934/*
80935** If parameter iOp is less than zero, then invoke the destructor for
80936** all auxiliary data pointers currently cached by the VM passed as
80937** the first argument.
80938**
80939** Or, if iOp is greater than or equal to zero, then the destructor is
80940** only invoked for those auxiliary data pointers created by the user
80941** function invoked by the OP_Function opcode at instruction iOp of
80942** VM pVdbe, and only then if:
80943**
80944**    * the associated function parameter is the 32nd or later (counting
80945**      from left to right), or
80946**
80947**    * the corresponding bit in argument mask is clear (where the first
80948**      function parameter corresponds to bit 0 etc.).
80949*/
80950SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){
80951  while( *pp ){
80952    AuxData *pAux = *pp;
80953    if( (iOp<0)
80954     || (pAux->iAuxOp==iOp
80955          && pAux->iAuxArg>=0
80956          && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
80957    ){
80958      testcase( pAux->iAuxArg==31 );
80959      if( pAux->xDeleteAux ){
80960        pAux->xDeleteAux(pAux->pAux);
80961      }
80962      *pp = pAux->pNextAux;
80963      sqlite3DbFree(db, pAux);
80964    }else{
80965      pp= &pAux->pNextAux;
80966    }
80967  }
80968}
80969
80970/*
80971** Free all memory associated with the Vdbe passed as the second argument,
80972** except for object itself, which is preserved.
80973**
80974** The difference between this function and sqlite3VdbeDelete() is that
80975** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
80976** the database connection and frees the object itself.
80977*/
80978SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
80979  SubProgram *pSub, *pNext;
80980  assert( p->db==0 || p->db==db );
80981  releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
80982  for(pSub=p->pProgram; pSub; pSub=pNext){
80983    pNext = pSub->pNext;
80984    vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
80985    sqlite3DbFree(db, pSub);
80986  }
80987  if( p->magic!=VDBE_MAGIC_INIT ){
80988    releaseMemArray(p->aVar, p->nVar);
80989    sqlite3DbFree(db, p->pVList);
80990    sqlite3DbFree(db, p->pFree);
80991  }
80992  vdbeFreeOpArray(db, p->aOp, p->nOp);
80993  sqlite3DbFree(db, p->aColName);
80994  sqlite3DbFree(db, p->zSql);
80995#ifdef SQLITE_ENABLE_NORMALIZE
80996  sqlite3DbFree(db, p->zNormSql);
80997  {
80998    DblquoteStr *pThis, *pNext;
80999    for(pThis=p->pDblStr; pThis; pThis=pNext){
81000      pNext = pThis->pNextStr;
81001      sqlite3DbFree(db, pThis);
81002    }
81003  }
81004#endif
81005#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
81006  {
81007    int i;
81008    for(i=0; i<p->nScan; i++){
81009      sqlite3DbFree(db, p->aScan[i].zName);
81010    }
81011    sqlite3DbFree(db, p->aScan);
81012  }
81013#endif
81014}
81015
81016/*
81017** Delete an entire VDBE.
81018*/
81019SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
81020  sqlite3 *db;
81021
81022  assert( p!=0 );
81023  db = p->db;
81024  assert( sqlite3_mutex_held(db->mutex) );
81025  sqlite3VdbeClearObject(db, p);
81026  if( p->pPrev ){
81027    p->pPrev->pNext = p->pNext;
81028  }else{
81029    assert( db->pVdbe==p );
81030    db->pVdbe = p->pNext;
81031  }
81032  if( p->pNext ){
81033    p->pNext->pPrev = p->pPrev;
81034  }
81035  p->magic = VDBE_MAGIC_DEAD;
81036  p->db = 0;
81037  sqlite3DbFreeNN(db, p);
81038}
81039
81040/*
81041** The cursor "p" has a pending seek operation that has not yet been
81042** carried out.  Seek the cursor now.  If an error occurs, return
81043** the appropriate error code.
81044*/
81045SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeFinishMoveto(VdbeCursor *p){
81046  int res, rc;
81047#ifdef SQLITE_TEST
81048  extern int sqlite3_search_count;
81049#endif
81050  assert( p->deferredMoveto );
81051  assert( p->isTable );
81052  assert( p->eCurType==CURTYPE_BTREE );
81053  rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res);
81054  if( rc ) return rc;
81055  if( res!=0 ) return SQLITE_CORRUPT_BKPT;
81056#ifdef SQLITE_TEST
81057  sqlite3_search_count++;
81058#endif
81059  p->deferredMoveto = 0;
81060  p->cacheStatus = CACHE_STALE;
81061  return SQLITE_OK;
81062}
81063
81064/*
81065** Something has moved cursor "p" out of place.  Maybe the row it was
81066** pointed to was deleted out from under it.  Or maybe the btree was
81067** rebalanced.  Whatever the cause, try to restore "p" to the place it
81068** is supposed to be pointing.  If the row was deleted out from under the
81069** cursor, set the cursor to point to a NULL row.
81070*/
81071static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
81072  int isDifferentRow, rc;
81073  assert( p->eCurType==CURTYPE_BTREE );
81074  assert( p->uc.pCursor!=0 );
81075  assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
81076  rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
81077  p->cacheStatus = CACHE_STALE;
81078  if( isDifferentRow ) p->nullRow = 1;
81079  return rc;
81080}
81081
81082/*
81083** Check to ensure that the cursor is valid.  Restore the cursor
81084** if need be.  Return any I/O error from the restore operation.
81085*/
81086SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor *p){
81087  assert( p->eCurType==CURTYPE_BTREE );
81088  if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
81089    return handleMovedCursor(p);
81090  }
81091  return SQLITE_OK;
81092}
81093
81094/*
81095** Make sure the cursor p is ready to read or write the row to which it
81096** was last positioned.  Return an error code if an OOM fault or I/O error
81097** prevents us from positioning the cursor to its correct position.
81098**
81099** If a MoveTo operation is pending on the given cursor, then do that
81100** MoveTo now.  If no move is pending, check to see if the row has been
81101** deleted out from under the cursor and if it has, mark the row as
81102** a NULL row.
81103**
81104** If the cursor is already pointing to the correct row and that row has
81105** not been deleted out from under the cursor, then this routine is a no-op.
81106*/
81107SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){
81108  VdbeCursor *p = *pp;
81109  assert( p->eCurType==CURTYPE_BTREE || p->eCurType==CURTYPE_PSEUDO );
81110  if( p->deferredMoveto ){
81111    int iMap;
81112    if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 && !p->nullRow ){
81113      *pp = p->pAltCursor;
81114      *piCol = iMap - 1;
81115      return SQLITE_OK;
81116    }
81117    return sqlite3VdbeFinishMoveto(p);
81118  }
81119  if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
81120    return handleMovedCursor(p);
81121  }
81122  return SQLITE_OK;
81123}
81124
81125/*
81126** The following functions:
81127**
81128** sqlite3VdbeSerialType()
81129** sqlite3VdbeSerialTypeLen()
81130** sqlite3VdbeSerialLen()
81131** sqlite3VdbeSerialPut()
81132** sqlite3VdbeSerialGet()
81133**
81134** encapsulate the code that serializes values for storage in SQLite
81135** data and index records. Each serialized value consists of a
81136** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
81137** integer, stored as a varint.
81138**
81139** In an SQLite index record, the serial type is stored directly before
81140** the blob of data that it corresponds to. In a table record, all serial
81141** types are stored at the start of the record, and the blobs of data at
81142** the end. Hence these functions allow the caller to handle the
81143** serial-type and data blob separately.
81144**
81145** The following table describes the various storage classes for data:
81146**
81147**   serial type        bytes of data      type
81148**   --------------     ---------------    ---------------
81149**      0                     0            NULL
81150**      1                     1            signed integer
81151**      2                     2            signed integer
81152**      3                     3            signed integer
81153**      4                     4            signed integer
81154**      5                     6            signed integer
81155**      6                     8            signed integer
81156**      7                     8            IEEE float
81157**      8                     0            Integer constant 0
81158**      9                     0            Integer constant 1
81159**     10,11                               reserved for expansion
81160**    N>=12 and even       (N-12)/2        BLOB
81161**    N>=13 and odd        (N-13)/2        text
81162**
81163** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
81164** of SQLite will not understand those serial types.
81165*/
81166
81167#if 0 /* Inlined into the OP_MakeRecord opcode */
81168/*
81169** Return the serial-type for the value stored in pMem.
81170**
81171** This routine might convert a large MEM_IntReal value into MEM_Real.
81172**
81173** 2019-07-11:  The primary user of this subroutine was the OP_MakeRecord
81174** opcode in the byte-code engine.  But by moving this routine in-line, we
81175** can omit some redundant tests and make that opcode a lot faster.  So
81176** this routine is now only used by the STAT3 logic and STAT3 support has
81177** ended.  The code is kept here for historical reference only.
81178*/
81179SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
81180  int flags = pMem->flags;
81181  u32 n;
81182
81183  assert( pLen!=0 );
81184  if( flags&MEM_Null ){
81185    *pLen = 0;
81186    return 0;
81187  }
81188  if( flags&(MEM_Int|MEM_IntReal) ){
81189    /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
81190#   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
81191    i64 i = pMem->u.i;
81192    u64 u;
81193    testcase( flags & MEM_Int );
81194    testcase( flags & MEM_IntReal );
81195    if( i<0 ){
81196      u = ~i;
81197    }else{
81198      u = i;
81199    }
81200    if( u<=127 ){
81201      if( (i&1)==i && file_format>=4 ){
81202        *pLen = 0;
81203        return 8+(u32)u;
81204      }else{
81205        *pLen = 1;
81206        return 1;
81207      }
81208    }
81209    if( u<=32767 ){ *pLen = 2; return 2; }
81210    if( u<=8388607 ){ *pLen = 3; return 3; }
81211    if( u<=2147483647 ){ *pLen = 4; return 4; }
81212    if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
81213    *pLen = 8;
81214    if( flags&MEM_IntReal ){
81215      /* If the value is IntReal and is going to take up 8 bytes to store
81216      ** as an integer, then we might as well make it an 8-byte floating
81217      ** point value */
81218      pMem->u.r = (double)pMem->u.i;
81219      pMem->flags &= ~MEM_IntReal;
81220      pMem->flags |= MEM_Real;
81221      return 7;
81222    }
81223    return 6;
81224  }
81225  if( flags&MEM_Real ){
81226    *pLen = 8;
81227    return 7;
81228  }
81229  assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
81230  assert( pMem->n>=0 );
81231  n = (u32)pMem->n;
81232  if( flags & MEM_Zero ){
81233    n += pMem->u.nZero;
81234  }
81235  *pLen = n;
81236  return ((n*2) + 12 + ((flags&MEM_Str)!=0));
81237}
81238#endif /* inlined into OP_MakeRecord */
81239
81240/*
81241** The sizes for serial types less than 128
81242*/
81243static const u8 sqlite3SmallTypeSizes[] = {
81244        /*  0   1   2   3   4   5   6   7   8   9 */
81245/*   0 */   0,  1,  2,  3,  4,  6,  8,  8,  0,  0,
81246/*  10 */   0,  0,  0,  0,  1,  1,  2,  2,  3,  3,
81247/*  20 */   4,  4,  5,  5,  6,  6,  7,  7,  8,  8,
81248/*  30 */   9,  9, 10, 10, 11, 11, 12, 12, 13, 13,
81249/*  40 */  14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
81250/*  50 */  19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
81251/*  60 */  24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
81252/*  70 */  29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
81253/*  80 */  34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
81254/*  90 */  39, 39, 40, 40, 41, 41, 42, 42, 43, 43,
81255/* 100 */  44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
81256/* 110 */  49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
81257/* 120 */  54, 54, 55, 55, 56, 56, 57, 57
81258};
81259
81260/*
81261** Return the length of the data corresponding to the supplied serial-type.
81262*/
81263SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
81264  if( serial_type>=128 ){
81265    return (serial_type-12)/2;
81266  }else{
81267    assert( serial_type<12
81268            || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
81269    return sqlite3SmallTypeSizes[serial_type];
81270  }
81271}
81272SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){
81273  assert( serial_type<128 );
81274  return sqlite3SmallTypeSizes[serial_type];
81275}
81276
81277/*
81278** If we are on an architecture with mixed-endian floating
81279** points (ex: ARM7) then swap the lower 4 bytes with the
81280** upper 4 bytes.  Return the result.
81281**
81282** For most architectures, this is a no-op.
81283**
81284** (later):  It is reported to me that the mixed-endian problem
81285** on ARM7 is an issue with GCC, not with the ARM7 chip.  It seems
81286** that early versions of GCC stored the two words of a 64-bit
81287** float in the wrong order.  And that error has been propagated
81288** ever since.  The blame is not necessarily with GCC, though.
81289** GCC might have just copying the problem from a prior compiler.
81290** I am also told that newer versions of GCC that follow a different
81291** ABI get the byte order right.
81292**
81293** Developers using SQLite on an ARM7 should compile and run their
81294** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
81295** enabled, some asserts below will ensure that the byte order of
81296** floating point values is correct.
81297**
81298** (2007-08-30)  Frank van Vugt has studied this problem closely
81299** and has send his findings to the SQLite developers.  Frank
81300** writes that some Linux kernels offer floating point hardware
81301** emulation that uses only 32-bit mantissas instead of a full
81302** 48-bits as required by the IEEE standard.  (This is the
81303** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
81304** byte swapping becomes very complicated.  To avoid problems,
81305** the necessary byte swapping is carried out using a 64-bit integer
81306** rather than a 64-bit float.  Frank assures us that the code here
81307** works for him.  We, the developers, have no way to independently
81308** verify this, but Frank seems to know what he is talking about
81309** so we trust him.
81310*/
81311#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
81312static u64 floatSwap(u64 in){
81313  union {
81314    u64 r;
81315    u32 i[2];
81316  } u;
81317  u32 t;
81318
81319  u.r = in;
81320  t = u.i[0];
81321  u.i[0] = u.i[1];
81322  u.i[1] = t;
81323  return u.r;
81324}
81325# define swapMixedEndianFloat(X)  X = floatSwap(X)
81326#else
81327# define swapMixedEndianFloat(X)
81328#endif
81329
81330/*
81331** Write the serialized data blob for the value stored in pMem into
81332** buf. It is assumed that the caller has allocated sufficient space.
81333** Return the number of bytes written.
81334**
81335** nBuf is the amount of space left in buf[].  The caller is responsible
81336** for allocating enough space to buf[] to hold the entire field, exclusive
81337** of the pMem->u.nZero bytes for a MEM_Zero value.
81338**
81339** Return the number of bytes actually written into buf[].  The number
81340** of bytes in the zero-filled tail is included in the return value only
81341** if those bytes were zeroed in buf[].
81342*/
81343SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
81344  u32 len;
81345
81346  /* Integer and Real */
81347  if( serial_type<=7 && serial_type>0 ){
81348    u64 v;
81349    u32 i;
81350    if( serial_type==7 ){
81351      assert( sizeof(v)==sizeof(pMem->u.r) );
81352      memcpy(&v, &pMem->u.r, sizeof(v));
81353      swapMixedEndianFloat(v);
81354    }else{
81355      v = pMem->u.i;
81356    }
81357    len = i = sqlite3SmallTypeSizes[serial_type];
81358    assert( i>0 );
81359    do{
81360      buf[--i] = (u8)(v&0xFF);
81361      v >>= 8;
81362    }while( i );
81363    return len;
81364  }
81365
81366  /* String or blob */
81367  if( serial_type>=12 ){
81368    assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
81369             == (int)sqlite3VdbeSerialTypeLen(serial_type) );
81370    len = pMem->n;
81371    if( len>0 ) memcpy(buf, pMem->z, len);
81372    return len;
81373  }
81374
81375  /* NULL or constants 0 or 1 */
81376  return 0;
81377}
81378
81379/* Input "x" is a sequence of unsigned characters that represent a
81380** big-endian integer.  Return the equivalent native integer
81381*/
81382#define ONE_BYTE_INT(x)    ((i8)(x)[0])
81383#define TWO_BYTE_INT(x)    (256*(i8)((x)[0])|(x)[1])
81384#define THREE_BYTE_INT(x)  (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
81385#define FOUR_BYTE_UINT(x)  (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
81386#define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
81387
81388/*
81389** Deserialize the data blob pointed to by buf as serial type serial_type
81390** and store the result in pMem.  Return the number of bytes read.
81391**
81392** This function is implemented as two separate routines for performance.
81393** The few cases that require local variables are broken out into a separate
81394** routine so that in most cases the overhead of moving the stack pointer
81395** is avoided.
81396*/
81397static u32 serialGet(
81398  const unsigned char *buf,     /* Buffer to deserialize from */
81399  u32 serial_type,              /* Serial type to deserialize */
81400  Mem *pMem                     /* Memory cell to write value into */
81401){
81402  u64 x = FOUR_BYTE_UINT(buf);
81403  u32 y = FOUR_BYTE_UINT(buf+4);
81404  x = (x<<32) + y;
81405  if( serial_type==6 ){
81406    /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
81407    ** twos-complement integer. */
81408    pMem->u.i = *(i64*)&x;
81409    pMem->flags = MEM_Int;
81410    testcase( pMem->u.i<0 );
81411  }else{
81412    /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
81413    ** floating point number. */
81414#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
81415    /* Verify that integers and floating point values use the same
81416    ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
81417    ** defined that 64-bit floating point values really are mixed
81418    ** endian.
81419    */
81420    static const u64 t1 = ((u64)0x3ff00000)<<32;
81421    static const double r1 = 1.0;
81422    u64 t2 = t1;
81423    swapMixedEndianFloat(t2);
81424    assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
81425#endif
81426    assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
81427    swapMixedEndianFloat(x);
81428    memcpy(&pMem->u.r, &x, sizeof(x));
81429    pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
81430  }
81431  return 8;
81432}
81433SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
81434  const unsigned char *buf,     /* Buffer to deserialize from */
81435  u32 serial_type,              /* Serial type to deserialize */
81436  Mem *pMem                     /* Memory cell to write value into */
81437){
81438  switch( serial_type ){
81439    case 10: { /* Internal use only: NULL with virtual table
81440               ** UPDATE no-change flag set */
81441      pMem->flags = MEM_Null|MEM_Zero;
81442      pMem->n = 0;
81443      pMem->u.nZero = 0;
81444      break;
81445    }
81446    case 11:   /* Reserved for future use */
81447    case 0: {  /* Null */
81448      /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
81449      pMem->flags = MEM_Null;
81450      break;
81451    }
81452    case 1: {
81453      /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
81454      ** integer. */
81455      pMem->u.i = ONE_BYTE_INT(buf);
81456      pMem->flags = MEM_Int;
81457      testcase( pMem->u.i<0 );
81458      return 1;
81459    }
81460    case 2: { /* 2-byte signed integer */
81461      /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
81462      ** twos-complement integer. */
81463      pMem->u.i = TWO_BYTE_INT(buf);
81464      pMem->flags = MEM_Int;
81465      testcase( pMem->u.i<0 );
81466      return 2;
81467    }
81468    case 3: { /* 3-byte signed integer */
81469      /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
81470      ** twos-complement integer. */
81471      pMem->u.i = THREE_BYTE_INT(buf);
81472      pMem->flags = MEM_Int;
81473      testcase( pMem->u.i<0 );
81474      return 3;
81475    }
81476    case 4: { /* 4-byte signed integer */
81477      /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
81478      ** twos-complement integer. */
81479      pMem->u.i = FOUR_BYTE_INT(buf);
81480#ifdef __HP_cc
81481      /* Work around a sign-extension bug in the HP compiler for HP/UX */
81482      if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
81483#endif
81484      pMem->flags = MEM_Int;
81485      testcase( pMem->u.i<0 );
81486      return 4;
81487    }
81488    case 5: { /* 6-byte signed integer */
81489      /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
81490      ** twos-complement integer. */
81491      pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
81492      pMem->flags = MEM_Int;
81493      testcase( pMem->u.i<0 );
81494      return 6;
81495    }
81496    case 6:   /* 8-byte signed integer */
81497    case 7: { /* IEEE floating point */
81498      /* These use local variables, so do them in a separate routine
81499      ** to avoid having to move the frame pointer in the common case */
81500      return serialGet(buf,serial_type,pMem);
81501    }
81502    case 8:    /* Integer 0 */
81503    case 9: {  /* Integer 1 */
81504      /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
81505      /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
81506      pMem->u.i = serial_type-8;
81507      pMem->flags = MEM_Int;
81508      return 0;
81509    }
81510    default: {
81511      /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
81512      ** length.
81513      ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
81514      ** (N-13)/2 bytes in length. */
81515      static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
81516      pMem->z = (char *)buf;
81517      pMem->n = (serial_type-12)/2;
81518      pMem->flags = aFlag[serial_type&1];
81519      return pMem->n;
81520    }
81521  }
81522  return 0;
81523}
81524/*
81525** This routine is used to allocate sufficient space for an UnpackedRecord
81526** structure large enough to be used with sqlite3VdbeRecordUnpack() if
81527** the first argument is a pointer to KeyInfo structure pKeyInfo.
81528**
81529** The space is either allocated using sqlite3DbMallocRaw() or from within
81530** the unaligned buffer passed via the second and third arguments (presumably
81531** stack space). If the former, then *ppFree is set to a pointer that should
81532** be eventually freed by the caller using sqlite3DbFree(). Or, if the
81533** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
81534** before returning.
81535**
81536** If an OOM error occurs, NULL is returned.
81537*/
81538SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
81539  KeyInfo *pKeyInfo               /* Description of the record */
81540){
81541  UnpackedRecord *p;              /* Unpacked record to return */
81542  int nByte;                      /* Number of bytes required for *p */
81543  nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
81544  p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
81545  if( !p ) return 0;
81546  p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
81547  assert( pKeyInfo->aSortFlags!=0 );
81548  p->pKeyInfo = pKeyInfo;
81549  p->nField = pKeyInfo->nKeyField + 1;
81550  return p;
81551}
81552
81553/*
81554** Given the nKey-byte encoding of a record in pKey[], populate the
81555** UnpackedRecord structure indicated by the fourth argument with the
81556** contents of the decoded record.
81557*/
81558SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
81559  KeyInfo *pKeyInfo,     /* Information about the record format */
81560  int nKey,              /* Size of the binary record */
81561  const void *pKey,      /* The binary record */
81562  UnpackedRecord *p      /* Populate this structure before returning. */
81563){
81564  const unsigned char *aKey = (const unsigned char *)pKey;
81565  u32 d;
81566  u32 idx;                        /* Offset in aKey[] to read from */
81567  u16 u;                          /* Unsigned loop counter */
81568  u32 szHdr;
81569  Mem *pMem = p->aMem;
81570
81571  p->default_rc = 0;
81572  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
81573  idx = getVarint32(aKey, szHdr);
81574  d = szHdr;
81575  u = 0;
81576  while( idx<szHdr && d<=(u32)nKey ){
81577    u32 serial_type;
81578
81579    idx += getVarint32(&aKey[idx], serial_type);
81580    pMem->enc = pKeyInfo->enc;
81581    pMem->db = pKeyInfo->db;
81582    /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
81583    pMem->szMalloc = 0;
81584    pMem->z = 0;
81585    d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
81586    pMem++;
81587    if( (++u)>=p->nField ) break;
81588  }
81589  if( d>(u32)nKey && u ){
81590    assert( CORRUPT_DB );
81591    /* In a corrupt record entry, the last pMem might have been set up using
81592    ** uninitialized memory. Overwrite its value with NULL, to prevent
81593    ** warnings from MSAN. */
81594    sqlite3VdbeMemSetNull(pMem-1);
81595  }
81596  assert( u<=pKeyInfo->nKeyField + 1 );
81597  p->nField = u;
81598}
81599
81600#ifdef SQLITE_DEBUG
81601/*
81602** This function compares two index or table record keys in the same way
81603** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
81604** this function deserializes and compares values using the
81605** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
81606** in assert() statements to ensure that the optimized code in
81607** sqlite3VdbeRecordCompare() returns results with these two primitives.
81608**
81609** Return true if the result of comparison is equivalent to desiredResult.
81610** Return false if there is a disagreement.
81611*/
81612static int vdbeRecordCompareDebug(
81613  int nKey1, const void *pKey1, /* Left key */
81614  const UnpackedRecord *pPKey2, /* Right key */
81615  int desiredResult             /* Correct answer */
81616){
81617  u32 d1;            /* Offset into aKey[] of next data element */
81618  u32 idx1;          /* Offset into aKey[] of next header element */
81619  u32 szHdr1;        /* Number of bytes in header */
81620  int i = 0;
81621  int rc = 0;
81622  const unsigned char *aKey1 = (const unsigned char *)pKey1;
81623  KeyInfo *pKeyInfo;
81624  Mem mem1;
81625
81626  pKeyInfo = pPKey2->pKeyInfo;
81627  if( pKeyInfo->db==0 ) return 1;
81628  mem1.enc = pKeyInfo->enc;
81629  mem1.db = pKeyInfo->db;
81630  /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
81631  VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
81632
81633  /* Compilers may complain that mem1.u.i is potentially uninitialized.
81634  ** We could initialize it, as shown here, to silence those complaints.
81635  ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
81636  ** the unnecessary initialization has a measurable negative performance
81637  ** impact, since this routine is a very high runner.  And so, we choose
81638  ** to ignore the compiler warnings and leave this variable uninitialized.
81639  */
81640  /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
81641
81642  idx1 = getVarint32(aKey1, szHdr1);
81643  if( szHdr1>98307 ) return SQLITE_CORRUPT;
81644  d1 = szHdr1;
81645  assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
81646  assert( pKeyInfo->aSortFlags!=0 );
81647  assert( pKeyInfo->nKeyField>0 );
81648  assert( idx1<=szHdr1 || CORRUPT_DB );
81649  do{
81650    u32 serial_type1;
81651
81652    /* Read the serial types for the next element in each key. */
81653    idx1 += getVarint32( aKey1+idx1, serial_type1 );
81654
81655    /* Verify that there is enough key space remaining to avoid
81656    ** a buffer overread.  The "d1+serial_type1+2" subexpression will
81657    ** always be greater than or equal to the amount of required key space.
81658    ** Use that approximation to avoid the more expensive call to
81659    ** sqlite3VdbeSerialTypeLen() in the common case.
81660    */
81661    if( d1+(u64)serial_type1+2>(u64)nKey1
81662     && d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)>(u64)nKey1
81663    ){
81664      break;
81665    }
81666
81667    /* Extract the values to be compared.
81668    */
81669    d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
81670
81671    /* Do the comparison
81672    */
81673    rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
81674                           pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
81675    if( rc!=0 ){
81676      assert( mem1.szMalloc==0 );  /* See comment below */
81677      if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
81678       && ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null))
81679      ){
81680        rc = -rc;
81681      }
81682      if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){
81683        rc = -rc;  /* Invert the result for DESC sort order. */
81684      }
81685      goto debugCompareEnd;
81686    }
81687    i++;
81688  }while( idx1<szHdr1 && i<pPKey2->nField );
81689
81690  /* No memory allocation is ever used on mem1.  Prove this using
81691  ** the following assert().  If the assert() fails, it indicates a
81692  ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
81693  */
81694  assert( mem1.szMalloc==0 );
81695
81696  /* rc==0 here means that one of the keys ran out of fields and
81697  ** all the fields up to that point were equal. Return the default_rc
81698  ** value.  */
81699  rc = pPKey2->default_rc;
81700
81701debugCompareEnd:
81702  if( desiredResult==0 && rc==0 ) return 1;
81703  if( desiredResult<0 && rc<0 ) return 1;
81704  if( desiredResult>0 && rc>0 ) return 1;
81705  if( CORRUPT_DB ) return 1;
81706  if( pKeyInfo->db->mallocFailed ) return 1;
81707  return 0;
81708}
81709#endif
81710
81711#ifdef SQLITE_DEBUG
81712/*
81713** Count the number of fields (a.k.a. columns) in the record given by
81714** pKey,nKey.  The verify that this count is less than or equal to the
81715** limit given by pKeyInfo->nAllField.
81716**
81717** If this constraint is not satisfied, it means that the high-speed
81718** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
81719** not work correctly.  If this assert() ever fires, it probably means
81720** that the KeyInfo.nKeyField or KeyInfo.nAllField values were computed
81721** incorrectly.
81722*/
81723static void vdbeAssertFieldCountWithinLimits(
81724  int nKey, const void *pKey,   /* The record to verify */
81725  const KeyInfo *pKeyInfo       /* Compare size with this KeyInfo */
81726){
81727  int nField = 0;
81728  u32 szHdr;
81729  u32 idx;
81730  u32 notUsed;
81731  const unsigned char *aKey = (const unsigned char*)pKey;
81732
81733  if( CORRUPT_DB ) return;
81734  idx = getVarint32(aKey, szHdr);
81735  assert( nKey>=0 );
81736  assert( szHdr<=(u32)nKey );
81737  while( idx<szHdr ){
81738    idx += getVarint32(aKey+idx, notUsed);
81739    nField++;
81740  }
81741  assert( nField <= pKeyInfo->nAllField );
81742}
81743#else
81744# define vdbeAssertFieldCountWithinLimits(A,B,C)
81745#endif
81746
81747/*
81748** Both *pMem1 and *pMem2 contain string values. Compare the two values
81749** using the collation sequence pColl. As usual, return a negative , zero
81750** or positive value if *pMem1 is less than, equal to or greater than
81751** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
81752*/
81753static int vdbeCompareMemString(
81754  const Mem *pMem1,
81755  const Mem *pMem2,
81756  const CollSeq *pColl,
81757  u8 *prcErr                      /* If an OOM occurs, set to SQLITE_NOMEM */
81758){
81759  if( pMem1->enc==pColl->enc ){
81760    /* The strings are already in the correct encoding.  Call the
81761     ** comparison function directly */
81762    return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
81763  }else{
81764    int rc;
81765    const void *v1, *v2;
81766    Mem c1;
81767    Mem c2;
81768    sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
81769    sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
81770    sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
81771    sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
81772    v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
81773    v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
81774    if( (v1==0 || v2==0) ){
81775      if( prcErr ) *prcErr = SQLITE_NOMEM_BKPT;
81776      rc = 0;
81777    }else{
81778      rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
81779    }
81780    sqlite3VdbeMemRelease(&c1);
81781    sqlite3VdbeMemRelease(&c2);
81782    return rc;
81783  }
81784}
81785
81786/*
81787** The input pBlob is guaranteed to be a Blob that is not marked
81788** with MEM_Zero.  Return true if it could be a zero-blob.
81789*/
81790static int isAllZero(const char *z, int n){
81791  int i;
81792  for(i=0; i<n; i++){
81793    if( z[i] ) return 0;
81794  }
81795  return 1;
81796}
81797
81798/*
81799** Compare two blobs.  Return negative, zero, or positive if the first
81800** is less than, equal to, or greater than the second, respectively.
81801** If one blob is a prefix of the other, then the shorter is the lessor.
81802*/
81803SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
81804  int c;
81805  int n1 = pB1->n;
81806  int n2 = pB2->n;
81807
81808  /* It is possible to have a Blob value that has some non-zero content
81809  ** followed by zero content.  But that only comes up for Blobs formed
81810  ** by the OP_MakeRecord opcode, and such Blobs never get passed into
81811  ** sqlite3MemCompare(). */
81812  assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
81813  assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
81814
81815  if( (pB1->flags|pB2->flags) & MEM_Zero ){
81816    if( pB1->flags & pB2->flags & MEM_Zero ){
81817      return pB1->u.nZero - pB2->u.nZero;
81818    }else if( pB1->flags & MEM_Zero ){
81819      if( !isAllZero(pB2->z, pB2->n) ) return -1;
81820      return pB1->u.nZero - n2;
81821    }else{
81822      if( !isAllZero(pB1->z, pB1->n) ) return +1;
81823      return n1 - pB2->u.nZero;
81824    }
81825  }
81826  c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
81827  if( c ) return c;
81828  return n1 - n2;
81829}
81830
81831/*
81832** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
81833** number.  Return negative, zero, or positive if the first (i64) is less than,
81834** equal to, or greater than the second (double).
81835*/
81836static int sqlite3IntFloatCompare(i64 i, double r){
81837  if( sizeof(LONGDOUBLE_TYPE)>8 ){
81838    LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i;
81839    if( x<r ) return -1;
81840    if( x>r ) return +1;
81841    return 0;
81842  }else{
81843    i64 y;
81844    double s;
81845    if( r<-9223372036854775808.0 ) return +1;
81846    if( r>=9223372036854775808.0 ) return -1;
81847    y = (i64)r;
81848    if( i<y ) return -1;
81849    if( i>y ) return +1;
81850    s = (double)i;
81851    if( s<r ) return -1;
81852    if( s>r ) return +1;
81853    return 0;
81854  }
81855}
81856
81857/*
81858** Compare the values contained by the two memory cells, returning
81859** negative, zero or positive if pMem1 is less than, equal to, or greater
81860** than pMem2. Sorting order is NULL's first, followed by numbers (integers
81861** and reals) sorted numerically, followed by text ordered by the collating
81862** sequence pColl and finally blob's ordered by memcmp().
81863**
81864** Two NULL values are considered equal by this function.
81865*/
81866SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
81867  int f1, f2;
81868  int combined_flags;
81869
81870  f1 = pMem1->flags;
81871  f2 = pMem2->flags;
81872  combined_flags = f1|f2;
81873  assert( !sqlite3VdbeMemIsRowSet(pMem1) && !sqlite3VdbeMemIsRowSet(pMem2) );
81874
81875  /* If one value is NULL, it is less than the other. If both values
81876  ** are NULL, return 0.
81877  */
81878  if( combined_flags&MEM_Null ){
81879    return (f2&MEM_Null) - (f1&MEM_Null);
81880  }
81881
81882  /* At least one of the two values is a number
81883  */
81884  if( combined_flags&(MEM_Int|MEM_Real|MEM_IntReal) ){
81885    testcase( combined_flags & MEM_Int );
81886    testcase( combined_flags & MEM_Real );
81887    testcase( combined_flags & MEM_IntReal );
81888    if( (f1 & f2 & (MEM_Int|MEM_IntReal))!=0 ){
81889      testcase( f1 & f2 & MEM_Int );
81890      testcase( f1 & f2 & MEM_IntReal );
81891      if( pMem1->u.i < pMem2->u.i ) return -1;
81892      if( pMem1->u.i > pMem2->u.i ) return +1;
81893      return 0;
81894    }
81895    if( (f1 & f2 & MEM_Real)!=0 ){
81896      if( pMem1->u.r < pMem2->u.r ) return -1;
81897      if( pMem1->u.r > pMem2->u.r ) return +1;
81898      return 0;
81899    }
81900    if( (f1&(MEM_Int|MEM_IntReal))!=0 ){
81901      testcase( f1 & MEM_Int );
81902      testcase( f1 & MEM_IntReal );
81903      if( (f2&MEM_Real)!=0 ){
81904        return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
81905      }else if( (f2&(MEM_Int|MEM_IntReal))!=0 ){
81906        if( pMem1->u.i < pMem2->u.i ) return -1;
81907        if( pMem1->u.i > pMem2->u.i ) return +1;
81908        return 0;
81909      }else{
81910        return -1;
81911      }
81912    }
81913    if( (f1&MEM_Real)!=0 ){
81914      if( (f2&(MEM_Int|MEM_IntReal))!=0 ){
81915        testcase( f2 & MEM_Int );
81916        testcase( f2 & MEM_IntReal );
81917        return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
81918      }else{
81919        return -1;
81920      }
81921    }
81922    return +1;
81923  }
81924
81925  /* If one value is a string and the other is a blob, the string is less.
81926  ** If both are strings, compare using the collating functions.
81927  */
81928  if( combined_flags&MEM_Str ){
81929    if( (f1 & MEM_Str)==0 ){
81930      return 1;
81931    }
81932    if( (f2 & MEM_Str)==0 ){
81933      return -1;
81934    }
81935
81936    assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
81937    assert( pMem1->enc==SQLITE_UTF8 ||
81938            pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
81939
81940    /* The collation sequence must be defined at this point, even if
81941    ** the user deletes the collation sequence after the vdbe program is
81942    ** compiled (this was not always the case).
81943    */
81944    assert( !pColl || pColl->xCmp );
81945
81946    if( pColl ){
81947      return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
81948    }
81949    /* If a NULL pointer was passed as the collate function, fall through
81950    ** to the blob case and use memcmp().  */
81951  }
81952
81953  /* Both values must be blobs.  Compare using memcmp().  */
81954  return sqlite3BlobCompare(pMem1, pMem2);
81955}
81956
81957
81958/*
81959** The first argument passed to this function is a serial-type that
81960** corresponds to an integer - all values between 1 and 9 inclusive
81961** except 7. The second points to a buffer containing an integer value
81962** serialized according to serial_type. This function deserializes
81963** and returns the value.
81964*/
81965static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
81966  u32 y;
81967  assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
81968  switch( serial_type ){
81969    case 0:
81970    case 1:
81971      testcase( aKey[0]&0x80 );
81972      return ONE_BYTE_INT(aKey);
81973    case 2:
81974      testcase( aKey[0]&0x80 );
81975      return TWO_BYTE_INT(aKey);
81976    case 3:
81977      testcase( aKey[0]&0x80 );
81978      return THREE_BYTE_INT(aKey);
81979    case 4: {
81980      testcase( aKey[0]&0x80 );
81981      y = FOUR_BYTE_UINT(aKey);
81982      return (i64)*(int*)&y;
81983    }
81984    case 5: {
81985      testcase( aKey[0]&0x80 );
81986      return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
81987    }
81988    case 6: {
81989      u64 x = FOUR_BYTE_UINT(aKey);
81990      testcase( aKey[0]&0x80 );
81991      x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
81992      return (i64)*(i64*)&x;
81993    }
81994  }
81995
81996  return (serial_type - 8);
81997}
81998
81999/*
82000** This function compares the two table rows or index records
82001** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
82002** or positive integer if key1 is less than, equal to or
82003** greater than key2.  The {nKey1, pKey1} key must be a blob
82004** created by the OP_MakeRecord opcode of the VDBE.  The pPKey2
82005** key must be a parsed key such as obtained from
82006** sqlite3VdbeParseRecord.
82007**
82008** If argument bSkip is non-zero, it is assumed that the caller has already
82009** determined that the first fields of the keys are equal.
82010**
82011** Key1 and Key2 do not have to contain the same number of fields. If all
82012** fields that appear in both keys are equal, then pPKey2->default_rc is
82013** returned.
82014**
82015** If database corruption is discovered, set pPKey2->errCode to
82016** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
82017** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
82018** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
82019*/
82020SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
82021  int nKey1, const void *pKey1,   /* Left key */
82022  UnpackedRecord *pPKey2,         /* Right key */
82023  int bSkip                       /* If true, skip the first field */
82024){
82025  u32 d1;                         /* Offset into aKey[] of next data element */
82026  int i;                          /* Index of next field to compare */
82027  u32 szHdr1;                     /* Size of record header in bytes */
82028  u32 idx1;                       /* Offset of first type in header */
82029  int rc = 0;                     /* Return value */
82030  Mem *pRhs = pPKey2->aMem;       /* Next field of pPKey2 to compare */
82031  KeyInfo *pKeyInfo;
82032  const unsigned char *aKey1 = (const unsigned char *)pKey1;
82033  Mem mem1;
82034
82035  /* If bSkip is true, then the caller has already determined that the first
82036  ** two elements in the keys are equal. Fix the various stack variables so
82037  ** that this routine begins comparing at the second field. */
82038  if( bSkip ){
82039    u32 s1;
82040    idx1 = 1 + getVarint32(&aKey1[1], s1);
82041    szHdr1 = aKey1[0];
82042    d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
82043    i = 1;
82044    pRhs++;
82045  }else{
82046    idx1 = getVarint32(aKey1, szHdr1);
82047    d1 = szHdr1;
82048    i = 0;
82049  }
82050  if( d1>(unsigned)nKey1 ){
82051    pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
82052    return 0;  /* Corruption */
82053  }
82054
82055  VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
82056  assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
82057       || CORRUPT_DB );
82058  assert( pPKey2->pKeyInfo->aSortFlags!=0 );
82059  assert( pPKey2->pKeyInfo->nKeyField>0 );
82060  assert( idx1<=szHdr1 || CORRUPT_DB );
82061  do{
82062    u32 serial_type;
82063
82064    /* RHS is an integer */
82065    if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
82066      testcase( pRhs->flags & MEM_Int );
82067      testcase( pRhs->flags & MEM_IntReal );
82068      serial_type = aKey1[idx1];
82069      testcase( serial_type==12 );
82070      if( serial_type>=10 ){
82071        rc = +1;
82072      }else if( serial_type==0 ){
82073        rc = -1;
82074      }else if( serial_type==7 ){
82075        sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
82076        rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
82077      }else{
82078        i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
82079        i64 rhs = pRhs->u.i;
82080        if( lhs<rhs ){
82081          rc = -1;
82082        }else if( lhs>rhs ){
82083          rc = +1;
82084        }
82085      }
82086    }
82087
82088    /* RHS is real */
82089    else if( pRhs->flags & MEM_Real ){
82090      serial_type = aKey1[idx1];
82091      if( serial_type>=10 ){
82092        /* Serial types 12 or greater are strings and blobs (greater than
82093        ** numbers). Types 10 and 11 are currently "reserved for future
82094        ** use", so it doesn't really matter what the results of comparing
82095        ** them to numberic values are.  */
82096        rc = +1;
82097      }else if( serial_type==0 ){
82098        rc = -1;
82099      }else{
82100        sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
82101        if( serial_type==7 ){
82102          if( mem1.u.r<pRhs->u.r ){
82103            rc = -1;
82104          }else if( mem1.u.r>pRhs->u.r ){
82105            rc = +1;
82106          }
82107        }else{
82108          rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
82109        }
82110      }
82111    }
82112
82113    /* RHS is a string */
82114    else if( pRhs->flags & MEM_Str ){
82115      getVarint32NR(&aKey1[idx1], serial_type);
82116      testcase( serial_type==12 );
82117      if( serial_type<12 ){
82118        rc = -1;
82119      }else if( !(serial_type & 0x01) ){
82120        rc = +1;
82121      }else{
82122        mem1.n = (serial_type - 12) / 2;
82123        testcase( (d1+mem1.n)==(unsigned)nKey1 );
82124        testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
82125        if( (d1+mem1.n) > (unsigned)nKey1
82126         || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
82127        ){
82128          pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
82129          return 0;                /* Corruption */
82130        }else if( pKeyInfo->aColl[i] ){
82131          mem1.enc = pKeyInfo->enc;
82132          mem1.db = pKeyInfo->db;
82133          mem1.flags = MEM_Str;
82134          mem1.z = (char*)&aKey1[d1];
82135          rc = vdbeCompareMemString(
82136              &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
82137          );
82138        }else{
82139          int nCmp = MIN(mem1.n, pRhs->n);
82140          rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
82141          if( rc==0 ) rc = mem1.n - pRhs->n;
82142        }
82143      }
82144    }
82145
82146    /* RHS is a blob */
82147    else if( pRhs->flags & MEM_Blob ){
82148      assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
82149      getVarint32NR(&aKey1[idx1], serial_type);
82150      testcase( serial_type==12 );
82151      if( serial_type<12 || (serial_type & 0x01) ){
82152        rc = -1;
82153      }else{
82154        int nStr = (serial_type - 12) / 2;
82155        testcase( (d1+nStr)==(unsigned)nKey1 );
82156        testcase( (d1+nStr+1)==(unsigned)nKey1 );
82157        if( (d1+nStr) > (unsigned)nKey1 ){
82158          pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
82159          return 0;                /* Corruption */
82160        }else if( pRhs->flags & MEM_Zero ){
82161          if( !isAllZero((const char*)&aKey1[d1],nStr) ){
82162            rc = 1;
82163          }else{
82164            rc = nStr - pRhs->u.nZero;
82165          }
82166        }else{
82167          int nCmp = MIN(nStr, pRhs->n);
82168          rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
82169          if( rc==0 ) rc = nStr - pRhs->n;
82170        }
82171      }
82172    }
82173
82174    /* RHS is null */
82175    else{
82176      serial_type = aKey1[idx1];
82177      rc = (serial_type!=0);
82178    }
82179
82180    if( rc!=0 ){
82181      int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
82182      if( sortFlags ){
82183        if( (sortFlags & KEYINFO_ORDER_BIGNULL)==0
82184         || ((sortFlags & KEYINFO_ORDER_DESC)
82185           !=(serial_type==0 || (pRhs->flags&MEM_Null)))
82186        ){
82187          rc = -rc;
82188        }
82189      }
82190      assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
82191      assert( mem1.szMalloc==0 );  /* See comment below */
82192      return rc;
82193    }
82194
82195    i++;
82196    if( i==pPKey2->nField ) break;
82197    pRhs++;
82198    d1 += sqlite3VdbeSerialTypeLen(serial_type);
82199    idx1 += sqlite3VarintLen(serial_type);
82200  }while( idx1<(unsigned)szHdr1 && d1<=(unsigned)nKey1 );
82201
82202  /* No memory allocation is ever used on mem1.  Prove this using
82203  ** the following assert().  If the assert() fails, it indicates a
82204  ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).  */
82205  assert( mem1.szMalloc==0 );
82206
82207  /* rc==0 here means that one or both of the keys ran out of fields and
82208  ** all the fields up to that point were equal. Return the default_rc
82209  ** value.  */
82210  assert( CORRUPT_DB
82211       || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
82212       || pPKey2->pKeyInfo->db->mallocFailed
82213  );
82214  pPKey2->eqSeen = 1;
82215  return pPKey2->default_rc;
82216}
82217SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
82218  int nKey1, const void *pKey1,   /* Left key */
82219  UnpackedRecord *pPKey2          /* Right key */
82220){
82221  return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
82222}
82223
82224
82225/*
82226** This function is an optimized version of sqlite3VdbeRecordCompare()
82227** that (a) the first field of pPKey2 is an integer, and (b) the
82228** size-of-header varint at the start of (pKey1/nKey1) fits in a single
82229** byte (i.e. is less than 128).
82230**
82231** To avoid concerns about buffer overreads, this routine is only used
82232** on schemas where the maximum valid header size is 63 bytes or less.
82233*/
82234static int vdbeRecordCompareInt(
82235  int nKey1, const void *pKey1, /* Left key */
82236  UnpackedRecord *pPKey2        /* Right key */
82237){
82238  const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
82239  int serial_type = ((const u8*)pKey1)[1];
82240  int res;
82241  u32 y;
82242  u64 x;
82243  i64 v;
82244  i64 lhs;
82245
82246  vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
82247  assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
82248  switch( serial_type ){
82249    case 1: { /* 1-byte signed integer */
82250      lhs = ONE_BYTE_INT(aKey);
82251      testcase( lhs<0 );
82252      break;
82253    }
82254    case 2: { /* 2-byte signed integer */
82255      lhs = TWO_BYTE_INT(aKey);
82256      testcase( lhs<0 );
82257      break;
82258    }
82259    case 3: { /* 3-byte signed integer */
82260      lhs = THREE_BYTE_INT(aKey);
82261      testcase( lhs<0 );
82262      break;
82263    }
82264    case 4: { /* 4-byte signed integer */
82265      y = FOUR_BYTE_UINT(aKey);
82266      lhs = (i64)*(int*)&y;
82267      testcase( lhs<0 );
82268      break;
82269    }
82270    case 5: { /* 6-byte signed integer */
82271      lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
82272      testcase( lhs<0 );
82273      break;
82274    }
82275    case 6: { /* 8-byte signed integer */
82276      x = FOUR_BYTE_UINT(aKey);
82277      x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
82278      lhs = *(i64*)&x;
82279      testcase( lhs<0 );
82280      break;
82281    }
82282    case 8:
82283      lhs = 0;
82284      break;
82285    case 9:
82286      lhs = 1;
82287      break;
82288
82289    /* This case could be removed without changing the results of running
82290    ** this code. Including it causes gcc to generate a faster switch
82291    ** statement (since the range of switch targets now starts at zero and
82292    ** is contiguous) but does not cause any duplicate code to be generated
82293    ** (as gcc is clever enough to combine the two like cases). Other
82294    ** compilers might be similar.  */
82295    case 0: case 7:
82296      return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
82297
82298    default:
82299      return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
82300  }
82301
82302  v = pPKey2->aMem[0].u.i;
82303  if( v>lhs ){
82304    res = pPKey2->r1;
82305  }else if( v<lhs ){
82306    res = pPKey2->r2;
82307  }else if( pPKey2->nField>1 ){
82308    /* The first fields of the two keys are equal. Compare the trailing
82309    ** fields.  */
82310    res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
82311  }else{
82312    /* The first fields of the two keys are equal and there are no trailing
82313    ** fields. Return pPKey2->default_rc in this case. */
82314    res = pPKey2->default_rc;
82315    pPKey2->eqSeen = 1;
82316  }
82317
82318  assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
82319  return res;
82320}
82321
82322/*
82323** This function is an optimized version of sqlite3VdbeRecordCompare()
82324** that (a) the first field of pPKey2 is a string, that (b) the first field
82325** uses the collation sequence BINARY and (c) that the size-of-header varint
82326** at the start of (pKey1/nKey1) fits in a single byte.
82327*/
82328static int vdbeRecordCompareString(
82329  int nKey1, const void *pKey1, /* Left key */
82330  UnpackedRecord *pPKey2        /* Right key */
82331){
82332  const u8 *aKey1 = (const u8*)pKey1;
82333  int serial_type;
82334  int res;
82335
82336  assert( pPKey2->aMem[0].flags & MEM_Str );
82337  vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
82338  serial_type = (u8)(aKey1[1]);
82339  if( serial_type >= 0x80 ){
82340    sqlite3GetVarint32(&aKey1[1], (u32*)&serial_type);
82341  }
82342  if( serial_type<12 ){
82343    res = pPKey2->r1;      /* (pKey1/nKey1) is a number or a null */
82344  }else if( !(serial_type & 0x01) ){
82345    res = pPKey2->r2;      /* (pKey1/nKey1) is a blob */
82346  }else{
82347    int nCmp;
82348    int nStr;
82349    int szHdr = aKey1[0];
82350
82351    nStr = (serial_type-12) / 2;
82352    if( (szHdr + nStr) > nKey1 ){
82353      pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
82354      return 0;    /* Corruption */
82355    }
82356    nCmp = MIN( pPKey2->aMem[0].n, nStr );
82357    res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
82358
82359    if( res>0 ){
82360      res = pPKey2->r2;
82361    }else if( res<0 ){
82362      res = pPKey2->r1;
82363    }else{
82364      res = nStr - pPKey2->aMem[0].n;
82365      if( res==0 ){
82366        if( pPKey2->nField>1 ){
82367          res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
82368        }else{
82369          res = pPKey2->default_rc;
82370          pPKey2->eqSeen = 1;
82371        }
82372      }else if( res>0 ){
82373        res = pPKey2->r2;
82374      }else{
82375        res = pPKey2->r1;
82376      }
82377    }
82378  }
82379
82380  assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)
82381       || CORRUPT_DB
82382       || pPKey2->pKeyInfo->db->mallocFailed
82383  );
82384  return res;
82385}
82386
82387/*
82388** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
82389** suitable for comparing serialized records to the unpacked record passed
82390** as the only argument.
82391*/
82392SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
82393  /* varintRecordCompareInt() and varintRecordCompareString() both assume
82394  ** that the size-of-header varint that occurs at the start of each record
82395  ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
82396  ** also assumes that it is safe to overread a buffer by at least the
82397  ** maximum possible legal header size plus 8 bytes. Because there is
82398  ** guaranteed to be at least 74 (but not 136) bytes of padding following each
82399  ** buffer passed to varintRecordCompareInt() this makes it convenient to
82400  ** limit the size of the header to 64 bytes in cases where the first field
82401  ** is an integer.
82402  **
82403  ** The easiest way to enforce this limit is to consider only records with
82404  ** 13 fields or less. If the first field is an integer, the maximum legal
82405  ** header size is (12*5 + 1 + 1) bytes.  */
82406  if( p->pKeyInfo->nAllField<=13 ){
82407    int flags = p->aMem[0].flags;
82408    if( p->pKeyInfo->aSortFlags[0] ){
82409      if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){
82410        return sqlite3VdbeRecordCompare;
82411      }
82412      p->r1 = 1;
82413      p->r2 = -1;
82414    }else{
82415      p->r1 = -1;
82416      p->r2 = 1;
82417    }
82418    if( (flags & MEM_Int) ){
82419      return vdbeRecordCompareInt;
82420    }
82421    testcase( flags & MEM_Real );
82422    testcase( flags & MEM_Null );
82423    testcase( flags & MEM_Blob );
82424    if( (flags & (MEM_Real|MEM_IntReal|MEM_Null|MEM_Blob))==0
82425     && p->pKeyInfo->aColl[0]==0
82426    ){
82427      assert( flags & MEM_Str );
82428      return vdbeRecordCompareString;
82429    }
82430  }
82431
82432  return sqlite3VdbeRecordCompare;
82433}
82434
82435/*
82436** pCur points at an index entry created using the OP_MakeRecord opcode.
82437** Read the rowid (the last field in the record) and store it in *rowid.
82438** Return SQLITE_OK if everything works, or an error code otherwise.
82439**
82440** pCur might be pointing to text obtained from a corrupt database file.
82441** So the content cannot be trusted.  Do appropriate checks on the content.
82442*/
82443SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
82444  i64 nCellKey = 0;
82445  int rc;
82446  u32 szHdr;        /* Size of the header */
82447  u32 typeRowid;    /* Serial type of the rowid */
82448  u32 lenRowid;     /* Size of the rowid */
82449  Mem m, v;
82450
82451  /* Get the size of the index entry.  Only indices entries of less
82452  ** than 2GiB are support - anything large must be database corruption.
82453  ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
82454  ** this code can safely assume that nCellKey is 32-bits
82455  */
82456  assert( sqlite3BtreeCursorIsValid(pCur) );
82457  nCellKey = sqlite3BtreePayloadSize(pCur);
82458  assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
82459
82460  /* Read in the complete content of the index entry */
82461  sqlite3VdbeMemInit(&m, db, 0);
82462  rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
82463  if( rc ){
82464    return rc;
82465  }
82466
82467  /* The index entry must begin with a header size */
82468  getVarint32NR((u8*)m.z, szHdr);
82469  testcase( szHdr==3 );
82470  testcase( szHdr==m.n );
82471  testcase( szHdr>0x7fffffff );
82472  assert( m.n>=0 );
82473  if( unlikely(szHdr<3 || szHdr>(unsigned)m.n) ){
82474    goto idx_rowid_corruption;
82475  }
82476
82477  /* The last field of the index should be an integer - the ROWID.
82478  ** Verify that the last entry really is an integer. */
82479  getVarint32NR((u8*)&m.z[szHdr-1], typeRowid);
82480  testcase( typeRowid==1 );
82481  testcase( typeRowid==2 );
82482  testcase( typeRowid==3 );
82483  testcase( typeRowid==4 );
82484  testcase( typeRowid==5 );
82485  testcase( typeRowid==6 );
82486  testcase( typeRowid==8 );
82487  testcase( typeRowid==9 );
82488  if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
82489    goto idx_rowid_corruption;
82490  }
82491  lenRowid = sqlite3SmallTypeSizes[typeRowid];
82492  testcase( (u32)m.n==szHdr+lenRowid );
82493  if( unlikely((u32)m.n<szHdr+lenRowid) ){
82494    goto idx_rowid_corruption;
82495  }
82496
82497  /* Fetch the integer off the end of the index record */
82498  sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
82499  *rowid = v.u.i;
82500  sqlite3VdbeMemRelease(&m);
82501  return SQLITE_OK;
82502
82503  /* Jump here if database corruption is detected after m has been
82504  ** allocated.  Free the m object and return SQLITE_CORRUPT. */
82505idx_rowid_corruption:
82506  testcase( m.szMalloc!=0 );
82507  sqlite3VdbeMemRelease(&m);
82508  return SQLITE_CORRUPT_BKPT;
82509}
82510
82511/*
82512** Compare the key of the index entry that cursor pC is pointing to against
82513** the key string in pUnpacked.  Write into *pRes a number
82514** that is negative, zero, or positive if pC is less than, equal to,
82515** or greater than pUnpacked.  Return SQLITE_OK on success.
82516**
82517** pUnpacked is either created without a rowid or is truncated so that it
82518** omits the rowid at the end.  The rowid at the end of the index entry
82519** is ignored as well.  Hence, this routine only compares the prefixes
82520** of the keys prior to the final rowid, not the entire key.
82521*/
82522SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
82523  sqlite3 *db,                     /* Database connection */
82524  VdbeCursor *pC,                  /* The cursor to compare against */
82525  UnpackedRecord *pUnpacked,       /* Unpacked version of key */
82526  int *res                         /* Write the comparison result here */
82527){
82528  i64 nCellKey = 0;
82529  int rc;
82530  BtCursor *pCur;
82531  Mem m;
82532
82533  assert( pC->eCurType==CURTYPE_BTREE );
82534  pCur = pC->uc.pCursor;
82535  assert( sqlite3BtreeCursorIsValid(pCur) );
82536  nCellKey = sqlite3BtreePayloadSize(pCur);
82537  /* nCellKey will always be between 0 and 0xffffffff because of the way
82538  ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
82539  if( nCellKey<=0 || nCellKey>0x7fffffff ){
82540    *res = 0;
82541    return SQLITE_CORRUPT_BKPT;
82542  }
82543  sqlite3VdbeMemInit(&m, db, 0);
82544  rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
82545  if( rc ){
82546    return rc;
82547  }
82548  *res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, pUnpacked, 0);
82549  sqlite3VdbeMemRelease(&m);
82550  return SQLITE_OK;
82551}
82552
82553/*
82554** This routine sets the value to be returned by subsequent calls to
82555** sqlite3_changes() on the database handle 'db'.
82556*/
82557SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
82558  assert( sqlite3_mutex_held(db->mutex) );
82559  db->nChange = nChange;
82560  db->nTotalChange += nChange;
82561}
82562
82563/*
82564** Set a flag in the vdbe to update the change counter when it is finalised
82565** or reset.
82566*/
82567SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
82568  v->changeCntOn = 1;
82569}
82570
82571/*
82572** Mark every prepared statement associated with a database connection
82573** as expired.
82574**
82575** An expired statement means that recompilation of the statement is
82576** recommend.  Statements expire when things happen that make their
82577** programs obsolete.  Removing user-defined functions or collating
82578** sequences, or changing an authorization function are the types of
82579** things that make prepared statements obsolete.
82580**
82581** If iCode is 1, then expiration is advisory.  The statement should
82582** be reprepared before being restarted, but if it is already running
82583** it is allowed to run to completion.
82584**
82585** Internally, this function just sets the Vdbe.expired flag on all
82586** prepared statements.  The flag is set to 1 for an immediate expiration
82587** and set to 2 for an advisory expiration.
82588*/
82589SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){
82590  Vdbe *p;
82591  for(p = db->pVdbe; p; p=p->pNext){
82592    p->expired = iCode+1;
82593  }
82594}
82595
82596/*
82597** Return the database associated with the Vdbe.
82598*/
82599SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
82600  return v->db;
82601}
82602
82603/*
82604** Return the SQLITE_PREPARE flags for a Vdbe.
82605*/
82606SQLITE_PRIVATE u8 sqlite3VdbePrepareFlags(Vdbe *v){
82607  return v->prepFlags;
82608}
82609
82610/*
82611** Return a pointer to an sqlite3_value structure containing the value bound
82612** parameter iVar of VM v. Except, if the value is an SQL NULL, return
82613** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
82614** constants) to the value before returning it.
82615**
82616** The returned value must be freed by the caller using sqlite3ValueFree().
82617*/
82618SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
82619  assert( iVar>0 );
82620  if( v ){
82621    Mem *pMem = &v->aVar[iVar-1];
82622    assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
82623    if( 0==(pMem->flags & MEM_Null) ){
82624      sqlite3_value *pRet = sqlite3ValueNew(v->db);
82625      if( pRet ){
82626        sqlite3VdbeMemCopy((Mem *)pRet, pMem);
82627        sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
82628      }
82629      return pRet;
82630    }
82631  }
82632  return 0;
82633}
82634
82635/*
82636** Configure SQL variable iVar so that binding a new value to it signals
82637** to sqlite3_reoptimize() that re-preparing the statement may result
82638** in a better query plan.
82639*/
82640SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
82641  assert( iVar>0 );
82642  assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
82643  if( iVar>=32 ){
82644    v->expmask |= 0x80000000;
82645  }else{
82646    v->expmask |= ((u32)1 << (iVar-1));
82647  }
82648}
82649
82650/*
82651** Cause a function to throw an error if it was call from OP_PureFunc
82652** rather than OP_Function.
82653**
82654** OP_PureFunc means that the function must be deterministic, and should
82655** throw an error if it is given inputs that would make it non-deterministic.
82656** This routine is invoked by date/time functions that use non-deterministic
82657** features such as 'now'.
82658*/
82659SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context *pCtx){
82660  const VdbeOp *pOp;
82661#ifdef SQLITE_ENABLE_STAT4
82662  if( pCtx->pVdbe==0 ) return 1;
82663#endif
82664  pOp = pCtx->pVdbe->aOp + pCtx->iOp;
82665  if( pOp->opcode==OP_PureFunc ){
82666    const char *zContext;
82667    char *zMsg;
82668    if( pOp->p5 & NC_IsCheck ){
82669      zContext = "a CHECK constraint";
82670    }else if( pOp->p5 & NC_GenCol ){
82671      zContext = "a generated column";
82672    }else{
82673      zContext = "an index";
82674    }
82675    zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s",
82676                           pCtx->pFunc->zName, zContext);
82677    sqlite3_result_error(pCtx, zMsg, -1);
82678    sqlite3_free(zMsg);
82679    return 0;
82680  }
82681  return 1;
82682}
82683
82684#ifndef SQLITE_OMIT_VIRTUALTABLE
82685/*
82686** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
82687** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
82688** in memory obtained from sqlite3DbMalloc).
82689*/
82690SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
82691  if( pVtab->zErrMsg ){
82692    sqlite3 *db = p->db;
82693    sqlite3DbFree(db, p->zErrMsg);
82694    p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
82695    sqlite3_free(pVtab->zErrMsg);
82696    pVtab->zErrMsg = 0;
82697  }
82698}
82699#endif /* SQLITE_OMIT_VIRTUALTABLE */
82700
82701#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82702
82703/*
82704** If the second argument is not NULL, release any allocations associated
82705** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
82706** structure itself, using sqlite3DbFree().
82707**
82708** This function is used to free UnpackedRecord structures allocated by
82709** the vdbeUnpackRecord() function found in vdbeapi.c.
82710*/
82711static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
82712  if( p ){
82713    int i;
82714    for(i=0; i<nField; i++){
82715      Mem *pMem = &p->aMem[i];
82716      if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
82717    }
82718    sqlite3DbFreeNN(db, p);
82719  }
82720}
82721#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
82722
82723#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82724/*
82725** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
82726** then cursor passed as the second argument should point to the row about
82727** to be update or deleted. If the application calls sqlite3_preupdate_old(),
82728** the required value will be read from the row the cursor points to.
82729*/
82730SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(
82731  Vdbe *v,                        /* Vdbe pre-update hook is invoked by */
82732  VdbeCursor *pCsr,               /* Cursor to grab old.* values from */
82733  int op,                         /* SQLITE_INSERT, UPDATE or DELETE */
82734  const char *zDb,                /* Database name */
82735  Table *pTab,                    /* Modified table */
82736  i64 iKey1,                      /* Initial key value */
82737  int iReg                        /* Register for new.* record */
82738){
82739  sqlite3 *db = v->db;
82740  i64 iKey2;
82741  PreUpdate preupdate;
82742  const char *zTbl = pTab->zName;
82743  static const u8 fakeSortOrder = 0;
82744
82745  assert( db->pPreUpdate==0 );
82746  memset(&preupdate, 0, sizeof(PreUpdate));
82747  if( HasRowid(pTab)==0 ){
82748    iKey1 = iKey2 = 0;
82749    preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
82750  }else{
82751    if( op==SQLITE_UPDATE ){
82752      iKey2 = v->aMem[iReg].u.i;
82753    }else{
82754      iKey2 = iKey1;
82755    }
82756  }
82757
82758  assert( pCsr->nField==pTab->nCol
82759       || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
82760  );
82761
82762  preupdate.v = v;
82763  preupdate.pCsr = pCsr;
82764  preupdate.op = op;
82765  preupdate.iNewReg = iReg;
82766  preupdate.keyinfo.db = db;
82767  preupdate.keyinfo.enc = ENC(db);
82768  preupdate.keyinfo.nKeyField = pTab->nCol;
82769  preupdate.keyinfo.aSortFlags = (u8*)&fakeSortOrder;
82770  preupdate.iKey1 = iKey1;
82771  preupdate.iKey2 = iKey2;
82772  preupdate.pTab = pTab;
82773
82774  db->pPreUpdate = &preupdate;
82775  db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
82776  db->pPreUpdate = 0;
82777  sqlite3DbFree(db, preupdate.aRecord);
82778  vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked);
82779  vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked);
82780  if( preupdate.aNew ){
82781    int i;
82782    for(i=0; i<pCsr->nField; i++){
82783      sqlite3VdbeMemRelease(&preupdate.aNew[i]);
82784    }
82785    sqlite3DbFreeNN(db, preupdate.aNew);
82786  }
82787}
82788#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
82789
82790/************** End of vdbeaux.c *********************************************/
82791/************** Begin file vdbeapi.c *****************************************/
82792/*
82793** 2004 May 26
82794**
82795** The author disclaims copyright to this source code.  In place of
82796** a legal notice, here is a blessing:
82797**
82798**    May you do good and not evil.
82799**    May you find forgiveness for yourself and forgive others.
82800**    May you share freely, never taking more than you give.
82801**
82802*************************************************************************
82803**
82804** This file contains code use to implement APIs that are part of the
82805** VDBE.
82806*/
82807/* #include "sqliteInt.h" */
82808/* #include "vdbeInt.h" */
82809
82810#ifndef SQLITE_OMIT_DEPRECATED
82811/*
82812** Return TRUE (non-zero) of the statement supplied as an argument needs
82813** to be recompiled.  A statement needs to be recompiled whenever the
82814** execution environment changes in a way that would alter the program
82815** that sqlite3_prepare() generates.  For example, if new functions or
82816** collating sequences are registered or if an authorizer function is
82817** added or changed.
82818*/
82819SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
82820  Vdbe *p = (Vdbe*)pStmt;
82821  return p==0 || p->expired;
82822}
82823#endif
82824
82825/*
82826** Check on a Vdbe to make sure it has not been finalized.  Log
82827** an error and return true if it has been finalized (or is otherwise
82828** invalid).  Return false if it is ok.
82829*/
82830static int vdbeSafety(Vdbe *p){
82831  if( p->db==0 ){
82832    sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
82833    return 1;
82834  }else{
82835    return 0;
82836  }
82837}
82838static int vdbeSafetyNotNull(Vdbe *p){
82839  if( p==0 ){
82840    sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
82841    return 1;
82842  }else{
82843    return vdbeSafety(p);
82844  }
82845}
82846
82847#ifndef SQLITE_OMIT_TRACE
82848/*
82849** Invoke the profile callback.  This routine is only called if we already
82850** know that the profile callback is defined and needs to be invoked.
82851*/
82852static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
82853  sqlite3_int64 iNow;
82854  sqlite3_int64 iElapse;
82855  assert( p->startTime>0 );
82856  assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
82857  assert( db->init.busy==0 );
82858  assert( p->zSql!=0 );
82859  sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
82860  iElapse = (iNow - p->startTime)*1000000;
82861#ifndef SQLITE_OMIT_DEPRECATED
82862  if( db->xProfile ){
82863    db->xProfile(db->pProfileArg, p->zSql, iElapse);
82864  }
82865#endif
82866  if( db->mTrace & SQLITE_TRACE_PROFILE ){
82867    db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
82868  }
82869  p->startTime = 0;
82870}
82871/*
82872** The checkProfileCallback(DB,P) macro checks to see if a profile callback
82873** is needed, and it invokes the callback if it is needed.
82874*/
82875# define checkProfileCallback(DB,P) \
82876   if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
82877#else
82878# define checkProfileCallback(DB,P)  /*no-op*/
82879#endif
82880
82881/*
82882** The following routine destroys a virtual machine that is created by
82883** the sqlite3_compile() routine. The integer returned is an SQLITE_
82884** success/failure code that describes the result of executing the virtual
82885** machine.
82886**
82887** This routine sets the error code and string returned by
82888** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
82889*/
82890SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
82891  int rc;
82892  if( pStmt==0 ){
82893    /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
82894    ** pointer is a harmless no-op. */
82895    rc = SQLITE_OK;
82896  }else{
82897    Vdbe *v = (Vdbe*)pStmt;
82898    sqlite3 *db = v->db;
82899    if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
82900    sqlite3_mutex_enter(db->mutex);
82901    checkProfileCallback(db, v);
82902    rc = sqlite3VdbeFinalize(v);
82903    rc = sqlite3ApiExit(db, rc);
82904    sqlite3LeaveMutexAndCloseZombie(db);
82905  }
82906  return rc;
82907}
82908
82909/*
82910** Terminate the current execution of an SQL statement and reset it
82911** back to its starting state so that it can be reused. A success code from
82912** the prior execution is returned.
82913**
82914** This routine sets the error code and string returned by
82915** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
82916*/
82917SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
82918  int rc;
82919  if( pStmt==0 ){
82920    rc = SQLITE_OK;
82921  }else{
82922    Vdbe *v = (Vdbe*)pStmt;
82923    sqlite3 *db = v->db;
82924    sqlite3_mutex_enter(db->mutex);
82925    checkProfileCallback(db, v);
82926    rc = sqlite3VdbeReset(v);
82927    sqlite3VdbeRewind(v);
82928    assert( (rc & (db->errMask))==rc );
82929    rc = sqlite3ApiExit(db, rc);
82930    sqlite3_mutex_leave(db->mutex);
82931  }
82932  return rc;
82933}
82934
82935/*
82936** Set all the parameters in the compiled SQL statement to NULL.
82937*/
82938SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
82939  int i;
82940  int rc = SQLITE_OK;
82941  Vdbe *p = (Vdbe*)pStmt;
82942#if SQLITE_THREADSAFE
82943  sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
82944#endif
82945  sqlite3_mutex_enter(mutex);
82946  for(i=0; i<p->nVar; i++){
82947    sqlite3VdbeMemRelease(&p->aVar[i]);
82948    p->aVar[i].flags = MEM_Null;
82949  }
82950  assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
82951  if( p->expmask ){
82952    p->expired = 1;
82953  }
82954  sqlite3_mutex_leave(mutex);
82955  return rc;
82956}
82957
82958
82959/**************************** sqlite3_value_  *******************************
82960** The following routines extract information from a Mem or sqlite3_value
82961** structure.
82962*/
82963SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
82964  Mem *p = (Mem*)pVal;
82965  if( p->flags & (MEM_Blob|MEM_Str) ){
82966    if( ExpandBlob(p)!=SQLITE_OK ){
82967      assert( p->flags==MEM_Null && p->z==0 );
82968      return 0;
82969    }
82970    p->flags |= MEM_Blob;
82971    return p->n ? p->z : 0;
82972  }else{
82973    return sqlite3_value_text(pVal);
82974  }
82975}
82976SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
82977  return sqlite3ValueBytes(pVal, SQLITE_UTF8);
82978}
82979SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
82980  return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
82981}
82982SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
82983  return sqlite3VdbeRealValue((Mem*)pVal);
82984}
82985SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
82986  return (int)sqlite3VdbeIntValue((Mem*)pVal);
82987}
82988SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
82989  return sqlite3VdbeIntValue((Mem*)pVal);
82990}
82991SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
82992  Mem *pMem = (Mem*)pVal;
82993  return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
82994}
82995SQLITE_API void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){
82996  Mem *p = (Mem*)pVal;
82997  if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
82998                 (MEM_Null|MEM_Term|MEM_Subtype)
82999   && zPType!=0
83000   && p->eSubtype=='p'
83001   && strcmp(p->u.zPType, zPType)==0
83002  ){
83003    return (void*)p->z;
83004  }else{
83005    return 0;
83006  }
83007}
83008SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
83009  return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
83010}
83011#ifndef SQLITE_OMIT_UTF16
83012SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
83013  return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
83014}
83015SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
83016  return sqlite3ValueText(pVal, SQLITE_UTF16BE);
83017}
83018SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
83019  return sqlite3ValueText(pVal, SQLITE_UTF16LE);
83020}
83021#endif /* SQLITE_OMIT_UTF16 */
83022/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
83023** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
83024** point number string BLOB NULL
83025*/
83026SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
83027  static const u8 aType[] = {
83028     SQLITE_BLOB,     /* 0x00 (not possible) */
83029     SQLITE_NULL,     /* 0x01 NULL */
83030     SQLITE_TEXT,     /* 0x02 TEXT */
83031     SQLITE_NULL,     /* 0x03 (not possible) */
83032     SQLITE_INTEGER,  /* 0x04 INTEGER */
83033     SQLITE_NULL,     /* 0x05 (not possible) */
83034     SQLITE_INTEGER,  /* 0x06 INTEGER + TEXT */
83035     SQLITE_NULL,     /* 0x07 (not possible) */
83036     SQLITE_FLOAT,    /* 0x08 FLOAT */
83037     SQLITE_NULL,     /* 0x09 (not possible) */
83038     SQLITE_FLOAT,    /* 0x0a FLOAT + TEXT */
83039     SQLITE_NULL,     /* 0x0b (not possible) */
83040     SQLITE_INTEGER,  /* 0x0c (not possible) */
83041     SQLITE_NULL,     /* 0x0d (not possible) */
83042     SQLITE_INTEGER,  /* 0x0e (not possible) */
83043     SQLITE_NULL,     /* 0x0f (not possible) */
83044     SQLITE_BLOB,     /* 0x10 BLOB */
83045     SQLITE_NULL,     /* 0x11 (not possible) */
83046     SQLITE_TEXT,     /* 0x12 (not possible) */
83047     SQLITE_NULL,     /* 0x13 (not possible) */
83048     SQLITE_INTEGER,  /* 0x14 INTEGER + BLOB */
83049     SQLITE_NULL,     /* 0x15 (not possible) */
83050     SQLITE_INTEGER,  /* 0x16 (not possible) */
83051     SQLITE_NULL,     /* 0x17 (not possible) */
83052     SQLITE_FLOAT,    /* 0x18 FLOAT + BLOB */
83053     SQLITE_NULL,     /* 0x19 (not possible) */
83054     SQLITE_FLOAT,    /* 0x1a (not possible) */
83055     SQLITE_NULL,     /* 0x1b (not possible) */
83056     SQLITE_INTEGER,  /* 0x1c (not possible) */
83057     SQLITE_NULL,     /* 0x1d (not possible) */
83058     SQLITE_INTEGER,  /* 0x1e (not possible) */
83059     SQLITE_NULL,     /* 0x1f (not possible) */
83060     SQLITE_FLOAT,    /* 0x20 INTREAL */
83061     SQLITE_NULL,     /* 0x21 (not possible) */
83062     SQLITE_TEXT,     /* 0x22 INTREAL + TEXT */
83063     SQLITE_NULL,     /* 0x23 (not possible) */
83064     SQLITE_FLOAT,    /* 0x24 (not possible) */
83065     SQLITE_NULL,     /* 0x25 (not possible) */
83066     SQLITE_FLOAT,    /* 0x26 (not possible) */
83067     SQLITE_NULL,     /* 0x27 (not possible) */
83068     SQLITE_FLOAT,    /* 0x28 (not possible) */
83069     SQLITE_NULL,     /* 0x29 (not possible) */
83070     SQLITE_FLOAT,    /* 0x2a (not possible) */
83071     SQLITE_NULL,     /* 0x2b (not possible) */
83072     SQLITE_FLOAT,    /* 0x2c (not possible) */
83073     SQLITE_NULL,     /* 0x2d (not possible) */
83074     SQLITE_FLOAT,    /* 0x2e (not possible) */
83075     SQLITE_NULL,     /* 0x2f (not possible) */
83076     SQLITE_BLOB,     /* 0x30 (not possible) */
83077     SQLITE_NULL,     /* 0x31 (not possible) */
83078     SQLITE_TEXT,     /* 0x32 (not possible) */
83079     SQLITE_NULL,     /* 0x33 (not possible) */
83080     SQLITE_FLOAT,    /* 0x34 (not possible) */
83081     SQLITE_NULL,     /* 0x35 (not possible) */
83082     SQLITE_FLOAT,    /* 0x36 (not possible) */
83083     SQLITE_NULL,     /* 0x37 (not possible) */
83084     SQLITE_FLOAT,    /* 0x38 (not possible) */
83085     SQLITE_NULL,     /* 0x39 (not possible) */
83086     SQLITE_FLOAT,    /* 0x3a (not possible) */
83087     SQLITE_NULL,     /* 0x3b (not possible) */
83088     SQLITE_FLOAT,    /* 0x3c (not possible) */
83089     SQLITE_NULL,     /* 0x3d (not possible) */
83090     SQLITE_FLOAT,    /* 0x3e (not possible) */
83091     SQLITE_NULL,     /* 0x3f (not possible) */
83092  };
83093#ifdef SQLITE_DEBUG
83094  {
83095    int eType = SQLITE_BLOB;
83096    if( pVal->flags & MEM_Null ){
83097      eType = SQLITE_NULL;
83098    }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
83099      eType = SQLITE_FLOAT;
83100    }else if( pVal->flags & MEM_Int ){
83101      eType = SQLITE_INTEGER;
83102    }else if( pVal->flags & MEM_Str ){
83103      eType = SQLITE_TEXT;
83104    }
83105    assert( eType == aType[pVal->flags&MEM_AffMask] );
83106  }
83107#endif
83108  return aType[pVal->flags&MEM_AffMask];
83109}
83110
83111/* Return true if a parameter to xUpdate represents an unchanged column */
83112SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
83113  return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
83114}
83115
83116/* Return true if a parameter value originated from an sqlite3_bind() */
83117SQLITE_API int sqlite3_value_frombind(sqlite3_value *pVal){
83118  return (pVal->flags&MEM_FromBind)!=0;
83119}
83120
83121/* Make a copy of an sqlite3_value object
83122*/
83123SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
83124  sqlite3_value *pNew;
83125  if( pOrig==0 ) return 0;
83126  pNew = sqlite3_malloc( sizeof(*pNew) );
83127  if( pNew==0 ) return 0;
83128  memset(pNew, 0, sizeof(*pNew));
83129  memcpy(pNew, pOrig, MEMCELLSIZE);
83130  pNew->flags &= ~MEM_Dyn;
83131  pNew->db = 0;
83132  if( pNew->flags&(MEM_Str|MEM_Blob) ){
83133    pNew->flags &= ~(MEM_Static|MEM_Dyn);
83134    pNew->flags |= MEM_Ephem;
83135    if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
83136      sqlite3ValueFree(pNew);
83137      pNew = 0;
83138    }
83139  }
83140  return pNew;
83141}
83142
83143/* Destroy an sqlite3_value object previously obtained from
83144** sqlite3_value_dup().
83145*/
83146SQLITE_API void sqlite3_value_free(sqlite3_value *pOld){
83147  sqlite3ValueFree(pOld);
83148}
83149
83150
83151/**************************** sqlite3_result_  *******************************
83152** The following routines are used by user-defined functions to specify
83153** the function result.
83154**
83155** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
83156** result as a string or blob but if the string or blob is too large, it
83157** then sets the error code to SQLITE_TOOBIG
83158**
83159** The invokeValueDestructor(P,X) routine invokes destructor function X()
83160** on value P is not going to be used and need to be destroyed.
83161*/
83162static void setResultStrOrError(
83163  sqlite3_context *pCtx,  /* Function context */
83164  const char *z,          /* String pointer */
83165  int n,                  /* Bytes in string, or negative */
83166  u8 enc,                 /* Encoding of z.  0 for BLOBs */
83167  void (*xDel)(void*)     /* Destructor function */
83168){
83169  if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
83170    sqlite3_result_error_toobig(pCtx);
83171  }
83172}
83173static int invokeValueDestructor(
83174  const void *p,             /* Value to destroy */
83175  void (*xDel)(void*),       /* The destructor */
83176  sqlite3_context *pCtx      /* Set a SQLITE_TOOBIG error if no NULL */
83177){
83178  assert( xDel!=SQLITE_DYNAMIC );
83179  if( xDel==0 ){
83180    /* noop */
83181  }else if( xDel==SQLITE_TRANSIENT ){
83182    /* noop */
83183  }else{
83184    xDel((void*)p);
83185  }
83186  if( pCtx ) sqlite3_result_error_toobig(pCtx);
83187  return SQLITE_TOOBIG;
83188}
83189SQLITE_API void sqlite3_result_blob(
83190  sqlite3_context *pCtx,
83191  const void *z,
83192  int n,
83193  void (*xDel)(void *)
83194){
83195  assert( n>=0 );
83196  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83197  setResultStrOrError(pCtx, z, n, 0, xDel);
83198}
83199SQLITE_API void sqlite3_result_blob64(
83200  sqlite3_context *pCtx,
83201  const void *z,
83202  sqlite3_uint64 n,
83203  void (*xDel)(void *)
83204){
83205  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83206  assert( xDel!=SQLITE_DYNAMIC );
83207  if( n>0x7fffffff ){
83208    (void)invokeValueDestructor(z, xDel, pCtx);
83209  }else{
83210    setResultStrOrError(pCtx, z, (int)n, 0, xDel);
83211  }
83212}
83213SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
83214  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83215  sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
83216}
83217SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
83218  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83219  pCtx->isError = SQLITE_ERROR;
83220  sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
83221}
83222#ifndef SQLITE_OMIT_UTF16
83223SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
83224  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83225  pCtx->isError = SQLITE_ERROR;
83226  sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
83227}
83228#endif
83229SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
83230  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83231  sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
83232}
83233SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
83234  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83235  sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
83236}
83237SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
83238  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83239  sqlite3VdbeMemSetNull(pCtx->pOut);
83240}
83241SQLITE_API void sqlite3_result_pointer(
83242  sqlite3_context *pCtx,
83243  void *pPtr,
83244  const char *zPType,
83245  void (*xDestructor)(void*)
83246){
83247  Mem *pOut = pCtx->pOut;
83248  assert( sqlite3_mutex_held(pOut->db->mutex) );
83249  sqlite3VdbeMemRelease(pOut);
83250  pOut->flags = MEM_Null;
83251  sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor);
83252}
83253SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
83254  Mem *pOut = pCtx->pOut;
83255  assert( sqlite3_mutex_held(pOut->db->mutex) );
83256  pOut->eSubtype = eSubtype & 0xff;
83257  pOut->flags |= MEM_Subtype;
83258}
83259SQLITE_API void sqlite3_result_text(
83260  sqlite3_context *pCtx,
83261  const char *z,
83262  int n,
83263  void (*xDel)(void *)
83264){
83265  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83266  setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
83267}
83268SQLITE_API void sqlite3_result_text64(
83269  sqlite3_context *pCtx,
83270  const char *z,
83271  sqlite3_uint64 n,
83272  void (*xDel)(void *),
83273  unsigned char enc
83274){
83275  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83276  assert( xDel!=SQLITE_DYNAMIC );
83277  if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
83278  if( n>0x7fffffff ){
83279    (void)invokeValueDestructor(z, xDel, pCtx);
83280  }else{
83281    setResultStrOrError(pCtx, z, (int)n, enc, xDel);
83282  }
83283}
83284#ifndef SQLITE_OMIT_UTF16
83285SQLITE_API void sqlite3_result_text16(
83286  sqlite3_context *pCtx,
83287  const void *z,
83288  int n,
83289  void (*xDel)(void *)
83290){
83291  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83292  setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
83293}
83294SQLITE_API void sqlite3_result_text16be(
83295  sqlite3_context *pCtx,
83296  const void *z,
83297  int n,
83298  void (*xDel)(void *)
83299){
83300  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83301  setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
83302}
83303SQLITE_API void sqlite3_result_text16le(
83304  sqlite3_context *pCtx,
83305  const void *z,
83306  int n,
83307  void (*xDel)(void *)
83308){
83309  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83310  setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
83311}
83312#endif /* SQLITE_OMIT_UTF16 */
83313SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
83314  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83315  sqlite3VdbeMemCopy(pCtx->pOut, pValue);
83316}
83317SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
83318  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83319  sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
83320}
83321SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
83322  Mem *pOut = pCtx->pOut;
83323  assert( sqlite3_mutex_held(pOut->db->mutex) );
83324  if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
83325    return SQLITE_TOOBIG;
83326  }
83327  sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
83328  return SQLITE_OK;
83329}
83330SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
83331  pCtx->isError = errCode ? errCode : -1;
83332#ifdef SQLITE_DEBUG
83333  if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
83334#endif
83335  if( pCtx->pOut->flags & MEM_Null ){
83336    sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
83337                         SQLITE_UTF8, SQLITE_STATIC);
83338  }
83339}
83340
83341/* Force an SQLITE_TOOBIG error. */
83342SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
83343  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83344  pCtx->isError = SQLITE_TOOBIG;
83345  sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
83346                       SQLITE_UTF8, SQLITE_STATIC);
83347}
83348
83349/* An SQLITE_NOMEM error. */
83350SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
83351  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83352  sqlite3VdbeMemSetNull(pCtx->pOut);
83353  pCtx->isError = SQLITE_NOMEM_BKPT;
83354  sqlite3OomFault(pCtx->pOut->db);
83355}
83356
83357#ifndef SQLITE_UNTESTABLE
83358/* Force the INT64 value currently stored as the result to be
83359** a MEM_IntReal value.  See the SQLITE_TESTCTRL_RESULT_INTREAL
83360** test-control.
83361*/
83362SQLITE_PRIVATE void sqlite3ResultIntReal(sqlite3_context *pCtx){
83363  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83364  if( pCtx->pOut->flags & MEM_Int ){
83365    pCtx->pOut->flags &= ~MEM_Int;
83366    pCtx->pOut->flags |= MEM_IntReal;
83367  }
83368}
83369#endif
83370
83371
83372/*
83373** This function is called after a transaction has been committed. It
83374** invokes callbacks registered with sqlite3_wal_hook() as required.
83375*/
83376static int doWalCallbacks(sqlite3 *db){
83377  int rc = SQLITE_OK;
83378#ifndef SQLITE_OMIT_WAL
83379  int i;
83380  for(i=0; i<db->nDb; i++){
83381    Btree *pBt = db->aDb[i].pBt;
83382    if( pBt ){
83383      int nEntry;
83384      sqlite3BtreeEnter(pBt);
83385      nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
83386      sqlite3BtreeLeave(pBt);
83387      if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
83388        rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
83389      }
83390    }
83391  }
83392#endif
83393  return rc;
83394}
83395
83396
83397/*
83398** Execute the statement pStmt, either until a row of data is ready, the
83399** statement is completely executed or an error occurs.
83400**
83401** This routine implements the bulk of the logic behind the sqlite_step()
83402** API.  The only thing omitted is the automatic recompile if a
83403** schema change has occurred.  That detail is handled by the
83404** outer sqlite3_step() wrapper procedure.
83405*/
83406static int sqlite3Step(Vdbe *p){
83407  sqlite3 *db;
83408  int rc;
83409
83410  assert(p);
83411  if( p->magic!=VDBE_MAGIC_RUN ){
83412    /* We used to require that sqlite3_reset() be called before retrying
83413    ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
83414    ** with version 3.7.0, we changed this so that sqlite3_reset() would
83415    ** be called automatically instead of throwing the SQLITE_MISUSE error.
83416    ** This "automatic-reset" change is not technically an incompatibility,
83417    ** since any application that receives an SQLITE_MISUSE is broken by
83418    ** definition.
83419    **
83420    ** Nevertheless, some published applications that were originally written
83421    ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
83422    ** returns, and those were broken by the automatic-reset change.  As a
83423    ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
83424    ** legacy behavior of returning SQLITE_MISUSE for cases where the
83425    ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
83426    ** or SQLITE_BUSY error.
83427    */
83428#ifdef SQLITE_OMIT_AUTORESET
83429    if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
83430      sqlite3_reset((sqlite3_stmt*)p);
83431    }else{
83432      return SQLITE_MISUSE_BKPT;
83433    }
83434#else
83435    sqlite3_reset((sqlite3_stmt*)p);
83436#endif
83437  }
83438
83439  /* Check that malloc() has not failed. If it has, return early. */
83440  db = p->db;
83441  if( db->mallocFailed ){
83442    p->rc = SQLITE_NOMEM;
83443    return SQLITE_NOMEM_BKPT;
83444  }
83445
83446  if( p->pc<0 && p->expired ){
83447    p->rc = SQLITE_SCHEMA;
83448    rc = SQLITE_ERROR;
83449    goto end_of_step;
83450  }
83451  if( p->pc<0 ){
83452    /* If there are no other statements currently running, then
83453    ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
83454    ** from interrupting a statement that has not yet started.
83455    */
83456    if( db->nVdbeActive==0 ){
83457      AtomicStore(&db->u1.isInterrupted, 0);
83458    }
83459
83460    assert( db->nVdbeWrite>0 || db->autoCommit==0
83461        || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
83462    );
83463
83464#ifndef SQLITE_OMIT_TRACE
83465    if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
83466        && !db->init.busy && p->zSql ){
83467      sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
83468    }else{
83469      assert( p->startTime==0 );
83470    }
83471#endif
83472
83473    db->nVdbeActive++;
83474    if( p->readOnly==0 ) db->nVdbeWrite++;
83475    if( p->bIsReader ) db->nVdbeRead++;
83476    p->pc = 0;
83477  }
83478#ifdef SQLITE_DEBUG
83479  p->rcApp = SQLITE_OK;
83480#endif
83481#ifndef SQLITE_OMIT_EXPLAIN
83482  if( p->explain ){
83483    rc = sqlite3VdbeList(p);
83484  }else
83485#endif /* SQLITE_OMIT_EXPLAIN */
83486  {
83487    db->nVdbeExec++;
83488    rc = sqlite3VdbeExec(p);
83489    db->nVdbeExec--;
83490  }
83491
83492  if( rc!=SQLITE_ROW ){
83493#ifndef SQLITE_OMIT_TRACE
83494    /* If the statement completed successfully, invoke the profile callback */
83495    checkProfileCallback(db, p);
83496#endif
83497
83498    if( rc==SQLITE_DONE && db->autoCommit ){
83499      assert( p->rc==SQLITE_OK );
83500      p->rc = doWalCallbacks(db);
83501      if( p->rc!=SQLITE_OK ){
83502        rc = SQLITE_ERROR;
83503      }
83504    }
83505  }
83506
83507  db->errCode = rc;
83508  if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
83509    p->rc = SQLITE_NOMEM_BKPT;
83510  }
83511end_of_step:
83512  /* At this point local variable rc holds the value that should be
83513  ** returned if this statement was compiled using the legacy
83514  ** sqlite3_prepare() interface. According to the docs, this can only
83515  ** be one of the values in the first assert() below. Variable p->rc
83516  ** contains the value that would be returned if sqlite3_finalize()
83517  ** were called on statement p.
83518  */
83519  assert( rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR
83520       || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE
83521  );
83522  assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp );
83523  if( rc!=SQLITE_ROW
83524   && rc!=SQLITE_DONE
83525   && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
83526  ){
83527    /* If this statement was prepared using saved SQL and an
83528    ** error has occurred, then return the error code in p->rc to the
83529    ** caller. Set the error code in the database handle to the same value.
83530    */
83531    rc = sqlite3VdbeTransferError(p);
83532  }
83533  return (rc&db->errMask);
83534}
83535
83536/*
83537** This is the top-level implementation of sqlite3_step().  Call
83538** sqlite3Step() to do most of the work.  If a schema error occurs,
83539** call sqlite3Reprepare() and try again.
83540*/
83541SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
83542  int rc = SQLITE_OK;      /* Result from sqlite3Step() */
83543  Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
83544  int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
83545  sqlite3 *db;             /* The database connection */
83546
83547  if( vdbeSafetyNotNull(v) ){
83548    return SQLITE_MISUSE_BKPT;
83549  }
83550  db = v->db;
83551  sqlite3_mutex_enter(db->mutex);
83552  v->doingRerun = 0;
83553  while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
83554         && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
83555    int savedPc = v->pc;
83556    rc = sqlite3Reprepare(v);
83557    if( rc!=SQLITE_OK ){
83558      /* This case occurs after failing to recompile an sql statement.
83559      ** The error message from the SQL compiler has already been loaded
83560      ** into the database handle. This block copies the error message
83561      ** from the database handle into the statement and sets the statement
83562      ** program counter to 0 to ensure that when the statement is
83563      ** finalized or reset the parser error message is available via
83564      ** sqlite3_errmsg() and sqlite3_errcode().
83565      */
83566      const char *zErr = (const char *)sqlite3_value_text(db->pErr);
83567      sqlite3DbFree(db, v->zErrMsg);
83568      if( !db->mallocFailed ){
83569        v->zErrMsg = sqlite3DbStrDup(db, zErr);
83570        v->rc = rc = sqlite3ApiExit(db, rc);
83571      } else {
83572        v->zErrMsg = 0;
83573        v->rc = rc = SQLITE_NOMEM_BKPT;
83574      }
83575      break;
83576    }
83577    sqlite3_reset(pStmt);
83578    if( savedPc>=0 ) v->doingRerun = 1;
83579    assert( v->expired==0 );
83580  }
83581  sqlite3_mutex_leave(db->mutex);
83582  return rc;
83583}
83584
83585
83586/*
83587** Extract the user data from a sqlite3_context structure and return a
83588** pointer to it.
83589*/
83590SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
83591  assert( p && p->pFunc );
83592  return p->pFunc->pUserData;
83593}
83594
83595/*
83596** Extract the user data from a sqlite3_context structure and return a
83597** pointer to it.
83598**
83599** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
83600** returns a copy of the pointer to the database connection (the 1st
83601** parameter) of the sqlite3_create_function() and
83602** sqlite3_create_function16() routines that originally registered the
83603** application defined function.
83604*/
83605SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
83606  assert( p && p->pOut );
83607  return p->pOut->db;
83608}
83609
83610/*
83611** If this routine is invoked from within an xColumn method of a virtual
83612** table, then it returns true if and only if the the call is during an
83613** UPDATE operation and the value of the column will not be modified
83614** by the UPDATE.
83615**
83616** If this routine is called from any context other than within the
83617** xColumn method of a virtual table, then the return value is meaningless
83618** and arbitrary.
83619**
83620** Virtual table implements might use this routine to optimize their
83621** performance by substituting a NULL result, or some other light-weight
83622** value, as a signal to the xUpdate routine that the column is unchanged.
83623*/
83624SQLITE_API int sqlite3_vtab_nochange(sqlite3_context *p){
83625  assert( p );
83626  return sqlite3_value_nochange(p->pOut);
83627}
83628
83629/*
83630** Return the current time for a statement.  If the current time
83631** is requested more than once within the same run of a single prepared
83632** statement, the exact same time is returned for each invocation regardless
83633** of the amount of time that elapses between invocations.  In other words,
83634** the time returned is always the time of the first call.
83635*/
83636SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
83637  int rc;
83638#ifndef SQLITE_ENABLE_STAT4
83639  sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
83640  assert( p->pVdbe!=0 );
83641#else
83642  sqlite3_int64 iTime = 0;
83643  sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
83644#endif
83645  if( *piTime==0 ){
83646    rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
83647    if( rc ) *piTime = 0;
83648  }
83649  return *piTime;
83650}
83651
83652/*
83653** Create a new aggregate context for p and return a pointer to
83654** its pMem->z element.
83655*/
83656static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
83657  Mem *pMem = p->pMem;
83658  assert( (pMem->flags & MEM_Agg)==0 );
83659  if( nByte<=0 ){
83660    sqlite3VdbeMemSetNull(pMem);
83661    pMem->z = 0;
83662  }else{
83663    sqlite3VdbeMemClearAndResize(pMem, nByte);
83664    pMem->flags = MEM_Agg;
83665    pMem->u.pDef = p->pFunc;
83666    if( pMem->z ){
83667      memset(pMem->z, 0, nByte);
83668    }
83669  }
83670  return (void*)pMem->z;
83671}
83672
83673/*
83674** Allocate or return the aggregate context for a user function.  A new
83675** context is allocated on the first call.  Subsequent calls return the
83676** same context that was returned on prior calls.
83677*/
83678SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
83679  assert( p && p->pFunc && p->pFunc->xFinalize );
83680  assert( sqlite3_mutex_held(p->pOut->db->mutex) );
83681  testcase( nByte<0 );
83682  if( (p->pMem->flags & MEM_Agg)==0 ){
83683    return createAggContext(p, nByte);
83684  }else{
83685    return (void*)p->pMem->z;
83686  }
83687}
83688
83689/*
83690** Return the auxiliary data pointer, if any, for the iArg'th argument to
83691** the user-function defined by pCtx.
83692**
83693** The left-most argument is 0.
83694**
83695** Undocumented behavior:  If iArg is negative then access a cache of
83696** auxiliary data pointers that is available to all functions within a
83697** single prepared statement.  The iArg values must match.
83698*/
83699SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
83700  AuxData *pAuxData;
83701
83702  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83703#if SQLITE_ENABLE_STAT4
83704  if( pCtx->pVdbe==0 ) return 0;
83705#else
83706  assert( pCtx->pVdbe!=0 );
83707#endif
83708  for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
83709    if(  pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
83710      return pAuxData->pAux;
83711    }
83712  }
83713  return 0;
83714}
83715
83716/*
83717** Set the auxiliary data pointer and delete function, for the iArg'th
83718** argument to the user-function defined by pCtx. Any previous value is
83719** deleted by calling the delete function specified when it was set.
83720**
83721** The left-most argument is 0.
83722**
83723** Undocumented behavior:  If iArg is negative then make the data available
83724** to all functions within the current prepared statement using iArg as an
83725** access code.
83726*/
83727SQLITE_API void sqlite3_set_auxdata(
83728  sqlite3_context *pCtx,
83729  int iArg,
83730  void *pAux,
83731  void (*xDelete)(void*)
83732){
83733  AuxData *pAuxData;
83734  Vdbe *pVdbe = pCtx->pVdbe;
83735
83736  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83737#ifdef SQLITE_ENABLE_STAT4
83738  if( pVdbe==0 ) goto failed;
83739#else
83740  assert( pVdbe!=0 );
83741#endif
83742
83743  for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
83744    if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
83745      break;
83746    }
83747  }
83748  if( pAuxData==0 ){
83749    pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
83750    if( !pAuxData ) goto failed;
83751    pAuxData->iAuxOp = pCtx->iOp;
83752    pAuxData->iAuxArg = iArg;
83753    pAuxData->pNextAux = pVdbe->pAuxData;
83754    pVdbe->pAuxData = pAuxData;
83755    if( pCtx->isError==0 ) pCtx->isError = -1;
83756  }else if( pAuxData->xDeleteAux ){
83757    pAuxData->xDeleteAux(pAuxData->pAux);
83758  }
83759
83760  pAuxData->pAux = pAux;
83761  pAuxData->xDeleteAux = xDelete;
83762  return;
83763
83764failed:
83765  if( xDelete ){
83766    xDelete(pAux);
83767  }
83768}
83769
83770#ifndef SQLITE_OMIT_DEPRECATED
83771/*
83772** Return the number of times the Step function of an aggregate has been
83773** called.
83774**
83775** This function is deprecated.  Do not use it for new code.  It is
83776** provide only to avoid breaking legacy code.  New aggregate function
83777** implementations should keep their own counts within their aggregate
83778** context.
83779*/
83780SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
83781  assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
83782  return p->pMem->n;
83783}
83784#endif
83785
83786/*
83787** Return the number of columns in the result set for the statement pStmt.
83788*/
83789SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
83790  Vdbe *pVm = (Vdbe *)pStmt;
83791  return pVm ? pVm->nResColumn : 0;
83792}
83793
83794/*
83795** Return the number of values available from the current row of the
83796** currently executing statement pStmt.
83797*/
83798SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
83799  Vdbe *pVm = (Vdbe *)pStmt;
83800  if( pVm==0 || pVm->pResultSet==0 ) return 0;
83801  return pVm->nResColumn;
83802}
83803
83804/*
83805** Return a pointer to static memory containing an SQL NULL value.
83806*/
83807static const Mem *columnNullValue(void){
83808  /* Even though the Mem structure contains an element
83809  ** of type i64, on certain architectures (x86) with certain compiler
83810  ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
83811  ** instead of an 8-byte one. This all works fine, except that when
83812  ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
83813  ** that a Mem structure is located on an 8-byte boundary. To prevent
83814  ** these assert()s from failing, when building with SQLITE_DEBUG defined
83815  ** using gcc, we force nullMem to be 8-byte aligned using the magical
83816  ** __attribute__((aligned(8))) macro.  */
83817  static const Mem nullMem
83818#if defined(SQLITE_DEBUG) && defined(__GNUC__)
83819    __attribute__((aligned(8)))
83820#endif
83821    = {
83822        /* .u          = */ {0},
83823        /* .flags      = */ (u16)MEM_Null,
83824        /* .enc        = */ (u8)0,
83825        /* .eSubtype   = */ (u8)0,
83826        /* .n          = */ (int)0,
83827        /* .z          = */ (char*)0,
83828        /* .zMalloc    = */ (char*)0,
83829        /* .szMalloc   = */ (int)0,
83830        /* .uTemp      = */ (u32)0,
83831        /* .db         = */ (sqlite3*)0,
83832        /* .xDel       = */ (void(*)(void*))0,
83833#ifdef SQLITE_DEBUG
83834        /* .pScopyFrom = */ (Mem*)0,
83835        /* .mScopyFlags= */ 0,
83836#endif
83837      };
83838  return &nullMem;
83839}
83840
83841/*
83842** Check to see if column iCol of the given statement is valid.  If
83843** it is, return a pointer to the Mem for the value of that column.
83844** If iCol is not valid, return a pointer to a Mem which has a value
83845** of NULL.
83846*/
83847static Mem *columnMem(sqlite3_stmt *pStmt, int i){
83848  Vdbe *pVm;
83849  Mem *pOut;
83850
83851  pVm = (Vdbe *)pStmt;
83852  if( pVm==0 ) return (Mem*)columnNullValue();
83853  assert( pVm->db );
83854  sqlite3_mutex_enter(pVm->db->mutex);
83855  if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
83856    pOut = &pVm->pResultSet[i];
83857  }else{
83858    sqlite3Error(pVm->db, SQLITE_RANGE);
83859    pOut = (Mem*)columnNullValue();
83860  }
83861  return pOut;
83862}
83863
83864/*
83865** This function is called after invoking an sqlite3_value_XXX function on a
83866** column value (i.e. a value returned by evaluating an SQL expression in the
83867** select list of a SELECT statement) that may cause a malloc() failure. If
83868** malloc() has failed, the threads mallocFailed flag is cleared and the result
83869** code of statement pStmt set to SQLITE_NOMEM.
83870**
83871** Specifically, this is called from within:
83872**
83873**     sqlite3_column_int()
83874**     sqlite3_column_int64()
83875**     sqlite3_column_text()
83876**     sqlite3_column_text16()
83877**     sqlite3_column_real()
83878**     sqlite3_column_bytes()
83879**     sqlite3_column_bytes16()
83880**     sqiite3_column_blob()
83881*/
83882static void columnMallocFailure(sqlite3_stmt *pStmt)
83883{
83884  /* If malloc() failed during an encoding conversion within an
83885  ** sqlite3_column_XXX API, then set the return code of the statement to
83886  ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
83887  ** and _finalize() will return NOMEM.
83888  */
83889  Vdbe *p = (Vdbe *)pStmt;
83890  if( p ){
83891    assert( p->db!=0 );
83892    assert( sqlite3_mutex_held(p->db->mutex) );
83893    p->rc = sqlite3ApiExit(p->db, p->rc);
83894    sqlite3_mutex_leave(p->db->mutex);
83895  }
83896}
83897
83898/**************************** sqlite3_column_  *******************************
83899** The following routines are used to access elements of the current row
83900** in the result set.
83901*/
83902SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
83903  const void *val;
83904  val = sqlite3_value_blob( columnMem(pStmt,i) );
83905  /* Even though there is no encoding conversion, value_blob() might
83906  ** need to call malloc() to expand the result of a zeroblob()
83907  ** expression.
83908  */
83909  columnMallocFailure(pStmt);
83910  return val;
83911}
83912SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
83913  int val = sqlite3_value_bytes( columnMem(pStmt,i) );
83914  columnMallocFailure(pStmt);
83915  return val;
83916}
83917SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
83918  int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
83919  columnMallocFailure(pStmt);
83920  return val;
83921}
83922SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
83923  double val = sqlite3_value_double( columnMem(pStmt,i) );
83924  columnMallocFailure(pStmt);
83925  return val;
83926}
83927SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
83928  int val = sqlite3_value_int( columnMem(pStmt,i) );
83929  columnMallocFailure(pStmt);
83930  return val;
83931}
83932SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
83933  sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
83934  columnMallocFailure(pStmt);
83935  return val;
83936}
83937SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
83938  const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
83939  columnMallocFailure(pStmt);
83940  return val;
83941}
83942SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
83943  Mem *pOut = columnMem(pStmt, i);
83944  if( pOut->flags&MEM_Static ){
83945    pOut->flags &= ~MEM_Static;
83946    pOut->flags |= MEM_Ephem;
83947  }
83948  columnMallocFailure(pStmt);
83949  return (sqlite3_value *)pOut;
83950}
83951#ifndef SQLITE_OMIT_UTF16
83952SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
83953  const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
83954  columnMallocFailure(pStmt);
83955  return val;
83956}
83957#endif /* SQLITE_OMIT_UTF16 */
83958SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
83959  int iType = sqlite3_value_type( columnMem(pStmt,i) );
83960  columnMallocFailure(pStmt);
83961  return iType;
83962}
83963
83964/*
83965** Convert the N-th element of pStmt->pColName[] into a string using
83966** xFunc() then return that string.  If N is out of range, return 0.
83967**
83968** There are up to 5 names for each column.  useType determines which
83969** name is returned.  Here are the names:
83970**
83971**    0      The column name as it should be displayed for output
83972**    1      The datatype name for the column
83973**    2      The name of the database that the column derives from
83974**    3      The name of the table that the column derives from
83975**    4      The name of the table column that the result column derives from
83976**
83977** If the result is not a simple column reference (if it is an expression
83978** or a constant) then useTypes 2, 3, and 4 return NULL.
83979*/
83980static const void *columnName(
83981  sqlite3_stmt *pStmt,     /* The statement */
83982  int N,                   /* Which column to get the name for */
83983  int useUtf16,            /* True to return the name as UTF16 */
83984  int useType              /* What type of name */
83985){
83986  const void *ret;
83987  Vdbe *p;
83988  int n;
83989  sqlite3 *db;
83990#ifdef SQLITE_ENABLE_API_ARMOR
83991  if( pStmt==0 ){
83992    (void)SQLITE_MISUSE_BKPT;
83993    return 0;
83994  }
83995#endif
83996  ret = 0;
83997  p = (Vdbe *)pStmt;
83998  db = p->db;
83999  assert( db!=0 );
84000  n = sqlite3_column_count(pStmt);
84001  if( N<n && N>=0 ){
84002    N += useType*n;
84003    sqlite3_mutex_enter(db->mutex);
84004    assert( db->mallocFailed==0 );
84005#ifndef SQLITE_OMIT_UTF16
84006    if( useUtf16 ){
84007      ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
84008    }else
84009#endif
84010    {
84011      ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
84012    }
84013    /* A malloc may have failed inside of the _text() call. If this
84014    ** is the case, clear the mallocFailed flag and return NULL.
84015    */
84016    if( db->mallocFailed ){
84017      sqlite3OomClear(db);
84018      ret = 0;
84019    }
84020    sqlite3_mutex_leave(db->mutex);
84021  }
84022  return ret;
84023}
84024
84025/*
84026** Return the name of the Nth column of the result set returned by SQL
84027** statement pStmt.
84028*/
84029SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
84030  return columnName(pStmt, N, 0, COLNAME_NAME);
84031}
84032#ifndef SQLITE_OMIT_UTF16
84033SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
84034  return columnName(pStmt, N, 1, COLNAME_NAME);
84035}
84036#endif
84037
84038/*
84039** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
84040** not define OMIT_DECLTYPE.
84041*/
84042#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
84043# error "Must not define both SQLITE_OMIT_DECLTYPE \
84044         and SQLITE_ENABLE_COLUMN_METADATA"
84045#endif
84046
84047#ifndef SQLITE_OMIT_DECLTYPE
84048/*
84049** Return the column declaration type (if applicable) of the 'i'th column
84050** of the result set of SQL statement pStmt.
84051*/
84052SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
84053  return columnName(pStmt, N, 0, COLNAME_DECLTYPE);
84054}
84055#ifndef SQLITE_OMIT_UTF16
84056SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
84057  return columnName(pStmt, N, 1, COLNAME_DECLTYPE);
84058}
84059#endif /* SQLITE_OMIT_UTF16 */
84060#endif /* SQLITE_OMIT_DECLTYPE */
84061
84062#ifdef SQLITE_ENABLE_COLUMN_METADATA
84063/*
84064** Return the name of the database from which a result column derives.
84065** NULL is returned if the result column is an expression or constant or
84066** anything else which is not an unambiguous reference to a database column.
84067*/
84068SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
84069  return columnName(pStmt, N, 0, COLNAME_DATABASE);
84070}
84071#ifndef SQLITE_OMIT_UTF16
84072SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
84073  return columnName(pStmt, N, 1, COLNAME_DATABASE);
84074}
84075#endif /* SQLITE_OMIT_UTF16 */
84076
84077/*
84078** Return the name of the table from which a result column derives.
84079** NULL is returned if the result column is an expression or constant or
84080** anything else which is not an unambiguous reference to a database column.
84081*/
84082SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
84083  return columnName(pStmt, N, 0, COLNAME_TABLE);
84084}
84085#ifndef SQLITE_OMIT_UTF16
84086SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
84087  return columnName(pStmt, N, 1, COLNAME_TABLE);
84088}
84089#endif /* SQLITE_OMIT_UTF16 */
84090
84091/*
84092** Return the name of the table column from which a result column derives.
84093** NULL is returned if the result column is an expression or constant or
84094** anything else which is not an unambiguous reference to a database column.
84095*/
84096SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
84097  return columnName(pStmt, N, 0, COLNAME_COLUMN);
84098}
84099#ifndef SQLITE_OMIT_UTF16
84100SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
84101  return columnName(pStmt, N, 1, COLNAME_COLUMN);
84102}
84103#endif /* SQLITE_OMIT_UTF16 */
84104#endif /* SQLITE_ENABLE_COLUMN_METADATA */
84105
84106
84107/******************************* sqlite3_bind_  ***************************
84108**
84109** Routines used to attach values to wildcards in a compiled SQL statement.
84110*/
84111/*
84112** Unbind the value bound to variable i in virtual machine p. This is the
84113** the same as binding a NULL value to the column. If the "i" parameter is
84114** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
84115**
84116** A successful evaluation of this routine acquires the mutex on p.
84117** the mutex is released if any kind of error occurs.
84118**
84119** The error code stored in database p->db is overwritten with the return
84120** value in any case.
84121*/
84122static int vdbeUnbind(Vdbe *p, int i){
84123  Mem *pVar;
84124  if( vdbeSafetyNotNull(p) ){
84125    return SQLITE_MISUSE_BKPT;
84126  }
84127  sqlite3_mutex_enter(p->db->mutex);
84128  if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
84129    sqlite3Error(p->db, SQLITE_MISUSE);
84130    sqlite3_mutex_leave(p->db->mutex);
84131    sqlite3_log(SQLITE_MISUSE,
84132        "bind on a busy prepared statement: [%s]", p->zSql);
84133    return SQLITE_MISUSE_BKPT;
84134  }
84135  if( i<1 || i>p->nVar ){
84136    sqlite3Error(p->db, SQLITE_RANGE);
84137    sqlite3_mutex_leave(p->db->mutex);
84138    return SQLITE_RANGE;
84139  }
84140  i--;
84141  pVar = &p->aVar[i];
84142  sqlite3VdbeMemRelease(pVar);
84143  pVar->flags = MEM_Null;
84144  p->db->errCode = SQLITE_OK;
84145
84146  /* If the bit corresponding to this variable in Vdbe.expmask is set, then
84147  ** binding a new value to this variable invalidates the current query plan.
84148  **
84149  ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
84150  ** parameter in the WHERE clause might influence the choice of query plan
84151  ** for a statement, then the statement will be automatically recompiled,
84152  ** as if there had been a schema change, on the first sqlite3_step() call
84153  ** following any change to the bindings of that parameter.
84154  */
84155  assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
84156  if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
84157    p->expired = 1;
84158  }
84159  return SQLITE_OK;
84160}
84161
84162/*
84163** Bind a text or BLOB value.
84164*/
84165static int bindText(
84166  sqlite3_stmt *pStmt,   /* The statement to bind against */
84167  int i,                 /* Index of the parameter to bind */
84168  const void *zData,     /* Pointer to the data to be bound */
84169  int nData,             /* Number of bytes of data to be bound */
84170  void (*xDel)(void*),   /* Destructor for the data */
84171  u8 encoding            /* Encoding for the data */
84172){
84173  Vdbe *p = (Vdbe *)pStmt;
84174  Mem *pVar;
84175  int rc;
84176
84177  rc = vdbeUnbind(p, i);
84178  if( rc==SQLITE_OK ){
84179    if( zData!=0 ){
84180      pVar = &p->aVar[i-1];
84181      rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
84182      if( rc==SQLITE_OK && encoding!=0 ){
84183        rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
84184      }
84185      if( rc ){
84186        sqlite3Error(p->db, rc);
84187        rc = sqlite3ApiExit(p->db, rc);
84188      }
84189    }
84190    sqlite3_mutex_leave(p->db->mutex);
84191  }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
84192    xDel((void*)zData);
84193  }
84194  return rc;
84195}
84196
84197
84198/*
84199** Bind a blob value to an SQL statement variable.
84200*/
84201SQLITE_API int sqlite3_bind_blob(
84202  sqlite3_stmt *pStmt,
84203  int i,
84204  const void *zData,
84205  int nData,
84206  void (*xDel)(void*)
84207){
84208#ifdef SQLITE_ENABLE_API_ARMOR
84209  if( nData<0 ) return SQLITE_MISUSE_BKPT;
84210#endif
84211  return bindText(pStmt, i, zData, nData, xDel, 0);
84212}
84213SQLITE_API int sqlite3_bind_blob64(
84214  sqlite3_stmt *pStmt,
84215  int i,
84216  const void *zData,
84217  sqlite3_uint64 nData,
84218  void (*xDel)(void*)
84219){
84220  assert( xDel!=SQLITE_DYNAMIC );
84221  if( nData>0x7fffffff ){
84222    return invokeValueDestructor(zData, xDel, 0);
84223  }else{
84224    return bindText(pStmt, i, zData, (int)nData, xDel, 0);
84225  }
84226}
84227SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
84228  int rc;
84229  Vdbe *p = (Vdbe *)pStmt;
84230  rc = vdbeUnbind(p, i);
84231  if( rc==SQLITE_OK ){
84232    sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
84233    sqlite3_mutex_leave(p->db->mutex);
84234  }
84235  return rc;
84236}
84237SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
84238  return sqlite3_bind_int64(p, i, (i64)iValue);
84239}
84240SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
84241  int rc;
84242  Vdbe *p = (Vdbe *)pStmt;
84243  rc = vdbeUnbind(p, i);
84244  if( rc==SQLITE_OK ){
84245    sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
84246    sqlite3_mutex_leave(p->db->mutex);
84247  }
84248  return rc;
84249}
84250SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
84251  int rc;
84252  Vdbe *p = (Vdbe*)pStmt;
84253  rc = vdbeUnbind(p, i);
84254  if( rc==SQLITE_OK ){
84255    sqlite3_mutex_leave(p->db->mutex);
84256  }
84257  return rc;
84258}
84259SQLITE_API int sqlite3_bind_pointer(
84260  sqlite3_stmt *pStmt,
84261  int i,
84262  void *pPtr,
84263  const char *zPTtype,
84264  void (*xDestructor)(void*)
84265){
84266  int rc;
84267  Vdbe *p = (Vdbe*)pStmt;
84268  rc = vdbeUnbind(p, i);
84269  if( rc==SQLITE_OK ){
84270    sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
84271    sqlite3_mutex_leave(p->db->mutex);
84272  }else if( xDestructor ){
84273    xDestructor(pPtr);
84274  }
84275  return rc;
84276}
84277SQLITE_API int sqlite3_bind_text(
84278  sqlite3_stmt *pStmt,
84279  int i,
84280  const char *zData,
84281  int nData,
84282  void (*xDel)(void*)
84283){
84284  return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
84285}
84286SQLITE_API int sqlite3_bind_text64(
84287  sqlite3_stmt *pStmt,
84288  int i,
84289  const char *zData,
84290  sqlite3_uint64 nData,
84291  void (*xDel)(void*),
84292  unsigned char enc
84293){
84294  assert( xDel!=SQLITE_DYNAMIC );
84295  if( nData>0x7fffffff ){
84296    return invokeValueDestructor(zData, xDel, 0);
84297  }else{
84298    if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
84299    return bindText(pStmt, i, zData, (int)nData, xDel, enc);
84300  }
84301}
84302#ifndef SQLITE_OMIT_UTF16
84303SQLITE_API int sqlite3_bind_text16(
84304  sqlite3_stmt *pStmt,
84305  int i,
84306  const void *zData,
84307  int nData,
84308  void (*xDel)(void*)
84309){
84310  return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
84311}
84312#endif /* SQLITE_OMIT_UTF16 */
84313SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
84314  int rc;
84315  switch( sqlite3_value_type((sqlite3_value*)pValue) ){
84316    case SQLITE_INTEGER: {
84317      rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
84318      break;
84319    }
84320    case SQLITE_FLOAT: {
84321      rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
84322      break;
84323    }
84324    case SQLITE_BLOB: {
84325      if( pValue->flags & MEM_Zero ){
84326        rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
84327      }else{
84328        rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
84329      }
84330      break;
84331    }
84332    case SQLITE_TEXT: {
84333      rc = bindText(pStmt,i,  pValue->z, pValue->n, SQLITE_TRANSIENT,
84334                              pValue->enc);
84335      break;
84336    }
84337    default: {
84338      rc = sqlite3_bind_null(pStmt, i);
84339      break;
84340    }
84341  }
84342  return rc;
84343}
84344SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
84345  int rc;
84346  Vdbe *p = (Vdbe *)pStmt;
84347  rc = vdbeUnbind(p, i);
84348  if( rc==SQLITE_OK ){
84349    sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
84350    sqlite3_mutex_leave(p->db->mutex);
84351  }
84352  return rc;
84353}
84354SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
84355  int rc;
84356  Vdbe *p = (Vdbe *)pStmt;
84357  sqlite3_mutex_enter(p->db->mutex);
84358  if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
84359    rc = SQLITE_TOOBIG;
84360  }else{
84361    assert( (n & 0x7FFFFFFF)==n );
84362    rc = sqlite3_bind_zeroblob(pStmt, i, n);
84363  }
84364  rc = sqlite3ApiExit(p->db, rc);
84365  sqlite3_mutex_leave(p->db->mutex);
84366  return rc;
84367}
84368
84369/*
84370** Return the number of wildcards that can be potentially bound to.
84371** This routine is added to support DBD::SQLite.
84372*/
84373SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
84374  Vdbe *p = (Vdbe*)pStmt;
84375  return p ? p->nVar : 0;
84376}
84377
84378/*
84379** Return the name of a wildcard parameter.  Return NULL if the index
84380** is out of range or if the wildcard is unnamed.
84381**
84382** The result is always UTF-8.
84383*/
84384SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
84385  Vdbe *p = (Vdbe*)pStmt;
84386  if( p==0 ) return 0;
84387  return sqlite3VListNumToName(p->pVList, i);
84388}
84389
84390/*
84391** Given a wildcard parameter name, return the index of the variable
84392** with that name.  If there is no variable with the given name,
84393** return 0.
84394*/
84395SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
84396  if( p==0 || zName==0 ) return 0;
84397  return sqlite3VListNameToNum(p->pVList, zName, nName);
84398}
84399SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
84400  return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
84401}
84402
84403/*
84404** Transfer all bindings from the first statement over to the second.
84405*/
84406SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
84407  Vdbe *pFrom = (Vdbe*)pFromStmt;
84408  Vdbe *pTo = (Vdbe*)pToStmt;
84409  int i;
84410  assert( pTo->db==pFrom->db );
84411  assert( pTo->nVar==pFrom->nVar );
84412  sqlite3_mutex_enter(pTo->db->mutex);
84413  for(i=0; i<pFrom->nVar; i++){
84414    sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
84415  }
84416  sqlite3_mutex_leave(pTo->db->mutex);
84417  return SQLITE_OK;
84418}
84419
84420#ifndef SQLITE_OMIT_DEPRECATED
84421/*
84422** Deprecated external interface.  Internal/core SQLite code
84423** should call sqlite3TransferBindings.
84424**
84425** It is misuse to call this routine with statements from different
84426** database connections.  But as this is a deprecated interface, we
84427** will not bother to check for that condition.
84428**
84429** If the two statements contain a different number of bindings, then
84430** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
84431** SQLITE_OK is returned.
84432*/
84433SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
84434  Vdbe *pFrom = (Vdbe*)pFromStmt;
84435  Vdbe *pTo = (Vdbe*)pToStmt;
84436  if( pFrom->nVar!=pTo->nVar ){
84437    return SQLITE_ERROR;
84438  }
84439  assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
84440  if( pTo->expmask ){
84441    pTo->expired = 1;
84442  }
84443  assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
84444  if( pFrom->expmask ){
84445    pFrom->expired = 1;
84446  }
84447  return sqlite3TransferBindings(pFromStmt, pToStmt);
84448}
84449#endif
84450
84451/*
84452** Return the sqlite3* database handle to which the prepared statement given
84453** in the argument belongs.  This is the same database handle that was
84454** the first argument to the sqlite3_prepare() that was used to create
84455** the statement in the first place.
84456*/
84457SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
84458  return pStmt ? ((Vdbe*)pStmt)->db : 0;
84459}
84460
84461/*
84462** Return true if the prepared statement is guaranteed to not modify the
84463** database.
84464*/
84465SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
84466  return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
84467}
84468
84469/*
84470** Return 1 if the statement is an EXPLAIN and return 2 if the
84471** statement is an EXPLAIN QUERY PLAN
84472*/
84473SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){
84474  return pStmt ? ((Vdbe*)pStmt)->explain : 0;
84475}
84476
84477/*
84478** Return true if the prepared statement is in need of being reset.
84479*/
84480SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
84481  Vdbe *v = (Vdbe*)pStmt;
84482  return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
84483}
84484
84485/*
84486** Return a pointer to the next prepared statement after pStmt associated
84487** with database connection pDb.  If pStmt is NULL, return the first
84488** prepared statement for the database connection.  Return NULL if there
84489** are no more.
84490*/
84491SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
84492  sqlite3_stmt *pNext;
84493#ifdef SQLITE_ENABLE_API_ARMOR
84494  if( !sqlite3SafetyCheckOk(pDb) ){
84495    (void)SQLITE_MISUSE_BKPT;
84496    return 0;
84497  }
84498#endif
84499  sqlite3_mutex_enter(pDb->mutex);
84500  if( pStmt==0 ){
84501    pNext = (sqlite3_stmt*)pDb->pVdbe;
84502  }else{
84503    pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
84504  }
84505  sqlite3_mutex_leave(pDb->mutex);
84506  return pNext;
84507}
84508
84509/*
84510** Return the value of a status counter for a prepared statement
84511*/
84512SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
84513  Vdbe *pVdbe = (Vdbe*)pStmt;
84514  u32 v;
84515#ifdef SQLITE_ENABLE_API_ARMOR
84516  if( !pStmt
84517   || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
84518  ){
84519    (void)SQLITE_MISUSE_BKPT;
84520    return 0;
84521  }
84522#endif
84523  if( op==SQLITE_STMTSTATUS_MEMUSED ){
84524    sqlite3 *db = pVdbe->db;
84525    sqlite3_mutex_enter(db->mutex);
84526    v = 0;
84527    db->pnBytesFreed = (int*)&v;
84528    sqlite3VdbeClearObject(db, pVdbe);
84529    sqlite3DbFree(db, pVdbe);
84530    db->pnBytesFreed = 0;
84531    sqlite3_mutex_leave(db->mutex);
84532  }else{
84533    v = pVdbe->aCounter[op];
84534    if( resetFlag ) pVdbe->aCounter[op] = 0;
84535  }
84536  return (int)v;
84537}
84538
84539/*
84540** Return the SQL associated with a prepared statement
84541*/
84542SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
84543  Vdbe *p = (Vdbe *)pStmt;
84544  return p ? p->zSql : 0;
84545}
84546
84547/*
84548** Return the SQL associated with a prepared statement with
84549** bound parameters expanded.  Space to hold the returned string is
84550** obtained from sqlite3_malloc().  The caller is responsible for
84551** freeing the returned string by passing it to sqlite3_free().
84552**
84553** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
84554** expanded bound parameters.
84555*/
84556SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
84557#ifdef SQLITE_OMIT_TRACE
84558  return 0;
84559#else
84560  char *z = 0;
84561  const char *zSql = sqlite3_sql(pStmt);
84562  if( zSql ){
84563    Vdbe *p = (Vdbe *)pStmt;
84564    sqlite3_mutex_enter(p->db->mutex);
84565    z = sqlite3VdbeExpandSql(p, zSql);
84566    sqlite3_mutex_leave(p->db->mutex);
84567  }
84568  return z;
84569#endif
84570}
84571
84572#ifdef SQLITE_ENABLE_NORMALIZE
84573/*
84574** Return the normalized SQL associated with a prepared statement.
84575*/
84576SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){
84577  Vdbe *p = (Vdbe *)pStmt;
84578  if( p==0 ) return 0;
84579  if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
84580    sqlite3_mutex_enter(p->db->mutex);
84581    p->zNormSql = sqlite3Normalize(p, p->zSql);
84582    sqlite3_mutex_leave(p->db->mutex);
84583  }
84584  return p->zNormSql;
84585}
84586#endif /* SQLITE_ENABLE_NORMALIZE */
84587
84588#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
84589/*
84590** Allocate and populate an UnpackedRecord structure based on the serialized
84591** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
84592** if successful, or a NULL pointer if an OOM error is encountered.
84593*/
84594static UnpackedRecord *vdbeUnpackRecord(
84595  KeyInfo *pKeyInfo,
84596  int nKey,
84597  const void *pKey
84598){
84599  UnpackedRecord *pRet;           /* Return value */
84600
84601  pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
84602  if( pRet ){
84603    memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
84604    sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
84605  }
84606  return pRet;
84607}
84608
84609/*
84610** This function is called from within a pre-update callback to retrieve
84611** a field of the row currently being updated or deleted.
84612*/
84613SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
84614  PreUpdate *p = db->pPreUpdate;
84615  Mem *pMem;
84616  int rc = SQLITE_OK;
84617
84618  /* Test that this call is being made from within an SQLITE_DELETE or
84619  ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
84620  if( !p || p->op==SQLITE_INSERT ){
84621    rc = SQLITE_MISUSE_BKPT;
84622    goto preupdate_old_out;
84623  }
84624  if( p->pPk ){
84625    iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
84626  }
84627  if( iIdx>=p->pCsr->nField || iIdx<0 ){
84628    rc = SQLITE_RANGE;
84629    goto preupdate_old_out;
84630  }
84631
84632  /* If the old.* record has not yet been loaded into memory, do so now. */
84633  if( p->pUnpacked==0 ){
84634    u32 nRec;
84635    u8 *aRec;
84636
84637    nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
84638    aRec = sqlite3DbMallocRaw(db, nRec);
84639    if( !aRec ) goto preupdate_old_out;
84640    rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
84641    if( rc==SQLITE_OK ){
84642      p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
84643      if( !p->pUnpacked ) rc = SQLITE_NOMEM;
84644    }
84645    if( rc!=SQLITE_OK ){
84646      sqlite3DbFree(db, aRec);
84647      goto preupdate_old_out;
84648    }
84649    p->aRecord = aRec;
84650  }
84651
84652  pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
84653  if( iIdx==p->pTab->iPKey ){
84654    sqlite3VdbeMemSetInt64(pMem, p->iKey1);
84655  }else if( iIdx>=p->pUnpacked->nField ){
84656    *ppValue = (sqlite3_value *)columnNullValue();
84657  }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
84658    if( pMem->flags & (MEM_Int|MEM_IntReal) ){
84659      testcase( pMem->flags & MEM_Int );
84660      testcase( pMem->flags & MEM_IntReal );
84661      sqlite3VdbeMemRealify(pMem);
84662    }
84663  }
84664
84665 preupdate_old_out:
84666  sqlite3Error(db, rc);
84667  return sqlite3ApiExit(db, rc);
84668}
84669#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
84670
84671#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
84672/*
84673** This function is called from within a pre-update callback to retrieve
84674** the number of columns in the row being updated, deleted or inserted.
84675*/
84676SQLITE_API int sqlite3_preupdate_count(sqlite3 *db){
84677  PreUpdate *p = db->pPreUpdate;
84678  return (p ? p->keyinfo.nKeyField : 0);
84679}
84680#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
84681
84682#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
84683/*
84684** This function is designed to be called from within a pre-update callback
84685** only. It returns zero if the change that caused the callback was made
84686** immediately by a user SQL statement. Or, if the change was made by a
84687** trigger program, it returns the number of trigger programs currently
84688** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
84689** top-level trigger etc.).
84690**
84691** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
84692** or SET DEFAULT action is considered a trigger.
84693*/
84694SQLITE_API int sqlite3_preupdate_depth(sqlite3 *db){
84695  PreUpdate *p = db->pPreUpdate;
84696  return (p ? p->v->nFrame : 0);
84697}
84698#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
84699
84700#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
84701/*
84702** This function is called from within a pre-update callback to retrieve
84703** a field of the row currently being updated or inserted.
84704*/
84705SQLITE_API int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
84706  PreUpdate *p = db->pPreUpdate;
84707  int rc = SQLITE_OK;
84708  Mem *pMem;
84709
84710  if( !p || p->op==SQLITE_DELETE ){
84711    rc = SQLITE_MISUSE_BKPT;
84712    goto preupdate_new_out;
84713  }
84714  if( p->pPk && p->op!=SQLITE_UPDATE ){
84715    iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
84716  }
84717  if( iIdx>=p->pCsr->nField || iIdx<0 ){
84718    rc = SQLITE_RANGE;
84719    goto preupdate_new_out;
84720  }
84721
84722  if( p->op==SQLITE_INSERT ){
84723    /* For an INSERT, memory cell p->iNewReg contains the serialized record
84724    ** that is being inserted. Deserialize it. */
84725    UnpackedRecord *pUnpack = p->pNewUnpacked;
84726    if( !pUnpack ){
84727      Mem *pData = &p->v->aMem[p->iNewReg];
84728      rc = ExpandBlob(pData);
84729      if( rc!=SQLITE_OK ) goto preupdate_new_out;
84730      pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
84731      if( !pUnpack ){
84732        rc = SQLITE_NOMEM;
84733        goto preupdate_new_out;
84734      }
84735      p->pNewUnpacked = pUnpack;
84736    }
84737    pMem = &pUnpack->aMem[iIdx];
84738    if( iIdx==p->pTab->iPKey ){
84739      sqlite3VdbeMemSetInt64(pMem, p->iKey2);
84740    }else if( iIdx>=pUnpack->nField ){
84741      pMem = (sqlite3_value *)columnNullValue();
84742    }
84743  }else{
84744    /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
84745    ** value. Make a copy of the cell contents and return a pointer to it.
84746    ** It is not safe to return a pointer to the memory cell itself as the
84747    ** caller may modify the value text encoding.
84748    */
84749    assert( p->op==SQLITE_UPDATE );
84750    if( !p->aNew ){
84751      p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
84752      if( !p->aNew ){
84753        rc = SQLITE_NOMEM;
84754        goto preupdate_new_out;
84755      }
84756    }
84757    assert( iIdx>=0 && iIdx<p->pCsr->nField );
84758    pMem = &p->aNew[iIdx];
84759    if( pMem->flags==0 ){
84760      if( iIdx==p->pTab->iPKey ){
84761        sqlite3VdbeMemSetInt64(pMem, p->iKey2);
84762      }else{
84763        rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
84764        if( rc!=SQLITE_OK ) goto preupdate_new_out;
84765      }
84766    }
84767  }
84768  *ppValue = pMem;
84769
84770 preupdate_new_out:
84771  sqlite3Error(db, rc);
84772  return sqlite3ApiExit(db, rc);
84773}
84774#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
84775
84776#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
84777/*
84778** Return status data for a single loop within query pStmt.
84779*/
84780SQLITE_API int sqlite3_stmt_scanstatus(
84781  sqlite3_stmt *pStmt,            /* Prepared statement being queried */
84782  int idx,                        /* Index of loop to report on */
84783  int iScanStatusOp,              /* Which metric to return */
84784  void *pOut                      /* OUT: Write the answer here */
84785){
84786  Vdbe *p = (Vdbe*)pStmt;
84787  ScanStatus *pScan;
84788  if( idx<0 || idx>=p->nScan ) return 1;
84789  pScan = &p->aScan[idx];
84790  switch( iScanStatusOp ){
84791    case SQLITE_SCANSTAT_NLOOP: {
84792      *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
84793      break;
84794    }
84795    case SQLITE_SCANSTAT_NVISIT: {
84796      *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
84797      break;
84798    }
84799    case SQLITE_SCANSTAT_EST: {
84800      double r = 1.0;
84801      LogEst x = pScan->nEst;
84802      while( x<100 ){
84803        x += 10;
84804        r *= 0.5;
84805      }
84806      *(double*)pOut = r*sqlite3LogEstToInt(x);
84807      break;
84808    }
84809    case SQLITE_SCANSTAT_NAME: {
84810      *(const char**)pOut = pScan->zName;
84811      break;
84812    }
84813    case SQLITE_SCANSTAT_EXPLAIN: {
84814      if( pScan->addrExplain ){
84815        *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
84816      }else{
84817        *(const char**)pOut = 0;
84818      }
84819      break;
84820    }
84821    case SQLITE_SCANSTAT_SELECTID: {
84822      if( pScan->addrExplain ){
84823        *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
84824      }else{
84825        *(int*)pOut = -1;
84826      }
84827      break;
84828    }
84829    default: {
84830      return 1;
84831    }
84832  }
84833  return 0;
84834}
84835
84836/*
84837** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
84838*/
84839SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
84840  Vdbe *p = (Vdbe*)pStmt;
84841  memset(p->anExec, 0, p->nOp * sizeof(i64));
84842}
84843#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
84844
84845/************** End of vdbeapi.c *********************************************/
84846/************** Begin file vdbetrace.c ***************************************/
84847/*
84848** 2009 November 25
84849**
84850** The author disclaims copyright to this source code.  In place of
84851** a legal notice, here is a blessing:
84852**
84853**    May you do good and not evil.
84854**    May you find forgiveness for yourself and forgive others.
84855**    May you share freely, never taking more than you give.
84856**
84857*************************************************************************
84858**
84859** This file contains code used to insert the values of host parameters
84860** (aka "wildcards") into the SQL text output by sqlite3_trace().
84861**
84862** The Vdbe parse-tree explainer is also found here.
84863*/
84864/* #include "sqliteInt.h" */
84865/* #include "vdbeInt.h" */
84866
84867#ifndef SQLITE_OMIT_TRACE
84868
84869/*
84870** zSql is a zero-terminated string of UTF-8 SQL text.  Return the number of
84871** bytes in this text up to but excluding the first character in
84872** a host parameter.  If the text contains no host parameters, return
84873** the total number of bytes in the text.
84874*/
84875static int findNextHostParameter(const char *zSql, int *pnToken){
84876  int tokenType;
84877  int nTotal = 0;
84878  int n;
84879
84880  *pnToken = 0;
84881  while( zSql[0] ){
84882    n = sqlite3GetToken((u8*)zSql, &tokenType);
84883    assert( n>0 && tokenType!=TK_ILLEGAL );
84884    if( tokenType==TK_VARIABLE ){
84885      *pnToken = n;
84886      break;
84887    }
84888    nTotal += n;
84889    zSql += n;
84890  }
84891  return nTotal;
84892}
84893
84894/*
84895** This function returns a pointer to a nul-terminated string in memory
84896** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
84897** string contains a copy of zRawSql but with host parameters expanded to
84898** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
84899** then the returned string holds a copy of zRawSql with "-- " prepended
84900** to each line of text.
84901**
84902** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
84903** then long strings and blobs are truncated to that many bytes.  This
84904** can be used to prevent unreasonably large trace strings when dealing
84905** with large (multi-megabyte) strings and blobs.
84906**
84907** The calling function is responsible for making sure the memory returned
84908** is eventually freed.
84909**
84910** ALGORITHM:  Scan the input string looking for host parameters in any of
84911** these forms:  ?, ?N, $A, @A, :A.  Take care to avoid text within
84912** string literals, quoted identifier names, and comments.  For text forms,
84913** the host parameter index is found by scanning the prepared
84914** statement for the corresponding OP_Variable opcode.  Once the host
84915** parameter index is known, locate the value in p->aVar[].  Then render
84916** the value as a literal in place of the host parameter name.
84917*/
84918SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
84919  Vdbe *p,                 /* The prepared statement being evaluated */
84920  const char *zRawSql      /* Raw text of the SQL statement */
84921){
84922  sqlite3 *db;             /* The database connection */
84923  int idx = 0;             /* Index of a host parameter */
84924  int nextIndex = 1;       /* Index of next ? host parameter */
84925  int n;                   /* Length of a token prefix */
84926  int nToken;              /* Length of the parameter token */
84927  int i;                   /* Loop counter */
84928  Mem *pVar;               /* Value of a host parameter */
84929  StrAccum out;            /* Accumulate the output here */
84930#ifndef SQLITE_OMIT_UTF16
84931  Mem utf8;                /* Used to convert UTF16 into UTF8 for display */
84932#endif
84933  char zBase[100];         /* Initial working space */
84934
84935  db = p->db;
84936  sqlite3StrAccumInit(&out, 0, zBase, sizeof(zBase),
84937                      db->aLimit[SQLITE_LIMIT_LENGTH]);
84938  if( db->nVdbeExec>1 ){
84939    while( *zRawSql ){
84940      const char *zStart = zRawSql;
84941      while( *(zRawSql++)!='\n' && *zRawSql );
84942      sqlite3_str_append(&out, "-- ", 3);
84943      assert( (zRawSql - zStart) > 0 );
84944      sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
84945    }
84946  }else if( p->nVar==0 ){
84947    sqlite3_str_append(&out, zRawSql, sqlite3Strlen30(zRawSql));
84948  }else{
84949    while( zRawSql[0] ){
84950      n = findNextHostParameter(zRawSql, &nToken);
84951      assert( n>0 );
84952      sqlite3_str_append(&out, zRawSql, n);
84953      zRawSql += n;
84954      assert( zRawSql[0] || nToken==0 );
84955      if( nToken==0 ) break;
84956      if( zRawSql[0]=='?' ){
84957        if( nToken>1 ){
84958          assert( sqlite3Isdigit(zRawSql[1]) );
84959          sqlite3GetInt32(&zRawSql[1], &idx);
84960        }else{
84961          idx = nextIndex;
84962        }
84963      }else{
84964        assert( zRawSql[0]==':' || zRawSql[0]=='$' ||
84965                zRawSql[0]=='@' || zRawSql[0]=='#' );
84966        testcase( zRawSql[0]==':' );
84967        testcase( zRawSql[0]=='$' );
84968        testcase( zRawSql[0]=='@' );
84969        testcase( zRawSql[0]=='#' );
84970        idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
84971        assert( idx>0 );
84972      }
84973      zRawSql += nToken;
84974      nextIndex = idx + 1;
84975      assert( idx>0 && idx<=p->nVar );
84976      pVar = &p->aVar[idx-1];
84977      if( pVar->flags & MEM_Null ){
84978        sqlite3_str_append(&out, "NULL", 4);
84979      }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
84980        sqlite3_str_appendf(&out, "%lld", pVar->u.i);
84981      }else if( pVar->flags & MEM_Real ){
84982        sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
84983      }else if( pVar->flags & MEM_Str ){
84984        int nOut;  /* Number of bytes of the string text to include in output */
84985#ifndef SQLITE_OMIT_UTF16
84986        u8 enc = ENC(db);
84987        if( enc!=SQLITE_UTF8 ){
84988          memset(&utf8, 0, sizeof(utf8));
84989          utf8.db = db;
84990          sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
84991          if( SQLITE_NOMEM==sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8) ){
84992            out.accError = SQLITE_NOMEM;
84993            out.nAlloc = 0;
84994          }
84995          pVar = &utf8;
84996        }
84997#endif
84998        nOut = pVar->n;
84999#ifdef SQLITE_TRACE_SIZE_LIMIT
85000        if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
85001          nOut = SQLITE_TRACE_SIZE_LIMIT;
85002          while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
85003        }
85004#endif
85005        sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
85006#ifdef SQLITE_TRACE_SIZE_LIMIT
85007        if( nOut<pVar->n ){
85008          sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
85009        }
85010#endif
85011#ifndef SQLITE_OMIT_UTF16
85012        if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
85013#endif
85014      }else if( pVar->flags & MEM_Zero ){
85015        sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
85016      }else{
85017        int nOut;  /* Number of bytes of the blob to include in output */
85018        assert( pVar->flags & MEM_Blob );
85019        sqlite3_str_append(&out, "x'", 2);
85020        nOut = pVar->n;
85021#ifdef SQLITE_TRACE_SIZE_LIMIT
85022        if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
85023#endif
85024        for(i=0; i<nOut; i++){
85025          sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
85026        }
85027        sqlite3_str_append(&out, "'", 1);
85028#ifdef SQLITE_TRACE_SIZE_LIMIT
85029        if( nOut<pVar->n ){
85030          sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
85031        }
85032#endif
85033      }
85034    }
85035  }
85036  if( out.accError ) sqlite3_str_reset(&out);
85037  return sqlite3StrAccumFinish(&out);
85038}
85039
85040#endif /* #ifndef SQLITE_OMIT_TRACE */
85041
85042/************** End of vdbetrace.c *******************************************/
85043/************** Begin file vdbe.c ********************************************/
85044/*
85045** 2001 September 15
85046**
85047** The author disclaims copyright to this source code.  In place of
85048** a legal notice, here is a blessing:
85049**
85050**    May you do good and not evil.
85051**    May you find forgiveness for yourself and forgive others.
85052**    May you share freely, never taking more than you give.
85053**
85054*************************************************************************
85055** The code in this file implements the function that runs the
85056** bytecode of a prepared statement.
85057**
85058** Various scripts scan this source file in order to generate HTML
85059** documentation, headers files, or other derived files.  The formatting
85060** of the code in this file is, therefore, important.  See other comments
85061** in this file for details.  If in doubt, do not deviate from existing
85062** commenting and indentation practices when changing or adding code.
85063*/
85064/* #include "sqliteInt.h" */
85065/* #include "vdbeInt.h" */
85066
85067/*
85068** Invoke this macro on memory cells just prior to changing the
85069** value of the cell.  This macro verifies that shallow copies are
85070** not misused.  A shallow copy of a string or blob just copies a
85071** pointer to the string or blob, not the content.  If the original
85072** is changed while the copy is still in use, the string or blob might
85073** be changed out from under the copy.  This macro verifies that nothing
85074** like that ever happens.
85075*/
85076#ifdef SQLITE_DEBUG
85077# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
85078#else
85079# define memAboutToChange(P,M)
85080#endif
85081
85082/*
85083** The following global variable is incremented every time a cursor
85084** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes.  The test
85085** procedures use this information to make sure that indices are
85086** working correctly.  This variable has no function other than to
85087** help verify the correct operation of the library.
85088*/
85089#ifdef SQLITE_TEST
85090SQLITE_API int sqlite3_search_count = 0;
85091#endif
85092
85093/*
85094** When this global variable is positive, it gets decremented once before
85095** each instruction in the VDBE.  When it reaches zero, the u1.isInterrupted
85096** field of the sqlite3 structure is set in order to simulate an interrupt.
85097**
85098** This facility is used for testing purposes only.  It does not function
85099** in an ordinary build.
85100*/
85101#ifdef SQLITE_TEST
85102SQLITE_API int sqlite3_interrupt_count = 0;
85103#endif
85104
85105/*
85106** The next global variable is incremented each type the OP_Sort opcode
85107** is executed.  The test procedures use this information to make sure that
85108** sorting is occurring or not occurring at appropriate times.   This variable
85109** has no function other than to help verify the correct operation of the
85110** library.
85111*/
85112#ifdef SQLITE_TEST
85113SQLITE_API int sqlite3_sort_count = 0;
85114#endif
85115
85116/*
85117** The next global variable records the size of the largest MEM_Blob
85118** or MEM_Str that has been used by a VDBE opcode.  The test procedures
85119** use this information to make sure that the zero-blob functionality
85120** is working correctly.   This variable has no function other than to
85121** help verify the correct operation of the library.
85122*/
85123#ifdef SQLITE_TEST
85124SQLITE_API int sqlite3_max_blobsize = 0;
85125static void updateMaxBlobsize(Mem *p){
85126  if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
85127    sqlite3_max_blobsize = p->n;
85128  }
85129}
85130#endif
85131
85132/*
85133** This macro evaluates to true if either the update hook or the preupdate
85134** hook are enabled for database connect DB.
85135*/
85136#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
85137# define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
85138#else
85139# define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
85140#endif
85141
85142/*
85143** The next global variable is incremented each time the OP_Found opcode
85144** is executed. This is used to test whether or not the foreign key
85145** operation implemented using OP_FkIsZero is working. This variable
85146** has no function other than to help verify the correct operation of the
85147** library.
85148*/
85149#ifdef SQLITE_TEST
85150SQLITE_API int sqlite3_found_count = 0;
85151#endif
85152
85153/*
85154** Test a register to see if it exceeds the current maximum blob size.
85155** If it does, record the new maximum blob size.
85156*/
85157#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
85158# define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
85159#else
85160# define UPDATE_MAX_BLOBSIZE(P)
85161#endif
85162
85163#ifdef SQLITE_DEBUG
85164/* This routine provides a convenient place to set a breakpoint during
85165** tracing with PRAGMA vdbe_trace=on.  The breakpoint fires right after
85166** each opcode is printed.  Variables "pc" (program counter) and pOp are
85167** available to add conditionals to the breakpoint.  GDB example:
85168**
85169**         break test_trace_breakpoint if pc=22
85170**
85171** Other useful labels for breakpoints include:
85172**   test_addop_breakpoint(pc,pOp)
85173**   sqlite3CorruptError(lineno)
85174**   sqlite3MisuseError(lineno)
85175**   sqlite3CantopenError(lineno)
85176*/
85177static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){
85178  static int n = 0;
85179  n++;
85180}
85181#endif
85182
85183/*
85184** Invoke the VDBE coverage callback, if that callback is defined.  This
85185** feature is used for test suite validation only and does not appear an
85186** production builds.
85187**
85188** M is the type of branch.  I is the direction taken for this instance of
85189** the branch.
85190**
85191**   M: 2 - two-way branch (I=0: fall-thru   1: jump                )
85192**      3 - two-way + NULL (I=0: fall-thru   1: jump      2: NULL   )
85193**      4 - OP_Jump        (I=0: jump p1     1: jump p2   2: jump p3)
85194**
85195** In other words, if M is 2, then I is either 0 (for fall-through) or
85196** 1 (for when the branch is taken).  If M is 3, the I is 0 for an
85197** ordinary fall-through, I is 1 if the branch was taken, and I is 2
85198** if the result of comparison is NULL.  For M=3, I=2 the jump may or
85199** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
85200** When M is 4, that means that an OP_Jump is being run.  I is 0, 1, or 2
85201** depending on if the operands are less than, equal, or greater than.
85202**
85203** iSrcLine is the source code line (from the __LINE__ macro) that
85204** generated the VDBE instruction combined with flag bits.  The source
85205** code line number is in the lower 24 bits of iSrcLine and the upper
85206** 8 bytes are flags.  The lower three bits of the flags indicate
85207** values for I that should never occur.  For example, if the branch is
85208** always taken, the flags should be 0x05 since the fall-through and
85209** alternate branch are never taken.  If a branch is never taken then
85210** flags should be 0x06 since only the fall-through approach is allowed.
85211**
85212** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
85213** interested in equal or not-equal.  In other words, I==0 and I==2
85214** should be treated as equivalent
85215**
85216** Since only a line number is retained, not the filename, this macro
85217** only works for amalgamation builds.  But that is ok, since these macros
85218** should be no-ops except for special builds used to measure test coverage.
85219*/
85220#if !defined(SQLITE_VDBE_COVERAGE)
85221# define VdbeBranchTaken(I,M)
85222#else
85223# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
85224  static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
85225    u8 mNever;
85226    assert( I<=2 );  /* 0: fall through,  1: taken,  2: alternate taken */
85227    assert( M<=4 );  /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
85228    assert( I<M );   /* I can only be 2 if M is 3 or 4 */
85229    /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
85230    I = 1<<I;
85231    /* The upper 8 bits of iSrcLine are flags.  The lower three bits of
85232    ** the flags indicate directions that the branch can never go.  If
85233    ** a branch really does go in one of those directions, assert right
85234    ** away. */
85235    mNever = iSrcLine >> 24;
85236    assert( (I & mNever)==0 );
85237    if( sqlite3GlobalConfig.xVdbeBranch==0 ) return;  /*NO_TEST*/
85238    /* Invoke the branch coverage callback with three arguments:
85239    **    iSrcLine - the line number of the VdbeCoverage() macro, with
85240    **               flags removed.
85241    **    I        - Mask of bits 0x07 indicating which cases are are
85242    **               fulfilled by this instance of the jump.  0x01 means
85243    **               fall-thru, 0x02 means taken, 0x04 means NULL.  Any
85244    **               impossible cases (ex: if the comparison is never NULL)
85245    **               are filled in automatically so that the coverage
85246    **               measurement logic does not flag those impossible cases
85247    **               as missed coverage.
85248    **    M        - Type of jump.  Same as M argument above
85249    */
85250    I |= mNever;
85251    if( M==2 ) I |= 0x04;
85252    if( M==4 ){
85253      I |= 0x08;
85254      if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
85255    }
85256    sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
85257                                    iSrcLine&0xffffff, I, M);
85258  }
85259#endif
85260
85261/*
85262** An ephemeral string value (signified by the MEM_Ephem flag) contains
85263** a pointer to a dynamically allocated string where some other entity
85264** is responsible for deallocating that string.  Because the register
85265** does not control the string, it might be deleted without the register
85266** knowing it.
85267**
85268** This routine converts an ephemeral string into a dynamically allocated
85269** string that the register itself controls.  In other words, it
85270** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
85271*/
85272#define Deephemeralize(P) \
85273   if( ((P)->flags&MEM_Ephem)!=0 \
85274       && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
85275
85276/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
85277#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
85278
85279/*
85280** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
85281** if we run out of memory.
85282*/
85283static VdbeCursor *allocateCursor(
85284  Vdbe *p,              /* The virtual machine */
85285  int iCur,             /* Index of the new VdbeCursor */
85286  int nField,           /* Number of fields in the table or index */
85287  int iDb,              /* Database the cursor belongs to, or -1 */
85288  u8 eCurType           /* Type of the new cursor */
85289){
85290  /* Find the memory cell that will be used to store the blob of memory
85291  ** required for this VdbeCursor structure. It is convenient to use a
85292  ** vdbe memory cell to manage the memory allocation required for a
85293  ** VdbeCursor structure for the following reasons:
85294  **
85295  **   * Sometimes cursor numbers are used for a couple of different
85296  **     purposes in a vdbe program. The different uses might require
85297  **     different sized allocations. Memory cells provide growable
85298  **     allocations.
85299  **
85300  **   * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
85301  **     be freed lazily via the sqlite3_release_memory() API. This
85302  **     minimizes the number of malloc calls made by the system.
85303  **
85304  ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
85305  ** the top of the register space.  Cursor 1 is at Mem[p->nMem-1].
85306  ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
85307  */
85308  Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
85309
85310  int nByte;
85311  VdbeCursor *pCx = 0;
85312  nByte =
85313      ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
85314      (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
85315
85316  assert( iCur>=0 && iCur<p->nCursor );
85317  if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
85318    /* Before calling sqlite3VdbeFreeCursor(), ensure the isEphemeral flag
85319    ** is clear. Otherwise, if this is an ephemeral cursor created by
85320    ** OP_OpenDup, the cursor will not be closed and will still be part
85321    ** of a BtShared.pCursor list.  */
85322    if( p->apCsr[iCur]->pBtx==0 ) p->apCsr[iCur]->isEphemeral = 0;
85323    sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
85324    p->apCsr[iCur] = 0;
85325  }
85326  if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
85327    p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
85328    memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
85329    pCx->eCurType = eCurType;
85330    pCx->iDb = iDb;
85331    pCx->nField = nField;
85332    pCx->aOffset = &pCx->aType[nField];
85333    if( eCurType==CURTYPE_BTREE ){
85334      pCx->uc.pCursor = (BtCursor*)
85335          &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
85336      sqlite3BtreeCursorZero(pCx->uc.pCursor);
85337    }
85338  }
85339  return pCx;
85340}
85341
85342/*
85343** The string in pRec is known to look like an integer and to have a
85344** floating point value of rValue.  Return true and set *piValue to the
85345** integer value if the string is in range to be an integer.  Otherwise,
85346** return false.
85347*/
85348static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
85349  i64 iValue = (double)rValue;
85350  if( sqlite3RealSameAsInt(rValue,iValue) ){
85351    *piValue = iValue;
85352    return 1;
85353  }
85354  return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
85355}
85356
85357/*
85358** Try to convert a value into a numeric representation if we can
85359** do so without loss of information.  In other words, if the string
85360** looks like a number, convert it into a number.  If it does not
85361** look like a number, leave it alone.
85362**
85363** If the bTryForInt flag is true, then extra effort is made to give
85364** an integer representation.  Strings that look like floating point
85365** values but which have no fractional component (example: '48.00')
85366** will have a MEM_Int representation when bTryForInt is true.
85367**
85368** If bTryForInt is false, then if the input string contains a decimal
85369** point or exponential notation, the result is only MEM_Real, even
85370** if there is an exact integer representation of the quantity.
85371*/
85372static void applyNumericAffinity(Mem *pRec, int bTryForInt){
85373  double rValue;
85374  u8 enc = pRec->enc;
85375  int rc;
85376  assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
85377  rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
85378  if( rc<=0 ) return;
85379  if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
85380    pRec->flags |= MEM_Int;
85381  }else{
85382    pRec->u.r = rValue;
85383    pRec->flags |= MEM_Real;
85384    if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
85385  }
85386  /* TEXT->NUMERIC is many->one.  Hence, it is important to invalidate the
85387  ** string representation after computing a numeric equivalent, because the
85388  ** string representation might not be the canonical representation for the
85389  ** numeric value.  Ticket [343634942dd54ab57b7024] 2018-01-31. */
85390  pRec->flags &= ~MEM_Str;
85391}
85392
85393/*
85394** Processing is determine by the affinity parameter:
85395**
85396** SQLITE_AFF_INTEGER:
85397** SQLITE_AFF_REAL:
85398** SQLITE_AFF_NUMERIC:
85399**    Try to convert pRec to an integer representation or a
85400**    floating-point representation if an integer representation
85401**    is not possible.  Note that the integer representation is
85402**    always preferred, even if the affinity is REAL, because
85403**    an integer representation is more space efficient on disk.
85404**
85405** SQLITE_AFF_TEXT:
85406**    Convert pRec to a text representation.
85407**
85408** SQLITE_AFF_BLOB:
85409** SQLITE_AFF_NONE:
85410**    No-op.  pRec is unchanged.
85411*/
85412static void applyAffinity(
85413  Mem *pRec,          /* The value to apply affinity to */
85414  char affinity,      /* The affinity to be applied */
85415  u8 enc              /* Use this text encoding */
85416){
85417  if( affinity>=SQLITE_AFF_NUMERIC ){
85418    assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
85419             || affinity==SQLITE_AFF_NUMERIC );
85420    if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
85421      if( (pRec->flags & MEM_Real)==0 ){
85422        if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
85423      }else{
85424        sqlite3VdbeIntegerAffinity(pRec);
85425      }
85426    }
85427  }else if( affinity==SQLITE_AFF_TEXT ){
85428    /* Only attempt the conversion to TEXT if there is an integer or real
85429    ** representation (blob and NULL do not get converted) but no string
85430    ** representation.  It would be harmless to repeat the conversion if
85431    ** there is already a string rep, but it is pointless to waste those
85432    ** CPU cycles. */
85433    if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
85434      if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
85435        testcase( pRec->flags & MEM_Int );
85436        testcase( pRec->flags & MEM_Real );
85437        testcase( pRec->flags & MEM_IntReal );
85438        sqlite3VdbeMemStringify(pRec, enc, 1);
85439      }
85440    }
85441    pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
85442  }
85443}
85444
85445/*
85446** Try to convert the type of a function argument or a result column
85447** into a numeric representation.  Use either INTEGER or REAL whichever
85448** is appropriate.  But only do the conversion if it is possible without
85449** loss of information and return the revised type of the argument.
85450*/
85451SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
85452  int eType = sqlite3_value_type(pVal);
85453  if( eType==SQLITE_TEXT ){
85454    Mem *pMem = (Mem*)pVal;
85455    applyNumericAffinity(pMem, 0);
85456    eType = sqlite3_value_type(pVal);
85457  }
85458  return eType;
85459}
85460
85461/*
85462** Exported version of applyAffinity(). This one works on sqlite3_value*,
85463** not the internal Mem* type.
85464*/
85465SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
85466  sqlite3_value *pVal,
85467  u8 affinity,
85468  u8 enc
85469){
85470  applyAffinity((Mem *)pVal, affinity, enc);
85471}
85472
85473/*
85474** pMem currently only holds a string type (or maybe a BLOB that we can
85475** interpret as a string if we want to).  Compute its corresponding
85476** numeric type, if has one.  Set the pMem->u.r and pMem->u.i fields
85477** accordingly.
85478*/
85479static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
85480  int rc;
85481  sqlite3_int64 ix;
85482  assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
85483  assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
85484  ExpandBlob(pMem);
85485  rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
85486  if( rc<=0 ){
85487    if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
85488      pMem->u.i = ix;
85489      return MEM_Int;
85490    }else{
85491      return MEM_Real;
85492    }
85493  }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
85494    pMem->u.i = ix;
85495    return MEM_Int;
85496  }
85497  return MEM_Real;
85498}
85499
85500/*
85501** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
85502** none.
85503**
85504** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
85505** But it does set pMem->u.r and pMem->u.i appropriately.
85506*/
85507static u16 numericType(Mem *pMem){
85508  if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){
85509    testcase( pMem->flags & MEM_Int );
85510    testcase( pMem->flags & MEM_Real );
85511    testcase( pMem->flags & MEM_IntReal );
85512    return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal);
85513  }
85514  if( pMem->flags & (MEM_Str|MEM_Blob) ){
85515    testcase( pMem->flags & MEM_Str );
85516    testcase( pMem->flags & MEM_Blob );
85517    return computeNumericType(pMem);
85518  }
85519  return 0;
85520}
85521
85522#ifdef SQLITE_DEBUG
85523/*
85524** Write a nice string representation of the contents of cell pMem
85525** into buffer zBuf, length nBuf.
85526*/
85527SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){
85528  int f = pMem->flags;
85529  static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
85530  if( f&MEM_Blob ){
85531    int i;
85532    char c;
85533    if( f & MEM_Dyn ){
85534      c = 'z';
85535      assert( (f & (MEM_Static|MEM_Ephem))==0 );
85536    }else if( f & MEM_Static ){
85537      c = 't';
85538      assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
85539    }else if( f & MEM_Ephem ){
85540      c = 'e';
85541      assert( (f & (MEM_Static|MEM_Dyn))==0 );
85542    }else{
85543      c = 's';
85544    }
85545    sqlite3_str_appendf(pStr, "%cx[", c);
85546    for(i=0; i<25 && i<pMem->n; i++){
85547      sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
85548    }
85549    sqlite3_str_appendf(pStr, "|");
85550    for(i=0; i<25 && i<pMem->n; i++){
85551      char z = pMem->z[i];
85552      sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z);
85553    }
85554    sqlite3_str_appendf(pStr,"]");
85555    if( f & MEM_Zero ){
85556      sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
85557    }
85558  }else if( f & MEM_Str ){
85559    int j;
85560    u8 c;
85561    if( f & MEM_Dyn ){
85562      c = 'z';
85563      assert( (f & (MEM_Static|MEM_Ephem))==0 );
85564    }else if( f & MEM_Static ){
85565      c = 't';
85566      assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
85567    }else if( f & MEM_Ephem ){
85568      c = 'e';
85569      assert( (f & (MEM_Static|MEM_Dyn))==0 );
85570    }else{
85571      c = 's';
85572    }
85573    sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
85574    for(j=0; j<25 && j<pMem->n; j++){
85575      c = pMem->z[j];
85576      sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.');
85577    }
85578    sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
85579  }
85580}
85581#endif
85582
85583#ifdef SQLITE_DEBUG
85584/*
85585** Print the value of a register for tracing purposes:
85586*/
85587static void memTracePrint(Mem *p){
85588  if( p->flags & MEM_Undefined ){
85589    printf(" undefined");
85590  }else if( p->flags & MEM_Null ){
85591    printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
85592  }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
85593    printf(" si:%lld", p->u.i);
85594  }else if( (p->flags & (MEM_IntReal))!=0 ){
85595    printf(" ir:%lld", p->u.i);
85596  }else if( p->flags & MEM_Int ){
85597    printf(" i:%lld", p->u.i);
85598#ifndef SQLITE_OMIT_FLOATING_POINT
85599  }else if( p->flags & MEM_Real ){
85600    printf(" r:%.17g", p->u.r);
85601#endif
85602  }else if( sqlite3VdbeMemIsRowSet(p) ){
85603    printf(" (rowset)");
85604  }else{
85605    StrAccum acc;
85606    char zBuf[1000];
85607    sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
85608    sqlite3VdbeMemPrettyPrint(p, &acc);
85609    printf(" %s", sqlite3StrAccumFinish(&acc));
85610  }
85611  if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
85612}
85613static void registerTrace(int iReg, Mem *p){
85614  printf("R[%d] = ", iReg);
85615  memTracePrint(p);
85616  if( p->pScopyFrom ){
85617    printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
85618  }
85619  printf("\n");
85620  sqlite3VdbeCheckMemInvariants(p);
85621}
85622#endif
85623
85624#ifdef SQLITE_DEBUG
85625/*
85626** Show the values of all registers in the virtual machine.  Used for
85627** interactive debugging.
85628*/
85629SQLITE_PRIVATE void sqlite3VdbeRegisterDump(Vdbe *v){
85630  int i;
85631  for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
85632}
85633#endif /* SQLITE_DEBUG */
85634
85635
85636#ifdef SQLITE_DEBUG
85637#  define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
85638#else
85639#  define REGISTER_TRACE(R,M)
85640#endif
85641
85642
85643#ifdef VDBE_PROFILE
85644
85645/*
85646** hwtime.h contains inline assembler code for implementing
85647** high-performance timing routines.
85648*/
85649/************** Include hwtime.h in the middle of vdbe.c *********************/
85650/************** Begin file hwtime.h ******************************************/
85651/*
85652** 2008 May 27
85653**
85654** The author disclaims copyright to this source code.  In place of
85655** a legal notice, here is a blessing:
85656**
85657**    May you do good and not evil.
85658**    May you find forgiveness for yourself and forgive others.
85659**    May you share freely, never taking more than you give.
85660**
85661******************************************************************************
85662**
85663** This file contains inline asm code for retrieving "high-performance"
85664** counters for x86 and x86_64 class CPUs.
85665*/
85666#ifndef SQLITE_HWTIME_H
85667#define SQLITE_HWTIME_H
85668
85669/*
85670** The following routine only works on pentium-class (or newer) processors.
85671** It uses the RDTSC opcode to read the cycle count value out of the
85672** processor and returns that value.  This can be used for high-res
85673** profiling.
85674*/
85675#if !defined(__STRICT_ANSI__) && \
85676    (defined(__GNUC__) || defined(_MSC_VER)) && \
85677    (defined(i386) || defined(__i386__) || defined(_M_IX86))
85678
85679  #if defined(__GNUC__)
85680
85681  __inline__ sqlite_uint64 sqlite3Hwtime(void){
85682     unsigned int lo, hi;
85683     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
85684     return (sqlite_uint64)hi << 32 | lo;
85685  }
85686
85687  #elif defined(_MSC_VER)
85688
85689  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
85690     __asm {
85691        rdtsc
85692        ret       ; return value at EDX:EAX
85693     }
85694  }
85695
85696  #endif
85697
85698#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__x86_64__))
85699
85700  __inline__ sqlite_uint64 sqlite3Hwtime(void){
85701      unsigned long val;
85702      __asm__ __volatile__ ("rdtsc" : "=A" (val));
85703      return val;
85704  }
85705
85706#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__ppc__))
85707
85708  __inline__ sqlite_uint64 sqlite3Hwtime(void){
85709      unsigned long long retval;
85710      unsigned long junk;
85711      __asm__ __volatile__ ("\n\
85712          1:      mftbu   %1\n\
85713                  mftb    %L0\n\
85714                  mftbu   %0\n\
85715                  cmpw    %0,%1\n\
85716                  bne     1b"
85717                  : "=r" (retval), "=r" (junk));
85718      return retval;
85719  }
85720
85721#else
85722
85723  /*
85724  ** asm() is needed for hardware timing support.  Without asm(),
85725  ** disable the sqlite3Hwtime() routine.
85726  **
85727  ** sqlite3Hwtime() is only used for some obscure debugging
85728  ** and analysis configurations, not in any deliverable, so this
85729  ** should not be a great loss.
85730  */
85731SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
85732
85733#endif
85734
85735#endif /* !defined(SQLITE_HWTIME_H) */
85736
85737/************** End of hwtime.h **********************************************/
85738/************** Continuing where we left off in vdbe.c ***********************/
85739
85740#endif
85741
85742#ifndef NDEBUG
85743/*
85744** This function is only called from within an assert() expression. It
85745** checks that the sqlite3.nTransaction variable is correctly set to
85746** the number of non-transaction savepoints currently in the
85747** linked list starting at sqlite3.pSavepoint.
85748**
85749** Usage:
85750**
85751**     assert( checkSavepointCount(db) );
85752*/
85753static int checkSavepointCount(sqlite3 *db){
85754  int n = 0;
85755  Savepoint *p;
85756  for(p=db->pSavepoint; p; p=p->pNext) n++;
85757  assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
85758  return 1;
85759}
85760#endif
85761
85762/*
85763** Return the register of pOp->p2 after first preparing it to be
85764** overwritten with an integer value.
85765*/
85766static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
85767  sqlite3VdbeMemSetNull(pOut);
85768  pOut->flags = MEM_Int;
85769  return pOut;
85770}
85771static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
85772  Mem *pOut;
85773  assert( pOp->p2>0 );
85774  assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
85775  pOut = &p->aMem[pOp->p2];
85776  memAboutToChange(p, pOut);
85777  if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
85778    return out2PrereleaseWithClear(pOut);
85779  }else{
85780    pOut->flags = MEM_Int;
85781    return pOut;
85782  }
85783}
85784
85785
85786/*
85787** Execute as much of a VDBE program as we can.
85788** This is the core of sqlite3_step().
85789*/
85790SQLITE_PRIVATE int sqlite3VdbeExec(
85791  Vdbe *p                    /* The VDBE */
85792){
85793  Op *aOp = p->aOp;          /* Copy of p->aOp */
85794  Op *pOp = aOp;             /* Current operation */
85795#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
85796  Op *pOrigOp;               /* Value of pOp at the top of the loop */
85797#endif
85798#ifdef SQLITE_DEBUG
85799  int nExtraDelete = 0;      /* Verifies FORDELETE and AUXDELETE flags */
85800#endif
85801  int rc = SQLITE_OK;        /* Value to return */
85802  sqlite3 *db = p->db;       /* The database */
85803  u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
85804  u8 encoding = ENC(db);     /* The database encoding */
85805  int iCompare = 0;          /* Result of last comparison */
85806  unsigned nVmStep = 0;      /* Number of virtual machine steps */
85807#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
85808  unsigned nProgressLimit;   /* Invoke xProgress() when nVmStep reaches this */
85809#endif
85810  Mem *aMem = p->aMem;       /* Copy of p->aMem */
85811  Mem *pIn1 = 0;             /* 1st input operand */
85812  Mem *pIn2 = 0;             /* 2nd input operand */
85813  Mem *pIn3 = 0;             /* 3rd input operand */
85814  Mem *pOut = 0;             /* Output operand */
85815#ifdef VDBE_PROFILE
85816  u64 start;                 /* CPU clock count at start of opcode */
85817#endif
85818  /*** INSERT STACK UNION HERE ***/
85819
85820  assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
85821  sqlite3VdbeEnter(p);
85822#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
85823  if( db->xProgress ){
85824    u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
85825    assert( 0 < db->nProgressOps );
85826    nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
85827  }else{
85828    nProgressLimit = 0xffffffff;
85829  }
85830#endif
85831  if( p->rc==SQLITE_NOMEM ){
85832    /* This happens if a malloc() inside a call to sqlite3_column_text() or
85833    ** sqlite3_column_text16() failed.  */
85834    goto no_mem;
85835  }
85836  assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
85837  testcase( p->rc!=SQLITE_OK );
85838  p->rc = SQLITE_OK;
85839  assert( p->bIsReader || p->readOnly!=0 );
85840  p->iCurrentTime = 0;
85841  assert( p->explain==0 );
85842  p->pResultSet = 0;
85843  db->busyHandler.nBusy = 0;
85844  if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
85845  sqlite3VdbeIOTraceSql(p);
85846#ifdef SQLITE_DEBUG
85847  sqlite3BeginBenignMalloc();
85848  if( p->pc==0
85849   && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
85850  ){
85851    int i;
85852    int once = 1;
85853    sqlite3VdbePrintSql(p);
85854    if( p->db->flags & SQLITE_VdbeListing ){
85855      printf("VDBE Program Listing:\n");
85856      for(i=0; i<p->nOp; i++){
85857        sqlite3VdbePrintOp(stdout, i, &aOp[i]);
85858      }
85859    }
85860    if( p->db->flags & SQLITE_VdbeEQP ){
85861      for(i=0; i<p->nOp; i++){
85862        if( aOp[i].opcode==OP_Explain ){
85863          if( once ) printf("VDBE Query Plan:\n");
85864          printf("%s\n", aOp[i].p4.z);
85865          once = 0;
85866        }
85867      }
85868    }
85869    if( p->db->flags & SQLITE_VdbeTrace )  printf("VDBE Trace:\n");
85870  }
85871  sqlite3EndBenignMalloc();
85872#endif
85873  for(pOp=&aOp[p->pc]; 1; pOp++){
85874    /* Errors are detected by individual opcodes, with an immediate
85875    ** jumps to abort_due_to_error. */
85876    assert( rc==SQLITE_OK );
85877
85878    assert( pOp>=aOp && pOp<&aOp[p->nOp]);
85879#ifdef VDBE_PROFILE
85880    start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
85881#endif
85882    nVmStep++;
85883#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
85884    if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
85885#endif
85886
85887    /* Only allow tracing if SQLITE_DEBUG is defined.
85888    */
85889#ifdef SQLITE_DEBUG
85890    if( db->flags & SQLITE_VdbeTrace ){
85891      sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
85892      test_trace_breakpoint((int)(pOp - aOp),pOp,p);
85893    }
85894#endif
85895
85896
85897    /* Check to see if we need to simulate an interrupt.  This only happens
85898    ** if we have a special test build.
85899    */
85900#ifdef SQLITE_TEST
85901    if( sqlite3_interrupt_count>0 ){
85902      sqlite3_interrupt_count--;
85903      if( sqlite3_interrupt_count==0 ){
85904        sqlite3_interrupt(db);
85905      }
85906    }
85907#endif
85908
85909    /* Sanity checking on other operands */
85910#ifdef SQLITE_DEBUG
85911    {
85912      u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
85913      if( (opProperty & OPFLG_IN1)!=0 ){
85914        assert( pOp->p1>0 );
85915        assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
85916        assert( memIsValid(&aMem[pOp->p1]) );
85917        assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
85918        REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
85919      }
85920      if( (opProperty & OPFLG_IN2)!=0 ){
85921        assert( pOp->p2>0 );
85922        assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
85923        assert( memIsValid(&aMem[pOp->p2]) );
85924        assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
85925        REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
85926      }
85927      if( (opProperty & OPFLG_IN3)!=0 ){
85928        assert( pOp->p3>0 );
85929        assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
85930        assert( memIsValid(&aMem[pOp->p3]) );
85931        assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
85932        REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
85933      }
85934      if( (opProperty & OPFLG_OUT2)!=0 ){
85935        assert( pOp->p2>0 );
85936        assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
85937        memAboutToChange(p, &aMem[pOp->p2]);
85938      }
85939      if( (opProperty & OPFLG_OUT3)!=0 ){
85940        assert( pOp->p3>0 );
85941        assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
85942        memAboutToChange(p, &aMem[pOp->p3]);
85943      }
85944    }
85945#endif
85946#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
85947    pOrigOp = pOp;
85948#endif
85949
85950    switch( pOp->opcode ){
85951
85952/*****************************************************************************
85953** What follows is a massive switch statement where each case implements a
85954** separate instruction in the virtual machine.  If we follow the usual
85955** indentation conventions, each case should be indented by 6 spaces.  But
85956** that is a lot of wasted space on the left margin.  So the code within
85957** the switch statement will break with convention and be flush-left. Another
85958** big comment (similar to this one) will mark the point in the code where
85959** we transition back to normal indentation.
85960**
85961** The formatting of each case is important.  The makefile for SQLite
85962** generates two C files "opcodes.h" and "opcodes.c" by scanning this
85963** file looking for lines that begin with "case OP_".  The opcodes.h files
85964** will be filled with #defines that give unique integer values to each
85965** opcode and the opcodes.c file is filled with an array of strings where
85966** each string is the symbolic name for the corresponding opcode.  If the
85967** case statement is followed by a comment of the form "/# same as ... #/"
85968** that comment is used to determine the particular value of the opcode.
85969**
85970** Other keywords in the comment that follows each case are used to
85971** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
85972** Keywords include: in1, in2, in3, out2, out3.  See
85973** the mkopcodeh.awk script for additional information.
85974**
85975** Documentation about VDBE opcodes is generated by scanning this file
85976** for lines of that contain "Opcode:".  That line and all subsequent
85977** comment lines are used in the generation of the opcode.html documentation
85978** file.
85979**
85980** SUMMARY:
85981**
85982**     Formatting is important to scripts that scan this file.
85983**     Do not deviate from the formatting style currently in use.
85984**
85985*****************************************************************************/
85986
85987/* Opcode:  Goto * P2 * * *
85988**
85989** An unconditional jump to address P2.
85990** The next instruction executed will be
85991** the one at index P2 from the beginning of
85992** the program.
85993**
85994** The P1 parameter is not actually used by this opcode.  However, it
85995** is sometimes set to 1 instead of 0 as a hint to the command-line shell
85996** that this Goto is the bottom of a loop and that the lines from P2 down
85997** to the current line should be indented for EXPLAIN output.
85998*/
85999case OP_Goto: {             /* jump */
86000
86001#ifdef SQLITE_DEBUG
86002  /* In debuggging mode, when the p5 flags is set on an OP_Goto, that
86003  ** means we should really jump back to the preceeding OP_ReleaseReg
86004  ** instruction. */
86005  if( pOp->p5 ){
86006    assert( pOp->p2 < (int)(pOp - aOp) );
86007    assert( pOp->p2 > 1 );
86008    pOp = &aOp[pOp->p2 - 2];
86009    assert( pOp[1].opcode==OP_ReleaseReg );
86010    goto check_for_interrupt;
86011  }
86012#endif
86013
86014jump_to_p2_and_check_for_interrupt:
86015  pOp = &aOp[pOp->p2 - 1];
86016
86017  /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
86018  ** OP_VNext, or OP_SorterNext) all jump here upon
86019  ** completion.  Check to see if sqlite3_interrupt() has been called
86020  ** or if the progress callback needs to be invoked.
86021  **
86022  ** This code uses unstructured "goto" statements and does not look clean.
86023  ** But that is not due to sloppy coding habits. The code is written this
86024  ** way for performance, to avoid having to run the interrupt and progress
86025  ** checks on every opcode.  This helps sqlite3_step() to run about 1.5%
86026  ** faster according to "valgrind --tool=cachegrind" */
86027check_for_interrupt:
86028  if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
86029#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
86030  /* Call the progress callback if it is configured and the required number
86031  ** of VDBE ops have been executed (either since this invocation of
86032  ** sqlite3VdbeExec() or since last time the progress callback was called).
86033  ** If the progress callback returns non-zero, exit the virtual machine with
86034  ** a return code SQLITE_ABORT.
86035  */
86036  while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
86037    assert( db->nProgressOps!=0 );
86038    nProgressLimit += db->nProgressOps;
86039    if( db->xProgress(db->pProgressArg) ){
86040      nProgressLimit = 0xffffffff;
86041      rc = SQLITE_INTERRUPT;
86042      goto abort_due_to_error;
86043    }
86044  }
86045#endif
86046
86047  break;
86048}
86049
86050/* Opcode:  Gosub P1 P2 * * *
86051**
86052** Write the current address onto register P1
86053** and then jump to address P2.
86054*/
86055case OP_Gosub: {            /* jump */
86056  assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
86057  pIn1 = &aMem[pOp->p1];
86058  assert( VdbeMemDynamic(pIn1)==0 );
86059  memAboutToChange(p, pIn1);
86060  pIn1->flags = MEM_Int;
86061  pIn1->u.i = (int)(pOp-aOp);
86062  REGISTER_TRACE(pOp->p1, pIn1);
86063
86064  /* Most jump operations do a goto to this spot in order to update
86065  ** the pOp pointer. */
86066jump_to_p2:
86067  pOp = &aOp[pOp->p2 - 1];
86068  break;
86069}
86070
86071/* Opcode:  Return P1 * * * *
86072**
86073** Jump to the next instruction after the address in register P1.  After
86074** the jump, register P1 becomes undefined.
86075*/
86076case OP_Return: {           /* in1 */
86077  pIn1 = &aMem[pOp->p1];
86078  assert( pIn1->flags==MEM_Int );
86079  pOp = &aOp[pIn1->u.i];
86080  pIn1->flags = MEM_Undefined;
86081  break;
86082}
86083
86084/* Opcode: InitCoroutine P1 P2 P3 * *
86085**
86086** Set up register P1 so that it will Yield to the coroutine
86087** located at address P3.
86088**
86089** If P2!=0 then the coroutine implementation immediately follows
86090** this opcode.  So jump over the coroutine implementation to
86091** address P2.
86092**
86093** See also: EndCoroutine
86094*/
86095case OP_InitCoroutine: {     /* jump */
86096  assert( pOp->p1>0 &&  pOp->p1<=(p->nMem+1 - p->nCursor) );
86097  assert( pOp->p2>=0 && pOp->p2<p->nOp );
86098  assert( pOp->p3>=0 && pOp->p3<p->nOp );
86099  pOut = &aMem[pOp->p1];
86100  assert( !VdbeMemDynamic(pOut) );
86101  pOut->u.i = pOp->p3 - 1;
86102  pOut->flags = MEM_Int;
86103  if( pOp->p2 ) goto jump_to_p2;
86104  break;
86105}
86106
86107/* Opcode:  EndCoroutine P1 * * * *
86108**
86109** The instruction at the address in register P1 is a Yield.
86110** Jump to the P2 parameter of that Yield.
86111** After the jump, register P1 becomes undefined.
86112**
86113** See also: InitCoroutine
86114*/
86115case OP_EndCoroutine: {           /* in1 */
86116  VdbeOp *pCaller;
86117  pIn1 = &aMem[pOp->p1];
86118  assert( pIn1->flags==MEM_Int );
86119  assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
86120  pCaller = &aOp[pIn1->u.i];
86121  assert( pCaller->opcode==OP_Yield );
86122  assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
86123  pOp = &aOp[pCaller->p2 - 1];
86124  pIn1->flags = MEM_Undefined;
86125  break;
86126}
86127
86128/* Opcode:  Yield P1 P2 * * *
86129**
86130** Swap the program counter with the value in register P1.  This
86131** has the effect of yielding to a coroutine.
86132**
86133** If the coroutine that is launched by this instruction ends with
86134** Yield or Return then continue to the next instruction.  But if
86135** the coroutine launched by this instruction ends with
86136** EndCoroutine, then jump to P2 rather than continuing with the
86137** next instruction.
86138**
86139** See also: InitCoroutine
86140*/
86141case OP_Yield: {            /* in1, jump */
86142  int pcDest;
86143  pIn1 = &aMem[pOp->p1];
86144  assert( VdbeMemDynamic(pIn1)==0 );
86145  pIn1->flags = MEM_Int;
86146  pcDest = (int)pIn1->u.i;
86147  pIn1->u.i = (int)(pOp - aOp);
86148  REGISTER_TRACE(pOp->p1, pIn1);
86149  pOp = &aOp[pcDest];
86150  break;
86151}
86152
86153/* Opcode:  HaltIfNull  P1 P2 P3 P4 P5
86154** Synopsis: if r[P3]=null halt
86155**
86156** Check the value in register P3.  If it is NULL then Halt using
86157** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
86158** value in register P3 is not NULL, then this routine is a no-op.
86159** The P5 parameter should be 1.
86160*/
86161case OP_HaltIfNull: {      /* in3 */
86162  pIn3 = &aMem[pOp->p3];
86163#ifdef SQLITE_DEBUG
86164  if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
86165#endif
86166  if( (pIn3->flags & MEM_Null)==0 ) break;
86167  /* Fall through into OP_Halt */
86168}
86169
86170/* Opcode:  Halt P1 P2 * P4 P5
86171**
86172** Exit immediately.  All open cursors, etc are closed
86173** automatically.
86174**
86175** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
86176** or sqlite3_finalize().  For a normal halt, this should be SQLITE_OK (0).
86177** For errors, it can be some other value.  If P1!=0 then P2 will determine
86178** whether or not to rollback the current transaction.  Do not rollback
86179** if P2==OE_Fail. Do the rollback if P2==OE_Rollback.  If P2==OE_Abort,
86180** then back out all changes that have occurred during this execution of the
86181** VDBE, but do not rollback the transaction.
86182**
86183** If P4 is not null then it is an error message string.
86184**
86185** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
86186**
86187**    0:  (no change)
86188**    1:  NOT NULL contraint failed: P4
86189**    2:  UNIQUE constraint failed: P4
86190**    3:  CHECK constraint failed: P4
86191**    4:  FOREIGN KEY constraint failed: P4
86192**
86193** If P5 is not zero and P4 is NULL, then everything after the ":" is
86194** omitted.
86195**
86196** There is an implied "Halt 0 0 0" instruction inserted at the very end of
86197** every program.  So a jump past the last instruction of the program
86198** is the same as executing Halt.
86199*/
86200case OP_Halt: {
86201  VdbeFrame *pFrame;
86202  int pcx;
86203
86204  pcx = (int)(pOp - aOp);
86205#ifdef SQLITE_DEBUG
86206  if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
86207#endif
86208  if( pOp->p1==SQLITE_OK && p->pFrame ){
86209    /* Halt the sub-program. Return control to the parent frame. */
86210    pFrame = p->pFrame;
86211    p->pFrame = pFrame->pParent;
86212    p->nFrame--;
86213    sqlite3VdbeSetChanges(db, p->nChange);
86214    pcx = sqlite3VdbeFrameRestore(pFrame);
86215    if( pOp->p2==OE_Ignore ){
86216      /* Instruction pcx is the OP_Program that invoked the sub-program
86217      ** currently being halted. If the p2 instruction of this OP_Halt
86218      ** instruction is set to OE_Ignore, then the sub-program is throwing
86219      ** an IGNORE exception. In this case jump to the address specified
86220      ** as the p2 of the calling OP_Program.  */
86221      pcx = p->aOp[pcx].p2-1;
86222    }
86223    aOp = p->aOp;
86224    aMem = p->aMem;
86225    pOp = &aOp[pcx];
86226    break;
86227  }
86228  p->rc = pOp->p1;
86229  p->errorAction = (u8)pOp->p2;
86230  p->pc = pcx;
86231  assert( pOp->p5<=4 );
86232  if( p->rc ){
86233    if( pOp->p5 ){
86234      static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
86235                                             "FOREIGN KEY" };
86236      testcase( pOp->p5==1 );
86237      testcase( pOp->p5==2 );
86238      testcase( pOp->p5==3 );
86239      testcase( pOp->p5==4 );
86240      sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
86241      if( pOp->p4.z ){
86242        p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
86243      }
86244    }else{
86245      sqlite3VdbeError(p, "%s", pOp->p4.z);
86246    }
86247    sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
86248  }
86249  rc = sqlite3VdbeHalt(p);
86250  assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
86251  if( rc==SQLITE_BUSY ){
86252    p->rc = SQLITE_BUSY;
86253  }else{
86254    assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
86255    assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
86256    rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
86257  }
86258  goto vdbe_return;
86259}
86260
86261/* Opcode: Integer P1 P2 * * *
86262** Synopsis: r[P2]=P1
86263**
86264** The 32-bit integer value P1 is written into register P2.
86265*/
86266case OP_Integer: {         /* out2 */
86267  pOut = out2Prerelease(p, pOp);
86268  pOut->u.i = pOp->p1;
86269  break;
86270}
86271
86272/* Opcode: Int64 * P2 * P4 *
86273** Synopsis: r[P2]=P4
86274**
86275** P4 is a pointer to a 64-bit integer value.
86276** Write that value into register P2.
86277*/
86278case OP_Int64: {           /* out2 */
86279  pOut = out2Prerelease(p, pOp);
86280  assert( pOp->p4.pI64!=0 );
86281  pOut->u.i = *pOp->p4.pI64;
86282  break;
86283}
86284
86285#ifndef SQLITE_OMIT_FLOATING_POINT
86286/* Opcode: Real * P2 * P4 *
86287** Synopsis: r[P2]=P4
86288**
86289** P4 is a pointer to a 64-bit floating point value.
86290** Write that value into register P2.
86291*/
86292case OP_Real: {            /* same as TK_FLOAT, out2 */
86293  pOut = out2Prerelease(p, pOp);
86294  pOut->flags = MEM_Real;
86295  assert( !sqlite3IsNaN(*pOp->p4.pReal) );
86296  pOut->u.r = *pOp->p4.pReal;
86297  break;
86298}
86299#endif
86300
86301/* Opcode: String8 * P2 * P4 *
86302** Synopsis: r[P2]='P4'
86303**
86304** P4 points to a nul terminated UTF-8 string. This opcode is transformed
86305** into a String opcode before it is executed for the first time.  During
86306** this transformation, the length of string P4 is computed and stored
86307** as the P1 parameter.
86308*/
86309case OP_String8: {         /* same as TK_STRING, out2 */
86310  assert( pOp->p4.z!=0 );
86311  pOut = out2Prerelease(p, pOp);
86312  pOp->p1 = sqlite3Strlen30(pOp->p4.z);
86313
86314#ifndef SQLITE_OMIT_UTF16
86315  if( encoding!=SQLITE_UTF8 ){
86316    rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
86317    assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
86318    if( rc ) goto too_big;
86319    if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
86320    assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
86321    assert( VdbeMemDynamic(pOut)==0 );
86322    pOut->szMalloc = 0;
86323    pOut->flags |= MEM_Static;
86324    if( pOp->p4type==P4_DYNAMIC ){
86325      sqlite3DbFree(db, pOp->p4.z);
86326    }
86327    pOp->p4type = P4_DYNAMIC;
86328    pOp->p4.z = pOut->z;
86329    pOp->p1 = pOut->n;
86330  }
86331#endif
86332  if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
86333    goto too_big;
86334  }
86335  pOp->opcode = OP_String;
86336  assert( rc==SQLITE_OK );
86337  /* Fall through to the next case, OP_String */
86338}
86339
86340/* Opcode: String P1 P2 P3 P4 P5
86341** Synopsis: r[P2]='P4' (len=P1)
86342**
86343** The string value P4 of length P1 (bytes) is stored in register P2.
86344**
86345** If P3 is not zero and the content of register P3 is equal to P5, then
86346** the datatype of the register P2 is converted to BLOB.  The content is
86347** the same sequence of bytes, it is merely interpreted as a BLOB instead
86348** of a string, as if it had been CAST.  In other words:
86349**
86350** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
86351*/
86352case OP_String: {          /* out2 */
86353  assert( pOp->p4.z!=0 );
86354  pOut = out2Prerelease(p, pOp);
86355  pOut->flags = MEM_Str|MEM_Static|MEM_Term;
86356  pOut->z = pOp->p4.z;
86357  pOut->n = pOp->p1;
86358  pOut->enc = encoding;
86359  UPDATE_MAX_BLOBSIZE(pOut);
86360#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
86361  if( pOp->p3>0 ){
86362    assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
86363    pIn3 = &aMem[pOp->p3];
86364    assert( pIn3->flags & MEM_Int );
86365    if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
86366  }
86367#endif
86368  break;
86369}
86370
86371/* Opcode: Null P1 P2 P3 * *
86372** Synopsis: r[P2..P3]=NULL
86373**
86374** Write a NULL into registers P2.  If P3 greater than P2, then also write
86375** NULL into register P3 and every register in between P2 and P3.  If P3
86376** is less than P2 (typically P3 is zero) then only register P2 is
86377** set to NULL.
86378**
86379** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
86380** NULL values will not compare equal even if SQLITE_NULLEQ is set on
86381** OP_Ne or OP_Eq.
86382*/
86383case OP_Null: {           /* out2 */
86384  int cnt;
86385  u16 nullFlag;
86386  pOut = out2Prerelease(p, pOp);
86387  cnt = pOp->p3-pOp->p2;
86388  assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
86389  pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
86390  pOut->n = 0;
86391#ifdef SQLITE_DEBUG
86392  pOut->uTemp = 0;
86393#endif
86394  while( cnt>0 ){
86395    pOut++;
86396    memAboutToChange(p, pOut);
86397    sqlite3VdbeMemSetNull(pOut);
86398    pOut->flags = nullFlag;
86399    pOut->n = 0;
86400    cnt--;
86401  }
86402  break;
86403}
86404
86405/* Opcode: SoftNull P1 * * * *
86406** Synopsis: r[P1]=NULL
86407**
86408** Set register P1 to have the value NULL as seen by the OP_MakeRecord
86409** instruction, but do not free any string or blob memory associated with
86410** the register, so that if the value was a string or blob that was
86411** previously copied using OP_SCopy, the copies will continue to be valid.
86412*/
86413case OP_SoftNull: {
86414  assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
86415  pOut = &aMem[pOp->p1];
86416  pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
86417  break;
86418}
86419
86420/* Opcode: Blob P1 P2 * P4 *
86421** Synopsis: r[P2]=P4 (len=P1)
86422**
86423** P4 points to a blob of data P1 bytes long.  Store this
86424** blob in register P2.
86425*/
86426case OP_Blob: {                /* out2 */
86427  assert( pOp->p1 <= SQLITE_MAX_LENGTH );
86428  pOut = out2Prerelease(p, pOp);
86429  sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
86430  pOut->enc = encoding;
86431  UPDATE_MAX_BLOBSIZE(pOut);
86432  break;
86433}
86434
86435/* Opcode: Variable P1 P2 * P4 *
86436** Synopsis: r[P2]=parameter(P1,P4)
86437**
86438** Transfer the values of bound parameter P1 into register P2
86439**
86440** If the parameter is named, then its name appears in P4.
86441** The P4 value is used by sqlite3_bind_parameter_name().
86442*/
86443case OP_Variable: {            /* out2 */
86444  Mem *pVar;       /* Value being transferred */
86445
86446  assert( pOp->p1>0 && pOp->p1<=p->nVar );
86447  assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
86448  pVar = &p->aVar[pOp->p1 - 1];
86449  if( sqlite3VdbeMemTooBig(pVar) ){
86450    goto too_big;
86451  }
86452  pOut = &aMem[pOp->p2];
86453  if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
86454  memcpy(pOut, pVar, MEMCELLSIZE);
86455  pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
86456  pOut->flags |= MEM_Static|MEM_FromBind;
86457  UPDATE_MAX_BLOBSIZE(pOut);
86458  break;
86459}
86460
86461/* Opcode: Move P1 P2 P3 * *
86462** Synopsis: r[P2@P3]=r[P1@P3]
86463**
86464** Move the P3 values in register P1..P1+P3-1 over into
86465** registers P2..P2+P3-1.  Registers P1..P1+P3-1 are
86466** left holding a NULL.  It is an error for register ranges
86467** P1..P1+P3-1 and P2..P2+P3-1 to overlap.  It is an error
86468** for P3 to be less than 1.
86469*/
86470case OP_Move: {
86471  int n;           /* Number of registers left to copy */
86472  int p1;          /* Register to copy from */
86473  int p2;          /* Register to copy to */
86474
86475  n = pOp->p3;
86476  p1 = pOp->p1;
86477  p2 = pOp->p2;
86478  assert( n>0 && p1>0 && p2>0 );
86479  assert( p1+n<=p2 || p2+n<=p1 );
86480
86481  pIn1 = &aMem[p1];
86482  pOut = &aMem[p2];
86483  do{
86484    assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
86485    assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
86486    assert( memIsValid(pIn1) );
86487    memAboutToChange(p, pOut);
86488    sqlite3VdbeMemMove(pOut, pIn1);
86489#ifdef SQLITE_DEBUG
86490    pIn1->pScopyFrom = 0;
86491    { int i;
86492      for(i=1; i<p->nMem; i++){
86493        if( aMem[i].pScopyFrom==pIn1 ){
86494          aMem[i].pScopyFrom = pOut;
86495        }
86496      }
86497    }
86498#endif
86499    Deephemeralize(pOut);
86500    REGISTER_TRACE(p2++, pOut);
86501    pIn1++;
86502    pOut++;
86503  }while( --n );
86504  break;
86505}
86506
86507/* Opcode: Copy P1 P2 P3 * *
86508** Synopsis: r[P2@P3+1]=r[P1@P3+1]
86509**
86510** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
86511**
86512** This instruction makes a deep copy of the value.  A duplicate
86513** is made of any string or blob constant.  See also OP_SCopy.
86514*/
86515case OP_Copy: {
86516  int n;
86517
86518  n = pOp->p3;
86519  pIn1 = &aMem[pOp->p1];
86520  pOut = &aMem[pOp->p2];
86521  assert( pOut!=pIn1 );
86522  while( 1 ){
86523    memAboutToChange(p, pOut);
86524    sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
86525    Deephemeralize(pOut);
86526#ifdef SQLITE_DEBUG
86527    pOut->pScopyFrom = 0;
86528#endif
86529    REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
86530    if( (n--)==0 ) break;
86531    pOut++;
86532    pIn1++;
86533  }
86534  break;
86535}
86536
86537/* Opcode: SCopy P1 P2 * * *
86538** Synopsis: r[P2]=r[P1]
86539**
86540** Make a shallow copy of register P1 into register P2.
86541**
86542** This instruction makes a shallow copy of the value.  If the value
86543** is a string or blob, then the copy is only a pointer to the
86544** original and hence if the original changes so will the copy.
86545** Worse, if the original is deallocated, the copy becomes invalid.
86546** Thus the program must guarantee that the original will not change
86547** during the lifetime of the copy.  Use OP_Copy to make a complete
86548** copy.
86549*/
86550case OP_SCopy: {            /* out2 */
86551  pIn1 = &aMem[pOp->p1];
86552  pOut = &aMem[pOp->p2];
86553  assert( pOut!=pIn1 );
86554  sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
86555#ifdef SQLITE_DEBUG
86556  pOut->pScopyFrom = pIn1;
86557  pOut->mScopyFlags = pIn1->flags;
86558#endif
86559  break;
86560}
86561
86562/* Opcode: IntCopy P1 P2 * * *
86563** Synopsis: r[P2]=r[P1]
86564**
86565** Transfer the integer value held in register P1 into register P2.
86566**
86567** This is an optimized version of SCopy that works only for integer
86568** values.
86569*/
86570case OP_IntCopy: {            /* out2 */
86571  pIn1 = &aMem[pOp->p1];
86572  assert( (pIn1->flags & MEM_Int)!=0 );
86573  pOut = &aMem[pOp->p2];
86574  sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
86575  break;
86576}
86577
86578/* Opcode: ResultRow P1 P2 * * *
86579** Synopsis: output=r[P1@P2]
86580**
86581** The registers P1 through P1+P2-1 contain a single row of
86582** results. This opcode causes the sqlite3_step() call to terminate
86583** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
86584** structure to provide access to the r(P1)..r(P1+P2-1) values as
86585** the result row.
86586*/
86587case OP_ResultRow: {
86588  Mem *pMem;
86589  int i;
86590  assert( p->nResColumn==pOp->p2 );
86591  assert( pOp->p1>0 );
86592  assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
86593
86594  /* If this statement has violated immediate foreign key constraints, do
86595  ** not return the number of rows modified. And do not RELEASE the statement
86596  ** transaction. It needs to be rolled back.  */
86597  if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
86598    assert( db->flags&SQLITE_CountRows );
86599    assert( p->usesStmtJournal );
86600    goto abort_due_to_error;
86601  }
86602
86603  /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
86604  ** DML statements invoke this opcode to return the number of rows
86605  ** modified to the user. This is the only way that a VM that
86606  ** opens a statement transaction may invoke this opcode.
86607  **
86608  ** In case this is such a statement, close any statement transaction
86609  ** opened by this VM before returning control to the user. This is to
86610  ** ensure that statement-transactions are always nested, not overlapping.
86611  ** If the open statement-transaction is not closed here, then the user
86612  ** may step another VM that opens its own statement transaction. This
86613  ** may lead to overlapping statement transactions.
86614  **
86615  ** The statement transaction is never a top-level transaction.  Hence
86616  ** the RELEASE call below can never fail.
86617  */
86618  assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
86619  rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
86620  assert( rc==SQLITE_OK );
86621
86622  /* Invalidate all ephemeral cursor row caches */
86623  p->cacheCtr = (p->cacheCtr + 2)|1;
86624
86625  /* Make sure the results of the current row are \000 terminated
86626  ** and have an assigned type.  The results are de-ephemeralized as
86627  ** a side effect.
86628  */
86629  pMem = p->pResultSet = &aMem[pOp->p1];
86630  for(i=0; i<pOp->p2; i++){
86631    assert( memIsValid(&pMem[i]) );
86632    Deephemeralize(&pMem[i]);
86633    assert( (pMem[i].flags & MEM_Ephem)==0
86634            || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
86635    sqlite3VdbeMemNulTerminate(&pMem[i]);
86636    REGISTER_TRACE(pOp->p1+i, &pMem[i]);
86637#ifdef SQLITE_DEBUG
86638    /* The registers in the result will not be used again when the
86639    ** prepared statement restarts.  This is because sqlite3_column()
86640    ** APIs might have caused type conversions of made other changes to
86641    ** the register values.  Therefore, we can go ahead and break any
86642    ** OP_SCopy dependencies. */
86643    pMem[i].pScopyFrom = 0;
86644#endif
86645  }
86646  if( db->mallocFailed ) goto no_mem;
86647
86648  if( db->mTrace & SQLITE_TRACE_ROW ){
86649    db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
86650  }
86651
86652
86653  /* Return SQLITE_ROW
86654  */
86655  p->pc = (int)(pOp - aOp) + 1;
86656  rc = SQLITE_ROW;
86657  goto vdbe_return;
86658}
86659
86660/* Opcode: Concat P1 P2 P3 * *
86661** Synopsis: r[P3]=r[P2]+r[P1]
86662**
86663** Add the text in register P1 onto the end of the text in
86664** register P2 and store the result in register P3.
86665** If either the P1 or P2 text are NULL then store NULL in P3.
86666**
86667**   P3 = P2 || P1
86668**
86669** It is illegal for P1 and P3 to be the same register. Sometimes,
86670** if P3 is the same register as P2, the implementation is able
86671** to avoid a memcpy().
86672*/
86673case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
86674  i64 nByte;          /* Total size of the output string or blob */
86675  u16 flags1;         /* Initial flags for P1 */
86676  u16 flags2;         /* Initial flags for P2 */
86677
86678  pIn1 = &aMem[pOp->p1];
86679  pIn2 = &aMem[pOp->p2];
86680  pOut = &aMem[pOp->p3];
86681  testcase( pOut==pIn2 );
86682  assert( pIn1!=pOut );
86683  flags1 = pIn1->flags;
86684  testcase( flags1 & MEM_Null );
86685  testcase( pIn2->flags & MEM_Null );
86686  if( (flags1 | pIn2->flags) & MEM_Null ){
86687    sqlite3VdbeMemSetNull(pOut);
86688    break;
86689  }
86690  if( (flags1 & (MEM_Str|MEM_Blob))==0 ){
86691    if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem;
86692    flags1 = pIn1->flags & ~MEM_Str;
86693  }else if( (flags1 & MEM_Zero)!=0 ){
86694    if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem;
86695    flags1 = pIn1->flags & ~MEM_Str;
86696  }
86697  flags2 = pIn2->flags;
86698  if( (flags2 & (MEM_Str|MEM_Blob))==0 ){
86699    if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem;
86700    flags2 = pIn2->flags & ~MEM_Str;
86701  }else if( (flags2 & MEM_Zero)!=0 ){
86702    if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem;
86703    flags2 = pIn2->flags & ~MEM_Str;
86704  }
86705  nByte = pIn1->n + pIn2->n;
86706  if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
86707    goto too_big;
86708  }
86709  if( sqlite3VdbeMemGrow(pOut, (int)nByte+3, pOut==pIn2) ){
86710    goto no_mem;
86711  }
86712  MemSetTypeFlag(pOut, MEM_Str);
86713  if( pOut!=pIn2 ){
86714    memcpy(pOut->z, pIn2->z, pIn2->n);
86715    assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
86716    pIn2->flags = flags2;
86717  }
86718  memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
86719  assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
86720  pIn1->flags = flags1;
86721  pOut->z[nByte]=0;
86722  pOut->z[nByte+1] = 0;
86723  pOut->z[nByte+2] = 0;
86724  pOut->flags |= MEM_Term;
86725  pOut->n = (int)nByte;
86726  pOut->enc = encoding;
86727  UPDATE_MAX_BLOBSIZE(pOut);
86728  break;
86729}
86730
86731/* Opcode: Add P1 P2 P3 * *
86732** Synopsis: r[P3]=r[P1]+r[P2]
86733**
86734** Add the value in register P1 to the value in register P2
86735** and store the result in register P3.
86736** If either input is NULL, the result is NULL.
86737*/
86738/* Opcode: Multiply P1 P2 P3 * *
86739** Synopsis: r[P3]=r[P1]*r[P2]
86740**
86741**
86742** Multiply the value in register P1 by the value in register P2
86743** and store the result in register P3.
86744** If either input is NULL, the result is NULL.
86745*/
86746/* Opcode: Subtract P1 P2 P3 * *
86747** Synopsis: r[P3]=r[P2]-r[P1]
86748**
86749** Subtract the value in register P1 from the value in register P2
86750** and store the result in register P3.
86751** If either input is NULL, the result is NULL.
86752*/
86753/* Opcode: Divide P1 P2 P3 * *
86754** Synopsis: r[P3]=r[P2]/r[P1]
86755**
86756** Divide the value in register P1 by the value in register P2
86757** and store the result in register P3 (P3=P2/P1). If the value in
86758** register P1 is zero, then the result is NULL. If either input is
86759** NULL, the result is NULL.
86760*/
86761/* Opcode: Remainder P1 P2 P3 * *
86762** Synopsis: r[P3]=r[P2]%r[P1]
86763**
86764** Compute the remainder after integer register P2 is divided by
86765** register P1 and store the result in register P3.
86766** If the value in register P1 is zero the result is NULL.
86767** If either operand is NULL, the result is NULL.
86768*/
86769case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
86770case OP_Subtract:              /* same as TK_MINUS, in1, in2, out3 */
86771case OP_Multiply:              /* same as TK_STAR, in1, in2, out3 */
86772case OP_Divide:                /* same as TK_SLASH, in1, in2, out3 */
86773case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
86774  u16 flags;      /* Combined MEM_* flags from both inputs */
86775  u16 type1;      /* Numeric type of left operand */
86776  u16 type2;      /* Numeric type of right operand */
86777  i64 iA;         /* Integer value of left operand */
86778  i64 iB;         /* Integer value of right operand */
86779  double rA;      /* Real value of left operand */
86780  double rB;      /* Real value of right operand */
86781
86782  pIn1 = &aMem[pOp->p1];
86783  type1 = numericType(pIn1);
86784  pIn2 = &aMem[pOp->p2];
86785  type2 = numericType(pIn2);
86786  pOut = &aMem[pOp->p3];
86787  flags = pIn1->flags | pIn2->flags;
86788  if( (type1 & type2 & MEM_Int)!=0 ){
86789    iA = pIn1->u.i;
86790    iB = pIn2->u.i;
86791    switch( pOp->opcode ){
86792      case OP_Add:       if( sqlite3AddInt64(&iB,iA) ) goto fp_math;  break;
86793      case OP_Subtract:  if( sqlite3SubInt64(&iB,iA) ) goto fp_math;  break;
86794      case OP_Multiply:  if( sqlite3MulInt64(&iB,iA) ) goto fp_math;  break;
86795      case OP_Divide: {
86796        if( iA==0 ) goto arithmetic_result_is_null;
86797        if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
86798        iB /= iA;
86799        break;
86800      }
86801      default: {
86802        if( iA==0 ) goto arithmetic_result_is_null;
86803        if( iA==-1 ) iA = 1;
86804        iB %= iA;
86805        break;
86806      }
86807    }
86808    pOut->u.i = iB;
86809    MemSetTypeFlag(pOut, MEM_Int);
86810  }else if( (flags & MEM_Null)!=0 ){
86811    goto arithmetic_result_is_null;
86812  }else{
86813fp_math:
86814    rA = sqlite3VdbeRealValue(pIn1);
86815    rB = sqlite3VdbeRealValue(pIn2);
86816    switch( pOp->opcode ){
86817      case OP_Add:         rB += rA;       break;
86818      case OP_Subtract:    rB -= rA;       break;
86819      case OP_Multiply:    rB *= rA;       break;
86820      case OP_Divide: {
86821        /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
86822        if( rA==(double)0 ) goto arithmetic_result_is_null;
86823        rB /= rA;
86824        break;
86825      }
86826      default: {
86827        iA = sqlite3VdbeIntValue(pIn1);
86828        iB = sqlite3VdbeIntValue(pIn2);
86829        if( iA==0 ) goto arithmetic_result_is_null;
86830        if( iA==-1 ) iA = 1;
86831        rB = (double)(iB % iA);
86832        break;
86833      }
86834    }
86835#ifdef SQLITE_OMIT_FLOATING_POINT
86836    pOut->u.i = rB;
86837    MemSetTypeFlag(pOut, MEM_Int);
86838#else
86839    if( sqlite3IsNaN(rB) ){
86840      goto arithmetic_result_is_null;
86841    }
86842    pOut->u.r = rB;
86843    MemSetTypeFlag(pOut, MEM_Real);
86844#endif
86845  }
86846  break;
86847
86848arithmetic_result_is_null:
86849  sqlite3VdbeMemSetNull(pOut);
86850  break;
86851}
86852
86853/* Opcode: CollSeq P1 * * P4
86854**
86855** P4 is a pointer to a CollSeq object. If the next call to a user function
86856** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
86857** be returned. This is used by the built-in min(), max() and nullif()
86858** functions.
86859**
86860** If P1 is not zero, then it is a register that a subsequent min() or
86861** max() aggregate will set to 1 if the current row is not the minimum or
86862** maximum.  The P1 register is initialized to 0 by this instruction.
86863**
86864** The interface used by the implementation of the aforementioned functions
86865** to retrieve the collation sequence set by this opcode is not available
86866** publicly.  Only built-in functions have access to this feature.
86867*/
86868case OP_CollSeq: {
86869  assert( pOp->p4type==P4_COLLSEQ );
86870  if( pOp->p1 ){
86871    sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
86872  }
86873  break;
86874}
86875
86876/* Opcode: BitAnd P1 P2 P3 * *
86877** Synopsis: r[P3]=r[P1]&r[P2]
86878**
86879** Take the bit-wise AND of the values in register P1 and P2 and
86880** store the result in register P3.
86881** If either input is NULL, the result is NULL.
86882*/
86883/* Opcode: BitOr P1 P2 P3 * *
86884** Synopsis: r[P3]=r[P1]|r[P2]
86885**
86886** Take the bit-wise OR of the values in register P1 and P2 and
86887** store the result in register P3.
86888** If either input is NULL, the result is NULL.
86889*/
86890/* Opcode: ShiftLeft P1 P2 P3 * *
86891** Synopsis: r[P3]=r[P2]<<r[P1]
86892**
86893** Shift the integer value in register P2 to the left by the
86894** number of bits specified by the integer in register P1.
86895** Store the result in register P3.
86896** If either input is NULL, the result is NULL.
86897*/
86898/* Opcode: ShiftRight P1 P2 P3 * *
86899** Synopsis: r[P3]=r[P2]>>r[P1]
86900**
86901** Shift the integer value in register P2 to the right by the
86902** number of bits specified by the integer in register P1.
86903** Store the result in register P3.
86904** If either input is NULL, the result is NULL.
86905*/
86906case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
86907case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
86908case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
86909case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
86910  i64 iA;
86911  u64 uA;
86912  i64 iB;
86913  u8 op;
86914
86915  pIn1 = &aMem[pOp->p1];
86916  pIn2 = &aMem[pOp->p2];
86917  pOut = &aMem[pOp->p3];
86918  if( (pIn1->flags | pIn2->flags) & MEM_Null ){
86919    sqlite3VdbeMemSetNull(pOut);
86920    break;
86921  }
86922  iA = sqlite3VdbeIntValue(pIn2);
86923  iB = sqlite3VdbeIntValue(pIn1);
86924  op = pOp->opcode;
86925  if( op==OP_BitAnd ){
86926    iA &= iB;
86927  }else if( op==OP_BitOr ){
86928    iA |= iB;
86929  }else if( iB!=0 ){
86930    assert( op==OP_ShiftRight || op==OP_ShiftLeft );
86931
86932    /* If shifting by a negative amount, shift in the other direction */
86933    if( iB<0 ){
86934      assert( OP_ShiftRight==OP_ShiftLeft+1 );
86935      op = 2*OP_ShiftLeft + 1 - op;
86936      iB = iB>(-64) ? -iB : 64;
86937    }
86938
86939    if( iB>=64 ){
86940      iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
86941    }else{
86942      memcpy(&uA, &iA, sizeof(uA));
86943      if( op==OP_ShiftLeft ){
86944        uA <<= iB;
86945      }else{
86946        uA >>= iB;
86947        /* Sign-extend on a right shift of a negative number */
86948        if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
86949      }
86950      memcpy(&iA, &uA, sizeof(iA));
86951    }
86952  }
86953  pOut->u.i = iA;
86954  MemSetTypeFlag(pOut, MEM_Int);
86955  break;
86956}
86957
86958/* Opcode: AddImm  P1 P2 * * *
86959** Synopsis: r[P1]=r[P1]+P2
86960**
86961** Add the constant P2 to the value in register P1.
86962** The result is always an integer.
86963**
86964** To force any register to be an integer, just add 0.
86965*/
86966case OP_AddImm: {            /* in1 */
86967  pIn1 = &aMem[pOp->p1];
86968  memAboutToChange(p, pIn1);
86969  sqlite3VdbeMemIntegerify(pIn1);
86970  pIn1->u.i += pOp->p2;
86971  break;
86972}
86973
86974/* Opcode: MustBeInt P1 P2 * * *
86975**
86976** Force the value in register P1 to be an integer.  If the value
86977** in P1 is not an integer and cannot be converted into an integer
86978** without data loss, then jump immediately to P2, or if P2==0
86979** raise an SQLITE_MISMATCH exception.
86980*/
86981case OP_MustBeInt: {            /* jump, in1 */
86982  pIn1 = &aMem[pOp->p1];
86983  if( (pIn1->flags & MEM_Int)==0 ){
86984    applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
86985    if( (pIn1->flags & MEM_Int)==0 ){
86986      VdbeBranchTaken(1, 2);
86987      if( pOp->p2==0 ){
86988        rc = SQLITE_MISMATCH;
86989        goto abort_due_to_error;
86990      }else{
86991        goto jump_to_p2;
86992      }
86993    }
86994  }
86995  VdbeBranchTaken(0, 2);
86996  MemSetTypeFlag(pIn1, MEM_Int);
86997  break;
86998}
86999
87000#ifndef SQLITE_OMIT_FLOATING_POINT
87001/* Opcode: RealAffinity P1 * * * *
87002**
87003** If register P1 holds an integer convert it to a real value.
87004**
87005** This opcode is used when extracting information from a column that
87006** has REAL affinity.  Such column values may still be stored as
87007** integers, for space efficiency, but after extraction we want them
87008** to have only a real value.
87009*/
87010case OP_RealAffinity: {                  /* in1 */
87011  pIn1 = &aMem[pOp->p1];
87012  if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
87013    testcase( pIn1->flags & MEM_Int );
87014    testcase( pIn1->flags & MEM_IntReal );
87015    sqlite3VdbeMemRealify(pIn1);
87016    REGISTER_TRACE(pOp->p1, pIn1);
87017  }
87018  break;
87019}
87020#endif
87021
87022#ifndef SQLITE_OMIT_CAST
87023/* Opcode: Cast P1 P2 * * *
87024** Synopsis: affinity(r[P1])
87025**
87026** Force the value in register P1 to be the type defined by P2.
87027**
87028** <ul>
87029** <li> P2=='A' &rarr; BLOB
87030** <li> P2=='B' &rarr; TEXT
87031** <li> P2=='C' &rarr; NUMERIC
87032** <li> P2=='D' &rarr; INTEGER
87033** <li> P2=='E' &rarr; REAL
87034** </ul>
87035**
87036** A NULL value is not changed by this routine.  It remains NULL.
87037*/
87038case OP_Cast: {                  /* in1 */
87039  assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
87040  testcase( pOp->p2==SQLITE_AFF_TEXT );
87041  testcase( pOp->p2==SQLITE_AFF_BLOB );
87042  testcase( pOp->p2==SQLITE_AFF_NUMERIC );
87043  testcase( pOp->p2==SQLITE_AFF_INTEGER );
87044  testcase( pOp->p2==SQLITE_AFF_REAL );
87045  pIn1 = &aMem[pOp->p1];
87046  memAboutToChange(p, pIn1);
87047  rc = ExpandBlob(pIn1);
87048  if( rc ) goto abort_due_to_error;
87049  rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
87050  if( rc ) goto abort_due_to_error;
87051  UPDATE_MAX_BLOBSIZE(pIn1);
87052  REGISTER_TRACE(pOp->p1, pIn1);
87053  break;
87054}
87055#endif /* SQLITE_OMIT_CAST */
87056
87057/* Opcode: Eq P1 P2 P3 P4 P5
87058** Synopsis: IF r[P3]==r[P1]
87059**
87060** Compare the values in register P1 and P3.  If reg(P3)==reg(P1) then
87061** jump to address P2.  Or if the SQLITE_STOREP2 flag is set in P5, then
87062** store the result of comparison in register P2.
87063**
87064** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
87065** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
87066** to coerce both inputs according to this affinity before the
87067** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
87068** affinity is used. Note that the affinity conversions are stored
87069** back into the input registers P1 and P3.  So this opcode can cause
87070** persistent changes to registers P1 and P3.
87071**
87072** Once any conversions have taken place, and neither value is NULL,
87073** the values are compared. If both values are blobs then memcmp() is
87074** used to determine the results of the comparison.  If both values
87075** are text, then the appropriate collating function specified in
87076** P4 is used to do the comparison.  If P4 is not specified then
87077** memcmp() is used to compare text string.  If both values are
87078** numeric, then a numeric comparison is used. If the two values
87079** are of different types, then numbers are considered less than
87080** strings and strings are considered less than blobs.
87081**
87082** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
87083** true or false and is never NULL.  If both operands are NULL then the result
87084** of comparison is true.  If either operand is NULL then the result is false.
87085** If neither operand is NULL the result is the same as it would be if
87086** the SQLITE_NULLEQ flag were omitted from P5.
87087**
87088** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
87089** content of r[P2] is only changed if the new value is NULL or 0 (false).
87090** In other words, a prior r[P2] value will not be overwritten by 1 (true).
87091*/
87092/* Opcode: Ne P1 P2 P3 P4 P5
87093** Synopsis: IF r[P3]!=r[P1]
87094**
87095** This works just like the Eq opcode except that the jump is taken if
87096** the operands in registers P1 and P3 are not equal.  See the Eq opcode for
87097** additional information.
87098**
87099** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
87100** content of r[P2] is only changed if the new value is NULL or 1 (true).
87101** In other words, a prior r[P2] value will not be overwritten by 0 (false).
87102*/
87103/* Opcode: Lt P1 P2 P3 P4 P5
87104** Synopsis: IF r[P3]<r[P1]
87105**
87106** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
87107** jump to address P2.  Or if the SQLITE_STOREP2 flag is set in P5 store
87108** the result of comparison (0 or 1 or NULL) into register P2.
87109**
87110** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
87111** reg(P3) is NULL then the take the jump.  If the SQLITE_JUMPIFNULL
87112** bit is clear then fall through if either operand is NULL.
87113**
87114** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
87115** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
87116** to coerce both inputs according to this affinity before the
87117** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
87118** affinity is used. Note that the affinity conversions are stored
87119** back into the input registers P1 and P3.  So this opcode can cause
87120** persistent changes to registers P1 and P3.
87121**
87122** Once any conversions have taken place, and neither value is NULL,
87123** the values are compared. If both values are blobs then memcmp() is
87124** used to determine the results of the comparison.  If both values
87125** are text, then the appropriate collating function specified in
87126** P4 is  used to do the comparison.  If P4 is not specified then
87127** memcmp() is used to compare text string.  If both values are
87128** numeric, then a numeric comparison is used. If the two values
87129** are of different types, then numbers are considered less than
87130** strings and strings are considered less than blobs.
87131*/
87132/* Opcode: Le P1 P2 P3 P4 P5
87133** Synopsis: IF r[P3]<=r[P1]
87134**
87135** This works just like the Lt opcode except that the jump is taken if
87136** the content of register P3 is less than or equal to the content of
87137** register P1.  See the Lt opcode for additional information.
87138*/
87139/* Opcode: Gt P1 P2 P3 P4 P5
87140** Synopsis: IF r[P3]>r[P1]
87141**
87142** This works just like the Lt opcode except that the jump is taken if
87143** the content of register P3 is greater than the content of
87144** register P1.  See the Lt opcode for additional information.
87145*/
87146/* Opcode: Ge P1 P2 P3 P4 P5
87147** Synopsis: IF r[P3]>=r[P1]
87148**
87149** This works just like the Lt opcode except that the jump is taken if
87150** the content of register P3 is greater than or equal to the content of
87151** register P1.  See the Lt opcode for additional information.
87152*/
87153case OP_Eq:               /* same as TK_EQ, jump, in1, in3 */
87154case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
87155case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
87156case OP_Le:               /* same as TK_LE, jump, in1, in3 */
87157case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
87158case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
87159  int res, res2;      /* Result of the comparison of pIn1 against pIn3 */
87160  char affinity;      /* Affinity to use for comparison */
87161  u16 flags1;         /* Copy of initial value of pIn1->flags */
87162  u16 flags3;         /* Copy of initial value of pIn3->flags */
87163
87164  pIn1 = &aMem[pOp->p1];
87165  pIn3 = &aMem[pOp->p3];
87166  flags1 = pIn1->flags;
87167  flags3 = pIn3->flags;
87168  if( (flags1 | flags3)&MEM_Null ){
87169    /* One or both operands are NULL */
87170    if( pOp->p5 & SQLITE_NULLEQ ){
87171      /* If SQLITE_NULLEQ is set (which will only happen if the operator is
87172      ** OP_Eq or OP_Ne) then take the jump or not depending on whether
87173      ** or not both operands are null.
87174      */
87175      assert( (flags1 & MEM_Cleared)==0 );
87176      assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
87177      testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
87178      if( (flags1&flags3&MEM_Null)!=0
87179       && (flags3&MEM_Cleared)==0
87180      ){
87181        res = 0;  /* Operands are equal */
87182      }else{
87183        res = ((flags3 & MEM_Null) ? -1 : +1);  /* Operands are not equal */
87184      }
87185    }else{
87186      /* SQLITE_NULLEQ is clear and at least one operand is NULL,
87187      ** then the result is always NULL.
87188      ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
87189      */
87190      if( pOp->p5 & SQLITE_STOREP2 ){
87191        pOut = &aMem[pOp->p2];
87192        iCompare = 1;    /* Operands are not equal */
87193        memAboutToChange(p, pOut);
87194        MemSetTypeFlag(pOut, MEM_Null);
87195        REGISTER_TRACE(pOp->p2, pOut);
87196      }else{
87197        VdbeBranchTaken(2,3);
87198        if( pOp->p5 & SQLITE_JUMPIFNULL ){
87199          goto jump_to_p2;
87200        }
87201      }
87202      break;
87203    }
87204  }else{
87205    /* Neither operand is NULL.  Do a comparison. */
87206    affinity = pOp->p5 & SQLITE_AFF_MASK;
87207    if( affinity>=SQLITE_AFF_NUMERIC ){
87208      if( (flags1 | flags3)&MEM_Str ){
87209        if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
87210          applyNumericAffinity(pIn1,0);
87211          testcase( flags3==pIn3->flags );
87212          flags3 = pIn3->flags;
87213        }
87214        if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
87215          applyNumericAffinity(pIn3,0);
87216        }
87217      }
87218      /* Handle the common case of integer comparison here, as an
87219      ** optimization, to avoid a call to sqlite3MemCompare() */
87220      if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
87221        if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
87222        if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
87223        res = 0;
87224        goto compare_op;
87225      }
87226    }else if( affinity==SQLITE_AFF_TEXT ){
87227      if( (flags1 & MEM_Str)==0 && (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
87228        testcase( pIn1->flags & MEM_Int );
87229        testcase( pIn1->flags & MEM_Real );
87230        testcase( pIn1->flags & MEM_IntReal );
87231        sqlite3VdbeMemStringify(pIn1, encoding, 1);
87232        testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
87233        flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
87234        if( NEVER(pIn1==pIn3) ) flags3 = flags1 | MEM_Str;
87235      }
87236      if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
87237        testcase( pIn3->flags & MEM_Int );
87238        testcase( pIn3->flags & MEM_Real );
87239        testcase( pIn3->flags & MEM_IntReal );
87240        sqlite3VdbeMemStringify(pIn3, encoding, 1);
87241        testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
87242        flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
87243      }
87244    }
87245    assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
87246    res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
87247  }
87248compare_op:
87249  /* At this point, res is negative, zero, or positive if reg[P1] is
87250  ** less than, equal to, or greater than reg[P3], respectively.  Compute
87251  ** the answer to this operator in res2, depending on what the comparison
87252  ** operator actually is.  The next block of code depends on the fact
87253  ** that the 6 comparison operators are consecutive integers in this
87254  ** order:  NE, EQ, GT, LE, LT, GE */
87255  assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
87256  assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
87257  if( res<0 ){                        /* ne, eq, gt, le, lt, ge */
87258    static const unsigned char aLTb[] = { 1,  0,  0,  1,  1,  0 };
87259    res2 = aLTb[pOp->opcode - OP_Ne];
87260  }else if( res==0 ){
87261    static const unsigned char aEQb[] = { 0,  1,  0,  1,  0,  1 };
87262    res2 = aEQb[pOp->opcode - OP_Ne];
87263  }else{
87264    static const unsigned char aGTb[] = { 1,  0,  1,  0,  0,  1 };
87265    res2 = aGTb[pOp->opcode - OP_Ne];
87266  }
87267
87268  /* Undo any changes made by applyAffinity() to the input registers. */
87269  assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
87270  pIn3->flags = flags3;
87271  assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
87272  pIn1->flags = flags1;
87273
87274  if( pOp->p5 & SQLITE_STOREP2 ){
87275    pOut = &aMem[pOp->p2];
87276    iCompare = res;
87277    if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
87278      /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
87279      ** and prevents OP_Ne from overwriting NULL with 0.  This flag
87280      ** is only used in contexts where either:
87281      **   (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
87282      **   (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
87283      ** Therefore it is not necessary to check the content of r[P2] for
87284      ** NULL. */
87285      assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
87286      assert( res2==0 || res2==1 );
87287      testcase( res2==0 && pOp->opcode==OP_Eq );
87288      testcase( res2==1 && pOp->opcode==OP_Eq );
87289      testcase( res2==0 && pOp->opcode==OP_Ne );
87290      testcase( res2==1 && pOp->opcode==OP_Ne );
87291      if( (pOp->opcode==OP_Eq)==res2 ) break;
87292    }
87293    memAboutToChange(p, pOut);
87294    MemSetTypeFlag(pOut, MEM_Int);
87295    pOut->u.i = res2;
87296    REGISTER_TRACE(pOp->p2, pOut);
87297  }else{
87298    VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
87299    if( res2 ){
87300      goto jump_to_p2;
87301    }
87302  }
87303  break;
87304}
87305
87306/* Opcode: ElseNotEq * P2 * * *
87307**
87308** This opcode must follow an OP_Lt or OP_Gt comparison operator.  There
87309** can be zero or more OP_ReleaseReg opcodes intervening, but no other
87310** opcodes are allowed to occur between this instruction and the previous
87311** OP_Lt or OP_Gt.  Furthermore, the prior OP_Lt or OP_Gt must have the
87312** SQLITE_STOREP2 bit set in the P5 field.
87313**
87314** If result of an OP_Eq comparison on the same two operands as the
87315** prior OP_Lt or OP_Gt would have been NULL or false (0), then then
87316** jump to P2.  If the result of an OP_Eq comparison on the two previous
87317** operands would have been true (1), then fall through.
87318*/
87319case OP_ElseNotEq: {       /* same as TK_ESCAPE, jump */
87320
87321#ifdef SQLITE_DEBUG
87322  /* Verify the preconditions of this opcode - that it follows an OP_Lt or
87323  ** OP_Gt with the SQLITE_STOREP2 flag set, with zero or more intervening
87324  ** OP_ReleaseReg opcodes */
87325  int iAddr;
87326  for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
87327    if( aOp[iAddr].opcode==OP_ReleaseReg ) continue;
87328    assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt );
87329    assert( aOp[iAddr].p5 & SQLITE_STOREP2 );
87330    break;
87331  }
87332#endif /* SQLITE_DEBUG */
87333  VdbeBranchTaken(iCompare!=0, 2);
87334  if( iCompare!=0 ) goto jump_to_p2;
87335  break;
87336}
87337
87338
87339/* Opcode: Permutation * * * P4 *
87340**
87341** Set the permutation used by the OP_Compare operator in the next
87342** instruction.  The permutation is stored in the P4 operand.
87343**
87344** The permutation is only valid until the next OP_Compare that has
87345** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
87346** occur immediately prior to the OP_Compare.
87347**
87348** The first integer in the P4 integer array is the length of the array
87349** and does not become part of the permutation.
87350*/
87351case OP_Permutation: {
87352  assert( pOp->p4type==P4_INTARRAY );
87353  assert( pOp->p4.ai );
87354  assert( pOp[1].opcode==OP_Compare );
87355  assert( pOp[1].p5 & OPFLAG_PERMUTE );
87356  break;
87357}
87358
87359/* Opcode: Compare P1 P2 P3 P4 P5
87360** Synopsis: r[P1@P3] <-> r[P2@P3]
87361**
87362** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
87363** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
87364** the comparison for use by the next OP_Jump instruct.
87365**
87366** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
87367** determined by the most recent OP_Permutation operator.  If the
87368** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
87369** order.
87370**
87371** P4 is a KeyInfo structure that defines collating sequences and sort
87372** orders for the comparison.  The permutation applies to registers
87373** only.  The KeyInfo elements are used sequentially.
87374**
87375** The comparison is a sort comparison, so NULLs compare equal,
87376** NULLs are less than numbers, numbers are less than strings,
87377** and strings are less than blobs.
87378*/
87379case OP_Compare: {
87380  int n;
87381  int i;
87382  int p1;
87383  int p2;
87384  const KeyInfo *pKeyInfo;
87385  int idx;
87386  CollSeq *pColl;    /* Collating sequence to use on this term */
87387  int bRev;          /* True for DESCENDING sort order */
87388  int *aPermute;     /* The permutation */
87389
87390  if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
87391    aPermute = 0;
87392  }else{
87393    assert( pOp>aOp );
87394    assert( pOp[-1].opcode==OP_Permutation );
87395    assert( pOp[-1].p4type==P4_INTARRAY );
87396    aPermute = pOp[-1].p4.ai + 1;
87397    assert( aPermute!=0 );
87398  }
87399  n = pOp->p3;
87400  pKeyInfo = pOp->p4.pKeyInfo;
87401  assert( n>0 );
87402  assert( pKeyInfo!=0 );
87403  p1 = pOp->p1;
87404  p2 = pOp->p2;
87405#ifdef SQLITE_DEBUG
87406  if( aPermute ){
87407    int k, mx = 0;
87408    for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
87409    assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
87410    assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
87411  }else{
87412    assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
87413    assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
87414  }
87415#endif /* SQLITE_DEBUG */
87416  for(i=0; i<n; i++){
87417    idx = aPermute ? aPermute[i] : i;
87418    assert( memIsValid(&aMem[p1+idx]) );
87419    assert( memIsValid(&aMem[p2+idx]) );
87420    REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
87421    REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
87422    assert( i<pKeyInfo->nKeyField );
87423    pColl = pKeyInfo->aColl[i];
87424    bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
87425    iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
87426    if( iCompare ){
87427      if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
87428       && ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null))
87429      ){
87430        iCompare = -iCompare;
87431      }
87432      if( bRev ) iCompare = -iCompare;
87433      break;
87434    }
87435  }
87436  break;
87437}
87438
87439/* Opcode: Jump P1 P2 P3 * *
87440**
87441** Jump to the instruction at address P1, P2, or P3 depending on whether
87442** in the most recent OP_Compare instruction the P1 vector was less than
87443** equal to, or greater than the P2 vector, respectively.
87444*/
87445case OP_Jump: {             /* jump */
87446  if( iCompare<0 ){
87447    VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
87448  }else if( iCompare==0 ){
87449    VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
87450  }else{
87451    VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
87452  }
87453  break;
87454}
87455
87456/* Opcode: And P1 P2 P3 * *
87457** Synopsis: r[P3]=(r[P1] && r[P2])
87458**
87459** Take the logical AND of the values in registers P1 and P2 and
87460** write the result into register P3.
87461**
87462** If either P1 or P2 is 0 (false) then the result is 0 even if
87463** the other input is NULL.  A NULL and true or two NULLs give
87464** a NULL output.
87465*/
87466/* Opcode: Or P1 P2 P3 * *
87467** Synopsis: r[P3]=(r[P1] || r[P2])
87468**
87469** Take the logical OR of the values in register P1 and P2 and
87470** store the answer in register P3.
87471**
87472** If either P1 or P2 is nonzero (true) then the result is 1 (true)
87473** even if the other input is NULL.  A NULL and false or two NULLs
87474** give a NULL output.
87475*/
87476case OP_And:              /* same as TK_AND, in1, in2, out3 */
87477case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
87478  int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
87479  int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
87480
87481  v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
87482  v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
87483  if( pOp->opcode==OP_And ){
87484    static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
87485    v1 = and_logic[v1*3+v2];
87486  }else{
87487    static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
87488    v1 = or_logic[v1*3+v2];
87489  }
87490  pOut = &aMem[pOp->p3];
87491  if( v1==2 ){
87492    MemSetTypeFlag(pOut, MEM_Null);
87493  }else{
87494    pOut->u.i = v1;
87495    MemSetTypeFlag(pOut, MEM_Int);
87496  }
87497  break;
87498}
87499
87500/* Opcode: IsTrue P1 P2 P3 P4 *
87501** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
87502**
87503** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
87504** IS NOT FALSE operators.
87505**
87506** Interpret the value in register P1 as a boolean value.  Store that
87507** boolean (a 0 or 1) in register P2.  Or if the value in register P1 is
87508** NULL, then the P3 is stored in register P2.  Invert the answer if P4
87509** is 1.
87510**
87511** The logic is summarized like this:
87512**
87513** <ul>
87514** <li> If P3==0 and P4==0  then  r[P2] := r[P1] IS TRUE
87515** <li> If P3==1 and P4==1  then  r[P2] := r[P1] IS FALSE
87516** <li> If P3==0 and P4==1  then  r[P2] := r[P1] IS NOT TRUE
87517** <li> If P3==1 and P4==0  then  r[P2] := r[P1] IS NOT FALSE
87518** </ul>
87519*/
87520case OP_IsTrue: {               /* in1, out2 */
87521  assert( pOp->p4type==P4_INT32 );
87522  assert( pOp->p4.i==0 || pOp->p4.i==1 );
87523  assert( pOp->p3==0 || pOp->p3==1 );
87524  sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
87525      sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
87526  break;
87527}
87528
87529/* Opcode: Not P1 P2 * * *
87530** Synopsis: r[P2]= !r[P1]
87531**
87532** Interpret the value in register P1 as a boolean value.  Store the
87533** boolean complement in register P2.  If the value in register P1 is
87534** NULL, then a NULL is stored in P2.
87535*/
87536case OP_Not: {                /* same as TK_NOT, in1, out2 */
87537  pIn1 = &aMem[pOp->p1];
87538  pOut = &aMem[pOp->p2];
87539  if( (pIn1->flags & MEM_Null)==0 ){
87540    sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
87541  }else{
87542    sqlite3VdbeMemSetNull(pOut);
87543  }
87544  break;
87545}
87546
87547/* Opcode: BitNot P1 P2 * * *
87548** Synopsis: r[P2]= ~r[P1]
87549**
87550** Interpret the content of register P1 as an integer.  Store the
87551** ones-complement of the P1 value into register P2.  If P1 holds
87552** a NULL then store a NULL in P2.
87553*/
87554case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
87555  pIn1 = &aMem[pOp->p1];
87556  pOut = &aMem[pOp->p2];
87557  sqlite3VdbeMemSetNull(pOut);
87558  if( (pIn1->flags & MEM_Null)==0 ){
87559    pOut->flags = MEM_Int;
87560    pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
87561  }
87562  break;
87563}
87564
87565/* Opcode: Once P1 P2 * * *
87566**
87567** Fall through to the next instruction the first time this opcode is
87568** encountered on each invocation of the byte-code program.  Jump to P2
87569** on the second and all subsequent encounters during the same invocation.
87570**
87571** Top-level programs determine first invocation by comparing the P1
87572** operand against the P1 operand on the OP_Init opcode at the beginning
87573** of the program.  If the P1 values differ, then fall through and make
87574** the P1 of this opcode equal to the P1 of OP_Init.  If P1 values are
87575** the same then take the jump.
87576**
87577** For subprograms, there is a bitmask in the VdbeFrame that determines
87578** whether or not the jump should be taken.  The bitmask is necessary
87579** because the self-altering code trick does not work for recursive
87580** triggers.
87581*/
87582case OP_Once: {             /* jump */
87583  u32 iAddr;                /* Address of this instruction */
87584  assert( p->aOp[0].opcode==OP_Init );
87585  if( p->pFrame ){
87586    iAddr = (int)(pOp - p->aOp);
87587    if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
87588      VdbeBranchTaken(1, 2);
87589      goto jump_to_p2;
87590    }
87591    p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
87592  }else{
87593    if( p->aOp[0].p1==pOp->p1 ){
87594      VdbeBranchTaken(1, 2);
87595      goto jump_to_p2;
87596    }
87597  }
87598  VdbeBranchTaken(0, 2);
87599  pOp->p1 = p->aOp[0].p1;
87600  break;
87601}
87602
87603/* Opcode: If P1 P2 P3 * *
87604**
87605** Jump to P2 if the value in register P1 is true.  The value
87606** is considered true if it is numeric and non-zero.  If the value
87607** in P1 is NULL then take the jump if and only if P3 is non-zero.
87608*/
87609case OP_If:  {               /* jump, in1 */
87610  int c;
87611  c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
87612  VdbeBranchTaken(c!=0, 2);
87613  if( c ) goto jump_to_p2;
87614  break;
87615}
87616
87617/* Opcode: IfNot P1 P2 P3 * *
87618**
87619** Jump to P2 if the value in register P1 is False.  The value
87620** is considered false if it has a numeric value of zero.  If the value
87621** in P1 is NULL then take the jump if and only if P3 is non-zero.
87622*/
87623case OP_IfNot: {            /* jump, in1 */
87624  int c;
87625  c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
87626  VdbeBranchTaken(c!=0, 2);
87627  if( c ) goto jump_to_p2;
87628  break;
87629}
87630
87631/* Opcode: IsNull P1 P2 * * *
87632** Synopsis: if r[P1]==NULL goto P2
87633**
87634** Jump to P2 if the value in register P1 is NULL.
87635*/
87636case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
87637  pIn1 = &aMem[pOp->p1];
87638  VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
87639  if( (pIn1->flags & MEM_Null)!=0 ){
87640    goto jump_to_p2;
87641  }
87642  break;
87643}
87644
87645/* Opcode: NotNull P1 P2 * * *
87646** Synopsis: if r[P1]!=NULL goto P2
87647**
87648** Jump to P2 if the value in register P1 is not NULL.
87649*/
87650case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
87651  pIn1 = &aMem[pOp->p1];
87652  VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
87653  if( (pIn1->flags & MEM_Null)==0 ){
87654    goto jump_to_p2;
87655  }
87656  break;
87657}
87658
87659/* Opcode: IfNullRow P1 P2 P3 * *
87660** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
87661**
87662** Check the cursor P1 to see if it is currently pointing at a NULL row.
87663** If it is, then set register P3 to NULL and jump immediately to P2.
87664** If P1 is not on a NULL row, then fall through without making any
87665** changes.
87666*/
87667case OP_IfNullRow: {         /* jump */
87668  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87669  assert( p->apCsr[pOp->p1]!=0 );
87670  if( p->apCsr[pOp->p1]->nullRow ){
87671    sqlite3VdbeMemSetNull(aMem + pOp->p3);
87672    goto jump_to_p2;
87673  }
87674  break;
87675}
87676
87677#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
87678/* Opcode: Offset P1 P2 P3 * *
87679** Synopsis: r[P3] = sqlite_offset(P1)
87680**
87681** Store in register r[P3] the byte offset into the database file that is the
87682** start of the payload for the record at which that cursor P1 is currently
87683** pointing.
87684**
87685** P2 is the column number for the argument to the sqlite_offset() function.
87686** This opcode does not use P2 itself, but the P2 value is used by the
87687** code generator.  The P1, P2, and P3 operands to this opcode are the
87688** same as for OP_Column.
87689**
87690** This opcode is only available if SQLite is compiled with the
87691** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
87692*/
87693case OP_Offset: {          /* out3 */
87694  VdbeCursor *pC;    /* The VDBE cursor */
87695  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87696  pC = p->apCsr[pOp->p1];
87697  pOut = &p->aMem[pOp->p3];
87698  if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
87699    sqlite3VdbeMemSetNull(pOut);
87700  }else{
87701    sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
87702  }
87703  break;
87704}
87705#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
87706
87707/* Opcode: Column P1 P2 P3 P4 P5
87708** Synopsis: r[P3]=PX
87709**
87710** Interpret the data that cursor P1 points to as a structure built using
87711** the MakeRecord instruction.  (See the MakeRecord opcode for additional
87712** information about the format of the data.)  Extract the P2-th column
87713** from this record.  If there are less that (P2+1)
87714** values in the record, extract a NULL.
87715**
87716** The value extracted is stored in register P3.
87717**
87718** If the record contains fewer than P2 fields, then extract a NULL.  Or,
87719** if the P4 argument is a P4_MEM use the value of the P4 argument as
87720** the result.
87721**
87722** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
87723** the result is guaranteed to only be used as the argument of a length()
87724** or typeof() function, respectively.  The loading of large blobs can be
87725** skipped for length() and all content loading can be skipped for typeof().
87726*/
87727case OP_Column: {
87728  int p2;            /* column number to retrieve */
87729  VdbeCursor *pC;    /* The VDBE cursor */
87730  BtCursor *pCrsr;   /* The BTree cursor */
87731  u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
87732  int len;           /* The length of the serialized data for the column */
87733  int i;             /* Loop counter */
87734  Mem *pDest;        /* Where to write the extracted value */
87735  Mem sMem;          /* For storing the record being decoded */
87736  const u8 *zData;   /* Part of the record being decoded */
87737  const u8 *zHdr;    /* Next unparsed byte of the header */
87738  const u8 *zEndHdr; /* Pointer to first byte after the header */
87739  u64 offset64;      /* 64-bit offset */
87740  u32 t;             /* A type code from the record header */
87741  Mem *pReg;         /* PseudoTable input register */
87742
87743  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87744  pC = p->apCsr[pOp->p1];
87745  assert( pC!=0 );
87746  p2 = pOp->p2;
87747
87748  /* If the cursor cache is stale (meaning it is not currently point at
87749  ** the correct row) then bring it up-to-date by doing the necessary
87750  ** B-Tree seek. */
87751  rc = sqlite3VdbeCursorMoveto(&pC, &p2);
87752  if( rc ) goto abort_due_to_error;
87753
87754  assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
87755  pDest = &aMem[pOp->p3];
87756  memAboutToChange(p, pDest);
87757  assert( pC!=0 );
87758  assert( p2<pC->nField );
87759  aOffset = pC->aOffset;
87760  assert( pC->eCurType!=CURTYPE_VTAB );
87761  assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
87762  assert( pC->eCurType!=CURTYPE_SORTER );
87763
87764  if( pC->cacheStatus!=p->cacheCtr ){                /*OPTIMIZATION-IF-FALSE*/
87765    if( pC->nullRow ){
87766      if( pC->eCurType==CURTYPE_PSEUDO ){
87767        /* For the special case of as pseudo-cursor, the seekResult field
87768        ** identifies the register that holds the record */
87769        assert( pC->seekResult>0 );
87770        pReg = &aMem[pC->seekResult];
87771        assert( pReg->flags & MEM_Blob );
87772        assert( memIsValid(pReg) );
87773        pC->payloadSize = pC->szRow = pReg->n;
87774        pC->aRow = (u8*)pReg->z;
87775      }else{
87776        sqlite3VdbeMemSetNull(pDest);
87777        goto op_column_out;
87778      }
87779    }else{
87780      pCrsr = pC->uc.pCursor;
87781      assert( pC->eCurType==CURTYPE_BTREE );
87782      assert( pCrsr );
87783      assert( sqlite3BtreeCursorIsValid(pCrsr) );
87784      pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
87785      pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
87786      assert( pC->szRow<=pC->payloadSize );
87787      assert( pC->szRow<=65536 );  /* Maximum page size is 64KiB */
87788      if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
87789        goto too_big;
87790      }
87791    }
87792    pC->cacheStatus = p->cacheCtr;
87793    pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
87794    pC->nHdrParsed = 0;
87795
87796
87797    if( pC->szRow<aOffset[0] ){      /*OPTIMIZATION-IF-FALSE*/
87798      /* pC->aRow does not have to hold the entire row, but it does at least
87799      ** need to cover the header of the record.  If pC->aRow does not contain
87800      ** the complete header, then set it to zero, forcing the header to be
87801      ** dynamically allocated. */
87802      pC->aRow = 0;
87803      pC->szRow = 0;
87804
87805      /* Make sure a corrupt database has not given us an oversize header.
87806      ** Do this now to avoid an oversize memory allocation.
87807      **
87808      ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
87809      ** types use so much data space that there can only be 4096 and 32 of
87810      ** them, respectively.  So the maximum header length results from a
87811      ** 3-byte type for each of the maximum of 32768 columns plus three
87812      ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
87813      */
87814      if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
87815        goto op_column_corrupt;
87816      }
87817    }else{
87818      /* This is an optimization.  By skipping over the first few tests
87819      ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
87820      ** measurable performance gain.
87821      **
87822      ** This branch is taken even if aOffset[0]==0.  Such a record is never
87823      ** generated by SQLite, and could be considered corruption, but we
87824      ** accept it for historical reasons.  When aOffset[0]==0, the code this
87825      ** branch jumps to reads past the end of the record, but never more
87826      ** than a few bytes.  Even if the record occurs at the end of the page
87827      ** content area, the "page header" comes after the page content and so
87828      ** this overread is harmless.  Similar overreads can occur for a corrupt
87829      ** database file.
87830      */
87831      zData = pC->aRow;
87832      assert( pC->nHdrParsed<=p2 );         /* Conditional skipped */
87833      testcase( aOffset[0]==0 );
87834      goto op_column_read_header;
87835    }
87836  }
87837
87838  /* Make sure at least the first p2+1 entries of the header have been
87839  ** parsed and valid information is in aOffset[] and pC->aType[].
87840  */
87841  if( pC->nHdrParsed<=p2 ){
87842    /* If there is more header available for parsing in the record, try
87843    ** to extract additional fields up through the p2+1-th field
87844    */
87845    if( pC->iHdrOffset<aOffset[0] ){
87846      /* Make sure zData points to enough of the record to cover the header. */
87847      if( pC->aRow==0 ){
87848        memset(&sMem, 0, sizeof(sMem));
87849        rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
87850        if( rc!=SQLITE_OK ) goto abort_due_to_error;
87851        zData = (u8*)sMem.z;
87852      }else{
87853        zData = pC->aRow;
87854      }
87855
87856      /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
87857    op_column_read_header:
87858      i = pC->nHdrParsed;
87859      offset64 = aOffset[i];
87860      zHdr = zData + pC->iHdrOffset;
87861      zEndHdr = zData + aOffset[0];
87862      testcase( zHdr>=zEndHdr );
87863      do{
87864        if( (pC->aType[i] = t = zHdr[0])<0x80 ){
87865          zHdr++;
87866          offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
87867        }else{
87868          zHdr += sqlite3GetVarint32(zHdr, &t);
87869          pC->aType[i] = t;
87870          offset64 += sqlite3VdbeSerialTypeLen(t);
87871        }
87872        aOffset[++i] = (u32)(offset64 & 0xffffffff);
87873      }while( i<=p2 && zHdr<zEndHdr );
87874
87875      /* The record is corrupt if any of the following are true:
87876      ** (1) the bytes of the header extend past the declared header size
87877      ** (2) the entire header was used but not all data was used
87878      ** (3) the end of the data extends beyond the end of the record.
87879      */
87880      if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
87881       || (offset64 > pC->payloadSize)
87882      ){
87883        if( aOffset[0]==0 ){
87884          i = 0;
87885          zHdr = zEndHdr;
87886        }else{
87887          if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
87888          goto op_column_corrupt;
87889        }
87890      }
87891
87892      pC->nHdrParsed = i;
87893      pC->iHdrOffset = (u32)(zHdr - zData);
87894      if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
87895    }else{
87896      t = 0;
87897    }
87898
87899    /* If after trying to extract new entries from the header, nHdrParsed is
87900    ** still not up to p2, that means that the record has fewer than p2
87901    ** columns.  So the result will be either the default value or a NULL.
87902    */
87903    if( pC->nHdrParsed<=p2 ){
87904      if( pOp->p4type==P4_MEM ){
87905        sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
87906      }else{
87907        sqlite3VdbeMemSetNull(pDest);
87908      }
87909      goto op_column_out;
87910    }
87911  }else{
87912    t = pC->aType[p2];
87913  }
87914
87915  /* Extract the content for the p2+1-th column.  Control can only
87916  ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
87917  ** all valid.
87918  */
87919  assert( p2<pC->nHdrParsed );
87920  assert( rc==SQLITE_OK );
87921  assert( sqlite3VdbeCheckMemInvariants(pDest) );
87922  if( VdbeMemDynamic(pDest) ){
87923    sqlite3VdbeMemSetNull(pDest);
87924  }
87925  assert( t==pC->aType[p2] );
87926  if( pC->szRow>=aOffset[p2+1] ){
87927    /* This is the common case where the desired content fits on the original
87928    ** page - where the content is not on an overflow page */
87929    zData = pC->aRow + aOffset[p2];
87930    if( t<12 ){
87931      sqlite3VdbeSerialGet(zData, t, pDest);
87932    }else{
87933      /* If the column value is a string, we need a persistent value, not
87934      ** a MEM_Ephem value.  This branch is a fast short-cut that is equivalent
87935      ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
87936      */
87937      static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
87938      pDest->n = len = (t-12)/2;
87939      pDest->enc = encoding;
87940      if( pDest->szMalloc < len+2 ){
87941        pDest->flags = MEM_Null;
87942        if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
87943      }else{
87944        pDest->z = pDest->zMalloc;
87945      }
87946      memcpy(pDest->z, zData, len);
87947      pDest->z[len] = 0;
87948      pDest->z[len+1] = 0;
87949      pDest->flags = aFlag[t&1];
87950    }
87951  }else{
87952    pDest->enc = encoding;
87953    /* This branch happens only when content is on overflow pages */
87954    if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
87955          && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
87956     || (len = sqlite3VdbeSerialTypeLen(t))==0
87957    ){
87958      /* Content is irrelevant for
87959      **    1. the typeof() function,
87960      **    2. the length(X) function if X is a blob, and
87961      **    3. if the content length is zero.
87962      ** So we might as well use bogus content rather than reading
87963      ** content from disk.
87964      **
87965      ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
87966      ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
87967      ** read more.  Use the global constant sqlite3CtypeMap[] as the array,
87968      ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint())
87969      ** and it begins with a bunch of zeros.
87970      */
87971      sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest);
87972    }else{
87973      rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
87974      if( rc!=SQLITE_OK ) goto abort_due_to_error;
87975      sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
87976      pDest->flags &= ~MEM_Ephem;
87977    }
87978  }
87979
87980op_column_out:
87981  UPDATE_MAX_BLOBSIZE(pDest);
87982  REGISTER_TRACE(pOp->p3, pDest);
87983  break;
87984
87985op_column_corrupt:
87986  if( aOp[0].p3>0 ){
87987    pOp = &aOp[aOp[0].p3-1];
87988    break;
87989  }else{
87990    rc = SQLITE_CORRUPT_BKPT;
87991    goto abort_due_to_error;
87992  }
87993}
87994
87995/* Opcode: Affinity P1 P2 * P4 *
87996** Synopsis: affinity(r[P1@P2])
87997**
87998** Apply affinities to a range of P2 registers starting with P1.
87999**
88000** P4 is a string that is P2 characters long. The N-th character of the
88001** string indicates the column affinity that should be used for the N-th
88002** memory cell in the range.
88003*/
88004case OP_Affinity: {
88005  const char *zAffinity;   /* The affinity to be applied */
88006
88007  zAffinity = pOp->p4.z;
88008  assert( zAffinity!=0 );
88009  assert( pOp->p2>0 );
88010  assert( zAffinity[pOp->p2]==0 );
88011  pIn1 = &aMem[pOp->p1];
88012  while( 1 /*exit-by-break*/ ){
88013    assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
88014    assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) );
88015    applyAffinity(pIn1, zAffinity[0], encoding);
88016    if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
88017      /* When applying REAL affinity, if the result is still an MEM_Int
88018      ** that will fit in 6 bytes, then change the type to MEM_IntReal
88019      ** so that we keep the high-resolution integer value but know that
88020      ** the type really wants to be REAL. */
88021      testcase( pIn1->u.i==140737488355328LL );
88022      testcase( pIn1->u.i==140737488355327LL );
88023      testcase( pIn1->u.i==-140737488355328LL );
88024      testcase( pIn1->u.i==-140737488355329LL );
88025      if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
88026        pIn1->flags |= MEM_IntReal;
88027        pIn1->flags &= ~MEM_Int;
88028      }else{
88029        pIn1->u.r = (double)pIn1->u.i;
88030        pIn1->flags |= MEM_Real;
88031        pIn1->flags &= ~MEM_Int;
88032      }
88033    }
88034    REGISTER_TRACE((int)(pIn1-aMem), pIn1);
88035    zAffinity++;
88036    if( zAffinity[0]==0 ) break;
88037    pIn1++;
88038  }
88039  break;
88040}
88041
88042/* Opcode: MakeRecord P1 P2 P3 P4 *
88043** Synopsis: r[P3]=mkrec(r[P1@P2])
88044**
88045** Convert P2 registers beginning with P1 into the [record format]
88046** use as a data record in a database table or as a key
88047** in an index.  The OP_Column opcode can decode the record later.
88048**
88049** P4 may be a string that is P2 characters long.  The N-th character of the
88050** string indicates the column affinity that should be used for the N-th
88051** field of the index key.
88052**
88053** The mapping from character to affinity is given by the SQLITE_AFF_
88054** macros defined in sqliteInt.h.
88055**
88056** If P4 is NULL then all index fields have the affinity BLOB.
88057*/
88058case OP_MakeRecord: {
88059  Mem *pRec;             /* The new record */
88060  u64 nData;             /* Number of bytes of data space */
88061  int nHdr;              /* Number of bytes of header space */
88062  i64 nByte;             /* Data space required for this record */
88063  i64 nZero;             /* Number of zero bytes at the end of the record */
88064  int nVarint;           /* Number of bytes in a varint */
88065  u32 serial_type;       /* Type field */
88066  Mem *pData0;           /* First field to be combined into the record */
88067  Mem *pLast;            /* Last field of the record */
88068  int nField;            /* Number of fields in the record */
88069  char *zAffinity;       /* The affinity string for the record */
88070  int file_format;       /* File format to use for encoding */
88071  u32 len;               /* Length of a field */
88072  u8 *zHdr;              /* Where to write next byte of the header */
88073  u8 *zPayload;          /* Where to write next byte of the payload */
88074
88075  /* Assuming the record contains N fields, the record format looks
88076  ** like this:
88077  **
88078  ** ------------------------------------------------------------------------
88079  ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
88080  ** ------------------------------------------------------------------------
88081  **
88082  ** Data(0) is taken from register P1.  Data(1) comes from register P1+1
88083  ** and so forth.
88084  **
88085  ** Each type field is a varint representing the serial type of the
88086  ** corresponding data element (see sqlite3VdbeSerialType()). The
88087  ** hdr-size field is also a varint which is the offset from the beginning
88088  ** of the record to data0.
88089  */
88090  nData = 0;         /* Number of bytes of data space */
88091  nHdr = 0;          /* Number of bytes of header space */
88092  nZero = 0;         /* Number of zero bytes at the end of the record */
88093  nField = pOp->p1;
88094  zAffinity = pOp->p4.z;
88095  assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
88096  pData0 = &aMem[nField];
88097  nField = pOp->p2;
88098  pLast = &pData0[nField-1];
88099  file_format = p->minWriteFileFormat;
88100
88101  /* Identify the output register */
88102  assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
88103  pOut = &aMem[pOp->p3];
88104  memAboutToChange(p, pOut);
88105
88106  /* Apply the requested affinity to all inputs
88107  */
88108  assert( pData0<=pLast );
88109  if( zAffinity ){
88110    pRec = pData0;
88111    do{
88112      applyAffinity(pRec, zAffinity[0], encoding);
88113      if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
88114        pRec->flags |= MEM_IntReal;
88115        pRec->flags &= ~(MEM_Int);
88116      }
88117      REGISTER_TRACE((int)(pRec-aMem), pRec);
88118      zAffinity++;
88119      pRec++;
88120      assert( zAffinity[0]==0 || pRec<=pLast );
88121    }while( zAffinity[0] );
88122  }
88123
88124#ifdef SQLITE_ENABLE_NULL_TRIM
88125  /* NULLs can be safely trimmed from the end of the record, as long as
88126  ** as the schema format is 2 or more and none of the omitted columns
88127  ** have a non-NULL default value.  Also, the record must be left with
88128  ** at least one field.  If P5>0 then it will be one more than the
88129  ** index of the right-most column with a non-NULL default value */
88130  if( pOp->p5 ){
88131    while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
88132      pLast--;
88133      nField--;
88134    }
88135  }
88136#endif
88137
88138  /* Loop through the elements that will make up the record to figure
88139  ** out how much space is required for the new record.  After this loop,
88140  ** the Mem.uTemp field of each term should hold the serial-type that will
88141  ** be used for that term in the generated record:
88142  **
88143  **   Mem.uTemp value    type
88144  **   ---------------    ---------------
88145  **      0               NULL
88146  **      1               1-byte signed integer
88147  **      2               2-byte signed integer
88148  **      3               3-byte signed integer
88149  **      4               4-byte signed integer
88150  **      5               6-byte signed integer
88151  **      6               8-byte signed integer
88152  **      7               IEEE float
88153  **      8               Integer constant 0
88154  **      9               Integer constant 1
88155  **     10,11            reserved for expansion
88156  **    N>=12 and even    BLOB
88157  **    N>=13 and odd     text
88158  **
88159  ** The following additional values are computed:
88160  **     nHdr        Number of bytes needed for the record header
88161  **     nData       Number of bytes of data space needed for the record
88162  **     nZero       Zero bytes at the end of the record
88163  */
88164  pRec = pLast;
88165  do{
88166    assert( memIsValid(pRec) );
88167    if( pRec->flags & MEM_Null ){
88168      if( pRec->flags & MEM_Zero ){
88169        /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
88170        ** table methods that never invoke sqlite3_result_xxxxx() while
88171        ** computing an unchanging column value in an UPDATE statement.
88172        ** Give such values a special internal-use-only serial-type of 10
88173        ** so that they can be passed through to xUpdate and have
88174        ** a true sqlite3_value_nochange(). */
88175        assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
88176        pRec->uTemp = 10;
88177      }else{
88178        pRec->uTemp = 0;
88179      }
88180      nHdr++;
88181    }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
88182      /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
88183      i64 i = pRec->u.i;
88184      u64 uu;
88185      testcase( pRec->flags & MEM_Int );
88186      testcase( pRec->flags & MEM_IntReal );
88187      if( i<0 ){
88188        uu = ~i;
88189      }else{
88190        uu = i;
88191      }
88192      nHdr++;
88193      testcase( uu==127 );               testcase( uu==128 );
88194      testcase( uu==32767 );             testcase( uu==32768 );
88195      testcase( uu==8388607 );           testcase( uu==8388608 );
88196      testcase( uu==2147483647 );        testcase( uu==2147483648 );
88197      testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL );
88198      if( uu<=127 ){
88199        if( (i&1)==i && file_format>=4 ){
88200          pRec->uTemp = 8+(u32)uu;
88201        }else{
88202          nData++;
88203          pRec->uTemp = 1;
88204        }
88205      }else if( uu<=32767 ){
88206        nData += 2;
88207        pRec->uTemp = 2;
88208      }else if( uu<=8388607 ){
88209        nData += 3;
88210        pRec->uTemp = 3;
88211      }else if( uu<=2147483647 ){
88212        nData += 4;
88213        pRec->uTemp = 4;
88214      }else if( uu<=140737488355327LL ){
88215        nData += 6;
88216        pRec->uTemp = 5;
88217      }else{
88218        nData += 8;
88219        if( pRec->flags & MEM_IntReal ){
88220          /* If the value is IntReal and is going to take up 8 bytes to store
88221          ** as an integer, then we might as well make it an 8-byte floating
88222          ** point value */
88223          pRec->u.r = (double)pRec->u.i;
88224          pRec->flags &= ~MEM_IntReal;
88225          pRec->flags |= MEM_Real;
88226          pRec->uTemp = 7;
88227        }else{
88228          pRec->uTemp = 6;
88229        }
88230      }
88231    }else if( pRec->flags & MEM_Real ){
88232      nHdr++;
88233      nData += 8;
88234      pRec->uTemp = 7;
88235    }else{
88236      assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
88237      assert( pRec->n>=0 );
88238      len = (u32)pRec->n;
88239      serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
88240      if( pRec->flags & MEM_Zero ){
88241        serial_type += pRec->u.nZero*2;
88242        if( nData ){
88243          if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
88244          len += pRec->u.nZero;
88245        }else{
88246          nZero += pRec->u.nZero;
88247        }
88248      }
88249      nData += len;
88250      nHdr += sqlite3VarintLen(serial_type);
88251      pRec->uTemp = serial_type;
88252    }
88253    if( pRec==pData0 ) break;
88254    pRec--;
88255  }while(1);
88256
88257  /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
88258  ** which determines the total number of bytes in the header. The varint
88259  ** value is the size of the header in bytes including the size varint
88260  ** itself. */
88261  testcase( nHdr==126 );
88262  testcase( nHdr==127 );
88263  if( nHdr<=126 ){
88264    /* The common case */
88265    nHdr += 1;
88266  }else{
88267    /* Rare case of a really large header */
88268    nVarint = sqlite3VarintLen(nHdr);
88269    nHdr += nVarint;
88270    if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
88271  }
88272  nByte = nHdr+nData;
88273
88274  /* Make sure the output register has a buffer large enough to store
88275  ** the new record. The output register (pOp->p3) is not allowed to
88276  ** be one of the input registers (because the following call to
88277  ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
88278  */
88279  if( nByte+nZero<=pOut->szMalloc ){
88280    /* The output register is already large enough to hold the record.
88281    ** No error checks or buffer enlargement is required */
88282    pOut->z = pOut->zMalloc;
88283  }else{
88284    /* Need to make sure that the output is not too big and then enlarge
88285    ** the output register to hold the full result */
88286    if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
88287      goto too_big;
88288    }
88289    if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
88290      goto no_mem;
88291    }
88292  }
88293  pOut->n = (int)nByte;
88294  pOut->flags = MEM_Blob;
88295  if( nZero ){
88296    pOut->u.nZero = nZero;
88297    pOut->flags |= MEM_Zero;
88298  }
88299  UPDATE_MAX_BLOBSIZE(pOut);
88300  zHdr = (u8 *)pOut->z;
88301  zPayload = zHdr + nHdr;
88302
88303  /* Write the record */
88304  zHdr += putVarint32(zHdr, nHdr);
88305  assert( pData0<=pLast );
88306  pRec = pData0;
88307  do{
88308    serial_type = pRec->uTemp;
88309    /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
88310    ** additional varints, one per column. */
88311    zHdr += putVarint32(zHdr, serial_type);            /* serial type */
88312    /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
88313    ** immediately follow the header. */
88314    zPayload += sqlite3VdbeSerialPut(zPayload, pRec, serial_type); /* content */
88315  }while( (++pRec)<=pLast );
88316  assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
88317  assert( nByte==(int)(zPayload - (u8*)pOut->z) );
88318
88319  assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
88320  REGISTER_TRACE(pOp->p3, pOut);
88321  break;
88322}
88323
88324/* Opcode: Count P1 P2 p3 * *
88325** Synopsis: r[P2]=count()
88326**
88327** Store the number of entries (an integer value) in the table or index
88328** opened by cursor P1 in register P2.
88329**
88330** If P3==0, then an exact count is obtained, which involves visiting
88331** every btree page of the table.  But if P3 is non-zero, an estimate
88332** is returned based on the current cursor position.
88333*/
88334case OP_Count: {         /* out2 */
88335  i64 nEntry;
88336  BtCursor *pCrsr;
88337
88338  assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
88339  pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
88340  assert( pCrsr );
88341  if( pOp->p3 ){
88342    nEntry = sqlite3BtreeRowCountEst(pCrsr);
88343  }else{
88344    nEntry = 0;  /* Not needed.  Only used to silence a warning. */
88345    rc = sqlite3BtreeCount(db, pCrsr, &nEntry);
88346    if( rc ) goto abort_due_to_error;
88347  }
88348  pOut = out2Prerelease(p, pOp);
88349  pOut->u.i = nEntry;
88350  goto check_for_interrupt;
88351}
88352
88353/* Opcode: Savepoint P1 * * P4 *
88354**
88355** Open, release or rollback the savepoint named by parameter P4, depending
88356** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
88357** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
88358** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
88359*/
88360case OP_Savepoint: {
88361  int p1;                         /* Value of P1 operand */
88362  char *zName;                    /* Name of savepoint */
88363  int nName;
88364  Savepoint *pNew;
88365  Savepoint *pSavepoint;
88366  Savepoint *pTmp;
88367  int iSavepoint;
88368  int ii;
88369
88370  p1 = pOp->p1;
88371  zName = pOp->p4.z;
88372
88373  /* Assert that the p1 parameter is valid. Also that if there is no open
88374  ** transaction, then there cannot be any savepoints.
88375  */
88376  assert( db->pSavepoint==0 || db->autoCommit==0 );
88377  assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
88378  assert( db->pSavepoint || db->isTransactionSavepoint==0 );
88379  assert( checkSavepointCount(db) );
88380  assert( p->bIsReader );
88381
88382  if( p1==SAVEPOINT_BEGIN ){
88383    if( db->nVdbeWrite>0 ){
88384      /* A new savepoint cannot be created if there are active write
88385      ** statements (i.e. open read/write incremental blob handles).
88386      */
88387      sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
88388      rc = SQLITE_BUSY;
88389    }else{
88390      nName = sqlite3Strlen30(zName);
88391
88392#ifndef SQLITE_OMIT_VIRTUALTABLE
88393      /* This call is Ok even if this savepoint is actually a transaction
88394      ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
88395      ** If this is a transaction savepoint being opened, it is guaranteed
88396      ** that the db->aVTrans[] array is empty.  */
88397      assert( db->autoCommit==0 || db->nVTrans==0 );
88398      rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
88399                                db->nStatement+db->nSavepoint);
88400      if( rc!=SQLITE_OK ) goto abort_due_to_error;
88401#endif
88402
88403      /* Create a new savepoint structure. */
88404      pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
88405      if( pNew ){
88406        pNew->zName = (char *)&pNew[1];
88407        memcpy(pNew->zName, zName, nName+1);
88408
88409        /* If there is no open transaction, then mark this as a special
88410        ** "transaction savepoint". */
88411        if( db->autoCommit ){
88412          db->autoCommit = 0;
88413          db->isTransactionSavepoint = 1;
88414        }else{
88415          db->nSavepoint++;
88416        }
88417
88418        /* Link the new savepoint into the database handle's list. */
88419        pNew->pNext = db->pSavepoint;
88420        db->pSavepoint = pNew;
88421        pNew->nDeferredCons = db->nDeferredCons;
88422        pNew->nDeferredImmCons = db->nDeferredImmCons;
88423      }
88424    }
88425  }else{
88426    assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK );
88427    iSavepoint = 0;
88428
88429    /* Find the named savepoint. If there is no such savepoint, then an
88430    ** an error is returned to the user.  */
88431    for(
88432      pSavepoint = db->pSavepoint;
88433      pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
88434      pSavepoint = pSavepoint->pNext
88435    ){
88436      iSavepoint++;
88437    }
88438    if( !pSavepoint ){
88439      sqlite3VdbeError(p, "no such savepoint: %s", zName);
88440      rc = SQLITE_ERROR;
88441    }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
88442      /* It is not possible to release (commit) a savepoint if there are
88443      ** active write statements.
88444      */
88445      sqlite3VdbeError(p, "cannot release savepoint - "
88446                          "SQL statements in progress");
88447      rc = SQLITE_BUSY;
88448    }else{
88449
88450      /* Determine whether or not this is a transaction savepoint. If so,
88451      ** and this is a RELEASE command, then the current transaction
88452      ** is committed.
88453      */
88454      int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
88455      if( isTransaction && p1==SAVEPOINT_RELEASE ){
88456        if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
88457          goto vdbe_return;
88458        }
88459        db->autoCommit = 1;
88460        if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
88461          p->pc = (int)(pOp - aOp);
88462          db->autoCommit = 0;
88463          p->rc = rc = SQLITE_BUSY;
88464          goto vdbe_return;
88465        }
88466        rc = p->rc;
88467        if( rc ){
88468          db->autoCommit = 0;
88469        }else{
88470          db->isTransactionSavepoint = 0;
88471        }
88472      }else{
88473        int isSchemaChange;
88474        iSavepoint = db->nSavepoint - iSavepoint - 1;
88475        if( p1==SAVEPOINT_ROLLBACK ){
88476          isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
88477          for(ii=0; ii<db->nDb; ii++){
88478            rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
88479                                       SQLITE_ABORT_ROLLBACK,
88480                                       isSchemaChange==0);
88481            if( rc!=SQLITE_OK ) goto abort_due_to_error;
88482          }
88483        }else{
88484          assert( p1==SAVEPOINT_RELEASE );
88485          isSchemaChange = 0;
88486        }
88487        for(ii=0; ii<db->nDb; ii++){
88488          rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
88489          if( rc!=SQLITE_OK ){
88490            goto abort_due_to_error;
88491          }
88492        }
88493        if( isSchemaChange ){
88494          sqlite3ExpirePreparedStatements(db, 0);
88495          sqlite3ResetAllSchemasOfConnection(db);
88496          db->mDbFlags |= DBFLAG_SchemaChange;
88497        }
88498      }
88499      if( rc ) goto abort_due_to_error;
88500
88501      /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
88502      ** savepoints nested inside of the savepoint being operated on. */
88503      while( db->pSavepoint!=pSavepoint ){
88504        pTmp = db->pSavepoint;
88505        db->pSavepoint = pTmp->pNext;
88506        sqlite3DbFree(db, pTmp);
88507        db->nSavepoint--;
88508      }
88509
88510      /* If it is a RELEASE, then destroy the savepoint being operated on
88511      ** too. If it is a ROLLBACK TO, then set the number of deferred
88512      ** constraint violations present in the database to the value stored
88513      ** when the savepoint was created.  */
88514      if( p1==SAVEPOINT_RELEASE ){
88515        assert( pSavepoint==db->pSavepoint );
88516        db->pSavepoint = pSavepoint->pNext;
88517        sqlite3DbFree(db, pSavepoint);
88518        if( !isTransaction ){
88519          db->nSavepoint--;
88520        }
88521      }else{
88522        assert( p1==SAVEPOINT_ROLLBACK );
88523        db->nDeferredCons = pSavepoint->nDeferredCons;
88524        db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
88525      }
88526
88527      if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
88528        rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
88529        if( rc!=SQLITE_OK ) goto abort_due_to_error;
88530      }
88531    }
88532  }
88533  if( rc ) goto abort_due_to_error;
88534
88535  break;
88536}
88537
88538/* Opcode: AutoCommit P1 P2 * * *
88539**
88540** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
88541** back any currently active btree transactions. If there are any active
88542** VMs (apart from this one), then a ROLLBACK fails.  A COMMIT fails if
88543** there are active writing VMs or active VMs that use shared cache.
88544**
88545** This instruction causes the VM to halt.
88546*/
88547case OP_AutoCommit: {
88548  int desiredAutoCommit;
88549  int iRollback;
88550
88551  desiredAutoCommit = pOp->p1;
88552  iRollback = pOp->p2;
88553  assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
88554  assert( desiredAutoCommit==1 || iRollback==0 );
88555  assert( db->nVdbeActive>0 );  /* At least this one VM is active */
88556  assert( p->bIsReader );
88557
88558  if( desiredAutoCommit!=db->autoCommit ){
88559    if( iRollback ){
88560      assert( desiredAutoCommit==1 );
88561      sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
88562      db->autoCommit = 1;
88563    }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
88564      /* If this instruction implements a COMMIT and other VMs are writing
88565      ** return an error indicating that the other VMs must complete first.
88566      */
88567      sqlite3VdbeError(p, "cannot commit transaction - "
88568                          "SQL statements in progress");
88569      rc = SQLITE_BUSY;
88570      goto abort_due_to_error;
88571    }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
88572      goto vdbe_return;
88573    }else{
88574      db->autoCommit = (u8)desiredAutoCommit;
88575    }
88576    if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
88577      p->pc = (int)(pOp - aOp);
88578      db->autoCommit = (u8)(1-desiredAutoCommit);
88579      p->rc = rc = SQLITE_BUSY;
88580      goto vdbe_return;
88581    }
88582    sqlite3CloseSavepoints(db);
88583    if( p->rc==SQLITE_OK ){
88584      rc = SQLITE_DONE;
88585    }else{
88586      rc = SQLITE_ERROR;
88587    }
88588    goto vdbe_return;
88589  }else{
88590    sqlite3VdbeError(p,
88591        (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
88592        (iRollback)?"cannot rollback - no transaction is active":
88593                   "cannot commit - no transaction is active"));
88594
88595    rc = SQLITE_ERROR;
88596    goto abort_due_to_error;
88597  }
88598  /*NOTREACHED*/ assert(0);
88599}
88600
88601/* Opcode: Transaction P1 P2 P3 P4 P5
88602**
88603** Begin a transaction on database P1 if a transaction is not already
88604** active.
88605** If P2 is non-zero, then a write-transaction is started, or if a
88606** read-transaction is already active, it is upgraded to a write-transaction.
88607** If P2 is zero, then a read-transaction is started.
88608**
88609** P1 is the index of the database file on which the transaction is
88610** started.  Index 0 is the main database file and index 1 is the
88611** file used for temporary tables.  Indices of 2 or more are used for
88612** attached databases.
88613**
88614** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
88615** true (this flag is set if the Vdbe may modify more than one row and may
88616** throw an ABORT exception), a statement transaction may also be opened.
88617** More specifically, a statement transaction is opened iff the database
88618** connection is currently not in autocommit mode, or if there are other
88619** active statements. A statement transaction allows the changes made by this
88620** VDBE to be rolled back after an error without having to roll back the
88621** entire transaction. If no error is encountered, the statement transaction
88622** will automatically commit when the VDBE halts.
88623**
88624** If P5!=0 then this opcode also checks the schema cookie against P3
88625** and the schema generation counter against P4.
88626** The cookie changes its value whenever the database schema changes.
88627** This operation is used to detect when that the cookie has changed
88628** and that the current process needs to reread the schema.  If the schema
88629** cookie in P3 differs from the schema cookie in the database header or
88630** if the schema generation counter in P4 differs from the current
88631** generation counter, then an SQLITE_SCHEMA error is raised and execution
88632** halts.  The sqlite3_step() wrapper function might then reprepare the
88633** statement and rerun it from the beginning.
88634*/
88635case OP_Transaction: {
88636  Btree *pBt;
88637  int iMeta = 0;
88638
88639  assert( p->bIsReader );
88640  assert( p->readOnly==0 || pOp->p2==0 );
88641  assert( pOp->p1>=0 && pOp->p1<db->nDb );
88642  assert( DbMaskTest(p->btreeMask, pOp->p1) );
88643  if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
88644    rc = SQLITE_READONLY;
88645    goto abort_due_to_error;
88646  }
88647  pBt = db->aDb[pOp->p1].pBt;
88648
88649  if( pBt ){
88650    rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
88651    testcase( rc==SQLITE_BUSY_SNAPSHOT );
88652    testcase( rc==SQLITE_BUSY_RECOVERY );
88653    if( rc!=SQLITE_OK ){
88654      if( (rc&0xff)==SQLITE_BUSY ){
88655        p->pc = (int)(pOp - aOp);
88656        p->rc = rc;
88657        goto vdbe_return;
88658      }
88659      goto abort_due_to_error;
88660    }
88661
88662    if( p->usesStmtJournal
88663     && pOp->p2
88664     && (db->autoCommit==0 || db->nVdbeRead>1)
88665    ){
88666      assert( sqlite3BtreeIsInTrans(pBt) );
88667      if( p->iStatement==0 ){
88668        assert( db->nStatement>=0 && db->nSavepoint>=0 );
88669        db->nStatement++;
88670        p->iStatement = db->nSavepoint + db->nStatement;
88671      }
88672
88673      rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
88674      if( rc==SQLITE_OK ){
88675        rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
88676      }
88677
88678      /* Store the current value of the database handles deferred constraint
88679      ** counter. If the statement transaction needs to be rolled back,
88680      ** the value of this counter needs to be restored too.  */
88681      p->nStmtDefCons = db->nDeferredCons;
88682      p->nStmtDefImmCons = db->nDeferredImmCons;
88683    }
88684  }
88685  assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
88686  if( pOp->p5
88687   && (iMeta!=pOp->p3
88688      || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
88689  ){
88690    /*
88691    ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
88692    ** version is checked to ensure that the schema has not changed since the
88693    ** SQL statement was prepared.
88694    */
88695    sqlite3DbFree(db, p->zErrMsg);
88696    p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
88697    /* If the schema-cookie from the database file matches the cookie
88698    ** stored with the in-memory representation of the schema, do
88699    ** not reload the schema from the database file.
88700    **
88701    ** If virtual-tables are in use, this is not just an optimization.
88702    ** Often, v-tables store their data in other SQLite tables, which
88703    ** are queried from within xNext() and other v-table methods using
88704    ** prepared queries. If such a query is out-of-date, we do not want to
88705    ** discard the database schema, as the user code implementing the
88706    ** v-table would have to be ready for the sqlite3_vtab structure itself
88707    ** to be invalidated whenever sqlite3_step() is called from within
88708    ** a v-table method.
88709    */
88710    if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
88711      sqlite3ResetOneSchema(db, pOp->p1);
88712    }
88713    p->expired = 1;
88714    rc = SQLITE_SCHEMA;
88715  }
88716  if( rc ) goto abort_due_to_error;
88717  break;
88718}
88719
88720/* Opcode: ReadCookie P1 P2 P3 * *
88721**
88722** Read cookie number P3 from database P1 and write it into register P2.
88723** P3==1 is the schema version.  P3==2 is the database format.
88724** P3==3 is the recommended pager cache size, and so forth.  P1==0 is
88725** the main database file and P1==1 is the database file used to store
88726** temporary tables.
88727**
88728** There must be a read-lock on the database (either a transaction
88729** must be started or there must be an open cursor) before
88730** executing this instruction.
88731*/
88732case OP_ReadCookie: {               /* out2 */
88733  int iMeta;
88734  int iDb;
88735  int iCookie;
88736
88737  assert( p->bIsReader );
88738  iDb = pOp->p1;
88739  iCookie = pOp->p3;
88740  assert( pOp->p3<SQLITE_N_BTREE_META );
88741  assert( iDb>=0 && iDb<db->nDb );
88742  assert( db->aDb[iDb].pBt!=0 );
88743  assert( DbMaskTest(p->btreeMask, iDb) );
88744
88745  sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
88746  pOut = out2Prerelease(p, pOp);
88747  pOut->u.i = iMeta;
88748  break;
88749}
88750
88751/* Opcode: SetCookie P1 P2 P3 * *
88752**
88753** Write the integer value P3 into cookie number P2 of database P1.
88754** P2==1 is the schema version.  P2==2 is the database format.
88755** P2==3 is the recommended pager cache
88756** size, and so forth.  P1==0 is the main database file and P1==1 is the
88757** database file used to store temporary tables.
88758**
88759** A transaction must be started before executing this opcode.
88760*/
88761case OP_SetCookie: {
88762  Db *pDb;
88763
88764  sqlite3VdbeIncrWriteCounter(p, 0);
88765  assert( pOp->p2<SQLITE_N_BTREE_META );
88766  assert( pOp->p1>=0 && pOp->p1<db->nDb );
88767  assert( DbMaskTest(p->btreeMask, pOp->p1) );
88768  assert( p->readOnly==0 );
88769  pDb = &db->aDb[pOp->p1];
88770  assert( pDb->pBt!=0 );
88771  assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
88772  /* See note about index shifting on OP_ReadCookie */
88773  rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
88774  if( pOp->p2==BTREE_SCHEMA_VERSION ){
88775    /* When the schema cookie changes, record the new cookie internally */
88776    pDb->pSchema->schema_cookie = pOp->p3;
88777    db->mDbFlags |= DBFLAG_SchemaChange;
88778  }else if( pOp->p2==BTREE_FILE_FORMAT ){
88779    /* Record changes in the file format */
88780    pDb->pSchema->file_format = pOp->p3;
88781  }
88782  if( pOp->p1==1 ){
88783    /* Invalidate all prepared statements whenever the TEMP database
88784    ** schema is changed.  Ticket #1644 */
88785    sqlite3ExpirePreparedStatements(db, 0);
88786    p->expired = 0;
88787  }
88788  if( rc ) goto abort_due_to_error;
88789  break;
88790}
88791
88792/* Opcode: OpenRead P1 P2 P3 P4 P5
88793** Synopsis: root=P2 iDb=P3
88794**
88795** Open a read-only cursor for the database table whose root page is
88796** P2 in a database file.  The database file is determined by P3.
88797** P3==0 means the main database, P3==1 means the database used for
88798** temporary tables, and P3>1 means used the corresponding attached
88799** database.  Give the new cursor an identifier of P1.  The P1
88800** values need not be contiguous but all P1 values should be small integers.
88801** It is an error for P1 to be negative.
88802**
88803** Allowed P5 bits:
88804** <ul>
88805** <li>  <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
88806**       equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
88807**       of OP_SeekLE/OP_IdxLT)
88808** </ul>
88809**
88810** The P4 value may be either an integer (P4_INT32) or a pointer to
88811** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
88812** object, then table being opened must be an [index b-tree] where the
88813** KeyInfo object defines the content and collating
88814** sequence of that index b-tree. Otherwise, if P4 is an integer
88815** value, then the table being opened must be a [table b-tree] with a
88816** number of columns no less than the value of P4.
88817**
88818** See also: OpenWrite, ReopenIdx
88819*/
88820/* Opcode: ReopenIdx P1 P2 P3 P4 P5
88821** Synopsis: root=P2 iDb=P3
88822**
88823** The ReopenIdx opcode works like OP_OpenRead except that it first
88824** checks to see if the cursor on P1 is already open on the same
88825** b-tree and if it is this opcode becomes a no-op.  In other words,
88826** if the cursor is already open, do not reopen it.
88827**
88828** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
88829** and with P4 being a P4_KEYINFO object.  Furthermore, the P3 value must
88830** be the same as every other ReopenIdx or OpenRead for the same cursor
88831** number.
88832**
88833** Allowed P5 bits:
88834** <ul>
88835** <li>  <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
88836**       equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
88837**       of OP_SeekLE/OP_IdxLT)
88838** </ul>
88839**
88840** See also: OP_OpenRead, OP_OpenWrite
88841*/
88842/* Opcode: OpenWrite P1 P2 P3 P4 P5
88843** Synopsis: root=P2 iDb=P3
88844**
88845** Open a read/write cursor named P1 on the table or index whose root
88846** page is P2 (or whose root page is held in register P2 if the
88847** OPFLAG_P2ISREG bit is set in P5 - see below).
88848**
88849** The P4 value may be either an integer (P4_INT32) or a pointer to
88850** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
88851** object, then table being opened must be an [index b-tree] where the
88852** KeyInfo object defines the content and collating
88853** sequence of that index b-tree. Otherwise, if P4 is an integer
88854** value, then the table being opened must be a [table b-tree] with a
88855** number of columns no less than the value of P4.
88856**
88857** Allowed P5 bits:
88858** <ul>
88859** <li>  <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
88860**       equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
88861**       of OP_SeekLE/OP_IdxLT)
88862** <li>  <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
88863**       and subsequently delete entries in an index btree.  This is a
88864**       hint to the storage engine that the storage engine is allowed to
88865**       ignore.  The hint is not used by the official SQLite b*tree storage
88866**       engine, but is used by COMDB2.
88867** <li>  <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
88868**       as the root page, not the value of P2 itself.
88869** </ul>
88870**
88871** This instruction works like OpenRead except that it opens the cursor
88872** in read/write mode.
88873**
88874** See also: OP_OpenRead, OP_ReopenIdx
88875*/
88876case OP_ReopenIdx: {
88877  int nField;
88878  KeyInfo *pKeyInfo;
88879  int p2;
88880  int iDb;
88881  int wrFlag;
88882  Btree *pX;
88883  VdbeCursor *pCur;
88884  Db *pDb;
88885
88886  assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
88887  assert( pOp->p4type==P4_KEYINFO );
88888  pCur = p->apCsr[pOp->p1];
88889  if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
88890    assert( pCur->iDb==pOp->p3 );      /* Guaranteed by the code generator */
88891    goto open_cursor_set_hints;
88892  }
88893  /* If the cursor is not currently open or is open on a different
88894  ** index, then fall through into OP_OpenRead to force a reopen */
88895case OP_OpenRead:
88896case OP_OpenWrite:
88897
88898  assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
88899  assert( p->bIsReader );
88900  assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
88901          || p->readOnly==0 );
88902
88903  if( p->expired==1 ){
88904    rc = SQLITE_ABORT_ROLLBACK;
88905    goto abort_due_to_error;
88906  }
88907
88908  nField = 0;
88909  pKeyInfo = 0;
88910  p2 = pOp->p2;
88911  iDb = pOp->p3;
88912  assert( iDb>=0 && iDb<db->nDb );
88913  assert( DbMaskTest(p->btreeMask, iDb) );
88914  pDb = &db->aDb[iDb];
88915  pX = pDb->pBt;
88916  assert( pX!=0 );
88917  if( pOp->opcode==OP_OpenWrite ){
88918    assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
88919    wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
88920    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
88921    if( pDb->pSchema->file_format < p->minWriteFileFormat ){
88922      p->minWriteFileFormat = pDb->pSchema->file_format;
88923    }
88924  }else{
88925    wrFlag = 0;
88926  }
88927  if( pOp->p5 & OPFLAG_P2ISREG ){
88928    assert( p2>0 );
88929    assert( p2<=(p->nMem+1 - p->nCursor) );
88930    assert( pOp->opcode==OP_OpenWrite );
88931    pIn2 = &aMem[p2];
88932    assert( memIsValid(pIn2) );
88933    assert( (pIn2->flags & MEM_Int)!=0 );
88934    sqlite3VdbeMemIntegerify(pIn2);
88935    p2 = (int)pIn2->u.i;
88936    /* The p2 value always comes from a prior OP_CreateBtree opcode and
88937    ** that opcode will always set the p2 value to 2 or more or else fail.
88938    ** If there were a failure, the prepared statement would have halted
88939    ** before reaching this instruction. */
88940    assert( p2>=2 );
88941  }
88942  if( pOp->p4type==P4_KEYINFO ){
88943    pKeyInfo = pOp->p4.pKeyInfo;
88944    assert( pKeyInfo->enc==ENC(db) );
88945    assert( pKeyInfo->db==db );
88946    nField = pKeyInfo->nAllField;
88947  }else if( pOp->p4type==P4_INT32 ){
88948    nField = pOp->p4.i;
88949  }
88950  assert( pOp->p1>=0 );
88951  assert( nField>=0 );
88952  testcase( nField==0 );  /* Table with INTEGER PRIMARY KEY and nothing else */
88953  pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
88954  if( pCur==0 ) goto no_mem;
88955  pCur->nullRow = 1;
88956  pCur->isOrdered = 1;
88957  pCur->pgnoRoot = p2;
88958#ifdef SQLITE_DEBUG
88959  pCur->wrFlag = wrFlag;
88960#endif
88961  rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
88962  pCur->pKeyInfo = pKeyInfo;
88963  /* Set the VdbeCursor.isTable variable. Previous versions of
88964  ** SQLite used to check if the root-page flags were sane at this point
88965  ** and report database corruption if they were not, but this check has
88966  ** since moved into the btree layer.  */
88967  pCur->isTable = pOp->p4type!=P4_KEYINFO;
88968
88969open_cursor_set_hints:
88970  assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
88971  assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
88972  testcase( pOp->p5 & OPFLAG_BULKCSR );
88973  testcase( pOp->p2 & OPFLAG_SEEKEQ );
88974  sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
88975                               (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
88976  if( rc ) goto abort_due_to_error;
88977  break;
88978}
88979
88980/* Opcode: OpenDup P1 P2 * * *
88981**
88982** Open a new cursor P1 that points to the same ephemeral table as
88983** cursor P2.  The P2 cursor must have been opened by a prior OP_OpenEphemeral
88984** opcode.  Only ephemeral cursors may be duplicated.
88985**
88986** Duplicate ephemeral cursors are used for self-joins of materialized views.
88987*/
88988case OP_OpenDup: {
88989  VdbeCursor *pOrig;    /* The original cursor to be duplicated */
88990  VdbeCursor *pCx;      /* The new cursor */
88991
88992  pOrig = p->apCsr[pOp->p2];
88993  assert( pOrig );
88994  assert( pOrig->pBtx!=0 );  /* Only ephemeral cursors can be duplicated */
88995
88996  pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
88997  if( pCx==0 ) goto no_mem;
88998  pCx->nullRow = 1;
88999  pCx->isEphemeral = 1;
89000  pCx->pKeyInfo = pOrig->pKeyInfo;
89001  pCx->isTable = pOrig->isTable;
89002  pCx->pgnoRoot = pOrig->pgnoRoot;
89003  pCx->isOrdered = pOrig->isOrdered;
89004  rc = sqlite3BtreeCursor(pOrig->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
89005                          pCx->pKeyInfo, pCx->uc.pCursor);
89006  /* The sqlite3BtreeCursor() routine can only fail for the first cursor
89007  ** opened for a database.  Since there is already an open cursor when this
89008  ** opcode is run, the sqlite3BtreeCursor() cannot fail */
89009  assert( rc==SQLITE_OK );
89010  break;
89011}
89012
89013
89014/* Opcode: OpenEphemeral P1 P2 * P4 P5
89015** Synopsis: nColumn=P2
89016**
89017** Open a new cursor P1 to a transient table.
89018** The cursor is always opened read/write even if
89019** the main database is read-only.  The ephemeral
89020** table is deleted automatically when the cursor is closed.
89021**
89022** If the cursor P1 is already opened on an ephemeral table, the table
89023** is cleared (all content is erased).
89024**
89025** P2 is the number of columns in the ephemeral table.
89026** The cursor points to a BTree table if P4==0 and to a BTree index
89027** if P4 is not 0.  If P4 is not NULL, it points to a KeyInfo structure
89028** that defines the format of keys in the index.
89029**
89030** The P5 parameter can be a mask of the BTREE_* flags defined
89031** in btree.h.  These flags control aspects of the operation of
89032** the btree.  The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
89033** added automatically.
89034*/
89035/* Opcode: OpenAutoindex P1 P2 * P4 *
89036** Synopsis: nColumn=P2
89037**
89038** This opcode works the same as OP_OpenEphemeral.  It has a
89039** different name to distinguish its use.  Tables created using
89040** by this opcode will be used for automatically created transient
89041** indices in joins.
89042*/
89043case OP_OpenAutoindex:
89044case OP_OpenEphemeral: {
89045  VdbeCursor *pCx;
89046  KeyInfo *pKeyInfo;
89047
89048  static const int vfsFlags =
89049      SQLITE_OPEN_READWRITE |
89050      SQLITE_OPEN_CREATE |
89051      SQLITE_OPEN_EXCLUSIVE |
89052      SQLITE_OPEN_DELETEONCLOSE |
89053      SQLITE_OPEN_TRANSIENT_DB;
89054  assert( pOp->p1>=0 );
89055  assert( pOp->p2>=0 );
89056  pCx = p->apCsr[pOp->p1];
89057  if( pCx && pCx->pBtx ){
89058    /* If the ephermeral table is already open, erase all existing content
89059    ** so that the table is empty again, rather than creating a new table. */
89060    assert( pCx->isEphemeral );
89061    pCx->seqCount = 0;
89062    pCx->cacheStatus = CACHE_STALE;
89063    rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0);
89064  }else{
89065    pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
89066    if( pCx==0 ) goto no_mem;
89067    pCx->isEphemeral = 1;
89068    rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
89069                          BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
89070                          vfsFlags);
89071    if( rc==SQLITE_OK ){
89072      rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0);
89073    }
89074    if( rc==SQLITE_OK ){
89075      /* If a transient index is required, create it by calling
89076      ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
89077      ** opening it. If a transient table is required, just use the
89078      ** automatically created table with root-page 1 (an BLOB_INTKEY table).
89079      */
89080      if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
89081        assert( pOp->p4type==P4_KEYINFO );
89082        rc = sqlite3BtreeCreateTable(pCx->pBtx, (int*)&pCx->pgnoRoot,
89083                                     BTREE_BLOBKEY | pOp->p5);
89084        if( rc==SQLITE_OK ){
89085          assert( pCx->pgnoRoot==MASTER_ROOT+1 );
89086          assert( pKeyInfo->db==db );
89087          assert( pKeyInfo->enc==ENC(db) );
89088          rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
89089                                  pKeyInfo, pCx->uc.pCursor);
89090        }
89091        pCx->isTable = 0;
89092      }else{
89093        pCx->pgnoRoot = MASTER_ROOT;
89094        rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
89095                                0, pCx->uc.pCursor);
89096        pCx->isTable = 1;
89097      }
89098    }
89099    pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
89100  }
89101  if( rc ) goto abort_due_to_error;
89102  pCx->nullRow = 1;
89103  break;
89104}
89105
89106/* Opcode: SorterOpen P1 P2 P3 P4 *
89107**
89108** This opcode works like OP_OpenEphemeral except that it opens
89109** a transient index that is specifically designed to sort large
89110** tables using an external merge-sort algorithm.
89111**
89112** If argument P3 is non-zero, then it indicates that the sorter may
89113** assume that a stable sort considering the first P3 fields of each
89114** key is sufficient to produce the required results.
89115*/
89116case OP_SorterOpen: {
89117  VdbeCursor *pCx;
89118
89119  assert( pOp->p1>=0 );
89120  assert( pOp->p2>=0 );
89121  pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
89122  if( pCx==0 ) goto no_mem;
89123  pCx->pKeyInfo = pOp->p4.pKeyInfo;
89124  assert( pCx->pKeyInfo->db==db );
89125  assert( pCx->pKeyInfo->enc==ENC(db) );
89126  rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
89127  if( rc ) goto abort_due_to_error;
89128  break;
89129}
89130
89131/* Opcode: SequenceTest P1 P2 * * *
89132** Synopsis: if( cursor[P1].ctr++ ) pc = P2
89133**
89134** P1 is a sorter cursor. If the sequence counter is currently zero, jump
89135** to P2. Regardless of whether or not the jump is taken, increment the
89136** the sequence value.
89137*/
89138case OP_SequenceTest: {
89139  VdbeCursor *pC;
89140  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89141  pC = p->apCsr[pOp->p1];
89142  assert( isSorter(pC) );
89143  if( (pC->seqCount++)==0 ){
89144    goto jump_to_p2;
89145  }
89146  break;
89147}
89148
89149/* Opcode: OpenPseudo P1 P2 P3 * *
89150** Synopsis: P3 columns in r[P2]
89151**
89152** Open a new cursor that points to a fake table that contains a single
89153** row of data.  The content of that one row is the content of memory
89154** register P2.  In other words, cursor P1 becomes an alias for the
89155** MEM_Blob content contained in register P2.
89156**
89157** A pseudo-table created by this opcode is used to hold a single
89158** row output from the sorter so that the row can be decomposed into
89159** individual columns using the OP_Column opcode.  The OP_Column opcode
89160** is the only cursor opcode that works with a pseudo-table.
89161**
89162** P3 is the number of fields in the records that will be stored by
89163** the pseudo-table.
89164*/
89165case OP_OpenPseudo: {
89166  VdbeCursor *pCx;
89167
89168  assert( pOp->p1>=0 );
89169  assert( pOp->p3>=0 );
89170  pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
89171  if( pCx==0 ) goto no_mem;
89172  pCx->nullRow = 1;
89173  pCx->seekResult = pOp->p2;
89174  pCx->isTable = 1;
89175  /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
89176  ** can be safely passed to sqlite3VdbeCursorMoveto().  This avoids a test
89177  ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
89178  ** which is a performance optimization */
89179  pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
89180  assert( pOp->p5==0 );
89181  break;
89182}
89183
89184/* Opcode: Close P1 * * * *
89185**
89186** Close a cursor previously opened as P1.  If P1 is not
89187** currently open, this instruction is a no-op.
89188*/
89189case OP_Close: {
89190  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89191  sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
89192  p->apCsr[pOp->p1] = 0;
89193  break;
89194}
89195
89196#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
89197/* Opcode: ColumnsUsed P1 * * P4 *
89198**
89199** This opcode (which only exists if SQLite was compiled with
89200** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
89201** table or index for cursor P1 are used.  P4 is a 64-bit integer
89202** (P4_INT64) in which the first 63 bits are one for each of the
89203** first 63 columns of the table or index that are actually used
89204** by the cursor.  The high-order bit is set if any column after
89205** the 64th is used.
89206*/
89207case OP_ColumnsUsed: {
89208  VdbeCursor *pC;
89209  pC = p->apCsr[pOp->p1];
89210  assert( pC->eCurType==CURTYPE_BTREE );
89211  pC->maskUsed = *(u64*)pOp->p4.pI64;
89212  break;
89213}
89214#endif
89215
89216/* Opcode: SeekGE P1 P2 P3 P4 *
89217** Synopsis: key=r[P3@P4]
89218**
89219** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
89220** use the value in register P3 as the key.  If cursor P1 refers
89221** to an SQL index, then P3 is the first in an array of P4 registers
89222** that are used as an unpacked index key.
89223**
89224** Reposition cursor P1 so that  it points to the smallest entry that
89225** is greater than or equal to the key value. If there are no records
89226** greater than or equal to the key and P2 is not zero, then jump to P2.
89227**
89228** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
89229** opcode will either land on a record that exactly matches the key, or
89230** else it will cause a jump to P2.  When the cursor is OPFLAG_SEEKEQ,
89231** this opcode must be followed by an IdxLE opcode with the same arguments.
89232** The IdxGT opcode will be skipped if this opcode succeeds, but the
89233** IdxGT opcode will be used on subsequent loop iterations.  The
89234** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
89235** is an equality search.
89236**
89237** This opcode leaves the cursor configured to move in forward order,
89238** from the beginning toward the end.  In other words, the cursor is
89239** configured to use Next, not Prev.
89240**
89241** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
89242*/
89243/* Opcode: SeekGT P1 P2 P3 P4 *
89244** Synopsis: key=r[P3@P4]
89245**
89246** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
89247** use the value in register P3 as a key. If cursor P1 refers
89248** to an SQL index, then P3 is the first in an array of P4 registers
89249** that are used as an unpacked index key.
89250**
89251** Reposition cursor P1 so that it points to the smallest entry that
89252** is greater than the key value. If there are no records greater than
89253** the key and P2 is not zero, then jump to P2.
89254**
89255** This opcode leaves the cursor configured to move in forward order,
89256** from the beginning toward the end.  In other words, the cursor is
89257** configured to use Next, not Prev.
89258**
89259** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
89260*/
89261/* Opcode: SeekLT P1 P2 P3 P4 *
89262** Synopsis: key=r[P3@P4]
89263**
89264** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
89265** use the value in register P3 as a key. If cursor P1 refers
89266** to an SQL index, then P3 is the first in an array of P4 registers
89267** that are used as an unpacked index key.
89268**
89269** Reposition cursor P1 so that  it points to the largest entry that
89270** is less than the key value. If there are no records less than
89271** the key and P2 is not zero, then jump to P2.
89272**
89273** This opcode leaves the cursor configured to move in reverse order,
89274** from the end toward the beginning.  In other words, the cursor is
89275** configured to use Prev, not Next.
89276**
89277** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
89278*/
89279/* Opcode: SeekLE P1 P2 P3 P4 *
89280** Synopsis: key=r[P3@P4]
89281**
89282** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
89283** use the value in register P3 as a key. If cursor P1 refers
89284** to an SQL index, then P3 is the first in an array of P4 registers
89285** that are used as an unpacked index key.
89286**
89287** Reposition cursor P1 so that it points to the largest entry that
89288** is less than or equal to the key value. If there are no records
89289** less than or equal to the key and P2 is not zero, then jump to P2.
89290**
89291** This opcode leaves the cursor configured to move in reverse order,
89292** from the end toward the beginning.  In other words, the cursor is
89293** configured to use Prev, not Next.
89294**
89295** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
89296** opcode will either land on a record that exactly matches the key, or
89297** else it will cause a jump to P2.  When the cursor is OPFLAG_SEEKEQ,
89298** this opcode must be followed by an IdxLE opcode with the same arguments.
89299** The IdxGE opcode will be skipped if this opcode succeeds, but the
89300** IdxGE opcode will be used on subsequent loop iterations.  The
89301** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
89302** is an equality search.
89303**
89304** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
89305*/
89306case OP_SeekLT:         /* jump, in3, group */
89307case OP_SeekLE:         /* jump, in3, group */
89308case OP_SeekGE:         /* jump, in3, group */
89309case OP_SeekGT: {       /* jump, in3, group */
89310  int res;           /* Comparison result */
89311  int oc;            /* Opcode */
89312  VdbeCursor *pC;    /* The cursor to seek */
89313  UnpackedRecord r;  /* The key to seek for */
89314  int nField;        /* Number of columns or fields in the key */
89315  i64 iKey;          /* The rowid we are to seek to */
89316  int eqOnly;        /* Only interested in == results */
89317
89318  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89319  assert( pOp->p2!=0 );
89320  pC = p->apCsr[pOp->p1];
89321  assert( pC!=0 );
89322  assert( pC->eCurType==CURTYPE_BTREE );
89323  assert( OP_SeekLE == OP_SeekLT+1 );
89324  assert( OP_SeekGE == OP_SeekLT+2 );
89325  assert( OP_SeekGT == OP_SeekLT+3 );
89326  assert( pC->isOrdered );
89327  assert( pC->uc.pCursor!=0 );
89328  oc = pOp->opcode;
89329  eqOnly = 0;
89330  pC->nullRow = 0;
89331#ifdef SQLITE_DEBUG
89332  pC->seekOp = pOp->opcode;
89333#endif
89334
89335  pC->deferredMoveto = 0;
89336  pC->cacheStatus = CACHE_STALE;
89337  if( pC->isTable ){
89338    u16 flags3, newType;
89339    /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
89340    assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
89341              || CORRUPT_DB );
89342
89343    /* The input value in P3 might be of any type: integer, real, string,
89344    ** blob, or NULL.  But it needs to be an integer before we can do
89345    ** the seek, so convert it. */
89346    pIn3 = &aMem[pOp->p3];
89347    flags3 = pIn3->flags;
89348    if( (flags3 & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){
89349      applyNumericAffinity(pIn3, 0);
89350    }
89351    iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */
89352    newType = pIn3->flags; /* Record the type after applying numeric affinity */
89353    pIn3->flags = flags3;  /* But convert the type back to its original */
89354
89355    /* If the P3 value could not be converted into an integer without
89356    ** loss of information, then special processing is required... */
89357    if( (newType & (MEM_Int|MEM_IntReal))==0 ){
89358      if( (newType & MEM_Real)==0 ){
89359        if( (newType & MEM_Null) || oc>=OP_SeekGE ){
89360          VdbeBranchTaken(1,2);
89361          goto jump_to_p2;
89362        }else{
89363          rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
89364          if( rc!=SQLITE_OK ) goto abort_due_to_error;
89365          goto seek_not_found;
89366        }
89367      }else
89368
89369      /* If the approximation iKey is larger than the actual real search
89370      ** term, substitute >= for > and < for <=. e.g. if the search term
89371      ** is 4.9 and the integer approximation 5:
89372      **
89373      **        (x >  4.9)    ->     (x >= 5)
89374      **        (x <= 4.9)    ->     (x <  5)
89375      */
89376      if( pIn3->u.r<(double)iKey ){
89377        assert( OP_SeekGE==(OP_SeekGT-1) );
89378        assert( OP_SeekLT==(OP_SeekLE-1) );
89379        assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
89380        if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
89381      }
89382
89383      /* If the approximation iKey is smaller than the actual real search
89384      ** term, substitute <= for < and > for >=.  */
89385      else if( pIn3->u.r>(double)iKey ){
89386        assert( OP_SeekLE==(OP_SeekLT+1) );
89387        assert( OP_SeekGT==(OP_SeekGE+1) );
89388        assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
89389        if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
89390      }
89391    }
89392    rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
89393    pC->movetoTarget = iKey;  /* Used by OP_Delete */
89394    if( rc!=SQLITE_OK ){
89395      goto abort_due_to_error;
89396    }
89397  }else{
89398    /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
89399    ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
89400    ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
89401    ** with the same key.
89402    */
89403    if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
89404      eqOnly = 1;
89405      assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
89406      assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
89407      assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
89408      assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
89409      assert( pOp[1].p1==pOp[0].p1 );
89410      assert( pOp[1].p2==pOp[0].p2 );
89411      assert( pOp[1].p3==pOp[0].p3 );
89412      assert( pOp[1].p4.i==pOp[0].p4.i );
89413    }
89414
89415    nField = pOp->p4.i;
89416    assert( pOp->p4type==P4_INT32 );
89417    assert( nField>0 );
89418    r.pKeyInfo = pC->pKeyInfo;
89419    r.nField = (u16)nField;
89420
89421    /* The next line of code computes as follows, only faster:
89422    **   if( oc==OP_SeekGT || oc==OP_SeekLE ){
89423    **     r.default_rc = -1;
89424    **   }else{
89425    **     r.default_rc = +1;
89426    **   }
89427    */
89428    r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
89429    assert( oc!=OP_SeekGT || r.default_rc==-1 );
89430    assert( oc!=OP_SeekLE || r.default_rc==-1 );
89431    assert( oc!=OP_SeekGE || r.default_rc==+1 );
89432    assert( oc!=OP_SeekLT || r.default_rc==+1 );
89433
89434    r.aMem = &aMem[pOp->p3];
89435#ifdef SQLITE_DEBUG
89436    { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
89437#endif
89438    r.eqSeen = 0;
89439    rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
89440    if( rc!=SQLITE_OK ){
89441      goto abort_due_to_error;
89442    }
89443    if( eqOnly && r.eqSeen==0 ){
89444      assert( res!=0 );
89445      goto seek_not_found;
89446    }
89447  }
89448#ifdef SQLITE_TEST
89449  sqlite3_search_count++;
89450#endif
89451  if( oc>=OP_SeekGE ){  assert( oc==OP_SeekGE || oc==OP_SeekGT );
89452    if( res<0 || (res==0 && oc==OP_SeekGT) ){
89453      res = 0;
89454      rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
89455      if( rc!=SQLITE_OK ){
89456        if( rc==SQLITE_DONE ){
89457          rc = SQLITE_OK;
89458          res = 1;
89459        }else{
89460          goto abort_due_to_error;
89461        }
89462      }
89463    }else{
89464      res = 0;
89465    }
89466  }else{
89467    assert( oc==OP_SeekLT || oc==OP_SeekLE );
89468    if( res>0 || (res==0 && oc==OP_SeekLT) ){
89469      res = 0;
89470      rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
89471      if( rc!=SQLITE_OK ){
89472        if( rc==SQLITE_DONE ){
89473          rc = SQLITE_OK;
89474          res = 1;
89475        }else{
89476          goto abort_due_to_error;
89477        }
89478      }
89479    }else{
89480      /* res might be negative because the table is empty.  Check to
89481      ** see if this is the case.
89482      */
89483      res = sqlite3BtreeEof(pC->uc.pCursor);
89484    }
89485  }
89486seek_not_found:
89487  assert( pOp->p2>0 );
89488  VdbeBranchTaken(res!=0,2);
89489  if( res ){
89490    goto jump_to_p2;
89491  }else if( eqOnly ){
89492    assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
89493    pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
89494  }
89495  break;
89496}
89497
89498/* Opcode: SeekHit P1 P2 * * *
89499** Synopsis: seekHit=P2
89500**
89501** Set the seekHit flag on cursor P1 to the value in P2.
89502* The seekHit flag is used by the IfNoHope opcode.
89503**
89504** P1 must be a valid b-tree cursor.  P2 must be a boolean value,
89505** either 0 or 1.
89506*/
89507case OP_SeekHit: {
89508  VdbeCursor *pC;
89509  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89510  pC = p->apCsr[pOp->p1];
89511  assert( pC!=0 );
89512  assert( pOp->p2==0 || pOp->p2==1 );
89513  pC->seekHit = pOp->p2 & 1;
89514  break;
89515}
89516
89517/* Opcode: IfNotOpen P1 P2 * * *
89518** Synopsis: if( !csr[P1] ) goto P2
89519**
89520** If cursor P1 is not open, jump to instruction P2. Otherwise, fall through.
89521*/
89522case OP_IfNotOpen: {        /* jump */
89523  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89524  VdbeBranchTaken(p->apCsr[pOp->p1]==0, 2);
89525  if( !p->apCsr[pOp->p1] ){
89526    goto jump_to_p2_and_check_for_interrupt;
89527  }
89528  break;
89529}
89530
89531/* Opcode: Found P1 P2 P3 P4 *
89532** Synopsis: key=r[P3@P4]
89533**
89534** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
89535** P4>0 then register P3 is the first of P4 registers that form an unpacked
89536** record.
89537**
89538** Cursor P1 is on an index btree.  If the record identified by P3 and P4
89539** is a prefix of any entry in P1 then a jump is made to P2 and
89540** P1 is left pointing at the matching entry.
89541**
89542** This operation leaves the cursor in a state where it can be
89543** advanced in the forward direction.  The Next instruction will work,
89544** but not the Prev instruction.
89545**
89546** See also: NotFound, NoConflict, NotExists. SeekGe
89547*/
89548/* Opcode: NotFound P1 P2 P3 P4 *
89549** Synopsis: key=r[P3@P4]
89550**
89551** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
89552** P4>0 then register P3 is the first of P4 registers that form an unpacked
89553** record.
89554**
89555** Cursor P1 is on an index btree.  If the record identified by P3 and P4
89556** is not the prefix of any entry in P1 then a jump is made to P2.  If P1
89557** does contain an entry whose prefix matches the P3/P4 record then control
89558** falls through to the next instruction and P1 is left pointing at the
89559** matching entry.
89560**
89561** This operation leaves the cursor in a state where it cannot be
89562** advanced in either direction.  In other words, the Next and Prev
89563** opcodes do not work after this operation.
89564**
89565** See also: Found, NotExists, NoConflict, IfNoHope
89566*/
89567/* Opcode: IfNoHope P1 P2 P3 P4 *
89568** Synopsis: key=r[P3@P4]
89569**
89570** Register P3 is the first of P4 registers that form an unpacked
89571** record.
89572**
89573** Cursor P1 is on an index btree.  If the seekHit flag is set on P1, then
89574** this opcode is a no-op.  But if the seekHit flag of P1 is clear, then
89575** check to see if there is any entry in P1 that matches the
89576** prefix identified by P3 and P4.  If no entry matches the prefix,
89577** jump to P2.  Otherwise fall through.
89578**
89579** This opcode behaves like OP_NotFound if the seekHit
89580** flag is clear and it behaves like OP_Noop if the seekHit flag is set.
89581**
89582** This opcode is used in IN clause processing for a multi-column key.
89583** If an IN clause is attached to an element of the key other than the
89584** left-most element, and if there are no matches on the most recent
89585** seek over the whole key, then it might be that one of the key element
89586** to the left is prohibiting a match, and hence there is "no hope" of
89587** any match regardless of how many IN clause elements are checked.
89588** In such a case, we abandon the IN clause search early, using this
89589** opcode.  The opcode name comes from the fact that the
89590** jump is taken if there is "no hope" of achieving a match.
89591**
89592** See also: NotFound, SeekHit
89593*/
89594/* Opcode: NoConflict P1 P2 P3 P4 *
89595** Synopsis: key=r[P3@P4]
89596**
89597** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
89598** P4>0 then register P3 is the first of P4 registers that form an unpacked
89599** record.
89600**
89601** Cursor P1 is on an index btree.  If the record identified by P3 and P4
89602** contains any NULL value, jump immediately to P2.  If all terms of the
89603** record are not-NULL then a check is done to determine if any row in the
89604** P1 index btree has a matching key prefix.  If there are no matches, jump
89605** immediately to P2.  If there is a match, fall through and leave the P1
89606** cursor pointing to the matching row.
89607**
89608** This opcode is similar to OP_NotFound with the exceptions that the
89609** branch is always taken if any part of the search key input is NULL.
89610**
89611** This operation leaves the cursor in a state where it cannot be
89612** advanced in either direction.  In other words, the Next and Prev
89613** opcodes do not work after this operation.
89614**
89615** See also: NotFound, Found, NotExists
89616*/
89617case OP_IfNoHope: {     /* jump, in3 */
89618  VdbeCursor *pC;
89619  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89620  pC = p->apCsr[pOp->p1];
89621  assert( pC!=0 );
89622  if( pC->seekHit ) break;
89623  /* Fall through into OP_NotFound */
89624}
89625case OP_NoConflict:     /* jump, in3 */
89626case OP_NotFound:       /* jump, in3 */
89627case OP_Found: {        /* jump, in3 */
89628  int alreadyExists;
89629  int takeJump;
89630  int ii;
89631  VdbeCursor *pC;
89632  int res;
89633  UnpackedRecord *pFree;
89634  UnpackedRecord *pIdxKey;
89635  UnpackedRecord r;
89636
89637#ifdef SQLITE_TEST
89638  if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
89639#endif
89640
89641  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89642  assert( pOp->p4type==P4_INT32 );
89643  pC = p->apCsr[pOp->p1];
89644  assert( pC!=0 );
89645#ifdef SQLITE_DEBUG
89646  pC->seekOp = pOp->opcode;
89647#endif
89648  pIn3 = &aMem[pOp->p3];
89649  assert( pC->eCurType==CURTYPE_BTREE );
89650  assert( pC->uc.pCursor!=0 );
89651  assert( pC->isTable==0 );
89652  if( pOp->p4.i>0 ){
89653    r.pKeyInfo = pC->pKeyInfo;
89654    r.nField = (u16)pOp->p4.i;
89655    r.aMem = pIn3;
89656#ifdef SQLITE_DEBUG
89657    for(ii=0; ii<r.nField; ii++){
89658      assert( memIsValid(&r.aMem[ii]) );
89659      assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
89660      if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
89661    }
89662#endif
89663    pIdxKey = &r;
89664    pFree = 0;
89665  }else{
89666    assert( pIn3->flags & MEM_Blob );
89667    rc = ExpandBlob(pIn3);
89668    assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
89669    if( rc ) goto no_mem;
89670    pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
89671    if( pIdxKey==0 ) goto no_mem;
89672    sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
89673  }
89674  pIdxKey->default_rc = 0;
89675  takeJump = 0;
89676  if( pOp->opcode==OP_NoConflict ){
89677    /* For the OP_NoConflict opcode, take the jump if any of the
89678    ** input fields are NULL, since any key with a NULL will not
89679    ** conflict */
89680    for(ii=0; ii<pIdxKey->nField; ii++){
89681      if( pIdxKey->aMem[ii].flags & MEM_Null ){
89682        takeJump = 1;
89683        break;
89684      }
89685    }
89686  }
89687  rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
89688  if( pFree ) sqlite3DbFreeNN(db, pFree);
89689  if( rc!=SQLITE_OK ){
89690    goto abort_due_to_error;
89691  }
89692  pC->seekResult = res;
89693  alreadyExists = (res==0);
89694  pC->nullRow = 1-alreadyExists;
89695  pC->deferredMoveto = 0;
89696  pC->cacheStatus = CACHE_STALE;
89697  if( pOp->opcode==OP_Found ){
89698    VdbeBranchTaken(alreadyExists!=0,2);
89699    if( alreadyExists ) goto jump_to_p2;
89700  }else{
89701    VdbeBranchTaken(takeJump||alreadyExists==0,2);
89702    if( takeJump || !alreadyExists ) goto jump_to_p2;
89703  }
89704  break;
89705}
89706
89707/* Opcode: SeekRowid P1 P2 P3 * *
89708** Synopsis: intkey=r[P3]
89709**
89710** P1 is the index of a cursor open on an SQL table btree (with integer
89711** keys).  If register P3 does not contain an integer or if P1 does not
89712** contain a record with rowid P3 then jump immediately to P2.
89713** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
89714** a record with rowid P3 then
89715** leave the cursor pointing at that record and fall through to the next
89716** instruction.
89717**
89718** The OP_NotExists opcode performs the same operation, but with OP_NotExists
89719** the P3 register must be guaranteed to contain an integer value.  With this
89720** opcode, register P3 might not contain an integer.
89721**
89722** The OP_NotFound opcode performs the same operation on index btrees
89723** (with arbitrary multi-value keys).
89724**
89725** This opcode leaves the cursor in a state where it cannot be advanced
89726** in either direction.  In other words, the Next and Prev opcodes will
89727** not work following this opcode.
89728**
89729** See also: Found, NotFound, NoConflict, SeekRowid
89730*/
89731/* Opcode: NotExists P1 P2 P3 * *
89732** Synopsis: intkey=r[P3]
89733**
89734** P1 is the index of a cursor open on an SQL table btree (with integer
89735** keys).  P3 is an integer rowid.  If P1 does not contain a record with
89736** rowid P3 then jump immediately to P2.  Or, if P2 is 0, raise an
89737** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
89738** leave the cursor pointing at that record and fall through to the next
89739** instruction.
89740**
89741** The OP_SeekRowid opcode performs the same operation but also allows the
89742** P3 register to contain a non-integer value, in which case the jump is
89743** always taken.  This opcode requires that P3 always contain an integer.
89744**
89745** The OP_NotFound opcode performs the same operation on index btrees
89746** (with arbitrary multi-value keys).
89747**
89748** This opcode leaves the cursor in a state where it cannot be advanced
89749** in either direction.  In other words, the Next and Prev opcodes will
89750** not work following this opcode.
89751**
89752** See also: Found, NotFound, NoConflict, SeekRowid
89753*/
89754case OP_SeekRowid: {        /* jump, in3 */
89755  VdbeCursor *pC;
89756  BtCursor *pCrsr;
89757  int res;
89758  u64 iKey;
89759
89760  pIn3 = &aMem[pOp->p3];
89761  testcase( pIn3->flags & MEM_Int );
89762  testcase( pIn3->flags & MEM_IntReal );
89763  testcase( pIn3->flags & MEM_Real );
89764  testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
89765  if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
89766    /* If pIn3->u.i does not contain an integer, compute iKey as the
89767    ** integer value of pIn3.  Jump to P2 if pIn3 cannot be converted
89768    ** into an integer without loss of information.  Take care to avoid
89769    ** changing the datatype of pIn3, however, as it is used by other
89770    ** parts of the prepared statement. */
89771    Mem x = pIn3[0];
89772    applyAffinity(&x, SQLITE_AFF_NUMERIC, encoding);
89773    if( (x.flags & MEM_Int)==0 ) goto jump_to_p2;
89774    iKey = x.u.i;
89775    goto notExistsWithKey;
89776  }
89777  /* Fall through into OP_NotExists */
89778case OP_NotExists:          /* jump, in3 */
89779  pIn3 = &aMem[pOp->p3];
89780  assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
89781  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89782  iKey = pIn3->u.i;
89783notExistsWithKey:
89784  pC = p->apCsr[pOp->p1];
89785  assert( pC!=0 );
89786#ifdef SQLITE_DEBUG
89787  if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
89788#endif
89789  assert( pC->isTable );
89790  assert( pC->eCurType==CURTYPE_BTREE );
89791  pCrsr = pC->uc.pCursor;
89792  assert( pCrsr!=0 );
89793  res = 0;
89794  rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
89795  assert( rc==SQLITE_OK || res==0 );
89796  pC->movetoTarget = iKey;  /* Used by OP_Delete */
89797  pC->nullRow = 0;
89798  pC->cacheStatus = CACHE_STALE;
89799  pC->deferredMoveto = 0;
89800  VdbeBranchTaken(res!=0,2);
89801  pC->seekResult = res;
89802  if( res!=0 ){
89803    assert( rc==SQLITE_OK );
89804    if( pOp->p2==0 ){
89805      rc = SQLITE_CORRUPT_BKPT;
89806    }else{
89807      goto jump_to_p2;
89808    }
89809  }
89810  if( rc ) goto abort_due_to_error;
89811  break;
89812}
89813
89814/* Opcode: Sequence P1 P2 * * *
89815** Synopsis: r[P2]=cursor[P1].ctr++
89816**
89817** Find the next available sequence number for cursor P1.
89818** Write the sequence number into register P2.
89819** The sequence number on the cursor is incremented after this
89820** instruction.
89821*/
89822case OP_Sequence: {           /* out2 */
89823  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89824  assert( p->apCsr[pOp->p1]!=0 );
89825  assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
89826  pOut = out2Prerelease(p, pOp);
89827  pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
89828  break;
89829}
89830
89831
89832/* Opcode: NewRowid P1 P2 P3 * *
89833** Synopsis: r[P2]=rowid
89834**
89835** Get a new integer record number (a.k.a "rowid") used as the key to a table.
89836** The record number is not previously used as a key in the database
89837** table that cursor P1 points to.  The new record number is written
89838** written to register P2.
89839**
89840** If P3>0 then P3 is a register in the root frame of this VDBE that holds
89841** the largest previously generated record number. No new record numbers are
89842** allowed to be less than this value. When this value reaches its maximum,
89843** an SQLITE_FULL error is generated. The P3 register is updated with the '
89844** generated record number. This P3 mechanism is used to help implement the
89845** AUTOINCREMENT feature.
89846*/
89847case OP_NewRowid: {           /* out2 */
89848  i64 v;                 /* The new rowid */
89849  VdbeCursor *pC;        /* Cursor of table to get the new rowid */
89850  int res;               /* Result of an sqlite3BtreeLast() */
89851  int cnt;               /* Counter to limit the number of searches */
89852  Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
89853  VdbeFrame *pFrame;     /* Root frame of VDBE */
89854
89855  v = 0;
89856  res = 0;
89857  pOut = out2Prerelease(p, pOp);
89858  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89859  pC = p->apCsr[pOp->p1];
89860  assert( pC!=0 );
89861  assert( pC->isTable );
89862  assert( pC->eCurType==CURTYPE_BTREE );
89863  assert( pC->uc.pCursor!=0 );
89864  {
89865    /* The next rowid or record number (different terms for the same
89866    ** thing) is obtained in a two-step algorithm.
89867    **
89868    ** First we attempt to find the largest existing rowid and add one
89869    ** to that.  But if the largest existing rowid is already the maximum
89870    ** positive integer, we have to fall through to the second
89871    ** probabilistic algorithm
89872    **
89873    ** The second algorithm is to select a rowid at random and see if
89874    ** it already exists in the table.  If it does not exist, we have
89875    ** succeeded.  If the random rowid does exist, we select a new one
89876    ** and try again, up to 100 times.
89877    */
89878    assert( pC->isTable );
89879
89880#ifdef SQLITE_32BIT_ROWID
89881#   define MAX_ROWID 0x7fffffff
89882#else
89883    /* Some compilers complain about constants of the form 0x7fffffffffffffff.
89884    ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
89885    ** to provide the constant while making all compilers happy.
89886    */
89887#   define MAX_ROWID  (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
89888#endif
89889
89890    if( !pC->useRandomRowid ){
89891      rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
89892      if( rc!=SQLITE_OK ){
89893        goto abort_due_to_error;
89894      }
89895      if( res ){
89896        v = 1;   /* IMP: R-61914-48074 */
89897      }else{
89898        assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
89899        v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
89900        if( v>=MAX_ROWID ){
89901          pC->useRandomRowid = 1;
89902        }else{
89903          v++;   /* IMP: R-29538-34987 */
89904        }
89905      }
89906    }
89907
89908#ifndef SQLITE_OMIT_AUTOINCREMENT
89909    if( pOp->p3 ){
89910      /* Assert that P3 is a valid memory cell. */
89911      assert( pOp->p3>0 );
89912      if( p->pFrame ){
89913        for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
89914        /* Assert that P3 is a valid memory cell. */
89915        assert( pOp->p3<=pFrame->nMem );
89916        pMem = &pFrame->aMem[pOp->p3];
89917      }else{
89918        /* Assert that P3 is a valid memory cell. */
89919        assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
89920        pMem = &aMem[pOp->p3];
89921        memAboutToChange(p, pMem);
89922      }
89923      assert( memIsValid(pMem) );
89924
89925      REGISTER_TRACE(pOp->p3, pMem);
89926      sqlite3VdbeMemIntegerify(pMem);
89927      assert( (pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
89928      if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
89929        rc = SQLITE_FULL;   /* IMP: R-17817-00630 */
89930        goto abort_due_to_error;
89931      }
89932      if( v<pMem->u.i+1 ){
89933        v = pMem->u.i + 1;
89934      }
89935      pMem->u.i = v;
89936    }
89937#endif
89938    if( pC->useRandomRowid ){
89939      /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
89940      ** largest possible integer (9223372036854775807) then the database
89941      ** engine starts picking positive candidate ROWIDs at random until
89942      ** it finds one that is not previously used. */
89943      assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
89944                             ** an AUTOINCREMENT table. */
89945      cnt = 0;
89946      do{
89947        sqlite3_randomness(sizeof(v), &v);
89948        v &= (MAX_ROWID>>1); v++;  /* Ensure that v is greater than zero */
89949      }while(  ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
89950                                                 0, &res))==SQLITE_OK)
89951            && (res==0)
89952            && (++cnt<100));
89953      if( rc ) goto abort_due_to_error;
89954      if( res==0 ){
89955        rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
89956        goto abort_due_to_error;
89957      }
89958      assert( v>0 );  /* EV: R-40812-03570 */
89959    }
89960    pC->deferredMoveto = 0;
89961    pC->cacheStatus = CACHE_STALE;
89962  }
89963  pOut->u.i = v;
89964  break;
89965}
89966
89967/* Opcode: Insert P1 P2 P3 P4 P5
89968** Synopsis: intkey=r[P3] data=r[P2]
89969**
89970** Write an entry into the table of cursor P1.  A new entry is
89971** created if it doesn't already exist or the data for an existing
89972** entry is overwritten.  The data is the value MEM_Blob stored in register
89973** number P2. The key is stored in register P3. The key must
89974** be a MEM_Int.
89975**
89976** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
89977** incremented (otherwise not).  If the OPFLAG_LASTROWID flag of P5 is set,
89978** then rowid is stored for subsequent return by the
89979** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
89980**
89981** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
89982** run faster by avoiding an unnecessary seek on cursor P1.  However,
89983** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
89984** seeks on the cursor or if the most recent seek used a key equal to P3.
89985**
89986** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
89987** UPDATE operation.  Otherwise (if the flag is clear) then this opcode
89988** is part of an INSERT operation.  The difference is only important to
89989** the update hook.
89990**
89991** Parameter P4 may point to a Table structure, or may be NULL. If it is
89992** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
89993** following a successful insert.
89994**
89995** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
89996** allocated, then ownership of P2 is transferred to the pseudo-cursor
89997** and register P2 becomes ephemeral.  If the cursor is changed, the
89998** value of register P2 will then change.  Make sure this does not
89999** cause any problems.)
90000**
90001** This instruction only works on tables.  The equivalent instruction
90002** for indices is OP_IdxInsert.
90003*/
90004case OP_Insert: {
90005  Mem *pData;       /* MEM cell holding data for the record to be inserted */
90006  Mem *pKey;        /* MEM cell holding key  for the record */
90007  VdbeCursor *pC;   /* Cursor to table into which insert is written */
90008  int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
90009  const char *zDb;  /* database name - used by the update hook */
90010  Table *pTab;      /* Table structure - used by update and pre-update hooks */
90011  BtreePayload x;   /* Payload to be inserted */
90012
90013  pData = &aMem[pOp->p2];
90014  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90015  assert( memIsValid(pData) );
90016  pC = p->apCsr[pOp->p1];
90017  assert( pC!=0 );
90018  assert( pC->eCurType==CURTYPE_BTREE );
90019  assert( pC->deferredMoveto==0 );
90020  assert( pC->uc.pCursor!=0 );
90021  assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
90022  assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
90023  REGISTER_TRACE(pOp->p2, pData);
90024  sqlite3VdbeIncrWriteCounter(p, pC);
90025
90026  pKey = &aMem[pOp->p3];
90027  assert( pKey->flags & MEM_Int );
90028  assert( memIsValid(pKey) );
90029  REGISTER_TRACE(pOp->p3, pKey);
90030  x.nKey = pKey->u.i;
90031
90032  if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
90033    assert( pC->iDb>=0 );
90034    zDb = db->aDb[pC->iDb].zDbSName;
90035    pTab = pOp->p4.pTab;
90036    assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
90037  }else{
90038    pTab = 0;
90039    zDb = 0;  /* Not needed.  Silence a compiler warning. */
90040  }
90041
90042#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
90043  /* Invoke the pre-update hook, if any */
90044  if( pTab ){
90045    if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
90046      sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2);
90047    }
90048    if( db->xUpdateCallback==0 || pTab->aCol==0 ){
90049      /* Prevent post-update hook from running in cases when it should not */
90050      pTab = 0;
90051    }
90052  }
90053  if( pOp->p5 & OPFLAG_ISNOOP ) break;
90054#endif
90055
90056  if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
90057  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
90058  assert( pData->flags & (MEM_Blob|MEM_Str) );
90059  x.pData = pData->z;
90060  x.nData = pData->n;
90061  seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
90062  if( pData->flags & MEM_Zero ){
90063    x.nZero = pData->u.nZero;
90064  }else{
90065    x.nZero = 0;
90066  }
90067  x.pKey = 0;
90068  rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
90069      (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
90070  );
90071  pC->deferredMoveto = 0;
90072  pC->cacheStatus = CACHE_STALE;
90073
90074  /* Invoke the update-hook if required. */
90075  if( rc ) goto abort_due_to_error;
90076  if( pTab ){
90077    assert( db->xUpdateCallback!=0 );
90078    assert( pTab->aCol!=0 );
90079    db->xUpdateCallback(db->pUpdateArg,
90080           (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
90081           zDb, pTab->zName, x.nKey);
90082  }
90083  break;
90084}
90085
90086/* Opcode: Delete P1 P2 P3 P4 P5
90087**
90088** Delete the record at which the P1 cursor is currently pointing.
90089**
90090** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
90091** the cursor will be left pointing at  either the next or the previous
90092** record in the table. If it is left pointing at the next record, then
90093** the next Next instruction will be a no-op. As a result, in this case
90094** it is ok to delete a record from within a Next loop. If
90095** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
90096** left in an undefined state.
90097**
90098** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
90099** delete one of several associated with deleting a table row and all its
90100** associated index entries.  Exactly one of those deletes is the "primary"
90101** delete.  The others are all on OPFLAG_FORDELETE cursors or else are
90102** marked with the AUXDELETE flag.
90103**
90104** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
90105** change count is incremented (otherwise not).
90106**
90107** P1 must not be pseudo-table.  It has to be a real table with
90108** multiple rows.
90109**
90110** If P4 is not NULL then it points to a Table object. In this case either
90111** the update or pre-update hook, or both, may be invoked. The P1 cursor must
90112** have been positioned using OP_NotFound prior to invoking this opcode in
90113** this case. Specifically, if one is configured, the pre-update hook is
90114** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
90115** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
90116**
90117** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
90118** of the memory cell that contains the value that the rowid of the row will
90119** be set to by the update.
90120*/
90121case OP_Delete: {
90122  VdbeCursor *pC;
90123  const char *zDb;
90124  Table *pTab;
90125  int opflags;
90126
90127  opflags = pOp->p2;
90128  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90129  pC = p->apCsr[pOp->p1];
90130  assert( pC!=0 );
90131  assert( pC->eCurType==CURTYPE_BTREE );
90132  assert( pC->uc.pCursor!=0 );
90133  assert( pC->deferredMoveto==0 );
90134  sqlite3VdbeIncrWriteCounter(p, pC);
90135
90136#ifdef SQLITE_DEBUG
90137  if( pOp->p4type==P4_TABLE
90138   && HasRowid(pOp->p4.pTab)
90139   && pOp->p5==0
90140   && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
90141  ){
90142    /* If p5 is zero, the seek operation that positioned the cursor prior to
90143    ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
90144    ** the row that is being deleted */
90145    i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
90146    assert( CORRUPT_DB || pC->movetoTarget==iKey );
90147  }
90148#endif
90149
90150  /* If the update-hook or pre-update-hook will be invoked, set zDb to
90151  ** the name of the db to pass as to it. Also set local pTab to a copy
90152  ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
90153  ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
90154  ** VdbeCursor.movetoTarget to the current rowid.  */
90155  if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
90156    assert( pC->iDb>=0 );
90157    assert( pOp->p4.pTab!=0 );
90158    zDb = db->aDb[pC->iDb].zDbSName;
90159    pTab = pOp->p4.pTab;
90160    if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
90161      pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
90162    }
90163  }else{
90164    zDb = 0;   /* Not needed.  Silence a compiler warning. */
90165    pTab = 0;  /* Not needed.  Silence a compiler warning. */
90166  }
90167
90168#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
90169  /* Invoke the pre-update-hook if required. */
90170  if( db->xPreUpdateCallback && pOp->p4.pTab ){
90171    assert( !(opflags & OPFLAG_ISUPDATE)
90172         || HasRowid(pTab)==0
90173         || (aMem[pOp->p3].flags & MEM_Int)
90174    );
90175    sqlite3VdbePreUpdateHook(p, pC,
90176        (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
90177        zDb, pTab, pC->movetoTarget,
90178        pOp->p3
90179    );
90180  }
90181  if( opflags & OPFLAG_ISNOOP ) break;
90182#endif
90183
90184  /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
90185  assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
90186  assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
90187  assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
90188
90189#ifdef SQLITE_DEBUG
90190  if( p->pFrame==0 ){
90191    if( pC->isEphemeral==0
90192        && (pOp->p5 & OPFLAG_AUXDELETE)==0
90193        && (pC->wrFlag & OPFLAG_FORDELETE)==0
90194      ){
90195      nExtraDelete++;
90196    }
90197    if( pOp->p2 & OPFLAG_NCHANGE ){
90198      nExtraDelete--;
90199    }
90200  }
90201#endif
90202
90203  rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
90204  pC->cacheStatus = CACHE_STALE;
90205  pC->seekResult = 0;
90206  if( rc ) goto abort_due_to_error;
90207
90208  /* Invoke the update-hook if required. */
90209  if( opflags & OPFLAG_NCHANGE ){
90210    p->nChange++;
90211    if( db->xUpdateCallback && HasRowid(pTab) ){
90212      db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
90213          pC->movetoTarget);
90214      assert( pC->iDb>=0 );
90215    }
90216  }
90217
90218  break;
90219}
90220/* Opcode: ResetCount * * * * *
90221**
90222** The value of the change counter is copied to the database handle
90223** change counter (returned by subsequent calls to sqlite3_changes()).
90224** Then the VMs internal change counter resets to 0.
90225** This is used by trigger programs.
90226*/
90227case OP_ResetCount: {
90228  sqlite3VdbeSetChanges(db, p->nChange);
90229  p->nChange = 0;
90230  break;
90231}
90232
90233/* Opcode: SorterCompare P1 P2 P3 P4
90234** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
90235**
90236** P1 is a sorter cursor. This instruction compares a prefix of the
90237** record blob in register P3 against a prefix of the entry that
90238** the sorter cursor currently points to.  Only the first P4 fields
90239** of r[P3] and the sorter record are compared.
90240**
90241** If either P3 or the sorter contains a NULL in one of their significant
90242** fields (not counting the P4 fields at the end which are ignored) then
90243** the comparison is assumed to be equal.
90244**
90245** Fall through to next instruction if the two records compare equal to
90246** each other.  Jump to P2 if they are different.
90247*/
90248case OP_SorterCompare: {
90249  VdbeCursor *pC;
90250  int res;
90251  int nKeyCol;
90252
90253  pC = p->apCsr[pOp->p1];
90254  assert( isSorter(pC) );
90255  assert( pOp->p4type==P4_INT32 );
90256  pIn3 = &aMem[pOp->p3];
90257  nKeyCol = pOp->p4.i;
90258  res = 0;
90259  rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
90260  VdbeBranchTaken(res!=0,2);
90261  if( rc ) goto abort_due_to_error;
90262  if( res ) goto jump_to_p2;
90263  break;
90264};
90265
90266/* Opcode: SorterData P1 P2 P3 * *
90267** Synopsis: r[P2]=data
90268**
90269** Write into register P2 the current sorter data for sorter cursor P1.
90270** Then clear the column header cache on cursor P3.
90271**
90272** This opcode is normally use to move a record out of the sorter and into
90273** a register that is the source for a pseudo-table cursor created using
90274** OpenPseudo.  That pseudo-table cursor is the one that is identified by
90275** parameter P3.  Clearing the P3 column cache as part of this opcode saves
90276** us from having to issue a separate NullRow instruction to clear that cache.
90277*/
90278case OP_SorterData: {
90279  VdbeCursor *pC;
90280
90281  pOut = &aMem[pOp->p2];
90282  pC = p->apCsr[pOp->p1];
90283  assert( isSorter(pC) );
90284  rc = sqlite3VdbeSorterRowkey(pC, pOut);
90285  assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
90286  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90287  if( rc ) goto abort_due_to_error;
90288  p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
90289  break;
90290}
90291
90292/* Opcode: RowData P1 P2 P3 * *
90293** Synopsis: r[P2]=data
90294**
90295** Write into register P2 the complete row content for the row at
90296** which cursor P1 is currently pointing.
90297** There is no interpretation of the data.
90298** It is just copied onto the P2 register exactly as
90299** it is found in the database file.
90300**
90301** If cursor P1 is an index, then the content is the key of the row.
90302** If cursor P2 is a table, then the content extracted is the data.
90303**
90304** If the P1 cursor must be pointing to a valid row (not a NULL row)
90305** of a real table, not a pseudo-table.
90306**
90307** If P3!=0 then this opcode is allowed to make an ephemeral pointer
90308** into the database page.  That means that the content of the output
90309** register will be invalidated as soon as the cursor moves - including
90310** moves caused by other cursors that "save" the current cursors
90311** position in order that they can write to the same table.  If P3==0
90312** then a copy of the data is made into memory.  P3!=0 is faster, but
90313** P3==0 is safer.
90314**
90315** If P3!=0 then the content of the P2 register is unsuitable for use
90316** in OP_Result and any OP_Result will invalidate the P2 register content.
90317** The P2 register content is invalidated by opcodes like OP_Function or
90318** by any use of another cursor pointing to the same table.
90319*/
90320case OP_RowData: {
90321  VdbeCursor *pC;
90322  BtCursor *pCrsr;
90323  u32 n;
90324
90325  pOut = out2Prerelease(p, pOp);
90326
90327  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90328  pC = p->apCsr[pOp->p1];
90329  assert( pC!=0 );
90330  assert( pC->eCurType==CURTYPE_BTREE );
90331  assert( isSorter(pC)==0 );
90332  assert( pC->nullRow==0 );
90333  assert( pC->uc.pCursor!=0 );
90334  pCrsr = pC->uc.pCursor;
90335
90336  /* The OP_RowData opcodes always follow OP_NotExists or
90337  ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
90338  ** that might invalidate the cursor.
90339  ** If this where not the case, on of the following assert()s
90340  ** would fail.  Should this ever change (because of changes in the code
90341  ** generator) then the fix would be to insert a call to
90342  ** sqlite3VdbeCursorMoveto().
90343  */
90344  assert( pC->deferredMoveto==0 );
90345  assert( sqlite3BtreeCursorIsValid(pCrsr) );
90346#if 0  /* Not required due to the previous to assert() statements */
90347  rc = sqlite3VdbeCursorMoveto(pC);
90348  if( rc!=SQLITE_OK ) goto abort_due_to_error;
90349#endif
90350
90351  n = sqlite3BtreePayloadSize(pCrsr);
90352  if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
90353    goto too_big;
90354  }
90355  testcase( n==0 );
90356  rc = sqlite3VdbeMemFromBtreeZeroOffset(pCrsr, n, pOut);
90357  if( rc ) goto abort_due_to_error;
90358  if( !pOp->p3 ) Deephemeralize(pOut);
90359  UPDATE_MAX_BLOBSIZE(pOut);
90360  REGISTER_TRACE(pOp->p2, pOut);
90361  break;
90362}
90363
90364/* Opcode: Rowid P1 P2 * * *
90365** Synopsis: r[P2]=rowid
90366**
90367** Store in register P2 an integer which is the key of the table entry that
90368** P1 is currently point to.
90369**
90370** P1 can be either an ordinary table or a virtual table.  There used to
90371** be a separate OP_VRowid opcode for use with virtual tables, but this
90372** one opcode now works for both table types.
90373*/
90374case OP_Rowid: {                 /* out2 */
90375  VdbeCursor *pC;
90376  i64 v;
90377  sqlite3_vtab *pVtab;
90378  const sqlite3_module *pModule;
90379
90380  pOut = out2Prerelease(p, pOp);
90381  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90382  pC = p->apCsr[pOp->p1];
90383  assert( pC!=0 );
90384  assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
90385  if( pC->nullRow ){
90386    pOut->flags = MEM_Null;
90387    break;
90388  }else if( pC->deferredMoveto ){
90389    v = pC->movetoTarget;
90390#ifndef SQLITE_OMIT_VIRTUALTABLE
90391  }else if( pC->eCurType==CURTYPE_VTAB ){
90392    assert( pC->uc.pVCur!=0 );
90393    pVtab = pC->uc.pVCur->pVtab;
90394    pModule = pVtab->pModule;
90395    assert( pModule->xRowid );
90396    rc = pModule->xRowid(pC->uc.pVCur, &v);
90397    sqlite3VtabImportErrmsg(p, pVtab);
90398    if( rc ) goto abort_due_to_error;
90399#endif /* SQLITE_OMIT_VIRTUALTABLE */
90400  }else{
90401    assert( pC->eCurType==CURTYPE_BTREE );
90402    assert( pC->uc.pCursor!=0 );
90403    rc = sqlite3VdbeCursorRestore(pC);
90404    if( rc ) goto abort_due_to_error;
90405    if( pC->nullRow ){
90406      pOut->flags = MEM_Null;
90407      break;
90408    }
90409    v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
90410  }
90411  pOut->u.i = v;
90412  break;
90413}
90414
90415/* Opcode: NullRow P1 * * * *
90416**
90417** Move the cursor P1 to a null row.  Any OP_Column operations
90418** that occur while the cursor is on the null row will always
90419** write a NULL.
90420*/
90421case OP_NullRow: {
90422  VdbeCursor *pC;
90423
90424  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90425  pC = p->apCsr[pOp->p1];
90426  assert( pC!=0 );
90427  pC->nullRow = 1;
90428  pC->cacheStatus = CACHE_STALE;
90429  if( pC->eCurType==CURTYPE_BTREE ){
90430    assert( pC->uc.pCursor!=0 );
90431    sqlite3BtreeClearCursor(pC->uc.pCursor);
90432  }
90433#ifdef SQLITE_DEBUG
90434  if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
90435#endif
90436  break;
90437}
90438
90439/* Opcode: SeekEnd P1 * * * *
90440**
90441** Position cursor P1 at the end of the btree for the purpose of
90442** appending a new entry onto the btree.
90443**
90444** It is assumed that the cursor is used only for appending and so
90445** if the cursor is valid, then the cursor must already be pointing
90446** at the end of the btree and so no changes are made to
90447** the cursor.
90448*/
90449/* Opcode: Last P1 P2 * * *
90450**
90451** The next use of the Rowid or Column or Prev instruction for P1
90452** will refer to the last entry in the database table or index.
90453** If the table or index is empty and P2>0, then jump immediately to P2.
90454** If P2 is 0 or if the table or index is not empty, fall through
90455** to the following instruction.
90456**
90457** This opcode leaves the cursor configured to move in reverse order,
90458** from the end toward the beginning.  In other words, the cursor is
90459** configured to use Prev, not Next.
90460*/
90461case OP_SeekEnd:
90462case OP_Last: {        /* jump */
90463  VdbeCursor *pC;
90464  BtCursor *pCrsr;
90465  int res;
90466
90467  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90468  pC = p->apCsr[pOp->p1];
90469  assert( pC!=0 );
90470  assert( pC->eCurType==CURTYPE_BTREE );
90471  pCrsr = pC->uc.pCursor;
90472  res = 0;
90473  assert( pCrsr!=0 );
90474#ifdef SQLITE_DEBUG
90475  pC->seekOp = pOp->opcode;
90476#endif
90477  if( pOp->opcode==OP_SeekEnd ){
90478    assert( pOp->p2==0 );
90479    pC->seekResult = -1;
90480    if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
90481      break;
90482    }
90483  }
90484  rc = sqlite3BtreeLast(pCrsr, &res);
90485  pC->nullRow = (u8)res;
90486  pC->deferredMoveto = 0;
90487  pC->cacheStatus = CACHE_STALE;
90488  if( rc ) goto abort_due_to_error;
90489  if( pOp->p2>0 ){
90490    VdbeBranchTaken(res!=0,2);
90491    if( res ) goto jump_to_p2;
90492  }
90493  break;
90494}
90495
90496/* Opcode: IfSmaller P1 P2 P3 * *
90497**
90498** Estimate the number of rows in the table P1.  Jump to P2 if that
90499** estimate is less than approximately 2**(0.1*P3).
90500*/
90501case OP_IfSmaller: {        /* jump */
90502  VdbeCursor *pC;
90503  BtCursor *pCrsr;
90504  int res;
90505  i64 sz;
90506
90507  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90508  pC = p->apCsr[pOp->p1];
90509  assert( pC!=0 );
90510  pCrsr = pC->uc.pCursor;
90511  assert( pCrsr );
90512  rc = sqlite3BtreeFirst(pCrsr, &res);
90513  if( rc ) goto abort_due_to_error;
90514  if( res==0 ){
90515    sz = sqlite3BtreeRowCountEst(pCrsr);
90516    if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
90517  }
90518  VdbeBranchTaken(res!=0,2);
90519  if( res ) goto jump_to_p2;
90520  break;
90521}
90522
90523
90524/* Opcode: SorterSort P1 P2 * * *
90525**
90526** After all records have been inserted into the Sorter object
90527** identified by P1, invoke this opcode to actually do the sorting.
90528** Jump to P2 if there are no records to be sorted.
90529**
90530** This opcode is an alias for OP_Sort and OP_Rewind that is used
90531** for Sorter objects.
90532*/
90533/* Opcode: Sort P1 P2 * * *
90534**
90535** This opcode does exactly the same thing as OP_Rewind except that
90536** it increments an undocumented global variable used for testing.
90537**
90538** Sorting is accomplished by writing records into a sorting index,
90539** then rewinding that index and playing it back from beginning to
90540** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
90541** rewinding so that the global variable will be incremented and
90542** regression tests can determine whether or not the optimizer is
90543** correctly optimizing out sorts.
90544*/
90545case OP_SorterSort:    /* jump */
90546case OP_Sort: {        /* jump */
90547#ifdef SQLITE_TEST
90548  sqlite3_sort_count++;
90549  sqlite3_search_count--;
90550#endif
90551  p->aCounter[SQLITE_STMTSTATUS_SORT]++;
90552  /* Fall through into OP_Rewind */
90553}
90554/* Opcode: Rewind P1 P2 * * *
90555**
90556** The next use of the Rowid or Column or Next instruction for P1
90557** will refer to the first entry in the database table or index.
90558** If the table or index is empty, jump immediately to P2.
90559** If the table or index is not empty, fall through to the following
90560** instruction.
90561**
90562** This opcode leaves the cursor configured to move in forward order,
90563** from the beginning toward the end.  In other words, the cursor is
90564** configured to use Next, not Prev.
90565*/
90566case OP_Rewind: {        /* jump */
90567  VdbeCursor *pC;
90568  BtCursor *pCrsr;
90569  int res;
90570
90571  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90572  assert( pOp->p5==0 );
90573  pC = p->apCsr[pOp->p1];
90574  assert( pC!=0 );
90575  assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
90576  res = 1;
90577#ifdef SQLITE_DEBUG
90578  pC->seekOp = OP_Rewind;
90579#endif
90580  if( isSorter(pC) ){
90581    rc = sqlite3VdbeSorterRewind(pC, &res);
90582  }else{
90583    assert( pC->eCurType==CURTYPE_BTREE );
90584    pCrsr = pC->uc.pCursor;
90585    assert( pCrsr );
90586    rc = sqlite3BtreeFirst(pCrsr, &res);
90587    pC->deferredMoveto = 0;
90588    pC->cacheStatus = CACHE_STALE;
90589  }
90590  if( rc ) goto abort_due_to_error;
90591  pC->nullRow = (u8)res;
90592  assert( pOp->p2>0 && pOp->p2<p->nOp );
90593  VdbeBranchTaken(res!=0,2);
90594  if( res ) goto jump_to_p2;
90595  break;
90596}
90597
90598/* Opcode: Next P1 P2 P3 P4 P5
90599**
90600** Advance cursor P1 so that it points to the next key/data pair in its
90601** table or index.  If there are no more key/value pairs then fall through
90602** to the following instruction.  But if the cursor advance was successful,
90603** jump immediately to P2.
90604**
90605** The Next opcode is only valid following an SeekGT, SeekGE, or
90606** OP_Rewind opcode used to position the cursor.  Next is not allowed
90607** to follow SeekLT, SeekLE, or OP_Last.
90608**
90609** The P1 cursor must be for a real table, not a pseudo-table.  P1 must have
90610** been opened prior to this opcode or the program will segfault.
90611**
90612** The P3 value is a hint to the btree implementation. If P3==1, that
90613** means P1 is an SQL index and that this instruction could have been
90614** omitted if that index had been unique.  P3 is usually 0.  P3 is
90615** always either 0 or 1.
90616**
90617** P4 is always of type P4_ADVANCE. The function pointer points to
90618** sqlite3BtreeNext().
90619**
90620** If P5 is positive and the jump is taken, then event counter
90621** number P5-1 in the prepared statement is incremented.
90622**
90623** See also: Prev
90624*/
90625/* Opcode: Prev P1 P2 P3 P4 P5
90626**
90627** Back up cursor P1 so that it points to the previous key/data pair in its
90628** table or index.  If there is no previous key/value pairs then fall through
90629** to the following instruction.  But if the cursor backup was successful,
90630** jump immediately to P2.
90631**
90632**
90633** The Prev opcode is only valid following an SeekLT, SeekLE, or
90634** OP_Last opcode used to position the cursor.  Prev is not allowed
90635** to follow SeekGT, SeekGE, or OP_Rewind.
90636**
90637** The P1 cursor must be for a real table, not a pseudo-table.  If P1 is
90638** not open then the behavior is undefined.
90639**
90640** The P3 value is a hint to the btree implementation. If P3==1, that
90641** means P1 is an SQL index and that this instruction could have been
90642** omitted if that index had been unique.  P3 is usually 0.  P3 is
90643** always either 0 or 1.
90644**
90645** P4 is always of type P4_ADVANCE. The function pointer points to
90646** sqlite3BtreePrevious().
90647**
90648** If P5 is positive and the jump is taken, then event counter
90649** number P5-1 in the prepared statement is incremented.
90650*/
90651/* Opcode: SorterNext P1 P2 * * P5
90652**
90653** This opcode works just like OP_Next except that P1 must be a
90654** sorter object for which the OP_SorterSort opcode has been
90655** invoked.  This opcode advances the cursor to the next sorted
90656** record, or jumps to P2 if there are no more sorted records.
90657*/
90658case OP_SorterNext: {  /* jump */
90659  VdbeCursor *pC;
90660
90661  pC = p->apCsr[pOp->p1];
90662  assert( isSorter(pC) );
90663  rc = sqlite3VdbeSorterNext(db, pC);
90664  goto next_tail;
90665case OP_Prev:          /* jump */
90666case OP_Next:          /* jump */
90667  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90668  assert( pOp->p5<ArraySize(p->aCounter) );
90669  pC = p->apCsr[pOp->p1];
90670  assert( pC!=0 );
90671  assert( pC->deferredMoveto==0 );
90672  assert( pC->eCurType==CURTYPE_BTREE );
90673  assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
90674  assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
90675
90676  /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found.
90677  ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
90678  assert( pOp->opcode!=OP_Next
90679       || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
90680       || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
90681       || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
90682       || pC->seekOp==OP_IfNoHope);
90683  assert( pOp->opcode!=OP_Prev
90684       || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
90685       || pC->seekOp==OP_Last   || pC->seekOp==OP_IfNoHope
90686       || pC->seekOp==OP_NullRow);
90687
90688  rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
90689next_tail:
90690  pC->cacheStatus = CACHE_STALE;
90691  VdbeBranchTaken(rc==SQLITE_OK,2);
90692  if( rc==SQLITE_OK ){
90693    pC->nullRow = 0;
90694    p->aCounter[pOp->p5]++;
90695#ifdef SQLITE_TEST
90696    sqlite3_search_count++;
90697#endif
90698    goto jump_to_p2_and_check_for_interrupt;
90699  }
90700  if( rc!=SQLITE_DONE ) goto abort_due_to_error;
90701  rc = SQLITE_OK;
90702  pC->nullRow = 1;
90703  goto check_for_interrupt;
90704}
90705
90706/* Opcode: IdxInsert P1 P2 P3 P4 P5
90707** Synopsis: key=r[P2]
90708**
90709** Register P2 holds an SQL index key made using the
90710** MakeRecord instructions.  This opcode writes that key
90711** into the index P1.  Data for the entry is nil.
90712**
90713** If P4 is not zero, then it is the number of values in the unpacked
90714** key of reg(P2).  In that case, P3 is the index of the first register
90715** for the unpacked key.  The availability of the unpacked key can sometimes
90716** be an optimization.
90717**
90718** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
90719** that this insert is likely to be an append.
90720**
90721** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
90722** incremented by this instruction.  If the OPFLAG_NCHANGE bit is clear,
90723** then the change counter is unchanged.
90724**
90725** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
90726** run faster by avoiding an unnecessary seek on cursor P1.  However,
90727** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
90728** seeks on the cursor or if the most recent seek used a key equivalent
90729** to P2.
90730**
90731** This instruction only works for indices.  The equivalent instruction
90732** for tables is OP_Insert.
90733*/
90734case OP_IdxInsert: {        /* in2 */
90735  VdbeCursor *pC;
90736  BtreePayload x;
90737
90738  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90739  pC = p->apCsr[pOp->p1];
90740  sqlite3VdbeIncrWriteCounter(p, pC);
90741  assert( pC!=0 );
90742  assert( !isSorter(pC) );
90743  pIn2 = &aMem[pOp->p2];
90744  assert( pIn2->flags & MEM_Blob );
90745  if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
90746  assert( pC->eCurType==CURTYPE_BTREE );
90747  assert( pC->isTable==0 );
90748  rc = ExpandBlob(pIn2);
90749  if( rc ) goto abort_due_to_error;
90750  x.nKey = pIn2->n;
90751  x.pKey = pIn2->z;
90752  x.aMem = aMem + pOp->p3;
90753  x.nMem = (u16)pOp->p4.i;
90754  rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
90755       (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
90756      ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
90757      );
90758  assert( pC->deferredMoveto==0 );
90759  pC->cacheStatus = CACHE_STALE;
90760  if( rc) goto abort_due_to_error;
90761  break;
90762}
90763
90764/* Opcode: SorterInsert P1 P2 * * *
90765** Synopsis: key=r[P2]
90766**
90767** Register P2 holds an SQL index key made using the
90768** MakeRecord instructions.  This opcode writes that key
90769** into the sorter P1.  Data for the entry is nil.
90770*/
90771case OP_SorterInsert: {     /* in2 */
90772  VdbeCursor *pC;
90773
90774  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90775  pC = p->apCsr[pOp->p1];
90776  sqlite3VdbeIncrWriteCounter(p, pC);
90777  assert( pC!=0 );
90778  assert( isSorter(pC) );
90779  pIn2 = &aMem[pOp->p2];
90780  assert( pIn2->flags & MEM_Blob );
90781  assert( pC->isTable==0 );
90782  rc = ExpandBlob(pIn2);
90783  if( rc ) goto abort_due_to_error;
90784  rc = sqlite3VdbeSorterWrite(pC, pIn2);
90785  if( rc) goto abort_due_to_error;
90786  break;
90787}
90788
90789/* Opcode: IdxDelete P1 P2 P3 * P5
90790** Synopsis: key=r[P2@P3]
90791**
90792** The content of P3 registers starting at register P2 form
90793** an unpacked index key. This opcode removes that entry from the
90794** index opened by cursor P1.
90795**
90796** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error
90797** if no matching index entry is found.  This happens when running
90798** an UPDATE or DELETE statement and the index entry to be updated
90799** or deleted is not found.  For some uses of IdxDelete
90800** (example:  the EXCEPT operator) it does not matter that no matching
90801** entry is found.  For those cases, P5 is zero.
90802*/
90803case OP_IdxDelete: {
90804  VdbeCursor *pC;
90805  BtCursor *pCrsr;
90806  int res;
90807  UnpackedRecord r;
90808
90809  assert( pOp->p3>0 );
90810  assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
90811  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90812  pC = p->apCsr[pOp->p1];
90813  assert( pC!=0 );
90814  assert( pC->eCurType==CURTYPE_BTREE );
90815  sqlite3VdbeIncrWriteCounter(p, pC);
90816  pCrsr = pC->uc.pCursor;
90817  assert( pCrsr!=0 );
90818  r.pKeyInfo = pC->pKeyInfo;
90819  r.nField = (u16)pOp->p3;
90820  r.default_rc = 0;
90821  r.aMem = &aMem[pOp->p2];
90822  rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
90823  if( rc ) goto abort_due_to_error;
90824  if( res==0 ){
90825    rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
90826    if( rc ) goto abort_due_to_error;
90827  }else if( pOp->p5 ){
90828    rc = SQLITE_CORRUPT_INDEX;
90829    goto abort_due_to_error;
90830  }
90831  assert( pC->deferredMoveto==0 );
90832  pC->cacheStatus = CACHE_STALE;
90833  pC->seekResult = 0;
90834  break;
90835}
90836
90837/* Opcode: DeferredSeek P1 * P3 P4 *
90838** Synopsis: Move P3 to P1.rowid if needed
90839**
90840** P1 is an open index cursor and P3 is a cursor on the corresponding
90841** table.  This opcode does a deferred seek of the P3 table cursor
90842** to the row that corresponds to the current row of P1.
90843**
90844** This is a deferred seek.  Nothing actually happens until
90845** the cursor is used to read a record.  That way, if no reads
90846** occur, no unnecessary I/O happens.
90847**
90848** P4 may be an array of integers (type P4_INTARRAY) containing
90849** one entry for each column in the P3 table.  If array entry a(i)
90850** is non-zero, then reading column a(i)-1 from cursor P3 is
90851** equivalent to performing the deferred seek and then reading column i
90852** from P1.  This information is stored in P3 and used to redirect
90853** reads against P3 over to P1, thus possibly avoiding the need to
90854** seek and read cursor P3.
90855*/
90856/* Opcode: IdxRowid P1 P2 * * *
90857** Synopsis: r[P2]=rowid
90858**
90859** Write into register P2 an integer which is the last entry in the record at
90860** the end of the index key pointed to by cursor P1.  This integer should be
90861** the rowid of the table entry to which this index entry points.
90862**
90863** See also: Rowid, MakeRecord.
90864*/
90865case OP_DeferredSeek:
90866case OP_IdxRowid: {           /* out2 */
90867  VdbeCursor *pC;             /* The P1 index cursor */
90868  VdbeCursor *pTabCur;        /* The P2 table cursor (OP_DeferredSeek only) */
90869  i64 rowid;                  /* Rowid that P1 current points to */
90870
90871  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90872  pC = p->apCsr[pOp->p1];
90873  assert( pC!=0 );
90874  assert( pC->eCurType==CURTYPE_BTREE );
90875  assert( pC->uc.pCursor!=0 );
90876  assert( pC->isTable==0 );
90877  assert( pC->deferredMoveto==0 );
90878  assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
90879
90880  /* The IdxRowid and Seek opcodes are combined because of the commonality
90881  ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
90882  rc = sqlite3VdbeCursorRestore(pC);
90883
90884  /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
90885  ** out from under the cursor.  That will never happens for an IdxRowid
90886  ** or Seek opcode */
90887  if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
90888
90889  if( !pC->nullRow ){
90890    rowid = 0;  /* Not needed.  Only used to silence a warning. */
90891    rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
90892    if( rc!=SQLITE_OK ){
90893      goto abort_due_to_error;
90894    }
90895    if( pOp->opcode==OP_DeferredSeek ){
90896      assert( pOp->p3>=0 && pOp->p3<p->nCursor );
90897      pTabCur = p->apCsr[pOp->p3];
90898      assert( pTabCur!=0 );
90899      assert( pTabCur->eCurType==CURTYPE_BTREE );
90900      assert( pTabCur->uc.pCursor!=0 );
90901      assert( pTabCur->isTable );
90902      pTabCur->nullRow = 0;
90903      pTabCur->movetoTarget = rowid;
90904      pTabCur->deferredMoveto = 1;
90905      assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
90906      pTabCur->aAltMap = pOp->p4.ai;
90907      pTabCur->pAltCursor = pC;
90908    }else{
90909      pOut = out2Prerelease(p, pOp);
90910      pOut->u.i = rowid;
90911    }
90912  }else{
90913    assert( pOp->opcode==OP_IdxRowid );
90914    sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
90915  }
90916  break;
90917}
90918
90919/* Opcode: FinishSeek P1 * * * *
90920**
90921** If cursor P1 was previously moved via OP_DeferredSeek, complete that
90922** seek operation now, without further delay.  If the cursor seek has
90923** already occurred, this instruction is a no-op.
90924*/
90925case OP_FinishSeek: {
90926  VdbeCursor *pC;             /* The P1 index cursor */
90927
90928  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90929  pC = p->apCsr[pOp->p1];
90930  if( pC->deferredMoveto ){
90931    rc = sqlite3VdbeFinishMoveto(pC);
90932    if( rc ) goto abort_due_to_error;
90933  }
90934  break;
90935}
90936
90937/* Opcode: IdxGE P1 P2 P3 P4 P5
90938** Synopsis: key=r[P3@P4]
90939**
90940** The P4 register values beginning with P3 form an unpacked index
90941** key that omits the PRIMARY KEY.  Compare this key value against the index
90942** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
90943** fields at the end.
90944**
90945** If the P1 index entry is greater than or equal to the key value
90946** then jump to P2.  Otherwise fall through to the next instruction.
90947*/
90948/* Opcode: IdxGT P1 P2 P3 P4 P5
90949** Synopsis: key=r[P3@P4]
90950**
90951** The P4 register values beginning with P3 form an unpacked index
90952** key that omits the PRIMARY KEY.  Compare this key value against the index
90953** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
90954** fields at the end.
90955**
90956** If the P1 index entry is greater than the key value
90957** then jump to P2.  Otherwise fall through to the next instruction.
90958*/
90959/* Opcode: IdxLT P1 P2 P3 P4 P5
90960** Synopsis: key=r[P3@P4]
90961**
90962** The P4 register values beginning with P3 form an unpacked index
90963** key that omits the PRIMARY KEY or ROWID.  Compare this key value against
90964** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
90965** ROWID on the P1 index.
90966**
90967** If the P1 index entry is less than the key value then jump to P2.
90968** Otherwise fall through to the next instruction.
90969*/
90970/* Opcode: IdxLE P1 P2 P3 P4 P5
90971** Synopsis: key=r[P3@P4]
90972**
90973** The P4 register values beginning with P3 form an unpacked index
90974** key that omits the PRIMARY KEY or ROWID.  Compare this key value against
90975** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
90976** ROWID on the P1 index.
90977**
90978** If the P1 index entry is less than or equal to the key value then jump
90979** to P2. Otherwise fall through to the next instruction.
90980*/
90981case OP_IdxLE:          /* jump */
90982case OP_IdxGT:          /* jump */
90983case OP_IdxLT:          /* jump */
90984case OP_IdxGE:  {       /* jump */
90985  VdbeCursor *pC;
90986  int res;
90987  UnpackedRecord r;
90988
90989  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90990  pC = p->apCsr[pOp->p1];
90991  assert( pC!=0 );
90992  assert( pC->isOrdered );
90993  assert( pC->eCurType==CURTYPE_BTREE );
90994  assert( pC->uc.pCursor!=0);
90995  assert( pC->deferredMoveto==0 );
90996  assert( pOp->p5==0 || pOp->p5==1 );
90997  assert( pOp->p4type==P4_INT32 );
90998  r.pKeyInfo = pC->pKeyInfo;
90999  r.nField = (u16)pOp->p4.i;
91000  if( pOp->opcode<OP_IdxLT ){
91001    assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
91002    r.default_rc = -1;
91003  }else{
91004    assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
91005    r.default_rc = 0;
91006  }
91007  r.aMem = &aMem[pOp->p3];
91008#ifdef SQLITE_DEBUG
91009  {
91010    int i;
91011    for(i=0; i<r.nField; i++){
91012      assert( memIsValid(&r.aMem[i]) );
91013      REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
91014    }
91015  }
91016#endif
91017  res = 0;  /* Not needed.  Only used to silence a warning. */
91018  rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
91019  assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
91020  if( (pOp->opcode&1)==(OP_IdxLT&1) ){
91021    assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
91022    res = -res;
91023  }else{
91024    assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
91025    res++;
91026  }
91027  VdbeBranchTaken(res>0,2);
91028  if( rc ) goto abort_due_to_error;
91029  if( res>0 ) goto jump_to_p2;
91030  break;
91031}
91032
91033/* Opcode: Destroy P1 P2 P3 * *
91034**
91035** Delete an entire database table or index whose root page in the database
91036** file is given by P1.
91037**
91038** The table being destroyed is in the main database file if P3==0.  If
91039** P3==1 then the table to be clear is in the auxiliary database file
91040** that is used to store tables create using CREATE TEMPORARY TABLE.
91041**
91042** If AUTOVACUUM is enabled then it is possible that another root page
91043** might be moved into the newly deleted root page in order to keep all
91044** root pages contiguous at the beginning of the database.  The former
91045** value of the root page that moved - its value before the move occurred -
91046** is stored in register P2. If no page movement was required (because the
91047** table being dropped was already the last one in the database) then a
91048** zero is stored in register P2.  If AUTOVACUUM is disabled then a zero
91049** is stored in register P2.
91050**
91051** This opcode throws an error if there are any active reader VMs when
91052** it is invoked. This is done to avoid the difficulty associated with
91053** updating existing cursors when a root page is moved in an AUTOVACUUM
91054** database. This error is thrown even if the database is not an AUTOVACUUM
91055** db in order to avoid introducing an incompatibility between autovacuum
91056** and non-autovacuum modes.
91057**
91058** See also: Clear
91059*/
91060case OP_Destroy: {     /* out2 */
91061  int iMoved;
91062  int iDb;
91063
91064  sqlite3VdbeIncrWriteCounter(p, 0);
91065  assert( p->readOnly==0 );
91066  assert( pOp->p1>1 );
91067  pOut = out2Prerelease(p, pOp);
91068  pOut->flags = MEM_Null;
91069  if( db->nVdbeRead > db->nVDestroy+1 ){
91070    rc = SQLITE_LOCKED;
91071    p->errorAction = OE_Abort;
91072    goto abort_due_to_error;
91073  }else{
91074    iDb = pOp->p3;
91075    assert( DbMaskTest(p->btreeMask, iDb) );
91076    iMoved = 0;  /* Not needed.  Only to silence a warning. */
91077    rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
91078    pOut->flags = MEM_Int;
91079    pOut->u.i = iMoved;
91080    if( rc ) goto abort_due_to_error;
91081#ifndef SQLITE_OMIT_AUTOVACUUM
91082    if( iMoved!=0 ){
91083      sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
91084      /* All OP_Destroy operations occur on the same btree */
91085      assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
91086      resetSchemaOnFault = iDb+1;
91087    }
91088#endif
91089  }
91090  break;
91091}
91092
91093/* Opcode: Clear P1 P2 P3
91094**
91095** Delete all contents of the database table or index whose root page
91096** in the database file is given by P1.  But, unlike Destroy, do not
91097** remove the table or index from the database file.
91098**
91099** The table being clear is in the main database file if P2==0.  If
91100** P2==1 then the table to be clear is in the auxiliary database file
91101** that is used to store tables create using CREATE TEMPORARY TABLE.
91102**
91103** If the P3 value is non-zero, then the table referred to must be an
91104** intkey table (an SQL table, not an index). In this case the row change
91105** count is incremented by the number of rows in the table being cleared.
91106** If P3 is greater than zero, then the value stored in register P3 is
91107** also incremented by the number of rows in the table being cleared.
91108**
91109** See also: Destroy
91110*/
91111case OP_Clear: {
91112  int nChange;
91113
91114  sqlite3VdbeIncrWriteCounter(p, 0);
91115  nChange = 0;
91116  assert( p->readOnly==0 );
91117  assert( DbMaskTest(p->btreeMask, pOp->p2) );
91118  rc = sqlite3BtreeClearTable(
91119      db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
91120  );
91121  if( pOp->p3 ){
91122    p->nChange += nChange;
91123    if( pOp->p3>0 ){
91124      assert( memIsValid(&aMem[pOp->p3]) );
91125      memAboutToChange(p, &aMem[pOp->p3]);
91126      aMem[pOp->p3].u.i += nChange;
91127    }
91128  }
91129  if( rc ) goto abort_due_to_error;
91130  break;
91131}
91132
91133/* Opcode: ResetSorter P1 * * * *
91134**
91135** Delete all contents from the ephemeral table or sorter
91136** that is open on cursor P1.
91137**
91138** This opcode only works for cursors used for sorting and
91139** opened with OP_OpenEphemeral or OP_SorterOpen.
91140*/
91141case OP_ResetSorter: {
91142  VdbeCursor *pC;
91143
91144  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91145  pC = p->apCsr[pOp->p1];
91146  assert( pC!=0 );
91147  if( isSorter(pC) ){
91148    sqlite3VdbeSorterReset(db, pC->uc.pSorter);
91149  }else{
91150    assert( pC->eCurType==CURTYPE_BTREE );
91151    assert( pC->isEphemeral );
91152    rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
91153    if( rc ) goto abort_due_to_error;
91154  }
91155  break;
91156}
91157
91158/* Opcode: CreateBtree P1 P2 P3 * *
91159** Synopsis: r[P2]=root iDb=P1 flags=P3
91160**
91161** Allocate a new b-tree in the main database file if P1==0 or in the
91162** TEMP database file if P1==1 or in an attached database if
91163** P1>1.  The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
91164** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
91165** The root page number of the new b-tree is stored in register P2.
91166*/
91167case OP_CreateBtree: {          /* out2 */
91168  int pgno;
91169  Db *pDb;
91170
91171  sqlite3VdbeIncrWriteCounter(p, 0);
91172  pOut = out2Prerelease(p, pOp);
91173  pgno = 0;
91174  assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
91175  assert( pOp->p1>=0 && pOp->p1<db->nDb );
91176  assert( DbMaskTest(p->btreeMask, pOp->p1) );
91177  assert( p->readOnly==0 );
91178  pDb = &db->aDb[pOp->p1];
91179  assert( pDb->pBt!=0 );
91180  rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
91181  if( rc ) goto abort_due_to_error;
91182  pOut->u.i = pgno;
91183  break;
91184}
91185
91186/* Opcode: SqlExec * * * P4 *
91187**
91188** Run the SQL statement or statements specified in the P4 string.
91189*/
91190case OP_SqlExec: {
91191  sqlite3VdbeIncrWriteCounter(p, 0);
91192  db->nSqlExec++;
91193  rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
91194  db->nSqlExec--;
91195  if( rc ) goto abort_due_to_error;
91196  break;
91197}
91198
91199/* Opcode: ParseSchema P1 * * P4 *
91200**
91201** Read and parse all entries from the SQLITE_MASTER table of database P1
91202** that match the WHERE clause P4.  If P4 is a NULL pointer, then the
91203** entire schema for P1 is reparsed.
91204**
91205** This opcode invokes the parser to create a new virtual machine,
91206** then runs the new virtual machine.  It is thus a re-entrant opcode.
91207*/
91208case OP_ParseSchema: {
91209  int iDb;
91210  const char *zMaster;
91211  char *zSql;
91212  InitData initData;
91213
91214  /* Any prepared statement that invokes this opcode will hold mutexes
91215  ** on every btree.  This is a prerequisite for invoking
91216  ** sqlite3InitCallback().
91217  */
91218#ifdef SQLITE_DEBUG
91219  for(iDb=0; iDb<db->nDb; iDb++){
91220    assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
91221  }
91222#endif
91223
91224  iDb = pOp->p1;
91225  assert( iDb>=0 && iDb<db->nDb );
91226  assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
91227
91228#ifndef SQLITE_OMIT_ALTERTABLE
91229  if( pOp->p4.z==0 ){
91230    sqlite3SchemaClear(db->aDb[iDb].pSchema);
91231    db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
91232    rc = sqlite3InitOne(db, iDb, &p->zErrMsg, INITFLAG_AlterTable);
91233    db->mDbFlags |= DBFLAG_SchemaChange;
91234    p->expired = 0;
91235  }else
91236#endif
91237  {
91238    zMaster = MASTER_NAME;
91239    initData.db = db;
91240    initData.iDb = iDb;
91241    initData.pzErrMsg = &p->zErrMsg;
91242    initData.mInitFlags = 0;
91243    zSql = sqlite3MPrintf(db,
91244       "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
91245       db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
91246    if( zSql==0 ){
91247      rc = SQLITE_NOMEM_BKPT;
91248    }else{
91249      assert( db->init.busy==0 );
91250      db->init.busy = 1;
91251      initData.rc = SQLITE_OK;
91252      initData.nInitRow = 0;
91253      assert( !db->mallocFailed );
91254      rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
91255      if( rc==SQLITE_OK ) rc = initData.rc;
91256      if( rc==SQLITE_OK && initData.nInitRow==0 ){
91257        /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
91258        ** at least one SQL statement. Any less than that indicates that
91259        ** the sqlite_master table is corrupt. */
91260        rc = SQLITE_CORRUPT_BKPT;
91261      }
91262      sqlite3DbFreeNN(db, zSql);
91263      db->init.busy = 0;
91264    }
91265  }
91266  if( rc ){
91267    sqlite3ResetAllSchemasOfConnection(db);
91268    if( rc==SQLITE_NOMEM ){
91269      goto no_mem;
91270    }
91271    goto abort_due_to_error;
91272  }
91273  break;
91274}
91275
91276#if !defined(SQLITE_OMIT_ANALYZE)
91277/* Opcode: LoadAnalysis P1 * * * *
91278**
91279** Read the sqlite_stat1 table for database P1 and load the content
91280** of that table into the internal index hash table.  This will cause
91281** the analysis to be used when preparing all subsequent queries.
91282*/
91283case OP_LoadAnalysis: {
91284  assert( pOp->p1>=0 && pOp->p1<db->nDb );
91285  rc = sqlite3AnalysisLoad(db, pOp->p1);
91286  if( rc ) goto abort_due_to_error;
91287  break;
91288}
91289#endif /* !defined(SQLITE_OMIT_ANALYZE) */
91290
91291/* Opcode: DropTable P1 * * P4 *
91292**
91293** Remove the internal (in-memory) data structures that describe
91294** the table named P4 in database P1.  This is called after a table
91295** is dropped from disk (using the Destroy opcode) in order to keep
91296** the internal representation of the
91297** schema consistent with what is on disk.
91298*/
91299case OP_DropTable: {
91300  sqlite3VdbeIncrWriteCounter(p, 0);
91301  sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
91302  break;
91303}
91304
91305/* Opcode: DropIndex P1 * * P4 *
91306**
91307** Remove the internal (in-memory) data structures that describe
91308** the index named P4 in database P1.  This is called after an index
91309** is dropped from disk (using the Destroy opcode)
91310** in order to keep the internal representation of the
91311** schema consistent with what is on disk.
91312*/
91313case OP_DropIndex: {
91314  sqlite3VdbeIncrWriteCounter(p, 0);
91315  sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
91316  break;
91317}
91318
91319/* Opcode: DropTrigger P1 * * P4 *
91320**
91321** Remove the internal (in-memory) data structures that describe
91322** the trigger named P4 in database P1.  This is called after a trigger
91323** is dropped from disk (using the Destroy opcode) in order to keep
91324** the internal representation of the
91325** schema consistent with what is on disk.
91326*/
91327case OP_DropTrigger: {
91328  sqlite3VdbeIncrWriteCounter(p, 0);
91329  sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
91330  break;
91331}
91332
91333
91334#ifndef SQLITE_OMIT_INTEGRITY_CHECK
91335/* Opcode: IntegrityCk P1 P2 P3 P4 P5
91336**
91337** Do an analysis of the currently open database.  Store in
91338** register P1 the text of an error message describing any problems.
91339** If no problems are found, store a NULL in register P1.
91340**
91341** The register P3 contains one less than the maximum number of allowed errors.
91342** At most reg(P3) errors will be reported.
91343** In other words, the analysis stops as soon as reg(P1) errors are
91344** seen.  Reg(P1) is updated with the number of errors remaining.
91345**
91346** The root page numbers of all tables in the database are integers
91347** stored in P4_INTARRAY argument.
91348**
91349** If P5 is not zero, the check is done on the auxiliary database
91350** file, not the main database file.
91351**
91352** This opcode is used to implement the integrity_check pragma.
91353*/
91354case OP_IntegrityCk: {
91355  int nRoot;      /* Number of tables to check.  (Number of root pages.) */
91356  int *aRoot;     /* Array of rootpage numbers for tables to be checked */
91357  int nErr;       /* Number of errors reported */
91358  char *z;        /* Text of the error report */
91359  Mem *pnErr;     /* Register keeping track of errors remaining */
91360
91361  assert( p->bIsReader );
91362  nRoot = pOp->p2;
91363  aRoot = pOp->p4.ai;
91364  assert( nRoot>0 );
91365  assert( aRoot[0]==nRoot );
91366  assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
91367  pnErr = &aMem[pOp->p3];
91368  assert( (pnErr->flags & MEM_Int)!=0 );
91369  assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
91370  pIn1 = &aMem[pOp->p1];
91371  assert( pOp->p5<db->nDb );
91372  assert( DbMaskTest(p->btreeMask, pOp->p5) );
91373  z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
91374                                 (int)pnErr->u.i+1, &nErr);
91375  sqlite3VdbeMemSetNull(pIn1);
91376  if( nErr==0 ){
91377    assert( z==0 );
91378  }else if( z==0 ){
91379    goto no_mem;
91380  }else{
91381    pnErr->u.i -= nErr-1;
91382    sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
91383  }
91384  UPDATE_MAX_BLOBSIZE(pIn1);
91385  sqlite3VdbeChangeEncoding(pIn1, encoding);
91386  goto check_for_interrupt;
91387}
91388#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
91389
91390/* Opcode: RowSetAdd P1 P2 * * *
91391** Synopsis: rowset(P1)=r[P2]
91392**
91393** Insert the integer value held by register P2 into a RowSet object
91394** held in register P1.
91395**
91396** An assertion fails if P2 is not an integer.
91397*/
91398case OP_RowSetAdd: {       /* in1, in2 */
91399  pIn1 = &aMem[pOp->p1];
91400  pIn2 = &aMem[pOp->p2];
91401  assert( (pIn2->flags & MEM_Int)!=0 );
91402  if( (pIn1->flags & MEM_Blob)==0 ){
91403    if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
91404  }
91405  assert( sqlite3VdbeMemIsRowSet(pIn1) );
91406  sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
91407  break;
91408}
91409
91410/* Opcode: RowSetRead P1 P2 P3 * *
91411** Synopsis: r[P3]=rowset(P1)
91412**
91413** Extract the smallest value from the RowSet object in P1
91414** and put that value into register P3.
91415** Or, if RowSet object P1 is initially empty, leave P3
91416** unchanged and jump to instruction P2.
91417*/
91418case OP_RowSetRead: {       /* jump, in1, out3 */
91419  i64 val;
91420
91421  pIn1 = &aMem[pOp->p1];
91422  assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
91423  if( (pIn1->flags & MEM_Blob)==0
91424   || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
91425  ){
91426    /* The boolean index is empty */
91427    sqlite3VdbeMemSetNull(pIn1);
91428    VdbeBranchTaken(1,2);
91429    goto jump_to_p2_and_check_for_interrupt;
91430  }else{
91431    /* A value was pulled from the index */
91432    VdbeBranchTaken(0,2);
91433    sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
91434  }
91435  goto check_for_interrupt;
91436}
91437
91438/* Opcode: RowSetTest P1 P2 P3 P4
91439** Synopsis: if r[P3] in rowset(P1) goto P2
91440**
91441** Register P3 is assumed to hold a 64-bit integer value. If register P1
91442** contains a RowSet object and that RowSet object contains
91443** the value held in P3, jump to register P2. Otherwise, insert the
91444** integer in P3 into the RowSet and continue on to the
91445** next opcode.
91446**
91447** The RowSet object is optimized for the case where sets of integers
91448** are inserted in distinct phases, which each set contains no duplicates.
91449** Each set is identified by a unique P4 value. The first set
91450** must have P4==0, the final set must have P4==-1, and for all other sets
91451** must have P4>0.
91452**
91453** This allows optimizations: (a) when P4==0 there is no need to test
91454** the RowSet object for P3, as it is guaranteed not to contain it,
91455** (b) when P4==-1 there is no need to insert the value, as it will
91456** never be tested for, and (c) when a value that is part of set X is
91457** inserted, there is no need to search to see if the same value was
91458** previously inserted as part of set X (only if it was previously
91459** inserted as part of some other set).
91460*/
91461case OP_RowSetTest: {                     /* jump, in1, in3 */
91462  int iSet;
91463  int exists;
91464
91465  pIn1 = &aMem[pOp->p1];
91466  pIn3 = &aMem[pOp->p3];
91467  iSet = pOp->p4.i;
91468  assert( pIn3->flags&MEM_Int );
91469
91470  /* If there is anything other than a rowset object in memory cell P1,
91471  ** delete it now and initialize P1 with an empty rowset
91472  */
91473  if( (pIn1->flags & MEM_Blob)==0 ){
91474    if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
91475  }
91476  assert( sqlite3VdbeMemIsRowSet(pIn1) );
91477  assert( pOp->p4type==P4_INT32 );
91478  assert( iSet==-1 || iSet>=0 );
91479  if( iSet ){
91480    exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
91481    VdbeBranchTaken(exists!=0,2);
91482    if( exists ) goto jump_to_p2;
91483  }
91484  if( iSet>=0 ){
91485    sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
91486  }
91487  break;
91488}
91489
91490
91491#ifndef SQLITE_OMIT_TRIGGER
91492
91493/* Opcode: Program P1 P2 P3 P4 P5
91494**
91495** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
91496**
91497** P1 contains the address of the memory cell that contains the first memory
91498** cell in an array of values used as arguments to the sub-program. P2
91499** contains the address to jump to if the sub-program throws an IGNORE
91500** exception using the RAISE() function. Register P3 contains the address
91501** of a memory cell in this (the parent) VM that is used to allocate the
91502** memory required by the sub-vdbe at runtime.
91503**
91504** P4 is a pointer to the VM containing the trigger program.
91505**
91506** If P5 is non-zero, then recursive program invocation is enabled.
91507*/
91508case OP_Program: {        /* jump */
91509  int nMem;               /* Number of memory registers for sub-program */
91510  int nByte;              /* Bytes of runtime space required for sub-program */
91511  Mem *pRt;               /* Register to allocate runtime space */
91512  Mem *pMem;              /* Used to iterate through memory cells */
91513  Mem *pEnd;              /* Last memory cell in new array */
91514  VdbeFrame *pFrame;      /* New vdbe frame to execute in */
91515  SubProgram *pProgram;   /* Sub-program to execute */
91516  void *t;                /* Token identifying trigger */
91517
91518  pProgram = pOp->p4.pProgram;
91519  pRt = &aMem[pOp->p3];
91520  assert( pProgram->nOp>0 );
91521
91522  /* If the p5 flag is clear, then recursive invocation of triggers is
91523  ** disabled for backwards compatibility (p5 is set if this sub-program
91524  ** is really a trigger, not a foreign key action, and the flag set
91525  ** and cleared by the "PRAGMA recursive_triggers" command is clear).
91526  **
91527  ** It is recursive invocation of triggers, at the SQL level, that is
91528  ** disabled. In some cases a single trigger may generate more than one
91529  ** SubProgram (if the trigger may be executed with more than one different
91530  ** ON CONFLICT algorithm). SubProgram structures associated with a
91531  ** single trigger all have the same value for the SubProgram.token
91532  ** variable.  */
91533  if( pOp->p5 ){
91534    t = pProgram->token;
91535    for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
91536    if( pFrame ) break;
91537  }
91538
91539  if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
91540    rc = SQLITE_ERROR;
91541    sqlite3VdbeError(p, "too many levels of trigger recursion");
91542    goto abort_due_to_error;
91543  }
91544
91545  /* Register pRt is used to store the memory required to save the state
91546  ** of the current program, and the memory required at runtime to execute
91547  ** the trigger program. If this trigger has been fired before, then pRt
91548  ** is already allocated. Otherwise, it must be initialized.  */
91549  if( (pRt->flags&MEM_Blob)==0 ){
91550    /* SubProgram.nMem is set to the number of memory cells used by the
91551    ** program stored in SubProgram.aOp. As well as these, one memory
91552    ** cell is required for each cursor used by the program. Set local
91553    ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
91554    */
91555    nMem = pProgram->nMem + pProgram->nCsr;
91556    assert( nMem>0 );
91557    if( pProgram->nCsr==0 ) nMem++;
91558    nByte = ROUND8(sizeof(VdbeFrame))
91559              + nMem * sizeof(Mem)
91560              + pProgram->nCsr * sizeof(VdbeCursor*)
91561              + (pProgram->nOp + 7)/8;
91562    pFrame = sqlite3DbMallocZero(db, nByte);
91563    if( !pFrame ){
91564      goto no_mem;
91565    }
91566    sqlite3VdbeMemRelease(pRt);
91567    pRt->flags = MEM_Blob|MEM_Dyn;
91568    pRt->z = (char*)pFrame;
91569    pRt->n = nByte;
91570    pRt->xDel = sqlite3VdbeFrameMemDel;
91571
91572    pFrame->v = p;
91573    pFrame->nChildMem = nMem;
91574    pFrame->nChildCsr = pProgram->nCsr;
91575    pFrame->pc = (int)(pOp - aOp);
91576    pFrame->aMem = p->aMem;
91577    pFrame->nMem = p->nMem;
91578    pFrame->apCsr = p->apCsr;
91579    pFrame->nCursor = p->nCursor;
91580    pFrame->aOp = p->aOp;
91581    pFrame->nOp = p->nOp;
91582    pFrame->token = pProgram->token;
91583#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
91584    pFrame->anExec = p->anExec;
91585#endif
91586#ifdef SQLITE_DEBUG
91587    pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
91588#endif
91589
91590    pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
91591    for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
91592      pMem->flags = MEM_Undefined;
91593      pMem->db = db;
91594    }
91595  }else{
91596    pFrame = (VdbeFrame*)pRt->z;
91597    assert( pRt->xDel==sqlite3VdbeFrameMemDel );
91598    assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
91599        || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
91600    assert( pProgram->nCsr==pFrame->nChildCsr );
91601    assert( (int)(pOp - aOp)==pFrame->pc );
91602  }
91603
91604  p->nFrame++;
91605  pFrame->pParent = p->pFrame;
91606  pFrame->lastRowid = db->lastRowid;
91607  pFrame->nChange = p->nChange;
91608  pFrame->nDbChange = p->db->nChange;
91609  assert( pFrame->pAuxData==0 );
91610  pFrame->pAuxData = p->pAuxData;
91611  p->pAuxData = 0;
91612  p->nChange = 0;
91613  p->pFrame = pFrame;
91614  p->aMem = aMem = VdbeFrameMem(pFrame);
91615  p->nMem = pFrame->nChildMem;
91616  p->nCursor = (u16)pFrame->nChildCsr;
91617  p->apCsr = (VdbeCursor **)&aMem[p->nMem];
91618  pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
91619  memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
91620  p->aOp = aOp = pProgram->aOp;
91621  p->nOp = pProgram->nOp;
91622#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
91623  p->anExec = 0;
91624#endif
91625#ifdef SQLITE_DEBUG
91626  /* Verify that second and subsequent executions of the same trigger do not
91627  ** try to reuse register values from the first use. */
91628  {
91629    int i;
91630    for(i=0; i<p->nMem; i++){
91631      aMem[i].pScopyFrom = 0;  /* Prevent false-positive AboutToChange() errs */
91632      MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */
91633    }
91634  }
91635#endif
91636  pOp = &aOp[-1];
91637  goto check_for_interrupt;
91638}
91639
91640/* Opcode: Param P1 P2 * * *
91641**
91642** This opcode is only ever present in sub-programs called via the
91643** OP_Program instruction. Copy a value currently stored in a memory
91644** cell of the calling (parent) frame to cell P2 in the current frames
91645** address space. This is used by trigger programs to access the new.*
91646** and old.* values.
91647**
91648** The address of the cell in the parent frame is determined by adding
91649** the value of the P1 argument to the value of the P1 argument to the
91650** calling OP_Program instruction.
91651*/
91652case OP_Param: {           /* out2 */
91653  VdbeFrame *pFrame;
91654  Mem *pIn;
91655  pOut = out2Prerelease(p, pOp);
91656  pFrame = p->pFrame;
91657  pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
91658  sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
91659  break;
91660}
91661
91662#endif /* #ifndef SQLITE_OMIT_TRIGGER */
91663
91664#ifndef SQLITE_OMIT_FOREIGN_KEY
91665/* Opcode: FkCounter P1 P2 * * *
91666** Synopsis: fkctr[P1]+=P2
91667**
91668** Increment a "constraint counter" by P2 (P2 may be negative or positive).
91669** If P1 is non-zero, the database constraint counter is incremented
91670** (deferred foreign key constraints). Otherwise, if P1 is zero, the
91671** statement counter is incremented (immediate foreign key constraints).
91672*/
91673case OP_FkCounter: {
91674  if( db->flags & SQLITE_DeferFKs ){
91675    db->nDeferredImmCons += pOp->p2;
91676  }else if( pOp->p1 ){
91677    db->nDeferredCons += pOp->p2;
91678  }else{
91679    p->nFkConstraint += pOp->p2;
91680  }
91681  break;
91682}
91683
91684/* Opcode: FkIfZero P1 P2 * * *
91685** Synopsis: if fkctr[P1]==0 goto P2
91686**
91687** This opcode tests if a foreign key constraint-counter is currently zero.
91688** If so, jump to instruction P2. Otherwise, fall through to the next
91689** instruction.
91690**
91691** If P1 is non-zero, then the jump is taken if the database constraint-counter
91692** is zero (the one that counts deferred constraint violations). If P1 is
91693** zero, the jump is taken if the statement constraint-counter is zero
91694** (immediate foreign key constraint violations).
91695*/
91696case OP_FkIfZero: {         /* jump */
91697  if( pOp->p1 ){
91698    VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
91699    if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
91700  }else{
91701    VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
91702    if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
91703  }
91704  break;
91705}
91706#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
91707
91708#ifndef SQLITE_OMIT_AUTOINCREMENT
91709/* Opcode: MemMax P1 P2 * * *
91710** Synopsis: r[P1]=max(r[P1],r[P2])
91711**
91712** P1 is a register in the root frame of this VM (the root frame is
91713** different from the current frame if this instruction is being executed
91714** within a sub-program). Set the value of register P1 to the maximum of
91715** its current value and the value in register P2.
91716**
91717** This instruction throws an error if the memory cell is not initially
91718** an integer.
91719*/
91720case OP_MemMax: {        /* in2 */
91721  VdbeFrame *pFrame;
91722  if( p->pFrame ){
91723    for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
91724    pIn1 = &pFrame->aMem[pOp->p1];
91725  }else{
91726    pIn1 = &aMem[pOp->p1];
91727  }
91728  assert( memIsValid(pIn1) );
91729  sqlite3VdbeMemIntegerify(pIn1);
91730  pIn2 = &aMem[pOp->p2];
91731  sqlite3VdbeMemIntegerify(pIn2);
91732  if( pIn1->u.i<pIn2->u.i){
91733    pIn1->u.i = pIn2->u.i;
91734  }
91735  break;
91736}
91737#endif /* SQLITE_OMIT_AUTOINCREMENT */
91738
91739/* Opcode: IfPos P1 P2 P3 * *
91740** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
91741**
91742** Register P1 must contain an integer.
91743** If the value of register P1 is 1 or greater, subtract P3 from the
91744** value in P1 and jump to P2.
91745**
91746** If the initial value of register P1 is less than 1, then the
91747** value is unchanged and control passes through to the next instruction.
91748*/
91749case OP_IfPos: {        /* jump, in1 */
91750  pIn1 = &aMem[pOp->p1];
91751  assert( pIn1->flags&MEM_Int );
91752  VdbeBranchTaken( pIn1->u.i>0, 2);
91753  if( pIn1->u.i>0 ){
91754    pIn1->u.i -= pOp->p3;
91755    goto jump_to_p2;
91756  }
91757  break;
91758}
91759
91760/* Opcode: OffsetLimit P1 P2 P3 * *
91761** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
91762**
91763** This opcode performs a commonly used computation associated with
91764** LIMIT and OFFSET process.  r[P1] holds the limit counter.  r[P3]
91765** holds the offset counter.  The opcode computes the combined value
91766** of the LIMIT and OFFSET and stores that value in r[P2].  The r[P2]
91767** value computed is the total number of rows that will need to be
91768** visited in order to complete the query.
91769**
91770** If r[P3] is zero or negative, that means there is no OFFSET
91771** and r[P2] is set to be the value of the LIMIT, r[P1].
91772**
91773** if r[P1] is zero or negative, that means there is no LIMIT
91774** and r[P2] is set to -1.
91775**
91776** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
91777*/
91778case OP_OffsetLimit: {    /* in1, out2, in3 */
91779  i64 x;
91780  pIn1 = &aMem[pOp->p1];
91781  pIn3 = &aMem[pOp->p3];
91782  pOut = out2Prerelease(p, pOp);
91783  assert( pIn1->flags & MEM_Int );
91784  assert( pIn3->flags & MEM_Int );
91785  x = pIn1->u.i;
91786  if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
91787    /* If the LIMIT is less than or equal to zero, loop forever.  This
91788    ** is documented.  But also, if the LIMIT+OFFSET exceeds 2^63 then
91789    ** also loop forever.  This is undocumented.  In fact, one could argue
91790    ** that the loop should terminate.  But assuming 1 billion iterations
91791    ** per second (far exceeding the capabilities of any current hardware)
91792    ** it would take nearly 300 years to actually reach the limit.  So
91793    ** looping forever is a reasonable approximation. */
91794    pOut->u.i = -1;
91795  }else{
91796    pOut->u.i = x;
91797  }
91798  break;
91799}
91800
91801/* Opcode: IfNotZero P1 P2 * * *
91802** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
91803**
91804** Register P1 must contain an integer.  If the content of register P1 is
91805** initially greater than zero, then decrement the value in register P1.
91806** If it is non-zero (negative or positive) and then also jump to P2.
91807** If register P1 is initially zero, leave it unchanged and fall through.
91808*/
91809case OP_IfNotZero: {        /* jump, in1 */
91810  pIn1 = &aMem[pOp->p1];
91811  assert( pIn1->flags&MEM_Int );
91812  VdbeBranchTaken(pIn1->u.i<0, 2);
91813  if( pIn1->u.i ){
91814     if( pIn1->u.i>0 ) pIn1->u.i--;
91815     goto jump_to_p2;
91816  }
91817  break;
91818}
91819
91820/* Opcode: DecrJumpZero P1 P2 * * *
91821** Synopsis: if (--r[P1])==0 goto P2
91822**
91823** Register P1 must hold an integer.  Decrement the value in P1
91824** and jump to P2 if the new value is exactly zero.
91825*/
91826case OP_DecrJumpZero: {      /* jump, in1 */
91827  pIn1 = &aMem[pOp->p1];
91828  assert( pIn1->flags&MEM_Int );
91829  if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
91830  VdbeBranchTaken(pIn1->u.i==0, 2);
91831  if( pIn1->u.i==0 ) goto jump_to_p2;
91832  break;
91833}
91834
91835
91836/* Opcode: AggStep * P2 P3 P4 P5
91837** Synopsis: accum=r[P3] step(r[P2@P5])
91838**
91839** Execute the xStep function for an aggregate.
91840** The function has P5 arguments.  P4 is a pointer to the
91841** FuncDef structure that specifies the function.  Register P3 is the
91842** accumulator.
91843**
91844** The P5 arguments are taken from register P2 and its
91845** successors.
91846*/
91847/* Opcode: AggInverse * P2 P3 P4 P5
91848** Synopsis: accum=r[P3] inverse(r[P2@P5])
91849**
91850** Execute the xInverse function for an aggregate.
91851** The function has P5 arguments.  P4 is a pointer to the
91852** FuncDef structure that specifies the function.  Register P3 is the
91853** accumulator.
91854**
91855** The P5 arguments are taken from register P2 and its
91856** successors.
91857*/
91858/* Opcode: AggStep1 P1 P2 P3 P4 P5
91859** Synopsis: accum=r[P3] step(r[P2@P5])
91860**
91861** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
91862** aggregate.  The function has P5 arguments.  P4 is a pointer to the
91863** FuncDef structure that specifies the function.  Register P3 is the
91864** accumulator.
91865**
91866** The P5 arguments are taken from register P2 and its
91867** successors.
91868**
91869** This opcode is initially coded as OP_AggStep0.  On first evaluation,
91870** the FuncDef stored in P4 is converted into an sqlite3_context and
91871** the opcode is changed.  In this way, the initialization of the
91872** sqlite3_context only happens once, instead of on each call to the
91873** step function.
91874*/
91875case OP_AggInverse:
91876case OP_AggStep: {
91877  int n;
91878  sqlite3_context *pCtx;
91879
91880  assert( pOp->p4type==P4_FUNCDEF );
91881  n = pOp->p5;
91882  assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
91883  assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
91884  assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
91885  pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
91886               (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
91887  if( pCtx==0 ) goto no_mem;
91888  pCtx->pMem = 0;
91889  pCtx->pOut = (Mem*)&(pCtx->argv[n]);
91890  sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
91891  pCtx->pFunc = pOp->p4.pFunc;
91892  pCtx->iOp = (int)(pOp - aOp);
91893  pCtx->pVdbe = p;
91894  pCtx->skipFlag = 0;
91895  pCtx->isError = 0;
91896  pCtx->argc = n;
91897  pOp->p4type = P4_FUNCCTX;
91898  pOp->p4.pCtx = pCtx;
91899
91900  /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
91901  assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
91902
91903  pOp->opcode = OP_AggStep1;
91904  /* Fall through into OP_AggStep */
91905}
91906case OP_AggStep1: {
91907  int i;
91908  sqlite3_context *pCtx;
91909  Mem *pMem;
91910
91911  assert( pOp->p4type==P4_FUNCCTX );
91912  pCtx = pOp->p4.pCtx;
91913  pMem = &aMem[pOp->p3];
91914
91915#ifdef SQLITE_DEBUG
91916  if( pOp->p1 ){
91917    /* This is an OP_AggInverse call.  Verify that xStep has always
91918    ** been called at least once prior to any xInverse call. */
91919    assert( pMem->uTemp==0x1122e0e3 );
91920  }else{
91921    /* This is an OP_AggStep call.  Mark it as such. */
91922    pMem->uTemp = 0x1122e0e3;
91923  }
91924#endif
91925
91926  /* If this function is inside of a trigger, the register array in aMem[]
91927  ** might change from one evaluation to the next.  The next block of code
91928  ** checks to see if the register array has changed, and if so it
91929  ** reinitializes the relavant parts of the sqlite3_context object */
91930  if( pCtx->pMem != pMem ){
91931    pCtx->pMem = pMem;
91932    for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
91933  }
91934
91935#ifdef SQLITE_DEBUG
91936  for(i=0; i<pCtx->argc; i++){
91937    assert( memIsValid(pCtx->argv[i]) );
91938    REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
91939  }
91940#endif
91941
91942  pMem->n++;
91943  assert( pCtx->pOut->flags==MEM_Null );
91944  assert( pCtx->isError==0 );
91945  assert( pCtx->skipFlag==0 );
91946#ifndef SQLITE_OMIT_WINDOWFUNC
91947  if( pOp->p1 ){
91948    (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
91949  }else
91950#endif
91951  (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
91952
91953  if( pCtx->isError ){
91954    if( pCtx->isError>0 ){
91955      sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
91956      rc = pCtx->isError;
91957    }
91958    if( pCtx->skipFlag ){
91959      assert( pOp[-1].opcode==OP_CollSeq );
91960      i = pOp[-1].p1;
91961      if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
91962      pCtx->skipFlag = 0;
91963    }
91964    sqlite3VdbeMemRelease(pCtx->pOut);
91965    pCtx->pOut->flags = MEM_Null;
91966    pCtx->isError = 0;
91967    if( rc ) goto abort_due_to_error;
91968  }
91969  assert( pCtx->pOut->flags==MEM_Null );
91970  assert( pCtx->skipFlag==0 );
91971  break;
91972}
91973
91974/* Opcode: AggFinal P1 P2 * P4 *
91975** Synopsis: accum=r[P1] N=P2
91976**
91977** P1 is the memory location that is the accumulator for an aggregate
91978** or window function.  Execute the finalizer function
91979** for an aggregate and store the result in P1.
91980**
91981** P2 is the number of arguments that the step function takes and
91982** P4 is a pointer to the FuncDef for this function.  The P2
91983** argument is not used by this opcode.  It is only there to disambiguate
91984** functions that can take varying numbers of arguments.  The
91985** P4 argument is only needed for the case where
91986** the step function was not previously called.
91987*/
91988/* Opcode: AggValue * P2 P3 P4 *
91989** Synopsis: r[P3]=value N=P2
91990**
91991** Invoke the xValue() function and store the result in register P3.
91992**
91993** P2 is the number of arguments that the step function takes and
91994** P4 is a pointer to the FuncDef for this function.  The P2
91995** argument is not used by this opcode.  It is only there to disambiguate
91996** functions that can take varying numbers of arguments.  The
91997** P4 argument is only needed for the case where
91998** the step function was not previously called.
91999*/
92000case OP_AggValue:
92001case OP_AggFinal: {
92002  Mem *pMem;
92003  assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
92004  assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
92005  pMem = &aMem[pOp->p1];
92006  assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
92007#ifndef SQLITE_OMIT_WINDOWFUNC
92008  if( pOp->p3 ){
92009    memAboutToChange(p, &aMem[pOp->p3]);
92010    rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
92011    pMem = &aMem[pOp->p3];
92012  }else
92013#endif
92014  {
92015    rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
92016  }
92017
92018  if( rc ){
92019    sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
92020    goto abort_due_to_error;
92021  }
92022  sqlite3VdbeChangeEncoding(pMem, encoding);
92023  UPDATE_MAX_BLOBSIZE(pMem);
92024  if( sqlite3VdbeMemTooBig(pMem) ){
92025    goto too_big;
92026  }
92027  break;
92028}
92029
92030#ifndef SQLITE_OMIT_WAL
92031/* Opcode: Checkpoint P1 P2 P3 * *
92032**
92033** Checkpoint database P1. This is a no-op if P1 is not currently in
92034** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
92035** RESTART, or TRUNCATE.  Write 1 or 0 into mem[P3] if the checkpoint returns
92036** SQLITE_BUSY or not, respectively.  Write the number of pages in the
92037** WAL after the checkpoint into mem[P3+1] and the number of pages
92038** in the WAL that have been checkpointed after the checkpoint
92039** completes into mem[P3+2].  However on an error, mem[P3+1] and
92040** mem[P3+2] are initialized to -1.
92041*/
92042case OP_Checkpoint: {
92043  int i;                          /* Loop counter */
92044  int aRes[3];                    /* Results */
92045  Mem *pMem;                      /* Write results here */
92046
92047  assert( p->readOnly==0 );
92048  aRes[0] = 0;
92049  aRes[1] = aRes[2] = -1;
92050  assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
92051       || pOp->p2==SQLITE_CHECKPOINT_FULL
92052       || pOp->p2==SQLITE_CHECKPOINT_RESTART
92053       || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
92054  );
92055  rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
92056  if( rc ){
92057    if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
92058    rc = SQLITE_OK;
92059    aRes[0] = 1;
92060  }
92061  for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
92062    sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
92063  }
92064  break;
92065};
92066#endif
92067
92068#ifndef SQLITE_OMIT_PRAGMA
92069/* Opcode: JournalMode P1 P2 P3 * *
92070**
92071** Change the journal mode of database P1 to P3. P3 must be one of the
92072** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
92073** modes (delete, truncate, persist, off and memory), this is a simple
92074** operation. No IO is required.
92075**
92076** If changing into or out of WAL mode the procedure is more complicated.
92077**
92078** Write a string containing the final journal-mode to register P2.
92079*/
92080case OP_JournalMode: {    /* out2 */
92081  Btree *pBt;                     /* Btree to change journal mode of */
92082  Pager *pPager;                  /* Pager associated with pBt */
92083  int eNew;                       /* New journal mode */
92084  int eOld;                       /* The old journal mode */
92085#ifndef SQLITE_OMIT_WAL
92086  const char *zFilename;          /* Name of database file for pPager */
92087#endif
92088
92089  pOut = out2Prerelease(p, pOp);
92090  eNew = pOp->p3;
92091  assert( eNew==PAGER_JOURNALMODE_DELETE
92092       || eNew==PAGER_JOURNALMODE_TRUNCATE
92093       || eNew==PAGER_JOURNALMODE_PERSIST
92094       || eNew==PAGER_JOURNALMODE_OFF
92095       || eNew==PAGER_JOURNALMODE_MEMORY
92096       || eNew==PAGER_JOURNALMODE_WAL
92097       || eNew==PAGER_JOURNALMODE_QUERY
92098  );
92099  assert( pOp->p1>=0 && pOp->p1<db->nDb );
92100  assert( p->readOnly==0 );
92101
92102  pBt = db->aDb[pOp->p1].pBt;
92103  pPager = sqlite3BtreePager(pBt);
92104  eOld = sqlite3PagerGetJournalMode(pPager);
92105  if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
92106  if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
92107
92108#ifndef SQLITE_OMIT_WAL
92109  zFilename = sqlite3PagerFilename(pPager, 1);
92110
92111  /* Do not allow a transition to journal_mode=WAL for a database
92112  ** in temporary storage or if the VFS does not support shared memory
92113  */
92114  if( eNew==PAGER_JOURNALMODE_WAL
92115   && (sqlite3Strlen30(zFilename)==0           /* Temp file */
92116       || !sqlite3PagerWalSupported(pPager))   /* No shared-memory support */
92117  ){
92118    eNew = eOld;
92119  }
92120
92121  if( (eNew!=eOld)
92122   && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
92123  ){
92124    if( !db->autoCommit || db->nVdbeRead>1 ){
92125      rc = SQLITE_ERROR;
92126      sqlite3VdbeError(p,
92127          "cannot change %s wal mode from within a transaction",
92128          (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
92129      );
92130      goto abort_due_to_error;
92131    }else{
92132
92133      if( eOld==PAGER_JOURNALMODE_WAL ){
92134        /* If leaving WAL mode, close the log file. If successful, the call
92135        ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
92136        ** file. An EXCLUSIVE lock may still be held on the database file
92137        ** after a successful return.
92138        */
92139        rc = sqlite3PagerCloseWal(pPager, db);
92140        if( rc==SQLITE_OK ){
92141          sqlite3PagerSetJournalMode(pPager, eNew);
92142        }
92143      }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
92144        /* Cannot transition directly from MEMORY to WAL.  Use mode OFF
92145        ** as an intermediate */
92146        sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
92147      }
92148
92149      /* Open a transaction on the database file. Regardless of the journal
92150      ** mode, this transaction always uses a rollback journal.
92151      */
92152      assert( sqlite3BtreeIsInTrans(pBt)==0 );
92153      if( rc==SQLITE_OK ){
92154        rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
92155      }
92156    }
92157  }
92158#endif /* ifndef SQLITE_OMIT_WAL */
92159
92160  if( rc ) eNew = eOld;
92161  eNew = sqlite3PagerSetJournalMode(pPager, eNew);
92162
92163  pOut->flags = MEM_Str|MEM_Static|MEM_Term;
92164  pOut->z = (char *)sqlite3JournalModename(eNew);
92165  pOut->n = sqlite3Strlen30(pOut->z);
92166  pOut->enc = SQLITE_UTF8;
92167  sqlite3VdbeChangeEncoding(pOut, encoding);
92168  if( rc ) goto abort_due_to_error;
92169  break;
92170};
92171#endif /* SQLITE_OMIT_PRAGMA */
92172
92173#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
92174/* Opcode: Vacuum P1 P2 * * *
92175**
92176** Vacuum the entire database P1.  P1 is 0 for "main", and 2 or more
92177** for an attached database.  The "temp" database may not be vacuumed.
92178**
92179** If P2 is not zero, then it is a register holding a string which is
92180** the file into which the result of vacuum should be written.  When
92181** P2 is zero, the vacuum overwrites the original database.
92182*/
92183case OP_Vacuum: {
92184  assert( p->readOnly==0 );
92185  rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
92186                        pOp->p2 ? &aMem[pOp->p2] : 0);
92187  if( rc ) goto abort_due_to_error;
92188  break;
92189}
92190#endif
92191
92192#if !defined(SQLITE_OMIT_AUTOVACUUM)
92193/* Opcode: IncrVacuum P1 P2 * * *
92194**
92195** Perform a single step of the incremental vacuum procedure on
92196** the P1 database. If the vacuum has finished, jump to instruction
92197** P2. Otherwise, fall through to the next instruction.
92198*/
92199case OP_IncrVacuum: {        /* jump */
92200  Btree *pBt;
92201
92202  assert( pOp->p1>=0 && pOp->p1<db->nDb );
92203  assert( DbMaskTest(p->btreeMask, pOp->p1) );
92204  assert( p->readOnly==0 );
92205  pBt = db->aDb[pOp->p1].pBt;
92206  rc = sqlite3BtreeIncrVacuum(pBt);
92207  VdbeBranchTaken(rc==SQLITE_DONE,2);
92208  if( rc ){
92209    if( rc!=SQLITE_DONE ) goto abort_due_to_error;
92210    rc = SQLITE_OK;
92211    goto jump_to_p2;
92212  }
92213  break;
92214}
92215#endif
92216
92217/* Opcode: Expire P1 P2 * * *
92218**
92219** Cause precompiled statements to expire.  When an expired statement
92220** is executed using sqlite3_step() it will either automatically
92221** reprepare itself (if it was originally created using sqlite3_prepare_v2())
92222** or it will fail with SQLITE_SCHEMA.
92223**
92224** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
92225** then only the currently executing statement is expired.
92226**
92227** If P2 is 0, then SQL statements are expired immediately.  If P2 is 1,
92228** then running SQL statements are allowed to continue to run to completion.
92229** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
92230** that might help the statement run faster but which does not affect the
92231** correctness of operation.
92232*/
92233case OP_Expire: {
92234  assert( pOp->p2==0 || pOp->p2==1 );
92235  if( !pOp->p1 ){
92236    sqlite3ExpirePreparedStatements(db, pOp->p2);
92237  }else{
92238    p->expired = pOp->p2+1;
92239  }
92240  break;
92241}
92242
92243/* Opcode: CursorLock P1 * * * *
92244**
92245** Lock the btree to which cursor P1 is pointing so that the btree cannot be
92246** written by an other cursor.
92247*/
92248case OP_CursorLock: {
92249  VdbeCursor *pC;
92250  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92251  pC = p->apCsr[pOp->p1];
92252  assert( pC!=0 );
92253  assert( pC->eCurType==CURTYPE_BTREE );
92254  sqlite3BtreeCursorPin(pC->uc.pCursor);
92255  break;
92256}
92257
92258/* Opcode: CursorUnlock P1 * * * *
92259**
92260** Unlock the btree to which cursor P1 is pointing so that it can be
92261** written by other cursors.
92262*/
92263case OP_CursorUnlock: {
92264  VdbeCursor *pC;
92265  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92266  pC = p->apCsr[pOp->p1];
92267  assert( pC!=0 );
92268  assert( pC->eCurType==CURTYPE_BTREE );
92269  sqlite3BtreeCursorUnpin(pC->uc.pCursor);
92270  break;
92271}
92272
92273#ifndef SQLITE_OMIT_SHARED_CACHE
92274/* Opcode: TableLock P1 P2 P3 P4 *
92275** Synopsis: iDb=P1 root=P2 write=P3
92276**
92277** Obtain a lock on a particular table. This instruction is only used when
92278** the shared-cache feature is enabled.
92279**
92280** P1 is the index of the database in sqlite3.aDb[] of the database
92281** on which the lock is acquired.  A readlock is obtained if P3==0 or
92282** a write lock if P3==1.
92283**
92284** P2 contains the root-page of the table to lock.
92285**
92286** P4 contains a pointer to the name of the table being locked. This is only
92287** used to generate an error message if the lock cannot be obtained.
92288*/
92289case OP_TableLock: {
92290  u8 isWriteLock = (u8)pOp->p3;
92291  if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
92292    int p1 = pOp->p1;
92293    assert( p1>=0 && p1<db->nDb );
92294    assert( DbMaskTest(p->btreeMask, p1) );
92295    assert( isWriteLock==0 || isWriteLock==1 );
92296    rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
92297    if( rc ){
92298      if( (rc&0xFF)==SQLITE_LOCKED ){
92299        const char *z = pOp->p4.z;
92300        sqlite3VdbeError(p, "database table is locked: %s", z);
92301      }
92302      goto abort_due_to_error;
92303    }
92304  }
92305  break;
92306}
92307#endif /* SQLITE_OMIT_SHARED_CACHE */
92308
92309#ifndef SQLITE_OMIT_VIRTUALTABLE
92310/* Opcode: VBegin * * * P4 *
92311**
92312** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
92313** xBegin method for that table.
92314**
92315** Also, whether or not P4 is set, check that this is not being called from
92316** within a callback to a virtual table xSync() method. If it is, the error
92317** code will be set to SQLITE_LOCKED.
92318*/
92319case OP_VBegin: {
92320  VTable *pVTab;
92321  pVTab = pOp->p4.pVtab;
92322  rc = sqlite3VtabBegin(db, pVTab);
92323  if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
92324  if( rc ) goto abort_due_to_error;
92325  break;
92326}
92327#endif /* SQLITE_OMIT_VIRTUALTABLE */
92328
92329#ifndef SQLITE_OMIT_VIRTUALTABLE
92330/* Opcode: VCreate P1 P2 * * *
92331**
92332** P2 is a register that holds the name of a virtual table in database
92333** P1. Call the xCreate method for that table.
92334*/
92335case OP_VCreate: {
92336  Mem sMem;          /* For storing the record being decoded */
92337  const char *zTab;  /* Name of the virtual table */
92338
92339  memset(&sMem, 0, sizeof(sMem));
92340  sMem.db = db;
92341  /* Because P2 is always a static string, it is impossible for the
92342  ** sqlite3VdbeMemCopy() to fail */
92343  assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
92344  assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
92345  rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
92346  assert( rc==SQLITE_OK );
92347  zTab = (const char*)sqlite3_value_text(&sMem);
92348  assert( zTab || db->mallocFailed );
92349  if( zTab ){
92350    rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
92351  }
92352  sqlite3VdbeMemRelease(&sMem);
92353  if( rc ) goto abort_due_to_error;
92354  break;
92355}
92356#endif /* SQLITE_OMIT_VIRTUALTABLE */
92357
92358#ifndef SQLITE_OMIT_VIRTUALTABLE
92359/* Opcode: VDestroy P1 * * P4 *
92360**
92361** P4 is the name of a virtual table in database P1.  Call the xDestroy method
92362** of that table.
92363*/
92364case OP_VDestroy: {
92365  db->nVDestroy++;
92366  rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
92367  db->nVDestroy--;
92368  assert( p->errorAction==OE_Abort && p->usesStmtJournal );
92369  if( rc ) goto abort_due_to_error;
92370  break;
92371}
92372#endif /* SQLITE_OMIT_VIRTUALTABLE */
92373
92374#ifndef SQLITE_OMIT_VIRTUALTABLE
92375/* Opcode: VOpen P1 * * P4 *
92376**
92377** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
92378** P1 is a cursor number.  This opcode opens a cursor to the virtual
92379** table and stores that cursor in P1.
92380*/
92381case OP_VOpen: {
92382  VdbeCursor *pCur;
92383  sqlite3_vtab_cursor *pVCur;
92384  sqlite3_vtab *pVtab;
92385  const sqlite3_module *pModule;
92386
92387  assert( p->bIsReader );
92388  pCur = 0;
92389  pVCur = 0;
92390  pVtab = pOp->p4.pVtab->pVtab;
92391  if( pVtab==0 || NEVER(pVtab->pModule==0) ){
92392    rc = SQLITE_LOCKED;
92393    goto abort_due_to_error;
92394  }
92395  pModule = pVtab->pModule;
92396  rc = pModule->xOpen(pVtab, &pVCur);
92397  sqlite3VtabImportErrmsg(p, pVtab);
92398  if( rc ) goto abort_due_to_error;
92399
92400  /* Initialize sqlite3_vtab_cursor base class */
92401  pVCur->pVtab = pVtab;
92402
92403  /* Initialize vdbe cursor object */
92404  pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
92405  if( pCur ){
92406    pCur->uc.pVCur = pVCur;
92407    pVtab->nRef++;
92408  }else{
92409    assert( db->mallocFailed );
92410    pModule->xClose(pVCur);
92411    goto no_mem;
92412  }
92413  break;
92414}
92415#endif /* SQLITE_OMIT_VIRTUALTABLE */
92416
92417#ifndef SQLITE_OMIT_VIRTUALTABLE
92418/* Opcode: VFilter P1 P2 P3 P4 *
92419** Synopsis: iplan=r[P3] zplan='P4'
92420**
92421** P1 is a cursor opened using VOpen.  P2 is an address to jump to if
92422** the filtered result set is empty.
92423**
92424** P4 is either NULL or a string that was generated by the xBestIndex
92425** method of the module.  The interpretation of the P4 string is left
92426** to the module implementation.
92427**
92428** This opcode invokes the xFilter method on the virtual table specified
92429** by P1.  The integer query plan parameter to xFilter is stored in register
92430** P3. Register P3+1 stores the argc parameter to be passed to the
92431** xFilter method. Registers P3+2..P3+1+argc are the argc
92432** additional parameters which are passed to
92433** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
92434**
92435** A jump is made to P2 if the result set after filtering would be empty.
92436*/
92437case OP_VFilter: {   /* jump */
92438  int nArg;
92439  int iQuery;
92440  const sqlite3_module *pModule;
92441  Mem *pQuery;
92442  Mem *pArgc;
92443  sqlite3_vtab_cursor *pVCur;
92444  sqlite3_vtab *pVtab;
92445  VdbeCursor *pCur;
92446  int res;
92447  int i;
92448  Mem **apArg;
92449
92450  pQuery = &aMem[pOp->p3];
92451  pArgc = &pQuery[1];
92452  pCur = p->apCsr[pOp->p1];
92453  assert( memIsValid(pQuery) );
92454  REGISTER_TRACE(pOp->p3, pQuery);
92455  assert( pCur->eCurType==CURTYPE_VTAB );
92456  pVCur = pCur->uc.pVCur;
92457  pVtab = pVCur->pVtab;
92458  pModule = pVtab->pModule;
92459
92460  /* Grab the index number and argc parameters */
92461  assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
92462  nArg = (int)pArgc->u.i;
92463  iQuery = (int)pQuery->u.i;
92464
92465  /* Invoke the xFilter method */
92466  res = 0;
92467  apArg = p->apArg;
92468  for(i = 0; i<nArg; i++){
92469    apArg[i] = &pArgc[i+1];
92470  }
92471  rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
92472  sqlite3VtabImportErrmsg(p, pVtab);
92473  if( rc ) goto abort_due_to_error;
92474  res = pModule->xEof(pVCur);
92475  pCur->nullRow = 0;
92476  VdbeBranchTaken(res!=0,2);
92477  if( res ) goto jump_to_p2;
92478  break;
92479}
92480#endif /* SQLITE_OMIT_VIRTUALTABLE */
92481
92482#ifndef SQLITE_OMIT_VIRTUALTABLE
92483/* Opcode: VColumn P1 P2 P3 * P5
92484** Synopsis: r[P3]=vcolumn(P2)
92485**
92486** Store in register P3 the value of the P2-th column of
92487** the current row of the virtual-table of cursor P1.
92488**
92489** If the VColumn opcode is being used to fetch the value of
92490** an unchanging column during an UPDATE operation, then the P5
92491** value is OPFLAG_NOCHNG.  This will cause the sqlite3_vtab_nochange()
92492** function to return true inside the xColumn method of the virtual
92493** table implementation.  The P5 column might also contain other
92494** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
92495** unused by OP_VColumn.
92496*/
92497case OP_VColumn: {
92498  sqlite3_vtab *pVtab;
92499  const sqlite3_module *pModule;
92500  Mem *pDest;
92501  sqlite3_context sContext;
92502
92503  VdbeCursor *pCur = p->apCsr[pOp->p1];
92504  assert( pCur->eCurType==CURTYPE_VTAB );
92505  assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
92506  pDest = &aMem[pOp->p3];
92507  memAboutToChange(p, pDest);
92508  if( pCur->nullRow ){
92509    sqlite3VdbeMemSetNull(pDest);
92510    break;
92511  }
92512  pVtab = pCur->uc.pVCur->pVtab;
92513  pModule = pVtab->pModule;
92514  assert( pModule->xColumn );
92515  memset(&sContext, 0, sizeof(sContext));
92516  sContext.pOut = pDest;
92517  assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
92518  if( pOp->p5 & OPFLAG_NOCHNG ){
92519    sqlite3VdbeMemSetNull(pDest);
92520    pDest->flags = MEM_Null|MEM_Zero;
92521    pDest->u.nZero = 0;
92522  }else{
92523    MemSetTypeFlag(pDest, MEM_Null);
92524  }
92525  rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
92526  sqlite3VtabImportErrmsg(p, pVtab);
92527  if( sContext.isError>0 ){
92528    sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
92529    rc = sContext.isError;
92530  }
92531  sqlite3VdbeChangeEncoding(pDest, encoding);
92532  REGISTER_TRACE(pOp->p3, pDest);
92533  UPDATE_MAX_BLOBSIZE(pDest);
92534
92535  if( sqlite3VdbeMemTooBig(pDest) ){
92536    goto too_big;
92537  }
92538  if( rc ) goto abort_due_to_error;
92539  break;
92540}
92541#endif /* SQLITE_OMIT_VIRTUALTABLE */
92542
92543#ifndef SQLITE_OMIT_VIRTUALTABLE
92544/* Opcode: VNext P1 P2 * * *
92545**
92546** Advance virtual table P1 to the next row in its result set and
92547** jump to instruction P2.  Or, if the virtual table has reached
92548** the end of its result set, then fall through to the next instruction.
92549*/
92550case OP_VNext: {   /* jump */
92551  sqlite3_vtab *pVtab;
92552  const sqlite3_module *pModule;
92553  int res;
92554  VdbeCursor *pCur;
92555
92556  res = 0;
92557  pCur = p->apCsr[pOp->p1];
92558  assert( pCur->eCurType==CURTYPE_VTAB );
92559  if( pCur->nullRow ){
92560    break;
92561  }
92562  pVtab = pCur->uc.pVCur->pVtab;
92563  pModule = pVtab->pModule;
92564  assert( pModule->xNext );
92565
92566  /* Invoke the xNext() method of the module. There is no way for the
92567  ** underlying implementation to return an error if one occurs during
92568  ** xNext(). Instead, if an error occurs, true is returned (indicating that
92569  ** data is available) and the error code returned when xColumn or
92570  ** some other method is next invoked on the save virtual table cursor.
92571  */
92572  rc = pModule->xNext(pCur->uc.pVCur);
92573  sqlite3VtabImportErrmsg(p, pVtab);
92574  if( rc ) goto abort_due_to_error;
92575  res = pModule->xEof(pCur->uc.pVCur);
92576  VdbeBranchTaken(!res,2);
92577  if( !res ){
92578    /* If there is data, jump to P2 */
92579    goto jump_to_p2_and_check_for_interrupt;
92580  }
92581  goto check_for_interrupt;
92582}
92583#endif /* SQLITE_OMIT_VIRTUALTABLE */
92584
92585#ifndef SQLITE_OMIT_VIRTUALTABLE
92586/* Opcode: VRename P1 * * P4 *
92587**
92588** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
92589** This opcode invokes the corresponding xRename method. The value
92590** in register P1 is passed as the zName argument to the xRename method.
92591*/
92592case OP_VRename: {
92593  sqlite3_vtab *pVtab;
92594  Mem *pName;
92595  int isLegacy;
92596
92597  isLegacy = (db->flags & SQLITE_LegacyAlter);
92598  db->flags |= SQLITE_LegacyAlter;
92599  pVtab = pOp->p4.pVtab->pVtab;
92600  pName = &aMem[pOp->p1];
92601  assert( pVtab->pModule->xRename );
92602  assert( memIsValid(pName) );
92603  assert( p->readOnly==0 );
92604  REGISTER_TRACE(pOp->p1, pName);
92605  assert( pName->flags & MEM_Str );
92606  testcase( pName->enc==SQLITE_UTF8 );
92607  testcase( pName->enc==SQLITE_UTF16BE );
92608  testcase( pName->enc==SQLITE_UTF16LE );
92609  rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
92610  if( rc ) goto abort_due_to_error;
92611  rc = pVtab->pModule->xRename(pVtab, pName->z);
92612  if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
92613  sqlite3VtabImportErrmsg(p, pVtab);
92614  p->expired = 0;
92615  if( rc ) goto abort_due_to_error;
92616  break;
92617}
92618#endif
92619
92620#ifndef SQLITE_OMIT_VIRTUALTABLE
92621/* Opcode: VUpdate P1 P2 P3 P4 P5
92622** Synopsis: data=r[P3@P2]
92623**
92624** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
92625** This opcode invokes the corresponding xUpdate method. P2 values
92626** are contiguous memory cells starting at P3 to pass to the xUpdate
92627** invocation. The value in register (P3+P2-1) corresponds to the
92628** p2th element of the argv array passed to xUpdate.
92629**
92630** The xUpdate method will do a DELETE or an INSERT or both.
92631** The argv[0] element (which corresponds to memory cell P3)
92632** is the rowid of a row to delete.  If argv[0] is NULL then no
92633** deletion occurs.  The argv[1] element is the rowid of the new
92634** row.  This can be NULL to have the virtual table select the new
92635** rowid for itself.  The subsequent elements in the array are
92636** the values of columns in the new row.
92637**
92638** If P2==1 then no insert is performed.  argv[0] is the rowid of
92639** a row to delete.
92640**
92641** P1 is a boolean flag. If it is set to true and the xUpdate call
92642** is successful, then the value returned by sqlite3_last_insert_rowid()
92643** is set to the value of the rowid for the row just inserted.
92644**
92645** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
92646** apply in the case of a constraint failure on an insert or update.
92647*/
92648case OP_VUpdate: {
92649  sqlite3_vtab *pVtab;
92650  const sqlite3_module *pModule;
92651  int nArg;
92652  int i;
92653  sqlite_int64 rowid;
92654  Mem **apArg;
92655  Mem *pX;
92656
92657  assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback
92658       || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
92659  );
92660  assert( p->readOnly==0 );
92661  if( db->mallocFailed ) goto no_mem;
92662  sqlite3VdbeIncrWriteCounter(p, 0);
92663  pVtab = pOp->p4.pVtab->pVtab;
92664  if( pVtab==0 || NEVER(pVtab->pModule==0) ){
92665    rc = SQLITE_LOCKED;
92666    goto abort_due_to_error;
92667  }
92668  pModule = pVtab->pModule;
92669  nArg = pOp->p2;
92670  assert( pOp->p4type==P4_VTAB );
92671  if( ALWAYS(pModule->xUpdate) ){
92672    u8 vtabOnConflict = db->vtabOnConflict;
92673    apArg = p->apArg;
92674    pX = &aMem[pOp->p3];
92675    for(i=0; i<nArg; i++){
92676      assert( memIsValid(pX) );
92677      memAboutToChange(p, pX);
92678      apArg[i] = pX;
92679      pX++;
92680    }
92681    db->vtabOnConflict = pOp->p5;
92682    rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
92683    db->vtabOnConflict = vtabOnConflict;
92684    sqlite3VtabImportErrmsg(p, pVtab);
92685    if( rc==SQLITE_OK && pOp->p1 ){
92686      assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
92687      db->lastRowid = rowid;
92688    }
92689    if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
92690      if( pOp->p5==OE_Ignore ){
92691        rc = SQLITE_OK;
92692      }else{
92693        p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
92694      }
92695    }else{
92696      p->nChange++;
92697    }
92698    if( rc ) goto abort_due_to_error;
92699  }
92700  break;
92701}
92702#endif /* SQLITE_OMIT_VIRTUALTABLE */
92703
92704#ifndef  SQLITE_OMIT_PAGER_PRAGMAS
92705/* Opcode: Pagecount P1 P2 * * *
92706**
92707** Write the current number of pages in database P1 to memory cell P2.
92708*/
92709case OP_Pagecount: {            /* out2 */
92710  pOut = out2Prerelease(p, pOp);
92711  pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
92712  break;
92713}
92714#endif
92715
92716
92717#ifndef  SQLITE_OMIT_PAGER_PRAGMAS
92718/* Opcode: MaxPgcnt P1 P2 P3 * *
92719**
92720** Try to set the maximum page count for database P1 to the value in P3.
92721** Do not let the maximum page count fall below the current page count and
92722** do not change the maximum page count value if P3==0.
92723**
92724** Store the maximum page count after the change in register P2.
92725*/
92726case OP_MaxPgcnt: {            /* out2 */
92727  unsigned int newMax;
92728  Btree *pBt;
92729
92730  pOut = out2Prerelease(p, pOp);
92731  pBt = db->aDb[pOp->p1].pBt;
92732  newMax = 0;
92733  if( pOp->p3 ){
92734    newMax = sqlite3BtreeLastPage(pBt);
92735    if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
92736  }
92737  pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
92738  break;
92739}
92740#endif
92741
92742/* Opcode: Function P1 P2 P3 P4 *
92743** Synopsis: r[P3]=func(r[P2@NP])
92744**
92745** Invoke a user function (P4 is a pointer to an sqlite3_context object that
92746** contains a pointer to the function to be run) with arguments taken
92747** from register P2 and successors.  The number of arguments is in
92748** the sqlite3_context object that P4 points to.
92749** The result of the function is stored
92750** in register P3.  Register P3 must not be one of the function inputs.
92751**
92752** P1 is a 32-bit bitmask indicating whether or not each argument to the
92753** function was determined to be constant at compile time. If the first
92754** argument was constant then bit 0 of P1 is set. This is used to determine
92755** whether meta data associated with a user function argument using the
92756** sqlite3_set_auxdata() API may be safely retained until the next
92757** invocation of this opcode.
92758**
92759** See also: AggStep, AggFinal, PureFunc
92760*/
92761/* Opcode: PureFunc P1 P2 P3 P4 *
92762** Synopsis: r[P3]=func(r[P2@NP])
92763**
92764** Invoke a user function (P4 is a pointer to an sqlite3_context object that
92765** contains a pointer to the function to be run) with arguments taken
92766** from register P2 and successors.  The number of arguments is in
92767** the sqlite3_context object that P4 points to.
92768** The result of the function is stored
92769** in register P3.  Register P3 must not be one of the function inputs.
92770**
92771** P1 is a 32-bit bitmask indicating whether or not each argument to the
92772** function was determined to be constant at compile time. If the first
92773** argument was constant then bit 0 of P1 is set. This is used to determine
92774** whether meta data associated with a user function argument using the
92775** sqlite3_set_auxdata() API may be safely retained until the next
92776** invocation of this opcode.
92777**
92778** This opcode works exactly like OP_Function.  The only difference is in
92779** its name.  This opcode is used in places where the function must be
92780** purely non-deterministic.  Some built-in date/time functions can be
92781** either determinitic of non-deterministic, depending on their arguments.
92782** When those function are used in a non-deterministic way, they will check
92783** to see if they were called using OP_PureFunc instead of OP_Function, and
92784** if they were, they throw an error.
92785**
92786** See also: AggStep, AggFinal, Function
92787*/
92788case OP_PureFunc:              /* group */
92789case OP_Function: {            /* group */
92790  int i;
92791  sqlite3_context *pCtx;
92792
92793  assert( pOp->p4type==P4_FUNCCTX );
92794  pCtx = pOp->p4.pCtx;
92795
92796  /* If this function is inside of a trigger, the register array in aMem[]
92797  ** might change from one evaluation to the next.  The next block of code
92798  ** checks to see if the register array has changed, and if so it
92799  ** reinitializes the relavant parts of the sqlite3_context object */
92800  pOut = &aMem[pOp->p3];
92801  if( pCtx->pOut != pOut ){
92802    pCtx->pVdbe = p;
92803    pCtx->pOut = pOut;
92804    for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
92805  }
92806  assert( pCtx->pVdbe==p );
92807
92808  memAboutToChange(p, pOut);
92809#ifdef SQLITE_DEBUG
92810  for(i=0; i<pCtx->argc; i++){
92811    assert( memIsValid(pCtx->argv[i]) );
92812    REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
92813  }
92814#endif
92815  MemSetTypeFlag(pOut, MEM_Null);
92816  assert( pCtx->isError==0 );
92817  (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
92818
92819  /* If the function returned an error, throw an exception */
92820  if( pCtx->isError ){
92821    if( pCtx->isError>0 ){
92822      sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
92823      rc = pCtx->isError;
92824    }
92825    sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
92826    pCtx->isError = 0;
92827    if( rc ) goto abort_due_to_error;
92828  }
92829
92830  /* Copy the result of the function into register P3 */
92831  if( pOut->flags & (MEM_Str|MEM_Blob) ){
92832    sqlite3VdbeChangeEncoding(pOut, encoding);
92833    if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
92834  }
92835
92836  REGISTER_TRACE(pOp->p3, pOut);
92837  UPDATE_MAX_BLOBSIZE(pOut);
92838  break;
92839}
92840
92841/* Opcode: Trace P1 P2 * P4 *
92842**
92843** Write P4 on the statement trace output if statement tracing is
92844** enabled.
92845**
92846** Operand P1 must be 0x7fffffff and P2 must positive.
92847*/
92848/* Opcode: Init P1 P2 P3 P4 *
92849** Synopsis: Start at P2
92850**
92851** Programs contain a single instance of this opcode as the very first
92852** opcode.
92853**
92854** If tracing is enabled (by the sqlite3_trace()) interface, then
92855** the UTF-8 string contained in P4 is emitted on the trace callback.
92856** Or if P4 is blank, use the string returned by sqlite3_sql().
92857**
92858** If P2 is not zero, jump to instruction P2.
92859**
92860** Increment the value of P1 so that OP_Once opcodes will jump the
92861** first time they are evaluated for this run.
92862**
92863** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
92864** error is encountered.
92865*/
92866case OP_Trace:
92867case OP_Init: {          /* jump */
92868  int i;
92869#ifndef SQLITE_OMIT_TRACE
92870  char *zTrace;
92871#endif
92872
92873  /* If the P4 argument is not NULL, then it must be an SQL comment string.
92874  ** The "--" string is broken up to prevent false-positives with srcck1.c.
92875  **
92876  ** This assert() provides evidence for:
92877  ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
92878  ** would have been returned by the legacy sqlite3_trace() interface by
92879  ** using the X argument when X begins with "--" and invoking
92880  ** sqlite3_expanded_sql(P) otherwise.
92881  */
92882  assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
92883
92884  /* OP_Init is always instruction 0 */
92885  assert( pOp==p->aOp || pOp->opcode==OP_Trace );
92886
92887#ifndef SQLITE_OMIT_TRACE
92888  if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
92889   && !p->doingRerun
92890   && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
92891  ){
92892#ifndef SQLITE_OMIT_DEPRECATED
92893    if( db->mTrace & SQLITE_TRACE_LEGACY ){
92894      void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
92895      char *z = sqlite3VdbeExpandSql(p, zTrace);
92896      x(db->pTraceArg, z);
92897      sqlite3_free(z);
92898    }else
92899#endif
92900    if( db->nVdbeExec>1 ){
92901      char *z = sqlite3MPrintf(db, "-- %s", zTrace);
92902      (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
92903      sqlite3DbFree(db, z);
92904    }else{
92905      (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
92906    }
92907  }
92908#ifdef SQLITE_USE_FCNTL_TRACE
92909  zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
92910  if( zTrace ){
92911    int j;
92912    for(j=0; j<db->nDb; j++){
92913      if( DbMaskTest(p->btreeMask, j)==0 ) continue;
92914      sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
92915    }
92916  }
92917#endif /* SQLITE_USE_FCNTL_TRACE */
92918#ifdef SQLITE_DEBUG
92919  if( (db->flags & SQLITE_SqlTrace)!=0
92920   && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
92921  ){
92922    sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
92923  }
92924#endif /* SQLITE_DEBUG */
92925#endif /* SQLITE_OMIT_TRACE */
92926  assert( pOp->p2>0 );
92927  if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
92928    if( pOp->opcode==OP_Trace ) break;
92929    for(i=1; i<p->nOp; i++){
92930      if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
92931    }
92932    pOp->p1 = 0;
92933  }
92934  pOp->p1++;
92935  p->aCounter[SQLITE_STMTSTATUS_RUN]++;
92936  goto jump_to_p2;
92937}
92938
92939#ifdef SQLITE_ENABLE_CURSOR_HINTS
92940/* Opcode: CursorHint P1 * * P4 *
92941**
92942** Provide a hint to cursor P1 that it only needs to return rows that
92943** satisfy the Expr in P4.  TK_REGISTER terms in the P4 expression refer
92944** to values currently held in registers.  TK_COLUMN terms in the P4
92945** expression refer to columns in the b-tree to which cursor P1 is pointing.
92946*/
92947case OP_CursorHint: {
92948  VdbeCursor *pC;
92949
92950  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92951  assert( pOp->p4type==P4_EXPR );
92952  pC = p->apCsr[pOp->p1];
92953  if( pC ){
92954    assert( pC->eCurType==CURTYPE_BTREE );
92955    sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
92956                           pOp->p4.pExpr, aMem);
92957  }
92958  break;
92959}
92960#endif /* SQLITE_ENABLE_CURSOR_HINTS */
92961
92962#ifdef SQLITE_DEBUG
92963/* Opcode:  Abortable   * * * * *
92964**
92965** Verify that an Abort can happen.  Assert if an Abort at this point
92966** might cause database corruption.  This opcode only appears in debugging
92967** builds.
92968**
92969** An Abort is safe if either there have been no writes, or if there is
92970** an active statement journal.
92971*/
92972case OP_Abortable: {
92973  sqlite3VdbeAssertAbortable(p);
92974  break;
92975}
92976#endif
92977
92978#ifdef SQLITE_DEBUG
92979/* Opcode:  ReleaseReg   P1 P2 P3 * P5
92980** Synopsis: release r[P1@P2] mask P3
92981**
92982** Release registers from service.  Any content that was in the
92983** the registers is unreliable after this opcode completes.
92984**
92985** The registers released will be the P2 registers starting at P1,
92986** except if bit ii of P3 set, then do not release register P1+ii.
92987** In other words, P3 is a mask of registers to preserve.
92988**
92989** Releasing a register clears the Mem.pScopyFrom pointer.  That means
92990** that if the content of the released register was set using OP_SCopy,
92991** a change to the value of the source register for the OP_SCopy will no longer
92992** generate an assertion fault in sqlite3VdbeMemAboutToChange().
92993**
92994** If P5 is set, then all released registers have their type set
92995** to MEM_Undefined so that any subsequent attempt to read the released
92996** register (before it is reinitialized) will generate an assertion fault.
92997**
92998** P5 ought to be set on every call to this opcode.
92999** However, there are places in the code generator will release registers
93000** before their are used, under the (valid) assumption that the registers
93001** will not be reallocated for some other purpose before they are used and
93002** hence are safe to release.
93003**
93004** This opcode is only available in testing and debugging builds.  It is
93005** not generated for release builds.  The purpose of this opcode is to help
93006** validate the generated bytecode.  This opcode does not actually contribute
93007** to computing an answer.
93008*/
93009case OP_ReleaseReg: {
93010  Mem *pMem;
93011  int i;
93012  u32 constMask;
93013  assert( pOp->p1>0 );
93014  assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
93015  pMem = &aMem[pOp->p1];
93016  constMask = pOp->p3;
93017  for(i=0; i<pOp->p2; i++, pMem++){
93018    if( i>=32 || (constMask & MASKBIT32(i))==0 ){
93019      pMem->pScopyFrom = 0;
93020      if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
93021    }
93022  }
93023  break;
93024}
93025#endif
93026
93027/* Opcode: Noop * * * * *
93028**
93029** Do nothing.  This instruction is often useful as a jump
93030** destination.
93031*/
93032/*
93033** The magic Explain opcode are only inserted when explain==2 (which
93034** is to say when the EXPLAIN QUERY PLAN syntax is used.)
93035** This opcode records information from the optimizer.  It is the
93036** the same as a no-op.  This opcodesnever appears in a real VM program.
93037*/
93038default: {          /* This is really OP_Noop, OP_Explain */
93039  assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
93040
93041  break;
93042}
93043
93044/*****************************************************************************
93045** The cases of the switch statement above this line should all be indented
93046** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
93047** readability.  From this point on down, the normal indentation rules are
93048** restored.
93049*****************************************************************************/
93050    }
93051
93052#ifdef VDBE_PROFILE
93053    {
93054      u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
93055      if( endTime>start ) pOrigOp->cycles += endTime - start;
93056      pOrigOp->cnt++;
93057    }
93058#endif
93059
93060    /* The following code adds nothing to the actual functionality
93061    ** of the program.  It is only here for testing and debugging.
93062    ** On the other hand, it does burn CPU cycles every time through
93063    ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
93064    */
93065#ifndef NDEBUG
93066    assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
93067
93068#ifdef SQLITE_DEBUG
93069    if( db->flags & SQLITE_VdbeTrace ){
93070      u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
93071      if( rc!=0 ) printf("rc=%d\n",rc);
93072      if( opProperty & (OPFLG_OUT2) ){
93073        registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
93074      }
93075      if( opProperty & OPFLG_OUT3 ){
93076        registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
93077      }
93078      if( opProperty==0xff ){
93079        /* Never happens.  This code exists to avoid a harmless linkage
93080        ** warning aboud sqlite3VdbeRegisterDump() being defined but not
93081        ** used. */
93082        sqlite3VdbeRegisterDump(p);
93083      }
93084    }
93085#endif  /* SQLITE_DEBUG */
93086#endif  /* NDEBUG */
93087  }  /* The end of the for(;;) loop the loops through opcodes */
93088
93089  /* If we reach this point, it means that execution is finished with
93090  ** an error of some kind.
93091  */
93092abort_due_to_error:
93093  if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
93094  assert( rc );
93095  if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
93096    sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
93097  }
93098  p->rc = rc;
93099  sqlite3SystemError(db, rc);
93100  testcase( sqlite3GlobalConfig.xLog!=0 );
93101  sqlite3_log(rc, "statement aborts at %d: [%s] %s",
93102                   (int)(pOp - aOp), p->zSql, p->zErrMsg);
93103  sqlite3VdbeHalt(p);
93104  if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
93105  rc = SQLITE_ERROR;
93106  if( resetSchemaOnFault>0 ){
93107    sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
93108  }
93109
93110  /* This is the only way out of this procedure.  We have to
93111  ** release the mutexes on btrees that were acquired at the
93112  ** top. */
93113vdbe_return:
93114#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
93115  while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
93116    nProgressLimit += db->nProgressOps;
93117    if( db->xProgress(db->pProgressArg) ){
93118      nProgressLimit = 0xffffffff;
93119      rc = SQLITE_INTERRUPT;
93120      goto abort_due_to_error;
93121    }
93122  }
93123#endif
93124  p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
93125  sqlite3VdbeLeave(p);
93126  assert( rc!=SQLITE_OK || nExtraDelete==0
93127       || sqlite3_strlike("DELETE%",p->zSql,0)!=0
93128  );
93129  return rc;
93130
93131  /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
93132  ** is encountered.
93133  */
93134too_big:
93135  sqlite3VdbeError(p, "string or blob too big");
93136  rc = SQLITE_TOOBIG;
93137  goto abort_due_to_error;
93138
93139  /* Jump to here if a malloc() fails.
93140  */
93141no_mem:
93142  sqlite3OomFault(db);
93143  sqlite3VdbeError(p, "out of memory");
93144  rc = SQLITE_NOMEM_BKPT;
93145  goto abort_due_to_error;
93146
93147  /* Jump to here if the sqlite3_interrupt() API sets the interrupt
93148  ** flag.
93149  */
93150abort_due_to_interrupt:
93151  assert( AtomicLoad(&db->u1.isInterrupted) );
93152  rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
93153  p->rc = rc;
93154  sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
93155  goto abort_due_to_error;
93156}
93157
93158
93159/************** End of vdbe.c ************************************************/
93160/************** Begin file vdbeblob.c ****************************************/
93161/*
93162** 2007 May 1
93163**
93164** The author disclaims copyright to this source code.  In place of
93165** a legal notice, here is a blessing:
93166**
93167**    May you do good and not evil.
93168**    May you find forgiveness for yourself and forgive others.
93169**    May you share freely, never taking more than you give.
93170**
93171*************************************************************************
93172**
93173** This file contains code used to implement incremental BLOB I/O.
93174*/
93175
93176/* #include "sqliteInt.h" */
93177/* #include "vdbeInt.h" */
93178
93179#ifndef SQLITE_OMIT_INCRBLOB
93180
93181/*
93182** Valid sqlite3_blob* handles point to Incrblob structures.
93183*/
93184typedef struct Incrblob Incrblob;
93185struct Incrblob {
93186  int nByte;              /* Size of open blob, in bytes */
93187  int iOffset;            /* Byte offset of blob in cursor data */
93188  u16 iCol;               /* Table column this handle is open on */
93189  BtCursor *pCsr;         /* Cursor pointing at blob row */
93190  sqlite3_stmt *pStmt;    /* Statement holding cursor open */
93191  sqlite3 *db;            /* The associated database */
93192  char *zDb;              /* Database name */
93193  Table *pTab;            /* Table object */
93194};
93195
93196
93197/*
93198** This function is used by both blob_open() and blob_reopen(). It seeks
93199** the b-tree cursor associated with blob handle p to point to row iRow.
93200** If successful, SQLITE_OK is returned and subsequent calls to
93201** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
93202**
93203** If an error occurs, or if the specified row does not exist or does not
93204** contain a value of type TEXT or BLOB in the column nominated when the
93205** blob handle was opened, then an error code is returned and *pzErr may
93206** be set to point to a buffer containing an error message. It is the
93207** responsibility of the caller to free the error message buffer using
93208** sqlite3DbFree().
93209**
93210** If an error does occur, then the b-tree cursor is closed. All subsequent
93211** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
93212** immediately return SQLITE_ABORT.
93213*/
93214static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
93215  int rc;                         /* Error code */
93216  char *zErr = 0;                 /* Error message */
93217  Vdbe *v = (Vdbe *)p->pStmt;
93218
93219  /* Set the value of register r[1] in the SQL statement to integer iRow.
93220  ** This is done directly as a performance optimization
93221  */
93222  v->aMem[1].flags = MEM_Int;
93223  v->aMem[1].u.i = iRow;
93224
93225  /* If the statement has been run before (and is paused at the OP_ResultRow)
93226  ** then back it up to the point where it does the OP_NotExists.  This could
93227  ** have been down with an extra OP_Goto, but simply setting the program
93228  ** counter is faster. */
93229  if( v->pc>4 ){
93230    v->pc = 4;
93231    assert( v->aOp[v->pc].opcode==OP_NotExists );
93232    rc = sqlite3VdbeExec(v);
93233  }else{
93234    rc = sqlite3_step(p->pStmt);
93235  }
93236  if( rc==SQLITE_ROW ){
93237    VdbeCursor *pC = v->apCsr[0];
93238    u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
93239    testcase( pC->nHdrParsed==p->iCol );
93240    testcase( pC->nHdrParsed==p->iCol+1 );
93241    if( type<12 ){
93242      zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
93243          type==0?"null": type==7?"real": "integer"
93244      );
93245      rc = SQLITE_ERROR;
93246      sqlite3_finalize(p->pStmt);
93247      p->pStmt = 0;
93248    }else{
93249      p->iOffset = pC->aType[p->iCol + pC->nField];
93250      p->nByte = sqlite3VdbeSerialTypeLen(type);
93251      p->pCsr =  pC->uc.pCursor;
93252      sqlite3BtreeIncrblobCursor(p->pCsr);
93253    }
93254  }
93255
93256  if( rc==SQLITE_ROW ){
93257    rc = SQLITE_OK;
93258  }else if( p->pStmt ){
93259    rc = sqlite3_finalize(p->pStmt);
93260    p->pStmt = 0;
93261    if( rc==SQLITE_OK ){
93262      zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
93263      rc = SQLITE_ERROR;
93264    }else{
93265      zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
93266    }
93267  }
93268
93269  assert( rc!=SQLITE_OK || zErr==0 );
93270  assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
93271
93272  *pzErr = zErr;
93273  return rc;
93274}
93275
93276/*
93277** Open a blob handle.
93278*/
93279SQLITE_API int sqlite3_blob_open(
93280  sqlite3* db,            /* The database connection */
93281  const char *zDb,        /* The attached database containing the blob */
93282  const char *zTable,     /* The table containing the blob */
93283  const char *zColumn,    /* The column containing the blob */
93284  sqlite_int64 iRow,      /* The row containing the glob */
93285  int wrFlag,             /* True -> read/write access, false -> read-only */
93286  sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
93287){
93288  int nAttempt = 0;
93289  int iCol;               /* Index of zColumn in row-record */
93290  int rc = SQLITE_OK;
93291  char *zErr = 0;
93292  Table *pTab;
93293  Incrblob *pBlob = 0;
93294  Parse sParse;
93295
93296#ifdef SQLITE_ENABLE_API_ARMOR
93297  if( ppBlob==0 ){
93298    return SQLITE_MISUSE_BKPT;
93299  }
93300#endif
93301  *ppBlob = 0;
93302#ifdef SQLITE_ENABLE_API_ARMOR
93303  if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
93304    return SQLITE_MISUSE_BKPT;
93305  }
93306#endif
93307  wrFlag = !!wrFlag;                /* wrFlag = (wrFlag ? 1 : 0); */
93308
93309  sqlite3_mutex_enter(db->mutex);
93310
93311  pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
93312  do {
93313    memset(&sParse, 0, sizeof(Parse));
93314    if( !pBlob ) goto blob_open_out;
93315    sParse.db = db;
93316    sqlite3DbFree(db, zErr);
93317    zErr = 0;
93318
93319    sqlite3BtreeEnterAll(db);
93320    pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb);
93321    if( pTab && IsVirtual(pTab) ){
93322      pTab = 0;
93323      sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable);
93324    }
93325    if( pTab && !HasRowid(pTab) ){
93326      pTab = 0;
93327      sqlite3ErrorMsg(&sParse, "cannot open table without rowid: %s", zTable);
93328    }
93329#ifndef SQLITE_OMIT_VIEW
93330    if( pTab && pTab->pSelect ){
93331      pTab = 0;
93332      sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable);
93333    }
93334#endif
93335    if( !pTab ){
93336      if( sParse.zErrMsg ){
93337        sqlite3DbFree(db, zErr);
93338        zErr = sParse.zErrMsg;
93339        sParse.zErrMsg = 0;
93340      }
93341      rc = SQLITE_ERROR;
93342      sqlite3BtreeLeaveAll(db);
93343      goto blob_open_out;
93344    }
93345    pBlob->pTab = pTab;
93346    pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
93347
93348    /* Now search pTab for the exact column. */
93349    for(iCol=0; iCol<pTab->nCol; iCol++) {
93350      if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
93351        break;
93352      }
93353    }
93354    if( iCol==pTab->nCol ){
93355      sqlite3DbFree(db, zErr);
93356      zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
93357      rc = SQLITE_ERROR;
93358      sqlite3BtreeLeaveAll(db);
93359      goto blob_open_out;
93360    }
93361
93362    /* If the value is being opened for writing, check that the
93363    ** column is not indexed, and that it is not part of a foreign key.
93364    */
93365    if( wrFlag ){
93366      const char *zFault = 0;
93367      Index *pIdx;
93368#ifndef SQLITE_OMIT_FOREIGN_KEY
93369      if( db->flags&SQLITE_ForeignKeys ){
93370        /* Check that the column is not part of an FK child key definition. It
93371        ** is not necessary to check if it is part of a parent key, as parent
93372        ** key columns must be indexed. The check below will pick up this
93373        ** case.  */
93374        FKey *pFKey;
93375        for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
93376          int j;
93377          for(j=0; j<pFKey->nCol; j++){
93378            if( pFKey->aCol[j].iFrom==iCol ){
93379              zFault = "foreign key";
93380            }
93381          }
93382        }
93383      }
93384#endif
93385      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
93386        int j;
93387        for(j=0; j<pIdx->nKeyCol; j++){
93388          /* FIXME: Be smarter about indexes that use expressions */
93389          if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
93390            zFault = "indexed";
93391          }
93392        }
93393      }
93394      if( zFault ){
93395        sqlite3DbFree(db, zErr);
93396        zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
93397        rc = SQLITE_ERROR;
93398        sqlite3BtreeLeaveAll(db);
93399        goto blob_open_out;
93400      }
93401    }
93402
93403    pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
93404    assert( pBlob->pStmt || db->mallocFailed );
93405    if( pBlob->pStmt ){
93406
93407      /* This VDBE program seeks a btree cursor to the identified
93408      ** db/table/row entry. The reason for using a vdbe program instead
93409      ** of writing code to use the b-tree layer directly is that the
93410      ** vdbe program will take advantage of the various transaction,
93411      ** locking and error handling infrastructure built into the vdbe.
93412      **
93413      ** After seeking the cursor, the vdbe executes an OP_ResultRow.
93414      ** Code external to the Vdbe then "borrows" the b-tree cursor and
93415      ** uses it to implement the blob_read(), blob_write() and
93416      ** blob_bytes() functions.
93417      **
93418      ** The sqlite3_blob_close() function finalizes the vdbe program,
93419      ** which closes the b-tree cursor and (possibly) commits the
93420      ** transaction.
93421      */
93422      static const int iLn = VDBE_OFFSET_LINENO(2);
93423      static const VdbeOpList openBlob[] = {
93424        {OP_TableLock,      0, 0, 0},  /* 0: Acquire a read or write lock */
93425        {OP_OpenRead,       0, 0, 0},  /* 1: Open a cursor */
93426        /* blobSeekToRow() will initialize r[1] to the desired rowid */
93427        {OP_NotExists,      0, 5, 1},  /* 2: Seek the cursor to rowid=r[1] */
93428        {OP_Column,         0, 0, 1},  /* 3  */
93429        {OP_ResultRow,      1, 0, 0},  /* 4  */
93430        {OP_Halt,           0, 0, 0},  /* 5  */
93431      };
93432      Vdbe *v = (Vdbe *)pBlob->pStmt;
93433      int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
93434      VdbeOp *aOp;
93435
93436      sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag,
93437                           pTab->pSchema->schema_cookie,
93438                           pTab->pSchema->iGeneration);
93439      sqlite3VdbeChangeP5(v, 1);
93440      assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
93441      aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
93442
93443      /* Make sure a mutex is held on the table to be accessed */
93444      sqlite3VdbeUsesBtree(v, iDb);
93445
93446      if( db->mallocFailed==0 ){
93447        assert( aOp!=0 );
93448        /* Configure the OP_TableLock instruction */
93449#ifdef SQLITE_OMIT_SHARED_CACHE
93450        aOp[0].opcode = OP_Noop;
93451#else
93452        aOp[0].p1 = iDb;
93453        aOp[0].p2 = pTab->tnum;
93454        aOp[0].p3 = wrFlag;
93455        sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
93456      }
93457      if( db->mallocFailed==0 ){
93458#endif
93459
93460        /* Remove either the OP_OpenWrite or OpenRead. Set the P2
93461        ** parameter of the other to pTab->tnum.  */
93462        if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
93463        aOp[1].p2 = pTab->tnum;
93464        aOp[1].p3 = iDb;
93465
93466        /* Configure the number of columns. Configure the cursor to
93467        ** think that the table has one more column than it really
93468        ** does. An OP_Column to retrieve this imaginary column will
93469        ** always return an SQL NULL. This is useful because it means
93470        ** we can invoke OP_Column to fill in the vdbe cursors type
93471        ** and offset cache without causing any IO.
93472        */
93473        aOp[1].p4type = P4_INT32;
93474        aOp[1].p4.i = pTab->nCol+1;
93475        aOp[3].p2 = pTab->nCol;
93476
93477        sParse.nVar = 0;
93478        sParse.nMem = 1;
93479        sParse.nTab = 1;
93480        sqlite3VdbeMakeReady(v, &sParse);
93481      }
93482    }
93483
93484    pBlob->iCol = iCol;
93485    pBlob->db = db;
93486    sqlite3BtreeLeaveAll(db);
93487    if( db->mallocFailed ){
93488      goto blob_open_out;
93489    }
93490    rc = blobSeekToRow(pBlob, iRow, &zErr);
93491  } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
93492
93493blob_open_out:
93494  if( rc==SQLITE_OK && db->mallocFailed==0 ){
93495    *ppBlob = (sqlite3_blob *)pBlob;
93496  }else{
93497    if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
93498    sqlite3DbFree(db, pBlob);
93499  }
93500  sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
93501  sqlite3DbFree(db, zErr);
93502  sqlite3ParserReset(&sParse);
93503  rc = sqlite3ApiExit(db, rc);
93504  sqlite3_mutex_leave(db->mutex);
93505  return rc;
93506}
93507
93508/*
93509** Close a blob handle that was previously created using
93510** sqlite3_blob_open().
93511*/
93512SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
93513  Incrblob *p = (Incrblob *)pBlob;
93514  int rc;
93515  sqlite3 *db;
93516
93517  if( p ){
93518    sqlite3_stmt *pStmt = p->pStmt;
93519    db = p->db;
93520    sqlite3_mutex_enter(db->mutex);
93521    sqlite3DbFree(db, p);
93522    sqlite3_mutex_leave(db->mutex);
93523    rc = sqlite3_finalize(pStmt);
93524  }else{
93525    rc = SQLITE_OK;
93526  }
93527  return rc;
93528}
93529
93530/*
93531** Perform a read or write operation on a blob
93532*/
93533static int blobReadWrite(
93534  sqlite3_blob *pBlob,
93535  void *z,
93536  int n,
93537  int iOffset,
93538  int (*xCall)(BtCursor*, u32, u32, void*)
93539){
93540  int rc;
93541  Incrblob *p = (Incrblob *)pBlob;
93542  Vdbe *v;
93543  sqlite3 *db;
93544
93545  if( p==0 ) return SQLITE_MISUSE_BKPT;
93546  db = p->db;
93547  sqlite3_mutex_enter(db->mutex);
93548  v = (Vdbe*)p->pStmt;
93549
93550  if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
93551    /* Request is out of range. Return a transient error. */
93552    rc = SQLITE_ERROR;
93553  }else if( v==0 ){
93554    /* If there is no statement handle, then the blob-handle has
93555    ** already been invalidated. Return SQLITE_ABORT in this case.
93556    */
93557    rc = SQLITE_ABORT;
93558  }else{
93559    /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
93560    ** returned, clean-up the statement handle.
93561    */
93562    assert( db == v->db );
93563    sqlite3BtreeEnterCursor(p->pCsr);
93564
93565#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
93566    if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
93567      /* If a pre-update hook is registered and this is a write cursor,
93568      ** invoke it here.
93569      **
93570      ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
93571      ** operation should really be an SQLITE_UPDATE. This is probably
93572      ** incorrect, but is convenient because at this point the new.* values
93573      ** are not easily obtainable. And for the sessions module, an
93574      ** SQLITE_UPDATE where the PK columns do not change is handled in the
93575      ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
93576      ** slightly more efficient). Since you cannot write to a PK column
93577      ** using the incremental-blob API, this works. For the sessions module
93578      ** anyhow.
93579      */
93580      sqlite3_int64 iKey;
93581      iKey = sqlite3BtreeIntegerKey(p->pCsr);
93582      sqlite3VdbePreUpdateHook(
93583          v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1
93584      );
93585    }
93586#endif
93587
93588    rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
93589    sqlite3BtreeLeaveCursor(p->pCsr);
93590    if( rc==SQLITE_ABORT ){
93591      sqlite3VdbeFinalize(v);
93592      p->pStmt = 0;
93593    }else{
93594      v->rc = rc;
93595    }
93596  }
93597  sqlite3Error(db, rc);
93598  rc = sqlite3ApiExit(db, rc);
93599  sqlite3_mutex_leave(db->mutex);
93600  return rc;
93601}
93602
93603/*
93604** Read data from a blob handle.
93605*/
93606SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
93607  return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked);
93608}
93609
93610/*
93611** Write data to a blob handle.
93612*/
93613SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
93614  return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
93615}
93616
93617/*
93618** Query a blob handle for the size of the data.
93619**
93620** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
93621** so no mutex is required for access.
93622*/
93623SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
93624  Incrblob *p = (Incrblob *)pBlob;
93625  return (p && p->pStmt) ? p->nByte : 0;
93626}
93627
93628/*
93629** Move an existing blob handle to point to a different row of the same
93630** database table.
93631**
93632** If an error occurs, or if the specified row does not exist or does not
93633** contain a blob or text value, then an error code is returned and the
93634** database handle error code and message set. If this happens, then all
93635** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
93636** immediately return SQLITE_ABORT.
93637*/
93638SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
93639  int rc;
93640  Incrblob *p = (Incrblob *)pBlob;
93641  sqlite3 *db;
93642
93643  if( p==0 ) return SQLITE_MISUSE_BKPT;
93644  db = p->db;
93645  sqlite3_mutex_enter(db->mutex);
93646
93647  if( p->pStmt==0 ){
93648    /* If there is no statement handle, then the blob-handle has
93649    ** already been invalidated. Return SQLITE_ABORT in this case.
93650    */
93651    rc = SQLITE_ABORT;
93652  }else{
93653    char *zErr;
93654    rc = blobSeekToRow(p, iRow, &zErr);
93655    if( rc!=SQLITE_OK ){
93656      sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
93657      sqlite3DbFree(db, zErr);
93658    }
93659    assert( rc!=SQLITE_SCHEMA );
93660  }
93661
93662  rc = sqlite3ApiExit(db, rc);
93663  assert( rc==SQLITE_OK || p->pStmt==0 );
93664  sqlite3_mutex_leave(db->mutex);
93665  return rc;
93666}
93667
93668#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
93669
93670/************** End of vdbeblob.c ********************************************/
93671/************** Begin file vdbesort.c ****************************************/
93672/*
93673** 2011-07-09
93674**
93675** The author disclaims copyright to this source code.  In place of
93676** a legal notice, here is a blessing:
93677**
93678**    May you do good and not evil.
93679**    May you find forgiveness for yourself and forgive others.
93680**    May you share freely, never taking more than you give.
93681**
93682*************************************************************************
93683** This file contains code for the VdbeSorter object, used in concert with
93684** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements
93685** or by SELECT statements with ORDER BY clauses that cannot be satisfied
93686** using indexes and without LIMIT clauses.
93687**
93688** The VdbeSorter object implements a multi-threaded external merge sort
93689** algorithm that is efficient even if the number of elements being sorted
93690** exceeds the available memory.
93691**
93692** Here is the (internal, non-API) interface between this module and the
93693** rest of the SQLite system:
93694**
93695**    sqlite3VdbeSorterInit()       Create a new VdbeSorter object.
93696**
93697**    sqlite3VdbeSorterWrite()      Add a single new row to the VdbeSorter
93698**                                  object.  The row is a binary blob in the
93699**                                  OP_MakeRecord format that contains both
93700**                                  the ORDER BY key columns and result columns
93701**                                  in the case of a SELECT w/ ORDER BY, or
93702**                                  the complete record for an index entry
93703**                                  in the case of a CREATE INDEX.
93704**
93705**    sqlite3VdbeSorterRewind()     Sort all content previously added.
93706**                                  Position the read cursor on the
93707**                                  first sorted element.
93708**
93709**    sqlite3VdbeSorterNext()       Advance the read cursor to the next sorted
93710**                                  element.
93711**
93712**    sqlite3VdbeSorterRowkey()     Return the complete binary blob for the
93713**                                  row currently under the read cursor.
93714**
93715**    sqlite3VdbeSorterCompare()    Compare the binary blob for the row
93716**                                  currently under the read cursor against
93717**                                  another binary blob X and report if
93718**                                  X is strictly less than the read cursor.
93719**                                  Used to enforce uniqueness in a
93720**                                  CREATE UNIQUE INDEX statement.
93721**
93722**    sqlite3VdbeSorterClose()      Close the VdbeSorter object and reclaim
93723**                                  all resources.
93724**
93725**    sqlite3VdbeSorterReset()      Refurbish the VdbeSorter for reuse.  This
93726**                                  is like Close() followed by Init() only
93727**                                  much faster.
93728**
93729** The interfaces above must be called in a particular order.  Write() can
93730** only occur in between Init()/Reset() and Rewind().  Next(), Rowkey(), and
93731** Compare() can only occur in between Rewind() and Close()/Reset(). i.e.
93732**
93733**   Init()
93734**   for each record: Write()
93735**   Rewind()
93736**     Rowkey()/Compare()
93737**   Next()
93738**   Close()
93739**
93740** Algorithm:
93741**
93742** Records passed to the sorter via calls to Write() are initially held
93743** unsorted in main memory. Assuming the amount of memory used never exceeds
93744** a threshold, when Rewind() is called the set of records is sorted using
93745** an in-memory merge sort. In this case, no temporary files are required
93746** and subsequent calls to Rowkey(), Next() and Compare() read records
93747** directly from main memory.
93748**
93749** If the amount of space used to store records in main memory exceeds the
93750** threshold, then the set of records currently in memory are sorted and
93751** written to a temporary file in "Packed Memory Array" (PMA) format.
93752** A PMA created at this point is known as a "level-0 PMA". Higher levels
93753** of PMAs may be created by merging existing PMAs together - for example
93754** merging two or more level-0 PMAs together creates a level-1 PMA.
93755**
93756** The threshold for the amount of main memory to use before flushing
93757** records to a PMA is roughly the same as the limit configured for the
93758** page-cache of the main database. Specifically, the threshold is set to
93759** the value returned by "PRAGMA main.page_size" multipled by
93760** that returned by "PRAGMA main.cache_size", in bytes.
93761**
93762** If the sorter is running in single-threaded mode, then all PMAs generated
93763** are appended to a single temporary file. Or, if the sorter is running in
93764** multi-threaded mode then up to (N+1) temporary files may be opened, where
93765** N is the configured number of worker threads. In this case, instead of
93766** sorting the records and writing the PMA to a temporary file itself, the
93767** calling thread usually launches a worker thread to do so. Except, if
93768** there are already N worker threads running, the main thread does the work
93769** itself.
93770**
93771** The sorter is running in multi-threaded mode if (a) the library was built
93772** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
93773** than zero, and (b) worker threads have been enabled at runtime by calling
93774** "PRAGMA threads=N" with some value of N greater than 0.
93775**
93776** When Rewind() is called, any data remaining in memory is flushed to a
93777** final PMA. So at this point the data is stored in some number of sorted
93778** PMAs within temporary files on disk.
93779**
93780** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the
93781** sorter is running in single-threaded mode, then these PMAs are merged
93782** incrementally as keys are retreived from the sorter by the VDBE.  The
93783** MergeEngine object, described in further detail below, performs this
93784** merge.
93785**
93786** Or, if running in multi-threaded mode, then a background thread is
93787** launched to merge the existing PMAs. Once the background thread has
93788** merged T bytes of data into a single sorted PMA, the main thread
93789** begins reading keys from that PMA while the background thread proceeds
93790** with merging the next T bytes of data. And so on.
93791**
93792** Parameter T is set to half the value of the memory threshold used
93793** by Write() above to determine when to create a new PMA.
93794**
93795** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when
93796** Rewind() is called, then a hierarchy of incremental-merges is used.
93797** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on
93798** disk are merged together. Then T bytes of data from the second set, and
93799** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT
93800** PMAs at a time. This done is to improve locality.
93801**
93802** If running in multi-threaded mode and there are more than
93803** SORTER_MAX_MERGE_COUNT PMAs on disk when Rewind() is called, then more
93804** than one background thread may be created. Specifically, there may be
93805** one background thread for each temporary file on disk, and one background
93806** thread to merge the output of each of the others to a single PMA for
93807** the main thread to read from.
93808*/
93809/* #include "sqliteInt.h" */
93810/* #include "vdbeInt.h" */
93811
93812/*
93813** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
93814** messages to stderr that may be helpful in understanding the performance
93815** characteristics of the sorter in multi-threaded mode.
93816*/
93817#if 0
93818# define SQLITE_DEBUG_SORTER_THREADS 1
93819#endif
93820
93821/*
93822** Hard-coded maximum amount of data to accumulate in memory before flushing
93823** to a level 0 PMA. The purpose of this limit is to prevent various integer
93824** overflows. 512MiB.
93825*/
93826#define SQLITE_MAX_PMASZ    (1<<29)
93827
93828/*
93829** Private objects used by the sorter
93830*/
93831typedef struct MergeEngine MergeEngine;     /* Merge PMAs together */
93832typedef struct PmaReader PmaReader;         /* Incrementally read one PMA */
93833typedef struct PmaWriter PmaWriter;         /* Incrementally write one PMA */
93834typedef struct SorterRecord SorterRecord;   /* A record being sorted */
93835typedef struct SortSubtask SortSubtask;     /* A sub-task in the sort process */
93836typedef struct SorterFile SorterFile;       /* Temporary file object wrapper */
93837typedef struct SorterList SorterList;       /* In-memory list of records */
93838typedef struct IncrMerger IncrMerger;       /* Read & merge multiple PMAs */
93839
93840/*
93841** A container for a temp file handle and the current amount of data
93842** stored in the file.
93843*/
93844struct SorterFile {
93845  sqlite3_file *pFd;              /* File handle */
93846  i64 iEof;                       /* Bytes of data stored in pFd */
93847};
93848
93849/*
93850** An in-memory list of objects to be sorted.
93851**
93852** If aMemory==0 then each object is allocated separately and the objects
93853** are connected using SorterRecord.u.pNext.  If aMemory!=0 then all objects
93854** are stored in the aMemory[] bulk memory, one right after the other, and
93855** are connected using SorterRecord.u.iNext.
93856*/
93857struct SorterList {
93858  SorterRecord *pList;            /* Linked list of records */
93859  u8 *aMemory;                    /* If non-NULL, bulk memory to hold pList */
93860  int szPMA;                      /* Size of pList as PMA in bytes */
93861};
93862
93863/*
93864** The MergeEngine object is used to combine two or more smaller PMAs into
93865** one big PMA using a merge operation.  Separate PMAs all need to be
93866** combined into one big PMA in order to be able to step through the sorted
93867** records in order.
93868**
93869** The aReadr[] array contains a PmaReader object for each of the PMAs being
93870** merged.  An aReadr[] object either points to a valid key or else is at EOF.
93871** ("EOF" means "End Of File".  When aReadr[] is at EOF there is no more data.)
93872** For the purposes of the paragraphs below, we assume that the array is
93873** actually N elements in size, where N is the smallest power of 2 greater
93874** to or equal to the number of PMAs being merged. The extra aReadr[] elements
93875** are treated as if they are empty (always at EOF).
93876**
93877** The aTree[] array is also N elements in size. The value of N is stored in
93878** the MergeEngine.nTree variable.
93879**
93880** The final (N/2) elements of aTree[] contain the results of comparing
93881** pairs of PMA keys together. Element i contains the result of
93882** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
93883** aTree element is set to the index of it.
93884**
93885** For the purposes of this comparison, EOF is considered greater than any
93886** other key value. If the keys are equal (only possible with two EOF
93887** values), it doesn't matter which index is stored.
93888**
93889** The (N/4) elements of aTree[] that precede the final (N/2) described
93890** above contains the index of the smallest of each block of 4 PmaReaders
93891** And so on. So that aTree[1] contains the index of the PmaReader that
93892** currently points to the smallest key value. aTree[0] is unused.
93893**
93894** Example:
93895**
93896**     aReadr[0] -> Banana
93897**     aReadr[1] -> Feijoa
93898**     aReadr[2] -> Elderberry
93899**     aReadr[3] -> Currant
93900**     aReadr[4] -> Grapefruit
93901**     aReadr[5] -> Apple
93902**     aReadr[6] -> Durian
93903**     aReadr[7] -> EOF
93904**
93905**     aTree[] = { X, 5   0, 5    0, 3, 5, 6 }
93906**
93907** The current element is "Apple" (the value of the key indicated by
93908** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will
93909** be advanced to the next key in its segment. Say the next key is
93910** "Eggplant":
93911**
93912**     aReadr[5] -> Eggplant
93913**
93914** The contents of aTree[] are updated first by comparing the new PmaReader
93915** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader
93916** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
93917** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
93918** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
93919** so the value written into element 1 of the array is 0. As follows:
93920**
93921**     aTree[] = { X, 0   0, 6    0, 3, 5, 6 }
93922**
93923** In other words, each time we advance to the next sorter element, log2(N)
93924** key comparison operations are required, where N is the number of segments
93925** being merged (rounded up to the next power of 2).
93926*/
93927struct MergeEngine {
93928  int nTree;                 /* Used size of aTree/aReadr (power of 2) */
93929  SortSubtask *pTask;        /* Used by this thread only */
93930  int *aTree;                /* Current state of incremental merge */
93931  PmaReader *aReadr;         /* Array of PmaReaders to merge data from */
93932};
93933
93934/*
93935** This object represents a single thread of control in a sort operation.
93936** Exactly VdbeSorter.nTask instances of this object are allocated
93937** as part of each VdbeSorter object. Instances are never allocated any
93938** other way. VdbeSorter.nTask is set to the number of worker threads allowed
93939** (see SQLITE_CONFIG_WORKER_THREADS) plus one (the main thread).  Thus for
93940** single-threaded operation, there is exactly one instance of this object
93941** and for multi-threaded operation there are two or more instances.
93942**
93943** Essentially, this structure contains all those fields of the VdbeSorter
93944** structure for which each thread requires a separate instance. For example,
93945** each thread requries its own UnpackedRecord object to unpack records in
93946** as part of comparison operations.
93947**
93948** Before a background thread is launched, variable bDone is set to 0. Then,
93949** right before it exits, the thread itself sets bDone to 1. This is used for
93950** two purposes:
93951**
93952**   1. When flushing the contents of memory to a level-0 PMA on disk, to
93953**      attempt to select a SortSubtask for which there is not already an
93954**      active background thread (since doing so causes the main thread
93955**      to block until it finishes).
93956**
93957**   2. If SQLITE_DEBUG_SORTER_THREADS is defined, to determine if a call
93958**      to sqlite3ThreadJoin() is likely to block. Cases that are likely to
93959**      block provoke debugging output.
93960**
93961** In both cases, the effects of the main thread seeing (bDone==0) even
93962** after the thread has finished are not dire. So we don't worry about
93963** memory barriers and such here.
93964*/
93965typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int);
93966struct SortSubtask {
93967  SQLiteThread *pThread;          /* Background thread, if any */
93968  int bDone;                      /* Set if thread is finished but not joined */
93969  VdbeSorter *pSorter;            /* Sorter that owns this sub-task */
93970  UnpackedRecord *pUnpacked;      /* Space to unpack a record */
93971  SorterList list;                /* List for thread to write to a PMA */
93972  int nPMA;                       /* Number of PMAs currently in file */
93973  SorterCompare xCompare;         /* Compare function to use */
93974  SorterFile file;                /* Temp file for level-0 PMAs */
93975  SorterFile file2;               /* Space for other PMAs */
93976};
93977
93978
93979/*
93980** Main sorter structure. A single instance of this is allocated for each
93981** sorter cursor created by the VDBE.
93982**
93983** mxKeysize:
93984**   As records are added to the sorter by calls to sqlite3VdbeSorterWrite(),
93985**   this variable is updated so as to be set to the size on disk of the
93986**   largest record in the sorter.
93987*/
93988struct VdbeSorter {
93989  int mnPmaSize;                  /* Minimum PMA size, in bytes */
93990  int mxPmaSize;                  /* Maximum PMA size, in bytes.  0==no limit */
93991  int mxKeysize;                  /* Largest serialized key seen so far */
93992  int pgsz;                       /* Main database page size */
93993  PmaReader *pReader;             /* Readr data from here after Rewind() */
93994  MergeEngine *pMerger;           /* Or here, if bUseThreads==0 */
93995  sqlite3 *db;                    /* Database connection */
93996  KeyInfo *pKeyInfo;              /* How to compare records */
93997  UnpackedRecord *pUnpacked;      /* Used by VdbeSorterCompare() */
93998  SorterList list;                /* List of in-memory records */
93999  int iMemory;                    /* Offset of free space in list.aMemory */
94000  int nMemory;                    /* Size of list.aMemory allocation in bytes */
94001  u8 bUsePMA;                     /* True if one or more PMAs created */
94002  u8 bUseThreads;                 /* True to use background threads */
94003  u8 iPrev;                       /* Previous thread used to flush PMA */
94004  u8 nTask;                       /* Size of aTask[] array */
94005  u8 typeMask;
94006  SortSubtask aTask[1];           /* One or more subtasks */
94007};
94008
94009#define SORTER_TYPE_INTEGER 0x01
94010#define SORTER_TYPE_TEXT    0x02
94011
94012/*
94013** An instance of the following object is used to read records out of a
94014** PMA, in sorted order.  The next key to be read is cached in nKey/aKey.
94015** aKey might point into aMap or into aBuffer.  If neither of those locations
94016** contain a contiguous representation of the key, then aAlloc is allocated
94017** and the key is copied into aAlloc and aKey is made to poitn to aAlloc.
94018**
94019** pFd==0 at EOF.
94020*/
94021struct PmaReader {
94022  i64 iReadOff;               /* Current read offset */
94023  i64 iEof;                   /* 1 byte past EOF for this PmaReader */
94024  int nAlloc;                 /* Bytes of space at aAlloc */
94025  int nKey;                   /* Number of bytes in key */
94026  sqlite3_file *pFd;          /* File handle we are reading from */
94027  u8 *aAlloc;                 /* Space for aKey if aBuffer and pMap wont work */
94028  u8 *aKey;                   /* Pointer to current key */
94029  u8 *aBuffer;                /* Current read buffer */
94030  int nBuffer;                /* Size of read buffer in bytes */
94031  u8 *aMap;                   /* Pointer to mapping of entire file */
94032  IncrMerger *pIncr;          /* Incremental merger */
94033};
94034
94035/*
94036** Normally, a PmaReader object iterates through an existing PMA stored
94037** within a temp file. However, if the PmaReader.pIncr variable points to
94038** an object of the following type, it may be used to iterate/merge through
94039** multiple PMAs simultaneously.
94040**
94041** There are two types of IncrMerger object - single (bUseThread==0) and
94042** multi-threaded (bUseThread==1).
94043**
94044** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
94045** and aFile[1]. Neither file is allowed to grow to more than mxSz bytes in
94046** size. When the IncrMerger is initialized, it reads enough data from
94047** pMerger to populate aFile[0]. It then sets variables within the
94048** corresponding PmaReader object to read from that file and kicks off
94049** a background thread to populate aFile[1] with the next mxSz bytes of
94050** sorted record data from pMerger.
94051**
94052** When the PmaReader reaches the end of aFile[0], it blocks until the
94053** background thread has finished populating aFile[1]. It then exchanges
94054** the contents of the aFile[0] and aFile[1] variables within this structure,
94055** sets the PmaReader fields to read from the new aFile[0] and kicks off
94056** another background thread to populate the new aFile[1]. And so on, until
94057** the contents of pMerger are exhausted.
94058**
94059** A single-threaded IncrMerger does not open any temporary files of its
94060** own. Instead, it has exclusive access to mxSz bytes of space beginning
94061** at offset iStartOff of file pTask->file2. And instead of using a
94062** background thread to prepare data for the PmaReader, with a single
94063** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
94064** keys from pMerger by the calling thread whenever the PmaReader runs out
94065** of data.
94066*/
94067struct IncrMerger {
94068  SortSubtask *pTask;             /* Task that owns this merger */
94069  MergeEngine *pMerger;           /* Merge engine thread reads data from */
94070  i64 iStartOff;                  /* Offset to start writing file at */
94071  int mxSz;                       /* Maximum bytes of data to store */
94072  int bEof;                       /* Set to true when merge is finished */
94073  int bUseThread;                 /* True to use a bg thread for this object */
94074  SorterFile aFile[2];            /* aFile[0] for reading, [1] for writing */
94075};
94076
94077/*
94078** An instance of this object is used for writing a PMA.
94079**
94080** The PMA is written one record at a time.  Each record is of an arbitrary
94081** size.  But I/O is more efficient if it occurs in page-sized blocks where
94082** each block is aligned on a page boundary.  This object caches writes to
94083** the PMA so that aligned, page-size blocks are written.
94084*/
94085struct PmaWriter {
94086  int eFWErr;                     /* Non-zero if in an error state */
94087  u8 *aBuffer;                    /* Pointer to write buffer */
94088  int nBuffer;                    /* Size of write buffer in bytes */
94089  int iBufStart;                  /* First byte of buffer to write */
94090  int iBufEnd;                    /* Last byte of buffer to write */
94091  i64 iWriteOff;                  /* Offset of start of buffer in file */
94092  sqlite3_file *pFd;              /* File handle to write to */
94093};
94094
94095/*
94096** This object is the header on a single record while that record is being
94097** held in memory and prior to being written out as part of a PMA.
94098**
94099** How the linked list is connected depends on how memory is being managed
94100** by this module. If using a separate allocation for each in-memory record
94101** (VdbeSorter.list.aMemory==0), then the list is always connected using the
94102** SorterRecord.u.pNext pointers.
94103**
94104** Or, if using the single large allocation method (VdbeSorter.list.aMemory!=0),
94105** then while records are being accumulated the list is linked using the
94106** SorterRecord.u.iNext offset. This is because the aMemory[] array may
94107** be sqlite3Realloc()ed while records are being accumulated. Once the VM
94108** has finished passing records to the sorter, or when the in-memory buffer
94109** is full, the list is sorted. As part of the sorting process, it is
94110** converted to use the SorterRecord.u.pNext pointers. See function
94111** vdbeSorterSort() for details.
94112*/
94113struct SorterRecord {
94114  int nVal;                       /* Size of the record in bytes */
94115  union {
94116    SorterRecord *pNext;          /* Pointer to next record in list */
94117    int iNext;                    /* Offset within aMemory of next record */
94118  } u;
94119  /* The data for the record immediately follows this header */
94120};
94121
94122/* Return a pointer to the buffer containing the record data for SorterRecord
94123** object p. Should be used as if:
94124**
94125**   void *SRVAL(SorterRecord *p) { return (void*)&p[1]; }
94126*/
94127#define SRVAL(p) ((void*)((SorterRecord*)(p) + 1))
94128
94129
94130/* Maximum number of PMAs that a single MergeEngine can merge */
94131#define SORTER_MAX_MERGE_COUNT 16
94132
94133static int vdbeIncrSwap(IncrMerger*);
94134static void vdbeIncrFree(IncrMerger *);
94135
94136/*
94137** Free all memory belonging to the PmaReader object passed as the
94138** argument. All structure fields are set to zero before returning.
94139*/
94140static void vdbePmaReaderClear(PmaReader *pReadr){
94141  sqlite3_free(pReadr->aAlloc);
94142  sqlite3_free(pReadr->aBuffer);
94143  if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
94144  vdbeIncrFree(pReadr->pIncr);
94145  memset(pReadr, 0, sizeof(PmaReader));
94146}
94147
94148/*
94149** Read the next nByte bytes of data from the PMA p.
94150** If successful, set *ppOut to point to a buffer containing the data
94151** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
94152** error code.
94153**
94154** The buffer returned in *ppOut is only valid until the
94155** next call to this function.
94156*/
94157static int vdbePmaReadBlob(
94158  PmaReader *p,                   /* PmaReader from which to take the blob */
94159  int nByte,                      /* Bytes of data to read */
94160  u8 **ppOut                      /* OUT: Pointer to buffer containing data */
94161){
94162  int iBuf;                       /* Offset within buffer to read from */
94163  int nAvail;                     /* Bytes of data available in buffer */
94164
94165  if( p->aMap ){
94166    *ppOut = &p->aMap[p->iReadOff];
94167    p->iReadOff += nByte;
94168    return SQLITE_OK;
94169  }
94170
94171  assert( p->aBuffer );
94172
94173  /* If there is no more data to be read from the buffer, read the next
94174  ** p->nBuffer bytes of data from the file into it. Or, if there are less
94175  ** than p->nBuffer bytes remaining in the PMA, read all remaining data.  */
94176  iBuf = p->iReadOff % p->nBuffer;
94177  if( iBuf==0 ){
94178    int nRead;                    /* Bytes to read from disk */
94179    int rc;                       /* sqlite3OsRead() return code */
94180
94181    /* Determine how many bytes of data to read. */
94182    if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
94183      nRead = p->nBuffer;
94184    }else{
94185      nRead = (int)(p->iEof - p->iReadOff);
94186    }
94187    assert( nRead>0 );
94188
94189    /* Readr data from the file. Return early if an error occurs. */
94190    rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
94191    assert( rc!=SQLITE_IOERR_SHORT_READ );
94192    if( rc!=SQLITE_OK ) return rc;
94193  }
94194  nAvail = p->nBuffer - iBuf;
94195
94196  if( nByte<=nAvail ){
94197    /* The requested data is available in the in-memory buffer. In this
94198    ** case there is no need to make a copy of the data, just return a
94199    ** pointer into the buffer to the caller.  */
94200    *ppOut = &p->aBuffer[iBuf];
94201    p->iReadOff += nByte;
94202  }else{
94203    /* The requested data is not all available in the in-memory buffer.
94204    ** In this case, allocate space at p->aAlloc[] to copy the requested
94205    ** range into. Then return a copy of pointer p->aAlloc to the caller.  */
94206    int nRem;                     /* Bytes remaining to copy */
94207
94208    /* Extend the p->aAlloc[] allocation if required. */
94209    if( p->nAlloc<nByte ){
94210      u8 *aNew;
94211      sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
94212      while( nByte>nNew ) nNew = nNew*2;
94213      aNew = sqlite3Realloc(p->aAlloc, nNew);
94214      if( !aNew ) return SQLITE_NOMEM_BKPT;
94215      p->nAlloc = nNew;
94216      p->aAlloc = aNew;
94217    }
94218
94219    /* Copy as much data as is available in the buffer into the start of
94220    ** p->aAlloc[].  */
94221    memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
94222    p->iReadOff += nAvail;
94223    nRem = nByte - nAvail;
94224
94225    /* The following loop copies up to p->nBuffer bytes per iteration into
94226    ** the p->aAlloc[] buffer.  */
94227    while( nRem>0 ){
94228      int rc;                     /* vdbePmaReadBlob() return code */
94229      int nCopy;                  /* Number of bytes to copy */
94230      u8 *aNext;                  /* Pointer to buffer to copy data from */
94231
94232      nCopy = nRem;
94233      if( nRem>p->nBuffer ) nCopy = p->nBuffer;
94234      rc = vdbePmaReadBlob(p, nCopy, &aNext);
94235      if( rc!=SQLITE_OK ) return rc;
94236      assert( aNext!=p->aAlloc );
94237      memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
94238      nRem -= nCopy;
94239    }
94240
94241    *ppOut = p->aAlloc;
94242  }
94243
94244  return SQLITE_OK;
94245}
94246
94247/*
94248** Read a varint from the stream of data accessed by p. Set *pnOut to
94249** the value read.
94250*/
94251static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){
94252  int iBuf;
94253
94254  if( p->aMap ){
94255    p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
94256  }else{
94257    iBuf = p->iReadOff % p->nBuffer;
94258    if( iBuf && (p->nBuffer-iBuf)>=9 ){
94259      p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
94260    }else{
94261      u8 aVarint[16], *a;
94262      int i = 0, rc;
94263      do{
94264        rc = vdbePmaReadBlob(p, 1, &a);
94265        if( rc ) return rc;
94266        aVarint[(i++)&0xf] = a[0];
94267      }while( (a[0]&0x80)!=0 );
94268      sqlite3GetVarint(aVarint, pnOut);
94269    }
94270  }
94271
94272  return SQLITE_OK;
94273}
94274
94275/*
94276** Attempt to memory map file pFile. If successful, set *pp to point to the
94277** new mapping and return SQLITE_OK. If the mapping is not attempted
94278** (because the file is too large or the VFS layer is configured not to use
94279** mmap), return SQLITE_OK and set *pp to NULL.
94280**
94281** Or, if an error occurs, return an SQLite error code. The final value of
94282** *pp is undefined in this case.
94283*/
94284static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){
94285  int rc = SQLITE_OK;
94286  if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
94287    sqlite3_file *pFd = pFile->pFd;
94288    if( pFd->pMethods->iVersion>=3 ){
94289      rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
94290      testcase( rc!=SQLITE_OK );
94291    }
94292  }
94293  return rc;
94294}
94295
94296/*
94297** Attach PmaReader pReadr to file pFile (if it is not already attached to
94298** that file) and seek it to offset iOff within the file.  Return SQLITE_OK
94299** if successful, or an SQLite error code if an error occurs.
94300*/
94301static int vdbePmaReaderSeek(
94302  SortSubtask *pTask,             /* Task context */
94303  PmaReader *pReadr,              /* Reader whose cursor is to be moved */
94304  SorterFile *pFile,              /* Sorter file to read from */
94305  i64 iOff                        /* Offset in pFile */
94306){
94307  int rc = SQLITE_OK;
94308
94309  assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
94310
94311  if( sqlite3FaultSim(201) ) return SQLITE_IOERR_READ;
94312  if( pReadr->aMap ){
94313    sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
94314    pReadr->aMap = 0;
94315  }
94316  pReadr->iReadOff = iOff;
94317  pReadr->iEof = pFile->iEof;
94318  pReadr->pFd = pFile->pFd;
94319
94320  rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
94321  if( rc==SQLITE_OK && pReadr->aMap==0 ){
94322    int pgsz = pTask->pSorter->pgsz;
94323    int iBuf = pReadr->iReadOff % pgsz;
94324    if( pReadr->aBuffer==0 ){
94325      pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
94326      if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
94327      pReadr->nBuffer = pgsz;
94328    }
94329    if( rc==SQLITE_OK && iBuf ){
94330      int nRead = pgsz - iBuf;
94331      if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
94332        nRead = (int)(pReadr->iEof - pReadr->iReadOff);
94333      }
94334      rc = sqlite3OsRead(
94335          pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
94336      );
94337      testcase( rc!=SQLITE_OK );
94338    }
94339  }
94340
94341  return rc;
94342}
94343
94344/*
94345** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if
94346** no error occurs, or an SQLite error code if one does.
94347*/
94348static int vdbePmaReaderNext(PmaReader *pReadr){
94349  int rc = SQLITE_OK;             /* Return Code */
94350  u64 nRec = 0;                   /* Size of record in bytes */
94351
94352
94353  if( pReadr->iReadOff>=pReadr->iEof ){
94354    IncrMerger *pIncr = pReadr->pIncr;
94355    int bEof = 1;
94356    if( pIncr ){
94357      rc = vdbeIncrSwap(pIncr);
94358      if( rc==SQLITE_OK && pIncr->bEof==0 ){
94359        rc = vdbePmaReaderSeek(
94360            pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
94361        );
94362        bEof = 0;
94363      }
94364    }
94365
94366    if( bEof ){
94367      /* This is an EOF condition */
94368      vdbePmaReaderClear(pReadr);
94369      testcase( rc!=SQLITE_OK );
94370      return rc;
94371    }
94372  }
94373
94374  if( rc==SQLITE_OK ){
94375    rc = vdbePmaReadVarint(pReadr, &nRec);
94376  }
94377  if( rc==SQLITE_OK ){
94378    pReadr->nKey = (int)nRec;
94379    rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
94380    testcase( rc!=SQLITE_OK );
94381  }
94382
94383  return rc;
94384}
94385
94386/*
94387** Initialize PmaReader pReadr to scan through the PMA stored in file pFile
94388** starting at offset iStart and ending at offset iEof-1. This function
94389** leaves the PmaReader pointing to the first key in the PMA (or EOF if the
94390** PMA is empty).
94391**
94392** If the pnByte parameter is NULL, then it is assumed that the file
94393** contains a single PMA, and that that PMA omits the initial length varint.
94394*/
94395static int vdbePmaReaderInit(
94396  SortSubtask *pTask,             /* Task context */
94397  SorterFile *pFile,              /* Sorter file to read from */
94398  i64 iStart,                     /* Start offset in pFile */
94399  PmaReader *pReadr,              /* PmaReader to populate */
94400  i64 *pnByte                     /* IN/OUT: Increment this value by PMA size */
94401){
94402  int rc;
94403
94404  assert( pFile->iEof>iStart );
94405  assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
94406  assert( pReadr->aBuffer==0 );
94407  assert( pReadr->aMap==0 );
94408
94409  rc = vdbePmaReaderSeek(pTask, pReadr, pFile, iStart);
94410  if( rc==SQLITE_OK ){
94411    u64 nByte = 0;                 /* Size of PMA in bytes */
94412    rc = vdbePmaReadVarint(pReadr, &nByte);
94413    pReadr->iEof = pReadr->iReadOff + nByte;
94414    *pnByte += nByte;
94415  }
94416
94417  if( rc==SQLITE_OK ){
94418    rc = vdbePmaReaderNext(pReadr);
94419  }
94420  return rc;
94421}
94422
94423/*
94424** A version of vdbeSorterCompare() that assumes that it has already been
94425** determined that the first field of key1 is equal to the first field of
94426** key2.
94427*/
94428static int vdbeSorterCompareTail(
94429  SortSubtask *pTask,             /* Subtask context (for pKeyInfo) */
94430  int *pbKey2Cached,              /* True if pTask->pUnpacked is pKey2 */
94431  const void *pKey1, int nKey1,   /* Left side of comparison */
94432  const void *pKey2, int nKey2    /* Right side of comparison */
94433){
94434  UnpackedRecord *r2 = pTask->pUnpacked;
94435  if( *pbKey2Cached==0 ){
94436    sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
94437    *pbKey2Cached = 1;
94438  }
94439  return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1);
94440}
94441
94442/*
94443** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
94444** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
94445** used by the comparison. Return the result of the comparison.
94446**
94447** If IN/OUT parameter *pbKey2Cached is true when this function is called,
94448** it is assumed that (pTask->pUnpacked) contains the unpacked version
94449** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
94450** version of key2 and *pbKey2Cached set to true before returning.
94451**
94452** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
94453** to SQLITE_NOMEM.
94454*/
94455static int vdbeSorterCompare(
94456  SortSubtask *pTask,             /* Subtask context (for pKeyInfo) */
94457  int *pbKey2Cached,              /* True if pTask->pUnpacked is pKey2 */
94458  const void *pKey1, int nKey1,   /* Left side of comparison */
94459  const void *pKey2, int nKey2    /* Right side of comparison */
94460){
94461  UnpackedRecord *r2 = pTask->pUnpacked;
94462  if( !*pbKey2Cached ){
94463    sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
94464    *pbKey2Cached = 1;
94465  }
94466  return sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
94467}
94468
94469/*
94470** A specially optimized version of vdbeSorterCompare() that assumes that
94471** the first field of each key is a TEXT value and that the collation
94472** sequence to compare them with is BINARY.
94473*/
94474static int vdbeSorterCompareText(
94475  SortSubtask *pTask,             /* Subtask context (for pKeyInfo) */
94476  int *pbKey2Cached,              /* True if pTask->pUnpacked is pKey2 */
94477  const void *pKey1, int nKey1,   /* Left side of comparison */
94478  const void *pKey2, int nKey2    /* Right side of comparison */
94479){
94480  const u8 * const p1 = (const u8 * const)pKey1;
94481  const u8 * const p2 = (const u8 * const)pKey2;
94482  const u8 * const v1 = &p1[ p1[0] ];   /* Pointer to value 1 */
94483  const u8 * const v2 = &p2[ p2[0] ];   /* Pointer to value 2 */
94484
94485  int n1;
94486  int n2;
94487  int res;
94488
94489  getVarint32NR(&p1[1], n1);
94490  getVarint32NR(&p2[1], n2);
94491  res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
94492  if( res==0 ){
94493    res = n1 - n2;
94494  }
94495
94496  if( res==0 ){
94497    if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
94498      res = vdbeSorterCompareTail(
94499          pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
94500      );
94501    }
94502  }else{
94503    assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
94504    if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
94505      res = res * -1;
94506    }
94507  }
94508
94509  return res;
94510}
94511
94512/*
94513** A specially optimized version of vdbeSorterCompare() that assumes that
94514** the first field of each key is an INTEGER value.
94515*/
94516static int vdbeSorterCompareInt(
94517  SortSubtask *pTask,             /* Subtask context (for pKeyInfo) */
94518  int *pbKey2Cached,              /* True if pTask->pUnpacked is pKey2 */
94519  const void *pKey1, int nKey1,   /* Left side of comparison */
94520  const void *pKey2, int nKey2    /* Right side of comparison */
94521){
94522  const u8 * const p1 = (const u8 * const)pKey1;
94523  const u8 * const p2 = (const u8 * const)pKey2;
94524  const int s1 = p1[1];                 /* Left hand serial type */
94525  const int s2 = p2[1];                 /* Right hand serial type */
94526  const u8 * const v1 = &p1[ p1[0] ];   /* Pointer to value 1 */
94527  const u8 * const v2 = &p2[ p2[0] ];   /* Pointer to value 2 */
94528  int res;                              /* Return value */
94529
94530  assert( (s1>0 && s1<7) || s1==8 || s1==9 );
94531  assert( (s2>0 && s2<7) || s2==8 || s2==9 );
94532
94533  if( s1==s2 ){
94534    /* The two values have the same sign. Compare using memcmp(). */
94535    static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8, 0, 0, 0 };
94536    const u8 n = aLen[s1];
94537    int i;
94538    res = 0;
94539    for(i=0; i<n; i++){
94540      if( (res = v1[i] - v2[i])!=0 ){
94541        if( ((v1[0] ^ v2[0]) & 0x80)!=0 ){
94542          res = v1[0] & 0x80 ? -1 : +1;
94543        }
94544        break;
94545      }
94546    }
94547  }else if( s1>7 && s2>7 ){
94548    res = s1 - s2;
94549  }else{
94550    if( s2>7 ){
94551      res = +1;
94552    }else if( s1>7 ){
94553      res = -1;
94554    }else{
94555      res = s1 - s2;
94556    }
94557    assert( res!=0 );
94558
94559    if( res>0 ){
94560      if( *v1 & 0x80 ) res = -1;
94561    }else{
94562      if( *v2 & 0x80 ) res = +1;
94563    }
94564  }
94565
94566  if( res==0 ){
94567    if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
94568      res = vdbeSorterCompareTail(
94569          pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
94570      );
94571    }
94572  }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
94573    assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
94574    res = res * -1;
94575  }
94576
94577  return res;
94578}
94579
94580/*
94581** Initialize the temporary index cursor just opened as a sorter cursor.
94582**
94583** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
94584** to determine the number of fields that should be compared from the
94585** records being sorted. However, if the value passed as argument nField
94586** is non-zero and the sorter is able to guarantee a stable sort, nField
94587** is used instead. This is used when sorting records for a CREATE INDEX
94588** statement. In this case, keys are always delivered to the sorter in
94589** order of the primary key, which happens to be make up the final part
94590** of the records being sorted. So if the sort is stable, there is never
94591** any reason to compare PK fields and they can be ignored for a small
94592** performance boost.
94593**
94594** The sorter can guarantee a stable sort when running in single-threaded
94595** mode, but not in multi-threaded mode.
94596**
94597** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
94598*/
94599SQLITE_PRIVATE int sqlite3VdbeSorterInit(
94600  sqlite3 *db,                    /* Database connection (for malloc()) */
94601  int nField,                     /* Number of key fields in each record */
94602  VdbeCursor *pCsr                /* Cursor that holds the new sorter */
94603){
94604  int pgsz;                       /* Page size of main database */
94605  int i;                          /* Used to iterate through aTask[] */
94606  VdbeSorter *pSorter;            /* The new sorter */
94607  KeyInfo *pKeyInfo;              /* Copy of pCsr->pKeyInfo with db==0 */
94608  int szKeyInfo;                  /* Size of pCsr->pKeyInfo in bytes */
94609  int sz;                         /* Size of pSorter in bytes */
94610  int rc = SQLITE_OK;
94611#if SQLITE_MAX_WORKER_THREADS==0
94612# define nWorker 0
94613#else
94614  int nWorker;
94615#endif
94616
94617  /* Initialize the upper limit on the number of worker threads */
94618#if SQLITE_MAX_WORKER_THREADS>0
94619  if( sqlite3TempInMemory(db) || sqlite3GlobalConfig.bCoreMutex==0 ){
94620    nWorker = 0;
94621  }else{
94622    nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
94623  }
94624#endif
94625
94626  /* Do not allow the total number of threads (main thread + all workers)
94627  ** to exceed the maximum merge count */
94628#if SQLITE_MAX_WORKER_THREADS>=SORTER_MAX_MERGE_COUNT
94629  if( nWorker>=SORTER_MAX_MERGE_COUNT ){
94630    nWorker = SORTER_MAX_MERGE_COUNT-1;
94631  }
94632#endif
94633
94634  assert( pCsr->pKeyInfo && pCsr->pBtx==0 );
94635  assert( pCsr->eCurType==CURTYPE_SORTER );
94636  szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
94637  sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
94638
94639  pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
94640  pCsr->uc.pSorter = pSorter;
94641  if( pSorter==0 ){
94642    rc = SQLITE_NOMEM_BKPT;
94643  }else{
94644    pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
94645    memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
94646    pKeyInfo->db = 0;
94647    if( nField && nWorker==0 ){
94648      pKeyInfo->nKeyField = nField;
94649    }
94650    pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
94651    pSorter->nTask = nWorker + 1;
94652    pSorter->iPrev = (u8)(nWorker - 1);
94653    pSorter->bUseThreads = (pSorter->nTask>1);
94654    pSorter->db = db;
94655    for(i=0; i<pSorter->nTask; i++){
94656      SortSubtask *pTask = &pSorter->aTask[i];
94657      pTask->pSorter = pSorter;
94658    }
94659
94660    if( !sqlite3TempInMemory(db) ){
94661      i64 mxCache;                /* Cache size in bytes*/
94662      u32 szPma = sqlite3GlobalConfig.szPma;
94663      pSorter->mnPmaSize = szPma * pgsz;
94664
94665      mxCache = db->aDb[0].pSchema->cache_size;
94666      if( mxCache<0 ){
94667        /* A negative cache-size value C indicates that the cache is abs(C)
94668        ** KiB in size.  */
94669        mxCache = mxCache * -1024;
94670      }else{
94671        mxCache = mxCache * pgsz;
94672      }
94673      mxCache = MIN(mxCache, SQLITE_MAX_PMASZ);
94674      pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
94675
94676      /* Avoid large memory allocations if the application has requested
94677      ** SQLITE_CONFIG_SMALL_MALLOC. */
94678      if( sqlite3GlobalConfig.bSmallMalloc==0 ){
94679        assert( pSorter->iMemory==0 );
94680        pSorter->nMemory = pgsz;
94681        pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
94682        if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
94683      }
94684    }
94685
94686    if( pKeyInfo->nAllField<13
94687     && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
94688     && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL)==0
94689    ){
94690      pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
94691    }
94692  }
94693
94694  return rc;
94695}
94696#undef nWorker   /* Defined at the top of this function */
94697
94698/*
94699** Free the list of sorted records starting at pRecord.
94700*/
94701static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
94702  SorterRecord *p;
94703  SorterRecord *pNext;
94704  for(p=pRecord; p; p=pNext){
94705    pNext = p->u.pNext;
94706    sqlite3DbFree(db, p);
94707  }
94708}
94709
94710/*
94711** Free all resources owned by the object indicated by argument pTask. All
94712** fields of *pTask are zeroed before returning.
94713*/
94714static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){
94715  sqlite3DbFree(db, pTask->pUnpacked);
94716#if SQLITE_MAX_WORKER_THREADS>0
94717  /* pTask->list.aMemory can only be non-zero if it was handed memory
94718  ** from the main thread.  That only occurs SQLITE_MAX_WORKER_THREADS>0 */
94719  if( pTask->list.aMemory ){
94720    sqlite3_free(pTask->list.aMemory);
94721  }else
94722#endif
94723  {
94724    assert( pTask->list.aMemory==0 );
94725    vdbeSorterRecordFree(0, pTask->list.pList);
94726  }
94727  if( pTask->file.pFd ){
94728    sqlite3OsCloseFree(pTask->file.pFd);
94729  }
94730  if( pTask->file2.pFd ){
94731    sqlite3OsCloseFree(pTask->file2.pFd);
94732  }
94733  memset(pTask, 0, sizeof(SortSubtask));
94734}
94735
94736#ifdef SQLITE_DEBUG_SORTER_THREADS
94737static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){
94738  i64 t;
94739  int iTask = (pTask - pTask->pSorter->aTask);
94740  sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
94741  fprintf(stderr, "%lld:%d %s\n", t, iTask, zEvent);
94742}
94743static void vdbeSorterRewindDebug(const char *zEvent){
94744  i64 t;
94745  sqlite3OsCurrentTimeInt64(sqlite3_vfs_find(0), &t);
94746  fprintf(stderr, "%lld:X %s\n", t, zEvent);
94747}
94748static void vdbeSorterPopulateDebug(
94749  SortSubtask *pTask,
94750  const char *zEvent
94751){
94752  i64 t;
94753  int iTask = (pTask - pTask->pSorter->aTask);
94754  sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
94755  fprintf(stderr, "%lld:bg%d %s\n", t, iTask, zEvent);
94756}
94757static void vdbeSorterBlockDebug(
94758  SortSubtask *pTask,
94759  int bBlocked,
94760  const char *zEvent
94761){
94762  if( bBlocked ){
94763    i64 t;
94764    sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
94765    fprintf(stderr, "%lld:main %s\n", t, zEvent);
94766  }
94767}
94768#else
94769# define vdbeSorterWorkDebug(x,y)
94770# define vdbeSorterRewindDebug(y)
94771# define vdbeSorterPopulateDebug(x,y)
94772# define vdbeSorterBlockDebug(x,y,z)
94773#endif
94774
94775#if SQLITE_MAX_WORKER_THREADS>0
94776/*
94777** Join thread pTask->thread.
94778*/
94779static int vdbeSorterJoinThread(SortSubtask *pTask){
94780  int rc = SQLITE_OK;
94781  if( pTask->pThread ){
94782#ifdef SQLITE_DEBUG_SORTER_THREADS
94783    int bDone = pTask->bDone;
94784#endif
94785    void *pRet = SQLITE_INT_TO_PTR(SQLITE_ERROR);
94786    vdbeSorterBlockDebug(pTask, !bDone, "enter");
94787    (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
94788    vdbeSorterBlockDebug(pTask, !bDone, "exit");
94789    rc = SQLITE_PTR_TO_INT(pRet);
94790    assert( pTask->bDone==1 );
94791    pTask->bDone = 0;
94792    pTask->pThread = 0;
94793  }
94794  return rc;
94795}
94796
94797/*
94798** Launch a background thread to run xTask(pIn).
94799*/
94800static int vdbeSorterCreateThread(
94801  SortSubtask *pTask,             /* Thread will use this task object */
94802  void *(*xTask)(void*),          /* Routine to run in a separate thread */
94803  void *pIn                       /* Argument passed into xTask() */
94804){
94805  assert( pTask->pThread==0 && pTask->bDone==0 );
94806  return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
94807}
94808
94809/*
94810** Join all outstanding threads launched by SorterWrite() to create
94811** level-0 PMAs.
94812*/
94813static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){
94814  int rc = rcin;
94815  int i;
94816
94817  /* This function is always called by the main user thread.
94818  **
94819  ** If this function is being called after SorterRewind() has been called,
94820  ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
94821  ** is currently attempt to join one of the other threads. To avoid a race
94822  ** condition where this thread also attempts to join the same object, join
94823  ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
94824  for(i=pSorter->nTask-1; i>=0; i--){
94825    SortSubtask *pTask = &pSorter->aTask[i];
94826    int rc2 = vdbeSorterJoinThread(pTask);
94827    if( rc==SQLITE_OK ) rc = rc2;
94828  }
94829  return rc;
94830}
94831#else
94832# define vdbeSorterJoinAll(x,rcin) (rcin)
94833# define vdbeSorterJoinThread(pTask) SQLITE_OK
94834#endif
94835
94836/*
94837** Allocate a new MergeEngine object capable of handling up to
94838** nReader PmaReader inputs.
94839**
94840** nReader is automatically rounded up to the next power of two.
94841** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up.
94842*/
94843static MergeEngine *vdbeMergeEngineNew(int nReader){
94844  int N = 2;                      /* Smallest power of two >= nReader */
94845  int nByte;                      /* Total bytes of space to allocate */
94846  MergeEngine *pNew;              /* Pointer to allocated object to return */
94847
94848  assert( nReader<=SORTER_MAX_MERGE_COUNT );
94849
94850  while( N<nReader ) N += N;
94851  nByte = sizeof(MergeEngine) + N * (sizeof(int) + sizeof(PmaReader));
94852
94853  pNew = sqlite3FaultSim(100) ? 0 : (MergeEngine*)sqlite3MallocZero(nByte);
94854  if( pNew ){
94855    pNew->nTree = N;
94856    pNew->pTask = 0;
94857    pNew->aReadr = (PmaReader*)&pNew[1];
94858    pNew->aTree = (int*)&pNew->aReadr[N];
94859  }
94860  return pNew;
94861}
94862
94863/*
94864** Free the MergeEngine object passed as the only argument.
94865*/
94866static void vdbeMergeEngineFree(MergeEngine *pMerger){
94867  int i;
94868  if( pMerger ){
94869    for(i=0; i<pMerger->nTree; i++){
94870      vdbePmaReaderClear(&pMerger->aReadr[i]);
94871    }
94872  }
94873  sqlite3_free(pMerger);
94874}
94875
94876/*
94877** Free all resources associated with the IncrMerger object indicated by
94878** the first argument.
94879*/
94880static void vdbeIncrFree(IncrMerger *pIncr){
94881  if( pIncr ){
94882#if SQLITE_MAX_WORKER_THREADS>0
94883    if( pIncr->bUseThread ){
94884      vdbeSorterJoinThread(pIncr->pTask);
94885      if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
94886      if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
94887    }
94888#endif
94889    vdbeMergeEngineFree(pIncr->pMerger);
94890    sqlite3_free(pIncr);
94891  }
94892}
94893
94894/*
94895** Reset a sorting cursor back to its original empty state.
94896*/
94897SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){
94898  int i;
94899  (void)vdbeSorterJoinAll(pSorter, SQLITE_OK);
94900  assert( pSorter->bUseThreads || pSorter->pReader==0 );
94901#if SQLITE_MAX_WORKER_THREADS>0
94902  if( pSorter->pReader ){
94903    vdbePmaReaderClear(pSorter->pReader);
94904    sqlite3DbFree(db, pSorter->pReader);
94905    pSorter->pReader = 0;
94906  }
94907#endif
94908  vdbeMergeEngineFree(pSorter->pMerger);
94909  pSorter->pMerger = 0;
94910  for(i=0; i<pSorter->nTask; i++){
94911    SortSubtask *pTask = &pSorter->aTask[i];
94912    vdbeSortSubtaskCleanup(db, pTask);
94913    pTask->pSorter = pSorter;
94914  }
94915  if( pSorter->list.aMemory==0 ){
94916    vdbeSorterRecordFree(0, pSorter->list.pList);
94917  }
94918  pSorter->list.pList = 0;
94919  pSorter->list.szPMA = 0;
94920  pSorter->bUsePMA = 0;
94921  pSorter->iMemory = 0;
94922  pSorter->mxKeysize = 0;
94923  sqlite3DbFree(db, pSorter->pUnpacked);
94924  pSorter->pUnpacked = 0;
94925}
94926
94927/*
94928** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
94929*/
94930SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
94931  VdbeSorter *pSorter;
94932  assert( pCsr->eCurType==CURTYPE_SORTER );
94933  pSorter = pCsr->uc.pSorter;
94934  if( pSorter ){
94935    sqlite3VdbeSorterReset(db, pSorter);
94936    sqlite3_free(pSorter->list.aMemory);
94937    sqlite3DbFree(db, pSorter);
94938    pCsr->uc.pSorter = 0;
94939  }
94940}
94941
94942#if SQLITE_MAX_MMAP_SIZE>0
94943/*
94944** The first argument is a file-handle open on a temporary file. The file
94945** is guaranteed to be nByte bytes or smaller in size. This function
94946** attempts to extend the file to nByte bytes in size and to ensure that
94947** the VFS has memory mapped it.
94948**
94949** Whether or not the file does end up memory mapped of course depends on
94950** the specific VFS implementation.
94951*/
94952static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){
94953  if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
94954    void *p = 0;
94955    int chunksize = 4*1024;
94956    sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_CHUNK_SIZE, &chunksize);
94957    sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_SIZE_HINT, &nByte);
94958    sqlite3OsFetch(pFd, 0, (int)nByte, &p);
94959    sqlite3OsUnfetch(pFd, 0, p);
94960  }
94961}
94962#else
94963# define vdbeSorterExtendFile(x,y,z)
94964#endif
94965
94966/*
94967** Allocate space for a file-handle and open a temporary file. If successful,
94968** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
94969** Otherwise, set *ppFd to 0 and return an SQLite error code.
94970*/
94971static int vdbeSorterOpenTempFile(
94972  sqlite3 *db,                    /* Database handle doing sort */
94973  i64 nExtend,                    /* Attempt to extend file to this size */
94974  sqlite3_file **ppFd
94975){
94976  int rc;
94977  if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS;
94978  rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
94979      SQLITE_OPEN_TEMP_JOURNAL |
94980      SQLITE_OPEN_READWRITE    | SQLITE_OPEN_CREATE |
94981      SQLITE_OPEN_EXCLUSIVE    | SQLITE_OPEN_DELETEONCLOSE, &rc
94982  );
94983  if( rc==SQLITE_OK ){
94984    i64 max = SQLITE_MAX_MMAP_SIZE;
94985    sqlite3OsFileControlHint(*ppFd, SQLITE_FCNTL_MMAP_SIZE, (void*)&max);
94986    if( nExtend>0 ){
94987      vdbeSorterExtendFile(db, *ppFd, nExtend);
94988    }
94989  }
94990  return rc;
94991}
94992
94993/*
94994** If it has not already been allocated, allocate the UnpackedRecord
94995** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
94996** if no allocation was required), or SQLITE_NOMEM otherwise.
94997*/
94998static int vdbeSortAllocUnpacked(SortSubtask *pTask){
94999  if( pTask->pUnpacked==0 ){
95000    pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
95001    if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
95002    pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
95003    pTask->pUnpacked->errCode = 0;
95004  }
95005  return SQLITE_OK;
95006}
95007
95008
95009/*
95010** Merge the two sorted lists p1 and p2 into a single list.
95011*/
95012static SorterRecord *vdbeSorterMerge(
95013  SortSubtask *pTask,             /* Calling thread context */
95014  SorterRecord *p1,               /* First list to merge */
95015  SorterRecord *p2                /* Second list to merge */
95016){
95017  SorterRecord *pFinal = 0;
95018  SorterRecord **pp = &pFinal;
95019  int bCached = 0;
95020
95021  assert( p1!=0 && p2!=0 );
95022  for(;;){
95023    int res;
95024    res = pTask->xCompare(
95025        pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
95026    );
95027
95028    if( res<=0 ){
95029      *pp = p1;
95030      pp = &p1->u.pNext;
95031      p1 = p1->u.pNext;
95032      if( p1==0 ){
95033        *pp = p2;
95034        break;
95035      }
95036    }else{
95037      *pp = p2;
95038      pp = &p2->u.pNext;
95039      p2 = p2->u.pNext;
95040      bCached = 0;
95041      if( p2==0 ){
95042        *pp = p1;
95043        break;
95044      }
95045    }
95046  }
95047  return pFinal;
95048}
95049
95050/*
95051** Return the SorterCompare function to compare values collected by the
95052** sorter object passed as the only argument.
95053*/
95054static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){
95055  if( p->typeMask==SORTER_TYPE_INTEGER ){
95056    return vdbeSorterCompareInt;
95057  }else if( p->typeMask==SORTER_TYPE_TEXT ){
95058    return vdbeSorterCompareText;
95059  }
95060  return vdbeSorterCompare;
95061}
95062
95063/*
95064** Sort the linked list of records headed at pTask->pList. Return
95065** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if
95066** an error occurs.
95067*/
95068static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
95069  int i;
95070  SorterRecord *p;
95071  int rc;
95072  SorterRecord *aSlot[64];
95073
95074  rc = vdbeSortAllocUnpacked(pTask);
95075  if( rc!=SQLITE_OK ) return rc;
95076
95077  p = pList->pList;
95078  pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
95079  memset(aSlot, 0, sizeof(aSlot));
95080
95081  while( p ){
95082    SorterRecord *pNext;
95083    if( pList->aMemory ){
95084      if( (u8*)p==pList->aMemory ){
95085        pNext = 0;
95086      }else{
95087        assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
95088        pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
95089      }
95090    }else{
95091      pNext = p->u.pNext;
95092    }
95093
95094    p->u.pNext = 0;
95095    for(i=0; aSlot[i]; i++){
95096      p = vdbeSorterMerge(pTask, p, aSlot[i]);
95097      aSlot[i] = 0;
95098    }
95099    aSlot[i] = p;
95100    p = pNext;
95101  }
95102
95103  p = 0;
95104  for(i=0; i<ArraySize(aSlot); i++){
95105    if( aSlot[i]==0 ) continue;
95106    p = p ? vdbeSorterMerge(pTask, p, aSlot[i]) : aSlot[i];
95107  }
95108  pList->pList = p;
95109
95110  assert( pTask->pUnpacked->errCode==SQLITE_OK
95111       || pTask->pUnpacked->errCode==SQLITE_NOMEM
95112  );
95113  return pTask->pUnpacked->errCode;
95114}
95115
95116/*
95117** Initialize a PMA-writer object.
95118*/
95119static void vdbePmaWriterInit(
95120  sqlite3_file *pFd,              /* File handle to write to */
95121  PmaWriter *p,                   /* Object to populate */
95122  int nBuf,                       /* Buffer size */
95123  i64 iStart                      /* Offset of pFd to begin writing at */
95124){
95125  memset(p, 0, sizeof(PmaWriter));
95126  p->aBuffer = (u8*)sqlite3Malloc(nBuf);
95127  if( !p->aBuffer ){
95128    p->eFWErr = SQLITE_NOMEM_BKPT;
95129  }else{
95130    p->iBufEnd = p->iBufStart = (iStart % nBuf);
95131    p->iWriteOff = iStart - p->iBufStart;
95132    p->nBuffer = nBuf;
95133    p->pFd = pFd;
95134  }
95135}
95136
95137/*
95138** Write nData bytes of data to the PMA. Return SQLITE_OK
95139** if successful, or an SQLite error code if an error occurs.
95140*/
95141static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){
95142  int nRem = nData;
95143  while( nRem>0 && p->eFWErr==0 ){
95144    int nCopy = nRem;
95145    if( nCopy>(p->nBuffer - p->iBufEnd) ){
95146      nCopy = p->nBuffer - p->iBufEnd;
95147    }
95148
95149    memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
95150    p->iBufEnd += nCopy;
95151    if( p->iBufEnd==p->nBuffer ){
95152      p->eFWErr = sqlite3OsWrite(p->pFd,
95153          &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
95154          p->iWriteOff + p->iBufStart
95155      );
95156      p->iBufStart = p->iBufEnd = 0;
95157      p->iWriteOff += p->nBuffer;
95158    }
95159    assert( p->iBufEnd<p->nBuffer );
95160
95161    nRem -= nCopy;
95162  }
95163}
95164
95165/*
95166** Flush any buffered data to disk and clean up the PMA-writer object.
95167** The results of using the PMA-writer after this call are undefined.
95168** Return SQLITE_OK if flushing the buffered data succeeds or is not
95169** required. Otherwise, return an SQLite error code.
95170**
95171** Before returning, set *piEof to the offset immediately following the
95172** last byte written to the file.
95173*/
95174static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){
95175  int rc;
95176  if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
95177    p->eFWErr = sqlite3OsWrite(p->pFd,
95178        &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
95179        p->iWriteOff + p->iBufStart
95180    );
95181  }
95182  *piEof = (p->iWriteOff + p->iBufEnd);
95183  sqlite3_free(p->aBuffer);
95184  rc = p->eFWErr;
95185  memset(p, 0, sizeof(PmaWriter));
95186  return rc;
95187}
95188
95189/*
95190** Write value iVal encoded as a varint to the PMA. Return
95191** SQLITE_OK if successful, or an SQLite error code if an error occurs.
95192*/
95193static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){
95194  int nByte;
95195  u8 aByte[10];
95196  nByte = sqlite3PutVarint(aByte, iVal);
95197  vdbePmaWriteBlob(p, aByte, nByte);
95198}
95199
95200/*
95201** Write the current contents of in-memory linked-list pList to a level-0
95202** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
95203** successful, or an SQLite error code otherwise.
95204**
95205** The format of a PMA is:
95206**
95207**     * A varint. This varint contains the total number of bytes of content
95208**       in the PMA (not including the varint itself).
95209**
95210**     * One or more records packed end-to-end in order of ascending keys.
95211**       Each record consists of a varint followed by a blob of data (the
95212**       key). The varint is the number of bytes in the blob of data.
95213*/
95214static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){
95215  sqlite3 *db = pTask->pSorter->db;
95216  int rc = SQLITE_OK;             /* Return code */
95217  PmaWriter writer;               /* Object used to write to the file */
95218
95219#ifdef SQLITE_DEBUG
95220  /* Set iSz to the expected size of file pTask->file after writing the PMA.
95221  ** This is used by an assert() statement at the end of this function.  */
95222  i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
95223#endif
95224
95225  vdbeSorterWorkDebug(pTask, "enter");
95226  memset(&writer, 0, sizeof(PmaWriter));
95227  assert( pList->szPMA>0 );
95228
95229  /* If the first temporary PMA file has not been opened, open it now. */
95230  if( pTask->file.pFd==0 ){
95231    rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
95232    assert( rc!=SQLITE_OK || pTask->file.pFd );
95233    assert( pTask->file.iEof==0 );
95234    assert( pTask->nPMA==0 );
95235  }
95236
95237  /* Try to get the file to memory map */
95238  if( rc==SQLITE_OK ){
95239    vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
95240  }
95241
95242  /* Sort the list */
95243  if( rc==SQLITE_OK ){
95244    rc = vdbeSorterSort(pTask, pList);
95245  }
95246
95247  if( rc==SQLITE_OK ){
95248    SorterRecord *p;
95249    SorterRecord *pNext = 0;
95250
95251    vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
95252                      pTask->file.iEof);
95253    pTask->nPMA++;
95254    vdbePmaWriteVarint(&writer, pList->szPMA);
95255    for(p=pList->pList; p; p=pNext){
95256      pNext = p->u.pNext;
95257      vdbePmaWriteVarint(&writer, p->nVal);
95258      vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
95259      if( pList->aMemory==0 ) sqlite3_free(p);
95260    }
95261    pList->pList = p;
95262    rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
95263  }
95264
95265  vdbeSorterWorkDebug(pTask, "exit");
95266  assert( rc!=SQLITE_OK || pList->pList==0 );
95267  assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
95268  return rc;
95269}
95270
95271/*
95272** Advance the MergeEngine to its next entry.
95273** Set *pbEof to true there is no next entry because
95274** the MergeEngine has reached the end of all its inputs.
95275**
95276** Return SQLITE_OK if successful or an error code if an error occurs.
95277*/
95278static int vdbeMergeEngineStep(
95279  MergeEngine *pMerger,      /* The merge engine to advance to the next row */
95280  int *pbEof                 /* Set TRUE at EOF.  Set false for more content */
95281){
95282  int rc;
95283  int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
95284  SortSubtask *pTask = pMerger->pTask;
95285
95286  /* Advance the current PmaReader */
95287  rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
95288
95289  /* Update contents of aTree[] */
95290  if( rc==SQLITE_OK ){
95291    int i;                      /* Index of aTree[] to recalculate */
95292    PmaReader *pReadr1;         /* First PmaReader to compare */
95293    PmaReader *pReadr2;         /* Second PmaReader to compare */
95294    int bCached = 0;
95295
95296    /* Find the first two PmaReaders to compare. The one that was just
95297    ** advanced (iPrev) and the one next to it in the array.  */
95298    pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
95299    pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
95300
95301    for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
95302      /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */
95303      int iRes;
95304      if( pReadr1->pFd==0 ){
95305        iRes = +1;
95306      }else if( pReadr2->pFd==0 ){
95307        iRes = -1;
95308      }else{
95309        iRes = pTask->xCompare(pTask, &bCached,
95310            pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
95311        );
95312      }
95313
95314      /* If pReadr1 contained the smaller value, set aTree[i] to its index.
95315      ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
95316      ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
95317      ** pKey2 to point to the record belonging to pReadr2.
95318      **
95319      ** Alternatively, if pReadr2 contains the smaller of the two values,
95320      ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare()
95321      ** was actually called above, then pTask->pUnpacked now contains
95322      ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent
95323      ** vdbeSorterCompare() from decoding pReadr2 again.
95324      **
95325      ** If the two values were equal, then the value from the oldest
95326      ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
95327      ** is sorted from oldest to newest, so pReadr1 contains older values
95328      ** than pReadr2 iff (pReadr1<pReadr2).  */
95329      if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){
95330        pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
95331        pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
95332        bCached = 0;
95333      }else{
95334        if( pReadr1->pFd ) bCached = 0;
95335        pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
95336        pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
95337      }
95338    }
95339    *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
95340  }
95341
95342  return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
95343}
95344
95345#if SQLITE_MAX_WORKER_THREADS>0
95346/*
95347** The main routine for background threads that write level-0 PMAs.
95348*/
95349static void *vdbeSorterFlushThread(void *pCtx){
95350  SortSubtask *pTask = (SortSubtask*)pCtx;
95351  int rc;                         /* Return code */
95352  assert( pTask->bDone==0 );
95353  rc = vdbeSorterListToPMA(pTask, &pTask->list);
95354  pTask->bDone = 1;
95355  return SQLITE_INT_TO_PTR(rc);
95356}
95357#endif /* SQLITE_MAX_WORKER_THREADS>0 */
95358
95359/*
95360** Flush the current contents of VdbeSorter.list to a new PMA, possibly
95361** using a background thread.
95362*/
95363static int vdbeSorterFlushPMA(VdbeSorter *pSorter){
95364#if SQLITE_MAX_WORKER_THREADS==0
95365  pSorter->bUsePMA = 1;
95366  return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
95367#else
95368  int rc = SQLITE_OK;
95369  int i;
95370  SortSubtask *pTask = 0;    /* Thread context used to create new PMA */
95371  int nWorker = (pSorter->nTask-1);
95372
95373  /* Set the flag to indicate that at least one PMA has been written.
95374  ** Or will be, anyhow.  */
95375  pSorter->bUsePMA = 1;
95376
95377  /* Select a sub-task to sort and flush the current list of in-memory
95378  ** records to disk. If the sorter is running in multi-threaded mode,
95379  ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
95380  ** the background thread from a sub-tasks previous turn is still running,
95381  ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
95382  ** fall back to using the final sub-task. The first (pSorter->nTask-1)
95383  ** sub-tasks are prefered as they use background threads - the final
95384  ** sub-task uses the main thread. */
95385  for(i=0; i<nWorker; i++){
95386    int iTest = (pSorter->iPrev + i + 1) % nWorker;
95387    pTask = &pSorter->aTask[iTest];
95388    if( pTask->bDone ){
95389      rc = vdbeSorterJoinThread(pTask);
95390    }
95391    if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
95392  }
95393
95394  if( rc==SQLITE_OK ){
95395    if( i==nWorker ){
95396      /* Use the foreground thread for this operation */
95397      rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
95398    }else{
95399      /* Launch a background thread for this operation */
95400      u8 *aMem;
95401      void *pCtx;
95402
95403      assert( pTask!=0 );
95404      assert( pTask->pThread==0 && pTask->bDone==0 );
95405      assert( pTask->list.pList==0 );
95406      assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
95407
95408      aMem = pTask->list.aMemory;
95409      pCtx = (void*)pTask;
95410      pSorter->iPrev = (u8)(pTask - pSorter->aTask);
95411      pTask->list = pSorter->list;
95412      pSorter->list.pList = 0;
95413      pSorter->list.szPMA = 0;
95414      if( aMem ){
95415        pSorter->list.aMemory = aMem;
95416        pSorter->nMemory = sqlite3MallocSize(aMem);
95417      }else if( pSorter->list.aMemory ){
95418        pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
95419        if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
95420      }
95421
95422      rc = vdbeSorterCreateThread(pTask, vdbeSorterFlushThread, pCtx);
95423    }
95424  }
95425
95426  return rc;
95427#endif /* SQLITE_MAX_WORKER_THREADS!=0 */
95428}
95429
95430/*
95431** Add a record to the sorter.
95432*/
95433SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
95434  const VdbeCursor *pCsr,         /* Sorter cursor */
95435  Mem *pVal                       /* Memory cell containing record */
95436){
95437  VdbeSorter *pSorter;
95438  int rc = SQLITE_OK;             /* Return Code */
95439  SorterRecord *pNew;             /* New list element */
95440  int bFlush;                     /* True to flush contents of memory to PMA */
95441  int nReq;                       /* Bytes of memory required */
95442  int nPMA;                       /* Bytes of PMA space required */
95443  int t;                          /* serial type of first record field */
95444
95445  assert( pCsr->eCurType==CURTYPE_SORTER );
95446  pSorter = pCsr->uc.pSorter;
95447  getVarint32NR((const u8*)&pVal->z[1], t);
95448  if( t>0 && t<10 && t!=7 ){
95449    pSorter->typeMask &= SORTER_TYPE_INTEGER;
95450  }else if( t>10 && (t & 0x01) ){
95451    pSorter->typeMask &= SORTER_TYPE_TEXT;
95452  }else{
95453    pSorter->typeMask = 0;
95454  }
95455
95456  assert( pSorter );
95457
95458  /* Figure out whether or not the current contents of memory should be
95459  ** flushed to a PMA before continuing. If so, do so.
95460  **
95461  ** If using the single large allocation mode (pSorter->aMemory!=0), then
95462  ** flush the contents of memory to a new PMA if (a) at least one value is
95463  ** already in memory and (b) the new value will not fit in memory.
95464  **
95465  ** Or, if using separate allocations for each record, flush the contents
95466  ** of memory to a PMA if either of the following are true:
95467  **
95468  **   * The total memory allocated for the in-memory list is greater
95469  **     than (page-size * cache-size), or
95470  **
95471  **   * The total memory allocated for the in-memory list is greater
95472  **     than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
95473  */
95474  nReq = pVal->n + sizeof(SorterRecord);
95475  nPMA = pVal->n + sqlite3VarintLen(pVal->n);
95476  if( pSorter->mxPmaSize ){
95477    if( pSorter->list.aMemory ){
95478      bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
95479    }else{
95480      bFlush = (
95481          (pSorter->list.szPMA > pSorter->mxPmaSize)
95482       || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
95483      );
95484    }
95485    if( bFlush ){
95486      rc = vdbeSorterFlushPMA(pSorter);
95487      pSorter->list.szPMA = 0;
95488      pSorter->iMemory = 0;
95489      assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
95490    }
95491  }
95492
95493  pSorter->list.szPMA += nPMA;
95494  if( nPMA>pSorter->mxKeysize ){
95495    pSorter->mxKeysize = nPMA;
95496  }
95497
95498  if( pSorter->list.aMemory ){
95499    int nMin = pSorter->iMemory + nReq;
95500
95501    if( nMin>pSorter->nMemory ){
95502      u8 *aNew;
95503      sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
95504      int iListOff = -1;
95505      if( pSorter->list.pList ){
95506        iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
95507      }
95508      while( nNew < nMin ) nNew = nNew*2;
95509      if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
95510      if( nNew < nMin ) nNew = nMin;
95511      aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
95512      if( !aNew ) return SQLITE_NOMEM_BKPT;
95513      if( iListOff>=0 ){
95514        pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
95515      }
95516      pSorter->list.aMemory = aNew;
95517      pSorter->nMemory = nNew;
95518    }
95519
95520    pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
95521    pSorter->iMemory += ROUND8(nReq);
95522    if( pSorter->list.pList ){
95523      pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
95524    }
95525  }else{
95526    pNew = (SorterRecord *)sqlite3Malloc(nReq);
95527    if( pNew==0 ){
95528      return SQLITE_NOMEM_BKPT;
95529    }
95530    pNew->u.pNext = pSorter->list.pList;
95531  }
95532
95533  memcpy(SRVAL(pNew), pVal->z, pVal->n);
95534  pNew->nVal = pVal->n;
95535  pSorter->list.pList = pNew;
95536
95537  return rc;
95538}
95539
95540/*
95541** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
95542** of the data stored in aFile[1] is the same as that used by regular PMAs,
95543** except that the number-of-bytes varint is omitted from the start.
95544*/
95545static int vdbeIncrPopulate(IncrMerger *pIncr){
95546  int rc = SQLITE_OK;
95547  int rc2;
95548  i64 iStart = pIncr->iStartOff;
95549  SorterFile *pOut = &pIncr->aFile[1];
95550  SortSubtask *pTask = pIncr->pTask;
95551  MergeEngine *pMerger = pIncr->pMerger;
95552  PmaWriter writer;
95553  assert( pIncr->bEof==0 );
95554
95555  vdbeSorterPopulateDebug(pTask, "enter");
95556
95557  vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
95558  while( rc==SQLITE_OK ){
95559    int dummy;
95560    PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
95561    int nKey = pReader->nKey;
95562    i64 iEof = writer.iWriteOff + writer.iBufEnd;
95563
95564    /* Check if the output file is full or if the input has been exhausted.
95565    ** In either case exit the loop. */
95566    if( pReader->pFd==0 ) break;
95567    if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
95568
95569    /* Write the next key to the output. */
95570    vdbePmaWriteVarint(&writer, nKey);
95571    vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
95572    assert( pIncr->pMerger->pTask==pTask );
95573    rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
95574  }
95575
95576  rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
95577  if( rc==SQLITE_OK ) rc = rc2;
95578  vdbeSorterPopulateDebug(pTask, "exit");
95579  return rc;
95580}
95581
95582#if SQLITE_MAX_WORKER_THREADS>0
95583/*
95584** The main routine for background threads that populate aFile[1] of
95585** multi-threaded IncrMerger objects.
95586*/
95587static void *vdbeIncrPopulateThread(void *pCtx){
95588  IncrMerger *pIncr = (IncrMerger*)pCtx;
95589  void *pRet = SQLITE_INT_TO_PTR( vdbeIncrPopulate(pIncr) );
95590  pIncr->pTask->bDone = 1;
95591  return pRet;
95592}
95593
95594/*
95595** Launch a background thread to populate aFile[1] of pIncr.
95596*/
95597static int vdbeIncrBgPopulate(IncrMerger *pIncr){
95598  void *p = (void*)pIncr;
95599  assert( pIncr->bUseThread );
95600  return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
95601}
95602#endif
95603
95604/*
95605** This function is called when the PmaReader corresponding to pIncr has
95606** finished reading the contents of aFile[0]. Its purpose is to "refill"
95607** aFile[0] such that the PmaReader should start rereading it from the
95608** beginning.
95609**
95610** For single-threaded objects, this is accomplished by literally reading
95611** keys from pIncr->pMerger and repopulating aFile[0].
95612**
95613** For multi-threaded objects, all that is required is to wait until the
95614** background thread is finished (if it is not already) and then swap
95615** aFile[0] and aFile[1] in place. If the contents of pMerger have not
95616** been exhausted, this function also launches a new background thread
95617** to populate the new aFile[1].
95618**
95619** SQLITE_OK is returned on success, or an SQLite error code otherwise.
95620*/
95621static int vdbeIncrSwap(IncrMerger *pIncr){
95622  int rc = SQLITE_OK;
95623
95624#if SQLITE_MAX_WORKER_THREADS>0
95625  if( pIncr->bUseThread ){
95626    rc = vdbeSorterJoinThread(pIncr->pTask);
95627
95628    if( rc==SQLITE_OK ){
95629      SorterFile f0 = pIncr->aFile[0];
95630      pIncr->aFile[0] = pIncr->aFile[1];
95631      pIncr->aFile[1] = f0;
95632    }
95633
95634    if( rc==SQLITE_OK ){
95635      if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
95636        pIncr->bEof = 1;
95637      }else{
95638        rc = vdbeIncrBgPopulate(pIncr);
95639      }
95640    }
95641  }else
95642#endif
95643  {
95644    rc = vdbeIncrPopulate(pIncr);
95645    pIncr->aFile[0] = pIncr->aFile[1];
95646    if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
95647      pIncr->bEof = 1;
95648    }
95649  }
95650
95651  return rc;
95652}
95653
95654/*
95655** Allocate and return a new IncrMerger object to read data from pMerger.
95656**
95657** If an OOM condition is encountered, return NULL. In this case free the
95658** pMerger argument before returning.
95659*/
95660static int vdbeIncrMergerNew(
95661  SortSubtask *pTask,     /* The thread that will be using the new IncrMerger */
95662  MergeEngine *pMerger,   /* The MergeEngine that the IncrMerger will control */
95663  IncrMerger **ppOut      /* Write the new IncrMerger here */
95664){
95665  int rc = SQLITE_OK;
95666  IncrMerger *pIncr = *ppOut = (IncrMerger*)
95667       (sqlite3FaultSim(100) ? 0 : sqlite3MallocZero(sizeof(*pIncr)));
95668  if( pIncr ){
95669    pIncr->pMerger = pMerger;
95670    pIncr->pTask = pTask;
95671    pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
95672    pTask->file2.iEof += pIncr->mxSz;
95673  }else{
95674    vdbeMergeEngineFree(pMerger);
95675    rc = SQLITE_NOMEM_BKPT;
95676  }
95677  return rc;
95678}
95679
95680#if SQLITE_MAX_WORKER_THREADS>0
95681/*
95682** Set the "use-threads" flag on object pIncr.
95683*/
95684static void vdbeIncrMergerSetThreads(IncrMerger *pIncr){
95685  pIncr->bUseThread = 1;
95686  pIncr->pTask->file2.iEof -= pIncr->mxSz;
95687}
95688#endif /* SQLITE_MAX_WORKER_THREADS>0 */
95689
95690
95691
95692/*
95693** Recompute pMerger->aTree[iOut] by comparing the next keys on the
95694** two PmaReaders that feed that entry.  Neither of the PmaReaders
95695** are advanced.  This routine merely does the comparison.
95696*/
95697static void vdbeMergeEngineCompare(
95698  MergeEngine *pMerger,  /* Merge engine containing PmaReaders to compare */
95699  int iOut               /* Store the result in pMerger->aTree[iOut] */
95700){
95701  int i1;
95702  int i2;
95703  int iRes;
95704  PmaReader *p1;
95705  PmaReader *p2;
95706
95707  assert( iOut<pMerger->nTree && iOut>0 );
95708
95709  if( iOut>=(pMerger->nTree/2) ){
95710    i1 = (iOut - pMerger->nTree/2) * 2;
95711    i2 = i1 + 1;
95712  }else{
95713    i1 = pMerger->aTree[iOut*2];
95714    i2 = pMerger->aTree[iOut*2+1];
95715  }
95716
95717  p1 = &pMerger->aReadr[i1];
95718  p2 = &pMerger->aReadr[i2];
95719
95720  if( p1->pFd==0 ){
95721    iRes = i2;
95722  }else if( p2->pFd==0 ){
95723    iRes = i1;
95724  }else{
95725    SortSubtask *pTask = pMerger->pTask;
95726    int bCached = 0;
95727    int res;
95728    assert( pTask->pUnpacked!=0 );  /* from vdbeSortSubtaskMain() */
95729    res = pTask->xCompare(
95730        pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
95731    );
95732    if( res<=0 ){
95733      iRes = i1;
95734    }else{
95735      iRes = i2;
95736    }
95737  }
95738
95739  pMerger->aTree[iOut] = iRes;
95740}
95741
95742/*
95743** Allowed values for the eMode parameter to vdbeMergeEngineInit()
95744** and vdbePmaReaderIncrMergeInit().
95745**
95746** Only INCRINIT_NORMAL is valid in single-threaded builds (when
95747** SQLITE_MAX_WORKER_THREADS==0).  The other values are only used
95748** when there exists one or more separate worker threads.
95749*/
95750#define INCRINIT_NORMAL 0
95751#define INCRINIT_TASK   1
95752#define INCRINIT_ROOT   2
95753
95754/*
95755** Forward reference required as the vdbeIncrMergeInit() and
95756** vdbePmaReaderIncrInit() routines are called mutually recursively when
95757** building a merge tree.
95758*/
95759static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode);
95760
95761/*
95762** Initialize the MergeEngine object passed as the second argument. Once this
95763** function returns, the first key of merged data may be read from the
95764** MergeEngine object in the usual fashion.
95765**
95766** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge
95767** objects attached to the PmaReader objects that the merger reads from have
95768** already been populated, but that they have not yet populated aFile[0] and
95769** set the PmaReader objects up to read from it. In this case all that is
95770** required is to call vdbePmaReaderNext() on each PmaReader to point it at
95771** its first key.
95772**
95773** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use
95774** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data
95775** to pMerger.
95776**
95777** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
95778*/
95779static int vdbeMergeEngineInit(
95780  SortSubtask *pTask,             /* Thread that will run pMerger */
95781  MergeEngine *pMerger,           /* MergeEngine to initialize */
95782  int eMode                       /* One of the INCRINIT_XXX constants */
95783){
95784  int rc = SQLITE_OK;             /* Return code */
95785  int i;                          /* For looping over PmaReader objects */
95786  int nTree;                      /* Number of subtrees to merge */
95787
95788  /* Failure to allocate the merge would have been detected prior to
95789  ** invoking this routine */
95790  assert( pMerger!=0 );
95791
95792  /* eMode is always INCRINIT_NORMAL in single-threaded mode */
95793  assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
95794
95795  /* Verify that the MergeEngine is assigned to a single thread */
95796  assert( pMerger->pTask==0 );
95797  pMerger->pTask = pTask;
95798
95799  nTree = pMerger->nTree;
95800  for(i=0; i<nTree; i++){
95801    if( SQLITE_MAX_WORKER_THREADS>0 && eMode==INCRINIT_ROOT ){
95802      /* PmaReaders should be normally initialized in order, as if they are
95803      ** reading from the same temp file this makes for more linear file IO.
95804      ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
95805      ** in use it will block the vdbePmaReaderNext() call while it uses
95806      ** the main thread to fill its buffer. So calling PmaReaderNext()
95807      ** on this PmaReader before any of the multi-threaded PmaReaders takes
95808      ** better advantage of multi-processor hardware. */
95809      rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
95810    }else{
95811      rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
95812    }
95813    if( rc!=SQLITE_OK ) return rc;
95814  }
95815
95816  for(i=pMerger->nTree-1; i>0; i--){
95817    vdbeMergeEngineCompare(pMerger, i);
95818  }
95819  return pTask->pUnpacked->errCode;
95820}
95821
95822/*
95823** The PmaReader passed as the first argument is guaranteed to be an
95824** incremental-reader (pReadr->pIncr!=0). This function serves to open
95825** and/or initialize the temp file related fields of the IncrMerge
95826** object at (pReadr->pIncr).
95827**
95828** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders
95829** in the sub-tree headed by pReadr are also initialized. Data is then
95830** loaded into the buffers belonging to pReadr and it is set to point to
95831** the first key in its range.
95832**
95833** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed
95834** to be a multi-threaded PmaReader and this function is being called in a
95835** background thread. In this case all PmaReaders in the sub-tree are
95836** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to
95837** pReadr is populated. However, pReadr itself is not set up to point
95838** to its first key. A call to vdbePmaReaderNext() is still required to do
95839** that.
95840**
95841** The reason this function does not call vdbePmaReaderNext() immediately
95842** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has
95843** to block on thread (pTask->thread) before accessing aFile[1]. But, since
95844** this entire function is being run by thread (pTask->thread), that will
95845** lead to the current background thread attempting to join itself.
95846**
95847** Finally, if argument eMode is set to INCRINIT_ROOT, it may be assumed
95848** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
95849** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
95850** In this case vdbePmaReaderNext() is called on all child PmaReaders and
95851** the current PmaReader set to point to the first key in its range.
95852**
95853** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
95854*/
95855static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){
95856  int rc = SQLITE_OK;
95857  IncrMerger *pIncr = pReadr->pIncr;
95858  SortSubtask *pTask = pIncr->pTask;
95859  sqlite3 *db = pTask->pSorter->db;
95860
95861  /* eMode is always INCRINIT_NORMAL in single-threaded mode */
95862  assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
95863
95864  rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
95865
95866  /* Set up the required files for pIncr. A multi-theaded IncrMerge object
95867  ** requires two temp files to itself, whereas a single-threaded object
95868  ** only requires a region of pTask->file2. */
95869  if( rc==SQLITE_OK ){
95870    int mxSz = pIncr->mxSz;
95871#if SQLITE_MAX_WORKER_THREADS>0
95872    if( pIncr->bUseThread ){
95873      rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
95874      if( rc==SQLITE_OK ){
95875        rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
95876      }
95877    }else
95878#endif
95879    /*if( !pIncr->bUseThread )*/{
95880      if( pTask->file2.pFd==0 ){
95881        assert( pTask->file2.iEof>0 );
95882        rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
95883        pTask->file2.iEof = 0;
95884      }
95885      if( rc==SQLITE_OK ){
95886        pIncr->aFile[1].pFd = pTask->file2.pFd;
95887        pIncr->iStartOff = pTask->file2.iEof;
95888        pTask->file2.iEof += mxSz;
95889      }
95890    }
95891  }
95892
95893#if SQLITE_MAX_WORKER_THREADS>0
95894  if( rc==SQLITE_OK && pIncr->bUseThread ){
95895    /* Use the current thread to populate aFile[1], even though this
95896    ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
95897    ** then this function is already running in background thread
95898    ** pIncr->pTask->thread.
95899    **
95900    ** If this is the INCRINIT_ROOT object, then it is running in the
95901    ** main VDBE thread. But that is Ok, as that thread cannot return
95902    ** control to the VDBE or proceed with anything useful until the
95903    ** first results are ready from this merger object anyway.
95904    */
95905    assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK );
95906    rc = vdbeIncrPopulate(pIncr);
95907  }
95908#endif
95909
95910  if( rc==SQLITE_OK && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK) ){
95911    rc = vdbePmaReaderNext(pReadr);
95912  }
95913
95914  return rc;
95915}
95916
95917#if SQLITE_MAX_WORKER_THREADS>0
95918/*
95919** The main routine for vdbePmaReaderIncrMergeInit() operations run in
95920** background threads.
95921*/
95922static void *vdbePmaReaderBgIncrInit(void *pCtx){
95923  PmaReader *pReader = (PmaReader*)pCtx;
95924  void *pRet = SQLITE_INT_TO_PTR(
95925                  vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK)
95926               );
95927  pReader->pIncr->pTask->bDone = 1;
95928  return pRet;
95929}
95930#endif
95931
95932/*
95933** If the PmaReader passed as the first argument is not an incremental-reader
95934** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
95935** the vdbePmaReaderIncrMergeInit() function with the parameters passed to
95936** this routine to initialize the incremental merge.
95937**
95938** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
95939** then a background thread is launched to call vdbePmaReaderIncrMergeInit().
95940** Or, if the IncrMerger is single threaded, the same function is called
95941** using the current thread.
95942*/
95943static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){
95944  IncrMerger *pIncr = pReadr->pIncr;   /* Incremental merger */
95945  int rc = SQLITE_OK;                  /* Return code */
95946  if( pIncr ){
95947#if SQLITE_MAX_WORKER_THREADS>0
95948    assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
95949    if( pIncr->bUseThread ){
95950      void *pCtx = (void*)pReadr;
95951      rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
95952    }else
95953#endif
95954    {
95955      rc = vdbePmaReaderIncrMergeInit(pReadr, eMode);
95956    }
95957  }
95958  return rc;
95959}
95960
95961/*
95962** Allocate a new MergeEngine object to merge the contents of nPMA level-0
95963** PMAs from pTask->file. If no error occurs, set *ppOut to point to
95964** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut
95965** to NULL and return an SQLite error code.
95966**
95967** When this function is called, *piOffset is set to the offset of the
95968** first PMA to read from pTask->file. Assuming no error occurs, it is
95969** set to the offset immediately following the last byte of the last
95970** PMA before returning. If an error does occur, then the final value of
95971** *piOffset is undefined.
95972*/
95973static int vdbeMergeEngineLevel0(
95974  SortSubtask *pTask,             /* Sorter task to read from */
95975  int nPMA,                       /* Number of PMAs to read */
95976  i64 *piOffset,                  /* IN/OUT: Readr offset in pTask->file */
95977  MergeEngine **ppOut             /* OUT: New merge-engine */
95978){
95979  MergeEngine *pNew;              /* Merge engine to return */
95980  i64 iOff = *piOffset;
95981  int i;
95982  int rc = SQLITE_OK;
95983
95984  *ppOut = pNew = vdbeMergeEngineNew(nPMA);
95985  if( pNew==0 ) rc = SQLITE_NOMEM_BKPT;
95986
95987  for(i=0; i<nPMA && rc==SQLITE_OK; i++){
95988    i64 nDummy = 0;
95989    PmaReader *pReadr = &pNew->aReadr[i];
95990    rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
95991    iOff = pReadr->iEof;
95992  }
95993
95994  if( rc!=SQLITE_OK ){
95995    vdbeMergeEngineFree(pNew);
95996    *ppOut = 0;
95997  }
95998  *piOffset = iOff;
95999  return rc;
96000}
96001
96002/*
96003** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of
96004** SORTER_MAX_MERGE_COUNT. The returned value does not include leaf nodes.
96005**
96006** i.e.
96007**
96008**   nPMA<=16    -> TreeDepth() == 0
96009**   nPMA<=256   -> TreeDepth() == 1
96010**   nPMA<=65536 -> TreeDepth() == 2
96011*/
96012static int vdbeSorterTreeDepth(int nPMA){
96013  int nDepth = 0;
96014  i64 nDiv = SORTER_MAX_MERGE_COUNT;
96015  while( nDiv < (i64)nPMA ){
96016    nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
96017    nDepth++;
96018  }
96019  return nDepth;
96020}
96021
96022/*
96023** pRoot is the root of an incremental merge-tree with depth nDepth (according
96024** to vdbeSorterTreeDepth()). pLeaf is the iSeq'th leaf to be added to the
96025** tree, counting from zero. This function adds pLeaf to the tree.
96026**
96027** If successful, SQLITE_OK is returned. If an error occurs, an SQLite error
96028** code is returned and pLeaf is freed.
96029*/
96030static int vdbeSorterAddToTree(
96031  SortSubtask *pTask,             /* Task context */
96032  int nDepth,                     /* Depth of tree according to TreeDepth() */
96033  int iSeq,                       /* Sequence number of leaf within tree */
96034  MergeEngine *pRoot,             /* Root of tree */
96035  MergeEngine *pLeaf              /* Leaf to add to tree */
96036){
96037  int rc = SQLITE_OK;
96038  int nDiv = 1;
96039  int i;
96040  MergeEngine *p = pRoot;
96041  IncrMerger *pIncr;
96042
96043  rc = vdbeIncrMergerNew(pTask, pLeaf, &pIncr);
96044
96045  for(i=1; i<nDepth; i++){
96046    nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
96047  }
96048
96049  for(i=1; i<nDepth && rc==SQLITE_OK; i++){
96050    int iIter = (iSeq / nDiv) % SORTER_MAX_MERGE_COUNT;
96051    PmaReader *pReadr = &p->aReadr[iIter];
96052
96053    if( pReadr->pIncr==0 ){
96054      MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
96055      if( pNew==0 ){
96056        rc = SQLITE_NOMEM_BKPT;
96057      }else{
96058        rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
96059      }
96060    }
96061    if( rc==SQLITE_OK ){
96062      p = pReadr->pIncr->pMerger;
96063      nDiv = nDiv / SORTER_MAX_MERGE_COUNT;
96064    }
96065  }
96066
96067  if( rc==SQLITE_OK ){
96068    p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
96069  }else{
96070    vdbeIncrFree(pIncr);
96071  }
96072  return rc;
96073}
96074
96075/*
96076** This function is called as part of a SorterRewind() operation on a sorter
96077** that has already written two or more level-0 PMAs to one or more temp
96078** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that
96079** can be used to incrementally merge all PMAs on disk.
96080**
96081** If successful, SQLITE_OK is returned and *ppOut set to point to the
96082** MergeEngine object at the root of the tree before returning. Or, if an
96083** error occurs, an SQLite error code is returned and the final value
96084** of *ppOut is undefined.
96085*/
96086static int vdbeSorterMergeTreeBuild(
96087  VdbeSorter *pSorter,       /* The VDBE cursor that implements the sort */
96088  MergeEngine **ppOut        /* Write the MergeEngine here */
96089){
96090  MergeEngine *pMain = 0;
96091  int rc = SQLITE_OK;
96092  int iTask;
96093
96094#if SQLITE_MAX_WORKER_THREADS>0
96095  /* If the sorter uses more than one task, then create the top-level
96096  ** MergeEngine here. This MergeEngine will read data from exactly
96097  ** one PmaReader per sub-task.  */
96098  assert( pSorter->bUseThreads || pSorter->nTask==1 );
96099  if( pSorter->nTask>1 ){
96100    pMain = vdbeMergeEngineNew(pSorter->nTask);
96101    if( pMain==0 ) rc = SQLITE_NOMEM_BKPT;
96102  }
96103#endif
96104
96105  for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
96106    SortSubtask *pTask = &pSorter->aTask[iTask];
96107    assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
96108    if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
96109      MergeEngine *pRoot = 0;     /* Root node of tree for this task */
96110      int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
96111      i64 iReadOff = 0;
96112
96113      if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
96114        rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
96115      }else{
96116        int i;
96117        int iSeq = 0;
96118        pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
96119        if( pRoot==0 ) rc = SQLITE_NOMEM_BKPT;
96120        for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
96121          MergeEngine *pMerger = 0; /* New level-0 PMA merger */
96122          int nReader;              /* Number of level-0 PMAs to merge */
96123
96124          nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
96125          rc = vdbeMergeEngineLevel0(pTask, nReader, &iReadOff, &pMerger);
96126          if( rc==SQLITE_OK ){
96127            rc = vdbeSorterAddToTree(pTask, nDepth, iSeq++, pRoot, pMerger);
96128          }
96129        }
96130      }
96131
96132      if( rc==SQLITE_OK ){
96133#if SQLITE_MAX_WORKER_THREADS>0
96134        if( pMain!=0 ){
96135          rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
96136        }else
96137#endif
96138        {
96139          assert( pMain==0 );
96140          pMain = pRoot;
96141        }
96142      }else{
96143        vdbeMergeEngineFree(pRoot);
96144      }
96145    }
96146  }
96147
96148  if( rc!=SQLITE_OK ){
96149    vdbeMergeEngineFree(pMain);
96150    pMain = 0;
96151  }
96152  *ppOut = pMain;
96153  return rc;
96154}
96155
96156/*
96157** This function is called as part of an sqlite3VdbeSorterRewind() operation
96158** on a sorter that has written two or more PMAs to temporary files. It sets
96159** up either VdbeSorter.pMerger (for single threaded sorters) or pReader
96160** (for multi-threaded sorters) so that it can be used to iterate through
96161** all records stored in the sorter.
96162**
96163** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
96164*/
96165static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
96166  int rc;                         /* Return code */
96167  SortSubtask *pTask0 = &pSorter->aTask[0];
96168  MergeEngine *pMain = 0;
96169#if SQLITE_MAX_WORKER_THREADS
96170  sqlite3 *db = pTask0->pSorter->db;
96171  int i;
96172  SorterCompare xCompare = vdbeSorterGetCompare(pSorter);
96173  for(i=0; i<pSorter->nTask; i++){
96174    pSorter->aTask[i].xCompare = xCompare;
96175  }
96176#endif
96177
96178  rc = vdbeSorterMergeTreeBuild(pSorter, &pMain);
96179  if( rc==SQLITE_OK ){
96180#if SQLITE_MAX_WORKER_THREADS
96181    assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
96182    if( pSorter->bUseThreads ){
96183      int iTask;
96184      PmaReader *pReadr = 0;
96185      SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
96186      rc = vdbeSortAllocUnpacked(pLast);
96187      if( rc==SQLITE_OK ){
96188        pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader));
96189        pSorter->pReader = pReadr;
96190        if( pReadr==0 ) rc = SQLITE_NOMEM_BKPT;
96191      }
96192      if( rc==SQLITE_OK ){
96193        rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
96194        if( rc==SQLITE_OK ){
96195          vdbeIncrMergerSetThreads(pReadr->pIncr);
96196          for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
96197            IncrMerger *pIncr;
96198            if( (pIncr = pMain->aReadr[iTask].pIncr) ){
96199              vdbeIncrMergerSetThreads(pIncr);
96200              assert( pIncr->pTask!=pLast );
96201            }
96202          }
96203          for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
96204            /* Check that:
96205            **
96206            **   a) The incremental merge object is configured to use the
96207            **      right task, and
96208            **   b) If it is using task (nTask-1), it is configured to run
96209            **      in single-threaded mode. This is important, as the
96210            **      root merge (INCRINIT_ROOT) will be using the same task
96211            **      object.
96212            */
96213            PmaReader *p = &pMain->aReadr[iTask];
96214            assert( p->pIncr==0 || (
96215                (p->pIncr->pTask==&pSorter->aTask[iTask])             /* a */
96216             && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0)  /* b */
96217            ));
96218            rc = vdbePmaReaderIncrInit(p, INCRINIT_TASK);
96219          }
96220        }
96221        pMain = 0;
96222      }
96223      if( rc==SQLITE_OK ){
96224        rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT);
96225      }
96226    }else
96227#endif
96228    {
96229      rc = vdbeMergeEngineInit(pTask0, pMain, INCRINIT_NORMAL);
96230      pSorter->pMerger = pMain;
96231      pMain = 0;
96232    }
96233  }
96234
96235  if( rc!=SQLITE_OK ){
96236    vdbeMergeEngineFree(pMain);
96237  }
96238  return rc;
96239}
96240
96241
96242/*
96243** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite,
96244** this function is called to prepare for iterating through the records
96245** in sorted order.
96246*/
96247SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
96248  VdbeSorter *pSorter;
96249  int rc = SQLITE_OK;             /* Return code */
96250
96251  assert( pCsr->eCurType==CURTYPE_SORTER );
96252  pSorter = pCsr->uc.pSorter;
96253  assert( pSorter );
96254
96255  /* If no data has been written to disk, then do not do so now. Instead,
96256  ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
96257  ** from the in-memory list.  */
96258  if( pSorter->bUsePMA==0 ){
96259    if( pSorter->list.pList ){
96260      *pbEof = 0;
96261      rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
96262    }else{
96263      *pbEof = 1;
96264    }
96265    return rc;
96266  }
96267
96268  /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
96269  ** function flushes the contents of memory to disk, it immediately always
96270  ** creates a new list consisting of a single key immediately afterwards.
96271  ** So the list is never empty at this point.  */
96272  assert( pSorter->list.pList );
96273  rc = vdbeSorterFlushPMA(pSorter);
96274
96275  /* Join all threads */
96276  rc = vdbeSorterJoinAll(pSorter, rc);
96277
96278  vdbeSorterRewindDebug("rewind");
96279
96280  /* Assuming no errors have occurred, set up a merger structure to
96281  ** incrementally read and merge all remaining PMAs.  */
96282  assert( pSorter->pReader==0 );
96283  if( rc==SQLITE_OK ){
96284    rc = vdbeSorterSetupMerge(pSorter);
96285    *pbEof = 0;
96286  }
96287
96288  vdbeSorterRewindDebug("rewinddone");
96289  return rc;
96290}
96291
96292/*
96293** Advance to the next element in the sorter.  Return value:
96294**
96295**    SQLITE_OK     success
96296**    SQLITE_DONE   end of data
96297**    otherwise     some kind of error.
96298*/
96299SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr){
96300  VdbeSorter *pSorter;
96301  int rc;                         /* Return code */
96302
96303  assert( pCsr->eCurType==CURTYPE_SORTER );
96304  pSorter = pCsr->uc.pSorter;
96305  assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
96306  if( pSorter->bUsePMA ){
96307    assert( pSorter->pReader==0 || pSorter->pMerger==0 );
96308    assert( pSorter->bUseThreads==0 || pSorter->pReader );
96309    assert( pSorter->bUseThreads==1 || pSorter->pMerger );
96310#if SQLITE_MAX_WORKER_THREADS>0
96311    if( pSorter->bUseThreads ){
96312      rc = vdbePmaReaderNext(pSorter->pReader);
96313      if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
96314    }else
96315#endif
96316    /*if( !pSorter->bUseThreads )*/ {
96317      int res = 0;
96318      assert( pSorter->pMerger!=0 );
96319      assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
96320      rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
96321      if( rc==SQLITE_OK && res ) rc = SQLITE_DONE;
96322    }
96323  }else{
96324    SorterRecord *pFree = pSorter->list.pList;
96325    pSorter->list.pList = pFree->u.pNext;
96326    pFree->u.pNext = 0;
96327    if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
96328    rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
96329  }
96330  return rc;
96331}
96332
96333/*
96334** Return a pointer to a buffer owned by the sorter that contains the
96335** current key.
96336*/
96337static void *vdbeSorterRowkey(
96338  const VdbeSorter *pSorter,      /* Sorter object */
96339  int *pnKey                      /* OUT: Size of current key in bytes */
96340){
96341  void *pKey;
96342  if( pSorter->bUsePMA ){
96343    PmaReader *pReader;
96344#if SQLITE_MAX_WORKER_THREADS>0
96345    if( pSorter->bUseThreads ){
96346      pReader = pSorter->pReader;
96347    }else
96348#endif
96349    /*if( !pSorter->bUseThreads )*/{
96350      pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
96351    }
96352    *pnKey = pReader->nKey;
96353    pKey = pReader->aKey;
96354  }else{
96355    *pnKey = pSorter->list.pList->nVal;
96356    pKey = SRVAL(pSorter->list.pList);
96357  }
96358  return pKey;
96359}
96360
96361/*
96362** Copy the current sorter key into the memory cell pOut.
96363*/
96364SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
96365  VdbeSorter *pSorter;
96366  void *pKey; int nKey;           /* Sorter key to copy into pOut */
96367
96368  assert( pCsr->eCurType==CURTYPE_SORTER );
96369  pSorter = pCsr->uc.pSorter;
96370  pKey = vdbeSorterRowkey(pSorter, &nKey);
96371  if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){
96372    return SQLITE_NOMEM_BKPT;
96373  }
96374  pOut->n = nKey;
96375  MemSetTypeFlag(pOut, MEM_Blob);
96376  memcpy(pOut->z, pKey, nKey);
96377
96378  return SQLITE_OK;
96379}
96380
96381/*
96382** Compare the key in memory cell pVal with the key that the sorter cursor
96383** passed as the first argument currently points to. For the purposes of
96384** the comparison, ignore the rowid field at the end of each record.
96385**
96386** If the sorter cursor key contains any NULL values, consider it to be
96387** less than pVal. Even if pVal also contains NULL values.
96388**
96389** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
96390** Otherwise, set *pRes to a negative, zero or positive value if the
96391** key in pVal is smaller than, equal to or larger than the current sorter
96392** key.
96393**
96394** This routine forms the core of the OP_SorterCompare opcode, which in
96395** turn is used to verify uniqueness when constructing a UNIQUE INDEX.
96396*/
96397SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
96398  const VdbeCursor *pCsr,         /* Sorter cursor */
96399  Mem *pVal,                      /* Value to compare to current sorter key */
96400  int nKeyCol,                    /* Compare this many columns */
96401  int *pRes                       /* OUT: Result of comparison */
96402){
96403  VdbeSorter *pSorter;
96404  UnpackedRecord *r2;
96405  KeyInfo *pKeyInfo;
96406  int i;
96407  void *pKey; int nKey;           /* Sorter key to compare pVal with */
96408
96409  assert( pCsr->eCurType==CURTYPE_SORTER );
96410  pSorter = pCsr->uc.pSorter;
96411  r2 = pSorter->pUnpacked;
96412  pKeyInfo = pCsr->pKeyInfo;
96413  if( r2==0 ){
96414    r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
96415    if( r2==0 ) return SQLITE_NOMEM_BKPT;
96416    r2->nField = nKeyCol;
96417  }
96418  assert( r2->nField==nKeyCol );
96419
96420  pKey = vdbeSorterRowkey(pSorter, &nKey);
96421  sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, r2);
96422  for(i=0; i<nKeyCol; i++){
96423    if( r2->aMem[i].flags & MEM_Null ){
96424      *pRes = -1;
96425      return SQLITE_OK;
96426    }
96427  }
96428
96429  *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
96430  return SQLITE_OK;
96431}
96432
96433/************** End of vdbesort.c ********************************************/
96434/************** Begin file vdbevtab.c ****************************************/
96435/*
96436** 2020-03-23
96437**
96438** The author disclaims copyright to this source code.  In place of
96439** a legal notice, here is a blessing:
96440**
96441**    May you do good and not evil.
96442**    May you find forgiveness for yourself and forgive others.
96443**    May you share freely, never taking more than you give.
96444**
96445*************************************************************************
96446**
96447** This file implements virtual-tables for examining the bytecode content
96448** of a prepared statement.
96449*/
96450/* #include "sqliteInt.h" */
96451#if defined(SQLITE_ENABLE_BYTECODE_VTAB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
96452/* #include "vdbeInt.h" */
96453
96454/* An instance of the bytecode() table-valued function.
96455*/
96456typedef struct bytecodevtab bytecodevtab;
96457struct bytecodevtab {
96458  sqlite3_vtab base;     /* Base class - must be first */
96459  sqlite3 *db;           /* Database connection */
96460  int bTablesUsed;       /* 2 for tables_used().  0 for bytecode(). */
96461};
96462
96463/* A cursor for scanning through the bytecode
96464*/
96465typedef struct bytecodevtab_cursor bytecodevtab_cursor;
96466struct bytecodevtab_cursor {
96467  sqlite3_vtab_cursor base;  /* Base class - must be first */
96468  sqlite3_stmt *pStmt;       /* The statement whose bytecode is displayed */
96469  int iRowid;                /* The rowid of the output table */
96470  int iAddr;                 /* Address */
96471  int needFinalize;          /* Cursors owns pStmt and must finalize it */
96472  int showSubprograms;       /* Provide a listing of subprograms */
96473  Op *aOp;                   /* Operand array */
96474  char *zP4;                 /* Rendered P4 value */
96475  const char *zType;         /* tables_used.type */
96476  const char *zSchema;       /* tables_used.schema */
96477  const char *zName;         /* tables_used.name */
96478  Mem sub;                   /* Subprograms */
96479};
96480
96481/*
96482** Create a new bytecode() table-valued function.
96483*/
96484static int bytecodevtabConnect(
96485  sqlite3 *db,
96486  void *pAux,
96487  int argc, const char *const*argv,
96488  sqlite3_vtab **ppVtab,
96489  char **pzErr
96490){
96491  bytecodevtab *pNew;
96492  int rc;
96493  int isTabUsed = pAux!=0;
96494  const char *azSchema[2] = {
96495    /* bytecode() schema */
96496    "CREATE TABLE x("
96497      "addr INT,"
96498      "opcode TEXT,"
96499      "p1 INT,"
96500      "p2 INT,"
96501      "p3 INT,"
96502      "p4 TEXT,"
96503      "p5 INT,"
96504      "comment TEXT,"
96505      "subprog TEXT,"
96506      "stmt HIDDEN"
96507    ");",
96508
96509    /* Tables_used() schema */
96510    "CREATE TABLE x("
96511      "type TEXT,"
96512      "schema TEXT,"
96513      "name TEXT,"
96514      "wr INT,"
96515      "subprog TEXT,"
96516      "stmt HIDDEN"
96517   ");"
96518  };
96519
96520  rc = sqlite3_declare_vtab(db, azSchema[isTabUsed]);
96521  if( rc==SQLITE_OK ){
96522    pNew = sqlite3_malloc( sizeof(*pNew) );
96523    *ppVtab = (sqlite3_vtab*)pNew;
96524    if( pNew==0 ) return SQLITE_NOMEM;
96525    memset(pNew, 0, sizeof(*pNew));
96526    pNew->db = db;
96527    pNew->bTablesUsed = isTabUsed*2;
96528  }
96529  return rc;
96530}
96531
96532/*
96533** This method is the destructor for bytecodevtab objects.
96534*/
96535static int bytecodevtabDisconnect(sqlite3_vtab *pVtab){
96536  bytecodevtab *p = (bytecodevtab*)pVtab;
96537  sqlite3_free(p);
96538  return SQLITE_OK;
96539}
96540
96541/*
96542** Constructor for a new bytecodevtab_cursor object.
96543*/
96544static int bytecodevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
96545  bytecodevtab *pVTab = (bytecodevtab*)p;
96546  bytecodevtab_cursor *pCur;
96547  pCur = sqlite3_malloc( sizeof(*pCur) );
96548  if( pCur==0 ) return SQLITE_NOMEM;
96549  memset(pCur, 0, sizeof(*pCur));
96550  sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
96551  *ppCursor = &pCur->base;
96552  return SQLITE_OK;
96553}
96554
96555/*
96556** Clear all internal content from a bytecodevtab cursor.
96557*/
96558static void bytecodevtabCursorClear(bytecodevtab_cursor *pCur){
96559  sqlite3_free(pCur->zP4);
96560  pCur->zP4 = 0;
96561  sqlite3VdbeMemRelease(&pCur->sub);
96562  sqlite3VdbeMemSetNull(&pCur->sub);
96563  if( pCur->needFinalize ){
96564    sqlite3_finalize(pCur->pStmt);
96565  }
96566  pCur->pStmt = 0;
96567  pCur->needFinalize = 0;
96568  pCur->zType = 0;
96569  pCur->zSchema = 0;
96570  pCur->zName = 0;
96571}
96572
96573/*
96574** Destructor for a bytecodevtab_cursor.
96575*/
96576static int bytecodevtabClose(sqlite3_vtab_cursor *cur){
96577  bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
96578  bytecodevtabCursorClear(pCur);
96579  sqlite3_free(pCur);
96580  return SQLITE_OK;
96581}
96582
96583
96584/*
96585** Advance a bytecodevtab_cursor to its next row of output.
96586*/
96587static int bytecodevtabNext(sqlite3_vtab_cursor *cur){
96588  bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
96589  bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
96590  int rc;
96591  if( pCur->zP4 ){
96592    sqlite3_free(pCur->zP4);
96593    pCur->zP4 = 0;
96594  }
96595  if( pCur->zName ){
96596    pCur->zName = 0;
96597    pCur->zType = 0;
96598    pCur->zSchema = 0;
96599  }
96600  rc = sqlite3VdbeNextOpcode(
96601           (Vdbe*)pCur->pStmt,
96602           pCur->showSubprograms ? &pCur->sub : 0,
96603           pTab->bTablesUsed,
96604           &pCur->iRowid,
96605           &pCur->iAddr,
96606           &pCur->aOp);
96607  if( rc!=SQLITE_OK ){
96608    sqlite3VdbeMemSetNull(&pCur->sub);
96609    pCur->aOp = 0;
96610  }
96611  return SQLITE_OK;
96612}
96613
96614/*
96615** Return TRUE if the cursor has been moved off of the last
96616** row of output.
96617*/
96618static int bytecodevtabEof(sqlite3_vtab_cursor *cur){
96619  bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
96620  return pCur->aOp==0;
96621}
96622
96623/*
96624** Return values of columns for the row at which the bytecodevtab_cursor
96625** is currently pointing.
96626*/
96627static int bytecodevtabColumn(
96628  sqlite3_vtab_cursor *cur,   /* The cursor */
96629  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
96630  int i                       /* Which column to return */
96631){
96632  bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
96633  bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
96634  Op *pOp = pCur->aOp + pCur->iAddr;
96635  if( pVTab->bTablesUsed ){
96636    if( i==4 ){
96637      i = 8;
96638    }else{
96639      if( i<=2 && pCur->zType==0 ){
96640        Schema *pSchema;
96641        HashElem *k;
96642        int iDb = pOp->p3;
96643        int iRoot = pOp->p2;
96644        sqlite3 *db = pVTab->db;
96645        pSchema = db->aDb[iDb].pSchema;
96646        pCur->zSchema = db->aDb[iDb].zDbSName;
96647        for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
96648          Table *pTab = (Table*)sqliteHashData(k);
96649          if( !IsVirtual(pTab) && pTab->tnum==iRoot ){
96650            pCur->zName = pTab->zName;
96651            pCur->zType = "table";
96652            break;
96653          }
96654        }
96655        if( pCur->zName==0 ){
96656          for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){
96657            Index *pIdx = (Index*)sqliteHashData(k);
96658            if( pIdx->tnum==iRoot ){
96659              pCur->zName = pIdx->zName;
96660              pCur->zType = "index";
96661            }
96662          }
96663        }
96664      }
96665      i += 10;
96666    }
96667  }
96668  switch( i ){
96669    case 0:   /* addr */
96670      sqlite3_result_int(ctx, pCur->iAddr);
96671      break;
96672    case 1:   /* opcode */
96673      sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
96674                          -1, SQLITE_STATIC);
96675      break;
96676    case 2:   /* p1 */
96677      sqlite3_result_int(ctx, pOp->p1);
96678      break;
96679    case 3:   /* p2 */
96680      sqlite3_result_int(ctx, pOp->p2);
96681      break;
96682    case 4:   /* p3 */
96683      sqlite3_result_int(ctx, pOp->p3);
96684      break;
96685    case 5:   /* p4 */
96686    case 7:   /* comment */
96687      if( pCur->zP4==0 ){
96688        pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
96689      }
96690      if( i==5 ){
96691        sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC);
96692      }else{
96693#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
96694        char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
96695        sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
96696#endif
96697      }
96698      break;
96699    case 6:     /* p5 */
96700      sqlite3_result_int(ctx, pOp->p5);
96701      break;
96702    case 8: {   /* subprog */
96703      Op *aOp = pCur->aOp;
96704      assert( aOp[0].opcode==OP_Init );
96705      assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 );
96706      if( pCur->iRowid==pCur->iAddr+1 ){
96707        break;  /* Result is NULL for the main program */
96708      }else if( aOp[0].p4.z!=0 ){
96709         sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC);
96710      }else{
96711         sqlite3_result_text(ctx, "(FK)", 4, SQLITE_STATIC);
96712      }
96713      break;
96714    }
96715    case 10:  /* tables_used.type */
96716      sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
96717      break;
96718    case 11:  /* tables_used.schema */
96719      sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
96720      break;
96721    case 12:  /* tables_used.name */
96722      sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
96723      break;
96724    case 13:  /* tables_used.wr */
96725      sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite);
96726      break;
96727  }
96728  return SQLITE_OK;
96729}
96730
96731/*
96732** Return the rowid for the current row.  In this implementation, the
96733** rowid is the same as the output value.
96734*/
96735static int bytecodevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
96736  bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
96737  *pRowid = pCur->iRowid;
96738  return SQLITE_OK;
96739}
96740
96741/*
96742** Initialize a cursor.
96743**
96744**    idxNum==0     means show all subprograms
96745**    idxNum==1     means show only the main bytecode and omit subprograms.
96746*/
96747static int bytecodevtabFilter(
96748  sqlite3_vtab_cursor *pVtabCursor,
96749  int idxNum, const char *idxStr,
96750  int argc, sqlite3_value **argv
96751){
96752  bytecodevtab_cursor *pCur = (bytecodevtab_cursor *)pVtabCursor;
96753  bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
96754  int rc = SQLITE_OK;
96755
96756  bytecodevtabCursorClear(pCur);
96757  pCur->iRowid = 0;
96758  pCur->iAddr = 0;
96759  pCur->showSubprograms = idxNum==0;
96760  assert( argc==1 );
96761  if( sqlite3_value_type(argv[0])==SQLITE_TEXT ){
96762    const char *zSql = (const char*)sqlite3_value_text(argv[0]);
96763    if( zSql==0 ){
96764      rc = SQLITE_NOMEM;
96765    }else{
96766      rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
96767      pCur->needFinalize = 1;
96768    }
96769  }else{
96770    pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
96771  }
96772  if( pCur->pStmt==0 ){
96773    pVTab->base.zErrMsg = sqlite3_mprintf(
96774       "argument to %s() is not a valid SQL statement",
96775       pVTab->bTablesUsed ? "tables_used" : "bytecode"
96776    );
96777    rc = SQLITE_ERROR;
96778  }else{
96779    bytecodevtabNext(pVtabCursor);
96780  }
96781  return rc;
96782}
96783
96784/*
96785** We must have a single stmt=? constraint that will be passed through
96786** into the xFilter method.  If there is no valid stmt=? constraint,
96787** then return an SQLITE_CONSTRAINT error.
96788*/
96789static int bytecodevtabBestIndex(
96790  sqlite3_vtab *tab,
96791  sqlite3_index_info *pIdxInfo
96792){
96793  int i;
96794  int rc = SQLITE_CONSTRAINT;
96795  struct sqlite3_index_constraint *p;
96796  bytecodevtab *pVTab = (bytecodevtab*)tab;
96797  int iBaseCol = pVTab->bTablesUsed ? 4 : 8;
96798  pIdxInfo->estimatedCost = (double)100;
96799  pIdxInfo->estimatedRows = 100;
96800  pIdxInfo->idxNum = 0;
96801  for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
96802    if( p->usable==0 ) continue;
96803    if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){
96804      rc = SQLITE_OK;
96805      pIdxInfo->aConstraintUsage[i].omit = 1;
96806      pIdxInfo->aConstraintUsage[i].argvIndex = 1;
96807    }
96808    if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
96809      pIdxInfo->aConstraintUsage[i].omit = 1;
96810      pIdxInfo->idxNum = 1;
96811    }
96812  }
96813  return rc;
96814}
96815
96816/*
96817** This following structure defines all the methods for the
96818** virtual table.
96819*/
96820static sqlite3_module bytecodevtabModule = {
96821  /* iVersion    */ 0,
96822  /* xCreate     */ 0,
96823  /* xConnect    */ bytecodevtabConnect,
96824  /* xBestIndex  */ bytecodevtabBestIndex,
96825  /* xDisconnect */ bytecodevtabDisconnect,
96826  /* xDestroy    */ 0,
96827  /* xOpen       */ bytecodevtabOpen,
96828  /* xClose      */ bytecodevtabClose,
96829  /* xFilter     */ bytecodevtabFilter,
96830  /* xNext       */ bytecodevtabNext,
96831  /* xEof        */ bytecodevtabEof,
96832  /* xColumn     */ bytecodevtabColumn,
96833  /* xRowid      */ bytecodevtabRowid,
96834  /* xUpdate     */ 0,
96835  /* xBegin      */ 0,
96836  /* xSync       */ 0,
96837  /* xCommit     */ 0,
96838  /* xRollback   */ 0,
96839  /* xFindMethod */ 0,
96840  /* xRename     */ 0,
96841  /* xSavepoint  */ 0,
96842  /* xRelease    */ 0,
96843  /* xRollbackTo */ 0,
96844  /* xShadowName */ 0
96845};
96846
96847
96848SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){
96849  int rc;
96850  rc = sqlite3_create_module(db, "bytecode", &bytecodevtabModule, 0);
96851  if( rc==SQLITE_OK ){
96852    rc = sqlite3_create_module(db, "tables_used", &bytecodevtabModule, &db);
96853  }
96854  return rc;
96855}
96856#elif defined(SQLITE_ENABLE_BYTECODE_VTAB)
96857SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){ return SQLITE_OK; }
96858#endif /* SQLITE_ENABLE_BYTECODE_VTAB */
96859
96860/************** End of vdbevtab.c ********************************************/
96861/************** Begin file memjournal.c **************************************/
96862/*
96863** 2008 October 7
96864**
96865** The author disclaims copyright to this source code.  In place of
96866** a legal notice, here is a blessing:
96867**
96868**    May you do good and not evil.
96869**    May you find forgiveness for yourself and forgive others.
96870**    May you share freely, never taking more than you give.
96871**
96872*************************************************************************
96873**
96874** This file contains code use to implement an in-memory rollback journal.
96875** The in-memory rollback journal is used to journal transactions for
96876** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
96877**
96878** Update:  The in-memory journal is also used to temporarily cache
96879** smaller journals that are not critical for power-loss recovery.
96880** For example, statement journals that are not too big will be held
96881** entirely in memory, thus reducing the number of file I/O calls, and
96882** more importantly, reducing temporary file creation events.  If these
96883** journals become too large for memory, they are spilled to disk.  But
96884** in the common case, they are usually small and no file I/O needs to
96885** occur.
96886*/
96887/* #include "sqliteInt.h" */
96888
96889/* Forward references to internal structures */
96890typedef struct MemJournal MemJournal;
96891typedef struct FilePoint FilePoint;
96892typedef struct FileChunk FileChunk;
96893
96894/*
96895** The rollback journal is composed of a linked list of these structures.
96896**
96897** The zChunk array is always at least 8 bytes in size - usually much more.
96898** Its actual size is stored in the MemJournal.nChunkSize variable.
96899*/
96900struct FileChunk {
96901  FileChunk *pNext;               /* Next chunk in the journal */
96902  u8 zChunk[8];                   /* Content of this chunk */
96903};
96904
96905/*
96906** By default, allocate this many bytes of memory for each FileChunk object.
96907*/
96908#define MEMJOURNAL_DFLT_FILECHUNKSIZE 1024
96909
96910/*
96911** For chunk size nChunkSize, return the number of bytes that should
96912** be allocated for each FileChunk structure.
96913*/
96914#define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
96915
96916/*
96917** An instance of this object serves as a cursor into the rollback journal.
96918** The cursor can be either for reading or writing.
96919*/
96920struct FilePoint {
96921  sqlite3_int64 iOffset;          /* Offset from the beginning of the file */
96922  FileChunk *pChunk;              /* Specific chunk into which cursor points */
96923};
96924
96925/*
96926** This structure is a subclass of sqlite3_file. Each open memory-journal
96927** is an instance of this class.
96928*/
96929struct MemJournal {
96930  const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
96931  int nChunkSize;                 /* In-memory chunk-size */
96932
96933  int nSpill;                     /* Bytes of data before flushing */
96934  int nSize;                      /* Bytes of data currently in memory */
96935  FileChunk *pFirst;              /* Head of in-memory chunk-list */
96936  FilePoint endpoint;             /* Pointer to the end of the file */
96937  FilePoint readpoint;            /* Pointer to the end of the last xRead() */
96938
96939  int flags;                      /* xOpen flags */
96940  sqlite3_vfs *pVfs;              /* The "real" underlying VFS */
96941  const char *zJournal;           /* Name of the journal file */
96942};
96943
96944/*
96945** Read data from the in-memory journal file.  This is the implementation
96946** of the sqlite3_vfs.xRead method.
96947*/
96948static int memjrnlRead(
96949  sqlite3_file *pJfd,    /* The journal file from which to read */
96950  void *zBuf,            /* Put the results here */
96951  int iAmt,              /* Number of bytes to read */
96952  sqlite_int64 iOfst     /* Begin reading at this offset */
96953){
96954  MemJournal *p = (MemJournal *)pJfd;
96955  u8 *zOut = zBuf;
96956  int nRead = iAmt;
96957  int iChunkOffset;
96958  FileChunk *pChunk;
96959
96960  if( (iAmt+iOfst)>p->endpoint.iOffset ){
96961    return SQLITE_IOERR_SHORT_READ;
96962  }
96963  assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
96964  if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
96965    sqlite3_int64 iOff = 0;
96966    for(pChunk=p->pFirst;
96967        ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
96968        pChunk=pChunk->pNext
96969    ){
96970      iOff += p->nChunkSize;
96971    }
96972  }else{
96973    pChunk = p->readpoint.pChunk;
96974    assert( pChunk!=0 );
96975  }
96976
96977  iChunkOffset = (int)(iOfst%p->nChunkSize);
96978  do {
96979    int iSpace = p->nChunkSize - iChunkOffset;
96980    int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
96981    memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
96982    zOut += nCopy;
96983    nRead -= iSpace;
96984    iChunkOffset = 0;
96985  } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
96986  p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
96987  p->readpoint.pChunk = pChunk;
96988
96989  return SQLITE_OK;
96990}
96991
96992/*
96993** Free the list of FileChunk structures headed at MemJournal.pFirst.
96994*/
96995static void memjrnlFreeChunks(MemJournal *p){
96996  FileChunk *pIter;
96997  FileChunk *pNext;
96998  for(pIter=p->pFirst; pIter; pIter=pNext){
96999    pNext = pIter->pNext;
97000    sqlite3_free(pIter);
97001  }
97002  p->pFirst = 0;
97003}
97004
97005/*
97006** Flush the contents of memory to a real file on disk.
97007*/
97008static int memjrnlCreateFile(MemJournal *p){
97009  int rc;
97010  sqlite3_file *pReal = (sqlite3_file*)p;
97011  MemJournal copy = *p;
97012
97013  memset(p, 0, sizeof(MemJournal));
97014  rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0);
97015  if( rc==SQLITE_OK ){
97016    int nChunk = copy.nChunkSize;
97017    i64 iOff = 0;
97018    FileChunk *pIter;
97019    for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
97020      if( iOff + nChunk > copy.endpoint.iOffset ){
97021        nChunk = copy.endpoint.iOffset - iOff;
97022      }
97023      rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
97024      if( rc ) break;
97025      iOff += nChunk;
97026    }
97027    if( rc==SQLITE_OK ){
97028      /* No error has occurred. Free the in-memory buffers. */
97029      memjrnlFreeChunks(&copy);
97030    }
97031  }
97032  if( rc!=SQLITE_OK ){
97033    /* If an error occurred while creating or writing to the file, restore
97034    ** the original before returning. This way, SQLite uses the in-memory
97035    ** journal data to roll back changes made to the internal page-cache
97036    ** before this function was called.  */
97037    sqlite3OsClose(pReal);
97038    *p = copy;
97039  }
97040  return rc;
97041}
97042
97043
97044/*
97045** Write data to the file.
97046*/
97047static int memjrnlWrite(
97048  sqlite3_file *pJfd,    /* The journal file into which to write */
97049  const void *zBuf,      /* Take data to be written from here */
97050  int iAmt,              /* Number of bytes to write */
97051  sqlite_int64 iOfst     /* Begin writing at this offset into the file */
97052){
97053  MemJournal *p = (MemJournal *)pJfd;
97054  int nWrite = iAmt;
97055  u8 *zWrite = (u8 *)zBuf;
97056
97057  /* If the file should be created now, create it and write the new data
97058  ** into the file on disk. */
97059  if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
97060    int rc = memjrnlCreateFile(p);
97061    if( rc==SQLITE_OK ){
97062      rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst);
97063    }
97064    return rc;
97065  }
97066
97067  /* If the contents of this write should be stored in memory */
97068  else{
97069    /* An in-memory journal file should only ever be appended to. Random
97070    ** access writes are not required. The only exception to this is when
97071    ** the in-memory journal is being used by a connection using the
97072    ** atomic-write optimization. In this case the first 28 bytes of the
97073    ** journal file may be written as part of committing the transaction. */
97074    assert( iOfst==p->endpoint.iOffset || iOfst==0 );
97075#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
97076 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
97077    if( iOfst==0 && p->pFirst ){
97078      assert( p->nChunkSize>iAmt );
97079      memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
97080    }else
97081#else
97082    assert( iOfst>0 || p->pFirst==0 );
97083#endif
97084    {
97085      while( nWrite>0 ){
97086        FileChunk *pChunk = p->endpoint.pChunk;
97087        int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
97088        int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
97089
97090        if( iChunkOffset==0 ){
97091          /* New chunk is required to extend the file. */
97092          FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
97093          if( !pNew ){
97094            return SQLITE_IOERR_NOMEM_BKPT;
97095          }
97096          pNew->pNext = 0;
97097          if( pChunk ){
97098            assert( p->pFirst );
97099            pChunk->pNext = pNew;
97100          }else{
97101            assert( !p->pFirst );
97102            p->pFirst = pNew;
97103          }
97104          p->endpoint.pChunk = pNew;
97105        }
97106
97107        memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace);
97108        zWrite += iSpace;
97109        nWrite -= iSpace;
97110        p->endpoint.iOffset += iSpace;
97111      }
97112      p->nSize = iAmt + iOfst;
97113    }
97114  }
97115
97116  return SQLITE_OK;
97117}
97118
97119/*
97120** Truncate the file.
97121**
97122** If the journal file is already on disk, truncate it there. Or, if it
97123** is still in main memory but is being truncated to zero bytes in size,
97124** ignore
97125*/
97126static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
97127  MemJournal *p = (MemJournal *)pJfd;
97128  if( ALWAYS(size==0) ){
97129    memjrnlFreeChunks(p);
97130    p->nSize = 0;
97131    p->endpoint.pChunk = 0;
97132    p->endpoint.iOffset = 0;
97133    p->readpoint.pChunk = 0;
97134    p->readpoint.iOffset = 0;
97135  }
97136  return SQLITE_OK;
97137}
97138
97139/*
97140** Close the file.
97141*/
97142static int memjrnlClose(sqlite3_file *pJfd){
97143  MemJournal *p = (MemJournal *)pJfd;
97144  memjrnlFreeChunks(p);
97145  return SQLITE_OK;
97146}
97147
97148/*
97149** Sync the file.
97150**
97151** If the real file has been created, call its xSync method. Otherwise,
97152** syncing an in-memory journal is a no-op.
97153*/
97154static int memjrnlSync(sqlite3_file *pJfd, int flags){
97155  UNUSED_PARAMETER2(pJfd, flags);
97156  return SQLITE_OK;
97157}
97158
97159/*
97160** Query the size of the file in bytes.
97161*/
97162static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
97163  MemJournal *p = (MemJournal *)pJfd;
97164  *pSize = (sqlite_int64) p->endpoint.iOffset;
97165  return SQLITE_OK;
97166}
97167
97168/*
97169** Table of methods for MemJournal sqlite3_file object.
97170*/
97171static const struct sqlite3_io_methods MemJournalMethods = {
97172  1,                /* iVersion */
97173  memjrnlClose,     /* xClose */
97174  memjrnlRead,      /* xRead */
97175  memjrnlWrite,     /* xWrite */
97176  memjrnlTruncate,  /* xTruncate */
97177  memjrnlSync,      /* xSync */
97178  memjrnlFileSize,  /* xFileSize */
97179  0,                /* xLock */
97180  0,                /* xUnlock */
97181  0,                /* xCheckReservedLock */
97182  0,                /* xFileControl */
97183  0,                /* xSectorSize */
97184  0,                /* xDeviceCharacteristics */
97185  0,                /* xShmMap */
97186  0,                /* xShmLock */
97187  0,                /* xShmBarrier */
97188  0,                /* xShmUnmap */
97189  0,                /* xFetch */
97190  0                 /* xUnfetch */
97191};
97192
97193/*
97194** Open a journal file.
97195**
97196** The behaviour of the journal file depends on the value of parameter
97197** nSpill. If nSpill is 0, then the journal file is always create and
97198** accessed using the underlying VFS. If nSpill is less than zero, then
97199** all content is always stored in main-memory. Finally, if nSpill is a
97200** positive value, then the journal file is initially created in-memory
97201** but may be flushed to disk later on. In this case the journal file is
97202** flushed to disk either when it grows larger than nSpill bytes in size,
97203** or when sqlite3JournalCreate() is called.
97204*/
97205SQLITE_PRIVATE int sqlite3JournalOpen(
97206  sqlite3_vfs *pVfs,         /* The VFS to use for actual file I/O */
97207  const char *zName,         /* Name of the journal file */
97208  sqlite3_file *pJfd,        /* Preallocated, blank file handle */
97209  int flags,                 /* Opening flags */
97210  int nSpill                 /* Bytes buffered before opening the file */
97211){
97212  MemJournal *p = (MemJournal*)pJfd;
97213
97214  /* Zero the file-handle object. If nSpill was passed zero, initialize
97215  ** it using the sqlite3OsOpen() function of the underlying VFS. In this
97216  ** case none of the code in this module is executed as a result of calls
97217  ** made on the journal file-handle.  */
97218  memset(p, 0, sizeof(MemJournal));
97219  if( nSpill==0 ){
97220    return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
97221  }
97222
97223  if( nSpill>0 ){
97224    p->nChunkSize = nSpill;
97225  }else{
97226    p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
97227    assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
97228  }
97229
97230  p->pMethod = (const sqlite3_io_methods*)&MemJournalMethods;
97231  p->nSpill = nSpill;
97232  p->flags = flags;
97233  p->zJournal = zName;
97234  p->pVfs = pVfs;
97235  return SQLITE_OK;
97236}
97237
97238/*
97239** Open an in-memory journal file.
97240*/
97241SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
97242  sqlite3JournalOpen(0, 0, pJfd, 0, -1);
97243}
97244
97245#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
97246 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
97247/*
97248** If the argument p points to a MemJournal structure that is not an
97249** in-memory-only journal file (i.e. is one that was opened with a +ve
97250** nSpill parameter or as SQLITE_OPEN_MAIN_JOURNAL), and the underlying
97251** file has not yet been created, create it now.
97252*/
97253SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *pJfd){
97254  int rc = SQLITE_OK;
97255  MemJournal *p = (MemJournal*)pJfd;
97256  if( p->pMethod==&MemJournalMethods && (
97257#ifdef SQLITE_ENABLE_ATOMIC_WRITE
97258     p->nSpill>0
97259#else
97260     /* While this appears to not be possible without ATOMIC_WRITE, the
97261     ** paths are complex, so it seems prudent to leave the test in as
97262     ** a NEVER(), in case our analysis is subtly flawed. */
97263     NEVER(p->nSpill>0)
97264#endif
97265#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
97266     || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
97267#endif
97268  )){
97269    rc = memjrnlCreateFile(p);
97270  }
97271  return rc;
97272}
97273#endif
97274
97275/*
97276** The file-handle passed as the only argument is open on a journal file.
97277** Return true if this "journal file" is currently stored in heap memory,
97278** or false otherwise.
97279*/
97280SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p){
97281  return p->pMethods==&MemJournalMethods;
97282}
97283
97284/*
97285** Return the number of bytes required to store a JournalFile that uses vfs
97286** pVfs to create the underlying on-disk files.
97287*/
97288SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
97289  return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
97290}
97291
97292/************** End of memjournal.c ******************************************/
97293/************** Begin file walker.c ******************************************/
97294/*
97295** 2008 August 16
97296**
97297** The author disclaims copyright to this source code.  In place of
97298** a legal notice, here is a blessing:
97299**
97300**    May you do good and not evil.
97301**    May you find forgiveness for yourself and forgive others.
97302**    May you share freely, never taking more than you give.
97303**
97304*************************************************************************
97305** This file contains routines used for walking the parser tree for
97306** an SQL statement.
97307*/
97308/* #include "sqliteInt.h" */
97309/* #include <stdlib.h> */
97310/* #include <string.h> */
97311
97312
97313#if !defined(SQLITE_OMIT_WINDOWFUNC)
97314/*
97315** Walk all expressions linked into the list of Window objects passed
97316** as the second argument.
97317*/
97318static int walkWindowList(Walker *pWalker, Window *pList){
97319  Window *pWin;
97320  for(pWin=pList; pWin; pWin=pWin->pNextWin){
97321    int rc;
97322    rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
97323    if( rc ) return WRC_Abort;
97324    rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
97325    if( rc ) return WRC_Abort;
97326    rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
97327    if( rc ) return WRC_Abort;
97328
97329    /* The next two are purely for calls to sqlite3RenameExprUnmap()
97330    ** within sqlite3WindowOffsetExpr().  Because of constraints imposed
97331    ** by sqlite3WindowOffsetExpr(), they can never fail.  The results do
97332    ** not matter anyhow. */
97333    rc = sqlite3WalkExpr(pWalker, pWin->pStart);
97334    if( NEVER(rc) ) return WRC_Abort;
97335    rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
97336    if( NEVER(rc) ) return WRC_Abort;
97337  }
97338  return WRC_Continue;
97339}
97340#endif
97341
97342/*
97343** Walk an expression tree.  Invoke the callback once for each node
97344** of the expression, while descending.  (In other words, the callback
97345** is invoked before visiting children.)
97346**
97347** The return value from the callback should be one of the WRC_*
97348** constants to specify how to proceed with the walk.
97349**
97350**    WRC_Continue      Continue descending down the tree.
97351**
97352**    WRC_Prune         Do not descend into child nodes, but allow
97353**                      the walk to continue with sibling nodes.
97354**
97355**    WRC_Abort         Do no more callbacks.  Unwind the stack and
97356**                      return from the top-level walk call.
97357**
97358** The return value from this routine is WRC_Abort to abandon the tree walk
97359** and WRC_Continue to continue.
97360*/
97361static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
97362  int rc;
97363  testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
97364  testcase( ExprHasProperty(pExpr, EP_Reduced) );
97365  while(1){
97366    rc = pWalker->xExprCallback(pWalker, pExpr);
97367    if( rc ) return rc & WRC_Abort;
97368    if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
97369      assert( pExpr->x.pList==0 || pExpr->pRight==0 );
97370      if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
97371      if( pExpr->pRight ){
97372        assert( !ExprHasProperty(pExpr, EP_WinFunc) );
97373        pExpr = pExpr->pRight;
97374        continue;
97375      }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
97376        assert( !ExprHasProperty(pExpr, EP_WinFunc) );
97377        if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
97378      }else{
97379        if( pExpr->x.pList ){
97380          if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
97381        }
97382#ifndef SQLITE_OMIT_WINDOWFUNC
97383        if( ExprHasProperty(pExpr, EP_WinFunc) ){
97384          if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort;
97385        }
97386#endif
97387      }
97388    }
97389    break;
97390  }
97391  return WRC_Continue;
97392}
97393SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
97394  return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue;
97395}
97396
97397/*
97398** Call sqlite3WalkExpr() for every expression in list p or until
97399** an abort request is seen.
97400*/
97401SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
97402  int i;
97403  struct ExprList_item *pItem;
97404  if( p ){
97405    for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
97406      if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
97407    }
97408  }
97409  return WRC_Continue;
97410}
97411
97412/*
97413** Walk all expressions associated with SELECT statement p.  Do
97414** not invoke the SELECT callback on p, but do (of course) invoke
97415** any expr callbacks and SELECT callbacks that come from subqueries.
97416** Return WRC_Abort or WRC_Continue.
97417*/
97418SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
97419  if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
97420  if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
97421  if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
97422  if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
97423  if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
97424  if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
97425#if !defined(SQLITE_OMIT_WINDOWFUNC) && !defined(SQLITE_OMIT_ALTERTABLE)
97426  {
97427    Parse *pParse = pWalker->pParse;
97428    if( pParse && IN_RENAME_OBJECT ){
97429      /* The following may return WRC_Abort if there are unresolvable
97430      ** symbols (e.g. a table that does not exist) in a window definition. */
97431      int rc = walkWindowList(pWalker, p->pWinDefn);
97432      return rc;
97433    }
97434  }
97435#endif
97436  return WRC_Continue;
97437}
97438
97439/*
97440** Walk the parse trees associated with all subqueries in the
97441** FROM clause of SELECT statement p.  Do not invoke the select
97442** callback on p, but do invoke it on each FROM clause subquery
97443** and on any subqueries further down in the tree.  Return
97444** WRC_Abort or WRC_Continue;
97445*/
97446SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
97447  SrcList *pSrc;
97448  int i;
97449  struct SrcList_item *pItem;
97450
97451  pSrc = p->pSrc;
97452  if( pSrc ){
97453    for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
97454      if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
97455        return WRC_Abort;
97456      }
97457      if( pItem->fg.isTabFunc
97458       && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
97459      ){
97460        return WRC_Abort;
97461      }
97462    }
97463  }
97464  return WRC_Continue;
97465}
97466
97467/*
97468** Call sqlite3WalkExpr() for every expression in Select statement p.
97469** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
97470** on the compound select chain, p->pPrior.
97471**
97472** If it is not NULL, the xSelectCallback() callback is invoked before
97473** the walk of the expressions and FROM clause. The xSelectCallback2()
97474** method is invoked following the walk of the expressions and FROM clause,
97475** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
97476** and if the expressions and FROM clause both return WRC_Continue;
97477**
97478** Return WRC_Continue under normal conditions.  Return WRC_Abort if
97479** there is an abort request.
97480**
97481** If the Walker does not have an xSelectCallback() then this routine
97482** is a no-op returning WRC_Continue.
97483*/
97484SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
97485  int rc;
97486  if( p==0 ) return WRC_Continue;
97487  if( pWalker->xSelectCallback==0 ) return WRC_Continue;
97488  do{
97489    rc = pWalker->xSelectCallback(pWalker, p);
97490    if( rc ) return rc & WRC_Abort;
97491    if( sqlite3WalkSelectExpr(pWalker, p)
97492     || sqlite3WalkSelectFrom(pWalker, p)
97493    ){
97494      return WRC_Abort;
97495    }
97496    if( pWalker->xSelectCallback2 ){
97497      pWalker->xSelectCallback2(pWalker, p);
97498    }
97499    p = p->pPrior;
97500  }while( p!=0 );
97501  return WRC_Continue;
97502}
97503
97504/* Increase the walkerDepth when entering a subquery, and
97505** descrease when leaving the subquery.
97506*/
97507SQLITE_PRIVATE int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){
97508  UNUSED_PARAMETER(pSelect);
97509  pWalker->walkerDepth++;
97510  return WRC_Continue;
97511}
97512SQLITE_PRIVATE void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect){
97513  UNUSED_PARAMETER(pSelect);
97514  pWalker->walkerDepth--;
97515}
97516
97517
97518/*
97519** No-op routine for the parse-tree walker.
97520**
97521** When this routine is the Walker.xExprCallback then expression trees
97522** are walked without any actions being taken at each node.  Presumably,
97523** when this routine is used for Walker.xExprCallback then
97524** Walker.xSelectCallback is set to do something useful for every
97525** subquery in the parser tree.
97526*/
97527SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
97528  UNUSED_PARAMETER2(NotUsed, NotUsed2);
97529  return WRC_Continue;
97530}
97531
97532/*
97533** No-op routine for the parse-tree walker for SELECT statements.
97534** subquery in the parser tree.
97535*/
97536SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){
97537  UNUSED_PARAMETER2(NotUsed, NotUsed2);
97538  return WRC_Continue;
97539}
97540
97541/************** End of walker.c **********************************************/
97542/************** Begin file resolve.c *****************************************/
97543/*
97544** 2008 August 18
97545**
97546** The author disclaims copyright to this source code.  In place of
97547** a legal notice, here is a blessing:
97548**
97549**    May you do good and not evil.
97550**    May you find forgiveness for yourself and forgive others.
97551**    May you share freely, never taking more than you give.
97552**
97553*************************************************************************
97554**
97555** This file contains routines used for walking the parser tree and
97556** resolve all identifiers by associating them with a particular
97557** table and column.
97558*/
97559/* #include "sqliteInt.h" */
97560
97561/*
97562** Walk the expression tree pExpr and increase the aggregate function
97563** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
97564** This needs to occur when copying a TK_AGG_FUNCTION node from an
97565** outer query into an inner subquery.
97566**
97567** incrAggFunctionDepth(pExpr,n) is the main routine.  incrAggDepth(..)
97568** is a helper function - a callback for the tree walker.
97569**
97570** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c
97571*/
97572static int incrAggDepth(Walker *pWalker, Expr *pExpr){
97573  if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
97574  return WRC_Continue;
97575}
97576static void incrAggFunctionDepth(Expr *pExpr, int N){
97577  if( N>0 ){
97578    Walker w;
97579    memset(&w, 0, sizeof(w));
97580    w.xExprCallback = incrAggDepth;
97581    w.u.n = N;
97582    sqlite3WalkExpr(&w, pExpr);
97583  }
97584}
97585
97586/*
97587** Turn the pExpr expression into an alias for the iCol-th column of the
97588** result set in pEList.
97589**
97590** If the reference is followed by a COLLATE operator, then make sure
97591** the COLLATE operator is preserved.  For example:
97592**
97593**     SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
97594**
97595** Should be transformed into:
97596**
97597**     SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
97598**
97599** The nSubquery parameter specifies how many levels of subquery the
97600** alias is removed from the original expression.  The usual value is
97601** zero but it might be more if the alias is contained within a subquery
97602** of the original expression.  The Expr.op2 field of TK_AGG_FUNCTION
97603** structures must be increased by the nSubquery amount.
97604*/
97605static void resolveAlias(
97606  Parse *pParse,         /* Parsing context */
97607  ExprList *pEList,      /* A result set */
97608  int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
97609  Expr *pExpr,           /* Transform this into an alias to the result set */
97610  const char *zType,     /* "GROUP" or "ORDER" or "" */
97611  int nSubquery          /* Number of subqueries that the label is moving */
97612){
97613  Expr *pOrig;           /* The iCol-th column of the result set */
97614  Expr *pDup;            /* Copy of pOrig */
97615  sqlite3 *db;           /* The database connection */
97616
97617  assert( iCol>=0 && iCol<pEList->nExpr );
97618  pOrig = pEList->a[iCol].pExpr;
97619  assert( pOrig!=0 );
97620  db = pParse->db;
97621  pDup = sqlite3ExprDup(db, pOrig, 0);
97622  if( pDup!=0 ){
97623    if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery);
97624    if( pExpr->op==TK_COLLATE ){
97625      pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
97626    }
97627
97628    /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
97629    ** prevents ExprDelete() from deleting the Expr structure itself,
97630    ** allowing it to be repopulated by the memcpy() on the following line.
97631    ** The pExpr->u.zToken might point into memory that will be freed by the
97632    ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
97633    ** make a copy of the token before doing the sqlite3DbFree().
97634    */
97635    ExprSetProperty(pExpr, EP_Static);
97636    sqlite3ExprDelete(db, pExpr);
97637    memcpy(pExpr, pDup, sizeof(*pExpr));
97638    if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
97639      assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
97640      pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
97641      pExpr->flags |= EP_MemToken;
97642    }
97643    if( ExprHasProperty(pExpr, EP_WinFunc) ){
97644      if( pExpr->y.pWin!=0 ){
97645        pExpr->y.pWin->pOwner = pExpr;
97646      }else{
97647        assert( db->mallocFailed );
97648      }
97649    }
97650    sqlite3DbFree(db, pDup);
97651  }
97652  ExprSetProperty(pExpr, EP_Alias);
97653}
97654
97655
97656/*
97657** Return TRUE if the name zCol occurs anywhere in the USING clause.
97658**
97659** Return FALSE if the USING clause is NULL or if it does not contain
97660** zCol.
97661*/
97662static int nameInUsingClause(IdList *pUsing, const char *zCol){
97663  if( pUsing ){
97664    int k;
97665    for(k=0; k<pUsing->nId; k++){
97666      if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
97667    }
97668  }
97669  return 0;
97670}
97671
97672/*
97673** Subqueries stores the original database, table and column names for their
97674** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
97675** Check to see if the zSpan given to this routine matches the zDb, zTab,
97676** and zCol.  If any of zDb, zTab, and zCol are NULL then those fields will
97677** match anything.
97678*/
97679SQLITE_PRIVATE int sqlite3MatchEName(
97680  const struct ExprList_item *pItem,
97681  const char *zCol,
97682  const char *zTab,
97683  const char *zDb
97684){
97685  int n;
97686  const char *zSpan;
97687  if( pItem->eEName!=ENAME_TAB ) return 0;
97688  zSpan = pItem->zEName;
97689  for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
97690  if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
97691    return 0;
97692  }
97693  zSpan += n+1;
97694  for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
97695  if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
97696    return 0;
97697  }
97698  zSpan += n+1;
97699  if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
97700    return 0;
97701  }
97702  return 1;
97703}
97704
97705/*
97706** Return TRUE if the double-quoted string  mis-feature should be supported.
97707*/
97708static int areDoubleQuotedStringsEnabled(sqlite3 *db, NameContext *pTopNC){
97709  if( db->init.busy ) return 1;  /* Always support for legacy schemas */
97710  if( pTopNC->ncFlags & NC_IsDDL ){
97711    /* Currently parsing a DDL statement */
97712    if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){
97713      return 1;
97714    }
97715    return (db->flags & SQLITE_DqsDDL)!=0;
97716  }else{
97717    /* Currently parsing a DML statement */
97718    return (db->flags & SQLITE_DqsDML)!=0;
97719  }
97720}
97721
97722/*
97723** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
97724** return the appropriate colUsed mask.
97725*/
97726SQLITE_PRIVATE Bitmask sqlite3ExprColUsed(Expr *pExpr){
97727  int n;
97728  Table *pExTab;
97729
97730  n = pExpr->iColumn;
97731  pExTab = pExpr->y.pTab;
97732  assert( pExTab!=0 );
97733  if( (pExTab->tabFlags & TF_HasGenerated)!=0
97734   && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
97735  ){
97736    testcase( pExTab->nCol==BMS-1 );
97737    testcase( pExTab->nCol==BMS );
97738    return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
97739  }else{
97740    testcase( n==BMS-1 );
97741    testcase( n==BMS );
97742    if( n>=BMS ) n = BMS-1;
97743    return ((Bitmask)1)<<n;
97744  }
97745}
97746
97747/*
97748** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
97749** that name in the set of source tables in pSrcList and make the pExpr
97750** expression node refer back to that source column.  The following changes
97751** are made to pExpr:
97752**
97753**    pExpr->iDb           Set the index in db->aDb[] of the database X
97754**                         (even if X is implied).
97755**    pExpr->iTable        Set to the cursor number for the table obtained
97756**                         from pSrcList.
97757**    pExpr->y.pTab        Points to the Table structure of X.Y (even if
97758**                         X and/or Y are implied.)
97759**    pExpr->iColumn       Set to the column number within the table.
97760**    pExpr->op            Set to TK_COLUMN.
97761**    pExpr->pLeft         Any expression this points to is deleted
97762**    pExpr->pRight        Any expression this points to is deleted.
97763**
97764** The zDb variable is the name of the database (the "X").  This value may be
97765** NULL meaning that name is of the form Y.Z or Z.  Any available database
97766** can be used.  The zTable variable is the name of the table (the "Y").  This
97767** value can be NULL if zDb is also NULL.  If zTable is NULL it
97768** means that the form of the name is Z and that columns from any table
97769** can be used.
97770**
97771** If the name cannot be resolved unambiguously, leave an error message
97772** in pParse and return WRC_Abort.  Return WRC_Prune on success.
97773*/
97774static int lookupName(
97775  Parse *pParse,       /* The parsing context */
97776  const char *zDb,     /* Name of the database containing table, or NULL */
97777  const char *zTab,    /* Name of table containing column, or NULL */
97778  const char *zCol,    /* Name of the column. */
97779  NameContext *pNC,    /* The name context used to resolve the name */
97780  Expr *pExpr          /* Make this EXPR node point to the selected column */
97781){
97782  int i, j;                         /* Loop counters */
97783  int cnt = 0;                      /* Number of matching column names */
97784  int cntTab = 0;                   /* Number of matching table names */
97785  int nSubquery = 0;                /* How many levels of subquery */
97786  sqlite3 *db = pParse->db;         /* The database connection */
97787  struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
97788  struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
97789  NameContext *pTopNC = pNC;        /* First namecontext in the list */
97790  Schema *pSchema = 0;              /* Schema of the expression */
97791  int eNewExprOp = TK_COLUMN;       /* New value for pExpr->op on success */
97792  Table *pTab = 0;                  /* Table hold the row */
97793  Column *pCol;                     /* A column of pTab */
97794
97795  assert( pNC );     /* the name context cannot be NULL. */
97796  assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
97797  assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
97798
97799  /* Initialize the node to no-match */
97800  pExpr->iTable = -1;
97801  ExprSetVVAProperty(pExpr, EP_NoReduce);
97802
97803  /* Translate the schema name in zDb into a pointer to the corresponding
97804  ** schema.  If not found, pSchema will remain NULL and nothing will match
97805  ** resulting in an appropriate error message toward the end of this routine
97806  */
97807  if( zDb ){
97808    testcase( pNC->ncFlags & NC_PartIdx );
97809    testcase( pNC->ncFlags & NC_IsCheck );
97810    if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
97811      /* Silently ignore database qualifiers inside CHECK constraints and
97812      ** partial indices.  Do not raise errors because that might break
97813      ** legacy and because it does not hurt anything to just ignore the
97814      ** database name. */
97815      zDb = 0;
97816    }else{
97817      for(i=0; i<db->nDb; i++){
97818        assert( db->aDb[i].zDbSName );
97819        if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
97820          pSchema = db->aDb[i].pSchema;
97821          break;
97822        }
97823      }
97824      if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
97825        /* This branch is taken when the main database has been renamed
97826        ** using SQLITE_DBCONFIG_MAINDBNAME. */
97827        pSchema = db->aDb[0].pSchema;
97828        zDb = db->aDb[0].zDbSName;
97829      }
97830    }
97831  }
97832
97833  /* Start at the inner-most context and move outward until a match is found */
97834  assert( pNC && cnt==0 );
97835  do{
97836    ExprList *pEList;
97837    SrcList *pSrcList = pNC->pSrcList;
97838
97839    if( pSrcList ){
97840      for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
97841        u8 hCol;
97842        pTab = pItem->pTab;
97843        assert( pTab!=0 && pTab->zName!=0 );
97844        assert( pTab->nCol>0 );
97845        if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
97846          int hit = 0;
97847          pEList = pItem->pSelect->pEList;
97848          for(j=0; j<pEList->nExpr; j++){
97849            if( sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb) ){
97850              cnt++;
97851              cntTab = 2;
97852              pMatch = pItem;
97853              pExpr->iColumn = j;
97854              hit = 1;
97855            }
97856          }
97857          if( hit || zTab==0 ) continue;
97858        }
97859        if( zDb && pTab->pSchema!=pSchema ){
97860          continue;
97861        }
97862        if( zTab ){
97863          const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
97864          assert( zTabName!=0 );
97865          if( sqlite3StrICmp(zTabName, zTab)!=0 ){
97866            continue;
97867          }
97868          if( IN_RENAME_OBJECT && pItem->zAlias ){
97869            sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
97870          }
97871        }
97872        if( 0==(cntTab++) ){
97873          pMatch = pItem;
97874        }
97875        hCol = sqlite3StrIHash(zCol);
97876        for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
97877          if( pCol->hName==hCol && sqlite3StrICmp(pCol->zName, zCol)==0 ){
97878            /* If there has been exactly one prior match and this match
97879            ** is for the right-hand table of a NATURAL JOIN or is in a
97880            ** USING clause, then skip this match.
97881            */
97882            if( cnt==1 ){
97883              if( pItem->fg.jointype & JT_NATURAL ) continue;
97884              if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
97885            }
97886            cnt++;
97887            pMatch = pItem;
97888            /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
97889            pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
97890            break;
97891          }
97892        }
97893      }
97894      if( pMatch ){
97895        pExpr->iTable = pMatch->iCursor;
97896        pExpr->y.pTab = pMatch->pTab;
97897        /* RIGHT JOIN not (yet) supported */
97898        assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
97899        if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
97900          ExprSetProperty(pExpr, EP_CanBeNull);
97901        }
97902        pSchema = pExpr->y.pTab->pSchema;
97903      }
97904    } /* if( pSrcList ) */
97905
97906#if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
97907    /* If we have not already resolved the name, then maybe
97908    ** it is a new.* or old.* trigger argument reference.  Or
97909    ** maybe it is an excluded.* from an upsert.
97910    */
97911    if( zDb==0 && zTab!=0 && cntTab==0 ){
97912      pTab = 0;
97913#ifndef SQLITE_OMIT_TRIGGER
97914      if( pParse->pTriggerTab!=0 ){
97915        int op = pParse->eTriggerOp;
97916        assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
97917        if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
97918          pExpr->iTable = 1;
97919          pTab = pParse->pTriggerTab;
97920        }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
97921          pExpr->iTable = 0;
97922          pTab = pParse->pTriggerTab;
97923        }
97924      }
97925#endif /* SQLITE_OMIT_TRIGGER */
97926#ifndef SQLITE_OMIT_UPSERT
97927      if( (pNC->ncFlags & NC_UUpsert)!=0 ){
97928        Upsert *pUpsert = pNC->uNC.pUpsert;
97929        if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){
97930          pTab = pUpsert->pUpsertSrc->a[0].pTab;
97931          pExpr->iTable = 2;
97932        }
97933      }
97934#endif /* SQLITE_OMIT_UPSERT */
97935
97936      if( pTab ){
97937        int iCol;
97938        u8 hCol = sqlite3StrIHash(zCol);
97939        pSchema = pTab->pSchema;
97940        cntTab++;
97941        for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
97942          if( pCol->hName==hCol && sqlite3StrICmp(pCol->zName, zCol)==0 ){
97943            if( iCol==pTab->iPKey ){
97944              iCol = -1;
97945            }
97946            break;
97947          }
97948        }
97949        if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
97950          /* IMP: R-51414-32910 */
97951          iCol = -1;
97952        }
97953        if( iCol<pTab->nCol ){
97954          cnt++;
97955#ifndef SQLITE_OMIT_UPSERT
97956          if( pExpr->iTable==2 ){
97957            testcase( iCol==(-1) );
97958            if( IN_RENAME_OBJECT ){
97959              pExpr->iColumn = iCol;
97960              pExpr->y.pTab = pTab;
97961              eNewExprOp = TK_COLUMN;
97962            }else{
97963              pExpr->iTable = pNC->uNC.pUpsert->regData + iCol;
97964              eNewExprOp = TK_REGISTER;
97965              ExprSetProperty(pExpr, EP_Alias);
97966            }
97967          }else
97968#endif /* SQLITE_OMIT_UPSERT */
97969          {
97970#ifndef SQLITE_OMIT_TRIGGER
97971            if( iCol<0 ){
97972              pExpr->affExpr = SQLITE_AFF_INTEGER;
97973            }else if( pExpr->iTable==0 ){
97974              testcase( iCol==31 );
97975              testcase( iCol==32 );
97976              pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
97977            }else{
97978              testcase( iCol==31 );
97979              testcase( iCol==32 );
97980              pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
97981            }
97982            pExpr->y.pTab = pTab;
97983            pExpr->iColumn = (i16)iCol;
97984            eNewExprOp = TK_TRIGGER;
97985#endif /* SQLITE_OMIT_TRIGGER */
97986          }
97987        }
97988      }
97989    }
97990#endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */
97991
97992    /*
97993    ** Perhaps the name is a reference to the ROWID
97994    */
97995    if( cnt==0
97996     && cntTab==1
97997     && pMatch
97998     && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0
97999     && sqlite3IsRowid(zCol)
98000     && VisibleRowid(pMatch->pTab)
98001    ){
98002      cnt = 1;
98003      pExpr->iColumn = -1;
98004      pExpr->affExpr = SQLITE_AFF_INTEGER;
98005    }
98006
98007    /*
98008    ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
98009    ** might refer to an result-set alias.  This happens, for example, when
98010    ** we are resolving names in the WHERE clause of the following command:
98011    **
98012    **     SELECT a+b AS x FROM table WHERE x<10;
98013    **
98014    ** In cases like this, replace pExpr with a copy of the expression that
98015    ** forms the result set entry ("a+b" in the example) and return immediately.
98016    ** Note that the expression in the result set should have already been
98017    ** resolved by the time the WHERE clause is resolved.
98018    **
98019    ** The ability to use an output result-set column in the WHERE, GROUP BY,
98020    ** or HAVING clauses, or as part of a larger expression in the ORDER BY
98021    ** clause is not standard SQL.  This is a (goofy) SQLite extension, that
98022    ** is supported for backwards compatibility only. Hence, we issue a warning
98023    ** on sqlite3_log() whenever the capability is used.
98024    */
98025    if( (pNC->ncFlags & NC_UEList)!=0
98026     && cnt==0
98027     && zTab==0
98028    ){
98029      pEList = pNC->uNC.pEList;
98030      assert( pEList!=0 );
98031      for(j=0; j<pEList->nExpr; j++){
98032        char *zAs = pEList->a[j].zEName;
98033        if( pEList->a[j].eEName==ENAME_NAME
98034         && sqlite3_stricmp(zAs, zCol)==0
98035        ){
98036          Expr *pOrig;
98037          assert( pExpr->pLeft==0 && pExpr->pRight==0 );
98038          assert( pExpr->x.pList==0 );
98039          assert( pExpr->x.pSelect==0 );
98040          pOrig = pEList->a[j].pExpr;
98041          if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
98042            sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
98043            return WRC_Abort;
98044          }
98045          if( ExprHasProperty(pOrig, EP_Win)
98046           && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC )
98047          ){
98048            sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs);
98049            return WRC_Abort;
98050          }
98051          if( sqlite3ExprVectorSize(pOrig)!=1 ){
98052            sqlite3ErrorMsg(pParse, "row value misused");
98053            return WRC_Abort;
98054          }
98055          resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
98056          cnt = 1;
98057          pMatch = 0;
98058          assert( zTab==0 && zDb==0 );
98059          if( IN_RENAME_OBJECT ){
98060            sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
98061          }
98062          goto lookupname_end;
98063        }
98064      }
98065    }
98066
98067    /* Advance to the next name context.  The loop will exit when either
98068    ** we have a match (cnt>0) or when we run out of name contexts.
98069    */
98070    if( cnt ) break;
98071    pNC = pNC->pNext;
98072    nSubquery++;
98073  }while( pNC );
98074
98075
98076  /*
98077  ** If X and Y are NULL (in other words if only the column name Z is
98078  ** supplied) and the value of Z is enclosed in double-quotes, then
98079  ** Z is a string literal if it doesn't match any column names.  In that
98080  ** case, we need to return right away and not make any changes to
98081  ** pExpr.
98082  **
98083  ** Because no reference was made to outer contexts, the pNC->nRef
98084  ** fields are not changed in any context.
98085  */
98086  if( cnt==0 && zTab==0 ){
98087    assert( pExpr->op==TK_ID );
98088    if( ExprHasProperty(pExpr,EP_DblQuoted)
98089     && areDoubleQuotedStringsEnabled(db, pTopNC)
98090    ){
98091      /* If a double-quoted identifier does not match any known column name,
98092      ** then treat it as a string.
98093      **
98094      ** This hack was added in the early days of SQLite in a misguided attempt
98095      ** to be compatible with MySQL 3.x, which used double-quotes for strings.
98096      ** I now sorely regret putting in this hack. The effect of this hack is
98097      ** that misspelled identifier names are silently converted into strings
98098      ** rather than causing an error, to the frustration of countless
98099      ** programmers. To all those frustrated programmers, my apologies.
98100      **
98101      ** Someday, I hope to get rid of this hack. Unfortunately there is
98102      ** a huge amount of legacy SQL that uses it. So for now, we just
98103      ** issue a warning.
98104      */
98105      sqlite3_log(SQLITE_WARNING,
98106        "double-quoted string literal: \"%w\"", zCol);
98107#ifdef SQLITE_ENABLE_NORMALIZE
98108      sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
98109#endif
98110      pExpr->op = TK_STRING;
98111      pExpr->y.pTab = 0;
98112      return WRC_Prune;
98113    }
98114    if( sqlite3ExprIdToTrueFalse(pExpr) ){
98115      return WRC_Prune;
98116    }
98117  }
98118
98119  /*
98120  ** cnt==0 means there was not match.  cnt>1 means there were two or
98121  ** more matches.  Either way, we have an error.
98122  */
98123  if( cnt!=1 ){
98124    const char *zErr;
98125    zErr = cnt==0 ? "no such column" : "ambiguous column name";
98126    if( zDb ){
98127      sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
98128    }else if( zTab ){
98129      sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
98130    }else{
98131      sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
98132    }
98133    pParse->checkSchema = 1;
98134    pTopNC->nErr++;
98135  }
98136
98137  /* If a column from a table in pSrcList is referenced, then record
98138  ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
98139  ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  Bit 63 is
98140  ** set if the 63rd or any subsequent column is used.
98141  **
98142  ** The colUsed mask is an optimization used to help determine if an
98143  ** index is a covering index.  The correct answer is still obtained
98144  ** if the mask contains extra set bits.  However, it is important to
98145  ** avoid setting bits beyond the maximum column number of the table.
98146  ** (See ticket [b92e5e8ec2cdbaa1]).
98147  **
98148  ** If a generated column is referenced, set bits for every column
98149  ** of the table.
98150  */
98151  if( pExpr->iColumn>=0 && pMatch!=0 ){
98152    pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
98153  }
98154
98155  /* Clean up and return
98156  */
98157  sqlite3ExprDelete(db, pExpr->pLeft);
98158  pExpr->pLeft = 0;
98159  sqlite3ExprDelete(db, pExpr->pRight);
98160  pExpr->pRight = 0;
98161  pExpr->op = eNewExprOp;
98162  ExprSetProperty(pExpr, EP_Leaf);
98163lookupname_end:
98164  if( cnt==1 ){
98165    assert( pNC!=0 );
98166    if( !ExprHasProperty(pExpr, EP_Alias) ){
98167      sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
98168    }
98169    /* Increment the nRef value on all name contexts from TopNC up to
98170    ** the point where the name matched. */
98171    for(;;){
98172      assert( pTopNC!=0 );
98173      pTopNC->nRef++;
98174      if( pTopNC==pNC ) break;
98175      pTopNC = pTopNC->pNext;
98176    }
98177    return WRC_Prune;
98178  } else {
98179    return WRC_Abort;
98180  }
98181}
98182
98183/*
98184** Allocate and return a pointer to an expression to load the column iCol
98185** from datasource iSrc in SrcList pSrc.
98186*/
98187SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
98188  Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
98189  if( p ){
98190    struct SrcList_item *pItem = &pSrc->a[iSrc];
98191    Table *pTab = p->y.pTab = pItem->pTab;
98192    p->iTable = pItem->iCursor;
98193    if( p->y.pTab->iPKey==iCol ){
98194      p->iColumn = -1;
98195    }else{
98196      p->iColumn = (ynVar)iCol;
98197      if( (pTab->tabFlags & TF_HasGenerated)!=0
98198       && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0
98199      ){
98200        testcase( pTab->nCol==63 );
98201        testcase( pTab->nCol==64 );
98202        pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1;
98203      }else{
98204        testcase( iCol==BMS );
98205        testcase( iCol==BMS-1 );
98206        pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
98207      }
98208    }
98209  }
98210  return p;
98211}
98212
98213/*
98214** Report an error that an expression is not valid for some set of
98215** pNC->ncFlags values determined by validMask.
98216**
98217** static void notValid(
98218**   Parse *pParse,       // Leave error message here
98219**   NameContext *pNC,    // The name context
98220**   const char *zMsg,    // Type of error
98221**   int validMask,       // Set of contexts for which prohibited
98222**   Expr *pExpr          // Invalidate this expression on error
98223** ){...}
98224**
98225** As an optimization, since the conditional is almost always false
98226** (because errors are rare), the conditional is moved outside of the
98227** function call using a macro.
98228*/
98229static void notValidImpl(
98230   Parse *pParse,       /* Leave error message here */
98231   NameContext *pNC,    /* The name context */
98232   const char *zMsg,    /* Type of error */
98233   Expr *pExpr          /* Invalidate this expression on error */
98234){
98235  const char *zIn = "partial index WHERE clauses";
98236  if( pNC->ncFlags & NC_IdxExpr )      zIn = "index expressions";
98237#ifndef SQLITE_OMIT_CHECK
98238  else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
98239#endif
98240#ifndef SQLITE_OMIT_GENERATED_COLUMNS
98241  else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns";
98242#endif
98243  sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn);
98244  if( pExpr ) pExpr->op = TK_NULL;
98245}
98246#define sqlite3ResolveNotValid(P,N,M,X,E) \
98247  assert( ((X)&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol))==0 ); \
98248  if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E);
98249
98250/*
98251** Expression p should encode a floating point value between 1.0 and 0.0.
98252** Return 1024 times this value.  Or return -1 if p is not a floating point
98253** value between 1.0 and 0.0.
98254*/
98255static int exprProbability(Expr *p){
98256  double r = -1.0;
98257  if( p->op!=TK_FLOAT ) return -1;
98258  sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
98259  assert( r>=0.0 );
98260  if( r>1.0 ) return -1;
98261  return (int)(r*134217728.0);
98262}
98263
98264/*
98265** This routine is callback for sqlite3WalkExpr().
98266**
98267** Resolve symbolic names into TK_COLUMN operators for the current
98268** node in the expression tree.  Return 0 to continue the search down
98269** the tree or 2 to abort the tree walk.
98270**
98271** This routine also does error checking and name resolution for
98272** function names.  The operator for aggregate functions is changed
98273** to TK_AGG_FUNCTION.
98274*/
98275static int resolveExprStep(Walker *pWalker, Expr *pExpr){
98276  NameContext *pNC;
98277  Parse *pParse;
98278
98279  pNC = pWalker->u.pNC;
98280  assert( pNC!=0 );
98281  pParse = pNC->pParse;
98282  assert( pParse==pWalker->pParse );
98283
98284#ifndef NDEBUG
98285  if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
98286    SrcList *pSrcList = pNC->pSrcList;
98287    int i;
98288    for(i=0; i<pNC->pSrcList->nSrc; i++){
98289      assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
98290    }
98291  }
98292#endif
98293  switch( pExpr->op ){
98294
98295#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
98296    /* The special operator TK_ROW means use the rowid for the first
98297    ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
98298    ** clause processing on UPDATE and DELETE statements.
98299    */
98300    case TK_ROW: {
98301      SrcList *pSrcList = pNC->pSrcList;
98302      struct SrcList_item *pItem;
98303      assert( pSrcList && pSrcList->nSrc==1 );
98304      pItem = pSrcList->a;
98305      assert( HasRowid(pItem->pTab) && pItem->pTab->pSelect==0 );
98306      pExpr->op = TK_COLUMN;
98307      pExpr->y.pTab = pItem->pTab;
98308      pExpr->iTable = pItem->iCursor;
98309      pExpr->iColumn = -1;
98310      pExpr->affExpr = SQLITE_AFF_INTEGER;
98311      break;
98312    }
98313#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
98314          && !defined(SQLITE_OMIT_SUBQUERY) */
98315
98316    /* A column name:                    ID
98317    ** Or table name and column name:    ID.ID
98318    ** Or a database, table and column:  ID.ID.ID
98319    **
98320    ** The TK_ID and TK_OUT cases are combined so that there will only
98321    ** be one call to lookupName().  Then the compiler will in-line
98322    ** lookupName() for a size reduction and performance increase.
98323    */
98324    case TK_ID:
98325    case TK_DOT: {
98326      const char *zColumn;
98327      const char *zTable;
98328      const char *zDb;
98329      Expr *pRight;
98330
98331      if( pExpr->op==TK_ID ){
98332        zDb = 0;
98333        zTable = 0;
98334        zColumn = pExpr->u.zToken;
98335      }else{
98336        Expr *pLeft = pExpr->pLeft;
98337        testcase( pNC->ncFlags & NC_IdxExpr );
98338        testcase( pNC->ncFlags & NC_GenCol );
98339        sqlite3ResolveNotValid(pParse, pNC, "the \".\" operator",
98340                               NC_IdxExpr|NC_GenCol, 0);
98341        pRight = pExpr->pRight;
98342        if( pRight->op==TK_ID ){
98343          zDb = 0;
98344        }else{
98345          assert( pRight->op==TK_DOT );
98346          zDb = pLeft->u.zToken;
98347          pLeft = pRight->pLeft;
98348          pRight = pRight->pRight;
98349        }
98350        zTable = pLeft->u.zToken;
98351        zColumn = pRight->u.zToken;
98352        if( IN_RENAME_OBJECT ){
98353          sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);
98354          sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
98355        }
98356      }
98357      return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
98358    }
98359
98360    /* Resolve function names
98361    */
98362    case TK_FUNCTION: {
98363      ExprList *pList = pExpr->x.pList;    /* The argument list */
98364      int n = pList ? pList->nExpr : 0;    /* Number of arguments */
98365      int no_such_func = 0;       /* True if no such function exists */
98366      int wrong_num_args = 0;     /* True if wrong number of arguments */
98367      int is_agg = 0;             /* True if is an aggregate function */
98368      int nId;                    /* Number of characters in function name */
98369      const char *zId;            /* The function name. */
98370      FuncDef *pDef;              /* Information about the function */
98371      u8 enc = ENC(pParse->db);   /* The database encoding */
98372      int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
98373#ifndef SQLITE_OMIT_WINDOWFUNC
98374      Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);
98375#endif
98376      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
98377      zId = pExpr->u.zToken;
98378      nId = sqlite3Strlen30(zId);
98379      pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
98380      if( pDef==0 ){
98381        pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
98382        if( pDef==0 ){
98383          no_such_func = 1;
98384        }else{
98385          wrong_num_args = 1;
98386        }
98387      }else{
98388        is_agg = pDef->xFinalize!=0;
98389        if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
98390          ExprSetProperty(pExpr, EP_Unlikely);
98391          if( n==2 ){
98392            pExpr->iTable = exprProbability(pList->a[1].pExpr);
98393            if( pExpr->iTable<0 ){
98394              sqlite3ErrorMsg(pParse,
98395                "second argument to likelihood() must be a "
98396                "constant between 0.0 and 1.0");
98397              pNC->nErr++;
98398            }
98399          }else{
98400            /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
98401            ** equivalent to likelihood(X, 0.0625).
98402            ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
98403            ** short-hand for likelihood(X,0.0625).
98404            ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
98405            ** for likelihood(X,0.9375).
98406            ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
98407            ** to likelihood(X,0.9375). */
98408            /* TUNING: unlikely() probability is 0.0625.  likely() is 0.9375 */
98409            pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
98410          }
98411        }
98412#ifndef SQLITE_OMIT_AUTHORIZATION
98413        {
98414          int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
98415          if( auth!=SQLITE_OK ){
98416            if( auth==SQLITE_DENY ){
98417              sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
98418                                      pDef->zName);
98419              pNC->nErr++;
98420            }
98421            pExpr->op = TK_NULL;
98422            return WRC_Prune;
98423          }
98424        }
98425#endif
98426        if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
98427          /* For the purposes of the EP_ConstFunc flag, date and time
98428          ** functions and other functions that change slowly are considered
98429          ** constant because they are constant for the duration of one query.
98430          ** This allows them to be factored out of inner loops. */
98431          ExprSetProperty(pExpr,EP_ConstFunc);
98432        }
98433        if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
98434          /* Clearly non-deterministic functions like random(), but also
98435          ** date/time functions that use 'now', and other functions like
98436          ** sqlite_version() that might change over time cannot be used
98437          ** in an index or generated column.  Curiously, they can be used
98438          ** in a CHECK constraint.  SQLServer, MySQL, and PostgreSQL all
98439          ** all this. */
98440          sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",
98441                                 NC_IdxExpr|NC_PartIdx|NC_GenCol, 0);
98442        }else{
98443          assert( (NC_SelfRef & 0xff)==NC_SelfRef ); /* Must fit in 8 bits */
98444          pExpr->op2 = pNC->ncFlags & NC_SelfRef;
98445          if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
98446        }
98447        if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
98448         && pParse->nested==0
98449         && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0
98450        ){
98451          /* Internal-use-only functions are disallowed unless the
98452          ** SQL is being compiled using sqlite3NestedParse() or
98453          ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
98454          ** used to activate internal functionsn for testing purposes */
98455          no_such_func = 1;
98456          pDef = 0;
98457        }else
98458        if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0
98459         && !IN_RENAME_OBJECT
98460        ){
98461          sqlite3ExprFunctionUsable(pParse, pExpr, pDef);
98462        }
98463      }
98464
98465      if( 0==IN_RENAME_OBJECT ){
98466#ifndef SQLITE_OMIT_WINDOWFUNC
98467        assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
98468          || (pDef->xValue==0 && pDef->xInverse==0)
98469          || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
98470        );
98471        if( pDef && pDef->xValue==0 && pWin ){
98472          sqlite3ErrorMsg(pParse,
98473              "%.*s() may not be used as a window function", nId, zId
98474          );
98475          pNC->nErr++;
98476        }else if(
98477              (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
98478           || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
98479           || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
98480        ){
98481          const char *zType;
98482          if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
98483            zType = "window";
98484          }else{
98485            zType = "aggregate";
98486          }
98487          sqlite3ErrorMsg(pParse, "misuse of %s function %.*s()",zType,nId,zId);
98488          pNC->nErr++;
98489          is_agg = 0;
98490        }
98491#else
98492        if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
98493          sqlite3ErrorMsg(pParse,"misuse of aggregate function %.*s()",nId,zId);
98494          pNC->nErr++;
98495          is_agg = 0;
98496        }
98497#endif
98498        else if( no_such_func && pParse->db->init.busy==0
98499#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
98500                  && pParse->explain==0
98501#endif
98502        ){
98503          sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
98504          pNC->nErr++;
98505        }else if( wrong_num_args ){
98506          sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
98507               nId, zId);
98508          pNC->nErr++;
98509        }
98510#ifndef SQLITE_OMIT_WINDOWFUNC
98511        else if( is_agg==0 && ExprHasProperty(pExpr, EP_WinFunc) ){
98512          sqlite3ErrorMsg(pParse,
98513              "FILTER may not be used with non-aggregate %.*s()",
98514              nId, zId
98515          );
98516          pNC->nErr++;
98517        }
98518#endif
98519        if( is_agg ){
98520          /* Window functions may not be arguments of aggregate functions.
98521          ** Or arguments of other window functions. But aggregate functions
98522          ** may be arguments for window functions.  */
98523#ifndef SQLITE_OMIT_WINDOWFUNC
98524          pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
98525#else
98526          pNC->ncFlags &= ~NC_AllowAgg;
98527#endif
98528        }
98529      }
98530#ifndef SQLITE_OMIT_WINDOWFUNC
98531      else if( ExprHasProperty(pExpr, EP_WinFunc) ){
98532        is_agg = 1;
98533      }
98534#endif
98535      sqlite3WalkExprList(pWalker, pList);
98536      if( is_agg ){
98537#ifndef SQLITE_OMIT_WINDOWFUNC
98538        if( pWin ){
98539          Select *pSel = pNC->pWinSelect;
98540          assert( pWin==pExpr->y.pWin );
98541          if( IN_RENAME_OBJECT==0 ){
98542            sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
98543          }
98544          sqlite3WalkExprList(pWalker, pWin->pPartition);
98545          sqlite3WalkExprList(pWalker, pWin->pOrderBy);
98546          sqlite3WalkExpr(pWalker, pWin->pFilter);
98547          sqlite3WindowLink(pSel, pWin);
98548          pNC->ncFlags |= NC_HasWin;
98549        }else
98550#endif /* SQLITE_OMIT_WINDOWFUNC */
98551        {
98552          NameContext *pNC2 = pNC;
98553          pExpr->op = TK_AGG_FUNCTION;
98554          pExpr->op2 = 0;
98555#ifndef SQLITE_OMIT_WINDOWFUNC
98556          if( ExprHasProperty(pExpr, EP_WinFunc) ){
98557            sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
98558          }
98559#endif
98560          while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
98561            pExpr->op2++;
98562            pNC2 = pNC2->pNext;
98563          }
98564          assert( pDef!=0 || IN_RENAME_OBJECT );
98565          if( pNC2 && pDef ){
98566            assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
98567            testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
98568            pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
98569
98570          }
98571        }
98572        pNC->ncFlags |= savedAllowFlags;
98573      }
98574      /* FIX ME:  Compute pExpr->affinity based on the expected return
98575      ** type of the function
98576      */
98577      return WRC_Prune;
98578    }
98579#ifndef SQLITE_OMIT_SUBQUERY
98580    case TK_SELECT:
98581    case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
98582#endif
98583    case TK_IN: {
98584      testcase( pExpr->op==TK_IN );
98585      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
98586        int nRef = pNC->nRef;
98587        testcase( pNC->ncFlags & NC_IsCheck );
98588        testcase( pNC->ncFlags & NC_PartIdx );
98589        testcase( pNC->ncFlags & NC_IdxExpr );
98590        testcase( pNC->ncFlags & NC_GenCol );
98591        sqlite3ResolveNotValid(pParse, pNC, "subqueries",
98592                 NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol, pExpr);
98593        sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
98594        assert( pNC->nRef>=nRef );
98595        if( nRef!=pNC->nRef ){
98596          ExprSetProperty(pExpr, EP_VarSelect);
98597          pNC->ncFlags |= NC_VarSelect;
98598        }
98599      }
98600      break;
98601    }
98602    case TK_VARIABLE: {
98603      testcase( pNC->ncFlags & NC_IsCheck );
98604      testcase( pNC->ncFlags & NC_PartIdx );
98605      testcase( pNC->ncFlags & NC_IdxExpr );
98606      testcase( pNC->ncFlags & NC_GenCol );
98607      sqlite3ResolveNotValid(pParse, pNC, "parameters",
98608               NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol, pExpr);
98609      break;
98610    }
98611    case TK_IS:
98612    case TK_ISNOT: {
98613      Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
98614      assert( !ExprHasProperty(pExpr, EP_Reduced) );
98615      /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
98616      ** and "x IS NOT FALSE". */
98617      if( pRight && pRight->op==TK_ID ){
98618        int rc = resolveExprStep(pWalker, pRight);
98619        if( rc==WRC_Abort ) return WRC_Abort;
98620        if( pRight->op==TK_TRUEFALSE ){
98621          pExpr->op2 = pExpr->op;
98622          pExpr->op = TK_TRUTH;
98623          return WRC_Continue;
98624        }
98625      }
98626      /* Fall thru */
98627    }
98628    case TK_BETWEEN:
98629    case TK_EQ:
98630    case TK_NE:
98631    case TK_LT:
98632    case TK_LE:
98633    case TK_GT:
98634    case TK_GE: {
98635      int nLeft, nRight;
98636      if( pParse->db->mallocFailed ) break;
98637      assert( pExpr->pLeft!=0 );
98638      nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
98639      if( pExpr->op==TK_BETWEEN ){
98640        nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
98641        if( nRight==nLeft ){
98642          nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
98643        }
98644      }else{
98645        assert( pExpr->pRight!=0 );
98646        nRight = sqlite3ExprVectorSize(pExpr->pRight);
98647      }
98648      if( nLeft!=nRight ){
98649        testcase( pExpr->op==TK_EQ );
98650        testcase( pExpr->op==TK_NE );
98651        testcase( pExpr->op==TK_LT );
98652        testcase( pExpr->op==TK_LE );
98653        testcase( pExpr->op==TK_GT );
98654        testcase( pExpr->op==TK_GE );
98655        testcase( pExpr->op==TK_IS );
98656        testcase( pExpr->op==TK_ISNOT );
98657        testcase( pExpr->op==TK_BETWEEN );
98658        sqlite3ErrorMsg(pParse, "row value misused");
98659      }
98660      break;
98661    }
98662  }
98663  return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
98664}
98665
98666/*
98667** pEList is a list of expressions which are really the result set of the
98668** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
98669** This routine checks to see if pE is a simple identifier which corresponds
98670** to the AS-name of one of the terms of the expression list.  If it is,
98671** this routine return an integer between 1 and N where N is the number of
98672** elements in pEList, corresponding to the matching entry.  If there is
98673** no match, or if pE is not a simple identifier, then this routine
98674** return 0.
98675**
98676** pEList has been resolved.  pE has not.
98677*/
98678static int resolveAsName(
98679  Parse *pParse,     /* Parsing context for error messages */
98680  ExprList *pEList,  /* List of expressions to scan */
98681  Expr *pE           /* Expression we are trying to match */
98682){
98683  int i;             /* Loop counter */
98684
98685  UNUSED_PARAMETER(pParse);
98686
98687  if( pE->op==TK_ID ){
98688    char *zCol = pE->u.zToken;
98689    for(i=0; i<pEList->nExpr; i++){
98690      if( pEList->a[i].eEName==ENAME_NAME
98691       && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0
98692      ){
98693        return i+1;
98694      }
98695    }
98696  }
98697  return 0;
98698}
98699
98700/*
98701** pE is a pointer to an expression which is a single term in the
98702** ORDER BY of a compound SELECT.  The expression has not been
98703** name resolved.
98704**
98705** At the point this routine is called, we already know that the
98706** ORDER BY term is not an integer index into the result set.  That
98707** case is handled by the calling routine.
98708**
98709** Attempt to match pE against result set columns in the left-most
98710** SELECT statement.  Return the index i of the matching column,
98711** as an indication to the caller that it should sort by the i-th column.
98712** The left-most column is 1.  In other words, the value returned is the
98713** same integer value that would be used in the SQL statement to indicate
98714** the column.
98715**
98716** If there is no match, return 0.  Return -1 if an error occurs.
98717*/
98718static int resolveOrderByTermToExprList(
98719  Parse *pParse,     /* Parsing context for error messages */
98720  Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
98721  Expr *pE           /* The specific ORDER BY term */
98722){
98723  int i;             /* Loop counter */
98724  ExprList *pEList;  /* The columns of the result set */
98725  NameContext nc;    /* Name context for resolving pE */
98726  sqlite3 *db;       /* Database connection */
98727  int rc;            /* Return code from subprocedures */
98728  u8 savedSuppErr;   /* Saved value of db->suppressErr */
98729
98730  assert( sqlite3ExprIsInteger(pE, &i)==0 );
98731  pEList = pSelect->pEList;
98732
98733  /* Resolve all names in the ORDER BY term expression
98734  */
98735  memset(&nc, 0, sizeof(nc));
98736  nc.pParse = pParse;
98737  nc.pSrcList = pSelect->pSrc;
98738  nc.uNC.pEList = pEList;
98739  nc.ncFlags = NC_AllowAgg|NC_UEList;
98740  nc.nErr = 0;
98741  db = pParse->db;
98742  savedSuppErr = db->suppressErr;
98743  if( IN_RENAME_OBJECT==0 ) db->suppressErr = 1;
98744  rc = sqlite3ResolveExprNames(&nc, pE);
98745  db->suppressErr = savedSuppErr;
98746  if( rc ) return 0;
98747
98748  /* Try to match the ORDER BY expression against an expression
98749  ** in the result set.  Return an 1-based index of the matching
98750  ** result-set entry.
98751  */
98752  for(i=0; i<pEList->nExpr; i++){
98753    if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
98754      return i+1;
98755    }
98756  }
98757
98758  /* If no match, return 0. */
98759  return 0;
98760}
98761
98762/*
98763** Generate an ORDER BY or GROUP BY term out-of-range error.
98764*/
98765static void resolveOutOfRangeError(
98766  Parse *pParse,         /* The error context into which to write the error */
98767  const char *zType,     /* "ORDER" or "GROUP" */
98768  int i,                 /* The index (1-based) of the term out of range */
98769  int mx                 /* Largest permissible value of i */
98770){
98771  sqlite3ErrorMsg(pParse,
98772    "%r %s BY term out of range - should be "
98773    "between 1 and %d", i, zType, mx);
98774}
98775
98776/*
98777** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
98778** each term of the ORDER BY clause is a constant integer between 1
98779** and N where N is the number of columns in the compound SELECT.
98780**
98781** ORDER BY terms that are already an integer between 1 and N are
98782** unmodified.  ORDER BY terms that are integers outside the range of
98783** 1 through N generate an error.  ORDER BY terms that are expressions
98784** are matched against result set expressions of compound SELECT
98785** beginning with the left-most SELECT and working toward the right.
98786** At the first match, the ORDER BY expression is transformed into
98787** the integer column number.
98788**
98789** Return the number of errors seen.
98790*/
98791static int resolveCompoundOrderBy(
98792  Parse *pParse,        /* Parsing context.  Leave error messages here */
98793  Select *pSelect       /* The SELECT statement containing the ORDER BY */
98794){
98795  int i;
98796  ExprList *pOrderBy;
98797  ExprList *pEList;
98798  sqlite3 *db;
98799  int moreToDo = 1;
98800
98801  pOrderBy = pSelect->pOrderBy;
98802  if( pOrderBy==0 ) return 0;
98803  db = pParse->db;
98804  if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
98805    sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
98806    return 1;
98807  }
98808  for(i=0; i<pOrderBy->nExpr; i++){
98809    pOrderBy->a[i].done = 0;
98810  }
98811  pSelect->pNext = 0;
98812  while( pSelect->pPrior ){
98813    pSelect->pPrior->pNext = pSelect;
98814    pSelect = pSelect->pPrior;
98815  }
98816  while( pSelect && moreToDo ){
98817    struct ExprList_item *pItem;
98818    moreToDo = 0;
98819    pEList = pSelect->pEList;
98820    assert( pEList!=0 );
98821    for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
98822      int iCol = -1;
98823      Expr *pE, *pDup;
98824      if( pItem->done ) continue;
98825      pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
98826      if( sqlite3ExprIsInteger(pE, &iCol) ){
98827        if( iCol<=0 || iCol>pEList->nExpr ){
98828          resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
98829          return 1;
98830        }
98831      }else{
98832        iCol = resolveAsName(pParse, pEList, pE);
98833        if( iCol==0 ){
98834          /* Now test if expression pE matches one of the values returned
98835          ** by pSelect. In the usual case this is done by duplicating the
98836          ** expression, resolving any symbols in it, and then comparing
98837          ** it against each expression returned by the SELECT statement.
98838          ** Once the comparisons are finished, the duplicate expression
98839          ** is deleted.
98840          **
98841          ** Or, if this is running as part of an ALTER TABLE operation,
98842          ** resolve the symbols in the actual expression, not a duplicate.
98843          ** And, if one of the comparisons is successful, leave the expression
98844          ** as is instead of transforming it to an integer as in the usual
98845          ** case. This allows the code in alter.c to modify column
98846          ** refererences within the ORDER BY expression as required.  */
98847          if( IN_RENAME_OBJECT ){
98848            pDup = pE;
98849          }else{
98850            pDup = sqlite3ExprDup(db, pE, 0);
98851          }
98852          if( !db->mallocFailed ){
98853            assert(pDup);
98854            iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
98855          }
98856          if( !IN_RENAME_OBJECT ){
98857            sqlite3ExprDelete(db, pDup);
98858          }
98859        }
98860      }
98861      if( iCol>0 ){
98862        /* Convert the ORDER BY term into an integer column number iCol,
98863        ** taking care to preserve the COLLATE clause if it exists */
98864        if( !IN_RENAME_OBJECT ){
98865          Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
98866          if( pNew==0 ) return 1;
98867          pNew->flags |= EP_IntValue;
98868          pNew->u.iValue = iCol;
98869          if( pItem->pExpr==pE ){
98870            pItem->pExpr = pNew;
98871          }else{
98872            Expr *pParent = pItem->pExpr;
98873            assert( pParent->op==TK_COLLATE );
98874            while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
98875            assert( pParent->pLeft==pE );
98876            pParent->pLeft = pNew;
98877          }
98878          sqlite3ExprDelete(db, pE);
98879          pItem->u.x.iOrderByCol = (u16)iCol;
98880        }
98881        pItem->done = 1;
98882      }else{
98883        moreToDo = 1;
98884      }
98885    }
98886    pSelect = pSelect->pNext;
98887  }
98888  for(i=0; i<pOrderBy->nExpr; i++){
98889    if( pOrderBy->a[i].done==0 ){
98890      sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
98891            "column in the result set", i+1);
98892      return 1;
98893    }
98894  }
98895  return 0;
98896}
98897
98898/*
98899** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
98900** the SELECT statement pSelect.  If any term is reference to a
98901** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
98902** field) then convert that term into a copy of the corresponding result set
98903** column.
98904**
98905** If any errors are detected, add an error message to pParse and
98906** return non-zero.  Return zero if no errors are seen.
98907*/
98908SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
98909  Parse *pParse,        /* Parsing context.  Leave error messages here */
98910  Select *pSelect,      /* The SELECT statement containing the clause */
98911  ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
98912  const char *zType     /* "ORDER" or "GROUP" */
98913){
98914  int i;
98915  sqlite3 *db = pParse->db;
98916  ExprList *pEList;
98917  struct ExprList_item *pItem;
98918
98919  if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
98920  if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
98921    sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
98922    return 1;
98923  }
98924  pEList = pSelect->pEList;
98925  assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
98926  for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
98927    if( pItem->u.x.iOrderByCol ){
98928      if( pItem->u.x.iOrderByCol>pEList->nExpr ){
98929        resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
98930        return 1;
98931      }
98932      resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,
98933                   zType,0);
98934    }
98935  }
98936  return 0;
98937}
98938
98939#ifndef SQLITE_OMIT_WINDOWFUNC
98940/*
98941** Walker callback for windowRemoveExprFromSelect().
98942*/
98943static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
98944  UNUSED_PARAMETER(pWalker);
98945  if( ExprHasProperty(pExpr, EP_WinFunc) ){
98946    Window *pWin = pExpr->y.pWin;
98947    sqlite3WindowUnlinkFromSelect(pWin);
98948  }
98949  return WRC_Continue;
98950}
98951
98952/*
98953** Remove any Window objects owned by the expression pExpr from the
98954** Select.pWin list of Select object pSelect.
98955*/
98956static void windowRemoveExprFromSelect(Select *pSelect, Expr *pExpr){
98957  if( pSelect->pWin ){
98958    Walker sWalker;
98959    memset(&sWalker, 0, sizeof(Walker));
98960    sWalker.xExprCallback = resolveRemoveWindowsCb;
98961    sWalker.u.pSelect = pSelect;
98962    sqlite3WalkExpr(&sWalker, pExpr);
98963  }
98964}
98965#else
98966# define windowRemoveExprFromSelect(a, b)
98967#endif /* SQLITE_OMIT_WINDOWFUNC */
98968
98969/*
98970** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
98971** The Name context of the SELECT statement is pNC.  zType is either
98972** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
98973**
98974** This routine resolves each term of the clause into an expression.
98975** If the order-by term is an integer I between 1 and N (where N is the
98976** number of columns in the result set of the SELECT) then the expression
98977** in the resolution is a copy of the I-th result-set expression.  If
98978** the order-by term is an identifier that corresponds to the AS-name of
98979** a result-set expression, then the term resolves to a copy of the
98980** result-set expression.  Otherwise, the expression is resolved in
98981** the usual way - using sqlite3ResolveExprNames().
98982**
98983** This routine returns the number of errors.  If errors occur, then
98984** an appropriate error message might be left in pParse.  (OOM errors
98985** excepted.)
98986*/
98987static int resolveOrderGroupBy(
98988  NameContext *pNC,     /* The name context of the SELECT statement */
98989  Select *pSelect,      /* The SELECT statement holding pOrderBy */
98990  ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
98991  const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
98992){
98993  int i, j;                      /* Loop counters */
98994  int iCol;                      /* Column number */
98995  struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
98996  Parse *pParse;                 /* Parsing context */
98997  int nResult;                   /* Number of terms in the result set */
98998
98999  if( pOrderBy==0 ) return 0;
99000  nResult = pSelect->pEList->nExpr;
99001  pParse = pNC->pParse;
99002  for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
99003    Expr *pE = pItem->pExpr;
99004    Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pE);
99005    if( zType[0]!='G' ){
99006      iCol = resolveAsName(pParse, pSelect->pEList, pE2);
99007      if( iCol>0 ){
99008        /* If an AS-name match is found, mark this ORDER BY column as being
99009        ** a copy of the iCol-th result-set column.  The subsequent call to
99010        ** sqlite3ResolveOrderGroupBy() will convert the expression to a
99011        ** copy of the iCol-th result-set expression. */
99012        pItem->u.x.iOrderByCol = (u16)iCol;
99013        continue;
99014      }
99015    }
99016    if( sqlite3ExprIsInteger(pE2, &iCol) ){
99017      /* The ORDER BY term is an integer constant.  Again, set the column
99018      ** number so that sqlite3ResolveOrderGroupBy() will convert the
99019      ** order-by term to a copy of the result-set expression */
99020      if( iCol<1 || iCol>0xffff ){
99021        resolveOutOfRangeError(pParse, zType, i+1, nResult);
99022        return 1;
99023      }
99024      pItem->u.x.iOrderByCol = (u16)iCol;
99025      continue;
99026    }
99027
99028    /* Otherwise, treat the ORDER BY term as an ordinary expression */
99029    pItem->u.x.iOrderByCol = 0;
99030    if( sqlite3ResolveExprNames(pNC, pE) ){
99031      return 1;
99032    }
99033    for(j=0; j<pSelect->pEList->nExpr; j++){
99034      if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
99035        /* Since this expresion is being changed into a reference
99036        ** to an identical expression in the result set, remove all Window
99037        ** objects belonging to the expression from the Select.pWin list. */
99038        windowRemoveExprFromSelect(pSelect, pE);
99039        pItem->u.x.iOrderByCol = j+1;
99040      }
99041    }
99042  }
99043  return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
99044}
99045
99046/*
99047** Resolve names in the SELECT statement p and all of its descendants.
99048*/
99049static int resolveSelectStep(Walker *pWalker, Select *p){
99050  NameContext *pOuterNC;  /* Context that contains this SELECT */
99051  NameContext sNC;        /* Name context of this SELECT */
99052  int isCompound;         /* True if p is a compound select */
99053  int nCompound;          /* Number of compound terms processed so far */
99054  Parse *pParse;          /* Parsing context */
99055  int i;                  /* Loop counter */
99056  ExprList *pGroupBy;     /* The GROUP BY clause */
99057  Select *pLeftmost;      /* Left-most of SELECT of a compound */
99058  sqlite3 *db;            /* Database connection */
99059
99060
99061  assert( p!=0 );
99062  if( p->selFlags & SF_Resolved ){
99063    return WRC_Prune;
99064  }
99065  pOuterNC = pWalker->u.pNC;
99066  pParse = pWalker->pParse;
99067  db = pParse->db;
99068
99069  /* Normally sqlite3SelectExpand() will be called first and will have
99070  ** already expanded this SELECT.  However, if this is a subquery within
99071  ** an expression, sqlite3ResolveExprNames() will be called without a
99072  ** prior call to sqlite3SelectExpand().  When that happens, let
99073  ** sqlite3SelectPrep() do all of the processing for this SELECT.
99074  ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
99075  ** this routine in the correct order.
99076  */
99077  if( (p->selFlags & SF_Expanded)==0 ){
99078    sqlite3SelectPrep(pParse, p, pOuterNC);
99079    return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
99080  }
99081
99082  isCompound = p->pPrior!=0;
99083  nCompound = 0;
99084  pLeftmost = p;
99085  while( p ){
99086    assert( (p->selFlags & SF_Expanded)!=0 );
99087    assert( (p->selFlags & SF_Resolved)==0 );
99088    p->selFlags |= SF_Resolved;
99089
99090    /* Resolve the expressions in the LIMIT and OFFSET clauses. These
99091    ** are not allowed to refer to any names, so pass an empty NameContext.
99092    */
99093    memset(&sNC, 0, sizeof(sNC));
99094    sNC.pParse = pParse;
99095    sNC.pWinSelect = p;
99096    if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
99097      return WRC_Abort;
99098    }
99099
99100    /* If the SF_Converted flags is set, then this Select object was
99101    ** was created by the convertCompoundSelectToSubquery() function.
99102    ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
99103    ** as if it were part of the sub-query, not the parent. This block
99104    ** moves the pOrderBy down to the sub-query. It will be moved back
99105    ** after the names have been resolved.  */
99106    if( p->selFlags & SF_Converted ){
99107      Select *pSub = p->pSrc->a[0].pSelect;
99108      assert( p->pSrc->nSrc==1 && p->pOrderBy );
99109      assert( pSub->pPrior && pSub->pOrderBy==0 );
99110      pSub->pOrderBy = p->pOrderBy;
99111      p->pOrderBy = 0;
99112    }
99113
99114    /* Recursively resolve names in all subqueries
99115    */
99116    for(i=0; i<p->pSrc->nSrc; i++){
99117      struct SrcList_item *pItem = &p->pSrc->a[i];
99118      if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
99119        NameContext *pNC;         /* Used to iterate name contexts */
99120        int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
99121        const char *zSavedContext = pParse->zAuthContext;
99122
99123        /* Count the total number of references to pOuterNC and all of its
99124        ** parent contexts. After resolving references to expressions in
99125        ** pItem->pSelect, check if this value has changed. If so, then
99126        ** SELECT statement pItem->pSelect must be correlated. Set the
99127        ** pItem->fg.isCorrelated flag if this is the case. */
99128        for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
99129
99130        if( pItem->zName ) pParse->zAuthContext = pItem->zName;
99131        sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
99132        pParse->zAuthContext = zSavedContext;
99133        if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
99134
99135        for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
99136        assert( pItem->fg.isCorrelated==0 && nRef<=0 );
99137        pItem->fg.isCorrelated = (nRef!=0);
99138      }
99139    }
99140
99141    /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
99142    ** resolve the result-set expression list.
99143    */
99144    sNC.ncFlags = NC_AllowAgg|NC_AllowWin;
99145    sNC.pSrcList = p->pSrc;
99146    sNC.pNext = pOuterNC;
99147
99148    /* Resolve names in the result set. */
99149    if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
99150    sNC.ncFlags &= ~NC_AllowWin;
99151
99152    /* If there are no aggregate functions in the result-set, and no GROUP BY
99153    ** expression, do not allow aggregates in any of the other expressions.
99154    */
99155    assert( (p->selFlags & SF_Aggregate)==0 );
99156    pGroupBy = p->pGroupBy;
99157    if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
99158      assert( NC_MinMaxAgg==SF_MinMaxAgg );
99159      p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg);
99160    }else{
99161      sNC.ncFlags &= ~NC_AllowAgg;
99162    }
99163
99164    /* If a HAVING clause is present, then there must be a GROUP BY clause.
99165    */
99166    if( p->pHaving && !pGroupBy ){
99167      sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
99168      return WRC_Abort;
99169    }
99170
99171    /* Add the output column list to the name-context before parsing the
99172    ** other expressions in the SELECT statement. This is so that
99173    ** expressions in the WHERE clause (etc.) can refer to expressions by
99174    ** aliases in the result set.
99175    **
99176    ** Minor point: If this is the case, then the expression will be
99177    ** re-evaluated for each reference to it.
99178    */
99179    assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert))==0 );
99180    sNC.uNC.pEList = p->pEList;
99181    sNC.ncFlags |= NC_UEList;
99182    if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
99183    if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
99184
99185    /* Resolve names in table-valued-function arguments */
99186    for(i=0; i<p->pSrc->nSrc; i++){
99187      struct SrcList_item *pItem = &p->pSrc->a[i];
99188      if( pItem->fg.isTabFunc
99189       && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
99190      ){
99191        return WRC_Abort;
99192      }
99193    }
99194
99195    /* The ORDER BY and GROUP BY clauses may not refer to terms in
99196    ** outer queries
99197    */
99198    sNC.pNext = 0;
99199    sNC.ncFlags |= NC_AllowAgg|NC_AllowWin;
99200
99201    /* If this is a converted compound query, move the ORDER BY clause from
99202    ** the sub-query back to the parent query. At this point each term
99203    ** within the ORDER BY clause has been transformed to an integer value.
99204    ** These integers will be replaced by copies of the corresponding result
99205    ** set expressions by the call to resolveOrderGroupBy() below.  */
99206    if( p->selFlags & SF_Converted ){
99207      Select *pSub = p->pSrc->a[0].pSelect;
99208      p->pOrderBy = pSub->pOrderBy;
99209      pSub->pOrderBy = 0;
99210    }
99211
99212    /* Process the ORDER BY clause for singleton SELECT statements.
99213    ** The ORDER BY clause for compounds SELECT statements is handled
99214    ** below, after all of the result-sets for all of the elements of
99215    ** the compound have been resolved.
99216    **
99217    ** If there is an ORDER BY clause on a term of a compound-select other
99218    ** than the right-most term, then that is a syntax error.  But the error
99219    ** is not detected until much later, and so we need to go ahead and
99220    ** resolve those symbols on the incorrect ORDER BY for consistency.
99221    */
99222    if( isCompound<=nCompound  /* Defer right-most ORDER BY of a compound */
99223     && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
99224    ){
99225      return WRC_Abort;
99226    }
99227    if( db->mallocFailed ){
99228      return WRC_Abort;
99229    }
99230    sNC.ncFlags &= ~NC_AllowWin;
99231
99232    /* Resolve the GROUP BY clause.  At the same time, make sure
99233    ** the GROUP BY clause does not contain aggregate functions.
99234    */
99235    if( pGroupBy ){
99236      struct ExprList_item *pItem;
99237
99238      if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
99239        return WRC_Abort;
99240      }
99241      for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
99242        if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
99243          sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
99244              "the GROUP BY clause");
99245          return WRC_Abort;
99246        }
99247      }
99248    }
99249
99250#ifndef SQLITE_OMIT_WINDOWFUNC
99251    if( IN_RENAME_OBJECT ){
99252      Window *pWin;
99253      for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
99254        if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
99255         || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
99256        ){
99257          return WRC_Abort;
99258        }
99259      }
99260    }
99261#endif
99262
99263    /* If this is part of a compound SELECT, check that it has the right
99264    ** number of expressions in the select list. */
99265    if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
99266      sqlite3SelectWrongNumTermsError(pParse, p->pNext);
99267      return WRC_Abort;
99268    }
99269
99270    /* Advance to the next term of the compound
99271    */
99272    p = p->pPrior;
99273    nCompound++;
99274  }
99275
99276  /* Resolve the ORDER BY on a compound SELECT after all terms of
99277  ** the compound have been resolved.
99278  */
99279  if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
99280    return WRC_Abort;
99281  }
99282
99283  return WRC_Prune;
99284}
99285
99286/*
99287** This routine walks an expression tree and resolves references to
99288** table columns and result-set columns.  At the same time, do error
99289** checking on function usage and set a flag if any aggregate functions
99290** are seen.
99291**
99292** To resolve table columns references we look for nodes (or subtrees) of the
99293** form X.Y.Z or Y.Z or just Z where
99294**
99295**      X:   The name of a database.  Ex:  "main" or "temp" or
99296**           the symbolic name assigned to an ATTACH-ed database.
99297**
99298**      Y:   The name of a table in a FROM clause.  Or in a trigger
99299**           one of the special names "old" or "new".
99300**
99301**      Z:   The name of a column in table Y.
99302**
99303** The node at the root of the subtree is modified as follows:
99304**
99305**    Expr.op        Changed to TK_COLUMN
99306**    Expr.pTab      Points to the Table object for X.Y
99307**    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
99308**    Expr.iTable    The VDBE cursor number for X.Y
99309**
99310**
99311** To resolve result-set references, look for expression nodes of the
99312** form Z (with no X and Y prefix) where the Z matches the right-hand
99313** size of an AS clause in the result-set of a SELECT.  The Z expression
99314** is replaced by a copy of the left-hand side of the result-set expression.
99315** Table-name and function resolution occurs on the substituted expression
99316** tree.  For example, in:
99317**
99318**      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
99319**
99320** The "x" term of the order by is replaced by "a+b" to render:
99321**
99322**      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
99323**
99324** Function calls are checked to make sure that the function is
99325** defined and that the correct number of arguments are specified.
99326** If the function is an aggregate function, then the NC_HasAgg flag is
99327** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
99328** If an expression contains aggregate functions then the EP_Agg
99329** property on the expression is set.
99330**
99331** An error message is left in pParse if anything is amiss.  The number
99332** if errors is returned.
99333*/
99334SQLITE_PRIVATE int sqlite3ResolveExprNames(
99335  NameContext *pNC,       /* Namespace to resolve expressions in. */
99336  Expr *pExpr             /* The expression to be analyzed. */
99337){
99338  int savedHasAgg;
99339  Walker w;
99340
99341  if( pExpr==0 ) return SQLITE_OK;
99342  savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
99343  pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
99344  w.pParse = pNC->pParse;
99345  w.xExprCallback = resolveExprStep;
99346  w.xSelectCallback = resolveSelectStep;
99347  w.xSelectCallback2 = 0;
99348  w.u.pNC = pNC;
99349#if SQLITE_MAX_EXPR_DEPTH>0
99350  w.pParse->nHeight += pExpr->nHeight;
99351  if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
99352    return SQLITE_ERROR;
99353  }
99354#endif
99355  sqlite3WalkExpr(&w, pExpr);
99356#if SQLITE_MAX_EXPR_DEPTH>0
99357  w.pParse->nHeight -= pExpr->nHeight;
99358#endif
99359  assert( EP_Agg==NC_HasAgg );
99360  assert( EP_Win==NC_HasWin );
99361  testcase( pNC->ncFlags & NC_HasAgg );
99362  testcase( pNC->ncFlags & NC_HasWin );
99363  ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
99364  pNC->ncFlags |= savedHasAgg;
99365  return pNC->nErr>0 || w.pParse->nErr>0;
99366}
99367
99368/*
99369** Resolve all names for all expression in an expression list.  This is
99370** just like sqlite3ResolveExprNames() except that it works for an expression
99371** list rather than a single expression.
99372*/
99373SQLITE_PRIVATE int sqlite3ResolveExprListNames(
99374  NameContext *pNC,       /* Namespace to resolve expressions in. */
99375  ExprList *pList         /* The expression list to be analyzed. */
99376){
99377  int i;
99378  int savedHasAgg = 0;
99379  Walker w;
99380  if( pList==0 ) return WRC_Continue;
99381  w.pParse = pNC->pParse;
99382  w.xExprCallback = resolveExprStep;
99383  w.xSelectCallback = resolveSelectStep;
99384  w.xSelectCallback2 = 0;
99385  w.u.pNC = pNC;
99386  savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
99387  pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
99388  for(i=0; i<pList->nExpr; i++){
99389    Expr *pExpr = pList->a[i].pExpr;
99390    if( pExpr==0 ) continue;
99391#if SQLITE_MAX_EXPR_DEPTH>0
99392    w.pParse->nHeight += pExpr->nHeight;
99393    if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
99394      return WRC_Abort;
99395    }
99396#endif
99397    sqlite3WalkExpr(&w, pExpr);
99398#if SQLITE_MAX_EXPR_DEPTH>0
99399    w.pParse->nHeight -= pExpr->nHeight;
99400#endif
99401    assert( EP_Agg==NC_HasAgg );
99402    assert( EP_Win==NC_HasWin );
99403    testcase( pNC->ncFlags & NC_HasAgg );
99404    testcase( pNC->ncFlags & NC_HasWin );
99405    if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin) ){
99406      ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
99407      savedHasAgg |= pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
99408      pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
99409    }
99410    if( pNC->nErr>0 || w.pParse->nErr>0 ) return WRC_Abort;
99411  }
99412  pNC->ncFlags |= savedHasAgg;
99413  return WRC_Continue;
99414}
99415
99416/*
99417** Resolve all names in all expressions of a SELECT and in all
99418** decendents of the SELECT, including compounds off of p->pPrior,
99419** subqueries in expressions, and subqueries used as FROM clause
99420** terms.
99421**
99422** See sqlite3ResolveExprNames() for a description of the kinds of
99423** transformations that occur.
99424**
99425** All SELECT statements should have been expanded using
99426** sqlite3SelectExpand() prior to invoking this routine.
99427*/
99428SQLITE_PRIVATE void sqlite3ResolveSelectNames(
99429  Parse *pParse,         /* The parser context */
99430  Select *p,             /* The SELECT statement being coded. */
99431  NameContext *pOuterNC  /* Name context for parent SELECT statement */
99432){
99433  Walker w;
99434
99435  assert( p!=0 );
99436  w.xExprCallback = resolveExprStep;
99437  w.xSelectCallback = resolveSelectStep;
99438  w.xSelectCallback2 = 0;
99439  w.pParse = pParse;
99440  w.u.pNC = pOuterNC;
99441  sqlite3WalkSelect(&w, p);
99442}
99443
99444/*
99445** Resolve names in expressions that can only reference a single table
99446** or which cannot reference any tables at all.  Examples:
99447**
99448**                                                    "type" flag
99449**                                                    ------------
99450**    (1)   CHECK constraints                         NC_IsCheck
99451**    (2)   WHERE clauses on partial indices          NC_PartIdx
99452**    (3)   Expressions in indexes on expressions     NC_IdxExpr
99453**    (4)   Expression arguments to VACUUM INTO.      0
99454**    (5)   GENERATED ALWAYS as expressions           NC_GenCol
99455**
99456** In all cases except (4), the Expr.iTable value for Expr.op==TK_COLUMN
99457** nodes of the expression is set to -1 and the Expr.iColumn value is
99458** set to the column number.  In case (4), TK_COLUMN nodes cause an error.
99459**
99460** Any errors cause an error message to be set in pParse.
99461*/
99462SQLITE_PRIVATE int sqlite3ResolveSelfReference(
99463  Parse *pParse,   /* Parsing context */
99464  Table *pTab,     /* The table being referenced, or NULL */
99465  int type,        /* NC_IsCheck, NC_PartIdx, NC_IdxExpr, NC_GenCol, or 0 */
99466  Expr *pExpr,     /* Expression to resolve.  May be NULL. */
99467  ExprList *pList  /* Expression list to resolve.  May be NULL. */
99468){
99469  SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
99470  NameContext sNC;                /* Name context for pParse->pNewTable */
99471  int rc;
99472
99473  assert( type==0 || pTab!=0 );
99474  assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr
99475          || type==NC_GenCol || pTab==0 );
99476  memset(&sNC, 0, sizeof(sNC));
99477  memset(&sSrc, 0, sizeof(sSrc));
99478  if( pTab ){
99479    sSrc.nSrc = 1;
99480    sSrc.a[0].zName = pTab->zName;
99481    sSrc.a[0].pTab = pTab;
99482    sSrc.a[0].iCursor = -1;
99483    if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
99484      /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
99485      ** schema elements */
99486      type |= NC_FromDDL;
99487    }
99488  }
99489  sNC.pParse = pParse;
99490  sNC.pSrcList = &sSrc;
99491  sNC.ncFlags = type | NC_IsDDL;
99492  if( (rc = sqlite3ResolveExprNames(&sNC, pExpr))!=SQLITE_OK ) return rc;
99493  if( pList ) rc = sqlite3ResolveExprListNames(&sNC, pList);
99494  return rc;
99495}
99496
99497/************** End of resolve.c *********************************************/
99498/************** Begin file expr.c ********************************************/
99499/*
99500** 2001 September 15
99501**
99502** The author disclaims copyright to this source code.  In place of
99503** a legal notice, here is a blessing:
99504**
99505**    May you do good and not evil.
99506**    May you find forgiveness for yourself and forgive others.
99507**    May you share freely, never taking more than you give.
99508**
99509*************************************************************************
99510** This file contains routines used for analyzing expressions and
99511** for generating VDBE code that evaluates expressions in SQLite.
99512*/
99513/* #include "sqliteInt.h" */
99514
99515/* Forward declarations */
99516static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
99517static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree);
99518
99519/*
99520** Return the affinity character for a single column of a table.
99521*/
99522SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table *pTab, int iCol){
99523  assert( iCol<pTab->nCol );
99524  return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
99525}
99526
99527/*
99528** Return the 'affinity' of the expression pExpr if any.
99529**
99530** If pExpr is a column, a reference to a column via an 'AS' alias,
99531** or a sub-select with a column as the return value, then the
99532** affinity of that column is returned. Otherwise, 0x00 is returned,
99533** indicating no affinity for the expression.
99534**
99535** i.e. the WHERE clause expressions in the following statements all
99536** have an affinity:
99537**
99538** CREATE TABLE t1(a);
99539** SELECT * FROM t1 WHERE a;
99540** SELECT a AS b FROM t1 WHERE b;
99541** SELECT * FROM t1 WHERE (select a from t1);
99542*/
99543SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr){
99544  int op;
99545  while( ExprHasProperty(pExpr, EP_Skip) ){
99546    assert( pExpr->op==TK_COLLATE || pExpr->op==TK_IF_NULL_ROW );
99547    pExpr = pExpr->pLeft;
99548    assert( pExpr!=0 );
99549  }
99550  op = pExpr->op;
99551  if( op==TK_SELECT ){
99552    assert( pExpr->flags&EP_xIsSelect );
99553    if( ALWAYS(pExpr->x.pSelect)
99554     && pExpr->x.pSelect->pEList
99555     && ALWAYS(pExpr->x.pSelect->pEList->a[0].pExpr)
99556    ){
99557      return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
99558    }
99559  }
99560  if( op==TK_REGISTER ) op = pExpr->op2;
99561#ifndef SQLITE_OMIT_CAST
99562  if( op==TK_CAST ){
99563    assert( !ExprHasProperty(pExpr, EP_IntValue) );
99564    return sqlite3AffinityType(pExpr->u.zToken, 0);
99565  }
99566#endif
99567  if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->y.pTab ){
99568    return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
99569  }
99570  if( op==TK_SELECT_COLUMN ){
99571    assert( pExpr->pLeft->flags&EP_xIsSelect );
99572    return sqlite3ExprAffinity(
99573        pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
99574    );
99575  }
99576  if( op==TK_VECTOR ){
99577    return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
99578  }
99579  return pExpr->affExpr;
99580}
99581
99582/*
99583** Set the collating sequence for expression pExpr to be the collating
99584** sequence named by pToken.   Return a pointer to a new Expr node that
99585** implements the COLLATE operator.
99586**
99587** If a memory allocation error occurs, that fact is recorded in pParse->db
99588** and the pExpr parameter is returned unchanged.
99589*/
99590SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(
99591  Parse *pParse,           /* Parsing context */
99592  Expr *pExpr,             /* Add the "COLLATE" clause to this expression */
99593  const Token *pCollName,  /* Name of collating sequence */
99594  int dequote              /* True to dequote pCollName */
99595){
99596  if( pCollName->n>0 ){
99597    Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
99598    if( pNew ){
99599      pNew->pLeft = pExpr;
99600      pNew->flags |= EP_Collate|EP_Skip;
99601      pExpr = pNew;
99602    }
99603  }
99604  return pExpr;
99605}
99606SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
99607  Token s;
99608  assert( zC!=0 );
99609  sqlite3TokenInit(&s, (char*)zC);
99610  return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
99611}
99612
99613/*
99614** Skip over any TK_COLLATE operators.
99615*/
99616SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
99617  while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
99618    assert( pExpr->op==TK_COLLATE || pExpr->op==TK_IF_NULL_ROW );
99619    pExpr = pExpr->pLeft;
99620  }
99621  return pExpr;
99622}
99623
99624/*
99625** Skip over any TK_COLLATE operators and/or any unlikely()
99626** or likelihood() or likely() functions at the root of an
99627** expression.
99628*/
99629SQLITE_PRIVATE Expr *sqlite3ExprSkipCollateAndLikely(Expr *pExpr){
99630  while( pExpr && ExprHasProperty(pExpr, EP_Skip|EP_Unlikely) ){
99631    if( ExprHasProperty(pExpr, EP_Unlikely) ){
99632      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
99633      assert( pExpr->x.pList->nExpr>0 );
99634      assert( pExpr->op==TK_FUNCTION );
99635      pExpr = pExpr->x.pList->a[0].pExpr;
99636    }else{
99637      assert( pExpr->op==TK_COLLATE || pExpr->op==TK_IF_NULL_ROW );
99638      pExpr = pExpr->pLeft;
99639    }
99640  }
99641  return pExpr;
99642}
99643
99644/*
99645** Return the collation sequence for the expression pExpr. If
99646** there is no defined collating sequence, return NULL.
99647**
99648** See also: sqlite3ExprNNCollSeq()
99649**
99650** The sqlite3ExprNNCollSeq() works the same exact that it returns the
99651** default collation if pExpr has no defined collation.
99652**
99653** The collating sequence might be determined by a COLLATE operator
99654** or by the presence of a column with a defined collating sequence.
99655** COLLATE operators take first precedence.  Left operands take
99656** precedence over right operands.
99657*/
99658SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr){
99659  sqlite3 *db = pParse->db;
99660  CollSeq *pColl = 0;
99661  const Expr *p = pExpr;
99662  while( p ){
99663    int op = p->op;
99664    if( op==TK_REGISTER ) op = p->op2;
99665    if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER)
99666     && p->y.pTab!=0
99667    ){
99668      /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
99669      ** a TK_COLUMN but was previously evaluated and cached in a register */
99670      int j = p->iColumn;
99671      if( j>=0 ){
99672        const char *zColl = p->y.pTab->aCol[j].zColl;
99673        pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
99674      }
99675      break;
99676    }
99677    if( op==TK_CAST || op==TK_UPLUS ){
99678      p = p->pLeft;
99679      continue;
99680    }
99681    if( op==TK_VECTOR ){
99682      p = p->x.pList->a[0].pExpr;
99683      continue;
99684    }
99685    if( op==TK_COLLATE ){
99686      pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
99687      break;
99688    }
99689    if( p->flags & EP_Collate ){
99690      if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
99691        p = p->pLeft;
99692      }else{
99693        Expr *pNext  = p->pRight;
99694        /* The Expr.x union is never used at the same time as Expr.pRight */
99695        assert( p->x.pList==0 || p->pRight==0 );
99696        if( p->x.pList!=0
99697         && !db->mallocFailed
99698         && ALWAYS(!ExprHasProperty(p, EP_xIsSelect))
99699        ){
99700          int i;
99701          for(i=0; i<p->x.pList->nExpr; i++){
99702            if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
99703              pNext = p->x.pList->a[i].pExpr;
99704              break;
99705            }
99706          }
99707        }
99708        p = pNext;
99709      }
99710    }else{
99711      break;
99712    }
99713  }
99714  if( sqlite3CheckCollSeq(pParse, pColl) ){
99715    pColl = 0;
99716  }
99717  return pColl;
99718}
99719
99720/*
99721** Return the collation sequence for the expression pExpr. If
99722** there is no defined collating sequence, return a pointer to the
99723** defautl collation sequence.
99724**
99725** See also: sqlite3ExprCollSeq()
99726**
99727** The sqlite3ExprCollSeq() routine works the same except that it
99728** returns NULL if there is no defined collation.
99729*/
99730SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr){
99731  CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
99732  if( p==0 ) p = pParse->db->pDfltColl;
99733  assert( p!=0 );
99734  return p;
99735}
99736
99737/*
99738** Return TRUE if the two expressions have equivalent collating sequences.
99739*/
99740SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse *pParse, const Expr *pE1, const Expr *pE2){
99741  CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1);
99742  CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2);
99743  return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
99744}
99745
99746/*
99747** pExpr is an operand of a comparison operator.  aff2 is the
99748** type affinity of the other operand.  This routine returns the
99749** type affinity that should be used for the comparison operator.
99750*/
99751SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2){
99752  char aff1 = sqlite3ExprAffinity(pExpr);
99753  if( aff1>SQLITE_AFF_NONE && aff2>SQLITE_AFF_NONE ){
99754    /* Both sides of the comparison are columns. If one has numeric
99755    ** affinity, use that. Otherwise use no affinity.
99756    */
99757    if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
99758      return SQLITE_AFF_NUMERIC;
99759    }else{
99760      return SQLITE_AFF_BLOB;
99761    }
99762  }else{
99763    /* One side is a column, the other is not. Use the columns affinity. */
99764    assert( aff1<=SQLITE_AFF_NONE || aff2<=SQLITE_AFF_NONE );
99765    return (aff1<=SQLITE_AFF_NONE ? aff2 : aff1) | SQLITE_AFF_NONE;
99766  }
99767}
99768
99769/*
99770** pExpr is a comparison operator.  Return the type affinity that should
99771** be applied to both operands prior to doing the comparison.
99772*/
99773static char comparisonAffinity(const Expr *pExpr){
99774  char aff;
99775  assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
99776          pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
99777          pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
99778  assert( pExpr->pLeft );
99779  aff = sqlite3ExprAffinity(pExpr->pLeft);
99780  if( pExpr->pRight ){
99781    aff = sqlite3CompareAffinity(pExpr->pRight, aff);
99782  }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
99783    aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
99784  }else if( aff==0 ){
99785    aff = SQLITE_AFF_BLOB;
99786  }
99787  return aff;
99788}
99789
99790/*
99791** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
99792** idx_affinity is the affinity of an indexed column. Return true
99793** if the index with affinity idx_affinity may be used to implement
99794** the comparison in pExpr.
99795*/
99796SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity){
99797  char aff = comparisonAffinity(pExpr);
99798  if( aff<SQLITE_AFF_TEXT ){
99799    return 1;
99800  }
99801  if( aff==SQLITE_AFF_TEXT ){
99802    return idx_affinity==SQLITE_AFF_TEXT;
99803  }
99804  return sqlite3IsNumericAffinity(idx_affinity);
99805}
99806
99807/*
99808** Return the P5 value that should be used for a binary comparison
99809** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
99810*/
99811static u8 binaryCompareP5(
99812  const Expr *pExpr1,   /* Left operand */
99813  const Expr *pExpr2,   /* Right operand */
99814  int jumpIfNull        /* Extra flags added to P5 */
99815){
99816  u8 aff = (char)sqlite3ExprAffinity(pExpr2);
99817  aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
99818  return aff;
99819}
99820
99821/*
99822** Return a pointer to the collation sequence that should be used by
99823** a binary comparison operator comparing pLeft and pRight.
99824**
99825** If the left hand expression has a collating sequence type, then it is
99826** used. Otherwise the collation sequence for the right hand expression
99827** is used, or the default (BINARY) if neither expression has a collating
99828** type.
99829**
99830** Argument pRight (but not pLeft) may be a null pointer. In this case,
99831** it is not considered.
99832*/
99833SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
99834  Parse *pParse,
99835  const Expr *pLeft,
99836  const Expr *pRight
99837){
99838  CollSeq *pColl;
99839  assert( pLeft );
99840  if( pLeft->flags & EP_Collate ){
99841    pColl = sqlite3ExprCollSeq(pParse, pLeft);
99842  }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
99843    pColl = sqlite3ExprCollSeq(pParse, pRight);
99844  }else{
99845    pColl = sqlite3ExprCollSeq(pParse, pLeft);
99846    if( !pColl ){
99847      pColl = sqlite3ExprCollSeq(pParse, pRight);
99848    }
99849  }
99850  return pColl;
99851}
99852
99853/* Expresssion p is a comparison operator.  Return a collation sequence
99854** appropriate for the comparison operator.
99855**
99856** This is normally just a wrapper around sqlite3BinaryCompareCollSeq().
99857** However, if the OP_Commuted flag is set, then the order of the operands
99858** is reversed in the sqlite3BinaryCompareCollSeq() call so that the
99859** correct collating sequence is found.
99860*/
99861SQLITE_PRIVATE CollSeq *sqlite3ExprCompareCollSeq(Parse *pParse, const Expr *p){
99862  if( ExprHasProperty(p, EP_Commuted) ){
99863    return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
99864  }else{
99865    return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
99866  }
99867}
99868
99869/*
99870** Generate code for a comparison operator.
99871*/
99872static int codeCompare(
99873  Parse *pParse,    /* The parsing (and code generating) context */
99874  Expr *pLeft,      /* The left operand */
99875  Expr *pRight,     /* The right operand */
99876  int opcode,       /* The comparison opcode */
99877  int in1, int in2, /* Register holding operands */
99878  int dest,         /* Jump here if true.  */
99879  int jumpIfNull,   /* If true, jump if either operand is NULL */
99880  int isCommuted    /* The comparison has been commuted */
99881){
99882  int p5;
99883  int addr;
99884  CollSeq *p4;
99885
99886  if( pParse->nErr ) return 0;
99887  if( isCommuted ){
99888    p4 = sqlite3BinaryCompareCollSeq(pParse, pRight, pLeft);
99889  }else{
99890    p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
99891  }
99892  p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
99893  addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
99894                           (void*)p4, P4_COLLSEQ);
99895  sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
99896  return addr;
99897}
99898
99899/*
99900** Return true if expression pExpr is a vector, or false otherwise.
99901**
99902** A vector is defined as any expression that results in two or more
99903** columns of result.  Every TK_VECTOR node is an vector because the
99904** parser will not generate a TK_VECTOR with fewer than two entries.
99905** But a TK_SELECT might be either a vector or a scalar. It is only
99906** considered a vector if it has two or more result columns.
99907*/
99908SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr){
99909  return sqlite3ExprVectorSize(pExpr)>1;
99910}
99911
99912/*
99913** If the expression passed as the only argument is of type TK_VECTOR
99914** return the number of expressions in the vector. Or, if the expression
99915** is a sub-select, return the number of columns in the sub-select. For
99916** any other type of expression, return 1.
99917*/
99918SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr){
99919  u8 op = pExpr->op;
99920  if( op==TK_REGISTER ) op = pExpr->op2;
99921  if( op==TK_VECTOR ){
99922    return pExpr->x.pList->nExpr;
99923  }else if( op==TK_SELECT ){
99924    return pExpr->x.pSelect->pEList->nExpr;
99925  }else{
99926    return 1;
99927  }
99928}
99929
99930/*
99931** Return a pointer to a subexpression of pVector that is the i-th
99932** column of the vector (numbered starting with 0).  The caller must
99933** ensure that i is within range.
99934**
99935** If pVector is really a scalar (and "scalar" here includes subqueries
99936** that return a single column!) then return pVector unmodified.
99937**
99938** pVector retains ownership of the returned subexpression.
99939**
99940** If the vector is a (SELECT ...) then the expression returned is
99941** just the expression for the i-th term of the result set, and may
99942** not be ready for evaluation because the table cursor has not yet
99943** been positioned.
99944*/
99945SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){
99946  assert( i<sqlite3ExprVectorSize(pVector) );
99947  if( sqlite3ExprIsVector(pVector) ){
99948    assert( pVector->op2==0 || pVector->op==TK_REGISTER );
99949    if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
99950      return pVector->x.pSelect->pEList->a[i].pExpr;
99951    }else{
99952      return pVector->x.pList->a[i].pExpr;
99953    }
99954  }
99955  return pVector;
99956}
99957
99958/*
99959** Compute and return a new Expr object which when passed to
99960** sqlite3ExprCode() will generate all necessary code to compute
99961** the iField-th column of the vector expression pVector.
99962**
99963** It is ok for pVector to be a scalar (as long as iField==0).
99964** In that case, this routine works like sqlite3ExprDup().
99965**
99966** The caller owns the returned Expr object and is responsible for
99967** ensuring that the returned value eventually gets freed.
99968**
99969** The caller retains ownership of pVector.  If pVector is a TK_SELECT,
99970** then the returned object will reference pVector and so pVector must remain
99971** valid for the life of the returned object.  If pVector is a TK_VECTOR
99972** or a scalar expression, then it can be deleted as soon as this routine
99973** returns.
99974**
99975** A trick to cause a TK_SELECT pVector to be deleted together with
99976** the returned Expr object is to attach the pVector to the pRight field
99977** of the returned TK_SELECT_COLUMN Expr object.
99978*/
99979SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(
99980  Parse *pParse,       /* Parsing context */
99981  Expr *pVector,       /* The vector.  List of expressions or a sub-SELECT */
99982  int iField           /* Which column of the vector to return */
99983){
99984  Expr *pRet;
99985  if( pVector->op==TK_SELECT ){
99986    assert( pVector->flags & EP_xIsSelect );
99987    /* The TK_SELECT_COLUMN Expr node:
99988    **
99989    ** pLeft:           pVector containing TK_SELECT.  Not deleted.
99990    ** pRight:          not used.  But recursively deleted.
99991    ** iColumn:         Index of a column in pVector
99992    ** iTable:          0 or the number of columns on the LHS of an assignment
99993    ** pLeft->iTable:   First in an array of register holding result, or 0
99994    **                  if the result is not yet computed.
99995    **
99996    ** sqlite3ExprDelete() specifically skips the recursive delete of
99997    ** pLeft on TK_SELECT_COLUMN nodes.  But pRight is followed, so pVector
99998    ** can be attached to pRight to cause this node to take ownership of
99999    ** pVector.  Typically there will be multiple TK_SELECT_COLUMN nodes
100000    ** with the same pLeft pointer to the pVector, but only one of them
100001    ** will own the pVector.
100002    */
100003    pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0);
100004    if( pRet ){
100005      pRet->iColumn = iField;
100006      pRet->pLeft = pVector;
100007    }
100008    assert( pRet==0 || pRet->iTable==0 );
100009  }else{
100010    if( pVector->op==TK_VECTOR ) pVector = pVector->x.pList->a[iField].pExpr;
100011    pRet = sqlite3ExprDup(pParse->db, pVector, 0);
100012    sqlite3RenameTokenRemap(pParse, pRet, pVector);
100013  }
100014  return pRet;
100015}
100016
100017/*
100018** If expression pExpr is of type TK_SELECT, generate code to evaluate
100019** it. Return the register in which the result is stored (or, if the
100020** sub-select returns more than one column, the first in an array
100021** of registers in which the result is stored).
100022**
100023** If pExpr is not a TK_SELECT expression, return 0.
100024*/
100025static int exprCodeSubselect(Parse *pParse, Expr *pExpr){
100026  int reg = 0;
100027#ifndef SQLITE_OMIT_SUBQUERY
100028  if( pExpr->op==TK_SELECT ){
100029    reg = sqlite3CodeSubselect(pParse, pExpr);
100030  }
100031#endif
100032  return reg;
100033}
100034
100035/*
100036** Argument pVector points to a vector expression - either a TK_VECTOR
100037** or TK_SELECT that returns more than one column. This function returns
100038** the register number of a register that contains the value of
100039** element iField of the vector.
100040**
100041** If pVector is a TK_SELECT expression, then code for it must have
100042** already been generated using the exprCodeSubselect() routine. In this
100043** case parameter regSelect should be the first in an array of registers
100044** containing the results of the sub-select.
100045**
100046** If pVector is of type TK_VECTOR, then code for the requested field
100047** is generated. In this case (*pRegFree) may be set to the number of
100048** a temporary register to be freed by the caller before returning.
100049**
100050** Before returning, output parameter (*ppExpr) is set to point to the
100051** Expr object corresponding to element iElem of the vector.
100052*/
100053static int exprVectorRegister(
100054  Parse *pParse,                  /* Parse context */
100055  Expr *pVector,                  /* Vector to extract element from */
100056  int iField,                     /* Field to extract from pVector */
100057  int regSelect,                  /* First in array of registers */
100058  Expr **ppExpr,                  /* OUT: Expression element */
100059  int *pRegFree                   /* OUT: Temp register to free */
100060){
100061  u8 op = pVector->op;
100062  assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT );
100063  if( op==TK_REGISTER ){
100064    *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField);
100065    return pVector->iTable+iField;
100066  }
100067  if( op==TK_SELECT ){
100068    *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
100069     return regSelect+iField;
100070  }
100071  *ppExpr = pVector->x.pList->a[iField].pExpr;
100072  return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree);
100073}
100074
100075/*
100076** Expression pExpr is a comparison between two vector values. Compute
100077** the result of the comparison (1, 0, or NULL) and write that
100078** result into register dest.
100079**
100080** The caller must satisfy the following preconditions:
100081**
100082**    if pExpr->op==TK_IS:      op==TK_EQ and p5==SQLITE_NULLEQ
100083**    if pExpr->op==TK_ISNOT:   op==TK_NE and p5==SQLITE_NULLEQ
100084**    otherwise:                op==pExpr->op and p5==0
100085*/
100086static void codeVectorCompare(
100087  Parse *pParse,        /* Code generator context */
100088  Expr *pExpr,          /* The comparison operation */
100089  int dest,             /* Write results into this register */
100090  u8 op,                /* Comparison operator */
100091  u8 p5                 /* SQLITE_NULLEQ or zero */
100092){
100093  Vdbe *v = pParse->pVdbe;
100094  Expr *pLeft = pExpr->pLeft;
100095  Expr *pRight = pExpr->pRight;
100096  int nLeft = sqlite3ExprVectorSize(pLeft);
100097  int i;
100098  int regLeft = 0;
100099  int regRight = 0;
100100  u8 opx = op;
100101  int addrDone = sqlite3VdbeMakeLabel(pParse);
100102  int isCommuted = ExprHasProperty(pExpr,EP_Commuted);
100103
100104  assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
100105  if( pParse->nErr ) return;
100106  if( nLeft!=sqlite3ExprVectorSize(pRight) ){
100107    sqlite3ErrorMsg(pParse, "row value misused");
100108    return;
100109  }
100110  assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
100111       || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
100112       || pExpr->op==TK_LT || pExpr->op==TK_GT
100113       || pExpr->op==TK_LE || pExpr->op==TK_GE
100114  );
100115  assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
100116            || (pExpr->op==TK_ISNOT && op==TK_NE) );
100117  assert( p5==0 || pExpr->op!=op );
100118  assert( p5==SQLITE_NULLEQ || pExpr->op==op );
100119
100120  p5 |= SQLITE_STOREP2;
100121  if( opx==TK_LE ) opx = TK_LT;
100122  if( opx==TK_GE ) opx = TK_GT;
100123
100124  regLeft = exprCodeSubselect(pParse, pLeft);
100125  regRight = exprCodeSubselect(pParse, pRight);
100126
100127  for(i=0; 1 /*Loop exits by "break"*/; i++){
100128    int regFree1 = 0, regFree2 = 0;
100129    Expr *pL, *pR;
100130    int r1, r2;
100131    assert( i>=0 && i<nLeft );
100132    r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, &regFree1);
100133    r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, &regFree2);
100134    codeCompare(pParse, pL, pR, opx, r1, r2, dest, p5, isCommuted);
100135    testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
100136    testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
100137    testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
100138    testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
100139    testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
100140    testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
100141    sqlite3ReleaseTempReg(pParse, regFree1);
100142    sqlite3ReleaseTempReg(pParse, regFree2);
100143    if( i==nLeft-1 ){
100144      break;
100145    }
100146    if( opx==TK_EQ ){
100147      sqlite3VdbeAddOp2(v, OP_IfNot, dest, addrDone); VdbeCoverage(v);
100148      p5 |= SQLITE_KEEPNULL;
100149    }else if( opx==TK_NE ){
100150      sqlite3VdbeAddOp2(v, OP_If, dest, addrDone); VdbeCoverage(v);
100151      p5 |= SQLITE_KEEPNULL;
100152    }else{
100153      assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE );
100154      sqlite3VdbeAddOp2(v, OP_ElseNotEq, 0, addrDone);
100155      VdbeCoverageIf(v, op==TK_LT);
100156      VdbeCoverageIf(v, op==TK_GT);
100157      VdbeCoverageIf(v, op==TK_LE);
100158      VdbeCoverageIf(v, op==TK_GE);
100159      if( i==nLeft-2 ) opx = op;
100160    }
100161  }
100162  sqlite3VdbeResolveLabel(v, addrDone);
100163}
100164
100165#if SQLITE_MAX_EXPR_DEPTH>0
100166/*
100167** Check that argument nHeight is less than or equal to the maximum
100168** expression depth allowed. If it is not, leave an error message in
100169** pParse.
100170*/
100171SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
100172  int rc = SQLITE_OK;
100173  int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
100174  if( nHeight>mxHeight ){
100175    sqlite3ErrorMsg(pParse,
100176       "Expression tree is too large (maximum depth %d)", mxHeight
100177    );
100178    rc = SQLITE_ERROR;
100179  }
100180  return rc;
100181}
100182
100183/* The following three functions, heightOfExpr(), heightOfExprList()
100184** and heightOfSelect(), are used to determine the maximum height
100185** of any expression tree referenced by the structure passed as the
100186** first argument.
100187**
100188** If this maximum height is greater than the current value pointed
100189** to by pnHeight, the second parameter, then set *pnHeight to that
100190** value.
100191*/
100192static void heightOfExpr(Expr *p, int *pnHeight){
100193  if( p ){
100194    if( p->nHeight>*pnHeight ){
100195      *pnHeight = p->nHeight;
100196    }
100197  }
100198}
100199static void heightOfExprList(ExprList *p, int *pnHeight){
100200  if( p ){
100201    int i;
100202    for(i=0; i<p->nExpr; i++){
100203      heightOfExpr(p->a[i].pExpr, pnHeight);
100204    }
100205  }
100206}
100207static void heightOfSelect(Select *pSelect, int *pnHeight){
100208  Select *p;
100209  for(p=pSelect; p; p=p->pPrior){
100210    heightOfExpr(p->pWhere, pnHeight);
100211    heightOfExpr(p->pHaving, pnHeight);
100212    heightOfExpr(p->pLimit, pnHeight);
100213    heightOfExprList(p->pEList, pnHeight);
100214    heightOfExprList(p->pGroupBy, pnHeight);
100215    heightOfExprList(p->pOrderBy, pnHeight);
100216  }
100217}
100218
100219/*
100220** Set the Expr.nHeight variable in the structure passed as an
100221** argument. An expression with no children, Expr.pList or
100222** Expr.pSelect member has a height of 1. Any other expression
100223** has a height equal to the maximum height of any other
100224** referenced Expr plus one.
100225**
100226** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
100227** if appropriate.
100228*/
100229static void exprSetHeight(Expr *p){
100230  int nHeight = 0;
100231  heightOfExpr(p->pLeft, &nHeight);
100232  heightOfExpr(p->pRight, &nHeight);
100233  if( ExprHasProperty(p, EP_xIsSelect) ){
100234    heightOfSelect(p->x.pSelect, &nHeight);
100235  }else if( p->x.pList ){
100236    heightOfExprList(p->x.pList, &nHeight);
100237    p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
100238  }
100239  p->nHeight = nHeight + 1;
100240}
100241
100242/*
100243** Set the Expr.nHeight variable using the exprSetHeight() function. If
100244** the height is greater than the maximum allowed expression depth,
100245** leave an error in pParse.
100246**
100247** Also propagate all EP_Propagate flags from the Expr.x.pList into
100248** Expr.flags.
100249*/
100250SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
100251  if( pParse->nErr ) return;
100252  exprSetHeight(p);
100253  sqlite3ExprCheckHeight(pParse, p->nHeight);
100254}
100255
100256/*
100257** Return the maximum height of any expression tree referenced
100258** by the select statement passed as an argument.
100259*/
100260SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
100261  int nHeight = 0;
100262  heightOfSelect(p, &nHeight);
100263  return nHeight;
100264}
100265#else /* ABOVE:  Height enforcement enabled.  BELOW: Height enforcement off */
100266/*
100267** Propagate all EP_Propagate flags from the Expr.x.pList into
100268** Expr.flags.
100269*/
100270SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
100271  if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){
100272    p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
100273  }
100274}
100275#define exprSetHeight(y)
100276#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
100277
100278/*
100279** This routine is the core allocator for Expr nodes.
100280**
100281** Construct a new expression node and return a pointer to it.  Memory
100282** for this node and for the pToken argument is a single allocation
100283** obtained from sqlite3DbMalloc().  The calling function
100284** is responsible for making sure the node eventually gets freed.
100285**
100286** If dequote is true, then the token (if it exists) is dequoted.
100287** If dequote is false, no dequoting is performed.  The deQuote
100288** parameter is ignored if pToken is NULL or if the token does not
100289** appear to be quoted.  If the quotes were of the form "..." (double-quotes)
100290** then the EP_DblQuoted flag is set on the expression node.
100291**
100292** Special case:  If op==TK_INTEGER and pToken points to a string that
100293** can be translated into a 32-bit integer, then the token is not
100294** stored in u.zToken.  Instead, the integer values is written
100295** into u.iValue and the EP_IntValue flag is set.  No extra storage
100296** is allocated to hold the integer text and the dequote flag is ignored.
100297*/
100298SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
100299  sqlite3 *db,            /* Handle for sqlite3DbMallocRawNN() */
100300  int op,                 /* Expression opcode */
100301  const Token *pToken,    /* Token argument.  Might be NULL */
100302  int dequote             /* True to dequote */
100303){
100304  Expr *pNew;
100305  int nExtra = 0;
100306  int iValue = 0;
100307
100308  assert( db!=0 );
100309  if( pToken ){
100310    if( op!=TK_INTEGER || pToken->z==0
100311          || sqlite3GetInt32(pToken->z, &iValue)==0 ){
100312      nExtra = pToken->n+1;
100313      assert( iValue>=0 );
100314    }
100315  }
100316  pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
100317  if( pNew ){
100318    memset(pNew, 0, sizeof(Expr));
100319    pNew->op = (u8)op;
100320    pNew->iAgg = -1;
100321    if( pToken ){
100322      if( nExtra==0 ){
100323        pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse);
100324        pNew->u.iValue = iValue;
100325      }else{
100326        pNew->u.zToken = (char*)&pNew[1];
100327        assert( pToken->z!=0 || pToken->n==0 );
100328        if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
100329        pNew->u.zToken[pToken->n] = 0;
100330        if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
100331          sqlite3DequoteExpr(pNew);
100332        }
100333      }
100334    }
100335#if SQLITE_MAX_EXPR_DEPTH>0
100336    pNew->nHeight = 1;
100337#endif
100338  }
100339  return pNew;
100340}
100341
100342/*
100343** Allocate a new expression node from a zero-terminated token that has
100344** already been dequoted.
100345*/
100346SQLITE_PRIVATE Expr *sqlite3Expr(
100347  sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
100348  int op,                 /* Expression opcode */
100349  const char *zToken      /* Token argument.  Might be NULL */
100350){
100351  Token x;
100352  x.z = zToken;
100353  x.n = sqlite3Strlen30(zToken);
100354  return sqlite3ExprAlloc(db, op, &x, 0);
100355}
100356
100357/*
100358** Attach subtrees pLeft and pRight to the Expr node pRoot.
100359**
100360** If pRoot==NULL that means that a memory allocation error has occurred.
100361** In that case, delete the subtrees pLeft and pRight.
100362*/
100363SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
100364  sqlite3 *db,
100365  Expr *pRoot,
100366  Expr *pLeft,
100367  Expr *pRight
100368){
100369  if( pRoot==0 ){
100370    assert( db->mallocFailed );
100371    sqlite3ExprDelete(db, pLeft);
100372    sqlite3ExprDelete(db, pRight);
100373  }else{
100374    if( pRight ){
100375      pRoot->pRight = pRight;
100376      pRoot->flags |= EP_Propagate & pRight->flags;
100377    }
100378    if( pLeft ){
100379      pRoot->pLeft = pLeft;
100380      pRoot->flags |= EP_Propagate & pLeft->flags;
100381    }
100382    exprSetHeight(pRoot);
100383  }
100384}
100385
100386/*
100387** Allocate an Expr node which joins as many as two subtrees.
100388**
100389** One or both of the subtrees can be NULL.  Return a pointer to the new
100390** Expr node.  Or, if an OOM error occurs, set pParse->db->mallocFailed,
100391** free the subtrees and return NULL.
100392*/
100393SQLITE_PRIVATE Expr *sqlite3PExpr(
100394  Parse *pParse,          /* Parsing context */
100395  int op,                 /* Expression opcode */
100396  Expr *pLeft,            /* Left operand */
100397  Expr *pRight            /* Right operand */
100398){
100399  Expr *p;
100400  p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
100401  if( p ){
100402    memset(p, 0, sizeof(Expr));
100403    p->op = op & 0xff;
100404    p->iAgg = -1;
100405    sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
100406    sqlite3ExprCheckHeight(pParse, p->nHeight);
100407  }else{
100408    sqlite3ExprDelete(pParse->db, pLeft);
100409    sqlite3ExprDelete(pParse->db, pRight);
100410  }
100411  return p;
100412}
100413
100414/*
100415** Add pSelect to the Expr.x.pSelect field.  Or, if pExpr is NULL (due
100416** do a memory allocation failure) then delete the pSelect object.
100417*/
100418SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){
100419  if( pExpr ){
100420    pExpr->x.pSelect = pSelect;
100421    ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery);
100422    sqlite3ExprSetHeightAndFlags(pParse, pExpr);
100423  }else{
100424    assert( pParse->db->mallocFailed );
100425    sqlite3SelectDelete(pParse->db, pSelect);
100426  }
100427}
100428
100429
100430/*
100431** Join two expressions using an AND operator.  If either expression is
100432** NULL, then just return the other expression.
100433**
100434** If one side or the other of the AND is known to be false, then instead
100435** of returning an AND expression, just return a constant expression with
100436** a value of false.
100437*/
100438SQLITE_PRIVATE Expr *sqlite3ExprAnd(Parse *pParse, Expr *pLeft, Expr *pRight){
100439  sqlite3 *db = pParse->db;
100440  if( pLeft==0  ){
100441    return pRight;
100442  }else if( pRight==0 ){
100443    return pLeft;
100444  }else if( (ExprAlwaysFalse(pLeft) || ExprAlwaysFalse(pRight))
100445         && !IN_RENAME_OBJECT
100446  ){
100447    sqlite3ExprDelete(db, pLeft);
100448    sqlite3ExprDelete(db, pRight);
100449    return sqlite3Expr(db, TK_INTEGER, "0");
100450  }else{
100451    return sqlite3PExpr(pParse, TK_AND, pLeft, pRight);
100452  }
100453}
100454
100455/*
100456** Construct a new expression node for a function with multiple
100457** arguments.
100458*/
100459SQLITE_PRIVATE Expr *sqlite3ExprFunction(
100460  Parse *pParse,        /* Parsing context */
100461  ExprList *pList,      /* Argument list */
100462  Token *pToken,        /* Name of the function */
100463  int eDistinct         /* SF_Distinct or SF_ALL or 0 */
100464){
100465  Expr *pNew;
100466  sqlite3 *db = pParse->db;
100467  assert( pToken );
100468  pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
100469  if( pNew==0 ){
100470    sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
100471    return 0;
100472  }
100473  if( pList && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
100474    sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken);
100475  }
100476  pNew->x.pList = pList;
100477  ExprSetProperty(pNew, EP_HasFunc);
100478  assert( !ExprHasProperty(pNew, EP_xIsSelect) );
100479  sqlite3ExprSetHeightAndFlags(pParse, pNew);
100480  if( eDistinct==SF_Distinct ) ExprSetProperty(pNew, EP_Distinct);
100481  return pNew;
100482}
100483
100484/*
100485** Check to see if a function is usable according to current access
100486** rules:
100487**
100488**    SQLITE_FUNC_DIRECT    -     Only usable from top-level SQL
100489**
100490**    SQLITE_FUNC_UNSAFE    -     Usable if TRUSTED_SCHEMA or from
100491**                                top-level SQL
100492**
100493** If the function is not usable, create an error.
100494*/
100495SQLITE_PRIVATE void sqlite3ExprFunctionUsable(
100496  Parse *pParse,         /* Parsing and code generating context */
100497  Expr *pExpr,           /* The function invocation */
100498  FuncDef *pDef          /* The function being invoked */
100499){
100500  assert( !IN_RENAME_OBJECT );
100501  assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 );
100502  if( ExprHasProperty(pExpr, EP_FromDDL) ){
100503    if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0
100504     || (pParse->db->flags & SQLITE_TrustedSchema)==0
100505    ){
100506      /* Functions prohibited in triggers and views if:
100507      **     (1) tagged with SQLITE_DIRECTONLY
100508      **     (2) not tagged with SQLITE_INNOCUOUS (which means it
100509      **         is tagged with SQLITE_FUNC_UNSAFE) and
100510      **         SQLITE_DBCONFIG_TRUSTED_SCHEMA is off (meaning
100511      **         that the schema is possibly tainted).
100512      */
100513      sqlite3ErrorMsg(pParse, "unsafe use of %s()", pDef->zName);
100514    }
100515  }
100516}
100517
100518/*
100519** Assign a variable number to an expression that encodes a wildcard
100520** in the original SQL statement.
100521**
100522** Wildcards consisting of a single "?" are assigned the next sequential
100523** variable number.
100524**
100525** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
100526** sure "nnn" is not too big to avoid a denial of service attack when
100527** the SQL statement comes from an external source.
100528**
100529** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
100530** as the previous instance of the same wildcard.  Or if this is the first
100531** instance of the wildcard, the next sequential variable number is
100532** assigned.
100533*/
100534SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){
100535  sqlite3 *db = pParse->db;
100536  const char *z;
100537  ynVar x;
100538
100539  if( pExpr==0 ) return;
100540  assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
100541  z = pExpr->u.zToken;
100542  assert( z!=0 );
100543  assert( z[0]!=0 );
100544  assert( n==(u32)sqlite3Strlen30(z) );
100545  if( z[1]==0 ){
100546    /* Wildcard of the form "?".  Assign the next variable number */
100547    assert( z[0]=='?' );
100548    x = (ynVar)(++pParse->nVar);
100549  }else{
100550    int doAdd = 0;
100551    if( z[0]=='?' ){
100552      /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
100553      ** use it as the variable number */
100554      i64 i;
100555      int bOk;
100556      if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
100557        i = z[1]-'0';  /* The common case of ?N for a single digit N */
100558        bOk = 1;
100559      }else{
100560        bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
100561      }
100562      testcase( i==0 );
100563      testcase( i==1 );
100564      testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
100565      testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
100566      if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
100567        sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
100568            db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
100569        return;
100570      }
100571      x = (ynVar)i;
100572      if( x>pParse->nVar ){
100573        pParse->nVar = (int)x;
100574        doAdd = 1;
100575      }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
100576        doAdd = 1;
100577      }
100578    }else{
100579      /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
100580      ** number as the prior appearance of the same name, or if the name
100581      ** has never appeared before, reuse the same variable number
100582      */
100583      x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
100584      if( x==0 ){
100585        x = (ynVar)(++pParse->nVar);
100586        doAdd = 1;
100587      }
100588    }
100589    if( doAdd ){
100590      pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
100591    }
100592  }
100593  pExpr->iColumn = x;
100594  if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
100595    sqlite3ErrorMsg(pParse, "too many SQL variables");
100596  }
100597}
100598
100599/*
100600** Recursively delete an expression tree.
100601*/
100602static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
100603  assert( p!=0 );
100604  /* Sanity check: Assert that the IntValue is non-negative if it exists */
100605  assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
100606
100607  assert( !ExprHasProperty(p, EP_WinFunc) || p->y.pWin!=0 || db->mallocFailed );
100608  assert( p->op!=TK_FUNCTION || ExprHasProperty(p, EP_TokenOnly|EP_Reduced)
100609          || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc) );
100610#ifdef SQLITE_DEBUG
100611  if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){
100612    assert( p->pLeft==0 );
100613    assert( p->pRight==0 );
100614    assert( p->x.pSelect==0 );
100615  }
100616#endif
100617  if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
100618    /* The Expr.x union is never used at the same time as Expr.pRight */
100619    assert( p->x.pList==0 || p->pRight==0 );
100620    if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
100621    if( p->pRight ){
100622      assert( !ExprHasProperty(p, EP_WinFunc) );
100623      sqlite3ExprDeleteNN(db, p->pRight);
100624    }else if( ExprHasProperty(p, EP_xIsSelect) ){
100625      assert( !ExprHasProperty(p, EP_WinFunc) );
100626      sqlite3SelectDelete(db, p->x.pSelect);
100627    }else{
100628      sqlite3ExprListDelete(db, p->x.pList);
100629#ifndef SQLITE_OMIT_WINDOWFUNC
100630      if( ExprHasProperty(p, EP_WinFunc) ){
100631        sqlite3WindowDelete(db, p->y.pWin);
100632      }
100633#endif
100634    }
100635  }
100636  if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
100637  if( !ExprHasProperty(p, EP_Static) ){
100638    sqlite3DbFreeNN(db, p);
100639  }
100640}
100641SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
100642  if( p ) sqlite3ExprDeleteNN(db, p);
100643}
100644
100645/* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the
100646** expression.
100647*/
100648SQLITE_PRIVATE void sqlite3ExprUnmapAndDelete(Parse *pParse, Expr *p){
100649  if( p ){
100650    if( IN_RENAME_OBJECT ){
100651      sqlite3RenameExprUnmap(pParse, p);
100652    }
100653    sqlite3ExprDeleteNN(pParse->db, p);
100654  }
100655}
100656
100657/*
100658** Return the number of bytes allocated for the expression structure
100659** passed as the first argument. This is always one of EXPR_FULLSIZE,
100660** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
100661*/
100662static int exprStructSize(Expr *p){
100663  if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
100664  if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
100665  return EXPR_FULLSIZE;
100666}
100667
100668/*
100669** The dupedExpr*Size() routines each return the number of bytes required
100670** to store a copy of an expression or expression tree.  They differ in
100671** how much of the tree is measured.
100672**
100673**     dupedExprStructSize()     Size of only the Expr structure
100674**     dupedExprNodeSize()       Size of Expr + space for token
100675**     dupedExprSize()           Expr + token + subtree components
100676**
100677***************************************************************************
100678**
100679** The dupedExprStructSize() function returns two values OR-ed together:
100680** (1) the space required for a copy of the Expr structure only and
100681** (2) the EP_xxx flags that indicate what the structure size should be.
100682** The return values is always one of:
100683**
100684**      EXPR_FULLSIZE
100685**      EXPR_REDUCEDSIZE   | EP_Reduced
100686**      EXPR_TOKENONLYSIZE | EP_TokenOnly
100687**
100688** The size of the structure can be found by masking the return value
100689** of this routine with 0xfff.  The flags can be found by masking the
100690** return value with EP_Reduced|EP_TokenOnly.
100691**
100692** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
100693** (unreduced) Expr objects as they or originally constructed by the parser.
100694** During expression analysis, extra information is computed and moved into
100695** later parts of the Expr object and that extra information might get chopped
100696** off if the expression is reduced.  Note also that it does not work to
100697** make an EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
100698** to reduce a pristine expression tree from the parser.  The implementation
100699** of dupedExprStructSize() contain multiple assert() statements that attempt
100700** to enforce this constraint.
100701*/
100702static int dupedExprStructSize(Expr *p, int flags){
100703  int nSize;
100704  assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
100705  assert( EXPR_FULLSIZE<=0xfff );
100706  assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
100707  if( 0==flags || p->op==TK_SELECT_COLUMN
100708#ifndef SQLITE_OMIT_WINDOWFUNC
100709   || ExprHasProperty(p, EP_WinFunc)
100710#endif
100711  ){
100712    nSize = EXPR_FULLSIZE;
100713  }else{
100714    assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
100715    assert( !ExprHasProperty(p, EP_FromJoin) );
100716    assert( !ExprHasProperty(p, EP_MemToken) );
100717    assert( !ExprHasVVAProperty(p, EP_NoReduce) );
100718    if( p->pLeft || p->x.pList ){
100719      nSize = EXPR_REDUCEDSIZE | EP_Reduced;
100720    }else{
100721      assert( p->pRight==0 );
100722      nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
100723    }
100724  }
100725  return nSize;
100726}
100727
100728/*
100729** This function returns the space in bytes required to store the copy
100730** of the Expr structure and a copy of the Expr.u.zToken string (if that
100731** string is defined.)
100732*/
100733static int dupedExprNodeSize(Expr *p, int flags){
100734  int nByte = dupedExprStructSize(p, flags) & 0xfff;
100735  if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
100736    nByte += sqlite3Strlen30NN(p->u.zToken)+1;
100737  }
100738  return ROUND8(nByte);
100739}
100740
100741/*
100742** Return the number of bytes required to create a duplicate of the
100743** expression passed as the first argument. The second argument is a
100744** mask containing EXPRDUP_XXX flags.
100745**
100746** The value returned includes space to create a copy of the Expr struct
100747** itself and the buffer referred to by Expr.u.zToken, if any.
100748**
100749** If the EXPRDUP_REDUCE flag is set, then the return value includes
100750** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
100751** and Expr.pRight variables (but not for any structures pointed to or
100752** descended from the Expr.x.pList or Expr.x.pSelect variables).
100753*/
100754static int dupedExprSize(Expr *p, int flags){
100755  int nByte = 0;
100756  if( p ){
100757    nByte = dupedExprNodeSize(p, flags);
100758    if( flags&EXPRDUP_REDUCE ){
100759      nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
100760    }
100761  }
100762  return nByte;
100763}
100764
100765/*
100766** This function is similar to sqlite3ExprDup(), except that if pzBuffer
100767** is not NULL then *pzBuffer is assumed to point to a buffer large enough
100768** to store the copy of expression p, the copies of p->u.zToken
100769** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
100770** if any. Before returning, *pzBuffer is set to the first byte past the
100771** portion of the buffer copied into by this function.
100772*/
100773static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){
100774  Expr *pNew;           /* Value to return */
100775  u8 *zAlloc;           /* Memory space from which to build Expr object */
100776  u32 staticFlag;       /* EP_Static if space not obtained from malloc */
100777
100778  assert( db!=0 );
100779  assert( p );
100780  assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE );
100781  assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE );
100782
100783  /* Figure out where to write the new Expr structure. */
100784  if( pzBuffer ){
100785    zAlloc = *pzBuffer;
100786    staticFlag = EP_Static;
100787  }else{
100788    zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags));
100789    staticFlag = 0;
100790  }
100791  pNew = (Expr *)zAlloc;
100792
100793  if( pNew ){
100794    /* Set nNewSize to the size allocated for the structure pointed to
100795    ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
100796    ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
100797    ** by the copy of the p->u.zToken string (if any).
100798    */
100799    const unsigned nStructSize = dupedExprStructSize(p, dupFlags);
100800    const int nNewSize = nStructSize & 0xfff;
100801    int nToken;
100802    if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
100803      nToken = sqlite3Strlen30(p->u.zToken) + 1;
100804    }else{
100805      nToken = 0;
100806    }
100807    if( dupFlags ){
100808      assert( ExprHasProperty(p, EP_Reduced)==0 );
100809      memcpy(zAlloc, p, nNewSize);
100810    }else{
100811      u32 nSize = (u32)exprStructSize(p);
100812      memcpy(zAlloc, p, nSize);
100813      if( nSize<EXPR_FULLSIZE ){
100814        memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
100815      }
100816    }
100817
100818    /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
100819    pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
100820    pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
100821    pNew->flags |= staticFlag;
100822    ExprClearVVAProperties(pNew);
100823    if( dupFlags ){
100824      ExprSetVVAProperty(pNew, EP_Immutable);
100825    }
100826
100827    /* Copy the p->u.zToken string, if any. */
100828    if( nToken ){
100829      char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
100830      memcpy(zToken, p->u.zToken, nToken);
100831    }
100832
100833    if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
100834      /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
100835      if( ExprHasProperty(p, EP_xIsSelect) ){
100836        pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
100837      }else{
100838        pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
100839      }
100840    }
100841
100842    /* Fill in pNew->pLeft and pNew->pRight. */
100843    if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc) ){
100844      zAlloc += dupedExprNodeSize(p, dupFlags);
100845      if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){
100846        pNew->pLeft = p->pLeft ?
100847                      exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
100848        pNew->pRight = p->pRight ?
100849                       exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
100850      }
100851#ifndef SQLITE_OMIT_WINDOWFUNC
100852      if( ExprHasProperty(p, EP_WinFunc) ){
100853        pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
100854        assert( ExprHasProperty(pNew, EP_WinFunc) );
100855      }
100856#endif /* SQLITE_OMIT_WINDOWFUNC */
100857      if( pzBuffer ){
100858        *pzBuffer = zAlloc;
100859      }
100860    }else{
100861      if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
100862        if( pNew->op==TK_SELECT_COLUMN ){
100863          pNew->pLeft = p->pLeft;
100864          assert( p->iColumn==0 || p->pRight==0 );
100865          assert( p->pRight==0  || p->pRight==p->pLeft );
100866        }else{
100867          pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
100868        }
100869        pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
100870      }
100871    }
100872  }
100873  return pNew;
100874}
100875
100876/*
100877** Create and return a deep copy of the object passed as the second
100878** argument. If an OOM condition is encountered, NULL is returned
100879** and the db->mallocFailed flag set.
100880*/
100881#ifndef SQLITE_OMIT_CTE
100882static With *withDup(sqlite3 *db, With *p){
100883  With *pRet = 0;
100884  if( p ){
100885    sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
100886    pRet = sqlite3DbMallocZero(db, nByte);
100887    if( pRet ){
100888      int i;
100889      pRet->nCte = p->nCte;
100890      for(i=0; i<p->nCte; i++){
100891        pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
100892        pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
100893        pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
100894      }
100895    }
100896  }
100897  return pRet;
100898}
100899#else
100900# define withDup(x,y) 0
100901#endif
100902
100903#ifndef SQLITE_OMIT_WINDOWFUNC
100904/*
100905** The gatherSelectWindows() procedure and its helper routine
100906** gatherSelectWindowsCallback() are used to scan all the expressions
100907** an a newly duplicated SELECT statement and gather all of the Window
100908** objects found there, assembling them onto the linked list at Select->pWin.
100909*/
100910static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){
100911  if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
100912    Select *pSelect = pWalker->u.pSelect;
100913    Window *pWin = pExpr->y.pWin;
100914    assert( pWin );
100915    assert( IsWindowFunc(pExpr) );
100916    assert( pWin->ppThis==0 );
100917    sqlite3WindowLink(pSelect, pWin);
100918  }
100919  return WRC_Continue;
100920}
100921static int gatherSelectWindowsSelectCallback(Walker *pWalker, Select *p){
100922  return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
100923}
100924static void gatherSelectWindows(Select *p){
100925  Walker w;
100926  w.xExprCallback = gatherSelectWindowsCallback;
100927  w.xSelectCallback = gatherSelectWindowsSelectCallback;
100928  w.xSelectCallback2 = 0;
100929  w.pParse = 0;
100930  w.u.pSelect = p;
100931  sqlite3WalkSelect(&w, p);
100932}
100933#endif
100934
100935
100936/*
100937** The following group of routines make deep copies of expressions,
100938** expression lists, ID lists, and select statements.  The copies can
100939** be deleted (by being passed to their respective ...Delete() routines)
100940** without effecting the originals.
100941**
100942** The expression list, ID, and source lists return by sqlite3ExprListDup(),
100943** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
100944** by subsequent calls to sqlite*ListAppend() routines.
100945**
100946** Any tables that the SrcList might point to are not duplicated.
100947**
100948** The flags parameter contains a combination of the EXPRDUP_XXX flags.
100949** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
100950** truncated version of the usual Expr structure that will be stored as
100951** part of the in-memory representation of the database schema.
100952*/
100953SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
100954  assert( flags==0 || flags==EXPRDUP_REDUCE );
100955  return p ? exprDup(db, p, flags, 0) : 0;
100956}
100957SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
100958  ExprList *pNew;
100959  struct ExprList_item *pItem, *pOldItem;
100960  int i;
100961  Expr *pPriorSelectCol = 0;
100962  assert( db!=0 );
100963  if( p==0 ) return 0;
100964  pNew = sqlite3DbMallocRawNN(db, sqlite3DbMallocSize(db, p));
100965  if( pNew==0 ) return 0;
100966  pNew->nExpr = p->nExpr;
100967  pItem = pNew->a;
100968  pOldItem = p->a;
100969  for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
100970    Expr *pOldExpr = pOldItem->pExpr;
100971    Expr *pNewExpr;
100972    pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
100973    if( pOldExpr
100974     && pOldExpr->op==TK_SELECT_COLUMN
100975     && (pNewExpr = pItem->pExpr)!=0
100976    ){
100977      assert( pNewExpr->iColumn==0 || i>0 );
100978      if( pNewExpr->iColumn==0 ){
100979        assert( pOldExpr->pLeft==pOldExpr->pRight );
100980        pPriorSelectCol = pNewExpr->pLeft = pNewExpr->pRight;
100981      }else{
100982        assert( i>0 );
100983        assert( pItem[-1].pExpr!=0 );
100984        assert( pNewExpr->iColumn==pItem[-1].pExpr->iColumn+1 );
100985        assert( pPriorSelectCol==pItem[-1].pExpr->pLeft );
100986        pNewExpr->pLeft = pPriorSelectCol;
100987      }
100988    }
100989    pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
100990    pItem->sortFlags = pOldItem->sortFlags;
100991    pItem->eEName = pOldItem->eEName;
100992    pItem->done = 0;
100993    pItem->bNulls = pOldItem->bNulls;
100994    pItem->bSorterRef = pOldItem->bSorterRef;
100995    pItem->u = pOldItem->u;
100996  }
100997  return pNew;
100998}
100999
101000/*
101001** If cursors, triggers, views and subqueries are all omitted from
101002** the build, then none of the following routines, except for
101003** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
101004** called with a NULL argument.
101005*/
101006#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
101007 || !defined(SQLITE_OMIT_SUBQUERY)
101008SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
101009  SrcList *pNew;
101010  int i;
101011  int nByte;
101012  assert( db!=0 );
101013  if( p==0 ) return 0;
101014  nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
101015  pNew = sqlite3DbMallocRawNN(db, nByte );
101016  if( pNew==0 ) return 0;
101017  pNew->nSrc = pNew->nAlloc = p->nSrc;
101018  for(i=0; i<p->nSrc; i++){
101019    struct SrcList_item *pNewItem = &pNew->a[i];
101020    struct SrcList_item *pOldItem = &p->a[i];
101021    Table *pTab;
101022    pNewItem->pSchema = pOldItem->pSchema;
101023    pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
101024    pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
101025    pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
101026    pNewItem->fg = pOldItem->fg;
101027    pNewItem->iCursor = pOldItem->iCursor;
101028    pNewItem->addrFillSub = pOldItem->addrFillSub;
101029    pNewItem->regReturn = pOldItem->regReturn;
101030    if( pNewItem->fg.isIndexedBy ){
101031      pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
101032    }
101033    pNewItem->pIBIndex = pOldItem->pIBIndex;
101034    if( pNewItem->fg.isTabFunc ){
101035      pNewItem->u1.pFuncArg =
101036          sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
101037    }
101038    pTab = pNewItem->pTab = pOldItem->pTab;
101039    if( pTab ){
101040      pTab->nTabRef++;
101041    }
101042    pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
101043    pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
101044    pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
101045    pNewItem->colUsed = pOldItem->colUsed;
101046  }
101047  return pNew;
101048}
101049SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
101050  IdList *pNew;
101051  int i;
101052  assert( db!=0 );
101053  if( p==0 ) return 0;
101054  pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
101055  if( pNew==0 ) return 0;
101056  pNew->nId = p->nId;
101057  pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
101058  if( pNew->a==0 ){
101059    sqlite3DbFreeNN(db, pNew);
101060    return 0;
101061  }
101062  /* Note that because the size of the allocation for p->a[] is not
101063  ** necessarily a power of two, sqlite3IdListAppend() may not be called
101064  ** on the duplicate created by this function. */
101065  for(i=0; i<p->nId; i++){
101066    struct IdList_item *pNewItem = &pNew->a[i];
101067    struct IdList_item *pOldItem = &p->a[i];
101068    pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
101069    pNewItem->idx = pOldItem->idx;
101070  }
101071  return pNew;
101072}
101073SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){
101074  Select *pRet = 0;
101075  Select *pNext = 0;
101076  Select **pp = &pRet;
101077  Select *p;
101078
101079  assert( db!=0 );
101080  for(p=pDup; p; p=p->pPrior){
101081    Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
101082    if( pNew==0 ) break;
101083    pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
101084    pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
101085    pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
101086    pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
101087    pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
101088    pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
101089    pNew->op = p->op;
101090    pNew->pNext = pNext;
101091    pNew->pPrior = 0;
101092    pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
101093    pNew->iLimit = 0;
101094    pNew->iOffset = 0;
101095    pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
101096    pNew->addrOpenEphm[0] = -1;
101097    pNew->addrOpenEphm[1] = -1;
101098    pNew->nSelectRow = p->nSelectRow;
101099    pNew->pWith = withDup(db, p->pWith);
101100#ifndef SQLITE_OMIT_WINDOWFUNC
101101    pNew->pWin = 0;
101102    pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
101103    if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
101104#endif
101105    pNew->selId = p->selId;
101106    *pp = pNew;
101107    pp = &pNew->pPrior;
101108    pNext = pNew;
101109  }
101110
101111  return pRet;
101112}
101113#else
101114SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
101115  assert( p==0 );
101116  return 0;
101117}
101118#endif
101119
101120
101121/*
101122** Add a new element to the end of an expression list.  If pList is
101123** initially NULL, then create a new expression list.
101124**
101125** The pList argument must be either NULL or a pointer to an ExprList
101126** obtained from a prior call to sqlite3ExprListAppend().  This routine
101127** may not be used with an ExprList obtained from sqlite3ExprListDup().
101128** Reason:  This routine assumes that the number of slots in pList->a[]
101129** is a power of two.  That is true for sqlite3ExprListAppend() returns
101130** but is not necessarily true from the return value of sqlite3ExprListDup().
101131**
101132** If a memory allocation error occurs, the entire list is freed and
101133** NULL is returned.  If non-NULL is returned, then it is guaranteed
101134** that the new entry was successfully appended.
101135*/
101136SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
101137  Parse *pParse,          /* Parsing context */
101138  ExprList *pList,        /* List to which to append. Might be NULL */
101139  Expr *pExpr             /* Expression to be appended. Might be NULL */
101140){
101141  struct ExprList_item *pItem;
101142  sqlite3 *db = pParse->db;
101143  assert( db!=0 );
101144  if( pList==0 ){
101145    pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) );
101146    if( pList==0 ){
101147      goto no_mem;
101148    }
101149    pList->nExpr = 0;
101150  }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
101151    ExprList *pNew;
101152    pNew = sqlite3DbRealloc(db, pList,
101153         sizeof(*pList)+(2*(sqlite3_int64)pList->nExpr-1)*sizeof(pList->a[0]));
101154    if( pNew==0 ){
101155      goto no_mem;
101156    }
101157    pList = pNew;
101158  }
101159  pItem = &pList->a[pList->nExpr++];
101160  assert( offsetof(struct ExprList_item,zEName)==sizeof(pItem->pExpr) );
101161  assert( offsetof(struct ExprList_item,pExpr)==0 );
101162  memset(&pItem->zEName,0,sizeof(*pItem)-offsetof(struct ExprList_item,zEName));
101163  pItem->pExpr = pExpr;
101164  return pList;
101165
101166no_mem:
101167  /* Avoid leaking memory if malloc has failed. */
101168  sqlite3ExprDelete(db, pExpr);
101169  sqlite3ExprListDelete(db, pList);
101170  return 0;
101171}
101172
101173/*
101174** pColumns and pExpr form a vector assignment which is part of the SET
101175** clause of an UPDATE statement.  Like this:
101176**
101177**        (a,b,c) = (expr1,expr2,expr3)
101178** Or:    (a,b,c) = (SELECT x,y,z FROM ....)
101179**
101180** For each term of the vector assignment, append new entries to the
101181** expression list pList.  In the case of a subquery on the RHS, append
101182** TK_SELECT_COLUMN expressions.
101183*/
101184SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(
101185  Parse *pParse,         /* Parsing context */
101186  ExprList *pList,       /* List to which to append. Might be NULL */
101187  IdList *pColumns,      /* List of names of LHS of the assignment */
101188  Expr *pExpr            /* Vector expression to be appended. Might be NULL */
101189){
101190  sqlite3 *db = pParse->db;
101191  int n;
101192  int i;
101193  int iFirst = pList ? pList->nExpr : 0;
101194  /* pColumns can only be NULL due to an OOM but an OOM will cause an
101195  ** exit prior to this routine being invoked */
101196  if( NEVER(pColumns==0) ) goto vector_append_error;
101197  if( pExpr==0 ) goto vector_append_error;
101198
101199  /* If the RHS is a vector, then we can immediately check to see that
101200  ** the size of the RHS and LHS match.  But if the RHS is a SELECT,
101201  ** wildcards ("*") in the result set of the SELECT must be expanded before
101202  ** we can do the size check, so defer the size check until code generation.
101203  */
101204  if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
101205    sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
101206                    pColumns->nId, n);
101207    goto vector_append_error;
101208  }
101209
101210  for(i=0; i<pColumns->nId; i++){
101211    Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i);
101212    assert( pSubExpr!=0 || db->mallocFailed );
101213    assert( pSubExpr==0 || pSubExpr->iTable==0 );
101214    if( pSubExpr==0 ) continue;
101215    pSubExpr->iTable = pColumns->nId;
101216    pList = sqlite3ExprListAppend(pParse, pList, pSubExpr);
101217    if( pList ){
101218      assert( pList->nExpr==iFirst+i+1 );
101219      pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
101220      pColumns->a[i].zName = 0;
101221    }
101222  }
101223
101224  if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
101225    Expr *pFirst = pList->a[iFirst].pExpr;
101226    assert( pFirst!=0 );
101227    assert( pFirst->op==TK_SELECT_COLUMN );
101228
101229    /* Store the SELECT statement in pRight so it will be deleted when
101230    ** sqlite3ExprListDelete() is called */
101231    pFirst->pRight = pExpr;
101232    pExpr = 0;
101233
101234    /* Remember the size of the LHS in iTable so that we can check that
101235    ** the RHS and LHS sizes match during code generation. */
101236    pFirst->iTable = pColumns->nId;
101237  }
101238
101239vector_append_error:
101240  sqlite3ExprUnmapAndDelete(pParse, pExpr);
101241  sqlite3IdListDelete(db, pColumns);
101242  return pList;
101243}
101244
101245/*
101246** Set the sort order for the last element on the given ExprList.
101247*/
101248SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder, int eNulls){
101249  struct ExprList_item *pItem;
101250  if( p==0 ) return;
101251  assert( p->nExpr>0 );
101252
101253  assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC==0 && SQLITE_SO_DESC>0 );
101254  assert( iSortOrder==SQLITE_SO_UNDEFINED
101255       || iSortOrder==SQLITE_SO_ASC
101256       || iSortOrder==SQLITE_SO_DESC
101257  );
101258  assert( eNulls==SQLITE_SO_UNDEFINED
101259       || eNulls==SQLITE_SO_ASC
101260       || eNulls==SQLITE_SO_DESC
101261  );
101262
101263  pItem = &p->a[p->nExpr-1];
101264  assert( pItem->bNulls==0 );
101265  if( iSortOrder==SQLITE_SO_UNDEFINED ){
101266    iSortOrder = SQLITE_SO_ASC;
101267  }
101268  pItem->sortFlags = (u8)iSortOrder;
101269
101270  if( eNulls!=SQLITE_SO_UNDEFINED ){
101271    pItem->bNulls = 1;
101272    if( iSortOrder!=eNulls ){
101273      pItem->sortFlags |= KEYINFO_ORDER_BIGNULL;
101274    }
101275  }
101276}
101277
101278/*
101279** Set the ExprList.a[].zEName element of the most recently added item
101280** on the expression list.
101281**
101282** pList might be NULL following an OOM error.  But pName should never be
101283** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
101284** is set.
101285*/
101286SQLITE_PRIVATE void sqlite3ExprListSetName(
101287  Parse *pParse,          /* Parsing context */
101288  ExprList *pList,        /* List to which to add the span. */
101289  Token *pName,           /* Name to be added */
101290  int dequote             /* True to cause the name to be dequoted */
101291){
101292  assert( pList!=0 || pParse->db->mallocFailed!=0 );
101293  assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 );
101294  if( pList ){
101295    struct ExprList_item *pItem;
101296    assert( pList->nExpr>0 );
101297    pItem = &pList->a[pList->nExpr-1];
101298    assert( pItem->zEName==0 );
101299    assert( pItem->eEName==ENAME_NAME );
101300    pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
101301    if( dequote ){
101302      /* If dequote==0, then pName->z does not point to part of a DDL
101303      ** statement handled by the parser. And so no token need be added
101304      ** to the token-map.  */
101305      sqlite3Dequote(pItem->zEName);
101306      if( IN_RENAME_OBJECT ){
101307        sqlite3RenameTokenMap(pParse, (void*)pItem->zEName, pName);
101308      }
101309    }
101310  }
101311}
101312
101313/*
101314** Set the ExprList.a[].zSpan element of the most recently added item
101315** on the expression list.
101316**
101317** pList might be NULL following an OOM error.  But pSpan should never be
101318** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
101319** is set.
101320*/
101321SQLITE_PRIVATE void sqlite3ExprListSetSpan(
101322  Parse *pParse,          /* Parsing context */
101323  ExprList *pList,        /* List to which to add the span. */
101324  const char *zStart,     /* Start of the span */
101325  const char *zEnd        /* End of the span */
101326){
101327  sqlite3 *db = pParse->db;
101328  assert( pList!=0 || db->mallocFailed!=0 );
101329  if( pList ){
101330    struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
101331    assert( pList->nExpr>0 );
101332    if( pItem->zEName==0 ){
101333      pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
101334      pItem->eEName = ENAME_SPAN;
101335    }
101336  }
101337}
101338
101339/*
101340** If the expression list pEList contains more than iLimit elements,
101341** leave an error message in pParse.
101342*/
101343SQLITE_PRIVATE void sqlite3ExprListCheckLength(
101344  Parse *pParse,
101345  ExprList *pEList,
101346  const char *zObject
101347){
101348  int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
101349  testcase( pEList && pEList->nExpr==mx );
101350  testcase( pEList && pEList->nExpr==mx+1 );
101351  if( pEList && pEList->nExpr>mx ){
101352    sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
101353  }
101354}
101355
101356/*
101357** Delete an entire expression list.
101358*/
101359static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){
101360  int i = pList->nExpr;
101361  struct ExprList_item *pItem =  pList->a;
101362  assert( pList->nExpr>0 );
101363  do{
101364    sqlite3ExprDelete(db, pItem->pExpr);
101365    sqlite3DbFree(db, pItem->zEName);
101366    pItem++;
101367  }while( --i>0 );
101368  sqlite3DbFreeNN(db, pList);
101369}
101370SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
101371  if( pList ) exprListDeleteNN(db, pList);
101372}
101373
101374/*
101375** Return the bitwise-OR of all Expr.flags fields in the given
101376** ExprList.
101377*/
101378SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList *pList){
101379  int i;
101380  u32 m = 0;
101381  assert( pList!=0 );
101382  for(i=0; i<pList->nExpr; i++){
101383     Expr *pExpr = pList->a[i].pExpr;
101384     assert( pExpr!=0 );
101385     m |= pExpr->flags;
101386  }
101387  return m;
101388}
101389
101390/*
101391** This is a SELECT-node callback for the expression walker that
101392** always "fails".  By "fail" in this case, we mean set
101393** pWalker->eCode to zero and abort.
101394**
101395** This callback is used by multiple expression walkers.
101396*/
101397SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
101398  UNUSED_PARAMETER(NotUsed);
101399  pWalker->eCode = 0;
101400  return WRC_Abort;
101401}
101402
101403/*
101404** Check the input string to see if it is "true" or "false" (in any case).
101405**
101406**       If the string is....           Return
101407**         "true"                         EP_IsTrue
101408**         "false"                        EP_IsFalse
101409**         anything else                  0
101410*/
101411SQLITE_PRIVATE u32 sqlite3IsTrueOrFalse(const char *zIn){
101412  if( sqlite3StrICmp(zIn, "true")==0  ) return EP_IsTrue;
101413  if( sqlite3StrICmp(zIn, "false")==0 ) return EP_IsFalse;
101414  return 0;
101415}
101416
101417
101418/*
101419** If the input expression is an ID with the name "true" or "false"
101420** then convert it into an TK_TRUEFALSE term.  Return non-zero if
101421** the conversion happened, and zero if the expression is unaltered.
101422*/
101423SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr *pExpr){
101424  u32 v;
101425  assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
101426  if( !ExprHasProperty(pExpr, EP_Quoted)
101427   && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0
101428  ){
101429    pExpr->op = TK_TRUEFALSE;
101430    ExprSetProperty(pExpr, v);
101431    return 1;
101432  }
101433  return 0;
101434}
101435
101436/*
101437** The argument must be a TK_TRUEFALSE Expr node.  Return 1 if it is TRUE
101438** and 0 if it is FALSE.
101439*/
101440SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr *pExpr){
101441  pExpr = sqlite3ExprSkipCollate((Expr*)pExpr);
101442  assert( pExpr->op==TK_TRUEFALSE );
101443  assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
101444       || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
101445  return pExpr->u.zToken[4]==0;
101446}
101447
101448/*
101449** If pExpr is an AND or OR expression, try to simplify it by eliminating
101450** terms that are always true or false.  Return the simplified expression.
101451** Or return the original expression if no simplification is possible.
101452**
101453** Examples:
101454**
101455**     (x<10) AND true                =>   (x<10)
101456**     (x<10) AND false               =>   false
101457**     (x<10) AND (y=22 OR false)     =>   (x<10) AND (y=22)
101458**     (x<10) AND (y=22 OR true)      =>   (x<10)
101459**     (y=22) OR true                 =>   true
101460*/
101461SQLITE_PRIVATE Expr *sqlite3ExprSimplifiedAndOr(Expr *pExpr){
101462  assert( pExpr!=0 );
101463  if( pExpr->op==TK_AND || pExpr->op==TK_OR ){
101464    Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
101465    Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
101466    if( ExprAlwaysTrue(pLeft) || ExprAlwaysFalse(pRight) ){
101467      pExpr = pExpr->op==TK_AND ? pRight : pLeft;
101468    }else if( ExprAlwaysTrue(pRight) || ExprAlwaysFalse(pLeft) ){
101469      pExpr = pExpr->op==TK_AND ? pLeft : pRight;
101470    }
101471  }
101472  return pExpr;
101473}
101474
101475
101476/*
101477** These routines are Walker callbacks used to check expressions to
101478** see if they are "constant" for some definition of constant.  The
101479** Walker.eCode value determines the type of "constant" we are looking
101480** for.
101481**
101482** These callback routines are used to implement the following:
101483**
101484**     sqlite3ExprIsConstant()                  pWalker->eCode==1
101485**     sqlite3ExprIsConstantNotJoin()           pWalker->eCode==2
101486**     sqlite3ExprIsTableConstant()             pWalker->eCode==3
101487**     sqlite3ExprIsConstantOrFunction()        pWalker->eCode==4 or 5
101488**
101489** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
101490** is found to not be a constant.
101491**
101492** The sqlite3ExprIsConstantOrFunction() is used for evaluating DEFAULT
101493** expressions in a CREATE TABLE statement.  The Walker.eCode value is 5
101494** when parsing an existing schema out of the sqlite_master table and 4
101495** when processing a new CREATE TABLE statement.  A bound parameter raises
101496** an error for new statements, but is silently converted
101497** to NULL for existing schemas.  This allows sqlite_master tables that
101498** contain a bound parameter because they were generated by older versions
101499** of SQLite to be parsed by newer versions of SQLite without raising a
101500** malformed schema error.
101501*/
101502static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
101503
101504  /* If pWalker->eCode is 2 then any term of the expression that comes from
101505  ** the ON or USING clauses of a left join disqualifies the expression
101506  ** from being considered constant. */
101507  if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
101508    pWalker->eCode = 0;
101509    return WRC_Abort;
101510  }
101511
101512  switch( pExpr->op ){
101513    /* Consider functions to be constant if all their arguments are constant
101514    ** and either pWalker->eCode==4 or 5 or the function has the
101515    ** SQLITE_FUNC_CONST flag. */
101516    case TK_FUNCTION:
101517      if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc))
101518       && !ExprHasProperty(pExpr, EP_WinFunc)
101519      ){
101520        if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL);
101521        return WRC_Continue;
101522      }else{
101523        pWalker->eCode = 0;
101524        return WRC_Abort;
101525      }
101526    case TK_ID:
101527      /* Convert "true" or "false" in a DEFAULT clause into the
101528      ** appropriate TK_TRUEFALSE operator */
101529      if( sqlite3ExprIdToTrueFalse(pExpr) ){
101530        return WRC_Prune;
101531      }
101532      /* Fall thru */
101533    case TK_COLUMN:
101534    case TK_AGG_FUNCTION:
101535    case TK_AGG_COLUMN:
101536      testcase( pExpr->op==TK_ID );
101537      testcase( pExpr->op==TK_COLUMN );
101538      testcase( pExpr->op==TK_AGG_FUNCTION );
101539      testcase( pExpr->op==TK_AGG_COLUMN );
101540      if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
101541        return WRC_Continue;
101542      }
101543      if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
101544        return WRC_Continue;
101545      }
101546      /* Fall through */
101547    case TK_IF_NULL_ROW:
101548    case TK_REGISTER:
101549    case TK_DOT:
101550      testcase( pExpr->op==TK_REGISTER );
101551      testcase( pExpr->op==TK_IF_NULL_ROW );
101552      testcase( pExpr->op==TK_DOT );
101553      pWalker->eCode = 0;
101554      return WRC_Abort;
101555    case TK_VARIABLE:
101556      if( pWalker->eCode==5 ){
101557        /* Silently convert bound parameters that appear inside of CREATE
101558        ** statements into a NULL when parsing the CREATE statement text out
101559        ** of the sqlite_master table */
101560        pExpr->op = TK_NULL;
101561      }else if( pWalker->eCode==4 ){
101562        /* A bound parameter in a CREATE statement that originates from
101563        ** sqlite3_prepare() causes an error */
101564        pWalker->eCode = 0;
101565        return WRC_Abort;
101566      }
101567      /* Fall through */
101568    default:
101569      testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
101570      testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
101571      return WRC_Continue;
101572  }
101573}
101574static int exprIsConst(Expr *p, int initFlag, int iCur){
101575  Walker w;
101576  w.eCode = initFlag;
101577  w.xExprCallback = exprNodeIsConstant;
101578  w.xSelectCallback = sqlite3SelectWalkFail;
101579#ifdef SQLITE_DEBUG
101580  w.xSelectCallback2 = sqlite3SelectWalkAssert2;
101581#endif
101582  w.u.iCur = iCur;
101583  sqlite3WalkExpr(&w, p);
101584  return w.eCode;
101585}
101586
101587/*
101588** Walk an expression tree.  Return non-zero if the expression is constant
101589** and 0 if it involves variables or function calls.
101590**
101591** For the purposes of this function, a double-quoted string (ex: "abc")
101592** is considered a variable but a single-quoted string (ex: 'abc') is
101593** a constant.
101594*/
101595SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
101596  return exprIsConst(p, 1, 0);
101597}
101598
101599/*
101600** Walk an expression tree.  Return non-zero if
101601**
101602**   (1) the expression is constant, and
101603**   (2) the expression does originate in the ON or USING clause
101604**       of a LEFT JOIN, and
101605**   (3) the expression does not contain any EP_FixedCol TK_COLUMN
101606**       operands created by the constant propagation optimization.
101607**
101608** When this routine returns true, it indicates that the expression
101609** can be added to the pParse->pConstExpr list and evaluated once when
101610** the prepared statement starts up.  See sqlite3ExprCodeRunJustOnce().
101611*/
101612SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
101613  return exprIsConst(p, 2, 0);
101614}
101615
101616/*
101617** Walk an expression tree.  Return non-zero if the expression is constant
101618** for any single row of the table with cursor iCur.  In other words, the
101619** expression must not refer to any non-deterministic function nor any
101620** table other than iCur.
101621*/
101622SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
101623  return exprIsConst(p, 3, iCur);
101624}
101625
101626
101627/*
101628** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy().
101629*/
101630static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){
101631  ExprList *pGroupBy = pWalker->u.pGroupBy;
101632  int i;
101633
101634  /* Check if pExpr is identical to any GROUP BY term. If so, consider
101635  ** it constant.  */
101636  for(i=0; i<pGroupBy->nExpr; i++){
101637    Expr *p = pGroupBy->a[i].pExpr;
101638    if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
101639      CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
101640      if( sqlite3IsBinary(pColl) ){
101641        return WRC_Prune;
101642      }
101643    }
101644  }
101645
101646  /* Check if pExpr is a sub-select. If so, consider it variable. */
101647  if( ExprHasProperty(pExpr, EP_xIsSelect) ){
101648    pWalker->eCode = 0;
101649    return WRC_Abort;
101650  }
101651
101652  return exprNodeIsConstant(pWalker, pExpr);
101653}
101654
101655/*
101656** Walk the expression tree passed as the first argument. Return non-zero
101657** if the expression consists entirely of constants or copies of terms
101658** in pGroupBy that sort with the BINARY collation sequence.
101659**
101660** This routine is used to determine if a term of the HAVING clause can
101661** be promoted into the WHERE clause.  In order for such a promotion to work,
101662** the value of the HAVING clause term must be the same for all members of
101663** a "group".  The requirement that the GROUP BY term must be BINARY
101664** assumes that no other collating sequence will have a finer-grained
101665** grouping than binary.  In other words (A=B COLLATE binary) implies
101666** A=B in every other collating sequence.  The requirement that the
101667** GROUP BY be BINARY is stricter than necessary.  It would also work
101668** to promote HAVING clauses that use the same alternative collating
101669** sequence as the GROUP BY term, but that is much harder to check,
101670** alternative collating sequences are uncommon, and this is only an
101671** optimization, so we take the easy way out and simply require the
101672** GROUP BY to use the BINARY collating sequence.
101673*/
101674SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){
101675  Walker w;
101676  w.eCode = 1;
101677  w.xExprCallback = exprNodeIsConstantOrGroupBy;
101678  w.xSelectCallback = 0;
101679  w.u.pGroupBy = pGroupBy;
101680  w.pParse = pParse;
101681  sqlite3WalkExpr(&w, p);
101682  return w.eCode;
101683}
101684
101685/*
101686** Walk an expression tree for the DEFAULT field of a column definition
101687** in a CREATE TABLE statement.  Return non-zero if the expression is
101688** acceptable for use as a DEFAULT.  That is to say, return non-zero if
101689** the expression is constant or a function call with constant arguments.
101690** Return and 0 if there are any variables.
101691**
101692** isInit is true when parsing from sqlite_master.  isInit is false when
101693** processing a new CREATE TABLE statement.  When isInit is true, parameters
101694** (such as ? or $abc) in the expression are converted into NULL.  When
101695** isInit is false, parameters raise an error.  Parameters should not be
101696** allowed in a CREATE TABLE statement, but some legacy versions of SQLite
101697** allowed it, so we need to support it when reading sqlite_master for
101698** backwards compatibility.
101699**
101700** If isInit is true, set EP_FromDDL on every TK_FUNCTION node.
101701**
101702** For the purposes of this function, a double-quoted string (ex: "abc")
101703** is considered a variable but a single-quoted string (ex: 'abc') is
101704** a constant.
101705*/
101706SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
101707  assert( isInit==0 || isInit==1 );
101708  return exprIsConst(p, 4+isInit, 0);
101709}
101710
101711#ifdef SQLITE_ENABLE_CURSOR_HINTS
101712/*
101713** Walk an expression tree.  Return 1 if the expression contains a
101714** subquery of some kind.  Return 0 if there are no subqueries.
101715*/
101716SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr *p){
101717  Walker w;
101718  w.eCode = 1;
101719  w.xExprCallback = sqlite3ExprWalkNoop;
101720  w.xSelectCallback = sqlite3SelectWalkFail;
101721#ifdef SQLITE_DEBUG
101722  w.xSelectCallback2 = sqlite3SelectWalkAssert2;
101723#endif
101724  sqlite3WalkExpr(&w, p);
101725  return w.eCode==0;
101726}
101727#endif
101728
101729/*
101730** If the expression p codes a constant integer that is small enough
101731** to fit in a 32-bit integer, return 1 and put the value of the integer
101732** in *pValue.  If the expression is not an integer or if it is too big
101733** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
101734*/
101735SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
101736  int rc = 0;
101737  if( NEVER(p==0) ) return 0;  /* Used to only happen following on OOM */
101738
101739  /* If an expression is an integer literal that fits in a signed 32-bit
101740  ** integer, then the EP_IntValue flag will have already been set */
101741  assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
101742           || sqlite3GetInt32(p->u.zToken, &rc)==0 );
101743
101744  if( p->flags & EP_IntValue ){
101745    *pValue = p->u.iValue;
101746    return 1;
101747  }
101748  switch( p->op ){
101749    case TK_UPLUS: {
101750      rc = sqlite3ExprIsInteger(p->pLeft, pValue);
101751      break;
101752    }
101753    case TK_UMINUS: {
101754      int v;
101755      if( sqlite3ExprIsInteger(p->pLeft, &v) ){
101756        assert( v!=(-2147483647-1) );
101757        *pValue = -v;
101758        rc = 1;
101759      }
101760      break;
101761    }
101762    default: break;
101763  }
101764  return rc;
101765}
101766
101767/*
101768** Return FALSE if there is no chance that the expression can be NULL.
101769**
101770** If the expression might be NULL or if the expression is too complex
101771** to tell return TRUE.
101772**
101773** This routine is used as an optimization, to skip OP_IsNull opcodes
101774** when we know that a value cannot be NULL.  Hence, a false positive
101775** (returning TRUE when in fact the expression can never be NULL) might
101776** be a small performance hit but is otherwise harmless.  On the other
101777** hand, a false negative (returning FALSE when the result could be NULL)
101778** will likely result in an incorrect answer.  So when in doubt, return
101779** TRUE.
101780*/
101781SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
101782  u8 op;
101783  while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
101784    p = p->pLeft;
101785  }
101786  op = p->op;
101787  if( op==TK_REGISTER ) op = p->op2;
101788  switch( op ){
101789    case TK_INTEGER:
101790    case TK_STRING:
101791    case TK_FLOAT:
101792    case TK_BLOB:
101793      return 0;
101794    case TK_COLUMN:
101795      return ExprHasProperty(p, EP_CanBeNull) ||
101796             p->y.pTab==0 ||  /* Reference to column of index on expression */
101797             (p->iColumn>=0
101798              && ALWAYS(p->y.pTab->aCol!=0) /* Defense against OOM problems */
101799              && p->y.pTab->aCol[p->iColumn].notNull==0);
101800    default:
101801      return 1;
101802  }
101803}
101804
101805/*
101806** Return TRUE if the given expression is a constant which would be
101807** unchanged by OP_Affinity with the affinity given in the second
101808** argument.
101809**
101810** This routine is used to determine if the OP_Affinity operation
101811** can be omitted.  When in doubt return FALSE.  A false negative
101812** is harmless.  A false positive, however, can result in the wrong
101813** answer.
101814*/
101815SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
101816  u8 op;
101817  int unaryMinus = 0;
101818  if( aff==SQLITE_AFF_BLOB ) return 1;
101819  while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
101820    if( p->op==TK_UMINUS ) unaryMinus = 1;
101821    p = p->pLeft;
101822  }
101823  op = p->op;
101824  if( op==TK_REGISTER ) op = p->op2;
101825  switch( op ){
101826    case TK_INTEGER: {
101827      return aff>=SQLITE_AFF_NUMERIC;
101828    }
101829    case TK_FLOAT: {
101830      return aff>=SQLITE_AFF_NUMERIC;
101831    }
101832    case TK_STRING: {
101833      return !unaryMinus && aff==SQLITE_AFF_TEXT;
101834    }
101835    case TK_BLOB: {
101836      return !unaryMinus;
101837    }
101838    case TK_COLUMN: {
101839      assert( p->iTable>=0 );  /* p cannot be part of a CHECK constraint */
101840      return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0;
101841    }
101842    default: {
101843      return 0;
101844    }
101845  }
101846}
101847
101848/*
101849** Return TRUE if the given string is a row-id column name.
101850*/
101851SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
101852  if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
101853  if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
101854  if( sqlite3StrICmp(z, "OID")==0 ) return 1;
101855  return 0;
101856}
101857
101858/*
101859** pX is the RHS of an IN operator.  If pX is a SELECT statement
101860** that can be simplified to a direct table access, then return
101861** a pointer to the SELECT statement.  If pX is not a SELECT statement,
101862** or if the SELECT statement needs to be manifested into a transient
101863** table, then return NULL.
101864*/
101865#ifndef SQLITE_OMIT_SUBQUERY
101866static Select *isCandidateForInOpt(Expr *pX){
101867  Select *p;
101868  SrcList *pSrc;
101869  ExprList *pEList;
101870  Table *pTab;
101871  int i;
101872  if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0;  /* Not a subquery */
101873  if( ExprHasProperty(pX, EP_VarSelect)  ) return 0;  /* Correlated subq */
101874  p = pX->x.pSelect;
101875  if( p->pPrior ) return 0;              /* Not a compound SELECT */
101876  if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
101877    testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
101878    testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
101879    return 0; /* No DISTINCT keyword and no aggregate functions */
101880  }
101881  assert( p->pGroupBy==0 );              /* Has no GROUP BY clause */
101882  if( p->pLimit ) return 0;              /* Has no LIMIT clause */
101883  if( p->pWhere ) return 0;              /* Has no WHERE clause */
101884  pSrc = p->pSrc;
101885  assert( pSrc!=0 );
101886  if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
101887  if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
101888  pTab = pSrc->a[0].pTab;
101889  assert( pTab!=0 );
101890  assert( pTab->pSelect==0 );            /* FROM clause is not a view */
101891  if( IsVirtual(pTab) ) return 0;        /* FROM clause not a virtual table */
101892  pEList = p->pEList;
101893  assert( pEList!=0 );
101894  /* All SELECT results must be columns. */
101895  for(i=0; i<pEList->nExpr; i++){
101896    Expr *pRes = pEList->a[i].pExpr;
101897    if( pRes->op!=TK_COLUMN ) return 0;
101898    assert( pRes->iTable==pSrc->a[0].iCursor );  /* Not a correlated subquery */
101899  }
101900  return p;
101901}
101902#endif /* SQLITE_OMIT_SUBQUERY */
101903
101904#ifndef SQLITE_OMIT_SUBQUERY
101905/*
101906** Generate code that checks the left-most column of index table iCur to see if
101907** it contains any NULL entries.  Cause the register at regHasNull to be set
101908** to a non-NULL value if iCur contains no NULLs.  Cause register regHasNull
101909** to be set to NULL if iCur contains one or more NULL values.
101910*/
101911static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
101912  int addr1;
101913  sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
101914  addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
101915  sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
101916  sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
101917  VdbeComment((v, "first_entry_in(%d)", iCur));
101918  sqlite3VdbeJumpHere(v, addr1);
101919}
101920#endif
101921
101922
101923#ifndef SQLITE_OMIT_SUBQUERY
101924/*
101925** The argument is an IN operator with a list (not a subquery) on the
101926** right-hand side.  Return TRUE if that list is constant.
101927*/
101928static int sqlite3InRhsIsConstant(Expr *pIn){
101929  Expr *pLHS;
101930  int res;
101931  assert( !ExprHasProperty(pIn, EP_xIsSelect) );
101932  pLHS = pIn->pLeft;
101933  pIn->pLeft = 0;
101934  res = sqlite3ExprIsConstant(pIn);
101935  pIn->pLeft = pLHS;
101936  return res;
101937}
101938#endif
101939
101940/*
101941** This function is used by the implementation of the IN (...) operator.
101942** The pX parameter is the expression on the RHS of the IN operator, which
101943** might be either a list of expressions or a subquery.
101944**
101945** The job of this routine is to find or create a b-tree object that can
101946** be used either to test for membership in the RHS set or to iterate through
101947** all members of the RHS set, skipping duplicates.
101948**
101949** A cursor is opened on the b-tree object that is the RHS of the IN operator
101950** and pX->iTable is set to the index of that cursor.
101951**
101952** The returned value of this function indicates the b-tree type, as follows:
101953**
101954**   IN_INDEX_ROWID      - The cursor was opened on a database table.
101955**   IN_INDEX_INDEX_ASC  - The cursor was opened on an ascending index.
101956**   IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
101957**   IN_INDEX_EPH        - The cursor was opened on a specially created and
101958**                         populated epheremal table.
101959**   IN_INDEX_NOOP       - No cursor was allocated.  The IN operator must be
101960**                         implemented as a sequence of comparisons.
101961**
101962** An existing b-tree might be used if the RHS expression pX is a simple
101963** subquery such as:
101964**
101965**     SELECT <column1>, <column2>... FROM <table>
101966**
101967** If the RHS of the IN operator is a list or a more complex subquery, then
101968** an ephemeral table might need to be generated from the RHS and then
101969** pX->iTable made to point to the ephemeral table instead of an
101970** existing table.
101971**
101972** The inFlags parameter must contain, at a minimum, one of the bits
101973** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both.  If inFlags contains
101974** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast
101975** membership test.  When the IN_INDEX_LOOP bit is set, the IN index will
101976** be used to loop over all values of the RHS of the IN operator.
101977**
101978** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
101979** through the set members) then the b-tree must not contain duplicates.
101980** An epheremal table will be created unless the selected columns are guaranteed
101981** to be unique - either because it is an INTEGER PRIMARY KEY or due to
101982** a UNIQUE constraint or index.
101983**
101984** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
101985** for fast set membership tests) then an epheremal table must
101986** be used unless <columns> is a single INTEGER PRIMARY KEY column or an
101987** index can be found with the specified <columns> as its left-most.
101988**
101989** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
101990** if the RHS of the IN operator is a list (not a subquery) then this
101991** routine might decide that creating an ephemeral b-tree for membership
101992** testing is too expensive and return IN_INDEX_NOOP.  In that case, the
101993** calling routine should implement the IN operator using a sequence
101994** of Eq or Ne comparison operations.
101995**
101996** When the b-tree is being used for membership tests, the calling function
101997** might need to know whether or not the RHS side of the IN operator
101998** contains a NULL.  If prRhsHasNull is not a NULL pointer and
101999** if there is any chance that the (...) might contain a NULL value at
102000** runtime, then a register is allocated and the register number written
102001** to *prRhsHasNull. If there is no chance that the (...) contains a
102002** NULL value, then *prRhsHasNull is left unchanged.
102003**
102004** If a register is allocated and its location stored in *prRhsHasNull, then
102005** the value in that register will be NULL if the b-tree contains one or more
102006** NULL values, and it will be some non-NULL value if the b-tree contains no
102007** NULL values.
102008**
102009** If the aiMap parameter is not NULL, it must point to an array containing
102010** one element for each column returned by the SELECT statement on the RHS
102011** of the IN(...) operator. The i'th entry of the array is populated with the
102012** offset of the index column that matches the i'th column returned by the
102013** SELECT. For example, if the expression and selected index are:
102014**
102015**   (?,?,?) IN (SELECT a, b, c FROM t1)
102016**   CREATE INDEX i1 ON t1(b, c, a);
102017**
102018** then aiMap[] is populated with {2, 0, 1}.
102019*/
102020#ifndef SQLITE_OMIT_SUBQUERY
102021SQLITE_PRIVATE int sqlite3FindInIndex(
102022  Parse *pParse,             /* Parsing context */
102023  Expr *pX,                  /* The IN expression */
102024  u32 inFlags,               /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */
102025  int *prRhsHasNull,         /* Register holding NULL status.  See notes */
102026  int *aiMap,                /* Mapping from Index fields to RHS fields */
102027  int *piTab                 /* OUT: index to use */
102028){
102029  Select *p;                            /* SELECT to the right of IN operator */
102030  int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
102031  int iTab = pParse->nTab++;            /* Cursor of the RHS table */
102032  int mustBeUnique;                     /* True if RHS must be unique */
102033  Vdbe *v = sqlite3GetVdbe(pParse);     /* Virtual machine being coded */
102034
102035  assert( pX->op==TK_IN );
102036  mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
102037
102038  /* If the RHS of this IN(...) operator is a SELECT, and if it matters
102039  ** whether or not the SELECT result contains NULL values, check whether
102040  ** or not NULL is actually possible (it may not be, for example, due
102041  ** to NOT NULL constraints in the schema). If no NULL values are possible,
102042  ** set prRhsHasNull to 0 before continuing.  */
102043  if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){
102044    int i;
102045    ExprList *pEList = pX->x.pSelect->pEList;
102046    for(i=0; i<pEList->nExpr; i++){
102047      if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
102048    }
102049    if( i==pEList->nExpr ){
102050      prRhsHasNull = 0;
102051    }
102052  }
102053
102054  /* Check to see if an existing table or index can be used to
102055  ** satisfy the query.  This is preferable to generating a new
102056  ** ephemeral table.  */
102057  if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
102058    sqlite3 *db = pParse->db;              /* Database connection */
102059    Table *pTab;                           /* Table <table>. */
102060    i16 iDb;                               /* Database idx for pTab */
102061    ExprList *pEList = p->pEList;
102062    int nExpr = pEList->nExpr;
102063
102064    assert( p->pEList!=0 );             /* Because of isCandidateForInOpt(p) */
102065    assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
102066    assert( p->pSrc!=0 );               /* Because of isCandidateForInOpt(p) */
102067    pTab = p->pSrc->a[0].pTab;
102068
102069    /* Code an OP_Transaction and OP_TableLock for <table>. */
102070    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
102071    sqlite3CodeVerifySchema(pParse, iDb);
102072    sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
102073
102074    assert(v);  /* sqlite3GetVdbe() has always been previously called */
102075    if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
102076      /* The "x IN (SELECT rowid FROM table)" case */
102077      int iAddr = sqlite3VdbeAddOp0(v, OP_Once);
102078      VdbeCoverage(v);
102079
102080      sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
102081      eType = IN_INDEX_ROWID;
102082      ExplainQueryPlan((pParse, 0,
102083            "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
102084      sqlite3VdbeJumpHere(v, iAddr);
102085    }else{
102086      Index *pIdx;                         /* Iterator variable */
102087      int affinity_ok = 1;
102088      int i;
102089
102090      /* Check that the affinity that will be used to perform each
102091      ** comparison is the same as the affinity of each column in table
102092      ** on the RHS of the IN operator.  If it not, it is not possible to
102093      ** use any index of the RHS table.  */
102094      for(i=0; i<nExpr && affinity_ok; i++){
102095        Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
102096        int iCol = pEList->a[i].pExpr->iColumn;
102097        char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */
102098        char cmpaff = sqlite3CompareAffinity(pLhs, idxaff);
102099        testcase( cmpaff==SQLITE_AFF_BLOB );
102100        testcase( cmpaff==SQLITE_AFF_TEXT );
102101        switch( cmpaff ){
102102          case SQLITE_AFF_BLOB:
102103            break;
102104          case SQLITE_AFF_TEXT:
102105            /* sqlite3CompareAffinity() only returns TEXT if one side or the
102106            ** other has no affinity and the other side is TEXT.  Hence,
102107            ** the only way for cmpaff to be TEXT is for idxaff to be TEXT
102108            ** and for the term on the LHS of the IN to have no affinity. */
102109            assert( idxaff==SQLITE_AFF_TEXT );
102110            break;
102111          default:
102112            affinity_ok = sqlite3IsNumericAffinity(idxaff);
102113        }
102114      }
102115
102116      if( affinity_ok ){
102117        /* Search for an existing index that will work for this IN operator */
102118        for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
102119          Bitmask colUsed;      /* Columns of the index used */
102120          Bitmask mCol;         /* Mask for the current column */
102121          if( pIdx->nColumn<nExpr ) continue;
102122          if( pIdx->pPartIdxWhere!=0 ) continue;
102123          /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
102124          ** BITMASK(nExpr) without overflowing */
102125          testcase( pIdx->nColumn==BMS-2 );
102126          testcase( pIdx->nColumn==BMS-1 );
102127          if( pIdx->nColumn>=BMS-1 ) continue;
102128          if( mustBeUnique ){
102129            if( pIdx->nKeyCol>nExpr
102130             ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
102131            ){
102132              continue;  /* This index is not unique over the IN RHS columns */
102133            }
102134          }
102135
102136          colUsed = 0;   /* Columns of index used so far */
102137          for(i=0; i<nExpr; i++){
102138            Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
102139            Expr *pRhs = pEList->a[i].pExpr;
102140            CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
102141            int j;
102142
102143            assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
102144            for(j=0; j<nExpr; j++){
102145              if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
102146              assert( pIdx->azColl[j] );
102147              if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
102148                continue;
102149              }
102150              break;
102151            }
102152            if( j==nExpr ) break;
102153            mCol = MASKBIT(j);
102154            if( mCol & colUsed ) break; /* Each column used only once */
102155            colUsed |= mCol;
102156            if( aiMap ) aiMap[i] = j;
102157          }
102158
102159          assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
102160          if( colUsed==(MASKBIT(nExpr)-1) ){
102161            /* If we reach this point, that means the index pIdx is usable */
102162            int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
102163            ExplainQueryPlan((pParse, 0,
102164                              "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
102165            sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
102166            sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
102167            VdbeComment((v, "%s", pIdx->zName));
102168            assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
102169            eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
102170
102171            if( prRhsHasNull ){
102172#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
102173              i64 mask = (1<<nExpr)-1;
102174              sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed,
102175                  iTab, 0, 0, (u8*)&mask, P4_INT64);
102176#endif
102177              *prRhsHasNull = ++pParse->nMem;
102178              if( nExpr==1 ){
102179                sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
102180              }
102181            }
102182            sqlite3VdbeJumpHere(v, iAddr);
102183          }
102184        } /* End loop over indexes */
102185      } /* End if( affinity_ok ) */
102186    } /* End if not an rowid index */
102187  } /* End attempt to optimize using an index */
102188
102189  /* If no preexisting index is available for the IN clause
102190  ** and IN_INDEX_NOOP is an allowed reply
102191  ** and the RHS of the IN operator is a list, not a subquery
102192  ** and the RHS is not constant or has two or fewer terms,
102193  ** then it is not worth creating an ephemeral table to evaluate
102194  ** the IN operator so return IN_INDEX_NOOP.
102195  */
102196  if( eType==0
102197   && (inFlags & IN_INDEX_NOOP_OK)
102198   && !ExprHasProperty(pX, EP_xIsSelect)
102199   && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
102200  ){
102201    eType = IN_INDEX_NOOP;
102202  }
102203
102204  if( eType==0 ){
102205    /* Could not find an existing table or index to use as the RHS b-tree.
102206    ** We will have to generate an ephemeral table to do the job.
102207    */
102208    u32 savedNQueryLoop = pParse->nQueryLoop;
102209    int rMayHaveNull = 0;
102210    eType = IN_INDEX_EPH;
102211    if( inFlags & IN_INDEX_LOOP ){
102212      pParse->nQueryLoop = 0;
102213    }else if( prRhsHasNull ){
102214      *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
102215    }
102216    assert( pX->op==TK_IN );
102217    sqlite3CodeRhsOfIN(pParse, pX, iTab);
102218    if( rMayHaveNull ){
102219      sqlite3SetHasNullFlag(v, iTab, rMayHaveNull);
102220    }
102221    pParse->nQueryLoop = savedNQueryLoop;
102222  }
102223
102224  if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){
102225    int i, n;
102226    n = sqlite3ExprVectorSize(pX->pLeft);
102227    for(i=0; i<n; i++) aiMap[i] = i;
102228  }
102229  *piTab = iTab;
102230  return eType;
102231}
102232#endif
102233
102234#ifndef SQLITE_OMIT_SUBQUERY
102235/*
102236** Argument pExpr is an (?, ?...) IN(...) expression. This
102237** function allocates and returns a nul-terminated string containing
102238** the affinities to be used for each column of the comparison.
102239**
102240** It is the responsibility of the caller to ensure that the returned
102241** string is eventually freed using sqlite3DbFree().
102242*/
102243static char *exprINAffinity(Parse *pParse, Expr *pExpr){
102244  Expr *pLeft = pExpr->pLeft;
102245  int nVal = sqlite3ExprVectorSize(pLeft);
102246  Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0;
102247  char *zRet;
102248
102249  assert( pExpr->op==TK_IN );
102250  zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
102251  if( zRet ){
102252    int i;
102253    for(i=0; i<nVal; i++){
102254      Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i);
102255      char a = sqlite3ExprAffinity(pA);
102256      if( pSelect ){
102257        zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
102258      }else{
102259        zRet[i] = a;
102260      }
102261    }
102262    zRet[nVal] = '\0';
102263  }
102264  return zRet;
102265}
102266#endif
102267
102268#ifndef SQLITE_OMIT_SUBQUERY
102269/*
102270** Load the Parse object passed as the first argument with an error
102271** message of the form:
102272**
102273**   "sub-select returns N columns - expected M"
102274*/
102275SQLITE_PRIVATE void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){
102276  if( pParse->nErr==0 ){
102277    const char *zFmt = "sub-select returns %d columns - expected %d";
102278    sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect);
102279  }
102280}
102281#endif
102282
102283/*
102284** Expression pExpr is a vector that has been used in a context where
102285** it is not permitted. If pExpr is a sub-select vector, this routine
102286** loads the Parse object with a message of the form:
102287**
102288**   "sub-select returns N columns - expected 1"
102289**
102290** Or, if it is a regular scalar vector:
102291**
102292**   "row value misused"
102293*/
102294SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){
102295#ifndef SQLITE_OMIT_SUBQUERY
102296  if( pExpr->flags & EP_xIsSelect ){
102297    sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
102298  }else
102299#endif
102300  {
102301    sqlite3ErrorMsg(pParse, "row value misused");
102302  }
102303}
102304
102305#ifndef SQLITE_OMIT_SUBQUERY
102306/*
102307** Generate code that will construct an ephemeral table containing all terms
102308** in the RHS of an IN operator.  The IN operator can be in either of two
102309** forms:
102310**
102311**     x IN (4,5,11)              -- IN operator with list on right-hand side
102312**     x IN (SELECT a FROM b)     -- IN operator with subquery on the right
102313**
102314** The pExpr parameter is the IN operator.  The cursor number for the
102315** constructed ephermeral table is returned.  The first time the ephemeral
102316** table is computed, the cursor number is also stored in pExpr->iTable,
102317** however the cursor number returned might not be the same, as it might
102318** have been duplicated using OP_OpenDup.
102319**
102320** If the LHS expression ("x" in the examples) is a column value, or
102321** the SELECT statement returns a column value, then the affinity of that
102322** column is used to build the index keys. If both 'x' and the
102323** SELECT... statement are columns, then numeric affinity is used
102324** if either column has NUMERIC or INTEGER affinity. If neither
102325** 'x' nor the SELECT... statement are columns, then numeric affinity
102326** is used.
102327*/
102328SQLITE_PRIVATE void sqlite3CodeRhsOfIN(
102329  Parse *pParse,          /* Parsing context */
102330  Expr *pExpr,            /* The IN operator */
102331  int iTab                /* Use this cursor number */
102332){
102333  int addrOnce = 0;           /* Address of the OP_Once instruction at top */
102334  int addr;                   /* Address of OP_OpenEphemeral instruction */
102335  Expr *pLeft;                /* the LHS of the IN operator */
102336  KeyInfo *pKeyInfo = 0;      /* Key information */
102337  int nVal;                   /* Size of vector pLeft */
102338  Vdbe *v;                    /* The prepared statement under construction */
102339
102340  v = pParse->pVdbe;
102341  assert( v!=0 );
102342
102343  /* The evaluation of the IN must be repeated every time it
102344  ** is encountered if any of the following is true:
102345  **
102346  **    *  The right-hand side is a correlated subquery
102347  **    *  The right-hand side is an expression list containing variables
102348  **    *  We are inside a trigger
102349  **
102350  ** If all of the above are false, then we can compute the RHS just once
102351  ** and reuse it many names.
102352  */
102353  if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
102354    /* Reuse of the RHS is allowed */
102355    /* If this routine has already been coded, but the previous code
102356    ** might not have been invoked yet, so invoke it now as a subroutine.
102357    */
102358    if( ExprHasProperty(pExpr, EP_Subrtn) ){
102359      addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
102360      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
102361        ExplainQueryPlan((pParse, 0, "REUSE LIST SUBQUERY %d",
102362              pExpr->x.pSelect->selId));
102363      }
102364      sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
102365                        pExpr->y.sub.iAddr);
102366      sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
102367      sqlite3VdbeJumpHere(v, addrOnce);
102368      return;
102369    }
102370
102371    /* Begin coding the subroutine */
102372    ExprSetProperty(pExpr, EP_Subrtn);
102373    assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
102374    pExpr->y.sub.regReturn = ++pParse->nMem;
102375    pExpr->y.sub.iAddr =
102376      sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1;
102377    VdbeComment((v, "return address"));
102378
102379    addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
102380  }
102381
102382  /* Check to see if this is a vector IN operator */
102383  pLeft = pExpr->pLeft;
102384  nVal = sqlite3ExprVectorSize(pLeft);
102385
102386  /* Construct the ephemeral table that will contain the content of
102387  ** RHS of the IN operator.
102388  */
102389  pExpr->iTable = iTab;
102390  addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal);
102391#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
102392  if( ExprHasProperty(pExpr, EP_xIsSelect) ){
102393    VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
102394  }else{
102395    VdbeComment((v, "RHS of IN operator"));
102396  }
102397#endif
102398  pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
102399
102400  if( ExprHasProperty(pExpr, EP_xIsSelect) ){
102401    /* Case 1:     expr IN (SELECT ...)
102402    **
102403    ** Generate code to write the results of the select into the temporary
102404    ** table allocated and opened above.
102405    */
102406    Select *pSelect = pExpr->x.pSelect;
102407    ExprList *pEList = pSelect->pEList;
102408
102409    ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY %d",
102410        addrOnce?"":"CORRELATED ", pSelect->selId
102411    ));
102412    /* If the LHS and RHS of the IN operator do not match, that
102413    ** error will have been caught long before we reach this point. */
102414    if( ALWAYS(pEList->nExpr==nVal) ){
102415      SelectDest dest;
102416      int i;
102417      sqlite3SelectDestInit(&dest, SRT_Set, iTab);
102418      dest.zAffSdst = exprINAffinity(pParse, pExpr);
102419      pSelect->iLimit = 0;
102420      testcase( pSelect->selFlags & SF_Distinct );
102421      testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
102422      if( sqlite3Select(pParse, pSelect, &dest) ){
102423        sqlite3DbFree(pParse->db, dest.zAffSdst);
102424        sqlite3KeyInfoUnref(pKeyInfo);
102425        return;
102426      }
102427      sqlite3DbFree(pParse->db, dest.zAffSdst);
102428      assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
102429      assert( pEList!=0 );
102430      assert( pEList->nExpr>0 );
102431      assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
102432      for(i=0; i<nVal; i++){
102433        Expr *p = sqlite3VectorFieldSubexpr(pLeft, i);
102434        pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
102435            pParse, p, pEList->a[i].pExpr
102436        );
102437      }
102438    }
102439  }else if( ALWAYS(pExpr->x.pList!=0) ){
102440    /* Case 2:     expr IN (exprlist)
102441    **
102442    ** For each expression, build an index key from the evaluation and
102443    ** store it in the temporary table. If <expr> is a column, then use
102444    ** that columns affinity when building index keys. If <expr> is not
102445    ** a column, use numeric affinity.
102446    */
102447    char affinity;            /* Affinity of the LHS of the IN */
102448    int i;
102449    ExprList *pList = pExpr->x.pList;
102450    struct ExprList_item *pItem;
102451    int r1, r2;
102452    affinity = sqlite3ExprAffinity(pLeft);
102453    if( affinity<=SQLITE_AFF_NONE ){
102454      affinity = SQLITE_AFF_BLOB;
102455    }else if( affinity==SQLITE_AFF_REAL ){
102456      affinity = SQLITE_AFF_NUMERIC;
102457    }
102458    if( pKeyInfo ){
102459      assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
102460      pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
102461    }
102462
102463    /* Loop through each expression in <exprlist>. */
102464    r1 = sqlite3GetTempReg(pParse);
102465    r2 = sqlite3GetTempReg(pParse);
102466    for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
102467      Expr *pE2 = pItem->pExpr;
102468
102469      /* If the expression is not constant then we will need to
102470      ** disable the test that was generated above that makes sure
102471      ** this code only executes once.  Because for a non-constant
102472      ** expression we need to rerun this code each time.
102473      */
102474      if( addrOnce && !sqlite3ExprIsConstant(pE2) ){
102475        sqlite3VdbeChangeToNoop(v, addrOnce);
102476        ExprClearProperty(pExpr, EP_Subrtn);
102477        addrOnce = 0;
102478      }
102479
102480      /* Evaluate the expression and insert it into the temp table */
102481      sqlite3ExprCode(pParse, pE2, r1);
102482      sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);
102483      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r2, r1, 1);
102484    }
102485    sqlite3ReleaseTempReg(pParse, r1);
102486    sqlite3ReleaseTempReg(pParse, r2);
102487  }
102488  if( pKeyInfo ){
102489    sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
102490  }
102491  if( addrOnce ){
102492    sqlite3VdbeJumpHere(v, addrOnce);
102493    /* Subroutine return */
102494    sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
102495    sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
102496    sqlite3ClearTempRegCache(pParse);
102497  }
102498}
102499#endif /* SQLITE_OMIT_SUBQUERY */
102500
102501/*
102502** Generate code for scalar subqueries used as a subquery expression
102503** or EXISTS operator:
102504**
102505**     (SELECT a FROM b)          -- subquery
102506**     EXISTS (SELECT a FROM b)   -- EXISTS subquery
102507**
102508** The pExpr parameter is the SELECT or EXISTS operator to be coded.
102509**
102510** Return the register that holds the result.  For a multi-column SELECT,
102511** the result is stored in a contiguous array of registers and the
102512** return value is the register of the left-most result column.
102513** Return 0 if an error occurs.
102514*/
102515#ifndef SQLITE_OMIT_SUBQUERY
102516SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
102517  int addrOnce = 0;           /* Address of OP_Once at top of subroutine */
102518  int rReg = 0;               /* Register storing resulting */
102519  Select *pSel;               /* SELECT statement to encode */
102520  SelectDest dest;            /* How to deal with SELECT result */
102521  int nReg;                   /* Registers to allocate */
102522  Expr *pLimit;               /* New limit expression */
102523
102524  Vdbe *v = pParse->pVdbe;
102525  assert( v!=0 );
102526  testcase( pExpr->op==TK_EXISTS );
102527  testcase( pExpr->op==TK_SELECT );
102528  assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
102529  assert( ExprHasProperty(pExpr, EP_xIsSelect) );
102530  pSel = pExpr->x.pSelect;
102531
102532  /* The evaluation of the EXISTS/SELECT must be repeated every time it
102533  ** is encountered if any of the following is true:
102534  **
102535  **    *  The right-hand side is a correlated subquery
102536  **    *  The right-hand side is an expression list containing variables
102537  **    *  We are inside a trigger
102538  **
102539  ** If all of the above are false, then we can run this code just once
102540  ** save the results, and reuse the same result on subsequent invocations.
102541  */
102542  if( !ExprHasProperty(pExpr, EP_VarSelect) ){
102543    /* If this routine has already been coded, then invoke it as a
102544    ** subroutine. */
102545    if( ExprHasProperty(pExpr, EP_Subrtn) ){
102546      ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
102547      sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
102548                        pExpr->y.sub.iAddr);
102549      return pExpr->iTable;
102550    }
102551
102552    /* Begin coding the subroutine */
102553    ExprSetProperty(pExpr, EP_Subrtn);
102554    pExpr->y.sub.regReturn = ++pParse->nMem;
102555    pExpr->y.sub.iAddr =
102556      sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1;
102557    VdbeComment((v, "return address"));
102558
102559    addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
102560  }
102561
102562  /* For a SELECT, generate code to put the values for all columns of
102563  ** the first row into an array of registers and return the index of
102564  ** the first register.
102565  **
102566  ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
102567  ** into a register and return that register number.
102568  **
102569  ** In both cases, the query is augmented with "LIMIT 1".  Any
102570  ** preexisting limit is discarded in place of the new LIMIT 1.
102571  */
102572  ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY %d",
102573        addrOnce?"":"CORRELATED ", pSel->selId));
102574  nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
102575  sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
102576  pParse->nMem += nReg;
102577  if( pExpr->op==TK_SELECT ){
102578    dest.eDest = SRT_Mem;
102579    dest.iSdst = dest.iSDParm;
102580    dest.nSdst = nReg;
102581    sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
102582    VdbeComment((v, "Init subquery result"));
102583  }else{
102584    dest.eDest = SRT_Exists;
102585    sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
102586    VdbeComment((v, "Init EXISTS result"));
102587  }
102588  if( pSel->pLimit ){
102589    /* The subquery already has a limit.  If the pre-existing limit is X
102590    ** then make the new limit X<>0 so that the new limit is either 1 or 0 */
102591    sqlite3 *db = pParse->db;
102592    pLimit = sqlite3Expr(db, TK_INTEGER, "0");
102593    if( pLimit ){
102594      pLimit->affExpr = SQLITE_AFF_NUMERIC;
102595      pLimit = sqlite3PExpr(pParse, TK_NE,
102596                            sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
102597    }
102598    sqlite3ExprDelete(db, pSel->pLimit->pLeft);
102599    pSel->pLimit->pLeft = pLimit;
102600  }else{
102601    /* If there is no pre-existing limit add a limit of 1 */
102602    pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
102603    pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
102604  }
102605  pSel->iLimit = 0;
102606  if( sqlite3Select(pParse, pSel, &dest) ){
102607    return 0;
102608  }
102609  pExpr->iTable = rReg = dest.iSDParm;
102610  ExprSetVVAProperty(pExpr, EP_NoReduce);
102611  if( addrOnce ){
102612    sqlite3VdbeJumpHere(v, addrOnce);
102613
102614    /* Subroutine return */
102615    sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
102616    sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
102617    sqlite3ClearTempRegCache(pParse);
102618  }
102619
102620  return rReg;
102621}
102622#endif /* SQLITE_OMIT_SUBQUERY */
102623
102624#ifndef SQLITE_OMIT_SUBQUERY
102625/*
102626** Expr pIn is an IN(...) expression. This function checks that the
102627** sub-select on the RHS of the IN() operator has the same number of
102628** columns as the vector on the LHS. Or, if the RHS of the IN() is not
102629** a sub-query, that the LHS is a vector of size 1.
102630*/
102631SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
102632  int nVector = sqlite3ExprVectorSize(pIn->pLeft);
102633  if( (pIn->flags & EP_xIsSelect) ){
102634    if( nVector!=pIn->x.pSelect->pEList->nExpr ){
102635      sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
102636      return 1;
102637    }
102638  }else if( nVector!=1 ){
102639    sqlite3VectorErrorMsg(pParse, pIn->pLeft);
102640    return 1;
102641  }
102642  return 0;
102643}
102644#endif
102645
102646#ifndef SQLITE_OMIT_SUBQUERY
102647/*
102648** Generate code for an IN expression.
102649**
102650**      x IN (SELECT ...)
102651**      x IN (value, value, ...)
102652**
102653** The left-hand side (LHS) is a scalar or vector expression.  The
102654** right-hand side (RHS) is an array of zero or more scalar values, or a
102655** subquery.  If the RHS is a subquery, the number of result columns must
102656** match the number of columns in the vector on the LHS.  If the RHS is
102657** a list of values, the LHS must be a scalar.
102658**
102659** The IN operator is true if the LHS value is contained within the RHS.
102660** The result is false if the LHS is definitely not in the RHS.  The
102661** result is NULL if the presence of the LHS in the RHS cannot be
102662** determined due to NULLs.
102663**
102664** This routine generates code that jumps to destIfFalse if the LHS is not
102665** contained within the RHS.  If due to NULLs we cannot determine if the LHS
102666** is contained in the RHS then jump to destIfNull.  If the LHS is contained
102667** within the RHS then fall through.
102668**
102669** See the separate in-operator.md documentation file in the canonical
102670** SQLite source tree for additional information.
102671*/
102672static void sqlite3ExprCodeIN(
102673  Parse *pParse,        /* Parsing and code generating context */
102674  Expr *pExpr,          /* The IN expression */
102675  int destIfFalse,      /* Jump here if LHS is not contained in the RHS */
102676  int destIfNull        /* Jump here if the results are unknown due to NULLs */
102677){
102678  int rRhsHasNull = 0;  /* Register that is true if RHS contains NULL values */
102679  int eType;            /* Type of the RHS */
102680  int rLhs;             /* Register(s) holding the LHS values */
102681  int rLhsOrig;         /* LHS values prior to reordering by aiMap[] */
102682  Vdbe *v;              /* Statement under construction */
102683  int *aiMap = 0;       /* Map from vector field to index column */
102684  char *zAff = 0;       /* Affinity string for comparisons */
102685  int nVector;          /* Size of vectors for this IN operator */
102686  int iDummy;           /* Dummy parameter to exprCodeVector() */
102687  Expr *pLeft;          /* The LHS of the IN operator */
102688  int i;                /* loop counter */
102689  int destStep2;        /* Where to jump when NULLs seen in step 2 */
102690  int destStep6 = 0;    /* Start of code for Step 6 */
102691  int addrTruthOp;      /* Address of opcode that determines the IN is true */
102692  int destNotNull;      /* Jump here if a comparison is not true in step 6 */
102693  int addrTop;          /* Top of the step-6 loop */
102694  int iTab = 0;         /* Index to use */
102695  u8 okConstFactor = pParse->okConstFactor;
102696
102697  assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
102698  pLeft = pExpr->pLeft;
102699  if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
102700  zAff = exprINAffinity(pParse, pExpr);
102701  nVector = sqlite3ExprVectorSize(pExpr->pLeft);
102702  aiMap = (int*)sqlite3DbMallocZero(
102703      pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
102704  );
102705  if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
102706
102707  /* Attempt to compute the RHS. After this step, if anything other than
102708  ** IN_INDEX_NOOP is returned, the table opened with cursor iTab
102709  ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
102710  ** the RHS has not yet been coded.  */
102711  v = pParse->pVdbe;
102712  assert( v!=0 );       /* OOM detected prior to this routine */
102713  VdbeNoopComment((v, "begin IN expr"));
102714  eType = sqlite3FindInIndex(pParse, pExpr,
102715                             IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
102716                             destIfFalse==destIfNull ? 0 : &rRhsHasNull,
102717                             aiMap, &iTab);
102718
102719  assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
102720       || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC
102721  );
102722#ifdef SQLITE_DEBUG
102723  /* Confirm that aiMap[] contains nVector integer values between 0 and
102724  ** nVector-1. */
102725  for(i=0; i<nVector; i++){
102726    int j, cnt;
102727    for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++;
102728    assert( cnt==1 );
102729  }
102730#endif
102731
102732  /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a
102733  ** vector, then it is stored in an array of nVector registers starting
102734  ** at r1.
102735  **
102736  ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
102737  ** so that the fields are in the same order as an existing index.   The
102738  ** aiMap[] array contains a mapping from the original LHS field order to
102739  ** the field order that matches the RHS index.
102740  **
102741  ** Avoid factoring the LHS of the IN(...) expression out of the loop,
102742  ** even if it is constant, as OP_Affinity may be used on the register
102743  ** by code generated below.  */
102744  assert( pParse->okConstFactor==okConstFactor );
102745  pParse->okConstFactor = 0;
102746  rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy);
102747  pParse->okConstFactor = okConstFactor;
102748  for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
102749  if( i==nVector ){
102750    /* LHS fields are not reordered */
102751    rLhs = rLhsOrig;
102752  }else{
102753    /* Need to reorder the LHS fields according to aiMap */
102754    rLhs = sqlite3GetTempRange(pParse, nVector);
102755    for(i=0; i<nVector; i++){
102756      sqlite3VdbeAddOp3(v, OP_Copy, rLhsOrig+i, rLhs+aiMap[i], 0);
102757    }
102758  }
102759
102760  /* If sqlite3FindInIndex() did not find or create an index that is
102761  ** suitable for evaluating the IN operator, then evaluate using a
102762  ** sequence of comparisons.
102763  **
102764  ** This is step (1) in the in-operator.md optimized algorithm.
102765  */
102766  if( eType==IN_INDEX_NOOP ){
102767    ExprList *pList = pExpr->x.pList;
102768    CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
102769    int labelOk = sqlite3VdbeMakeLabel(pParse);
102770    int r2, regToFree;
102771    int regCkNull = 0;
102772    int ii;
102773    assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
102774    if( destIfNull!=destIfFalse ){
102775      regCkNull = sqlite3GetTempReg(pParse);
102776      sqlite3VdbeAddOp3(v, OP_BitAnd, rLhs, rLhs, regCkNull);
102777    }
102778    for(ii=0; ii<pList->nExpr; ii++){
102779      r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
102780      if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
102781        sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
102782      }
102783      sqlite3ReleaseTempReg(pParse, regToFree);
102784      if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
102785        int op = rLhs!=r2 ? OP_Eq : OP_NotNull;
102786        sqlite3VdbeAddOp4(v, op, rLhs, labelOk, r2,
102787                          (void*)pColl, P4_COLLSEQ);
102788        VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
102789        VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
102790        VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
102791        VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
102792        sqlite3VdbeChangeP5(v, zAff[0]);
102793      }else{
102794        int op = rLhs!=r2 ? OP_Ne : OP_IsNull;
102795        assert( destIfNull==destIfFalse );
102796        sqlite3VdbeAddOp4(v, op, rLhs, destIfFalse, r2,
102797                          (void*)pColl, P4_COLLSEQ);
102798        VdbeCoverageIf(v, op==OP_Ne);
102799        VdbeCoverageIf(v, op==OP_IsNull);
102800        sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL);
102801      }
102802    }
102803    if( regCkNull ){
102804      sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
102805      sqlite3VdbeGoto(v, destIfFalse);
102806    }
102807    sqlite3VdbeResolveLabel(v, labelOk);
102808    sqlite3ReleaseTempReg(pParse, regCkNull);
102809    goto sqlite3ExprCodeIN_finished;
102810  }
102811
102812  /* Step 2: Check to see if the LHS contains any NULL columns.  If the
102813  ** LHS does contain NULLs then the result must be either FALSE or NULL.
102814  ** We will then skip the binary search of the RHS.
102815  */
102816  if( destIfNull==destIfFalse ){
102817    destStep2 = destIfFalse;
102818  }else{
102819    destStep2 = destStep6 = sqlite3VdbeMakeLabel(pParse);
102820  }
102821  if( pParse->nErr ) goto sqlite3ExprCodeIN_finished;
102822  for(i=0; i<nVector; i++){
102823    Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
102824    if( sqlite3ExprCanBeNull(p) ){
102825      sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2);
102826      VdbeCoverage(v);
102827    }
102828  }
102829
102830  /* Step 3.  The LHS is now known to be non-NULL.  Do the binary search
102831  ** of the RHS using the LHS as a probe.  If found, the result is
102832  ** true.
102833  */
102834  if( eType==IN_INDEX_ROWID ){
102835    /* In this case, the RHS is the ROWID of table b-tree and so we also
102836    ** know that the RHS is non-NULL.  Hence, we combine steps 3 and 4
102837    ** into a single opcode. */
102838    sqlite3VdbeAddOp3(v, OP_SeekRowid, iTab, destIfFalse, rLhs);
102839    VdbeCoverage(v);
102840    addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto);  /* Return True */
102841  }else{
102842    sqlite3VdbeAddOp4(v, OP_Affinity, rLhs, nVector, 0, zAff, nVector);
102843    if( destIfFalse==destIfNull ){
102844      /* Combine Step 3 and Step 5 into a single opcode */
102845      sqlite3VdbeAddOp4Int(v, OP_NotFound, iTab, destIfFalse,
102846                           rLhs, nVector); VdbeCoverage(v);
102847      goto sqlite3ExprCodeIN_finished;
102848    }
102849    /* Ordinary Step 3, for the case where FALSE and NULL are distinct */
102850    addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found, iTab, 0,
102851                                      rLhs, nVector); VdbeCoverage(v);
102852  }
102853
102854  /* Step 4.  If the RHS is known to be non-NULL and we did not find
102855  ** an match on the search above, then the result must be FALSE.
102856  */
102857  if( rRhsHasNull && nVector==1 ){
102858    sqlite3VdbeAddOp2(v, OP_NotNull, rRhsHasNull, destIfFalse);
102859    VdbeCoverage(v);
102860  }
102861
102862  /* Step 5.  If we do not care about the difference between NULL and
102863  ** FALSE, then just return false.
102864  */
102865  if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse);
102866
102867  /* Step 6: Loop through rows of the RHS.  Compare each row to the LHS.
102868  ** If any comparison is NULL, then the result is NULL.  If all
102869  ** comparisons are FALSE then the final result is FALSE.
102870  **
102871  ** For a scalar LHS, it is sufficient to check just the first row
102872  ** of the RHS.
102873  */
102874  if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6);
102875  addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, destIfFalse);
102876  VdbeCoverage(v);
102877  if( nVector>1 ){
102878    destNotNull = sqlite3VdbeMakeLabel(pParse);
102879  }else{
102880    /* For nVector==1, combine steps 6 and 7 by immediately returning
102881    ** FALSE if the first comparison is not NULL */
102882    destNotNull = destIfFalse;
102883  }
102884  for(i=0; i<nVector; i++){
102885    Expr *p;
102886    CollSeq *pColl;
102887    int r3 = sqlite3GetTempReg(pParse);
102888    p = sqlite3VectorFieldSubexpr(pLeft, i);
102889    pColl = sqlite3ExprCollSeq(pParse, p);
102890    sqlite3VdbeAddOp3(v, OP_Column, iTab, i, r3);
102891    sqlite3VdbeAddOp4(v, OP_Ne, rLhs+i, destNotNull, r3,
102892                      (void*)pColl, P4_COLLSEQ);
102893    VdbeCoverage(v);
102894    sqlite3ReleaseTempReg(pParse, r3);
102895  }
102896  sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
102897  if( nVector>1 ){
102898    sqlite3VdbeResolveLabel(v, destNotNull);
102899    sqlite3VdbeAddOp2(v, OP_Next, iTab, addrTop+1);
102900    VdbeCoverage(v);
102901
102902    /* Step 7:  If we reach this point, we know that the result must
102903    ** be false. */
102904    sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
102905  }
102906
102907  /* Jumps here in order to return true. */
102908  sqlite3VdbeJumpHere(v, addrTruthOp);
102909
102910sqlite3ExprCodeIN_finished:
102911  if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
102912  VdbeComment((v, "end IN expr"));
102913sqlite3ExprCodeIN_oom_error:
102914  sqlite3DbFree(pParse->db, aiMap);
102915  sqlite3DbFree(pParse->db, zAff);
102916}
102917#endif /* SQLITE_OMIT_SUBQUERY */
102918
102919#ifndef SQLITE_OMIT_FLOATING_POINT
102920/*
102921** Generate an instruction that will put the floating point
102922** value described by z[0..n-1] into register iMem.
102923**
102924** The z[] string will probably not be zero-terminated.  But the
102925** z[n] character is guaranteed to be something that does not look
102926** like the continuation of the number.
102927*/
102928static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
102929  if( ALWAYS(z!=0) ){
102930    double value;
102931    sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
102932    assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
102933    if( negateFlag ) value = -value;
102934    sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL);
102935  }
102936}
102937#endif
102938
102939
102940/*
102941** Generate an instruction that will put the integer describe by
102942** text z[0..n-1] into register iMem.
102943**
102944** Expr.u.zToken is always UTF8 and zero-terminated.
102945*/
102946static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
102947  Vdbe *v = pParse->pVdbe;
102948  if( pExpr->flags & EP_IntValue ){
102949    int i = pExpr->u.iValue;
102950    assert( i>=0 );
102951    if( negFlag ) i = -i;
102952    sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
102953  }else{
102954    int c;
102955    i64 value;
102956    const char *z = pExpr->u.zToken;
102957    assert( z!=0 );
102958    c = sqlite3DecOrHexToI64(z, &value);
102959    if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64)){
102960#ifdef SQLITE_OMIT_FLOATING_POINT
102961      sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
102962#else
102963#ifndef SQLITE_OMIT_HEX_INTEGER
102964      if( sqlite3_strnicmp(z,"0x",2)==0 ){
102965        sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z);
102966      }else
102967#endif
102968      {
102969        codeReal(v, z, negFlag, iMem);
102970      }
102971#endif
102972    }else{
102973      if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
102974      sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
102975    }
102976  }
102977}
102978
102979
102980/* Generate code that will load into register regOut a value that is
102981** appropriate for the iIdxCol-th column of index pIdx.
102982*/
102983SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(
102984  Parse *pParse,  /* The parsing context */
102985  Index *pIdx,    /* The index whose column is to be loaded */
102986  int iTabCur,    /* Cursor pointing to a table row */
102987  int iIdxCol,    /* The column of the index to be loaded */
102988  int regOut      /* Store the index column value in this register */
102989){
102990  i16 iTabCol = pIdx->aiColumn[iIdxCol];
102991  if( iTabCol==XN_EXPR ){
102992    assert( pIdx->aColExpr );
102993    assert( pIdx->aColExpr->nExpr>iIdxCol );
102994    pParse->iSelfTab = iTabCur + 1;
102995    sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
102996    pParse->iSelfTab = 0;
102997  }else{
102998    sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
102999                                    iTabCol, regOut);
103000  }
103001}
103002
103003#ifndef SQLITE_OMIT_GENERATED_COLUMNS
103004/*
103005** Generate code that will compute the value of generated column pCol
103006** and store the result in register regOut
103007*/
103008SQLITE_PRIVATE void sqlite3ExprCodeGeneratedColumn(
103009  Parse *pParse,
103010  Column *pCol,
103011  int regOut
103012){
103013  int iAddr;
103014  Vdbe *v = pParse->pVdbe;
103015  assert( v!=0 );
103016  assert( pParse->iSelfTab!=0 );
103017  if( pParse->iSelfTab>0 ){
103018    iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut);
103019  }else{
103020    iAddr = 0;
103021  }
103022  sqlite3ExprCodeCopy(pParse, pCol->pDflt, regOut);
103023  if( pCol->affinity>=SQLITE_AFF_TEXT ){
103024    sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1);
103025  }
103026  if( iAddr ) sqlite3VdbeJumpHere(v, iAddr);
103027}
103028#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
103029
103030/*
103031** Generate code to extract the value of the iCol-th column of a table.
103032*/
103033SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
103034  Vdbe *v,        /* Parsing context */
103035  Table *pTab,    /* The table containing the value */
103036  int iTabCur,    /* The table cursor.  Or the PK cursor for WITHOUT ROWID */
103037  int iCol,       /* Index of the column to extract */
103038  int regOut      /* Extract the value into this register */
103039){
103040  Column *pCol;
103041  assert( v!=0 );
103042  if( pTab==0 ){
103043    sqlite3VdbeAddOp3(v, OP_Column, iTabCur, iCol, regOut);
103044    return;
103045  }
103046  if( iCol<0 || iCol==pTab->iPKey ){
103047    sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
103048  }else{
103049    int op;
103050    int x;
103051    if( IsVirtual(pTab) ){
103052      op = OP_VColumn;
103053      x = iCol;
103054#ifndef SQLITE_OMIT_GENERATED_COLUMNS
103055    }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){
103056      Parse *pParse = sqlite3VdbeParser(v);
103057      if( pCol->colFlags & COLFLAG_BUSY ){
103058        sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pCol->zName);
103059      }else{
103060        int savedSelfTab = pParse->iSelfTab;
103061        pCol->colFlags |= COLFLAG_BUSY;
103062        pParse->iSelfTab = iTabCur+1;
103063        sqlite3ExprCodeGeneratedColumn(pParse, pCol, regOut);
103064        pParse->iSelfTab = savedSelfTab;
103065        pCol->colFlags &= ~COLFLAG_BUSY;
103066      }
103067      return;
103068#endif
103069    }else if( !HasRowid(pTab) ){
103070      testcase( iCol!=sqlite3TableColumnToStorage(pTab, iCol) );
103071      x = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
103072      op = OP_Column;
103073    }else{
103074      x = sqlite3TableColumnToStorage(pTab,iCol);
103075      testcase( x!=iCol );
103076      op = OP_Column;
103077    }
103078    sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
103079    sqlite3ColumnDefault(v, pTab, iCol, regOut);
103080  }
103081}
103082
103083/*
103084** Generate code that will extract the iColumn-th column from
103085** table pTab and store the column value in register iReg.
103086**
103087** There must be an open cursor to pTab in iTable when this routine
103088** is called.  If iColumn<0 then code is generated that extracts the rowid.
103089*/
103090SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
103091  Parse *pParse,   /* Parsing and code generating context */
103092  Table *pTab,     /* Description of the table we are reading from */
103093  int iColumn,     /* Index of the table column */
103094  int iTable,      /* The cursor pointing to the table */
103095  int iReg,        /* Store results here */
103096  u8 p5            /* P5 value for OP_Column + FLAGS */
103097){
103098  assert( pParse->pVdbe!=0 );
103099  sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
103100  if( p5 ){
103101    VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1);
103102    if( pOp->opcode==OP_Column ) pOp->p5 = p5;
103103  }
103104  return iReg;
103105}
103106
103107/*
103108** Generate code to move content from registers iFrom...iFrom+nReg-1
103109** over to iTo..iTo+nReg-1.
103110*/
103111SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
103112  sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
103113}
103114
103115/*
103116** Convert a scalar expression node to a TK_REGISTER referencing
103117** register iReg.  The caller must ensure that iReg already contains
103118** the correct value for the expression.
103119*/
103120static void exprToRegister(Expr *pExpr, int iReg){
103121  Expr *p = sqlite3ExprSkipCollateAndLikely(pExpr);
103122  p->op2 = p->op;
103123  p->op = TK_REGISTER;
103124  p->iTable = iReg;
103125  ExprClearProperty(p, EP_Skip);
103126}
103127
103128/*
103129** Evaluate an expression (either a vector or a scalar expression) and store
103130** the result in continguous temporary registers.  Return the index of
103131** the first register used to store the result.
103132**
103133** If the returned result register is a temporary scalar, then also write
103134** that register number into *piFreeable.  If the returned result register
103135** is not a temporary or if the expression is a vector set *piFreeable
103136** to 0.
103137*/
103138static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){
103139  int iResult;
103140  int nResult = sqlite3ExprVectorSize(p);
103141  if( nResult==1 ){
103142    iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable);
103143  }else{
103144    *piFreeable = 0;
103145    if( p->op==TK_SELECT ){
103146#if SQLITE_OMIT_SUBQUERY
103147      iResult = 0;
103148#else
103149      iResult = sqlite3CodeSubselect(pParse, p);
103150#endif
103151    }else{
103152      int i;
103153      iResult = pParse->nMem+1;
103154      pParse->nMem += nResult;
103155      for(i=0; i<nResult; i++){
103156        sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
103157      }
103158    }
103159  }
103160  return iResult;
103161}
103162
103163/*
103164** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
103165** so that a subsequent copy will not be merged into this one.
103166*/
103167static void setDoNotMergeFlagOnCopy(Vdbe *v){
103168  if( sqlite3VdbeGetOp(v, -1)->opcode==OP_Copy ){
103169    sqlite3VdbeChangeP5(v, 1);  /* Tag trailing OP_Copy as not mergable */
103170  }
103171}
103172
103173/*
103174** Generate code to implement special SQL functions that are implemented
103175** in-line rather than by using the usual callbacks.
103176*/
103177static int exprCodeInlineFunction(
103178  Parse *pParse,        /* Parsing context */
103179  ExprList *pFarg,      /* List of function arguments */
103180  int iFuncId,          /* Function ID.  One of the INTFUNC_... values */
103181  int target            /* Store function result in this register */
103182){
103183  int nFarg;
103184  Vdbe *v = pParse->pVdbe;
103185  assert( v!=0 );
103186  assert( pFarg!=0 );
103187  nFarg = pFarg->nExpr;
103188  assert( nFarg>0 );  /* All in-line functions have at least one argument */
103189  switch( iFuncId ){
103190    case INLINEFUNC_coalesce: {
103191      /* Attempt a direct implementation of the built-in COALESCE() and
103192      ** IFNULL() functions.  This avoids unnecessary evaluation of
103193      ** arguments past the first non-NULL argument.
103194      */
103195      int endCoalesce = sqlite3VdbeMakeLabel(pParse);
103196      int i;
103197      assert( nFarg>=2 );
103198      sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
103199      for(i=1; i<nFarg; i++){
103200        sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
103201        VdbeCoverage(v);
103202        sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
103203      }
103204      setDoNotMergeFlagOnCopy(v);
103205      sqlite3VdbeResolveLabel(v, endCoalesce);
103206      break;
103207    }
103208    case INLINEFUNC_iif: {
103209      Expr caseExpr;
103210      memset(&caseExpr, 0, sizeof(caseExpr));
103211      caseExpr.op = TK_CASE;
103212      caseExpr.x.pList = pFarg;
103213      return sqlite3ExprCodeTarget(pParse, &caseExpr, target);
103214    }
103215
103216    default: {
103217      /* The UNLIKELY() function is a no-op.  The result is the value
103218      ** of the first argument.
103219      */
103220      assert( nFarg==1 || nFarg==2 );
103221      target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
103222      break;
103223    }
103224
103225  /***********************************************************************
103226  ** Test-only SQL functions that are only usable if enabled
103227  ** via SQLITE_TESTCTRL_INTERNAL_FUNCTIONS
103228  */
103229    case INLINEFUNC_expr_compare: {
103230      /* Compare two expressions using sqlite3ExprCompare() */
103231      assert( nFarg==2 );
103232      sqlite3VdbeAddOp2(v, OP_Integer,
103233         sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
103234         target);
103235      break;
103236    }
103237
103238    case INLINEFUNC_expr_implies_expr: {
103239      /* Compare two expressions using sqlite3ExprImpliesExpr() */
103240      assert( nFarg==2 );
103241      sqlite3VdbeAddOp2(v, OP_Integer,
103242         sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
103243         target);
103244      break;
103245    }
103246
103247    case INLINEFUNC_implies_nonnull_row: {
103248      /* REsult of sqlite3ExprImpliesNonNullRow() */
103249      Expr *pA1;
103250      assert( nFarg==2 );
103251      pA1 = pFarg->a[1].pExpr;
103252      if( pA1->op==TK_COLUMN ){
103253        sqlite3VdbeAddOp2(v, OP_Integer,
103254           sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable),
103255           target);
103256      }else{
103257        sqlite3VdbeAddOp2(v, OP_Null, 0, target);
103258      }
103259      break;
103260    }
103261
103262#ifdef SQLITE_DEBUG
103263    case INLINEFUNC_affinity: {
103264      /* The AFFINITY() function evaluates to a string that describes
103265      ** the type affinity of the argument.  This is used for testing of
103266      ** the SQLite type logic.
103267      */
103268      const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
103269      char aff;
103270      assert( nFarg==1 );
103271      aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
103272      sqlite3VdbeLoadString(v, target,
103273              (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
103274      break;
103275    }
103276#endif
103277  }
103278  return target;
103279}
103280
103281
103282/*
103283** Generate code into the current Vdbe to evaluate the given
103284** expression.  Attempt to store the results in register "target".
103285** Return the register where results are stored.
103286**
103287** With this routine, there is no guarantee that results will
103288** be stored in target.  The result might be stored in some other
103289** register if it is convenient to do so.  The calling function
103290** must check the return code and move the results to the desired
103291** register.
103292*/
103293SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
103294  Vdbe *v = pParse->pVdbe;  /* The VM under construction */
103295  int op;                   /* The opcode being coded */
103296  int inReg = target;       /* Results stored in register inReg */
103297  int regFree1 = 0;         /* If non-zero free this temporary register */
103298  int regFree2 = 0;         /* If non-zero free this temporary register */
103299  int r1, r2;               /* Various register numbers */
103300  Expr tempX;               /* Temporary expression node */
103301  int p5 = 0;
103302
103303  assert( target>0 && target<=pParse->nMem );
103304  if( v==0 ){
103305    assert( pParse->db->mallocFailed );
103306    return 0;
103307  }
103308
103309expr_code_doover:
103310  if( pExpr==0 ){
103311    op = TK_NULL;
103312  }else{
103313    assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
103314    op = pExpr->op;
103315  }
103316  switch( op ){
103317    case TK_AGG_COLUMN: {
103318      AggInfo *pAggInfo = pExpr->pAggInfo;
103319      struct AggInfo_col *pCol;
103320      assert( pAggInfo!=0 );
103321      assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
103322      pCol = &pAggInfo->aCol[pExpr->iAgg];
103323      if( !pAggInfo->directMode ){
103324        assert( pCol->iMem>0 );
103325        return pCol->iMem;
103326      }else if( pAggInfo->useSortingIdx ){
103327        Table *pTab = pCol->pTab;
103328        sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
103329                              pCol->iSorterColumn, target);
103330        if( pCol->iColumn<0 ){
103331          VdbeComment((v,"%s.rowid",pTab->zName));
103332        }else{
103333          VdbeComment((v,"%s.%s",pTab->zName,pTab->aCol[pCol->iColumn].zName));
103334          if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
103335            sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
103336          }
103337        }
103338        return target;
103339      }
103340      /* Otherwise, fall thru into the TK_COLUMN case */
103341    }
103342    case TK_COLUMN: {
103343      int iTab = pExpr->iTable;
103344      int iReg;
103345      if( ExprHasProperty(pExpr, EP_FixedCol) ){
103346        /* This COLUMN expression is really a constant due to WHERE clause
103347        ** constraints, and that constant is coded by the pExpr->pLeft
103348        ** expresssion.  However, make sure the constant has the correct
103349        ** datatype by applying the Affinity of the table column to the
103350        ** constant.
103351        */
103352        int aff;
103353        iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
103354        if( pExpr->y.pTab ){
103355          aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
103356        }else{
103357          aff = pExpr->affExpr;
103358        }
103359        if( aff>SQLITE_AFF_BLOB ){
103360          static const char zAff[] = "B\000C\000D\000E";
103361          assert( SQLITE_AFF_BLOB=='A' );
103362          assert( SQLITE_AFF_TEXT=='B' );
103363          sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0,
103364                            &zAff[(aff-'B')*2], P4_STATIC);
103365        }
103366        return iReg;
103367      }
103368      if( iTab<0 ){
103369        if( pParse->iSelfTab<0 ){
103370          /* Other columns in the same row for CHECK constraints or
103371          ** generated columns or for inserting into partial index.
103372          ** The row is unpacked into registers beginning at
103373          ** 0-(pParse->iSelfTab).  The rowid (if any) is in a register
103374          ** immediately prior to the first column.
103375          */
103376          Column *pCol;
103377          Table *pTab = pExpr->y.pTab;
103378          int iSrc;
103379          int iCol = pExpr->iColumn;
103380          assert( pTab!=0 );
103381          assert( iCol>=XN_ROWID );
103382          assert( iCol<pTab->nCol );
103383          if( iCol<0 ){
103384            return -1-pParse->iSelfTab;
103385          }
103386          pCol = pTab->aCol + iCol;
103387          testcase( iCol!=sqlite3TableColumnToStorage(pTab,iCol) );
103388          iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
103389#ifndef SQLITE_OMIT_GENERATED_COLUMNS
103390          if( pCol->colFlags & COLFLAG_GENERATED ){
103391            if( pCol->colFlags & COLFLAG_BUSY ){
103392              sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"",
103393                              pCol->zName);
103394              return 0;
103395            }
103396            pCol->colFlags |= COLFLAG_BUSY;
103397            if( pCol->colFlags & COLFLAG_NOTAVAIL ){
103398              sqlite3ExprCodeGeneratedColumn(pParse, pCol, iSrc);
103399            }
103400            pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL);
103401            return iSrc;
103402          }else
103403#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
103404          if( pCol->affinity==SQLITE_AFF_REAL ){
103405            sqlite3VdbeAddOp2(v, OP_SCopy, iSrc, target);
103406            sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
103407            return target;
103408          }else{
103409            return iSrc;
103410          }
103411        }else{
103412          /* Coding an expression that is part of an index where column names
103413          ** in the index refer to the table to which the index belongs */
103414          iTab = pParse->iSelfTab - 1;
103415        }
103416      }
103417      iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
103418                               pExpr->iColumn, iTab, target,
103419                               pExpr->op2);
103420      if( pExpr->y.pTab==0 && pExpr->affExpr==SQLITE_AFF_REAL ){
103421        sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
103422      }
103423      return iReg;
103424    }
103425    case TK_INTEGER: {
103426      codeInteger(pParse, pExpr, 0, target);
103427      return target;
103428    }
103429    case TK_TRUEFALSE: {
103430      sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target);
103431      return target;
103432    }
103433#ifndef SQLITE_OMIT_FLOATING_POINT
103434    case TK_FLOAT: {
103435      assert( !ExprHasProperty(pExpr, EP_IntValue) );
103436      codeReal(v, pExpr->u.zToken, 0, target);
103437      return target;
103438    }
103439#endif
103440    case TK_STRING: {
103441      assert( !ExprHasProperty(pExpr, EP_IntValue) );
103442      sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
103443      return target;
103444    }
103445    default: {
103446      /* Make NULL the default case so that if a bug causes an illegal
103447      ** Expr node to be passed into this function, it will be handled
103448      ** sanely and not crash.  But keep the assert() to bring the problem
103449      ** to the attention of the developers. */
103450      assert( op==TK_NULL );
103451      sqlite3VdbeAddOp2(v, OP_Null, 0, target);
103452      return target;
103453    }
103454#ifndef SQLITE_OMIT_BLOB_LITERAL
103455    case TK_BLOB: {
103456      int n;
103457      const char *z;
103458      char *zBlob;
103459      assert( !ExprHasProperty(pExpr, EP_IntValue) );
103460      assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
103461      assert( pExpr->u.zToken[1]=='\'' );
103462      z = &pExpr->u.zToken[2];
103463      n = sqlite3Strlen30(z) - 1;
103464      assert( z[n]=='\'' );
103465      zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
103466      sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
103467      return target;
103468    }
103469#endif
103470    case TK_VARIABLE: {
103471      assert( !ExprHasProperty(pExpr, EP_IntValue) );
103472      assert( pExpr->u.zToken!=0 );
103473      assert( pExpr->u.zToken[0]!=0 );
103474      sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
103475      if( pExpr->u.zToken[1]!=0 ){
103476        const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
103477        assert( pExpr->u.zToken[0]=='?' || (z && !strcmp(pExpr->u.zToken, z)) );
103478        pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
103479        sqlite3VdbeAppendP4(v, (char*)z, P4_STATIC);
103480      }
103481      return target;
103482    }
103483    case TK_REGISTER: {
103484      return pExpr->iTable;
103485    }
103486#ifndef SQLITE_OMIT_CAST
103487    case TK_CAST: {
103488      /* Expressions of the form:   CAST(pLeft AS token) */
103489      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
103490      if( inReg!=target ){
103491        sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
103492        inReg = target;
103493      }
103494      sqlite3VdbeAddOp2(v, OP_Cast, target,
103495                        sqlite3AffinityType(pExpr->u.zToken, 0));
103496      return inReg;
103497    }
103498#endif /* SQLITE_OMIT_CAST */
103499    case TK_IS:
103500    case TK_ISNOT:
103501      op = (op==TK_IS) ? TK_EQ : TK_NE;
103502      p5 = SQLITE_NULLEQ;
103503      /* fall-through */
103504    case TK_LT:
103505    case TK_LE:
103506    case TK_GT:
103507    case TK_GE:
103508    case TK_NE:
103509    case TK_EQ: {
103510      Expr *pLeft = pExpr->pLeft;
103511      if( sqlite3ExprIsVector(pLeft) ){
103512        codeVectorCompare(pParse, pExpr, target, op, p5);
103513      }else{
103514        r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
103515        r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
103516        codeCompare(pParse, pLeft, pExpr->pRight, op,
103517            r1, r2, inReg, SQLITE_STOREP2 | p5,
103518            ExprHasProperty(pExpr,EP_Commuted));
103519        assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
103520        assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
103521        assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
103522        assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
103523        assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
103524        assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
103525        testcase( regFree1==0 );
103526        testcase( regFree2==0 );
103527      }
103528      break;
103529    }
103530    case TK_AND:
103531    case TK_OR:
103532    case TK_PLUS:
103533    case TK_STAR:
103534    case TK_MINUS:
103535    case TK_REM:
103536    case TK_BITAND:
103537    case TK_BITOR:
103538    case TK_SLASH:
103539    case TK_LSHIFT:
103540    case TK_RSHIFT:
103541    case TK_CONCAT: {
103542      assert( TK_AND==OP_And );            testcase( op==TK_AND );
103543      assert( TK_OR==OP_Or );              testcase( op==TK_OR );
103544      assert( TK_PLUS==OP_Add );           testcase( op==TK_PLUS );
103545      assert( TK_MINUS==OP_Subtract );     testcase( op==TK_MINUS );
103546      assert( TK_REM==OP_Remainder );      testcase( op==TK_REM );
103547      assert( TK_BITAND==OP_BitAnd );      testcase( op==TK_BITAND );
103548      assert( TK_BITOR==OP_BitOr );        testcase( op==TK_BITOR );
103549      assert( TK_SLASH==OP_Divide );       testcase( op==TK_SLASH );
103550      assert( TK_LSHIFT==OP_ShiftLeft );   testcase( op==TK_LSHIFT );
103551      assert( TK_RSHIFT==OP_ShiftRight );  testcase( op==TK_RSHIFT );
103552      assert( TK_CONCAT==OP_Concat );      testcase( op==TK_CONCAT );
103553      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
103554      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
103555      sqlite3VdbeAddOp3(v, op, r2, r1, target);
103556      testcase( regFree1==0 );
103557      testcase( regFree2==0 );
103558      break;
103559    }
103560    case TK_UMINUS: {
103561      Expr *pLeft = pExpr->pLeft;
103562      assert( pLeft );
103563      if( pLeft->op==TK_INTEGER ){
103564        codeInteger(pParse, pLeft, 1, target);
103565        return target;
103566#ifndef SQLITE_OMIT_FLOATING_POINT
103567      }else if( pLeft->op==TK_FLOAT ){
103568        assert( !ExprHasProperty(pExpr, EP_IntValue) );
103569        codeReal(v, pLeft->u.zToken, 1, target);
103570        return target;
103571#endif
103572      }else{
103573        tempX.op = TK_INTEGER;
103574        tempX.flags = EP_IntValue|EP_TokenOnly;
103575        tempX.u.iValue = 0;
103576        ExprClearVVAProperties(&tempX);
103577        r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
103578        r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
103579        sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
103580        testcase( regFree2==0 );
103581      }
103582      break;
103583    }
103584    case TK_BITNOT:
103585    case TK_NOT: {
103586      assert( TK_BITNOT==OP_BitNot );   testcase( op==TK_BITNOT );
103587      assert( TK_NOT==OP_Not );         testcase( op==TK_NOT );
103588      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
103589      testcase( regFree1==0 );
103590      sqlite3VdbeAddOp2(v, op, r1, inReg);
103591      break;
103592    }
103593    case TK_TRUTH: {
103594      int isTrue;    /* IS TRUE or IS NOT TRUE */
103595      int bNormal;   /* IS TRUE or IS FALSE */
103596      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
103597      testcase( regFree1==0 );
103598      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
103599      bNormal = pExpr->op2==TK_IS;
103600      testcase( isTrue && bNormal);
103601      testcase( !isTrue && bNormal);
103602      sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal);
103603      break;
103604    }
103605    case TK_ISNULL:
103606    case TK_NOTNULL: {
103607      int addr;
103608      assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
103609      assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
103610      sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
103611      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
103612      testcase( regFree1==0 );
103613      addr = sqlite3VdbeAddOp1(v, op, r1);
103614      VdbeCoverageIf(v, op==TK_ISNULL);
103615      VdbeCoverageIf(v, op==TK_NOTNULL);
103616      sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
103617      sqlite3VdbeJumpHere(v, addr);
103618      break;
103619    }
103620    case TK_AGG_FUNCTION: {
103621      AggInfo *pInfo = pExpr->pAggInfo;
103622      if( pInfo==0
103623       || NEVER(pExpr->iAgg<0)
103624       || NEVER(pExpr->iAgg>=pInfo->nFunc)
103625      ){
103626        assert( !ExprHasProperty(pExpr, EP_IntValue) );
103627        sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
103628      }else{
103629        return pInfo->aFunc[pExpr->iAgg].iMem;
103630      }
103631      break;
103632    }
103633    case TK_FUNCTION: {
103634      ExprList *pFarg;       /* List of function arguments */
103635      int nFarg;             /* Number of function arguments */
103636      FuncDef *pDef;         /* The function definition object */
103637      const char *zId;       /* The function name */
103638      u32 constMask = 0;     /* Mask of function arguments that are constant */
103639      int i;                 /* Loop counter */
103640      sqlite3 *db = pParse->db;  /* The database connection */
103641      u8 enc = ENC(db);      /* The text encoding used by this database */
103642      CollSeq *pColl = 0;    /* A collating sequence */
103643
103644#ifndef SQLITE_OMIT_WINDOWFUNC
103645      if( ExprHasProperty(pExpr, EP_WinFunc) ){
103646        return pExpr->y.pWin->regResult;
103647      }
103648#endif
103649
103650      if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
103651        /* SQL functions can be expensive. So try to avoid running them
103652        ** multiple times if we know they always give the same result */
103653        return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
103654      }
103655      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
103656      assert( !ExprHasProperty(pExpr, EP_TokenOnly) );
103657      pFarg = pExpr->x.pList;
103658      nFarg = pFarg ? pFarg->nExpr : 0;
103659      assert( !ExprHasProperty(pExpr, EP_IntValue) );
103660      zId = pExpr->u.zToken;
103661      pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
103662#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
103663      if( pDef==0 && pParse->explain ){
103664        pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0);
103665      }
103666#endif
103667      if( pDef==0 || pDef->xFinalize!=0 ){
103668        sqlite3ErrorMsg(pParse, "unknown function: %s()", zId);
103669        break;
103670      }
103671      if( pDef->funcFlags & SQLITE_FUNC_INLINE ){
103672        assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 );
103673        assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 );
103674        return exprCodeInlineFunction(pParse, pFarg,
103675             SQLITE_PTR_TO_INT(pDef->pUserData), target);
103676      }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE) ){
103677        sqlite3ExprFunctionUsable(pParse, pExpr, pDef);
103678      }
103679
103680      for(i=0; i<nFarg; i++){
103681        if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
103682          testcase( i==31 );
103683          constMask |= MASKBIT32(i);
103684        }
103685        if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
103686          pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
103687        }
103688      }
103689      if( pFarg ){
103690        if( constMask ){
103691          r1 = pParse->nMem+1;
103692          pParse->nMem += nFarg;
103693        }else{
103694          r1 = sqlite3GetTempRange(pParse, nFarg);
103695        }
103696
103697        /* For length() and typeof() functions with a column argument,
103698        ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
103699        ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
103700        ** loading.
103701        */
103702        if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
103703          u8 exprOp;
103704          assert( nFarg==1 );
103705          assert( pFarg->a[0].pExpr!=0 );
103706          exprOp = pFarg->a[0].pExpr->op;
103707          if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
103708            assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
103709            assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
103710            testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
103711            pFarg->a[0].pExpr->op2 =
103712                  pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
103713          }
103714        }
103715
103716        sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
103717                                SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
103718      }else{
103719        r1 = 0;
103720      }
103721#ifndef SQLITE_OMIT_VIRTUALTABLE
103722      /* Possibly overload the function if the first argument is
103723      ** a virtual table column.
103724      **
103725      ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
103726      ** second argument, not the first, as the argument to test to
103727      ** see if it is a column in a virtual table.  This is done because
103728      ** the left operand of infix functions (the operand we want to
103729      ** control overloading) ends up as the second argument to the
103730      ** function.  The expression "A glob B" is equivalent to
103731      ** "glob(B,A).  We want to use the A in "A glob B" to test
103732      ** for function overloading.  But we use the B term in "glob(B,A)".
103733      */
103734      if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc) ){
103735        pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
103736      }else if( nFarg>0 ){
103737        pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
103738      }
103739#endif
103740      if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
103741        if( !pColl ) pColl = db->pDfltColl;
103742        sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
103743      }
103744#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
103745      if( pDef->funcFlags & SQLITE_FUNC_OFFSET ){
103746        Expr *pArg = pFarg->a[0].pExpr;
103747        if( pArg->op==TK_COLUMN ){
103748          sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
103749        }else{
103750          sqlite3VdbeAddOp2(v, OP_Null, 0, target);
103751        }
103752      }else
103753#endif
103754      {
103755        sqlite3VdbeAddFunctionCall(pParse, constMask, r1, target, nFarg,
103756                                   pDef, pExpr->op2);
103757      }
103758      if( nFarg ){
103759        if( constMask==0 ){
103760          sqlite3ReleaseTempRange(pParse, r1, nFarg);
103761        }else{
103762          sqlite3VdbeReleaseRegisters(pParse, r1, nFarg, constMask, 1);
103763        }
103764      }
103765      return target;
103766    }
103767#ifndef SQLITE_OMIT_SUBQUERY
103768    case TK_EXISTS:
103769    case TK_SELECT: {
103770      int nCol;
103771      testcase( op==TK_EXISTS );
103772      testcase( op==TK_SELECT );
103773      if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){
103774        sqlite3SubselectError(pParse, nCol, 1);
103775      }else{
103776        return sqlite3CodeSubselect(pParse, pExpr);
103777      }
103778      break;
103779    }
103780    case TK_SELECT_COLUMN: {
103781      int n;
103782      if( pExpr->pLeft->iTable==0 ){
103783        pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft);
103784      }
103785      assert( pExpr->iTable==0 || pExpr->pLeft->op==TK_SELECT );
103786      if( pExpr->iTable!=0
103787       && pExpr->iTable!=(n = sqlite3ExprVectorSize(pExpr->pLeft))
103788      ){
103789        sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
103790                                pExpr->iTable, n);
103791      }
103792      return pExpr->pLeft->iTable + pExpr->iColumn;
103793    }
103794    case TK_IN: {
103795      int destIfFalse = sqlite3VdbeMakeLabel(pParse);
103796      int destIfNull = sqlite3VdbeMakeLabel(pParse);
103797      sqlite3VdbeAddOp2(v, OP_Null, 0, target);
103798      sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
103799      sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
103800      sqlite3VdbeResolveLabel(v, destIfFalse);
103801      sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
103802      sqlite3VdbeResolveLabel(v, destIfNull);
103803      return target;
103804    }
103805#endif /* SQLITE_OMIT_SUBQUERY */
103806
103807
103808    /*
103809    **    x BETWEEN y AND z
103810    **
103811    ** This is equivalent to
103812    **
103813    **    x>=y AND x<=z
103814    **
103815    ** X is stored in pExpr->pLeft.
103816    ** Y is stored in pExpr->pList->a[0].pExpr.
103817    ** Z is stored in pExpr->pList->a[1].pExpr.
103818    */
103819    case TK_BETWEEN: {
103820      exprCodeBetween(pParse, pExpr, target, 0, 0);
103821      return target;
103822    }
103823    case TK_SPAN:
103824    case TK_COLLATE:
103825    case TK_UPLUS: {
103826      pExpr = pExpr->pLeft;
103827      goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
103828    }
103829
103830    case TK_TRIGGER: {
103831      /* If the opcode is TK_TRIGGER, then the expression is a reference
103832      ** to a column in the new.* or old.* pseudo-tables available to
103833      ** trigger programs. In this case Expr.iTable is set to 1 for the
103834      ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
103835      ** is set to the column of the pseudo-table to read, or to -1 to
103836      ** read the rowid field.
103837      **
103838      ** The expression is implemented using an OP_Param opcode. The p1
103839      ** parameter is set to 0 for an old.rowid reference, or to (i+1)
103840      ** to reference another column of the old.* pseudo-table, where
103841      ** i is the index of the column. For a new.rowid reference, p1 is
103842      ** set to (n+1), where n is the number of columns in each pseudo-table.
103843      ** For a reference to any other column in the new.* pseudo-table, p1
103844      ** is set to (n+2+i), where n and i are as defined previously. For
103845      ** example, if the table on which triggers are being fired is
103846      ** declared as:
103847      **
103848      **   CREATE TABLE t1(a, b);
103849      **
103850      ** Then p1 is interpreted as follows:
103851      **
103852      **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
103853      **   p1==1   ->    old.a         p1==4   ->    new.a
103854      **   p1==2   ->    old.b         p1==5   ->    new.b
103855      */
103856      Table *pTab = pExpr->y.pTab;
103857      int iCol = pExpr->iColumn;
103858      int p1 = pExpr->iTable * (pTab->nCol+1) + 1
103859                     + sqlite3TableColumnToStorage(pTab, iCol);
103860
103861      assert( pExpr->iTable==0 || pExpr->iTable==1 );
103862      assert( iCol>=-1 && iCol<pTab->nCol );
103863      assert( pTab->iPKey<0 || iCol!=pTab->iPKey );
103864      assert( p1>=0 && p1<(pTab->nCol*2+2) );
103865
103866      sqlite3VdbeAddOp2(v, OP_Param, p1, target);
103867      VdbeComment((v, "r[%d]=%s.%s", target,
103868        (pExpr->iTable ? "new" : "old"),
103869        (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zName)
103870      ));
103871
103872#ifndef SQLITE_OMIT_FLOATING_POINT
103873      /* If the column has REAL affinity, it may currently be stored as an
103874      ** integer. Use OP_RealAffinity to make sure it is really real.
103875      **
103876      ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
103877      ** floating point when extracting it from the record.  */
103878      if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
103879        sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
103880      }
103881#endif
103882      break;
103883    }
103884
103885    case TK_VECTOR: {
103886      sqlite3ErrorMsg(pParse, "row value misused");
103887      break;
103888    }
103889
103890    /* TK_IF_NULL_ROW Expr nodes are inserted ahead of expressions
103891    ** that derive from the right-hand table of a LEFT JOIN.  The
103892    ** Expr.iTable value is the table number for the right-hand table.
103893    ** The expression is only evaluated if that table is not currently
103894    ** on a LEFT JOIN NULL row.
103895    */
103896    case TK_IF_NULL_ROW: {
103897      int addrINR;
103898      u8 okConstFactor = pParse->okConstFactor;
103899      addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
103900      /* Temporarily disable factoring of constant expressions, since
103901      ** even though expressions may appear to be constant, they are not
103902      ** really constant because they originate from the right-hand side
103903      ** of a LEFT JOIN. */
103904      pParse->okConstFactor = 0;
103905      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
103906      pParse->okConstFactor = okConstFactor;
103907      sqlite3VdbeJumpHere(v, addrINR);
103908      sqlite3VdbeChangeP3(v, addrINR, inReg);
103909      break;
103910    }
103911
103912    /*
103913    ** Form A:
103914    **   CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
103915    **
103916    ** Form B:
103917    **   CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
103918    **
103919    ** Form A is can be transformed into the equivalent form B as follows:
103920    **   CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
103921    **        WHEN x=eN THEN rN ELSE y END
103922    **
103923    ** X (if it exists) is in pExpr->pLeft.
103924    ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
103925    ** odd.  The Y is also optional.  If the number of elements in x.pList
103926    ** is even, then Y is omitted and the "otherwise" result is NULL.
103927    ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
103928    **
103929    ** The result of the expression is the Ri for the first matching Ei,
103930    ** or if there is no matching Ei, the ELSE term Y, or if there is
103931    ** no ELSE term, NULL.
103932    */
103933    case TK_CASE: {
103934      int endLabel;                     /* GOTO label for end of CASE stmt */
103935      int nextCase;                     /* GOTO label for next WHEN clause */
103936      int nExpr;                        /* 2x number of WHEN terms */
103937      int i;                            /* Loop counter */
103938      ExprList *pEList;                 /* List of WHEN terms */
103939      struct ExprList_item *aListelem;  /* Array of WHEN terms */
103940      Expr opCompare;                   /* The X==Ei expression */
103941      Expr *pX;                         /* The X expression */
103942      Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
103943      Expr *pDel = 0;
103944      sqlite3 *db = pParse->db;
103945
103946      assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
103947      assert(pExpr->x.pList->nExpr > 0);
103948      pEList = pExpr->x.pList;
103949      aListelem = pEList->a;
103950      nExpr = pEList->nExpr;
103951      endLabel = sqlite3VdbeMakeLabel(pParse);
103952      if( (pX = pExpr->pLeft)!=0 ){
103953        pDel = sqlite3ExprDup(db, pX, 0);
103954        if( db->mallocFailed ){
103955          sqlite3ExprDelete(db, pDel);
103956          break;
103957        }
103958        testcase( pX->op==TK_COLUMN );
103959        exprToRegister(pDel, exprCodeVector(pParse, pDel, &regFree1));
103960        testcase( regFree1==0 );
103961        memset(&opCompare, 0, sizeof(opCompare));
103962        opCompare.op = TK_EQ;
103963        opCompare.pLeft = pDel;
103964        pTest = &opCompare;
103965        /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
103966        ** The value in regFree1 might get SCopy-ed into the file result.
103967        ** So make sure that the regFree1 register is not reused for other
103968        ** purposes and possibly overwritten.  */
103969        regFree1 = 0;
103970      }
103971      for(i=0; i<nExpr-1; i=i+2){
103972        if( pX ){
103973          assert( pTest!=0 );
103974          opCompare.pRight = aListelem[i].pExpr;
103975        }else{
103976          pTest = aListelem[i].pExpr;
103977        }
103978        nextCase = sqlite3VdbeMakeLabel(pParse);
103979        testcase( pTest->op==TK_COLUMN );
103980        sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
103981        testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
103982        sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
103983        sqlite3VdbeGoto(v, endLabel);
103984        sqlite3VdbeResolveLabel(v, nextCase);
103985      }
103986      if( (nExpr&1)!=0 ){
103987        sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
103988      }else{
103989        sqlite3VdbeAddOp2(v, OP_Null, 0, target);
103990      }
103991      sqlite3ExprDelete(db, pDel);
103992      setDoNotMergeFlagOnCopy(v);
103993      sqlite3VdbeResolveLabel(v, endLabel);
103994      break;
103995    }
103996#ifndef SQLITE_OMIT_TRIGGER
103997    case TK_RAISE: {
103998      assert( pExpr->affExpr==OE_Rollback
103999           || pExpr->affExpr==OE_Abort
104000           || pExpr->affExpr==OE_Fail
104001           || pExpr->affExpr==OE_Ignore
104002      );
104003      if( !pParse->pTriggerTab && !pParse->nested ){
104004        sqlite3ErrorMsg(pParse,
104005                       "RAISE() may only be used within a trigger-program");
104006        return 0;
104007      }
104008      if( pExpr->affExpr==OE_Abort ){
104009        sqlite3MayAbort(pParse);
104010      }
104011      assert( !ExprHasProperty(pExpr, EP_IntValue) );
104012      if( pExpr->affExpr==OE_Ignore ){
104013        sqlite3VdbeAddOp4(
104014            v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
104015        VdbeCoverage(v);
104016      }else{
104017        sqlite3HaltConstraint(pParse,
104018             pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR,
104019             pExpr->affExpr, pExpr->u.zToken, 0, 0);
104020      }
104021
104022      break;
104023    }
104024#endif
104025  }
104026  sqlite3ReleaseTempReg(pParse, regFree1);
104027  sqlite3ReleaseTempReg(pParse, regFree2);
104028  return inReg;
104029}
104030
104031/*
104032** Generate code that will evaluate expression pExpr just one time
104033** per prepared statement execution.
104034**
104035** If the expression uses functions (that might throw an exception) then
104036** guard them with an OP_Once opcode to ensure that the code is only executed
104037** once. If no functions are involved, then factor the code out and put it at
104038** the end of the prepared statement in the initialization section.
104039**
104040** If regDest>=0 then the result is always stored in that register and the
104041** result is not reusable.  If regDest<0 then this routine is free to
104042** store the value whereever it wants.  The register where the expression
104043** is stored is returned.  When regDest<0, two identical expressions might
104044** code to the same register, if they do not contain function calls and hence
104045** are factored out into the initialization section at the end of the
104046** prepared statement.
104047*/
104048SQLITE_PRIVATE int sqlite3ExprCodeRunJustOnce(
104049  Parse *pParse,    /* Parsing context */
104050  Expr *pExpr,      /* The expression to code when the VDBE initializes */
104051  int regDest       /* Store the value in this register */
104052){
104053  ExprList *p;
104054  assert( ConstFactorOk(pParse) );
104055  p = pParse->pConstExpr;
104056  if( regDest<0 && p ){
104057    struct ExprList_item *pItem;
104058    int i;
104059    for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
104060      if( pItem->reusable && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0 ){
104061        return pItem->u.iConstExprReg;
104062      }
104063    }
104064  }
104065  pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
104066  if( pExpr!=0 && ExprHasProperty(pExpr, EP_HasFunc) ){
104067    Vdbe *v = pParse->pVdbe;
104068    int addr;
104069    assert( v );
104070    addr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
104071    pParse->okConstFactor = 0;
104072    if( !pParse->db->mallocFailed ){
104073      if( regDest<0 ) regDest = ++pParse->nMem;
104074      sqlite3ExprCode(pParse, pExpr, regDest);
104075    }
104076    pParse->okConstFactor = 1;
104077    sqlite3ExprDelete(pParse->db, pExpr);
104078    sqlite3VdbeJumpHere(v, addr);
104079  }else{
104080    p = sqlite3ExprListAppend(pParse, p, pExpr);
104081    if( p ){
104082       struct ExprList_item *pItem = &p->a[p->nExpr-1];
104083       pItem->reusable = regDest<0;
104084       if( regDest<0 ) regDest = ++pParse->nMem;
104085       pItem->u.iConstExprReg = regDest;
104086    }
104087    pParse->pConstExpr = p;
104088  }
104089  return regDest;
104090}
104091
104092/*
104093** Generate code to evaluate an expression and store the results
104094** into a register.  Return the register number where the results
104095** are stored.
104096**
104097** If the register is a temporary register that can be deallocated,
104098** then write its number into *pReg.  If the result register is not
104099** a temporary, then set *pReg to zero.
104100**
104101** If pExpr is a constant, then this routine might generate this
104102** code to fill the register in the initialization section of the
104103** VDBE program, in order to factor it out of the evaluation loop.
104104*/
104105SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
104106  int r2;
104107  pExpr = sqlite3ExprSkipCollateAndLikely(pExpr);
104108  if( ConstFactorOk(pParse)
104109   && pExpr->op!=TK_REGISTER
104110   && sqlite3ExprIsConstantNotJoin(pExpr)
104111  ){
104112    *pReg  = 0;
104113    r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
104114  }else{
104115    int r1 = sqlite3GetTempReg(pParse);
104116    r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
104117    if( r2==r1 ){
104118      *pReg = r1;
104119    }else{
104120      sqlite3ReleaseTempReg(pParse, r1);
104121      *pReg = 0;
104122    }
104123  }
104124  return r2;
104125}
104126
104127/*
104128** Generate code that will evaluate expression pExpr and store the
104129** results in register target.  The results are guaranteed to appear
104130** in register target.
104131*/
104132SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
104133  int inReg;
104134
104135  assert( pExpr==0 || !ExprHasVVAProperty(pExpr,EP_Immutable) );
104136  assert( target>0 && target<=pParse->nMem );
104137  inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
104138  assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
104139  if( inReg!=target && pParse->pVdbe ){
104140    u8 op;
104141    if( ExprHasProperty(pExpr,EP_Subquery) ){
104142      op = OP_Copy;
104143    }else{
104144      op = OP_SCopy;
104145    }
104146    sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target);
104147  }
104148}
104149
104150/*
104151** Make a transient copy of expression pExpr and then code it using
104152** sqlite3ExprCode().  This routine works just like sqlite3ExprCode()
104153** except that the input expression is guaranteed to be unchanged.
104154*/
104155SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
104156  sqlite3 *db = pParse->db;
104157  pExpr = sqlite3ExprDup(db, pExpr, 0);
104158  if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
104159  sqlite3ExprDelete(db, pExpr);
104160}
104161
104162/*
104163** Generate code that will evaluate expression pExpr and store the
104164** results in register target.  The results are guaranteed to appear
104165** in register target.  If the expression is constant, then this routine
104166** might choose to code the expression at initialization time.
104167*/
104168SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
104169  if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
104170    sqlite3ExprCodeRunJustOnce(pParse, pExpr, target);
104171  }else{
104172    sqlite3ExprCodeCopy(pParse, pExpr, target);
104173  }
104174}
104175
104176/*
104177** Generate code that pushes the value of every element of the given
104178** expression list into a sequence of registers beginning at target.
104179**
104180** Return the number of elements evaluated.  The number returned will
104181** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
104182** is defined.
104183**
104184** The SQLITE_ECEL_DUP flag prevents the arguments from being
104185** filled using OP_SCopy.  OP_Copy must be used instead.
104186**
104187** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
104188** factored out into initialization code.
104189**
104190** The SQLITE_ECEL_REF flag means that expressions in the list with
104191** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
104192** in registers at srcReg, and so the value can be copied from there.
104193** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0
104194** are simply omitted rather than being copied from srcReg.
104195*/
104196SQLITE_PRIVATE int sqlite3ExprCodeExprList(
104197  Parse *pParse,     /* Parsing context */
104198  ExprList *pList,   /* The expression list to be coded */
104199  int target,        /* Where to write results */
104200  int srcReg,        /* Source registers if SQLITE_ECEL_REF */
104201  u8 flags           /* SQLITE_ECEL_* flags */
104202){
104203  struct ExprList_item *pItem;
104204  int i, j, n;
104205  u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
104206  Vdbe *v = pParse->pVdbe;
104207  assert( pList!=0 );
104208  assert( target>0 );
104209  assert( pParse->pVdbe!=0 );  /* Never gets this far otherwise */
104210  n = pList->nExpr;
104211  if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
104212  for(pItem=pList->a, i=0; i<n; i++, pItem++){
104213    Expr *pExpr = pItem->pExpr;
104214#ifdef SQLITE_ENABLE_SORTER_REFERENCES
104215    if( pItem->bSorterRef ){
104216      i--;
104217      n--;
104218    }else
104219#endif
104220    if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
104221      if( flags & SQLITE_ECEL_OMITREF ){
104222        i--;
104223        n--;
104224      }else{
104225        sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
104226      }
104227    }else if( (flags & SQLITE_ECEL_FACTOR)!=0
104228           && sqlite3ExprIsConstantNotJoin(pExpr)
104229    ){
104230      sqlite3ExprCodeRunJustOnce(pParse, pExpr, target+i);
104231    }else{
104232      int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
104233      if( inReg!=target+i ){
104234        VdbeOp *pOp;
104235        if( copyOp==OP_Copy
104236         && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
104237         && pOp->p1+pOp->p3+1==inReg
104238         && pOp->p2+pOp->p3+1==target+i
104239         && pOp->p5==0  /* The do-not-merge flag must be clear */
104240        ){
104241          pOp->p3++;
104242        }else{
104243          sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
104244        }
104245      }
104246    }
104247  }
104248  return n;
104249}
104250
104251/*
104252** Generate code for a BETWEEN operator.
104253**
104254**    x BETWEEN y AND z
104255**
104256** The above is equivalent to
104257**
104258**    x>=y AND x<=z
104259**
104260** Code it as such, taking care to do the common subexpression
104261** elimination of x.
104262**
104263** The xJumpIf parameter determines details:
104264**
104265**    NULL:                   Store the boolean result in reg[dest]
104266**    sqlite3ExprIfTrue:      Jump to dest if true
104267**    sqlite3ExprIfFalse:     Jump to dest if false
104268**
104269** The jumpIfNull parameter is ignored if xJumpIf is NULL.
104270*/
104271static void exprCodeBetween(
104272  Parse *pParse,    /* Parsing and code generating context */
104273  Expr *pExpr,      /* The BETWEEN expression */
104274  int dest,         /* Jump destination or storage location */
104275  void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
104276  int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
104277){
104278  Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
104279  Expr compLeft;    /* The  x>=y  term */
104280  Expr compRight;   /* The  x<=z  term */
104281  int regFree1 = 0; /* Temporary use register */
104282  Expr *pDel = 0;
104283  sqlite3 *db = pParse->db;
104284
104285  memset(&compLeft, 0, sizeof(Expr));
104286  memset(&compRight, 0, sizeof(Expr));
104287  memset(&exprAnd, 0, sizeof(Expr));
104288
104289  assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
104290  pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
104291  if( db->mallocFailed==0 ){
104292    exprAnd.op = TK_AND;
104293    exprAnd.pLeft = &compLeft;
104294    exprAnd.pRight = &compRight;
104295    compLeft.op = TK_GE;
104296    compLeft.pLeft = pDel;
104297    compLeft.pRight = pExpr->x.pList->a[0].pExpr;
104298    compRight.op = TK_LE;
104299    compRight.pLeft = pDel;
104300    compRight.pRight = pExpr->x.pList->a[1].pExpr;
104301    exprToRegister(pDel, exprCodeVector(pParse, pDel, &regFree1));
104302    if( xJump ){
104303      xJump(pParse, &exprAnd, dest, jumpIfNull);
104304    }else{
104305      /* Mark the expression is being from the ON or USING clause of a join
104306      ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
104307      ** it into the Parse.pConstExpr list.  We should use a new bit for this,
104308      ** for clarity, but we are out of bits in the Expr.flags field so we
104309      ** have to reuse the EP_FromJoin bit.  Bummer. */
104310      pDel->flags |= EP_FromJoin;
104311      sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
104312    }
104313    sqlite3ReleaseTempReg(pParse, regFree1);
104314  }
104315  sqlite3ExprDelete(db, pDel);
104316
104317  /* Ensure adequate test coverage */
104318  testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull==0 && regFree1==0 );
104319  testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull==0 && regFree1!=0 );
104320  testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull!=0 && regFree1==0 );
104321  testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull!=0 && regFree1!=0 );
104322  testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
104323  testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 );
104324  testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 );
104325  testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 );
104326  testcase( xJump==0 );
104327}
104328
104329/*
104330** Generate code for a boolean expression such that a jump is made
104331** to the label "dest" if the expression is true but execution
104332** continues straight thru if the expression is false.
104333**
104334** If the expression evaluates to NULL (neither true nor false), then
104335** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
104336**
104337** This code depends on the fact that certain token values (ex: TK_EQ)
104338** are the same as opcode values (ex: OP_Eq) that implement the corresponding
104339** operation.  Special comments in vdbe.c and the mkopcodeh.awk script in
104340** the make process cause these values to align.  Assert()s in the code
104341** below verify that the numbers are aligned correctly.
104342*/
104343SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
104344  Vdbe *v = pParse->pVdbe;
104345  int op = 0;
104346  int regFree1 = 0;
104347  int regFree2 = 0;
104348  int r1, r2;
104349
104350  assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
104351  if( NEVER(v==0) )     return;  /* Existence of VDBE checked by caller */
104352  if( NEVER(pExpr==0) ) return;  /* No way this can happen */
104353  assert( !ExprHasVVAProperty(pExpr, EP_Immutable) );
104354  op = pExpr->op;
104355  switch( op ){
104356    case TK_AND:
104357    case TK_OR: {
104358      Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
104359      if( pAlt!=pExpr ){
104360        sqlite3ExprIfTrue(pParse, pAlt, dest, jumpIfNull);
104361      }else if( op==TK_AND ){
104362        int d2 = sqlite3VdbeMakeLabel(pParse);
104363        testcase( jumpIfNull==0 );
104364        sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
104365                           jumpIfNull^SQLITE_JUMPIFNULL);
104366        sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
104367        sqlite3VdbeResolveLabel(v, d2);
104368      }else{
104369        testcase( jumpIfNull==0 );
104370        sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
104371        sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
104372      }
104373      break;
104374    }
104375    case TK_NOT: {
104376      testcase( jumpIfNull==0 );
104377      sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
104378      break;
104379    }
104380    case TK_TRUTH: {
104381      int isNot;      /* IS NOT TRUE or IS NOT FALSE */
104382      int isTrue;     /* IS TRUE or IS NOT TRUE */
104383      testcase( jumpIfNull==0 );
104384      isNot = pExpr->op2==TK_ISNOT;
104385      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
104386      testcase( isTrue && isNot );
104387      testcase( !isTrue && isNot );
104388      if( isTrue ^ isNot ){
104389        sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
104390                          isNot ? SQLITE_JUMPIFNULL : 0);
104391      }else{
104392        sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
104393                           isNot ? SQLITE_JUMPIFNULL : 0);
104394      }
104395      break;
104396    }
104397    case TK_IS:
104398    case TK_ISNOT:
104399      testcase( op==TK_IS );
104400      testcase( op==TK_ISNOT );
104401      op = (op==TK_IS) ? TK_EQ : TK_NE;
104402      jumpIfNull = SQLITE_NULLEQ;
104403      /* Fall thru */
104404    case TK_LT:
104405    case TK_LE:
104406    case TK_GT:
104407    case TK_GE:
104408    case TK_NE:
104409    case TK_EQ: {
104410      if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
104411      testcase( jumpIfNull==0 );
104412      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
104413      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
104414      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
104415                  r1, r2, dest, jumpIfNull, ExprHasProperty(pExpr,EP_Commuted));
104416      assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
104417      assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
104418      assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
104419      assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
104420      assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
104421      VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
104422      VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
104423      assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
104424      VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
104425      VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
104426      testcase( regFree1==0 );
104427      testcase( regFree2==0 );
104428      break;
104429    }
104430    case TK_ISNULL:
104431    case TK_NOTNULL: {
104432      assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
104433      assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
104434      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
104435      sqlite3VdbeAddOp2(v, op, r1, dest);
104436      VdbeCoverageIf(v, op==TK_ISNULL);
104437      VdbeCoverageIf(v, op==TK_NOTNULL);
104438      testcase( regFree1==0 );
104439      break;
104440    }
104441    case TK_BETWEEN: {
104442      testcase( jumpIfNull==0 );
104443      exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull);
104444      break;
104445    }
104446#ifndef SQLITE_OMIT_SUBQUERY
104447    case TK_IN: {
104448      int destIfFalse = sqlite3VdbeMakeLabel(pParse);
104449      int destIfNull = jumpIfNull ? dest : destIfFalse;
104450      sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
104451      sqlite3VdbeGoto(v, dest);
104452      sqlite3VdbeResolveLabel(v, destIfFalse);
104453      break;
104454    }
104455#endif
104456    default: {
104457    default_expr:
104458      if( ExprAlwaysTrue(pExpr) ){
104459        sqlite3VdbeGoto(v, dest);
104460      }else if( ExprAlwaysFalse(pExpr) ){
104461        /* No-op */
104462      }else{
104463        r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
104464        sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
104465        VdbeCoverage(v);
104466        testcase( regFree1==0 );
104467        testcase( jumpIfNull==0 );
104468      }
104469      break;
104470    }
104471  }
104472  sqlite3ReleaseTempReg(pParse, regFree1);
104473  sqlite3ReleaseTempReg(pParse, regFree2);
104474}
104475
104476/*
104477** Generate code for a boolean expression such that a jump is made
104478** to the label "dest" if the expression is false but execution
104479** continues straight thru if the expression is true.
104480**
104481** If the expression evaluates to NULL (neither true nor false) then
104482** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
104483** is 0.
104484*/
104485SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
104486  Vdbe *v = pParse->pVdbe;
104487  int op = 0;
104488  int regFree1 = 0;
104489  int regFree2 = 0;
104490  int r1, r2;
104491
104492  assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
104493  if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
104494  if( pExpr==0 )    return;
104495  assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
104496
104497  /* The value of pExpr->op and op are related as follows:
104498  **
104499  **       pExpr->op            op
104500  **       ---------          ----------
104501  **       TK_ISNULL          OP_NotNull
104502  **       TK_NOTNULL         OP_IsNull
104503  **       TK_NE              OP_Eq
104504  **       TK_EQ              OP_Ne
104505  **       TK_GT              OP_Le
104506  **       TK_LE              OP_Gt
104507  **       TK_GE              OP_Lt
104508  **       TK_LT              OP_Ge
104509  **
104510  ** For other values of pExpr->op, op is undefined and unused.
104511  ** The value of TK_ and OP_ constants are arranged such that we
104512  ** can compute the mapping above using the following expression.
104513  ** Assert()s verify that the computation is correct.
104514  */
104515  op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
104516
104517  /* Verify correct alignment of TK_ and OP_ constants
104518  */
104519  assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
104520  assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
104521  assert( pExpr->op!=TK_NE || op==OP_Eq );
104522  assert( pExpr->op!=TK_EQ || op==OP_Ne );
104523  assert( pExpr->op!=TK_LT || op==OP_Ge );
104524  assert( pExpr->op!=TK_LE || op==OP_Gt );
104525  assert( pExpr->op!=TK_GT || op==OP_Le );
104526  assert( pExpr->op!=TK_GE || op==OP_Lt );
104527
104528  switch( pExpr->op ){
104529    case TK_AND:
104530    case TK_OR: {
104531      Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
104532      if( pAlt!=pExpr ){
104533        sqlite3ExprIfFalse(pParse, pAlt, dest, jumpIfNull);
104534      }else if( pExpr->op==TK_AND ){
104535        testcase( jumpIfNull==0 );
104536        sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
104537        sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
104538      }else{
104539        int d2 = sqlite3VdbeMakeLabel(pParse);
104540        testcase( jumpIfNull==0 );
104541        sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
104542                          jumpIfNull^SQLITE_JUMPIFNULL);
104543        sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
104544        sqlite3VdbeResolveLabel(v, d2);
104545      }
104546      break;
104547    }
104548    case TK_NOT: {
104549      testcase( jumpIfNull==0 );
104550      sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
104551      break;
104552    }
104553    case TK_TRUTH: {
104554      int isNot;   /* IS NOT TRUE or IS NOT FALSE */
104555      int isTrue;  /* IS TRUE or IS NOT TRUE */
104556      testcase( jumpIfNull==0 );
104557      isNot = pExpr->op2==TK_ISNOT;
104558      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
104559      testcase( isTrue && isNot );
104560      testcase( !isTrue && isNot );
104561      if( isTrue ^ isNot ){
104562        /* IS TRUE and IS NOT FALSE */
104563        sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
104564                           isNot ? 0 : SQLITE_JUMPIFNULL);
104565
104566      }else{
104567        /* IS FALSE and IS NOT TRUE */
104568        sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
104569                          isNot ? 0 : SQLITE_JUMPIFNULL);
104570      }
104571      break;
104572    }
104573    case TK_IS:
104574    case TK_ISNOT:
104575      testcase( pExpr->op==TK_IS );
104576      testcase( pExpr->op==TK_ISNOT );
104577      op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
104578      jumpIfNull = SQLITE_NULLEQ;
104579      /* Fall thru */
104580    case TK_LT:
104581    case TK_LE:
104582    case TK_GT:
104583    case TK_GE:
104584    case TK_NE:
104585    case TK_EQ: {
104586      if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
104587      testcase( jumpIfNull==0 );
104588      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
104589      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
104590      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
104591                  r1, r2, dest, jumpIfNull,ExprHasProperty(pExpr,EP_Commuted));
104592      assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
104593      assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
104594      assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
104595      assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
104596      assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
104597      VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
104598      VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
104599      assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
104600      VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
104601      VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
104602      testcase( regFree1==0 );
104603      testcase( regFree2==0 );
104604      break;
104605    }
104606    case TK_ISNULL:
104607    case TK_NOTNULL: {
104608      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
104609      sqlite3VdbeAddOp2(v, op, r1, dest);
104610      testcase( op==TK_ISNULL );   VdbeCoverageIf(v, op==TK_ISNULL);
104611      testcase( op==TK_NOTNULL );  VdbeCoverageIf(v, op==TK_NOTNULL);
104612      testcase( regFree1==0 );
104613      break;
104614    }
104615    case TK_BETWEEN: {
104616      testcase( jumpIfNull==0 );
104617      exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull);
104618      break;
104619    }
104620#ifndef SQLITE_OMIT_SUBQUERY
104621    case TK_IN: {
104622      if( jumpIfNull ){
104623        sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
104624      }else{
104625        int destIfNull = sqlite3VdbeMakeLabel(pParse);
104626        sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
104627        sqlite3VdbeResolveLabel(v, destIfNull);
104628      }
104629      break;
104630    }
104631#endif
104632    default: {
104633    default_expr:
104634      if( ExprAlwaysFalse(pExpr) ){
104635        sqlite3VdbeGoto(v, dest);
104636      }else if( ExprAlwaysTrue(pExpr) ){
104637        /* no-op */
104638      }else{
104639        r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
104640        sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
104641        VdbeCoverage(v);
104642        testcase( regFree1==0 );
104643        testcase( jumpIfNull==0 );
104644      }
104645      break;
104646    }
104647  }
104648  sqlite3ReleaseTempReg(pParse, regFree1);
104649  sqlite3ReleaseTempReg(pParse, regFree2);
104650}
104651
104652/*
104653** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
104654** code generation, and that copy is deleted after code generation. This
104655** ensures that the original pExpr is unchanged.
104656*/
104657SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
104658  sqlite3 *db = pParse->db;
104659  Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
104660  if( db->mallocFailed==0 ){
104661    sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
104662  }
104663  sqlite3ExprDelete(db, pCopy);
104664}
104665
104666/*
104667** Expression pVar is guaranteed to be an SQL variable. pExpr may be any
104668** type of expression.
104669**
104670** If pExpr is a simple SQL value - an integer, real, string, blob
104671** or NULL value - then the VDBE currently being prepared is configured
104672** to re-prepare each time a new value is bound to variable pVar.
104673**
104674** Additionally, if pExpr is a simple SQL value and the value is the
104675** same as that currently bound to variable pVar, non-zero is returned.
104676** Otherwise, if the values are not the same or if pExpr is not a simple
104677** SQL value, zero is returned.
104678*/
104679static int exprCompareVariable(Parse *pParse, Expr *pVar, Expr *pExpr){
104680  int res = 0;
104681  int iVar;
104682  sqlite3_value *pL, *pR = 0;
104683
104684  sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
104685  if( pR ){
104686    iVar = pVar->iColumn;
104687    sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
104688    pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
104689    if( pL ){
104690      if( sqlite3_value_type(pL)==SQLITE_TEXT ){
104691        sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
104692      }
104693      res =  0==sqlite3MemCompare(pL, pR, 0);
104694    }
104695    sqlite3ValueFree(pR);
104696    sqlite3ValueFree(pL);
104697  }
104698
104699  return res;
104700}
104701
104702/*
104703** Do a deep comparison of two expression trees.  Return 0 if the two
104704** expressions are completely identical.  Return 1 if they differ only
104705** by a COLLATE operator at the top level.  Return 2 if there are differences
104706** other than the top-level COLLATE operator.
104707**
104708** If any subelement of pB has Expr.iTable==(-1) then it is allowed
104709** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
104710**
104711** The pA side might be using TK_REGISTER.  If that is the case and pB is
104712** not using TK_REGISTER but is otherwise equivalent, then still return 0.
104713**
104714** Sometimes this routine will return 2 even if the two expressions
104715** really are equivalent.  If we cannot prove that the expressions are
104716** identical, we return 2 just to be safe.  So if this routine
104717** returns 2, then you do not really know for certain if the two
104718** expressions are the same.  But if you get a 0 or 1 return, then you
104719** can be sure the expressions are the same.  In the places where
104720** this routine is used, it does not hurt to get an extra 2 - that
104721** just might result in some slightly slower code.  But returning
104722** an incorrect 0 or 1 could lead to a malfunction.
104723**
104724** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in
104725** pParse->pReprepare can be matched against literals in pB.  The
104726** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
104727** If pParse is NULL (the normal case) then any TK_VARIABLE term in
104728** Argument pParse should normally be NULL. If it is not NULL and pA or
104729** pB causes a return value of 2.
104730*/
104731SQLITE_PRIVATE int sqlite3ExprCompare(Parse *pParse, Expr *pA, Expr *pB, int iTab){
104732  u32 combinedFlags;
104733  if( pA==0 || pB==0 ){
104734    return pB==pA ? 0 : 2;
104735  }
104736  if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
104737    return 0;
104738  }
104739  combinedFlags = pA->flags | pB->flags;
104740  if( combinedFlags & EP_IntValue ){
104741    if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
104742      return 0;
104743    }
104744    return 2;
104745  }
104746  if( pA->op!=pB->op || pA->op==TK_RAISE ){
104747    if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
104748      return 1;
104749    }
104750    if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
104751      return 1;
104752    }
104753    return 2;
104754  }
104755  if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
104756    if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
104757      if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
104758#ifndef SQLITE_OMIT_WINDOWFUNC
104759      assert( pA->op==pB->op );
104760      if( ExprHasProperty(pA,EP_WinFunc)!=ExprHasProperty(pB,EP_WinFunc) ){
104761        return 2;
104762      }
104763      if( ExprHasProperty(pA,EP_WinFunc) ){
104764        if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
104765          return 2;
104766        }
104767      }
104768#endif
104769    }else if( pA->op==TK_NULL ){
104770      return 0;
104771    }else if( pA->op==TK_COLLATE ){
104772      if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
104773    }else if( ALWAYS(pB->u.zToken!=0) && strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
104774      return 2;
104775    }
104776  }
104777  if( (pA->flags & (EP_Distinct|EP_Commuted))
104778     != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
104779  if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
104780    if( combinedFlags & EP_xIsSelect ) return 2;
104781    if( (combinedFlags & EP_FixedCol)==0
104782     && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
104783    if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
104784    if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
104785    if( pA->op!=TK_STRING
104786     && pA->op!=TK_TRUEFALSE
104787     && ALWAYS((combinedFlags & EP_Reduced)==0)
104788    ){
104789      if( pA->iColumn!=pB->iColumn ) return 2;
104790      if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2;
104791      if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
104792        return 2;
104793      }
104794    }
104795  }
104796  return 0;
104797}
104798
104799/*
104800** Compare two ExprList objects.  Return 0 if they are identical, 1
104801** if they are certainly different, or 2 if it is not possible to
104802** determine if they are identical or not.
104803**
104804** If any subelement of pB has Expr.iTable==(-1) then it is allowed
104805** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
104806**
104807** This routine might return non-zero for equivalent ExprLists.  The
104808** only consequence will be disabled optimizations.  But this routine
104809** must never return 0 if the two ExprList objects are different, or
104810** a malfunction will result.
104811**
104812** Two NULL pointers are considered to be the same.  But a NULL pointer
104813** always differs from a non-NULL pointer.
104814*/
104815SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
104816  int i;
104817  if( pA==0 && pB==0 ) return 0;
104818  if( pA==0 || pB==0 ) return 1;
104819  if( pA->nExpr!=pB->nExpr ) return 1;
104820  for(i=0; i<pA->nExpr; i++){
104821    int res;
104822    Expr *pExprA = pA->a[i].pExpr;
104823    Expr *pExprB = pB->a[i].pExpr;
104824    if( pA->a[i].sortFlags!=pB->a[i].sortFlags ) return 1;
104825    if( (res = sqlite3ExprCompare(0, pExprA, pExprB, iTab)) ) return res;
104826  }
104827  return 0;
104828}
104829
104830/*
104831** Like sqlite3ExprCompare() except COLLATE operators at the top-level
104832** are ignored.
104833*/
104834SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr *pA, Expr *pB, int iTab){
104835  return sqlite3ExprCompare(0,
104836             sqlite3ExprSkipCollateAndLikely(pA),
104837             sqlite3ExprSkipCollateAndLikely(pB),
104838             iTab);
104839}
104840
104841/*
104842** Return non-zero if Expr p can only be true if pNN is not NULL.
104843**
104844** Or if seenNot is true, return non-zero if Expr p can only be
104845** non-NULL if pNN is not NULL
104846*/
104847static int exprImpliesNotNull(
104848  Parse *pParse,      /* Parsing context */
104849  Expr *p,            /* The expression to be checked */
104850  Expr *pNN,          /* The expression that is NOT NULL */
104851  int iTab,           /* Table being evaluated */
104852  int seenNot         /* Return true only if p can be any non-NULL value */
104853){
104854  assert( p );
104855  assert( pNN );
104856  if( sqlite3ExprCompare(pParse, p, pNN, iTab)==0 ){
104857    return pNN->op!=TK_NULL;
104858  }
104859  switch( p->op ){
104860    case TK_IN: {
104861      if( seenNot && ExprHasProperty(p, EP_xIsSelect) ) return 0;
104862      assert( ExprHasProperty(p,EP_xIsSelect)
104863           || (p->x.pList!=0 && p->x.pList->nExpr>0) );
104864      return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
104865    }
104866    case TK_BETWEEN: {
104867      ExprList *pList = p->x.pList;
104868      assert( pList!=0 );
104869      assert( pList->nExpr==2 );
104870      if( seenNot ) return 0;
104871      if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
104872       || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
104873      ){
104874        return 1;
104875      }
104876      return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
104877    }
104878    case TK_EQ:
104879    case TK_NE:
104880    case TK_LT:
104881    case TK_LE:
104882    case TK_GT:
104883    case TK_GE:
104884    case TK_PLUS:
104885    case TK_MINUS:
104886    case TK_BITOR:
104887    case TK_LSHIFT:
104888    case TK_RSHIFT:
104889    case TK_CONCAT:
104890      seenNot = 1;
104891      /* Fall thru */
104892    case TK_STAR:
104893    case TK_REM:
104894    case TK_BITAND:
104895    case TK_SLASH: {
104896      if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
104897      /* Fall thru into the next case */
104898    }
104899    case TK_SPAN:
104900    case TK_COLLATE:
104901    case TK_UPLUS:
104902    case TK_UMINUS: {
104903      return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
104904    }
104905    case TK_TRUTH: {
104906      if( seenNot ) return 0;
104907      if( p->op2!=TK_IS ) return 0;
104908      return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
104909    }
104910    case TK_BITNOT:
104911    case TK_NOT: {
104912      return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
104913    }
104914  }
104915  return 0;
104916}
104917
104918/*
104919** Return true if we can prove the pE2 will always be true if pE1 is
104920** true.  Return false if we cannot complete the proof or if pE2 might
104921** be false.  Examples:
104922**
104923**     pE1: x==5       pE2: x==5             Result: true
104924**     pE1: x>0        pE2: x==5             Result: false
104925**     pE1: x=21       pE2: x=21 OR y=43     Result: true
104926**     pE1: x!=123     pE2: x IS NOT NULL    Result: true
104927**     pE1: x!=?1      pE2: x IS NOT NULL    Result: true
104928**     pE1: x IS NULL  pE2: x IS NOT NULL    Result: false
104929**     pE1: x IS ?2    pE2: x IS NOT NULL    Reuslt: false
104930**
104931** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
104932** Expr.iTable<0 then assume a table number given by iTab.
104933**
104934** If pParse is not NULL, then the values of bound variables in pE1 are
104935** compared against literal values in pE2 and pParse->pVdbe->expmask is
104936** modified to record which bound variables are referenced.  If pParse
104937** is NULL, then false will be returned if pE1 contains any bound variables.
104938**
104939** When in doubt, return false.  Returning true might give a performance
104940** improvement.  Returning false might cause a performance reduction, but
104941** it will always give the correct answer and is hence always safe.
104942*/
104943SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Parse *pParse, Expr *pE1, Expr *pE2, int iTab){
104944  if( sqlite3ExprCompare(pParse, pE1, pE2, iTab)==0 ){
104945    return 1;
104946  }
104947  if( pE2->op==TK_OR
104948   && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
104949             || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
104950  ){
104951    return 1;
104952  }
104953  if( pE2->op==TK_NOTNULL
104954   && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
104955  ){
104956    return 1;
104957  }
104958  return 0;
104959}
104960
104961/*
104962** This is the Expr node callback for sqlite3ExprImpliesNonNullRow().
104963** If the expression node requires that the table at pWalker->iCur
104964** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
104965**
104966** This routine controls an optimization.  False positives (setting
104967** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
104968** (never setting pWalker->eCode) is a harmless missed optimization.
104969*/
104970static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
104971  testcase( pExpr->op==TK_AGG_COLUMN );
104972  testcase( pExpr->op==TK_AGG_FUNCTION );
104973  if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
104974  switch( pExpr->op ){
104975    case TK_ISNOT:
104976    case TK_ISNULL:
104977    case TK_NOTNULL:
104978    case TK_IS:
104979    case TK_OR:
104980    case TK_VECTOR:
104981    case TK_CASE:
104982    case TK_IN:
104983    case TK_FUNCTION:
104984    case TK_TRUTH:
104985      testcase( pExpr->op==TK_ISNOT );
104986      testcase( pExpr->op==TK_ISNULL );
104987      testcase( pExpr->op==TK_NOTNULL );
104988      testcase( pExpr->op==TK_IS );
104989      testcase( pExpr->op==TK_OR );
104990      testcase( pExpr->op==TK_VECTOR );
104991      testcase( pExpr->op==TK_CASE );
104992      testcase( pExpr->op==TK_IN );
104993      testcase( pExpr->op==TK_FUNCTION );
104994      testcase( pExpr->op==TK_TRUTH );
104995      return WRC_Prune;
104996    case TK_COLUMN:
104997      if( pWalker->u.iCur==pExpr->iTable ){
104998        pWalker->eCode = 1;
104999        return WRC_Abort;
105000      }
105001      return WRC_Prune;
105002
105003    case TK_AND:
105004      if( pWalker->eCode==0 ){
105005        sqlite3WalkExpr(pWalker, pExpr->pLeft);
105006        if( pWalker->eCode ){
105007          pWalker->eCode = 0;
105008          sqlite3WalkExpr(pWalker, pExpr->pRight);
105009        }
105010      }
105011      return WRC_Prune;
105012
105013    case TK_BETWEEN:
105014      if( sqlite3WalkExpr(pWalker, pExpr->pLeft)==WRC_Abort ){
105015        assert( pWalker->eCode );
105016        return WRC_Abort;
105017      }
105018      return WRC_Prune;
105019
105020    /* Virtual tables are allowed to use constraints like x=NULL.  So
105021    ** a term of the form x=y does not prove that y is not null if x
105022    ** is the column of a virtual table */
105023    case TK_EQ:
105024    case TK_NE:
105025    case TK_LT:
105026    case TK_LE:
105027    case TK_GT:
105028    case TK_GE: {
105029      Expr *pLeft = pExpr->pLeft;
105030      Expr *pRight = pExpr->pRight;
105031      testcase( pExpr->op==TK_EQ );
105032      testcase( pExpr->op==TK_NE );
105033      testcase( pExpr->op==TK_LT );
105034      testcase( pExpr->op==TK_LE );
105035      testcase( pExpr->op==TK_GT );
105036      testcase( pExpr->op==TK_GE );
105037      /* The y.pTab=0 assignment in wherecode.c always happens after the
105038      ** impliesNotNullRow() test */
105039      if( (pLeft->op==TK_COLUMN && ALWAYS(pLeft->y.pTab!=0)
105040                               && IsVirtual(pLeft->y.pTab))
105041       || (pRight->op==TK_COLUMN && ALWAYS(pRight->y.pTab!=0)
105042                               && IsVirtual(pRight->y.pTab))
105043      ){
105044        return WRC_Prune;
105045      }
105046    }
105047    default:
105048      return WRC_Continue;
105049  }
105050}
105051
105052/*
105053** Return true (non-zero) if expression p can only be true if at least
105054** one column of table iTab is non-null.  In other words, return true
105055** if expression p will always be NULL or false if every column of iTab
105056** is NULL.
105057**
105058** False negatives are acceptable.  In other words, it is ok to return
105059** zero even if expression p will never be true of every column of iTab
105060** is NULL.  A false negative is merely a missed optimization opportunity.
105061**
105062** False positives are not allowed, however.  A false positive may result
105063** in an incorrect answer.
105064**
105065** Terms of p that are marked with EP_FromJoin (and hence that come from
105066** the ON or USING clauses of LEFT JOINS) are excluded from the analysis.
105067**
105068** This routine is used to check if a LEFT JOIN can be converted into
105069** an ordinary JOIN.  The p argument is the WHERE clause.  If the WHERE
105070** clause requires that some column of the right table of the LEFT JOIN
105071** be non-NULL, then the LEFT JOIN can be safely converted into an
105072** ordinary join.
105073*/
105074SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
105075  Walker w;
105076  p = sqlite3ExprSkipCollateAndLikely(p);
105077  if( p==0 ) return 0;
105078  if( p->op==TK_NOTNULL ){
105079    p = p->pLeft;
105080  }else{
105081    while( p->op==TK_AND ){
105082      if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
105083      p = p->pRight;
105084    }
105085  }
105086  w.xExprCallback = impliesNotNullRow;
105087  w.xSelectCallback = 0;
105088  w.xSelectCallback2 = 0;
105089  w.eCode = 0;
105090  w.u.iCur = iTab;
105091  sqlite3WalkExpr(&w, p);
105092  return w.eCode;
105093}
105094
105095/*
105096** An instance of the following structure is used by the tree walker
105097** to determine if an expression can be evaluated by reference to the
105098** index only, without having to do a search for the corresponding
105099** table entry.  The IdxCover.pIdx field is the index.  IdxCover.iCur
105100** is the cursor for the table.
105101*/
105102struct IdxCover {
105103  Index *pIdx;     /* The index to be tested for coverage */
105104  int iCur;        /* Cursor number for the table corresponding to the index */
105105};
105106
105107/*
105108** Check to see if there are references to columns in table
105109** pWalker->u.pIdxCover->iCur can be satisfied using the index
105110** pWalker->u.pIdxCover->pIdx.
105111*/
105112static int exprIdxCover(Walker *pWalker, Expr *pExpr){
105113  if( pExpr->op==TK_COLUMN
105114   && pExpr->iTable==pWalker->u.pIdxCover->iCur
105115   && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
105116  ){
105117    pWalker->eCode = 1;
105118    return WRC_Abort;
105119  }
105120  return WRC_Continue;
105121}
105122
105123/*
105124** Determine if an index pIdx on table with cursor iCur contains will
105125** the expression pExpr.  Return true if the index does cover the
105126** expression and false if the pExpr expression references table columns
105127** that are not found in the index pIdx.
105128**
105129** An index covering an expression means that the expression can be
105130** evaluated using only the index and without having to lookup the
105131** corresponding table entry.
105132*/
105133SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(
105134  Expr *pExpr,        /* The index to be tested */
105135  int iCur,           /* The cursor number for the corresponding table */
105136  Index *pIdx         /* The index that might be used for coverage */
105137){
105138  Walker w;
105139  struct IdxCover xcov;
105140  memset(&w, 0, sizeof(w));
105141  xcov.iCur = iCur;
105142  xcov.pIdx = pIdx;
105143  w.xExprCallback = exprIdxCover;
105144  w.u.pIdxCover = &xcov;
105145  sqlite3WalkExpr(&w, pExpr);
105146  return !w.eCode;
105147}
105148
105149
105150/*
105151** An instance of the following structure is used by the tree walker
105152** to count references to table columns in the arguments of an
105153** aggregate function, in order to implement the
105154** sqlite3FunctionThisSrc() routine.
105155*/
105156struct SrcCount {
105157  SrcList *pSrc;   /* One particular FROM clause in a nested query */
105158  int iSrcInner;   /* Smallest cursor number in this context */
105159  int nThis;       /* Number of references to columns in pSrcList */
105160  int nOther;      /* Number of references to columns in other FROM clauses */
105161};
105162
105163/*
105164** xSelect callback for sqlite3FunctionUsesThisSrc(). If this is the first
105165** SELECT with a FROM clause encountered during this iteration, set
105166** SrcCount.iSrcInner to the cursor number of the leftmost object in
105167** the FROM cause.
105168*/
105169static int selectSrcCount(Walker *pWalker, Select *pSel){
105170  struct SrcCount *p = pWalker->u.pSrcCount;
105171  if( p->iSrcInner==0x7FFFFFFF && ALWAYS(pSel->pSrc) && pSel->pSrc->nSrc ){
105172    pWalker->u.pSrcCount->iSrcInner = pSel->pSrc->a[0].iCursor;
105173  }
105174  return WRC_Continue;
105175}
105176
105177/*
105178** Count the number of references to columns.
105179*/
105180static int exprSrcCount(Walker *pWalker, Expr *pExpr){
105181  /* There was once a NEVER() on the second term on the grounds that
105182  ** sqlite3FunctionUsesThisSrc() was always called before
105183  ** sqlite3ExprAnalyzeAggregates() and so the TK_COLUMNs have not yet
105184  ** been converted into TK_AGG_COLUMN. But this is no longer true due
105185  ** to window functions - sqlite3WindowRewrite() may now indirectly call
105186  ** FunctionUsesThisSrc() when creating a new sub-select. */
105187  if( pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN ){
105188    int i;
105189    struct SrcCount *p = pWalker->u.pSrcCount;
105190    SrcList *pSrc = p->pSrc;
105191    int nSrc = pSrc ? pSrc->nSrc : 0;
105192    for(i=0; i<nSrc; i++){
105193      if( pExpr->iTable==pSrc->a[i].iCursor ) break;
105194    }
105195    if( i<nSrc ){
105196      p->nThis++;
105197    }else if( pExpr->iTable<p->iSrcInner ){
105198      /* In a well-formed parse tree (no name resolution errors),
105199      ** TK_COLUMN nodes with smaller Expr.iTable values are in an
105200      ** outer context.  Those are the only ones to count as "other" */
105201      p->nOther++;
105202    }
105203  }
105204  return WRC_Continue;
105205}
105206
105207/*
105208** Determine if any of the arguments to the pExpr Function reference
105209** pSrcList.  Return true if they do.  Also return true if the function
105210** has no arguments or has only constant arguments.  Return false if pExpr
105211** references columns but not columns of tables found in pSrcList.
105212*/
105213SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
105214  Walker w;
105215  struct SrcCount cnt;
105216  assert( pExpr->op==TK_AGG_FUNCTION );
105217  memset(&w, 0, sizeof(w));
105218  w.xExprCallback = exprSrcCount;
105219  w.xSelectCallback = selectSrcCount;
105220  w.u.pSrcCount = &cnt;
105221  cnt.pSrc = pSrcList;
105222  cnt.iSrcInner = (pSrcList&&pSrcList->nSrc)?pSrcList->a[0].iCursor:0x7FFFFFFF;
105223  cnt.nThis = 0;
105224  cnt.nOther = 0;
105225  sqlite3WalkExprList(&w, pExpr->x.pList);
105226#ifndef SQLITE_OMIT_WINDOWFUNC
105227  if( ExprHasProperty(pExpr, EP_WinFunc) ){
105228    sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
105229  }
105230#endif
105231  return cnt.nThis>0 || cnt.nOther==0;
105232}
105233
105234/*
105235** This is a Walker expression node callback.
105236**
105237** For Expr nodes that contain pAggInfo pointers, make sure the AggInfo
105238** object that is referenced does not refer directly to the Expr.  If
105239** it does, make a copy.  This is done because the pExpr argument is
105240** subject to change.
105241**
105242** The copy is stored on pParse->pConstExpr with a register number of 0.
105243** This will cause the expression to be deleted automatically when the
105244** Parse object is destroyed, but the zero register number means that it
105245** will not generate any code in the preamble.
105246*/
105247static int agginfoPersistExprCb(Walker *pWalker, Expr *pExpr){
105248  if( ALWAYS(!ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced))
105249   && pExpr->pAggInfo!=0
105250  ){
105251    AggInfo *pAggInfo = pExpr->pAggInfo;
105252    int iAgg = pExpr->iAgg;
105253    Parse *pParse = pWalker->pParse;
105254    sqlite3 *db = pParse->db;
105255    assert( pExpr->op==TK_AGG_COLUMN || pExpr->op==TK_AGG_FUNCTION );
105256    if( pExpr->op==TK_AGG_COLUMN ){
105257      assert( iAgg>=0 && iAgg<pAggInfo->nColumn );
105258      if( pAggInfo->aCol[iAgg].pExpr==pExpr ){
105259        pExpr = sqlite3ExprDup(db, pExpr, 0);
105260        if( pExpr ){
105261          pAggInfo->aCol[iAgg].pExpr = pExpr;
105262          pParse->pConstExpr =
105263             sqlite3ExprListAppend(pParse, pParse->pConstExpr, pExpr);
105264        }
105265      }
105266    }else{
105267      assert( iAgg>=0 && iAgg<pAggInfo->nFunc );
105268      if( pAggInfo->aFunc[iAgg].pExpr==pExpr ){
105269        pExpr = sqlite3ExprDup(db, pExpr, 0);
105270        if( pExpr ){
105271          pAggInfo->aFunc[iAgg].pExpr = pExpr;
105272          pParse->pConstExpr =
105273             sqlite3ExprListAppend(pParse, pParse->pConstExpr, pExpr);
105274        }
105275      }
105276    }
105277  }
105278  return WRC_Continue;
105279}
105280
105281/*
105282** Initialize a Walker object so that will persist AggInfo entries referenced
105283** by the tree that is walked.
105284*/
105285SQLITE_PRIVATE void sqlite3AggInfoPersistWalkerInit(Walker *pWalker, Parse *pParse){
105286  memset(pWalker, 0, sizeof(*pWalker));
105287  pWalker->pParse = pParse;
105288  pWalker->xExprCallback = agginfoPersistExprCb;
105289  pWalker->xSelectCallback = sqlite3SelectWalkNoop;
105290}
105291
105292/*
105293** Add a new element to the pAggInfo->aCol[] array.  Return the index of
105294** the new element.  Return a negative number if malloc fails.
105295*/
105296static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
105297  int i;
105298  pInfo->aCol = sqlite3ArrayAllocate(
105299       db,
105300       pInfo->aCol,
105301       sizeof(pInfo->aCol[0]),
105302       &pInfo->nColumn,
105303       &i
105304  );
105305  return i;
105306}
105307
105308/*
105309** Add a new element to the pAggInfo->aFunc[] array.  Return the index of
105310** the new element.  Return a negative number if malloc fails.
105311*/
105312static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
105313  int i;
105314  pInfo->aFunc = sqlite3ArrayAllocate(
105315       db,
105316       pInfo->aFunc,
105317       sizeof(pInfo->aFunc[0]),
105318       &pInfo->nFunc,
105319       &i
105320  );
105321  return i;
105322}
105323
105324/*
105325** This is the xExprCallback for a tree walker.  It is used to
105326** implement sqlite3ExprAnalyzeAggregates().  See sqlite3ExprAnalyzeAggregates
105327** for additional information.
105328*/
105329static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
105330  int i;
105331  NameContext *pNC = pWalker->u.pNC;
105332  Parse *pParse = pNC->pParse;
105333  SrcList *pSrcList = pNC->pSrcList;
105334  AggInfo *pAggInfo = pNC->uNC.pAggInfo;
105335
105336  assert( pNC->ncFlags & NC_UAggInfo );
105337  switch( pExpr->op ){
105338    case TK_AGG_COLUMN:
105339    case TK_COLUMN: {
105340      testcase( pExpr->op==TK_AGG_COLUMN );
105341      testcase( pExpr->op==TK_COLUMN );
105342      /* Check to see if the column is in one of the tables in the FROM
105343      ** clause of the aggregate query */
105344      if( ALWAYS(pSrcList!=0) ){
105345        struct SrcList_item *pItem = pSrcList->a;
105346        for(i=0; i<pSrcList->nSrc; i++, pItem++){
105347          struct AggInfo_col *pCol;
105348          assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
105349          if( pExpr->iTable==pItem->iCursor ){
105350            /* If we reach this point, it means that pExpr refers to a table
105351            ** that is in the FROM clause of the aggregate query.
105352            **
105353            ** Make an entry for the column in pAggInfo->aCol[] if there
105354            ** is not an entry there already.
105355            */
105356            int k;
105357            pCol = pAggInfo->aCol;
105358            for(k=0; k<pAggInfo->nColumn; k++, pCol++){
105359              if( pCol->iTable==pExpr->iTable &&
105360                  pCol->iColumn==pExpr->iColumn ){
105361                break;
105362              }
105363            }
105364            if( (k>=pAggInfo->nColumn)
105365             && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
105366            ){
105367              pCol = &pAggInfo->aCol[k];
105368              pCol->pTab = pExpr->y.pTab;
105369              pCol->iTable = pExpr->iTable;
105370              pCol->iColumn = pExpr->iColumn;
105371              pCol->iMem = ++pParse->nMem;
105372              pCol->iSorterColumn = -1;
105373              pCol->pExpr = pExpr;
105374              if( pAggInfo->pGroupBy ){
105375                int j, n;
105376                ExprList *pGB = pAggInfo->pGroupBy;
105377                struct ExprList_item *pTerm = pGB->a;
105378                n = pGB->nExpr;
105379                for(j=0; j<n; j++, pTerm++){
105380                  Expr *pE = pTerm->pExpr;
105381                  if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
105382                      pE->iColumn==pExpr->iColumn ){
105383                    pCol->iSorterColumn = j;
105384                    break;
105385                  }
105386                }
105387              }
105388              if( pCol->iSorterColumn<0 ){
105389                pCol->iSorterColumn = pAggInfo->nSortingColumn++;
105390              }
105391            }
105392            /* There is now an entry for pExpr in pAggInfo->aCol[] (either
105393            ** because it was there before or because we just created it).
105394            ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
105395            ** pAggInfo->aCol[] entry.
105396            */
105397            ExprSetVVAProperty(pExpr, EP_NoReduce);
105398            pExpr->pAggInfo = pAggInfo;
105399            pExpr->op = TK_AGG_COLUMN;
105400            pExpr->iAgg = (i16)k;
105401            break;
105402          } /* endif pExpr->iTable==pItem->iCursor */
105403        } /* end loop over pSrcList */
105404      }
105405      return WRC_Prune;
105406    }
105407    case TK_AGG_FUNCTION: {
105408      if( (pNC->ncFlags & NC_InAggFunc)==0
105409       && pWalker->walkerDepth==pExpr->op2
105410      ){
105411        /* Check to see if pExpr is a duplicate of another aggregate
105412        ** function that is already in the pAggInfo structure
105413        */
105414        struct AggInfo_func *pItem = pAggInfo->aFunc;
105415        for(i=0; i<pAggInfo->nFunc; i++, pItem++){
105416          if( sqlite3ExprCompare(0, pItem->pExpr, pExpr, -1)==0 ){
105417            break;
105418          }
105419        }
105420        if( i>=pAggInfo->nFunc ){
105421          /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
105422          */
105423          u8 enc = ENC(pParse->db);
105424          i = addAggInfoFunc(pParse->db, pAggInfo);
105425          if( i>=0 ){
105426            assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
105427            pItem = &pAggInfo->aFunc[i];
105428            pItem->pExpr = pExpr;
105429            pItem->iMem = ++pParse->nMem;
105430            assert( !ExprHasProperty(pExpr, EP_IntValue) );
105431            pItem->pFunc = sqlite3FindFunction(pParse->db,
105432                   pExpr->u.zToken,
105433                   pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
105434            if( pExpr->flags & EP_Distinct ){
105435              pItem->iDistinct = pParse->nTab++;
105436            }else{
105437              pItem->iDistinct = -1;
105438            }
105439          }
105440        }
105441        /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
105442        */
105443        assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
105444        ExprSetVVAProperty(pExpr, EP_NoReduce);
105445        pExpr->iAgg = (i16)i;
105446        pExpr->pAggInfo = pAggInfo;
105447        return WRC_Prune;
105448      }else{
105449        return WRC_Continue;
105450      }
105451    }
105452  }
105453  return WRC_Continue;
105454}
105455
105456/*
105457** Analyze the pExpr expression looking for aggregate functions and
105458** for variables that need to be added to AggInfo object that pNC->pAggInfo
105459** points to.  Additional entries are made on the AggInfo object as
105460** necessary.
105461**
105462** This routine should only be called after the expression has been
105463** analyzed by sqlite3ResolveExprNames().
105464*/
105465SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
105466  Walker w;
105467  w.xExprCallback = analyzeAggregate;
105468  w.xSelectCallback = sqlite3WalkerDepthIncrease;
105469  w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
105470  w.walkerDepth = 0;
105471  w.u.pNC = pNC;
105472  w.pParse = 0;
105473  assert( pNC->pSrcList!=0 );
105474  sqlite3WalkExpr(&w, pExpr);
105475}
105476
105477/*
105478** Call sqlite3ExprAnalyzeAggregates() for every expression in an
105479** expression list.  Return the number of errors.
105480**
105481** If an error is found, the analysis is cut short.
105482*/
105483SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
105484  struct ExprList_item *pItem;
105485  int i;
105486  if( pList ){
105487    for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
105488      sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
105489    }
105490  }
105491}
105492
105493/*
105494** Allocate a single new register for use to hold some intermediate result.
105495*/
105496SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
105497  if( pParse->nTempReg==0 ){
105498    return ++pParse->nMem;
105499  }
105500  return pParse->aTempReg[--pParse->nTempReg];
105501}
105502
105503/*
105504** Deallocate a register, making available for reuse for some other
105505** purpose.
105506*/
105507SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
105508  if( iReg ){
105509    sqlite3VdbeReleaseRegisters(pParse, iReg, 1, 0, 0);
105510    if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
105511      pParse->aTempReg[pParse->nTempReg++] = iReg;
105512    }
105513  }
105514}
105515
105516/*
105517** Allocate or deallocate a block of nReg consecutive registers.
105518*/
105519SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
105520  int i, n;
105521  if( nReg==1 ) return sqlite3GetTempReg(pParse);
105522  i = pParse->iRangeReg;
105523  n = pParse->nRangeReg;
105524  if( nReg<=n ){
105525    pParse->iRangeReg += nReg;
105526    pParse->nRangeReg -= nReg;
105527  }else{
105528    i = pParse->nMem+1;
105529    pParse->nMem += nReg;
105530  }
105531  return i;
105532}
105533SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
105534  if( nReg==1 ){
105535    sqlite3ReleaseTempReg(pParse, iReg);
105536    return;
105537  }
105538  sqlite3VdbeReleaseRegisters(pParse, iReg, nReg, 0, 0);
105539  if( nReg>pParse->nRangeReg ){
105540    pParse->nRangeReg = nReg;
105541    pParse->iRangeReg = iReg;
105542  }
105543}
105544
105545/*
105546** Mark all temporary registers as being unavailable for reuse.
105547**
105548** Always invoke this procedure after coding a subroutine or co-routine
105549** that might be invoked from other parts of the code, to ensure that
105550** the sub/co-routine does not use registers in common with the code that
105551** invokes the sub/co-routine.
105552*/
105553SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
105554  pParse->nTempReg = 0;
105555  pParse->nRangeReg = 0;
105556}
105557
105558/*
105559** Validate that no temporary register falls within the range of
105560** iFirst..iLast, inclusive.  This routine is only call from within assert()
105561** statements.
105562*/
105563#ifdef SQLITE_DEBUG
105564SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){
105565  int i;
105566  if( pParse->nRangeReg>0
105567   && pParse->iRangeReg+pParse->nRangeReg > iFirst
105568   && pParse->iRangeReg <= iLast
105569  ){
105570     return 0;
105571  }
105572  for(i=0; i<pParse->nTempReg; i++){
105573    if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
105574      return 0;
105575    }
105576  }
105577  return 1;
105578}
105579#endif /* SQLITE_DEBUG */
105580
105581/************** End of expr.c ************************************************/
105582/************** Begin file alter.c *******************************************/
105583/*
105584** 2005 February 15
105585**
105586** The author disclaims copyright to this source code.  In place of
105587** a legal notice, here is a blessing:
105588**
105589**    May you do good and not evil.
105590**    May you find forgiveness for yourself and forgive others.
105591**    May you share freely, never taking more than you give.
105592**
105593*************************************************************************
105594** This file contains C code routines that used to generate VDBE code
105595** that implements the ALTER TABLE command.
105596*/
105597/* #include "sqliteInt.h" */
105598
105599/*
105600** The code in this file only exists if we are not omitting the
105601** ALTER TABLE logic from the build.
105602*/
105603#ifndef SQLITE_OMIT_ALTERTABLE
105604
105605/*
105606** Parameter zName is the name of a table that is about to be altered
105607** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
105608** If the table is a system table, this function leaves an error message
105609** in pParse->zErr (system tables may not be altered) and returns non-zero.
105610**
105611** Or, if zName is not a system table, zero is returned.
105612*/
105613static int isAlterableTable(Parse *pParse, Table *pTab){
105614  if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
105615#ifndef SQLITE_OMIT_VIRTUALTABLE
105616   || ( (pTab->tabFlags & TF_Shadow)!=0
105617        && sqlite3ReadOnlyShadowTables(pParse->db)
105618   )
105619#endif
105620  ){
105621    sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
105622    return 1;
105623  }
105624  return 0;
105625}
105626
105627/*
105628** Generate code to verify that the schemas of database zDb and, if
105629** bTemp is not true, database "temp", can still be parsed. This is
105630** called at the end of the generation of an ALTER TABLE ... RENAME ...
105631** statement to ensure that the operation has not rendered any schema
105632** objects unusable.
105633*/
105634static void renameTestSchema(Parse *pParse, const char *zDb, int bTemp){
105635  sqlite3NestedParse(pParse,
105636      "SELECT 1 "
105637      "FROM \"%w\".%s "
105638      "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
105639      " AND sql NOT LIKE 'create virtual%%'"
105640      " AND sqlite_rename_test(%Q, sql, type, name, %d)=NULL ",
105641      zDb, MASTER_NAME,
105642      zDb, bTemp
105643  );
105644
105645  if( bTemp==0 ){
105646    sqlite3NestedParse(pParse,
105647        "SELECT 1 "
105648        "FROM temp.%s "
105649        "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
105650        " AND sql NOT LIKE 'create virtual%%'"
105651        " AND sqlite_rename_test(%Q, sql, type, name, 1)=NULL ",
105652        MASTER_NAME, zDb
105653    );
105654  }
105655}
105656
105657/*
105658** Generate code to reload the schema for database iDb. And, if iDb!=1, for
105659** the temp database as well.
105660*/
105661static void renameReloadSchema(Parse *pParse, int iDb){
105662  Vdbe *v = pParse->pVdbe;
105663  if( v ){
105664    sqlite3ChangeCookie(pParse, iDb);
105665    sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0);
105666    if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0);
105667  }
105668}
105669
105670/*
105671** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
105672** command.
105673*/
105674SQLITE_PRIVATE void sqlite3AlterRenameTable(
105675  Parse *pParse,            /* Parser context. */
105676  SrcList *pSrc,            /* The table to rename. */
105677  Token *pName              /* The new table name. */
105678){
105679  int iDb;                  /* Database that contains the table */
105680  char *zDb;                /* Name of database iDb */
105681  Table *pTab;              /* Table being renamed */
105682  char *zName = 0;          /* NULL-terminated version of pName */
105683  sqlite3 *db = pParse->db; /* Database connection */
105684  int nTabName;             /* Number of UTF-8 characters in zTabName */
105685  const char *zTabName;     /* Original name of the table */
105686  Vdbe *v;
105687  VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
105688  u32 savedDbFlags;         /* Saved value of db->mDbFlags */
105689
105690  savedDbFlags = db->mDbFlags;
105691  if( NEVER(db->mallocFailed) ) goto exit_rename_table;
105692  assert( pSrc->nSrc==1 );
105693  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
105694
105695  pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
105696  if( !pTab ) goto exit_rename_table;
105697  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
105698  zDb = db->aDb[iDb].zDbSName;
105699  db->mDbFlags |= DBFLAG_PreferBuiltin;
105700
105701  /* Get a NULL terminated version of the new table name. */
105702  zName = sqlite3NameFromToken(db, pName);
105703  if( !zName ) goto exit_rename_table;
105704
105705  /* Check that a table or index named 'zName' does not already exist
105706  ** in database iDb. If so, this is an error.
105707  */
105708  if( sqlite3FindTable(db, zName, zDb)
105709   || sqlite3FindIndex(db, zName, zDb)
105710   || sqlite3IsShadowTableOf(db, pTab, zName)
105711  ){
105712    sqlite3ErrorMsg(pParse,
105713        "there is already another table or index with this name: %s", zName);
105714    goto exit_rename_table;
105715  }
105716
105717  /* Make sure it is not a system table being altered, or a reserved name
105718  ** that the table is being renamed to.
105719  */
105720  if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
105721    goto exit_rename_table;
105722  }
105723  if( SQLITE_OK!=sqlite3CheckObjectName(pParse,zName,"table",zName) ){
105724    goto exit_rename_table;
105725  }
105726
105727#ifndef SQLITE_OMIT_VIEW
105728  if( pTab->pSelect ){
105729    sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
105730    goto exit_rename_table;
105731  }
105732#endif
105733
105734#ifndef SQLITE_OMIT_AUTHORIZATION
105735  /* Invoke the authorization callback. */
105736  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
105737    goto exit_rename_table;
105738  }
105739#endif
105740
105741#ifndef SQLITE_OMIT_VIRTUALTABLE
105742  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
105743    goto exit_rename_table;
105744  }
105745  if( IsVirtual(pTab) ){
105746    pVTab = sqlite3GetVTable(db, pTab);
105747    if( pVTab->pVtab->pModule->xRename==0 ){
105748      pVTab = 0;
105749    }
105750  }
105751#endif
105752
105753  /* Begin a transaction for database iDb. Then modify the schema cookie
105754  ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
105755  ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the
105756  ** nested SQL may raise an exception.  */
105757  v = sqlite3GetVdbe(pParse);
105758  if( v==0 ){
105759    goto exit_rename_table;
105760  }
105761  sqlite3MayAbort(pParse);
105762
105763  /* figure out how many UTF-8 characters are in zName */
105764  zTabName = pTab->zName;
105765  nTabName = sqlite3Utf8CharLen(zTabName, -1);
105766
105767  /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
105768  ** the schema to use the new table name.  */
105769  sqlite3NestedParse(pParse,
105770      "UPDATE \"%w\".%s SET "
105771      "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
105772      "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
105773      "AND   name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
105774      , zDb, MASTER_NAME, zDb, zTabName, zName, (iDb==1), zTabName
105775  );
105776
105777  /* Update the tbl_name and name columns of the sqlite_master table
105778  ** as required.  */
105779  sqlite3NestedParse(pParse,
105780      "UPDATE %Q.%s SET "
105781          "tbl_name = %Q, "
105782          "name = CASE "
105783            "WHEN type='table' THEN %Q "
105784            "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' "
105785            "     AND type='index' THEN "
105786             "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
105787            "ELSE name END "
105788      "WHERE tbl_name=%Q COLLATE nocase AND "
105789          "(type='table' OR type='index' OR type='trigger');",
105790      zDb, MASTER_NAME,
105791      zName, zName, zName,
105792      nTabName, zTabName
105793  );
105794
105795#ifndef SQLITE_OMIT_AUTOINCREMENT
105796  /* If the sqlite_sequence table exists in this database, then update
105797  ** it with the new table name.
105798  */
105799  if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
105800    sqlite3NestedParse(pParse,
105801        "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
105802        zDb, zName, pTab->zName);
105803  }
105804#endif
105805
105806  /* If the table being renamed is not itself part of the temp database,
105807  ** edit view and trigger definitions within the temp database
105808  ** as required.  */
105809  if( iDb!=1 ){
105810    sqlite3NestedParse(pParse,
105811        "UPDATE sqlite_temp_master SET "
105812            "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
105813            "tbl_name = "
105814              "CASE WHEN tbl_name=%Q COLLATE nocase AND "
105815              "          sqlite_rename_test(%Q, sql, type, name, 1) "
105816              "THEN %Q ELSE tbl_name END "
105817            "WHERE type IN ('view', 'trigger')"
105818        , zDb, zTabName, zName, zTabName, zDb, zName);
105819  }
105820
105821  /* If this is a virtual table, invoke the xRename() function if
105822  ** one is defined. The xRename() callback will modify the names
105823  ** of any resources used by the v-table implementation (including other
105824  ** SQLite tables) that are identified by the name of the virtual table.
105825  */
105826#ifndef SQLITE_OMIT_VIRTUALTABLE
105827  if( pVTab ){
105828    int i = ++pParse->nMem;
105829    sqlite3VdbeLoadString(v, i, zName);
105830    sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
105831  }
105832#endif
105833
105834  renameReloadSchema(pParse, iDb);
105835  renameTestSchema(pParse, zDb, iDb==1);
105836
105837exit_rename_table:
105838  sqlite3SrcListDelete(db, pSrc);
105839  sqlite3DbFree(db, zName);
105840  db->mDbFlags = savedDbFlags;
105841}
105842
105843/*
105844** Write code that will raise an error if the table described by
105845** zDb and zTab is not empty.
105846*/
105847static void sqlite3ErrorIfNotEmpty(
105848  Parse *pParse,        /* Parsing context */
105849  const char *zDb,      /* Schema holding the table */
105850  const char *zTab,     /* Table to check for empty */
105851  const char *zErr      /* Error message text */
105852){
105853  sqlite3NestedParse(pParse,
105854     "SELECT raise(ABORT,%Q) FROM \"%w\".\"%w\"",
105855     zErr, zDb, zTab
105856  );
105857}
105858
105859/*
105860** This function is called after an "ALTER TABLE ... ADD" statement
105861** has been parsed. Argument pColDef contains the text of the new
105862** column definition.
105863**
105864** The Table structure pParse->pNewTable was extended to include
105865** the new column during parsing.
105866*/
105867SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
105868  Table *pNew;              /* Copy of pParse->pNewTable */
105869  Table *pTab;              /* Table being altered */
105870  int iDb;                  /* Database number */
105871  const char *zDb;          /* Database name */
105872  const char *zTab;         /* Table name */
105873  char *zCol;               /* Null-terminated column definition */
105874  Column *pCol;             /* The new column */
105875  Expr *pDflt;              /* Default value for the new column */
105876  sqlite3 *db;              /* The database connection; */
105877  Vdbe *v;                  /* The prepared statement under construction */
105878  int r1;                   /* Temporary registers */
105879
105880  db = pParse->db;
105881  if( pParse->nErr || db->mallocFailed ) return;
105882  pNew = pParse->pNewTable;
105883  assert( pNew );
105884
105885  assert( sqlite3BtreeHoldsAllMutexes(db) );
105886  iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
105887  zDb = db->aDb[iDb].zDbSName;
105888  zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
105889  pCol = &pNew->aCol[pNew->nCol-1];
105890  pDflt = pCol->pDflt;
105891  pTab = sqlite3FindTable(db, zTab, zDb);
105892  assert( pTab );
105893
105894#ifndef SQLITE_OMIT_AUTHORIZATION
105895  /* Invoke the authorization callback. */
105896  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
105897    return;
105898  }
105899#endif
105900
105901
105902  /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
105903  ** If there is a NOT NULL constraint, then the default value for the
105904  ** column must not be NULL.
105905  */
105906  if( pCol->colFlags & COLFLAG_PRIMKEY ){
105907    sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
105908    return;
105909  }
105910  if( pNew->pIndex ){
105911    sqlite3ErrorMsg(pParse,
105912         "Cannot add a UNIQUE column");
105913    return;
105914  }
105915  if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
105916    /* If the default value for the new column was specified with a
105917    ** literal NULL, then set pDflt to 0. This simplifies checking
105918    ** for an SQL NULL default below.
105919    */
105920    assert( pDflt==0 || pDflt->op==TK_SPAN );
105921    if( pDflt && pDflt->pLeft->op==TK_NULL ){
105922      pDflt = 0;
105923    }
105924    if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
105925      sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
105926          "Cannot add a REFERENCES column with non-NULL default value");
105927    }
105928    if( pCol->notNull && !pDflt ){
105929      sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
105930          "Cannot add a NOT NULL column with default value NULL");
105931    }
105932
105933
105934    /* Ensure the default expression is something that sqlite3ValueFromExpr()
105935    ** can handle (i.e. not CURRENT_TIME etc.)
105936    */
105937    if( pDflt ){
105938      sqlite3_value *pVal = 0;
105939      int rc;
105940      rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
105941      assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
105942      if( rc!=SQLITE_OK ){
105943        assert( db->mallocFailed == 1 );
105944        return;
105945      }
105946      if( !pVal ){
105947        sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
105948           "Cannot add a column with non-constant default");
105949      }
105950      sqlite3ValueFree(pVal);
105951    }
105952  }else if( pCol->colFlags & COLFLAG_STORED ){
105953    sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, "cannot add a STORED column");
105954  }
105955
105956
105957  /* Modify the CREATE TABLE statement. */
105958  zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
105959  if( zCol ){
105960    char *zEnd = &zCol[pColDef->n-1];
105961    u32 savedDbFlags = db->mDbFlags;
105962    while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
105963      *zEnd-- = '\0';
105964    }
105965    db->mDbFlags |= DBFLAG_PreferBuiltin;
105966    sqlite3NestedParse(pParse,
105967        "UPDATE \"%w\".%s SET "
105968          "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
105969        "WHERE type = 'table' AND name = %Q",
105970      zDb, MASTER_NAME, pNew->addColOffset, zCol, pNew->addColOffset+1,
105971      zTab
105972    );
105973    sqlite3DbFree(db, zCol);
105974    db->mDbFlags = savedDbFlags;
105975  }
105976
105977  /* Make sure the schema version is at least 3.  But do not upgrade
105978  ** from less than 3 to 4, as that will corrupt any preexisting DESC
105979  ** index.
105980  */
105981  v = sqlite3GetVdbe(pParse);
105982  if( v ){
105983    r1 = sqlite3GetTempReg(pParse);
105984    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
105985    sqlite3VdbeUsesBtree(v, iDb);
105986    sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
105987    sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2);
105988    VdbeCoverage(v);
105989    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3);
105990    sqlite3ReleaseTempReg(pParse, r1);
105991  }
105992
105993  /* Reload the table definition */
105994  renameReloadSchema(pParse, iDb);
105995}
105996
105997/*
105998** This function is called by the parser after the table-name in
105999** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
106000** pSrc is the full-name of the table being altered.
106001**
106002** This routine makes a (partial) copy of the Table structure
106003** for the table being altered and sets Parse.pNewTable to point
106004** to it. Routines called by the parser as the column definition
106005** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
106006** the copy. The copy of the Table structure is deleted by tokenize.c
106007** after parsing is finished.
106008**
106009** Routine sqlite3AlterFinishAddColumn() will be called to complete
106010** coding the "ALTER TABLE ... ADD" statement.
106011*/
106012SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
106013  Table *pNew;
106014  Table *pTab;
106015  int iDb;
106016  int i;
106017  int nAlloc;
106018  sqlite3 *db = pParse->db;
106019
106020  /* Look up the table being altered. */
106021  assert( pParse->pNewTable==0 );
106022  assert( sqlite3BtreeHoldsAllMutexes(db) );
106023  if( db->mallocFailed ) goto exit_begin_add_column;
106024  pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
106025  if( !pTab ) goto exit_begin_add_column;
106026
106027#ifndef SQLITE_OMIT_VIRTUALTABLE
106028  if( IsVirtual(pTab) ){
106029    sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
106030    goto exit_begin_add_column;
106031  }
106032#endif
106033
106034  /* Make sure this is not an attempt to ALTER a view. */
106035  if( pTab->pSelect ){
106036    sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
106037    goto exit_begin_add_column;
106038  }
106039  if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
106040    goto exit_begin_add_column;
106041  }
106042
106043  sqlite3MayAbort(pParse);
106044  assert( pTab->addColOffset>0 );
106045  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
106046
106047  /* Put a copy of the Table struct in Parse.pNewTable for the
106048  ** sqlite3AddColumn() function and friends to modify.  But modify
106049  ** the name by adding an "sqlite_altertab_" prefix.  By adding this
106050  ** prefix, we insure that the name will not collide with an existing
106051  ** table because user table are not allowed to have the "sqlite_"
106052  ** prefix on their name.
106053  */
106054  pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
106055  if( !pNew ) goto exit_begin_add_column;
106056  pParse->pNewTable = pNew;
106057  pNew->nTabRef = 1;
106058  pNew->nCol = pTab->nCol;
106059  assert( pNew->nCol>0 );
106060  nAlloc = (((pNew->nCol-1)/8)*8)+8;
106061  assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
106062  pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
106063  pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
106064  if( !pNew->aCol || !pNew->zName ){
106065    assert( db->mallocFailed );
106066    goto exit_begin_add_column;
106067  }
106068  memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
106069  for(i=0; i<pNew->nCol; i++){
106070    Column *pCol = &pNew->aCol[i];
106071    pCol->zName = sqlite3DbStrDup(db, pCol->zName);
106072    pCol->hName = sqlite3StrIHash(pCol->zName);
106073    pCol->zColl = 0;
106074    pCol->pDflt = 0;
106075  }
106076  pNew->pSchema = db->aDb[iDb].pSchema;
106077  pNew->addColOffset = pTab->addColOffset;
106078  pNew->nTabRef = 1;
106079
106080exit_begin_add_column:
106081  sqlite3SrcListDelete(db, pSrc);
106082  return;
106083}
106084
106085/*
106086** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN
106087** command. This function checks if the table is a view or virtual
106088** table (columns of views or virtual tables may not be renamed). If so,
106089** it loads an error message into pParse and returns non-zero.
106090**
106091** Or, if pTab is not a view or virtual table, zero is returned.
106092*/
106093#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
106094static int isRealTable(Parse *pParse, Table *pTab){
106095  const char *zType = 0;
106096#ifndef SQLITE_OMIT_VIEW
106097  if( pTab->pSelect ){
106098    zType = "view";
106099  }
106100#endif
106101#ifndef SQLITE_OMIT_VIRTUALTABLE
106102  if( IsVirtual(pTab) ){
106103    zType = "virtual table";
106104  }
106105#endif
106106  if( zType ){
106107    sqlite3ErrorMsg(
106108        pParse, "cannot rename columns of %s \"%s\"", zType, pTab->zName
106109    );
106110    return 1;
106111  }
106112  return 0;
106113}
106114#else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
106115# define isRealTable(x,y) (0)
106116#endif
106117
106118/*
106119** Handles the following parser reduction:
106120**
106121**  cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
106122*/
106123SQLITE_PRIVATE void sqlite3AlterRenameColumn(
106124  Parse *pParse,                  /* Parsing context */
106125  SrcList *pSrc,                  /* Table being altered.  pSrc->nSrc==1 */
106126  Token *pOld,                    /* Name of column being changed */
106127  Token *pNew                     /* New column name */
106128){
106129  sqlite3 *db = pParse->db;       /* Database connection */
106130  Table *pTab;                    /* Table being updated */
106131  int iCol;                       /* Index of column being renamed */
106132  char *zOld = 0;                 /* Old column name */
106133  char *zNew = 0;                 /* New column name */
106134  const char *zDb;                /* Name of schema containing the table */
106135  int iSchema;                    /* Index of the schema */
106136  int bQuote;                     /* True to quote the new name */
106137
106138  /* Locate the table to be altered */
106139  pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
106140  if( !pTab ) goto exit_rename_column;
106141
106142  /* Cannot alter a system table */
106143  if( SQLITE_OK!=isAlterableTable(pParse, pTab) ) goto exit_rename_column;
106144  if( SQLITE_OK!=isRealTable(pParse, pTab) ) goto exit_rename_column;
106145
106146  /* Which schema holds the table to be altered */
106147  iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
106148  assert( iSchema>=0 );
106149  zDb = db->aDb[iSchema].zDbSName;
106150
106151#ifndef SQLITE_OMIT_AUTHORIZATION
106152  /* Invoke the authorization callback. */
106153  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
106154    goto exit_rename_column;
106155  }
106156#endif
106157
106158  /* Make sure the old name really is a column name in the table to be
106159  ** altered.  Set iCol to be the index of the column being renamed */
106160  zOld = sqlite3NameFromToken(db, pOld);
106161  if( !zOld ) goto exit_rename_column;
106162  for(iCol=0; iCol<pTab->nCol; iCol++){
106163    if( 0==sqlite3StrICmp(pTab->aCol[iCol].zName, zOld) ) break;
106164  }
106165  if( iCol==pTab->nCol ){
106166    sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zOld);
106167    goto exit_rename_column;
106168  }
106169
106170  /* Do the rename operation using a recursive UPDATE statement that
106171  ** uses the sqlite_rename_column() SQL function to compute the new
106172  ** CREATE statement text for the sqlite_master table.
106173  */
106174  sqlite3MayAbort(pParse);
106175  zNew = sqlite3NameFromToken(db, pNew);
106176  if( !zNew ) goto exit_rename_column;
106177  assert( pNew->n>0 );
106178  bQuote = sqlite3Isquote(pNew->z[0]);
106179  sqlite3NestedParse(pParse,
106180      "UPDATE \"%w\".%s SET "
106181      "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
106182      "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' "
106183      " AND (type != 'index' OR tbl_name = %Q)"
106184      " AND sql NOT LIKE 'create virtual%%'",
106185      zDb, MASTER_NAME,
106186      zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
106187      pTab->zName
106188  );
106189
106190  sqlite3NestedParse(pParse,
106191      "UPDATE temp.%s SET "
106192      "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
106193      "WHERE type IN ('trigger', 'view')",
106194      MASTER_NAME,
106195      zDb, pTab->zName, iCol, zNew, bQuote
106196  );
106197
106198  /* Drop and reload the database schema. */
106199  renameReloadSchema(pParse, iSchema);
106200  renameTestSchema(pParse, zDb, iSchema==1);
106201
106202 exit_rename_column:
106203  sqlite3SrcListDelete(db, pSrc);
106204  sqlite3DbFree(db, zOld);
106205  sqlite3DbFree(db, zNew);
106206  return;
106207}
106208
106209/*
106210** Each RenameToken object maps an element of the parse tree into
106211** the token that generated that element.  The parse tree element
106212** might be one of:
106213**
106214**     *  A pointer to an Expr that represents an ID
106215**     *  The name of a table column in Column.zName
106216**
106217** A list of RenameToken objects can be constructed during parsing.
106218** Each new object is created by sqlite3RenameTokenMap().
106219** As the parse tree is transformed, the sqlite3RenameTokenRemap()
106220** routine is used to keep the mapping current.
106221**
106222** After the parse finishes, renameTokenFind() routine can be used
106223** to look up the actual token value that created some element in
106224** the parse tree.
106225*/
106226struct RenameToken {
106227  void *p;               /* Parse tree element created by token t */
106228  Token t;               /* The token that created parse tree element p */
106229  RenameToken *pNext;    /* Next is a list of all RenameToken objects */
106230};
106231
106232/*
106233** The context of an ALTER TABLE RENAME COLUMN operation that gets passed
106234** down into the Walker.
106235*/
106236typedef struct RenameCtx RenameCtx;
106237struct RenameCtx {
106238  RenameToken *pList;             /* List of tokens to overwrite */
106239  int nList;                      /* Number of tokens in pList */
106240  int iCol;                       /* Index of column being renamed */
106241  Table *pTab;                    /* Table being ALTERed */
106242  const char *zOld;               /* Old column name */
106243};
106244
106245#ifdef SQLITE_DEBUG
106246/*
106247** This function is only for debugging. It performs two tasks:
106248**
106249**   1. Checks that pointer pPtr does not already appear in the
106250**      rename-token list.
106251**
106252**   2. Dereferences each pointer in the rename-token list.
106253**
106254** The second is most effective when debugging under valgrind or
106255** address-sanitizer or similar. If any of these pointers no longer
106256** point to valid objects, an exception is raised by the memory-checking
106257** tool.
106258**
106259** The point of this is to prevent comparisons of invalid pointer values.
106260** Even though this always seems to work, it is undefined according to the
106261** C standard. Example of undefined comparison:
106262**
106263**     sqlite3_free(x);
106264**     if( x==y ) ...
106265**
106266** Technically, as x no longer points into a valid object or to the byte
106267** following a valid object, it may not be used in comparison operations.
106268*/
106269static void renameTokenCheckAll(Parse *pParse, void *pPtr){
106270  if( pParse->nErr==0 && pParse->db->mallocFailed==0 ){
106271    RenameToken *p;
106272    u8 i = 0;
106273    for(p=pParse->pRename; p; p=p->pNext){
106274      if( p->p ){
106275        assert( p->p!=pPtr );
106276        i += *(u8*)(p->p);
106277      }
106278    }
106279  }
106280}
106281#else
106282# define renameTokenCheckAll(x,y)
106283#endif
106284
106285/*
106286** Remember that the parser tree element pPtr was created using
106287** the token pToken.
106288**
106289** In other words, construct a new RenameToken object and add it
106290** to the list of RenameToken objects currently being built up
106291** in pParse->pRename.
106292**
106293** The pPtr argument is returned so that this routine can be used
106294** with tail recursion in tokenExpr() routine, for a small performance
106295** improvement.
106296*/
106297SQLITE_PRIVATE void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){
106298  RenameToken *pNew;
106299  assert( pPtr || pParse->db->mallocFailed );
106300  renameTokenCheckAll(pParse, pPtr);
106301  if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
106302    pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
106303    if( pNew ){
106304      pNew->p = pPtr;
106305      pNew->t = *pToken;
106306      pNew->pNext = pParse->pRename;
106307      pParse->pRename = pNew;
106308    }
106309  }
106310
106311  return pPtr;
106312}
106313
106314/*
106315** It is assumed that there is already a RenameToken object associated
106316** with parse tree element pFrom. This function remaps the associated token
106317** to parse tree element pTo.
106318*/
106319SQLITE_PRIVATE void sqlite3RenameTokenRemap(Parse *pParse, void *pTo, void *pFrom){
106320  RenameToken *p;
106321  renameTokenCheckAll(pParse, pTo);
106322  for(p=pParse->pRename; p; p=p->pNext){
106323    if( p->p==pFrom ){
106324      p->p = pTo;
106325      break;
106326    }
106327  }
106328}
106329
106330/*
106331** Walker callback used by sqlite3RenameExprUnmap().
106332*/
106333static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
106334  Parse *pParse = pWalker->pParse;
106335  sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
106336  return WRC_Continue;
106337}
106338
106339/*
106340** Iterate through the Select objects that are part of WITH clauses attached
106341** to select statement pSelect.
106342*/
106343static void renameWalkWith(Walker *pWalker, Select *pSelect){
106344  With *pWith = pSelect->pWith;
106345  if( pWith ){
106346    int i;
106347    for(i=0; i<pWith->nCte; i++){
106348      Select *p = pWith->a[i].pSelect;
106349      NameContext sNC;
106350      memset(&sNC, 0, sizeof(sNC));
106351      sNC.pParse = pWalker->pParse;
106352      sqlite3SelectPrep(sNC.pParse, p, &sNC);
106353      sqlite3WalkSelect(pWalker, p);
106354      sqlite3RenameExprlistUnmap(pWalker->pParse, pWith->a[i].pCols);
106355    }
106356  }
106357}
106358
106359/*
106360** Unmap all tokens in the IdList object passed as the second argument.
106361*/
106362static void unmapColumnIdlistNames(
106363  Parse *pParse,
106364  IdList *pIdList
106365){
106366  if( pIdList ){
106367    int ii;
106368    for(ii=0; ii<pIdList->nId; ii++){
106369      sqlite3RenameTokenRemap(pParse, 0, (void*)pIdList->a[ii].zName);
106370    }
106371  }
106372}
106373
106374/*
106375** Walker callback used by sqlite3RenameExprUnmap().
106376*/
106377static int renameUnmapSelectCb(Walker *pWalker, Select *p){
106378  Parse *pParse = pWalker->pParse;
106379  int i;
106380  if( pParse->nErr ) return WRC_Abort;
106381  if( NEVER(p->selFlags & SF_View) ) return WRC_Prune;
106382  if( ALWAYS(p->pEList) ){
106383    ExprList *pList = p->pEList;
106384    for(i=0; i<pList->nExpr; i++){
106385      if( pList->a[i].zEName && pList->a[i].eEName==ENAME_NAME ){
106386        sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
106387      }
106388    }
106389  }
106390  if( ALWAYS(p->pSrc) ){  /* Every Select as a SrcList, even if it is empty */
106391    SrcList *pSrc = p->pSrc;
106392    for(i=0; i<pSrc->nSrc; i++){
106393      sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
106394      if( sqlite3WalkExpr(pWalker, pSrc->a[i].pOn) ) return WRC_Abort;
106395      unmapColumnIdlistNames(pParse, pSrc->a[i].pUsing);
106396    }
106397  }
106398
106399  renameWalkWith(pWalker, p);
106400  return WRC_Continue;
106401}
106402
106403/*
106404** Remove all nodes that are part of expression pExpr from the rename list.
106405*/
106406SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){
106407  u8 eMode = pParse->eParseMode;
106408  Walker sWalker;
106409  memset(&sWalker, 0, sizeof(Walker));
106410  sWalker.pParse = pParse;
106411  sWalker.xExprCallback = renameUnmapExprCb;
106412  sWalker.xSelectCallback = renameUnmapSelectCb;
106413  pParse->eParseMode = PARSE_MODE_UNMAP;
106414  sqlite3WalkExpr(&sWalker, pExpr);
106415  pParse->eParseMode = eMode;
106416}
106417
106418/*
106419** Remove all nodes that are part of expression-list pEList from the
106420** rename list.
106421*/
106422SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){
106423  if( pEList ){
106424    int i;
106425    Walker sWalker;
106426    memset(&sWalker, 0, sizeof(Walker));
106427    sWalker.pParse = pParse;
106428    sWalker.xExprCallback = renameUnmapExprCb;
106429    sqlite3WalkExprList(&sWalker, pEList);
106430    for(i=0; i<pEList->nExpr; i++){
106431      if( ALWAYS(pEList->a[i].eEName==ENAME_NAME) ){
106432        sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
106433      }
106434    }
106435  }
106436}
106437
106438/*
106439** Free the list of RenameToken objects given in the second argument
106440*/
106441static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
106442  RenameToken *pNext;
106443  RenameToken *p;
106444  for(p=pToken; p; p=pNext){
106445    pNext = p->pNext;
106446    sqlite3DbFree(db, p);
106447  }
106448}
106449
106450/*
106451** Search the Parse object passed as the first argument for a RenameToken
106452** object associated with parse tree element pPtr. If found, remove it
106453** from the Parse object and add it to the list maintained by the
106454** RenameCtx object passed as the second argument.
106455*/
106456static void renameTokenFind(Parse *pParse, struct RenameCtx *pCtx, void *pPtr){
106457  RenameToken **pp;
106458  assert( pPtr!=0 );
106459  for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
106460    if( (*pp)->p==pPtr ){
106461      RenameToken *pToken = *pp;
106462      *pp = pToken->pNext;
106463      pToken->pNext = pCtx->pList;
106464      pCtx->pList = pToken;
106465      pCtx->nList++;
106466      break;
106467    }
106468  }
106469}
106470
106471/*
106472** This is a Walker select callback. It does nothing. It is only required
106473** because without a dummy callback, sqlite3WalkExpr() and similar do not
106474** descend into sub-select statements.
106475*/
106476static int renameColumnSelectCb(Walker *pWalker, Select *p){
106477  if( p->selFlags & SF_View ) return WRC_Prune;
106478  renameWalkWith(pWalker, p);
106479  return WRC_Continue;
106480}
106481
106482/*
106483** This is a Walker expression callback.
106484**
106485** For every TK_COLUMN node in the expression tree, search to see
106486** if the column being references is the column being renamed by an
106487** ALTER TABLE statement.  If it is, then attach its associated
106488** RenameToken object to the list of RenameToken objects being
106489** constructed in RenameCtx object at pWalker->u.pRename.
106490*/
106491static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
106492  RenameCtx *p = pWalker->u.pRename;
106493  if( pExpr->op==TK_TRIGGER
106494   && pExpr->iColumn==p->iCol
106495   && pWalker->pParse->pTriggerTab==p->pTab
106496  ){
106497    renameTokenFind(pWalker->pParse, p, (void*)pExpr);
106498  }else if( pExpr->op==TK_COLUMN
106499   && pExpr->iColumn==p->iCol
106500   && p->pTab==pExpr->y.pTab
106501  ){
106502    renameTokenFind(pWalker->pParse, p, (void*)pExpr);
106503  }
106504  return WRC_Continue;
106505}
106506
106507/*
106508** The RenameCtx contains a list of tokens that reference a column that
106509** is being renamed by an ALTER TABLE statement.  Return the "last"
106510** RenameToken in the RenameCtx and remove that RenameToken from the
106511** RenameContext.  "Last" means the last RenameToken encountered when
106512** the input SQL is parsed from left to right.  Repeated calls to this routine
106513** return all column name tokens in the order that they are encountered
106514** in the SQL statement.
106515*/
106516static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){
106517  RenameToken *pBest = pCtx->pList;
106518  RenameToken *pToken;
106519  RenameToken **pp;
106520
106521  for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
106522    if( pToken->t.z>pBest->t.z ) pBest = pToken;
106523  }
106524  for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
106525  *pp = pBest->pNext;
106526
106527  return pBest;
106528}
106529
106530/*
106531** An error occured while parsing or otherwise processing a database
106532** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
106533** ALTER TABLE RENAME COLUMN program. The error message emitted by the
106534** sub-routine is currently stored in pParse->zErrMsg. This function
106535** adds context to the error message and then stores it in pCtx.
106536*/
106537static void renameColumnParseError(
106538  sqlite3_context *pCtx,
106539  int bPost,
106540  sqlite3_value *pType,
106541  sqlite3_value *pObject,
106542  Parse *pParse
106543){
106544  const char *zT = (const char*)sqlite3_value_text(pType);
106545  const char *zN = (const char*)sqlite3_value_text(pObject);
106546  char *zErr;
106547
106548  zErr = sqlite3_mprintf("error in %s %s%s: %s",
106549      zT, zN, (bPost ? " after rename" : ""),
106550      pParse->zErrMsg
106551  );
106552  sqlite3_result_error(pCtx, zErr, -1);
106553  sqlite3_free(zErr);
106554}
106555
106556/*
106557** For each name in the the expression-list pEList (i.e. each
106558** pEList->a[i].zName) that matches the string in zOld, extract the
106559** corresponding rename-token from Parse object pParse and add it
106560** to the RenameCtx pCtx.
106561*/
106562static void renameColumnElistNames(
106563  Parse *pParse,
106564  RenameCtx *pCtx,
106565  ExprList *pEList,
106566  const char *zOld
106567){
106568  if( pEList ){
106569    int i;
106570    for(i=0; i<pEList->nExpr; i++){
106571      char *zName = pEList->a[i].zEName;
106572      if( ALWAYS(pEList->a[i].eEName==ENAME_NAME)
106573       && ALWAYS(zName!=0)
106574       && 0==sqlite3_stricmp(zName, zOld)
106575      ){
106576        renameTokenFind(pParse, pCtx, (void*)zName);
106577      }
106578    }
106579  }
106580}
106581
106582/*
106583** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
106584** that matches the string in zOld, extract the corresponding rename-token
106585** from Parse object pParse and add it to the RenameCtx pCtx.
106586*/
106587static void renameColumnIdlistNames(
106588  Parse *pParse,
106589  RenameCtx *pCtx,
106590  IdList *pIdList,
106591  const char *zOld
106592){
106593  if( pIdList ){
106594    int i;
106595    for(i=0; i<pIdList->nId; i++){
106596      char *zName = pIdList->a[i].zName;
106597      if( 0==sqlite3_stricmp(zName, zOld) ){
106598        renameTokenFind(pParse, pCtx, (void*)zName);
106599      }
106600    }
106601  }
106602}
106603
106604
106605/*
106606** Parse the SQL statement zSql using Parse object (*p). The Parse object
106607** is initialized by this function before it is used.
106608*/
106609static int renameParseSql(
106610  Parse *p,                       /* Memory to use for Parse object */
106611  const char *zDb,                /* Name of schema SQL belongs to */
106612  sqlite3 *db,                    /* Database handle */
106613  const char *zSql,               /* SQL to parse */
106614  int bTemp                       /* True if SQL is from temp schema */
106615){
106616  int rc;
106617  char *zErr = 0;
106618
106619  db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
106620
106621  /* Parse the SQL statement passed as the first argument. If no error
106622  ** occurs and the parse does not result in a new table, index or
106623  ** trigger object, the database must be corrupt. */
106624  memset(p, 0, sizeof(Parse));
106625  p->eParseMode = PARSE_MODE_RENAME;
106626  p->db = db;
106627  p->nQueryLoop = 1;
106628  rc = sqlite3RunParser(p, zSql, &zErr);
106629  assert( p->zErrMsg==0 );
106630  assert( rc!=SQLITE_OK || zErr==0 );
106631  p->zErrMsg = zErr;
106632  if( db->mallocFailed ) rc = SQLITE_NOMEM;
106633  if( rc==SQLITE_OK
106634   && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0
106635  ){
106636    rc = SQLITE_CORRUPT_BKPT;
106637  }
106638
106639#ifdef SQLITE_DEBUG
106640  /* Ensure that all mappings in the Parse.pRename list really do map to
106641  ** a part of the input string.  */
106642  if( rc==SQLITE_OK ){
106643    int nSql = sqlite3Strlen30(zSql);
106644    RenameToken *pToken;
106645    for(pToken=p->pRename; pToken; pToken=pToken->pNext){
106646      assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
106647    }
106648  }
106649#endif
106650
106651  db->init.iDb = 0;
106652  return rc;
106653}
106654
106655/*
106656** This function edits SQL statement zSql, replacing each token identified
106657** by the linked list pRename with the text of zNew. If argument bQuote is
106658** true, then zNew is always quoted first. If no error occurs, the result
106659** is loaded into context object pCtx as the result.
106660**
106661** Or, if an error occurs (i.e. an OOM condition), an error is left in
106662** pCtx and an SQLite error code returned.
106663*/
106664static int renameEditSql(
106665  sqlite3_context *pCtx,          /* Return result here */
106666  RenameCtx *pRename,             /* Rename context */
106667  const char *zSql,               /* SQL statement to edit */
106668  const char *zNew,               /* New token text */
106669  int bQuote                      /* True to always quote token */
106670){
106671  int nNew = sqlite3Strlen30(zNew);
106672  int nSql = sqlite3Strlen30(zSql);
106673  sqlite3 *db = sqlite3_context_db_handle(pCtx);
106674  int rc = SQLITE_OK;
106675  char *zQuot;
106676  char *zOut;
106677  int nQuot;
106678
106679  /* Set zQuot to point to a buffer containing a quoted copy of the
106680  ** identifier zNew. If the corresponding identifier in the original
106681  ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
106682  ** point to zQuot so that all substitutions are made using the
106683  ** quoted version of the new column name.  */
106684  zQuot = sqlite3MPrintf(db, "\"%w\"", zNew);
106685  if( zQuot==0 ){
106686    return SQLITE_NOMEM;
106687  }else{
106688    nQuot = sqlite3Strlen30(zQuot);
106689  }
106690  if( bQuote ){
106691    zNew = zQuot;
106692    nNew = nQuot;
106693  }
106694
106695  /* At this point pRename->pList contains a list of RenameToken objects
106696  ** corresponding to all tokens in the input SQL that must be replaced
106697  ** with the new column name. All that remains is to construct and
106698  ** return the edited SQL string. */
106699  assert( nQuot>=nNew );
106700  zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
106701  if( zOut ){
106702    int nOut = nSql;
106703    memcpy(zOut, zSql, nSql);
106704    while( pRename->pList ){
106705      int iOff;                   /* Offset of token to replace in zOut */
106706      RenameToken *pBest = renameColumnTokenNext(pRename);
106707
106708      u32 nReplace;
106709      const char *zReplace;
106710      if( sqlite3IsIdChar(*pBest->t.z) ){
106711        nReplace = nNew;
106712        zReplace = zNew;
106713      }else{
106714        nReplace = nQuot;
106715        zReplace = zQuot;
106716      }
106717
106718      iOff = pBest->t.z - zSql;
106719      if( pBest->t.n!=nReplace ){
106720        memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
106721            nOut - (iOff + pBest->t.n)
106722        );
106723        nOut += nReplace - pBest->t.n;
106724        zOut[nOut] = '\0';
106725      }
106726      memcpy(&zOut[iOff], zReplace, nReplace);
106727      sqlite3DbFree(db, pBest);
106728    }
106729
106730    sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
106731    sqlite3DbFree(db, zOut);
106732  }else{
106733    rc = SQLITE_NOMEM;
106734  }
106735
106736  sqlite3_free(zQuot);
106737  return rc;
106738}
106739
106740/*
106741** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
106742** it was read from the schema of database zDb. Return SQLITE_OK if
106743** successful. Otherwise, return an SQLite error code and leave an error
106744** message in the Parse object.
106745*/
106746static int renameResolveTrigger(Parse *pParse, const char *zDb){
106747  sqlite3 *db = pParse->db;
106748  Trigger *pNew = pParse->pNewTrigger;
106749  TriggerStep *pStep;
106750  NameContext sNC;
106751  int rc = SQLITE_OK;
106752
106753  memset(&sNC, 0, sizeof(sNC));
106754  sNC.pParse = pParse;
106755  assert( pNew->pTabSchema );
106756  pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
106757      db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
106758  );
106759  pParse->eTriggerOp = pNew->op;
106760  /* ALWAYS() because if the table of the trigger does not exist, the
106761  ** error would have been hit before this point */
106762  if( ALWAYS(pParse->pTriggerTab) ){
106763    rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
106764  }
106765
106766  /* Resolve symbols in WHEN clause */
106767  if( rc==SQLITE_OK && pNew->pWhen ){
106768    rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
106769  }
106770
106771  for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
106772    if( pStep->pSelect ){
106773      sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
106774      if( pParse->nErr ) rc = pParse->rc;
106775    }
106776    if( rc==SQLITE_OK && pStep->zTarget ){
106777      Table *pTarget = sqlite3LocateTable(pParse, 0, pStep->zTarget, zDb);
106778      if( pTarget==0 ){
106779        rc = SQLITE_ERROR;
106780      }else if( SQLITE_OK==(rc = sqlite3ViewGetColumnNames(pParse, pTarget)) ){
106781        SrcList sSrc;
106782        memset(&sSrc, 0, sizeof(sSrc));
106783        sSrc.nSrc = 1;
106784        sSrc.a[0].zName = pStep->zTarget;
106785        sSrc.a[0].pTab = pTarget;
106786        sNC.pSrcList = &sSrc;
106787        if( pStep->pWhere ){
106788          rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
106789        }
106790        if( rc==SQLITE_OK ){
106791          rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
106792        }
106793        assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
106794        if( pStep->pUpsert ){
106795          Upsert *pUpsert = pStep->pUpsert;
106796          assert( rc==SQLITE_OK );
106797          pUpsert->pUpsertSrc = &sSrc;
106798          sNC.uNC.pUpsert = pUpsert;
106799          sNC.ncFlags = NC_UUpsert;
106800          rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
106801          if( rc==SQLITE_OK ){
106802            ExprList *pUpsertSet = pUpsert->pUpsertSet;
106803            rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet);
106804          }
106805          if( rc==SQLITE_OK ){
106806            rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
106807          }
106808          if( rc==SQLITE_OK ){
106809            rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
106810          }
106811          sNC.ncFlags = 0;
106812        }
106813        sNC.pSrcList = 0;
106814      }
106815    }
106816  }
106817  return rc;
106818}
106819
106820/*
106821** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
106822** objects that are part of the trigger passed as the second argument.
106823*/
106824static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
106825  TriggerStep *pStep;
106826
106827  /* Find tokens to edit in WHEN clause */
106828  sqlite3WalkExpr(pWalker, pTrigger->pWhen);
106829
106830  /* Find tokens to edit in trigger steps */
106831  for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
106832    sqlite3WalkSelect(pWalker, pStep->pSelect);
106833    sqlite3WalkExpr(pWalker, pStep->pWhere);
106834    sqlite3WalkExprList(pWalker, pStep->pExprList);
106835    if( pStep->pUpsert ){
106836      Upsert *pUpsert = pStep->pUpsert;
106837      sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
106838      sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
106839      sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
106840      sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
106841    }
106842  }
106843}
106844
106845/*
106846** Free the contents of Parse object (*pParse). Do not free the memory
106847** occupied by the Parse object itself.
106848*/
106849static void renameParseCleanup(Parse *pParse){
106850  sqlite3 *db = pParse->db;
106851  Index *pIdx;
106852  if( pParse->pVdbe ){
106853    sqlite3VdbeFinalize(pParse->pVdbe);
106854  }
106855  sqlite3DeleteTable(db, pParse->pNewTable);
106856  while( (pIdx = pParse->pNewIndex)!=0 ){
106857    pParse->pNewIndex = pIdx->pNext;
106858    sqlite3FreeIndex(db, pIdx);
106859  }
106860  sqlite3DeleteTrigger(db, pParse->pNewTrigger);
106861  sqlite3DbFree(db, pParse->zErrMsg);
106862  renameTokenFree(db, pParse->pRename);
106863  sqlite3ParserReset(pParse);
106864}
106865
106866/*
106867** SQL function:
106868**
106869**     sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld)
106870**
106871**   0. zSql:     SQL statement to rewrite
106872**   1. type:     Type of object ("table", "view" etc.)
106873**   2. object:   Name of object
106874**   3. Database: Database name (e.g. "main")
106875**   4. Table:    Table name
106876**   5. iCol:     Index of column to rename
106877**   6. zNew:     New column name
106878**   7. bQuote:   Non-zero if the new column name should be quoted.
106879**   8. bTemp:    True if zSql comes from temp schema
106880**
106881** Do a column rename operation on the CREATE statement given in zSql.
106882** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
106883** into zNew.  The name should be quoted if bQuote is true.
106884**
106885** This function is used internally by the ALTER TABLE RENAME COLUMN command.
106886** It is only accessible to SQL created using sqlite3NestedParse().  It is
106887** not reachable from ordinary SQL passed into sqlite3_prepare().
106888*/
106889static void renameColumnFunc(
106890  sqlite3_context *context,
106891  int NotUsed,
106892  sqlite3_value **argv
106893){
106894  sqlite3 *db = sqlite3_context_db_handle(context);
106895  RenameCtx sCtx;
106896  const char *zSql = (const char*)sqlite3_value_text(argv[0]);
106897  const char *zDb = (const char*)sqlite3_value_text(argv[3]);
106898  const char *zTable = (const char*)sqlite3_value_text(argv[4]);
106899  int iCol = sqlite3_value_int(argv[5]);
106900  const char *zNew = (const char*)sqlite3_value_text(argv[6]);
106901  int bQuote = sqlite3_value_int(argv[7]);
106902  int bTemp = sqlite3_value_int(argv[8]);
106903  const char *zOld;
106904  int rc;
106905  Parse sParse;
106906  Walker sWalker;
106907  Index *pIdx;
106908  int i;
106909  Table *pTab;
106910#ifndef SQLITE_OMIT_AUTHORIZATION
106911  sqlite3_xauth xAuth = db->xAuth;
106912#endif
106913
106914  UNUSED_PARAMETER(NotUsed);
106915  if( zSql==0 ) return;
106916  if( zTable==0 ) return;
106917  if( zNew==0 ) return;
106918  if( iCol<0 ) return;
106919  sqlite3BtreeEnterAll(db);
106920  pTab = sqlite3FindTable(db, zTable, zDb);
106921  if( pTab==0 || iCol>=pTab->nCol ){
106922    sqlite3BtreeLeaveAll(db);
106923    return;
106924  }
106925  zOld = pTab->aCol[iCol].zName;
106926  memset(&sCtx, 0, sizeof(sCtx));
106927  sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
106928
106929#ifndef SQLITE_OMIT_AUTHORIZATION
106930  db->xAuth = 0;
106931#endif
106932  rc = renameParseSql(&sParse, zDb, db, zSql, bTemp);
106933
106934  /* Find tokens that need to be replaced. */
106935  memset(&sWalker, 0, sizeof(Walker));
106936  sWalker.pParse = &sParse;
106937  sWalker.xExprCallback = renameColumnExprCb;
106938  sWalker.xSelectCallback = renameColumnSelectCb;
106939  sWalker.u.pRename = &sCtx;
106940
106941  sCtx.pTab = pTab;
106942  if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
106943  if( sParse.pNewTable ){
106944    Select *pSelect = sParse.pNewTable->pSelect;
106945    if( pSelect ){
106946      pSelect->selFlags &= ~SF_View;
106947      sParse.rc = SQLITE_OK;
106948      sqlite3SelectPrep(&sParse, pSelect, 0);
106949      rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
106950      if( rc==SQLITE_OK ){
106951        sqlite3WalkSelect(&sWalker, pSelect);
106952      }
106953      if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
106954    }else{
106955      /* A regular table */
106956      int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
106957      FKey *pFKey;
106958      assert( sParse.pNewTable->pSelect==0 );
106959      sCtx.pTab = sParse.pNewTable;
106960      if( bFKOnly==0 ){
106961        renameTokenFind(
106962            &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zName
106963        );
106964        if( sCtx.iCol<0 ){
106965          renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
106966        }
106967        sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
106968        for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
106969          sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
106970        }
106971        for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
106972          sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
106973        }
106974      }
106975#ifndef SQLITE_OMIT_GENERATED_COLUMNS
106976      for(i=0; i<sParse.pNewTable->nCol; i++){
106977        sqlite3WalkExpr(&sWalker, sParse.pNewTable->aCol[i].pDflt);
106978      }
106979#endif
106980
106981      for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
106982        for(i=0; i<pFKey->nCol; i++){
106983          if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
106984            renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
106985          }
106986          if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
106987           && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
106988          ){
106989            renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
106990          }
106991        }
106992      }
106993    }
106994  }else if( sParse.pNewIndex ){
106995    sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
106996    sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
106997  }else{
106998    /* A trigger */
106999    TriggerStep *pStep;
107000    rc = renameResolveTrigger(&sParse, (bTemp ? 0 : zDb));
107001    if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
107002
107003    for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
107004      if( pStep->zTarget ){
107005        Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
107006        if( pTarget==pTab ){
107007          if( pStep->pUpsert ){
107008            ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
107009            renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld);
107010          }
107011          renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
107012          renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
107013        }
107014      }
107015    }
107016
107017
107018    /* Find tokens to edit in UPDATE OF clause */
107019    if( sParse.pTriggerTab==pTab ){
107020      renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
107021    }
107022
107023    /* Find tokens to edit in various expressions and selects */
107024    renameWalkTrigger(&sWalker, sParse.pNewTrigger);
107025  }
107026
107027  assert( rc==SQLITE_OK );
107028  rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote);
107029
107030renameColumnFunc_done:
107031  if( rc!=SQLITE_OK ){
107032    if( sParse.zErrMsg ){
107033      renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
107034    }else{
107035      sqlite3_result_error_code(context, rc);
107036    }
107037  }
107038
107039  renameParseCleanup(&sParse);
107040  renameTokenFree(db, sCtx.pList);
107041#ifndef SQLITE_OMIT_AUTHORIZATION
107042  db->xAuth = xAuth;
107043#endif
107044  sqlite3BtreeLeaveAll(db);
107045}
107046
107047/*
107048** Walker expression callback used by "RENAME TABLE".
107049*/
107050static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
107051  RenameCtx *p = pWalker->u.pRename;
107052  if( pExpr->op==TK_COLUMN && p->pTab==pExpr->y.pTab ){
107053    renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
107054  }
107055  return WRC_Continue;
107056}
107057
107058/*
107059** Walker select callback used by "RENAME TABLE".
107060*/
107061static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
107062  int i;
107063  RenameCtx *p = pWalker->u.pRename;
107064  SrcList *pSrc = pSelect->pSrc;
107065  if( pSelect->selFlags & SF_View ) return WRC_Prune;
107066  if( pSrc==0 ){
107067    assert( pWalker->pParse->db->mallocFailed );
107068    return WRC_Abort;
107069  }
107070  for(i=0; i<pSrc->nSrc; i++){
107071    struct SrcList_item *pItem = &pSrc->a[i];
107072    if( pItem->pTab==p->pTab ){
107073      renameTokenFind(pWalker->pParse, p, pItem->zName);
107074    }
107075  }
107076  renameWalkWith(pWalker, pSelect);
107077
107078  return WRC_Continue;
107079}
107080
107081
107082/*
107083** This C function implements an SQL user function that is used by SQL code
107084** generated by the ALTER TABLE ... RENAME command to modify the definition
107085** of any foreign key constraints that use the table being renamed as the
107086** parent table. It is passed three arguments:
107087**
107088**   0: The database containing the table being renamed.
107089**   1. type:     Type of object ("table", "view" etc.)
107090**   2. object:   Name of object
107091**   3: The complete text of the schema statement being modified,
107092**   4: The old name of the table being renamed, and
107093**   5: The new name of the table being renamed.
107094**   6: True if the schema statement comes from the temp db.
107095**
107096** It returns the new schema statement. For example:
107097**
107098** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
107099**       -> 'CREATE TABLE t1(a REFERENCES t3)'
107100*/
107101static void renameTableFunc(
107102  sqlite3_context *context,
107103  int NotUsed,
107104  sqlite3_value **argv
107105){
107106  sqlite3 *db = sqlite3_context_db_handle(context);
107107  const char *zDb = (const char*)sqlite3_value_text(argv[0]);
107108  const char *zInput = (const char*)sqlite3_value_text(argv[3]);
107109  const char *zOld = (const char*)sqlite3_value_text(argv[4]);
107110  const char *zNew = (const char*)sqlite3_value_text(argv[5]);
107111  int bTemp = sqlite3_value_int(argv[6]);
107112  UNUSED_PARAMETER(NotUsed);
107113
107114  if( zInput && zOld && zNew ){
107115    Parse sParse;
107116    int rc;
107117    int bQuote = 1;
107118    RenameCtx sCtx;
107119    Walker sWalker;
107120
107121#ifndef SQLITE_OMIT_AUTHORIZATION
107122    sqlite3_xauth xAuth = db->xAuth;
107123    db->xAuth = 0;
107124#endif
107125
107126    sqlite3BtreeEnterAll(db);
107127
107128    memset(&sCtx, 0, sizeof(RenameCtx));
107129    sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
107130    memset(&sWalker, 0, sizeof(Walker));
107131    sWalker.pParse = &sParse;
107132    sWalker.xExprCallback = renameTableExprCb;
107133    sWalker.xSelectCallback = renameTableSelectCb;
107134    sWalker.u.pRename = &sCtx;
107135
107136    rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
107137
107138    if( rc==SQLITE_OK ){
107139      int isLegacy = (db->flags & SQLITE_LegacyAlter);
107140      if( sParse.pNewTable ){
107141        Table *pTab = sParse.pNewTable;
107142
107143        if( pTab->pSelect ){
107144          if( isLegacy==0 ){
107145            Select *pSelect = pTab->pSelect;
107146            NameContext sNC;
107147            memset(&sNC, 0, sizeof(sNC));
107148            sNC.pParse = &sParse;
107149
107150            assert( pSelect->selFlags & SF_View );
107151            pSelect->selFlags &= ~SF_View;
107152            sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
107153            if( sParse.nErr ){
107154              rc = sParse.rc;
107155            }else{
107156              sqlite3WalkSelect(&sWalker, pTab->pSelect);
107157            }
107158          }
107159        }else{
107160          /* Modify any FK definitions to point to the new table. */
107161#ifndef SQLITE_OMIT_FOREIGN_KEY
107162          if( isLegacy==0 || (db->flags & SQLITE_ForeignKeys) ){
107163            FKey *pFKey;
107164            for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
107165              if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
107166                renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
107167              }
107168            }
107169          }
107170#endif
107171
107172          /* If this is the table being altered, fix any table refs in CHECK
107173          ** expressions. Also update the name that appears right after the
107174          ** "CREATE [VIRTUAL] TABLE" bit. */
107175          if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
107176            sCtx.pTab = pTab;
107177            if( isLegacy==0 ){
107178              sqlite3WalkExprList(&sWalker, pTab->pCheck);
107179            }
107180            renameTokenFind(&sParse, &sCtx, pTab->zName);
107181          }
107182        }
107183      }
107184
107185      else if( sParse.pNewIndex ){
107186        renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
107187        if( isLegacy==0 ){
107188          sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
107189        }
107190      }
107191
107192#ifndef SQLITE_OMIT_TRIGGER
107193      else{
107194        Trigger *pTrigger = sParse.pNewTrigger;
107195        TriggerStep *pStep;
107196        if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
107197            && sCtx.pTab->pSchema==pTrigger->pTabSchema
107198          ){
107199          renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
107200        }
107201
107202        if( isLegacy==0 ){
107203          rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
107204          if( rc==SQLITE_OK ){
107205            renameWalkTrigger(&sWalker, pTrigger);
107206            for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
107207              if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
107208                renameTokenFind(&sParse, &sCtx, pStep->zTarget);
107209              }
107210            }
107211          }
107212        }
107213      }
107214#endif
107215    }
107216
107217    if( rc==SQLITE_OK ){
107218      rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
107219    }
107220    if( rc!=SQLITE_OK ){
107221      if( sParse.zErrMsg ){
107222        renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
107223      }else{
107224        sqlite3_result_error_code(context, rc);
107225      }
107226    }
107227
107228    renameParseCleanup(&sParse);
107229    renameTokenFree(db, sCtx.pList);
107230    sqlite3BtreeLeaveAll(db);
107231#ifndef SQLITE_OMIT_AUTHORIZATION
107232    db->xAuth = xAuth;
107233#endif
107234  }
107235
107236  return;
107237}
107238
107239/*
107240** An SQL user function that checks that there are no parse or symbol
107241** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
107242** After an ALTER TABLE .. RENAME operation is performed and the schema
107243** reloaded, this function is called on each SQL statement in the schema
107244** to ensure that it is still usable.
107245**
107246**   0: Database name ("main", "temp" etc.).
107247**   1: SQL statement.
107248**   2: Object type ("view", "table", "trigger" or "index").
107249**   3: Object name.
107250**   4: True if object is from temp schema.
107251**
107252** Unless it finds an error, this function normally returns NULL. However, it
107253** returns integer value 1 if:
107254**
107255**   * the SQL argument creates a trigger, and
107256**   * the table that the trigger is attached to is in database zDb.
107257*/
107258static void renameTableTest(
107259  sqlite3_context *context,
107260  int NotUsed,
107261  sqlite3_value **argv
107262){
107263  sqlite3 *db = sqlite3_context_db_handle(context);
107264  char const *zDb = (const char*)sqlite3_value_text(argv[0]);
107265  char const *zInput = (const char*)sqlite3_value_text(argv[1]);
107266  int bTemp = sqlite3_value_int(argv[4]);
107267  int isLegacy = (db->flags & SQLITE_LegacyAlter);
107268
107269#ifndef SQLITE_OMIT_AUTHORIZATION
107270  sqlite3_xauth xAuth = db->xAuth;
107271  db->xAuth = 0;
107272#endif
107273
107274  UNUSED_PARAMETER(NotUsed);
107275  if( zDb && zInput ){
107276    int rc;
107277    Parse sParse;
107278    rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
107279    if( rc==SQLITE_OK ){
107280      if( isLegacy==0 && sParse.pNewTable && sParse.pNewTable->pSelect ){
107281        NameContext sNC;
107282        memset(&sNC, 0, sizeof(sNC));
107283        sNC.pParse = &sParse;
107284        sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, &sNC);
107285        if( sParse.nErr ) rc = sParse.rc;
107286      }
107287
107288      else if( sParse.pNewTrigger ){
107289        if( isLegacy==0 ){
107290          rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
107291        }
107292        if( rc==SQLITE_OK ){
107293          int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
107294          int i2 = sqlite3FindDbName(db, zDb);
107295          if( i1==i2 ) sqlite3_result_int(context, 1);
107296        }
107297      }
107298    }
107299
107300    if( rc!=SQLITE_OK ){
107301      renameColumnParseError(context, 1, argv[2], argv[3], &sParse);
107302    }
107303    renameParseCleanup(&sParse);
107304  }
107305
107306#ifndef SQLITE_OMIT_AUTHORIZATION
107307  db->xAuth = xAuth;
107308#endif
107309}
107310
107311/*
107312** Register built-in functions used to help implement ALTER TABLE
107313*/
107314SQLITE_PRIVATE void sqlite3AlterFunctions(void){
107315  static FuncDef aAlterTableFuncs[] = {
107316    INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc),
107317    INTERNAL_FUNCTION(sqlite_rename_table,  7, renameTableFunc),
107318    INTERNAL_FUNCTION(sqlite_rename_test,   5, renameTableTest),
107319  };
107320  sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
107321}
107322#endif  /* SQLITE_ALTER_TABLE */
107323
107324/************** End of alter.c ***********************************************/
107325/************** Begin file analyze.c *****************************************/
107326/*
107327** 2005-07-08
107328**
107329** The author disclaims copyright to this source code.  In place of
107330** a legal notice, here is a blessing:
107331**
107332**    May you do good and not evil.
107333**    May you find forgiveness for yourself and forgive others.
107334**    May you share freely, never taking more than you give.
107335**
107336*************************************************************************
107337** This file contains code associated with the ANALYZE command.
107338**
107339** The ANALYZE command gather statistics about the content of tables
107340** and indices.  These statistics are made available to the query planner
107341** to help it make better decisions about how to perform queries.
107342**
107343** The following system tables are or have been supported:
107344**
107345**    CREATE TABLE sqlite_stat1(tbl, idx, stat);
107346**    CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
107347**    CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
107348**    CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
107349**
107350** Additional tables might be added in future releases of SQLite.
107351** The sqlite_stat2 table is not created or used unless the SQLite version
107352** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
107353** with SQLITE_ENABLE_STAT2.  The sqlite_stat2 table is deprecated.
107354** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
107355** created and used by SQLite versions 3.7.9 through 3.29.0 when
107356** SQLITE_ENABLE_STAT3 defined.  The functionality of sqlite_stat3
107357** is a superset of sqlite_stat2 and is also now deprecated.  The
107358** sqlite_stat4 is an enhanced version of sqlite_stat3 and is only
107359** available when compiled with SQLITE_ENABLE_STAT4 and in SQLite
107360** versions 3.8.1 and later.  STAT4 is the only variant that is still
107361** supported.
107362**
107363** For most applications, sqlite_stat1 provides all the statistics required
107364** for the query planner to make good choices.
107365**
107366** Format of sqlite_stat1:
107367**
107368** There is normally one row per index, with the index identified by the
107369** name in the idx column.  The tbl column is the name of the table to
107370** which the index belongs.  In each such row, the stat column will be
107371** a string consisting of a list of integers.  The first integer in this
107372** list is the number of rows in the index.  (This is the same as the
107373** number of rows in the table, except for partial indices.)  The second
107374** integer is the average number of rows in the index that have the same
107375** value in the first column of the index.  The third integer is the average
107376** number of rows in the index that have the same value for the first two
107377** columns.  The N-th integer (for N>1) is the average number of rows in
107378** the index which have the same value for the first N-1 columns.  For
107379** a K-column index, there will be K+1 integers in the stat column.  If
107380** the index is unique, then the last integer will be 1.
107381**
107382** The list of integers in the stat column can optionally be followed
107383** by the keyword "unordered".  The "unordered" keyword, if it is present,
107384** must be separated from the last integer by a single space.  If the
107385** "unordered" keyword is present, then the query planner assumes that
107386** the index is unordered and will not use the index for a range query.
107387**
107388** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
107389** column contains a single integer which is the (estimated) number of
107390** rows in the table identified by sqlite_stat1.tbl.
107391**
107392** Format of sqlite_stat2:
107393**
107394** The sqlite_stat2 is only created and is only used if SQLite is compiled
107395** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
107396** 3.6.18 and 3.7.8.  The "stat2" table contains additional information
107397** about the distribution of keys within an index.  The index is identified by
107398** the "idx" column and the "tbl" column is the name of the table to which
107399** the index belongs.  There are usually 10 rows in the sqlite_stat2
107400** table for each index.
107401**
107402** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
107403** inclusive are samples of the left-most key value in the index taken at
107404** evenly spaced points along the index.  Let the number of samples be S
107405** (10 in the standard build) and let C be the number of rows in the index.
107406** Then the sampled rows are given by:
107407**
107408**     rownumber = (i*C*2 + C)/(S*2)
107409**
107410** For i between 0 and S-1.  Conceptually, the index space is divided into
107411** S uniform buckets and the samples are the middle row from each bucket.
107412**
107413** The format for sqlite_stat2 is recorded here for legacy reference.  This
107414** version of SQLite does not support sqlite_stat2.  It neither reads nor
107415** writes the sqlite_stat2 table.  This version of SQLite only supports
107416** sqlite_stat3.
107417**
107418** Format for sqlite_stat3:
107419**
107420** The sqlite_stat3 format is a subset of sqlite_stat4.  Hence, the
107421** sqlite_stat4 format will be described first.  Further information
107422** about sqlite_stat3 follows the sqlite_stat4 description.
107423**
107424** Format for sqlite_stat4:
107425**
107426** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
107427** to aid the query planner in choosing good indices based on the values
107428** that indexed columns are compared against in the WHERE clauses of
107429** queries.
107430**
107431** The sqlite_stat4 table contains multiple entries for each index.
107432** The idx column names the index and the tbl column is the table of the
107433** index.  If the idx and tbl columns are the same, then the sample is
107434** of the INTEGER PRIMARY KEY.  The sample column is a blob which is the
107435** binary encoding of a key from the index.  The nEq column is a
107436** list of integers.  The first integer is the approximate number
107437** of entries in the index whose left-most column exactly matches
107438** the left-most column of the sample.  The second integer in nEq
107439** is the approximate number of entries in the index where the
107440** first two columns match the first two columns of the sample.
107441** And so forth.  nLt is another list of integers that show the approximate
107442** number of entries that are strictly less than the sample.  The first
107443** integer in nLt contains the number of entries in the index where the
107444** left-most column is less than the left-most column of the sample.
107445** The K-th integer in the nLt entry is the number of index entries
107446** where the first K columns are less than the first K columns of the
107447** sample.  The nDLt column is like nLt except that it contains the
107448** number of distinct entries in the index that are less than the
107449** sample.
107450**
107451** There can be an arbitrary number of sqlite_stat4 entries per index.
107452** The ANALYZE command will typically generate sqlite_stat4 tables
107453** that contain between 10 and 40 samples which are distributed across
107454** the key space, though not uniformly, and which include samples with
107455** large nEq values.
107456**
107457** Format for sqlite_stat3 redux:
107458**
107459** The sqlite_stat3 table is like sqlite_stat4 except that it only
107460** looks at the left-most column of the index.  The sqlite_stat3.sample
107461** column contains the actual value of the left-most column instead
107462** of a blob encoding of the complete index key as is found in
107463** sqlite_stat4.sample.  The nEq, nLt, and nDLt entries of sqlite_stat3
107464** all contain just a single integer which is the same as the first
107465** integer in the equivalent columns in sqlite_stat4.
107466*/
107467#ifndef SQLITE_OMIT_ANALYZE
107468/* #include "sqliteInt.h" */
107469
107470#if defined(SQLITE_ENABLE_STAT4)
107471# define IsStat4     1
107472#else
107473# define IsStat4     0
107474# undef SQLITE_STAT4_SAMPLES
107475# define SQLITE_STAT4_SAMPLES 1
107476#endif
107477
107478/*
107479** This routine generates code that opens the sqlite_statN tables.
107480** The sqlite_stat1 table is always relevant.  sqlite_stat2 is now
107481** obsolete.  sqlite_stat3 and sqlite_stat4 are only opened when
107482** appropriate compile-time options are provided.
107483**
107484** If the sqlite_statN tables do not previously exist, it is created.
107485**
107486** Argument zWhere may be a pointer to a buffer containing a table name,
107487** or it may be a NULL pointer. If it is not NULL, then all entries in
107488** the sqlite_statN tables associated with the named table are deleted.
107489** If zWhere==0, then code is generated to delete all stat table entries.
107490*/
107491static void openStatTable(
107492  Parse *pParse,          /* Parsing context */
107493  int iDb,                /* The database we are looking in */
107494  int iStatCur,           /* Open the sqlite_stat1 table on this cursor */
107495  const char *zWhere,     /* Delete entries for this table or index */
107496  const char *zWhereType  /* Either "tbl" or "idx" */
107497){
107498  static const struct {
107499    const char *zName;
107500    const char *zCols;
107501  } aTable[] = {
107502    { "sqlite_stat1", "tbl,idx,stat" },
107503#if defined(SQLITE_ENABLE_STAT4)
107504    { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
107505#else
107506    { "sqlite_stat4", 0 },
107507#endif
107508    { "sqlite_stat3", 0 },
107509  };
107510  int i;
107511  sqlite3 *db = pParse->db;
107512  Db *pDb;
107513  Vdbe *v = sqlite3GetVdbe(pParse);
107514  int aRoot[ArraySize(aTable)];
107515  u8 aCreateTbl[ArraySize(aTable)];
107516#ifdef SQLITE_ENABLE_STAT4
107517  const int nToOpen = OptimizationEnabled(db,SQLITE_Stat4) ? 2 : 1;
107518#else
107519  const int nToOpen = 1;
107520#endif
107521
107522  if( v==0 ) return;
107523  assert( sqlite3BtreeHoldsAllMutexes(db) );
107524  assert( sqlite3VdbeDb(v)==db );
107525  pDb = &db->aDb[iDb];
107526
107527  /* Create new statistic tables if they do not exist, or clear them
107528  ** if they do already exist.
107529  */
107530  for(i=0; i<ArraySize(aTable); i++){
107531    const char *zTab = aTable[i].zName;
107532    Table *pStat;
107533    aCreateTbl[i] = 0;
107534    if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
107535      if( i<nToOpen ){
107536        /* The sqlite_statN table does not exist. Create it. Note that a
107537        ** side-effect of the CREATE TABLE statement is to leave the rootpage
107538        ** of the new table in register pParse->regRoot. This is important
107539        ** because the OpenWrite opcode below will be needing it. */
107540        sqlite3NestedParse(pParse,
107541            "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
107542        );
107543        aRoot[i] = pParse->regRoot;
107544        aCreateTbl[i] = OPFLAG_P2ISREG;
107545      }
107546    }else{
107547      /* The table already exists. If zWhere is not NULL, delete all entries
107548      ** associated with the table zWhere. If zWhere is NULL, delete the
107549      ** entire contents of the table. */
107550      aRoot[i] = pStat->tnum;
107551      sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
107552      if( zWhere ){
107553        sqlite3NestedParse(pParse,
107554           "DELETE FROM %Q.%s WHERE %s=%Q",
107555           pDb->zDbSName, zTab, zWhereType, zWhere
107556        );
107557#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
107558      }else if( db->xPreUpdateCallback ){
107559        sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
107560#endif
107561      }else{
107562        /* The sqlite_stat[134] table already exists.  Delete all rows. */
107563        sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
107564      }
107565    }
107566  }
107567
107568  /* Open the sqlite_stat[134] tables for writing. */
107569  for(i=0; i<nToOpen; i++){
107570    assert( i<ArraySize(aTable) );
107571    sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3);
107572    sqlite3VdbeChangeP5(v, aCreateTbl[i]);
107573    VdbeComment((v, aTable[i].zName));
107574  }
107575}
107576
107577/*
107578** Recommended number of samples for sqlite_stat4
107579*/
107580#ifndef SQLITE_STAT4_SAMPLES
107581# define SQLITE_STAT4_SAMPLES 24
107582#endif
107583
107584/*
107585** Three SQL functions - stat_init(), stat_push(), and stat_get() -
107586** share an instance of the following structure to hold their state
107587** information.
107588*/
107589typedef struct StatAccum StatAccum;
107590typedef struct StatSample StatSample;
107591struct StatSample {
107592  tRowcnt *anEq;                  /* sqlite_stat4.nEq */
107593  tRowcnt *anDLt;                 /* sqlite_stat4.nDLt */
107594#ifdef SQLITE_ENABLE_STAT4
107595  tRowcnt *anLt;                  /* sqlite_stat4.nLt */
107596  union {
107597    i64 iRowid;                     /* Rowid in main table of the key */
107598    u8 *aRowid;                     /* Key for WITHOUT ROWID tables */
107599  } u;
107600  u32 nRowid;                     /* Sizeof aRowid[] */
107601  u8 isPSample;                   /* True if a periodic sample */
107602  int iCol;                       /* If !isPSample, the reason for inclusion */
107603  u32 iHash;                      /* Tiebreaker hash */
107604#endif
107605};
107606struct StatAccum {
107607  sqlite3 *db;              /* Database connection, for malloc() */
107608  tRowcnt nEst;             /* Estimated number of rows */
107609  tRowcnt nRow;             /* Number of rows visited so far */
107610  int nLimit;               /* Analysis row-scan limit */
107611  int nCol;                 /* Number of columns in index + pk/rowid */
107612  int nKeyCol;              /* Number of index columns w/o the pk/rowid */
107613  u8 nSkipAhead;            /* Number of times of skip-ahead */
107614  StatSample current;       /* Current row as a StatSample */
107615#ifdef SQLITE_ENABLE_STAT4
107616  tRowcnt nPSample;         /* How often to do a periodic sample */
107617  int mxSample;             /* Maximum number of samples to accumulate */
107618  u32 iPrn;                 /* Pseudo-random number used for sampling */
107619  StatSample *aBest;        /* Array of nCol best samples */
107620  int iMin;                 /* Index in a[] of entry with minimum score */
107621  int nSample;              /* Current number of samples */
107622  int nMaxEqZero;           /* Max leading 0 in anEq[] for any a[] entry */
107623  int iGet;                 /* Index of current sample accessed by stat_get() */
107624  StatSample *a;            /* Array of mxSample StatSample objects */
107625#endif
107626};
107627
107628/* Reclaim memory used by a StatSample
107629*/
107630#ifdef SQLITE_ENABLE_STAT4
107631static void sampleClear(sqlite3 *db, StatSample *p){
107632  assert( db!=0 );
107633  if( p->nRowid ){
107634    sqlite3DbFree(db, p->u.aRowid);
107635    p->nRowid = 0;
107636  }
107637}
107638#endif
107639
107640/* Initialize the BLOB value of a ROWID
107641*/
107642#ifdef SQLITE_ENABLE_STAT4
107643static void sampleSetRowid(sqlite3 *db, StatSample *p, int n, const u8 *pData){
107644  assert( db!=0 );
107645  if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
107646  p->u.aRowid = sqlite3DbMallocRawNN(db, n);
107647  if( p->u.aRowid ){
107648    p->nRowid = n;
107649    memcpy(p->u.aRowid, pData, n);
107650  }else{
107651    p->nRowid = 0;
107652  }
107653}
107654#endif
107655
107656/* Initialize the INTEGER value of a ROWID.
107657*/
107658#ifdef SQLITE_ENABLE_STAT4
107659static void sampleSetRowidInt64(sqlite3 *db, StatSample *p, i64 iRowid){
107660  assert( db!=0 );
107661  if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
107662  p->nRowid = 0;
107663  p->u.iRowid = iRowid;
107664}
107665#endif
107666
107667
107668/*
107669** Copy the contents of object (*pFrom) into (*pTo).
107670*/
107671#ifdef SQLITE_ENABLE_STAT4
107672static void sampleCopy(StatAccum *p, StatSample *pTo, StatSample *pFrom){
107673  pTo->isPSample = pFrom->isPSample;
107674  pTo->iCol = pFrom->iCol;
107675  pTo->iHash = pFrom->iHash;
107676  memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
107677  memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
107678  memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
107679  if( pFrom->nRowid ){
107680    sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
107681  }else{
107682    sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
107683  }
107684}
107685#endif
107686
107687/*
107688** Reclaim all memory of a StatAccum structure.
107689*/
107690static void statAccumDestructor(void *pOld){
107691  StatAccum *p = (StatAccum*)pOld;
107692#ifdef SQLITE_ENABLE_STAT4
107693  if( p->mxSample ){
107694    int i;
107695    for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
107696    for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
107697    sampleClear(p->db, &p->current);
107698  }
107699#endif
107700  sqlite3DbFree(p->db, p);
107701}
107702
107703/*
107704** Implementation of the stat_init(N,K,C,L) SQL function. The four parameters
107705** are:
107706**     N:    The number of columns in the index including the rowid/pk (note 1)
107707**     K:    The number of columns in the index excluding the rowid/pk.
107708**     C:    Estimated number of rows in the index
107709**     L:    A limit on the number of rows to scan, or 0 for no-limit
107710**
107711** Note 1:  In the special case of the covering index that implements a
107712** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
107713** total number of columns in the table.
107714**
107715** For indexes on ordinary rowid tables, N==K+1.  But for indexes on
107716** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
107717** PRIMARY KEY of the table.  The covering index that implements the
107718** original WITHOUT ROWID table as N==K as a special case.
107719**
107720** This routine allocates the StatAccum object in heap memory. The return
107721** value is a pointer to the StatAccum object.  The datatype of the
107722** return value is BLOB, but it is really just a pointer to the StatAccum
107723** object.
107724*/
107725static void statInit(
107726  sqlite3_context *context,
107727  int argc,
107728  sqlite3_value **argv
107729){
107730  StatAccum *p;
107731  int nCol;                       /* Number of columns in index being sampled */
107732  int nKeyCol;                    /* Number of key columns */
107733  int nColUp;                     /* nCol rounded up for alignment */
107734  int n;                          /* Bytes of space to allocate */
107735  sqlite3 *db = sqlite3_context_db_handle(context);   /* Database connection */
107736#ifdef SQLITE_ENABLE_STAT4
107737  /* Maximum number of samples.  0 if STAT4 data is not collected */
107738  int mxSample = OptimizationEnabled(db,SQLITE_Stat4) ?SQLITE_STAT4_SAMPLES :0;
107739#endif
107740
107741  /* Decode the three function arguments */
107742  UNUSED_PARAMETER(argc);
107743  nCol = sqlite3_value_int(argv[0]);
107744  assert( nCol>0 );
107745  nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
107746  nKeyCol = sqlite3_value_int(argv[1]);
107747  assert( nKeyCol<=nCol );
107748  assert( nKeyCol>0 );
107749
107750  /* Allocate the space required for the StatAccum object */
107751  n = sizeof(*p)
107752    + sizeof(tRowcnt)*nColUp                  /* StatAccum.anEq */
107753    + sizeof(tRowcnt)*nColUp;                 /* StatAccum.anDLt */
107754#ifdef SQLITE_ENABLE_STAT4
107755  if( mxSample ){
107756    n += sizeof(tRowcnt)*nColUp                  /* StatAccum.anLt */
107757      + sizeof(StatSample)*(nCol+mxSample)       /* StatAccum.aBest[], a[] */
107758      + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample);
107759  }
107760#endif
107761  db = sqlite3_context_db_handle(context);
107762  p = sqlite3DbMallocZero(db, n);
107763  if( p==0 ){
107764    sqlite3_result_error_nomem(context);
107765    return;
107766  }
107767
107768  p->db = db;
107769  p->nEst = sqlite3_value_int64(argv[2]);
107770  p->nRow = 0;
107771  p->nLimit = sqlite3_value_int64(argv[3]);
107772  p->nCol = nCol;
107773  p->nKeyCol = nKeyCol;
107774  p->nSkipAhead = 0;
107775  p->current.anDLt = (tRowcnt*)&p[1];
107776  p->current.anEq = &p->current.anDLt[nColUp];
107777
107778#ifdef SQLITE_ENABLE_STAT4
107779  p->mxSample = p->nLimit==0 ? mxSample : 0;
107780  if( mxSample ){
107781    u8 *pSpace;                     /* Allocated space not yet assigned */
107782    int i;                          /* Used to iterate through p->aSample[] */
107783
107784    p->iGet = -1;
107785    p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
107786    p->current.anLt = &p->current.anEq[nColUp];
107787    p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
107788
107789    /* Set up the StatAccum.a[] and aBest[] arrays */
107790    p->a = (struct StatSample*)&p->current.anLt[nColUp];
107791    p->aBest = &p->a[mxSample];
107792    pSpace = (u8*)(&p->a[mxSample+nCol]);
107793    for(i=0; i<(mxSample+nCol); i++){
107794      p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
107795      p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
107796      p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
107797    }
107798    assert( (pSpace - (u8*)p)==n );
107799
107800    for(i=0; i<nCol; i++){
107801      p->aBest[i].iCol = i;
107802    }
107803  }
107804#endif
107805
107806  /* Return a pointer to the allocated object to the caller.  Note that
107807  ** only the pointer (the 2nd parameter) matters.  The size of the object
107808  ** (given by the 3rd parameter) is never used and can be any positive
107809  ** value. */
107810  sqlite3_result_blob(context, p, sizeof(*p), statAccumDestructor);
107811}
107812static const FuncDef statInitFuncdef = {
107813  4,               /* nArg */
107814  SQLITE_UTF8,     /* funcFlags */
107815  0,               /* pUserData */
107816  0,               /* pNext */
107817  statInit,        /* xSFunc */
107818  0,               /* xFinalize */
107819  0, 0,            /* xValue, xInverse */
107820  "stat_init",     /* zName */
107821  {0}
107822};
107823
107824#ifdef SQLITE_ENABLE_STAT4
107825/*
107826** pNew and pOld are both candidate non-periodic samples selected for
107827** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
107828** considering only any trailing columns and the sample hash value, this
107829** function returns true if sample pNew is to be preferred over pOld.
107830** In other words, if we assume that the cardinalities of the selected
107831** column for pNew and pOld are equal, is pNew to be preferred over pOld.
107832**
107833** This function assumes that for each argument sample, the contents of
107834** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
107835*/
107836static int sampleIsBetterPost(
107837  StatAccum *pAccum,
107838  StatSample *pNew,
107839  StatSample *pOld
107840){
107841  int nCol = pAccum->nCol;
107842  int i;
107843  assert( pNew->iCol==pOld->iCol );
107844  for(i=pNew->iCol+1; i<nCol; i++){
107845    if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
107846    if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
107847  }
107848  if( pNew->iHash>pOld->iHash ) return 1;
107849  return 0;
107850}
107851#endif
107852
107853#ifdef SQLITE_ENABLE_STAT4
107854/*
107855** Return true if pNew is to be preferred over pOld.
107856**
107857** This function assumes that for each argument sample, the contents of
107858** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
107859*/
107860static int sampleIsBetter(
107861  StatAccum *pAccum,
107862  StatSample *pNew,
107863  StatSample *pOld
107864){
107865  tRowcnt nEqNew = pNew->anEq[pNew->iCol];
107866  tRowcnt nEqOld = pOld->anEq[pOld->iCol];
107867
107868  assert( pOld->isPSample==0 && pNew->isPSample==0 );
107869  assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
107870
107871  if( (nEqNew>nEqOld) ) return 1;
107872  if( nEqNew==nEqOld ){
107873    if( pNew->iCol<pOld->iCol ) return 1;
107874    return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
107875  }
107876  return 0;
107877}
107878
107879/*
107880** Copy the contents of sample *pNew into the p->a[] array. If necessary,
107881** remove the least desirable sample from p->a[] to make room.
107882*/
107883static void sampleInsert(StatAccum *p, StatSample *pNew, int nEqZero){
107884  StatSample *pSample = 0;
107885  int i;
107886
107887  assert( IsStat4 || nEqZero==0 );
107888
107889  /* StatAccum.nMaxEqZero is set to the maximum number of leading 0
107890  ** values in the anEq[] array of any sample in StatAccum.a[]. In
107891  ** other words, if nMaxEqZero is n, then it is guaranteed that there
107892  ** are no samples with StatSample.anEq[m]==0 for (m>=n). */
107893  if( nEqZero>p->nMaxEqZero ){
107894    p->nMaxEqZero = nEqZero;
107895  }
107896  if( pNew->isPSample==0 ){
107897    StatSample *pUpgrade = 0;
107898    assert( pNew->anEq[pNew->iCol]>0 );
107899
107900    /* This sample is being added because the prefix that ends in column
107901    ** iCol occurs many times in the table. However, if we have already
107902    ** added a sample that shares this prefix, there is no need to add
107903    ** this one. Instead, upgrade the priority of the highest priority
107904    ** existing sample that shares this prefix.  */
107905    for(i=p->nSample-1; i>=0; i--){
107906      StatSample *pOld = &p->a[i];
107907      if( pOld->anEq[pNew->iCol]==0 ){
107908        if( pOld->isPSample ) return;
107909        assert( pOld->iCol>pNew->iCol );
107910        assert( sampleIsBetter(p, pNew, pOld) );
107911        if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
107912          pUpgrade = pOld;
107913        }
107914      }
107915    }
107916    if( pUpgrade ){
107917      pUpgrade->iCol = pNew->iCol;
107918      pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
107919      goto find_new_min;
107920    }
107921  }
107922
107923  /* If necessary, remove sample iMin to make room for the new sample. */
107924  if( p->nSample>=p->mxSample ){
107925    StatSample *pMin = &p->a[p->iMin];
107926    tRowcnt *anEq = pMin->anEq;
107927    tRowcnt *anLt = pMin->anLt;
107928    tRowcnt *anDLt = pMin->anDLt;
107929    sampleClear(p->db, pMin);
107930    memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
107931    pSample = &p->a[p->nSample-1];
107932    pSample->nRowid = 0;
107933    pSample->anEq = anEq;
107934    pSample->anDLt = anDLt;
107935    pSample->anLt = anLt;
107936    p->nSample = p->mxSample-1;
107937  }
107938
107939  /* The "rows less-than" for the rowid column must be greater than that
107940  ** for the last sample in the p->a[] array. Otherwise, the samples would
107941  ** be out of order. */
107942  assert( p->nSample==0
107943       || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
107944
107945  /* Insert the new sample */
107946  pSample = &p->a[p->nSample];
107947  sampleCopy(p, pSample, pNew);
107948  p->nSample++;
107949
107950  /* Zero the first nEqZero entries in the anEq[] array. */
107951  memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
107952
107953find_new_min:
107954  if( p->nSample>=p->mxSample ){
107955    int iMin = -1;
107956    for(i=0; i<p->mxSample; i++){
107957      if( p->a[i].isPSample ) continue;
107958      if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
107959        iMin = i;
107960      }
107961    }
107962    assert( iMin>=0 );
107963    p->iMin = iMin;
107964  }
107965}
107966#endif /* SQLITE_ENABLE_STAT4 */
107967
107968#ifdef SQLITE_ENABLE_STAT4
107969/*
107970** Field iChng of the index being scanned has changed. So at this point
107971** p->current contains a sample that reflects the previous row of the
107972** index. The value of anEq[iChng] and subsequent anEq[] elements are
107973** correct at this point.
107974*/
107975static void samplePushPrevious(StatAccum *p, int iChng){
107976  int i;
107977
107978  /* Check if any samples from the aBest[] array should be pushed
107979  ** into IndexSample.a[] at this point.  */
107980  for(i=(p->nCol-2); i>=iChng; i--){
107981    StatSample *pBest = &p->aBest[i];
107982    pBest->anEq[i] = p->current.anEq[i];
107983    if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
107984      sampleInsert(p, pBest, i);
107985    }
107986  }
107987
107988  /* Check that no sample contains an anEq[] entry with an index of
107989  ** p->nMaxEqZero or greater set to zero. */
107990  for(i=p->nSample-1; i>=0; i--){
107991    int j;
107992    for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
107993  }
107994
107995  /* Update the anEq[] fields of any samples already collected. */
107996  if( iChng<p->nMaxEqZero ){
107997    for(i=p->nSample-1; i>=0; i--){
107998      int j;
107999      for(j=iChng; j<p->nCol; j++){
108000        if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
108001      }
108002    }
108003    p->nMaxEqZero = iChng;
108004  }
108005}
108006#endif /* SQLITE_ENABLE_STAT4 */
108007
108008/*
108009** Implementation of the stat_push SQL function:  stat_push(P,C,R)
108010** Arguments:
108011**
108012**    P     Pointer to the StatAccum object created by stat_init()
108013**    C     Index of left-most column to differ from previous row
108014**    R     Rowid for the current row.  Might be a key record for
108015**          WITHOUT ROWID tables.
108016**
108017** The purpose of this routine is to collect statistical data and/or
108018** samples from the index being analyzed into the StatAccum object.
108019** The stat_get() SQL function will be used afterwards to
108020** retrieve the information gathered.
108021**
108022** This SQL function usually returns NULL, but might return an integer
108023** if it wants the byte-code to do special processing.
108024**
108025** The R parameter is only used for STAT4
108026*/
108027static void statPush(
108028  sqlite3_context *context,
108029  int argc,
108030  sqlite3_value **argv
108031){
108032  int i;
108033
108034  /* The three function arguments */
108035  StatAccum *p = (StatAccum*)sqlite3_value_blob(argv[0]);
108036  int iChng = sqlite3_value_int(argv[1]);
108037
108038  UNUSED_PARAMETER( argc );
108039  UNUSED_PARAMETER( context );
108040  assert( p->nCol>0 );
108041  assert( iChng<p->nCol );
108042
108043  if( p->nRow==0 ){
108044    /* This is the first call to this function. Do initialization. */
108045    for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
108046  }else{
108047    /* Second and subsequent calls get processed here */
108048#ifdef SQLITE_ENABLE_STAT4
108049    if( p->mxSample ) samplePushPrevious(p, iChng);
108050#endif
108051
108052    /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
108053    ** to the current row of the index. */
108054    for(i=0; i<iChng; i++){
108055      p->current.anEq[i]++;
108056    }
108057    for(i=iChng; i<p->nCol; i++){
108058      p->current.anDLt[i]++;
108059#ifdef SQLITE_ENABLE_STAT4
108060      if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
108061#endif
108062      p->current.anEq[i] = 1;
108063    }
108064  }
108065
108066  p->nRow++;
108067#ifdef SQLITE_ENABLE_STAT4
108068  if( p->mxSample ){
108069    tRowcnt nLt;
108070    if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){
108071      sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
108072    }else{
108073      sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
108074                                         sqlite3_value_blob(argv[2]));
108075    }
108076    p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
108077
108078    nLt = p->current.anLt[p->nCol-1];
108079    /* Check if this is to be a periodic sample. If so, add it. */
108080    if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
108081      p->current.isPSample = 1;
108082      p->current.iCol = 0;
108083      sampleInsert(p, &p->current, p->nCol-1);
108084      p->current.isPSample = 0;
108085    }
108086
108087    /* Update the aBest[] array. */
108088    for(i=0; i<(p->nCol-1); i++){
108089      p->current.iCol = i;
108090      if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
108091        sampleCopy(p, &p->aBest[i], &p->current);
108092      }
108093    }
108094  }else
108095#endif
108096  if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
108097    p->nSkipAhead++;
108098    sqlite3_result_int(context, p->current.anDLt[0]>0);
108099  }
108100}
108101
108102static const FuncDef statPushFuncdef = {
108103  2+IsStat4,       /* nArg */
108104  SQLITE_UTF8,     /* funcFlags */
108105  0,               /* pUserData */
108106  0,               /* pNext */
108107  statPush,        /* xSFunc */
108108  0,               /* xFinalize */
108109  0, 0,            /* xValue, xInverse */
108110  "stat_push",     /* zName */
108111  {0}
108112};
108113
108114#define STAT_GET_STAT1 0          /* "stat" column of stat1 table */
108115#define STAT_GET_ROWID 1          /* "rowid" column of stat[34] entry */
108116#define STAT_GET_NEQ   2          /* "neq" column of stat[34] entry */
108117#define STAT_GET_NLT   3          /* "nlt" column of stat[34] entry */
108118#define STAT_GET_NDLT  4          /* "ndlt" column of stat[34] entry */
108119
108120/*
108121** Implementation of the stat_get(P,J) SQL function.  This routine is
108122** used to query statistical information that has been gathered into
108123** the StatAccum object by prior calls to stat_push().  The P parameter
108124** has type BLOB but it is really just a pointer to the StatAccum object.
108125** The content to returned is determined by the parameter J
108126** which is one of the STAT_GET_xxxx values defined above.
108127**
108128** The stat_get(P,J) function is not available to generic SQL.  It is
108129** inserted as part of a manually constructed bytecode program.  (See
108130** the callStatGet() routine below.)  It is guaranteed that the P
108131** parameter will always be a pointer to a StatAccum object, never a
108132** NULL.
108133**
108134** If STAT4 is not enabled, then J is always
108135** STAT_GET_STAT1 and is hence omitted and this routine becomes
108136** a one-parameter function, stat_get(P), that always returns the
108137** stat1 table entry information.
108138*/
108139static void statGet(
108140  sqlite3_context *context,
108141  int argc,
108142  sqlite3_value **argv
108143){
108144  StatAccum *p = (StatAccum*)sqlite3_value_blob(argv[0]);
108145#ifdef SQLITE_ENABLE_STAT4
108146  /* STAT4 has a parameter on this routine. */
108147  int eCall = sqlite3_value_int(argv[1]);
108148  assert( argc==2 );
108149  assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
108150       || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
108151       || eCall==STAT_GET_NDLT
108152  );
108153  assert( eCall==STAT_GET_STAT1 || p->mxSample );
108154  if( eCall==STAT_GET_STAT1 )
108155#else
108156  assert( argc==1 );
108157#endif
108158  {
108159    /* Return the value to store in the "stat" column of the sqlite_stat1
108160    ** table for this index.
108161    **
108162    ** The value is a string composed of a list of integers describing
108163    ** the index. The first integer in the list is the total number of
108164    ** entries in the index. There is one additional integer in the list
108165    ** for each indexed column. This additional integer is an estimate of
108166    ** the number of rows matched by a equality query on the index using
108167    ** a key with the corresponding number of fields. In other words,
108168    ** if the index is on columns (a,b) and the sqlite_stat1 value is
108169    ** "100 10 2", then SQLite estimates that:
108170    **
108171    **   * the index contains 100 rows,
108172    **   * "WHERE a=?" matches 10 rows, and
108173    **   * "WHERE a=? AND b=?" matches 2 rows.
108174    **
108175    ** If D is the count of distinct values and K is the total number of
108176    ** rows, then each estimate is computed as:
108177    **
108178    **        I = (K+D-1)/D
108179    */
108180    char *z;
108181    int i;
108182
108183    char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 );
108184    if( zRet==0 ){
108185      sqlite3_result_error_nomem(context);
108186      return;
108187    }
108188
108189    sqlite3_snprintf(24, zRet, "%llu",
108190        p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
108191    z = zRet + sqlite3Strlen30(zRet);
108192    for(i=0; i<p->nKeyCol; i++){
108193      u64 nDistinct = p->current.anDLt[i] + 1;
108194      u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
108195      sqlite3_snprintf(24, z, " %llu", iVal);
108196      z += sqlite3Strlen30(z);
108197      assert( p->current.anEq[i] );
108198    }
108199    assert( z[0]=='\0' && z>zRet );
108200
108201    sqlite3_result_text(context, zRet, -1, sqlite3_free);
108202  }
108203#ifdef SQLITE_ENABLE_STAT4
108204  else if( eCall==STAT_GET_ROWID ){
108205    if( p->iGet<0 ){
108206      samplePushPrevious(p, 0);
108207      p->iGet = 0;
108208    }
108209    if( p->iGet<p->nSample ){
108210      StatSample *pS = p->a + p->iGet;
108211      if( pS->nRowid==0 ){
108212        sqlite3_result_int64(context, pS->u.iRowid);
108213      }else{
108214        sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
108215                            SQLITE_TRANSIENT);
108216      }
108217    }
108218  }else{
108219    tRowcnt *aCnt = 0;
108220
108221    assert( p->iGet<p->nSample );
108222    switch( eCall ){
108223      case STAT_GET_NEQ:  aCnt = p->a[p->iGet].anEq; break;
108224      case STAT_GET_NLT:  aCnt = p->a[p->iGet].anLt; break;
108225      default: {
108226        aCnt = p->a[p->iGet].anDLt;
108227        p->iGet++;
108228        break;
108229      }
108230    }
108231
108232    {
108233      char *zRet = sqlite3MallocZero(p->nCol * 25);
108234      if( zRet==0 ){
108235        sqlite3_result_error_nomem(context);
108236      }else{
108237        int i;
108238        char *z = zRet;
108239        for(i=0; i<p->nCol; i++){
108240          sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]);
108241          z += sqlite3Strlen30(z);
108242        }
108243        assert( z[0]=='\0' && z>zRet );
108244        z[-1] = '\0';
108245        sqlite3_result_text(context, zRet, -1, sqlite3_free);
108246      }
108247    }
108248  }
108249#endif /* SQLITE_ENABLE_STAT4 */
108250#ifndef SQLITE_DEBUG
108251  UNUSED_PARAMETER( argc );
108252#endif
108253}
108254static const FuncDef statGetFuncdef = {
108255  1+IsStat4,       /* nArg */
108256  SQLITE_UTF8,     /* funcFlags */
108257  0,               /* pUserData */
108258  0,               /* pNext */
108259  statGet,         /* xSFunc */
108260  0,               /* xFinalize */
108261  0, 0,            /* xValue, xInverse */
108262  "stat_get",      /* zName */
108263  {0}
108264};
108265
108266static void callStatGet(Parse *pParse, int regStat, int iParam, int regOut){
108267#ifdef SQLITE_ENABLE_STAT4
108268  sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1);
108269#elif SQLITE_DEBUG
108270  assert( iParam==STAT_GET_STAT1 );
108271#else
108272  UNUSED_PARAMETER( iParam );
108273#endif
108274  assert( regOut!=regStat && regOut!=regStat+1 );
108275  sqlite3VdbeAddFunctionCall(pParse, 0, regStat, regOut, 1+IsStat4,
108276                             &statGetFuncdef, 0);
108277}
108278
108279/*
108280** Generate code to do an analysis of all indices associated with
108281** a single table.
108282*/
108283static void analyzeOneTable(
108284  Parse *pParse,   /* Parser context */
108285  Table *pTab,     /* Table whose indices are to be analyzed */
108286  Index *pOnlyIdx, /* If not NULL, only analyze this one index */
108287  int iStatCur,    /* Index of VdbeCursor that writes the sqlite_stat1 table */
108288  int iMem,        /* Available memory locations begin here */
108289  int iTab         /* Next available cursor */
108290){
108291  sqlite3 *db = pParse->db;    /* Database handle */
108292  Index *pIdx;                 /* An index to being analyzed */
108293  int iIdxCur;                 /* Cursor open on index being analyzed */
108294  int iTabCur;                 /* Table cursor */
108295  Vdbe *v;                     /* The virtual machine being built up */
108296  int i;                       /* Loop counter */
108297  int jZeroRows = -1;          /* Jump from here if number of rows is zero */
108298  int iDb;                     /* Index of database containing pTab */
108299  u8 needTableCnt = 1;         /* True to count the table */
108300  int regNewRowid = iMem++;    /* Rowid for the inserted record */
108301  int regStat = iMem++;        /* Register to hold StatAccum object */
108302  int regChng = iMem++;        /* Index of changed index field */
108303  int regRowid = iMem++;       /* Rowid argument passed to stat_push() */
108304  int regTemp = iMem++;        /* Temporary use register */
108305  int regTemp2 = iMem++;       /* Second temporary use register */
108306  int regTabname = iMem++;     /* Register containing table name */
108307  int regIdxname = iMem++;     /* Register containing index name */
108308  int regStat1 = iMem++;       /* Value for the stat column of sqlite_stat1 */
108309  int regPrev = iMem;          /* MUST BE LAST (see below) */
108310#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108311  Table *pStat1 = 0;
108312#endif
108313
108314  pParse->nMem = MAX(pParse->nMem, iMem);
108315  v = sqlite3GetVdbe(pParse);
108316  if( v==0 || NEVER(pTab==0) ){
108317    return;
108318  }
108319  if( pTab->tnum==0 ){
108320    /* Do not gather statistics on views or virtual tables */
108321    return;
108322  }
108323  if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
108324    /* Do not gather statistics on system tables */
108325    return;
108326  }
108327  assert( sqlite3BtreeHoldsAllMutexes(db) );
108328  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108329  assert( iDb>=0 );
108330  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108331#ifndef SQLITE_OMIT_AUTHORIZATION
108332  if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
108333      db->aDb[iDb].zDbSName ) ){
108334    return;
108335  }
108336#endif
108337
108338#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108339  if( db->xPreUpdateCallback ){
108340    pStat1 = (Table*)sqlite3DbMallocZero(db, sizeof(Table) + 13);
108341    if( pStat1==0 ) return;
108342    pStat1->zName = (char*)&pStat1[1];
108343    memcpy(pStat1->zName, "sqlite_stat1", 13);
108344    pStat1->nCol = 3;
108345    pStat1->iPKey = -1;
108346    sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNBLOB);
108347  }
108348#endif
108349
108350  /* Establish a read-lock on the table at the shared-cache level.
108351  ** Open a read-only cursor on the table. Also allocate a cursor number
108352  ** to use for scanning indexes (iIdxCur). No index cursor is opened at
108353  ** this time though.  */
108354  sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
108355  iTabCur = iTab++;
108356  iIdxCur = iTab++;
108357  pParse->nTab = MAX(pParse->nTab, iTab);
108358  sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
108359  sqlite3VdbeLoadString(v, regTabname, pTab->zName);
108360
108361  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108362    int nCol;                     /* Number of columns in pIdx. "N" */
108363    int addrRewind;               /* Address of "OP_Rewind iIdxCur" */
108364    int addrNextRow;              /* Address of "next_row:" */
108365    const char *zIdxName;         /* Name of the index */
108366    int nColTest;                 /* Number of columns to test for changes */
108367
108368    if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
108369    if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
108370    if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIdx) ){
108371      nCol = pIdx->nKeyCol;
108372      zIdxName = pTab->zName;
108373      nColTest = nCol - 1;
108374    }else{
108375      nCol = pIdx->nColumn;
108376      zIdxName = pIdx->zName;
108377      nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
108378    }
108379
108380    /* Populate the register containing the index name. */
108381    sqlite3VdbeLoadString(v, regIdxname, zIdxName);
108382    VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
108383
108384    /*
108385    ** Pseudo-code for loop that calls stat_push():
108386    **
108387    **   Rewind csr
108388    **   if eof(csr) goto end_of_scan;
108389    **   regChng = 0
108390    **   goto chng_addr_0;
108391    **
108392    **  next_row:
108393    **   regChng = 0
108394    **   if( idx(0) != regPrev(0) ) goto chng_addr_0
108395    **   regChng = 1
108396    **   if( idx(1) != regPrev(1) ) goto chng_addr_1
108397    **   ...
108398    **   regChng = N
108399    **   goto chng_addr_N
108400    **
108401    **  chng_addr_0:
108402    **   regPrev(0) = idx(0)
108403    **  chng_addr_1:
108404    **   regPrev(1) = idx(1)
108405    **  ...
108406    **
108407    **  endDistinctTest:
108408    **   regRowid = idx(rowid)
108409    **   stat_push(P, regChng, regRowid)
108410    **   Next csr
108411    **   if !eof(csr) goto next_row;
108412    **
108413    **  end_of_scan:
108414    */
108415
108416    /* Make sure there are enough memory cells allocated to accommodate
108417    ** the regPrev array and a trailing rowid (the rowid slot is required
108418    ** when building a record to insert into the sample column of
108419    ** the sqlite_stat4 table.  */
108420    pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
108421
108422    /* Open a read-only cursor on the index being analyzed. */
108423    assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
108424    sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
108425    sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
108426    VdbeComment((v, "%s", pIdx->zName));
108427
108428    /* Invoke the stat_init() function. The arguments are:
108429    **
108430    **    (1) the number of columns in the index including the rowid
108431    **        (or for a WITHOUT ROWID table, the number of PK columns),
108432    **    (2) the number of columns in the key without the rowid/pk
108433    **    (3) estimated number of rows in the index,
108434    */
108435    sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat+1);
108436    assert( regRowid==regStat+2 );
108437    sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regRowid);
108438#ifdef SQLITE_ENABLE_STAT4
108439    if( OptimizationEnabled(db, SQLITE_Stat4) ){
108440      sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regTemp);
108441      addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
108442      VdbeCoverage(v);
108443    }else
108444#endif
108445    {
108446      addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
108447      VdbeCoverage(v);
108448      sqlite3VdbeAddOp3(v, OP_Count, iIdxCur, regTemp, 1);
108449    }
108450    assert( regTemp2==regStat+4 );
108451    sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2);
108452    sqlite3VdbeAddFunctionCall(pParse, 0, regStat+1, regStat, 4,
108453                               &statInitFuncdef, 0);
108454
108455    /* Implementation of the following:
108456    **
108457    **   Rewind csr
108458    **   if eof(csr) goto end_of_scan;
108459    **   regChng = 0
108460    **   goto next_push_0;
108461    **
108462    */
108463    sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
108464    addrNextRow = sqlite3VdbeCurrentAddr(v);
108465
108466    if( nColTest>0 ){
108467      int endDistinctTest = sqlite3VdbeMakeLabel(pParse);
108468      int *aGotoChng;               /* Array of jump instruction addresses */
108469      aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest);
108470      if( aGotoChng==0 ) continue;
108471
108472      /*
108473      **  next_row:
108474      **   regChng = 0
108475      **   if( idx(0) != regPrev(0) ) goto chng_addr_0
108476      **   regChng = 1
108477      **   if( idx(1) != regPrev(1) ) goto chng_addr_1
108478      **   ...
108479      **   regChng = N
108480      **   goto endDistinctTest
108481      */
108482      sqlite3VdbeAddOp0(v, OP_Goto);
108483      addrNextRow = sqlite3VdbeCurrentAddr(v);
108484      if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
108485        /* For a single-column UNIQUE index, once we have found a non-NULL
108486        ** row, we know that all the rest will be distinct, so skip
108487        ** subsequent distinctness tests. */
108488        sqlite3VdbeAddOp2(v, OP_NotNull, regPrev, endDistinctTest);
108489        VdbeCoverage(v);
108490      }
108491      for(i=0; i<nColTest; i++){
108492        char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
108493        sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
108494        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
108495        VdbeComment((v, "%s.column(%d)", pIdx->zName, i));
108496        aGotoChng[i] =
108497        sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
108498        sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
108499        VdbeCoverage(v);
108500      }
108501      sqlite3VdbeAddOp2(v, OP_Integer, nColTest, regChng);
108502      sqlite3VdbeGoto(v, endDistinctTest);
108503
108504
108505      /*
108506      **  chng_addr_0:
108507      **   regPrev(0) = idx(0)
108508      **  chng_addr_1:
108509      **   regPrev(1) = idx(1)
108510      **  ...
108511      */
108512      sqlite3VdbeJumpHere(v, addrNextRow-1);
108513      for(i=0; i<nColTest; i++){
108514        sqlite3VdbeJumpHere(v, aGotoChng[i]);
108515        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
108516        VdbeComment((v, "%s.column(%d)", pIdx->zName, i));
108517      }
108518      sqlite3VdbeResolveLabel(v, endDistinctTest);
108519      sqlite3DbFree(db, aGotoChng);
108520    }
108521
108522    /*
108523    **  chng_addr_N:
108524    **   regRowid = idx(rowid)            // STAT4 only
108525    **   stat_push(P, regChng, regRowid)  // 3rd parameter STAT4 only
108526    **   Next csr
108527    **   if !eof(csr) goto next_row;
108528    */
108529#ifdef SQLITE_ENABLE_STAT4
108530    if( OptimizationEnabled(db, SQLITE_Stat4) ){
108531      assert( regRowid==(regStat+2) );
108532      if( HasRowid(pTab) ){
108533        sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
108534      }else{
108535        Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
108536        int j, k, regKey;
108537        regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
108538        for(j=0; j<pPk->nKeyCol; j++){
108539          k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
108540          assert( k>=0 && k<pIdx->nColumn );
108541          sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
108542          VdbeComment((v, "%s.column(%d)", pIdx->zName, i));
108543        }
108544        sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
108545        sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
108546      }
108547    }
108548#endif
108549    assert( regChng==(regStat+1) );
108550    {
108551      sqlite3VdbeAddFunctionCall(pParse, 1, regStat, regTemp, 2+IsStat4,
108552                                 &statPushFuncdef, 0);
108553      if( db->nAnalysisLimit ){
108554        int j1, j2, j3;
108555        j1 = sqlite3VdbeAddOp1(v, OP_IsNull, regTemp); VdbeCoverage(v);
108556        j2 = sqlite3VdbeAddOp1(v, OP_If, regTemp); VdbeCoverage(v);
108557        j3 = sqlite3VdbeAddOp4Int(v, OP_SeekGT, iIdxCur, 0, regPrev, 1);
108558        VdbeCoverage(v);
108559        sqlite3VdbeJumpHere(v, j1);
108560        sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
108561        sqlite3VdbeJumpHere(v, j2);
108562        sqlite3VdbeJumpHere(v, j3);
108563      }else{
108564        sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
108565      }
108566    }
108567
108568    /* Add the entry to the stat1 table. */
108569    callStatGet(pParse, regStat, STAT_GET_STAT1, regStat1);
108570    assert( "BBB"[0]==SQLITE_AFF_TEXT );
108571    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
108572    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
108573    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
108574#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108575    sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
108576#endif
108577    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
108578
108579    /* Add the entries to the stat4 table. */
108580#ifdef SQLITE_ENABLE_STAT4
108581    if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){
108582      int regEq = regStat1;
108583      int regLt = regStat1+1;
108584      int regDLt = regStat1+2;
108585      int regSample = regStat1+3;
108586      int regCol = regStat1+4;
108587      int regSampleRowid = regCol + nCol;
108588      int addrNext;
108589      int addrIsNull;
108590      u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
108591
108592      pParse->nMem = MAX(pParse->nMem, regCol+nCol);
108593
108594      addrNext = sqlite3VdbeCurrentAddr(v);
108595      callStatGet(pParse, regStat, STAT_GET_ROWID, regSampleRowid);
108596      addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
108597      VdbeCoverage(v);
108598      callStatGet(pParse, regStat, STAT_GET_NEQ, regEq);
108599      callStatGet(pParse, regStat, STAT_GET_NLT, regLt);
108600      callStatGet(pParse, regStat, STAT_GET_NDLT, regDLt);
108601      sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
108602      VdbeCoverage(v);
108603      for(i=0; i<nCol; i++){
108604        sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i);
108605      }
108606      sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample);
108607      sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
108608      sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
108609      sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
108610      sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
108611      sqlite3VdbeJumpHere(v, addrIsNull);
108612    }
108613#endif /* SQLITE_ENABLE_STAT4 */
108614
108615    /* End of analysis */
108616    sqlite3VdbeJumpHere(v, addrRewind);
108617  }
108618
108619
108620  /* Create a single sqlite_stat1 entry containing NULL as the index
108621  ** name and the row count as the content.
108622  */
108623  if( pOnlyIdx==0 && needTableCnt ){
108624    VdbeComment((v, "%s", pTab->zName));
108625    sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
108626    jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v);
108627    sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
108628    assert( "BBB"[0]==SQLITE_AFF_TEXT );
108629    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
108630    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
108631    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
108632    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
108633#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108634    sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
108635#endif
108636    sqlite3VdbeJumpHere(v, jZeroRows);
108637  }
108638}
108639
108640
108641/*
108642** Generate code that will cause the most recent index analysis to
108643** be loaded into internal hash tables where is can be used.
108644*/
108645static void loadAnalysis(Parse *pParse, int iDb){
108646  Vdbe *v = sqlite3GetVdbe(pParse);
108647  if( v ){
108648    sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
108649  }
108650}
108651
108652/*
108653** Generate code that will do an analysis of an entire database
108654*/
108655static void analyzeDatabase(Parse *pParse, int iDb){
108656  sqlite3 *db = pParse->db;
108657  Schema *pSchema = db->aDb[iDb].pSchema;    /* Schema of database iDb */
108658  HashElem *k;
108659  int iStatCur;
108660  int iMem;
108661  int iTab;
108662
108663  sqlite3BeginWriteOperation(pParse, 0, iDb);
108664  iStatCur = pParse->nTab;
108665  pParse->nTab += 3;
108666  openStatTable(pParse, iDb, iStatCur, 0, 0);
108667  iMem = pParse->nMem+1;
108668  iTab = pParse->nTab;
108669  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108670  for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
108671    Table *pTab = (Table*)sqliteHashData(k);
108672    analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
108673  }
108674  loadAnalysis(pParse, iDb);
108675}
108676
108677/*
108678** Generate code that will do an analysis of a single table in
108679** a database.  If pOnlyIdx is not NULL then it is a single index
108680** in pTab that should be analyzed.
108681*/
108682static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
108683  int iDb;
108684  int iStatCur;
108685
108686  assert( pTab!=0 );
108687  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
108688  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
108689  sqlite3BeginWriteOperation(pParse, 0, iDb);
108690  iStatCur = pParse->nTab;
108691  pParse->nTab += 3;
108692  if( pOnlyIdx ){
108693    openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
108694  }else{
108695    openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
108696  }
108697  analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
108698  loadAnalysis(pParse, iDb);
108699}
108700
108701/*
108702** Generate code for the ANALYZE command.  The parser calls this routine
108703** when it recognizes an ANALYZE command.
108704**
108705**        ANALYZE                            -- 1
108706**        ANALYZE  <database>                -- 2
108707**        ANALYZE  ?<database>.?<tablename>  -- 3
108708**
108709** Form 1 causes all indices in all attached databases to be analyzed.
108710** Form 2 analyzes all indices the single database named.
108711** Form 3 analyzes all indices associated with the named table.
108712*/
108713SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
108714  sqlite3 *db = pParse->db;
108715  int iDb;
108716  int i;
108717  char *z, *zDb;
108718  Table *pTab;
108719  Index *pIdx;
108720  Token *pTableName;
108721  Vdbe *v;
108722
108723  /* Read the database schema. If an error occurs, leave an error message
108724  ** and code in pParse and return NULL. */
108725  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
108726  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
108727    return;
108728  }
108729
108730  assert( pName2!=0 || pName1==0 );
108731  if( pName1==0 ){
108732    /* Form 1:  Analyze everything */
108733    for(i=0; i<db->nDb; i++){
108734      if( i==1 ) continue;  /* Do not analyze the TEMP database */
108735      analyzeDatabase(pParse, i);
108736    }
108737  }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
108738    /* Analyze the schema named as the argument */
108739    analyzeDatabase(pParse, iDb);
108740  }else{
108741    /* Form 3: Analyze the table or index named as an argument */
108742    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
108743    if( iDb>=0 ){
108744      zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
108745      z = sqlite3NameFromToken(db, pTableName);
108746      if( z ){
108747        if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
108748          analyzeTable(pParse, pIdx->pTable, pIdx);
108749        }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
108750          analyzeTable(pParse, pTab, 0);
108751        }
108752        sqlite3DbFree(db, z);
108753      }
108754    }
108755  }
108756  if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
108757    sqlite3VdbeAddOp0(v, OP_Expire);
108758  }
108759}
108760
108761/*
108762** Used to pass information from the analyzer reader through to the
108763** callback routine.
108764*/
108765typedef struct analysisInfo analysisInfo;
108766struct analysisInfo {
108767  sqlite3 *db;
108768  const char *zDatabase;
108769};
108770
108771/*
108772** The first argument points to a nul-terminated string containing a
108773** list of space separated integers. Read the first nOut of these into
108774** the array aOut[].
108775*/
108776static void decodeIntArray(
108777  char *zIntArray,       /* String containing int array to decode */
108778  int nOut,              /* Number of slots in aOut[] */
108779  tRowcnt *aOut,         /* Store integers here */
108780  LogEst *aLog,          /* Or, if aOut==0, here */
108781  Index *pIndex          /* Handle extra flags for this index, if not NULL */
108782){
108783  char *z = zIntArray;
108784  int c;
108785  int i;
108786  tRowcnt v;
108787
108788#ifdef SQLITE_ENABLE_STAT4
108789  if( z==0 ) z = "";
108790#else
108791  assert( z!=0 );
108792#endif
108793  for(i=0; *z && i<nOut; i++){
108794    v = 0;
108795    while( (c=z[0])>='0' && c<='9' ){
108796      v = v*10 + c - '0';
108797      z++;
108798    }
108799#ifdef SQLITE_ENABLE_STAT4
108800    if( aOut ) aOut[i] = v;
108801    if( aLog ) aLog[i] = sqlite3LogEst(v);
108802#else
108803    assert( aOut==0 );
108804    UNUSED_PARAMETER(aOut);
108805    assert( aLog!=0 );
108806    aLog[i] = sqlite3LogEst(v);
108807#endif
108808    if( *z==' ' ) z++;
108809  }
108810#ifndef SQLITE_ENABLE_STAT4
108811  assert( pIndex!=0 ); {
108812#else
108813  if( pIndex ){
108814#endif
108815    pIndex->bUnordered = 0;
108816    pIndex->noSkipScan = 0;
108817    while( z[0] ){
108818      if( sqlite3_strglob("unordered*", z)==0 ){
108819        pIndex->bUnordered = 1;
108820      }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
108821        int sz = sqlite3Atoi(z+3);
108822        if( sz<2 ) sz = 2;
108823        pIndex->szIdxRow = sqlite3LogEst(sz);
108824      }else if( sqlite3_strglob("noskipscan*", z)==0 ){
108825        pIndex->noSkipScan = 1;
108826      }
108827#ifdef SQLITE_ENABLE_COSTMULT
108828      else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
108829        pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
108830      }
108831#endif
108832      while( z[0]!=0 && z[0]!=' ' ) z++;
108833      while( z[0]==' ' ) z++;
108834    }
108835  }
108836}
108837
108838/*
108839** This callback is invoked once for each index when reading the
108840** sqlite_stat1 table.
108841**
108842**     argv[0] = name of the table
108843**     argv[1] = name of the index (might be NULL)
108844**     argv[2] = results of analysis - on integer for each column
108845**
108846** Entries for which argv[1]==NULL simply record the number of rows in
108847** the table.
108848*/
108849static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
108850  analysisInfo *pInfo = (analysisInfo*)pData;
108851  Index *pIndex;
108852  Table *pTable;
108853  const char *z;
108854
108855  assert( argc==3 );
108856  UNUSED_PARAMETER2(NotUsed, argc);
108857
108858  if( argv==0 || argv[0]==0 || argv[2]==0 ){
108859    return 0;
108860  }
108861  pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
108862  if( pTable==0 ){
108863    return 0;
108864  }
108865  if( argv[1]==0 ){
108866    pIndex = 0;
108867  }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){
108868    pIndex = sqlite3PrimaryKeyIndex(pTable);
108869  }else{
108870    pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
108871  }
108872  z = argv[2];
108873
108874  if( pIndex ){
108875    tRowcnt *aiRowEst = 0;
108876    int nCol = pIndex->nKeyCol+1;
108877#ifdef SQLITE_ENABLE_STAT4
108878    /* Index.aiRowEst may already be set here if there are duplicate
108879    ** sqlite_stat1 entries for this index. In that case just clobber
108880    ** the old data with the new instead of allocating a new array.  */
108881    if( pIndex->aiRowEst==0 ){
108882      pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
108883      if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
108884    }
108885    aiRowEst = pIndex->aiRowEst;
108886#endif
108887    pIndex->bUnordered = 0;
108888    decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
108889    pIndex->hasStat1 = 1;
108890    if( pIndex->pPartIdxWhere==0 ){
108891      pTable->nRowLogEst = pIndex->aiRowLogEst[0];
108892      pTable->tabFlags |= TF_HasStat1;
108893    }
108894  }else{
108895    Index fakeIdx;
108896    fakeIdx.szIdxRow = pTable->szTabRow;
108897#ifdef SQLITE_ENABLE_COSTMULT
108898    fakeIdx.pTable = pTable;
108899#endif
108900    decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
108901    pTable->szTabRow = fakeIdx.szIdxRow;
108902    pTable->tabFlags |= TF_HasStat1;
108903  }
108904
108905  return 0;
108906}
108907
108908/*
108909** If the Index.aSample variable is not NULL, delete the aSample[] array
108910** and its contents.
108911*/
108912SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
108913#ifdef SQLITE_ENABLE_STAT4
108914  if( pIdx->aSample ){
108915    int j;
108916    for(j=0; j<pIdx->nSample; j++){
108917      IndexSample *p = &pIdx->aSample[j];
108918      sqlite3DbFree(db, p->p);
108919    }
108920    sqlite3DbFree(db, pIdx->aSample);
108921  }
108922  if( db && db->pnBytesFreed==0 ){
108923    pIdx->nSample = 0;
108924    pIdx->aSample = 0;
108925  }
108926#else
108927  UNUSED_PARAMETER(db);
108928  UNUSED_PARAMETER(pIdx);
108929#endif /* SQLITE_ENABLE_STAT4 */
108930}
108931
108932#ifdef SQLITE_ENABLE_STAT4
108933/*
108934** Populate the pIdx->aAvgEq[] array based on the samples currently
108935** stored in pIdx->aSample[].
108936*/
108937static void initAvgEq(Index *pIdx){
108938  if( pIdx ){
108939    IndexSample *aSample = pIdx->aSample;
108940    IndexSample *pFinal = &aSample[pIdx->nSample-1];
108941    int iCol;
108942    int nCol = 1;
108943    if( pIdx->nSampleCol>1 ){
108944      /* If this is stat4 data, then calculate aAvgEq[] values for all
108945      ** sample columns except the last. The last is always set to 1, as
108946      ** once the trailing PK fields are considered all index keys are
108947      ** unique.  */
108948      nCol = pIdx->nSampleCol-1;
108949      pIdx->aAvgEq[nCol] = 1;
108950    }
108951    for(iCol=0; iCol<nCol; iCol++){
108952      int nSample = pIdx->nSample;
108953      int i;                    /* Used to iterate through samples */
108954      tRowcnt sumEq = 0;        /* Sum of the nEq values */
108955      tRowcnt avgEq = 0;
108956      tRowcnt nRow;             /* Number of rows in index */
108957      i64 nSum100 = 0;          /* Number of terms contributing to sumEq */
108958      i64 nDist100;             /* Number of distinct values in index */
108959
108960      if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
108961        nRow = pFinal->anLt[iCol];
108962        nDist100 = (i64)100 * pFinal->anDLt[iCol];
108963        nSample--;
108964      }else{
108965        nRow = pIdx->aiRowEst[0];
108966        nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
108967      }
108968      pIdx->nRowEst0 = nRow;
108969
108970      /* Set nSum to the number of distinct (iCol+1) field prefixes that
108971      ** occur in the stat4 table for this index. Set sumEq to the sum of
108972      ** the nEq values for column iCol for the same set (adding the value
108973      ** only once where there exist duplicate prefixes).  */
108974      for(i=0; i<nSample; i++){
108975        if( i==(pIdx->nSample-1)
108976         || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol]
108977        ){
108978          sumEq += aSample[i].anEq[iCol];
108979          nSum100 += 100;
108980        }
108981      }
108982
108983      if( nDist100>nSum100 && sumEq<nRow ){
108984        avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
108985      }
108986      if( avgEq==0 ) avgEq = 1;
108987      pIdx->aAvgEq[iCol] = avgEq;
108988    }
108989  }
108990}
108991
108992/*
108993** Look up an index by name.  Or, if the name of a WITHOUT ROWID table
108994** is supplied instead, find the PRIMARY KEY index for that table.
108995*/
108996static Index *findIndexOrPrimaryKey(
108997  sqlite3 *db,
108998  const char *zName,
108999  const char *zDb
109000){
109001  Index *pIdx = sqlite3FindIndex(db, zName, zDb);
109002  if( pIdx==0 ){
109003    Table *pTab = sqlite3FindTable(db, zName, zDb);
109004    if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
109005  }
109006  return pIdx;
109007}
109008
109009/*
109010** Load the content from either the sqlite_stat4
109011** into the relevant Index.aSample[] arrays.
109012**
109013** Arguments zSql1 and zSql2 must point to SQL statements that return
109014** data equivalent to the following:
109015**
109016**    zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
109017**    zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
109018**
109019** where %Q is replaced with the database name before the SQL is executed.
109020*/
109021static int loadStatTbl(
109022  sqlite3 *db,                  /* Database handle */
109023  const char *zSql1,            /* SQL statement 1 (see above) */
109024  const char *zSql2,            /* SQL statement 2 (see above) */
109025  const char *zDb               /* Database name (e.g. "main") */
109026){
109027  int rc;                       /* Result codes from subroutines */
109028  sqlite3_stmt *pStmt = 0;      /* An SQL statement being run */
109029  char *zSql;                   /* Text of the SQL statement */
109030  Index *pPrevIdx = 0;          /* Previous index in the loop */
109031  IndexSample *pSample;         /* A slot in pIdx->aSample[] */
109032
109033  assert( db->lookaside.bDisable );
109034  zSql = sqlite3MPrintf(db, zSql1, zDb);
109035  if( !zSql ){
109036    return SQLITE_NOMEM_BKPT;
109037  }
109038  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
109039  sqlite3DbFree(db, zSql);
109040  if( rc ) return rc;
109041
109042  while( sqlite3_step(pStmt)==SQLITE_ROW ){
109043    int nIdxCol = 1;              /* Number of columns in stat4 records */
109044
109045    char *zIndex;   /* Index name */
109046    Index *pIdx;    /* Pointer to the index object */
109047    int nSample;    /* Number of samples */
109048    int nByte;      /* Bytes of space required */
109049    int i;          /* Bytes of space required */
109050    tRowcnt *pSpace;
109051
109052    zIndex = (char *)sqlite3_column_text(pStmt, 0);
109053    if( zIndex==0 ) continue;
109054    nSample = sqlite3_column_int(pStmt, 1);
109055    pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
109056    assert( pIdx==0 || pIdx->nSample==0 );
109057    if( pIdx==0 ) continue;
109058    assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
109059    if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
109060      nIdxCol = pIdx->nKeyCol;
109061    }else{
109062      nIdxCol = pIdx->nColumn;
109063    }
109064    pIdx->nSampleCol = nIdxCol;
109065    nByte = sizeof(IndexSample) * nSample;
109066    nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
109067    nByte += nIdxCol * sizeof(tRowcnt);     /* Space for Index.aAvgEq[] */
109068
109069    pIdx->aSample = sqlite3DbMallocZero(db, nByte);
109070    if( pIdx->aSample==0 ){
109071      sqlite3_finalize(pStmt);
109072      return SQLITE_NOMEM_BKPT;
109073    }
109074    pSpace = (tRowcnt*)&pIdx->aSample[nSample];
109075    pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
109076    for(i=0; i<nSample; i++){
109077      pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
109078      pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
109079      pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
109080    }
109081    assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
109082  }
109083  rc = sqlite3_finalize(pStmt);
109084  if( rc ) return rc;
109085
109086  zSql = sqlite3MPrintf(db, zSql2, zDb);
109087  if( !zSql ){
109088    return SQLITE_NOMEM_BKPT;
109089  }
109090  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
109091  sqlite3DbFree(db, zSql);
109092  if( rc ) return rc;
109093
109094  while( sqlite3_step(pStmt)==SQLITE_ROW ){
109095    char *zIndex;                 /* Index name */
109096    Index *pIdx;                  /* Pointer to the index object */
109097    int nCol = 1;                 /* Number of columns in index */
109098
109099    zIndex = (char *)sqlite3_column_text(pStmt, 0);
109100    if( zIndex==0 ) continue;
109101    pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
109102    if( pIdx==0 ) continue;
109103    /* This next condition is true if data has already been loaded from
109104    ** the sqlite_stat4 table. */
109105    nCol = pIdx->nSampleCol;
109106    if( pIdx!=pPrevIdx ){
109107      initAvgEq(pPrevIdx);
109108      pPrevIdx = pIdx;
109109    }
109110    pSample = &pIdx->aSample[pIdx->nSample];
109111    decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
109112    decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
109113    decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
109114
109115    /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
109116    ** This is in case the sample record is corrupted. In that case, the
109117    ** sqlite3VdbeRecordCompare() may read up to two varints past the
109118    ** end of the allocated buffer before it realizes it is dealing with
109119    ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
109120    ** a buffer overread.  */
109121    pSample->n = sqlite3_column_bytes(pStmt, 4);
109122    pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
109123    if( pSample->p==0 ){
109124      sqlite3_finalize(pStmt);
109125      return SQLITE_NOMEM_BKPT;
109126    }
109127    if( pSample->n ){
109128      memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
109129    }
109130    pIdx->nSample++;
109131  }
109132  rc = sqlite3_finalize(pStmt);
109133  if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
109134  return rc;
109135}
109136
109137/*
109138** Load content from the sqlite_stat4 table into
109139** the Index.aSample[] arrays of all indices.
109140*/
109141static int loadStat4(sqlite3 *db, const char *zDb){
109142  int rc = SQLITE_OK;             /* Result codes from subroutines */
109143
109144  assert( db->lookaside.bDisable );
109145  if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
109146    rc = loadStatTbl(db,
109147      "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
109148      "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
109149      zDb
109150    );
109151  }
109152  return rc;
109153}
109154#endif /* SQLITE_ENABLE_STAT4 */
109155
109156/*
109157** Load the content of the sqlite_stat1 and sqlite_stat4 tables. The
109158** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
109159** arrays. The contents of sqlite_stat4 are used to populate the
109160** Index.aSample[] arrays.
109161**
109162** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
109163** is returned. In this case, even if SQLITE_ENABLE_STAT4 was defined
109164** during compilation and the sqlite_stat4 table is present, no data is
109165** read from it.
109166**
109167** If SQLITE_ENABLE_STAT4 was defined during compilation and the
109168** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
109169** returned. However, in this case, data is read from the sqlite_stat1
109170** table (if it is present) before returning.
109171**
109172** If an OOM error occurs, this function always sets db->mallocFailed.
109173** This means if the caller does not care about other errors, the return
109174** code may be ignored.
109175*/
109176SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
109177  analysisInfo sInfo;
109178  HashElem *i;
109179  char *zSql;
109180  int rc = SQLITE_OK;
109181  Schema *pSchema = db->aDb[iDb].pSchema;
109182
109183  assert( iDb>=0 && iDb<db->nDb );
109184  assert( db->aDb[iDb].pBt!=0 );
109185
109186  /* Clear any prior statistics */
109187  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
109188  for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
109189    Table *pTab = sqliteHashData(i);
109190    pTab->tabFlags &= ~TF_HasStat1;
109191  }
109192  for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
109193    Index *pIdx = sqliteHashData(i);
109194    pIdx->hasStat1 = 0;
109195#ifdef SQLITE_ENABLE_STAT4
109196    sqlite3DeleteIndexSamples(db, pIdx);
109197    pIdx->aSample = 0;
109198#endif
109199  }
109200
109201  /* Load new statistics out of the sqlite_stat1 table */
109202  sInfo.db = db;
109203  sInfo.zDatabase = db->aDb[iDb].zDbSName;
109204  if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)!=0 ){
109205    zSql = sqlite3MPrintf(db,
109206        "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
109207    if( zSql==0 ){
109208      rc = SQLITE_NOMEM_BKPT;
109209    }else{
109210      rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
109211      sqlite3DbFree(db, zSql);
109212    }
109213  }
109214
109215  /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */
109216  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
109217  for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
109218    Index *pIdx = sqliteHashData(i);
109219    if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
109220  }
109221
109222  /* Load the statistics from the sqlite_stat4 table. */
109223#ifdef SQLITE_ENABLE_STAT4
109224  if( rc==SQLITE_OK ){
109225    DisableLookaside;
109226    rc = loadStat4(db, sInfo.zDatabase);
109227    EnableLookaside;
109228  }
109229  for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
109230    Index *pIdx = sqliteHashData(i);
109231    sqlite3_free(pIdx->aiRowEst);
109232    pIdx->aiRowEst = 0;
109233  }
109234#endif
109235
109236  if( rc==SQLITE_NOMEM ){
109237    sqlite3OomFault(db);
109238  }
109239  return rc;
109240}
109241
109242
109243#endif /* SQLITE_OMIT_ANALYZE */
109244
109245/************** End of analyze.c *********************************************/
109246/************** Begin file attach.c ******************************************/
109247/*
109248** 2003 April 6
109249**
109250** The author disclaims copyright to this source code.  In place of
109251** a legal notice, here is a blessing:
109252**
109253**    May you do good and not evil.
109254**    May you find forgiveness for yourself and forgive others.
109255**    May you share freely, never taking more than you give.
109256**
109257*************************************************************************
109258** This file contains code used to implement the ATTACH and DETACH commands.
109259*/
109260/* #include "sqliteInt.h" */
109261
109262#ifndef SQLITE_OMIT_ATTACH
109263/*
109264** Resolve an expression that was part of an ATTACH or DETACH statement. This
109265** is slightly different from resolving a normal SQL expression, because simple
109266** identifiers are treated as strings, not possible column names or aliases.
109267**
109268** i.e. if the parser sees:
109269**
109270**     ATTACH DATABASE abc AS def
109271**
109272** it treats the two expressions as literal strings 'abc' and 'def' instead of
109273** looking for columns of the same name.
109274**
109275** This only applies to the root node of pExpr, so the statement:
109276**
109277**     ATTACH DATABASE abc||def AS 'db2'
109278**
109279** will fail because neither abc or def can be resolved.
109280*/
109281static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
109282{
109283  int rc = SQLITE_OK;
109284  if( pExpr ){
109285    if( pExpr->op!=TK_ID ){
109286      rc = sqlite3ResolveExprNames(pName, pExpr);
109287    }else{
109288      pExpr->op = TK_STRING;
109289    }
109290  }
109291  return rc;
109292}
109293
109294/*
109295** Return true if zName points to a name that may be used to refer to
109296** database iDb attached to handle db.
109297*/
109298SQLITE_PRIVATE int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName){
109299  return (
109300      sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
109301   || (iDb==0 && sqlite3StrICmp("main", zName)==0)
109302  );
109303}
109304
109305/*
109306** An SQL user-function registered to do the work of an ATTACH statement. The
109307** three arguments to the function come directly from an attach statement:
109308**
109309**     ATTACH DATABASE x AS y KEY z
109310**
109311**     SELECT sqlite_attach(x, y, z)
109312**
109313** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
109314** third argument.
109315**
109316** If the db->init.reopenMemdb flags is set, then instead of attaching a
109317** new database, close the database on db->init.iDb and reopen it as an
109318** empty MemDB.
109319*/
109320static void attachFunc(
109321  sqlite3_context *context,
109322  int NotUsed,
109323  sqlite3_value **argv
109324){
109325  int i;
109326  int rc = 0;
109327  sqlite3 *db = sqlite3_context_db_handle(context);
109328  const char *zName;
109329  const char *zFile;
109330  char *zPath = 0;
109331  char *zErr = 0;
109332  unsigned int flags;
109333  Db *aNew;                 /* New array of Db pointers */
109334  Db *pNew;                 /* Db object for the newly attached database */
109335  char *zErrDyn = 0;
109336  sqlite3_vfs *pVfs;
109337
109338  UNUSED_PARAMETER(NotUsed);
109339  zFile = (const char *)sqlite3_value_text(argv[0]);
109340  zName = (const char *)sqlite3_value_text(argv[1]);
109341  if( zFile==0 ) zFile = "";
109342  if( zName==0 ) zName = "";
109343
109344#ifdef SQLITE_ENABLE_DESERIALIZE
109345# define REOPEN_AS_MEMDB(db)  (db->init.reopenMemdb)
109346#else
109347# define REOPEN_AS_MEMDB(db)  (0)
109348#endif
109349
109350  if( REOPEN_AS_MEMDB(db) ){
109351    /* This is not a real ATTACH.  Instead, this routine is being called
109352    ** from sqlite3_deserialize() to close database db->init.iDb and
109353    ** reopen it as a MemDB */
109354    pVfs = sqlite3_vfs_find("memdb");
109355    if( pVfs==0 ) return;
109356    pNew = &db->aDb[db->init.iDb];
109357    if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
109358    pNew->pBt = 0;
109359    pNew->pSchema = 0;
109360    rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
109361  }else{
109362    /* This is a real ATTACH
109363    **
109364    ** Check for the following errors:
109365    **
109366    **     * Too many attached databases,
109367    **     * Transaction currently open
109368    **     * Specified database name already being used.
109369    */
109370    if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
109371      zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
109372        db->aLimit[SQLITE_LIMIT_ATTACHED]
109373      );
109374      goto attach_error;
109375    }
109376    for(i=0; i<db->nDb; i++){
109377      assert( zName );
109378      if( sqlite3DbIsNamed(db, i, zName) ){
109379        zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
109380        goto attach_error;
109381      }
109382    }
109383
109384    /* Allocate the new entry in the db->aDb[] array and initialize the schema
109385    ** hash tables.
109386    */
109387    if( db->aDb==db->aDbStatic ){
109388      aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
109389      if( aNew==0 ) return;
109390      memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
109391    }else{
109392      aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
109393      if( aNew==0 ) return;
109394    }
109395    db->aDb = aNew;
109396    pNew = &db->aDb[db->nDb];
109397    memset(pNew, 0, sizeof(*pNew));
109398
109399    /* Open the database file. If the btree is successfully opened, use
109400    ** it to obtain the database schema. At this point the schema may
109401    ** or may not be initialized.
109402    */
109403    flags = db->openFlags;
109404    rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
109405    if( rc!=SQLITE_OK ){
109406      if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
109407      sqlite3_result_error(context, zErr, -1);
109408      sqlite3_free(zErr);
109409      return;
109410    }
109411    assert( pVfs );
109412    flags |= SQLITE_OPEN_MAIN_DB;
109413    rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
109414    db->nDb++;
109415    pNew->zDbSName = sqlite3DbStrDup(db, zName);
109416  }
109417  db->noSharedCache = 0;
109418  if( rc==SQLITE_CONSTRAINT ){
109419    rc = SQLITE_ERROR;
109420    zErrDyn = sqlite3MPrintf(db, "database is already attached");
109421  }else if( rc==SQLITE_OK ){
109422    Pager *pPager;
109423    pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
109424    if( !pNew->pSchema ){
109425      rc = SQLITE_NOMEM_BKPT;
109426    }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
109427      zErrDyn = sqlite3MPrintf(db,
109428        "attached databases must use the same text encoding as main database");
109429      rc = SQLITE_ERROR;
109430    }
109431    sqlite3BtreeEnter(pNew->pBt);
109432    pPager = sqlite3BtreePager(pNew->pBt);
109433    sqlite3PagerLockingMode(pPager, db->dfltLockMode);
109434    sqlite3BtreeSecureDelete(pNew->pBt,
109435                             sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
109436#ifndef SQLITE_OMIT_PAGER_PRAGMAS
109437    sqlite3BtreeSetPagerFlags(pNew->pBt,
109438                      PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
109439#endif
109440    sqlite3BtreeLeave(pNew->pBt);
109441  }
109442  pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
109443  if( rc==SQLITE_OK && pNew->zDbSName==0 ){
109444    rc = SQLITE_NOMEM_BKPT;
109445  }
109446  sqlite3_free_filename( zPath );
109447
109448  /* If the file was opened successfully, read the schema for the new database.
109449  ** If this fails, or if opening the file failed, then close the file and
109450  ** remove the entry from the db->aDb[] array. i.e. put everything back the
109451  ** way we found it.
109452  */
109453  if( rc==SQLITE_OK ){
109454    sqlite3BtreeEnterAll(db);
109455    db->init.iDb = 0;
109456    db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
109457    if( !REOPEN_AS_MEMDB(db) ){
109458      rc = sqlite3Init(db, &zErrDyn);
109459    }
109460    sqlite3BtreeLeaveAll(db);
109461    assert( zErrDyn==0 || rc!=SQLITE_OK );
109462  }
109463#ifdef SQLITE_USER_AUTHENTICATION
109464  if( rc==SQLITE_OK && !REOPEN_AS_MEMDB(db) ){
109465    u8 newAuth = 0;
109466    rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
109467    if( newAuth<db->auth.authLevel ){
109468      rc = SQLITE_AUTH_USER;
109469    }
109470  }
109471#endif
109472  if( rc ){
109473    if( !REOPEN_AS_MEMDB(db) ){
109474      int iDb = db->nDb - 1;
109475      assert( iDb>=2 );
109476      if( db->aDb[iDb].pBt ){
109477        sqlite3BtreeClose(db->aDb[iDb].pBt);
109478        db->aDb[iDb].pBt = 0;
109479        db->aDb[iDb].pSchema = 0;
109480      }
109481      sqlite3ResetAllSchemasOfConnection(db);
109482      db->nDb = iDb;
109483      if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
109484        sqlite3OomFault(db);
109485        sqlite3DbFree(db, zErrDyn);
109486        zErrDyn = sqlite3MPrintf(db, "out of memory");
109487      }else if( zErrDyn==0 ){
109488        zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
109489      }
109490    }
109491    goto attach_error;
109492  }
109493
109494  return;
109495
109496attach_error:
109497  /* Return an error if we get here */
109498  if( zErrDyn ){
109499    sqlite3_result_error(context, zErrDyn, -1);
109500    sqlite3DbFree(db, zErrDyn);
109501  }
109502  if( rc ) sqlite3_result_error_code(context, rc);
109503}
109504
109505/*
109506** An SQL user-function registered to do the work of an DETACH statement. The
109507** three arguments to the function come directly from a detach statement:
109508**
109509**     DETACH DATABASE x
109510**
109511**     SELECT sqlite_detach(x)
109512*/
109513static void detachFunc(
109514  sqlite3_context *context,
109515  int NotUsed,
109516  sqlite3_value **argv
109517){
109518  const char *zName = (const char *)sqlite3_value_text(argv[0]);
109519  sqlite3 *db = sqlite3_context_db_handle(context);
109520  int i;
109521  Db *pDb = 0;
109522  HashElem *pEntry;
109523  char zErr[128];
109524
109525  UNUSED_PARAMETER(NotUsed);
109526
109527  if( zName==0 ) zName = "";
109528  for(i=0; i<db->nDb; i++){
109529    pDb = &db->aDb[i];
109530    if( pDb->pBt==0 ) continue;
109531    if( sqlite3DbIsNamed(db, i, zName) ) break;
109532  }
109533
109534  if( i>=db->nDb ){
109535    sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
109536    goto detach_error;
109537  }
109538  if( i<2 ){
109539    sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
109540    goto detach_error;
109541  }
109542  if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
109543    sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
109544    goto detach_error;
109545  }
109546
109547  /* If any TEMP triggers reference the schema being detached, move those
109548  ** triggers to reference the TEMP schema itself. */
109549  assert( db->aDb[1].pSchema );
109550  pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash);
109551  while( pEntry ){
109552    Trigger *pTrig = (Trigger*)sqliteHashData(pEntry);
109553    if( pTrig->pTabSchema==pDb->pSchema ){
109554      pTrig->pTabSchema = pTrig->pSchema;
109555    }
109556    pEntry = sqliteHashNext(pEntry);
109557  }
109558
109559  sqlite3BtreeClose(pDb->pBt);
109560  pDb->pBt = 0;
109561  pDb->pSchema = 0;
109562  sqlite3CollapseDatabaseArray(db);
109563  return;
109564
109565detach_error:
109566  sqlite3_result_error(context, zErr, -1);
109567}
109568
109569/*
109570** This procedure generates VDBE code for a single invocation of either the
109571** sqlite_detach() or sqlite_attach() SQL user functions.
109572*/
109573static void codeAttach(
109574  Parse *pParse,       /* The parser context */
109575  int type,            /* Either SQLITE_ATTACH or SQLITE_DETACH */
109576  FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
109577  Expr *pAuthArg,      /* Expression to pass to authorization callback */
109578  Expr *pFilename,     /* Name of database file */
109579  Expr *pDbname,       /* Name of the database to use internally */
109580  Expr *pKey           /* Database key for encryption extension */
109581){
109582  int rc;
109583  NameContext sName;
109584  Vdbe *v;
109585  sqlite3* db = pParse->db;
109586  int regArgs;
109587
109588  if( pParse->nErr ) goto attach_end;
109589  memset(&sName, 0, sizeof(NameContext));
109590  sName.pParse = pParse;
109591
109592  if(
109593      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
109594      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
109595      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
109596  ){
109597    goto attach_end;
109598  }
109599
109600#ifndef SQLITE_OMIT_AUTHORIZATION
109601  if( pAuthArg ){
109602    char *zAuthArg;
109603    if( pAuthArg->op==TK_STRING ){
109604      zAuthArg = pAuthArg->u.zToken;
109605    }else{
109606      zAuthArg = 0;
109607    }
109608    rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
109609    if(rc!=SQLITE_OK ){
109610      goto attach_end;
109611    }
109612  }
109613#endif /* SQLITE_OMIT_AUTHORIZATION */
109614
109615
109616  v = sqlite3GetVdbe(pParse);
109617  regArgs = sqlite3GetTempRange(pParse, 4);
109618  sqlite3ExprCode(pParse, pFilename, regArgs);
109619  sqlite3ExprCode(pParse, pDbname, regArgs+1);
109620  sqlite3ExprCode(pParse, pKey, regArgs+2);
109621
109622  assert( v || db->mallocFailed );
109623  if( v ){
109624    sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3,
109625                               pFunc->nArg, pFunc, 0);
109626    /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
109627    ** statement only). For DETACH, set it to false (expire all existing
109628    ** statements).
109629    */
109630    sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
109631  }
109632
109633attach_end:
109634  sqlite3ExprDelete(db, pFilename);
109635  sqlite3ExprDelete(db, pDbname);
109636  sqlite3ExprDelete(db, pKey);
109637}
109638
109639/*
109640** Called by the parser to compile a DETACH statement.
109641**
109642**     DETACH pDbname
109643*/
109644SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
109645  static const FuncDef detach_func = {
109646    1,                /* nArg */
109647    SQLITE_UTF8,      /* funcFlags */
109648    0,                /* pUserData */
109649    0,                /* pNext */
109650    detachFunc,       /* xSFunc */
109651    0,                /* xFinalize */
109652    0, 0,             /* xValue, xInverse */
109653    "sqlite_detach",  /* zName */
109654    {0}
109655  };
109656  codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
109657}
109658
109659/*
109660** Called by the parser to compile an ATTACH statement.
109661**
109662**     ATTACH p AS pDbname KEY pKey
109663*/
109664SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
109665  static const FuncDef attach_func = {
109666    3,                /* nArg */
109667    SQLITE_UTF8,      /* funcFlags */
109668    0,                /* pUserData */
109669    0,                /* pNext */
109670    attachFunc,       /* xSFunc */
109671    0,                /* xFinalize */
109672    0, 0,             /* xValue, xInverse */
109673    "sqlite_attach",  /* zName */
109674    {0}
109675  };
109676  codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
109677}
109678#endif /* SQLITE_OMIT_ATTACH */
109679
109680/*
109681** Initialize a DbFixer structure.  This routine must be called prior
109682** to passing the structure to one of the sqliteFixAAAA() routines below.
109683*/
109684SQLITE_PRIVATE void sqlite3FixInit(
109685  DbFixer *pFix,      /* The fixer to be initialized */
109686  Parse *pParse,      /* Error messages will be written here */
109687  int iDb,            /* This is the database that must be used */
109688  const char *zType,  /* "view", "trigger", or "index" */
109689  const Token *pName  /* Name of the view, trigger, or index */
109690){
109691  sqlite3 *db;
109692
109693  db = pParse->db;
109694  assert( db->nDb>iDb );
109695  pFix->pParse = pParse;
109696  pFix->zDb = db->aDb[iDb].zDbSName;
109697  pFix->pSchema = db->aDb[iDb].pSchema;
109698  pFix->zType = zType;
109699  pFix->pName = pName;
109700  pFix->bTemp = (iDb==1);
109701}
109702
109703/*
109704** The following set of routines walk through the parse tree and assign
109705** a specific database to all table references where the database name
109706** was left unspecified in the original SQL statement.  The pFix structure
109707** must have been initialized by a prior call to sqlite3FixInit().
109708**
109709** These routines are used to make sure that an index, trigger, or
109710** view in one database does not refer to objects in a different database.
109711** (Exception: indices, triggers, and views in the TEMP database are
109712** allowed to refer to anything.)  If a reference is explicitly made
109713** to an object in a different database, an error message is added to
109714** pParse->zErrMsg and these routines return non-zero.  If everything
109715** checks out, these routines return 0.
109716*/
109717SQLITE_PRIVATE int sqlite3FixSrcList(
109718  DbFixer *pFix,       /* Context of the fixation */
109719  SrcList *pList       /* The Source list to check and modify */
109720){
109721  int i;
109722  struct SrcList_item *pItem;
109723  sqlite3 *db = pFix->pParse->db;
109724  int iDb = sqlite3FindDbName(db, pFix->zDb);
109725
109726  if( NEVER(pList==0) ) return 0;
109727
109728  for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
109729    if( pFix->bTemp==0 ){
109730      if( pItem->zDatabase && iDb!=sqlite3FindDbName(db, pItem->zDatabase) ){
109731        sqlite3ErrorMsg(pFix->pParse,
109732            "%s %T cannot reference objects in database %s",
109733            pFix->zType, pFix->pName, pItem->zDatabase);
109734        return 1;
109735      }
109736      sqlite3DbFree(db, pItem->zDatabase);
109737      pItem->zDatabase = 0;
109738      pItem->pSchema = pFix->pSchema;
109739      pItem->fg.fromDDL = 1;
109740    }
109741#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
109742    if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
109743    if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
109744#endif
109745    if( pItem->fg.isTabFunc && sqlite3FixExprList(pFix, pItem->u1.pFuncArg) ){
109746      return 1;
109747    }
109748  }
109749  return 0;
109750}
109751#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
109752SQLITE_PRIVATE int sqlite3FixSelect(
109753  DbFixer *pFix,       /* Context of the fixation */
109754  Select *pSelect      /* The SELECT statement to be fixed to one database */
109755){
109756  while( pSelect ){
109757    if( sqlite3FixExprList(pFix, pSelect->pEList) ){
109758      return 1;
109759    }
109760    if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
109761      return 1;
109762    }
109763    if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
109764      return 1;
109765    }
109766    if( sqlite3FixExprList(pFix, pSelect->pGroupBy) ){
109767      return 1;
109768    }
109769    if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
109770      return 1;
109771    }
109772    if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
109773      return 1;
109774    }
109775    if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
109776      return 1;
109777    }
109778    if( pSelect->pWith ){
109779      int i;
109780      for(i=0; i<pSelect->pWith->nCte; i++){
109781        if( sqlite3FixSelect(pFix, pSelect->pWith->a[i].pSelect) ){
109782          return 1;
109783        }
109784      }
109785    }
109786    pSelect = pSelect->pPrior;
109787  }
109788  return 0;
109789}
109790SQLITE_PRIVATE int sqlite3FixExpr(
109791  DbFixer *pFix,     /* Context of the fixation */
109792  Expr *pExpr        /* The expression to be fixed to one database */
109793){
109794  while( pExpr ){
109795    if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL);
109796    if( pExpr->op==TK_VARIABLE ){
109797      if( pFix->pParse->db->init.busy ){
109798        pExpr->op = TK_NULL;
109799      }else{
109800        sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
109801        return 1;
109802      }
109803    }
109804    if( ExprHasProperty(pExpr, EP_TokenOnly|EP_Leaf) ) break;
109805    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
109806      if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
109807    }else{
109808      if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
109809    }
109810    if( sqlite3FixExpr(pFix, pExpr->pRight) ){
109811      return 1;
109812    }
109813    pExpr = pExpr->pLeft;
109814  }
109815  return 0;
109816}
109817SQLITE_PRIVATE int sqlite3FixExprList(
109818  DbFixer *pFix,     /* Context of the fixation */
109819  ExprList *pList    /* The expression to be fixed to one database */
109820){
109821  int i;
109822  struct ExprList_item *pItem;
109823  if( pList==0 ) return 0;
109824  for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
109825    if( sqlite3FixExpr(pFix, pItem->pExpr) ){
109826      return 1;
109827    }
109828  }
109829  return 0;
109830}
109831#endif
109832
109833#ifndef SQLITE_OMIT_TRIGGER
109834SQLITE_PRIVATE int sqlite3FixTriggerStep(
109835  DbFixer *pFix,     /* Context of the fixation */
109836  TriggerStep *pStep /* The trigger step be fixed to one database */
109837){
109838  while( pStep ){
109839    if( sqlite3FixSelect(pFix, pStep->pSelect) ){
109840      return 1;
109841    }
109842    if( sqlite3FixExpr(pFix, pStep->pWhere) ){
109843      return 1;
109844    }
109845    if( sqlite3FixExprList(pFix, pStep->pExprList) ){
109846      return 1;
109847    }
109848#ifndef SQLITE_OMIT_UPSERT
109849    if( pStep->pUpsert ){
109850      Upsert *pUp = pStep->pUpsert;
109851      if( sqlite3FixExprList(pFix, pUp->pUpsertTarget)
109852       || sqlite3FixExpr(pFix, pUp->pUpsertTargetWhere)
109853       || sqlite3FixExprList(pFix, pUp->pUpsertSet)
109854       || sqlite3FixExpr(pFix, pUp->pUpsertWhere)
109855      ){
109856        return 1;
109857      }
109858    }
109859#endif
109860    pStep = pStep->pNext;
109861  }
109862  return 0;
109863}
109864#endif
109865
109866/************** End of attach.c **********************************************/
109867/************** Begin file auth.c ********************************************/
109868/*
109869** 2003 January 11
109870**
109871** The author disclaims copyright to this source code.  In place of
109872** a legal notice, here is a blessing:
109873**
109874**    May you do good and not evil.
109875**    May you find forgiveness for yourself and forgive others.
109876**    May you share freely, never taking more than you give.
109877**
109878*************************************************************************
109879** This file contains code used to implement the sqlite3_set_authorizer()
109880** API.  This facility is an optional feature of the library.  Embedded
109881** systems that do not need this facility may omit it by recompiling
109882** the library with -DSQLITE_OMIT_AUTHORIZATION=1
109883*/
109884/* #include "sqliteInt.h" */
109885
109886/*
109887** All of the code in this file may be omitted by defining a single
109888** macro.
109889*/
109890#ifndef SQLITE_OMIT_AUTHORIZATION
109891
109892/*
109893** Set or clear the access authorization function.
109894**
109895** The access authorization function is be called during the compilation
109896** phase to verify that the user has read and/or write access permission on
109897** various fields of the database.  The first argument to the auth function
109898** is a copy of the 3rd argument to this routine.  The second argument
109899** to the auth function is one of these constants:
109900**
109901**       SQLITE_CREATE_INDEX
109902**       SQLITE_CREATE_TABLE
109903**       SQLITE_CREATE_TEMP_INDEX
109904**       SQLITE_CREATE_TEMP_TABLE
109905**       SQLITE_CREATE_TEMP_TRIGGER
109906**       SQLITE_CREATE_TEMP_VIEW
109907**       SQLITE_CREATE_TRIGGER
109908**       SQLITE_CREATE_VIEW
109909**       SQLITE_DELETE
109910**       SQLITE_DROP_INDEX
109911**       SQLITE_DROP_TABLE
109912**       SQLITE_DROP_TEMP_INDEX
109913**       SQLITE_DROP_TEMP_TABLE
109914**       SQLITE_DROP_TEMP_TRIGGER
109915**       SQLITE_DROP_TEMP_VIEW
109916**       SQLITE_DROP_TRIGGER
109917**       SQLITE_DROP_VIEW
109918**       SQLITE_INSERT
109919**       SQLITE_PRAGMA
109920**       SQLITE_READ
109921**       SQLITE_SELECT
109922**       SQLITE_TRANSACTION
109923**       SQLITE_UPDATE
109924**
109925** The third and fourth arguments to the auth function are the name of
109926** the table and the column that are being accessed.  The auth function
109927** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE.  If
109928** SQLITE_OK is returned, it means that access is allowed.  SQLITE_DENY
109929** means that the SQL statement will never-run - the sqlite3_exec() call
109930** will return with an error.  SQLITE_IGNORE means that the SQL statement
109931** should run but attempts to read the specified column will return NULL
109932** and attempts to write the column will be ignored.
109933**
109934** Setting the auth function to NULL disables this hook.  The default
109935** setting of the auth function is NULL.
109936*/
109937SQLITE_API int sqlite3_set_authorizer(
109938  sqlite3 *db,
109939  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
109940  void *pArg
109941){
109942#ifdef SQLITE_ENABLE_API_ARMOR
109943  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
109944#endif
109945  sqlite3_mutex_enter(db->mutex);
109946  db->xAuth = (sqlite3_xauth)xAuth;
109947  db->pAuthArg = pArg;
109948  if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
109949  sqlite3_mutex_leave(db->mutex);
109950  return SQLITE_OK;
109951}
109952
109953/*
109954** Write an error message into pParse->zErrMsg that explains that the
109955** user-supplied authorization function returned an illegal value.
109956*/
109957static void sqliteAuthBadReturnCode(Parse *pParse){
109958  sqlite3ErrorMsg(pParse, "authorizer malfunction");
109959  pParse->rc = SQLITE_ERROR;
109960}
109961
109962/*
109963** Invoke the authorization callback for permission to read column zCol from
109964** table zTab in database zDb. This function assumes that an authorization
109965** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
109966**
109967** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
109968** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
109969** is treated as SQLITE_DENY. In this case an error is left in pParse.
109970*/
109971SQLITE_PRIVATE int sqlite3AuthReadCol(
109972  Parse *pParse,                  /* The parser context */
109973  const char *zTab,               /* Table name */
109974  const char *zCol,               /* Column name */
109975  int iDb                         /* Index of containing database. */
109976){
109977  sqlite3 *db = pParse->db;          /* Database handle */
109978  char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
109979  int rc;                            /* Auth callback return code */
109980
109981  if( db->init.busy ) return SQLITE_OK;
109982  rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
109983#ifdef SQLITE_USER_AUTHENTICATION
109984                 ,db->auth.zAuthUser
109985#endif
109986                );
109987  if( rc==SQLITE_DENY ){
109988    char *z = sqlite3_mprintf("%s.%s", zTab, zCol);
109989    if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
109990    sqlite3ErrorMsg(pParse, "access to %z is prohibited", z);
109991    pParse->rc = SQLITE_AUTH;
109992  }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
109993    sqliteAuthBadReturnCode(pParse);
109994  }
109995  return rc;
109996}
109997
109998/*
109999** The pExpr should be a TK_COLUMN expression.  The table referred to
110000** is in pTabList or else it is the NEW or OLD table of a trigger.
110001** Check to see if it is OK to read this particular column.
110002**
110003** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
110004** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
110005** then generate an error.
110006*/
110007SQLITE_PRIVATE void sqlite3AuthRead(
110008  Parse *pParse,        /* The parser context */
110009  Expr *pExpr,          /* The expression to check authorization on */
110010  Schema *pSchema,      /* The schema of the expression */
110011  SrcList *pTabList     /* All table that pExpr might refer to */
110012){
110013  sqlite3 *db = pParse->db;
110014  Table *pTab = 0;      /* The table being read */
110015  const char *zCol;     /* Name of the column of the table */
110016  int iSrc;             /* Index in pTabList->a[] of table being read */
110017  int iDb;              /* The index of the database the expression refers to */
110018  int iCol;             /* Index of column in table */
110019
110020  assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
110021  assert( !IN_RENAME_OBJECT || db->xAuth==0 );
110022  if( db->xAuth==0 ) return;
110023  iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
110024  if( iDb<0 ){
110025    /* An attempt to read a column out of a subquery or other
110026    ** temporary table. */
110027    return;
110028  }
110029
110030  if( pExpr->op==TK_TRIGGER ){
110031    pTab = pParse->pTriggerTab;
110032  }else{
110033    assert( pTabList );
110034    for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
110035      if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
110036        pTab = pTabList->a[iSrc].pTab;
110037        break;
110038      }
110039    }
110040  }
110041  iCol = pExpr->iColumn;
110042  if( NEVER(pTab==0) ) return;
110043
110044  if( iCol>=0 ){
110045    assert( iCol<pTab->nCol );
110046    zCol = pTab->aCol[iCol].zName;
110047  }else if( pTab->iPKey>=0 ){
110048    assert( pTab->iPKey<pTab->nCol );
110049    zCol = pTab->aCol[pTab->iPKey].zName;
110050  }else{
110051    zCol = "ROWID";
110052  }
110053  assert( iDb>=0 && iDb<db->nDb );
110054  if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
110055    pExpr->op = TK_NULL;
110056  }
110057}
110058
110059/*
110060** Do an authorization check using the code and arguments given.  Return
110061** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
110062** is returned, then the error count and error message in pParse are
110063** modified appropriately.
110064*/
110065SQLITE_PRIVATE int sqlite3AuthCheck(
110066  Parse *pParse,
110067  int code,
110068  const char *zArg1,
110069  const char *zArg2,
110070  const char *zArg3
110071){
110072  sqlite3 *db = pParse->db;
110073  int rc;
110074
110075  /* Don't do any authorization checks if the database is initialising
110076  ** or if the parser is being invoked from within sqlite3_declare_vtab.
110077  */
110078  assert( !IN_RENAME_OBJECT || db->xAuth==0 );
110079  if( db->init.busy || IN_SPECIAL_PARSE ){
110080    return SQLITE_OK;
110081  }
110082
110083  if( db->xAuth==0 ){
110084    return SQLITE_OK;
110085  }
110086
110087  /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
110088  ** callback are either NULL pointers or zero-terminated strings that
110089  ** contain additional details about the action to be authorized.
110090  **
110091  ** The following testcase() macros show that any of the 3rd through 6th
110092  ** parameters can be either NULL or a string. */
110093  testcase( zArg1==0 );
110094  testcase( zArg2==0 );
110095  testcase( zArg3==0 );
110096  testcase( pParse->zAuthContext==0 );
110097
110098  rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
110099#ifdef SQLITE_USER_AUTHENTICATION
110100                 ,db->auth.zAuthUser
110101#endif
110102                );
110103  if( rc==SQLITE_DENY ){
110104    sqlite3ErrorMsg(pParse, "not authorized");
110105    pParse->rc = SQLITE_AUTH;
110106  }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
110107    rc = SQLITE_DENY;
110108    sqliteAuthBadReturnCode(pParse);
110109  }
110110  return rc;
110111}
110112
110113/*
110114** Push an authorization context.  After this routine is called, the
110115** zArg3 argument to authorization callbacks will be zContext until
110116** popped.  Or if pParse==0, this routine is a no-op.
110117*/
110118SQLITE_PRIVATE void sqlite3AuthContextPush(
110119  Parse *pParse,
110120  AuthContext *pContext,
110121  const char *zContext
110122){
110123  assert( pParse );
110124  pContext->pParse = pParse;
110125  pContext->zAuthContext = pParse->zAuthContext;
110126  pParse->zAuthContext = zContext;
110127}
110128
110129/*
110130** Pop an authorization context that was previously pushed
110131** by sqlite3AuthContextPush
110132*/
110133SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
110134  if( pContext->pParse ){
110135    pContext->pParse->zAuthContext = pContext->zAuthContext;
110136    pContext->pParse = 0;
110137  }
110138}
110139
110140#endif /* SQLITE_OMIT_AUTHORIZATION */
110141
110142/************** End of auth.c ************************************************/
110143/************** Begin file build.c *******************************************/
110144/*
110145** 2001 September 15
110146**
110147** The author disclaims copyright to this source code.  In place of
110148** a legal notice, here is a blessing:
110149**
110150**    May you do good and not evil.
110151**    May you find forgiveness for yourself and forgive others.
110152**    May you share freely, never taking more than you give.
110153**
110154*************************************************************************
110155** This file contains C code routines that are called by the SQLite parser
110156** when syntax rules are reduced.  The routines in this file handle the
110157** following kinds of SQL syntax:
110158**
110159**     CREATE TABLE
110160**     DROP TABLE
110161**     CREATE INDEX
110162**     DROP INDEX
110163**     creating ID lists
110164**     BEGIN TRANSACTION
110165**     COMMIT
110166**     ROLLBACK
110167*/
110168/* #include "sqliteInt.h" */
110169
110170#ifndef SQLITE_OMIT_SHARED_CACHE
110171/*
110172** The TableLock structure is only used by the sqlite3TableLock() and
110173** codeTableLocks() functions.
110174*/
110175struct TableLock {
110176  int iDb;               /* The database containing the table to be locked */
110177  int iTab;              /* The root page of the table to be locked */
110178  u8 isWriteLock;        /* True for write lock.  False for a read lock */
110179  const char *zLockName; /* Name of the table */
110180};
110181
110182/*
110183** Record the fact that we want to lock a table at run-time.
110184**
110185** The table to be locked has root page iTab and is found in database iDb.
110186** A read or a write lock can be taken depending on isWritelock.
110187**
110188** This routine just records the fact that the lock is desired.  The
110189** code to make the lock occur is generated by a later call to
110190** codeTableLocks() which occurs during sqlite3FinishCoding().
110191*/
110192SQLITE_PRIVATE void sqlite3TableLock(
110193  Parse *pParse,     /* Parsing context */
110194  int iDb,           /* Index of the database containing the table to lock */
110195  int iTab,          /* Root page number of the table to be locked */
110196  u8 isWriteLock,    /* True for a write lock */
110197  const char *zName  /* Name of the table to be locked */
110198){
110199  Parse *pToplevel = sqlite3ParseToplevel(pParse);
110200  int i;
110201  int nBytes;
110202  TableLock *p;
110203  assert( iDb>=0 );
110204
110205  if( iDb==1 ) return;
110206  if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
110207  for(i=0; i<pToplevel->nTableLock; i++){
110208    p = &pToplevel->aTableLock[i];
110209    if( p->iDb==iDb && p->iTab==iTab ){
110210      p->isWriteLock = (p->isWriteLock || isWriteLock);
110211      return;
110212    }
110213  }
110214
110215  nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
110216  pToplevel->aTableLock =
110217      sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
110218  if( pToplevel->aTableLock ){
110219    p = &pToplevel->aTableLock[pToplevel->nTableLock++];
110220    p->iDb = iDb;
110221    p->iTab = iTab;
110222    p->isWriteLock = isWriteLock;
110223    p->zLockName = zName;
110224  }else{
110225    pToplevel->nTableLock = 0;
110226    sqlite3OomFault(pToplevel->db);
110227  }
110228}
110229
110230/*
110231** Code an OP_TableLock instruction for each table locked by the
110232** statement (configured by calls to sqlite3TableLock()).
110233*/
110234static void codeTableLocks(Parse *pParse){
110235  int i;
110236  Vdbe *pVdbe;
110237
110238  pVdbe = sqlite3GetVdbe(pParse);
110239  assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
110240
110241  for(i=0; i<pParse->nTableLock; i++){
110242    TableLock *p = &pParse->aTableLock[i];
110243    int p1 = p->iDb;
110244    sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
110245                      p->zLockName, P4_STATIC);
110246  }
110247}
110248#else
110249  #define codeTableLocks(x)
110250#endif
110251
110252/*
110253** Return TRUE if the given yDbMask object is empty - if it contains no
110254** 1 bits.  This routine is used by the DbMaskAllZero() and DbMaskNotZero()
110255** macros when SQLITE_MAX_ATTACHED is greater than 30.
110256*/
110257#if SQLITE_MAX_ATTACHED>30
110258SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask m){
110259  int i;
110260  for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
110261  return 1;
110262}
110263#endif
110264
110265/*
110266** This routine is called after a single SQL statement has been
110267** parsed and a VDBE program to execute that statement has been
110268** prepared.  This routine puts the finishing touches on the
110269** VDBE program and resets the pParse structure for the next
110270** parse.
110271**
110272** Note that if an error occurred, it might be the case that
110273** no VDBE code was generated.
110274*/
110275SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
110276  sqlite3 *db;
110277  Vdbe *v;
110278
110279  assert( pParse->pToplevel==0 );
110280  db = pParse->db;
110281  if( pParse->nested ) return;
110282  if( db->mallocFailed || pParse->nErr ){
110283    if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR;
110284    return;
110285  }
110286
110287  /* Begin by generating some termination code at the end of the
110288  ** vdbe program
110289  */
110290  v = sqlite3GetVdbe(pParse);
110291  assert( !pParse->isMultiWrite
110292       || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
110293  if( v ){
110294    sqlite3VdbeAddOp0(v, OP_Halt);
110295
110296#if SQLITE_USER_AUTHENTICATION
110297    if( pParse->nTableLock>0 && db->init.busy==0 ){
110298      sqlite3UserAuthInit(db);
110299      if( db->auth.authLevel<UAUTH_User ){
110300        sqlite3ErrorMsg(pParse, "user not authenticated");
110301        pParse->rc = SQLITE_AUTH_USER;
110302        return;
110303      }
110304    }
110305#endif
110306
110307    /* The cookie mask contains one bit for each database file open.
110308    ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
110309    ** set for each database that is used.  Generate code to start a
110310    ** transaction on each used database and to verify the schema cookie
110311    ** on each used database.
110312    */
110313    if( db->mallocFailed==0
110314     && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
110315    ){
110316      int iDb, i;
110317      assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
110318      sqlite3VdbeJumpHere(v, 0);
110319      for(iDb=0; iDb<db->nDb; iDb++){
110320        Schema *pSchema;
110321        if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
110322        sqlite3VdbeUsesBtree(v, iDb);
110323        pSchema = db->aDb[iDb].pSchema;
110324        sqlite3VdbeAddOp4Int(v,
110325          OP_Transaction,                    /* Opcode */
110326          iDb,                               /* P1 */
110327          DbMaskTest(pParse->writeMask,iDb), /* P2 */
110328          pSchema->schema_cookie,            /* P3 */
110329          pSchema->iGeneration               /* P4 */
110330        );
110331        if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
110332        VdbeComment((v,
110333              "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
110334      }
110335#ifndef SQLITE_OMIT_VIRTUALTABLE
110336      for(i=0; i<pParse->nVtabLock; i++){
110337        char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
110338        sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
110339      }
110340      pParse->nVtabLock = 0;
110341#endif
110342
110343      /* Once all the cookies have been verified and transactions opened,
110344      ** obtain the required table-locks. This is a no-op unless the
110345      ** shared-cache feature is enabled.
110346      */
110347      codeTableLocks(pParse);
110348
110349      /* Initialize any AUTOINCREMENT data structures required.
110350      */
110351      sqlite3AutoincrementBegin(pParse);
110352
110353      /* Code constant expressions that where factored out of inner loops.
110354      **
110355      ** The pConstExpr list might also contain expressions that we simply
110356      ** want to keep around until the Parse object is deleted.  Such
110357      ** expressions have iConstExprReg==0.  Do not generate code for
110358      ** those expressions, of course.
110359      */
110360      if( pParse->pConstExpr ){
110361        ExprList *pEL = pParse->pConstExpr;
110362        pParse->okConstFactor = 0;
110363        for(i=0; i<pEL->nExpr; i++){
110364          int iReg = pEL->a[i].u.iConstExprReg;
110365          if( iReg>0 ){
110366            sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg);
110367          }
110368        }
110369      }
110370
110371      /* Finally, jump back to the beginning of the executable code. */
110372      sqlite3VdbeGoto(v, 1);
110373    }
110374  }
110375
110376
110377  /* Get the VDBE program ready for execution
110378  */
110379  if( v && pParse->nErr==0 && !db->mallocFailed ){
110380    /* A minimum of one cursor is required if autoincrement is used
110381    *  See ticket [a696379c1f08866] */
110382    assert( pParse->pAinc==0 || pParse->nTab>0 );
110383    sqlite3VdbeMakeReady(v, pParse);
110384    pParse->rc = SQLITE_DONE;
110385  }else{
110386    pParse->rc = SQLITE_ERROR;
110387  }
110388}
110389
110390/*
110391** Run the parser and code generator recursively in order to generate
110392** code for the SQL statement given onto the end of the pParse context
110393** currently under construction.  When the parser is run recursively
110394** this way, the final OP_Halt is not appended and other initialization
110395** and finalization steps are omitted because those are handling by the
110396** outermost parser.
110397**
110398** Not everything is nestable.  This facility is designed to permit
110399** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER.  Use
110400** care if you decide to try to use this routine for some other purposes.
110401*/
110402SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
110403  va_list ap;
110404  char *zSql;
110405  char *zErrMsg = 0;
110406  sqlite3 *db = pParse->db;
110407  char saveBuf[PARSE_TAIL_SZ];
110408
110409  if( pParse->nErr ) return;
110410  assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
110411  va_start(ap, zFormat);
110412  zSql = sqlite3VMPrintf(db, zFormat, ap);
110413  va_end(ap);
110414  if( zSql==0 ){
110415    /* This can result either from an OOM or because the formatted string
110416    ** exceeds SQLITE_LIMIT_LENGTH.  In the latter case, we need to set
110417    ** an error */
110418    if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
110419    pParse->nErr++;
110420    return;
110421  }
110422  pParse->nested++;
110423  memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
110424  memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
110425  sqlite3RunParser(pParse, zSql, &zErrMsg);
110426  sqlite3DbFree(db, zErrMsg);
110427  sqlite3DbFree(db, zSql);
110428  memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ);
110429  pParse->nested--;
110430}
110431
110432#if SQLITE_USER_AUTHENTICATION
110433/*
110434** Return TRUE if zTable is the name of the system table that stores the
110435** list of users and their access credentials.
110436*/
110437SQLITE_PRIVATE int sqlite3UserAuthTable(const char *zTable){
110438  return sqlite3_stricmp(zTable, "sqlite_user")==0;
110439}
110440#endif
110441
110442/*
110443** Locate the in-memory structure that describes a particular database
110444** table given the name of that table and (optionally) the name of the
110445** database containing the table.  Return NULL if not found.
110446**
110447** If zDatabase is 0, all databases are searched for the table and the
110448** first matching table is returned.  (No checking for duplicate table
110449** names is done.)  The search order is TEMP first, then MAIN, then any
110450** auxiliary databases added using the ATTACH command.
110451**
110452** See also sqlite3LocateTable().
110453*/
110454SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
110455  Table *p = 0;
110456  int i;
110457
110458  /* All mutexes are required for schema access.  Make sure we hold them. */
110459  assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
110460#if SQLITE_USER_AUTHENTICATION
110461  /* Only the admin user is allowed to know that the sqlite_user table
110462  ** exists */
110463  if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
110464    return 0;
110465  }
110466#endif
110467  if( zDatabase ){
110468    for(i=0; i<db->nDb; i++){
110469      if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
110470    }
110471    if( i>=db->nDb ){
110472      /* No match against the official names.  But always match "main"
110473      ** to schema 0 as a legacy fallback. */
110474      if( sqlite3StrICmp(zDatabase,"main")==0 ){
110475        i = 0;
110476      }else{
110477        return 0;
110478      }
110479    }
110480    p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
110481    if( p==0 && i==1 && sqlite3StrICmp(zName, MASTER_NAME)==0 ){
110482      /* All temp.sqlite_master to be an alias for sqlite_temp_master */
110483      p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, TEMP_MASTER_NAME);
110484    }
110485  }else{
110486    /* Match against TEMP first */
110487    p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName);
110488    if( p ) return p;
110489    /* The main database is second */
110490    p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName);
110491    if( p ) return p;
110492    /* Attached databases are in order of attachment */
110493    for(i=2; i<db->nDb; i++){
110494      assert( sqlite3SchemaMutexHeld(db, i, 0) );
110495      p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
110496      if( p ) break;
110497    }
110498  }
110499  return p;
110500}
110501
110502/*
110503** Locate the in-memory structure that describes a particular database
110504** table given the name of that table and (optionally) the name of the
110505** database containing the table.  Return NULL if not found.  Also leave an
110506** error message in pParse->zErrMsg.
110507**
110508** The difference between this routine and sqlite3FindTable() is that this
110509** routine leaves an error message in pParse->zErrMsg where
110510** sqlite3FindTable() does not.
110511*/
110512SQLITE_PRIVATE Table *sqlite3LocateTable(
110513  Parse *pParse,         /* context in which to report errors */
110514  u32 flags,             /* LOCATE_VIEW or LOCATE_NOERR */
110515  const char *zName,     /* Name of the table we are looking for */
110516  const char *zDbase     /* Name of the database.  Might be NULL */
110517){
110518  Table *p;
110519  sqlite3 *db = pParse->db;
110520
110521  /* Read the database schema. If an error occurs, leave an error message
110522  ** and code in pParse and return NULL. */
110523  if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
110524   && SQLITE_OK!=sqlite3ReadSchema(pParse)
110525  ){
110526    return 0;
110527  }
110528
110529  p = sqlite3FindTable(db, zName, zDbase);
110530  if( p==0 ){
110531#ifndef SQLITE_OMIT_VIRTUALTABLE
110532    /* If zName is the not the name of a table in the schema created using
110533    ** CREATE, then check to see if it is the name of an virtual table that
110534    ** can be an eponymous virtual table. */
110535    if( pParse->disableVtab==0 ){
110536      Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
110537      if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
110538        pMod = sqlite3PragmaVtabRegister(db, zName);
110539      }
110540      if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
110541        return pMod->pEpoTab;
110542      }
110543    }
110544#endif
110545    if( flags & LOCATE_NOERR ) return 0;
110546    pParse->checkSchema = 1;
110547  }else if( IsVirtual(p) && pParse->disableVtab ){
110548    p = 0;
110549  }
110550
110551  if( p==0 ){
110552    const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
110553    if( zDbase ){
110554      sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
110555    }else{
110556      sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
110557    }
110558  }
110559
110560  return p;
110561}
110562
110563/*
110564** Locate the table identified by *p.
110565**
110566** This is a wrapper around sqlite3LocateTable(). The difference between
110567** sqlite3LocateTable() and this function is that this function restricts
110568** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
110569** non-NULL if it is part of a view or trigger program definition. See
110570** sqlite3FixSrcList() for details.
110571*/
110572SQLITE_PRIVATE Table *sqlite3LocateTableItem(
110573  Parse *pParse,
110574  u32 flags,
110575  struct SrcList_item *p
110576){
110577  const char *zDb;
110578  assert( p->pSchema==0 || p->zDatabase==0 );
110579  if( p->pSchema ){
110580    int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
110581    zDb = pParse->db->aDb[iDb].zDbSName;
110582  }else{
110583    zDb = p->zDatabase;
110584  }
110585  return sqlite3LocateTable(pParse, flags, p->zName, zDb);
110586}
110587
110588/*
110589** Locate the in-memory structure that describes
110590** a particular index given the name of that index
110591** and the name of the database that contains the index.
110592** Return NULL if not found.
110593**
110594** If zDatabase is 0, all databases are searched for the
110595** table and the first matching index is returned.  (No checking
110596** for duplicate index names is done.)  The search order is
110597** TEMP first, then MAIN, then any auxiliary databases added
110598** using the ATTACH command.
110599*/
110600SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
110601  Index *p = 0;
110602  int i;
110603  /* All mutexes are required for schema access.  Make sure we hold them. */
110604  assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
110605  for(i=OMIT_TEMPDB; i<db->nDb; i++){
110606    int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
110607    Schema *pSchema = db->aDb[j].pSchema;
110608    assert( pSchema );
110609    if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
110610    assert( sqlite3SchemaMutexHeld(db, j, 0) );
110611    p = sqlite3HashFind(&pSchema->idxHash, zName);
110612    if( p ) break;
110613  }
110614  return p;
110615}
110616
110617/*
110618** Reclaim the memory used by an index
110619*/
110620SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3 *db, Index *p){
110621#ifndef SQLITE_OMIT_ANALYZE
110622  sqlite3DeleteIndexSamples(db, p);
110623#endif
110624  sqlite3ExprDelete(db, p->pPartIdxWhere);
110625  sqlite3ExprListDelete(db, p->aColExpr);
110626  sqlite3DbFree(db, p->zColAff);
110627  if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
110628#ifdef SQLITE_ENABLE_STAT4
110629  sqlite3_free(p->aiRowEst);
110630#endif
110631  sqlite3DbFree(db, p);
110632}
110633
110634/*
110635** For the index called zIdxName which is found in the database iDb,
110636** unlike that index from its Table then remove the index from
110637** the index hash table and free all memory structures associated
110638** with the index.
110639*/
110640SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
110641  Index *pIndex;
110642  Hash *pHash;
110643
110644  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
110645  pHash = &db->aDb[iDb].pSchema->idxHash;
110646  pIndex = sqlite3HashInsert(pHash, zIdxName, 0);
110647  if( ALWAYS(pIndex) ){
110648    if( pIndex->pTable->pIndex==pIndex ){
110649      pIndex->pTable->pIndex = pIndex->pNext;
110650    }else{
110651      Index *p;
110652      /* Justification of ALWAYS();  The index must be on the list of
110653      ** indices. */
110654      p = pIndex->pTable->pIndex;
110655      while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
110656      if( ALWAYS(p && p->pNext==pIndex) ){
110657        p->pNext = pIndex->pNext;
110658      }
110659    }
110660    sqlite3FreeIndex(db, pIndex);
110661  }
110662  db->mDbFlags |= DBFLAG_SchemaChange;
110663}
110664
110665/*
110666** Look through the list of open database files in db->aDb[] and if
110667** any have been closed, remove them from the list.  Reallocate the
110668** db->aDb[] structure to a smaller size, if possible.
110669**
110670** Entry 0 (the "main" database) and entry 1 (the "temp" database)
110671** are never candidates for being collapsed.
110672*/
110673SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
110674  int i, j;
110675  for(i=j=2; i<db->nDb; i++){
110676    struct Db *pDb = &db->aDb[i];
110677    if( pDb->pBt==0 ){
110678      sqlite3DbFree(db, pDb->zDbSName);
110679      pDb->zDbSName = 0;
110680      continue;
110681    }
110682    if( j<i ){
110683      db->aDb[j] = db->aDb[i];
110684    }
110685    j++;
110686  }
110687  db->nDb = j;
110688  if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
110689    memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
110690    sqlite3DbFree(db, db->aDb);
110691    db->aDb = db->aDbStatic;
110692  }
110693}
110694
110695/*
110696** Reset the schema for the database at index iDb.  Also reset the
110697** TEMP schema.  The reset is deferred if db->nSchemaLock is not zero.
110698** Deferred resets may be run by calling with iDb<0.
110699*/
110700SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
110701  int i;
110702  assert( iDb<db->nDb );
110703
110704  if( iDb>=0 ){
110705    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
110706    DbSetProperty(db, iDb, DB_ResetWanted);
110707    DbSetProperty(db, 1, DB_ResetWanted);
110708    db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
110709  }
110710
110711  if( db->nSchemaLock==0 ){
110712    for(i=0; i<db->nDb; i++){
110713      if( DbHasProperty(db, i, DB_ResetWanted) ){
110714        sqlite3SchemaClear(db->aDb[i].pSchema);
110715      }
110716    }
110717  }
110718}
110719
110720/*
110721** Erase all schema information from all attached databases (including
110722** "main" and "temp") for a single database connection.
110723*/
110724SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
110725  int i;
110726  sqlite3BtreeEnterAll(db);
110727  for(i=0; i<db->nDb; i++){
110728    Db *pDb = &db->aDb[i];
110729    if( pDb->pSchema ){
110730      if( db->nSchemaLock==0 ){
110731        sqlite3SchemaClear(pDb->pSchema);
110732      }else{
110733        DbSetProperty(db, i, DB_ResetWanted);
110734      }
110735    }
110736  }
110737  db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
110738  sqlite3VtabUnlockList(db);
110739  sqlite3BtreeLeaveAll(db);
110740  if( db->nSchemaLock==0 ){
110741    sqlite3CollapseDatabaseArray(db);
110742  }
110743}
110744
110745/*
110746** This routine is called when a commit occurs.
110747*/
110748SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
110749  db->mDbFlags &= ~DBFLAG_SchemaChange;
110750}
110751
110752/*
110753** Delete memory allocated for the column names of a table or view (the
110754** Table.aCol[] array).
110755*/
110756SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
110757  int i;
110758  Column *pCol;
110759  assert( pTable!=0 );
110760  if( (pCol = pTable->aCol)!=0 ){
110761    for(i=0; i<pTable->nCol; i++, pCol++){
110762      assert( pCol->zName==0 || pCol->hName==sqlite3StrIHash(pCol->zName) );
110763      sqlite3DbFree(db, pCol->zName);
110764      sqlite3ExprDelete(db, pCol->pDflt);
110765      sqlite3DbFree(db, pCol->zColl);
110766    }
110767    sqlite3DbFree(db, pTable->aCol);
110768  }
110769}
110770
110771/*
110772** Remove the memory data structures associated with the given
110773** Table.  No changes are made to disk by this routine.
110774**
110775** This routine just deletes the data structure.  It does not unlink
110776** the table data structure from the hash table.  But it does destroy
110777** memory structures of the indices and foreign keys associated with
110778** the table.
110779**
110780** The db parameter is optional.  It is needed if the Table object
110781** contains lookaside memory.  (Table objects in the schema do not use
110782** lookaside memory, but some ephemeral Table objects do.)  Or the
110783** db parameter can be used with db->pnBytesFreed to measure the memory
110784** used by the Table object.
110785*/
110786static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){
110787  Index *pIndex, *pNext;
110788
110789#ifdef SQLITE_DEBUG
110790  /* Record the number of outstanding lookaside allocations in schema Tables
110791  ** prior to doing any free() operations. Since schema Tables do not use
110792  ** lookaside, this number should not change.
110793  **
110794  ** If malloc has already failed, it may be that it failed while allocating
110795  ** a Table object that was going to be marked ephemeral. So do not check
110796  ** that no lookaside memory is used in this case either. */
110797  int nLookaside = 0;
110798  if( db && !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
110799    nLookaside = sqlite3LookasideUsed(db, 0);
110800  }
110801#endif
110802
110803  /* Delete all indices associated with this table. */
110804  for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
110805    pNext = pIndex->pNext;
110806    assert( pIndex->pSchema==pTable->pSchema
110807         || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
110808    if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
110809      char *zName = pIndex->zName;
110810      TESTONLY ( Index *pOld = ) sqlite3HashInsert(
110811         &pIndex->pSchema->idxHash, zName, 0
110812      );
110813      assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
110814      assert( pOld==pIndex || pOld==0 );
110815    }
110816    sqlite3FreeIndex(db, pIndex);
110817  }
110818
110819  /* Delete any foreign keys attached to this table. */
110820  sqlite3FkDelete(db, pTable);
110821
110822  /* Delete the Table structure itself.
110823  */
110824  sqlite3DeleteColumnNames(db, pTable);
110825  sqlite3DbFree(db, pTable->zName);
110826  sqlite3DbFree(db, pTable->zColAff);
110827  sqlite3SelectDelete(db, pTable->pSelect);
110828  sqlite3ExprListDelete(db, pTable->pCheck);
110829#ifndef SQLITE_OMIT_VIRTUALTABLE
110830  sqlite3VtabClear(db, pTable);
110831#endif
110832  sqlite3DbFree(db, pTable);
110833
110834  /* Verify that no lookaside memory was used by schema tables */
110835  assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) );
110836}
110837SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
110838  /* Do not delete the table until the reference count reaches zero. */
110839  if( !pTable ) return;
110840  if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
110841  deleteTable(db, pTable);
110842}
110843
110844
110845/*
110846** Unlink the given table from the hash tables and the delete the
110847** table structure with all its indices and foreign keys.
110848*/
110849SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
110850  Table *p;
110851  Db *pDb;
110852
110853  assert( db!=0 );
110854  assert( iDb>=0 && iDb<db->nDb );
110855  assert( zTabName );
110856  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
110857  testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
110858  pDb = &db->aDb[iDb];
110859  p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
110860  sqlite3DeleteTable(db, p);
110861  db->mDbFlags |= DBFLAG_SchemaChange;
110862}
110863
110864/*
110865** Given a token, return a string that consists of the text of that
110866** token.  Space to hold the returned string
110867** is obtained from sqliteMalloc() and must be freed by the calling
110868** function.
110869**
110870** Any quotation marks (ex:  "name", 'name', [name], or `name`) that
110871** surround the body of the token are removed.
110872**
110873** Tokens are often just pointers into the original SQL text and so
110874** are not \000 terminated and are not persistent.  The returned string
110875** is \000 terminated and is persistent.
110876*/
110877SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
110878  char *zName;
110879  if( pName ){
110880    zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
110881    sqlite3Dequote(zName);
110882  }else{
110883    zName = 0;
110884  }
110885  return zName;
110886}
110887
110888/*
110889** Open the sqlite_master table stored in database number iDb for
110890** writing. The table is opened using cursor 0.
110891*/
110892SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
110893  Vdbe *v = sqlite3GetVdbe(p);
110894  sqlite3TableLock(p, iDb, MASTER_ROOT, 1, MASTER_NAME);
110895  sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5);
110896  if( p->nTab==0 ){
110897    p->nTab = 1;
110898  }
110899}
110900
110901/*
110902** Parameter zName points to a nul-terminated buffer containing the name
110903** of a database ("main", "temp" or the name of an attached db). This
110904** function returns the index of the named database in db->aDb[], or
110905** -1 if the named db cannot be found.
110906*/
110907SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
110908  int i = -1;         /* Database number */
110909  if( zName ){
110910    Db *pDb;
110911    for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
110912      if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
110913      /* "main" is always an acceptable alias for the primary database
110914      ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */
110915      if( i==0 && 0==sqlite3_stricmp("main", zName) ) break;
110916    }
110917  }
110918  return i;
110919}
110920
110921/*
110922** The token *pName contains the name of a database (either "main" or
110923** "temp" or the name of an attached db). This routine returns the
110924** index of the named database in db->aDb[], or -1 if the named db
110925** does not exist.
110926*/
110927SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
110928  int i;                               /* Database number */
110929  char *zName;                         /* Name we are searching for */
110930  zName = sqlite3NameFromToken(db, pName);
110931  i = sqlite3FindDbName(db, zName);
110932  sqlite3DbFree(db, zName);
110933  return i;
110934}
110935
110936/* The table or view or trigger name is passed to this routine via tokens
110937** pName1 and pName2. If the table name was fully qualified, for example:
110938**
110939** CREATE TABLE xxx.yyy (...);
110940**
110941** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
110942** the table name is not fully qualified, i.e.:
110943**
110944** CREATE TABLE yyy(...);
110945**
110946** Then pName1 is set to "yyy" and pName2 is "".
110947**
110948** This routine sets the *ppUnqual pointer to point at the token (pName1 or
110949** pName2) that stores the unqualified table name.  The index of the
110950** database "xxx" is returned.
110951*/
110952SQLITE_PRIVATE int sqlite3TwoPartName(
110953  Parse *pParse,      /* Parsing and code generating context */
110954  Token *pName1,      /* The "xxx" in the name "xxx.yyy" or "xxx" */
110955  Token *pName2,      /* The "yyy" in the name "xxx.yyy" */
110956  Token **pUnqual     /* Write the unqualified object name here */
110957){
110958  int iDb;                    /* Database holding the object */
110959  sqlite3 *db = pParse->db;
110960
110961  assert( pName2!=0 );
110962  if( pName2->n>0 ){
110963    if( db->init.busy ) {
110964      sqlite3ErrorMsg(pParse, "corrupt database");
110965      return -1;
110966    }
110967    *pUnqual = pName2;
110968    iDb = sqlite3FindDb(db, pName1);
110969    if( iDb<0 ){
110970      sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
110971      return -1;
110972    }
110973  }else{
110974    assert( db->init.iDb==0 || db->init.busy || IN_RENAME_OBJECT
110975             || (db->mDbFlags & DBFLAG_Vacuum)!=0);
110976    iDb = db->init.iDb;
110977    *pUnqual = pName1;
110978  }
110979  return iDb;
110980}
110981
110982/*
110983** True if PRAGMA writable_schema is ON
110984*/
110985SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3 *db){
110986  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
110987  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
110988               SQLITE_WriteSchema );
110989  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
110990               SQLITE_Defensive );
110991  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
110992               (SQLITE_WriteSchema|SQLITE_Defensive) );
110993  return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
110994}
110995
110996/*
110997** This routine is used to check if the UTF-8 string zName is a legal
110998** unqualified name for a new schema object (table, index, view or
110999** trigger). All names are legal except those that begin with the string
111000** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
111001** is reserved for internal use.
111002**
111003** When parsing the sqlite_master table, this routine also checks to
111004** make sure the "type", "name", and "tbl_name" columns are consistent
111005** with the SQL.
111006*/
111007SQLITE_PRIVATE int sqlite3CheckObjectName(
111008  Parse *pParse,            /* Parsing context */
111009  const char *zName,        /* Name of the object to check */
111010  const char *zType,        /* Type of this object */
111011  const char *zTblName      /* Parent table name for triggers and indexes */
111012){
111013  sqlite3 *db = pParse->db;
111014  if( sqlite3WritableSchema(db) || db->init.imposterTable ){
111015    /* Skip these error checks for writable_schema=ON */
111016    return SQLITE_OK;
111017  }
111018  if( db->init.busy ){
111019    if( sqlite3_stricmp(zType, db->init.azInit[0])
111020     || sqlite3_stricmp(zName, db->init.azInit[1])
111021     || sqlite3_stricmp(zTblName, db->init.azInit[2])
111022    ){
111023      if( sqlite3Config.bExtraSchemaChecks ){
111024        sqlite3ErrorMsg(pParse, ""); /* corruptSchema() will supply the error */
111025        return SQLITE_ERROR;
111026      }
111027    }
111028  }else{
111029    if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7))
111030     || (sqlite3ReadOnlyShadowTables(db) && sqlite3ShadowTableName(db, zName))
111031    ){
111032      sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s",
111033                      zName);
111034      return SQLITE_ERROR;
111035    }
111036
111037  }
111038  return SQLITE_OK;
111039}
111040
111041/*
111042** Return the PRIMARY KEY index of a table
111043*/
111044SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){
111045  Index *p;
111046  for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
111047  return p;
111048}
111049
111050/*
111051** Convert an table column number into a index column number.  That is,
111052** for the column iCol in the table (as defined by the CREATE TABLE statement)
111053** find the (first) offset of that column in index pIdx.  Or return -1
111054** if column iCol is not used in index pIdx.
111055*/
111056SQLITE_PRIVATE i16 sqlite3TableColumnToIndex(Index *pIdx, i16 iCol){
111057  int i;
111058  for(i=0; i<pIdx->nColumn; i++){
111059    if( iCol==pIdx->aiColumn[i] ) return i;
111060  }
111061  return -1;
111062}
111063
111064#ifndef SQLITE_OMIT_GENERATED_COLUMNS
111065/* Convert a storage column number into a table column number.
111066**
111067** The storage column number (0,1,2,....) is the index of the value
111068** as it appears in the record on disk.  The true column number
111069** is the index (0,1,2,...) of the column in the CREATE TABLE statement.
111070**
111071** The storage column number is less than the table column number if
111072** and only there are VIRTUAL columns to the left.
111073**
111074** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
111075*/
111076SQLITE_PRIVATE i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){
111077  if( pTab->tabFlags & TF_HasVirtual ){
111078    int i;
111079    for(i=0; i<=iCol; i++){
111080      if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++;
111081    }
111082  }
111083  return iCol;
111084}
111085#endif
111086
111087#ifndef SQLITE_OMIT_GENERATED_COLUMNS
111088/* Convert a table column number into a storage column number.
111089**
111090** The storage column number (0,1,2,....) is the index of the value
111091** as it appears in the record on disk.  Or, if the input column is
111092** the N-th virtual column (zero-based) then the storage number is
111093** the number of non-virtual columns in the table plus N.
111094**
111095** The true column number is the index (0,1,2,...) of the column in
111096** the CREATE TABLE statement.
111097**
111098** If the input column is a VIRTUAL column, then it should not appear
111099** in storage.  But the value sometimes is cached in registers that
111100** follow the range of registers used to construct storage.  This
111101** avoids computing the same VIRTUAL column multiple times, and provides
111102** values for use by OP_Param opcodes in triggers.  Hence, if the
111103** input column is a VIRTUAL table, put it after all the other columns.
111104**
111105** In the following, N means "normal column", S means STORED, and
111106** V means VIRTUAL.  Suppose the CREATE TABLE has columns like this:
111107**
111108**        CREATE TABLE ex(N,S,V,N,S,V,N,S,V);
111109**                     -- 0 1 2 3 4 5 6 7 8
111110**
111111** Then the mapping from this function is as follows:
111112**
111113**    INPUTS:     0 1 2 3 4 5 6 7 8
111114**    OUTPUTS:    0 1 6 2 3 7 4 5 8
111115**
111116** So, in other words, this routine shifts all the virtual columns to
111117** the end.
111118**
111119** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and
111120** this routine is a no-op macro.  If the pTab does not have any virtual
111121** columns, then this routine is no-op that always return iCol.  If iCol
111122** is negative (indicating the ROWID column) then this routine return iCol.
111123*/
111124SQLITE_PRIVATE i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){
111125  int i;
111126  i16 n;
111127  assert( iCol<pTab->nCol );
111128  if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
111129  for(i=0, n=0; i<iCol; i++){
111130    if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
111131  }
111132  if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
111133    /* iCol is a virtual column itself */
111134    return pTab->nNVCol + i - n;
111135  }else{
111136    /* iCol is a normal or stored column */
111137    return n;
111138  }
111139}
111140#endif
111141
111142/*
111143** Begin constructing a new table representation in memory.  This is
111144** the first of several action routines that get called in response
111145** to a CREATE TABLE statement.  In particular, this routine is called
111146** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
111147** flag is true if the table should be stored in the auxiliary database
111148** file instead of in the main database file.  This is normally the case
111149** when the "TEMP" or "TEMPORARY" keyword occurs in between
111150** CREATE and TABLE.
111151**
111152** The new table record is initialized and put in pParse->pNewTable.
111153** As more of the CREATE TABLE statement is parsed, additional action
111154** routines will be called to add more information to this record.
111155** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
111156** is called to complete the construction of the new table record.
111157*/
111158SQLITE_PRIVATE void sqlite3StartTable(
111159  Parse *pParse,   /* Parser context */
111160  Token *pName1,   /* First part of the name of the table or view */
111161  Token *pName2,   /* Second part of the name of the table or view */
111162  int isTemp,      /* True if this is a TEMP table */
111163  int isView,      /* True if this is a VIEW */
111164  int isVirtual,   /* True if this is a VIRTUAL table */
111165  int noErr        /* Do nothing if table already exists */
111166){
111167  Table *pTable;
111168  char *zName = 0; /* The name of the new table */
111169  sqlite3 *db = pParse->db;
111170  Vdbe *v;
111171  int iDb;         /* Database number to create the table in */
111172  Token *pName;    /* Unqualified name of the table to create */
111173
111174  if( db->init.busy && db->init.newTnum==1 ){
111175    /* Special case:  Parsing the sqlite_master or sqlite_temp_master schema */
111176    iDb = db->init.iDb;
111177    zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb));
111178    pName = pName1;
111179  }else{
111180    /* The common case */
111181    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
111182    if( iDb<0 ) return;
111183    if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
111184      /* If creating a temp table, the name may not be qualified. Unless
111185      ** the database name is "temp" anyway.  */
111186      sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
111187      return;
111188    }
111189    if( !OMIT_TEMPDB && isTemp ) iDb = 1;
111190    zName = sqlite3NameFromToken(db, pName);
111191    if( IN_RENAME_OBJECT ){
111192      sqlite3RenameTokenMap(pParse, (void*)zName, pName);
111193    }
111194  }
111195  pParse->sNameToken = *pName;
111196  if( zName==0 ) return;
111197  if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName) ){
111198    goto begin_table_error;
111199  }
111200  if( db->init.iDb==1 ) isTemp = 1;
111201#ifndef SQLITE_OMIT_AUTHORIZATION
111202  assert( isTemp==0 || isTemp==1 );
111203  assert( isView==0 || isView==1 );
111204  {
111205    static const u8 aCode[] = {
111206       SQLITE_CREATE_TABLE,
111207       SQLITE_CREATE_TEMP_TABLE,
111208       SQLITE_CREATE_VIEW,
111209       SQLITE_CREATE_TEMP_VIEW
111210    };
111211    char *zDb = db->aDb[iDb].zDbSName;
111212    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
111213      goto begin_table_error;
111214    }
111215    if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
111216                                       zName, 0, zDb) ){
111217      goto begin_table_error;
111218    }
111219  }
111220#endif
111221
111222  /* Make sure the new table name does not collide with an existing
111223  ** index or table name in the same database.  Issue an error message if
111224  ** it does. The exception is if the statement being parsed was passed
111225  ** to an sqlite3_declare_vtab() call. In that case only the column names
111226  ** and types will be used, so there is no need to test for namespace
111227  ** collisions.
111228  */
111229  if( !IN_SPECIAL_PARSE ){
111230    char *zDb = db->aDb[iDb].zDbSName;
111231    if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
111232      goto begin_table_error;
111233    }
111234    pTable = sqlite3FindTable(db, zName, zDb);
111235    if( pTable ){
111236      if( !noErr ){
111237        sqlite3ErrorMsg(pParse, "table %T already exists", pName);
111238      }else{
111239        assert( !db->init.busy || CORRUPT_DB );
111240        sqlite3CodeVerifySchema(pParse, iDb);
111241      }
111242      goto begin_table_error;
111243    }
111244    if( sqlite3FindIndex(db, zName, zDb)!=0 ){
111245      sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
111246      goto begin_table_error;
111247    }
111248  }
111249
111250  pTable = sqlite3DbMallocZero(db, sizeof(Table));
111251  if( pTable==0 ){
111252    assert( db->mallocFailed );
111253    pParse->rc = SQLITE_NOMEM_BKPT;
111254    pParse->nErr++;
111255    goto begin_table_error;
111256  }
111257  pTable->zName = zName;
111258  pTable->iPKey = -1;
111259  pTable->pSchema = db->aDb[iDb].pSchema;
111260  pTable->nTabRef = 1;
111261#ifdef SQLITE_DEFAULT_ROWEST
111262  pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
111263#else
111264  pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
111265#endif
111266  assert( pParse->pNewTable==0 );
111267  pParse->pNewTable = pTable;
111268
111269  /* If this is the magic sqlite_sequence table used by autoincrement,
111270  ** then record a pointer to this table in the main database structure
111271  ** so that INSERT can find the table easily.
111272  */
111273#ifndef SQLITE_OMIT_AUTOINCREMENT
111274  if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
111275    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
111276    pTable->pSchema->pSeqTab = pTable;
111277  }
111278#endif
111279
111280  /* Begin generating the code that will insert the table record into
111281  ** the SQLITE_MASTER table.  Note in particular that we must go ahead
111282  ** and allocate the record number for the table entry now.  Before any
111283  ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
111284  ** indices to be created and the table record must come before the
111285  ** indices.  Hence, the record number for the table must be allocated
111286  ** now.
111287  */
111288  if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
111289    int addr1;
111290    int fileFormat;
111291    int reg1, reg2, reg3;
111292    /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
111293    static const char nullRow[] = { 6, 0, 0, 0, 0, 0 };
111294    sqlite3BeginWriteOperation(pParse, 1, iDb);
111295
111296#ifndef SQLITE_OMIT_VIRTUALTABLE
111297    if( isVirtual ){
111298      sqlite3VdbeAddOp0(v, OP_VBegin);
111299    }
111300#endif
111301
111302    /* If the file format and encoding in the database have not been set,
111303    ** set them now.
111304    */
111305    reg1 = pParse->regRowid = ++pParse->nMem;
111306    reg2 = pParse->regRoot = ++pParse->nMem;
111307    reg3 = ++pParse->nMem;
111308    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
111309    sqlite3VdbeUsesBtree(v, iDb);
111310    addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
111311    fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
111312                  1 : SQLITE_MAX_FILE_FORMAT;
111313    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat);
111314    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db));
111315    sqlite3VdbeJumpHere(v, addr1);
111316
111317    /* This just creates a place-holder record in the sqlite_master table.
111318    ** The record created does not contain anything yet.  It will be replaced
111319    ** by the real entry in code generated at sqlite3EndTable().
111320    **
111321    ** The rowid for the new entry is left in register pParse->regRowid.
111322    ** The root page number of the new table is left in reg pParse->regRoot.
111323    ** The rowid and root page number values are needed by the code that
111324    ** sqlite3EndTable will generate.
111325    */
111326#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
111327    if( isView || isVirtual ){
111328      sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
111329    }else
111330#endif
111331    {
111332      pParse->addrCrTab =
111333         sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, reg2, BTREE_INTKEY);
111334    }
111335    sqlite3OpenMasterTable(pParse, iDb);
111336    sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
111337    sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC);
111338    sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
111339    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
111340    sqlite3VdbeAddOp0(v, OP_Close);
111341  }
111342
111343  /* Normal (non-error) return. */
111344  return;
111345
111346  /* If an error occurs, we jump here */
111347begin_table_error:
111348  sqlite3DbFree(db, zName);
111349  return;
111350}
111351
111352/* Set properties of a table column based on the (magical)
111353** name of the column.
111354*/
111355#if SQLITE_ENABLE_HIDDEN_COLUMNS
111356SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
111357  if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){
111358    pCol->colFlags |= COLFLAG_HIDDEN;
111359  }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
111360    pTab->tabFlags |= TF_OOOHidden;
111361  }
111362}
111363#endif
111364
111365
111366/*
111367** Add a new column to the table currently being constructed.
111368**
111369** The parser calls this routine once for each column declaration
111370** in a CREATE TABLE statement.  sqlite3StartTable() gets called
111371** first to get things going.  Then this routine is called for each
111372** column.
111373*/
111374SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName, Token *pType){
111375  Table *p;
111376  int i;
111377  char *z;
111378  char *zType;
111379  Column *pCol;
111380  sqlite3 *db = pParse->db;
111381  if( (p = pParse->pNewTable)==0 ) return;
111382  if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
111383    sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
111384    return;
111385  }
111386  z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2);
111387  if( z==0 ) return;
111388  if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, pName);
111389  memcpy(z, pName->z, pName->n);
111390  z[pName->n] = 0;
111391  sqlite3Dequote(z);
111392  for(i=0; i<p->nCol; i++){
111393    if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
111394      sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
111395      sqlite3DbFree(db, z);
111396      return;
111397    }
111398  }
111399  if( (p->nCol & 0x7)==0 ){
111400    Column *aNew;
111401    aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
111402    if( aNew==0 ){
111403      sqlite3DbFree(db, z);
111404      return;
111405    }
111406    p->aCol = aNew;
111407  }
111408  pCol = &p->aCol[p->nCol];
111409  memset(pCol, 0, sizeof(p->aCol[0]));
111410  pCol->zName = z;
111411  pCol->hName = sqlite3StrIHash(z);
111412  sqlite3ColumnPropertiesFromName(p, pCol);
111413
111414  if( pType->n==0 ){
111415    /* If there is no type specified, columns have the default affinity
111416    ** 'BLOB' with a default size of 4 bytes. */
111417    pCol->affinity = SQLITE_AFF_BLOB;
111418    pCol->szEst = 1;
111419#ifdef SQLITE_ENABLE_SORTER_REFERENCES
111420    if( 4>=sqlite3GlobalConfig.szSorterRef ){
111421      pCol->colFlags |= COLFLAG_SORTERREF;
111422    }
111423#endif
111424  }else{
111425    zType = z + sqlite3Strlen30(z) + 1;
111426    memcpy(zType, pType->z, pType->n);
111427    zType[pType->n] = 0;
111428    sqlite3Dequote(zType);
111429    pCol->affinity = sqlite3AffinityType(zType, pCol);
111430    pCol->colFlags |= COLFLAG_HASTYPE;
111431  }
111432  p->nCol++;
111433  p->nNVCol++;
111434  pParse->constraintName.n = 0;
111435}
111436
111437/*
111438** This routine is called by the parser while in the middle of
111439** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
111440** been seen on a column.  This routine sets the notNull flag on
111441** the column currently under construction.
111442*/
111443SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
111444  Table *p;
111445  Column *pCol;
111446  p = pParse->pNewTable;
111447  if( p==0 || NEVER(p->nCol<1) ) return;
111448  pCol = &p->aCol[p->nCol-1];
111449  pCol->notNull = (u8)onError;
111450  p->tabFlags |= TF_HasNotNull;
111451
111452  /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
111453  ** on this column.  */
111454  if( pCol->colFlags & COLFLAG_UNIQUE ){
111455    Index *pIdx;
111456    for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
111457      assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
111458      if( pIdx->aiColumn[0]==p->nCol-1 ){
111459        pIdx->uniqNotNull = 1;
111460      }
111461    }
111462  }
111463}
111464
111465/*
111466** Scan the column type name zType (length nType) and return the
111467** associated affinity type.
111468**
111469** This routine does a case-independent search of zType for the
111470** substrings in the following table. If one of the substrings is
111471** found, the corresponding affinity is returned. If zType contains
111472** more than one of the substrings, entries toward the top of
111473** the table take priority. For example, if zType is 'BLOBINT',
111474** SQLITE_AFF_INTEGER is returned.
111475**
111476** Substring     | Affinity
111477** --------------------------------
111478** 'INT'         | SQLITE_AFF_INTEGER
111479** 'CHAR'        | SQLITE_AFF_TEXT
111480** 'CLOB'        | SQLITE_AFF_TEXT
111481** 'TEXT'        | SQLITE_AFF_TEXT
111482** 'BLOB'        | SQLITE_AFF_BLOB
111483** 'REAL'        | SQLITE_AFF_REAL
111484** 'FLOA'        | SQLITE_AFF_REAL
111485** 'DOUB'        | SQLITE_AFF_REAL
111486**
111487** If none of the substrings in the above table are found,
111488** SQLITE_AFF_NUMERIC is returned.
111489*/
111490SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, Column *pCol){
111491  u32 h = 0;
111492  char aff = SQLITE_AFF_NUMERIC;
111493  const char *zChar = 0;
111494
111495  assert( zIn!=0 );
111496  while( zIn[0] ){
111497    h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
111498    zIn++;
111499    if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */
111500      aff = SQLITE_AFF_TEXT;
111501      zChar = zIn;
111502    }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
111503      aff = SQLITE_AFF_TEXT;
111504    }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
111505      aff = SQLITE_AFF_TEXT;
111506    }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
111507        && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
111508      aff = SQLITE_AFF_BLOB;
111509      if( zIn[0]=='(' ) zChar = zIn;
111510#ifndef SQLITE_OMIT_FLOATING_POINT
111511    }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
111512        && aff==SQLITE_AFF_NUMERIC ){
111513      aff = SQLITE_AFF_REAL;
111514    }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
111515        && aff==SQLITE_AFF_NUMERIC ){
111516      aff = SQLITE_AFF_REAL;
111517    }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b')          /* DOUB */
111518        && aff==SQLITE_AFF_NUMERIC ){
111519      aff = SQLITE_AFF_REAL;
111520#endif
111521    }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
111522      aff = SQLITE_AFF_INTEGER;
111523      break;
111524    }
111525  }
111526
111527  /* If pCol is not NULL, store an estimate of the field size.  The
111528  ** estimate is scaled so that the size of an integer is 1.  */
111529  if( pCol ){
111530    int v = 0;   /* default size is approx 4 bytes */
111531    if( aff<SQLITE_AFF_NUMERIC ){
111532      if( zChar ){
111533        while( zChar[0] ){
111534          if( sqlite3Isdigit(zChar[0]) ){
111535            /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
111536            sqlite3GetInt32(zChar, &v);
111537            break;
111538          }
111539          zChar++;
111540        }
111541      }else{
111542        v = 16;   /* BLOB, TEXT, CLOB -> r=5  (approx 20 bytes)*/
111543      }
111544    }
111545#ifdef SQLITE_ENABLE_SORTER_REFERENCES
111546    if( v>=sqlite3GlobalConfig.szSorterRef ){
111547      pCol->colFlags |= COLFLAG_SORTERREF;
111548    }
111549#endif
111550    v = v/4 + 1;
111551    if( v>255 ) v = 255;
111552    pCol->szEst = v;
111553  }
111554  return aff;
111555}
111556
111557/*
111558** The expression is the default value for the most recently added column
111559** of the table currently under construction.
111560**
111561** Default value expressions must be constant.  Raise an exception if this
111562** is not the case.
111563**
111564** This routine is called by the parser while in the middle of
111565** parsing a CREATE TABLE statement.
111566*/
111567SQLITE_PRIVATE void sqlite3AddDefaultValue(
111568  Parse *pParse,           /* Parsing context */
111569  Expr *pExpr,             /* The parsed expression of the default value */
111570  const char *zStart,      /* Start of the default value text */
111571  const char *zEnd         /* First character past end of defaut value text */
111572){
111573  Table *p;
111574  Column *pCol;
111575  sqlite3 *db = pParse->db;
111576  p = pParse->pNewTable;
111577  if( p!=0 ){
111578    int isInit = db->init.busy && db->init.iDb!=1;
111579    pCol = &(p->aCol[p->nCol-1]);
111580    if( !sqlite3ExprIsConstantOrFunction(pExpr, isInit) ){
111581      sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
111582          pCol->zName);
111583#ifndef SQLITE_OMIT_GENERATED_COLUMNS
111584    }else if( pCol->colFlags & COLFLAG_GENERATED ){
111585      testcase( pCol->colFlags & COLFLAG_VIRTUAL );
111586      testcase( pCol->colFlags & COLFLAG_STORED );
111587      sqlite3ErrorMsg(pParse, "cannot use DEFAULT on a generated column");
111588#endif
111589    }else{
111590      /* A copy of pExpr is used instead of the original, as pExpr contains
111591      ** tokens that point to volatile memory.
111592      */
111593      Expr x;
111594      sqlite3ExprDelete(db, pCol->pDflt);
111595      memset(&x, 0, sizeof(x));
111596      x.op = TK_SPAN;
111597      x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
111598      x.pLeft = pExpr;
111599      x.flags = EP_Skip;
111600      pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
111601      sqlite3DbFree(db, x.u.zToken);
111602    }
111603  }
111604  if( IN_RENAME_OBJECT ){
111605    sqlite3RenameExprUnmap(pParse, pExpr);
111606  }
111607  sqlite3ExprDelete(db, pExpr);
111608}
111609
111610/*
111611** Backwards Compatibility Hack:
111612**
111613** Historical versions of SQLite accepted strings as column names in
111614** indexes and PRIMARY KEY constraints and in UNIQUE constraints.  Example:
111615**
111616**     CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
111617**     CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
111618**
111619** This is goofy.  But to preserve backwards compatibility we continue to
111620** accept it.  This routine does the necessary conversion.  It converts
111621** the expression given in its argument from a TK_STRING into a TK_ID
111622** if the expression is just a TK_STRING with an optional COLLATE clause.
111623** If the expression is anything other than TK_STRING, the expression is
111624** unchanged.
111625*/
111626static void sqlite3StringToId(Expr *p){
111627  if( p->op==TK_STRING ){
111628    p->op = TK_ID;
111629  }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
111630    p->pLeft->op = TK_ID;
111631  }
111632}
111633
111634/*
111635** Tag the given column as being part of the PRIMARY KEY
111636*/
111637static void makeColumnPartOfPrimaryKey(Parse *pParse, Column *pCol){
111638  pCol->colFlags |= COLFLAG_PRIMKEY;
111639#ifndef SQLITE_OMIT_GENERATED_COLUMNS
111640  if( pCol->colFlags & COLFLAG_GENERATED ){
111641    testcase( pCol->colFlags & COLFLAG_VIRTUAL );
111642    testcase( pCol->colFlags & COLFLAG_STORED );
111643    sqlite3ErrorMsg(pParse,
111644      "generated columns cannot be part of the PRIMARY KEY");
111645  }
111646#endif
111647}
111648
111649/*
111650** Designate the PRIMARY KEY for the table.  pList is a list of names
111651** of columns that form the primary key.  If pList is NULL, then the
111652** most recently added column of the table is the primary key.
111653**
111654** A table can have at most one primary key.  If the table already has
111655** a primary key (and this is the second primary key) then create an
111656** error.
111657**
111658** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
111659** then we will try to use that column as the rowid.  Set the Table.iPKey
111660** field of the table under construction to be the index of the
111661** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
111662** no INTEGER PRIMARY KEY.
111663**
111664** If the key is not an INTEGER PRIMARY KEY, then create a unique
111665** index for the key.  No index is created for INTEGER PRIMARY KEYs.
111666*/
111667SQLITE_PRIVATE void sqlite3AddPrimaryKey(
111668  Parse *pParse,    /* Parsing context */
111669  ExprList *pList,  /* List of field names to be indexed */
111670  int onError,      /* What to do with a uniqueness conflict */
111671  int autoInc,      /* True if the AUTOINCREMENT keyword is present */
111672  int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
111673){
111674  Table *pTab = pParse->pNewTable;
111675  Column *pCol = 0;
111676  int iCol = -1, i;
111677  int nTerm;
111678  if( pTab==0 ) goto primary_key_exit;
111679  if( pTab->tabFlags & TF_HasPrimaryKey ){
111680    sqlite3ErrorMsg(pParse,
111681      "table \"%s\" has more than one primary key", pTab->zName);
111682    goto primary_key_exit;
111683  }
111684  pTab->tabFlags |= TF_HasPrimaryKey;
111685  if( pList==0 ){
111686    iCol = pTab->nCol - 1;
111687    pCol = &pTab->aCol[iCol];
111688    makeColumnPartOfPrimaryKey(pParse, pCol);
111689    nTerm = 1;
111690  }else{
111691    nTerm = pList->nExpr;
111692    for(i=0; i<nTerm; i++){
111693      Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
111694      assert( pCExpr!=0 );
111695      sqlite3StringToId(pCExpr);
111696      if( pCExpr->op==TK_ID ){
111697        const char *zCName = pCExpr->u.zToken;
111698        for(iCol=0; iCol<pTab->nCol; iCol++){
111699          if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){
111700            pCol = &pTab->aCol[iCol];
111701            makeColumnPartOfPrimaryKey(pParse, pCol);
111702            break;
111703          }
111704        }
111705      }
111706    }
111707  }
111708  if( nTerm==1
111709   && pCol
111710   && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0
111711   && sortOrder!=SQLITE_SO_DESC
111712  ){
111713    if( IN_RENAME_OBJECT && pList ){
111714      Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
111715      sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
111716    }
111717    pTab->iPKey = iCol;
111718    pTab->keyConf = (u8)onError;
111719    assert( autoInc==0 || autoInc==1 );
111720    pTab->tabFlags |= autoInc*TF_Autoincrement;
111721    if( pList ) pParse->iPkSortOrder = pList->a[0].sortFlags;
111722    (void)sqlite3HasExplicitNulls(pParse, pList);
111723  }else if( autoInc ){
111724#ifndef SQLITE_OMIT_AUTOINCREMENT
111725    sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
111726       "INTEGER PRIMARY KEY");
111727#endif
111728  }else{
111729    sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
111730                           0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY);
111731    pList = 0;
111732  }
111733
111734primary_key_exit:
111735  sqlite3ExprListDelete(pParse->db, pList);
111736  return;
111737}
111738
111739/*
111740** Add a new CHECK constraint to the table currently under construction.
111741*/
111742SQLITE_PRIVATE void sqlite3AddCheckConstraint(
111743  Parse *pParse,    /* Parsing context */
111744  Expr *pCheckExpr  /* The check expression */
111745){
111746#ifndef SQLITE_OMIT_CHECK
111747  Table *pTab = pParse->pNewTable;
111748  sqlite3 *db = pParse->db;
111749  if( pTab && !IN_DECLARE_VTAB
111750   && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
111751  ){
111752    pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
111753    if( pParse->constraintName.n ){
111754      sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
111755    }
111756  }else
111757#endif
111758  {
111759    sqlite3ExprDelete(pParse->db, pCheckExpr);
111760  }
111761}
111762
111763/*
111764** Set the collation function of the most recently parsed table column
111765** to the CollSeq given.
111766*/
111767SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
111768  Table *p;
111769  int i;
111770  char *zColl;              /* Dequoted name of collation sequence */
111771  sqlite3 *db;
111772
111773  if( (p = pParse->pNewTable)==0 ) return;
111774  i = p->nCol-1;
111775  db = pParse->db;
111776  zColl = sqlite3NameFromToken(db, pToken);
111777  if( !zColl ) return;
111778
111779  if( sqlite3LocateCollSeq(pParse, zColl) ){
111780    Index *pIdx;
111781    sqlite3DbFree(db, p->aCol[i].zColl);
111782    p->aCol[i].zColl = zColl;
111783
111784    /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
111785    ** then an index may have been created on this column before the
111786    ** collation type was added. Correct this if it is the case.
111787    */
111788    for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
111789      assert( pIdx->nKeyCol==1 );
111790      if( pIdx->aiColumn[0]==i ){
111791        pIdx->azColl[0] = p->aCol[i].zColl;
111792      }
111793    }
111794  }else{
111795    sqlite3DbFree(db, zColl);
111796  }
111797}
111798
111799/* Change the most recently parsed column to be a GENERATED ALWAYS AS
111800** column.
111801*/
111802SQLITE_PRIVATE void sqlite3AddGenerated(Parse *pParse, Expr *pExpr, Token *pType){
111803#ifndef SQLITE_OMIT_GENERATED_COLUMNS
111804  u8 eType = COLFLAG_VIRTUAL;
111805  Table *pTab = pParse->pNewTable;
111806  Column *pCol;
111807  if( pTab==0 ){
111808    /* generated column in an CREATE TABLE IF NOT EXISTS that already exists */
111809    goto generated_done;
111810  }
111811  pCol = &(pTab->aCol[pTab->nCol-1]);
111812  if( IN_DECLARE_VTAB ){
111813    sqlite3ErrorMsg(pParse, "virtual tables cannot use computed columns");
111814    goto generated_done;
111815  }
111816  if( pCol->pDflt ) goto generated_error;
111817  if( pType ){
111818    if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){
111819      /* no-op */
111820    }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){
111821      eType = COLFLAG_STORED;
111822    }else{
111823      goto generated_error;
111824    }
111825  }
111826  if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--;
111827  pCol->colFlags |= eType;
111828  assert( TF_HasVirtual==COLFLAG_VIRTUAL );
111829  assert( TF_HasStored==COLFLAG_STORED );
111830  pTab->tabFlags |= eType;
111831  if( pCol->colFlags & COLFLAG_PRIMKEY ){
111832    makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */
111833  }
111834  pCol->pDflt = pExpr;
111835  pExpr = 0;
111836  goto generated_done;
111837
111838generated_error:
111839  sqlite3ErrorMsg(pParse, "error in generated column \"%s\"",
111840                  pCol->zName);
111841generated_done:
111842  sqlite3ExprDelete(pParse->db, pExpr);
111843#else
111844  /* Throw and error for the GENERATED ALWAYS AS clause if the
111845  ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
111846  sqlite3ErrorMsg(pParse, "generated columns not supported");
111847  sqlite3ExprDelete(pParse->db, pExpr);
111848#endif
111849}
111850
111851/*
111852** Generate code that will increment the schema cookie.
111853**
111854** The schema cookie is used to determine when the schema for the
111855** database changes.  After each schema change, the cookie value
111856** changes.  When a process first reads the schema it records the
111857** cookie.  Thereafter, whenever it goes to access the database,
111858** it checks the cookie to make sure the schema has not changed
111859** since it was last read.
111860**
111861** This plan is not completely bullet-proof.  It is possible for
111862** the schema to change multiple times and for the cookie to be
111863** set back to prior value.  But schema changes are infrequent
111864** and the probability of hitting the same cookie value is only
111865** 1 chance in 2^32.  So we're safe enough.
111866**
111867** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
111868** the schema-version whenever the schema changes.
111869*/
111870SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
111871  sqlite3 *db = pParse->db;
111872  Vdbe *v = pParse->pVdbe;
111873  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
111874  sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION,
111875                   (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
111876}
111877
111878/*
111879** Measure the number of characters needed to output the given
111880** identifier.  The number returned includes any quotes used
111881** but does not include the null terminator.
111882**
111883** The estimate is conservative.  It might be larger that what is
111884** really needed.
111885*/
111886static int identLength(const char *z){
111887  int n;
111888  for(n=0; *z; n++, z++){
111889    if( *z=='"' ){ n++; }
111890  }
111891  return n + 2;
111892}
111893
111894/*
111895** The first parameter is a pointer to an output buffer. The second
111896** parameter is a pointer to an integer that contains the offset at
111897** which to write into the output buffer. This function copies the
111898** nul-terminated string pointed to by the third parameter, zSignedIdent,
111899** to the specified offset in the buffer and updates *pIdx to refer
111900** to the first byte after the last byte written before returning.
111901**
111902** If the string zSignedIdent consists entirely of alpha-numeric
111903** characters, does not begin with a digit and is not an SQL keyword,
111904** then it is copied to the output buffer exactly as it is. Otherwise,
111905** it is quoted using double-quotes.
111906*/
111907static void identPut(char *z, int *pIdx, char *zSignedIdent){
111908  unsigned char *zIdent = (unsigned char*)zSignedIdent;
111909  int i, j, needQuote;
111910  i = *pIdx;
111911
111912  for(j=0; zIdent[j]; j++){
111913    if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
111914  }
111915  needQuote = sqlite3Isdigit(zIdent[0])
111916            || sqlite3KeywordCode(zIdent, j)!=TK_ID
111917            || zIdent[j]!=0
111918            || j==0;
111919
111920  if( needQuote ) z[i++] = '"';
111921  for(j=0; zIdent[j]; j++){
111922    z[i++] = zIdent[j];
111923    if( zIdent[j]=='"' ) z[i++] = '"';
111924  }
111925  if( needQuote ) z[i++] = '"';
111926  z[i] = 0;
111927  *pIdx = i;
111928}
111929
111930/*
111931** Generate a CREATE TABLE statement appropriate for the given
111932** table.  Memory to hold the text of the statement is obtained
111933** from sqliteMalloc() and must be freed by the calling function.
111934*/
111935static char *createTableStmt(sqlite3 *db, Table *p){
111936  int i, k, n;
111937  char *zStmt;
111938  char *zSep, *zSep2, *zEnd;
111939  Column *pCol;
111940  n = 0;
111941  for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
111942    n += identLength(pCol->zName) + 5;
111943  }
111944  n += identLength(p->zName);
111945  if( n<50 ){
111946    zSep = "";
111947    zSep2 = ",";
111948    zEnd = ")";
111949  }else{
111950    zSep = "\n  ";
111951    zSep2 = ",\n  ";
111952    zEnd = "\n)";
111953  }
111954  n += 35 + 6*p->nCol;
111955  zStmt = sqlite3DbMallocRaw(0, n);
111956  if( zStmt==0 ){
111957    sqlite3OomFault(db);
111958    return 0;
111959  }
111960  sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
111961  k = sqlite3Strlen30(zStmt);
111962  identPut(zStmt, &k, p->zName);
111963  zStmt[k++] = '(';
111964  for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
111965    static const char * const azType[] = {
111966        /* SQLITE_AFF_BLOB    */ "",
111967        /* SQLITE_AFF_TEXT    */ " TEXT",
111968        /* SQLITE_AFF_NUMERIC */ " NUM",
111969        /* SQLITE_AFF_INTEGER */ " INT",
111970        /* SQLITE_AFF_REAL    */ " REAL"
111971    };
111972    int len;
111973    const char *zType;
111974
111975    sqlite3_snprintf(n-k, &zStmt[k], zSep);
111976    k += sqlite3Strlen30(&zStmt[k]);
111977    zSep = zSep2;
111978    identPut(zStmt, &k, pCol->zName);
111979    assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
111980    assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
111981    testcase( pCol->affinity==SQLITE_AFF_BLOB );
111982    testcase( pCol->affinity==SQLITE_AFF_TEXT );
111983    testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
111984    testcase( pCol->affinity==SQLITE_AFF_INTEGER );
111985    testcase( pCol->affinity==SQLITE_AFF_REAL );
111986
111987    zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
111988    len = sqlite3Strlen30(zType);
111989    assert( pCol->affinity==SQLITE_AFF_BLOB
111990            || pCol->affinity==sqlite3AffinityType(zType, 0) );
111991    memcpy(&zStmt[k], zType, len);
111992    k += len;
111993    assert( k<=n );
111994  }
111995  sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
111996  return zStmt;
111997}
111998
111999/*
112000** Resize an Index object to hold N columns total.  Return SQLITE_OK
112001** on success and SQLITE_NOMEM on an OOM error.
112002*/
112003static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
112004  char *zExtra;
112005  int nByte;
112006  if( pIdx->nColumn>=N ) return SQLITE_OK;
112007  assert( pIdx->isResized==0 );
112008  nByte = (sizeof(char*) + sizeof(i16) + 1)*N;
112009  zExtra = sqlite3DbMallocZero(db, nByte);
112010  if( zExtra==0 ) return SQLITE_NOMEM_BKPT;
112011  memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
112012  pIdx->azColl = (const char**)zExtra;
112013  zExtra += sizeof(char*)*N;
112014  memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
112015  pIdx->aiColumn = (i16*)zExtra;
112016  zExtra += sizeof(i16)*N;
112017  memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
112018  pIdx->aSortOrder = (u8*)zExtra;
112019  pIdx->nColumn = N;
112020  pIdx->isResized = 1;
112021  return SQLITE_OK;
112022}
112023
112024/*
112025** Estimate the total row width for a table.
112026*/
112027static void estimateTableWidth(Table *pTab){
112028  unsigned wTable = 0;
112029  const Column *pTabCol;
112030  int i;
112031  for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
112032    wTable += pTabCol->szEst;
112033  }
112034  if( pTab->iPKey<0 ) wTable++;
112035  pTab->szTabRow = sqlite3LogEst(wTable*4);
112036}
112037
112038/*
112039** Estimate the average size of a row for an index.
112040*/
112041static void estimateIndexWidth(Index *pIdx){
112042  unsigned wIndex = 0;
112043  int i;
112044  const Column *aCol = pIdx->pTable->aCol;
112045  for(i=0; i<pIdx->nColumn; i++){
112046    i16 x = pIdx->aiColumn[i];
112047    assert( x<pIdx->pTable->nCol );
112048    wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
112049  }
112050  pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
112051}
112052
112053/* Return true if column number x is any of the first nCol entries of aiCol[].
112054** This is used to determine if the column number x appears in any of the
112055** first nCol entries of an index.
112056*/
112057static int hasColumn(const i16 *aiCol, int nCol, int x){
112058  while( nCol-- > 0 ){
112059    assert( aiCol[0]>=0 );
112060    if( x==*(aiCol++) ){
112061      return 1;
112062    }
112063  }
112064  return 0;
112065}
112066
112067/*
112068** Return true if any of the first nKey entries of index pIdx exactly
112069** match the iCol-th entry of pPk.  pPk is always a WITHOUT ROWID
112070** PRIMARY KEY index.  pIdx is an index on the same table.  pIdx may
112071** or may not be the same index as pPk.
112072**
112073** The first nKey entries of pIdx are guaranteed to be ordinary columns,
112074** not a rowid or expression.
112075**
112076** This routine differs from hasColumn() in that both the column and the
112077** collating sequence must match for this routine, but for hasColumn() only
112078** the column name must match.
112079*/
112080static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){
112081  int i, j;
112082  assert( nKey<=pIdx->nColumn );
112083  assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
112084  assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
112085  assert( pPk->pTable->tabFlags & TF_WithoutRowid );
112086  assert( pPk->pTable==pIdx->pTable );
112087  testcase( pPk==pIdx );
112088  j = pPk->aiColumn[iCol];
112089  assert( j!=XN_ROWID && j!=XN_EXPR );
112090  for(i=0; i<nKey; i++){
112091    assert( pIdx->aiColumn[i]>=0 || j>=0 );
112092    if( pIdx->aiColumn[i]==j
112093     && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
112094    ){
112095      return 1;
112096    }
112097  }
112098  return 0;
112099}
112100
112101/* Recompute the colNotIdxed field of the Index.
112102**
112103** colNotIdxed is a bitmask that has a 0 bit representing each indexed
112104** columns that are within the first 63 columns of the table.  The
112105** high-order bit of colNotIdxed is always 1.  All unindexed columns
112106** of the table have a 1.
112107**
112108** 2019-10-24:  For the purpose of this computation, virtual columns are
112109** not considered to be covered by the index, even if they are in the
112110** index, because we do not trust the logic in whereIndexExprTrans() to be
112111** able to find all instances of a reference to the indexed table column
112112** and convert them into references to the index.  Hence we always want
112113** the actual table at hand in order to recompute the virtual column, if
112114** necessary.
112115**
112116** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
112117** to determine if the index is covering index.
112118*/
112119static void recomputeColumnsNotIndexed(Index *pIdx){
112120  Bitmask m = 0;
112121  int j;
112122  Table *pTab = pIdx->pTable;
112123  for(j=pIdx->nColumn-1; j>=0; j--){
112124    int x = pIdx->aiColumn[j];
112125    if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){
112126      testcase( x==BMS-1 );
112127      testcase( x==BMS-2 );
112128      if( x<BMS-1 ) m |= MASKBIT(x);
112129    }
112130  }
112131  pIdx->colNotIdxed = ~m;
112132  assert( (pIdx->colNotIdxed>>63)==1 );
112133}
112134
112135/*
112136** This routine runs at the end of parsing a CREATE TABLE statement that
112137** has a WITHOUT ROWID clause.  The job of this routine is to convert both
112138** internal schema data structures and the generated VDBE code so that they
112139** are appropriate for a WITHOUT ROWID table instead of a rowid table.
112140** Changes include:
112141**
112142**     (1)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
112143**     (2)  Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY
112144**          into BTREE_BLOBKEY.
112145**     (3)  Bypass the creation of the sqlite_master table entry
112146**          for the PRIMARY KEY as the primary key index is now
112147**          identified by the sqlite_master table entry of the table itself.
112148**     (4)  Set the Index.tnum of the PRIMARY KEY Index object in the
112149**          schema to the rootpage from the main table.
112150**     (5)  Add all table columns to the PRIMARY KEY Index object
112151**          so that the PRIMARY KEY is a covering index.  The surplus
112152**          columns are part of KeyInfo.nAllField and are not used for
112153**          sorting or lookup or uniqueness checks.
112154**     (6)  Replace the rowid tail on all automatically generated UNIQUE
112155**          indices with the PRIMARY KEY columns.
112156**
112157** For virtual tables, only (1) is performed.
112158*/
112159static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
112160  Index *pIdx;
112161  Index *pPk;
112162  int nPk;
112163  int nExtra;
112164  int i, j;
112165  sqlite3 *db = pParse->db;
112166  Vdbe *v = pParse->pVdbe;
112167
112168  /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
112169  */
112170  if( !db->init.imposterTable ){
112171    for(i=0; i<pTab->nCol; i++){
112172      if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){
112173        pTab->aCol[i].notNull = OE_Abort;
112174      }
112175    }
112176    pTab->tabFlags |= TF_HasNotNull;
112177  }
112178
112179  /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
112180  ** into BTREE_BLOBKEY.
112181  */
112182  if( pParse->addrCrTab ){
112183    assert( v );
112184    sqlite3VdbeChangeP3(v, pParse->addrCrTab, BTREE_BLOBKEY);
112185  }
112186
112187  /* Locate the PRIMARY KEY index.  Or, if this table was originally
112188  ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
112189  */
112190  if( pTab->iPKey>=0 ){
112191    ExprList *pList;
112192    Token ipkToken;
112193    sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
112194    pList = sqlite3ExprListAppend(pParse, 0,
112195                  sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
112196    if( pList==0 ) return;
112197    if( IN_RENAME_OBJECT ){
112198      sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
112199    }
112200    pList->a[0].sortFlags = pParse->iPkSortOrder;
112201    assert( pParse->pNewTable==pTab );
112202    pTab->iPKey = -1;
112203    sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
112204                       SQLITE_IDXTYPE_PRIMARYKEY);
112205    if( db->mallocFailed || pParse->nErr ) return;
112206    pPk = sqlite3PrimaryKeyIndex(pTab);
112207    assert( pPk->nKeyCol==1 );
112208  }else{
112209    pPk = sqlite3PrimaryKeyIndex(pTab);
112210    assert( pPk!=0 );
112211
112212    /*
112213    ** Remove all redundant columns from the PRIMARY KEY.  For example, change
112214    ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)".  Later
112215    ** code assumes the PRIMARY KEY contains no repeated columns.
112216    */
112217    for(i=j=1; i<pPk->nKeyCol; i++){
112218      if( isDupColumn(pPk, j, pPk, i) ){
112219        pPk->nColumn--;
112220      }else{
112221        testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
112222        pPk->azColl[j] = pPk->azColl[i];
112223        pPk->aSortOrder[j] = pPk->aSortOrder[i];
112224        pPk->aiColumn[j++] = pPk->aiColumn[i];
112225      }
112226    }
112227    pPk->nKeyCol = j;
112228  }
112229  assert( pPk!=0 );
112230  pPk->isCovering = 1;
112231  if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
112232  nPk = pPk->nColumn = pPk->nKeyCol;
112233
112234  /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
112235  ** table entry. This is only required if currently generating VDBE
112236  ** code for a CREATE TABLE (not when parsing one as part of reading
112237  ** a database schema).  */
112238  if( v && pPk->tnum>0 ){
112239    assert( db->init.busy==0 );
112240    sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto);
112241  }
112242
112243  /* The root page of the PRIMARY KEY is the table root page */
112244  pPk->tnum = pTab->tnum;
112245
112246  /* Update the in-memory representation of all UNIQUE indices by converting
112247  ** the final rowid column into one or more columns of the PRIMARY KEY.
112248  */
112249  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
112250    int n;
112251    if( IsPrimaryKeyIndex(pIdx) ) continue;
112252    for(i=n=0; i<nPk; i++){
112253      if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
112254        testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
112255        n++;
112256      }
112257    }
112258    if( n==0 ){
112259      /* This index is a superset of the primary key */
112260      pIdx->nColumn = pIdx->nKeyCol;
112261      continue;
112262    }
112263    if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
112264    for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
112265      if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
112266        testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
112267        pIdx->aiColumn[j] = pPk->aiColumn[i];
112268        pIdx->azColl[j] = pPk->azColl[i];
112269        if( pPk->aSortOrder[i] ){
112270          /* See ticket https://www.sqlite.org/src/info/bba7b69f9849b5bf */
112271          pIdx->bAscKeyBug = 1;
112272        }
112273        j++;
112274      }
112275    }
112276    assert( pIdx->nColumn>=pIdx->nKeyCol+n );
112277    assert( pIdx->nColumn>=j );
112278  }
112279
112280  /* Add all table columns to the PRIMARY KEY index
112281  */
112282  nExtra = 0;
112283  for(i=0; i<pTab->nCol; i++){
112284    if( !hasColumn(pPk->aiColumn, nPk, i)
112285     && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
112286  }
112287  if( resizeIndexObject(db, pPk, nPk+nExtra) ) return;
112288  for(i=0, j=nPk; i<pTab->nCol; i++){
112289    if( !hasColumn(pPk->aiColumn, j, i)
112290     && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
112291    ){
112292      assert( j<pPk->nColumn );
112293      pPk->aiColumn[j] = i;
112294      pPk->azColl[j] = sqlite3StrBINARY;
112295      j++;
112296    }
112297  }
112298  assert( pPk->nColumn==j );
112299  assert( pTab->nNVCol<=j );
112300  recomputeColumnsNotIndexed(pPk);
112301}
112302
112303
112304#ifndef SQLITE_OMIT_VIRTUALTABLE
112305/*
112306** Return true if pTab is a virtual table and zName is a shadow table name
112307** for that virtual table.
112308*/
112309SQLITE_PRIVATE int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){
112310  int nName;                    /* Length of zName */
112311  Module *pMod;                 /* Module for the virtual table */
112312
112313  if( !IsVirtual(pTab) ) return 0;
112314  nName = sqlite3Strlen30(pTab->zName);
112315  if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
112316  if( zName[nName]!='_' ) return 0;
112317  pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]);
112318  if( pMod==0 ) return 0;
112319  if( pMod->pModule->iVersion<3 ) return 0;
112320  if( pMod->pModule->xShadowName==0 ) return 0;
112321  return pMod->pModule->xShadowName(zName+nName+1);
112322}
112323#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
112324
112325#ifndef SQLITE_OMIT_VIRTUALTABLE
112326/*
112327** Return true if zName is a shadow table name in the current database
112328** connection.
112329**
112330** zName is temporarily modified while this routine is running, but is
112331** restored to its original value prior to this routine returning.
112332*/
112333SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName){
112334  char *zTail;                  /* Pointer to the last "_" in zName */
112335  Table *pTab;                  /* Table that zName is a shadow of */
112336  zTail = strrchr(zName, '_');
112337  if( zTail==0 ) return 0;
112338  *zTail = 0;
112339  pTab = sqlite3FindTable(db, zName, 0);
112340  *zTail = '_';
112341  if( pTab==0 ) return 0;
112342  if( !IsVirtual(pTab) ) return 0;
112343  return sqlite3IsShadowTableOf(db, pTab, zName);
112344}
112345#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
112346
112347
112348#ifdef SQLITE_DEBUG
112349/*
112350** Mark all nodes of an expression as EP_Immutable, indicating that
112351** they should not be changed.  Expressions attached to a table or
112352** index definition are tagged this way to help ensure that we do
112353** not pass them into code generator routines by mistake.
112354*/
112355static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){
112356  ExprSetVVAProperty(pExpr, EP_Immutable);
112357  return WRC_Continue;
112358}
112359static void markExprListImmutable(ExprList *pList){
112360  if( pList ){
112361    Walker w;
112362    memset(&w, 0, sizeof(w));
112363    w.xExprCallback = markImmutableExprStep;
112364    w.xSelectCallback = sqlite3SelectWalkNoop;
112365    w.xSelectCallback2 = 0;
112366    sqlite3WalkExprList(&w, pList);
112367  }
112368}
112369#else
112370#define markExprListImmutable(X)  /* no-op */
112371#endif /* SQLITE_DEBUG */
112372
112373
112374/*
112375** This routine is called to report the final ")" that terminates
112376** a CREATE TABLE statement.
112377**
112378** The table structure that other action routines have been building
112379** is added to the internal hash tables, assuming no errors have
112380** occurred.
112381**
112382** An entry for the table is made in the master table on disk, unless
112383** this is a temporary table or db->init.busy==1.  When db->init.busy==1
112384** it means we are reading the sqlite_master table because we just
112385** connected to the database or because the sqlite_master table has
112386** recently changed, so the entry for this table already exists in
112387** the sqlite_master table.  We do not want to create it again.
112388**
112389** If the pSelect argument is not NULL, it means that this routine
112390** was called to create a table generated from a
112391** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
112392** the new table will match the result set of the SELECT.
112393*/
112394SQLITE_PRIVATE void sqlite3EndTable(
112395  Parse *pParse,          /* Parse context */
112396  Token *pCons,           /* The ',' token after the last column defn. */
112397  Token *pEnd,            /* The ')' before options in the CREATE TABLE */
112398  u8 tabOpts,             /* Extra table options. Usually 0. */
112399  Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
112400){
112401  Table *p;                 /* The new table */
112402  sqlite3 *db = pParse->db; /* The database connection */
112403  int iDb;                  /* Database in which the table lives */
112404  Index *pIdx;              /* An implied index of the table */
112405
112406  if( pEnd==0 && pSelect==0 ){
112407    return;
112408  }
112409  assert( !db->mallocFailed );
112410  p = pParse->pNewTable;
112411  if( p==0 ) return;
112412
112413  if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
112414    p->tabFlags |= TF_Shadow;
112415  }
112416
112417  /* If the db->init.busy is 1 it means we are reading the SQL off the
112418  ** "sqlite_master" or "sqlite_temp_master" table on the disk.
112419  ** So do not write to the disk again.  Extract the root page number
112420  ** for the table from the db->init.newTnum field.  (The page number
112421  ** should have been put there by the sqliteOpenCb routine.)
112422  **
112423  ** If the root page number is 1, that means this is the sqlite_master
112424  ** table itself.  So mark it read-only.
112425  */
112426  if( db->init.busy ){
112427    if( pSelect ){
112428      sqlite3ErrorMsg(pParse, "");
112429      return;
112430    }
112431    p->tnum = db->init.newTnum;
112432    if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
112433  }
112434
112435  assert( (p->tabFlags & TF_HasPrimaryKey)==0
112436       || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
112437  assert( (p->tabFlags & TF_HasPrimaryKey)!=0
112438       || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
112439
112440  /* Special processing for WITHOUT ROWID Tables */
112441  if( tabOpts & TF_WithoutRowid ){
112442    if( (p->tabFlags & TF_Autoincrement) ){
112443      sqlite3ErrorMsg(pParse,
112444          "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
112445      return;
112446    }
112447    if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
112448      sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
112449      return;
112450    }
112451    p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
112452    convertToWithoutRowidTable(pParse, p);
112453  }
112454  iDb = sqlite3SchemaToIndex(db, p->pSchema);
112455
112456#ifndef SQLITE_OMIT_CHECK
112457  /* Resolve names in all CHECK constraint expressions.
112458  */
112459  if( p->pCheck ){
112460    sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
112461    if( pParse->nErr ){
112462      /* If errors are seen, delete the CHECK constraints now, else they might
112463      ** actually be used if PRAGMA writable_schema=ON is set. */
112464      sqlite3ExprListDelete(db, p->pCheck);
112465      p->pCheck = 0;
112466    }else{
112467      markExprListImmutable(p->pCheck);
112468    }
112469  }
112470#endif /* !defined(SQLITE_OMIT_CHECK) */
112471#ifndef SQLITE_OMIT_GENERATED_COLUMNS
112472  if( p->tabFlags & TF_HasGenerated ){
112473    int ii, nNG = 0;
112474    testcase( p->tabFlags & TF_HasVirtual );
112475    testcase( p->tabFlags & TF_HasStored );
112476    for(ii=0; ii<p->nCol; ii++){
112477      u32 colFlags = p->aCol[ii].colFlags;
112478      if( (colFlags & COLFLAG_GENERATED)!=0 ){
112479        Expr *pX = p->aCol[ii].pDflt;
112480        testcase( colFlags & COLFLAG_VIRTUAL );
112481        testcase( colFlags & COLFLAG_STORED );
112482        if( sqlite3ResolveSelfReference(pParse, p, NC_GenCol, pX, 0) ){
112483          /* If there are errors in resolving the expression, change the
112484          ** expression to a NULL.  This prevents code generators that operate
112485          ** on the expression from inserting extra parts into the expression
112486          ** tree that have been allocated from lookaside memory, which is
112487          ** illegal in a schema and will lead to errors or heap corruption
112488          ** when the database connection closes. */
112489          sqlite3ExprDelete(db, pX);
112490          p->aCol[ii].pDflt = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
112491        }
112492      }else{
112493        nNG++;
112494      }
112495    }
112496    if( nNG==0 ){
112497      sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
112498      return;
112499    }
112500  }
112501#endif
112502
112503  /* Estimate the average row size for the table and for all implied indices */
112504  estimateTableWidth(p);
112505  for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
112506    estimateIndexWidth(pIdx);
112507  }
112508
112509  /* If not initializing, then create a record for the new table
112510  ** in the SQLITE_MASTER table of the database.
112511  **
112512  ** If this is a TEMPORARY table, write the entry into the auxiliary
112513  ** file instead of into the main database file.
112514  */
112515  if( !db->init.busy ){
112516    int n;
112517    Vdbe *v;
112518    char *zType;    /* "view" or "table" */
112519    char *zType2;   /* "VIEW" or "TABLE" */
112520    char *zStmt;    /* Text of the CREATE TABLE or CREATE VIEW statement */
112521
112522    v = sqlite3GetVdbe(pParse);
112523    if( NEVER(v==0) ) return;
112524
112525    sqlite3VdbeAddOp1(v, OP_Close, 0);
112526
112527    /*
112528    ** Initialize zType for the new view or table.
112529    */
112530    if( p->pSelect==0 ){
112531      /* A regular table */
112532      zType = "table";
112533      zType2 = "TABLE";
112534#ifndef SQLITE_OMIT_VIEW
112535    }else{
112536      /* A view */
112537      zType = "view";
112538      zType2 = "VIEW";
112539#endif
112540    }
112541
112542    /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
112543    ** statement to populate the new table. The root-page number for the
112544    ** new table is in register pParse->regRoot.
112545    **
112546    ** Once the SELECT has been coded by sqlite3Select(), it is in a
112547    ** suitable state to query for the column names and types to be used
112548    ** by the new table.
112549    **
112550    ** A shared-cache write-lock is not required to write to the new table,
112551    ** as a schema-lock must have already been obtained to create it. Since
112552    ** a schema-lock excludes all other database users, the write-lock would
112553    ** be redundant.
112554    */
112555    if( pSelect ){
112556      SelectDest dest;    /* Where the SELECT should store results */
112557      int regYield;       /* Register holding co-routine entry-point */
112558      int addrTop;        /* Top of the co-routine */
112559      int regRec;         /* A record to be insert into the new table */
112560      int regRowid;       /* Rowid of the next row to insert */
112561      int addrInsLoop;    /* Top of the loop for inserting rows */
112562      Table *pSelTab;     /* A table that describes the SELECT results */
112563
112564      regYield = ++pParse->nMem;
112565      regRec = ++pParse->nMem;
112566      regRowid = ++pParse->nMem;
112567      assert(pParse->nTab==1);
112568      sqlite3MayAbort(pParse);
112569      sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
112570      sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
112571      pParse->nTab = 2;
112572      addrTop = sqlite3VdbeCurrentAddr(v) + 1;
112573      sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
112574      if( pParse->nErr ) return;
112575      pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB);
112576      if( pSelTab==0 ) return;
112577      assert( p->aCol==0 );
112578      p->nCol = p->nNVCol = pSelTab->nCol;
112579      p->aCol = pSelTab->aCol;
112580      pSelTab->nCol = 0;
112581      pSelTab->aCol = 0;
112582      sqlite3DeleteTable(db, pSelTab);
112583      sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
112584      sqlite3Select(pParse, pSelect, &dest);
112585      if( pParse->nErr ) return;
112586      sqlite3VdbeEndCoroutine(v, regYield);
112587      sqlite3VdbeJumpHere(v, addrTop - 1);
112588      addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
112589      VdbeCoverage(v);
112590      sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec);
112591      sqlite3TableAffinity(v, p, 0);
112592      sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid);
112593      sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid);
112594      sqlite3VdbeGoto(v, addrInsLoop);
112595      sqlite3VdbeJumpHere(v, addrInsLoop);
112596      sqlite3VdbeAddOp1(v, OP_Close, 1);
112597    }
112598
112599    /* Compute the complete text of the CREATE statement */
112600    if( pSelect ){
112601      zStmt = createTableStmt(db, p);
112602    }else{
112603      Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
112604      n = (int)(pEnd2->z - pParse->sNameToken.z);
112605      if( pEnd2->z[0]!=';' ) n += pEnd2->n;
112606      zStmt = sqlite3MPrintf(db,
112607          "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
112608      );
112609    }
112610
112611    /* A slot for the record has already been allocated in the
112612    ** SQLITE_MASTER table.  We just need to update that slot with all
112613    ** the information we've collected.
112614    */
112615    sqlite3NestedParse(pParse,
112616      "UPDATE %Q.%s "
112617         "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
112618       "WHERE rowid=#%d",
112619      db->aDb[iDb].zDbSName, MASTER_NAME,
112620      zType,
112621      p->zName,
112622      p->zName,
112623      pParse->regRoot,
112624      zStmt,
112625      pParse->regRowid
112626    );
112627    sqlite3DbFree(db, zStmt);
112628    sqlite3ChangeCookie(pParse, iDb);
112629
112630#ifndef SQLITE_OMIT_AUTOINCREMENT
112631    /* Check to see if we need to create an sqlite_sequence table for
112632    ** keeping track of autoincrement keys.
112633    */
112634    if( (p->tabFlags & TF_Autoincrement)!=0 ){
112635      Db *pDb = &db->aDb[iDb];
112636      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
112637      if( pDb->pSchema->pSeqTab==0 ){
112638        sqlite3NestedParse(pParse,
112639          "CREATE TABLE %Q.sqlite_sequence(name,seq)",
112640          pDb->zDbSName
112641        );
112642      }
112643    }
112644#endif
112645
112646    /* Reparse everything to update our internal data structures */
112647    sqlite3VdbeAddParseSchemaOp(v, iDb,
112648           sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName));
112649  }
112650
112651  /* Add the table to the in-memory representation of the database.
112652  */
112653  if( db->init.busy ){
112654    Table *pOld;
112655    Schema *pSchema = p->pSchema;
112656    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
112657    pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
112658    if( pOld ){
112659      assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
112660      sqlite3OomFault(db);
112661      return;
112662    }
112663    pParse->pNewTable = 0;
112664    db->mDbFlags |= DBFLAG_SchemaChange;
112665
112666#ifndef SQLITE_OMIT_ALTERTABLE
112667    if( !p->pSelect ){
112668      const char *zName = (const char *)pParse->sNameToken.z;
112669      int nName;
112670      assert( !pSelect && pCons && pEnd );
112671      if( pCons->z==0 ){
112672        pCons = pEnd;
112673      }
112674      nName = (int)((const char *)pCons->z - zName);
112675      p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
112676    }
112677#endif
112678  }
112679}
112680
112681#ifndef SQLITE_OMIT_VIEW
112682/*
112683** The parser calls this routine in order to create a new VIEW
112684*/
112685SQLITE_PRIVATE void sqlite3CreateView(
112686  Parse *pParse,     /* The parsing context */
112687  Token *pBegin,     /* The CREATE token that begins the statement */
112688  Token *pName1,     /* The token that holds the name of the view */
112689  Token *pName2,     /* The token that holds the name of the view */
112690  ExprList *pCNames, /* Optional list of view column names */
112691  Select *pSelect,   /* A SELECT statement that will become the new view */
112692  int isTemp,        /* TRUE for a TEMPORARY view */
112693  int noErr          /* Suppress error messages if VIEW already exists */
112694){
112695  Table *p;
112696  int n;
112697  const char *z;
112698  Token sEnd;
112699  DbFixer sFix;
112700  Token *pName = 0;
112701  int iDb;
112702  sqlite3 *db = pParse->db;
112703
112704  if( pParse->nVar>0 ){
112705    sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
112706    goto create_view_fail;
112707  }
112708  sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
112709  p = pParse->pNewTable;
112710  if( p==0 || pParse->nErr ) goto create_view_fail;
112711  sqlite3TwoPartName(pParse, pName1, pName2, &pName);
112712  iDb = sqlite3SchemaToIndex(db, p->pSchema);
112713  sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
112714  if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail;
112715
112716  /* Make a copy of the entire SELECT statement that defines the view.
112717  ** This will force all the Expr.token.z values to be dynamically
112718  ** allocated rather than point to the input string - which means that
112719  ** they will persist after the current sqlite3_exec() call returns.
112720  */
112721  pSelect->selFlags |= SF_View;
112722  if( IN_RENAME_OBJECT ){
112723    p->pSelect = pSelect;
112724    pSelect = 0;
112725  }else{
112726    p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
112727  }
112728  p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
112729  if( db->mallocFailed ) goto create_view_fail;
112730
112731  /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
112732  ** the end.
112733  */
112734  sEnd = pParse->sLastToken;
112735  assert( sEnd.z[0]!=0 || sEnd.n==0 );
112736  if( sEnd.z[0]!=';' ){
112737    sEnd.z += sEnd.n;
112738  }
112739  sEnd.n = 0;
112740  n = (int)(sEnd.z - pBegin->z);
112741  assert( n>0 );
112742  z = pBegin->z;
112743  while( sqlite3Isspace(z[n-1]) ){ n--; }
112744  sEnd.z = &z[n-1];
112745  sEnd.n = 1;
112746
112747  /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
112748  sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
112749
112750create_view_fail:
112751  sqlite3SelectDelete(db, pSelect);
112752  if( IN_RENAME_OBJECT ){
112753    sqlite3RenameExprlistUnmap(pParse, pCNames);
112754  }
112755  sqlite3ExprListDelete(db, pCNames);
112756  return;
112757}
112758#endif /* SQLITE_OMIT_VIEW */
112759
112760#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
112761/*
112762** The Table structure pTable is really a VIEW.  Fill in the names of
112763** the columns of the view in the pTable structure.  Return the number
112764** of errors.  If an error is seen leave an error message in pParse->zErrMsg.
112765*/
112766SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
112767  Table *pSelTab;   /* A fake table from which we get the result set */
112768  Select *pSel;     /* Copy of the SELECT that implements the view */
112769  int nErr = 0;     /* Number of errors encountered */
112770  int n;            /* Temporarily holds the number of cursors assigned */
112771  sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
112772#ifndef SQLITE_OMIT_VIRTUALTABLE
112773  int rc;
112774#endif
112775#ifndef SQLITE_OMIT_AUTHORIZATION
112776  sqlite3_xauth xAuth;       /* Saved xAuth pointer */
112777#endif
112778
112779  assert( pTable );
112780
112781#ifndef SQLITE_OMIT_VIRTUALTABLE
112782  db->nSchemaLock++;
112783  rc = sqlite3VtabCallConnect(pParse, pTable);
112784  db->nSchemaLock--;
112785  if( rc ){
112786    return 1;
112787  }
112788  if( IsVirtual(pTable) ) return 0;
112789#endif
112790
112791#ifndef SQLITE_OMIT_VIEW
112792  /* A positive nCol means the columns names for this view are
112793  ** already known.
112794  */
112795  if( pTable->nCol>0 ) return 0;
112796
112797  /* A negative nCol is a special marker meaning that we are currently
112798  ** trying to compute the column names.  If we enter this routine with
112799  ** a negative nCol, it means two or more views form a loop, like this:
112800  **
112801  **     CREATE VIEW one AS SELECT * FROM two;
112802  **     CREATE VIEW two AS SELECT * FROM one;
112803  **
112804  ** Actually, the error above is now caught prior to reaching this point.
112805  ** But the following test is still important as it does come up
112806  ** in the following:
112807  **
112808  **     CREATE TABLE main.ex1(a);
112809  **     CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
112810  **     SELECT * FROM temp.ex1;
112811  */
112812  if( pTable->nCol<0 ){
112813    sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
112814    return 1;
112815  }
112816  assert( pTable->nCol>=0 );
112817
112818  /* If we get this far, it means we need to compute the table names.
112819  ** Note that the call to sqlite3ResultSetOfSelect() will expand any
112820  ** "*" elements in the results set of the view and will assign cursors
112821  ** to the elements of the FROM clause.  But we do not want these changes
112822  ** to be permanent.  So the computation is done on a copy of the SELECT
112823  ** statement that defines the view.
112824  */
112825  assert( pTable->pSelect );
112826  pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
112827  if( pSel ){
112828#ifndef SQLITE_OMIT_ALTERTABLE
112829    u8 eParseMode = pParse->eParseMode;
112830    pParse->eParseMode = PARSE_MODE_NORMAL;
112831#endif
112832    n = pParse->nTab;
112833    sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
112834    pTable->nCol = -1;
112835    DisableLookaside;
112836#ifndef SQLITE_OMIT_AUTHORIZATION
112837    xAuth = db->xAuth;
112838    db->xAuth = 0;
112839    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE);
112840    db->xAuth = xAuth;
112841#else
112842    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE);
112843#endif
112844    pParse->nTab = n;
112845    if( pSelTab==0 ){
112846      pTable->nCol = 0;
112847      nErr++;
112848    }else if( pTable->pCheck ){
112849      /* CREATE VIEW name(arglist) AS ...
112850      ** The names of the columns in the table are taken from
112851      ** arglist which is stored in pTable->pCheck.  The pCheck field
112852      ** normally holds CHECK constraints on an ordinary table, but for
112853      ** a VIEW it holds the list of column names.
112854      */
112855      sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
112856                                 &pTable->nCol, &pTable->aCol);
112857      if( db->mallocFailed==0
112858       && pParse->nErr==0
112859       && pTable->nCol==pSel->pEList->nExpr
112860      ){
112861        sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel,
112862                                               SQLITE_AFF_NONE);
112863      }
112864    }else{
112865      /* CREATE VIEW name AS...  without an argument list.  Construct
112866      ** the column names from the SELECT statement that defines the view.
112867      */
112868      assert( pTable->aCol==0 );
112869      pTable->nCol = pSelTab->nCol;
112870      pTable->aCol = pSelTab->aCol;
112871      pSelTab->nCol = 0;
112872      pSelTab->aCol = 0;
112873      assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
112874    }
112875    pTable->nNVCol = pTable->nCol;
112876    sqlite3DeleteTable(db, pSelTab);
112877    sqlite3SelectDelete(db, pSel);
112878    EnableLookaside;
112879#ifndef SQLITE_OMIT_ALTERTABLE
112880    pParse->eParseMode = eParseMode;
112881#endif
112882  } else {
112883    nErr++;
112884  }
112885  pTable->pSchema->schemaFlags |= DB_UnresetViews;
112886  if( db->mallocFailed ){
112887    sqlite3DeleteColumnNames(db, pTable);
112888    pTable->aCol = 0;
112889    pTable->nCol = 0;
112890  }
112891#endif /* SQLITE_OMIT_VIEW */
112892  return nErr;
112893}
112894#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
112895
112896#ifndef SQLITE_OMIT_VIEW
112897/*
112898** Clear the column names from every VIEW in database idx.
112899*/
112900static void sqliteViewResetAll(sqlite3 *db, int idx){
112901  HashElem *i;
112902  assert( sqlite3SchemaMutexHeld(db, idx, 0) );
112903  if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
112904  for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
112905    Table *pTab = sqliteHashData(i);
112906    if( pTab->pSelect ){
112907      sqlite3DeleteColumnNames(db, pTab);
112908      pTab->aCol = 0;
112909      pTab->nCol = 0;
112910    }
112911  }
112912  DbClearProperty(db, idx, DB_UnresetViews);
112913}
112914#else
112915# define sqliteViewResetAll(A,B)
112916#endif /* SQLITE_OMIT_VIEW */
112917
112918/*
112919** This function is called by the VDBE to adjust the internal schema
112920** used by SQLite when the btree layer moves a table root page. The
112921** root-page of a table or index in database iDb has changed from iFrom
112922** to iTo.
112923**
112924** Ticket #1728:  The symbol table might still contain information
112925** on tables and/or indices that are the process of being deleted.
112926** If you are unlucky, one of those deleted indices or tables might
112927** have the same rootpage number as the real table or index that is
112928** being moved.  So we cannot stop searching after the first match
112929** because the first match might be for one of the deleted indices
112930** or tables and not the table/index that is actually being moved.
112931** We must continue looping until all tables and indices with
112932** rootpage==iFrom have been converted to have a rootpage of iTo
112933** in order to be certain that we got the right one.
112934*/
112935#ifndef SQLITE_OMIT_AUTOVACUUM
112936SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
112937  HashElem *pElem;
112938  Hash *pHash;
112939  Db *pDb;
112940
112941  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
112942  pDb = &db->aDb[iDb];
112943  pHash = &pDb->pSchema->tblHash;
112944  for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
112945    Table *pTab = sqliteHashData(pElem);
112946    if( pTab->tnum==iFrom ){
112947      pTab->tnum = iTo;
112948    }
112949  }
112950  pHash = &pDb->pSchema->idxHash;
112951  for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
112952    Index *pIdx = sqliteHashData(pElem);
112953    if( pIdx->tnum==iFrom ){
112954      pIdx->tnum = iTo;
112955    }
112956  }
112957}
112958#endif
112959
112960/*
112961** Write code to erase the table with root-page iTable from database iDb.
112962** Also write code to modify the sqlite_master table and internal schema
112963** if a root-page of another table is moved by the btree-layer whilst
112964** erasing iTable (this can happen with an auto-vacuum database).
112965*/
112966static void destroyRootPage(Parse *pParse, int iTable, int iDb){
112967  Vdbe *v = sqlite3GetVdbe(pParse);
112968  int r1 = sqlite3GetTempReg(pParse);
112969  if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema");
112970  sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
112971  sqlite3MayAbort(pParse);
112972#ifndef SQLITE_OMIT_AUTOVACUUM
112973  /* OP_Destroy stores an in integer r1. If this integer
112974  ** is non-zero, then it is the root page number of a table moved to
112975  ** location iTable. The following code modifies the sqlite_master table to
112976  ** reflect this.
112977  **
112978  ** The "#NNN" in the SQL is a special constant that means whatever value
112979  ** is in register NNN.  See grammar rules associated with the TK_REGISTER
112980  ** token for additional information.
112981  */
112982  sqlite3NestedParse(pParse,
112983     "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
112984     pParse->db->aDb[iDb].zDbSName, MASTER_NAME, iTable, r1, r1);
112985#endif
112986  sqlite3ReleaseTempReg(pParse, r1);
112987}
112988
112989/*
112990** Write VDBE code to erase table pTab and all associated indices on disk.
112991** Code to update the sqlite_master tables and internal schema definitions
112992** in case a root-page belonging to another table is moved by the btree layer
112993** is also added (this can happen with an auto-vacuum database).
112994*/
112995static void destroyTable(Parse *pParse, Table *pTab){
112996  /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
112997  ** is not defined), then it is important to call OP_Destroy on the
112998  ** table and index root-pages in order, starting with the numerically
112999  ** largest root-page number. This guarantees that none of the root-pages
113000  ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
113001  ** following were coded:
113002  **
113003  ** OP_Destroy 4 0
113004  ** ...
113005  ** OP_Destroy 5 0
113006  **
113007  ** and root page 5 happened to be the largest root-page number in the
113008  ** database, then root page 5 would be moved to page 4 by the
113009  ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
113010  ** a free-list page.
113011  */
113012  int iTab = pTab->tnum;
113013  int iDestroyed = 0;
113014
113015  while( 1 ){
113016    Index *pIdx;
113017    int iLargest = 0;
113018
113019    if( iDestroyed==0 || iTab<iDestroyed ){
113020      iLargest = iTab;
113021    }
113022    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
113023      int iIdx = pIdx->tnum;
113024      assert( pIdx->pSchema==pTab->pSchema );
113025      if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
113026        iLargest = iIdx;
113027      }
113028    }
113029    if( iLargest==0 ){
113030      return;
113031    }else{
113032      int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
113033      assert( iDb>=0 && iDb<pParse->db->nDb );
113034      destroyRootPage(pParse, iLargest, iDb);
113035      iDestroyed = iLargest;
113036    }
113037  }
113038}
113039
113040/*
113041** Remove entries from the sqlite_statN tables (for N in (1,2,3))
113042** after a DROP INDEX or DROP TABLE command.
113043*/
113044static void sqlite3ClearStatTables(
113045  Parse *pParse,         /* The parsing context */
113046  int iDb,               /* The database number */
113047  const char *zType,     /* "idx" or "tbl" */
113048  const char *zName      /* Name of index or table */
113049){
113050  int i;
113051  const char *zDbName = pParse->db->aDb[iDb].zDbSName;
113052  for(i=1; i<=4; i++){
113053    char zTab[24];
113054    sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
113055    if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
113056      sqlite3NestedParse(pParse,
113057        "DELETE FROM %Q.%s WHERE %s=%Q",
113058        zDbName, zTab, zType, zName
113059      );
113060    }
113061  }
113062}
113063
113064/*
113065** Generate code to drop a table.
113066*/
113067SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
113068  Vdbe *v;
113069  sqlite3 *db = pParse->db;
113070  Trigger *pTrigger;
113071  Db *pDb = &db->aDb[iDb];
113072
113073  v = sqlite3GetVdbe(pParse);
113074  assert( v!=0 );
113075  sqlite3BeginWriteOperation(pParse, 1, iDb);
113076
113077#ifndef SQLITE_OMIT_VIRTUALTABLE
113078  if( IsVirtual(pTab) ){
113079    sqlite3VdbeAddOp0(v, OP_VBegin);
113080  }
113081#endif
113082
113083  /* Drop all triggers associated with the table being dropped. Code
113084  ** is generated to remove entries from sqlite_master and/or
113085  ** sqlite_temp_master if required.
113086  */
113087  pTrigger = sqlite3TriggerList(pParse, pTab);
113088  while( pTrigger ){
113089    assert( pTrigger->pSchema==pTab->pSchema ||
113090        pTrigger->pSchema==db->aDb[1].pSchema );
113091    sqlite3DropTriggerPtr(pParse, pTrigger);
113092    pTrigger = pTrigger->pNext;
113093  }
113094
113095#ifndef SQLITE_OMIT_AUTOINCREMENT
113096  /* Remove any entries of the sqlite_sequence table associated with
113097  ** the table being dropped. This is done before the table is dropped
113098  ** at the btree level, in case the sqlite_sequence table needs to
113099  ** move as a result of the drop (can happen in auto-vacuum mode).
113100  */
113101  if( pTab->tabFlags & TF_Autoincrement ){
113102    sqlite3NestedParse(pParse,
113103      "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
113104      pDb->zDbSName, pTab->zName
113105    );
113106  }
113107#endif
113108
113109  /* Drop all SQLITE_MASTER table and index entries that refer to the
113110  ** table. The program name loops through the master table and deletes
113111  ** every row that refers to a table of the same name as the one being
113112  ** dropped. Triggers are handled separately because a trigger can be
113113  ** created in the temp database that refers to a table in another
113114  ** database.
113115  */
113116  sqlite3NestedParse(pParse,
113117      "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
113118      pDb->zDbSName, MASTER_NAME, pTab->zName);
113119  if( !isView && !IsVirtual(pTab) ){
113120    destroyTable(pParse, pTab);
113121  }
113122
113123  /* Remove the table entry from SQLite's internal schema and modify
113124  ** the schema cookie.
113125  */
113126  if( IsVirtual(pTab) ){
113127    sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
113128    sqlite3MayAbort(pParse);
113129  }
113130  sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
113131  sqlite3ChangeCookie(pParse, iDb);
113132  sqliteViewResetAll(db, iDb);
113133}
113134
113135/*
113136** Return TRUE if shadow tables should be read-only in the current
113137** context.
113138*/
113139SQLITE_PRIVATE int sqlite3ReadOnlyShadowTables(sqlite3 *db){
113140#ifndef SQLITE_OMIT_VIRTUALTABLE
113141  if( (db->flags & SQLITE_Defensive)!=0
113142   && db->pVtabCtx==0
113143   && db->nVdbeExec==0
113144  ){
113145    return 1;
113146  }
113147#endif
113148  return 0;
113149}
113150
113151/*
113152** Return true if it is not allowed to drop the given table
113153*/
113154static int tableMayNotBeDropped(sqlite3 *db, Table *pTab){
113155  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
113156    if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0;
113157    if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0;
113158    return 1;
113159  }
113160  if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
113161    return 1;
113162  }
113163  return 0;
113164}
113165
113166/*
113167** This routine is called to do the work of a DROP TABLE statement.
113168** pName is the name of the table to be dropped.
113169*/
113170SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
113171  Table *pTab;
113172  Vdbe *v;
113173  sqlite3 *db = pParse->db;
113174  int iDb;
113175
113176  if( db->mallocFailed ){
113177    goto exit_drop_table;
113178  }
113179  assert( pParse->nErr==0 );
113180  assert( pName->nSrc==1 );
113181  if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
113182  if( noErr ) db->suppressErr++;
113183  assert( isView==0 || isView==LOCATE_VIEW );
113184  pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
113185  if( noErr ) db->suppressErr--;
113186
113187  if( pTab==0 ){
113188    if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
113189    goto exit_drop_table;
113190  }
113191  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
113192  assert( iDb>=0 && iDb<db->nDb );
113193
113194  /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
113195  ** it is initialized.
113196  */
113197  if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
113198    goto exit_drop_table;
113199  }
113200#ifndef SQLITE_OMIT_AUTHORIZATION
113201  {
113202    int code;
113203    const char *zTab = SCHEMA_TABLE(iDb);
113204    const char *zDb = db->aDb[iDb].zDbSName;
113205    const char *zArg2 = 0;
113206    if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
113207      goto exit_drop_table;
113208    }
113209    if( isView ){
113210      if( !OMIT_TEMPDB && iDb==1 ){
113211        code = SQLITE_DROP_TEMP_VIEW;
113212      }else{
113213        code = SQLITE_DROP_VIEW;
113214      }
113215#ifndef SQLITE_OMIT_VIRTUALTABLE
113216    }else if( IsVirtual(pTab) ){
113217      code = SQLITE_DROP_VTABLE;
113218      zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
113219#endif
113220    }else{
113221      if( !OMIT_TEMPDB && iDb==1 ){
113222        code = SQLITE_DROP_TEMP_TABLE;
113223      }else{
113224        code = SQLITE_DROP_TABLE;
113225      }
113226    }
113227    if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
113228      goto exit_drop_table;
113229    }
113230    if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
113231      goto exit_drop_table;
113232    }
113233  }
113234#endif
113235  if( tableMayNotBeDropped(db, pTab) ){
113236    sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
113237    goto exit_drop_table;
113238  }
113239
113240#ifndef SQLITE_OMIT_VIEW
113241  /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
113242  ** on a table.
113243  */
113244  if( isView && pTab->pSelect==0 ){
113245    sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
113246    goto exit_drop_table;
113247  }
113248  if( !isView && pTab->pSelect ){
113249    sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
113250    goto exit_drop_table;
113251  }
113252#endif
113253
113254  /* Generate code to remove the table from the master table
113255  ** on disk.
113256  */
113257  v = sqlite3GetVdbe(pParse);
113258  if( v ){
113259    sqlite3BeginWriteOperation(pParse, 1, iDb);
113260    if( !isView ){
113261      sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
113262      sqlite3FkDropTable(pParse, pName, pTab);
113263    }
113264    sqlite3CodeDropTable(pParse, pTab, iDb, isView);
113265  }
113266
113267exit_drop_table:
113268  sqlite3SrcListDelete(db, pName);
113269}
113270
113271/*
113272** This routine is called to create a new foreign key on the table
113273** currently under construction.  pFromCol determines which columns
113274** in the current table point to the foreign key.  If pFromCol==0 then
113275** connect the key to the last column inserted.  pTo is the name of
113276** the table referred to (a.k.a the "parent" table).  pToCol is a list
113277** of tables in the parent pTo table.  flags contains all
113278** information about the conflict resolution algorithms specified
113279** in the ON DELETE, ON UPDATE and ON INSERT clauses.
113280**
113281** An FKey structure is created and added to the table currently
113282** under construction in the pParse->pNewTable field.
113283**
113284** The foreign key is set for IMMEDIATE processing.  A subsequent call
113285** to sqlite3DeferForeignKey() might change this to DEFERRED.
113286*/
113287SQLITE_PRIVATE void sqlite3CreateForeignKey(
113288  Parse *pParse,       /* Parsing context */
113289  ExprList *pFromCol,  /* Columns in this table that point to other table */
113290  Token *pTo,          /* Name of the other table */
113291  ExprList *pToCol,    /* Columns in the other table */
113292  int flags            /* Conflict resolution algorithms. */
113293){
113294  sqlite3 *db = pParse->db;
113295#ifndef SQLITE_OMIT_FOREIGN_KEY
113296  FKey *pFKey = 0;
113297  FKey *pNextTo;
113298  Table *p = pParse->pNewTable;
113299  int nByte;
113300  int i;
113301  int nCol;
113302  char *z;
113303
113304  assert( pTo!=0 );
113305  if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
113306  if( pFromCol==0 ){
113307    int iCol = p->nCol-1;
113308    if( NEVER(iCol<0) ) goto fk_end;
113309    if( pToCol && pToCol->nExpr!=1 ){
113310      sqlite3ErrorMsg(pParse, "foreign key on %s"
113311         " should reference only one column of table %T",
113312         p->aCol[iCol].zName, pTo);
113313      goto fk_end;
113314    }
113315    nCol = 1;
113316  }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
113317    sqlite3ErrorMsg(pParse,
113318        "number of columns in foreign key does not match the number of "
113319        "columns in the referenced table");
113320    goto fk_end;
113321  }else{
113322    nCol = pFromCol->nExpr;
113323  }
113324  nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
113325  if( pToCol ){
113326    for(i=0; i<pToCol->nExpr; i++){
113327      nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
113328    }
113329  }
113330  pFKey = sqlite3DbMallocZero(db, nByte );
113331  if( pFKey==0 ){
113332    goto fk_end;
113333  }
113334  pFKey->pFrom = p;
113335  pFKey->pNextFrom = p->pFKey;
113336  z = (char*)&pFKey->aCol[nCol];
113337  pFKey->zTo = z;
113338  if( IN_RENAME_OBJECT ){
113339    sqlite3RenameTokenMap(pParse, (void*)z, pTo);
113340  }
113341  memcpy(z, pTo->z, pTo->n);
113342  z[pTo->n] = 0;
113343  sqlite3Dequote(z);
113344  z += pTo->n+1;
113345  pFKey->nCol = nCol;
113346  if( pFromCol==0 ){
113347    pFKey->aCol[0].iFrom = p->nCol-1;
113348  }else{
113349    for(i=0; i<nCol; i++){
113350      int j;
113351      for(j=0; j<p->nCol; j++){
113352        if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zEName)==0 ){
113353          pFKey->aCol[i].iFrom = j;
113354          break;
113355        }
113356      }
113357      if( j>=p->nCol ){
113358        sqlite3ErrorMsg(pParse,
113359          "unknown column \"%s\" in foreign key definition",
113360          pFromCol->a[i].zEName);
113361        goto fk_end;
113362      }
113363      if( IN_RENAME_OBJECT ){
113364        sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
113365      }
113366    }
113367  }
113368  if( pToCol ){
113369    for(i=0; i<nCol; i++){
113370      int n = sqlite3Strlen30(pToCol->a[i].zEName);
113371      pFKey->aCol[i].zCol = z;
113372      if( IN_RENAME_OBJECT ){
113373        sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
113374      }
113375      memcpy(z, pToCol->a[i].zEName, n);
113376      z[n] = 0;
113377      z += n+1;
113378    }
113379  }
113380  pFKey->isDeferred = 0;
113381  pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
113382  pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
113383
113384  assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
113385  pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
113386      pFKey->zTo, (void *)pFKey
113387  );
113388  if( pNextTo==pFKey ){
113389    sqlite3OomFault(db);
113390    goto fk_end;
113391  }
113392  if( pNextTo ){
113393    assert( pNextTo->pPrevTo==0 );
113394    pFKey->pNextTo = pNextTo;
113395    pNextTo->pPrevTo = pFKey;
113396  }
113397
113398  /* Link the foreign key to the table as the last step.
113399  */
113400  p->pFKey = pFKey;
113401  pFKey = 0;
113402
113403fk_end:
113404  sqlite3DbFree(db, pFKey);
113405#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
113406  sqlite3ExprListDelete(db, pFromCol);
113407  sqlite3ExprListDelete(db, pToCol);
113408}
113409
113410/*
113411** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
113412** clause is seen as part of a foreign key definition.  The isDeferred
113413** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
113414** The behavior of the most recently created foreign key is adjusted
113415** accordingly.
113416*/
113417SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
113418#ifndef SQLITE_OMIT_FOREIGN_KEY
113419  Table *pTab;
113420  FKey *pFKey;
113421  if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
113422  assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
113423  pFKey->isDeferred = (u8)isDeferred;
113424#endif
113425}
113426
113427/*
113428** Generate code that will erase and refill index *pIdx.  This is
113429** used to initialize a newly created index or to recompute the
113430** content of an index in response to a REINDEX command.
113431**
113432** if memRootPage is not negative, it means that the index is newly
113433** created.  The register specified by memRootPage contains the
113434** root page number of the index.  If memRootPage is negative, then
113435** the index already exists and must be cleared before being refilled and
113436** the root page number of the index is taken from pIndex->tnum.
113437*/
113438static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
113439  Table *pTab = pIndex->pTable;  /* The table that is indexed */
113440  int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
113441  int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
113442  int iSorter;                   /* Cursor opened by OpenSorter (if in use) */
113443  int addr1;                     /* Address of top of loop */
113444  int addr2;                     /* Address to jump to for next iteration */
113445  int tnum;                      /* Root page of index */
113446  int iPartIdxLabel;             /* Jump to this label to skip a row */
113447  Vdbe *v;                       /* Generate code into this virtual machine */
113448  KeyInfo *pKey;                 /* KeyInfo for index */
113449  int regRecord;                 /* Register holding assembled index record */
113450  sqlite3 *db = pParse->db;      /* The database connection */
113451  int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
113452
113453#ifndef SQLITE_OMIT_AUTHORIZATION
113454  if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
113455      db->aDb[iDb].zDbSName ) ){
113456    return;
113457  }
113458#endif
113459
113460  /* Require a write-lock on the table to perform this operation */
113461  sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
113462
113463  v = sqlite3GetVdbe(pParse);
113464  if( v==0 ) return;
113465  if( memRootPage>=0 ){
113466    tnum = memRootPage;
113467  }else{
113468    tnum = pIndex->tnum;
113469  }
113470  pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
113471  assert( pKey!=0 || db->mallocFailed || pParse->nErr );
113472
113473  /* Open the sorter cursor if we are to use one. */
113474  iSorter = pParse->nTab++;
113475  sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
113476                    sqlite3KeyInfoRef(pKey), P4_KEYINFO);
113477
113478  /* Open the table. Loop through all rows of the table, inserting index
113479  ** records into the sorter. */
113480  sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
113481  addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
113482  regRecord = sqlite3GetTempReg(pParse);
113483  sqlite3MultiWrite(pParse);
113484
113485  sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
113486  sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
113487  sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
113488  sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
113489  sqlite3VdbeJumpHere(v, addr1);
113490  if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
113491  sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
113492                    (char *)pKey, P4_KEYINFO);
113493  sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
113494
113495  addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
113496  if( IsUniqueIndex(pIndex) ){
113497    int j2 = sqlite3VdbeGoto(v, 1);
113498    addr2 = sqlite3VdbeCurrentAddr(v);
113499    sqlite3VdbeVerifyAbortable(v, OE_Abort);
113500    sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
113501                         pIndex->nKeyCol); VdbeCoverage(v);
113502    sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
113503    sqlite3VdbeJumpHere(v, j2);
113504  }else{
113505    /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not
113506    ** abort. The exception is if one of the indexed expressions contains a
113507    ** user function that throws an exception when it is evaluated. But the
113508    ** overhead of adding a statement journal to a CREATE INDEX statement is
113509    ** very small (since most of the pages written do not contain content that
113510    ** needs to be restored if the statement aborts), so we call
113511    ** sqlite3MayAbort() for all CREATE INDEX statements.  */
113512    sqlite3MayAbort(pParse);
113513    addr2 = sqlite3VdbeCurrentAddr(v);
113514  }
113515  sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
113516  if( !pIndex->bAscKeyBug ){
113517    /* This OP_SeekEnd opcode makes index insert for a REINDEX go much
113518    ** faster by avoiding unnecessary seeks.  But the optimization does
113519    ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables
113520    ** with DESC primary keys, since those indexes have there keys in
113521    ** a different order from the main table.
113522    ** See ticket: https://www.sqlite.org/src/info/bba7b69f9849b5bf
113523    */
113524    sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
113525  }
113526  sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
113527  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
113528  sqlite3ReleaseTempReg(pParse, regRecord);
113529  sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
113530  sqlite3VdbeJumpHere(v, addr1);
113531
113532  sqlite3VdbeAddOp1(v, OP_Close, iTab);
113533  sqlite3VdbeAddOp1(v, OP_Close, iIdx);
113534  sqlite3VdbeAddOp1(v, OP_Close, iSorter);
113535}
113536
113537/*
113538** Allocate heap space to hold an Index object with nCol columns.
113539**
113540** Increase the allocation size to provide an extra nExtra bytes
113541** of 8-byte aligned space after the Index object and return a
113542** pointer to this extra space in *ppExtra.
113543*/
113544SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
113545  sqlite3 *db,         /* Database connection */
113546  i16 nCol,            /* Total number of columns in the index */
113547  int nExtra,          /* Number of bytes of extra space to alloc */
113548  char **ppExtra       /* Pointer to the "extra" space */
113549){
113550  Index *p;            /* Allocated index object */
113551  int nByte;           /* Bytes of space for Index object + arrays */
113552
113553  nByte = ROUND8(sizeof(Index)) +              /* Index structure  */
113554          ROUND8(sizeof(char*)*nCol) +         /* Index.azColl     */
113555          ROUND8(sizeof(LogEst)*(nCol+1) +     /* Index.aiRowLogEst   */
113556                 sizeof(i16)*nCol +            /* Index.aiColumn   */
113557                 sizeof(u8)*nCol);             /* Index.aSortOrder */
113558  p = sqlite3DbMallocZero(db, nByte + nExtra);
113559  if( p ){
113560    char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
113561    p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
113562    p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
113563    p->aiColumn = (i16*)pExtra;       pExtra += sizeof(i16)*nCol;
113564    p->aSortOrder = (u8*)pExtra;
113565    p->nColumn = nCol;
113566    p->nKeyCol = nCol - 1;
113567    *ppExtra = ((char*)p) + nByte;
113568  }
113569  return p;
113570}
113571
113572/*
113573** If expression list pList contains an expression that was parsed with
113574** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in
113575** pParse and return non-zero. Otherwise, return zero.
113576*/
113577SQLITE_PRIVATE int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){
113578  if( pList ){
113579    int i;
113580    for(i=0; i<pList->nExpr; i++){
113581      if( pList->a[i].bNulls ){
113582        u8 sf = pList->a[i].sortFlags;
113583        sqlite3ErrorMsg(pParse, "unsupported use of NULLS %s",
113584            (sf==0 || sf==3) ? "FIRST" : "LAST"
113585        );
113586        return 1;
113587      }
113588    }
113589  }
113590  return 0;
113591}
113592
113593/*
113594** Create a new index for an SQL table.  pName1.pName2 is the name of the index
113595** and pTblList is the name of the table that is to be indexed.  Both will
113596** be NULL for a primary key or an index that is created to satisfy a
113597** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
113598** as the table to be indexed.  pParse->pNewTable is a table that is
113599** currently being constructed by a CREATE TABLE statement.
113600**
113601** pList is a list of columns to be indexed.  pList will be NULL if this
113602** is a primary key or unique-constraint on the most recent column added
113603** to the table currently under construction.
113604*/
113605SQLITE_PRIVATE void sqlite3CreateIndex(
113606  Parse *pParse,     /* All information about this parse */
113607  Token *pName1,     /* First part of index name. May be NULL */
113608  Token *pName2,     /* Second part of index name. May be NULL */
113609  SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
113610  ExprList *pList,   /* A list of columns to be indexed */
113611  int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
113612  Token *pStart,     /* The CREATE token that begins this statement */
113613  Expr *pPIWhere,    /* WHERE clause for partial indices */
113614  int sortOrder,     /* Sort order of primary key when pList==NULL */
113615  int ifNotExist,    /* Omit error if index already exists */
113616  u8 idxType         /* The index type */
113617){
113618  Table *pTab = 0;     /* Table to be indexed */
113619  Index *pIndex = 0;   /* The index to be created */
113620  char *zName = 0;     /* Name of the index */
113621  int nName;           /* Number of characters in zName */
113622  int i, j;
113623  DbFixer sFix;        /* For assigning database names to pTable */
113624  int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
113625  sqlite3 *db = pParse->db;
113626  Db *pDb;             /* The specific table containing the indexed database */
113627  int iDb;             /* Index of the database that is being written */
113628  Token *pName = 0;    /* Unqualified name of the index to create */
113629  struct ExprList_item *pListItem; /* For looping over pList */
113630  int nExtra = 0;                  /* Space allocated for zExtra[] */
113631  int nExtraCol;                   /* Number of extra columns needed */
113632  char *zExtra = 0;                /* Extra space after the Index object */
113633  Index *pPk = 0;      /* PRIMARY KEY index for WITHOUT ROWID tables */
113634
113635  if( db->mallocFailed || pParse->nErr>0 ){
113636    goto exit_create_index;
113637  }
113638  if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){
113639    goto exit_create_index;
113640  }
113641  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
113642    goto exit_create_index;
113643  }
113644  if( sqlite3HasExplicitNulls(pParse, pList) ){
113645    goto exit_create_index;
113646  }
113647
113648  /*
113649  ** Find the table that is to be indexed.  Return early if not found.
113650  */
113651  if( pTblName!=0 ){
113652
113653    /* Use the two-part index name to determine the database
113654    ** to search for the table. 'Fix' the table name to this db
113655    ** before looking up the table.
113656    */
113657    assert( pName1 && pName2 );
113658    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
113659    if( iDb<0 ) goto exit_create_index;
113660    assert( pName && pName->z );
113661
113662#ifndef SQLITE_OMIT_TEMPDB
113663    /* If the index name was unqualified, check if the table
113664    ** is a temp table. If so, set the database to 1. Do not do this
113665    ** if initialising a database schema.
113666    */
113667    if( !db->init.busy ){
113668      pTab = sqlite3SrcListLookup(pParse, pTblName);
113669      if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
113670        iDb = 1;
113671      }
113672    }
113673#endif
113674
113675    sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
113676    if( sqlite3FixSrcList(&sFix, pTblName) ){
113677      /* Because the parser constructs pTblName from a single identifier,
113678      ** sqlite3FixSrcList can never fail. */
113679      assert(0);
113680    }
113681    pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
113682    assert( db->mallocFailed==0 || pTab==0 );
113683    if( pTab==0 ) goto exit_create_index;
113684    if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
113685      sqlite3ErrorMsg(pParse,
113686           "cannot create a TEMP index on non-TEMP table \"%s\"",
113687           pTab->zName);
113688      goto exit_create_index;
113689    }
113690    if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab);
113691  }else{
113692    assert( pName==0 );
113693    assert( pStart==0 );
113694    pTab = pParse->pNewTable;
113695    if( !pTab ) goto exit_create_index;
113696    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
113697  }
113698  pDb = &db->aDb[iDb];
113699
113700  assert( pTab!=0 );
113701  assert( pParse->nErr==0 );
113702  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
113703       && db->init.busy==0
113704       && pTblName!=0
113705#if SQLITE_USER_AUTHENTICATION
113706       && sqlite3UserAuthTable(pTab->zName)==0
113707#endif
113708#ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX
113709       && sqlite3StrICmp(&pTab->zName[7],"master")!=0
113710#endif
113711 ){
113712    sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
113713    goto exit_create_index;
113714  }
113715#ifndef SQLITE_OMIT_VIEW
113716  if( pTab->pSelect ){
113717    sqlite3ErrorMsg(pParse, "views may not be indexed");
113718    goto exit_create_index;
113719  }
113720#endif
113721#ifndef SQLITE_OMIT_VIRTUALTABLE
113722  if( IsVirtual(pTab) ){
113723    sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
113724    goto exit_create_index;
113725  }
113726#endif
113727
113728  /*
113729  ** Find the name of the index.  Make sure there is not already another
113730  ** index or table with the same name.
113731  **
113732  ** Exception:  If we are reading the names of permanent indices from the
113733  ** sqlite_master table (because some other process changed the schema) and
113734  ** one of the index names collides with the name of a temporary table or
113735  ** index, then we will continue to process this index.
113736  **
113737  ** If pName==0 it means that we are
113738  ** dealing with a primary key or UNIQUE constraint.  We have to invent our
113739  ** own name.
113740  */
113741  if( pName ){
113742    zName = sqlite3NameFromToken(db, pName);
113743    if( zName==0 ) goto exit_create_index;
113744    assert( pName->z!=0 );
113745    if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
113746      goto exit_create_index;
113747    }
113748    if( !IN_RENAME_OBJECT ){
113749      if( !db->init.busy ){
113750        if( sqlite3FindTable(db, zName, 0)!=0 ){
113751          sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
113752          goto exit_create_index;
113753        }
113754      }
113755      if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
113756        if( !ifNotExist ){
113757          sqlite3ErrorMsg(pParse, "index %s already exists", zName);
113758        }else{
113759          assert( !db->init.busy );
113760          sqlite3CodeVerifySchema(pParse, iDb);
113761        }
113762        goto exit_create_index;
113763      }
113764    }
113765  }else{
113766    int n;
113767    Index *pLoop;
113768    for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
113769    zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
113770    if( zName==0 ){
113771      goto exit_create_index;
113772    }
113773
113774    /* Automatic index names generated from within sqlite3_declare_vtab()
113775    ** must have names that are distinct from normal automatic index names.
113776    ** The following statement converts "sqlite3_autoindex..." into
113777    ** "sqlite3_butoindex..." in order to make the names distinct.
113778    ** The "vtab_err.test" test demonstrates the need of this statement. */
113779    if( IN_SPECIAL_PARSE ) zName[7]++;
113780  }
113781
113782  /* Check for authorization to create an index.
113783  */
113784#ifndef SQLITE_OMIT_AUTHORIZATION
113785  if( !IN_RENAME_OBJECT ){
113786    const char *zDb = pDb->zDbSName;
113787    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
113788      goto exit_create_index;
113789    }
113790    i = SQLITE_CREATE_INDEX;
113791    if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
113792    if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
113793      goto exit_create_index;
113794    }
113795  }
113796#endif
113797
113798  /* If pList==0, it means this routine was called to make a primary
113799  ** key out of the last column added to the table under construction.
113800  ** So create a fake list to simulate this.
113801  */
113802  if( pList==0 ){
113803    Token prevCol;
113804    Column *pCol = &pTab->aCol[pTab->nCol-1];
113805    pCol->colFlags |= COLFLAG_UNIQUE;
113806    sqlite3TokenInit(&prevCol, pCol->zName);
113807    pList = sqlite3ExprListAppend(pParse, 0,
113808              sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
113809    if( pList==0 ) goto exit_create_index;
113810    assert( pList->nExpr==1 );
113811    sqlite3ExprListSetSortOrder(pList, sortOrder, SQLITE_SO_UNDEFINED);
113812  }else{
113813    sqlite3ExprListCheckLength(pParse, pList, "index");
113814    if( pParse->nErr ) goto exit_create_index;
113815  }
113816
113817  /* Figure out how many bytes of space are required to store explicitly
113818  ** specified collation sequence names.
113819  */
113820  for(i=0; i<pList->nExpr; i++){
113821    Expr *pExpr = pList->a[i].pExpr;
113822    assert( pExpr!=0 );
113823    if( pExpr->op==TK_COLLATE ){
113824      nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
113825    }
113826  }
113827
113828  /*
113829  ** Allocate the index structure.
113830  */
113831  nName = sqlite3Strlen30(zName);
113832  nExtraCol = pPk ? pPk->nKeyCol : 1;
113833  assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
113834  pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
113835                                      nName + nExtra + 1, &zExtra);
113836  if( db->mallocFailed ){
113837    goto exit_create_index;
113838  }
113839  assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
113840  assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
113841  pIndex->zName = zExtra;
113842  zExtra += nName + 1;
113843  memcpy(pIndex->zName, zName, nName+1);
113844  pIndex->pTable = pTab;
113845  pIndex->onError = (u8)onError;
113846  pIndex->uniqNotNull = onError!=OE_None;
113847  pIndex->idxType = idxType;
113848  pIndex->pSchema = db->aDb[iDb].pSchema;
113849  pIndex->nKeyCol = pList->nExpr;
113850  if( pPIWhere ){
113851    sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
113852    pIndex->pPartIdxWhere = pPIWhere;
113853    pPIWhere = 0;
113854  }
113855  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
113856
113857  /* Check to see if we should honor DESC requests on index columns
113858  */
113859  if( pDb->pSchema->file_format>=4 ){
113860    sortOrderMask = -1;   /* Honor DESC */
113861  }else{
113862    sortOrderMask = 0;    /* Ignore DESC */
113863  }
113864
113865  /* Analyze the list of expressions that form the terms of the index and
113866  ** report any errors.  In the common case where the expression is exactly
113867  ** a table column, store that column in aiColumn[].  For general expressions,
113868  ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
113869  **
113870  ** TODO: Issue a warning if two or more columns of the index are identical.
113871  ** TODO: Issue a warning if the table primary key is used as part of the
113872  ** index key.
113873  */
113874  pListItem = pList->a;
113875  if( IN_RENAME_OBJECT ){
113876    pIndex->aColExpr = pList;
113877    pList = 0;
113878  }
113879  for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
113880    Expr *pCExpr;                  /* The i-th index expression */
113881    int requestedSortOrder;        /* ASC or DESC on the i-th expression */
113882    const char *zColl;             /* Collation sequence name */
113883
113884    sqlite3StringToId(pListItem->pExpr);
113885    sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
113886    if( pParse->nErr ) goto exit_create_index;
113887    pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
113888    if( pCExpr->op!=TK_COLUMN ){
113889      if( pTab==pParse->pNewTable ){
113890        sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and "
113891                                "UNIQUE constraints");
113892        goto exit_create_index;
113893      }
113894      if( pIndex->aColExpr==0 ){
113895        pIndex->aColExpr = pList;
113896        pList = 0;
113897      }
113898      j = XN_EXPR;
113899      pIndex->aiColumn[i] = XN_EXPR;
113900      pIndex->uniqNotNull = 0;
113901    }else{
113902      j = pCExpr->iColumn;
113903      assert( j<=0x7fff );
113904      if( j<0 ){
113905        j = pTab->iPKey;
113906      }else{
113907        if( pTab->aCol[j].notNull==0 ){
113908          pIndex->uniqNotNull = 0;
113909        }
113910        if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
113911          pIndex->bHasVCol = 1;
113912        }
113913      }
113914      pIndex->aiColumn[i] = (i16)j;
113915    }
113916    zColl = 0;
113917    if( pListItem->pExpr->op==TK_COLLATE ){
113918      int nColl;
113919      zColl = pListItem->pExpr->u.zToken;
113920      nColl = sqlite3Strlen30(zColl) + 1;
113921      assert( nExtra>=nColl );
113922      memcpy(zExtra, zColl, nColl);
113923      zColl = zExtra;
113924      zExtra += nColl;
113925      nExtra -= nColl;
113926    }else if( j>=0 ){
113927      zColl = pTab->aCol[j].zColl;
113928    }
113929    if( !zColl ) zColl = sqlite3StrBINARY;
113930    if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
113931      goto exit_create_index;
113932    }
113933    pIndex->azColl[i] = zColl;
113934    requestedSortOrder = pListItem->sortFlags & sortOrderMask;
113935    pIndex->aSortOrder[i] = (u8)requestedSortOrder;
113936  }
113937
113938  /* Append the table key to the end of the index.  For WITHOUT ROWID
113939  ** tables (when pPk!=0) this will be the declared PRIMARY KEY.  For
113940  ** normal tables (when pPk==0) this will be the rowid.
113941  */
113942  if( pPk ){
113943    for(j=0; j<pPk->nKeyCol; j++){
113944      int x = pPk->aiColumn[j];
113945      assert( x>=0 );
113946      if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
113947        pIndex->nColumn--;
113948      }else{
113949        testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
113950        pIndex->aiColumn[i] = x;
113951        pIndex->azColl[i] = pPk->azColl[j];
113952        pIndex->aSortOrder[i] = pPk->aSortOrder[j];
113953        i++;
113954      }
113955    }
113956    assert( i==pIndex->nColumn );
113957  }else{
113958    pIndex->aiColumn[i] = XN_ROWID;
113959    pIndex->azColl[i] = sqlite3StrBINARY;
113960  }
113961  sqlite3DefaultRowEst(pIndex);
113962  if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
113963
113964  /* If this index contains every column of its table, then mark
113965  ** it as a covering index */
113966  assert( HasRowid(pTab)
113967      || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
113968  recomputeColumnsNotIndexed(pIndex);
113969  if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
113970    pIndex->isCovering = 1;
113971    for(j=0; j<pTab->nCol; j++){
113972      if( j==pTab->iPKey ) continue;
113973      if( sqlite3TableColumnToIndex(pIndex,j)>=0 ) continue;
113974      pIndex->isCovering = 0;
113975      break;
113976    }
113977  }
113978
113979  if( pTab==pParse->pNewTable ){
113980    /* This routine has been called to create an automatic index as a
113981    ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
113982    ** a PRIMARY KEY or UNIQUE clause following the column definitions.
113983    ** i.e. one of:
113984    **
113985    ** CREATE TABLE t(x PRIMARY KEY, y);
113986    ** CREATE TABLE t(x, y, UNIQUE(x, y));
113987    **
113988    ** Either way, check to see if the table already has such an index. If
113989    ** so, don't bother creating this one. This only applies to
113990    ** automatically created indices. Users can do as they wish with
113991    ** explicit indices.
113992    **
113993    ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
113994    ** (and thus suppressing the second one) even if they have different
113995    ** sort orders.
113996    **
113997    ** If there are different collating sequences or if the columns of
113998    ** the constraint occur in different orders, then the constraints are
113999    ** considered distinct and both result in separate indices.
114000    */
114001    Index *pIdx;
114002    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
114003      int k;
114004      assert( IsUniqueIndex(pIdx) );
114005      assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
114006      assert( IsUniqueIndex(pIndex) );
114007
114008      if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
114009      for(k=0; k<pIdx->nKeyCol; k++){
114010        const char *z1;
114011        const char *z2;
114012        assert( pIdx->aiColumn[k]>=0 );
114013        if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
114014        z1 = pIdx->azColl[k];
114015        z2 = pIndex->azColl[k];
114016        if( sqlite3StrICmp(z1, z2) ) break;
114017      }
114018      if( k==pIdx->nKeyCol ){
114019        if( pIdx->onError!=pIndex->onError ){
114020          /* This constraint creates the same index as a previous
114021          ** constraint specified somewhere in the CREATE TABLE statement.
114022          ** However the ON CONFLICT clauses are different. If both this
114023          ** constraint and the previous equivalent constraint have explicit
114024          ** ON CONFLICT clauses this is an error. Otherwise, use the
114025          ** explicitly specified behavior for the index.
114026          */
114027          if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
114028            sqlite3ErrorMsg(pParse,
114029                "conflicting ON CONFLICT clauses specified", 0);
114030          }
114031          if( pIdx->onError==OE_Default ){
114032            pIdx->onError = pIndex->onError;
114033          }
114034        }
114035        if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
114036        if( IN_RENAME_OBJECT ){
114037          pIndex->pNext = pParse->pNewIndex;
114038          pParse->pNewIndex = pIndex;
114039          pIndex = 0;
114040        }
114041        goto exit_create_index;
114042      }
114043    }
114044  }
114045
114046  if( !IN_RENAME_OBJECT ){
114047
114048    /* Link the new Index structure to its table and to the other
114049    ** in-memory database structures.
114050    */
114051    assert( pParse->nErr==0 );
114052    if( db->init.busy ){
114053      Index *p;
114054      assert( !IN_SPECIAL_PARSE );
114055      assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
114056      if( pTblName!=0 ){
114057        pIndex->tnum = db->init.newTnum;
114058        if( sqlite3IndexHasDuplicateRootPage(pIndex) ){
114059          sqlite3ErrorMsg(pParse, "invalid rootpage");
114060          pParse->rc = SQLITE_CORRUPT_BKPT;
114061          goto exit_create_index;
114062        }
114063      }
114064      p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
114065          pIndex->zName, pIndex);
114066      if( p ){
114067        assert( p==pIndex );  /* Malloc must have failed */
114068        sqlite3OomFault(db);
114069        goto exit_create_index;
114070      }
114071      db->mDbFlags |= DBFLAG_SchemaChange;
114072    }
114073
114074    /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
114075    ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
114076    ** emit code to allocate the index rootpage on disk and make an entry for
114077    ** the index in the sqlite_master table and populate the index with
114078    ** content.  But, do not do this if we are simply reading the sqlite_master
114079    ** table to parse the schema, or if this index is the PRIMARY KEY index
114080    ** of a WITHOUT ROWID table.
114081    **
114082    ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
114083    ** or UNIQUE index in a CREATE TABLE statement.  Since the table
114084    ** has just been created, it contains no data and the index initialization
114085    ** step can be skipped.
114086    */
114087    else if( HasRowid(pTab) || pTblName!=0 ){
114088      Vdbe *v;
114089      char *zStmt;
114090      int iMem = ++pParse->nMem;
114091
114092      v = sqlite3GetVdbe(pParse);
114093      if( v==0 ) goto exit_create_index;
114094
114095      sqlite3BeginWriteOperation(pParse, 1, iDb);
114096
114097      /* Create the rootpage for the index using CreateIndex. But before
114098      ** doing so, code a Noop instruction and store its address in
114099      ** Index.tnum. This is required in case this index is actually a
114100      ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
114101      ** that case the convertToWithoutRowidTable() routine will replace
114102      ** the Noop with a Goto to jump over the VDBE code generated below. */
114103      pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
114104      sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY);
114105
114106      /* Gather the complete text of the CREATE INDEX statement into
114107      ** the zStmt variable
114108      */
114109      assert( pName!=0 || pStart==0 );
114110      if( pStart ){
114111        int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
114112        if( pName->z[n-1]==';' ) n--;
114113        /* A named index with an explicit CREATE INDEX statement */
114114        zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
114115            onError==OE_None ? "" : " UNIQUE", n, pName->z);
114116      }else{
114117        /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
114118        /* zStmt = sqlite3MPrintf(""); */
114119        zStmt = 0;
114120      }
114121
114122      /* Add an entry in sqlite_master for this index
114123      */
114124      sqlite3NestedParse(pParse,
114125          "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
114126          db->aDb[iDb].zDbSName, MASTER_NAME,
114127          pIndex->zName,
114128          pTab->zName,
114129          iMem,
114130          zStmt
114131          );
114132      sqlite3DbFree(db, zStmt);
114133
114134      /* Fill the index with data and reparse the schema. Code an OP_Expire
114135      ** to invalidate all pre-compiled statements.
114136      */
114137      if( pTblName ){
114138        sqlite3RefillIndex(pParse, pIndex, iMem);
114139        sqlite3ChangeCookie(pParse, iDb);
114140        sqlite3VdbeAddParseSchemaOp(v, iDb,
114141            sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
114142        sqlite3VdbeAddOp2(v, OP_Expire, 0, 1);
114143      }
114144
114145      sqlite3VdbeJumpHere(v, pIndex->tnum);
114146    }
114147  }
114148  if( db->init.busy || pTblName==0 ){
114149    pIndex->pNext = pTab->pIndex;
114150    pTab->pIndex = pIndex;
114151    pIndex = 0;
114152  }
114153  else if( IN_RENAME_OBJECT ){
114154    assert( pParse->pNewIndex==0 );
114155    pParse->pNewIndex = pIndex;
114156    pIndex = 0;
114157  }
114158
114159  /* Clean up before exiting */
114160exit_create_index:
114161  if( pIndex ) sqlite3FreeIndex(db, pIndex);
114162  if( pTab ){  /* Ensure all REPLACE indexes are at the end of the list */
114163    Index **ppFrom = &pTab->pIndex;
114164    Index *pThis;
114165    for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
114166      Index *pNext;
114167      if( pThis->onError!=OE_Replace ) continue;
114168      while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){
114169        *ppFrom = pNext;
114170        pThis->pNext = pNext->pNext;
114171        pNext->pNext = pThis;
114172        ppFrom = &pNext->pNext;
114173      }
114174      break;
114175    }
114176  }
114177  sqlite3ExprDelete(db, pPIWhere);
114178  sqlite3ExprListDelete(db, pList);
114179  sqlite3SrcListDelete(db, pTblName);
114180  sqlite3DbFree(db, zName);
114181}
114182
114183/*
114184** Fill the Index.aiRowEst[] array with default information - information
114185** to be used when we have not run the ANALYZE command.
114186**
114187** aiRowEst[0] is supposed to contain the number of elements in the index.
114188** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
114189** number of rows in the table that match any particular value of the
114190** first column of the index.  aiRowEst[2] is an estimate of the number
114191** of rows that match any particular combination of the first 2 columns
114192** of the index.  And so forth.  It must always be the case that
114193*
114194**           aiRowEst[N]<=aiRowEst[N-1]
114195**           aiRowEst[N]>=1
114196**
114197** Apart from that, we have little to go on besides intuition as to
114198** how aiRowEst[] should be initialized.  The numbers generated here
114199** are based on typical values found in actual indices.
114200*/
114201SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
114202  /*                10,  9,  8,  7,  6 */
114203  LogEst aVal[] = { 33, 32, 30, 28, 26 };
114204  LogEst *a = pIdx->aiRowLogEst;
114205  int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
114206  int i;
114207
114208  /* Indexes with default row estimates should not have stat1 data */
114209  assert( !pIdx->hasStat1 );
114210
114211  /* Set the first entry (number of rows in the index) to the estimated
114212  ** number of rows in the table, or half the number of rows in the table
114213  ** for a partial index.   But do not let the estimate drop below 10. */
114214  a[0] = pIdx->pTable->nRowLogEst;
114215  if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10;  assert( 10==sqlite3LogEst(2) );
114216  if( a[0]<33 ) a[0] = 33;                  assert( 33==sqlite3LogEst(10) );
114217
114218  /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
114219  ** 6 and each subsequent value (if any) is 5.  */
114220  memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
114221  for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
114222    a[i] = 23;                    assert( 23==sqlite3LogEst(5) );
114223  }
114224
114225  assert( 0==sqlite3LogEst(1) );
114226  if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
114227}
114228
114229/*
114230** This routine will drop an existing named index.  This routine
114231** implements the DROP INDEX statement.
114232*/
114233SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
114234  Index *pIndex;
114235  Vdbe *v;
114236  sqlite3 *db = pParse->db;
114237  int iDb;
114238
114239  assert( pParse->nErr==0 );   /* Never called with prior errors */
114240  if( db->mallocFailed ){
114241    goto exit_drop_index;
114242  }
114243  assert( pName->nSrc==1 );
114244  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
114245    goto exit_drop_index;
114246  }
114247  pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
114248  if( pIndex==0 ){
114249    if( !ifExists ){
114250      sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
114251    }else{
114252      sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
114253    }
114254    pParse->checkSchema = 1;
114255    goto exit_drop_index;
114256  }
114257  if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
114258    sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
114259      "or PRIMARY KEY constraint cannot be dropped", 0);
114260    goto exit_drop_index;
114261  }
114262  iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
114263#ifndef SQLITE_OMIT_AUTHORIZATION
114264  {
114265    int code = SQLITE_DROP_INDEX;
114266    Table *pTab = pIndex->pTable;
114267    const char *zDb = db->aDb[iDb].zDbSName;
114268    const char *zTab = SCHEMA_TABLE(iDb);
114269    if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
114270      goto exit_drop_index;
114271    }
114272    if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
114273    if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
114274      goto exit_drop_index;
114275    }
114276  }
114277#endif
114278
114279  /* Generate code to remove the index and from the master table */
114280  v = sqlite3GetVdbe(pParse);
114281  if( v ){
114282    sqlite3BeginWriteOperation(pParse, 1, iDb);
114283    sqlite3NestedParse(pParse,
114284       "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
114285       db->aDb[iDb].zDbSName, MASTER_NAME, pIndex->zName
114286    );
114287    sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
114288    sqlite3ChangeCookie(pParse, iDb);
114289    destroyRootPage(pParse, pIndex->tnum, iDb);
114290    sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
114291  }
114292
114293exit_drop_index:
114294  sqlite3SrcListDelete(db, pName);
114295}
114296
114297/*
114298** pArray is a pointer to an array of objects. Each object in the
114299** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
114300** to extend the array so that there is space for a new object at the end.
114301**
114302** When this function is called, *pnEntry contains the current size of
114303** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
114304** in total).
114305**
114306** If the realloc() is successful (i.e. if no OOM condition occurs), the
114307** space allocated for the new object is zeroed, *pnEntry updated to
114308** reflect the new size of the array and a pointer to the new allocation
114309** returned. *pIdx is set to the index of the new array entry in this case.
114310**
114311** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
114312** unchanged and a copy of pArray returned.
114313*/
114314SQLITE_PRIVATE void *sqlite3ArrayAllocate(
114315  sqlite3 *db,      /* Connection to notify of malloc failures */
114316  void *pArray,     /* Array of objects.  Might be reallocated */
114317  int szEntry,      /* Size of each object in the array */
114318  int *pnEntry,     /* Number of objects currently in use */
114319  int *pIdx         /* Write the index of a new slot here */
114320){
114321  char *z;
114322  sqlite3_int64 n = *pIdx = *pnEntry;
114323  if( (n & (n-1))==0 ){
114324    sqlite3_int64 sz = (n==0) ? 1 : 2*n;
114325    void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
114326    if( pNew==0 ){
114327      *pIdx = -1;
114328      return pArray;
114329    }
114330    pArray = pNew;
114331  }
114332  z = (char*)pArray;
114333  memset(&z[n * szEntry], 0, szEntry);
114334  ++*pnEntry;
114335  return pArray;
114336}
114337
114338/*
114339** Append a new element to the given IdList.  Create a new IdList if
114340** need be.
114341**
114342** A new IdList is returned, or NULL if malloc() fails.
114343*/
114344SQLITE_PRIVATE IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){
114345  sqlite3 *db = pParse->db;
114346  int i;
114347  if( pList==0 ){
114348    pList = sqlite3DbMallocZero(db, sizeof(IdList) );
114349    if( pList==0 ) return 0;
114350  }
114351  pList->a = sqlite3ArrayAllocate(
114352      db,
114353      pList->a,
114354      sizeof(pList->a[0]),
114355      &pList->nId,
114356      &i
114357  );
114358  if( i<0 ){
114359    sqlite3IdListDelete(db, pList);
114360    return 0;
114361  }
114362  pList->a[i].zName = sqlite3NameFromToken(db, pToken);
114363  if( IN_RENAME_OBJECT && pList->a[i].zName ){
114364    sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
114365  }
114366  return pList;
114367}
114368
114369/*
114370** Delete an IdList.
114371*/
114372SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
114373  int i;
114374  if( pList==0 ) return;
114375  for(i=0; i<pList->nId; i++){
114376    sqlite3DbFree(db, pList->a[i].zName);
114377  }
114378  sqlite3DbFree(db, pList->a);
114379  sqlite3DbFreeNN(db, pList);
114380}
114381
114382/*
114383** Return the index in pList of the identifier named zId.  Return -1
114384** if not found.
114385*/
114386SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
114387  int i;
114388  if( pList==0 ) return -1;
114389  for(i=0; i<pList->nId; i++){
114390    if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
114391  }
114392  return -1;
114393}
114394
114395/*
114396** Maximum size of a SrcList object.
114397** The SrcList object is used to represent the FROM clause of a
114398** SELECT statement, and the query planner cannot deal with more
114399** than 64 tables in a join.  So any value larger than 64 here
114400** is sufficient for most uses.  Smaller values, like say 10, are
114401** appropriate for small and memory-limited applications.
114402*/
114403#ifndef SQLITE_MAX_SRCLIST
114404# define SQLITE_MAX_SRCLIST 200
114405#endif
114406
114407/*
114408** Expand the space allocated for the given SrcList object by
114409** creating nExtra new slots beginning at iStart.  iStart is zero based.
114410** New slots are zeroed.
114411**
114412** For example, suppose a SrcList initially contains two entries: A,B.
114413** To append 3 new entries onto the end, do this:
114414**
114415**    sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
114416**
114417** After the call above it would contain:  A, B, nil, nil, nil.
114418** If the iStart argument had been 1 instead of 2, then the result
114419** would have been:  A, nil, nil, nil, B.  To prepend the new slots,
114420** the iStart value would be 0.  The result then would
114421** be: nil, nil, nil, A, B.
114422**
114423** If a memory allocation fails or the SrcList becomes too large, leave
114424** the original SrcList unchanged, return NULL, and leave an error message
114425** in pParse.
114426*/
114427SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
114428  Parse *pParse,     /* Parsing context into which errors are reported */
114429  SrcList *pSrc,     /* The SrcList to be enlarged */
114430  int nExtra,        /* Number of new slots to add to pSrc->a[] */
114431  int iStart         /* Index in pSrc->a[] of first new slot */
114432){
114433  int i;
114434
114435  /* Sanity checking on calling parameters */
114436  assert( iStart>=0 );
114437  assert( nExtra>=1 );
114438  assert( pSrc!=0 );
114439  assert( iStart<=pSrc->nSrc );
114440
114441  /* Allocate additional space if needed */
114442  if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
114443    SrcList *pNew;
114444    sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
114445    sqlite3 *db = pParse->db;
114446
114447    if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
114448      sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d",
114449                      SQLITE_MAX_SRCLIST);
114450      return 0;
114451    }
114452    if( nAlloc>SQLITE_MAX_SRCLIST ) nAlloc = SQLITE_MAX_SRCLIST;
114453    pNew = sqlite3DbRealloc(db, pSrc,
114454               sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
114455    if( pNew==0 ){
114456      assert( db->mallocFailed );
114457      return 0;
114458    }
114459    pSrc = pNew;
114460    pSrc->nAlloc = nAlloc;
114461  }
114462
114463  /* Move existing slots that come after the newly inserted slots
114464  ** out of the way */
114465  for(i=pSrc->nSrc-1; i>=iStart; i--){
114466    pSrc->a[i+nExtra] = pSrc->a[i];
114467  }
114468  pSrc->nSrc += nExtra;
114469
114470  /* Zero the newly allocated slots */
114471  memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
114472  for(i=iStart; i<iStart+nExtra; i++){
114473    pSrc->a[i].iCursor = -1;
114474  }
114475
114476  /* Return a pointer to the enlarged SrcList */
114477  return pSrc;
114478}
114479
114480
114481/*
114482** Append a new table name to the given SrcList.  Create a new SrcList if
114483** need be.  A new entry is created in the SrcList even if pTable is NULL.
114484**
114485** A SrcList is returned, or NULL if there is an OOM error or if the
114486** SrcList grows to large.  The returned
114487** SrcList might be the same as the SrcList that was input or it might be
114488** a new one.  If an OOM error does occurs, then the prior value of pList
114489** that is input to this routine is automatically freed.
114490**
114491** If pDatabase is not null, it means that the table has an optional
114492** database name prefix.  Like this:  "database.table".  The pDatabase
114493** points to the table name and the pTable points to the database name.
114494** The SrcList.a[].zName field is filled with the table name which might
114495** come from pTable (if pDatabase is NULL) or from pDatabase.
114496** SrcList.a[].zDatabase is filled with the database name from pTable,
114497** or with NULL if no database is specified.
114498**
114499** In other words, if call like this:
114500**
114501**         sqlite3SrcListAppend(D,A,B,0);
114502**
114503** Then B is a table name and the database name is unspecified.  If called
114504** like this:
114505**
114506**         sqlite3SrcListAppend(D,A,B,C);
114507**
114508** Then C is the table name and B is the database name.  If C is defined
114509** then so is B.  In other words, we never have a case where:
114510**
114511**         sqlite3SrcListAppend(D,A,0,C);
114512**
114513** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
114514** before being added to the SrcList.
114515*/
114516SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
114517  Parse *pParse,      /* Parsing context, in which errors are reported */
114518  SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
114519  Token *pTable,      /* Table to append */
114520  Token *pDatabase    /* Database of the table */
114521){
114522  struct SrcList_item *pItem;
114523  sqlite3 *db;
114524  assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
114525  assert( pParse!=0 );
114526  assert( pParse->db!=0 );
114527  db = pParse->db;
114528  if( pList==0 ){
114529    pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
114530    if( pList==0 ) return 0;
114531    pList->nAlloc = 1;
114532    pList->nSrc = 1;
114533    memset(&pList->a[0], 0, sizeof(pList->a[0]));
114534    pList->a[0].iCursor = -1;
114535  }else{
114536    SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
114537    if( pNew==0 ){
114538      sqlite3SrcListDelete(db, pList);
114539      return 0;
114540    }else{
114541      pList = pNew;
114542    }
114543  }
114544  pItem = &pList->a[pList->nSrc-1];
114545  if( pDatabase && pDatabase->z==0 ){
114546    pDatabase = 0;
114547  }
114548  if( pDatabase ){
114549    pItem->zName = sqlite3NameFromToken(db, pDatabase);
114550    pItem->zDatabase = sqlite3NameFromToken(db, pTable);
114551  }else{
114552    pItem->zName = sqlite3NameFromToken(db, pTable);
114553    pItem->zDatabase = 0;
114554  }
114555  return pList;
114556}
114557
114558/*
114559** Assign VdbeCursor index numbers to all tables in a SrcList
114560*/
114561SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
114562  int i;
114563  struct SrcList_item *pItem;
114564  assert(pList || pParse->db->mallocFailed );
114565  if( pList ){
114566    for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
114567      if( pItem->iCursor>=0 ) break;
114568      pItem->iCursor = pParse->nTab++;
114569      if( pItem->pSelect ){
114570        sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
114571      }
114572    }
114573  }
114574}
114575
114576/*
114577** Delete an entire SrcList including all its substructure.
114578*/
114579SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
114580  int i;
114581  struct SrcList_item *pItem;
114582  if( pList==0 ) return;
114583  for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
114584    sqlite3DbFree(db, pItem->zDatabase);
114585    sqlite3DbFree(db, pItem->zName);
114586    sqlite3DbFree(db, pItem->zAlias);
114587    if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
114588    if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
114589    sqlite3DeleteTable(db, pItem->pTab);
114590    sqlite3SelectDelete(db, pItem->pSelect);
114591    sqlite3ExprDelete(db, pItem->pOn);
114592    sqlite3IdListDelete(db, pItem->pUsing);
114593  }
114594  sqlite3DbFreeNN(db, pList);
114595}
114596
114597/*
114598** This routine is called by the parser to add a new term to the
114599** end of a growing FROM clause.  The "p" parameter is the part of
114600** the FROM clause that has already been constructed.  "p" is NULL
114601** if this is the first term of the FROM clause.  pTable and pDatabase
114602** are the name of the table and database named in the FROM clause term.
114603** pDatabase is NULL if the database name qualifier is missing - the
114604** usual case.  If the term has an alias, then pAlias points to the
114605** alias token.  If the term is a subquery, then pSubquery is the
114606** SELECT statement that the subquery encodes.  The pTable and
114607** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
114608** parameters are the content of the ON and USING clauses.
114609**
114610** Return a new SrcList which encodes is the FROM with the new
114611** term added.
114612*/
114613SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
114614  Parse *pParse,          /* Parsing context */
114615  SrcList *p,             /* The left part of the FROM clause already seen */
114616  Token *pTable,          /* Name of the table to add to the FROM clause */
114617  Token *pDatabase,       /* Name of the database containing pTable */
114618  Token *pAlias,          /* The right-hand side of the AS subexpression */
114619  Select *pSubquery,      /* A subquery used in place of a table name */
114620  Expr *pOn,              /* The ON clause of a join */
114621  IdList *pUsing          /* The USING clause of a join */
114622){
114623  struct SrcList_item *pItem;
114624  sqlite3 *db = pParse->db;
114625  if( !p && (pOn || pUsing) ){
114626    sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
114627      (pOn ? "ON" : "USING")
114628    );
114629    goto append_from_error;
114630  }
114631  p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase);
114632  if( p==0 ){
114633    goto append_from_error;
114634  }
114635  assert( p->nSrc>0 );
114636  pItem = &p->a[p->nSrc-1];
114637  assert( (pTable==0)==(pDatabase==0) );
114638  assert( pItem->zName==0 || pDatabase!=0 );
114639  if( IN_RENAME_OBJECT && pItem->zName ){
114640    Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
114641    sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
114642  }
114643  assert( pAlias!=0 );
114644  if( pAlias->n ){
114645    pItem->zAlias = sqlite3NameFromToken(db, pAlias);
114646  }
114647  pItem->pSelect = pSubquery;
114648  pItem->pOn = pOn;
114649  pItem->pUsing = pUsing;
114650  return p;
114651
114652 append_from_error:
114653  assert( p==0 );
114654  sqlite3ExprDelete(db, pOn);
114655  sqlite3IdListDelete(db, pUsing);
114656  sqlite3SelectDelete(db, pSubquery);
114657  return 0;
114658}
114659
114660/*
114661** Add an INDEXED BY or NOT INDEXED clause to the most recently added
114662** element of the source-list passed as the second argument.
114663*/
114664SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
114665  assert( pIndexedBy!=0 );
114666  if( p && pIndexedBy->n>0 ){
114667    struct SrcList_item *pItem;
114668    assert( p->nSrc>0 );
114669    pItem = &p->a[p->nSrc-1];
114670    assert( pItem->fg.notIndexed==0 );
114671    assert( pItem->fg.isIndexedBy==0 );
114672    assert( pItem->fg.isTabFunc==0 );
114673    if( pIndexedBy->n==1 && !pIndexedBy->z ){
114674      /* A "NOT INDEXED" clause was supplied. See parse.y
114675      ** construct "indexed_opt" for details. */
114676      pItem->fg.notIndexed = 1;
114677    }else{
114678      pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
114679      pItem->fg.isIndexedBy = 1;
114680    }
114681  }
114682}
114683
114684/*
114685** Add the list of function arguments to the SrcList entry for a
114686** table-valued-function.
114687*/
114688SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){
114689  if( p ){
114690    struct SrcList_item *pItem = &p->a[p->nSrc-1];
114691    assert( pItem->fg.notIndexed==0 );
114692    assert( pItem->fg.isIndexedBy==0 );
114693    assert( pItem->fg.isTabFunc==0 );
114694    pItem->u1.pFuncArg = pList;
114695    pItem->fg.isTabFunc = 1;
114696  }else{
114697    sqlite3ExprListDelete(pParse->db, pList);
114698  }
114699}
114700
114701/*
114702** When building up a FROM clause in the parser, the join operator
114703** is initially attached to the left operand.  But the code generator
114704** expects the join operator to be on the right operand.  This routine
114705** Shifts all join operators from left to right for an entire FROM
114706** clause.
114707**
114708** Example: Suppose the join is like this:
114709**
114710**           A natural cross join B
114711**
114712** The operator is "natural cross join".  The A and B operands are stored
114713** in p->a[0] and p->a[1], respectively.  The parser initially stores the
114714** operator with A.  This routine shifts that operator over to B.
114715*/
114716SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
114717  if( p ){
114718    int i;
114719    for(i=p->nSrc-1; i>0; i--){
114720      p->a[i].fg.jointype = p->a[i-1].fg.jointype;
114721    }
114722    p->a[0].fg.jointype = 0;
114723  }
114724}
114725
114726/*
114727** Generate VDBE code for a BEGIN statement.
114728*/
114729SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
114730  sqlite3 *db;
114731  Vdbe *v;
114732  int i;
114733
114734  assert( pParse!=0 );
114735  db = pParse->db;
114736  assert( db!=0 );
114737  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
114738    return;
114739  }
114740  v = sqlite3GetVdbe(pParse);
114741  if( !v ) return;
114742  if( type!=TK_DEFERRED ){
114743    for(i=0; i<db->nDb; i++){
114744      sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
114745      sqlite3VdbeUsesBtree(v, i);
114746    }
114747  }
114748  sqlite3VdbeAddOp0(v, OP_AutoCommit);
114749}
114750
114751/*
114752** Generate VDBE code for a COMMIT or ROLLBACK statement.
114753** Code for ROLLBACK is generated if eType==TK_ROLLBACK.  Otherwise
114754** code is generated for a COMMIT.
114755*/
114756SQLITE_PRIVATE void sqlite3EndTransaction(Parse *pParse, int eType){
114757  Vdbe *v;
114758  int isRollback;
114759
114760  assert( pParse!=0 );
114761  assert( pParse->db!=0 );
114762  assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK );
114763  isRollback = eType==TK_ROLLBACK;
114764  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION,
114765       isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){
114766    return;
114767  }
114768  v = sqlite3GetVdbe(pParse);
114769  if( v ){
114770    sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, isRollback);
114771  }
114772}
114773
114774/*
114775** This function is called by the parser when it parses a command to create,
114776** release or rollback an SQL savepoint.
114777*/
114778SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
114779  char *zName = sqlite3NameFromToken(pParse->db, pName);
114780  if( zName ){
114781    Vdbe *v = sqlite3GetVdbe(pParse);
114782#ifndef SQLITE_OMIT_AUTHORIZATION
114783    static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
114784    assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
114785#endif
114786    if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
114787      sqlite3DbFree(pParse->db, zName);
114788      return;
114789    }
114790    sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
114791  }
114792}
114793
114794/*
114795** Make sure the TEMP database is open and available for use.  Return
114796** the number of errors.  Leave any error messages in the pParse structure.
114797*/
114798SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
114799  sqlite3 *db = pParse->db;
114800  if( db->aDb[1].pBt==0 && !pParse->explain ){
114801    int rc;
114802    Btree *pBt;
114803    static const int flags =
114804          SQLITE_OPEN_READWRITE |
114805          SQLITE_OPEN_CREATE |
114806          SQLITE_OPEN_EXCLUSIVE |
114807          SQLITE_OPEN_DELETEONCLOSE |
114808          SQLITE_OPEN_TEMP_DB;
114809
114810    rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
114811    if( rc!=SQLITE_OK ){
114812      sqlite3ErrorMsg(pParse, "unable to open a temporary database "
114813        "file for storing temporary tables");
114814      pParse->rc = rc;
114815      return 1;
114816    }
114817    db->aDb[1].pBt = pBt;
114818    assert( db->aDb[1].pSchema );
114819    if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
114820      sqlite3OomFault(db);
114821      return 1;
114822    }
114823  }
114824  return 0;
114825}
114826
114827/*
114828** Record the fact that the schema cookie will need to be verified
114829** for database iDb.  The code to actually verify the schema cookie
114830** will occur at the end of the top-level VDBE and will be generated
114831** later, by sqlite3FinishCoding().
114832*/
114833SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
114834  Parse *pToplevel = sqlite3ParseToplevel(pParse);
114835
114836  assert( iDb>=0 && iDb<pParse->db->nDb );
114837  assert( pParse->db->aDb[iDb].pBt!=0 || iDb==1 );
114838  assert( iDb<SQLITE_MAX_ATTACHED+2 );
114839  assert( sqlite3SchemaMutexHeld(pParse->db, iDb, 0) );
114840  if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
114841    DbMaskSet(pToplevel->cookieMask, iDb);
114842    if( !OMIT_TEMPDB && iDb==1 ){
114843      sqlite3OpenTempDatabase(pToplevel);
114844    }
114845  }
114846}
114847
114848/*
114849** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
114850** attached database. Otherwise, invoke it for the database named zDb only.
114851*/
114852SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
114853  sqlite3 *db = pParse->db;
114854  int i;
114855  for(i=0; i<db->nDb; i++){
114856    Db *pDb = &db->aDb[i];
114857    if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
114858      sqlite3CodeVerifySchema(pParse, i);
114859    }
114860  }
114861}
114862
114863/*
114864** Generate VDBE code that prepares for doing an operation that
114865** might change the database.
114866**
114867** This routine starts a new transaction if we are not already within
114868** a transaction.  If we are already within a transaction, then a checkpoint
114869** is set if the setStatement parameter is true.  A checkpoint should
114870** be set for operations that might fail (due to a constraint) part of
114871** the way through and which will need to undo some writes without having to
114872** rollback the whole transaction.  For operations where all constraints
114873** can be checked before any changes are made to the database, it is never
114874** necessary to undo a write and the checkpoint should not be set.
114875*/
114876SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
114877  Parse *pToplevel = sqlite3ParseToplevel(pParse);
114878  sqlite3CodeVerifySchema(pParse, iDb);
114879  DbMaskSet(pToplevel->writeMask, iDb);
114880  pToplevel->isMultiWrite |= setStatement;
114881}
114882
114883/*
114884** Indicate that the statement currently under construction might write
114885** more than one entry (example: deleting one row then inserting another,
114886** inserting multiple rows in a table, or inserting a row and index entries.)
114887** If an abort occurs after some of these writes have completed, then it will
114888** be necessary to undo the completed writes.
114889*/
114890SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
114891  Parse *pToplevel = sqlite3ParseToplevel(pParse);
114892  pToplevel->isMultiWrite = 1;
114893}
114894
114895/*
114896** The code generator calls this routine if is discovers that it is
114897** possible to abort a statement prior to completion.  In order to
114898** perform this abort without corrupting the database, we need to make
114899** sure that the statement is protected by a statement transaction.
114900**
114901** Technically, we only need to set the mayAbort flag if the
114902** isMultiWrite flag was previously set.  There is a time dependency
114903** such that the abort must occur after the multiwrite.  This makes
114904** some statements involving the REPLACE conflict resolution algorithm
114905** go a little faster.  But taking advantage of this time dependency
114906** makes it more difficult to prove that the code is correct (in
114907** particular, it prevents us from writing an effective
114908** implementation of sqlite3AssertMayAbort()) and so we have chosen
114909** to take the safe route and skip the optimization.
114910*/
114911SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
114912  Parse *pToplevel = sqlite3ParseToplevel(pParse);
114913  pToplevel->mayAbort = 1;
114914}
114915
114916/*
114917** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
114918** error. The onError parameter determines which (if any) of the statement
114919** and/or current transaction is rolled back.
114920*/
114921SQLITE_PRIVATE void sqlite3HaltConstraint(
114922  Parse *pParse,    /* Parsing context */
114923  int errCode,      /* extended error code */
114924  int onError,      /* Constraint type */
114925  char *p4,         /* Error message */
114926  i8 p4type,        /* P4_STATIC or P4_TRANSIENT */
114927  u8 p5Errmsg       /* P5_ErrMsg type */
114928){
114929  Vdbe *v = sqlite3GetVdbe(pParse);
114930  assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested );
114931  if( onError==OE_Abort ){
114932    sqlite3MayAbort(pParse);
114933  }
114934  sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
114935  sqlite3VdbeChangeP5(v, p5Errmsg);
114936}
114937
114938/*
114939** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
114940*/
114941SQLITE_PRIVATE void sqlite3UniqueConstraint(
114942  Parse *pParse,    /* Parsing context */
114943  int onError,      /* Constraint type */
114944  Index *pIdx       /* The index that triggers the constraint */
114945){
114946  char *zErr;
114947  int j;
114948  StrAccum errMsg;
114949  Table *pTab = pIdx->pTable;
114950
114951  sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
114952                      pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
114953  if( pIdx->aColExpr ){
114954    sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
114955  }else{
114956    for(j=0; j<pIdx->nKeyCol; j++){
114957      char *zCol;
114958      assert( pIdx->aiColumn[j]>=0 );
114959      zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
114960      if( j ) sqlite3_str_append(&errMsg, ", ", 2);
114961      sqlite3_str_appendall(&errMsg, pTab->zName);
114962      sqlite3_str_append(&errMsg, ".", 1);
114963      sqlite3_str_appendall(&errMsg, zCol);
114964    }
114965  }
114966  zErr = sqlite3StrAccumFinish(&errMsg);
114967  sqlite3HaltConstraint(pParse,
114968    IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY
114969                            : SQLITE_CONSTRAINT_UNIQUE,
114970    onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
114971}
114972
114973
114974/*
114975** Code an OP_Halt due to non-unique rowid.
114976*/
114977SQLITE_PRIVATE void sqlite3RowidConstraint(
114978  Parse *pParse,    /* Parsing context */
114979  int onError,      /* Conflict resolution algorithm */
114980  Table *pTab       /* The table with the non-unique rowid */
114981){
114982  char *zMsg;
114983  int rc;
114984  if( pTab->iPKey>=0 ){
114985    zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
114986                          pTab->aCol[pTab->iPKey].zName);
114987    rc = SQLITE_CONSTRAINT_PRIMARYKEY;
114988  }else{
114989    zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
114990    rc = SQLITE_CONSTRAINT_ROWID;
114991  }
114992  sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC,
114993                        P5_ConstraintUnique);
114994}
114995
114996/*
114997** Check to see if pIndex uses the collating sequence pColl.  Return
114998** true if it does and false if it does not.
114999*/
115000#ifndef SQLITE_OMIT_REINDEX
115001static int collationMatch(const char *zColl, Index *pIndex){
115002  int i;
115003  assert( zColl!=0 );
115004  for(i=0; i<pIndex->nColumn; i++){
115005    const char *z = pIndex->azColl[i];
115006    assert( z!=0 || pIndex->aiColumn[i]<0 );
115007    if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
115008      return 1;
115009    }
115010  }
115011  return 0;
115012}
115013#endif
115014
115015/*
115016** Recompute all indices of pTab that use the collating sequence pColl.
115017** If pColl==0 then recompute all indices of pTab.
115018*/
115019#ifndef SQLITE_OMIT_REINDEX
115020static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
115021  if( !IsVirtual(pTab) ){
115022    Index *pIndex;              /* An index associated with pTab */
115023
115024    for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
115025      if( zColl==0 || collationMatch(zColl, pIndex) ){
115026        int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
115027        sqlite3BeginWriteOperation(pParse, 0, iDb);
115028        sqlite3RefillIndex(pParse, pIndex, -1);
115029      }
115030    }
115031  }
115032}
115033#endif
115034
115035/*
115036** Recompute all indices of all tables in all databases where the
115037** indices use the collating sequence pColl.  If pColl==0 then recompute
115038** all indices everywhere.
115039*/
115040#ifndef SQLITE_OMIT_REINDEX
115041static void reindexDatabases(Parse *pParse, char const *zColl){
115042  Db *pDb;                    /* A single database */
115043  int iDb;                    /* The database index number */
115044  sqlite3 *db = pParse->db;   /* The database connection */
115045  HashElem *k;                /* For looping over tables in pDb */
115046  Table *pTab;                /* A table in the database */
115047
115048  assert( sqlite3BtreeHoldsAllMutexes(db) );  /* Needed for schema access */
115049  for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
115050    assert( pDb!=0 );
115051    for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
115052      pTab = (Table*)sqliteHashData(k);
115053      reindexTable(pParse, pTab, zColl);
115054    }
115055  }
115056}
115057#endif
115058
115059/*
115060** Generate code for the REINDEX command.
115061**
115062**        REINDEX                            -- 1
115063**        REINDEX  <collation>               -- 2
115064**        REINDEX  ?<database>.?<tablename>  -- 3
115065**        REINDEX  ?<database>.?<indexname>  -- 4
115066**
115067** Form 1 causes all indices in all attached databases to be rebuilt.
115068** Form 2 rebuilds all indices in all databases that use the named
115069** collating function.  Forms 3 and 4 rebuild the named index or all
115070** indices associated with the named table.
115071*/
115072#ifndef SQLITE_OMIT_REINDEX
115073SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
115074  CollSeq *pColl;             /* Collating sequence to be reindexed, or NULL */
115075  char *z;                    /* Name of a table or index */
115076  const char *zDb;            /* Name of the database */
115077  Table *pTab;                /* A table in the database */
115078  Index *pIndex;              /* An index associated with pTab */
115079  int iDb;                    /* The database index number */
115080  sqlite3 *db = pParse->db;   /* The database connection */
115081  Token *pObjName;            /* Name of the table or index to be reindexed */
115082
115083  /* Read the database schema. If an error occurs, leave an error message
115084  ** and code in pParse and return NULL. */
115085  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
115086    return;
115087  }
115088
115089  if( pName1==0 ){
115090    reindexDatabases(pParse, 0);
115091    return;
115092  }else if( NEVER(pName2==0) || pName2->z==0 ){
115093    char *zColl;
115094    assert( pName1->z );
115095    zColl = sqlite3NameFromToken(pParse->db, pName1);
115096    if( !zColl ) return;
115097    pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
115098    if( pColl ){
115099      reindexDatabases(pParse, zColl);
115100      sqlite3DbFree(db, zColl);
115101      return;
115102    }
115103    sqlite3DbFree(db, zColl);
115104  }
115105  iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
115106  if( iDb<0 ) return;
115107  z = sqlite3NameFromToken(db, pObjName);
115108  if( z==0 ) return;
115109  zDb = db->aDb[iDb].zDbSName;
115110  pTab = sqlite3FindTable(db, z, zDb);
115111  if( pTab ){
115112    reindexTable(pParse, pTab, 0);
115113    sqlite3DbFree(db, z);
115114    return;
115115  }
115116  pIndex = sqlite3FindIndex(db, z, zDb);
115117  sqlite3DbFree(db, z);
115118  if( pIndex ){
115119    sqlite3BeginWriteOperation(pParse, 0, iDb);
115120    sqlite3RefillIndex(pParse, pIndex, -1);
115121    return;
115122  }
115123  sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
115124}
115125#endif
115126
115127/*
115128** Return a KeyInfo structure that is appropriate for the given Index.
115129**
115130** The caller should invoke sqlite3KeyInfoUnref() on the returned object
115131** when it has finished using it.
115132*/
115133SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
115134  int i;
115135  int nCol = pIdx->nColumn;
115136  int nKey = pIdx->nKeyCol;
115137  KeyInfo *pKey;
115138  if( pParse->nErr ) return 0;
115139  if( pIdx->uniqNotNull ){
115140    pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
115141  }else{
115142    pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
115143  }
115144  if( pKey ){
115145    assert( sqlite3KeyInfoIsWriteable(pKey) );
115146    for(i=0; i<nCol; i++){
115147      const char *zColl = pIdx->azColl[i];
115148      pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
115149                        sqlite3LocateCollSeq(pParse, zColl);
115150      pKey->aSortFlags[i] = pIdx->aSortOrder[i];
115151      assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
115152    }
115153    if( pParse->nErr ){
115154      assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
115155      if( pIdx->bNoQuery==0 ){
115156        /* Deactivate the index because it contains an unknown collating
115157        ** sequence.  The only way to reactive the index is to reload the
115158        ** schema.  Adding the missing collating sequence later does not
115159        ** reactive the index.  The application had the chance to register
115160        ** the missing index using the collation-needed callback.  For
115161        ** simplicity, SQLite will not give the application a second chance.
115162        */
115163        pIdx->bNoQuery = 1;
115164        pParse->rc = SQLITE_ERROR_RETRY;
115165      }
115166      sqlite3KeyInfoUnref(pKey);
115167      pKey = 0;
115168    }
115169  }
115170  return pKey;
115171}
115172
115173#ifndef SQLITE_OMIT_CTE
115174/*
115175** This routine is invoked once per CTE by the parser while parsing a
115176** WITH clause.
115177*/
115178SQLITE_PRIVATE With *sqlite3WithAdd(
115179  Parse *pParse,          /* Parsing context */
115180  With *pWith,            /* Existing WITH clause, or NULL */
115181  Token *pName,           /* Name of the common-table */
115182  ExprList *pArglist,     /* Optional column name list for the table */
115183  Select *pQuery          /* Query used to initialize the table */
115184){
115185  sqlite3 *db = pParse->db;
115186  With *pNew;
115187  char *zName;
115188
115189  /* Check that the CTE name is unique within this WITH clause. If
115190  ** not, store an error in the Parse structure. */
115191  zName = sqlite3NameFromToken(pParse->db, pName);
115192  if( zName && pWith ){
115193    int i;
115194    for(i=0; i<pWith->nCte; i++){
115195      if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
115196        sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
115197      }
115198    }
115199  }
115200
115201  if( pWith ){
115202    sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
115203    pNew = sqlite3DbRealloc(db, pWith, nByte);
115204  }else{
115205    pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
115206  }
115207  assert( (pNew!=0 && zName!=0) || db->mallocFailed );
115208
115209  if( db->mallocFailed ){
115210    sqlite3ExprListDelete(db, pArglist);
115211    sqlite3SelectDelete(db, pQuery);
115212    sqlite3DbFree(db, zName);
115213    pNew = pWith;
115214  }else{
115215    pNew->a[pNew->nCte].pSelect = pQuery;
115216    pNew->a[pNew->nCte].pCols = pArglist;
115217    pNew->a[pNew->nCte].zName = zName;
115218    pNew->a[pNew->nCte].zCteErr = 0;
115219    pNew->nCte++;
115220  }
115221
115222  return pNew;
115223}
115224
115225/*
115226** Free the contents of the With object passed as the second argument.
115227*/
115228SQLITE_PRIVATE void sqlite3WithDelete(sqlite3 *db, With *pWith){
115229  if( pWith ){
115230    int i;
115231    for(i=0; i<pWith->nCte; i++){
115232      struct Cte *pCte = &pWith->a[i];
115233      sqlite3ExprListDelete(db, pCte->pCols);
115234      sqlite3SelectDelete(db, pCte->pSelect);
115235      sqlite3DbFree(db, pCte->zName);
115236    }
115237    sqlite3DbFree(db, pWith);
115238  }
115239}
115240#endif /* !defined(SQLITE_OMIT_CTE) */
115241
115242/************** End of build.c ***********************************************/
115243/************** Begin file callback.c ****************************************/
115244/*
115245** 2005 May 23
115246**
115247** The author disclaims copyright to this source code.  In place of
115248** a legal notice, here is a blessing:
115249**
115250**    May you do good and not evil.
115251**    May you find forgiveness for yourself and forgive others.
115252**    May you share freely, never taking more than you give.
115253**
115254*************************************************************************
115255**
115256** This file contains functions used to access the internal hash tables
115257** of user defined functions and collation sequences.
115258*/
115259
115260/* #include "sqliteInt.h" */
115261
115262/*
115263** Invoke the 'collation needed' callback to request a collation sequence
115264** in the encoding enc of name zName, length nName.
115265*/
115266static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
115267  assert( !db->xCollNeeded || !db->xCollNeeded16 );
115268  if( db->xCollNeeded ){
115269    char *zExternal = sqlite3DbStrDup(db, zName);
115270    if( !zExternal ) return;
115271    db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
115272    sqlite3DbFree(db, zExternal);
115273  }
115274#ifndef SQLITE_OMIT_UTF16
115275  if( db->xCollNeeded16 ){
115276    char const *zExternal;
115277    sqlite3_value *pTmp = sqlite3ValueNew(db);
115278    sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
115279    zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
115280    if( zExternal ){
115281      db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
115282    }
115283    sqlite3ValueFree(pTmp);
115284  }
115285#endif
115286}
115287
115288/*
115289** This routine is called if the collation factory fails to deliver a
115290** collation function in the best encoding but there may be other versions
115291** of this collation function (for other text encodings) available. Use one
115292** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
115293** possible.
115294*/
115295static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
115296  CollSeq *pColl2;
115297  char *z = pColl->zName;
115298  int i;
115299  static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
115300  for(i=0; i<3; i++){
115301    pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
115302    if( pColl2->xCmp!=0 ){
115303      memcpy(pColl, pColl2, sizeof(CollSeq));
115304      pColl->xDel = 0;         /* Do not copy the destructor */
115305      return SQLITE_OK;
115306    }
115307  }
115308  return SQLITE_ERROR;
115309}
115310
115311/*
115312** This routine is called on a collation sequence before it is used to
115313** check that it is defined. An undefined collation sequence exists when
115314** a database is loaded that contains references to collation sequences
115315** that have not been defined by sqlite3_create_collation() etc.
115316**
115317** If required, this routine calls the 'collation needed' callback to
115318** request a definition of the collating sequence. If this doesn't work,
115319** an equivalent collating sequence that uses a text encoding different
115320** from the main database is substituted, if one is available.
115321*/
115322SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
115323  if( pColl && pColl->xCmp==0 ){
115324    const char *zName = pColl->zName;
115325    sqlite3 *db = pParse->db;
115326    CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
115327    if( !p ){
115328      return SQLITE_ERROR;
115329    }
115330    assert( p==pColl );
115331  }
115332  return SQLITE_OK;
115333}
115334
115335
115336
115337/*
115338** Locate and return an entry from the db.aCollSeq hash table. If the entry
115339** specified by zName and nName is not found and parameter 'create' is
115340** true, then create a new entry. Otherwise return NULL.
115341**
115342** Each pointer stored in the sqlite3.aCollSeq hash table contains an
115343** array of three CollSeq structures. The first is the collation sequence
115344** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
115345**
115346** Stored immediately after the three collation sequences is a copy of
115347** the collation sequence name. A pointer to this string is stored in
115348** each collation sequence structure.
115349*/
115350static CollSeq *findCollSeqEntry(
115351  sqlite3 *db,          /* Database connection */
115352  const char *zName,    /* Name of the collating sequence */
115353  int create            /* Create a new entry if true */
115354){
115355  CollSeq *pColl;
115356  pColl = sqlite3HashFind(&db->aCollSeq, zName);
115357
115358  if( 0==pColl && create ){
115359    int nName = sqlite3Strlen30(zName) + 1;
115360    pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName);
115361    if( pColl ){
115362      CollSeq *pDel = 0;
115363      pColl[0].zName = (char*)&pColl[3];
115364      pColl[0].enc = SQLITE_UTF8;
115365      pColl[1].zName = (char*)&pColl[3];
115366      pColl[1].enc = SQLITE_UTF16LE;
115367      pColl[2].zName = (char*)&pColl[3];
115368      pColl[2].enc = SQLITE_UTF16BE;
115369      memcpy(pColl[0].zName, zName, nName);
115370      pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
115371
115372      /* If a malloc() failure occurred in sqlite3HashInsert(), it will
115373      ** return the pColl pointer to be deleted (because it wasn't added
115374      ** to the hash table).
115375      */
115376      assert( pDel==0 || pDel==pColl );
115377      if( pDel!=0 ){
115378        sqlite3OomFault(db);
115379        sqlite3DbFree(db, pDel);
115380        pColl = 0;
115381      }
115382    }
115383  }
115384  return pColl;
115385}
115386
115387/*
115388** Parameter zName points to a UTF-8 encoded string nName bytes long.
115389** Return the CollSeq* pointer for the collation sequence named zName
115390** for the encoding 'enc' from the database 'db'.
115391**
115392** If the entry specified is not found and 'create' is true, then create a
115393** new entry.  Otherwise return NULL.
115394**
115395** A separate function sqlite3LocateCollSeq() is a wrapper around
115396** this routine.  sqlite3LocateCollSeq() invokes the collation factory
115397** if necessary and generates an error message if the collating sequence
115398** cannot be found.
115399**
115400** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
115401*/
115402SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
115403  sqlite3 *db,          /* Database connection to search */
115404  u8 enc,               /* Desired text encoding */
115405  const char *zName,    /* Name of the collating sequence.  Might be NULL */
115406  int create            /* True to create CollSeq if doesn't already exist */
115407){
115408  CollSeq *pColl;
115409  assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
115410  assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
115411  if( zName ){
115412    pColl = findCollSeqEntry(db, zName, create);
115413    if( pColl ) pColl += enc-1;
115414  }else{
115415    pColl = db->pDfltColl;
115416  }
115417  return pColl;
115418}
115419
115420/*
115421** Change the text encoding for a database connection. This means that
115422** the pDfltColl must change as well.
115423*/
115424SQLITE_PRIVATE void sqlite3SetTextEncoding(sqlite3 *db, u8 enc){
115425  assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
115426  db->enc = enc;
115427  /* EVIDENCE-OF: R-08308-17224 The default collating function for all
115428  ** strings is BINARY.
115429  */
115430  db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
115431}
115432
115433/*
115434** This function is responsible for invoking the collation factory callback
115435** or substituting a collation sequence of a different encoding when the
115436** requested collation sequence is not available in the desired encoding.
115437**
115438** If it is not NULL, then pColl must point to the database native encoding
115439** collation sequence with name zName, length nName.
115440**
115441** The return value is either the collation sequence to be used in database
115442** db for collation type name zName, length nName, or NULL, if no collation
115443** sequence can be found.  If no collation is found, leave an error message.
115444**
115445** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
115446*/
115447SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
115448  Parse *pParse,        /* Parsing context */
115449  u8 enc,               /* The desired encoding for the collating sequence */
115450  CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
115451  const char *zName     /* Collating sequence name */
115452){
115453  CollSeq *p;
115454  sqlite3 *db = pParse->db;
115455
115456  p = pColl;
115457  if( !p ){
115458    p = sqlite3FindCollSeq(db, enc, zName, 0);
115459  }
115460  if( !p || !p->xCmp ){
115461    /* No collation sequence of this type for this encoding is registered.
115462    ** Call the collation factory to see if it can supply us with one.
115463    */
115464    callCollNeeded(db, enc, zName);
115465    p = sqlite3FindCollSeq(db, enc, zName, 0);
115466  }
115467  if( p && !p->xCmp && synthCollSeq(db, p) ){
115468    p = 0;
115469  }
115470  assert( !p || p->xCmp );
115471  if( p==0 ){
115472    sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
115473    pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
115474  }
115475  return p;
115476}
115477
115478/*
115479** This function returns the collation sequence for database native text
115480** encoding identified by the string zName.
115481**
115482** If the requested collation sequence is not available, or not available
115483** in the database native encoding, the collation factory is invoked to
115484** request it. If the collation factory does not supply such a sequence,
115485** and the sequence is available in another text encoding, then that is
115486** returned instead.
115487**
115488** If no versions of the requested collations sequence are available, or
115489** another error occurs, NULL is returned and an error message written into
115490** pParse.
115491**
115492** This routine is a wrapper around sqlite3FindCollSeq().  This routine
115493** invokes the collation factory if the named collation cannot be found
115494** and generates an error message.
115495**
115496** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
115497*/
115498SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
115499  sqlite3 *db = pParse->db;
115500  u8 enc = ENC(db);
115501  u8 initbusy = db->init.busy;
115502  CollSeq *pColl;
115503
115504  pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
115505  if( !initbusy && (!pColl || !pColl->xCmp) ){
115506    pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
115507  }
115508
115509  return pColl;
115510}
115511
115512/* During the search for the best function definition, this procedure
115513** is called to test how well the function passed as the first argument
115514** matches the request for a function with nArg arguments in a system
115515** that uses encoding enc. The value returned indicates how well the
115516** request is matched. A higher value indicates a better match.
115517**
115518** If nArg is -1 that means to only return a match (non-zero) if p->nArg
115519** is also -1.  In other words, we are searching for a function that
115520** takes a variable number of arguments.
115521**
115522** If nArg is -2 that means that we are searching for any function
115523** regardless of the number of arguments it uses, so return a positive
115524** match score for any
115525**
115526** The returned value is always between 0 and 6, as follows:
115527**
115528** 0: Not a match.
115529** 1: UTF8/16 conversion required and function takes any number of arguments.
115530** 2: UTF16 byte order change required and function takes any number of args.
115531** 3: encoding matches and function takes any number of arguments
115532** 4: UTF8/16 conversion required - argument count matches exactly
115533** 5: UTF16 byte order conversion required - argument count matches exactly
115534** 6: Perfect match:  encoding and argument count match exactly.
115535**
115536** If nArg==(-2) then any function with a non-null xSFunc is
115537** a perfect match and any function with xSFunc NULL is
115538** a non-match.
115539*/
115540#define FUNC_PERFECT_MATCH 6  /* The score for a perfect match */
115541static int matchQuality(
115542  FuncDef *p,     /* The function we are evaluating for match quality */
115543  int nArg,       /* Desired number of arguments.  (-1)==any */
115544  u8 enc          /* Desired text encoding */
115545){
115546  int match;
115547  assert( p->nArg>=-1 );
115548
115549  /* Wrong number of arguments means "no match" */
115550  if( p->nArg!=nArg ){
115551    if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
115552    if( p->nArg>=0 ) return 0;
115553  }
115554
115555  /* Give a better score to a function with a specific number of arguments
115556  ** than to function that accepts any number of arguments. */
115557  if( p->nArg==nArg ){
115558    match = 4;
115559  }else{
115560    match = 1;
115561  }
115562
115563  /* Bonus points if the text encoding matches */
115564  if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
115565    match += 2;  /* Exact encoding match */
115566  }else if( (enc & p->funcFlags & 2)!=0 ){
115567    match += 1;  /* Both are UTF16, but with different byte orders */
115568  }
115569
115570  return match;
115571}
115572
115573/*
115574** Search a FuncDefHash for a function with the given name.  Return
115575** a pointer to the matching FuncDef if found, or 0 if there is no match.
115576*/
115577SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(
115578  int h,               /* Hash of the name */
115579  const char *zFunc    /* Name of function */
115580){
115581  FuncDef *p;
115582  for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
115583    if( sqlite3StrICmp(p->zName, zFunc)==0 ){
115584      return p;
115585    }
115586  }
115587  return 0;
115588}
115589
115590/*
115591** Insert a new FuncDef into a FuncDefHash hash table.
115592*/
115593SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(
115594  FuncDef *aDef,      /* List of global functions to be inserted */
115595  int nDef            /* Length of the apDef[] list */
115596){
115597  int i;
115598  for(i=0; i<nDef; i++){
115599    FuncDef *pOther;
115600    const char *zName = aDef[i].zName;
115601    int nName = sqlite3Strlen30(zName);
115602    int h = SQLITE_FUNC_HASH(zName[0], nName);
115603    assert( zName[0]>='a' && zName[0]<='z' );
115604    pOther = sqlite3FunctionSearch(h, zName);
115605    if( pOther ){
115606      assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
115607      aDef[i].pNext = pOther->pNext;
115608      pOther->pNext = &aDef[i];
115609    }else{
115610      aDef[i].pNext = 0;
115611      aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
115612      sqlite3BuiltinFunctions.a[h] = &aDef[i];
115613    }
115614  }
115615}
115616
115617
115618
115619/*
115620** Locate a user function given a name, a number of arguments and a flag
115621** indicating whether the function prefers UTF-16 over UTF-8.  Return a
115622** pointer to the FuncDef structure that defines that function, or return
115623** NULL if the function does not exist.
115624**
115625** If the createFlag argument is true, then a new (blank) FuncDef
115626** structure is created and liked into the "db" structure if a
115627** no matching function previously existed.
115628**
115629** If nArg is -2, then the first valid function found is returned.  A
115630** function is valid if xSFunc is non-zero.  The nArg==(-2)
115631** case is used to see if zName is a valid function name for some number
115632** of arguments.  If nArg is -2, then createFlag must be 0.
115633**
115634** If createFlag is false, then a function with the required name and
115635** number of arguments may be returned even if the eTextRep flag does not
115636** match that requested.
115637*/
115638SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
115639  sqlite3 *db,       /* An open database */
115640  const char *zName, /* Name of the function.  zero-terminated */
115641  int nArg,          /* Number of arguments.  -1 means any number */
115642  u8 enc,            /* Preferred text encoding */
115643  u8 createFlag      /* Create new entry if true and does not otherwise exist */
115644){
115645  FuncDef *p;         /* Iterator variable */
115646  FuncDef *pBest = 0; /* Best match found so far */
115647  int bestScore = 0;  /* Score of best match */
115648  int h;              /* Hash value */
115649  int nName;          /* Length of the name */
115650
115651  assert( nArg>=(-2) );
115652  assert( nArg>=(-1) || createFlag==0 );
115653  nName = sqlite3Strlen30(zName);
115654
115655  /* First search for a match amongst the application-defined functions.
115656  */
115657  p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
115658  while( p ){
115659    int score = matchQuality(p, nArg, enc);
115660    if( score>bestScore ){
115661      pBest = p;
115662      bestScore = score;
115663    }
115664    p = p->pNext;
115665  }
115666
115667  /* If no match is found, search the built-in functions.
115668  **
115669  ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
115670  ** functions even if a prior app-defined function was found.  And give
115671  ** priority to built-in functions.
115672  **
115673  ** Except, if createFlag is true, that means that we are trying to
115674  ** install a new function.  Whatever FuncDef structure is returned it will
115675  ** have fields overwritten with new information appropriate for the
115676  ** new function.  But the FuncDefs for built-in functions are read-only.
115677  ** So we must not search for built-ins when creating a new function.
115678  */
115679  if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
115680    bestScore = 0;
115681    h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName);
115682    p = sqlite3FunctionSearch(h, zName);
115683    while( p ){
115684      int score = matchQuality(p, nArg, enc);
115685      if( score>bestScore ){
115686        pBest = p;
115687        bestScore = score;
115688      }
115689      p = p->pNext;
115690    }
115691  }
115692
115693  /* If the createFlag parameter is true and the search did not reveal an
115694  ** exact match for the name, number of arguments and encoding, then add a
115695  ** new entry to the hash table and return it.
115696  */
115697  if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
115698      (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
115699    FuncDef *pOther;
115700    u8 *z;
115701    pBest->zName = (const char*)&pBest[1];
115702    pBest->nArg = (u16)nArg;
115703    pBest->funcFlags = enc;
115704    memcpy((char*)&pBest[1], zName, nName+1);
115705    for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
115706    pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
115707    if( pOther==pBest ){
115708      sqlite3DbFree(db, pBest);
115709      sqlite3OomFault(db);
115710      return 0;
115711    }else{
115712      pBest->pNext = pOther;
115713    }
115714  }
115715
115716  if( pBest && (pBest->xSFunc || createFlag) ){
115717    return pBest;
115718  }
115719  return 0;
115720}
115721
115722/*
115723** Free all resources held by the schema structure. The void* argument points
115724** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
115725** pointer itself, it just cleans up subsidiary resources (i.e. the contents
115726** of the schema hash tables).
115727**
115728** The Schema.cache_size variable is not cleared.
115729*/
115730SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
115731  Hash temp1;
115732  Hash temp2;
115733  HashElem *pElem;
115734  Schema *pSchema = (Schema *)p;
115735
115736  temp1 = pSchema->tblHash;
115737  temp2 = pSchema->trigHash;
115738  sqlite3HashInit(&pSchema->trigHash);
115739  sqlite3HashClear(&pSchema->idxHash);
115740  for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
115741    sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
115742  }
115743  sqlite3HashClear(&temp2);
115744  sqlite3HashInit(&pSchema->tblHash);
115745  for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
115746    Table *pTab = sqliteHashData(pElem);
115747    sqlite3DeleteTable(0, pTab);
115748  }
115749  sqlite3HashClear(&temp1);
115750  sqlite3HashClear(&pSchema->fkeyHash);
115751  pSchema->pSeqTab = 0;
115752  if( pSchema->schemaFlags & DB_SchemaLoaded ){
115753    pSchema->iGeneration++;
115754  }
115755  pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
115756}
115757
115758/*
115759** Find and return the schema associated with a BTree.  Create
115760** a new one if necessary.
115761*/
115762SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
115763  Schema * p;
115764  if( pBt ){
115765    p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
115766  }else{
115767    p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
115768  }
115769  if( !p ){
115770    sqlite3OomFault(db);
115771  }else if ( 0==p->file_format ){
115772    sqlite3HashInit(&p->tblHash);
115773    sqlite3HashInit(&p->idxHash);
115774    sqlite3HashInit(&p->trigHash);
115775    sqlite3HashInit(&p->fkeyHash);
115776    p->enc = SQLITE_UTF8;
115777  }
115778  return p;
115779}
115780
115781/************** End of callback.c ********************************************/
115782/************** Begin file delete.c ******************************************/
115783/*
115784** 2001 September 15
115785**
115786** The author disclaims copyright to this source code.  In place of
115787** a legal notice, here is a blessing:
115788**
115789**    May you do good and not evil.
115790**    May you find forgiveness for yourself and forgive others.
115791**    May you share freely, never taking more than you give.
115792**
115793*************************************************************************
115794** This file contains C code routines that are called by the parser
115795** in order to generate code for DELETE FROM statements.
115796*/
115797/* #include "sqliteInt.h" */
115798
115799/*
115800** While a SrcList can in general represent multiple tables and subqueries
115801** (as in the FROM clause of a SELECT statement) in this case it contains
115802** the name of a single table, as one might find in an INSERT, DELETE,
115803** or UPDATE statement.  Look up that table in the symbol table and
115804** return a pointer.  Set an error message and return NULL if the table
115805** name is not found or if any other error occurs.
115806**
115807** The following fields are initialized appropriate in pSrc:
115808**
115809**    pSrc->a[0].pTab       Pointer to the Table object
115810**    pSrc->a[0].pIndex     Pointer to the INDEXED BY index, if there is one
115811**
115812*/
115813SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
115814  struct SrcList_item *pItem = pSrc->a;
115815  Table *pTab;
115816  assert( pItem && pSrc->nSrc==1 );
115817  pTab = sqlite3LocateTableItem(pParse, 0, pItem);
115818  sqlite3DeleteTable(pParse->db, pItem->pTab);
115819  pItem->pTab = pTab;
115820  if( pTab ){
115821    pTab->nTabRef++;
115822  }
115823  if( sqlite3IndexedByLookup(pParse, pItem) ){
115824    pTab = 0;
115825  }
115826  return pTab;
115827}
115828
115829/* Return true if table pTab is read-only.
115830**
115831** A table is read-only if any of the following are true:
115832**
115833**   1) It is a virtual table and no implementation of the xUpdate method
115834**      has been provided
115835**
115836**   2) It is a system table (i.e. sqlite_master), this call is not
115837**      part of a nested parse and writable_schema pragma has not
115838**      been specified
115839**
115840**   3) The table is a shadow table, the database connection is in
115841**      defensive mode, and the current sqlite3_prepare()
115842**      is for a top-level SQL statement.
115843*/
115844static int tabIsReadOnly(Parse *pParse, Table *pTab){
115845  sqlite3 *db;
115846  if( IsVirtual(pTab) ){
115847    return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0;
115848  }
115849  if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
115850  db = pParse->db;
115851  if( (pTab->tabFlags & TF_Readonly)!=0 ){
115852    return sqlite3WritableSchema(db)==0 && pParse->nested==0;
115853  }
115854  assert( pTab->tabFlags & TF_Shadow );
115855  return sqlite3ReadOnlyShadowTables(db);
115856}
115857
115858/*
115859** Check to make sure the given table is writable.  If it is not
115860** writable, generate an error message and return 1.  If it is
115861** writable return 0;
115862*/
115863SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
115864  if( tabIsReadOnly(pParse, pTab) ){
115865    sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
115866    return 1;
115867  }
115868#ifndef SQLITE_OMIT_VIEW
115869  if( !viewOk && pTab->pSelect ){
115870    sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
115871    return 1;
115872  }
115873#endif
115874  return 0;
115875}
115876
115877
115878#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
115879/*
115880** Evaluate a view and store its result in an ephemeral table.  The
115881** pWhere argument is an optional WHERE clause that restricts the
115882** set of rows in the view that are to be added to the ephemeral table.
115883*/
115884SQLITE_PRIVATE void sqlite3MaterializeView(
115885  Parse *pParse,       /* Parsing context */
115886  Table *pView,        /* View definition */
115887  Expr *pWhere,        /* Optional WHERE clause to be added */
115888  ExprList *pOrderBy,  /* Optional ORDER BY clause */
115889  Expr *pLimit,        /* Optional LIMIT clause */
115890  int iCur             /* Cursor number for ephemeral table */
115891){
115892  SelectDest dest;
115893  Select *pSel;
115894  SrcList *pFrom;
115895  sqlite3 *db = pParse->db;
115896  int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
115897  pWhere = sqlite3ExprDup(db, pWhere, 0);
115898  pFrom = sqlite3SrcListAppend(pParse, 0, 0, 0);
115899  if( pFrom ){
115900    assert( pFrom->nSrc==1 );
115901    pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
115902    pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
115903    assert( pFrom->a[0].pOn==0 );
115904    assert( pFrom->a[0].pUsing==0 );
115905  }
115906  pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, pOrderBy,
115907                          SF_IncludeHidden, pLimit);
115908  sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
115909  sqlite3Select(pParse, pSel, &dest);
115910  sqlite3SelectDelete(db, pSel);
115911}
115912#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
115913
115914#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
115915/*
115916** Generate an expression tree to implement the WHERE, ORDER BY,
115917** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
115918**
115919**     DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
115920**                            \__________________________/
115921**                               pLimitWhere (pInClause)
115922*/
115923SQLITE_PRIVATE Expr *sqlite3LimitWhere(
115924  Parse *pParse,               /* The parser context */
115925  SrcList *pSrc,               /* the FROM clause -- which tables to scan */
115926  Expr *pWhere,                /* The WHERE clause.  May be null */
115927  ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
115928  Expr *pLimit,                /* The LIMIT clause.  May be null */
115929  char *zStmtType              /* Either DELETE or UPDATE.  For err msgs. */
115930){
115931  sqlite3 *db = pParse->db;
115932  Expr *pLhs = NULL;           /* LHS of IN(SELECT...) operator */
115933  Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
115934  ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
115935  SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
115936  Select *pSelect = NULL;      /* Complete SELECT tree */
115937  Table *pTab;
115938
115939  /* Check that there isn't an ORDER BY without a LIMIT clause.
115940  */
115941  if( pOrderBy && pLimit==0 ) {
115942    sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
115943    sqlite3ExprDelete(pParse->db, pWhere);
115944    sqlite3ExprListDelete(pParse->db, pOrderBy);
115945    return 0;
115946  }
115947
115948  /* We only need to generate a select expression if there
115949  ** is a limit/offset term to enforce.
115950  */
115951  if( pLimit == 0 ) {
115952    return pWhere;
115953  }
115954
115955  /* Generate a select expression tree to enforce the limit/offset
115956  ** term for the DELETE or UPDATE statement.  For example:
115957  **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
115958  ** becomes:
115959  **   DELETE FROM table_a WHERE rowid IN (
115960  **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
115961  **   );
115962  */
115963
115964  pTab = pSrc->a[0].pTab;
115965  if( HasRowid(pTab) ){
115966    pLhs = sqlite3PExpr(pParse, TK_ROW, 0, 0);
115967    pEList = sqlite3ExprListAppend(
115968        pParse, 0, sqlite3PExpr(pParse, TK_ROW, 0, 0)
115969    );
115970  }else{
115971    Index *pPk = sqlite3PrimaryKeyIndex(pTab);
115972    if( pPk->nKeyCol==1 ){
115973      const char *zName = pTab->aCol[pPk->aiColumn[0]].zName;
115974      pLhs = sqlite3Expr(db, TK_ID, zName);
115975      pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, zName));
115976    }else{
115977      int i;
115978      for(i=0; i<pPk->nKeyCol; i++){
115979        Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zName);
115980        pEList = sqlite3ExprListAppend(pParse, pEList, p);
115981      }
115982      pLhs = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
115983      if( pLhs ){
115984        pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
115985      }
115986    }
115987  }
115988
115989  /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
115990  ** and the SELECT subtree. */
115991  pSrc->a[0].pTab = 0;
115992  pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
115993  pSrc->a[0].pTab = pTab;
115994  pSrc->a[0].pIBIndex = 0;
115995
115996  /* generate the SELECT expression tree. */
115997  pSelect = sqlite3SelectNew(pParse, pEList, pSelectSrc, pWhere, 0 ,0,
115998      pOrderBy,0,pLimit
115999  );
116000
116001  /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
116002  pInClause = sqlite3PExpr(pParse, TK_IN, pLhs, 0);
116003  sqlite3PExprAddSelect(pParse, pInClause, pSelect);
116004  return pInClause;
116005}
116006#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
116007       /*      && !defined(SQLITE_OMIT_SUBQUERY) */
116008
116009/*
116010** Generate code for a DELETE FROM statement.
116011**
116012**     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
116013**                 \________/       \________________/
116014**                  pTabList              pWhere
116015*/
116016SQLITE_PRIVATE void sqlite3DeleteFrom(
116017  Parse *pParse,         /* The parser context */
116018  SrcList *pTabList,     /* The table from which we should delete things */
116019  Expr *pWhere,          /* The WHERE clause.  May be null */
116020  ExprList *pOrderBy,    /* ORDER BY clause. May be null */
116021  Expr *pLimit           /* LIMIT clause. May be null */
116022){
116023  Vdbe *v;               /* The virtual database engine */
116024  Table *pTab;           /* The table from which records will be deleted */
116025  int i;                 /* Loop counter */
116026  WhereInfo *pWInfo;     /* Information about the WHERE clause */
116027  Index *pIdx;           /* For looping over indices of the table */
116028  int iTabCur;           /* Cursor number for the table */
116029  int iDataCur = 0;      /* VDBE cursor for the canonical data source */
116030  int iIdxCur = 0;       /* Cursor number of the first index */
116031  int nIdx;              /* Number of indices */
116032  sqlite3 *db;           /* Main database structure */
116033  AuthContext sContext;  /* Authorization context */
116034  NameContext sNC;       /* Name context to resolve expressions in */
116035  int iDb;               /* Database number */
116036  int memCnt = 0;        /* Memory cell used for change counting */
116037  int rcauth;            /* Value returned by authorization callback */
116038  int eOnePass;          /* ONEPASS_OFF or _SINGLE or _MULTI */
116039  int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
116040  u8 *aToOpen = 0;       /* Open cursor iTabCur+j if aToOpen[j] is true */
116041  Index *pPk;            /* The PRIMARY KEY index on the table */
116042  int iPk = 0;           /* First of nPk registers holding PRIMARY KEY value */
116043  i16 nPk = 1;           /* Number of columns in the PRIMARY KEY */
116044  int iKey;              /* Memory cell holding key of row to be deleted */
116045  i16 nKey;              /* Number of memory cells in the row key */
116046  int iEphCur = 0;       /* Ephemeral table holding all primary key values */
116047  int iRowSet = 0;       /* Register for rowset of rows to delete */
116048  int addrBypass = 0;    /* Address of jump over the delete logic */
116049  int addrLoop = 0;      /* Top of the delete loop */
116050  int addrEphOpen = 0;   /* Instruction to open the Ephemeral table */
116051  int bComplex;          /* True if there are triggers or FKs or
116052                         ** subqueries in the WHERE clause */
116053
116054#ifndef SQLITE_OMIT_TRIGGER
116055  int isView;                  /* True if attempting to delete from a view */
116056  Trigger *pTrigger;           /* List of table triggers, if required */
116057#endif
116058
116059  memset(&sContext, 0, sizeof(sContext));
116060  db = pParse->db;
116061  if( pParse->nErr || db->mallocFailed ){
116062    goto delete_from_cleanup;
116063  }
116064  assert( pTabList->nSrc==1 );
116065
116066
116067  /* Locate the table which we want to delete.  This table has to be
116068  ** put in an SrcList structure because some of the subroutines we
116069  ** will be calling are designed to work with multiple tables and expect
116070  ** an SrcList* parameter instead of just a Table* parameter.
116071  */
116072  pTab = sqlite3SrcListLookup(pParse, pTabList);
116073  if( pTab==0 )  goto delete_from_cleanup;
116074
116075  /* Figure out if we have any triggers and if the table being
116076  ** deleted from is a view
116077  */
116078#ifndef SQLITE_OMIT_TRIGGER
116079  pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
116080  isView = pTab->pSelect!=0;
116081#else
116082# define pTrigger 0
116083# define isView 0
116084#endif
116085  bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
116086#ifdef SQLITE_OMIT_VIEW
116087# undef isView
116088# define isView 0
116089#endif
116090
116091#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
116092  if( !isView ){
116093    pWhere = sqlite3LimitWhere(
116094        pParse, pTabList, pWhere, pOrderBy, pLimit, "DELETE"
116095    );
116096    pOrderBy = 0;
116097    pLimit = 0;
116098  }
116099#endif
116100
116101  /* If pTab is really a view, make sure it has been initialized.
116102  */
116103  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
116104    goto delete_from_cleanup;
116105  }
116106
116107  if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
116108    goto delete_from_cleanup;
116109  }
116110  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
116111  assert( iDb<db->nDb );
116112  rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
116113                            db->aDb[iDb].zDbSName);
116114  assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
116115  if( rcauth==SQLITE_DENY ){
116116    goto delete_from_cleanup;
116117  }
116118  assert(!isView || pTrigger);
116119
116120  /* Assign cursor numbers to the table and all its indices.
116121  */
116122  assert( pTabList->nSrc==1 );
116123  iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
116124  for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
116125    pParse->nTab++;
116126  }
116127
116128  /* Start the view context
116129  */
116130  if( isView ){
116131    sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
116132  }
116133
116134  /* Begin generating code.
116135  */
116136  v = sqlite3GetVdbe(pParse);
116137  if( v==0 ){
116138    goto delete_from_cleanup;
116139  }
116140  if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
116141  sqlite3BeginWriteOperation(pParse, bComplex, iDb);
116142
116143  /* If we are trying to delete from a view, realize that view into
116144  ** an ephemeral table.
116145  */
116146#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
116147  if( isView ){
116148    sqlite3MaterializeView(pParse, pTab,
116149        pWhere, pOrderBy, pLimit, iTabCur
116150    );
116151    iDataCur = iIdxCur = iTabCur;
116152    pOrderBy = 0;
116153    pLimit = 0;
116154  }
116155#endif
116156
116157  /* Resolve the column names in the WHERE clause.
116158  */
116159  memset(&sNC, 0, sizeof(sNC));
116160  sNC.pParse = pParse;
116161  sNC.pSrcList = pTabList;
116162  if( sqlite3ResolveExprNames(&sNC, pWhere) ){
116163    goto delete_from_cleanup;
116164  }
116165
116166  /* Initialize the counter of the number of rows deleted, if
116167  ** we are counting rows.
116168  */
116169  if( (db->flags & SQLITE_CountRows)!=0
116170   && !pParse->nested
116171   && !pParse->pTriggerTab
116172  ){
116173    memCnt = ++pParse->nMem;
116174    sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
116175  }
116176
116177#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
116178  /* Special case: A DELETE without a WHERE clause deletes everything.
116179  ** It is easier just to erase the whole table. Prior to version 3.6.5,
116180  ** this optimization caused the row change count (the value returned by
116181  ** API function sqlite3_count_changes) to be set incorrectly.
116182  **
116183  ** The "rcauth==SQLITE_OK" terms is the
116184  ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
116185  ** the callback returns SQLITE_IGNORE then the DELETE operation proceeds but
116186  ** the truncate optimization is disabled and all rows are deleted
116187  ** individually.
116188  */
116189  if( rcauth==SQLITE_OK
116190   && pWhere==0
116191   && !bComplex
116192   && !IsVirtual(pTab)
116193#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
116194   && db->xPreUpdateCallback==0
116195#endif
116196  ){
116197    assert( !isView );
116198    sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
116199    if( HasRowid(pTab) ){
116200      sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
116201                        pTab->zName, P4_STATIC);
116202    }
116203    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
116204      assert( pIdx->pSchema==pTab->pSchema );
116205      sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
116206    }
116207  }else
116208#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
116209  {
116210    u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK|WHERE_SEEK_TABLE;
116211    if( sNC.ncFlags & NC_VarSelect ) bComplex = 1;
116212    wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW);
116213    if( HasRowid(pTab) ){
116214      /* For a rowid table, initialize the RowSet to an empty set */
116215      pPk = 0;
116216      nPk = 1;
116217      iRowSet = ++pParse->nMem;
116218      sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
116219    }else{
116220      /* For a WITHOUT ROWID table, create an ephemeral table used to
116221      ** hold all primary keys for rows to be deleted. */
116222      pPk = sqlite3PrimaryKeyIndex(pTab);
116223      assert( pPk!=0 );
116224      nPk = pPk->nKeyCol;
116225      iPk = pParse->nMem+1;
116226      pParse->nMem += nPk;
116227      iEphCur = pParse->nTab++;
116228      addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk);
116229      sqlite3VdbeSetP4KeyInfo(pParse, pPk);
116230    }
116231
116232    /* Construct a query to find the rowid or primary key for every row
116233    ** to be deleted, based on the WHERE clause. Set variable eOnePass
116234    ** to indicate the strategy used to implement this delete:
116235    **
116236    **  ONEPASS_OFF:    Two-pass approach - use a FIFO for rowids/PK values.
116237    **  ONEPASS_SINGLE: One-pass approach - at most one row deleted.
116238    **  ONEPASS_MULTI:  One-pass approach - any number of rows may be deleted.
116239    */
116240    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1);
116241    if( pWInfo==0 ) goto delete_from_cleanup;
116242    eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
116243    assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
116244    assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF );
116245    if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse);
116246
116247    /* Keep track of the number of rows to be deleted */
116248    if( memCnt ){
116249      sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
116250    }
116251
116252    /* Extract the rowid or primary key for the current row */
116253    if( pPk ){
116254      for(i=0; i<nPk; i++){
116255        assert( pPk->aiColumn[i]>=0 );
116256        sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
116257                                        pPk->aiColumn[i], iPk+i);
116258      }
116259      iKey = iPk;
116260    }else{
116261      iKey = ++pParse->nMem;
116262      sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
116263    }
116264
116265    if( eOnePass!=ONEPASS_OFF ){
116266      /* For ONEPASS, no need to store the rowid/primary-key. There is only
116267      ** one, so just keep it in its register(s) and fall through to the
116268      ** delete code.  */
116269      nKey = nPk; /* OP_Found will use an unpacked key */
116270      aToOpen = sqlite3DbMallocRawNN(db, nIdx+2);
116271      if( aToOpen==0 ){
116272        sqlite3WhereEnd(pWInfo);
116273        goto delete_from_cleanup;
116274      }
116275      memset(aToOpen, 1, nIdx+1);
116276      aToOpen[nIdx+1] = 0;
116277      if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
116278      if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
116279      if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
116280    }else{
116281      if( pPk ){
116282        /* Add the PK key for this row to the temporary table */
116283        iKey = ++pParse->nMem;
116284        nKey = 0;   /* Zero tells OP_Found to use a composite key */
116285        sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
116286            sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
116287        sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEphCur, iKey, iPk, nPk);
116288      }else{
116289        /* Add the rowid of the row to be deleted to the RowSet */
116290        nKey = 1;  /* OP_DeferredSeek always uses a single rowid */
116291        sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
116292      }
116293    }
116294
116295    /* If this DELETE cannot use the ONEPASS strategy, this is the
116296    ** end of the WHERE loop */
116297    if( eOnePass!=ONEPASS_OFF ){
116298      addrBypass = sqlite3VdbeMakeLabel(pParse);
116299    }else{
116300      sqlite3WhereEnd(pWInfo);
116301    }
116302
116303    /* Unless this is a view, open cursors for the table we are
116304    ** deleting from and all its indices. If this is a view, then the
116305    ** only effect this statement has is to fire the INSTEAD OF
116306    ** triggers.
116307    */
116308    if( !isView ){
116309      int iAddrOnce = 0;
116310      if( eOnePass==ONEPASS_MULTI ){
116311        iAddrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
116312      }
116313      testcase( IsVirtual(pTab) );
116314      sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, OPFLAG_FORDELETE,
116315                                 iTabCur, aToOpen, &iDataCur, &iIdxCur);
116316      assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
116317      assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
116318      if( eOnePass==ONEPASS_MULTI ){
116319        sqlite3VdbeJumpHereOrPopInst(v, iAddrOnce);
116320      }
116321    }
116322
116323    /* Set up a loop over the rowids/primary-keys that were found in the
116324    ** where-clause loop above.
116325    */
116326    if( eOnePass!=ONEPASS_OFF ){
116327      assert( nKey==nPk );  /* OP_Found will use an unpacked key */
116328      if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
116329        assert( pPk!=0 || pTab->pSelect!=0 );
116330        sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
116331        VdbeCoverage(v);
116332      }
116333    }else if( pPk ){
116334      addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);
116335      if( IsVirtual(pTab) ){
116336        sqlite3VdbeAddOp3(v, OP_Column, iEphCur, 0, iKey);
116337      }else{
116338        sqlite3VdbeAddOp2(v, OP_RowData, iEphCur, iKey);
116339      }
116340      assert( nKey==0 );  /* OP_Found will use a composite key */
116341    }else{
116342      addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
116343      VdbeCoverage(v);
116344      assert( nKey==1 );
116345    }
116346
116347    /* Delete the row */
116348#ifndef SQLITE_OMIT_VIRTUALTABLE
116349    if( IsVirtual(pTab) ){
116350      const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
116351      sqlite3VtabMakeWritable(pParse, pTab);
116352      assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
116353      sqlite3MayAbort(pParse);
116354      if( eOnePass==ONEPASS_SINGLE ){
116355        sqlite3VdbeAddOp1(v, OP_Close, iTabCur);
116356        if( sqlite3IsToplevel(pParse) ){
116357          pParse->isMultiWrite = 0;
116358        }
116359      }
116360      sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
116361      sqlite3VdbeChangeP5(v, OE_Abort);
116362    }else
116363#endif
116364    {
116365      int count = (pParse->nested==0);    /* True to count changes */
116366      sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
116367          iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
116368    }
116369
116370    /* End of the loop over all rowids/primary-keys. */
116371    if( eOnePass!=ONEPASS_OFF ){
116372      sqlite3VdbeResolveLabel(v, addrBypass);
116373      sqlite3WhereEnd(pWInfo);
116374    }else if( pPk ){
116375      sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1); VdbeCoverage(v);
116376      sqlite3VdbeJumpHere(v, addrLoop);
116377    }else{
116378      sqlite3VdbeGoto(v, addrLoop);
116379      sqlite3VdbeJumpHere(v, addrLoop);
116380    }
116381  } /* End non-truncate path */
116382
116383  /* Update the sqlite_sequence table by storing the content of the
116384  ** maximum rowid counter values recorded while inserting into
116385  ** autoincrement tables.
116386  */
116387  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
116388    sqlite3AutoincrementEnd(pParse);
116389  }
116390
116391  /* Return the number of rows that were deleted. If this routine is
116392  ** generating code because of a call to sqlite3NestedParse(), do not
116393  ** invoke the callback function.
116394  */
116395  if( memCnt ){
116396    sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
116397    sqlite3VdbeSetNumCols(v, 1);
116398    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
116399  }
116400
116401delete_from_cleanup:
116402  sqlite3AuthContextPop(&sContext);
116403  sqlite3SrcListDelete(db, pTabList);
116404  sqlite3ExprDelete(db, pWhere);
116405#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
116406  sqlite3ExprListDelete(db, pOrderBy);
116407  sqlite3ExprDelete(db, pLimit);
116408#endif
116409  sqlite3DbFree(db, aToOpen);
116410  return;
116411}
116412/* Make sure "isView" and other macros defined above are undefined. Otherwise
116413** they may interfere with compilation of other functions in this file
116414** (or in another file, if this file becomes part of the amalgamation).  */
116415#ifdef isView
116416 #undef isView
116417#endif
116418#ifdef pTrigger
116419 #undef pTrigger
116420#endif
116421
116422/*
116423** This routine generates VDBE code that causes a single row of a
116424** single table to be deleted.  Both the original table entry and
116425** all indices are removed.
116426**
116427** Preconditions:
116428**
116429**   1.  iDataCur is an open cursor on the btree that is the canonical data
116430**       store for the table.  (This will be either the table itself,
116431**       in the case of a rowid table, or the PRIMARY KEY index in the case
116432**       of a WITHOUT ROWID table.)
116433**
116434**   2.  Read/write cursors for all indices of pTab must be open as
116435**       cursor number iIdxCur+i for the i-th index.
116436**
116437**   3.  The primary key for the row to be deleted must be stored in a
116438**       sequence of nPk memory cells starting at iPk.  If nPk==0 that means
116439**       that a search record formed from OP_MakeRecord is contained in the
116440**       single memory location iPk.
116441**
116442** eMode:
116443**   Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or
116444**   ONEPASS_MULTI.  If eMode is not ONEPASS_OFF, then the cursor
116445**   iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
116446**   then this function must seek iDataCur to the entry identified by iPk
116447**   and nPk before reading from it.
116448**
116449**   If eMode is ONEPASS_MULTI, then this call is being made as part
116450**   of a ONEPASS delete that affects multiple rows. In this case, if
116451**   iIdxNoSeek is a valid cursor number (>=0) and is not the same as
116452**   iDataCur, then its position should be preserved following the delete
116453**   operation. Or, if iIdxNoSeek is not a valid cursor number, the
116454**   position of iDataCur should be preserved instead.
116455**
116456** iIdxNoSeek:
116457**   If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
116458**   then it identifies an index cursor (from within array of cursors
116459**   starting at iIdxCur) that already points to the index entry to be deleted.
116460**   Except, this optimization is disabled if there are BEFORE triggers since
116461**   the trigger body might have moved the cursor.
116462*/
116463SQLITE_PRIVATE void sqlite3GenerateRowDelete(
116464  Parse *pParse,     /* Parsing context */
116465  Table *pTab,       /* Table containing the row to be deleted */
116466  Trigger *pTrigger, /* List of triggers to (potentially) fire */
116467  int iDataCur,      /* Cursor from which column data is extracted */
116468  int iIdxCur,       /* First index cursor */
116469  int iPk,           /* First memory cell containing the PRIMARY KEY */
116470  i16 nPk,           /* Number of PRIMARY KEY memory cells */
116471  u8 count,          /* If non-zero, increment the row change counter */
116472  u8 onconf,         /* Default ON CONFLICT policy for triggers */
116473  u8 eMode,          /* ONEPASS_OFF, _SINGLE, or _MULTI.  See above */
116474  int iIdxNoSeek     /* Cursor number of cursor that does not need seeking */
116475){
116476  Vdbe *v = pParse->pVdbe;        /* Vdbe */
116477  int iOld = 0;                   /* First register in OLD.* array */
116478  int iLabel;                     /* Label resolved to end of generated code */
116479  u8 opSeek;                      /* Seek opcode */
116480
116481  /* Vdbe is guaranteed to have been allocated by this stage. */
116482  assert( v );
116483  VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
116484                         iDataCur, iIdxCur, iPk, (int)nPk));
116485
116486  /* Seek cursor iCur to the row to delete. If this row no longer exists
116487  ** (this can happen if a trigger program has already deleted it), do
116488  ** not attempt to delete it or fire any DELETE triggers.  */
116489  iLabel = sqlite3VdbeMakeLabel(pParse);
116490  opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
116491  if( eMode==ONEPASS_OFF ){
116492    sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
116493    VdbeCoverageIf(v, opSeek==OP_NotExists);
116494    VdbeCoverageIf(v, opSeek==OP_NotFound);
116495  }
116496
116497  /* If there are any triggers to fire, allocate a range of registers to
116498  ** use for the old.* references in the triggers.  */
116499  if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
116500    u32 mask;                     /* Mask of OLD.* columns in use */
116501    int iCol;                     /* Iterator used while populating OLD.* */
116502    int addrStart;                /* Start of BEFORE trigger programs */
116503
116504    /* TODO: Could use temporary registers here. Also could attempt to
116505    ** avoid copying the contents of the rowid register.  */
116506    mask = sqlite3TriggerColmask(
116507        pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
116508    );
116509    mask |= sqlite3FkOldmask(pParse, pTab);
116510    iOld = pParse->nMem+1;
116511    pParse->nMem += (1 + pTab->nCol);
116512
116513    /* Populate the OLD.* pseudo-table register array. These values will be
116514    ** used by any BEFORE and AFTER triggers that exist.  */
116515    sqlite3VdbeAddOp2(v, OP_Copy, iPk, iOld);
116516    for(iCol=0; iCol<pTab->nCol; iCol++){
116517      testcase( mask!=0xffffffff && iCol==31 );
116518      testcase( mask!=0xffffffff && iCol==32 );
116519      if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol))!=0) ){
116520        int kk = sqlite3TableColumnToStorage(pTab, iCol);
116521        sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+kk+1);
116522      }
116523    }
116524
116525    /* Invoke BEFORE DELETE trigger programs. */
116526    addrStart = sqlite3VdbeCurrentAddr(v);
116527    sqlite3CodeRowTrigger(pParse, pTrigger,
116528        TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
116529    );
116530
116531    /* If any BEFORE triggers were coded, then seek the cursor to the
116532    ** row to be deleted again. It may be that the BEFORE triggers moved
116533    ** the cursor or already deleted the row that the cursor was
116534    ** pointing to.
116535    **
116536    ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
116537    ** may have moved that cursor.
116538    */
116539    if( addrStart<sqlite3VdbeCurrentAddr(v) ){
116540      sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
116541      VdbeCoverageIf(v, opSeek==OP_NotExists);
116542      VdbeCoverageIf(v, opSeek==OP_NotFound);
116543      testcase( iIdxNoSeek>=0 );
116544      iIdxNoSeek = -1;
116545    }
116546
116547    /* Do FK processing. This call checks that any FK constraints that
116548    ** refer to this table (i.e. constraints attached to other tables)
116549    ** are not violated by deleting this row.  */
116550    sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
116551  }
116552
116553  /* Delete the index and table entries. Skip this step if pTab is really
116554  ** a view (in which case the only effect of the DELETE statement is to
116555  ** fire the INSTEAD OF triggers).
116556  **
116557  ** If variable 'count' is non-zero, then this OP_Delete instruction should
116558  ** invoke the update-hook. The pre-update-hook, on the other hand should
116559  ** be invoked unless table pTab is a system table. The difference is that
116560  ** the update-hook is not invoked for rows removed by REPLACE, but the
116561  ** pre-update-hook is.
116562  */
116563  if( pTab->pSelect==0 ){
116564    u8 p5 = 0;
116565    sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
116566    sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
116567    if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
116568      sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
116569    }
116570    if( eMode!=ONEPASS_OFF ){
116571      sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
116572    }
116573    if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
116574      sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
116575    }
116576    if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
116577    sqlite3VdbeChangeP5(v, p5);
116578  }
116579
116580  /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
116581  ** handle rows (possibly in other tables) that refer via a foreign key
116582  ** to the row just deleted. */
116583  sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
116584
116585  /* Invoke AFTER DELETE trigger programs. */
116586  sqlite3CodeRowTrigger(pParse, pTrigger,
116587      TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
116588  );
116589
116590  /* Jump here if the row had already been deleted before any BEFORE
116591  ** trigger programs were invoked. Or if a trigger program throws a
116592  ** RAISE(IGNORE) exception.  */
116593  sqlite3VdbeResolveLabel(v, iLabel);
116594  VdbeModuleComment((v, "END: GenRowDel()"));
116595}
116596
116597/*
116598** This routine generates VDBE code that causes the deletion of all
116599** index entries associated with a single row of a single table, pTab
116600**
116601** Preconditions:
116602**
116603**   1.  A read/write cursor "iDataCur" must be open on the canonical storage
116604**       btree for the table pTab.  (This will be either the table itself
116605**       for rowid tables or to the primary key index for WITHOUT ROWID
116606**       tables.)
116607**
116608**   2.  Read/write cursors for all indices of pTab must be open as
116609**       cursor number iIdxCur+i for the i-th index.  (The pTab->pIndex
116610**       index is the 0-th index.)
116611**
116612**   3.  The "iDataCur" cursor must be already be positioned on the row
116613**       that is to be deleted.
116614*/
116615SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
116616  Parse *pParse,     /* Parsing and code generating context */
116617  Table *pTab,       /* Table containing the row to be deleted */
116618  int iDataCur,      /* Cursor of table holding data. */
116619  int iIdxCur,       /* First index cursor */
116620  int *aRegIdx,      /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
116621  int iIdxNoSeek     /* Do not delete from this cursor */
116622){
116623  int i;             /* Index loop counter */
116624  int r1 = -1;       /* Register holding an index key */
116625  int iPartIdxLabel; /* Jump destination for skipping partial index entries */
116626  Index *pIdx;       /* Current index */
116627  Index *pPrior = 0; /* Prior index */
116628  Vdbe *v;           /* The prepared statement under construction */
116629  Index *pPk;        /* PRIMARY KEY index, or NULL for rowid tables */
116630
116631  v = pParse->pVdbe;
116632  pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
116633  for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
116634    assert( iIdxCur+i!=iDataCur || pPk==pIdx );
116635    if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
116636    if( pIdx==pPk ) continue;
116637    if( iIdxCur+i==iIdxNoSeek ) continue;
116638    VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
116639    r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
116640        &iPartIdxLabel, pPrior, r1);
116641    sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
116642        pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
116643    sqlite3VdbeChangeP5(v, 1);  /* Cause IdxDelete to error if no entry found */
116644    sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
116645    pPrior = pIdx;
116646  }
116647}
116648
116649/*
116650** Generate code that will assemble an index key and stores it in register
116651** regOut.  The key with be for index pIdx which is an index on pTab.
116652** iCur is the index of a cursor open on the pTab table and pointing to
116653** the entry that needs indexing.  If pTab is a WITHOUT ROWID table, then
116654** iCur must be the cursor of the PRIMARY KEY index.
116655**
116656** Return a register number which is the first in a block of
116657** registers that holds the elements of the index key.  The
116658** block of registers has already been deallocated by the time
116659** this routine returns.
116660**
116661** If *piPartIdxLabel is not NULL, fill it in with a label and jump
116662** to that label if pIdx is a partial index that should be skipped.
116663** The label should be resolved using sqlite3ResolvePartIdxLabel().
116664** A partial index should be skipped if its WHERE clause evaluates
116665** to false or null.  If pIdx is not a partial index, *piPartIdxLabel
116666** will be set to zero which is an empty label that is ignored by
116667** sqlite3ResolvePartIdxLabel().
116668**
116669** The pPrior and regPrior parameters are used to implement a cache to
116670** avoid unnecessary register loads.  If pPrior is not NULL, then it is
116671** a pointer to a different index for which an index key has just been
116672** computed into register regPrior.  If the current pIdx index is generating
116673** its key into the same sequence of registers and if pPrior and pIdx share
116674** a column in common, then the register corresponding to that column already
116675** holds the correct value and the loading of that register is skipped.
116676** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
116677** on a table with multiple indices, and especially with the ROWID or
116678** PRIMARY KEY columns of the index.
116679*/
116680SQLITE_PRIVATE int sqlite3GenerateIndexKey(
116681  Parse *pParse,       /* Parsing context */
116682  Index *pIdx,         /* The index for which to generate a key */
116683  int iDataCur,        /* Cursor number from which to take column data */
116684  int regOut,          /* Put the new key into this register if not 0 */
116685  int prefixOnly,      /* Compute only a unique prefix of the key */
116686  int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
116687  Index *pPrior,       /* Previously generated index key */
116688  int regPrior         /* Register holding previous generated key */
116689){
116690  Vdbe *v = pParse->pVdbe;
116691  int j;
116692  int regBase;
116693  int nCol;
116694
116695  if( piPartIdxLabel ){
116696    if( pIdx->pPartIdxWhere ){
116697      *piPartIdxLabel = sqlite3VdbeMakeLabel(pParse);
116698      pParse->iSelfTab = iDataCur + 1;
116699      sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
116700                            SQLITE_JUMPIFNULL);
116701      pParse->iSelfTab = 0;
116702      pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
116703                  ** pPartIdxWhere may have corrupted regPrior registers */
116704    }else{
116705      *piPartIdxLabel = 0;
116706    }
116707  }
116708  nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
116709  regBase = sqlite3GetTempRange(pParse, nCol);
116710  if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
116711  for(j=0; j<nCol; j++){
116712    if( pPrior
116713     && pPrior->aiColumn[j]==pIdx->aiColumn[j]
116714     && pPrior->aiColumn[j]!=XN_EXPR
116715    ){
116716      /* This column was already computed by the previous index */
116717      continue;
116718    }
116719    sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iDataCur, j, regBase+j);
116720    /* If the column affinity is REAL but the number is an integer, then it
116721    ** might be stored in the table as an integer (using a compact
116722    ** representation) then converted to REAL by an OP_RealAffinity opcode.
116723    ** But we are getting ready to store this value back into an index, where
116724    ** it should be converted by to INTEGER again.  So omit the OP_RealAffinity
116725    ** opcode if it is present */
116726    sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
116727  }
116728  if( regOut ){
116729    sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
116730    if( pIdx->pTable->pSelect ){
116731      const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx);
116732      sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
116733    }
116734  }
116735  sqlite3ReleaseTempRange(pParse, regBase, nCol);
116736  return regBase;
116737}
116738
116739/*
116740** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
116741** because it was a partial index, then this routine should be called to
116742** resolve that label.
116743*/
116744SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
116745  if( iLabel ){
116746    sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
116747  }
116748}
116749
116750/************** End of delete.c **********************************************/
116751/************** Begin file func.c ********************************************/
116752/*
116753** 2002 February 23
116754**
116755** The author disclaims copyright to this source code.  In place of
116756** a legal notice, here is a blessing:
116757**
116758**    May you do good and not evil.
116759**    May you find forgiveness for yourself and forgive others.
116760**    May you share freely, never taking more than you give.
116761**
116762*************************************************************************
116763** This file contains the C-language implementations for many of the SQL
116764** functions of SQLite.  (Some function, and in particular the date and
116765** time functions, are implemented separately.)
116766*/
116767/* #include "sqliteInt.h" */
116768/* #include <stdlib.h> */
116769/* #include <assert.h> */
116770#ifndef SQLITE_OMIT_FLOATING_POINT
116771/* #include <math.h> */
116772#endif
116773/* #include "vdbeInt.h" */
116774
116775/*
116776** Return the collating function associated with a function.
116777*/
116778static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
116779  VdbeOp *pOp;
116780  assert( context->pVdbe!=0 );
116781  pOp = &context->pVdbe->aOp[context->iOp-1];
116782  assert( pOp->opcode==OP_CollSeq );
116783  assert( pOp->p4type==P4_COLLSEQ );
116784  return pOp->p4.pColl;
116785}
116786
116787/*
116788** Indicate that the accumulator load should be skipped on this
116789** iteration of the aggregate loop.
116790*/
116791static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
116792  assert( context->isError<=0 );
116793  context->isError = -1;
116794  context->skipFlag = 1;
116795}
116796
116797/*
116798** Implementation of the non-aggregate min() and max() functions
116799*/
116800static void minmaxFunc(
116801  sqlite3_context *context,
116802  int argc,
116803  sqlite3_value **argv
116804){
116805  int i;
116806  int mask;    /* 0 for min() or 0xffffffff for max() */
116807  int iBest;
116808  CollSeq *pColl;
116809
116810  assert( argc>1 );
116811  mask = sqlite3_user_data(context)==0 ? 0 : -1;
116812  pColl = sqlite3GetFuncCollSeq(context);
116813  assert( pColl );
116814  assert( mask==-1 || mask==0 );
116815  iBest = 0;
116816  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
116817  for(i=1; i<argc; i++){
116818    if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
116819    if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
116820      testcase( mask==0 );
116821      iBest = i;
116822    }
116823  }
116824  sqlite3_result_value(context, argv[iBest]);
116825}
116826
116827/*
116828** Return the type of the argument.
116829*/
116830static void typeofFunc(
116831  sqlite3_context *context,
116832  int NotUsed,
116833  sqlite3_value **argv
116834){
116835  static const char *azType[] = { "integer", "real", "text", "blob", "null" };
116836  int i = sqlite3_value_type(argv[0]) - 1;
116837  UNUSED_PARAMETER(NotUsed);
116838  assert( i>=0 && i<ArraySize(azType) );
116839  assert( SQLITE_INTEGER==1 );
116840  assert( SQLITE_FLOAT==2 );
116841  assert( SQLITE_TEXT==3 );
116842  assert( SQLITE_BLOB==4 );
116843  assert( SQLITE_NULL==5 );
116844  /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
116845  ** the datatype code for the initial datatype of the sqlite3_value object
116846  ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT,
116847  ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */
116848  sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
116849}
116850
116851
116852/*
116853** Implementation of the length() function
116854*/
116855static void lengthFunc(
116856  sqlite3_context *context,
116857  int argc,
116858  sqlite3_value **argv
116859){
116860  assert( argc==1 );
116861  UNUSED_PARAMETER(argc);
116862  switch( sqlite3_value_type(argv[0]) ){
116863    case SQLITE_BLOB:
116864    case SQLITE_INTEGER:
116865    case SQLITE_FLOAT: {
116866      sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
116867      break;
116868    }
116869    case SQLITE_TEXT: {
116870      const unsigned char *z = sqlite3_value_text(argv[0]);
116871      const unsigned char *z0;
116872      unsigned char c;
116873      if( z==0 ) return;
116874      z0 = z;
116875      while( (c = *z)!=0 ){
116876        z++;
116877        if( c>=0xc0 ){
116878          while( (*z & 0xc0)==0x80 ){ z++; z0++; }
116879        }
116880      }
116881      sqlite3_result_int(context, (int)(z-z0));
116882      break;
116883    }
116884    default: {
116885      sqlite3_result_null(context);
116886      break;
116887    }
116888  }
116889}
116890
116891/*
116892** Implementation of the abs() function.
116893**
116894** IMP: R-23979-26855 The abs(X) function returns the absolute value of
116895** the numeric argument X.
116896*/
116897static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
116898  assert( argc==1 );
116899  UNUSED_PARAMETER(argc);
116900  switch( sqlite3_value_type(argv[0]) ){
116901    case SQLITE_INTEGER: {
116902      i64 iVal = sqlite3_value_int64(argv[0]);
116903      if( iVal<0 ){
116904        if( iVal==SMALLEST_INT64 ){
116905          /* IMP: R-31676-45509 If X is the integer -9223372036854775808
116906          ** then abs(X) throws an integer overflow error since there is no
116907          ** equivalent positive 64-bit two complement value. */
116908          sqlite3_result_error(context, "integer overflow", -1);
116909          return;
116910        }
116911        iVal = -iVal;
116912      }
116913      sqlite3_result_int64(context, iVal);
116914      break;
116915    }
116916    case SQLITE_NULL: {
116917      /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
116918      sqlite3_result_null(context);
116919      break;
116920    }
116921    default: {
116922      /* Because sqlite3_value_double() returns 0.0 if the argument is not
116923      ** something that can be converted into a number, we have:
116924      ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
116925      ** that cannot be converted to a numeric value.
116926      */
116927      double rVal = sqlite3_value_double(argv[0]);
116928      if( rVal<0 ) rVal = -rVal;
116929      sqlite3_result_double(context, rVal);
116930      break;
116931    }
116932  }
116933}
116934
116935/*
116936** Implementation of the instr() function.
116937**
116938** instr(haystack,needle) finds the first occurrence of needle
116939** in haystack and returns the number of previous characters plus 1,
116940** or 0 if needle does not occur within haystack.
116941**
116942** If both haystack and needle are BLOBs, then the result is one more than
116943** the number of bytes in haystack prior to the first occurrence of needle,
116944** or 0 if needle never occurs in haystack.
116945*/
116946static void instrFunc(
116947  sqlite3_context *context,
116948  int argc,
116949  sqlite3_value **argv
116950){
116951  const unsigned char *zHaystack;
116952  const unsigned char *zNeedle;
116953  int nHaystack;
116954  int nNeedle;
116955  int typeHaystack, typeNeedle;
116956  int N = 1;
116957  int isText;
116958  unsigned char firstChar;
116959  sqlite3_value *pC1 = 0;
116960  sqlite3_value *pC2 = 0;
116961
116962  UNUSED_PARAMETER(argc);
116963  typeHaystack = sqlite3_value_type(argv[0]);
116964  typeNeedle = sqlite3_value_type(argv[1]);
116965  if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
116966  nHaystack = sqlite3_value_bytes(argv[0]);
116967  nNeedle = sqlite3_value_bytes(argv[1]);
116968  if( nNeedle>0 ){
116969    if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
116970      zHaystack = sqlite3_value_blob(argv[0]);
116971      zNeedle = sqlite3_value_blob(argv[1]);
116972      isText = 0;
116973    }else if( typeHaystack!=SQLITE_BLOB && typeNeedle!=SQLITE_BLOB ){
116974      zHaystack = sqlite3_value_text(argv[0]);
116975      zNeedle = sqlite3_value_text(argv[1]);
116976      isText = 1;
116977    }else{
116978      pC1 = sqlite3_value_dup(argv[0]);
116979      zHaystack = sqlite3_value_text(pC1);
116980      if( zHaystack==0 ) goto endInstrOOM;
116981      nHaystack = sqlite3_value_bytes(pC1);
116982      pC2 = sqlite3_value_dup(argv[1]);
116983      zNeedle = sqlite3_value_text(pC2);
116984      if( zNeedle==0 ) goto endInstrOOM;
116985      nNeedle = sqlite3_value_bytes(pC2);
116986      isText = 1;
116987    }
116988    if( zNeedle==0 || (nHaystack && zHaystack==0) ) goto endInstrOOM;
116989    firstChar = zNeedle[0];
116990    while( nNeedle<=nHaystack
116991       && (zHaystack[0]!=firstChar || memcmp(zHaystack, zNeedle, nNeedle)!=0)
116992    ){
116993      N++;
116994      do{
116995        nHaystack--;
116996        zHaystack++;
116997      }while( isText && (zHaystack[0]&0xc0)==0x80 );
116998    }
116999    if( nNeedle>nHaystack ) N = 0;
117000  }
117001  sqlite3_result_int(context, N);
117002endInstr:
117003  sqlite3_value_free(pC1);
117004  sqlite3_value_free(pC2);
117005  return;
117006endInstrOOM:
117007  sqlite3_result_error_nomem(context);
117008  goto endInstr;
117009}
117010
117011/*
117012** Implementation of the printf() function.
117013*/
117014static void printfFunc(
117015  sqlite3_context *context,
117016  int argc,
117017  sqlite3_value **argv
117018){
117019  PrintfArguments x;
117020  StrAccum str;
117021  const char *zFormat;
117022  int n;
117023  sqlite3 *db = sqlite3_context_db_handle(context);
117024
117025  if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
117026    x.nArg = argc-1;
117027    x.nUsed = 0;
117028    x.apArg = argv+1;
117029    sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
117030    str.printfFlags = SQLITE_PRINTF_SQLFUNC;
117031    sqlite3_str_appendf(&str, zFormat, &x);
117032    n = str.nChar;
117033    sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
117034                        SQLITE_DYNAMIC);
117035  }
117036}
117037
117038/*
117039** Implementation of the substr() function.
117040**
117041** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
117042** p1 is 1-indexed.  So substr(x,1,1) returns the first character
117043** of x.  If x is text, then we actually count UTF-8 characters.
117044** If x is a blob, then we count bytes.
117045**
117046** If p1 is negative, then we begin abs(p1) from the end of x[].
117047**
117048** If p2 is negative, return the p2 characters preceding p1.
117049*/
117050static void substrFunc(
117051  sqlite3_context *context,
117052  int argc,
117053  sqlite3_value **argv
117054){
117055  const unsigned char *z;
117056  const unsigned char *z2;
117057  int len;
117058  int p0type;
117059  i64 p1, p2;
117060  int negP2 = 0;
117061
117062  assert( argc==3 || argc==2 );
117063  if( sqlite3_value_type(argv[1])==SQLITE_NULL
117064   || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
117065  ){
117066    return;
117067  }
117068  p0type = sqlite3_value_type(argv[0]);
117069  p1 = sqlite3_value_int(argv[1]);
117070  if( p0type==SQLITE_BLOB ){
117071    len = sqlite3_value_bytes(argv[0]);
117072    z = sqlite3_value_blob(argv[0]);
117073    if( z==0 ) return;
117074    assert( len==sqlite3_value_bytes(argv[0]) );
117075  }else{
117076    z = sqlite3_value_text(argv[0]);
117077    if( z==0 ) return;
117078    len = 0;
117079    if( p1<0 ){
117080      for(z2=z; *z2; len++){
117081        SQLITE_SKIP_UTF8(z2);
117082      }
117083    }
117084  }
117085#ifdef SQLITE_SUBSTR_COMPATIBILITY
117086  /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as
117087  ** as substr(X,1,N) - it returns the first N characters of X.  This
117088  ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
117089  ** from 2009-02-02 for compatibility of applications that exploited the
117090  ** old buggy behavior. */
117091  if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */
117092#endif
117093  if( argc==3 ){
117094    p2 = sqlite3_value_int(argv[2]);
117095    if( p2<0 ){
117096      p2 = -p2;
117097      negP2 = 1;
117098    }
117099  }else{
117100    p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
117101  }
117102  if( p1<0 ){
117103    p1 += len;
117104    if( p1<0 ){
117105      p2 += p1;
117106      if( p2<0 ) p2 = 0;
117107      p1 = 0;
117108    }
117109  }else if( p1>0 ){
117110    p1--;
117111  }else if( p2>0 ){
117112    p2--;
117113  }
117114  if( negP2 ){
117115    p1 -= p2;
117116    if( p1<0 ){
117117      p2 += p1;
117118      p1 = 0;
117119    }
117120  }
117121  assert( p1>=0 && p2>=0 );
117122  if( p0type!=SQLITE_BLOB ){
117123    while( *z && p1 ){
117124      SQLITE_SKIP_UTF8(z);
117125      p1--;
117126    }
117127    for(z2=z; *z2 && p2; p2--){
117128      SQLITE_SKIP_UTF8(z2);
117129    }
117130    sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
117131                          SQLITE_UTF8);
117132  }else{
117133    if( p1+p2>len ){
117134      p2 = len-p1;
117135      if( p2<0 ) p2 = 0;
117136    }
117137    sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT);
117138  }
117139}
117140
117141/*
117142** Implementation of the round() function
117143*/
117144#ifndef SQLITE_OMIT_FLOATING_POINT
117145static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
117146  int n = 0;
117147  double r;
117148  char *zBuf;
117149  assert( argc==1 || argc==2 );
117150  if( argc==2 ){
117151    if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
117152    n = sqlite3_value_int(argv[1]);
117153    if( n>30 ) n = 30;
117154    if( n<0 ) n = 0;
117155  }
117156  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
117157  r = sqlite3_value_double(argv[0]);
117158  /* If Y==0 and X will fit in a 64-bit int,
117159  ** handle the rounding directly,
117160  ** otherwise use printf.
117161  */
117162  if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
117163    /* The value has no fractional part so there is nothing to round */
117164  }else if( n==0 ){
117165    r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
117166  }else{
117167    zBuf = sqlite3_mprintf("%.*f",n,r);
117168    if( zBuf==0 ){
117169      sqlite3_result_error_nomem(context);
117170      return;
117171    }
117172    sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
117173    sqlite3_free(zBuf);
117174  }
117175  sqlite3_result_double(context, r);
117176}
117177#endif
117178
117179/*
117180** Allocate nByte bytes of space using sqlite3Malloc(). If the
117181** allocation fails, call sqlite3_result_error_nomem() to notify
117182** the database handle that malloc() has failed and return NULL.
117183** If nByte is larger than the maximum string or blob length, then
117184** raise an SQLITE_TOOBIG exception and return NULL.
117185*/
117186static void *contextMalloc(sqlite3_context *context, i64 nByte){
117187  char *z;
117188  sqlite3 *db = sqlite3_context_db_handle(context);
117189  assert( nByte>0 );
117190  testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
117191  testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
117192  if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
117193    sqlite3_result_error_toobig(context);
117194    z = 0;
117195  }else{
117196    z = sqlite3Malloc(nByte);
117197    if( !z ){
117198      sqlite3_result_error_nomem(context);
117199    }
117200  }
117201  return z;
117202}
117203
117204/*
117205** Implementation of the upper() and lower() SQL functions.
117206*/
117207static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
117208  char *z1;
117209  const char *z2;
117210  int i, n;
117211  UNUSED_PARAMETER(argc);
117212  z2 = (char*)sqlite3_value_text(argv[0]);
117213  n = sqlite3_value_bytes(argv[0]);
117214  /* Verify that the call to _bytes() does not invalidate the _text() pointer */
117215  assert( z2==(char*)sqlite3_value_text(argv[0]) );
117216  if( z2 ){
117217    z1 = contextMalloc(context, ((i64)n)+1);
117218    if( z1 ){
117219      for(i=0; i<n; i++){
117220        z1[i] = (char)sqlite3Toupper(z2[i]);
117221      }
117222      sqlite3_result_text(context, z1, n, sqlite3_free);
117223    }
117224  }
117225}
117226static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
117227  char *z1;
117228  const char *z2;
117229  int i, n;
117230  UNUSED_PARAMETER(argc);
117231  z2 = (char*)sqlite3_value_text(argv[0]);
117232  n = sqlite3_value_bytes(argv[0]);
117233  /* Verify that the call to _bytes() does not invalidate the _text() pointer */
117234  assert( z2==(char*)sqlite3_value_text(argv[0]) );
117235  if( z2 ){
117236    z1 = contextMalloc(context, ((i64)n)+1);
117237    if( z1 ){
117238      for(i=0; i<n; i++){
117239        z1[i] = sqlite3Tolower(z2[i]);
117240      }
117241      sqlite3_result_text(context, z1, n, sqlite3_free);
117242    }
117243  }
117244}
117245
117246/*
117247** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
117248** as VDBE code so that unused argument values do not have to be computed.
117249** However, we still need some kind of function implementation for this
117250** routines in the function table.  The noopFunc macro provides this.
117251** noopFunc will never be called so it doesn't matter what the implementation
117252** is.  We might as well use the "version()" function as a substitute.
117253*/
117254#define noopFunc versionFunc   /* Substitute function - never called */
117255
117256/*
117257** Implementation of random().  Return a random integer.
117258*/
117259static void randomFunc(
117260  sqlite3_context *context,
117261  int NotUsed,
117262  sqlite3_value **NotUsed2
117263){
117264  sqlite_int64 r;
117265  UNUSED_PARAMETER2(NotUsed, NotUsed2);
117266  sqlite3_randomness(sizeof(r), &r);
117267  if( r<0 ){
117268    /* We need to prevent a random number of 0x8000000000000000
117269    ** (or -9223372036854775808) since when you do abs() of that
117270    ** number of you get the same value back again.  To do this
117271    ** in a way that is testable, mask the sign bit off of negative
117272    ** values, resulting in a positive value.  Then take the
117273    ** 2s complement of that positive value.  The end result can
117274    ** therefore be no less than -9223372036854775807.
117275    */
117276    r = -(r & LARGEST_INT64);
117277  }
117278  sqlite3_result_int64(context, r);
117279}
117280
117281/*
117282** Implementation of randomblob(N).  Return a random blob
117283** that is N bytes long.
117284*/
117285static void randomBlob(
117286  sqlite3_context *context,
117287  int argc,
117288  sqlite3_value **argv
117289){
117290  sqlite3_int64 n;
117291  unsigned char *p;
117292  assert( argc==1 );
117293  UNUSED_PARAMETER(argc);
117294  n = sqlite3_value_int64(argv[0]);
117295  if( n<1 ){
117296    n = 1;
117297  }
117298  p = contextMalloc(context, n);
117299  if( p ){
117300    sqlite3_randomness(n, p);
117301    sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
117302  }
117303}
117304
117305/*
117306** Implementation of the last_insert_rowid() SQL function.  The return
117307** value is the same as the sqlite3_last_insert_rowid() API function.
117308*/
117309static void last_insert_rowid(
117310  sqlite3_context *context,
117311  int NotUsed,
117312  sqlite3_value **NotUsed2
117313){
117314  sqlite3 *db = sqlite3_context_db_handle(context);
117315  UNUSED_PARAMETER2(NotUsed, NotUsed2);
117316  /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
117317  ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
117318  ** function. */
117319  sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
117320}
117321
117322/*
117323** Implementation of the changes() SQL function.
117324**
117325** IMP: R-62073-11209 The changes() SQL function is a wrapper
117326** around the sqlite3_changes() C/C++ function and hence follows the same
117327** rules for counting changes.
117328*/
117329static void changes(
117330  sqlite3_context *context,
117331  int NotUsed,
117332  sqlite3_value **NotUsed2
117333){
117334  sqlite3 *db = sqlite3_context_db_handle(context);
117335  UNUSED_PARAMETER2(NotUsed, NotUsed2);
117336  sqlite3_result_int(context, sqlite3_changes(db));
117337}
117338
117339/*
117340** Implementation of the total_changes() SQL function.  The return value is
117341** the same as the sqlite3_total_changes() API function.
117342*/
117343static void total_changes(
117344  sqlite3_context *context,
117345  int NotUsed,
117346  sqlite3_value **NotUsed2
117347){
117348  sqlite3 *db = sqlite3_context_db_handle(context);
117349  UNUSED_PARAMETER2(NotUsed, NotUsed2);
117350  /* IMP: R-52756-41993 This function is a wrapper around the
117351  ** sqlite3_total_changes() C/C++ interface. */
117352  sqlite3_result_int(context, sqlite3_total_changes(db));
117353}
117354
117355/*
117356** A structure defining how to do GLOB-style comparisons.
117357*/
117358struct compareInfo {
117359  u8 matchAll;          /* "*" or "%" */
117360  u8 matchOne;          /* "?" or "_" */
117361  u8 matchSet;          /* "[" or 0 */
117362  u8 noCase;            /* true to ignore case differences */
117363};
117364
117365/*
117366** For LIKE and GLOB matching on EBCDIC machines, assume that every
117367** character is exactly one byte in size.  Also, provde the Utf8Read()
117368** macro for fast reading of the next character in the common case where
117369** the next character is ASCII.
117370*/
117371#if defined(SQLITE_EBCDIC)
117372# define sqlite3Utf8Read(A)        (*((*A)++))
117373# define Utf8Read(A)               (*(A++))
117374#else
117375# define Utf8Read(A)               (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
117376#endif
117377
117378static const struct compareInfo globInfo = { '*', '?', '[', 0 };
117379/* The correct SQL-92 behavior is for the LIKE operator to ignore
117380** case.  Thus  'a' LIKE 'A' would be true. */
117381static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
117382/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
117383** is case sensitive causing 'a' LIKE 'A' to be false */
117384static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
117385
117386/*
117387** Possible error returns from patternMatch()
117388*/
117389#define SQLITE_MATCH             0
117390#define SQLITE_NOMATCH           1
117391#define SQLITE_NOWILDCARDMATCH   2
117392
117393/*
117394** Compare two UTF-8 strings for equality where the first string is
117395** a GLOB or LIKE expression.  Return values:
117396**
117397**    SQLITE_MATCH:            Match
117398**    SQLITE_NOMATCH:          No match
117399**    SQLITE_NOWILDCARDMATCH:  No match in spite of having * or % wildcards.
117400**
117401** Globbing rules:
117402**
117403**      '*'       Matches any sequence of zero or more characters.
117404**
117405**      '?'       Matches exactly one character.
117406**
117407**     [...]      Matches one character from the enclosed list of
117408**                characters.
117409**
117410**     [^...]     Matches one character not in the enclosed list.
117411**
117412** With the [...] and [^...] matching, a ']' character can be included
117413** in the list by making it the first character after '[' or '^'.  A
117414** range of characters can be specified using '-'.  Example:
117415** "[a-z]" matches any single lower-case letter.  To match a '-', make
117416** it the last character in the list.
117417**
117418** Like matching rules:
117419**
117420**      '%'       Matches any sequence of zero or more characters
117421**
117422***     '_'       Matches any one character
117423**
117424**      Ec        Where E is the "esc" character and c is any other
117425**                character, including '%', '_', and esc, match exactly c.
117426**
117427** The comments within this routine usually assume glob matching.
117428**
117429** This routine is usually quick, but can be N**2 in the worst case.
117430*/
117431static int patternCompare(
117432  const u8 *zPattern,              /* The glob pattern */
117433  const u8 *zString,               /* The string to compare against the glob */
117434  const struct compareInfo *pInfo, /* Information about how to do the compare */
117435  u32 matchOther                   /* The escape char (LIKE) or '[' (GLOB) */
117436){
117437  u32 c, c2;                       /* Next pattern and input string chars */
117438  u32 matchOne = pInfo->matchOne;  /* "?" or "_" */
117439  u32 matchAll = pInfo->matchAll;  /* "*" or "%" */
117440  u8 noCase = pInfo->noCase;       /* True if uppercase==lowercase */
117441  const u8 *zEscaped = 0;          /* One past the last escaped input char */
117442
117443  while( (c = Utf8Read(zPattern))!=0 ){
117444    if( c==matchAll ){  /* Match "*" */
117445      /* Skip over multiple "*" characters in the pattern.  If there
117446      ** are also "?" characters, skip those as well, but consume a
117447      ** single character of the input string for each "?" skipped */
117448      while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){
117449        if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
117450          return SQLITE_NOWILDCARDMATCH;
117451        }
117452      }
117453      if( c==0 ){
117454        return SQLITE_MATCH;   /* "*" at the end of the pattern matches */
117455      }else if( c==matchOther ){
117456        if( pInfo->matchSet==0 ){
117457          c = sqlite3Utf8Read(&zPattern);
117458          if( c==0 ) return SQLITE_NOWILDCARDMATCH;
117459        }else{
117460          /* "[...]" immediately follows the "*".  We have to do a slow
117461          ** recursive search in this case, but it is an unusual case. */
117462          assert( matchOther<0x80 );  /* '[' is a single-byte character */
117463          while( *zString ){
117464            int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
117465            if( bMatch!=SQLITE_NOMATCH ) return bMatch;
117466            SQLITE_SKIP_UTF8(zString);
117467          }
117468          return SQLITE_NOWILDCARDMATCH;
117469        }
117470      }
117471
117472      /* At this point variable c contains the first character of the
117473      ** pattern string past the "*".  Search in the input string for the
117474      ** first matching character and recursively continue the match from
117475      ** that point.
117476      **
117477      ** For a case-insensitive search, set variable cx to be the same as
117478      ** c but in the other case and search the input string for either
117479      ** c or cx.
117480      */
117481      if( c<=0x80 ){
117482        char zStop[3];
117483        int bMatch;
117484        if( noCase ){
117485          zStop[0] = sqlite3Toupper(c);
117486          zStop[1] = sqlite3Tolower(c);
117487          zStop[2] = 0;
117488        }else{
117489          zStop[0] = c;
117490          zStop[1] = 0;
117491        }
117492        while(1){
117493          zString += strcspn((const char*)zString, zStop);
117494          if( zString[0]==0 ) break;
117495          zString++;
117496          bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
117497          if( bMatch!=SQLITE_NOMATCH ) return bMatch;
117498        }
117499      }else{
117500        int bMatch;
117501        while( (c2 = Utf8Read(zString))!=0 ){
117502          if( c2!=c ) continue;
117503          bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
117504          if( bMatch!=SQLITE_NOMATCH ) return bMatch;
117505        }
117506      }
117507      return SQLITE_NOWILDCARDMATCH;
117508    }
117509    if( c==matchOther ){
117510      if( pInfo->matchSet==0 ){
117511        c = sqlite3Utf8Read(&zPattern);
117512        if( c==0 ) return SQLITE_NOMATCH;
117513        zEscaped = zPattern;
117514      }else{
117515        u32 prior_c = 0;
117516        int seen = 0;
117517        int invert = 0;
117518        c = sqlite3Utf8Read(&zString);
117519        if( c==0 ) return SQLITE_NOMATCH;
117520        c2 = sqlite3Utf8Read(&zPattern);
117521        if( c2=='^' ){
117522          invert = 1;
117523          c2 = sqlite3Utf8Read(&zPattern);
117524        }
117525        if( c2==']' ){
117526          if( c==']' ) seen = 1;
117527          c2 = sqlite3Utf8Read(&zPattern);
117528        }
117529        while( c2 && c2!=']' ){
117530          if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
117531            c2 = sqlite3Utf8Read(&zPattern);
117532            if( c>=prior_c && c<=c2 ) seen = 1;
117533            prior_c = 0;
117534          }else{
117535            if( c==c2 ){
117536              seen = 1;
117537            }
117538            prior_c = c2;
117539          }
117540          c2 = sqlite3Utf8Read(&zPattern);
117541        }
117542        if( c2==0 || (seen ^ invert)==0 ){
117543          return SQLITE_NOMATCH;
117544        }
117545        continue;
117546      }
117547    }
117548    c2 = Utf8Read(zString);
117549    if( c==c2 ) continue;
117550    if( noCase  && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){
117551      continue;
117552    }
117553    if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
117554    return SQLITE_NOMATCH;
117555  }
117556  return *zString==0 ? SQLITE_MATCH : SQLITE_NOMATCH;
117557}
117558
117559/*
117560** The sqlite3_strglob() interface.  Return 0 on a match (like strcmp()) and
117561** non-zero if there is no match.
117562*/
117563SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
117564  return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
117565}
117566
117567/*
117568** The sqlite3_strlike() interface.  Return 0 on a match and non-zero for
117569** a miss - like strcmp().
117570*/
117571SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
117572  return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
117573}
117574
117575/*
117576** Count the number of times that the LIKE operator (or GLOB which is
117577** just a variation of LIKE) gets called.  This is used for testing
117578** only.
117579*/
117580#ifdef SQLITE_TEST
117581SQLITE_API int sqlite3_like_count = 0;
117582#endif
117583
117584
117585/*
117586** Implementation of the like() SQL function.  This function implements
117587** the build-in LIKE operator.  The first argument to the function is the
117588** pattern and the second argument is the string.  So, the SQL statements:
117589**
117590**       A LIKE B
117591**
117592** is implemented as like(B,A).
117593**
117594** This same function (with a different compareInfo structure) computes
117595** the GLOB operator.
117596*/
117597static void likeFunc(
117598  sqlite3_context *context,
117599  int argc,
117600  sqlite3_value **argv
117601){
117602  const unsigned char *zA, *zB;
117603  u32 escape;
117604  int nPat;
117605  sqlite3 *db = sqlite3_context_db_handle(context);
117606  struct compareInfo *pInfo = sqlite3_user_data(context);
117607  struct compareInfo backupInfo;
117608
117609#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
117610  if( sqlite3_value_type(argv[0])==SQLITE_BLOB
117611   || sqlite3_value_type(argv[1])==SQLITE_BLOB
117612  ){
117613#ifdef SQLITE_TEST
117614    sqlite3_like_count++;
117615#endif
117616    sqlite3_result_int(context, 0);
117617    return;
117618  }
117619#endif
117620
117621  /* Limit the length of the LIKE or GLOB pattern to avoid problems
117622  ** of deep recursion and N*N behavior in patternCompare().
117623  */
117624  nPat = sqlite3_value_bytes(argv[0]);
117625  testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
117626  testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
117627  if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
117628    sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
117629    return;
117630  }
117631  if( argc==3 ){
117632    /* The escape character string must consist of a single UTF-8 character.
117633    ** Otherwise, return an error.
117634    */
117635    const unsigned char *zEsc = sqlite3_value_text(argv[2]);
117636    if( zEsc==0 ) return;
117637    if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
117638      sqlite3_result_error(context,
117639          "ESCAPE expression must be a single character", -1);
117640      return;
117641    }
117642    escape = sqlite3Utf8Read(&zEsc);
117643    if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
117644      memcpy(&backupInfo, pInfo, sizeof(backupInfo));
117645      pInfo = &backupInfo;
117646      if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
117647      if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
117648    }
117649  }else{
117650    escape = pInfo->matchSet;
117651  }
117652  zB = sqlite3_value_text(argv[0]);
117653  zA = sqlite3_value_text(argv[1]);
117654  if( zA && zB ){
117655#ifdef SQLITE_TEST
117656    sqlite3_like_count++;
117657#endif
117658    sqlite3_result_int(context,
117659                      patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH);
117660  }
117661}
117662
117663/*
117664** Implementation of the NULLIF(x,y) function.  The result is the first
117665** argument if the arguments are different.  The result is NULL if the
117666** arguments are equal to each other.
117667*/
117668static void nullifFunc(
117669  sqlite3_context *context,
117670  int NotUsed,
117671  sqlite3_value **argv
117672){
117673  CollSeq *pColl = sqlite3GetFuncCollSeq(context);
117674  UNUSED_PARAMETER(NotUsed);
117675  if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
117676    sqlite3_result_value(context, argv[0]);
117677  }
117678}
117679
117680/*
117681** Implementation of the sqlite_version() function.  The result is the version
117682** of the SQLite library that is running.
117683*/
117684static void versionFunc(
117685  sqlite3_context *context,
117686  int NotUsed,
117687  sqlite3_value **NotUsed2
117688){
117689  UNUSED_PARAMETER2(NotUsed, NotUsed2);
117690  /* IMP: R-48699-48617 This function is an SQL wrapper around the
117691  ** sqlite3_libversion() C-interface. */
117692  sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
117693}
117694
117695/*
117696** Implementation of the sqlite_source_id() function. The result is a string
117697** that identifies the particular version of the source code used to build
117698** SQLite.
117699*/
117700static void sourceidFunc(
117701  sqlite3_context *context,
117702  int NotUsed,
117703  sqlite3_value **NotUsed2
117704){
117705  UNUSED_PARAMETER2(NotUsed, NotUsed2);
117706  /* IMP: R-24470-31136 This function is an SQL wrapper around the
117707  ** sqlite3_sourceid() C interface. */
117708  sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
117709}
117710
117711/*
117712** Implementation of the sqlite_log() function.  This is a wrapper around
117713** sqlite3_log().  The return value is NULL.  The function exists purely for
117714** its side-effects.
117715*/
117716static void errlogFunc(
117717  sqlite3_context *context,
117718  int argc,
117719  sqlite3_value **argv
117720){
117721  UNUSED_PARAMETER(argc);
117722  UNUSED_PARAMETER(context);
117723  sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
117724}
117725
117726/*
117727** Implementation of the sqlite_compileoption_used() function.
117728** The result is an integer that identifies if the compiler option
117729** was used to build SQLite.
117730*/
117731#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
117732static void compileoptionusedFunc(
117733  sqlite3_context *context,
117734  int argc,
117735  sqlite3_value **argv
117736){
117737  const char *zOptName;
117738  assert( argc==1 );
117739  UNUSED_PARAMETER(argc);
117740  /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
117741  ** function is a wrapper around the sqlite3_compileoption_used() C/C++
117742  ** function.
117743  */
117744  if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
117745    sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
117746  }
117747}
117748#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
117749
117750/*
117751** Implementation of the sqlite_compileoption_get() function.
117752** The result is a string that identifies the compiler options
117753** used to build SQLite.
117754*/
117755#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
117756static void compileoptiongetFunc(
117757  sqlite3_context *context,
117758  int argc,
117759  sqlite3_value **argv
117760){
117761  int n;
117762  assert( argc==1 );
117763  UNUSED_PARAMETER(argc);
117764  /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
117765  ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
117766  */
117767  n = sqlite3_value_int(argv[0]);
117768  sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
117769}
117770#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
117771
117772/* Array for converting from half-bytes (nybbles) into ASCII hex
117773** digits. */
117774static const char hexdigits[] = {
117775  '0', '1', '2', '3', '4', '5', '6', '7',
117776  '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
117777};
117778
117779/*
117780** Implementation of the QUOTE() function.  This function takes a single
117781** argument.  If the argument is numeric, the return value is the same as
117782** the argument.  If the argument is NULL, the return value is the string
117783** "NULL".  Otherwise, the argument is enclosed in single quotes with
117784** single-quote escapes.
117785*/
117786static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
117787  assert( argc==1 );
117788  UNUSED_PARAMETER(argc);
117789  switch( sqlite3_value_type(argv[0]) ){
117790    case SQLITE_FLOAT: {
117791      double r1, r2;
117792      char zBuf[50];
117793      r1 = sqlite3_value_double(argv[0]);
117794      sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
117795      sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
117796      if( r1!=r2 ){
117797        sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
117798      }
117799      sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
117800      break;
117801    }
117802    case SQLITE_INTEGER: {
117803      sqlite3_result_value(context, argv[0]);
117804      break;
117805    }
117806    case SQLITE_BLOB: {
117807      char *zText = 0;
117808      char const *zBlob = sqlite3_value_blob(argv[0]);
117809      int nBlob = sqlite3_value_bytes(argv[0]);
117810      assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
117811      zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);
117812      if( zText ){
117813        int i;
117814        for(i=0; i<nBlob; i++){
117815          zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
117816          zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
117817        }
117818        zText[(nBlob*2)+2] = '\'';
117819        zText[(nBlob*2)+3] = '\0';
117820        zText[0] = 'X';
117821        zText[1] = '\'';
117822        sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
117823        sqlite3_free(zText);
117824      }
117825      break;
117826    }
117827    case SQLITE_TEXT: {
117828      int i,j;
117829      u64 n;
117830      const unsigned char *zArg = sqlite3_value_text(argv[0]);
117831      char *z;
117832
117833      if( zArg==0 ) return;
117834      for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
117835      z = contextMalloc(context, ((i64)i)+((i64)n)+3);
117836      if( z ){
117837        z[0] = '\'';
117838        for(i=0, j=1; zArg[i]; i++){
117839          z[j++] = zArg[i];
117840          if( zArg[i]=='\'' ){
117841            z[j++] = '\'';
117842          }
117843        }
117844        z[j++] = '\'';
117845        z[j] = 0;
117846        sqlite3_result_text(context, z, j, sqlite3_free);
117847      }
117848      break;
117849    }
117850    default: {
117851      assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
117852      sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
117853      break;
117854    }
117855  }
117856}
117857
117858/*
117859** The unicode() function.  Return the integer unicode code-point value
117860** for the first character of the input string.
117861*/
117862static void unicodeFunc(
117863  sqlite3_context *context,
117864  int argc,
117865  sqlite3_value **argv
117866){
117867  const unsigned char *z = sqlite3_value_text(argv[0]);
117868  (void)argc;
117869  if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
117870}
117871
117872/*
117873** The char() function takes zero or more arguments, each of which is
117874** an integer.  It constructs a string where each character of the string
117875** is the unicode character for the corresponding integer argument.
117876*/
117877static void charFunc(
117878  sqlite3_context *context,
117879  int argc,
117880  sqlite3_value **argv
117881){
117882  unsigned char *z, *zOut;
117883  int i;
117884  zOut = z = sqlite3_malloc64( argc*4+1 );
117885  if( z==0 ){
117886    sqlite3_result_error_nomem(context);
117887    return;
117888  }
117889  for(i=0; i<argc; i++){
117890    sqlite3_int64 x;
117891    unsigned c;
117892    x = sqlite3_value_int64(argv[i]);
117893    if( x<0 || x>0x10ffff ) x = 0xfffd;
117894    c = (unsigned)(x & 0x1fffff);
117895    if( c<0x00080 ){
117896      *zOut++ = (u8)(c&0xFF);
117897    }else if( c<0x00800 ){
117898      *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
117899      *zOut++ = 0x80 + (u8)(c & 0x3F);
117900    }else if( c<0x10000 ){
117901      *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
117902      *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
117903      *zOut++ = 0x80 + (u8)(c & 0x3F);
117904    }else{
117905      *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
117906      *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
117907      *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
117908      *zOut++ = 0x80 + (u8)(c & 0x3F);
117909    }                                                    \
117910  }
117911  sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
117912}
117913
117914/*
117915** The hex() function.  Interpret the argument as a blob.  Return
117916** a hexadecimal rendering as text.
117917*/
117918static void hexFunc(
117919  sqlite3_context *context,
117920  int argc,
117921  sqlite3_value **argv
117922){
117923  int i, n;
117924  const unsigned char *pBlob;
117925  char *zHex, *z;
117926  assert( argc==1 );
117927  UNUSED_PARAMETER(argc);
117928  pBlob = sqlite3_value_blob(argv[0]);
117929  n = sqlite3_value_bytes(argv[0]);
117930  assert( pBlob==sqlite3_value_blob(argv[0]) );  /* No encoding change */
117931  z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
117932  if( zHex ){
117933    for(i=0; i<n; i++, pBlob++){
117934      unsigned char c = *pBlob;
117935      *(z++) = hexdigits[(c>>4)&0xf];
117936      *(z++) = hexdigits[c&0xf];
117937    }
117938    *z = 0;
117939    sqlite3_result_text(context, zHex, n*2, sqlite3_free);
117940  }
117941}
117942
117943/*
117944** The zeroblob(N) function returns a zero-filled blob of size N bytes.
117945*/
117946static void zeroblobFunc(
117947  sqlite3_context *context,
117948  int argc,
117949  sqlite3_value **argv
117950){
117951  i64 n;
117952  int rc;
117953  assert( argc==1 );
117954  UNUSED_PARAMETER(argc);
117955  n = sqlite3_value_int64(argv[0]);
117956  if( n<0 ) n = 0;
117957  rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
117958  if( rc ){
117959    sqlite3_result_error_code(context, rc);
117960  }
117961}
117962
117963/*
117964** The replace() function.  Three arguments are all strings: call
117965** them A, B, and C. The result is also a string which is derived
117966** from A by replacing every occurrence of B with C.  The match
117967** must be exact.  Collating sequences are not used.
117968*/
117969static void replaceFunc(
117970  sqlite3_context *context,
117971  int argc,
117972  sqlite3_value **argv
117973){
117974  const unsigned char *zStr;        /* The input string A */
117975  const unsigned char *zPattern;    /* The pattern string B */
117976  const unsigned char *zRep;        /* The replacement string C */
117977  unsigned char *zOut;              /* The output */
117978  int nStr;                /* Size of zStr */
117979  int nPattern;            /* Size of zPattern */
117980  int nRep;                /* Size of zRep */
117981  i64 nOut;                /* Maximum size of zOut */
117982  int loopLimit;           /* Last zStr[] that might match zPattern[] */
117983  int i, j;                /* Loop counters */
117984  unsigned cntExpand;      /* Number zOut expansions */
117985  sqlite3 *db = sqlite3_context_db_handle(context);
117986
117987  assert( argc==3 );
117988  UNUSED_PARAMETER(argc);
117989  zStr = sqlite3_value_text(argv[0]);
117990  if( zStr==0 ) return;
117991  nStr = sqlite3_value_bytes(argv[0]);
117992  assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
117993  zPattern = sqlite3_value_text(argv[1]);
117994  if( zPattern==0 ){
117995    assert( sqlite3_value_type(argv[1])==SQLITE_NULL
117996            || sqlite3_context_db_handle(context)->mallocFailed );
117997    return;
117998  }
117999  if( zPattern[0]==0 ){
118000    assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
118001    sqlite3_result_value(context, argv[0]);
118002    return;
118003  }
118004  nPattern = sqlite3_value_bytes(argv[1]);
118005  assert( zPattern==sqlite3_value_text(argv[1]) );  /* No encoding change */
118006  zRep = sqlite3_value_text(argv[2]);
118007  if( zRep==0 ) return;
118008  nRep = sqlite3_value_bytes(argv[2]);
118009  assert( zRep==sqlite3_value_text(argv[2]) );
118010  nOut = nStr + 1;
118011  assert( nOut<SQLITE_MAX_LENGTH );
118012  zOut = contextMalloc(context, (i64)nOut);
118013  if( zOut==0 ){
118014    return;
118015  }
118016  loopLimit = nStr - nPattern;
118017  cntExpand = 0;
118018  for(i=j=0; i<=loopLimit; i++){
118019    if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
118020      zOut[j++] = zStr[i];
118021    }else{
118022      if( nRep>nPattern ){
118023        nOut += nRep - nPattern;
118024        testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
118025        testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
118026        if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
118027          sqlite3_result_error_toobig(context);
118028          sqlite3_free(zOut);
118029          return;
118030        }
118031        cntExpand++;
118032        if( (cntExpand&(cntExpand-1))==0 ){
118033          /* Grow the size of the output buffer only on substitutions
118034          ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
118035          u8 *zOld;
118036          zOld = zOut;
118037          zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1));
118038          if( zOut==0 ){
118039            sqlite3_result_error_nomem(context);
118040            sqlite3_free(zOld);
118041            return;
118042          }
118043        }
118044      }
118045      memcpy(&zOut[j], zRep, nRep);
118046      j += nRep;
118047      i += nPattern-1;
118048    }
118049  }
118050  assert( j+nStr-i+1<=nOut );
118051  memcpy(&zOut[j], &zStr[i], nStr-i);
118052  j += nStr - i;
118053  assert( j<=nOut );
118054  zOut[j] = 0;
118055  sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
118056}
118057
118058/*
118059** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
118060** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
118061*/
118062static void trimFunc(
118063  sqlite3_context *context,
118064  int argc,
118065  sqlite3_value **argv
118066){
118067  const unsigned char *zIn;         /* Input string */
118068  const unsigned char *zCharSet;    /* Set of characters to trim */
118069  int nIn;                          /* Number of bytes in input */
118070  int flags;                        /* 1: trimleft  2: trimright  3: trim */
118071  int i;                            /* Loop counter */
118072  unsigned char *aLen = 0;          /* Length of each character in zCharSet */
118073  unsigned char **azChar = 0;       /* Individual characters in zCharSet */
118074  int nChar;                        /* Number of characters in zCharSet */
118075
118076  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
118077    return;
118078  }
118079  zIn = sqlite3_value_text(argv[0]);
118080  if( zIn==0 ) return;
118081  nIn = sqlite3_value_bytes(argv[0]);
118082  assert( zIn==sqlite3_value_text(argv[0]) );
118083  if( argc==1 ){
118084    static const unsigned char lenOne[] = { 1 };
118085    static unsigned char * const azOne[] = { (u8*)" " };
118086    nChar = 1;
118087    aLen = (u8*)lenOne;
118088    azChar = (unsigned char **)azOne;
118089    zCharSet = 0;
118090  }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
118091    return;
118092  }else{
118093    const unsigned char *z;
118094    for(z=zCharSet, nChar=0; *z; nChar++){
118095      SQLITE_SKIP_UTF8(z);
118096    }
118097    if( nChar>0 ){
118098      azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
118099      if( azChar==0 ){
118100        return;
118101      }
118102      aLen = (unsigned char*)&azChar[nChar];
118103      for(z=zCharSet, nChar=0; *z; nChar++){
118104        azChar[nChar] = (unsigned char *)z;
118105        SQLITE_SKIP_UTF8(z);
118106        aLen[nChar] = (u8)(z - azChar[nChar]);
118107      }
118108    }
118109  }
118110  if( nChar>0 ){
118111    flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
118112    if( flags & 1 ){
118113      while( nIn>0 ){
118114        int len = 0;
118115        for(i=0; i<nChar; i++){
118116          len = aLen[i];
118117          if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
118118        }
118119        if( i>=nChar ) break;
118120        zIn += len;
118121        nIn -= len;
118122      }
118123    }
118124    if( flags & 2 ){
118125      while( nIn>0 ){
118126        int len = 0;
118127        for(i=0; i<nChar; i++){
118128          len = aLen[i];
118129          if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
118130        }
118131        if( i>=nChar ) break;
118132        nIn -= len;
118133      }
118134    }
118135    if( zCharSet ){
118136      sqlite3_free(azChar);
118137    }
118138  }
118139  sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
118140}
118141
118142
118143#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
118144/*
118145** The "unknown" function is automatically substituted in place of
118146** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN
118147** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
118148** When the "sqlite3" command-line shell is built using this functionality,
118149** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries
118150** involving application-defined functions to be examined in a generic
118151** sqlite3 shell.
118152*/
118153static void unknownFunc(
118154  sqlite3_context *context,
118155  int argc,
118156  sqlite3_value **argv
118157){
118158  /* no-op */
118159}
118160#endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/
118161
118162
118163/* IMP: R-25361-16150 This function is omitted from SQLite by default. It
118164** is only available if the SQLITE_SOUNDEX compile-time option is used
118165** when SQLite is built.
118166*/
118167#ifdef SQLITE_SOUNDEX
118168/*
118169** Compute the soundex encoding of a word.
118170**
118171** IMP: R-59782-00072 The soundex(X) function returns a string that is the
118172** soundex encoding of the string X.
118173*/
118174static void soundexFunc(
118175  sqlite3_context *context,
118176  int argc,
118177  sqlite3_value **argv
118178){
118179  char zResult[8];
118180  const u8 *zIn;
118181  int i, j;
118182  static const unsigned char iCode[] = {
118183    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118184    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118185    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118186    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118187    0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
118188    1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
118189    0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
118190    1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
118191  };
118192  assert( argc==1 );
118193  zIn = (u8*)sqlite3_value_text(argv[0]);
118194  if( zIn==0 ) zIn = (u8*)"";
118195  for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
118196  if( zIn[i] ){
118197    u8 prevcode = iCode[zIn[i]&0x7f];
118198    zResult[0] = sqlite3Toupper(zIn[i]);
118199    for(j=1; j<4 && zIn[i]; i++){
118200      int code = iCode[zIn[i]&0x7f];
118201      if( code>0 ){
118202        if( code!=prevcode ){
118203          prevcode = code;
118204          zResult[j++] = code + '0';
118205        }
118206      }else{
118207        prevcode = 0;
118208      }
118209    }
118210    while( j<4 ){
118211      zResult[j++] = '0';
118212    }
118213    zResult[j] = 0;
118214    sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
118215  }else{
118216    /* IMP: R-64894-50321 The string "?000" is returned if the argument
118217    ** is NULL or contains no ASCII alphabetic characters. */
118218    sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
118219  }
118220}
118221#endif /* SQLITE_SOUNDEX */
118222
118223#ifndef SQLITE_OMIT_LOAD_EXTENSION
118224/*
118225** A function that loads a shared-library extension then returns NULL.
118226*/
118227static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
118228  const char *zFile = (const char *)sqlite3_value_text(argv[0]);
118229  const char *zProc;
118230  sqlite3 *db = sqlite3_context_db_handle(context);
118231  char *zErrMsg = 0;
118232
118233  /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc
118234  ** flag is set.  See the sqlite3_enable_load_extension() API.
118235  */
118236  if( (db->flags & SQLITE_LoadExtFunc)==0 ){
118237    sqlite3_result_error(context, "not authorized", -1);
118238    return;
118239  }
118240
118241  if( argc==2 ){
118242    zProc = (const char *)sqlite3_value_text(argv[1]);
118243  }else{
118244    zProc = 0;
118245  }
118246  if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
118247    sqlite3_result_error(context, zErrMsg, -1);
118248    sqlite3_free(zErrMsg);
118249  }
118250}
118251#endif
118252
118253
118254/*
118255** An instance of the following structure holds the context of a
118256** sum() or avg() aggregate computation.
118257*/
118258typedef struct SumCtx SumCtx;
118259struct SumCtx {
118260  double rSum;      /* Floating point sum */
118261  i64 iSum;         /* Integer sum */
118262  i64 cnt;          /* Number of elements summed */
118263  u8 overflow;      /* True if integer overflow seen */
118264  u8 approx;        /* True if non-integer value was input to the sum */
118265};
118266
118267/*
118268** Routines used to compute the sum, average, and total.
118269**
118270** The SUM() function follows the (broken) SQL standard which means
118271** that it returns NULL if it sums over no inputs.  TOTAL returns
118272** 0.0 in that case.  In addition, TOTAL always returns a float where
118273** SUM might return an integer if it never encounters a floating point
118274** value.  TOTAL never fails, but SUM might through an exception if
118275** it overflows an integer.
118276*/
118277static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
118278  SumCtx *p;
118279  int type;
118280  assert( argc==1 );
118281  UNUSED_PARAMETER(argc);
118282  p = sqlite3_aggregate_context(context, sizeof(*p));
118283  type = sqlite3_value_numeric_type(argv[0]);
118284  if( p && type!=SQLITE_NULL ){
118285    p->cnt++;
118286    if( type==SQLITE_INTEGER ){
118287      i64 v = sqlite3_value_int64(argv[0]);
118288      p->rSum += v;
118289      if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
118290        p->approx = p->overflow = 1;
118291      }
118292    }else{
118293      p->rSum += sqlite3_value_double(argv[0]);
118294      p->approx = 1;
118295    }
118296  }
118297}
118298#ifndef SQLITE_OMIT_WINDOWFUNC
118299static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
118300  SumCtx *p;
118301  int type;
118302  assert( argc==1 );
118303  UNUSED_PARAMETER(argc);
118304  p = sqlite3_aggregate_context(context, sizeof(*p));
118305  type = sqlite3_value_numeric_type(argv[0]);
118306  /* p is always non-NULL because sumStep() will have been called first
118307  ** to initialize it */
118308  if( ALWAYS(p) && type!=SQLITE_NULL ){
118309    assert( p->cnt>0 );
118310    p->cnt--;
118311    assert( type==SQLITE_INTEGER || p->approx );
118312    if( type==SQLITE_INTEGER && p->approx==0 ){
118313      i64 v = sqlite3_value_int64(argv[0]);
118314      p->rSum -= v;
118315      p->iSum -= v;
118316    }else{
118317      p->rSum -= sqlite3_value_double(argv[0]);
118318    }
118319  }
118320}
118321#else
118322# define sumInverse 0
118323#endif /* SQLITE_OMIT_WINDOWFUNC */
118324static void sumFinalize(sqlite3_context *context){
118325  SumCtx *p;
118326  p = sqlite3_aggregate_context(context, 0);
118327  if( p && p->cnt>0 ){
118328    if( p->overflow ){
118329      sqlite3_result_error(context,"integer overflow",-1);
118330    }else if( p->approx ){
118331      sqlite3_result_double(context, p->rSum);
118332    }else{
118333      sqlite3_result_int64(context, p->iSum);
118334    }
118335  }
118336}
118337static void avgFinalize(sqlite3_context *context){
118338  SumCtx *p;
118339  p = sqlite3_aggregate_context(context, 0);
118340  if( p && p->cnt>0 ){
118341    sqlite3_result_double(context, p->rSum/(double)p->cnt);
118342  }
118343}
118344static void totalFinalize(sqlite3_context *context){
118345  SumCtx *p;
118346  p = sqlite3_aggregate_context(context, 0);
118347  /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
118348  sqlite3_result_double(context, p ? p->rSum : (double)0);
118349}
118350
118351/*
118352** The following structure keeps track of state information for the
118353** count() aggregate function.
118354*/
118355typedef struct CountCtx CountCtx;
118356struct CountCtx {
118357  i64 n;
118358#ifdef SQLITE_DEBUG
118359  int bInverse;                   /* True if xInverse() ever called */
118360#endif
118361};
118362
118363/*
118364** Routines to implement the count() aggregate function.
118365*/
118366static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
118367  CountCtx *p;
118368  p = sqlite3_aggregate_context(context, sizeof(*p));
118369  if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
118370    p->n++;
118371  }
118372
118373#ifndef SQLITE_OMIT_DEPRECATED
118374  /* The sqlite3_aggregate_count() function is deprecated.  But just to make
118375  ** sure it still operates correctly, verify that its count agrees with our
118376  ** internal count when using count(*) and when the total count can be
118377  ** expressed as a 32-bit integer. */
118378  assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
118379          || p->n==sqlite3_aggregate_count(context) );
118380#endif
118381}
118382static void countFinalize(sqlite3_context *context){
118383  CountCtx *p;
118384  p = sqlite3_aggregate_context(context, 0);
118385  sqlite3_result_int64(context, p ? p->n : 0);
118386}
118387#ifndef SQLITE_OMIT_WINDOWFUNC
118388static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){
118389  CountCtx *p;
118390  p = sqlite3_aggregate_context(ctx, sizeof(*p));
118391  /* p is always non-NULL since countStep() will have been called first */
118392  if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && ALWAYS(p) ){
118393    p->n--;
118394#ifdef SQLITE_DEBUG
118395    p->bInverse = 1;
118396#endif
118397  }
118398}
118399#else
118400# define countInverse 0
118401#endif /* SQLITE_OMIT_WINDOWFUNC */
118402
118403/*
118404** Routines to implement min() and max() aggregate functions.
118405*/
118406static void minmaxStep(
118407  sqlite3_context *context,
118408  int NotUsed,
118409  sqlite3_value **argv
118410){
118411  Mem *pArg  = (Mem *)argv[0];
118412  Mem *pBest;
118413  UNUSED_PARAMETER(NotUsed);
118414
118415  pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
118416  if( !pBest ) return;
118417
118418  if( sqlite3_value_type(pArg)==SQLITE_NULL ){
118419    if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
118420  }else if( pBest->flags ){
118421    int max;
118422    int cmp;
118423    CollSeq *pColl = sqlite3GetFuncCollSeq(context);
118424    /* This step function is used for both the min() and max() aggregates,
118425    ** the only difference between the two being that the sense of the
118426    ** comparison is inverted. For the max() aggregate, the
118427    ** sqlite3_user_data() function returns (void *)-1. For min() it
118428    ** returns (void *)db, where db is the sqlite3* database pointer.
118429    ** Therefore the next statement sets variable 'max' to 1 for the max()
118430    ** aggregate, or 0 for min().
118431    */
118432    max = sqlite3_user_data(context)!=0;
118433    cmp = sqlite3MemCompare(pBest, pArg, pColl);
118434    if( (max && cmp<0) || (!max && cmp>0) ){
118435      sqlite3VdbeMemCopy(pBest, pArg);
118436    }else{
118437      sqlite3SkipAccumulatorLoad(context);
118438    }
118439  }else{
118440    pBest->db = sqlite3_context_db_handle(context);
118441    sqlite3VdbeMemCopy(pBest, pArg);
118442  }
118443}
118444static void minMaxValueFinalize(sqlite3_context *context, int bValue){
118445  sqlite3_value *pRes;
118446  pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
118447  if( pRes ){
118448    if( pRes->flags ){
118449      sqlite3_result_value(context, pRes);
118450    }
118451    if( bValue==0 ) sqlite3VdbeMemRelease(pRes);
118452  }
118453}
118454#ifndef SQLITE_OMIT_WINDOWFUNC
118455static void minMaxValue(sqlite3_context *context){
118456  minMaxValueFinalize(context, 1);
118457}
118458#else
118459# define minMaxValue 0
118460#endif /* SQLITE_OMIT_WINDOWFUNC */
118461static void minMaxFinalize(sqlite3_context *context){
118462  minMaxValueFinalize(context, 0);
118463}
118464
118465/*
118466** group_concat(EXPR, ?SEPARATOR?)
118467*/
118468static void groupConcatStep(
118469  sqlite3_context *context,
118470  int argc,
118471  sqlite3_value **argv
118472){
118473  const char *zVal;
118474  StrAccum *pAccum;
118475  const char *zSep;
118476  int nVal, nSep;
118477  assert( argc==1 || argc==2 );
118478  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
118479  pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
118480
118481  if( pAccum ){
118482    sqlite3 *db = sqlite3_context_db_handle(context);
118483    int firstTerm = pAccum->mxAlloc==0;
118484    pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
118485    if( !firstTerm ){
118486      if( argc==2 ){
118487        zSep = (char*)sqlite3_value_text(argv[1]);
118488        nSep = sqlite3_value_bytes(argv[1]);
118489      }else{
118490        zSep = ",";
118491        nSep = 1;
118492      }
118493      if( zSep ) sqlite3_str_append(pAccum, zSep, nSep);
118494    }
118495    zVal = (char*)sqlite3_value_text(argv[0]);
118496    nVal = sqlite3_value_bytes(argv[0]);
118497    if( zVal ) sqlite3_str_append(pAccum, zVal, nVal);
118498  }
118499}
118500#ifndef SQLITE_OMIT_WINDOWFUNC
118501static void groupConcatInverse(
118502  sqlite3_context *context,
118503  int argc,
118504  sqlite3_value **argv
118505){
118506  int n;
118507  StrAccum *pAccum;
118508  assert( argc==1 || argc==2 );
118509  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
118510  pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
118511  /* pAccum is always non-NULL since groupConcatStep() will have always
118512  ** run frist to initialize it */
118513  if( ALWAYS(pAccum) ){
118514    n = sqlite3_value_bytes(argv[0]);
118515    if( argc==2 ){
118516      n += sqlite3_value_bytes(argv[1]);
118517    }else{
118518      n++;
118519    }
118520    if( n>=(int)pAccum->nChar ){
118521      pAccum->nChar = 0;
118522    }else{
118523      pAccum->nChar -= n;
118524      memmove(pAccum->zText, &pAccum->zText[n], pAccum->nChar);
118525    }
118526    if( pAccum->nChar==0 ) pAccum->mxAlloc = 0;
118527  }
118528}
118529#else
118530# define groupConcatInverse 0
118531#endif /* SQLITE_OMIT_WINDOWFUNC */
118532static void groupConcatFinalize(sqlite3_context *context){
118533  StrAccum *pAccum;
118534  pAccum = sqlite3_aggregate_context(context, 0);
118535  if( pAccum ){
118536    if( pAccum->accError==SQLITE_TOOBIG ){
118537      sqlite3_result_error_toobig(context);
118538    }else if( pAccum->accError==SQLITE_NOMEM ){
118539      sqlite3_result_error_nomem(context);
118540    }else{
118541      sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
118542                          sqlite3_free);
118543    }
118544  }
118545}
118546#ifndef SQLITE_OMIT_WINDOWFUNC
118547static void groupConcatValue(sqlite3_context *context){
118548  sqlite3_str *pAccum;
118549  pAccum = (sqlite3_str*)sqlite3_aggregate_context(context, 0);
118550  if( pAccum ){
118551    if( pAccum->accError==SQLITE_TOOBIG ){
118552      sqlite3_result_error_toobig(context);
118553    }else if( pAccum->accError==SQLITE_NOMEM ){
118554      sqlite3_result_error_nomem(context);
118555    }else{
118556      const char *zText = sqlite3_str_value(pAccum);
118557      sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
118558    }
118559  }
118560}
118561#else
118562# define groupConcatValue 0
118563#endif /* SQLITE_OMIT_WINDOWFUNC */
118564
118565/*
118566** This routine does per-connection function registration.  Most
118567** of the built-in functions above are part of the global function set.
118568** This routine only deals with those that are not global.
118569*/
118570SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){
118571  int rc = sqlite3_overload_function(db, "MATCH", 2);
118572  assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
118573  if( rc==SQLITE_NOMEM ){
118574    sqlite3OomFault(db);
118575  }
118576}
118577
118578/*
118579** Re-register the built-in LIKE functions.  The caseSensitive
118580** parameter determines whether or not the LIKE operator is case
118581** sensitive.
118582*/
118583SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
118584  struct compareInfo *pInfo;
118585  int flags;
118586  if( caseSensitive ){
118587    pInfo = (struct compareInfo*)&likeInfoAlt;
118588    flags = SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE;
118589  }else{
118590    pInfo = (struct compareInfo*)&likeInfoNorm;
118591    flags = SQLITE_FUNC_LIKE;
118592  }
118593  sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0);
118594  sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0);
118595  sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0)->funcFlags |= flags;
118596  sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0)->funcFlags |= flags;
118597}
118598
118599/*
118600** pExpr points to an expression which implements a function.  If
118601** it is appropriate to apply the LIKE optimization to that function
118602** then set aWc[0] through aWc[2] to the wildcard characters and the
118603** escape character and then return TRUE.  If the function is not a
118604** LIKE-style function then return FALSE.
118605**
118606** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE
118607** operator if c is a string literal that is exactly one byte in length.
118608** That one byte is stored in aWc[3].  aWc[3] is set to zero if there is
118609** no ESCAPE clause.
118610**
118611** *pIsNocase is set to true if uppercase and lowercase are equivalent for
118612** the function (default for LIKE).  If the function makes the distinction
118613** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to
118614** false.
118615*/
118616SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
118617  FuncDef *pDef;
118618  int nExpr;
118619  if( pExpr->op!=TK_FUNCTION || !pExpr->x.pList ){
118620    return 0;
118621  }
118622  assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
118623  nExpr = pExpr->x.pList->nExpr;
118624  pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
118625#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
118626  if( pDef==0 ) return 0;
118627#endif
118628  if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
118629    return 0;
118630  }
118631
118632  /* The memcpy() statement assumes that the wildcard characters are
118633  ** the first three statements in the compareInfo structure.  The
118634  ** asserts() that follow verify that assumption
118635  */
118636  memcpy(aWc, pDef->pUserData, 3);
118637  assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
118638  assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
118639  assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
118640
118641  if( nExpr<3 ){
118642    aWc[3] = 0;
118643  }else{
118644    Expr *pEscape = pExpr->x.pList->a[2].pExpr;
118645    char *zEscape;
118646    if( pEscape->op!=TK_STRING ) return 0;
118647    zEscape = pEscape->u.zToken;
118648    if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
118649    if( zEscape[0]==aWc[0] ) return 0;
118650    if( zEscape[0]==aWc[1] ) return 0;
118651    aWc[3] = zEscape[0];
118652  }
118653
118654  *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
118655  return 1;
118656}
118657
118658/*
118659** All of the FuncDef structures in the aBuiltinFunc[] array above
118660** to the global function hash table.  This occurs at start-time (as
118661** a consequence of calling sqlite3_initialize()).
118662**
118663** After this routine runs
118664*/
118665SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void){
118666  /*
118667  ** The following array holds FuncDef structures for all of the functions
118668  ** defined in this file.
118669  **
118670  ** The array cannot be constant since changes are made to the
118671  ** FuncDef.pHash elements at start-time.  The elements of this array
118672  ** are read-only after initialization is complete.
118673  **
118674  ** For peak efficiency, put the most frequently used function last.
118675  */
118676  static FuncDef aBuiltinFunc[] = {
118677/***** Functions only available with SQLITE_TESTCTRL_INTERNAL_FUNCTIONS *****/
118678    TEST_FUNC(implies_nonnull_row, 2, INLINEFUNC_implies_nonnull_row, 0),
118679    TEST_FUNC(expr_compare,        2, INLINEFUNC_expr_compare,        0),
118680    TEST_FUNC(expr_implies_expr,   2, INLINEFUNC_expr_implies_expr,   0),
118681#ifdef SQLITE_DEBUG
118682    TEST_FUNC(affinity,          1, INLINEFUNC_affinity, 0),
118683#endif
118684/***** Regular functions *****/
118685#ifdef SQLITE_SOUNDEX
118686    FUNCTION(soundex,            1, 0, 0, soundexFunc      ),
118687#endif
118688#ifndef SQLITE_OMIT_LOAD_EXTENSION
118689    SFUNCTION(load_extension,    1, 0, 0, loadExt          ),
118690    SFUNCTION(load_extension,    2, 0, 0, loadExt          ),
118691#endif
118692#if SQLITE_USER_AUTHENTICATION
118693    FUNCTION(sqlite_crypt,       2, 0, 0, sqlite3CryptFunc ),
118694#endif
118695#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
118696    DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
118697    DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
118698#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
118699    INLINE_FUNC(unlikely,        1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
118700    INLINE_FUNC(likelihood,      2, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
118701    INLINE_FUNC(likely,          1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
118702#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
118703    FUNCTION2(sqlite_offset,     1, 0, 0, noopFunc,  SQLITE_FUNC_OFFSET|
118704                                                     SQLITE_FUNC_TYPEOF),
118705#endif
118706    FUNCTION(ltrim,              1, 1, 0, trimFunc         ),
118707    FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
118708    FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
118709    FUNCTION(rtrim,              2, 2, 0, trimFunc         ),
118710    FUNCTION(trim,               1, 3, 0, trimFunc         ),
118711    FUNCTION(trim,               2, 3, 0, trimFunc         ),
118712    FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
118713    FUNCTION(min,                0, 0, 1, 0                ),
118714    WAGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,
118715                                          SQLITE_FUNC_MINMAX ),
118716    FUNCTION(max,               -1, 1, 1, minmaxFunc       ),
118717    FUNCTION(max,                0, 1, 1, 0                ),
118718    WAGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,
118719                                          SQLITE_FUNC_MINMAX ),
118720    FUNCTION2(typeof,            1, 0, 0, typeofFunc,  SQLITE_FUNC_TYPEOF),
118721    FUNCTION2(length,            1, 0, 0, lengthFunc,  SQLITE_FUNC_LENGTH),
118722    FUNCTION(instr,              2, 0, 0, instrFunc        ),
118723    FUNCTION(printf,            -1, 0, 0, printfFunc       ),
118724    FUNCTION(unicode,            1, 0, 0, unicodeFunc      ),
118725    FUNCTION(char,              -1, 0, 0, charFunc         ),
118726    FUNCTION(abs,                1, 0, 0, absFunc          ),
118727#ifndef SQLITE_OMIT_FLOATING_POINT
118728    FUNCTION(round,              1, 0, 0, roundFunc        ),
118729    FUNCTION(round,              2, 0, 0, roundFunc        ),
118730#endif
118731    FUNCTION(upper,              1, 0, 0, upperFunc        ),
118732    FUNCTION(lower,              1, 0, 0, lowerFunc        ),
118733    FUNCTION(hex,                1, 0, 0, hexFunc          ),
118734    INLINE_FUNC(ifnull,          2, INLINEFUNC_coalesce, 0 ),
118735    VFUNCTION(random,            0, 0, 0, randomFunc       ),
118736    VFUNCTION(randomblob,        1, 0, 0, randomBlob       ),
118737    FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
118738    DFUNCTION(sqlite_version,    0, 0, 0, versionFunc      ),
118739    DFUNCTION(sqlite_source_id,  0, 0, 0, sourceidFunc     ),
118740    FUNCTION(sqlite_log,         2, 0, 0, errlogFunc       ),
118741    FUNCTION(quote,              1, 0, 0, quoteFunc        ),
118742    VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
118743    VFUNCTION(changes,           0, 0, 0, changes          ),
118744    VFUNCTION(total_changes,     0, 0, 0, total_changes    ),
118745    FUNCTION(replace,            3, 0, 0, replaceFunc      ),
118746    FUNCTION(zeroblob,           1, 0, 0, zeroblobFunc     ),
118747    FUNCTION(substr,             2, 0, 0, substrFunc       ),
118748    FUNCTION(substr,             3, 0, 0, substrFunc       ),
118749    WAGGREGATE(sum,   1,0,0, sumStep, sumFinalize, sumFinalize, sumInverse, 0),
118750    WAGGREGATE(total, 1,0,0, sumStep,totalFinalize,totalFinalize,sumInverse, 0),
118751    WAGGREGATE(avg,   1,0,0, sumStep, avgFinalize, avgFinalize, sumInverse, 0),
118752    WAGGREGATE(count, 0,0,0, countStep,
118753        countFinalize, countFinalize, countInverse, SQLITE_FUNC_COUNT  ),
118754    WAGGREGATE(count, 1,0,0, countStep,
118755        countFinalize, countFinalize, countInverse, 0  ),
118756    WAGGREGATE(group_concat, 1, 0, 0, groupConcatStep,
118757        groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
118758    WAGGREGATE(group_concat, 2, 0, 0, groupConcatStep,
118759        groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
118760
118761    LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
118762#ifdef SQLITE_CASE_SENSITIVE_LIKE
118763    LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
118764    LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
118765#else
118766    LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
118767    LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
118768#endif
118769#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
118770    FUNCTION(unknown,           -1, 0, 0, unknownFunc      ),
118771#endif
118772    FUNCTION(coalesce,           1, 0, 0, 0                ),
118773    FUNCTION(coalesce,           0, 0, 0, 0                ),
118774    INLINE_FUNC(coalesce,       -1, INLINEFUNC_coalesce, 0 ),
118775    INLINE_FUNC(iif,             3, INLINEFUNC_iif,      0 ),
118776  };
118777#ifndef SQLITE_OMIT_ALTERTABLE
118778  sqlite3AlterFunctions();
118779#endif
118780  sqlite3WindowFunctions();
118781  sqlite3RegisterDateTimeFunctions();
118782  sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc));
118783
118784#if 0  /* Enable to print out how the built-in functions are hashed */
118785  {
118786    int i;
118787    FuncDef *p;
118788    for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
118789      printf("FUNC-HASH %02d:", i);
118790      for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
118791        int n = sqlite3Strlen30(p->zName);
118792        int h = p->zName[0] + n;
118793        printf(" %s(%d)", p->zName, h);
118794      }
118795      printf("\n");
118796    }
118797  }
118798#endif
118799}
118800
118801/************** End of func.c ************************************************/
118802/************** Begin file fkey.c ********************************************/
118803/*
118804**
118805** The author disclaims copyright to this source code.  In place of
118806** a legal notice, here is a blessing:
118807**
118808**    May you do good and not evil.
118809**    May you find forgiveness for yourself and forgive others.
118810**    May you share freely, never taking more than you give.
118811**
118812*************************************************************************
118813** This file contains code used by the compiler to add foreign key
118814** support to compiled SQL statements.
118815*/
118816/* #include "sqliteInt.h" */
118817
118818#ifndef SQLITE_OMIT_FOREIGN_KEY
118819#ifndef SQLITE_OMIT_TRIGGER
118820
118821/*
118822** Deferred and Immediate FKs
118823** --------------------------
118824**
118825** Foreign keys in SQLite come in two flavours: deferred and immediate.
118826** If an immediate foreign key constraint is violated,
118827** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
118828** statement transaction rolled back. If a
118829** deferred foreign key constraint is violated, no action is taken
118830** immediately. However if the application attempts to commit the
118831** transaction before fixing the constraint violation, the attempt fails.
118832**
118833** Deferred constraints are implemented using a simple counter associated
118834** with the database handle. The counter is set to zero each time a
118835** database transaction is opened. Each time a statement is executed
118836** that causes a foreign key violation, the counter is incremented. Each
118837** time a statement is executed that removes an existing violation from
118838** the database, the counter is decremented. When the transaction is
118839** committed, the commit fails if the current value of the counter is
118840** greater than zero. This scheme has two big drawbacks:
118841**
118842**   * When a commit fails due to a deferred foreign key constraint,
118843**     there is no way to tell which foreign constraint is not satisfied,
118844**     or which row it is not satisfied for.
118845**
118846**   * If the database contains foreign key violations when the
118847**     transaction is opened, this may cause the mechanism to malfunction.
118848**
118849** Despite these problems, this approach is adopted as it seems simpler
118850** than the alternatives.
118851**
118852** INSERT operations:
118853**
118854**   I.1) For each FK for which the table is the child table, search
118855**        the parent table for a match. If none is found increment the
118856**        constraint counter.
118857**
118858**   I.2) For each FK for which the table is the parent table,
118859**        search the child table for rows that correspond to the new
118860**        row in the parent table. Decrement the counter for each row
118861**        found (as the constraint is now satisfied).
118862**
118863** DELETE operations:
118864**
118865**   D.1) For each FK for which the table is the child table,
118866**        search the parent table for a row that corresponds to the
118867**        deleted row in the child table. If such a row is not found,
118868**        decrement the counter.
118869**
118870**   D.2) For each FK for which the table is the parent table, search
118871**        the child table for rows that correspond to the deleted row
118872**        in the parent table. For each found increment the counter.
118873**
118874** UPDATE operations:
118875**
118876**   An UPDATE command requires that all 4 steps above are taken, but only
118877**   for FK constraints for which the affected columns are actually
118878**   modified (values must be compared at runtime).
118879**
118880** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
118881** This simplifies the implementation a bit.
118882**
118883** For the purposes of immediate FK constraints, the OR REPLACE conflict
118884** resolution is considered to delete rows before the new row is inserted.
118885** If a delete caused by OR REPLACE violates an FK constraint, an exception
118886** is thrown, even if the FK constraint would be satisfied after the new
118887** row is inserted.
118888**
118889** Immediate constraints are usually handled similarly. The only difference
118890** is that the counter used is stored as part of each individual statement
118891** object (struct Vdbe). If, after the statement has run, its immediate
118892** constraint counter is greater than zero,
118893** it returns SQLITE_CONSTRAINT_FOREIGNKEY
118894** and the statement transaction is rolled back. An exception is an INSERT
118895** statement that inserts a single row only (no triggers). In this case,
118896** instead of using a counter, an exception is thrown immediately if the
118897** INSERT violates a foreign key constraint. This is necessary as such
118898** an INSERT does not open a statement transaction.
118899**
118900** TODO: How should dropping a table be handled? How should renaming a
118901** table be handled?
118902**
118903**
118904** Query API Notes
118905** ---------------
118906**
118907** Before coding an UPDATE or DELETE row operation, the code-generator
118908** for those two operations needs to know whether or not the operation
118909** requires any FK processing and, if so, which columns of the original
118910** row are required by the FK processing VDBE code (i.e. if FKs were
118911** implemented using triggers, which of the old.* columns would be
118912** accessed). No information is required by the code-generator before
118913** coding an INSERT operation. The functions used by the UPDATE/DELETE
118914** generation code to query for this information are:
118915**
118916**   sqlite3FkRequired() - Test to see if FK processing is required.
118917**   sqlite3FkOldmask()  - Query for the set of required old.* columns.
118918**
118919**
118920** Externally accessible module functions
118921** --------------------------------------
118922**
118923**   sqlite3FkCheck()    - Check for foreign key violations.
118924**   sqlite3FkActions()  - Code triggers for ON UPDATE/ON DELETE actions.
118925**   sqlite3FkDelete()   - Delete an FKey structure.
118926*/
118927
118928/*
118929** VDBE Calling Convention
118930** -----------------------
118931**
118932** Example:
118933**
118934**   For the following INSERT statement:
118935**
118936**     CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
118937**     INSERT INTO t1 VALUES(1, 2, 3.1);
118938**
118939**   Register (x):        2    (type integer)
118940**   Register (x+1):      1    (type integer)
118941**   Register (x+2):      NULL (type NULL)
118942**   Register (x+3):      3.1  (type real)
118943*/
118944
118945/*
118946** A foreign key constraint requires that the key columns in the parent
118947** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
118948** Given that pParent is the parent table for foreign key constraint pFKey,
118949** search the schema for a unique index on the parent key columns.
118950**
118951** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
118952** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
118953** is set to point to the unique index.
118954**
118955** If the parent key consists of a single column (the foreign key constraint
118956** is not a composite foreign key), output variable *paiCol is set to NULL.
118957** Otherwise, it is set to point to an allocated array of size N, where
118958** N is the number of columns in the parent key. The first element of the
118959** array is the index of the child table column that is mapped by the FK
118960** constraint to the parent table column stored in the left-most column
118961** of index *ppIdx. The second element of the array is the index of the
118962** child table column that corresponds to the second left-most column of
118963** *ppIdx, and so on.
118964**
118965** If the required index cannot be found, either because:
118966**
118967**   1) The named parent key columns do not exist, or
118968**
118969**   2) The named parent key columns do exist, but are not subject to a
118970**      UNIQUE or PRIMARY KEY constraint, or
118971**
118972**   3) No parent key columns were provided explicitly as part of the
118973**      foreign key definition, and the parent table does not have a
118974**      PRIMARY KEY, or
118975**
118976**   4) No parent key columns were provided explicitly as part of the
118977**      foreign key definition, and the PRIMARY KEY of the parent table
118978**      consists of a different number of columns to the child key in
118979**      the child table.
118980**
118981** then non-zero is returned, and a "foreign key mismatch" error loaded
118982** into pParse. If an OOM error occurs, non-zero is returned and the
118983** pParse->db->mallocFailed flag is set.
118984*/
118985SQLITE_PRIVATE int sqlite3FkLocateIndex(
118986  Parse *pParse,                  /* Parse context to store any error in */
118987  Table *pParent,                 /* Parent table of FK constraint pFKey */
118988  FKey *pFKey,                    /* Foreign key to find index for */
118989  Index **ppIdx,                  /* OUT: Unique index on parent table */
118990  int **paiCol                    /* OUT: Map of index columns in pFKey */
118991){
118992  Index *pIdx = 0;                    /* Value to return via *ppIdx */
118993  int *aiCol = 0;                     /* Value to return via *paiCol */
118994  int nCol = pFKey->nCol;             /* Number of columns in parent key */
118995  char *zKey = pFKey->aCol[0].zCol;   /* Name of left-most parent key column */
118996
118997  /* The caller is responsible for zeroing output parameters. */
118998  assert( ppIdx && *ppIdx==0 );
118999  assert( !paiCol || *paiCol==0 );
119000  assert( pParse );
119001
119002  /* If this is a non-composite (single column) foreign key, check if it
119003  ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
119004  ** and *paiCol set to zero and return early.
119005  **
119006  ** Otherwise, for a composite foreign key (more than one column), allocate
119007  ** space for the aiCol array (returned via output parameter *paiCol).
119008  ** Non-composite foreign keys do not require the aiCol array.
119009  */
119010  if( nCol==1 ){
119011    /* The FK maps to the IPK if any of the following are true:
119012    **
119013    **   1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
119014    **      mapped to the primary key of table pParent, or
119015    **   2) The FK is explicitly mapped to a column declared as INTEGER
119016    **      PRIMARY KEY.
119017    */
119018    if( pParent->iPKey>=0 ){
119019      if( !zKey ) return 0;
119020      if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
119021    }
119022  }else if( paiCol ){
119023    assert( nCol>1 );
119024    aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
119025    if( !aiCol ) return 1;
119026    *paiCol = aiCol;
119027  }
119028
119029  for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
119030    if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
119031      /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
119032      ** of columns. If each indexed column corresponds to a foreign key
119033      ** column of pFKey, then this index is a winner.  */
119034
119035      if( zKey==0 ){
119036        /* If zKey is NULL, then this foreign key is implicitly mapped to
119037        ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
119038        ** identified by the test.  */
119039        if( IsPrimaryKeyIndex(pIdx) ){
119040          if( aiCol ){
119041            int i;
119042            for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
119043          }
119044          break;
119045        }
119046      }else{
119047        /* If zKey is non-NULL, then this foreign key was declared to
119048        ** map to an explicit list of columns in table pParent. Check if this
119049        ** index matches those columns. Also, check that the index uses
119050        ** the default collation sequences for each column. */
119051        int i, j;
119052        for(i=0; i<nCol; i++){
119053          i16 iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
119054          const char *zDfltColl;            /* Def. collation for column */
119055          char *zIdxCol;                    /* Name of indexed column */
119056
119057          if( iCol<0 ) break; /* No foreign keys against expression indexes */
119058
119059          /* If the index uses a collation sequence that is different from
119060          ** the default collation sequence for the column, this index is
119061          ** unusable. Bail out early in this case.  */
119062          zDfltColl = pParent->aCol[iCol].zColl;
119063          if( !zDfltColl ) zDfltColl = sqlite3StrBINARY;
119064          if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
119065
119066          zIdxCol = pParent->aCol[iCol].zName;
119067          for(j=0; j<nCol; j++){
119068            if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
119069              if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
119070              break;
119071            }
119072          }
119073          if( j==nCol ) break;
119074        }
119075        if( i==nCol ) break;      /* pIdx is usable */
119076      }
119077    }
119078  }
119079
119080  if( !pIdx ){
119081    if( !pParse->disableTriggers ){
119082      sqlite3ErrorMsg(pParse,
119083           "foreign key mismatch - \"%w\" referencing \"%w\"",
119084           pFKey->pFrom->zName, pFKey->zTo);
119085    }
119086    sqlite3DbFree(pParse->db, aiCol);
119087    return 1;
119088  }
119089
119090  *ppIdx = pIdx;
119091  return 0;
119092}
119093
119094/*
119095** This function is called when a row is inserted into or deleted from the
119096** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
119097** on the child table of pFKey, this function is invoked twice for each row
119098** affected - once to "delete" the old row, and then again to "insert" the
119099** new row.
119100**
119101** Each time it is called, this function generates VDBE code to locate the
119102** row in the parent table that corresponds to the row being inserted into
119103** or deleted from the child table. If the parent row can be found, no
119104** special action is taken. Otherwise, if the parent row can *not* be
119105** found in the parent table:
119106**
119107**   Operation | FK type   | Action taken
119108**   --------------------------------------------------------------------------
119109**   INSERT      immediate   Increment the "immediate constraint counter".
119110**
119111**   DELETE      immediate   Decrement the "immediate constraint counter".
119112**
119113**   INSERT      deferred    Increment the "deferred constraint counter".
119114**
119115**   DELETE      deferred    Decrement the "deferred constraint counter".
119116**
119117** These operations are identified in the comment at the top of this file
119118** (fkey.c) as "I.1" and "D.1".
119119*/
119120static void fkLookupParent(
119121  Parse *pParse,        /* Parse context */
119122  int iDb,              /* Index of database housing pTab */
119123  Table *pTab,          /* Parent table of FK pFKey */
119124  Index *pIdx,          /* Unique index on parent key columns in pTab */
119125  FKey *pFKey,          /* Foreign key constraint */
119126  int *aiCol,           /* Map from parent key columns to child table columns */
119127  int regData,          /* Address of array containing child table row */
119128  int nIncr,            /* Increment constraint counter by this */
119129  int isIgnore          /* If true, pretend pTab contains all NULL values */
119130){
119131  int i;                                    /* Iterator variable */
119132  Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
119133  int iCur = pParse->nTab - 1;              /* Cursor number to use */
119134  int iOk = sqlite3VdbeMakeLabel(pParse);   /* jump here if parent key found */
119135
119136  sqlite3VdbeVerifyAbortable(v,
119137    (!pFKey->isDeferred
119138      && !(pParse->db->flags & SQLITE_DeferFKs)
119139      && !pParse->pToplevel
119140      && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
119141
119142  /* If nIncr is less than zero, then check at runtime if there are any
119143  ** outstanding constraints to resolve. If there are not, there is no need
119144  ** to check if deleting this row resolves any outstanding violations.
119145  **
119146  ** Check if any of the key columns in the child table row are NULL. If
119147  ** any are, then the constraint is considered satisfied. No need to
119148  ** search for a matching row in the parent table.  */
119149  if( nIncr<0 ){
119150    sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
119151    VdbeCoverage(v);
119152  }
119153  for(i=0; i<pFKey->nCol; i++){
119154    int iReg = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + regData + 1;
119155    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk); VdbeCoverage(v);
119156  }
119157
119158  if( isIgnore==0 ){
119159    if( pIdx==0 ){
119160      /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
119161      ** column of the parent table (table pTab).  */
119162      int iMustBeInt;               /* Address of MustBeInt instruction */
119163      int regTemp = sqlite3GetTempReg(pParse);
119164
119165      /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
119166      ** apply the affinity of the parent key). If this fails, then there
119167      ** is no matching parent key. Before using MustBeInt, make a copy of
119168      ** the value. Otherwise, the value inserted into the child key column
119169      ** will have INTEGER affinity applied to it, which may not be correct.  */
119170      sqlite3VdbeAddOp2(v, OP_SCopy,
119171        sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[0])+1+regData, regTemp);
119172      iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
119173      VdbeCoverage(v);
119174
119175      /* If the parent table is the same as the child table, and we are about
119176      ** to increment the constraint-counter (i.e. this is an INSERT operation),
119177      ** then check if the row being inserted matches itself. If so, do not
119178      ** increment the constraint-counter.  */
119179      if( pTab==pFKey->pFrom && nIncr==1 ){
119180        sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp); VdbeCoverage(v);
119181        sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
119182      }
119183
119184      sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
119185      sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp); VdbeCoverage(v);
119186      sqlite3VdbeGoto(v, iOk);
119187      sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
119188      sqlite3VdbeJumpHere(v, iMustBeInt);
119189      sqlite3ReleaseTempReg(pParse, regTemp);
119190    }else{
119191      int nCol = pFKey->nCol;
119192      int regTemp = sqlite3GetTempRange(pParse, nCol);
119193      int regRec = sqlite3GetTempReg(pParse);
119194
119195      sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
119196      sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
119197      for(i=0; i<nCol; i++){
119198        sqlite3VdbeAddOp2(v, OP_Copy,
119199               sqlite3TableColumnToStorage(pFKey->pFrom, aiCol[i])+1+regData,
119200               regTemp+i);
119201      }
119202
119203      /* If the parent table is the same as the child table, and we are about
119204      ** to increment the constraint-counter (i.e. this is an INSERT operation),
119205      ** then check if the row being inserted matches itself. If so, do not
119206      ** increment the constraint-counter.
119207      **
119208      ** If any of the parent-key values are NULL, then the row cannot match
119209      ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
119210      ** of the parent-key values are NULL (at this point it is known that
119211      ** none of the child key values are).
119212      */
119213      if( pTab==pFKey->pFrom && nIncr==1 ){
119214        int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
119215        for(i=0; i<nCol; i++){
119216          int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i])
119217                              +1+regData;
119218          int iParent = 1+regData;
119219          iParent += sqlite3TableColumnToStorage(pIdx->pTable,
119220                                                 pIdx->aiColumn[i]);
119221          assert( pIdx->aiColumn[i]>=0 );
119222          assert( aiCol[i]!=pTab->iPKey );
119223          if( pIdx->aiColumn[i]==pTab->iPKey ){
119224            /* The parent key is a composite key that includes the IPK column */
119225            iParent = regData;
119226          }
119227          sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent); VdbeCoverage(v);
119228          sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
119229        }
119230        sqlite3VdbeGoto(v, iOk);
119231      }
119232
119233      sqlite3VdbeAddOp4(v, OP_MakeRecord, regTemp, nCol, regRec,
119234                        sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
119235      sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0); VdbeCoverage(v);
119236
119237      sqlite3ReleaseTempReg(pParse, regRec);
119238      sqlite3ReleaseTempRange(pParse, regTemp, nCol);
119239    }
119240  }
119241
119242  if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
119243   && !pParse->pToplevel
119244   && !pParse->isMultiWrite
119245  ){
119246    /* Special case: If this is an INSERT statement that will insert exactly
119247    ** one row into the table, raise a constraint immediately instead of
119248    ** incrementing a counter. This is necessary as the VM code is being
119249    ** generated for will not open a statement transaction.  */
119250    assert( nIncr==1 );
119251    sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
119252        OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
119253  }else{
119254    if( nIncr>0 && pFKey->isDeferred==0 ){
119255      sqlite3MayAbort(pParse);
119256    }
119257    sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
119258  }
119259
119260  sqlite3VdbeResolveLabel(v, iOk);
119261  sqlite3VdbeAddOp1(v, OP_Close, iCur);
119262}
119263
119264
119265/*
119266** Return an Expr object that refers to a memory register corresponding
119267** to column iCol of table pTab.
119268**
119269** regBase is the first of an array of register that contains the data
119270** for pTab.  regBase itself holds the rowid.  regBase+1 holds the first
119271** column.  regBase+2 holds the second column, and so forth.
119272*/
119273static Expr *exprTableRegister(
119274  Parse *pParse,     /* Parsing and code generating context */
119275  Table *pTab,       /* The table whose content is at r[regBase]... */
119276  int regBase,       /* Contents of table pTab */
119277  i16 iCol           /* Which column of pTab is desired */
119278){
119279  Expr *pExpr;
119280  Column *pCol;
119281  const char *zColl;
119282  sqlite3 *db = pParse->db;
119283
119284  pExpr = sqlite3Expr(db, TK_REGISTER, 0);
119285  if( pExpr ){
119286    if( iCol>=0 && iCol!=pTab->iPKey ){
119287      pCol = &pTab->aCol[iCol];
119288      pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
119289      pExpr->affExpr = pCol->affinity;
119290      zColl = pCol->zColl;
119291      if( zColl==0 ) zColl = db->pDfltColl->zName;
119292      pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
119293    }else{
119294      pExpr->iTable = regBase;
119295      pExpr->affExpr = SQLITE_AFF_INTEGER;
119296    }
119297  }
119298  return pExpr;
119299}
119300
119301/*
119302** Return an Expr object that refers to column iCol of table pTab which
119303** has cursor iCur.
119304*/
119305static Expr *exprTableColumn(
119306  sqlite3 *db,      /* The database connection */
119307  Table *pTab,      /* The table whose column is desired */
119308  int iCursor,      /* The open cursor on the table */
119309  i16 iCol          /* The column that is wanted */
119310){
119311  Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0);
119312  if( pExpr ){
119313    pExpr->y.pTab = pTab;
119314    pExpr->iTable = iCursor;
119315    pExpr->iColumn = iCol;
119316  }
119317  return pExpr;
119318}
119319
119320/*
119321** This function is called to generate code executed when a row is deleted
119322** from the parent table of foreign key constraint pFKey and, if pFKey is
119323** deferred, when a row is inserted into the same table. When generating
119324** code for an SQL UPDATE operation, this function may be called twice -
119325** once to "delete" the old row and once to "insert" the new row.
119326**
119327** Parameter nIncr is passed -1 when inserting a row (as this may decrease
119328** the number of FK violations in the db) or +1 when deleting one (as this
119329** may increase the number of FK constraint problems).
119330**
119331** The code generated by this function scans through the rows in the child
119332** table that correspond to the parent table row being deleted or inserted.
119333** For each child row found, one of the following actions is taken:
119334**
119335**   Operation | FK type   | Action taken
119336**   --------------------------------------------------------------------------
119337**   DELETE      immediate   Increment the "immediate constraint counter".
119338**                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
119339**                           throw a "FOREIGN KEY constraint failed" exception.
119340**
119341**   INSERT      immediate   Decrement the "immediate constraint counter".
119342**
119343**   DELETE      deferred    Increment the "deferred constraint counter".
119344**                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
119345**                           throw a "FOREIGN KEY constraint failed" exception.
119346**
119347**   INSERT      deferred    Decrement the "deferred constraint counter".
119348**
119349** These operations are identified in the comment at the top of this file
119350** (fkey.c) as "I.2" and "D.2".
119351*/
119352static void fkScanChildren(
119353  Parse *pParse,                  /* Parse context */
119354  SrcList *pSrc,                  /* The child table to be scanned */
119355  Table *pTab,                    /* The parent table */
119356  Index *pIdx,                    /* Index on parent covering the foreign key */
119357  FKey *pFKey,                    /* The foreign key linking pSrc to pTab */
119358  int *aiCol,                     /* Map from pIdx cols to child table cols */
119359  int regData,                    /* Parent row data starts here */
119360  int nIncr                       /* Amount to increment deferred counter by */
119361){
119362  sqlite3 *db = pParse->db;       /* Database handle */
119363  int i;                          /* Iterator variable */
119364  Expr *pWhere = 0;               /* WHERE clause to scan with */
119365  NameContext sNameContext;       /* Context used to resolve WHERE clause */
119366  WhereInfo *pWInfo;              /* Context used by sqlite3WhereXXX() */
119367  int iFkIfZero = 0;              /* Address of OP_FkIfZero */
119368  Vdbe *v = sqlite3GetVdbe(pParse);
119369
119370  assert( pIdx==0 || pIdx->pTable==pTab );
119371  assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
119372  assert( pIdx!=0 || pFKey->nCol==1 );
119373  assert( pIdx!=0 || HasRowid(pTab) );
119374
119375  if( nIncr<0 ){
119376    iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
119377    VdbeCoverage(v);
119378  }
119379
119380  /* Create an Expr object representing an SQL expression like:
119381  **
119382  **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
119383  **
119384  ** The collation sequence used for the comparison should be that of
119385  ** the parent key columns. The affinity of the parent key column should
119386  ** be applied to each child key value before the comparison takes place.
119387  */
119388  for(i=0; i<pFKey->nCol; i++){
119389    Expr *pLeft;                  /* Value from parent table row */
119390    Expr *pRight;                 /* Column ref to child table */
119391    Expr *pEq;                    /* Expression (pLeft = pRight) */
119392    i16 iCol;                     /* Index of column in child table */
119393    const char *zCol;             /* Name of column in child table */
119394
119395    iCol = pIdx ? pIdx->aiColumn[i] : -1;
119396    pLeft = exprTableRegister(pParse, pTab, regData, iCol);
119397    iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
119398    assert( iCol>=0 );
119399    zCol = pFKey->pFrom->aCol[iCol].zName;
119400    pRight = sqlite3Expr(db, TK_ID, zCol);
119401    pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
119402    pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
119403  }
119404
119405  /* If the child table is the same as the parent table, then add terms
119406  ** to the WHERE clause that prevent this entry from being scanned.
119407  ** The added WHERE clause terms are like this:
119408  **
119409  **     $current_rowid!=rowid
119410  **     NOT( $current_a==a AND $current_b==b AND ... )
119411  **
119412  ** The first form is used for rowid tables.  The second form is used
119413  ** for WITHOUT ROWID tables. In the second form, the *parent* key is
119414  ** (a,b,...). Either the parent or primary key could be used to
119415  ** uniquely identify the current row, but the parent key is more convenient
119416  ** as the required values have already been loaded into registers
119417  ** by the caller.
119418  */
119419  if( pTab==pFKey->pFrom && nIncr>0 ){
119420    Expr *pNe;                    /* Expression (pLeft != pRight) */
119421    Expr *pLeft;                  /* Value from parent table row */
119422    Expr *pRight;                 /* Column ref to child table */
119423    if( HasRowid(pTab) ){
119424      pLeft = exprTableRegister(pParse, pTab, regData, -1);
119425      pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
119426      pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight);
119427    }else{
119428      Expr *pEq, *pAll = 0;
119429      assert( pIdx!=0 );
119430      for(i=0; i<pIdx->nKeyCol; i++){
119431        i16 iCol = pIdx->aiColumn[i];
119432        assert( iCol>=0 );
119433        pLeft = exprTableRegister(pParse, pTab, regData, iCol);
119434        pRight = sqlite3Expr(db, TK_ID, pTab->aCol[iCol].zName);
119435        pEq = sqlite3PExpr(pParse, TK_IS, pLeft, pRight);
119436        pAll = sqlite3ExprAnd(pParse, pAll, pEq);
119437      }
119438      pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0);
119439    }
119440    pWhere = sqlite3ExprAnd(pParse, pWhere, pNe);
119441  }
119442
119443  /* Resolve the references in the WHERE clause. */
119444  memset(&sNameContext, 0, sizeof(NameContext));
119445  sNameContext.pSrcList = pSrc;
119446  sNameContext.pParse = pParse;
119447  sqlite3ResolveExprNames(&sNameContext, pWhere);
119448
119449  /* Create VDBE to loop through the entries in pSrc that match the WHERE
119450  ** clause. For each row found, increment either the deferred or immediate
119451  ** foreign key constraint counter. */
119452  if( pParse->nErr==0 ){
119453    pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
119454    sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
119455    if( pWInfo ){
119456      sqlite3WhereEnd(pWInfo);
119457    }
119458  }
119459
119460  /* Clean up the WHERE clause constructed above. */
119461  sqlite3ExprDelete(db, pWhere);
119462  if( iFkIfZero ){
119463    sqlite3VdbeJumpHereOrPopInst(v, iFkIfZero);
119464  }
119465}
119466
119467/*
119468** This function returns a linked list of FKey objects (connected by
119469** FKey.pNextTo) holding all children of table pTab.  For example,
119470** given the following schema:
119471**
119472**   CREATE TABLE t1(a PRIMARY KEY);
119473**   CREATE TABLE t2(b REFERENCES t1(a);
119474**
119475** Calling this function with table "t1" as an argument returns a pointer
119476** to the FKey structure representing the foreign key constraint on table
119477** "t2". Calling this function with "t2" as the argument would return a
119478** NULL pointer (as there are no FK constraints for which t2 is the parent
119479** table).
119480*/
119481SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
119482  return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
119483}
119484
119485/*
119486** The second argument is a Trigger structure allocated by the
119487** fkActionTrigger() routine. This function deletes the Trigger structure
119488** and all of its sub-components.
119489**
119490** The Trigger structure or any of its sub-components may be allocated from
119491** the lookaside buffer belonging to database handle dbMem.
119492*/
119493static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
119494  if( p ){
119495    TriggerStep *pStep = p->step_list;
119496    sqlite3ExprDelete(dbMem, pStep->pWhere);
119497    sqlite3ExprListDelete(dbMem, pStep->pExprList);
119498    sqlite3SelectDelete(dbMem, pStep->pSelect);
119499    sqlite3ExprDelete(dbMem, p->pWhen);
119500    sqlite3DbFree(dbMem, p);
119501  }
119502}
119503
119504/*
119505** This function is called to generate code that runs when table pTab is
119506** being dropped from the database. The SrcList passed as the second argument
119507** to this function contains a single entry guaranteed to resolve to
119508** table pTab.
119509**
119510** Normally, no code is required. However, if either
119511**
119512**   (a) The table is the parent table of a FK constraint, or
119513**   (b) The table is the child table of a deferred FK constraint and it is
119514**       determined at runtime that there are outstanding deferred FK
119515**       constraint violations in the database,
119516**
119517** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
119518** the table from the database. Triggers are disabled while running this
119519** DELETE, but foreign key actions are not.
119520*/
119521SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
119522  sqlite3 *db = pParse->db;
119523  if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) ){
119524    int iSkip = 0;
119525    Vdbe *v = sqlite3GetVdbe(pParse);
119526
119527    assert( v );                  /* VDBE has already been allocated */
119528    assert( pTab->pSelect==0 );   /* Not a view */
119529    if( sqlite3FkReferences(pTab)==0 ){
119530      /* Search for a deferred foreign key constraint for which this table
119531      ** is the child table. If one cannot be found, return without
119532      ** generating any VDBE code. If one can be found, then jump over
119533      ** the entire DELETE if there are no outstanding deferred constraints
119534      ** when this statement is run.  */
119535      FKey *p;
119536      for(p=pTab->pFKey; p; p=p->pNextFrom){
119537        if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
119538      }
119539      if( !p ) return;
119540      iSkip = sqlite3VdbeMakeLabel(pParse);
119541      sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); VdbeCoverage(v);
119542    }
119543
119544    pParse->disableTriggers = 1;
119545    sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0, 0, 0);
119546    pParse->disableTriggers = 0;
119547
119548    /* If the DELETE has generated immediate foreign key constraint
119549    ** violations, halt the VDBE and return an error at this point, before
119550    ** any modifications to the schema are made. This is because statement
119551    ** transactions are not able to rollback schema changes.
119552    **
119553    ** If the SQLITE_DeferFKs flag is set, then this is not required, as
119554    ** the statement transaction will not be rolled back even if FK
119555    ** constraints are violated.
119556    */
119557    if( (db->flags & SQLITE_DeferFKs)==0 ){
119558      sqlite3VdbeVerifyAbortable(v, OE_Abort);
119559      sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
119560      VdbeCoverage(v);
119561      sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
119562          OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
119563    }
119564
119565    if( iSkip ){
119566      sqlite3VdbeResolveLabel(v, iSkip);
119567    }
119568  }
119569}
119570
119571
119572/*
119573** The second argument points to an FKey object representing a foreign key
119574** for which pTab is the child table. An UPDATE statement against pTab
119575** is currently being processed. For each column of the table that is
119576** actually updated, the corresponding element in the aChange[] array
119577** is zero or greater (if a column is unmodified the corresponding element
119578** is set to -1). If the rowid column is modified by the UPDATE statement
119579** the bChngRowid argument is non-zero.
119580**
119581** This function returns true if any of the columns that are part of the
119582** child key for FK constraint *p are modified.
119583*/
119584static int fkChildIsModified(
119585  Table *pTab,                    /* Table being updated */
119586  FKey *p,                        /* Foreign key for which pTab is the child */
119587  int *aChange,                   /* Array indicating modified columns */
119588  int bChngRowid                  /* True if rowid is modified by this update */
119589){
119590  int i;
119591  for(i=0; i<p->nCol; i++){
119592    int iChildKey = p->aCol[i].iFrom;
119593    if( aChange[iChildKey]>=0 ) return 1;
119594    if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
119595  }
119596  return 0;
119597}
119598
119599/*
119600** The second argument points to an FKey object representing a foreign key
119601** for which pTab is the parent table. An UPDATE statement against pTab
119602** is currently being processed. For each column of the table that is
119603** actually updated, the corresponding element in the aChange[] array
119604** is zero or greater (if a column is unmodified the corresponding element
119605** is set to -1). If the rowid column is modified by the UPDATE statement
119606** the bChngRowid argument is non-zero.
119607**
119608** This function returns true if any of the columns that are part of the
119609** parent key for FK constraint *p are modified.
119610*/
119611static int fkParentIsModified(
119612  Table *pTab,
119613  FKey *p,
119614  int *aChange,
119615  int bChngRowid
119616){
119617  int i;
119618  for(i=0; i<p->nCol; i++){
119619    char *zKey = p->aCol[i].zCol;
119620    int iKey;
119621    for(iKey=0; iKey<pTab->nCol; iKey++){
119622      if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
119623        Column *pCol = &pTab->aCol[iKey];
119624        if( zKey ){
119625          if( 0==sqlite3StrICmp(pCol->zName, zKey) ) return 1;
119626        }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
119627          return 1;
119628        }
119629      }
119630    }
119631  }
119632  return 0;
119633}
119634
119635/*
119636** Return true if the parser passed as the first argument is being
119637** used to code a trigger that is really a "SET NULL" action belonging
119638** to trigger pFKey.
119639*/
119640static int isSetNullAction(Parse *pParse, FKey *pFKey){
119641  Parse *pTop = sqlite3ParseToplevel(pParse);
119642  if( pTop->pTriggerPrg ){
119643    Trigger *p = pTop->pTriggerPrg->pTrigger;
119644    if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
119645     || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
119646    ){
119647      return 1;
119648    }
119649  }
119650  return 0;
119651}
119652
119653/*
119654** This function is called when inserting, deleting or updating a row of
119655** table pTab to generate VDBE code to perform foreign key constraint
119656** processing for the operation.
119657**
119658** For a DELETE operation, parameter regOld is passed the index of the
119659** first register in an array of (pTab->nCol+1) registers containing the
119660** rowid of the row being deleted, followed by each of the column values
119661** of the row being deleted, from left to right. Parameter regNew is passed
119662** zero in this case.
119663**
119664** For an INSERT operation, regOld is passed zero and regNew is passed the
119665** first register of an array of (pTab->nCol+1) registers containing the new
119666** row data.
119667**
119668** For an UPDATE operation, this function is called twice. Once before
119669** the original record is deleted from the table using the calling convention
119670** described for DELETE. Then again after the original record is deleted
119671** but before the new record is inserted using the INSERT convention.
119672*/
119673SQLITE_PRIVATE void sqlite3FkCheck(
119674  Parse *pParse,                  /* Parse context */
119675  Table *pTab,                    /* Row is being deleted from this table */
119676  int regOld,                     /* Previous row data is stored here */
119677  int regNew,                     /* New row data is stored here */
119678  int *aChange,                   /* Array indicating UPDATEd columns (or 0) */
119679  int bChngRowid                  /* True if rowid is UPDATEd */
119680){
119681  sqlite3 *db = pParse->db;       /* Database handle */
119682  FKey *pFKey;                    /* Used to iterate through FKs */
119683  int iDb;                        /* Index of database containing pTab */
119684  const char *zDb;                /* Name of database containing pTab */
119685  int isIgnoreErrors = pParse->disableTriggers;
119686
119687  /* Exactly one of regOld and regNew should be non-zero. */
119688  assert( (regOld==0)!=(regNew==0) );
119689
119690  /* If foreign-keys are disabled, this function is a no-op. */
119691  if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
119692
119693  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
119694  zDb = db->aDb[iDb].zDbSName;
119695
119696  /* Loop through all the foreign key constraints for which pTab is the
119697  ** child table (the table that the foreign key definition is part of).  */
119698  for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
119699    Table *pTo;                   /* Parent table of foreign key pFKey */
119700    Index *pIdx = 0;              /* Index on key columns in pTo */
119701    int *aiFree = 0;
119702    int *aiCol;
119703    int iCol;
119704    int i;
119705    int bIgnore = 0;
119706
119707    if( aChange
119708     && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
119709     && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0
119710    ){
119711      continue;
119712    }
119713
119714    /* Find the parent table of this foreign key. Also find a unique index
119715    ** on the parent key columns in the parent table. If either of these
119716    ** schema items cannot be located, set an error in pParse and return
119717    ** early.  */
119718    if( pParse->disableTriggers ){
119719      pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
119720    }else{
119721      pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
119722    }
119723    if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
119724      assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
119725      if( !isIgnoreErrors || db->mallocFailed ) return;
119726      if( pTo==0 ){
119727        /* If isIgnoreErrors is true, then a table is being dropped. In this
119728        ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
119729        ** before actually dropping it in order to check FK constraints.
119730        ** If the parent table of an FK constraint on the current table is
119731        ** missing, behave as if it is empty. i.e. decrement the relevant
119732        ** FK counter for each row of the current table with non-NULL keys.
119733        */
119734        Vdbe *v = sqlite3GetVdbe(pParse);
119735        int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
119736        for(i=0; i<pFKey->nCol; i++){
119737          int iFromCol, iReg;
119738          iFromCol = pFKey->aCol[i].iFrom;
119739          iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1;
119740          sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); VdbeCoverage(v);
119741        }
119742        sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
119743      }
119744      continue;
119745    }
119746    assert( pFKey->nCol==1 || (aiFree && pIdx) );
119747
119748    if( aiFree ){
119749      aiCol = aiFree;
119750    }else{
119751      iCol = pFKey->aCol[0].iFrom;
119752      aiCol = &iCol;
119753    }
119754    for(i=0; i<pFKey->nCol; i++){
119755      if( aiCol[i]==pTab->iPKey ){
119756        aiCol[i] = -1;
119757      }
119758      assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
119759#ifndef SQLITE_OMIT_AUTHORIZATION
119760      /* Request permission to read the parent key columns. If the
119761      ** authorization callback returns SQLITE_IGNORE, behave as if any
119762      ** values read from the parent table are NULL. */
119763      if( db->xAuth ){
119764        int rcauth;
119765        char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
119766        rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
119767        bIgnore = (rcauth==SQLITE_IGNORE);
119768      }
119769#endif
119770    }
119771
119772    /* Take a shared-cache advisory read-lock on the parent table. Allocate
119773    ** a cursor to use to search the unique index on the parent key columns
119774    ** in the parent table.  */
119775    sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
119776    pParse->nTab++;
119777
119778    if( regOld!=0 ){
119779      /* A row is being removed from the child table. Search for the parent.
119780      ** If the parent does not exist, removing the child row resolves an
119781      ** outstanding foreign key constraint violation. */
119782      fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
119783    }
119784    if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){
119785      /* A row is being added to the child table. If a parent row cannot
119786      ** be found, adding the child row has violated the FK constraint.
119787      **
119788      ** If this operation is being performed as part of a trigger program
119789      ** that is actually a "SET NULL" action belonging to this very
119790      ** foreign key, then omit this scan altogether. As all child key
119791      ** values are guaranteed to be NULL, it is not possible for adding
119792      ** this row to cause an FK violation.  */
119793      fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1, bIgnore);
119794    }
119795
119796    sqlite3DbFree(db, aiFree);
119797  }
119798
119799  /* Loop through all the foreign key constraints that refer to this table.
119800  ** (the "child" constraints) */
119801  for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
119802    Index *pIdx = 0;              /* Foreign key index for pFKey */
119803    SrcList *pSrc;
119804    int *aiCol = 0;
119805
119806    if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){
119807      continue;
119808    }
119809
119810    if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
119811     && !pParse->pToplevel && !pParse->isMultiWrite
119812    ){
119813      assert( regOld==0 && regNew!=0 );
119814      /* Inserting a single row into a parent table cannot cause (or fix)
119815      ** an immediate foreign key violation. So do nothing in this case.  */
119816      continue;
119817    }
119818
119819    if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
119820      if( !isIgnoreErrors || db->mallocFailed ) return;
119821      continue;
119822    }
119823    assert( aiCol || pFKey->nCol==1 );
119824
119825    /* Create a SrcList structure containing the child table.  We need the
119826    ** child table as a SrcList for sqlite3WhereBegin() */
119827    pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
119828    if( pSrc ){
119829      struct SrcList_item *pItem = pSrc->a;
119830      pItem->pTab = pFKey->pFrom;
119831      pItem->zName = pFKey->pFrom->zName;
119832      pItem->pTab->nTabRef++;
119833      pItem->iCursor = pParse->nTab++;
119834
119835      if( regNew!=0 ){
119836        fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
119837      }
119838      if( regOld!=0 ){
119839        int eAction = pFKey->aAction[aChange!=0];
119840        fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
119841        /* If this is a deferred FK constraint, or a CASCADE or SET NULL
119842        ** action applies, then any foreign key violations caused by
119843        ** removing the parent key will be rectified by the action trigger.
119844        ** So do not set the "may-abort" flag in this case.
119845        **
119846        ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the
119847        ** may-abort flag will eventually be set on this statement anyway
119848        ** (when this function is called as part of processing the UPDATE
119849        ** within the action trigger).
119850        **
119851        ** Note 2: At first glance it may seem like SQLite could simply omit
119852        ** all OP_FkCounter related scans when either CASCADE or SET NULL
119853        ** applies. The trouble starts if the CASCADE or SET NULL action
119854        ** trigger causes other triggers or action rules attached to the
119855        ** child table to fire. In these cases the fk constraint counters
119856        ** might be set incorrectly if any OP_FkCounter related scans are
119857        ** omitted.  */
119858        if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
119859          sqlite3MayAbort(pParse);
119860        }
119861      }
119862      pItem->zName = 0;
119863      sqlite3SrcListDelete(db, pSrc);
119864    }
119865    sqlite3DbFree(db, aiCol);
119866  }
119867}
119868
119869#define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
119870
119871/*
119872** This function is called before generating code to update or delete a
119873** row contained in table pTab.
119874*/
119875SQLITE_PRIVATE u32 sqlite3FkOldmask(
119876  Parse *pParse,                  /* Parse context */
119877  Table *pTab                     /* Table being modified */
119878){
119879  u32 mask = 0;
119880  if( pParse->db->flags&SQLITE_ForeignKeys ){
119881    FKey *p;
119882    int i;
119883    for(p=pTab->pFKey; p; p=p->pNextFrom){
119884      for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
119885    }
119886    for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
119887      Index *pIdx = 0;
119888      sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
119889      if( pIdx ){
119890        for(i=0; i<pIdx->nKeyCol; i++){
119891          assert( pIdx->aiColumn[i]>=0 );
119892          mask |= COLUMN_MASK(pIdx->aiColumn[i]);
119893        }
119894      }
119895    }
119896  }
119897  return mask;
119898}
119899
119900
119901/*
119902** This function is called before generating code to update or delete a
119903** row contained in table pTab. If the operation is a DELETE, then
119904** parameter aChange is passed a NULL value. For an UPDATE, aChange points
119905** to an array of size N, where N is the number of columns in table pTab.
119906** If the i'th column is not modified by the UPDATE, then the corresponding
119907** entry in the aChange[] array is set to -1. If the column is modified,
119908** the value is 0 or greater. Parameter chngRowid is set to true if the
119909** UPDATE statement modifies the rowid fields of the table.
119910**
119911** If any foreign key processing will be required, this function returns
119912** non-zero. If there is no foreign key related processing, this function
119913** returns zero.
119914**
119915** For an UPDATE, this function returns 2 if:
119916**
119917**   * There are any FKs for which pTab is the child and the parent table, or
119918**   * the UPDATE modifies one or more parent keys for which the action is
119919**     not "NO ACTION" (i.e. is CASCADE, SET DEFAULT or SET NULL).
119920**
119921** Or, assuming some other foreign key processing is required, 1.
119922*/
119923SQLITE_PRIVATE int sqlite3FkRequired(
119924  Parse *pParse,                  /* Parse context */
119925  Table *pTab,                    /* Table being modified */
119926  int *aChange,                   /* Non-NULL for UPDATE operations */
119927  int chngRowid                   /* True for UPDATE that affects rowid */
119928){
119929  int eRet = 0;
119930  if( pParse->db->flags&SQLITE_ForeignKeys ){
119931    if( !aChange ){
119932      /* A DELETE operation. Foreign key processing is required if the
119933      ** table in question is either the child or parent table for any
119934      ** foreign key constraint.  */
119935      eRet = (sqlite3FkReferences(pTab) || pTab->pFKey);
119936    }else{
119937      /* This is an UPDATE. Foreign key processing is only required if the
119938      ** operation modifies one or more child or parent key columns. */
119939      FKey *p;
119940
119941      /* Check if any child key columns are being modified. */
119942      for(p=pTab->pFKey; p; p=p->pNextFrom){
119943        if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) return 2;
119944        if( fkChildIsModified(pTab, p, aChange, chngRowid) ){
119945          eRet = 1;
119946        }
119947      }
119948
119949      /* Check if any parent key columns are being modified. */
119950      for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
119951        if( fkParentIsModified(pTab, p, aChange, chngRowid) ){
119952          if( p->aAction[1]!=OE_None ) return 2;
119953          eRet = 1;
119954        }
119955      }
119956    }
119957  }
119958  return eRet;
119959}
119960
119961/*
119962** This function is called when an UPDATE or DELETE operation is being
119963** compiled on table pTab, which is the parent table of foreign-key pFKey.
119964** If the current operation is an UPDATE, then the pChanges parameter is
119965** passed a pointer to the list of columns being modified. If it is a
119966** DELETE, pChanges is passed a NULL pointer.
119967**
119968** It returns a pointer to a Trigger structure containing a trigger
119969** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
119970** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
119971** returned (these actions require no special handling by the triggers
119972** sub-system, code for them is created by fkScanChildren()).
119973**
119974** For example, if pFKey is the foreign key and pTab is table "p" in
119975** the following schema:
119976**
119977**   CREATE TABLE p(pk PRIMARY KEY);
119978**   CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
119979**
119980** then the returned trigger structure is equivalent to:
119981**
119982**   CREATE TRIGGER ... DELETE ON p BEGIN
119983**     DELETE FROM c WHERE ck = old.pk;
119984**   END;
119985**
119986** The returned pointer is cached as part of the foreign key object. It
119987** is eventually freed along with the rest of the foreign key object by
119988** sqlite3FkDelete().
119989*/
119990static Trigger *fkActionTrigger(
119991  Parse *pParse,                  /* Parse context */
119992  Table *pTab,                    /* Table being updated or deleted from */
119993  FKey *pFKey,                    /* Foreign key to get action for */
119994  ExprList *pChanges              /* Change-list for UPDATE, NULL for DELETE */
119995){
119996  sqlite3 *db = pParse->db;       /* Database handle */
119997  int action;                     /* One of OE_None, OE_Cascade etc. */
119998  Trigger *pTrigger;              /* Trigger definition to return */
119999  int iAction = (pChanges!=0);    /* 1 for UPDATE, 0 for DELETE */
120000
120001  action = pFKey->aAction[iAction];
120002  if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
120003    return 0;
120004  }
120005  pTrigger = pFKey->apTrigger[iAction];
120006
120007  if( action!=OE_None && !pTrigger ){
120008    char const *zFrom;            /* Name of child table */
120009    int nFrom;                    /* Length in bytes of zFrom */
120010    Index *pIdx = 0;              /* Parent key index for this FK */
120011    int *aiCol = 0;               /* child table cols -> parent key cols */
120012    TriggerStep *pStep = 0;        /* First (only) step of trigger program */
120013    Expr *pWhere = 0;             /* WHERE clause of trigger step */
120014    ExprList *pList = 0;          /* Changes list if ON UPDATE CASCADE */
120015    Select *pSelect = 0;          /* If RESTRICT, "SELECT RAISE(...)" */
120016    int i;                        /* Iterator variable */
120017    Expr *pWhen = 0;              /* WHEN clause for the trigger */
120018
120019    if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
120020    assert( aiCol || pFKey->nCol==1 );
120021
120022    for(i=0; i<pFKey->nCol; i++){
120023      Token tOld = { "old", 3 };  /* Literal "old" token */
120024      Token tNew = { "new", 3 };  /* Literal "new" token */
120025      Token tFromCol;             /* Name of column in child table */
120026      Token tToCol;               /* Name of column in parent table */
120027      int iFromCol;               /* Idx of column in child table */
120028      Expr *pEq;                  /* tFromCol = OLD.tToCol */
120029
120030      iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
120031      assert( iFromCol>=0 );
120032      assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
120033      assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
120034      sqlite3TokenInit(&tToCol,
120035                   pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName);
120036      sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zName);
120037
120038      /* Create the expression "OLD.zToCol = zFromCol". It is important
120039      ** that the "OLD.zToCol" term is on the LHS of the = operator, so
120040      ** that the affinity and collation sequence associated with the
120041      ** parent table are used for the comparison. */
120042      pEq = sqlite3PExpr(pParse, TK_EQ,
120043          sqlite3PExpr(pParse, TK_DOT,
120044            sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
120045            sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
120046          sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0)
120047      );
120048      pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
120049
120050      /* For ON UPDATE, construct the next term of the WHEN clause.
120051      ** The final WHEN clause will be like this:
120052      **
120053      **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
120054      */
120055      if( pChanges ){
120056        pEq = sqlite3PExpr(pParse, TK_IS,
120057            sqlite3PExpr(pParse, TK_DOT,
120058              sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
120059              sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
120060            sqlite3PExpr(pParse, TK_DOT,
120061              sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
120062              sqlite3ExprAlloc(db, TK_ID, &tToCol, 0))
120063            );
120064        pWhen = sqlite3ExprAnd(pParse, pWhen, pEq);
120065      }
120066
120067      if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
120068        Expr *pNew;
120069        if( action==OE_Cascade ){
120070          pNew = sqlite3PExpr(pParse, TK_DOT,
120071            sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
120072            sqlite3ExprAlloc(db, TK_ID, &tToCol, 0));
120073        }else if( action==OE_SetDflt ){
120074          Column *pCol = pFKey->pFrom->aCol + iFromCol;
120075          Expr *pDflt;
120076          if( pCol->colFlags & COLFLAG_GENERATED ){
120077            testcase( pCol->colFlags & COLFLAG_VIRTUAL );
120078            testcase( pCol->colFlags & COLFLAG_STORED );
120079            pDflt = 0;
120080          }else{
120081            pDflt = pCol->pDflt;
120082          }
120083          if( pDflt ){
120084            pNew = sqlite3ExprDup(db, pDflt, 0);
120085          }else{
120086            pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
120087          }
120088        }else{
120089          pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
120090        }
120091        pList = sqlite3ExprListAppend(pParse, pList, pNew);
120092        sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
120093      }
120094    }
120095    sqlite3DbFree(db, aiCol);
120096
120097    zFrom = pFKey->pFrom->zName;
120098    nFrom = sqlite3Strlen30(zFrom);
120099
120100    if( action==OE_Restrict ){
120101      Token tFrom;
120102      Expr *pRaise;
120103
120104      tFrom.z = zFrom;
120105      tFrom.n = nFrom;
120106      pRaise = sqlite3Expr(db, TK_RAISE, "FOREIGN KEY constraint failed");
120107      if( pRaise ){
120108        pRaise->affExpr = OE_Abort;
120109      }
120110      pSelect = sqlite3SelectNew(pParse,
120111          sqlite3ExprListAppend(pParse, 0, pRaise),
120112          sqlite3SrcListAppend(pParse, 0, &tFrom, 0),
120113          pWhere,
120114          0, 0, 0, 0, 0
120115      );
120116      pWhere = 0;
120117    }
120118
120119    /* Disable lookaside memory allocation */
120120    DisableLookaside;
120121
120122    pTrigger = (Trigger *)sqlite3DbMallocZero(db,
120123        sizeof(Trigger) +         /* struct Trigger */
120124        sizeof(TriggerStep) +     /* Single step in trigger program */
120125        nFrom + 1                 /* Space for pStep->zTarget */
120126    );
120127    if( pTrigger ){
120128      pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
120129      pStep->zTarget = (char *)&pStep[1];
120130      memcpy((char *)pStep->zTarget, zFrom, nFrom);
120131
120132      pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
120133      pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
120134      pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
120135      if( pWhen ){
120136        pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0);
120137        pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
120138      }
120139    }
120140
120141    /* Re-enable the lookaside buffer, if it was disabled earlier. */
120142    EnableLookaside;
120143
120144    sqlite3ExprDelete(db, pWhere);
120145    sqlite3ExprDelete(db, pWhen);
120146    sqlite3ExprListDelete(db, pList);
120147    sqlite3SelectDelete(db, pSelect);
120148    if( db->mallocFailed==1 ){
120149      fkTriggerDelete(db, pTrigger);
120150      return 0;
120151    }
120152    assert( pStep!=0 );
120153    assert( pTrigger!=0 );
120154
120155    switch( action ){
120156      case OE_Restrict:
120157        pStep->op = TK_SELECT;
120158        break;
120159      case OE_Cascade:
120160        if( !pChanges ){
120161          pStep->op = TK_DELETE;
120162          break;
120163        }
120164      default:
120165        pStep->op = TK_UPDATE;
120166    }
120167    pStep->pTrig = pTrigger;
120168    pTrigger->pSchema = pTab->pSchema;
120169    pTrigger->pTabSchema = pTab->pSchema;
120170    pFKey->apTrigger[iAction] = pTrigger;
120171    pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
120172  }
120173
120174  return pTrigger;
120175}
120176
120177/*
120178** This function is called when deleting or updating a row to implement
120179** any required CASCADE, SET NULL or SET DEFAULT actions.
120180*/
120181SQLITE_PRIVATE void sqlite3FkActions(
120182  Parse *pParse,                  /* Parse context */
120183  Table *pTab,                    /* Table being updated or deleted from */
120184  ExprList *pChanges,             /* Change-list for UPDATE, NULL for DELETE */
120185  int regOld,                     /* Address of array containing old row */
120186  int *aChange,                   /* Array indicating UPDATEd columns (or 0) */
120187  int bChngRowid                  /* True if rowid is UPDATEd */
120188){
120189  /* If foreign-key support is enabled, iterate through all FKs that
120190  ** refer to table pTab. If there is an action associated with the FK
120191  ** for this operation (either update or delete), invoke the associated
120192  ** trigger sub-program.  */
120193  if( pParse->db->flags&SQLITE_ForeignKeys ){
120194    FKey *pFKey;                  /* Iterator variable */
120195    for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
120196      if( aChange==0 || fkParentIsModified(pTab, pFKey, aChange, bChngRowid) ){
120197        Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges);
120198        if( pAct ){
120199          sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort, 0);
120200        }
120201      }
120202    }
120203  }
120204}
120205
120206#endif /* ifndef SQLITE_OMIT_TRIGGER */
120207
120208/*
120209** Free all memory associated with foreign key definitions attached to
120210** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
120211** hash table.
120212*/
120213SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
120214  FKey *pFKey;                    /* Iterator variable */
120215  FKey *pNext;                    /* Copy of pFKey->pNextFrom */
120216
120217  assert( db==0 || IsVirtual(pTab)
120218         || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
120219  for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
120220
120221    /* Remove the FK from the fkeyHash hash table. */
120222    if( !db || db->pnBytesFreed==0 ){
120223      if( pFKey->pPrevTo ){
120224        pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
120225      }else{
120226        void *p = (void *)pFKey->pNextTo;
120227        const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
120228        sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
120229      }
120230      if( pFKey->pNextTo ){
120231        pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
120232      }
120233    }
120234
120235    /* EV: R-30323-21917 Each foreign key constraint in SQLite is
120236    ** classified as either immediate or deferred.
120237    */
120238    assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
120239
120240    /* Delete any triggers created to implement actions for this FK. */
120241#ifndef SQLITE_OMIT_TRIGGER
120242    fkTriggerDelete(db, pFKey->apTrigger[0]);
120243    fkTriggerDelete(db, pFKey->apTrigger[1]);
120244#endif
120245
120246    pNext = pFKey->pNextFrom;
120247    sqlite3DbFree(db, pFKey);
120248  }
120249}
120250#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
120251
120252/************** End of fkey.c ************************************************/
120253/************** Begin file insert.c ******************************************/
120254/*
120255** 2001 September 15
120256**
120257** The author disclaims copyright to this source code.  In place of
120258** a legal notice, here is a blessing:
120259**
120260**    May you do good and not evil.
120261**    May you find forgiveness for yourself and forgive others.
120262**    May you share freely, never taking more than you give.
120263**
120264*************************************************************************
120265** This file contains C code routines that are called by the parser
120266** to handle INSERT statements in SQLite.
120267*/
120268/* #include "sqliteInt.h" */
120269
120270/*
120271** Generate code that will
120272**
120273**   (1) acquire a lock for table pTab then
120274**   (2) open pTab as cursor iCur.
120275**
120276** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
120277** for that table that is actually opened.
120278*/
120279SQLITE_PRIVATE void sqlite3OpenTable(
120280  Parse *pParse,  /* Generate code into this VDBE */
120281  int iCur,       /* The cursor number of the table */
120282  int iDb,        /* The database index in sqlite3.aDb[] */
120283  Table *pTab,    /* The table to be opened */
120284  int opcode      /* OP_OpenRead or OP_OpenWrite */
120285){
120286  Vdbe *v;
120287  assert( !IsVirtual(pTab) );
120288  v = sqlite3GetVdbe(pParse);
120289  assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
120290  sqlite3TableLock(pParse, iDb, pTab->tnum,
120291                   (opcode==OP_OpenWrite)?1:0, pTab->zName);
120292  if( HasRowid(pTab) ){
120293    sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
120294    VdbeComment((v, "%s", pTab->zName));
120295  }else{
120296    Index *pPk = sqlite3PrimaryKeyIndex(pTab);
120297    assert( pPk!=0 );
120298    assert( pPk->tnum==pTab->tnum );
120299    sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
120300    sqlite3VdbeSetP4KeyInfo(pParse, pPk);
120301    VdbeComment((v, "%s", pTab->zName));
120302  }
120303}
120304
120305/*
120306** Return a pointer to the column affinity string associated with index
120307** pIdx. A column affinity string has one character for each column in
120308** the table, according to the affinity of the column:
120309**
120310**  Character      Column affinity
120311**  ------------------------------
120312**  'A'            BLOB
120313**  'B'            TEXT
120314**  'C'            NUMERIC
120315**  'D'            INTEGER
120316**  'F'            REAL
120317**
120318** An extra 'D' is appended to the end of the string to cover the
120319** rowid that appears as the last column in every index.
120320**
120321** Memory for the buffer containing the column index affinity string
120322** is managed along with the rest of the Index structure. It will be
120323** released when sqlite3DeleteIndex() is called.
120324*/
120325SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
120326  if( !pIdx->zColAff ){
120327    /* The first time a column affinity string for a particular index is
120328    ** required, it is allocated and populated here. It is then stored as
120329    ** a member of the Index structure for subsequent use.
120330    **
120331    ** The column affinity string will eventually be deleted by
120332    ** sqliteDeleteIndex() when the Index structure itself is cleaned
120333    ** up.
120334    */
120335    int n;
120336    Table *pTab = pIdx->pTable;
120337    pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
120338    if( !pIdx->zColAff ){
120339      sqlite3OomFault(db);
120340      return 0;
120341    }
120342    for(n=0; n<pIdx->nColumn; n++){
120343      i16 x = pIdx->aiColumn[n];
120344      char aff;
120345      if( x>=0 ){
120346        aff = pTab->aCol[x].affinity;
120347      }else if( x==XN_ROWID ){
120348        aff = SQLITE_AFF_INTEGER;
120349      }else{
120350        assert( x==XN_EXPR );
120351        assert( pIdx->aColExpr!=0 );
120352        aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
120353      }
120354      if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB;
120355      if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC;
120356      pIdx->zColAff[n] = aff;
120357    }
120358    pIdx->zColAff[n] = 0;
120359  }
120360
120361  return pIdx->zColAff;
120362}
120363
120364/*
120365** Compute the affinity string for table pTab, if it has not already been
120366** computed.  As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
120367**
120368** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and
120369** if iReg>0 then code an OP_Affinity opcode that will set the affinities
120370** for register iReg and following.  Or if affinities exists and iReg==0,
120371** then just set the P4 operand of the previous opcode (which should  be
120372** an OP_MakeRecord) to the affinity string.
120373**
120374** A column affinity string has one character per column:
120375**
120376**  Character      Column affinity
120377**  ------------------------------
120378**  'A'            BLOB
120379**  'B'            TEXT
120380**  'C'            NUMERIC
120381**  'D'            INTEGER
120382**  'E'            REAL
120383*/
120384SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
120385  int i, j;
120386  char *zColAff = pTab->zColAff;
120387  if( zColAff==0 ){
120388    sqlite3 *db = sqlite3VdbeDb(v);
120389    zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
120390    if( !zColAff ){
120391      sqlite3OomFault(db);
120392      return;
120393    }
120394
120395    for(i=j=0; i<pTab->nCol; i++){
120396      assert( pTab->aCol[i].affinity!=0 );
120397      if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
120398        zColAff[j++] = pTab->aCol[i].affinity;
120399      }
120400    }
120401    do{
120402      zColAff[j--] = 0;
120403    }while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB );
120404    pTab->zColAff = zColAff;
120405  }
120406  assert( zColAff!=0 );
120407  i = sqlite3Strlen30NN(zColAff);
120408  if( i ){
120409    if( iReg ){
120410      sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
120411    }else{
120412      sqlite3VdbeChangeP4(v, -1, zColAff, i);
120413    }
120414  }
120415}
120416
120417/*
120418** Return non-zero if the table pTab in database iDb or any of its indices
120419** have been opened at any point in the VDBE program. This is used to see if
120420** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can
120421** run without using a temporary table for the results of the SELECT.
120422*/
120423static int readsTable(Parse *p, int iDb, Table *pTab){
120424  Vdbe *v = sqlite3GetVdbe(p);
120425  int i;
120426  int iEnd = sqlite3VdbeCurrentAddr(v);
120427#ifndef SQLITE_OMIT_VIRTUALTABLE
120428  VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
120429#endif
120430
120431  for(i=1; i<iEnd; i++){
120432    VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
120433    assert( pOp!=0 );
120434    if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
120435      Index *pIndex;
120436      int tnum = pOp->p2;
120437      if( tnum==pTab->tnum ){
120438        return 1;
120439      }
120440      for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
120441        if( tnum==pIndex->tnum ){
120442          return 1;
120443        }
120444      }
120445    }
120446#ifndef SQLITE_OMIT_VIRTUALTABLE
120447    if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
120448      assert( pOp->p4.pVtab!=0 );
120449      assert( pOp->p4type==P4_VTAB );
120450      return 1;
120451    }
120452#endif
120453  }
120454  return 0;
120455}
120456
120457/* This walker callback will compute the union of colFlags flags for all
120458** referenced columns in a CHECK constraint or generated column expression.
120459*/
120460static int exprColumnFlagUnion(Walker *pWalker, Expr *pExpr){
120461  if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){
120462    assert( pExpr->iColumn < pWalker->u.pTab->nCol );
120463    pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
120464  }
120465  return WRC_Continue;
120466}
120467
120468#ifndef SQLITE_OMIT_GENERATED_COLUMNS
120469/*
120470** All regular columns for table pTab have been puts into registers
120471** starting with iRegStore.  The registers that correspond to STORED
120472** or VIRTUAL columns have not yet been initialized.  This routine goes
120473** back and computes the values for those columns based on the previously
120474** computed normal columns.
120475*/
120476SQLITE_PRIVATE void sqlite3ComputeGeneratedColumns(
120477  Parse *pParse,    /* Parsing context */
120478  int iRegStore,    /* Register holding the first column */
120479  Table *pTab       /* The table */
120480){
120481  int i;
120482  Walker w;
120483  Column *pRedo;
120484  int eProgress;
120485  VdbeOp *pOp;
120486
120487  assert( pTab->tabFlags & TF_HasGenerated );
120488  testcase( pTab->tabFlags & TF_HasVirtual );
120489  testcase( pTab->tabFlags & TF_HasStored );
120490
120491  /* Before computing generated columns, first go through and make sure
120492  ** that appropriate affinity has been applied to the regular columns
120493  */
120494  sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
120495  if( (pTab->tabFlags & TF_HasStored)!=0
120496   && (pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1))->opcode==OP_Affinity
120497  ){
120498    /* Change the OP_Affinity argument to '@' (NONE) for all stored
120499    ** columns.  '@' is the no-op affinity and those columns have not
120500    ** yet been computed. */
120501    int ii, jj;
120502    char *zP4 = pOp->p4.z;
120503    assert( zP4!=0 );
120504    assert( pOp->p4type==P4_DYNAMIC );
120505    for(ii=jj=0; zP4[jj]; ii++){
120506      if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){
120507        continue;
120508      }
120509      if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){
120510        zP4[jj] = SQLITE_AFF_NONE;
120511      }
120512      jj++;
120513    }
120514  }
120515
120516  /* Because there can be multiple generated columns that refer to one another,
120517  ** this is a two-pass algorithm.  On the first pass, mark all generated
120518  ** columns as "not available".
120519  */
120520  for(i=0; i<pTab->nCol; i++){
120521    if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
120522      testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
120523      testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
120524      pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
120525    }
120526  }
120527
120528  w.u.pTab = pTab;
120529  w.xExprCallback = exprColumnFlagUnion;
120530  w.xSelectCallback = 0;
120531  w.xSelectCallback2 = 0;
120532
120533  /* On the second pass, compute the value of each NOT-AVAILABLE column.
120534  ** Companion code in the TK_COLUMN case of sqlite3ExprCodeTarget() will
120535  ** compute dependencies and mark remove the COLSPAN_NOTAVAIL mark, as
120536  ** they are needed.
120537  */
120538  pParse->iSelfTab = -iRegStore;
120539  do{
120540    eProgress = 0;
120541    pRedo = 0;
120542    for(i=0; i<pTab->nCol; i++){
120543      Column *pCol = pTab->aCol + i;
120544      if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){
120545        int x;
120546        pCol->colFlags |= COLFLAG_BUSY;
120547        w.eCode = 0;
120548        sqlite3WalkExpr(&w, pCol->pDflt);
120549        pCol->colFlags &= ~COLFLAG_BUSY;
120550        if( w.eCode & COLFLAG_NOTAVAIL ){
120551          pRedo = pCol;
120552          continue;
120553        }
120554        eProgress = 1;
120555        assert( pCol->colFlags & COLFLAG_GENERATED );
120556        x = sqlite3TableColumnToStorage(pTab, i) + iRegStore;
120557        sqlite3ExprCodeGeneratedColumn(pParse, pCol, x);
120558        pCol->colFlags &= ~COLFLAG_NOTAVAIL;
120559      }
120560    }
120561  }while( pRedo && eProgress );
120562  if( pRedo ){
120563    sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zName);
120564  }
120565  pParse->iSelfTab = 0;
120566}
120567#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
120568
120569
120570#ifndef SQLITE_OMIT_AUTOINCREMENT
120571/*
120572** Locate or create an AutoincInfo structure associated with table pTab
120573** which is in database iDb.  Return the register number for the register
120574** that holds the maximum rowid.  Return zero if pTab is not an AUTOINCREMENT
120575** table.  (Also return zero when doing a VACUUM since we do not want to
120576** update the AUTOINCREMENT counters during a VACUUM.)
120577**
120578** There is at most one AutoincInfo structure per table even if the
120579** same table is autoincremented multiple times due to inserts within
120580** triggers.  A new AutoincInfo structure is created if this is the
120581** first use of table pTab.  On 2nd and subsequent uses, the original
120582** AutoincInfo structure is used.
120583**
120584** Four consecutive registers are allocated:
120585**
120586**   (1)  The name of the pTab table.
120587**   (2)  The maximum ROWID of pTab.
120588**   (3)  The rowid in sqlite_sequence of pTab
120589**   (4)  The original value of the max ROWID in pTab, or NULL if none
120590**
120591** The 2nd register is the one that is returned.  That is all the
120592** insert routine needs to know about.
120593*/
120594static int autoIncBegin(
120595  Parse *pParse,      /* Parsing context */
120596  int iDb,            /* Index of the database holding pTab */
120597  Table *pTab         /* The table we are writing to */
120598){
120599  int memId = 0;      /* Register holding maximum rowid */
120600  assert( pParse->db->aDb[iDb].pSchema!=0 );
120601  if( (pTab->tabFlags & TF_Autoincrement)!=0
120602   && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
120603  ){
120604    Parse *pToplevel = sqlite3ParseToplevel(pParse);
120605    AutoincInfo *pInfo;
120606    Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
120607
120608    /* Verify that the sqlite_sequence table exists and is an ordinary
120609    ** rowid table with exactly two columns.
120610    ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
120611    if( pSeqTab==0
120612     || !HasRowid(pSeqTab)
120613     || IsVirtual(pSeqTab)
120614     || pSeqTab->nCol!=2
120615    ){
120616      pParse->nErr++;
120617      pParse->rc = SQLITE_CORRUPT_SEQUENCE;
120618      return 0;
120619    }
120620
120621    pInfo = pToplevel->pAinc;
120622    while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
120623    if( pInfo==0 ){
120624      pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
120625      if( pInfo==0 ) return 0;
120626      pInfo->pNext = pToplevel->pAinc;
120627      pToplevel->pAinc = pInfo;
120628      pInfo->pTab = pTab;
120629      pInfo->iDb = iDb;
120630      pToplevel->nMem++;                  /* Register to hold name of table */
120631      pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
120632      pToplevel->nMem +=2;       /* Rowid in sqlite_sequence + orig max val */
120633    }
120634    memId = pInfo->regCtr;
120635  }
120636  return memId;
120637}
120638
120639/*
120640** This routine generates code that will initialize all of the
120641** register used by the autoincrement tracker.
120642*/
120643SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
120644  AutoincInfo *p;            /* Information about an AUTOINCREMENT */
120645  sqlite3 *db = pParse->db;  /* The database connection */
120646  Db *pDb;                   /* Database only autoinc table */
120647  int memId;                 /* Register holding max rowid */
120648  Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
120649
120650  /* This routine is never called during trigger-generation.  It is
120651  ** only called from the top-level */
120652  assert( pParse->pTriggerTab==0 );
120653  assert( sqlite3IsToplevel(pParse) );
120654
120655  assert( v );   /* We failed long ago if this is not so */
120656  for(p = pParse->pAinc; p; p = p->pNext){
120657    static const int iLn = VDBE_OFFSET_LINENO(2);
120658    static const VdbeOpList autoInc[] = {
120659      /* 0  */ {OP_Null,    0,  0, 0},
120660      /* 1  */ {OP_Rewind,  0, 10, 0},
120661      /* 2  */ {OP_Column,  0,  0, 0},
120662      /* 3  */ {OP_Ne,      0,  9, 0},
120663      /* 4  */ {OP_Rowid,   0,  0, 0},
120664      /* 5  */ {OP_Column,  0,  1, 0},
120665      /* 6  */ {OP_AddImm,  0,  0, 0},
120666      /* 7  */ {OP_Copy,    0,  0, 0},
120667      /* 8  */ {OP_Goto,    0, 11, 0},
120668      /* 9  */ {OP_Next,    0,  2, 0},
120669      /* 10 */ {OP_Integer, 0,  0, 0},
120670      /* 11 */ {OP_Close,   0,  0, 0}
120671    };
120672    VdbeOp *aOp;
120673    pDb = &db->aDb[p->iDb];
120674    memId = p->regCtr;
120675    assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
120676    sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
120677    sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
120678    aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn);
120679    if( aOp==0 ) break;
120680    aOp[0].p2 = memId;
120681    aOp[0].p3 = memId+2;
120682    aOp[2].p3 = memId;
120683    aOp[3].p1 = memId-1;
120684    aOp[3].p3 = memId;
120685    aOp[3].p5 = SQLITE_JUMPIFNULL;
120686    aOp[4].p2 = memId+1;
120687    aOp[5].p3 = memId;
120688    aOp[6].p1 = memId;
120689    aOp[7].p2 = memId+2;
120690    aOp[7].p1 = memId;
120691    aOp[10].p2 = memId;
120692    if( pParse->nTab==0 ) pParse->nTab = 1;
120693  }
120694}
120695
120696/*
120697** Update the maximum rowid for an autoincrement calculation.
120698**
120699** This routine should be called when the regRowid register holds a
120700** new rowid that is about to be inserted.  If that new rowid is
120701** larger than the maximum rowid in the memId memory cell, then the
120702** memory cell is updated.
120703*/
120704static void autoIncStep(Parse *pParse, int memId, int regRowid){
120705  if( memId>0 ){
120706    sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
120707  }
120708}
120709
120710/*
120711** This routine generates the code needed to write autoincrement
120712** maximum rowid values back into the sqlite_sequence register.
120713** Every statement that might do an INSERT into an autoincrement
120714** table (either directly or through triggers) needs to call this
120715** routine just before the "exit" code.
120716*/
120717static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){
120718  AutoincInfo *p;
120719  Vdbe *v = pParse->pVdbe;
120720  sqlite3 *db = pParse->db;
120721
120722  assert( v );
120723  for(p = pParse->pAinc; p; p = p->pNext){
120724    static const int iLn = VDBE_OFFSET_LINENO(2);
120725    static const VdbeOpList autoIncEnd[] = {
120726      /* 0 */ {OP_NotNull,     0, 2, 0},
120727      /* 1 */ {OP_NewRowid,    0, 0, 0},
120728      /* 2 */ {OP_MakeRecord,  0, 2, 0},
120729      /* 3 */ {OP_Insert,      0, 0, 0},
120730      /* 4 */ {OP_Close,       0, 0, 0}
120731    };
120732    VdbeOp *aOp;
120733    Db *pDb = &db->aDb[p->iDb];
120734    int iRec;
120735    int memId = p->regCtr;
120736
120737    iRec = sqlite3GetTempReg(pParse);
120738    assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
120739    sqlite3VdbeAddOp3(v, OP_Le, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId);
120740    VdbeCoverage(v);
120741    sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
120742    aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn);
120743    if( aOp==0 ) break;
120744    aOp[0].p1 = memId+1;
120745    aOp[1].p2 = memId+1;
120746    aOp[2].p1 = memId-1;
120747    aOp[2].p3 = iRec;
120748    aOp[3].p2 = iRec;
120749    aOp[3].p3 = memId+1;
120750    aOp[3].p5 = OPFLAG_APPEND;
120751    sqlite3ReleaseTempReg(pParse, iRec);
120752  }
120753}
120754SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
120755  if( pParse->pAinc ) autoIncrementEnd(pParse);
120756}
120757#else
120758/*
120759** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
120760** above are all no-ops
120761*/
120762# define autoIncBegin(A,B,C) (0)
120763# define autoIncStep(A,B,C)
120764#endif /* SQLITE_OMIT_AUTOINCREMENT */
120765
120766
120767/* Forward declaration */
120768static int xferOptimization(
120769  Parse *pParse,        /* Parser context */
120770  Table *pDest,         /* The table we are inserting into */
120771  Select *pSelect,      /* A SELECT statement to use as the data source */
120772  int onError,          /* How to handle constraint errors */
120773  int iDbDest           /* The database of pDest */
120774);
120775
120776/*
120777** This routine is called to handle SQL of the following forms:
120778**
120779**    insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
120780**    insert into TABLE (IDLIST) select
120781**    insert into TABLE (IDLIST) default values
120782**
120783** The IDLIST following the table name is always optional.  If omitted,
120784** then a list of all (non-hidden) columns for the table is substituted.
120785** The IDLIST appears in the pColumn parameter.  pColumn is NULL if IDLIST
120786** is omitted.
120787**
120788** For the pSelect parameter holds the values to be inserted for the
120789** first two forms shown above.  A VALUES clause is really just short-hand
120790** for a SELECT statement that omits the FROM clause and everything else
120791** that follows.  If the pSelect parameter is NULL, that means that the
120792** DEFAULT VALUES form of the INSERT statement is intended.
120793**
120794** The code generated follows one of four templates.  For a simple
120795** insert with data coming from a single-row VALUES clause, the code executes
120796** once straight down through.  Pseudo-code follows (we call this
120797** the "1st template"):
120798**
120799**         open write cursor to <table> and its indices
120800**         put VALUES clause expressions into registers
120801**         write the resulting record into <table>
120802**         cleanup
120803**
120804** The three remaining templates assume the statement is of the form
120805**
120806**   INSERT INTO <table> SELECT ...
120807**
120808** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
120809** in other words if the SELECT pulls all columns from a single table
120810** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
120811** if <table2> and <table1> are distinct tables but have identical
120812** schemas, including all the same indices, then a special optimization
120813** is invoked that copies raw records from <table2> over to <table1>.
120814** See the xferOptimization() function for the implementation of this
120815** template.  This is the 2nd template.
120816**
120817**         open a write cursor to <table>
120818**         open read cursor on <table2>
120819**         transfer all records in <table2> over to <table>
120820**         close cursors
120821**         foreach index on <table>
120822**           open a write cursor on the <table> index
120823**           open a read cursor on the corresponding <table2> index
120824**           transfer all records from the read to the write cursors
120825**           close cursors
120826**         end foreach
120827**
120828** The 3rd template is for when the second template does not apply
120829** and the SELECT clause does not read from <table> at any time.
120830** The generated code follows this template:
120831**
120832**         X <- A
120833**         goto B
120834**      A: setup for the SELECT
120835**         loop over the rows in the SELECT
120836**           load values into registers R..R+n
120837**           yield X
120838**         end loop
120839**         cleanup after the SELECT
120840**         end-coroutine X
120841**      B: open write cursor to <table> and its indices
120842**      C: yield X, at EOF goto D
120843**         insert the select result into <table> from R..R+n
120844**         goto C
120845**      D: cleanup
120846**
120847** The 4th template is used if the insert statement takes its
120848** values from a SELECT but the data is being inserted into a table
120849** that is also read as part of the SELECT.  In the third form,
120850** we have to use an intermediate table to store the results of
120851** the select.  The template is like this:
120852**
120853**         X <- A
120854**         goto B
120855**      A: setup for the SELECT
120856**         loop over the tables in the SELECT
120857**           load value into register R..R+n
120858**           yield X
120859**         end loop
120860**         cleanup after the SELECT
120861**         end co-routine R
120862**      B: open temp table
120863**      L: yield X, at EOF goto M
120864**         insert row from R..R+n into temp table
120865**         goto L
120866**      M: open write cursor to <table> and its indices
120867**         rewind temp table
120868**      C: loop over rows of intermediate table
120869**           transfer values form intermediate table into <table>
120870**         end loop
120871**      D: cleanup
120872*/
120873SQLITE_PRIVATE void sqlite3Insert(
120874  Parse *pParse,        /* Parser context */
120875  SrcList *pTabList,    /* Name of table into which we are inserting */
120876  Select *pSelect,      /* A SELECT statement to use as the data source */
120877  IdList *pColumn,      /* Column names corresponding to IDLIST, or NULL. */
120878  int onError,          /* How to handle constraint errors */
120879  Upsert *pUpsert       /* ON CONFLICT clauses for upsert, or NULL */
120880){
120881  sqlite3 *db;          /* The main database structure */
120882  Table *pTab;          /* The table to insert into.  aka TABLE */
120883  int i, j;             /* Loop counters */
120884  Vdbe *v;              /* Generate code into this virtual machine */
120885  Index *pIdx;          /* For looping over indices of the table */
120886  int nColumn;          /* Number of columns in the data */
120887  int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
120888  int iDataCur = 0;     /* VDBE cursor that is the main data repository */
120889  int iIdxCur = 0;      /* First index cursor */
120890  int ipkColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
120891  int endOfLoop;        /* Label for the end of the insertion loop */
120892  int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
120893  int addrInsTop = 0;   /* Jump to label "D" */
120894  int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
120895  SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
120896  int iDb;              /* Index of database holding TABLE */
120897  u8 useTempTable = 0;  /* Store SELECT results in intermediate table */
120898  u8 appendFlag = 0;    /* True if the insert is likely to be an append */
120899  u8 withoutRowid;      /* 0 for normal table.  1 for WITHOUT ROWID table */
120900  u8 bIdListInOrder;    /* True if IDLIST is in table order */
120901  ExprList *pList = 0;  /* List of VALUES() to be inserted  */
120902  int iRegStore;        /* Register in which to store next column */
120903
120904  /* Register allocations */
120905  int regFromSelect = 0;/* Base register for data coming from SELECT */
120906  int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
120907  int regRowCount = 0;  /* Memory cell used for the row counter */
120908  int regIns;           /* Block of regs holding rowid+data being inserted */
120909  int regRowid;         /* registers holding insert rowid */
120910  int regData;          /* register holding first column to insert */
120911  int *aRegIdx = 0;     /* One register allocated to each index */
120912
120913#ifndef SQLITE_OMIT_TRIGGER
120914  int isView;                 /* True if attempting to insert into a view */
120915  Trigger *pTrigger;          /* List of triggers on pTab, if required */
120916  int tmask;                  /* Mask of trigger times */
120917#endif
120918
120919  db = pParse->db;
120920  if( pParse->nErr || db->mallocFailed ){
120921    goto insert_cleanup;
120922  }
120923  dest.iSDParm = 0;  /* Suppress a harmless compiler warning */
120924
120925  /* If the Select object is really just a simple VALUES() list with a
120926  ** single row (the common case) then keep that one row of values
120927  ** and discard the other (unused) parts of the pSelect object
120928  */
120929  if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
120930    pList = pSelect->pEList;
120931    pSelect->pEList = 0;
120932    sqlite3SelectDelete(db, pSelect);
120933    pSelect = 0;
120934  }
120935
120936  /* Locate the table into which we will be inserting new information.
120937  */
120938  assert( pTabList->nSrc==1 );
120939  pTab = sqlite3SrcListLookup(pParse, pTabList);
120940  if( pTab==0 ){
120941    goto insert_cleanup;
120942  }
120943  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
120944  assert( iDb<db->nDb );
120945  if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
120946                       db->aDb[iDb].zDbSName) ){
120947    goto insert_cleanup;
120948  }
120949  withoutRowid = !HasRowid(pTab);
120950
120951  /* Figure out if we have any triggers and if the table being
120952  ** inserted into is a view
120953  */
120954#ifndef SQLITE_OMIT_TRIGGER
120955  pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
120956  isView = pTab->pSelect!=0;
120957#else
120958# define pTrigger 0
120959# define tmask 0
120960# define isView 0
120961#endif
120962#ifdef SQLITE_OMIT_VIEW
120963# undef isView
120964# define isView 0
120965#endif
120966  assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
120967
120968  /* If pTab is really a view, make sure it has been initialized.
120969  ** ViewGetColumnNames() is a no-op if pTab is not a view.
120970  */
120971  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
120972    goto insert_cleanup;
120973  }
120974
120975  /* Cannot insert into a read-only table.
120976  */
120977  if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
120978    goto insert_cleanup;
120979  }
120980
120981  /* Allocate a VDBE
120982  */
120983  v = sqlite3GetVdbe(pParse);
120984  if( v==0 ) goto insert_cleanup;
120985  if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
120986  sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
120987
120988#ifndef SQLITE_OMIT_XFER_OPT
120989  /* If the statement is of the form
120990  **
120991  **       INSERT INTO <table1> SELECT * FROM <table2>;
120992  **
120993  ** Then special optimizations can be applied that make the transfer
120994  ** very fast and which reduce fragmentation of indices.
120995  **
120996  ** This is the 2nd template.
120997  */
120998  if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
120999    assert( !pTrigger );
121000    assert( pList==0 );
121001    goto insert_end;
121002  }
121003#endif /* SQLITE_OMIT_XFER_OPT */
121004
121005  /* If this is an AUTOINCREMENT table, look up the sequence number in the
121006  ** sqlite_sequence table and store it in memory cell regAutoinc.
121007  */
121008  regAutoinc = autoIncBegin(pParse, iDb, pTab);
121009
121010  /* Allocate a block registers to hold the rowid and the values
121011  ** for all columns of the new row.
121012  */
121013  regRowid = regIns = pParse->nMem+1;
121014  pParse->nMem += pTab->nCol + 1;
121015  if( IsVirtual(pTab) ){
121016    regRowid++;
121017    pParse->nMem++;
121018  }
121019  regData = regRowid+1;
121020
121021  /* If the INSERT statement included an IDLIST term, then make sure
121022  ** all elements of the IDLIST really are columns of the table and
121023  ** remember the column indices.
121024  **
121025  ** If the table has an INTEGER PRIMARY KEY column and that column
121026  ** is named in the IDLIST, then record in the ipkColumn variable
121027  ** the index into IDLIST of the primary key column.  ipkColumn is
121028  ** the index of the primary key as it appears in IDLIST, not as
121029  ** is appears in the original table.  (The index of the INTEGER
121030  ** PRIMARY KEY in the original table is pTab->iPKey.)  After this
121031  ** loop, if ipkColumn==(-1), that means that integer primary key
121032  ** is unspecified, and hence the table is either WITHOUT ROWID or
121033  ** it will automatically generated an integer primary key.
121034  **
121035  ** bIdListInOrder is true if the columns in IDLIST are in storage
121036  ** order.  This enables an optimization that avoids shuffling the
121037  ** columns into storage order.  False negatives are harmless,
121038  ** but false positives will cause database corruption.
121039  */
121040  bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
121041  if( pColumn ){
121042    for(i=0; i<pColumn->nId; i++){
121043      pColumn->a[i].idx = -1;
121044    }
121045    for(i=0; i<pColumn->nId; i++){
121046      for(j=0; j<pTab->nCol; j++){
121047        if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
121048          pColumn->a[i].idx = j;
121049          if( i!=j ) bIdListInOrder = 0;
121050          if( j==pTab->iPKey ){
121051            ipkColumn = i;  assert( !withoutRowid );
121052          }
121053#ifndef SQLITE_OMIT_GENERATED_COLUMNS
121054          if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){
121055            sqlite3ErrorMsg(pParse,
121056               "cannot INSERT into generated column \"%s\"",
121057               pTab->aCol[j].zName);
121058            goto insert_cleanup;
121059          }
121060#endif
121061          break;
121062        }
121063      }
121064      if( j>=pTab->nCol ){
121065        if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
121066          ipkColumn = i;
121067          bIdListInOrder = 0;
121068        }else{
121069          sqlite3ErrorMsg(pParse, "table %S has no column named %s",
121070              pTabList, 0, pColumn->a[i].zName);
121071          pParse->checkSchema = 1;
121072          goto insert_cleanup;
121073        }
121074      }
121075    }
121076  }
121077
121078  /* Figure out how many columns of data are supplied.  If the data
121079  ** is coming from a SELECT statement, then generate a co-routine that
121080  ** produces a single row of the SELECT on each invocation.  The
121081  ** co-routine is the common header to the 3rd and 4th templates.
121082  */
121083  if( pSelect ){
121084    /* Data is coming from a SELECT or from a multi-row VALUES clause.
121085    ** Generate a co-routine to run the SELECT. */
121086    int regYield;       /* Register holding co-routine entry-point */
121087    int addrTop;        /* Top of the co-routine */
121088    int rc;             /* Result code */
121089
121090    regYield = ++pParse->nMem;
121091    addrTop = sqlite3VdbeCurrentAddr(v) + 1;
121092    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
121093    sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
121094    dest.iSdst = bIdListInOrder ? regData : 0;
121095    dest.nSdst = pTab->nCol;
121096    rc = sqlite3Select(pParse, pSelect, &dest);
121097    regFromSelect = dest.iSdst;
121098    if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
121099    sqlite3VdbeEndCoroutine(v, regYield);
121100    sqlite3VdbeJumpHere(v, addrTop - 1);                       /* label B: */
121101    assert( pSelect->pEList );
121102    nColumn = pSelect->pEList->nExpr;
121103
121104    /* Set useTempTable to TRUE if the result of the SELECT statement
121105    ** should be written into a temporary table (template 4).  Set to
121106    ** FALSE if each output row of the SELECT can be written directly into
121107    ** the destination table (template 3).
121108    **
121109    ** A temp table must be used if the table being updated is also one
121110    ** of the tables being read by the SELECT statement.  Also use a
121111    ** temp table in the case of row triggers.
121112    */
121113    if( pTrigger || readsTable(pParse, iDb, pTab) ){
121114      useTempTable = 1;
121115    }
121116
121117    if( useTempTable ){
121118      /* Invoke the coroutine to extract information from the SELECT
121119      ** and add it to a transient table srcTab.  The code generated
121120      ** here is from the 4th template:
121121      **
121122      **      B: open temp table
121123      **      L: yield X, goto M at EOF
121124      **         insert row from R..R+n into temp table
121125      **         goto L
121126      **      M: ...
121127      */
121128      int regRec;          /* Register to hold packed record */
121129      int regTempRowid;    /* Register to hold temp table ROWID */
121130      int addrL;           /* Label "L" */
121131
121132      srcTab = pParse->nTab++;
121133      regRec = sqlite3GetTempReg(pParse);
121134      regTempRowid = sqlite3GetTempReg(pParse);
121135      sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
121136      addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v);
121137      sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
121138      sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
121139      sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
121140      sqlite3VdbeGoto(v, addrL);
121141      sqlite3VdbeJumpHere(v, addrL);
121142      sqlite3ReleaseTempReg(pParse, regRec);
121143      sqlite3ReleaseTempReg(pParse, regTempRowid);
121144    }
121145  }else{
121146    /* This is the case if the data for the INSERT is coming from a
121147    ** single-row VALUES clause
121148    */
121149    NameContext sNC;
121150    memset(&sNC, 0, sizeof(sNC));
121151    sNC.pParse = pParse;
121152    srcTab = -1;
121153    assert( useTempTable==0 );
121154    if( pList ){
121155      nColumn = pList->nExpr;
121156      if( sqlite3ResolveExprListNames(&sNC, pList) ){
121157        goto insert_cleanup;
121158      }
121159    }else{
121160      nColumn = 0;
121161    }
121162  }
121163
121164  /* If there is no IDLIST term but the table has an integer primary
121165  ** key, the set the ipkColumn variable to the integer primary key
121166  ** column index in the original table definition.
121167  */
121168  if( pColumn==0 && nColumn>0 ){
121169    ipkColumn = pTab->iPKey;
121170#ifndef SQLITE_OMIT_GENERATED_COLUMNS
121171    if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
121172      testcase( pTab->tabFlags & TF_HasVirtual );
121173      testcase( pTab->tabFlags & TF_HasStored );
121174      for(i=ipkColumn-1; i>=0; i--){
121175        if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
121176          testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
121177          testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
121178          ipkColumn--;
121179        }
121180      }
121181    }
121182#endif
121183  }
121184
121185  /* Make sure the number of columns in the source data matches the number
121186  ** of columns to be inserted into the table.
121187  */
121188  for(i=0; i<pTab->nCol; i++){
121189    if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++;
121190  }
121191  if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
121192    sqlite3ErrorMsg(pParse,
121193       "table %S has %d columns but %d values were supplied",
121194       pTabList, 0, pTab->nCol-nHidden, nColumn);
121195    goto insert_cleanup;
121196  }
121197  if( pColumn!=0 && nColumn!=pColumn->nId ){
121198    sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
121199    goto insert_cleanup;
121200  }
121201
121202  /* Initialize the count of rows to be inserted
121203  */
121204  if( (db->flags & SQLITE_CountRows)!=0
121205   && !pParse->nested
121206   && !pParse->pTriggerTab
121207  ){
121208    regRowCount = ++pParse->nMem;
121209    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
121210  }
121211
121212  /* If this is not a view, open the table and and all indices */
121213  if( !isView ){
121214    int nIdx;
121215    nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
121216                                      &iDataCur, &iIdxCur);
121217    aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+2));
121218    if( aRegIdx==0 ){
121219      goto insert_cleanup;
121220    }
121221    for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
121222      assert( pIdx );
121223      aRegIdx[i] = ++pParse->nMem;
121224      pParse->nMem += pIdx->nColumn;
121225    }
121226    aRegIdx[i] = ++pParse->nMem;  /* Register to store the table record */
121227  }
121228#ifndef SQLITE_OMIT_UPSERT
121229  if( pUpsert ){
121230    if( IsVirtual(pTab) ){
121231      sqlite3ErrorMsg(pParse, "UPSERT not implemented for virtual table \"%s\"",
121232              pTab->zName);
121233      goto insert_cleanup;
121234    }
121235    if( pTab->pSelect ){
121236      sqlite3ErrorMsg(pParse, "cannot UPSERT a view");
121237      goto insert_cleanup;
121238    }
121239    if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
121240      goto insert_cleanup;
121241    }
121242    pTabList->a[0].iCursor = iDataCur;
121243    pUpsert->pUpsertSrc = pTabList;
121244    pUpsert->regData = regData;
121245    pUpsert->iDataCur = iDataCur;
121246    pUpsert->iIdxCur = iIdxCur;
121247    if( pUpsert->pUpsertTarget ){
121248      sqlite3UpsertAnalyzeTarget(pParse, pTabList, pUpsert);
121249    }
121250  }
121251#endif
121252
121253
121254  /* This is the top of the main insertion loop */
121255  if( useTempTable ){
121256    /* This block codes the top of loop only.  The complete loop is the
121257    ** following pseudocode (template 4):
121258    **
121259    **         rewind temp table, if empty goto D
121260    **      C: loop over rows of intermediate table
121261    **           transfer values form intermediate table into <table>
121262    **         end loop
121263    **      D: ...
121264    */
121265    addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v);
121266    addrCont = sqlite3VdbeCurrentAddr(v);
121267  }else if( pSelect ){
121268    /* This block codes the top of loop only.  The complete loop is the
121269    ** following pseudocode (template 3):
121270    **
121271    **      C: yield X, at EOF goto D
121272    **         insert the select result into <table> from R..R+n
121273    **         goto C
121274    **      D: ...
121275    */
121276    sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
121277    addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
121278    VdbeCoverage(v);
121279    if( ipkColumn>=0 ){
121280      /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
121281      ** SELECT, go ahead and copy the value into the rowid slot now, so that
121282      ** the value does not get overwritten by a NULL at tag-20191021-002. */
121283      sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
121284    }
121285  }
121286
121287  /* Compute data for ordinary columns of the new entry.  Values
121288  ** are written in storage order into registers starting with regData.
121289  ** Only ordinary columns are computed in this loop. The rowid
121290  ** (if there is one) is computed later and generated columns are
121291  ** computed after the rowid since they might depend on the value
121292  ** of the rowid.
121293  */
121294  nHidden = 0;
121295  iRegStore = regData;  assert( regData==regRowid+1 );
121296  for(i=0; i<pTab->nCol; i++, iRegStore++){
121297    int k;
121298    u32 colFlags;
121299    assert( i>=nHidden );
121300    if( i==pTab->iPKey ){
121301      /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
121302      ** using the rowid. So put a NULL in the IPK slot of the record to avoid
121303      ** using excess space.  The file format definition requires this extra
121304      ** NULL - we cannot optimize further by skipping the column completely */
121305      sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
121306      continue;
121307    }
121308    if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
121309      nHidden++;
121310      if( (colFlags & COLFLAG_VIRTUAL)!=0 ){
121311        /* Virtual columns do not participate in OP_MakeRecord.  So back up
121312        ** iRegStore by one slot to compensate for the iRegStore++ in the
121313        ** outer for() loop */
121314        iRegStore--;
121315        continue;
121316      }else if( (colFlags & COLFLAG_STORED)!=0 ){
121317        /* Stored columns are computed later.  But if there are BEFORE
121318        ** triggers, the slots used for stored columns will be OP_Copy-ed
121319        ** to a second block of registers, so the register needs to be
121320        ** initialized to NULL to avoid an uninitialized register read */
121321        if( tmask & TRIGGER_BEFORE ){
121322          sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
121323        }
121324        continue;
121325      }else if( pColumn==0 ){
121326        /* Hidden columns that are not explicitly named in the INSERT
121327        ** get there default value */
121328        sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
121329        continue;
121330      }
121331    }
121332    if( pColumn ){
121333      for(j=0; j<pColumn->nId && pColumn->a[j].idx!=i; j++){}
121334      if( j>=pColumn->nId ){
121335        /* A column not named in the insert column list gets its
121336        ** default value */
121337        sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
121338        continue;
121339      }
121340      k = j;
121341    }else if( nColumn==0 ){
121342      /* This is INSERT INTO ... DEFAULT VALUES.  Load the default value. */
121343      sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
121344      continue;
121345    }else{
121346      k = i - nHidden;
121347    }
121348
121349    if( useTempTable ){
121350      sqlite3VdbeAddOp3(v, OP_Column, srcTab, k, iRegStore);
121351    }else if( pSelect ){
121352      if( regFromSelect!=regData ){
121353        sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+k, iRegStore);
121354      }
121355    }else{
121356      sqlite3ExprCode(pParse, pList->a[k].pExpr, iRegStore);
121357    }
121358  }
121359
121360
121361  /* Run the BEFORE and INSTEAD OF triggers, if there are any
121362  */
121363  endOfLoop = sqlite3VdbeMakeLabel(pParse);
121364  if( tmask & TRIGGER_BEFORE ){
121365    int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
121366
121367    /* build the NEW.* reference row.  Note that if there is an INTEGER
121368    ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
121369    ** translated into a unique ID for the row.  But on a BEFORE trigger,
121370    ** we do not know what the unique ID will be (because the insert has
121371    ** not happened yet) so we substitute a rowid of -1
121372    */
121373    if( ipkColumn<0 ){
121374      sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
121375    }else{
121376      int addr1;
121377      assert( !withoutRowid );
121378      if( useTempTable ){
121379        sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
121380      }else{
121381        assert( pSelect==0 );  /* Otherwise useTempTable is true */
121382        sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
121383      }
121384      addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v);
121385      sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
121386      sqlite3VdbeJumpHere(v, addr1);
121387      sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v);
121388    }
121389
121390    /* Cannot have triggers on a virtual table. If it were possible,
121391    ** this block would have to account for hidden column.
121392    */
121393    assert( !IsVirtual(pTab) );
121394
121395    /* Copy the new data already generated. */
121396    assert( pTab->nNVCol>0 );
121397    sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1);
121398
121399#ifndef SQLITE_OMIT_GENERATED_COLUMNS
121400    /* Compute the new value for generated columns after all other
121401    ** columns have already been computed.  This must be done after
121402    ** computing the ROWID in case one of the generated columns
121403    ** refers to the ROWID. */
121404    if( pTab->tabFlags & TF_HasGenerated ){
121405      testcase( pTab->tabFlags & TF_HasVirtual );
121406      testcase( pTab->tabFlags & TF_HasStored );
121407      sqlite3ComputeGeneratedColumns(pParse, regCols+1, pTab);
121408    }
121409#endif
121410
121411    /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
121412    ** do not attempt any conversions before assembling the record.
121413    ** If this is a real table, attempt conversions as required by the
121414    ** table column affinities.
121415    */
121416    if( !isView ){
121417      sqlite3TableAffinity(v, pTab, regCols+1);
121418    }
121419
121420    /* Fire BEFORE or INSTEAD OF triggers */
121421    sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
121422        pTab, regCols-pTab->nCol-1, onError, endOfLoop);
121423
121424    sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
121425  }
121426
121427  if( !isView ){
121428    if( IsVirtual(pTab) ){
121429      /* The row that the VUpdate opcode will delete: none */
121430      sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
121431    }
121432    if( ipkColumn>=0 ){
121433      /* Compute the new rowid */
121434      if( useTempTable ){
121435        sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
121436      }else if( pSelect ){
121437        /* Rowid already initialized at tag-20191021-001 */
121438      }else{
121439        Expr *pIpk = pList->a[ipkColumn].pExpr;
121440        if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
121441          sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
121442          appendFlag = 1;
121443        }else{
121444          sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
121445        }
121446      }
121447      /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
121448      ** to generate a unique primary key value.
121449      */
121450      if( !appendFlag ){
121451        int addr1;
121452        if( !IsVirtual(pTab) ){
121453          addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v);
121454          sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
121455          sqlite3VdbeJumpHere(v, addr1);
121456        }else{
121457          addr1 = sqlite3VdbeCurrentAddr(v);
121458          sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, addr1+2); VdbeCoverage(v);
121459        }
121460        sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v);
121461      }
121462    }else if( IsVirtual(pTab) || withoutRowid ){
121463      sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
121464    }else{
121465      sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
121466      appendFlag = 1;
121467    }
121468    autoIncStep(pParse, regAutoinc, regRowid);
121469
121470#ifndef SQLITE_OMIT_GENERATED_COLUMNS
121471    /* Compute the new value for generated columns after all other
121472    ** columns have already been computed.  This must be done after
121473    ** computing the ROWID in case one of the generated columns
121474    ** is derived from the INTEGER PRIMARY KEY. */
121475    if( pTab->tabFlags & TF_HasGenerated ){
121476      sqlite3ComputeGeneratedColumns(pParse, regRowid+1, pTab);
121477    }
121478#endif
121479
121480    /* Generate code to check constraints and generate index keys and
121481    ** do the insertion.
121482    */
121483#ifndef SQLITE_OMIT_VIRTUALTABLE
121484    if( IsVirtual(pTab) ){
121485      const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
121486      sqlite3VtabMakeWritable(pParse, pTab);
121487      sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
121488      sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
121489      sqlite3MayAbort(pParse);
121490    }else
121491#endif
121492    {
121493      int isReplace;    /* Set to true if constraints may cause a replace */
121494      int bUseSeek;     /* True to use OPFLAG_SEEKRESULT */
121495      sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
121496          regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
121497      );
121498      sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
121499
121500      /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
121501      ** constraints or (b) there are no triggers and this table is not a
121502      ** parent table in a foreign key constraint. It is safe to set the
121503      ** flag in the second case as if any REPLACE constraint is hit, an
121504      ** OP_Delete or OP_IdxDelete instruction will be executed on each
121505      ** cursor that is disturbed. And these instructions both clear the
121506      ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
121507      ** functionality.  */
121508      bUseSeek = (isReplace==0 || !sqlite3VdbeHasSubProgram(v));
121509      sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
121510          regIns, aRegIdx, 0, appendFlag, bUseSeek
121511      );
121512    }
121513  }
121514
121515  /* Update the count of rows that are inserted
121516  */
121517  if( regRowCount ){
121518    sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
121519  }
121520
121521  if( pTrigger ){
121522    /* Code AFTER triggers */
121523    sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
121524        pTab, regData-2-pTab->nCol, onError, endOfLoop);
121525  }
121526
121527  /* The bottom of the main insertion loop, if the data source
121528  ** is a SELECT statement.
121529  */
121530  sqlite3VdbeResolveLabel(v, endOfLoop);
121531  if( useTempTable ){
121532    sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v);
121533    sqlite3VdbeJumpHere(v, addrInsTop);
121534    sqlite3VdbeAddOp1(v, OP_Close, srcTab);
121535  }else if( pSelect ){
121536    sqlite3VdbeGoto(v, addrCont);
121537#ifdef SQLITE_DEBUG
121538    /* If we are jumping back to an OP_Yield that is preceded by an
121539    ** OP_ReleaseReg, set the p5 flag on the OP_Goto so that the
121540    ** OP_ReleaseReg will be included in the loop. */
121541    if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){
121542      assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield );
121543      sqlite3VdbeChangeP5(v, 1);
121544    }
121545#endif
121546    sqlite3VdbeJumpHere(v, addrInsTop);
121547  }
121548
121549insert_end:
121550  /* Update the sqlite_sequence table by storing the content of the
121551  ** maximum rowid counter values recorded while inserting into
121552  ** autoincrement tables.
121553  */
121554  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
121555    sqlite3AutoincrementEnd(pParse);
121556  }
121557
121558  /*
121559  ** Return the number of rows inserted. If this routine is
121560  ** generating code because of a call to sqlite3NestedParse(), do not
121561  ** invoke the callback function.
121562  */
121563  if( regRowCount ){
121564    sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
121565    sqlite3VdbeSetNumCols(v, 1);
121566    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
121567  }
121568
121569insert_cleanup:
121570  sqlite3SrcListDelete(db, pTabList);
121571  sqlite3ExprListDelete(db, pList);
121572  sqlite3UpsertDelete(db, pUpsert);
121573  sqlite3SelectDelete(db, pSelect);
121574  sqlite3IdListDelete(db, pColumn);
121575  sqlite3DbFree(db, aRegIdx);
121576}
121577
121578/* Make sure "isView" and other macros defined above are undefined. Otherwise
121579** they may interfere with compilation of other functions in this file
121580** (or in another file, if this file becomes part of the amalgamation).  */
121581#ifdef isView
121582 #undef isView
121583#endif
121584#ifdef pTrigger
121585 #undef pTrigger
121586#endif
121587#ifdef tmask
121588 #undef tmask
121589#endif
121590
121591/*
121592** Meanings of bits in of pWalker->eCode for
121593** sqlite3ExprReferencesUpdatedColumn()
121594*/
121595#define CKCNSTRNT_COLUMN   0x01    /* CHECK constraint uses a changing column */
121596#define CKCNSTRNT_ROWID    0x02    /* CHECK constraint references the ROWID */
121597
121598/* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
121599*  Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
121600** expression node references any of the
121601** columns that are being modifed by an UPDATE statement.
121602*/
121603static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
121604  if( pExpr->op==TK_COLUMN ){
121605    assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
121606    if( pExpr->iColumn>=0 ){
121607      if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
121608        pWalker->eCode |= CKCNSTRNT_COLUMN;
121609      }
121610    }else{
121611      pWalker->eCode |= CKCNSTRNT_ROWID;
121612    }
121613  }
121614  return WRC_Continue;
121615}
121616
121617/*
121618** pExpr is a CHECK constraint on a row that is being UPDATE-ed.  The
121619** only columns that are modified by the UPDATE are those for which
121620** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
121621**
121622** Return true if CHECK constraint pExpr uses any of the
121623** changing columns (or the rowid if it is changing).  In other words,
121624** return true if this CHECK constraint must be validated for
121625** the new row in the UPDATE statement.
121626**
121627** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
121628** The operation of this routine is the same - return true if an only if
121629** the expression uses one or more of columns identified by the second and
121630** third arguments.
121631*/
121632SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(
121633  Expr *pExpr,    /* The expression to be checked */
121634  int *aiChng,    /* aiChng[x]>=0 if column x changed by the UPDATE */
121635  int chngRowid   /* True if UPDATE changes the rowid */
121636){
121637  Walker w;
121638  memset(&w, 0, sizeof(w));
121639  w.eCode = 0;
121640  w.xExprCallback = checkConstraintExprNode;
121641  w.u.aiCol = aiChng;
121642  sqlite3WalkExpr(&w, pExpr);
121643  if( !chngRowid ){
121644    testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
121645    w.eCode &= ~CKCNSTRNT_ROWID;
121646  }
121647  testcase( w.eCode==0 );
121648  testcase( w.eCode==CKCNSTRNT_COLUMN );
121649  testcase( w.eCode==CKCNSTRNT_ROWID );
121650  testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
121651  return w.eCode!=0;
121652}
121653
121654/*
121655** Generate code to do constraint checks prior to an INSERT or an UPDATE
121656** on table pTab.
121657**
121658** The regNewData parameter is the first register in a range that contains
121659** the data to be inserted or the data after the update.  There will be
121660** pTab->nCol+1 registers in this range.  The first register (the one
121661** that regNewData points to) will contain the new rowid, or NULL in the
121662** case of a WITHOUT ROWID table.  The second register in the range will
121663** contain the content of the first table column.  The third register will
121664** contain the content of the second table column.  And so forth.
121665**
121666** The regOldData parameter is similar to regNewData except that it contains
121667** the data prior to an UPDATE rather than afterwards.  regOldData is zero
121668** for an INSERT.  This routine can distinguish between UPDATE and INSERT by
121669** checking regOldData for zero.
121670**
121671** For an UPDATE, the pkChng boolean is true if the true primary key (the
121672** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
121673** might be modified by the UPDATE.  If pkChng is false, then the key of
121674** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
121675**
121676** For an INSERT, the pkChng boolean indicates whether or not the rowid
121677** was explicitly specified as part of the INSERT statement.  If pkChng
121678** is zero, it means that the either rowid is computed automatically or
121679** that the table is a WITHOUT ROWID table and has no rowid.  On an INSERT,
121680** pkChng will only be true if the INSERT statement provides an integer
121681** value for either the rowid column or its INTEGER PRIMARY KEY alias.
121682**
121683** The code generated by this routine will store new index entries into
121684** registers identified by aRegIdx[].  No index entry is created for
121685** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
121686** the same as the order of indices on the linked list of indices
121687** at pTab->pIndex.
121688**
121689** (2019-05-07) The generated code also creates a new record for the
121690** main table, if pTab is a rowid table, and stores that record in the
121691** register identified by aRegIdx[nIdx] - in other words in the first
121692** entry of aRegIdx[] past the last index.  It is important that the
121693** record be generated during constraint checks to avoid affinity changes
121694** to the register content that occur after constraint checks but before
121695** the new record is inserted.
121696**
121697** The caller must have already opened writeable cursors on the main
121698** table and all applicable indices (that is to say, all indices for which
121699** aRegIdx[] is not zero).  iDataCur is the cursor for the main table when
121700** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
121701** index when operating on a WITHOUT ROWID table.  iIdxCur is the cursor
121702** for the first index in the pTab->pIndex list.  Cursors for other indices
121703** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
121704**
121705** This routine also generates code to check constraints.  NOT NULL,
121706** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
121707** then the appropriate action is performed.  There are five possible
121708** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
121709**
121710**  Constraint type  Action       What Happens
121711**  ---------------  ----------   ----------------------------------------
121712**  any              ROLLBACK     The current transaction is rolled back and
121713**                                sqlite3_step() returns immediately with a
121714**                                return code of SQLITE_CONSTRAINT.
121715**
121716**  any              ABORT        Back out changes from the current command
121717**                                only (do not do a complete rollback) then
121718**                                cause sqlite3_step() to return immediately
121719**                                with SQLITE_CONSTRAINT.
121720**
121721**  any              FAIL         Sqlite3_step() returns immediately with a
121722**                                return code of SQLITE_CONSTRAINT.  The
121723**                                transaction is not rolled back and any
121724**                                changes to prior rows are retained.
121725**
121726**  any              IGNORE       The attempt in insert or update the current
121727**                                row is skipped, without throwing an error.
121728**                                Processing continues with the next row.
121729**                                (There is an immediate jump to ignoreDest.)
121730**
121731**  NOT NULL         REPLACE      The NULL value is replace by the default
121732**                                value for that column.  If the default value
121733**                                is NULL, the action is the same as ABORT.
121734**
121735**  UNIQUE           REPLACE      The other row that conflicts with the row
121736**                                being inserted is removed.
121737**
121738**  CHECK            REPLACE      Illegal.  The results in an exception.
121739**
121740** Which action to take is determined by the overrideError parameter.
121741** Or if overrideError==OE_Default, then the pParse->onError parameter
121742** is used.  Or if pParse->onError==OE_Default then the onError value
121743** for the constraint is used.
121744*/
121745SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
121746  Parse *pParse,       /* The parser context */
121747  Table *pTab,         /* The table being inserted or updated */
121748  int *aRegIdx,        /* Use register aRegIdx[i] for index i.  0 for unused */
121749  int iDataCur,        /* Canonical data cursor (main table or PK index) */
121750  int iIdxCur,         /* First index cursor */
121751  int regNewData,      /* First register in a range holding values to insert */
121752  int regOldData,      /* Previous content.  0 for INSERTs */
121753  u8 pkChng,           /* Non-zero if the rowid or PRIMARY KEY changed */
121754  u8 overrideError,    /* Override onError to this if not OE_Default */
121755  int ignoreDest,      /* Jump to this label on an OE_Ignore resolution */
121756  int *pbMayReplace,   /* OUT: Set to true if constraint may cause a replace */
121757  int *aiChng,         /* column i is unchanged if aiChng[i]<0 */
121758  Upsert *pUpsert      /* ON CONFLICT clauses, if any.  NULL otherwise */
121759){
121760  Vdbe *v;             /* VDBE under constrution */
121761  Index *pIdx;         /* Pointer to one of the indices */
121762  Index *pPk = 0;      /* The PRIMARY KEY index */
121763  sqlite3 *db;         /* Database connection */
121764  int i;               /* loop counter */
121765  int ix;              /* Index loop counter */
121766  int nCol;            /* Number of columns */
121767  int onError;         /* Conflict resolution strategy */
121768  int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
121769  int nPkField;        /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
121770  Index *pUpIdx = 0;   /* Index to which to apply the upsert */
121771  u8 isUpdate;         /* True if this is an UPDATE operation */
121772  u8 bAffinityDone = 0;  /* True if the OP_Affinity operation has been run */
121773  int upsertBypass = 0;  /* Address of Goto to bypass upsert subroutine */
121774  int upsertJump = 0;    /* Address of Goto that jumps into upsert subroutine */
121775  int ipkTop = 0;        /* Top of the IPK uniqueness check */
121776  int ipkBottom = 0;     /* OP_Goto at the end of the IPK uniqueness check */
121777  /* Variables associated with retesting uniqueness constraints after
121778  ** replace triggers fire have run */
121779  int regTrigCnt;       /* Register used to count replace trigger invocations */
121780  int addrRecheck = 0;  /* Jump here to recheck all uniqueness constraints */
121781  int lblRecheckOk = 0; /* Each recheck jumps to this label if it passes */
121782  Trigger *pTrigger;    /* List of DELETE triggers on the table pTab */
121783  int nReplaceTrig = 0; /* Number of replace triggers coded */
121784
121785  isUpdate = regOldData!=0;
121786  db = pParse->db;
121787  v = sqlite3GetVdbe(pParse);
121788  assert( v!=0 );
121789  assert( pTab->pSelect==0 );  /* This table is not a VIEW */
121790  nCol = pTab->nCol;
121791
121792  /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
121793  ** normal rowid tables.  nPkField is the number of key fields in the
121794  ** pPk index or 1 for a rowid table.  In other words, nPkField is the
121795  ** number of fields in the true primary key of the table. */
121796  if( HasRowid(pTab) ){
121797    pPk = 0;
121798    nPkField = 1;
121799  }else{
121800    pPk = sqlite3PrimaryKeyIndex(pTab);
121801    nPkField = pPk->nKeyCol;
121802  }
121803
121804  /* Record that this module has started */
121805  VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
121806                     iDataCur, iIdxCur, regNewData, regOldData, pkChng));
121807
121808  /* Test all NOT NULL constraints.
121809  */
121810  if( pTab->tabFlags & TF_HasNotNull ){
121811    int b2ndPass = 0;         /* True if currently running 2nd pass */
121812    int nSeenReplace = 0;     /* Number of ON CONFLICT REPLACE operations */
121813    int nGenerated = 0;       /* Number of generated columns with NOT NULL */
121814    while(1){  /* Make 2 passes over columns. Exit loop via "break" */
121815      for(i=0; i<nCol; i++){
121816        int iReg;                        /* Register holding column value */
121817        Column *pCol = &pTab->aCol[i];   /* The column to check for NOT NULL */
121818        int isGenerated;                 /* non-zero if column is generated */
121819        onError = pCol->notNull;
121820        if( onError==OE_None ) continue; /* No NOT NULL on this column */
121821        if( i==pTab->iPKey ){
121822          continue;        /* ROWID is never NULL */
121823        }
121824        isGenerated = pCol->colFlags & COLFLAG_GENERATED;
121825        if( isGenerated && !b2ndPass ){
121826          nGenerated++;
121827          continue;        /* Generated columns processed on 2nd pass */
121828        }
121829        if( aiChng && aiChng[i]<0 && !isGenerated ){
121830          /* Do not check NOT NULL on columns that do not change */
121831          continue;
121832        }
121833        if( overrideError!=OE_Default ){
121834          onError = overrideError;
121835        }else if( onError==OE_Default ){
121836          onError = OE_Abort;
121837        }
121838        if( onError==OE_Replace ){
121839          if( b2ndPass        /* REPLACE becomes ABORT on the 2nd pass */
121840           || pCol->pDflt==0  /* REPLACE is ABORT if no DEFAULT value */
121841          ){
121842            testcase( pCol->colFlags & COLFLAG_VIRTUAL );
121843            testcase( pCol->colFlags & COLFLAG_STORED );
121844            testcase( pCol->colFlags & COLFLAG_GENERATED );
121845            onError = OE_Abort;
121846          }else{
121847            assert( !isGenerated );
121848          }
121849        }else if( b2ndPass && !isGenerated ){
121850          continue;
121851        }
121852        assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
121853            || onError==OE_Ignore || onError==OE_Replace );
121854        testcase( i!=sqlite3TableColumnToStorage(pTab, i) );
121855        iReg = sqlite3TableColumnToStorage(pTab, i) + regNewData + 1;
121856        switch( onError ){
121857          case OE_Replace: {
121858            int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, iReg);
121859            VdbeCoverage(v);
121860            assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
121861            nSeenReplace++;
121862            sqlite3ExprCodeCopy(pParse, pCol->pDflt, iReg);
121863            sqlite3VdbeJumpHere(v, addr1);
121864            break;
121865          }
121866          case OE_Abort:
121867            sqlite3MayAbort(pParse);
121868            /* Fall through */
121869          case OE_Rollback:
121870          case OE_Fail: {
121871            char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
121872                                        pCol->zName);
121873            sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL,
121874                              onError, iReg);
121875            sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC);
121876            sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
121877            VdbeCoverage(v);
121878            break;
121879          }
121880          default: {
121881            assert( onError==OE_Ignore );
121882            sqlite3VdbeAddOp2(v, OP_IsNull, iReg, ignoreDest);
121883            VdbeCoverage(v);
121884            break;
121885          }
121886        } /* end switch(onError) */
121887      } /* end loop i over columns */
121888      if( nGenerated==0 && nSeenReplace==0 ){
121889        /* If there are no generated columns with NOT NULL constraints
121890        ** and no NOT NULL ON CONFLICT REPLACE constraints, then a single
121891        ** pass is sufficient */
121892        break;
121893      }
121894      if( b2ndPass ) break;  /* Never need more than 2 passes */
121895      b2ndPass = 1;
121896#ifndef SQLITE_OMIT_GENERATED_COLUMNS
121897      if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
121898        /* If any NOT NULL ON CONFLICT REPLACE constraints fired on the
121899        ** first pass, recomputed values for all generated columns, as
121900        ** those values might depend on columns affected by the REPLACE.
121901        */
121902        sqlite3ComputeGeneratedColumns(pParse, regNewData+1, pTab);
121903      }
121904#endif
121905    } /* end of 2-pass loop */
121906  } /* end if( has-not-null-constraints ) */
121907
121908  /* Test all CHECK constraints
121909  */
121910#ifndef SQLITE_OMIT_CHECK
121911  if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
121912    ExprList *pCheck = pTab->pCheck;
121913    pParse->iSelfTab = -(regNewData+1);
121914    onError = overrideError!=OE_Default ? overrideError : OE_Abort;
121915    for(i=0; i<pCheck->nExpr; i++){
121916      int allOk;
121917      Expr *pCopy;
121918      Expr *pExpr = pCheck->a[i].pExpr;
121919      if( aiChng
121920       && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
121921      ){
121922        /* The check constraints do not reference any of the columns being
121923        ** updated so there is no point it verifying the check constraint */
121924        continue;
121925      }
121926      if( bAffinityDone==0 ){
121927        sqlite3TableAffinity(v, pTab, regNewData+1);
121928        bAffinityDone = 1;
121929      }
121930      allOk = sqlite3VdbeMakeLabel(pParse);
121931      sqlite3VdbeVerifyAbortable(v, onError);
121932      pCopy = sqlite3ExprDup(db, pExpr, 0);
121933      if( !db->mallocFailed ){
121934        sqlite3ExprIfTrue(pParse, pCopy, allOk, SQLITE_JUMPIFNULL);
121935      }
121936      sqlite3ExprDelete(db, pCopy);
121937      if( onError==OE_Ignore ){
121938        sqlite3VdbeGoto(v, ignoreDest);
121939      }else{
121940        char *zName = pCheck->a[i].zEName;
121941        if( zName==0 ) zName = pTab->zName;
121942        if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
121943        sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
121944                              onError, zName, P4_TRANSIENT,
121945                              P5_ConstraintCheck);
121946      }
121947      sqlite3VdbeResolveLabel(v, allOk);
121948    }
121949    pParse->iSelfTab = 0;
121950  }
121951#endif /* !defined(SQLITE_OMIT_CHECK) */
121952
121953  /* UNIQUE and PRIMARY KEY constraints should be handled in the following
121954  ** order:
121955  **
121956  **   (1)  OE_Update
121957  **   (2)  OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
121958  **   (3)  OE_Replace
121959  **
121960  ** OE_Fail and OE_Ignore must happen before any changes are made.
121961  ** OE_Update guarantees that only a single row will change, so it
121962  ** must happen before OE_Replace.  Technically, OE_Abort and OE_Rollback
121963  ** could happen in any order, but they are grouped up front for
121964  ** convenience.
121965  **
121966  ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
121967  ** The order of constraints used to have OE_Update as (2) and OE_Abort
121968  ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
121969  ** constraint before any others, so it had to be moved.
121970  **
121971  ** Constraint checking code is generated in this order:
121972  **   (A)  The rowid constraint
121973  **   (B)  Unique index constraints that do not have OE_Replace as their
121974  **        default conflict resolution strategy
121975  **   (C)  Unique index that do use OE_Replace by default.
121976  **
121977  ** The ordering of (2) and (3) is accomplished by making sure the linked
121978  ** list of indexes attached to a table puts all OE_Replace indexes last
121979  ** in the list.  See sqlite3CreateIndex() for where that happens.
121980  */
121981
121982  if( pUpsert ){
121983    if( pUpsert->pUpsertTarget==0 ){
121984      /* An ON CONFLICT DO NOTHING clause, without a constraint-target.
121985      ** Make all unique constraint resolution be OE_Ignore */
121986      assert( pUpsert->pUpsertSet==0 );
121987      overrideError = OE_Ignore;
121988      pUpsert = 0;
121989    }else if( (pUpIdx = pUpsert->pUpsertIdx)!=0 ){
121990      /* If the constraint-target uniqueness check must be run first.
121991      ** Jump to that uniqueness check now */
121992      upsertJump = sqlite3VdbeAddOp0(v, OP_Goto);
121993      VdbeComment((v, "UPSERT constraint goes first"));
121994    }
121995  }
121996
121997  /* Determine if it is possible that triggers (either explicitly coded
121998  ** triggers or FK resolution actions) might run as a result of deletes
121999  ** that happen when OE_Replace conflict resolution occurs. (Call these
122000  ** "replace triggers".)  If any replace triggers run, we will need to
122001  ** recheck all of the uniqueness constraints after they have all run.
122002  ** But on the recheck, the resolution is OE_Abort instead of OE_Replace.
122003  **
122004  ** If replace triggers are a possibility, then
122005  **
122006  **   (1) Allocate register regTrigCnt and initialize it to zero.
122007  **       That register will count the number of replace triggers that
122008  **       fire.  Constraint recheck only occurs if the number is positive.
122009  **   (2) Initialize pTrigger to the list of all DELETE triggers on pTab.
122010  **   (3) Initialize addrRecheck and lblRecheckOk
122011  **
122012  ** The uniqueness rechecking code will create a series of tests to run
122013  ** in a second pass.  The addrRecheck and lblRecheckOk variables are
122014  ** used to link together these tests which are separated from each other
122015  ** in the generate bytecode.
122016  */
122017  if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){
122018    /* There are not DELETE triggers nor FK constraints.  No constraint
122019    ** rechecks are needed. */
122020    pTrigger = 0;
122021    regTrigCnt = 0;
122022  }else{
122023    if( db->flags&SQLITE_RecTriggers ){
122024      pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
122025      regTrigCnt = pTrigger!=0 || sqlite3FkRequired(pParse, pTab, 0, 0);
122026    }else{
122027      pTrigger = 0;
122028      regTrigCnt = sqlite3FkRequired(pParse, pTab, 0, 0);
122029    }
122030    if( regTrigCnt ){
122031      /* Replace triggers might exist.  Allocate the counter and
122032      ** initialize it to zero. */
122033      regTrigCnt = ++pParse->nMem;
122034      sqlite3VdbeAddOp2(v, OP_Integer, 0, regTrigCnt);
122035      VdbeComment((v, "trigger count"));
122036      lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
122037      addrRecheck = lblRecheckOk;
122038    }
122039  }
122040
122041  /* If rowid is changing, make sure the new rowid does not previously
122042  ** exist in the table.
122043  */
122044  if( pkChng && pPk==0 ){
122045    int addrRowidOk = sqlite3VdbeMakeLabel(pParse);
122046
122047    /* Figure out what action to take in case of a rowid collision */
122048    onError = pTab->keyConf;
122049    if( overrideError!=OE_Default ){
122050      onError = overrideError;
122051    }else if( onError==OE_Default ){
122052      onError = OE_Abort;
122053    }
122054
122055    /* figure out whether or not upsert applies in this case */
122056    if( pUpsert && pUpsert->pUpsertIdx==0 ){
122057      if( pUpsert->pUpsertSet==0 ){
122058        onError = OE_Ignore;  /* DO NOTHING is the same as INSERT OR IGNORE */
122059      }else{
122060        onError = OE_Update;  /* DO UPDATE */
122061      }
122062    }
122063
122064    /* If the response to a rowid conflict is REPLACE but the response
122065    ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
122066    ** to defer the running of the rowid conflict checking until after
122067    ** the UNIQUE constraints have run.
122068    */
122069    if( onError==OE_Replace      /* IPK rule is REPLACE */
122070     && onError!=overrideError   /* Rules for other contraints are different */
122071     && pTab->pIndex             /* There exist other constraints */
122072    ){
122073      ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
122074      VdbeComment((v, "defer IPK REPLACE until last"));
122075    }
122076
122077    if( isUpdate ){
122078      /* pkChng!=0 does not mean that the rowid has changed, only that
122079      ** it might have changed.  Skip the conflict logic below if the rowid
122080      ** is unchanged. */
122081      sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
122082      sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
122083      VdbeCoverage(v);
122084    }
122085
122086    /* Check to see if the new rowid already exists in the table.  Skip
122087    ** the following conflict logic if it does not. */
122088    VdbeNoopComment((v, "uniqueness check for ROWID"));
122089    sqlite3VdbeVerifyAbortable(v, onError);
122090    sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
122091    VdbeCoverage(v);
122092
122093    switch( onError ){
122094      default: {
122095        onError = OE_Abort;
122096        /* Fall thru into the next case */
122097      }
122098      case OE_Rollback:
122099      case OE_Abort:
122100      case OE_Fail: {
122101        testcase( onError==OE_Rollback );
122102        testcase( onError==OE_Abort );
122103        testcase( onError==OE_Fail );
122104        sqlite3RowidConstraint(pParse, onError, pTab);
122105        break;
122106      }
122107      case OE_Replace: {
122108        /* If there are DELETE triggers on this table and the
122109        ** recursive-triggers flag is set, call GenerateRowDelete() to
122110        ** remove the conflicting row from the table. This will fire
122111        ** the triggers and remove both the table and index b-tree entries.
122112        **
122113        ** Otherwise, if there are no triggers or the recursive-triggers
122114        ** flag is not set, but the table has one or more indexes, call
122115        ** GenerateRowIndexDelete(). This removes the index b-tree entries
122116        ** only. The table b-tree entry will be replaced by the new entry
122117        ** when it is inserted.
122118        **
122119        ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
122120        ** also invoke MultiWrite() to indicate that this VDBE may require
122121        ** statement rollback (if the statement is aborted after the delete
122122        ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
122123        ** but being more selective here allows statements like:
122124        **
122125        **   REPLACE INTO t(rowid) VALUES($newrowid)
122126        **
122127        ** to run without a statement journal if there are no indexes on the
122128        ** table.
122129        */
122130        if( regTrigCnt ){
122131          sqlite3MultiWrite(pParse);
122132          sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
122133                                   regNewData, 1, 0, OE_Replace, 1, -1);
122134          sqlite3VdbeAddOp2(v, OP_AddImm, regTrigCnt, 1); /* incr trigger cnt */
122135          nReplaceTrig++;
122136        }else{
122137#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
122138          assert( HasRowid(pTab) );
122139          /* This OP_Delete opcode fires the pre-update-hook only. It does
122140          ** not modify the b-tree. It is more efficient to let the coming
122141          ** OP_Insert replace the existing entry than it is to delete the
122142          ** existing entry and then insert a new one. */
122143          sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
122144          sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
122145#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
122146          if( pTab->pIndex ){
122147            sqlite3MultiWrite(pParse);
122148            sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
122149          }
122150        }
122151        seenReplace = 1;
122152        break;
122153      }
122154#ifndef SQLITE_OMIT_UPSERT
122155      case OE_Update: {
122156        sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
122157        /* Fall through */
122158      }
122159#endif
122160      case OE_Ignore: {
122161        testcase( onError==OE_Ignore );
122162        sqlite3VdbeGoto(v, ignoreDest);
122163        break;
122164      }
122165    }
122166    sqlite3VdbeResolveLabel(v, addrRowidOk);
122167    if( ipkTop ){
122168      ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
122169      sqlite3VdbeJumpHere(v, ipkTop-1);
122170    }
122171  }
122172
122173  /* Test all UNIQUE constraints by creating entries for each UNIQUE
122174  ** index and making sure that duplicate entries do not already exist.
122175  ** Compute the revised record entries for indices as we go.
122176  **
122177  ** This loop also handles the case of the PRIMARY KEY index for a
122178  ** WITHOUT ROWID table.
122179  */
122180  for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
122181    int regIdx;          /* Range of registers hold conent for pIdx */
122182    int regR;            /* Range of registers holding conflicting PK */
122183    int iThisCur;        /* Cursor for this UNIQUE index */
122184    int addrUniqueOk;    /* Jump here if the UNIQUE constraint is satisfied */
122185    int addrConflictCk;  /* First opcode in the conflict check logic */
122186
122187    if( aRegIdx[ix]==0 ) continue;  /* Skip indices that do not change */
122188    if( pUpIdx==pIdx ){
122189      addrUniqueOk = upsertJump+1;
122190      upsertBypass = sqlite3VdbeGoto(v, 0);
122191      VdbeComment((v, "Skip upsert subroutine"));
122192      sqlite3VdbeJumpHere(v, upsertJump);
122193    }else{
122194      addrUniqueOk = sqlite3VdbeMakeLabel(pParse);
122195    }
122196    if( bAffinityDone==0 && (pUpIdx==0 || pUpIdx==pIdx) ){
122197      sqlite3TableAffinity(v, pTab, regNewData+1);
122198      bAffinityDone = 1;
122199    }
122200    VdbeNoopComment((v, "prep index %s", pIdx->zName));
122201    iThisCur = iIdxCur+ix;
122202
122203
122204    /* Skip partial indices for which the WHERE clause is not true */
122205    if( pIdx->pPartIdxWhere ){
122206      sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
122207      pParse->iSelfTab = -(regNewData+1);
122208      sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
122209                            SQLITE_JUMPIFNULL);
122210      pParse->iSelfTab = 0;
122211    }
122212
122213    /* Create a record for this index entry as it should appear after
122214    ** the insert or update.  Store that record in the aRegIdx[ix] register
122215    */
122216    regIdx = aRegIdx[ix]+1;
122217    for(i=0; i<pIdx->nColumn; i++){
122218      int iField = pIdx->aiColumn[i];
122219      int x;
122220      if( iField==XN_EXPR ){
122221        pParse->iSelfTab = -(regNewData+1);
122222        sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
122223        pParse->iSelfTab = 0;
122224        VdbeComment((v, "%s column %d", pIdx->zName, i));
122225      }else if( iField==XN_ROWID || iField==pTab->iPKey ){
122226        x = regNewData;
122227        sqlite3VdbeAddOp2(v, OP_IntCopy, x, regIdx+i);
122228        VdbeComment((v, "rowid"));
122229      }else{
122230        testcase( sqlite3TableColumnToStorage(pTab, iField)!=iField );
122231        x = sqlite3TableColumnToStorage(pTab, iField) + regNewData + 1;
122232        sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i);
122233        VdbeComment((v, "%s", pTab->aCol[iField].zName));
122234      }
122235    }
122236    sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
122237    VdbeComment((v, "for %s", pIdx->zName));
122238#ifdef SQLITE_ENABLE_NULL_TRIM
122239    if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
122240      sqlite3SetMakeRecordP5(v, pIdx->pTable);
122241    }
122242#endif
122243    sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
122244
122245    /* In an UPDATE operation, if this index is the PRIMARY KEY index
122246    ** of a WITHOUT ROWID table and there has been no change the
122247    ** primary key, then no collision is possible.  The collision detection
122248    ** logic below can all be skipped. */
122249    if( isUpdate && pPk==pIdx && pkChng==0 ){
122250      sqlite3VdbeResolveLabel(v, addrUniqueOk);
122251      continue;
122252    }
122253
122254    /* Find out what action to take in case there is a uniqueness conflict */
122255    onError = pIdx->onError;
122256    if( onError==OE_None ){
122257      sqlite3VdbeResolveLabel(v, addrUniqueOk);
122258      continue;  /* pIdx is not a UNIQUE index */
122259    }
122260    if( overrideError!=OE_Default ){
122261      onError = overrideError;
122262    }else if( onError==OE_Default ){
122263      onError = OE_Abort;
122264    }
122265
122266    /* Figure out if the upsert clause applies to this index */
122267    if( pUpIdx==pIdx ){
122268      if( pUpsert->pUpsertSet==0 ){
122269        onError = OE_Ignore;  /* DO NOTHING is the same as INSERT OR IGNORE */
122270      }else{
122271        onError = OE_Update;  /* DO UPDATE */
122272      }
122273    }
122274
122275    /* Collision detection may be omitted if all of the following are true:
122276    **   (1) The conflict resolution algorithm is REPLACE
122277    **   (2) The table is a WITHOUT ROWID table
122278    **   (3) There are no secondary indexes on the table
122279    **   (4) No delete triggers need to be fired if there is a conflict
122280    **   (5) No FK constraint counters need to be updated if a conflict occurs.
122281    **
122282    ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row
122283    ** must be explicitly deleted in order to ensure any pre-update hook
122284    ** is invoked.  */
122285#ifndef SQLITE_ENABLE_PREUPDATE_HOOK
122286    if( (ix==0 && pIdx->pNext==0)                   /* Condition 3 */
122287     && pPk==pIdx                                   /* Condition 2 */
122288     && onError==OE_Replace                         /* Condition 1 */
122289     && ( 0==(db->flags&SQLITE_RecTriggers) ||      /* Condition 4 */
122290          0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0))
122291     && ( 0==(db->flags&SQLITE_ForeignKeys) ||      /* Condition 5 */
122292         (0==pTab->pFKey && 0==sqlite3FkReferences(pTab)))
122293    ){
122294      sqlite3VdbeResolveLabel(v, addrUniqueOk);
122295      continue;
122296    }
122297#endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */
122298
122299    /* Check to see if the new index entry will be unique */
122300    sqlite3VdbeVerifyAbortable(v, onError);
122301    addrConflictCk =
122302      sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
122303                           regIdx, pIdx->nKeyCol); VdbeCoverage(v);
122304
122305    /* Generate code to handle collisions */
122306    regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
122307    if( isUpdate || onError==OE_Replace ){
122308      if( HasRowid(pTab) ){
122309        sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
122310        /* Conflict only if the rowid of the existing index entry
122311        ** is different from old-rowid */
122312        if( isUpdate ){
122313          sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData);
122314          sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
122315          VdbeCoverage(v);
122316        }
122317      }else{
122318        int x;
122319        /* Extract the PRIMARY KEY from the end of the index entry and
122320        ** store it in registers regR..regR+nPk-1 */
122321        if( pIdx!=pPk ){
122322          for(i=0; i<pPk->nKeyCol; i++){
122323            assert( pPk->aiColumn[i]>=0 );
122324            x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
122325            sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
122326            VdbeComment((v, "%s.%s", pTab->zName,
122327                         pTab->aCol[pPk->aiColumn[i]].zName));
122328          }
122329        }
122330        if( isUpdate ){
122331          /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
122332          ** table, only conflict if the new PRIMARY KEY values are actually
122333          ** different from the old.
122334          **
122335          ** For a UNIQUE index, only conflict if the PRIMARY KEY values
122336          ** of the matched index row are different from the original PRIMARY
122337          ** KEY values of this row before the update.  */
122338          int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
122339          int op = OP_Ne;
122340          int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
122341
122342          for(i=0; i<pPk->nKeyCol; i++){
122343            char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
122344            x = pPk->aiColumn[i];
122345            assert( x>=0 );
122346            if( i==(pPk->nKeyCol-1) ){
122347              addrJump = addrUniqueOk;
122348              op = OP_Eq;
122349            }
122350            x = sqlite3TableColumnToStorage(pTab, x);
122351            sqlite3VdbeAddOp4(v, op,
122352                regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
122353            );
122354            sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
122355            VdbeCoverageIf(v, op==OP_Eq);
122356            VdbeCoverageIf(v, op==OP_Ne);
122357          }
122358        }
122359      }
122360    }
122361
122362    /* Generate code that executes if the new index entry is not unique */
122363    assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
122364        || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update );
122365    switch( onError ){
122366      case OE_Rollback:
122367      case OE_Abort:
122368      case OE_Fail: {
122369        testcase( onError==OE_Rollback );
122370        testcase( onError==OE_Abort );
122371        testcase( onError==OE_Fail );
122372        sqlite3UniqueConstraint(pParse, onError, pIdx);
122373        break;
122374      }
122375#ifndef SQLITE_OMIT_UPSERT
122376      case OE_Update: {
122377        sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
122378        /* Fall through */
122379      }
122380#endif
122381      case OE_Ignore: {
122382        testcase( onError==OE_Ignore );
122383        sqlite3VdbeGoto(v, ignoreDest);
122384        break;
122385      }
122386      default: {
122387        int nConflictCk;   /* Number of opcodes in conflict check logic */
122388
122389        assert( onError==OE_Replace );
122390        nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
122391        assert( nConflictCk>0 );
122392        testcase( nConflictCk>1 );
122393        if( regTrigCnt ){
122394          sqlite3MultiWrite(pParse);
122395          nReplaceTrig++;
122396        }
122397        if( pTrigger && isUpdate ){
122398          sqlite3VdbeAddOp1(v, OP_CursorLock, iDataCur);
122399        }
122400        sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
122401            regR, nPkField, 0, OE_Replace,
122402            (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
122403        if( pTrigger && isUpdate ){
122404          sqlite3VdbeAddOp1(v, OP_CursorUnlock, iDataCur);
122405        }
122406        if( regTrigCnt ){
122407          int addrBypass;  /* Jump destination to bypass recheck logic */
122408
122409          sqlite3VdbeAddOp2(v, OP_AddImm, regTrigCnt, 1); /* incr trigger cnt */
122410          addrBypass = sqlite3VdbeAddOp0(v, OP_Goto);  /* Bypass recheck */
122411          VdbeComment((v, "bypass recheck"));
122412
122413          /* Here we insert code that will be invoked after all constraint
122414          ** checks have run, if and only if one or more replace triggers
122415          ** fired. */
122416          sqlite3VdbeResolveLabel(v, lblRecheckOk);
122417          lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
122418          if( pIdx->pPartIdxWhere ){
122419            /* Bypass the recheck if this partial index is not defined
122420            ** for the current row */
122421            sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk);
122422            VdbeCoverage(v);
122423          }
122424          /* Copy the constraint check code from above, except change
122425          ** the constraint-ok jump destination to be the address of
122426          ** the next retest block */
122427          while( nConflictCk>0 ){
122428            VdbeOp x;    /* Conflict check opcode to copy */
122429            /* The sqlite3VdbeAddOp4() call might reallocate the opcode array.
122430            ** Hence, make a complete copy of the opcode, rather than using
122431            ** a pointer to the opcode. */
122432            x = *sqlite3VdbeGetOp(v, addrConflictCk);
122433            if( x.opcode!=OP_IdxRowid ){
122434              int p2;      /* New P2 value for copied conflict check opcode */
122435              const char *zP4;
122436              if( sqlite3OpcodeProperty[x.opcode]&OPFLG_JUMP ){
122437                p2 = lblRecheckOk;
122438              }else{
122439                p2 = x.p2;
122440              }
122441              zP4 = x.p4type==P4_INT32 ? SQLITE_INT_TO_PTR(x.p4.i) : x.p4.z;
122442              sqlite3VdbeAddOp4(v, x.opcode, x.p1, p2, x.p3, zP4, x.p4type);
122443              sqlite3VdbeChangeP5(v, x.p5);
122444              VdbeCoverageIf(v, p2!=x.p2);
122445            }
122446            nConflictCk--;
122447            addrConflictCk++;
122448          }
122449          /* If the retest fails, issue an abort */
122450          sqlite3UniqueConstraint(pParse, OE_Abort, pIdx);
122451
122452          sqlite3VdbeJumpHere(v, addrBypass); /* Terminate the recheck bypass */
122453        }
122454        seenReplace = 1;
122455        break;
122456      }
122457    }
122458    if( pUpIdx==pIdx ){
122459      sqlite3VdbeGoto(v, upsertJump+1);
122460      sqlite3VdbeJumpHere(v, upsertBypass);
122461    }else{
122462      sqlite3VdbeResolveLabel(v, addrUniqueOk);
122463    }
122464    if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
122465  }
122466
122467  /* If the IPK constraint is a REPLACE, run it last */
122468  if( ipkTop ){
122469    sqlite3VdbeGoto(v, ipkTop);
122470    VdbeComment((v, "Do IPK REPLACE"));
122471    sqlite3VdbeJumpHere(v, ipkBottom);
122472  }
122473
122474  /* Recheck all uniqueness constraints after replace triggers have run */
122475  testcase( regTrigCnt!=0 && nReplaceTrig==0 );
122476  assert( regTrigCnt!=0 || nReplaceTrig==0 );
122477  if( nReplaceTrig ){
122478    sqlite3VdbeAddOp2(v, OP_IfNot, regTrigCnt, lblRecheckOk);VdbeCoverage(v);
122479    if( !pPk ){
122480      if( isUpdate ){
122481        sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRecheck, regOldData);
122482        sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
122483        VdbeCoverage(v);
122484      }
122485      sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRecheck, regNewData);
122486      VdbeCoverage(v);
122487      sqlite3RowidConstraint(pParse, OE_Abort, pTab);
122488    }else{
122489      sqlite3VdbeGoto(v, addrRecheck);
122490    }
122491    sqlite3VdbeResolveLabel(v, lblRecheckOk);
122492  }
122493
122494  /* Generate the table record */
122495  if( HasRowid(pTab) ){
122496    int regRec = aRegIdx[ix];
122497    sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec);
122498    sqlite3SetMakeRecordP5(v, pTab);
122499    if( !bAffinityDone ){
122500      sqlite3TableAffinity(v, pTab, 0);
122501    }
122502  }
122503
122504  *pbMayReplace = seenReplace;
122505  VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
122506}
122507
122508#ifdef SQLITE_ENABLE_NULL_TRIM
122509/*
122510** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
122511** to be the number of columns in table pTab that must not be NULL-trimmed.
122512**
122513** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
122514*/
122515SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
122516  u16 i;
122517
122518  /* Records with omitted columns are only allowed for schema format
122519  ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
122520  if( pTab->pSchema->file_format<2 ) return;
122521
122522  for(i=pTab->nCol-1; i>0; i--){
122523    if( pTab->aCol[i].pDflt!=0 ) break;
122524    if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
122525  }
122526  sqlite3VdbeChangeP5(v, i+1);
122527}
122528#endif
122529
122530/*
122531** This routine generates code to finish the INSERT or UPDATE operation
122532** that was started by a prior call to sqlite3GenerateConstraintChecks.
122533** A consecutive range of registers starting at regNewData contains the
122534** rowid and the content to be inserted.
122535**
122536** The arguments to this routine should be the same as the first six
122537** arguments to sqlite3GenerateConstraintChecks.
122538*/
122539SQLITE_PRIVATE void sqlite3CompleteInsertion(
122540  Parse *pParse,      /* The parser context */
122541  Table *pTab,        /* the table into which we are inserting */
122542  int iDataCur,       /* Cursor of the canonical data source */
122543  int iIdxCur,        /* First index cursor */
122544  int regNewData,     /* Range of content */
122545  int *aRegIdx,       /* Register used by each index.  0 for unused indices */
122546  int update_flags,   /* True for UPDATE, False for INSERT */
122547  int appendBias,     /* True if this is likely to be an append */
122548  int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
122549){
122550  Vdbe *v;            /* Prepared statements under construction */
122551  Index *pIdx;        /* An index being inserted or updated */
122552  u8 pik_flags;       /* flag values passed to the btree insert */
122553  int i;              /* Loop counter */
122554
122555  assert( update_flags==0
122556       || update_flags==OPFLAG_ISUPDATE
122557       || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
122558  );
122559
122560  v = sqlite3GetVdbe(pParse);
122561  assert( v!=0 );
122562  assert( pTab->pSelect==0 );  /* This table is not a VIEW */
122563  for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
122564    /* All REPLACE indexes are at the end of the list */
122565    assert( pIdx->onError!=OE_Replace
122566         || pIdx->pNext==0
122567         || pIdx->pNext->onError==OE_Replace );
122568    if( aRegIdx[i]==0 ) continue;
122569    if( pIdx->pPartIdxWhere ){
122570      sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
122571      VdbeCoverage(v);
122572    }
122573    pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
122574    if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
122575      assert( pParse->nested==0 );
122576      pik_flags |= OPFLAG_NCHANGE;
122577      pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
122578#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
122579      if( update_flags==0 ){
122580        int r = sqlite3GetTempReg(pParse);
122581        sqlite3VdbeAddOp2(v, OP_Integer, 0, r);
122582        sqlite3VdbeAddOp4(v, OP_Insert,
122583            iIdxCur+i, aRegIdx[i], r, (char*)pTab, P4_TABLE
122584        );
122585        sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
122586        sqlite3ReleaseTempReg(pParse, r);
122587      }
122588#endif
122589    }
122590    sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
122591                         aRegIdx[i]+1,
122592                         pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
122593    sqlite3VdbeChangeP5(v, pik_flags);
122594  }
122595  if( !HasRowid(pTab) ) return;
122596  if( pParse->nested ){
122597    pik_flags = 0;
122598  }else{
122599    pik_flags = OPFLAG_NCHANGE;
122600    pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
122601  }
122602  if( appendBias ){
122603    pik_flags |= OPFLAG_APPEND;
122604  }
122605  if( useSeekResult ){
122606    pik_flags |= OPFLAG_USESEEKRESULT;
122607  }
122608  sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, aRegIdx[i], regNewData);
122609  if( !pParse->nested ){
122610    sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
122611  }
122612  sqlite3VdbeChangeP5(v, pik_flags);
122613}
122614
122615/*
122616** Allocate cursors for the pTab table and all its indices and generate
122617** code to open and initialized those cursors.
122618**
122619** The cursor for the object that contains the complete data (normally
122620** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
122621** ROWID table) is returned in *piDataCur.  The first index cursor is
122622** returned in *piIdxCur.  The number of indices is returned.
122623**
122624** Use iBase as the first cursor (either the *piDataCur for rowid tables
122625** or the first index for WITHOUT ROWID tables) if it is non-negative.
122626** If iBase is negative, then allocate the next available cursor.
122627**
122628** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
122629** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
122630** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
122631** pTab->pIndex list.
122632**
122633** If pTab is a virtual table, then this routine is a no-op and the
122634** *piDataCur and *piIdxCur values are left uninitialized.
122635*/
122636SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
122637  Parse *pParse,   /* Parsing context */
122638  Table *pTab,     /* Table to be opened */
122639  int op,          /* OP_OpenRead or OP_OpenWrite */
122640  u8 p5,           /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
122641  int iBase,       /* Use this for the table cursor, if there is one */
122642  u8 *aToOpen,     /* If not NULL: boolean for each table and index */
122643  int *piDataCur,  /* Write the database source cursor number here */
122644  int *piIdxCur    /* Write the first index cursor number here */
122645){
122646  int i;
122647  int iDb;
122648  int iDataCur;
122649  Index *pIdx;
122650  Vdbe *v;
122651
122652  assert( op==OP_OpenRead || op==OP_OpenWrite );
122653  assert( op==OP_OpenWrite || p5==0 );
122654  if( IsVirtual(pTab) ){
122655    /* This routine is a no-op for virtual tables. Leave the output
122656    ** variables *piDataCur and *piIdxCur uninitialized so that valgrind
122657    ** can detect if they are used by mistake in the caller. */
122658    return 0;
122659  }
122660  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
122661  v = sqlite3GetVdbe(pParse);
122662  assert( v!=0 );
122663  if( iBase<0 ) iBase = pParse->nTab;
122664  iDataCur = iBase++;
122665  if( piDataCur ) *piDataCur = iDataCur;
122666  if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
122667    sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
122668  }else{
122669    sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
122670  }
122671  if( piIdxCur ) *piIdxCur = iBase;
122672  for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
122673    int iIdxCur = iBase++;
122674    assert( pIdx->pSchema==pTab->pSchema );
122675    if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
122676      if( piDataCur ) *piDataCur = iIdxCur;
122677      p5 = 0;
122678    }
122679    if( aToOpen==0 || aToOpen[i+1] ){
122680      sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
122681      sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
122682      sqlite3VdbeChangeP5(v, p5);
122683      VdbeComment((v, "%s", pIdx->zName));
122684    }
122685  }
122686  if( iBase>pParse->nTab ) pParse->nTab = iBase;
122687  return i;
122688}
122689
122690
122691#ifdef SQLITE_TEST
122692/*
122693** The following global variable is incremented whenever the
122694** transfer optimization is used.  This is used for testing
122695** purposes only - to make sure the transfer optimization really
122696** is happening when it is supposed to.
122697*/
122698SQLITE_API int sqlite3_xferopt_count;
122699#endif /* SQLITE_TEST */
122700
122701
122702#ifndef SQLITE_OMIT_XFER_OPT
122703/*
122704** Check to see if index pSrc is compatible as a source of data
122705** for index pDest in an insert transfer optimization.  The rules
122706** for a compatible index:
122707**
122708**    *   The index is over the same set of columns
122709**    *   The same DESC and ASC markings occurs on all columns
122710**    *   The same onError processing (OE_Abort, OE_Ignore, etc)
122711**    *   The same collating sequence on each column
122712**    *   The index has the exact same WHERE clause
122713*/
122714static int xferCompatibleIndex(Index *pDest, Index *pSrc){
122715  int i;
122716  assert( pDest && pSrc );
122717  assert( pDest->pTable!=pSrc->pTable );
122718  if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
122719    return 0;   /* Different number of columns */
122720  }
122721  if( pDest->onError!=pSrc->onError ){
122722    return 0;   /* Different conflict resolution strategies */
122723  }
122724  for(i=0; i<pSrc->nKeyCol; i++){
122725    if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
122726      return 0;   /* Different columns indexed */
122727    }
122728    if( pSrc->aiColumn[i]==XN_EXPR ){
122729      assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
122730      if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
122731                             pDest->aColExpr->a[i].pExpr, -1)!=0 ){
122732        return 0;   /* Different expressions in the index */
122733      }
122734    }
122735    if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
122736      return 0;   /* Different sort orders */
122737    }
122738    if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
122739      return 0;   /* Different collating sequences */
122740    }
122741  }
122742  if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
122743    return 0;     /* Different WHERE clauses */
122744  }
122745
122746  /* If no test above fails then the indices must be compatible */
122747  return 1;
122748}
122749
122750/*
122751** Attempt the transfer optimization on INSERTs of the form
122752**
122753**     INSERT INTO tab1 SELECT * FROM tab2;
122754**
122755** The xfer optimization transfers raw records from tab2 over to tab1.
122756** Columns are not decoded and reassembled, which greatly improves
122757** performance.  Raw index records are transferred in the same way.
122758**
122759** The xfer optimization is only attempted if tab1 and tab2 are compatible.
122760** There are lots of rules for determining compatibility - see comments
122761** embedded in the code for details.
122762**
122763** This routine returns TRUE if the optimization is guaranteed to be used.
122764** Sometimes the xfer optimization will only work if the destination table
122765** is empty - a factor that can only be determined at run-time.  In that
122766** case, this routine generates code for the xfer optimization but also
122767** does a test to see if the destination table is empty and jumps over the
122768** xfer optimization code if the test fails.  In that case, this routine
122769** returns FALSE so that the caller will know to go ahead and generate
122770** an unoptimized transfer.  This routine also returns FALSE if there
122771** is no chance that the xfer optimization can be applied.
122772**
122773** This optimization is particularly useful at making VACUUM run faster.
122774*/
122775static int xferOptimization(
122776  Parse *pParse,        /* Parser context */
122777  Table *pDest,         /* The table we are inserting into */
122778  Select *pSelect,      /* A SELECT statement to use as the data source */
122779  int onError,          /* How to handle constraint errors */
122780  int iDbDest           /* The database of pDest */
122781){
122782  sqlite3 *db = pParse->db;
122783  ExprList *pEList;                /* The result set of the SELECT */
122784  Table *pSrc;                     /* The table in the FROM clause of SELECT */
122785  Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
122786  struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
122787  int i;                           /* Loop counter */
122788  int iDbSrc;                      /* The database of pSrc */
122789  int iSrc, iDest;                 /* Cursors from source and destination */
122790  int addr1, addr2;                /* Loop addresses */
122791  int emptyDestTest = 0;           /* Address of test for empty pDest */
122792  int emptySrcTest = 0;            /* Address of test for empty pSrc */
122793  Vdbe *v;                         /* The VDBE we are building */
122794  int regAutoinc;                  /* Memory register used by AUTOINC */
122795  int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
122796  int regData, regRowid;           /* Registers holding data and rowid */
122797
122798  if( pSelect==0 ){
122799    return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
122800  }
122801  if( pParse->pWith || pSelect->pWith ){
122802    /* Do not attempt to process this query if there are an WITH clauses
122803    ** attached to it. Proceeding may generate a false "no such table: xxx"
122804    ** error if pSelect reads from a CTE named "xxx".  */
122805    return 0;
122806  }
122807  if( sqlite3TriggerList(pParse, pDest) ){
122808    return 0;   /* tab1 must not have triggers */
122809  }
122810#ifndef SQLITE_OMIT_VIRTUALTABLE
122811  if( IsVirtual(pDest) ){
122812    return 0;   /* tab1 must not be a virtual table */
122813  }
122814#endif
122815  if( onError==OE_Default ){
122816    if( pDest->iPKey>=0 ) onError = pDest->keyConf;
122817    if( onError==OE_Default ) onError = OE_Abort;
122818  }
122819  assert(pSelect->pSrc);   /* allocated even if there is no FROM clause */
122820  if( pSelect->pSrc->nSrc!=1 ){
122821    return 0;   /* FROM clause must have exactly one term */
122822  }
122823  if( pSelect->pSrc->a[0].pSelect ){
122824    return 0;   /* FROM clause cannot contain a subquery */
122825  }
122826  if( pSelect->pWhere ){
122827    return 0;   /* SELECT may not have a WHERE clause */
122828  }
122829  if( pSelect->pOrderBy ){
122830    return 0;   /* SELECT may not have an ORDER BY clause */
122831  }
122832  /* Do not need to test for a HAVING clause.  If HAVING is present but
122833  ** there is no ORDER BY, we will get an error. */
122834  if( pSelect->pGroupBy ){
122835    return 0;   /* SELECT may not have a GROUP BY clause */
122836  }
122837  if( pSelect->pLimit ){
122838    return 0;   /* SELECT may not have a LIMIT clause */
122839  }
122840  if( pSelect->pPrior ){
122841    return 0;   /* SELECT may not be a compound query */
122842  }
122843  if( pSelect->selFlags & SF_Distinct ){
122844    return 0;   /* SELECT may not be DISTINCT */
122845  }
122846  pEList = pSelect->pEList;
122847  assert( pEList!=0 );
122848  if( pEList->nExpr!=1 ){
122849    return 0;   /* The result set must have exactly one column */
122850  }
122851  assert( pEList->a[0].pExpr );
122852  if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
122853    return 0;   /* The result set must be the special operator "*" */
122854  }
122855
122856  /* At this point we have established that the statement is of the
122857  ** correct syntactic form to participate in this optimization.  Now
122858  ** we have to check the semantics.
122859  */
122860  pItem = pSelect->pSrc->a;
122861  pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
122862  if( pSrc==0 ){
122863    return 0;   /* FROM clause does not contain a real table */
122864  }
122865  if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
122866    testcase( pSrc!=pDest ); /* Possible due to bad sqlite_master.rootpage */
122867    return 0;   /* tab1 and tab2 may not be the same table */
122868  }
122869  if( HasRowid(pDest)!=HasRowid(pSrc) ){
122870    return 0;   /* source and destination must both be WITHOUT ROWID or not */
122871  }
122872#ifndef SQLITE_OMIT_VIRTUALTABLE
122873  if( IsVirtual(pSrc) ){
122874    return 0;   /* tab2 must not be a virtual table */
122875  }
122876#endif
122877  if( pSrc->pSelect ){
122878    return 0;   /* tab2 may not be a view */
122879  }
122880  if( pDest->nCol!=pSrc->nCol ){
122881    return 0;   /* Number of columns must be the same in tab1 and tab2 */
122882  }
122883  if( pDest->iPKey!=pSrc->iPKey ){
122884    return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
122885  }
122886  for(i=0; i<pDest->nCol; i++){
122887    Column *pDestCol = &pDest->aCol[i];
122888    Column *pSrcCol = &pSrc->aCol[i];
122889#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
122890    if( (db->mDbFlags & DBFLAG_Vacuum)==0
122891     && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
122892    ){
122893      return 0;    /* Neither table may have __hidden__ columns */
122894    }
122895#endif
122896#ifndef SQLITE_OMIT_GENERATED_COLUMNS
122897    /* Even if tables t1 and t2 have identical schemas, if they contain
122898    ** generated columns, then this statement is semantically incorrect:
122899    **
122900    **     INSERT INTO t2 SELECT * FROM t1;
122901    **
122902    ** The reason is that generated column values are returned by the
122903    ** the SELECT statement on the right but the INSERT statement on the
122904    ** left wants them to be omitted.
122905    **
122906    ** Nevertheless, this is a useful notational shorthand to tell SQLite
122907    ** to do a bulk transfer all of the content from t1 over to t2.
122908    **
122909    ** We could, in theory, disable this (except for internal use by the
122910    ** VACUUM command where it is actually needed).  But why do that?  It
122911    ** seems harmless enough, and provides a useful service.
122912    */
122913    if( (pDestCol->colFlags & COLFLAG_GENERATED) !=
122914        (pSrcCol->colFlags & COLFLAG_GENERATED) ){
122915      return 0;    /* Both columns have the same generated-column type */
122916    }
122917    /* But the transfer is only allowed if both the source and destination
122918    ** tables have the exact same expressions for generated columns.
122919    ** This requirement could be relaxed for VIRTUAL columns, I suppose.
122920    */
122921    if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){
122922      if( sqlite3ExprCompare(0, pSrcCol->pDflt, pDestCol->pDflt, -1)!=0 ){
122923        testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
122924        testcase( pDestCol->colFlags & COLFLAG_STORED );
122925        return 0;  /* Different generator expressions */
122926      }
122927    }
122928#endif
122929    if( pDestCol->affinity!=pSrcCol->affinity ){
122930      return 0;    /* Affinity must be the same on all columns */
122931    }
122932    if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){
122933      return 0;    /* Collating sequence must be the same on all columns */
122934    }
122935    if( pDestCol->notNull && !pSrcCol->notNull ){
122936      return 0;    /* tab2 must be NOT NULL if tab1 is */
122937    }
122938    /* Default values for second and subsequent columns need to match. */
122939    if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){
122940      assert( pDestCol->pDflt==0 || pDestCol->pDflt->op==TK_SPAN );
122941      assert( pSrcCol->pDflt==0 || pSrcCol->pDflt->op==TK_SPAN );
122942      if( (pDestCol->pDflt==0)!=(pSrcCol->pDflt==0)
122943       || (pDestCol->pDflt && strcmp(pDestCol->pDflt->u.zToken,
122944                                       pSrcCol->pDflt->u.zToken)!=0)
122945      ){
122946        return 0;    /* Default values must be the same for all columns */
122947      }
122948    }
122949  }
122950  for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
122951    if( IsUniqueIndex(pDestIdx) ){
122952      destHasUniqueIdx = 1;
122953    }
122954    for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
122955      if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
122956    }
122957    if( pSrcIdx==0 ){
122958      return 0;    /* pDestIdx has no corresponding index in pSrc */
122959    }
122960    if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
122961         && sqlite3FaultSim(411)==SQLITE_OK ){
122962      /* The sqlite3FaultSim() call allows this corruption test to be
122963      ** bypassed during testing, in order to exercise other corruption tests
122964      ** further downstream. */
122965      return 0;   /* Corrupt schema - two indexes on the same btree */
122966    }
122967  }
122968#ifndef SQLITE_OMIT_CHECK
122969  if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
122970    return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
122971  }
122972#endif
122973#ifndef SQLITE_OMIT_FOREIGN_KEY
122974  /* Disallow the transfer optimization if the destination table constains
122975  ** any foreign key constraints.  This is more restrictive than necessary.
122976  ** But the main beneficiary of the transfer optimization is the VACUUM
122977  ** command, and the VACUUM command disables foreign key constraints.  So
122978  ** the extra complication to make this rule less restrictive is probably
122979  ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
122980  */
122981  if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
122982    return 0;
122983  }
122984#endif
122985  if( (db->flags & SQLITE_CountRows)!=0 ){
122986    return 0;  /* xfer opt does not play well with PRAGMA count_changes */
122987  }
122988
122989  /* If we get this far, it means that the xfer optimization is at
122990  ** least a possibility, though it might only work if the destination
122991  ** table (tab1) is initially empty.
122992  */
122993#ifdef SQLITE_TEST
122994  sqlite3_xferopt_count++;
122995#endif
122996  iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
122997  v = sqlite3GetVdbe(pParse);
122998  sqlite3CodeVerifySchema(pParse, iDbSrc);
122999  iSrc = pParse->nTab++;
123000  iDest = pParse->nTab++;
123001  regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
123002  regData = sqlite3GetTempReg(pParse);
123003  regRowid = sqlite3GetTempReg(pParse);
123004  sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
123005  assert( HasRowid(pDest) || destHasUniqueIdx );
123006  if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
123007      (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
123008   || destHasUniqueIdx                              /* (2) */
123009   || (onError!=OE_Abort && onError!=OE_Rollback)   /* (3) */
123010  )){
123011    /* In some circumstances, we are able to run the xfer optimization
123012    ** only if the destination table is initially empty. Unless the
123013    ** DBFLAG_Vacuum flag is set, this block generates code to make
123014    ** that determination. If DBFLAG_Vacuum is set, then the destination
123015    ** table is always empty.
123016    **
123017    ** Conditions under which the destination must be empty:
123018    **
123019    ** (1) There is no INTEGER PRIMARY KEY but there are indices.
123020    **     (If the destination is not initially empty, the rowid fields
123021    **     of index entries might need to change.)
123022    **
123023    ** (2) The destination has a unique index.  (The xfer optimization
123024    **     is unable to test uniqueness.)
123025    **
123026    ** (3) onError is something other than OE_Abort and OE_Rollback.
123027    */
123028    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v);
123029    emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto);
123030    sqlite3VdbeJumpHere(v, addr1);
123031  }
123032  if( HasRowid(pSrc) ){
123033    u8 insFlags;
123034    sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
123035    emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
123036    if( pDest->iPKey>=0 ){
123037      addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
123038      sqlite3VdbeVerifyAbortable(v, onError);
123039      addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
123040      VdbeCoverage(v);
123041      sqlite3RowidConstraint(pParse, onError, pDest);
123042      sqlite3VdbeJumpHere(v, addr2);
123043      autoIncStep(pParse, regAutoinc, regRowid);
123044    }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
123045      addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
123046    }else{
123047      addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
123048      assert( (pDest->tabFlags & TF_Autoincrement)==0 );
123049    }
123050    if( db->mDbFlags & DBFLAG_Vacuum ){
123051      sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
123052      insFlags = OPFLAG_APPEND|OPFLAG_USESEEKRESULT;
123053    }else{
123054      insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND;
123055    }
123056    sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
123057    sqlite3VdbeAddOp4(v, OP_Insert, iDest, regData, regRowid,
123058                      (char*)pDest, P4_TABLE);
123059    sqlite3VdbeChangeP5(v, insFlags);
123060    sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
123061    sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
123062    sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
123063  }else{
123064    sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
123065    sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
123066  }
123067  for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
123068    u8 idxInsFlags = 0;
123069    for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
123070      if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
123071    }
123072    assert( pSrcIdx );
123073    sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
123074    sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
123075    VdbeComment((v, "%s", pSrcIdx->zName));
123076    sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
123077    sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
123078    sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
123079    VdbeComment((v, "%s", pDestIdx->zName));
123080    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
123081    if( db->mDbFlags & DBFLAG_Vacuum ){
123082      /* This INSERT command is part of a VACUUM operation, which guarantees
123083      ** that the destination table is empty. If all indexed columns use
123084      ** collation sequence BINARY, then it can also be assumed that the
123085      ** index will be populated by inserting keys in strictly sorted
123086      ** order. In this case, instead of seeking within the b-tree as part
123087      ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
123088      ** OP_IdxInsert to seek to the point within the b-tree where each key
123089      ** should be inserted. This is faster.
123090      **
123091      ** If any of the indexed columns use a collation sequence other than
123092      ** BINARY, this optimization is disabled. This is because the user
123093      ** might change the definition of a collation sequence and then run
123094      ** a VACUUM command. In that case keys may not be written in strictly
123095      ** sorted order.  */
123096      for(i=0; i<pSrcIdx->nColumn; i++){
123097        const char *zColl = pSrcIdx->azColl[i];
123098        if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
123099      }
123100      if( i==pSrcIdx->nColumn ){
123101        idxInsFlags = OPFLAG_USESEEKRESULT;
123102        sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
123103      }
123104    }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
123105      idxInsFlags |= OPFLAG_NCHANGE;
123106    }
123107    sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
123108    sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
123109    sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND);
123110    sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
123111    sqlite3VdbeJumpHere(v, addr1);
123112    sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
123113    sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
123114  }
123115  if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
123116  sqlite3ReleaseTempReg(pParse, regRowid);
123117  sqlite3ReleaseTempReg(pParse, regData);
123118  if( emptyDestTest ){
123119    sqlite3AutoincrementEnd(pParse);
123120    sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
123121    sqlite3VdbeJumpHere(v, emptyDestTest);
123122    sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
123123    return 0;
123124  }else{
123125    return 1;
123126  }
123127}
123128#endif /* SQLITE_OMIT_XFER_OPT */
123129
123130/************** End of insert.c **********************************************/
123131/************** Begin file legacy.c ******************************************/
123132/*
123133** 2001 September 15
123134**
123135** The author disclaims copyright to this source code.  In place of
123136** a legal notice, here is a blessing:
123137**
123138**    May you do good and not evil.
123139**    May you find forgiveness for yourself and forgive others.
123140**    May you share freely, never taking more than you give.
123141**
123142*************************************************************************
123143** Main file for the SQLite library.  The routines in this file
123144** implement the programmer interface to the library.  Routines in
123145** other files are for internal use by SQLite and should not be
123146** accessed by users of the library.
123147*/
123148
123149/* #include "sqliteInt.h" */
123150
123151/*
123152** Execute SQL code.  Return one of the SQLITE_ success/failure
123153** codes.  Also write an error message into memory obtained from
123154** malloc() and make *pzErrMsg point to that message.
123155**
123156** If the SQL is a query, then for each row in the query result
123157** the xCallback() function is called.  pArg becomes the first
123158** argument to xCallback().  If xCallback=NULL then no callback
123159** is invoked, even for queries.
123160*/
123161SQLITE_API int sqlite3_exec(
123162  sqlite3 *db,                /* The database on which the SQL executes */
123163  const char *zSql,           /* The SQL to be executed */
123164  sqlite3_callback xCallback, /* Invoke this callback routine */
123165  void *pArg,                 /* First argument to xCallback() */
123166  char **pzErrMsg             /* Write error messages here */
123167){
123168  int rc = SQLITE_OK;         /* Return code */
123169  const char *zLeftover;      /* Tail of unprocessed SQL */
123170  sqlite3_stmt *pStmt = 0;    /* The current SQL statement */
123171  char **azCols = 0;          /* Names of result columns */
123172  int callbackIsInit;         /* True if callback data is initialized */
123173
123174  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
123175  if( zSql==0 ) zSql = "";
123176
123177  sqlite3_mutex_enter(db->mutex);
123178  sqlite3Error(db, SQLITE_OK);
123179  while( rc==SQLITE_OK && zSql[0] ){
123180    int nCol = 0;
123181    char **azVals = 0;
123182
123183    pStmt = 0;
123184    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
123185    assert( rc==SQLITE_OK || pStmt==0 );
123186    if( rc!=SQLITE_OK ){
123187      continue;
123188    }
123189    if( !pStmt ){
123190      /* this happens for a comment or white-space */
123191      zSql = zLeftover;
123192      continue;
123193    }
123194    callbackIsInit = 0;
123195
123196    while( 1 ){
123197      int i;
123198      rc = sqlite3_step(pStmt);
123199
123200      /* Invoke the callback function if required */
123201      if( xCallback && (SQLITE_ROW==rc ||
123202          (SQLITE_DONE==rc && !callbackIsInit
123203                           && db->flags&SQLITE_NullCallback)) ){
123204        if( !callbackIsInit ){
123205          nCol = sqlite3_column_count(pStmt);
123206          azCols = sqlite3DbMallocRaw(db, (2*nCol+1)*sizeof(const char*));
123207          if( azCols==0 ){
123208            goto exec_out;
123209          }
123210          for(i=0; i<nCol; i++){
123211            azCols[i] = (char *)sqlite3_column_name(pStmt, i);
123212            /* sqlite3VdbeSetColName() installs column names as UTF8
123213            ** strings so there is no way for sqlite3_column_name() to fail. */
123214            assert( azCols[i]!=0 );
123215          }
123216          callbackIsInit = 1;
123217        }
123218        if( rc==SQLITE_ROW ){
123219          azVals = &azCols[nCol];
123220          for(i=0; i<nCol; i++){
123221            azVals[i] = (char *)sqlite3_column_text(pStmt, i);
123222            if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
123223              sqlite3OomFault(db);
123224              goto exec_out;
123225            }
123226          }
123227          azVals[i] = 0;
123228        }
123229        if( xCallback(pArg, nCol, azVals, azCols) ){
123230          /* EVIDENCE-OF: R-38229-40159 If the callback function to
123231          ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
123232          ** return SQLITE_ABORT. */
123233          rc = SQLITE_ABORT;
123234          sqlite3VdbeFinalize((Vdbe *)pStmt);
123235          pStmt = 0;
123236          sqlite3Error(db, SQLITE_ABORT);
123237          goto exec_out;
123238        }
123239      }
123240
123241      if( rc!=SQLITE_ROW ){
123242        rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
123243        pStmt = 0;
123244        zSql = zLeftover;
123245        while( sqlite3Isspace(zSql[0]) ) zSql++;
123246        break;
123247      }
123248    }
123249
123250    sqlite3DbFree(db, azCols);
123251    azCols = 0;
123252  }
123253
123254exec_out:
123255  if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
123256  sqlite3DbFree(db, azCols);
123257
123258  rc = sqlite3ApiExit(db, rc);
123259  if( rc!=SQLITE_OK && pzErrMsg ){
123260    *pzErrMsg = sqlite3DbStrDup(0, sqlite3_errmsg(db));
123261    if( *pzErrMsg==0 ){
123262      rc = SQLITE_NOMEM_BKPT;
123263      sqlite3Error(db, SQLITE_NOMEM);
123264    }
123265  }else if( pzErrMsg ){
123266    *pzErrMsg = 0;
123267  }
123268
123269  assert( (rc&db->errMask)==rc );
123270  sqlite3_mutex_leave(db->mutex);
123271  return rc;
123272}
123273
123274/************** End of legacy.c **********************************************/
123275/************** Begin file loadext.c *****************************************/
123276/*
123277** 2006 June 7
123278**
123279** The author disclaims copyright to this source code.  In place of
123280** a legal notice, here is a blessing:
123281**
123282**    May you do good and not evil.
123283**    May you find forgiveness for yourself and forgive others.
123284**    May you share freely, never taking more than you give.
123285**
123286*************************************************************************
123287** This file contains code used to dynamically load extensions into
123288** the SQLite library.
123289*/
123290
123291#ifndef SQLITE_CORE
123292  #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
123293#endif
123294/************** Include sqlite3ext.h in the middle of loadext.c **************/
123295/************** Begin file sqlite3ext.h **************************************/
123296/*
123297** 2006 June 7
123298**
123299** The author disclaims copyright to this source code.  In place of
123300** a legal notice, here is a blessing:
123301**
123302**    May you do good and not evil.
123303**    May you find forgiveness for yourself and forgive others.
123304**    May you share freely, never taking more than you give.
123305**
123306*************************************************************************
123307** This header file defines the SQLite interface for use by
123308** shared libraries that want to be imported as extensions into
123309** an SQLite instance.  Shared libraries that intend to be loaded
123310** as extensions by SQLite should #include this file instead of
123311** sqlite3.h.
123312*/
123313#ifndef SQLITE3EXT_H
123314#define SQLITE3EXT_H
123315/* #include "sqlite3.h" */
123316
123317/*
123318** The following structure holds pointers to all of the SQLite API
123319** routines.
123320**
123321** WARNING:  In order to maintain backwards compatibility, add new
123322** interfaces to the end of this structure only.  If you insert new
123323** interfaces in the middle of this structure, then older different
123324** versions of SQLite will not be able to load each other's shared
123325** libraries!
123326*/
123327struct sqlite3_api_routines {
123328  void * (*aggregate_context)(sqlite3_context*,int nBytes);
123329  int  (*aggregate_count)(sqlite3_context*);
123330  int  (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
123331  int  (*bind_double)(sqlite3_stmt*,int,double);
123332  int  (*bind_int)(sqlite3_stmt*,int,int);
123333  int  (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
123334  int  (*bind_null)(sqlite3_stmt*,int);
123335  int  (*bind_parameter_count)(sqlite3_stmt*);
123336  int  (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
123337  const char * (*bind_parameter_name)(sqlite3_stmt*,int);
123338  int  (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
123339  int  (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
123340  int  (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
123341  int  (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
123342  int  (*busy_timeout)(sqlite3*,int ms);
123343  int  (*changes)(sqlite3*);
123344  int  (*close)(sqlite3*);
123345  int  (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
123346                           int eTextRep,const char*));
123347  int  (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
123348                             int eTextRep,const void*));
123349  const void * (*column_blob)(sqlite3_stmt*,int iCol);
123350  int  (*column_bytes)(sqlite3_stmt*,int iCol);
123351  int  (*column_bytes16)(sqlite3_stmt*,int iCol);
123352  int  (*column_count)(sqlite3_stmt*pStmt);
123353  const char * (*column_database_name)(sqlite3_stmt*,int);
123354  const void * (*column_database_name16)(sqlite3_stmt*,int);
123355  const char * (*column_decltype)(sqlite3_stmt*,int i);
123356  const void * (*column_decltype16)(sqlite3_stmt*,int);
123357  double  (*column_double)(sqlite3_stmt*,int iCol);
123358  int  (*column_int)(sqlite3_stmt*,int iCol);
123359  sqlite_int64  (*column_int64)(sqlite3_stmt*,int iCol);
123360  const char * (*column_name)(sqlite3_stmt*,int);
123361  const void * (*column_name16)(sqlite3_stmt*,int);
123362  const char * (*column_origin_name)(sqlite3_stmt*,int);
123363  const void * (*column_origin_name16)(sqlite3_stmt*,int);
123364  const char * (*column_table_name)(sqlite3_stmt*,int);
123365  const void * (*column_table_name16)(sqlite3_stmt*,int);
123366  const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
123367  const void * (*column_text16)(sqlite3_stmt*,int iCol);
123368  int  (*column_type)(sqlite3_stmt*,int iCol);
123369  sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
123370  void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
123371  int  (*complete)(const char*sql);
123372  int  (*complete16)(const void*sql);
123373  int  (*create_collation)(sqlite3*,const char*,int,void*,
123374                           int(*)(void*,int,const void*,int,const void*));
123375  int  (*create_collation16)(sqlite3*,const void*,int,void*,
123376                             int(*)(void*,int,const void*,int,const void*));
123377  int  (*create_function)(sqlite3*,const char*,int,int,void*,
123378                          void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
123379                          void (*xStep)(sqlite3_context*,int,sqlite3_value**),
123380                          void (*xFinal)(sqlite3_context*));
123381  int  (*create_function16)(sqlite3*,const void*,int,int,void*,
123382                            void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
123383                            void (*xStep)(sqlite3_context*,int,sqlite3_value**),
123384                            void (*xFinal)(sqlite3_context*));
123385  int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
123386  int  (*data_count)(sqlite3_stmt*pStmt);
123387  sqlite3 * (*db_handle)(sqlite3_stmt*);
123388  int (*declare_vtab)(sqlite3*,const char*);
123389  int  (*enable_shared_cache)(int);
123390  int  (*errcode)(sqlite3*db);
123391  const char * (*errmsg)(sqlite3*);
123392  const void * (*errmsg16)(sqlite3*);
123393  int  (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
123394  int  (*expired)(sqlite3_stmt*);
123395  int  (*finalize)(sqlite3_stmt*pStmt);
123396  void  (*free)(void*);
123397  void  (*free_table)(char**result);
123398  int  (*get_autocommit)(sqlite3*);
123399  void * (*get_auxdata)(sqlite3_context*,int);
123400  int  (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
123401  int  (*global_recover)(void);
123402  void  (*interruptx)(sqlite3*);
123403  sqlite_int64  (*last_insert_rowid)(sqlite3*);
123404  const char * (*libversion)(void);
123405  int  (*libversion_number)(void);
123406  void *(*malloc)(int);
123407  char * (*mprintf)(const char*,...);
123408  int  (*open)(const char*,sqlite3**);
123409  int  (*open16)(const void*,sqlite3**);
123410  int  (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
123411  int  (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
123412  void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
123413  void  (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
123414  void *(*realloc)(void*,int);
123415  int  (*reset)(sqlite3_stmt*pStmt);
123416  void  (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
123417  void  (*result_double)(sqlite3_context*,double);
123418  void  (*result_error)(sqlite3_context*,const char*,int);
123419  void  (*result_error16)(sqlite3_context*,const void*,int);
123420  void  (*result_int)(sqlite3_context*,int);
123421  void  (*result_int64)(sqlite3_context*,sqlite_int64);
123422  void  (*result_null)(sqlite3_context*);
123423  void  (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
123424  void  (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
123425  void  (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
123426  void  (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
123427  void  (*result_value)(sqlite3_context*,sqlite3_value*);
123428  void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
123429  int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
123430                         const char*,const char*),void*);
123431  void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
123432  char * (*xsnprintf)(int,char*,const char*,...);
123433  int  (*step)(sqlite3_stmt*);
123434  int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
123435                                char const**,char const**,int*,int*,int*);
123436  void  (*thread_cleanup)(void);
123437  int  (*total_changes)(sqlite3*);
123438  void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
123439  int  (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
123440  void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
123441                                         sqlite_int64),void*);
123442  void * (*user_data)(sqlite3_context*);
123443  const void * (*value_blob)(sqlite3_value*);
123444  int  (*value_bytes)(sqlite3_value*);
123445  int  (*value_bytes16)(sqlite3_value*);
123446  double  (*value_double)(sqlite3_value*);
123447  int  (*value_int)(sqlite3_value*);
123448  sqlite_int64  (*value_int64)(sqlite3_value*);
123449  int  (*value_numeric_type)(sqlite3_value*);
123450  const unsigned char * (*value_text)(sqlite3_value*);
123451  const void * (*value_text16)(sqlite3_value*);
123452  const void * (*value_text16be)(sqlite3_value*);
123453  const void * (*value_text16le)(sqlite3_value*);
123454  int  (*value_type)(sqlite3_value*);
123455  char *(*vmprintf)(const char*,va_list);
123456  /* Added ??? */
123457  int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
123458  /* Added by 3.3.13 */
123459  int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
123460  int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
123461  int (*clear_bindings)(sqlite3_stmt*);
123462  /* Added by 3.4.1 */
123463  int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
123464                          void (*xDestroy)(void *));
123465  /* Added by 3.5.0 */
123466  int (*bind_zeroblob)(sqlite3_stmt*,int,int);
123467  int (*blob_bytes)(sqlite3_blob*);
123468  int (*blob_close)(sqlite3_blob*);
123469  int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
123470                   int,sqlite3_blob**);
123471  int (*blob_read)(sqlite3_blob*,void*,int,int);
123472  int (*blob_write)(sqlite3_blob*,const void*,int,int);
123473  int (*create_collation_v2)(sqlite3*,const char*,int,void*,
123474                             int(*)(void*,int,const void*,int,const void*),
123475                             void(*)(void*));
123476  int (*file_control)(sqlite3*,const char*,int,void*);
123477  sqlite3_int64 (*memory_highwater)(int);
123478  sqlite3_int64 (*memory_used)(void);
123479  sqlite3_mutex *(*mutex_alloc)(int);
123480  void (*mutex_enter)(sqlite3_mutex*);
123481  void (*mutex_free)(sqlite3_mutex*);
123482  void (*mutex_leave)(sqlite3_mutex*);
123483  int (*mutex_try)(sqlite3_mutex*);
123484  int (*open_v2)(const char*,sqlite3**,int,const char*);
123485  int (*release_memory)(int);
123486  void (*result_error_nomem)(sqlite3_context*);
123487  void (*result_error_toobig)(sqlite3_context*);
123488  int (*sleep)(int);
123489  void (*soft_heap_limit)(int);
123490  sqlite3_vfs *(*vfs_find)(const char*);
123491  int (*vfs_register)(sqlite3_vfs*,int);
123492  int (*vfs_unregister)(sqlite3_vfs*);
123493  int (*xthreadsafe)(void);
123494  void (*result_zeroblob)(sqlite3_context*,int);
123495  void (*result_error_code)(sqlite3_context*,int);
123496  int (*test_control)(int, ...);
123497  void (*randomness)(int,void*);
123498  sqlite3 *(*context_db_handle)(sqlite3_context*);
123499  int (*extended_result_codes)(sqlite3*,int);
123500  int (*limit)(sqlite3*,int,int);
123501  sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
123502  const char *(*sql)(sqlite3_stmt*);
123503  int (*status)(int,int*,int*,int);
123504  int (*backup_finish)(sqlite3_backup*);
123505  sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
123506  int (*backup_pagecount)(sqlite3_backup*);
123507  int (*backup_remaining)(sqlite3_backup*);
123508  int (*backup_step)(sqlite3_backup*,int);
123509  const char *(*compileoption_get)(int);
123510  int (*compileoption_used)(const char*);
123511  int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
123512                            void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
123513                            void (*xStep)(sqlite3_context*,int,sqlite3_value**),
123514                            void (*xFinal)(sqlite3_context*),
123515                            void(*xDestroy)(void*));
123516  int (*db_config)(sqlite3*,int,...);
123517  sqlite3_mutex *(*db_mutex)(sqlite3*);
123518  int (*db_status)(sqlite3*,int,int*,int*,int);
123519  int (*extended_errcode)(sqlite3*);
123520  void (*log)(int,const char*,...);
123521  sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
123522  const char *(*sourceid)(void);
123523  int (*stmt_status)(sqlite3_stmt*,int,int);
123524  int (*strnicmp)(const char*,const char*,int);
123525  int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
123526  int (*wal_autocheckpoint)(sqlite3*,int);
123527  int (*wal_checkpoint)(sqlite3*,const char*);
123528  void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
123529  int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
123530  int (*vtab_config)(sqlite3*,int op,...);
123531  int (*vtab_on_conflict)(sqlite3*);
123532  /* Version 3.7.16 and later */
123533  int (*close_v2)(sqlite3*);
123534  const char *(*db_filename)(sqlite3*,const char*);
123535  int (*db_readonly)(sqlite3*,const char*);
123536  int (*db_release_memory)(sqlite3*);
123537  const char *(*errstr)(int);
123538  int (*stmt_busy)(sqlite3_stmt*);
123539  int (*stmt_readonly)(sqlite3_stmt*);
123540  int (*stricmp)(const char*,const char*);
123541  int (*uri_boolean)(const char*,const char*,int);
123542  sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
123543  const char *(*uri_parameter)(const char*,const char*);
123544  char *(*xvsnprintf)(int,char*,const char*,va_list);
123545  int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
123546  /* Version 3.8.7 and later */
123547  int (*auto_extension)(void(*)(void));
123548  int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
123549                     void(*)(void*));
123550  int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
123551                      void(*)(void*),unsigned char);
123552  int (*cancel_auto_extension)(void(*)(void));
123553  int (*load_extension)(sqlite3*,const char*,const char*,char**);
123554  void *(*malloc64)(sqlite3_uint64);
123555  sqlite3_uint64 (*msize)(void*);
123556  void *(*realloc64)(void*,sqlite3_uint64);
123557  void (*reset_auto_extension)(void);
123558  void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
123559                        void(*)(void*));
123560  void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
123561                         void(*)(void*), unsigned char);
123562  int (*strglob)(const char*,const char*);
123563  /* Version 3.8.11 and later */
123564  sqlite3_value *(*value_dup)(const sqlite3_value*);
123565  void (*value_free)(sqlite3_value*);
123566  int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
123567  int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
123568  /* Version 3.9.0 and later */
123569  unsigned int (*value_subtype)(sqlite3_value*);
123570  void (*result_subtype)(sqlite3_context*,unsigned int);
123571  /* Version 3.10.0 and later */
123572  int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
123573  int (*strlike)(const char*,const char*,unsigned int);
123574  int (*db_cacheflush)(sqlite3*);
123575  /* Version 3.12.0 and later */
123576  int (*system_errno)(sqlite3*);
123577  /* Version 3.14.0 and later */
123578  int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
123579  char *(*expanded_sql)(sqlite3_stmt*);
123580  /* Version 3.18.0 and later */
123581  void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
123582  /* Version 3.20.0 and later */
123583  int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
123584                    sqlite3_stmt**,const char**);
123585  int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
123586                      sqlite3_stmt**,const void**);
123587  int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
123588  void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
123589  void *(*value_pointer)(sqlite3_value*,const char*);
123590  int (*vtab_nochange)(sqlite3_context*);
123591  int (*value_nochange)(sqlite3_value*);
123592  const char *(*vtab_collation)(sqlite3_index_info*,int);
123593  /* Version 3.24.0 and later */
123594  int (*keyword_count)(void);
123595  int (*keyword_name)(int,const char**,int*);
123596  int (*keyword_check)(const char*,int);
123597  sqlite3_str *(*str_new)(sqlite3*);
123598  char *(*str_finish)(sqlite3_str*);
123599  void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
123600  void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
123601  void (*str_append)(sqlite3_str*, const char *zIn, int N);
123602  void (*str_appendall)(sqlite3_str*, const char *zIn);
123603  void (*str_appendchar)(sqlite3_str*, int N, char C);
123604  void (*str_reset)(sqlite3_str*);
123605  int (*str_errcode)(sqlite3_str*);
123606  int (*str_length)(sqlite3_str*);
123607  char *(*str_value)(sqlite3_str*);
123608  /* Version 3.25.0 and later */
123609  int (*create_window_function)(sqlite3*,const char*,int,int,void*,
123610                            void (*xStep)(sqlite3_context*,int,sqlite3_value**),
123611                            void (*xFinal)(sqlite3_context*),
123612                            void (*xValue)(sqlite3_context*),
123613                            void (*xInv)(sqlite3_context*,int,sqlite3_value**),
123614                            void(*xDestroy)(void*));
123615  /* Version 3.26.0 and later */
123616  const char *(*normalized_sql)(sqlite3_stmt*);
123617  /* Version 3.28.0 and later */
123618  int (*stmt_isexplain)(sqlite3_stmt*);
123619  int (*value_frombind)(sqlite3_value*);
123620  /* Version 3.30.0 and later */
123621  int (*drop_modules)(sqlite3*,const char**);
123622  /* Version 3.31.0 and later */
123623  sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64);
123624  const char *(*uri_key)(const char*,int);
123625  const char *(*filename_database)(const char*);
123626  const char *(*filename_journal)(const char*);
123627  const char *(*filename_wal)(const char*);
123628  /* Version 3.32.0 and later */
123629  char *(*create_filename)(const char*,const char*,const char*,
123630                           int,const char**);
123631  void (*free_filename)(char*);
123632  sqlite3_file *(*database_file_object)(const char*);
123633};
123634
123635/*
123636** This is the function signature used for all extension entry points.  It
123637** is also defined in the file "loadext.c".
123638*/
123639typedef int (*sqlite3_loadext_entry)(
123640  sqlite3 *db,                       /* Handle to the database. */
123641  char **pzErrMsg,                   /* Used to set error string on failure. */
123642  const sqlite3_api_routines *pThunk /* Extension API function pointers. */
123643);
123644
123645/*
123646** The following macros redefine the API routines so that they are
123647** redirected through the global sqlite3_api structure.
123648**
123649** This header file is also used by the loadext.c source file
123650** (part of the main SQLite library - not an extension) so that
123651** it can get access to the sqlite3_api_routines structure
123652** definition.  But the main library does not want to redefine
123653** the API.  So the redefinition macros are only valid if the
123654** SQLITE_CORE macros is undefined.
123655*/
123656#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
123657#define sqlite3_aggregate_context      sqlite3_api->aggregate_context
123658#ifndef SQLITE_OMIT_DEPRECATED
123659#define sqlite3_aggregate_count        sqlite3_api->aggregate_count
123660#endif
123661#define sqlite3_bind_blob              sqlite3_api->bind_blob
123662#define sqlite3_bind_double            sqlite3_api->bind_double
123663#define sqlite3_bind_int               sqlite3_api->bind_int
123664#define sqlite3_bind_int64             sqlite3_api->bind_int64
123665#define sqlite3_bind_null              sqlite3_api->bind_null
123666#define sqlite3_bind_parameter_count   sqlite3_api->bind_parameter_count
123667#define sqlite3_bind_parameter_index   sqlite3_api->bind_parameter_index
123668#define sqlite3_bind_parameter_name    sqlite3_api->bind_parameter_name
123669#define sqlite3_bind_text              sqlite3_api->bind_text
123670#define sqlite3_bind_text16            sqlite3_api->bind_text16
123671#define sqlite3_bind_value             sqlite3_api->bind_value
123672#define sqlite3_busy_handler           sqlite3_api->busy_handler
123673#define sqlite3_busy_timeout           sqlite3_api->busy_timeout
123674#define sqlite3_changes                sqlite3_api->changes
123675#define sqlite3_close                  sqlite3_api->close
123676#define sqlite3_collation_needed       sqlite3_api->collation_needed
123677#define sqlite3_collation_needed16     sqlite3_api->collation_needed16
123678#define sqlite3_column_blob            sqlite3_api->column_blob
123679#define sqlite3_column_bytes           sqlite3_api->column_bytes
123680#define sqlite3_column_bytes16         sqlite3_api->column_bytes16
123681#define sqlite3_column_count           sqlite3_api->column_count
123682#define sqlite3_column_database_name   sqlite3_api->column_database_name
123683#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
123684#define sqlite3_column_decltype        sqlite3_api->column_decltype
123685#define sqlite3_column_decltype16      sqlite3_api->column_decltype16
123686#define sqlite3_column_double          sqlite3_api->column_double
123687#define sqlite3_column_int             sqlite3_api->column_int
123688#define sqlite3_column_int64           sqlite3_api->column_int64
123689#define sqlite3_column_name            sqlite3_api->column_name
123690#define sqlite3_column_name16          sqlite3_api->column_name16
123691#define sqlite3_column_origin_name     sqlite3_api->column_origin_name
123692#define sqlite3_column_origin_name16   sqlite3_api->column_origin_name16
123693#define sqlite3_column_table_name      sqlite3_api->column_table_name
123694#define sqlite3_column_table_name16    sqlite3_api->column_table_name16
123695#define sqlite3_column_text            sqlite3_api->column_text
123696#define sqlite3_column_text16          sqlite3_api->column_text16
123697#define sqlite3_column_type            sqlite3_api->column_type
123698#define sqlite3_column_value           sqlite3_api->column_value
123699#define sqlite3_commit_hook            sqlite3_api->commit_hook
123700#define sqlite3_complete               sqlite3_api->complete
123701#define sqlite3_complete16             sqlite3_api->complete16
123702#define sqlite3_create_collation       sqlite3_api->create_collation
123703#define sqlite3_create_collation16     sqlite3_api->create_collation16
123704#define sqlite3_create_function        sqlite3_api->create_function
123705#define sqlite3_create_function16      sqlite3_api->create_function16
123706#define sqlite3_create_module          sqlite3_api->create_module
123707#define sqlite3_create_module_v2       sqlite3_api->create_module_v2
123708#define sqlite3_data_count             sqlite3_api->data_count
123709#define sqlite3_db_handle              sqlite3_api->db_handle
123710#define sqlite3_declare_vtab           sqlite3_api->declare_vtab
123711#define sqlite3_enable_shared_cache    sqlite3_api->enable_shared_cache
123712#define sqlite3_errcode                sqlite3_api->errcode
123713#define sqlite3_errmsg                 sqlite3_api->errmsg
123714#define sqlite3_errmsg16               sqlite3_api->errmsg16
123715#define sqlite3_exec                   sqlite3_api->exec
123716#ifndef SQLITE_OMIT_DEPRECATED
123717#define sqlite3_expired                sqlite3_api->expired
123718#endif
123719#define sqlite3_finalize               sqlite3_api->finalize
123720#define sqlite3_free                   sqlite3_api->free
123721#define sqlite3_free_table             sqlite3_api->free_table
123722#define sqlite3_get_autocommit         sqlite3_api->get_autocommit
123723#define sqlite3_get_auxdata            sqlite3_api->get_auxdata
123724#define sqlite3_get_table              sqlite3_api->get_table
123725#ifndef SQLITE_OMIT_DEPRECATED
123726#define sqlite3_global_recover         sqlite3_api->global_recover
123727#endif
123728#define sqlite3_interrupt              sqlite3_api->interruptx
123729#define sqlite3_last_insert_rowid      sqlite3_api->last_insert_rowid
123730#define sqlite3_libversion             sqlite3_api->libversion
123731#define sqlite3_libversion_number      sqlite3_api->libversion_number
123732#define sqlite3_malloc                 sqlite3_api->malloc
123733#define sqlite3_mprintf                sqlite3_api->mprintf
123734#define sqlite3_open                   sqlite3_api->open
123735#define sqlite3_open16                 sqlite3_api->open16
123736#define sqlite3_prepare                sqlite3_api->prepare
123737#define sqlite3_prepare16              sqlite3_api->prepare16
123738#define sqlite3_prepare_v2             sqlite3_api->prepare_v2
123739#define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
123740#define sqlite3_profile                sqlite3_api->profile
123741#define sqlite3_progress_handler       sqlite3_api->progress_handler
123742#define sqlite3_realloc                sqlite3_api->realloc
123743#define sqlite3_reset                  sqlite3_api->reset
123744#define sqlite3_result_blob            sqlite3_api->result_blob
123745#define sqlite3_result_double          sqlite3_api->result_double
123746#define sqlite3_result_error           sqlite3_api->result_error
123747#define sqlite3_result_error16         sqlite3_api->result_error16
123748#define sqlite3_result_int             sqlite3_api->result_int
123749#define sqlite3_result_int64           sqlite3_api->result_int64
123750#define sqlite3_result_null            sqlite3_api->result_null
123751#define sqlite3_result_text            sqlite3_api->result_text
123752#define sqlite3_result_text16          sqlite3_api->result_text16
123753#define sqlite3_result_text16be        sqlite3_api->result_text16be
123754#define sqlite3_result_text16le        sqlite3_api->result_text16le
123755#define sqlite3_result_value           sqlite3_api->result_value
123756#define sqlite3_rollback_hook          sqlite3_api->rollback_hook
123757#define sqlite3_set_authorizer         sqlite3_api->set_authorizer
123758#define sqlite3_set_auxdata            sqlite3_api->set_auxdata
123759#define sqlite3_snprintf               sqlite3_api->xsnprintf
123760#define sqlite3_step                   sqlite3_api->step
123761#define sqlite3_table_column_metadata  sqlite3_api->table_column_metadata
123762#define sqlite3_thread_cleanup         sqlite3_api->thread_cleanup
123763#define sqlite3_total_changes          sqlite3_api->total_changes
123764#define sqlite3_trace                  sqlite3_api->trace
123765#ifndef SQLITE_OMIT_DEPRECATED
123766#define sqlite3_transfer_bindings      sqlite3_api->transfer_bindings
123767#endif
123768#define sqlite3_update_hook            sqlite3_api->update_hook
123769#define sqlite3_user_data              sqlite3_api->user_data
123770#define sqlite3_value_blob             sqlite3_api->value_blob
123771#define sqlite3_value_bytes            sqlite3_api->value_bytes
123772#define sqlite3_value_bytes16          sqlite3_api->value_bytes16
123773#define sqlite3_value_double           sqlite3_api->value_double
123774#define sqlite3_value_int              sqlite3_api->value_int
123775#define sqlite3_value_int64            sqlite3_api->value_int64
123776#define sqlite3_value_numeric_type     sqlite3_api->value_numeric_type
123777#define sqlite3_value_text             sqlite3_api->value_text
123778#define sqlite3_value_text16           sqlite3_api->value_text16
123779#define sqlite3_value_text16be         sqlite3_api->value_text16be
123780#define sqlite3_value_text16le         sqlite3_api->value_text16le
123781#define sqlite3_value_type             sqlite3_api->value_type
123782#define sqlite3_vmprintf               sqlite3_api->vmprintf
123783#define sqlite3_vsnprintf              sqlite3_api->xvsnprintf
123784#define sqlite3_overload_function      sqlite3_api->overload_function
123785#define sqlite3_prepare_v2             sqlite3_api->prepare_v2
123786#define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
123787#define sqlite3_clear_bindings         sqlite3_api->clear_bindings
123788#define sqlite3_bind_zeroblob          sqlite3_api->bind_zeroblob
123789#define sqlite3_blob_bytes             sqlite3_api->blob_bytes
123790#define sqlite3_blob_close             sqlite3_api->blob_close
123791#define sqlite3_blob_open              sqlite3_api->blob_open
123792#define sqlite3_blob_read              sqlite3_api->blob_read
123793#define sqlite3_blob_write             sqlite3_api->blob_write
123794#define sqlite3_create_collation_v2    sqlite3_api->create_collation_v2
123795#define sqlite3_file_control           sqlite3_api->file_control
123796#define sqlite3_memory_highwater       sqlite3_api->memory_highwater
123797#define sqlite3_memory_used            sqlite3_api->memory_used
123798#define sqlite3_mutex_alloc            sqlite3_api->mutex_alloc
123799#define sqlite3_mutex_enter            sqlite3_api->mutex_enter
123800#define sqlite3_mutex_free             sqlite3_api->mutex_free
123801#define sqlite3_mutex_leave            sqlite3_api->mutex_leave
123802#define sqlite3_mutex_try              sqlite3_api->mutex_try
123803#define sqlite3_open_v2                sqlite3_api->open_v2
123804#define sqlite3_release_memory         sqlite3_api->release_memory
123805#define sqlite3_result_error_nomem     sqlite3_api->result_error_nomem
123806#define sqlite3_result_error_toobig    sqlite3_api->result_error_toobig
123807#define sqlite3_sleep                  sqlite3_api->sleep
123808#define sqlite3_soft_heap_limit        sqlite3_api->soft_heap_limit
123809#define sqlite3_vfs_find               sqlite3_api->vfs_find
123810#define sqlite3_vfs_register           sqlite3_api->vfs_register
123811#define sqlite3_vfs_unregister         sqlite3_api->vfs_unregister
123812#define sqlite3_threadsafe             sqlite3_api->xthreadsafe
123813#define sqlite3_result_zeroblob        sqlite3_api->result_zeroblob
123814#define sqlite3_result_error_code      sqlite3_api->result_error_code
123815#define sqlite3_test_control           sqlite3_api->test_control
123816#define sqlite3_randomness             sqlite3_api->randomness
123817#define sqlite3_context_db_handle      sqlite3_api->context_db_handle
123818#define sqlite3_extended_result_codes  sqlite3_api->extended_result_codes
123819#define sqlite3_limit                  sqlite3_api->limit
123820#define sqlite3_next_stmt              sqlite3_api->next_stmt
123821#define sqlite3_sql                    sqlite3_api->sql
123822#define sqlite3_status                 sqlite3_api->status
123823#define sqlite3_backup_finish          sqlite3_api->backup_finish
123824#define sqlite3_backup_init            sqlite3_api->backup_init
123825#define sqlite3_backup_pagecount       sqlite3_api->backup_pagecount
123826#define sqlite3_backup_remaining       sqlite3_api->backup_remaining
123827#define sqlite3_backup_step            sqlite3_api->backup_step
123828#define sqlite3_compileoption_get      sqlite3_api->compileoption_get
123829#define sqlite3_compileoption_used     sqlite3_api->compileoption_used
123830#define sqlite3_create_function_v2     sqlite3_api->create_function_v2
123831#define sqlite3_db_config              sqlite3_api->db_config
123832#define sqlite3_db_mutex               sqlite3_api->db_mutex
123833#define sqlite3_db_status              sqlite3_api->db_status
123834#define sqlite3_extended_errcode       sqlite3_api->extended_errcode
123835#define sqlite3_log                    sqlite3_api->log
123836#define sqlite3_soft_heap_limit64      sqlite3_api->soft_heap_limit64
123837#define sqlite3_sourceid               sqlite3_api->sourceid
123838#define sqlite3_stmt_status            sqlite3_api->stmt_status
123839#define sqlite3_strnicmp               sqlite3_api->strnicmp
123840#define sqlite3_unlock_notify          sqlite3_api->unlock_notify
123841#define sqlite3_wal_autocheckpoint     sqlite3_api->wal_autocheckpoint
123842#define sqlite3_wal_checkpoint         sqlite3_api->wal_checkpoint
123843#define sqlite3_wal_hook               sqlite3_api->wal_hook
123844#define sqlite3_blob_reopen            sqlite3_api->blob_reopen
123845#define sqlite3_vtab_config            sqlite3_api->vtab_config
123846#define sqlite3_vtab_on_conflict       sqlite3_api->vtab_on_conflict
123847/* Version 3.7.16 and later */
123848#define sqlite3_close_v2               sqlite3_api->close_v2
123849#define sqlite3_db_filename            sqlite3_api->db_filename
123850#define sqlite3_db_readonly            sqlite3_api->db_readonly
123851#define sqlite3_db_release_memory      sqlite3_api->db_release_memory
123852#define sqlite3_errstr                 sqlite3_api->errstr
123853#define sqlite3_stmt_busy              sqlite3_api->stmt_busy
123854#define sqlite3_stmt_readonly          sqlite3_api->stmt_readonly
123855#define sqlite3_stricmp                sqlite3_api->stricmp
123856#define sqlite3_uri_boolean            sqlite3_api->uri_boolean
123857#define sqlite3_uri_int64              sqlite3_api->uri_int64
123858#define sqlite3_uri_parameter          sqlite3_api->uri_parameter
123859#define sqlite3_uri_vsnprintf          sqlite3_api->xvsnprintf
123860#define sqlite3_wal_checkpoint_v2      sqlite3_api->wal_checkpoint_v2
123861/* Version 3.8.7 and later */
123862#define sqlite3_auto_extension         sqlite3_api->auto_extension
123863#define sqlite3_bind_blob64            sqlite3_api->bind_blob64
123864#define sqlite3_bind_text64            sqlite3_api->bind_text64
123865#define sqlite3_cancel_auto_extension  sqlite3_api->cancel_auto_extension
123866#define sqlite3_load_extension         sqlite3_api->load_extension
123867#define sqlite3_malloc64               sqlite3_api->malloc64
123868#define sqlite3_msize                  sqlite3_api->msize
123869#define sqlite3_realloc64              sqlite3_api->realloc64
123870#define sqlite3_reset_auto_extension   sqlite3_api->reset_auto_extension
123871#define sqlite3_result_blob64          sqlite3_api->result_blob64
123872#define sqlite3_result_text64          sqlite3_api->result_text64
123873#define sqlite3_strglob                sqlite3_api->strglob
123874/* Version 3.8.11 and later */
123875#define sqlite3_value_dup              sqlite3_api->value_dup
123876#define sqlite3_value_free             sqlite3_api->value_free
123877#define sqlite3_result_zeroblob64      sqlite3_api->result_zeroblob64
123878#define sqlite3_bind_zeroblob64        sqlite3_api->bind_zeroblob64
123879/* Version 3.9.0 and later */
123880#define sqlite3_value_subtype          sqlite3_api->value_subtype
123881#define sqlite3_result_subtype         sqlite3_api->result_subtype
123882/* Version 3.10.0 and later */
123883#define sqlite3_status64               sqlite3_api->status64
123884#define sqlite3_strlike                sqlite3_api->strlike
123885#define sqlite3_db_cacheflush          sqlite3_api->db_cacheflush
123886/* Version 3.12.0 and later */
123887#define sqlite3_system_errno           sqlite3_api->system_errno
123888/* Version 3.14.0 and later */
123889#define sqlite3_trace_v2               sqlite3_api->trace_v2
123890#define sqlite3_expanded_sql           sqlite3_api->expanded_sql
123891/* Version 3.18.0 and later */
123892#define sqlite3_set_last_insert_rowid  sqlite3_api->set_last_insert_rowid
123893/* Version 3.20.0 and later */
123894#define sqlite3_prepare_v3             sqlite3_api->prepare_v3
123895#define sqlite3_prepare16_v3           sqlite3_api->prepare16_v3
123896#define sqlite3_bind_pointer           sqlite3_api->bind_pointer
123897#define sqlite3_result_pointer         sqlite3_api->result_pointer
123898#define sqlite3_value_pointer          sqlite3_api->value_pointer
123899/* Version 3.22.0 and later */
123900#define sqlite3_vtab_nochange          sqlite3_api->vtab_nochange
123901#define sqlite3_value_nochange         sqlite3_api->value_nochange
123902#define sqlite3_vtab_collation         sqlite3_api->vtab_collation
123903/* Version 3.24.0 and later */
123904#define sqlite3_keyword_count          sqlite3_api->keyword_count
123905#define sqlite3_keyword_name           sqlite3_api->keyword_name
123906#define sqlite3_keyword_check          sqlite3_api->keyword_check
123907#define sqlite3_str_new                sqlite3_api->str_new
123908#define sqlite3_str_finish             sqlite3_api->str_finish
123909#define sqlite3_str_appendf            sqlite3_api->str_appendf
123910#define sqlite3_str_vappendf           sqlite3_api->str_vappendf
123911#define sqlite3_str_append             sqlite3_api->str_append
123912#define sqlite3_str_appendall          sqlite3_api->str_appendall
123913#define sqlite3_str_appendchar         sqlite3_api->str_appendchar
123914#define sqlite3_str_reset              sqlite3_api->str_reset
123915#define sqlite3_str_errcode            sqlite3_api->str_errcode
123916#define sqlite3_str_length             sqlite3_api->str_length
123917#define sqlite3_str_value              sqlite3_api->str_value
123918/* Version 3.25.0 and later */
123919#define sqlite3_create_window_function sqlite3_api->create_window_function
123920/* Version 3.26.0 and later */
123921#define sqlite3_normalized_sql         sqlite3_api->normalized_sql
123922/* Version 3.28.0 and later */
123923#define sqlite3_stmt_isexplain         sqlite3_api->stmt_isexplain
123924#define sqlite3_value_frombind         sqlite3_api->value_frombind
123925/* Version 3.30.0 and later */
123926#define sqlite3_drop_modules           sqlite3_api->drop_modules
123927/* Version 3.31.0 and later */
123928#define sqlite3_hard_heap_limit64      sqlite3_api->hard_heap_limit64
123929#define sqlite3_uri_key                sqlite3_api->uri_key
123930#define sqlite3_filename_database      sqlite3_api->filename_database
123931#define sqlite3_filename_journal       sqlite3_api->filename_journal
123932#define sqlite3_filename_wal           sqlite3_api->filename_wal
123933/* Version 3.32.0 and later */
123934#define sqlite3_create_filename        sqlite3_api->create_filename
123935#define sqlite3_free_filename          sqlite3_api->free_filename
123936#define sqlite3_database_file_object   sqlite3_api->database_file_object
123937#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
123938
123939#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
123940  /* This case when the file really is being compiled as a loadable
123941  ** extension */
123942# define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
123943# define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
123944# define SQLITE_EXTENSION_INIT3     \
123945    extern const sqlite3_api_routines *sqlite3_api;
123946#else
123947  /* This case when the file is being statically linked into the
123948  ** application */
123949# define SQLITE_EXTENSION_INIT1     /*no-op*/
123950# define SQLITE_EXTENSION_INIT2(v)  (void)v; /* unused parameter */
123951# define SQLITE_EXTENSION_INIT3     /*no-op*/
123952#endif
123953
123954#endif /* SQLITE3EXT_H */
123955
123956/************** End of sqlite3ext.h ******************************************/
123957/************** Continuing where we left off in loadext.c ********************/
123958/* #include "sqliteInt.h" */
123959
123960#ifndef SQLITE_OMIT_LOAD_EXTENSION
123961/*
123962** Some API routines are omitted when various features are
123963** excluded from a build of SQLite.  Substitute a NULL pointer
123964** for any missing APIs.
123965*/
123966#ifndef SQLITE_ENABLE_COLUMN_METADATA
123967# define sqlite3_column_database_name   0
123968# define sqlite3_column_database_name16 0
123969# define sqlite3_column_table_name      0
123970# define sqlite3_column_table_name16    0
123971# define sqlite3_column_origin_name     0
123972# define sqlite3_column_origin_name16   0
123973#endif
123974
123975#ifdef SQLITE_OMIT_AUTHORIZATION
123976# define sqlite3_set_authorizer         0
123977#endif
123978
123979#ifdef SQLITE_OMIT_UTF16
123980# define sqlite3_bind_text16            0
123981# define sqlite3_collation_needed16     0
123982# define sqlite3_column_decltype16      0
123983# define sqlite3_column_name16          0
123984# define sqlite3_column_text16          0
123985# define sqlite3_complete16             0
123986# define sqlite3_create_collation16     0
123987# define sqlite3_create_function16      0
123988# define sqlite3_errmsg16               0
123989# define sqlite3_open16                 0
123990# define sqlite3_prepare16              0
123991# define sqlite3_prepare16_v2           0
123992# define sqlite3_prepare16_v3           0
123993# define sqlite3_result_error16         0
123994# define sqlite3_result_text16          0
123995# define sqlite3_result_text16be        0
123996# define sqlite3_result_text16le        0
123997# define sqlite3_value_text16           0
123998# define sqlite3_value_text16be         0
123999# define sqlite3_value_text16le         0
124000# define sqlite3_column_database_name16 0
124001# define sqlite3_column_table_name16    0
124002# define sqlite3_column_origin_name16   0
124003#endif
124004
124005#ifdef SQLITE_OMIT_COMPLETE
124006# define sqlite3_complete 0
124007# define sqlite3_complete16 0
124008#endif
124009
124010#ifdef SQLITE_OMIT_DECLTYPE
124011# define sqlite3_column_decltype16      0
124012# define sqlite3_column_decltype        0
124013#endif
124014
124015#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
124016# define sqlite3_progress_handler 0
124017#endif
124018
124019#ifdef SQLITE_OMIT_VIRTUALTABLE
124020# define sqlite3_create_module 0
124021# define sqlite3_create_module_v2 0
124022# define sqlite3_declare_vtab 0
124023# define sqlite3_vtab_config 0
124024# define sqlite3_vtab_on_conflict 0
124025# define sqlite3_vtab_collation 0
124026#endif
124027
124028#ifdef SQLITE_OMIT_SHARED_CACHE
124029# define sqlite3_enable_shared_cache 0
124030#endif
124031
124032#if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED)
124033# define sqlite3_profile       0
124034# define sqlite3_trace         0
124035#endif
124036
124037#ifdef SQLITE_OMIT_GET_TABLE
124038# define sqlite3_free_table    0
124039# define sqlite3_get_table     0
124040#endif
124041
124042#ifdef SQLITE_OMIT_INCRBLOB
124043#define sqlite3_bind_zeroblob  0
124044#define sqlite3_blob_bytes     0
124045#define sqlite3_blob_close     0
124046#define sqlite3_blob_open      0
124047#define sqlite3_blob_read      0
124048#define sqlite3_blob_write     0
124049#define sqlite3_blob_reopen    0
124050#endif
124051
124052#if defined(SQLITE_OMIT_TRACE)
124053# define sqlite3_trace_v2      0
124054#endif
124055
124056/*
124057** The following structure contains pointers to all SQLite API routines.
124058** A pointer to this structure is passed into extensions when they are
124059** loaded so that the extension can make calls back into the SQLite
124060** library.
124061**
124062** When adding new APIs, add them to the bottom of this structure
124063** in order to preserve backwards compatibility.
124064**
124065** Extensions that use newer APIs should first call the
124066** sqlite3_libversion_number() to make sure that the API they
124067** intend to use is supported by the library.  Extensions should
124068** also check to make sure that the pointer to the function is
124069** not NULL before calling it.
124070*/
124071static const sqlite3_api_routines sqlite3Apis = {
124072  sqlite3_aggregate_context,
124073#ifndef SQLITE_OMIT_DEPRECATED
124074  sqlite3_aggregate_count,
124075#else
124076  0,
124077#endif
124078  sqlite3_bind_blob,
124079  sqlite3_bind_double,
124080  sqlite3_bind_int,
124081  sqlite3_bind_int64,
124082  sqlite3_bind_null,
124083  sqlite3_bind_parameter_count,
124084  sqlite3_bind_parameter_index,
124085  sqlite3_bind_parameter_name,
124086  sqlite3_bind_text,
124087  sqlite3_bind_text16,
124088  sqlite3_bind_value,
124089  sqlite3_busy_handler,
124090  sqlite3_busy_timeout,
124091  sqlite3_changes,
124092  sqlite3_close,
124093  sqlite3_collation_needed,
124094  sqlite3_collation_needed16,
124095  sqlite3_column_blob,
124096  sqlite3_column_bytes,
124097  sqlite3_column_bytes16,
124098  sqlite3_column_count,
124099  sqlite3_column_database_name,
124100  sqlite3_column_database_name16,
124101  sqlite3_column_decltype,
124102  sqlite3_column_decltype16,
124103  sqlite3_column_double,
124104  sqlite3_column_int,
124105  sqlite3_column_int64,
124106  sqlite3_column_name,
124107  sqlite3_column_name16,
124108  sqlite3_column_origin_name,
124109  sqlite3_column_origin_name16,
124110  sqlite3_column_table_name,
124111  sqlite3_column_table_name16,
124112  sqlite3_column_text,
124113  sqlite3_column_text16,
124114  sqlite3_column_type,
124115  sqlite3_column_value,
124116  sqlite3_commit_hook,
124117  sqlite3_complete,
124118  sqlite3_complete16,
124119  sqlite3_create_collation,
124120  sqlite3_create_collation16,
124121  sqlite3_create_function,
124122  sqlite3_create_function16,
124123  sqlite3_create_module,
124124  sqlite3_data_count,
124125  sqlite3_db_handle,
124126  sqlite3_declare_vtab,
124127  sqlite3_enable_shared_cache,
124128  sqlite3_errcode,
124129  sqlite3_errmsg,
124130  sqlite3_errmsg16,
124131  sqlite3_exec,
124132#ifndef SQLITE_OMIT_DEPRECATED
124133  sqlite3_expired,
124134#else
124135  0,
124136#endif
124137  sqlite3_finalize,
124138  sqlite3_free,
124139  sqlite3_free_table,
124140  sqlite3_get_autocommit,
124141  sqlite3_get_auxdata,
124142  sqlite3_get_table,
124143  0,     /* Was sqlite3_global_recover(), but that function is deprecated */
124144  sqlite3_interrupt,
124145  sqlite3_last_insert_rowid,
124146  sqlite3_libversion,
124147  sqlite3_libversion_number,
124148  sqlite3_malloc,
124149  sqlite3_mprintf,
124150  sqlite3_open,
124151  sqlite3_open16,
124152  sqlite3_prepare,
124153  sqlite3_prepare16,
124154  sqlite3_profile,
124155  sqlite3_progress_handler,
124156  sqlite3_realloc,
124157  sqlite3_reset,
124158  sqlite3_result_blob,
124159  sqlite3_result_double,
124160  sqlite3_result_error,
124161  sqlite3_result_error16,
124162  sqlite3_result_int,
124163  sqlite3_result_int64,
124164  sqlite3_result_null,
124165  sqlite3_result_text,
124166  sqlite3_result_text16,
124167  sqlite3_result_text16be,
124168  sqlite3_result_text16le,
124169  sqlite3_result_value,
124170  sqlite3_rollback_hook,
124171  sqlite3_set_authorizer,
124172  sqlite3_set_auxdata,
124173  sqlite3_snprintf,
124174  sqlite3_step,
124175  sqlite3_table_column_metadata,
124176#ifndef SQLITE_OMIT_DEPRECATED
124177  sqlite3_thread_cleanup,
124178#else
124179  0,
124180#endif
124181  sqlite3_total_changes,
124182  sqlite3_trace,
124183#ifndef SQLITE_OMIT_DEPRECATED
124184  sqlite3_transfer_bindings,
124185#else
124186  0,
124187#endif
124188  sqlite3_update_hook,
124189  sqlite3_user_data,
124190  sqlite3_value_blob,
124191  sqlite3_value_bytes,
124192  sqlite3_value_bytes16,
124193  sqlite3_value_double,
124194  sqlite3_value_int,
124195  sqlite3_value_int64,
124196  sqlite3_value_numeric_type,
124197  sqlite3_value_text,
124198  sqlite3_value_text16,
124199  sqlite3_value_text16be,
124200  sqlite3_value_text16le,
124201  sqlite3_value_type,
124202  sqlite3_vmprintf,
124203  /*
124204  ** The original API set ends here.  All extensions can call any
124205  ** of the APIs above provided that the pointer is not NULL.  But
124206  ** before calling APIs that follow, extension should check the
124207  ** sqlite3_libversion_number() to make sure they are dealing with
124208  ** a library that is new enough to support that API.
124209  *************************************************************************
124210  */
124211  sqlite3_overload_function,
124212
124213  /*
124214  ** Added after 3.3.13
124215  */
124216  sqlite3_prepare_v2,
124217  sqlite3_prepare16_v2,
124218  sqlite3_clear_bindings,
124219
124220  /*
124221  ** Added for 3.4.1
124222  */
124223  sqlite3_create_module_v2,
124224
124225  /*
124226  ** Added for 3.5.0
124227  */
124228  sqlite3_bind_zeroblob,
124229  sqlite3_blob_bytes,
124230  sqlite3_blob_close,
124231  sqlite3_blob_open,
124232  sqlite3_blob_read,
124233  sqlite3_blob_write,
124234  sqlite3_create_collation_v2,
124235  sqlite3_file_control,
124236  sqlite3_memory_highwater,
124237  sqlite3_memory_used,
124238#ifdef SQLITE_MUTEX_OMIT
124239  0,
124240  0,
124241  0,
124242  0,
124243  0,
124244#else
124245  sqlite3_mutex_alloc,
124246  sqlite3_mutex_enter,
124247  sqlite3_mutex_free,
124248  sqlite3_mutex_leave,
124249  sqlite3_mutex_try,
124250#endif
124251  sqlite3_open_v2,
124252  sqlite3_release_memory,
124253  sqlite3_result_error_nomem,
124254  sqlite3_result_error_toobig,
124255  sqlite3_sleep,
124256  sqlite3_soft_heap_limit,
124257  sqlite3_vfs_find,
124258  sqlite3_vfs_register,
124259  sqlite3_vfs_unregister,
124260
124261  /*
124262  ** Added for 3.5.8
124263  */
124264  sqlite3_threadsafe,
124265  sqlite3_result_zeroblob,
124266  sqlite3_result_error_code,
124267  sqlite3_test_control,
124268  sqlite3_randomness,
124269  sqlite3_context_db_handle,
124270
124271  /*
124272  ** Added for 3.6.0
124273  */
124274  sqlite3_extended_result_codes,
124275  sqlite3_limit,
124276  sqlite3_next_stmt,
124277  sqlite3_sql,
124278  sqlite3_status,
124279
124280  /*
124281  ** Added for 3.7.4
124282  */
124283  sqlite3_backup_finish,
124284  sqlite3_backup_init,
124285  sqlite3_backup_pagecount,
124286  sqlite3_backup_remaining,
124287  sqlite3_backup_step,
124288#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
124289  sqlite3_compileoption_get,
124290  sqlite3_compileoption_used,
124291#else
124292  0,
124293  0,
124294#endif
124295  sqlite3_create_function_v2,
124296  sqlite3_db_config,
124297  sqlite3_db_mutex,
124298  sqlite3_db_status,
124299  sqlite3_extended_errcode,
124300  sqlite3_log,
124301  sqlite3_soft_heap_limit64,
124302  sqlite3_sourceid,
124303  sqlite3_stmt_status,
124304  sqlite3_strnicmp,
124305#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
124306  sqlite3_unlock_notify,
124307#else
124308  0,
124309#endif
124310#ifndef SQLITE_OMIT_WAL
124311  sqlite3_wal_autocheckpoint,
124312  sqlite3_wal_checkpoint,
124313  sqlite3_wal_hook,
124314#else
124315  0,
124316  0,
124317  0,
124318#endif
124319  sqlite3_blob_reopen,
124320  sqlite3_vtab_config,
124321  sqlite3_vtab_on_conflict,
124322  sqlite3_close_v2,
124323  sqlite3_db_filename,
124324  sqlite3_db_readonly,
124325  sqlite3_db_release_memory,
124326  sqlite3_errstr,
124327  sqlite3_stmt_busy,
124328  sqlite3_stmt_readonly,
124329  sqlite3_stricmp,
124330  sqlite3_uri_boolean,
124331  sqlite3_uri_int64,
124332  sqlite3_uri_parameter,
124333  sqlite3_vsnprintf,
124334  sqlite3_wal_checkpoint_v2,
124335  /* Version 3.8.7 and later */
124336  sqlite3_auto_extension,
124337  sqlite3_bind_blob64,
124338  sqlite3_bind_text64,
124339  sqlite3_cancel_auto_extension,
124340  sqlite3_load_extension,
124341  sqlite3_malloc64,
124342  sqlite3_msize,
124343  sqlite3_realloc64,
124344  sqlite3_reset_auto_extension,
124345  sqlite3_result_blob64,
124346  sqlite3_result_text64,
124347  sqlite3_strglob,
124348  /* Version 3.8.11 and later */
124349  (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
124350  sqlite3_value_free,
124351  sqlite3_result_zeroblob64,
124352  sqlite3_bind_zeroblob64,
124353  /* Version 3.9.0 and later */
124354  sqlite3_value_subtype,
124355  sqlite3_result_subtype,
124356  /* Version 3.10.0 and later */
124357  sqlite3_status64,
124358  sqlite3_strlike,
124359  sqlite3_db_cacheflush,
124360  /* Version 3.12.0 and later */
124361  sqlite3_system_errno,
124362  /* Version 3.14.0 and later */
124363  sqlite3_trace_v2,
124364  sqlite3_expanded_sql,
124365  /* Version 3.18.0 and later */
124366  sqlite3_set_last_insert_rowid,
124367  /* Version 3.20.0 and later */
124368  sqlite3_prepare_v3,
124369  sqlite3_prepare16_v3,
124370  sqlite3_bind_pointer,
124371  sqlite3_result_pointer,
124372  sqlite3_value_pointer,
124373  /* Version 3.22.0 and later */
124374  sqlite3_vtab_nochange,
124375  sqlite3_value_nochange,
124376  sqlite3_vtab_collation,
124377  /* Version 3.24.0 and later */
124378  sqlite3_keyword_count,
124379  sqlite3_keyword_name,
124380  sqlite3_keyword_check,
124381  sqlite3_str_new,
124382  sqlite3_str_finish,
124383  sqlite3_str_appendf,
124384  sqlite3_str_vappendf,
124385  sqlite3_str_append,
124386  sqlite3_str_appendall,
124387  sqlite3_str_appendchar,
124388  sqlite3_str_reset,
124389  sqlite3_str_errcode,
124390  sqlite3_str_length,
124391  sqlite3_str_value,
124392  /* Version 3.25.0 and later */
124393  sqlite3_create_window_function,
124394  /* Version 3.26.0 and later */
124395#ifdef SQLITE_ENABLE_NORMALIZE
124396  sqlite3_normalized_sql,
124397#else
124398  0,
124399#endif
124400  /* Version 3.28.0 and later */
124401  sqlite3_stmt_isexplain,
124402  sqlite3_value_frombind,
124403  /* Version 3.30.0 and later */
124404#ifndef SQLITE_OMIT_VIRTUALTABLE
124405  sqlite3_drop_modules,
124406#else
124407  0,
124408#endif
124409  /* Version 3.31.0 and later */
124410  sqlite3_hard_heap_limit64,
124411  sqlite3_uri_key,
124412  sqlite3_filename_database,
124413  sqlite3_filename_journal,
124414  sqlite3_filename_wal,
124415  /* Version 3.32.0 and later */
124416  sqlite3_create_filename,
124417  sqlite3_free_filename,
124418  sqlite3_database_file_object,
124419};
124420
124421/* True if x is the directory separator character
124422*/
124423#if SQLITE_OS_WIN
124424# define DirSep(X)  ((X)=='/'||(X)=='\\')
124425#else
124426# define DirSep(X)  ((X)=='/')
124427#endif
124428
124429/*
124430** Attempt to load an SQLite extension library contained in the file
124431** zFile.  The entry point is zProc.  zProc may be 0 in which case a
124432** default entry point name (sqlite3_extension_init) is used.  Use
124433** of the default name is recommended.
124434**
124435** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
124436**
124437** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
124438** error message text.  The calling function should free this memory
124439** by calling sqlite3DbFree(db, ).
124440*/
124441static int sqlite3LoadExtension(
124442  sqlite3 *db,          /* Load the extension into this database connection */
124443  const char *zFile,    /* Name of the shared library containing extension */
124444  const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
124445  char **pzErrMsg       /* Put error message here if not 0 */
124446){
124447  sqlite3_vfs *pVfs = db->pVfs;
124448  void *handle;
124449  sqlite3_loadext_entry xInit;
124450  char *zErrmsg = 0;
124451  const char *zEntry;
124452  char *zAltEntry = 0;
124453  void **aHandle;
124454  u64 nMsg = 300 + sqlite3Strlen30(zFile);
124455  int ii;
124456  int rc;
124457
124458  /* Shared library endings to try if zFile cannot be loaded as written */
124459  static const char *azEndings[] = {
124460#if SQLITE_OS_WIN
124461     "dll"
124462#elif defined(__APPLE__)
124463     "dylib"
124464#else
124465     "so"
124466#endif
124467  };
124468
124469
124470  if( pzErrMsg ) *pzErrMsg = 0;
124471
124472  /* Ticket #1863.  To avoid a creating security problems for older
124473  ** applications that relink against newer versions of SQLite, the
124474  ** ability to run load_extension is turned off by default.  One
124475  ** must call either sqlite3_enable_load_extension(db) or
124476  ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0)
124477  ** to turn on extension loading.
124478  */
124479  if( (db->flags & SQLITE_LoadExtension)==0 ){
124480    if( pzErrMsg ){
124481      *pzErrMsg = sqlite3_mprintf("not authorized");
124482    }
124483    return SQLITE_ERROR;
124484  }
124485
124486  zEntry = zProc ? zProc : "sqlite3_extension_init";
124487
124488  handle = sqlite3OsDlOpen(pVfs, zFile);
124489#if SQLITE_OS_UNIX || SQLITE_OS_WIN
124490  for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
124491    char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
124492    if( zAltFile==0 ) return SQLITE_NOMEM_BKPT;
124493    handle = sqlite3OsDlOpen(pVfs, zAltFile);
124494    sqlite3_free(zAltFile);
124495  }
124496#endif
124497  if( handle==0 ){
124498    if( pzErrMsg ){
124499      *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
124500      if( zErrmsg ){
124501        sqlite3_snprintf(nMsg, zErrmsg,
124502            "unable to open shared library [%s]", zFile);
124503        sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
124504      }
124505    }
124506    return SQLITE_ERROR;
124507  }
124508  xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
124509
124510  /* If no entry point was specified and the default legacy
124511  ** entry point name "sqlite3_extension_init" was not found, then
124512  ** construct an entry point name "sqlite3_X_init" where the X is
124513  ** replaced by the lowercase value of every ASCII alphabetic
124514  ** character in the filename after the last "/" upto the first ".",
124515  ** and eliding the first three characters if they are "lib".
124516  ** Examples:
124517  **
124518  **    /usr/local/lib/libExample5.4.3.so ==>  sqlite3_example_init
124519  **    C:/lib/mathfuncs.dll              ==>  sqlite3_mathfuncs_init
124520  */
124521  if( xInit==0 && zProc==0 ){
124522    int iFile, iEntry, c;
124523    int ncFile = sqlite3Strlen30(zFile);
124524    zAltEntry = sqlite3_malloc64(ncFile+30);
124525    if( zAltEntry==0 ){
124526      sqlite3OsDlClose(pVfs, handle);
124527      return SQLITE_NOMEM_BKPT;
124528    }
124529    memcpy(zAltEntry, "sqlite3_", 8);
124530    for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
124531    iFile++;
124532    if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
124533    for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
124534      if( sqlite3Isalpha(c) ){
124535        zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
124536      }
124537    }
124538    memcpy(zAltEntry+iEntry, "_init", 6);
124539    zEntry = zAltEntry;
124540    xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
124541  }
124542  if( xInit==0 ){
124543    if( pzErrMsg ){
124544      nMsg += sqlite3Strlen30(zEntry);
124545      *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
124546      if( zErrmsg ){
124547        sqlite3_snprintf(nMsg, zErrmsg,
124548            "no entry point [%s] in shared library [%s]", zEntry, zFile);
124549        sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
124550      }
124551    }
124552    sqlite3OsDlClose(pVfs, handle);
124553    sqlite3_free(zAltEntry);
124554    return SQLITE_ERROR;
124555  }
124556  sqlite3_free(zAltEntry);
124557  rc = xInit(db, &zErrmsg, &sqlite3Apis);
124558  if( rc ){
124559    if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK;
124560    if( pzErrMsg ){
124561      *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
124562    }
124563    sqlite3_free(zErrmsg);
124564    sqlite3OsDlClose(pVfs, handle);
124565    return SQLITE_ERROR;
124566  }
124567
124568  /* Append the new shared library handle to the db->aExtension array. */
124569  aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
124570  if( aHandle==0 ){
124571    return SQLITE_NOMEM_BKPT;
124572  }
124573  if( db->nExtension>0 ){
124574    memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
124575  }
124576  sqlite3DbFree(db, db->aExtension);
124577  db->aExtension = aHandle;
124578
124579  db->aExtension[db->nExtension++] = handle;
124580  return SQLITE_OK;
124581}
124582SQLITE_API int sqlite3_load_extension(
124583  sqlite3 *db,          /* Load the extension into this database connection */
124584  const char *zFile,    /* Name of the shared library containing extension */
124585  const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
124586  char **pzErrMsg       /* Put error message here if not 0 */
124587){
124588  int rc;
124589  sqlite3_mutex_enter(db->mutex);
124590  rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
124591  rc = sqlite3ApiExit(db, rc);
124592  sqlite3_mutex_leave(db->mutex);
124593  return rc;
124594}
124595
124596/*
124597** Call this routine when the database connection is closing in order
124598** to clean up loaded extensions
124599*/
124600SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
124601  int i;
124602  assert( sqlite3_mutex_held(db->mutex) );
124603  for(i=0; i<db->nExtension; i++){
124604    sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
124605  }
124606  sqlite3DbFree(db, db->aExtension);
124607}
124608
124609/*
124610** Enable or disable extension loading.  Extension loading is disabled by
124611** default so as not to open security holes in older applications.
124612*/
124613SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
124614  sqlite3_mutex_enter(db->mutex);
124615  if( onoff ){
124616    db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
124617  }else{
124618    db->flags &= ~(u64)(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
124619  }
124620  sqlite3_mutex_leave(db->mutex);
124621  return SQLITE_OK;
124622}
124623
124624#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
124625
124626/*
124627** The following object holds the list of automatically loaded
124628** extensions.
124629**
124630** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
124631** mutex must be held while accessing this list.
124632*/
124633typedef struct sqlite3AutoExtList sqlite3AutoExtList;
124634static SQLITE_WSD struct sqlite3AutoExtList {
124635  u32 nExt;              /* Number of entries in aExt[] */
124636  void (**aExt)(void);   /* Pointers to the extension init functions */
124637} sqlite3Autoext = { 0, 0 };
124638
124639/* The "wsdAutoext" macro will resolve to the autoextension
124640** state vector.  If writable static data is unsupported on the target,
124641** we have to locate the state vector at run-time.  In the more common
124642** case where writable static data is supported, wsdStat can refer directly
124643** to the "sqlite3Autoext" state vector declared above.
124644*/
124645#ifdef SQLITE_OMIT_WSD
124646# define wsdAutoextInit \
124647  sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
124648# define wsdAutoext x[0]
124649#else
124650# define wsdAutoextInit
124651# define wsdAutoext sqlite3Autoext
124652#endif
124653
124654
124655/*
124656** Register a statically linked extension that is automatically
124657** loaded by every new database connection.
124658*/
124659SQLITE_API int sqlite3_auto_extension(
124660  void (*xInit)(void)
124661){
124662  int rc = SQLITE_OK;
124663#ifndef SQLITE_OMIT_AUTOINIT
124664  rc = sqlite3_initialize();
124665  if( rc ){
124666    return rc;
124667  }else
124668#endif
124669  {
124670    u32 i;
124671#if SQLITE_THREADSAFE
124672    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
124673#endif
124674    wsdAutoextInit;
124675    sqlite3_mutex_enter(mutex);
124676    for(i=0; i<wsdAutoext.nExt; i++){
124677      if( wsdAutoext.aExt[i]==xInit ) break;
124678    }
124679    if( i==wsdAutoext.nExt ){
124680      u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
124681      void (**aNew)(void);
124682      aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte);
124683      if( aNew==0 ){
124684        rc = SQLITE_NOMEM_BKPT;
124685      }else{
124686        wsdAutoext.aExt = aNew;
124687        wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
124688        wsdAutoext.nExt++;
124689      }
124690    }
124691    sqlite3_mutex_leave(mutex);
124692    assert( (rc&0xff)==rc );
124693    return rc;
124694  }
124695}
124696
124697/*
124698** Cancel a prior call to sqlite3_auto_extension.  Remove xInit from the
124699** set of routines that is invoked for each new database connection, if it
124700** is currently on the list.  If xInit is not on the list, then this
124701** routine is a no-op.
124702**
124703** Return 1 if xInit was found on the list and removed.  Return 0 if xInit
124704** was not on the list.
124705*/
124706SQLITE_API int sqlite3_cancel_auto_extension(
124707  void (*xInit)(void)
124708){
124709#if SQLITE_THREADSAFE
124710  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
124711#endif
124712  int i;
124713  int n = 0;
124714  wsdAutoextInit;
124715  sqlite3_mutex_enter(mutex);
124716  for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
124717    if( wsdAutoext.aExt[i]==xInit ){
124718      wsdAutoext.nExt--;
124719      wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
124720      n++;
124721      break;
124722    }
124723  }
124724  sqlite3_mutex_leave(mutex);
124725  return n;
124726}
124727
124728/*
124729** Reset the automatic extension loading mechanism.
124730*/
124731SQLITE_API void sqlite3_reset_auto_extension(void){
124732#ifndef SQLITE_OMIT_AUTOINIT
124733  if( sqlite3_initialize()==SQLITE_OK )
124734#endif
124735  {
124736#if SQLITE_THREADSAFE
124737    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
124738#endif
124739    wsdAutoextInit;
124740    sqlite3_mutex_enter(mutex);
124741    sqlite3_free(wsdAutoext.aExt);
124742    wsdAutoext.aExt = 0;
124743    wsdAutoext.nExt = 0;
124744    sqlite3_mutex_leave(mutex);
124745  }
124746}
124747
124748/*
124749** Load all automatic extensions.
124750**
124751** If anything goes wrong, set an error in the database connection.
124752*/
124753SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
124754  u32 i;
124755  int go = 1;
124756  int rc;
124757  sqlite3_loadext_entry xInit;
124758
124759  wsdAutoextInit;
124760  if( wsdAutoext.nExt==0 ){
124761    /* Common case: early out without every having to acquire a mutex */
124762    return;
124763  }
124764  for(i=0; go; i++){
124765    char *zErrmsg;
124766#if SQLITE_THREADSAFE
124767    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
124768#endif
124769#ifdef SQLITE_OMIT_LOAD_EXTENSION
124770    const sqlite3_api_routines *pThunk = 0;
124771#else
124772    const sqlite3_api_routines *pThunk = &sqlite3Apis;
124773#endif
124774    sqlite3_mutex_enter(mutex);
124775    if( i>=wsdAutoext.nExt ){
124776      xInit = 0;
124777      go = 0;
124778    }else{
124779      xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i];
124780    }
124781    sqlite3_mutex_leave(mutex);
124782    zErrmsg = 0;
124783    if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){
124784      sqlite3ErrorWithMsg(db, rc,
124785            "automatic extension loading failed: %s", zErrmsg);
124786      go = 0;
124787    }
124788    sqlite3_free(zErrmsg);
124789  }
124790}
124791
124792/************** End of loadext.c *********************************************/
124793/************** Begin file pragma.c ******************************************/
124794/*
124795** 2003 April 6
124796**
124797** The author disclaims copyright to this source code.  In place of
124798** a legal notice, here is a blessing:
124799**
124800**    May you do good and not evil.
124801**    May you find forgiveness for yourself and forgive others.
124802**    May you share freely, never taking more than you give.
124803**
124804*************************************************************************
124805** This file contains code used to implement the PRAGMA command.
124806*/
124807/* #include "sqliteInt.h" */
124808
124809#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
124810#  if defined(__APPLE__)
124811#    define SQLITE_ENABLE_LOCKING_STYLE 1
124812#  else
124813#    define SQLITE_ENABLE_LOCKING_STYLE 0
124814#  endif
124815#endif
124816
124817/***************************************************************************
124818** The "pragma.h" include file is an automatically generated file that
124819** that includes the PragType_XXXX macro definitions and the aPragmaName[]
124820** object.  This ensures that the aPragmaName[] table is arranged in
124821** lexicographical order to facility a binary search of the pragma name.
124822** Do not edit pragma.h directly.  Edit and rerun the script in at
124823** ../tool/mkpragmatab.tcl. */
124824/************** Include pragma.h in the middle of pragma.c *******************/
124825/************** Begin file pragma.h ******************************************/
124826/* DO NOT EDIT!
124827** This file is automatically generated by the script at
124828** ../tool/mkpragmatab.tcl.  To update the set of pragmas, edit
124829** that script and rerun it.
124830*/
124831
124832/* The various pragma types */
124833#define PragTyp_ACTIVATE_EXTENSIONS            0
124834#define PragTyp_ANALYSIS_LIMIT                 1
124835#define PragTyp_HEADER_VALUE                   2
124836#define PragTyp_AUTO_VACUUM                    3
124837#define PragTyp_FLAG                           4
124838#define PragTyp_BUSY_TIMEOUT                   5
124839#define PragTyp_CACHE_SIZE                     6
124840#define PragTyp_CACHE_SPILL                    7
124841#define PragTyp_CASE_SENSITIVE_LIKE            8
124842#define PragTyp_COLLATION_LIST                 9
124843#define PragTyp_COMPILE_OPTIONS               10
124844#define PragTyp_DATA_STORE_DIRECTORY          11
124845#define PragTyp_DATABASE_LIST                 12
124846#define PragTyp_DEFAULT_CACHE_SIZE            13
124847#define PragTyp_ENCODING                      14
124848#define PragTyp_FOREIGN_KEY_CHECK             15
124849#define PragTyp_FOREIGN_KEY_LIST              16
124850#define PragTyp_FUNCTION_LIST                 17
124851#define PragTyp_HARD_HEAP_LIMIT               18
124852#define PragTyp_INCREMENTAL_VACUUM            19
124853#define PragTyp_INDEX_INFO                    20
124854#define PragTyp_INDEX_LIST                    21
124855#define PragTyp_INTEGRITY_CHECK               22
124856#define PragTyp_JOURNAL_MODE                  23
124857#define PragTyp_JOURNAL_SIZE_LIMIT            24
124858#define PragTyp_LOCK_PROXY_FILE               25
124859#define PragTyp_LOCKING_MODE                  26
124860#define PragTyp_PAGE_COUNT                    27
124861#define PragTyp_MMAP_SIZE                     28
124862#define PragTyp_MODULE_LIST                   29
124863#define PragTyp_OPTIMIZE                      30
124864#define PragTyp_PAGE_SIZE                     31
124865#define PragTyp_PRAGMA_LIST                   32
124866#define PragTyp_SECURE_DELETE                 33
124867#define PragTyp_SHRINK_MEMORY                 34
124868#define PragTyp_SOFT_HEAP_LIMIT               35
124869#define PragTyp_SYNCHRONOUS                   36
124870#define PragTyp_TABLE_INFO                    37
124871#define PragTyp_TEMP_STORE                    38
124872#define PragTyp_TEMP_STORE_DIRECTORY          39
124873#define PragTyp_THREADS                       40
124874#define PragTyp_WAL_AUTOCHECKPOINT            41
124875#define PragTyp_WAL_CHECKPOINT                42
124876#define PragTyp_LOCK_STATUS                   43
124877#define PragTyp_STATS                         44
124878
124879/* Property flags associated with various pragma. */
124880#define PragFlg_NeedSchema 0x01 /* Force schema load before running */
124881#define PragFlg_NoColumns  0x02 /* OP_ResultRow called with zero columns */
124882#define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */
124883#define PragFlg_ReadOnly   0x08 /* Read-only HEADER_VALUE */
124884#define PragFlg_Result0    0x10 /* Acts as query when no argument */
124885#define PragFlg_Result1    0x20 /* Acts as query when has one argument */
124886#define PragFlg_SchemaOpt  0x40 /* Schema restricts name search if present */
124887#define PragFlg_SchemaReq  0x80 /* Schema required - "main" is default */
124888
124889/* Names of columns for pragmas that return multi-column result
124890** or that return single-column results where the name of the
124891** result column is different from the name of the pragma
124892*/
124893static const char *const pragCName[] = {
124894  /*   0 */ "id",          /* Used by: foreign_key_list */
124895  /*   1 */ "seq",
124896  /*   2 */ "table",
124897  /*   3 */ "from",
124898  /*   4 */ "to",
124899  /*   5 */ "on_update",
124900  /*   6 */ "on_delete",
124901  /*   7 */ "match",
124902  /*   8 */ "cid",         /* Used by: table_xinfo */
124903  /*   9 */ "name",
124904  /*  10 */ "type",
124905  /*  11 */ "notnull",
124906  /*  12 */ "dflt_value",
124907  /*  13 */ "pk",
124908  /*  14 */ "hidden",
124909                           /* table_info reuses 8 */
124910  /*  15 */ "seqno",       /* Used by: index_xinfo */
124911  /*  16 */ "cid",
124912  /*  17 */ "name",
124913  /*  18 */ "desc",
124914  /*  19 */ "coll",
124915  /*  20 */ "key",
124916  /*  21 */ "name",        /* Used by: function_list */
124917  /*  22 */ "builtin",
124918  /*  23 */ "type",
124919  /*  24 */ "enc",
124920  /*  25 */ "narg",
124921  /*  26 */ "flags",
124922  /*  27 */ "tbl",         /* Used by: stats */
124923  /*  28 */ "idx",
124924  /*  29 */ "wdth",
124925  /*  30 */ "hght",
124926  /*  31 */ "flgs",
124927  /*  32 */ "seq",         /* Used by: index_list */
124928  /*  33 */ "name",
124929  /*  34 */ "unique",
124930  /*  35 */ "origin",
124931  /*  36 */ "partial",
124932  /*  37 */ "table",       /* Used by: foreign_key_check */
124933  /*  38 */ "rowid",
124934  /*  39 */ "parent",
124935  /*  40 */ "fkid",
124936                           /* index_info reuses 15 */
124937  /*  41 */ "seq",         /* Used by: database_list */
124938  /*  42 */ "name",
124939  /*  43 */ "file",
124940  /*  44 */ "busy",        /* Used by: wal_checkpoint */
124941  /*  45 */ "log",
124942  /*  46 */ "checkpointed",
124943                           /* collation_list reuses 32 */
124944  /*  47 */ "database",    /* Used by: lock_status */
124945  /*  48 */ "status",
124946  /*  49 */ "cache_size",  /* Used by: default_cache_size */
124947                           /* module_list pragma_list reuses 9 */
124948  /*  50 */ "timeout",     /* Used by: busy_timeout */
124949};
124950
124951/* Definitions of all built-in pragmas */
124952typedef struct PragmaName {
124953  const char *const zName; /* Name of pragma */
124954  u8 ePragTyp;             /* PragTyp_XXX value */
124955  u8 mPragFlg;             /* Zero or more PragFlg_XXX values */
124956  u8 iPragCName;           /* Start of column names in pragCName[] */
124957  u8 nPragCName;           /* Num of col names. 0 means use pragma name */
124958  u64 iArg;                /* Extra argument */
124959} PragmaName;
124960static const PragmaName aPragmaName[] = {
124961#if defined(SQLITE_ENABLE_CEROD)
124962 {/* zName:     */ "activate_extensions",
124963  /* ePragTyp:  */ PragTyp_ACTIVATE_EXTENSIONS,
124964  /* ePragFlg:  */ 0,
124965  /* ColNames:  */ 0, 0,
124966  /* iArg:      */ 0 },
124967#endif
124968 {/* zName:     */ "analysis_limit",
124969  /* ePragTyp:  */ PragTyp_ANALYSIS_LIMIT,
124970  /* ePragFlg:  */ PragFlg_Result0,
124971  /* ColNames:  */ 0, 0,
124972  /* iArg:      */ 0 },
124973#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
124974 {/* zName:     */ "application_id",
124975  /* ePragTyp:  */ PragTyp_HEADER_VALUE,
124976  /* ePragFlg:  */ PragFlg_NoColumns1|PragFlg_Result0,
124977  /* ColNames:  */ 0, 0,
124978  /* iArg:      */ BTREE_APPLICATION_ID },
124979#endif
124980#if !defined(SQLITE_OMIT_AUTOVACUUM)
124981 {/* zName:     */ "auto_vacuum",
124982  /* ePragTyp:  */ PragTyp_AUTO_VACUUM,
124983  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
124984  /* ColNames:  */ 0, 0,
124985  /* iArg:      */ 0 },
124986#endif
124987#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
124988#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
124989 {/* zName:     */ "automatic_index",
124990  /* ePragTyp:  */ PragTyp_FLAG,
124991  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
124992  /* ColNames:  */ 0, 0,
124993  /* iArg:      */ SQLITE_AutoIndex },
124994#endif
124995#endif
124996 {/* zName:     */ "busy_timeout",
124997  /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
124998  /* ePragFlg:  */ PragFlg_Result0,
124999  /* ColNames:  */ 50, 1,
125000  /* iArg:      */ 0 },
125001#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
125002 {/* zName:     */ "cache_size",
125003  /* ePragTyp:  */ PragTyp_CACHE_SIZE,
125004  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
125005  /* ColNames:  */ 0, 0,
125006  /* iArg:      */ 0 },
125007#endif
125008#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125009 {/* zName:     */ "cache_spill",
125010  /* ePragTyp:  */ PragTyp_CACHE_SPILL,
125011  /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
125012  /* ColNames:  */ 0, 0,
125013  /* iArg:      */ 0 },
125014#endif
125015#if !defined(SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA)
125016 {/* zName:     */ "case_sensitive_like",
125017  /* ePragTyp:  */ PragTyp_CASE_SENSITIVE_LIKE,
125018  /* ePragFlg:  */ PragFlg_NoColumns,
125019  /* ColNames:  */ 0, 0,
125020  /* iArg:      */ 0 },
125021#endif
125022 {/* zName:     */ "cell_size_check",
125023  /* ePragTyp:  */ PragTyp_FLAG,
125024  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125025  /* ColNames:  */ 0, 0,
125026  /* iArg:      */ SQLITE_CellSizeCk },
125027#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125028 {/* zName:     */ "checkpoint_fullfsync",
125029  /* ePragTyp:  */ PragTyp_FLAG,
125030  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125031  /* ColNames:  */ 0, 0,
125032  /* iArg:      */ SQLITE_CkptFullFSync },
125033#endif
125034#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
125035 {/* zName:     */ "collation_list",
125036  /* ePragTyp:  */ PragTyp_COLLATION_LIST,
125037  /* ePragFlg:  */ PragFlg_Result0,
125038  /* ColNames:  */ 32, 2,
125039  /* iArg:      */ 0 },
125040#endif
125041#if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
125042 {/* zName:     */ "compile_options",
125043  /* ePragTyp:  */ PragTyp_COMPILE_OPTIONS,
125044  /* ePragFlg:  */ PragFlg_Result0,
125045  /* ColNames:  */ 0, 0,
125046  /* iArg:      */ 0 },
125047#endif
125048#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125049 {/* zName:     */ "count_changes",
125050  /* ePragTyp:  */ PragTyp_FLAG,
125051  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125052  /* ColNames:  */ 0, 0,
125053  /* iArg:      */ SQLITE_CountRows },
125054#endif
125055#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
125056 {/* zName:     */ "data_store_directory",
125057  /* ePragTyp:  */ PragTyp_DATA_STORE_DIRECTORY,
125058  /* ePragFlg:  */ PragFlg_NoColumns1,
125059  /* ColNames:  */ 0, 0,
125060  /* iArg:      */ 0 },
125061#endif
125062#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
125063 {/* zName:     */ "data_version",
125064  /* ePragTyp:  */ PragTyp_HEADER_VALUE,
125065  /* ePragFlg:  */ PragFlg_ReadOnly|PragFlg_Result0,
125066  /* ColNames:  */ 0, 0,
125067  /* iArg:      */ BTREE_DATA_VERSION },
125068#endif
125069#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
125070 {/* zName:     */ "database_list",
125071  /* ePragTyp:  */ PragTyp_DATABASE_LIST,
125072  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0,
125073  /* ColNames:  */ 41, 3,
125074  /* iArg:      */ 0 },
125075#endif
125076#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
125077 {/* zName:     */ "default_cache_size",
125078  /* ePragTyp:  */ PragTyp_DEFAULT_CACHE_SIZE,
125079  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
125080  /* ColNames:  */ 49, 1,
125081  /* iArg:      */ 0 },
125082#endif
125083#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125084#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
125085 {/* zName:     */ "defer_foreign_keys",
125086  /* ePragTyp:  */ PragTyp_FLAG,
125087  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125088  /* ColNames:  */ 0, 0,
125089  /* iArg:      */ SQLITE_DeferFKs },
125090#endif
125091#endif
125092#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125093 {/* zName:     */ "empty_result_callbacks",
125094  /* ePragTyp:  */ PragTyp_FLAG,
125095  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125096  /* ColNames:  */ 0, 0,
125097  /* iArg:      */ SQLITE_NullCallback },
125098#endif
125099#if !defined(SQLITE_OMIT_UTF16)
125100 {/* zName:     */ "encoding",
125101  /* ePragTyp:  */ PragTyp_ENCODING,
125102  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125103  /* ColNames:  */ 0, 0,
125104  /* iArg:      */ 0 },
125105#endif
125106#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
125107 {/* zName:     */ "foreign_key_check",
125108  /* ePragTyp:  */ PragTyp_FOREIGN_KEY_CHECK,
125109  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0,
125110  /* ColNames:  */ 37, 4,
125111  /* iArg:      */ 0 },
125112#endif
125113#if !defined(SQLITE_OMIT_FOREIGN_KEY)
125114 {/* zName:     */ "foreign_key_list",
125115  /* ePragTyp:  */ PragTyp_FOREIGN_KEY_LIST,
125116  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
125117  /* ColNames:  */ 0, 8,
125118  /* iArg:      */ 0 },
125119#endif
125120#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125121#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
125122 {/* zName:     */ "foreign_keys",
125123  /* ePragTyp:  */ PragTyp_FLAG,
125124  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125125  /* ColNames:  */ 0, 0,
125126  /* iArg:      */ SQLITE_ForeignKeys },
125127#endif
125128#endif
125129#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
125130 {/* zName:     */ "freelist_count",
125131  /* ePragTyp:  */ PragTyp_HEADER_VALUE,
125132  /* ePragFlg:  */ PragFlg_ReadOnly|PragFlg_Result0,
125133  /* ColNames:  */ 0, 0,
125134  /* iArg:      */ BTREE_FREE_PAGE_COUNT },
125135#endif
125136#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125137 {/* zName:     */ "full_column_names",
125138  /* ePragTyp:  */ PragTyp_FLAG,
125139  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125140  /* ColNames:  */ 0, 0,
125141  /* iArg:      */ SQLITE_FullColNames },
125142 {/* zName:     */ "fullfsync",
125143  /* ePragTyp:  */ PragTyp_FLAG,
125144  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125145  /* ColNames:  */ 0, 0,
125146  /* iArg:      */ SQLITE_FullFSync },
125147#endif
125148#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
125149#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
125150 {/* zName:     */ "function_list",
125151  /* ePragTyp:  */ PragTyp_FUNCTION_LIST,
125152  /* ePragFlg:  */ PragFlg_Result0,
125153  /* ColNames:  */ 21, 6,
125154  /* iArg:      */ 0 },
125155#endif
125156#endif
125157 {/* zName:     */ "hard_heap_limit",
125158  /* ePragTyp:  */ PragTyp_HARD_HEAP_LIMIT,
125159  /* ePragFlg:  */ PragFlg_Result0,
125160  /* ColNames:  */ 0, 0,
125161  /* iArg:      */ 0 },
125162#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125163#if !defined(SQLITE_OMIT_CHECK)
125164 {/* zName:     */ "ignore_check_constraints",
125165  /* ePragTyp:  */ PragTyp_FLAG,
125166  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125167  /* ColNames:  */ 0, 0,
125168  /* iArg:      */ SQLITE_IgnoreChecks },
125169#endif
125170#endif
125171#if !defined(SQLITE_OMIT_AUTOVACUUM)
125172 {/* zName:     */ "incremental_vacuum",
125173  /* ePragTyp:  */ PragTyp_INCREMENTAL_VACUUM,
125174  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_NoColumns,
125175  /* ColNames:  */ 0, 0,
125176  /* iArg:      */ 0 },
125177#endif
125178#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
125179 {/* zName:     */ "index_info",
125180  /* ePragTyp:  */ PragTyp_INDEX_INFO,
125181  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
125182  /* ColNames:  */ 15, 3,
125183  /* iArg:      */ 0 },
125184 {/* zName:     */ "index_list",
125185  /* ePragTyp:  */ PragTyp_INDEX_LIST,
125186  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
125187  /* ColNames:  */ 32, 5,
125188  /* iArg:      */ 0 },
125189 {/* zName:     */ "index_xinfo",
125190  /* ePragTyp:  */ PragTyp_INDEX_INFO,
125191  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
125192  /* ColNames:  */ 15, 6,
125193  /* iArg:      */ 1 },
125194#endif
125195#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
125196 {/* zName:     */ "integrity_check",
125197  /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
125198  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1,
125199  /* ColNames:  */ 0, 0,
125200  /* iArg:      */ 0 },
125201#endif
125202#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
125203 {/* zName:     */ "journal_mode",
125204  /* ePragTyp:  */ PragTyp_JOURNAL_MODE,
125205  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
125206  /* ColNames:  */ 0, 0,
125207  /* iArg:      */ 0 },
125208 {/* zName:     */ "journal_size_limit",
125209  /* ePragTyp:  */ PragTyp_JOURNAL_SIZE_LIMIT,
125210  /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq,
125211  /* ColNames:  */ 0, 0,
125212  /* iArg:      */ 0 },
125213#endif
125214#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125215 {/* zName:     */ "legacy_alter_table",
125216  /* ePragTyp:  */ PragTyp_FLAG,
125217  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125218  /* ColNames:  */ 0, 0,
125219  /* iArg:      */ SQLITE_LegacyAlter },
125220#endif
125221#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
125222 {/* zName:     */ "lock_proxy_file",
125223  /* ePragTyp:  */ PragTyp_LOCK_PROXY_FILE,
125224  /* ePragFlg:  */ PragFlg_NoColumns1,
125225  /* ColNames:  */ 0, 0,
125226  /* iArg:      */ 0 },
125227#endif
125228#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
125229 {/* zName:     */ "lock_status",
125230  /* ePragTyp:  */ PragTyp_LOCK_STATUS,
125231  /* ePragFlg:  */ PragFlg_Result0,
125232  /* ColNames:  */ 47, 2,
125233  /* iArg:      */ 0 },
125234#endif
125235#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
125236 {/* zName:     */ "locking_mode",
125237  /* ePragTyp:  */ PragTyp_LOCKING_MODE,
125238  /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq,
125239  /* ColNames:  */ 0, 0,
125240  /* iArg:      */ 0 },
125241 {/* zName:     */ "max_page_count",
125242  /* ePragTyp:  */ PragTyp_PAGE_COUNT,
125243  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
125244  /* ColNames:  */ 0, 0,
125245  /* iArg:      */ 0 },
125246 {/* zName:     */ "mmap_size",
125247  /* ePragTyp:  */ PragTyp_MMAP_SIZE,
125248  /* ePragFlg:  */ 0,
125249  /* ColNames:  */ 0, 0,
125250  /* iArg:      */ 0 },
125251#endif
125252#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
125253#if !defined(SQLITE_OMIT_VIRTUALTABLE)
125254#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
125255 {/* zName:     */ "module_list",
125256  /* ePragTyp:  */ PragTyp_MODULE_LIST,
125257  /* ePragFlg:  */ PragFlg_Result0,
125258  /* ColNames:  */ 9, 1,
125259  /* iArg:      */ 0 },
125260#endif
125261#endif
125262#endif
125263 {/* zName:     */ "optimize",
125264  /* ePragTyp:  */ PragTyp_OPTIMIZE,
125265  /* ePragFlg:  */ PragFlg_Result1|PragFlg_NeedSchema,
125266  /* ColNames:  */ 0, 0,
125267  /* iArg:      */ 0 },
125268#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
125269 {/* zName:     */ "page_count",
125270  /* ePragTyp:  */ PragTyp_PAGE_COUNT,
125271  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
125272  /* ColNames:  */ 0, 0,
125273  /* iArg:      */ 0 },
125274 {/* zName:     */ "page_size",
125275  /* ePragTyp:  */ PragTyp_PAGE_SIZE,
125276  /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
125277  /* ColNames:  */ 0, 0,
125278  /* iArg:      */ 0 },
125279#endif
125280#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125281#if defined(SQLITE_DEBUG)
125282 {/* zName:     */ "parser_trace",
125283  /* ePragTyp:  */ PragTyp_FLAG,
125284  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125285  /* ColNames:  */ 0, 0,
125286  /* iArg:      */ SQLITE_ParserTrace },
125287#endif
125288#endif
125289#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
125290 {/* zName:     */ "pragma_list",
125291  /* ePragTyp:  */ PragTyp_PRAGMA_LIST,
125292  /* ePragFlg:  */ PragFlg_Result0,
125293  /* ColNames:  */ 9, 1,
125294  /* iArg:      */ 0 },
125295#endif
125296#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125297 {/* zName:     */ "query_only",
125298  /* ePragTyp:  */ PragTyp_FLAG,
125299  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125300  /* ColNames:  */ 0, 0,
125301  /* iArg:      */ SQLITE_QueryOnly },
125302#endif
125303#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
125304 {/* zName:     */ "quick_check",
125305  /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
125306  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1,
125307  /* ColNames:  */ 0, 0,
125308  /* iArg:      */ 0 },
125309#endif
125310#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125311 {/* zName:     */ "read_uncommitted",
125312  /* ePragTyp:  */ PragTyp_FLAG,
125313  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125314  /* ColNames:  */ 0, 0,
125315  /* iArg:      */ SQLITE_ReadUncommit },
125316 {/* zName:     */ "recursive_triggers",
125317  /* ePragTyp:  */ PragTyp_FLAG,
125318  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125319  /* ColNames:  */ 0, 0,
125320  /* iArg:      */ SQLITE_RecTriggers },
125321 {/* zName:     */ "reverse_unordered_selects",
125322  /* ePragTyp:  */ PragTyp_FLAG,
125323  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125324  /* ColNames:  */ 0, 0,
125325  /* iArg:      */ SQLITE_ReverseOrder },
125326#endif
125327#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
125328 {/* zName:     */ "schema_version",
125329  /* ePragTyp:  */ PragTyp_HEADER_VALUE,
125330  /* ePragFlg:  */ PragFlg_NoColumns1|PragFlg_Result0,
125331  /* ColNames:  */ 0, 0,
125332  /* iArg:      */ BTREE_SCHEMA_VERSION },
125333#endif
125334#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
125335 {/* zName:     */ "secure_delete",
125336  /* ePragTyp:  */ PragTyp_SECURE_DELETE,
125337  /* ePragFlg:  */ PragFlg_Result0,
125338  /* ColNames:  */ 0, 0,
125339  /* iArg:      */ 0 },
125340#endif
125341#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125342 {/* zName:     */ "short_column_names",
125343  /* ePragTyp:  */ PragTyp_FLAG,
125344  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125345  /* ColNames:  */ 0, 0,
125346  /* iArg:      */ SQLITE_ShortColNames },
125347#endif
125348 {/* zName:     */ "shrink_memory",
125349  /* ePragTyp:  */ PragTyp_SHRINK_MEMORY,
125350  /* ePragFlg:  */ PragFlg_NoColumns,
125351  /* ColNames:  */ 0, 0,
125352  /* iArg:      */ 0 },
125353 {/* zName:     */ "soft_heap_limit",
125354  /* ePragTyp:  */ PragTyp_SOFT_HEAP_LIMIT,
125355  /* ePragFlg:  */ PragFlg_Result0,
125356  /* ColNames:  */ 0, 0,
125357  /* iArg:      */ 0 },
125358#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125359#if defined(SQLITE_DEBUG)
125360 {/* zName:     */ "sql_trace",
125361  /* ePragTyp:  */ PragTyp_FLAG,
125362  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125363  /* ColNames:  */ 0, 0,
125364  /* iArg:      */ SQLITE_SqlTrace },
125365#endif
125366#endif
125367#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
125368 {/* zName:     */ "stats",
125369  /* ePragTyp:  */ PragTyp_STATS,
125370  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
125371  /* ColNames:  */ 27, 5,
125372  /* iArg:      */ 0 },
125373#endif
125374#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
125375 {/* zName:     */ "synchronous",
125376  /* ePragTyp:  */ PragTyp_SYNCHRONOUS,
125377  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
125378  /* ColNames:  */ 0, 0,
125379  /* iArg:      */ 0 },
125380#endif
125381#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
125382 {/* zName:     */ "table_info",
125383  /* ePragTyp:  */ PragTyp_TABLE_INFO,
125384  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
125385  /* ColNames:  */ 8, 6,
125386  /* iArg:      */ 0 },
125387 {/* zName:     */ "table_xinfo",
125388  /* ePragTyp:  */ PragTyp_TABLE_INFO,
125389  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
125390  /* ColNames:  */ 8, 7,
125391  /* iArg:      */ 1 },
125392#endif
125393#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
125394 {/* zName:     */ "temp_store",
125395  /* ePragTyp:  */ PragTyp_TEMP_STORE,
125396  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125397  /* ColNames:  */ 0, 0,
125398  /* iArg:      */ 0 },
125399 {/* zName:     */ "temp_store_directory",
125400  /* ePragTyp:  */ PragTyp_TEMP_STORE_DIRECTORY,
125401  /* ePragFlg:  */ PragFlg_NoColumns1,
125402  /* ColNames:  */ 0, 0,
125403  /* iArg:      */ 0 },
125404#endif
125405 {/* zName:     */ "threads",
125406  /* ePragTyp:  */ PragTyp_THREADS,
125407  /* ePragFlg:  */ PragFlg_Result0,
125408  /* ColNames:  */ 0, 0,
125409  /* iArg:      */ 0 },
125410#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125411 {/* zName:     */ "trusted_schema",
125412  /* ePragTyp:  */ PragTyp_FLAG,
125413  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125414  /* ColNames:  */ 0, 0,
125415  /* iArg:      */ SQLITE_TrustedSchema },
125416#endif
125417#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
125418 {/* zName:     */ "user_version",
125419  /* ePragTyp:  */ PragTyp_HEADER_VALUE,
125420  /* ePragFlg:  */ PragFlg_NoColumns1|PragFlg_Result0,
125421  /* ColNames:  */ 0, 0,
125422  /* iArg:      */ BTREE_USER_VERSION },
125423#endif
125424#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125425#if defined(SQLITE_DEBUG)
125426 {/* zName:     */ "vdbe_addoptrace",
125427  /* ePragTyp:  */ PragTyp_FLAG,
125428  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125429  /* ColNames:  */ 0, 0,
125430  /* iArg:      */ SQLITE_VdbeAddopTrace },
125431 {/* zName:     */ "vdbe_debug",
125432  /* ePragTyp:  */ PragTyp_FLAG,
125433  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125434  /* ColNames:  */ 0, 0,
125435  /* iArg:      */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
125436 {/* zName:     */ "vdbe_eqp",
125437  /* ePragTyp:  */ PragTyp_FLAG,
125438  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125439  /* ColNames:  */ 0, 0,
125440  /* iArg:      */ SQLITE_VdbeEQP },
125441 {/* zName:     */ "vdbe_listing",
125442  /* ePragTyp:  */ PragTyp_FLAG,
125443  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125444  /* ColNames:  */ 0, 0,
125445  /* iArg:      */ SQLITE_VdbeListing },
125446 {/* zName:     */ "vdbe_trace",
125447  /* ePragTyp:  */ PragTyp_FLAG,
125448  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125449  /* ColNames:  */ 0, 0,
125450  /* iArg:      */ SQLITE_VdbeTrace },
125451#endif
125452#endif
125453#if !defined(SQLITE_OMIT_WAL)
125454 {/* zName:     */ "wal_autocheckpoint",
125455  /* ePragTyp:  */ PragTyp_WAL_AUTOCHECKPOINT,
125456  /* ePragFlg:  */ 0,
125457  /* ColNames:  */ 0, 0,
125458  /* iArg:      */ 0 },
125459 {/* zName:     */ "wal_checkpoint",
125460  /* ePragTyp:  */ PragTyp_WAL_CHECKPOINT,
125461  /* ePragFlg:  */ PragFlg_NeedSchema,
125462  /* ColNames:  */ 44, 3,
125463  /* iArg:      */ 0 },
125464#endif
125465#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125466 {/* zName:     */ "writable_schema",
125467  /* ePragTyp:  */ PragTyp_FLAG,
125468  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
125469  /* ColNames:  */ 0, 0,
125470  /* iArg:      */ SQLITE_WriteSchema|SQLITE_NoSchemaError },
125471#endif
125472};
125473/* Number of pragmas: 67 on by default, 77 total. */
125474
125475/************** End of pragma.h **********************************************/
125476/************** Continuing where we left off in pragma.c *********************/
125477
125478/*
125479** Interpret the given string as a safety level.  Return 0 for OFF,
125480** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA.  Return 1 for an empty or
125481** unrecognized string argument.  The FULL and EXTRA option is disallowed
125482** if the omitFull parameter it 1.
125483**
125484** Note that the values returned are one less that the values that
125485** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
125486** to support legacy SQL code.  The safety level used to be boolean
125487** and older scripts may have used numbers 0 for OFF and 1 for ON.
125488*/
125489static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
125490                             /* 123456789 123456789 123 */
125491  static const char zText[] = "onoffalseyestruextrafull";
125492  static const u8 iOffset[] = {0, 1, 2,  4,    9,  12,  15,   20};
125493  static const u8 iLength[] = {2, 2, 3,  5,    3,   4,   5,    4};
125494  static const u8 iValue[] =  {1, 0, 0,  0,    1,   1,   3,    2};
125495                            /* on no off false yes true extra full */
125496  int i, n;
125497  if( sqlite3Isdigit(*z) ){
125498    return (u8)sqlite3Atoi(z);
125499  }
125500  n = sqlite3Strlen30(z);
125501  for(i=0; i<ArraySize(iLength); i++){
125502    if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0
125503     && (!omitFull || iValue[i]<=1)
125504    ){
125505      return iValue[i];
125506    }
125507  }
125508  return dflt;
125509}
125510
125511/*
125512** Interpret the given string as a boolean value.
125513*/
125514SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, u8 dflt){
125515  return getSafetyLevel(z,1,dflt)!=0;
125516}
125517
125518/* The sqlite3GetBoolean() function is used by other modules but the
125519** remainder of this file is specific to PRAGMA processing.  So omit
125520** the rest of the file if PRAGMAs are omitted from the build.
125521*/
125522#if !defined(SQLITE_OMIT_PRAGMA)
125523
125524/*
125525** Interpret the given string as a locking mode value.
125526*/
125527static int getLockingMode(const char *z){
125528  if( z ){
125529    if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
125530    if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
125531  }
125532  return PAGER_LOCKINGMODE_QUERY;
125533}
125534
125535#ifndef SQLITE_OMIT_AUTOVACUUM
125536/*
125537** Interpret the given string as an auto-vacuum mode value.
125538**
125539** The following strings, "none", "full" and "incremental" are
125540** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
125541*/
125542static int getAutoVacuum(const char *z){
125543  int i;
125544  if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
125545  if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
125546  if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
125547  i = sqlite3Atoi(z);
125548  return (u8)((i>=0&&i<=2)?i:0);
125549}
125550#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
125551
125552#ifndef SQLITE_OMIT_PAGER_PRAGMAS
125553/*
125554** Interpret the given string as a temp db location. Return 1 for file
125555** backed temporary databases, 2 for the Red-Black tree in memory database
125556** and 0 to use the compile-time default.
125557*/
125558static int getTempStore(const char *z){
125559  if( z[0]>='0' && z[0]<='2' ){
125560    return z[0] - '0';
125561  }else if( sqlite3StrICmp(z, "file")==0 ){
125562    return 1;
125563  }else if( sqlite3StrICmp(z, "memory")==0 ){
125564    return 2;
125565  }else{
125566    return 0;
125567  }
125568}
125569#endif /* SQLITE_PAGER_PRAGMAS */
125570
125571#ifndef SQLITE_OMIT_PAGER_PRAGMAS
125572/*
125573** Invalidate temp storage, either when the temp storage is changed
125574** from default, or when 'file' and the temp_store_directory has changed
125575*/
125576static int invalidateTempStorage(Parse *pParse){
125577  sqlite3 *db = pParse->db;
125578  if( db->aDb[1].pBt!=0 ){
125579    if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
125580      sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
125581        "from within a transaction");
125582      return SQLITE_ERROR;
125583    }
125584    sqlite3BtreeClose(db->aDb[1].pBt);
125585    db->aDb[1].pBt = 0;
125586    sqlite3ResetAllSchemasOfConnection(db);
125587  }
125588  return SQLITE_OK;
125589}
125590#endif /* SQLITE_PAGER_PRAGMAS */
125591
125592#ifndef SQLITE_OMIT_PAGER_PRAGMAS
125593/*
125594** If the TEMP database is open, close it and mark the database schema
125595** as needing reloading.  This must be done when using the SQLITE_TEMP_STORE
125596** or DEFAULT_TEMP_STORE pragmas.
125597*/
125598static int changeTempStorage(Parse *pParse, const char *zStorageType){
125599  int ts = getTempStore(zStorageType);
125600  sqlite3 *db = pParse->db;
125601  if( db->temp_store==ts ) return SQLITE_OK;
125602  if( invalidateTempStorage( pParse ) != SQLITE_OK ){
125603    return SQLITE_ERROR;
125604  }
125605  db->temp_store = (u8)ts;
125606  return SQLITE_OK;
125607}
125608#endif /* SQLITE_PAGER_PRAGMAS */
125609
125610/*
125611** Set result column names for a pragma.
125612*/
125613static void setPragmaResultColumnNames(
125614  Vdbe *v,                     /* The query under construction */
125615  const PragmaName *pPragma    /* The pragma */
125616){
125617  u8 n = pPragma->nPragCName;
125618  sqlite3VdbeSetNumCols(v, n==0 ? 1 : n);
125619  if( n==0 ){
125620    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
125621  }else{
125622    int i, j;
125623    for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
125624      sqlite3VdbeSetColName(v, i, COLNAME_NAME, pragCName[j], SQLITE_STATIC);
125625    }
125626  }
125627}
125628
125629/*
125630** Generate code to return a single integer value.
125631*/
125632static void returnSingleInt(Vdbe *v, i64 value){
125633  sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64);
125634  sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
125635}
125636
125637/*
125638** Generate code to return a single text value.
125639*/
125640static void returnSingleText(
125641  Vdbe *v,                /* Prepared statement under construction */
125642  const char *zValue      /* Value to be returned */
125643){
125644  if( zValue ){
125645    sqlite3VdbeLoadString(v, 1, (const char*)zValue);
125646    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
125647  }
125648}
125649
125650
125651/*
125652** Set the safety_level and pager flags for pager iDb.  Or if iDb<0
125653** set these values for all pagers.
125654*/
125655#ifndef SQLITE_OMIT_PAGER_PRAGMAS
125656static void setAllPagerFlags(sqlite3 *db){
125657  if( db->autoCommit ){
125658    Db *pDb = db->aDb;
125659    int n = db->nDb;
125660    assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
125661    assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
125662    assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
125663    assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
125664             ==  PAGER_FLAGS_MASK );
125665    assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
125666    while( (n--) > 0 ){
125667      if( pDb->pBt ){
125668        sqlite3BtreeSetPagerFlags(pDb->pBt,
125669                 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
125670      }
125671      pDb++;
125672    }
125673  }
125674}
125675#else
125676# define setAllPagerFlags(X)  /* no-op */
125677#endif
125678
125679
125680/*
125681** Return a human-readable name for a constraint resolution action.
125682*/
125683#ifndef SQLITE_OMIT_FOREIGN_KEY
125684static const char *actionName(u8 action){
125685  const char *zName;
125686  switch( action ){
125687    case OE_SetNull:  zName = "SET NULL";        break;
125688    case OE_SetDflt:  zName = "SET DEFAULT";     break;
125689    case OE_Cascade:  zName = "CASCADE";         break;
125690    case OE_Restrict: zName = "RESTRICT";        break;
125691    default:          zName = "NO ACTION";
125692                      assert( action==OE_None ); break;
125693  }
125694  return zName;
125695}
125696#endif
125697
125698
125699/*
125700** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
125701** defined in pager.h. This function returns the associated lowercase
125702** journal-mode name.
125703*/
125704SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
125705  static char * const azModeName[] = {
125706    "delete", "persist", "off", "truncate", "memory"
125707#ifndef SQLITE_OMIT_WAL
125708     , "wal"
125709#endif
125710  };
125711  assert( PAGER_JOURNALMODE_DELETE==0 );
125712  assert( PAGER_JOURNALMODE_PERSIST==1 );
125713  assert( PAGER_JOURNALMODE_OFF==2 );
125714  assert( PAGER_JOURNALMODE_TRUNCATE==3 );
125715  assert( PAGER_JOURNALMODE_MEMORY==4 );
125716  assert( PAGER_JOURNALMODE_WAL==5 );
125717  assert( eMode>=0 && eMode<=ArraySize(azModeName) );
125718
125719  if( eMode==ArraySize(azModeName) ) return 0;
125720  return azModeName[eMode];
125721}
125722
125723/*
125724** Locate a pragma in the aPragmaName[] array.
125725*/
125726static const PragmaName *pragmaLocate(const char *zName){
125727  int upr, lwr, mid = 0, rc;
125728  lwr = 0;
125729  upr = ArraySize(aPragmaName)-1;
125730  while( lwr<=upr ){
125731    mid = (lwr+upr)/2;
125732    rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
125733    if( rc==0 ) break;
125734    if( rc<0 ){
125735      upr = mid - 1;
125736    }else{
125737      lwr = mid + 1;
125738    }
125739  }
125740  return lwr>upr ? 0 : &aPragmaName[mid];
125741}
125742
125743/*
125744** Create zero or more entries in the output for the SQL functions
125745** defined by FuncDef p.
125746*/
125747static void pragmaFunclistLine(
125748  Vdbe *v,               /* The prepared statement being created */
125749  FuncDef *p,            /* A particular function definition */
125750  int isBuiltin,         /* True if this is a built-in function */
125751  int showInternFuncs    /* True if showing internal functions */
125752){
125753  for(; p; p=p->pNext){
125754    const char *zType;
125755    static const u32 mask =
125756        SQLITE_DETERMINISTIC |
125757        SQLITE_DIRECTONLY |
125758        SQLITE_SUBTYPE |
125759        SQLITE_INNOCUOUS |
125760        SQLITE_FUNC_INTERNAL
125761    ;
125762    static const char *azEnc[] = { 0, "utf8", "utf16le", "utf16be" };
125763
125764    assert( SQLITE_FUNC_ENCMASK==0x3 );
125765    assert( strcmp(azEnc[SQLITE_UTF8],"utf8")==0 );
125766    assert( strcmp(azEnc[SQLITE_UTF16LE],"utf16le")==0 );
125767    assert( strcmp(azEnc[SQLITE_UTF16BE],"utf16be")==0 );
125768
125769    if( p->xSFunc==0 ) continue;
125770    if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
125771     && showInternFuncs==0
125772    ){
125773      continue;
125774    }
125775    if( p->xValue!=0 ){
125776      zType = "w";
125777    }else if( p->xFinalize!=0 ){
125778      zType = "a";
125779    }else{
125780      zType = "s";
125781    }
125782    sqlite3VdbeMultiLoad(v, 1, "sissii",
125783       p->zName, isBuiltin,
125784       zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
125785       p->nArg,
125786       (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
125787    );
125788  }
125789}
125790
125791
125792/*
125793** Helper subroutine for PRAGMA integrity_check:
125794**
125795** Generate code to output a single-column result row with a value of the
125796** string held in register 3.  Decrement the result count in register 1
125797** and halt if the maximum number of result rows have been issued.
125798*/
125799static int integrityCheckResultRow(Vdbe *v){
125800  int addr;
125801  sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
125802  addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
125803  VdbeCoverage(v);
125804  sqlite3VdbeAddOp0(v, OP_Halt);
125805  return addr;
125806}
125807
125808/*
125809** Process a pragma statement.
125810**
125811** Pragmas are of this form:
125812**
125813**      PRAGMA [schema.]id [= value]
125814**
125815** The identifier might also be a string.  The value is a string, and
125816** identifier, or a number.  If minusFlag is true, then the value is
125817** a number that was preceded by a minus sign.
125818**
125819** If the left side is "database.id" then pId1 is the database name
125820** and pId2 is the id.  If the left side is just "id" then pId1 is the
125821** id and pId2 is any empty string.
125822*/
125823SQLITE_PRIVATE void sqlite3Pragma(
125824  Parse *pParse,
125825  Token *pId1,        /* First part of [schema.]id field */
125826  Token *pId2,        /* Second part of [schema.]id field, or NULL */
125827  Token *pValue,      /* Token for <value>, or NULL */
125828  int minusFlag       /* True if a '-' sign preceded <value> */
125829){
125830  char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
125831  char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
125832  const char *zDb = 0;   /* The database name */
125833  Token *pId;            /* Pointer to <id> token */
125834  char *aFcntl[4];       /* Argument to SQLITE_FCNTL_PRAGMA */
125835  int iDb;               /* Database index for <database> */
125836  int rc;                      /* return value form SQLITE_FCNTL_PRAGMA */
125837  sqlite3 *db = pParse->db;    /* The database connection */
125838  Db *pDb;                     /* The specific database being pragmaed */
125839  Vdbe *v = sqlite3GetVdbe(pParse);  /* Prepared statement */
125840  const PragmaName *pPragma;   /* The pragma */
125841
125842  if( v==0 ) return;
125843  sqlite3VdbeRunOnlyOnce(v);
125844  pParse->nMem = 2;
125845
125846  /* Interpret the [schema.] part of the pragma statement. iDb is the
125847  ** index of the database this pragma is being applied to in db.aDb[]. */
125848  iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
125849  if( iDb<0 ) return;
125850  pDb = &db->aDb[iDb];
125851
125852  /* If the temp database has been explicitly named as part of the
125853  ** pragma, make sure it is open.
125854  */
125855  if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
125856    return;
125857  }
125858
125859  zLeft = sqlite3NameFromToken(db, pId);
125860  if( !zLeft ) return;
125861  if( minusFlag ){
125862    zRight = sqlite3MPrintf(db, "-%T", pValue);
125863  }else{
125864    zRight = sqlite3NameFromToken(db, pValue);
125865  }
125866
125867  assert( pId2 );
125868  zDb = pId2->n>0 ? pDb->zDbSName : 0;
125869  if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
125870    goto pragma_out;
125871  }
125872
125873  /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
125874  ** connection.  If it returns SQLITE_OK, then assume that the VFS
125875  ** handled the pragma and generate a no-op prepared statement.
125876  **
125877  ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
125878  ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
125879  ** object corresponding to the database file to which the pragma
125880  ** statement refers.
125881  **
125882  ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
125883  ** file control is an array of pointers to strings (char**) in which the
125884  ** second element of the array is the name of the pragma and the third
125885  ** element is the argument to the pragma or NULL if the pragma has no
125886  ** argument.
125887  */
125888  aFcntl[0] = 0;
125889  aFcntl[1] = zLeft;
125890  aFcntl[2] = zRight;
125891  aFcntl[3] = 0;
125892  db->busyHandler.nBusy = 0;
125893  rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
125894  if( rc==SQLITE_OK ){
125895    sqlite3VdbeSetNumCols(v, 1);
125896    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, aFcntl[0], SQLITE_TRANSIENT);
125897    returnSingleText(v, aFcntl[0]);
125898    sqlite3_free(aFcntl[0]);
125899    goto pragma_out;
125900  }
125901  if( rc!=SQLITE_NOTFOUND ){
125902    if( aFcntl[0] ){
125903      sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
125904      sqlite3_free(aFcntl[0]);
125905    }
125906    pParse->nErr++;
125907    pParse->rc = rc;
125908    goto pragma_out;
125909  }
125910
125911  /* Locate the pragma in the lookup table */
125912  pPragma = pragmaLocate(zLeft);
125913  if( pPragma==0 ) goto pragma_out;
125914
125915  /* Make sure the database schema is loaded if the pragma requires that */
125916  if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
125917    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
125918  }
125919
125920  /* Register the result column names for pragmas that return results */
125921  if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
125922   && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
125923  ){
125924    setPragmaResultColumnNames(v, pPragma);
125925  }
125926
125927  /* Jump to the appropriate pragma handler */
125928  switch( pPragma->ePragTyp ){
125929
125930#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
125931  /*
125932  **  PRAGMA [schema.]default_cache_size
125933  **  PRAGMA [schema.]default_cache_size=N
125934  **
125935  ** The first form reports the current persistent setting for the
125936  ** page cache size.  The value returned is the maximum number of
125937  ** pages in the page cache.  The second form sets both the current
125938  ** page cache size value and the persistent page cache size value
125939  ** stored in the database file.
125940  **
125941  ** Older versions of SQLite would set the default cache size to a
125942  ** negative number to indicate synchronous=OFF.  These days, synchronous
125943  ** is always on by default regardless of the sign of the default cache
125944  ** size.  But continue to take the absolute value of the default cache
125945  ** size of historical compatibility.
125946  */
125947  case PragTyp_DEFAULT_CACHE_SIZE: {
125948    static const int iLn = VDBE_OFFSET_LINENO(2);
125949    static const VdbeOpList getCacheSize[] = {
125950      { OP_Transaction, 0, 0,        0},                         /* 0 */
125951      { OP_ReadCookie,  0, 1,        BTREE_DEFAULT_CACHE_SIZE},  /* 1 */
125952      { OP_IfPos,       1, 8,        0},
125953      { OP_Integer,     0, 2,        0},
125954      { OP_Subtract,    1, 2,        1},
125955      { OP_IfPos,       1, 8,        0},
125956      { OP_Integer,     0, 1,        0},                         /* 6 */
125957      { OP_Noop,        0, 0,        0},
125958      { OP_ResultRow,   1, 1,        0},
125959    };
125960    VdbeOp *aOp;
125961    sqlite3VdbeUsesBtree(v, iDb);
125962    if( !zRight ){
125963      pParse->nMem += 2;
125964      sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
125965      aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn);
125966      if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
125967      aOp[0].p1 = iDb;
125968      aOp[1].p1 = iDb;
125969      aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
125970    }else{
125971      int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
125972      sqlite3BeginWriteOperation(pParse, 0, iDb);
125973      sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
125974      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
125975      pDb->pSchema->cache_size = size;
125976      sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
125977    }
125978    break;
125979  }
125980#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
125981
125982#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
125983  /*
125984  **  PRAGMA [schema.]page_size
125985  **  PRAGMA [schema.]page_size=N
125986  **
125987  ** The first form reports the current setting for the
125988  ** database page size in bytes.  The second form sets the
125989  ** database page size value.  The value can only be set if
125990  ** the database has not yet been created.
125991  */
125992  case PragTyp_PAGE_SIZE: {
125993    Btree *pBt = pDb->pBt;
125994    assert( pBt!=0 );
125995    if( !zRight ){
125996      int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
125997      returnSingleInt(v, size);
125998    }else{
125999      /* Malloc may fail when setting the page-size, as there is an internal
126000      ** buffer that the pager module resizes using sqlite3_realloc().
126001      */
126002      db->nextPagesize = sqlite3Atoi(zRight);
126003      if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
126004        sqlite3OomFault(db);
126005      }
126006    }
126007    break;
126008  }
126009
126010  /*
126011  **  PRAGMA [schema.]secure_delete
126012  **  PRAGMA [schema.]secure_delete=ON/OFF/FAST
126013  **
126014  ** The first form reports the current setting for the
126015  ** secure_delete flag.  The second form changes the secure_delete
126016  ** flag setting and reports the new value.
126017  */
126018  case PragTyp_SECURE_DELETE: {
126019    Btree *pBt = pDb->pBt;
126020    int b = -1;
126021    assert( pBt!=0 );
126022    if( zRight ){
126023      if( sqlite3_stricmp(zRight, "fast")==0 ){
126024        b = 2;
126025      }else{
126026        b = sqlite3GetBoolean(zRight, 0);
126027      }
126028    }
126029    if( pId2->n==0 && b>=0 ){
126030      int ii;
126031      for(ii=0; ii<db->nDb; ii++){
126032        sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
126033      }
126034    }
126035    b = sqlite3BtreeSecureDelete(pBt, b);
126036    returnSingleInt(v, b);
126037    break;
126038  }
126039
126040  /*
126041  **  PRAGMA [schema.]max_page_count
126042  **  PRAGMA [schema.]max_page_count=N
126043  **
126044  ** The first form reports the current setting for the
126045  ** maximum number of pages in the database file.  The
126046  ** second form attempts to change this setting.  Both
126047  ** forms return the current setting.
126048  **
126049  ** The absolute value of N is used.  This is undocumented and might
126050  ** change.  The only purpose is to provide an easy way to test
126051  ** the sqlite3AbsInt32() function.
126052  **
126053  **  PRAGMA [schema.]page_count
126054  **
126055  ** Return the number of pages in the specified database.
126056  */
126057  case PragTyp_PAGE_COUNT: {
126058    int iReg;
126059    sqlite3CodeVerifySchema(pParse, iDb);
126060    iReg = ++pParse->nMem;
126061    if( sqlite3Tolower(zLeft[0])=='p' ){
126062      sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
126063    }else{
126064      sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
126065                        sqlite3AbsInt32(sqlite3Atoi(zRight)));
126066    }
126067    sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
126068    break;
126069  }
126070
126071  /*
126072  **  PRAGMA [schema.]locking_mode
126073  **  PRAGMA [schema.]locking_mode = (normal|exclusive)
126074  */
126075  case PragTyp_LOCKING_MODE: {
126076    const char *zRet = "normal";
126077    int eMode = getLockingMode(zRight);
126078
126079    if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
126080      /* Simple "PRAGMA locking_mode;" statement. This is a query for
126081      ** the current default locking mode (which may be different to
126082      ** the locking-mode of the main database).
126083      */
126084      eMode = db->dfltLockMode;
126085    }else{
126086      Pager *pPager;
126087      if( pId2->n==0 ){
126088        /* This indicates that no database name was specified as part
126089        ** of the PRAGMA command. In this case the locking-mode must be
126090        ** set on all attached databases, as well as the main db file.
126091        **
126092        ** Also, the sqlite3.dfltLockMode variable is set so that
126093        ** any subsequently attached databases also use the specified
126094        ** locking mode.
126095        */
126096        int ii;
126097        assert(pDb==&db->aDb[0]);
126098        for(ii=2; ii<db->nDb; ii++){
126099          pPager = sqlite3BtreePager(db->aDb[ii].pBt);
126100          sqlite3PagerLockingMode(pPager, eMode);
126101        }
126102        db->dfltLockMode = (u8)eMode;
126103      }
126104      pPager = sqlite3BtreePager(pDb->pBt);
126105      eMode = sqlite3PagerLockingMode(pPager, eMode);
126106    }
126107
126108    assert( eMode==PAGER_LOCKINGMODE_NORMAL
126109            || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
126110    if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
126111      zRet = "exclusive";
126112    }
126113    returnSingleText(v, zRet);
126114    break;
126115  }
126116
126117  /*
126118  **  PRAGMA [schema.]journal_mode
126119  **  PRAGMA [schema.]journal_mode =
126120  **                      (delete|persist|off|truncate|memory|wal|off)
126121  */
126122  case PragTyp_JOURNAL_MODE: {
126123    int eMode;        /* One of the PAGER_JOURNALMODE_XXX symbols */
126124    int ii;           /* Loop counter */
126125
126126    if( zRight==0 ){
126127      /* If there is no "=MODE" part of the pragma, do a query for the
126128      ** current mode */
126129      eMode = PAGER_JOURNALMODE_QUERY;
126130    }else{
126131      const char *zMode;
126132      int n = sqlite3Strlen30(zRight);
126133      for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
126134        if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
126135      }
126136      if( !zMode ){
126137        /* If the "=MODE" part does not match any known journal mode,
126138        ** then do a query */
126139        eMode = PAGER_JOURNALMODE_QUERY;
126140      }
126141      if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
126142        /* Do not allow journal-mode "OFF" in defensive since the database
126143        ** can become corrupted using ordinary SQL when the journal is off */
126144        eMode = PAGER_JOURNALMODE_QUERY;
126145      }
126146    }
126147    if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
126148      /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
126149      iDb = 0;
126150      pId2->n = 1;
126151    }
126152    for(ii=db->nDb-1; ii>=0; ii--){
126153      if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
126154        sqlite3VdbeUsesBtree(v, ii);
126155        sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
126156      }
126157    }
126158    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
126159    break;
126160  }
126161
126162  /*
126163  **  PRAGMA [schema.]journal_size_limit
126164  **  PRAGMA [schema.]journal_size_limit=N
126165  **
126166  ** Get or set the size limit on rollback journal files.
126167  */
126168  case PragTyp_JOURNAL_SIZE_LIMIT: {
126169    Pager *pPager = sqlite3BtreePager(pDb->pBt);
126170    i64 iLimit = -2;
126171    if( zRight ){
126172      sqlite3DecOrHexToI64(zRight, &iLimit);
126173      if( iLimit<-1 ) iLimit = -1;
126174    }
126175    iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
126176    returnSingleInt(v, iLimit);
126177    break;
126178  }
126179
126180#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
126181
126182  /*
126183  **  PRAGMA [schema.]auto_vacuum
126184  **  PRAGMA [schema.]auto_vacuum=N
126185  **
126186  ** Get or set the value of the database 'auto-vacuum' parameter.
126187  ** The value is one of:  0 NONE 1 FULL 2 INCREMENTAL
126188  */
126189#ifndef SQLITE_OMIT_AUTOVACUUM
126190  case PragTyp_AUTO_VACUUM: {
126191    Btree *pBt = pDb->pBt;
126192    assert( pBt!=0 );
126193    if( !zRight ){
126194      returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
126195    }else{
126196      int eAuto = getAutoVacuum(zRight);
126197      assert( eAuto>=0 && eAuto<=2 );
126198      db->nextAutovac = (u8)eAuto;
126199      /* Call SetAutoVacuum() to set initialize the internal auto and
126200      ** incr-vacuum flags. This is required in case this connection
126201      ** creates the database file. It is important that it is created
126202      ** as an auto-vacuum capable db.
126203      */
126204      rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
126205      if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
126206        /* When setting the auto_vacuum mode to either "full" or
126207        ** "incremental", write the value of meta[6] in the database
126208        ** file. Before writing to meta[6], check that meta[3] indicates
126209        ** that this really is an auto-vacuum capable database.
126210        */
126211        static const int iLn = VDBE_OFFSET_LINENO(2);
126212        static const VdbeOpList setMeta6[] = {
126213          { OP_Transaction,    0,         1,                 0},    /* 0 */
126214          { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
126215          { OP_If,             1,         0,                 0},    /* 2 */
126216          { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
126217          { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 0},    /* 4 */
126218        };
126219        VdbeOp *aOp;
126220        int iAddr = sqlite3VdbeCurrentAddr(v);
126221        sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
126222        aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
126223        if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
126224        aOp[0].p1 = iDb;
126225        aOp[1].p1 = iDb;
126226        aOp[2].p2 = iAddr+4;
126227        aOp[4].p1 = iDb;
126228        aOp[4].p3 = eAuto - 1;
126229        sqlite3VdbeUsesBtree(v, iDb);
126230      }
126231    }
126232    break;
126233  }
126234#endif
126235
126236  /*
126237  **  PRAGMA [schema.]incremental_vacuum(N)
126238  **
126239  ** Do N steps of incremental vacuuming on a database.
126240  */
126241#ifndef SQLITE_OMIT_AUTOVACUUM
126242  case PragTyp_INCREMENTAL_VACUUM: {
126243    int iLimit, addr;
126244    if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
126245      iLimit = 0x7fffffff;
126246    }
126247    sqlite3BeginWriteOperation(pParse, 0, iDb);
126248    sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
126249    addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
126250    sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
126251    sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
126252    sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
126253    sqlite3VdbeJumpHere(v, addr);
126254    break;
126255  }
126256#endif
126257
126258#ifndef SQLITE_OMIT_PAGER_PRAGMAS
126259  /*
126260  **  PRAGMA [schema.]cache_size
126261  **  PRAGMA [schema.]cache_size=N
126262  **
126263  ** The first form reports the current local setting for the
126264  ** page cache size. The second form sets the local
126265  ** page cache size value.  If N is positive then that is the
126266  ** number of pages in the cache.  If N is negative, then the
126267  ** number of pages is adjusted so that the cache uses -N kibibytes
126268  ** of memory.
126269  */
126270  case PragTyp_CACHE_SIZE: {
126271    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
126272    if( !zRight ){
126273      returnSingleInt(v, pDb->pSchema->cache_size);
126274    }else{
126275      int size = sqlite3Atoi(zRight);
126276      pDb->pSchema->cache_size = size;
126277      sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
126278    }
126279    break;
126280  }
126281
126282  /*
126283  **  PRAGMA [schema.]cache_spill
126284  **  PRAGMA cache_spill=BOOLEAN
126285  **  PRAGMA [schema.]cache_spill=N
126286  **
126287  ** The first form reports the current local setting for the
126288  ** page cache spill size. The second form turns cache spill on
126289  ** or off.  When turnning cache spill on, the size is set to the
126290  ** current cache_size.  The third form sets a spill size that
126291  ** may be different form the cache size.
126292  ** If N is positive then that is the
126293  ** number of pages in the cache.  If N is negative, then the
126294  ** number of pages is adjusted so that the cache uses -N kibibytes
126295  ** of memory.
126296  **
126297  ** If the number of cache_spill pages is less then the number of
126298  ** cache_size pages, no spilling occurs until the page count exceeds
126299  ** the number of cache_size pages.
126300  **
126301  ** The cache_spill=BOOLEAN setting applies to all attached schemas,
126302  ** not just the schema specified.
126303  */
126304  case PragTyp_CACHE_SPILL: {
126305    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
126306    if( !zRight ){
126307      returnSingleInt(v,
126308         (db->flags & SQLITE_CacheSpill)==0 ? 0 :
126309            sqlite3BtreeSetSpillSize(pDb->pBt,0));
126310    }else{
126311      int size = 1;
126312      if( sqlite3GetInt32(zRight, &size) ){
126313        sqlite3BtreeSetSpillSize(pDb->pBt, size);
126314      }
126315      if( sqlite3GetBoolean(zRight, size!=0) ){
126316        db->flags |= SQLITE_CacheSpill;
126317      }else{
126318        db->flags &= ~(u64)SQLITE_CacheSpill;
126319      }
126320      setAllPagerFlags(db);
126321    }
126322    break;
126323  }
126324
126325  /*
126326  **  PRAGMA [schema.]mmap_size(N)
126327  **
126328  ** Used to set mapping size limit. The mapping size limit is
126329  ** used to limit the aggregate size of all memory mapped regions of the
126330  ** database file. If this parameter is set to zero, then memory mapping
126331  ** is not used at all.  If N is negative, then the default memory map
126332  ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
126333  ** The parameter N is measured in bytes.
126334  **
126335  ** This value is advisory.  The underlying VFS is free to memory map
126336  ** as little or as much as it wants.  Except, if N is set to 0 then the
126337  ** upper layers will never invoke the xFetch interfaces to the VFS.
126338  */
126339  case PragTyp_MMAP_SIZE: {
126340    sqlite3_int64 sz;
126341#if SQLITE_MAX_MMAP_SIZE>0
126342    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
126343    if( zRight ){
126344      int ii;
126345      sqlite3DecOrHexToI64(zRight, &sz);
126346      if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
126347      if( pId2->n==0 ) db->szMmap = sz;
126348      for(ii=db->nDb-1; ii>=0; ii--){
126349        if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
126350          sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
126351        }
126352      }
126353    }
126354    sz = -1;
126355    rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
126356#else
126357    sz = 0;
126358    rc = SQLITE_OK;
126359#endif
126360    if( rc==SQLITE_OK ){
126361      returnSingleInt(v, sz);
126362    }else if( rc!=SQLITE_NOTFOUND ){
126363      pParse->nErr++;
126364      pParse->rc = rc;
126365    }
126366    break;
126367  }
126368
126369  /*
126370  **   PRAGMA temp_store
126371  **   PRAGMA temp_store = "default"|"memory"|"file"
126372  **
126373  ** Return or set the local value of the temp_store flag.  Changing
126374  ** the local value does not make changes to the disk file and the default
126375  ** value will be restored the next time the database is opened.
126376  **
126377  ** Note that it is possible for the library compile-time options to
126378  ** override this setting
126379  */
126380  case PragTyp_TEMP_STORE: {
126381    if( !zRight ){
126382      returnSingleInt(v, db->temp_store);
126383    }else{
126384      changeTempStorage(pParse, zRight);
126385    }
126386    break;
126387  }
126388
126389  /*
126390  **   PRAGMA temp_store_directory
126391  **   PRAGMA temp_store_directory = ""|"directory_name"
126392  **
126393  ** Return or set the local value of the temp_store_directory flag.  Changing
126394  ** the value sets a specific directory to be used for temporary files.
126395  ** Setting to a null string reverts to the default temporary directory search.
126396  ** If temporary directory is changed, then invalidateTempStorage.
126397  **
126398  */
126399  case PragTyp_TEMP_STORE_DIRECTORY: {
126400    if( !zRight ){
126401      returnSingleText(v, sqlite3_temp_directory);
126402    }else{
126403#ifndef SQLITE_OMIT_WSD
126404      if( zRight[0] ){
126405        int res;
126406        rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
126407        if( rc!=SQLITE_OK || res==0 ){
126408          sqlite3ErrorMsg(pParse, "not a writable directory");
126409          goto pragma_out;
126410        }
126411      }
126412      if( SQLITE_TEMP_STORE==0
126413       || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
126414       || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
126415      ){
126416        invalidateTempStorage(pParse);
126417      }
126418      sqlite3_free(sqlite3_temp_directory);
126419      if( zRight[0] ){
126420        sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
126421      }else{
126422        sqlite3_temp_directory = 0;
126423      }
126424#endif /* SQLITE_OMIT_WSD */
126425    }
126426    break;
126427  }
126428
126429#if SQLITE_OS_WIN
126430  /*
126431  **   PRAGMA data_store_directory
126432  **   PRAGMA data_store_directory = ""|"directory_name"
126433  **
126434  ** Return or set the local value of the data_store_directory flag.  Changing
126435  ** the value sets a specific directory to be used for database files that
126436  ** were specified with a relative pathname.  Setting to a null string reverts
126437  ** to the default database directory, which for database files specified with
126438  ** a relative path will probably be based on the current directory for the
126439  ** process.  Database file specified with an absolute path are not impacted
126440  ** by this setting, regardless of its value.
126441  **
126442  */
126443  case PragTyp_DATA_STORE_DIRECTORY: {
126444    if( !zRight ){
126445      returnSingleText(v, sqlite3_data_directory);
126446    }else{
126447#ifndef SQLITE_OMIT_WSD
126448      if( zRight[0] ){
126449        int res;
126450        rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
126451        if( rc!=SQLITE_OK || res==0 ){
126452          sqlite3ErrorMsg(pParse, "not a writable directory");
126453          goto pragma_out;
126454        }
126455      }
126456      sqlite3_free(sqlite3_data_directory);
126457      if( zRight[0] ){
126458        sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
126459      }else{
126460        sqlite3_data_directory = 0;
126461      }
126462#endif /* SQLITE_OMIT_WSD */
126463    }
126464    break;
126465  }
126466#endif
126467
126468#if SQLITE_ENABLE_LOCKING_STYLE
126469  /*
126470  **   PRAGMA [schema.]lock_proxy_file
126471  **   PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
126472  **
126473  ** Return or set the value of the lock_proxy_file flag.  Changing
126474  ** the value sets a specific file to be used for database access locks.
126475  **
126476  */
126477  case PragTyp_LOCK_PROXY_FILE: {
126478    if( !zRight ){
126479      Pager *pPager = sqlite3BtreePager(pDb->pBt);
126480      char *proxy_file_path = NULL;
126481      sqlite3_file *pFile = sqlite3PagerFile(pPager);
126482      sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
126483                           &proxy_file_path);
126484      returnSingleText(v, proxy_file_path);
126485    }else{
126486      Pager *pPager = sqlite3BtreePager(pDb->pBt);
126487      sqlite3_file *pFile = sqlite3PagerFile(pPager);
126488      int res;
126489      if( zRight[0] ){
126490        res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
126491                                     zRight);
126492      } else {
126493        res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
126494                                     NULL);
126495      }
126496      if( res!=SQLITE_OK ){
126497        sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
126498        goto pragma_out;
126499      }
126500    }
126501    break;
126502  }
126503#endif /* SQLITE_ENABLE_LOCKING_STYLE */
126504
126505  /*
126506  **   PRAGMA [schema.]synchronous
126507  **   PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
126508  **
126509  ** Return or set the local value of the synchronous flag.  Changing
126510  ** the local value does not make changes to the disk file and the
126511  ** default value will be restored the next time the database is
126512  ** opened.
126513  */
126514  case PragTyp_SYNCHRONOUS: {
126515    if( !zRight ){
126516      returnSingleInt(v, pDb->safety_level-1);
126517    }else{
126518      if( !db->autoCommit ){
126519        sqlite3ErrorMsg(pParse,
126520            "Safety level may not be changed inside a transaction");
126521      }else if( iDb!=1 ){
126522        int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
126523        if( iLevel==0 ) iLevel = 1;
126524        pDb->safety_level = iLevel;
126525        pDb->bSyncSet = 1;
126526        setAllPagerFlags(db);
126527      }
126528    }
126529    break;
126530  }
126531#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
126532
126533#ifndef SQLITE_OMIT_FLAG_PRAGMAS
126534  case PragTyp_FLAG: {
126535    if( zRight==0 ){
126536      setPragmaResultColumnNames(v, pPragma);
126537      returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
126538    }else{
126539      u64 mask = pPragma->iArg;    /* Mask of bits to set or clear. */
126540      if( db->autoCommit==0 ){
126541        /* Foreign key support may not be enabled or disabled while not
126542        ** in auto-commit mode.  */
126543        mask &= ~(SQLITE_ForeignKeys);
126544      }
126545#if SQLITE_USER_AUTHENTICATION
126546      if( db->auth.authLevel==UAUTH_User ){
126547        /* Do not allow non-admin users to modify the schema arbitrarily */
126548        mask &= ~(SQLITE_WriteSchema);
126549      }
126550#endif
126551
126552      if( sqlite3GetBoolean(zRight, 0) ){
126553        db->flags |= mask;
126554      }else{
126555        db->flags &= ~mask;
126556        if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
126557      }
126558
126559      /* Many of the flag-pragmas modify the code generated by the SQL
126560      ** compiler (eg. count_changes). So add an opcode to expire all
126561      ** compiled SQL statements after modifying a pragma value.
126562      */
126563      sqlite3VdbeAddOp0(v, OP_Expire);
126564      setAllPagerFlags(db);
126565    }
126566    break;
126567  }
126568#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
126569
126570#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
126571  /*
126572  **   PRAGMA table_info(<table>)
126573  **
126574  ** Return a single row for each column of the named table. The columns of
126575  ** the returned data set are:
126576  **
126577  ** cid:        Column id (numbered from left to right, starting at 0)
126578  ** name:       Column name
126579  ** type:       Column declaration type.
126580  ** notnull:    True if 'NOT NULL' is part of column declaration
126581  ** dflt_value: The default value for the column, if any.
126582  ** pk:         Non-zero for PK fields.
126583  */
126584  case PragTyp_TABLE_INFO: if( zRight ){
126585    Table *pTab;
126586    pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
126587    if( pTab ){
126588      int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126589      int i, k;
126590      int nHidden = 0;
126591      Column *pCol;
126592      Index *pPk = sqlite3PrimaryKeyIndex(pTab);
126593      pParse->nMem = 7;
126594      sqlite3CodeVerifySchema(pParse, iTabDb);
126595      sqlite3ViewGetColumnNames(pParse, pTab);
126596      for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
126597        int isHidden = 0;
126598        if( pCol->colFlags & COLFLAG_NOINSERT ){
126599          if( pPragma->iArg==0 ){
126600            nHidden++;
126601            continue;
126602          }
126603          if( pCol->colFlags & COLFLAG_VIRTUAL ){
126604            isHidden = 2;  /* GENERATED ALWAYS AS ... VIRTUAL */
126605          }else if( pCol->colFlags & COLFLAG_STORED ){
126606            isHidden = 3;  /* GENERATED ALWAYS AS ... STORED */
126607          }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
126608            isHidden = 1;  /* HIDDEN */
126609          }
126610        }
126611        if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
126612          k = 0;
126613        }else if( pPk==0 ){
126614          k = 1;
126615        }else{
126616          for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
126617        }
126618        assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN || isHidden>=2 );
126619        sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
126620               i-nHidden,
126621               pCol->zName,
126622               sqlite3ColumnType(pCol,""),
126623               pCol->notNull ? 1 : 0,
126624               pCol->pDflt && isHidden<2 ? pCol->pDflt->u.zToken : 0,
126625               k,
126626               isHidden);
126627      }
126628    }
126629  }
126630  break;
126631
126632#ifdef SQLITE_DEBUG
126633  case PragTyp_STATS: {
126634    Index *pIdx;
126635    HashElem *i;
126636    pParse->nMem = 5;
126637    sqlite3CodeVerifySchema(pParse, iDb);
126638    for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
126639      Table *pTab = sqliteHashData(i);
126640      sqlite3VdbeMultiLoad(v, 1, "ssiii",
126641           pTab->zName,
126642           0,
126643           pTab->szTabRow,
126644           pTab->nRowLogEst,
126645           pTab->tabFlags);
126646      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
126647        sqlite3VdbeMultiLoad(v, 2, "siiiX",
126648           pIdx->zName,
126649           pIdx->szIdxRow,
126650           pIdx->aiRowLogEst[0],
126651           pIdx->hasStat1);
126652        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
126653      }
126654    }
126655  }
126656  break;
126657#endif
126658
126659  case PragTyp_INDEX_INFO: if( zRight ){
126660    Index *pIdx;
126661    Table *pTab;
126662    pIdx = sqlite3FindIndex(db, zRight, zDb);
126663    if( pIdx==0 ){
126664      /* If there is no index named zRight, check to see if there is a
126665      ** WITHOUT ROWID table named zRight, and if there is, show the
126666      ** structure of the PRIMARY KEY index for that table. */
126667      pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
126668      if( pTab && !HasRowid(pTab) ){
126669        pIdx = sqlite3PrimaryKeyIndex(pTab);
126670      }
126671    }
126672    if( pIdx ){
126673      int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
126674      int i;
126675      int mx;
126676      if( pPragma->iArg ){
126677        /* PRAGMA index_xinfo (newer version with more rows and columns) */
126678        mx = pIdx->nColumn;
126679        pParse->nMem = 6;
126680      }else{
126681        /* PRAGMA index_info (legacy version) */
126682        mx = pIdx->nKeyCol;
126683        pParse->nMem = 3;
126684      }
126685      pTab = pIdx->pTable;
126686      sqlite3CodeVerifySchema(pParse, iIdxDb);
126687      assert( pParse->nMem<=pPragma->nPragCName );
126688      for(i=0; i<mx; i++){
126689        i16 cnum = pIdx->aiColumn[i];
126690        sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
126691                             cnum<0 ? 0 : pTab->aCol[cnum].zName);
126692        if( pPragma->iArg ){
126693          sqlite3VdbeMultiLoad(v, 4, "isiX",
126694            pIdx->aSortOrder[i],
126695            pIdx->azColl[i],
126696            i<pIdx->nKeyCol);
126697        }
126698        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
126699      }
126700    }
126701  }
126702  break;
126703
126704  case PragTyp_INDEX_LIST: if( zRight ){
126705    Index *pIdx;
126706    Table *pTab;
126707    int i;
126708    pTab = sqlite3FindTable(db, zRight, zDb);
126709    if( pTab ){
126710      int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126711      pParse->nMem = 5;
126712      sqlite3CodeVerifySchema(pParse, iTabDb);
126713      for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
126714        const char *azOrigin[] = { "c", "u", "pk" };
126715        sqlite3VdbeMultiLoad(v, 1, "isisi",
126716           i,
126717           pIdx->zName,
126718           IsUniqueIndex(pIdx),
126719           azOrigin[pIdx->idxType],
126720           pIdx->pPartIdxWhere!=0);
126721      }
126722    }
126723  }
126724  break;
126725
126726  case PragTyp_DATABASE_LIST: {
126727    int i;
126728    pParse->nMem = 3;
126729    for(i=0; i<db->nDb; i++){
126730      if( db->aDb[i].pBt==0 ) continue;
126731      assert( db->aDb[i].zDbSName!=0 );
126732      sqlite3VdbeMultiLoad(v, 1, "iss",
126733         i,
126734         db->aDb[i].zDbSName,
126735         sqlite3BtreeGetFilename(db->aDb[i].pBt));
126736    }
126737  }
126738  break;
126739
126740  case PragTyp_COLLATION_LIST: {
126741    int i = 0;
126742    HashElem *p;
126743    pParse->nMem = 2;
126744    for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
126745      CollSeq *pColl = (CollSeq *)sqliteHashData(p);
126746      sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
126747    }
126748  }
126749  break;
126750
126751#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
126752  case PragTyp_FUNCTION_LIST: {
126753    int i;
126754    HashElem *j;
126755    FuncDef *p;
126756    int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
126757    pParse->nMem = 6;
126758    for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
126759      for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
126760        pragmaFunclistLine(v, p, 1, showInternFunc);
126761      }
126762    }
126763    for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
126764      p = (FuncDef*)sqliteHashData(j);
126765      pragmaFunclistLine(v, p, 0, showInternFunc);
126766    }
126767  }
126768  break;
126769
126770#ifndef SQLITE_OMIT_VIRTUALTABLE
126771  case PragTyp_MODULE_LIST: {
126772    HashElem *j;
126773    pParse->nMem = 1;
126774    for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
126775      Module *pMod = (Module*)sqliteHashData(j);
126776      sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
126777    }
126778  }
126779  break;
126780#endif /* SQLITE_OMIT_VIRTUALTABLE */
126781
126782  case PragTyp_PRAGMA_LIST: {
126783    int i;
126784    for(i=0; i<ArraySize(aPragmaName); i++){
126785      sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName);
126786    }
126787  }
126788  break;
126789#endif /* SQLITE_INTROSPECTION_PRAGMAS */
126790
126791#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
126792
126793#ifndef SQLITE_OMIT_FOREIGN_KEY
126794  case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
126795    FKey *pFK;
126796    Table *pTab;
126797    pTab = sqlite3FindTable(db, zRight, zDb);
126798    if( pTab ){
126799      pFK = pTab->pFKey;
126800      if( pFK ){
126801        int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126802        int i = 0;
126803        pParse->nMem = 8;
126804        sqlite3CodeVerifySchema(pParse, iTabDb);
126805        while(pFK){
126806          int j;
126807          for(j=0; j<pFK->nCol; j++){
126808            sqlite3VdbeMultiLoad(v, 1, "iissssss",
126809                   i,
126810                   j,
126811                   pFK->zTo,
126812                   pTab->aCol[pFK->aCol[j].iFrom].zName,
126813                   pFK->aCol[j].zCol,
126814                   actionName(pFK->aAction[1]),  /* ON UPDATE */
126815                   actionName(pFK->aAction[0]),  /* ON DELETE */
126816                   "NONE");
126817          }
126818          ++i;
126819          pFK = pFK->pNextFrom;
126820        }
126821      }
126822    }
126823  }
126824  break;
126825#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
126826
126827#ifndef SQLITE_OMIT_FOREIGN_KEY
126828#ifndef SQLITE_OMIT_TRIGGER
126829  case PragTyp_FOREIGN_KEY_CHECK: {
126830    FKey *pFK;             /* A foreign key constraint */
126831    Table *pTab;           /* Child table contain "REFERENCES" keyword */
126832    Table *pParent;        /* Parent table that child points to */
126833    Index *pIdx;           /* Index in the parent table */
126834    int i;                 /* Loop counter:  Foreign key number for pTab */
126835    int j;                 /* Loop counter:  Field of the foreign key */
126836    HashElem *k;           /* Loop counter:  Next table in schema */
126837    int x;                 /* result variable */
126838    int regResult;         /* 3 registers to hold a result row */
126839    int regKey;            /* Register to hold key for checking the FK */
126840    int regRow;            /* Registers to hold a row from pTab */
126841    int addrTop;           /* Top of a loop checking foreign keys */
126842    int addrOk;            /* Jump here if the key is OK */
126843    int *aiCols;           /* child to parent column mapping */
126844
126845    regResult = pParse->nMem+1;
126846    pParse->nMem += 4;
126847    regKey = ++pParse->nMem;
126848    regRow = ++pParse->nMem;
126849    k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
126850    while( k ){
126851      int iTabDb;
126852      if( zRight ){
126853        pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
126854        k = 0;
126855      }else{
126856        pTab = (Table*)sqliteHashData(k);
126857        k = sqliteHashNext(k);
126858      }
126859      if( pTab==0 || pTab->pFKey==0 ) continue;
126860      iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126861      sqlite3CodeVerifySchema(pParse, iTabDb);
126862      sqlite3TableLock(pParse, iTabDb, pTab->tnum, 0, pTab->zName);
126863      if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
126864      sqlite3OpenTable(pParse, 0, iTabDb, pTab, OP_OpenRead);
126865      sqlite3VdbeLoadString(v, regResult, pTab->zName);
126866      for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
126867        pParent = sqlite3FindTable(db, pFK->zTo, zDb);
126868        if( pParent==0 ) continue;
126869        pIdx = 0;
126870        sqlite3TableLock(pParse, iTabDb, pParent->tnum, 0, pParent->zName);
126871        x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
126872        if( x==0 ){
126873          if( pIdx==0 ){
126874            sqlite3OpenTable(pParse, i, iTabDb, pParent, OP_OpenRead);
126875          }else{
126876            sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iTabDb);
126877            sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
126878          }
126879        }else{
126880          k = 0;
126881          break;
126882        }
126883      }
126884      assert( pParse->nErr>0 || pFK==0 );
126885      if( pFK ) break;
126886      if( pParse->nTab<i ) pParse->nTab = i;
126887      addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
126888      for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
126889        pParent = sqlite3FindTable(db, pFK->zTo, zDb);
126890        pIdx = 0;
126891        aiCols = 0;
126892        if( pParent ){
126893          x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
126894          assert( x==0 );
126895        }
126896        addrOk = sqlite3VdbeMakeLabel(pParse);
126897
126898        /* Generate code to read the child key values into registers
126899        ** regRow..regRow+n. If any of the child key values are NULL, this
126900        ** row cannot cause an FK violation. Jump directly to addrOk in
126901        ** this case. */
126902        for(j=0; j<pFK->nCol; j++){
126903          int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
126904          sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j);
126905          sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
126906        }
126907
126908        /* Generate code to query the parent index for a matching parent
126909        ** key. If a match is found, jump to addrOk. */
126910        if( pIdx ){
126911          sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
126912              sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
126913          sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
126914          VdbeCoverage(v);
126915        }else if( pParent ){
126916          int jmp = sqlite3VdbeCurrentAddr(v)+2;
126917          sqlite3VdbeAddOp3(v, OP_SeekRowid, i, jmp, regRow); VdbeCoverage(v);
126918          sqlite3VdbeGoto(v, addrOk);
126919          assert( pFK->nCol==1 );
126920        }
126921
126922        /* Generate code to report an FK violation to the caller. */
126923        if( HasRowid(pTab) ){
126924          sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
126925        }else{
126926          sqlite3VdbeAddOp2(v, OP_Null, 0, regResult+1);
126927        }
126928        sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
126929        sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
126930        sqlite3VdbeResolveLabel(v, addrOk);
126931        sqlite3DbFree(db, aiCols);
126932      }
126933      sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
126934      sqlite3VdbeJumpHere(v, addrTop);
126935    }
126936  }
126937  break;
126938#endif /* !defined(SQLITE_OMIT_TRIGGER) */
126939#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
126940
126941#ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA
126942  /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
126943  ** used will be case sensitive or not depending on the RHS.
126944  */
126945  case PragTyp_CASE_SENSITIVE_LIKE: {
126946    if( zRight ){
126947      sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
126948    }
126949  }
126950  break;
126951#endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */
126952
126953#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
126954# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
126955#endif
126956
126957#ifndef SQLITE_OMIT_INTEGRITY_CHECK
126958  /*    PRAGMA integrity_check
126959  **    PRAGMA integrity_check(N)
126960  **    PRAGMA quick_check
126961  **    PRAGMA quick_check(N)
126962  **
126963  ** Verify the integrity of the database.
126964  **
126965  ** The "quick_check" is reduced version of
126966  ** integrity_check designed to detect most database corruption
126967  ** without the overhead of cross-checking indexes.  Quick_check
126968  ** is linear time wherease integrity_check is O(NlogN).
126969  */
126970  case PragTyp_INTEGRITY_CHECK: {
126971    int i, j, addr, mxErr;
126972
126973    int isQuick = (sqlite3Tolower(zLeft[0])=='q');
126974
126975    /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
126976    ** then iDb is set to the index of the database identified by <db>.
126977    ** In this case, the integrity of database iDb only is verified by
126978    ** the VDBE created below.
126979    **
126980    ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
126981    ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
126982    ** to -1 here, to indicate that the VDBE should verify the integrity
126983    ** of all attached databases.  */
126984    assert( iDb>=0 );
126985    assert( iDb==0 || pId2->z );
126986    if( pId2->z==0 ) iDb = -1;
126987
126988    /* Initialize the VDBE program */
126989    pParse->nMem = 6;
126990
126991    /* Set the maximum error count */
126992    mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
126993    if( zRight ){
126994      sqlite3GetInt32(zRight, &mxErr);
126995      if( mxErr<=0 ){
126996        mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
126997      }
126998    }
126999    sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
127000
127001    /* Do an integrity check on each database file */
127002    for(i=0; i<db->nDb; i++){
127003      HashElem *x;     /* For looping over tables in the schema */
127004      Hash *pTbls;     /* Set of all tables in the schema */
127005      int *aRoot;      /* Array of root page numbers of all btrees */
127006      int cnt = 0;     /* Number of entries in aRoot[] */
127007      int mxIdx = 0;   /* Maximum number of indexes for any table */
127008
127009      if( OMIT_TEMPDB && i==1 ) continue;
127010      if( iDb>=0 && i!=iDb ) continue;
127011
127012      sqlite3CodeVerifySchema(pParse, i);
127013
127014      /* Do an integrity check of the B-Tree
127015      **
127016      ** Begin by finding the root pages numbers
127017      ** for all tables and indices in the database.
127018      */
127019      assert( sqlite3SchemaMutexHeld(db, i, 0) );
127020      pTbls = &db->aDb[i].pSchema->tblHash;
127021      for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
127022        Table *pTab = sqliteHashData(x);  /* Current table */
127023        Index *pIdx;                      /* An index on pTab */
127024        int nIdx;                         /* Number of indexes on pTab */
127025        if( HasRowid(pTab) ) cnt++;
127026        for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
127027        if( nIdx>mxIdx ) mxIdx = nIdx;
127028      }
127029      aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
127030      if( aRoot==0 ) break;
127031      for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
127032        Table *pTab = sqliteHashData(x);
127033        Index *pIdx;
127034        if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
127035        for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
127036          aRoot[++cnt] = pIdx->tnum;
127037        }
127038      }
127039      aRoot[0] = cnt;
127040
127041      /* Make sure sufficient number of registers have been allocated */
127042      pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
127043      sqlite3ClearTempRegCache(pParse);
127044
127045      /* Do the b-tree integrity checks */
127046      sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
127047      sqlite3VdbeChangeP5(v, (u8)i);
127048      addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
127049      sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
127050         sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
127051         P4_DYNAMIC);
127052      sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3);
127053      integrityCheckResultRow(v);
127054      sqlite3VdbeJumpHere(v, addr);
127055
127056      /* Make sure all the indices are constructed correctly.
127057      */
127058      for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
127059        Table *pTab = sqliteHashData(x);
127060        Index *pIdx, *pPk;
127061        Index *pPrior = 0;
127062        int loopTop;
127063        int iDataCur, iIdxCur;
127064        int r1 = -1;
127065
127066        if( pTab->tnum<1 ) continue;  /* Skip VIEWs or VIRTUAL TABLEs */
127067        pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
127068        sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
127069                                   1, 0, &iDataCur, &iIdxCur);
127070        /* reg[7] counts the number of entries in the table.
127071        ** reg[8+i] counts the number of entries in the i-th index
127072        */
127073        sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
127074        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
127075          sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
127076        }
127077        assert( pParse->nMem>=8+j );
127078        assert( sqlite3NoTempsInRange(pParse,1,7+j) );
127079        sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
127080        loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
127081        if( !isQuick ){
127082          /* Sanity check on record header decoding */
127083          sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nNVCol-1,3);
127084          sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
127085        }
127086        /* Verify that all NOT NULL columns really are NOT NULL */
127087        for(j=0; j<pTab->nCol; j++){
127088          char *zErr;
127089          int jmp2;
127090          if( j==pTab->iPKey ) continue;
127091          if( pTab->aCol[j].notNull==0 ) continue;
127092          sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
127093          if( sqlite3VdbeGetOp(v,-1)->opcode==OP_Column ){
127094            sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
127095          }
127096          jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
127097          zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
127098                              pTab->aCol[j].zName);
127099          sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
127100          integrityCheckResultRow(v);
127101          sqlite3VdbeJumpHere(v, jmp2);
127102        }
127103        /* Verify CHECK constraints */
127104        if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
127105          ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
127106          if( db->mallocFailed==0 ){
127107            int addrCkFault = sqlite3VdbeMakeLabel(pParse);
127108            int addrCkOk = sqlite3VdbeMakeLabel(pParse);
127109            char *zErr;
127110            int k;
127111            pParse->iSelfTab = iDataCur + 1;
127112            for(k=pCheck->nExpr-1; k>0; k--){
127113              sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
127114            }
127115            sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
127116                SQLITE_JUMPIFNULL);
127117            sqlite3VdbeResolveLabel(v, addrCkFault);
127118            pParse->iSelfTab = 0;
127119            zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
127120                pTab->zName);
127121            sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
127122            integrityCheckResultRow(v);
127123            sqlite3VdbeResolveLabel(v, addrCkOk);
127124          }
127125          sqlite3ExprListDelete(db, pCheck);
127126        }
127127        if( !isQuick ){ /* Omit the remaining tests for quick_check */
127128          /* Validate index entries for the current row */
127129          for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
127130            int jmp2, jmp3, jmp4, jmp5;
127131            int ckUniq = sqlite3VdbeMakeLabel(pParse);
127132            if( pPk==pIdx ) continue;
127133            r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
127134                                         pPrior, r1);
127135            pPrior = pIdx;
127136            sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */
127137            /* Verify that an index entry exists for the current table row */
127138            jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
127139                                        pIdx->nColumn); VdbeCoverage(v);
127140            sqlite3VdbeLoadString(v, 3, "row ");
127141            sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
127142            sqlite3VdbeLoadString(v, 4, " missing from index ");
127143            sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
127144            jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
127145            sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
127146            jmp4 = integrityCheckResultRow(v);
127147            sqlite3VdbeJumpHere(v, jmp2);
127148            /* For UNIQUE indexes, verify that only one entry exists with the
127149            ** current key.  The entry is unique if (1) any column is NULL
127150            ** or (2) the next entry has a different key */
127151            if( IsUniqueIndex(pIdx) ){
127152              int uniqOk = sqlite3VdbeMakeLabel(pParse);
127153              int jmp6;
127154              int kk;
127155              for(kk=0; kk<pIdx->nKeyCol; kk++){
127156                int iCol = pIdx->aiColumn[kk];
127157                assert( iCol!=XN_ROWID && iCol<pTab->nCol );
127158                if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
127159                sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
127160                VdbeCoverage(v);
127161              }
127162              jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
127163              sqlite3VdbeGoto(v, uniqOk);
127164              sqlite3VdbeJumpHere(v, jmp6);
127165              sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
127166                                   pIdx->nKeyCol); VdbeCoverage(v);
127167              sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
127168              sqlite3VdbeGoto(v, jmp5);
127169              sqlite3VdbeResolveLabel(v, uniqOk);
127170            }
127171            sqlite3VdbeJumpHere(v, jmp4);
127172            sqlite3ResolvePartIdxLabel(pParse, jmp3);
127173          }
127174        }
127175        sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
127176        sqlite3VdbeJumpHere(v, loopTop-1);
127177        if( !isQuick ){
127178          sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
127179          for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
127180            if( pPk==pIdx ) continue;
127181            sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
127182            addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v);
127183            sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
127184            sqlite3VdbeLoadString(v, 4, pIdx->zName);
127185            sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
127186            integrityCheckResultRow(v);
127187            sqlite3VdbeJumpHere(v, addr);
127188          }
127189        }
127190      }
127191    }
127192    {
127193      static const int iLn = VDBE_OFFSET_LINENO(2);
127194      static const VdbeOpList endCode[] = {
127195        { OP_AddImm,      1, 0,        0},    /* 0 */
127196        { OP_IfNotZero,   1, 4,        0},    /* 1 */
127197        { OP_String8,     0, 3,        0},    /* 2 */
127198        { OP_ResultRow,   3, 1,        0},    /* 3 */
127199        { OP_Halt,        0, 0,        0},    /* 4 */
127200        { OP_String8,     0, 3,        0},    /* 5 */
127201        { OP_Goto,        0, 3,        0},    /* 6 */
127202      };
127203      VdbeOp *aOp;
127204
127205      aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
127206      if( aOp ){
127207        aOp[0].p2 = 1-mxErr;
127208        aOp[2].p4type = P4_STATIC;
127209        aOp[2].p4.z = "ok";
127210        aOp[5].p4type = P4_STATIC;
127211        aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT);
127212      }
127213      sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
127214    }
127215  }
127216  break;
127217#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
127218
127219#ifndef SQLITE_OMIT_UTF16
127220  /*
127221  **   PRAGMA encoding
127222  **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
127223  **
127224  ** In its first form, this pragma returns the encoding of the main
127225  ** database. If the database is not initialized, it is initialized now.
127226  **
127227  ** The second form of this pragma is a no-op if the main database file
127228  ** has not already been initialized. In this case it sets the default
127229  ** encoding that will be used for the main database file if a new file
127230  ** is created. If an existing main database file is opened, then the
127231  ** default text encoding for the existing database is used.
127232  **
127233  ** In all cases new databases created using the ATTACH command are
127234  ** created to use the same default text encoding as the main database. If
127235  ** the main database has not been initialized and/or created when ATTACH
127236  ** is executed, this is done before the ATTACH operation.
127237  **
127238  ** In the second form this pragma sets the text encoding to be used in
127239  ** new database files created using this database handle. It is only
127240  ** useful if invoked immediately after the main database i
127241  */
127242  case PragTyp_ENCODING: {
127243    static const struct EncName {
127244      char *zName;
127245      u8 enc;
127246    } encnames[] = {
127247      { "UTF8",     SQLITE_UTF8        },
127248      { "UTF-8",    SQLITE_UTF8        },  /* Must be element [1] */
127249      { "UTF-16le", SQLITE_UTF16LE     },  /* Must be element [2] */
127250      { "UTF-16be", SQLITE_UTF16BE     },  /* Must be element [3] */
127251      { "UTF16le",  SQLITE_UTF16LE     },
127252      { "UTF16be",  SQLITE_UTF16BE     },
127253      { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
127254      { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
127255      { 0, 0 }
127256    };
127257    const struct EncName *pEnc;
127258    if( !zRight ){    /* "PRAGMA encoding" */
127259      if( sqlite3ReadSchema(pParse) ) goto pragma_out;
127260      assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
127261      assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
127262      assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
127263      returnSingleText(v, encnames[ENC(pParse->db)].zName);
127264    }else{                        /* "PRAGMA encoding = XXX" */
127265      /* Only change the value of sqlite.enc if the database handle is not
127266      ** initialized. If the main database exists, the new sqlite.enc value
127267      ** will be overwritten when the schema is next loaded. If it does not
127268      ** already exists, it will be created to use the new encoding value.
127269      */
127270      if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
127271        for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
127272          if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
127273            u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
127274            SCHEMA_ENC(db) = enc;
127275            sqlite3SetTextEncoding(db, enc);
127276            break;
127277          }
127278        }
127279        if( !pEnc->zName ){
127280          sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
127281        }
127282      }
127283    }
127284  }
127285  break;
127286#endif /* SQLITE_OMIT_UTF16 */
127287
127288#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
127289  /*
127290  **   PRAGMA [schema.]schema_version
127291  **   PRAGMA [schema.]schema_version = <integer>
127292  **
127293  **   PRAGMA [schema.]user_version
127294  **   PRAGMA [schema.]user_version = <integer>
127295  **
127296  **   PRAGMA [schema.]freelist_count
127297  **
127298  **   PRAGMA [schema.]data_version
127299  **
127300  **   PRAGMA [schema.]application_id
127301  **   PRAGMA [schema.]application_id = <integer>
127302  **
127303  ** The pragma's schema_version and user_version are used to set or get
127304  ** the value of the schema-version and user-version, respectively. Both
127305  ** the schema-version and the user-version are 32-bit signed integers
127306  ** stored in the database header.
127307  **
127308  ** The schema-cookie is usually only manipulated internally by SQLite. It
127309  ** is incremented by SQLite whenever the database schema is modified (by
127310  ** creating or dropping a table or index). The schema version is used by
127311  ** SQLite each time a query is executed to ensure that the internal cache
127312  ** of the schema used when compiling the SQL query matches the schema of
127313  ** the database against which the compiled query is actually executed.
127314  ** Subverting this mechanism by using "PRAGMA schema_version" to modify
127315  ** the schema-version is potentially dangerous and may lead to program
127316  ** crashes or database corruption. Use with caution!
127317  **
127318  ** The user-version is not used internally by SQLite. It may be used by
127319  ** applications for any purpose.
127320  */
127321  case PragTyp_HEADER_VALUE: {
127322    int iCookie = pPragma->iArg;  /* Which cookie to read or write */
127323    sqlite3VdbeUsesBtree(v, iDb);
127324    if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
127325      /* Write the specified cookie value */
127326      static const VdbeOpList setCookie[] = {
127327        { OP_Transaction,    0,  1,  0},    /* 0 */
127328        { OP_SetCookie,      0,  0,  0},    /* 1 */
127329      };
127330      VdbeOp *aOp;
127331      sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
127332      aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
127333      if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
127334      aOp[0].p1 = iDb;
127335      aOp[1].p1 = iDb;
127336      aOp[1].p2 = iCookie;
127337      aOp[1].p3 = sqlite3Atoi(zRight);
127338    }else{
127339      /* Read the specified cookie value */
127340      static const VdbeOpList readCookie[] = {
127341        { OP_Transaction,     0,  0,  0},    /* 0 */
127342        { OP_ReadCookie,      0,  1,  0},    /* 1 */
127343        { OP_ResultRow,       1,  1,  0}
127344      };
127345      VdbeOp *aOp;
127346      sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
127347      aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
127348      if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
127349      aOp[0].p1 = iDb;
127350      aOp[1].p1 = iDb;
127351      aOp[1].p3 = iCookie;
127352      sqlite3VdbeReusable(v);
127353    }
127354  }
127355  break;
127356#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
127357
127358#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
127359  /*
127360  **   PRAGMA compile_options
127361  **
127362  ** Return the names of all compile-time options used in this build,
127363  ** one option per row.
127364  */
127365  case PragTyp_COMPILE_OPTIONS: {
127366    int i = 0;
127367    const char *zOpt;
127368    pParse->nMem = 1;
127369    while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
127370      sqlite3VdbeLoadString(v, 1, zOpt);
127371      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
127372    }
127373    sqlite3VdbeReusable(v);
127374  }
127375  break;
127376#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
127377
127378#ifndef SQLITE_OMIT_WAL
127379  /*
127380  **   PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
127381  **
127382  ** Checkpoint the database.
127383  */
127384  case PragTyp_WAL_CHECKPOINT: {
127385    int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
127386    int eMode = SQLITE_CHECKPOINT_PASSIVE;
127387    if( zRight ){
127388      if( sqlite3StrICmp(zRight, "full")==0 ){
127389        eMode = SQLITE_CHECKPOINT_FULL;
127390      }else if( sqlite3StrICmp(zRight, "restart")==0 ){
127391        eMode = SQLITE_CHECKPOINT_RESTART;
127392      }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
127393        eMode = SQLITE_CHECKPOINT_TRUNCATE;
127394      }
127395    }
127396    pParse->nMem = 3;
127397    sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
127398    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
127399  }
127400  break;
127401
127402  /*
127403  **   PRAGMA wal_autocheckpoint
127404  **   PRAGMA wal_autocheckpoint = N
127405  **
127406  ** Configure a database connection to automatically checkpoint a database
127407  ** after accumulating N frames in the log. Or query for the current value
127408  ** of N.
127409  */
127410  case PragTyp_WAL_AUTOCHECKPOINT: {
127411    if( zRight ){
127412      sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
127413    }
127414    returnSingleInt(v,
127415       db->xWalCallback==sqlite3WalDefaultHook ?
127416           SQLITE_PTR_TO_INT(db->pWalArg) : 0);
127417  }
127418  break;
127419#endif
127420
127421  /*
127422  **  PRAGMA shrink_memory
127423  **
127424  ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
127425  ** connection on which it is invoked to free up as much memory as it
127426  ** can, by calling sqlite3_db_release_memory().
127427  */
127428  case PragTyp_SHRINK_MEMORY: {
127429    sqlite3_db_release_memory(db);
127430    break;
127431  }
127432
127433  /*
127434  **  PRAGMA optimize
127435  **  PRAGMA optimize(MASK)
127436  **  PRAGMA schema.optimize
127437  **  PRAGMA schema.optimize(MASK)
127438  **
127439  ** Attempt to optimize the database.  All schemas are optimized in the first
127440  ** two forms, and only the specified schema is optimized in the latter two.
127441  **
127442  ** The details of optimizations performed by this pragma are expected
127443  ** to change and improve over time.  Applications should anticipate that
127444  ** this pragma will perform new optimizations in future releases.
127445  **
127446  ** The optional argument is a bitmask of optimizations to perform:
127447  **
127448  **    0x0001    Debugging mode.  Do not actually perform any optimizations
127449  **              but instead return one line of text for each optimization
127450  **              that would have been done.  Off by default.
127451  **
127452  **    0x0002    Run ANALYZE on tables that might benefit.  On by default.
127453  **              See below for additional information.
127454  **
127455  **    0x0004    (Not yet implemented) Record usage and performance
127456  **              information from the current session in the
127457  **              database file so that it will be available to "optimize"
127458  **              pragmas run by future database connections.
127459  **
127460  **    0x0008    (Not yet implemented) Create indexes that might have
127461  **              been helpful to recent queries
127462  **
127463  ** The default MASK is and always shall be 0xfffe.  0xfffe means perform all
127464  ** of the optimizations listed above except Debug Mode, including new
127465  ** optimizations that have not yet been invented.  If new optimizations are
127466  ** ever added that should be off by default, those off-by-default
127467  ** optimizations will have bitmasks of 0x10000 or larger.
127468  **
127469  ** DETERMINATION OF WHEN TO RUN ANALYZE
127470  **
127471  ** In the current implementation, a table is analyzed if only if all of
127472  ** the following are true:
127473  **
127474  ** (1) MASK bit 0x02 is set.
127475  **
127476  ** (2) The query planner used sqlite_stat1-style statistics for one or
127477  **     more indexes of the table at some point during the lifetime of
127478  **     the current connection.
127479  **
127480  ** (3) One or more indexes of the table are currently unanalyzed OR
127481  **     the number of rows in the table has increased by 25 times or more
127482  **     since the last time ANALYZE was run.
127483  **
127484  ** The rules for when tables are analyzed are likely to change in
127485  ** future releases.
127486  */
127487  case PragTyp_OPTIMIZE: {
127488    int iDbLast;           /* Loop termination point for the schema loop */
127489    int iTabCur;           /* Cursor for a table whose size needs checking */
127490    HashElem *k;           /* Loop over tables of a schema */
127491    Schema *pSchema;       /* The current schema */
127492    Table *pTab;           /* A table in the schema */
127493    Index *pIdx;           /* An index of the table */
127494    LogEst szThreshold;    /* Size threshold above which reanalysis is needd */
127495    char *zSubSql;         /* SQL statement for the OP_SqlExec opcode */
127496    u32 opMask;            /* Mask of operations to perform */
127497
127498    if( zRight ){
127499      opMask = (u32)sqlite3Atoi(zRight);
127500      if( (opMask & 0x02)==0 ) break;
127501    }else{
127502      opMask = 0xfffe;
127503    }
127504    iTabCur = pParse->nTab++;
127505    for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
127506      if( iDb==1 ) continue;
127507      sqlite3CodeVerifySchema(pParse, iDb);
127508      pSchema = db->aDb[iDb].pSchema;
127509      for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
127510        pTab = (Table*)sqliteHashData(k);
127511
127512        /* If table pTab has not been used in a way that would benefit from
127513        ** having analysis statistics during the current session, then skip it.
127514        ** This also has the effect of skipping virtual tables and views */
127515        if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
127516
127517        /* Reanalyze if the table is 25 times larger than the last analysis */
127518        szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
127519        for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
127520          if( !pIdx->hasStat1 ){
127521            szThreshold = 0; /* Always analyze if any index lacks statistics */
127522            break;
127523          }
127524        }
127525        if( szThreshold ){
127526          sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
127527          sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur,
127528                         sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
127529          VdbeCoverage(v);
127530        }
127531        zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
127532                                 db->aDb[iDb].zDbSName, pTab->zName);
127533        if( opMask & 0x01 ){
127534          int r1 = sqlite3GetTempReg(pParse);
127535          sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC);
127536          sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1);
127537        }else{
127538          sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
127539        }
127540      }
127541    }
127542    sqlite3VdbeAddOp0(v, OP_Expire);
127543    break;
127544  }
127545
127546  /*
127547  **   PRAGMA busy_timeout
127548  **   PRAGMA busy_timeout = N
127549  **
127550  ** Call sqlite3_busy_timeout(db, N).  Return the current timeout value
127551  ** if one is set.  If no busy handler or a different busy handler is set
127552  ** then 0 is returned.  Setting the busy_timeout to 0 or negative
127553  ** disables the timeout.
127554  */
127555  /*case PragTyp_BUSY_TIMEOUT*/ default: {
127556    assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
127557    if( zRight ){
127558      sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
127559    }
127560    returnSingleInt(v, db->busyTimeout);
127561    break;
127562  }
127563
127564  /*
127565  **   PRAGMA soft_heap_limit
127566  **   PRAGMA soft_heap_limit = N
127567  **
127568  ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
127569  ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
127570  ** specified and is a non-negative integer.
127571  ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
127572  ** returns the same integer that would be returned by the
127573  ** sqlite3_soft_heap_limit64(-1) C-language function.
127574  */
127575  case PragTyp_SOFT_HEAP_LIMIT: {
127576    sqlite3_int64 N;
127577    if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
127578      sqlite3_soft_heap_limit64(N);
127579    }
127580    returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
127581    break;
127582  }
127583
127584  /*
127585  **   PRAGMA hard_heap_limit
127586  **   PRAGMA hard_heap_limit = N
127587  **
127588  ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap
127589  ** limit.  The hard heap limit can be activated or lowered by this
127590  ** pragma, but not raised or deactivated.  Only the
127591  ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
127592  ** the hard heap limit.  This allows an application to set a heap limit
127593  ** constraint that cannot be relaxed by an untrusted SQL script.
127594  */
127595  case PragTyp_HARD_HEAP_LIMIT: {
127596    sqlite3_int64 N;
127597    if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
127598      sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
127599      if( N>0 && (iPrior==0 || iPrior>N) ) sqlite3_hard_heap_limit64(N);
127600    }
127601    returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
127602    break;
127603  }
127604
127605  /*
127606  **   PRAGMA threads
127607  **   PRAGMA threads = N
127608  **
127609  ** Configure the maximum number of worker threads.  Return the new
127610  ** maximum, which might be less than requested.
127611  */
127612  case PragTyp_THREADS: {
127613    sqlite3_int64 N;
127614    if( zRight
127615     && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
127616     && N>=0
127617    ){
127618      sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
127619    }
127620    returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
127621    break;
127622  }
127623
127624  /*
127625  **   PRAGMA analysis_limit
127626  **   PRAGMA analysis_limit = N
127627  **
127628  ** Configure the maximum number of rows that ANALYZE will examine
127629  ** in each index that it looks at.  Return the new limit.
127630  */
127631  case PragTyp_ANALYSIS_LIMIT: {
127632    sqlite3_int64 N;
127633    if( zRight
127634     && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
127635     && N>=0
127636    ){
127637      db->nAnalysisLimit = (int)(N&0x7fffffff);
127638    }
127639    returnSingleInt(v, db->nAnalysisLimit);
127640    break;
127641  }
127642
127643#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
127644  /*
127645  ** Report the current state of file logs for all databases
127646  */
127647  case PragTyp_LOCK_STATUS: {
127648    static const char *const azLockName[] = {
127649      "unlocked", "shared", "reserved", "pending", "exclusive"
127650    };
127651    int i;
127652    pParse->nMem = 2;
127653    for(i=0; i<db->nDb; i++){
127654      Btree *pBt;
127655      const char *zState = "unknown";
127656      int j;
127657      if( db->aDb[i].zDbSName==0 ) continue;
127658      pBt = db->aDb[i].pBt;
127659      if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
127660        zState = "closed";
127661      }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
127662                                     SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
127663         zState = azLockName[j];
127664      }
127665      sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
127666    }
127667    break;
127668  }
127669#endif
127670
127671#if defined(SQLITE_ENABLE_CEROD)
127672  case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
127673    if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
127674      sqlite3_activate_cerod(&zRight[6]);
127675    }
127676  }
127677  break;
127678#endif
127679
127680  } /* End of the PRAGMA switch */
127681
127682  /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
127683  ** purpose is to execute assert() statements to verify that if the
127684  ** PragFlg_NoColumns1 flag is set and the caller specified an argument
127685  ** to the PRAGMA, the implementation has not added any OP_ResultRow
127686  ** instructions to the VM.  */
127687  if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
127688    sqlite3VdbeVerifyNoResultRow(v);
127689  }
127690
127691pragma_out:
127692  sqlite3DbFree(db, zLeft);
127693  sqlite3DbFree(db, zRight);
127694}
127695#ifndef SQLITE_OMIT_VIRTUALTABLE
127696/*****************************************************************************
127697** Implementation of an eponymous virtual table that runs a pragma.
127698**
127699*/
127700typedef struct PragmaVtab PragmaVtab;
127701typedef struct PragmaVtabCursor PragmaVtabCursor;
127702struct PragmaVtab {
127703  sqlite3_vtab base;        /* Base class.  Must be first */
127704  sqlite3 *db;              /* The database connection to which it belongs */
127705  const PragmaName *pName;  /* Name of the pragma */
127706  u8 nHidden;               /* Number of hidden columns */
127707  u8 iHidden;               /* Index of the first hidden column */
127708};
127709struct PragmaVtabCursor {
127710  sqlite3_vtab_cursor base; /* Base class.  Must be first */
127711  sqlite3_stmt *pPragma;    /* The pragma statement to run */
127712  sqlite_int64 iRowid;      /* Current rowid */
127713  char *azArg[2];           /* Value of the argument and schema */
127714};
127715
127716/*
127717** Pragma virtual table module xConnect method.
127718*/
127719static int pragmaVtabConnect(
127720  sqlite3 *db,
127721  void *pAux,
127722  int argc, const char *const*argv,
127723  sqlite3_vtab **ppVtab,
127724  char **pzErr
127725){
127726  const PragmaName *pPragma = (const PragmaName*)pAux;
127727  PragmaVtab *pTab = 0;
127728  int rc;
127729  int i, j;
127730  char cSep = '(';
127731  StrAccum acc;
127732  char zBuf[200];
127733
127734  UNUSED_PARAMETER(argc);
127735  UNUSED_PARAMETER(argv);
127736  sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
127737  sqlite3_str_appendall(&acc, "CREATE TABLE x");
127738  for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
127739    sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
127740    cSep = ',';
127741  }
127742  if( i==0 ){
127743    sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
127744    i++;
127745  }
127746  j = 0;
127747  if( pPragma->mPragFlg & PragFlg_Result1 ){
127748    sqlite3_str_appendall(&acc, ",arg HIDDEN");
127749    j++;
127750  }
127751  if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
127752    sqlite3_str_appendall(&acc, ",schema HIDDEN");
127753    j++;
127754  }
127755  sqlite3_str_append(&acc, ")", 1);
127756  sqlite3StrAccumFinish(&acc);
127757  assert( strlen(zBuf) < sizeof(zBuf)-1 );
127758  rc = sqlite3_declare_vtab(db, zBuf);
127759  if( rc==SQLITE_OK ){
127760    pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
127761    if( pTab==0 ){
127762      rc = SQLITE_NOMEM;
127763    }else{
127764      memset(pTab, 0, sizeof(PragmaVtab));
127765      pTab->pName = pPragma;
127766      pTab->db = db;
127767      pTab->iHidden = i;
127768      pTab->nHidden = j;
127769    }
127770  }else{
127771    *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
127772  }
127773
127774  *ppVtab = (sqlite3_vtab*)pTab;
127775  return rc;
127776}
127777
127778/*
127779** Pragma virtual table module xDisconnect method.
127780*/
127781static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
127782  PragmaVtab *pTab = (PragmaVtab*)pVtab;
127783  sqlite3_free(pTab);
127784  return SQLITE_OK;
127785}
127786
127787/* Figure out the best index to use to search a pragma virtual table.
127788**
127789** There are not really any index choices.  But we want to encourage the
127790** query planner to give == constraints on as many hidden parameters as
127791** possible, and especially on the first hidden parameter.  So return a
127792** high cost if hidden parameters are unconstrained.
127793*/
127794static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
127795  PragmaVtab *pTab = (PragmaVtab*)tab;
127796  const struct sqlite3_index_constraint *pConstraint;
127797  int i, j;
127798  int seen[2];
127799
127800  pIdxInfo->estimatedCost = (double)1;
127801  if( pTab->nHidden==0 ){ return SQLITE_OK; }
127802  pConstraint = pIdxInfo->aConstraint;
127803  seen[0] = 0;
127804  seen[1] = 0;
127805  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
127806    if( pConstraint->usable==0 ) continue;
127807    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
127808    if( pConstraint->iColumn < pTab->iHidden ) continue;
127809    j = pConstraint->iColumn - pTab->iHidden;
127810    assert( j < 2 );
127811    seen[j] = i+1;
127812  }
127813  if( seen[0]==0 ){
127814    pIdxInfo->estimatedCost = (double)2147483647;
127815    pIdxInfo->estimatedRows = 2147483647;
127816    return SQLITE_OK;
127817  }
127818  j = seen[0]-1;
127819  pIdxInfo->aConstraintUsage[j].argvIndex = 1;
127820  pIdxInfo->aConstraintUsage[j].omit = 1;
127821  if( seen[1]==0 ) return SQLITE_OK;
127822  pIdxInfo->estimatedCost = (double)20;
127823  pIdxInfo->estimatedRows = 20;
127824  j = seen[1]-1;
127825  pIdxInfo->aConstraintUsage[j].argvIndex = 2;
127826  pIdxInfo->aConstraintUsage[j].omit = 1;
127827  return SQLITE_OK;
127828}
127829
127830/* Create a new cursor for the pragma virtual table */
127831static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
127832  PragmaVtabCursor *pCsr;
127833  pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
127834  if( pCsr==0 ) return SQLITE_NOMEM;
127835  memset(pCsr, 0, sizeof(PragmaVtabCursor));
127836  pCsr->base.pVtab = pVtab;
127837  *ppCursor = &pCsr->base;
127838  return SQLITE_OK;
127839}
127840
127841/* Clear all content from pragma virtual table cursor. */
127842static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
127843  int i;
127844  sqlite3_finalize(pCsr->pPragma);
127845  pCsr->pPragma = 0;
127846  for(i=0; i<ArraySize(pCsr->azArg); i++){
127847    sqlite3_free(pCsr->azArg[i]);
127848    pCsr->azArg[i] = 0;
127849  }
127850}
127851
127852/* Close a pragma virtual table cursor */
127853static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
127854  PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
127855  pragmaVtabCursorClear(pCsr);
127856  sqlite3_free(pCsr);
127857  return SQLITE_OK;
127858}
127859
127860/* Advance the pragma virtual table cursor to the next row */
127861static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
127862  PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
127863  int rc = SQLITE_OK;
127864
127865  /* Increment the xRowid value */
127866  pCsr->iRowid++;
127867  assert( pCsr->pPragma );
127868  if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
127869    rc = sqlite3_finalize(pCsr->pPragma);
127870    pCsr->pPragma = 0;
127871    pragmaVtabCursorClear(pCsr);
127872  }
127873  return rc;
127874}
127875
127876/*
127877** Pragma virtual table module xFilter method.
127878*/
127879static int pragmaVtabFilter(
127880  sqlite3_vtab_cursor *pVtabCursor,
127881  int idxNum, const char *idxStr,
127882  int argc, sqlite3_value **argv
127883){
127884  PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
127885  PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
127886  int rc;
127887  int i, j;
127888  StrAccum acc;
127889  char *zSql;
127890
127891  UNUSED_PARAMETER(idxNum);
127892  UNUSED_PARAMETER(idxStr);
127893  pragmaVtabCursorClear(pCsr);
127894  j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
127895  for(i=0; i<argc; i++, j++){
127896    const char *zText = (const char*)sqlite3_value_text(argv[i]);
127897    assert( j<ArraySize(pCsr->azArg) );
127898    assert( pCsr->azArg[j]==0 );
127899    if( zText ){
127900      pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
127901      if( pCsr->azArg[j]==0 ){
127902        return SQLITE_NOMEM;
127903      }
127904    }
127905  }
127906  sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
127907  sqlite3_str_appendall(&acc, "PRAGMA ");
127908  if( pCsr->azArg[1] ){
127909    sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
127910  }
127911  sqlite3_str_appendall(&acc, pTab->pName->zName);
127912  if( pCsr->azArg[0] ){
127913    sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
127914  }
127915  zSql = sqlite3StrAccumFinish(&acc);
127916  if( zSql==0 ) return SQLITE_NOMEM;
127917  rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
127918  sqlite3_free(zSql);
127919  if( rc!=SQLITE_OK ){
127920    pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
127921    return rc;
127922  }
127923  return pragmaVtabNext(pVtabCursor);
127924}
127925
127926/*
127927** Pragma virtual table module xEof method.
127928*/
127929static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
127930  PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
127931  return (pCsr->pPragma==0);
127932}
127933
127934/* The xColumn method simply returns the corresponding column from
127935** the PRAGMA.
127936*/
127937static int pragmaVtabColumn(
127938  sqlite3_vtab_cursor *pVtabCursor,
127939  sqlite3_context *ctx,
127940  int i
127941){
127942  PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
127943  PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
127944  if( i<pTab->iHidden ){
127945    sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
127946  }else{
127947    sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
127948  }
127949  return SQLITE_OK;
127950}
127951
127952/*
127953** Pragma virtual table module xRowid method.
127954*/
127955static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
127956  PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
127957  *p = pCsr->iRowid;
127958  return SQLITE_OK;
127959}
127960
127961/* The pragma virtual table object */
127962static const sqlite3_module pragmaVtabModule = {
127963  0,                           /* iVersion */
127964  0,                           /* xCreate - create a table */
127965  pragmaVtabConnect,           /* xConnect - connect to an existing table */
127966  pragmaVtabBestIndex,         /* xBestIndex - Determine search strategy */
127967  pragmaVtabDisconnect,        /* xDisconnect - Disconnect from a table */
127968  0,                           /* xDestroy - Drop a table */
127969  pragmaVtabOpen,              /* xOpen - open a cursor */
127970  pragmaVtabClose,             /* xClose - close a cursor */
127971  pragmaVtabFilter,            /* xFilter - configure scan constraints */
127972  pragmaVtabNext,              /* xNext - advance a cursor */
127973  pragmaVtabEof,               /* xEof */
127974  pragmaVtabColumn,            /* xColumn - read data */
127975  pragmaVtabRowid,             /* xRowid - read data */
127976  0,                           /* xUpdate - write data */
127977  0,                           /* xBegin - begin transaction */
127978  0,                           /* xSync - sync transaction */
127979  0,                           /* xCommit - commit transaction */
127980  0,                           /* xRollback - rollback transaction */
127981  0,                           /* xFindFunction - function overloading */
127982  0,                           /* xRename - rename the table */
127983  0,                           /* xSavepoint */
127984  0,                           /* xRelease */
127985  0,                           /* xRollbackTo */
127986  0                            /* xShadowName */
127987};
127988
127989/*
127990** Check to see if zTabName is really the name of a pragma.  If it is,
127991** then register an eponymous virtual table for that pragma and return
127992** a pointer to the Module object for the new virtual table.
127993*/
127994SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
127995  const PragmaName *pName;
127996  assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
127997  pName = pragmaLocate(zName+7);
127998  if( pName==0 ) return 0;
127999  if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
128000  assert( sqlite3HashFind(&db->aModule, zName)==0 );
128001  return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
128002}
128003
128004#endif /* SQLITE_OMIT_VIRTUALTABLE */
128005
128006#endif /* SQLITE_OMIT_PRAGMA */
128007
128008/************** End of pragma.c **********************************************/
128009/************** Begin file prepare.c *****************************************/
128010/*
128011** 2005 May 25
128012**
128013** The author disclaims copyright to this source code.  In place of
128014** a legal notice, here is a blessing:
128015**
128016**    May you do good and not evil.
128017**    May you find forgiveness for yourself and forgive others.
128018**    May you share freely, never taking more than you give.
128019**
128020*************************************************************************
128021** This file contains the implementation of the sqlite3_prepare()
128022** interface, and routines that contribute to loading the database schema
128023** from disk.
128024*/
128025/* #include "sqliteInt.h" */
128026
128027/*
128028** Fill the InitData structure with an error message that indicates
128029** that the database is corrupt.
128030*/
128031static void corruptSchema(
128032  InitData *pData,     /* Initialization context */
128033  const char *zObj,    /* Object being parsed at the point of error */
128034  const char *zExtra   /* Error information */
128035){
128036  sqlite3 *db = pData->db;
128037  if( db->mallocFailed ){
128038    pData->rc = SQLITE_NOMEM_BKPT;
128039  }else if( pData->pzErrMsg[0]!=0 ){
128040    /* A error message has already been generated.  Do not overwrite it */
128041  }else if( pData->mInitFlags & INITFLAG_AlterTable ){
128042    *pData->pzErrMsg = sqlite3DbStrDup(db, zExtra);
128043    pData->rc = SQLITE_ERROR;
128044  }else if( db->flags & SQLITE_WriteSchema ){
128045    pData->rc = SQLITE_CORRUPT_BKPT;
128046  }else{
128047    char *z;
128048    if( zObj==0 ) zObj = "?";
128049    z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
128050    if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
128051    *pData->pzErrMsg = z;
128052    pData->rc = SQLITE_CORRUPT_BKPT;
128053  }
128054}
128055
128056/*
128057** Check to see if any sibling index (another index on the same table)
128058** of pIndex has the same root page number, and if it does, return true.
128059** This would indicate a corrupt schema.
128060*/
128061SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index *pIndex){
128062  Index *p;
128063  for(p=pIndex->pTable->pIndex; p; p=p->pNext){
128064    if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
128065  }
128066  return 0;
128067}
128068
128069/* forward declaration */
128070static int sqlite3Prepare(
128071  sqlite3 *db,              /* Database handle. */
128072  const char *zSql,         /* UTF-8 encoded SQL statement. */
128073  int nBytes,               /* Length of zSql in bytes. */
128074  u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */
128075  Vdbe *pReprepare,         /* VM being reprepared */
128076  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
128077  const char **pzTail       /* OUT: End of parsed string */
128078);
128079
128080
128081/*
128082** This is the callback routine for the code that initializes the
128083** database.  See sqlite3Init() below for additional information.
128084** This routine is also called from the OP_ParseSchema opcode of the VDBE.
128085**
128086** Each callback contains the following information:
128087**
128088**     argv[0] = type of object: "table", "index", "trigger", or "view".
128089**     argv[1] = name of thing being created
128090**     argv[2] = associated table if an index or trigger
128091**     argv[3] = root page number for table or index. 0 for trigger or view.
128092**     argv[4] = SQL text for the CREATE statement.
128093**
128094*/
128095SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
128096  InitData *pData = (InitData*)pInit;
128097  sqlite3 *db = pData->db;
128098  int iDb = pData->iDb;
128099
128100  assert( argc==5 );
128101  UNUSED_PARAMETER2(NotUsed, argc);
128102  assert( sqlite3_mutex_held(db->mutex) );
128103  db->mDbFlags |= DBFLAG_EncodingFixed;
128104  pData->nInitRow++;
128105  if( db->mallocFailed ){
128106    corruptSchema(pData, argv[1], 0);
128107    return 1;
128108  }
128109
128110  assert( iDb>=0 && iDb<db->nDb );
128111  if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
128112  if( argv[3]==0 ){
128113    corruptSchema(pData, argv[1], 0);
128114  }else if( sqlite3_strnicmp(argv[4],"create ",7)==0 ){
128115    /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
128116    ** But because db->init.busy is set to 1, no VDBE code is generated
128117    ** or executed.  All the parser does is build the internal data
128118    ** structures that describe the table, index, or view.
128119    */
128120    int rc;
128121    u8 saved_iDb = db->init.iDb;
128122    sqlite3_stmt *pStmt;
128123    TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
128124
128125    assert( db->init.busy );
128126    db->init.iDb = iDb;
128127    db->init.newTnum = sqlite3Atoi(argv[3]);
128128    db->init.orphanTrigger = 0;
128129    db->init.azInit = argv;
128130    pStmt = 0;
128131    TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
128132    rc = db->errCode;
128133    assert( (rc&0xFF)==(rcp&0xFF) );
128134    db->init.iDb = saved_iDb;
128135    /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
128136    if( SQLITE_OK!=rc ){
128137      if( db->init.orphanTrigger ){
128138        assert( iDb==1 );
128139      }else{
128140        if( rc > pData->rc ) pData->rc = rc;
128141        if( rc==SQLITE_NOMEM ){
128142          sqlite3OomFault(db);
128143        }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
128144          corruptSchema(pData, argv[1], sqlite3_errmsg(db));
128145        }
128146      }
128147    }
128148    sqlite3_finalize(pStmt);
128149  }else if( argv[1]==0 || (argv[4]!=0 && argv[4][0]!=0) ){
128150    corruptSchema(pData, argv[1], 0);
128151  }else{
128152    /* If the SQL column is blank it means this is an index that
128153    ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
128154    ** constraint for a CREATE TABLE.  The index should have already
128155    ** been created when we processed the CREATE TABLE.  All we have
128156    ** to do here is record the root page number for that index.
128157    */
128158    Index *pIndex;
128159    pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
128160    if( pIndex==0
128161     || sqlite3GetInt32(argv[3],&pIndex->tnum)==0
128162     || pIndex->tnum<2
128163     || sqlite3IndexHasDuplicateRootPage(pIndex)
128164    ){
128165      corruptSchema(pData, argv[1], pIndex?"invalid rootpage":"orphan index");
128166    }
128167  }
128168  return 0;
128169}
128170
128171/*
128172** Attempt to read the database schema and initialize internal
128173** data structures for a single database file.  The index of the
128174** database file is given by iDb.  iDb==0 is used for the main
128175** database.  iDb==1 should never be used.  iDb>=2 is used for
128176** auxiliary databases.  Return one of the SQLITE_ error codes to
128177** indicate success or failure.
128178*/
128179SQLITE_PRIVATE int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
128180  int rc;
128181  int i;
128182#ifndef SQLITE_OMIT_DEPRECATED
128183  int size;
128184#endif
128185  Db *pDb;
128186  char const *azArg[6];
128187  int meta[5];
128188  InitData initData;
128189  const char *zMasterName;
128190  int openedTransaction = 0;
128191  int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
128192
128193  assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
128194  assert( iDb>=0 && iDb<db->nDb );
128195  assert( db->aDb[iDb].pSchema );
128196  assert( sqlite3_mutex_held(db->mutex) );
128197  assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
128198
128199  db->init.busy = 1;
128200
128201  /* Construct the in-memory representation schema tables (sqlite_master or
128202  ** sqlite_temp_master) by invoking the parser directly.  The appropriate
128203  ** table name will be inserted automatically by the parser so we can just
128204  ** use the abbreviation "x" here.  The parser will also automatically tag
128205  ** the schema table as read-only. */
128206  azArg[0] = "table";
128207  azArg[1] = zMasterName = SCHEMA_TABLE(iDb);
128208  azArg[2] = azArg[1];
128209  azArg[3] = "1";
128210  azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text,"
128211                            "rootpage int,sql text)";
128212  azArg[5] = 0;
128213  initData.db = db;
128214  initData.iDb = iDb;
128215  initData.rc = SQLITE_OK;
128216  initData.pzErrMsg = pzErrMsg;
128217  initData.mInitFlags = mFlags;
128218  initData.nInitRow = 0;
128219  sqlite3InitCallback(&initData, 5, (char **)azArg, 0);
128220  db->mDbFlags &= mask;
128221  if( initData.rc ){
128222    rc = initData.rc;
128223    goto error_out;
128224  }
128225
128226  /* Create a cursor to hold the database open
128227  */
128228  pDb = &db->aDb[iDb];
128229  if( pDb->pBt==0 ){
128230    assert( iDb==1 );
128231    DbSetProperty(db, 1, DB_SchemaLoaded);
128232    rc = SQLITE_OK;
128233    goto error_out;
128234  }
128235
128236  /* If there is not already a read-only (or read-write) transaction opened
128237  ** on the b-tree database, open one now. If a transaction is opened, it
128238  ** will be closed before this function returns.  */
128239  sqlite3BtreeEnter(pDb->pBt);
128240  if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
128241    rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
128242    if( rc!=SQLITE_OK ){
128243      sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
128244      goto initone_error_out;
128245    }
128246    openedTransaction = 1;
128247  }
128248
128249  /* Get the database meta information.
128250  **
128251  ** Meta values are as follows:
128252  **    meta[0]   Schema cookie.  Changes with each schema change.
128253  **    meta[1]   File format of schema layer.
128254  **    meta[2]   Size of the page cache.
128255  **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
128256  **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
128257  **    meta[5]   User version
128258  **    meta[6]   Incremental vacuum mode
128259  **    meta[7]   unused
128260  **    meta[8]   unused
128261  **    meta[9]   unused
128262  **
128263  ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
128264  ** the possible values of meta[4].
128265  */
128266  for(i=0; i<ArraySize(meta); i++){
128267    sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
128268  }
128269  if( (db->flags & SQLITE_ResetDatabase)!=0 ){
128270    memset(meta, 0, sizeof(meta));
128271  }
128272  pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
128273
128274  /* If opening a non-empty database, check the text encoding. For the
128275  ** main database, set sqlite3.enc to the encoding of the main database.
128276  ** For an attached db, it is an error if the encoding is not the same
128277  ** as sqlite3.enc.
128278  */
128279  if( meta[BTREE_TEXT_ENCODING-1] ){  /* text encoding */
128280    if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
128281      u8 encoding;
128282#ifndef SQLITE_OMIT_UTF16
128283      /* If opening the main database, set ENC(db). */
128284      encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
128285      if( encoding==0 ) encoding = SQLITE_UTF8;
128286#else
128287      encoding = SQLITE_UTF8;
128288#endif
128289      sqlite3SetTextEncoding(db, encoding);
128290    }else{
128291      /* If opening an attached database, the encoding much match ENC(db) */
128292      if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
128293        sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
128294            " text encoding as main database");
128295        rc = SQLITE_ERROR;
128296        goto initone_error_out;
128297      }
128298    }
128299  }
128300  pDb->pSchema->enc = ENC(db);
128301
128302  if( pDb->pSchema->cache_size==0 ){
128303#ifndef SQLITE_OMIT_DEPRECATED
128304    size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
128305    if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
128306    pDb->pSchema->cache_size = size;
128307#else
128308    pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
128309#endif
128310    sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
128311  }
128312
128313  /*
128314  ** file_format==1    Version 3.0.0.
128315  ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
128316  ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
128317  ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
128318  */
128319  pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
128320  if( pDb->pSchema->file_format==0 ){
128321    pDb->pSchema->file_format = 1;
128322  }
128323  if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
128324    sqlite3SetString(pzErrMsg, db, "unsupported file format");
128325    rc = SQLITE_ERROR;
128326    goto initone_error_out;
128327  }
128328
128329  /* Ticket #2804:  When we open a database in the newer file format,
128330  ** clear the legacy_file_format pragma flag so that a VACUUM will
128331  ** not downgrade the database and thus invalidate any descending
128332  ** indices that the user might have created.
128333  */
128334  if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
128335    db->flags &= ~(u64)SQLITE_LegacyFileFmt;
128336  }
128337
128338  /* Read the schema information out of the schema tables
128339  */
128340  assert( db->init.busy );
128341  {
128342    char *zSql;
128343    zSql = sqlite3MPrintf(db,
128344        "SELECT*FROM\"%w\".%s ORDER BY rowid",
128345        db->aDb[iDb].zDbSName, zMasterName);
128346#ifndef SQLITE_OMIT_AUTHORIZATION
128347    {
128348      sqlite3_xauth xAuth;
128349      xAuth = db->xAuth;
128350      db->xAuth = 0;
128351#endif
128352      rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
128353#ifndef SQLITE_OMIT_AUTHORIZATION
128354      db->xAuth = xAuth;
128355    }
128356#endif
128357    if( rc==SQLITE_OK ) rc = initData.rc;
128358    sqlite3DbFree(db, zSql);
128359#ifndef SQLITE_OMIT_ANALYZE
128360    if( rc==SQLITE_OK ){
128361      sqlite3AnalysisLoad(db, iDb);
128362    }
128363#endif
128364  }
128365  if( db->mallocFailed ){
128366    rc = SQLITE_NOMEM_BKPT;
128367    sqlite3ResetAllSchemasOfConnection(db);
128368  }
128369  if( rc==SQLITE_OK || (db->flags&SQLITE_NoSchemaError)){
128370    /* Black magic: If the SQLITE_NoSchemaError flag is set, then consider
128371    ** the schema loaded, even if errors occurred. In this situation the
128372    ** current sqlite3_prepare() operation will fail, but the following one
128373    ** will attempt to compile the supplied statement against whatever subset
128374    ** of the schema was loaded before the error occurred. The primary
128375    ** purpose of this is to allow access to the sqlite_master table
128376    ** even when its contents have been corrupted.
128377    */
128378    DbSetProperty(db, iDb, DB_SchemaLoaded);
128379    rc = SQLITE_OK;
128380  }
128381
128382  /* Jump here for an error that occurs after successfully allocating
128383  ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
128384  ** before that point, jump to error_out.
128385  */
128386initone_error_out:
128387  if( openedTransaction ){
128388    sqlite3BtreeCommit(pDb->pBt);
128389  }
128390  sqlite3BtreeLeave(pDb->pBt);
128391
128392error_out:
128393  if( rc ){
128394    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
128395      sqlite3OomFault(db);
128396    }
128397    sqlite3ResetOneSchema(db, iDb);
128398  }
128399  db->init.busy = 0;
128400  return rc;
128401}
128402
128403/*
128404** Initialize all database files - the main database file, the file
128405** used to store temporary tables, and any additional database files
128406** created using ATTACH statements.  Return a success code.  If an
128407** error occurs, write an error message into *pzErrMsg.
128408**
128409** After a database is initialized, the DB_SchemaLoaded bit is set
128410** bit is set in the flags field of the Db structure.
128411*/
128412SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
128413  int i, rc;
128414  int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
128415
128416  assert( sqlite3_mutex_held(db->mutex) );
128417  assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
128418  assert( db->init.busy==0 );
128419  ENC(db) = SCHEMA_ENC(db);
128420  assert( db->nDb>0 );
128421  /* Do the main schema first */
128422  if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){
128423    rc = sqlite3InitOne(db, 0, pzErrMsg, 0);
128424    if( rc ) return rc;
128425  }
128426  /* All other schemas after the main schema. The "temp" schema must be last */
128427  for(i=db->nDb-1; i>0; i--){
128428    assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
128429    if( !DbHasProperty(db, i, DB_SchemaLoaded) ){
128430      rc = sqlite3InitOne(db, i, pzErrMsg, 0);
128431      if( rc ) return rc;
128432    }
128433  }
128434  if( commit_internal ){
128435    sqlite3CommitInternalChanges(db);
128436  }
128437  return SQLITE_OK;
128438}
128439
128440/*
128441** This routine is a no-op if the database schema is already initialized.
128442** Otherwise, the schema is loaded. An error code is returned.
128443*/
128444SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
128445  int rc = SQLITE_OK;
128446  sqlite3 *db = pParse->db;
128447  assert( sqlite3_mutex_held(db->mutex) );
128448  if( !db->init.busy ){
128449    rc = sqlite3Init(db, &pParse->zErrMsg);
128450    if( rc!=SQLITE_OK ){
128451      pParse->rc = rc;
128452      pParse->nErr++;
128453    }else if( db->noSharedCache ){
128454      db->mDbFlags |= DBFLAG_SchemaKnownOk;
128455    }
128456  }
128457  return rc;
128458}
128459
128460
128461/*
128462** Check schema cookies in all databases.  If any cookie is out
128463** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies
128464** make no changes to pParse->rc.
128465*/
128466static void schemaIsValid(Parse *pParse){
128467  sqlite3 *db = pParse->db;
128468  int iDb;
128469  int rc;
128470  int cookie;
128471
128472  assert( pParse->checkSchema );
128473  assert( sqlite3_mutex_held(db->mutex) );
128474  for(iDb=0; iDb<db->nDb; iDb++){
128475    int openedTransaction = 0;         /* True if a transaction is opened */
128476    Btree *pBt = db->aDb[iDb].pBt;     /* Btree database to read cookie from */
128477    if( pBt==0 ) continue;
128478
128479    /* If there is not already a read-only (or read-write) transaction opened
128480    ** on the b-tree database, open one now. If a transaction is opened, it
128481    ** will be closed immediately after reading the meta-value. */
128482    if( !sqlite3BtreeIsInReadTrans(pBt) ){
128483      rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
128484      if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
128485        sqlite3OomFault(db);
128486      }
128487      if( rc!=SQLITE_OK ) return;
128488      openedTransaction = 1;
128489    }
128490
128491    /* Read the schema cookie from the database. If it does not match the
128492    ** value stored as part of the in-memory schema representation,
128493    ** set Parse.rc to SQLITE_SCHEMA. */
128494    sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
128495    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
128496    if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
128497      sqlite3ResetOneSchema(db, iDb);
128498      pParse->rc = SQLITE_SCHEMA;
128499    }
128500
128501    /* Close the transaction, if one was opened. */
128502    if( openedTransaction ){
128503      sqlite3BtreeCommit(pBt);
128504    }
128505  }
128506}
128507
128508/*
128509** Convert a schema pointer into the iDb index that indicates
128510** which database file in db->aDb[] the schema refers to.
128511**
128512** If the same database is attached more than once, the first
128513** attached database is returned.
128514*/
128515SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
128516  int i = -1000000;
128517
128518  /* If pSchema is NULL, then return -1000000. This happens when code in
128519  ** expr.c is trying to resolve a reference to a transient table (i.e. one
128520  ** created by a sub-select). In this case the return value of this
128521  ** function should never be used.
128522  **
128523  ** We return -1000000 instead of the more usual -1 simply because using
128524  ** -1000000 as the incorrect index into db->aDb[] is much
128525  ** more likely to cause a segfault than -1 (of course there are assert()
128526  ** statements too, but it never hurts to play the odds).
128527  */
128528  assert( sqlite3_mutex_held(db->mutex) );
128529  if( pSchema ){
128530    for(i=0; 1; i++){
128531      assert( i<db->nDb );
128532      if( db->aDb[i].pSchema==pSchema ){
128533        break;
128534      }
128535    }
128536    assert( i>=0 && i<db->nDb );
128537  }
128538  return i;
128539}
128540
128541/*
128542** Deallocate a single AggInfo object
128543*/
128544static void agginfoFree(sqlite3 *db, AggInfo *p){
128545  sqlite3DbFree(db, p->aCol);
128546  sqlite3DbFree(db, p->aFunc);
128547  sqlite3DbFree(db, p);
128548}
128549
128550/*
128551** Free all memory allocations in the pParse object
128552*/
128553SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
128554  sqlite3 *db = pParse->db;
128555  AggInfo *pThis = pParse->pAggList;
128556  while( pThis ){
128557    AggInfo *pNext = pThis->pNext;
128558    agginfoFree(db, pThis);
128559    pThis = pNext;
128560  }
128561  sqlite3DbFree(db, pParse->aLabel);
128562  sqlite3ExprListDelete(db, pParse->pConstExpr);
128563  if( db ){
128564    assert( db->lookaside.bDisable >= pParse->disableLookaside );
128565    db->lookaside.bDisable -= pParse->disableLookaside;
128566    db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
128567  }
128568  pParse->disableLookaside = 0;
128569}
128570
128571/*
128572** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
128573*/
128574static int sqlite3Prepare(
128575  sqlite3 *db,              /* Database handle. */
128576  const char *zSql,         /* UTF-8 encoded SQL statement. */
128577  int nBytes,               /* Length of zSql in bytes. */
128578  u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */
128579  Vdbe *pReprepare,         /* VM being reprepared */
128580  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
128581  const char **pzTail       /* OUT: End of parsed string */
128582){
128583  char *zErrMsg = 0;        /* Error message */
128584  int rc = SQLITE_OK;       /* Result code */
128585  int i;                    /* Loop counter */
128586  Parse sParse;             /* Parsing context */
128587
128588  memset(&sParse, 0, PARSE_HDR_SZ);
128589  memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ);
128590  sParse.pReprepare = pReprepare;
128591  assert( ppStmt && *ppStmt==0 );
128592  /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
128593  assert( sqlite3_mutex_held(db->mutex) );
128594
128595  /* For a long-term use prepared statement avoid the use of
128596  ** lookaside memory.
128597  */
128598  if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
128599    sParse.disableLookaside++;
128600    DisableLookaside;
128601  }
128602  sParse.disableVtab = (prepFlags & SQLITE_PREPARE_NO_VTAB)!=0;
128603
128604  /* Check to verify that it is possible to get a read lock on all
128605  ** database schemas.  The inability to get a read lock indicates that
128606  ** some other database connection is holding a write-lock, which in
128607  ** turn means that the other connection has made uncommitted changes
128608  ** to the schema.
128609  **
128610  ** Were we to proceed and prepare the statement against the uncommitted
128611  ** schema changes and if those schema changes are subsequently rolled
128612  ** back and different changes are made in their place, then when this
128613  ** prepared statement goes to run the schema cookie would fail to detect
128614  ** the schema change.  Disaster would follow.
128615  **
128616  ** This thread is currently holding mutexes on all Btrees (because
128617  ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
128618  ** is not possible for another thread to start a new schema change
128619  ** while this routine is running.  Hence, we do not need to hold
128620  ** locks on the schema, we just need to make sure nobody else is
128621  ** holding them.
128622  **
128623  ** Note that setting READ_UNCOMMITTED overrides most lock detection,
128624  ** but it does *not* override schema lock detection, so this all still
128625  ** works even if READ_UNCOMMITTED is set.
128626  */
128627  if( !db->noSharedCache ){
128628    for(i=0; i<db->nDb; i++) {
128629      Btree *pBt = db->aDb[i].pBt;
128630      if( pBt ){
128631        assert( sqlite3BtreeHoldsMutex(pBt) );
128632        rc = sqlite3BtreeSchemaLocked(pBt);
128633        if( rc ){
128634          const char *zDb = db->aDb[i].zDbSName;
128635          sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
128636          testcase( db->flags & SQLITE_ReadUncommit );
128637          goto end_prepare;
128638        }
128639      }
128640    }
128641  }
128642
128643  sqlite3VtabUnlockList(db);
128644
128645  sParse.db = db;
128646  if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
128647    char *zSqlCopy;
128648    int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
128649    testcase( nBytes==mxLen );
128650    testcase( nBytes==mxLen+1 );
128651    if( nBytes>mxLen ){
128652      sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");
128653      rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
128654      goto end_prepare;
128655    }
128656    zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
128657    if( zSqlCopy ){
128658      sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
128659      sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
128660      sqlite3DbFree(db, zSqlCopy);
128661    }else{
128662      sParse.zTail = &zSql[nBytes];
128663    }
128664  }else{
128665    sqlite3RunParser(&sParse, zSql, &zErrMsg);
128666  }
128667  assert( 0==sParse.nQueryLoop );
128668
128669  if( sParse.rc==SQLITE_DONE ){
128670    sParse.rc = SQLITE_OK;
128671  }
128672  if( sParse.checkSchema ){
128673    schemaIsValid(&sParse);
128674  }
128675  if( pzTail ){
128676    *pzTail = sParse.zTail;
128677  }
128678
128679  if( db->init.busy==0 ){
128680    sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
128681  }
128682  if( db->mallocFailed ){
128683    sParse.rc = SQLITE_NOMEM_BKPT;
128684  }
128685  rc = sParse.rc;
128686  if( rc!=SQLITE_OK ){
128687    if( sParse.pVdbe ) sqlite3VdbeFinalize(sParse.pVdbe);
128688    assert(!(*ppStmt));
128689  }else{
128690    *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
128691  }
128692
128693  if( zErrMsg ){
128694    sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);
128695    sqlite3DbFree(db, zErrMsg);
128696  }else{
128697    sqlite3Error(db, rc);
128698  }
128699
128700  /* Delete any TriggerPrg structures allocated while parsing this statement. */
128701  while( sParse.pTriggerPrg ){
128702    TriggerPrg *pT = sParse.pTriggerPrg;
128703    sParse.pTriggerPrg = pT->pNext;
128704    sqlite3DbFree(db, pT);
128705  }
128706
128707end_prepare:
128708
128709  sqlite3ParserReset(&sParse);
128710  return rc;
128711}
128712static int sqlite3LockAndPrepare(
128713  sqlite3 *db,              /* Database handle. */
128714  const char *zSql,         /* UTF-8 encoded SQL statement. */
128715  int nBytes,               /* Length of zSql in bytes. */
128716  u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */
128717  Vdbe *pOld,               /* VM being reprepared */
128718  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
128719  const char **pzTail       /* OUT: End of parsed string */
128720){
128721  int rc;
128722  int cnt = 0;
128723
128724#ifdef SQLITE_ENABLE_API_ARMOR
128725  if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
128726#endif
128727  *ppStmt = 0;
128728  if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
128729    return SQLITE_MISUSE_BKPT;
128730  }
128731  sqlite3_mutex_enter(db->mutex);
128732  sqlite3BtreeEnterAll(db);
128733  do{
128734    /* Make multiple attempts to compile the SQL, until it either succeeds
128735    ** or encounters a permanent error.  A schema problem after one schema
128736    ** reset is considered a permanent error. */
128737    rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);
128738    assert( rc==SQLITE_OK || *ppStmt==0 );
128739  }while( rc==SQLITE_ERROR_RETRY
128740       || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
128741  sqlite3BtreeLeaveAll(db);
128742  rc = sqlite3ApiExit(db, rc);
128743  assert( (rc&db->errMask)==rc );
128744  sqlite3_mutex_leave(db->mutex);
128745  return rc;
128746}
128747
128748
128749/*
128750** Rerun the compilation of a statement after a schema change.
128751**
128752** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
128753** if the statement cannot be recompiled because another connection has
128754** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
128755** occurs, return SQLITE_SCHEMA.
128756*/
128757SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
128758  int rc;
128759  sqlite3_stmt *pNew;
128760  const char *zSql;
128761  sqlite3 *db;
128762  u8 prepFlags;
128763
128764  assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
128765  zSql = sqlite3_sql((sqlite3_stmt *)p);
128766  assert( zSql!=0 );  /* Reprepare only called for prepare_v2() statements */
128767  db = sqlite3VdbeDb(p);
128768  assert( sqlite3_mutex_held(db->mutex) );
128769  prepFlags = sqlite3VdbePrepareFlags(p);
128770  rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
128771  if( rc ){
128772    if( rc==SQLITE_NOMEM ){
128773      sqlite3OomFault(db);
128774    }
128775    assert( pNew==0 );
128776    return rc;
128777  }else{
128778    assert( pNew!=0 );
128779  }
128780  sqlite3VdbeSwap((Vdbe*)pNew, p);
128781  sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
128782  sqlite3VdbeResetStepResult((Vdbe*)pNew);
128783  sqlite3VdbeFinalize((Vdbe*)pNew);
128784  return SQLITE_OK;
128785}
128786
128787
128788/*
128789** Two versions of the official API.  Legacy and new use.  In the legacy
128790** version, the original SQL text is not saved in the prepared statement
128791** and so if a schema change occurs, SQLITE_SCHEMA is returned by
128792** sqlite3_step().  In the new version, the original SQL text is retained
128793** and the statement is automatically recompiled if an schema change
128794** occurs.
128795*/
128796SQLITE_API int sqlite3_prepare(
128797  sqlite3 *db,              /* Database handle. */
128798  const char *zSql,         /* UTF-8 encoded SQL statement. */
128799  int nBytes,               /* Length of zSql in bytes. */
128800  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
128801  const char **pzTail       /* OUT: End of parsed string */
128802){
128803  int rc;
128804  rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
128805  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
128806  return rc;
128807}
128808SQLITE_API int sqlite3_prepare_v2(
128809  sqlite3 *db,              /* Database handle. */
128810  const char *zSql,         /* UTF-8 encoded SQL statement. */
128811  int nBytes,               /* Length of zSql in bytes. */
128812  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
128813  const char **pzTail       /* OUT: End of parsed string */
128814){
128815  int rc;
128816  /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
128817  ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags
128818  ** parameter.
128819  **
128820  ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */
128821  rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,0,
128822                             ppStmt,pzTail);
128823  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
128824  return rc;
128825}
128826SQLITE_API int sqlite3_prepare_v3(
128827  sqlite3 *db,              /* Database handle. */
128828  const char *zSql,         /* UTF-8 encoded SQL statement. */
128829  int nBytes,               /* Length of zSql in bytes. */
128830  unsigned int prepFlags,   /* Zero or more SQLITE_PREPARE_* flags */
128831  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
128832  const char **pzTail       /* OUT: End of parsed string */
128833){
128834  int rc;
128835  /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
128836  ** sqlite3_prepare_v2() only in having the extra prepFlags parameter,
128837  ** which is a bit array consisting of zero or more of the
128838  ** SQLITE_PREPARE_* flags.
128839  **
128840  ** Proof by comparison to the implementation of sqlite3_prepare_v2()
128841  ** directly above. */
128842  rc = sqlite3LockAndPrepare(db,zSql,nBytes,
128843                 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
128844                 0,ppStmt,pzTail);
128845  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
128846  return rc;
128847}
128848
128849
128850#ifndef SQLITE_OMIT_UTF16
128851/*
128852** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
128853*/
128854static int sqlite3Prepare16(
128855  sqlite3 *db,              /* Database handle. */
128856  const void *zSql,         /* UTF-16 encoded SQL statement. */
128857  int nBytes,               /* Length of zSql in bytes. */
128858  u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */
128859  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
128860  const void **pzTail       /* OUT: End of parsed string */
128861){
128862  /* This function currently works by first transforming the UTF-16
128863  ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
128864  ** tricky bit is figuring out the pointer to return in *pzTail.
128865  */
128866  char *zSql8;
128867  const char *zTail8 = 0;
128868  int rc = SQLITE_OK;
128869
128870#ifdef SQLITE_ENABLE_API_ARMOR
128871  if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
128872#endif
128873  *ppStmt = 0;
128874  if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
128875    return SQLITE_MISUSE_BKPT;
128876  }
128877  if( nBytes>=0 ){
128878    int sz;
128879    const char *z = (const char*)zSql;
128880    for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
128881    nBytes = sz;
128882  }
128883  sqlite3_mutex_enter(db->mutex);
128884  zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
128885  if( zSql8 ){
128886    rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
128887  }
128888
128889  if( zTail8 && pzTail ){
128890    /* If sqlite3_prepare returns a tail pointer, we calculate the
128891    ** equivalent pointer into the UTF-16 string by counting the unicode
128892    ** characters between zSql8 and zTail8, and then returning a pointer
128893    ** the same number of characters into the UTF-16 string.
128894    */
128895    int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
128896    *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
128897  }
128898  sqlite3DbFree(db, zSql8);
128899  rc = sqlite3ApiExit(db, rc);
128900  sqlite3_mutex_leave(db->mutex);
128901  return rc;
128902}
128903
128904/*
128905** Two versions of the official API.  Legacy and new use.  In the legacy
128906** version, the original SQL text is not saved in the prepared statement
128907** and so if a schema change occurs, SQLITE_SCHEMA is returned by
128908** sqlite3_step().  In the new version, the original SQL text is retained
128909** and the statement is automatically recompiled if an schema change
128910** occurs.
128911*/
128912SQLITE_API int sqlite3_prepare16(
128913  sqlite3 *db,              /* Database handle. */
128914  const void *zSql,         /* UTF-16 encoded SQL statement. */
128915  int nBytes,               /* Length of zSql in bytes. */
128916  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
128917  const void **pzTail       /* OUT: End of parsed string */
128918){
128919  int rc;
128920  rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
128921  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
128922  return rc;
128923}
128924SQLITE_API int sqlite3_prepare16_v2(
128925  sqlite3 *db,              /* Database handle. */
128926  const void *zSql,         /* UTF-16 encoded SQL statement. */
128927  int nBytes,               /* Length of zSql in bytes. */
128928  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
128929  const void **pzTail       /* OUT: End of parsed string */
128930){
128931  int rc;
128932  rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,ppStmt,pzTail);
128933  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
128934  return rc;
128935}
128936SQLITE_API int sqlite3_prepare16_v3(
128937  sqlite3 *db,              /* Database handle. */
128938  const void *zSql,         /* UTF-16 encoded SQL statement. */
128939  int nBytes,               /* Length of zSql in bytes. */
128940  unsigned int prepFlags,   /* Zero or more SQLITE_PREPARE_* flags */
128941  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
128942  const void **pzTail       /* OUT: End of parsed string */
128943){
128944  int rc;
128945  rc = sqlite3Prepare16(db,zSql,nBytes,
128946         SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
128947         ppStmt,pzTail);
128948  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
128949  return rc;
128950}
128951
128952#endif /* SQLITE_OMIT_UTF16 */
128953
128954/************** End of prepare.c *********************************************/
128955/************** Begin file select.c ******************************************/
128956/*
128957** 2001 September 15
128958**
128959** The author disclaims copyright to this source code.  In place of
128960** a legal notice, here is a blessing:
128961**
128962**    May you do good and not evil.
128963**    May you find forgiveness for yourself and forgive others.
128964**    May you share freely, never taking more than you give.
128965**
128966*************************************************************************
128967** This file contains C code routines that are called by the parser
128968** to handle SELECT statements in SQLite.
128969*/
128970/* #include "sqliteInt.h" */
128971
128972/*
128973** Trace output macros
128974*/
128975#if SELECTTRACE_ENABLED
128976/***/ int sqlite3SelectTrace = 0;
128977# define SELECTTRACE(K,P,S,X)  \
128978  if(sqlite3SelectTrace&(K))   \
128979    sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
128980    sqlite3DebugPrintf X
128981#else
128982# define SELECTTRACE(K,P,S,X)
128983#endif
128984
128985
128986/*
128987** An instance of the following object is used to record information about
128988** how to process the DISTINCT keyword, to simplify passing that information
128989** into the selectInnerLoop() routine.
128990*/
128991typedef struct DistinctCtx DistinctCtx;
128992struct DistinctCtx {
128993  u8 isTnct;      /* True if the DISTINCT keyword is present */
128994  u8 eTnctType;   /* One of the WHERE_DISTINCT_* operators */
128995  int tabTnct;    /* Ephemeral table used for DISTINCT processing */
128996  int addrTnct;   /* Address of OP_OpenEphemeral opcode for tabTnct */
128997};
128998
128999/*
129000** An instance of the following object is used to record information about
129001** the ORDER BY (or GROUP BY) clause of query is being coded.
129002**
129003** The aDefer[] array is used by the sorter-references optimization. For
129004** example, assuming there is no index that can be used for the ORDER BY,
129005** for the query:
129006**
129007**     SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;
129008**
129009** it may be more efficient to add just the "a" values to the sorter, and
129010** retrieve the associated "bigblob" values directly from table t1 as the
129011** 10 smallest "a" values are extracted from the sorter.
129012**
129013** When the sorter-reference optimization is used, there is one entry in the
129014** aDefer[] array for each database table that may be read as values are
129015** extracted from the sorter.
129016*/
129017typedef struct SortCtx SortCtx;
129018struct SortCtx {
129019  ExprList *pOrderBy;   /* The ORDER BY (or GROUP BY clause) */
129020  int nOBSat;           /* Number of ORDER BY terms satisfied by indices */
129021  int iECursor;         /* Cursor number for the sorter */
129022  int regReturn;        /* Register holding block-output return address */
129023  int labelBkOut;       /* Start label for the block-output subroutine */
129024  int addrSortIndex;    /* Address of the OP_SorterOpen or OP_OpenEphemeral */
129025  int labelDone;        /* Jump here when done, ex: LIMIT reached */
129026  int labelOBLopt;      /* Jump here when sorter is full */
129027  u8 sortFlags;         /* Zero or more SORTFLAG_* bits */
129028#ifdef SQLITE_ENABLE_SORTER_REFERENCES
129029  u8 nDefer;            /* Number of valid entries in aDefer[] */
129030  struct DeferredCsr {
129031    Table *pTab;        /* Table definition */
129032    int iCsr;           /* Cursor number for table */
129033    int nKey;           /* Number of PK columns for table pTab (>=1) */
129034  } aDefer[4];
129035#endif
129036  struct RowLoadInfo *pDeferredRowLoad;  /* Deferred row loading info or NULL */
129037};
129038#define SORTFLAG_UseSorter  0x01   /* Use SorterOpen instead of OpenEphemeral */
129039
129040/*
129041** Delete all the content of a Select structure.  Deallocate the structure
129042** itself depending on the value of bFree
129043**
129044** If bFree==1, call sqlite3DbFree() on the p object.
129045** If bFree==0, Leave the first Select object unfreed
129046*/
129047static void clearSelect(sqlite3 *db, Select *p, int bFree){
129048  while( p ){
129049    Select *pPrior = p->pPrior;
129050    sqlite3ExprListDelete(db, p->pEList);
129051    sqlite3SrcListDelete(db, p->pSrc);
129052    sqlite3ExprDelete(db, p->pWhere);
129053    sqlite3ExprListDelete(db, p->pGroupBy);
129054    sqlite3ExprDelete(db, p->pHaving);
129055    sqlite3ExprListDelete(db, p->pOrderBy);
129056    sqlite3ExprDelete(db, p->pLimit);
129057#ifndef SQLITE_OMIT_WINDOWFUNC
129058    if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
129059      sqlite3WindowListDelete(db, p->pWinDefn);
129060    }
129061#endif
129062    if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
129063    if( bFree ) sqlite3DbFreeNN(db, p);
129064    p = pPrior;
129065    bFree = 1;
129066  }
129067}
129068
129069/*
129070** Initialize a SelectDest structure.
129071*/
129072SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
129073  pDest->eDest = (u8)eDest;
129074  pDest->iSDParm = iParm;
129075  pDest->zAffSdst = 0;
129076  pDest->iSdst = 0;
129077  pDest->nSdst = 0;
129078}
129079
129080
129081/*
129082** Allocate a new Select structure and return a pointer to that
129083** structure.
129084*/
129085SQLITE_PRIVATE Select *sqlite3SelectNew(
129086  Parse *pParse,        /* Parsing context */
129087  ExprList *pEList,     /* which columns to include in the result */
129088  SrcList *pSrc,        /* the FROM clause -- which tables to scan */
129089  Expr *pWhere,         /* the WHERE clause */
129090  ExprList *pGroupBy,   /* the GROUP BY clause */
129091  Expr *pHaving,        /* the HAVING clause */
129092  ExprList *pOrderBy,   /* the ORDER BY clause */
129093  u32 selFlags,         /* Flag parameters, such as SF_Distinct */
129094  Expr *pLimit          /* LIMIT value.  NULL means not used */
129095){
129096  Select *pNew;
129097  Select standin;
129098  pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
129099  if( pNew==0 ){
129100    assert( pParse->db->mallocFailed );
129101    pNew = &standin;
129102  }
129103  if( pEList==0 ){
129104    pEList = sqlite3ExprListAppend(pParse, 0,
129105                                   sqlite3Expr(pParse->db,TK_ASTERISK,0));
129106  }
129107  pNew->pEList = pEList;
129108  pNew->op = TK_SELECT;
129109  pNew->selFlags = selFlags;
129110  pNew->iLimit = 0;
129111  pNew->iOffset = 0;
129112  pNew->selId = ++pParse->nSelect;
129113  pNew->addrOpenEphm[0] = -1;
129114  pNew->addrOpenEphm[1] = -1;
129115  pNew->nSelectRow = 0;
129116  if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
129117  pNew->pSrc = pSrc;
129118  pNew->pWhere = pWhere;
129119  pNew->pGroupBy = pGroupBy;
129120  pNew->pHaving = pHaving;
129121  pNew->pOrderBy = pOrderBy;
129122  pNew->pPrior = 0;
129123  pNew->pNext = 0;
129124  pNew->pLimit = pLimit;
129125  pNew->pWith = 0;
129126#ifndef SQLITE_OMIT_WINDOWFUNC
129127  pNew->pWin = 0;
129128  pNew->pWinDefn = 0;
129129#endif
129130  if( pParse->db->mallocFailed ) {
129131    clearSelect(pParse->db, pNew, pNew!=&standin);
129132    pNew = 0;
129133  }else{
129134    assert( pNew->pSrc!=0 || pParse->nErr>0 );
129135  }
129136  assert( pNew!=&standin );
129137  return pNew;
129138}
129139
129140
129141/*
129142** Delete the given Select structure and all of its substructures.
129143*/
129144SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
129145  if( OK_IF_ALWAYS_TRUE(p) ) clearSelect(db, p, 1);
129146}
129147
129148/*
129149** Delete all the substructure for p, but keep p allocated.  Redefine
129150** p to be a single SELECT where every column of the result set has a
129151** value of NULL.
129152*/
129153SQLITE_PRIVATE void sqlite3SelectReset(Parse *pParse, Select *p){
129154  if( ALWAYS(p) ){
129155    clearSelect(pParse->db, p, 0);
129156    memset(&p->iLimit, 0, sizeof(Select) - offsetof(Select,iLimit));
129157    p->pEList = sqlite3ExprListAppend(pParse, 0,
129158                     sqlite3ExprAlloc(pParse->db,TK_NULL,0,0));
129159    p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(SrcList));
129160  }
129161}
129162
129163/*
129164** Return a pointer to the right-most SELECT statement in a compound.
129165*/
129166static Select *findRightmost(Select *p){
129167  while( p->pNext ) p = p->pNext;
129168  return p;
129169}
129170
129171/*
129172** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
129173** type of join.  Return an integer constant that expresses that type
129174** in terms of the following bit values:
129175**
129176**     JT_INNER
129177**     JT_CROSS
129178**     JT_OUTER
129179**     JT_NATURAL
129180**     JT_LEFT
129181**     JT_RIGHT
129182**
129183** A full outer join is the combination of JT_LEFT and JT_RIGHT.
129184**
129185** If an illegal or unsupported join type is seen, then still return
129186** a join type, but put an error in the pParse structure.
129187*/
129188SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
129189  int jointype = 0;
129190  Token *apAll[3];
129191  Token *p;
129192                             /*   0123456789 123456789 123456789 123 */
129193  static const char zKeyText[] = "naturaleftouterightfullinnercross";
129194  static const struct {
129195    u8 i;        /* Beginning of keyword text in zKeyText[] */
129196    u8 nChar;    /* Length of the keyword in characters */
129197    u8 code;     /* Join type mask */
129198  } aKeyword[] = {
129199    /* natural */ { 0,  7, JT_NATURAL                },
129200    /* left    */ { 6,  4, JT_LEFT|JT_OUTER          },
129201    /* outer   */ { 10, 5, JT_OUTER                  },
129202    /* right   */ { 14, 5, JT_RIGHT|JT_OUTER         },
129203    /* full    */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
129204    /* inner   */ { 23, 5, JT_INNER                  },
129205    /* cross   */ { 28, 5, JT_INNER|JT_CROSS         },
129206  };
129207  int i, j;
129208  apAll[0] = pA;
129209  apAll[1] = pB;
129210  apAll[2] = pC;
129211  for(i=0; i<3 && apAll[i]; i++){
129212    p = apAll[i];
129213    for(j=0; j<ArraySize(aKeyword); j++){
129214      if( p->n==aKeyword[j].nChar
129215          && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
129216        jointype |= aKeyword[j].code;
129217        break;
129218      }
129219    }
129220    testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
129221    if( j>=ArraySize(aKeyword) ){
129222      jointype |= JT_ERROR;
129223      break;
129224    }
129225  }
129226  if(
129227     (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
129228     (jointype & JT_ERROR)!=0
129229  ){
129230    const char *zSp = " ";
129231    assert( pB!=0 );
129232    if( pC==0 ){ zSp++; }
129233    sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
129234       "%T %T%s%T", pA, pB, zSp, pC);
129235    jointype = JT_INNER;
129236  }else if( (jointype & JT_OUTER)!=0
129237         && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
129238    sqlite3ErrorMsg(pParse,
129239      "RIGHT and FULL OUTER JOINs are not currently supported");
129240    jointype = JT_INNER;
129241  }
129242  return jointype;
129243}
129244
129245/*
129246** Return the index of a column in a table.  Return -1 if the column
129247** is not contained in the table.
129248*/
129249static int columnIndex(Table *pTab, const char *zCol){
129250  int i;
129251  for(i=0; i<pTab->nCol; i++){
129252    if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
129253  }
129254  return -1;
129255}
129256
129257/*
129258** Search the first N tables in pSrc, from left to right, looking for a
129259** table that has a column named zCol.
129260**
129261** When found, set *piTab and *piCol to the table index and column index
129262** of the matching column and return TRUE.
129263**
129264** If not found, return FALSE.
129265*/
129266static int tableAndColumnIndex(
129267  SrcList *pSrc,       /* Array of tables to search */
129268  int N,               /* Number of tables in pSrc->a[] to search */
129269  const char *zCol,    /* Name of the column we are looking for */
129270  int *piTab,          /* Write index of pSrc->a[] here */
129271  int *piCol,          /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
129272  int bIgnoreHidden    /* True to ignore hidden columns */
129273){
129274  int i;               /* For looping over tables in pSrc */
129275  int iCol;            /* Index of column matching zCol */
129276
129277  assert( (piTab==0)==(piCol==0) );  /* Both or neither are NULL */
129278  for(i=0; i<N; i++){
129279    iCol = columnIndex(pSrc->a[i].pTab, zCol);
129280    if( iCol>=0
129281     && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])==0)
129282    ){
129283      if( piTab ){
129284        *piTab = i;
129285        *piCol = iCol;
129286      }
129287      return 1;
129288    }
129289  }
129290  return 0;
129291}
129292
129293/*
129294** This function is used to add terms implied by JOIN syntax to the
129295** WHERE clause expression of a SELECT statement. The new term, which
129296** is ANDed with the existing WHERE clause, is of the form:
129297**
129298**    (tab1.col1 = tab2.col2)
129299**
129300** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
129301** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
129302** column iColRight of tab2.
129303*/
129304static void addWhereTerm(
129305  Parse *pParse,                  /* Parsing context */
129306  SrcList *pSrc,                  /* List of tables in FROM clause */
129307  int iLeft,                      /* Index of first table to join in pSrc */
129308  int iColLeft,                   /* Index of column in first table */
129309  int iRight,                     /* Index of second table in pSrc */
129310  int iColRight,                  /* Index of column in second table */
129311  int isOuterJoin,                /* True if this is an OUTER join */
129312  Expr **ppWhere                  /* IN/OUT: The WHERE clause to add to */
129313){
129314  sqlite3 *db = pParse->db;
129315  Expr *pE1;
129316  Expr *pE2;
129317  Expr *pEq;
129318
129319  assert( iLeft<iRight );
129320  assert( pSrc->nSrc>iRight );
129321  assert( pSrc->a[iLeft].pTab );
129322  assert( pSrc->a[iRight].pTab );
129323
129324  pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
129325  pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
129326
129327  pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2);
129328  if( pEq && isOuterJoin ){
129329    ExprSetProperty(pEq, EP_FromJoin);
129330    assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
129331    ExprSetVVAProperty(pEq, EP_NoReduce);
129332    pEq->iRightJoinTable = (i16)pE2->iTable;
129333  }
129334  *ppWhere = sqlite3ExprAnd(pParse, *ppWhere, pEq);
129335}
129336
129337/*
129338** Set the EP_FromJoin property on all terms of the given expression.
129339** And set the Expr.iRightJoinTable to iTable for every term in the
129340** expression.
129341**
129342** The EP_FromJoin property is used on terms of an expression to tell
129343** the LEFT OUTER JOIN processing logic that this term is part of the
129344** join restriction specified in the ON or USING clause and not a part
129345** of the more general WHERE clause.  These terms are moved over to the
129346** WHERE clause during join processing but we need to remember that they
129347** originated in the ON or USING clause.
129348**
129349** The Expr.iRightJoinTable tells the WHERE clause processing that the
129350** expression depends on table iRightJoinTable even if that table is not
129351** explicitly mentioned in the expression.  That information is needed
129352** for cases like this:
129353**
129354**    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
129355**
129356** The where clause needs to defer the handling of the t1.x=5
129357** term until after the t2 loop of the join.  In that way, a
129358** NULL t2 row will be inserted whenever t1.x!=5.  If we do not
129359** defer the handling of t1.x=5, it will be processed immediately
129360** after the t1 loop and rows with t1.x!=5 will never appear in
129361** the output, which is incorrect.
129362*/
129363SQLITE_PRIVATE void sqlite3SetJoinExpr(Expr *p, int iTable){
129364  while( p ){
129365    ExprSetProperty(p, EP_FromJoin);
129366    assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
129367    ExprSetVVAProperty(p, EP_NoReduce);
129368    p->iRightJoinTable = (i16)iTable;
129369    if( p->op==TK_FUNCTION && p->x.pList ){
129370      int i;
129371      for(i=0; i<p->x.pList->nExpr; i++){
129372        sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable);
129373      }
129374    }
129375    sqlite3SetJoinExpr(p->pLeft, iTable);
129376    p = p->pRight;
129377  }
129378}
129379
129380/* Undo the work of sqlite3SetJoinExpr(). In the expression p, convert every
129381** term that is marked with EP_FromJoin and iRightJoinTable==iTable into
129382** an ordinary term that omits the EP_FromJoin mark.
129383**
129384** This happens when a LEFT JOIN is simplified into an ordinary JOIN.
129385*/
129386static void unsetJoinExpr(Expr *p, int iTable){
129387  while( p ){
129388    if( ExprHasProperty(p, EP_FromJoin)
129389     && (iTable<0 || p->iRightJoinTable==iTable) ){
129390      ExprClearProperty(p, EP_FromJoin);
129391    }
129392    if( p->op==TK_FUNCTION && p->x.pList ){
129393      int i;
129394      for(i=0; i<p->x.pList->nExpr; i++){
129395        unsetJoinExpr(p->x.pList->a[i].pExpr, iTable);
129396      }
129397    }
129398    unsetJoinExpr(p->pLeft, iTable);
129399    p = p->pRight;
129400  }
129401}
129402
129403/*
129404** This routine processes the join information for a SELECT statement.
129405** ON and USING clauses are converted into extra terms of the WHERE clause.
129406** NATURAL joins also create extra WHERE clause terms.
129407**
129408** The terms of a FROM clause are contained in the Select.pSrc structure.
129409** The left most table is the first entry in Select.pSrc.  The right-most
129410** table is the last entry.  The join operator is held in the entry to
129411** the left.  Thus entry 0 contains the join operator for the join between
129412** entries 0 and 1.  Any ON or USING clauses associated with the join are
129413** also attached to the left entry.
129414**
129415** This routine returns the number of errors encountered.
129416*/
129417static int sqliteProcessJoin(Parse *pParse, Select *p){
129418  SrcList *pSrc;                  /* All tables in the FROM clause */
129419  int i, j;                       /* Loop counters */
129420  struct SrcList_item *pLeft;     /* Left table being joined */
129421  struct SrcList_item *pRight;    /* Right table being joined */
129422
129423  pSrc = p->pSrc;
129424  pLeft = &pSrc->a[0];
129425  pRight = &pLeft[1];
129426  for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
129427    Table *pRightTab = pRight->pTab;
129428    int isOuter;
129429
129430    if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
129431    isOuter = (pRight->fg.jointype & JT_OUTER)!=0;
129432
129433    /* When the NATURAL keyword is present, add WHERE clause terms for
129434    ** every column that the two tables have in common.
129435    */
129436    if( pRight->fg.jointype & JT_NATURAL ){
129437      if( pRight->pOn || pRight->pUsing ){
129438        sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
129439           "an ON or USING clause", 0);
129440        return 1;
129441      }
129442      for(j=0; j<pRightTab->nCol; j++){
129443        char *zName;   /* Name of column in the right table */
129444        int iLeft;     /* Matching left table */
129445        int iLeftCol;  /* Matching column in the left table */
129446
129447        if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;
129448        zName = pRightTab->aCol[j].zName;
129449        if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol, 1) ){
129450          addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
129451                isOuter, &p->pWhere);
129452        }
129453      }
129454    }
129455
129456    /* Disallow both ON and USING clauses in the same join
129457    */
129458    if( pRight->pOn && pRight->pUsing ){
129459      sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
129460        "clauses in the same join");
129461      return 1;
129462    }
129463
129464    /* Add the ON clause to the end of the WHERE clause, connected by
129465    ** an AND operator.
129466    */
129467    if( pRight->pOn ){
129468      if( isOuter ) sqlite3SetJoinExpr(pRight->pOn, pRight->iCursor);
129469      p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->pOn);
129470      pRight->pOn = 0;
129471    }
129472
129473    /* Create extra terms on the WHERE clause for each column named
129474    ** in the USING clause.  Example: If the two tables to be joined are
129475    ** A and B and the USING clause names X, Y, and Z, then add this
129476    ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
129477    ** Report an error if any column mentioned in the USING clause is
129478    ** not contained in both tables to be joined.
129479    */
129480    if( pRight->pUsing ){
129481      IdList *pList = pRight->pUsing;
129482      for(j=0; j<pList->nId; j++){
129483        char *zName;     /* Name of the term in the USING clause */
129484        int iLeft;       /* Table on the left with matching column name */
129485        int iLeftCol;    /* Column number of matching column on the left */
129486        int iRightCol;   /* Column number of matching column on the right */
129487
129488        zName = pList->a[j].zName;
129489        iRightCol = columnIndex(pRightTab, zName);
129490        if( iRightCol<0
129491         || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol, 0)
129492        ){
129493          sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
129494            "not present in both tables", zName);
129495          return 1;
129496        }
129497        addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
129498                     isOuter, &p->pWhere);
129499      }
129500    }
129501  }
129502  return 0;
129503}
129504
129505/*
129506** An instance of this object holds information (beyond pParse and pSelect)
129507** needed to load the next result row that is to be added to the sorter.
129508*/
129509typedef struct RowLoadInfo RowLoadInfo;
129510struct RowLoadInfo {
129511  int regResult;               /* Store results in array of registers here */
129512  u8 ecelFlags;                /* Flag argument to ExprCodeExprList() */
129513#ifdef SQLITE_ENABLE_SORTER_REFERENCES
129514  ExprList *pExtra;            /* Extra columns needed by sorter refs */
129515  int regExtraResult;          /* Where to load the extra columns */
129516#endif
129517};
129518
129519/*
129520** This routine does the work of loading query data into an array of
129521** registers so that it can be added to the sorter.
129522*/
129523static void innerLoopLoadRow(
129524  Parse *pParse,             /* Statement under construction */
129525  Select *pSelect,           /* The query being coded */
129526  RowLoadInfo *pInfo         /* Info needed to complete the row load */
129527){
129528  sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
129529                          0, pInfo->ecelFlags);
129530#ifdef SQLITE_ENABLE_SORTER_REFERENCES
129531  if( pInfo->pExtra ){
129532    sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
129533    sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
129534  }
129535#endif
129536}
129537
129538/*
129539** Code the OP_MakeRecord instruction that generates the entry to be
129540** added into the sorter.
129541**
129542** Return the register in which the result is stored.
129543*/
129544static int makeSorterRecord(
129545  Parse *pParse,
129546  SortCtx *pSort,
129547  Select *pSelect,
129548  int regBase,
129549  int nBase
129550){
129551  int nOBSat = pSort->nOBSat;
129552  Vdbe *v = pParse->pVdbe;
129553  int regOut = ++pParse->nMem;
129554  if( pSort->pDeferredRowLoad ){
129555    innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
129556  }
129557  sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
129558  return regOut;
129559}
129560
129561/*
129562** Generate code that will push the record in registers regData
129563** through regData+nData-1 onto the sorter.
129564*/
129565static void pushOntoSorter(
129566  Parse *pParse,         /* Parser context */
129567  SortCtx *pSort,        /* Information about the ORDER BY clause */
129568  Select *pSelect,       /* The whole SELECT statement */
129569  int regData,           /* First register holding data to be sorted */
129570  int regOrigData,       /* First register holding data before packing */
129571  int nData,             /* Number of elements in the regData data array */
129572  int nPrefixReg         /* No. of reg prior to regData available for use */
129573){
129574  Vdbe *v = pParse->pVdbe;                         /* Stmt under construction */
129575  int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
129576  int nExpr = pSort->pOrderBy->nExpr;              /* No. of ORDER BY terms */
129577  int nBase = nExpr + bSeq + nData;                /* Fields in sorter record */
129578  int regBase;                                     /* Regs for sorter record */
129579  int regRecord = 0;                               /* Assembled sorter record */
129580  int nOBSat = pSort->nOBSat;                      /* ORDER BY terms to skip */
129581  int op;                            /* Opcode to add sorter record to sorter */
129582  int iLimit;                        /* LIMIT counter */
129583  int iSkip = 0;                     /* End of the sorter insert loop */
129584
129585  assert( bSeq==0 || bSeq==1 );
129586
129587  /* Three cases:
129588  **   (1) The data to be sorted has already been packed into a Record
129589  **       by a prior OP_MakeRecord.  In this case nData==1 and regData
129590  **       will be completely unrelated to regOrigData.
129591  **   (2) All output columns are included in the sort record.  In that
129592  **       case regData==regOrigData.
129593  **   (3) Some output columns are omitted from the sort record due to
129594  **       the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the
129595  **       SQLITE_ECEL_OMITREF optimization, or due to the
129596  **       SortCtx.pDeferredRowLoad optimiation.  In any of these cases
129597  **       regOrigData is 0 to prevent this routine from trying to copy
129598  **       values that might not yet exist.
129599  */
129600  assert( nData==1 || regData==regOrigData || regOrigData==0 );
129601
129602  if( nPrefixReg ){
129603    assert( nPrefixReg==nExpr+bSeq );
129604    regBase = regData - nPrefixReg;
129605  }else{
129606    regBase = pParse->nMem + 1;
129607    pParse->nMem += nBase;
129608  }
129609  assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
129610  iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
129611  pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
129612  sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
129613                          SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0));
129614  if( bSeq ){
129615    sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
129616  }
129617  if( nPrefixReg==0 && nData>0 ){
129618    sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
129619  }
129620  if( nOBSat>0 ){
129621    int regPrevKey;   /* The first nOBSat columns of the previous row */
129622    int addrFirst;    /* Address of the OP_IfNot opcode */
129623    int addrJmp;      /* Address of the OP_Jump opcode */
129624    VdbeOp *pOp;      /* Opcode that opens the sorter */
129625    int nKey;         /* Number of sorting key columns, including OP_Sequence */
129626    KeyInfo *pKI;     /* Original KeyInfo on the sorter table */
129627
129628    regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
129629    regPrevKey = pParse->nMem+1;
129630    pParse->nMem += pSort->nOBSat;
129631    nKey = nExpr - pSort->nOBSat + bSeq;
129632    if( bSeq ){
129633      addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr);
129634    }else{
129635      addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
129636    }
129637    VdbeCoverage(v);
129638    sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
129639    pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
129640    if( pParse->db->mallocFailed ) return;
129641    pOp->p2 = nKey + nData;
129642    pKI = pOp->p4.pKeyInfo;
129643    memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
129644    sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
129645    testcase( pKI->nAllField > pKI->nKeyField+2 );
129646    pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
129647                                           pKI->nAllField-pKI->nKeyField-1);
129648    pOp = 0; /* Ensure pOp not used after sqltie3VdbeAddOp3() */
129649    addrJmp = sqlite3VdbeCurrentAddr(v);
129650    sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
129651    pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
129652    pSort->regReturn = ++pParse->nMem;
129653    sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
129654    sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
129655    if( iLimit ){
129656      sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
129657      VdbeCoverage(v);
129658    }
129659    sqlite3VdbeJumpHere(v, addrFirst);
129660    sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
129661    sqlite3VdbeJumpHere(v, addrJmp);
129662  }
129663  if( iLimit ){
129664    /* At this point the values for the new sorter entry are stored
129665    ** in an array of registers. They need to be composed into a record
129666    ** and inserted into the sorter if either (a) there are currently
129667    ** less than LIMIT+OFFSET items or (b) the new record is smaller than
129668    ** the largest record currently in the sorter. If (b) is true and there
129669    ** are already LIMIT+OFFSET items in the sorter, delete the largest
129670    ** entry before inserting the new one. This way there are never more
129671    ** than LIMIT+OFFSET items in the sorter.
129672    **
129673    ** If the new record does not need to be inserted into the sorter,
129674    ** jump to the next iteration of the loop. If the pSort->labelOBLopt
129675    ** value is not zero, then it is a label of where to jump.  Otherwise,
129676    ** just bypass the row insert logic.  See the header comment on the
129677    ** sqlite3WhereOrderByLimitOptLabel() function for additional info.
129678    */
129679    int iCsr = pSort->iECursor;
129680    sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4);
129681    VdbeCoverage(v);
129682    sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0);
129683    iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE,
129684                                 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
129685    VdbeCoverage(v);
129686    sqlite3VdbeAddOp1(v, OP_Delete, iCsr);
129687  }
129688  if( regRecord==0 ){
129689    regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
129690  }
129691  if( pSort->sortFlags & SORTFLAG_UseSorter ){
129692    op = OP_SorterInsert;
129693  }else{
129694    op = OP_IdxInsert;
129695  }
129696  sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
129697                       regBase+nOBSat, nBase-nOBSat);
129698  if( iSkip ){
129699    sqlite3VdbeChangeP2(v, iSkip,
129700         pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
129701  }
129702}
129703
129704/*
129705** Add code to implement the OFFSET
129706*/
129707static void codeOffset(
129708  Vdbe *v,          /* Generate code into this VM */
129709  int iOffset,      /* Register holding the offset counter */
129710  int iContinue     /* Jump here to skip the current record */
129711){
129712  if( iOffset>0 ){
129713    sqlite3VdbeAddOp3(v, OP_IfPos, iOffset, iContinue, 1); VdbeCoverage(v);
129714    VdbeComment((v, "OFFSET"));
129715  }
129716}
129717
129718/*
129719** Add code that will check to make sure the N registers starting at iMem
129720** form a distinct entry.  iTab is a sorting index that holds previously
129721** seen combinations of the N values.  A new entry is made in iTab
129722** if the current N values are new.
129723**
129724** A jump to addrRepeat is made and the N+1 values are popped from the
129725** stack if the top N elements are not distinct.
129726*/
129727static void codeDistinct(
129728  Parse *pParse,     /* Parsing and code generating context */
129729  int iTab,          /* A sorting index used to test for distinctness */
129730  int addrRepeat,    /* Jump to here if not distinct */
129731  int N,             /* Number of elements */
129732  int iMem           /* First element */
129733){
129734  Vdbe *v;
129735  int r1;
129736
129737  v = pParse->pVdbe;
129738  r1 = sqlite3GetTempReg(pParse);
129739  sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
129740  sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
129741  sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N);
129742  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
129743  sqlite3ReleaseTempReg(pParse, r1);
129744}
129745
129746#ifdef SQLITE_ENABLE_SORTER_REFERENCES
129747/*
129748** This function is called as part of inner-loop generation for a SELECT
129749** statement with an ORDER BY that is not optimized by an index. It
129750** determines the expressions, if any, that the sorter-reference
129751** optimization should be used for. The sorter-reference optimization
129752** is used for SELECT queries like:
129753**
129754**   SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10
129755**
129756** If the optimization is used for expression "bigblob", then instead of
129757** storing values read from that column in the sorter records, the PK of
129758** the row from table t1 is stored instead. Then, as records are extracted from
129759** the sorter to return to the user, the required value of bigblob is
129760** retrieved directly from table t1. If the values are very large, this
129761** can be more efficient than storing them directly in the sorter records.
129762**
129763** The ExprList_item.bSorterRef flag is set for each expression in pEList
129764** for which the sorter-reference optimization should be enabled.
129765** Additionally, the pSort->aDefer[] array is populated with entries
129766** for all cursors required to evaluate all selected expressions. Finally.
129767** output variable (*ppExtra) is set to an expression list containing
129768** expressions for all extra PK values that should be stored in the
129769** sorter records.
129770*/
129771static void selectExprDefer(
129772  Parse *pParse,                  /* Leave any error here */
129773  SortCtx *pSort,                 /* Sorter context */
129774  ExprList *pEList,               /* Expressions destined for sorter */
129775  ExprList **ppExtra              /* Expressions to append to sorter record */
129776){
129777  int i;
129778  int nDefer = 0;
129779  ExprList *pExtra = 0;
129780  for(i=0; i<pEList->nExpr; i++){
129781    struct ExprList_item *pItem = &pEList->a[i];
129782    if( pItem->u.x.iOrderByCol==0 ){
129783      Expr *pExpr = pItem->pExpr;
129784      Table *pTab = pExpr->y.pTab;
129785      if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 && pTab && !IsVirtual(pTab)
129786       && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
129787      ){
129788        int j;
129789        for(j=0; j<nDefer; j++){
129790          if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
129791        }
129792        if( j==nDefer ){
129793          if( nDefer==ArraySize(pSort->aDefer) ){
129794            continue;
129795          }else{
129796            int nKey = 1;
129797            int k;
129798            Index *pPk = 0;
129799            if( !HasRowid(pTab) ){
129800              pPk = sqlite3PrimaryKeyIndex(pTab);
129801              nKey = pPk->nKeyCol;
129802            }
129803            for(k=0; k<nKey; k++){
129804              Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0);
129805              if( pNew ){
129806                pNew->iTable = pExpr->iTable;
129807                pNew->y.pTab = pExpr->y.pTab;
129808                pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
129809                pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
129810              }
129811            }
129812            pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
129813            pSort->aDefer[nDefer].iCsr = pExpr->iTable;
129814            pSort->aDefer[nDefer].nKey = nKey;
129815            nDefer++;
129816          }
129817        }
129818        pItem->bSorterRef = 1;
129819      }
129820    }
129821  }
129822  pSort->nDefer = (u8)nDefer;
129823  *ppExtra = pExtra;
129824}
129825#endif
129826
129827/*
129828** This routine generates the code for the inside of the inner loop
129829** of a SELECT.
129830**
129831** If srcTab is negative, then the p->pEList expressions
129832** are evaluated in order to get the data for this row.  If srcTab is
129833** zero or more, then data is pulled from srcTab and p->pEList is used only
129834** to get the number of columns and the collation sequence for each column.
129835*/
129836static void selectInnerLoop(
129837  Parse *pParse,          /* The parser context */
129838  Select *p,              /* The complete select statement being coded */
129839  int srcTab,             /* Pull data from this table if non-negative */
129840  SortCtx *pSort,         /* If not NULL, info on how to process ORDER BY */
129841  DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
129842  SelectDest *pDest,      /* How to dispose of the results */
129843  int iContinue,          /* Jump here to continue with next row */
129844  int iBreak              /* Jump here to break out of the inner loop */
129845){
129846  Vdbe *v = pParse->pVdbe;
129847  int i;
129848  int hasDistinct;            /* True if the DISTINCT keyword is present */
129849  int eDest = pDest->eDest;   /* How to dispose of results */
129850  int iParm = pDest->iSDParm; /* First argument to disposal method */
129851  int nResultCol;             /* Number of result columns */
129852  int nPrefixReg = 0;         /* Number of extra registers before regResult */
129853  RowLoadInfo sRowLoadInfo;   /* Info for deferred row loading */
129854
129855  /* Usually, regResult is the first cell in an array of memory cells
129856  ** containing the current result row. In this case regOrig is set to the
129857  ** same value. However, if the results are being sent to the sorter, the
129858  ** values for any expressions that are also part of the sort-key are omitted
129859  ** from this array. In this case regOrig is set to zero.  */
129860  int regResult;              /* Start of memory holding current results */
129861  int regOrig;                /* Start of memory holding full result (or 0) */
129862
129863  assert( v );
129864  assert( p->pEList!=0 );
129865  hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
129866  if( pSort && pSort->pOrderBy==0 ) pSort = 0;
129867  if( pSort==0 && !hasDistinct ){
129868    assert( iContinue!=0 );
129869    codeOffset(v, p->iOffset, iContinue);
129870  }
129871
129872  /* Pull the requested columns.
129873  */
129874  nResultCol = p->pEList->nExpr;
129875
129876  if( pDest->iSdst==0 ){
129877    if( pSort ){
129878      nPrefixReg = pSort->pOrderBy->nExpr;
129879      if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
129880      pParse->nMem += nPrefixReg;
129881    }
129882    pDest->iSdst = pParse->nMem+1;
129883    pParse->nMem += nResultCol;
129884  }else if( pDest->iSdst+nResultCol > pParse->nMem ){
129885    /* This is an error condition that can result, for example, when a SELECT
129886    ** on the right-hand side of an INSERT contains more result columns than
129887    ** there are columns in the table on the left.  The error will be caught
129888    ** and reported later.  But we need to make sure enough memory is allocated
129889    ** to avoid other spurious errors in the meantime. */
129890    pParse->nMem += nResultCol;
129891  }
129892  pDest->nSdst = nResultCol;
129893  regOrig = regResult = pDest->iSdst;
129894  if( srcTab>=0 ){
129895    for(i=0; i<nResultCol; i++){
129896      sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
129897      VdbeComment((v, "%s", p->pEList->a[i].zEName));
129898    }
129899  }else if( eDest!=SRT_Exists ){
129900#ifdef SQLITE_ENABLE_SORTER_REFERENCES
129901    ExprList *pExtra = 0;
129902#endif
129903    /* If the destination is an EXISTS(...) expression, the actual
129904    ** values returned by the SELECT are not required.
129905    */
129906    u8 ecelFlags;    /* "ecel" is an abbreviation of "ExprCodeExprList" */
129907    ExprList *pEList;
129908    if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
129909      ecelFlags = SQLITE_ECEL_DUP;
129910    }else{
129911      ecelFlags = 0;
129912    }
129913    if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){
129914      /* For each expression in p->pEList that is a copy of an expression in
129915      ** the ORDER BY clause (pSort->pOrderBy), set the associated
129916      ** iOrderByCol value to one more than the index of the ORDER BY
129917      ** expression within the sort-key that pushOntoSorter() will generate.
129918      ** This allows the p->pEList field to be omitted from the sorted record,
129919      ** saving space and CPU cycles.  */
129920      ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
129921
129922      for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
129923        int j;
129924        if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
129925          p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
129926        }
129927      }
129928#ifdef SQLITE_ENABLE_SORTER_REFERENCES
129929      selectExprDefer(pParse, pSort, p->pEList, &pExtra);
129930      if( pExtra && pParse->db->mallocFailed==0 ){
129931        /* If there are any extra PK columns to add to the sorter records,
129932        ** allocate extra memory cells and adjust the OpenEphemeral
129933        ** instruction to account for the larger records. This is only
129934        ** required if there are one or more WITHOUT ROWID tables with
129935        ** composite primary keys in the SortCtx.aDefer[] array.  */
129936        VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
129937        pOp->p2 += (pExtra->nExpr - pSort->nDefer);
129938        pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
129939        pParse->nMem += pExtra->nExpr;
129940      }
129941#endif
129942
129943      /* Adjust nResultCol to account for columns that are omitted
129944      ** from the sorter by the optimizations in this branch */
129945      pEList = p->pEList;
129946      for(i=0; i<pEList->nExpr; i++){
129947        if( pEList->a[i].u.x.iOrderByCol>0
129948#ifdef SQLITE_ENABLE_SORTER_REFERENCES
129949         || pEList->a[i].bSorterRef
129950#endif
129951        ){
129952          nResultCol--;
129953          regOrig = 0;
129954        }
129955      }
129956
129957      testcase( regOrig );
129958      testcase( eDest==SRT_Set );
129959      testcase( eDest==SRT_Mem );
129960      testcase( eDest==SRT_Coroutine );
129961      testcase( eDest==SRT_Output );
129962      assert( eDest==SRT_Set || eDest==SRT_Mem
129963           || eDest==SRT_Coroutine || eDest==SRT_Output );
129964    }
129965    sRowLoadInfo.regResult = regResult;
129966    sRowLoadInfo.ecelFlags = ecelFlags;
129967#ifdef SQLITE_ENABLE_SORTER_REFERENCES
129968    sRowLoadInfo.pExtra = pExtra;
129969    sRowLoadInfo.regExtraResult = regResult + nResultCol;
129970    if( pExtra ) nResultCol += pExtra->nExpr;
129971#endif
129972    if( p->iLimit
129973     && (ecelFlags & SQLITE_ECEL_OMITREF)!=0
129974     && nPrefixReg>0
129975    ){
129976      assert( pSort!=0 );
129977      assert( hasDistinct==0 );
129978      pSort->pDeferredRowLoad = &sRowLoadInfo;
129979      regOrig = 0;
129980    }else{
129981      innerLoopLoadRow(pParse, p, &sRowLoadInfo);
129982    }
129983  }
129984
129985  /* If the DISTINCT keyword was present on the SELECT statement
129986  ** and this row has been seen before, then do not make this row
129987  ** part of the result.
129988  */
129989  if( hasDistinct ){
129990    switch( pDistinct->eTnctType ){
129991      case WHERE_DISTINCT_ORDERED: {
129992        VdbeOp *pOp;            /* No longer required OpenEphemeral instr. */
129993        int iJump;              /* Jump destination */
129994        int regPrev;            /* Previous row content */
129995
129996        /* Allocate space for the previous row */
129997        regPrev = pParse->nMem+1;
129998        pParse->nMem += nResultCol;
129999
130000        /* Change the OP_OpenEphemeral coded earlier to an OP_Null
130001        ** sets the MEM_Cleared bit on the first register of the
130002        ** previous value.  This will cause the OP_Ne below to always
130003        ** fail on the first iteration of the loop even if the first
130004        ** row is all NULLs.
130005        */
130006        sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
130007        pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
130008        pOp->opcode = OP_Null;
130009        pOp->p1 = 1;
130010        pOp->p2 = regPrev;
130011        pOp = 0;  /* Ensure pOp is not used after sqlite3VdbeAddOp() */
130012
130013        iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
130014        for(i=0; i<nResultCol; i++){
130015          CollSeq *pColl = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr);
130016          if( i<nResultCol-1 ){
130017            sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
130018            VdbeCoverage(v);
130019          }else{
130020            sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
130021            VdbeCoverage(v);
130022           }
130023          sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
130024          sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
130025        }
130026        assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
130027        sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
130028        break;
130029      }
130030
130031      case WHERE_DISTINCT_UNIQUE: {
130032        sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
130033        break;
130034      }
130035
130036      default: {
130037        assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
130038        codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
130039                     regResult);
130040        break;
130041      }
130042    }
130043    if( pSort==0 ){
130044      codeOffset(v, p->iOffset, iContinue);
130045    }
130046  }
130047
130048  switch( eDest ){
130049    /* In this mode, write each query result to the key of the temporary
130050    ** table iParm.
130051    */
130052#ifndef SQLITE_OMIT_COMPOUND_SELECT
130053    case SRT_Union: {
130054      int r1;
130055      r1 = sqlite3GetTempReg(pParse);
130056      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
130057      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
130058      sqlite3ReleaseTempReg(pParse, r1);
130059      break;
130060    }
130061
130062    /* Construct a record from the query result, but instead of
130063    ** saving that record, use it as a key to delete elements from
130064    ** the temporary table iParm.
130065    */
130066    case SRT_Except: {
130067      sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol);
130068      break;
130069    }
130070#endif /* SQLITE_OMIT_COMPOUND_SELECT */
130071
130072    /* Store the result as data using a unique key.
130073    */
130074    case SRT_Fifo:
130075    case SRT_DistFifo:
130076    case SRT_Table:
130077    case SRT_EphemTab: {
130078      int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
130079      testcase( eDest==SRT_Table );
130080      testcase( eDest==SRT_EphemTab );
130081      testcase( eDest==SRT_Fifo );
130082      testcase( eDest==SRT_DistFifo );
130083      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
130084#ifndef SQLITE_OMIT_CTE
130085      if( eDest==SRT_DistFifo ){
130086        /* If the destination is DistFifo, then cursor (iParm+1) is open
130087        ** on an ephemeral index. If the current row is already present
130088        ** in the index, do not write it to the output. If not, add the
130089        ** current row to the index and proceed with writing it to the
130090        ** output table as well.  */
130091        int addr = sqlite3VdbeCurrentAddr(v) + 4;
130092        sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
130093        VdbeCoverage(v);
130094        sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol);
130095        assert( pSort==0 );
130096      }
130097#endif
130098      if( pSort ){
130099        assert( regResult==regOrig );
130100        pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg);
130101      }else{
130102        int r2 = sqlite3GetTempReg(pParse);
130103        sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
130104        sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
130105        sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
130106        sqlite3ReleaseTempReg(pParse, r2);
130107      }
130108      sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1);
130109      break;
130110    }
130111
130112#ifndef SQLITE_OMIT_SUBQUERY
130113    /* If we are creating a set for an "expr IN (SELECT ...)" construct,
130114    ** then there should be a single item on the stack.  Write this
130115    ** item into the set table with bogus data.
130116    */
130117    case SRT_Set: {
130118      if( pSort ){
130119        /* At first glance you would think we could optimize out the
130120        ** ORDER BY in this case since the order of entries in the set
130121        ** does not matter.  But there might be a LIMIT clause, in which
130122        ** case the order does matter */
130123        pushOntoSorter(
130124            pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
130125      }else{
130126        int r1 = sqlite3GetTempReg(pParse);
130127        assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
130128        sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, nResultCol,
130129            r1, pDest->zAffSdst, nResultCol);
130130        sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
130131        sqlite3ReleaseTempReg(pParse, r1);
130132      }
130133      break;
130134    }
130135
130136    /* If any row exist in the result set, record that fact and abort.
130137    */
130138    case SRT_Exists: {
130139      sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
130140      /* The LIMIT clause will terminate the loop for us */
130141      break;
130142    }
130143
130144    /* If this is a scalar select that is part of an expression, then
130145    ** store the results in the appropriate memory cell or array of
130146    ** memory cells and break out of the scan loop.
130147    */
130148    case SRT_Mem: {
130149      if( pSort ){
130150        assert( nResultCol<=pDest->nSdst );
130151        pushOntoSorter(
130152            pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
130153      }else{
130154        assert( nResultCol==pDest->nSdst );
130155        assert( regResult==iParm );
130156        /* The LIMIT clause will jump out of the loop for us */
130157      }
130158      break;
130159    }
130160#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
130161
130162    case SRT_Coroutine:       /* Send data to a co-routine */
130163    case SRT_Output: {        /* Return the results */
130164      testcase( eDest==SRT_Coroutine );
130165      testcase( eDest==SRT_Output );
130166      if( pSort ){
130167        pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol,
130168                       nPrefixReg);
130169      }else if( eDest==SRT_Coroutine ){
130170        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
130171      }else{
130172        sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol);
130173      }
130174      break;
130175    }
130176
130177#ifndef SQLITE_OMIT_CTE
130178    /* Write the results into a priority queue that is order according to
130179    ** pDest->pOrderBy (in pSO).  pDest->iSDParm (in iParm) is the cursor for an
130180    ** index with pSO->nExpr+2 columns.  Build a key using pSO for the first
130181    ** pSO->nExpr columns, then make sure all keys are unique by adding a
130182    ** final OP_Sequence column.  The last column is the record as a blob.
130183    */
130184    case SRT_DistQueue:
130185    case SRT_Queue: {
130186      int nKey;
130187      int r1, r2, r3;
130188      int addrTest = 0;
130189      ExprList *pSO;
130190      pSO = pDest->pOrderBy;
130191      assert( pSO );
130192      nKey = pSO->nExpr;
130193      r1 = sqlite3GetTempReg(pParse);
130194      r2 = sqlite3GetTempRange(pParse, nKey+2);
130195      r3 = r2+nKey+1;
130196      if( eDest==SRT_DistQueue ){
130197        /* If the destination is DistQueue, then cursor (iParm+1) is open
130198        ** on a second ephemeral index that holds all values every previously
130199        ** added to the queue. */
130200        addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0,
130201                                        regResult, nResultCol);
130202        VdbeCoverage(v);
130203      }
130204      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3);
130205      if( eDest==SRT_DistQueue ){
130206        sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
130207        sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
130208      }
130209      for(i=0; i<nKey; i++){
130210        sqlite3VdbeAddOp2(v, OP_SCopy,
130211                          regResult + pSO->a[i].u.x.iOrderByCol - 1,
130212                          r2+i);
130213      }
130214      sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey);
130215      sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1);
130216      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, r2, nKey+2);
130217      if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
130218      sqlite3ReleaseTempReg(pParse, r1);
130219      sqlite3ReleaseTempRange(pParse, r2, nKey+2);
130220      break;
130221    }
130222#endif /* SQLITE_OMIT_CTE */
130223
130224
130225
130226#if !defined(SQLITE_OMIT_TRIGGER)
130227    /* Discard the results.  This is used for SELECT statements inside
130228    ** the body of a TRIGGER.  The purpose of such selects is to call
130229    ** user-defined functions that have side effects.  We do not care
130230    ** about the actual results of the select.
130231    */
130232    default: {
130233      assert( eDest==SRT_Discard );
130234      break;
130235    }
130236#endif
130237  }
130238
130239  /* Jump to the end of the loop if the LIMIT is reached.  Except, if
130240  ** there is a sorter, in which case the sorter has already limited
130241  ** the output for us.
130242  */
130243  if( pSort==0 && p->iLimit ){
130244    sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
130245  }
130246}
130247
130248/*
130249** Allocate a KeyInfo object sufficient for an index of N key columns and
130250** X extra columns.
130251*/
130252SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
130253  int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
130254  KeyInfo *p = sqlite3DbMallocRawNN(db, sizeof(KeyInfo) + nExtra);
130255  if( p ){
130256    p->aSortFlags = (u8*)&p->aColl[N+X];
130257    p->nKeyField = (u16)N;
130258    p->nAllField = (u16)(N+X);
130259    p->enc = ENC(db);
130260    p->db = db;
130261    p->nRef = 1;
130262    memset(&p[1], 0, nExtra);
130263  }else{
130264    sqlite3OomFault(db);
130265  }
130266  return p;
130267}
130268
130269/*
130270** Deallocate a KeyInfo object
130271*/
130272SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){
130273  if( p ){
130274    assert( p->nRef>0 );
130275    p->nRef--;
130276    if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
130277  }
130278}
130279
130280/*
130281** Make a new pointer to a KeyInfo object
130282*/
130283SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
130284  if( p ){
130285    assert( p->nRef>0 );
130286    p->nRef++;
130287  }
130288  return p;
130289}
130290
130291#ifdef SQLITE_DEBUG
130292/*
130293** Return TRUE if a KeyInfo object can be change.  The KeyInfo object
130294** can only be changed if this is just a single reference to the object.
130295**
130296** This routine is used only inside of assert() statements.
130297*/
130298SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
130299#endif /* SQLITE_DEBUG */
130300
130301/*
130302** Given an expression list, generate a KeyInfo structure that records
130303** the collating sequence for each expression in that expression list.
130304**
130305** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
130306** KeyInfo structure is appropriate for initializing a virtual index to
130307** implement that clause.  If the ExprList is the result set of a SELECT
130308** then the KeyInfo structure is appropriate for initializing a virtual
130309** index to implement a DISTINCT test.
130310**
130311** Space to hold the KeyInfo structure is obtained from malloc.  The calling
130312** function is responsible for seeing that this structure is eventually
130313** freed.
130314*/
130315SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoFromExprList(
130316  Parse *pParse,       /* Parsing context */
130317  ExprList *pList,     /* Form the KeyInfo object from this ExprList */
130318  int iStart,          /* Begin with this column of pList */
130319  int nExtra           /* Add this many extra columns to the end */
130320){
130321  int nExpr;
130322  KeyInfo *pInfo;
130323  struct ExprList_item *pItem;
130324  sqlite3 *db = pParse->db;
130325  int i;
130326
130327  nExpr = pList->nExpr;
130328  pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
130329  if( pInfo ){
130330    assert( sqlite3KeyInfoIsWriteable(pInfo) );
130331    for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
130332      pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
130333      pInfo->aSortFlags[i-iStart] = pItem->sortFlags;
130334    }
130335  }
130336  return pInfo;
130337}
130338
130339/*
130340** Name of the connection operator, used for error messages.
130341*/
130342static const char *selectOpName(int id){
130343  char *z;
130344  switch( id ){
130345    case TK_ALL:       z = "UNION ALL";   break;
130346    case TK_INTERSECT: z = "INTERSECT";   break;
130347    case TK_EXCEPT:    z = "EXCEPT";      break;
130348    default:           z = "UNION";       break;
130349  }
130350  return z;
130351}
130352
130353#ifndef SQLITE_OMIT_EXPLAIN
130354/*
130355** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
130356** is a no-op. Otherwise, it adds a single row of output to the EQP result,
130357** where the caption is of the form:
130358**
130359**   "USE TEMP B-TREE FOR xxx"
130360**
130361** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
130362** is determined by the zUsage argument.
130363*/
130364static void explainTempTable(Parse *pParse, const char *zUsage){
130365  ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
130366}
130367
130368/*
130369** Assign expression b to lvalue a. A second, no-op, version of this macro
130370** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
130371** in sqlite3Select() to assign values to structure member variables that
130372** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
130373** code with #ifndef directives.
130374*/
130375# define explainSetInteger(a, b) a = b
130376
130377#else
130378/* No-op versions of the explainXXX() functions and macros. */
130379# define explainTempTable(y,z)
130380# define explainSetInteger(y,z)
130381#endif
130382
130383
130384/*
130385** If the inner loop was generated using a non-null pOrderBy argument,
130386** then the results were placed in a sorter.  After the loop is terminated
130387** we need to run the sorter and output the results.  The following
130388** routine generates the code needed to do that.
130389*/
130390static void generateSortTail(
130391  Parse *pParse,    /* Parsing context */
130392  Select *p,        /* The SELECT statement */
130393  SortCtx *pSort,   /* Information on the ORDER BY clause */
130394  int nColumn,      /* Number of columns of data */
130395  SelectDest *pDest /* Write the sorted results here */
130396){
130397  Vdbe *v = pParse->pVdbe;                     /* The prepared statement */
130398  int addrBreak = pSort->labelDone;            /* Jump here to exit loop */
130399  int addrContinue = sqlite3VdbeMakeLabel(pParse);/* Jump here for next cycle */
130400  int addr;                       /* Top of output loop. Jump for Next. */
130401  int addrOnce = 0;
130402  int iTab;
130403  ExprList *pOrderBy = pSort->pOrderBy;
130404  int eDest = pDest->eDest;
130405  int iParm = pDest->iSDParm;
130406  int regRow;
130407  int regRowid;
130408  int iCol;
130409  int nKey;                       /* Number of key columns in sorter record */
130410  int iSortTab;                   /* Sorter cursor to read from */
130411  int i;
130412  int bSeq;                       /* True if sorter record includes seq. no. */
130413  int nRefKey = 0;
130414  struct ExprList_item *aOutEx = p->pEList->a;
130415
130416  assert( addrBreak<0 );
130417  if( pSort->labelBkOut ){
130418    sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
130419    sqlite3VdbeGoto(v, addrBreak);
130420    sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
130421  }
130422
130423#ifdef SQLITE_ENABLE_SORTER_REFERENCES
130424  /* Open any cursors needed for sorter-reference expressions */
130425  for(i=0; i<pSort->nDefer; i++){
130426    Table *pTab = pSort->aDefer[i].pTab;
130427    int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
130428    sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
130429    nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
130430  }
130431#endif
130432
130433  iTab = pSort->iECursor;
130434  if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){
130435    regRowid = 0;
130436    regRow = pDest->iSdst;
130437  }else{
130438    regRowid = sqlite3GetTempReg(pParse);
130439    if( eDest==SRT_EphemTab || eDest==SRT_Table ){
130440      regRow = sqlite3GetTempReg(pParse);
130441      nColumn = 0;
130442    }else{
130443      regRow = sqlite3GetTempRange(pParse, nColumn);
130444    }
130445  }
130446  nKey = pOrderBy->nExpr - pSort->nOBSat;
130447  if( pSort->sortFlags & SORTFLAG_UseSorter ){
130448    int regSortOut = ++pParse->nMem;
130449    iSortTab = pParse->nTab++;
130450    if( pSort->labelBkOut ){
130451      addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
130452    }
130453    sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut,
130454        nKey+1+nColumn+nRefKey);
130455    if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
130456    addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
130457    VdbeCoverage(v);
130458    codeOffset(v, p->iOffset, addrContinue);
130459    sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
130460    bSeq = 0;
130461  }else{
130462    addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
130463    codeOffset(v, p->iOffset, addrContinue);
130464    iSortTab = iTab;
130465    bSeq = 1;
130466  }
130467  for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
130468#ifdef SQLITE_ENABLE_SORTER_REFERENCES
130469    if( aOutEx[i].bSorterRef ) continue;
130470#endif
130471    if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
130472  }
130473#ifdef SQLITE_ENABLE_SORTER_REFERENCES
130474  if( pSort->nDefer ){
130475    int iKey = iCol+1;
130476    int regKey = sqlite3GetTempRange(pParse, nRefKey);
130477
130478    for(i=0; i<pSort->nDefer; i++){
130479      int iCsr = pSort->aDefer[i].iCsr;
130480      Table *pTab = pSort->aDefer[i].pTab;
130481      int nKey = pSort->aDefer[i].nKey;
130482
130483      sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
130484      if( HasRowid(pTab) ){
130485        sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey);
130486        sqlite3VdbeAddOp3(v, OP_SeekRowid, iCsr,
130487            sqlite3VdbeCurrentAddr(v)+1, regKey);
130488      }else{
130489        int k;
130490        int iJmp;
130491        assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
130492        for(k=0; k<nKey; k++){
130493          sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey+k);
130494        }
130495        iJmp = sqlite3VdbeCurrentAddr(v);
130496        sqlite3VdbeAddOp4Int(v, OP_SeekGE, iCsr, iJmp+2, regKey, nKey);
130497        sqlite3VdbeAddOp4Int(v, OP_IdxLE, iCsr, iJmp+3, regKey, nKey);
130498        sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
130499      }
130500    }
130501    sqlite3ReleaseTempRange(pParse, regKey, nRefKey);
130502  }
130503#endif
130504  for(i=nColumn-1; i>=0; i--){
130505#ifdef SQLITE_ENABLE_SORTER_REFERENCES
130506    if( aOutEx[i].bSorterRef ){
130507      sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i);
130508    }else
130509#endif
130510    {
130511      int iRead;
130512      if( aOutEx[i].u.x.iOrderByCol ){
130513        iRead = aOutEx[i].u.x.iOrderByCol-1;
130514      }else{
130515        iRead = iCol--;
130516      }
130517      sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
130518      VdbeComment((v, "%s", aOutEx[i].zEName));
130519    }
130520  }
130521  switch( eDest ){
130522    case SRT_Table:
130523    case SRT_EphemTab: {
130524      sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq, regRow);
130525      sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
130526      sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
130527      sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
130528      break;
130529    }
130530#ifndef SQLITE_OMIT_SUBQUERY
130531    case SRT_Set: {
130532      assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
130533      sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, nColumn, regRowid,
130534                        pDest->zAffSdst, nColumn);
130535      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, regRowid, regRow, nColumn);
130536      break;
130537    }
130538    case SRT_Mem: {
130539      /* The LIMIT clause will terminate the loop for us */
130540      break;
130541    }
130542#endif
130543    default: {
130544      assert( eDest==SRT_Output || eDest==SRT_Coroutine );
130545      testcase( eDest==SRT_Output );
130546      testcase( eDest==SRT_Coroutine );
130547      if( eDest==SRT_Output ){
130548        sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
130549      }else{
130550        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
130551      }
130552      break;
130553    }
130554  }
130555  if( regRowid ){
130556    if( eDest==SRT_Set ){
130557      sqlite3ReleaseTempRange(pParse, regRow, nColumn);
130558    }else{
130559      sqlite3ReleaseTempReg(pParse, regRow);
130560    }
130561    sqlite3ReleaseTempReg(pParse, regRowid);
130562  }
130563  /* The bottom of the loop
130564  */
130565  sqlite3VdbeResolveLabel(v, addrContinue);
130566  if( pSort->sortFlags & SORTFLAG_UseSorter ){
130567    sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v);
130568  }else{
130569    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v);
130570  }
130571  if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
130572  sqlite3VdbeResolveLabel(v, addrBreak);
130573}
130574
130575/*
130576** Return a pointer to a string containing the 'declaration type' of the
130577** expression pExpr. The string may be treated as static by the caller.
130578**
130579** Also try to estimate the size of the returned value and return that
130580** result in *pEstWidth.
130581**
130582** The declaration type is the exact datatype definition extracted from the
130583** original CREATE TABLE statement if the expression is a column. The
130584** declaration type for a ROWID field is INTEGER. Exactly when an expression
130585** is considered a column can be complex in the presence of subqueries. The
130586** result-set expression in all of the following SELECT statements is
130587** considered a column by this function.
130588**
130589**   SELECT col FROM tbl;
130590**   SELECT (SELECT col FROM tbl;
130591**   SELECT (SELECT col FROM tbl);
130592**   SELECT abc FROM (SELECT col AS abc FROM tbl);
130593**
130594** The declaration type for any expression other than a column is NULL.
130595**
130596** This routine has either 3 or 6 parameters depending on whether or not
130597** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
130598*/
130599#ifdef SQLITE_ENABLE_COLUMN_METADATA
130600# define columnType(A,B,C,D,E) columnTypeImpl(A,B,C,D,E)
130601#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
130602# define columnType(A,B,C,D,E) columnTypeImpl(A,B)
130603#endif
130604static const char *columnTypeImpl(
130605  NameContext *pNC,
130606#ifndef SQLITE_ENABLE_COLUMN_METADATA
130607  Expr *pExpr
130608#else
130609  Expr *pExpr,
130610  const char **pzOrigDb,
130611  const char **pzOrigTab,
130612  const char **pzOrigCol
130613#endif
130614){
130615  char const *zType = 0;
130616  int j;
130617#ifdef SQLITE_ENABLE_COLUMN_METADATA
130618  char const *zOrigDb = 0;
130619  char const *zOrigTab = 0;
130620  char const *zOrigCol = 0;
130621#endif
130622
130623  assert( pExpr!=0 );
130624  assert( pNC->pSrcList!=0 );
130625  switch( pExpr->op ){
130626    case TK_COLUMN: {
130627      /* The expression is a column. Locate the table the column is being
130628      ** extracted from in NameContext.pSrcList. This table may be real
130629      ** database table or a subquery.
130630      */
130631      Table *pTab = 0;            /* Table structure column is extracted from */
130632      Select *pS = 0;             /* Select the column is extracted from */
130633      int iCol = pExpr->iColumn;  /* Index of column in pTab */
130634      while( pNC && !pTab ){
130635        SrcList *pTabList = pNC->pSrcList;
130636        for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
130637        if( j<pTabList->nSrc ){
130638          pTab = pTabList->a[j].pTab;
130639          pS = pTabList->a[j].pSelect;
130640        }else{
130641          pNC = pNC->pNext;
130642        }
130643      }
130644
130645      if( pTab==0 ){
130646        /* At one time, code such as "SELECT new.x" within a trigger would
130647        ** cause this condition to run.  Since then, we have restructured how
130648        ** trigger code is generated and so this condition is no longer
130649        ** possible. However, it can still be true for statements like
130650        ** the following:
130651        **
130652        **   CREATE TABLE t1(col INTEGER);
130653        **   SELECT (SELECT t1.col) FROM FROM t1;
130654        **
130655        ** when columnType() is called on the expression "t1.col" in the
130656        ** sub-select. In this case, set the column type to NULL, even
130657        ** though it should really be "INTEGER".
130658        **
130659        ** This is not a problem, as the column type of "t1.col" is never
130660        ** used. When columnType() is called on the expression
130661        ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
130662        ** branch below.  */
130663        break;
130664      }
130665
130666      assert( pTab && pExpr->y.pTab==pTab );
130667      if( pS ){
130668        /* The "table" is actually a sub-select or a view in the FROM clause
130669        ** of the SELECT statement. Return the declaration type and origin
130670        ** data for the result-set column of the sub-select.
130671        */
130672        if( iCol>=0 && iCol<pS->pEList->nExpr ){
130673          /* If iCol is less than zero, then the expression requests the
130674          ** rowid of the sub-select or view. This expression is legal (see
130675          ** test case misc2.2.2) - it always evaluates to NULL.
130676          */
130677          NameContext sNC;
130678          Expr *p = pS->pEList->a[iCol].pExpr;
130679          sNC.pSrcList = pS->pSrc;
130680          sNC.pNext = pNC;
130681          sNC.pParse = pNC->pParse;
130682          zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol);
130683        }
130684      }else{
130685        /* A real table or a CTE table */
130686        assert( !pS );
130687#ifdef SQLITE_ENABLE_COLUMN_METADATA
130688        if( iCol<0 ) iCol = pTab->iPKey;
130689        assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
130690        if( iCol<0 ){
130691          zType = "INTEGER";
130692          zOrigCol = "rowid";
130693        }else{
130694          zOrigCol = pTab->aCol[iCol].zName;
130695          zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
130696        }
130697        zOrigTab = pTab->zName;
130698        if( pNC->pParse && pTab->pSchema ){
130699          int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
130700          zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
130701        }
130702#else
130703        assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
130704        if( iCol<0 ){
130705          zType = "INTEGER";
130706        }else{
130707          zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
130708        }
130709#endif
130710      }
130711      break;
130712    }
130713#ifndef SQLITE_OMIT_SUBQUERY
130714    case TK_SELECT: {
130715      /* The expression is a sub-select. Return the declaration type and
130716      ** origin info for the single column in the result set of the SELECT
130717      ** statement.
130718      */
130719      NameContext sNC;
130720      Select *pS = pExpr->x.pSelect;
130721      Expr *p = pS->pEList->a[0].pExpr;
130722      assert( ExprHasProperty(pExpr, EP_xIsSelect) );
130723      sNC.pSrcList = pS->pSrc;
130724      sNC.pNext = pNC;
130725      sNC.pParse = pNC->pParse;
130726      zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
130727      break;
130728    }
130729#endif
130730  }
130731
130732#ifdef SQLITE_ENABLE_COLUMN_METADATA
130733  if( pzOrigDb ){
130734    assert( pzOrigTab && pzOrigCol );
130735    *pzOrigDb = zOrigDb;
130736    *pzOrigTab = zOrigTab;
130737    *pzOrigCol = zOrigCol;
130738  }
130739#endif
130740  return zType;
130741}
130742
130743/*
130744** Generate code that will tell the VDBE the declaration types of columns
130745** in the result set.
130746*/
130747static void generateColumnTypes(
130748  Parse *pParse,      /* Parser context */
130749  SrcList *pTabList,  /* List of tables */
130750  ExprList *pEList    /* Expressions defining the result set */
130751){
130752#ifndef SQLITE_OMIT_DECLTYPE
130753  Vdbe *v = pParse->pVdbe;
130754  int i;
130755  NameContext sNC;
130756  sNC.pSrcList = pTabList;
130757  sNC.pParse = pParse;
130758  sNC.pNext = 0;
130759  for(i=0; i<pEList->nExpr; i++){
130760    Expr *p = pEList->a[i].pExpr;
130761    const char *zType;
130762#ifdef SQLITE_ENABLE_COLUMN_METADATA
130763    const char *zOrigDb = 0;
130764    const char *zOrigTab = 0;
130765    const char *zOrigCol = 0;
130766    zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
130767
130768    /* The vdbe must make its own copy of the column-type and other
130769    ** column specific strings, in case the schema is reset before this
130770    ** virtual machine is deleted.
130771    */
130772    sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
130773    sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
130774    sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
130775#else
130776    zType = columnType(&sNC, p, 0, 0, 0);
130777#endif
130778    sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
130779  }
130780#endif /* !defined(SQLITE_OMIT_DECLTYPE) */
130781}
130782
130783
130784/*
130785** Compute the column names for a SELECT statement.
130786**
130787** The only guarantee that SQLite makes about column names is that if the
130788** column has an AS clause assigning it a name, that will be the name used.
130789** That is the only documented guarantee.  However, countless applications
130790** developed over the years have made baseless assumptions about column names
130791** and will break if those assumptions changes.  Hence, use extreme caution
130792** when modifying this routine to avoid breaking legacy.
130793**
130794** See Also: sqlite3ColumnsFromExprList()
130795**
130796** The PRAGMA short_column_names and PRAGMA full_column_names settings are
130797** deprecated.  The default setting is short=ON, full=OFF.  99.9% of all
130798** applications should operate this way.  Nevertheless, we need to support the
130799** other modes for legacy:
130800**
130801**    short=OFF, full=OFF:      Column name is the text of the expression has it
130802**                              originally appears in the SELECT statement.  In
130803**                              other words, the zSpan of the result expression.
130804**
130805**    short=ON, full=OFF:       (This is the default setting).  If the result
130806**                              refers directly to a table column, then the
130807**                              result column name is just the table column
130808**                              name: COLUMN.  Otherwise use zSpan.
130809**
130810**    full=ON, short=ANY:       If the result refers directly to a table column,
130811**                              then the result column name with the table name
130812**                              prefix, ex: TABLE.COLUMN.  Otherwise use zSpan.
130813*/
130814static void generateColumnNames(
130815  Parse *pParse,      /* Parser context */
130816  Select *pSelect     /* Generate column names for this SELECT statement */
130817){
130818  Vdbe *v = pParse->pVdbe;
130819  int i;
130820  Table *pTab;
130821  SrcList *pTabList;
130822  ExprList *pEList;
130823  sqlite3 *db = pParse->db;
130824  int fullName;    /* TABLE.COLUMN if no AS clause and is a direct table ref */
130825  int srcName;     /* COLUMN or TABLE.COLUMN if no AS clause and is direct */
130826
130827#ifndef SQLITE_OMIT_EXPLAIN
130828  /* If this is an EXPLAIN, skip this step */
130829  if( pParse->explain ){
130830    return;
130831  }
130832#endif
130833
130834  if( pParse->colNamesSet ) return;
130835  /* Column names are determined by the left-most term of a compound select */
130836  while( pSelect->pPrior ) pSelect = pSelect->pPrior;
130837  SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
130838  pTabList = pSelect->pSrc;
130839  pEList = pSelect->pEList;
130840  assert( v!=0 );
130841  assert( pTabList!=0 );
130842  pParse->colNamesSet = 1;
130843  fullName = (db->flags & SQLITE_FullColNames)!=0;
130844  srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
130845  sqlite3VdbeSetNumCols(v, pEList->nExpr);
130846  for(i=0; i<pEList->nExpr; i++){
130847    Expr *p = pEList->a[i].pExpr;
130848
130849    assert( p!=0 );
130850    assert( p->op!=TK_AGG_COLUMN );  /* Agg processing has not run yet */
130851    assert( p->op!=TK_COLUMN || p->y.pTab!=0 ); /* Covering idx not yet coded */
130852    if( pEList->a[i].zEName && pEList->a[i].eEName==ENAME_NAME ){
130853      /* An AS clause always takes first priority */
130854      char *zName = pEList->a[i].zEName;
130855      sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
130856    }else if( srcName && p->op==TK_COLUMN ){
130857      char *zCol;
130858      int iCol = p->iColumn;
130859      pTab = p->y.pTab;
130860      assert( pTab!=0 );
130861      if( iCol<0 ) iCol = pTab->iPKey;
130862      assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
130863      if( iCol<0 ){
130864        zCol = "rowid";
130865      }else{
130866        zCol = pTab->aCol[iCol].zName;
130867      }
130868      if( fullName ){
130869        char *zName = 0;
130870        zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
130871        sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
130872      }else{
130873        sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
130874      }
130875    }else{
130876      const char *z = pEList->a[i].zEName;
130877      z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
130878      sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC);
130879    }
130880  }
130881  generateColumnTypes(pParse, pTabList, pEList);
130882}
130883
130884/*
130885** Given an expression list (which is really the list of expressions
130886** that form the result set of a SELECT statement) compute appropriate
130887** column names for a table that would hold the expression list.
130888**
130889** All column names will be unique.
130890**
130891** Only the column names are computed.  Column.zType, Column.zColl,
130892** and other fields of Column are zeroed.
130893**
130894** Return SQLITE_OK on success.  If a memory allocation error occurs,
130895** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
130896**
130897** The only guarantee that SQLite makes about column names is that if the
130898** column has an AS clause assigning it a name, that will be the name used.
130899** That is the only documented guarantee.  However, countless applications
130900** developed over the years have made baseless assumptions about column names
130901** and will break if those assumptions changes.  Hence, use extreme caution
130902** when modifying this routine to avoid breaking legacy.
130903**
130904** See Also: generateColumnNames()
130905*/
130906SQLITE_PRIVATE int sqlite3ColumnsFromExprList(
130907  Parse *pParse,          /* Parsing context */
130908  ExprList *pEList,       /* Expr list from which to derive column names */
130909  i16 *pnCol,             /* Write the number of columns here */
130910  Column **paCol          /* Write the new column list here */
130911){
130912  sqlite3 *db = pParse->db;   /* Database connection */
130913  int i, j;                   /* Loop counters */
130914  u32 cnt;                    /* Index added to make the name unique */
130915  Column *aCol, *pCol;        /* For looping over result columns */
130916  int nCol;                   /* Number of columns in the result set */
130917  char *zName;                /* Column name */
130918  int nName;                  /* Size of name in zName[] */
130919  Hash ht;                    /* Hash table of column names */
130920
130921  sqlite3HashInit(&ht);
130922  if( pEList ){
130923    nCol = pEList->nExpr;
130924    aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
130925    testcase( aCol==0 );
130926    if( nCol>32767 ) nCol = 32767;
130927  }else{
130928    nCol = 0;
130929    aCol = 0;
130930  }
130931  assert( nCol==(i16)nCol );
130932  *pnCol = nCol;
130933  *paCol = aCol;
130934
130935  for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
130936    /* Get an appropriate name for the column
130937    */
130938    if( (zName = pEList->a[i].zEName)!=0 && pEList->a[i].eEName==ENAME_NAME ){
130939      /* If the column contains an "AS <name>" phrase, use <name> as the name */
130940    }else{
130941      Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pEList->a[i].pExpr);
130942      while( pColExpr->op==TK_DOT ){
130943        pColExpr = pColExpr->pRight;
130944        assert( pColExpr!=0 );
130945      }
130946      if( pColExpr->op==TK_COLUMN ){
130947        /* For columns use the column name name */
130948        int iCol = pColExpr->iColumn;
130949        Table *pTab = pColExpr->y.pTab;
130950        assert( pTab!=0 );
130951        if( iCol<0 ) iCol = pTab->iPKey;
130952        zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
130953      }else if( pColExpr->op==TK_ID ){
130954        assert( !ExprHasProperty(pColExpr, EP_IntValue) );
130955        zName = pColExpr->u.zToken;
130956      }else{
130957        /* Use the original text of the column expression as its name */
130958        zName = pEList->a[i].zEName;
130959      }
130960    }
130961    if( zName && !sqlite3IsTrueOrFalse(zName) ){
130962      zName = sqlite3DbStrDup(db, zName);
130963    }else{
130964      zName = sqlite3MPrintf(db,"column%d",i+1);
130965    }
130966
130967    /* Make sure the column name is unique.  If the name is not unique,
130968    ** append an integer to the name so that it becomes unique.
130969    */
130970    cnt = 0;
130971    while( zName && sqlite3HashFind(&ht, zName)!=0 ){
130972      nName = sqlite3Strlen30(zName);
130973      if( nName>0 ){
130974        for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
130975        if( zName[j]==':' ) nName = j;
130976      }
130977      zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
130978      if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
130979    }
130980    pCol->zName = zName;
130981    pCol->hName = sqlite3StrIHash(zName);
130982    sqlite3ColumnPropertiesFromName(0, pCol);
130983    if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){
130984      sqlite3OomFault(db);
130985    }
130986  }
130987  sqlite3HashClear(&ht);
130988  if( db->mallocFailed ){
130989    for(j=0; j<i; j++){
130990      sqlite3DbFree(db, aCol[j].zName);
130991    }
130992    sqlite3DbFree(db, aCol);
130993    *paCol = 0;
130994    *pnCol = 0;
130995    return SQLITE_NOMEM_BKPT;
130996  }
130997  return SQLITE_OK;
130998}
130999
131000/*
131001** Add type and collation information to a column list based on
131002** a SELECT statement.
131003**
131004** The column list presumably came from selectColumnNamesFromExprList().
131005** The column list has only names, not types or collations.  This
131006** routine goes through and adds the types and collations.
131007**
131008** This routine requires that all identifiers in the SELECT
131009** statement be resolved.
131010*/
131011SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(
131012  Parse *pParse,        /* Parsing contexts */
131013  Table *pTab,          /* Add column type information to this table */
131014  Select *pSelect,      /* SELECT used to determine types and collations */
131015  char aff              /* Default affinity for columns */
131016){
131017  sqlite3 *db = pParse->db;
131018  NameContext sNC;
131019  Column *pCol;
131020  CollSeq *pColl;
131021  int i;
131022  Expr *p;
131023  struct ExprList_item *a;
131024
131025  assert( pSelect!=0 );
131026  assert( (pSelect->selFlags & SF_Resolved)!=0 );
131027  assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
131028  if( db->mallocFailed ) return;
131029  memset(&sNC, 0, sizeof(sNC));
131030  sNC.pSrcList = pSelect->pSrc;
131031  a = pSelect->pEList->a;
131032  for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
131033    const char *zType;
131034    int n, m;
131035    p = a[i].pExpr;
131036    zType = columnType(&sNC, p, 0, 0, 0);
131037    /* pCol->szEst = ... // Column size est for SELECT tables never used */
131038    pCol->affinity = sqlite3ExprAffinity(p);
131039    if( zType ){
131040      m = sqlite3Strlen30(zType);
131041      n = sqlite3Strlen30(pCol->zName);
131042      pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2);
131043      if( pCol->zName ){
131044        memcpy(&pCol->zName[n+1], zType, m+1);
131045        pCol->colFlags |= COLFLAG_HASTYPE;
131046      }
131047    }
131048    if( pCol->affinity<=SQLITE_AFF_NONE ) pCol->affinity = aff;
131049    pColl = sqlite3ExprCollSeq(pParse, p);
131050    if( pColl && pCol->zColl==0 ){
131051      pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
131052    }
131053  }
131054  pTab->szTabRow = 1; /* Any non-zero value works */
131055}
131056
131057/*
131058** Given a SELECT statement, generate a Table structure that describes
131059** the result set of that SELECT.
131060*/
131061SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect, char aff){
131062  Table *pTab;
131063  sqlite3 *db = pParse->db;
131064  u64 savedFlags;
131065
131066  savedFlags = db->flags;
131067  db->flags &= ~(u64)SQLITE_FullColNames;
131068  db->flags |= SQLITE_ShortColNames;
131069  sqlite3SelectPrep(pParse, pSelect, 0);
131070  db->flags = savedFlags;
131071  if( pParse->nErr ) return 0;
131072  while( pSelect->pPrior ) pSelect = pSelect->pPrior;
131073  pTab = sqlite3DbMallocZero(db, sizeof(Table) );
131074  if( pTab==0 ){
131075    return 0;
131076  }
131077  pTab->nTabRef = 1;
131078  pTab->zName = 0;
131079  pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
131080  sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
131081  sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect, aff);
131082  pTab->iPKey = -1;
131083  if( db->mallocFailed ){
131084    sqlite3DeleteTable(db, pTab);
131085    return 0;
131086  }
131087  return pTab;
131088}
131089
131090/*
131091** Get a VDBE for the given parser context.  Create a new one if necessary.
131092** If an error occurs, return NULL and leave a message in pParse.
131093*/
131094SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
131095  if( pParse->pVdbe ){
131096    return pParse->pVdbe;
131097  }
131098  if( pParse->pToplevel==0
131099   && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
131100  ){
131101    pParse->okConstFactor = 1;
131102  }
131103  return sqlite3VdbeCreate(pParse);
131104}
131105
131106
131107/*
131108** Compute the iLimit and iOffset fields of the SELECT based on the
131109** pLimit expressions.  pLimit->pLeft and pLimit->pRight hold the expressions
131110** that appear in the original SQL statement after the LIMIT and OFFSET
131111** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset
131112** are the integer memory register numbers for counters used to compute
131113** the limit and offset.  If there is no limit and/or offset, then
131114** iLimit and iOffset are negative.
131115**
131116** This routine changes the values of iLimit and iOffset only if
131117** a limit or offset is defined by pLimit->pLeft and pLimit->pRight.  iLimit
131118** and iOffset should have been preset to appropriate default values (zero)
131119** prior to calling this routine.
131120**
131121** The iOffset register (if it exists) is initialized to the value
131122** of the OFFSET.  The iLimit register is initialized to LIMIT.  Register
131123** iOffset+1 is initialized to LIMIT+OFFSET.
131124**
131125** Only if pLimit->pLeft!=0 do the limit registers get
131126** redefined.  The UNION ALL operator uses this property to force
131127** the reuse of the same limit and offset registers across multiple
131128** SELECT statements.
131129*/
131130static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
131131  Vdbe *v = 0;
131132  int iLimit = 0;
131133  int iOffset;
131134  int n;
131135  Expr *pLimit = p->pLimit;
131136
131137  if( p->iLimit ) return;
131138
131139  /*
131140  ** "LIMIT -1" always shows all rows.  There is some
131141  ** controversy about what the correct behavior should be.
131142  ** The current implementation interprets "LIMIT 0" to mean
131143  ** no rows.
131144  */
131145  if( pLimit ){
131146    assert( pLimit->op==TK_LIMIT );
131147    assert( pLimit->pLeft!=0 );
131148    p->iLimit = iLimit = ++pParse->nMem;
131149    v = sqlite3GetVdbe(pParse);
131150    assert( v!=0 );
131151    if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
131152      sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
131153      VdbeComment((v, "LIMIT counter"));
131154      if( n==0 ){
131155        sqlite3VdbeGoto(v, iBreak);
131156      }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
131157        p->nSelectRow = sqlite3LogEst((u64)n);
131158        p->selFlags |= SF_FixedLimit;
131159      }
131160    }else{
131161      sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
131162      sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
131163      VdbeComment((v, "LIMIT counter"));
131164      sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v);
131165    }
131166    if( pLimit->pRight ){
131167      p->iOffset = iOffset = ++pParse->nMem;
131168      pParse->nMem++;   /* Allocate an extra register for limit+offset */
131169      sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
131170      sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
131171      VdbeComment((v, "OFFSET counter"));
131172      sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset);
131173      VdbeComment((v, "LIMIT+OFFSET"));
131174    }
131175  }
131176}
131177
131178#ifndef SQLITE_OMIT_COMPOUND_SELECT
131179/*
131180** Return the appropriate collating sequence for the iCol-th column of
131181** the result set for the compound-select statement "p".  Return NULL if
131182** the column has no default collating sequence.
131183**
131184** The collating sequence for the compound select is taken from the
131185** left-most term of the select that has a collating sequence.
131186*/
131187static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
131188  CollSeq *pRet;
131189  if( p->pPrior ){
131190    pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
131191  }else{
131192    pRet = 0;
131193  }
131194  assert( iCol>=0 );
131195  /* iCol must be less than p->pEList->nExpr.  Otherwise an error would
131196  ** have been thrown during name resolution and we would not have gotten
131197  ** this far */
131198  if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
131199    pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
131200  }
131201  return pRet;
131202}
131203
131204/*
131205** The select statement passed as the second parameter is a compound SELECT
131206** with an ORDER BY clause. This function allocates and returns a KeyInfo
131207** structure suitable for implementing the ORDER BY.
131208**
131209** Space to hold the KeyInfo structure is obtained from malloc. The calling
131210** function is responsible for ensuring that this structure is eventually
131211** freed.
131212*/
131213static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
131214  ExprList *pOrderBy = p->pOrderBy;
131215  int nOrderBy = p->pOrderBy->nExpr;
131216  sqlite3 *db = pParse->db;
131217  KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
131218  if( pRet ){
131219    int i;
131220    for(i=0; i<nOrderBy; i++){
131221      struct ExprList_item *pItem = &pOrderBy->a[i];
131222      Expr *pTerm = pItem->pExpr;
131223      CollSeq *pColl;
131224
131225      if( pTerm->flags & EP_Collate ){
131226        pColl = sqlite3ExprCollSeq(pParse, pTerm);
131227      }else{
131228        pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
131229        if( pColl==0 ) pColl = db->pDfltColl;
131230        pOrderBy->a[i].pExpr =
131231          sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
131232      }
131233      assert( sqlite3KeyInfoIsWriteable(pRet) );
131234      pRet->aColl[i] = pColl;
131235      pRet->aSortFlags[i] = pOrderBy->a[i].sortFlags;
131236    }
131237  }
131238
131239  return pRet;
131240}
131241
131242#ifndef SQLITE_OMIT_CTE
131243/*
131244** This routine generates VDBE code to compute the content of a WITH RECURSIVE
131245** query of the form:
131246**
131247**   <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
131248**                         \___________/             \_______________/
131249**                           p->pPrior                      p
131250**
131251**
131252** There is exactly one reference to the recursive-table in the FROM clause
131253** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
131254**
131255** The setup-query runs once to generate an initial set of rows that go
131256** into a Queue table.  Rows are extracted from the Queue table one by
131257** one.  Each row extracted from Queue is output to pDest.  Then the single
131258** extracted row (now in the iCurrent table) becomes the content of the
131259** recursive-table for a recursive-query run.  The output of the recursive-query
131260** is added back into the Queue table.  Then another row is extracted from Queue
131261** and the iteration continues until the Queue table is empty.
131262**
131263** If the compound query operator is UNION then no duplicate rows are ever
131264** inserted into the Queue table.  The iDistinct table keeps a copy of all rows
131265** that have ever been inserted into Queue and causes duplicates to be
131266** discarded.  If the operator is UNION ALL, then duplicates are allowed.
131267**
131268** If the query has an ORDER BY, then entries in the Queue table are kept in
131269** ORDER BY order and the first entry is extracted for each cycle.  Without
131270** an ORDER BY, the Queue table is just a FIFO.
131271**
131272** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
131273** have been output to pDest.  A LIMIT of zero means to output no rows and a
131274** negative LIMIT means to output all rows.  If there is also an OFFSET clause
131275** with a positive value, then the first OFFSET outputs are discarded rather
131276** than being sent to pDest.  The LIMIT count does not begin until after OFFSET
131277** rows have been skipped.
131278*/
131279static void generateWithRecursiveQuery(
131280  Parse *pParse,        /* Parsing context */
131281  Select *p,            /* The recursive SELECT to be coded */
131282  SelectDest *pDest     /* What to do with query results */
131283){
131284  SrcList *pSrc = p->pSrc;      /* The FROM clause of the recursive query */
131285  int nCol = p->pEList->nExpr;  /* Number of columns in the recursive table */
131286  Vdbe *v = pParse->pVdbe;      /* The prepared statement under construction */
131287  Select *pSetup = p->pPrior;   /* The setup query */
131288  int addrTop;                  /* Top of the loop */
131289  int addrCont, addrBreak;      /* CONTINUE and BREAK addresses */
131290  int iCurrent = 0;             /* The Current table */
131291  int regCurrent;               /* Register holding Current table */
131292  int iQueue;                   /* The Queue table */
131293  int iDistinct = 0;            /* To ensure unique results if UNION */
131294  int eDest = SRT_Fifo;         /* How to write to Queue */
131295  SelectDest destQueue;         /* SelectDest targetting the Queue table */
131296  int i;                        /* Loop counter */
131297  int rc;                       /* Result code */
131298  ExprList *pOrderBy;           /* The ORDER BY clause */
131299  Expr *pLimit;                 /* Saved LIMIT and OFFSET */
131300  int regLimit, regOffset;      /* Registers used by LIMIT and OFFSET */
131301
131302#ifndef SQLITE_OMIT_WINDOWFUNC
131303  if( p->pWin ){
131304    sqlite3ErrorMsg(pParse, "cannot use window functions in recursive queries");
131305    return;
131306  }
131307#endif
131308
131309  /* Obtain authorization to do a recursive query */
131310  if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
131311
131312  /* Process the LIMIT and OFFSET clauses, if they exist */
131313  addrBreak = sqlite3VdbeMakeLabel(pParse);
131314  p->nSelectRow = 320;  /* 4 billion rows */
131315  computeLimitRegisters(pParse, p, addrBreak);
131316  pLimit = p->pLimit;
131317  regLimit = p->iLimit;
131318  regOffset = p->iOffset;
131319  p->pLimit = 0;
131320  p->iLimit = p->iOffset = 0;
131321  pOrderBy = p->pOrderBy;
131322
131323  /* Locate the cursor number of the Current table */
131324  for(i=0; ALWAYS(i<pSrc->nSrc); i++){
131325    if( pSrc->a[i].fg.isRecursive ){
131326      iCurrent = pSrc->a[i].iCursor;
131327      break;
131328    }
131329  }
131330
131331  /* Allocate cursors numbers for Queue and Distinct.  The cursor number for
131332  ** the Distinct table must be exactly one greater than Queue in order
131333  ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
131334  iQueue = pParse->nTab++;
131335  if( p->op==TK_UNION ){
131336    eDest = pOrderBy ? SRT_DistQueue : SRT_DistFifo;
131337    iDistinct = pParse->nTab++;
131338  }else{
131339    eDest = pOrderBy ? SRT_Queue : SRT_Fifo;
131340  }
131341  sqlite3SelectDestInit(&destQueue, eDest, iQueue);
131342
131343  /* Allocate cursors for Current, Queue, and Distinct. */
131344  regCurrent = ++pParse->nMem;
131345  sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
131346  if( pOrderBy ){
131347    KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
131348    sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
131349                      (char*)pKeyInfo, P4_KEYINFO);
131350    destQueue.pOrderBy = pOrderBy;
131351  }else{
131352    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol);
131353  }
131354  VdbeComment((v, "Queue table"));
131355  if( iDistinct ){
131356    p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
131357    p->selFlags |= SF_UsesEphemeral;
131358  }
131359
131360  /* Detach the ORDER BY clause from the compound SELECT */
131361  p->pOrderBy = 0;
131362
131363  /* Store the results of the setup-query in Queue. */
131364  pSetup->pNext = 0;
131365  ExplainQueryPlan((pParse, 1, "SETUP"));
131366  rc = sqlite3Select(pParse, pSetup, &destQueue);
131367  pSetup->pNext = p;
131368  if( rc ) goto end_of_recursive_query;
131369
131370  /* Find the next row in the Queue and output that row */
131371  addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v);
131372
131373  /* Transfer the next row in Queue over to Current */
131374  sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
131375  if( pOrderBy ){
131376    sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
131377  }else{
131378    sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
131379  }
131380  sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
131381
131382  /* Output the single row in Current */
131383  addrCont = sqlite3VdbeMakeLabel(pParse);
131384  codeOffset(v, regOffset, addrCont);
131385  selectInnerLoop(pParse, p, iCurrent,
131386      0, 0, pDest, addrCont, addrBreak);
131387  if( regLimit ){
131388    sqlite3VdbeAddOp2(v, OP_DecrJumpZero, regLimit, addrBreak);
131389    VdbeCoverage(v);
131390  }
131391  sqlite3VdbeResolveLabel(v, addrCont);
131392
131393  /* Execute the recursive SELECT taking the single row in Current as
131394  ** the value for the recursive-table. Store the results in the Queue.
131395  */
131396  if( p->selFlags & SF_Aggregate ){
131397    sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
131398  }else{
131399    p->pPrior = 0;
131400    ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"));
131401    sqlite3Select(pParse, p, &destQueue);
131402    assert( p->pPrior==0 );
131403    p->pPrior = pSetup;
131404  }
131405
131406  /* Keep running the loop until the Queue is empty */
131407  sqlite3VdbeGoto(v, addrTop);
131408  sqlite3VdbeResolveLabel(v, addrBreak);
131409
131410end_of_recursive_query:
131411  sqlite3ExprListDelete(pParse->db, p->pOrderBy);
131412  p->pOrderBy = pOrderBy;
131413  p->pLimit = pLimit;
131414  return;
131415}
131416#endif /* SQLITE_OMIT_CTE */
131417
131418/* Forward references */
131419static int multiSelectOrderBy(
131420  Parse *pParse,        /* Parsing context */
131421  Select *p,            /* The right-most of SELECTs to be coded */
131422  SelectDest *pDest     /* What to do with query results */
131423);
131424
131425/*
131426** Handle the special case of a compound-select that originates from a
131427** VALUES clause.  By handling this as a special case, we avoid deep
131428** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
131429** on a VALUES clause.
131430**
131431** Because the Select object originates from a VALUES clause:
131432**   (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1
131433**   (2) All terms are UNION ALL
131434**   (3) There is no ORDER BY clause
131435**
131436** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES
131437** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))").
131438** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case.
131439** Since the limit is exactly 1, we only need to evalutes the left-most VALUES.
131440*/
131441static int multiSelectValues(
131442  Parse *pParse,        /* Parsing context */
131443  Select *p,            /* The right-most of SELECTs to be coded */
131444  SelectDest *pDest     /* What to do with query results */
131445){
131446  int nRow = 1;
131447  int rc = 0;
131448  int bShowAll = p->pLimit==0;
131449  assert( p->selFlags & SF_MultiValue );
131450  do{
131451    assert( p->selFlags & SF_Values );
131452    assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
131453    assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
131454#ifndef SQLITE_OMIT_WINDOWFUNC
131455    if( p->pWin ) return -1;
131456#endif
131457    if( p->pPrior==0 ) break;
131458    assert( p->pPrior->pNext==p );
131459    p = p->pPrior;
131460    nRow += bShowAll;
131461  }while(1);
131462  ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,
131463                    nRow==1 ? "" : "S"));
131464  while( p ){
131465    selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
131466    if( !bShowAll ) break;
131467    p->nSelectRow = nRow;
131468    p = p->pNext;
131469  }
131470  return rc;
131471}
131472
131473/*
131474** This routine is called to process a compound query form from
131475** two or more separate queries using UNION, UNION ALL, EXCEPT, or
131476** INTERSECT
131477**
131478** "p" points to the right-most of the two queries.  the query on the
131479** left is p->pPrior.  The left query could also be a compound query
131480** in which case this routine will be called recursively.
131481**
131482** The results of the total query are to be written into a destination
131483** of type eDest with parameter iParm.
131484**
131485** Example 1:  Consider a three-way compound SQL statement.
131486**
131487**     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
131488**
131489** This statement is parsed up as follows:
131490**
131491**     SELECT c FROM t3
131492**      |
131493**      `----->  SELECT b FROM t2
131494**                |
131495**                `------>  SELECT a FROM t1
131496**
131497** The arrows in the diagram above represent the Select.pPrior pointer.
131498** So if this routine is called with p equal to the t3 query, then
131499** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
131500**
131501** Notice that because of the way SQLite parses compound SELECTs, the
131502** individual selects always group from left to right.
131503*/
131504static int multiSelect(
131505  Parse *pParse,        /* Parsing context */
131506  Select *p,            /* The right-most of SELECTs to be coded */
131507  SelectDest *pDest     /* What to do with query results */
131508){
131509  int rc = SQLITE_OK;   /* Success code from a subroutine */
131510  Select *pPrior;       /* Another SELECT immediately to our left */
131511  Vdbe *v;              /* Generate code to this VDBE */
131512  SelectDest dest;      /* Alternative data destination */
131513  Select *pDelete = 0;  /* Chain of simple selects to delete */
131514  sqlite3 *db;          /* Database connection */
131515
131516  /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
131517  ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
131518  */
131519  assert( p && p->pPrior );  /* Calling function guarantees this much */
131520  assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
131521  assert( p->selFlags & SF_Compound );
131522  db = pParse->db;
131523  pPrior = p->pPrior;
131524  dest = *pDest;
131525  if( pPrior->pOrderBy || pPrior->pLimit ){
131526    sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
131527      pPrior->pOrderBy!=0 ? "ORDER BY" : "LIMIT", selectOpName(p->op));
131528    rc = 1;
131529    goto multi_select_end;
131530  }
131531
131532  v = sqlite3GetVdbe(pParse);
131533  assert( v!=0 );  /* The VDBE already created by calling function */
131534
131535  /* Create the destination temporary table if necessary
131536  */
131537  if( dest.eDest==SRT_EphemTab ){
131538    assert( p->pEList );
131539    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
131540    dest.eDest = SRT_Table;
131541  }
131542
131543  /* Special handling for a compound-select that originates as a VALUES clause.
131544  */
131545  if( p->selFlags & SF_MultiValue ){
131546    rc = multiSelectValues(pParse, p, &dest);
131547    if( rc>=0 ) goto multi_select_end;
131548    rc = SQLITE_OK;
131549  }
131550
131551  /* Make sure all SELECTs in the statement have the same number of elements
131552  ** in their result sets.
131553  */
131554  assert( p->pEList && pPrior->pEList );
131555  assert( p->pEList->nExpr==pPrior->pEList->nExpr );
131556
131557#ifndef SQLITE_OMIT_CTE
131558  if( p->selFlags & SF_Recursive ){
131559    generateWithRecursiveQuery(pParse, p, &dest);
131560  }else
131561#endif
131562
131563  /* Compound SELECTs that have an ORDER BY clause are handled separately.
131564  */
131565  if( p->pOrderBy ){
131566    return multiSelectOrderBy(pParse, p, pDest);
131567  }else{
131568
131569#ifndef SQLITE_OMIT_EXPLAIN
131570    if( pPrior->pPrior==0 ){
131571      ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"));
131572      ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
131573    }
131574#endif
131575
131576    /* Generate code for the left and right SELECT statements.
131577    */
131578    switch( p->op ){
131579      case TK_ALL: {
131580        int addr = 0;
131581        int nLimit;
131582        assert( !pPrior->pLimit );
131583        pPrior->iLimit = p->iLimit;
131584        pPrior->iOffset = p->iOffset;
131585        pPrior->pLimit = p->pLimit;
131586        rc = sqlite3Select(pParse, pPrior, &dest);
131587        p->pLimit = 0;
131588        if( rc ){
131589          goto multi_select_end;
131590        }
131591        p->pPrior = 0;
131592        p->iLimit = pPrior->iLimit;
131593        p->iOffset = pPrior->iOffset;
131594        if( p->iLimit ){
131595          addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
131596          VdbeComment((v, "Jump ahead if LIMIT reached"));
131597          if( p->iOffset ){
131598            sqlite3VdbeAddOp3(v, OP_OffsetLimit,
131599                              p->iLimit, p->iOffset+1, p->iOffset);
131600          }
131601        }
131602        ExplainQueryPlan((pParse, 1, "UNION ALL"));
131603        rc = sqlite3Select(pParse, p, &dest);
131604        testcase( rc!=SQLITE_OK );
131605        pDelete = p->pPrior;
131606        p->pPrior = pPrior;
131607        p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
131608        if( pPrior->pLimit
131609         && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
131610         && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
131611        ){
131612          p->nSelectRow = sqlite3LogEst((u64)nLimit);
131613        }
131614        if( addr ){
131615          sqlite3VdbeJumpHere(v, addr);
131616        }
131617        break;
131618      }
131619      case TK_EXCEPT:
131620      case TK_UNION: {
131621        int unionTab;    /* Cursor number of the temp table holding result */
131622        u8 op = 0;       /* One of the SRT_ operations to apply to self */
131623        int priorOp;     /* The SRT_ operation to apply to prior selects */
131624        Expr *pLimit;    /* Saved values of p->nLimit  */
131625        int addr;
131626        SelectDest uniondest;
131627
131628        testcase( p->op==TK_EXCEPT );
131629        testcase( p->op==TK_UNION );
131630        priorOp = SRT_Union;
131631        if( dest.eDest==priorOp ){
131632          /* We can reuse a temporary table generated by a SELECT to our
131633          ** right.
131634          */
131635          assert( p->pLimit==0 );      /* Not allowed on leftward elements */
131636          unionTab = dest.iSDParm;
131637        }else{
131638          /* We will need to create our own temporary table to hold the
131639          ** intermediate results.
131640          */
131641          unionTab = pParse->nTab++;
131642          assert( p->pOrderBy==0 );
131643          addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
131644          assert( p->addrOpenEphm[0] == -1 );
131645          p->addrOpenEphm[0] = addr;
131646          findRightmost(p)->selFlags |= SF_UsesEphemeral;
131647          assert( p->pEList );
131648        }
131649
131650        /* Code the SELECT statements to our left
131651        */
131652        assert( !pPrior->pOrderBy );
131653        sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
131654        rc = sqlite3Select(pParse, pPrior, &uniondest);
131655        if( rc ){
131656          goto multi_select_end;
131657        }
131658
131659        /* Code the current SELECT statement
131660        */
131661        if( p->op==TK_EXCEPT ){
131662          op = SRT_Except;
131663        }else{
131664          assert( p->op==TK_UNION );
131665          op = SRT_Union;
131666        }
131667        p->pPrior = 0;
131668        pLimit = p->pLimit;
131669        p->pLimit = 0;
131670        uniondest.eDest = op;
131671        ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
131672                          selectOpName(p->op)));
131673        rc = sqlite3Select(pParse, p, &uniondest);
131674        testcase( rc!=SQLITE_OK );
131675        assert( p->pOrderBy==0 );
131676        pDelete = p->pPrior;
131677        p->pPrior = pPrior;
131678        p->pOrderBy = 0;
131679        if( p->op==TK_UNION ){
131680          p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
131681        }
131682        sqlite3ExprDelete(db, p->pLimit);
131683        p->pLimit = pLimit;
131684        p->iLimit = 0;
131685        p->iOffset = 0;
131686
131687        /* Convert the data in the temporary table into whatever form
131688        ** it is that we currently need.
131689        */
131690        assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
131691        assert( p->pEList || db->mallocFailed );
131692        if( dest.eDest!=priorOp && db->mallocFailed==0 ){
131693          int iCont, iBreak, iStart;
131694          iBreak = sqlite3VdbeMakeLabel(pParse);
131695          iCont = sqlite3VdbeMakeLabel(pParse);
131696          computeLimitRegisters(pParse, p, iBreak);
131697          sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
131698          iStart = sqlite3VdbeCurrentAddr(v);
131699          selectInnerLoop(pParse, p, unionTab,
131700                          0, 0, &dest, iCont, iBreak);
131701          sqlite3VdbeResolveLabel(v, iCont);
131702          sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
131703          sqlite3VdbeResolveLabel(v, iBreak);
131704          sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
131705        }
131706        break;
131707      }
131708      default: assert( p->op==TK_INTERSECT ); {
131709        int tab1, tab2;
131710        int iCont, iBreak, iStart;
131711        Expr *pLimit;
131712        int addr;
131713        SelectDest intersectdest;
131714        int r1;
131715
131716        /* INTERSECT is different from the others since it requires
131717        ** two temporary tables.  Hence it has its own case.  Begin
131718        ** by allocating the tables we will need.
131719        */
131720        tab1 = pParse->nTab++;
131721        tab2 = pParse->nTab++;
131722        assert( p->pOrderBy==0 );
131723
131724        addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
131725        assert( p->addrOpenEphm[0] == -1 );
131726        p->addrOpenEphm[0] = addr;
131727        findRightmost(p)->selFlags |= SF_UsesEphemeral;
131728        assert( p->pEList );
131729
131730        /* Code the SELECTs to our left into temporary table "tab1".
131731        */
131732        sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
131733        rc = sqlite3Select(pParse, pPrior, &intersectdest);
131734        if( rc ){
131735          goto multi_select_end;
131736        }
131737
131738        /* Code the current SELECT into temporary table "tab2"
131739        */
131740        addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
131741        assert( p->addrOpenEphm[1] == -1 );
131742        p->addrOpenEphm[1] = addr;
131743        p->pPrior = 0;
131744        pLimit = p->pLimit;
131745        p->pLimit = 0;
131746        intersectdest.iSDParm = tab2;
131747        ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
131748                          selectOpName(p->op)));
131749        rc = sqlite3Select(pParse, p, &intersectdest);
131750        testcase( rc!=SQLITE_OK );
131751        pDelete = p->pPrior;
131752        p->pPrior = pPrior;
131753        if( p->nSelectRow>pPrior->nSelectRow ){
131754          p->nSelectRow = pPrior->nSelectRow;
131755        }
131756        sqlite3ExprDelete(db, p->pLimit);
131757        p->pLimit = pLimit;
131758
131759        /* Generate code to take the intersection of the two temporary
131760        ** tables.
131761        */
131762        if( rc ) break;
131763        assert( p->pEList );
131764        iBreak = sqlite3VdbeMakeLabel(pParse);
131765        iCont = sqlite3VdbeMakeLabel(pParse);
131766        computeLimitRegisters(pParse, p, iBreak);
131767        sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
131768        r1 = sqlite3GetTempReg(pParse);
131769        iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
131770        sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
131771        VdbeCoverage(v);
131772        sqlite3ReleaseTempReg(pParse, r1);
131773        selectInnerLoop(pParse, p, tab1,
131774                        0, 0, &dest, iCont, iBreak);
131775        sqlite3VdbeResolveLabel(v, iCont);
131776        sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
131777        sqlite3VdbeResolveLabel(v, iBreak);
131778        sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
131779        sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
131780        break;
131781      }
131782    }
131783
131784  #ifndef SQLITE_OMIT_EXPLAIN
131785    if( p->pNext==0 ){
131786      ExplainQueryPlanPop(pParse);
131787    }
131788  #endif
131789  }
131790  if( pParse->nErr ) goto multi_select_end;
131791
131792  /* Compute collating sequences used by
131793  ** temporary tables needed to implement the compound select.
131794  ** Attach the KeyInfo structure to all temporary tables.
131795  **
131796  ** This section is run by the right-most SELECT statement only.
131797  ** SELECT statements to the left always skip this part.  The right-most
131798  ** SELECT might also skip this part if it has no ORDER BY clause and
131799  ** no temp tables are required.
131800  */
131801  if( p->selFlags & SF_UsesEphemeral ){
131802    int i;                        /* Loop counter */
131803    KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
131804    Select *pLoop;                /* For looping through SELECT statements */
131805    CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
131806    int nCol;                     /* Number of columns in result set */
131807
131808    assert( p->pNext==0 );
131809    nCol = p->pEList->nExpr;
131810    pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
131811    if( !pKeyInfo ){
131812      rc = SQLITE_NOMEM_BKPT;
131813      goto multi_select_end;
131814    }
131815    for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
131816      *apColl = multiSelectCollSeq(pParse, p, i);
131817      if( 0==*apColl ){
131818        *apColl = db->pDfltColl;
131819      }
131820    }
131821
131822    for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
131823      for(i=0; i<2; i++){
131824        int addr = pLoop->addrOpenEphm[i];
131825        if( addr<0 ){
131826          /* If [0] is unused then [1] is also unused.  So we can
131827          ** always safely abort as soon as the first unused slot is found */
131828          assert( pLoop->addrOpenEphm[1]<0 );
131829          break;
131830        }
131831        sqlite3VdbeChangeP2(v, addr, nCol);
131832        sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
131833                            P4_KEYINFO);
131834        pLoop->addrOpenEphm[i] = -1;
131835      }
131836    }
131837    sqlite3KeyInfoUnref(pKeyInfo);
131838  }
131839
131840multi_select_end:
131841  pDest->iSdst = dest.iSdst;
131842  pDest->nSdst = dest.nSdst;
131843  sqlite3SelectDelete(db, pDelete);
131844  return rc;
131845}
131846#endif /* SQLITE_OMIT_COMPOUND_SELECT */
131847
131848/*
131849** Error message for when two or more terms of a compound select have different
131850** size result sets.
131851*/
131852SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
131853  if( p->selFlags & SF_Values ){
131854    sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
131855  }else{
131856    sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
131857      " do not have the same number of result columns", selectOpName(p->op));
131858  }
131859}
131860
131861/*
131862** Code an output subroutine for a coroutine implementation of a
131863** SELECT statment.
131864**
131865** The data to be output is contained in pIn->iSdst.  There are
131866** pIn->nSdst columns to be output.  pDest is where the output should
131867** be sent.
131868**
131869** regReturn is the number of the register holding the subroutine
131870** return address.
131871**
131872** If regPrev>0 then it is the first register in a vector that
131873** records the previous output.  mem[regPrev] is a flag that is false
131874** if there has been no previous output.  If regPrev>0 then code is
131875** generated to suppress duplicates.  pKeyInfo is used for comparing
131876** keys.
131877**
131878** If the LIMIT found in p->iLimit is reached, jump immediately to
131879** iBreak.
131880*/
131881static int generateOutputSubroutine(
131882  Parse *pParse,          /* Parsing context */
131883  Select *p,              /* The SELECT statement */
131884  SelectDest *pIn,        /* Coroutine supplying data */
131885  SelectDest *pDest,      /* Where to send the data */
131886  int regReturn,          /* The return address register */
131887  int regPrev,            /* Previous result register.  No uniqueness if 0 */
131888  KeyInfo *pKeyInfo,      /* For comparing with previous entry */
131889  int iBreak              /* Jump here if we hit the LIMIT */
131890){
131891  Vdbe *v = pParse->pVdbe;
131892  int iContinue;
131893  int addr;
131894
131895  addr = sqlite3VdbeCurrentAddr(v);
131896  iContinue = sqlite3VdbeMakeLabel(pParse);
131897
131898  /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
131899  */
131900  if( regPrev ){
131901    int addr1, addr2;
131902    addr1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v);
131903    addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
131904                              (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
131905    sqlite3VdbeAddOp3(v, OP_Jump, addr2+2, iContinue, addr2+2); VdbeCoverage(v);
131906    sqlite3VdbeJumpHere(v, addr1);
131907    sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
131908    sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
131909  }
131910  if( pParse->db->mallocFailed ) return 0;
131911
131912  /* Suppress the first OFFSET entries if there is an OFFSET clause
131913  */
131914  codeOffset(v, p->iOffset, iContinue);
131915
131916  assert( pDest->eDest!=SRT_Exists );
131917  assert( pDest->eDest!=SRT_Table );
131918  switch( pDest->eDest ){
131919    /* Store the result as data using a unique key.
131920    */
131921    case SRT_EphemTab: {
131922      int r1 = sqlite3GetTempReg(pParse);
131923      int r2 = sqlite3GetTempReg(pParse);
131924      sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
131925      sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
131926      sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
131927      sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
131928      sqlite3ReleaseTempReg(pParse, r2);
131929      sqlite3ReleaseTempReg(pParse, r1);
131930      break;
131931    }
131932
131933#ifndef SQLITE_OMIT_SUBQUERY
131934    /* If we are creating a set for an "expr IN (SELECT ...)".
131935    */
131936    case SRT_Set: {
131937      int r1;
131938      testcase( pIn->nSdst>1 );
131939      r1 = sqlite3GetTempReg(pParse);
131940      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
131941          r1, pDest->zAffSdst, pIn->nSdst);
131942      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
131943                           pIn->iSdst, pIn->nSdst);
131944      sqlite3ReleaseTempReg(pParse, r1);
131945      break;
131946    }
131947
131948    /* If this is a scalar select that is part of an expression, then
131949    ** store the results in the appropriate memory cell and break out
131950    ** of the scan loop.  Note that the select might return multiple columns
131951    ** if it is the RHS of a row-value IN operator.
131952    */
131953    case SRT_Mem: {
131954      if( pParse->nErr==0 ){
131955        testcase( pIn->nSdst>1 );
131956        sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
131957      }
131958      /* The LIMIT clause will jump out of the loop for us */
131959      break;
131960    }
131961#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
131962
131963    /* The results are stored in a sequence of registers
131964    ** starting at pDest->iSdst.  Then the co-routine yields.
131965    */
131966    case SRT_Coroutine: {
131967      if( pDest->iSdst==0 ){
131968        pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
131969        pDest->nSdst = pIn->nSdst;
131970      }
131971      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
131972      sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
131973      break;
131974    }
131975
131976    /* If none of the above, then the result destination must be
131977    ** SRT_Output.  This routine is never called with any other
131978    ** destination other than the ones handled above or SRT_Output.
131979    **
131980    ** For SRT_Output, results are stored in a sequence of registers.
131981    ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
131982    ** return the next row of result.
131983    */
131984    default: {
131985      assert( pDest->eDest==SRT_Output );
131986      sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
131987      break;
131988    }
131989  }
131990
131991  /* Jump to the end of the loop if the LIMIT is reached.
131992  */
131993  if( p->iLimit ){
131994    sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
131995  }
131996
131997  /* Generate the subroutine return
131998  */
131999  sqlite3VdbeResolveLabel(v, iContinue);
132000  sqlite3VdbeAddOp1(v, OP_Return, regReturn);
132001
132002  return addr;
132003}
132004
132005/*
132006** Alternative compound select code generator for cases when there
132007** is an ORDER BY clause.
132008**
132009** We assume a query of the following form:
132010**
132011**      <selectA>  <operator>  <selectB>  ORDER BY <orderbylist>
132012**
132013** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT.  The idea
132014** is to code both <selectA> and <selectB> with the ORDER BY clause as
132015** co-routines.  Then run the co-routines in parallel and merge the results
132016** into the output.  In addition to the two coroutines (called selectA and
132017** selectB) there are 7 subroutines:
132018**
132019**    outA:    Move the output of the selectA coroutine into the output
132020**             of the compound query.
132021**
132022**    outB:    Move the output of the selectB coroutine into the output
132023**             of the compound query.  (Only generated for UNION and
132024**             UNION ALL.  EXCEPT and INSERTSECT never output a row that
132025**             appears only in B.)
132026**
132027**    AltB:    Called when there is data from both coroutines and A<B.
132028**
132029**    AeqB:    Called when there is data from both coroutines and A==B.
132030**
132031**    AgtB:    Called when there is data from both coroutines and A>B.
132032**
132033**    EofA:    Called when data is exhausted from selectA.
132034**
132035**    EofB:    Called when data is exhausted from selectB.
132036**
132037** The implementation of the latter five subroutines depend on which
132038** <operator> is used:
132039**
132040**
132041**             UNION ALL         UNION            EXCEPT          INTERSECT
132042**          -------------  -----------------  --------------  -----------------
132043**   AltB:   outA, nextA      outA, nextA       outA, nextA         nextA
132044**
132045**   AeqB:   outA, nextA         nextA             nextA         outA, nextA
132046**
132047**   AgtB:   outB, nextB      outB, nextB          nextB            nextB
132048**
132049**   EofA:   outB, nextB      outB, nextB          halt             halt
132050**
132051**   EofB:   outA, nextA      outA, nextA       outA, nextA         halt
132052**
132053** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
132054** causes an immediate jump to EofA and an EOF on B following nextB causes
132055** an immediate jump to EofB.  Within EofA and EofB, and EOF on entry or
132056** following nextX causes a jump to the end of the select processing.
132057**
132058** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
132059** within the output subroutine.  The regPrev register set holds the previously
132060** output value.  A comparison is made against this value and the output
132061** is skipped if the next results would be the same as the previous.
132062**
132063** The implementation plan is to implement the two coroutines and seven
132064** subroutines first, then put the control logic at the bottom.  Like this:
132065**
132066**          goto Init
132067**     coA: coroutine for left query (A)
132068**     coB: coroutine for right query (B)
132069**    outA: output one row of A
132070**    outB: output one row of B (UNION and UNION ALL only)
132071**    EofA: ...
132072**    EofB: ...
132073**    AltB: ...
132074**    AeqB: ...
132075**    AgtB: ...
132076**    Init: initialize coroutine registers
132077**          yield coA
132078**          if eof(A) goto EofA
132079**          yield coB
132080**          if eof(B) goto EofB
132081**    Cmpr: Compare A, B
132082**          Jump AltB, AeqB, AgtB
132083**     End: ...
132084**
132085** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
132086** actually called using Gosub and they do not Return.  EofA and EofB loop
132087** until all data is exhausted then jump to the "end" labe.  AltB, AeqB,
132088** and AgtB jump to either L2 or to one of EofA or EofB.
132089*/
132090#ifndef SQLITE_OMIT_COMPOUND_SELECT
132091static int multiSelectOrderBy(
132092  Parse *pParse,        /* Parsing context */
132093  Select *p,            /* The right-most of SELECTs to be coded */
132094  SelectDest *pDest     /* What to do with query results */
132095){
132096  int i, j;             /* Loop counters */
132097  Select *pPrior;       /* Another SELECT immediately to our left */
132098  Vdbe *v;              /* Generate code to this VDBE */
132099  SelectDest destA;     /* Destination for coroutine A */
132100  SelectDest destB;     /* Destination for coroutine B */
132101  int regAddrA;         /* Address register for select-A coroutine */
132102  int regAddrB;         /* Address register for select-B coroutine */
132103  int addrSelectA;      /* Address of the select-A coroutine */
132104  int addrSelectB;      /* Address of the select-B coroutine */
132105  int regOutA;          /* Address register for the output-A subroutine */
132106  int regOutB;          /* Address register for the output-B subroutine */
132107  int addrOutA;         /* Address of the output-A subroutine */
132108  int addrOutB = 0;     /* Address of the output-B subroutine */
132109  int addrEofA;         /* Address of the select-A-exhausted subroutine */
132110  int addrEofA_noB;     /* Alternate addrEofA if B is uninitialized */
132111  int addrEofB;         /* Address of the select-B-exhausted subroutine */
132112  int addrAltB;         /* Address of the A<B subroutine */
132113  int addrAeqB;         /* Address of the A==B subroutine */
132114  int addrAgtB;         /* Address of the A>B subroutine */
132115  int regLimitA;        /* Limit register for select-A */
132116  int regLimitB;        /* Limit register for select-A */
132117  int regPrev;          /* A range of registers to hold previous output */
132118  int savedLimit;       /* Saved value of p->iLimit */
132119  int savedOffset;      /* Saved value of p->iOffset */
132120  int labelCmpr;        /* Label for the start of the merge algorithm */
132121  int labelEnd;         /* Label for the end of the overall SELECT stmt */
132122  int addr1;            /* Jump instructions that get retargetted */
132123  int op;               /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
132124  KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
132125  KeyInfo *pKeyMerge;   /* Comparison information for merging rows */
132126  sqlite3 *db;          /* Database connection */
132127  ExprList *pOrderBy;   /* The ORDER BY clause */
132128  int nOrderBy;         /* Number of terms in the ORDER BY clause */
132129  int *aPermute;        /* Mapping from ORDER BY terms to result set columns */
132130
132131  assert( p->pOrderBy!=0 );
132132  assert( pKeyDup==0 ); /* "Managed" code needs this.  Ticket #3382. */
132133  db = pParse->db;
132134  v = pParse->pVdbe;
132135  assert( v!=0 );       /* Already thrown the error if VDBE alloc failed */
132136  labelEnd = sqlite3VdbeMakeLabel(pParse);
132137  labelCmpr = sqlite3VdbeMakeLabel(pParse);
132138
132139
132140  /* Patch up the ORDER BY clause
132141  */
132142  op = p->op;
132143  pPrior = p->pPrior;
132144  assert( pPrior->pOrderBy==0 );
132145  pOrderBy = p->pOrderBy;
132146  assert( pOrderBy );
132147  nOrderBy = pOrderBy->nExpr;
132148
132149  /* For operators other than UNION ALL we have to make sure that
132150  ** the ORDER BY clause covers every term of the result set.  Add
132151  ** terms to the ORDER BY clause as necessary.
132152  */
132153  if( op!=TK_ALL ){
132154    for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
132155      struct ExprList_item *pItem;
132156      for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
132157        assert( pItem->u.x.iOrderByCol>0 );
132158        if( pItem->u.x.iOrderByCol==i ) break;
132159      }
132160      if( j==nOrderBy ){
132161        Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
132162        if( pNew==0 ) return SQLITE_NOMEM_BKPT;
132163        pNew->flags |= EP_IntValue;
132164        pNew->u.iValue = i;
132165        p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
132166        if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
132167      }
132168    }
132169  }
132170
132171  /* Compute the comparison permutation and keyinfo that is used with
132172  ** the permutation used to determine if the next
132173  ** row of results comes from selectA or selectB.  Also add explicit
132174  ** collations to the ORDER BY clause terms so that when the subqueries
132175  ** to the right and the left are evaluated, they use the correct
132176  ** collation.
132177  */
132178  aPermute = sqlite3DbMallocRawNN(db, sizeof(int)*(nOrderBy + 1));
132179  if( aPermute ){
132180    struct ExprList_item *pItem;
132181    aPermute[0] = nOrderBy;
132182    for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
132183      assert( pItem->u.x.iOrderByCol>0 );
132184      assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
132185      aPermute[i] = pItem->u.x.iOrderByCol - 1;
132186    }
132187    pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
132188  }else{
132189    pKeyMerge = 0;
132190  }
132191
132192  /* Reattach the ORDER BY clause to the query.
132193  */
132194  p->pOrderBy = pOrderBy;
132195  pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
132196
132197  /* Allocate a range of temporary registers and the KeyInfo needed
132198  ** for the logic that removes duplicate result rows when the
132199  ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
132200  */
132201  if( op==TK_ALL ){
132202    regPrev = 0;
132203  }else{
132204    int nExpr = p->pEList->nExpr;
132205    assert( nOrderBy>=nExpr || db->mallocFailed );
132206    regPrev = pParse->nMem+1;
132207    pParse->nMem += nExpr+1;
132208    sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
132209    pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
132210    if( pKeyDup ){
132211      assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
132212      for(i=0; i<nExpr; i++){
132213        pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
132214        pKeyDup->aSortFlags[i] = 0;
132215      }
132216    }
132217  }
132218
132219  /* Separate the left and the right query from one another
132220  */
132221  p->pPrior = 0;
132222  pPrior->pNext = 0;
132223  sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
132224  if( pPrior->pPrior==0 ){
132225    sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
132226  }
132227
132228  /* Compute the limit registers */
132229  computeLimitRegisters(pParse, p, labelEnd);
132230  if( p->iLimit && op==TK_ALL ){
132231    regLimitA = ++pParse->nMem;
132232    regLimitB = ++pParse->nMem;
132233    sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
132234                                  regLimitA);
132235    sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
132236  }else{
132237    regLimitA = regLimitB = 0;
132238  }
132239  sqlite3ExprDelete(db, p->pLimit);
132240  p->pLimit = 0;
132241
132242  regAddrA = ++pParse->nMem;
132243  regAddrB = ++pParse->nMem;
132244  regOutA = ++pParse->nMem;
132245  regOutB = ++pParse->nMem;
132246  sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
132247  sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
132248
132249  ExplainQueryPlan((pParse, 1, "MERGE (%s)", selectOpName(p->op)));
132250
132251  /* Generate a coroutine to evaluate the SELECT statement to the
132252  ** left of the compound operator - the "A" select.
132253  */
132254  addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
132255  addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
132256  VdbeComment((v, "left SELECT"));
132257  pPrior->iLimit = regLimitA;
132258  ExplainQueryPlan((pParse, 1, "LEFT"));
132259  sqlite3Select(pParse, pPrior, &destA);
132260  sqlite3VdbeEndCoroutine(v, regAddrA);
132261  sqlite3VdbeJumpHere(v, addr1);
132262
132263  /* Generate a coroutine to evaluate the SELECT statement on
132264  ** the right - the "B" select
132265  */
132266  addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
132267  addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
132268  VdbeComment((v, "right SELECT"));
132269  savedLimit = p->iLimit;
132270  savedOffset = p->iOffset;
132271  p->iLimit = regLimitB;
132272  p->iOffset = 0;
132273  ExplainQueryPlan((pParse, 1, "RIGHT"));
132274  sqlite3Select(pParse, p, &destB);
132275  p->iLimit = savedLimit;
132276  p->iOffset = savedOffset;
132277  sqlite3VdbeEndCoroutine(v, regAddrB);
132278
132279  /* Generate a subroutine that outputs the current row of the A
132280  ** select as the next output row of the compound select.
132281  */
132282  VdbeNoopComment((v, "Output routine for A"));
132283  addrOutA = generateOutputSubroutine(pParse,
132284                 p, &destA, pDest, regOutA,
132285                 regPrev, pKeyDup, labelEnd);
132286
132287  /* Generate a subroutine that outputs the current row of the B
132288  ** select as the next output row of the compound select.
132289  */
132290  if( op==TK_ALL || op==TK_UNION ){
132291    VdbeNoopComment((v, "Output routine for B"));
132292    addrOutB = generateOutputSubroutine(pParse,
132293                 p, &destB, pDest, regOutB,
132294                 regPrev, pKeyDup, labelEnd);
132295  }
132296  sqlite3KeyInfoUnref(pKeyDup);
132297
132298  /* Generate a subroutine to run when the results from select A
132299  ** are exhausted and only data in select B remains.
132300  */
132301  if( op==TK_EXCEPT || op==TK_INTERSECT ){
132302    addrEofA_noB = addrEofA = labelEnd;
132303  }else{
132304    VdbeNoopComment((v, "eof-A subroutine"));
132305    addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
132306    addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
132307                                     VdbeCoverage(v);
132308    sqlite3VdbeGoto(v, addrEofA);
132309    p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
132310  }
132311
132312  /* Generate a subroutine to run when the results from select B
132313  ** are exhausted and only data in select A remains.
132314  */
132315  if( op==TK_INTERSECT ){
132316    addrEofB = addrEofA;
132317    if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
132318  }else{
132319    VdbeNoopComment((v, "eof-B subroutine"));
132320    addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
132321    sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
132322    sqlite3VdbeGoto(v, addrEofB);
132323  }
132324
132325  /* Generate code to handle the case of A<B
132326  */
132327  VdbeNoopComment((v, "A-lt-B subroutine"));
132328  addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
132329  sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
132330  sqlite3VdbeGoto(v, labelCmpr);
132331
132332  /* Generate code to handle the case of A==B
132333  */
132334  if( op==TK_ALL ){
132335    addrAeqB = addrAltB;
132336  }else if( op==TK_INTERSECT ){
132337    addrAeqB = addrAltB;
132338    addrAltB++;
132339  }else{
132340    VdbeNoopComment((v, "A-eq-B subroutine"));
132341    addrAeqB =
132342    sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
132343    sqlite3VdbeGoto(v, labelCmpr);
132344  }
132345
132346  /* Generate code to handle the case of A>B
132347  */
132348  VdbeNoopComment((v, "A-gt-B subroutine"));
132349  addrAgtB = sqlite3VdbeCurrentAddr(v);
132350  if( op==TK_ALL || op==TK_UNION ){
132351    sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
132352  }
132353  sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
132354  sqlite3VdbeGoto(v, labelCmpr);
132355
132356  /* This code runs once to initialize everything.
132357  */
132358  sqlite3VdbeJumpHere(v, addr1);
132359  sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
132360  sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
132361
132362  /* Implement the main merge loop
132363  */
132364  sqlite3VdbeResolveLabel(v, labelCmpr);
132365  sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
132366  sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
132367                         (char*)pKeyMerge, P4_KEYINFO);
132368  sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
132369  sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
132370
132371  /* Jump to the this point in order to terminate the query.
132372  */
132373  sqlite3VdbeResolveLabel(v, labelEnd);
132374
132375  /* Reassembly the compound query so that it will be freed correctly
132376  ** by the calling function */
132377  if( p->pPrior ){
132378    sqlite3SelectDelete(db, p->pPrior);
132379  }
132380  p->pPrior = pPrior;
132381  pPrior->pNext = p;
132382
132383  /*** TBD:  Insert subroutine calls to close cursors on incomplete
132384  **** subqueries ****/
132385  ExplainQueryPlanPop(pParse);
132386  return pParse->nErr!=0;
132387}
132388#endif
132389
132390#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
132391
132392/* An instance of the SubstContext object describes an substitution edit
132393** to be performed on a parse tree.
132394**
132395** All references to columns in table iTable are to be replaced by corresponding
132396** expressions in pEList.
132397*/
132398typedef struct SubstContext {
132399  Parse *pParse;            /* The parsing context */
132400  int iTable;               /* Replace references to this table */
132401  int iNewTable;            /* New table number */
132402  int isLeftJoin;           /* Add TK_IF_NULL_ROW opcodes on each replacement */
132403  ExprList *pEList;         /* Replacement expressions */
132404} SubstContext;
132405
132406/* Forward Declarations */
132407static void substExprList(SubstContext*, ExprList*);
132408static void substSelect(SubstContext*, Select*, int);
132409
132410/*
132411** Scan through the expression pExpr.  Replace every reference to
132412** a column in table number iTable with a copy of the iColumn-th
132413** entry in pEList.  (But leave references to the ROWID column
132414** unchanged.)
132415**
132416** This routine is part of the flattening procedure.  A subquery
132417** whose result set is defined by pEList appears as entry in the
132418** FROM clause of a SELECT such that the VDBE cursor assigned to that
132419** FORM clause entry is iTable.  This routine makes the necessary
132420** changes to pExpr so that it refers directly to the source table
132421** of the subquery rather the result set of the subquery.
132422*/
132423static Expr *substExpr(
132424  SubstContext *pSubst,  /* Description of the substitution */
132425  Expr *pExpr            /* Expr in which substitution occurs */
132426){
132427  if( pExpr==0 ) return 0;
132428  if( ExprHasProperty(pExpr, EP_FromJoin)
132429   && pExpr->iRightJoinTable==pSubst->iTable
132430  ){
132431    pExpr->iRightJoinTable = pSubst->iNewTable;
132432  }
132433  if( pExpr->op==TK_COLUMN
132434   && pExpr->iTable==pSubst->iTable
132435   && !ExprHasProperty(pExpr, EP_FixedCol)
132436  ){
132437    if( pExpr->iColumn<0 ){
132438      pExpr->op = TK_NULL;
132439    }else{
132440      Expr *pNew;
132441      Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
132442      Expr ifNullRow;
132443      assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
132444      assert( pExpr->pRight==0 );
132445      if( sqlite3ExprIsVector(pCopy) ){
132446        sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
132447      }else{
132448        sqlite3 *db = pSubst->pParse->db;
132449        if( pSubst->isLeftJoin && pCopy->op!=TK_COLUMN ){
132450          memset(&ifNullRow, 0, sizeof(ifNullRow));
132451          ifNullRow.op = TK_IF_NULL_ROW;
132452          ifNullRow.pLeft = pCopy;
132453          ifNullRow.iTable = pSubst->iNewTable;
132454          ifNullRow.flags = EP_Skip;
132455          pCopy = &ifNullRow;
132456        }
132457        testcase( ExprHasProperty(pCopy, EP_Subquery) );
132458        pNew = sqlite3ExprDup(db, pCopy, 0);
132459        if( pNew && pSubst->isLeftJoin ){
132460          ExprSetProperty(pNew, EP_CanBeNull);
132461        }
132462        if( pNew && ExprHasProperty(pExpr,EP_FromJoin) ){
132463          pNew->iRightJoinTable = pExpr->iRightJoinTable;
132464          ExprSetProperty(pNew, EP_FromJoin);
132465        }
132466        sqlite3ExprDelete(db, pExpr);
132467        pExpr = pNew;
132468
132469        /* Ensure that the expression now has an implicit collation sequence,
132470        ** just as it did when it was a column of a view or sub-query. */
132471        if( pExpr ){
132472          if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE ){
132473            CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
132474            pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
132475                (pColl ? pColl->zName : "BINARY")
132476            );
132477          }
132478          ExprClearProperty(pExpr, EP_Collate);
132479        }
132480      }
132481    }
132482  }else{
132483    if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
132484      pExpr->iTable = pSubst->iNewTable;
132485    }
132486    pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
132487    pExpr->pRight = substExpr(pSubst, pExpr->pRight);
132488    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
132489      substSelect(pSubst, pExpr->x.pSelect, 1);
132490    }else{
132491      substExprList(pSubst, pExpr->x.pList);
132492    }
132493#ifndef SQLITE_OMIT_WINDOWFUNC
132494    if( ExprHasProperty(pExpr, EP_WinFunc) ){
132495      Window *pWin = pExpr->y.pWin;
132496      pWin->pFilter = substExpr(pSubst, pWin->pFilter);
132497      substExprList(pSubst, pWin->pPartition);
132498      substExprList(pSubst, pWin->pOrderBy);
132499    }
132500#endif
132501  }
132502  return pExpr;
132503}
132504static void substExprList(
132505  SubstContext *pSubst, /* Description of the substitution */
132506  ExprList *pList       /* List to scan and in which to make substitutes */
132507){
132508  int i;
132509  if( pList==0 ) return;
132510  for(i=0; i<pList->nExpr; i++){
132511    pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
132512  }
132513}
132514static void substSelect(
132515  SubstContext *pSubst, /* Description of the substitution */
132516  Select *p,            /* SELECT statement in which to make substitutions */
132517  int doPrior           /* Do substitutes on p->pPrior too */
132518){
132519  SrcList *pSrc;
132520  struct SrcList_item *pItem;
132521  int i;
132522  if( !p ) return;
132523  do{
132524    substExprList(pSubst, p->pEList);
132525    substExprList(pSubst, p->pGroupBy);
132526    substExprList(pSubst, p->pOrderBy);
132527    p->pHaving = substExpr(pSubst, p->pHaving);
132528    p->pWhere = substExpr(pSubst, p->pWhere);
132529    pSrc = p->pSrc;
132530    assert( pSrc!=0 );
132531    for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
132532      substSelect(pSubst, pItem->pSelect, 1);
132533      if( pItem->fg.isTabFunc ){
132534        substExprList(pSubst, pItem->u1.pFuncArg);
132535      }
132536    }
132537  }while( doPrior && (p = p->pPrior)!=0 );
132538}
132539#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
132540
132541#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
132542/*
132543** pSelect is a SELECT statement and pSrcItem is one item in the FROM
132544** clause of that SELECT.
132545**
132546** This routine scans the entire SELECT statement and recomputes the
132547** pSrcItem->colUsed mask.
132548*/
132549static int recomputeColumnsUsedExpr(Walker *pWalker, Expr *pExpr){
132550  struct SrcList_item *pItem;
132551  if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
132552  pItem = pWalker->u.pSrcItem;
132553  if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
132554  if( pExpr->iColumn<0 ) return WRC_Continue;
132555  pItem->colUsed |= sqlite3ExprColUsed(pExpr);
132556  return WRC_Continue;
132557}
132558static void recomputeColumnsUsed(
132559  Select *pSelect,                 /* The complete SELECT statement */
132560  struct SrcList_item *pSrcItem    /* Which FROM clause item to recompute */
132561){
132562  Walker w;
132563  if( NEVER(pSrcItem->pTab==0) ) return;
132564  memset(&w, 0, sizeof(w));
132565  w.xExprCallback = recomputeColumnsUsedExpr;
132566  w.xSelectCallback = sqlite3SelectWalkNoop;
132567  w.u.pSrcItem = pSrcItem;
132568  pSrcItem->colUsed = 0;
132569  sqlite3WalkSelect(&w, pSelect);
132570}
132571#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
132572
132573#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
132574/*
132575** This routine attempts to flatten subqueries as a performance optimization.
132576** This routine returns 1 if it makes changes and 0 if no flattening occurs.
132577**
132578** To understand the concept of flattening, consider the following
132579** query:
132580**
132581**     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
132582**
132583** The default way of implementing this query is to execute the
132584** subquery first and store the results in a temporary table, then
132585** run the outer query on that temporary table.  This requires two
132586** passes over the data.  Furthermore, because the temporary table
132587** has no indices, the WHERE clause on the outer query cannot be
132588** optimized.
132589**
132590** This routine attempts to rewrite queries such as the above into
132591** a single flat select, like this:
132592**
132593**     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
132594**
132595** The code generated for this simplification gives the same result
132596** but only has to scan the data once.  And because indices might
132597** exist on the table t1, a complete scan of the data might be
132598** avoided.
132599**
132600** Flattening is subject to the following constraints:
132601**
132602**  (**)  We no longer attempt to flatten aggregate subqueries. Was:
132603**        The subquery and the outer query cannot both be aggregates.
132604**
132605**  (**)  We no longer attempt to flatten aggregate subqueries. Was:
132606**        (2) If the subquery is an aggregate then
132607**        (2a) the outer query must not be a join and
132608**        (2b) the outer query must not use subqueries
132609**             other than the one FROM-clause subquery that is a candidate
132610**             for flattening.  (This is due to ticket [2f7170d73bf9abf80]
132611**             from 2015-02-09.)
132612**
132613**   (3)  If the subquery is the right operand of a LEFT JOIN then
132614**        (3a) the subquery may not be a join and
132615**        (3b) the FROM clause of the subquery may not contain a virtual
132616**             table and
132617**        (3c) the outer query may not be an aggregate.
132618**        (3d) the outer query may not be DISTINCT.
132619**
132620**   (4)  The subquery can not be DISTINCT.
132621**
132622**  (**)  At one point restrictions (4) and (5) defined a subset of DISTINCT
132623**        sub-queries that were excluded from this optimization. Restriction
132624**        (4) has since been expanded to exclude all DISTINCT subqueries.
132625**
132626**  (**)  We no longer attempt to flatten aggregate subqueries.  Was:
132627**        If the subquery is aggregate, the outer query may not be DISTINCT.
132628**
132629**   (7)  The subquery must have a FROM clause.  TODO:  For subqueries without
132630**        A FROM clause, consider adding a FROM clause with the special
132631**        table sqlite_once that consists of a single row containing a
132632**        single NULL.
132633**
132634**   (8)  If the subquery uses LIMIT then the outer query may not be a join.
132635**
132636**   (9)  If the subquery uses LIMIT then the outer query may not be aggregate.
132637**
132638**  (**)  Restriction (10) was removed from the code on 2005-02-05 but we
132639**        accidently carried the comment forward until 2014-09-15.  Original
132640**        constraint: "If the subquery is aggregate then the outer query
132641**        may not use LIMIT."
132642**
132643**  (11)  The subquery and the outer query may not both have ORDER BY clauses.
132644**
132645**  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
132646**        a separate restriction deriving from ticket #350.
132647**
132648**  (13)  The subquery and outer query may not both use LIMIT.
132649**
132650**  (14)  The subquery may not use OFFSET.
132651**
132652**  (15)  If the outer query is part of a compound select, then the
132653**        subquery may not use LIMIT.
132654**        (See ticket #2339 and ticket [02a8e81d44]).
132655**
132656**  (16)  If the outer query is aggregate, then the subquery may not
132657**        use ORDER BY.  (Ticket #2942)  This used to not matter
132658**        until we introduced the group_concat() function.
132659**
132660**  (17)  If the subquery is a compound select, then
132661**        (17a) all compound operators must be a UNION ALL, and
132662**        (17b) no terms within the subquery compound may be aggregate
132663**              or DISTINCT, and
132664**        (17c) every term within the subquery compound must have a FROM clause
132665**        (17d) the outer query may not be
132666**              (17d1) aggregate, or
132667**              (17d2) DISTINCT, or
132668**              (17d3) a join.
132669**        (17e) the subquery may not contain window functions
132670**
132671**        The parent and sub-query may contain WHERE clauses. Subject to
132672**        rules (11), (13) and (14), they may also contain ORDER BY,
132673**        LIMIT and OFFSET clauses.  The subquery cannot use any compound
132674**        operator other than UNION ALL because all the other compound
132675**        operators have an implied DISTINCT which is disallowed by
132676**        restriction (4).
132677**
132678**        Also, each component of the sub-query must return the same number
132679**        of result columns. This is actually a requirement for any compound
132680**        SELECT statement, but all the code here does is make sure that no
132681**        such (illegal) sub-query is flattened. The caller will detect the
132682**        syntax error and return a detailed message.
132683**
132684**  (18)  If the sub-query is a compound select, then all terms of the
132685**        ORDER BY clause of the parent must be simple references to
132686**        columns of the sub-query.
132687**
132688**  (19)  If the subquery uses LIMIT then the outer query may not
132689**        have a WHERE clause.
132690**
132691**  (20)  If the sub-query is a compound select, then it must not use
132692**        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
132693**        somewhat by saying that the terms of the ORDER BY clause must
132694**        appear as unmodified result columns in the outer query.  But we
132695**        have other optimizations in mind to deal with that case.
132696**
132697**  (21)  If the subquery uses LIMIT then the outer query may not be
132698**        DISTINCT.  (See ticket [752e1646fc]).
132699**
132700**  (22)  The subquery may not be a recursive CTE.
132701**
132702**  (**)  Subsumed into restriction (17d3).  Was: If the outer query is
132703**        a recursive CTE, then the sub-query may not be a compound query.
132704**        This restriction is because transforming the
132705**        parent to a compound query confuses the code that handles
132706**        recursive queries in multiSelect().
132707**
132708**  (**)  We no longer attempt to flatten aggregate subqueries.  Was:
132709**        The subquery may not be an aggregate that uses the built-in min() or
132710**        or max() functions.  (Without this restriction, a query like:
132711**        "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
132712**        return the value X for which Y was maximal.)
132713**
132714**  (25)  If either the subquery or the parent query contains a window
132715**        function in the select list or ORDER BY clause, flattening
132716**        is not attempted.
132717**
132718**
132719** In this routine, the "p" parameter is a pointer to the outer query.
132720** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
132721** uses aggregates.
132722**
132723** If flattening is not attempted, this routine is a no-op and returns 0.
132724** If flattening is attempted this routine returns 1.
132725**
132726** All of the expression analysis must occur on both the outer query and
132727** the subquery before this routine runs.
132728*/
132729static int flattenSubquery(
132730  Parse *pParse,       /* Parsing context */
132731  Select *p,           /* The parent or outer SELECT statement */
132732  int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
132733  int isAgg            /* True if outer SELECT uses aggregate functions */
132734){
132735  const char *zSavedAuthContext = pParse->zAuthContext;
132736  Select *pParent;    /* Current UNION ALL term of the other query */
132737  Select *pSub;       /* The inner query or "subquery" */
132738  Select *pSub1;      /* Pointer to the rightmost select in sub-query */
132739  SrcList *pSrc;      /* The FROM clause of the outer query */
132740  SrcList *pSubSrc;   /* The FROM clause of the subquery */
132741  int iParent;        /* VDBE cursor number of the pSub result set temp table */
132742  int iNewParent = -1;/* Replacement table for iParent */
132743  int isLeftJoin = 0; /* True if pSub is the right side of a LEFT JOIN */
132744  int i;              /* Loop counter */
132745  Expr *pWhere;                    /* The WHERE clause */
132746  struct SrcList_item *pSubitem;   /* The subquery */
132747  sqlite3 *db = pParse->db;
132748  Walker w;                        /* Walker to persist agginfo data */
132749
132750  /* Check to see if flattening is permitted.  Return 0 if not.
132751  */
132752  assert( p!=0 );
132753  assert( p->pPrior==0 );
132754  if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
132755  pSrc = p->pSrc;
132756  assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
132757  pSubitem = &pSrc->a[iFrom];
132758  iParent = pSubitem->iCursor;
132759  pSub = pSubitem->pSelect;
132760  assert( pSub!=0 );
132761
132762#ifndef SQLITE_OMIT_WINDOWFUNC
132763  if( p->pWin || pSub->pWin ) return 0;                  /* Restriction (25) */
132764#endif
132765
132766  pSubSrc = pSub->pSrc;
132767  assert( pSubSrc );
132768  /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
132769  ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
132770  ** because they could be computed at compile-time.  But when LIMIT and OFFSET
132771  ** became arbitrary expressions, we were forced to add restrictions (13)
132772  ** and (14). */
132773  if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
132774  if( pSub->pLimit && pSub->pLimit->pRight ) return 0;   /* Restriction (14) */
132775  if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
132776    return 0;                                            /* Restriction (15) */
132777  }
132778  if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
132779  if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (4)  */
132780  if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
132781     return 0;         /* Restrictions (8)(9) */
132782  }
132783  if( p->pOrderBy && pSub->pOrderBy ){
132784     return 0;                                           /* Restriction (11) */
132785  }
132786  if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
132787  if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
132788  if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
132789     return 0;         /* Restriction (21) */
132790  }
132791  if( pSub->selFlags & (SF_Recursive) ){
132792    return 0; /* Restrictions (22) */
132793  }
132794
132795  /*
132796  ** If the subquery is the right operand of a LEFT JOIN, then the
132797  ** subquery may not be a join itself (3a). Example of why this is not
132798  ** allowed:
132799  **
132800  **         t1 LEFT OUTER JOIN (t2 JOIN t3)
132801  **
132802  ** If we flatten the above, we would get
132803  **
132804  **         (t1 LEFT OUTER JOIN t2) JOIN t3
132805  **
132806  ** which is not at all the same thing.
132807  **
132808  ** If the subquery is the right operand of a LEFT JOIN, then the outer
132809  ** query cannot be an aggregate. (3c)  This is an artifact of the way
132810  ** aggregates are processed - there is no mechanism to determine if
132811  ** the LEFT JOIN table should be all-NULL.
132812  **
132813  ** See also tickets #306, #350, and #3300.
132814  */
132815  if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){
132816    isLeftJoin = 1;
132817    if( pSubSrc->nSrc>1                   /* (3a) */
132818     || isAgg                             /* (3b) */
132819     || IsVirtual(pSubSrc->a[0].pTab)     /* (3c) */
132820     || (p->selFlags & SF_Distinct)!=0    /* (3d) */
132821    ){
132822      return 0;
132823    }
132824  }
132825#ifdef SQLITE_EXTRA_IFNULLROW
132826  else if( iFrom>0 && !isAgg ){
132827    /* Setting isLeftJoin to -1 causes OP_IfNullRow opcodes to be generated for
132828    ** every reference to any result column from subquery in a join, even
132829    ** though they are not necessary.  This will stress-test the OP_IfNullRow
132830    ** opcode. */
132831    isLeftJoin = -1;
132832  }
132833#endif
132834
132835  /* Restriction (17): If the sub-query is a compound SELECT, then it must
132836  ** use only the UNION ALL operator. And none of the simple select queries
132837  ** that make up the compound SELECT are allowed to be aggregate or distinct
132838  ** queries.
132839  */
132840  if( pSub->pPrior ){
132841    if( pSub->pOrderBy ){
132842      return 0;  /* Restriction (20) */
132843    }
132844    if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
132845      return 0; /* (17d1), (17d2), or (17d3) */
132846    }
132847    for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
132848      testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
132849      testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
132850      assert( pSub->pSrc!=0 );
132851      assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
132852      if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0    /* (17b) */
132853       || (pSub1->pPrior && pSub1->op!=TK_ALL)                 /* (17a) */
132854       || pSub1->pSrc->nSrc<1                                  /* (17c) */
132855#ifndef SQLITE_OMIT_WINDOWFUNC
132856       || pSub1->pWin                                          /* (17e) */
132857#endif
132858      ){
132859        return 0;
132860      }
132861      testcase( pSub1->pSrc->nSrc>1 );
132862    }
132863
132864    /* Restriction (18). */
132865    if( p->pOrderBy ){
132866      int ii;
132867      for(ii=0; ii<p->pOrderBy->nExpr; ii++){
132868        if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
132869      }
132870    }
132871  }
132872
132873  /* Ex-restriction (23):
132874  ** The only way that the recursive part of a CTE can contain a compound
132875  ** subquery is for the subquery to be one term of a join.  But if the
132876  ** subquery is a join, then the flattening has already been stopped by
132877  ** restriction (17d3)
132878  */
132879  assert( (p->selFlags & SF_Recursive)==0 || pSub->pPrior==0 );
132880
132881  /***** If we reach this point, flattening is permitted. *****/
132882  SELECTTRACE(1,pParse,p,("flatten %u.%p from term %d\n",
132883                   pSub->selId, pSub, iFrom));
132884
132885  /* Authorize the subquery */
132886  pParse->zAuthContext = pSubitem->zName;
132887  TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
132888  testcase( i==SQLITE_DENY );
132889  pParse->zAuthContext = zSavedAuthContext;
132890
132891  /* If the sub-query is a compound SELECT statement, then (by restrictions
132892  ** 17 and 18 above) it must be a UNION ALL and the parent query must
132893  ** be of the form:
132894  **
132895  **     SELECT <expr-list> FROM (<sub-query>) <where-clause>
132896  **
132897  ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
132898  ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
132899  ** OFFSET clauses and joins them to the left-hand-side of the original
132900  ** using UNION ALL operators. In this case N is the number of simple
132901  ** select statements in the compound sub-query.
132902  **
132903  ** Example:
132904  **
132905  **     SELECT a+1 FROM (
132906  **        SELECT x FROM tab
132907  **        UNION ALL
132908  **        SELECT y FROM tab
132909  **        UNION ALL
132910  **        SELECT abs(z*2) FROM tab2
132911  **     ) WHERE a!=5 ORDER BY 1
132912  **
132913  ** Transformed into:
132914  **
132915  **     SELECT x+1 FROM tab WHERE x+1!=5
132916  **     UNION ALL
132917  **     SELECT y+1 FROM tab WHERE y+1!=5
132918  **     UNION ALL
132919  **     SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
132920  **     ORDER BY 1
132921  **
132922  ** We call this the "compound-subquery flattening".
132923  */
132924  for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
132925    Select *pNew;
132926    ExprList *pOrderBy = p->pOrderBy;
132927    Expr *pLimit = p->pLimit;
132928    Select *pPrior = p->pPrior;
132929    p->pOrderBy = 0;
132930    p->pSrc = 0;
132931    p->pPrior = 0;
132932    p->pLimit = 0;
132933    pNew = sqlite3SelectDup(db, p, 0);
132934    p->pLimit = pLimit;
132935    p->pOrderBy = pOrderBy;
132936    p->pSrc = pSrc;
132937    p->op = TK_ALL;
132938    if( pNew==0 ){
132939      p->pPrior = pPrior;
132940    }else{
132941      pNew->pPrior = pPrior;
132942      if( pPrior ) pPrior->pNext = pNew;
132943      pNew->pNext = p;
132944      p->pPrior = pNew;
132945      SELECTTRACE(2,pParse,p,("compound-subquery flattener"
132946                              " creates %u as peer\n",pNew->selId));
132947    }
132948    if( db->mallocFailed ) return 1;
132949  }
132950
132951  /* Begin flattening the iFrom-th entry of the FROM clause
132952  ** in the outer query.
132953  */
132954  pSub = pSub1 = pSubitem->pSelect;
132955
132956  /* Delete the transient table structure associated with the
132957  ** subquery
132958  */
132959  sqlite3DbFree(db, pSubitem->zDatabase);
132960  sqlite3DbFree(db, pSubitem->zName);
132961  sqlite3DbFree(db, pSubitem->zAlias);
132962  pSubitem->zDatabase = 0;
132963  pSubitem->zName = 0;
132964  pSubitem->zAlias = 0;
132965  pSubitem->pSelect = 0;
132966
132967  /* Defer deleting the Table object associated with the
132968  ** subquery until code generation is
132969  ** complete, since there may still exist Expr.pTab entries that
132970  ** refer to the subquery even after flattening.  Ticket #3346.
132971  **
132972  ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
132973  */
132974  if( ALWAYS(pSubitem->pTab!=0) ){
132975    Table *pTabToDel = pSubitem->pTab;
132976    if( pTabToDel->nTabRef==1 ){
132977      Parse *pToplevel = sqlite3ParseToplevel(pParse);
132978      pTabToDel->pNextZombie = pToplevel->pZombieTab;
132979      pToplevel->pZombieTab = pTabToDel;
132980    }else{
132981      pTabToDel->nTabRef--;
132982    }
132983    pSubitem->pTab = 0;
132984  }
132985
132986  /* The following loop runs once for each term in a compound-subquery
132987  ** flattening (as described above).  If we are doing a different kind
132988  ** of flattening - a flattening other than a compound-subquery flattening -
132989  ** then this loop only runs once.
132990  **
132991  ** This loop moves all of the FROM elements of the subquery into the
132992  ** the FROM clause of the outer query.  Before doing this, remember
132993  ** the cursor number for the original outer query FROM element in
132994  ** iParent.  The iParent cursor will never be used.  Subsequent code
132995  ** will scan expressions looking for iParent references and replace
132996  ** those references with expressions that resolve to the subquery FROM
132997  ** elements we are now copying in.
132998  */
132999  for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
133000    int nSubSrc;
133001    u8 jointype = 0;
133002    assert( pSub!=0 );
133003    pSubSrc = pSub->pSrc;     /* FROM clause of subquery */
133004    nSubSrc = pSubSrc->nSrc;  /* Number of terms in subquery FROM clause */
133005    pSrc = pParent->pSrc;     /* FROM clause of the outer query */
133006
133007    if( pSrc ){
133008      assert( pParent==p );  /* First time through the loop */
133009      jointype = pSubitem->fg.jointype;
133010    }else{
133011      assert( pParent!=p );  /* 2nd and subsequent times through the loop */
133012      pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
133013      if( pSrc==0 ) break;
133014      pParent->pSrc = pSrc;
133015    }
133016
133017    /* The subquery uses a single slot of the FROM clause of the outer
133018    ** query.  If the subquery has more than one element in its FROM clause,
133019    ** then expand the outer query to make space for it to hold all elements
133020    ** of the subquery.
133021    **
133022    ** Example:
133023    **
133024    **    SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
133025    **
133026    ** The outer query has 3 slots in its FROM clause.  One slot of the
133027    ** outer query (the middle slot) is used by the subquery.  The next
133028    ** block of code will expand the outer query FROM clause to 4 slots.
133029    ** The middle slot is expanded to two slots in order to make space
133030    ** for the two elements in the FROM clause of the subquery.
133031    */
133032    if( nSubSrc>1 ){
133033      pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
133034      if( pSrc==0 ) break;
133035      pParent->pSrc = pSrc;
133036    }
133037
133038    /* Transfer the FROM clause terms from the subquery into the
133039    ** outer query.
133040    */
133041    for(i=0; i<nSubSrc; i++){
133042      sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
133043      assert( pSrc->a[i+iFrom].fg.isTabFunc==0 );
133044      pSrc->a[i+iFrom] = pSubSrc->a[i];
133045      iNewParent = pSubSrc->a[i].iCursor;
133046      memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
133047    }
133048    pSrc->a[iFrom].fg.jointype = jointype;
133049
133050    /* Now begin substituting subquery result set expressions for
133051    ** references to the iParent in the outer query.
133052    **
133053    ** Example:
133054    **
133055    **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
133056    **   \                     \_____________ subquery __________/          /
133057    **    \_____________________ outer query ______________________________/
133058    **
133059    ** We look at every expression in the outer query and every place we see
133060    ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
133061    */
133062    if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
133063      /* At this point, any non-zero iOrderByCol values indicate that the
133064      ** ORDER BY column expression is identical to the iOrderByCol'th
133065      ** expression returned by SELECT statement pSub. Since these values
133066      ** do not necessarily correspond to columns in SELECT statement pParent,
133067      ** zero them before transfering the ORDER BY clause.
133068      **
133069      ** Not doing this may cause an error if a subsequent call to this
133070      ** function attempts to flatten a compound sub-query into pParent
133071      ** (the only way this can happen is if the compound sub-query is
133072      ** currently part of pSub->pSrc). See ticket [d11a6e908f].  */
133073      ExprList *pOrderBy = pSub->pOrderBy;
133074      for(i=0; i<pOrderBy->nExpr; i++){
133075        pOrderBy->a[i].u.x.iOrderByCol = 0;
133076      }
133077      assert( pParent->pOrderBy==0 );
133078      pParent->pOrderBy = pOrderBy;
133079      pSub->pOrderBy = 0;
133080    }
133081    pWhere = pSub->pWhere;
133082    pSub->pWhere = 0;
133083    if( isLeftJoin>0 ){
133084      sqlite3SetJoinExpr(pWhere, iNewParent);
133085    }
133086    if( pWhere ){
133087      if( pParent->pWhere ){
133088        pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pWhere, pParent->pWhere);
133089      }else{
133090        pParent->pWhere = pWhere;
133091      }
133092    }
133093    if( db->mallocFailed==0 ){
133094      SubstContext x;
133095      x.pParse = pParse;
133096      x.iTable = iParent;
133097      x.iNewTable = iNewParent;
133098      x.isLeftJoin = isLeftJoin;
133099      x.pEList = pSub->pEList;
133100      substSelect(&x, pParent, 0);
133101    }
133102
133103    /* The flattened query is a compound if either the inner or the
133104    ** outer query is a compound. */
133105    pParent->selFlags |= pSub->selFlags & SF_Compound;
133106    assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */
133107
133108    /*
133109    ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
133110    **
133111    ** One is tempted to try to add a and b to combine the limits.  But this
133112    ** does not work if either limit is negative.
133113    */
133114    if( pSub->pLimit ){
133115      pParent->pLimit = pSub->pLimit;
133116      pSub->pLimit = 0;
133117    }
133118
133119    /* Recompute the SrcList_item.colUsed masks for the flattened
133120    ** tables. */
133121    for(i=0; i<nSubSrc; i++){
133122      recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
133123    }
133124  }
133125
133126  /* Finially, delete what is left of the subquery and return
133127  ** success.
133128  */
133129  sqlite3AggInfoPersistWalkerInit(&w, pParse);
133130  sqlite3WalkSelect(&w,pSub1);
133131  sqlite3SelectDelete(db, pSub1);
133132
133133#if SELECTTRACE_ENABLED
133134  if( sqlite3SelectTrace & 0x100 ){
133135    SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
133136    sqlite3TreeViewSelect(0, p, 0);
133137  }
133138#endif
133139
133140  return 1;
133141}
133142#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
133143
133144/*
133145** A structure to keep track of all of the column values that are fixed to
133146** a known value due to WHERE clause constraints of the form COLUMN=VALUE.
133147*/
133148typedef struct WhereConst WhereConst;
133149struct WhereConst {
133150  Parse *pParse;   /* Parsing context */
133151  int nConst;      /* Number for COLUMN=CONSTANT terms */
133152  int nChng;       /* Number of times a constant is propagated */
133153  Expr **apExpr;   /* [i*2] is COLUMN and [i*2+1] is VALUE */
133154};
133155
133156/*
133157** Add a new entry to the pConst object.  Except, do not add duplicate
133158** pColumn entires.  Also, do not add if doing so would not be appropriate.
133159**
133160** The caller guarantees the pColumn is a column and pValue is a constant.
133161** This routine has to do some additional checks before completing the
133162** insert.
133163*/
133164static void constInsert(
133165  WhereConst *pConst,  /* The WhereConst into which we are inserting */
133166  Expr *pColumn,       /* The COLUMN part of the constraint */
133167  Expr *pValue,        /* The VALUE part of the constraint */
133168  Expr *pExpr          /* Overall expression: COLUMN=VALUE or VALUE=COLUMN */
133169){
133170  int i;
133171  assert( pColumn->op==TK_COLUMN );
133172  assert( sqlite3ExprIsConstant(pValue) );
133173
133174  if( ExprHasProperty(pColumn, EP_FixedCol) ) return;
133175  if( sqlite3ExprAffinity(pValue)!=0 ) return;
133176  if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){
133177    return;
133178  }
133179
133180  /* 2018-10-25 ticket [cf5ed20f]
133181  ** Make sure the same pColumn is not inserted more than once */
133182  for(i=0; i<pConst->nConst; i++){
133183    const Expr *pE2 = pConst->apExpr[i*2];
133184    assert( pE2->op==TK_COLUMN );
133185    if( pE2->iTable==pColumn->iTable
133186     && pE2->iColumn==pColumn->iColumn
133187    ){
133188      return;  /* Already present.  Return without doing anything. */
133189    }
133190  }
133191
133192  pConst->nConst++;
133193  pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
133194                         pConst->nConst*2*sizeof(Expr*));
133195  if( pConst->apExpr==0 ){
133196    pConst->nConst = 0;
133197  }else{
133198    pConst->apExpr[pConst->nConst*2-2] = pColumn;
133199    pConst->apExpr[pConst->nConst*2-1] = pValue;
133200  }
133201}
133202
133203/*
133204** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE
133205** is a constant expression and where the term must be true because it
133206** is part of the AND-connected terms of the expression.  For each term
133207** found, add it to the pConst structure.
133208*/
133209static void findConstInWhere(WhereConst *pConst, Expr *pExpr){
133210  Expr *pRight, *pLeft;
133211  if( pExpr==0 ) return;
133212  if( ExprHasProperty(pExpr, EP_FromJoin) ) return;
133213  if( pExpr->op==TK_AND ){
133214    findConstInWhere(pConst, pExpr->pRight);
133215    findConstInWhere(pConst, pExpr->pLeft);
133216    return;
133217  }
133218  if( pExpr->op!=TK_EQ ) return;
133219  pRight = pExpr->pRight;
133220  pLeft = pExpr->pLeft;
133221  assert( pRight!=0 );
133222  assert( pLeft!=0 );
133223  if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pLeft) ){
133224    constInsert(pConst,pRight,pLeft,pExpr);
133225  }
133226  if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pRight) ){
133227    constInsert(pConst,pLeft,pRight,pExpr);
133228  }
133229}
133230
133231/*
133232** This is a Walker expression callback.  pExpr is a candidate expression
133233** to be replaced by a value.  If pExpr is equivalent to one of the
133234** columns named in pWalker->u.pConst, then overwrite it with its
133235** corresponding value.
133236*/
133237static int propagateConstantExprRewrite(Walker *pWalker, Expr *pExpr){
133238  int i;
133239  WhereConst *pConst;
133240  if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
133241  if( ExprHasProperty(pExpr, EP_FixedCol|EP_FromJoin) ){
133242    testcase( ExprHasProperty(pExpr, EP_FixedCol) );
133243    testcase( ExprHasProperty(pExpr, EP_FromJoin) );
133244    return WRC_Continue;
133245  }
133246  pConst = pWalker->u.pConst;
133247  for(i=0; i<pConst->nConst; i++){
133248    Expr *pColumn = pConst->apExpr[i*2];
133249    if( pColumn==pExpr ) continue;
133250    if( pColumn->iTable!=pExpr->iTable ) continue;
133251    if( pColumn->iColumn!=pExpr->iColumn ) continue;
133252    /* A match is found.  Add the EP_FixedCol property */
133253    pConst->nChng++;
133254    ExprClearProperty(pExpr, EP_Leaf);
133255    ExprSetProperty(pExpr, EP_FixedCol);
133256    assert( pExpr->pLeft==0 );
133257    pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
133258    break;
133259  }
133260  return WRC_Prune;
133261}
133262
133263/*
133264** The WHERE-clause constant propagation optimization.
133265**
133266** If the WHERE clause contains terms of the form COLUMN=CONSTANT or
133267** CONSTANT=COLUMN that are top-level AND-connected terms that are not
133268** part of a ON clause from a LEFT JOIN, then throughout the query
133269** replace all other occurrences of COLUMN with CONSTANT.
133270**
133271** For example, the query:
133272**
133273**      SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b
133274**
133275** Is transformed into
133276**
133277**      SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39
133278**
133279** Return true if any transformations where made and false if not.
133280**
133281** Implementation note:  Constant propagation is tricky due to affinity
133282** and collating sequence interactions.  Consider this example:
133283**
133284**    CREATE TABLE t1(a INT,b TEXT);
133285**    INSERT INTO t1 VALUES(123,'0123');
133286**    SELECT * FROM t1 WHERE a=123 AND b=a;
133287**    SELECT * FROM t1 WHERE a=123 AND b=123;
133288**
133289** The two SELECT statements above should return different answers.  b=a
133290** is alway true because the comparison uses numeric affinity, but b=123
133291** is false because it uses text affinity and '0123' is not the same as '123'.
133292** To work around this, the expression tree is not actually changed from
133293** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol
133294** and the "123" value is hung off of the pLeft pointer.  Code generator
133295** routines know to generate the constant "123" instead of looking up the
133296** column value.  Also, to avoid collation problems, this optimization is
133297** only attempted if the "a=123" term uses the default BINARY collation.
133298*/
133299static int propagateConstants(
133300  Parse *pParse,   /* The parsing context */
133301  Select *p        /* The query in which to propagate constants */
133302){
133303  WhereConst x;
133304  Walker w;
133305  int nChng = 0;
133306  x.pParse = pParse;
133307  do{
133308    x.nConst = 0;
133309    x.nChng = 0;
133310    x.apExpr = 0;
133311    findConstInWhere(&x, p->pWhere);
133312    if( x.nConst ){
133313      memset(&w, 0, sizeof(w));
133314      w.pParse = pParse;
133315      w.xExprCallback = propagateConstantExprRewrite;
133316      w.xSelectCallback = sqlite3SelectWalkNoop;
133317      w.xSelectCallback2 = 0;
133318      w.walkerDepth = 0;
133319      w.u.pConst = &x;
133320      sqlite3WalkExpr(&w, p->pWhere);
133321      sqlite3DbFree(x.pParse->db, x.apExpr);
133322      nChng += x.nChng;
133323    }
133324  }while( x.nChng );
133325  return nChng;
133326}
133327
133328#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
133329/*
133330** Make copies of relevant WHERE clause terms of the outer query into
133331** the WHERE clause of subquery.  Example:
133332**
133333**    SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
133334**
133335** Transformed into:
133336**
133337**    SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
133338**     WHERE x=5 AND y=10;
133339**
133340** The hope is that the terms added to the inner query will make it more
133341** efficient.
133342**
133343** Do not attempt this optimization if:
133344**
133345**   (1) (** This restriction was removed on 2017-09-29.  We used to
133346**           disallow this optimization for aggregate subqueries, but now
133347**           it is allowed by putting the extra terms on the HAVING clause.
133348**           The added HAVING clause is pointless if the subquery lacks
133349**           a GROUP BY clause.  But such a HAVING clause is also harmless
133350**           so there does not appear to be any reason to add extra logic
133351**           to suppress it. **)
133352**
133353**   (2) The inner query is the recursive part of a common table expression.
133354**
133355**   (3) The inner query has a LIMIT clause (since the changes to the WHERE
133356**       clause would change the meaning of the LIMIT).
133357**
133358**   (4) The inner query is the right operand of a LEFT JOIN and the
133359**       expression to be pushed down does not come from the ON clause
133360**       on that LEFT JOIN.
133361**
133362**   (5) The WHERE clause expression originates in the ON or USING clause
133363**       of a LEFT JOIN where iCursor is not the right-hand table of that
133364**       left join.  An example:
133365**
133366**           SELECT *
133367**           FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
133368**           JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
133369**           LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
133370**
133371**       The correct answer is three rows:  (1,1,NULL),(2,2,8),(2,2,9).
133372**       But if the (b2=2) term were to be pushed down into the bb subquery,
133373**       then the (1,1,NULL) row would be suppressed.
133374**
133375**   (6) The inner query features one or more window-functions (since
133376**       changes to the WHERE clause of the inner query could change the
133377**       window over which window functions are calculated).
133378**
133379** Return 0 if no changes are made and non-zero if one or more WHERE clause
133380** terms are duplicated into the subquery.
133381*/
133382static int pushDownWhereTerms(
133383  Parse *pParse,        /* Parse context (for malloc() and error reporting) */
133384  Select *pSubq,        /* The subquery whose WHERE clause is to be augmented */
133385  Expr *pWhere,         /* The WHERE clause of the outer query */
133386  int iCursor,          /* Cursor number of the subquery */
133387  int isLeftJoin        /* True if pSubq is the right term of a LEFT JOIN */
133388){
133389  Expr *pNew;
133390  int nChng = 0;
133391  Select *pSel;
133392  if( pWhere==0 ) return 0;
133393  if( pSubq->selFlags & SF_Recursive ) return 0;  /* restriction (2) */
133394
133395#ifndef SQLITE_OMIT_WINDOWFUNC
133396  for(pSel=pSubq; pSel; pSel=pSel->pPrior){
133397    if( pSel->pWin ) return 0;    /* restriction (6) */
133398  }
133399#endif
133400
133401#ifdef SQLITE_DEBUG
133402  /* Only the first term of a compound can have a WITH clause.  But make
133403  ** sure no other terms are marked SF_Recursive in case something changes
133404  ** in the future.
133405  */
133406  {
133407    Select *pX;
133408    for(pX=pSubq; pX; pX=pX->pPrior){
133409      assert( (pX->selFlags & (SF_Recursive))==0 );
133410    }
133411  }
133412#endif
133413
133414  if( pSubq->pLimit!=0 ){
133415    return 0; /* restriction (3) */
133416  }
133417  while( pWhere->op==TK_AND ){
133418    nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight,
133419                                iCursor, isLeftJoin);
133420    pWhere = pWhere->pLeft;
133421  }
133422  if( isLeftJoin
133423   && (ExprHasProperty(pWhere,EP_FromJoin)==0
133424         || pWhere->iRightJoinTable!=iCursor)
133425  ){
133426    return 0; /* restriction (4) */
133427  }
133428  if( ExprHasProperty(pWhere,EP_FromJoin) && pWhere->iRightJoinTable!=iCursor ){
133429    return 0; /* restriction (5) */
133430  }
133431  if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
133432    nChng++;
133433    while( pSubq ){
133434      SubstContext x;
133435      pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
133436      unsetJoinExpr(pNew, -1);
133437      x.pParse = pParse;
133438      x.iTable = iCursor;
133439      x.iNewTable = iCursor;
133440      x.isLeftJoin = 0;
133441      x.pEList = pSubq->pEList;
133442      pNew = substExpr(&x, pNew);
133443      if( pSubq->selFlags & SF_Aggregate ){
133444        pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
133445      }else{
133446        pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
133447      }
133448      pSubq = pSubq->pPrior;
133449    }
133450  }
133451  return nChng;
133452}
133453#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
133454
133455/*
133456** The pFunc is the only aggregate function in the query.  Check to see
133457** if the query is a candidate for the min/max optimization.
133458**
133459** If the query is a candidate for the min/max optimization, then set
133460** *ppMinMax to be an ORDER BY clause to be used for the optimization
133461** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
133462** whether pFunc is a min() or max() function.
133463**
133464** If the query is not a candidate for the min/max optimization, return
133465** WHERE_ORDERBY_NORMAL (which must be zero).
133466**
133467** This routine must be called after aggregate functions have been
133468** located but before their arguments have been subjected to aggregate
133469** analysis.
133470*/
133471static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
133472  int eRet = WHERE_ORDERBY_NORMAL;      /* Return value */
133473  ExprList *pEList = pFunc->x.pList;    /* Arguments to agg function */
133474  const char *zFunc;                    /* Name of aggregate function pFunc */
133475  ExprList *pOrderBy;
133476  u8 sortFlags = 0;
133477
133478  assert( *ppMinMax==0 );
133479  assert( pFunc->op==TK_AGG_FUNCTION );
133480  assert( !IsWindowFunc(pFunc) );
133481  if( pEList==0 || pEList->nExpr!=1 || ExprHasProperty(pFunc, EP_WinFunc) ){
133482    return eRet;
133483  }
133484  zFunc = pFunc->u.zToken;
133485  if( sqlite3StrICmp(zFunc, "min")==0 ){
133486    eRet = WHERE_ORDERBY_MIN;
133487    if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){
133488      sortFlags = KEYINFO_ORDER_BIGNULL;
133489    }
133490  }else if( sqlite3StrICmp(zFunc, "max")==0 ){
133491    eRet = WHERE_ORDERBY_MAX;
133492    sortFlags = KEYINFO_ORDER_DESC;
133493  }else{
133494    return eRet;
133495  }
133496  *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0);
133497  assert( pOrderBy!=0 || db->mallocFailed );
133498  if( pOrderBy ) pOrderBy->a[0].sortFlags = sortFlags;
133499  return eRet;
133500}
133501
133502/*
133503** The select statement passed as the first argument is an aggregate query.
133504** The second argument is the associated aggregate-info object. This
133505** function tests if the SELECT is of the form:
133506**
133507**   SELECT count(*) FROM <tbl>
133508**
133509** where table is a database table, not a sub-select or view. If the query
133510** does match this pattern, then a pointer to the Table object representing
133511** <tbl> is returned. Otherwise, 0 is returned.
133512*/
133513static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
133514  Table *pTab;
133515  Expr *pExpr;
133516
133517  assert( !p->pGroupBy );
133518
133519  if( p->pWhere || p->pEList->nExpr!=1
133520   || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
133521  ){
133522    return 0;
133523  }
133524  pTab = p->pSrc->a[0].pTab;
133525  pExpr = p->pEList->a[0].pExpr;
133526  assert( pTab && !pTab->pSelect && pExpr );
133527
133528  if( IsVirtual(pTab) ) return 0;
133529  if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
133530  if( NEVER(pAggInfo->nFunc==0) ) return 0;
133531  if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
133532  if( ExprHasProperty(pExpr, EP_Distinct|EP_WinFunc) ) return 0;
133533
133534  return pTab;
133535}
133536
133537/*
133538** If the source-list item passed as an argument was augmented with an
133539** INDEXED BY clause, then try to locate the specified index. If there
133540** was such a clause and the named index cannot be found, return
133541** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
133542** pFrom->pIndex and return SQLITE_OK.
133543*/
133544SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
133545  if( pFrom->pTab && pFrom->fg.isIndexedBy ){
133546    Table *pTab = pFrom->pTab;
133547    char *zIndexedBy = pFrom->u1.zIndexedBy;
133548    Index *pIdx;
133549    for(pIdx=pTab->pIndex;
133550        pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
133551        pIdx=pIdx->pNext
133552    );
133553    if( !pIdx ){
133554      sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
133555      pParse->checkSchema = 1;
133556      return SQLITE_ERROR;
133557    }
133558    pFrom->pIBIndex = pIdx;
133559  }
133560  return SQLITE_OK;
133561}
133562/*
133563** Detect compound SELECT statements that use an ORDER BY clause with
133564** an alternative collating sequence.
133565**
133566**    SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
133567**
133568** These are rewritten as a subquery:
133569**
133570**    SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
133571**     ORDER BY ... COLLATE ...
133572**
133573** This transformation is necessary because the multiSelectOrderBy() routine
133574** above that generates the code for a compound SELECT with an ORDER BY clause
133575** uses a merge algorithm that requires the same collating sequence on the
133576** result columns as on the ORDER BY clause.  See ticket
133577** http://www.sqlite.org/src/info/6709574d2a
133578**
133579** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
133580** The UNION ALL operator works fine with multiSelectOrderBy() even when
133581** there are COLLATE terms in the ORDER BY.
133582*/
133583static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
133584  int i;
133585  Select *pNew;
133586  Select *pX;
133587  sqlite3 *db;
133588  struct ExprList_item *a;
133589  SrcList *pNewSrc;
133590  Parse *pParse;
133591  Token dummy;
133592
133593  if( p->pPrior==0 ) return WRC_Continue;
133594  if( p->pOrderBy==0 ) return WRC_Continue;
133595  for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
133596  if( pX==0 ) return WRC_Continue;
133597  a = p->pOrderBy->a;
133598#ifndef SQLITE_OMIT_WINDOWFUNC
133599  /* If iOrderByCol is already non-zero, then it has already been matched
133600  ** to a result column of the SELECT statement. This occurs when the
133601  ** SELECT is rewritten for window-functions processing and then passed
133602  ** to sqlite3SelectPrep() and similar a second time. The rewriting done
133603  ** by this function is not required in this case. */
133604  if( a[0].u.x.iOrderByCol ) return WRC_Continue;
133605#endif
133606  for(i=p->pOrderBy->nExpr-1; i>=0; i--){
133607    if( a[i].pExpr->flags & EP_Collate ) break;
133608  }
133609  if( i<0 ) return WRC_Continue;
133610
133611  /* If we reach this point, that means the transformation is required. */
133612
133613  pParse = pWalker->pParse;
133614  db = pParse->db;
133615  pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
133616  if( pNew==0 ) return WRC_Abort;
133617  memset(&dummy, 0, sizeof(dummy));
133618  pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0);
133619  if( pNewSrc==0 ) return WRC_Abort;
133620  *pNew = *p;
133621  p->pSrc = pNewSrc;
133622  p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
133623  p->op = TK_SELECT;
133624  p->pWhere = 0;
133625  pNew->pGroupBy = 0;
133626  pNew->pHaving = 0;
133627  pNew->pOrderBy = 0;
133628  p->pPrior = 0;
133629  p->pNext = 0;
133630  p->pWith = 0;
133631#ifndef SQLITE_OMIT_WINDOWFUNC
133632  p->pWinDefn = 0;
133633#endif
133634  p->selFlags &= ~SF_Compound;
133635  assert( (p->selFlags & SF_Converted)==0 );
133636  p->selFlags |= SF_Converted;
133637  assert( pNew->pPrior!=0 );
133638  pNew->pPrior->pNext = pNew;
133639  pNew->pLimit = 0;
133640  return WRC_Continue;
133641}
133642
133643/*
133644** Check to see if the FROM clause term pFrom has table-valued function
133645** arguments.  If it does, leave an error message in pParse and return
133646** non-zero, since pFrom is not allowed to be a table-valued function.
133647*/
133648static int cannotBeFunction(Parse *pParse, struct SrcList_item *pFrom){
133649  if( pFrom->fg.isTabFunc ){
133650    sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
133651    return 1;
133652  }
133653  return 0;
133654}
133655
133656#ifndef SQLITE_OMIT_CTE
133657/*
133658** Argument pWith (which may be NULL) points to a linked list of nested
133659** WITH contexts, from inner to outermost. If the table identified by
133660** FROM clause element pItem is really a common-table-expression (CTE)
133661** then return a pointer to the CTE definition for that table. Otherwise
133662** return NULL.
133663**
133664** If a non-NULL value is returned, set *ppContext to point to the With
133665** object that the returned CTE belongs to.
133666*/
133667static struct Cte *searchWith(
133668  With *pWith,                    /* Current innermost WITH clause */
133669  struct SrcList_item *pItem,     /* FROM clause element to resolve */
133670  With **ppContext                /* OUT: WITH clause return value belongs to */
133671){
133672  const char *zName;
133673  if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
133674    With *p;
133675    for(p=pWith; p; p=p->pOuter){
133676      int i;
133677      for(i=0; i<p->nCte; i++){
133678        if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
133679          *ppContext = p;
133680          return &p->a[i];
133681        }
133682      }
133683    }
133684  }
133685  return 0;
133686}
133687
133688/* The code generator maintains a stack of active WITH clauses
133689** with the inner-most WITH clause being at the top of the stack.
133690**
133691** This routine pushes the WITH clause passed as the second argument
133692** onto the top of the stack. If argument bFree is true, then this
133693** WITH clause will never be popped from the stack. In this case it
133694** should be freed along with the Parse object. In other cases, when
133695** bFree==0, the With object will be freed along with the SELECT
133696** statement with which it is associated.
133697*/
133698SQLITE_PRIVATE void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
133699  assert( bFree==0 || (pParse->pWith==0 && pParse->pWithToFree==0) );
133700  if( pWith ){
133701    assert( pParse->pWith!=pWith );
133702    pWith->pOuter = pParse->pWith;
133703    pParse->pWith = pWith;
133704    if( bFree ) pParse->pWithToFree = pWith;
133705  }
133706}
133707
133708/*
133709** This function checks if argument pFrom refers to a CTE declared by
133710** a WITH clause on the stack currently maintained by the parser. And,
133711** if currently processing a CTE expression, if it is a recursive
133712** reference to the current CTE.
133713**
133714** If pFrom falls into either of the two categories above, pFrom->pTab
133715** and other fields are populated accordingly. The caller should check
133716** (pFrom->pTab!=0) to determine whether or not a successful match
133717** was found.
133718**
133719** Whether or not a match is found, SQLITE_OK is returned if no error
133720** occurs. If an error does occur, an error message is stored in the
133721** parser and some error code other than SQLITE_OK returned.
133722*/
133723static int withExpand(
133724  Walker *pWalker,
133725  struct SrcList_item *pFrom
133726){
133727  Parse *pParse = pWalker->pParse;
133728  sqlite3 *db = pParse->db;
133729  struct Cte *pCte;               /* Matched CTE (or NULL if no match) */
133730  With *pWith;                    /* WITH clause that pCte belongs to */
133731
133732  assert( pFrom->pTab==0 );
133733  if( pParse->nErr ){
133734    return SQLITE_ERROR;
133735  }
133736
133737  pCte = searchWith(pParse->pWith, pFrom, &pWith);
133738  if( pCte ){
133739    Table *pTab;
133740    ExprList *pEList;
133741    Select *pSel;
133742    Select *pLeft;                /* Left-most SELECT statement */
133743    int bMayRecursive;            /* True if compound joined by UNION [ALL] */
133744    With *pSavedWith;             /* Initial value of pParse->pWith */
133745
133746    /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
133747    ** recursive reference to CTE pCte. Leave an error in pParse and return
133748    ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
133749    ** In this case, proceed.  */
133750    if( pCte->zCteErr ){
133751      sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
133752      return SQLITE_ERROR;
133753    }
133754    if( cannotBeFunction(pParse, pFrom) ) return SQLITE_ERROR;
133755
133756    assert( pFrom->pTab==0 );
133757    pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
133758    if( pTab==0 ) return WRC_Abort;
133759    pTab->nTabRef = 1;
133760    pTab->zName = sqlite3DbStrDup(db, pCte->zName);
133761    pTab->iPKey = -1;
133762    pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
133763    pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
133764    pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
133765    if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
133766    assert( pFrom->pSelect );
133767
133768    /* Check if this is a recursive CTE. */
133769    pSel = pFrom->pSelect;
133770    bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
133771    if( bMayRecursive ){
133772      int i;
133773      SrcList *pSrc = pFrom->pSelect->pSrc;
133774      for(i=0; i<pSrc->nSrc; i++){
133775        struct SrcList_item *pItem = &pSrc->a[i];
133776        if( pItem->zDatabase==0
133777         && pItem->zName!=0
133778         && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
133779          ){
133780          pItem->pTab = pTab;
133781          pItem->fg.isRecursive = 1;
133782          pTab->nTabRef++;
133783          pSel->selFlags |= SF_Recursive;
133784        }
133785      }
133786    }
133787
133788    /* Only one recursive reference is permitted. */
133789    if( pTab->nTabRef>2 ){
133790      sqlite3ErrorMsg(
133791          pParse, "multiple references to recursive table: %s", pCte->zName
133792      );
133793      return SQLITE_ERROR;
133794    }
133795    assert( pTab->nTabRef==1 ||
133796            ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
133797
133798    pCte->zCteErr = "circular reference: %s";
133799    pSavedWith = pParse->pWith;
133800    pParse->pWith = pWith;
133801    if( bMayRecursive ){
133802      Select *pPrior = pSel->pPrior;
133803      assert( pPrior->pWith==0 );
133804      pPrior->pWith = pSel->pWith;
133805      sqlite3WalkSelect(pWalker, pPrior);
133806      pPrior->pWith = 0;
133807    }else{
133808      sqlite3WalkSelect(pWalker, pSel);
133809    }
133810    pParse->pWith = pWith;
133811
133812    for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
133813    pEList = pLeft->pEList;
133814    if( pCte->pCols ){
133815      if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
133816        sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
133817            pCte->zName, pEList->nExpr, pCte->pCols->nExpr
133818        );
133819        pParse->pWith = pSavedWith;
133820        return SQLITE_ERROR;
133821      }
133822      pEList = pCte->pCols;
133823    }
133824
133825    sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
133826    if( bMayRecursive ){
133827      if( pSel->selFlags & SF_Recursive ){
133828        pCte->zCteErr = "multiple recursive references: %s";
133829      }else{
133830        pCte->zCteErr = "recursive reference in a subquery: %s";
133831      }
133832      sqlite3WalkSelect(pWalker, pSel);
133833    }
133834    pCte->zCteErr = 0;
133835    pParse->pWith = pSavedWith;
133836  }
133837
133838  return SQLITE_OK;
133839}
133840#endif
133841
133842#ifndef SQLITE_OMIT_CTE
133843/*
133844** If the SELECT passed as the second argument has an associated WITH
133845** clause, pop it from the stack stored as part of the Parse object.
133846**
133847** This function is used as the xSelectCallback2() callback by
133848** sqlite3SelectExpand() when walking a SELECT tree to resolve table
133849** names and other FROM clause elements.
133850*/
133851static void selectPopWith(Walker *pWalker, Select *p){
133852  Parse *pParse = pWalker->pParse;
133853  if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
133854    With *pWith = findRightmost(p)->pWith;
133855    if( pWith!=0 ){
133856      assert( pParse->pWith==pWith || pParse->nErr );
133857      pParse->pWith = pWith->pOuter;
133858    }
133859  }
133860}
133861#else
133862#define selectPopWith 0
133863#endif
133864
133865/*
133866** The SrcList_item structure passed as the second argument represents a
133867** sub-query in the FROM clause of a SELECT statement. This function
133868** allocates and populates the SrcList_item.pTab object. If successful,
133869** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
133870** SQLITE_NOMEM.
133871*/
133872SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse *pParse, struct SrcList_item *pFrom){
133873  Select *pSel = pFrom->pSelect;
133874  Table *pTab;
133875
133876  assert( pSel );
133877  pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
133878  if( pTab==0 ) return SQLITE_NOMEM;
133879  pTab->nTabRef = 1;
133880  if( pFrom->zAlias ){
133881    pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
133882  }else{
133883    pTab->zName = sqlite3MPrintf(pParse->db, "subquery_%u", pSel->selId);
133884  }
133885  while( pSel->pPrior ){ pSel = pSel->pPrior; }
133886  sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
133887  pTab->iPKey = -1;
133888  pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
133889  pTab->tabFlags |= TF_Ephemeral;
133890
133891  return pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
133892}
133893
133894/*
133895** This routine is a Walker callback for "expanding" a SELECT statement.
133896** "Expanding" means to do the following:
133897**
133898**    (1)  Make sure VDBE cursor numbers have been assigned to every
133899**         element of the FROM clause.
133900**
133901**    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that
133902**         defines FROM clause.  When views appear in the FROM clause,
133903**         fill pTabList->a[].pSelect with a copy of the SELECT statement
133904**         that implements the view.  A copy is made of the view's SELECT
133905**         statement so that we can freely modify or delete that statement
133906**         without worrying about messing up the persistent representation
133907**         of the view.
133908**
133909**    (3)  Add terms to the WHERE clause to accommodate the NATURAL keyword
133910**         on joins and the ON and USING clause of joins.
133911**
133912**    (4)  Scan the list of columns in the result set (pEList) looking
133913**         for instances of the "*" operator or the TABLE.* operator.
133914**         If found, expand each "*" to be every column in every table
133915**         and TABLE.* to be every column in TABLE.
133916**
133917*/
133918static int selectExpander(Walker *pWalker, Select *p){
133919  Parse *pParse = pWalker->pParse;
133920  int i, j, k;
133921  SrcList *pTabList;
133922  ExprList *pEList;
133923  struct SrcList_item *pFrom;
133924  sqlite3 *db = pParse->db;
133925  Expr *pE, *pRight, *pExpr;
133926  u16 selFlags = p->selFlags;
133927  u32 elistFlags = 0;
133928
133929  p->selFlags |= SF_Expanded;
133930  if( db->mallocFailed  ){
133931    return WRC_Abort;
133932  }
133933  assert( p->pSrc!=0 );
133934  if( (selFlags & SF_Expanded)!=0 ){
133935    return WRC_Prune;
133936  }
133937  if( pWalker->eCode ){
133938    /* Renumber selId because it has been copied from a view */
133939    p->selId = ++pParse->nSelect;
133940  }
133941  pTabList = p->pSrc;
133942  pEList = p->pEList;
133943  sqlite3WithPush(pParse, p->pWith, 0);
133944
133945  /* Make sure cursor numbers have been assigned to all entries in
133946  ** the FROM clause of the SELECT statement.
133947  */
133948  sqlite3SrcListAssignCursors(pParse, pTabList);
133949
133950  /* Look up every table named in the FROM clause of the select.  If
133951  ** an entry of the FROM clause is a subquery instead of a table or view,
133952  ** then create a transient table structure to describe the subquery.
133953  */
133954  for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
133955    Table *pTab;
133956    assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
133957    if( pFrom->fg.isRecursive ) continue;
133958    assert( pFrom->pTab==0 );
133959#ifndef SQLITE_OMIT_CTE
133960    if( withExpand(pWalker, pFrom) ) return WRC_Abort;
133961    if( pFrom->pTab ) {} else
133962#endif
133963    if( pFrom->zName==0 ){
133964#ifndef SQLITE_OMIT_SUBQUERY
133965      Select *pSel = pFrom->pSelect;
133966      /* A sub-query in the FROM clause of a SELECT */
133967      assert( pSel!=0 );
133968      assert( pFrom->pTab==0 );
133969      if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort;
133970      if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort;
133971#endif
133972    }else{
133973      /* An ordinary table or view name in the FROM clause */
133974      assert( pFrom->pTab==0 );
133975      pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
133976      if( pTab==0 ) return WRC_Abort;
133977      if( pTab->nTabRef>=0xffff ){
133978        sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
133979           pTab->zName);
133980        pFrom->pTab = 0;
133981        return WRC_Abort;
133982      }
133983      pTab->nTabRef++;
133984      if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){
133985        return WRC_Abort;
133986      }
133987#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
133988      if( IsVirtual(pTab) || pTab->pSelect ){
133989        i16 nCol;
133990        u8 eCodeOrig = pWalker->eCode;
133991        if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
133992        assert( pFrom->pSelect==0 );
133993        if( pTab->pSelect && (db->flags & SQLITE_EnableView)==0 ){
133994          sqlite3ErrorMsg(pParse, "access to view \"%s\" prohibited",
133995            pTab->zName);
133996        }
133997#ifndef SQLITE_OMIT_VIRTUALTABLE
133998        if( IsVirtual(pTab)
133999         && pFrom->fg.fromDDL
134000         && ALWAYS(pTab->pVTable!=0)
134001         && pTab->pVTable->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0)
134002        ){
134003          sqlite3ErrorMsg(pParse, "unsafe use of virtual table \"%s\"",
134004                                  pTab->zName);
134005        }
134006#endif
134007        pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
134008        nCol = pTab->nCol;
134009        pTab->nCol = -1;
134010        pWalker->eCode = 1;  /* Turn on Select.selId renumbering */
134011        sqlite3WalkSelect(pWalker, pFrom->pSelect);
134012        pWalker->eCode = eCodeOrig;
134013        pTab->nCol = nCol;
134014      }
134015#endif
134016    }
134017
134018    /* Locate the index named by the INDEXED BY clause, if any. */
134019    if( sqlite3IndexedByLookup(pParse, pFrom) ){
134020      return WRC_Abort;
134021    }
134022  }
134023
134024  /* Process NATURAL keywords, and ON and USING clauses of joins.
134025  */
134026  if( pParse->nErr || db->mallocFailed || sqliteProcessJoin(pParse, p) ){
134027    return WRC_Abort;
134028  }
134029
134030  /* For every "*" that occurs in the column list, insert the names of
134031  ** all columns in all tables.  And for every TABLE.* insert the names
134032  ** of all columns in TABLE.  The parser inserted a special expression
134033  ** with the TK_ASTERISK operator for each "*" that it found in the column
134034  ** list.  The following code just has to locate the TK_ASTERISK
134035  ** expressions and expand each one to the list of all columns in
134036  ** all tables.
134037  **
134038  ** The first loop just checks to see if there are any "*" operators
134039  ** that need expanding.
134040  */
134041  for(k=0; k<pEList->nExpr; k++){
134042    pE = pEList->a[k].pExpr;
134043    if( pE->op==TK_ASTERISK ) break;
134044    assert( pE->op!=TK_DOT || pE->pRight!=0 );
134045    assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
134046    if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
134047    elistFlags |= pE->flags;
134048  }
134049  if( k<pEList->nExpr ){
134050    /*
134051    ** If we get here it means the result set contains one or more "*"
134052    ** operators that need to be expanded.  Loop through each expression
134053    ** in the result set and expand them one by one.
134054    */
134055    struct ExprList_item *a = pEList->a;
134056    ExprList *pNew = 0;
134057    int flags = pParse->db->flags;
134058    int longNames = (flags & SQLITE_FullColNames)!=0
134059                      && (flags & SQLITE_ShortColNames)==0;
134060
134061    for(k=0; k<pEList->nExpr; k++){
134062      pE = a[k].pExpr;
134063      elistFlags |= pE->flags;
134064      pRight = pE->pRight;
134065      assert( pE->op!=TK_DOT || pRight!=0 );
134066      if( pE->op!=TK_ASTERISK
134067       && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
134068      ){
134069        /* This particular expression does not need to be expanded.
134070        */
134071        pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
134072        if( pNew ){
134073          pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
134074          pNew->a[pNew->nExpr-1].eEName = a[k].eEName;
134075          a[k].zEName = 0;
134076        }
134077        a[k].pExpr = 0;
134078      }else{
134079        /* This expression is a "*" or a "TABLE.*" and needs to be
134080        ** expanded. */
134081        int tableSeen = 0;      /* Set to 1 when TABLE matches */
134082        char *zTName = 0;       /* text of name of TABLE */
134083        if( pE->op==TK_DOT ){
134084          assert( pE->pLeft!=0 );
134085          assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
134086          zTName = pE->pLeft->u.zToken;
134087        }
134088        for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
134089          Table *pTab = pFrom->pTab;
134090          Select *pSub = pFrom->pSelect;
134091          char *zTabName = pFrom->zAlias;
134092          const char *zSchemaName = 0;
134093          int iDb;
134094          if( zTabName==0 ){
134095            zTabName = pTab->zName;
134096          }
134097          if( db->mallocFailed ) break;
134098          if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
134099            pSub = 0;
134100            if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
134101              continue;
134102            }
134103            iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
134104            zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
134105          }
134106          for(j=0; j<pTab->nCol; j++){
134107            char *zName = pTab->aCol[j].zName;
134108            char *zColname;  /* The computed column name */
134109            char *zToFree;   /* Malloced string that needs to be freed */
134110            Token sColname;  /* Computed column name as a token */
134111
134112            assert( zName );
134113            if( zTName && pSub
134114             && sqlite3MatchEName(&pSub->pEList->a[j], 0, zTName, 0)==0
134115            ){
134116              continue;
134117            }
134118
134119            /* If a column is marked as 'hidden', omit it from the expanded
134120            ** result-set list unless the SELECT has the SF_IncludeHidden
134121            ** bit set.
134122            */
134123            if( (p->selFlags & SF_IncludeHidden)==0
134124             && IsHiddenColumn(&pTab->aCol[j])
134125            ){
134126              continue;
134127            }
134128            tableSeen = 1;
134129
134130            if( i>0 && zTName==0 ){
134131              if( (pFrom->fg.jointype & JT_NATURAL)!=0
134132                && tableAndColumnIndex(pTabList, i, zName, 0, 0, 1)
134133              ){
134134                /* In a NATURAL join, omit the join columns from the
134135                ** table to the right of the join */
134136                continue;
134137              }
134138              if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
134139                /* In a join with a USING clause, omit columns in the
134140                ** using clause from the table on the right. */
134141                continue;
134142              }
134143            }
134144            pRight = sqlite3Expr(db, TK_ID, zName);
134145            zColname = zName;
134146            zToFree = 0;
134147            if( longNames || pTabList->nSrc>1 ){
134148              Expr *pLeft;
134149              pLeft = sqlite3Expr(db, TK_ID, zTabName);
134150              pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
134151              if( zSchemaName ){
134152                pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
134153                pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr);
134154              }
134155              if( longNames ){
134156                zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
134157                zToFree = zColname;
134158              }
134159            }else{
134160              pExpr = pRight;
134161            }
134162            pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
134163            sqlite3TokenInit(&sColname, zColname);
134164            sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
134165            if( pNew && (p->selFlags & SF_NestedFrom)!=0 && !IN_RENAME_OBJECT ){
134166              struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
134167              sqlite3DbFree(db, pX->zEName);
134168              if( pSub ){
134169                pX->zEName = sqlite3DbStrDup(db, pSub->pEList->a[j].zEName);
134170                testcase( pX->zEName==0 );
134171              }else{
134172                pX->zEName = sqlite3MPrintf(db, "%s.%s.%s",
134173                                           zSchemaName, zTabName, zColname);
134174                testcase( pX->zEName==0 );
134175              }
134176              pX->eEName = ENAME_TAB;
134177            }
134178            sqlite3DbFree(db, zToFree);
134179          }
134180        }
134181        if( !tableSeen ){
134182          if( zTName ){
134183            sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
134184          }else{
134185            sqlite3ErrorMsg(pParse, "no tables specified");
134186          }
134187        }
134188      }
134189    }
134190    sqlite3ExprListDelete(db, pEList);
134191    p->pEList = pNew;
134192  }
134193  if( p->pEList ){
134194    if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
134195      sqlite3ErrorMsg(pParse, "too many columns in result set");
134196      return WRC_Abort;
134197    }
134198    if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){
134199      p->selFlags |= SF_ComplexResult;
134200    }
134201  }
134202  return WRC_Continue;
134203}
134204
134205#if SQLITE_DEBUG
134206/*
134207** Always assert.  This xSelectCallback2 implementation proves that the
134208** xSelectCallback2 is never invoked.
134209*/
134210SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){
134211  UNUSED_PARAMETER2(NotUsed, NotUsed2);
134212  assert( 0 );
134213}
134214#endif
134215/*
134216** This routine "expands" a SELECT statement and all of its subqueries.
134217** For additional information on what it means to "expand" a SELECT
134218** statement, see the comment on the selectExpand worker callback above.
134219**
134220** Expanding a SELECT statement is the first step in processing a
134221** SELECT statement.  The SELECT statement must be expanded before
134222** name resolution is performed.
134223**
134224** If anything goes wrong, an error message is written into pParse.
134225** The calling function can detect the problem by looking at pParse->nErr
134226** and/or pParse->db->mallocFailed.
134227*/
134228static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
134229  Walker w;
134230  w.xExprCallback = sqlite3ExprWalkNoop;
134231  w.pParse = pParse;
134232  if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
134233    w.xSelectCallback = convertCompoundSelectToSubquery;
134234    w.xSelectCallback2 = 0;
134235    sqlite3WalkSelect(&w, pSelect);
134236  }
134237  w.xSelectCallback = selectExpander;
134238  w.xSelectCallback2 = selectPopWith;
134239  w.eCode = 0;
134240  sqlite3WalkSelect(&w, pSelect);
134241}
134242
134243
134244#ifndef SQLITE_OMIT_SUBQUERY
134245/*
134246** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
134247** interface.
134248**
134249** For each FROM-clause subquery, add Column.zType and Column.zColl
134250** information to the Table structure that represents the result set
134251** of that subquery.
134252**
134253** The Table structure that represents the result set was constructed
134254** by selectExpander() but the type and collation information was omitted
134255** at that point because identifiers had not yet been resolved.  This
134256** routine is called after identifier resolution.
134257*/
134258static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
134259  Parse *pParse;
134260  int i;
134261  SrcList *pTabList;
134262  struct SrcList_item *pFrom;
134263
134264  assert( p->selFlags & SF_Resolved );
134265  if( p->selFlags & SF_HasTypeInfo ) return;
134266  p->selFlags |= SF_HasTypeInfo;
134267  pParse = pWalker->pParse;
134268  pTabList = p->pSrc;
134269  for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
134270    Table *pTab = pFrom->pTab;
134271    assert( pTab!=0 );
134272    if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
134273      /* A sub-query in the FROM clause of a SELECT */
134274      Select *pSel = pFrom->pSelect;
134275      if( pSel ){
134276        while( pSel->pPrior ) pSel = pSel->pPrior;
134277        sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel,
134278                                               SQLITE_AFF_NONE);
134279      }
134280    }
134281  }
134282}
134283#endif
134284
134285
134286/*
134287** This routine adds datatype and collating sequence information to
134288** the Table structures of all FROM-clause subqueries in a
134289** SELECT statement.
134290**
134291** Use this routine after name resolution.
134292*/
134293static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
134294#ifndef SQLITE_OMIT_SUBQUERY
134295  Walker w;
134296  w.xSelectCallback = sqlite3SelectWalkNoop;
134297  w.xSelectCallback2 = selectAddSubqueryTypeInfo;
134298  w.xExprCallback = sqlite3ExprWalkNoop;
134299  w.pParse = pParse;
134300  sqlite3WalkSelect(&w, pSelect);
134301#endif
134302}
134303
134304
134305/*
134306** This routine sets up a SELECT statement for processing.  The
134307** following is accomplished:
134308**
134309**     *  VDBE Cursor numbers are assigned to all FROM-clause terms.
134310**     *  Ephemeral Table objects are created for all FROM-clause subqueries.
134311**     *  ON and USING clauses are shifted into WHERE statements
134312**     *  Wildcards "*" and "TABLE.*" in result sets are expanded.
134313**     *  Identifiers in expression are matched to tables.
134314**
134315** This routine acts recursively on all subqueries within the SELECT.
134316*/
134317SQLITE_PRIVATE void sqlite3SelectPrep(
134318  Parse *pParse,         /* The parser context */
134319  Select *p,             /* The SELECT statement being coded. */
134320  NameContext *pOuterNC  /* Name context for container */
134321){
134322  assert( p!=0 || pParse->db->mallocFailed );
134323  if( pParse->db->mallocFailed ) return;
134324  if( p->selFlags & SF_HasTypeInfo ) return;
134325  sqlite3SelectExpand(pParse, p);
134326  if( pParse->nErr || pParse->db->mallocFailed ) return;
134327  sqlite3ResolveSelectNames(pParse, p, pOuterNC);
134328  if( pParse->nErr || pParse->db->mallocFailed ) return;
134329  sqlite3SelectAddTypeInfo(pParse, p);
134330}
134331
134332/*
134333** Reset the aggregate accumulator.
134334**
134335** The aggregate accumulator is a set of memory cells that hold
134336** intermediate results while calculating an aggregate.  This
134337** routine generates code that stores NULLs in all of those memory
134338** cells.
134339*/
134340static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
134341  Vdbe *v = pParse->pVdbe;
134342  int i;
134343  struct AggInfo_func *pFunc;
134344  int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
134345  if( nReg==0 ) return;
134346  if( pParse->nErr ) return;
134347#ifdef SQLITE_DEBUG
134348  /* Verify that all AggInfo registers are within the range specified by
134349  ** AggInfo.mnReg..AggInfo.mxReg */
134350  assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
134351  for(i=0; i<pAggInfo->nColumn; i++){
134352    assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
134353         && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
134354  }
134355  for(i=0; i<pAggInfo->nFunc; i++){
134356    assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
134357         && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
134358  }
134359#endif
134360  sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
134361  for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
134362    if( pFunc->iDistinct>=0 ){
134363      Expr *pE = pFunc->pExpr;
134364      assert( !ExprHasProperty(pE, EP_xIsSelect) );
134365      if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
134366        sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
134367           "argument");
134368        pFunc->iDistinct = -1;
134369      }else{
134370        KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
134371        sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
134372                          (char*)pKeyInfo, P4_KEYINFO);
134373      }
134374    }
134375  }
134376}
134377
134378/*
134379** Invoke the OP_AggFinalize opcode for every aggregate function
134380** in the AggInfo structure.
134381*/
134382static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
134383  Vdbe *v = pParse->pVdbe;
134384  int i;
134385  struct AggInfo_func *pF;
134386  for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
134387    ExprList *pList = pF->pExpr->x.pList;
134388    assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
134389    sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
134390    sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
134391  }
134392}
134393
134394
134395/*
134396** Update the accumulator memory cells for an aggregate based on
134397** the current cursor position.
134398**
134399** If regAcc is non-zero and there are no min() or max() aggregates
134400** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
134401** registers if register regAcc contains 0. The caller will take care
134402** of setting and clearing regAcc.
134403*/
134404static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){
134405  Vdbe *v = pParse->pVdbe;
134406  int i;
134407  int regHit = 0;
134408  int addrHitTest = 0;
134409  struct AggInfo_func *pF;
134410  struct AggInfo_col *pC;
134411
134412  pAggInfo->directMode = 1;
134413  for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
134414    int nArg;
134415    int addrNext = 0;
134416    int regAgg;
134417    ExprList *pList = pF->pExpr->x.pList;
134418    assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
134419    assert( !IsWindowFunc(pF->pExpr) );
134420    if( ExprHasProperty(pF->pExpr, EP_WinFunc) ){
134421      Expr *pFilter = pF->pExpr->y.pWin->pFilter;
134422      if( pAggInfo->nAccumulator
134423       && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
134424      ){
134425        if( regHit==0 ) regHit = ++pParse->nMem;
134426        /* If this is the first row of the group (regAcc==0), clear the
134427        ** "magnet" register regHit so that the accumulator registers
134428        ** are populated if the FILTER clause jumps over the the
134429        ** invocation of min() or max() altogether. Or, if this is not
134430        ** the first row (regAcc==1), set the magnet register so that the
134431        ** accumulators are not populated unless the min()/max() is invoked and
134432        ** indicates that they should be.  */
134433        sqlite3VdbeAddOp2(v, OP_Copy, regAcc, regHit);
134434      }
134435      addrNext = sqlite3VdbeMakeLabel(pParse);
134436      sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL);
134437    }
134438    if( pList ){
134439      nArg = pList->nExpr;
134440      regAgg = sqlite3GetTempRange(pParse, nArg);
134441      sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP);
134442    }else{
134443      nArg = 0;
134444      regAgg = 0;
134445    }
134446    if( pF->iDistinct>=0 ){
134447      if( addrNext==0 ){
134448        addrNext = sqlite3VdbeMakeLabel(pParse);
134449      }
134450      testcase( nArg==0 );  /* Error condition */
134451      testcase( nArg>1 );   /* Also an error */
134452      codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
134453    }
134454    if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
134455      CollSeq *pColl = 0;
134456      struct ExprList_item *pItem;
134457      int j;
134458      assert( pList!=0 );  /* pList!=0 if pF->pFunc has NEEDCOLL */
134459      for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
134460        pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
134461      }
134462      if( !pColl ){
134463        pColl = pParse->db->pDfltColl;
134464      }
134465      if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
134466      sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
134467    }
134468    sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, pF->iMem);
134469    sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
134470    sqlite3VdbeChangeP5(v, (u8)nArg);
134471    sqlite3ReleaseTempRange(pParse, regAgg, nArg);
134472    if( addrNext ){
134473      sqlite3VdbeResolveLabel(v, addrNext);
134474    }
134475  }
134476  if( regHit==0 && pAggInfo->nAccumulator ){
134477    regHit = regAcc;
134478  }
134479  if( regHit ){
134480    addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
134481  }
134482  for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
134483    sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
134484  }
134485
134486  pAggInfo->directMode = 0;
134487  if( addrHitTest ){
134488    sqlite3VdbeJumpHereOrPopInst(v, addrHitTest);
134489  }
134490}
134491
134492/*
134493** Add a single OP_Explain instruction to the VDBE to explain a simple
134494** count(*) query ("SELECT count(*) FROM pTab").
134495*/
134496#ifndef SQLITE_OMIT_EXPLAIN
134497static void explainSimpleCount(
134498  Parse *pParse,                  /* Parse context */
134499  Table *pTab,                    /* Table being queried */
134500  Index *pIdx                     /* Index used to optimize scan, or NULL */
134501){
134502  if( pParse->explain==2 ){
134503    int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
134504    sqlite3VdbeExplain(pParse, 0, "SCAN TABLE %s%s%s",
134505        pTab->zName,
134506        bCover ? " USING COVERING INDEX " : "",
134507        bCover ? pIdx->zName : ""
134508    );
134509  }
134510}
134511#else
134512# define explainSimpleCount(a,b,c)
134513#endif
134514
134515/*
134516** sqlite3WalkExpr() callback used by havingToWhere().
134517**
134518** If the node passed to the callback is a TK_AND node, return
134519** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
134520**
134521** Otherwise, return WRC_Prune. In this case, also check if the
134522** sub-expression matches the criteria for being moved to the WHERE
134523** clause. If so, add it to the WHERE clause and replace the sub-expression
134524** within the HAVING expression with a constant "1".
134525*/
134526static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
134527  if( pExpr->op!=TK_AND ){
134528    Select *pS = pWalker->u.pSelect;
134529    if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){
134530      sqlite3 *db = pWalker->pParse->db;
134531      Expr *pNew = sqlite3Expr(db, TK_INTEGER, "1");
134532      if( pNew ){
134533        Expr *pWhere = pS->pWhere;
134534        SWAP(Expr, *pNew, *pExpr);
134535        pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
134536        pS->pWhere = pNew;
134537        pWalker->eCode = 1;
134538      }
134539    }
134540    return WRC_Prune;
134541  }
134542  return WRC_Continue;
134543}
134544
134545/*
134546** Transfer eligible terms from the HAVING clause of a query, which is
134547** processed after grouping, to the WHERE clause, which is processed before
134548** grouping. For example, the query:
134549**
134550**   SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=?
134551**
134552** can be rewritten as:
134553**
134554**   SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
134555**
134556** A term of the HAVING expression is eligible for transfer if it consists
134557** entirely of constants and expressions that are also GROUP BY terms that
134558** use the "BINARY" collation sequence.
134559*/
134560static void havingToWhere(Parse *pParse, Select *p){
134561  Walker sWalker;
134562  memset(&sWalker, 0, sizeof(sWalker));
134563  sWalker.pParse = pParse;
134564  sWalker.xExprCallback = havingToWhereExprCb;
134565  sWalker.u.pSelect = p;
134566  sqlite3WalkExpr(&sWalker, p->pHaving);
134567#if SELECTTRACE_ENABLED
134568  if( sWalker.eCode && (sqlite3SelectTrace & 0x100)!=0 ){
134569    SELECTTRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
134570    sqlite3TreeViewSelect(0, p, 0);
134571  }
134572#endif
134573}
134574
134575/*
134576** Check to see if the pThis entry of pTabList is a self-join of a prior view.
134577** If it is, then return the SrcList_item for the prior view.  If it is not,
134578** then return 0.
134579*/
134580static struct SrcList_item *isSelfJoinView(
134581  SrcList *pTabList,           /* Search for self-joins in this FROM clause */
134582  struct SrcList_item *pThis   /* Search for prior reference to this subquery */
134583){
134584  struct SrcList_item *pItem;
134585  for(pItem = pTabList->a; pItem<pThis; pItem++){
134586    Select *pS1;
134587    if( pItem->pSelect==0 ) continue;
134588    if( pItem->fg.viaCoroutine ) continue;
134589    if( pItem->zName==0 ) continue;
134590    assert( pItem->pTab!=0 );
134591    assert( pThis->pTab!=0 );
134592    if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue;
134593    if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
134594    pS1 = pItem->pSelect;
134595    if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){
134596      /* The query flattener left two different CTE tables with identical
134597      ** names in the same FROM clause. */
134598      continue;
134599    }
134600    if( sqlite3ExprCompare(0, pThis->pSelect->pWhere, pS1->pWhere, -1)
134601     || sqlite3ExprCompare(0, pThis->pSelect->pHaving, pS1->pHaving, -1)
134602    ){
134603      /* The view was modified by some other optimization such as
134604      ** pushDownWhereTerms() */
134605      continue;
134606    }
134607    return pItem;
134608  }
134609  return 0;
134610}
134611
134612#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
134613/*
134614** Attempt to transform a query of the form
134615**
134616**    SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2)
134617**
134618** Into this:
134619**
134620**    SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2)
134621**
134622** The transformation only works if all of the following are true:
134623**
134624**   *  The subquery is a UNION ALL of two or more terms
134625**   *  The subquery does not have a LIMIT clause
134626**   *  There is no WHERE or GROUP BY or HAVING clauses on the subqueries
134627**   *  The outer query is a simple count(*) with no WHERE clause or other
134628**      extraneous syntax.
134629**
134630** Return TRUE if the optimization is undertaken.
134631*/
134632static int countOfViewOptimization(Parse *pParse, Select *p){
134633  Select *pSub, *pPrior;
134634  Expr *pExpr;
134635  Expr *pCount;
134636  sqlite3 *db;
134637  if( (p->selFlags & SF_Aggregate)==0 ) return 0;   /* This is an aggregate */
134638  if( p->pEList->nExpr!=1 ) return 0;               /* Single result column */
134639  if( p->pWhere ) return 0;
134640  if( p->pGroupBy ) return 0;
134641  pExpr = p->pEList->a[0].pExpr;
134642  if( pExpr->op!=TK_AGG_FUNCTION ) return 0;        /* Result is an aggregate */
134643  if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0;  /* Is count() */
134644  if( pExpr->x.pList!=0 ) return 0;                 /* Must be count(*) */
134645  if( p->pSrc->nSrc!=1 ) return 0;                  /* One table in FROM  */
134646  pSub = p->pSrc->a[0].pSelect;
134647  if( pSub==0 ) return 0;                           /* The FROM is a subquery */
134648  if( pSub->pPrior==0 ) return 0;                   /* Must be a compound ry */
134649  do{
134650    if( pSub->op!=TK_ALL && pSub->pPrior ) return 0;  /* Must be UNION ALL */
134651    if( pSub->pWhere ) return 0;                      /* No WHERE clause */
134652    if( pSub->pLimit ) return 0;                      /* No LIMIT clause */
134653    if( pSub->selFlags & SF_Aggregate ) return 0;     /* Not an aggregate */
134654    pSub = pSub->pPrior;                              /* Repeat over compound */
134655  }while( pSub );
134656
134657  /* If we reach this point then it is OK to perform the transformation */
134658
134659  db = pParse->db;
134660  pCount = pExpr;
134661  pExpr = 0;
134662  pSub = p->pSrc->a[0].pSelect;
134663  p->pSrc->a[0].pSelect = 0;
134664  sqlite3SrcListDelete(db, p->pSrc);
134665  p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
134666  while( pSub ){
134667    Expr *pTerm;
134668    pPrior = pSub->pPrior;
134669    pSub->pPrior = 0;
134670    pSub->pNext = 0;
134671    pSub->selFlags |= SF_Aggregate;
134672    pSub->selFlags &= ~SF_Compound;
134673    pSub->nSelectRow = 0;
134674    sqlite3ExprListDelete(db, pSub->pEList);
134675    pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
134676    pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
134677    pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
134678    sqlite3PExprAddSelect(pParse, pTerm, pSub);
134679    if( pExpr==0 ){
134680      pExpr = pTerm;
134681    }else{
134682      pExpr = sqlite3PExpr(pParse, TK_PLUS, pTerm, pExpr);
134683    }
134684    pSub = pPrior;
134685  }
134686  p->pEList->a[0].pExpr = pExpr;
134687  p->selFlags &= ~SF_Aggregate;
134688
134689#if SELECTTRACE_ENABLED
134690  if( sqlite3SelectTrace & 0x400 ){
134691    SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
134692    sqlite3TreeViewSelect(0, p, 0);
134693  }
134694#endif
134695  return 1;
134696}
134697#endif /* SQLITE_COUNTOFVIEW_OPTIMIZATION */
134698
134699/*
134700** Generate code for the SELECT statement given in the p argument.
134701**
134702** The results are returned according to the SelectDest structure.
134703** See comments in sqliteInt.h for further information.
134704**
134705** This routine returns the number of errors.  If any errors are
134706** encountered, then an appropriate error message is left in
134707** pParse->zErrMsg.
134708**
134709** This routine does NOT free the Select structure passed in.  The
134710** calling function needs to do that.
134711*/
134712SQLITE_PRIVATE int sqlite3Select(
134713  Parse *pParse,         /* The parser context */
134714  Select *p,             /* The SELECT statement being coded. */
134715  SelectDest *pDest      /* What to do with the query results */
134716){
134717  int i, j;              /* Loop counters */
134718  WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
134719  Vdbe *v;               /* The virtual machine under construction */
134720  int isAgg;             /* True for select lists like "count(*)" */
134721  ExprList *pEList = 0;  /* List of columns to extract. */
134722  SrcList *pTabList;     /* List of tables to select from */
134723  Expr *pWhere;          /* The WHERE clause.  May be NULL */
134724  ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
134725  Expr *pHaving;         /* The HAVING clause.  May be NULL */
134726  AggInfo *pAggInfo = 0; /* Aggregate information */
134727  int rc = 1;            /* Value to return from this function */
134728  DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
134729  SortCtx sSort;         /* Info on how to code the ORDER BY clause */
134730  int iEnd;              /* Address of the end of the query */
134731  sqlite3 *db;           /* The database connection */
134732  ExprList *pMinMaxOrderBy = 0;  /* Added ORDER BY for min/max queries */
134733  u8 minMaxFlag;                 /* Flag for min/max queries */
134734
134735  db = pParse->db;
134736  v = sqlite3GetVdbe(pParse);
134737  if( p==0 || db->mallocFailed || pParse->nErr ){
134738    return 1;
134739  }
134740  if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
134741#if SELECTTRACE_ENABLED
134742  SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
134743  if( sqlite3SelectTrace & 0x100 ){
134744    sqlite3TreeViewSelect(0, p, 0);
134745  }
134746#endif
134747
134748  assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
134749  assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
134750  assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
134751  assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
134752  if( IgnorableOrderby(pDest) ){
134753    assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
134754           pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
134755           pDest->eDest==SRT_Queue  || pDest->eDest==SRT_DistFifo ||
134756           pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_Fifo);
134757    /* If ORDER BY makes no difference in the output then neither does
134758    ** DISTINCT so it can be removed too. */
134759    sqlite3ExprListDelete(db, p->pOrderBy);
134760    p->pOrderBy = 0;
134761    p->selFlags &= ~SF_Distinct;
134762    p->selFlags |= SF_NoopOrderBy;
134763  }
134764  sqlite3SelectPrep(pParse, p, 0);
134765  if( pParse->nErr || db->mallocFailed ){
134766    goto select_end;
134767  }
134768  assert( p->pEList!=0 );
134769#if SELECTTRACE_ENABLED
134770  if( sqlite3SelectTrace & 0x104 ){
134771    SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
134772    sqlite3TreeViewSelect(0, p, 0);
134773  }
134774#endif
134775
134776  if( pDest->eDest==SRT_Output ){
134777    generateColumnNames(pParse, p);
134778  }
134779
134780#ifndef SQLITE_OMIT_WINDOWFUNC
134781  rc = sqlite3WindowRewrite(pParse, p);
134782  if( rc ){
134783    assert( db->mallocFailed || pParse->nErr>0 );
134784    goto select_end;
134785  }
134786#if SELECTTRACE_ENABLED
134787  if( p->pWin && (sqlite3SelectTrace & 0x108)!=0 ){
134788    SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
134789    sqlite3TreeViewSelect(0, p, 0);
134790  }
134791#endif
134792#endif /* SQLITE_OMIT_WINDOWFUNC */
134793  pTabList = p->pSrc;
134794  isAgg = (p->selFlags & SF_Aggregate)!=0;
134795  memset(&sSort, 0, sizeof(sSort));
134796  sSort.pOrderBy = p->pOrderBy;
134797
134798  /* Try to various optimizations (flattening subqueries, and strength
134799  ** reduction of join operators) in the FROM clause up into the main query
134800  */
134801#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
134802  for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
134803    struct SrcList_item *pItem = &pTabList->a[i];
134804    Select *pSub = pItem->pSelect;
134805    Table *pTab = pItem->pTab;
134806
134807    /* Convert LEFT JOIN into JOIN if there are terms of the right table
134808    ** of the LEFT JOIN used in the WHERE clause.
134809    */
134810    if( (pItem->fg.jointype & JT_LEFT)!=0
134811     && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
134812     && OptimizationEnabled(db, SQLITE_SimplifyJoin)
134813    ){
134814      SELECTTRACE(0x100,pParse,p,
134815                ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
134816      pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
134817      unsetJoinExpr(p->pWhere, pItem->iCursor);
134818    }
134819
134820    /* No futher action if this term of the FROM clause is no a subquery */
134821    if( pSub==0 ) continue;
134822
134823    /* Catch mismatch in the declared columns of a view and the number of
134824    ** columns in the SELECT on the RHS */
134825    if( pTab->nCol!=pSub->pEList->nExpr ){
134826      sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
134827                      pTab->nCol, pTab->zName, pSub->pEList->nExpr);
134828      goto select_end;
134829    }
134830
134831    /* Do not try to flatten an aggregate subquery.
134832    **
134833    ** Flattening an aggregate subquery is only possible if the outer query
134834    ** is not a join.  But if the outer query is not a join, then the subquery
134835    ** will be implemented as a co-routine and there is no advantage to
134836    ** flattening in that case.
134837    */
134838    if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
134839    assert( pSub->pGroupBy==0 );
134840
134841    /* If the outer query contains a "complex" result set (that is,
134842    ** if the result set of the outer query uses functions or subqueries)
134843    ** and if the subquery contains an ORDER BY clause and if
134844    ** it will be implemented as a co-routine, then do not flatten.  This
134845    ** restriction allows SQL constructs like this:
134846    **
134847    **  SELECT expensive_function(x)
134848    **    FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
134849    **
134850    ** The expensive_function() is only computed on the 10 rows that
134851    ** are output, rather than every row of the table.
134852    **
134853    ** The requirement that the outer query have a complex result set
134854    ** means that flattening does occur on simpler SQL constraints without
134855    ** the expensive_function() like:
134856    **
134857    **  SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
134858    */
134859    if( pSub->pOrderBy!=0
134860     && i==0
134861     && (p->selFlags & SF_ComplexResult)!=0
134862     && (pTabList->nSrc==1
134863         || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)
134864    ){
134865      continue;
134866    }
134867
134868    if( flattenSubquery(pParse, p, i, isAgg) ){
134869      if( pParse->nErr ) goto select_end;
134870      /* This subquery can be absorbed into its parent. */
134871      i = -1;
134872    }
134873    pTabList = p->pSrc;
134874    if( db->mallocFailed ) goto select_end;
134875    if( !IgnorableOrderby(pDest) ){
134876      sSort.pOrderBy = p->pOrderBy;
134877    }
134878  }
134879#endif
134880
134881#ifndef SQLITE_OMIT_COMPOUND_SELECT
134882  /* Handle compound SELECT statements using the separate multiSelect()
134883  ** procedure.
134884  */
134885  if( p->pPrior ){
134886    rc = multiSelect(pParse, p, pDest);
134887#if SELECTTRACE_ENABLED
134888    SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
134889    if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
134890      sqlite3TreeViewSelect(0, p, 0);
134891    }
134892#endif
134893    if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
134894    return rc;
134895  }
134896#endif
134897
134898  /* Do the WHERE-clause constant propagation optimization if this is
134899  ** a join.  No need to speed time on this operation for non-join queries
134900  ** as the equivalent optimization will be handled by query planner in
134901  ** sqlite3WhereBegin().
134902  */
134903  if( pTabList->nSrc>1
134904   && OptimizationEnabled(db, SQLITE_PropagateConst)
134905   && propagateConstants(pParse, p)
134906  ){
134907#if SELECTTRACE_ENABLED
134908    if( sqlite3SelectTrace & 0x100 ){
134909      SELECTTRACE(0x100,pParse,p,("After constant propagation:\n"));
134910      sqlite3TreeViewSelect(0, p, 0);
134911    }
134912#endif
134913  }else{
134914    SELECTTRACE(0x100,pParse,p,("Constant propagation not helpful\n"));
134915  }
134916
134917#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
134918  if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)
134919   && countOfViewOptimization(pParse, p)
134920  ){
134921    if( db->mallocFailed ) goto select_end;
134922    pEList = p->pEList;
134923    pTabList = p->pSrc;
134924  }
134925#endif
134926
134927  /* For each term in the FROM clause, do two things:
134928  ** (1) Authorized unreferenced tables
134929  ** (2) Generate code for all sub-queries
134930  */
134931  for(i=0; i<pTabList->nSrc; i++){
134932    struct SrcList_item *pItem = &pTabList->a[i];
134933    SelectDest dest;
134934    Select *pSub;
134935#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
134936    const char *zSavedAuthContext;
134937#endif
134938
134939    /* Issue SQLITE_READ authorizations with a fake column name for any
134940    ** tables that are referenced but from which no values are extracted.
134941    ** Examples of where these kinds of null SQLITE_READ authorizations
134942    ** would occur:
134943    **
134944    **     SELECT count(*) FROM t1;   -- SQLITE_READ t1.""
134945    **     SELECT t1.* FROM t1, t2;   -- SQLITE_READ t2.""
134946    **
134947    ** The fake column name is an empty string.  It is possible for a table to
134948    ** have a column named by the empty string, in which case there is no way to
134949    ** distinguish between an unreferenced table and an actual reference to the
134950    ** "" column. The original design was for the fake column name to be a NULL,
134951    ** which would be unambiguous.  But legacy authorization callbacks might
134952    ** assume the column name is non-NULL and segfault.  The use of an empty
134953    ** string for the fake column name seems safer.
134954    */
134955    if( pItem->colUsed==0 && pItem->zName!=0 ){
134956      sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
134957    }
134958
134959#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
134960    /* Generate code for all sub-queries in the FROM clause
134961    */
134962    pSub = pItem->pSelect;
134963    if( pSub==0 ) continue;
134964
134965    /* The code for a subquery should only be generated once, though it is
134966    ** technically harmless for it to be generated multiple times. The
134967    ** following assert() will detect if something changes to cause
134968    ** the same subquery to be coded multiple times, as a signal to the
134969    ** developers to try to optimize the situation.
134970    **
134971    ** Update 2019-07-24:
134972    ** See ticket https://sqlite.org/src/tktview/c52b09c7f38903b1311cec40.
134973    ** The dbsqlfuzz fuzzer found a case where the same subquery gets
134974    ** coded twice.  So this assert() now becomes a testcase().  It should
134975    ** be very rare, though.
134976    */
134977    testcase( pItem->addrFillSub!=0 );
134978
134979    /* Increment Parse.nHeight by the height of the largest expression
134980    ** tree referred to by this, the parent select. The child select
134981    ** may contain expression trees of at most
134982    ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
134983    ** more conservative than necessary, but much easier than enforcing
134984    ** an exact limit.
134985    */
134986    pParse->nHeight += sqlite3SelectExprHeight(p);
134987
134988    /* Make copies of constant WHERE-clause terms in the outer query down
134989    ** inside the subquery.  This can help the subquery to run more efficiently.
134990    */
134991    if( OptimizationEnabled(db, SQLITE_PushDown)
134992     && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor,
134993                           (pItem->fg.jointype & JT_OUTER)!=0)
134994    ){
134995#if SELECTTRACE_ENABLED
134996      if( sqlite3SelectTrace & 0x100 ){
134997        SELECTTRACE(0x100,pParse,p,
134998            ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
134999        sqlite3TreeViewSelect(0, p, 0);
135000      }
135001#endif
135002    }else{
135003      SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
135004    }
135005
135006    zSavedAuthContext = pParse->zAuthContext;
135007    pParse->zAuthContext = pItem->zName;
135008
135009    /* Generate code to implement the subquery
135010    **
135011    ** The subquery is implemented as a co-routine if the subquery is
135012    ** guaranteed to be the outer loop (so that it does not need to be
135013    ** computed more than once)
135014    **
135015    ** TODO: Are there other reasons beside (1) to use a co-routine
135016    ** implementation?
135017    */
135018    if( i==0
135019     && (pTabList->nSrc==1
135020            || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)  /* (1) */
135021    ){
135022      /* Implement a co-routine that will return a single row of the result
135023      ** set on each invocation.
135024      */
135025      int addrTop = sqlite3VdbeCurrentAddr(v)+1;
135026
135027      pItem->regReturn = ++pParse->nMem;
135028      sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
135029      VdbeComment((v, "%s", pItem->pTab->zName));
135030      pItem->addrFillSub = addrTop;
135031      sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
135032      ExplainQueryPlan((pParse, 1, "CO-ROUTINE %u", pSub->selId));
135033      sqlite3Select(pParse, pSub, &dest);
135034      pItem->pTab->nRowLogEst = pSub->nSelectRow;
135035      pItem->fg.viaCoroutine = 1;
135036      pItem->regResult = dest.iSdst;
135037      sqlite3VdbeEndCoroutine(v, pItem->regReturn);
135038      sqlite3VdbeJumpHere(v, addrTop-1);
135039      sqlite3ClearTempRegCache(pParse);
135040    }else{
135041      /* Generate a subroutine that will fill an ephemeral table with
135042      ** the content of this subquery.  pItem->addrFillSub will point
135043      ** to the address of the generated subroutine.  pItem->regReturn
135044      ** is a register allocated to hold the subroutine return address
135045      */
135046      int topAddr;
135047      int onceAddr = 0;
135048      int retAddr;
135049      struct SrcList_item *pPrior;
135050
135051      testcase( pItem->addrFillSub==0 ); /* Ticket c52b09c7f38903b1311 */
135052      pItem->regReturn = ++pParse->nMem;
135053      topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
135054      pItem->addrFillSub = topAddr+1;
135055      if( pItem->fg.isCorrelated==0 ){
135056        /* If the subquery is not correlated and if we are not inside of
135057        ** a trigger, then we only need to compute the value of the subquery
135058        ** once. */
135059        onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
135060        VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
135061      }else{
135062        VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
135063      }
135064      pPrior = isSelfJoinView(pTabList, pItem);
135065      if( pPrior ){
135066        sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
135067        assert( pPrior->pSelect!=0 );
135068        pSub->nSelectRow = pPrior->pSelect->nSelectRow;
135069      }else{
135070        sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
135071        ExplainQueryPlan((pParse, 1, "MATERIALIZE %u", pSub->selId));
135072        sqlite3Select(pParse, pSub, &dest);
135073      }
135074      pItem->pTab->nRowLogEst = pSub->nSelectRow;
135075      if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
135076      retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
135077      VdbeComment((v, "end %s", pItem->pTab->zName));
135078      sqlite3VdbeChangeP1(v, topAddr, retAddr);
135079      sqlite3ClearTempRegCache(pParse);
135080    }
135081    if( db->mallocFailed ) goto select_end;
135082    pParse->nHeight -= sqlite3SelectExprHeight(p);
135083    pParse->zAuthContext = zSavedAuthContext;
135084#endif
135085  }
135086
135087  /* Various elements of the SELECT copied into local variables for
135088  ** convenience */
135089  pEList = p->pEList;
135090  pWhere = p->pWhere;
135091  pGroupBy = p->pGroupBy;
135092  pHaving = p->pHaving;
135093  sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
135094
135095#if SELECTTRACE_ENABLED
135096  if( sqlite3SelectTrace & 0x400 ){
135097    SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
135098    sqlite3TreeViewSelect(0, p, 0);
135099  }
135100#endif
135101
135102  /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
135103  ** if the select-list is the same as the ORDER BY list, then this query
135104  ** can be rewritten as a GROUP BY. In other words, this:
135105  **
135106  **     SELECT DISTINCT xyz FROM ... ORDER BY xyz
135107  **
135108  ** is transformed to:
135109  **
135110  **     SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
135111  **
135112  ** The second form is preferred as a single index (or temp-table) may be
135113  ** used for both the ORDER BY and DISTINCT processing. As originally
135114  ** written the query must use a temp-table for at least one of the ORDER
135115  ** BY and DISTINCT, and an index or separate temp-table for the other.
135116  */
135117  if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
135118   && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
135119#ifndef SQLITE_OMIT_WINDOWFUNC
135120   && p->pWin==0
135121#endif
135122  ){
135123    p->selFlags &= ~SF_Distinct;
135124    pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
135125    p->selFlags |= SF_Aggregate;
135126    /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
135127    ** the sDistinct.isTnct is still set.  Hence, isTnct represents the
135128    ** original setting of the SF_Distinct flag, not the current setting */
135129    assert( sDistinct.isTnct );
135130
135131#if SELECTTRACE_ENABLED
135132    if( sqlite3SelectTrace & 0x400 ){
135133      SELECTTRACE(0x400,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
135134      sqlite3TreeViewSelect(0, p, 0);
135135    }
135136#endif
135137  }
135138
135139  /* If there is an ORDER BY clause, then create an ephemeral index to
135140  ** do the sorting.  But this sorting ephemeral index might end up
135141  ** being unused if the data can be extracted in pre-sorted order.
135142  ** If that is the case, then the OP_OpenEphemeral instruction will be
135143  ** changed to an OP_Noop once we figure out that the sorting index is
135144  ** not needed.  The sSort.addrSortIndex variable is used to facilitate
135145  ** that change.
135146  */
135147  if( sSort.pOrderBy ){
135148    KeyInfo *pKeyInfo;
135149    pKeyInfo = sqlite3KeyInfoFromExprList(
135150        pParse, sSort.pOrderBy, 0, pEList->nExpr);
135151    sSort.iECursor = pParse->nTab++;
135152    sSort.addrSortIndex =
135153      sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
135154          sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
135155          (char*)pKeyInfo, P4_KEYINFO
135156      );
135157  }else{
135158    sSort.addrSortIndex = -1;
135159  }
135160
135161  /* If the output is destined for a temporary table, open that table.
135162  */
135163  if( pDest->eDest==SRT_EphemTab ){
135164    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
135165  }
135166
135167  /* Set the limiter.
135168  */
135169  iEnd = sqlite3VdbeMakeLabel(pParse);
135170  if( (p->selFlags & SF_FixedLimit)==0 ){
135171    p->nSelectRow = 320;  /* 4 billion rows */
135172  }
135173  computeLimitRegisters(pParse, p, iEnd);
135174  if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
135175    sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen);
135176    sSort.sortFlags |= SORTFLAG_UseSorter;
135177  }
135178
135179  /* Open an ephemeral index to use for the distinct set.
135180  */
135181  if( p->selFlags & SF_Distinct ){
135182    sDistinct.tabTnct = pParse->nTab++;
135183    sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
135184                       sDistinct.tabTnct, 0, 0,
135185                       (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
135186                       P4_KEYINFO);
135187    sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
135188    sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
135189  }else{
135190    sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
135191  }
135192
135193  if( !isAgg && pGroupBy==0 ){
135194    /* No aggregate functions and no GROUP BY clause */
135195    u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0)
135196                   | (p->selFlags & SF_FixedLimit);
135197#ifndef SQLITE_OMIT_WINDOWFUNC
135198    Window *pWin = p->pWin;      /* Master window object (or NULL) */
135199    if( pWin ){
135200      sqlite3WindowCodeInit(pParse, p);
135201    }
135202#endif
135203    assert( WHERE_USE_LIMIT==SF_FixedLimit );
135204
135205
135206    /* Begin the database scan. */
135207    SELECTTRACE(1,pParse,p,("WhereBegin\n"));
135208    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
135209                               p->pEList, wctrlFlags, p->nSelectRow);
135210    if( pWInfo==0 ) goto select_end;
135211    if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
135212      p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
135213    }
135214    if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
135215      sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
135216    }
135217    if( sSort.pOrderBy ){
135218      sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
135219      sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo);
135220      if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
135221        sSort.pOrderBy = 0;
135222      }
135223    }
135224
135225    /* If sorting index that was created by a prior OP_OpenEphemeral
135226    ** instruction ended up not being needed, then change the OP_OpenEphemeral
135227    ** into an OP_Noop.
135228    */
135229    if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
135230      sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
135231    }
135232
135233    assert( p->pEList==pEList );
135234#ifndef SQLITE_OMIT_WINDOWFUNC
135235    if( pWin ){
135236      int addrGosub = sqlite3VdbeMakeLabel(pParse);
135237      int iCont = sqlite3VdbeMakeLabel(pParse);
135238      int iBreak = sqlite3VdbeMakeLabel(pParse);
135239      int regGosub = ++pParse->nMem;
135240
135241      sqlite3WindowCodeStep(pParse, p, pWInfo, regGosub, addrGosub);
135242
135243      sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
135244      sqlite3VdbeResolveLabel(v, addrGosub);
135245      VdbeNoopComment((v, "inner-loop subroutine"));
135246      sSort.labelOBLopt = 0;
135247      selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
135248      sqlite3VdbeResolveLabel(v, iCont);
135249      sqlite3VdbeAddOp1(v, OP_Return, regGosub);
135250      VdbeComment((v, "end inner-loop subroutine"));
135251      sqlite3VdbeResolveLabel(v, iBreak);
135252    }else
135253#endif /* SQLITE_OMIT_WINDOWFUNC */
135254    {
135255      /* Use the standard inner loop. */
135256      selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
135257          sqlite3WhereContinueLabel(pWInfo),
135258          sqlite3WhereBreakLabel(pWInfo));
135259
135260      /* End the database scan loop.
135261      */
135262      sqlite3WhereEnd(pWInfo);
135263    }
135264  }else{
135265    /* This case when there exist aggregate functions or a GROUP BY clause
135266    ** or both */
135267    NameContext sNC;    /* Name context for processing aggregate information */
135268    int iAMem;          /* First Mem address for storing current GROUP BY */
135269    int iBMem;          /* First Mem address for previous GROUP BY */
135270    int iUseFlag;       /* Mem address holding flag indicating that at least
135271                        ** one row of the input to the aggregator has been
135272                        ** processed */
135273    int iAbortFlag;     /* Mem address which causes query abort if positive */
135274    int groupBySort;    /* Rows come from source in GROUP BY order */
135275    int addrEnd;        /* End of processing for this SELECT */
135276    int sortPTab = 0;   /* Pseudotable used to decode sorting results */
135277    int sortOut = 0;    /* Output register from the sorter */
135278    int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
135279
135280    /* Remove any and all aliases between the result set and the
135281    ** GROUP BY clause.
135282    */
135283    if( pGroupBy ){
135284      int k;                        /* Loop counter */
135285      struct ExprList_item *pItem;  /* For looping over expression in a list */
135286
135287      for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
135288        pItem->u.x.iAlias = 0;
135289      }
135290      for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
135291        pItem->u.x.iAlias = 0;
135292      }
135293      assert( 66==sqlite3LogEst(100) );
135294      if( p->nSelectRow>66 ) p->nSelectRow = 66;
135295
135296      /* If there is both a GROUP BY and an ORDER BY clause and they are
135297      ** identical, then it may be possible to disable the ORDER BY clause
135298      ** on the grounds that the GROUP BY will cause elements to come out
135299      ** in the correct order. It also may not - the GROUP BY might use a
135300      ** database index that causes rows to be grouped together as required
135301      ** but not actually sorted. Either way, record the fact that the
135302      ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
135303      ** variable.  */
135304      if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
135305        int ii;
135306        /* The GROUP BY processing doesn't care whether rows are delivered in
135307        ** ASC or DESC order - only that each group is returned contiguously.
135308        ** So set the ASC/DESC flags in the GROUP BY to match those in the
135309        ** ORDER BY to maximize the chances of rows being delivered in an
135310        ** order that makes the ORDER BY redundant.  */
135311        for(ii=0; ii<pGroupBy->nExpr; ii++){
135312          u8 sortFlags = sSort.pOrderBy->a[ii].sortFlags & KEYINFO_ORDER_DESC;
135313          pGroupBy->a[ii].sortFlags = sortFlags;
135314        }
135315        if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
135316          orderByGrp = 1;
135317        }
135318      }
135319    }else{
135320      assert( 0==sqlite3LogEst(1) );
135321      p->nSelectRow = 0;
135322    }
135323
135324    /* Create a label to jump to when we want to abort the query */
135325    addrEnd = sqlite3VdbeMakeLabel(pParse);
135326
135327    /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
135328    ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
135329    ** SELECT statement.
135330    */
135331    pAggInfo = sqlite3DbMallocZero(db, sizeof(*pAggInfo) );
135332    if( pAggInfo==0 ){
135333      goto select_end;
135334    }
135335    pAggInfo->pNext = pParse->pAggList;
135336    pParse->pAggList = pAggInfo;
135337    memset(&sNC, 0, sizeof(sNC));
135338    sNC.pParse = pParse;
135339    sNC.pSrcList = pTabList;
135340    sNC.uNC.pAggInfo = pAggInfo;
135341    VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
135342    pAggInfo->mnReg = pParse->nMem+1;
135343    pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
135344    pAggInfo->pGroupBy = pGroupBy;
135345    sqlite3ExprAnalyzeAggList(&sNC, pEList);
135346    sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
135347    if( pHaving ){
135348      if( pGroupBy ){
135349        assert( pWhere==p->pWhere );
135350        assert( pHaving==p->pHaving );
135351        assert( pGroupBy==p->pGroupBy );
135352        havingToWhere(pParse, p);
135353        pWhere = p->pWhere;
135354      }
135355      sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
135356    }
135357    pAggInfo->nAccumulator = pAggInfo->nColumn;
135358    if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
135359      minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pExpr, &pMinMaxOrderBy);
135360    }else{
135361      minMaxFlag = WHERE_ORDERBY_NORMAL;
135362    }
135363    for(i=0; i<pAggInfo->nFunc; i++){
135364      Expr *pExpr = pAggInfo->aFunc[i].pExpr;
135365      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
135366      sNC.ncFlags |= NC_InAggFunc;
135367      sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList);
135368#ifndef SQLITE_OMIT_WINDOWFUNC
135369      assert( !IsWindowFunc(pExpr) );
135370      if( ExprHasProperty(pExpr, EP_WinFunc) ){
135371        sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pWin->pFilter);
135372      }
135373#endif
135374      sNC.ncFlags &= ~NC_InAggFunc;
135375    }
135376    pAggInfo->mxReg = pParse->nMem;
135377    if( db->mallocFailed ) goto select_end;
135378#if SELECTTRACE_ENABLED
135379    if( sqlite3SelectTrace & 0x400 ){
135380      int ii;
135381      SELECTTRACE(0x400,pParse,p,("After aggregate analysis %p:\n", pAggInfo));
135382      sqlite3TreeViewSelect(0, p, 0);
135383      for(ii=0; ii<pAggInfo->nColumn; ii++){
135384        sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
135385            ii, pAggInfo->aCol[ii].iMem);
135386        sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pExpr, 0);
135387      }
135388      for(ii=0; ii<pAggInfo->nFunc; ii++){
135389        sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
135390            ii, pAggInfo->aFunc[ii].iMem);
135391        sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pExpr, 0);
135392      }
135393    }
135394#endif
135395
135396
135397    /* Processing for aggregates with GROUP BY is very different and
135398    ** much more complex than aggregates without a GROUP BY.
135399    */
135400    if( pGroupBy ){
135401      KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
135402      int addr1;          /* A-vs-B comparision jump */
135403      int addrOutputRow;  /* Start of subroutine that outputs a result row */
135404      int regOutputRow;   /* Return address register for output subroutine */
135405      int addrSetAbort;   /* Set the abort flag and return */
135406      int addrTopOfLoop;  /* Top of the input loop */
135407      int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
135408      int addrReset;      /* Subroutine for resetting the accumulator */
135409      int regReset;       /* Return address register for reset subroutine */
135410
135411      /* If there is a GROUP BY clause we might need a sorting index to
135412      ** implement it.  Allocate that sorting index now.  If it turns out
135413      ** that we do not need it after all, the OP_SorterOpen instruction
135414      ** will be converted into a Noop.
135415      */
135416      pAggInfo->sortingIdx = pParse->nTab++;
135417      pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pGroupBy,
135418                                            0, pAggInfo->nColumn);
135419      addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen,
135420          pAggInfo->sortingIdx, pAggInfo->nSortingColumn,
135421          0, (char*)pKeyInfo, P4_KEYINFO);
135422
135423      /* Initialize memory locations used by GROUP BY aggregate processing
135424      */
135425      iUseFlag = ++pParse->nMem;
135426      iAbortFlag = ++pParse->nMem;
135427      regOutputRow = ++pParse->nMem;
135428      addrOutputRow = sqlite3VdbeMakeLabel(pParse);
135429      regReset = ++pParse->nMem;
135430      addrReset = sqlite3VdbeMakeLabel(pParse);
135431      iAMem = pParse->nMem + 1;
135432      pParse->nMem += pGroupBy->nExpr;
135433      iBMem = pParse->nMem + 1;
135434      pParse->nMem += pGroupBy->nExpr;
135435      sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
135436      VdbeComment((v, "clear abort flag"));
135437      sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
135438
135439      /* Begin a loop that will extract all source rows in GROUP BY order.
135440      ** This might involve two separate loops with an OP_Sort in between, or
135441      ** it might be a single loop that uses an index to extract information
135442      ** in the right order to begin with.
135443      */
135444      sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
135445      SELECTTRACE(1,pParse,p,("WhereBegin\n"));
135446      pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
135447          WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0
135448      );
135449      if( pWInfo==0 ) goto select_end;
135450      if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
135451        /* The optimizer is able to deliver rows in group by order so
135452        ** we do not have to sort.  The OP_OpenEphemeral table will be
135453        ** cancelled later because we still need to use the pKeyInfo
135454        */
135455        groupBySort = 0;
135456      }else{
135457        /* Rows are coming out in undetermined order.  We have to push
135458        ** each row into a sorting index, terminate the first loop,
135459        ** then loop over the sorting index in order to get the output
135460        ** in sorted order
135461        */
135462        int regBase;
135463        int regRecord;
135464        int nCol;
135465        int nGroupBy;
135466
135467        explainTempTable(pParse,
135468            (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
135469                    "DISTINCT" : "GROUP BY");
135470
135471        groupBySort = 1;
135472        nGroupBy = pGroupBy->nExpr;
135473        nCol = nGroupBy;
135474        j = nGroupBy;
135475        for(i=0; i<pAggInfo->nColumn; i++){
135476          if( pAggInfo->aCol[i].iSorterColumn>=j ){
135477            nCol++;
135478            j++;
135479          }
135480        }
135481        regBase = sqlite3GetTempRange(pParse, nCol);
135482        sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
135483        j = nGroupBy;
135484        for(i=0; i<pAggInfo->nColumn; i++){
135485          struct AggInfo_col *pCol = &pAggInfo->aCol[i];
135486          if( pCol->iSorterColumn>=j ){
135487            int r1 = j + regBase;
135488            sqlite3ExprCodeGetColumnOfTable(v,
135489                               pCol->pTab, pCol->iTable, pCol->iColumn, r1);
135490            j++;
135491          }
135492        }
135493        regRecord = sqlite3GetTempReg(pParse);
135494        sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
135495        sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
135496        sqlite3ReleaseTempReg(pParse, regRecord);
135497        sqlite3ReleaseTempRange(pParse, regBase, nCol);
135498        sqlite3WhereEnd(pWInfo);
135499        pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
135500        sortOut = sqlite3GetTempReg(pParse);
135501        sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
135502        sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
135503        VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
135504        pAggInfo->useSortingIdx = 1;
135505      }
135506
135507      /* If the index or temporary table used by the GROUP BY sort
135508      ** will naturally deliver rows in the order required by the ORDER BY
135509      ** clause, cancel the ephemeral table open coded earlier.
135510      **
135511      ** This is an optimization - the correct answer should result regardless.
135512      ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
135513      ** disable this optimization for testing purposes.  */
135514      if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)
135515       && (groupBySort || sqlite3WhereIsSorted(pWInfo))
135516      ){
135517        sSort.pOrderBy = 0;
135518        sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
135519      }
135520
135521      /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
135522      ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
135523      ** Then compare the current GROUP BY terms against the GROUP BY terms
135524      ** from the previous row currently stored in a0, a1, a2...
135525      */
135526      addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
135527      if( groupBySort ){
135528        sqlite3VdbeAddOp3(v, OP_SorterData, pAggInfo->sortingIdx,
135529                          sortOut, sortPTab);
135530      }
135531      for(j=0; j<pGroupBy->nExpr; j++){
135532        if( groupBySort ){
135533          sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
135534        }else{
135535          pAggInfo->directMode = 1;
135536          sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
135537        }
135538      }
135539      sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
135540                          (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
135541      addr1 = sqlite3VdbeCurrentAddr(v);
135542      sqlite3VdbeAddOp3(v, OP_Jump, addr1+1, 0, addr1+1); VdbeCoverage(v);
135543
135544      /* Generate code that runs whenever the GROUP BY changes.
135545      ** Changes in the GROUP BY are detected by the previous code
135546      ** block.  If there were no changes, this block is skipped.
135547      **
135548      ** This code copies current group by terms in b0,b1,b2,...
135549      ** over to a0,a1,a2.  It then calls the output subroutine
135550      ** and resets the aggregate accumulator registers in preparation
135551      ** for the next GROUP BY batch.
135552      */
135553      sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
135554      sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
135555      VdbeComment((v, "output one row"));
135556      sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); VdbeCoverage(v);
135557      VdbeComment((v, "check abort flag"));
135558      sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
135559      VdbeComment((v, "reset accumulator"));
135560
135561      /* Update the aggregate accumulators based on the content of
135562      ** the current row
135563      */
135564      sqlite3VdbeJumpHere(v, addr1);
135565      updateAccumulator(pParse, iUseFlag, pAggInfo);
135566      sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
135567      VdbeComment((v, "indicate data in accumulator"));
135568
135569      /* End of the loop
135570      */
135571      if( groupBySort ){
135572        sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx, addrTopOfLoop);
135573        VdbeCoverage(v);
135574      }else{
135575        sqlite3WhereEnd(pWInfo);
135576        sqlite3VdbeChangeToNoop(v, addrSortingIdx);
135577      }
135578
135579      /* Output the final row of result
135580      */
135581      sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
135582      VdbeComment((v, "output final row"));
135583
135584      /* Jump over the subroutines
135585      */
135586      sqlite3VdbeGoto(v, addrEnd);
135587
135588      /* Generate a subroutine that outputs a single row of the result
135589      ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
135590      ** is less than or equal to zero, the subroutine is a no-op.  If
135591      ** the processing calls for the query to abort, this subroutine
135592      ** increments the iAbortFlag memory location before returning in
135593      ** order to signal the caller to abort.
135594      */
135595      addrSetAbort = sqlite3VdbeCurrentAddr(v);
135596      sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
135597      VdbeComment((v, "set abort flag"));
135598      sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
135599      sqlite3VdbeResolveLabel(v, addrOutputRow);
135600      addrOutputRow = sqlite3VdbeCurrentAddr(v);
135601      sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
135602      VdbeCoverage(v);
135603      VdbeComment((v, "Groupby result generator entry point"));
135604      sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
135605      finalizeAggFunctions(pParse, pAggInfo);
135606      sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
135607      selectInnerLoop(pParse, p, -1, &sSort,
135608                      &sDistinct, pDest,
135609                      addrOutputRow+1, addrSetAbort);
135610      sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
135611      VdbeComment((v, "end groupby result generator"));
135612
135613      /* Generate a subroutine that will reset the group-by accumulator
135614      */
135615      sqlite3VdbeResolveLabel(v, addrReset);
135616      resetAccumulator(pParse, pAggInfo);
135617      sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
135618      VdbeComment((v, "indicate accumulator empty"));
135619      sqlite3VdbeAddOp1(v, OP_Return, regReset);
135620
135621    } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
135622    else {
135623      Table *pTab;
135624      if( (pTab = isSimpleCount(p, pAggInfo))!=0 ){
135625        /* If isSimpleCount() returns a pointer to a Table structure, then
135626        ** the SQL statement is of the form:
135627        **
135628        **   SELECT count(*) FROM <tbl>
135629        **
135630        ** where the Table structure returned represents table <tbl>.
135631        **
135632        ** This statement is so common that it is optimized specially. The
135633        ** OP_Count instruction is executed either on the intkey table that
135634        ** contains the data for table <tbl> or on one of its indexes. It
135635        ** is better to execute the op on an index, as indexes are almost
135636        ** always spread across less pages than their corresponding tables.
135637        */
135638        const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
135639        const int iCsr = pParse->nTab++;     /* Cursor to scan b-tree */
135640        Index *pIdx;                         /* Iterator variable */
135641        KeyInfo *pKeyInfo = 0;               /* Keyinfo for scanned index */
135642        Index *pBest = 0;                    /* Best index found so far */
135643        int iRoot = pTab->tnum;              /* Root page of scanned b-tree */
135644
135645        sqlite3CodeVerifySchema(pParse, iDb);
135646        sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
135647
135648        /* Search for the index that has the lowest scan cost.
135649        **
135650        ** (2011-04-15) Do not do a full scan of an unordered index.
135651        **
135652        ** (2013-10-03) Do not count the entries in a partial index.
135653        **
135654        ** In practice the KeyInfo structure will not be used. It is only
135655        ** passed to keep OP_OpenRead happy.
135656        */
135657        if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab);
135658        if( !p->pSrc->a[0].fg.notIndexed ){
135659          for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
135660            if( pIdx->bUnordered==0
135661             && pIdx->szIdxRow<pTab->szTabRow
135662             && pIdx->pPartIdxWhere==0
135663             && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
135664            ){
135665              pBest = pIdx;
135666            }
135667          }
135668        }
135669        if( pBest ){
135670          iRoot = pBest->tnum;
135671          pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
135672        }
135673
135674        /* Open a read-only cursor, execute the OP_Count, close the cursor. */
135675        sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, iRoot, iDb, 1);
135676        if( pKeyInfo ){
135677          sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
135678        }
135679        sqlite3VdbeAddOp2(v, OP_Count, iCsr, pAggInfo->aFunc[0].iMem);
135680        sqlite3VdbeAddOp1(v, OP_Close, iCsr);
135681        explainSimpleCount(pParse, pTab, pBest);
135682      }else{
135683        int regAcc = 0;           /* "populate accumulators" flag */
135684
135685        /* If there are accumulator registers but no min() or max() functions
135686        ** without FILTER clauses, allocate register regAcc. Register regAcc
135687        ** will contain 0 the first time the inner loop runs, and 1 thereafter.
135688        ** The code generated by updateAccumulator() uses this to ensure
135689        ** that the accumulator registers are (a) updated only once if
135690        ** there are no min() or max functions or (b) always updated for the
135691        ** first row visited by the aggregate, so that they are updated at
135692        ** least once even if the FILTER clause means the min() or max()
135693        ** function visits zero rows.  */
135694        if( pAggInfo->nAccumulator ){
135695          for(i=0; i<pAggInfo->nFunc; i++){
135696            if( ExprHasProperty(pAggInfo->aFunc[i].pExpr, EP_WinFunc) ){
135697              continue;
135698            }
135699            if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ){
135700              break;
135701            }
135702          }
135703          if( i==pAggInfo->nFunc ){
135704            regAcc = ++pParse->nMem;
135705            sqlite3VdbeAddOp2(v, OP_Integer, 0, regAcc);
135706          }
135707        }
135708
135709        /* This case runs if the aggregate has no GROUP BY clause.  The
135710        ** processing is much simpler since there is only a single row
135711        ** of output.
135712        */
135713        assert( p->pGroupBy==0 );
135714        resetAccumulator(pParse, pAggInfo);
135715
135716        /* If this query is a candidate for the min/max optimization, then
135717        ** minMaxFlag will have been previously set to either
135718        ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
135719        ** be an appropriate ORDER BY expression for the optimization.
135720        */
135721        assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
135722        assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
135723
135724        SELECTTRACE(1,pParse,p,("WhereBegin\n"));
135725        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
135726                                   0, minMaxFlag, 0);
135727        if( pWInfo==0 ){
135728          goto select_end;
135729        }
135730        updateAccumulator(pParse, regAcc, pAggInfo);
135731        if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer, 1, regAcc);
135732        if( sqlite3WhereIsOrdered(pWInfo)>0 ){
135733          sqlite3VdbeGoto(v, sqlite3WhereBreakLabel(pWInfo));
135734          VdbeComment((v, "%s() by index",
135735                (minMaxFlag==WHERE_ORDERBY_MIN?"min":"max")));
135736        }
135737        sqlite3WhereEnd(pWInfo);
135738        finalizeAggFunctions(pParse, pAggInfo);
135739      }
135740
135741      sSort.pOrderBy = 0;
135742      sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
135743      selectInnerLoop(pParse, p, -1, 0, 0,
135744                      pDest, addrEnd, addrEnd);
135745    }
135746    sqlite3VdbeResolveLabel(v, addrEnd);
135747
135748  } /* endif aggregate query */
135749
135750  if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
135751    explainTempTable(pParse, "DISTINCT");
135752  }
135753
135754  /* If there is an ORDER BY clause, then we need to sort the results
135755  ** and send them to the callback one by one.
135756  */
135757  if( sSort.pOrderBy ){
135758    explainTempTable(pParse,
135759                     sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
135760    assert( p->pEList==pEList );
135761    generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
135762  }
135763
135764  /* Jump here to skip this query
135765  */
135766  sqlite3VdbeResolveLabel(v, iEnd);
135767
135768  /* The SELECT has been coded. If there is an error in the Parse structure,
135769  ** set the return code to 1. Otherwise 0. */
135770  rc = (pParse->nErr>0);
135771
135772  /* Control jumps to here if an error is encountered above, or upon
135773  ** successful coding of the SELECT.
135774  */
135775select_end:
135776  sqlite3ExprListDelete(db, pMinMaxOrderBy);
135777#ifdef SQLITE_DEBUG
135778  if( pAggInfo && !db->mallocFailed ){
135779    for(i=0; i<pAggInfo->nColumn; i++){
135780      Expr *pExpr = pAggInfo->aCol[i].pExpr;
135781      assert( pExpr!=0 || db->mallocFailed );
135782      if( pExpr==0 ) continue;
135783      assert( pExpr->pAggInfo==pAggInfo );
135784      assert( pExpr->iAgg==i );
135785    }
135786    for(i=0; i<pAggInfo->nFunc; i++){
135787      Expr *pExpr = pAggInfo->aFunc[i].pExpr;
135788      assert( pExpr!=0 || db->mallocFailed );
135789      if( pExpr==0 ) continue;
135790      assert( pExpr->pAggInfo==pAggInfo );
135791      assert( pExpr->iAgg==i );
135792    }
135793  }
135794#endif
135795
135796#if SELECTTRACE_ENABLED
135797  SELECTTRACE(0x1,pParse,p,("end processing\n"));
135798  if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
135799    sqlite3TreeViewSelect(0, p, 0);
135800  }
135801#endif
135802  ExplainQueryPlanPop(pParse);
135803  return rc;
135804}
135805
135806/************** End of select.c **********************************************/
135807/************** Begin file table.c *******************************************/
135808/*
135809** 2001 September 15
135810**
135811** The author disclaims copyright to this source code.  In place of
135812** a legal notice, here is a blessing:
135813**
135814**    May you do good and not evil.
135815**    May you find forgiveness for yourself and forgive others.
135816**    May you share freely, never taking more than you give.
135817**
135818*************************************************************************
135819** This file contains the sqlite3_get_table() and sqlite3_free_table()
135820** interface routines.  These are just wrappers around the main
135821** interface routine of sqlite3_exec().
135822**
135823** These routines are in a separate files so that they will not be linked
135824** if they are not used.
135825*/
135826/* #include "sqliteInt.h" */
135827
135828#ifndef SQLITE_OMIT_GET_TABLE
135829
135830/*
135831** This structure is used to pass data from sqlite3_get_table() through
135832** to the callback function is uses to build the result.
135833*/
135834typedef struct TabResult {
135835  char **azResult;   /* Accumulated output */
135836  char *zErrMsg;     /* Error message text, if an error occurs */
135837  u32 nAlloc;        /* Slots allocated for azResult[] */
135838  u32 nRow;          /* Number of rows in the result */
135839  u32 nColumn;       /* Number of columns in the result */
135840  u32 nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
135841  int rc;            /* Return code from sqlite3_exec() */
135842} TabResult;
135843
135844/*
135845** This routine is called once for each row in the result table.  Its job
135846** is to fill in the TabResult structure appropriately, allocating new
135847** memory as necessary.
135848*/
135849static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
135850  TabResult *p = (TabResult*)pArg;  /* Result accumulator */
135851  int need;                         /* Slots needed in p->azResult[] */
135852  int i;                            /* Loop counter */
135853  char *z;                          /* A single column of result */
135854
135855  /* Make sure there is enough space in p->azResult to hold everything
135856  ** we need to remember from this invocation of the callback.
135857  */
135858  if( p->nRow==0 && argv!=0 ){
135859    need = nCol*2;
135860  }else{
135861    need = nCol;
135862  }
135863  if( p->nData + need > p->nAlloc ){
135864    char **azNew;
135865    p->nAlloc = p->nAlloc*2 + need;
135866    azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc );
135867    if( azNew==0 ) goto malloc_failed;
135868    p->azResult = azNew;
135869  }
135870
135871  /* If this is the first row, then generate an extra row containing
135872  ** the names of all columns.
135873  */
135874  if( p->nRow==0 ){
135875    p->nColumn = nCol;
135876    for(i=0; i<nCol; i++){
135877      z = sqlite3_mprintf("%s", colv[i]);
135878      if( z==0 ) goto malloc_failed;
135879      p->azResult[p->nData++] = z;
135880    }
135881  }else if( (int)p->nColumn!=nCol ){
135882    sqlite3_free(p->zErrMsg);
135883    p->zErrMsg = sqlite3_mprintf(
135884       "sqlite3_get_table() called with two or more incompatible queries"
135885    );
135886    p->rc = SQLITE_ERROR;
135887    return 1;
135888  }
135889
135890  /* Copy over the row data
135891  */
135892  if( argv!=0 ){
135893    for(i=0; i<nCol; i++){
135894      if( argv[i]==0 ){
135895        z = 0;
135896      }else{
135897        int n = sqlite3Strlen30(argv[i])+1;
135898        z = sqlite3_malloc64( n );
135899        if( z==0 ) goto malloc_failed;
135900        memcpy(z, argv[i], n);
135901      }
135902      p->azResult[p->nData++] = z;
135903    }
135904    p->nRow++;
135905  }
135906  return 0;
135907
135908malloc_failed:
135909  p->rc = SQLITE_NOMEM_BKPT;
135910  return 1;
135911}
135912
135913/*
135914** Query the database.  But instead of invoking a callback for each row,
135915** malloc() for space to hold the result and return the entire results
135916** at the conclusion of the call.
135917**
135918** The result that is written to ***pazResult is held in memory obtained
135919** from malloc().  But the caller cannot free this memory directly.
135920** Instead, the entire table should be passed to sqlite3_free_table() when
135921** the calling procedure is finished using it.
135922*/
135923SQLITE_API int sqlite3_get_table(
135924  sqlite3 *db,                /* The database on which the SQL executes */
135925  const char *zSql,           /* The SQL to be executed */
135926  char ***pazResult,          /* Write the result table here */
135927  int *pnRow,                 /* Write the number of rows in the result here */
135928  int *pnColumn,              /* Write the number of columns of result here */
135929  char **pzErrMsg             /* Write error messages here */
135930){
135931  int rc;
135932  TabResult res;
135933
135934#ifdef SQLITE_ENABLE_API_ARMOR
135935  if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPT;
135936#endif
135937  *pazResult = 0;
135938  if( pnColumn ) *pnColumn = 0;
135939  if( pnRow ) *pnRow = 0;
135940  if( pzErrMsg ) *pzErrMsg = 0;
135941  res.zErrMsg = 0;
135942  res.nRow = 0;
135943  res.nColumn = 0;
135944  res.nData = 1;
135945  res.nAlloc = 20;
135946  res.rc = SQLITE_OK;
135947  res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
135948  if( res.azResult==0 ){
135949     db->errCode = SQLITE_NOMEM;
135950     return SQLITE_NOMEM_BKPT;
135951  }
135952  res.azResult[0] = 0;
135953  rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
135954  assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
135955  res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
135956  if( (rc&0xff)==SQLITE_ABORT ){
135957    sqlite3_free_table(&res.azResult[1]);
135958    if( res.zErrMsg ){
135959      if( pzErrMsg ){
135960        sqlite3_free(*pzErrMsg);
135961        *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
135962      }
135963      sqlite3_free(res.zErrMsg);
135964    }
135965    db->errCode = res.rc;  /* Assume 32-bit assignment is atomic */
135966    return res.rc;
135967  }
135968  sqlite3_free(res.zErrMsg);
135969  if( rc!=SQLITE_OK ){
135970    sqlite3_free_table(&res.azResult[1]);
135971    return rc;
135972  }
135973  if( res.nAlloc>res.nData ){
135974    char **azNew;
135975    azNew = sqlite3Realloc( res.azResult, sizeof(char*)*res.nData );
135976    if( azNew==0 ){
135977      sqlite3_free_table(&res.azResult[1]);
135978      db->errCode = SQLITE_NOMEM;
135979      return SQLITE_NOMEM_BKPT;
135980    }
135981    res.azResult = azNew;
135982  }
135983  *pazResult = &res.azResult[1];
135984  if( pnColumn ) *pnColumn = res.nColumn;
135985  if( pnRow ) *pnRow = res.nRow;
135986  return rc;
135987}
135988
135989/*
135990** This routine frees the space the sqlite3_get_table() malloced.
135991*/
135992SQLITE_API void sqlite3_free_table(
135993  char **azResult            /* Result returned from sqlite3_get_table() */
135994){
135995  if( azResult ){
135996    int i, n;
135997    azResult--;
135998    assert( azResult!=0 );
135999    n = SQLITE_PTR_TO_INT(azResult[0]);
136000    for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
136001    sqlite3_free(azResult);
136002  }
136003}
136004
136005#endif /* SQLITE_OMIT_GET_TABLE */
136006
136007/************** End of table.c ***********************************************/
136008/************** Begin file trigger.c *****************************************/
136009/*
136010**
136011** The author disclaims copyright to this source code.  In place of
136012** a legal notice, here is a blessing:
136013**
136014**    May you do good and not evil.
136015**    May you find forgiveness for yourself and forgive others.
136016**    May you share freely, never taking more than you give.
136017**
136018*************************************************************************
136019** This file contains the implementation for TRIGGERs
136020*/
136021/* #include "sqliteInt.h" */
136022
136023#ifndef SQLITE_OMIT_TRIGGER
136024/*
136025** Delete a linked list of TriggerStep structures.
136026*/
136027SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
136028  while( pTriggerStep ){
136029    TriggerStep * pTmp = pTriggerStep;
136030    pTriggerStep = pTriggerStep->pNext;
136031
136032    sqlite3ExprDelete(db, pTmp->pWhere);
136033    sqlite3ExprListDelete(db, pTmp->pExprList);
136034    sqlite3SelectDelete(db, pTmp->pSelect);
136035    sqlite3IdListDelete(db, pTmp->pIdList);
136036    sqlite3UpsertDelete(db, pTmp->pUpsert);
136037    sqlite3DbFree(db, pTmp->zSpan);
136038
136039    sqlite3DbFree(db, pTmp);
136040  }
136041}
136042
136043/*
136044** Given table pTab, return a list of all the triggers attached to
136045** the table. The list is connected by Trigger.pNext pointers.
136046**
136047** All of the triggers on pTab that are in the same database as pTab
136048** are already attached to pTab->pTrigger.  But there might be additional
136049** triggers on pTab in the TEMP schema.  This routine prepends all
136050** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
136051** and returns the combined list.
136052**
136053** To state it another way:  This routine returns a list of all triggers
136054** that fire off of pTab.  The list will include any TEMP triggers on
136055** pTab as well as the triggers lised in pTab->pTrigger.
136056*/
136057SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
136058  Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
136059  Trigger *pList = 0;                  /* List of triggers to return */
136060
136061  if( pParse->disableTriggers ){
136062    return 0;
136063  }
136064
136065  if( pTmpSchema!=pTab->pSchema ){
136066    HashElem *p;
136067    assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
136068    for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
136069      Trigger *pTrig = (Trigger *)sqliteHashData(p);
136070      if( pTrig->pTabSchema==pTab->pSchema
136071       && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
136072      ){
136073        pTrig->pNext = (pList ? pList : pTab->pTrigger);
136074        pList = pTrig;
136075      }
136076    }
136077  }
136078
136079  return (pList ? pList : pTab->pTrigger);
136080}
136081
136082/*
136083** This is called by the parser when it sees a CREATE TRIGGER statement
136084** up to the point of the BEGIN before the trigger actions.  A Trigger
136085** structure is generated based on the information available and stored
136086** in pParse->pNewTrigger.  After the trigger actions have been parsed, the
136087** sqlite3FinishTrigger() function is called to complete the trigger
136088** construction process.
136089*/
136090SQLITE_PRIVATE void sqlite3BeginTrigger(
136091  Parse *pParse,      /* The parse context of the CREATE TRIGGER statement */
136092  Token *pName1,      /* The name of the trigger */
136093  Token *pName2,      /* The name of the trigger */
136094  int tr_tm,          /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
136095  int op,             /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
136096  IdList *pColumns,   /* column list if this is an UPDATE OF trigger */
136097  SrcList *pTableName,/* The name of the table/view the trigger applies to */
136098  Expr *pWhen,        /* WHEN clause */
136099  int isTemp,         /* True if the TEMPORARY keyword is present */
136100  int noErr           /* Suppress errors if the trigger already exists */
136101){
136102  Trigger *pTrigger = 0;  /* The new trigger */
136103  Table *pTab;            /* Table that the trigger fires off of */
136104  char *zName = 0;        /* Name of the trigger */
136105  sqlite3 *db = pParse->db;  /* The database connection */
136106  int iDb;                /* The database to store the trigger in */
136107  Token *pName;           /* The unqualified db name */
136108  DbFixer sFix;           /* State vector for the DB fixer */
136109
136110  assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
136111  assert( pName2!=0 );
136112  assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
136113  assert( op>0 && op<0xff );
136114  if( isTemp ){
136115    /* If TEMP was specified, then the trigger name may not be qualified. */
136116    if( pName2->n>0 ){
136117      sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
136118      goto trigger_cleanup;
136119    }
136120    iDb = 1;
136121    pName = pName1;
136122  }else{
136123    /* Figure out the db that the trigger will be created in */
136124    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
136125    if( iDb<0 ){
136126      goto trigger_cleanup;
136127    }
136128  }
136129  if( !pTableName || db->mallocFailed ){
136130    goto trigger_cleanup;
136131  }
136132
136133  /* A long-standing parser bug is that this syntax was allowed:
136134  **
136135  **    CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
136136  **                                                 ^^^^^^^^
136137  **
136138  ** To maintain backwards compatibility, ignore the database
136139  ** name on pTableName if we are reparsing out of SQLITE_MASTER.
136140  */
136141  if( db->init.busy && iDb!=1 ){
136142    sqlite3DbFree(db, pTableName->a[0].zDatabase);
136143    pTableName->a[0].zDatabase = 0;
136144  }
136145
136146  /* If the trigger name was unqualified, and the table is a temp table,
136147  ** then set iDb to 1 to create the trigger in the temporary database.
136148  ** If sqlite3SrcListLookup() returns 0, indicating the table does not
136149  ** exist, the error is caught by the block below.
136150  */
136151  pTab = sqlite3SrcListLookup(pParse, pTableName);
136152  if( db->init.busy==0 && pName2->n==0 && pTab
136153        && pTab->pSchema==db->aDb[1].pSchema ){
136154    iDb = 1;
136155  }
136156
136157  /* Ensure the table name matches database name and that the table exists */
136158  if( db->mallocFailed ) goto trigger_cleanup;
136159  assert( pTableName->nSrc==1 );
136160  sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
136161  if( sqlite3FixSrcList(&sFix, pTableName) ){
136162    goto trigger_cleanup;
136163  }
136164  pTab = sqlite3SrcListLookup(pParse, pTableName);
136165  if( !pTab ){
136166    /* The table does not exist. */
136167    if( db->init.iDb==1 ){
136168      /* Ticket #3810.
136169      ** Normally, whenever a table is dropped, all associated triggers are
136170      ** dropped too.  But if a TEMP trigger is created on a non-TEMP table
136171      ** and the table is dropped by a different database connection, the
136172      ** trigger is not visible to the database connection that does the
136173      ** drop so the trigger cannot be dropped.  This results in an
136174      ** "orphaned trigger" - a trigger whose associated table is missing.
136175      */
136176      db->init.orphanTrigger = 1;
136177    }
136178    goto trigger_cleanup;
136179  }
136180  if( IsVirtual(pTab) ){
136181    sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
136182    goto trigger_cleanup;
136183  }
136184
136185  /* Check that the trigger name is not reserved and that no trigger of the
136186  ** specified name exists */
136187  zName = sqlite3NameFromToken(db, pName);
136188  if( zName==0 ){
136189    assert( db->mallocFailed );
136190    goto trigger_cleanup;
136191  }
136192  if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
136193    goto trigger_cleanup;
136194  }
136195  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
136196  if( !IN_RENAME_OBJECT ){
136197    if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
136198      if( !noErr ){
136199        sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
136200      }else{
136201        assert( !db->init.busy );
136202        sqlite3CodeVerifySchema(pParse, iDb);
136203      }
136204      goto trigger_cleanup;
136205    }
136206  }
136207
136208  /* Do not create a trigger on a system table */
136209  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
136210    sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
136211    goto trigger_cleanup;
136212  }
136213
136214  /* INSTEAD of triggers are only for views and views only support INSTEAD
136215  ** of triggers.
136216  */
136217  if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
136218    sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
136219        (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
136220    goto trigger_cleanup;
136221  }
136222  if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
136223    sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
136224        " trigger on table: %S", pTableName, 0);
136225    goto trigger_cleanup;
136226  }
136227
136228#ifndef SQLITE_OMIT_AUTHORIZATION
136229  if( !IN_RENAME_OBJECT ){
136230    int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
136231    int code = SQLITE_CREATE_TRIGGER;
136232    const char *zDb = db->aDb[iTabDb].zDbSName;
136233    const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
136234    if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
136235    if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
136236      goto trigger_cleanup;
136237    }
136238    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
136239      goto trigger_cleanup;
136240    }
136241  }
136242#endif
136243
136244  /* INSTEAD OF triggers can only appear on views and BEFORE triggers
136245  ** cannot appear on views.  So we might as well translate every
136246  ** INSTEAD OF trigger into a BEFORE trigger.  It simplifies code
136247  ** elsewhere.
136248  */
136249  if (tr_tm == TK_INSTEAD){
136250    tr_tm = TK_BEFORE;
136251  }
136252
136253  /* Build the Trigger object */
136254  pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
136255  if( pTrigger==0 ) goto trigger_cleanup;
136256  pTrigger->zName = zName;
136257  zName = 0;
136258  pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
136259  pTrigger->pSchema = db->aDb[iDb].pSchema;
136260  pTrigger->pTabSchema = pTab->pSchema;
136261  pTrigger->op = (u8)op;
136262  pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
136263  if( IN_RENAME_OBJECT ){
136264    sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
136265    pTrigger->pWhen = pWhen;
136266    pWhen = 0;
136267  }else{
136268    pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
136269  }
136270  pTrigger->pColumns = pColumns;
136271  pColumns = 0;
136272  assert( pParse->pNewTrigger==0 );
136273  pParse->pNewTrigger = pTrigger;
136274
136275trigger_cleanup:
136276  sqlite3DbFree(db, zName);
136277  sqlite3SrcListDelete(db, pTableName);
136278  sqlite3IdListDelete(db, pColumns);
136279  sqlite3ExprDelete(db, pWhen);
136280  if( !pParse->pNewTrigger ){
136281    sqlite3DeleteTrigger(db, pTrigger);
136282  }else{
136283    assert( pParse->pNewTrigger==pTrigger );
136284  }
136285}
136286
136287/*
136288** This routine is called after all of the trigger actions have been parsed
136289** in order to complete the process of building the trigger.
136290*/
136291SQLITE_PRIVATE void sqlite3FinishTrigger(
136292  Parse *pParse,          /* Parser context */
136293  TriggerStep *pStepList, /* The triggered program */
136294  Token *pAll             /* Token that describes the complete CREATE TRIGGER */
136295){
136296  Trigger *pTrig = pParse->pNewTrigger;   /* Trigger being finished */
136297  char *zName;                            /* Name of trigger */
136298  sqlite3 *db = pParse->db;               /* The database */
136299  DbFixer sFix;                           /* Fixer object */
136300  int iDb;                                /* Database containing the trigger */
136301  Token nameToken;                        /* Trigger name for error reporting */
136302
136303  pParse->pNewTrigger = 0;
136304  if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
136305  zName = pTrig->zName;
136306  iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
136307  pTrig->step_list = pStepList;
136308  while( pStepList ){
136309    pStepList->pTrig = pTrig;
136310    pStepList = pStepList->pNext;
136311  }
136312  sqlite3TokenInit(&nameToken, pTrig->zName);
136313  sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
136314  if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
136315   || sqlite3FixExpr(&sFix, pTrig->pWhen)
136316  ){
136317    goto triggerfinish_cleanup;
136318  }
136319
136320#ifndef SQLITE_OMIT_ALTERTABLE
136321  if( IN_RENAME_OBJECT ){
136322    assert( !db->init.busy );
136323    pParse->pNewTrigger = pTrig;
136324    pTrig = 0;
136325  }else
136326#endif
136327
136328  /* if we are not initializing,
136329  ** build the sqlite_master entry
136330  */
136331  if( !db->init.busy ){
136332    Vdbe *v;
136333    char *z;
136334
136335    /* Make an entry in the sqlite_master table */
136336    v = sqlite3GetVdbe(pParse);
136337    if( v==0 ) goto triggerfinish_cleanup;
136338    sqlite3BeginWriteOperation(pParse, 0, iDb);
136339    z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
136340    testcase( z==0 );
136341    sqlite3NestedParse(pParse,
136342       "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
136343       db->aDb[iDb].zDbSName, MASTER_NAME, zName,
136344       pTrig->table, z);
136345    sqlite3DbFree(db, z);
136346    sqlite3ChangeCookie(pParse, iDb);
136347    sqlite3VdbeAddParseSchemaOp(v, iDb,
136348        sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
136349  }
136350
136351  if( db->init.busy ){
136352    Trigger *pLink = pTrig;
136353    Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
136354    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
136355    assert( pLink!=0 );
136356    pTrig = sqlite3HashInsert(pHash, zName, pTrig);
136357    if( pTrig ){
136358      sqlite3OomFault(db);
136359    }else if( pLink->pSchema==pLink->pTabSchema ){
136360      Table *pTab;
136361      pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
136362      assert( pTab!=0 );
136363      pLink->pNext = pTab->pTrigger;
136364      pTab->pTrigger = pLink;
136365    }
136366  }
136367
136368triggerfinish_cleanup:
136369  sqlite3DeleteTrigger(db, pTrig);
136370  assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
136371  sqlite3DeleteTriggerStep(db, pStepList);
136372}
136373
136374/*
136375** Duplicate a range of text from an SQL statement, then convert all
136376** whitespace characters into ordinary space characters.
136377*/
136378static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
136379  char *z = sqlite3DbSpanDup(db, zStart, zEnd);
136380  int i;
136381  if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
136382  return z;
136383}
136384
136385/*
136386** Turn a SELECT statement (that the pSelect parameter points to) into
136387** a trigger step.  Return a pointer to a TriggerStep structure.
136388**
136389** The parser calls this routine when it finds a SELECT statement in
136390** body of a TRIGGER.
136391*/
136392SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(
136393  sqlite3 *db,                /* Database connection */
136394  Select *pSelect,            /* The SELECT statement */
136395  const char *zStart,         /* Start of SQL text */
136396  const char *zEnd            /* End of SQL text */
136397){
136398  TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
136399  if( pTriggerStep==0 ) {
136400    sqlite3SelectDelete(db, pSelect);
136401    return 0;
136402  }
136403  pTriggerStep->op = TK_SELECT;
136404  pTriggerStep->pSelect = pSelect;
136405  pTriggerStep->orconf = OE_Default;
136406  pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
136407  return pTriggerStep;
136408}
136409
136410/*
136411** Allocate space to hold a new trigger step.  The allocated space
136412** holds both the TriggerStep object and the TriggerStep.target.z string.
136413**
136414** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
136415*/
136416static TriggerStep *triggerStepAllocate(
136417  Parse *pParse,              /* Parser context */
136418  u8 op,                      /* Trigger opcode */
136419  Token *pName,               /* The target name */
136420  const char *zStart,         /* Start of SQL text */
136421  const char *zEnd            /* End of SQL text */
136422){
136423  sqlite3 *db = pParse->db;
136424  TriggerStep *pTriggerStep;
136425
136426  pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
136427  if( pTriggerStep ){
136428    char *z = (char*)&pTriggerStep[1];
136429    memcpy(z, pName->z, pName->n);
136430    sqlite3Dequote(z);
136431    pTriggerStep->zTarget = z;
136432    pTriggerStep->op = op;
136433    pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
136434    if( IN_RENAME_OBJECT ){
136435      sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
136436    }
136437  }
136438  return pTriggerStep;
136439}
136440
136441/*
136442** Build a trigger step out of an INSERT statement.  Return a pointer
136443** to the new trigger step.
136444**
136445** The parser calls this routine when it sees an INSERT inside the
136446** body of a trigger.
136447*/
136448SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
136449  Parse *pParse,      /* Parser */
136450  Token *pTableName,  /* Name of the table into which we insert */
136451  IdList *pColumn,    /* List of columns in pTableName to insert into */
136452  Select *pSelect,    /* A SELECT statement that supplies values */
136453  u8 orconf,          /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
136454  Upsert *pUpsert,    /* ON CONFLICT clauses for upsert */
136455  const char *zStart, /* Start of SQL text */
136456  const char *zEnd    /* End of SQL text */
136457){
136458  sqlite3 *db = pParse->db;
136459  TriggerStep *pTriggerStep;
136460
136461  assert(pSelect != 0 || db->mallocFailed);
136462
136463  pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd);
136464  if( pTriggerStep ){
136465    if( IN_RENAME_OBJECT ){
136466      pTriggerStep->pSelect = pSelect;
136467      pSelect = 0;
136468    }else{
136469      pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
136470    }
136471    pTriggerStep->pIdList = pColumn;
136472    pTriggerStep->pUpsert = pUpsert;
136473    pTriggerStep->orconf = orconf;
136474    if( pUpsert ){
136475      sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
136476    }
136477  }else{
136478    testcase( pColumn );
136479    sqlite3IdListDelete(db, pColumn);
136480    testcase( pUpsert );
136481    sqlite3UpsertDelete(db, pUpsert);
136482  }
136483  sqlite3SelectDelete(db, pSelect);
136484
136485  return pTriggerStep;
136486}
136487
136488/*
136489** Construct a trigger step that implements an UPDATE statement and return
136490** a pointer to that trigger step.  The parser calls this routine when it
136491** sees an UPDATE statement inside the body of a CREATE TRIGGER.
136492*/
136493SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
136494  Parse *pParse,          /* Parser */
136495  Token *pTableName,   /* Name of the table to be updated */
136496  ExprList *pEList,    /* The SET clause: list of column and new values */
136497  Expr *pWhere,        /* The WHERE clause */
136498  u8 orconf,           /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
136499  const char *zStart,  /* Start of SQL text */
136500  const char *zEnd     /* End of SQL text */
136501){
136502  sqlite3 *db = pParse->db;
136503  TriggerStep *pTriggerStep;
136504
136505  pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd);
136506  if( pTriggerStep ){
136507    if( IN_RENAME_OBJECT ){
136508      pTriggerStep->pExprList = pEList;
136509      pTriggerStep->pWhere = pWhere;
136510      pEList = 0;
136511      pWhere = 0;
136512    }else{
136513      pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
136514      pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
136515    }
136516    pTriggerStep->orconf = orconf;
136517  }
136518  sqlite3ExprListDelete(db, pEList);
136519  sqlite3ExprDelete(db, pWhere);
136520  return pTriggerStep;
136521}
136522
136523/*
136524** Construct a trigger step that implements a DELETE statement and return
136525** a pointer to that trigger step.  The parser calls this routine when it
136526** sees a DELETE statement inside the body of a CREATE TRIGGER.
136527*/
136528SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
136529  Parse *pParse,          /* Parser */
136530  Token *pTableName,      /* The table from which rows are deleted */
136531  Expr *pWhere,           /* The WHERE clause */
136532  const char *zStart,     /* Start of SQL text */
136533  const char *zEnd        /* End of SQL text */
136534){
136535  sqlite3 *db = pParse->db;
136536  TriggerStep *pTriggerStep;
136537
136538  pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd);
136539  if( pTriggerStep ){
136540    if( IN_RENAME_OBJECT ){
136541      pTriggerStep->pWhere = pWhere;
136542      pWhere = 0;
136543    }else{
136544      pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
136545    }
136546    pTriggerStep->orconf = OE_Default;
136547  }
136548  sqlite3ExprDelete(db, pWhere);
136549  return pTriggerStep;
136550}
136551
136552/*
136553** Recursively delete a Trigger structure
136554*/
136555SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
136556  if( pTrigger==0 ) return;
136557  sqlite3DeleteTriggerStep(db, pTrigger->step_list);
136558  sqlite3DbFree(db, pTrigger->zName);
136559  sqlite3DbFree(db, pTrigger->table);
136560  sqlite3ExprDelete(db, pTrigger->pWhen);
136561  sqlite3IdListDelete(db, pTrigger->pColumns);
136562  sqlite3DbFree(db, pTrigger);
136563}
136564
136565/*
136566** This function is called to drop a trigger from the database schema.
136567**
136568** This may be called directly from the parser and therefore identifies
136569** the trigger by name.  The sqlite3DropTriggerPtr() routine does the
136570** same job as this routine except it takes a pointer to the trigger
136571** instead of the trigger name.
136572**/
136573SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
136574  Trigger *pTrigger = 0;
136575  int i;
136576  const char *zDb;
136577  const char *zName;
136578  sqlite3 *db = pParse->db;
136579
136580  if( db->mallocFailed ) goto drop_trigger_cleanup;
136581  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
136582    goto drop_trigger_cleanup;
136583  }
136584
136585  assert( pName->nSrc==1 );
136586  zDb = pName->a[0].zDatabase;
136587  zName = pName->a[0].zName;
136588  assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
136589  for(i=OMIT_TEMPDB; i<db->nDb; i++){
136590    int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
136591    if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
136592    assert( sqlite3SchemaMutexHeld(db, j, 0) );
136593    pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
136594    if( pTrigger ) break;
136595  }
136596  if( !pTrigger ){
136597    if( !noErr ){
136598      sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
136599    }else{
136600      sqlite3CodeVerifyNamedSchema(pParse, zDb);
136601    }
136602    pParse->checkSchema = 1;
136603    goto drop_trigger_cleanup;
136604  }
136605  sqlite3DropTriggerPtr(pParse, pTrigger);
136606
136607drop_trigger_cleanup:
136608  sqlite3SrcListDelete(db, pName);
136609}
136610
136611/*
136612** Return a pointer to the Table structure for the table that a trigger
136613** is set on.
136614*/
136615static Table *tableOfTrigger(Trigger *pTrigger){
136616  return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
136617}
136618
136619
136620/*
136621** Drop a trigger given a pointer to that trigger.
136622*/
136623SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
136624  Table   *pTable;
136625  Vdbe *v;
136626  sqlite3 *db = pParse->db;
136627  int iDb;
136628
136629  iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
136630  assert( iDb>=0 && iDb<db->nDb );
136631  pTable = tableOfTrigger(pTrigger);
136632  assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
136633#ifndef SQLITE_OMIT_AUTHORIZATION
136634  if( pTable ){
136635    int code = SQLITE_DROP_TRIGGER;
136636    const char *zDb = db->aDb[iDb].zDbSName;
136637    const char *zTab = SCHEMA_TABLE(iDb);
136638    if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
136639    if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
136640      sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
136641      return;
136642    }
136643  }
136644#endif
136645
136646  /* Generate code to destroy the database record of the trigger.
136647  */
136648  if( (v = sqlite3GetVdbe(pParse))!=0 ){
136649    sqlite3NestedParse(pParse,
136650       "DELETE FROM %Q.%s WHERE name=%Q AND type='trigger'",
136651       db->aDb[iDb].zDbSName, MASTER_NAME, pTrigger->zName
136652    );
136653    sqlite3ChangeCookie(pParse, iDb);
136654    sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
136655  }
136656}
136657
136658/*
136659** Remove a trigger from the hash tables of the sqlite* pointer.
136660*/
136661SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
136662  Trigger *pTrigger;
136663  Hash *pHash;
136664
136665  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
136666  pHash = &(db->aDb[iDb].pSchema->trigHash);
136667  pTrigger = sqlite3HashInsert(pHash, zName, 0);
136668  if( ALWAYS(pTrigger) ){
136669    if( pTrigger->pSchema==pTrigger->pTabSchema ){
136670      Table *pTab = tableOfTrigger(pTrigger);
136671      if( pTab ){
136672        Trigger **pp;
136673        for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
136674          if( *pp==pTrigger ){
136675            *pp = (*pp)->pNext;
136676            break;
136677          }
136678        }
136679      }
136680    }
136681    sqlite3DeleteTrigger(db, pTrigger);
136682    db->mDbFlags |= DBFLAG_SchemaChange;
136683  }
136684}
136685
136686/*
136687** pEList is the SET clause of an UPDATE statement.  Each entry
136688** in pEList is of the format <id>=<expr>.  If any of the entries
136689** in pEList have an <id> which matches an identifier in pIdList,
136690** then return TRUE.  If pIdList==NULL, then it is considered a
136691** wildcard that matches anything.  Likewise if pEList==NULL then
136692** it matches anything so always return true.  Return false only
136693** if there is no match.
136694*/
136695static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
136696  int e;
136697  if( pIdList==0 || NEVER(pEList==0) ) return 1;
136698  for(e=0; e<pEList->nExpr; e++){
136699    if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
136700  }
136701  return 0;
136702}
136703
136704/*
136705** Return a list of all triggers on table pTab if there exists at least
136706** one trigger that must be fired when an operation of type 'op' is
136707** performed on the table, and, if that operation is an UPDATE, if at
136708** least one of the columns in pChanges is being modified.
136709*/
136710SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
136711  Parse *pParse,          /* Parse context */
136712  Table *pTab,            /* The table the contains the triggers */
136713  int op,                 /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
136714  ExprList *pChanges,     /* Columns that change in an UPDATE statement */
136715  int *pMask              /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
136716){
136717  int mask = 0;
136718  Trigger *pList = 0;
136719  Trigger *p;
136720
136721  if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
136722    pList = sqlite3TriggerList(pParse, pTab);
136723  }
136724  assert( pList==0 || IsVirtual(pTab)==0 );
136725  for(p=pList; p; p=p->pNext){
136726    if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
136727      mask |= p->tr_tm;
136728    }
136729  }
136730  if( pMask ){
136731    *pMask = mask;
136732  }
136733  return (mask ? pList : 0);
136734}
136735
136736/*
136737** Convert the pStep->zTarget string into a SrcList and return a pointer
136738** to that SrcList.
136739**
136740** This routine adds a specific database name, if needed, to the target when
136741** forming the SrcList.  This prevents a trigger in one database from
136742** referring to a target in another database.  An exception is when the
136743** trigger is in TEMP in which case it can refer to any other database it
136744** wants.
136745*/
136746static SrcList *targetSrcList(
136747  Parse *pParse,       /* The parsing context */
136748  TriggerStep *pStep   /* The trigger containing the target token */
136749){
136750  sqlite3 *db = pParse->db;
136751  int iDb;             /* Index of the database to use */
136752  SrcList *pSrc;       /* SrcList to be returned */
136753
136754  pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
136755  if( pSrc ){
136756    assert( pSrc->nSrc>0 );
136757    pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget);
136758    iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema);
136759    if( iDb==0 || iDb>=2 ){
136760      const char *zDb;
136761      assert( iDb<db->nDb );
136762      zDb = db->aDb[iDb].zDbSName;
136763      pSrc->a[pSrc->nSrc-1].zDatabase =  sqlite3DbStrDup(db, zDb);
136764    }
136765  }
136766  return pSrc;
136767}
136768
136769/*
136770** Generate VDBE code for the statements inside the body of a single
136771** trigger.
136772*/
136773static int codeTriggerProgram(
136774  Parse *pParse,            /* The parser context */
136775  TriggerStep *pStepList,   /* List of statements inside the trigger body */
136776  int orconf                /* Conflict algorithm. (OE_Abort, etc) */
136777){
136778  TriggerStep *pStep;
136779  Vdbe *v = pParse->pVdbe;
136780  sqlite3 *db = pParse->db;
136781
136782  assert( pParse->pTriggerTab && pParse->pToplevel );
136783  assert( pStepList );
136784  assert( v!=0 );
136785  for(pStep=pStepList; pStep; pStep=pStep->pNext){
136786    /* Figure out the ON CONFLICT policy that will be used for this step
136787    ** of the trigger program. If the statement that caused this trigger
136788    ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
136789    ** the ON CONFLICT policy that was specified as part of the trigger
136790    ** step statement. Example:
136791    **
136792    **   CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
136793    **     INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
136794    **   END;
136795    **
136796    **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
136797    **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
136798    */
136799    pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
136800    assert( pParse->okConstFactor==0 );
136801
136802#ifndef SQLITE_OMIT_TRACE
136803    if( pStep->zSpan ){
136804      sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
136805                        sqlite3MPrintf(db, "-- %s", pStep->zSpan),
136806                        P4_DYNAMIC);
136807    }
136808#endif
136809
136810    switch( pStep->op ){
136811      case TK_UPDATE: {
136812        sqlite3Update(pParse,
136813          targetSrcList(pParse, pStep),
136814          sqlite3ExprListDup(db, pStep->pExprList, 0),
136815          sqlite3ExprDup(db, pStep->pWhere, 0),
136816          pParse->eOrconf, 0, 0, 0
136817        );
136818        break;
136819      }
136820      case TK_INSERT: {
136821        sqlite3Insert(pParse,
136822          targetSrcList(pParse, pStep),
136823          sqlite3SelectDup(db, pStep->pSelect, 0),
136824          sqlite3IdListDup(db, pStep->pIdList),
136825          pParse->eOrconf,
136826          sqlite3UpsertDup(db, pStep->pUpsert)
136827        );
136828        break;
136829      }
136830      case TK_DELETE: {
136831        sqlite3DeleteFrom(pParse,
136832          targetSrcList(pParse, pStep),
136833          sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
136834        );
136835        break;
136836      }
136837      default: assert( pStep->op==TK_SELECT ); {
136838        SelectDest sDest;
136839        Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
136840        sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
136841        sqlite3Select(pParse, pSelect, &sDest);
136842        sqlite3SelectDelete(db, pSelect);
136843        break;
136844      }
136845    }
136846    if( pStep->op!=TK_SELECT ){
136847      sqlite3VdbeAddOp0(v, OP_ResetCount);
136848    }
136849  }
136850
136851  return 0;
136852}
136853
136854#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
136855/*
136856** This function is used to add VdbeComment() annotations to a VDBE
136857** program. It is not used in production code, only for debugging.
136858*/
136859static const char *onErrorText(int onError){
136860  switch( onError ){
136861    case OE_Abort:    return "abort";
136862    case OE_Rollback: return "rollback";
136863    case OE_Fail:     return "fail";
136864    case OE_Replace:  return "replace";
136865    case OE_Ignore:   return "ignore";
136866    case OE_Default:  return "default";
136867  }
136868  return "n/a";
136869}
136870#endif
136871
136872/*
136873** Parse context structure pFrom has just been used to create a sub-vdbe
136874** (trigger program). If an error has occurred, transfer error information
136875** from pFrom to pTo.
136876*/
136877static void transferParseError(Parse *pTo, Parse *pFrom){
136878  assert( pFrom->zErrMsg==0 || pFrom->nErr );
136879  assert( pTo->zErrMsg==0 || pTo->nErr );
136880  if( pTo->nErr==0 ){
136881    pTo->zErrMsg = pFrom->zErrMsg;
136882    pTo->nErr = pFrom->nErr;
136883    pTo->rc = pFrom->rc;
136884  }else{
136885    sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
136886  }
136887}
136888
136889/*
136890** Create and populate a new TriggerPrg object with a sub-program
136891** implementing trigger pTrigger with ON CONFLICT policy orconf.
136892*/
136893static TriggerPrg *codeRowTrigger(
136894  Parse *pParse,       /* Current parse context */
136895  Trigger *pTrigger,   /* Trigger to code */
136896  Table *pTab,         /* The table pTrigger is attached to */
136897  int orconf           /* ON CONFLICT policy to code trigger program with */
136898){
136899  Parse *pTop = sqlite3ParseToplevel(pParse);
136900  sqlite3 *db = pParse->db;   /* Database handle */
136901  TriggerPrg *pPrg;           /* Value to return */
136902  Expr *pWhen = 0;            /* Duplicate of trigger WHEN expression */
136903  Vdbe *v;                    /* Temporary VM */
136904  NameContext sNC;            /* Name context for sub-vdbe */
136905  SubProgram *pProgram = 0;   /* Sub-vdbe for trigger program */
136906  Parse *pSubParse;           /* Parse context for sub-vdbe */
136907  int iEndTrigger = 0;        /* Label to jump to if WHEN is false */
136908
136909  assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
136910  assert( pTop->pVdbe );
136911
136912  /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
136913  ** are freed if an error occurs, link them into the Parse.pTriggerPrg
136914  ** list of the top-level Parse object sooner rather than later.  */
136915  pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
136916  if( !pPrg ) return 0;
136917  pPrg->pNext = pTop->pTriggerPrg;
136918  pTop->pTriggerPrg = pPrg;
136919  pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
136920  if( !pProgram ) return 0;
136921  sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
136922  pPrg->pTrigger = pTrigger;
136923  pPrg->orconf = orconf;
136924  pPrg->aColmask[0] = 0xffffffff;
136925  pPrg->aColmask[1] = 0xffffffff;
136926
136927  /* Allocate and populate a new Parse context to use for coding the
136928  ** trigger sub-program.  */
136929  pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
136930  if( !pSubParse ) return 0;
136931  memset(&sNC, 0, sizeof(sNC));
136932  sNC.pParse = pSubParse;
136933  pSubParse->db = db;
136934  pSubParse->pTriggerTab = pTab;
136935  pSubParse->pToplevel = pTop;
136936  pSubParse->zAuthContext = pTrigger->zName;
136937  pSubParse->eTriggerOp = pTrigger->op;
136938  pSubParse->nQueryLoop = pParse->nQueryLoop;
136939  pSubParse->disableVtab = pParse->disableVtab;
136940
136941  v = sqlite3GetVdbe(pSubParse);
136942  if( v ){
136943    VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
136944      pTrigger->zName, onErrorText(orconf),
136945      (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
136946        (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
136947        (pTrigger->op==TK_INSERT ? "INSERT" : ""),
136948        (pTrigger->op==TK_DELETE ? "DELETE" : ""),
136949      pTab->zName
136950    ));
136951#ifndef SQLITE_OMIT_TRACE
136952    if( pTrigger->zName ){
136953      sqlite3VdbeChangeP4(v, -1,
136954        sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
136955      );
136956    }
136957#endif
136958
136959    /* If one was specified, code the WHEN clause. If it evaluates to false
136960    ** (or NULL) the sub-vdbe is immediately halted by jumping to the
136961    ** OP_Halt inserted at the end of the program.  */
136962    if( pTrigger->pWhen ){
136963      pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
136964      if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
136965       && db->mallocFailed==0
136966      ){
136967        iEndTrigger = sqlite3VdbeMakeLabel(pSubParse);
136968        sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
136969      }
136970      sqlite3ExprDelete(db, pWhen);
136971    }
136972
136973    /* Code the trigger program into the sub-vdbe. */
136974    codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
136975
136976    /* Insert an OP_Halt at the end of the sub-program. */
136977    if( iEndTrigger ){
136978      sqlite3VdbeResolveLabel(v, iEndTrigger);
136979    }
136980    sqlite3VdbeAddOp0(v, OP_Halt);
136981    VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
136982
136983    transferParseError(pParse, pSubParse);
136984    if( db->mallocFailed==0 && pParse->nErr==0 ){
136985      pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
136986    }
136987    pProgram->nMem = pSubParse->nMem;
136988    pProgram->nCsr = pSubParse->nTab;
136989    pProgram->token = (void *)pTrigger;
136990    pPrg->aColmask[0] = pSubParse->oldmask;
136991    pPrg->aColmask[1] = pSubParse->newmask;
136992    sqlite3VdbeDelete(v);
136993  }
136994
136995  assert( !pSubParse->pAinc       && !pSubParse->pZombieTab );
136996  assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
136997  sqlite3ParserReset(pSubParse);
136998  sqlite3StackFree(db, pSubParse);
136999
137000  return pPrg;
137001}
137002
137003/*
137004** Return a pointer to a TriggerPrg object containing the sub-program for
137005** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
137006** TriggerPrg object exists, a new object is allocated and populated before
137007** being returned.
137008*/
137009static TriggerPrg *getRowTrigger(
137010  Parse *pParse,       /* Current parse context */
137011  Trigger *pTrigger,   /* Trigger to code */
137012  Table *pTab,         /* The table trigger pTrigger is attached to */
137013  int orconf           /* ON CONFLICT algorithm. */
137014){
137015  Parse *pRoot = sqlite3ParseToplevel(pParse);
137016  TriggerPrg *pPrg;
137017
137018  assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
137019
137020  /* It may be that this trigger has already been coded (or is in the
137021  ** process of being coded). If this is the case, then an entry with
137022  ** a matching TriggerPrg.pTrigger field will be present somewhere
137023  ** in the Parse.pTriggerPrg list. Search for such an entry.  */
137024  for(pPrg=pRoot->pTriggerPrg;
137025      pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
137026      pPrg=pPrg->pNext
137027  );
137028
137029  /* If an existing TriggerPrg could not be located, create a new one. */
137030  if( !pPrg ){
137031    pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
137032  }
137033
137034  return pPrg;
137035}
137036
137037/*
137038** Generate code for the trigger program associated with trigger p on
137039** table pTab. The reg, orconf and ignoreJump parameters passed to this
137040** function are the same as those described in the header function for
137041** sqlite3CodeRowTrigger()
137042*/
137043SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
137044  Parse *pParse,       /* Parse context */
137045  Trigger *p,          /* Trigger to code */
137046  Table *pTab,         /* The table to code triggers from */
137047  int reg,             /* Reg array containing OLD.* and NEW.* values */
137048  int orconf,          /* ON CONFLICT policy */
137049  int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
137050){
137051  Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
137052  TriggerPrg *pPrg;
137053  pPrg = getRowTrigger(pParse, p, pTab, orconf);
137054  assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
137055
137056  /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
137057  ** is a pointer to the sub-vdbe containing the trigger program.  */
137058  if( pPrg ){
137059    int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
137060
137061    sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
137062                      (const char *)pPrg->pProgram, P4_SUBPROGRAM);
137063    VdbeComment(
137064        (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
137065
137066    /* Set the P5 operand of the OP_Program instruction to non-zero if
137067    ** recursive invocation of this trigger program is disallowed. Recursive
137068    ** invocation is disallowed if (a) the sub-program is really a trigger,
137069    ** not a foreign key action, and (b) the flag to enable recursive triggers
137070    ** is clear.  */
137071    sqlite3VdbeChangeP5(v, (u8)bRecursive);
137072  }
137073}
137074
137075/*
137076** This is called to code the required FOR EACH ROW triggers for an operation
137077** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
137078** is given by the op parameter. The tr_tm parameter determines whether the
137079** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
137080** parameter pChanges is passed the list of columns being modified.
137081**
137082** If there are no triggers that fire at the specified time for the specified
137083** operation on pTab, this function is a no-op.
137084**
137085** The reg argument is the address of the first in an array of registers
137086** that contain the values substituted for the new.* and old.* references
137087** in the trigger program. If N is the number of columns in table pTab
137088** (a copy of pTab->nCol), then registers are populated as follows:
137089**
137090**   Register       Contains
137091**   ------------------------------------------------------
137092**   reg+0          OLD.rowid
137093**   reg+1          OLD.* value of left-most column of pTab
137094**   ...            ...
137095**   reg+N          OLD.* value of right-most column of pTab
137096**   reg+N+1        NEW.rowid
137097**   reg+N+2        OLD.* value of left-most column of pTab
137098**   ...            ...
137099**   reg+N+N+1      NEW.* value of right-most column of pTab
137100**
137101** For ON DELETE triggers, the registers containing the NEW.* values will
137102** never be accessed by the trigger program, so they are not allocated or
137103** populated by the caller (there is no data to populate them with anyway).
137104** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
137105** are never accessed, and so are not allocated by the caller. So, for an
137106** ON INSERT trigger, the value passed to this function as parameter reg
137107** is not a readable register, although registers (reg+N) through
137108** (reg+N+N+1) are.
137109**
137110** Parameter orconf is the default conflict resolution algorithm for the
137111** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
137112** is the instruction that control should jump to if a trigger program
137113** raises an IGNORE exception.
137114*/
137115SQLITE_PRIVATE void sqlite3CodeRowTrigger(
137116  Parse *pParse,       /* Parse context */
137117  Trigger *pTrigger,   /* List of triggers on table pTab */
137118  int op,              /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
137119  ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
137120  int tr_tm,           /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
137121  Table *pTab,         /* The table to code triggers from */
137122  int reg,             /* The first in an array of registers (see above) */
137123  int orconf,          /* ON CONFLICT policy */
137124  int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
137125){
137126  Trigger *p;          /* Used to iterate through pTrigger list */
137127
137128  assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
137129  assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
137130  assert( (op==TK_UPDATE)==(pChanges!=0) );
137131
137132  for(p=pTrigger; p; p=p->pNext){
137133
137134    /* Sanity checking:  The schema for the trigger and for the table are
137135    ** always defined.  The trigger must be in the same schema as the table
137136    ** or else it must be a TEMP trigger. */
137137    assert( p->pSchema!=0 );
137138    assert( p->pTabSchema!=0 );
137139    assert( p->pSchema==p->pTabSchema
137140         || p->pSchema==pParse->db->aDb[1].pSchema );
137141
137142    /* Determine whether we should code this trigger */
137143    if( p->op==op
137144     && p->tr_tm==tr_tm
137145     && checkColumnOverlap(p->pColumns, pChanges)
137146    ){
137147      sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
137148    }
137149  }
137150}
137151
137152/*
137153** Triggers may access values stored in the old.* or new.* pseudo-table.
137154** This function returns a 32-bit bitmask indicating which columns of the
137155** old.* or new.* tables actually are used by triggers. This information
137156** may be used by the caller, for example, to avoid having to load the entire
137157** old.* record into memory when executing an UPDATE or DELETE command.
137158**
137159** Bit 0 of the returned mask is set if the left-most column of the
137160** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
137161** the second leftmost column value is required, and so on. If there
137162** are more than 32 columns in the table, and at least one of the columns
137163** with an index greater than 32 may be accessed, 0xffffffff is returned.
137164**
137165** It is not possible to determine if the old.rowid or new.rowid column is
137166** accessed by triggers. The caller must always assume that it is.
137167**
137168** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
137169** applies to the old.* table. If 1, the new.* table.
137170**
137171** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
137172** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
137173** included in the returned mask if the TRIGGER_BEFORE bit is set in the
137174** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
137175** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
137176*/
137177SQLITE_PRIVATE u32 sqlite3TriggerColmask(
137178  Parse *pParse,       /* Parse context */
137179  Trigger *pTrigger,   /* List of triggers on table pTab */
137180  ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
137181  int isNew,           /* 1 for new.* ref mask, 0 for old.* ref mask */
137182  int tr_tm,           /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
137183  Table *pTab,         /* The table to code triggers from */
137184  int orconf           /* Default ON CONFLICT policy for trigger steps */
137185){
137186  const int op = pChanges ? TK_UPDATE : TK_DELETE;
137187  u32 mask = 0;
137188  Trigger *p;
137189
137190  assert( isNew==1 || isNew==0 );
137191  for(p=pTrigger; p; p=p->pNext){
137192    if( p->op==op && (tr_tm&p->tr_tm)
137193     && checkColumnOverlap(p->pColumns,pChanges)
137194    ){
137195      TriggerPrg *pPrg;
137196      pPrg = getRowTrigger(pParse, p, pTab, orconf);
137197      if( pPrg ){
137198        mask |= pPrg->aColmask[isNew];
137199      }
137200    }
137201  }
137202
137203  return mask;
137204}
137205
137206#endif /* !defined(SQLITE_OMIT_TRIGGER) */
137207
137208/************** End of trigger.c *********************************************/
137209/************** Begin file update.c ******************************************/
137210/*
137211** 2001 September 15
137212**
137213** The author disclaims copyright to this source code.  In place of
137214** a legal notice, here is a blessing:
137215**
137216**    May you do good and not evil.
137217**    May you find forgiveness for yourself and forgive others.
137218**    May you share freely, never taking more than you give.
137219**
137220*************************************************************************
137221** This file contains C code routines that are called by the parser
137222** to handle UPDATE statements.
137223*/
137224/* #include "sqliteInt.h" */
137225
137226#ifndef SQLITE_OMIT_VIRTUALTABLE
137227/* Forward declaration */
137228static void updateVirtualTable(
137229  Parse *pParse,       /* The parsing context */
137230  SrcList *pSrc,       /* The virtual table to be modified */
137231  Table *pTab,         /* The virtual table */
137232  ExprList *pChanges,  /* The columns to change in the UPDATE statement */
137233  Expr *pRowidExpr,    /* Expression used to recompute the rowid */
137234  int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
137235  Expr *pWhere,        /* WHERE clause of the UPDATE statement */
137236  int onError          /* ON CONFLICT strategy */
137237);
137238#endif /* SQLITE_OMIT_VIRTUALTABLE */
137239
137240/*
137241** The most recently coded instruction was an OP_Column to retrieve the
137242** i-th column of table pTab. This routine sets the P4 parameter of the
137243** OP_Column to the default value, if any.
137244**
137245** The default value of a column is specified by a DEFAULT clause in the
137246** column definition. This was either supplied by the user when the table
137247** was created, or added later to the table definition by an ALTER TABLE
137248** command. If the latter, then the row-records in the table btree on disk
137249** may not contain a value for the column and the default value, taken
137250** from the P4 parameter of the OP_Column instruction, is returned instead.
137251** If the former, then all row-records are guaranteed to include a value
137252** for the column and the P4 value is not required.
137253**
137254** Column definitions created by an ALTER TABLE command may only have
137255** literal default values specified: a number, null or a string. (If a more
137256** complicated default expression value was provided, it is evaluated
137257** when the ALTER TABLE is executed and one of the literal values written
137258** into the sqlite_master table.)
137259**
137260** Therefore, the P4 parameter is only required if the default value for
137261** the column is a literal number, string or null. The sqlite3ValueFromExpr()
137262** function is capable of transforming these types of expressions into
137263** sqlite3_value objects.
137264**
137265** If column as REAL affinity and the table is an ordinary b-tree table
137266** (not a virtual table) then the value might have been stored as an
137267** integer.  In that case, add an OP_RealAffinity opcode to make sure
137268** it has been converted into REAL.
137269*/
137270SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
137271  assert( pTab!=0 );
137272  if( !pTab->pSelect ){
137273    sqlite3_value *pValue = 0;
137274    u8 enc = ENC(sqlite3VdbeDb(v));
137275    Column *pCol = &pTab->aCol[i];
137276    VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
137277    assert( i<pTab->nCol );
137278    sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
137279                         pCol->affinity, &pValue);
137280    if( pValue ){
137281      sqlite3VdbeAppendP4(v, pValue, P4_MEM);
137282    }
137283  }
137284#ifndef SQLITE_OMIT_FLOATING_POINT
137285  if( pTab->aCol[i].affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
137286    sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
137287  }
137288#endif
137289}
137290
137291/*
137292** Check to see if column iCol of index pIdx references any of the
137293** columns defined by aXRef and chngRowid.  Return true if it does
137294** and false if not.  This is an optimization.  False-positives are a
137295** performance degradation, but false-negatives can result in a corrupt
137296** index and incorrect answers.
137297**
137298** aXRef[j] will be non-negative if column j of the original table is
137299** being updated.  chngRowid will be true if the rowid of the table is
137300** being updated.
137301*/
137302static int indexColumnIsBeingUpdated(
137303  Index *pIdx,      /* The index to check */
137304  int iCol,         /* Which column of the index to check */
137305  int *aXRef,       /* aXRef[j]>=0 if column j is being updated */
137306  int chngRowid     /* true if the rowid is being updated */
137307){
137308  i16 iIdxCol = pIdx->aiColumn[iCol];
137309  assert( iIdxCol!=XN_ROWID ); /* Cannot index rowid */
137310  if( iIdxCol>=0 ){
137311    return aXRef[iIdxCol]>=0;
137312  }
137313  assert( iIdxCol==XN_EXPR );
137314  assert( pIdx->aColExpr!=0 );
137315  assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
137316  return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
137317                                            aXRef,chngRowid);
137318}
137319
137320/*
137321** Check to see if index pIdx is a partial index whose conditional
137322** expression might change values due to an UPDATE.  Return true if
137323** the index is subject to change and false if the index is guaranteed
137324** to be unchanged.  This is an optimization.  False-positives are a
137325** performance degradation, but false-negatives can result in a corrupt
137326** index and incorrect answers.
137327**
137328** aXRef[j] will be non-negative if column j of the original table is
137329** being updated.  chngRowid will be true if the rowid of the table is
137330** being updated.
137331*/
137332static int indexWhereClauseMightChange(
137333  Index *pIdx,      /* The index to check */
137334  int *aXRef,       /* aXRef[j]>=0 if column j is being updated */
137335  int chngRowid     /* true if the rowid is being updated */
137336){
137337  if( pIdx->pPartIdxWhere==0 ) return 0;
137338  return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
137339                                            aXRef, chngRowid);
137340}
137341
137342/*
137343** Process an UPDATE statement.
137344**
137345**   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
137346**          \_______/ \________/     \______/       \________________/
137347*            onError   pTabList      pChanges             pWhere
137348*/
137349SQLITE_PRIVATE void sqlite3Update(
137350  Parse *pParse,         /* The parser context */
137351  SrcList *pTabList,     /* The table in which we should change things */
137352  ExprList *pChanges,    /* Things to be changed */
137353  Expr *pWhere,          /* The WHERE clause.  May be null */
137354  int onError,           /* How to handle constraint errors */
137355  ExprList *pOrderBy,    /* ORDER BY clause. May be null */
137356  Expr *pLimit,          /* LIMIT clause. May be null */
137357  Upsert *pUpsert        /* ON CONFLICT clause, or null */
137358){
137359  int i, j, k;           /* Loop counters */
137360  Table *pTab;           /* The table to be updated */
137361  int addrTop = 0;       /* VDBE instruction address of the start of the loop */
137362  WhereInfo *pWInfo;     /* Information about the WHERE clause */
137363  Vdbe *v;               /* The virtual database engine */
137364  Index *pIdx;           /* For looping over indices */
137365  Index *pPk;            /* The PRIMARY KEY index for WITHOUT ROWID tables */
137366  int nIdx;              /* Number of indices that need updating */
137367  int nAllIdx;           /* Total number of indexes */
137368  int iBaseCur;          /* Base cursor number */
137369  int iDataCur;          /* Cursor for the canonical data btree */
137370  int iIdxCur;           /* Cursor for the first index */
137371  sqlite3 *db;           /* The database structure */
137372  int *aRegIdx = 0;      /* Registers for to each index and the main table */
137373  int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
137374                         ** an expression for the i-th column of the table.
137375                         ** aXRef[i]==-1 if the i-th column is not changed. */
137376  u8 *aToOpen;           /* 1 for tables and indices to be opened */
137377  u8 chngPk;             /* PRIMARY KEY changed in a WITHOUT ROWID table */
137378  u8 chngRowid;          /* Rowid changed in a normal table */
137379  u8 chngKey;            /* Either chngPk or chngRowid */
137380  Expr *pRowidExpr = 0;  /* Expression defining the new record number */
137381  AuthContext sContext;  /* The authorization context */
137382  NameContext sNC;       /* The name-context to resolve expressions in */
137383  int iDb;               /* Database containing the table being updated */
137384  int eOnePass;          /* ONEPASS_XXX value from where.c */
137385  int hasFK;             /* True if foreign key processing is required */
137386  int labelBreak;        /* Jump here to break out of UPDATE loop */
137387  int labelContinue;     /* Jump here to continue next step of UPDATE loop */
137388  int flags;             /* Flags for sqlite3WhereBegin() */
137389
137390#ifndef SQLITE_OMIT_TRIGGER
137391  int isView;            /* True when updating a view (INSTEAD OF trigger) */
137392  Trigger *pTrigger;     /* List of triggers on pTab, if required */
137393  int tmask;             /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
137394#endif
137395  int newmask;           /* Mask of NEW.* columns accessed by BEFORE triggers */
137396  int iEph = 0;          /* Ephemeral table holding all primary key values */
137397  int nKey = 0;          /* Number of elements in regKey for WITHOUT ROWID */
137398  int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
137399  int addrOpen = 0;      /* Address of OP_OpenEphemeral */
137400  int iPk = 0;           /* First of nPk cells holding PRIMARY KEY value */
137401  i16 nPk = 0;           /* Number of components of the PRIMARY KEY */
137402  int bReplace = 0;      /* True if REPLACE conflict resolution might happen */
137403  int bFinishSeek = 1;   /* The OP_FinishSeek opcode is needed */
137404
137405  /* Register Allocations */
137406  int regRowCount = 0;   /* A count of rows changed */
137407  int regOldRowid = 0;   /* The old rowid */
137408  int regNewRowid = 0;   /* The new rowid */
137409  int regNew = 0;        /* Content of the NEW.* table in triggers */
137410  int regOld = 0;        /* Content of OLD.* table in triggers */
137411  int regRowSet = 0;     /* Rowset of rows to be updated */
137412  int regKey = 0;        /* composite PRIMARY KEY value */
137413
137414  memset(&sContext, 0, sizeof(sContext));
137415  db = pParse->db;
137416  if( pParse->nErr || db->mallocFailed ){
137417    goto update_cleanup;
137418  }
137419  assert( pTabList->nSrc==1 );
137420
137421  /* Locate the table which we want to update.
137422  */
137423  pTab = sqlite3SrcListLookup(pParse, pTabList);
137424  if( pTab==0 ) goto update_cleanup;
137425  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
137426
137427  /* Figure out if we have any triggers and if the table being
137428  ** updated is a view.
137429  */
137430#ifndef SQLITE_OMIT_TRIGGER
137431  pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
137432  isView = pTab->pSelect!=0;
137433  assert( pTrigger || tmask==0 );
137434#else
137435# define pTrigger 0
137436# define isView 0
137437# define tmask 0
137438#endif
137439#ifdef SQLITE_OMIT_VIEW
137440# undef isView
137441# define isView 0
137442#endif
137443
137444#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
137445  if( !isView ){
137446    pWhere = sqlite3LimitWhere(
137447        pParse, pTabList, pWhere, pOrderBy, pLimit, "UPDATE"
137448    );
137449    pOrderBy = 0;
137450    pLimit = 0;
137451  }
137452#endif
137453
137454  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
137455    goto update_cleanup;
137456  }
137457  if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
137458    goto update_cleanup;
137459  }
137460
137461  /* Allocate a cursors for the main database table and for all indices.
137462  ** The index cursors might not be used, but if they are used they
137463  ** need to occur right after the database cursor.  So go ahead and
137464  ** allocate enough space, just in case.
137465  */
137466  iBaseCur = iDataCur = pParse->nTab++;
137467  iIdxCur = iDataCur+1;
137468  pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
137469  testcase( pPk!=0 && pPk!=pTab->pIndex );
137470  for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
137471    if( pPk==pIdx ){
137472      iDataCur = pParse->nTab;
137473    }
137474    pParse->nTab++;
137475  }
137476  if( pUpsert ){
137477    /* On an UPSERT, reuse the same cursors already opened by INSERT */
137478    iDataCur = pUpsert->iDataCur;
137479    iIdxCur = pUpsert->iIdxCur;
137480    pParse->nTab = iBaseCur;
137481  }
137482  pTabList->a[0].iCursor = iDataCur;
137483
137484  /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
137485  ** Initialize aXRef[] and aToOpen[] to their default values.
137486  */
137487  aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
137488  if( aXRef==0 ) goto update_cleanup;
137489  aRegIdx = aXRef+pTab->nCol;
137490  aToOpen = (u8*)(aRegIdx+nIdx+1);
137491  memset(aToOpen, 1, nIdx+1);
137492  aToOpen[nIdx+1] = 0;
137493  for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
137494
137495  /* Initialize the name-context */
137496  memset(&sNC, 0, sizeof(sNC));
137497  sNC.pParse = pParse;
137498  sNC.pSrcList = pTabList;
137499  sNC.uNC.pUpsert = pUpsert;
137500  sNC.ncFlags = NC_UUpsert;
137501
137502  /* Begin generating code. */
137503  v = sqlite3GetVdbe(pParse);
137504  if( v==0 ) goto update_cleanup;
137505
137506  /* Resolve the column names in all the expressions of the
137507  ** of the UPDATE statement.  Also find the column index
137508  ** for each column to be updated in the pChanges array.  For each
137509  ** column to be updated, make sure we have authorization to change
137510  ** that column.
137511  */
137512  chngRowid = chngPk = 0;
137513  for(i=0; i<pChanges->nExpr; i++){
137514    if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
137515      goto update_cleanup;
137516    }
137517    for(j=0; j<pTab->nCol; j++){
137518      if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zEName)==0 ){
137519        if( j==pTab->iPKey ){
137520          chngRowid = 1;
137521          pRowidExpr = pChanges->a[i].pExpr;
137522        }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
137523          chngPk = 1;
137524        }
137525#ifndef SQLITE_OMIT_GENERATED_COLUMNS
137526        else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED ){
137527          testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );
137528          testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );
137529          sqlite3ErrorMsg(pParse,
137530             "cannot UPDATE generated column \"%s\"",
137531             pTab->aCol[j].zName);
137532          goto update_cleanup;
137533        }
137534#endif
137535        aXRef[j] = i;
137536        break;
137537      }
137538    }
137539    if( j>=pTab->nCol ){
137540      if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zEName) ){
137541        j = -1;
137542        chngRowid = 1;
137543        pRowidExpr = pChanges->a[i].pExpr;
137544      }else{
137545        sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zEName);
137546        pParse->checkSchema = 1;
137547        goto update_cleanup;
137548      }
137549    }
137550#ifndef SQLITE_OMIT_AUTHORIZATION
137551    {
137552      int rc;
137553      rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
137554                            j<0 ? "ROWID" : pTab->aCol[j].zName,
137555                            db->aDb[iDb].zDbSName);
137556      if( rc==SQLITE_DENY ){
137557        goto update_cleanup;
137558      }else if( rc==SQLITE_IGNORE ){
137559        aXRef[j] = -1;
137560      }
137561    }
137562#endif
137563  }
137564  assert( (chngRowid & chngPk)==0 );
137565  assert( chngRowid==0 || chngRowid==1 );
137566  assert( chngPk==0 || chngPk==1 );
137567  chngKey = chngRowid + chngPk;
137568
137569#ifndef SQLITE_OMIT_GENERATED_COLUMNS
137570  /* Mark generated columns as changing if their generator expressions
137571  ** reference any changing column.  The actual aXRef[] value for
137572  ** generated expressions is not used, other than to check to see that it
137573  ** is non-negative, so the value of aXRef[] for generated columns can be
137574  ** set to any non-negative number.  We use 99999 so that the value is
137575  ** obvious when looking at aXRef[] in a symbolic debugger.
137576  */
137577  if( pTab->tabFlags & TF_HasGenerated ){
137578    int bProgress;
137579    testcase( pTab->tabFlags & TF_HasVirtual );
137580    testcase( pTab->tabFlags & TF_HasStored );
137581    do{
137582      bProgress = 0;
137583      for(i=0; i<pTab->nCol; i++){
137584        if( aXRef[i]>=0 ) continue;
137585        if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ) continue;
137586        if( sqlite3ExprReferencesUpdatedColumn(pTab->aCol[i].pDflt,
137587                                               aXRef, chngRowid) ){
137588          aXRef[i] = 99999;
137589          bProgress = 1;
137590        }
137591      }
137592    }while( bProgress );
137593  }
137594#endif
137595
137596  /* The SET expressions are not actually used inside the WHERE loop.
137597  ** So reset the colUsed mask. Unless this is a virtual table. In that
137598  ** case, set all bits of the colUsed mask (to ensure that the virtual
137599  ** table implementation makes all columns available).
137600  */
137601  pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
137602
137603  hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
137604
137605  /* There is one entry in the aRegIdx[] array for each index on the table
137606  ** being updated.  Fill in aRegIdx[] with a register number that will hold
137607  ** the key for accessing each index.
137608  */
137609  if( onError==OE_Replace ) bReplace = 1;
137610  for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
137611    int reg;
137612    if( chngKey || hasFK>1 || pIdx==pPk
137613     || indexWhereClauseMightChange(pIdx,aXRef,chngRowid)
137614    ){
137615      reg = ++pParse->nMem;
137616      pParse->nMem += pIdx->nColumn;
137617    }else{
137618      reg = 0;
137619      for(i=0; i<pIdx->nKeyCol; i++){
137620        if( indexColumnIsBeingUpdated(pIdx, i, aXRef, chngRowid) ){
137621          reg = ++pParse->nMem;
137622          pParse->nMem += pIdx->nColumn;
137623          if( onError==OE_Default && pIdx->onError==OE_Replace ){
137624            bReplace = 1;
137625          }
137626          break;
137627        }
137628      }
137629    }
137630    if( reg==0 ) aToOpen[nAllIdx+1] = 0;
137631    aRegIdx[nAllIdx] = reg;
137632  }
137633  aRegIdx[nAllIdx] = ++pParse->nMem;  /* Register storing the table record */
137634  if( bReplace ){
137635    /* If REPLACE conflict resolution might be invoked, open cursors on all
137636    ** indexes in case they are needed to delete records.  */
137637    memset(aToOpen, 1, nIdx+1);
137638  }
137639
137640  if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
137641  sqlite3BeginWriteOperation(pParse, pTrigger || hasFK, iDb);
137642
137643  /* Allocate required registers. */
137644  if( !IsVirtual(pTab) ){
137645    /* For now, regRowSet and aRegIdx[nAllIdx] share the same register.
137646    ** If regRowSet turns out to be needed, then aRegIdx[nAllIdx] will be
137647    ** reallocated.  aRegIdx[nAllIdx] is the register in which the main
137648    ** table record is written.  regRowSet holds the RowSet for the
137649    ** two-pass update algorithm. */
137650    assert( aRegIdx[nAllIdx]==pParse->nMem );
137651    regRowSet = aRegIdx[nAllIdx];
137652    regOldRowid = regNewRowid = ++pParse->nMem;
137653    if( chngPk || pTrigger || hasFK ){
137654      regOld = pParse->nMem + 1;
137655      pParse->nMem += pTab->nCol;
137656    }
137657    if( chngKey || pTrigger || hasFK ){
137658      regNewRowid = ++pParse->nMem;
137659    }
137660    regNew = pParse->nMem + 1;
137661    pParse->nMem += pTab->nCol;
137662  }
137663
137664  /* Start the view context. */
137665  if( isView ){
137666    sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
137667  }
137668
137669  /* If we are trying to update a view, realize that view into
137670  ** an ephemeral table.
137671  */
137672#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
137673  if( isView ){
137674    sqlite3MaterializeView(pParse, pTab,
137675        pWhere, pOrderBy, pLimit, iDataCur
137676    );
137677    pOrderBy = 0;
137678    pLimit = 0;
137679  }
137680#endif
137681
137682  /* Resolve the column names in all the expressions in the
137683  ** WHERE clause.
137684  */
137685  if( sqlite3ResolveExprNames(&sNC, pWhere) ){
137686    goto update_cleanup;
137687  }
137688
137689#ifndef SQLITE_OMIT_VIRTUALTABLE
137690  /* Virtual tables must be handled separately */
137691  if( IsVirtual(pTab) ){
137692    updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
137693                       pWhere, onError);
137694    goto update_cleanup;
137695  }
137696#endif
137697
137698  /* Jump to labelBreak to abandon further processing of this UPDATE */
137699  labelContinue = labelBreak = sqlite3VdbeMakeLabel(pParse);
137700
137701  /* Not an UPSERT.  Normal processing.  Begin by
137702  ** initialize the count of updated rows */
137703  if( (db->flags&SQLITE_CountRows)!=0
137704   && !pParse->pTriggerTab
137705   && !pParse->nested
137706   && pUpsert==0
137707  ){
137708    regRowCount = ++pParse->nMem;
137709    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
137710  }
137711
137712  if( HasRowid(pTab) ){
137713    sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
137714  }else{
137715    assert( pPk!=0 );
137716    nPk = pPk->nKeyCol;
137717    iPk = pParse->nMem+1;
137718    pParse->nMem += nPk;
137719    regKey = ++pParse->nMem;
137720    if( pUpsert==0 ){
137721      iEph = pParse->nTab++;
137722        sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
137723      addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
137724      sqlite3VdbeSetP4KeyInfo(pParse, pPk);
137725    }
137726  }
137727
137728  if( pUpsert ){
137729    /* If this is an UPSERT, then all cursors have already been opened by
137730    ** the outer INSERT and the data cursor should be pointing at the row
137731    ** that is to be updated.  So bypass the code that searches for the
137732    ** row(s) to be updated.
137733    */
137734    pWInfo = 0;
137735    eOnePass = ONEPASS_SINGLE;
137736    sqlite3ExprIfFalse(pParse, pWhere, labelBreak, SQLITE_JUMPIFNULL);
137737    bFinishSeek = 0;
137738  }else{
137739    /* Begin the database scan.
137740    **
137741    ** Do not consider a single-pass strategy for a multi-row update if
137742    ** there are any triggers or foreign keys to process, or rows may
137743    ** be deleted as a result of REPLACE conflict handling. Any of these
137744    ** things might disturb a cursor being used to scan through the table
137745    ** or index, causing a single-pass approach to malfunction.  */
137746    flags = WHERE_ONEPASS_DESIRED|WHERE_SEEK_UNIQ_TABLE;
137747    if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
137748      flags |= WHERE_ONEPASS_MULTIROW;
137749    }
137750    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags, iIdxCur);
137751    if( pWInfo==0 ) goto update_cleanup;
137752
137753    /* A one-pass strategy that might update more than one row may not
137754    ** be used if any column of the index used for the scan is being
137755    ** updated. Otherwise, if there is an index on "b", statements like
137756    ** the following could create an infinite loop:
137757    **
137758    **   UPDATE t1 SET b=b+1 WHERE b>?
137759    **
137760    ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
137761    ** strategy that uses an index for which one or more columns are being
137762    ** updated.  */
137763    eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
137764    bFinishSeek = sqlite3WhereUsesDeferredSeek(pWInfo);
137765    if( eOnePass!=ONEPASS_SINGLE ){
137766      sqlite3MultiWrite(pParse);
137767      if( eOnePass==ONEPASS_MULTI ){
137768        int iCur = aiCurOnePass[1];
137769        if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
137770          eOnePass = ONEPASS_OFF;
137771        }
137772        assert( iCur!=iDataCur || !HasRowid(pTab) );
137773      }
137774    }
137775  }
137776
137777  if( HasRowid(pTab) ){
137778    /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
137779    ** mode, write the rowid into the FIFO. In either of the one-pass modes,
137780    ** leave it in register regOldRowid.  */
137781    sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
137782    if( eOnePass==ONEPASS_OFF ){
137783      /* We need to use regRowSet, so reallocate aRegIdx[nAllIdx] */
137784      aRegIdx[nAllIdx] = ++pParse->nMem;
137785      sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
137786    }
137787  }else{
137788    /* Read the PK of the current row into an array of registers. In
137789    ** ONEPASS_OFF mode, serialize the array into a record and store it in
137790    ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
137791    ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
137792    ** is not required) and leave the PK fields in the array of registers.  */
137793    for(i=0; i<nPk; i++){
137794      assert( pPk->aiColumn[i]>=0 );
137795      sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,
137796                                      pPk->aiColumn[i], iPk+i);
137797    }
137798    if( eOnePass ){
137799      if( addrOpen ) sqlite3VdbeChangeToNoop(v, addrOpen);
137800      nKey = nPk;
137801      regKey = iPk;
137802    }else{
137803      sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
137804                        sqlite3IndexAffinityStr(db, pPk), nPk);
137805      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk);
137806    }
137807  }
137808
137809  if( pUpsert==0 ){
137810    if( eOnePass!=ONEPASS_MULTI ){
137811      sqlite3WhereEnd(pWInfo);
137812    }
137813
137814    if( !isView ){
137815      int addrOnce = 0;
137816
137817      /* Open every index that needs updating. */
137818      if( eOnePass!=ONEPASS_OFF ){
137819        if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
137820        if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
137821      }
137822
137823      if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
137824        addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
137825      }
137826      sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur,
137827                                 aToOpen, 0, 0);
137828      if( addrOnce ){
137829        sqlite3VdbeJumpHereOrPopInst(v, addrOnce);
137830      }
137831    }
137832
137833    /* Top of the update loop */
137834    if( eOnePass!=ONEPASS_OFF ){
137835      if( !isView && aiCurOnePass[0]!=iDataCur && aiCurOnePass[1]!=iDataCur ){
137836        assert( pPk );
137837        sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey,nKey);
137838        VdbeCoverage(v);
137839      }
137840      if( eOnePass!=ONEPASS_SINGLE ){
137841        labelContinue = sqlite3VdbeMakeLabel(pParse);
137842      }
137843      sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
137844      VdbeCoverageIf(v, pPk==0);
137845      VdbeCoverageIf(v, pPk!=0);
137846    }else if( pPk ){
137847      labelContinue = sqlite3VdbeMakeLabel(pParse);
137848      sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
137849      addrTop = sqlite3VdbeAddOp2(v, OP_RowData, iEph, regKey);
137850      sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
137851      VdbeCoverage(v);
137852    }else{
137853      labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet,labelBreak,
137854                               regOldRowid);
137855      VdbeCoverage(v);
137856      sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
137857      VdbeCoverage(v);
137858    }
137859  }
137860
137861  /* If the rowid value will change, set register regNewRowid to
137862  ** contain the new value. If the rowid is not being modified,
137863  ** then regNewRowid is the same register as regOldRowid, which is
137864  ** already populated.  */
137865  assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
137866  if( chngRowid ){
137867    sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
137868    sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); VdbeCoverage(v);
137869  }
137870
137871  /* Compute the old pre-UPDATE content of the row being changed, if that
137872  ** information is needed */
137873  if( chngPk || hasFK || pTrigger ){
137874    u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
137875    oldmask |= sqlite3TriggerColmask(pParse,
137876        pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
137877    );
137878    for(i=0; i<pTab->nCol; i++){
137879      u32 colFlags = pTab->aCol[i].colFlags;
137880      k = sqlite3TableColumnToStorage(pTab, i) + regOld;
137881      if( oldmask==0xffffffff
137882       || (i<32 && (oldmask & MASKBIT32(i))!=0)
137883       || (colFlags & COLFLAG_PRIMKEY)!=0
137884      ){
137885        testcase(  oldmask!=0xffffffff && i==31 );
137886        sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);
137887      }else{
137888        sqlite3VdbeAddOp2(v, OP_Null, 0, k);
137889      }
137890    }
137891    if( chngRowid==0 && pPk==0 ){
137892      sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
137893    }
137894  }
137895
137896  /* Populate the array of registers beginning at regNew with the new
137897  ** row data. This array is used to check constants, create the new
137898  ** table and index records, and as the values for any new.* references
137899  ** made by triggers.
137900  **
137901  ** If there are one or more BEFORE triggers, then do not populate the
137902  ** registers associated with columns that are (a) not modified by
137903  ** this UPDATE statement and (b) not accessed by new.* references. The
137904  ** values for registers not modified by the UPDATE must be reloaded from
137905  ** the database after the BEFORE triggers are fired anyway (as the trigger
137906  ** may have modified them). So not loading those that are not going to
137907  ** be used eliminates some redundant opcodes.
137908  */
137909  newmask = sqlite3TriggerColmask(
137910      pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
137911  );
137912  for(i=0, k=regNew; i<pTab->nCol; i++, k++){
137913    if( i==pTab->iPKey ){
137914      sqlite3VdbeAddOp2(v, OP_Null, 0, k);
137915    }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)!=0 ){
137916      if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
137917    }else{
137918      j = aXRef[i];
137919      if( j>=0 ){
137920        sqlite3ExprCode(pParse, pChanges->a[j].pExpr, k);
137921      }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){
137922        /* This branch loads the value of a column that will not be changed
137923        ** into a register. This is done if there are no BEFORE triggers, or
137924        ** if there are one or more BEFORE triggers that use this value via
137925        ** a new.* reference in a trigger program.
137926        */
137927        testcase( i==31 );
137928        testcase( i==32 );
137929        sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);
137930        bFinishSeek = 0;
137931      }else{
137932        sqlite3VdbeAddOp2(v, OP_Null, 0, k);
137933      }
137934    }
137935  }
137936#ifndef SQLITE_OMIT_GENERATED_COLUMNS
137937  if( pTab->tabFlags & TF_HasGenerated ){
137938    testcase( pTab->tabFlags & TF_HasVirtual );
137939    testcase( pTab->tabFlags & TF_HasStored );
137940    sqlite3ComputeGeneratedColumns(pParse, regNew, pTab);
137941  }
137942#endif
137943
137944  /* Fire any BEFORE UPDATE triggers. This happens before constraints are
137945  ** verified. One could argue that this is wrong.
137946  */
137947  if( tmask&TRIGGER_BEFORE ){
137948    sqlite3TableAffinity(v, pTab, regNew);
137949    sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
137950        TRIGGER_BEFORE, pTab, regOldRowid, onError, labelContinue);
137951
137952    /* The row-trigger may have deleted the row being updated. In this
137953    ** case, jump to the next row. No updates or AFTER triggers are
137954    ** required. This behavior - what happens when the row being updated
137955    ** is deleted or renamed by a BEFORE trigger - is left undefined in the
137956    ** documentation.
137957    */
137958    if( pPk ){
137959      sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue,regKey,nKey);
137960      VdbeCoverage(v);
137961    }else{
137962      sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
137963      VdbeCoverage(v);
137964    }
137965
137966    /* After-BEFORE-trigger-reload-loop:
137967    ** If it did not delete it, the BEFORE trigger may still have modified
137968    ** some of the columns of the row being updated. Load the values for
137969    ** all columns not modified by the update statement into their registers
137970    ** in case this has happened. Only unmodified columns are reloaded.
137971    ** The values computed for modified columns use the values before the
137972    ** BEFORE trigger runs.  See test case trigger1-18.0 (added 2018-04-26)
137973    ** for an example.
137974    */
137975    for(i=0, k=regNew; i<pTab->nCol; i++, k++){
137976      if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
137977        if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
137978      }else if( aXRef[i]<0 && i!=pTab->iPKey ){
137979        sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);
137980      }
137981    }
137982#ifndef SQLITE_OMIT_GENERATED_COLUMNS
137983    if( pTab->tabFlags & TF_HasGenerated ){
137984      testcase( pTab->tabFlags & TF_HasVirtual );
137985      testcase( pTab->tabFlags & TF_HasStored );
137986      sqlite3ComputeGeneratedColumns(pParse, regNew, pTab);
137987    }
137988#endif
137989  }
137990
137991  if( !isView ){
137992    /* Do constraint checks. */
137993    assert( regOldRowid>0 );
137994    sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
137995        regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
137996        aXRef, 0);
137997
137998    /* If REPLACE conflict handling may have been used, or if the PK of the
137999    ** row is changing, then the GenerateConstraintChecks() above may have
138000    ** moved cursor iDataCur. Reseek it. */
138001    if( bReplace || chngKey ){
138002      if( pPk ){
138003        sqlite3VdbeAddOp4Int(v, OP_NotFound,iDataCur,labelContinue,regKey,nKey);
138004      }else{
138005        sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue,regOldRowid);
138006      }
138007      VdbeCoverageNeverTaken(v);
138008    }
138009
138010    /* Do FK constraint checks. */
138011    if( hasFK ){
138012      sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
138013    }
138014
138015    /* Delete the index entries associated with the current record.  */
138016    sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
138017
138018    /* We must run the OP_FinishSeek opcode to resolve a prior
138019    ** OP_DeferredSeek if there is any possibility that there have been
138020    ** no OP_Column opcodes since the OP_DeferredSeek was issued.  But
138021    ** we want to avoid the OP_FinishSeek if possible, as running it
138022    ** costs CPU cycles. */
138023    if( bFinishSeek ){
138024      sqlite3VdbeAddOp1(v, OP_FinishSeek, iDataCur);
138025    }
138026
138027    /* If changing the rowid value, or if there are foreign key constraints
138028    ** to process, delete the old record. Otherwise, add a noop OP_Delete
138029    ** to invoke the pre-update hook.
138030    **
138031    ** That (regNew==regnewRowid+1) is true is also important for the
138032    ** pre-update hook. If the caller invokes preupdate_new(), the returned
138033    ** value is copied from memory cell (regNewRowid+1+iCol), where iCol
138034    ** is the column index supplied by the user.
138035    */
138036    assert( regNew==regNewRowid+1 );
138037#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
138038    sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,
138039        OPFLAG_ISUPDATE | ((hasFK>1 || chngKey) ? 0 : OPFLAG_ISNOOP),
138040        regNewRowid
138041    );
138042    if( eOnePass==ONEPASS_MULTI ){
138043      assert( hasFK==0 && chngKey==0 );
138044      sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
138045    }
138046    if( !pParse->nested ){
138047      sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
138048    }
138049#else
138050    if( hasFK>1 || chngKey ){
138051      sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
138052    }
138053#endif
138054
138055    if( hasFK ){
138056      sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
138057    }
138058
138059    /* Insert the new index entries and the new record. */
138060    sqlite3CompleteInsertion(
138061        pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx,
138062        OPFLAG_ISUPDATE | (eOnePass==ONEPASS_MULTI ? OPFLAG_SAVEPOSITION : 0),
138063        0, 0
138064    );
138065
138066    /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
138067    ** handle rows (possibly in other tables) that refer via a foreign key
138068    ** to the row just updated. */
138069    if( hasFK ){
138070      sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
138071    }
138072  }
138073
138074  /* Increment the row counter
138075  */
138076  if( regRowCount ){
138077    sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
138078  }
138079
138080  sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
138081      TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
138082
138083  /* Repeat the above with the next record to be updated, until
138084  ** all record selected by the WHERE clause have been updated.
138085  */
138086  if( eOnePass==ONEPASS_SINGLE ){
138087    /* Nothing to do at end-of-loop for a single-pass */
138088  }else if( eOnePass==ONEPASS_MULTI ){
138089    sqlite3VdbeResolveLabel(v, labelContinue);
138090    sqlite3WhereEnd(pWInfo);
138091  }else if( pPk ){
138092    sqlite3VdbeResolveLabel(v, labelContinue);
138093    sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
138094  }else{
138095    sqlite3VdbeGoto(v, labelContinue);
138096  }
138097  sqlite3VdbeResolveLabel(v, labelBreak);
138098
138099  /* Update the sqlite_sequence table by storing the content of the
138100  ** maximum rowid counter values recorded while inserting into
138101  ** autoincrement tables.
138102  */
138103  if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
138104    sqlite3AutoincrementEnd(pParse);
138105  }
138106
138107  /*
138108  ** Return the number of rows that were changed, if we are tracking
138109  ** that information.
138110  */
138111  if( regRowCount ){
138112    sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
138113    sqlite3VdbeSetNumCols(v, 1);
138114    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
138115  }
138116
138117update_cleanup:
138118  sqlite3AuthContextPop(&sContext);
138119  sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
138120  sqlite3SrcListDelete(db, pTabList);
138121  sqlite3ExprListDelete(db, pChanges);
138122  sqlite3ExprDelete(db, pWhere);
138123#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
138124  sqlite3ExprListDelete(db, pOrderBy);
138125  sqlite3ExprDelete(db, pLimit);
138126#endif
138127  return;
138128}
138129/* Make sure "isView" and other macros defined above are undefined. Otherwise
138130** they may interfere with compilation of other functions in this file
138131** (or in another file, if this file becomes part of the amalgamation).  */
138132#ifdef isView
138133 #undef isView
138134#endif
138135#ifdef pTrigger
138136 #undef pTrigger
138137#endif
138138
138139#ifndef SQLITE_OMIT_VIRTUALTABLE
138140/*
138141** Generate code for an UPDATE of a virtual table.
138142**
138143** There are two possible strategies - the default and the special
138144** "onepass" strategy. Onepass is only used if the virtual table
138145** implementation indicates that pWhere may match at most one row.
138146**
138147** The default strategy is to create an ephemeral table that contains
138148** for each row to be changed:
138149**
138150**   (A)  The original rowid of that row.
138151**   (B)  The revised rowid for the row.
138152**   (C)  The content of every column in the row.
138153**
138154** Then loop through the contents of this ephemeral table executing a
138155** VUpdate for each row. When finished, drop the ephemeral table.
138156**
138157** The "onepass" strategy does not use an ephemeral table. Instead, it
138158** stores the same values (A, B and C above) in a register array and
138159** makes a single invocation of VUpdate.
138160*/
138161static void updateVirtualTable(
138162  Parse *pParse,       /* The parsing context */
138163  SrcList *pSrc,       /* The virtual table to be modified */
138164  Table *pTab,         /* The virtual table */
138165  ExprList *pChanges,  /* The columns to change in the UPDATE statement */
138166  Expr *pRowid,        /* Expression used to recompute the rowid */
138167  int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
138168  Expr *pWhere,        /* WHERE clause of the UPDATE statement */
138169  int onError          /* ON CONFLICT strategy */
138170){
138171  Vdbe *v = pParse->pVdbe;  /* Virtual machine under construction */
138172  int ephemTab;             /* Table holding the result of the SELECT */
138173  int i;                    /* Loop counter */
138174  sqlite3 *db = pParse->db; /* Database connection */
138175  const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
138176  WhereInfo *pWInfo;
138177  int nArg = 2 + pTab->nCol;      /* Number of arguments to VUpdate */
138178  int regArg;                     /* First register in VUpdate arg array */
138179  int regRec;                     /* Register in which to assemble record */
138180  int regRowid;                   /* Register for ephem table rowid */
138181  int iCsr = pSrc->a[0].iCursor;  /* Cursor used for virtual table scan */
138182  int aDummy[2];                  /* Unused arg for sqlite3WhereOkOnePass() */
138183  int eOnePass;                   /* True to use onepass strategy */
138184  int addr;                       /* Address of OP_OpenEphemeral */
138185
138186  /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
138187  ** create and open the ephemeral table in which the records created from
138188  ** these arguments will be temporarily stored. */
138189  assert( v );
138190  ephemTab = pParse->nTab++;
138191  addr= sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, nArg);
138192  regArg = pParse->nMem + 1;
138193  pParse->nMem += nArg;
138194  regRec = ++pParse->nMem;
138195  regRowid = ++pParse->nMem;
138196
138197  /* Start scanning the virtual table */
138198  pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0,0,WHERE_ONEPASS_DESIRED,0);
138199  if( pWInfo==0 ) return;
138200
138201  /* Populate the argument registers. */
138202  for(i=0; i<pTab->nCol; i++){
138203    assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 );
138204    if( aXRef[i]>=0 ){
138205      sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
138206    }else{
138207      sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, i, regArg+2+i);
138208      sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG);/* Enable sqlite3_vtab_nochange() */
138209    }
138210  }
138211  if( HasRowid(pTab) ){
138212    sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg);
138213    if( pRowid ){
138214      sqlite3ExprCode(pParse, pRowid, regArg+1);
138215    }else{
138216      sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg+1);
138217    }
138218  }else{
138219    Index *pPk;   /* PRIMARY KEY index */
138220    i16 iPk;      /* PRIMARY KEY column */
138221    pPk = sqlite3PrimaryKeyIndex(pTab);
138222    assert( pPk!=0 );
138223    assert( pPk->nKeyCol==1 );
138224    iPk = pPk->aiColumn[0];
138225    sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, iPk, regArg);
138226    sqlite3VdbeAddOp2(v, OP_SCopy, regArg+2+iPk, regArg+1);
138227  }
138228
138229  eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
138230
138231  /* There is no ONEPASS_MULTI on virtual tables */
138232  assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
138233
138234  if( eOnePass ){
138235    /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
138236    ** above. */
138237    sqlite3VdbeChangeToNoop(v, addr);
138238    sqlite3VdbeAddOp1(v, OP_Close, iCsr);
138239  }else{
138240    /* Create a record from the argument register contents and insert it into
138241    ** the ephemeral table. */
138242    sqlite3MultiWrite(pParse);
138243    sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec);
138244#ifdef SQLITE_DEBUG
138245    /* Signal an assert() within OP_MakeRecord that it is allowed to
138246    ** accept no-change records with serial_type 10 */
138247    sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC);
138248#endif
138249    sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid);
138250    sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid);
138251  }
138252
138253
138254  if( eOnePass==ONEPASS_OFF ){
138255    /* End the virtual table scan */
138256    sqlite3WhereEnd(pWInfo);
138257
138258    /* Begin scannning through the ephemeral table. */
138259    addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v);
138260
138261    /* Extract arguments from the current row of the ephemeral table and
138262    ** invoke the VUpdate method.  */
138263    for(i=0; i<nArg; i++){
138264      sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i, regArg+i);
138265    }
138266  }
138267  sqlite3VtabMakeWritable(pParse, pTab);
138268  sqlite3VdbeAddOp4(v, OP_VUpdate, 0, nArg, regArg, pVTab, P4_VTAB);
138269  sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
138270  sqlite3MayAbort(pParse);
138271
138272  /* End of the ephemeral table scan. Or, if using the onepass strategy,
138273  ** jump to here if the scan visited zero rows. */
138274  if( eOnePass==ONEPASS_OFF ){
138275    sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
138276    sqlite3VdbeJumpHere(v, addr);
138277    sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
138278  }else{
138279    sqlite3WhereEnd(pWInfo);
138280  }
138281}
138282#endif /* SQLITE_OMIT_VIRTUALTABLE */
138283
138284/************** End of update.c **********************************************/
138285/************** Begin file upsert.c ******************************************/
138286/*
138287** 2018-04-12
138288**
138289** The author disclaims copyright to this source code.  In place of
138290** a legal notice, here is a blessing:
138291**
138292**    May you do good and not evil.
138293**    May you find forgiveness for yourself and forgive others.
138294**    May you share freely, never taking more than you give.
138295**
138296*************************************************************************
138297** This file contains code to implement various aspects of UPSERT
138298** processing and handling of the Upsert object.
138299*/
138300/* #include "sqliteInt.h" */
138301
138302#ifndef SQLITE_OMIT_UPSERT
138303/*
138304** Free a list of Upsert objects
138305*/
138306SQLITE_PRIVATE void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
138307  if( p ){
138308    sqlite3ExprListDelete(db, p->pUpsertTarget);
138309    sqlite3ExprDelete(db, p->pUpsertTargetWhere);
138310    sqlite3ExprListDelete(db, p->pUpsertSet);
138311    sqlite3ExprDelete(db, p->pUpsertWhere);
138312    sqlite3DbFree(db, p);
138313  }
138314}
138315
138316/*
138317** Duplicate an Upsert object.
138318*/
138319SQLITE_PRIVATE Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
138320  if( p==0 ) return 0;
138321  return sqlite3UpsertNew(db,
138322           sqlite3ExprListDup(db, p->pUpsertTarget, 0),
138323           sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
138324           sqlite3ExprListDup(db, p->pUpsertSet, 0),
138325           sqlite3ExprDup(db, p->pUpsertWhere, 0)
138326         );
138327}
138328
138329/*
138330** Create a new Upsert object.
138331*/
138332SQLITE_PRIVATE Upsert *sqlite3UpsertNew(
138333  sqlite3 *db,           /* Determines which memory allocator to use */
138334  ExprList *pTarget,     /* Target argument to ON CONFLICT, or NULL */
138335  Expr *pTargetWhere,    /* Optional WHERE clause on the target */
138336  ExprList *pSet,        /* UPDATE columns, or NULL for a DO NOTHING */
138337  Expr *pWhere           /* WHERE clause for the ON CONFLICT UPDATE */
138338){
138339  Upsert *pNew;
138340  pNew = sqlite3DbMallocRaw(db, sizeof(Upsert));
138341  if( pNew==0 ){
138342    sqlite3ExprListDelete(db, pTarget);
138343    sqlite3ExprDelete(db, pTargetWhere);
138344    sqlite3ExprListDelete(db, pSet);
138345    sqlite3ExprDelete(db, pWhere);
138346    return 0;
138347  }else{
138348    pNew->pUpsertTarget = pTarget;
138349    pNew->pUpsertTargetWhere = pTargetWhere;
138350    pNew->pUpsertSet = pSet;
138351    pNew->pUpsertWhere = pWhere;
138352    pNew->pUpsertIdx = 0;
138353  }
138354  return pNew;
138355}
138356
138357/*
138358** Analyze the ON CONFLICT clause described by pUpsert.  Resolve all
138359** symbols in the conflict-target.
138360**
138361** Return SQLITE_OK if everything works, or an error code is something
138362** is wrong.
138363*/
138364SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(
138365  Parse *pParse,     /* The parsing context */
138366  SrcList *pTabList, /* Table into which we are inserting */
138367  Upsert *pUpsert    /* The ON CONFLICT clauses */
138368){
138369  Table *pTab;            /* That table into which we are inserting */
138370  int rc;                 /* Result code */
138371  int iCursor;            /* Cursor used by pTab */
138372  Index *pIdx;            /* One of the indexes of pTab */
138373  ExprList *pTarget;      /* The conflict-target clause */
138374  Expr *pTerm;            /* One term of the conflict-target clause */
138375  NameContext sNC;        /* Context for resolving symbolic names */
138376  Expr sCol[2];           /* Index column converted into an Expr */
138377
138378  assert( pTabList->nSrc==1 );
138379  assert( pTabList->a[0].pTab!=0 );
138380  assert( pUpsert!=0 );
138381  assert( pUpsert->pUpsertTarget!=0 );
138382
138383  /* Resolve all symbolic names in the conflict-target clause, which
138384  ** includes both the list of columns and the optional partial-index
138385  ** WHERE clause.
138386  */
138387  memset(&sNC, 0, sizeof(sNC));
138388  sNC.pParse = pParse;
138389  sNC.pSrcList = pTabList;
138390  rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
138391  if( rc ) return rc;
138392  rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
138393  if( rc ) return rc;
138394
138395  /* Check to see if the conflict target matches the rowid. */
138396  pTab = pTabList->a[0].pTab;
138397  pTarget = pUpsert->pUpsertTarget;
138398  iCursor = pTabList->a[0].iCursor;
138399  if( HasRowid(pTab)
138400   && pTarget->nExpr==1
138401   && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
138402   && pTerm->iColumn==XN_ROWID
138403  ){
138404    /* The conflict-target is the rowid of the primary table */
138405    assert( pUpsert->pUpsertIdx==0 );
138406    return SQLITE_OK;
138407  }
138408
138409  /* Initialize sCol[0..1] to be an expression parse tree for a
138410  ** single column of an index.  The sCol[0] node will be the TK_COLLATE
138411  ** operator and sCol[1] will be the TK_COLUMN operator.  Code below
138412  ** will populate the specific collation and column number values
138413  ** prior to comparing against the conflict-target expression.
138414  */
138415  memset(sCol, 0, sizeof(sCol));
138416  sCol[0].op = TK_COLLATE;
138417  sCol[0].pLeft = &sCol[1];
138418  sCol[1].op = TK_COLUMN;
138419  sCol[1].iTable = pTabList->a[0].iCursor;
138420
138421  /* Check for matches against other indexes */
138422  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
138423    int ii, jj, nn;
138424    if( !IsUniqueIndex(pIdx) ) continue;
138425    if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
138426    if( pIdx->pPartIdxWhere ){
138427      if( pUpsert->pUpsertTargetWhere==0 ) continue;
138428      if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
138429                             pIdx->pPartIdxWhere, iCursor)!=0 ){
138430        continue;
138431      }
138432    }
138433    nn = pIdx->nKeyCol;
138434    for(ii=0; ii<nn; ii++){
138435      Expr *pExpr;
138436      sCol[0].u.zToken = (char*)pIdx->azColl[ii];
138437      if( pIdx->aiColumn[ii]==XN_EXPR ){
138438        assert( pIdx->aColExpr!=0 );
138439        assert( pIdx->aColExpr->nExpr>ii );
138440        pExpr = pIdx->aColExpr->a[ii].pExpr;
138441        if( pExpr->op!=TK_COLLATE ){
138442          sCol[0].pLeft = pExpr;
138443          pExpr = &sCol[0];
138444        }
138445      }else{
138446        sCol[0].pLeft = &sCol[1];
138447        sCol[1].iColumn = pIdx->aiColumn[ii];
138448        pExpr = &sCol[0];
138449      }
138450      for(jj=0; jj<nn; jj++){
138451        if( sqlite3ExprCompare(pParse, pTarget->a[jj].pExpr, pExpr,iCursor)<2 ){
138452          break;  /* Column ii of the index matches column jj of target */
138453        }
138454      }
138455      if( jj>=nn ){
138456        /* The target contains no match for column jj of the index */
138457        break;
138458      }
138459    }
138460    if( ii<nn ){
138461      /* Column ii of the index did not match any term of the conflict target.
138462      ** Continue the search with the next index. */
138463      continue;
138464    }
138465    pUpsert->pUpsertIdx = pIdx;
138466    return SQLITE_OK;
138467  }
138468  sqlite3ErrorMsg(pParse, "ON CONFLICT clause does not match any "
138469                          "PRIMARY KEY or UNIQUE constraint");
138470  return SQLITE_ERROR;
138471}
138472
138473/*
138474** Generate bytecode that does an UPDATE as part of an upsert.
138475**
138476** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK.
138477** In this case parameter iCur is a cursor open on the table b-tree that
138478** currently points to the conflicting table row. Otherwise, if pIdx
138479** is not NULL, then pIdx is the constraint that failed and iCur is a
138480** cursor points to the conflicting row.
138481*/
138482SQLITE_PRIVATE void sqlite3UpsertDoUpdate(
138483  Parse *pParse,        /* The parsing and code-generating context */
138484  Upsert *pUpsert,      /* The ON CONFLICT clause for the upsert */
138485  Table *pTab,          /* The table being updated */
138486  Index *pIdx,          /* The UNIQUE constraint that failed */
138487  int iCur              /* Cursor for pIdx (or pTab if pIdx==NULL) */
138488){
138489  Vdbe *v = pParse->pVdbe;
138490  sqlite3 *db = pParse->db;
138491  SrcList *pSrc;            /* FROM clause for the UPDATE */
138492  int iDataCur;
138493  int i;
138494
138495  assert( v!=0 );
138496  assert( pUpsert!=0 );
138497  VdbeNoopComment((v, "Begin DO UPDATE of UPSERT"));
138498  iDataCur = pUpsert->iDataCur;
138499  if( pIdx && iCur!=iDataCur ){
138500    if( HasRowid(pTab) ){
138501      int regRowid = sqlite3GetTempReg(pParse);
138502      sqlite3VdbeAddOp2(v, OP_IdxRowid, iCur, regRowid);
138503      sqlite3VdbeAddOp3(v, OP_SeekRowid, iDataCur, 0, regRowid);
138504      VdbeCoverage(v);
138505      sqlite3ReleaseTempReg(pParse, regRowid);
138506    }else{
138507      Index *pPk = sqlite3PrimaryKeyIndex(pTab);
138508      int nPk = pPk->nKeyCol;
138509      int iPk = pParse->nMem+1;
138510      pParse->nMem += nPk;
138511      for(i=0; i<nPk; i++){
138512        int k;
138513        assert( pPk->aiColumn[i]>=0 );
138514        k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
138515        sqlite3VdbeAddOp3(v, OP_Column, iCur, k, iPk+i);
138516        VdbeComment((v, "%s.%s", pIdx->zName,
138517                    pTab->aCol[pPk->aiColumn[i]].zName));
138518      }
138519      sqlite3VdbeVerifyAbortable(v, OE_Abort);
138520      i = sqlite3VdbeAddOp4Int(v, OP_Found, iDataCur, 0, iPk, nPk);
138521      VdbeCoverage(v);
138522      sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CORRUPT, OE_Abort, 0,
138523            "corrupt database", P4_STATIC);
138524      sqlite3MayAbort(pParse);
138525      sqlite3VdbeJumpHere(v, i);
138526    }
138527  }
138528  /* pUpsert does not own pUpsertSrc - the outer INSERT statement does.  So
138529  ** we have to make a copy before passing it down into sqlite3Update() */
138530  pSrc = sqlite3SrcListDup(db, pUpsert->pUpsertSrc, 0);
138531  /* excluded.* columns of type REAL need to be converted to a hard real */
138532  for(i=0; i<pTab->nCol; i++){
138533    if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
138534      sqlite3VdbeAddOp1(v, OP_RealAffinity, pUpsert->regData+i);
138535    }
138536  }
138537  sqlite3Update(pParse, pSrc, pUpsert->pUpsertSet,
138538      pUpsert->pUpsertWhere, OE_Abort, 0, 0, pUpsert);
138539  pUpsert->pUpsertSet = 0;    /* Will have been deleted by sqlite3Update() */
138540  pUpsert->pUpsertWhere = 0;  /* Will have been deleted by sqlite3Update() */
138541  VdbeNoopComment((v, "End DO UPDATE of UPSERT"));
138542}
138543
138544#endif /* SQLITE_OMIT_UPSERT */
138545
138546/************** End of upsert.c **********************************************/
138547/************** Begin file vacuum.c ******************************************/
138548/*
138549** 2003 April 6
138550**
138551** The author disclaims copyright to this source code.  In place of
138552** a legal notice, here is a blessing:
138553**
138554**    May you do good and not evil.
138555**    May you find forgiveness for yourself and forgive others.
138556**    May you share freely, never taking more than you give.
138557**
138558*************************************************************************
138559** This file contains code used to implement the VACUUM command.
138560**
138561** Most of the code in this file may be omitted by defining the
138562** SQLITE_OMIT_VACUUM macro.
138563*/
138564/* #include "sqliteInt.h" */
138565/* #include "vdbeInt.h" */
138566
138567#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
138568
138569/*
138570** Execute zSql on database db.
138571**
138572** If zSql returns rows, then each row will have exactly one
138573** column.  (This will only happen if zSql begins with "SELECT".)
138574** Take each row of result and call execSql() again recursively.
138575**
138576** The execSqlF() routine does the same thing, except it accepts
138577** a format string as its third argument
138578*/
138579static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
138580  sqlite3_stmt *pStmt;
138581  int rc;
138582
138583  /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
138584  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
138585  if( rc!=SQLITE_OK ) return rc;
138586  while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
138587    const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
138588    assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
138589    /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
138590    ** or INSERT.  Historically there have been attacks that first
138591    ** corrupt the sqlite_master.sql field with other kinds of statements
138592    ** then run VACUUM to get those statements to execute at inappropriate
138593    ** times. */
138594    if( zSubSql
138595     && (strncmp(zSubSql,"CRE",3)==0 || strncmp(zSubSql,"INS",3)==0)
138596    ){
138597      rc = execSql(db, pzErrMsg, zSubSql);
138598      if( rc!=SQLITE_OK ) break;
138599    }
138600  }
138601  assert( rc!=SQLITE_ROW );
138602  if( rc==SQLITE_DONE ) rc = SQLITE_OK;
138603  if( rc ){
138604    sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
138605  }
138606  (void)sqlite3_finalize(pStmt);
138607  return rc;
138608}
138609static int execSqlF(sqlite3 *db, char **pzErrMsg, const char *zSql, ...){
138610  char *z;
138611  va_list ap;
138612  int rc;
138613  va_start(ap, zSql);
138614  z = sqlite3VMPrintf(db, zSql, ap);
138615  va_end(ap);
138616  if( z==0 ) return SQLITE_NOMEM;
138617  rc = execSql(db, pzErrMsg, z);
138618  sqlite3DbFree(db, z);
138619  return rc;
138620}
138621
138622/*
138623** The VACUUM command is used to clean up the database,
138624** collapse free space, etc.  It is modelled after the VACUUM command
138625** in PostgreSQL.  The VACUUM command works as follows:
138626**
138627**   (1)  Create a new transient database file
138628**   (2)  Copy all content from the database being vacuumed into
138629**        the new transient database file
138630**   (3)  Copy content from the transient database back into the
138631**        original database.
138632**
138633** The transient database requires temporary disk space approximately
138634** equal to the size of the original database.  The copy operation of
138635** step (3) requires additional temporary disk space approximately equal
138636** to the size of the original database for the rollback journal.
138637** Hence, temporary disk space that is approximately 2x the size of the
138638** original database is required.  Every page of the database is written
138639** approximately 3 times:  Once for step (2) and twice for step (3).
138640** Two writes per page are required in step (3) because the original
138641** database content must be written into the rollback journal prior to
138642** overwriting the database with the vacuumed content.
138643**
138644** Only 1x temporary space and only 1x writes would be required if
138645** the copy of step (3) were replaced by deleting the original database
138646** and renaming the transient database as the original.  But that will
138647** not work if other processes are attached to the original database.
138648** And a power loss in between deleting the original and renaming the
138649** transient would cause the database file to appear to be deleted
138650** following reboot.
138651*/
138652SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse, Token *pNm, Expr *pInto){
138653  Vdbe *v = sqlite3GetVdbe(pParse);
138654  int iDb = 0;
138655  if( v==0 ) goto build_vacuum_end;
138656  if( pParse->nErr ) goto build_vacuum_end;
138657  if( pNm ){
138658#ifndef SQLITE_BUG_COMPATIBLE_20160819
138659    /* Default behavior:  Report an error if the argument to VACUUM is
138660    ** not recognized */
138661    iDb = sqlite3TwoPartName(pParse, pNm, pNm, &pNm);
138662    if( iDb<0 ) goto build_vacuum_end;
138663#else
138664    /* When SQLITE_BUG_COMPATIBLE_20160819 is defined, unrecognized arguments
138665    ** to VACUUM are silently ignored.  This is a back-out of a bug fix that
138666    ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
138667    ** The buggy behavior is required for binary compatibility with some
138668    ** legacy applications. */
138669    iDb = sqlite3FindDb(pParse->db, pNm);
138670    if( iDb<0 ) iDb = 0;
138671#endif
138672  }
138673  if( iDb!=1 ){
138674    int iIntoReg = 0;
138675    if( pInto && sqlite3ResolveSelfReference(pParse,0,0,pInto,0)==0 ){
138676      iIntoReg = ++pParse->nMem;
138677      sqlite3ExprCode(pParse, pInto, iIntoReg);
138678    }
138679    sqlite3VdbeAddOp2(v, OP_Vacuum, iDb, iIntoReg);
138680    sqlite3VdbeUsesBtree(v, iDb);
138681  }
138682build_vacuum_end:
138683  sqlite3ExprDelete(pParse->db, pInto);
138684  return;
138685}
138686
138687/*
138688** This routine implements the OP_Vacuum opcode of the VDBE.
138689*/
138690SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3RunVacuum(
138691  char **pzErrMsg,        /* Write error message here */
138692  sqlite3 *db,            /* Database connection */
138693  int iDb,                /* Which attached DB to vacuum */
138694  sqlite3_value *pOut     /* Write results here, if not NULL. VACUUM INTO */
138695){
138696  int rc = SQLITE_OK;     /* Return code from service routines */
138697  Btree *pMain;           /* The database being vacuumed */
138698  Btree *pTemp;           /* The temporary database we vacuum into */
138699  u32 saved_mDbFlags;     /* Saved value of db->mDbFlags */
138700  u64 saved_flags;        /* Saved value of db->flags */
138701  int saved_nChange;      /* Saved value of db->nChange */
138702  int saved_nTotalChange; /* Saved value of db->nTotalChange */
138703  u32 saved_openFlags;    /* Saved value of db->openFlags */
138704  u8 saved_mTrace;        /* Saved trace settings */
138705  Db *pDb = 0;            /* Database to detach at end of vacuum */
138706  int isMemDb;            /* True if vacuuming a :memory: database */
138707  int nRes;               /* Bytes of reserved space at the end of each page */
138708  int nDb;                /* Number of attached databases */
138709  const char *zDbMain;    /* Schema name of database to vacuum */
138710  const char *zOut;       /* Name of output file */
138711
138712  if( !db->autoCommit ){
138713    sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
138714    return SQLITE_ERROR; /* IMP: R-12218-18073 */
138715  }
138716  if( db->nVdbeActive>1 ){
138717    sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
138718    return SQLITE_ERROR; /* IMP: R-15610-35227 */
138719  }
138720  saved_openFlags = db->openFlags;
138721  if( pOut ){
138722    if( sqlite3_value_type(pOut)!=SQLITE_TEXT ){
138723      sqlite3SetString(pzErrMsg, db, "non-text filename");
138724      return SQLITE_ERROR;
138725    }
138726    zOut = (const char*)sqlite3_value_text(pOut);
138727    db->openFlags &= ~SQLITE_OPEN_READONLY;
138728    db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
138729  }else{
138730    zOut = "";
138731  }
138732
138733  /* Save the current value of the database flags so that it can be
138734  ** restored before returning. Then set the writable-schema flag, and
138735  ** disable CHECK and foreign key constraints.  */
138736  saved_flags = db->flags;
138737  saved_mDbFlags = db->mDbFlags;
138738  saved_nChange = db->nChange;
138739  saved_nTotalChange = db->nTotalChange;
138740  saved_mTrace = db->mTrace;
138741  db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
138742  db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
138743  db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
138744                   | SQLITE_Defensive | SQLITE_CountRows);
138745  db->mTrace = 0;
138746
138747  zDbMain = db->aDb[iDb].zDbSName;
138748  pMain = db->aDb[iDb].pBt;
138749  isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
138750
138751  /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
138752  ** can be set to 'off' for this file, as it is not recovered if a crash
138753  ** occurs anyway. The integrity of the database is maintained by a
138754  ** (possibly synchronous) transaction opened on the main database before
138755  ** sqlite3BtreeCopyFile() is called.
138756  **
138757  ** An optimisation would be to use a non-journaled pager.
138758  ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
138759  ** that actually made the VACUUM run slower.  Very little journalling
138760  ** actually occurs when doing a vacuum since the vacuum_db is initially
138761  ** empty.  Only the journal header is written.  Apparently it takes more
138762  ** time to parse and run the PRAGMA to turn journalling off than it does
138763  ** to write the journal header file.
138764  */
138765  nDb = db->nDb;
138766  rc = execSqlF(db, pzErrMsg, "ATTACH %Q AS vacuum_db", zOut);
138767  db->openFlags = saved_openFlags;
138768  if( rc!=SQLITE_OK ) goto end_of_vacuum;
138769  assert( (db->nDb-1)==nDb );
138770  pDb = &db->aDb[nDb];
138771  assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
138772  pTemp = pDb->pBt;
138773  if( pOut ){
138774    sqlite3_file *id = sqlite3PagerFile(sqlite3BtreePager(pTemp));
138775    i64 sz = 0;
138776    if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
138777      rc = SQLITE_ERROR;
138778      sqlite3SetString(pzErrMsg, db, "output file already exists");
138779      goto end_of_vacuum;
138780    }
138781    db->mDbFlags |= DBFLAG_VacuumInto;
138782  }
138783  nRes = sqlite3BtreeGetRequestedReserve(pMain);
138784
138785  sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
138786  sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
138787  sqlite3BtreeSetPagerFlags(pTemp, PAGER_SYNCHRONOUS_OFF|PAGER_CACHESPILL);
138788
138789  /* Begin a transaction and take an exclusive lock on the main database
138790  ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
138791  ** to ensure that we do not try to change the page-size on a WAL database.
138792  */
138793  rc = execSql(db, pzErrMsg, "BEGIN");
138794  if( rc!=SQLITE_OK ) goto end_of_vacuum;
138795  rc = sqlite3BtreeBeginTrans(pMain, pOut==0 ? 2 : 0, 0);
138796  if( rc!=SQLITE_OK ) goto end_of_vacuum;
138797
138798  /* Do not attempt to change the page size for a WAL database */
138799  if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
138800                                               ==PAGER_JOURNALMODE_WAL ){
138801    db->nextPagesize = 0;
138802  }
138803
138804  if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
138805   || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
138806   || NEVER(db->mallocFailed)
138807  ){
138808    rc = SQLITE_NOMEM_BKPT;
138809    goto end_of_vacuum;
138810  }
138811
138812#ifndef SQLITE_OMIT_AUTOVACUUM
138813  sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
138814                                           sqlite3BtreeGetAutoVacuum(pMain));
138815#endif
138816
138817  /* Query the schema of the main database. Create a mirror schema
138818  ** in the temporary database.
138819  */
138820  db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
138821  rc = execSqlF(db, pzErrMsg,
138822      "SELECT sql FROM \"%w\".sqlite_master"
138823      " WHERE type='table'AND name<>'sqlite_sequence'"
138824      " AND coalesce(rootpage,1)>0",
138825      zDbMain
138826  );
138827  if( rc!=SQLITE_OK ) goto end_of_vacuum;
138828  rc = execSqlF(db, pzErrMsg,
138829      "SELECT sql FROM \"%w\".sqlite_master"
138830      " WHERE type='index'",
138831      zDbMain
138832  );
138833  if( rc!=SQLITE_OK ) goto end_of_vacuum;
138834  db->init.iDb = 0;
138835
138836  /* Loop through the tables in the main database. For each, do
138837  ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
138838  ** the contents to the temporary database.
138839  */
138840  rc = execSqlF(db, pzErrMsg,
138841      "SELECT'INSERT INTO vacuum_db.'||quote(name)"
138842      "||' SELECT*FROM\"%w\".'||quote(name)"
138843      "FROM vacuum_db.sqlite_master "
138844      "WHERE type='table'AND coalesce(rootpage,1)>0",
138845      zDbMain
138846  );
138847  assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
138848  db->mDbFlags &= ~DBFLAG_Vacuum;
138849  if( rc!=SQLITE_OK ) goto end_of_vacuum;
138850
138851  /* Copy the triggers, views, and virtual tables from the main database
138852  ** over to the temporary database.  None of these objects has any
138853  ** associated storage, so all we have to do is copy their entries
138854  ** from the SQLITE_MASTER table.
138855  */
138856  rc = execSqlF(db, pzErrMsg,
138857      "INSERT INTO vacuum_db.sqlite_master"
138858      " SELECT*FROM \"%w\".sqlite_master"
138859      " WHERE type IN('view','trigger')"
138860      " OR(type='table'AND rootpage=0)",
138861      zDbMain
138862  );
138863  if( rc ) goto end_of_vacuum;
138864
138865  /* At this point, there is a write transaction open on both the
138866  ** vacuum database and the main database. Assuming no error occurs,
138867  ** both transactions are closed by this block - the main database
138868  ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
138869  ** call to sqlite3BtreeCommit().
138870  */
138871  {
138872    u32 meta;
138873    int i;
138874
138875    /* This array determines which meta meta values are preserved in the
138876    ** vacuum.  Even entries are the meta value number and odd entries
138877    ** are an increment to apply to the meta value after the vacuum.
138878    ** The increment is used to increase the schema cookie so that other
138879    ** connections to the same database will know to reread the schema.
138880    */
138881    static const unsigned char aCopy[] = {
138882       BTREE_SCHEMA_VERSION,     1,  /* Add one to the old schema cookie */
138883       BTREE_DEFAULT_CACHE_SIZE, 0,  /* Preserve the default page cache size */
138884       BTREE_TEXT_ENCODING,      0,  /* Preserve the text encoding */
138885       BTREE_USER_VERSION,       0,  /* Preserve the user version */
138886       BTREE_APPLICATION_ID,     0,  /* Preserve the application id */
138887    };
138888
138889    assert( 1==sqlite3BtreeIsInTrans(pTemp) );
138890    assert( pOut!=0 || 1==sqlite3BtreeIsInTrans(pMain) );
138891
138892    /* Copy Btree meta values */
138893    for(i=0; i<ArraySize(aCopy); i+=2){
138894      /* GetMeta() and UpdateMeta() cannot fail in this context because
138895      ** we already have page 1 loaded into cache and marked dirty. */
138896      sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
138897      rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
138898      if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
138899    }
138900
138901    if( pOut==0 ){
138902      rc = sqlite3BtreeCopyFile(pMain, pTemp);
138903    }
138904    if( rc!=SQLITE_OK ) goto end_of_vacuum;
138905    rc = sqlite3BtreeCommit(pTemp);
138906    if( rc!=SQLITE_OK ) goto end_of_vacuum;
138907#ifndef SQLITE_OMIT_AUTOVACUUM
138908    if( pOut==0 ){
138909      sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
138910    }
138911#endif
138912  }
138913
138914  assert( rc==SQLITE_OK );
138915  if( pOut==0 ){
138916    rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
138917  }
138918
138919end_of_vacuum:
138920  /* Restore the original value of db->flags */
138921  db->init.iDb = 0;
138922  db->mDbFlags = saved_mDbFlags;
138923  db->flags = saved_flags;
138924  db->nChange = saved_nChange;
138925  db->nTotalChange = saved_nTotalChange;
138926  db->mTrace = saved_mTrace;
138927  sqlite3BtreeSetPageSize(pMain, -1, 0, 1);
138928
138929  /* Currently there is an SQL level transaction open on the vacuum
138930  ** database. No locks are held on any other files (since the main file
138931  ** was committed at the btree level). So it safe to end the transaction
138932  ** by manually setting the autoCommit flag to true and detaching the
138933  ** vacuum database. The vacuum_db journal file is deleted when the pager
138934  ** is closed by the DETACH.
138935  */
138936  db->autoCommit = 1;
138937
138938  if( pDb ){
138939    sqlite3BtreeClose(pDb->pBt);
138940    pDb->pBt = 0;
138941    pDb->pSchema = 0;
138942  }
138943
138944  /* This both clears the schemas and reduces the size of the db->aDb[]
138945  ** array. */
138946  sqlite3ResetAllSchemasOfConnection(db);
138947
138948  return rc;
138949}
138950
138951#endif  /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
138952
138953/************** End of vacuum.c **********************************************/
138954/************** Begin file vtab.c ********************************************/
138955/*
138956** 2006 June 10
138957**
138958** The author disclaims copyright to this source code.  In place of
138959** a legal notice, here is a blessing:
138960**
138961**    May you do good and not evil.
138962**    May you find forgiveness for yourself and forgive others.
138963**    May you share freely, never taking more than you give.
138964**
138965*************************************************************************
138966** This file contains code used to help implement virtual tables.
138967*/
138968#ifndef SQLITE_OMIT_VIRTUALTABLE
138969/* #include "sqliteInt.h" */
138970
138971/*
138972** Before a virtual table xCreate() or xConnect() method is invoked, the
138973** sqlite3.pVtabCtx member variable is set to point to an instance of
138974** this struct allocated on the stack. It is used by the implementation of
138975** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
138976** are invoked only from within xCreate and xConnect methods.
138977*/
138978struct VtabCtx {
138979  VTable *pVTable;    /* The virtual table being constructed */
138980  Table *pTab;        /* The Table object to which the virtual table belongs */
138981  VtabCtx *pPrior;    /* Parent context (if any) */
138982  int bDeclared;      /* True after sqlite3_declare_vtab() is called */
138983};
138984
138985/*
138986** Construct and install a Module object for a virtual table.  When this
138987** routine is called, it is guaranteed that all appropriate locks are held
138988** and the module is not already part of the connection.
138989**
138990** If there already exists a module with zName, replace it with the new one.
138991** If pModule==0, then delete the module zName if it exists.
138992*/
138993SQLITE_PRIVATE Module *sqlite3VtabCreateModule(
138994  sqlite3 *db,                    /* Database in which module is registered */
138995  const char *zName,              /* Name assigned to this module */
138996  const sqlite3_module *pModule,  /* The definition of the module */
138997  void *pAux,                     /* Context pointer for xCreate/xConnect */
138998  void (*xDestroy)(void *)        /* Module destructor function */
138999){
139000  Module *pMod;
139001  Module *pDel;
139002  char *zCopy;
139003  if( pModule==0 ){
139004    zCopy = (char*)zName;
139005    pMod = 0;
139006  }else{
139007    int nName = sqlite3Strlen30(zName);
139008    pMod = (Module *)sqlite3Malloc(sizeof(Module) + nName + 1);
139009    if( pMod==0 ){
139010      sqlite3OomFault(db);
139011      return 0;
139012    }
139013    zCopy = (char *)(&pMod[1]);
139014    memcpy(zCopy, zName, nName+1);
139015    pMod->zName = zCopy;
139016    pMod->pModule = pModule;
139017    pMod->pAux = pAux;
139018    pMod->xDestroy = xDestroy;
139019    pMod->pEpoTab = 0;
139020    pMod->nRefModule = 1;
139021  }
139022  pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
139023  if( pDel ){
139024    if( pDel==pMod ){
139025      sqlite3OomFault(db);
139026      sqlite3DbFree(db, pDel);
139027      pMod = 0;
139028    }else{
139029      sqlite3VtabEponymousTableClear(db, pDel);
139030      sqlite3VtabModuleUnref(db, pDel);
139031    }
139032  }
139033  return pMod;
139034}
139035
139036/*
139037** The actual function that does the work of creating a new module.
139038** This function implements the sqlite3_create_module() and
139039** sqlite3_create_module_v2() interfaces.
139040*/
139041static int createModule(
139042  sqlite3 *db,                    /* Database in which module is registered */
139043  const char *zName,              /* Name assigned to this module */
139044  const sqlite3_module *pModule,  /* The definition of the module */
139045  void *pAux,                     /* Context pointer for xCreate/xConnect */
139046  void (*xDestroy)(void *)        /* Module destructor function */
139047){
139048  int rc = SQLITE_OK;
139049
139050  sqlite3_mutex_enter(db->mutex);
139051  (void)sqlite3VtabCreateModule(db, zName, pModule, pAux, xDestroy);
139052  rc = sqlite3ApiExit(db, rc);
139053  if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
139054  sqlite3_mutex_leave(db->mutex);
139055  return rc;
139056}
139057
139058
139059/*
139060** External API function used to create a new virtual-table module.
139061*/
139062SQLITE_API int sqlite3_create_module(
139063  sqlite3 *db,                    /* Database in which module is registered */
139064  const char *zName,              /* Name assigned to this module */
139065  const sqlite3_module *pModule,  /* The definition of the module */
139066  void *pAux                      /* Context pointer for xCreate/xConnect */
139067){
139068#ifdef SQLITE_ENABLE_API_ARMOR
139069  if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
139070#endif
139071  return createModule(db, zName, pModule, pAux, 0);
139072}
139073
139074/*
139075** External API function used to create a new virtual-table module.
139076*/
139077SQLITE_API int sqlite3_create_module_v2(
139078  sqlite3 *db,                    /* Database in which module is registered */
139079  const char *zName,              /* Name assigned to this module */
139080  const sqlite3_module *pModule,  /* The definition of the module */
139081  void *pAux,                     /* Context pointer for xCreate/xConnect */
139082  void (*xDestroy)(void *)        /* Module destructor function */
139083){
139084#ifdef SQLITE_ENABLE_API_ARMOR
139085  if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
139086#endif
139087  return createModule(db, zName, pModule, pAux, xDestroy);
139088}
139089
139090/*
139091** External API to drop all virtual-table modules, except those named
139092** on the azNames list.
139093*/
139094SQLITE_API int sqlite3_drop_modules(sqlite3 *db, const char** azNames){
139095  HashElem *pThis, *pNext;
139096#ifdef SQLITE_ENABLE_API_ARMOR
139097  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
139098#endif
139099  for(pThis=sqliteHashFirst(&db->aModule); pThis; pThis=pNext){
139100    Module *pMod = (Module*)sqliteHashData(pThis);
139101    pNext = sqliteHashNext(pThis);
139102    if( azNames ){
139103      int ii;
139104      for(ii=0; azNames[ii]!=0 && strcmp(azNames[ii],pMod->zName)!=0; ii++){}
139105      if( azNames[ii]!=0 ) continue;
139106    }
139107    createModule(db, pMod->zName, 0, 0, 0);
139108  }
139109  return SQLITE_OK;
139110}
139111
139112/*
139113** Decrement the reference count on a Module object.  Destroy the
139114** module when the reference count reaches zero.
139115*/
139116SQLITE_PRIVATE void sqlite3VtabModuleUnref(sqlite3 *db, Module *pMod){
139117  assert( pMod->nRefModule>0 );
139118  pMod->nRefModule--;
139119  if( pMod->nRefModule==0 ){
139120    if( pMod->xDestroy ){
139121      pMod->xDestroy(pMod->pAux);
139122    }
139123    assert( pMod->pEpoTab==0 );
139124    sqlite3DbFree(db, pMod);
139125  }
139126}
139127
139128/*
139129** Lock the virtual table so that it cannot be disconnected.
139130** Locks nest.  Every lock should have a corresponding unlock.
139131** If an unlock is omitted, resources leaks will occur.
139132**
139133** If a disconnect is attempted while a virtual table is locked,
139134** the disconnect is deferred until all locks have been removed.
139135*/
139136SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
139137  pVTab->nRef++;
139138}
139139
139140
139141/*
139142** pTab is a pointer to a Table structure representing a virtual-table.
139143** Return a pointer to the VTable object used by connection db to access
139144** this virtual-table, if one has been created, or NULL otherwise.
139145*/
139146SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
139147  VTable *pVtab;
139148  assert( IsVirtual(pTab) );
139149  for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
139150  return pVtab;
139151}
139152
139153/*
139154** Decrement the ref-count on a virtual table object. When the ref-count
139155** reaches zero, call the xDisconnect() method to delete the object.
139156*/
139157SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
139158  sqlite3 *db = pVTab->db;
139159
139160  assert( db );
139161  assert( pVTab->nRef>0 );
139162  assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
139163
139164  pVTab->nRef--;
139165  if( pVTab->nRef==0 ){
139166    sqlite3_vtab *p = pVTab->pVtab;
139167    sqlite3VtabModuleUnref(pVTab->db, pVTab->pMod);
139168    if( p ){
139169      p->pModule->xDisconnect(p);
139170    }
139171    sqlite3DbFree(db, pVTab);
139172  }
139173}
139174
139175/*
139176** Table p is a virtual table. This function moves all elements in the
139177** p->pVTable list to the sqlite3.pDisconnect lists of their associated
139178** database connections to be disconnected at the next opportunity.
139179** Except, if argument db is not NULL, then the entry associated with
139180** connection db is left in the p->pVTable list.
139181*/
139182static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
139183  VTable *pRet = 0;
139184  VTable *pVTable = p->pVTable;
139185  p->pVTable = 0;
139186
139187  /* Assert that the mutex (if any) associated with the BtShared database
139188  ** that contains table p is held by the caller. See header comments
139189  ** above function sqlite3VtabUnlockList() for an explanation of why
139190  ** this makes it safe to access the sqlite3.pDisconnect list of any
139191  ** database connection that may have an entry in the p->pVTable list.
139192  */
139193  assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
139194
139195  while( pVTable ){
139196    sqlite3 *db2 = pVTable->db;
139197    VTable *pNext = pVTable->pNext;
139198    assert( db2 );
139199    if( db2==db ){
139200      pRet = pVTable;
139201      p->pVTable = pRet;
139202      pRet->pNext = 0;
139203    }else{
139204      pVTable->pNext = db2->pDisconnect;
139205      db2->pDisconnect = pVTable;
139206    }
139207    pVTable = pNext;
139208  }
139209
139210  assert( !db || pRet );
139211  return pRet;
139212}
139213
139214/*
139215** Table *p is a virtual table. This function removes the VTable object
139216** for table *p associated with database connection db from the linked
139217** list in p->pVTab. It also decrements the VTable ref count. This is
139218** used when closing database connection db to free all of its VTable
139219** objects without disturbing the rest of the Schema object (which may
139220** be being used by other shared-cache connections).
139221*/
139222SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
139223  VTable **ppVTab;
139224
139225  assert( IsVirtual(p) );
139226  assert( sqlite3BtreeHoldsAllMutexes(db) );
139227  assert( sqlite3_mutex_held(db->mutex) );
139228
139229  for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
139230    if( (*ppVTab)->db==db  ){
139231      VTable *pVTab = *ppVTab;
139232      *ppVTab = pVTab->pNext;
139233      sqlite3VtabUnlock(pVTab);
139234      break;
139235    }
139236  }
139237}
139238
139239
139240/*
139241** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
139242**
139243** This function may only be called when the mutexes associated with all
139244** shared b-tree databases opened using connection db are held by the
139245** caller. This is done to protect the sqlite3.pDisconnect list. The
139246** sqlite3.pDisconnect list is accessed only as follows:
139247**
139248**   1) By this function. In this case, all BtShared mutexes and the mutex
139249**      associated with the database handle itself must be held.
139250**
139251**   2) By function vtabDisconnectAll(), when it adds a VTable entry to
139252**      the sqlite3.pDisconnect list. In this case either the BtShared mutex
139253**      associated with the database the virtual table is stored in is held
139254**      or, if the virtual table is stored in a non-sharable database, then
139255**      the database handle mutex is held.
139256**
139257** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
139258** by multiple threads. It is thread-safe.
139259*/
139260SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
139261  VTable *p = db->pDisconnect;
139262
139263  assert( sqlite3BtreeHoldsAllMutexes(db) );
139264  assert( sqlite3_mutex_held(db->mutex) );
139265
139266  if( p ){
139267    db->pDisconnect = 0;
139268    sqlite3ExpirePreparedStatements(db, 0);
139269    do {
139270      VTable *pNext = p->pNext;
139271      sqlite3VtabUnlock(p);
139272      p = pNext;
139273    }while( p );
139274  }
139275}
139276
139277/*
139278** Clear any and all virtual-table information from the Table record.
139279** This routine is called, for example, just before deleting the Table
139280** record.
139281**
139282** Since it is a virtual-table, the Table structure contains a pointer
139283** to the head of a linked list of VTable structures. Each VTable
139284** structure is associated with a single sqlite3* user of the schema.
139285** The reference count of the VTable structure associated with database
139286** connection db is decremented immediately (which may lead to the
139287** structure being xDisconnected and free). Any other VTable structures
139288** in the list are moved to the sqlite3.pDisconnect list of the associated
139289** database connection.
139290*/
139291SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
139292  if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
139293  if( p->azModuleArg ){
139294    int i;
139295    for(i=0; i<p->nModuleArg; i++){
139296      if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
139297    }
139298    sqlite3DbFree(db, p->azModuleArg);
139299  }
139300}
139301
139302/*
139303** Add a new module argument to pTable->azModuleArg[].
139304** The string is not copied - the pointer is stored.  The
139305** string will be freed automatically when the table is
139306** deleted.
139307*/
139308static void addModuleArgument(Parse *pParse, Table *pTable, char *zArg){
139309  sqlite3_int64 nBytes = sizeof(char *)*(2+pTable->nModuleArg);
139310  char **azModuleArg;
139311  sqlite3 *db = pParse->db;
139312  if( pTable->nModuleArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
139313    sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
139314  }
139315  azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
139316  if( azModuleArg==0 ){
139317    sqlite3DbFree(db, zArg);
139318  }else{
139319    int i = pTable->nModuleArg++;
139320    azModuleArg[i] = zArg;
139321    azModuleArg[i+1] = 0;
139322    pTable->azModuleArg = azModuleArg;
139323  }
139324}
139325
139326/*
139327** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
139328** statement.  The module name has been parsed, but the optional list
139329** of parameters that follow the module name are still pending.
139330*/
139331SQLITE_PRIVATE void sqlite3VtabBeginParse(
139332  Parse *pParse,        /* Parsing context */
139333  Token *pName1,        /* Name of new table, or database name */
139334  Token *pName2,        /* Name of new table or NULL */
139335  Token *pModuleName,   /* Name of the module for the virtual table */
139336  int ifNotExists       /* No error if the table already exists */
139337){
139338  Table *pTable;        /* The new virtual table */
139339  sqlite3 *db;          /* Database connection */
139340
139341  sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
139342  pTable = pParse->pNewTable;
139343  if( pTable==0 ) return;
139344  assert( 0==pTable->pIndex );
139345
139346  db = pParse->db;
139347
139348  assert( pTable->nModuleArg==0 );
139349  addModuleArgument(pParse, pTable, sqlite3NameFromToken(db, pModuleName));
139350  addModuleArgument(pParse, pTable, 0);
139351  addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
139352  assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
139353       || (pParse->sNameToken.z==pName1->z && pName2->z==0)
139354  );
139355  pParse->sNameToken.n = (int)(
139356      &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
139357  );
139358
139359#ifndef SQLITE_OMIT_AUTHORIZATION
139360  /* Creating a virtual table invokes the authorization callback twice.
139361  ** The first invocation, to obtain permission to INSERT a row into the
139362  ** sqlite_master table, has already been made by sqlite3StartTable().
139363  ** The second call, to obtain permission to create the table, is made now.
139364  */
139365  if( pTable->azModuleArg ){
139366    int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
139367    assert( iDb>=0 ); /* The database the table is being created in */
139368    sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
139369            pTable->azModuleArg[0], pParse->db->aDb[iDb].zDbSName);
139370  }
139371#endif
139372}
139373
139374/*
139375** This routine takes the module argument that has been accumulating
139376** in pParse->zArg[] and appends it to the list of arguments on the
139377** virtual table currently under construction in pParse->pTable.
139378*/
139379static void addArgumentToVtab(Parse *pParse){
139380  if( pParse->sArg.z && pParse->pNewTable ){
139381    const char *z = (const char*)pParse->sArg.z;
139382    int n = pParse->sArg.n;
139383    sqlite3 *db = pParse->db;
139384    addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
139385  }
139386}
139387
139388/*
139389** The parser calls this routine after the CREATE VIRTUAL TABLE statement
139390** has been completely parsed.
139391*/
139392SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
139393  Table *pTab = pParse->pNewTable;  /* The table being constructed */
139394  sqlite3 *db = pParse->db;         /* The database connection */
139395
139396  if( pTab==0 ) return;
139397  addArgumentToVtab(pParse);
139398  pParse->sArg.z = 0;
139399  if( pTab->nModuleArg<1 ) return;
139400
139401  /* If the CREATE VIRTUAL TABLE statement is being entered for the
139402  ** first time (in other words if the virtual table is actually being
139403  ** created now instead of just being read out of sqlite_master) then
139404  ** do additional initialization work and store the statement text
139405  ** in the sqlite_master table.
139406  */
139407  if( !db->init.busy ){
139408    char *zStmt;
139409    char *zWhere;
139410    int iDb;
139411    int iReg;
139412    Vdbe *v;
139413
139414    sqlite3MayAbort(pParse);
139415
139416    /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
139417    if( pEnd ){
139418      pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
139419    }
139420    zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
139421
139422    /* A slot for the record has already been allocated in the
139423    ** SQLITE_MASTER table.  We just need to update that slot with all
139424    ** the information we've collected.
139425    **
139426    ** The VM register number pParse->regRowid holds the rowid of an
139427    ** entry in the sqlite_master table tht was created for this vtab
139428    ** by sqlite3StartTable().
139429    */
139430    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
139431    sqlite3NestedParse(pParse,
139432      "UPDATE %Q.%s "
139433         "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
139434       "WHERE rowid=#%d",
139435      db->aDb[iDb].zDbSName, MASTER_NAME,
139436      pTab->zName,
139437      pTab->zName,
139438      zStmt,
139439      pParse->regRowid
139440    );
139441    v = sqlite3GetVdbe(pParse);
139442    sqlite3ChangeCookie(pParse, iDb);
139443
139444    sqlite3VdbeAddOp0(v, OP_Expire);
139445    zWhere = sqlite3MPrintf(db, "name=%Q AND sql=%Q", pTab->zName, zStmt);
139446    sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
139447    sqlite3DbFree(db, zStmt);
139448
139449    iReg = ++pParse->nMem;
139450    sqlite3VdbeLoadString(v, iReg, pTab->zName);
139451    sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg);
139452  }
139453
139454  /* If we are rereading the sqlite_master table create the in-memory
139455  ** record of the table. The xConnect() method is not called until
139456  ** the first time the virtual table is used in an SQL statement. This
139457  ** allows a schema that contains virtual tables to be loaded before
139458  ** the required virtual table implementations are registered.  */
139459  else {
139460    Table *pOld;
139461    Schema *pSchema = pTab->pSchema;
139462    const char *zName = pTab->zName;
139463    assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
139464    pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
139465    if( pOld ){
139466      sqlite3OomFault(db);
139467      assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
139468      return;
139469    }
139470    pParse->pNewTable = 0;
139471  }
139472}
139473
139474/*
139475** The parser calls this routine when it sees the first token
139476** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
139477*/
139478SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
139479  addArgumentToVtab(pParse);
139480  pParse->sArg.z = 0;
139481  pParse->sArg.n = 0;
139482}
139483
139484/*
139485** The parser calls this routine for each token after the first token
139486** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
139487*/
139488SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
139489  Token *pArg = &pParse->sArg;
139490  if( pArg->z==0 ){
139491    pArg->z = p->z;
139492    pArg->n = p->n;
139493  }else{
139494    assert(pArg->z <= p->z);
139495    pArg->n = (int)(&p->z[p->n] - pArg->z);
139496  }
139497}
139498
139499/*
139500** Invoke a virtual table constructor (either xCreate or xConnect). The
139501** pointer to the function to invoke is passed as the fourth parameter
139502** to this procedure.
139503*/
139504static int vtabCallConstructor(
139505  sqlite3 *db,
139506  Table *pTab,
139507  Module *pMod,
139508  int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
139509  char **pzErr
139510){
139511  VtabCtx sCtx;
139512  VTable *pVTable;
139513  int rc;
139514  const char *const*azArg = (const char *const*)pTab->azModuleArg;
139515  int nArg = pTab->nModuleArg;
139516  char *zErr = 0;
139517  char *zModuleName;
139518  int iDb;
139519  VtabCtx *pCtx;
139520
139521  /* Check that the virtual-table is not already being initialized */
139522  for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
139523    if( pCtx->pTab==pTab ){
139524      *pzErr = sqlite3MPrintf(db,
139525          "vtable constructor called recursively: %s", pTab->zName
139526      );
139527      return SQLITE_LOCKED;
139528    }
139529  }
139530
139531  zModuleName = sqlite3DbStrDup(db, pTab->zName);
139532  if( !zModuleName ){
139533    return SQLITE_NOMEM_BKPT;
139534  }
139535
139536  pVTable = sqlite3MallocZero(sizeof(VTable));
139537  if( !pVTable ){
139538    sqlite3OomFault(db);
139539    sqlite3DbFree(db, zModuleName);
139540    return SQLITE_NOMEM_BKPT;
139541  }
139542  pVTable->db = db;
139543  pVTable->pMod = pMod;
139544  pVTable->eVtabRisk = SQLITE_VTABRISK_Normal;
139545
139546  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
139547  pTab->azModuleArg[1] = db->aDb[iDb].zDbSName;
139548
139549  /* Invoke the virtual table constructor */
139550  assert( &db->pVtabCtx );
139551  assert( xConstruct );
139552  sCtx.pTab = pTab;
139553  sCtx.pVTable = pVTable;
139554  sCtx.pPrior = db->pVtabCtx;
139555  sCtx.bDeclared = 0;
139556  db->pVtabCtx = &sCtx;
139557  rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
139558  db->pVtabCtx = sCtx.pPrior;
139559  if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
139560  assert( sCtx.pTab==pTab );
139561
139562  if( SQLITE_OK!=rc ){
139563    if( zErr==0 ){
139564      *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
139565    }else {
139566      *pzErr = sqlite3MPrintf(db, "%s", zErr);
139567      sqlite3_free(zErr);
139568    }
139569    sqlite3DbFree(db, pVTable);
139570  }else if( ALWAYS(pVTable->pVtab) ){
139571    /* Justification of ALWAYS():  A correct vtab constructor must allocate
139572    ** the sqlite3_vtab object if successful.  */
139573    memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
139574    pVTable->pVtab->pModule = pMod->pModule;
139575    pMod->nRefModule++;
139576    pVTable->nRef = 1;
139577    if( sCtx.bDeclared==0 ){
139578      const char *zFormat = "vtable constructor did not declare schema: %s";
139579      *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
139580      sqlite3VtabUnlock(pVTable);
139581      rc = SQLITE_ERROR;
139582    }else{
139583      int iCol;
139584      u16 oooHidden = 0;
139585      /* If everything went according to plan, link the new VTable structure
139586      ** into the linked list headed by pTab->pVTable. Then loop through the
139587      ** columns of the table to see if any of them contain the token "hidden".
139588      ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
139589      ** the type string.  */
139590      pVTable->pNext = pTab->pVTable;
139591      pTab->pVTable = pVTable;
139592
139593      for(iCol=0; iCol<pTab->nCol; iCol++){
139594        char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
139595        int nType;
139596        int i = 0;
139597        nType = sqlite3Strlen30(zType);
139598        for(i=0; i<nType; i++){
139599          if( 0==sqlite3StrNICmp("hidden", &zType[i], 6)
139600           && (i==0 || zType[i-1]==' ')
139601           && (zType[i+6]=='\0' || zType[i+6]==' ')
139602          ){
139603            break;
139604          }
139605        }
139606        if( i<nType ){
139607          int j;
139608          int nDel = 6 + (zType[i+6] ? 1 : 0);
139609          for(j=i; (j+nDel)<=nType; j++){
139610            zType[j] = zType[j+nDel];
139611          }
139612          if( zType[i]=='\0' && i>0 ){
139613            assert(zType[i-1]==' ');
139614            zType[i-1] = '\0';
139615          }
139616          pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
139617          oooHidden = TF_OOOHidden;
139618        }else{
139619          pTab->tabFlags |= oooHidden;
139620        }
139621      }
139622    }
139623  }
139624
139625  sqlite3DbFree(db, zModuleName);
139626  return rc;
139627}
139628
139629/*
139630** This function is invoked by the parser to call the xConnect() method
139631** of the virtual table pTab. If an error occurs, an error code is returned
139632** and an error left in pParse.
139633**
139634** This call is a no-op if table pTab is not a virtual table.
139635*/
139636SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
139637  sqlite3 *db = pParse->db;
139638  const char *zMod;
139639  Module *pMod;
139640  int rc;
139641
139642  assert( pTab );
139643  if( !IsVirtual(pTab) || sqlite3GetVTable(db, pTab) ){
139644    return SQLITE_OK;
139645  }
139646
139647  /* Locate the required virtual table module */
139648  zMod = pTab->azModuleArg[0];
139649  pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
139650
139651  if( !pMod ){
139652    const char *zModule = pTab->azModuleArg[0];
139653    sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
139654    rc = SQLITE_ERROR;
139655  }else{
139656    char *zErr = 0;
139657    rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
139658    if( rc!=SQLITE_OK ){
139659      sqlite3ErrorMsg(pParse, "%s", zErr);
139660      pParse->rc = rc;
139661    }
139662    sqlite3DbFree(db, zErr);
139663  }
139664
139665  return rc;
139666}
139667/*
139668** Grow the db->aVTrans[] array so that there is room for at least one
139669** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
139670*/
139671static int growVTrans(sqlite3 *db){
139672  const int ARRAY_INCR = 5;
139673
139674  /* Grow the sqlite3.aVTrans array if required */
139675  if( (db->nVTrans%ARRAY_INCR)==0 ){
139676    VTable **aVTrans;
139677    sqlite3_int64 nBytes = sizeof(sqlite3_vtab*)*
139678                                 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
139679    aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
139680    if( !aVTrans ){
139681      return SQLITE_NOMEM_BKPT;
139682    }
139683    memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
139684    db->aVTrans = aVTrans;
139685  }
139686
139687  return SQLITE_OK;
139688}
139689
139690/*
139691** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
139692** have already been reserved using growVTrans().
139693*/
139694static void addToVTrans(sqlite3 *db, VTable *pVTab){
139695  /* Add pVtab to the end of sqlite3.aVTrans */
139696  db->aVTrans[db->nVTrans++] = pVTab;
139697  sqlite3VtabLock(pVTab);
139698}
139699
139700/*
139701** This function is invoked by the vdbe to call the xCreate method
139702** of the virtual table named zTab in database iDb.
139703**
139704** If an error occurs, *pzErr is set to point to an English language
139705** description of the error and an SQLITE_XXX error code is returned.
139706** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
139707*/
139708SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
139709  int rc = SQLITE_OK;
139710  Table *pTab;
139711  Module *pMod;
139712  const char *zMod;
139713
139714  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
139715  assert( pTab && IsVirtual(pTab) && !pTab->pVTable );
139716
139717  /* Locate the required virtual table module */
139718  zMod = pTab->azModuleArg[0];
139719  pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
139720
139721  /* If the module has been registered and includes a Create method,
139722  ** invoke it now. If the module has not been registered, return an
139723  ** error. Otherwise, do nothing.
139724  */
139725  if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
139726    *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
139727    rc = SQLITE_ERROR;
139728  }else{
139729    rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
139730  }
139731
139732  /* Justification of ALWAYS():  The xConstructor method is required to
139733  ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
139734  if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
139735    rc = growVTrans(db);
139736    if( rc==SQLITE_OK ){
139737      addToVTrans(db, sqlite3GetVTable(db, pTab));
139738    }
139739  }
139740
139741  return rc;
139742}
139743
139744/*
139745** This function is used to set the schema of a virtual table.  It is only
139746** valid to call this function from within the xCreate() or xConnect() of a
139747** virtual table module.
139748*/
139749SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
139750  VtabCtx *pCtx;
139751  int rc = SQLITE_OK;
139752  Table *pTab;
139753  char *zErr = 0;
139754  Parse sParse;
139755
139756#ifdef SQLITE_ENABLE_API_ARMOR
139757  if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
139758    return SQLITE_MISUSE_BKPT;
139759  }
139760#endif
139761  sqlite3_mutex_enter(db->mutex);
139762  pCtx = db->pVtabCtx;
139763  if( !pCtx || pCtx->bDeclared ){
139764    sqlite3Error(db, SQLITE_MISUSE);
139765    sqlite3_mutex_leave(db->mutex);
139766    return SQLITE_MISUSE_BKPT;
139767  }
139768  pTab = pCtx->pTab;
139769  assert( IsVirtual(pTab) );
139770
139771  memset(&sParse, 0, sizeof(sParse));
139772  sParse.eParseMode = PARSE_MODE_DECLARE_VTAB;
139773  sParse.db = db;
139774  sParse.nQueryLoop = 1;
139775  if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable, &zErr)
139776   && sParse.pNewTable
139777   && !db->mallocFailed
139778   && !sParse.pNewTable->pSelect
139779   && !IsVirtual(sParse.pNewTable)
139780  ){
139781    if( !pTab->aCol ){
139782      Table *pNew = sParse.pNewTable;
139783      Index *pIdx;
139784      pTab->aCol = pNew->aCol;
139785      pTab->nCol = pNew->nCol;
139786      pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
139787      pNew->nCol = 0;
139788      pNew->aCol = 0;
139789      assert( pTab->pIndex==0 );
139790      assert( HasRowid(pNew) || sqlite3PrimaryKeyIndex(pNew)!=0 );
139791      if( !HasRowid(pNew)
139792       && pCtx->pVTable->pMod->pModule->xUpdate!=0
139793       && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
139794      ){
139795        /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
139796        ** or else must have a single-column PRIMARY KEY */
139797        rc = SQLITE_ERROR;
139798      }
139799      pIdx = pNew->pIndex;
139800      if( pIdx ){
139801        assert( pIdx->pNext==0 );
139802        pTab->pIndex = pIdx;
139803        pNew->pIndex = 0;
139804        pIdx->pTable = pTab;
139805      }
139806    }
139807    pCtx->bDeclared = 1;
139808  }else{
139809    sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
139810    sqlite3DbFree(db, zErr);
139811    rc = SQLITE_ERROR;
139812  }
139813  sParse.eParseMode = PARSE_MODE_NORMAL;
139814
139815  if( sParse.pVdbe ){
139816    sqlite3VdbeFinalize(sParse.pVdbe);
139817  }
139818  sqlite3DeleteTable(db, sParse.pNewTable);
139819  sqlite3ParserReset(&sParse);
139820
139821  assert( (rc&0xff)==rc );
139822  rc = sqlite3ApiExit(db, rc);
139823  sqlite3_mutex_leave(db->mutex);
139824  return rc;
139825}
139826
139827/*
139828** This function is invoked by the vdbe to call the xDestroy method
139829** of the virtual table named zTab in database iDb. This occurs
139830** when a DROP TABLE is mentioned.
139831**
139832** This call is a no-op if zTab is not a virtual table.
139833*/
139834SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
139835  int rc = SQLITE_OK;
139836  Table *pTab;
139837
139838  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
139839  if( pTab!=0 && ALWAYS(pTab->pVTable!=0) ){
139840    VTable *p;
139841    int (*xDestroy)(sqlite3_vtab *);
139842    for(p=pTab->pVTable; p; p=p->pNext){
139843      assert( p->pVtab );
139844      if( p->pVtab->nRef>0 ){
139845        return SQLITE_LOCKED;
139846      }
139847    }
139848    p = vtabDisconnectAll(db, pTab);
139849    xDestroy = p->pMod->pModule->xDestroy;
139850    if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect;
139851    assert( xDestroy!=0 );
139852    pTab->nTabRef++;
139853    rc = xDestroy(p->pVtab);
139854    /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
139855    if( rc==SQLITE_OK ){
139856      assert( pTab->pVTable==p && p->pNext==0 );
139857      p->pVtab = 0;
139858      pTab->pVTable = 0;
139859      sqlite3VtabUnlock(p);
139860    }
139861    sqlite3DeleteTable(db, pTab);
139862  }
139863
139864  return rc;
139865}
139866
139867/*
139868** This function invokes either the xRollback or xCommit method
139869** of each of the virtual tables in the sqlite3.aVTrans array. The method
139870** called is identified by the second argument, "offset", which is
139871** the offset of the method to call in the sqlite3_module structure.
139872**
139873** The array is cleared after invoking the callbacks.
139874*/
139875static void callFinaliser(sqlite3 *db, int offset){
139876  int i;
139877  if( db->aVTrans ){
139878    VTable **aVTrans = db->aVTrans;
139879    db->aVTrans = 0;
139880    for(i=0; i<db->nVTrans; i++){
139881      VTable *pVTab = aVTrans[i];
139882      sqlite3_vtab *p = pVTab->pVtab;
139883      if( p ){
139884        int (*x)(sqlite3_vtab *);
139885        x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
139886        if( x ) x(p);
139887      }
139888      pVTab->iSavepoint = 0;
139889      sqlite3VtabUnlock(pVTab);
139890    }
139891    sqlite3DbFree(db, aVTrans);
139892    db->nVTrans = 0;
139893  }
139894}
139895
139896/*
139897** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
139898** array. Return the error code for the first error that occurs, or
139899** SQLITE_OK if all xSync operations are successful.
139900**
139901** If an error message is available, leave it in p->zErrMsg.
139902*/
139903SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
139904  int i;
139905  int rc = SQLITE_OK;
139906  VTable **aVTrans = db->aVTrans;
139907
139908  db->aVTrans = 0;
139909  for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
139910    int (*x)(sqlite3_vtab *);
139911    sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
139912    if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
139913      rc = x(pVtab);
139914      sqlite3VtabImportErrmsg(p, pVtab);
139915    }
139916  }
139917  db->aVTrans = aVTrans;
139918  return rc;
139919}
139920
139921/*
139922** Invoke the xRollback method of all virtual tables in the
139923** sqlite3.aVTrans array. Then clear the array itself.
139924*/
139925SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
139926  callFinaliser(db, offsetof(sqlite3_module,xRollback));
139927  return SQLITE_OK;
139928}
139929
139930/*
139931** Invoke the xCommit method of all virtual tables in the
139932** sqlite3.aVTrans array. Then clear the array itself.
139933*/
139934SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
139935  callFinaliser(db, offsetof(sqlite3_module,xCommit));
139936  return SQLITE_OK;
139937}
139938
139939/*
139940** If the virtual table pVtab supports the transaction interface
139941** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
139942** not currently open, invoke the xBegin method now.
139943**
139944** If the xBegin call is successful, place the sqlite3_vtab pointer
139945** in the sqlite3.aVTrans array.
139946*/
139947SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
139948  int rc = SQLITE_OK;
139949  const sqlite3_module *pModule;
139950
139951  /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
139952  ** than zero, then this function is being called from within a
139953  ** virtual module xSync() callback. It is illegal to write to
139954  ** virtual module tables in this case, so return SQLITE_LOCKED.
139955  */
139956  if( sqlite3VtabInSync(db) ){
139957    return SQLITE_LOCKED;
139958  }
139959  if( !pVTab ){
139960    return SQLITE_OK;
139961  }
139962  pModule = pVTab->pVtab->pModule;
139963
139964  if( pModule->xBegin ){
139965    int i;
139966
139967    /* If pVtab is already in the aVTrans array, return early */
139968    for(i=0; i<db->nVTrans; i++){
139969      if( db->aVTrans[i]==pVTab ){
139970        return SQLITE_OK;
139971      }
139972    }
139973
139974    /* Invoke the xBegin method. If successful, add the vtab to the
139975    ** sqlite3.aVTrans[] array. */
139976    rc = growVTrans(db);
139977    if( rc==SQLITE_OK ){
139978      rc = pModule->xBegin(pVTab->pVtab);
139979      if( rc==SQLITE_OK ){
139980        int iSvpt = db->nStatement + db->nSavepoint;
139981        addToVTrans(db, pVTab);
139982        if( iSvpt && pModule->xSavepoint ){
139983          pVTab->iSavepoint = iSvpt;
139984          rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
139985        }
139986      }
139987    }
139988  }
139989  return rc;
139990}
139991
139992/*
139993** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
139994** virtual tables that currently have an open transaction. Pass iSavepoint
139995** as the second argument to the virtual table method invoked.
139996**
139997** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
139998** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
139999** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
140000** an open transaction is invoked.
140001**
140002** If any virtual table method returns an error code other than SQLITE_OK,
140003** processing is abandoned and the error returned to the caller of this
140004** function immediately. If all calls to virtual table methods are successful,
140005** SQLITE_OK is returned.
140006*/
140007SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
140008  int rc = SQLITE_OK;
140009
140010  assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
140011  assert( iSavepoint>=-1 );
140012  if( db->aVTrans ){
140013    int i;
140014    for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
140015      VTable *pVTab = db->aVTrans[i];
140016      const sqlite3_module *pMod = pVTab->pMod->pModule;
140017      if( pVTab->pVtab && pMod->iVersion>=2 ){
140018        int (*xMethod)(sqlite3_vtab *, int);
140019        sqlite3VtabLock(pVTab);
140020        switch( op ){
140021          case SAVEPOINT_BEGIN:
140022            xMethod = pMod->xSavepoint;
140023            pVTab->iSavepoint = iSavepoint+1;
140024            break;
140025          case SAVEPOINT_ROLLBACK:
140026            xMethod = pMod->xRollbackTo;
140027            break;
140028          default:
140029            xMethod = pMod->xRelease;
140030            break;
140031        }
140032        if( xMethod && pVTab->iSavepoint>iSavepoint ){
140033          rc = xMethod(pVTab->pVtab, iSavepoint);
140034        }
140035        sqlite3VtabUnlock(pVTab);
140036      }
140037    }
140038  }
140039  return rc;
140040}
140041
140042/*
140043** The first parameter (pDef) is a function implementation.  The
140044** second parameter (pExpr) is the first argument to this function.
140045** If pExpr is a column in a virtual table, then let the virtual
140046** table implementation have an opportunity to overload the function.
140047**
140048** This routine is used to allow virtual table implementations to
140049** overload MATCH, LIKE, GLOB, and REGEXP operators.
140050**
140051** Return either the pDef argument (indicating no change) or a
140052** new FuncDef structure that is marked as ephemeral using the
140053** SQLITE_FUNC_EPHEM flag.
140054*/
140055SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
140056  sqlite3 *db,    /* Database connection for reporting malloc problems */
140057  FuncDef *pDef,  /* Function to possibly overload */
140058  int nArg,       /* Number of arguments to the function */
140059  Expr *pExpr     /* First argument to the function */
140060){
140061  Table *pTab;
140062  sqlite3_vtab *pVtab;
140063  sqlite3_module *pMod;
140064  void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
140065  void *pArg = 0;
140066  FuncDef *pNew;
140067  int rc = 0;
140068
140069  /* Check to see the left operand is a column in a virtual table */
140070  if( NEVER(pExpr==0) ) return pDef;
140071  if( pExpr->op!=TK_COLUMN ) return pDef;
140072  pTab = pExpr->y.pTab;
140073  if( pTab==0 ) return pDef;
140074  if( !IsVirtual(pTab) ) return pDef;
140075  pVtab = sqlite3GetVTable(db, pTab)->pVtab;
140076  assert( pVtab!=0 );
140077  assert( pVtab->pModule!=0 );
140078  pMod = (sqlite3_module *)pVtab->pModule;
140079  if( pMod->xFindFunction==0 ) return pDef;
140080
140081  /* Call the xFindFunction method on the virtual table implementation
140082  ** to see if the implementation wants to overload this function.
140083  **
140084  ** Though undocumented, we have historically always invoked xFindFunction
140085  ** with an all lower-case function name.  Continue in this tradition to
140086  ** avoid any chance of an incompatibility.
140087  */
140088#ifdef SQLITE_DEBUG
140089  {
140090    int i;
140091    for(i=0; pDef->zName[i]; i++){
140092      unsigned char x = (unsigned char)pDef->zName[i];
140093      assert( x==sqlite3UpperToLower[x] );
140094    }
140095  }
140096#endif
140097  rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
140098  if( rc==0 ){
140099    return pDef;
140100  }
140101
140102  /* Create a new ephemeral function definition for the overloaded
140103  ** function */
140104  pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
140105                             + sqlite3Strlen30(pDef->zName) + 1);
140106  if( pNew==0 ){
140107    return pDef;
140108  }
140109  *pNew = *pDef;
140110  pNew->zName = (const char*)&pNew[1];
140111  memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
140112  pNew->xSFunc = xSFunc;
140113  pNew->pUserData = pArg;
140114  pNew->funcFlags |= SQLITE_FUNC_EPHEM;
140115  return pNew;
140116}
140117
140118/*
140119** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
140120** array so that an OP_VBegin will get generated for it.  Add pTab to the
140121** array if it is missing.  If pTab is already in the array, this routine
140122** is a no-op.
140123*/
140124SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
140125  Parse *pToplevel = sqlite3ParseToplevel(pParse);
140126  int i, n;
140127  Table **apVtabLock;
140128
140129  assert( IsVirtual(pTab) );
140130  for(i=0; i<pToplevel->nVtabLock; i++){
140131    if( pTab==pToplevel->apVtabLock[i] ) return;
140132  }
140133  n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
140134  apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n);
140135  if( apVtabLock ){
140136    pToplevel->apVtabLock = apVtabLock;
140137    pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
140138  }else{
140139    sqlite3OomFault(pToplevel->db);
140140  }
140141}
140142
140143/*
140144** Check to see if virtual table module pMod can be have an eponymous
140145** virtual table instance.  If it can, create one if one does not already
140146** exist. Return non-zero if the eponymous virtual table instance exists
140147** when this routine returns, and return zero if it does not exist.
140148**
140149** An eponymous virtual table instance is one that is named after its
140150** module, and more importantly, does not require a CREATE VIRTUAL TABLE
140151** statement in order to come into existance.  Eponymous virtual table
140152** instances always exist.  They cannot be DROP-ed.
140153**
140154** Any virtual table module for which xConnect and xCreate are the same
140155** method can have an eponymous virtual table instance.
140156*/
140157SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){
140158  const sqlite3_module *pModule = pMod->pModule;
140159  Table *pTab;
140160  char *zErr = 0;
140161  int rc;
140162  sqlite3 *db = pParse->db;
140163  if( pMod->pEpoTab ) return 1;
140164  if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
140165  pTab = sqlite3DbMallocZero(db, sizeof(Table));
140166  if( pTab==0 ) return 0;
140167  pTab->zName = sqlite3DbStrDup(db, pMod->zName);
140168  if( pTab->zName==0 ){
140169    sqlite3DbFree(db, pTab);
140170    return 0;
140171  }
140172  pMod->pEpoTab = pTab;
140173  pTab->nTabRef = 1;
140174  pTab->pSchema = db->aDb[0].pSchema;
140175  assert( pTab->nModuleArg==0 );
140176  pTab->iPKey = -1;
140177  addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
140178  addModuleArgument(pParse, pTab, 0);
140179  addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
140180  rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
140181  if( rc ){
140182    sqlite3ErrorMsg(pParse, "%s", zErr);
140183    sqlite3DbFree(db, zErr);
140184    sqlite3VtabEponymousTableClear(db, pMod);
140185    return 0;
140186  }
140187  return 1;
140188}
140189
140190/*
140191** Erase the eponymous virtual table instance associated with
140192** virtual table module pMod, if it exists.
140193*/
140194SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){
140195  Table *pTab = pMod->pEpoTab;
140196  if( pTab!=0 ){
140197    /* Mark the table as Ephemeral prior to deleting it, so that the
140198    ** sqlite3DeleteTable() routine will know that it is not stored in
140199    ** the schema. */
140200    pTab->tabFlags |= TF_Ephemeral;
140201    sqlite3DeleteTable(db, pTab);
140202    pMod->pEpoTab = 0;
140203  }
140204}
140205
140206/*
140207** Return the ON CONFLICT resolution mode in effect for the virtual
140208** table update operation currently in progress.
140209**
140210** The results of this routine are undefined unless it is called from
140211** within an xUpdate method.
140212*/
140213SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
140214  static const unsigned char aMap[] = {
140215    SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
140216  };
140217#ifdef SQLITE_ENABLE_API_ARMOR
140218  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
140219#endif
140220  assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
140221  assert( OE_Ignore==4 && OE_Replace==5 );
140222  assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
140223  return (int)aMap[db->vtabOnConflict-1];
140224}
140225
140226/*
140227** Call from within the xCreate() or xConnect() methods to provide
140228** the SQLite core with additional information about the behavior
140229** of the virtual table being implemented.
140230*/
140231SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
140232  va_list ap;
140233  int rc = SQLITE_OK;
140234  VtabCtx *p;
140235
140236#ifdef SQLITE_ENABLE_API_ARMOR
140237  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
140238#endif
140239  sqlite3_mutex_enter(db->mutex);
140240  p = db->pVtabCtx;
140241  if( !p ){
140242    rc = SQLITE_MISUSE_BKPT;
140243  }else{
140244    assert( p->pTab==0 || IsVirtual(p->pTab) );
140245    va_start(ap, op);
140246    switch( op ){
140247      case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
140248        p->pVTable->bConstraint = (u8)va_arg(ap, int);
140249        break;
140250      }
140251      case SQLITE_VTAB_INNOCUOUS: {
140252        p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low;
140253        break;
140254      }
140255      case SQLITE_VTAB_DIRECTONLY: {
140256        p->pVTable->eVtabRisk = SQLITE_VTABRISK_High;
140257        break;
140258      }
140259      default: {
140260        rc = SQLITE_MISUSE_BKPT;
140261        break;
140262      }
140263    }
140264    va_end(ap);
140265  }
140266
140267  if( rc!=SQLITE_OK ) sqlite3Error(db, rc);
140268  sqlite3_mutex_leave(db->mutex);
140269  return rc;
140270}
140271
140272#endif /* SQLITE_OMIT_VIRTUALTABLE */
140273
140274/************** End of vtab.c ************************************************/
140275/************** Begin file wherecode.c ***************************************/
140276/*
140277** 2015-06-06
140278**
140279** The author disclaims copyright to this source code.  In place of
140280** a legal notice, here is a blessing:
140281**
140282**    May you do good and not evil.
140283**    May you find forgiveness for yourself and forgive others.
140284**    May you share freely, never taking more than you give.
140285**
140286*************************************************************************
140287** This module contains C code that generates VDBE code used to process
140288** the WHERE clause of SQL statements.
140289**
140290** This file was split off from where.c on 2015-06-06 in order to reduce the
140291** size of where.c and make it easier to edit.  This file contains the routines
140292** that actually generate the bulk of the WHERE loop code.  The original where.c
140293** file retains the code that does query planning and analysis.
140294*/
140295/* #include "sqliteInt.h" */
140296/************** Include whereInt.h in the middle of wherecode.c **************/
140297/************** Begin file whereInt.h ****************************************/
140298/*
140299** 2013-11-12
140300**
140301** The author disclaims copyright to this source code.  In place of
140302** a legal notice, here is a blessing:
140303**
140304**    May you do good and not evil.
140305**    May you find forgiveness for yourself and forgive others.
140306**    May you share freely, never taking more than you give.
140307**
140308*************************************************************************
140309**
140310** This file contains structure and macro definitions for the query
140311** planner logic in "where.c".  These definitions are broken out into
140312** a separate source file for easier editing.
140313*/
140314#ifndef SQLITE_WHEREINT_H
140315#define SQLITE_WHEREINT_H
140316
140317/*
140318** Trace output macros
140319*/
140320#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
140321/***/ extern int sqlite3WhereTrace;
140322#endif
140323#if defined(SQLITE_DEBUG) \
140324    && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
140325# define WHERETRACE(K,X)  if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
140326# define WHERETRACE_ENABLED 1
140327#else
140328# define WHERETRACE(K,X)
140329#endif
140330
140331/* Forward references
140332*/
140333typedef struct WhereClause WhereClause;
140334typedef struct WhereMaskSet WhereMaskSet;
140335typedef struct WhereOrInfo WhereOrInfo;
140336typedef struct WhereAndInfo WhereAndInfo;
140337typedef struct WhereLevel WhereLevel;
140338typedef struct WhereLoop WhereLoop;
140339typedef struct WherePath WherePath;
140340typedef struct WhereTerm WhereTerm;
140341typedef struct WhereLoopBuilder WhereLoopBuilder;
140342typedef struct WhereScan WhereScan;
140343typedef struct WhereOrCost WhereOrCost;
140344typedef struct WhereOrSet WhereOrSet;
140345
140346/*
140347** This object contains information needed to implement a single nested
140348** loop in WHERE clause.
140349**
140350** Contrast this object with WhereLoop.  This object describes the
140351** implementation of the loop.  WhereLoop describes the algorithm.
140352** This object contains a pointer to the WhereLoop algorithm as one of
140353** its elements.
140354**
140355** The WhereInfo object contains a single instance of this object for
140356** each term in the FROM clause (which is to say, for each of the
140357** nested loops as implemented).  The order of WhereLevel objects determines
140358** the loop nested order, with WhereInfo.a[0] being the outer loop and
140359** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
140360*/
140361struct WhereLevel {
140362  int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
140363  int iTabCur;          /* The VDBE cursor used to access the table */
140364  int iIdxCur;          /* The VDBE cursor used to access pIdx */
140365  int addrBrk;          /* Jump here to break out of the loop */
140366  int addrNxt;          /* Jump here to start the next IN combination */
140367  int addrSkip;         /* Jump here for next iteration of skip-scan */
140368  int addrCont;         /* Jump here to continue with the next loop cycle */
140369  int addrFirst;        /* First instruction of interior of the loop */
140370  int addrBody;         /* Beginning of the body of this loop */
140371  int regBignull;       /* big-null flag reg. True if a NULL-scan is needed */
140372  int addrBignull;      /* Jump here for next part of big-null scan */
140373#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
140374  u32 iLikeRepCntr;     /* LIKE range processing counter register (times 2) */
140375  int addrLikeRep;      /* LIKE range processing address */
140376#endif
140377  u8 iFrom;             /* Which entry in the FROM clause */
140378  u8 op, p3, p5;        /* Opcode, P3 & P5 of the opcode that ends the loop */
140379  int p1, p2;           /* Operands of the opcode used to end the loop */
140380  union {               /* Information that depends on pWLoop->wsFlags */
140381    struct {
140382      int nIn;              /* Number of entries in aInLoop[] */
140383      struct InLoop {
140384        int iCur;              /* The VDBE cursor used by this IN operator */
140385        int addrInTop;         /* Top of the IN loop */
140386        int iBase;             /* Base register of multi-key index record */
140387        int nPrefix;           /* Number of prior entires in the key */
140388        u8 eEndLoopOp;         /* IN Loop terminator. OP_Next or OP_Prev */
140389      } *aInLoop;           /* Information about each nested IN operator */
140390    } in;                 /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
140391    Index *pCovidx;       /* Possible covering index for WHERE_MULTI_OR */
140392  } u;
140393  struct WhereLoop *pWLoop;  /* The selected WhereLoop object */
140394  Bitmask notReady;          /* FROM entries not usable at this level */
140395#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
140396  int addrVisit;        /* Address at which row is visited */
140397#endif
140398};
140399
140400/*
140401** Each instance of this object represents an algorithm for evaluating one
140402** term of a join.  Every term of the FROM clause will have at least
140403** one corresponding WhereLoop object (unless INDEXED BY constraints
140404** prevent a query solution - which is an error) and many terms of the
140405** FROM clause will have multiple WhereLoop objects, each describing a
140406** potential way of implementing that FROM-clause term, together with
140407** dependencies and cost estimates for using the chosen algorithm.
140408**
140409** Query planning consists of building up a collection of these WhereLoop
140410** objects, then computing a particular sequence of WhereLoop objects, with
140411** one WhereLoop object per FROM clause term, that satisfy all dependencies
140412** and that minimize the overall cost.
140413*/
140414struct WhereLoop {
140415  Bitmask prereq;       /* Bitmask of other loops that must run first */
140416  Bitmask maskSelf;     /* Bitmask identifying table iTab */
140417#ifdef SQLITE_DEBUG
140418  char cId;             /* Symbolic ID of this loop for debugging use */
140419#endif
140420  u8 iTab;              /* Position in FROM clause of table for this loop */
140421  u8 iSortIdx;          /* Sorting index number.  0==None */
140422  LogEst rSetup;        /* One-time setup cost (ex: create transient index) */
140423  LogEst rRun;          /* Cost of running each loop */
140424  LogEst nOut;          /* Estimated number of output rows */
140425  union {
140426    struct {               /* Information for internal btree tables */
140427      u16 nEq;               /* Number of equality constraints */
140428      u16 nBtm;              /* Size of BTM vector */
140429      u16 nTop;              /* Size of TOP vector */
140430      u16 nDistinctCol;      /* Index columns used to sort for DISTINCT */
140431      Index *pIndex;         /* Index used, or NULL */
140432    } btree;
140433    struct {               /* Information for virtual tables */
140434      int idxNum;            /* Index number */
140435      u8 needFree;           /* True if sqlite3_free(idxStr) is needed */
140436      i8 isOrdered;          /* True if satisfies ORDER BY */
140437      u16 omitMask;          /* Terms that may be omitted */
140438      char *idxStr;          /* Index identifier string */
140439    } vtab;
140440  } u;
140441  u32 wsFlags;          /* WHERE_* flags describing the plan */
140442  u16 nLTerm;           /* Number of entries in aLTerm[] */
140443  u16 nSkip;            /* Number of NULL aLTerm[] entries */
140444  /**** whereLoopXfer() copies fields above ***********************/
140445# define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
140446  u16 nLSlot;           /* Number of slots allocated for aLTerm[] */
140447  WhereTerm **aLTerm;   /* WhereTerms used */
140448  WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
140449  WhereTerm *aLTermSpace[3];  /* Initial aLTerm[] space */
140450};
140451
140452/* This object holds the prerequisites and the cost of running a
140453** subquery on one operand of an OR operator in the WHERE clause.
140454** See WhereOrSet for additional information
140455*/
140456struct WhereOrCost {
140457  Bitmask prereq;     /* Prerequisites */
140458  LogEst rRun;        /* Cost of running this subquery */
140459  LogEst nOut;        /* Number of outputs for this subquery */
140460};
140461
140462/* The WhereOrSet object holds a set of possible WhereOrCosts that
140463** correspond to the subquery(s) of OR-clause processing.  Only the
140464** best N_OR_COST elements are retained.
140465*/
140466#define N_OR_COST 3
140467struct WhereOrSet {
140468  u16 n;                      /* Number of valid a[] entries */
140469  WhereOrCost a[N_OR_COST];   /* Set of best costs */
140470};
140471
140472/*
140473** Each instance of this object holds a sequence of WhereLoop objects
140474** that implement some or all of a query plan.
140475**
140476** Think of each WhereLoop object as a node in a graph with arcs
140477** showing dependencies and costs for travelling between nodes.  (That is
140478** not a completely accurate description because WhereLoop costs are a
140479** vector, not a scalar, and because dependencies are many-to-one, not
140480** one-to-one as are graph nodes.  But it is a useful visualization aid.)
140481** Then a WherePath object is a path through the graph that visits some
140482** or all of the WhereLoop objects once.
140483**
140484** The "solver" works by creating the N best WherePath objects of length
140485** 1.  Then using those as a basis to compute the N best WherePath objects
140486** of length 2.  And so forth until the length of WherePaths equals the
140487** number of nodes in the FROM clause.  The best (lowest cost) WherePath
140488** at the end is the chosen query plan.
140489*/
140490struct WherePath {
140491  Bitmask maskLoop;     /* Bitmask of all WhereLoop objects in this path */
140492  Bitmask revLoop;      /* aLoop[]s that should be reversed for ORDER BY */
140493  LogEst nRow;          /* Estimated number of rows generated by this path */
140494  LogEst rCost;         /* Total cost of this path */
140495  LogEst rUnsorted;     /* Total cost of this path ignoring sorting costs */
140496  i8 isOrdered;         /* No. of ORDER BY terms satisfied. -1 for unknown */
140497  WhereLoop **aLoop;    /* Array of WhereLoop objects implementing this path */
140498};
140499
140500/*
140501** The query generator uses an array of instances of this structure to
140502** help it analyze the subexpressions of the WHERE clause.  Each WHERE
140503** clause subexpression is separated from the others by AND operators,
140504** usually, or sometimes subexpressions separated by OR.
140505**
140506** All WhereTerms are collected into a single WhereClause structure.
140507** The following identity holds:
140508**
140509**        WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
140510**
140511** When a term is of the form:
140512**
140513**              X <op> <expr>
140514**
140515** where X is a column name and <op> is one of certain operators,
140516** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
140517** cursor number and column number for X.  WhereTerm.eOperator records
140518** the <op> using a bitmask encoding defined by WO_xxx below.  The
140519** use of a bitmask encoding for the operator allows us to search
140520** quickly for terms that match any of several different operators.
140521**
140522** A WhereTerm might also be two or more subterms connected by OR:
140523**
140524**         (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
140525**
140526** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
140527** and the WhereTerm.u.pOrInfo field points to auxiliary information that
140528** is collected about the OR clause.
140529**
140530** If a term in the WHERE clause does not match either of the two previous
140531** categories, then eOperator==0.  The WhereTerm.pExpr field is still set
140532** to the original subexpression content and wtFlags is set up appropriately
140533** but no other fields in the WhereTerm object are meaningful.
140534**
140535** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
140536** but they do so indirectly.  A single WhereMaskSet structure translates
140537** cursor number into bits and the translated bit is stored in the prereq
140538** fields.  The translation is used in order to maximize the number of
140539** bits that will fit in a Bitmask.  The VDBE cursor numbers might be
140540** spread out over the non-negative integers.  For example, the cursor
140541** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45.  The WhereMaskSet
140542** translates these sparse cursor numbers into consecutive integers
140543** beginning with 0 in order to make the best possible use of the available
140544** bits in the Bitmask.  So, in the example above, the cursor numbers
140545** would be mapped into integers 0 through 7.
140546**
140547** The number of terms in a join is limited by the number of bits
140548** in prereqRight and prereqAll.  The default is 64 bits, hence SQLite
140549** is only able to process joins with 64 or fewer tables.
140550*/
140551struct WhereTerm {
140552  Expr *pExpr;            /* Pointer to the subexpression that is this term */
140553  WhereClause *pWC;       /* The clause this term is part of */
140554  LogEst truthProb;       /* Probability of truth for this expression */
140555  u16 wtFlags;            /* TERM_xxx bit flags.  See below */
140556  u16 eOperator;          /* A WO_xx value describing <op> */
140557  u8 nChild;              /* Number of children that must disable us */
140558  u8 eMatchOp;            /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */
140559  int iParent;            /* Disable pWC->a[iParent] when this term disabled */
140560  int leftCursor;         /* Cursor number of X in "X <op> <expr>" */
140561  int iField;             /* Field in (?,?,?) IN (SELECT...) vector */
140562  union {
140563    int leftColumn;         /* Column number of X in "X <op> <expr>" */
140564    WhereOrInfo *pOrInfo;   /* Extra information if (eOperator & WO_OR)!=0 */
140565    WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
140566  } u;
140567  Bitmask prereqRight;    /* Bitmask of tables used by pExpr->pRight */
140568  Bitmask prereqAll;      /* Bitmask of tables referenced by pExpr */
140569};
140570
140571/*
140572** Allowed values of WhereTerm.wtFlags
140573*/
140574#define TERM_DYNAMIC    0x0001 /* Need to call sqlite3ExprDelete(db, pExpr) */
140575#define TERM_VIRTUAL    0x0002 /* Added by the optimizer.  Do not code */
140576#define TERM_CODED      0x0004 /* This term is already coded */
140577#define TERM_COPIED     0x0008 /* Has a child */
140578#define TERM_ORINFO     0x0010 /* Need to free the WhereTerm.u.pOrInfo object */
140579#define TERM_ANDINFO    0x0020 /* Need to free the WhereTerm.u.pAndInfo obj */
140580#define TERM_OR_OK      0x0040 /* Used during OR-clause processing */
140581#ifdef SQLITE_ENABLE_STAT4
140582#  define TERM_VNULL    0x0080 /* Manufactured x>NULL or x<=NULL term */
140583#else
140584#  define TERM_VNULL    0x0000 /* Disabled if not using stat4 */
140585#endif
140586#define TERM_LIKEOPT    0x0100 /* Virtual terms from the LIKE optimization */
140587#define TERM_LIKECOND   0x0200 /* Conditionally this LIKE operator term */
140588#define TERM_LIKE       0x0400 /* The original LIKE operator */
140589#define TERM_IS         0x0800 /* Term.pExpr is an IS operator */
140590#define TERM_VARSELECT  0x1000 /* Term.pExpr contains a correlated sub-query */
140591#define TERM_HEURTRUTH  0x2000 /* Heuristic truthProb used */
140592#ifdef SQLITE_ENABLE_STAT4
140593#  define TERM_HIGHTRUTH  0x4000 /* Term excludes few rows */
140594#else
140595#  define TERM_HIGHTRUTH  0      /* Only used with STAT4 */
140596#endif
140597
140598/*
140599** An instance of the WhereScan object is used as an iterator for locating
140600** terms in the WHERE clause that are useful to the query planner.
140601*/
140602struct WhereScan {
140603  WhereClause *pOrigWC;      /* Original, innermost WhereClause */
140604  WhereClause *pWC;          /* WhereClause currently being scanned */
140605  const char *zCollName;     /* Required collating sequence, if not NULL */
140606  Expr *pIdxExpr;            /* Search for this index expression */
140607  char idxaff;               /* Must match this affinity, if zCollName!=NULL */
140608  unsigned char nEquiv;      /* Number of entries in aEquiv[] */
140609  unsigned char iEquiv;      /* Next unused slot in aEquiv[] */
140610  u32 opMask;                /* Acceptable operators */
140611  int k;                     /* Resume scanning at this->pWC->a[this->k] */
140612  int aiCur[11];             /* Cursors in the equivalence class */
140613  i16 aiColumn[11];          /* Corresponding column number in the eq-class */
140614};
140615
140616/*
140617** An instance of the following structure holds all information about a
140618** WHERE clause.  Mostly this is a container for one or more WhereTerms.
140619**
140620** Explanation of pOuter:  For a WHERE clause of the form
140621**
140622**           a AND ((b AND c) OR (d AND e)) AND f
140623**
140624** There are separate WhereClause objects for the whole clause and for
140625** the subclauses "(b AND c)" and "(d AND e)".  The pOuter field of the
140626** subclauses points to the WhereClause object for the whole clause.
140627*/
140628struct WhereClause {
140629  WhereInfo *pWInfo;       /* WHERE clause processing context */
140630  WhereClause *pOuter;     /* Outer conjunction */
140631  u8 op;                   /* Split operator.  TK_AND or TK_OR */
140632  u8 hasOr;                /* True if any a[].eOperator is WO_OR */
140633  int nTerm;               /* Number of terms */
140634  int nSlot;               /* Number of entries in a[] */
140635  WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
140636#if defined(SQLITE_SMALL_STACK)
140637  WhereTerm aStatic[1];    /* Initial static space for a[] */
140638#else
140639  WhereTerm aStatic[8];    /* Initial static space for a[] */
140640#endif
140641};
140642
140643/*
140644** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
140645** a dynamically allocated instance of the following structure.
140646*/
140647struct WhereOrInfo {
140648  WhereClause wc;          /* Decomposition into subterms */
140649  Bitmask indexable;       /* Bitmask of all indexable tables in the clause */
140650};
140651
140652/*
140653** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
140654** a dynamically allocated instance of the following structure.
140655*/
140656struct WhereAndInfo {
140657  WhereClause wc;          /* The subexpression broken out */
140658};
140659
140660/*
140661** An instance of the following structure keeps track of a mapping
140662** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
140663**
140664** The VDBE cursor numbers are small integers contained in
140665** SrcList_item.iCursor and Expr.iTable fields.  For any given WHERE
140666** clause, the cursor numbers might not begin with 0 and they might
140667** contain gaps in the numbering sequence.  But we want to make maximum
140668** use of the bits in our bitmasks.  This structure provides a mapping
140669** from the sparse cursor numbers into consecutive integers beginning
140670** with 0.
140671**
140672** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
140673** corresponds VDBE cursor number B.  The A-th bit of a bitmask is 1<<A.
140674**
140675** For example, if the WHERE clause expression used these VDBE
140676** cursors:  4, 5, 8, 29, 57, 73.  Then the  WhereMaskSet structure
140677** would map those cursor numbers into bits 0 through 5.
140678**
140679** Note that the mapping is not necessarily ordered.  In the example
140680** above, the mapping might go like this:  4->3, 5->1, 8->2, 29->0,
140681** 57->5, 73->4.  Or one of 719 other combinations might be used. It
140682** does not really matter.  What is important is that sparse cursor
140683** numbers all get mapped into bit numbers that begin with 0 and contain
140684** no gaps.
140685*/
140686struct WhereMaskSet {
140687  int bVarSelect;               /* Used by sqlite3WhereExprUsage() */
140688  int n;                        /* Number of assigned cursor values */
140689  int ix[BMS];                  /* Cursor assigned to each bit */
140690};
140691
140692/*
140693** Initialize a WhereMaskSet object
140694*/
140695#define initMaskSet(P)  (P)->n=0
140696
140697/*
140698** This object is a convenience wrapper holding all information needed
140699** to construct WhereLoop objects for a particular query.
140700*/
140701struct WhereLoopBuilder {
140702  WhereInfo *pWInfo;        /* Information about this WHERE */
140703  WhereClause *pWC;         /* WHERE clause terms */
140704  ExprList *pOrderBy;       /* ORDER BY clause */
140705  WhereLoop *pNew;          /* Template WhereLoop */
140706  WhereOrSet *pOrSet;       /* Record best loops here, if not NULL */
140707#ifdef SQLITE_ENABLE_STAT4
140708  UnpackedRecord *pRec;     /* Probe for stat4 (if required) */
140709  int nRecValid;            /* Number of valid fields currently in pRec */
140710#endif
140711  unsigned char bldFlags1;  /* First set of SQLITE_BLDF_* flags */
140712  unsigned char bldFlags2;  /* Second set of SQLITE_BLDF_* flags */
140713  unsigned int iPlanLimit;  /* Search limiter */
140714};
140715
140716/* Allowed values for WhereLoopBuider.bldFlags */
140717#define SQLITE_BLDF1_INDEXED  0x0001   /* An index is used */
140718#define SQLITE_BLDF1_UNIQUE   0x0002   /* All keys of a UNIQUE index used */
140719
140720#define SQLITE_BLDF2_2NDPASS  0x0004   /* Second builder pass needed */
140721
140722/* The WhereLoopBuilder.iPlanLimit is used to limit the number of
140723** index+constraint combinations the query planner will consider for a
140724** particular query.  If this parameter is unlimited, then certain
140725** pathological queries can spend excess time in the sqlite3WhereBegin()
140726** routine.  The limit is high enough that is should not impact real-world
140727** queries.
140728**
140729** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit.  The limit is
140730** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM
140731** clause is processed, so that every table in a join is guaranteed to be
140732** able to propose a some index+constraint combinations even if the initial
140733** baseline limit was exhausted by prior tables of the join.
140734*/
140735#ifndef SQLITE_QUERY_PLANNER_LIMIT
140736# define SQLITE_QUERY_PLANNER_LIMIT 20000
140737#endif
140738#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR
140739# define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000
140740#endif
140741
140742/*
140743** Each instance of this object records a change to a single node
140744** in an expression tree to cause that node to point to a column
140745** of an index rather than an expression or a virtual column.  All
140746** such transformations need to be undone at the end of WHERE clause
140747** processing.
140748*/
140749typedef struct WhereExprMod WhereExprMod;
140750struct WhereExprMod {
140751  WhereExprMod *pNext;  /* Next translation on a list of them all */
140752  Expr *pExpr;          /* The Expr node that was transformed */
140753  Expr orig;            /* Original value of the Expr node */
140754};
140755
140756/*
140757** The WHERE clause processing routine has two halves.  The
140758** first part does the start of the WHERE loop and the second
140759** half does the tail of the WHERE loop.  An instance of
140760** this structure is returned by the first half and passed
140761** into the second half to give some continuity.
140762**
140763** An instance of this object holds the complete state of the query
140764** planner.
140765*/
140766struct WhereInfo {
140767  Parse *pParse;            /* Parsing and code generating context */
140768  SrcList *pTabList;        /* List of tables in the join */
140769  ExprList *pOrderBy;       /* The ORDER BY clause or NULL */
140770  ExprList *pResultSet;     /* Result set of the query */
140771  Expr *pWhere;             /* The complete WHERE clause */
140772  int aiCurOnePass[2];      /* OP_OpenWrite cursors for the ONEPASS opt */
140773  int iContinue;            /* Jump here to continue with next record */
140774  int iBreak;               /* Jump here to break out of the loop */
140775  int savedNQueryLoop;      /* pParse->nQueryLoop outside the WHERE loop */
140776  u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
140777  LogEst iLimit;            /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */
140778  u8 nLevel;                /* Number of nested loop */
140779  i8 nOBSat;                /* Number of ORDER BY terms satisfied by indices */
140780  u8 eOnePass;              /* ONEPASS_OFF, or _SINGLE, or _MULTI */
140781  u8 eDistinct;             /* One of the WHERE_DISTINCT_* values */
140782  unsigned bDeferredSeek :1;   /* Uses OP_DeferredSeek */
140783  unsigned untestedTerms :1;   /* Not all WHERE terms resolved by outer loop */
140784  unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
140785  unsigned sorted :1;          /* True if really sorted (not just grouped) */
140786  LogEst nRowOut;           /* Estimated number of output rows */
140787  int iTop;                 /* The very beginning of the WHERE loop */
140788  WhereLoop *pLoops;        /* List of all WhereLoop objects */
140789  WhereExprMod *pExprMods;  /* Expression modifications */
140790  Bitmask revMask;          /* Mask of ORDER BY terms that need reversing */
140791  WhereClause sWC;          /* Decomposition of the WHERE clause */
140792  WhereMaskSet sMaskSet;    /* Map cursor numbers to bitmasks */
140793  WhereLevel a[1];          /* Information about each nest loop in WHERE */
140794};
140795
140796/*
140797** Private interfaces - callable only by other where.c routines.
140798**
140799** where.c:
140800*/
140801SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
140802#ifdef WHERETRACE_ENABLED
140803SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC);
140804SQLITE_PRIVATE void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm);
140805SQLITE_PRIVATE void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC);
140806#endif
140807SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
140808  WhereClause *pWC,     /* The WHERE clause to be searched */
140809  int iCur,             /* Cursor number of LHS */
140810  int iColumn,          /* Column number of LHS */
140811  Bitmask notReady,     /* RHS must not overlap with this mask */
140812  u32 op,               /* Mask of WO_xx values describing operator */
140813  Index *pIdx           /* Must be compatible with this index, if not NULL */
140814);
140815
140816/* wherecode.c: */
140817#ifndef SQLITE_OMIT_EXPLAIN
140818SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
140819  Parse *pParse,                  /* Parse context */
140820  SrcList *pTabList,              /* Table list this loop refers to */
140821  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
140822  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
140823);
140824#else
140825# define sqlite3WhereExplainOneScan(u,v,w,x) 0
140826#endif /* SQLITE_OMIT_EXPLAIN */
140827#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
140828SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
140829  Vdbe *v,                        /* Vdbe to add scanstatus entry to */
140830  SrcList *pSrclist,              /* FROM clause pLvl reads data from */
140831  WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
140832  int addrExplain                 /* Address of OP_Explain (or 0) */
140833);
140834#else
140835# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
140836#endif
140837SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
140838  Parse *pParse,       /* Parsing context */
140839  Vdbe *v,             /* Prepared statement under construction */
140840  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
140841  int iLevel,          /* Which level of pWInfo->a[] should be coded */
140842  WhereLevel *pLevel,  /* The current level pointer */
140843  Bitmask notReady     /* Which tables are currently available */
140844);
140845
140846/* whereexpr.c: */
140847SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
140848SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*);
140849SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8);
140850SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
140851SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*);
140852SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
140853SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
140854SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, struct SrcList_item*, WhereClause*);
140855
140856
140857
140858
140859
140860/*
140861** Bitmasks for the operators on WhereTerm objects.  These are all
140862** operators that are of interest to the query planner.  An
140863** OR-ed combination of these values can be used when searching for
140864** particular WhereTerms within a WhereClause.
140865**
140866** Value constraints:
140867**     WO_EQ    == SQLITE_INDEX_CONSTRAINT_EQ
140868**     WO_LT    == SQLITE_INDEX_CONSTRAINT_LT
140869**     WO_LE    == SQLITE_INDEX_CONSTRAINT_LE
140870**     WO_GT    == SQLITE_INDEX_CONSTRAINT_GT
140871**     WO_GE    == SQLITE_INDEX_CONSTRAINT_GE
140872*/
140873#define WO_IN     0x0001
140874#define WO_EQ     0x0002
140875#define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
140876#define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
140877#define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
140878#define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
140879#define WO_AUX    0x0040       /* Op useful to virtual tables only */
140880#define WO_IS     0x0080
140881#define WO_ISNULL 0x0100
140882#define WO_OR     0x0200       /* Two or more OR-connected terms */
140883#define WO_AND    0x0400       /* Two or more AND-connected terms */
140884#define WO_EQUIV  0x0800       /* Of the form A==B, both columns */
140885#define WO_NOOP   0x1000       /* This term does not restrict search space */
140886
140887#define WO_ALL    0x1fff       /* Mask of all possible WO_* values */
140888#define WO_SINGLE 0x01ff       /* Mask of all non-compound WO_* values */
140889
140890/*
140891** These are definitions of bits in the WhereLoop.wsFlags field.
140892** The particular combination of bits in each WhereLoop help to
140893** determine the algorithm that WhereLoop represents.
140894*/
140895#define WHERE_COLUMN_EQ    0x00000001  /* x=EXPR */
140896#define WHERE_COLUMN_RANGE 0x00000002  /* x<EXPR and/or x>EXPR */
140897#define WHERE_COLUMN_IN    0x00000004  /* x IN (...) */
140898#define WHERE_COLUMN_NULL  0x00000008  /* x IS NULL */
140899#define WHERE_CONSTRAINT   0x0000000f  /* Any of the WHERE_COLUMN_xxx values */
140900#define WHERE_TOP_LIMIT    0x00000010  /* x<EXPR or x<=EXPR constraint */
140901#define WHERE_BTM_LIMIT    0x00000020  /* x>EXPR or x>=EXPR constraint */
140902#define WHERE_BOTH_LIMIT   0x00000030  /* Both x>EXPR and x<EXPR */
140903#define WHERE_IDX_ONLY     0x00000040  /* Use index only - omit table */
140904#define WHERE_IPK          0x00000100  /* x is the INTEGER PRIMARY KEY */
140905#define WHERE_INDEXED      0x00000200  /* WhereLoop.u.btree.pIndex is valid */
140906#define WHERE_VIRTUALTABLE 0x00000400  /* WhereLoop.u.vtab is valid */
140907#define WHERE_IN_ABLE      0x00000800  /* Able to support an IN operator */
140908#define WHERE_ONEROW       0x00001000  /* Selects no more than one row */
140909#define WHERE_MULTI_OR     0x00002000  /* OR using multiple indices */
140910#define WHERE_AUTO_INDEX   0x00004000  /* Uses an ephemeral index */
140911#define WHERE_SKIPSCAN     0x00008000  /* Uses the skip-scan algorithm */
140912#define WHERE_UNQ_WANTED   0x00010000  /* WHERE_ONEROW would have been helpful*/
140913#define WHERE_PARTIALIDX   0x00020000  /* The automatic index is partial */
140914#define WHERE_IN_EARLYOUT  0x00040000  /* Perhaps quit IN loops early */
140915#define WHERE_BIGNULL_SORT 0x00080000  /* Column nEq of index is BIGNULL */
140916
140917#endif /* !defined(SQLITE_WHEREINT_H) */
140918
140919/************** End of whereInt.h ********************************************/
140920/************** Continuing where we left off in wherecode.c ******************/
140921
140922#ifndef SQLITE_OMIT_EXPLAIN
140923
140924/*
140925** Return the name of the i-th column of the pIdx index.
140926*/
140927static const char *explainIndexColumnName(Index *pIdx, int i){
140928  i = pIdx->aiColumn[i];
140929  if( i==XN_EXPR ) return "<expr>";
140930  if( i==XN_ROWID ) return "rowid";
140931  return pIdx->pTable->aCol[i].zName;
140932}
140933
140934/*
140935** This routine is a helper for explainIndexRange() below
140936**
140937** pStr holds the text of an expression that we are building up one term
140938** at a time.  This routine adds a new term to the end of the expression.
140939** Terms are separated by AND so add the "AND" text for second and subsequent
140940** terms only.
140941*/
140942static void explainAppendTerm(
140943  StrAccum *pStr,             /* The text expression being built */
140944  Index *pIdx,                /* Index to read column names from */
140945  int nTerm,                  /* Number of terms */
140946  int iTerm,                  /* Zero-based index of first term. */
140947  int bAnd,                   /* Non-zero to append " AND " */
140948  const char *zOp             /* Name of the operator */
140949){
140950  int i;
140951
140952  assert( nTerm>=1 );
140953  if( bAnd ) sqlite3_str_append(pStr, " AND ", 5);
140954
140955  if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
140956  for(i=0; i<nTerm; i++){
140957    if( i ) sqlite3_str_append(pStr, ",", 1);
140958    sqlite3_str_appendall(pStr, explainIndexColumnName(pIdx, iTerm+i));
140959  }
140960  if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
140961
140962  sqlite3_str_append(pStr, zOp, 1);
140963
140964  if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
140965  for(i=0; i<nTerm; i++){
140966    if( i ) sqlite3_str_append(pStr, ",", 1);
140967    sqlite3_str_append(pStr, "?", 1);
140968  }
140969  if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
140970}
140971
140972/*
140973** Argument pLevel describes a strategy for scanning table pTab. This
140974** function appends text to pStr that describes the subset of table
140975** rows scanned by the strategy in the form of an SQL expression.
140976**
140977** For example, if the query:
140978**
140979**   SELECT * FROM t1 WHERE a=1 AND b>2;
140980**
140981** is run and there is an index on (a, b), then this function returns a
140982** string similar to:
140983**
140984**   "a=? AND b>?"
140985*/
140986static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){
140987  Index *pIndex = pLoop->u.btree.pIndex;
140988  u16 nEq = pLoop->u.btree.nEq;
140989  u16 nSkip = pLoop->nSkip;
140990  int i, j;
140991
140992  if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
140993  sqlite3_str_append(pStr, " (", 2);
140994  for(i=0; i<nEq; i++){
140995    const char *z = explainIndexColumnName(pIndex, i);
140996    if( i ) sqlite3_str_append(pStr, " AND ", 5);
140997    sqlite3_str_appendf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
140998  }
140999
141000  j = i;
141001  if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
141002    explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
141003    i = 1;
141004  }
141005  if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
141006    explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
141007  }
141008  sqlite3_str_append(pStr, ")", 1);
141009}
141010
141011/*
141012** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
141013** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
141014** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
141015** is added to the output to describe the table scan strategy in pLevel.
141016**
141017** If an OP_Explain opcode is added to the VM, its address is returned.
141018** Otherwise, if no OP_Explain is coded, zero is returned.
141019*/
141020SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
141021  Parse *pParse,                  /* Parse context */
141022  SrcList *pTabList,              /* Table list this loop refers to */
141023  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
141024  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
141025){
141026  int ret = 0;
141027#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
141028  if( sqlite3ParseToplevel(pParse)->explain==2 )
141029#endif
141030  {
141031    struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
141032    Vdbe *v = pParse->pVdbe;      /* VM being constructed */
141033    sqlite3 *db = pParse->db;     /* Database handle */
141034    int isSearch;                 /* True for a SEARCH. False for SCAN. */
141035    WhereLoop *pLoop;             /* The controlling WhereLoop object */
141036    u32 flags;                    /* Flags that describe this loop */
141037    char *zMsg;                   /* Text to add to EQP output */
141038    StrAccum str;                 /* EQP output string */
141039    char zBuf[100];               /* Initial space for EQP output string */
141040
141041    pLoop = pLevel->pWLoop;
141042    flags = pLoop->wsFlags;
141043    if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_OR_SUBCLAUSE) ) return 0;
141044
141045    isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
141046            || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
141047            || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
141048
141049    sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
141050    sqlite3_str_appendall(&str, isSearch ? "SEARCH" : "SCAN");
141051    if( pItem->pSelect ){
141052      sqlite3_str_appendf(&str, " SUBQUERY %u", pItem->pSelect->selId);
141053    }else{
141054      sqlite3_str_appendf(&str, " TABLE %s", pItem->zName);
141055    }
141056
141057    if( pItem->zAlias ){
141058      sqlite3_str_appendf(&str, " AS %s", pItem->zAlias);
141059    }
141060    if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
141061      const char *zFmt = 0;
141062      Index *pIdx;
141063
141064      assert( pLoop->u.btree.pIndex!=0 );
141065      pIdx = pLoop->u.btree.pIndex;
141066      assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
141067      if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
141068        if( isSearch ){
141069          zFmt = "PRIMARY KEY";
141070        }
141071      }else if( flags & WHERE_PARTIALIDX ){
141072        zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
141073      }else if( flags & WHERE_AUTO_INDEX ){
141074        zFmt = "AUTOMATIC COVERING INDEX";
141075      }else if( flags & WHERE_IDX_ONLY ){
141076        zFmt = "COVERING INDEX %s";
141077      }else{
141078        zFmt = "INDEX %s";
141079      }
141080      if( zFmt ){
141081        sqlite3_str_append(&str, " USING ", 7);
141082        sqlite3_str_appendf(&str, zFmt, pIdx->zName);
141083        explainIndexRange(&str, pLoop);
141084      }
141085    }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
141086      const char *zRangeOp;
141087      if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
141088        zRangeOp = "=";
141089      }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
141090        zRangeOp = ">? AND rowid<";
141091      }else if( flags&WHERE_BTM_LIMIT ){
141092        zRangeOp = ">";
141093      }else{
141094        assert( flags&WHERE_TOP_LIMIT);
141095        zRangeOp = "<";
141096      }
141097      sqlite3_str_appendf(&str,
141098          " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp);
141099    }
141100#ifndef SQLITE_OMIT_VIRTUALTABLE
141101    else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
141102      sqlite3_str_appendf(&str, " VIRTUAL TABLE INDEX %d:%s",
141103                  pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
141104    }
141105#endif
141106#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
141107    if( pLoop->nOut>=10 ){
141108      sqlite3_str_appendf(&str, " (~%llu rows)",
141109             sqlite3LogEstToInt(pLoop->nOut));
141110    }else{
141111      sqlite3_str_append(&str, " (~1 row)", 9);
141112    }
141113#endif
141114    zMsg = sqlite3StrAccumFinish(&str);
141115    sqlite3ExplainBreakpoint("",zMsg);
141116    ret = sqlite3VdbeAddOp4(v, OP_Explain, sqlite3VdbeCurrentAddr(v),
141117                            pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
141118  }
141119  return ret;
141120}
141121#endif /* SQLITE_OMIT_EXPLAIN */
141122
141123#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
141124/*
141125** Configure the VM passed as the first argument with an
141126** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
141127** implement level pLvl. Argument pSrclist is a pointer to the FROM
141128** clause that the scan reads data from.
141129**
141130** If argument addrExplain is not 0, it must be the address of an
141131** OP_Explain instruction that describes the same loop.
141132*/
141133SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
141134  Vdbe *v,                        /* Vdbe to add scanstatus entry to */
141135  SrcList *pSrclist,              /* FROM clause pLvl reads data from */
141136  WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
141137  int addrExplain                 /* Address of OP_Explain (or 0) */
141138){
141139  const char *zObj = 0;
141140  WhereLoop *pLoop = pLvl->pWLoop;
141141  if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0  &&  pLoop->u.btree.pIndex!=0 ){
141142    zObj = pLoop->u.btree.pIndex->zName;
141143  }else{
141144    zObj = pSrclist->a[pLvl->iFrom].zName;
141145  }
141146  sqlite3VdbeScanStatus(
141147      v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
141148  );
141149}
141150#endif
141151
141152
141153/*
141154** Disable a term in the WHERE clause.  Except, do not disable the term
141155** if it controls a LEFT OUTER JOIN and it did not originate in the ON
141156** or USING clause of that join.
141157**
141158** Consider the term t2.z='ok' in the following queries:
141159**
141160**   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
141161**   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
141162**   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
141163**
141164** The t2.z='ok' is disabled in the in (2) because it originates
141165** in the ON clause.  The term is disabled in (3) because it is not part
141166** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
141167**
141168** Disabling a term causes that term to not be tested in the inner loop
141169** of the join.  Disabling is an optimization.  When terms are satisfied
141170** by indices, we disable them to prevent redundant tests in the inner
141171** loop.  We would get the correct results if nothing were ever disabled,
141172** but joins might run a little slower.  The trick is to disable as much
141173** as we can without disabling too much.  If we disabled in (1), we'd get
141174** the wrong answer.  See ticket #813.
141175**
141176** If all the children of a term are disabled, then that term is also
141177** automatically disabled.  In this way, terms get disabled if derived
141178** virtual terms are tested first.  For example:
141179**
141180**      x GLOB 'abc*' AND x>='abc' AND x<'acd'
141181**      \___________/     \______/     \_____/
141182**         parent          child1       child2
141183**
141184** Only the parent term was in the original WHERE clause.  The child1
141185** and child2 terms were added by the LIKE optimization.  If both of
141186** the virtual child terms are valid, then testing of the parent can be
141187** skipped.
141188**
141189** Usually the parent term is marked as TERM_CODED.  But if the parent
141190** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
141191** The TERM_LIKECOND marking indicates that the term should be coded inside
141192** a conditional such that is only evaluated on the second pass of a
141193** LIKE-optimization loop, when scanning BLOBs instead of strings.
141194*/
141195static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
141196  int nLoop = 0;
141197  assert( pTerm!=0 );
141198  while( (pTerm->wtFlags & TERM_CODED)==0
141199      && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
141200      && (pLevel->notReady & pTerm->prereqAll)==0
141201  ){
141202    if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
141203      pTerm->wtFlags |= TERM_LIKECOND;
141204    }else{
141205      pTerm->wtFlags |= TERM_CODED;
141206    }
141207    if( pTerm->iParent<0 ) break;
141208    pTerm = &pTerm->pWC->a[pTerm->iParent];
141209    assert( pTerm!=0 );
141210    pTerm->nChild--;
141211    if( pTerm->nChild!=0 ) break;
141212    nLoop++;
141213  }
141214}
141215
141216/*
141217** Code an OP_Affinity opcode to apply the column affinity string zAff
141218** to the n registers starting at base.
141219**
141220** As an optimization, SQLITE_AFF_BLOB and SQLITE_AFF_NONE entries (which
141221** are no-ops) at the beginning and end of zAff are ignored.  If all entries
141222** in zAff are SQLITE_AFF_BLOB or SQLITE_AFF_NONE, then no code gets generated.
141223**
141224** This routine makes its own copy of zAff so that the caller is free
141225** to modify zAff after this routine returns.
141226*/
141227static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
141228  Vdbe *v = pParse->pVdbe;
141229  if( zAff==0 ){
141230    assert( pParse->db->mallocFailed );
141231    return;
141232  }
141233  assert( v!=0 );
141234
141235  /* Adjust base and n to skip over SQLITE_AFF_BLOB and SQLITE_AFF_NONE
141236  ** entries at the beginning and end of the affinity string.
141237  */
141238  assert( SQLITE_AFF_NONE<SQLITE_AFF_BLOB );
141239  while( n>0 && zAff[0]<=SQLITE_AFF_BLOB ){
141240    n--;
141241    base++;
141242    zAff++;
141243  }
141244  while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){
141245    n--;
141246  }
141247
141248  /* Code the OP_Affinity opcode if there is anything left to do. */
141249  if( n>0 ){
141250    sqlite3VdbeAddOp4(v, OP_Affinity, base, n, 0, zAff, n);
141251  }
141252}
141253
141254/*
141255** Expression pRight, which is the RHS of a comparison operation, is
141256** either a vector of n elements or, if n==1, a scalar expression.
141257** Before the comparison operation, affinity zAff is to be applied
141258** to the pRight values. This function modifies characters within the
141259** affinity string to SQLITE_AFF_BLOB if either:
141260**
141261**   * the comparison will be performed with no affinity, or
141262**   * the affinity change in zAff is guaranteed not to change the value.
141263*/
141264static void updateRangeAffinityStr(
141265  Expr *pRight,                   /* RHS of comparison */
141266  int n,                          /* Number of vector elements in comparison */
141267  char *zAff                      /* Affinity string to modify */
141268){
141269  int i;
141270  for(i=0; i<n; i++){
141271    Expr *p = sqlite3VectorFieldSubexpr(pRight, i);
141272    if( sqlite3CompareAffinity(p, zAff[i])==SQLITE_AFF_BLOB
141273     || sqlite3ExprNeedsNoAffinityChange(p, zAff[i])
141274    ){
141275      zAff[i] = SQLITE_AFF_BLOB;
141276    }
141277  }
141278}
141279
141280
141281/*
141282** pX is an expression of the form:  (vector) IN (SELECT ...)
141283** In other words, it is a vector IN operator with a SELECT clause on the
141284** LHS.  But not all terms in the vector are indexable and the terms might
141285** not be in the correct order for indexing.
141286**
141287** This routine makes a copy of the input pX expression and then adjusts
141288** the vector on the LHS with corresponding changes to the SELECT so that
141289** the vector contains only index terms and those terms are in the correct
141290** order.  The modified IN expression is returned.  The caller is responsible
141291** for deleting the returned expression.
141292**
141293** Example:
141294**
141295**    CREATE TABLE t1(a,b,c,d,e,f);
141296**    CREATE INDEX t1x1 ON t1(e,c);
141297**    SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2)
141298**                           \_______________________________________/
141299**                                     The pX expression
141300**
141301** Since only columns e and c can be used with the index, in that order,
141302** the modified IN expression that is returned will be:
141303**
141304**        (e,c) IN (SELECT z,x FROM t2)
141305**
141306** The reduced pX is different from the original (obviously) and thus is
141307** only used for indexing, to improve performance.  The original unaltered
141308** IN expression must also be run on each output row for correctness.
141309*/
141310static Expr *removeUnindexableInClauseTerms(
141311  Parse *pParse,        /* The parsing context */
141312  int iEq,              /* Look at loop terms starting here */
141313  WhereLoop *pLoop,     /* The current loop */
141314  Expr *pX              /* The IN expression to be reduced */
141315){
141316  sqlite3 *db = pParse->db;
141317  Expr *pNew;
141318  pNew = sqlite3ExprDup(db, pX, 0);
141319  if( db->mallocFailed==0 ){
141320    ExprList *pOrigRhs = pNew->x.pSelect->pEList;  /* Original unmodified RHS */
141321    ExprList *pOrigLhs = pNew->pLeft->x.pList;     /* Original unmodified LHS */
141322    ExprList *pRhs = 0;         /* New RHS after modifications */
141323    ExprList *pLhs = 0;         /* New LHS after mods */
141324    int i;                      /* Loop counter */
141325    Select *pSelect;            /* Pointer to the SELECT on the RHS */
141326
141327    for(i=iEq; i<pLoop->nLTerm; i++){
141328      if( pLoop->aLTerm[i]->pExpr==pX ){
141329        int iField = pLoop->aLTerm[i]->iField - 1;
141330        if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
141331        pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
141332        pOrigRhs->a[iField].pExpr = 0;
141333        assert( pOrigLhs->a[iField].pExpr!=0 );
141334        pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
141335        pOrigLhs->a[iField].pExpr = 0;
141336      }
141337    }
141338    sqlite3ExprListDelete(db, pOrigRhs);
141339    sqlite3ExprListDelete(db, pOrigLhs);
141340    pNew->pLeft->x.pList = pLhs;
141341    pNew->x.pSelect->pEList = pRhs;
141342    if( pLhs && pLhs->nExpr==1 ){
141343      /* Take care here not to generate a TK_VECTOR containing only a
141344      ** single value. Since the parser never creates such a vector, some
141345      ** of the subroutines do not handle this case.  */
141346      Expr *p = pLhs->a[0].pExpr;
141347      pLhs->a[0].pExpr = 0;
141348      sqlite3ExprDelete(db, pNew->pLeft);
141349      pNew->pLeft = p;
141350    }
141351    pSelect = pNew->x.pSelect;
141352    if( pSelect->pOrderBy ){
141353      /* If the SELECT statement has an ORDER BY clause, zero the
141354      ** iOrderByCol variables. These are set to non-zero when an
141355      ** ORDER BY term exactly matches one of the terms of the
141356      ** result-set. Since the result-set of the SELECT statement may
141357      ** have been modified or reordered, these variables are no longer
141358      ** set correctly.  Since setting them is just an optimization,
141359      ** it's easiest just to zero them here.  */
141360      ExprList *pOrderBy = pSelect->pOrderBy;
141361      for(i=0; i<pOrderBy->nExpr; i++){
141362        pOrderBy->a[i].u.x.iOrderByCol = 0;
141363      }
141364    }
141365
141366#if 0
141367    printf("For indexing, change the IN expr:\n");
141368    sqlite3TreeViewExpr(0, pX, 0);
141369    printf("Into:\n");
141370    sqlite3TreeViewExpr(0, pNew, 0);
141371#endif
141372  }
141373  return pNew;
141374}
141375
141376
141377/*
141378** Generate code for a single equality term of the WHERE clause.  An equality
141379** term can be either X=expr or X IN (...).   pTerm is the term to be
141380** coded.
141381**
141382** The current value for the constraint is left in a register, the index
141383** of which is returned.  An attempt is made store the result in iTarget but
141384** this is only guaranteed for TK_ISNULL and TK_IN constraints.  If the
141385** constraint is a TK_EQ or TK_IS, then the current value might be left in
141386** some other register and it is the caller's responsibility to compensate.
141387**
141388** For a constraint of the form X=expr, the expression is evaluated in
141389** straight-line code.  For constraints of the form X IN (...)
141390** this routine sets up a loop that will iterate over all values of X.
141391*/
141392static int codeEqualityTerm(
141393  Parse *pParse,      /* The parsing context */
141394  WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
141395  WhereLevel *pLevel, /* The level of the FROM clause we are working on */
141396  int iEq,            /* Index of the equality term within this level */
141397  int bRev,           /* True for reverse-order IN operations */
141398  int iTarget         /* Attempt to leave results in this register */
141399){
141400  Expr *pX = pTerm->pExpr;
141401  Vdbe *v = pParse->pVdbe;
141402  int iReg;                  /* Register holding results */
141403
141404  assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
141405  assert( iTarget>0 );
141406  if( pX->op==TK_EQ || pX->op==TK_IS ){
141407    iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
141408  }else if( pX->op==TK_ISNULL ){
141409    iReg = iTarget;
141410    sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
141411#ifndef SQLITE_OMIT_SUBQUERY
141412  }else{
141413    int eType = IN_INDEX_NOOP;
141414    int iTab;
141415    struct InLoop *pIn;
141416    WhereLoop *pLoop = pLevel->pWLoop;
141417    int i;
141418    int nEq = 0;
141419    int *aiMap = 0;
141420
141421    if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
141422      && pLoop->u.btree.pIndex!=0
141423      && pLoop->u.btree.pIndex->aSortOrder[iEq]
141424    ){
141425      testcase( iEq==0 );
141426      testcase( bRev );
141427      bRev = !bRev;
141428    }
141429    assert( pX->op==TK_IN );
141430    iReg = iTarget;
141431
141432    for(i=0; i<iEq; i++){
141433      if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
141434        disableTerm(pLevel, pTerm);
141435        return iTarget;
141436      }
141437    }
141438    for(i=iEq;i<pLoop->nLTerm; i++){
141439      assert( pLoop->aLTerm[i]!=0 );
141440      if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
141441    }
141442
141443    iTab = 0;
141444    if( (pX->flags & EP_xIsSelect)==0 || pX->x.pSelect->pEList->nExpr==1 ){
141445      eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, 0, &iTab);
141446    }else{
141447      sqlite3 *db = pParse->db;
141448      pX = removeUnindexableInClauseTerms(pParse, iEq, pLoop, pX);
141449
141450      if( !db->mallocFailed ){
141451        aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
141452        eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap, &iTab);
141453        pTerm->pExpr->iTable = iTab;
141454      }
141455      sqlite3ExprDelete(db, pX);
141456      pX = pTerm->pExpr;
141457    }
141458
141459    if( eType==IN_INDEX_INDEX_DESC ){
141460      testcase( bRev );
141461      bRev = !bRev;
141462    }
141463    sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
141464    VdbeCoverageIf(v, bRev);
141465    VdbeCoverageIf(v, !bRev);
141466    assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
141467
141468    pLoop->wsFlags |= WHERE_IN_ABLE;
141469    if( pLevel->u.in.nIn==0 ){
141470      pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
141471    }
141472
141473    i = pLevel->u.in.nIn;
141474    pLevel->u.in.nIn += nEq;
141475    pLevel->u.in.aInLoop =
141476       sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
141477                              sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
141478    pIn = pLevel->u.in.aInLoop;
141479    if( pIn ){
141480      int iMap = 0;               /* Index in aiMap[] */
141481      pIn += i;
141482      for(i=iEq;i<pLoop->nLTerm; i++){
141483        if( pLoop->aLTerm[i]->pExpr==pX ){
141484          int iOut = iReg + i - iEq;
141485          if( eType==IN_INDEX_ROWID ){
141486            pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
141487          }else{
141488            int iCol = aiMap ? aiMap[iMap++] : 0;
141489            pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
141490          }
141491          sqlite3VdbeAddOp1(v, OP_IsNull, iOut); VdbeCoverage(v);
141492          if( i==iEq ){
141493            pIn->iCur = iTab;
141494            pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
141495            if( iEq>0 ){
141496              pIn->iBase = iReg - i;
141497              pIn->nPrefix = i;
141498              pLoop->wsFlags |= WHERE_IN_EARLYOUT;
141499            }else{
141500              pIn->nPrefix = 0;
141501            }
141502          }else{
141503            pIn->eEndLoopOp = OP_Noop;
141504          }
141505          pIn++;
141506        }
141507      }
141508    }else{
141509      pLevel->u.in.nIn = 0;
141510    }
141511    sqlite3DbFree(pParse->db, aiMap);
141512#endif
141513  }
141514  disableTerm(pLevel, pTerm);
141515  return iReg;
141516}
141517
141518/*
141519** Generate code that will evaluate all == and IN constraints for an
141520** index scan.
141521**
141522** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
141523** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
141524** The index has as many as three equality constraints, but in this
141525** example, the third "c" value is an inequality.  So only two
141526** constraints are coded.  This routine will generate code to evaluate
141527** a==5 and b IN (1,2,3).  The current values for a and b will be stored
141528** in consecutive registers and the index of the first register is returned.
141529**
141530** In the example above nEq==2.  But this subroutine works for any value
141531** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
141532** The only thing it does is allocate the pLevel->iMem memory cell and
141533** compute the affinity string.
141534**
141535** The nExtraReg parameter is 0 or 1.  It is 0 if all WHERE clause constraints
141536** are == or IN and are covered by the nEq.  nExtraReg is 1 if there is
141537** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
141538** occurs after the nEq quality constraints.
141539**
141540** This routine allocates a range of nEq+nExtraReg memory cells and returns
141541** the index of the first memory cell in that range. The code that
141542** calls this routine will use that memory range to store keys for
141543** start and termination conditions of the loop.
141544** key value of the loop.  If one or more IN operators appear, then
141545** this routine allocates an additional nEq memory cells for internal
141546** use.
141547**
141548** Before returning, *pzAff is set to point to a buffer containing a
141549** copy of the column affinity string of the index allocated using
141550** sqlite3DbMalloc(). Except, entries in the copy of the string associated
141551** with equality constraints that use BLOB or NONE affinity are set to
141552** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
141553**
141554**   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
141555**   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
141556**
141557** In the example above, the index on t1(a) has TEXT affinity. But since
141558** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
141559** no conversion should be attempted before using a t2.b value as part of
141560** a key to search the index. Hence the first byte in the returned affinity
141561** string in this example would be set to SQLITE_AFF_BLOB.
141562*/
141563static int codeAllEqualityTerms(
141564  Parse *pParse,        /* Parsing context */
141565  WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
141566  int bRev,             /* Reverse the order of IN operators */
141567  int nExtraReg,        /* Number of extra registers to allocate */
141568  char **pzAff          /* OUT: Set to point to affinity string */
141569){
141570  u16 nEq;                      /* The number of == or IN constraints to code */
141571  u16 nSkip;                    /* Number of left-most columns to skip */
141572  Vdbe *v = pParse->pVdbe;      /* The vm under construction */
141573  Index *pIdx;                  /* The index being used for this loop */
141574  WhereTerm *pTerm;             /* A single constraint term */
141575  WhereLoop *pLoop;             /* The WhereLoop object */
141576  int j;                        /* Loop counter */
141577  int regBase;                  /* Base register */
141578  int nReg;                     /* Number of registers to allocate */
141579  char *zAff;                   /* Affinity string to return */
141580
141581  /* This module is only called on query plans that use an index. */
141582  pLoop = pLevel->pWLoop;
141583  assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
141584  nEq = pLoop->u.btree.nEq;
141585  nSkip = pLoop->nSkip;
141586  pIdx = pLoop->u.btree.pIndex;
141587  assert( pIdx!=0 );
141588
141589  /* Figure out how many memory cells we will need then allocate them.
141590  */
141591  regBase = pParse->nMem + 1;
141592  nReg = pLoop->u.btree.nEq + nExtraReg;
141593  pParse->nMem += nReg;
141594
141595  zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
141596  assert( zAff!=0 || pParse->db->mallocFailed );
141597
141598  if( nSkip ){
141599    int iIdxCur = pLevel->iIdxCur;
141600    sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
141601    VdbeCoverageIf(v, bRev==0);
141602    VdbeCoverageIf(v, bRev!=0);
141603    VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
141604    j = sqlite3VdbeAddOp0(v, OP_Goto);
141605    pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
141606                            iIdxCur, 0, regBase, nSkip);
141607    VdbeCoverageIf(v, bRev==0);
141608    VdbeCoverageIf(v, bRev!=0);
141609    sqlite3VdbeJumpHere(v, j);
141610    for(j=0; j<nSkip; j++){
141611      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
141612      testcase( pIdx->aiColumn[j]==XN_EXPR );
141613      VdbeComment((v, "%s", explainIndexColumnName(pIdx, j)));
141614    }
141615  }
141616
141617  /* Evaluate the equality constraints
141618  */
141619  assert( zAff==0 || (int)strlen(zAff)>=nEq );
141620  for(j=nSkip; j<nEq; j++){
141621    int r1;
141622    pTerm = pLoop->aLTerm[j];
141623    assert( pTerm!=0 );
141624    /* The following testcase is true for indices with redundant columns.
141625    ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
141626    testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
141627    testcase( pTerm->wtFlags & TERM_VIRTUAL );
141628    r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
141629    if( r1!=regBase+j ){
141630      if( nReg==1 ){
141631        sqlite3ReleaseTempReg(pParse, regBase);
141632        regBase = r1;
141633      }else{
141634        sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
141635      }
141636    }
141637    if( pTerm->eOperator & WO_IN ){
141638      if( pTerm->pExpr->flags & EP_xIsSelect ){
141639        /* No affinity ever needs to be (or should be) applied to a value
141640        ** from the RHS of an "? IN (SELECT ...)" expression. The
141641        ** sqlite3FindInIndex() routine has already ensured that the
141642        ** affinity of the comparison has been applied to the value.  */
141643        if( zAff ) zAff[j] = SQLITE_AFF_BLOB;
141644      }
141645    }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
141646      Expr *pRight = pTerm->pExpr->pRight;
141647      if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
141648        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
141649        VdbeCoverage(v);
141650      }
141651      if( zAff ){
141652        if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){
141653          zAff[j] = SQLITE_AFF_BLOB;
141654        }
141655        if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
141656          zAff[j] = SQLITE_AFF_BLOB;
141657        }
141658      }
141659    }
141660  }
141661  *pzAff = zAff;
141662  return regBase;
141663}
141664
141665#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
141666/*
141667** If the most recently coded instruction is a constant range constraint
141668** (a string literal) that originated from the LIKE optimization, then
141669** set P3 and P5 on the OP_String opcode so that the string will be cast
141670** to a BLOB at appropriate times.
141671**
141672** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
141673** expression: "x>='ABC' AND x<'abd'".  But this requires that the range
141674** scan loop run twice, once for strings and a second time for BLOBs.
141675** The OP_String opcodes on the second pass convert the upper and lower
141676** bound string constants to blobs.  This routine makes the necessary changes
141677** to the OP_String opcodes for that to happen.
141678**
141679** Except, of course, if SQLITE_LIKE_DOESNT_MATCH_BLOBS is defined, then
141680** only the one pass through the string space is required, so this routine
141681** becomes a no-op.
141682*/
141683static void whereLikeOptimizationStringFixup(
141684  Vdbe *v,                /* prepared statement under construction */
141685  WhereLevel *pLevel,     /* The loop that contains the LIKE operator */
141686  WhereTerm *pTerm        /* The upper or lower bound just coded */
141687){
141688  if( pTerm->wtFlags & TERM_LIKEOPT ){
141689    VdbeOp *pOp;
141690    assert( pLevel->iLikeRepCntr>0 );
141691    pOp = sqlite3VdbeGetOp(v, -1);
141692    assert( pOp!=0 );
141693    assert( pOp->opcode==OP_String8
141694            || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
141695    pOp->p3 = (int)(pLevel->iLikeRepCntr>>1);  /* Register holding counter */
141696    pOp->p5 = (u8)(pLevel->iLikeRepCntr&1);    /* ASC or DESC */
141697  }
141698}
141699#else
141700# define whereLikeOptimizationStringFixup(A,B,C)
141701#endif
141702
141703#ifdef SQLITE_ENABLE_CURSOR_HINTS
141704/*
141705** Information is passed from codeCursorHint() down to individual nodes of
141706** the expression tree (by sqlite3WalkExpr()) using an instance of this
141707** structure.
141708*/
141709struct CCurHint {
141710  int iTabCur;    /* Cursor for the main table */
141711  int iIdxCur;    /* Cursor for the index, if pIdx!=0.  Unused otherwise */
141712  Index *pIdx;    /* The index used to access the table */
141713};
141714
141715/*
141716** This function is called for every node of an expression that is a candidate
141717** for a cursor hint on an index cursor.  For TK_COLUMN nodes that reference
141718** the table CCurHint.iTabCur, verify that the same column can be
141719** accessed through the index.  If it cannot, then set pWalker->eCode to 1.
141720*/
141721static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){
141722  struct CCurHint *pHint = pWalker->u.pCCurHint;
141723  assert( pHint->pIdx!=0 );
141724  if( pExpr->op==TK_COLUMN
141725   && pExpr->iTable==pHint->iTabCur
141726   && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
141727  ){
141728    pWalker->eCode = 1;
141729  }
141730  return WRC_Continue;
141731}
141732
141733/*
141734** Test whether or not expression pExpr, which was part of a WHERE clause,
141735** should be included in the cursor-hint for a table that is on the rhs
141736** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
141737** expression is not suitable.
141738**
141739** An expression is unsuitable if it might evaluate to non NULL even if
141740** a TK_COLUMN node that does affect the value of the expression is set
141741** to NULL. For example:
141742**
141743**   col IS NULL
141744**   col IS NOT NULL
141745**   coalesce(col, 1)
141746**   CASE WHEN col THEN 0 ELSE 1 END
141747*/
141748static int codeCursorHintIsOrFunction(Walker *pWalker, Expr *pExpr){
141749  if( pExpr->op==TK_IS
141750   || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
141751   || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
141752  ){
141753    pWalker->eCode = 1;
141754  }else if( pExpr->op==TK_FUNCTION ){
141755    int d1;
141756    char d2[4];
141757    if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
141758      pWalker->eCode = 1;
141759    }
141760  }
141761
141762  return WRC_Continue;
141763}
141764
141765
141766/*
141767** This function is called on every node of an expression tree used as an
141768** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN
141769** that accesses any table other than the one identified by
141770** CCurHint.iTabCur, then do the following:
141771**
141772**   1) allocate a register and code an OP_Column instruction to read
141773**      the specified column into the new register, and
141774**
141775**   2) transform the expression node to a TK_REGISTER node that reads
141776**      from the newly populated register.
141777**
141778** Also, if the node is a TK_COLUMN that does access the table idenified
141779** by pCCurHint.iTabCur, and an index is being used (which we will
141780** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into
141781** an access of the index rather than the original table.
141782*/
141783static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){
141784  int rc = WRC_Continue;
141785  struct CCurHint *pHint = pWalker->u.pCCurHint;
141786  if( pExpr->op==TK_COLUMN ){
141787    if( pExpr->iTable!=pHint->iTabCur ){
141788      int reg = ++pWalker->pParse->nMem;   /* Register for column value */
141789      sqlite3ExprCode(pWalker->pParse, pExpr, reg);
141790      pExpr->op = TK_REGISTER;
141791      pExpr->iTable = reg;
141792    }else if( pHint->pIdx!=0 ){
141793      pExpr->iTable = pHint->iIdxCur;
141794      pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
141795      assert( pExpr->iColumn>=0 );
141796    }
141797  }else if( pExpr->op==TK_AGG_FUNCTION ){
141798    /* An aggregate function in the WHERE clause of a query means this must
141799    ** be a correlated sub-query, and expression pExpr is an aggregate from
141800    ** the parent context. Do not walk the function arguments in this case.
141801    **
141802    ** todo: It should be possible to replace this node with a TK_REGISTER
141803    ** expression, as the result of the expression must be stored in a
141804    ** register at this point. The same holds for TK_AGG_COLUMN nodes. */
141805    rc = WRC_Prune;
141806  }
141807  return rc;
141808}
141809
141810/*
141811** Insert an OP_CursorHint instruction if it is appropriate to do so.
141812*/
141813static void codeCursorHint(
141814  struct SrcList_item *pTabItem,  /* FROM clause item */
141815  WhereInfo *pWInfo,    /* The where clause */
141816  WhereLevel *pLevel,   /* Which loop to provide hints for */
141817  WhereTerm *pEndRange  /* Hint this end-of-scan boundary term if not NULL */
141818){
141819  Parse *pParse = pWInfo->pParse;
141820  sqlite3 *db = pParse->db;
141821  Vdbe *v = pParse->pVdbe;
141822  Expr *pExpr = 0;
141823  WhereLoop *pLoop = pLevel->pWLoop;
141824  int iCur;
141825  WhereClause *pWC;
141826  WhereTerm *pTerm;
141827  int i, j;
141828  struct CCurHint sHint;
141829  Walker sWalker;
141830
141831  if( OptimizationDisabled(db, SQLITE_CursorHints) ) return;
141832  iCur = pLevel->iTabCur;
141833  assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
141834  sHint.iTabCur = iCur;
141835  sHint.iIdxCur = pLevel->iIdxCur;
141836  sHint.pIdx = pLoop->u.btree.pIndex;
141837  memset(&sWalker, 0, sizeof(sWalker));
141838  sWalker.pParse = pParse;
141839  sWalker.u.pCCurHint = &sHint;
141840  pWC = &pWInfo->sWC;
141841  for(i=0; i<pWC->nTerm; i++){
141842    pTerm = &pWC->a[i];
141843    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
141844    if( pTerm->prereqAll & pLevel->notReady ) continue;
141845
141846    /* Any terms specified as part of the ON(...) clause for any LEFT
141847    ** JOIN for which the current table is not the rhs are omitted
141848    ** from the cursor-hint.
141849    **
141850    ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms
141851    ** that were specified as part of the WHERE clause must be excluded.
141852    ** This is to address the following:
141853    **
141854    **   SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL;
141855    **
141856    ** Say there is a single row in t2 that matches (t1.a=t2.b), but its
141857    ** t2.c values is not NULL. If the (t2.c IS NULL) constraint is
141858    ** pushed down to the cursor, this row is filtered out, causing
141859    ** SQLite to synthesize a row of NULL values. Which does match the
141860    ** WHERE clause, and so the query returns a row. Which is incorrect.
141861    **
141862    ** For the same reason, WHERE terms such as:
141863    **
141864    **   WHERE 1 = (t2.c IS NULL)
141865    **
141866    ** are also excluded. See codeCursorHintIsOrFunction() for details.
141867    */
141868    if( pTabItem->fg.jointype & JT_LEFT ){
141869      Expr *pExpr = pTerm->pExpr;
141870      if( !ExprHasProperty(pExpr, EP_FromJoin)
141871       || pExpr->iRightJoinTable!=pTabItem->iCursor
141872      ){
141873        sWalker.eCode = 0;
141874        sWalker.xExprCallback = codeCursorHintIsOrFunction;
141875        sqlite3WalkExpr(&sWalker, pTerm->pExpr);
141876        if( sWalker.eCode ) continue;
141877      }
141878    }else{
141879      if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) continue;
141880    }
141881
141882    /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
141883    ** the cursor.  These terms are not needed as hints for a pure range
141884    ** scan (that has no == terms) so omit them. */
141885    if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
141886      for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
141887      if( j<pLoop->nLTerm ) continue;
141888    }
141889
141890    /* No subqueries or non-deterministic functions allowed */
141891    if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
141892
141893    /* For an index scan, make sure referenced columns are actually in
141894    ** the index. */
141895    if( sHint.pIdx!=0 ){
141896      sWalker.eCode = 0;
141897      sWalker.xExprCallback = codeCursorHintCheckExpr;
141898      sqlite3WalkExpr(&sWalker, pTerm->pExpr);
141899      if( sWalker.eCode ) continue;
141900    }
141901
141902    /* If we survive all prior tests, that means this term is worth hinting */
141903    pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
141904  }
141905  if( pExpr!=0 ){
141906    sWalker.xExprCallback = codeCursorHintFixExpr;
141907    sqlite3WalkExpr(&sWalker, pExpr);
141908    sqlite3VdbeAddOp4(v, OP_CursorHint,
141909                      (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0,
141910                      (const char*)pExpr, P4_EXPR);
141911  }
141912}
141913#else
141914# define codeCursorHint(A,B,C,D)  /* No-op */
141915#endif /* SQLITE_ENABLE_CURSOR_HINTS */
141916
141917/*
141918** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
141919** a rowid value just read from cursor iIdxCur, open on index pIdx. This
141920** function generates code to do a deferred seek of cursor iCur to the
141921** rowid stored in register iRowid.
141922**
141923** Normally, this is just:
141924**
141925**   OP_DeferredSeek $iCur $iRowid
141926**
141927** However, if the scan currently being coded is a branch of an OR-loop and
141928** the statement currently being coded is a SELECT, then P3 of OP_DeferredSeek
141929** is set to iIdxCur and P4 is set to point to an array of integers
141930** containing one entry for each column of the table cursor iCur is open
141931** on. For each table column, if the column is the i'th column of the
141932** index, then the corresponding array entry is set to (i+1). If the column
141933** does not appear in the index at all, the array entry is set to 0.
141934*/
141935static void codeDeferredSeek(
141936  WhereInfo *pWInfo,              /* Where clause context */
141937  Index *pIdx,                    /* Index scan is using */
141938  int iCur,                       /* Cursor for IPK b-tree */
141939  int iIdxCur                     /* Index cursor */
141940){
141941  Parse *pParse = pWInfo->pParse; /* Parse context */
141942  Vdbe *v = pParse->pVdbe;        /* Vdbe to generate code within */
141943
141944  assert( iIdxCur>0 );
141945  assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
141946
141947  pWInfo->bDeferredSeek = 1;
141948  sqlite3VdbeAddOp3(v, OP_DeferredSeek, iIdxCur, 0, iCur);
141949  if( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
141950   && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
141951  ){
141952    int i;
141953    Table *pTab = pIdx->pTable;
141954    int *ai = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*(pTab->nCol+1));
141955    if( ai ){
141956      ai[0] = pTab->nCol;
141957      for(i=0; i<pIdx->nColumn-1; i++){
141958        int x1, x2;
141959        assert( pIdx->aiColumn[i]<pTab->nCol );
141960        x1 = pIdx->aiColumn[i];
141961        x2 = sqlite3TableColumnToStorage(pTab, x1);
141962        testcase( x1!=x2 );
141963        if( x1>=0 ) ai[x2+1] = i+1;
141964      }
141965      sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
141966    }
141967  }
141968}
141969
141970/*
141971** If the expression passed as the second argument is a vector, generate
141972** code to write the first nReg elements of the vector into an array
141973** of registers starting with iReg.
141974**
141975** If the expression is not a vector, then nReg must be passed 1. In
141976** this case, generate code to evaluate the expression and leave the
141977** result in register iReg.
141978*/
141979static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){
141980  assert( nReg>0 );
141981  if( p && sqlite3ExprIsVector(p) ){
141982#ifndef SQLITE_OMIT_SUBQUERY
141983    if( (p->flags & EP_xIsSelect) ){
141984      Vdbe *v = pParse->pVdbe;
141985      int iSelect;
141986      assert( p->op==TK_SELECT );
141987      iSelect = sqlite3CodeSubselect(pParse, p);
141988      sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
141989    }else
141990#endif
141991    {
141992      int i;
141993      ExprList *pList = p->x.pList;
141994      assert( nReg<=pList->nExpr );
141995      for(i=0; i<nReg; i++){
141996        sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
141997      }
141998    }
141999  }else{
142000    assert( nReg==1 );
142001    sqlite3ExprCode(pParse, p, iReg);
142002  }
142003}
142004
142005/* An instance of the IdxExprTrans object carries information about a
142006** mapping from an expression on table columns into a column in an index
142007** down through the Walker.
142008*/
142009typedef struct IdxExprTrans {
142010  Expr *pIdxExpr;    /* The index expression */
142011  int iTabCur;       /* The cursor of the corresponding table */
142012  int iIdxCur;       /* The cursor for the index */
142013  int iIdxCol;       /* The column for the index */
142014  int iTabCol;       /* The column for the table */
142015  WhereInfo *pWInfo; /* Complete WHERE clause information */
142016  sqlite3 *db;       /* Database connection (for malloc()) */
142017} IdxExprTrans;
142018
142019/*
142020** Preserve pExpr on the WhereETrans list of the WhereInfo.
142021*/
142022static void preserveExpr(IdxExprTrans *pTrans, Expr *pExpr){
142023  WhereExprMod *pNew;
142024  pNew = sqlite3DbMallocRaw(pTrans->db, sizeof(*pNew));
142025  if( pNew==0 ) return;
142026  pNew->pNext = pTrans->pWInfo->pExprMods;
142027  pTrans->pWInfo->pExprMods = pNew;
142028  pNew->pExpr = pExpr;
142029  memcpy(&pNew->orig, pExpr, sizeof(*pExpr));
142030}
142031
142032/* The walker node callback used to transform matching expressions into
142033** a reference to an index column for an index on an expression.
142034**
142035** If pExpr matches, then transform it into a reference to the index column
142036** that contains the value of pExpr.
142037*/
142038static int whereIndexExprTransNode(Walker *p, Expr *pExpr){
142039  IdxExprTrans *pX = p->u.pIdxTrans;
142040  if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
142041    preserveExpr(pX, pExpr);
142042    pExpr->affExpr = sqlite3ExprAffinity(pExpr);
142043    pExpr->op = TK_COLUMN;
142044    pExpr->iTable = pX->iIdxCur;
142045    pExpr->iColumn = pX->iIdxCol;
142046    pExpr->y.pTab = 0;
142047    testcase( ExprHasProperty(pExpr, EP_Skip) );
142048    testcase( ExprHasProperty(pExpr, EP_Unlikely) );
142049    ExprClearProperty(pExpr, EP_Skip|EP_Unlikely);
142050    return WRC_Prune;
142051  }else{
142052    return WRC_Continue;
142053  }
142054}
142055
142056#ifndef SQLITE_OMIT_GENERATED_COLUMNS
142057/* A walker node callback that translates a column reference to a table
142058** into a corresponding column reference of an index.
142059*/
142060static int whereIndexExprTransColumn(Walker *p, Expr *pExpr){
142061  if( pExpr->op==TK_COLUMN ){
142062    IdxExprTrans *pX = p->u.pIdxTrans;
142063    if( pExpr->iTable==pX->iTabCur && pExpr->iColumn==pX->iTabCol ){
142064      assert( pExpr->y.pTab!=0 );
142065      preserveExpr(pX, pExpr);
142066      pExpr->affExpr = sqlite3TableColumnAffinity(pExpr->y.pTab,pExpr->iColumn);
142067      pExpr->iTable = pX->iIdxCur;
142068      pExpr->iColumn = pX->iIdxCol;
142069      pExpr->y.pTab = 0;
142070    }
142071  }
142072  return WRC_Continue;
142073}
142074#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
142075
142076/*
142077** For an indexes on expression X, locate every instance of expression X
142078** in pExpr and change that subexpression into a reference to the appropriate
142079** column of the index.
142080**
142081** 2019-10-24: Updated to also translate references to a VIRTUAL column in
142082** the table into references to the corresponding (stored) column of the
142083** index.
142084*/
142085static void whereIndexExprTrans(
142086  Index *pIdx,      /* The Index */
142087  int iTabCur,      /* Cursor of the table that is being indexed */
142088  int iIdxCur,      /* Cursor of the index itself */
142089  WhereInfo *pWInfo /* Transform expressions in this WHERE clause */
142090){
142091  int iIdxCol;               /* Column number of the index */
142092  ExprList *aColExpr;        /* Expressions that are indexed */
142093  Table *pTab;
142094  Walker w;
142095  IdxExprTrans x;
142096  aColExpr = pIdx->aColExpr;
142097  if( aColExpr==0 && !pIdx->bHasVCol ){
142098    /* The index does not reference any expressions or virtual columns
142099    ** so no translations are needed. */
142100    return;
142101  }
142102  pTab = pIdx->pTable;
142103  memset(&w, 0, sizeof(w));
142104  w.u.pIdxTrans = &x;
142105  x.iTabCur = iTabCur;
142106  x.iIdxCur = iIdxCur;
142107  x.pWInfo = pWInfo;
142108  x.db = pWInfo->pParse->db;
142109  for(iIdxCol=0; iIdxCol<pIdx->nColumn; iIdxCol++){
142110    i16 iRef = pIdx->aiColumn[iIdxCol];
142111    if( iRef==XN_EXPR ){
142112      assert( aColExpr->a[iIdxCol].pExpr!=0 );
142113      x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
142114      if( sqlite3ExprIsConstant(x.pIdxExpr) ) continue;
142115      w.xExprCallback = whereIndexExprTransNode;
142116#ifndef SQLITE_OMIT_GENERATED_COLUMNS
142117    }else if( iRef>=0
142118       && (pTab->aCol[iRef].colFlags & COLFLAG_VIRTUAL)!=0
142119       && (pTab->aCol[iRef].zColl==0
142120           || sqlite3StrICmp(pTab->aCol[iRef].zColl, sqlite3StrBINARY)==0)
142121    ){
142122      /* Check to see if there are direct references to generated columns
142123      ** that are contained in the index.  Pulling the generated column
142124      ** out of the index is an optimization only - the main table is always
142125      ** available if the index cannot be used.  To avoid unnecessary
142126      ** complication, omit this optimization if the collating sequence for
142127      ** the column is non-standard */
142128      x.iTabCol = iRef;
142129      w.xExprCallback = whereIndexExprTransColumn;
142130#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
142131    }else{
142132      continue;
142133    }
142134    x.iIdxCol = iIdxCol;
142135    sqlite3WalkExpr(&w, pWInfo->pWhere);
142136    sqlite3WalkExprList(&w, pWInfo->pOrderBy);
142137    sqlite3WalkExprList(&w, pWInfo->pResultSet);
142138  }
142139}
142140
142141/*
142142** The pTruth expression is always true because it is the WHERE clause
142143** a partial index that is driving a query loop.  Look through all of the
142144** WHERE clause terms on the query, and if any of those terms must be
142145** true because pTruth is true, then mark those WHERE clause terms as
142146** coded.
142147*/
142148static void whereApplyPartialIndexConstraints(
142149  Expr *pTruth,
142150  int iTabCur,
142151  WhereClause *pWC
142152){
142153  int i;
142154  WhereTerm *pTerm;
142155  while( pTruth->op==TK_AND ){
142156    whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
142157    pTruth = pTruth->pRight;
142158  }
142159  for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
142160    Expr *pExpr;
142161    if( pTerm->wtFlags & TERM_CODED ) continue;
142162    pExpr = pTerm->pExpr;
142163    if( sqlite3ExprCompare(0, pExpr, pTruth, iTabCur)==0 ){
142164      pTerm->wtFlags |= TERM_CODED;
142165    }
142166  }
142167}
142168
142169/*
142170** Generate code for the start of the iLevel-th loop in the WHERE clause
142171** implementation described by pWInfo.
142172*/
142173SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
142174  Parse *pParse,       /* Parsing context */
142175  Vdbe *v,             /* Prepared statement under construction */
142176  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
142177  int iLevel,          /* Which level of pWInfo->a[] should be coded */
142178  WhereLevel *pLevel,  /* The current level pointer */
142179  Bitmask notReady     /* Which tables are currently available */
142180){
142181  int j, k;            /* Loop counters */
142182  int iCur;            /* The VDBE cursor for the table */
142183  int addrNxt;         /* Where to jump to continue with the next IN case */
142184  int bRev;            /* True if we need to scan in reverse order */
142185  WhereLoop *pLoop;    /* The WhereLoop object being coded */
142186  WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
142187  WhereTerm *pTerm;               /* A WHERE clause term */
142188  sqlite3 *db;                    /* Database connection */
142189  struct SrcList_item *pTabItem;  /* FROM clause term being coded */
142190  int addrBrk;                    /* Jump here to break out of the loop */
142191  int addrHalt;                   /* addrBrk for the outermost loop */
142192  int addrCont;                   /* Jump here to continue with next cycle */
142193  int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
142194  int iReleaseReg = 0;      /* Temp register to free before returning */
142195  Index *pIdx = 0;          /* Index used by loop (if any) */
142196  int iLoop;                /* Iteration of constraint generator loop */
142197
142198  pWC = &pWInfo->sWC;
142199  db = pParse->db;
142200  pLoop = pLevel->pWLoop;
142201  pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
142202  iCur = pTabItem->iCursor;
142203  pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
142204  bRev = (pWInfo->revMask>>iLevel)&1;
142205  VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
142206#if WHERETRACE_ENABLED /* 0x20800 */
142207  if( sqlite3WhereTrace & 0x800 ){
142208    sqlite3DebugPrintf("Coding level %d of %d:  notReady=%llx  iFrom=%d\n",
142209       iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
142210    sqlite3WhereLoopPrint(pLoop, pWC);
142211  }
142212  if( sqlite3WhereTrace & 0x20000 ){
142213    if( iLevel==0 ){
142214      sqlite3DebugPrintf("WHERE clause being coded:\n");
142215      sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
142216    }
142217    sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
142218    sqlite3WhereClausePrint(pWC);
142219  }
142220#endif
142221
142222  /* Create labels for the "break" and "continue" instructions
142223  ** for the current loop.  Jump to addrBrk to break out of a loop.
142224  ** Jump to cont to go immediately to the next iteration of the
142225  ** loop.
142226  **
142227  ** When there is an IN operator, we also have a "addrNxt" label that
142228  ** means to continue with the next IN value combination.  When
142229  ** there are no IN operators in the constraints, the "addrNxt" label
142230  ** is the same as "addrBrk".
142231  */
142232  addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
142233  addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
142234
142235  /* If this is the right table of a LEFT OUTER JOIN, allocate and
142236  ** initialize a memory cell that records if this table matches any
142237  ** row of the left table of the join.
142238  */
142239  assert( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
142240       || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
142241  );
142242  if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
142243    pLevel->iLeftJoin = ++pParse->nMem;
142244    sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
142245    VdbeComment((v, "init LEFT JOIN no-match flag"));
142246  }
142247
142248  /* Compute a safe address to jump to if we discover that the table for
142249  ** this loop is empty and can never contribute content. */
142250  for(j=iLevel; j>0 && pWInfo->a[j].iLeftJoin==0; j--){}
142251  addrHalt = pWInfo->a[j].addrBrk;
142252
142253  /* Special case of a FROM clause subquery implemented as a co-routine */
142254  if( pTabItem->fg.viaCoroutine ){
142255    int regYield = pTabItem->regReturn;
142256    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
142257    pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
142258    VdbeCoverage(v);
142259    VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
142260    pLevel->op = OP_Goto;
142261  }else
142262
142263#ifndef SQLITE_OMIT_VIRTUALTABLE
142264  if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
142265    /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
142266    **          to access the data.
142267    */
142268    int iReg;   /* P3 Value for OP_VFilter */
142269    int addrNotFound;
142270    int nConstraint = pLoop->nLTerm;
142271    int iIn;    /* Counter for IN constraints */
142272
142273    iReg = sqlite3GetTempRange(pParse, nConstraint+2);
142274    addrNotFound = pLevel->addrBrk;
142275    for(j=0; j<nConstraint; j++){
142276      int iTarget = iReg+j+2;
142277      pTerm = pLoop->aLTerm[j];
142278      if( NEVER(pTerm==0) ) continue;
142279      if( pTerm->eOperator & WO_IN ){
142280        codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
142281        addrNotFound = pLevel->addrNxt;
142282      }else{
142283        Expr *pRight = pTerm->pExpr->pRight;
142284        codeExprOrVector(pParse, pRight, iTarget, 1);
142285      }
142286    }
142287    sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
142288    sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
142289    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
142290                      pLoop->u.vtab.idxStr,
142291                      pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
142292    VdbeCoverage(v);
142293    pLoop->u.vtab.needFree = 0;
142294    pLevel->p1 = iCur;
142295    pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
142296    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
142297    iIn = pLevel->u.in.nIn;
142298    for(j=nConstraint-1; j>=0; j--){
142299      pTerm = pLoop->aLTerm[j];
142300      if( (pTerm->eOperator & WO_IN)!=0 ) iIn--;
142301      if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
142302        disableTerm(pLevel, pTerm);
142303      }else if( (pTerm->eOperator & WO_IN)!=0
142304        && sqlite3ExprVectorSize(pTerm->pExpr->pLeft)==1
142305      ){
142306        Expr *pCompare;  /* The comparison operator */
142307        Expr *pRight;    /* RHS of the comparison */
142308        VdbeOp *pOp;     /* Opcode to access the value of the IN constraint */
142309
142310        /* Reload the constraint value into reg[iReg+j+2].  The same value
142311        ** was loaded into the same register prior to the OP_VFilter, but
142312        ** the xFilter implementation might have changed the datatype or
142313        ** encoding of the value in the register, so it *must* be reloaded. */
142314        assert( pLevel->u.in.aInLoop!=0 || db->mallocFailed );
142315        if( !db->mallocFailed ){
142316          assert( iIn>=0 && iIn<pLevel->u.in.nIn );
142317          pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
142318          assert( pOp->opcode==OP_Column || pOp->opcode==OP_Rowid );
142319          assert( pOp->opcode!=OP_Column || pOp->p3==iReg+j+2 );
142320          assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 );
142321          testcase( pOp->opcode==OP_Rowid );
142322          sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
142323        }
142324
142325        /* Generate code that will continue to the next row if
142326        ** the IN constraint is not satisfied */
142327        pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0);
142328        assert( pCompare!=0 || db->mallocFailed );
142329        if( pCompare ){
142330          pCompare->pLeft = pTerm->pExpr->pLeft;
142331          pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
142332          if( pRight ){
142333            pRight->iTable = iReg+j+2;
142334            sqlite3ExprIfFalse(
142335                pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL
142336            );
142337          }
142338          pCompare->pLeft = 0;
142339          sqlite3ExprDelete(db, pCompare);
142340        }
142341      }
142342    }
142343    assert( iIn==0 || db->mallocFailed );
142344    /* These registers need to be preserved in case there is an IN operator
142345    ** loop.  So we could deallocate the registers here (and potentially
142346    ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0.  But it seems
142347    ** simpler and safer to simply not reuse the registers.
142348    **
142349    **    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
142350    */
142351  }else
142352#endif /* SQLITE_OMIT_VIRTUALTABLE */
142353
142354  if( (pLoop->wsFlags & WHERE_IPK)!=0
142355   && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
142356  ){
142357    /* Case 2:  We can directly reference a single row using an
142358    **          equality comparison against the ROWID field.  Or
142359    **          we reference multiple rows using a "rowid IN (...)"
142360    **          construct.
142361    */
142362    assert( pLoop->u.btree.nEq==1 );
142363    pTerm = pLoop->aLTerm[0];
142364    assert( pTerm!=0 );
142365    assert( pTerm->pExpr!=0 );
142366    testcase( pTerm->wtFlags & TERM_VIRTUAL );
142367    iReleaseReg = ++pParse->nMem;
142368    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
142369    if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
142370    addrNxt = pLevel->addrNxt;
142371    sqlite3VdbeAddOp3(v, OP_SeekRowid, iCur, addrNxt, iRowidReg);
142372    VdbeCoverage(v);
142373    pLevel->op = OP_Noop;
142374    if( (pTerm->prereqAll & pLevel->notReady)==0 ){
142375      pTerm->wtFlags |= TERM_CODED;
142376    }
142377  }else if( (pLoop->wsFlags & WHERE_IPK)!=0
142378         && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
142379  ){
142380    /* Case 3:  We have an inequality comparison against the ROWID field.
142381    */
142382    int testOp = OP_Noop;
142383    int start;
142384    int memEndValue = 0;
142385    WhereTerm *pStart, *pEnd;
142386
142387    j = 0;
142388    pStart = pEnd = 0;
142389    if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
142390    if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
142391    assert( pStart!=0 || pEnd!=0 );
142392    if( bRev ){
142393      pTerm = pStart;
142394      pStart = pEnd;
142395      pEnd = pTerm;
142396    }
142397    codeCursorHint(pTabItem, pWInfo, pLevel, pEnd);
142398    if( pStart ){
142399      Expr *pX;             /* The expression that defines the start bound */
142400      int r1, rTemp;        /* Registers for holding the start boundary */
142401      int op;               /* Cursor seek operation */
142402
142403      /* The following constant maps TK_xx codes into corresponding
142404      ** seek opcodes.  It depends on a particular ordering of TK_xx
142405      */
142406      const u8 aMoveOp[] = {
142407           /* TK_GT */  OP_SeekGT,
142408           /* TK_LE */  OP_SeekLE,
142409           /* TK_LT */  OP_SeekLT,
142410           /* TK_GE */  OP_SeekGE
142411      };
142412      assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
142413      assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
142414      assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
142415
142416      assert( (pStart->wtFlags & TERM_VNULL)==0 );
142417      testcase( pStart->wtFlags & TERM_VIRTUAL );
142418      pX = pStart->pExpr;
142419      assert( pX!=0 );
142420      testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
142421      if( sqlite3ExprIsVector(pX->pRight) ){
142422        r1 = rTemp = sqlite3GetTempReg(pParse);
142423        codeExprOrVector(pParse, pX->pRight, r1, 1);
142424        testcase( pX->op==TK_GT );
142425        testcase( pX->op==TK_GE );
142426        testcase( pX->op==TK_LT );
142427        testcase( pX->op==TK_LE );
142428        op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
142429        assert( pX->op!=TK_GT || op==OP_SeekGE );
142430        assert( pX->op!=TK_GE || op==OP_SeekGE );
142431        assert( pX->op!=TK_LT || op==OP_SeekLE );
142432        assert( pX->op!=TK_LE || op==OP_SeekLE );
142433      }else{
142434        r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
142435        disableTerm(pLevel, pStart);
142436        op = aMoveOp[(pX->op - TK_GT)];
142437      }
142438      sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1);
142439      VdbeComment((v, "pk"));
142440      VdbeCoverageIf(v, pX->op==TK_GT);
142441      VdbeCoverageIf(v, pX->op==TK_LE);
142442      VdbeCoverageIf(v, pX->op==TK_LT);
142443      VdbeCoverageIf(v, pX->op==TK_GE);
142444      sqlite3ReleaseTempReg(pParse, rTemp);
142445    }else{
142446      sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrHalt);
142447      VdbeCoverageIf(v, bRev==0);
142448      VdbeCoverageIf(v, bRev!=0);
142449    }
142450    if( pEnd ){
142451      Expr *pX;
142452      pX = pEnd->pExpr;
142453      assert( pX!=0 );
142454      assert( (pEnd->wtFlags & TERM_VNULL)==0 );
142455      testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
142456      testcase( pEnd->wtFlags & TERM_VIRTUAL );
142457      memEndValue = ++pParse->nMem;
142458      codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
142459      if( 0==sqlite3ExprIsVector(pX->pRight)
142460       && (pX->op==TK_LT || pX->op==TK_GT)
142461      ){
142462        testOp = bRev ? OP_Le : OP_Ge;
142463      }else{
142464        testOp = bRev ? OP_Lt : OP_Gt;
142465      }
142466      if( 0==sqlite3ExprIsVector(pX->pRight) ){
142467        disableTerm(pLevel, pEnd);
142468      }
142469    }
142470    start = sqlite3VdbeCurrentAddr(v);
142471    pLevel->op = bRev ? OP_Prev : OP_Next;
142472    pLevel->p1 = iCur;
142473    pLevel->p2 = start;
142474    assert( pLevel->p5==0 );
142475    if( testOp!=OP_Noop ){
142476      iRowidReg = ++pParse->nMem;
142477      sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
142478      sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
142479      VdbeCoverageIf(v, testOp==OP_Le);
142480      VdbeCoverageIf(v, testOp==OP_Lt);
142481      VdbeCoverageIf(v, testOp==OP_Ge);
142482      VdbeCoverageIf(v, testOp==OP_Gt);
142483      sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
142484    }
142485  }else if( pLoop->wsFlags & WHERE_INDEXED ){
142486    /* Case 4: A scan using an index.
142487    **
142488    **         The WHERE clause may contain zero or more equality
142489    **         terms ("==" or "IN" operators) that refer to the N
142490    **         left-most columns of the index. It may also contain
142491    **         inequality constraints (>, <, >= or <=) on the indexed
142492    **         column that immediately follows the N equalities. Only
142493    **         the right-most column can be an inequality - the rest must
142494    **         use the "==" and "IN" operators. For example, if the
142495    **         index is on (x,y,z), then the following clauses are all
142496    **         optimized:
142497    **
142498    **            x=5
142499    **            x=5 AND y=10
142500    **            x=5 AND y<10
142501    **            x=5 AND y>5 AND y<10
142502    **            x=5 AND y=5 AND z<=10
142503    **
142504    **         The z<10 term of the following cannot be used, only
142505    **         the x=5 term:
142506    **
142507    **            x=5 AND z<10
142508    **
142509    **         N may be zero if there are inequality constraints.
142510    **         If there are no inequality constraints, then N is at
142511    **         least one.
142512    **
142513    **         This case is also used when there are no WHERE clause
142514    **         constraints but an index is selected anyway, in order
142515    **         to force the output order to conform to an ORDER BY.
142516    */
142517    static const u8 aStartOp[] = {
142518      0,
142519      0,
142520      OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
142521      OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
142522      OP_SeekGT,           /* 4: (start_constraints  && !startEq && !bRev) */
142523      OP_SeekLT,           /* 5: (start_constraints  && !startEq &&  bRev) */
142524      OP_SeekGE,           /* 6: (start_constraints  &&  startEq && !bRev) */
142525      OP_SeekLE            /* 7: (start_constraints  &&  startEq &&  bRev) */
142526    };
142527    static const u8 aEndOp[] = {
142528      OP_IdxGE,            /* 0: (end_constraints && !bRev && !endEq) */
142529      OP_IdxGT,            /* 1: (end_constraints && !bRev &&  endEq) */
142530      OP_IdxLE,            /* 2: (end_constraints &&  bRev && !endEq) */
142531      OP_IdxLT,            /* 3: (end_constraints &&  bRev &&  endEq) */
142532    };
142533    u16 nEq = pLoop->u.btree.nEq;     /* Number of == or IN terms */
142534    u16 nBtm = pLoop->u.btree.nBtm;   /* Length of BTM vector */
142535    u16 nTop = pLoop->u.btree.nTop;   /* Length of TOP vector */
142536    int regBase;                 /* Base register holding constraint values */
142537    WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
142538    WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
142539    int startEq;                 /* True if range start uses ==, >= or <= */
142540    int endEq;                   /* True if range end uses ==, >= or <= */
142541    int start_constraints;       /* Start of range is constrained */
142542    int nConstraint;             /* Number of constraint terms */
142543    int iIdxCur;                 /* The VDBE cursor for the index */
142544    int nExtraReg = 0;           /* Number of extra registers needed */
142545    int op;                      /* Instruction opcode */
142546    char *zStartAff;             /* Affinity for start of range constraint */
142547    char *zEndAff = 0;           /* Affinity for end of range constraint */
142548    u8 bSeekPastNull = 0;        /* True to seek past initial nulls */
142549    u8 bStopAtNull = 0;          /* Add condition to terminate at NULLs */
142550    int omitTable;               /* True if we use the index only */
142551    int regBignull = 0;          /* big-null flag register */
142552
142553    pIdx = pLoop->u.btree.pIndex;
142554    iIdxCur = pLevel->iIdxCur;
142555    assert( nEq>=pLoop->nSkip );
142556
142557    /* Find any inequality constraint terms for the start and end
142558    ** of the range.
142559    */
142560    j = nEq;
142561    if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
142562      pRangeStart = pLoop->aLTerm[j++];
142563      nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
142564      /* Like optimization range constraints always occur in pairs */
142565      assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
142566              (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
142567    }
142568    if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
142569      pRangeEnd = pLoop->aLTerm[j++];
142570      nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
142571#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
142572      if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
142573        assert( pRangeStart!=0 );                     /* LIKE opt constraints */
142574        assert( pRangeStart->wtFlags & TERM_LIKEOPT );   /* occur in pairs */
142575        pLevel->iLikeRepCntr = (u32)++pParse->nMem;
142576        sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
142577        VdbeComment((v, "LIKE loop counter"));
142578        pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
142579        /* iLikeRepCntr actually stores 2x the counter register number.  The
142580        ** bottom bit indicates whether the search order is ASC or DESC. */
142581        testcase( bRev );
142582        testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
142583        assert( (bRev & ~1)==0 );
142584        pLevel->iLikeRepCntr <<=1;
142585        pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
142586      }
142587#endif
142588      if( pRangeStart==0 ){
142589        j = pIdx->aiColumn[nEq];
142590        if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
142591          bSeekPastNull = 1;
142592        }
142593      }
142594    }
142595    assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
142596
142597    /* If the WHERE_BIGNULL_SORT flag is set, then index column nEq uses
142598    ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
142599    ** FIRST). In both cases separate ordered scans are made of those
142600    ** index entries for which the column is null and for those for which
142601    ** it is not. For an ASC sort, the non-NULL entries are scanned first.
142602    ** For DESC, NULL entries are scanned first.
142603    */
142604    if( (pLoop->wsFlags & (WHERE_TOP_LIMIT|WHERE_BTM_LIMIT))==0
142605     && (pLoop->wsFlags & WHERE_BIGNULL_SORT)!=0
142606    ){
142607      assert( bSeekPastNull==0 && nExtraReg==0 && nBtm==0 && nTop==0 );
142608      assert( pRangeEnd==0 && pRangeStart==0 );
142609      testcase( pLoop->nSkip>0 );
142610      nExtraReg = 1;
142611      bSeekPastNull = 1;
142612      pLevel->regBignull = regBignull = ++pParse->nMem;
142613      if( pLevel->iLeftJoin ){
142614        sqlite3VdbeAddOp2(v, OP_Integer, 0, regBignull);
142615      }
142616      pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
142617    }
142618
142619    /* If we are doing a reverse order scan on an ascending index, or
142620    ** a forward order scan on a descending index, interchange the
142621    ** start and end terms (pRangeStart and pRangeEnd).
142622    */
142623    if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
142624     || (bRev && pIdx->nKeyCol==nEq)
142625    ){
142626      SWAP(WhereTerm *, pRangeEnd, pRangeStart);
142627      SWAP(u8, bSeekPastNull, bStopAtNull);
142628      SWAP(u8, nBtm, nTop);
142629    }
142630
142631    /* Generate code to evaluate all constraint terms using == or IN
142632    ** and store the values of those terms in an array of registers
142633    ** starting at regBase.
142634    */
142635    codeCursorHint(pTabItem, pWInfo, pLevel, pRangeEnd);
142636    regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
142637    assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
142638    if( zStartAff && nTop ){
142639      zEndAff = sqlite3DbStrDup(db, &zStartAff[nEq]);
142640    }
142641    addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
142642
142643    testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
142644    testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
142645    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
142646    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
142647    startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
142648    endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
142649    start_constraints = pRangeStart || nEq>0;
142650
142651    /* Seek the index cursor to the start of the range. */
142652    nConstraint = nEq;
142653    if( pRangeStart ){
142654      Expr *pRight = pRangeStart->pExpr->pRight;
142655      codeExprOrVector(pParse, pRight, regBase+nEq, nBtm);
142656      whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
142657      if( (pRangeStart->wtFlags & TERM_VNULL)==0
142658       && sqlite3ExprCanBeNull(pRight)
142659      ){
142660        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
142661        VdbeCoverage(v);
142662      }
142663      if( zStartAff ){
142664        updateRangeAffinityStr(pRight, nBtm, &zStartAff[nEq]);
142665      }
142666      nConstraint += nBtm;
142667      testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
142668      if( sqlite3ExprIsVector(pRight)==0 ){
142669        disableTerm(pLevel, pRangeStart);
142670      }else{
142671        startEq = 1;
142672      }
142673      bSeekPastNull = 0;
142674    }else if( bSeekPastNull ){
142675      startEq = 0;
142676      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
142677      start_constraints = 1;
142678      nConstraint++;
142679    }else if( regBignull ){
142680      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
142681      start_constraints = 1;
142682      nConstraint++;
142683    }
142684    codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
142685    if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
142686      /* The skip-scan logic inside the call to codeAllEqualityConstraints()
142687      ** above has already left the cursor sitting on the correct row,
142688      ** so no further seeking is needed */
142689    }else{
142690      if( pLoop->wsFlags & WHERE_IN_EARLYOUT ){
142691        sqlite3VdbeAddOp1(v, OP_SeekHit, iIdxCur);
142692      }
142693      if( regBignull ){
142694        sqlite3VdbeAddOp2(v, OP_Integer, 1, regBignull);
142695        VdbeComment((v, "NULL-scan pass ctr"));
142696      }
142697
142698      op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
142699      assert( op!=0 );
142700      sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
142701      VdbeCoverage(v);
142702      VdbeCoverageIf(v, op==OP_Rewind);  testcase( op==OP_Rewind );
142703      VdbeCoverageIf(v, op==OP_Last);    testcase( op==OP_Last );
142704      VdbeCoverageIf(v, op==OP_SeekGT);  testcase( op==OP_SeekGT );
142705      VdbeCoverageIf(v, op==OP_SeekGE);  testcase( op==OP_SeekGE );
142706      VdbeCoverageIf(v, op==OP_SeekLE);  testcase( op==OP_SeekLE );
142707      VdbeCoverageIf(v, op==OP_SeekLT);  testcase( op==OP_SeekLT );
142708
142709      assert( bSeekPastNull==0 || bStopAtNull==0 );
142710      if( regBignull ){
142711        assert( bSeekPastNull==1 || bStopAtNull==1 );
142712        assert( bSeekPastNull==!bStopAtNull );
142713        assert( bStopAtNull==startEq );
142714        sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+2);
142715        op = aStartOp[(nConstraint>1)*4 + 2 + bRev];
142716        sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase,
142717                             nConstraint-startEq);
142718        VdbeCoverage(v);
142719        VdbeCoverageIf(v, op==OP_Rewind);  testcase( op==OP_Rewind );
142720        VdbeCoverageIf(v, op==OP_Last);    testcase( op==OP_Last );
142721        VdbeCoverageIf(v, op==OP_SeekGE);  testcase( op==OP_SeekGE );
142722        VdbeCoverageIf(v, op==OP_SeekLE);  testcase( op==OP_SeekLE );
142723        assert( op==OP_Rewind || op==OP_Last || op==OP_SeekGE || op==OP_SeekLE);
142724      }
142725    }
142726
142727    /* Load the value for the inequality constraint at the end of the
142728    ** range (if any).
142729    */
142730    nConstraint = nEq;
142731    if( pRangeEnd ){
142732      Expr *pRight = pRangeEnd->pExpr->pRight;
142733      codeExprOrVector(pParse, pRight, regBase+nEq, nTop);
142734      whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
142735      if( (pRangeEnd->wtFlags & TERM_VNULL)==0
142736       && sqlite3ExprCanBeNull(pRight)
142737      ){
142738        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
142739        VdbeCoverage(v);
142740      }
142741      if( zEndAff ){
142742        updateRangeAffinityStr(pRight, nTop, zEndAff);
142743        codeApplyAffinity(pParse, regBase+nEq, nTop, zEndAff);
142744      }else{
142745        assert( pParse->db->mallocFailed );
142746      }
142747      nConstraint += nTop;
142748      testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
142749
142750      if( sqlite3ExprIsVector(pRight)==0 ){
142751        disableTerm(pLevel, pRangeEnd);
142752      }else{
142753        endEq = 1;
142754      }
142755    }else if( bStopAtNull ){
142756      if( regBignull==0 ){
142757        sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
142758        endEq = 0;
142759      }
142760      nConstraint++;
142761    }
142762    sqlite3DbFree(db, zStartAff);
142763    sqlite3DbFree(db, zEndAff);
142764
142765    /* Top of the loop body */
142766    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
142767
142768    /* Check if the index cursor is past the end of the range. */
142769    if( nConstraint ){
142770      if( regBignull ){
142771        /* Except, skip the end-of-range check while doing the NULL-scan */
142772        sqlite3VdbeAddOp2(v, OP_IfNot, regBignull, sqlite3VdbeCurrentAddr(v)+3);
142773        VdbeComment((v, "If NULL-scan 2nd pass"));
142774        VdbeCoverage(v);
142775      }
142776      op = aEndOp[bRev*2 + endEq];
142777      sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
142778      testcase( op==OP_IdxGT );  VdbeCoverageIf(v, op==OP_IdxGT );
142779      testcase( op==OP_IdxGE );  VdbeCoverageIf(v, op==OP_IdxGE );
142780      testcase( op==OP_IdxLT );  VdbeCoverageIf(v, op==OP_IdxLT );
142781      testcase( op==OP_IdxLE );  VdbeCoverageIf(v, op==OP_IdxLE );
142782    }
142783    if( regBignull ){
142784      /* During a NULL-scan, check to see if we have reached the end of
142785      ** the NULLs */
142786      assert( bSeekPastNull==!bStopAtNull );
142787      assert( bSeekPastNull+bStopAtNull==1 );
142788      assert( nConstraint+bSeekPastNull>0 );
142789      sqlite3VdbeAddOp2(v, OP_If, regBignull, sqlite3VdbeCurrentAddr(v)+2);
142790      VdbeComment((v, "If NULL-scan 1st pass"));
142791      VdbeCoverage(v);
142792      op = aEndOp[bRev*2 + bSeekPastNull];
142793      sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase,
142794                           nConstraint+bSeekPastNull);
142795      testcase( op==OP_IdxGT );  VdbeCoverageIf(v, op==OP_IdxGT );
142796      testcase( op==OP_IdxGE );  VdbeCoverageIf(v, op==OP_IdxGE );
142797      testcase( op==OP_IdxLT );  VdbeCoverageIf(v, op==OP_IdxLT );
142798      testcase( op==OP_IdxLE );  VdbeCoverageIf(v, op==OP_IdxLE );
142799    }
142800
142801    if( pLoop->wsFlags & WHERE_IN_EARLYOUT ){
142802      sqlite3VdbeAddOp2(v, OP_SeekHit, iIdxCur, 1);
142803    }
142804
142805    /* Seek the table cursor, if required */
142806    omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
142807           && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0;
142808    if( omitTable ){
142809      /* pIdx is a covering index.  No need to access the main table. */
142810    }else if( HasRowid(pIdx->pTable) ){
142811      if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE)
142812       || ( (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE)!=0
142813           && (pWInfo->eOnePass==ONEPASS_SINGLE || pLoop->nLTerm==0) )
142814      ){
142815        iRowidReg = ++pParse->nMem;
142816        sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
142817        sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
142818        VdbeCoverage(v);
142819      }else{
142820        codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
142821      }
142822    }else if( iCur!=iIdxCur ){
142823      Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
142824      iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
142825      for(j=0; j<pPk->nKeyCol; j++){
142826        k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
142827        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
142828      }
142829      sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
142830                           iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
142831    }
142832
142833    if( pLevel->iLeftJoin==0 ){
142834      /* If pIdx is an index on one or more expressions, then look through
142835      ** all the expressions in pWInfo and try to transform matching expressions
142836      ** into reference to index columns.  Also attempt to translate references
142837      ** to virtual columns in the table into references to (stored) columns
142838      ** of the index.
142839      **
142840      ** Do not do this for the RHS of a LEFT JOIN. This is because the
142841      ** expression may be evaluated after OP_NullRow has been executed on
142842      ** the cursor. In this case it is important to do the full evaluation,
142843      ** as the result of the expression may not be NULL, even if all table
142844      ** column values are.  https://www.sqlite.org/src/info/7fa8049685b50b5a
142845      **
142846      ** Also, do not do this when processing one index an a multi-index
142847      ** OR clause, since the transformation will become invalid once we
142848      ** move forward to the next index.
142849      ** https://sqlite.org/src/info/4e8e4857d32d401f
142850      */
142851      if( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){
142852        whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo);
142853      }
142854
142855      /* If a partial index is driving the loop, try to eliminate WHERE clause
142856      ** terms from the query that must be true due to the WHERE clause of
142857      ** the partial index.
142858      **
142859      ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
142860      ** for a LEFT JOIN.
142861      */
142862      if( pIdx->pPartIdxWhere ){
142863        whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
142864      }
142865    }else{
142866      testcase( pIdx->pPartIdxWhere );
142867      /* The following assert() is not a requirement, merely an observation:
142868      ** The OR-optimization doesn't work for the right hand table of
142869      ** a LEFT JOIN: */
142870      assert( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 );
142871    }
142872
142873    /* Record the instruction used to terminate the loop. */
142874    if( pLoop->wsFlags & WHERE_ONEROW ){
142875      pLevel->op = OP_Noop;
142876    }else if( bRev ){
142877      pLevel->op = OP_Prev;
142878    }else{
142879      pLevel->op = OP_Next;
142880    }
142881    pLevel->p1 = iIdxCur;
142882    pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
142883    if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
142884      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
142885    }else{
142886      assert( pLevel->p5==0 );
142887    }
142888    if( omitTable ) pIdx = 0;
142889  }else
142890
142891#ifndef SQLITE_OMIT_OR_OPTIMIZATION
142892  if( pLoop->wsFlags & WHERE_MULTI_OR ){
142893    /* Case 5:  Two or more separately indexed terms connected by OR
142894    **
142895    ** Example:
142896    **
142897    **   CREATE TABLE t1(a,b,c,d);
142898    **   CREATE INDEX i1 ON t1(a);
142899    **   CREATE INDEX i2 ON t1(b);
142900    **   CREATE INDEX i3 ON t1(c);
142901    **
142902    **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
142903    **
142904    ** In the example, there are three indexed terms connected by OR.
142905    ** The top of the loop looks like this:
142906    **
142907    **          Null       1                # Zero the rowset in reg 1
142908    **
142909    ** Then, for each indexed term, the following. The arguments to
142910    ** RowSetTest are such that the rowid of the current row is inserted
142911    ** into the RowSet. If it is already present, control skips the
142912    ** Gosub opcode and jumps straight to the code generated by WhereEnd().
142913    **
142914    **        sqlite3WhereBegin(<term>)
142915    **          RowSetTest                  # Insert rowid into rowset
142916    **          Gosub      2 A
142917    **        sqlite3WhereEnd()
142918    **
142919    ** Following the above, code to terminate the loop. Label A, the target
142920    ** of the Gosub above, jumps to the instruction right after the Goto.
142921    **
142922    **          Null       1                # Zero the rowset in reg 1
142923    **          Goto       B                # The loop is finished.
142924    **
142925    **       A: <loop body>                 # Return data, whatever.
142926    **
142927    **          Return     2                # Jump back to the Gosub
142928    **
142929    **       B: <after the loop>
142930    **
142931    ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
142932    ** use an ephemeral index instead of a RowSet to record the primary
142933    ** keys of the rows we have already seen.
142934    **
142935    */
142936    WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
142937    SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
142938    Index *pCov = 0;             /* Potential covering index (or NULL) */
142939    int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
142940
142941    int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
142942    int regRowset = 0;                        /* Register for RowSet object */
142943    int regRowid = 0;                         /* Register holding rowid */
142944    int iLoopBody = sqlite3VdbeMakeLabel(pParse);/* Start of loop body */
142945    int iRetInit;                             /* Address of regReturn init */
142946    int untestedTerms = 0;             /* Some terms not completely tested */
142947    int ii;                            /* Loop counter */
142948    u16 wctrlFlags;                    /* Flags for sub-WHERE clause */
142949    Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
142950    Table *pTab = pTabItem->pTab;
142951
142952    pTerm = pLoop->aLTerm[0];
142953    assert( pTerm!=0 );
142954    assert( pTerm->eOperator & WO_OR );
142955    assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
142956    pOrWc = &pTerm->u.pOrInfo->wc;
142957    pLevel->op = OP_Return;
142958    pLevel->p1 = regReturn;
142959
142960    /* Set up a new SrcList in pOrTab containing the table being scanned
142961    ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
142962    ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
142963    */
142964    if( pWInfo->nLevel>1 ){
142965      int nNotReady;                 /* The number of notReady tables */
142966      struct SrcList_item *origSrc;     /* Original list of tables */
142967      nNotReady = pWInfo->nLevel - iLevel - 1;
142968      pOrTab = sqlite3StackAllocRaw(db,
142969                            sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
142970      if( pOrTab==0 ) return notReady;
142971      pOrTab->nAlloc = (u8)(nNotReady + 1);
142972      pOrTab->nSrc = pOrTab->nAlloc;
142973      memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
142974      origSrc = pWInfo->pTabList->a;
142975      for(k=1; k<=nNotReady; k++){
142976        memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
142977      }
142978    }else{
142979      pOrTab = pWInfo->pTabList;
142980    }
142981
142982    /* Initialize the rowset register to contain NULL. An SQL NULL is
142983    ** equivalent to an empty rowset.  Or, create an ephemeral index
142984    ** capable of holding primary keys in the case of a WITHOUT ROWID.
142985    **
142986    ** Also initialize regReturn to contain the address of the instruction
142987    ** immediately following the OP_Return at the bottom of the loop. This
142988    ** is required in a few obscure LEFT JOIN cases where control jumps
142989    ** over the top of the loop into the body of it. In this case the
142990    ** correct response for the end-of-loop code (the OP_Return) is to
142991    ** fall through to the next instruction, just as an OP_Next does if
142992    ** called on an uninitialized cursor.
142993    */
142994    if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
142995      if( HasRowid(pTab) ){
142996        regRowset = ++pParse->nMem;
142997        sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
142998      }else{
142999        Index *pPk = sqlite3PrimaryKeyIndex(pTab);
143000        regRowset = pParse->nTab++;
143001        sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
143002        sqlite3VdbeSetP4KeyInfo(pParse, pPk);
143003      }
143004      regRowid = ++pParse->nMem;
143005    }
143006    iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
143007
143008    /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
143009    ** Then for every term xN, evaluate as the subexpression: xN AND z
143010    ** That way, terms in y that are factored into the disjunction will
143011    ** be picked up by the recursive calls to sqlite3WhereBegin() below.
143012    **
143013    ** Actually, each subexpression is converted to "xN AND w" where w is
143014    ** the "interesting" terms of z - terms that did not originate in the
143015    ** ON or USING clause of a LEFT JOIN, and terms that are usable as
143016    ** indices.
143017    **
143018    ** This optimization also only applies if the (x1 OR x2 OR ...) term
143019    ** is not contained in the ON clause of a LEFT JOIN.
143020    ** See ticket http://www.sqlite.org/src/info/f2369304e4
143021    */
143022    if( pWC->nTerm>1 ){
143023      int iTerm;
143024      for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
143025        Expr *pExpr = pWC->a[iTerm].pExpr;
143026        if( &pWC->a[iTerm] == pTerm ) continue;
143027        testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
143028        testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
143029        if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
143030        if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
143031        testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
143032        pExpr = sqlite3ExprDup(db, pExpr, 0);
143033        pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr);
143034      }
143035      if( pAndExpr ){
143036        /* The extra 0x10000 bit on the opcode is masked off and does not
143037        ** become part of the new Expr.op.  However, it does make the
143038        ** op==TK_AND comparison inside of sqlite3PExpr() false, and this
143039        ** prevents sqlite3PExpr() from implementing AND short-circuit
143040        ** optimization, which we do not want here. */
143041        pAndExpr = sqlite3PExpr(pParse, TK_AND|0x10000, 0, pAndExpr);
143042      }
143043    }
143044
143045    /* Run a separate WHERE clause for each term of the OR clause.  After
143046    ** eliminating duplicates from other WHERE clauses, the action for each
143047    ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
143048    */
143049    wctrlFlags =  WHERE_OR_SUBCLAUSE | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE);
143050    ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
143051    for(ii=0; ii<pOrWc->nTerm; ii++){
143052      WhereTerm *pOrTerm = &pOrWc->a[ii];
143053      if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
143054        WhereInfo *pSubWInfo;           /* Info for single OR-term scan */
143055        Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
143056        int jmp1 = 0;                   /* Address of jump operation */
143057        testcase( (pTabItem[0].fg.jointype & JT_LEFT)!=0
143058               && !ExprHasProperty(pOrExpr, EP_FromJoin)
143059        ); /* See TH3 vtab25.400 and ticket 614b25314c766238 */
143060        if( pAndExpr ){
143061          pAndExpr->pLeft = pOrExpr;
143062          pOrExpr = pAndExpr;
143063        }
143064        /* Loop through table entries that match term pOrTerm. */
143065        ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1));
143066        WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
143067        pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
143068                                      wctrlFlags, iCovCur);
143069        assert( pSubWInfo || pParse->nErr || db->mallocFailed );
143070        if( pSubWInfo ){
143071          WhereLoop *pSubLoop;
143072          int addrExplain = sqlite3WhereExplainOneScan(
143073              pParse, pOrTab, &pSubWInfo->a[0], 0
143074          );
143075          sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
143076
143077          /* This is the sub-WHERE clause body.  First skip over
143078          ** duplicate rows from prior sub-WHERE clauses, and record the
143079          ** rowid (or PRIMARY KEY) for the current row so that the same
143080          ** row will be skipped in subsequent sub-WHERE clauses.
143081          */
143082          if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
143083            int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
143084            if( HasRowid(pTab) ){
143085              sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
143086              jmp1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0,
143087                                          regRowid, iSet);
143088              VdbeCoverage(v);
143089            }else{
143090              Index *pPk = sqlite3PrimaryKeyIndex(pTab);
143091              int nPk = pPk->nKeyCol;
143092              int iPk;
143093              int r;
143094
143095              /* Read the PK into an array of temp registers. */
143096              r = sqlite3GetTempRange(pParse, nPk);
143097              for(iPk=0; iPk<nPk; iPk++){
143098                int iCol = pPk->aiColumn[iPk];
143099                sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+iPk);
143100              }
143101
143102              /* Check if the temp table already contains this key. If so,
143103              ** the row has already been included in the result set and
143104              ** can be ignored (by jumping past the Gosub below). Otherwise,
143105              ** insert the key into the temp table and proceed with processing
143106              ** the row.
143107              **
143108              ** Use some of the same optimizations as OP_RowSetTest: If iSet
143109              ** is zero, assume that the key cannot already be present in
143110              ** the temp table. And if iSet is -1, assume that there is no
143111              ** need to insert the key into the temp table, as it will never
143112              ** be tested for.  */
143113              if( iSet ){
143114                jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
143115                VdbeCoverage(v);
143116              }
143117              if( iSet>=0 ){
143118                sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
143119                sqlite3VdbeAddOp4Int(v, OP_IdxInsert, regRowset, regRowid,
143120                                     r, nPk);
143121                if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
143122              }
143123
143124              /* Release the array of temp registers */
143125              sqlite3ReleaseTempRange(pParse, r, nPk);
143126            }
143127          }
143128
143129          /* Invoke the main loop body as a subroutine */
143130          sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
143131
143132          /* Jump here (skipping the main loop body subroutine) if the
143133          ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
143134          if( jmp1 ) sqlite3VdbeJumpHere(v, jmp1);
143135
143136          /* The pSubWInfo->untestedTerms flag means that this OR term
143137          ** contained one or more AND term from a notReady table.  The
143138          ** terms from the notReady table could not be tested and will
143139          ** need to be tested later.
143140          */
143141          if( pSubWInfo->untestedTerms ) untestedTerms = 1;
143142
143143          /* If all of the OR-connected terms are optimized using the same
143144          ** index, and the index is opened using the same cursor number
143145          ** by each call to sqlite3WhereBegin() made by this loop, it may
143146          ** be possible to use that index as a covering index.
143147          **
143148          ** If the call to sqlite3WhereBegin() above resulted in a scan that
143149          ** uses an index, and this is either the first OR-connected term
143150          ** processed or the index is the same as that used by all previous
143151          ** terms, set pCov to the candidate covering index. Otherwise, set
143152          ** pCov to NULL to indicate that no candidate covering index will
143153          ** be available.
143154          */
143155          pSubLoop = pSubWInfo->a[0].pWLoop;
143156          assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
143157          if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
143158           && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
143159           && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
143160          ){
143161            assert( pSubWInfo->a[0].iIdxCur==iCovCur );
143162            pCov = pSubLoop->u.btree.pIndex;
143163          }else{
143164            pCov = 0;
143165          }
143166
143167          /* Finish the loop through table entries that match term pOrTerm. */
143168          sqlite3WhereEnd(pSubWInfo);
143169          ExplainQueryPlanPop(pParse);
143170        }
143171      }
143172    }
143173    ExplainQueryPlanPop(pParse);
143174    pLevel->u.pCovidx = pCov;
143175    if( pCov ) pLevel->iIdxCur = iCovCur;
143176    if( pAndExpr ){
143177      pAndExpr->pLeft = 0;
143178      sqlite3ExprDelete(db, pAndExpr);
143179    }
143180    sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
143181    sqlite3VdbeGoto(v, pLevel->addrBrk);
143182    sqlite3VdbeResolveLabel(v, iLoopBody);
143183
143184    if( pWInfo->nLevel>1 ){ sqlite3StackFree(db, pOrTab); }
143185    if( !untestedTerms ) disableTerm(pLevel, pTerm);
143186  }else
143187#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
143188
143189  {
143190    /* Case 6:  There is no usable index.  We must do a complete
143191    **          scan of the entire table.
143192    */
143193    static const u8 aStep[] = { OP_Next, OP_Prev };
143194    static const u8 aStart[] = { OP_Rewind, OP_Last };
143195    assert( bRev==0 || bRev==1 );
143196    if( pTabItem->fg.isRecursive ){
143197      /* Tables marked isRecursive have only a single row that is stored in
143198      ** a pseudo-cursor.  No need to Rewind or Next such cursors. */
143199      pLevel->op = OP_Noop;
143200    }else{
143201      codeCursorHint(pTabItem, pWInfo, pLevel, 0);
143202      pLevel->op = aStep[bRev];
143203      pLevel->p1 = iCur;
143204      pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
143205      VdbeCoverageIf(v, bRev==0);
143206      VdbeCoverageIf(v, bRev!=0);
143207      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
143208    }
143209  }
143210
143211#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
143212  pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
143213#endif
143214
143215  /* Insert code to test every subexpression that can be completely
143216  ** computed using the current set of tables.
143217  **
143218  ** This loop may run between one and three times, depending on the
143219  ** constraints to be generated. The value of stack variable iLoop
143220  ** determines the constraints coded by each iteration, as follows:
143221  **
143222  ** iLoop==1: Code only expressions that are entirely covered by pIdx.
143223  ** iLoop==2: Code remaining expressions that do not contain correlated
143224  **           sub-queries.
143225  ** iLoop==3: Code all remaining expressions.
143226  **
143227  ** An effort is made to skip unnecessary iterations of the loop.
143228  */
143229  iLoop = (pIdx ? 1 : 2);
143230  do{
143231    int iNext = 0;                /* Next value for iLoop */
143232    for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
143233      Expr *pE;
143234      int skipLikeAddr = 0;
143235      testcase( pTerm->wtFlags & TERM_VIRTUAL );
143236      testcase( pTerm->wtFlags & TERM_CODED );
143237      if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
143238      if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
143239        testcase( pWInfo->untestedTerms==0
143240            && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
143241        pWInfo->untestedTerms = 1;
143242        continue;
143243      }
143244      pE = pTerm->pExpr;
143245      assert( pE!=0 );
143246      if( (pTabItem->fg.jointype&JT_LEFT) && !ExprHasProperty(pE,EP_FromJoin) ){
143247        continue;
143248      }
143249
143250      if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
143251        iNext = 2;
143252        continue;
143253      }
143254      if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
143255        if( iNext==0 ) iNext = 3;
143256        continue;
143257      }
143258
143259      if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
143260        /* If the TERM_LIKECOND flag is set, that means that the range search
143261        ** is sufficient to guarantee that the LIKE operator is true, so we
143262        ** can skip the call to the like(A,B) function.  But this only works
143263        ** for strings.  So do not skip the call to the function on the pass
143264        ** that compares BLOBs. */
143265#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
143266        continue;
143267#else
143268        u32 x = pLevel->iLikeRepCntr;
143269        if( x>0 ){
143270          skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot:OP_If,(int)(x>>1));
143271          VdbeCoverageIf(v, (x&1)==1);
143272          VdbeCoverageIf(v, (x&1)==0);
143273        }
143274#endif
143275      }
143276#ifdef WHERETRACE_ENABLED /* 0xffff */
143277      if( sqlite3WhereTrace ){
143278        VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
143279                         pWC->nTerm-j, pTerm, iLoop));
143280      }
143281      if( sqlite3WhereTrace & 0x800 ){
143282        sqlite3DebugPrintf("Coding auxiliary constraint:\n");
143283        sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
143284      }
143285#endif
143286      sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
143287      if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
143288      pTerm->wtFlags |= TERM_CODED;
143289    }
143290    iLoop = iNext;
143291  }while( iLoop>0 );
143292
143293  /* Insert code to test for implied constraints based on transitivity
143294  ** of the "==" operator.
143295  **
143296  ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
143297  ** and we are coding the t1 loop and the t2 loop has not yet coded,
143298  ** then we cannot use the "t1.a=t2.b" constraint, but we can code
143299  ** the implied "t1.a=123" constraint.
143300  */
143301  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
143302    Expr *pE, sEAlt;
143303    WhereTerm *pAlt;
143304    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
143305    if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
143306    if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
143307    if( pTerm->leftCursor!=iCur ) continue;
143308    if( pTabItem->fg.jointype & JT_LEFT ) continue;
143309    pE = pTerm->pExpr;
143310#ifdef WHERETRACE_ENABLED /* 0x800 */
143311    if( sqlite3WhereTrace & 0x800 ){
143312      sqlite3DebugPrintf("Coding transitive constraint:\n");
143313      sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
143314    }
143315#endif
143316    assert( !ExprHasProperty(pE, EP_FromJoin) );
143317    assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
143318    pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
143319                    WO_EQ|WO_IN|WO_IS, 0);
143320    if( pAlt==0 ) continue;
143321    if( pAlt->wtFlags & (TERM_CODED) ) continue;
143322    if( (pAlt->eOperator & WO_IN)
143323     && (pAlt->pExpr->flags & EP_xIsSelect)
143324     && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
143325    ){
143326      continue;
143327    }
143328    testcase( pAlt->eOperator & WO_EQ );
143329    testcase( pAlt->eOperator & WO_IS );
143330    testcase( pAlt->eOperator & WO_IN );
143331    VdbeModuleComment((v, "begin transitive constraint"));
143332    sEAlt = *pAlt->pExpr;
143333    sEAlt.pLeft = pE->pLeft;
143334    sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL);
143335  }
143336
143337  /* For a LEFT OUTER JOIN, generate code that will record the fact that
143338  ** at least one row of the right table has matched the left table.
143339  */
143340  if( pLevel->iLeftJoin ){
143341    pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
143342    sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
143343    VdbeComment((v, "record LEFT JOIN hit"));
143344    for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
143345      testcase( pTerm->wtFlags & TERM_VIRTUAL );
143346      testcase( pTerm->wtFlags & TERM_CODED );
143347      if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
143348      if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
143349        assert( pWInfo->untestedTerms );
143350        continue;
143351      }
143352      assert( pTerm->pExpr );
143353      sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
143354      pTerm->wtFlags |= TERM_CODED;
143355    }
143356  }
143357
143358#if WHERETRACE_ENABLED /* 0x20800 */
143359  if( sqlite3WhereTrace & 0x20000 ){
143360    sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
143361                       iLevel);
143362    sqlite3WhereClausePrint(pWC);
143363  }
143364  if( sqlite3WhereTrace & 0x800 ){
143365    sqlite3DebugPrintf("End Coding level %d:  notReady=%llx\n",
143366       iLevel, (u64)pLevel->notReady);
143367  }
143368#endif
143369  return pLevel->notReady;
143370}
143371
143372/************** End of wherecode.c *******************************************/
143373/************** Begin file whereexpr.c ***************************************/
143374/*
143375** 2015-06-08
143376**
143377** The author disclaims copyright to this source code.  In place of
143378** a legal notice, here is a blessing:
143379**
143380**    May you do good and not evil.
143381**    May you find forgiveness for yourself and forgive others.
143382**    May you share freely, never taking more than you give.
143383**
143384*************************************************************************
143385** This module contains C code that generates VDBE code used to process
143386** the WHERE clause of SQL statements.
143387**
143388** This file was originally part of where.c but was split out to improve
143389** readability and editabiliity.  This file contains utility routines for
143390** analyzing Expr objects in the WHERE clause.
143391*/
143392/* #include "sqliteInt.h" */
143393/* #include "whereInt.h" */
143394
143395/* Forward declarations */
143396static void exprAnalyze(SrcList*, WhereClause*, int);
143397
143398/*
143399** Deallocate all memory associated with a WhereOrInfo object.
143400*/
143401static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
143402  sqlite3WhereClauseClear(&p->wc);
143403  sqlite3DbFree(db, p);
143404}
143405
143406/*
143407** Deallocate all memory associated with a WhereAndInfo object.
143408*/
143409static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
143410  sqlite3WhereClauseClear(&p->wc);
143411  sqlite3DbFree(db, p);
143412}
143413
143414/*
143415** Add a single new WhereTerm entry to the WhereClause object pWC.
143416** The new WhereTerm object is constructed from Expr p and with wtFlags.
143417** The index in pWC->a[] of the new WhereTerm is returned on success.
143418** 0 is returned if the new WhereTerm could not be added due to a memory
143419** allocation error.  The memory allocation failure will be recorded in
143420** the db->mallocFailed flag so that higher-level functions can detect it.
143421**
143422** This routine will increase the size of the pWC->a[] array as necessary.
143423**
143424** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
143425** for freeing the expression p is assumed by the WhereClause object pWC.
143426** This is true even if this routine fails to allocate a new WhereTerm.
143427**
143428** WARNING:  This routine might reallocate the space used to store
143429** WhereTerms.  All pointers to WhereTerms should be invalidated after
143430** calling this routine.  Such pointers may be reinitialized by referencing
143431** the pWC->a[] array.
143432*/
143433static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
143434  WhereTerm *pTerm;
143435  int idx;
143436  testcase( wtFlags & TERM_VIRTUAL );
143437  if( pWC->nTerm>=pWC->nSlot ){
143438    WhereTerm *pOld = pWC->a;
143439    sqlite3 *db = pWC->pWInfo->pParse->db;
143440    pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
143441    if( pWC->a==0 ){
143442      if( wtFlags & TERM_DYNAMIC ){
143443        sqlite3ExprDelete(db, p);
143444      }
143445      pWC->a = pOld;
143446      return 0;
143447    }
143448    memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
143449    if( pOld!=pWC->aStatic ){
143450      sqlite3DbFree(db, pOld);
143451    }
143452    pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
143453  }
143454  pTerm = &pWC->a[idx = pWC->nTerm++];
143455  if( p && ExprHasProperty(p, EP_Unlikely) ){
143456    pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
143457  }else{
143458    pTerm->truthProb = 1;
143459  }
143460  pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p);
143461  pTerm->wtFlags = wtFlags;
143462  pTerm->pWC = pWC;
143463  pTerm->iParent = -1;
143464  memset(&pTerm->eOperator, 0,
143465         sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
143466  return idx;
143467}
143468
143469/*
143470** Return TRUE if the given operator is one of the operators that is
143471** allowed for an indexable WHERE clause term.  The allowed operators are
143472** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL"
143473*/
143474static int allowedOp(int op){
143475  assert( TK_GT>TK_EQ && TK_GT<TK_GE );
143476  assert( TK_LT>TK_EQ && TK_LT<TK_GE );
143477  assert( TK_LE>TK_EQ && TK_LE<TK_GE );
143478  assert( TK_GE==TK_EQ+4 );
143479  return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
143480}
143481
143482/*
143483** Commute a comparison operator.  Expressions of the form "X op Y"
143484** are converted into "Y op X".
143485*/
143486static u16 exprCommute(Parse *pParse, Expr *pExpr){
143487  if( pExpr->pLeft->op==TK_VECTOR
143488   || pExpr->pRight->op==TK_VECTOR
143489   || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) !=
143490      sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft)
143491  ){
143492    pExpr->flags ^= EP_Commuted;
143493  }
143494  SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
143495  if( pExpr->op>=TK_GT ){
143496    assert( TK_LT==TK_GT+2 );
143497    assert( TK_GE==TK_LE+2 );
143498    assert( TK_GT>TK_EQ );
143499    assert( TK_GT<TK_LE );
143500    assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
143501    pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
143502  }
143503  return 0;
143504}
143505
143506/*
143507** Translate from TK_xx operator to WO_xx bitmask.
143508*/
143509static u16 operatorMask(int op){
143510  u16 c;
143511  assert( allowedOp(op) );
143512  if( op==TK_IN ){
143513    c = WO_IN;
143514  }else if( op==TK_ISNULL ){
143515    c = WO_ISNULL;
143516  }else if( op==TK_IS ){
143517    c = WO_IS;
143518  }else{
143519    assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
143520    c = (u16)(WO_EQ<<(op-TK_EQ));
143521  }
143522  assert( op!=TK_ISNULL || c==WO_ISNULL );
143523  assert( op!=TK_IN || c==WO_IN );
143524  assert( op!=TK_EQ || c==WO_EQ );
143525  assert( op!=TK_LT || c==WO_LT );
143526  assert( op!=TK_LE || c==WO_LE );
143527  assert( op!=TK_GT || c==WO_GT );
143528  assert( op!=TK_GE || c==WO_GE );
143529  assert( op!=TK_IS || c==WO_IS );
143530  return c;
143531}
143532
143533
143534#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
143535/*
143536** Check to see if the given expression is a LIKE or GLOB operator that
143537** can be optimized using inequality constraints.  Return TRUE if it is
143538** so and false if not.
143539**
143540** In order for the operator to be optimizible, the RHS must be a string
143541** literal that does not begin with a wildcard.  The LHS must be a column
143542** that may only be NULL, a string, or a BLOB, never a number. (This means
143543** that virtual tables cannot participate in the LIKE optimization.)  The
143544** collating sequence for the column on the LHS must be appropriate for
143545** the operator.
143546*/
143547static int isLikeOrGlob(
143548  Parse *pParse,    /* Parsing and code generating context */
143549  Expr *pExpr,      /* Test this expression */
143550  Expr **ppPrefix,  /* Pointer to TK_STRING expression with pattern prefix */
143551  int *pisComplete, /* True if the only wildcard is % in the last character */
143552  int *pnoCase      /* True if uppercase is equivalent to lowercase */
143553){
143554  const u8 *z = 0;           /* String on RHS of LIKE operator */
143555  Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
143556  ExprList *pList;           /* List of operands to the LIKE operator */
143557  u8 c;                      /* One character in z[] */
143558  int cnt;                   /* Number of non-wildcard prefix characters */
143559  u8 wc[4];                  /* Wildcard characters */
143560  sqlite3 *db = pParse->db;  /* Database connection */
143561  sqlite3_value *pVal = 0;
143562  int op;                    /* Opcode of pRight */
143563  int rc;                    /* Result code to return */
143564
143565  if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, (char*)wc) ){
143566    return 0;
143567  }
143568#ifdef SQLITE_EBCDIC
143569  if( *pnoCase ) return 0;
143570#endif
143571  pList = pExpr->x.pList;
143572  pLeft = pList->a[1].pExpr;
143573
143574  pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
143575  op = pRight->op;
143576  if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
143577    Vdbe *pReprepare = pParse->pReprepare;
143578    int iCol = pRight->iColumn;
143579    pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
143580    if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
143581      z = sqlite3_value_text(pVal);
143582    }
143583    sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
143584    assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
143585  }else if( op==TK_STRING ){
143586    z = (u8*)pRight->u.zToken;
143587  }
143588  if( z ){
143589
143590    /* Count the number of prefix characters prior to the first wildcard */
143591    cnt = 0;
143592    while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
143593      cnt++;
143594      if( c==wc[3] && z[cnt]!=0 ) cnt++;
143595    }
143596
143597    /* The optimization is possible only if (1) the pattern does not begin
143598    ** with a wildcard and if (2) the non-wildcard prefix does not end with
143599    ** an (illegal 0xff) character, or (3) the pattern does not consist of
143600    ** a single escape character. The second condition is necessary so
143601    ** that we can increment the prefix key to find an upper bound for the
143602    ** range search. The third is because the caller assumes that the pattern
143603    ** consists of at least one character after all escapes have been
143604    ** removed.  */
143605    if( cnt!=0 && 255!=(u8)z[cnt-1] && (cnt>1 || z[0]!=wc[3]) ){
143606      Expr *pPrefix;
143607
143608      /* A "complete" match if the pattern ends with "*" or "%" */
143609      *pisComplete = c==wc[0] && z[cnt+1]==0;
143610
143611      /* Get the pattern prefix.  Remove all escapes from the prefix. */
143612      pPrefix = sqlite3Expr(db, TK_STRING, (char*)z);
143613      if( pPrefix ){
143614        int iFrom, iTo;
143615        char *zNew = pPrefix->u.zToken;
143616        zNew[cnt] = 0;
143617        for(iFrom=iTo=0; iFrom<cnt; iFrom++){
143618          if( zNew[iFrom]==wc[3] ) iFrom++;
143619          zNew[iTo++] = zNew[iFrom];
143620        }
143621        zNew[iTo] = 0;
143622        assert( iTo>0 );
143623
143624        /* If the LHS is not an ordinary column with TEXT affinity, then the
143625        ** pattern prefix boundaries (both the start and end boundaries) must
143626        ** not look like a number.  Otherwise the pattern might be treated as
143627        ** a number, which will invalidate the LIKE optimization.
143628        **
143629        ** Getting this right has been a persistent source of bugs in the
143630        ** LIKE optimization.  See, for example:
143631        **    2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
143632        **    2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
143633        **    2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
143634        **    2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
143635        **    2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
143636        */
143637        if( pLeft->op!=TK_COLUMN
143638         || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
143639         || IsVirtual(pLeft->y.pTab)  /* Value might be numeric */
143640        ){
143641          int isNum;
143642          double rDummy;
143643          isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
143644          if( isNum<=0 ){
143645            if( iTo==1 && zNew[0]=='-' ){
143646              isNum = +1;
143647            }else{
143648              zNew[iTo-1]++;
143649              isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
143650              zNew[iTo-1]--;
143651            }
143652          }
143653          if( isNum>0 ){
143654            sqlite3ExprDelete(db, pPrefix);
143655            sqlite3ValueFree(pVal);
143656            return 0;
143657          }
143658        }
143659      }
143660      *ppPrefix = pPrefix;
143661
143662      /* If the RHS pattern is a bound parameter, make arrangements to
143663      ** reprepare the statement when that parameter is rebound */
143664      if( op==TK_VARIABLE ){
143665        Vdbe *v = pParse->pVdbe;
143666        sqlite3VdbeSetVarmask(v, pRight->iColumn);
143667        if( *pisComplete && pRight->u.zToken[1] ){
143668          /* If the rhs of the LIKE expression is a variable, and the current
143669          ** value of the variable means there is no need to invoke the LIKE
143670          ** function, then no OP_Variable will be added to the program.
143671          ** This causes problems for the sqlite3_bind_parameter_name()
143672          ** API. To work around them, add a dummy OP_Variable here.
143673          */
143674          int r1 = sqlite3GetTempReg(pParse);
143675          sqlite3ExprCodeTarget(pParse, pRight, r1);
143676          sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
143677          sqlite3ReleaseTempReg(pParse, r1);
143678        }
143679      }
143680    }else{
143681      z = 0;
143682    }
143683  }
143684
143685  rc = (z!=0);
143686  sqlite3ValueFree(pVal);
143687  return rc;
143688}
143689#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
143690
143691
143692#ifndef SQLITE_OMIT_VIRTUALTABLE
143693/*
143694** Check to see if the pExpr expression is a form that needs to be passed
143695** to the xBestIndex method of virtual tables.  Forms of interest include:
143696**
143697**          Expression                   Virtual Table Operator
143698**          -----------------------      ---------------------------------
143699**      1.  column MATCH expr            SQLITE_INDEX_CONSTRAINT_MATCH
143700**      2.  column GLOB expr             SQLITE_INDEX_CONSTRAINT_GLOB
143701**      3.  column LIKE expr             SQLITE_INDEX_CONSTRAINT_LIKE
143702**      4.  column REGEXP expr           SQLITE_INDEX_CONSTRAINT_REGEXP
143703**      5.  column != expr               SQLITE_INDEX_CONSTRAINT_NE
143704**      6.  expr != column               SQLITE_INDEX_CONSTRAINT_NE
143705**      7.  column IS NOT expr           SQLITE_INDEX_CONSTRAINT_ISNOT
143706**      8.  expr IS NOT column           SQLITE_INDEX_CONSTRAINT_ISNOT
143707**      9.  column IS NOT NULL           SQLITE_INDEX_CONSTRAINT_ISNOTNULL
143708**
143709** In every case, "column" must be a column of a virtual table.  If there
143710** is a match, set *ppLeft to the "column" expression, set *ppRight to the
143711** "expr" expression (even though in forms (6) and (8) the column is on the
143712** right and the expression is on the left).  Also set *peOp2 to the
143713** appropriate virtual table operator.  The return value is 1 or 2 if there
143714** is a match.  The usual return is 1, but if the RHS is also a column
143715** of virtual table in forms (5) or (7) then return 2.
143716**
143717** If the expression matches none of the patterns above, return 0.
143718*/
143719static int isAuxiliaryVtabOperator(
143720  sqlite3 *db,                    /* Parsing context */
143721  Expr *pExpr,                    /* Test this expression */
143722  unsigned char *peOp2,           /* OUT: 0 for MATCH, or else an op2 value */
143723  Expr **ppLeft,                  /* Column expression to left of MATCH/op2 */
143724  Expr **ppRight                  /* Expression to left of MATCH/op2 */
143725){
143726  if( pExpr->op==TK_FUNCTION ){
143727    static const struct Op2 {
143728      const char *zOp;
143729      unsigned char eOp2;
143730    } aOp[] = {
143731      { "match",  SQLITE_INDEX_CONSTRAINT_MATCH },
143732      { "glob",   SQLITE_INDEX_CONSTRAINT_GLOB },
143733      { "like",   SQLITE_INDEX_CONSTRAINT_LIKE },
143734      { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
143735    };
143736    ExprList *pList;
143737    Expr *pCol;                     /* Column reference */
143738    int i;
143739
143740    pList = pExpr->x.pList;
143741    if( pList==0 || pList->nExpr!=2 ){
143742      return 0;
143743    }
143744
143745    /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
143746    ** virtual table on their second argument, which is the same as
143747    ** the left-hand side operand in their in-fix form.
143748    **
143749    **       vtab_column MATCH expression
143750    **       MATCH(expression,vtab_column)
143751    */
143752    pCol = pList->a[1].pExpr;
143753    testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
143754    if( ExprIsVtab(pCol) ){
143755      for(i=0; i<ArraySize(aOp); i++){
143756        if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
143757          *peOp2 = aOp[i].eOp2;
143758          *ppRight = pList->a[0].pExpr;
143759          *ppLeft = pCol;
143760          return 1;
143761        }
143762      }
143763    }
143764
143765    /* We can also match against the first column of overloaded
143766    ** functions where xFindFunction returns a value of at least
143767    ** SQLITE_INDEX_CONSTRAINT_FUNCTION.
143768    **
143769    **      OVERLOADED(vtab_column,expression)
143770    **
143771    ** Historically, xFindFunction expected to see lower-case function
143772    ** names.  But for this use case, xFindFunction is expected to deal
143773    ** with function names in an arbitrary case.
143774    */
143775    pCol = pList->a[0].pExpr;
143776    testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
143777    if( ExprIsVtab(pCol) ){
143778      sqlite3_vtab *pVtab;
143779      sqlite3_module *pMod;
143780      void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
143781      void *pNotUsed;
143782      pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
143783      assert( pVtab!=0 );
143784      assert( pVtab->pModule!=0 );
143785      pMod = (sqlite3_module *)pVtab->pModule;
143786      if( pMod->xFindFunction!=0 ){
143787        i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
143788        if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
143789          *peOp2 = i;
143790          *ppRight = pList->a[1].pExpr;
143791          *ppLeft = pCol;
143792          return 1;
143793        }
143794      }
143795    }
143796  }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
143797    int res = 0;
143798    Expr *pLeft = pExpr->pLeft;
143799    Expr *pRight = pExpr->pRight;
143800    testcase( pLeft->op==TK_COLUMN && pLeft->y.pTab==0 );
143801    if( ExprIsVtab(pLeft) ){
143802      res++;
143803    }
143804    testcase( pRight && pRight->op==TK_COLUMN && pRight->y.pTab==0 );
143805    if( pRight && ExprIsVtab(pRight) ){
143806      res++;
143807      SWAP(Expr*, pLeft, pRight);
143808    }
143809    *ppLeft = pLeft;
143810    *ppRight = pRight;
143811    if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
143812    if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
143813    if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
143814    return res;
143815  }
143816  return 0;
143817}
143818#endif /* SQLITE_OMIT_VIRTUALTABLE */
143819
143820/*
143821** If the pBase expression originated in the ON or USING clause of
143822** a join, then transfer the appropriate markings over to derived.
143823*/
143824static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
143825  if( pDerived ){
143826    pDerived->flags |= pBase->flags & EP_FromJoin;
143827    pDerived->iRightJoinTable = pBase->iRightJoinTable;
143828  }
143829}
143830
143831/*
143832** Mark term iChild as being a child of term iParent
143833*/
143834static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
143835  pWC->a[iChild].iParent = iParent;
143836  pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
143837  pWC->a[iParent].nChild++;
143838}
143839
143840/*
143841** Return the N-th AND-connected subterm of pTerm.  Or if pTerm is not
143842** a conjunction, then return just pTerm when N==0.  If N is exceeds
143843** the number of available subterms, return NULL.
143844*/
143845static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
143846  if( pTerm->eOperator!=WO_AND ){
143847    return N==0 ? pTerm : 0;
143848  }
143849  if( N<pTerm->u.pAndInfo->wc.nTerm ){
143850    return &pTerm->u.pAndInfo->wc.a[N];
143851  }
143852  return 0;
143853}
143854
143855/*
143856** Subterms pOne and pTwo are contained within WHERE clause pWC.  The
143857** two subterms are in disjunction - they are OR-ed together.
143858**
143859** If these two terms are both of the form:  "A op B" with the same
143860** A and B values but different operators and if the operators are
143861** compatible (if one is = and the other is <, for example) then
143862** add a new virtual AND term to pWC that is the combination of the
143863** two.
143864**
143865** Some examples:
143866**
143867**    x<y OR x=y    -->     x<=y
143868**    x=y OR x=y    -->     x=y
143869**    x<=y OR x<y   -->     x<=y
143870**
143871** The following is NOT generated:
143872**
143873**    x<y OR x>y    -->     x!=y
143874*/
143875static void whereCombineDisjuncts(
143876  SrcList *pSrc,         /* the FROM clause */
143877  WhereClause *pWC,      /* The complete WHERE clause */
143878  WhereTerm *pOne,       /* First disjunct */
143879  WhereTerm *pTwo        /* Second disjunct */
143880){
143881  u16 eOp = pOne->eOperator | pTwo->eOperator;
143882  sqlite3 *db;           /* Database connection (for malloc) */
143883  Expr *pNew;            /* New virtual expression */
143884  int op;                /* Operator for the combined expression */
143885  int idxNew;            /* Index in pWC of the next virtual term */
143886
143887  if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
143888  if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
143889  if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
143890   && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
143891  assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
143892  assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
143893  if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
143894  if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
143895  /* If we reach this point, it means the two subterms can be combined */
143896  if( (eOp & (eOp-1))!=0 ){
143897    if( eOp & (WO_LT|WO_LE) ){
143898      eOp = WO_LE;
143899    }else{
143900      assert( eOp & (WO_GT|WO_GE) );
143901      eOp = WO_GE;
143902    }
143903  }
143904  db = pWC->pWInfo->pParse->db;
143905  pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
143906  if( pNew==0 ) return;
143907  for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
143908  pNew->op = op;
143909  idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
143910  exprAnalyze(pSrc, pWC, idxNew);
143911}
143912
143913#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
143914/*
143915** Analyze a term that consists of two or more OR-connected
143916** subterms.  So in:
143917**
143918**     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
143919**                          ^^^^^^^^^^^^^^^^^^^^
143920**
143921** This routine analyzes terms such as the middle term in the above example.
143922** A WhereOrTerm object is computed and attached to the term under
143923** analysis, regardless of the outcome of the analysis.  Hence:
143924**
143925**     WhereTerm.wtFlags   |=  TERM_ORINFO
143926**     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
143927**
143928** The term being analyzed must have two or more of OR-connected subterms.
143929** A single subterm might be a set of AND-connected sub-subterms.
143930** Examples of terms under analysis:
143931**
143932**     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
143933**     (B)     x=expr1 OR expr2=x OR x=expr3
143934**     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
143935**     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
143936**     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
143937**     (F)     x>A OR (x=A AND y>=B)
143938**
143939** CASE 1:
143940**
143941** If all subterms are of the form T.C=expr for some single column of C and
143942** a single table T (as shown in example B above) then create a new virtual
143943** term that is an equivalent IN expression.  In other words, if the term
143944** being analyzed is:
143945**
143946**      x = expr1  OR  expr2 = x  OR  x = expr3
143947**
143948** then create a new virtual term like this:
143949**
143950**      x IN (expr1,expr2,expr3)
143951**
143952** CASE 2:
143953**
143954** If there are exactly two disjuncts and one side has x>A and the other side
143955** has x=A (for the same x and A) then add a new virtual conjunct term to the
143956** WHERE clause of the form "x>=A".  Example:
143957**
143958**      x>A OR (x=A AND y>B)    adds:    x>=A
143959**
143960** The added conjunct can sometimes be helpful in query planning.
143961**
143962** CASE 3:
143963**
143964** If all subterms are indexable by a single table T, then set
143965**
143966**     WhereTerm.eOperator              =  WO_OR
143967**     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
143968**
143969** A subterm is "indexable" if it is of the form
143970** "T.C <op> <expr>" where C is any column of table T and
143971** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
143972** A subterm is also indexable if it is an AND of two or more
143973** subsubterms at least one of which is indexable.  Indexable AND
143974** subterms have their eOperator set to WO_AND and they have
143975** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
143976**
143977** From another point of view, "indexable" means that the subterm could
143978** potentially be used with an index if an appropriate index exists.
143979** This analysis does not consider whether or not the index exists; that
143980** is decided elsewhere.  This analysis only looks at whether subterms
143981** appropriate for indexing exist.
143982**
143983** All examples A through E above satisfy case 3.  But if a term
143984** also satisfies case 1 (such as B) we know that the optimizer will
143985** always prefer case 1, so in that case we pretend that case 3 is not
143986** satisfied.
143987**
143988** It might be the case that multiple tables are indexable.  For example,
143989** (E) above is indexable on tables P, Q, and R.
143990**
143991** Terms that satisfy case 3 are candidates for lookup by using
143992** separate indices to find rowids for each subterm and composing
143993** the union of all rowids using a RowSet object.  This is similar
143994** to "bitmap indices" in other database engines.
143995**
143996** OTHERWISE:
143997**
143998** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
143999** zero.  This term is not useful for search.
144000*/
144001static void exprAnalyzeOrTerm(
144002  SrcList *pSrc,            /* the FROM clause */
144003  WhereClause *pWC,         /* the complete WHERE clause */
144004  int idxTerm               /* Index of the OR-term to be analyzed */
144005){
144006  WhereInfo *pWInfo = pWC->pWInfo;        /* WHERE clause processing context */
144007  Parse *pParse = pWInfo->pParse;         /* Parser context */
144008  sqlite3 *db = pParse->db;               /* Database connection */
144009  WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
144010  Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
144011  int i;                                  /* Loop counters */
144012  WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
144013  WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
144014  WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
144015  Bitmask chngToIN;         /* Tables that might satisfy case 1 */
144016  Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
144017
144018  /*
144019  ** Break the OR clause into its separate subterms.  The subterms are
144020  ** stored in a WhereClause structure containing within the WhereOrInfo
144021  ** object that is attached to the original OR clause term.
144022  */
144023  assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
144024  assert( pExpr->op==TK_OR );
144025  pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
144026  if( pOrInfo==0 ) return;
144027  pTerm->wtFlags |= TERM_ORINFO;
144028  pOrWc = &pOrInfo->wc;
144029  memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
144030  sqlite3WhereClauseInit(pOrWc, pWInfo);
144031  sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
144032  sqlite3WhereExprAnalyze(pSrc, pOrWc);
144033  if( db->mallocFailed ) return;
144034  assert( pOrWc->nTerm>=2 );
144035
144036  /*
144037  ** Compute the set of tables that might satisfy cases 1 or 3.
144038  */
144039  indexable = ~(Bitmask)0;
144040  chngToIN = ~(Bitmask)0;
144041  for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
144042    if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
144043      WhereAndInfo *pAndInfo;
144044      assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
144045      chngToIN = 0;
144046      pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo));
144047      if( pAndInfo ){
144048        WhereClause *pAndWC;
144049        WhereTerm *pAndTerm;
144050        int j;
144051        Bitmask b = 0;
144052        pOrTerm->u.pAndInfo = pAndInfo;
144053        pOrTerm->wtFlags |= TERM_ANDINFO;
144054        pOrTerm->eOperator = WO_AND;
144055        pAndWC = &pAndInfo->wc;
144056        memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
144057        sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
144058        sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
144059        sqlite3WhereExprAnalyze(pSrc, pAndWC);
144060        pAndWC->pOuter = pWC;
144061        if( !db->mallocFailed ){
144062          for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
144063            assert( pAndTerm->pExpr );
144064            if( allowedOp(pAndTerm->pExpr->op)
144065             || pAndTerm->eOperator==WO_AUX
144066            ){
144067              b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
144068            }
144069          }
144070        }
144071        indexable &= b;
144072      }
144073    }else if( pOrTerm->wtFlags & TERM_COPIED ){
144074      /* Skip this term for now.  We revisit it when we process the
144075      ** corresponding TERM_VIRTUAL term */
144076    }else{
144077      Bitmask b;
144078      b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
144079      if( pOrTerm->wtFlags & TERM_VIRTUAL ){
144080        WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
144081        b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
144082      }
144083      indexable &= b;
144084      if( (pOrTerm->eOperator & WO_EQ)==0 ){
144085        chngToIN = 0;
144086      }else{
144087        chngToIN &= b;
144088      }
144089    }
144090  }
144091
144092  /*
144093  ** Record the set of tables that satisfy case 3.  The set might be
144094  ** empty.
144095  */
144096  pOrInfo->indexable = indexable;
144097  if( indexable ){
144098    pTerm->eOperator = WO_OR;
144099    pWC->hasOr = 1;
144100  }else{
144101    pTerm->eOperator = WO_OR;
144102  }
144103
144104  /* For a two-way OR, attempt to implementation case 2.
144105  */
144106  if( indexable && pOrWc->nTerm==2 ){
144107    int iOne = 0;
144108    WhereTerm *pOne;
144109    while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
144110      int iTwo = 0;
144111      WhereTerm *pTwo;
144112      while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
144113        whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
144114      }
144115    }
144116  }
144117
144118  /*
144119  ** chngToIN holds a set of tables that *might* satisfy case 1.  But
144120  ** we have to do some additional checking to see if case 1 really
144121  ** is satisfied.
144122  **
144123  ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
144124  ** that there is no possibility of transforming the OR clause into an
144125  ** IN operator because one or more terms in the OR clause contain
144126  ** something other than == on a column in the single table.  The 1-bit
144127  ** case means that every term of the OR clause is of the form
144128  ** "table.column=expr" for some single table.  The one bit that is set
144129  ** will correspond to the common table.  We still need to check to make
144130  ** sure the same column is used on all terms.  The 2-bit case is when
144131  ** the all terms are of the form "table1.column=table2.column".  It
144132  ** might be possible to form an IN operator with either table1.column
144133  ** or table2.column as the LHS if either is common to every term of
144134  ** the OR clause.
144135  **
144136  ** Note that terms of the form "table.column1=table.column2" (the
144137  ** same table on both sizes of the ==) cannot be optimized.
144138  */
144139  if( chngToIN ){
144140    int okToChngToIN = 0;     /* True if the conversion to IN is valid */
144141    int iColumn = -1;         /* Column index on lhs of IN operator */
144142    int iCursor = -1;         /* Table cursor common to all terms */
144143    int j = 0;                /* Loop counter */
144144
144145    /* Search for a table and column that appears on one side or the
144146    ** other of the == operator in every subterm.  That table and column
144147    ** will be recorded in iCursor and iColumn.  There might not be any
144148    ** such table and column.  Set okToChngToIN if an appropriate table
144149    ** and column is found but leave okToChngToIN false if not found.
144150    */
144151    for(j=0; j<2 && !okToChngToIN; j++){
144152      Expr *pLeft = 0;
144153      pOrTerm = pOrWc->a;
144154      for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
144155        assert( pOrTerm->eOperator & WO_EQ );
144156        pOrTerm->wtFlags &= ~TERM_OR_OK;
144157        if( pOrTerm->leftCursor==iCursor ){
144158          /* This is the 2-bit case and we are on the second iteration and
144159          ** current term is from the first iteration.  So skip this term. */
144160          assert( j==1 );
144161          continue;
144162        }
144163        if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
144164                                            pOrTerm->leftCursor))==0 ){
144165          /* This term must be of the form t1.a==t2.b where t2 is in the
144166          ** chngToIN set but t1 is not.  This term will be either preceded
144167          ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term
144168          ** and use its inversion. */
144169          testcase( pOrTerm->wtFlags & TERM_COPIED );
144170          testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
144171          assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
144172          continue;
144173        }
144174        iColumn = pOrTerm->u.leftColumn;
144175        iCursor = pOrTerm->leftCursor;
144176        pLeft = pOrTerm->pExpr->pLeft;
144177        break;
144178      }
144179      if( i<0 ){
144180        /* No candidate table+column was found.  This can only occur
144181        ** on the second iteration */
144182        assert( j==1 );
144183        assert( IsPowerOfTwo(chngToIN) );
144184        assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
144185        break;
144186      }
144187      testcase( j==1 );
144188
144189      /* We have found a candidate table and column.  Check to see if that
144190      ** table and column is common to every term in the OR clause */
144191      okToChngToIN = 1;
144192      for(; i>=0 && okToChngToIN; i--, pOrTerm++){
144193        assert( pOrTerm->eOperator & WO_EQ );
144194        if( pOrTerm->leftCursor!=iCursor ){
144195          pOrTerm->wtFlags &= ~TERM_OR_OK;
144196        }else if( pOrTerm->u.leftColumn!=iColumn || (iColumn==XN_EXPR
144197               && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
144198        )){
144199          okToChngToIN = 0;
144200        }else{
144201          int affLeft, affRight;
144202          /* If the right-hand side is also a column, then the affinities
144203          ** of both right and left sides must be such that no type
144204          ** conversions are required on the right.  (Ticket #2249)
144205          */
144206          affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
144207          affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
144208          if( affRight!=0 && affRight!=affLeft ){
144209            okToChngToIN = 0;
144210          }else{
144211            pOrTerm->wtFlags |= TERM_OR_OK;
144212          }
144213        }
144214      }
144215    }
144216
144217    /* At this point, okToChngToIN is true if original pTerm satisfies
144218    ** case 1.  In that case, construct a new virtual term that is
144219    ** pTerm converted into an IN operator.
144220    */
144221    if( okToChngToIN ){
144222      Expr *pDup;            /* A transient duplicate expression */
144223      ExprList *pList = 0;   /* The RHS of the IN operator */
144224      Expr *pLeft = 0;       /* The LHS of the IN operator */
144225      Expr *pNew;            /* The complete IN operator */
144226
144227      for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
144228        if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
144229        assert( pOrTerm->eOperator & WO_EQ );
144230        assert( pOrTerm->leftCursor==iCursor );
144231        assert( pOrTerm->u.leftColumn==iColumn );
144232        pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
144233        pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
144234        pLeft = pOrTerm->pExpr->pLeft;
144235      }
144236      assert( pLeft!=0 );
144237      pDup = sqlite3ExprDup(db, pLeft, 0);
144238      pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0);
144239      if( pNew ){
144240        int idxNew;
144241        transferJoinMarkings(pNew, pExpr);
144242        assert( !ExprHasProperty(pNew, EP_xIsSelect) );
144243        pNew->x.pList = pList;
144244        idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
144245        testcase( idxNew==0 );
144246        exprAnalyze(pSrc, pWC, idxNew);
144247        /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where used again */
144248        markTermAsChild(pWC, idxNew, idxTerm);
144249      }else{
144250        sqlite3ExprListDelete(db, pList);
144251      }
144252    }
144253  }
144254}
144255#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
144256
144257/*
144258** We already know that pExpr is a binary operator where both operands are
144259** column references.  This routine checks to see if pExpr is an equivalence
144260** relation:
144261**   1.  The SQLITE_Transitive optimization must be enabled
144262**   2.  Must be either an == or an IS operator
144263**   3.  Not originating in the ON clause of an OUTER JOIN
144264**   4.  The affinities of A and B must be compatible
144265**   5a. Both operands use the same collating sequence OR
144266**   5b. The overall collating sequence is BINARY
144267** If this routine returns TRUE, that means that the RHS can be substituted
144268** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
144269** This is an optimization.  No harm comes from returning 0.  But if 1 is
144270** returned when it should not be, then incorrect answers might result.
144271*/
144272static int termIsEquivalence(Parse *pParse, Expr *pExpr){
144273  char aff1, aff2;
144274  CollSeq *pColl;
144275  if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
144276  if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
144277  if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
144278  aff1 = sqlite3ExprAffinity(pExpr->pLeft);
144279  aff2 = sqlite3ExprAffinity(pExpr->pRight);
144280  if( aff1!=aff2
144281   && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
144282  ){
144283    return 0;
144284  }
144285  pColl = sqlite3ExprCompareCollSeq(pParse, pExpr);
144286  if( sqlite3IsBinary(pColl) ) return 1;
144287  return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
144288}
144289
144290/*
144291** Recursively walk the expressions of a SELECT statement and generate
144292** a bitmask indicating which tables are used in that expression
144293** tree.
144294*/
144295static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
144296  Bitmask mask = 0;
144297  while( pS ){
144298    SrcList *pSrc = pS->pSrc;
144299    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
144300    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
144301    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
144302    mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
144303    mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
144304    if( ALWAYS(pSrc!=0) ){
144305      int i;
144306      for(i=0; i<pSrc->nSrc; i++){
144307        mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
144308        mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
144309        if( pSrc->a[i].fg.isTabFunc ){
144310          mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
144311        }
144312      }
144313    }
144314    pS = pS->pPrior;
144315  }
144316  return mask;
144317}
144318
144319/*
144320** Expression pExpr is one operand of a comparison operator that might
144321** be useful for indexing.  This routine checks to see if pExpr appears
144322** in any index.  Return TRUE (1) if pExpr is an indexed term and return
144323** FALSE (0) if not.  If TRUE is returned, also set aiCurCol[0] to the cursor
144324** number of the table that is indexed and aiCurCol[1] to the column number
144325** of the column that is indexed, or XN_EXPR (-2) if an expression is being
144326** indexed.
144327**
144328** If pExpr is a TK_COLUMN column reference, then this routine always returns
144329** true even if that particular column is not indexed, because the column
144330** might be added to an automatic index later.
144331*/
144332static SQLITE_NOINLINE int exprMightBeIndexed2(
144333  SrcList *pFrom,        /* The FROM clause */
144334  Bitmask mPrereq,       /* Bitmask of FROM clause terms referenced by pExpr */
144335  int *aiCurCol,         /* Write the referenced table cursor and column here */
144336  Expr *pExpr            /* An operand of a comparison operator */
144337){
144338  Index *pIdx;
144339  int i;
144340  int iCur;
144341  for(i=0; mPrereq>1; i++, mPrereq>>=1){}
144342  iCur = pFrom->a[i].iCursor;
144343  for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
144344    if( pIdx->aColExpr==0 ) continue;
144345    for(i=0; i<pIdx->nKeyCol; i++){
144346      if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
144347      if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
144348        aiCurCol[0] = iCur;
144349        aiCurCol[1] = XN_EXPR;
144350        return 1;
144351      }
144352    }
144353  }
144354  return 0;
144355}
144356static int exprMightBeIndexed(
144357  SrcList *pFrom,        /* The FROM clause */
144358  Bitmask mPrereq,       /* Bitmask of FROM clause terms referenced by pExpr */
144359  int *aiCurCol,         /* Write the referenced table cursor & column here */
144360  Expr *pExpr,           /* An operand of a comparison operator */
144361  int op                 /* The specific comparison operator */
144362){
144363  /* If this expression is a vector to the left or right of a
144364  ** inequality constraint (>, <, >= or <=), perform the processing
144365  ** on the first element of the vector.  */
144366  assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE );
144367  assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE );
144368  assert( op<=TK_GE );
144369  if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
144370    pExpr = pExpr->x.pList->a[0].pExpr;
144371  }
144372
144373  if( pExpr->op==TK_COLUMN ){
144374    aiCurCol[0] = pExpr->iTable;
144375    aiCurCol[1] = pExpr->iColumn;
144376    return 1;
144377  }
144378  if( mPrereq==0 ) return 0;                 /* No table references */
144379  if( (mPrereq&(mPrereq-1))!=0 ) return 0;   /* Refs more than one table */
144380  return exprMightBeIndexed2(pFrom,mPrereq,aiCurCol,pExpr);
144381}
144382
144383/*
144384** The input to this routine is an WhereTerm structure with only the
144385** "pExpr" field filled in.  The job of this routine is to analyze the
144386** subexpression and populate all the other fields of the WhereTerm
144387** structure.
144388**
144389** If the expression is of the form "<expr> <op> X" it gets commuted
144390** to the standard form of "X <op> <expr>".
144391**
144392** If the expression is of the form "X <op> Y" where both X and Y are
144393** columns, then the original expression is unchanged and a new virtual
144394** term of the form "Y <op> X" is added to the WHERE clause and
144395** analyzed separately.  The original term is marked with TERM_COPIED
144396** and the new term is marked with TERM_DYNAMIC (because it's pExpr
144397** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
144398** is a commuted copy of a prior term.)  The original term has nChild=1
144399** and the copy has idxParent set to the index of the original term.
144400*/
144401static void exprAnalyze(
144402  SrcList *pSrc,            /* the FROM clause */
144403  WhereClause *pWC,         /* the WHERE clause */
144404  int idxTerm               /* Index of the term to be analyzed */
144405){
144406  WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
144407  WhereTerm *pTerm;                /* The term to be analyzed */
144408  WhereMaskSet *pMaskSet;          /* Set of table index masks */
144409  Expr *pExpr;                     /* The expression to be analyzed */
144410  Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
144411  Bitmask prereqAll;               /* Prerequesites of pExpr */
144412  Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
144413  Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
144414  int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
144415  int noCase = 0;                  /* uppercase equivalent to lowercase */
144416  int op;                          /* Top-level operator.  pExpr->op */
144417  Parse *pParse = pWInfo->pParse;  /* Parsing context */
144418  sqlite3 *db = pParse->db;        /* Database connection */
144419  unsigned char eOp2 = 0;          /* op2 value for LIKE/REGEXP/GLOB */
144420  int nLeft;                       /* Number of elements on left side vector */
144421
144422  if( db->mallocFailed ){
144423    return;
144424  }
144425  pTerm = &pWC->a[idxTerm];
144426  pMaskSet = &pWInfo->sMaskSet;
144427  pExpr = pTerm->pExpr;
144428  assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
144429  prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
144430  op = pExpr->op;
144431  if( op==TK_IN ){
144432    assert( pExpr->pRight==0 );
144433    if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
144434    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
144435      pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
144436    }else{
144437      pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
144438    }
144439  }else if( op==TK_ISNULL ){
144440    pTerm->prereqRight = 0;
144441  }else{
144442    pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
144443  }
144444  pMaskSet->bVarSelect = 0;
144445  prereqAll = sqlite3WhereExprUsageNN(pMaskSet, pExpr);
144446  if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
144447  if( ExprHasProperty(pExpr, EP_FromJoin) ){
144448    Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
144449    prereqAll |= x;
144450    extraRight = x-1;  /* ON clause terms may not be used with an index
144451                       ** on left table of a LEFT JOIN.  Ticket #3015 */
144452    if( (prereqAll>>1)>=x ){
144453      sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
144454      return;
144455    }
144456  }
144457  pTerm->prereqAll = prereqAll;
144458  pTerm->leftCursor = -1;
144459  pTerm->iParent = -1;
144460  pTerm->eOperator = 0;
144461  if( allowedOp(op) ){
144462    int aiCurCol[2];
144463    Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
144464    Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
144465    u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
144466
144467    if( pTerm->iField>0 ){
144468      assert( op==TK_IN );
144469      assert( pLeft->op==TK_VECTOR );
144470      pLeft = pLeft->x.pList->a[pTerm->iField-1].pExpr;
144471    }
144472
144473    if( exprMightBeIndexed(pSrc, prereqLeft, aiCurCol, pLeft, op) ){
144474      pTerm->leftCursor = aiCurCol[0];
144475      pTerm->u.leftColumn = aiCurCol[1];
144476      pTerm->eOperator = operatorMask(op) & opMask;
144477    }
144478    if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
144479    if( pRight
144480     && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op)
144481    ){
144482      WhereTerm *pNew;
144483      Expr *pDup;
144484      u16 eExtraOp = 0;        /* Extra bits for pNew->eOperator */
144485      assert( pTerm->iField==0 );
144486      if( pTerm->leftCursor>=0 ){
144487        int idxNew;
144488        pDup = sqlite3ExprDup(db, pExpr, 0);
144489        if( db->mallocFailed ){
144490          sqlite3ExprDelete(db, pDup);
144491          return;
144492        }
144493        idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
144494        if( idxNew==0 ) return;
144495        pNew = &pWC->a[idxNew];
144496        markTermAsChild(pWC, idxNew, idxTerm);
144497        if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
144498        pTerm = &pWC->a[idxTerm];
144499        pTerm->wtFlags |= TERM_COPIED;
144500
144501        if( termIsEquivalence(pParse, pDup) ){
144502          pTerm->eOperator |= WO_EQUIV;
144503          eExtraOp = WO_EQUIV;
144504        }
144505      }else{
144506        pDup = pExpr;
144507        pNew = pTerm;
144508      }
144509      pNew->wtFlags |= exprCommute(pParse, pDup);
144510      pNew->leftCursor = aiCurCol[0];
144511      pNew->u.leftColumn = aiCurCol[1];
144512      testcase( (prereqLeft | extraRight) != prereqLeft );
144513      pNew->prereqRight = prereqLeft | extraRight;
144514      pNew->prereqAll = prereqAll;
144515      pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
144516    }
144517  }
144518
144519#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
144520  /* If a term is the BETWEEN operator, create two new virtual terms
144521  ** that define the range that the BETWEEN implements.  For example:
144522  **
144523  **      a BETWEEN b AND c
144524  **
144525  ** is converted into:
144526  **
144527  **      (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
144528  **
144529  ** The two new terms are added onto the end of the WhereClause object.
144530  ** The new terms are "dynamic" and are children of the original BETWEEN
144531  ** term.  That means that if the BETWEEN term is coded, the children are
144532  ** skipped.  Or, if the children are satisfied by an index, the original
144533  ** BETWEEN term is skipped.
144534  */
144535  else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
144536    ExprList *pList = pExpr->x.pList;
144537    int i;
144538    static const u8 ops[] = {TK_GE, TK_LE};
144539    assert( pList!=0 );
144540    assert( pList->nExpr==2 );
144541    for(i=0; i<2; i++){
144542      Expr *pNewExpr;
144543      int idxNew;
144544      pNewExpr = sqlite3PExpr(pParse, ops[i],
144545                             sqlite3ExprDup(db, pExpr->pLeft, 0),
144546                             sqlite3ExprDup(db, pList->a[i].pExpr, 0));
144547      transferJoinMarkings(pNewExpr, pExpr);
144548      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
144549      testcase( idxNew==0 );
144550      exprAnalyze(pSrc, pWC, idxNew);
144551      pTerm = &pWC->a[idxTerm];
144552      markTermAsChild(pWC, idxNew, idxTerm);
144553    }
144554  }
144555#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
144556
144557#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
144558  /* Analyze a term that is composed of two or more subterms connected by
144559  ** an OR operator.
144560  */
144561  else if( pExpr->op==TK_OR ){
144562    assert( pWC->op==TK_AND );
144563    exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
144564    pTerm = &pWC->a[idxTerm];
144565  }
144566#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
144567
144568#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
144569  /* Add constraints to reduce the search space on a LIKE or GLOB
144570  ** operator.
144571  **
144572  ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
144573  **
144574  **          x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
144575  **
144576  ** The last character of the prefix "abc" is incremented to form the
144577  ** termination condition "abd".  If case is not significant (the default
144578  ** for LIKE) then the lower-bound is made all uppercase and the upper-
144579  ** bound is made all lowercase so that the bounds also work when comparing
144580  ** BLOBs.
144581  */
144582  if( pWC->op==TK_AND
144583   && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
144584  ){
144585    Expr *pLeft;       /* LHS of LIKE/GLOB operator */
144586    Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
144587    Expr *pNewExpr1;
144588    Expr *pNewExpr2;
144589    int idxNew1;
144590    int idxNew2;
144591    const char *zCollSeqName;     /* Name of collating sequence */
144592    const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
144593
144594    pLeft = pExpr->x.pList->a[1].pExpr;
144595    pStr2 = sqlite3ExprDup(db, pStr1, 0);
144596
144597    /* Convert the lower bound to upper-case and the upper bound to
144598    ** lower-case (upper-case is less than lower-case in ASCII) so that
144599    ** the range constraints also work for BLOBs
144600    */
144601    if( noCase && !pParse->db->mallocFailed ){
144602      int i;
144603      char c;
144604      pTerm->wtFlags |= TERM_LIKE;
144605      for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
144606        pStr1->u.zToken[i] = sqlite3Toupper(c);
144607        pStr2->u.zToken[i] = sqlite3Tolower(c);
144608      }
144609    }
144610
144611    if( !db->mallocFailed ){
144612      u8 c, *pC;       /* Last character before the first wildcard */
144613      pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
144614      c = *pC;
144615      if( noCase ){
144616        /* The point is to increment the last character before the first
144617        ** wildcard.  But if we increment '@', that will push it into the
144618        ** alphabetic range where case conversions will mess up the
144619        ** inequality.  To avoid this, make sure to also run the full
144620        ** LIKE on all candidate expressions by clearing the isComplete flag
144621        */
144622        if( c=='A'-1 ) isComplete = 0;
144623        c = sqlite3UpperToLower[c];
144624      }
144625      *pC = c + 1;
144626    }
144627    zCollSeqName = noCase ? "NOCASE" : sqlite3StrBINARY;
144628    pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
144629    pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
144630           sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
144631           pStr1);
144632    transferJoinMarkings(pNewExpr1, pExpr);
144633    idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
144634    testcase( idxNew1==0 );
144635    exprAnalyze(pSrc, pWC, idxNew1);
144636    pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
144637    pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
144638           sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
144639           pStr2);
144640    transferJoinMarkings(pNewExpr2, pExpr);
144641    idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
144642    testcase( idxNew2==0 );
144643    exprAnalyze(pSrc, pWC, idxNew2);
144644    pTerm = &pWC->a[idxTerm];
144645    if( isComplete ){
144646      markTermAsChild(pWC, idxNew1, idxTerm);
144647      markTermAsChild(pWC, idxNew2, idxTerm);
144648    }
144649  }
144650#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
144651
144652#ifndef SQLITE_OMIT_VIRTUALTABLE
144653  /* Add a WO_AUX auxiliary term to the constraint set if the
144654  ** current expression is of the form "column OP expr" where OP
144655  ** is an operator that gets passed into virtual tables but which is
144656  ** not normally optimized for ordinary tables.  In other words, OP
144657  ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL.
144658  ** This information is used by the xBestIndex methods of
144659  ** virtual tables.  The native query optimizer does not attempt
144660  ** to do anything with MATCH functions.
144661  */
144662  if( pWC->op==TK_AND ){
144663    Expr *pRight = 0, *pLeft = 0;
144664    int res = isAuxiliaryVtabOperator(db, pExpr, &eOp2, &pLeft, &pRight);
144665    while( res-- > 0 ){
144666      int idxNew;
144667      WhereTerm *pNewTerm;
144668      Bitmask prereqColumn, prereqExpr;
144669
144670      prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
144671      prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
144672      if( (prereqExpr & prereqColumn)==0 ){
144673        Expr *pNewExpr;
144674        pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
144675            0, sqlite3ExprDup(db, pRight, 0));
144676        if( ExprHasProperty(pExpr, EP_FromJoin) && pNewExpr ){
144677          ExprSetProperty(pNewExpr, EP_FromJoin);
144678          pNewExpr->iRightJoinTable = pExpr->iRightJoinTable;
144679        }
144680        idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
144681        testcase( idxNew==0 );
144682        pNewTerm = &pWC->a[idxNew];
144683        pNewTerm->prereqRight = prereqExpr;
144684        pNewTerm->leftCursor = pLeft->iTable;
144685        pNewTerm->u.leftColumn = pLeft->iColumn;
144686        pNewTerm->eOperator = WO_AUX;
144687        pNewTerm->eMatchOp = eOp2;
144688        markTermAsChild(pWC, idxNew, idxTerm);
144689        pTerm = &pWC->a[idxTerm];
144690        pTerm->wtFlags |= TERM_COPIED;
144691        pNewTerm->prereqAll = pTerm->prereqAll;
144692      }
144693      SWAP(Expr*, pLeft, pRight);
144694    }
144695  }
144696#endif /* SQLITE_OMIT_VIRTUALTABLE */
144697
144698  /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
144699  ** new terms for each component comparison - "a = ?" and "b = ?".  The
144700  ** new terms completely replace the original vector comparison, which is
144701  ** no longer used.
144702  **
144703  ** This is only required if at least one side of the comparison operation
144704  ** is not a sub-select.  */
144705  if( pWC->op==TK_AND
144706  && (pExpr->op==TK_EQ || pExpr->op==TK_IS)
144707  && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
144708  && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
144709  && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
144710    || (pExpr->pRight->flags & EP_xIsSelect)==0)
144711  ){
144712    int i;
144713    for(i=0; i<nLeft; i++){
144714      int idxNew;
144715      Expr *pNew;
144716      Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
144717      Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
144718
144719      pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
144720      transferJoinMarkings(pNew, pExpr);
144721      idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC);
144722      exprAnalyze(pSrc, pWC, idxNew);
144723    }
144724    pTerm = &pWC->a[idxTerm];
144725    pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL;  /* Disable the original */
144726    pTerm->eOperator = 0;
144727  }
144728
144729  /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
144730  ** a virtual term for each vector component. The expression object
144731  ** used by each such virtual term is pExpr (the full vector IN(...)
144732  ** expression). The WhereTerm.iField variable identifies the index within
144733  ** the vector on the LHS that the virtual term represents.
144734  **
144735  ** This only works if the RHS is a simple SELECT (not a compound) that does
144736  ** not use window functions.
144737  */
144738  if( pWC->op==TK_AND && pExpr->op==TK_IN && pTerm->iField==0
144739   && pExpr->pLeft->op==TK_VECTOR
144740   && pExpr->x.pSelect->pPrior==0
144741#ifndef SQLITE_OMIT_WINDOWFUNC
144742   && pExpr->x.pSelect->pWin==0
144743#endif
144744  ){
144745    int i;
144746    for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
144747      int idxNew;
144748      idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL);
144749      pWC->a[idxNew].iField = i+1;
144750      exprAnalyze(pSrc, pWC, idxNew);
144751      markTermAsChild(pWC, idxNew, idxTerm);
144752    }
144753  }
144754
144755#ifdef SQLITE_ENABLE_STAT4
144756  /* When sqlite_stat4 histogram data is available an operator of the
144757  ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
144758  ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
144759  ** virtual term of that form.
144760  **
144761  ** Note that the virtual term must be tagged with TERM_VNULL.
144762  */
144763  if( pExpr->op==TK_NOTNULL
144764   && pExpr->pLeft->op==TK_COLUMN
144765   && pExpr->pLeft->iColumn>=0
144766   && !ExprHasProperty(pExpr, EP_FromJoin)
144767   && OptimizationEnabled(db, SQLITE_Stat4)
144768  ){
144769    Expr *pNewExpr;
144770    Expr *pLeft = pExpr->pLeft;
144771    int idxNew;
144772    WhereTerm *pNewTerm;
144773
144774    pNewExpr = sqlite3PExpr(pParse, TK_GT,
144775                            sqlite3ExprDup(db, pLeft, 0),
144776                            sqlite3ExprAlloc(db, TK_NULL, 0, 0));
144777
144778    idxNew = whereClauseInsert(pWC, pNewExpr,
144779                              TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
144780    if( idxNew ){
144781      pNewTerm = &pWC->a[idxNew];
144782      pNewTerm->prereqRight = 0;
144783      pNewTerm->leftCursor = pLeft->iTable;
144784      pNewTerm->u.leftColumn = pLeft->iColumn;
144785      pNewTerm->eOperator = WO_GT;
144786      markTermAsChild(pWC, idxNew, idxTerm);
144787      pTerm = &pWC->a[idxTerm];
144788      pTerm->wtFlags |= TERM_COPIED;
144789      pNewTerm->prereqAll = pTerm->prereqAll;
144790    }
144791  }
144792#endif /* SQLITE_ENABLE_STAT4 */
144793
144794  /* Prevent ON clause terms of a LEFT JOIN from being used to drive
144795  ** an index for tables to the left of the join.
144796  */
144797  testcase( pTerm!=&pWC->a[idxTerm] );
144798  pTerm = &pWC->a[idxTerm];
144799  pTerm->prereqRight |= extraRight;
144800}
144801
144802/***************************************************************************
144803** Routines with file scope above.  Interface to the rest of the where.c
144804** subsystem follows.
144805***************************************************************************/
144806
144807/*
144808** This routine identifies subexpressions in the WHERE clause where
144809** each subexpression is separated by the AND operator or some other
144810** operator specified in the op parameter.  The WhereClause structure
144811** is filled with pointers to subexpressions.  For example:
144812**
144813**    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
144814**           \________/     \_______________/     \________________/
144815**            slot[0]            slot[1]               slot[2]
144816**
144817** The original WHERE clause in pExpr is unaltered.  All this routine
144818** does is make slot[] entries point to substructure within pExpr.
144819**
144820** In the previous sentence and in the diagram, "slot[]" refers to
144821** the WhereClause.a[] array.  The slot[] array grows as needed to contain
144822** all terms of the WHERE clause.
144823*/
144824SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
144825  Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pExpr);
144826  pWC->op = op;
144827  if( pE2==0 ) return;
144828  if( pE2->op!=op ){
144829    whereClauseInsert(pWC, pExpr, 0);
144830  }else{
144831    sqlite3WhereSplit(pWC, pE2->pLeft, op);
144832    sqlite3WhereSplit(pWC, pE2->pRight, op);
144833  }
144834}
144835
144836/*
144837** Initialize a preallocated WhereClause structure.
144838*/
144839SQLITE_PRIVATE void sqlite3WhereClauseInit(
144840  WhereClause *pWC,        /* The WhereClause to be initialized */
144841  WhereInfo *pWInfo        /* The WHERE processing context */
144842){
144843  pWC->pWInfo = pWInfo;
144844  pWC->hasOr = 0;
144845  pWC->pOuter = 0;
144846  pWC->nTerm = 0;
144847  pWC->nSlot = ArraySize(pWC->aStatic);
144848  pWC->a = pWC->aStatic;
144849}
144850
144851/*
144852** Deallocate a WhereClause structure.  The WhereClause structure
144853** itself is not freed.  This routine is the inverse of
144854** sqlite3WhereClauseInit().
144855*/
144856SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause *pWC){
144857  int i;
144858  WhereTerm *a;
144859  sqlite3 *db = pWC->pWInfo->pParse->db;
144860  for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
144861    if( a->wtFlags & TERM_DYNAMIC ){
144862      sqlite3ExprDelete(db, a->pExpr);
144863    }
144864    if( a->wtFlags & TERM_ORINFO ){
144865      whereOrInfoDelete(db, a->u.pOrInfo);
144866    }else if( a->wtFlags & TERM_ANDINFO ){
144867      whereAndInfoDelete(db, a->u.pAndInfo);
144868    }
144869  }
144870  if( pWC->a!=pWC->aStatic ){
144871    sqlite3DbFree(db, pWC->a);
144872  }
144873}
144874
144875
144876/*
144877** These routines walk (recursively) an expression tree and generate
144878** a bitmask indicating which tables are used in that expression
144879** tree.
144880*/
144881SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet *pMaskSet, Expr *p){
144882  Bitmask mask;
144883  if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
144884    return sqlite3WhereGetMask(pMaskSet, p->iTable);
144885  }else if( ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
144886    assert( p->op!=TK_IF_NULL_ROW );
144887    return 0;
144888  }
144889  mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
144890  if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
144891  if( p->pRight ){
144892    mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
144893    assert( p->x.pList==0 );
144894  }else if( ExprHasProperty(p, EP_xIsSelect) ){
144895    if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
144896    mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
144897  }else if( p->x.pList ){
144898    mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
144899  }
144900#ifndef SQLITE_OMIT_WINDOWFUNC
144901  if( (p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION) && p->y.pWin ){
144902    mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
144903    mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
144904    mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
144905  }
144906#endif
144907  return mask;
144908}
144909SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
144910  return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
144911}
144912SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
144913  int i;
144914  Bitmask mask = 0;
144915  if( pList ){
144916    for(i=0; i<pList->nExpr; i++){
144917      mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
144918    }
144919  }
144920  return mask;
144921}
144922
144923
144924/*
144925** Call exprAnalyze on all terms in a WHERE clause.
144926**
144927** Note that exprAnalyze() might add new virtual terms onto the
144928** end of the WHERE clause.  We do not want to analyze these new
144929** virtual terms, so start analyzing at the end and work forward
144930** so that the added virtual terms are never processed.
144931*/
144932SQLITE_PRIVATE void sqlite3WhereExprAnalyze(
144933  SrcList *pTabList,       /* the FROM clause */
144934  WhereClause *pWC         /* the WHERE clause to be analyzed */
144935){
144936  int i;
144937  for(i=pWC->nTerm-1; i>=0; i--){
144938    exprAnalyze(pTabList, pWC, i);
144939  }
144940}
144941
144942/*
144943** For table-valued-functions, transform the function arguments into
144944** new WHERE clause terms.
144945**
144946** Each function argument translates into an equality constraint against
144947** a HIDDEN column in the table.
144948*/
144949SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(
144950  Parse *pParse,                    /* Parsing context */
144951  struct SrcList_item *pItem,       /* The FROM clause term to process */
144952  WhereClause *pWC                  /* Xfer function arguments to here */
144953){
144954  Table *pTab;
144955  int j, k;
144956  ExprList *pArgs;
144957  Expr *pColRef;
144958  Expr *pTerm;
144959  if( pItem->fg.isTabFunc==0 ) return;
144960  pTab = pItem->pTab;
144961  assert( pTab!=0 );
144962  pArgs = pItem->u1.pFuncArg;
144963  if( pArgs==0 ) return;
144964  for(j=k=0; j<pArgs->nExpr; j++){
144965    Expr *pRhs;
144966    while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
144967    if( k>=pTab->nCol ){
144968      sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
144969                      pTab->zName, j);
144970      return;
144971    }
144972    pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
144973    if( pColRef==0 ) return;
144974    pColRef->iTable = pItem->iCursor;
144975    pColRef->iColumn = k++;
144976    pColRef->y.pTab = pTab;
144977    pRhs = sqlite3PExpr(pParse, TK_UPLUS,
144978        sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
144979    pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs);
144980    if( pItem->fg.jointype & JT_LEFT ){
144981      sqlite3SetJoinExpr(pTerm, pItem->iCursor);
144982    }
144983    whereClauseInsert(pWC, pTerm, TERM_DYNAMIC);
144984  }
144985}
144986
144987/************** End of whereexpr.c *******************************************/
144988/************** Begin file where.c *******************************************/
144989/*
144990** 2001 September 15
144991**
144992** The author disclaims copyright to this source code.  In place of
144993** a legal notice, here is a blessing:
144994**
144995**    May you do good and not evil.
144996**    May you find forgiveness for yourself and forgive others.
144997**    May you share freely, never taking more than you give.
144998**
144999*************************************************************************
145000** This module contains C code that generates VDBE code used to process
145001** the WHERE clause of SQL statements.  This module is responsible for
145002** generating the code that loops through a table looking for applicable
145003** rows.  Indices are selected and used to speed the search when doing
145004** so is applicable.  Because this module is responsible for selecting
145005** indices, you might also think of this module as the "query optimizer".
145006*/
145007/* #include "sqliteInt.h" */
145008/* #include "whereInt.h" */
145009
145010/*
145011** Extra information appended to the end of sqlite3_index_info but not
145012** visible to the xBestIndex function, at least not directly.  The
145013** sqlite3_vtab_collation() interface knows how to reach it, however.
145014**
145015** This object is not an API and can be changed from one release to the
145016** next.  As long as allocateIndexInfo() and sqlite3_vtab_collation()
145017** agree on the structure, all will be well.
145018*/
145019typedef struct HiddenIndexInfo HiddenIndexInfo;
145020struct HiddenIndexInfo {
145021  WhereClause *pWC;   /* The Where clause being analyzed */
145022  Parse *pParse;      /* The parsing context */
145023};
145024
145025/* Forward declaration of methods */
145026static int whereLoopResize(sqlite3*, WhereLoop*, int);
145027
145028/* Test variable that can be set to enable WHERE tracing */
145029#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
145030/***/ int sqlite3WhereTrace = 0;
145031#endif
145032
145033
145034/*
145035** Return the estimated number of output rows from a WHERE clause
145036*/
145037SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
145038  return pWInfo->nRowOut;
145039}
145040
145041/*
145042** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
145043** WHERE clause returns outputs for DISTINCT processing.
145044*/
145045SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
145046  return pWInfo->eDistinct;
145047}
145048
145049/*
145050** Return TRUE if the WHERE clause returns rows in ORDER BY order.
145051** Return FALSE if the output needs to be sorted.
145052*/
145053SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
145054  return pWInfo->nOBSat;
145055}
145056
145057/*
145058** In the ORDER BY LIMIT optimization, if the inner-most loop is known
145059** to emit rows in increasing order, and if the last row emitted by the
145060** inner-most loop did not fit within the sorter, then we can skip all
145061** subsequent rows for the current iteration of the inner loop (because they
145062** will not fit in the sorter either) and continue with the second inner
145063** loop - the loop immediately outside the inner-most.
145064**
145065** When a row does not fit in the sorter (because the sorter already
145066** holds LIMIT+OFFSET rows that are smaller), then a jump is made to the
145067** label returned by this function.
145068**
145069** If the ORDER BY LIMIT optimization applies, the jump destination should
145070** be the continuation for the second-inner-most loop.  If the ORDER BY
145071** LIMIT optimization does not apply, then the jump destination should
145072** be the continuation for the inner-most loop.
145073**
145074** It is always safe for this routine to return the continuation of the
145075** inner-most loop, in the sense that a correct answer will result.
145076** Returning the continuation the second inner loop is an optimization
145077** that might make the code run a little faster, but should not change
145078** the final answer.
145079*/
145080SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo *pWInfo){
145081  WhereLevel *pInner;
145082  if( !pWInfo->bOrderedInnerLoop ){
145083    /* The ORDER BY LIMIT optimization does not apply.  Jump to the
145084    ** continuation of the inner-most loop. */
145085    return pWInfo->iContinue;
145086  }
145087  pInner = &pWInfo->a[pWInfo->nLevel-1];
145088  assert( pInner->addrNxt!=0 );
145089  return pInner->addrNxt;
145090}
145091
145092/*
145093** Return the VDBE address or label to jump to in order to continue
145094** immediately with the next row of a WHERE clause.
145095*/
145096SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
145097  assert( pWInfo->iContinue!=0 );
145098  return pWInfo->iContinue;
145099}
145100
145101/*
145102** Return the VDBE address or label to jump to in order to break
145103** out of a WHERE loop.
145104*/
145105SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
145106  return pWInfo->iBreak;
145107}
145108
145109/*
145110** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to
145111** operate directly on the rowids returned by a WHERE clause.  Return
145112** ONEPASS_SINGLE (1) if the statement can operation directly because only
145113** a single row is to be changed.  Return ONEPASS_MULTI (2) if the one-pass
145114** optimization can be used on multiple
145115**
145116** If the ONEPASS optimization is used (if this routine returns true)
145117** then also write the indices of open cursors used by ONEPASS
145118** into aiCur[0] and aiCur[1].  iaCur[0] gets the cursor of the data
145119** table and iaCur[1] gets the cursor used by an auxiliary index.
145120** Either value may be -1, indicating that cursor is not used.
145121** Any cursors returned will have been opened for writing.
145122**
145123** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
145124** unable to use the ONEPASS optimization.
145125*/
145126SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
145127  memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
145128#ifdef WHERETRACE_ENABLED
145129  if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
145130    sqlite3DebugPrintf("%s cursors: %d %d\n",
145131         pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
145132         aiCur[0], aiCur[1]);
145133  }
145134#endif
145135  return pWInfo->eOnePass;
145136}
145137
145138/*
145139** Return TRUE if the WHERE loop uses the OP_DeferredSeek opcode to move
145140** the data cursor to the row selected by the index cursor.
145141*/
145142SQLITE_PRIVATE int sqlite3WhereUsesDeferredSeek(WhereInfo *pWInfo){
145143  return pWInfo->bDeferredSeek;
145144}
145145
145146/*
145147** Move the content of pSrc into pDest
145148*/
145149static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
145150  pDest->n = pSrc->n;
145151  memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
145152}
145153
145154/*
145155** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
145156**
145157** The new entry might overwrite an existing entry, or it might be
145158** appended, or it might be discarded.  Do whatever is the right thing
145159** so that pSet keeps the N_OR_COST best entries seen so far.
145160*/
145161static int whereOrInsert(
145162  WhereOrSet *pSet,      /* The WhereOrSet to be updated */
145163  Bitmask prereq,        /* Prerequisites of the new entry */
145164  LogEst rRun,           /* Run-cost of the new entry */
145165  LogEst nOut            /* Number of outputs for the new entry */
145166){
145167  u16 i;
145168  WhereOrCost *p;
145169  for(i=pSet->n, p=pSet->a; i>0; i--, p++){
145170    if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
145171      goto whereOrInsert_done;
145172    }
145173    if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
145174      return 0;
145175    }
145176  }
145177  if( pSet->n<N_OR_COST ){
145178    p = &pSet->a[pSet->n++];
145179    p->nOut = nOut;
145180  }else{
145181    p = pSet->a;
145182    for(i=1; i<pSet->n; i++){
145183      if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
145184    }
145185    if( p->rRun<=rRun ) return 0;
145186  }
145187whereOrInsert_done:
145188  p->prereq = prereq;
145189  p->rRun = rRun;
145190  if( p->nOut>nOut ) p->nOut = nOut;
145191  return 1;
145192}
145193
145194/*
145195** Return the bitmask for the given cursor number.  Return 0 if
145196** iCursor is not in the set.
145197*/
145198SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
145199  int i;
145200  assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
145201  for(i=0; i<pMaskSet->n; i++){
145202    if( pMaskSet->ix[i]==iCursor ){
145203      return MASKBIT(i);
145204    }
145205  }
145206  return 0;
145207}
145208
145209/*
145210** Create a new mask for cursor iCursor.
145211**
145212** There is one cursor per table in the FROM clause.  The number of
145213** tables in the FROM clause is limited by a test early in the
145214** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
145215** array will never overflow.
145216*/
145217static void createMask(WhereMaskSet *pMaskSet, int iCursor){
145218  assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
145219  pMaskSet->ix[pMaskSet->n++] = iCursor;
145220}
145221
145222/*
145223** Advance to the next WhereTerm that matches according to the criteria
145224** established when the pScan object was initialized by whereScanInit().
145225** Return NULL if there are no more matching WhereTerms.
145226*/
145227static WhereTerm *whereScanNext(WhereScan *pScan){
145228  int iCur;            /* The cursor on the LHS of the term */
145229  i16 iColumn;         /* The column on the LHS of the term.  -1 for IPK */
145230  Expr *pX;            /* An expression being tested */
145231  WhereClause *pWC;    /* Shorthand for pScan->pWC */
145232  WhereTerm *pTerm;    /* The term being tested */
145233  int k = pScan->k;    /* Where to start scanning */
145234
145235  assert( pScan->iEquiv<=pScan->nEquiv );
145236  pWC = pScan->pWC;
145237  while(1){
145238    iColumn = pScan->aiColumn[pScan->iEquiv-1];
145239    iCur = pScan->aiCur[pScan->iEquiv-1];
145240    assert( pWC!=0 );
145241    do{
145242      for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
145243        if( pTerm->leftCursor==iCur
145244         && pTerm->u.leftColumn==iColumn
145245         && (iColumn!=XN_EXPR
145246             || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
145247                                       pScan->pIdxExpr,iCur)==0)
145248         && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
145249        ){
145250          if( (pTerm->eOperator & WO_EQUIV)!=0
145251           && pScan->nEquiv<ArraySize(pScan->aiCur)
145252           && (pX = sqlite3ExprSkipCollateAndLikely(pTerm->pExpr->pRight))->op
145253               ==TK_COLUMN
145254          ){
145255            int j;
145256            for(j=0; j<pScan->nEquiv; j++){
145257              if( pScan->aiCur[j]==pX->iTable
145258               && pScan->aiColumn[j]==pX->iColumn ){
145259                  break;
145260              }
145261            }
145262            if( j==pScan->nEquiv ){
145263              pScan->aiCur[j] = pX->iTable;
145264              pScan->aiColumn[j] = pX->iColumn;
145265              pScan->nEquiv++;
145266            }
145267          }
145268          if( (pTerm->eOperator & pScan->opMask)!=0 ){
145269            /* Verify the affinity and collating sequence match */
145270            if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
145271              CollSeq *pColl;
145272              Parse *pParse = pWC->pWInfo->pParse;
145273              pX = pTerm->pExpr;
145274              if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
145275                continue;
145276              }
145277              assert(pX->pLeft);
145278              pColl = sqlite3ExprCompareCollSeq(pParse, pX);
145279              if( pColl==0 ) pColl = pParse->db->pDfltColl;
145280              if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
145281                continue;
145282              }
145283            }
145284            if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
145285             && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
145286             && pX->iTable==pScan->aiCur[0]
145287             && pX->iColumn==pScan->aiColumn[0]
145288            ){
145289              testcase( pTerm->eOperator & WO_IS );
145290              continue;
145291            }
145292            pScan->pWC = pWC;
145293            pScan->k = k+1;
145294            return pTerm;
145295          }
145296        }
145297      }
145298      pWC = pWC->pOuter;
145299      k = 0;
145300    }while( pWC!=0 );
145301    if( pScan->iEquiv>=pScan->nEquiv ) break;
145302    pWC = pScan->pOrigWC;
145303    k = 0;
145304    pScan->iEquiv++;
145305  }
145306  return 0;
145307}
145308
145309/*
145310** This is whereScanInit() for the case of an index on an expression.
145311** It is factored out into a separate tail-recursion subroutine so that
145312** the normal whereScanInit() routine, which is a high-runner, does not
145313** need to push registers onto the stack as part of its prologue.
145314*/
145315static SQLITE_NOINLINE WhereTerm *whereScanInitIndexExpr(WhereScan *pScan){
145316  pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr);
145317  return whereScanNext(pScan);
145318}
145319
145320/*
145321** Initialize a WHERE clause scanner object.  Return a pointer to the
145322** first match.  Return NULL if there are no matches.
145323**
145324** The scanner will be searching the WHERE clause pWC.  It will look
145325** for terms of the form "X <op> <expr>" where X is column iColumn of table
145326** iCur.   Or if pIdx!=0 then X is column iColumn of index pIdx.  pIdx
145327** must be one of the indexes of table iCur.
145328**
145329** The <op> must be one of the operators described by opMask.
145330**
145331** If the search is for X and the WHERE clause contains terms of the
145332** form X=Y then this routine might also return terms of the form
145333** "Y <op> <expr>".  The number of levels of transitivity is limited,
145334** but is enough to handle most commonly occurring SQL statements.
145335**
145336** If X is not the INTEGER PRIMARY KEY then X must be compatible with
145337** index pIdx.
145338*/
145339static WhereTerm *whereScanInit(
145340  WhereScan *pScan,       /* The WhereScan object being initialized */
145341  WhereClause *pWC,       /* The WHERE clause to be scanned */
145342  int iCur,               /* Cursor to scan for */
145343  int iColumn,            /* Column to scan for */
145344  u32 opMask,             /* Operator(s) to scan for */
145345  Index *pIdx             /* Must be compatible with this index */
145346){
145347  pScan->pOrigWC = pWC;
145348  pScan->pWC = pWC;
145349  pScan->pIdxExpr = 0;
145350  pScan->idxaff = 0;
145351  pScan->zCollName = 0;
145352  pScan->opMask = opMask;
145353  pScan->k = 0;
145354  pScan->aiCur[0] = iCur;
145355  pScan->nEquiv = 1;
145356  pScan->iEquiv = 1;
145357  if( pIdx ){
145358    int j = iColumn;
145359    iColumn = pIdx->aiColumn[j];
145360    if( iColumn==XN_EXPR ){
145361      pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
145362      pScan->zCollName = pIdx->azColl[j];
145363      pScan->aiColumn[0] = XN_EXPR;
145364      return whereScanInitIndexExpr(pScan);
145365    }else if( iColumn==pIdx->pTable->iPKey ){
145366      iColumn = XN_ROWID;
145367    }else if( iColumn>=0 ){
145368      pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
145369      pScan->zCollName = pIdx->azColl[j];
145370    }
145371  }else if( iColumn==XN_EXPR ){
145372    return 0;
145373  }
145374  pScan->aiColumn[0] = iColumn;
145375  return whereScanNext(pScan);
145376}
145377
145378/*
145379** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
145380** where X is a reference to the iColumn of table iCur or of index pIdx
145381** if pIdx!=0 and <op> is one of the WO_xx operator codes specified by
145382** the op parameter.  Return a pointer to the term.  Return 0 if not found.
145383**
145384** If pIdx!=0 then it must be one of the indexes of table iCur.
145385** Search for terms matching the iColumn-th column of pIdx
145386** rather than the iColumn-th column of table iCur.
145387**
145388** The term returned might by Y=<expr> if there is another constraint in
145389** the WHERE clause that specifies that X=Y.  Any such constraints will be
145390** identified by the WO_EQUIV bit in the pTerm->eOperator field.  The
145391** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11
145392** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10
145393** other equivalent values.  Hence a search for X will return <expr> if X=A1
145394** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>.
145395**
145396** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
145397** then try for the one with no dependencies on <expr> - in other words where
145398** <expr> is a constant expression of some kind.  Only return entries of
145399** the form "X <op> Y" where Y is a column in another table if no terms of
145400** the form "X <op> <const-expr>" exist.   If no terms with a constant RHS
145401** exist, try to return a term that does not use WO_EQUIV.
145402*/
145403SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
145404  WhereClause *pWC,     /* The WHERE clause to be searched */
145405  int iCur,             /* Cursor number of LHS */
145406  int iColumn,          /* Column number of LHS */
145407  Bitmask notReady,     /* RHS must not overlap with this mask */
145408  u32 op,               /* Mask of WO_xx values describing operator */
145409  Index *pIdx           /* Must be compatible with this index, if not NULL */
145410){
145411  WhereTerm *pResult = 0;
145412  WhereTerm *p;
145413  WhereScan scan;
145414
145415  p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
145416  op &= WO_EQ|WO_IS;
145417  while( p ){
145418    if( (p->prereqRight & notReady)==0 ){
145419      if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
145420        testcase( p->eOperator & WO_IS );
145421        return p;
145422      }
145423      if( pResult==0 ) pResult = p;
145424    }
145425    p = whereScanNext(&scan);
145426  }
145427  return pResult;
145428}
145429
145430/*
145431** This function searches pList for an entry that matches the iCol-th column
145432** of index pIdx.
145433**
145434** If such an expression is found, its index in pList->a[] is returned. If
145435** no expression is found, -1 is returned.
145436*/
145437static int findIndexCol(
145438  Parse *pParse,                  /* Parse context */
145439  ExprList *pList,                /* Expression list to search */
145440  int iBase,                      /* Cursor for table associated with pIdx */
145441  Index *pIdx,                    /* Index to match column of */
145442  int iCol                        /* Column of index to match */
145443){
145444  int i;
145445  const char *zColl = pIdx->azColl[iCol];
145446
145447  for(i=0; i<pList->nExpr; i++){
145448    Expr *p = sqlite3ExprSkipCollateAndLikely(pList->a[i].pExpr);
145449    if( p->op==TK_COLUMN
145450     && p->iColumn==pIdx->aiColumn[iCol]
145451     && p->iTable==iBase
145452    ){
145453      CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
145454      if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
145455        return i;
145456      }
145457    }
145458  }
145459
145460  return -1;
145461}
145462
145463/*
145464** Return TRUE if the iCol-th column of index pIdx is NOT NULL
145465*/
145466static int indexColumnNotNull(Index *pIdx, int iCol){
145467  int j;
145468  assert( pIdx!=0 );
145469  assert( iCol>=0 && iCol<pIdx->nColumn );
145470  j = pIdx->aiColumn[iCol];
145471  if( j>=0 ){
145472    return pIdx->pTable->aCol[j].notNull;
145473  }else if( j==(-1) ){
145474    return 1;
145475  }else{
145476    assert( j==(-2) );
145477    return 0;  /* Assume an indexed expression can always yield a NULL */
145478
145479  }
145480}
145481
145482/*
145483** Return true if the DISTINCT expression-list passed as the third argument
145484** is redundant.
145485**
145486** A DISTINCT list is redundant if any subset of the columns in the
145487** DISTINCT list are collectively unique and individually non-null.
145488*/
145489static int isDistinctRedundant(
145490  Parse *pParse,            /* Parsing context */
145491  SrcList *pTabList,        /* The FROM clause */
145492  WhereClause *pWC,         /* The WHERE clause */
145493  ExprList *pDistinct       /* The result set that needs to be DISTINCT */
145494){
145495  Table *pTab;
145496  Index *pIdx;
145497  int i;
145498  int iBase;
145499
145500  /* If there is more than one table or sub-select in the FROM clause of
145501  ** this query, then it will not be possible to show that the DISTINCT
145502  ** clause is redundant. */
145503  if( pTabList->nSrc!=1 ) return 0;
145504  iBase = pTabList->a[0].iCursor;
145505  pTab = pTabList->a[0].pTab;
145506
145507  /* If any of the expressions is an IPK column on table iBase, then return
145508  ** true. Note: The (p->iTable==iBase) part of this test may be false if the
145509  ** current SELECT is a correlated sub-query.
145510  */
145511  for(i=0; i<pDistinct->nExpr; i++){
145512    Expr *p = sqlite3ExprSkipCollateAndLikely(pDistinct->a[i].pExpr);
145513    if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
145514  }
145515
145516  /* Loop through all indices on the table, checking each to see if it makes
145517  ** the DISTINCT qualifier redundant. It does so if:
145518  **
145519  **   1. The index is itself UNIQUE, and
145520  **
145521  **   2. All of the columns in the index are either part of the pDistinct
145522  **      list, or else the WHERE clause contains a term of the form "col=X",
145523  **      where X is a constant value. The collation sequences of the
145524  **      comparison and select-list expressions must match those of the index.
145525  **
145526  **   3. All of those index columns for which the WHERE clause does not
145527  **      contain a "col=X" term are subject to a NOT NULL constraint.
145528  */
145529  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
145530    if( !IsUniqueIndex(pIdx) ) continue;
145531    for(i=0; i<pIdx->nKeyCol; i++){
145532      if( 0==sqlite3WhereFindTerm(pWC, iBase, i, ~(Bitmask)0, WO_EQ, pIdx) ){
145533        if( findIndexCol(pParse, pDistinct, iBase, pIdx, i)<0 ) break;
145534        if( indexColumnNotNull(pIdx, i)==0 ) break;
145535      }
145536    }
145537    if( i==pIdx->nKeyCol ){
145538      /* This index implies that the DISTINCT qualifier is redundant. */
145539      return 1;
145540    }
145541  }
145542
145543  return 0;
145544}
145545
145546
145547/*
145548** Estimate the logarithm of the input value to base 2.
145549*/
145550static LogEst estLog(LogEst N){
145551  return N<=10 ? 0 : sqlite3LogEst(N) - 33;
145552}
145553
145554/*
145555** Convert OP_Column opcodes to OP_Copy in previously generated code.
145556**
145557** This routine runs over generated VDBE code and translates OP_Column
145558** opcodes into OP_Copy when the table is being accessed via co-routine
145559** instead of via table lookup.
145560**
145561** If the iAutoidxCur is not zero, then any OP_Rowid instructions on
145562** cursor iTabCur are transformed into OP_Sequence opcode for the
145563** iAutoidxCur cursor, in order to generate unique rowids for the
145564** automatic index being generated.
145565*/
145566static void translateColumnToCopy(
145567  Parse *pParse,      /* Parsing context */
145568  int iStart,         /* Translate from this opcode to the end */
145569  int iTabCur,        /* OP_Column/OP_Rowid references to this table */
145570  int iRegister,      /* The first column is in this register */
145571  int iAutoidxCur     /* If non-zero, cursor of autoindex being generated */
145572){
145573  Vdbe *v = pParse->pVdbe;
145574  VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
145575  int iEnd = sqlite3VdbeCurrentAddr(v);
145576  if( pParse->db->mallocFailed ) return;
145577  for(; iStart<iEnd; iStart++, pOp++){
145578    if( pOp->p1!=iTabCur ) continue;
145579    if( pOp->opcode==OP_Column ){
145580      pOp->opcode = OP_Copy;
145581      pOp->p1 = pOp->p2 + iRegister;
145582      pOp->p2 = pOp->p3;
145583      pOp->p3 = 0;
145584    }else if( pOp->opcode==OP_Rowid ){
145585      if( iAutoidxCur ){
145586        pOp->opcode = OP_Sequence;
145587        pOp->p1 = iAutoidxCur;
145588      }else{
145589        pOp->opcode = OP_Null;
145590        pOp->p1 = 0;
145591        pOp->p3 = 0;
145592      }
145593    }
145594  }
145595}
145596
145597/*
145598** Two routines for printing the content of an sqlite3_index_info
145599** structure.  Used for testing and debugging only.  If neither
145600** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
145601** are no-ops.
145602*/
145603#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
145604static void whereTraceIndexInfoInputs(sqlite3_index_info *p){
145605  int i;
145606  if( !sqlite3WhereTrace ) return;
145607  for(i=0; i<p->nConstraint; i++){
145608    sqlite3DebugPrintf("  constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
145609       i,
145610       p->aConstraint[i].iColumn,
145611       p->aConstraint[i].iTermOffset,
145612       p->aConstraint[i].op,
145613       p->aConstraint[i].usable);
145614  }
145615  for(i=0; i<p->nOrderBy; i++){
145616    sqlite3DebugPrintf("  orderby[%d]: col=%d desc=%d\n",
145617       i,
145618       p->aOrderBy[i].iColumn,
145619       p->aOrderBy[i].desc);
145620  }
145621}
145622static void whereTraceIndexInfoOutputs(sqlite3_index_info *p){
145623  int i;
145624  if( !sqlite3WhereTrace ) return;
145625  for(i=0; i<p->nConstraint; i++){
145626    sqlite3DebugPrintf("  usage[%d]: argvIdx=%d omit=%d\n",
145627       i,
145628       p->aConstraintUsage[i].argvIndex,
145629       p->aConstraintUsage[i].omit);
145630  }
145631  sqlite3DebugPrintf("  idxNum=%d\n", p->idxNum);
145632  sqlite3DebugPrintf("  idxStr=%s\n", p->idxStr);
145633  sqlite3DebugPrintf("  orderByConsumed=%d\n", p->orderByConsumed);
145634  sqlite3DebugPrintf("  estimatedCost=%g\n", p->estimatedCost);
145635  sqlite3DebugPrintf("  estimatedRows=%lld\n", p->estimatedRows);
145636}
145637#else
145638#define whereTraceIndexInfoInputs(A)
145639#define whereTraceIndexInfoOutputs(A)
145640#endif
145641
145642#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
145643/*
145644** Return TRUE if the WHERE clause term pTerm is of a form where it
145645** could be used with an index to access pSrc, assuming an appropriate
145646** index existed.
145647*/
145648static int termCanDriveIndex(
145649  WhereTerm *pTerm,              /* WHERE clause term to check */
145650  struct SrcList_item *pSrc,     /* Table we are trying to access */
145651  Bitmask notReady               /* Tables in outer loops of the join */
145652){
145653  char aff;
145654  if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
145655  if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
145656  if( (pSrc->fg.jointype & JT_LEFT)
145657   && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
145658   && (pTerm->eOperator & WO_IS)
145659  ){
145660    /* Cannot use an IS term from the WHERE clause as an index driver for
145661    ** the RHS of a LEFT JOIN. Such a term can only be used if it is from
145662    ** the ON clause.  */
145663    return 0;
145664  }
145665  if( (pTerm->prereqRight & notReady)!=0 ) return 0;
145666  if( pTerm->u.leftColumn<0 ) return 0;
145667  aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
145668  if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
145669  testcase( pTerm->pExpr->op==TK_IS );
145670  return 1;
145671}
145672#endif
145673
145674
145675#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
145676/*
145677** Generate code to construct the Index object for an automatic index
145678** and to set up the WhereLevel object pLevel so that the code generator
145679** makes use of the automatic index.
145680*/
145681static void constructAutomaticIndex(
145682  Parse *pParse,              /* The parsing context */
145683  WhereClause *pWC,           /* The WHERE clause */
145684  struct SrcList_item *pSrc,  /* The FROM clause term to get the next index */
145685  Bitmask notReady,           /* Mask of cursors that are not available */
145686  WhereLevel *pLevel          /* Write new index here */
145687){
145688  int nKeyCol;                /* Number of columns in the constructed index */
145689  WhereTerm *pTerm;           /* A single term of the WHERE clause */
145690  WhereTerm *pWCEnd;          /* End of pWC->a[] */
145691  Index *pIdx;                /* Object describing the transient index */
145692  Vdbe *v;                    /* Prepared statement under construction */
145693  int addrInit;               /* Address of the initialization bypass jump */
145694  Table *pTable;              /* The table being indexed */
145695  int addrTop;                /* Top of the index fill loop */
145696  int regRecord;              /* Register holding an index record */
145697  int n;                      /* Column counter */
145698  int i;                      /* Loop counter */
145699  int mxBitCol;               /* Maximum column in pSrc->colUsed */
145700  CollSeq *pColl;             /* Collating sequence to on a column */
145701  WhereLoop *pLoop;           /* The Loop object */
145702  char *zNotUsed;             /* Extra space on the end of pIdx */
145703  Bitmask idxCols;            /* Bitmap of columns used for indexing */
145704  Bitmask extraCols;          /* Bitmap of additional columns */
145705  u8 sentWarning = 0;         /* True if a warnning has been issued */
145706  Expr *pPartial = 0;         /* Partial Index Expression */
145707  int iContinue = 0;          /* Jump here to skip excluded rows */
145708  struct SrcList_item *pTabItem;  /* FROM clause term being indexed */
145709  int addrCounter = 0;        /* Address where integer counter is initialized */
145710  int regBase;                /* Array of registers where record is assembled */
145711
145712  /* Generate code to skip over the creation and initialization of the
145713  ** transient index on 2nd and subsequent iterations of the loop. */
145714  v = pParse->pVdbe;
145715  assert( v!=0 );
145716  addrInit = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
145717
145718  /* Count the number of columns that will be added to the index
145719  ** and used to match WHERE clause constraints */
145720  nKeyCol = 0;
145721  pTable = pSrc->pTab;
145722  pWCEnd = &pWC->a[pWC->nTerm];
145723  pLoop = pLevel->pWLoop;
145724  idxCols = 0;
145725  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
145726    Expr *pExpr = pTerm->pExpr;
145727    assert( !ExprHasProperty(pExpr, EP_FromJoin)    /* prereq always non-zero */
145728         || pExpr->iRightJoinTable!=pSrc->iCursor   /*   for the right-hand   */
145729         || pLoop->prereq!=0 );                     /*   table of a LEFT JOIN */
145730    if( pLoop->prereq==0
145731     && (pTerm->wtFlags & TERM_VIRTUAL)==0
145732     && !ExprHasProperty(pExpr, EP_FromJoin)
145733     && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){
145734      pPartial = sqlite3ExprAnd(pParse, pPartial,
145735                                sqlite3ExprDup(pParse->db, pExpr, 0));
145736    }
145737    if( termCanDriveIndex(pTerm, pSrc, notReady) ){
145738      int iCol = pTerm->u.leftColumn;
145739      Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
145740      testcase( iCol==BMS );
145741      testcase( iCol==BMS-1 );
145742      if( !sentWarning ){
145743        sqlite3_log(SQLITE_WARNING_AUTOINDEX,
145744            "automatic index on %s(%s)", pTable->zName,
145745            pTable->aCol[iCol].zName);
145746        sentWarning = 1;
145747      }
145748      if( (idxCols & cMask)==0 ){
145749        if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
145750          goto end_auto_index_create;
145751        }
145752        pLoop->aLTerm[nKeyCol++] = pTerm;
145753        idxCols |= cMask;
145754      }
145755    }
145756  }
145757  assert( nKeyCol>0 );
145758  pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
145759  pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
145760                     | WHERE_AUTO_INDEX;
145761
145762  /* Count the number of additional columns needed to create a
145763  ** covering index.  A "covering index" is an index that contains all
145764  ** columns that are needed by the query.  With a covering index, the
145765  ** original table never needs to be accessed.  Automatic indices must
145766  ** be a covering index because the index will not be updated if the
145767  ** original table changes and the index and table cannot both be used
145768  ** if they go out of sync.
145769  */
145770  extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
145771  mxBitCol = MIN(BMS-1,pTable->nCol);
145772  testcase( pTable->nCol==BMS-1 );
145773  testcase( pTable->nCol==BMS-2 );
145774  for(i=0; i<mxBitCol; i++){
145775    if( extraCols & MASKBIT(i) ) nKeyCol++;
145776  }
145777  if( pSrc->colUsed & MASKBIT(BMS-1) ){
145778    nKeyCol += pTable->nCol - BMS + 1;
145779  }
145780
145781  /* Construct the Index object to describe this index */
145782  pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
145783  if( pIdx==0 ) goto end_auto_index_create;
145784  pLoop->u.btree.pIndex = pIdx;
145785  pIdx->zName = "auto-index";
145786  pIdx->pTable = pTable;
145787  n = 0;
145788  idxCols = 0;
145789  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
145790    if( termCanDriveIndex(pTerm, pSrc, notReady) ){
145791      int iCol = pTerm->u.leftColumn;
145792      Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
145793      testcase( iCol==BMS-1 );
145794      testcase( iCol==BMS );
145795      if( (idxCols & cMask)==0 ){
145796        Expr *pX = pTerm->pExpr;
145797        idxCols |= cMask;
145798        pIdx->aiColumn[n] = pTerm->u.leftColumn;
145799        pColl = sqlite3ExprCompareCollSeq(pParse, pX);
145800        assert( pColl!=0 || pParse->nErr>0 ); /* TH3 collate01.800 */
145801        pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
145802        n++;
145803      }
145804    }
145805  }
145806  assert( (u32)n==pLoop->u.btree.nEq );
145807
145808  /* Add additional columns needed to make the automatic index into
145809  ** a covering index */
145810  for(i=0; i<mxBitCol; i++){
145811    if( extraCols & MASKBIT(i) ){
145812      pIdx->aiColumn[n] = i;
145813      pIdx->azColl[n] = sqlite3StrBINARY;
145814      n++;
145815    }
145816  }
145817  if( pSrc->colUsed & MASKBIT(BMS-1) ){
145818    for(i=BMS-1; i<pTable->nCol; i++){
145819      pIdx->aiColumn[n] = i;
145820      pIdx->azColl[n] = sqlite3StrBINARY;
145821      n++;
145822    }
145823  }
145824  assert( n==nKeyCol );
145825  pIdx->aiColumn[n] = XN_ROWID;
145826  pIdx->azColl[n] = sqlite3StrBINARY;
145827
145828  /* Create the automatic index */
145829  assert( pLevel->iIdxCur>=0 );
145830  pLevel->iIdxCur = pParse->nTab++;
145831  sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
145832  sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
145833  VdbeComment((v, "for %s", pTable->zName));
145834
145835  /* Fill the automatic index with content */
145836  pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
145837  if( pTabItem->fg.viaCoroutine ){
145838    int regYield = pTabItem->regReturn;
145839    addrCounter = sqlite3VdbeAddOp2(v, OP_Integer, 0, 0);
145840    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
145841    addrTop =  sqlite3VdbeAddOp1(v, OP_Yield, regYield);
145842    VdbeCoverage(v);
145843    VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
145844  }else{
145845    addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
145846  }
145847  if( pPartial ){
145848    iContinue = sqlite3VdbeMakeLabel(pParse);
145849    sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL);
145850    pLoop->wsFlags |= WHERE_PARTIALIDX;
145851  }
145852  regRecord = sqlite3GetTempReg(pParse);
145853  regBase = sqlite3GenerateIndexKey(
145854      pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
145855  );
145856  sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
145857  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
145858  if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
145859  if( pTabItem->fg.viaCoroutine ){
145860    sqlite3VdbeChangeP2(v, addrCounter, regBase+n);
145861    testcase( pParse->db->mallocFailed );
145862    assert( pLevel->iIdxCur>0 );
145863    translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
145864                          pTabItem->regResult, pLevel->iIdxCur);
145865    sqlite3VdbeGoto(v, addrTop);
145866    pTabItem->fg.viaCoroutine = 0;
145867  }else{
145868    sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
145869    sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
145870  }
145871  sqlite3VdbeJumpHere(v, addrTop);
145872  sqlite3ReleaseTempReg(pParse, regRecord);
145873
145874  /* Jump here when skipping the initialization */
145875  sqlite3VdbeJumpHere(v, addrInit);
145876
145877end_auto_index_create:
145878  sqlite3ExprDelete(pParse->db, pPartial);
145879}
145880#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
145881
145882#ifndef SQLITE_OMIT_VIRTUALTABLE
145883/*
145884** Allocate and populate an sqlite3_index_info structure. It is the
145885** responsibility of the caller to eventually release the structure
145886** by passing the pointer returned by this function to sqlite3_free().
145887*/
145888static sqlite3_index_info *allocateIndexInfo(
145889  Parse *pParse,                  /* The parsing context */
145890  WhereClause *pWC,               /* The WHERE clause being analyzed */
145891  Bitmask mUnusable,              /* Ignore terms with these prereqs */
145892  struct SrcList_item *pSrc,      /* The FROM clause term that is the vtab */
145893  ExprList *pOrderBy,             /* The ORDER BY clause */
145894  u16 *pmNoOmit                   /* Mask of terms not to omit */
145895){
145896  int i, j;
145897  int nTerm;
145898  struct sqlite3_index_constraint *pIdxCons;
145899  struct sqlite3_index_orderby *pIdxOrderBy;
145900  struct sqlite3_index_constraint_usage *pUsage;
145901  struct HiddenIndexInfo *pHidden;
145902  WhereTerm *pTerm;
145903  int nOrderBy;
145904  sqlite3_index_info *pIdxInfo;
145905  u16 mNoOmit = 0;
145906
145907  /* Count the number of possible WHERE clause constraints referring
145908  ** to this virtual table */
145909  for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
145910    if( pTerm->leftCursor != pSrc->iCursor ) continue;
145911    if( pTerm->prereqRight & mUnusable ) continue;
145912    assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
145913    testcase( pTerm->eOperator & WO_IN );
145914    testcase( pTerm->eOperator & WO_ISNULL );
145915    testcase( pTerm->eOperator & WO_IS );
145916    testcase( pTerm->eOperator & WO_ALL );
145917    if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
145918    if( pTerm->wtFlags & TERM_VNULL ) continue;
145919    assert( pTerm->u.leftColumn>=(-1) );
145920    nTerm++;
145921  }
145922
145923  /* If the ORDER BY clause contains only columns in the current
145924  ** virtual table then allocate space for the aOrderBy part of
145925  ** the sqlite3_index_info structure.
145926  */
145927  nOrderBy = 0;
145928  if( pOrderBy ){
145929    int n = pOrderBy->nExpr;
145930    for(i=0; i<n; i++){
145931      Expr *pExpr = pOrderBy->a[i].pExpr;
145932      if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
145933      if( pOrderBy->a[i].sortFlags & KEYINFO_ORDER_BIGNULL ) break;
145934    }
145935    if( i==n){
145936      nOrderBy = n;
145937    }
145938  }
145939
145940  /* Allocate the sqlite3_index_info structure
145941  */
145942  pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
145943                           + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
145944                           + sizeof(*pIdxOrderBy)*nOrderBy + sizeof(*pHidden) );
145945  if( pIdxInfo==0 ){
145946    sqlite3ErrorMsg(pParse, "out of memory");
145947    return 0;
145948  }
145949  pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1];
145950  pIdxCons = (struct sqlite3_index_constraint*)&pHidden[1];
145951  pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
145952  pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
145953  pIdxInfo->nOrderBy = nOrderBy;
145954  pIdxInfo->aConstraint = pIdxCons;
145955  pIdxInfo->aOrderBy = pIdxOrderBy;
145956  pIdxInfo->aConstraintUsage = pUsage;
145957  pHidden->pWC = pWC;
145958  pHidden->pParse = pParse;
145959  for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
145960    u16 op;
145961    if( pTerm->leftCursor != pSrc->iCursor ) continue;
145962    if( pTerm->prereqRight & mUnusable ) continue;
145963    assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
145964    testcase( pTerm->eOperator & WO_IN );
145965    testcase( pTerm->eOperator & WO_IS );
145966    testcase( pTerm->eOperator & WO_ISNULL );
145967    testcase( pTerm->eOperator & WO_ALL );
145968    if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
145969    if( pTerm->wtFlags & TERM_VNULL ) continue;
145970
145971    /* tag-20191211-002: WHERE-clause constraints are not useful to the
145972    ** right-hand table of a LEFT JOIN.  See tag-20191211-001 for the
145973    ** equivalent restriction for ordinary tables. */
145974    if( (pSrc->fg.jointype & JT_LEFT)!=0
145975     && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
145976    ){
145977      continue;
145978    }
145979    assert( pTerm->u.leftColumn>=(-1) );
145980    pIdxCons[j].iColumn = pTerm->u.leftColumn;
145981    pIdxCons[j].iTermOffset = i;
145982    op = pTerm->eOperator & WO_ALL;
145983    if( op==WO_IN ) op = WO_EQ;
145984    if( op==WO_AUX ){
145985      pIdxCons[j].op = pTerm->eMatchOp;
145986    }else if( op & (WO_ISNULL|WO_IS) ){
145987      if( op==WO_ISNULL ){
145988        pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL;
145989      }else{
145990        pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_IS;
145991      }
145992    }else{
145993      pIdxCons[j].op = (u8)op;
145994      /* The direct assignment in the previous line is possible only because
145995      ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
145996      ** following asserts verify this fact. */
145997      assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
145998      assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
145999      assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
146000      assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
146001      assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
146002      assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
146003
146004      if( op & (WO_LT|WO_LE|WO_GT|WO_GE)
146005       && sqlite3ExprIsVector(pTerm->pExpr->pRight)
146006      ){
146007        testcase( j!=i );
146008        if( j<16 ) mNoOmit |= (1 << j);
146009        if( op==WO_LT ) pIdxCons[j].op = WO_LE;
146010        if( op==WO_GT ) pIdxCons[j].op = WO_GE;
146011      }
146012    }
146013
146014    j++;
146015  }
146016  pIdxInfo->nConstraint = j;
146017  for(i=0; i<nOrderBy; i++){
146018    Expr *pExpr = pOrderBy->a[i].pExpr;
146019    pIdxOrderBy[i].iColumn = pExpr->iColumn;
146020    pIdxOrderBy[i].desc = pOrderBy->a[i].sortFlags & KEYINFO_ORDER_DESC;
146021  }
146022
146023  *pmNoOmit = mNoOmit;
146024  return pIdxInfo;
146025}
146026
146027/*
146028** The table object reference passed as the second argument to this function
146029** must represent a virtual table. This function invokes the xBestIndex()
146030** method of the virtual table with the sqlite3_index_info object that
146031** comes in as the 3rd argument to this function.
146032**
146033** If an error occurs, pParse is populated with an error message and an
146034** appropriate error code is returned.  A return of SQLITE_CONSTRAINT from
146035** xBestIndex is not considered an error.  SQLITE_CONSTRAINT indicates that
146036** the current configuration of "unusable" flags in sqlite3_index_info can
146037** not result in a valid plan.
146038**
146039** Whether or not an error is returned, it is the responsibility of the
146040** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
146041** that this is required.
146042*/
146043static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
146044  sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
146045  int rc;
146046
146047  whereTraceIndexInfoInputs(p);
146048  rc = pVtab->pModule->xBestIndex(pVtab, p);
146049  whereTraceIndexInfoOutputs(p);
146050
146051  if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT ){
146052    if( rc==SQLITE_NOMEM ){
146053      sqlite3OomFault(pParse->db);
146054    }else if( !pVtab->zErrMsg ){
146055      sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
146056    }else{
146057      sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
146058    }
146059  }
146060  sqlite3_free(pVtab->zErrMsg);
146061  pVtab->zErrMsg = 0;
146062  return rc;
146063}
146064#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
146065
146066#ifdef SQLITE_ENABLE_STAT4
146067/*
146068** Estimate the location of a particular key among all keys in an
146069** index.  Store the results in aStat as follows:
146070**
146071**    aStat[0]      Est. number of rows less than pRec
146072**    aStat[1]      Est. number of rows equal to pRec
146073**
146074** Return the index of the sample that is the smallest sample that
146075** is greater than or equal to pRec. Note that this index is not an index
146076** into the aSample[] array - it is an index into a virtual set of samples
146077** based on the contents of aSample[] and the number of fields in record
146078** pRec.
146079*/
146080static int whereKeyStats(
146081  Parse *pParse,              /* Database connection */
146082  Index *pIdx,                /* Index to consider domain of */
146083  UnpackedRecord *pRec,       /* Vector of values to consider */
146084  int roundUp,                /* Round up if true.  Round down if false */
146085  tRowcnt *aStat              /* OUT: stats written here */
146086){
146087  IndexSample *aSample = pIdx->aSample;
146088  int iCol;                   /* Index of required stats in anEq[] etc. */
146089  int i;                      /* Index of first sample >= pRec */
146090  int iSample;                /* Smallest sample larger than or equal to pRec */
146091  int iMin = 0;               /* Smallest sample not yet tested */
146092  int iTest;                  /* Next sample to test */
146093  int res;                    /* Result of comparison operation */
146094  int nField;                 /* Number of fields in pRec */
146095  tRowcnt iLower = 0;         /* anLt[] + anEq[] of largest sample pRec is > */
146096
146097#ifndef SQLITE_DEBUG
146098  UNUSED_PARAMETER( pParse );
146099#endif
146100  assert( pRec!=0 );
146101  assert( pIdx->nSample>0 );
146102  assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol );
146103
146104  /* Do a binary search to find the first sample greater than or equal
146105  ** to pRec. If pRec contains a single field, the set of samples to search
146106  ** is simply the aSample[] array. If the samples in aSample[] contain more
146107  ** than one fields, all fields following the first are ignored.
146108  **
146109  ** If pRec contains N fields, where N is more than one, then as well as the
146110  ** samples in aSample[] (truncated to N fields), the search also has to
146111  ** consider prefixes of those samples. For example, if the set of samples
146112  ** in aSample is:
146113  **
146114  **     aSample[0] = (a, 5)
146115  **     aSample[1] = (a, 10)
146116  **     aSample[2] = (b, 5)
146117  **     aSample[3] = (c, 100)
146118  **     aSample[4] = (c, 105)
146119  **
146120  ** Then the search space should ideally be the samples above and the
146121  ** unique prefixes [a], [b] and [c]. But since that is hard to organize,
146122  ** the code actually searches this set:
146123  **
146124  **     0: (a)
146125  **     1: (a, 5)
146126  **     2: (a, 10)
146127  **     3: (a, 10)
146128  **     4: (b)
146129  **     5: (b, 5)
146130  **     6: (c)
146131  **     7: (c, 100)
146132  **     8: (c, 105)
146133  **     9: (c, 105)
146134  **
146135  ** For each sample in the aSample[] array, N samples are present in the
146136  ** effective sample array. In the above, samples 0 and 1 are based on
146137  ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc.
146138  **
146139  ** Often, sample i of each block of N effective samples has (i+1) fields.
146140  ** Except, each sample may be extended to ensure that it is greater than or
146141  ** equal to the previous sample in the array. For example, in the above,
146142  ** sample 2 is the first sample of a block of N samples, so at first it
146143  ** appears that it should be 1 field in size. However, that would make it
146144  ** smaller than sample 1, so the binary search would not work. As a result,
146145  ** it is extended to two fields. The duplicates that this creates do not
146146  ** cause any problems.
146147  */
146148  nField = pRec->nField;
146149  iCol = 0;
146150  iSample = pIdx->nSample * nField;
146151  do{
146152    int iSamp;                    /* Index in aSample[] of test sample */
146153    int n;                        /* Number of fields in test sample */
146154
146155    iTest = (iMin+iSample)/2;
146156    iSamp = iTest / nField;
146157    if( iSamp>0 ){
146158      /* The proposed effective sample is a prefix of sample aSample[iSamp].
146159      ** Specifically, the shortest prefix of at least (1 + iTest%nField)
146160      ** fields that is greater than the previous effective sample.  */
146161      for(n=(iTest % nField) + 1; n<nField; n++){
146162        if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
146163      }
146164    }else{
146165      n = iTest + 1;
146166    }
146167
146168    pRec->nField = n;
146169    res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec);
146170    if( res<0 ){
146171      iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
146172      iMin = iTest+1;
146173    }else if( res==0 && n<nField ){
146174      iLower = aSample[iSamp].anLt[n-1];
146175      iMin = iTest+1;
146176      res = -1;
146177    }else{
146178      iSample = iTest;
146179      iCol = n-1;
146180    }
146181  }while( res && iMin<iSample );
146182  i = iSample / nField;
146183
146184#ifdef SQLITE_DEBUG
146185  /* The following assert statements check that the binary search code
146186  ** above found the right answer. This block serves no purpose other
146187  ** than to invoke the asserts.  */
146188  if( pParse->db->mallocFailed==0 ){
146189    if( res==0 ){
146190      /* If (res==0) is true, then pRec must be equal to sample i. */
146191      assert( i<pIdx->nSample );
146192      assert( iCol==nField-1 );
146193      pRec->nField = nField;
146194      assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
146195           || pParse->db->mallocFailed
146196      );
146197    }else{
146198      /* Unless i==pIdx->nSample, indicating that pRec is larger than
146199      ** all samples in the aSample[] array, pRec must be smaller than the
146200      ** (iCol+1) field prefix of sample i.  */
146201      assert( i<=pIdx->nSample && i>=0 );
146202      pRec->nField = iCol+1;
146203      assert( i==pIdx->nSample
146204           || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
146205           || pParse->db->mallocFailed );
146206
146207      /* if i==0 and iCol==0, then record pRec is smaller than all samples
146208      ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must
146209      ** be greater than or equal to the (iCol) field prefix of sample i.
146210      ** If (i>0), then pRec must also be greater than sample (i-1).  */
146211      if( iCol>0 ){
146212        pRec->nField = iCol;
146213        assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0
146214             || pParse->db->mallocFailed );
146215      }
146216      if( i>0 ){
146217        pRec->nField = nField;
146218        assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
146219             || pParse->db->mallocFailed );
146220      }
146221    }
146222  }
146223#endif /* ifdef SQLITE_DEBUG */
146224
146225  if( res==0 ){
146226    /* Record pRec is equal to sample i */
146227    assert( iCol==nField-1 );
146228    aStat[0] = aSample[i].anLt[iCol];
146229    aStat[1] = aSample[i].anEq[iCol];
146230  }else{
146231    /* At this point, the (iCol+1) field prefix of aSample[i] is the first
146232    ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
146233    ** is larger than all samples in the array. */
146234    tRowcnt iUpper, iGap;
146235    if( i>=pIdx->nSample ){
146236      iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
146237    }else{
146238      iUpper = aSample[i].anLt[iCol];
146239    }
146240
146241    if( iLower>=iUpper ){
146242      iGap = 0;
146243    }else{
146244      iGap = iUpper - iLower;
146245    }
146246    if( roundUp ){
146247      iGap = (iGap*2)/3;
146248    }else{
146249      iGap = iGap/3;
146250    }
146251    aStat[0] = iLower + iGap;
146252    aStat[1] = pIdx->aAvgEq[nField-1];
146253  }
146254
146255  /* Restore the pRec->nField value before returning.  */
146256  pRec->nField = nField;
146257  return i;
146258}
146259#endif /* SQLITE_ENABLE_STAT4 */
146260
146261/*
146262** If it is not NULL, pTerm is a term that provides an upper or lower
146263** bound on a range scan. Without considering pTerm, it is estimated
146264** that the scan will visit nNew rows. This function returns the number
146265** estimated to be visited after taking pTerm into account.
146266**
146267** If the user explicitly specified a likelihood() value for this term,
146268** then the return value is the likelihood multiplied by the number of
146269** input rows. Otherwise, this function assumes that an "IS NOT NULL" term
146270** has a likelihood of 0.50, and any other term a likelihood of 0.25.
146271*/
146272static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
146273  LogEst nRet = nNew;
146274  if( pTerm ){
146275    if( pTerm->truthProb<=0 ){
146276      nRet += pTerm->truthProb;
146277    }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
146278      nRet -= 20;        assert( 20==sqlite3LogEst(4) );
146279    }
146280  }
146281  return nRet;
146282}
146283
146284
146285#ifdef SQLITE_ENABLE_STAT4
146286/*
146287** Return the affinity for a single column of an index.
146288*/
146289SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){
146290  assert( iCol>=0 && iCol<pIdx->nColumn );
146291  if( !pIdx->zColAff ){
146292    if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB;
146293  }
146294  assert( pIdx->zColAff[iCol]!=0 );
146295  return pIdx->zColAff[iCol];
146296}
146297#endif
146298
146299
146300#ifdef SQLITE_ENABLE_STAT4
146301/*
146302** This function is called to estimate the number of rows visited by a
146303** range-scan on a skip-scan index. For example:
146304**
146305**   CREATE INDEX i1 ON t1(a, b, c);
146306**   SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
146307**
146308** Value pLoop->nOut is currently set to the estimated number of rows
146309** visited for scanning (a=? AND b=?). This function reduces that estimate
146310** by some factor to account for the (c BETWEEN ? AND ?) expression based
146311** on the stat4 data for the index. this scan will be peformed multiple
146312** times (once for each (a,b) combination that matches a=?) is dealt with
146313** by the caller.
146314**
146315** It does this by scanning through all stat4 samples, comparing values
146316** extracted from pLower and pUpper with the corresponding column in each
146317** sample. If L and U are the number of samples found to be less than or
146318** equal to the values extracted from pLower and pUpper respectively, and
146319** N is the total number of samples, the pLoop->nOut value is adjusted
146320** as follows:
146321**
146322**   nOut = nOut * ( min(U - L, 1) / N )
146323**
146324** If pLower is NULL, or a value cannot be extracted from the term, L is
146325** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
146326** U is set to N.
146327**
146328** Normally, this function sets *pbDone to 1 before returning. However,
146329** if no value can be extracted from either pLower or pUpper (and so the
146330** estimate of the number of rows delivered remains unchanged), *pbDone
146331** is left as is.
146332**
146333** If an error occurs, an SQLite error code is returned. Otherwise,
146334** SQLITE_OK.
146335*/
146336static int whereRangeSkipScanEst(
146337  Parse *pParse,       /* Parsing & code generating context */
146338  WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
146339  WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
146340  WhereLoop *pLoop,    /* Update the .nOut value of this loop */
146341  int *pbDone          /* Set to true if at least one expr. value extracted */
146342){
146343  Index *p = pLoop->u.btree.pIndex;
146344  int nEq = pLoop->u.btree.nEq;
146345  sqlite3 *db = pParse->db;
146346  int nLower = -1;
146347  int nUpper = p->nSample+1;
146348  int rc = SQLITE_OK;
146349  u8 aff = sqlite3IndexColumnAffinity(db, p, nEq);
146350  CollSeq *pColl;
146351
146352  sqlite3_value *p1 = 0;          /* Value extracted from pLower */
146353  sqlite3_value *p2 = 0;          /* Value extracted from pUpper */
146354  sqlite3_value *pVal = 0;        /* Value extracted from record */
146355
146356  pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
146357  if( pLower ){
146358    rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
146359    nLower = 0;
146360  }
146361  if( pUpper && rc==SQLITE_OK ){
146362    rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
146363    nUpper = p2 ? 0 : p->nSample;
146364  }
146365
146366  if( p1 || p2 ){
146367    int i;
146368    int nDiff;
146369    for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
146370      rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
146371      if( rc==SQLITE_OK && p1 ){
146372        int res = sqlite3MemCompare(p1, pVal, pColl);
146373        if( res>=0 ) nLower++;
146374      }
146375      if( rc==SQLITE_OK && p2 ){
146376        int res = sqlite3MemCompare(p2, pVal, pColl);
146377        if( res>=0 ) nUpper++;
146378      }
146379    }
146380    nDiff = (nUpper - nLower);
146381    if( nDiff<=0 ) nDiff = 1;
146382
146383    /* If there is both an upper and lower bound specified, and the
146384    ** comparisons indicate that they are close together, use the fallback
146385    ** method (assume that the scan visits 1/64 of the rows) for estimating
146386    ** the number of rows visited. Otherwise, estimate the number of rows
146387    ** using the method described in the header comment for this function. */
146388    if( nDiff!=1 || pUpper==0 || pLower==0 ){
146389      int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
146390      pLoop->nOut -= nAdjust;
146391      *pbDone = 1;
146392      WHERETRACE(0x10, ("range skip-scan regions: %u..%u  adjust=%d est=%d\n",
146393                           nLower, nUpper, nAdjust*-1, pLoop->nOut));
146394    }
146395
146396  }else{
146397    assert( *pbDone==0 );
146398  }
146399
146400  sqlite3ValueFree(p1);
146401  sqlite3ValueFree(p2);
146402  sqlite3ValueFree(pVal);
146403
146404  return rc;
146405}
146406#endif /* SQLITE_ENABLE_STAT4 */
146407
146408/*
146409** This function is used to estimate the number of rows that will be visited
146410** by scanning an index for a range of values. The range may have an upper
146411** bound, a lower bound, or both. The WHERE clause terms that set the upper
146412** and lower bounds are represented by pLower and pUpper respectively. For
146413** example, assuming that index p is on t1(a):
146414**
146415**   ... FROM t1 WHERE a > ? AND a < ? ...
146416**                    |_____|   |_____|
146417**                       |         |
146418**                     pLower    pUpper
146419**
146420** If either of the upper or lower bound is not present, then NULL is passed in
146421** place of the corresponding WhereTerm.
146422**
146423** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
146424** column subject to the range constraint. Or, equivalently, the number of
146425** equality constraints optimized by the proposed index scan. For example,
146426** assuming index p is on t1(a, b), and the SQL query is:
146427**
146428**   ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
146429**
146430** then nEq is set to 1 (as the range restricted column, b, is the second
146431** left-most column of the index). Or, if the query is:
146432**
146433**   ... FROM t1 WHERE a > ? AND a < ? ...
146434**
146435** then nEq is set to 0.
146436**
146437** When this function is called, *pnOut is set to the sqlite3LogEst() of the
146438** number of rows that the index scan is expected to visit without
146439** considering the range constraints. If nEq is 0, then *pnOut is the number of
146440** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
146441** to account for the range constraints pLower and pUpper.
146442**
146443** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
146444** used, a single range inequality reduces the search space by a factor of 4.
146445** and a pair of constraints (x>? AND x<?) reduces the expected number of
146446** rows visited by a factor of 64.
146447*/
146448static int whereRangeScanEst(
146449  Parse *pParse,       /* Parsing & code generating context */
146450  WhereLoopBuilder *pBuilder,
146451  WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
146452  WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
146453  WhereLoop *pLoop     /* Modify the .nOut and maybe .rRun fields */
146454){
146455  int rc = SQLITE_OK;
146456  int nOut = pLoop->nOut;
146457  LogEst nNew;
146458
146459#ifdef SQLITE_ENABLE_STAT4
146460  Index *p = pLoop->u.btree.pIndex;
146461  int nEq = pLoop->u.btree.nEq;
146462
146463  if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)
146464   && OptimizationEnabled(pParse->db, SQLITE_Stat4)
146465  ){
146466    if( nEq==pBuilder->nRecValid ){
146467      UnpackedRecord *pRec = pBuilder->pRec;
146468      tRowcnt a[2];
146469      int nBtm = pLoop->u.btree.nBtm;
146470      int nTop = pLoop->u.btree.nTop;
146471
146472      /* Variable iLower will be set to the estimate of the number of rows in
146473      ** the index that are less than the lower bound of the range query. The
146474      ** lower bound being the concatenation of $P and $L, where $P is the
146475      ** key-prefix formed by the nEq values matched against the nEq left-most
146476      ** columns of the index, and $L is the value in pLower.
146477      **
146478      ** Or, if pLower is NULL or $L cannot be extracted from it (because it
146479      ** is not a simple variable or literal value), the lower bound of the
146480      ** range is $P. Due to a quirk in the way whereKeyStats() works, even
146481      ** if $L is available, whereKeyStats() is called for both ($P) and
146482      ** ($P:$L) and the larger of the two returned values is used.
146483      **
146484      ** Similarly, iUpper is to be set to the estimate of the number of rows
146485      ** less than the upper bound of the range query. Where the upper bound
146486      ** is either ($P) or ($P:$U). Again, even if $U is available, both values
146487      ** of iUpper are requested of whereKeyStats() and the smaller used.
146488      **
146489      ** The number of rows between the two bounds is then just iUpper-iLower.
146490      */
146491      tRowcnt iLower;     /* Rows less than the lower bound */
146492      tRowcnt iUpper;     /* Rows less than the upper bound */
146493      int iLwrIdx = -2;   /* aSample[] for the lower bound */
146494      int iUprIdx = -1;   /* aSample[] for the upper bound */
146495
146496      if( pRec ){
146497        testcase( pRec->nField!=pBuilder->nRecValid );
146498        pRec->nField = pBuilder->nRecValid;
146499      }
146500      /* Determine iLower and iUpper using ($P) only. */
146501      if( nEq==0 ){
146502        iLower = 0;
146503        iUpper = p->nRowEst0;
146504      }else{
146505        /* Note: this call could be optimized away - since the same values must
146506        ** have been requested when testing key $P in whereEqualScanEst().  */
146507        whereKeyStats(pParse, p, pRec, 0, a);
146508        iLower = a[0];
146509        iUpper = a[0] + a[1];
146510      }
146511
146512      assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
146513      assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
146514      assert( p->aSortOrder!=0 );
146515      if( p->aSortOrder[nEq] ){
146516        /* The roles of pLower and pUpper are swapped for a DESC index */
146517        SWAP(WhereTerm*, pLower, pUpper);
146518        SWAP(int, nBtm, nTop);
146519      }
146520
146521      /* If possible, improve on the iLower estimate using ($P:$L). */
146522      if( pLower ){
146523        int n;                    /* Values extracted from pExpr */
146524        Expr *pExpr = pLower->pExpr->pRight;
146525        rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nBtm, nEq, &n);
146526        if( rc==SQLITE_OK && n ){
146527          tRowcnt iNew;
146528          u16 mask = WO_GT|WO_LE;
146529          if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT);
146530          iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
146531          iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
146532          if( iNew>iLower ) iLower = iNew;
146533          nOut--;
146534          pLower = 0;
146535        }
146536      }
146537
146538      /* If possible, improve on the iUpper estimate using ($P:$U). */
146539      if( pUpper ){
146540        int n;                    /* Values extracted from pExpr */
146541        Expr *pExpr = pUpper->pExpr->pRight;
146542        rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nTop, nEq, &n);
146543        if( rc==SQLITE_OK && n ){
146544          tRowcnt iNew;
146545          u16 mask = WO_GT|WO_LE;
146546          if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT);
146547          iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
146548          iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
146549          if( iNew<iUpper ) iUpper = iNew;
146550          nOut--;
146551          pUpper = 0;
146552        }
146553      }
146554
146555      pBuilder->pRec = pRec;
146556      if( rc==SQLITE_OK ){
146557        if( iUpper>iLower ){
146558          nNew = sqlite3LogEst(iUpper - iLower);
146559          /* TUNING:  If both iUpper and iLower are derived from the same
146560          ** sample, then assume they are 4x more selective.  This brings
146561          ** the estimated selectivity more in line with what it would be
146562          ** if estimated without the use of STAT4 tables. */
146563          if( iLwrIdx==iUprIdx ) nNew -= 20;  assert( 20==sqlite3LogEst(4) );
146564        }else{
146565          nNew = 10;        assert( 10==sqlite3LogEst(2) );
146566        }
146567        if( nNew<nOut ){
146568          nOut = nNew;
146569        }
146570        WHERETRACE(0x10, ("STAT4 range scan: %u..%u  est=%d\n",
146571                           (u32)iLower, (u32)iUpper, nOut));
146572      }
146573    }else{
146574      int bDone = 0;
146575      rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
146576      if( bDone ) return rc;
146577    }
146578  }
146579#else
146580  UNUSED_PARAMETER(pParse);
146581  UNUSED_PARAMETER(pBuilder);
146582  assert( pLower || pUpper );
146583#endif
146584  assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
146585  nNew = whereRangeAdjust(pLower, nOut);
146586  nNew = whereRangeAdjust(pUpper, nNew);
146587
146588  /* TUNING: If there is both an upper and lower limit and neither limit
146589  ** has an application-defined likelihood(), assume the range is
146590  ** reduced by an additional 75%. This means that, by default, an open-ended
146591  ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
146592  ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
146593  ** match 1/64 of the index. */
146594  if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
146595    nNew -= 20;
146596  }
146597
146598  nOut -= (pLower!=0) + (pUpper!=0);
146599  if( nNew<10 ) nNew = 10;
146600  if( nNew<nOut ) nOut = nNew;
146601#if defined(WHERETRACE_ENABLED)
146602  if( pLoop->nOut>nOut ){
146603    WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n",
146604                    pLoop->nOut, nOut));
146605  }
146606#endif
146607  pLoop->nOut = (LogEst)nOut;
146608  return rc;
146609}
146610
146611#ifdef SQLITE_ENABLE_STAT4
146612/*
146613** Estimate the number of rows that will be returned based on
146614** an equality constraint x=VALUE and where that VALUE occurs in
146615** the histogram data.  This only works when x is the left-most
146616** column of an index and sqlite_stat4 histogram data is available
146617** for that index.  When pExpr==NULL that means the constraint is
146618** "x IS NULL" instead of "x=VALUE".
146619**
146620** Write the estimated row count into *pnRow and return SQLITE_OK.
146621** If unable to make an estimate, leave *pnRow unchanged and return
146622** non-zero.
146623**
146624** This routine can fail if it is unable to load a collating sequence
146625** required for string comparison, or if unable to allocate memory
146626** for a UTF conversion required for comparison.  The error is stored
146627** in the pParse structure.
146628*/
146629static int whereEqualScanEst(
146630  Parse *pParse,       /* Parsing & code generating context */
146631  WhereLoopBuilder *pBuilder,
146632  Expr *pExpr,         /* Expression for VALUE in the x=VALUE constraint */
146633  tRowcnt *pnRow       /* Write the revised row estimate here */
146634){
146635  Index *p = pBuilder->pNew->u.btree.pIndex;
146636  int nEq = pBuilder->pNew->u.btree.nEq;
146637  UnpackedRecord *pRec = pBuilder->pRec;
146638  int rc;                   /* Subfunction return code */
146639  tRowcnt a[2];             /* Statistics */
146640  int bOk;
146641
146642  assert( nEq>=1 );
146643  assert( nEq<=p->nColumn );
146644  assert( p->aSample!=0 );
146645  assert( p->nSample>0 );
146646  assert( pBuilder->nRecValid<nEq );
146647
146648  /* If values are not available for all fields of the index to the left
146649  ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
146650  if( pBuilder->nRecValid<(nEq-1) ){
146651    return SQLITE_NOTFOUND;
146652  }
146653
146654  /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
146655  ** below would return the same value.  */
146656  if( nEq>=p->nColumn ){
146657    *pnRow = 1;
146658    return SQLITE_OK;
146659  }
146660
146661  rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
146662  pBuilder->pRec = pRec;
146663  if( rc!=SQLITE_OK ) return rc;
146664  if( bOk==0 ) return SQLITE_NOTFOUND;
146665  pBuilder->nRecValid = nEq;
146666
146667  whereKeyStats(pParse, p, pRec, 0, a);
146668  WHERETRACE(0x10,("equality scan regions %s(%d): %d\n",
146669                   p->zName, nEq-1, (int)a[1]));
146670  *pnRow = a[1];
146671
146672  return rc;
146673}
146674#endif /* SQLITE_ENABLE_STAT4 */
146675
146676#ifdef SQLITE_ENABLE_STAT4
146677/*
146678** Estimate the number of rows that will be returned based on
146679** an IN constraint where the right-hand side of the IN operator
146680** is a list of values.  Example:
146681**
146682**        WHERE x IN (1,2,3,4)
146683**
146684** Write the estimated row count into *pnRow and return SQLITE_OK.
146685** If unable to make an estimate, leave *pnRow unchanged and return
146686** non-zero.
146687**
146688** This routine can fail if it is unable to load a collating sequence
146689** required for string comparison, or if unable to allocate memory
146690** for a UTF conversion required for comparison.  The error is stored
146691** in the pParse structure.
146692*/
146693static int whereInScanEst(
146694  Parse *pParse,       /* Parsing & code generating context */
146695  WhereLoopBuilder *pBuilder,
146696  ExprList *pList,     /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
146697  tRowcnt *pnRow       /* Write the revised row estimate here */
146698){
146699  Index *p = pBuilder->pNew->u.btree.pIndex;
146700  i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
146701  int nRecValid = pBuilder->nRecValid;
146702  int rc = SQLITE_OK;     /* Subfunction return code */
146703  tRowcnt nEst;           /* Number of rows for a single term */
146704  tRowcnt nRowEst = 0;    /* New estimate of the number of rows */
146705  int i;                  /* Loop counter */
146706
146707  assert( p->aSample!=0 );
146708  for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
146709    nEst = nRow0;
146710    rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
146711    nRowEst += nEst;
146712    pBuilder->nRecValid = nRecValid;
146713  }
146714
146715  if( rc==SQLITE_OK ){
146716    if( nRowEst > nRow0 ) nRowEst = nRow0;
146717    *pnRow = nRowEst;
146718    WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
146719  }
146720  assert( pBuilder->nRecValid==nRecValid );
146721  return rc;
146722}
146723#endif /* SQLITE_ENABLE_STAT4 */
146724
146725
146726#ifdef WHERETRACE_ENABLED
146727/*
146728** Print the content of a WhereTerm object
146729*/
146730SQLITE_PRIVATE void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm){
146731  if( pTerm==0 ){
146732    sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
146733  }else{
146734    char zType[8];
146735    char zLeft[50];
146736    memcpy(zType, "....", 5);
146737    if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
146738    if( pTerm->eOperator & WO_EQUIV  ) zType[1] = 'E';
146739    if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
146740    if( pTerm->wtFlags & TERM_CODED  ) zType[3] = 'C';
146741    if( pTerm->eOperator & WO_SINGLE ){
146742      sqlite3_snprintf(sizeof(zLeft),zLeft,"left={%d:%d}",
146743                       pTerm->leftCursor, pTerm->u.leftColumn);
146744    }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
146745      sqlite3_snprintf(sizeof(zLeft),zLeft,"indexable=0x%lld",
146746                       pTerm->u.pOrInfo->indexable);
146747    }else{
146748      sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
146749    }
146750    sqlite3DebugPrintf(
146751       "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x",
146752       iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags);
146753    /* The 0x10000 .wheretrace flag causes extra information to be
146754    ** shown about each Term */
146755    if( sqlite3WhereTrace & 0x10000 ){
146756      sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx",
146757        pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight);
146758    }
146759    if( pTerm->iField ){
146760      sqlite3DebugPrintf(" iField=%d", pTerm->iField);
146761    }
146762    if( pTerm->iParent>=0 ){
146763      sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
146764    }
146765    sqlite3DebugPrintf("\n");
146766    sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
146767  }
146768}
146769#endif
146770
146771#ifdef WHERETRACE_ENABLED
146772/*
146773** Show the complete content of a WhereClause
146774*/
146775SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC){
146776  int i;
146777  for(i=0; i<pWC->nTerm; i++){
146778    sqlite3WhereTermPrint(&pWC->a[i], i);
146779  }
146780}
146781#endif
146782
146783#ifdef WHERETRACE_ENABLED
146784/*
146785** Print a WhereLoop object for debugging purposes
146786*/
146787SQLITE_PRIVATE void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC){
146788  WhereInfo *pWInfo = pWC->pWInfo;
146789  int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
146790  struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab;
146791  Table *pTab = pItem->pTab;
146792  Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
146793  sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
146794                     p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
146795  sqlite3DebugPrintf(" %12s",
146796                     pItem->zAlias ? pItem->zAlias : pTab->zName);
146797  if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
146798    const char *zName;
146799    if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
146800      if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
146801        int i = sqlite3Strlen30(zName) - 1;
146802        while( zName[i]!='_' ) i--;
146803        zName += i;
146804      }
146805      sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
146806    }else{
146807      sqlite3DebugPrintf("%20s","");
146808    }
146809  }else{
146810    char *z;
146811    if( p->u.vtab.idxStr ){
146812      z = sqlite3_mprintf("(%d,\"%s\",%#x)",
146813                p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
146814    }else{
146815      z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
146816    }
146817    sqlite3DebugPrintf(" %-19s", z);
146818    sqlite3_free(z);
146819  }
146820  if( p->wsFlags & WHERE_SKIPSCAN ){
146821    sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
146822  }else{
146823    sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
146824  }
146825  sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
146826  if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
146827    int i;
146828    for(i=0; i<p->nLTerm; i++){
146829      sqlite3WhereTermPrint(p->aLTerm[i], i);
146830    }
146831  }
146832}
146833#endif
146834
146835/*
146836** Convert bulk memory into a valid WhereLoop that can be passed
146837** to whereLoopClear harmlessly.
146838*/
146839static void whereLoopInit(WhereLoop *p){
146840  p->aLTerm = p->aLTermSpace;
146841  p->nLTerm = 0;
146842  p->nLSlot = ArraySize(p->aLTermSpace);
146843  p->wsFlags = 0;
146844}
146845
146846/*
146847** Clear the WhereLoop.u union.  Leave WhereLoop.pLTerm intact.
146848*/
146849static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
146850  if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
146851    if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
146852      sqlite3_free(p->u.vtab.idxStr);
146853      p->u.vtab.needFree = 0;
146854      p->u.vtab.idxStr = 0;
146855    }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
146856      sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
146857      sqlite3DbFreeNN(db, p->u.btree.pIndex);
146858      p->u.btree.pIndex = 0;
146859    }
146860  }
146861}
146862
146863/*
146864** Deallocate internal memory used by a WhereLoop object
146865*/
146866static void whereLoopClear(sqlite3 *db, WhereLoop *p){
146867  if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
146868  whereLoopClearUnion(db, p);
146869  whereLoopInit(p);
146870}
146871
146872/*
146873** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
146874*/
146875static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
146876  WhereTerm **paNew;
146877  if( p->nLSlot>=n ) return SQLITE_OK;
146878  n = (n+7)&~7;
146879  paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
146880  if( paNew==0 ) return SQLITE_NOMEM_BKPT;
146881  memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
146882  if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
146883  p->aLTerm = paNew;
146884  p->nLSlot = n;
146885  return SQLITE_OK;
146886}
146887
146888/*
146889** Transfer content from the second pLoop into the first.
146890*/
146891static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
146892  whereLoopClearUnion(db, pTo);
146893  if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
146894    memset(&pTo->u, 0, sizeof(pTo->u));
146895    return SQLITE_NOMEM_BKPT;
146896  }
146897  memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
146898  memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
146899  if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
146900    pFrom->u.vtab.needFree = 0;
146901  }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
146902    pFrom->u.btree.pIndex = 0;
146903  }
146904  return SQLITE_OK;
146905}
146906
146907/*
146908** Delete a WhereLoop object
146909*/
146910static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
146911  whereLoopClear(db, p);
146912  sqlite3DbFreeNN(db, p);
146913}
146914
146915/*
146916** Free a WhereInfo structure
146917*/
146918static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
146919  int i;
146920  assert( pWInfo!=0 );
146921  for(i=0; i<pWInfo->nLevel; i++){
146922    WhereLevel *pLevel = &pWInfo->a[i];
146923    if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
146924      sqlite3DbFree(db, pLevel->u.in.aInLoop);
146925    }
146926  }
146927  sqlite3WhereClauseClear(&pWInfo->sWC);
146928  while( pWInfo->pLoops ){
146929    WhereLoop *p = pWInfo->pLoops;
146930    pWInfo->pLoops = p->pNextLoop;
146931    whereLoopDelete(db, p);
146932  }
146933  assert( pWInfo->pExprMods==0 );
146934  sqlite3DbFreeNN(db, pWInfo);
146935}
146936
146937/*
146938** Return TRUE if all of the following are true:
146939**
146940**   (1)  X has the same or lower cost that Y
146941**   (2)  X uses fewer WHERE clause terms than Y
146942**   (3)  Every WHERE clause term used by X is also used by Y
146943**   (4)  X skips at least as many columns as Y
146944**   (5)  If X is a covering index, than Y is too
146945**
146946** Conditions (2) and (3) mean that X is a "proper subset" of Y.
146947** If X is a proper subset of Y then Y is a better choice and ought
146948** to have a lower cost.  This routine returns TRUE when that cost
146949** relationship is inverted and needs to be adjusted.  Constraint (4)
146950** was added because if X uses skip-scan less than Y it still might
146951** deserve a lower cost even if it is a proper subset of Y.  Constraint (5)
146952** was added because a covering index probably deserves to have a lower cost
146953** than a non-covering index even if it is a proper subset.
146954*/
146955static int whereLoopCheaperProperSubset(
146956  const WhereLoop *pX,       /* First WhereLoop to compare */
146957  const WhereLoop *pY        /* Compare against this WhereLoop */
146958){
146959  int i, j;
146960  if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
146961    return 0; /* X is not a subset of Y */
146962  }
146963  if( pY->nSkip > pX->nSkip ) return 0;
146964  if( pX->rRun >= pY->rRun ){
146965    if( pX->rRun > pY->rRun ) return 0;    /* X costs more than Y */
146966    if( pX->nOut > pY->nOut ) return 0;    /* X costs more than Y */
146967  }
146968  for(i=pX->nLTerm-1; i>=0; i--){
146969    if( pX->aLTerm[i]==0 ) continue;
146970    for(j=pY->nLTerm-1; j>=0; j--){
146971      if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
146972    }
146973    if( j<0 ) return 0;  /* X not a subset of Y since term X[i] not used by Y */
146974  }
146975  if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
146976   && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
146977    return 0;  /* Constraint (5) */
146978  }
146979  return 1;  /* All conditions meet */
146980}
146981
146982/*
146983** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so
146984** that:
146985**
146986**   (1) pTemplate costs less than any other WhereLoops that are a proper
146987**       subset of pTemplate
146988**
146989**   (2) pTemplate costs more than any other WhereLoops for which pTemplate
146990**       is a proper subset.
146991**
146992** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
146993** WHERE clause terms than Y and that every WHERE clause term used by X is
146994** also used by Y.
146995*/
146996static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
146997  if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
146998  for(; p; p=p->pNextLoop){
146999    if( p->iTab!=pTemplate->iTab ) continue;
147000    if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
147001    if( whereLoopCheaperProperSubset(p, pTemplate) ){
147002      /* Adjust pTemplate cost downward so that it is cheaper than its
147003      ** subset p. */
147004      WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
147005                       pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1));
147006      pTemplate->rRun = p->rRun;
147007      pTemplate->nOut = p->nOut - 1;
147008    }else if( whereLoopCheaperProperSubset(pTemplate, p) ){
147009      /* Adjust pTemplate cost upward so that it is costlier than p since
147010      ** pTemplate is a proper subset of p */
147011      WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
147012                       pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1));
147013      pTemplate->rRun = p->rRun;
147014      pTemplate->nOut = p->nOut + 1;
147015    }
147016  }
147017}
147018
147019/*
147020** Search the list of WhereLoops in *ppPrev looking for one that can be
147021** replaced by pTemplate.
147022**
147023** Return NULL if pTemplate does not belong on the WhereLoop list.
147024** In other words if pTemplate ought to be dropped from further consideration.
147025**
147026** If pX is a WhereLoop that pTemplate can replace, then return the
147027** link that points to pX.
147028**
147029** If pTemplate cannot replace any existing element of the list but needs
147030** to be added to the list as a new entry, then return a pointer to the
147031** tail of the list.
147032*/
147033static WhereLoop **whereLoopFindLesser(
147034  WhereLoop **ppPrev,
147035  const WhereLoop *pTemplate
147036){
147037  WhereLoop *p;
147038  for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
147039    if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
147040      /* If either the iTab or iSortIdx values for two WhereLoop are different
147041      ** then those WhereLoops need to be considered separately.  Neither is
147042      ** a candidate to replace the other. */
147043      continue;
147044    }
147045    /* In the current implementation, the rSetup value is either zero
147046    ** or the cost of building an automatic index (NlogN) and the NlogN
147047    ** is the same for compatible WhereLoops. */
147048    assert( p->rSetup==0 || pTemplate->rSetup==0
147049                 || p->rSetup==pTemplate->rSetup );
147050
147051    /* whereLoopAddBtree() always generates and inserts the automatic index
147052    ** case first.  Hence compatible candidate WhereLoops never have a larger
147053    ** rSetup. Call this SETUP-INVARIANT */
147054    assert( p->rSetup>=pTemplate->rSetup );
147055
147056    /* Any loop using an appliation-defined index (or PRIMARY KEY or
147057    ** UNIQUE constraint) with one or more == constraints is better
147058    ** than an automatic index. Unless it is a skip-scan. */
147059    if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
147060     && (pTemplate->nSkip)==0
147061     && (pTemplate->wsFlags & WHERE_INDEXED)!=0
147062     && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
147063     && (p->prereq & pTemplate->prereq)==pTemplate->prereq
147064    ){
147065      break;
147066    }
147067
147068    /* If existing WhereLoop p is better than pTemplate, pTemplate can be
147069    ** discarded.  WhereLoop p is better if:
147070    **   (1)  p has no more dependencies than pTemplate, and
147071    **   (2)  p has an equal or lower cost than pTemplate
147072    */
147073    if( (p->prereq & pTemplate->prereq)==p->prereq    /* (1)  */
147074     && p->rSetup<=pTemplate->rSetup                  /* (2a) */
147075     && p->rRun<=pTemplate->rRun                      /* (2b) */
147076     && p->nOut<=pTemplate->nOut                      /* (2c) */
147077    ){
147078      return 0;  /* Discard pTemplate */
147079    }
147080
147081    /* If pTemplate is always better than p, then cause p to be overwritten
147082    ** with pTemplate.  pTemplate is better than p if:
147083    **   (1)  pTemplate has no more dependences than p, and
147084    **   (2)  pTemplate has an equal or lower cost than p.
147085    */
147086    if( (p->prereq & pTemplate->prereq)==pTemplate->prereq   /* (1)  */
147087     && p->rRun>=pTemplate->rRun                             /* (2a) */
147088     && p->nOut>=pTemplate->nOut                             /* (2b) */
147089    ){
147090      assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
147091      break;   /* Cause p to be overwritten by pTemplate */
147092    }
147093  }
147094  return ppPrev;
147095}
147096
147097/*
147098** Insert or replace a WhereLoop entry using the template supplied.
147099**
147100** An existing WhereLoop entry might be overwritten if the new template
147101** is better and has fewer dependencies.  Or the template will be ignored
147102** and no insert will occur if an existing WhereLoop is faster and has
147103** fewer dependencies than the template.  Otherwise a new WhereLoop is
147104** added based on the template.
147105**
147106** If pBuilder->pOrSet is not NULL then we care about only the
147107** prerequisites and rRun and nOut costs of the N best loops.  That
147108** information is gathered in the pBuilder->pOrSet object.  This special
147109** processing mode is used only for OR clause processing.
147110**
147111** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
147112** still might overwrite similar loops with the new template if the
147113** new template is better.  Loops may be overwritten if the following
147114** conditions are met:
147115**
147116**    (1)  They have the same iTab.
147117**    (2)  They have the same iSortIdx.
147118**    (3)  The template has same or fewer dependencies than the current loop
147119**    (4)  The template has the same or lower cost than the current loop
147120*/
147121static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
147122  WhereLoop **ppPrev, *p;
147123  WhereInfo *pWInfo = pBuilder->pWInfo;
147124  sqlite3 *db = pWInfo->pParse->db;
147125  int rc;
147126
147127  /* Stop the search once we hit the query planner search limit */
147128  if( pBuilder->iPlanLimit==0 ){
147129    WHERETRACE(0xffffffff,("=== query planner search limit reached ===\n"));
147130    if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
147131    return SQLITE_DONE;
147132  }
147133  pBuilder->iPlanLimit--;
147134
147135  whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
147136
147137  /* If pBuilder->pOrSet is defined, then only keep track of the costs
147138  ** and prereqs.
147139  */
147140  if( pBuilder->pOrSet!=0 ){
147141    if( pTemplate->nLTerm ){
147142#if WHERETRACE_ENABLED
147143      u16 n = pBuilder->pOrSet->n;
147144      int x =
147145#endif
147146      whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
147147                                    pTemplate->nOut);
147148#if WHERETRACE_ENABLED /* 0x8 */
147149      if( sqlite3WhereTrace & 0x8 ){
147150        sqlite3DebugPrintf(x?"   or-%d:  ":"   or-X:  ", n);
147151        sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
147152      }
147153#endif
147154    }
147155    return SQLITE_OK;
147156  }
147157
147158  /* Look for an existing WhereLoop to replace with pTemplate
147159  */
147160  ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
147161
147162  if( ppPrev==0 ){
147163    /* There already exists a WhereLoop on the list that is better
147164    ** than pTemplate, so just ignore pTemplate */
147165#if WHERETRACE_ENABLED /* 0x8 */
147166    if( sqlite3WhereTrace & 0x8 ){
147167      sqlite3DebugPrintf("   skip: ");
147168      sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
147169    }
147170#endif
147171    return SQLITE_OK;
147172  }else{
147173    p = *ppPrev;
147174  }
147175
147176  /* If we reach this point it means that either p[] should be overwritten
147177  ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
147178  ** WhereLoop and insert it.
147179  */
147180#if WHERETRACE_ENABLED /* 0x8 */
147181  if( sqlite3WhereTrace & 0x8 ){
147182    if( p!=0 ){
147183      sqlite3DebugPrintf("replace: ");
147184      sqlite3WhereLoopPrint(p, pBuilder->pWC);
147185      sqlite3DebugPrintf("   with: ");
147186    }else{
147187      sqlite3DebugPrintf("    add: ");
147188    }
147189    sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
147190  }
147191#endif
147192  if( p==0 ){
147193    /* Allocate a new WhereLoop to add to the end of the list */
147194    *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop));
147195    if( p==0 ) return SQLITE_NOMEM_BKPT;
147196    whereLoopInit(p);
147197    p->pNextLoop = 0;
147198  }else{
147199    /* We will be overwriting WhereLoop p[].  But before we do, first
147200    ** go through the rest of the list and delete any other entries besides
147201    ** p[] that are also supplated by pTemplate */
147202    WhereLoop **ppTail = &p->pNextLoop;
147203    WhereLoop *pToDel;
147204    while( *ppTail ){
147205      ppTail = whereLoopFindLesser(ppTail, pTemplate);
147206      if( ppTail==0 ) break;
147207      pToDel = *ppTail;
147208      if( pToDel==0 ) break;
147209      *ppTail = pToDel->pNextLoop;
147210#if WHERETRACE_ENABLED /* 0x8 */
147211      if( sqlite3WhereTrace & 0x8 ){
147212        sqlite3DebugPrintf(" delete: ");
147213        sqlite3WhereLoopPrint(pToDel, pBuilder->pWC);
147214      }
147215#endif
147216      whereLoopDelete(db, pToDel);
147217    }
147218  }
147219  rc = whereLoopXfer(db, p, pTemplate);
147220  if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
147221    Index *pIndex = p->u.btree.pIndex;
147222    if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK ){
147223      p->u.btree.pIndex = 0;
147224    }
147225  }
147226  return rc;
147227}
147228
147229/*
147230** Adjust the WhereLoop.nOut value downward to account for terms of the
147231** WHERE clause that reference the loop but which are not used by an
147232** index.
147233*
147234** For every WHERE clause term that is not used by the index
147235** and which has a truth probability assigned by one of the likelihood(),
147236** likely(), or unlikely() SQL functions, reduce the estimated number
147237** of output rows by the probability specified.
147238**
147239** TUNING:  For every WHERE clause term that is not used by the index
147240** and which does not have an assigned truth probability, heuristics
147241** described below are used to try to estimate the truth probability.
147242** TODO --> Perhaps this is something that could be improved by better
147243** table statistics.
147244**
147245** Heuristic 1:  Estimate the truth probability as 93.75%.  The 93.75%
147246** value corresponds to -1 in LogEst notation, so this means decrement
147247** the WhereLoop.nOut field for every such WHERE clause term.
147248**
147249** Heuristic 2:  If there exists one or more WHERE clause terms of the
147250** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the
147251** final output row estimate is no greater than 1/4 of the total number
147252** of rows in the table.  In other words, assume that x==EXPR will filter
147253** out at least 3 out of 4 rows.  If EXPR is -1 or 0 or 1, then maybe the
147254** "x" column is boolean or else -1 or 0 or 1 is a common default value
147255** on the "x" column and so in that case only cap the output row estimate
147256** at 1/2 instead of 1/4.
147257*/
147258static void whereLoopOutputAdjust(
147259  WhereClause *pWC,      /* The WHERE clause */
147260  WhereLoop *pLoop,      /* The loop to adjust downward */
147261  LogEst nRow            /* Number of rows in the entire table */
147262){
147263  WhereTerm *pTerm, *pX;
147264  Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
147265  int i, j;
147266  LogEst iReduce = 0;    /* pLoop->nOut should not exceed nRow-iReduce */
147267
147268  assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
147269  for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
147270    assert( pTerm!=0 );
147271    if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
147272    if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
147273    if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
147274    for(j=pLoop->nLTerm-1; j>=0; j--){
147275      pX = pLoop->aLTerm[j];
147276      if( pX==0 ) continue;
147277      if( pX==pTerm ) break;
147278      if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
147279    }
147280    if( j<0 ){
147281      if( pTerm->truthProb<=0 ){
147282        /* If a truth probability is specified using the likelihood() hints,
147283        ** then use the probability provided by the application. */
147284        pLoop->nOut += pTerm->truthProb;
147285      }else{
147286        /* In the absence of explicit truth probabilities, use heuristics to
147287        ** guess a reasonable truth probability. */
147288        pLoop->nOut--;
147289        if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0
147290         && (pTerm->wtFlags & TERM_HIGHTRUTH)==0  /* tag-20200224-1 */
147291        ){
147292          Expr *pRight = pTerm->pExpr->pRight;
147293          int k = 0;
147294          testcase( pTerm->pExpr->op==TK_IS );
147295          if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
147296            k = 10;
147297          }else{
147298            k = 20;
147299          }
147300          if( iReduce<k ){
147301            pTerm->wtFlags |= TERM_HEURTRUTH;
147302            iReduce = k;
147303          }
147304        }
147305      }
147306    }
147307  }
147308  if( pLoop->nOut > nRow-iReduce )  pLoop->nOut = nRow - iReduce;
147309}
147310
147311/*
147312** Term pTerm is a vector range comparison operation. The first comparison
147313** in the vector can be optimized using column nEq of the index. This
147314** function returns the total number of vector elements that can be used
147315** as part of the range comparison.
147316**
147317** For example, if the query is:
147318**
147319**   WHERE a = ? AND (b, c, d) > (?, ?, ?)
147320**
147321** and the index:
147322**
147323**   CREATE INDEX ... ON (a, b, c, d, e)
147324**
147325** then this function would be invoked with nEq=1. The value returned in
147326** this case is 3.
147327*/
147328static int whereRangeVectorLen(
147329  Parse *pParse,       /* Parsing context */
147330  int iCur,            /* Cursor open on pIdx */
147331  Index *pIdx,         /* The index to be used for a inequality constraint */
147332  int nEq,             /* Number of prior equality constraints on same index */
147333  WhereTerm *pTerm     /* The vector inequality constraint */
147334){
147335  int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
147336  int i;
147337
147338  nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
147339  for(i=1; i<nCmp; i++){
147340    /* Test if comparison i of pTerm is compatible with column (i+nEq)
147341    ** of the index. If not, exit the loop.  */
147342    char aff;                     /* Comparison affinity */
147343    char idxaff = 0;              /* Indexed columns affinity */
147344    CollSeq *pColl;               /* Comparison collation sequence */
147345    Expr *pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
147346    Expr *pRhs = pTerm->pExpr->pRight;
147347    if( pRhs->flags & EP_xIsSelect ){
147348      pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
147349    }else{
147350      pRhs = pRhs->x.pList->a[i].pExpr;
147351    }
147352
147353    /* Check that the LHS of the comparison is a column reference to
147354    ** the right column of the right source table. And that the sort
147355    ** order of the index column is the same as the sort order of the
147356    ** leftmost index column.  */
147357    if( pLhs->op!=TK_COLUMN
147358     || pLhs->iTable!=iCur
147359     || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
147360     || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
147361    ){
147362      break;
147363    }
147364
147365    testcase( pLhs->iColumn==XN_ROWID );
147366    aff = sqlite3CompareAffinity(pRhs, sqlite3ExprAffinity(pLhs));
147367    idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
147368    if( aff!=idxaff ) break;
147369
147370    pColl = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
147371    if( pColl==0 ) break;
147372    if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
147373  }
147374  return i;
147375}
147376
147377/*
147378** Adjust the cost C by the costMult facter T.  This only occurs if
147379** compiled with -DSQLITE_ENABLE_COSTMULT
147380*/
147381#ifdef SQLITE_ENABLE_COSTMULT
147382# define ApplyCostMultiplier(C,T)  C += T
147383#else
147384# define ApplyCostMultiplier(C,T)
147385#endif
147386
147387/*
147388** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
147389** index pIndex. Try to match one more.
147390**
147391** When this function is called, pBuilder->pNew->nOut contains the
147392** number of rows expected to be visited by filtering using the nEq
147393** terms only. If it is modified, this value is restored before this
147394** function returns.
147395**
147396** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
147397** a fake index used for the INTEGER PRIMARY KEY.
147398*/
147399static int whereLoopAddBtreeIndex(
147400  WhereLoopBuilder *pBuilder,     /* The WhereLoop factory */
147401  struct SrcList_item *pSrc,      /* FROM clause term being analyzed */
147402  Index *pProbe,                  /* An index on pSrc */
147403  LogEst nInMul                   /* log(Number of iterations due to IN) */
147404){
147405  WhereInfo *pWInfo = pBuilder->pWInfo;  /* WHERE analyse context */
147406  Parse *pParse = pWInfo->pParse;        /* Parsing context */
147407  sqlite3 *db = pParse->db;       /* Database connection malloc context */
147408  WhereLoop *pNew;                /* Template WhereLoop under construction */
147409  WhereTerm *pTerm;               /* A WhereTerm under consideration */
147410  int opMask;                     /* Valid operators for constraints */
147411  WhereScan scan;                 /* Iterator for WHERE terms */
147412  Bitmask saved_prereq;           /* Original value of pNew->prereq */
147413  u16 saved_nLTerm;               /* Original value of pNew->nLTerm */
147414  u16 saved_nEq;                  /* Original value of pNew->u.btree.nEq */
147415  u16 saved_nBtm;                 /* Original value of pNew->u.btree.nBtm */
147416  u16 saved_nTop;                 /* Original value of pNew->u.btree.nTop */
147417  u16 saved_nSkip;                /* Original value of pNew->nSkip */
147418  u32 saved_wsFlags;              /* Original value of pNew->wsFlags */
147419  LogEst saved_nOut;              /* Original value of pNew->nOut */
147420  int rc = SQLITE_OK;             /* Return code */
147421  LogEst rSize;                   /* Number of rows in the table */
147422  LogEst rLogSize;                /* Logarithm of table size */
147423  WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
147424
147425  pNew = pBuilder->pNew;
147426  if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
147427  WHERETRACE(0x800, ("BEGIN %s.addBtreeIdx(%s), nEq=%d, nSkip=%d\n",
147428                     pProbe->pTable->zName,pProbe->zName,
147429                     pNew->u.btree.nEq, pNew->nSkip));
147430
147431  assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
147432  assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
147433  if( pNew->wsFlags & WHERE_BTM_LIMIT ){
147434    opMask = WO_LT|WO_LE;
147435  }else{
147436    assert( pNew->u.btree.nBtm==0 );
147437    opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS;
147438  }
147439  if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
147440
147441  assert( pNew->u.btree.nEq<pProbe->nColumn );
147442
147443  saved_nEq = pNew->u.btree.nEq;
147444  saved_nBtm = pNew->u.btree.nBtm;
147445  saved_nTop = pNew->u.btree.nTop;
147446  saved_nSkip = pNew->nSkip;
147447  saved_nLTerm = pNew->nLTerm;
147448  saved_wsFlags = pNew->wsFlags;
147449  saved_prereq = pNew->prereq;
147450  saved_nOut = pNew->nOut;
147451  pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
147452                        opMask, pProbe);
147453  pNew->rSetup = 0;
147454  rSize = pProbe->aiRowLogEst[0];
147455  rLogSize = estLog(rSize);
147456  for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
147457    u16 eOp = pTerm->eOperator;   /* Shorthand for pTerm->eOperator */
147458    LogEst rCostIdx;
147459    LogEst nOutUnadjusted;        /* nOut before IN() and WHERE adjustments */
147460    int nIn = 0;
147461#ifdef SQLITE_ENABLE_STAT4
147462    int nRecValid = pBuilder->nRecValid;
147463#endif
147464    if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
147465     && indexColumnNotNull(pProbe, saved_nEq)
147466    ){
147467      continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
147468    }
147469    if( pTerm->prereqRight & pNew->maskSelf ) continue;
147470
147471    /* Do not allow the upper bound of a LIKE optimization range constraint
147472    ** to mix with a lower range bound from some other source */
147473    if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
147474
147475    /* tag-20191211-001:  Do not allow constraints from the WHERE clause to
147476    ** be used by the right table of a LEFT JOIN.  Only constraints in the
147477    ** ON clause are allowed.  See tag-20191211-002 for the vtab equivalent. */
147478    if( (pSrc->fg.jointype & JT_LEFT)!=0
147479     && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
147480    ){
147481      continue;
147482    }
147483
147484    if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
147485      pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE;
147486    }else{
147487      pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED;
147488    }
147489    pNew->wsFlags = saved_wsFlags;
147490    pNew->u.btree.nEq = saved_nEq;
147491    pNew->u.btree.nBtm = saved_nBtm;
147492    pNew->u.btree.nTop = saved_nTop;
147493    pNew->nLTerm = saved_nLTerm;
147494    if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
147495    pNew->aLTerm[pNew->nLTerm++] = pTerm;
147496    pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
147497
147498    assert( nInMul==0
147499        || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
147500        || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
147501        || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
147502    );
147503
147504    if( eOp & WO_IN ){
147505      Expr *pExpr = pTerm->pExpr;
147506      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
147507        /* "x IN (SELECT ...)":  TUNING: the SELECT returns 25 rows */
147508        int i;
147509        nIn = 46;  assert( 46==sqlite3LogEst(25) );
147510
147511        /* The expression may actually be of the form (x, y) IN (SELECT...).
147512        ** In this case there is a separate term for each of (x) and (y).
147513        ** However, the nIn multiplier should only be applied once, not once
147514        ** for each such term. The following loop checks that pTerm is the
147515        ** first such term in use, and sets nIn back to 0 if it is not. */
147516        for(i=0; i<pNew->nLTerm-1; i++){
147517          if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
147518        }
147519      }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
147520        /* "x IN (value, value, ...)" */
147521        nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
147522      }
147523      if( pProbe->hasStat1 ){
147524        LogEst M, logK, safetyMargin;
147525        /* Let:
147526        **   N = the total number of rows in the table
147527        **   K = the number of entries on the RHS of the IN operator
147528        **   M = the number of rows in the table that match terms to the
147529        **       to the left in the same index.  If the IN operator is on
147530        **       the left-most index column, M==N.
147531        **
147532        ** Given the definitions above, it is better to omit the IN operator
147533        ** from the index lookup and instead do a scan of the M elements,
147534        ** testing each scanned row against the IN operator separately, if:
147535        **
147536        **        M*log(K) < K*log(N)
147537        **
147538        ** Our estimates for M, K, and N might be inaccurate, so we build in
147539        ** a safety margin of 2 (LogEst: 10) that favors using the IN operator
147540        ** with the index, as using an index has better worst-case behavior.
147541        ** If we do not have real sqlite_stat1 data, always prefer to use
147542        ** the index.
147543        */
147544        M = pProbe->aiRowLogEst[saved_nEq];
147545        logK = estLog(nIn);
147546        safetyMargin = 10;  /* TUNING: extra weight for indexed IN */
147547        if( M + logK + safetyMargin < nIn + rLogSize ){
147548          WHERETRACE(0x40,
147549            ("Scan preferred over IN operator on column %d of \"%s\" (%d<%d)\n",
147550             saved_nEq, pProbe->zName, M+logK+10, nIn+rLogSize));
147551          continue;
147552        }else{
147553          WHERETRACE(0x40,
147554            ("IN operator preferred on column %d of \"%s\" (%d>=%d)\n",
147555             saved_nEq, pProbe->zName, M+logK+10, nIn+rLogSize));
147556        }
147557      }
147558      pNew->wsFlags |= WHERE_COLUMN_IN;
147559    }else if( eOp & (WO_EQ|WO_IS) ){
147560      int iCol = pProbe->aiColumn[saved_nEq];
147561      pNew->wsFlags |= WHERE_COLUMN_EQ;
147562      assert( saved_nEq==pNew->u.btree.nEq );
147563      if( iCol==XN_ROWID
147564       || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
147565      ){
147566        if( iCol==XN_ROWID || pProbe->uniqNotNull
147567         || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
147568        ){
147569          pNew->wsFlags |= WHERE_ONEROW;
147570        }else{
147571          pNew->wsFlags |= WHERE_UNQ_WANTED;
147572        }
147573      }
147574    }else if( eOp & WO_ISNULL ){
147575      pNew->wsFlags |= WHERE_COLUMN_NULL;
147576    }else if( eOp & (WO_GT|WO_GE) ){
147577      testcase( eOp & WO_GT );
147578      testcase( eOp & WO_GE );
147579      pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
147580      pNew->u.btree.nBtm = whereRangeVectorLen(
147581          pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
147582      );
147583      pBtm = pTerm;
147584      pTop = 0;
147585      if( pTerm->wtFlags & TERM_LIKEOPT ){
147586        /* Range contraints that come from the LIKE optimization are
147587        ** always used in pairs. */
147588        pTop = &pTerm[1];
147589        assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
147590        assert( pTop->wtFlags & TERM_LIKEOPT );
147591        assert( pTop->eOperator==WO_LT );
147592        if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
147593        pNew->aLTerm[pNew->nLTerm++] = pTop;
147594        pNew->wsFlags |= WHERE_TOP_LIMIT;
147595        pNew->u.btree.nTop = 1;
147596      }
147597    }else{
147598      assert( eOp & (WO_LT|WO_LE) );
147599      testcase( eOp & WO_LT );
147600      testcase( eOp & WO_LE );
147601      pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
147602      pNew->u.btree.nTop = whereRangeVectorLen(
147603          pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
147604      );
147605      pTop = pTerm;
147606      pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
147607                     pNew->aLTerm[pNew->nLTerm-2] : 0;
147608    }
147609
147610    /* At this point pNew->nOut is set to the number of rows expected to
147611    ** be visited by the index scan before considering term pTerm, or the
147612    ** values of nIn and nInMul. In other words, assuming that all
147613    ** "x IN(...)" terms are replaced with "x = ?". This block updates
147614    ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul).  */
147615    assert( pNew->nOut==saved_nOut );
147616    if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
147617      /* Adjust nOut using stat4 data. Or, if there is no stat4
147618      ** data, using some other estimate.  */
147619      whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
147620    }else{
147621      int nEq = ++pNew->u.btree.nEq;
147622      assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) );
147623
147624      assert( pNew->nOut==saved_nOut );
147625      if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
147626        assert( (eOp & WO_IN) || nIn==0 );
147627        testcase( eOp & WO_IN );
147628        pNew->nOut += pTerm->truthProb;
147629        pNew->nOut -= nIn;
147630      }else{
147631#ifdef SQLITE_ENABLE_STAT4
147632        tRowcnt nOut = 0;
147633        if( nInMul==0
147634         && pProbe->nSample
147635         && pNew->u.btree.nEq<=pProbe->nSampleCol
147636         && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
147637         && OptimizationEnabled(db, SQLITE_Stat4)
147638        ){
147639          Expr *pExpr = pTerm->pExpr;
147640          if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){
147641            testcase( eOp & WO_EQ );
147642            testcase( eOp & WO_IS );
147643            testcase( eOp & WO_ISNULL );
147644            rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
147645          }else{
147646            rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
147647          }
147648          if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
147649          if( rc!=SQLITE_OK ) break;          /* Jump out of the pTerm loop */
147650          if( nOut ){
147651            pNew->nOut = sqlite3LogEst(nOut);
147652            if( nEq==1
147653             /* TUNING: Mark terms as "low selectivity" if they seem likely
147654             ** to be true for half or more of the rows in the table.
147655             ** See tag-202002240-1 */
147656             && pNew->nOut+10 > pProbe->aiRowLogEst[0]
147657            ){
147658#if WHERETRACE_ENABLED /* 0x01 */
147659              if( sqlite3WhereTrace & 0x01 ){
147660                sqlite3DebugPrintf(
147661                   "STAT4 determines term has low selectivity:\n");
147662                sqlite3WhereTermPrint(pTerm, 999);
147663              }
147664#endif
147665              pTerm->wtFlags |= TERM_HIGHTRUTH;
147666              if( pTerm->wtFlags & TERM_HEURTRUTH ){
147667                /* If the term has previously been used with an assumption of
147668                ** higher selectivity, then set the flag to rerun the
147669                ** loop computations. */
147670                pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS;
147671              }
147672            }
147673            if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
147674            pNew->nOut -= nIn;
147675          }
147676        }
147677        if( nOut==0 )
147678#endif
147679        {
147680          pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
147681          if( eOp & WO_ISNULL ){
147682            /* TUNING: If there is no likelihood() value, assume that a
147683            ** "col IS NULL" expression matches twice as many rows
147684            ** as (col=?). */
147685            pNew->nOut += 10;
147686          }
147687        }
147688      }
147689    }
147690
147691    /* Set rCostIdx to the cost of visiting selected rows in index. Add
147692    ** it to pNew->rRun, which is currently set to the cost of the index
147693    ** seek only. Then, if this is a non-covering index, add the cost of
147694    ** visiting the rows in the main table.  */
147695    assert( pSrc->pTab->szTabRow>0 );
147696    rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
147697    pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
147698    if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
147699      pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
147700    }
147701    ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
147702
147703    nOutUnadjusted = pNew->nOut;
147704    pNew->rRun += nInMul + nIn;
147705    pNew->nOut += nInMul + nIn;
147706    whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
147707    rc = whereLoopInsert(pBuilder, pNew);
147708
147709    if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
147710      pNew->nOut = saved_nOut;
147711    }else{
147712      pNew->nOut = nOutUnadjusted;
147713    }
147714
147715    if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
147716     && pNew->u.btree.nEq<pProbe->nColumn
147717    ){
147718      whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
147719    }
147720    pNew->nOut = saved_nOut;
147721#ifdef SQLITE_ENABLE_STAT4
147722    pBuilder->nRecValid = nRecValid;
147723#endif
147724  }
147725  pNew->prereq = saved_prereq;
147726  pNew->u.btree.nEq = saved_nEq;
147727  pNew->u.btree.nBtm = saved_nBtm;
147728  pNew->u.btree.nTop = saved_nTop;
147729  pNew->nSkip = saved_nSkip;
147730  pNew->wsFlags = saved_wsFlags;
147731  pNew->nOut = saved_nOut;
147732  pNew->nLTerm = saved_nLTerm;
147733
147734  /* Consider using a skip-scan if there are no WHERE clause constraints
147735  ** available for the left-most terms of the index, and if the average
147736  ** number of repeats in the left-most terms is at least 18.
147737  **
147738  ** The magic number 18 is selected on the basis that scanning 17 rows
147739  ** is almost always quicker than an index seek (even though if the index
147740  ** contains fewer than 2^17 rows we assume otherwise in other parts of
147741  ** the code). And, even if it is not, it should not be too much slower.
147742  ** On the other hand, the extra seeks could end up being significantly
147743  ** more expensive.  */
147744  assert( 42==sqlite3LogEst(18) );
147745  if( saved_nEq==saved_nSkip
147746   && saved_nEq+1<pProbe->nKeyCol
147747   && saved_nEq==pNew->nLTerm
147748   && pProbe->noSkipScan==0
147749   && pProbe->hasStat1!=0
147750   && OptimizationEnabled(db, SQLITE_SkipScan)
147751   && pProbe->aiRowLogEst[saved_nEq+1]>=42  /* TUNING: Minimum for skip-scan */
147752   && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
147753  ){
147754    LogEst nIter;
147755    pNew->u.btree.nEq++;
147756    pNew->nSkip++;
147757    pNew->aLTerm[pNew->nLTerm++] = 0;
147758    pNew->wsFlags |= WHERE_SKIPSCAN;
147759    nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
147760    pNew->nOut -= nIter;
147761    /* TUNING:  Because uncertainties in the estimates for skip-scan queries,
147762    ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
147763    nIter += 5;
147764    whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
147765    pNew->nOut = saved_nOut;
147766    pNew->u.btree.nEq = saved_nEq;
147767    pNew->nSkip = saved_nSkip;
147768    pNew->wsFlags = saved_wsFlags;
147769  }
147770
147771  WHERETRACE(0x800, ("END %s.addBtreeIdx(%s), nEq=%d, rc=%d\n",
147772                      pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
147773  return rc;
147774}
147775
147776/*
147777** Return True if it is possible that pIndex might be useful in
147778** implementing the ORDER BY clause in pBuilder.
147779**
147780** Return False if pBuilder does not contain an ORDER BY clause or
147781** if there is no way for pIndex to be useful in implementing that
147782** ORDER BY clause.
147783*/
147784static int indexMightHelpWithOrderBy(
147785  WhereLoopBuilder *pBuilder,
147786  Index *pIndex,
147787  int iCursor
147788){
147789  ExprList *pOB;
147790  ExprList *aColExpr;
147791  int ii, jj;
147792
147793  if( pIndex->bUnordered ) return 0;
147794  if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
147795  for(ii=0; ii<pOB->nExpr; ii++){
147796    Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pOB->a[ii].pExpr);
147797    if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
147798      if( pExpr->iColumn<0 ) return 1;
147799      for(jj=0; jj<pIndex->nKeyCol; jj++){
147800        if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
147801      }
147802    }else if( (aColExpr = pIndex->aColExpr)!=0 ){
147803      for(jj=0; jj<pIndex->nKeyCol; jj++){
147804        if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
147805        if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
147806          return 1;
147807        }
147808      }
147809    }
147810  }
147811  return 0;
147812}
147813
147814/* Check to see if a partial index with pPartIndexWhere can be used
147815** in the current query.  Return true if it can be and false if not.
147816*/
147817static int whereUsablePartialIndex(
147818  int iTab,             /* The table for which we want an index */
147819  int isLeft,           /* True if iTab is the right table of a LEFT JOIN */
147820  WhereClause *pWC,     /* The WHERE clause of the query */
147821  Expr *pWhere          /* The WHERE clause from the partial index */
147822){
147823  int i;
147824  WhereTerm *pTerm;
147825  Parse *pParse = pWC->pWInfo->pParse;
147826  while( pWhere->op==TK_AND ){
147827    if( !whereUsablePartialIndex(iTab,isLeft,pWC,pWhere->pLeft) ) return 0;
147828    pWhere = pWhere->pRight;
147829  }
147830  if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
147831  for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
147832    Expr *pExpr;
147833    pExpr = pTerm->pExpr;
147834    if( (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab)
147835     && (isLeft==0 || ExprHasProperty(pExpr, EP_FromJoin))
147836     && sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, iTab)
147837    ){
147838      return 1;
147839    }
147840  }
147841  return 0;
147842}
147843
147844/*
147845** Add all WhereLoop objects for a single table of the join where the table
147846** is identified by pBuilder->pNew->iTab.  That table is guaranteed to be
147847** a b-tree table, not a virtual table.
147848**
147849** The costs (WhereLoop.rRun) of the b-tree loops added by this function
147850** are calculated as follows:
147851**
147852** For a full scan, assuming the table (or index) contains nRow rows:
147853**
147854**     cost = nRow * 3.0                    // full-table scan
147855**     cost = nRow * K                      // scan of covering index
147856**     cost = nRow * (K+3.0)                // scan of non-covering index
147857**
147858** where K is a value between 1.1 and 3.0 set based on the relative
147859** estimated average size of the index and table records.
147860**
147861** For an index scan, where nVisit is the number of index rows visited
147862** by the scan, and nSeek is the number of seek operations required on
147863** the index b-tree:
147864**
147865**     cost = nSeek * (log(nRow) + K * nVisit)          // covering index
147866**     cost = nSeek * (log(nRow) + (K+3.0) * nVisit)    // non-covering index
147867**
147868** Normally, nSeek is 1. nSeek values greater than 1 come about if the
147869** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
147870** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
147871**
147872** The estimated values (nRow, nVisit, nSeek) often contain a large amount
147873** of uncertainty.  For this reason, scoring is designed to pick plans that
147874** "do the least harm" if the estimates are inaccurate.  For example, a
147875** log(nRow) factor is omitted from a non-covering index scan in order to
147876** bias the scoring in favor of using an index, since the worst-case
147877** performance of using an index is far better than the worst-case performance
147878** of a full table scan.
147879*/
147880static int whereLoopAddBtree(
147881  WhereLoopBuilder *pBuilder, /* WHERE clause information */
147882  Bitmask mPrereq             /* Extra prerequesites for using this table */
147883){
147884  WhereInfo *pWInfo;          /* WHERE analysis context */
147885  Index *pProbe;              /* An index we are evaluating */
147886  Index sPk;                  /* A fake index object for the primary key */
147887  LogEst aiRowEstPk[2];       /* The aiRowLogEst[] value for the sPk index */
147888  i16 aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
147889  SrcList *pTabList;          /* The FROM clause */
147890  struct SrcList_item *pSrc;  /* The FROM clause btree term to add */
147891  WhereLoop *pNew;            /* Template WhereLoop object */
147892  int rc = SQLITE_OK;         /* Return code */
147893  int iSortIdx = 1;           /* Index number */
147894  int b;                      /* A boolean value */
147895  LogEst rSize;               /* number of rows in the table */
147896  LogEst rLogSize;            /* Logarithm of the number of rows in the table */
147897  WhereClause *pWC;           /* The parsed WHERE clause */
147898  Table *pTab;                /* Table being queried */
147899
147900  pNew = pBuilder->pNew;
147901  pWInfo = pBuilder->pWInfo;
147902  pTabList = pWInfo->pTabList;
147903  pSrc = pTabList->a + pNew->iTab;
147904  pTab = pSrc->pTab;
147905  pWC = pBuilder->pWC;
147906  assert( !IsVirtual(pSrc->pTab) );
147907
147908  if( pSrc->pIBIndex ){
147909    /* An INDEXED BY clause specifies a particular index to use */
147910    pProbe = pSrc->pIBIndex;
147911  }else if( !HasRowid(pTab) ){
147912    pProbe = pTab->pIndex;
147913  }else{
147914    /* There is no INDEXED BY clause.  Create a fake Index object in local
147915    ** variable sPk to represent the rowid primary key index.  Make this
147916    ** fake index the first in a chain of Index objects with all of the real
147917    ** indices to follow */
147918    Index *pFirst;                  /* First of real indices on the table */
147919    memset(&sPk, 0, sizeof(Index));
147920    sPk.nKeyCol = 1;
147921    sPk.nColumn = 1;
147922    sPk.aiColumn = &aiColumnPk;
147923    sPk.aiRowLogEst = aiRowEstPk;
147924    sPk.onError = OE_Replace;
147925    sPk.pTable = pTab;
147926    sPk.szIdxRow = pTab->szTabRow;
147927    sPk.idxType = SQLITE_IDXTYPE_IPK;
147928    aiRowEstPk[0] = pTab->nRowLogEst;
147929    aiRowEstPk[1] = 0;
147930    pFirst = pSrc->pTab->pIndex;
147931    if( pSrc->fg.notIndexed==0 ){
147932      /* The real indices of the table are only considered if the
147933      ** NOT INDEXED qualifier is omitted from the FROM clause */
147934      sPk.pNext = pFirst;
147935    }
147936    pProbe = &sPk;
147937  }
147938  rSize = pTab->nRowLogEst;
147939  rLogSize = estLog(rSize);
147940
147941#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
147942  /* Automatic indexes */
147943  if( !pBuilder->pOrSet      /* Not part of an OR optimization */
147944   && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0
147945   && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
147946   && pSrc->pIBIndex==0      /* Has no INDEXED BY clause */
147947   && !pSrc->fg.notIndexed   /* Has no NOT INDEXED clause */
147948   && HasRowid(pTab)         /* Not WITHOUT ROWID table. (FIXME: Why not?) */
147949   && !pSrc->fg.isCorrelated /* Not a correlated subquery */
147950   && !pSrc->fg.isRecursive  /* Not a recursive common table expression. */
147951  ){
147952    /* Generate auto-index WhereLoops */
147953    WhereTerm *pTerm;
147954    WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
147955    for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
147956      if( pTerm->prereqRight & pNew->maskSelf ) continue;
147957      if( termCanDriveIndex(pTerm, pSrc, 0) ){
147958        pNew->u.btree.nEq = 1;
147959        pNew->nSkip = 0;
147960        pNew->u.btree.pIndex = 0;
147961        pNew->nLTerm = 1;
147962        pNew->aLTerm[0] = pTerm;
147963        /* TUNING: One-time cost for computing the automatic index is
147964        ** estimated to be X*N*log2(N) where N is the number of rows in
147965        ** the table being indexed and where X is 7 (LogEst=28) for normal
147966        ** tables or 0.5 (LogEst=-10) for views and subqueries.  The value
147967        ** of X is smaller for views and subqueries so that the query planner
147968        ** will be more aggressive about generating automatic indexes for
147969        ** those objects, since there is no opportunity to add schema
147970        ** indexes on subqueries and views. */
147971        pNew->rSetup = rLogSize + rSize;
147972        if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
147973          pNew->rSetup += 28;
147974        }else{
147975          pNew->rSetup -= 10;
147976        }
147977        ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
147978        if( pNew->rSetup<0 ) pNew->rSetup = 0;
147979        /* TUNING: Each index lookup yields 20 rows in the table.  This
147980        ** is more than the usual guess of 10 rows, since we have no way
147981        ** of knowing how selective the index will ultimately be.  It would
147982        ** not be unreasonable to make this value much larger. */
147983        pNew->nOut = 43;  assert( 43==sqlite3LogEst(20) );
147984        pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
147985        pNew->wsFlags = WHERE_AUTO_INDEX;
147986        pNew->prereq = mPrereq | pTerm->prereqRight;
147987        rc = whereLoopInsert(pBuilder, pNew);
147988      }
147989    }
147990  }
147991#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
147992
147993  /* Loop over all indices. If there was an INDEXED BY clause, then only
147994  ** consider index pProbe.  */
147995  for(; rc==SQLITE_OK && pProbe;
147996      pProbe=(pSrc->pIBIndex ? 0 : pProbe->pNext), iSortIdx++
147997  ){
147998    int isLeft = (pSrc->fg.jointype & JT_OUTER)!=0;
147999    if( pProbe->pPartIdxWhere!=0
148000     && !whereUsablePartialIndex(pSrc->iCursor, isLeft, pWC,
148001                                 pProbe->pPartIdxWhere)
148002    ){
148003      testcase( pNew->iTab!=pSrc->iCursor );  /* See ticket [98d973b8f5] */
148004      continue;  /* Partial index inappropriate for this query */
148005    }
148006    if( pProbe->bNoQuery ) continue;
148007    rSize = pProbe->aiRowLogEst[0];
148008    pNew->u.btree.nEq = 0;
148009    pNew->u.btree.nBtm = 0;
148010    pNew->u.btree.nTop = 0;
148011    pNew->nSkip = 0;
148012    pNew->nLTerm = 0;
148013    pNew->iSortIdx = 0;
148014    pNew->rSetup = 0;
148015    pNew->prereq = mPrereq;
148016    pNew->nOut = rSize;
148017    pNew->u.btree.pIndex = pProbe;
148018    b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
148019    /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
148020    assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
148021    if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
148022      /* Integer primary key index */
148023      pNew->wsFlags = WHERE_IPK;
148024
148025      /* Full table scan */
148026      pNew->iSortIdx = b ? iSortIdx : 0;
148027      /* TUNING: Cost of full table scan is (N*3.0). */
148028      pNew->rRun = rSize + 16;
148029      ApplyCostMultiplier(pNew->rRun, pTab->costMult);
148030      whereLoopOutputAdjust(pWC, pNew, rSize);
148031      rc = whereLoopInsert(pBuilder, pNew);
148032      pNew->nOut = rSize;
148033      if( rc ) break;
148034    }else{
148035      Bitmask m;
148036      if( pProbe->isCovering ){
148037        pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
148038        m = 0;
148039      }else{
148040        m = pSrc->colUsed & pProbe->colNotIdxed;
148041        pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
148042      }
148043
148044      /* Full scan via index */
148045      if( b
148046       || !HasRowid(pTab)
148047       || pProbe->pPartIdxWhere!=0
148048       || ( m==0
148049         && pProbe->bUnordered==0
148050         && (pProbe->szIdxRow<pTab->szTabRow)
148051         && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
148052         && sqlite3GlobalConfig.bUseCis
148053         && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
148054          )
148055      ){
148056        pNew->iSortIdx = b ? iSortIdx : 0;
148057
148058        /* The cost of visiting the index rows is N*K, where K is
148059        ** between 1.1 and 3.0, depending on the relative sizes of the
148060        ** index and table rows. */
148061        pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
148062        if( m!=0 ){
148063          /* If this is a non-covering index scan, add in the cost of
148064          ** doing table lookups.  The cost will be 3x the number of
148065          ** lookups.  Take into account WHERE clause terms that can be
148066          ** satisfied using just the index, and that do not require a
148067          ** table lookup. */
148068          LogEst nLookup = rSize + 16;  /* Base cost:  N*3 */
148069          int ii;
148070          int iCur = pSrc->iCursor;
148071          WhereClause *pWC2 = &pWInfo->sWC;
148072          for(ii=0; ii<pWC2->nTerm; ii++){
148073            WhereTerm *pTerm = &pWC2->a[ii];
148074            if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
148075              break;
148076            }
148077            /* pTerm can be evaluated using just the index.  So reduce
148078            ** the expected number of table lookups accordingly */
148079            if( pTerm->truthProb<=0 ){
148080              nLookup += pTerm->truthProb;
148081            }else{
148082              nLookup--;
148083              if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
148084            }
148085          }
148086
148087          pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
148088        }
148089        ApplyCostMultiplier(pNew->rRun, pTab->costMult);
148090        whereLoopOutputAdjust(pWC, pNew, rSize);
148091        rc = whereLoopInsert(pBuilder, pNew);
148092        pNew->nOut = rSize;
148093        if( rc ) break;
148094      }
148095    }
148096
148097    pBuilder->bldFlags1 = 0;
148098    rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
148099    if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED ){
148100      /* If a non-unique index is used, or if a prefix of the key for
148101      ** unique index is used (making the index functionally non-unique)
148102      ** then the sqlite_stat1 data becomes important for scoring the
148103      ** plan */
148104      pTab->tabFlags |= TF_StatsUsed;
148105    }
148106#ifdef SQLITE_ENABLE_STAT4
148107    sqlite3Stat4ProbeFree(pBuilder->pRec);
148108    pBuilder->nRecValid = 0;
148109    pBuilder->pRec = 0;
148110#endif
148111  }
148112  return rc;
148113}
148114
148115#ifndef SQLITE_OMIT_VIRTUALTABLE
148116
148117/*
148118** Argument pIdxInfo is already populated with all constraints that may
148119** be used by the virtual table identified by pBuilder->pNew->iTab. This
148120** function marks a subset of those constraints usable, invokes the
148121** xBestIndex method and adds the returned plan to pBuilder.
148122**
148123** A constraint is marked usable if:
148124**
148125**   * Argument mUsable indicates that its prerequisites are available, and
148126**
148127**   * It is not one of the operators specified in the mExclude mask passed
148128**     as the fourth argument (which in practice is either WO_IN or 0).
148129**
148130** Argument mPrereq is a mask of tables that must be scanned before the
148131** virtual table in question. These are added to the plans prerequisites
148132** before it is added to pBuilder.
148133**
148134** Output parameter *pbIn is set to true if the plan added to pBuilder
148135** uses one or more WO_IN terms, or false otherwise.
148136*/
148137static int whereLoopAddVirtualOne(
148138  WhereLoopBuilder *pBuilder,
148139  Bitmask mPrereq,                /* Mask of tables that must be used. */
148140  Bitmask mUsable,                /* Mask of usable tables */
148141  u16 mExclude,                   /* Exclude terms using these operators */
148142  sqlite3_index_info *pIdxInfo,   /* Populated object for xBestIndex */
148143  u16 mNoOmit,                    /* Do not omit these constraints */
148144  int *pbIn                       /* OUT: True if plan uses an IN(...) op */
148145){
148146  WhereClause *pWC = pBuilder->pWC;
148147  struct sqlite3_index_constraint *pIdxCons;
148148  struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
148149  int i;
148150  int mxTerm;
148151  int rc = SQLITE_OK;
148152  WhereLoop *pNew = pBuilder->pNew;
148153  Parse *pParse = pBuilder->pWInfo->pParse;
148154  struct SrcList_item *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
148155  int nConstraint = pIdxInfo->nConstraint;
148156
148157  assert( (mUsable & mPrereq)==mPrereq );
148158  *pbIn = 0;
148159  pNew->prereq = mPrereq;
148160
148161  /* Set the usable flag on the subset of constraints identified by
148162  ** arguments mUsable and mExclude. */
148163  pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
148164  for(i=0; i<nConstraint; i++, pIdxCons++){
148165    WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
148166    pIdxCons->usable = 0;
148167    if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
148168     && (pTerm->eOperator & mExclude)==0
148169    ){
148170      pIdxCons->usable = 1;
148171    }
148172  }
148173
148174  /* Initialize the output fields of the sqlite3_index_info structure */
148175  memset(pUsage, 0, sizeof(pUsage[0])*nConstraint);
148176  assert( pIdxInfo->needToFreeIdxStr==0 );
148177  pIdxInfo->idxStr = 0;
148178  pIdxInfo->idxNum = 0;
148179  pIdxInfo->orderByConsumed = 0;
148180  pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
148181  pIdxInfo->estimatedRows = 25;
148182  pIdxInfo->idxFlags = 0;
148183  pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
148184
148185  /* Invoke the virtual table xBestIndex() method */
148186  rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
148187  if( rc ){
148188    if( rc==SQLITE_CONSTRAINT ){
148189      /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
148190      ** that the particular combination of parameters provided is unusable.
148191      ** Make no entries in the loop table.
148192      */
148193      WHERETRACE(0xffff, ("  ^^^^--- non-viable plan rejected!\n"));
148194      return SQLITE_OK;
148195    }
148196    return rc;
148197  }
148198
148199  mxTerm = -1;
148200  assert( pNew->nLSlot>=nConstraint );
148201  for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
148202  pNew->u.vtab.omitMask = 0;
148203  pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
148204  for(i=0; i<nConstraint; i++, pIdxCons++){
148205    int iTerm;
148206    if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
148207      WhereTerm *pTerm;
148208      int j = pIdxCons->iTermOffset;
148209      if( iTerm>=nConstraint
148210       || j<0
148211       || j>=pWC->nTerm
148212       || pNew->aLTerm[iTerm]!=0
148213       || pIdxCons->usable==0
148214      ){
148215        sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
148216        testcase( pIdxInfo->needToFreeIdxStr );
148217        return SQLITE_ERROR;
148218      }
148219      testcase( iTerm==nConstraint-1 );
148220      testcase( j==0 );
148221      testcase( j==pWC->nTerm-1 );
148222      pTerm = &pWC->a[j];
148223      pNew->prereq |= pTerm->prereqRight;
148224      assert( iTerm<pNew->nLSlot );
148225      pNew->aLTerm[iTerm] = pTerm;
148226      if( iTerm>mxTerm ) mxTerm = iTerm;
148227      testcase( iTerm==15 );
148228      testcase( iTerm==16 );
148229      if( pUsage[i].omit ){
148230        if( i<16 && ((1<<i)&mNoOmit)==0 ){
148231          testcase( i!=iTerm );
148232          pNew->u.vtab.omitMask |= 1<<iTerm;
148233        }else{
148234          testcase( i!=iTerm );
148235        }
148236      }
148237      if( (pTerm->eOperator & WO_IN)!=0 ){
148238        /* A virtual table that is constrained by an IN clause may not
148239        ** consume the ORDER BY clause because (1) the order of IN terms
148240        ** is not necessarily related to the order of output terms and
148241        ** (2) Multiple outputs from a single IN value will not merge
148242        ** together.  */
148243        pIdxInfo->orderByConsumed = 0;
148244        pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
148245        *pbIn = 1; assert( (mExclude & WO_IN)==0 );
148246      }
148247    }
148248  }
148249
148250  pNew->nLTerm = mxTerm+1;
148251  for(i=0; i<=mxTerm; i++){
148252    if( pNew->aLTerm[i]==0 ){
148253      /* The non-zero argvIdx values must be contiguous.  Raise an
148254      ** error if they are not */
148255      sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
148256      testcase( pIdxInfo->needToFreeIdxStr );
148257      return SQLITE_ERROR;
148258    }
148259  }
148260  assert( pNew->nLTerm<=pNew->nLSlot );
148261  pNew->u.vtab.idxNum = pIdxInfo->idxNum;
148262  pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
148263  pIdxInfo->needToFreeIdxStr = 0;
148264  pNew->u.vtab.idxStr = pIdxInfo->idxStr;
148265  pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
148266      pIdxInfo->nOrderBy : 0);
148267  pNew->rSetup = 0;
148268  pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
148269  pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
148270
148271  /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated
148272  ** that the scan will visit at most one row. Clear it otherwise. */
148273  if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
148274    pNew->wsFlags |= WHERE_ONEROW;
148275  }else{
148276    pNew->wsFlags &= ~WHERE_ONEROW;
148277  }
148278  rc = whereLoopInsert(pBuilder, pNew);
148279  if( pNew->u.vtab.needFree ){
148280    sqlite3_free(pNew->u.vtab.idxStr);
148281    pNew->u.vtab.needFree = 0;
148282  }
148283  WHERETRACE(0xffff, ("  bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
148284                      *pbIn, (sqlite3_uint64)mPrereq,
148285                      (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
148286
148287  return rc;
148288}
148289
148290/*
148291** If this function is invoked from within an xBestIndex() callback, it
148292** returns a pointer to a buffer containing the name of the collation
148293** sequence associated with element iCons of the sqlite3_index_info.aConstraint
148294** array. Or, if iCons is out of range or there is no active xBestIndex
148295** call, return NULL.
148296*/
148297SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){
148298  HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
148299  const char *zRet = 0;
148300  if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
148301    CollSeq *pC = 0;
148302    int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
148303    Expr *pX = pHidden->pWC->a[iTerm].pExpr;
148304    if( pX->pLeft ){
148305      pC = sqlite3ExprCompareCollSeq(pHidden->pParse, pX);
148306    }
148307    zRet = (pC ? pC->zName : sqlite3StrBINARY);
148308  }
148309  return zRet;
148310}
148311
148312/*
148313** Add all WhereLoop objects for a table of the join identified by
148314** pBuilder->pNew->iTab.  That table is guaranteed to be a virtual table.
148315**
148316** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and
148317** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause
148318** entries that occur before the virtual table in the FROM clause and are
148319** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
148320** mUnusable mask contains all FROM clause entries that occur after the
148321** virtual table and are separated from it by at least one LEFT or
148322** CROSS JOIN.
148323**
148324** For example, if the query were:
148325**
148326**   ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
148327**
148328** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6).
148329**
148330** All the tables in mPrereq must be scanned before the current virtual
148331** table. So any terms for which all prerequisites are satisfied by
148332** mPrereq may be specified as "usable" in all calls to xBestIndex.
148333** Conversely, all tables in mUnusable must be scanned after the current
148334** virtual table, so any terms for which the prerequisites overlap with
148335** mUnusable should always be configured as "not-usable" for xBestIndex.
148336*/
148337static int whereLoopAddVirtual(
148338  WhereLoopBuilder *pBuilder,  /* WHERE clause information */
148339  Bitmask mPrereq,             /* Tables that must be scanned before this one */
148340  Bitmask mUnusable            /* Tables that must be scanned after this one */
148341){
148342  int rc = SQLITE_OK;          /* Return code */
148343  WhereInfo *pWInfo;           /* WHERE analysis context */
148344  Parse *pParse;               /* The parsing context */
148345  WhereClause *pWC;            /* The WHERE clause */
148346  struct SrcList_item *pSrc;   /* The FROM clause term to search */
148347  sqlite3_index_info *p;       /* Object to pass to xBestIndex() */
148348  int nConstraint;             /* Number of constraints in p */
148349  int bIn;                     /* True if plan uses IN(...) operator */
148350  WhereLoop *pNew;
148351  Bitmask mBest;               /* Tables used by best possible plan */
148352  u16 mNoOmit;
148353
148354  assert( (mPrereq & mUnusable)==0 );
148355  pWInfo = pBuilder->pWInfo;
148356  pParse = pWInfo->pParse;
148357  pWC = pBuilder->pWC;
148358  pNew = pBuilder->pNew;
148359  pSrc = &pWInfo->pTabList->a[pNew->iTab];
148360  assert( IsVirtual(pSrc->pTab) );
148361  p = allocateIndexInfo(pParse, pWC, mUnusable, pSrc, pBuilder->pOrderBy,
148362      &mNoOmit);
148363  if( p==0 ) return SQLITE_NOMEM_BKPT;
148364  pNew->rSetup = 0;
148365  pNew->wsFlags = WHERE_VIRTUALTABLE;
148366  pNew->nLTerm = 0;
148367  pNew->u.vtab.needFree = 0;
148368  nConstraint = p->nConstraint;
148369  if( whereLoopResize(pParse->db, pNew, nConstraint) ){
148370    sqlite3DbFree(pParse->db, p);
148371    return SQLITE_NOMEM_BKPT;
148372  }
148373
148374  /* First call xBestIndex() with all constraints usable. */
148375  WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
148376  WHERETRACE(0x40, ("  VirtualOne: all usable\n"));
148377  rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn);
148378
148379  /* If the call to xBestIndex() with all terms enabled produced a plan
148380  ** that does not require any source tables (IOW: a plan with mBest==0)
148381  ** and does not use an IN(...) operator, then there is no point in making
148382  ** any further calls to xBestIndex() since they will all return the same
148383  ** result (if the xBestIndex() implementation is sane). */
148384  if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
148385    int seenZero = 0;             /* True if a plan with no prereqs seen */
148386    int seenZeroNoIN = 0;         /* Plan with no prereqs and no IN(...) seen */
148387    Bitmask mPrev = 0;
148388    Bitmask mBestNoIn = 0;
148389
148390    /* If the plan produced by the earlier call uses an IN(...) term, call
148391    ** xBestIndex again, this time with IN(...) terms disabled. */
148392    if( bIn ){
148393      WHERETRACE(0x40, ("  VirtualOne: all usable w/o IN\n"));
148394      rc = whereLoopAddVirtualOne(
148395          pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn);
148396      assert( bIn==0 );
148397      mBestNoIn = pNew->prereq & ~mPrereq;
148398      if( mBestNoIn==0 ){
148399        seenZero = 1;
148400        seenZeroNoIN = 1;
148401      }
148402    }
148403
148404    /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq)
148405    ** in the set of terms that apply to the current virtual table.  */
148406    while( rc==SQLITE_OK ){
148407      int i;
148408      Bitmask mNext = ALLBITS;
148409      assert( mNext>0 );
148410      for(i=0; i<nConstraint; i++){
148411        Bitmask mThis = (
148412            pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
148413        );
148414        if( mThis>mPrev && mThis<mNext ) mNext = mThis;
148415      }
148416      mPrev = mNext;
148417      if( mNext==ALLBITS ) break;
148418      if( mNext==mBest || mNext==mBestNoIn ) continue;
148419      WHERETRACE(0x40, ("  VirtualOne: mPrev=%04llx mNext=%04llx\n",
148420                       (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
148421      rc = whereLoopAddVirtualOne(
148422          pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn);
148423      if( pNew->prereq==mPrereq ){
148424        seenZero = 1;
148425        if( bIn==0 ) seenZeroNoIN = 1;
148426      }
148427    }
148428
148429    /* If the calls to xBestIndex() in the above loop did not find a plan
148430    ** that requires no source tables at all (i.e. one guaranteed to be
148431    ** usable), make a call here with all source tables disabled */
148432    if( rc==SQLITE_OK && seenZero==0 ){
148433      WHERETRACE(0x40, ("  VirtualOne: all disabled\n"));
148434      rc = whereLoopAddVirtualOne(
148435          pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn);
148436      if( bIn==0 ) seenZeroNoIN = 1;
148437    }
148438
148439    /* If the calls to xBestIndex() have so far failed to find a plan
148440    ** that requires no source tables at all and does not use an IN(...)
148441    ** operator, make a final call to obtain one here.  */
148442    if( rc==SQLITE_OK && seenZeroNoIN==0 ){
148443      WHERETRACE(0x40, ("  VirtualOne: all disabled and w/o IN\n"));
148444      rc = whereLoopAddVirtualOne(
148445          pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn);
148446    }
148447  }
148448
148449  if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
148450  sqlite3DbFreeNN(pParse->db, p);
148451  WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
148452  return rc;
148453}
148454#endif /* SQLITE_OMIT_VIRTUALTABLE */
148455
148456/*
148457** Add WhereLoop entries to handle OR terms.  This works for either
148458** btrees or virtual tables.
148459*/
148460static int whereLoopAddOr(
148461  WhereLoopBuilder *pBuilder,
148462  Bitmask mPrereq,
148463  Bitmask mUnusable
148464){
148465  WhereInfo *pWInfo = pBuilder->pWInfo;
148466  WhereClause *pWC;
148467  WhereLoop *pNew;
148468  WhereTerm *pTerm, *pWCEnd;
148469  int rc = SQLITE_OK;
148470  int iCur;
148471  WhereClause tempWC;
148472  WhereLoopBuilder sSubBuild;
148473  WhereOrSet sSum, sCur;
148474  struct SrcList_item *pItem;
148475
148476  pWC = pBuilder->pWC;
148477  pWCEnd = pWC->a + pWC->nTerm;
148478  pNew = pBuilder->pNew;
148479  memset(&sSum, 0, sizeof(sSum));
148480  pItem = pWInfo->pTabList->a + pNew->iTab;
148481  iCur = pItem->iCursor;
148482
148483  for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
148484    if( (pTerm->eOperator & WO_OR)!=0
148485     && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
148486    ){
148487      WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
148488      WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
148489      WhereTerm *pOrTerm;
148490      int once = 1;
148491      int i, j;
148492
148493      sSubBuild = *pBuilder;
148494      sSubBuild.pOrderBy = 0;
148495      sSubBuild.pOrSet = &sCur;
148496
148497      WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
148498      for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
148499        if( (pOrTerm->eOperator & WO_AND)!=0 ){
148500          sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
148501        }else if( pOrTerm->leftCursor==iCur ){
148502          tempWC.pWInfo = pWC->pWInfo;
148503          tempWC.pOuter = pWC;
148504          tempWC.op = TK_AND;
148505          tempWC.nTerm = 1;
148506          tempWC.a = pOrTerm;
148507          sSubBuild.pWC = &tempWC;
148508        }else{
148509          continue;
148510        }
148511        sCur.n = 0;
148512#ifdef WHERETRACE_ENABLED
148513        WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
148514                   (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
148515        if( sqlite3WhereTrace & 0x400 ){
148516          sqlite3WhereClausePrint(sSubBuild.pWC);
148517        }
148518#endif
148519#ifndef SQLITE_OMIT_VIRTUALTABLE
148520        if( IsVirtual(pItem->pTab) ){
148521          rc = whereLoopAddVirtual(&sSubBuild, mPrereq, mUnusable);
148522        }else
148523#endif
148524        {
148525          rc = whereLoopAddBtree(&sSubBuild, mPrereq);
148526        }
148527        if( rc==SQLITE_OK ){
148528          rc = whereLoopAddOr(&sSubBuild, mPrereq, mUnusable);
148529        }
148530        assert( rc==SQLITE_OK || rc==SQLITE_DONE || sCur.n==0 );
148531        testcase( rc==SQLITE_DONE );
148532        if( sCur.n==0 ){
148533          sSum.n = 0;
148534          break;
148535        }else if( once ){
148536          whereOrMove(&sSum, &sCur);
148537          once = 0;
148538        }else{
148539          WhereOrSet sPrev;
148540          whereOrMove(&sPrev, &sSum);
148541          sSum.n = 0;
148542          for(i=0; i<sPrev.n; i++){
148543            for(j=0; j<sCur.n; j++){
148544              whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
148545                            sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
148546                            sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
148547            }
148548          }
148549        }
148550      }
148551      pNew->nLTerm = 1;
148552      pNew->aLTerm[0] = pTerm;
148553      pNew->wsFlags = WHERE_MULTI_OR;
148554      pNew->rSetup = 0;
148555      pNew->iSortIdx = 0;
148556      memset(&pNew->u, 0, sizeof(pNew->u));
148557      for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
148558        /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
148559        ** of all sub-scans required by the OR-scan. However, due to rounding
148560        ** errors, it may be that the cost of the OR-scan is equal to its
148561        ** most expensive sub-scan. Add the smallest possible penalty
148562        ** (equivalent to multiplying the cost by 1.07) to ensure that
148563        ** this does not happen. Otherwise, for WHERE clauses such as the
148564        ** following where there is an index on "y":
148565        **
148566        **     WHERE likelihood(x=?, 0.99) OR y=?
148567        **
148568        ** the planner may elect to "OR" together a full-table scan and an
148569        ** index lookup. And other similarly odd results.  */
148570        pNew->rRun = sSum.a[i].rRun + 1;
148571        pNew->nOut = sSum.a[i].nOut;
148572        pNew->prereq = sSum.a[i].prereq;
148573        rc = whereLoopInsert(pBuilder, pNew);
148574      }
148575      WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
148576    }
148577  }
148578  return rc;
148579}
148580
148581/*
148582** Add all WhereLoop objects for all tables
148583*/
148584static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
148585  WhereInfo *pWInfo = pBuilder->pWInfo;
148586  Bitmask mPrereq = 0;
148587  Bitmask mPrior = 0;
148588  int iTab;
148589  SrcList *pTabList = pWInfo->pTabList;
148590  struct SrcList_item *pItem;
148591  struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel];
148592  sqlite3 *db = pWInfo->pParse->db;
148593  int rc = SQLITE_OK;
148594  WhereLoop *pNew;
148595  u8 priorJointype = 0;
148596
148597  /* Loop over the tables in the join, from left to right */
148598  pNew = pBuilder->pNew;
148599  whereLoopInit(pNew);
148600  pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
148601  for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
148602    Bitmask mUnusable = 0;
148603    pNew->iTab = iTab;
148604    pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
148605    pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
148606    if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){
148607      /* This condition is true when pItem is the FROM clause term on the
148608      ** right-hand-side of a LEFT or CROSS JOIN.  */
148609      mPrereq = mPrior;
148610    }
148611    priorJointype = pItem->fg.jointype;
148612#ifndef SQLITE_OMIT_VIRTUALTABLE
148613    if( IsVirtual(pItem->pTab) ){
148614      struct SrcList_item *p;
148615      for(p=&pItem[1]; p<pEnd; p++){
148616        if( mUnusable || (p->fg.jointype & (JT_LEFT|JT_CROSS)) ){
148617          mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
148618        }
148619      }
148620      rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable);
148621    }else
148622#endif /* SQLITE_OMIT_VIRTUALTABLE */
148623    {
148624      rc = whereLoopAddBtree(pBuilder, mPrereq);
148625    }
148626    if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
148627      rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable);
148628    }
148629    mPrior |= pNew->maskSelf;
148630    if( rc || db->mallocFailed ){
148631      if( rc==SQLITE_DONE ){
148632        /* We hit the query planner search limit set by iPlanLimit */
148633        sqlite3_log(SQLITE_WARNING, "abbreviated query algorithm search");
148634        rc = SQLITE_OK;
148635      }else{
148636        break;
148637      }
148638    }
148639  }
148640
148641  whereLoopClear(db, pNew);
148642  return rc;
148643}
148644
148645/*
148646** Examine a WherePath (with the addition of the extra WhereLoop of the 6th
148647** parameters) to see if it outputs rows in the requested ORDER BY
148648** (or GROUP BY) without requiring a separate sort operation.  Return N:
148649**
148650**   N>0:   N terms of the ORDER BY clause are satisfied
148651**   N==0:  No terms of the ORDER BY clause are satisfied
148652**   N<0:   Unknown yet how many terms of ORDER BY might be satisfied.
148653**
148654** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
148655** strict.  With GROUP BY and DISTINCT the only requirement is that
148656** equivalent rows appear immediately adjacent to one another.  GROUP BY
148657** and DISTINCT do not require rows to appear in any particular order as long
148658** as equivalent rows are grouped together.  Thus for GROUP BY and DISTINCT
148659** the pOrderBy terms can be matched in any order.  With ORDER BY, the
148660** pOrderBy terms must be matched in strict left-to-right order.
148661*/
148662static i8 wherePathSatisfiesOrderBy(
148663  WhereInfo *pWInfo,    /* The WHERE clause */
148664  ExprList *pOrderBy,   /* ORDER BY or GROUP BY or DISTINCT clause to check */
148665  WherePath *pPath,     /* The WherePath to check */
148666  u16 wctrlFlags,       /* WHERE_GROUPBY or _DISTINCTBY or _ORDERBY_LIMIT */
148667  u16 nLoop,            /* Number of entries in pPath->aLoop[] */
148668  WhereLoop *pLast,     /* Add this WhereLoop to the end of pPath->aLoop[] */
148669  Bitmask *pRevMask     /* OUT: Mask of WhereLoops to run in reverse order */
148670){
148671  u8 revSet;            /* True if rev is known */
148672  u8 rev;               /* Composite sort order */
148673  u8 revIdx;            /* Index sort order */
148674  u8 isOrderDistinct;   /* All prior WhereLoops are order-distinct */
148675  u8 distinctColumns;   /* True if the loop has UNIQUE NOT NULL columns */
148676  u8 isMatch;           /* iColumn matches a term of the ORDER BY clause */
148677  u16 eqOpMask;         /* Allowed equality operators */
148678  u16 nKeyCol;          /* Number of key columns in pIndex */
148679  u16 nColumn;          /* Total number of ordered columns in the index */
148680  u16 nOrderBy;         /* Number terms in the ORDER BY clause */
148681  int iLoop;            /* Index of WhereLoop in pPath being processed */
148682  int i, j;             /* Loop counters */
148683  int iCur;             /* Cursor number for current WhereLoop */
148684  int iColumn;          /* A column number within table iCur */
148685  WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
148686  WhereTerm *pTerm;     /* A single term of the WHERE clause */
148687  Expr *pOBExpr;        /* An expression from the ORDER BY clause */
148688  CollSeq *pColl;       /* COLLATE function from an ORDER BY clause term */
148689  Index *pIndex;        /* The index associated with pLoop */
148690  sqlite3 *db = pWInfo->pParse->db;  /* Database connection */
148691  Bitmask obSat = 0;    /* Mask of ORDER BY terms satisfied so far */
148692  Bitmask obDone;       /* Mask of all ORDER BY terms */
148693  Bitmask orderDistinctMask;  /* Mask of all well-ordered loops */
148694  Bitmask ready;              /* Mask of inner loops */
148695
148696  /*
148697  ** We say the WhereLoop is "one-row" if it generates no more than one
148698  ** row of output.  A WhereLoop is one-row if all of the following are true:
148699  **  (a) All index columns match with WHERE_COLUMN_EQ.
148700  **  (b) The index is unique
148701  ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
148702  ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
148703  **
148704  ** We say the WhereLoop is "order-distinct" if the set of columns from
148705  ** that WhereLoop that are in the ORDER BY clause are different for every
148706  ** row of the WhereLoop.  Every one-row WhereLoop is automatically
148707  ** order-distinct.   A WhereLoop that has no columns in the ORDER BY clause
148708  ** is not order-distinct. To be order-distinct is not quite the same as being
148709  ** UNIQUE since a UNIQUE column or index can have multiple rows that
148710  ** are NULL and NULL values are equivalent for the purpose of order-distinct.
148711  ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
148712  **
148713  ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
148714  ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
148715  ** automatically order-distinct.
148716  */
148717
148718  assert( pOrderBy!=0 );
148719  if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
148720
148721  nOrderBy = pOrderBy->nExpr;
148722  testcase( nOrderBy==BMS-1 );
148723  if( nOrderBy>BMS-1 ) return 0;  /* Cannot optimize overly large ORDER BYs */
148724  isOrderDistinct = 1;
148725  obDone = MASKBIT(nOrderBy)-1;
148726  orderDistinctMask = 0;
148727  ready = 0;
148728  eqOpMask = WO_EQ | WO_IS | WO_ISNULL;
148729  if( wctrlFlags & WHERE_ORDERBY_LIMIT ) eqOpMask |= WO_IN;
148730  for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
148731    if( iLoop>0 ) ready |= pLoop->maskSelf;
148732    if( iLoop<nLoop ){
148733      pLoop = pPath->aLoop[iLoop];
148734      if( wctrlFlags & WHERE_ORDERBY_LIMIT ) continue;
148735    }else{
148736      pLoop = pLast;
148737    }
148738    if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
148739      if( pLoop->u.vtab.isOrdered && (wctrlFlags & WHERE_DISTINCTBY)==0 ){
148740        obSat = obDone;
148741      }
148742      break;
148743    }else if( wctrlFlags & WHERE_DISTINCTBY ){
148744      pLoop->u.btree.nDistinctCol = 0;
148745    }
148746    iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
148747
148748    /* Mark off any ORDER BY term X that is a column in the table of
148749    ** the current loop for which there is term in the WHERE
148750    ** clause of the form X IS NULL or X=? that reference only outer
148751    ** loops.
148752    */
148753    for(i=0; i<nOrderBy; i++){
148754      if( MASKBIT(i) & obSat ) continue;
148755      pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
148756      if( pOBExpr->op!=TK_COLUMN ) continue;
148757      if( pOBExpr->iTable!=iCur ) continue;
148758      pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
148759                       ~ready, eqOpMask, 0);
148760      if( pTerm==0 ) continue;
148761      if( pTerm->eOperator==WO_IN ){
148762        /* IN terms are only valid for sorting in the ORDER BY LIMIT
148763        ** optimization, and then only if they are actually used
148764        ** by the query plan */
148765        assert( wctrlFlags & WHERE_ORDERBY_LIMIT );
148766        for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
148767        if( j>=pLoop->nLTerm ) continue;
148768      }
148769      if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
148770        Parse *pParse = pWInfo->pParse;
148771        CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[i].pExpr);
148772        CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr);
148773        assert( pColl1 );
148774        if( pColl2==0 || sqlite3StrICmp(pColl1->zName, pColl2->zName) ){
148775          continue;
148776        }
148777        testcase( pTerm->pExpr->op==TK_IS );
148778      }
148779      obSat |= MASKBIT(i);
148780    }
148781
148782    if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
148783      if( pLoop->wsFlags & WHERE_IPK ){
148784        pIndex = 0;
148785        nKeyCol = 0;
148786        nColumn = 1;
148787      }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
148788        return 0;
148789      }else{
148790        nKeyCol = pIndex->nKeyCol;
148791        nColumn = pIndex->nColumn;
148792        assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
148793        assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
148794                          || !HasRowid(pIndex->pTable));
148795        isOrderDistinct = IsUniqueIndex(pIndex)
148796                          && (pLoop->wsFlags & WHERE_SKIPSCAN)==0;
148797      }
148798
148799      /* Loop through all columns of the index and deal with the ones
148800      ** that are not constrained by == or IN.
148801      */
148802      rev = revSet = 0;
148803      distinctColumns = 0;
148804      for(j=0; j<nColumn; j++){
148805        u8 bOnce = 1; /* True to run the ORDER BY search loop */
148806
148807        assert( j>=pLoop->u.btree.nEq
148808            || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
148809        );
148810        if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
148811          u16 eOp = pLoop->aLTerm[j]->eOperator;
148812
148813          /* Skip over == and IS and ISNULL terms.  (Also skip IN terms when
148814          ** doing WHERE_ORDERBY_LIMIT processing).  Except, IS and ISNULL
148815          ** terms imply that the index is not UNIQUE NOT NULL in which case
148816          ** the loop need to be marked as not order-distinct because it can
148817          ** have repeated NULL rows.
148818          **
148819          ** If the current term is a column of an ((?,?) IN (SELECT...))
148820          ** expression for which the SELECT returns more than one column,
148821          ** check that it is the only column used by this loop. Otherwise,
148822          ** if it is one of two or more, none of the columns can be
148823          ** considered to match an ORDER BY term.
148824          */
148825          if( (eOp & eqOpMask)!=0 ){
148826            if( eOp & (WO_ISNULL|WO_IS) ){
148827              testcase( eOp & WO_ISNULL );
148828              testcase( eOp & WO_IS );
148829              testcase( isOrderDistinct );
148830              isOrderDistinct = 0;
148831            }
148832            continue;
148833          }else if( ALWAYS(eOp & WO_IN) ){
148834            /* ALWAYS() justification: eOp is an equality operator due to the
148835            ** j<pLoop->u.btree.nEq constraint above.  Any equality other
148836            ** than WO_IN is captured by the previous "if".  So this one
148837            ** always has to be WO_IN. */
148838            Expr *pX = pLoop->aLTerm[j]->pExpr;
148839            for(i=j+1; i<pLoop->u.btree.nEq; i++){
148840              if( pLoop->aLTerm[i]->pExpr==pX ){
148841                assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
148842                bOnce = 0;
148843                break;
148844              }
148845            }
148846          }
148847        }
148848
148849        /* Get the column number in the table (iColumn) and sort order
148850        ** (revIdx) for the j-th column of the index.
148851        */
148852        if( pIndex ){
148853          iColumn = pIndex->aiColumn[j];
148854          revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC;
148855          if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
148856        }else{
148857          iColumn = XN_ROWID;
148858          revIdx = 0;
148859        }
148860
148861        /* An unconstrained column that might be NULL means that this
148862        ** WhereLoop is not well-ordered
148863        */
148864        if( isOrderDistinct
148865         && iColumn>=0
148866         && j>=pLoop->u.btree.nEq
148867         && pIndex->pTable->aCol[iColumn].notNull==0
148868        ){
148869          isOrderDistinct = 0;
148870        }
148871
148872        /* Find the ORDER BY term that corresponds to the j-th column
148873        ** of the index and mark that ORDER BY term off
148874        */
148875        isMatch = 0;
148876        for(i=0; bOnce && i<nOrderBy; i++){
148877          if( MASKBIT(i) & obSat ) continue;
148878          pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
148879          testcase( wctrlFlags & WHERE_GROUPBY );
148880          testcase( wctrlFlags & WHERE_DISTINCTBY );
148881          if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0;
148882          if( iColumn>=XN_ROWID ){
148883            if( pOBExpr->op!=TK_COLUMN ) continue;
148884            if( pOBExpr->iTable!=iCur ) continue;
148885            if( pOBExpr->iColumn!=iColumn ) continue;
148886          }else{
148887            Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr;
148888            if( sqlite3ExprCompareSkip(pOBExpr, pIdxExpr, iCur) ){
148889              continue;
148890            }
148891          }
148892          if( iColumn!=XN_ROWID ){
148893            pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
148894            if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
148895          }
148896          if( wctrlFlags & WHERE_DISTINCTBY ){
148897            pLoop->u.btree.nDistinctCol = j+1;
148898          }
148899          isMatch = 1;
148900          break;
148901        }
148902        if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){
148903          /* Make sure the sort order is compatible in an ORDER BY clause.
148904          ** Sort order is irrelevant for a GROUP BY clause. */
148905          if( revSet ){
148906            if( (rev ^ revIdx)!=(pOrderBy->a[i].sortFlags&KEYINFO_ORDER_DESC) ){
148907              isMatch = 0;
148908            }
148909          }else{
148910            rev = revIdx ^ (pOrderBy->a[i].sortFlags & KEYINFO_ORDER_DESC);
148911            if( rev ) *pRevMask |= MASKBIT(iLoop);
148912            revSet = 1;
148913          }
148914        }
148915        if( isMatch && (pOrderBy->a[i].sortFlags & KEYINFO_ORDER_BIGNULL) ){
148916          if( j==pLoop->u.btree.nEq ){
148917            pLoop->wsFlags |= WHERE_BIGNULL_SORT;
148918          }else{
148919            isMatch = 0;
148920          }
148921        }
148922        if( isMatch ){
148923          if( iColumn==XN_ROWID ){
148924            testcase( distinctColumns==0 );
148925            distinctColumns = 1;
148926          }
148927          obSat |= MASKBIT(i);
148928        }else{
148929          /* No match found */
148930          if( j==0 || j<nKeyCol ){
148931            testcase( isOrderDistinct!=0 );
148932            isOrderDistinct = 0;
148933          }
148934          break;
148935        }
148936      } /* end Loop over all index columns */
148937      if( distinctColumns ){
148938        testcase( isOrderDistinct==0 );
148939        isOrderDistinct = 1;
148940      }
148941    } /* end-if not one-row */
148942
148943    /* Mark off any other ORDER BY terms that reference pLoop */
148944    if( isOrderDistinct ){
148945      orderDistinctMask |= pLoop->maskSelf;
148946      for(i=0; i<nOrderBy; i++){
148947        Expr *p;
148948        Bitmask mTerm;
148949        if( MASKBIT(i) & obSat ) continue;
148950        p = pOrderBy->a[i].pExpr;
148951        mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
148952        if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
148953        if( (mTerm&~orderDistinctMask)==0 ){
148954          obSat |= MASKBIT(i);
148955        }
148956      }
148957    }
148958  } /* End the loop over all WhereLoops from outer-most down to inner-most */
148959  if( obSat==obDone ) return (i8)nOrderBy;
148960  if( !isOrderDistinct ){
148961    for(i=nOrderBy-1; i>0; i--){
148962      Bitmask m = MASKBIT(i) - 1;
148963      if( (obSat&m)==m ) return i;
148964    }
148965    return 0;
148966  }
148967  return -1;
148968}
148969
148970
148971/*
148972** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(),
148973** the planner assumes that the specified pOrderBy list is actually a GROUP
148974** BY clause - and so any order that groups rows as required satisfies the
148975** request.
148976**
148977** Normally, in this case it is not possible for the caller to determine
148978** whether or not the rows are really being delivered in sorted order, or
148979** just in some other order that provides the required grouping. However,
148980** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then
148981** this function may be called on the returned WhereInfo object. It returns
148982** true if the rows really will be sorted in the specified order, or false
148983** otherwise.
148984**
148985** For example, assuming:
148986**
148987**   CREATE INDEX i1 ON t1(x, Y);
148988**
148989** then
148990**
148991**   SELECT * FROM t1 GROUP BY x,y ORDER BY x,y;   -- IsSorted()==1
148992**   SELECT * FROM t1 GROUP BY y,x ORDER BY y,x;   -- IsSorted()==0
148993*/
148994SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo *pWInfo){
148995  assert( pWInfo->wctrlFlags & WHERE_GROUPBY );
148996  assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
148997  return pWInfo->sorted;
148998}
148999
149000#ifdef WHERETRACE_ENABLED
149001/* For debugging use only: */
149002static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
149003  static char zName[65];
149004  int i;
149005  for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
149006  if( pLast ) zName[i++] = pLast->cId;
149007  zName[i] = 0;
149008  return zName;
149009}
149010#endif
149011
149012/*
149013** Return the cost of sorting nRow rows, assuming that the keys have
149014** nOrderby columns and that the first nSorted columns are already in
149015** order.
149016*/
149017static LogEst whereSortingCost(
149018  WhereInfo *pWInfo,
149019  LogEst nRow,
149020  int nOrderBy,
149021  int nSorted
149022){
149023  /* TUNING: Estimated cost of a full external sort, where N is
149024  ** the number of rows to sort is:
149025  **
149026  **   cost = (3.0 * N * log(N)).
149027  **
149028  ** Or, if the order-by clause has X terms but only the last Y
149029  ** terms are out of order, then block-sorting will reduce the
149030  ** sorting cost to:
149031  **
149032  **   cost = (3.0 * N * log(N)) * (Y/X)
149033  **
149034  ** The (Y/X) term is implemented using stack variable rScale
149035  ** below.  */
149036  LogEst rScale, rSortCost;
149037  assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
149038  rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
149039  rSortCost = nRow + rScale + 16;
149040
149041  /* Multiple by log(M) where M is the number of output rows.
149042  ** Use the LIMIT for M if it is smaller */
149043  if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
149044    nRow = pWInfo->iLimit;
149045  }
149046  rSortCost += estLog(nRow);
149047  return rSortCost;
149048}
149049
149050/*
149051** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
149052** attempts to find the lowest cost path that visits each WhereLoop
149053** once.  This path is then loaded into the pWInfo->a[].pWLoop fields.
149054**
149055** Assume that the total number of output rows that will need to be sorted
149056** will be nRowEst (in the 10*log2 representation).  Or, ignore sorting
149057** costs if nRowEst==0.
149058**
149059** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
149060** error occurs.
149061*/
149062static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
149063  int mxChoice;             /* Maximum number of simultaneous paths tracked */
149064  int nLoop;                /* Number of terms in the join */
149065  Parse *pParse;            /* Parsing context */
149066  sqlite3 *db;              /* The database connection */
149067  int iLoop;                /* Loop counter over the terms of the join */
149068  int ii, jj;               /* Loop counters */
149069  int mxI = 0;              /* Index of next entry to replace */
149070  int nOrderBy;             /* Number of ORDER BY clause terms */
149071  LogEst mxCost = 0;        /* Maximum cost of a set of paths */
149072  LogEst mxUnsorted = 0;    /* Maximum unsorted cost of a set of path */
149073  int nTo, nFrom;           /* Number of valid entries in aTo[] and aFrom[] */
149074  WherePath *aFrom;         /* All nFrom paths at the previous level */
149075  WherePath *aTo;           /* The nTo best paths at the current level */
149076  WherePath *pFrom;         /* An element of aFrom[] that we are working on */
149077  WherePath *pTo;           /* An element of aTo[] that we are working on */
149078  WhereLoop *pWLoop;        /* One of the WhereLoop objects */
149079  WhereLoop **pX;           /* Used to divy up the pSpace memory */
149080  LogEst *aSortCost = 0;    /* Sorting and partial sorting costs */
149081  char *pSpace;             /* Temporary memory used by this routine */
149082  int nSpace;               /* Bytes of space allocated at pSpace */
149083
149084  pParse = pWInfo->pParse;
149085  db = pParse->db;
149086  nLoop = pWInfo->nLevel;
149087  /* TUNING: For simple queries, only the best path is tracked.
149088  ** For 2-way joins, the 5 best paths are followed.
149089  ** For joins of 3 or more tables, track the 10 best paths */
149090  mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
149091  assert( nLoop<=pWInfo->pTabList->nSrc );
149092  WHERETRACE(0x002, ("---- begin solver.  (nRowEst=%d)\n", nRowEst));
149093
149094  /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this
149095  ** case the purpose of this call is to estimate the number of rows returned
149096  ** by the overall query. Once this estimate has been obtained, the caller
149097  ** will invoke this function a second time, passing the estimate as the
149098  ** nRowEst parameter.  */
149099  if( pWInfo->pOrderBy==0 || nRowEst==0 ){
149100    nOrderBy = 0;
149101  }else{
149102    nOrderBy = pWInfo->pOrderBy->nExpr;
149103  }
149104
149105  /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
149106  nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
149107  nSpace += sizeof(LogEst) * nOrderBy;
149108  pSpace = sqlite3DbMallocRawNN(db, nSpace);
149109  if( pSpace==0 ) return SQLITE_NOMEM_BKPT;
149110  aTo = (WherePath*)pSpace;
149111  aFrom = aTo+mxChoice;
149112  memset(aFrom, 0, sizeof(aFrom[0]));
149113  pX = (WhereLoop**)(aFrom+mxChoice);
149114  for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
149115    pFrom->aLoop = pX;
149116  }
149117  if( nOrderBy ){
149118    /* If there is an ORDER BY clause and it is not being ignored, set up
149119    ** space for the aSortCost[] array. Each element of the aSortCost array
149120    ** is either zero - meaning it has not yet been initialized - or the
149121    ** cost of sorting nRowEst rows of data where the first X terms of
149122    ** the ORDER BY clause are already in order, where X is the array
149123    ** index.  */
149124    aSortCost = (LogEst*)pX;
149125    memset(aSortCost, 0, sizeof(LogEst) * nOrderBy);
149126  }
149127  assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] );
149128  assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX );
149129
149130  /* Seed the search with a single WherePath containing zero WhereLoops.
149131  **
149132  ** TUNING: Do not let the number of iterations go above 28.  If the cost
149133  ** of computing an automatic index is not paid back within the first 28
149134  ** rows, then do not use the automatic index. */
149135  aFrom[0].nRow = MIN(pParse->nQueryLoop, 48);  assert( 48==sqlite3LogEst(28) );
149136  nFrom = 1;
149137  assert( aFrom[0].isOrdered==0 );
149138  if( nOrderBy ){
149139    /* If nLoop is zero, then there are no FROM terms in the query. Since
149140    ** in this case the query may return a maximum of one row, the results
149141    ** are already in the requested order. Set isOrdered to nOrderBy to
149142    ** indicate this. Or, if nLoop is greater than zero, set isOrdered to
149143    ** -1, indicating that the result set may or may not be ordered,
149144    ** depending on the loops added to the current plan.  */
149145    aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
149146  }
149147
149148  /* Compute successively longer WherePaths using the previous generation
149149  ** of WherePaths as the basis for the next.  Keep track of the mxChoice
149150  ** best paths at each generation */
149151  for(iLoop=0; iLoop<nLoop; iLoop++){
149152    nTo = 0;
149153    for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
149154      for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
149155        LogEst nOut;                      /* Rows visited by (pFrom+pWLoop) */
149156        LogEst rCost;                     /* Cost of path (pFrom+pWLoop) */
149157        LogEst rUnsorted;                 /* Unsorted cost of (pFrom+pWLoop) */
149158        i8 isOrdered = pFrom->isOrdered;  /* isOrdered for (pFrom+pWLoop) */
149159        Bitmask maskNew;                  /* Mask of src visited by (..) */
149160        Bitmask revMask = 0;              /* Mask of rev-order loops for (..) */
149161
149162        if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
149163        if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
149164        if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
149165          /* Do not use an automatic index if the this loop is expected
149166          ** to run less than 1.25 times.  It is tempting to also exclude
149167          ** automatic index usage on an outer loop, but sometimes an automatic
149168          ** index is useful in the outer loop of a correlated subquery. */
149169          assert( 10==sqlite3LogEst(2) );
149170          continue;
149171        }
149172
149173        /* At this point, pWLoop is a candidate to be the next loop.
149174        ** Compute its cost */
149175        rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
149176        rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
149177        nOut = pFrom->nRow + pWLoop->nOut;
149178        maskNew = pFrom->maskLoop | pWLoop->maskSelf;
149179        if( isOrdered<0 ){
149180          isOrdered = wherePathSatisfiesOrderBy(pWInfo,
149181                       pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
149182                       iLoop, pWLoop, &revMask);
149183        }else{
149184          revMask = pFrom->revLoop;
149185        }
149186        if( isOrdered>=0 && isOrdered<nOrderBy ){
149187          if( aSortCost[isOrdered]==0 ){
149188            aSortCost[isOrdered] = whereSortingCost(
149189                pWInfo, nRowEst, nOrderBy, isOrdered
149190            );
149191          }
149192          /* TUNING:  Add a small extra penalty (5) to sorting as an
149193          ** extra encouragment to the query planner to select a plan
149194          ** where the rows emerge in the correct order without any sorting
149195          ** required. */
149196          rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]) + 5;
149197
149198          WHERETRACE(0x002,
149199              ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
149200               aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
149201               rUnsorted, rCost));
149202        }else{
149203          rCost = rUnsorted;
149204          rUnsorted -= 2;  /* TUNING:  Slight bias in favor of no-sort plans */
149205        }
149206
149207        /* Check to see if pWLoop should be added to the set of
149208        ** mxChoice best-so-far paths.
149209        **
149210        ** First look for an existing path among best-so-far paths
149211        ** that covers the same set of loops and has the same isOrdered
149212        ** setting as the current path candidate.
149213        **
149214        ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
149215        ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
149216        ** of legal values for isOrdered, -1..64.
149217        */
149218        for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
149219          if( pTo->maskLoop==maskNew
149220           && ((pTo->isOrdered^isOrdered)&0x80)==0
149221          ){
149222            testcase( jj==nTo-1 );
149223            break;
149224          }
149225        }
149226        if( jj>=nTo ){
149227          /* None of the existing best-so-far paths match the candidate. */
149228          if( nTo>=mxChoice
149229           && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted))
149230          ){
149231            /* The current candidate is no better than any of the mxChoice
149232            ** paths currently in the best-so-far buffer.  So discard
149233            ** this candidate as not viable. */
149234#ifdef WHERETRACE_ENABLED /* 0x4 */
149235            if( sqlite3WhereTrace&0x4 ){
149236              sqlite3DebugPrintf("Skip   %s cost=%-3d,%3d,%3d order=%c\n",
149237                  wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
149238                  isOrdered>=0 ? isOrdered+'0' : '?');
149239            }
149240#endif
149241            continue;
149242          }
149243          /* If we reach this points it means that the new candidate path
149244          ** needs to be added to the set of best-so-far paths. */
149245          if( nTo<mxChoice ){
149246            /* Increase the size of the aTo set by one */
149247            jj = nTo++;
149248          }else{
149249            /* New path replaces the prior worst to keep count below mxChoice */
149250            jj = mxI;
149251          }
149252          pTo = &aTo[jj];
149253#ifdef WHERETRACE_ENABLED /* 0x4 */
149254          if( sqlite3WhereTrace&0x4 ){
149255            sqlite3DebugPrintf("New    %s cost=%-3d,%3d,%3d order=%c\n",
149256                wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
149257                isOrdered>=0 ? isOrdered+'0' : '?');
149258          }
149259#endif
149260        }else{
149261          /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
149262          ** same set of loops and has the same isOrdered setting as the
149263          ** candidate path.  Check to see if the candidate should replace
149264          ** pTo or if the candidate should be skipped.
149265          **
149266          ** The conditional is an expanded vector comparison equivalent to:
149267          **   (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
149268          */
149269          if( pTo->rCost<rCost
149270           || (pTo->rCost==rCost
149271               && (pTo->nRow<nOut
149272                   || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
149273                  )
149274              )
149275          ){
149276#ifdef WHERETRACE_ENABLED /* 0x4 */
149277            if( sqlite3WhereTrace&0x4 ){
149278              sqlite3DebugPrintf(
149279                  "Skip   %s cost=%-3d,%3d,%3d order=%c",
149280                  wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
149281                  isOrdered>=0 ? isOrdered+'0' : '?');
149282              sqlite3DebugPrintf("   vs %s cost=%-3d,%3d,%3d order=%c\n",
149283                  wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
149284                  pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
149285            }
149286#endif
149287            /* Discard the candidate path from further consideration */
149288            testcase( pTo->rCost==rCost );
149289            continue;
149290          }
149291          testcase( pTo->rCost==rCost+1 );
149292          /* Control reaches here if the candidate path is better than the
149293          ** pTo path.  Replace pTo with the candidate. */
149294#ifdef WHERETRACE_ENABLED /* 0x4 */
149295          if( sqlite3WhereTrace&0x4 ){
149296            sqlite3DebugPrintf(
149297                "Update %s cost=%-3d,%3d,%3d order=%c",
149298                wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
149299                isOrdered>=0 ? isOrdered+'0' : '?');
149300            sqlite3DebugPrintf("  was %s cost=%-3d,%3d,%3d order=%c\n",
149301                wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
149302                pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
149303          }
149304#endif
149305        }
149306        /* pWLoop is a winner.  Add it to the set of best so far */
149307        pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
149308        pTo->revLoop = revMask;
149309        pTo->nRow = nOut;
149310        pTo->rCost = rCost;
149311        pTo->rUnsorted = rUnsorted;
149312        pTo->isOrdered = isOrdered;
149313        memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
149314        pTo->aLoop[iLoop] = pWLoop;
149315        if( nTo>=mxChoice ){
149316          mxI = 0;
149317          mxCost = aTo[0].rCost;
149318          mxUnsorted = aTo[0].nRow;
149319          for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
149320            if( pTo->rCost>mxCost
149321             || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
149322            ){
149323              mxCost = pTo->rCost;
149324              mxUnsorted = pTo->rUnsorted;
149325              mxI = jj;
149326            }
149327          }
149328        }
149329      }
149330    }
149331
149332#ifdef WHERETRACE_ENABLED  /* >=2 */
149333    if( sqlite3WhereTrace & 0x02 ){
149334      sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
149335      for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
149336        sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
149337           wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
149338           pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
149339        if( pTo->isOrdered>0 ){
149340          sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
149341        }else{
149342          sqlite3DebugPrintf("\n");
149343        }
149344      }
149345    }
149346#endif
149347
149348    /* Swap the roles of aFrom and aTo for the next generation */
149349    pFrom = aTo;
149350    aTo = aFrom;
149351    aFrom = pFrom;
149352    nFrom = nTo;
149353  }
149354
149355  if( nFrom==0 ){
149356    sqlite3ErrorMsg(pParse, "no query solution");
149357    sqlite3DbFreeNN(db, pSpace);
149358    return SQLITE_ERROR;
149359  }
149360
149361  /* Find the lowest cost path.  pFrom will be left pointing to that path */
149362  pFrom = aFrom;
149363  for(ii=1; ii<nFrom; ii++){
149364    if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
149365  }
149366  assert( pWInfo->nLevel==nLoop );
149367  /* Load the lowest cost path into pWInfo */
149368  for(iLoop=0; iLoop<nLoop; iLoop++){
149369    WhereLevel *pLevel = pWInfo->a + iLoop;
149370    pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
149371    pLevel->iFrom = pWLoop->iTab;
149372    pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
149373  }
149374  if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
149375   && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
149376   && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
149377   && nRowEst
149378  ){
149379    Bitmask notUsed;
149380    int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
149381                 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
149382    if( rc==pWInfo->pResultSet->nExpr ){
149383      pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
149384    }
149385  }
149386  pWInfo->bOrderedInnerLoop = 0;
149387  if( pWInfo->pOrderBy ){
149388    if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
149389      if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
149390        pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
149391      }
149392    }else{
149393      pWInfo->nOBSat = pFrom->isOrdered;
149394      pWInfo->revMask = pFrom->revLoop;
149395      if( pWInfo->nOBSat<=0 ){
149396        pWInfo->nOBSat = 0;
149397        if( nLoop>0 ){
149398          u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
149399          if( (wsFlags & WHERE_ONEROW)==0
149400           && (wsFlags&(WHERE_IPK|WHERE_COLUMN_IN))!=(WHERE_IPK|WHERE_COLUMN_IN)
149401          ){
149402            Bitmask m = 0;
149403            int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
149404                      WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m);
149405            testcase( wsFlags & WHERE_IPK );
149406            testcase( wsFlags & WHERE_COLUMN_IN );
149407            if( rc==pWInfo->pOrderBy->nExpr ){
149408              pWInfo->bOrderedInnerLoop = 1;
149409              pWInfo->revMask = m;
149410            }
149411          }
149412        }
149413      }
149414    }
149415    if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
149416        && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
149417    ){
149418      Bitmask revMask = 0;
149419      int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
149420          pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
149421      );
149422      assert( pWInfo->sorted==0 );
149423      if( nOrder==pWInfo->pOrderBy->nExpr ){
149424        pWInfo->sorted = 1;
149425        pWInfo->revMask = revMask;
149426      }
149427    }
149428  }
149429
149430
149431  pWInfo->nRowOut = pFrom->nRow;
149432
149433  /* Free temporary memory and return success */
149434  sqlite3DbFreeNN(db, pSpace);
149435  return SQLITE_OK;
149436}
149437
149438/*
149439** Most queries use only a single table (they are not joins) and have
149440** simple == constraints against indexed fields.  This routine attempts
149441** to plan those simple cases using much less ceremony than the
149442** general-purpose query planner, and thereby yield faster sqlite3_prepare()
149443** times for the common case.
149444**
149445** Return non-zero on success, if this query can be handled by this
149446** no-frills query planner.  Return zero if this query needs the
149447** general-purpose query planner.
149448*/
149449static int whereShortCut(WhereLoopBuilder *pBuilder){
149450  WhereInfo *pWInfo;
149451  struct SrcList_item *pItem;
149452  WhereClause *pWC;
149453  WhereTerm *pTerm;
149454  WhereLoop *pLoop;
149455  int iCur;
149456  int j;
149457  Table *pTab;
149458  Index *pIdx;
149459
149460  pWInfo = pBuilder->pWInfo;
149461  if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
149462  assert( pWInfo->pTabList->nSrc>=1 );
149463  pItem = pWInfo->pTabList->a;
149464  pTab = pItem->pTab;
149465  if( IsVirtual(pTab) ) return 0;
149466  if( pItem->fg.isIndexedBy ) return 0;
149467  iCur = pItem->iCursor;
149468  pWC = &pWInfo->sWC;
149469  pLoop = pBuilder->pNew;
149470  pLoop->wsFlags = 0;
149471  pLoop->nSkip = 0;
149472  pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
149473  if( pTerm ){
149474    testcase( pTerm->eOperator & WO_IS );
149475    pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
149476    pLoop->aLTerm[0] = pTerm;
149477    pLoop->nLTerm = 1;
149478    pLoop->u.btree.nEq = 1;
149479    /* TUNING: Cost of a rowid lookup is 10 */
149480    pLoop->rRun = 33;  /* 33==sqlite3LogEst(10) */
149481  }else{
149482    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
149483      int opMask;
149484      assert( pLoop->aLTermSpace==pLoop->aLTerm );
149485      if( !IsUniqueIndex(pIdx)
149486       || pIdx->pPartIdxWhere!=0
149487       || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
149488      ) continue;
149489      opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
149490      for(j=0; j<pIdx->nKeyCol; j++){
149491        pTerm = sqlite3WhereFindTerm(pWC, iCur, j, 0, opMask, pIdx);
149492        if( pTerm==0 ) break;
149493        testcase( pTerm->eOperator & WO_IS );
149494        pLoop->aLTerm[j] = pTerm;
149495      }
149496      if( j!=pIdx->nKeyCol ) continue;
149497      pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
149498      if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
149499        pLoop->wsFlags |= WHERE_IDX_ONLY;
149500      }
149501      pLoop->nLTerm = j;
149502      pLoop->u.btree.nEq = j;
149503      pLoop->u.btree.pIndex = pIdx;
149504      /* TUNING: Cost of a unique index lookup is 15 */
149505      pLoop->rRun = 39;  /* 39==sqlite3LogEst(15) */
149506      break;
149507    }
149508  }
149509  if( pLoop->wsFlags ){
149510    pLoop->nOut = (LogEst)1;
149511    pWInfo->a[0].pWLoop = pLoop;
149512    assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
149513    pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
149514    pWInfo->a[0].iTabCur = iCur;
149515    pWInfo->nRowOut = 1;
149516    if( pWInfo->pOrderBy ) pWInfo->nOBSat =  pWInfo->pOrderBy->nExpr;
149517    if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
149518      pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
149519    }
149520#ifdef SQLITE_DEBUG
149521    pLoop->cId = '0';
149522#endif
149523    return 1;
149524  }
149525  return 0;
149526}
149527
149528/*
149529** Helper function for exprIsDeterministic().
149530*/
149531static int exprNodeIsDeterministic(Walker *pWalker, Expr *pExpr){
149532  if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
149533    pWalker->eCode = 0;
149534    return WRC_Abort;
149535  }
149536  return WRC_Continue;
149537}
149538
149539/*
149540** Return true if the expression contains no non-deterministic SQL
149541** functions. Do not consider non-deterministic SQL functions that are
149542** part of sub-select statements.
149543*/
149544static int exprIsDeterministic(Expr *p){
149545  Walker w;
149546  memset(&w, 0, sizeof(w));
149547  w.eCode = 1;
149548  w.xExprCallback = exprNodeIsDeterministic;
149549  w.xSelectCallback = sqlite3SelectWalkFail;
149550  sqlite3WalkExpr(&w, p);
149551  return w.eCode;
149552}
149553
149554
149555#ifdef WHERETRACE_ENABLED
149556/*
149557** Display all WhereLoops in pWInfo
149558*/
149559static void showAllWhereLoops(WhereInfo *pWInfo, WhereClause *pWC){
149560  if( sqlite3WhereTrace ){    /* Display all of the WhereLoop objects */
149561    WhereLoop *p;
149562    int i;
149563    static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
149564                                           "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
149565    for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
149566      p->cId = zLabel[i%(sizeof(zLabel)-1)];
149567      sqlite3WhereLoopPrint(p, pWC);
149568    }
149569  }
149570}
149571# define WHERETRACE_ALL_LOOPS(W,C) showAllWhereLoops(W,C)
149572#else
149573# define WHERETRACE_ALL_LOOPS(W,C)
149574#endif
149575
149576/*
149577** Generate the beginning of the loop used for WHERE clause processing.
149578** The return value is a pointer to an opaque structure that contains
149579** information needed to terminate the loop.  Later, the calling routine
149580** should invoke sqlite3WhereEnd() with the return value of this function
149581** in order to complete the WHERE clause processing.
149582**
149583** If an error occurs, this routine returns NULL.
149584**
149585** The basic idea is to do a nested loop, one loop for each table in
149586** the FROM clause of a select.  (INSERT and UPDATE statements are the
149587** same as a SELECT with only a single table in the FROM clause.)  For
149588** example, if the SQL is this:
149589**
149590**       SELECT * FROM t1, t2, t3 WHERE ...;
149591**
149592** Then the code generated is conceptually like the following:
149593**
149594**      foreach row1 in t1 do       \    Code generated
149595**        foreach row2 in t2 do      |-- by sqlite3WhereBegin()
149596**          foreach row3 in t3 do   /
149597**            ...
149598**          end                     \    Code generated
149599**        end                        |-- by sqlite3WhereEnd()
149600**      end                         /
149601**
149602** Note that the loops might not be nested in the order in which they
149603** appear in the FROM clause if a different order is better able to make
149604** use of indices.  Note also that when the IN operator appears in
149605** the WHERE clause, it might result in additional nested loops for
149606** scanning through all values on the right-hand side of the IN.
149607**
149608** There are Btree cursors associated with each table.  t1 uses cursor
149609** number pTabList->a[0].iCursor.  t2 uses the cursor pTabList->a[1].iCursor.
149610** And so forth.  This routine generates code to open those VDBE cursors
149611** and sqlite3WhereEnd() generates the code to close them.
149612**
149613** The code that sqlite3WhereBegin() generates leaves the cursors named
149614** in pTabList pointing at their appropriate entries.  The [...] code
149615** can use OP_Column and OP_Rowid opcodes on these cursors to extract
149616** data from the various tables of the loop.
149617**
149618** If the WHERE clause is empty, the foreach loops must each scan their
149619** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
149620** the tables have indices and there are terms in the WHERE clause that
149621** refer to those indices, a complete table scan can be avoided and the
149622** code will run much faster.  Most of the work of this routine is checking
149623** to see if there are indices that can be used to speed up the loop.
149624**
149625** Terms of the WHERE clause are also used to limit which rows actually
149626** make it to the "..." in the middle of the loop.  After each "foreach",
149627** terms of the WHERE clause that use only terms in that loop and outer
149628** loops are evaluated and if false a jump is made around all subsequent
149629** inner loops (or around the "..." if the test occurs within the inner-
149630** most loop)
149631**
149632** OUTER JOINS
149633**
149634** An outer join of tables t1 and t2 is conceptally coded as follows:
149635**
149636**    foreach row1 in t1 do
149637**      flag = 0
149638**      foreach row2 in t2 do
149639**        start:
149640**          ...
149641**          flag = 1
149642**      end
149643**      if flag==0 then
149644**        move the row2 cursor to a null row
149645**        goto start
149646**      fi
149647**    end
149648**
149649** ORDER BY CLAUSE PROCESSING
149650**
149651** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
149652** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
149653** if there is one.  If there is no ORDER BY clause or if this routine
149654** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
149655**
149656** The iIdxCur parameter is the cursor number of an index.  If
149657** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index
149658** to use for OR clause processing.  The WHERE clause should use this
149659** specific cursor.  If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
149660** the first cursor in an array of cursors for all indices.  iIdxCur should
149661** be used to compute the appropriate cursor depending on which index is
149662** used.
149663*/
149664SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
149665  Parse *pParse,          /* The parser context */
149666  SrcList *pTabList,      /* FROM clause: A list of all tables to be scanned */
149667  Expr *pWhere,           /* The WHERE clause */
149668  ExprList *pOrderBy,     /* An ORDER BY (or GROUP BY) clause, or NULL */
149669  ExprList *pResultSet,   /* Query result set.  Req'd for DISTINCT */
149670  u16 wctrlFlags,         /* The WHERE_* flags defined in sqliteInt.h */
149671  int iAuxArg             /* If WHERE_OR_SUBCLAUSE is set, index cursor number
149672                          ** If WHERE_USE_LIMIT, then the limit amount */
149673){
149674  int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
149675  int nTabList;              /* Number of elements in pTabList */
149676  WhereInfo *pWInfo;         /* Will become the return value of this function */
149677  Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
149678  Bitmask notReady;          /* Cursors that are not yet positioned */
149679  WhereLoopBuilder sWLB;     /* The WhereLoop builder */
149680  WhereMaskSet *pMaskSet;    /* The expression mask set */
149681  WhereLevel *pLevel;        /* A single level in pWInfo->a[] */
149682  WhereLoop *pLoop;          /* Pointer to a single WhereLoop object */
149683  int ii;                    /* Loop counter */
149684  sqlite3 *db;               /* Database connection */
149685  int rc;                    /* Return code */
149686  u8 bFordelete = 0;         /* OPFLAG_FORDELETE or zero, as appropriate */
149687
149688  assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || (
149689        (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
149690     && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0
149691  ));
149692
149693  /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */
149694  assert( (wctrlFlags & WHERE_OR_SUBCLAUSE)==0
149695            || (wctrlFlags & WHERE_USE_LIMIT)==0 );
149696
149697  /* Variable initialization */
149698  db = pParse->db;
149699  memset(&sWLB, 0, sizeof(sWLB));
149700
149701  /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */
149702  testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
149703  if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
149704  sWLB.pOrderBy = pOrderBy;
149705
149706  /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
149707  ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
149708  if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){
149709    wctrlFlags &= ~WHERE_WANT_DISTINCT;
149710  }
149711
149712  /* The number of tables in the FROM clause is limited by the number of
149713  ** bits in a Bitmask
149714  */
149715  testcase( pTabList->nSrc==BMS );
149716  if( pTabList->nSrc>BMS ){
149717    sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
149718    return 0;
149719  }
149720
149721  /* This function normally generates a nested loop for all tables in
149722  ** pTabList.  But if the WHERE_OR_SUBCLAUSE flag is set, then we should
149723  ** only generate code for the first table in pTabList and assume that
149724  ** any cursors associated with subsequent tables are uninitialized.
149725  */
149726  nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
149727
149728  /* Allocate and initialize the WhereInfo structure that will become the
149729  ** return value. A single allocation is used to store the WhereInfo
149730  ** struct, the contents of WhereInfo.a[], the WhereClause structure
149731  ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
149732  ** field (type Bitmask) it must be aligned on an 8-byte boundary on
149733  ** some architectures. Hence the ROUND8() below.
149734  */
149735  nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
149736  pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop));
149737  if( db->mallocFailed ){
149738    sqlite3DbFree(db, pWInfo);
149739    pWInfo = 0;
149740    goto whereBeginError;
149741  }
149742  pWInfo->pParse = pParse;
149743  pWInfo->pTabList = pTabList;
149744  pWInfo->pOrderBy = pOrderBy;
149745  pWInfo->pWhere = pWhere;
149746  pWInfo->pResultSet = pResultSet;
149747  pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
149748  pWInfo->nLevel = nTabList;
149749  pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
149750  pWInfo->wctrlFlags = wctrlFlags;
149751  pWInfo->iLimit = iAuxArg;
149752  pWInfo->savedNQueryLoop = pParse->nQueryLoop;
149753  memset(&pWInfo->nOBSat, 0,
149754         offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
149755  memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
149756  assert( pWInfo->eOnePass==ONEPASS_OFF );  /* ONEPASS defaults to OFF */
149757  pMaskSet = &pWInfo->sMaskSet;
149758  sWLB.pWInfo = pWInfo;
149759  sWLB.pWC = &pWInfo->sWC;
149760  sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
149761  assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) );
149762  whereLoopInit(sWLB.pNew);
149763#ifdef SQLITE_DEBUG
149764  sWLB.pNew->cId = '*';
149765#endif
149766
149767  /* Split the WHERE clause into separate subexpressions where each
149768  ** subexpression is separated by an AND operator.
149769  */
149770  initMaskSet(pMaskSet);
149771  sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
149772  sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
149773
149774  /* Special case: No FROM clause
149775  */
149776  if( nTabList==0 ){
149777    if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
149778    if( wctrlFlags & WHERE_WANT_DISTINCT ){
149779      pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
149780    }
149781    ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"));
149782  }else{
149783    /* Assign a bit from the bitmask to every term in the FROM clause.
149784    **
149785    ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
149786    **
149787    ** The rule of the previous sentence ensures thta if X is the bitmask for
149788    ** a table T, then X-1 is the bitmask for all other tables to the left of T.
149789    ** Knowing the bitmask for all tables to the left of a left join is
149790    ** important.  Ticket #3015.
149791    **
149792    ** Note that bitmasks are created for all pTabList->nSrc tables in
149793    ** pTabList, not just the first nTabList tables.  nTabList is normally
149794    ** equal to pTabList->nSrc but might be shortened to 1 if the
149795    ** WHERE_OR_SUBCLAUSE flag is set.
149796    */
149797    ii = 0;
149798    do{
149799      createMask(pMaskSet, pTabList->a[ii].iCursor);
149800      sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
149801    }while( (++ii)<pTabList->nSrc );
149802  #ifdef SQLITE_DEBUG
149803    {
149804      Bitmask mx = 0;
149805      for(ii=0; ii<pTabList->nSrc; ii++){
149806        Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
149807        assert( m>=mx );
149808        mx = m;
149809      }
149810    }
149811  #endif
149812  }
149813
149814  /* Analyze all of the subexpressions. */
149815  sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
149816  if( db->mallocFailed ) goto whereBeginError;
149817
149818  /* Special case: WHERE terms that do not refer to any tables in the join
149819  ** (constant expressions). Evaluate each such term, and jump over all the
149820  ** generated code if the result is not true.
149821  **
149822  ** Do not do this if the expression contains non-deterministic functions
149823  ** that are not within a sub-select. This is not strictly required, but
149824  ** preserves SQLite's legacy behaviour in the following two cases:
149825  **
149826  **   FROM ... WHERE random()>0;           -- eval random() once per row
149827  **   FROM ... WHERE (SELECT random())>0;  -- eval random() once overall
149828  */
149829  for(ii=0; ii<sWLB.pWC->nTerm; ii++){
149830    WhereTerm *pT = &sWLB.pWC->a[ii];
149831    if( pT->wtFlags & TERM_VIRTUAL ) continue;
149832    if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
149833      sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
149834      pT->wtFlags |= TERM_CODED;
149835    }
149836  }
149837
149838  if( wctrlFlags & WHERE_WANT_DISTINCT ){
149839    if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
149840      /* The DISTINCT marking is pointless.  Ignore it. */
149841      pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
149842    }else if( pOrderBy==0 ){
149843      /* Try to ORDER BY the result set to make distinct processing easier */
149844      pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
149845      pWInfo->pOrderBy = pResultSet;
149846    }
149847  }
149848
149849  /* Construct the WhereLoop objects */
149850#if defined(WHERETRACE_ENABLED)
149851  if( sqlite3WhereTrace & 0xffff ){
149852    sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags);
149853    if( wctrlFlags & WHERE_USE_LIMIT ){
149854      sqlite3DebugPrintf(", limit: %d", iAuxArg);
149855    }
149856    sqlite3DebugPrintf(")\n");
149857    if( sqlite3WhereTrace & 0x100 ){
149858      Select sSelect;
149859      memset(&sSelect, 0, sizeof(sSelect));
149860      sSelect.selFlags = SF_WhereBegin;
149861      sSelect.pSrc = pTabList;
149862      sSelect.pWhere = pWhere;
149863      sSelect.pOrderBy = pOrderBy;
149864      sSelect.pEList = pResultSet;
149865      sqlite3TreeViewSelect(0, &sSelect, 0);
149866    }
149867  }
149868  if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
149869    sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
149870    sqlite3WhereClausePrint(sWLB.pWC);
149871  }
149872#endif
149873
149874  if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
149875    rc = whereLoopAddAll(&sWLB);
149876    if( rc ) goto whereBeginError;
149877
149878#ifdef SQLITE_ENABLE_STAT4
149879    /* If one or more WhereTerm.truthProb values were used in estimating
149880    ** loop parameters, but then those truthProb values were subsequently
149881    ** changed based on STAT4 information while computing subsequent loops,
149882    ** then we need to rerun the whole loop building process so that all
149883    ** loops will be built using the revised truthProb values. */
149884    if( sWLB.bldFlags2 & SQLITE_BLDF2_2NDPASS ){
149885      WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
149886      WHERETRACE(0xffff,
149887           ("**** Redo all loop computations due to"
149888            " TERM_HIGHTRUTH changes ****\n"));
149889      while( pWInfo->pLoops ){
149890        WhereLoop *p = pWInfo->pLoops;
149891        pWInfo->pLoops = p->pNextLoop;
149892        whereLoopDelete(db, p);
149893      }
149894      rc = whereLoopAddAll(&sWLB);
149895      if( rc ) goto whereBeginError;
149896    }
149897#endif
149898    WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
149899
149900    wherePathSolver(pWInfo, 0);
149901    if( db->mallocFailed ) goto whereBeginError;
149902    if( pWInfo->pOrderBy ){
149903       wherePathSolver(pWInfo, pWInfo->nRowOut+1);
149904       if( db->mallocFailed ) goto whereBeginError;
149905    }
149906  }
149907  if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
149908     pWInfo->revMask = ALLBITS;
149909  }
149910  if( pParse->nErr || NEVER(db->mallocFailed) ){
149911    goto whereBeginError;
149912  }
149913#ifdef WHERETRACE_ENABLED
149914  if( sqlite3WhereTrace ){
149915    sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
149916    if( pWInfo->nOBSat>0 ){
149917      sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
149918    }
149919    switch( pWInfo->eDistinct ){
149920      case WHERE_DISTINCT_UNIQUE: {
149921        sqlite3DebugPrintf("  DISTINCT=unique");
149922        break;
149923      }
149924      case WHERE_DISTINCT_ORDERED: {
149925        sqlite3DebugPrintf("  DISTINCT=ordered");
149926        break;
149927      }
149928      case WHERE_DISTINCT_UNORDERED: {
149929        sqlite3DebugPrintf("  DISTINCT=unordered");
149930        break;
149931      }
149932    }
149933    sqlite3DebugPrintf("\n");
149934    for(ii=0; ii<pWInfo->nLevel; ii++){
149935      sqlite3WhereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
149936    }
149937  }
149938#endif
149939
149940  /* Attempt to omit tables from the join that do not affect the result.
149941  ** For a table to not affect the result, the following must be true:
149942  **
149943  **   1) The query must not be an aggregate.
149944  **   2) The table must be the RHS of a LEFT JOIN.
149945  **   3) Either the query must be DISTINCT, or else the ON or USING clause
149946  **      must contain a constraint that limits the scan of the table to
149947  **      at most a single row.
149948  **   4) The table must not be referenced by any part of the query apart
149949  **      from its own USING or ON clause.
149950  **
149951  ** For example, given:
149952  **
149953  **     CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1);
149954  **     CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2);
149955  **     CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3);
149956  **
149957  ** then table t2 can be omitted from the following:
149958  **
149959  **     SELECT v1, v3 FROM t1
149960  **       LEFT JOIN t2 ON (t1.ipk=t2.ipk)
149961  **       LEFT JOIN t3 ON (t1.ipk=t3.ipk)
149962  **
149963  ** or from:
149964  **
149965  **     SELECT DISTINCT v1, v3 FROM t1
149966  **       LEFT JOIN t2
149967  **       LEFT JOIN t3 ON (t1.ipk=t3.ipk)
149968  */
149969  notReady = ~(Bitmask)0;
149970  if( pWInfo->nLevel>=2
149971   && pResultSet!=0               /* guarantees condition (1) above */
149972   && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
149973  ){
149974    int i;
149975    Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet);
149976    if( sWLB.pOrderBy ){
149977      tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy);
149978    }
149979    for(i=pWInfo->nLevel-1; i>=1; i--){
149980      WhereTerm *pTerm, *pEnd;
149981      struct SrcList_item *pItem;
149982      pLoop = pWInfo->a[i].pWLoop;
149983      pItem = &pWInfo->pTabList->a[pLoop->iTab];
149984      if( (pItem->fg.jointype & JT_LEFT)==0 ) continue;
149985      if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
149986       && (pLoop->wsFlags & WHERE_ONEROW)==0
149987      ){
149988        continue;
149989      }
149990      if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
149991      pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
149992      for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
149993        if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
149994          if( !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
149995           || pTerm->pExpr->iRightJoinTable!=pItem->iCursor
149996          ){
149997            break;
149998          }
149999        }
150000      }
150001      if( pTerm<pEnd ) continue;
150002      WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
150003      notReady &= ~pLoop->maskSelf;
150004      for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
150005        if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
150006          pTerm->wtFlags |= TERM_CODED;
150007        }
150008      }
150009      if( i!=pWInfo->nLevel-1 ){
150010        int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
150011        memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
150012      }
150013      pWInfo->nLevel--;
150014      nTabList--;
150015    }
150016  }
150017#if defined(WHERETRACE_ENABLED)
150018  if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
150019    sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
150020    sqlite3WhereClausePrint(sWLB.pWC);
150021  }
150022  WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
150023#endif
150024  pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
150025
150026  /* If the caller is an UPDATE or DELETE statement that is requesting
150027  ** to use a one-pass algorithm, determine if this is appropriate.
150028  **
150029  ** A one-pass approach can be used if the caller has requested one
150030  ** and either (a) the scan visits at most one row or (b) each
150031  ** of the following are true:
150032  **
150033  **   * the caller has indicated that a one-pass approach can be used
150034  **     with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and
150035  **   * the table is not a virtual table, and
150036  **   * either the scan does not use the OR optimization or the caller
150037  **     is a DELETE operation (WHERE_DUPLICATES_OK is only specified
150038  **     for DELETE).
150039  **
150040  ** The last qualification is because an UPDATE statement uses
150041  ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can
150042  ** use a one-pass approach, and this is not set accurately for scans
150043  ** that use the OR optimization.
150044  */
150045  assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
150046  if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){
150047    int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
150048    int bOnerow = (wsFlags & WHERE_ONEROW)!=0;
150049    assert( !(wsFlags & WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pTab) );
150050    if( bOnerow || (
150051        0!=(wctrlFlags & WHERE_ONEPASS_MULTIROW)
150052     && !IsVirtual(pTabList->a[0].pTab)
150053     && (0==(wsFlags & WHERE_MULTI_OR) || (wctrlFlags & WHERE_DUPLICATES_OK))
150054    )){
150055      pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
150056      if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
150057        if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){
150058          bFordelete = OPFLAG_FORDELETE;
150059        }
150060        pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
150061      }
150062    }
150063  }
150064
150065  /* Open all tables in the pTabList and any indices selected for
150066  ** searching those tables.
150067  */
150068  for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
150069    Table *pTab;     /* Table to open */
150070    int iDb;         /* Index of database containing table/index */
150071    struct SrcList_item *pTabItem;
150072
150073    pTabItem = &pTabList->a[pLevel->iFrom];
150074    pTab = pTabItem->pTab;
150075    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
150076    pLoop = pLevel->pWLoop;
150077    if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
150078      /* Do nothing */
150079    }else
150080#ifndef SQLITE_OMIT_VIRTUALTABLE
150081    if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
150082      const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
150083      int iCur = pTabItem->iCursor;
150084      sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
150085    }else if( IsVirtual(pTab) ){
150086      /* noop */
150087    }else
150088#endif
150089    if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
150090         && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){
150091      int op = OP_OpenRead;
150092      if( pWInfo->eOnePass!=ONEPASS_OFF ){
150093        op = OP_OpenWrite;
150094        pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
150095      };
150096      sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
150097      assert( pTabItem->iCursor==pLevel->iTabCur );
150098      testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
150099      testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
150100      if( pWInfo->eOnePass==ONEPASS_OFF
150101       && pTab->nCol<BMS
150102       && (pTab->tabFlags & (TF_HasGenerated|TF_WithoutRowid))==0
150103      ){
150104        /* If we know that only a prefix of the record will be used,
150105        ** it is advantageous to reduce the "column count" field in
150106        ** the P4 operand of the OP_OpenRead/Write opcode. */
150107        Bitmask b = pTabItem->colUsed;
150108        int n = 0;
150109        for(; b; b=b>>1, n++){}
150110        sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32);
150111        assert( n<=pTab->nCol );
150112      }
150113#ifdef SQLITE_ENABLE_CURSOR_HINTS
150114      if( pLoop->u.btree.pIndex!=0 ){
150115        sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ|bFordelete);
150116      }else
150117#endif
150118      {
150119        sqlite3VdbeChangeP5(v, bFordelete);
150120      }
150121#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
150122      sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
150123                            (const u8*)&pTabItem->colUsed, P4_INT64);
150124#endif
150125    }else{
150126      sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
150127    }
150128    if( pLoop->wsFlags & WHERE_INDEXED ){
150129      Index *pIx = pLoop->u.btree.pIndex;
150130      int iIndexCur;
150131      int op = OP_OpenRead;
150132      /* iAuxArg is always set to a positive value if ONEPASS is possible */
150133      assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
150134      if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx)
150135       && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0
150136      ){
150137        /* This is one term of an OR-optimization using the PRIMARY KEY of a
150138        ** WITHOUT ROWID table.  No need for a separate index */
150139        iIndexCur = pLevel->iTabCur;
150140        op = 0;
150141      }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
150142        Index *pJ = pTabItem->pTab->pIndex;
150143        iIndexCur = iAuxArg;
150144        assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
150145        while( ALWAYS(pJ) && pJ!=pIx ){
150146          iIndexCur++;
150147          pJ = pJ->pNext;
150148        }
150149        op = OP_OpenWrite;
150150        pWInfo->aiCurOnePass[1] = iIndexCur;
150151      }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){
150152        iIndexCur = iAuxArg;
150153        op = OP_ReopenIdx;
150154      }else{
150155        iIndexCur = pParse->nTab++;
150156      }
150157      pLevel->iIdxCur = iIndexCur;
150158      assert( pIx->pSchema==pTab->pSchema );
150159      assert( iIndexCur>=0 );
150160      if( op ){
150161        sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
150162        sqlite3VdbeSetP4KeyInfo(pParse, pIx);
150163        if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
150164         && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
150165         && (pLoop->wsFlags & WHERE_BIGNULL_SORT)==0
150166         && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
150167         && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
150168        ){
150169          sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ);
150170        }
150171        VdbeComment((v, "%s", pIx->zName));
150172#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
150173        {
150174          u64 colUsed = 0;
150175          int ii, jj;
150176          for(ii=0; ii<pIx->nColumn; ii++){
150177            jj = pIx->aiColumn[ii];
150178            if( jj<0 ) continue;
150179            if( jj>63 ) jj = 63;
150180            if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
150181            colUsed |= ((u64)1)<<(ii<63 ? ii : 63);
150182          }
150183          sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, iIndexCur, 0, 0,
150184                                (u8*)&colUsed, P4_INT64);
150185        }
150186#endif /* SQLITE_ENABLE_COLUMN_USED_MASK */
150187      }
150188    }
150189    if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
150190  }
150191  pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
150192  if( db->mallocFailed ) goto whereBeginError;
150193
150194  /* Generate the code to do the search.  Each iteration of the for
150195  ** loop below generates code for a single nested loop of the VM
150196  ** program.
150197  */
150198  for(ii=0; ii<nTabList; ii++){
150199    int addrExplain;
150200    int wsFlags;
150201    pLevel = &pWInfo->a[ii];
150202    wsFlags = pLevel->pWLoop->wsFlags;
150203#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
150204    if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
150205      constructAutomaticIndex(pParse, &pWInfo->sWC,
150206                &pTabList->a[pLevel->iFrom], notReady, pLevel);
150207      if( db->mallocFailed ) goto whereBeginError;
150208    }
150209#endif
150210    addrExplain = sqlite3WhereExplainOneScan(
150211        pParse, pTabList, pLevel, wctrlFlags
150212    );
150213    pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
150214    notReady = sqlite3WhereCodeOneLoopStart(pParse,v,pWInfo,ii,pLevel,notReady);
150215    pWInfo->iContinue = pLevel->addrCont;
150216    if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE)==0 ){
150217      sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
150218    }
150219  }
150220
150221  /* Done. */
150222  VdbeModuleComment((v, "Begin WHERE-core"));
150223  return pWInfo;
150224
150225  /* Jump here if malloc fails */
150226whereBeginError:
150227  if( pWInfo ){
150228    pParse->nQueryLoop = pWInfo->savedNQueryLoop;
150229    whereInfoFree(db, pWInfo);
150230  }
150231  return 0;
150232}
150233
150234/*
150235** Part of sqlite3WhereEnd() will rewrite opcodes to reference the
150236** index rather than the main table.  In SQLITE_DEBUG mode, we want
150237** to trace those changes if PRAGMA vdbe_addoptrace=on.  This routine
150238** does that.
150239*/
150240#ifndef SQLITE_DEBUG
150241# define OpcodeRewriteTrace(D,K,P) /* no-op */
150242#else
150243# define OpcodeRewriteTrace(D,K,P) sqlite3WhereOpcodeRewriteTrace(D,K,P)
150244  static void sqlite3WhereOpcodeRewriteTrace(
150245    sqlite3 *db,
150246    int pc,
150247    VdbeOp *pOp
150248  ){
150249    if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
150250    sqlite3VdbePrintOp(0, pc, pOp);
150251  }
150252#endif
150253
150254/*
150255** Generate the end of the WHERE loop.  See comments on
150256** sqlite3WhereBegin() for additional information.
150257*/
150258SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
150259  Parse *pParse = pWInfo->pParse;
150260  Vdbe *v = pParse->pVdbe;
150261  int i;
150262  WhereLevel *pLevel;
150263  WhereLoop *pLoop;
150264  SrcList *pTabList = pWInfo->pTabList;
150265  sqlite3 *db = pParse->db;
150266
150267  /* Generate loop termination code.
150268  */
150269  VdbeModuleComment((v, "End WHERE-core"));
150270  for(i=pWInfo->nLevel-1; i>=0; i--){
150271    int addr;
150272    pLevel = &pWInfo->a[i];
150273    pLoop = pLevel->pWLoop;
150274    if( pLevel->op!=OP_Noop ){
150275#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
150276      int addrSeek = 0;
150277      Index *pIdx;
150278      int n;
150279      if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
150280       && i==pWInfo->nLevel-1  /* Ticket [ef9318757b152e3] 2017-10-21 */
150281       && (pLoop->wsFlags & WHERE_INDEXED)!=0
150282       && (pIdx = pLoop->u.btree.pIndex)->hasStat1
150283       && (n = pLoop->u.btree.nDistinctCol)>0
150284       && pIdx->aiRowLogEst[n]>=36
150285      ){
150286        int r1 = pParse->nMem+1;
150287        int j, op;
150288        for(j=0; j<n; j++){
150289          sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j);
150290        }
150291        pParse->nMem += n+1;
150292        op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
150293        addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
150294        VdbeCoverageIf(v, op==OP_SeekLT);
150295        VdbeCoverageIf(v, op==OP_SeekGT);
150296        sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
150297      }
150298#endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */
150299      /* The common case: Advance to the next row */
150300      sqlite3VdbeResolveLabel(v, pLevel->addrCont);
150301      sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
150302      sqlite3VdbeChangeP5(v, pLevel->p5);
150303      VdbeCoverage(v);
150304      VdbeCoverageIf(v, pLevel->op==OP_Next);
150305      VdbeCoverageIf(v, pLevel->op==OP_Prev);
150306      VdbeCoverageIf(v, pLevel->op==OP_VNext);
150307      if( pLevel->regBignull ){
150308        sqlite3VdbeResolveLabel(v, pLevel->addrBignull);
150309        sqlite3VdbeAddOp2(v, OP_DecrJumpZero, pLevel->regBignull, pLevel->p2-1);
150310        VdbeCoverage(v);
150311      }
150312#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
150313      if( addrSeek ) sqlite3VdbeJumpHere(v, addrSeek);
150314#endif
150315    }else{
150316      sqlite3VdbeResolveLabel(v, pLevel->addrCont);
150317    }
150318    if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
150319      struct InLoop *pIn;
150320      int j;
150321      sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
150322      for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
150323        sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
150324        if( pIn->eEndLoopOp!=OP_Noop ){
150325          if( pIn->nPrefix ){
150326            assert( pLoop->wsFlags & WHERE_IN_EARLYOUT );
150327            if( pLevel->iLeftJoin ){
150328              /* For LEFT JOIN queries, cursor pIn->iCur may not have been
150329              ** opened yet. This occurs for WHERE clauses such as
150330              ** "a = ? AND b IN (...)", where the index is on (a, b). If
150331              ** the RHS of the (a=?) is NULL, then the "b IN (...)" may
150332              ** never have been coded, but the body of the loop run to
150333              ** return the null-row. So, if the cursor is not open yet,
150334              ** jump over the OP_Next or OP_Prev instruction about to
150335              ** be coded.  */
150336              sqlite3VdbeAddOp2(v, OP_IfNotOpen, pIn->iCur,
150337                  sqlite3VdbeCurrentAddr(v) + 2 +
150338                     ((pLoop->wsFlags & WHERE_VIRTUALTABLE)==0)
150339              );
150340              VdbeCoverage(v);
150341            }
150342            if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ){
150343              sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
150344                  sqlite3VdbeCurrentAddr(v)+2,
150345                  pIn->iBase, pIn->nPrefix);
150346              VdbeCoverage(v);
150347            }
150348          }
150349          sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
150350          VdbeCoverage(v);
150351          VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
150352          VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
150353        }
150354        sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
150355      }
150356    }
150357    sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
150358    if( pLevel->addrSkip ){
150359      sqlite3VdbeGoto(v, pLevel->addrSkip);
150360      VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
150361      sqlite3VdbeJumpHere(v, pLevel->addrSkip);
150362      sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
150363    }
150364#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
150365    if( pLevel->addrLikeRep ){
150366      sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1),
150367                        pLevel->addrLikeRep);
150368      VdbeCoverage(v);
150369    }
150370#endif
150371    if( pLevel->iLeftJoin ){
150372      int ws = pLoop->wsFlags;
150373      addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
150374      assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 );
150375      if( (ws & WHERE_IDX_ONLY)==0 ){
150376        assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
150377        sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
150378      }
150379      if( (ws & WHERE_INDEXED)
150380       || ((ws & WHERE_MULTI_OR) && pLevel->u.pCovidx)
150381      ){
150382        sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
150383      }
150384      if( pLevel->op==OP_Return ){
150385        sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
150386      }else{
150387        sqlite3VdbeGoto(v, pLevel->addrFirst);
150388      }
150389      sqlite3VdbeJumpHere(v, addr);
150390    }
150391    VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
150392                     pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
150393  }
150394
150395  /* The "break" point is here, just past the end of the outer loop.
150396  ** Set it.
150397  */
150398  sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
150399
150400  assert( pWInfo->nLevel<=pTabList->nSrc );
150401  for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
150402    int k, last;
150403    VdbeOp *pOp;
150404    Index *pIdx = 0;
150405    struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
150406    Table *pTab = pTabItem->pTab;
150407    assert( pTab!=0 );
150408    pLoop = pLevel->pWLoop;
150409
150410    /* For a co-routine, change all OP_Column references to the table of
150411    ** the co-routine into OP_Copy of result contained in a register.
150412    ** OP_Rowid becomes OP_Null.
150413    */
150414    if( pTabItem->fg.viaCoroutine ){
150415      testcase( pParse->db->mallocFailed );
150416      translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
150417                            pTabItem->regResult, 0);
150418      continue;
150419    }
150420
150421#ifdef SQLITE_ENABLE_EARLY_CURSOR_CLOSE
150422    /* Close all of the cursors that were opened by sqlite3WhereBegin.
150423    ** Except, do not close cursors that will be reused by the OR optimization
150424    ** (WHERE_OR_SUBCLAUSE).  And do not close the OP_OpenWrite cursors
150425    ** created for the ONEPASS optimization.
150426    */
150427    if( (pTab->tabFlags & TF_Ephemeral)==0
150428     && pTab->pSelect==0
150429     && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0
150430    ){
150431      int ws = pLoop->wsFlags;
150432      if( pWInfo->eOnePass==ONEPASS_OFF && (ws & WHERE_IDX_ONLY)==0 ){
150433        sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
150434      }
150435      if( (ws & WHERE_INDEXED)!=0
150436       && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0
150437       && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1]
150438      ){
150439        sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
150440      }
150441    }
150442#endif
150443
150444    /* If this scan uses an index, make VDBE code substitutions to read data
150445    ** from the index instead of from the table where possible.  In some cases
150446    ** this optimization prevents the table from ever being read, which can
150447    ** yield a significant performance boost.
150448    **
150449    ** Calls to the code generator in between sqlite3WhereBegin and
150450    ** sqlite3WhereEnd will have created code that references the table
150451    ** directly.  This loop scans all that code looking for opcodes
150452    ** that reference the table and converts them into opcodes that
150453    ** reference the index.
150454    */
150455    if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
150456      pIdx = pLoop->u.btree.pIndex;
150457    }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
150458      pIdx = pLevel->u.pCovidx;
150459    }
150460    if( pIdx
150461     && (pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable))
150462     && !db->mallocFailed
150463    ){
150464      last = sqlite3VdbeCurrentAddr(v);
150465      k = pLevel->addrBody;
150466#ifdef SQLITE_DEBUG
150467      if( db->flags & SQLITE_VdbeAddopTrace ){
150468        printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
150469      }
150470#endif
150471      pOp = sqlite3VdbeGetOp(v, k);
150472      for(; k<last; k++, pOp++){
150473        if( pOp->p1!=pLevel->iTabCur ) continue;
150474        if( pOp->opcode==OP_Column
150475#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
150476         || pOp->opcode==OP_Offset
150477#endif
150478        ){
150479          int x = pOp->p2;
150480          assert( pIdx->pTable==pTab );
150481          if( !HasRowid(pTab) ){
150482            Index *pPk = sqlite3PrimaryKeyIndex(pTab);
150483            x = pPk->aiColumn[x];
150484            assert( x>=0 );
150485          }else{
150486            testcase( x!=sqlite3StorageColumnToTable(pTab,x) );
150487            x = sqlite3StorageColumnToTable(pTab,x);
150488          }
150489          x = sqlite3TableColumnToIndex(pIdx, x);
150490          if( x>=0 ){
150491            pOp->p2 = x;
150492            pOp->p1 = pLevel->iIdxCur;
150493            OpcodeRewriteTrace(db, k, pOp);
150494          }
150495          assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0
150496              || pWInfo->eOnePass );
150497        }else if( pOp->opcode==OP_Rowid ){
150498          pOp->p1 = pLevel->iIdxCur;
150499          pOp->opcode = OP_IdxRowid;
150500          OpcodeRewriteTrace(db, k, pOp);
150501        }else if( pOp->opcode==OP_IfNullRow ){
150502          pOp->p1 = pLevel->iIdxCur;
150503          OpcodeRewriteTrace(db, k, pOp);
150504        }
150505      }
150506#ifdef SQLITE_DEBUG
150507      if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
150508#endif
150509    }
150510  }
150511
150512  /* Undo all Expr node modifications */
150513  while( pWInfo->pExprMods ){
150514    WhereExprMod *p = pWInfo->pExprMods;
150515    pWInfo->pExprMods = p->pNext;
150516    memcpy(p->pExpr, &p->orig, sizeof(p->orig));
150517    sqlite3DbFree(db, p);
150518  }
150519
150520  /* Final cleanup
150521  */
150522  pParse->nQueryLoop = pWInfo->savedNQueryLoop;
150523  whereInfoFree(db, pWInfo);
150524  return;
150525}
150526
150527/************** End of where.c ***********************************************/
150528/************** Begin file window.c ******************************************/
150529/*
150530** 2018 May 08
150531**
150532** The author disclaims copyright to this source code.  In place of
150533** a legal notice, here is a blessing:
150534**
150535**    May you do good and not evil.
150536**    May you find forgiveness for yourself and forgive others.
150537**    May you share freely, never taking more than you give.
150538**
150539*************************************************************************
150540*/
150541/* #include "sqliteInt.h" */
150542
150543#ifndef SQLITE_OMIT_WINDOWFUNC
150544
150545/*
150546** SELECT REWRITING
150547**
150548**   Any SELECT statement that contains one or more window functions in
150549**   either the select list or ORDER BY clause (the only two places window
150550**   functions may be used) is transformed by function sqlite3WindowRewrite()
150551**   in order to support window function processing. For example, with the
150552**   schema:
150553**
150554**     CREATE TABLE t1(a, b, c, d, e, f, g);
150555**
150556**   the statement:
150557**
150558**     SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e;
150559**
150560**   is transformed to:
150561**
150562**     SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM (
150563**         SELECT a, e, c, d, b FROM t1 ORDER BY c, d
150564**     ) ORDER BY e;
150565**
150566**   The flattening optimization is disabled when processing this transformed
150567**   SELECT statement. This allows the implementation of the window function
150568**   (in this case max()) to process rows sorted in order of (c, d), which
150569**   makes things easier for obvious reasons. More generally:
150570**
150571**     * FROM, WHERE, GROUP BY and HAVING clauses are all moved to
150572**       the sub-query.
150573**
150574**     * ORDER BY, LIMIT and OFFSET remain part of the parent query.
150575**
150576**     * Terminals from each of the expression trees that make up the
150577**       select-list and ORDER BY expressions in the parent query are
150578**       selected by the sub-query. For the purposes of the transformation,
150579**       terminals are column references and aggregate functions.
150580**
150581**   If there is more than one window function in the SELECT that uses
150582**   the same window declaration (the OVER bit), then a single scan may
150583**   be used to process more than one window function. For example:
150584**
150585**     SELECT max(b) OVER (PARTITION BY c ORDER BY d),
150586**            min(e) OVER (PARTITION BY c ORDER BY d)
150587**     FROM t1;
150588**
150589**   is transformed in the same way as the example above. However:
150590**
150591**     SELECT max(b) OVER (PARTITION BY c ORDER BY d),
150592**            min(e) OVER (PARTITION BY a ORDER BY b)
150593**     FROM t1;
150594**
150595**   Must be transformed to:
150596**
150597**     SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM (
150598**         SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM
150599**           SELECT a, e, c, d, b FROM t1 ORDER BY a, b
150600**         ) ORDER BY c, d
150601**     ) ORDER BY e;
150602**
150603**   so that both min() and max() may process rows in the order defined by
150604**   their respective window declarations.
150605**
150606** INTERFACE WITH SELECT.C
150607**
150608**   When processing the rewritten SELECT statement, code in select.c calls
150609**   sqlite3WhereBegin() to begin iterating through the results of the
150610**   sub-query, which is always implemented as a co-routine. It then calls
150611**   sqlite3WindowCodeStep() to process rows and finish the scan by calling
150612**   sqlite3WhereEnd().
150613**
150614**   sqlite3WindowCodeStep() generates VM code so that, for each row returned
150615**   by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
150616**   When the sub-routine is invoked:
150617**
150618**     * The results of all window-functions for the row are stored
150619**       in the associated Window.regResult registers.
150620**
150621**     * The required terminal values are stored in the current row of
150622**       temp table Window.iEphCsr.
150623**
150624**   In some cases, depending on the window frame and the specific window
150625**   functions invoked, sqlite3WindowCodeStep() caches each entire partition
150626**   in a temp table before returning any rows. In other cases it does not.
150627**   This detail is encapsulated within this file, the code generated by
150628**   select.c is the same in either case.
150629**
150630** BUILT-IN WINDOW FUNCTIONS
150631**
150632**   This implementation features the following built-in window functions:
150633**
150634**     row_number()
150635**     rank()
150636**     dense_rank()
150637**     percent_rank()
150638**     cume_dist()
150639**     ntile(N)
150640**     lead(expr [, offset [, default]])
150641**     lag(expr [, offset [, default]])
150642**     first_value(expr)
150643**     last_value(expr)
150644**     nth_value(expr, N)
150645**
150646**   These are the same built-in window functions supported by Postgres.
150647**   Although the behaviour of aggregate window functions (functions that
150648**   can be used as either aggregates or window funtions) allows them to
150649**   be implemented using an API, built-in window functions are much more
150650**   esoteric. Additionally, some window functions (e.g. nth_value())
150651**   may only be implemented by caching the entire partition in memory.
150652**   As such, some built-in window functions use the same API as aggregate
150653**   window functions and some are implemented directly using VDBE
150654**   instructions. Additionally, for those functions that use the API, the
150655**   window frame is sometimes modified before the SELECT statement is
150656**   rewritten. For example, regardless of the specified window frame, the
150657**   row_number() function always uses:
150658**
150659**     ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
150660**
150661**   See sqlite3WindowUpdate() for details.
150662**
150663**   As well as some of the built-in window functions, aggregate window
150664**   functions min() and max() are implemented using VDBE instructions if
150665**   the start of the window frame is declared as anything other than
150666**   UNBOUNDED PRECEDING.
150667*/
150668
150669/*
150670** Implementation of built-in window function row_number(). Assumes that the
150671** window frame has been coerced to:
150672**
150673**   ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
150674*/
150675static void row_numberStepFunc(
150676  sqlite3_context *pCtx,
150677  int nArg,
150678  sqlite3_value **apArg
150679){
150680  i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150681  if( p ) (*p)++;
150682  UNUSED_PARAMETER(nArg);
150683  UNUSED_PARAMETER(apArg);
150684}
150685static void row_numberValueFunc(sqlite3_context *pCtx){
150686  i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150687  sqlite3_result_int64(pCtx, (p ? *p : 0));
150688}
150689
150690/*
150691** Context object type used by rank(), dense_rank(), percent_rank() and
150692** cume_dist().
150693*/
150694struct CallCount {
150695  i64 nValue;
150696  i64 nStep;
150697  i64 nTotal;
150698};
150699
150700/*
150701** Implementation of built-in window function dense_rank(). Assumes that
150702** the window frame has been set to:
150703**
150704**   RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
150705*/
150706static void dense_rankStepFunc(
150707  sqlite3_context *pCtx,
150708  int nArg,
150709  sqlite3_value **apArg
150710){
150711  struct CallCount *p;
150712  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150713  if( p ) p->nStep = 1;
150714  UNUSED_PARAMETER(nArg);
150715  UNUSED_PARAMETER(apArg);
150716}
150717static void dense_rankValueFunc(sqlite3_context *pCtx){
150718  struct CallCount *p;
150719  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150720  if( p ){
150721    if( p->nStep ){
150722      p->nValue++;
150723      p->nStep = 0;
150724    }
150725    sqlite3_result_int64(pCtx, p->nValue);
150726  }
150727}
150728
150729/*
150730** Implementation of built-in window function nth_value(). This
150731** implementation is used in "slow mode" only - when the EXCLUDE clause
150732** is not set to the default value "NO OTHERS".
150733*/
150734struct NthValueCtx {
150735  i64 nStep;
150736  sqlite3_value *pValue;
150737};
150738static void nth_valueStepFunc(
150739  sqlite3_context *pCtx,
150740  int nArg,
150741  sqlite3_value **apArg
150742){
150743  struct NthValueCtx *p;
150744  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150745  if( p ){
150746    i64 iVal;
150747    switch( sqlite3_value_numeric_type(apArg[1]) ){
150748      case SQLITE_INTEGER:
150749        iVal = sqlite3_value_int64(apArg[1]);
150750        break;
150751      case SQLITE_FLOAT: {
150752        double fVal = sqlite3_value_double(apArg[1]);
150753        if( ((i64)fVal)!=fVal ) goto error_out;
150754        iVal = (i64)fVal;
150755        break;
150756      }
150757      default:
150758        goto error_out;
150759    }
150760    if( iVal<=0 ) goto error_out;
150761
150762    p->nStep++;
150763    if( iVal==p->nStep ){
150764      p->pValue = sqlite3_value_dup(apArg[0]);
150765      if( !p->pValue ){
150766        sqlite3_result_error_nomem(pCtx);
150767      }
150768    }
150769  }
150770  UNUSED_PARAMETER(nArg);
150771  UNUSED_PARAMETER(apArg);
150772  return;
150773
150774 error_out:
150775  sqlite3_result_error(
150776      pCtx, "second argument to nth_value must be a positive integer", -1
150777  );
150778}
150779static void nth_valueFinalizeFunc(sqlite3_context *pCtx){
150780  struct NthValueCtx *p;
150781  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, 0);
150782  if( p && p->pValue ){
150783    sqlite3_result_value(pCtx, p->pValue);
150784    sqlite3_value_free(p->pValue);
150785    p->pValue = 0;
150786  }
150787}
150788#define nth_valueInvFunc noopStepFunc
150789#define nth_valueValueFunc noopValueFunc
150790
150791static void first_valueStepFunc(
150792  sqlite3_context *pCtx,
150793  int nArg,
150794  sqlite3_value **apArg
150795){
150796  struct NthValueCtx *p;
150797  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150798  if( p && p->pValue==0 ){
150799    p->pValue = sqlite3_value_dup(apArg[0]);
150800    if( !p->pValue ){
150801      sqlite3_result_error_nomem(pCtx);
150802    }
150803  }
150804  UNUSED_PARAMETER(nArg);
150805  UNUSED_PARAMETER(apArg);
150806}
150807static void first_valueFinalizeFunc(sqlite3_context *pCtx){
150808  struct NthValueCtx *p;
150809  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150810  if( p && p->pValue ){
150811    sqlite3_result_value(pCtx, p->pValue);
150812    sqlite3_value_free(p->pValue);
150813    p->pValue = 0;
150814  }
150815}
150816#define first_valueInvFunc noopStepFunc
150817#define first_valueValueFunc noopValueFunc
150818
150819/*
150820** Implementation of built-in window function rank(). Assumes that
150821** the window frame has been set to:
150822**
150823**   RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
150824*/
150825static void rankStepFunc(
150826  sqlite3_context *pCtx,
150827  int nArg,
150828  sqlite3_value **apArg
150829){
150830  struct CallCount *p;
150831  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150832  if( p ){
150833    p->nStep++;
150834    if( p->nValue==0 ){
150835      p->nValue = p->nStep;
150836    }
150837  }
150838  UNUSED_PARAMETER(nArg);
150839  UNUSED_PARAMETER(apArg);
150840}
150841static void rankValueFunc(sqlite3_context *pCtx){
150842  struct CallCount *p;
150843  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150844  if( p ){
150845    sqlite3_result_int64(pCtx, p->nValue);
150846    p->nValue = 0;
150847  }
150848}
150849
150850/*
150851** Implementation of built-in window function percent_rank(). Assumes that
150852** the window frame has been set to:
150853**
150854**   GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
150855*/
150856static void percent_rankStepFunc(
150857  sqlite3_context *pCtx,
150858  int nArg,
150859  sqlite3_value **apArg
150860){
150861  struct CallCount *p;
150862  UNUSED_PARAMETER(nArg); assert( nArg==0 );
150863  UNUSED_PARAMETER(apArg);
150864  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150865  if( p ){
150866    p->nTotal++;
150867  }
150868}
150869static void percent_rankInvFunc(
150870  sqlite3_context *pCtx,
150871  int nArg,
150872  sqlite3_value **apArg
150873){
150874  struct CallCount *p;
150875  UNUSED_PARAMETER(nArg); assert( nArg==0 );
150876  UNUSED_PARAMETER(apArg);
150877  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150878  p->nStep++;
150879}
150880static void percent_rankValueFunc(sqlite3_context *pCtx){
150881  struct CallCount *p;
150882  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150883  if( p ){
150884    p->nValue = p->nStep;
150885    if( p->nTotal>1 ){
150886      double r = (double)p->nValue / (double)(p->nTotal-1);
150887      sqlite3_result_double(pCtx, r);
150888    }else{
150889      sqlite3_result_double(pCtx, 0.0);
150890    }
150891  }
150892}
150893#define percent_rankFinalizeFunc percent_rankValueFunc
150894
150895/*
150896** Implementation of built-in window function cume_dist(). Assumes that
150897** the window frame has been set to:
150898**
150899**   GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING
150900*/
150901static void cume_distStepFunc(
150902  sqlite3_context *pCtx,
150903  int nArg,
150904  sqlite3_value **apArg
150905){
150906  struct CallCount *p;
150907  UNUSED_PARAMETER(nArg); assert( nArg==0 );
150908  UNUSED_PARAMETER(apArg);
150909  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150910  if( p ){
150911    p->nTotal++;
150912  }
150913}
150914static void cume_distInvFunc(
150915  sqlite3_context *pCtx,
150916  int nArg,
150917  sqlite3_value **apArg
150918){
150919  struct CallCount *p;
150920  UNUSED_PARAMETER(nArg); assert( nArg==0 );
150921  UNUSED_PARAMETER(apArg);
150922  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150923  p->nStep++;
150924}
150925static void cume_distValueFunc(sqlite3_context *pCtx){
150926  struct CallCount *p;
150927  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, 0);
150928  if( p ){
150929    double r = (double)(p->nStep) / (double)(p->nTotal);
150930    sqlite3_result_double(pCtx, r);
150931  }
150932}
150933#define cume_distFinalizeFunc cume_distValueFunc
150934
150935/*
150936** Context object for ntile() window function.
150937*/
150938struct NtileCtx {
150939  i64 nTotal;                     /* Total rows in partition */
150940  i64 nParam;                     /* Parameter passed to ntile(N) */
150941  i64 iRow;                       /* Current row */
150942};
150943
150944/*
150945** Implementation of ntile(). This assumes that the window frame has
150946** been coerced to:
150947**
150948**   ROWS CURRENT ROW AND UNBOUNDED FOLLOWING
150949*/
150950static void ntileStepFunc(
150951  sqlite3_context *pCtx,
150952  int nArg,
150953  sqlite3_value **apArg
150954){
150955  struct NtileCtx *p;
150956  assert( nArg==1 ); UNUSED_PARAMETER(nArg);
150957  p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150958  if( p ){
150959    if( p->nTotal==0 ){
150960      p->nParam = sqlite3_value_int64(apArg[0]);
150961      if( p->nParam<=0 ){
150962        sqlite3_result_error(
150963            pCtx, "argument of ntile must be a positive integer", -1
150964        );
150965      }
150966    }
150967    p->nTotal++;
150968  }
150969}
150970static void ntileInvFunc(
150971  sqlite3_context *pCtx,
150972  int nArg,
150973  sqlite3_value **apArg
150974){
150975  struct NtileCtx *p;
150976  assert( nArg==1 ); UNUSED_PARAMETER(nArg);
150977  UNUSED_PARAMETER(apArg);
150978  p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150979  p->iRow++;
150980}
150981static void ntileValueFunc(sqlite3_context *pCtx){
150982  struct NtileCtx *p;
150983  p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
150984  if( p && p->nParam>0 ){
150985    int nSize = (p->nTotal / p->nParam);
150986    if( nSize==0 ){
150987      sqlite3_result_int64(pCtx, p->iRow+1);
150988    }else{
150989      i64 nLarge = p->nTotal - p->nParam*nSize;
150990      i64 iSmall = nLarge*(nSize+1);
150991      i64 iRow = p->iRow;
150992
150993      assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
150994
150995      if( iRow<iSmall ){
150996        sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1));
150997      }else{
150998        sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
150999      }
151000    }
151001  }
151002}
151003#define ntileFinalizeFunc ntileValueFunc
151004
151005/*
151006** Context object for last_value() window function.
151007*/
151008struct LastValueCtx {
151009  sqlite3_value *pVal;
151010  int nVal;
151011};
151012
151013/*
151014** Implementation of last_value().
151015*/
151016static void last_valueStepFunc(
151017  sqlite3_context *pCtx,
151018  int nArg,
151019  sqlite3_value **apArg
151020){
151021  struct LastValueCtx *p;
151022  UNUSED_PARAMETER(nArg);
151023  p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151024  if( p ){
151025    sqlite3_value_free(p->pVal);
151026    p->pVal = sqlite3_value_dup(apArg[0]);
151027    if( p->pVal==0 ){
151028      sqlite3_result_error_nomem(pCtx);
151029    }else{
151030      p->nVal++;
151031    }
151032  }
151033}
151034static void last_valueInvFunc(
151035  sqlite3_context *pCtx,
151036  int nArg,
151037  sqlite3_value **apArg
151038){
151039  struct LastValueCtx *p;
151040  UNUSED_PARAMETER(nArg);
151041  UNUSED_PARAMETER(apArg);
151042  p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151043  if( ALWAYS(p) ){
151044    p->nVal--;
151045    if( p->nVal==0 ){
151046      sqlite3_value_free(p->pVal);
151047      p->pVal = 0;
151048    }
151049  }
151050}
151051static void last_valueValueFunc(sqlite3_context *pCtx){
151052  struct LastValueCtx *p;
151053  p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, 0);
151054  if( p && p->pVal ){
151055    sqlite3_result_value(pCtx, p->pVal);
151056  }
151057}
151058static void last_valueFinalizeFunc(sqlite3_context *pCtx){
151059  struct LastValueCtx *p;
151060  p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151061  if( p && p->pVal ){
151062    sqlite3_result_value(pCtx, p->pVal);
151063    sqlite3_value_free(p->pVal);
151064    p->pVal = 0;
151065  }
151066}
151067
151068/*
151069** Static names for the built-in window function names.  These static
151070** names are used, rather than string literals, so that FuncDef objects
151071** can be associated with a particular window function by direct
151072** comparison of the zName pointer.  Example:
151073**
151074**       if( pFuncDef->zName==row_valueName ){ ... }
151075*/
151076static const char row_numberName[] =   "row_number";
151077static const char dense_rankName[] =   "dense_rank";
151078static const char rankName[] =         "rank";
151079static const char percent_rankName[] = "percent_rank";
151080static const char cume_distName[] =    "cume_dist";
151081static const char ntileName[] =        "ntile";
151082static const char last_valueName[] =   "last_value";
151083static const char nth_valueName[] =    "nth_value";
151084static const char first_valueName[] =  "first_value";
151085static const char leadName[] =         "lead";
151086static const char lagName[] =          "lag";
151087
151088/*
151089** No-op implementations of xStep() and xFinalize().  Used as place-holders
151090** for built-in window functions that never call those interfaces.
151091**
151092** The noopValueFunc() is called but is expected to do nothing.  The
151093** noopStepFunc() is never called, and so it is marked with NO_TEST to
151094** let the test coverage routine know not to expect this function to be
151095** invoked.
151096*/
151097static void noopStepFunc(    /*NO_TEST*/
151098  sqlite3_context *p,        /*NO_TEST*/
151099  int n,                     /*NO_TEST*/
151100  sqlite3_value **a          /*NO_TEST*/
151101){                           /*NO_TEST*/
151102  UNUSED_PARAMETER(p);       /*NO_TEST*/
151103  UNUSED_PARAMETER(n);       /*NO_TEST*/
151104  UNUSED_PARAMETER(a);       /*NO_TEST*/
151105  assert(0);                 /*NO_TEST*/
151106}                            /*NO_TEST*/
151107static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p); /*no-op*/ }
151108
151109/* Window functions that use all window interfaces: xStep, xFinal,
151110** xValue, and xInverse */
151111#define WINDOWFUNCALL(name,nArg,extra) {                                   \
151112  nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0,                      \
151113  name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc,               \
151114  name ## InvFunc, name ## Name, {0}                                       \
151115}
151116
151117/* Window functions that are implemented using bytecode and thus have
151118** no-op routines for their methods */
151119#define WINDOWFUNCNOOP(name,nArg,extra) {                                  \
151120  nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0,                      \
151121  noopStepFunc, noopValueFunc, noopValueFunc,                              \
151122  noopStepFunc, name ## Name, {0}                                          \
151123}
151124
151125/* Window functions that use all window interfaces: xStep, the
151126** same routine for xFinalize and xValue and which never call
151127** xInverse. */
151128#define WINDOWFUNCX(name,nArg,extra) {                                     \
151129  nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0,                      \
151130  name ## StepFunc, name ## ValueFunc, name ## ValueFunc,                  \
151131  noopStepFunc, name ## Name, {0}                                          \
151132}
151133
151134
151135/*
151136** Register those built-in window functions that are not also aggregates.
151137*/
151138SQLITE_PRIVATE void sqlite3WindowFunctions(void){
151139  static FuncDef aWindowFuncs[] = {
151140    WINDOWFUNCX(row_number, 0, 0),
151141    WINDOWFUNCX(dense_rank, 0, 0),
151142    WINDOWFUNCX(rank, 0, 0),
151143    WINDOWFUNCALL(percent_rank, 0, 0),
151144    WINDOWFUNCALL(cume_dist, 0, 0),
151145    WINDOWFUNCALL(ntile, 1, 0),
151146    WINDOWFUNCALL(last_value, 1, 0),
151147    WINDOWFUNCALL(nth_value, 2, 0),
151148    WINDOWFUNCALL(first_value, 1, 0),
151149    WINDOWFUNCNOOP(lead, 1, 0),
151150    WINDOWFUNCNOOP(lead, 2, 0),
151151    WINDOWFUNCNOOP(lead, 3, 0),
151152    WINDOWFUNCNOOP(lag, 1, 0),
151153    WINDOWFUNCNOOP(lag, 2, 0),
151154    WINDOWFUNCNOOP(lag, 3, 0),
151155  };
151156  sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs));
151157}
151158
151159static Window *windowFind(Parse *pParse, Window *pList, const char *zName){
151160  Window *p;
151161  for(p=pList; p; p=p->pNextWin){
151162    if( sqlite3StrICmp(p->zName, zName)==0 ) break;
151163  }
151164  if( p==0 ){
151165    sqlite3ErrorMsg(pParse, "no such window: %s", zName);
151166  }
151167  return p;
151168}
151169
151170/*
151171** This function is called immediately after resolving the function name
151172** for a window function within a SELECT statement. Argument pList is a
151173** linked list of WINDOW definitions for the current SELECT statement.
151174** Argument pFunc is the function definition just resolved and pWin
151175** is the Window object representing the associated OVER clause. This
151176** function updates the contents of pWin as follows:
151177**
151178**   * If the OVER clause refered to a named window (as in "max(x) OVER win"),
151179**     search list pList for a matching WINDOW definition, and update pWin
151180**     accordingly. If no such WINDOW clause can be found, leave an error
151181**     in pParse.
151182**
151183**   * If the function is a built-in window function that requires the
151184**     window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
151185**     of this file), pWin is updated here.
151186*/
151187SQLITE_PRIVATE void sqlite3WindowUpdate(
151188  Parse *pParse,
151189  Window *pList,                  /* List of named windows for this SELECT */
151190  Window *pWin,                   /* Window frame to update */
151191  FuncDef *pFunc                  /* Window function definition */
151192){
151193  if( pWin->zName && pWin->eFrmType==0 ){
151194    Window *p = windowFind(pParse, pList, pWin->zName);
151195    if( p==0 ) return;
151196    pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
151197    pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
151198    pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
151199    pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
151200    pWin->eStart = p->eStart;
151201    pWin->eEnd = p->eEnd;
151202    pWin->eFrmType = p->eFrmType;
151203    pWin->eExclude = p->eExclude;
151204  }else{
151205    sqlite3WindowChain(pParse, pWin, pList);
151206  }
151207  if( (pWin->eFrmType==TK_RANGE)
151208   && (pWin->pStart || pWin->pEnd)
151209   && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
151210  ){
151211    sqlite3ErrorMsg(pParse,
151212      "RANGE with offset PRECEDING/FOLLOWING requires one ORDER BY expression"
151213    );
151214  }else
151215  if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
151216    sqlite3 *db = pParse->db;
151217    if( pWin->pFilter ){
151218      sqlite3ErrorMsg(pParse,
151219          "FILTER clause may only be used with aggregate window functions"
151220      );
151221    }else{
151222      struct WindowUpdate {
151223        const char *zFunc;
151224        int eFrmType;
151225        int eStart;
151226        int eEnd;
151227      } aUp[] = {
151228        { row_numberName,   TK_ROWS,   TK_UNBOUNDED, TK_CURRENT },
151229        { dense_rankName,   TK_RANGE,  TK_UNBOUNDED, TK_CURRENT },
151230        { rankName,         TK_RANGE,  TK_UNBOUNDED, TK_CURRENT },
151231        { percent_rankName, TK_GROUPS, TK_CURRENT,   TK_UNBOUNDED },
151232        { cume_distName,    TK_GROUPS, TK_FOLLOWING, TK_UNBOUNDED },
151233        { ntileName,        TK_ROWS,   TK_CURRENT,   TK_UNBOUNDED },
151234        { leadName,         TK_ROWS,   TK_UNBOUNDED, TK_UNBOUNDED },
151235        { lagName,          TK_ROWS,   TK_UNBOUNDED, TK_CURRENT },
151236      };
151237      int i;
151238      for(i=0; i<ArraySize(aUp); i++){
151239        if( pFunc->zName==aUp[i].zFunc ){
151240          sqlite3ExprDelete(db, pWin->pStart);
151241          sqlite3ExprDelete(db, pWin->pEnd);
151242          pWin->pEnd = pWin->pStart = 0;
151243          pWin->eFrmType = aUp[i].eFrmType;
151244          pWin->eStart = aUp[i].eStart;
151245          pWin->eEnd = aUp[i].eEnd;
151246          pWin->eExclude = 0;
151247          if( pWin->eStart==TK_FOLLOWING ){
151248            pWin->pStart = sqlite3Expr(db, TK_INTEGER, "1");
151249          }
151250          break;
151251        }
151252      }
151253    }
151254  }
151255  pWin->pFunc = pFunc;
151256}
151257
151258/*
151259** Context object passed through sqlite3WalkExprList() to
151260** selectWindowRewriteExprCb() by selectWindowRewriteEList().
151261*/
151262typedef struct WindowRewrite WindowRewrite;
151263struct WindowRewrite {
151264  Window *pWin;
151265  SrcList *pSrc;
151266  ExprList *pSub;
151267  Table *pTab;
151268  Select *pSubSelect;             /* Current sub-select, if any */
151269};
151270
151271/*
151272** Callback function used by selectWindowRewriteEList(). If necessary,
151273** this function appends to the output expression-list and updates
151274** expression (*ppExpr) in place.
151275*/
151276static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
151277  struct WindowRewrite *p = pWalker->u.pRewrite;
151278  Parse *pParse = pWalker->pParse;
151279  assert( p!=0 );
151280  assert( p->pWin!=0 );
151281
151282  /* If this function is being called from within a scalar sub-select
151283  ** that used by the SELECT statement being processed, only process
151284  ** TK_COLUMN expressions that refer to it (the outer SELECT). Do
151285  ** not process aggregates or window functions at all, as they belong
151286  ** to the scalar sub-select.  */
151287  if( p->pSubSelect ){
151288    if( pExpr->op!=TK_COLUMN ){
151289      return WRC_Continue;
151290    }else{
151291      int nSrc = p->pSrc->nSrc;
151292      int i;
151293      for(i=0; i<nSrc; i++){
151294        if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
151295      }
151296      if( i==nSrc ) return WRC_Continue;
151297    }
151298  }
151299
151300  switch( pExpr->op ){
151301
151302    case TK_FUNCTION:
151303      if( !ExprHasProperty(pExpr, EP_WinFunc) ){
151304        break;
151305      }else{
151306        Window *pWin;
151307        for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
151308          if( pExpr->y.pWin==pWin ){
151309            assert( pWin->pOwner==pExpr );
151310            return WRC_Prune;
151311          }
151312        }
151313      }
151314      /* Fall through.  */
151315
151316    case TK_AGG_FUNCTION:
151317    case TK_COLUMN: {
151318      int iCol = -1;
151319      if( p->pSub ){
151320        int i;
151321        for(i=0; i<p->pSub->nExpr; i++){
151322          if( 0==sqlite3ExprCompare(0, p->pSub->a[i].pExpr, pExpr, -1) ){
151323            iCol = i;
151324            break;
151325          }
151326        }
151327      }
151328      if( iCol<0 ){
151329        Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
151330        if( pDup && pDup->op==TK_AGG_FUNCTION ) pDup->op = TK_FUNCTION;
151331        p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
151332      }
151333      if( p->pSub ){
151334        assert( ExprHasProperty(pExpr, EP_Static)==0 );
151335        ExprSetProperty(pExpr, EP_Static);
151336        sqlite3ExprDelete(pParse->db, pExpr);
151337        ExprClearProperty(pExpr, EP_Static);
151338        memset(pExpr, 0, sizeof(Expr));
151339
151340        pExpr->op = TK_COLUMN;
151341        pExpr->iColumn = (iCol<0 ? p->pSub->nExpr-1: iCol);
151342        pExpr->iTable = p->pWin->iEphCsr;
151343        pExpr->y.pTab = p->pTab;
151344      }
151345      if( pParse->db->mallocFailed ) return WRC_Abort;
151346      break;
151347    }
151348
151349    default: /* no-op */
151350      break;
151351  }
151352
151353  return WRC_Continue;
151354}
151355static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
151356  struct WindowRewrite *p = pWalker->u.pRewrite;
151357  Select *pSave = p->pSubSelect;
151358  if( pSave==pSelect ){
151359    return WRC_Continue;
151360  }else{
151361    p->pSubSelect = pSelect;
151362    sqlite3WalkSelect(pWalker, pSelect);
151363    p->pSubSelect = pSave;
151364  }
151365  return WRC_Prune;
151366}
151367
151368
151369/*
151370** Iterate through each expression in expression-list pEList. For each:
151371**
151372**   * TK_COLUMN,
151373**   * aggregate function, or
151374**   * window function with a Window object that is not a member of the
151375**     Window list passed as the second argument (pWin).
151376**
151377** Append the node to output expression-list (*ppSub). And replace it
151378** with a TK_COLUMN that reads the (N-1)th element of table
151379** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
151380** appending the new one.
151381*/
151382static void selectWindowRewriteEList(
151383  Parse *pParse,
151384  Window *pWin,
151385  SrcList *pSrc,
151386  ExprList *pEList,               /* Rewrite expressions in this list */
151387  Table *pTab,
151388  ExprList **ppSub                /* IN/OUT: Sub-select expression-list */
151389){
151390  Walker sWalker;
151391  WindowRewrite sRewrite;
151392
151393  assert( pWin!=0 );
151394  memset(&sWalker, 0, sizeof(Walker));
151395  memset(&sRewrite, 0, sizeof(WindowRewrite));
151396
151397  sRewrite.pSub = *ppSub;
151398  sRewrite.pWin = pWin;
151399  sRewrite.pSrc = pSrc;
151400  sRewrite.pTab = pTab;
151401
151402  sWalker.pParse = pParse;
151403  sWalker.xExprCallback = selectWindowRewriteExprCb;
151404  sWalker.xSelectCallback = selectWindowRewriteSelectCb;
151405  sWalker.u.pRewrite = &sRewrite;
151406
151407  (void)sqlite3WalkExprList(&sWalker, pEList);
151408
151409  *ppSub = sRewrite.pSub;
151410}
151411
151412/*
151413** Append a copy of each expression in expression-list pAppend to
151414** expression list pList. Return a pointer to the result list.
151415*/
151416static ExprList *exprListAppendList(
151417  Parse *pParse,          /* Parsing context */
151418  ExprList *pList,        /* List to which to append. Might be NULL */
151419  ExprList *pAppend,      /* List of values to append. Might be NULL */
151420  int bIntToNull
151421){
151422  if( pAppend ){
151423    int i;
151424    int nInit = pList ? pList->nExpr : 0;
151425    for(i=0; i<pAppend->nExpr; i++){
151426      Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0);
151427      assert( pDup==0 || !ExprHasProperty(pDup, EP_MemToken) );
151428      if( bIntToNull && pDup ){
151429        int iDummy;
151430        Expr *pSub;
151431        for(pSub=pDup; ExprHasProperty(pSub, EP_Skip); pSub=pSub->pLeft){
151432          assert( pSub );
151433        }
151434        if( sqlite3ExprIsInteger(pSub, &iDummy) ){
151435          pSub->op = TK_NULL;
151436          pSub->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse);
151437          pSub->u.zToken = 0;
151438        }
151439      }
151440      pList = sqlite3ExprListAppend(pParse, pList, pDup);
151441      if( pList ) pList->a[nInit+i].sortFlags = pAppend->a[i].sortFlags;
151442    }
151443  }
151444  return pList;
151445}
151446
151447/*
151448** When rewriting a query, if the new subquery in the FROM clause
151449** contains TK_AGG_FUNCTION nodes that refer to an outer query,
151450** then we have to increase the Expr->op2 values of those nodes
151451** due to the extra subquery layer that was added.
151452**
151453** See also the incrAggDepth() routine in resolve.c
151454*/
151455static int sqlite3WindowExtraAggFuncDepth(Walker *pWalker, Expr *pExpr){
151456  if( pExpr->op==TK_AGG_FUNCTION
151457   && pExpr->op2>=pWalker->walkerDepth
151458  ){
151459    pExpr->op2++;
151460  }
151461  return WRC_Continue;
151462}
151463
151464/*
151465** If the SELECT statement passed as the second argument does not invoke
151466** any SQL window functions, this function is a no-op. Otherwise, it
151467** rewrites the SELECT statement so that window function xStep functions
151468** are invoked in the correct order as described under "SELECT REWRITING"
151469** at the top of this file.
151470*/
151471SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
151472  int rc = SQLITE_OK;
151473  if( p->pWin && p->pPrior==0 && (p->selFlags & SF_WinRewrite)==0 ){
151474    Vdbe *v = sqlite3GetVdbe(pParse);
151475    sqlite3 *db = pParse->db;
151476    Select *pSub = 0;             /* The subquery */
151477    SrcList *pSrc = p->pSrc;
151478    Expr *pWhere = p->pWhere;
151479    ExprList *pGroupBy = p->pGroupBy;
151480    Expr *pHaving = p->pHaving;
151481    ExprList *pSort = 0;
151482
151483    ExprList *pSublist = 0;       /* Expression list for sub-query */
151484    Window *pMWin = p->pWin;      /* Master window object */
151485    Window *pWin;                 /* Window object iterator */
151486    Table *pTab;
151487    Walker w;
151488
151489    u32 selFlags = p->selFlags;
151490
151491    pTab = sqlite3DbMallocZero(db, sizeof(Table));
151492    if( pTab==0 ){
151493      return sqlite3ErrorToParser(db, SQLITE_NOMEM);
151494    }
151495    sqlite3AggInfoPersistWalkerInit(&w, pParse);
151496    sqlite3WalkSelect(&w, p);
151497
151498    p->pSrc = 0;
151499    p->pWhere = 0;
151500    p->pGroupBy = 0;
151501    p->pHaving = 0;
151502    p->selFlags &= ~SF_Aggregate;
151503    p->selFlags |= SF_WinRewrite;
151504
151505    /* Create the ORDER BY clause for the sub-select. This is the concatenation
151506    ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
151507    ** redundant, remove the ORDER BY from the parent SELECT.  */
151508    pSort = exprListAppendList(pParse, 0, pMWin->pPartition, 1);
151509    pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
151510    if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
151511      int nSave = pSort->nExpr;
151512      pSort->nExpr = p->pOrderBy->nExpr;
151513      if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
151514        sqlite3ExprListDelete(db, p->pOrderBy);
151515        p->pOrderBy = 0;
151516      }
151517      pSort->nExpr = nSave;
151518    }
151519
151520    /* Assign a cursor number for the ephemeral table used to buffer rows.
151521    ** The OpenEphemeral instruction is coded later, after it is known how
151522    ** many columns the table will have.  */
151523    pMWin->iEphCsr = pParse->nTab++;
151524    pParse->nTab += 3;
151525
151526    selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, pTab, &pSublist);
151527    selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, pTab, &pSublist);
151528    pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
151529
151530    /* Append the PARTITION BY and ORDER BY expressions to the to the
151531    ** sub-select expression list. They are required to figure out where
151532    ** boundaries for partitions and sets of peer rows lie.  */
151533    pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0);
151534    pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0);
151535
151536    /* Append the arguments passed to each window function to the
151537    ** sub-select expression list. Also allocate two registers for each
151538    ** window function - one for the accumulator, another for interim
151539    ** results.  */
151540    for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
151541      ExprList *pArgs = pWin->pOwner->x.pList;
151542      if( pWin->pFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
151543        selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist);
151544        pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
151545        pWin->bExprArgs = 1;
151546      }else{
151547        pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
151548        pSublist = exprListAppendList(pParse, pSublist, pArgs, 0);
151549      }
151550      if( pWin->pFilter ){
151551        Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
151552        pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter);
151553      }
151554      pWin->regAccum = ++pParse->nMem;
151555      pWin->regResult = ++pParse->nMem;
151556      sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
151557    }
151558
151559    /* If there is no ORDER BY or PARTITION BY clause, and the window
151560    ** function accepts zero arguments, and there are no other columns
151561    ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible
151562    ** that pSublist is still NULL here. Add a constant expression here to
151563    ** keep everything legal in this case.
151564    */
151565    if( pSublist==0 ){
151566      pSublist = sqlite3ExprListAppend(pParse, 0,
151567        sqlite3Expr(db, TK_INTEGER, "0")
151568      );
151569    }
151570
151571    pSub = sqlite3SelectNew(
151572        pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
151573    );
151574    p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
151575    if( p->pSrc ){
151576      Table *pTab2;
151577      p->pSrc->a[0].pSelect = pSub;
151578      sqlite3SrcListAssignCursors(pParse, p->pSrc);
151579      pSub->selFlags |= SF_Expanded;
151580      pTab2 = sqlite3ResultSetOfSelect(pParse, pSub, SQLITE_AFF_NONE);
151581      pSub->selFlags |= (selFlags & SF_Aggregate);
151582      if( pTab2==0 ){
151583        /* Might actually be some other kind of error, but in that case
151584        ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
151585        ** the correct error message regardless. */
151586        rc = SQLITE_NOMEM;
151587      }else{
151588        memcpy(pTab, pTab2, sizeof(Table));
151589        pTab->tabFlags |= TF_Ephemeral;
151590        p->pSrc->a[0].pTab = pTab;
151591        pTab = pTab2;
151592        memset(&w, 0, sizeof(w));
151593        w.xExprCallback = sqlite3WindowExtraAggFuncDepth;
151594        w.xSelectCallback = sqlite3WalkerDepthIncrease;
151595        w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
151596        sqlite3WalkSelect(&w, pSub);
151597      }
151598    }else{
151599      sqlite3SelectDelete(db, pSub);
151600    }
151601    if( db->mallocFailed ) rc = SQLITE_NOMEM;
151602    sqlite3DbFree(db, pTab);
151603  }
151604
151605  if( rc ){
151606    if( pParse->nErr==0 ){
151607      assert( pParse->db->mallocFailed );
151608      sqlite3ErrorToParser(pParse->db, SQLITE_NOMEM);
151609    }
151610    sqlite3SelectReset(pParse, p);
151611  }
151612  return rc;
151613}
151614
151615/*
151616** Unlink the Window object from the Select to which it is attached,
151617** if it is attached.
151618*/
151619SQLITE_PRIVATE void sqlite3WindowUnlinkFromSelect(Window *p){
151620  if( p->ppThis ){
151621    *p->ppThis = p->pNextWin;
151622    if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
151623    p->ppThis = 0;
151624  }
151625}
151626
151627/*
151628** Free the Window object passed as the second argument.
151629*/
151630SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3 *db, Window *p){
151631  if( p ){
151632    sqlite3WindowUnlinkFromSelect(p);
151633    sqlite3ExprDelete(db, p->pFilter);
151634    sqlite3ExprListDelete(db, p->pPartition);
151635    sqlite3ExprListDelete(db, p->pOrderBy);
151636    sqlite3ExprDelete(db, p->pEnd);
151637    sqlite3ExprDelete(db, p->pStart);
151638    sqlite3DbFree(db, p->zName);
151639    sqlite3DbFree(db, p->zBase);
151640    sqlite3DbFree(db, p);
151641  }
151642}
151643
151644/*
151645** Free the linked list of Window objects starting at the second argument.
151646*/
151647SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p){
151648  while( p ){
151649    Window *pNext = p->pNextWin;
151650    sqlite3WindowDelete(db, p);
151651    p = pNext;
151652  }
151653}
151654
151655/*
151656** The argument expression is an PRECEDING or FOLLOWING offset.  The
151657** value should be a non-negative integer.  If the value is not a
151658** constant, change it to NULL.  The fact that it is then a non-negative
151659** integer will be caught later.  But it is important not to leave
151660** variable values in the expression tree.
151661*/
151662static Expr *sqlite3WindowOffsetExpr(Parse *pParse, Expr *pExpr){
151663  if( 0==sqlite3ExprIsConstant(pExpr) ){
151664    if( IN_RENAME_OBJECT ) sqlite3RenameExprUnmap(pParse, pExpr);
151665    sqlite3ExprDelete(pParse->db, pExpr);
151666    pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0);
151667  }
151668  return pExpr;
151669}
151670
151671/*
151672** Allocate and return a new Window object describing a Window Definition.
151673*/
151674SQLITE_PRIVATE Window *sqlite3WindowAlloc(
151675  Parse *pParse,    /* Parsing context */
151676  int eType,        /* Frame type. TK_RANGE, TK_ROWS, TK_GROUPS, or 0 */
151677  int eStart,       /* Start type: CURRENT, PRECEDING, FOLLOWING, UNBOUNDED */
151678  Expr *pStart,     /* Start window size if TK_PRECEDING or FOLLOWING */
151679  int eEnd,         /* End type: CURRENT, FOLLOWING, TK_UNBOUNDED, PRECEDING */
151680  Expr *pEnd,       /* End window size if TK_FOLLOWING or PRECEDING */
151681  u8 eExclude       /* EXCLUDE clause */
151682){
151683  Window *pWin = 0;
151684  int bImplicitFrame = 0;
151685
151686  /* Parser assures the following: */
151687  assert( eType==0 || eType==TK_RANGE || eType==TK_ROWS || eType==TK_GROUPS );
151688  assert( eStart==TK_CURRENT || eStart==TK_PRECEDING
151689           || eStart==TK_UNBOUNDED || eStart==TK_FOLLOWING );
151690  assert( eEnd==TK_CURRENT || eEnd==TK_FOLLOWING
151691           || eEnd==TK_UNBOUNDED || eEnd==TK_PRECEDING );
151692  assert( (eStart==TK_PRECEDING || eStart==TK_FOLLOWING)==(pStart!=0) );
151693  assert( (eEnd==TK_FOLLOWING || eEnd==TK_PRECEDING)==(pEnd!=0) );
151694
151695  if( eType==0 ){
151696    bImplicitFrame = 1;
151697    eType = TK_RANGE;
151698  }
151699
151700  /* Additionally, the
151701  ** starting boundary type may not occur earlier in the following list than
151702  ** the ending boundary type:
151703  **
151704  **   UNBOUNDED PRECEDING
151705  **   <expr> PRECEDING
151706  **   CURRENT ROW
151707  **   <expr> FOLLOWING
151708  **   UNBOUNDED FOLLOWING
151709  **
151710  ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending
151711  ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting
151712  ** frame boundary.
151713  */
151714  if( (eStart==TK_CURRENT && eEnd==TK_PRECEDING)
151715   || (eStart==TK_FOLLOWING && (eEnd==TK_PRECEDING || eEnd==TK_CURRENT))
151716  ){
151717    sqlite3ErrorMsg(pParse, "unsupported frame specification");
151718    goto windowAllocErr;
151719  }
151720
151721  pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
151722  if( pWin==0 ) goto windowAllocErr;
151723  pWin->eFrmType = eType;
151724  pWin->eStart = eStart;
151725  pWin->eEnd = eEnd;
151726  if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
151727    eExclude = TK_NO;
151728  }
151729  pWin->eExclude = eExclude;
151730  pWin->bImplicitFrame = bImplicitFrame;
151731  pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
151732  pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
151733  return pWin;
151734
151735windowAllocErr:
151736  sqlite3ExprDelete(pParse->db, pEnd);
151737  sqlite3ExprDelete(pParse->db, pStart);
151738  return 0;
151739}
151740
151741/*
151742** Attach PARTITION and ORDER BY clauses pPartition and pOrderBy to window
151743** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
151744** equivalent nul-terminated string.
151745*/
151746SQLITE_PRIVATE Window *sqlite3WindowAssemble(
151747  Parse *pParse,
151748  Window *pWin,
151749  ExprList *pPartition,
151750  ExprList *pOrderBy,
151751  Token *pBase
151752){
151753  if( pWin ){
151754    pWin->pPartition = pPartition;
151755    pWin->pOrderBy = pOrderBy;
151756    if( pBase ){
151757      pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
151758    }
151759  }else{
151760    sqlite3ExprListDelete(pParse->db, pPartition);
151761    sqlite3ExprListDelete(pParse->db, pOrderBy);
151762  }
151763  return pWin;
151764}
151765
151766/*
151767** Window *pWin has just been created from a WINDOW clause. Tokne pBase
151768** is the base window. Earlier windows from the same WINDOW clause are
151769** stored in the linked list starting at pWin->pNextWin. This function
151770** either updates *pWin according to the base specification, or else
151771** leaves an error in pParse.
151772*/
151773SQLITE_PRIVATE void sqlite3WindowChain(Parse *pParse, Window *pWin, Window *pList){
151774  if( pWin->zBase ){
151775    sqlite3 *db = pParse->db;
151776    Window *pExist = windowFind(pParse, pList, pWin->zBase);
151777    if( pExist ){
151778      const char *zErr = 0;
151779      /* Check for errors */
151780      if( pWin->pPartition ){
151781        zErr = "PARTITION clause";
151782      }else if( pExist->pOrderBy && pWin->pOrderBy ){
151783        zErr = "ORDER BY clause";
151784      }else if( pExist->bImplicitFrame==0 ){
151785        zErr = "frame specification";
151786      }
151787      if( zErr ){
151788        sqlite3ErrorMsg(pParse,
151789            "cannot override %s of window: %s", zErr, pWin->zBase
151790        );
151791      }else{
151792        pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
151793        if( pExist->pOrderBy ){
151794          assert( pWin->pOrderBy==0 );
151795          pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
151796        }
151797        sqlite3DbFree(db, pWin->zBase);
151798        pWin->zBase = 0;
151799      }
151800    }
151801  }
151802}
151803
151804/*
151805** Attach window object pWin to expression p.
151806*/
151807SQLITE_PRIVATE void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
151808  if( p ){
151809    assert( p->op==TK_FUNCTION );
151810    assert( pWin );
151811    p->y.pWin = pWin;
151812    ExprSetProperty(p, EP_WinFunc);
151813    pWin->pOwner = p;
151814    if( (p->flags & EP_Distinct) && pWin->eFrmType!=TK_FILTER ){
151815      sqlite3ErrorMsg(pParse,
151816          "DISTINCT is not supported for window functions"
151817      );
151818    }
151819  }else{
151820    sqlite3WindowDelete(pParse->db, pWin);
151821  }
151822}
151823
151824/*
151825** Possibly link window pWin into the list at pSel->pWin (window functions
151826** to be processed as part of SELECT statement pSel). The window is linked
151827** in if either (a) there are no other windows already linked to this
151828** SELECT, or (b) the windows already linked use a compatible window frame.
151829*/
151830SQLITE_PRIVATE void sqlite3WindowLink(Select *pSel, Window *pWin){
151831  if( pSel!=0
151832   && (0==pSel->pWin || 0==sqlite3WindowCompare(0, pSel->pWin, pWin, 0))
151833  ){
151834    pWin->pNextWin = pSel->pWin;
151835    if( pSel->pWin ){
151836      pSel->pWin->ppThis = &pWin->pNextWin;
151837    }
151838    pSel->pWin = pWin;
151839    pWin->ppThis = &pSel->pWin;
151840  }
151841}
151842
151843/*
151844** Return 0 if the two window objects are identical, 1 if they are
151845** different, or 2 if it cannot be determined if the objects are identical
151846** or not. Identical window objects can be processed in a single scan.
151847*/
151848SQLITE_PRIVATE int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2, int bFilter){
151849  int res;
151850  if( NEVER(p1==0) || NEVER(p2==0) ) return 1;
151851  if( p1->eFrmType!=p2->eFrmType ) return 1;
151852  if( p1->eStart!=p2->eStart ) return 1;
151853  if( p1->eEnd!=p2->eEnd ) return 1;
151854  if( p1->eExclude!=p2->eExclude ) return 1;
151855  if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
151856  if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
151857  if( (res = sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1)) ){
151858    return res;
151859  }
151860  if( (res = sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1)) ){
151861    return res;
151862  }
151863  if( bFilter ){
151864    if( (res = sqlite3ExprCompare(pParse, p1->pFilter, p2->pFilter, -1)) ){
151865      return res;
151866    }
151867  }
151868  return 0;
151869}
151870
151871
151872/*
151873** This is called by code in select.c before it calls sqlite3WhereBegin()
151874** to begin iterating through the sub-query results. It is used to allocate
151875** and initialize registers and cursors used by sqlite3WindowCodeStep().
151876*/
151877SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse *pParse, Select *pSelect){
151878  int nEphExpr = pSelect->pSrc->a[0].pSelect->pEList->nExpr;
151879  Window *pMWin = pSelect->pWin;
151880  Window *pWin;
151881  Vdbe *v = sqlite3GetVdbe(pParse);
151882
151883  sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, nEphExpr);
151884  sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr);
151885  sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr);
151886  sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr);
151887
151888  /* Allocate registers to use for PARTITION BY values, if any. Initialize
151889  ** said registers to NULL.  */
151890  if( pMWin->pPartition ){
151891    int nExpr = pMWin->pPartition->nExpr;
151892    pMWin->regPart = pParse->nMem+1;
151893    pParse->nMem += nExpr;
151894    sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
151895  }
151896
151897  pMWin->regOne = ++pParse->nMem;
151898  sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regOne);
151899
151900  if( pMWin->eExclude ){
151901    pMWin->regStartRowid = ++pParse->nMem;
151902    pMWin->regEndRowid = ++pParse->nMem;
151903    pMWin->csrApp = pParse->nTab++;
151904    sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
151905    sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
151906    sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr);
151907    return;
151908  }
151909
151910  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
151911    FuncDef *p = pWin->pFunc;
151912    if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
151913      /* The inline versions of min() and max() require a single ephemeral
151914      ** table and 3 registers. The registers are used as follows:
151915      **
151916      **   regApp+0: slot to copy min()/max() argument to for MakeRecord
151917      **   regApp+1: integer value used to ensure keys are unique
151918      **   regApp+2: output of MakeRecord
151919      */
151920      ExprList *pList = pWin->pOwner->x.pList;
151921      KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0);
151922      pWin->csrApp = pParse->nTab++;
151923      pWin->regApp = pParse->nMem+1;
151924      pParse->nMem += 3;
151925      if( pKeyInfo && pWin->pFunc->zName[1]=='i' ){
151926        assert( pKeyInfo->aSortFlags[0]==0 );
151927        pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC;
151928      }
151929      sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
151930      sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
151931      sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
151932    }
151933    else if( p->zName==nth_valueName || p->zName==first_valueName ){
151934      /* Allocate two registers at pWin->regApp. These will be used to
151935      ** store the start and end index of the current frame.  */
151936      pWin->regApp = pParse->nMem+1;
151937      pWin->csrApp = pParse->nTab++;
151938      pParse->nMem += 2;
151939      sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
151940    }
151941    else if( p->zName==leadName || p->zName==lagName ){
151942      pWin->csrApp = pParse->nTab++;
151943      sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
151944    }
151945  }
151946}
151947
151948#define WINDOW_STARTING_INT  0
151949#define WINDOW_ENDING_INT    1
151950#define WINDOW_NTH_VALUE_INT 2
151951#define WINDOW_STARTING_NUM  3
151952#define WINDOW_ENDING_NUM    4
151953
151954/*
151955** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the
151956** value of the second argument to nth_value() (eCond==2) has just been
151957** evaluated and the result left in register reg. This function generates VM
151958** code to check that the value is a non-negative integer and throws an
151959** exception if it is not.
151960*/
151961static void windowCheckValue(Parse *pParse, int reg, int eCond){
151962  static const char *azErr[] = {
151963    "frame starting offset must be a non-negative integer",
151964    "frame ending offset must be a non-negative integer",
151965    "second argument to nth_value must be a positive integer",
151966    "frame starting offset must be a non-negative number",
151967    "frame ending offset must be a non-negative number",
151968  };
151969  static int aOp[] = { OP_Ge, OP_Ge, OP_Gt, OP_Ge, OP_Ge };
151970  Vdbe *v = sqlite3GetVdbe(pParse);
151971  int regZero = sqlite3GetTempReg(pParse);
151972  assert( eCond>=0 && eCond<ArraySize(azErr) );
151973  sqlite3VdbeAddOp2(v, OP_Integer, 0, regZero);
151974  if( eCond>=WINDOW_STARTING_NUM ){
151975    int regString = sqlite3GetTempReg(pParse);
151976    sqlite3VdbeAddOp4(v, OP_String8, 0, regString, 0, "", P4_STATIC);
151977    sqlite3VdbeAddOp3(v, OP_Ge, regString, sqlite3VdbeCurrentAddr(v)+2, reg);
151978    sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC|SQLITE_JUMPIFNULL);
151979    VdbeCoverage(v);
151980    assert( eCond==3 || eCond==4 );
151981    VdbeCoverageIf(v, eCond==3);
151982    VdbeCoverageIf(v, eCond==4);
151983  }else{
151984    sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
151985    VdbeCoverage(v);
151986    assert( eCond==0 || eCond==1 || eCond==2 );
151987    VdbeCoverageIf(v, eCond==0);
151988    VdbeCoverageIf(v, eCond==1);
151989    VdbeCoverageIf(v, eCond==2);
151990  }
151991  sqlite3VdbeAddOp3(v, aOp[eCond], regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
151992  VdbeCoverageNeverNullIf(v, eCond==0); /* NULL case captured by */
151993  VdbeCoverageNeverNullIf(v, eCond==1); /*   the OP_MustBeInt */
151994  VdbeCoverageNeverNullIf(v, eCond==2);
151995  VdbeCoverageNeverNullIf(v, eCond==3); /* NULL case caught by */
151996  VdbeCoverageNeverNullIf(v, eCond==4); /*   the OP_Ge */
151997  sqlite3MayAbort(pParse);
151998  sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort);
151999  sqlite3VdbeAppendP4(v, (void*)azErr[eCond], P4_STATIC);
152000  sqlite3ReleaseTempReg(pParse, regZero);
152001}
152002
152003/*
152004** Return the number of arguments passed to the window-function associated
152005** with the object passed as the only argument to this function.
152006*/
152007static int windowArgCount(Window *pWin){
152008  ExprList *pList = pWin->pOwner->x.pList;
152009  return (pList ? pList->nExpr : 0);
152010}
152011
152012typedef struct WindowCodeArg WindowCodeArg;
152013typedef struct WindowCsrAndReg WindowCsrAndReg;
152014
152015/*
152016** See comments above struct WindowCodeArg.
152017*/
152018struct WindowCsrAndReg {
152019  int csr;                        /* Cursor number */
152020  int reg;                        /* First in array of peer values */
152021};
152022
152023/*
152024** A single instance of this structure is allocated on the stack by
152025** sqlite3WindowCodeStep() and a pointer to it passed to the various helper
152026** routines. This is to reduce the number of arguments required by each
152027** helper function.
152028**
152029** regArg:
152030**   Each window function requires an accumulator register (just as an
152031**   ordinary aggregate function does). This variable is set to the first
152032**   in an array of accumulator registers - one for each window function
152033**   in the WindowCodeArg.pMWin list.
152034**
152035** eDelete:
152036**   The window functions implementation sometimes caches the input rows
152037**   that it processes in a temporary table. If it is not zero, this
152038**   variable indicates when rows may be removed from the temp table (in
152039**   order to reduce memory requirements - it would always be safe just
152040**   to leave them there). Possible values for eDelete are:
152041**
152042**      WINDOW_RETURN_ROW:
152043**        An input row can be discarded after it is returned to the caller.
152044**
152045**      WINDOW_AGGINVERSE:
152046**        An input row can be discarded after the window functions xInverse()
152047**        callbacks have been invoked in it.
152048**
152049**      WINDOW_AGGSTEP:
152050**        An input row can be discarded after the window functions xStep()
152051**        callbacks have been invoked in it.
152052**
152053** start,current,end
152054**   Consider a window-frame similar to the following:
152055**
152056**     (ORDER BY a, b GROUPS BETWEEN 2 PRECEDING AND 2 FOLLOWING)
152057**
152058**   The windows functions implmentation caches the input rows in a temp
152059**   table, sorted by "a, b" (it actually populates the cache lazily, and
152060**   aggressively removes rows once they are no longer required, but that's
152061**   a mere detail). It keeps three cursors open on the temp table. One
152062**   (current) that points to the next row to return to the query engine
152063**   once its window function values have been calculated. Another (end)
152064**   points to the next row to call the xStep() method of each window function
152065**   on (so that it is 2 groups ahead of current). And a third (start) that
152066**   points to the next row to call the xInverse() method of each window
152067**   function on.
152068**
152069**   Each cursor (start, current and end) consists of a VDBE cursor
152070**   (WindowCsrAndReg.csr) and an array of registers (starting at
152071**   WindowCodeArg.reg) that always contains a copy of the peer values
152072**   read from the corresponding cursor.
152073**
152074**   Depending on the window-frame in question, all three cursors may not
152075**   be required. In this case both WindowCodeArg.csr and reg are set to
152076**   0.
152077*/
152078struct WindowCodeArg {
152079  Parse *pParse;             /* Parse context */
152080  Window *pMWin;             /* First in list of functions being processed */
152081  Vdbe *pVdbe;               /* VDBE object */
152082  int addrGosub;             /* OP_Gosub to this address to return one row */
152083  int regGosub;              /* Register used with OP_Gosub(addrGosub) */
152084  int regArg;                /* First in array of accumulator registers */
152085  int eDelete;               /* See above */
152086
152087  WindowCsrAndReg start;
152088  WindowCsrAndReg current;
152089  WindowCsrAndReg end;
152090};
152091
152092/*
152093** Generate VM code to read the window frames peer values from cursor csr into
152094** an array of registers starting at reg.
152095*/
152096static void windowReadPeerValues(
152097  WindowCodeArg *p,
152098  int csr,
152099  int reg
152100){
152101  Window *pMWin = p->pMWin;
152102  ExprList *pOrderBy = pMWin->pOrderBy;
152103  if( pOrderBy ){
152104    Vdbe *v = sqlite3GetVdbe(p->pParse);
152105    ExprList *pPart = pMWin->pPartition;
152106    int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
152107    int i;
152108    for(i=0; i<pOrderBy->nExpr; i++){
152109      sqlite3VdbeAddOp3(v, OP_Column, csr, iColOff+i, reg+i);
152110    }
152111  }
152112}
152113
152114/*
152115** Generate VM code to invoke either xStep() (if bInverse is 0) or
152116** xInverse (if bInverse is non-zero) for each window function in the
152117** linked list starting at pMWin. Or, for built-in window functions
152118** that do not use the standard function API, generate the required
152119** inline VM code.
152120**
152121** If argument csr is greater than or equal to 0, then argument reg is
152122** the first register in an array of registers guaranteed to be large
152123** enough to hold the array of arguments for each function. In this case
152124** the arguments are extracted from the current row of csr into the
152125** array of registers before invoking OP_AggStep or OP_AggInverse
152126**
152127** Or, if csr is less than zero, then the array of registers at reg is
152128** already populated with all columns from the current row of the sub-query.
152129**
152130** If argument regPartSize is non-zero, then it is a register containing the
152131** number of rows in the current partition.
152132*/
152133static void windowAggStep(
152134  WindowCodeArg *p,
152135  Window *pMWin,                  /* Linked list of window functions */
152136  int csr,                        /* Read arguments from this cursor */
152137  int bInverse,                   /* True to invoke xInverse instead of xStep */
152138  int reg                         /* Array of registers */
152139){
152140  Parse *pParse = p->pParse;
152141  Vdbe *v = sqlite3GetVdbe(pParse);
152142  Window *pWin;
152143  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
152144    FuncDef *pFunc = pWin->pFunc;
152145    int regArg;
152146    int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
152147    int i;
152148
152149    assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
152150
152151    /* All OVER clauses in the same window function aggregate step must
152152    ** be the same. */
152153    assert( pWin==pMWin || sqlite3WindowCompare(pParse,pWin,pMWin,0)!=1 );
152154
152155    for(i=0; i<nArg; i++){
152156      if( i!=1 || pFunc->zName!=nth_valueName ){
152157        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
152158      }else{
152159        sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
152160      }
152161    }
152162    regArg = reg;
152163
152164    if( pMWin->regStartRowid==0
152165     && (pFunc->funcFlags & SQLITE_FUNC_MINMAX)
152166     && (pWin->eStart!=TK_UNBOUNDED)
152167    ){
152168      int addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regArg);
152169      VdbeCoverage(v);
152170      if( bInverse==0 ){
152171        sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
152172        sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
152173        sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
152174        sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
152175      }else{
152176        sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
152177        VdbeCoverageNeverTaken(v);
152178        sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
152179        sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
152180      }
152181      sqlite3VdbeJumpHere(v, addrIsNull);
152182    }else if( pWin->regApp ){
152183      assert( pFunc->zName==nth_valueName
152184           || pFunc->zName==first_valueName
152185      );
152186      assert( bInverse==0 || bInverse==1 );
152187      sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
152188    }else if( pFunc->xSFunc!=noopStepFunc ){
152189      int addrIf = 0;
152190      if( pWin->pFilter ){
152191        int regTmp;
152192        assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
152193        assert( pWin->bExprArgs || nArg  ||pWin->pOwner->x.pList==0 );
152194        regTmp = sqlite3GetTempReg(pParse);
152195        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
152196        addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
152197        VdbeCoverage(v);
152198        sqlite3ReleaseTempReg(pParse, regTmp);
152199      }
152200
152201      if( pWin->bExprArgs ){
152202        int iStart = sqlite3VdbeCurrentAddr(v);
152203        VdbeOp *pOp, *pEnd;
152204
152205        nArg = pWin->pOwner->x.pList->nExpr;
152206        regArg = sqlite3GetTempRange(pParse, nArg);
152207        sqlite3ExprCodeExprList(pParse, pWin->pOwner->x.pList, regArg, 0, 0);
152208
152209        pEnd = sqlite3VdbeGetOp(v, -1);
152210        for(pOp=sqlite3VdbeGetOp(v, iStart); pOp<=pEnd; pOp++){
152211          if( pOp->opcode==OP_Column && pOp->p1==pWin->iEphCsr ){
152212            pOp->p1 = csr;
152213          }
152214        }
152215      }
152216      if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
152217        CollSeq *pColl;
152218        assert( nArg>0 );
152219        pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
152220        sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ);
152221      }
152222      sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse : OP_AggStep,
152223                        bInverse, regArg, pWin->regAccum);
152224      sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF);
152225      sqlite3VdbeChangeP5(v, (u8)nArg);
152226      if( pWin->bExprArgs ){
152227        sqlite3ReleaseTempRange(pParse, regArg, nArg);
152228      }
152229      if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
152230    }
152231  }
152232}
152233
152234/*
152235** Values that may be passed as the second argument to windowCodeOp().
152236*/
152237#define WINDOW_RETURN_ROW 1
152238#define WINDOW_AGGINVERSE 2
152239#define WINDOW_AGGSTEP    3
152240
152241/*
152242** Generate VM code to invoke either xValue() (bFin==0) or xFinalize()
152243** (bFin==1) for each window function in the linked list starting at
152244** pMWin. Or, for built-in window-functions that do not use the standard
152245** API, generate the equivalent VM code.
152246*/
152247static void windowAggFinal(WindowCodeArg *p, int bFin){
152248  Parse *pParse = p->pParse;
152249  Window *pMWin = p->pMWin;
152250  Vdbe *v = sqlite3GetVdbe(pParse);
152251  Window *pWin;
152252
152253  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
152254    if( pMWin->regStartRowid==0
152255     && (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
152256     && (pWin->eStart!=TK_UNBOUNDED)
152257    ){
152258      sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
152259      sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
152260      VdbeCoverage(v);
152261      sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
152262      sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
152263    }else if( pWin->regApp ){
152264      assert( pMWin->regStartRowid==0 );
152265    }else{
152266      int nArg = windowArgCount(pWin);
152267      if( bFin ){
152268        sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg);
152269        sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
152270        sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
152271        sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
152272      }else{
152273        sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult);
152274        sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
152275      }
152276    }
152277  }
152278}
152279
152280/*
152281** Generate code to calculate the current values of all window functions in the
152282** p->pMWin list by doing a full scan of the current window frame. Store the
152283** results in the Window.regResult registers, ready to return the upper
152284** layer.
152285*/
152286static void windowFullScan(WindowCodeArg *p){
152287  Window *pWin;
152288  Parse *pParse = p->pParse;
152289  Window *pMWin = p->pMWin;
152290  Vdbe *v = p->pVdbe;
152291
152292  int regCRowid = 0;              /* Current rowid value */
152293  int regCPeer = 0;               /* Current peer values */
152294  int regRowid = 0;               /* AggStep rowid value */
152295  int regPeer = 0;                /* AggStep peer values */
152296
152297  int nPeer;
152298  int lblNext;
152299  int lblBrk;
152300  int addrNext;
152301  int csr;
152302
152303  VdbeModuleComment((v, "windowFullScan begin"));
152304
152305  assert( pMWin!=0 );
152306  csr = pMWin->csrApp;
152307  nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
152308
152309  lblNext = sqlite3VdbeMakeLabel(pParse);
152310  lblBrk = sqlite3VdbeMakeLabel(pParse);
152311
152312  regCRowid = sqlite3GetTempReg(pParse);
152313  regRowid = sqlite3GetTempReg(pParse);
152314  if( nPeer ){
152315    regCPeer = sqlite3GetTempRange(pParse, nPeer);
152316    regPeer = sqlite3GetTempRange(pParse, nPeer);
152317  }
152318
152319  sqlite3VdbeAddOp2(v, OP_Rowid, pMWin->iEphCsr, regCRowid);
152320  windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
152321
152322  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
152323    sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
152324  }
152325
152326  sqlite3VdbeAddOp3(v, OP_SeekGE, csr, lblBrk, pMWin->regStartRowid);
152327  VdbeCoverage(v);
152328  addrNext = sqlite3VdbeCurrentAddr(v);
152329  sqlite3VdbeAddOp2(v, OP_Rowid, csr, regRowid);
152330  sqlite3VdbeAddOp3(v, OP_Gt, pMWin->regEndRowid, lblBrk, regRowid);
152331  VdbeCoverageNeverNull(v);
152332
152333  if( pMWin->eExclude==TK_CURRENT ){
152334    sqlite3VdbeAddOp3(v, OP_Eq, regCRowid, lblNext, regRowid);
152335    VdbeCoverageNeverNull(v);
152336  }else if( pMWin->eExclude!=TK_NO ){
152337    int addr;
152338    int addrEq = 0;
152339    KeyInfo *pKeyInfo = 0;
152340
152341    if( pMWin->pOrderBy ){
152342      pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
152343    }
152344    if( pMWin->eExclude==TK_TIES ){
152345      addrEq = sqlite3VdbeAddOp3(v, OP_Eq, regCRowid, 0, regRowid);
152346      VdbeCoverageNeverNull(v);
152347    }
152348    if( pKeyInfo ){
152349      windowReadPeerValues(p, csr, regPeer);
152350      sqlite3VdbeAddOp3(v, OP_Compare, regPeer, regCPeer, nPeer);
152351      sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
152352      addr = sqlite3VdbeCurrentAddr(v)+1;
152353      sqlite3VdbeAddOp3(v, OP_Jump, addr, lblNext, addr);
152354      VdbeCoverageEqNe(v);
152355    }else{
152356      sqlite3VdbeAddOp2(v, OP_Goto, 0, lblNext);
152357    }
152358    if( addrEq ) sqlite3VdbeJumpHere(v, addrEq);
152359  }
152360
152361  windowAggStep(p, pMWin, csr, 0, p->regArg);
152362
152363  sqlite3VdbeResolveLabel(v, lblNext);
152364  sqlite3VdbeAddOp2(v, OP_Next, csr, addrNext);
152365  VdbeCoverage(v);
152366  sqlite3VdbeJumpHere(v, addrNext-1);
152367  sqlite3VdbeJumpHere(v, addrNext+1);
152368  sqlite3ReleaseTempReg(pParse, regRowid);
152369  sqlite3ReleaseTempReg(pParse, regCRowid);
152370  if( nPeer ){
152371    sqlite3ReleaseTempRange(pParse, regPeer, nPeer);
152372    sqlite3ReleaseTempRange(pParse, regCPeer, nPeer);
152373  }
152374
152375  windowAggFinal(p, 1);
152376  VdbeModuleComment((v, "windowFullScan end"));
152377}
152378
152379/*
152380** Invoke the sub-routine at regGosub (generated by code in select.c) to
152381** return the current row of Window.iEphCsr. If all window functions are
152382** aggregate window functions that use the standard API, a single
152383** OP_Gosub instruction is all that this routine generates. Extra VM code
152384** for per-row processing is only generated for the following built-in window
152385** functions:
152386**
152387**   nth_value()
152388**   first_value()
152389**   lag()
152390**   lead()
152391*/
152392static void windowReturnOneRow(WindowCodeArg *p){
152393  Window *pMWin = p->pMWin;
152394  Vdbe *v = p->pVdbe;
152395
152396  if( pMWin->regStartRowid ){
152397    windowFullScan(p);
152398  }else{
152399    Parse *pParse = p->pParse;
152400    Window *pWin;
152401
152402    for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
152403      FuncDef *pFunc = pWin->pFunc;
152404      if( pFunc->zName==nth_valueName
152405       || pFunc->zName==first_valueName
152406      ){
152407        int csr = pWin->csrApp;
152408        int lbl = sqlite3VdbeMakeLabel(pParse);
152409        int tmpReg = sqlite3GetTempReg(pParse);
152410        sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
152411
152412        if( pFunc->zName==nth_valueName ){
152413          sqlite3VdbeAddOp3(v, OP_Column,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
152414          windowCheckValue(pParse, tmpReg, 2);
152415        }else{
152416          sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
152417        }
152418        sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
152419        sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
152420        VdbeCoverageNeverNull(v);
152421        sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, 0, tmpReg);
152422        VdbeCoverageNeverTaken(v);
152423        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
152424        sqlite3VdbeResolveLabel(v, lbl);
152425        sqlite3ReleaseTempReg(pParse, tmpReg);
152426      }
152427      else if( pFunc->zName==leadName || pFunc->zName==lagName ){
152428        int nArg = pWin->pOwner->x.pList->nExpr;
152429        int csr = pWin->csrApp;
152430        int lbl = sqlite3VdbeMakeLabel(pParse);
152431        int tmpReg = sqlite3GetTempReg(pParse);
152432        int iEph = pMWin->iEphCsr;
152433
152434        if( nArg<3 ){
152435          sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
152436        }else{
152437          sqlite3VdbeAddOp3(v, OP_Column, iEph,pWin->iArgCol+2,pWin->regResult);
152438        }
152439        sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg);
152440        if( nArg<2 ){
152441          int val = (pFunc->zName==leadName ? 1 : -1);
152442          sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val);
152443        }else{
152444          int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
152445          int tmpReg2 = sqlite3GetTempReg(pParse);
152446          sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
152447          sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
152448          sqlite3ReleaseTempReg(pParse, tmpReg2);
152449        }
152450
152451        sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
152452        VdbeCoverage(v);
152453        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
152454        sqlite3VdbeResolveLabel(v, lbl);
152455        sqlite3ReleaseTempReg(pParse, tmpReg);
152456      }
152457    }
152458  }
152459  sqlite3VdbeAddOp2(v, OP_Gosub, p->regGosub, p->addrGosub);
152460}
152461
152462/*
152463** Generate code to set the accumulator register for each window function
152464** in the linked list passed as the second argument to NULL. And perform
152465** any equivalent initialization required by any built-in window functions
152466** in the list.
152467*/
152468static int windowInitAccum(Parse *pParse, Window *pMWin){
152469  Vdbe *v = sqlite3GetVdbe(pParse);
152470  int regArg;
152471  int nArg = 0;
152472  Window *pWin;
152473  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
152474    FuncDef *pFunc = pWin->pFunc;
152475    assert( pWin->regAccum );
152476    sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
152477    nArg = MAX(nArg, windowArgCount(pWin));
152478    if( pMWin->regStartRowid==0 ){
152479      if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
152480        sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
152481        sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
152482      }
152483
152484      if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
152485        assert( pWin->eStart!=TK_UNBOUNDED );
152486        sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
152487        sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
152488      }
152489    }
152490  }
152491  regArg = pParse->nMem+1;
152492  pParse->nMem += nArg;
152493  return regArg;
152494}
152495
152496/*
152497** Return true if the current frame should be cached in the ephemeral table,
152498** even if there are no xInverse() calls required.
152499*/
152500static int windowCacheFrame(Window *pMWin){
152501  Window *pWin;
152502  if( pMWin->regStartRowid ) return 1;
152503  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
152504    FuncDef *pFunc = pWin->pFunc;
152505    if( (pFunc->zName==nth_valueName)
152506     || (pFunc->zName==first_valueName)
152507     || (pFunc->zName==leadName)
152508     || (pFunc->zName==lagName)
152509    ){
152510      return 1;
152511    }
152512  }
152513  return 0;
152514}
152515
152516/*
152517** regOld and regNew are each the first register in an array of size
152518** pOrderBy->nExpr. This function generates code to compare the two
152519** arrays of registers using the collation sequences and other comparison
152520** parameters specified by pOrderBy.
152521**
152522** If the two arrays are not equal, the contents of regNew is copied to
152523** regOld and control falls through. Otherwise, if the contents of the arrays
152524** are equal, an OP_Goto is executed. The address of the OP_Goto is returned.
152525*/
152526static void windowIfNewPeer(
152527  Parse *pParse,
152528  ExprList *pOrderBy,
152529  int regNew,                     /* First in array of new values */
152530  int regOld,                     /* First in array of old values */
152531  int addr                        /* Jump here */
152532){
152533  Vdbe *v = sqlite3GetVdbe(pParse);
152534  if( pOrderBy ){
152535    int nVal = pOrderBy->nExpr;
152536    KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
152537    sqlite3VdbeAddOp3(v, OP_Compare, regOld, regNew, nVal);
152538    sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
152539    sqlite3VdbeAddOp3(v, OP_Jump,
152540      sqlite3VdbeCurrentAddr(v)+1, addr, sqlite3VdbeCurrentAddr(v)+1
152541    );
152542    VdbeCoverageEqNe(v);
152543    sqlite3VdbeAddOp3(v, OP_Copy, regNew, regOld, nVal-1);
152544  }else{
152545    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
152546  }
152547}
152548
152549/*
152550** This function is called as part of generating VM programs for RANGE
152551** offset PRECEDING/FOLLOWING frame boundaries. Assuming "ASC" order for
152552** the ORDER BY term in the window, and that argument op is OP_Ge, it generates
152553** code equivalent to:
152554**
152555**   if( csr1.peerVal + regVal >= csr2.peerVal ) goto lbl;
152556**
152557** The value of parameter op may also be OP_Gt or OP_Le. In these cases the
152558** operator in the above pseudo-code is replaced with ">" or "<=", respectively.
152559**
152560** If the sort-order for the ORDER BY term in the window is DESC, then the
152561** comparison is reversed. Instead of adding regVal to csr1.peerVal, it is
152562** subtracted. And the comparison operator is inverted to - ">=" becomes "<=",
152563** ">" becomes "<", and so on. So, with DESC sort order, if the argument op
152564** is OP_Ge, the generated code is equivalent to:
152565**
152566**   if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl;
152567**
152568** A special type of arithmetic is used such that if csr1.peerVal is not
152569** a numeric type (real or integer), then the result of the addition addition
152570** or subtraction is a a copy of csr1.peerVal.
152571*/
152572static void windowCodeRangeTest(
152573  WindowCodeArg *p,
152574  int op,                         /* OP_Ge, OP_Gt, or OP_Le */
152575  int csr1,                       /* Cursor number for cursor 1 */
152576  int regVal,                     /* Register containing non-negative number */
152577  int csr2,                       /* Cursor number for cursor 2 */
152578  int lbl                         /* Jump destination if condition is true */
152579){
152580  Parse *pParse = p->pParse;
152581  Vdbe *v = sqlite3GetVdbe(pParse);
152582  ExprList *pOrderBy = p->pMWin->pOrderBy;  /* ORDER BY clause for window */
152583  int reg1 = sqlite3GetTempReg(pParse);     /* Reg. for csr1.peerVal+regVal */
152584  int reg2 = sqlite3GetTempReg(pParse);     /* Reg. for csr2.peerVal */
152585  int regString = ++pParse->nMem;           /* Reg. for constant value '' */
152586  int arith = OP_Add;                       /* OP_Add or OP_Subtract */
152587  int addrGe;                               /* Jump destination */
152588
152589  assert( op==OP_Ge || op==OP_Gt || op==OP_Le );
152590  assert( pOrderBy && pOrderBy->nExpr==1 );
152591  if( pOrderBy->a[0].sortFlags & KEYINFO_ORDER_DESC ){
152592    switch( op ){
152593      case OP_Ge: op = OP_Le; break;
152594      case OP_Gt: op = OP_Lt; break;
152595      default: assert( op==OP_Le ); op = OP_Ge; break;
152596    }
152597    arith = OP_Subtract;
152598  }
152599
152600  /* Read the peer-value from each cursor into a register */
152601  windowReadPeerValues(p, csr1, reg1);
152602  windowReadPeerValues(p, csr2, reg2);
152603
152604  VdbeModuleComment((v, "CodeRangeTest: if( R%d %s R%d %s R%d ) goto lbl",
152605      reg1, (arith==OP_Add ? "+" : "-"), regVal,
152606      ((op==OP_Ge) ? ">=" : (op==OP_Le) ? "<=" : (op==OP_Gt) ? ">" : "<"), reg2
152607  ));
152608
152609  /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
152610  ** This block adds (or subtracts for DESC) the numeric value in regVal
152611  ** from it. Or, if reg1 is not numeric (it is a NULL, a text value or a blob),
152612  ** then leave reg1 as it is. In pseudo-code, this is implemented as:
152613  **
152614  **   if( reg1>='' ) goto addrGe;
152615  **   reg1 = reg1 +/- regVal
152616  **   addrGe:
152617  **
152618  ** Since all strings and blobs are greater-than-or-equal-to an empty string,
152619  ** the add/subtract is skipped for these, as required. If reg1 is a NULL,
152620  ** then the arithmetic is performed, but since adding or subtracting from
152621  ** NULL is always NULL anyway, this case is handled as required too.  */
152622  sqlite3VdbeAddOp4(v, OP_String8, 0, regString, 0, "", P4_STATIC);
152623  addrGe = sqlite3VdbeAddOp3(v, OP_Ge, regString, 0, reg1);
152624  VdbeCoverage(v);
152625  sqlite3VdbeAddOp3(v, arith, regVal, reg1, reg1);
152626  sqlite3VdbeJumpHere(v, addrGe);
152627
152628  /* If the BIGNULL flag is set for the ORDER BY, then it is required to
152629  ** consider NULL values to be larger than all other values, instead of
152630  ** the usual smaller. The VDBE opcodes OP_Ge and so on do not handle this
152631  ** (and adding that capability causes a performance regression), so
152632  ** instead if the BIGNULL flag is set then cases where either reg1 or
152633  ** reg2 are NULL are handled separately in the following block. The code
152634  ** generated is equivalent to:
152635  **
152636  **   if( reg1 IS NULL ){
152637  **     if( op==OP_Ge ) goto lbl;
152638  **     if( op==OP_Gt && reg2 IS NOT NULL ) goto lbl;
152639  **     if( op==OP_Le && reg2 IS NULL ) goto lbl;
152640  **   }else if( reg2 IS NULL ){
152641  **     if( op==OP_Le ) goto lbl;
152642  **   }
152643  **
152644  ** Additionally, if either reg1 or reg2 are NULL but the jump to lbl is
152645  ** not taken, control jumps over the comparison operator coded below this
152646  ** block.  */
152647  if( pOrderBy->a[0].sortFlags & KEYINFO_ORDER_BIGNULL ){
152648    /* This block runs if reg1 contains a NULL. */
152649    int addr = sqlite3VdbeAddOp1(v, OP_NotNull, reg1); VdbeCoverage(v);
152650    switch( op ){
152651      case OP_Ge:
152652        sqlite3VdbeAddOp2(v, OP_Goto, 0, lbl);
152653        break;
152654      case OP_Gt:
152655        sqlite3VdbeAddOp2(v, OP_NotNull, reg2, lbl);
152656        VdbeCoverage(v);
152657        break;
152658      case OP_Le:
152659        sqlite3VdbeAddOp2(v, OP_IsNull, reg2, lbl);
152660        VdbeCoverage(v);
152661        break;
152662      default: assert( op==OP_Lt ); /* no-op */ break;
152663    }
152664    sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+3);
152665
152666    /* This block runs if reg1 is not NULL, but reg2 is. */
152667    sqlite3VdbeJumpHere(v, addr);
152668    sqlite3VdbeAddOp2(v, OP_IsNull, reg2, lbl); VdbeCoverage(v);
152669    if( op==OP_Gt || op==OP_Ge ){
152670      sqlite3VdbeChangeP2(v, -1, sqlite3VdbeCurrentAddr(v)+1);
152671    }
152672  }
152673
152674  /* Compare registers reg2 and reg1, taking the jump if required. Note that
152675  ** control skips over this test if the BIGNULL flag is set and either
152676  ** reg1 or reg2 contain a NULL value.  */
152677  sqlite3VdbeAddOp3(v, op, reg2, lbl, reg1); VdbeCoverage(v);
152678  sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
152679
152680  assert( op==OP_Ge || op==OP_Gt || op==OP_Lt || op==OP_Le );
152681  testcase(op==OP_Ge); VdbeCoverageIf(v, op==OP_Ge);
152682  testcase(op==OP_Lt); VdbeCoverageIf(v, op==OP_Lt);
152683  testcase(op==OP_Le); VdbeCoverageIf(v, op==OP_Le);
152684  testcase(op==OP_Gt); VdbeCoverageIf(v, op==OP_Gt);
152685  sqlite3ReleaseTempReg(pParse, reg1);
152686  sqlite3ReleaseTempReg(pParse, reg2);
152687
152688  VdbeModuleComment((v, "CodeRangeTest: end"));
152689}
152690
152691/*
152692** Helper function for sqlite3WindowCodeStep(). Each call to this function
152693** generates VM code for a single RETURN_ROW, AGGSTEP or AGGINVERSE
152694** operation. Refer to the header comment for sqlite3WindowCodeStep() for
152695** details.
152696*/
152697static int windowCodeOp(
152698 WindowCodeArg *p,                /* Context object */
152699 int op,                          /* WINDOW_RETURN_ROW, AGGSTEP or AGGINVERSE */
152700 int regCountdown,                /* Register for OP_IfPos countdown */
152701 int jumpOnEof                    /* Jump here if stepped cursor reaches EOF */
152702){
152703  int csr, reg;
152704  Parse *pParse = p->pParse;
152705  Window *pMWin = p->pMWin;
152706  int ret = 0;
152707  Vdbe *v = p->pVdbe;
152708  int addrContinue = 0;
152709  int bPeer = (pMWin->eFrmType!=TK_ROWS);
152710
152711  int lblDone = sqlite3VdbeMakeLabel(pParse);
152712  int addrNextRange = 0;
152713
152714  /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
152715  ** starts with UNBOUNDED PRECEDING. */
152716  if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){
152717    assert( regCountdown==0 && jumpOnEof==0 );
152718    return 0;
152719  }
152720
152721  if( regCountdown>0 ){
152722    if( pMWin->eFrmType==TK_RANGE ){
152723      addrNextRange = sqlite3VdbeCurrentAddr(v);
152724      assert( op==WINDOW_AGGINVERSE || op==WINDOW_AGGSTEP );
152725      if( op==WINDOW_AGGINVERSE ){
152726        if( pMWin->eStart==TK_FOLLOWING ){
152727          windowCodeRangeTest(
152728              p, OP_Le, p->current.csr, regCountdown, p->start.csr, lblDone
152729          );
152730        }else{
152731          windowCodeRangeTest(
152732              p, OP_Ge, p->start.csr, regCountdown, p->current.csr, lblDone
152733          );
152734        }
152735      }else{
152736        windowCodeRangeTest(
152737            p, OP_Gt, p->end.csr, regCountdown, p->current.csr, lblDone
152738        );
152739      }
152740    }else{
152741      sqlite3VdbeAddOp3(v, OP_IfPos, regCountdown, lblDone, 1);
152742      VdbeCoverage(v);
152743    }
152744  }
152745
152746  if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){
152747    windowAggFinal(p, 0);
152748  }
152749  addrContinue = sqlite3VdbeCurrentAddr(v);
152750
152751  /* If this is a (RANGE BETWEEN a FOLLOWING AND b FOLLOWING) or
152752  ** (RANGE BETWEEN b PRECEDING AND a PRECEDING) frame, ensure the
152753  ** start cursor does not advance past the end cursor within the
152754  ** temporary table. It otherwise might, if (a>b).  */
152755  if( pMWin->eStart==pMWin->eEnd && regCountdown
152756   && pMWin->eFrmType==TK_RANGE && op==WINDOW_AGGINVERSE
152757  ){
152758    int regRowid1 = sqlite3GetTempReg(pParse);
152759    int regRowid2 = sqlite3GetTempReg(pParse);
152760    sqlite3VdbeAddOp2(v, OP_Rowid, p->start.csr, regRowid1);
152761    sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid2);
152762    sqlite3VdbeAddOp3(v, OP_Ge, regRowid2, lblDone, regRowid1);
152763    VdbeCoverage(v);
152764    sqlite3ReleaseTempReg(pParse, regRowid1);
152765    sqlite3ReleaseTempReg(pParse, regRowid2);
152766    assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING );
152767  }
152768
152769  switch( op ){
152770    case WINDOW_RETURN_ROW:
152771      csr = p->current.csr;
152772      reg = p->current.reg;
152773      windowReturnOneRow(p);
152774      break;
152775
152776    case WINDOW_AGGINVERSE:
152777      csr = p->start.csr;
152778      reg = p->start.reg;
152779      if( pMWin->regStartRowid ){
152780        assert( pMWin->regEndRowid );
152781        sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regStartRowid, 1);
152782      }else{
152783        windowAggStep(p, pMWin, csr, 1, p->regArg);
152784      }
152785      break;
152786
152787    default:
152788      assert( op==WINDOW_AGGSTEP );
152789      csr = p->end.csr;
152790      reg = p->end.reg;
152791      if( pMWin->regStartRowid ){
152792        assert( pMWin->regEndRowid );
152793        sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regEndRowid, 1);
152794      }else{
152795        windowAggStep(p, pMWin, csr, 0, p->regArg);
152796      }
152797      break;
152798  }
152799
152800  if( op==p->eDelete ){
152801    sqlite3VdbeAddOp1(v, OP_Delete, csr);
152802    sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
152803  }
152804
152805  if( jumpOnEof ){
152806    sqlite3VdbeAddOp2(v, OP_Next, csr, sqlite3VdbeCurrentAddr(v)+2);
152807    VdbeCoverage(v);
152808    ret = sqlite3VdbeAddOp0(v, OP_Goto);
152809  }else{
152810    sqlite3VdbeAddOp2(v, OP_Next, csr, sqlite3VdbeCurrentAddr(v)+1+bPeer);
152811    VdbeCoverage(v);
152812    if( bPeer ){
152813      sqlite3VdbeAddOp2(v, OP_Goto, 0, lblDone);
152814    }
152815  }
152816
152817  if( bPeer ){
152818    int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
152819    int regTmp = (nReg ? sqlite3GetTempRange(pParse, nReg) : 0);
152820    windowReadPeerValues(p, csr, regTmp);
152821    windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
152822    sqlite3ReleaseTempRange(pParse, regTmp, nReg);
152823  }
152824
152825  if( addrNextRange ){
152826    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNextRange);
152827  }
152828  sqlite3VdbeResolveLabel(v, lblDone);
152829  return ret;
152830}
152831
152832
152833/*
152834** Allocate and return a duplicate of the Window object indicated by the
152835** third argument. Set the Window.pOwner field of the new object to
152836** pOwner.
152837*/
152838SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
152839  Window *pNew = 0;
152840  if( ALWAYS(p) ){
152841    pNew = sqlite3DbMallocZero(db, sizeof(Window));
152842    if( pNew ){
152843      pNew->zName = sqlite3DbStrDup(db, p->zName);
152844      pNew->zBase = sqlite3DbStrDup(db, p->zBase);
152845      pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
152846      pNew->pFunc = p->pFunc;
152847      pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
152848      pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
152849      pNew->eFrmType = p->eFrmType;
152850      pNew->eEnd = p->eEnd;
152851      pNew->eStart = p->eStart;
152852      pNew->eExclude = p->eExclude;
152853      pNew->regResult = p->regResult;
152854      pNew->regAccum = p->regAccum;
152855      pNew->iArgCol = p->iArgCol;
152856      pNew->iEphCsr = p->iEphCsr;
152857      pNew->bExprArgs = p->bExprArgs;
152858      pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
152859      pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
152860      pNew->pOwner = pOwner;
152861      pNew->bImplicitFrame = p->bImplicitFrame;
152862    }
152863  }
152864  return pNew;
152865}
152866
152867/*
152868** Return a copy of the linked list of Window objects passed as the
152869** second argument.
152870*/
152871SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p){
152872  Window *pWin;
152873  Window *pRet = 0;
152874  Window **pp = &pRet;
152875
152876  for(pWin=p; pWin; pWin=pWin->pNextWin){
152877    *pp = sqlite3WindowDup(db, 0, pWin);
152878    if( *pp==0 ) break;
152879    pp = &((*pp)->pNextWin);
152880  }
152881
152882  return pRet;
152883}
152884
152885/*
152886** Return true if it can be determined at compile time that expression
152887** pExpr evaluates to a value that, when cast to an integer, is greater
152888** than zero. False otherwise.
152889**
152890** If an OOM error occurs, this function sets the Parse.db.mallocFailed
152891** flag and returns zero.
152892*/
152893static int windowExprGtZero(Parse *pParse, Expr *pExpr){
152894  int ret = 0;
152895  sqlite3 *db = pParse->db;
152896  sqlite3_value *pVal = 0;
152897  sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC, &pVal);
152898  if( pVal && sqlite3_value_int(pVal)>0 ){
152899    ret = 1;
152900  }
152901  sqlite3ValueFree(pVal);
152902  return ret;
152903}
152904
152905/*
152906** sqlite3WhereBegin() has already been called for the SELECT statement
152907** passed as the second argument when this function is invoked. It generates
152908** code to populate the Window.regResult register for each window function
152909** and invoke the sub-routine at instruction addrGosub once for each row.
152910** sqlite3WhereEnd() is always called before returning.
152911**
152912** This function handles several different types of window frames, which
152913** require slightly different processing. The following pseudo code is
152914** used to implement window frames of the form:
152915**
152916**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
152917**
152918** Other window frame types use variants of the following:
152919**
152920**     ... loop started by sqlite3WhereBegin() ...
152921**       if( new partition ){
152922**         Gosub flush
152923**       }
152924**       Insert new row into eph table.
152925**
152926**       if( first row of partition ){
152927**         // Rewind three cursors, all open on the eph table.
152928**         Rewind(csrEnd);
152929**         Rewind(csrStart);
152930**         Rewind(csrCurrent);
152931**
152932**         regEnd = <expr2>          // FOLLOWING expression
152933**         regStart = <expr1>        // PRECEDING expression
152934**       }else{
152935**         // First time this branch is taken, the eph table contains two
152936**         // rows. The first row in the partition, which all three cursors
152937**         // currently point to, and the following row.
152938**         AGGSTEP
152939**         if( (regEnd--)<=0 ){
152940**           RETURN_ROW
152941**           if( (regStart--)<=0 ){
152942**             AGGINVERSE
152943**           }
152944**         }
152945**       }
152946**     }
152947**     flush:
152948**       AGGSTEP
152949**       while( 1 ){
152950**         RETURN ROW
152951**         if( csrCurrent is EOF ) break;
152952**         if( (regStart--)<=0 ){
152953**           AggInverse(csrStart)
152954**           Next(csrStart)
152955**         }
152956**       }
152957**
152958** The pseudo-code above uses the following shorthand:
152959**
152960**   AGGSTEP:    invoke the aggregate xStep() function for each window function
152961**               with arguments read from the current row of cursor csrEnd, then
152962**               step cursor csrEnd forward one row (i.e. sqlite3BtreeNext()).
152963**
152964**   RETURN_ROW: return a row to the caller based on the contents of the
152965**               current row of csrCurrent and the current state of all
152966**               aggregates. Then step cursor csrCurrent forward one row.
152967**
152968**   AGGINVERSE: invoke the aggregate xInverse() function for each window
152969**               functions with arguments read from the current row of cursor
152970**               csrStart. Then step csrStart forward one row.
152971**
152972** There are two other ROWS window frames that are handled significantly
152973** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
152974** and "BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING". These are special
152975** cases because they change the order in which the three cursors (csrStart,
152976** csrCurrent and csrEnd) iterate through the ephemeral table. Cases that
152977** use UNBOUNDED or CURRENT ROW are much simpler variations on one of these
152978** three.
152979**
152980**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
152981**
152982**     ... loop started by sqlite3WhereBegin() ...
152983**       if( new partition ){
152984**         Gosub flush
152985**       }
152986**       Insert new row into eph table.
152987**       if( first row of partition ){
152988**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
152989**         regEnd = <expr2>
152990**         regStart = <expr1>
152991**       }else{
152992**         if( (regEnd--)<=0 ){
152993**           AGGSTEP
152994**         }
152995**         RETURN_ROW
152996**         if( (regStart--)<=0 ){
152997**           AGGINVERSE
152998**         }
152999**       }
153000**     }
153001**     flush:
153002**       if( (regEnd--)<=0 ){
153003**         AGGSTEP
153004**       }
153005**       RETURN_ROW
153006**
153007**
153008**   ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
153009**
153010**     ... loop started by sqlite3WhereBegin() ...
153011**     if( new partition ){
153012**       Gosub flush
153013**     }
153014**     Insert new row into eph table.
153015**     if( first row of partition ){
153016**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
153017**       regEnd = <expr2>
153018**       regStart = regEnd - <expr1>
153019**     }else{
153020**       AGGSTEP
153021**       if( (regEnd--)<=0 ){
153022**         RETURN_ROW
153023**       }
153024**       if( (regStart--)<=0 ){
153025**         AGGINVERSE
153026**       }
153027**     }
153028**   }
153029**   flush:
153030**     AGGSTEP
153031**     while( 1 ){
153032**       if( (regEnd--)<=0 ){
153033**         RETURN_ROW
153034**         if( eof ) break;
153035**       }
153036**       if( (regStart--)<=0 ){
153037**         AGGINVERSE
153038**         if( eof ) break
153039**       }
153040**     }
153041**     while( !eof csrCurrent ){
153042**       RETURN_ROW
153043**     }
153044**
153045** For the most part, the patterns above are adapted to support UNBOUNDED by
153046** assuming that it is equivalent to "infinity PRECEDING/FOLLOWING" and
153047** CURRENT ROW by assuming that it is equivilent to "0 PRECEDING/FOLLOWING".
153048** This is optimized of course - branches that will never be taken and
153049** conditions that are always true are omitted from the VM code. The only
153050** exceptional case is:
153051**
153052**   ROWS BETWEEN <expr1> FOLLOWING AND UNBOUNDED FOLLOWING
153053**
153054**     ... loop started by sqlite3WhereBegin() ...
153055**     if( new partition ){
153056**       Gosub flush
153057**     }
153058**     Insert new row into eph table.
153059**     if( first row of partition ){
153060**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
153061**       regStart = <expr1>
153062**     }else{
153063**       AGGSTEP
153064**     }
153065**   }
153066**   flush:
153067**     AGGSTEP
153068**     while( 1 ){
153069**       if( (regStart--)<=0 ){
153070**         AGGINVERSE
153071**         if( eof ) break
153072**       }
153073**       RETURN_ROW
153074**     }
153075**     while( !eof csrCurrent ){
153076**       RETURN_ROW
153077**     }
153078**
153079** Also requiring special handling are the cases:
153080**
153081**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
153082**   ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
153083**
153084** when (expr1 < expr2). This is detected at runtime, not by this function.
153085** To handle this case, the pseudo-code programs depicted above are modified
153086** slightly to be:
153087**
153088**     ... loop started by sqlite3WhereBegin() ...
153089**     if( new partition ){
153090**       Gosub flush
153091**     }
153092**     Insert new row into eph table.
153093**     if( first row of partition ){
153094**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
153095**       regEnd = <expr2>
153096**       regStart = <expr1>
153097**       if( regEnd < regStart ){
153098**         RETURN_ROW
153099**         delete eph table contents
153100**         continue
153101**       }
153102**     ...
153103**
153104** The new "continue" statement in the above jumps to the next iteration
153105** of the outer loop - the one started by sqlite3WhereBegin().
153106**
153107** The various GROUPS cases are implemented using the same patterns as
153108** ROWS. The VM code is modified slightly so that:
153109**
153110**   1. The else branch in the main loop is only taken if the row just
153111**      added to the ephemeral table is the start of a new group. In
153112**      other words, it becomes:
153113**
153114**         ... loop started by sqlite3WhereBegin() ...
153115**         if( new partition ){
153116**           Gosub flush
153117**         }
153118**         Insert new row into eph table.
153119**         if( first row of partition ){
153120**           Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
153121**           regEnd = <expr2>
153122**           regStart = <expr1>
153123**         }else if( new group ){
153124**           ...
153125**         }
153126**       }
153127**
153128**   2. Instead of processing a single row, each RETURN_ROW, AGGSTEP or
153129**      AGGINVERSE step processes the current row of the relevant cursor and
153130**      all subsequent rows belonging to the same group.
153131**
153132** RANGE window frames are a little different again. As for GROUPS, the
153133** main loop runs once per group only. And RETURN_ROW, AGGSTEP and AGGINVERSE
153134** deal in groups instead of rows. As for ROWS and GROUPS, there are three
153135** basic cases:
153136**
153137**   RANGE BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
153138**
153139**     ... loop started by sqlite3WhereBegin() ...
153140**       if( new partition ){
153141**         Gosub flush
153142**       }
153143**       Insert new row into eph table.
153144**       if( first row of partition ){
153145**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
153146**         regEnd = <expr2>
153147**         regStart = <expr1>
153148**       }else{
153149**         AGGSTEP
153150**         while( (csrCurrent.key + regEnd) < csrEnd.key ){
153151**           RETURN_ROW
153152**           while( csrStart.key + regStart) < csrCurrent.key ){
153153**             AGGINVERSE
153154**           }
153155**         }
153156**       }
153157**     }
153158**     flush:
153159**       AGGSTEP
153160**       while( 1 ){
153161**         RETURN ROW
153162**         if( csrCurrent is EOF ) break;
153163**           while( csrStart.key + regStart) < csrCurrent.key ){
153164**             AGGINVERSE
153165**           }
153166**         }
153167**       }
153168**
153169** In the above notation, "csr.key" means the current value of the ORDER BY
153170** expression (there is only ever 1 for a RANGE that uses an <expr> FOLLOWING
153171** or <expr PRECEDING) read from cursor csr.
153172**
153173**   RANGE BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
153174**
153175**     ... loop started by sqlite3WhereBegin() ...
153176**       if( new partition ){
153177**         Gosub flush
153178**       }
153179**       Insert new row into eph table.
153180**       if( first row of partition ){
153181**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
153182**         regEnd = <expr2>
153183**         regStart = <expr1>
153184**       }else{
153185**         while( (csrEnd.key + regEnd) <= csrCurrent.key ){
153186**           AGGSTEP
153187**         }
153188**         while( (csrStart.key + regStart) < csrCurrent.key ){
153189**           AGGINVERSE
153190**         }
153191**         RETURN_ROW
153192**       }
153193**     }
153194**     flush:
153195**       while( (csrEnd.key + regEnd) <= csrCurrent.key ){
153196**         AGGSTEP
153197**       }
153198**       while( (csrStart.key + regStart) < csrCurrent.key ){
153199**         AGGINVERSE
153200**       }
153201**       RETURN_ROW
153202**
153203**   RANGE BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
153204**
153205**     ... loop started by sqlite3WhereBegin() ...
153206**       if( new partition ){
153207**         Gosub flush
153208**       }
153209**       Insert new row into eph table.
153210**       if( first row of partition ){
153211**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
153212**         regEnd = <expr2>
153213**         regStart = <expr1>
153214**       }else{
153215**         AGGSTEP
153216**         while( (csrCurrent.key + regEnd) < csrEnd.key ){
153217**           while( (csrCurrent.key + regStart) > csrStart.key ){
153218**             AGGINVERSE
153219**           }
153220**           RETURN_ROW
153221**         }
153222**       }
153223**     }
153224**     flush:
153225**       AGGSTEP
153226**       while( 1 ){
153227**         while( (csrCurrent.key + regStart) > csrStart.key ){
153228**           AGGINVERSE
153229**           if( eof ) break "while( 1 )" loop.
153230**         }
153231**         RETURN_ROW
153232**       }
153233**       while( !eof csrCurrent ){
153234**         RETURN_ROW
153235**       }
153236**
153237** The text above leaves out many details. Refer to the code and comments
153238** below for a more complete picture.
153239*/
153240SQLITE_PRIVATE void sqlite3WindowCodeStep(
153241  Parse *pParse,                  /* Parse context */
153242  Select *p,                      /* Rewritten SELECT statement */
153243  WhereInfo *pWInfo,              /* Context returned by sqlite3WhereBegin() */
153244  int regGosub,                   /* Register for OP_Gosub */
153245  int addrGosub                   /* OP_Gosub here to return each row */
153246){
153247  Window *pMWin = p->pWin;
153248  ExprList *pOrderBy = pMWin->pOrderBy;
153249  Vdbe *v = sqlite3GetVdbe(pParse);
153250  int csrWrite;                   /* Cursor used to write to eph. table */
153251  int csrInput = p->pSrc->a[0].iCursor;     /* Cursor of sub-select */
153252  int nInput = p->pSrc->a[0].pTab->nCol;    /* Number of cols returned by sub */
153253  int iInput;                               /* To iterate through sub cols */
153254  int addrNe;                     /* Address of OP_Ne */
153255  int addrGosubFlush = 0;         /* Address of OP_Gosub to flush: */
153256  int addrInteger = 0;            /* Address of OP_Integer */
153257  int addrEmpty;                  /* Address of OP_Rewind in flush: */
153258  int regNew;                     /* Array of registers holding new input row */
153259  int regRecord;                  /* regNew array in record form */
153260  int regRowid;                   /* Rowid for regRecord in eph table */
153261  int regNewPeer = 0;             /* Peer values for new row (part of regNew) */
153262  int regPeer = 0;                /* Peer values for current row */
153263  int regFlushPart = 0;           /* Register for "Gosub flush_partition" */
153264  WindowCodeArg s;                /* Context object for sub-routines */
153265  int lblWhereEnd;                /* Label just before sqlite3WhereEnd() code */
153266  int regStart = 0;               /* Value of <expr> PRECEDING */
153267  int regEnd = 0;                 /* Value of <expr> FOLLOWING */
153268
153269  assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT
153270       || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED
153271  );
153272  assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT
153273       || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING
153274  );
153275  assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT
153276       || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES
153277       || pMWin->eExclude==TK_NO
153278  );
153279
153280  lblWhereEnd = sqlite3VdbeMakeLabel(pParse);
153281
153282  /* Fill in the context object */
153283  memset(&s, 0, sizeof(WindowCodeArg));
153284  s.pParse = pParse;
153285  s.pMWin = pMWin;
153286  s.pVdbe = v;
153287  s.regGosub = regGosub;
153288  s.addrGosub = addrGosub;
153289  s.current.csr = pMWin->iEphCsr;
153290  csrWrite = s.current.csr+1;
153291  s.start.csr = s.current.csr+2;
153292  s.end.csr = s.current.csr+3;
153293
153294  /* Figure out when rows may be deleted from the ephemeral table. There
153295  ** are four options - they may never be deleted (eDelete==0), they may
153296  ** be deleted as soon as they are no longer part of the window frame
153297  ** (eDelete==WINDOW_AGGINVERSE), they may be deleted as after the row
153298  ** has been returned to the caller (WINDOW_RETURN_ROW), or they may
153299  ** be deleted after they enter the frame (WINDOW_AGGSTEP). */
153300  switch( pMWin->eStart ){
153301    case TK_FOLLOWING:
153302      if( pMWin->eFrmType!=TK_RANGE
153303       && windowExprGtZero(pParse, pMWin->pStart)
153304      ){
153305        s.eDelete = WINDOW_RETURN_ROW;
153306      }
153307      break;
153308    case TK_UNBOUNDED:
153309      if( windowCacheFrame(pMWin)==0 ){
153310        if( pMWin->eEnd==TK_PRECEDING ){
153311          if( pMWin->eFrmType!=TK_RANGE
153312           && windowExprGtZero(pParse, pMWin->pEnd)
153313          ){
153314            s.eDelete = WINDOW_AGGSTEP;
153315          }
153316        }else{
153317          s.eDelete = WINDOW_RETURN_ROW;
153318        }
153319      }
153320      break;
153321    default:
153322      s.eDelete = WINDOW_AGGINVERSE;
153323      break;
153324  }
153325
153326  /* Allocate registers for the array of values from the sub-query, the
153327  ** samve values in record form, and the rowid used to insert said record
153328  ** into the ephemeral table.  */
153329  regNew = pParse->nMem+1;
153330  pParse->nMem += nInput;
153331  regRecord = ++pParse->nMem;
153332  regRowid = ++pParse->nMem;
153333
153334  /* If the window frame contains an "<expr> PRECEDING" or "<expr> FOLLOWING"
153335  ** clause, allocate registers to store the results of evaluating each
153336  ** <expr>.  */
153337  if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){
153338    regStart = ++pParse->nMem;
153339  }
153340  if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){
153341    regEnd = ++pParse->nMem;
153342  }
153343
153344  /* If this is not a "ROWS BETWEEN ..." frame, then allocate arrays of
153345  ** registers to store copies of the ORDER BY expressions (peer values)
153346  ** for the main loop, and for each cursor (start, current and end). */
153347  if( pMWin->eFrmType!=TK_ROWS ){
153348    int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
153349    regNewPeer = regNew + pMWin->nBufferCol;
153350    if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
153351    regPeer = pParse->nMem+1;       pParse->nMem += nPeer;
153352    s.start.reg = pParse->nMem+1;   pParse->nMem += nPeer;
153353    s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
153354    s.end.reg = pParse->nMem+1;     pParse->nMem += nPeer;
153355  }
153356
153357  /* Load the column values for the row returned by the sub-select
153358  ** into an array of registers starting at regNew. Assemble them into
153359  ** a record in register regRecord. */
153360  for(iInput=0; iInput<nInput; iInput++){
153361    sqlite3VdbeAddOp3(v, OP_Column, csrInput, iInput, regNew+iInput);
153362  }
153363  sqlite3VdbeAddOp3(v, OP_MakeRecord, regNew, nInput, regRecord);
153364
153365  /* An input row has just been read into an array of registers starting
153366  ** at regNew. If the window has a PARTITION clause, this block generates
153367  ** VM code to check if the input row is the start of a new partition.
153368  ** If so, it does an OP_Gosub to an address to be filled in later. The
153369  ** address of the OP_Gosub is stored in local variable addrGosubFlush. */
153370  if( pMWin->pPartition ){
153371    int addr;
153372    ExprList *pPart = pMWin->pPartition;
153373    int nPart = pPart->nExpr;
153374    int regNewPart = regNew + pMWin->nBufferCol;
153375    KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
153376
153377    regFlushPart = ++pParse->nMem;
153378    addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart, nPart);
153379    sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
153380    sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2);
153381    VdbeCoverageEqNe(v);
153382    addrGosubFlush = sqlite3VdbeAddOp1(v, OP_Gosub, regFlushPart);
153383    VdbeComment((v, "call flush_partition"));
153384    sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
153385  }
153386
153387  /* Insert the new row into the ephemeral table */
153388  sqlite3VdbeAddOp2(v, OP_NewRowid, csrWrite, regRowid);
153389  sqlite3VdbeAddOp3(v, OP_Insert, csrWrite, regRecord, regRowid);
153390  addrNe = sqlite3VdbeAddOp3(v, OP_Ne, pMWin->regOne, 0, regRowid);
153391  VdbeCoverageNeverNull(v);
153392
153393  /* This block is run for the first row of each partition */
153394  s.regArg = windowInitAccum(pParse, pMWin);
153395
153396  if( regStart ){
153397    sqlite3ExprCode(pParse, pMWin->pStart, regStart);
153398    windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE?3:0));
153399  }
153400  if( regEnd ){
153401    sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
153402    windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE?3:0));
153403  }
153404
153405  if( pMWin->eFrmType!=TK_RANGE && pMWin->eStart==pMWin->eEnd && regStart ){
153406    int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
153407    int addrGe = sqlite3VdbeAddOp3(v, op, regStart, 0, regEnd);
153408    VdbeCoverageNeverNullIf(v, op==OP_Ge); /* NeverNull because bound <expr> */
153409    VdbeCoverageNeverNullIf(v, op==OP_Le); /*   values previously checked */
153410    windowAggFinal(&s, 0);
153411    sqlite3VdbeAddOp2(v, OP_Rewind, s.current.csr, 1);
153412    VdbeCoverageNeverTaken(v);
153413    windowReturnOneRow(&s);
153414    sqlite3VdbeAddOp1(v, OP_ResetSorter, s.current.csr);
153415    sqlite3VdbeAddOp2(v, OP_Goto, 0, lblWhereEnd);
153416    sqlite3VdbeJumpHere(v, addrGe);
153417  }
153418  if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){
153419    assert( pMWin->eEnd==TK_FOLLOWING );
153420    sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regStart);
153421  }
153422
153423  if( pMWin->eStart!=TK_UNBOUNDED ){
153424    sqlite3VdbeAddOp2(v, OP_Rewind, s.start.csr, 1);
153425    VdbeCoverageNeverTaken(v);
153426  }
153427  sqlite3VdbeAddOp2(v, OP_Rewind, s.current.csr, 1);
153428  VdbeCoverageNeverTaken(v);
153429  sqlite3VdbeAddOp2(v, OP_Rewind, s.end.csr, 1);
153430  VdbeCoverageNeverTaken(v);
153431  if( regPeer && pOrderBy ){
153432    sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, pOrderBy->nExpr-1);
153433    sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.start.reg, pOrderBy->nExpr-1);
153434    sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.current.reg, pOrderBy->nExpr-1);
153435    sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.end.reg, pOrderBy->nExpr-1);
153436  }
153437
153438  sqlite3VdbeAddOp2(v, OP_Goto, 0, lblWhereEnd);
153439
153440  sqlite3VdbeJumpHere(v, addrNe);
153441
153442  /* Beginning of the block executed for the second and subsequent rows. */
153443  if( regPeer ){
153444    windowIfNewPeer(pParse, pOrderBy, regNewPeer, regPeer, lblWhereEnd);
153445  }
153446  if( pMWin->eStart==TK_FOLLOWING ){
153447    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
153448    if( pMWin->eEnd!=TK_UNBOUNDED ){
153449      if( pMWin->eFrmType==TK_RANGE ){
153450        int lbl = sqlite3VdbeMakeLabel(pParse);
153451        int addrNext = sqlite3VdbeCurrentAddr(v);
153452        windowCodeRangeTest(&s, OP_Ge, s.current.csr, regEnd, s.end.csr, lbl);
153453        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
153454        windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
153455        sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNext);
153456        sqlite3VdbeResolveLabel(v, lbl);
153457      }else{
153458        windowCodeOp(&s, WINDOW_RETURN_ROW, regEnd, 0);
153459        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
153460      }
153461    }
153462  }else
153463  if( pMWin->eEnd==TK_PRECEDING ){
153464    int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
153465    windowCodeOp(&s, WINDOW_AGGSTEP, regEnd, 0);
153466    if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
153467    windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
153468    if( !bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
153469  }else{
153470    int addr = 0;
153471    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
153472    if( pMWin->eEnd!=TK_UNBOUNDED ){
153473      if( pMWin->eFrmType==TK_RANGE ){
153474        int lbl = 0;
153475        addr = sqlite3VdbeCurrentAddr(v);
153476        if( regEnd ){
153477          lbl = sqlite3VdbeMakeLabel(pParse);
153478          windowCodeRangeTest(&s, OP_Ge, s.current.csr, regEnd, s.end.csr, lbl);
153479        }
153480        windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
153481        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
153482        if( regEnd ){
153483          sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
153484          sqlite3VdbeResolveLabel(v, lbl);
153485        }
153486      }else{
153487        if( regEnd ){
153488          addr = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0, 1);
153489          VdbeCoverage(v);
153490        }
153491        windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
153492        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
153493        if( regEnd ) sqlite3VdbeJumpHere(v, addr);
153494      }
153495    }
153496  }
153497
153498  /* End of the main input loop */
153499  sqlite3VdbeResolveLabel(v, lblWhereEnd);
153500  sqlite3WhereEnd(pWInfo);
153501
153502  /* Fall through */
153503  if( pMWin->pPartition ){
153504    addrInteger = sqlite3VdbeAddOp2(v, OP_Integer, 0, regFlushPart);
153505    sqlite3VdbeJumpHere(v, addrGosubFlush);
153506  }
153507
153508  addrEmpty = sqlite3VdbeAddOp1(v, OP_Rewind, csrWrite);
153509  VdbeCoverage(v);
153510  if( pMWin->eEnd==TK_PRECEDING ){
153511    int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
153512    windowCodeOp(&s, WINDOW_AGGSTEP, regEnd, 0);
153513    if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
153514    windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
153515  }else if( pMWin->eStart==TK_FOLLOWING ){
153516    int addrStart;
153517    int addrBreak1;
153518    int addrBreak2;
153519    int addrBreak3;
153520    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
153521    if( pMWin->eFrmType==TK_RANGE ){
153522      addrStart = sqlite3VdbeCurrentAddr(v);
153523      addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 1);
153524      addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
153525    }else
153526    if( pMWin->eEnd==TK_UNBOUNDED ){
153527      addrStart = sqlite3VdbeCurrentAddr(v);
153528      addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, regStart, 1);
153529      addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, 0, 1);
153530    }else{
153531      assert( pMWin->eEnd==TK_FOLLOWING );
153532      addrStart = sqlite3VdbeCurrentAddr(v);
153533      addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, regEnd, 1);
153534      addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 1);
153535    }
153536    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
153537    sqlite3VdbeJumpHere(v, addrBreak2);
153538    addrStart = sqlite3VdbeCurrentAddr(v);
153539    addrBreak3 = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
153540    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
153541    sqlite3VdbeJumpHere(v, addrBreak1);
153542    sqlite3VdbeJumpHere(v, addrBreak3);
153543  }else{
153544    int addrBreak;
153545    int addrStart;
153546    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
153547    addrStart = sqlite3VdbeCurrentAddr(v);
153548    addrBreak = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
153549    windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
153550    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
153551    sqlite3VdbeJumpHere(v, addrBreak);
153552  }
153553  sqlite3VdbeJumpHere(v, addrEmpty);
153554
153555  sqlite3VdbeAddOp1(v, OP_ResetSorter, s.current.csr);
153556  if( pMWin->pPartition ){
153557    if( pMWin->regStartRowid ){
153558      sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
153559      sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
153560    }
153561    sqlite3VdbeChangeP1(v, addrInteger, sqlite3VdbeCurrentAddr(v));
153562    sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
153563  }
153564}
153565
153566#endif /* SQLITE_OMIT_WINDOWFUNC */
153567
153568/************** End of window.c **********************************************/
153569/************** Begin file parse.c *******************************************/
153570/*
153571** 2000-05-29
153572**
153573** The author disclaims copyright to this source code.  In place of
153574** a legal notice, here is a blessing:
153575**
153576**    May you do good and not evil.
153577**    May you find forgiveness for yourself and forgive others.
153578**    May you share freely, never taking more than you give.
153579**
153580*************************************************************************
153581** Driver template for the LEMON parser generator.
153582**
153583** The "lemon" program processes an LALR(1) input grammar file, then uses
153584** this template to construct a parser.  The "lemon" program inserts text
153585** at each "%%" line.  Also, any "P-a-r-s-e" identifer prefix (without the
153586** interstitial "-" characters) contained in this template is changed into
153587** the value of the %name directive from the grammar.  Otherwise, the content
153588** of this template is copied straight through into the generate parser
153589** source file.
153590**
153591** The following is the concatenation of all %include directives from the
153592** input grammar file:
153593*/
153594/* #include <stdio.h> */
153595/* #include <assert.h> */
153596/************ Begin %include sections from the grammar ************************/
153597
153598/* #include "sqliteInt.h" */
153599
153600/*
153601** Disable all error recovery processing in the parser push-down
153602** automaton.
153603*/
153604#define YYNOERRORRECOVERY 1
153605
153606/*
153607** Make yytestcase() the same as testcase()
153608*/
153609#define yytestcase(X) testcase(X)
153610
153611/*
153612** Indicate that sqlite3ParserFree() will never be called with a null
153613** pointer.
153614*/
153615#define YYPARSEFREENEVERNULL 1
153616
153617/*
153618** In the amalgamation, the parse.c file generated by lemon and the
153619** tokenize.c file are concatenated.  In that case, sqlite3RunParser()
153620** has access to the the size of the yyParser object and so the parser
153621** engine can be allocated from stack.  In that case, only the
153622** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
153623** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
153624** omitted.
153625*/
153626#ifdef SQLITE_AMALGAMATION
153627# define sqlite3Parser_ENGINEALWAYSONSTACK 1
153628#endif
153629
153630/*
153631** Alternative datatype for the argument to the malloc() routine passed
153632** into sqlite3ParserAlloc().  The default is size_t.
153633*/
153634#define YYMALLOCARGTYPE  u64
153635
153636/*
153637** An instance of the following structure describes the event of a
153638** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
153639** TK_DELETE, or TK_INSTEAD.  If the event is of the form
153640**
153641**      UPDATE ON (a,b,c)
153642**
153643** Then the "b" IdList records the list "a,b,c".
153644*/
153645struct TrigEvent { int a; IdList * b; };
153646
153647struct FrameBound     { int eType; Expr *pExpr; };
153648
153649/*
153650** Disable lookaside memory allocation for objects that might be
153651** shared across database connections.
153652*/
153653static void disableLookaside(Parse *pParse){
153654  sqlite3 *db = pParse->db;
153655  pParse->disableLookaside++;
153656  DisableLookaside;
153657}
153658
153659
153660  /*
153661  ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
153662  ** all elements in the list.  And make sure list length does not exceed
153663  ** SQLITE_LIMIT_COMPOUND_SELECT.
153664  */
153665  static void parserDoubleLinkSelect(Parse *pParse, Select *p){
153666    assert( p!=0 );
153667    if( p->pPrior ){
153668      Select *pNext = 0, *pLoop;
153669      int mxSelect, cnt = 0;
153670      for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
153671        pLoop->pNext = pNext;
153672        pLoop->selFlags |= SF_Compound;
153673      }
153674      if( (p->selFlags & SF_MultiValue)==0 &&
153675        (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
153676        cnt>mxSelect
153677      ){
153678        sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
153679      }
153680    }
153681  }
153682
153683
153684  /* Construct a new Expr object from a single identifier.  Use the
153685  ** new Expr to populate pOut.  Set the span of pOut to be the identifier
153686  ** that created the expression.
153687  */
153688  static Expr *tokenExpr(Parse *pParse, int op, Token t){
153689    Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
153690    if( p ){
153691      /* memset(p, 0, sizeof(Expr)); */
153692      p->op = (u8)op;
153693      p->affExpr = 0;
153694      p->flags = EP_Leaf;
153695      ExprClearVVAProperties(p);
153696      p->iAgg = -1;
153697      p->pLeft = p->pRight = 0;
153698      p->x.pList = 0;
153699      p->pAggInfo = 0;
153700      p->y.pTab = 0;
153701      p->op2 = 0;
153702      p->iTable = 0;
153703      p->iColumn = 0;
153704      p->u.zToken = (char*)&p[1];
153705      memcpy(p->u.zToken, t.z, t.n);
153706      p->u.zToken[t.n] = 0;
153707      if( sqlite3Isquote(p->u.zToken[0]) ){
153708        sqlite3DequoteExpr(p);
153709      }
153710#if SQLITE_MAX_EXPR_DEPTH>0
153711      p->nHeight = 1;
153712#endif
153713      if( IN_RENAME_OBJECT ){
153714        return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
153715      }
153716    }
153717    return p;
153718  }
153719
153720
153721  /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
153722  ** unary TK_ISNULL or TK_NOTNULL expression. */
153723  static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
153724    sqlite3 *db = pParse->db;
153725    if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
153726      pA->op = (u8)op;
153727      sqlite3ExprDelete(db, pA->pRight);
153728      pA->pRight = 0;
153729    }
153730  }
153731
153732  /* Add a single new term to an ExprList that is used to store a
153733  ** list of identifiers.  Report an error if the ID list contains
153734  ** a COLLATE clause or an ASC or DESC keyword, except ignore the
153735  ** error while parsing a legacy schema.
153736  */
153737  static ExprList *parserAddExprIdListTerm(
153738    Parse *pParse,
153739    ExprList *pPrior,
153740    Token *pIdToken,
153741    int hasCollate,
153742    int sortOrder
153743  ){
153744    ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
153745    if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
153746        && pParse->db->init.busy==0
153747    ){
153748      sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
153749                         pIdToken->n, pIdToken->z);
153750    }
153751    sqlite3ExprListSetName(pParse, p, pIdToken, 1);
153752    return p;
153753  }
153754
153755#if TK_SPAN>255
153756# error too many tokens in the grammar
153757#endif
153758/**************** End of %include directives **********************************/
153759/* These constants specify the various numeric values for terminal symbols
153760** in a format understandable to "makeheaders".  This section is blank unless
153761** "lemon" is run with the "-m" command-line option.
153762***************** Begin makeheaders token definitions *************************/
153763/**************** End makeheaders token definitions ***************************/
153764
153765/* The next sections is a series of control #defines.
153766** various aspects of the generated parser.
153767**    YYCODETYPE         is the data type used to store the integer codes
153768**                       that represent terminal and non-terminal symbols.
153769**                       "unsigned char" is used if there are fewer than
153770**                       256 symbols.  Larger types otherwise.
153771**    YYNOCODE           is a number of type YYCODETYPE that is not used for
153772**                       any terminal or nonterminal symbol.
153773**    YYFALLBACK         If defined, this indicates that one or more tokens
153774**                       (also known as: "terminal symbols") have fall-back
153775**                       values which should be used if the original symbol
153776**                       would not parse.  This permits keywords to sometimes
153777**                       be used as identifiers, for example.
153778**    YYACTIONTYPE       is the data type used for "action codes" - numbers
153779**                       that indicate what to do in response to the next
153780**                       token.
153781**    sqlite3ParserTOKENTYPE     is the data type used for minor type for terminal
153782**                       symbols.  Background: A "minor type" is a semantic
153783**                       value associated with a terminal or non-terminal
153784**                       symbols.  For example, for an "ID" terminal symbol,
153785**                       the minor type might be the name of the identifier.
153786**                       Each non-terminal can have a different minor type.
153787**                       Terminal symbols all have the same minor type, though.
153788**                       This macros defines the minor type for terminal
153789**                       symbols.
153790**    YYMINORTYPE        is the data type used for all minor types.
153791**                       This is typically a union of many types, one of
153792**                       which is sqlite3ParserTOKENTYPE.  The entry in the union
153793**                       for terminal symbols is called "yy0".
153794**    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
153795**                       zero the stack is dynamically sized using realloc()
153796**    sqlite3ParserARG_SDECL     A static variable declaration for the %extra_argument
153797**    sqlite3ParserARG_PDECL     A parameter declaration for the %extra_argument
153798**    sqlite3ParserARG_PARAM     Code to pass %extra_argument as a subroutine parameter
153799**    sqlite3ParserARG_STORE     Code to store %extra_argument into yypParser
153800**    sqlite3ParserARG_FETCH     Code to extract %extra_argument from yypParser
153801**    sqlite3ParserCTX_*         As sqlite3ParserARG_ except for %extra_context
153802**    YYERRORSYMBOL      is the code number of the error symbol.  If not
153803**                       defined, then do no error processing.
153804**    YYNSTATE           the combined number of states.
153805**    YYNRULE            the number of rules in the grammar
153806**    YYNTOKEN           Number of terminal symbols
153807**    YY_MAX_SHIFT       Maximum value for shift actions
153808**    YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
153809**    YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
153810**    YY_ERROR_ACTION    The yy_action[] code for syntax error
153811**    YY_ACCEPT_ACTION   The yy_action[] code for accept
153812**    YY_NO_ACTION       The yy_action[] code for no-op
153813**    YY_MIN_REDUCE      Minimum value for reduce actions
153814**    YY_MAX_REDUCE      Maximum value for reduce actions
153815*/
153816#ifndef INTERFACE
153817# define INTERFACE 1
153818#endif
153819/************* Begin control #defines *****************************************/
153820#define YYCODETYPE unsigned short int
153821#define YYNOCODE 310
153822#define YYACTIONTYPE unsigned short int
153823#define YYWILDCARD 100
153824#define sqlite3ParserTOKENTYPE Token
153825typedef union {
153826  int yyinit;
153827  sqlite3ParserTOKENTYPE yy0;
153828  SrcList* yy47;
153829  u8 yy58;
153830  struct FrameBound yy77;
153831  With* yy131;
153832  int yy192;
153833  Expr* yy202;
153834  struct {int value; int mask;} yy207;
153835  struct TrigEvent yy230;
153836  ExprList* yy242;
153837  Window* yy303;
153838  Upsert* yy318;
153839  const char* yy436;
153840  TriggerStep* yy447;
153841  Select* yy539;
153842  IdList* yy600;
153843} YYMINORTYPE;
153844#ifndef YYSTACKDEPTH
153845#define YYSTACKDEPTH 100
153846#endif
153847#define sqlite3ParserARG_SDECL
153848#define sqlite3ParserARG_PDECL
153849#define sqlite3ParserARG_PARAM
153850#define sqlite3ParserARG_FETCH
153851#define sqlite3ParserARG_STORE
153852#define sqlite3ParserCTX_SDECL Parse *pParse;
153853#define sqlite3ParserCTX_PDECL ,Parse *pParse
153854#define sqlite3ParserCTX_PARAM ,pParse
153855#define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
153856#define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
153857#define YYFALLBACK 1
153858#define YYNSTATE             551
153859#define YYNRULE              385
153860#define YYNRULE_WITH_ACTION  325
153861#define YYNTOKEN             181
153862#define YY_MAX_SHIFT         550
153863#define YY_MIN_SHIFTREDUCE   801
153864#define YY_MAX_SHIFTREDUCE   1185
153865#define YY_ERROR_ACTION      1186
153866#define YY_ACCEPT_ACTION     1187
153867#define YY_NO_ACTION         1188
153868#define YY_MIN_REDUCE        1189
153869#define YY_MAX_REDUCE        1573
153870/************* End control #defines *******************************************/
153871#define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
153872
153873/* Define the yytestcase() macro to be a no-op if is not already defined
153874** otherwise.
153875**
153876** Applications can choose to define yytestcase() in the %include section
153877** to a macro that can assist in verifying code coverage.  For production
153878** code the yytestcase() macro should be turned off.  But it is useful
153879** for testing.
153880*/
153881#ifndef yytestcase
153882# define yytestcase(X)
153883#endif
153884
153885
153886/* Next are the tables used to determine what action to take based on the
153887** current state and lookahead token.  These tables are used to implement
153888** functions that take a state number and lookahead value and return an
153889** action integer.
153890**
153891** Suppose the action integer is N.  Then the action is determined as
153892** follows
153893**
153894**   0 <= N <= YY_MAX_SHIFT             Shift N.  That is, push the lookahead
153895**                                      token onto the stack and goto state N.
153896**
153897**   N between YY_MIN_SHIFTREDUCE       Shift to an arbitrary state then
153898**     and YY_MAX_SHIFTREDUCE           reduce by rule N-YY_MIN_SHIFTREDUCE.
153899**
153900**   N == YY_ERROR_ACTION               A syntax error has occurred.
153901**
153902**   N == YY_ACCEPT_ACTION              The parser accepts its input.
153903**
153904**   N == YY_NO_ACTION                  No such action.  Denotes unused
153905**                                      slots in the yy_action[] table.
153906**
153907**   N between YY_MIN_REDUCE            Reduce by rule N-YY_MIN_REDUCE
153908**     and YY_MAX_REDUCE
153909**
153910** The action table is constructed as a single large table named yy_action[].
153911** Given state S and lookahead X, the action is computed as either:
153912**
153913**    (A)   N = yy_action[ yy_shift_ofst[S] + X ]
153914**    (B)   N = yy_default[S]
153915**
153916** The (A) formula is preferred.  The B formula is used instead if
153917** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X.
153918**
153919** The formulas above are for computing the action when the lookahead is
153920** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
153921** a reduce action) then the yy_reduce_ofst[] array is used in place of
153922** the yy_shift_ofst[] array.
153923**
153924** The following are the tables generated in this section:
153925**
153926**  yy_action[]        A single table containing all actions.
153927**  yy_lookahead[]     A table containing the lookahead for each entry in
153928**                     yy_action.  Used to detect hash collisions.
153929**  yy_shift_ofst[]    For each state, the offset into yy_action for
153930**                     shifting terminals.
153931**  yy_reduce_ofst[]   For each state, the offset into yy_action for
153932**                     shifting non-terminals after a reduce.
153933**  yy_default[]       Default action for each state.
153934**
153935*********** Begin parsing tables **********************************************/
153936#define YY_ACTTAB_COUNT (1958)
153937static const YYACTIONTYPE yy_action[] = {
153938 /*     0 */   544, 1220,  544,  449, 1258,  544, 1237,  544,  114,  111,
153939 /*    10 */   211,  544, 1535,  544, 1258,  521,  114,  111,  211,  390,
153940 /*    20 */  1230,  342,   42,   42,   42,   42, 1223,   42,   42,   71,
153941 /*    30 */    71,  935, 1222,   71,   71,   71,   71, 1460, 1491,  936,
153942 /*    40 */   818,  451,    6,  121,  122,  112, 1163, 1163, 1004, 1007,
153943 /*    50 */   997,  997,  119,  119,  120,  120,  120,  120, 1541,  390,
153944 /*    60 */  1356, 1515,  550,    2, 1191,  194,  526,  434,  143,  291,
153945 /*    70 */   526,  136,  526,  369,  261,  502,  272,  383, 1271,  525,
153946 /*    80 */   501,  491,  164,  121,  122,  112, 1163, 1163, 1004, 1007,
153947 /*    90 */   997,  997,  119,  119,  120,  120,  120,  120, 1356,  440,
153948 /*   100 */  1512,  118,  118,  118,  118,  117,  117,  116,  116,  116,
153949 /*   110 */   115,  422,  266,  266,  266,  266, 1496,  356, 1498,  433,
153950 /*   120 */   355, 1496,  515,  522, 1483,  541, 1112,  541, 1112,  390,
153951 /*   130 */   403,  241,  208,  114,  111,  211,   98,  290,  535,  221,
153952 /*   140 */  1027,  118,  118,  118,  118,  117,  117,  116,  116,  116,
153953 /*   150 */   115,  422, 1140,  121,  122,  112, 1163, 1163, 1004, 1007,
153954 /*   160 */   997,  997,  119,  119,  120,  120,  120,  120,  404,  426,
153955 /*   170 */   117,  117,  116,  116,  116,  115,  422, 1416,  466,  123,
153956 /*   180 */   118,  118,  118,  118,  117,  117,  116,  116,  116,  115,
153957 /*   190 */   422,  116,  116,  116,  115,  422,  538,  538,  538,  390,
153958 /*   200 */   503,  120,  120,  120,  120,  113, 1049, 1140, 1141, 1142,
153959 /*   210 */  1049,  118,  118,  118,  118,  117,  117,  116,  116,  116,
153960 /*   220 */   115,  422, 1459,  121,  122,  112, 1163, 1163, 1004, 1007,
153961 /*   230 */   997,  997,  119,  119,  120,  120,  120,  120,  390,  442,
153962 /*   240 */   314,   83,  461,   81,  357,  380, 1140,   80,  118,  118,
153963 /*   250 */   118,  118,  117,  117,  116,  116,  116,  115,  422,  179,
153964 /*   260 */   432,  422,  121,  122,  112, 1163, 1163, 1004, 1007,  997,
153965 /*   270 */   997,  119,  119,  120,  120,  120,  120,  432,  431,  266,
153966 /*   280 */   266,  118,  118,  118,  118,  117,  117,  116,  116,  116,
153967 /*   290 */   115,  422,  541, 1107,  901,  504, 1140,  114,  111,  211,
153968 /*   300 */  1429, 1140, 1141, 1142,  206,  489, 1107,  390,  447, 1107,
153969 /*   310 */   543,  328,  120,  120,  120,  120,  298, 1429, 1431,   17,
153970 /*   320 */   118,  118,  118,  118,  117,  117,  116,  116,  116,  115,
153971 /*   330 */   422,  121,  122,  112, 1163, 1163, 1004, 1007,  997,  997,
153972 /*   340 */   119,  119,  120,  120,  120,  120,  390, 1356,  432, 1140,
153973 /*   350 */   480, 1140, 1141, 1142,  994,  994, 1005, 1008,  443,  118,
153974 /*   360 */   118,  118,  118,  117,  117,  116,  116,  116,  115,  422,
153975 /*   370 */   121,  122,  112, 1163, 1163, 1004, 1007,  997,  997,  119,
153976 /*   380 */   119,  120,  120,  120,  120, 1052, 1052,  463, 1429,  118,
153977 /*   390 */   118,  118,  118,  117,  117,  116,  116,  116,  115,  422,
153978 /*   400 */  1140,  449,  544, 1424, 1140, 1141, 1142,  233,  964, 1140,
153979 /*   410 */   479,  476,  475,  171,  358,  390,  164,  405,  412,  840,
153980 /*   420 */   474,  164,  185,  332,   71,   71, 1241,  998,  118,  118,
153981 /*   430 */   118,  118,  117,  117,  116,  116,  116,  115,  422,  121,
153982 /*   440 */   122,  112, 1163, 1163, 1004, 1007,  997,  997,  119,  119,
153983 /*   450 */   120,  120,  120,  120,  390, 1140, 1141, 1142,  833,   12,
153984 /*   460 */   313,  507,  163,  354, 1140, 1141, 1142,  114,  111,  211,
153985 /*   470 */   506,  290,  535,  544,  276,  180,  290,  535,  121,  122,
153986 /*   480 */   112, 1163, 1163, 1004, 1007,  997,  997,  119,  119,  120,
153987 /*   490 */   120,  120,  120,  343,  482,   71,   71,  118,  118,  118,
153988 /*   500 */   118,  117,  117,  116,  116,  116,  115,  422, 1140,  209,
153989 /*   510 */   409,  521, 1140, 1107, 1569,  376,  252,  269,  340,  485,
153990 /*   520 */   335,  484,  238,  390,  511,  362, 1107, 1125,  331, 1107,
153991 /*   530 */   191,  407,  286,   32,  455,  441,  118,  118,  118,  118,
153992 /*   540 */   117,  117,  116,  116,  116,  115,  422,  121,  122,  112,
153993 /*   550 */  1163, 1163, 1004, 1007,  997,  997,  119,  119,  120,  120,
153994 /*   560 */   120,  120,  390, 1140, 1141, 1142,  985, 1140, 1141, 1142,
153995 /*   570 */  1140,  233,  490, 1490,  479,  476,  475,    6,  163,  544,
153996 /*   580 */   510,  544,  115,  422,  474,    5,  121,  122,  112, 1163,
153997 /*   590 */  1163, 1004, 1007,  997,  997,  119,  119,  120,  120,  120,
153998 /*   600 */   120,   13,   13,   13,   13,  118,  118,  118,  118,  117,
153999 /*   610 */   117,  116,  116,  116,  115,  422,  401,  500,  406,  544,
154000 /*   620 */  1484,  542, 1140,  890,  890, 1140, 1141, 1142, 1471, 1140,
154001 /*   630 */   275,  390,  806,  807,  808,  969,  420,  420,  420,   16,
154002 /*   640 */    16,   55,   55, 1240,  118,  118,  118,  118,  117,  117,
154003 /*   650 */   116,  116,  116,  115,  422,  121,  122,  112, 1163, 1163,
154004 /*   660 */  1004, 1007,  997,  997,  119,  119,  120,  120,  120,  120,
154005 /*   670 */   390, 1187,    1,    1,  550,    2, 1191, 1140, 1141, 1142,
154006 /*   680 */   194,  291,  896,  136, 1140, 1141, 1142,  895,  519, 1490,
154007 /*   690 */  1271,    3,  378,    6,  121,  122,  112, 1163, 1163, 1004,
154008 /*   700 */  1007,  997,  997,  119,  119,  120,  120,  120,  120,  856,
154009 /*   710 */   544,  922,  544,  118,  118,  118,  118,  117,  117,  116,
154010 /*   720 */   116,  116,  115,  422,  266,  266, 1090, 1567, 1140,  549,
154011 /*   730 */  1567, 1191,   13,   13,   13,   13,  291,  541,  136,  390,
154012 /*   740 */   483,  419,  418,  964,  342, 1271,  466,  408,  857,  279,
154013 /*   750 */   140,  221,  118,  118,  118,  118,  117,  117,  116,  116,
154014 /*   760 */   116,  115,  422,  121,  122,  112, 1163, 1163, 1004, 1007,
154015 /*   770 */   997,  997,  119,  119,  120,  120,  120,  120,  544,  266,
154016 /*   780 */   266,  426,  390, 1140, 1141, 1142, 1170,  828, 1170,  466,
154017 /*   790 */   429,  145,  541, 1144,  399,  313,  437,  301,  836, 1488,
154018 /*   800 */    71,   71,  410,    6, 1088,  471,  221,  100,  112, 1163,
154019 /*   810 */  1163, 1004, 1007,  997,  997,  119,  119,  120,  120,  120,
154020 /*   820 */   120,  118,  118,  118,  118,  117,  117,  116,  116,  116,
154021 /*   830 */   115,  422,  237, 1423,  544,  449,  426,  287,  984,  544,
154022 /*   840 */   236,  235,  234,  828,   97,  527,  427, 1263, 1263, 1144,
154023 /*   850 */   492,  306,  428,  836,  975,  544,   71,   71,  974, 1239,
154024 /*   860 */   544,   51,   51,  300,  118,  118,  118,  118,  117,  117,
154025 /*   870 */   116,  116,  116,  115,  422,  194,  103,   70,   70,  266,
154026 /*   880 */   266,  544,   71,   71,  266,  266,   30,  389,  342,  974,
154027 /*   890 */   974,  976,  541,  526, 1107,  326,  390,  541,  493,  395,
154028 /*   900 */  1468,  195,  528,   13,   13, 1356,  240, 1107,  277,  280,
154029 /*   910 */  1107,  280,  303,  455,  305,  331,  390,   31,  188,  417,
154030 /*   920 */   121,  122,  112, 1163, 1163, 1004, 1007,  997,  997,  119,
154031 /*   930 */   119,  120,  120,  120,  120,  142,  390,  363,  455,  984,
154032 /*   940 */   121,  122,  112, 1163, 1163, 1004, 1007,  997,  997,  119,
154033 /*   950 */   119,  120,  120,  120,  120,  975,  321, 1140,  324,  974,
154034 /*   960 */   121,  110,  112, 1163, 1163, 1004, 1007,  997,  997,  119,
154035 /*   970 */   119,  120,  120,  120,  120,  462,  375, 1183,  118,  118,
154036 /*   980 */   118,  118,  117,  117,  116,  116,  116,  115,  422, 1140,
154037 /*   990 */   974,  974,  976,  304,    9,  364,  244,  360,  118,  118,
154038 /*  1000 */   118,  118,  117,  117,  116,  116,  116,  115,  422,  312,
154039 /*  1010 */   544,  342, 1140, 1141, 1142,  299,  290,  535,  118,  118,
154040 /*  1020 */   118,  118,  117,  117,  116,  116,  116,  115,  422, 1261,
154041 /*  1030 */  1261, 1161,   13,   13,  278,  419,  418,  466,  390,  921,
154042 /*  1040 */   260,  260,  289, 1167, 1140, 1141, 1142,  189, 1169,  266,
154043 /*  1050 */   266,  466,  388,  541, 1184,  544, 1168,  263,  144,  487,
154044 /*  1060 */   920,  544,  541,  122,  112, 1163, 1163, 1004, 1007,  997,
154045 /*  1070 */   997,  119,  119,  120,  120,  120,  120,   71,   71, 1140,
154046 /*  1080 */  1170, 1270, 1170,   13,   13,  896, 1068, 1161,  544,  466,
154047 /*  1090 */   895,  107,  536, 1489,    4, 1266, 1107,    6,  523, 1047,
154048 /*  1100 */    12, 1069, 1090, 1568,  311,  453, 1568,  518,  539, 1107,
154049 /*  1110 */    56,   56, 1107, 1487,  421, 1356, 1070,    6,  343,  285,
154050 /*  1120 */   118,  118,  118,  118,  117,  117,  116,  116,  116,  115,
154051 /*  1130 */   422,  423, 1269,  319, 1140, 1141, 1142,  876,  266,  266,
154052 /*  1140 */  1275,  107,  536,  533,    4, 1486,  293,  877, 1209,    6,
154053 /*  1150 */   210,  541,  541,  164, 1540,  494,  414,  865,  539,  267,
154054 /*  1160 */   267, 1212,  396,  509,  497,  204,  266,  266,  394,  529,
154055 /*  1170 */     8,  984,  541,  517,  544,  920,  456,  105,  105,  541,
154056 /*  1180 */  1088,  423,  266,  266,  106,  415,  423,  546,  545,  266,
154057 /*  1190 */   266,  974,  516,  533, 1371,  541,   15,   15,  266,  266,
154058 /*  1200 */   454, 1118,  541,  266,  266, 1068, 1370,  513,  290,  535,
154059 /*  1210 */   544,  541,  512,   97,  442,  314,  541,  544,  920,  125,
154060 /*  1220 */  1069,  984,  974,  974,  976,  977,   27,  105,  105,  399,
154061 /*  1230 */   341, 1509,   44,   44,  106, 1070,  423,  546,  545,   57,
154062 /*  1240 */    57,  974,  341, 1509,  107,  536,  544,    4,  460,  399,
154063 /*  1250 */   214, 1118,  457,  294,  375, 1089,  532,  297,  544,  537,
154064 /*  1260 */   396,  539,  290,  535,  104,  244,  102,  524,   58,   58,
154065 /*  1270 */   544,  109,  974,  974,  976,  977,   27, 1514, 1129,  425,
154066 /*  1280 */    59,   59,  270,  237,  423,  138,   95,  373,  373,  372,
154067 /*  1290 */   255,  370,   60,   60,  815, 1178,  533,  544,  273,  544,
154068 /*  1300 */  1161,  843,  387,  386,  544, 1307,  544,  215,  210,  296,
154069 /*  1310 */   513,  847,  544,  265,  208,  514, 1306,  295,  274,   61,
154070 /*  1320 */    61,   62,   62,  436,  984, 1160,   45,   45,   46,   46,
154071 /*  1330 */   105,  105, 1184,  920,   47,   47, 1474,  106,  544,  423,
154072 /*  1340 */   546,  545,  218,  544,  974,  935, 1085,  217,  544,  377,
154073 /*  1350 */   395,  107,  536,  936,    4,  156, 1161,  843,  158,  544,
154074 /*  1360 */    49,   49,  141,  544,   38,   50,   50,  544,  539,  307,
154075 /*  1370 */    63,   63,  544, 1448,  216,  974,  974,  976,  977,   27,
154076 /*  1380 */   444,   64,   64,  544, 1447,   65,   65,  544,  524,   14,
154077 /*  1390 */    14,  423,  458,  544,   66,   66,  310,  544,  316,   97,
154078 /*  1400 */  1034,  544,  961,  533,  268,  127,  127,  544,  391,   67,
154079 /*  1410 */    67,  544,  978,  290,  535,   52,   52,  513,  544,   68,
154080 /*  1420 */    68, 1294,  512,   69,   69,  397,  165,  855,  854,   53,
154081 /*  1430 */    53,  984,  966,  151,  151,  243,  430,  105,  105,  199,
154082 /*  1440 */   152,  152,  448, 1303,  106,  243,  423,  546,  545, 1129,
154083 /*  1450 */   425,  974,  320,  270,  862,  863, 1034,  220,  373,  373,
154084 /*  1460 */   372,  255,  370,  450,  323,  815,  243,  544,  978,  544,
154085 /*  1470 */   107,  536,  544,    4,  544,  938,  939,  325,  215, 1046,
154086 /*  1480 */   296, 1046,  974,  974,  976,  977,   27,  539,  295,   76,
154087 /*  1490 */    76,   54,   54,  327,   72,   72,  128,  128, 1503, 1254,
154088 /*  1500 */   107,  536,  544,    4, 1045,  544, 1045,  531, 1238,  544,
154089 /*  1510 */   423,  544,  315,  334,  544,   97,  544,  539,  217,  544,
154090 /*  1520 */   472, 1528,  533,  239,   73,   73,  156,  129,  129,  158,
154091 /*  1530 */   467,  130,  130,  126,  126,  344,  150,  150,  149,  149,
154092 /*  1540 */   423,  134,  134,  329, 1030,  216,   97,  239,  929,  345,
154093 /*  1550 */   984,  243,  533, 1315,  339,  544,  105,  105,  900, 1355,
154094 /*  1560 */   544, 1290,  258,  106,  338,  423,  546,  545,  544, 1301,
154095 /*  1570 */   974,  893,   99,  536,  109,    4,  544,  133,  133,  391,
154096 /*  1580 */   984,  197,  131,  131,  290,  535,  105,  105,  530,  539,
154097 /*  1590 */   132,  132, 1361,  106, 1219,  423,  546,  545,   75,   75,
154098 /*  1600 */   974,  974,  974,  976,  977,   27,  544,  430,  826, 1211,
154099 /*  1610 */   894,  139,  423,  109,  544, 1200, 1199, 1201, 1522,  544,
154100 /*  1620 */   201,  544,   11,  374,  533, 1287,  347,  349,   77,   77,
154101 /*  1630 */  1340,  974,  974,  976,  977,   27,   74,   74,  351,  213,
154102 /*  1640 */   435,   43,   43,   48,   48,  302,  477,  309, 1348,  382,
154103 /*  1650 */   353,  452,  984,  337, 1237, 1420, 1419,  205,  105,  105,
154104 /*  1660 */   192,  367,  193,  534, 1525,  106, 1178,  423,  546,  545,
154105 /*  1670 */   247,  167,  974,  270, 1467,  200, 1465, 1175,  373,  373,
154106 /*  1680 */   372,  255,  370,  398,   79,  815,   83,   82, 1425,  446,
154107 /*  1690 */   161,  177,  169,   95, 1337,  438,  172,  173,  215,  174,
154108 /*  1700 */   296,  175,   35,  974,  974,  976,  977,   27,  295, 1345,
154109 /*  1710 */   439,  470,  223,   36,  379,  445, 1414,  381,  459, 1351,
154110 /*  1720 */   181,  227,   88,  465,  259,  229, 1436,  318,  186,  468,
154111 /*  1730 */   322,  230,  384, 1202,  231,  486, 1257, 1256,  217,  411,
154112 /*  1740 */  1255, 1248,   90,  847,  206,  413,  156,  505, 1539,  158,
154113 /*  1750 */  1226, 1538,  283, 1508, 1227,  336,  385,  284, 1225,  496,
154114 /*  1760 */  1537, 1298,   94,  346,  348,  216, 1247,  499, 1299,  245,
154115 /*  1770 */   246, 1297,  416,  350, 1494,  124, 1493,   10,  524,  361,
154116 /*  1780 */  1400,  101,   96,  288,  508,  253, 1135, 1208,   34, 1296,
154117 /*  1790 */   547,  254,  256,  257,  392,  548, 1197, 1192,  359,  391,
154118 /*  1800 */  1280, 1279,  196,  365,  290,  535,  366,  352, 1452, 1322,
154119 /*  1810 */  1321, 1453,  153,  137,  281,  154,  802,  424,  155, 1451,
154120 /*  1820 */  1450,  198,  292,  202,  203,   78,  212,  430,  271,  135,
154121 /*  1830 */  1044, 1042,  958,  168,  219,  157,  170,  879,  308,  222,
154122 /*  1840 */  1058,  176,  159,  962,  400,   84,  402,  178,   85,   86,
154123 /*  1850 */    87,  166,  160,  393, 1061,  224,  225, 1057,  146,   18,
154124 /*  1860 */   226,  317, 1050, 1172,  243,  464,  182,  228,   37,  183,
154125 /*  1870 */   817,  469,  338,  232,  330,  481,  184,   89,  845,   19,
154126 /*  1880 */    20,   92,  473,  478,  333,   91,  162,  858,  147,  488,
154127 /*  1890 */   282, 1123,  148, 1010,  928, 1093,   39,   93,   40,  495,
154128 /*  1900 */  1094,  187,  498,  207,  262,  264,  923,  242, 1109,  109,
154129 /*  1910 */  1113, 1111, 1097,   33,   21, 1117,  520, 1025,   22,   23,
154130 /*  1920 */    24, 1116,   25,  190,   97, 1011, 1009,   26, 1013, 1067,
154131 /*  1930 */   248,    7, 1066,  249, 1014,   28,   41,  889,  979,  827,
154132 /*  1940 */   108,   29,  250,  540,  251, 1530,  371,  368, 1131, 1130,
154133 /*  1950 */  1188, 1188, 1188, 1188, 1188, 1188, 1188, 1529,
154134};
154135static const YYCODETYPE yy_lookahead[] = {
154136 /*     0 */   189,  211,  189,  189,  218,  189,  220,  189,  267,  268,
154137 /*    10 */   269,  189,  210,  189,  228,  189,  267,  268,  269,   19,
154138 /*    20 */   218,  189,  211,  212,  211,  212,  211,  211,  212,  211,
154139 /*    30 */   212,   31,  211,  211,  212,  211,  212,  288,  300,   39,
154140 /*    40 */    21,  189,  304,   43,   44,   45,   46,   47,   48,   49,
154141 /*    50 */    50,   51,   52,   53,   54,   55,   56,   57,  225,   19,
154142 /*    60 */   189,  183,  184,  185,  186,  189,  248,  263,  236,  191,
154143 /*    70 */   248,  193,  248,  197,  208,  257,  262,  201,  200,  257,
154144 /*    80 */   200,  257,   81,   43,   44,   45,   46,   47,   48,   49,
154145 /*    90 */    50,   51,   52,   53,   54,   55,   56,   57,  189,   80,
154146 /*   100 */   189,  101,  102,  103,  104,  105,  106,  107,  108,  109,
154147 /*   110 */   110,  111,  234,  235,  234,  235,  305,  306,  305,  118,
154148 /*   120 */   307,  305,  306,  297,  298,  247,   86,  247,   88,   19,
154149 /*   130 */   259,  251,  252,  267,  268,  269,   26,  136,  137,  261,
154150 /*   140 */   121,  101,  102,  103,  104,  105,  106,  107,  108,  109,
154151 /*   150 */   110,  111,   59,   43,   44,   45,   46,   47,   48,   49,
154152 /*   160 */    50,   51,   52,   53,   54,   55,   56,   57,  259,  291,
154153 /*   170 */   105,  106,  107,  108,  109,  110,  111,  158,  189,   69,
154154 /*   180 */   101,  102,  103,  104,  105,  106,  107,  108,  109,  110,
154155 /*   190 */   111,  107,  108,  109,  110,  111,  205,  206,  207,   19,
154156 /*   200 */    19,   54,   55,   56,   57,   58,   29,  114,  115,  116,
154157 /*   210 */    33,  101,  102,  103,  104,  105,  106,  107,  108,  109,
154158 /*   220 */   110,  111,  233,   43,   44,   45,   46,   47,   48,   49,
154159 /*   230 */    50,   51,   52,   53,   54,   55,   56,   57,   19,  126,
154160 /*   240 */   127,  148,   65,   24,  214,  200,   59,   67,  101,  102,
154161 /*   250 */   103,  104,  105,  106,  107,  108,  109,  110,  111,   22,
154162 /*   260 */   189,  111,   43,   44,   45,   46,   47,   48,   49,   50,
154163 /*   270 */    51,   52,   53,   54,   55,   56,   57,  206,  207,  234,
154164 /*   280 */   235,  101,  102,  103,  104,  105,  106,  107,  108,  109,
154165 /*   290 */   110,  111,  247,   76,  107,  114,   59,  267,  268,  269,
154166 /*   300 */   189,  114,  115,  116,  162,  163,   89,   19,  263,   92,
154167 /*   310 */   189,   23,   54,   55,   56,   57,  189,  206,  207,   22,
154168 /*   320 */   101,  102,  103,  104,  105,  106,  107,  108,  109,  110,
154169 /*   330 */   111,   43,   44,   45,   46,   47,   48,   49,   50,   51,
154170 /*   340 */    52,   53,   54,   55,   56,   57,   19,  189,  277,   59,
154171 /*   350 */    23,  114,  115,  116,   46,   47,   48,   49,   61,  101,
154172 /*   360 */   102,  103,  104,  105,  106,  107,  108,  109,  110,  111,
154173 /*   370 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
154174 /*   380 */    53,   54,   55,   56,   57,  125,  126,  127,  277,  101,
154175 /*   390 */   102,  103,  104,  105,  106,  107,  108,  109,  110,  111,
154176 /*   400 */    59,  189,  189,  276,  114,  115,  116,  117,   73,   59,
154177 /*   410 */   120,  121,  122,   72,  214,   19,   81,  259,   19,   23,
154178 /*   420 */   130,   81,   72,   24,  211,  212,  221,  119,  101,  102,
154179 /*   430 */   103,  104,  105,  106,  107,  108,  109,  110,  111,   43,
154180 /*   440 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
154181 /*   450 */    54,   55,   56,   57,   19,  114,  115,  116,   23,  208,
154182 /*   460 */   125,  248,  189,  189,  114,  115,  116,  267,  268,  269,
154183 /*   470 */   189,  136,  137,  189,  262,   22,  136,  137,   43,   44,
154184 /*   480 */    45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
154185 /*   490 */    55,   56,   57,  189,   95,  211,  212,  101,  102,  103,
154186 /*   500 */   104,  105,  106,  107,  108,  109,  110,  111,   59,  189,
154187 /*   510 */   111,  189,   59,   76,  294,  295,  117,  118,  119,  120,
154188 /*   520 */   121,  122,  123,   19,   87,  189,   89,   23,  129,   92,
154189 /*   530 */   279,  227,  248,   22,  189,  284,  101,  102,  103,  104,
154190 /*   540 */   105,  106,  107,  108,  109,  110,  111,   43,   44,   45,
154191 /*   550 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
154192 /*   560 */    56,   57,   19,  114,  115,  116,   23,  114,  115,  116,
154193 /*   570 */    59,  117,  299,  300,  120,  121,  122,  304,  189,  189,
154194 /*   580 */   143,  189,  110,  111,  130,   22,   43,   44,   45,   46,
154195 /*   590 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
154196 /*   600 */    57,  211,  212,  211,  212,  101,  102,  103,  104,  105,
154197 /*   610 */   106,  107,  108,  109,  110,  111,  226,  189,  226,  189,
154198 /*   620 */   298,  132,   59,  134,  135,  114,  115,  116,  189,   59,
154199 /*   630 */   285,   19,    7,    8,    9,   23,  205,  206,  207,  211,
154200 /*   640 */   212,  211,  212,  221,  101,  102,  103,  104,  105,  106,
154201 /*   650 */   107,  108,  109,  110,  111,   43,   44,   45,   46,   47,
154202 /*   660 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
154203 /*   670 */    19,  181,  182,  183,  184,  185,  186,  114,  115,  116,
154204 /*   680 */   189,  191,  133,  193,  114,  115,  116,  138,  299,  300,
154205 /*   690 */   200,   22,  201,  304,   43,   44,   45,   46,   47,   48,
154206 /*   700 */    49,   50,   51,   52,   53,   54,   55,   56,   57,   35,
154207 /*   710 */   189,  141,  189,  101,  102,  103,  104,  105,  106,  107,
154208 /*   720 */   108,  109,  110,  111,  234,  235,   22,   23,   59,  184,
154209 /*   730 */    26,  186,  211,  212,  211,  212,  191,  247,  193,   19,
154210 /*   740 */    66,  105,  106,   73,  189,  200,  189,  226,   74,  226,
154211 /*   750 */    22,  261,  101,  102,  103,  104,  105,  106,  107,  108,
154212 /*   760 */   109,  110,  111,   43,   44,   45,   46,   47,   48,   49,
154213 /*   770 */    50,   51,   52,   53,   54,   55,   56,   57,  189,  234,
154214 /*   780 */   235,  291,   19,  114,  115,  116,  150,   59,  152,  189,
154215 /*   790 */   233,  236,  247,   59,  189,  125,  126,  127,   59,  300,
154216 /*   800 */   211,  212,  128,  304,  100,   19,  261,  156,   45,   46,
154217 /*   810 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
154218 /*   820 */    57,  101,  102,  103,  104,  105,  106,  107,  108,  109,
154219 /*   830 */   110,  111,   46,  233,  189,  189,  291,  248,   99,  189,
154220 /*   840 */   125,  126,  127,  115,   26,  200,  289,  230,  231,  115,
154221 /*   850 */   200,   16,  189,  114,  115,  189,  211,  212,  119,  221,
154222 /*   860 */   189,  211,  212,  258,  101,  102,  103,  104,  105,  106,
154223 /*   870 */   107,  108,  109,  110,  111,  189,  156,  211,  212,  234,
154224 /*   880 */   235,  189,  211,  212,  234,  235,   22,  201,  189,  150,
154225 /*   890 */   151,  152,  247,  248,   76,   16,   19,  247,  248,  113,
154226 /*   900 */   189,   24,  257,  211,  212,  189,   26,   89,  262,  223,
154227 /*   910 */    92,  225,   77,  189,   79,  129,   19,   53,  226,  248,
154228 /*   920 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
154229 /*   930 */    53,   54,   55,   56,   57,  236,   19,  271,  189,   99,
154230 /*   940 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
154231 /*   950 */    53,   54,   55,   56,   57,  115,   77,   59,   79,  119,
154232 /*   960 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
154233 /*   970 */    53,   54,   55,   56,   57,  259,   22,   23,  101,  102,
154234 /*   980 */   103,  104,  105,  106,  107,  108,  109,  110,  111,   59,
154235 /*   990 */   150,  151,  152,  158,   22,  244,   24,  246,  101,  102,
154236 /*  1000 */   103,  104,  105,  106,  107,  108,  109,  110,  111,  285,
154237 /*  1010 */   189,  189,  114,  115,  116,  200,  136,  137,  101,  102,
154238 /*  1020 */   103,  104,  105,  106,  107,  108,  109,  110,  111,  230,
154239 /*  1030 */   231,   59,  211,  212,  285,  105,  106,  189,   19,  141,
154240 /*  1040 */   234,  235,  239,  113,  114,  115,  116,  226,  118,  234,
154241 /*  1050 */   235,  189,  249,  247,  100,  189,  126,   23,  236,  107,
154242 /*  1060 */    26,  189,  247,   44,   45,   46,   47,   48,   49,   50,
154243 /*  1070 */    51,   52,   53,   54,   55,   56,   57,  211,  212,   59,
154244 /*  1080 */   150,  233,  152,  211,  212,  133,   12,  115,  189,  189,
154245 /*  1090 */   138,   19,   20,  300,   22,  233,   76,  304,  226,   11,
154246 /*  1100 */   208,   27,   22,   23,  200,   19,   26,   87,   36,   89,
154247 /*  1110 */   211,  212,   92,  300,  248,  189,   42,  304,  189,  250,
154248 /*  1120 */   101,  102,  103,  104,  105,  106,  107,  108,  109,  110,
154249 /*  1130 */   111,   59,  200,  233,  114,  115,  116,   63,  234,  235,
154250 /*  1140 */   235,   19,   20,   71,   22,  300,  189,   73,  200,  304,
154251 /*  1150 */   116,  247,  247,   81,   23,  200,  227,   26,   36,  234,
154252 /*  1160 */   235,  203,  204,  143,  200,   26,  234,  235,  194,  200,
154253 /*  1170 */    48,   99,  247,   66,  189,  141,  284,  105,  106,  247,
154254 /*  1180 */   100,   59,  234,  235,  112,  259,  114,  115,  116,  234,
154255 /*  1190 */   235,  119,   85,   71,  266,  247,  211,  212,  234,  235,
154256 /*  1200 */   114,   94,  247,  234,  235,   12,  266,   85,  136,  137,
154257 /*  1210 */   189,  247,   90,   26,  126,  127,  247,  189,   26,   22,
154258 /*  1220 */    27,   99,  150,  151,  152,  153,  154,  105,  106,  189,
154259 /*  1230 */   302,  303,  211,  212,  112,   42,  114,  115,  116,  211,
154260 /*  1240 */   212,  119,  302,  303,   19,   20,  189,   22,  274,  189,
154261 /*  1250 */    15,  144,  278,  189,   22,   23,   63,  189,  189,  203,
154262 /*  1260 */   204,   36,  136,  137,  155,   24,  157,  143,  211,  212,
154263 /*  1270 */   189,   26,  150,  151,  152,  153,  154,    0,    1,    2,
154264 /*  1280 */   211,  212,    5,   46,   59,  161,  147,   10,   11,   12,
154265 /*  1290 */    13,   14,  211,  212,   17,   60,   71,  189,  258,  189,
154266 /*  1300 */    59,   59,  105,  106,  189,  189,  189,   30,  116,   32,
154267 /*  1310 */    85,  124,  189,  251,  252,   90,  189,   40,  258,  211,
154268 /*  1320 */   212,  211,  212,  189,   99,   26,  211,  212,  211,  212,
154269 /*  1330 */   105,  106,  100,  141,  211,  212,  189,  112,  189,  114,
154270 /*  1340 */   115,  116,   24,  189,  119,   31,   23,   70,  189,   26,
154271 /*  1350 */   113,   19,   20,   39,   22,   78,  115,  115,   81,  189,
154272 /*  1360 */   211,  212,   22,  189,   24,  211,  212,  189,   36,  189,
154273 /*  1370 */   211,  212,  189,  189,   97,  150,  151,  152,  153,  154,
154274 /*  1380 */   127,  211,  212,  189,  189,  211,  212,  189,  143,  211,
154275 /*  1390 */   212,   59,  189,  189,  211,  212,   23,  189,  189,   26,
154276 /*  1400 */    59,  189,  149,   71,   22,  211,  212,  189,  131,  211,
154277 /*  1410 */   212,  189,   59,  136,  137,  211,  212,   85,  189,  211,
154278 /*  1420 */   212,  253,   90,  211,  212,  292,  293,  118,  119,  211,
154279 /*  1430 */   212,   99,   23,  211,  212,   26,  159,  105,  106,  140,
154280 /*  1440 */   211,  212,   23,  189,  112,   26,  114,  115,  116,    1,
154281 /*  1450 */     2,  119,  189,    5,    7,    8,  115,  139,   10,   11,
154282 /*  1460 */    12,   13,   14,   23,  189,   17,   26,  189,  115,  189,
154283 /*  1470 */    19,   20,  189,   22,  189,   83,   84,  189,   30,  150,
154284 /*  1480 */    32,  152,  150,  151,  152,  153,  154,   36,   40,  211,
154285 /*  1490 */   212,  211,  212,  189,  211,  212,  211,  212,  309,  189,
154286 /*  1500 */    19,   20,  189,   22,  150,  189,  152,  231,  189,  189,
154287 /*  1510 */    59,  189,   23,  189,  189,   26,  189,   36,   70,  189,
154288 /*  1520 */    23,  139,   71,   26,  211,  212,   78,  211,  212,   81,
154289 /*  1530 */   281,  211,  212,  211,  212,  189,  211,  212,  211,  212,
154290 /*  1540 */    59,  211,  212,   23,   23,   97,   26,   26,   23,  189,
154291 /*  1550 */    99,   26,   71,  189,  119,  189,  105,  106,  107,  189,
154292 /*  1560 */   189,  189,  280,  112,  129,  114,  115,  116,  189,  189,
154293 /*  1570 */   119,   23,   19,   20,   26,   22,  189,  211,  212,  131,
154294 /*  1580 */    99,  237,  211,  212,  136,  137,  105,  106,  189,   36,
154295 /*  1590 */   211,  212,  189,  112,  189,  114,  115,  116,  211,  212,
154296 /*  1600 */   119,  150,  151,  152,  153,  154,  189,  159,   23,  189,
154297 /*  1610 */    23,   26,   59,   26,  189,  189,  189,  189,  189,  189,
154298 /*  1620 */   209,  189,  238,  187,   71,  250,  250,  250,  211,  212,
154299 /*  1630 */   241,  150,  151,  152,  153,  154,  211,  212,  250,  290,
154300 /*  1640 */   254,  211,  212,  211,  212,  254,  215,  286,  241,  241,
154301 /*  1650 */   254,  286,   99,  214,  220,  214,  214,  224,  105,  106,
154302 /*  1660 */   244,  240,  244,  273,  192,  112,   60,  114,  115,  116,
154303 /*  1670 */   139,  290,  119,    5,  196,  238,  196,   38,   10,   11,
154304 /*  1680 */    12,   13,   14,  196,  287,   17,  148,  287,  276,  113,
154305 /*  1690 */    43,   22,  229,  147,  241,   18,  232,  232,   30,  232,
154306 /*  1700 */    32,  232,  264,  150,  151,  152,  153,  154,   40,  265,
154307 /*  1710 */   196,   18,  195,  264,  241,  241,  241,  265,  196,  229,
154308 /*  1720 */   229,  195,  155,   62,  196,  195,  283,  282,   22,  216,
154309 /*  1730 */   196,  195,  216,  196,  195,  113,  213,  213,   70,   64,
154310 /*  1740 */   213,  222,   22,  124,  162,  111,   78,  142,  219,   81,
154311 /*  1750 */   215,  219,  275,  303,  213,  213,  216,  275,  213,  216,
154312 /*  1760 */   213,  256,  113,  255,  255,   97,  222,  216,  256,  196,
154313 /*  1770 */    91,  256,   82,  255,  308,  146,  308,   22,  143,  196,
154314 /*  1780 */   270,  155,  145,  272,  144,   25,   13,  199,   26,  256,
154315 /*  1790 */   198,  190,  190,    6,  296,  188,  188,  188,  244,  131,
154316 /*  1800 */   245,  245,  243,  242,  136,  137,  241,  255,  208,  260,
154317 /*  1810 */   260,  208,  202,  217,  217,  202,    4,    3,  202,  208,
154318 /*  1820 */   208,   22,  160,  209,  209,  208,   15,  159,   98,   16,
154319 /*  1830 */    23,   23,  137,  148,   24,  128,  140,   20,   16,  142,
154320 /*  1840 */     1,  140,  128,  149,   61,   53,   37,  148,   53,   53,
154321 /*  1850 */    53,  293,  128,  296,  114,   34,  139,    1,    5,   22,
154322 /*  1860 */   113,  158,   68,   75,   26,   41,   68,  139,   24,  113,
154323 /*  1870 */    20,   19,  129,  123,   23,   96,   22,   22,   59,   22,
154324 /*  1880 */    22,  147,   67,   67,   24,   22,   37,   28,   23,   22,
154325 /*  1890 */    67,   23,   23,   23,  114,   23,   22,   26,   22,   24,
154326 /*  1900 */    23,   22,   24,  139,   23,   23,  141,   34,   88,   26,
154327 /*  1910 */    75,   86,   23,   22,   34,   75,   24,   23,   34,   34,
154328 /*  1920 */    34,   93,   34,   26,   26,   23,   23,   34,   23,   23,
154329 /*  1930 */    26,   44,   23,   22,   11,   22,   22,  133,   23,   23,
154330 /*  1940 */    22,   22,  139,   26,  139,  139,   15,   23,    1,    1,
154331 /*  1950 */   310,  310,  310,  310,  310,  310,  310,  139,  310,  310,
154332 /*  1960 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154333 /*  1970 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154334 /*  1980 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154335 /*  1990 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154336 /*  2000 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154337 /*  2010 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154338 /*  2020 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154339 /*  2030 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154340 /*  2040 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154341 /*  2050 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154342 /*  2060 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154343 /*  2070 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154344 /*  2080 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154345 /*  2090 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154346 /*  2100 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154347 /*  2110 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154348 /*  2120 */   310,  310,  310,  310,  310,  310,  310,  310,  310,  310,
154349 /*  2130 */   310,  310,  310,  310,  310,  310,  310,  310,  310,
154350};
154351#define YY_SHIFT_COUNT    (550)
154352#define YY_SHIFT_MIN      (0)
154353#define YY_SHIFT_MAX      (1948)
154354static const unsigned short int yy_shift_ofst[] = {
154355 /*     0 */  1448, 1277, 1668, 1072, 1072,  340, 1122, 1225, 1332, 1481,
154356 /*    10 */  1481, 1481,  335,    0,    0,  180,  897, 1481, 1481, 1481,
154357 /*    20 */  1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481,
154358 /*    30 */   930,  930, 1020, 1020,  290,    1,  340,  340,  340,  340,
154359 /*    40 */   340,  340,   40,  110,  219,  288,  327,  396,  435,  504,
154360 /*    50 */   543,  612,  651,  720,  877,  897,  897,  897,  897,  897,
154361 /*    60 */   897,  897,  897,  897,  897,  897,  897,  897,  897,  897,
154362 /*    70 */   897,  897,  897,  917,  897, 1019,  763,  763, 1451, 1481,
154363 /*    80 */  1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481,
154364 /*    90 */  1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481,
154365 /*   100 */  1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481,
154366 /*   110 */  1481, 1481, 1553, 1481, 1481, 1481, 1481, 1481, 1481, 1481,
154367 /*   120 */  1481, 1481, 1481, 1481, 1481, 1481,  147,  258,  258,  258,
154368 /*   130 */   258,  258,   79,   65,   84,  449,   19,  786,  449,  636,
154369 /*   140 */   636,  449,  880,  880,  880,  880,  113,  142,  142,  472,
154370 /*   150 */   150, 1958, 1958,  399,  399,  399,   93,  237,  341,  237,
154371 /*   160 */   237, 1074, 1074,  437,  350,  704, 1080,  449,  449,  449,
154372 /*   170 */   449,  449,  449,  449,  449,  449,  449,  449,  449,  449,
154373 /*   180 */   449,  449,  449,  449,  449,  449,  449,  449,  818,  818,
154374 /*   190 */   449, 1088,  217,  217,  734,  734, 1124, 1126, 1958, 1958,
154375 /*   200 */  1958,  739,  840,  840,  453,  454,  511,  187,  563,  570,
154376 /*   210 */   898,  669,  449,  449,  449,  449,  449,  449,  449,  449,
154377 /*   220 */   449,  670,  449,  449,  449,  449,  449,  449,  449,  449,
154378 /*   230 */   449,  449,  449,  449,  674,  674,  674,  449,  449,  449,
154379 /*   240 */   449, 1034,  449,  449,  449,  972, 1107,  449,  449, 1193,
154380 /*   250 */   449,  449,  449,  449,  449,  449,  449,  449,  260,  177,
154381 /*   260 */   489, 1241, 1241, 1241, 1241, 1192,  489,  489,  952, 1197,
154382 /*   270 */   625, 1235, 1139,  181,  181, 1086, 1139, 1139, 1086, 1187,
154383 /*   280 */  1131, 1237, 1314, 1314, 1314,  181, 1245, 1245, 1109, 1299,
154384 /*   290 */   549, 1340, 1606, 1531, 1531, 1639, 1639, 1531, 1538, 1576,
154385 /*   300 */  1669, 1647, 1546, 1677, 1677, 1677, 1677, 1531, 1693, 1546,
154386 /*   310 */  1546, 1576, 1669, 1647, 1647, 1546, 1531, 1693, 1567, 1661,
154387 /*   320 */  1531, 1693, 1706, 1531, 1693, 1531, 1693, 1706, 1622, 1622,
154388 /*   330 */  1622, 1675, 1720, 1720, 1706, 1622, 1619, 1622, 1675, 1622,
154389 /*   340 */  1622, 1582, 1706, 1634, 1634, 1706, 1605, 1649, 1605, 1649,
154390 /*   350 */  1605, 1649, 1605, 1649, 1531, 1679, 1679, 1690, 1690, 1629,
154391 /*   360 */  1635, 1755, 1531, 1626, 1629, 1637, 1640, 1546, 1760, 1762,
154392 /*   370 */  1773, 1773, 1787, 1787, 1787, 1958, 1958, 1958, 1958, 1958,
154393 /*   380 */  1958, 1958, 1958, 1958, 1958, 1958, 1958, 1958, 1958, 1958,
154394 /*   390 */   308,  835,  954, 1232,  879,  715,  728, 1323,  864, 1318,
154395 /*   400 */  1253, 1373,  297, 1409, 1419, 1440, 1489, 1497, 1520, 1242,
154396 /*   410 */  1309, 1447, 1435, 1341, 1521, 1525, 1392, 1548, 1329, 1354,
154397 /*   420 */  1585, 1587, 1353, 1382, 1812, 1814, 1799, 1662, 1811, 1730,
154398 /*   430 */  1813, 1807, 1808, 1695, 1685, 1707, 1810, 1696, 1817, 1697,
154399 /*   440 */  1822, 1839, 1701, 1694, 1714, 1783, 1809, 1699, 1792, 1795,
154400 /*   450 */  1796, 1797, 1724, 1740, 1821, 1717, 1856, 1853, 1837, 1747,
154401 /*   460 */  1703, 1794, 1838, 1798, 1788, 1824, 1728, 1756, 1844, 1850,
154402 /*   470 */  1852, 1743, 1750, 1854, 1815, 1855, 1857, 1851, 1858, 1816,
154403 /*   480 */  1819, 1860, 1779, 1859, 1863, 1823, 1849, 1865, 1734, 1867,
154404 /*   490 */  1868, 1869, 1870, 1871, 1872, 1874, 1875, 1877, 1876, 1878,
154405 /*   500 */  1764, 1881, 1882, 1780, 1873, 1879, 1765, 1883, 1880, 1884,
154406 /*   510 */  1885, 1886, 1820, 1835, 1825, 1887, 1840, 1828, 1888, 1889,
154407 /*   520 */  1891, 1892, 1897, 1898, 1893, 1894, 1883, 1902, 1903, 1905,
154408 /*   530 */  1906, 1904, 1909, 1911, 1923, 1913, 1914, 1915, 1916, 1918,
154409 /*   540 */  1919, 1917, 1804, 1803, 1805, 1806, 1818, 1924, 1931, 1947,
154410 /*   550 */  1948,
154411};
154412#define YY_REDUCE_COUNT (389)
154413#define YY_REDUCE_MIN   (-262)
154414#define YY_REDUCE_MAX   (1617)
154415static const short yy_reduce_ofst[] = {
154416 /*     0 */   490, -122,  545,  645,  650, -120, -189, -187, -184, -182,
154417 /*    10 */  -178, -176,   45,   30,  200, -251, -134,  390,  392,  521,
154418 /*    20 */   523,  213,  692,  821,  284,  589,  872,  666,  671,  866,
154419 /*    30 */    71,  111,  273,  389,  686,  815,  904,  932,  948,  955,
154420 /*    40 */   964,  969, -259, -259, -259, -259, -259, -259, -259, -259,
154421 /*    50 */  -259, -259, -259, -259, -259, -259, -259, -259, -259, -259,
154422 /*    60 */  -259, -259, -259, -259, -259, -259, -259, -259, -259, -259,
154423 /*    70 */  -259, -259, -259, -259, -259, -259, -259, -259,  428,  430,
154424 /*    80 */   899,  985, 1021, 1028, 1057, 1069, 1081, 1108, 1110, 1115,
154425 /*    90 */  1117, 1123, 1149, 1154, 1159, 1170, 1174, 1178, 1183, 1194,
154426 /*   100 */  1198, 1204, 1208, 1212, 1218, 1222, 1229, 1278, 1280, 1283,
154427 /*   110 */  1285, 1313, 1316, 1320, 1322, 1325, 1327, 1330, 1366, 1371,
154428 /*   120 */  1379, 1387, 1417, 1425, 1430, 1432, -259, -259, -259, -259,
154429 /*   130 */  -259, -259, -259, -259, -259,  557,  974, -214, -174,   -9,
154430 /*   140 */   431, -124,  806,  925,  806,  925,  251,  928,  940, -259,
154431 /*   150 */  -259, -259, -259, -198, -198, -198,  127, -186, -168,  212,
154432 /*   160 */   646,  617,  799, -262,  555,  220,  220,  491,  605, 1040,
154433 /*   170 */  1060,  699,  -11,  600,  848,  862,  345, -129,  724,  -91,
154434 /*   180 */   158,  749,  716,  900,  304,  822,  929,  926,  499,  793,
154435 /*   190 */   322,  892,  813,  845,  958, 1056,  751,  905, 1133, 1062,
154436 /*   200 */   803, -210, -185, -179, -148, -167,  -89,  121,  274,  281,
154437 /*   210 */   320,  336,  439,  663,  711,  957, 1064, 1068, 1116, 1127,
154438 /*   220 */  1134, -196, 1147, 1180, 1184, 1195, 1203, 1209, 1254, 1263,
154439 /*   230 */  1275, 1288, 1304, 1310,  205,  422,  638, 1319, 1324, 1346,
154440 /*   240 */  1360, 1168, 1364, 1370, 1372,  869, 1189, 1380, 1399, 1276,
154441 /*   250 */  1403,  121, 1405, 1420, 1426, 1427, 1428, 1429, 1249, 1282,
154442 /*   260 */  1344, 1375, 1376, 1377, 1388, 1168, 1344, 1344, 1384, 1411,
154443 /*   270 */  1436, 1349, 1389, 1386, 1391, 1361, 1407, 1408, 1365, 1431,
154444 /*   280 */  1433, 1434, 1439, 1441, 1442, 1396, 1416, 1418, 1390, 1421,
154445 /*   290 */  1437, 1472, 1381, 1478, 1480, 1397, 1400, 1487, 1412, 1444,
154446 /*   300 */  1438, 1463, 1453, 1464, 1465, 1467, 1469, 1514, 1517, 1473,
154447 /*   310 */  1474, 1452, 1449, 1490, 1491, 1475, 1522, 1526, 1443, 1445,
154448 /*   320 */  1528, 1530, 1513, 1534, 1536, 1537, 1539, 1516, 1523, 1524,
154449 /*   330 */  1527, 1519, 1529, 1532, 1540, 1541, 1535, 1542, 1544, 1545,
154450 /*   340 */  1547, 1450, 1543, 1477, 1482, 1551, 1505, 1508, 1512, 1509,
154451 /*   350 */  1515, 1518, 1533, 1552, 1573, 1466, 1468, 1549, 1550, 1555,
154452 /*   360 */  1554, 1510, 1583, 1511, 1556, 1559, 1561, 1565, 1588, 1592,
154453 /*   370 */  1601, 1602, 1607, 1608, 1609, 1498, 1557, 1558, 1610, 1600,
154454 /*   380 */  1603, 1611, 1612, 1613, 1596, 1597, 1614, 1615, 1617, 1616,
154455};
154456static const YYACTIONTYPE yy_default[] = {
154457 /*     0 */  1573, 1573, 1573, 1409, 1186, 1295, 1186, 1186, 1186, 1409,
154458 /*    10 */  1409, 1409, 1186, 1325, 1325, 1462, 1217, 1186, 1186, 1186,
154459 /*    20 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1408, 1186, 1186,
154460 /*    30 */  1186, 1186, 1492, 1492, 1186, 1186, 1186, 1186, 1186, 1186,
154461 /*    40 */  1186, 1186, 1186, 1334, 1186, 1186, 1186, 1186, 1186, 1186,
154462 /*    50 */  1410, 1411, 1186, 1186, 1186, 1461, 1463, 1426, 1344, 1343,
154463 /*    60 */  1342, 1341, 1444, 1312, 1339, 1332, 1336, 1404, 1405, 1403,
154464 /*    70 */  1407, 1411, 1410, 1186, 1335, 1375, 1389, 1374, 1186, 1186,
154465 /*    80 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186,
154466 /*    90 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186,
154467 /*   100 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186,
154468 /*   110 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186,
154469 /*   120 */  1186, 1186, 1186, 1186, 1186, 1186, 1383, 1388, 1394, 1387,
154470 /*   130 */  1384, 1377, 1376, 1378, 1379, 1186, 1207, 1259, 1186, 1186,
154471 /*   140 */  1186, 1186, 1480, 1479, 1186, 1186, 1217, 1369, 1368, 1380,
154472 /*   150 */  1381, 1391, 1390, 1469, 1527, 1526, 1427, 1186, 1186, 1186,
154473 /*   160 */  1186, 1186, 1186, 1492, 1186, 1186, 1186, 1186, 1186, 1186,
154474 /*   170 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186,
154475 /*   180 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1492, 1492,
154476 /*   190 */  1186, 1217, 1492, 1492, 1213, 1213, 1319, 1186, 1475, 1295,
154477 /*   200 */  1286, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186,
154478 /*   210 */  1186, 1186, 1186, 1186, 1186, 1466, 1464, 1186, 1186, 1186,
154479 /*   220 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186,
154480 /*   230 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186,
154481 /*   240 */  1186, 1186, 1186, 1186, 1186, 1291, 1186, 1186, 1186, 1186,
154482 /*   250 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1521, 1186, 1439,
154483 /*   260 */  1273, 1291, 1291, 1291, 1291, 1293, 1274, 1272, 1285, 1218,
154484 /*   270 */  1193, 1565, 1338, 1314, 1314, 1562, 1338, 1338, 1562, 1234,
154485 /*   280 */  1543, 1229, 1325, 1325, 1325, 1314, 1319, 1319, 1406, 1292,
154486 /*   290 */  1285, 1186, 1565, 1300, 1300, 1564, 1564, 1300, 1427, 1347,
154487 /*   300 */  1353, 1262, 1338, 1268, 1268, 1268, 1268, 1300, 1204, 1338,
154488 /*   310 */  1338, 1347, 1353, 1262, 1262, 1338, 1300, 1204, 1443, 1559,
154489 /*   320 */  1300, 1204, 1417, 1300, 1204, 1300, 1204, 1417, 1260, 1260,
154490 /*   330 */  1260, 1249, 1186, 1186, 1417, 1260, 1234, 1260, 1249, 1260,
154491 /*   340 */  1260, 1510, 1417, 1421, 1421, 1417, 1318, 1313, 1318, 1313,
154492 /*   350 */  1318, 1313, 1318, 1313, 1300, 1502, 1502, 1328, 1328, 1333,
154493 /*   360 */  1319, 1412, 1300, 1186, 1333, 1331, 1329, 1338, 1210, 1252,
154494 /*   370 */  1524, 1524, 1520, 1520, 1520, 1570, 1570, 1475, 1536, 1217,
154495 /*   380 */  1217, 1217, 1217, 1536, 1236, 1236, 1218, 1218, 1217, 1536,
154496 /*   390 */  1186, 1186, 1186, 1186, 1186, 1186, 1531, 1186, 1428, 1304,
154497 /*   400 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186,
154498 /*   410 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186,
154499 /*   420 */  1186, 1186, 1186, 1358, 1186, 1189, 1472, 1186, 1186, 1470,
154500 /*   430 */  1186, 1186, 1186, 1186, 1186, 1186, 1305, 1186, 1186, 1186,
154501 /*   440 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186,
154502 /*   450 */  1186, 1186, 1186, 1186, 1186, 1561, 1186, 1186, 1186, 1186,
154503 /*   460 */  1186, 1186, 1442, 1441, 1186, 1186, 1302, 1186, 1186, 1186,
154504 /*   470 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186,
154505 /*   480 */  1232, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186,
154506 /*   490 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186,
154507 /*   500 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1330, 1186, 1186,
154508 /*   510 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186,
154509 /*   520 */  1186, 1186, 1507, 1320, 1186, 1186, 1552, 1186, 1186, 1186,
154510 /*   530 */  1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186, 1186,
154511 /*   540 */  1186, 1547, 1276, 1360, 1186, 1359, 1363, 1186, 1198, 1186,
154512 /*   550 */  1186,
154513};
154514/********** End of lemon-generated parsing tables *****************************/
154515
154516/* The next table maps tokens (terminal symbols) into fallback tokens.
154517** If a construct like the following:
154518**
154519**      %fallback ID X Y Z.
154520**
154521** appears in the grammar, then ID becomes a fallback token for X, Y,
154522** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
154523** but it does not parse, the type of the token is changed to ID and
154524** the parse is retried before an error is thrown.
154525**
154526** This feature can be used, for example, to cause some keywords in a language
154527** to revert to identifiers if they keyword does not apply in the context where
154528** it appears.
154529*/
154530#ifdef YYFALLBACK
154531static const YYCODETYPE yyFallback[] = {
154532    0,  /*          $ => nothing */
154533    0,  /*       SEMI => nothing */
154534   59,  /*    EXPLAIN => ID */
154535   59,  /*      QUERY => ID */
154536   59,  /*       PLAN => ID */
154537   59,  /*      BEGIN => ID */
154538    0,  /* TRANSACTION => nothing */
154539   59,  /*   DEFERRED => ID */
154540   59,  /*  IMMEDIATE => ID */
154541   59,  /*  EXCLUSIVE => ID */
154542    0,  /*     COMMIT => nothing */
154543   59,  /*        END => ID */
154544   59,  /*   ROLLBACK => ID */
154545   59,  /*  SAVEPOINT => ID */
154546   59,  /*    RELEASE => ID */
154547    0,  /*         TO => nothing */
154548    0,  /*      TABLE => nothing */
154549    0,  /*     CREATE => nothing */
154550   59,  /*         IF => ID */
154551    0,  /*        NOT => nothing */
154552    0,  /*     EXISTS => nothing */
154553   59,  /*       TEMP => ID */
154554    0,  /*         LP => nothing */
154555    0,  /*         RP => nothing */
154556    0,  /*         AS => nothing */
154557   59,  /*    WITHOUT => ID */
154558    0,  /*      COMMA => nothing */
154559   59,  /*      ABORT => ID */
154560   59,  /*     ACTION => ID */
154561   59,  /*      AFTER => ID */
154562   59,  /*    ANALYZE => ID */
154563   59,  /*        ASC => ID */
154564   59,  /*     ATTACH => ID */
154565   59,  /*     BEFORE => ID */
154566   59,  /*         BY => ID */
154567   59,  /*    CASCADE => ID */
154568   59,  /*       CAST => ID */
154569   59,  /*   CONFLICT => ID */
154570   59,  /*   DATABASE => ID */
154571   59,  /*       DESC => ID */
154572   59,  /*     DETACH => ID */
154573   59,  /*       EACH => ID */
154574   59,  /*       FAIL => ID */
154575    0,  /*         OR => nothing */
154576    0,  /*        AND => nothing */
154577    0,  /*         IS => nothing */
154578   59,  /*      MATCH => ID */
154579   59,  /*    LIKE_KW => ID */
154580    0,  /*    BETWEEN => nothing */
154581    0,  /*         IN => nothing */
154582    0,  /*     ISNULL => nothing */
154583    0,  /*    NOTNULL => nothing */
154584    0,  /*         NE => nothing */
154585    0,  /*         EQ => nothing */
154586    0,  /*         GT => nothing */
154587    0,  /*         LE => nothing */
154588    0,  /*         LT => nothing */
154589    0,  /*         GE => nothing */
154590    0,  /*     ESCAPE => nothing */
154591    0,  /*         ID => nothing */
154592   59,  /*   COLUMNKW => ID */
154593   59,  /*         DO => ID */
154594   59,  /*        FOR => ID */
154595   59,  /*     IGNORE => ID */
154596   59,  /*  INITIALLY => ID */
154597   59,  /*    INSTEAD => ID */
154598   59,  /*         NO => ID */
154599   59,  /*        KEY => ID */
154600   59,  /*         OF => ID */
154601   59,  /*     OFFSET => ID */
154602   59,  /*     PRAGMA => ID */
154603   59,  /*      RAISE => ID */
154604   59,  /*  RECURSIVE => ID */
154605   59,  /*    REPLACE => ID */
154606   59,  /*   RESTRICT => ID */
154607   59,  /*        ROW => ID */
154608   59,  /*       ROWS => ID */
154609   59,  /*    TRIGGER => ID */
154610   59,  /*     VACUUM => ID */
154611   59,  /*       VIEW => ID */
154612   59,  /*    VIRTUAL => ID */
154613   59,  /*       WITH => ID */
154614   59,  /*      NULLS => ID */
154615   59,  /*      FIRST => ID */
154616   59,  /*       LAST => ID */
154617   59,  /*    CURRENT => ID */
154618   59,  /*  FOLLOWING => ID */
154619   59,  /*  PARTITION => ID */
154620   59,  /*  PRECEDING => ID */
154621   59,  /*      RANGE => ID */
154622   59,  /*  UNBOUNDED => ID */
154623   59,  /*    EXCLUDE => ID */
154624   59,  /*     GROUPS => ID */
154625   59,  /*     OTHERS => ID */
154626   59,  /*       TIES => ID */
154627   59,  /*  GENERATED => ID */
154628   59,  /*     ALWAYS => ID */
154629   59,  /*    REINDEX => ID */
154630   59,  /*     RENAME => ID */
154631   59,  /*   CTIME_KW => ID */
154632    0,  /*        ANY => nothing */
154633    0,  /*     BITAND => nothing */
154634    0,  /*      BITOR => nothing */
154635    0,  /*     LSHIFT => nothing */
154636    0,  /*     RSHIFT => nothing */
154637    0,  /*       PLUS => nothing */
154638    0,  /*      MINUS => nothing */
154639    0,  /*       STAR => nothing */
154640    0,  /*      SLASH => nothing */
154641    0,  /*        REM => nothing */
154642    0,  /*     CONCAT => nothing */
154643    0,  /*    COLLATE => nothing */
154644    0,  /*     BITNOT => nothing */
154645    0,  /*         ON => nothing */
154646    0,  /*    INDEXED => nothing */
154647    0,  /*     STRING => nothing */
154648    0,  /*    JOIN_KW => nothing */
154649    0,  /* CONSTRAINT => nothing */
154650    0,  /*    DEFAULT => nothing */
154651    0,  /*       NULL => nothing */
154652    0,  /*    PRIMARY => nothing */
154653    0,  /*     UNIQUE => nothing */
154654    0,  /*      CHECK => nothing */
154655    0,  /* REFERENCES => nothing */
154656    0,  /*   AUTOINCR => nothing */
154657    0,  /*     INSERT => nothing */
154658    0,  /*     DELETE => nothing */
154659    0,  /*     UPDATE => nothing */
154660    0,  /*        SET => nothing */
154661    0,  /* DEFERRABLE => nothing */
154662    0,  /*    FOREIGN => nothing */
154663    0,  /*       DROP => nothing */
154664    0,  /*      UNION => nothing */
154665    0,  /*        ALL => nothing */
154666    0,  /*     EXCEPT => nothing */
154667    0,  /*  INTERSECT => nothing */
154668    0,  /*     SELECT => nothing */
154669    0,  /*     VALUES => nothing */
154670    0,  /*   DISTINCT => nothing */
154671    0,  /*        DOT => nothing */
154672    0,  /*       FROM => nothing */
154673    0,  /*       JOIN => nothing */
154674    0,  /*      USING => nothing */
154675    0,  /*      ORDER => nothing */
154676    0,  /*      GROUP => nothing */
154677    0,  /*     HAVING => nothing */
154678    0,  /*      LIMIT => nothing */
154679    0,  /*      WHERE => nothing */
154680    0,  /*       INTO => nothing */
154681    0,  /*    NOTHING => nothing */
154682    0,  /*      FLOAT => nothing */
154683    0,  /*       BLOB => nothing */
154684    0,  /*    INTEGER => nothing */
154685    0,  /*   VARIABLE => nothing */
154686    0,  /*       CASE => nothing */
154687    0,  /*       WHEN => nothing */
154688    0,  /*       THEN => nothing */
154689    0,  /*       ELSE => nothing */
154690    0,  /*      INDEX => nothing */
154691    0,  /*      ALTER => nothing */
154692    0,  /*        ADD => nothing */
154693    0,  /*     WINDOW => nothing */
154694    0,  /*       OVER => nothing */
154695    0,  /*     FILTER => nothing */
154696    0,  /*     COLUMN => nothing */
154697    0,  /* AGG_FUNCTION => nothing */
154698    0,  /* AGG_COLUMN => nothing */
154699    0,  /*  TRUEFALSE => nothing */
154700    0,  /*      ISNOT => nothing */
154701    0,  /*   FUNCTION => nothing */
154702    0,  /*     UMINUS => nothing */
154703    0,  /*      UPLUS => nothing */
154704    0,  /*      TRUTH => nothing */
154705    0,  /*   REGISTER => nothing */
154706    0,  /*     VECTOR => nothing */
154707    0,  /* SELECT_COLUMN => nothing */
154708    0,  /* IF_NULL_ROW => nothing */
154709    0,  /*   ASTERISK => nothing */
154710    0,  /*       SPAN => nothing */
154711    0,  /*      SPACE => nothing */
154712    0,  /*    ILLEGAL => nothing */
154713};
154714#endif /* YYFALLBACK */
154715
154716/* The following structure represents a single element of the
154717** parser's stack.  Information stored includes:
154718**
154719**   +  The state number for the parser at this level of the stack.
154720**
154721**   +  The value of the token stored at this level of the stack.
154722**      (In other words, the "major" token.)
154723**
154724**   +  The semantic value stored at this level of the stack.  This is
154725**      the information used by the action routines in the grammar.
154726**      It is sometimes called the "minor" token.
154727**
154728** After the "shift" half of a SHIFTREDUCE action, the stateno field
154729** actually contains the reduce action for the second half of the
154730** SHIFTREDUCE.
154731*/
154732struct yyStackEntry {
154733  YYACTIONTYPE stateno;  /* The state-number, or reduce action in SHIFTREDUCE */
154734  YYCODETYPE major;      /* The major token value.  This is the code
154735                         ** number for the token at this stack level */
154736  YYMINORTYPE minor;     /* The user-supplied minor token value.  This
154737                         ** is the value of the token  */
154738};
154739typedef struct yyStackEntry yyStackEntry;
154740
154741/* The state of the parser is completely contained in an instance of
154742** the following structure */
154743struct yyParser {
154744  yyStackEntry *yytos;          /* Pointer to top element of the stack */
154745#ifdef YYTRACKMAXSTACKDEPTH
154746  int yyhwm;                    /* High-water mark of the stack */
154747#endif
154748#ifndef YYNOERRORRECOVERY
154749  int yyerrcnt;                 /* Shifts left before out of the error */
154750#endif
154751  sqlite3ParserARG_SDECL                /* A place to hold %extra_argument */
154752  sqlite3ParserCTX_SDECL                /* A place to hold %extra_context */
154753#if YYSTACKDEPTH<=0
154754  int yystksz;                  /* Current side of the stack */
154755  yyStackEntry *yystack;        /* The parser's stack */
154756  yyStackEntry yystk0;          /* First stack entry */
154757#else
154758  yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
154759  yyStackEntry *yystackEnd;            /* Last entry in the stack */
154760#endif
154761};
154762typedef struct yyParser yyParser;
154763
154764#ifndef NDEBUG
154765/* #include <stdio.h> */
154766static FILE *yyTraceFILE = 0;
154767static char *yyTracePrompt = 0;
154768#endif /* NDEBUG */
154769
154770#ifndef NDEBUG
154771/*
154772** Turn parser tracing on by giving a stream to which to write the trace
154773** and a prompt to preface each trace message.  Tracing is turned off
154774** by making either argument NULL
154775**
154776** Inputs:
154777** <ul>
154778** <li> A FILE* to which trace output should be written.
154779**      If NULL, then tracing is turned off.
154780** <li> A prefix string written at the beginning of every
154781**      line of trace output.  If NULL, then tracing is
154782**      turned off.
154783** </ul>
154784**
154785** Outputs:
154786** None.
154787*/
154788SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
154789  yyTraceFILE = TraceFILE;
154790  yyTracePrompt = zTracePrompt;
154791  if( yyTraceFILE==0 ) yyTracePrompt = 0;
154792  else if( yyTracePrompt==0 ) yyTraceFILE = 0;
154793}
154794#endif /* NDEBUG */
154795
154796#if defined(YYCOVERAGE) || !defined(NDEBUG)
154797/* For tracing shifts, the names of all terminals and nonterminals
154798** are required.  The following table supplies these names */
154799static const char *const yyTokenName[] = {
154800  /*    0 */ "$",
154801  /*    1 */ "SEMI",
154802  /*    2 */ "EXPLAIN",
154803  /*    3 */ "QUERY",
154804  /*    4 */ "PLAN",
154805  /*    5 */ "BEGIN",
154806  /*    6 */ "TRANSACTION",
154807  /*    7 */ "DEFERRED",
154808  /*    8 */ "IMMEDIATE",
154809  /*    9 */ "EXCLUSIVE",
154810  /*   10 */ "COMMIT",
154811  /*   11 */ "END",
154812  /*   12 */ "ROLLBACK",
154813  /*   13 */ "SAVEPOINT",
154814  /*   14 */ "RELEASE",
154815  /*   15 */ "TO",
154816  /*   16 */ "TABLE",
154817  /*   17 */ "CREATE",
154818  /*   18 */ "IF",
154819  /*   19 */ "NOT",
154820  /*   20 */ "EXISTS",
154821  /*   21 */ "TEMP",
154822  /*   22 */ "LP",
154823  /*   23 */ "RP",
154824  /*   24 */ "AS",
154825  /*   25 */ "WITHOUT",
154826  /*   26 */ "COMMA",
154827  /*   27 */ "ABORT",
154828  /*   28 */ "ACTION",
154829  /*   29 */ "AFTER",
154830  /*   30 */ "ANALYZE",
154831  /*   31 */ "ASC",
154832  /*   32 */ "ATTACH",
154833  /*   33 */ "BEFORE",
154834  /*   34 */ "BY",
154835  /*   35 */ "CASCADE",
154836  /*   36 */ "CAST",
154837  /*   37 */ "CONFLICT",
154838  /*   38 */ "DATABASE",
154839  /*   39 */ "DESC",
154840  /*   40 */ "DETACH",
154841  /*   41 */ "EACH",
154842  /*   42 */ "FAIL",
154843  /*   43 */ "OR",
154844  /*   44 */ "AND",
154845  /*   45 */ "IS",
154846  /*   46 */ "MATCH",
154847  /*   47 */ "LIKE_KW",
154848  /*   48 */ "BETWEEN",
154849  /*   49 */ "IN",
154850  /*   50 */ "ISNULL",
154851  /*   51 */ "NOTNULL",
154852  /*   52 */ "NE",
154853  /*   53 */ "EQ",
154854  /*   54 */ "GT",
154855  /*   55 */ "LE",
154856  /*   56 */ "LT",
154857  /*   57 */ "GE",
154858  /*   58 */ "ESCAPE",
154859  /*   59 */ "ID",
154860  /*   60 */ "COLUMNKW",
154861  /*   61 */ "DO",
154862  /*   62 */ "FOR",
154863  /*   63 */ "IGNORE",
154864  /*   64 */ "INITIALLY",
154865  /*   65 */ "INSTEAD",
154866  /*   66 */ "NO",
154867  /*   67 */ "KEY",
154868  /*   68 */ "OF",
154869  /*   69 */ "OFFSET",
154870  /*   70 */ "PRAGMA",
154871  /*   71 */ "RAISE",
154872  /*   72 */ "RECURSIVE",
154873  /*   73 */ "REPLACE",
154874  /*   74 */ "RESTRICT",
154875  /*   75 */ "ROW",
154876  /*   76 */ "ROWS",
154877  /*   77 */ "TRIGGER",
154878  /*   78 */ "VACUUM",
154879  /*   79 */ "VIEW",
154880  /*   80 */ "VIRTUAL",
154881  /*   81 */ "WITH",
154882  /*   82 */ "NULLS",
154883  /*   83 */ "FIRST",
154884  /*   84 */ "LAST",
154885  /*   85 */ "CURRENT",
154886  /*   86 */ "FOLLOWING",
154887  /*   87 */ "PARTITION",
154888  /*   88 */ "PRECEDING",
154889  /*   89 */ "RANGE",
154890  /*   90 */ "UNBOUNDED",
154891  /*   91 */ "EXCLUDE",
154892  /*   92 */ "GROUPS",
154893  /*   93 */ "OTHERS",
154894  /*   94 */ "TIES",
154895  /*   95 */ "GENERATED",
154896  /*   96 */ "ALWAYS",
154897  /*   97 */ "REINDEX",
154898  /*   98 */ "RENAME",
154899  /*   99 */ "CTIME_KW",
154900  /*  100 */ "ANY",
154901  /*  101 */ "BITAND",
154902  /*  102 */ "BITOR",
154903  /*  103 */ "LSHIFT",
154904  /*  104 */ "RSHIFT",
154905  /*  105 */ "PLUS",
154906  /*  106 */ "MINUS",
154907  /*  107 */ "STAR",
154908  /*  108 */ "SLASH",
154909  /*  109 */ "REM",
154910  /*  110 */ "CONCAT",
154911  /*  111 */ "COLLATE",
154912  /*  112 */ "BITNOT",
154913  /*  113 */ "ON",
154914  /*  114 */ "INDEXED",
154915  /*  115 */ "STRING",
154916  /*  116 */ "JOIN_KW",
154917  /*  117 */ "CONSTRAINT",
154918  /*  118 */ "DEFAULT",
154919  /*  119 */ "NULL",
154920  /*  120 */ "PRIMARY",
154921  /*  121 */ "UNIQUE",
154922  /*  122 */ "CHECK",
154923  /*  123 */ "REFERENCES",
154924  /*  124 */ "AUTOINCR",
154925  /*  125 */ "INSERT",
154926  /*  126 */ "DELETE",
154927  /*  127 */ "UPDATE",
154928  /*  128 */ "SET",
154929  /*  129 */ "DEFERRABLE",
154930  /*  130 */ "FOREIGN",
154931  /*  131 */ "DROP",
154932  /*  132 */ "UNION",
154933  /*  133 */ "ALL",
154934  /*  134 */ "EXCEPT",
154935  /*  135 */ "INTERSECT",
154936  /*  136 */ "SELECT",
154937  /*  137 */ "VALUES",
154938  /*  138 */ "DISTINCT",
154939  /*  139 */ "DOT",
154940  /*  140 */ "FROM",
154941  /*  141 */ "JOIN",
154942  /*  142 */ "USING",
154943  /*  143 */ "ORDER",
154944  /*  144 */ "GROUP",
154945  /*  145 */ "HAVING",
154946  /*  146 */ "LIMIT",
154947  /*  147 */ "WHERE",
154948  /*  148 */ "INTO",
154949  /*  149 */ "NOTHING",
154950  /*  150 */ "FLOAT",
154951  /*  151 */ "BLOB",
154952  /*  152 */ "INTEGER",
154953  /*  153 */ "VARIABLE",
154954  /*  154 */ "CASE",
154955  /*  155 */ "WHEN",
154956  /*  156 */ "THEN",
154957  /*  157 */ "ELSE",
154958  /*  158 */ "INDEX",
154959  /*  159 */ "ALTER",
154960  /*  160 */ "ADD",
154961  /*  161 */ "WINDOW",
154962  /*  162 */ "OVER",
154963  /*  163 */ "FILTER",
154964  /*  164 */ "COLUMN",
154965  /*  165 */ "AGG_FUNCTION",
154966  /*  166 */ "AGG_COLUMN",
154967  /*  167 */ "TRUEFALSE",
154968  /*  168 */ "ISNOT",
154969  /*  169 */ "FUNCTION",
154970  /*  170 */ "UMINUS",
154971  /*  171 */ "UPLUS",
154972  /*  172 */ "TRUTH",
154973  /*  173 */ "REGISTER",
154974  /*  174 */ "VECTOR",
154975  /*  175 */ "SELECT_COLUMN",
154976  /*  176 */ "IF_NULL_ROW",
154977  /*  177 */ "ASTERISK",
154978  /*  178 */ "SPAN",
154979  /*  179 */ "SPACE",
154980  /*  180 */ "ILLEGAL",
154981  /*  181 */ "input",
154982  /*  182 */ "cmdlist",
154983  /*  183 */ "ecmd",
154984  /*  184 */ "cmdx",
154985  /*  185 */ "explain",
154986  /*  186 */ "cmd",
154987  /*  187 */ "transtype",
154988  /*  188 */ "trans_opt",
154989  /*  189 */ "nm",
154990  /*  190 */ "savepoint_opt",
154991  /*  191 */ "create_table",
154992  /*  192 */ "create_table_args",
154993  /*  193 */ "createkw",
154994  /*  194 */ "temp",
154995  /*  195 */ "ifnotexists",
154996  /*  196 */ "dbnm",
154997  /*  197 */ "columnlist",
154998  /*  198 */ "conslist_opt",
154999  /*  199 */ "table_options",
155000  /*  200 */ "select",
155001  /*  201 */ "columnname",
155002  /*  202 */ "carglist",
155003  /*  203 */ "typetoken",
155004  /*  204 */ "typename",
155005  /*  205 */ "signed",
155006  /*  206 */ "plus_num",
155007  /*  207 */ "minus_num",
155008  /*  208 */ "scanpt",
155009  /*  209 */ "scantok",
155010  /*  210 */ "ccons",
155011  /*  211 */ "term",
155012  /*  212 */ "expr",
155013  /*  213 */ "onconf",
155014  /*  214 */ "sortorder",
155015  /*  215 */ "autoinc",
155016  /*  216 */ "eidlist_opt",
155017  /*  217 */ "refargs",
155018  /*  218 */ "defer_subclause",
155019  /*  219 */ "generated",
155020  /*  220 */ "refarg",
155021  /*  221 */ "refact",
155022  /*  222 */ "init_deferred_pred_opt",
155023  /*  223 */ "conslist",
155024  /*  224 */ "tconscomma",
155025  /*  225 */ "tcons",
155026  /*  226 */ "sortlist",
155027  /*  227 */ "eidlist",
155028  /*  228 */ "defer_subclause_opt",
155029  /*  229 */ "orconf",
155030  /*  230 */ "resolvetype",
155031  /*  231 */ "raisetype",
155032  /*  232 */ "ifexists",
155033  /*  233 */ "fullname",
155034  /*  234 */ "selectnowith",
155035  /*  235 */ "oneselect",
155036  /*  236 */ "wqlist",
155037  /*  237 */ "multiselect_op",
155038  /*  238 */ "distinct",
155039  /*  239 */ "selcollist",
155040  /*  240 */ "from",
155041  /*  241 */ "where_opt",
155042  /*  242 */ "groupby_opt",
155043  /*  243 */ "having_opt",
155044  /*  244 */ "orderby_opt",
155045  /*  245 */ "limit_opt",
155046  /*  246 */ "window_clause",
155047  /*  247 */ "values",
155048  /*  248 */ "nexprlist",
155049  /*  249 */ "sclp",
155050  /*  250 */ "as",
155051  /*  251 */ "seltablist",
155052  /*  252 */ "stl_prefix",
155053  /*  253 */ "joinop",
155054  /*  254 */ "indexed_opt",
155055  /*  255 */ "on_opt",
155056  /*  256 */ "using_opt",
155057  /*  257 */ "exprlist",
155058  /*  258 */ "xfullname",
155059  /*  259 */ "idlist",
155060  /*  260 */ "nulls",
155061  /*  261 */ "with",
155062  /*  262 */ "setlist",
155063  /*  263 */ "insert_cmd",
155064  /*  264 */ "idlist_opt",
155065  /*  265 */ "upsert",
155066  /*  266 */ "filter_over",
155067  /*  267 */ "likeop",
155068  /*  268 */ "between_op",
155069  /*  269 */ "in_op",
155070  /*  270 */ "paren_exprlist",
155071  /*  271 */ "case_operand",
155072  /*  272 */ "case_exprlist",
155073  /*  273 */ "case_else",
155074  /*  274 */ "uniqueflag",
155075  /*  275 */ "collate",
155076  /*  276 */ "vinto",
155077  /*  277 */ "nmnum",
155078  /*  278 */ "trigger_decl",
155079  /*  279 */ "trigger_cmd_list",
155080  /*  280 */ "trigger_time",
155081  /*  281 */ "trigger_event",
155082  /*  282 */ "foreach_clause",
155083  /*  283 */ "when_clause",
155084  /*  284 */ "trigger_cmd",
155085  /*  285 */ "trnm",
155086  /*  286 */ "tridxby",
155087  /*  287 */ "database_kw_opt",
155088  /*  288 */ "key_opt",
155089  /*  289 */ "add_column_fullname",
155090  /*  290 */ "kwcolumn_opt",
155091  /*  291 */ "create_vtab",
155092  /*  292 */ "vtabarglist",
155093  /*  293 */ "vtabarg",
155094  /*  294 */ "vtabargtoken",
155095  /*  295 */ "lp",
155096  /*  296 */ "anylist",
155097  /*  297 */ "windowdefn_list",
155098  /*  298 */ "windowdefn",
155099  /*  299 */ "window",
155100  /*  300 */ "frame_opt",
155101  /*  301 */ "part_opt",
155102  /*  302 */ "filter_clause",
155103  /*  303 */ "over_clause",
155104  /*  304 */ "range_or_rows",
155105  /*  305 */ "frame_bound",
155106  /*  306 */ "frame_bound_s",
155107  /*  307 */ "frame_bound_e",
155108  /*  308 */ "frame_exclude_opt",
155109  /*  309 */ "frame_exclude",
155110};
155111#endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
155112
155113#ifndef NDEBUG
155114/* For tracing reduce actions, the names of all rules are required.
155115*/
155116static const char *const yyRuleName[] = {
155117 /*   0 */ "explain ::= EXPLAIN",
155118 /*   1 */ "explain ::= EXPLAIN QUERY PLAN",
155119 /*   2 */ "cmdx ::= cmd",
155120 /*   3 */ "cmd ::= BEGIN transtype trans_opt",
155121 /*   4 */ "transtype ::=",
155122 /*   5 */ "transtype ::= DEFERRED",
155123 /*   6 */ "transtype ::= IMMEDIATE",
155124 /*   7 */ "transtype ::= EXCLUSIVE",
155125 /*   8 */ "cmd ::= COMMIT|END trans_opt",
155126 /*   9 */ "cmd ::= ROLLBACK trans_opt",
155127 /*  10 */ "cmd ::= SAVEPOINT nm",
155128 /*  11 */ "cmd ::= RELEASE savepoint_opt nm",
155129 /*  12 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
155130 /*  13 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
155131 /*  14 */ "createkw ::= CREATE",
155132 /*  15 */ "ifnotexists ::=",
155133 /*  16 */ "ifnotexists ::= IF NOT EXISTS",
155134 /*  17 */ "temp ::= TEMP",
155135 /*  18 */ "temp ::=",
155136 /*  19 */ "create_table_args ::= LP columnlist conslist_opt RP table_options",
155137 /*  20 */ "create_table_args ::= AS select",
155138 /*  21 */ "table_options ::=",
155139 /*  22 */ "table_options ::= WITHOUT nm",
155140 /*  23 */ "columnname ::= nm typetoken",
155141 /*  24 */ "typetoken ::=",
155142 /*  25 */ "typetoken ::= typename LP signed RP",
155143 /*  26 */ "typetoken ::= typename LP signed COMMA signed RP",
155144 /*  27 */ "typename ::= typename ID|STRING",
155145 /*  28 */ "scanpt ::=",
155146 /*  29 */ "scantok ::=",
155147 /*  30 */ "ccons ::= CONSTRAINT nm",
155148 /*  31 */ "ccons ::= DEFAULT scantok term",
155149 /*  32 */ "ccons ::= DEFAULT LP expr RP",
155150 /*  33 */ "ccons ::= DEFAULT PLUS scantok term",
155151 /*  34 */ "ccons ::= DEFAULT MINUS scantok term",
155152 /*  35 */ "ccons ::= DEFAULT scantok ID|INDEXED",
155153 /*  36 */ "ccons ::= NOT NULL onconf",
155154 /*  37 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
155155 /*  38 */ "ccons ::= UNIQUE onconf",
155156 /*  39 */ "ccons ::= CHECK LP expr RP",
155157 /*  40 */ "ccons ::= REFERENCES nm eidlist_opt refargs",
155158 /*  41 */ "ccons ::= defer_subclause",
155159 /*  42 */ "ccons ::= COLLATE ID|STRING",
155160 /*  43 */ "generated ::= LP expr RP",
155161 /*  44 */ "generated ::= LP expr RP ID",
155162 /*  45 */ "autoinc ::=",
155163 /*  46 */ "autoinc ::= AUTOINCR",
155164 /*  47 */ "refargs ::=",
155165 /*  48 */ "refargs ::= refargs refarg",
155166 /*  49 */ "refarg ::= MATCH nm",
155167 /*  50 */ "refarg ::= ON INSERT refact",
155168 /*  51 */ "refarg ::= ON DELETE refact",
155169 /*  52 */ "refarg ::= ON UPDATE refact",
155170 /*  53 */ "refact ::= SET NULL",
155171 /*  54 */ "refact ::= SET DEFAULT",
155172 /*  55 */ "refact ::= CASCADE",
155173 /*  56 */ "refact ::= RESTRICT",
155174 /*  57 */ "refact ::= NO ACTION",
155175 /*  58 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
155176 /*  59 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
155177 /*  60 */ "init_deferred_pred_opt ::=",
155178 /*  61 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
155179 /*  62 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
155180 /*  63 */ "conslist_opt ::=",
155181 /*  64 */ "tconscomma ::= COMMA",
155182 /*  65 */ "tcons ::= CONSTRAINT nm",
155183 /*  66 */ "tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf",
155184 /*  67 */ "tcons ::= UNIQUE LP sortlist RP onconf",
155185 /*  68 */ "tcons ::= CHECK LP expr RP onconf",
155186 /*  69 */ "tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt",
155187 /*  70 */ "defer_subclause_opt ::=",
155188 /*  71 */ "onconf ::=",
155189 /*  72 */ "onconf ::= ON CONFLICT resolvetype",
155190 /*  73 */ "orconf ::=",
155191 /*  74 */ "orconf ::= OR resolvetype",
155192 /*  75 */ "resolvetype ::= IGNORE",
155193 /*  76 */ "resolvetype ::= REPLACE",
155194 /*  77 */ "cmd ::= DROP TABLE ifexists fullname",
155195 /*  78 */ "ifexists ::= IF EXISTS",
155196 /*  79 */ "ifexists ::=",
155197 /*  80 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select",
155198 /*  81 */ "cmd ::= DROP VIEW ifexists fullname",
155199 /*  82 */ "cmd ::= select",
155200 /*  83 */ "select ::= WITH wqlist selectnowith",
155201 /*  84 */ "select ::= WITH RECURSIVE wqlist selectnowith",
155202 /*  85 */ "select ::= selectnowith",
155203 /*  86 */ "selectnowith ::= selectnowith multiselect_op oneselect",
155204 /*  87 */ "multiselect_op ::= UNION",
155205 /*  88 */ "multiselect_op ::= UNION ALL",
155206 /*  89 */ "multiselect_op ::= EXCEPT|INTERSECT",
155207 /*  90 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
155208 /*  91 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt",
155209 /*  92 */ "values ::= VALUES LP nexprlist RP",
155210 /*  93 */ "values ::= values COMMA LP nexprlist RP",
155211 /*  94 */ "distinct ::= DISTINCT",
155212 /*  95 */ "distinct ::= ALL",
155213 /*  96 */ "distinct ::=",
155214 /*  97 */ "sclp ::=",
155215 /*  98 */ "selcollist ::= sclp scanpt expr scanpt as",
155216 /*  99 */ "selcollist ::= sclp scanpt STAR",
155217 /* 100 */ "selcollist ::= sclp scanpt nm DOT STAR",
155218 /* 101 */ "as ::= AS nm",
155219 /* 102 */ "as ::=",
155220 /* 103 */ "from ::=",
155221 /* 104 */ "from ::= FROM seltablist",
155222 /* 105 */ "stl_prefix ::= seltablist joinop",
155223 /* 106 */ "stl_prefix ::=",
155224 /* 107 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
155225 /* 108 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt",
155226 /* 109 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
155227 /* 110 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
155228 /* 111 */ "dbnm ::=",
155229 /* 112 */ "dbnm ::= DOT nm",
155230 /* 113 */ "fullname ::= nm",
155231 /* 114 */ "fullname ::= nm DOT nm",
155232 /* 115 */ "xfullname ::= nm",
155233 /* 116 */ "xfullname ::= nm DOT nm",
155234 /* 117 */ "xfullname ::= nm DOT nm AS nm",
155235 /* 118 */ "xfullname ::= nm AS nm",
155236 /* 119 */ "joinop ::= COMMA|JOIN",
155237 /* 120 */ "joinop ::= JOIN_KW JOIN",
155238 /* 121 */ "joinop ::= JOIN_KW nm JOIN",
155239 /* 122 */ "joinop ::= JOIN_KW nm nm JOIN",
155240 /* 123 */ "on_opt ::= ON expr",
155241 /* 124 */ "on_opt ::=",
155242 /* 125 */ "indexed_opt ::=",
155243 /* 126 */ "indexed_opt ::= INDEXED BY nm",
155244 /* 127 */ "indexed_opt ::= NOT INDEXED",
155245 /* 128 */ "using_opt ::= USING LP idlist RP",
155246 /* 129 */ "using_opt ::=",
155247 /* 130 */ "orderby_opt ::=",
155248 /* 131 */ "orderby_opt ::= ORDER BY sortlist",
155249 /* 132 */ "sortlist ::= sortlist COMMA expr sortorder nulls",
155250 /* 133 */ "sortlist ::= expr sortorder nulls",
155251 /* 134 */ "sortorder ::= ASC",
155252 /* 135 */ "sortorder ::= DESC",
155253 /* 136 */ "sortorder ::=",
155254 /* 137 */ "nulls ::= NULLS FIRST",
155255 /* 138 */ "nulls ::= NULLS LAST",
155256 /* 139 */ "nulls ::=",
155257 /* 140 */ "groupby_opt ::=",
155258 /* 141 */ "groupby_opt ::= GROUP BY nexprlist",
155259 /* 142 */ "having_opt ::=",
155260 /* 143 */ "having_opt ::= HAVING expr",
155261 /* 144 */ "limit_opt ::=",
155262 /* 145 */ "limit_opt ::= LIMIT expr",
155263 /* 146 */ "limit_opt ::= LIMIT expr OFFSET expr",
155264 /* 147 */ "limit_opt ::= LIMIT expr COMMA expr",
155265 /* 148 */ "cmd ::= with DELETE FROM xfullname indexed_opt where_opt",
155266 /* 149 */ "where_opt ::=",
155267 /* 150 */ "where_opt ::= WHERE expr",
155268 /* 151 */ "cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt",
155269 /* 152 */ "setlist ::= setlist COMMA nm EQ expr",
155270 /* 153 */ "setlist ::= setlist COMMA LP idlist RP EQ expr",
155271 /* 154 */ "setlist ::= nm EQ expr",
155272 /* 155 */ "setlist ::= LP idlist RP EQ expr",
155273 /* 156 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert",
155274 /* 157 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES",
155275 /* 158 */ "upsert ::=",
155276 /* 159 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt",
155277 /* 160 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING",
155278 /* 161 */ "upsert ::= ON CONFLICT DO NOTHING",
155279 /* 162 */ "insert_cmd ::= INSERT orconf",
155280 /* 163 */ "insert_cmd ::= REPLACE",
155281 /* 164 */ "idlist_opt ::=",
155282 /* 165 */ "idlist_opt ::= LP idlist RP",
155283 /* 166 */ "idlist ::= idlist COMMA nm",
155284 /* 167 */ "idlist ::= nm",
155285 /* 168 */ "expr ::= LP expr RP",
155286 /* 169 */ "expr ::= ID|INDEXED",
155287 /* 170 */ "expr ::= JOIN_KW",
155288 /* 171 */ "expr ::= nm DOT nm",
155289 /* 172 */ "expr ::= nm DOT nm DOT nm",
155290 /* 173 */ "term ::= NULL|FLOAT|BLOB",
155291 /* 174 */ "term ::= STRING",
155292 /* 175 */ "term ::= INTEGER",
155293 /* 176 */ "expr ::= VARIABLE",
155294 /* 177 */ "expr ::= expr COLLATE ID|STRING",
155295 /* 178 */ "expr ::= CAST LP expr AS typetoken RP",
155296 /* 179 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
155297 /* 180 */ "expr ::= ID|INDEXED LP STAR RP",
155298 /* 181 */ "expr ::= ID|INDEXED LP distinct exprlist RP filter_over",
155299 /* 182 */ "expr ::= ID|INDEXED LP STAR RP filter_over",
155300 /* 183 */ "term ::= CTIME_KW",
155301 /* 184 */ "expr ::= LP nexprlist COMMA expr RP",
155302 /* 185 */ "expr ::= expr AND expr",
155303 /* 186 */ "expr ::= expr OR expr",
155304 /* 187 */ "expr ::= expr LT|GT|GE|LE expr",
155305 /* 188 */ "expr ::= expr EQ|NE expr",
155306 /* 189 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
155307 /* 190 */ "expr ::= expr PLUS|MINUS expr",
155308 /* 191 */ "expr ::= expr STAR|SLASH|REM expr",
155309 /* 192 */ "expr ::= expr CONCAT expr",
155310 /* 193 */ "likeop ::= NOT LIKE_KW|MATCH",
155311 /* 194 */ "expr ::= expr likeop expr",
155312 /* 195 */ "expr ::= expr likeop expr ESCAPE expr",
155313 /* 196 */ "expr ::= expr ISNULL|NOTNULL",
155314 /* 197 */ "expr ::= expr NOT NULL",
155315 /* 198 */ "expr ::= expr IS expr",
155316 /* 199 */ "expr ::= expr IS NOT expr",
155317 /* 200 */ "expr ::= NOT expr",
155318 /* 201 */ "expr ::= BITNOT expr",
155319 /* 202 */ "expr ::= PLUS|MINUS expr",
155320 /* 203 */ "between_op ::= BETWEEN",
155321 /* 204 */ "between_op ::= NOT BETWEEN",
155322 /* 205 */ "expr ::= expr between_op expr AND expr",
155323 /* 206 */ "in_op ::= IN",
155324 /* 207 */ "in_op ::= NOT IN",
155325 /* 208 */ "expr ::= expr in_op LP exprlist RP",
155326 /* 209 */ "expr ::= LP select RP",
155327 /* 210 */ "expr ::= expr in_op LP select RP",
155328 /* 211 */ "expr ::= expr in_op nm dbnm paren_exprlist",
155329 /* 212 */ "expr ::= EXISTS LP select RP",
155330 /* 213 */ "expr ::= CASE case_operand case_exprlist case_else END",
155331 /* 214 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
155332 /* 215 */ "case_exprlist ::= WHEN expr THEN expr",
155333 /* 216 */ "case_else ::= ELSE expr",
155334 /* 217 */ "case_else ::=",
155335 /* 218 */ "case_operand ::= expr",
155336 /* 219 */ "case_operand ::=",
155337 /* 220 */ "exprlist ::=",
155338 /* 221 */ "nexprlist ::= nexprlist COMMA expr",
155339 /* 222 */ "nexprlist ::= expr",
155340 /* 223 */ "paren_exprlist ::=",
155341 /* 224 */ "paren_exprlist ::= LP exprlist RP",
155342 /* 225 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
155343 /* 226 */ "uniqueflag ::= UNIQUE",
155344 /* 227 */ "uniqueflag ::=",
155345 /* 228 */ "eidlist_opt ::=",
155346 /* 229 */ "eidlist_opt ::= LP eidlist RP",
155347 /* 230 */ "eidlist ::= eidlist COMMA nm collate sortorder",
155348 /* 231 */ "eidlist ::= nm collate sortorder",
155349 /* 232 */ "collate ::=",
155350 /* 233 */ "collate ::= COLLATE ID|STRING",
155351 /* 234 */ "cmd ::= DROP INDEX ifexists fullname",
155352 /* 235 */ "cmd ::= VACUUM vinto",
155353 /* 236 */ "cmd ::= VACUUM nm vinto",
155354 /* 237 */ "vinto ::= INTO expr",
155355 /* 238 */ "vinto ::=",
155356 /* 239 */ "cmd ::= PRAGMA nm dbnm",
155357 /* 240 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
155358 /* 241 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
155359 /* 242 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
155360 /* 243 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
155361 /* 244 */ "plus_num ::= PLUS INTEGER|FLOAT",
155362 /* 245 */ "minus_num ::= MINUS INTEGER|FLOAT",
155363 /* 246 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
155364 /* 247 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
155365 /* 248 */ "trigger_time ::= BEFORE|AFTER",
155366 /* 249 */ "trigger_time ::= INSTEAD OF",
155367 /* 250 */ "trigger_time ::=",
155368 /* 251 */ "trigger_event ::= DELETE|INSERT",
155369 /* 252 */ "trigger_event ::= UPDATE",
155370 /* 253 */ "trigger_event ::= UPDATE OF idlist",
155371 /* 254 */ "when_clause ::=",
155372 /* 255 */ "when_clause ::= WHEN expr",
155373 /* 256 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
155374 /* 257 */ "trigger_cmd_list ::= trigger_cmd SEMI",
155375 /* 258 */ "trnm ::= nm DOT nm",
155376 /* 259 */ "tridxby ::= INDEXED BY nm",
155377 /* 260 */ "tridxby ::= NOT INDEXED",
155378 /* 261 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt",
155379 /* 262 */ "trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt",
155380 /* 263 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt",
155381 /* 264 */ "trigger_cmd ::= scanpt select scanpt",
155382 /* 265 */ "expr ::= RAISE LP IGNORE RP",
155383 /* 266 */ "expr ::= RAISE LP raisetype COMMA nm RP",
155384 /* 267 */ "raisetype ::= ROLLBACK",
155385 /* 268 */ "raisetype ::= ABORT",
155386 /* 269 */ "raisetype ::= FAIL",
155387 /* 270 */ "cmd ::= DROP TRIGGER ifexists fullname",
155388 /* 271 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
155389 /* 272 */ "cmd ::= DETACH database_kw_opt expr",
155390 /* 273 */ "key_opt ::=",
155391 /* 274 */ "key_opt ::= KEY expr",
155392 /* 275 */ "cmd ::= REINDEX",
155393 /* 276 */ "cmd ::= REINDEX nm dbnm",
155394 /* 277 */ "cmd ::= ANALYZE",
155395 /* 278 */ "cmd ::= ANALYZE nm dbnm",
155396 /* 279 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
155397 /* 280 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist",
155398 /* 281 */ "add_column_fullname ::= fullname",
155399 /* 282 */ "cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm",
155400 /* 283 */ "cmd ::= create_vtab",
155401 /* 284 */ "cmd ::= create_vtab LP vtabarglist RP",
155402 /* 285 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
155403 /* 286 */ "vtabarg ::=",
155404 /* 287 */ "vtabargtoken ::= ANY",
155405 /* 288 */ "vtabargtoken ::= lp anylist RP",
155406 /* 289 */ "lp ::= LP",
155407 /* 290 */ "with ::= WITH wqlist",
155408 /* 291 */ "with ::= WITH RECURSIVE wqlist",
155409 /* 292 */ "wqlist ::= nm eidlist_opt AS LP select RP",
155410 /* 293 */ "wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP",
155411 /* 294 */ "windowdefn_list ::= windowdefn",
155412 /* 295 */ "windowdefn_list ::= windowdefn_list COMMA windowdefn",
155413 /* 296 */ "windowdefn ::= nm AS LP window RP",
155414 /* 297 */ "window ::= PARTITION BY nexprlist orderby_opt frame_opt",
155415 /* 298 */ "window ::= nm PARTITION BY nexprlist orderby_opt frame_opt",
155416 /* 299 */ "window ::= ORDER BY sortlist frame_opt",
155417 /* 300 */ "window ::= nm ORDER BY sortlist frame_opt",
155418 /* 301 */ "window ::= frame_opt",
155419 /* 302 */ "window ::= nm frame_opt",
155420 /* 303 */ "frame_opt ::=",
155421 /* 304 */ "frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt",
155422 /* 305 */ "frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt",
155423 /* 306 */ "range_or_rows ::= RANGE|ROWS|GROUPS",
155424 /* 307 */ "frame_bound_s ::= frame_bound",
155425 /* 308 */ "frame_bound_s ::= UNBOUNDED PRECEDING",
155426 /* 309 */ "frame_bound_e ::= frame_bound",
155427 /* 310 */ "frame_bound_e ::= UNBOUNDED FOLLOWING",
155428 /* 311 */ "frame_bound ::= expr PRECEDING|FOLLOWING",
155429 /* 312 */ "frame_bound ::= CURRENT ROW",
155430 /* 313 */ "frame_exclude_opt ::=",
155431 /* 314 */ "frame_exclude_opt ::= EXCLUDE frame_exclude",
155432 /* 315 */ "frame_exclude ::= NO OTHERS",
155433 /* 316 */ "frame_exclude ::= CURRENT ROW",
155434 /* 317 */ "frame_exclude ::= GROUP|TIES",
155435 /* 318 */ "window_clause ::= WINDOW windowdefn_list",
155436 /* 319 */ "filter_over ::= filter_clause over_clause",
155437 /* 320 */ "filter_over ::= over_clause",
155438 /* 321 */ "filter_over ::= filter_clause",
155439 /* 322 */ "over_clause ::= OVER LP window RP",
155440 /* 323 */ "over_clause ::= OVER nm",
155441 /* 324 */ "filter_clause ::= FILTER LP WHERE expr RP",
155442 /* 325 */ "input ::= cmdlist",
155443 /* 326 */ "cmdlist ::= cmdlist ecmd",
155444 /* 327 */ "cmdlist ::= ecmd",
155445 /* 328 */ "ecmd ::= SEMI",
155446 /* 329 */ "ecmd ::= cmdx SEMI",
155447 /* 330 */ "ecmd ::= explain cmdx SEMI",
155448 /* 331 */ "trans_opt ::=",
155449 /* 332 */ "trans_opt ::= TRANSACTION",
155450 /* 333 */ "trans_opt ::= TRANSACTION nm",
155451 /* 334 */ "savepoint_opt ::= SAVEPOINT",
155452 /* 335 */ "savepoint_opt ::=",
155453 /* 336 */ "cmd ::= create_table create_table_args",
155454 /* 337 */ "columnlist ::= columnlist COMMA columnname carglist",
155455 /* 338 */ "columnlist ::= columnname carglist",
155456 /* 339 */ "nm ::= ID|INDEXED",
155457 /* 340 */ "nm ::= STRING",
155458 /* 341 */ "nm ::= JOIN_KW",
155459 /* 342 */ "typetoken ::= typename",
155460 /* 343 */ "typename ::= ID|STRING",
155461 /* 344 */ "signed ::= plus_num",
155462 /* 345 */ "signed ::= minus_num",
155463 /* 346 */ "carglist ::= carglist ccons",
155464 /* 347 */ "carglist ::=",
155465 /* 348 */ "ccons ::= NULL onconf",
155466 /* 349 */ "ccons ::= GENERATED ALWAYS AS generated",
155467 /* 350 */ "ccons ::= AS generated",
155468 /* 351 */ "conslist_opt ::= COMMA conslist",
155469 /* 352 */ "conslist ::= conslist tconscomma tcons",
155470 /* 353 */ "conslist ::= tcons",
155471 /* 354 */ "tconscomma ::=",
155472 /* 355 */ "defer_subclause_opt ::= defer_subclause",
155473 /* 356 */ "resolvetype ::= raisetype",
155474 /* 357 */ "selectnowith ::= oneselect",
155475 /* 358 */ "oneselect ::= values",
155476 /* 359 */ "sclp ::= selcollist COMMA",
155477 /* 360 */ "as ::= ID|STRING",
155478 /* 361 */ "expr ::= term",
155479 /* 362 */ "likeop ::= LIKE_KW|MATCH",
155480 /* 363 */ "exprlist ::= nexprlist",
155481 /* 364 */ "nmnum ::= plus_num",
155482 /* 365 */ "nmnum ::= nm",
155483 /* 366 */ "nmnum ::= ON",
155484 /* 367 */ "nmnum ::= DELETE",
155485 /* 368 */ "nmnum ::= DEFAULT",
155486 /* 369 */ "plus_num ::= INTEGER|FLOAT",
155487 /* 370 */ "foreach_clause ::=",
155488 /* 371 */ "foreach_clause ::= FOR EACH ROW",
155489 /* 372 */ "trnm ::= nm",
155490 /* 373 */ "tridxby ::=",
155491 /* 374 */ "database_kw_opt ::= DATABASE",
155492 /* 375 */ "database_kw_opt ::=",
155493 /* 376 */ "kwcolumn_opt ::=",
155494 /* 377 */ "kwcolumn_opt ::= COLUMNKW",
155495 /* 378 */ "vtabarglist ::= vtabarg",
155496 /* 379 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
155497 /* 380 */ "vtabarg ::= vtabarg vtabargtoken",
155498 /* 381 */ "anylist ::=",
155499 /* 382 */ "anylist ::= anylist LP anylist RP",
155500 /* 383 */ "anylist ::= anylist ANY",
155501 /* 384 */ "with ::=",
155502};
155503#endif /* NDEBUG */
155504
155505
155506#if YYSTACKDEPTH<=0
155507/*
155508** Try to increase the size of the parser stack.  Return the number
155509** of errors.  Return 0 on success.
155510*/
155511static int yyGrowStack(yyParser *p){
155512  int newSize;
155513  int idx;
155514  yyStackEntry *pNew;
155515
155516  newSize = p->yystksz*2 + 100;
155517  idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
155518  if( p->yystack==&p->yystk0 ){
155519    pNew = malloc(newSize*sizeof(pNew[0]));
155520    if( pNew ) pNew[0] = p->yystk0;
155521  }else{
155522    pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
155523  }
155524  if( pNew ){
155525    p->yystack = pNew;
155526    p->yytos = &p->yystack[idx];
155527#ifndef NDEBUG
155528    if( yyTraceFILE ){
155529      fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n",
155530              yyTracePrompt, p->yystksz, newSize);
155531    }
155532#endif
155533    p->yystksz = newSize;
155534  }
155535  return pNew==0;
155536}
155537#endif
155538
155539/* Datatype of the argument to the memory allocated passed as the
155540** second argument to sqlite3ParserAlloc() below.  This can be changed by
155541** putting an appropriate #define in the %include section of the input
155542** grammar.
155543*/
155544#ifndef YYMALLOCARGTYPE
155545# define YYMALLOCARGTYPE size_t
155546#endif
155547
155548/* Initialize a new parser that has already been allocated.
155549*/
155550SQLITE_PRIVATE void sqlite3ParserInit(void *yypRawParser sqlite3ParserCTX_PDECL){
155551  yyParser *yypParser = (yyParser*)yypRawParser;
155552  sqlite3ParserCTX_STORE
155553#ifdef YYTRACKMAXSTACKDEPTH
155554  yypParser->yyhwm = 0;
155555#endif
155556#if YYSTACKDEPTH<=0
155557  yypParser->yytos = NULL;
155558  yypParser->yystack = NULL;
155559  yypParser->yystksz = 0;
155560  if( yyGrowStack(yypParser) ){
155561    yypParser->yystack = &yypParser->yystk0;
155562    yypParser->yystksz = 1;
155563  }
155564#endif
155565#ifndef YYNOERRORRECOVERY
155566  yypParser->yyerrcnt = -1;
155567#endif
155568  yypParser->yytos = yypParser->yystack;
155569  yypParser->yystack[0].stateno = 0;
155570  yypParser->yystack[0].major = 0;
155571#if YYSTACKDEPTH>0
155572  yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
155573#endif
155574}
155575
155576#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
155577/*
155578** This function allocates a new parser.
155579** The only argument is a pointer to a function which works like
155580** malloc.
155581**
155582** Inputs:
155583** A pointer to the function used to allocate memory.
155584**
155585** Outputs:
155586** A pointer to a parser.  This pointer is used in subsequent calls
155587** to sqlite3Parser and sqlite3ParserFree.
155588*/
155589SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE) sqlite3ParserCTX_PDECL){
155590  yyParser *yypParser;
155591  yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
155592  if( yypParser ){
155593    sqlite3ParserCTX_STORE
155594    sqlite3ParserInit(yypParser sqlite3ParserCTX_PARAM);
155595  }
155596  return (void*)yypParser;
155597}
155598#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
155599
155600
155601/* The following function deletes the "minor type" or semantic value
155602** associated with a symbol.  The symbol can be either a terminal
155603** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
155604** a pointer to the value to be deleted.  The code used to do the
155605** deletions is derived from the %destructor and/or %token_destructor
155606** directives of the input grammar.
155607*/
155608static void yy_destructor(
155609  yyParser *yypParser,    /* The parser */
155610  YYCODETYPE yymajor,     /* Type code for object to destroy */
155611  YYMINORTYPE *yypminor   /* The object to be destroyed */
155612){
155613  sqlite3ParserARG_FETCH
155614  sqlite3ParserCTX_FETCH
155615  switch( yymajor ){
155616    /* Here is inserted the actions which take place when a
155617    ** terminal or non-terminal is destroyed.  This can happen
155618    ** when the symbol is popped from the stack during a
155619    ** reduce or during error processing or when a parser is
155620    ** being destroyed before it is finished parsing.
155621    **
155622    ** Note: during a reduce, the only symbols destroyed are those
155623    ** which appear on the RHS of the rule, but which are *not* used
155624    ** inside the C code.
155625    */
155626/********* Begin destructor definitions ***************************************/
155627    case 200: /* select */
155628    case 234: /* selectnowith */
155629    case 235: /* oneselect */
155630    case 247: /* values */
155631{
155632sqlite3SelectDelete(pParse->db, (yypminor->yy539));
155633}
155634      break;
155635    case 211: /* term */
155636    case 212: /* expr */
155637    case 241: /* where_opt */
155638    case 243: /* having_opt */
155639    case 255: /* on_opt */
155640    case 271: /* case_operand */
155641    case 273: /* case_else */
155642    case 276: /* vinto */
155643    case 283: /* when_clause */
155644    case 288: /* key_opt */
155645    case 302: /* filter_clause */
155646{
155647sqlite3ExprDelete(pParse->db, (yypminor->yy202));
155648}
155649      break;
155650    case 216: /* eidlist_opt */
155651    case 226: /* sortlist */
155652    case 227: /* eidlist */
155653    case 239: /* selcollist */
155654    case 242: /* groupby_opt */
155655    case 244: /* orderby_opt */
155656    case 248: /* nexprlist */
155657    case 249: /* sclp */
155658    case 257: /* exprlist */
155659    case 262: /* setlist */
155660    case 270: /* paren_exprlist */
155661    case 272: /* case_exprlist */
155662    case 301: /* part_opt */
155663{
155664sqlite3ExprListDelete(pParse->db, (yypminor->yy242));
155665}
155666      break;
155667    case 233: /* fullname */
155668    case 240: /* from */
155669    case 251: /* seltablist */
155670    case 252: /* stl_prefix */
155671    case 258: /* xfullname */
155672{
155673sqlite3SrcListDelete(pParse->db, (yypminor->yy47));
155674}
155675      break;
155676    case 236: /* wqlist */
155677{
155678sqlite3WithDelete(pParse->db, (yypminor->yy131));
155679}
155680      break;
155681    case 246: /* window_clause */
155682    case 297: /* windowdefn_list */
155683{
155684sqlite3WindowListDelete(pParse->db, (yypminor->yy303));
155685}
155686      break;
155687    case 256: /* using_opt */
155688    case 259: /* idlist */
155689    case 264: /* idlist_opt */
155690{
155691sqlite3IdListDelete(pParse->db, (yypminor->yy600));
155692}
155693      break;
155694    case 266: /* filter_over */
155695    case 298: /* windowdefn */
155696    case 299: /* window */
155697    case 300: /* frame_opt */
155698    case 303: /* over_clause */
155699{
155700sqlite3WindowDelete(pParse->db, (yypminor->yy303));
155701}
155702      break;
155703    case 279: /* trigger_cmd_list */
155704    case 284: /* trigger_cmd */
155705{
155706sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy447));
155707}
155708      break;
155709    case 281: /* trigger_event */
155710{
155711sqlite3IdListDelete(pParse->db, (yypminor->yy230).b);
155712}
155713      break;
155714    case 305: /* frame_bound */
155715    case 306: /* frame_bound_s */
155716    case 307: /* frame_bound_e */
155717{
155718sqlite3ExprDelete(pParse->db, (yypminor->yy77).pExpr);
155719}
155720      break;
155721/********* End destructor definitions *****************************************/
155722    default:  break;   /* If no destructor action specified: do nothing */
155723  }
155724}
155725
155726/*
155727** Pop the parser's stack once.
155728**
155729** If there is a destructor routine associated with the token which
155730** is popped from the stack, then call it.
155731*/
155732static void yy_pop_parser_stack(yyParser *pParser){
155733  yyStackEntry *yytos;
155734  assert( pParser->yytos!=0 );
155735  assert( pParser->yytos > pParser->yystack );
155736  yytos = pParser->yytos--;
155737#ifndef NDEBUG
155738  if( yyTraceFILE ){
155739    fprintf(yyTraceFILE,"%sPopping %s\n",
155740      yyTracePrompt,
155741      yyTokenName[yytos->major]);
155742  }
155743#endif
155744  yy_destructor(pParser, yytos->major, &yytos->minor);
155745}
155746
155747/*
155748** Clear all secondary memory allocations from the parser
155749*/
155750SQLITE_PRIVATE void sqlite3ParserFinalize(void *p){
155751  yyParser *pParser = (yyParser*)p;
155752  while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
155753#if YYSTACKDEPTH<=0
155754  if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
155755#endif
155756}
155757
155758#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
155759/*
155760** Deallocate and destroy a parser.  Destructors are called for
155761** all stack elements before shutting the parser down.
155762**
155763** If the YYPARSEFREENEVERNULL macro exists (for example because it
155764** is defined in a %include section of the input grammar) then it is
155765** assumed that the input pointer is never NULL.
155766*/
155767SQLITE_PRIVATE void sqlite3ParserFree(
155768  void *p,                    /* The parser to be deleted */
155769  void (*freeProc)(void*)     /* Function used to reclaim memory */
155770){
155771#ifndef YYPARSEFREENEVERNULL
155772  if( p==0 ) return;
155773#endif
155774  sqlite3ParserFinalize(p);
155775  (*freeProc)(p);
155776}
155777#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
155778
155779/*
155780** Return the peak depth of the stack for a parser.
155781*/
155782#ifdef YYTRACKMAXSTACKDEPTH
155783SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
155784  yyParser *pParser = (yyParser*)p;
155785  return pParser->yyhwm;
155786}
155787#endif
155788
155789/* This array of booleans keeps track of the parser statement
155790** coverage.  The element yycoverage[X][Y] is set when the parser
155791** is in state X and has a lookahead token Y.  In a well-tested
155792** systems, every element of this matrix should end up being set.
155793*/
155794#if defined(YYCOVERAGE)
155795static unsigned char yycoverage[YYNSTATE][YYNTOKEN];
155796#endif
155797
155798/*
155799** Write into out a description of every state/lookahead combination that
155800**
155801**   (1)  has not been used by the parser, and
155802**   (2)  is not a syntax error.
155803**
155804** Return the number of missed state/lookahead combinations.
155805*/
155806#if defined(YYCOVERAGE)
155807SQLITE_PRIVATE int sqlite3ParserCoverage(FILE *out){
155808  int stateno, iLookAhead, i;
155809  int nMissed = 0;
155810  for(stateno=0; stateno<YYNSTATE; stateno++){
155811    i = yy_shift_ofst[stateno];
155812    for(iLookAhead=0; iLookAhead<YYNTOKEN; iLookAhead++){
155813      if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
155814      if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
155815      if( out ){
155816        fprintf(out,"State %d lookahead %s %s\n", stateno,
155817                yyTokenName[iLookAhead],
155818                yycoverage[stateno][iLookAhead] ? "ok" : "missed");
155819      }
155820    }
155821  }
155822  return nMissed;
155823}
155824#endif
155825
155826/*
155827** Find the appropriate action for a parser given the terminal
155828** look-ahead token iLookAhead.
155829*/
155830static YYACTIONTYPE yy_find_shift_action(
155831  YYCODETYPE iLookAhead,    /* The look-ahead token */
155832  YYACTIONTYPE stateno      /* Current state number */
155833){
155834  int i;
155835
155836  if( stateno>YY_MAX_SHIFT ) return stateno;
155837  assert( stateno <= YY_SHIFT_COUNT );
155838#if defined(YYCOVERAGE)
155839  yycoverage[stateno][iLookAhead] = 1;
155840#endif
155841  do{
155842    i = yy_shift_ofst[stateno];
155843    assert( i>=0 );
155844    assert( i<=YY_ACTTAB_COUNT );
155845    assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD );
155846    assert( iLookAhead!=YYNOCODE );
155847    assert( iLookAhead < YYNTOKEN );
155848    i += iLookAhead;
155849    assert( i<(int)YY_NLOOKAHEAD );
155850    if( yy_lookahead[i]!=iLookAhead ){
155851#ifdef YYFALLBACK
155852      YYCODETYPE iFallback;            /* Fallback token */
155853      assert( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) );
155854      iFallback = yyFallback[iLookAhead];
155855      if( iFallback!=0 ){
155856#ifndef NDEBUG
155857        if( yyTraceFILE ){
155858          fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
155859             yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
155860        }
155861#endif
155862        assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
155863        iLookAhead = iFallback;
155864        continue;
155865      }
155866#endif
155867#ifdef YYWILDCARD
155868      {
155869        int j = i - iLookAhead + YYWILDCARD;
155870        assert( j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) );
155871        if( yy_lookahead[j]==YYWILDCARD && iLookAhead>0 ){
155872#ifndef NDEBUG
155873          if( yyTraceFILE ){
155874            fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
155875               yyTracePrompt, yyTokenName[iLookAhead],
155876               yyTokenName[YYWILDCARD]);
155877          }
155878#endif /* NDEBUG */
155879          return yy_action[j];
155880        }
155881      }
155882#endif /* YYWILDCARD */
155883      return yy_default[stateno];
155884    }else{
155885      assert( i>=0 && i<sizeof(yy_action)/sizeof(yy_action[0]) );
155886      return yy_action[i];
155887    }
155888  }while(1);
155889}
155890
155891/*
155892** Find the appropriate action for a parser given the non-terminal
155893** look-ahead token iLookAhead.
155894*/
155895static YYACTIONTYPE yy_find_reduce_action(
155896  YYACTIONTYPE stateno,     /* Current state number */
155897  YYCODETYPE iLookAhead     /* The look-ahead token */
155898){
155899  int i;
155900#ifdef YYERRORSYMBOL
155901  if( stateno>YY_REDUCE_COUNT ){
155902    return yy_default[stateno];
155903  }
155904#else
155905  assert( stateno<=YY_REDUCE_COUNT );
155906#endif
155907  i = yy_reduce_ofst[stateno];
155908  assert( iLookAhead!=YYNOCODE );
155909  i += iLookAhead;
155910#ifdef YYERRORSYMBOL
155911  if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
155912    return yy_default[stateno];
155913  }
155914#else
155915  assert( i>=0 && i<YY_ACTTAB_COUNT );
155916  assert( yy_lookahead[i]==iLookAhead );
155917#endif
155918  return yy_action[i];
155919}
155920
155921/*
155922** The following routine is called if the stack overflows.
155923*/
155924static void yyStackOverflow(yyParser *yypParser){
155925   sqlite3ParserARG_FETCH
155926   sqlite3ParserCTX_FETCH
155927#ifndef NDEBUG
155928   if( yyTraceFILE ){
155929     fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
155930   }
155931#endif
155932   while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
155933   /* Here code is inserted which will execute if the parser
155934   ** stack every overflows */
155935/******** Begin %stack_overflow code ******************************************/
155936
155937  sqlite3ErrorMsg(pParse, "parser stack overflow");
155938/******** End %stack_overflow code ********************************************/
155939   sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument var */
155940   sqlite3ParserCTX_STORE
155941}
155942
155943/*
155944** Print tracing information for a SHIFT action
155945*/
155946#ifndef NDEBUG
155947static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
155948  if( yyTraceFILE ){
155949    if( yyNewState<YYNSTATE ){
155950      fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
155951         yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
155952         yyNewState);
155953    }else{
155954      fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
155955         yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
155956         yyNewState - YY_MIN_REDUCE);
155957    }
155958  }
155959}
155960#else
155961# define yyTraceShift(X,Y,Z)
155962#endif
155963
155964/*
155965** Perform a shift action.
155966*/
155967static void yy_shift(
155968  yyParser *yypParser,          /* The parser to be shifted */
155969  YYACTIONTYPE yyNewState,      /* The new state to shift in */
155970  YYCODETYPE yyMajor,           /* The major token to shift in */
155971  sqlite3ParserTOKENTYPE yyMinor        /* The minor token to shift in */
155972){
155973  yyStackEntry *yytos;
155974  yypParser->yytos++;
155975#ifdef YYTRACKMAXSTACKDEPTH
155976  if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
155977    yypParser->yyhwm++;
155978    assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
155979  }
155980#endif
155981#if YYSTACKDEPTH>0
155982  if( yypParser->yytos>yypParser->yystackEnd ){
155983    yypParser->yytos--;
155984    yyStackOverflow(yypParser);
155985    return;
155986  }
155987#else
155988  if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
155989    if( yyGrowStack(yypParser) ){
155990      yypParser->yytos--;
155991      yyStackOverflow(yypParser);
155992      return;
155993    }
155994  }
155995#endif
155996  if( yyNewState > YY_MAX_SHIFT ){
155997    yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
155998  }
155999  yytos = yypParser->yytos;
156000  yytos->stateno = yyNewState;
156001  yytos->major = yyMajor;
156002  yytos->minor.yy0 = yyMinor;
156003  yyTraceShift(yypParser, yyNewState, "Shift");
156004}
156005
156006/* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
156007** of that rule */
156008static const YYCODETYPE yyRuleInfoLhs[] = {
156009   185,  /* (0) explain ::= EXPLAIN */
156010   185,  /* (1) explain ::= EXPLAIN QUERY PLAN */
156011   184,  /* (2) cmdx ::= cmd */
156012   186,  /* (3) cmd ::= BEGIN transtype trans_opt */
156013   187,  /* (4) transtype ::= */
156014   187,  /* (5) transtype ::= DEFERRED */
156015   187,  /* (6) transtype ::= IMMEDIATE */
156016   187,  /* (7) transtype ::= EXCLUSIVE */
156017   186,  /* (8) cmd ::= COMMIT|END trans_opt */
156018   186,  /* (9) cmd ::= ROLLBACK trans_opt */
156019   186,  /* (10) cmd ::= SAVEPOINT nm */
156020   186,  /* (11) cmd ::= RELEASE savepoint_opt nm */
156021   186,  /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
156022   191,  /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
156023   193,  /* (14) createkw ::= CREATE */
156024   195,  /* (15) ifnotexists ::= */
156025   195,  /* (16) ifnotexists ::= IF NOT EXISTS */
156026   194,  /* (17) temp ::= TEMP */
156027   194,  /* (18) temp ::= */
156028   192,  /* (19) create_table_args ::= LP columnlist conslist_opt RP table_options */
156029   192,  /* (20) create_table_args ::= AS select */
156030   199,  /* (21) table_options ::= */
156031   199,  /* (22) table_options ::= WITHOUT nm */
156032   201,  /* (23) columnname ::= nm typetoken */
156033   203,  /* (24) typetoken ::= */
156034   203,  /* (25) typetoken ::= typename LP signed RP */
156035   203,  /* (26) typetoken ::= typename LP signed COMMA signed RP */
156036   204,  /* (27) typename ::= typename ID|STRING */
156037   208,  /* (28) scanpt ::= */
156038   209,  /* (29) scantok ::= */
156039   210,  /* (30) ccons ::= CONSTRAINT nm */
156040   210,  /* (31) ccons ::= DEFAULT scantok term */
156041   210,  /* (32) ccons ::= DEFAULT LP expr RP */
156042   210,  /* (33) ccons ::= DEFAULT PLUS scantok term */
156043   210,  /* (34) ccons ::= DEFAULT MINUS scantok term */
156044   210,  /* (35) ccons ::= DEFAULT scantok ID|INDEXED */
156045   210,  /* (36) ccons ::= NOT NULL onconf */
156046   210,  /* (37) ccons ::= PRIMARY KEY sortorder onconf autoinc */
156047   210,  /* (38) ccons ::= UNIQUE onconf */
156048   210,  /* (39) ccons ::= CHECK LP expr RP */
156049   210,  /* (40) ccons ::= REFERENCES nm eidlist_opt refargs */
156050   210,  /* (41) ccons ::= defer_subclause */
156051   210,  /* (42) ccons ::= COLLATE ID|STRING */
156052   219,  /* (43) generated ::= LP expr RP */
156053   219,  /* (44) generated ::= LP expr RP ID */
156054   215,  /* (45) autoinc ::= */
156055   215,  /* (46) autoinc ::= AUTOINCR */
156056   217,  /* (47) refargs ::= */
156057   217,  /* (48) refargs ::= refargs refarg */
156058   220,  /* (49) refarg ::= MATCH nm */
156059   220,  /* (50) refarg ::= ON INSERT refact */
156060   220,  /* (51) refarg ::= ON DELETE refact */
156061   220,  /* (52) refarg ::= ON UPDATE refact */
156062   221,  /* (53) refact ::= SET NULL */
156063   221,  /* (54) refact ::= SET DEFAULT */
156064   221,  /* (55) refact ::= CASCADE */
156065   221,  /* (56) refact ::= RESTRICT */
156066   221,  /* (57) refact ::= NO ACTION */
156067   218,  /* (58) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
156068   218,  /* (59) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
156069   222,  /* (60) init_deferred_pred_opt ::= */
156070   222,  /* (61) init_deferred_pred_opt ::= INITIALLY DEFERRED */
156071   222,  /* (62) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
156072   198,  /* (63) conslist_opt ::= */
156073   224,  /* (64) tconscomma ::= COMMA */
156074   225,  /* (65) tcons ::= CONSTRAINT nm */
156075   225,  /* (66) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
156076   225,  /* (67) tcons ::= UNIQUE LP sortlist RP onconf */
156077   225,  /* (68) tcons ::= CHECK LP expr RP onconf */
156078   225,  /* (69) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
156079   228,  /* (70) defer_subclause_opt ::= */
156080   213,  /* (71) onconf ::= */
156081   213,  /* (72) onconf ::= ON CONFLICT resolvetype */
156082   229,  /* (73) orconf ::= */
156083   229,  /* (74) orconf ::= OR resolvetype */
156084   230,  /* (75) resolvetype ::= IGNORE */
156085   230,  /* (76) resolvetype ::= REPLACE */
156086   186,  /* (77) cmd ::= DROP TABLE ifexists fullname */
156087   232,  /* (78) ifexists ::= IF EXISTS */
156088   232,  /* (79) ifexists ::= */
156089   186,  /* (80) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
156090   186,  /* (81) cmd ::= DROP VIEW ifexists fullname */
156091   186,  /* (82) cmd ::= select */
156092   200,  /* (83) select ::= WITH wqlist selectnowith */
156093   200,  /* (84) select ::= WITH RECURSIVE wqlist selectnowith */
156094   200,  /* (85) select ::= selectnowith */
156095   234,  /* (86) selectnowith ::= selectnowith multiselect_op oneselect */
156096   237,  /* (87) multiselect_op ::= UNION */
156097   237,  /* (88) multiselect_op ::= UNION ALL */
156098   237,  /* (89) multiselect_op ::= EXCEPT|INTERSECT */
156099   235,  /* (90) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
156100   235,  /* (91) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
156101   247,  /* (92) values ::= VALUES LP nexprlist RP */
156102   247,  /* (93) values ::= values COMMA LP nexprlist RP */
156103   238,  /* (94) distinct ::= DISTINCT */
156104   238,  /* (95) distinct ::= ALL */
156105   238,  /* (96) distinct ::= */
156106   249,  /* (97) sclp ::= */
156107   239,  /* (98) selcollist ::= sclp scanpt expr scanpt as */
156108   239,  /* (99) selcollist ::= sclp scanpt STAR */
156109   239,  /* (100) selcollist ::= sclp scanpt nm DOT STAR */
156110   250,  /* (101) as ::= AS nm */
156111   250,  /* (102) as ::= */
156112   240,  /* (103) from ::= */
156113   240,  /* (104) from ::= FROM seltablist */
156114   252,  /* (105) stl_prefix ::= seltablist joinop */
156115   252,  /* (106) stl_prefix ::= */
156116   251,  /* (107) seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
156117   251,  /* (108) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
156118   251,  /* (109) seltablist ::= stl_prefix LP select RP as on_opt using_opt */
156119   251,  /* (110) seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
156120   196,  /* (111) dbnm ::= */
156121   196,  /* (112) dbnm ::= DOT nm */
156122   233,  /* (113) fullname ::= nm */
156123   233,  /* (114) fullname ::= nm DOT nm */
156124   258,  /* (115) xfullname ::= nm */
156125   258,  /* (116) xfullname ::= nm DOT nm */
156126   258,  /* (117) xfullname ::= nm DOT nm AS nm */
156127   258,  /* (118) xfullname ::= nm AS nm */
156128   253,  /* (119) joinop ::= COMMA|JOIN */
156129   253,  /* (120) joinop ::= JOIN_KW JOIN */
156130   253,  /* (121) joinop ::= JOIN_KW nm JOIN */
156131   253,  /* (122) joinop ::= JOIN_KW nm nm JOIN */
156132   255,  /* (123) on_opt ::= ON expr */
156133   255,  /* (124) on_opt ::= */
156134   254,  /* (125) indexed_opt ::= */
156135   254,  /* (126) indexed_opt ::= INDEXED BY nm */
156136   254,  /* (127) indexed_opt ::= NOT INDEXED */
156137   256,  /* (128) using_opt ::= USING LP idlist RP */
156138   256,  /* (129) using_opt ::= */
156139   244,  /* (130) orderby_opt ::= */
156140   244,  /* (131) orderby_opt ::= ORDER BY sortlist */
156141   226,  /* (132) sortlist ::= sortlist COMMA expr sortorder nulls */
156142   226,  /* (133) sortlist ::= expr sortorder nulls */
156143   214,  /* (134) sortorder ::= ASC */
156144   214,  /* (135) sortorder ::= DESC */
156145   214,  /* (136) sortorder ::= */
156146   260,  /* (137) nulls ::= NULLS FIRST */
156147   260,  /* (138) nulls ::= NULLS LAST */
156148   260,  /* (139) nulls ::= */
156149   242,  /* (140) groupby_opt ::= */
156150   242,  /* (141) groupby_opt ::= GROUP BY nexprlist */
156151   243,  /* (142) having_opt ::= */
156152   243,  /* (143) having_opt ::= HAVING expr */
156153   245,  /* (144) limit_opt ::= */
156154   245,  /* (145) limit_opt ::= LIMIT expr */
156155   245,  /* (146) limit_opt ::= LIMIT expr OFFSET expr */
156156   245,  /* (147) limit_opt ::= LIMIT expr COMMA expr */
156157   186,  /* (148) cmd ::= with DELETE FROM xfullname indexed_opt where_opt */
156158   241,  /* (149) where_opt ::= */
156159   241,  /* (150) where_opt ::= WHERE expr */
156160   186,  /* (151) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt */
156161   262,  /* (152) setlist ::= setlist COMMA nm EQ expr */
156162   262,  /* (153) setlist ::= setlist COMMA LP idlist RP EQ expr */
156163   262,  /* (154) setlist ::= nm EQ expr */
156164   262,  /* (155) setlist ::= LP idlist RP EQ expr */
156165   186,  /* (156) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
156166   186,  /* (157) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES */
156167   265,  /* (158) upsert ::= */
156168   265,  /* (159) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt */
156169   265,  /* (160) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING */
156170   265,  /* (161) upsert ::= ON CONFLICT DO NOTHING */
156171   263,  /* (162) insert_cmd ::= INSERT orconf */
156172   263,  /* (163) insert_cmd ::= REPLACE */
156173   264,  /* (164) idlist_opt ::= */
156174   264,  /* (165) idlist_opt ::= LP idlist RP */
156175   259,  /* (166) idlist ::= idlist COMMA nm */
156176   259,  /* (167) idlist ::= nm */
156177   212,  /* (168) expr ::= LP expr RP */
156178   212,  /* (169) expr ::= ID|INDEXED */
156179   212,  /* (170) expr ::= JOIN_KW */
156180   212,  /* (171) expr ::= nm DOT nm */
156181   212,  /* (172) expr ::= nm DOT nm DOT nm */
156182   211,  /* (173) term ::= NULL|FLOAT|BLOB */
156183   211,  /* (174) term ::= STRING */
156184   211,  /* (175) term ::= INTEGER */
156185   212,  /* (176) expr ::= VARIABLE */
156186   212,  /* (177) expr ::= expr COLLATE ID|STRING */
156187   212,  /* (178) expr ::= CAST LP expr AS typetoken RP */
156188   212,  /* (179) expr ::= ID|INDEXED LP distinct exprlist RP */
156189   212,  /* (180) expr ::= ID|INDEXED LP STAR RP */
156190   212,  /* (181) expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
156191   212,  /* (182) expr ::= ID|INDEXED LP STAR RP filter_over */
156192   211,  /* (183) term ::= CTIME_KW */
156193   212,  /* (184) expr ::= LP nexprlist COMMA expr RP */
156194   212,  /* (185) expr ::= expr AND expr */
156195   212,  /* (186) expr ::= expr OR expr */
156196   212,  /* (187) expr ::= expr LT|GT|GE|LE expr */
156197   212,  /* (188) expr ::= expr EQ|NE expr */
156198   212,  /* (189) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
156199   212,  /* (190) expr ::= expr PLUS|MINUS expr */
156200   212,  /* (191) expr ::= expr STAR|SLASH|REM expr */
156201   212,  /* (192) expr ::= expr CONCAT expr */
156202   267,  /* (193) likeop ::= NOT LIKE_KW|MATCH */
156203   212,  /* (194) expr ::= expr likeop expr */
156204   212,  /* (195) expr ::= expr likeop expr ESCAPE expr */
156205   212,  /* (196) expr ::= expr ISNULL|NOTNULL */
156206   212,  /* (197) expr ::= expr NOT NULL */
156207   212,  /* (198) expr ::= expr IS expr */
156208   212,  /* (199) expr ::= expr IS NOT expr */
156209   212,  /* (200) expr ::= NOT expr */
156210   212,  /* (201) expr ::= BITNOT expr */
156211   212,  /* (202) expr ::= PLUS|MINUS expr */
156212   268,  /* (203) between_op ::= BETWEEN */
156213   268,  /* (204) between_op ::= NOT BETWEEN */
156214   212,  /* (205) expr ::= expr between_op expr AND expr */
156215   269,  /* (206) in_op ::= IN */
156216   269,  /* (207) in_op ::= NOT IN */
156217   212,  /* (208) expr ::= expr in_op LP exprlist RP */
156218   212,  /* (209) expr ::= LP select RP */
156219   212,  /* (210) expr ::= expr in_op LP select RP */
156220   212,  /* (211) expr ::= expr in_op nm dbnm paren_exprlist */
156221   212,  /* (212) expr ::= EXISTS LP select RP */
156222   212,  /* (213) expr ::= CASE case_operand case_exprlist case_else END */
156223   272,  /* (214) case_exprlist ::= case_exprlist WHEN expr THEN expr */
156224   272,  /* (215) case_exprlist ::= WHEN expr THEN expr */
156225   273,  /* (216) case_else ::= ELSE expr */
156226   273,  /* (217) case_else ::= */
156227   271,  /* (218) case_operand ::= expr */
156228   271,  /* (219) case_operand ::= */
156229   257,  /* (220) exprlist ::= */
156230   248,  /* (221) nexprlist ::= nexprlist COMMA expr */
156231   248,  /* (222) nexprlist ::= expr */
156232   270,  /* (223) paren_exprlist ::= */
156233   270,  /* (224) paren_exprlist ::= LP exprlist RP */
156234   186,  /* (225) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
156235   274,  /* (226) uniqueflag ::= UNIQUE */
156236   274,  /* (227) uniqueflag ::= */
156237   216,  /* (228) eidlist_opt ::= */
156238   216,  /* (229) eidlist_opt ::= LP eidlist RP */
156239   227,  /* (230) eidlist ::= eidlist COMMA nm collate sortorder */
156240   227,  /* (231) eidlist ::= nm collate sortorder */
156241   275,  /* (232) collate ::= */
156242   275,  /* (233) collate ::= COLLATE ID|STRING */
156243   186,  /* (234) cmd ::= DROP INDEX ifexists fullname */
156244   186,  /* (235) cmd ::= VACUUM vinto */
156245   186,  /* (236) cmd ::= VACUUM nm vinto */
156246   276,  /* (237) vinto ::= INTO expr */
156247   276,  /* (238) vinto ::= */
156248   186,  /* (239) cmd ::= PRAGMA nm dbnm */
156249   186,  /* (240) cmd ::= PRAGMA nm dbnm EQ nmnum */
156250   186,  /* (241) cmd ::= PRAGMA nm dbnm LP nmnum RP */
156251   186,  /* (242) cmd ::= PRAGMA nm dbnm EQ minus_num */
156252   186,  /* (243) cmd ::= PRAGMA nm dbnm LP minus_num RP */
156253   206,  /* (244) plus_num ::= PLUS INTEGER|FLOAT */
156254   207,  /* (245) minus_num ::= MINUS INTEGER|FLOAT */
156255   186,  /* (246) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
156256   278,  /* (247) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
156257   280,  /* (248) trigger_time ::= BEFORE|AFTER */
156258   280,  /* (249) trigger_time ::= INSTEAD OF */
156259   280,  /* (250) trigger_time ::= */
156260   281,  /* (251) trigger_event ::= DELETE|INSERT */
156261   281,  /* (252) trigger_event ::= UPDATE */
156262   281,  /* (253) trigger_event ::= UPDATE OF idlist */
156263   283,  /* (254) when_clause ::= */
156264   283,  /* (255) when_clause ::= WHEN expr */
156265   279,  /* (256) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
156266   279,  /* (257) trigger_cmd_list ::= trigger_cmd SEMI */
156267   285,  /* (258) trnm ::= nm DOT nm */
156268   286,  /* (259) tridxby ::= INDEXED BY nm */
156269   286,  /* (260) tridxby ::= NOT INDEXED */
156270   284,  /* (261) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt */
156271   284,  /* (262) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
156272   284,  /* (263) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
156273   284,  /* (264) trigger_cmd ::= scanpt select scanpt */
156274   212,  /* (265) expr ::= RAISE LP IGNORE RP */
156275   212,  /* (266) expr ::= RAISE LP raisetype COMMA nm RP */
156276   231,  /* (267) raisetype ::= ROLLBACK */
156277   231,  /* (268) raisetype ::= ABORT */
156278   231,  /* (269) raisetype ::= FAIL */
156279   186,  /* (270) cmd ::= DROP TRIGGER ifexists fullname */
156280   186,  /* (271) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
156281   186,  /* (272) cmd ::= DETACH database_kw_opt expr */
156282   288,  /* (273) key_opt ::= */
156283   288,  /* (274) key_opt ::= KEY expr */
156284   186,  /* (275) cmd ::= REINDEX */
156285   186,  /* (276) cmd ::= REINDEX nm dbnm */
156286   186,  /* (277) cmd ::= ANALYZE */
156287   186,  /* (278) cmd ::= ANALYZE nm dbnm */
156288   186,  /* (279) cmd ::= ALTER TABLE fullname RENAME TO nm */
156289   186,  /* (280) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
156290   289,  /* (281) add_column_fullname ::= fullname */
156291   186,  /* (282) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
156292   186,  /* (283) cmd ::= create_vtab */
156293   186,  /* (284) cmd ::= create_vtab LP vtabarglist RP */
156294   291,  /* (285) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
156295   293,  /* (286) vtabarg ::= */
156296   294,  /* (287) vtabargtoken ::= ANY */
156297   294,  /* (288) vtabargtoken ::= lp anylist RP */
156298   295,  /* (289) lp ::= LP */
156299   261,  /* (290) with ::= WITH wqlist */
156300   261,  /* (291) with ::= WITH RECURSIVE wqlist */
156301   236,  /* (292) wqlist ::= nm eidlist_opt AS LP select RP */
156302   236,  /* (293) wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
156303   297,  /* (294) windowdefn_list ::= windowdefn */
156304   297,  /* (295) windowdefn_list ::= windowdefn_list COMMA windowdefn */
156305   298,  /* (296) windowdefn ::= nm AS LP window RP */
156306   299,  /* (297) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
156307   299,  /* (298) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
156308   299,  /* (299) window ::= ORDER BY sortlist frame_opt */
156309   299,  /* (300) window ::= nm ORDER BY sortlist frame_opt */
156310   299,  /* (301) window ::= frame_opt */
156311   299,  /* (302) window ::= nm frame_opt */
156312   300,  /* (303) frame_opt ::= */
156313   300,  /* (304) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
156314   300,  /* (305) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
156315   304,  /* (306) range_or_rows ::= RANGE|ROWS|GROUPS */
156316   306,  /* (307) frame_bound_s ::= frame_bound */
156317   306,  /* (308) frame_bound_s ::= UNBOUNDED PRECEDING */
156318   307,  /* (309) frame_bound_e ::= frame_bound */
156319   307,  /* (310) frame_bound_e ::= UNBOUNDED FOLLOWING */
156320   305,  /* (311) frame_bound ::= expr PRECEDING|FOLLOWING */
156321   305,  /* (312) frame_bound ::= CURRENT ROW */
156322   308,  /* (313) frame_exclude_opt ::= */
156323   308,  /* (314) frame_exclude_opt ::= EXCLUDE frame_exclude */
156324   309,  /* (315) frame_exclude ::= NO OTHERS */
156325   309,  /* (316) frame_exclude ::= CURRENT ROW */
156326   309,  /* (317) frame_exclude ::= GROUP|TIES */
156327   246,  /* (318) window_clause ::= WINDOW windowdefn_list */
156328   266,  /* (319) filter_over ::= filter_clause over_clause */
156329   266,  /* (320) filter_over ::= over_clause */
156330   266,  /* (321) filter_over ::= filter_clause */
156331   303,  /* (322) over_clause ::= OVER LP window RP */
156332   303,  /* (323) over_clause ::= OVER nm */
156333   302,  /* (324) filter_clause ::= FILTER LP WHERE expr RP */
156334   181,  /* (325) input ::= cmdlist */
156335   182,  /* (326) cmdlist ::= cmdlist ecmd */
156336   182,  /* (327) cmdlist ::= ecmd */
156337   183,  /* (328) ecmd ::= SEMI */
156338   183,  /* (329) ecmd ::= cmdx SEMI */
156339   183,  /* (330) ecmd ::= explain cmdx SEMI */
156340   188,  /* (331) trans_opt ::= */
156341   188,  /* (332) trans_opt ::= TRANSACTION */
156342   188,  /* (333) trans_opt ::= TRANSACTION nm */
156343   190,  /* (334) savepoint_opt ::= SAVEPOINT */
156344   190,  /* (335) savepoint_opt ::= */
156345   186,  /* (336) cmd ::= create_table create_table_args */
156346   197,  /* (337) columnlist ::= columnlist COMMA columnname carglist */
156347   197,  /* (338) columnlist ::= columnname carglist */
156348   189,  /* (339) nm ::= ID|INDEXED */
156349   189,  /* (340) nm ::= STRING */
156350   189,  /* (341) nm ::= JOIN_KW */
156351   203,  /* (342) typetoken ::= typename */
156352   204,  /* (343) typename ::= ID|STRING */
156353   205,  /* (344) signed ::= plus_num */
156354   205,  /* (345) signed ::= minus_num */
156355   202,  /* (346) carglist ::= carglist ccons */
156356   202,  /* (347) carglist ::= */
156357   210,  /* (348) ccons ::= NULL onconf */
156358   210,  /* (349) ccons ::= GENERATED ALWAYS AS generated */
156359   210,  /* (350) ccons ::= AS generated */
156360   198,  /* (351) conslist_opt ::= COMMA conslist */
156361   223,  /* (352) conslist ::= conslist tconscomma tcons */
156362   223,  /* (353) conslist ::= tcons */
156363   224,  /* (354) tconscomma ::= */
156364   228,  /* (355) defer_subclause_opt ::= defer_subclause */
156365   230,  /* (356) resolvetype ::= raisetype */
156366   234,  /* (357) selectnowith ::= oneselect */
156367   235,  /* (358) oneselect ::= values */
156368   249,  /* (359) sclp ::= selcollist COMMA */
156369   250,  /* (360) as ::= ID|STRING */
156370   212,  /* (361) expr ::= term */
156371   267,  /* (362) likeop ::= LIKE_KW|MATCH */
156372   257,  /* (363) exprlist ::= nexprlist */
156373   277,  /* (364) nmnum ::= plus_num */
156374   277,  /* (365) nmnum ::= nm */
156375   277,  /* (366) nmnum ::= ON */
156376   277,  /* (367) nmnum ::= DELETE */
156377   277,  /* (368) nmnum ::= DEFAULT */
156378   206,  /* (369) plus_num ::= INTEGER|FLOAT */
156379   282,  /* (370) foreach_clause ::= */
156380   282,  /* (371) foreach_clause ::= FOR EACH ROW */
156381   285,  /* (372) trnm ::= nm */
156382   286,  /* (373) tridxby ::= */
156383   287,  /* (374) database_kw_opt ::= DATABASE */
156384   287,  /* (375) database_kw_opt ::= */
156385   290,  /* (376) kwcolumn_opt ::= */
156386   290,  /* (377) kwcolumn_opt ::= COLUMNKW */
156387   292,  /* (378) vtabarglist ::= vtabarg */
156388   292,  /* (379) vtabarglist ::= vtabarglist COMMA vtabarg */
156389   293,  /* (380) vtabarg ::= vtabarg vtabargtoken */
156390   296,  /* (381) anylist ::= */
156391   296,  /* (382) anylist ::= anylist LP anylist RP */
156392   296,  /* (383) anylist ::= anylist ANY */
156393   261,  /* (384) with ::= */
156394};
156395
156396/* For rule J, yyRuleInfoNRhs[J] contains the negative of the number
156397** of symbols on the right-hand side of that rule. */
156398static const signed char yyRuleInfoNRhs[] = {
156399   -1,  /* (0) explain ::= EXPLAIN */
156400   -3,  /* (1) explain ::= EXPLAIN QUERY PLAN */
156401   -1,  /* (2) cmdx ::= cmd */
156402   -3,  /* (3) cmd ::= BEGIN transtype trans_opt */
156403    0,  /* (4) transtype ::= */
156404   -1,  /* (5) transtype ::= DEFERRED */
156405   -1,  /* (6) transtype ::= IMMEDIATE */
156406   -1,  /* (7) transtype ::= EXCLUSIVE */
156407   -2,  /* (8) cmd ::= COMMIT|END trans_opt */
156408   -2,  /* (9) cmd ::= ROLLBACK trans_opt */
156409   -2,  /* (10) cmd ::= SAVEPOINT nm */
156410   -3,  /* (11) cmd ::= RELEASE savepoint_opt nm */
156411   -5,  /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
156412   -6,  /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
156413   -1,  /* (14) createkw ::= CREATE */
156414    0,  /* (15) ifnotexists ::= */
156415   -3,  /* (16) ifnotexists ::= IF NOT EXISTS */
156416   -1,  /* (17) temp ::= TEMP */
156417    0,  /* (18) temp ::= */
156418   -5,  /* (19) create_table_args ::= LP columnlist conslist_opt RP table_options */
156419   -2,  /* (20) create_table_args ::= AS select */
156420    0,  /* (21) table_options ::= */
156421   -2,  /* (22) table_options ::= WITHOUT nm */
156422   -2,  /* (23) columnname ::= nm typetoken */
156423    0,  /* (24) typetoken ::= */
156424   -4,  /* (25) typetoken ::= typename LP signed RP */
156425   -6,  /* (26) typetoken ::= typename LP signed COMMA signed RP */
156426   -2,  /* (27) typename ::= typename ID|STRING */
156427    0,  /* (28) scanpt ::= */
156428    0,  /* (29) scantok ::= */
156429   -2,  /* (30) ccons ::= CONSTRAINT nm */
156430   -3,  /* (31) ccons ::= DEFAULT scantok term */
156431   -4,  /* (32) ccons ::= DEFAULT LP expr RP */
156432   -4,  /* (33) ccons ::= DEFAULT PLUS scantok term */
156433   -4,  /* (34) ccons ::= DEFAULT MINUS scantok term */
156434   -3,  /* (35) ccons ::= DEFAULT scantok ID|INDEXED */
156435   -3,  /* (36) ccons ::= NOT NULL onconf */
156436   -5,  /* (37) ccons ::= PRIMARY KEY sortorder onconf autoinc */
156437   -2,  /* (38) ccons ::= UNIQUE onconf */
156438   -4,  /* (39) ccons ::= CHECK LP expr RP */
156439   -4,  /* (40) ccons ::= REFERENCES nm eidlist_opt refargs */
156440   -1,  /* (41) ccons ::= defer_subclause */
156441   -2,  /* (42) ccons ::= COLLATE ID|STRING */
156442   -3,  /* (43) generated ::= LP expr RP */
156443   -4,  /* (44) generated ::= LP expr RP ID */
156444    0,  /* (45) autoinc ::= */
156445   -1,  /* (46) autoinc ::= AUTOINCR */
156446    0,  /* (47) refargs ::= */
156447   -2,  /* (48) refargs ::= refargs refarg */
156448   -2,  /* (49) refarg ::= MATCH nm */
156449   -3,  /* (50) refarg ::= ON INSERT refact */
156450   -3,  /* (51) refarg ::= ON DELETE refact */
156451   -3,  /* (52) refarg ::= ON UPDATE refact */
156452   -2,  /* (53) refact ::= SET NULL */
156453   -2,  /* (54) refact ::= SET DEFAULT */
156454   -1,  /* (55) refact ::= CASCADE */
156455   -1,  /* (56) refact ::= RESTRICT */
156456   -2,  /* (57) refact ::= NO ACTION */
156457   -3,  /* (58) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
156458   -2,  /* (59) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
156459    0,  /* (60) init_deferred_pred_opt ::= */
156460   -2,  /* (61) init_deferred_pred_opt ::= INITIALLY DEFERRED */
156461   -2,  /* (62) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
156462    0,  /* (63) conslist_opt ::= */
156463   -1,  /* (64) tconscomma ::= COMMA */
156464   -2,  /* (65) tcons ::= CONSTRAINT nm */
156465   -7,  /* (66) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
156466   -5,  /* (67) tcons ::= UNIQUE LP sortlist RP onconf */
156467   -5,  /* (68) tcons ::= CHECK LP expr RP onconf */
156468  -10,  /* (69) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
156469    0,  /* (70) defer_subclause_opt ::= */
156470    0,  /* (71) onconf ::= */
156471   -3,  /* (72) onconf ::= ON CONFLICT resolvetype */
156472    0,  /* (73) orconf ::= */
156473   -2,  /* (74) orconf ::= OR resolvetype */
156474   -1,  /* (75) resolvetype ::= IGNORE */
156475   -1,  /* (76) resolvetype ::= REPLACE */
156476   -4,  /* (77) cmd ::= DROP TABLE ifexists fullname */
156477   -2,  /* (78) ifexists ::= IF EXISTS */
156478    0,  /* (79) ifexists ::= */
156479   -9,  /* (80) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
156480   -4,  /* (81) cmd ::= DROP VIEW ifexists fullname */
156481   -1,  /* (82) cmd ::= select */
156482   -3,  /* (83) select ::= WITH wqlist selectnowith */
156483   -4,  /* (84) select ::= WITH RECURSIVE wqlist selectnowith */
156484   -1,  /* (85) select ::= selectnowith */
156485   -3,  /* (86) selectnowith ::= selectnowith multiselect_op oneselect */
156486   -1,  /* (87) multiselect_op ::= UNION */
156487   -2,  /* (88) multiselect_op ::= UNION ALL */
156488   -1,  /* (89) multiselect_op ::= EXCEPT|INTERSECT */
156489   -9,  /* (90) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
156490  -10,  /* (91) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
156491   -4,  /* (92) values ::= VALUES LP nexprlist RP */
156492   -5,  /* (93) values ::= values COMMA LP nexprlist RP */
156493   -1,  /* (94) distinct ::= DISTINCT */
156494   -1,  /* (95) distinct ::= ALL */
156495    0,  /* (96) distinct ::= */
156496    0,  /* (97) sclp ::= */
156497   -5,  /* (98) selcollist ::= sclp scanpt expr scanpt as */
156498   -3,  /* (99) selcollist ::= sclp scanpt STAR */
156499   -5,  /* (100) selcollist ::= sclp scanpt nm DOT STAR */
156500   -2,  /* (101) as ::= AS nm */
156501    0,  /* (102) as ::= */
156502    0,  /* (103) from ::= */
156503   -2,  /* (104) from ::= FROM seltablist */
156504   -2,  /* (105) stl_prefix ::= seltablist joinop */
156505    0,  /* (106) stl_prefix ::= */
156506   -7,  /* (107) seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
156507   -9,  /* (108) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
156508   -7,  /* (109) seltablist ::= stl_prefix LP select RP as on_opt using_opt */
156509   -7,  /* (110) seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
156510    0,  /* (111) dbnm ::= */
156511   -2,  /* (112) dbnm ::= DOT nm */
156512   -1,  /* (113) fullname ::= nm */
156513   -3,  /* (114) fullname ::= nm DOT nm */
156514   -1,  /* (115) xfullname ::= nm */
156515   -3,  /* (116) xfullname ::= nm DOT nm */
156516   -5,  /* (117) xfullname ::= nm DOT nm AS nm */
156517   -3,  /* (118) xfullname ::= nm AS nm */
156518   -1,  /* (119) joinop ::= COMMA|JOIN */
156519   -2,  /* (120) joinop ::= JOIN_KW JOIN */
156520   -3,  /* (121) joinop ::= JOIN_KW nm JOIN */
156521   -4,  /* (122) joinop ::= JOIN_KW nm nm JOIN */
156522   -2,  /* (123) on_opt ::= ON expr */
156523    0,  /* (124) on_opt ::= */
156524    0,  /* (125) indexed_opt ::= */
156525   -3,  /* (126) indexed_opt ::= INDEXED BY nm */
156526   -2,  /* (127) indexed_opt ::= NOT INDEXED */
156527   -4,  /* (128) using_opt ::= USING LP idlist RP */
156528    0,  /* (129) using_opt ::= */
156529    0,  /* (130) orderby_opt ::= */
156530   -3,  /* (131) orderby_opt ::= ORDER BY sortlist */
156531   -5,  /* (132) sortlist ::= sortlist COMMA expr sortorder nulls */
156532   -3,  /* (133) sortlist ::= expr sortorder nulls */
156533   -1,  /* (134) sortorder ::= ASC */
156534   -1,  /* (135) sortorder ::= DESC */
156535    0,  /* (136) sortorder ::= */
156536   -2,  /* (137) nulls ::= NULLS FIRST */
156537   -2,  /* (138) nulls ::= NULLS LAST */
156538    0,  /* (139) nulls ::= */
156539    0,  /* (140) groupby_opt ::= */
156540   -3,  /* (141) groupby_opt ::= GROUP BY nexprlist */
156541    0,  /* (142) having_opt ::= */
156542   -2,  /* (143) having_opt ::= HAVING expr */
156543    0,  /* (144) limit_opt ::= */
156544   -2,  /* (145) limit_opt ::= LIMIT expr */
156545   -4,  /* (146) limit_opt ::= LIMIT expr OFFSET expr */
156546   -4,  /* (147) limit_opt ::= LIMIT expr COMMA expr */
156547   -6,  /* (148) cmd ::= with DELETE FROM xfullname indexed_opt where_opt */
156548    0,  /* (149) where_opt ::= */
156549   -2,  /* (150) where_opt ::= WHERE expr */
156550   -8,  /* (151) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt */
156551   -5,  /* (152) setlist ::= setlist COMMA nm EQ expr */
156552   -7,  /* (153) setlist ::= setlist COMMA LP idlist RP EQ expr */
156553   -3,  /* (154) setlist ::= nm EQ expr */
156554   -5,  /* (155) setlist ::= LP idlist RP EQ expr */
156555   -7,  /* (156) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
156556   -7,  /* (157) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES */
156557    0,  /* (158) upsert ::= */
156558  -11,  /* (159) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt */
156559   -8,  /* (160) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING */
156560   -4,  /* (161) upsert ::= ON CONFLICT DO NOTHING */
156561   -2,  /* (162) insert_cmd ::= INSERT orconf */
156562   -1,  /* (163) insert_cmd ::= REPLACE */
156563    0,  /* (164) idlist_opt ::= */
156564   -3,  /* (165) idlist_opt ::= LP idlist RP */
156565   -3,  /* (166) idlist ::= idlist COMMA nm */
156566   -1,  /* (167) idlist ::= nm */
156567   -3,  /* (168) expr ::= LP expr RP */
156568   -1,  /* (169) expr ::= ID|INDEXED */
156569   -1,  /* (170) expr ::= JOIN_KW */
156570   -3,  /* (171) expr ::= nm DOT nm */
156571   -5,  /* (172) expr ::= nm DOT nm DOT nm */
156572   -1,  /* (173) term ::= NULL|FLOAT|BLOB */
156573   -1,  /* (174) term ::= STRING */
156574   -1,  /* (175) term ::= INTEGER */
156575   -1,  /* (176) expr ::= VARIABLE */
156576   -3,  /* (177) expr ::= expr COLLATE ID|STRING */
156577   -6,  /* (178) expr ::= CAST LP expr AS typetoken RP */
156578   -5,  /* (179) expr ::= ID|INDEXED LP distinct exprlist RP */
156579   -4,  /* (180) expr ::= ID|INDEXED LP STAR RP */
156580   -6,  /* (181) expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
156581   -5,  /* (182) expr ::= ID|INDEXED LP STAR RP filter_over */
156582   -1,  /* (183) term ::= CTIME_KW */
156583   -5,  /* (184) expr ::= LP nexprlist COMMA expr RP */
156584   -3,  /* (185) expr ::= expr AND expr */
156585   -3,  /* (186) expr ::= expr OR expr */
156586   -3,  /* (187) expr ::= expr LT|GT|GE|LE expr */
156587   -3,  /* (188) expr ::= expr EQ|NE expr */
156588   -3,  /* (189) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
156589   -3,  /* (190) expr ::= expr PLUS|MINUS expr */
156590   -3,  /* (191) expr ::= expr STAR|SLASH|REM expr */
156591   -3,  /* (192) expr ::= expr CONCAT expr */
156592   -2,  /* (193) likeop ::= NOT LIKE_KW|MATCH */
156593   -3,  /* (194) expr ::= expr likeop expr */
156594   -5,  /* (195) expr ::= expr likeop expr ESCAPE expr */
156595   -2,  /* (196) expr ::= expr ISNULL|NOTNULL */
156596   -3,  /* (197) expr ::= expr NOT NULL */
156597   -3,  /* (198) expr ::= expr IS expr */
156598   -4,  /* (199) expr ::= expr IS NOT expr */
156599   -2,  /* (200) expr ::= NOT expr */
156600   -2,  /* (201) expr ::= BITNOT expr */
156601   -2,  /* (202) expr ::= PLUS|MINUS expr */
156602   -1,  /* (203) between_op ::= BETWEEN */
156603   -2,  /* (204) between_op ::= NOT BETWEEN */
156604   -5,  /* (205) expr ::= expr between_op expr AND expr */
156605   -1,  /* (206) in_op ::= IN */
156606   -2,  /* (207) in_op ::= NOT IN */
156607   -5,  /* (208) expr ::= expr in_op LP exprlist RP */
156608   -3,  /* (209) expr ::= LP select RP */
156609   -5,  /* (210) expr ::= expr in_op LP select RP */
156610   -5,  /* (211) expr ::= expr in_op nm dbnm paren_exprlist */
156611   -4,  /* (212) expr ::= EXISTS LP select RP */
156612   -5,  /* (213) expr ::= CASE case_operand case_exprlist case_else END */
156613   -5,  /* (214) case_exprlist ::= case_exprlist WHEN expr THEN expr */
156614   -4,  /* (215) case_exprlist ::= WHEN expr THEN expr */
156615   -2,  /* (216) case_else ::= ELSE expr */
156616    0,  /* (217) case_else ::= */
156617   -1,  /* (218) case_operand ::= expr */
156618    0,  /* (219) case_operand ::= */
156619    0,  /* (220) exprlist ::= */
156620   -3,  /* (221) nexprlist ::= nexprlist COMMA expr */
156621   -1,  /* (222) nexprlist ::= expr */
156622    0,  /* (223) paren_exprlist ::= */
156623   -3,  /* (224) paren_exprlist ::= LP exprlist RP */
156624  -12,  /* (225) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
156625   -1,  /* (226) uniqueflag ::= UNIQUE */
156626    0,  /* (227) uniqueflag ::= */
156627    0,  /* (228) eidlist_opt ::= */
156628   -3,  /* (229) eidlist_opt ::= LP eidlist RP */
156629   -5,  /* (230) eidlist ::= eidlist COMMA nm collate sortorder */
156630   -3,  /* (231) eidlist ::= nm collate sortorder */
156631    0,  /* (232) collate ::= */
156632   -2,  /* (233) collate ::= COLLATE ID|STRING */
156633   -4,  /* (234) cmd ::= DROP INDEX ifexists fullname */
156634   -2,  /* (235) cmd ::= VACUUM vinto */
156635   -3,  /* (236) cmd ::= VACUUM nm vinto */
156636   -2,  /* (237) vinto ::= INTO expr */
156637    0,  /* (238) vinto ::= */
156638   -3,  /* (239) cmd ::= PRAGMA nm dbnm */
156639   -5,  /* (240) cmd ::= PRAGMA nm dbnm EQ nmnum */
156640   -6,  /* (241) cmd ::= PRAGMA nm dbnm LP nmnum RP */
156641   -5,  /* (242) cmd ::= PRAGMA nm dbnm EQ minus_num */
156642   -6,  /* (243) cmd ::= PRAGMA nm dbnm LP minus_num RP */
156643   -2,  /* (244) plus_num ::= PLUS INTEGER|FLOAT */
156644   -2,  /* (245) minus_num ::= MINUS INTEGER|FLOAT */
156645   -5,  /* (246) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
156646  -11,  /* (247) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
156647   -1,  /* (248) trigger_time ::= BEFORE|AFTER */
156648   -2,  /* (249) trigger_time ::= INSTEAD OF */
156649    0,  /* (250) trigger_time ::= */
156650   -1,  /* (251) trigger_event ::= DELETE|INSERT */
156651   -1,  /* (252) trigger_event ::= UPDATE */
156652   -3,  /* (253) trigger_event ::= UPDATE OF idlist */
156653    0,  /* (254) when_clause ::= */
156654   -2,  /* (255) when_clause ::= WHEN expr */
156655   -3,  /* (256) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
156656   -2,  /* (257) trigger_cmd_list ::= trigger_cmd SEMI */
156657   -3,  /* (258) trnm ::= nm DOT nm */
156658   -3,  /* (259) tridxby ::= INDEXED BY nm */
156659   -2,  /* (260) tridxby ::= NOT INDEXED */
156660   -8,  /* (261) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt */
156661   -8,  /* (262) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
156662   -6,  /* (263) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
156663   -3,  /* (264) trigger_cmd ::= scanpt select scanpt */
156664   -4,  /* (265) expr ::= RAISE LP IGNORE RP */
156665   -6,  /* (266) expr ::= RAISE LP raisetype COMMA nm RP */
156666   -1,  /* (267) raisetype ::= ROLLBACK */
156667   -1,  /* (268) raisetype ::= ABORT */
156668   -1,  /* (269) raisetype ::= FAIL */
156669   -4,  /* (270) cmd ::= DROP TRIGGER ifexists fullname */
156670   -6,  /* (271) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
156671   -3,  /* (272) cmd ::= DETACH database_kw_opt expr */
156672    0,  /* (273) key_opt ::= */
156673   -2,  /* (274) key_opt ::= KEY expr */
156674   -1,  /* (275) cmd ::= REINDEX */
156675   -3,  /* (276) cmd ::= REINDEX nm dbnm */
156676   -1,  /* (277) cmd ::= ANALYZE */
156677   -3,  /* (278) cmd ::= ANALYZE nm dbnm */
156678   -6,  /* (279) cmd ::= ALTER TABLE fullname RENAME TO nm */
156679   -7,  /* (280) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
156680   -1,  /* (281) add_column_fullname ::= fullname */
156681   -8,  /* (282) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
156682   -1,  /* (283) cmd ::= create_vtab */
156683   -4,  /* (284) cmd ::= create_vtab LP vtabarglist RP */
156684   -8,  /* (285) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
156685    0,  /* (286) vtabarg ::= */
156686   -1,  /* (287) vtabargtoken ::= ANY */
156687   -3,  /* (288) vtabargtoken ::= lp anylist RP */
156688   -1,  /* (289) lp ::= LP */
156689   -2,  /* (290) with ::= WITH wqlist */
156690   -3,  /* (291) with ::= WITH RECURSIVE wqlist */
156691   -6,  /* (292) wqlist ::= nm eidlist_opt AS LP select RP */
156692   -8,  /* (293) wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
156693   -1,  /* (294) windowdefn_list ::= windowdefn */
156694   -3,  /* (295) windowdefn_list ::= windowdefn_list COMMA windowdefn */
156695   -5,  /* (296) windowdefn ::= nm AS LP window RP */
156696   -5,  /* (297) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
156697   -6,  /* (298) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
156698   -4,  /* (299) window ::= ORDER BY sortlist frame_opt */
156699   -5,  /* (300) window ::= nm ORDER BY sortlist frame_opt */
156700   -1,  /* (301) window ::= frame_opt */
156701   -2,  /* (302) window ::= nm frame_opt */
156702    0,  /* (303) frame_opt ::= */
156703   -3,  /* (304) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
156704   -6,  /* (305) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
156705   -1,  /* (306) range_or_rows ::= RANGE|ROWS|GROUPS */
156706   -1,  /* (307) frame_bound_s ::= frame_bound */
156707   -2,  /* (308) frame_bound_s ::= UNBOUNDED PRECEDING */
156708   -1,  /* (309) frame_bound_e ::= frame_bound */
156709   -2,  /* (310) frame_bound_e ::= UNBOUNDED FOLLOWING */
156710   -2,  /* (311) frame_bound ::= expr PRECEDING|FOLLOWING */
156711   -2,  /* (312) frame_bound ::= CURRENT ROW */
156712    0,  /* (313) frame_exclude_opt ::= */
156713   -2,  /* (314) frame_exclude_opt ::= EXCLUDE frame_exclude */
156714   -2,  /* (315) frame_exclude ::= NO OTHERS */
156715   -2,  /* (316) frame_exclude ::= CURRENT ROW */
156716   -1,  /* (317) frame_exclude ::= GROUP|TIES */
156717   -2,  /* (318) window_clause ::= WINDOW windowdefn_list */
156718   -2,  /* (319) filter_over ::= filter_clause over_clause */
156719   -1,  /* (320) filter_over ::= over_clause */
156720   -1,  /* (321) filter_over ::= filter_clause */
156721   -4,  /* (322) over_clause ::= OVER LP window RP */
156722   -2,  /* (323) over_clause ::= OVER nm */
156723   -5,  /* (324) filter_clause ::= FILTER LP WHERE expr RP */
156724   -1,  /* (325) input ::= cmdlist */
156725   -2,  /* (326) cmdlist ::= cmdlist ecmd */
156726   -1,  /* (327) cmdlist ::= ecmd */
156727   -1,  /* (328) ecmd ::= SEMI */
156728   -2,  /* (329) ecmd ::= cmdx SEMI */
156729   -3,  /* (330) ecmd ::= explain cmdx SEMI */
156730    0,  /* (331) trans_opt ::= */
156731   -1,  /* (332) trans_opt ::= TRANSACTION */
156732   -2,  /* (333) trans_opt ::= TRANSACTION nm */
156733   -1,  /* (334) savepoint_opt ::= SAVEPOINT */
156734    0,  /* (335) savepoint_opt ::= */
156735   -2,  /* (336) cmd ::= create_table create_table_args */
156736   -4,  /* (337) columnlist ::= columnlist COMMA columnname carglist */
156737   -2,  /* (338) columnlist ::= columnname carglist */
156738   -1,  /* (339) nm ::= ID|INDEXED */
156739   -1,  /* (340) nm ::= STRING */
156740   -1,  /* (341) nm ::= JOIN_KW */
156741   -1,  /* (342) typetoken ::= typename */
156742   -1,  /* (343) typename ::= ID|STRING */
156743   -1,  /* (344) signed ::= plus_num */
156744   -1,  /* (345) signed ::= minus_num */
156745   -2,  /* (346) carglist ::= carglist ccons */
156746    0,  /* (347) carglist ::= */
156747   -2,  /* (348) ccons ::= NULL onconf */
156748   -4,  /* (349) ccons ::= GENERATED ALWAYS AS generated */
156749   -2,  /* (350) ccons ::= AS generated */
156750   -2,  /* (351) conslist_opt ::= COMMA conslist */
156751   -3,  /* (352) conslist ::= conslist tconscomma tcons */
156752   -1,  /* (353) conslist ::= tcons */
156753    0,  /* (354) tconscomma ::= */
156754   -1,  /* (355) defer_subclause_opt ::= defer_subclause */
156755   -1,  /* (356) resolvetype ::= raisetype */
156756   -1,  /* (357) selectnowith ::= oneselect */
156757   -1,  /* (358) oneselect ::= values */
156758   -2,  /* (359) sclp ::= selcollist COMMA */
156759   -1,  /* (360) as ::= ID|STRING */
156760   -1,  /* (361) expr ::= term */
156761   -1,  /* (362) likeop ::= LIKE_KW|MATCH */
156762   -1,  /* (363) exprlist ::= nexprlist */
156763   -1,  /* (364) nmnum ::= plus_num */
156764   -1,  /* (365) nmnum ::= nm */
156765   -1,  /* (366) nmnum ::= ON */
156766   -1,  /* (367) nmnum ::= DELETE */
156767   -1,  /* (368) nmnum ::= DEFAULT */
156768   -1,  /* (369) plus_num ::= INTEGER|FLOAT */
156769    0,  /* (370) foreach_clause ::= */
156770   -3,  /* (371) foreach_clause ::= FOR EACH ROW */
156771   -1,  /* (372) trnm ::= nm */
156772    0,  /* (373) tridxby ::= */
156773   -1,  /* (374) database_kw_opt ::= DATABASE */
156774    0,  /* (375) database_kw_opt ::= */
156775    0,  /* (376) kwcolumn_opt ::= */
156776   -1,  /* (377) kwcolumn_opt ::= COLUMNKW */
156777   -1,  /* (378) vtabarglist ::= vtabarg */
156778   -3,  /* (379) vtabarglist ::= vtabarglist COMMA vtabarg */
156779   -2,  /* (380) vtabarg ::= vtabarg vtabargtoken */
156780    0,  /* (381) anylist ::= */
156781   -4,  /* (382) anylist ::= anylist LP anylist RP */
156782   -2,  /* (383) anylist ::= anylist ANY */
156783    0,  /* (384) with ::= */
156784};
156785
156786static void yy_accept(yyParser*);  /* Forward Declaration */
156787
156788/*
156789** Perform a reduce action and the shift that must immediately
156790** follow the reduce.
156791**
156792** The yyLookahead and yyLookaheadToken parameters provide reduce actions
156793** access to the lookahead token (if any).  The yyLookahead will be YYNOCODE
156794** if the lookahead token has already been consumed.  As this procedure is
156795** only called from one place, optimizing compilers will in-line it, which
156796** means that the extra parameters have no performance impact.
156797*/
156798static YYACTIONTYPE yy_reduce(
156799  yyParser *yypParser,         /* The parser */
156800  unsigned int yyruleno,       /* Number of the rule by which to reduce */
156801  int yyLookahead,             /* Lookahead token, or YYNOCODE if none */
156802  sqlite3ParserTOKENTYPE yyLookaheadToken  /* Value of the lookahead token */
156803  sqlite3ParserCTX_PDECL                   /* %extra_context */
156804){
156805  int yygoto;                     /* The next state */
156806  YYACTIONTYPE yyact;             /* The next action */
156807  yyStackEntry *yymsp;            /* The top of the parser's stack */
156808  int yysize;                     /* Amount to pop the stack */
156809  sqlite3ParserARG_FETCH
156810  (void)yyLookahead;
156811  (void)yyLookaheadToken;
156812  yymsp = yypParser->yytos;
156813#ifndef NDEBUG
156814  if( yyTraceFILE && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
156815    yysize = yyRuleInfoNRhs[yyruleno];
156816    if( yysize ){
156817      fprintf(yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
156818        yyTracePrompt,
156819        yyruleno, yyRuleName[yyruleno],
156820        yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action",
156821        yymsp[yysize].stateno);
156822    }else{
156823      fprintf(yyTraceFILE, "%sReduce %d [%s]%s.\n",
156824        yyTracePrompt, yyruleno, yyRuleName[yyruleno],
156825        yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action");
156826    }
156827  }
156828#endif /* NDEBUG */
156829
156830  /* Check that the stack is large enough to grow by a single entry
156831  ** if the RHS of the rule is empty.  This ensures that there is room
156832  ** enough on the stack to push the LHS value */
156833  if( yyRuleInfoNRhs[yyruleno]==0 ){
156834#ifdef YYTRACKMAXSTACKDEPTH
156835    if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
156836      yypParser->yyhwm++;
156837      assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack));
156838    }
156839#endif
156840#if YYSTACKDEPTH>0
156841    if( yypParser->yytos>=yypParser->yystackEnd ){
156842      yyStackOverflow(yypParser);
156843      /* The call to yyStackOverflow() above pops the stack until it is
156844      ** empty, causing the main parser loop to exit.  So the return value
156845      ** is never used and does not matter. */
156846      return 0;
156847    }
156848#else
156849    if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
156850      if( yyGrowStack(yypParser) ){
156851        yyStackOverflow(yypParser);
156852        /* The call to yyStackOverflow() above pops the stack until it is
156853        ** empty, causing the main parser loop to exit.  So the return value
156854        ** is never used and does not matter. */
156855        return 0;
156856      }
156857      yymsp = yypParser->yytos;
156858    }
156859#endif
156860  }
156861
156862  switch( yyruleno ){
156863  /* Beginning here are the reduction cases.  A typical example
156864  ** follows:
156865  **   case 0:
156866  **  #line <lineno> <grammarfile>
156867  **     { ... }           // User supplied code
156868  **  #line <lineno> <thisfile>
156869  **     break;
156870  */
156871/********** Begin reduce actions **********************************************/
156872        YYMINORTYPE yylhsminor;
156873      case 0: /* explain ::= EXPLAIN */
156874{ pParse->explain = 1; }
156875        break;
156876      case 1: /* explain ::= EXPLAIN QUERY PLAN */
156877{ pParse->explain = 2; }
156878        break;
156879      case 2: /* cmdx ::= cmd */
156880{ sqlite3FinishCoding(pParse); }
156881        break;
156882      case 3: /* cmd ::= BEGIN transtype trans_opt */
156883{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy192);}
156884        break;
156885      case 4: /* transtype ::= */
156886{yymsp[1].minor.yy192 = TK_DEFERRED;}
156887        break;
156888      case 5: /* transtype ::= DEFERRED */
156889      case 6: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==6);
156890      case 7: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==7);
156891      case 306: /* range_or_rows ::= RANGE|ROWS|GROUPS */ yytestcase(yyruleno==306);
156892{yymsp[0].minor.yy192 = yymsp[0].major; /*A-overwrites-X*/}
156893        break;
156894      case 8: /* cmd ::= COMMIT|END trans_opt */
156895      case 9: /* cmd ::= ROLLBACK trans_opt */ yytestcase(yyruleno==9);
156896{sqlite3EndTransaction(pParse,yymsp[-1].major);}
156897        break;
156898      case 10: /* cmd ::= SAVEPOINT nm */
156899{
156900  sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
156901}
156902        break;
156903      case 11: /* cmd ::= RELEASE savepoint_opt nm */
156904{
156905  sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
156906}
156907        break;
156908      case 12: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
156909{
156910  sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
156911}
156912        break;
156913      case 13: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
156914{
156915   sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy192,0,0,yymsp[-2].minor.yy192);
156916}
156917        break;
156918      case 14: /* createkw ::= CREATE */
156919{disableLookaside(pParse);}
156920        break;
156921      case 15: /* ifnotexists ::= */
156922      case 18: /* temp ::= */ yytestcase(yyruleno==18);
156923      case 21: /* table_options ::= */ yytestcase(yyruleno==21);
156924      case 45: /* autoinc ::= */ yytestcase(yyruleno==45);
156925      case 60: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==60);
156926      case 70: /* defer_subclause_opt ::= */ yytestcase(yyruleno==70);
156927      case 79: /* ifexists ::= */ yytestcase(yyruleno==79);
156928      case 96: /* distinct ::= */ yytestcase(yyruleno==96);
156929      case 232: /* collate ::= */ yytestcase(yyruleno==232);
156930{yymsp[1].minor.yy192 = 0;}
156931        break;
156932      case 16: /* ifnotexists ::= IF NOT EXISTS */
156933{yymsp[-2].minor.yy192 = 1;}
156934        break;
156935      case 17: /* temp ::= TEMP */
156936      case 46: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==46);
156937{yymsp[0].minor.yy192 = 1;}
156938        break;
156939      case 19: /* create_table_args ::= LP columnlist conslist_opt RP table_options */
156940{
156941  sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy192,0);
156942}
156943        break;
156944      case 20: /* create_table_args ::= AS select */
156945{
156946  sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy539);
156947  sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy539);
156948}
156949        break;
156950      case 22: /* table_options ::= WITHOUT nm */
156951{
156952  if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
156953    yymsp[-1].minor.yy192 = TF_WithoutRowid | TF_NoVisibleRowid;
156954  }else{
156955    yymsp[-1].minor.yy192 = 0;
156956    sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
156957  }
156958}
156959        break;
156960      case 23: /* columnname ::= nm typetoken */
156961{sqlite3AddColumn(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
156962        break;
156963      case 24: /* typetoken ::= */
156964      case 63: /* conslist_opt ::= */ yytestcase(yyruleno==63);
156965      case 102: /* as ::= */ yytestcase(yyruleno==102);
156966{yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = 0;}
156967        break;
156968      case 25: /* typetoken ::= typename LP signed RP */
156969{
156970  yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
156971}
156972        break;
156973      case 26: /* typetoken ::= typename LP signed COMMA signed RP */
156974{
156975  yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
156976}
156977        break;
156978      case 27: /* typename ::= typename ID|STRING */
156979{yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
156980        break;
156981      case 28: /* scanpt ::= */
156982{
156983  assert( yyLookahead!=YYNOCODE );
156984  yymsp[1].minor.yy436 = yyLookaheadToken.z;
156985}
156986        break;
156987      case 29: /* scantok ::= */
156988{
156989  assert( yyLookahead!=YYNOCODE );
156990  yymsp[1].minor.yy0 = yyLookaheadToken;
156991}
156992        break;
156993      case 30: /* ccons ::= CONSTRAINT nm */
156994      case 65: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==65);
156995{pParse->constraintName = yymsp[0].minor.yy0;}
156996        break;
156997      case 31: /* ccons ::= DEFAULT scantok term */
156998{sqlite3AddDefaultValue(pParse,yymsp[0].minor.yy202,yymsp[-1].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);}
156999        break;
157000      case 32: /* ccons ::= DEFAULT LP expr RP */
157001{sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy202,yymsp[-2].minor.yy0.z+1,yymsp[0].minor.yy0.z);}
157002        break;
157003      case 33: /* ccons ::= DEFAULT PLUS scantok term */
157004{sqlite3AddDefaultValue(pParse,yymsp[0].minor.yy202,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);}
157005        break;
157006      case 34: /* ccons ::= DEFAULT MINUS scantok term */
157007{
157008  Expr *p = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy202, 0);
157009  sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);
157010}
157011        break;
157012      case 35: /* ccons ::= DEFAULT scantok ID|INDEXED */
157013{
157014  Expr *p = tokenExpr(pParse, TK_STRING, yymsp[0].minor.yy0);
157015  if( p ){
157016    sqlite3ExprIdToTrueFalse(p);
157017    testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
157018  }
157019    sqlite3AddDefaultValue(pParse,p,yymsp[0].minor.yy0.z,yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
157020}
157021        break;
157022      case 36: /* ccons ::= NOT NULL onconf */
157023{sqlite3AddNotNull(pParse, yymsp[0].minor.yy192);}
157024        break;
157025      case 37: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
157026{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy192,yymsp[0].minor.yy192,yymsp[-2].minor.yy192);}
157027        break;
157028      case 38: /* ccons ::= UNIQUE onconf */
157029{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy192,0,0,0,0,
157030                                   SQLITE_IDXTYPE_UNIQUE);}
157031        break;
157032      case 39: /* ccons ::= CHECK LP expr RP */
157033{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy202);}
157034        break;
157035      case 40: /* ccons ::= REFERENCES nm eidlist_opt refargs */
157036{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy242,yymsp[0].minor.yy192);}
157037        break;
157038      case 41: /* ccons ::= defer_subclause */
157039{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy192);}
157040        break;
157041      case 42: /* ccons ::= COLLATE ID|STRING */
157042{sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
157043        break;
157044      case 43: /* generated ::= LP expr RP */
157045{sqlite3AddGenerated(pParse,yymsp[-1].minor.yy202,0);}
157046        break;
157047      case 44: /* generated ::= LP expr RP ID */
157048{sqlite3AddGenerated(pParse,yymsp[-2].minor.yy202,&yymsp[0].minor.yy0);}
157049        break;
157050      case 47: /* refargs ::= */
157051{ yymsp[1].minor.yy192 = OE_None*0x0101; /* EV: R-19803-45884 */}
157052        break;
157053      case 48: /* refargs ::= refargs refarg */
157054{ yymsp[-1].minor.yy192 = (yymsp[-1].minor.yy192 & ~yymsp[0].minor.yy207.mask) | yymsp[0].minor.yy207.value; }
157055        break;
157056      case 49: /* refarg ::= MATCH nm */
157057{ yymsp[-1].minor.yy207.value = 0;     yymsp[-1].minor.yy207.mask = 0x000000; }
157058        break;
157059      case 50: /* refarg ::= ON INSERT refact */
157060{ yymsp[-2].minor.yy207.value = 0;     yymsp[-2].minor.yy207.mask = 0x000000; }
157061        break;
157062      case 51: /* refarg ::= ON DELETE refact */
157063{ yymsp[-2].minor.yy207.value = yymsp[0].minor.yy192;     yymsp[-2].minor.yy207.mask = 0x0000ff; }
157064        break;
157065      case 52: /* refarg ::= ON UPDATE refact */
157066{ yymsp[-2].minor.yy207.value = yymsp[0].minor.yy192<<8;  yymsp[-2].minor.yy207.mask = 0x00ff00; }
157067        break;
157068      case 53: /* refact ::= SET NULL */
157069{ yymsp[-1].minor.yy192 = OE_SetNull;  /* EV: R-33326-45252 */}
157070        break;
157071      case 54: /* refact ::= SET DEFAULT */
157072{ yymsp[-1].minor.yy192 = OE_SetDflt;  /* EV: R-33326-45252 */}
157073        break;
157074      case 55: /* refact ::= CASCADE */
157075{ yymsp[0].minor.yy192 = OE_Cascade;  /* EV: R-33326-45252 */}
157076        break;
157077      case 56: /* refact ::= RESTRICT */
157078{ yymsp[0].minor.yy192 = OE_Restrict; /* EV: R-33326-45252 */}
157079        break;
157080      case 57: /* refact ::= NO ACTION */
157081{ yymsp[-1].minor.yy192 = OE_None;     /* EV: R-33326-45252 */}
157082        break;
157083      case 58: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
157084{yymsp[-2].minor.yy192 = 0;}
157085        break;
157086      case 59: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
157087      case 74: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==74);
157088      case 162: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==162);
157089{yymsp[-1].minor.yy192 = yymsp[0].minor.yy192;}
157090        break;
157091      case 61: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */
157092      case 78: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==78);
157093      case 204: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==204);
157094      case 207: /* in_op ::= NOT IN */ yytestcase(yyruleno==207);
157095      case 233: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==233);
157096{yymsp[-1].minor.yy192 = 1;}
157097        break;
157098      case 62: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
157099{yymsp[-1].minor.yy192 = 0;}
157100        break;
157101      case 64: /* tconscomma ::= COMMA */
157102{pParse->constraintName.n = 0;}
157103        break;
157104      case 66: /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
157105{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy242,yymsp[0].minor.yy192,yymsp[-2].minor.yy192,0);}
157106        break;
157107      case 67: /* tcons ::= UNIQUE LP sortlist RP onconf */
157108{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy242,yymsp[0].minor.yy192,0,0,0,0,
157109                                       SQLITE_IDXTYPE_UNIQUE);}
157110        break;
157111      case 68: /* tcons ::= CHECK LP expr RP onconf */
157112{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy202);}
157113        break;
157114      case 69: /* tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
157115{
157116    sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy242, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy242, yymsp[-1].minor.yy192);
157117    sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy192);
157118}
157119        break;
157120      case 71: /* onconf ::= */
157121      case 73: /* orconf ::= */ yytestcase(yyruleno==73);
157122{yymsp[1].minor.yy192 = OE_Default;}
157123        break;
157124      case 72: /* onconf ::= ON CONFLICT resolvetype */
157125{yymsp[-2].minor.yy192 = yymsp[0].minor.yy192;}
157126        break;
157127      case 75: /* resolvetype ::= IGNORE */
157128{yymsp[0].minor.yy192 = OE_Ignore;}
157129        break;
157130      case 76: /* resolvetype ::= REPLACE */
157131      case 163: /* insert_cmd ::= REPLACE */ yytestcase(yyruleno==163);
157132{yymsp[0].minor.yy192 = OE_Replace;}
157133        break;
157134      case 77: /* cmd ::= DROP TABLE ifexists fullname */
157135{
157136  sqlite3DropTable(pParse, yymsp[0].minor.yy47, 0, yymsp[-1].minor.yy192);
157137}
157138        break;
157139      case 80: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
157140{
157141  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);
157142}
157143        break;
157144      case 81: /* cmd ::= DROP VIEW ifexists fullname */
157145{
157146  sqlite3DropTable(pParse, yymsp[0].minor.yy47, 1, yymsp[-1].minor.yy192);
157147}
157148        break;
157149      case 82: /* cmd ::= select */
157150{
157151  SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
157152  sqlite3Select(pParse, yymsp[0].minor.yy539, &dest);
157153  sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy539);
157154}
157155        break;
157156      case 83: /* select ::= WITH wqlist selectnowith */
157157{
157158  Select *p = yymsp[0].minor.yy539;
157159  if( p ){
157160    p->pWith = yymsp[-1].minor.yy131;
157161    parserDoubleLinkSelect(pParse, p);
157162  }else{
157163    sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy131);
157164  }
157165  yymsp[-2].minor.yy539 = p;
157166}
157167        break;
157168      case 84: /* select ::= WITH RECURSIVE wqlist selectnowith */
157169{
157170  Select *p = yymsp[0].minor.yy539;
157171  if( p ){
157172    p->pWith = yymsp[-1].minor.yy131;
157173    parserDoubleLinkSelect(pParse, p);
157174  }else{
157175    sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy131);
157176  }
157177  yymsp[-3].minor.yy539 = p;
157178}
157179        break;
157180      case 85: /* select ::= selectnowith */
157181{
157182  Select *p = yymsp[0].minor.yy539;
157183  if( p ){
157184    parserDoubleLinkSelect(pParse, p);
157185  }
157186  yymsp[0].minor.yy539 = p; /*A-overwrites-X*/
157187}
157188        break;
157189      case 86: /* selectnowith ::= selectnowith multiselect_op oneselect */
157190{
157191  Select *pRhs = yymsp[0].minor.yy539;
157192  Select *pLhs = yymsp[-2].minor.yy539;
157193  if( pRhs && pRhs->pPrior ){
157194    SrcList *pFrom;
157195    Token x;
157196    x.n = 0;
157197    parserDoubleLinkSelect(pParse, pRhs);
157198    pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
157199    pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
157200  }
157201  if( pRhs ){
157202    pRhs->op = (u8)yymsp[-1].minor.yy192;
157203    pRhs->pPrior = pLhs;
157204    if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
157205    pRhs->selFlags &= ~SF_MultiValue;
157206    if( yymsp[-1].minor.yy192!=TK_ALL ) pParse->hasCompound = 1;
157207  }else{
157208    sqlite3SelectDelete(pParse->db, pLhs);
157209  }
157210  yymsp[-2].minor.yy539 = pRhs;
157211}
157212        break;
157213      case 87: /* multiselect_op ::= UNION */
157214      case 89: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==89);
157215{yymsp[0].minor.yy192 = yymsp[0].major; /*A-overwrites-OP*/}
157216        break;
157217      case 88: /* multiselect_op ::= UNION ALL */
157218{yymsp[-1].minor.yy192 = TK_ALL;}
157219        break;
157220      case 90: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
157221{
157222  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);
157223}
157224        break;
157225      case 91: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
157226{
157227  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);
157228  if( yymsp[-9].minor.yy539 ){
157229    yymsp[-9].minor.yy539->pWinDefn = yymsp[-2].minor.yy303;
157230  }else{
157231    sqlite3WindowListDelete(pParse->db, yymsp[-2].minor.yy303);
157232  }
157233}
157234        break;
157235      case 92: /* values ::= VALUES LP nexprlist RP */
157236{
157237  yymsp[-3].minor.yy539 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy242,0,0,0,0,0,SF_Values,0);
157238}
157239        break;
157240      case 93: /* values ::= values COMMA LP nexprlist RP */
157241{
157242  Select *pRight, *pLeft = yymsp[-4].minor.yy539;
157243  pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy242,0,0,0,0,0,SF_Values|SF_MultiValue,0);
157244  if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
157245  if( pRight ){
157246    pRight->op = TK_ALL;
157247    pRight->pPrior = pLeft;
157248    yymsp[-4].minor.yy539 = pRight;
157249  }else{
157250    yymsp[-4].minor.yy539 = pLeft;
157251  }
157252}
157253        break;
157254      case 94: /* distinct ::= DISTINCT */
157255{yymsp[0].minor.yy192 = SF_Distinct;}
157256        break;
157257      case 95: /* distinct ::= ALL */
157258{yymsp[0].minor.yy192 = SF_All;}
157259        break;
157260      case 97: /* sclp ::= */
157261      case 130: /* orderby_opt ::= */ yytestcase(yyruleno==130);
157262      case 140: /* groupby_opt ::= */ yytestcase(yyruleno==140);
157263      case 220: /* exprlist ::= */ yytestcase(yyruleno==220);
157264      case 223: /* paren_exprlist ::= */ yytestcase(yyruleno==223);
157265      case 228: /* eidlist_opt ::= */ yytestcase(yyruleno==228);
157266{yymsp[1].minor.yy242 = 0;}
157267        break;
157268      case 98: /* selcollist ::= sclp scanpt expr scanpt as */
157269{
157270   yymsp[-4].minor.yy242 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy242, yymsp[-2].minor.yy202);
157271   if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy242, &yymsp[0].minor.yy0, 1);
157272   sqlite3ExprListSetSpan(pParse,yymsp[-4].minor.yy242,yymsp[-3].minor.yy436,yymsp[-1].minor.yy436);
157273}
157274        break;
157275      case 99: /* selcollist ::= sclp scanpt STAR */
157276{
157277  Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
157278  yymsp[-2].minor.yy242 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy242, p);
157279}
157280        break;
157281      case 100: /* selcollist ::= sclp scanpt nm DOT STAR */
157282{
157283  Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
157284  Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
157285  Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
157286  yymsp[-4].minor.yy242 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy242, pDot);
157287}
157288        break;
157289      case 101: /* as ::= AS nm */
157290      case 112: /* dbnm ::= DOT nm */ yytestcase(yyruleno==112);
157291      case 244: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==244);
157292      case 245: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==245);
157293{yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
157294        break;
157295      case 103: /* from ::= */
157296{yymsp[1].minor.yy47 = sqlite3DbMallocZero(pParse->db, sizeof(*yymsp[1].minor.yy47));}
157297        break;
157298      case 104: /* from ::= FROM seltablist */
157299{
157300  yymsp[-1].minor.yy47 = yymsp[0].minor.yy47;
157301  sqlite3SrcListShiftJoinType(yymsp[-1].minor.yy47);
157302}
157303        break;
157304      case 105: /* stl_prefix ::= seltablist joinop */
157305{
157306   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;
157307}
157308        break;
157309      case 106: /* stl_prefix ::= */
157310{yymsp[1].minor.yy47 = 0;}
157311        break;
157312      case 107: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
157313{
157314  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);
157315  sqlite3SrcListIndexedBy(pParse, yymsp[-6].minor.yy47, &yymsp[-2].minor.yy0);
157316}
157317        break;
157318      case 108: /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
157319{
157320  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);
157321  sqlite3SrcListFuncArgs(pParse, yymsp[-8].minor.yy47, yymsp[-4].minor.yy242);
157322}
157323        break;
157324      case 109: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
157325{
157326    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);
157327  }
157328        break;
157329      case 110: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
157330{
157331    if( yymsp[-6].minor.yy47==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy202==0 && yymsp[0].minor.yy600==0 ){
157332      yymsp[-6].minor.yy47 = yymsp[-4].minor.yy47;
157333    }else if( yymsp[-4].minor.yy47->nSrc==1 ){
157334      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);
157335      if( yymsp[-6].minor.yy47 ){
157336        struct SrcList_item *pNew = &yymsp[-6].minor.yy47->a[yymsp[-6].minor.yy47->nSrc-1];
157337        struct SrcList_item *pOld = yymsp[-4].minor.yy47->a;
157338        pNew->zName = pOld->zName;
157339        pNew->zDatabase = pOld->zDatabase;
157340        pNew->pSelect = pOld->pSelect;
157341        if( pOld->fg.isTabFunc ){
157342          pNew->u1.pFuncArg = pOld->u1.pFuncArg;
157343          pOld->u1.pFuncArg = 0;
157344          pOld->fg.isTabFunc = 0;
157345          pNew->fg.isTabFunc = 1;
157346        }
157347        pOld->zName = pOld->zDatabase = 0;
157348        pOld->pSelect = 0;
157349      }
157350      sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy47);
157351    }else{
157352      Select *pSubquery;
157353      sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy47);
157354      pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy47,0,0,0,0,SF_NestedFrom,0);
157355      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);
157356    }
157357  }
157358        break;
157359      case 111: /* dbnm ::= */
157360      case 125: /* indexed_opt ::= */ yytestcase(yyruleno==125);
157361{yymsp[1].minor.yy0.z=0; yymsp[1].minor.yy0.n=0;}
157362        break;
157363      case 113: /* fullname ::= nm */
157364{
157365  yylhsminor.yy47 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0);
157366  if( IN_RENAME_OBJECT && yylhsminor.yy47 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy47->a[0].zName, &yymsp[0].minor.yy0);
157367}
157368  yymsp[0].minor.yy47 = yylhsminor.yy47;
157369        break;
157370      case 114: /* fullname ::= nm DOT nm */
157371{
157372  yylhsminor.yy47 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
157373  if( IN_RENAME_OBJECT && yylhsminor.yy47 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy47->a[0].zName, &yymsp[0].minor.yy0);
157374}
157375  yymsp[-2].minor.yy47 = yylhsminor.yy47;
157376        break;
157377      case 115: /* xfullname ::= nm */
157378{yymsp[0].minor.yy47 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0); /*A-overwrites-X*/}
157379        break;
157380      case 116: /* xfullname ::= nm DOT nm */
157381{yymsp[-2].minor.yy47 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/}
157382        break;
157383      case 117: /* xfullname ::= nm DOT nm AS nm */
157384{
157385   yymsp[-4].minor.yy47 = sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,&yymsp[-2].minor.yy0); /*A-overwrites-X*/
157386   if( yymsp[-4].minor.yy47 ) yymsp[-4].minor.yy47->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
157387}
157388        break;
157389      case 118: /* xfullname ::= nm AS nm */
157390{
157391   yymsp[-2].minor.yy47 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,0); /*A-overwrites-X*/
157392   if( yymsp[-2].minor.yy47 ) yymsp[-2].minor.yy47->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
157393}
157394        break;
157395      case 119: /* joinop ::= COMMA|JOIN */
157396{ yymsp[0].minor.yy192 = JT_INNER; }
157397        break;
157398      case 120: /* joinop ::= JOIN_KW JOIN */
157399{yymsp[-1].minor.yy192 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0);  /*X-overwrites-A*/}
157400        break;
157401      case 121: /* joinop ::= JOIN_KW nm JOIN */
157402{yymsp[-2].minor.yy192 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-overwrites-A*/}
157403        break;
157404      case 122: /* joinop ::= JOIN_KW nm nm JOIN */
157405{yymsp[-3].minor.yy192 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);/*X-overwrites-A*/}
157406        break;
157407      case 123: /* on_opt ::= ON expr */
157408      case 143: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==143);
157409      case 150: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==150);
157410      case 216: /* case_else ::= ELSE expr */ yytestcase(yyruleno==216);
157411      case 237: /* vinto ::= INTO expr */ yytestcase(yyruleno==237);
157412{yymsp[-1].minor.yy202 = yymsp[0].minor.yy202;}
157413        break;
157414      case 124: /* on_opt ::= */
157415      case 142: /* having_opt ::= */ yytestcase(yyruleno==142);
157416      case 144: /* limit_opt ::= */ yytestcase(yyruleno==144);
157417      case 149: /* where_opt ::= */ yytestcase(yyruleno==149);
157418      case 217: /* case_else ::= */ yytestcase(yyruleno==217);
157419      case 219: /* case_operand ::= */ yytestcase(yyruleno==219);
157420      case 238: /* vinto ::= */ yytestcase(yyruleno==238);
157421{yymsp[1].minor.yy202 = 0;}
157422        break;
157423      case 126: /* indexed_opt ::= INDEXED BY nm */
157424{yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
157425        break;
157426      case 127: /* indexed_opt ::= NOT INDEXED */
157427{yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
157428        break;
157429      case 128: /* using_opt ::= USING LP idlist RP */
157430{yymsp[-3].minor.yy600 = yymsp[-1].minor.yy600;}
157431        break;
157432      case 129: /* using_opt ::= */
157433      case 164: /* idlist_opt ::= */ yytestcase(yyruleno==164);
157434{yymsp[1].minor.yy600 = 0;}
157435        break;
157436      case 131: /* orderby_opt ::= ORDER BY sortlist */
157437      case 141: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==141);
157438{yymsp[-2].minor.yy242 = yymsp[0].minor.yy242;}
157439        break;
157440      case 132: /* sortlist ::= sortlist COMMA expr sortorder nulls */
157441{
157442  yymsp[-4].minor.yy242 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy242,yymsp[-2].minor.yy202);
157443  sqlite3ExprListSetSortOrder(yymsp[-4].minor.yy242,yymsp[-1].minor.yy192,yymsp[0].minor.yy192);
157444}
157445        break;
157446      case 133: /* sortlist ::= expr sortorder nulls */
157447{
157448  yymsp[-2].minor.yy242 = sqlite3ExprListAppend(pParse,0,yymsp[-2].minor.yy202); /*A-overwrites-Y*/
157449  sqlite3ExprListSetSortOrder(yymsp[-2].minor.yy242,yymsp[-1].minor.yy192,yymsp[0].minor.yy192);
157450}
157451        break;
157452      case 134: /* sortorder ::= ASC */
157453{yymsp[0].minor.yy192 = SQLITE_SO_ASC;}
157454        break;
157455      case 135: /* sortorder ::= DESC */
157456{yymsp[0].minor.yy192 = SQLITE_SO_DESC;}
157457        break;
157458      case 136: /* sortorder ::= */
157459      case 139: /* nulls ::= */ yytestcase(yyruleno==139);
157460{yymsp[1].minor.yy192 = SQLITE_SO_UNDEFINED;}
157461        break;
157462      case 137: /* nulls ::= NULLS FIRST */
157463{yymsp[-1].minor.yy192 = SQLITE_SO_ASC;}
157464        break;
157465      case 138: /* nulls ::= NULLS LAST */
157466{yymsp[-1].minor.yy192 = SQLITE_SO_DESC;}
157467        break;
157468      case 145: /* limit_opt ::= LIMIT expr */
157469{yymsp[-1].minor.yy202 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy202,0);}
157470        break;
157471      case 146: /* limit_opt ::= LIMIT expr OFFSET expr */
157472{yymsp[-3].minor.yy202 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[-2].minor.yy202,yymsp[0].minor.yy202);}
157473        break;
157474      case 147: /* limit_opt ::= LIMIT expr COMMA expr */
157475{yymsp[-3].minor.yy202 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy202,yymsp[-2].minor.yy202);}
157476        break;
157477      case 148: /* cmd ::= with DELETE FROM xfullname indexed_opt where_opt */
157478{
157479  sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy47, &yymsp[-1].minor.yy0);
157480  sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy47,yymsp[0].minor.yy202,0,0);
157481}
157482        break;
157483      case 151: /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt */
157484{
157485  sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy47, &yymsp[-3].minor.yy0);
157486  sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy242,"set list");
157487  sqlite3Update(pParse,yymsp[-4].minor.yy47,yymsp[-1].minor.yy242,yymsp[0].minor.yy202,yymsp[-5].minor.yy192,0,0,0);
157488}
157489        break;
157490      case 152: /* setlist ::= setlist COMMA nm EQ expr */
157491{
157492  yymsp[-4].minor.yy242 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy242, yymsp[0].minor.yy202);
157493  sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy242, &yymsp[-2].minor.yy0, 1);
157494}
157495        break;
157496      case 153: /* setlist ::= setlist COMMA LP idlist RP EQ expr */
157497{
157498  yymsp[-6].minor.yy242 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy242, yymsp[-3].minor.yy600, yymsp[0].minor.yy202);
157499}
157500        break;
157501      case 154: /* setlist ::= nm EQ expr */
157502{
157503  yylhsminor.yy242 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy202);
157504  sqlite3ExprListSetName(pParse, yylhsminor.yy242, &yymsp[-2].minor.yy0, 1);
157505}
157506  yymsp[-2].minor.yy242 = yylhsminor.yy242;
157507        break;
157508      case 155: /* setlist ::= LP idlist RP EQ expr */
157509{
157510  yymsp[-4].minor.yy242 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy600, yymsp[0].minor.yy202);
157511}
157512        break;
157513      case 156: /* cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
157514{
157515  sqlite3Insert(pParse, yymsp[-3].minor.yy47, yymsp[-1].minor.yy539, yymsp[-2].minor.yy600, yymsp[-5].minor.yy192, yymsp[0].minor.yy318);
157516}
157517        break;
157518      case 157: /* cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES */
157519{
157520  sqlite3Insert(pParse, yymsp[-3].minor.yy47, 0, yymsp[-2].minor.yy600, yymsp[-5].minor.yy192, 0);
157521}
157522        break;
157523      case 158: /* upsert ::= */
157524{ yymsp[1].minor.yy318 = 0; }
157525        break;
157526      case 159: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt */
157527{ yymsp[-10].minor.yy318 = sqlite3UpsertNew(pParse->db,yymsp[-7].minor.yy242,yymsp[-5].minor.yy202,yymsp[-1].minor.yy242,yymsp[0].minor.yy202);}
157528        break;
157529      case 160: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING */
157530{ yymsp[-7].minor.yy318 = sqlite3UpsertNew(pParse->db,yymsp[-4].minor.yy242,yymsp[-2].minor.yy202,0,0); }
157531        break;
157532      case 161: /* upsert ::= ON CONFLICT DO NOTHING */
157533{ yymsp[-3].minor.yy318 = sqlite3UpsertNew(pParse->db,0,0,0,0); }
157534        break;
157535      case 165: /* idlist_opt ::= LP idlist RP */
157536{yymsp[-2].minor.yy600 = yymsp[-1].minor.yy600;}
157537        break;
157538      case 166: /* idlist ::= idlist COMMA nm */
157539{yymsp[-2].minor.yy600 = sqlite3IdListAppend(pParse,yymsp[-2].minor.yy600,&yymsp[0].minor.yy0);}
157540        break;
157541      case 167: /* idlist ::= nm */
157542{yymsp[0].minor.yy600 = sqlite3IdListAppend(pParse,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
157543        break;
157544      case 168: /* expr ::= LP expr RP */
157545{yymsp[-2].minor.yy202 = yymsp[-1].minor.yy202;}
157546        break;
157547      case 169: /* expr ::= ID|INDEXED */
157548      case 170: /* expr ::= JOIN_KW */ yytestcase(yyruleno==170);
157549{yymsp[0].minor.yy202=tokenExpr(pParse,TK_ID,yymsp[0].minor.yy0); /*A-overwrites-X*/}
157550        break;
157551      case 171: /* expr ::= nm DOT nm */
157552{
157553  Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
157554  Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1);
157555  if( IN_RENAME_OBJECT ){
157556    sqlite3RenameTokenMap(pParse, (void*)temp2, &yymsp[0].minor.yy0);
157557    sqlite3RenameTokenMap(pParse, (void*)temp1, &yymsp[-2].minor.yy0);
157558  }
157559  yylhsminor.yy202 = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
157560}
157561  yymsp[-2].minor.yy202 = yylhsminor.yy202;
157562        break;
157563      case 172: /* expr ::= nm DOT nm DOT nm */
157564{
157565  Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-4].minor.yy0, 1);
157566  Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
157567  Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1);
157568  Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
157569  if( IN_RENAME_OBJECT ){
157570    sqlite3RenameTokenMap(pParse, (void*)temp3, &yymsp[0].minor.yy0);
157571    sqlite3RenameTokenMap(pParse, (void*)temp2, &yymsp[-2].minor.yy0);
157572  }
157573  yylhsminor.yy202 = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
157574}
157575  yymsp[-4].minor.yy202 = yylhsminor.yy202;
157576        break;
157577      case 173: /* term ::= NULL|FLOAT|BLOB */
157578      case 174: /* term ::= STRING */ yytestcase(yyruleno==174);
157579{yymsp[0].minor.yy202=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
157580        break;
157581      case 175: /* term ::= INTEGER */
157582{
157583  yylhsminor.yy202 = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
157584}
157585  yymsp[0].minor.yy202 = yylhsminor.yy202;
157586        break;
157587      case 176: /* expr ::= VARIABLE */
157588{
157589  if( !(yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1])) ){
157590    u32 n = yymsp[0].minor.yy0.n;
157591    yymsp[0].minor.yy202 = tokenExpr(pParse, TK_VARIABLE, yymsp[0].minor.yy0);
157592    sqlite3ExprAssignVarNumber(pParse, yymsp[0].minor.yy202, n);
157593  }else{
157594    /* When doing a nested parse, one can include terms in an expression
157595    ** that look like this:   #1 #2 ...  These terms refer to registers
157596    ** in the virtual machine.  #N is the N-th register. */
157597    Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
157598    assert( t.n>=2 );
157599    if( pParse->nested==0 ){
157600      sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
157601      yymsp[0].minor.yy202 = 0;
157602    }else{
157603      yymsp[0].minor.yy202 = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
157604      if( yymsp[0].minor.yy202 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy202->iTable);
157605    }
157606  }
157607}
157608        break;
157609      case 177: /* expr ::= expr COLLATE ID|STRING */
157610{
157611  yymsp[-2].minor.yy202 = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy202, &yymsp[0].minor.yy0, 1);
157612}
157613        break;
157614      case 178: /* expr ::= CAST LP expr AS typetoken RP */
157615{
157616  yymsp[-5].minor.yy202 = sqlite3ExprAlloc(pParse->db, TK_CAST, &yymsp[-1].minor.yy0, 1);
157617  sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy202, yymsp[-3].minor.yy202, 0);
157618}
157619        break;
157620      case 179: /* expr ::= ID|INDEXED LP distinct exprlist RP */
157621{
157622  yylhsminor.yy202 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy242, &yymsp[-4].minor.yy0, yymsp[-2].minor.yy192);
157623}
157624  yymsp[-4].minor.yy202 = yylhsminor.yy202;
157625        break;
157626      case 180: /* expr ::= ID|INDEXED LP STAR RP */
157627{
157628  yylhsminor.yy202 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0, 0);
157629}
157630  yymsp[-3].minor.yy202 = yylhsminor.yy202;
157631        break;
157632      case 181: /* expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
157633{
157634  yylhsminor.yy202 = sqlite3ExprFunction(pParse, yymsp[-2].minor.yy242, &yymsp[-5].minor.yy0, yymsp[-3].minor.yy192);
157635  sqlite3WindowAttach(pParse, yylhsminor.yy202, yymsp[0].minor.yy303);
157636}
157637  yymsp[-5].minor.yy202 = yylhsminor.yy202;
157638        break;
157639      case 182: /* expr ::= ID|INDEXED LP STAR RP filter_over */
157640{
157641  yylhsminor.yy202 = sqlite3ExprFunction(pParse, 0, &yymsp[-4].minor.yy0, 0);
157642  sqlite3WindowAttach(pParse, yylhsminor.yy202, yymsp[0].minor.yy303);
157643}
157644  yymsp[-4].minor.yy202 = yylhsminor.yy202;
157645        break;
157646      case 183: /* term ::= CTIME_KW */
157647{
157648  yylhsminor.yy202 = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0, 0);
157649}
157650  yymsp[0].minor.yy202 = yylhsminor.yy202;
157651        break;
157652      case 184: /* expr ::= LP nexprlist COMMA expr RP */
157653{
157654  ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy242, yymsp[-1].minor.yy202);
157655  yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
157656  if( yymsp[-4].minor.yy202 ){
157657    yymsp[-4].minor.yy202->x.pList = pList;
157658    if( ALWAYS(pList->nExpr) ){
157659      yymsp[-4].minor.yy202->flags |= pList->a[0].pExpr->flags & EP_Propagate;
157660    }
157661  }else{
157662    sqlite3ExprListDelete(pParse->db, pList);
157663  }
157664}
157665        break;
157666      case 185: /* expr ::= expr AND expr */
157667{yymsp[-2].minor.yy202=sqlite3ExprAnd(pParse,yymsp[-2].minor.yy202,yymsp[0].minor.yy202);}
157668        break;
157669      case 186: /* expr ::= expr OR expr */
157670      case 187: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==187);
157671      case 188: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==188);
157672      case 189: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==189);
157673      case 190: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==190);
157674      case 191: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==191);
157675      case 192: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==192);
157676{yymsp[-2].minor.yy202=sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy202,yymsp[0].minor.yy202);}
157677        break;
157678      case 193: /* likeop ::= NOT LIKE_KW|MATCH */
157679{yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-overwrite-yymsp[0].minor.yy0*/}
157680        break;
157681      case 194: /* expr ::= expr likeop expr */
157682{
157683  ExprList *pList;
157684  int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
157685  yymsp[-1].minor.yy0.n &= 0x7fffffff;
157686  pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy202);
157687  pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy202);
157688  yymsp[-2].minor.yy202 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
157689  if( bNot ) yymsp[-2].minor.yy202 = sqlite3PExpr(pParse, TK_NOT, yymsp[-2].minor.yy202, 0);
157690  if( yymsp[-2].minor.yy202 ) yymsp[-2].minor.yy202->flags |= EP_InfixFunc;
157691}
157692        break;
157693      case 195: /* expr ::= expr likeop expr ESCAPE expr */
157694{
157695  ExprList *pList;
157696  int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
157697  yymsp[-3].minor.yy0.n &= 0x7fffffff;
157698  pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy202);
157699  pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy202);
157700  pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy202);
157701  yymsp[-4].minor.yy202 = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0, 0);
157702  if( bNot ) yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy202, 0);
157703  if( yymsp[-4].minor.yy202 ) yymsp[-4].minor.yy202->flags |= EP_InfixFunc;
157704}
157705        break;
157706      case 196: /* expr ::= expr ISNULL|NOTNULL */
157707{yymsp[-1].minor.yy202 = sqlite3PExpr(pParse,yymsp[0].major,yymsp[-1].minor.yy202,0);}
157708        break;
157709      case 197: /* expr ::= expr NOT NULL */
157710{yymsp[-2].minor.yy202 = sqlite3PExpr(pParse,TK_NOTNULL,yymsp[-2].minor.yy202,0);}
157711        break;
157712      case 198: /* expr ::= expr IS expr */
157713{
157714  yymsp[-2].minor.yy202 = sqlite3PExpr(pParse,TK_IS,yymsp[-2].minor.yy202,yymsp[0].minor.yy202);
157715  binaryToUnaryIfNull(pParse, yymsp[0].minor.yy202, yymsp[-2].minor.yy202, TK_ISNULL);
157716}
157717        break;
157718      case 199: /* expr ::= expr IS NOT expr */
157719{
157720  yymsp[-3].minor.yy202 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-3].minor.yy202,yymsp[0].minor.yy202);
157721  binaryToUnaryIfNull(pParse, yymsp[0].minor.yy202, yymsp[-3].minor.yy202, TK_NOTNULL);
157722}
157723        break;
157724      case 200: /* expr ::= NOT expr */
157725      case 201: /* expr ::= BITNOT expr */ yytestcase(yyruleno==201);
157726{yymsp[-1].minor.yy202 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy202, 0);/*A-overwrites-B*/}
157727        break;
157728      case 202: /* expr ::= PLUS|MINUS expr */
157729{
157730  yymsp[-1].minor.yy202 = sqlite3PExpr(pParse, yymsp[-1].major==TK_PLUS ? TK_UPLUS : TK_UMINUS, yymsp[0].minor.yy202, 0);
157731  /*A-overwrites-B*/
157732}
157733        break;
157734      case 203: /* between_op ::= BETWEEN */
157735      case 206: /* in_op ::= IN */ yytestcase(yyruleno==206);
157736{yymsp[0].minor.yy192 = 0;}
157737        break;
157738      case 205: /* expr ::= expr between_op expr AND expr */
157739{
157740  ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy202);
157741  pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy202);
157742  yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy202, 0);
157743  if( yymsp[-4].minor.yy202 ){
157744    yymsp[-4].minor.yy202->x.pList = pList;
157745  }else{
157746    sqlite3ExprListDelete(pParse->db, pList);
157747  }
157748  if( yymsp[-3].minor.yy192 ) yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy202, 0);
157749}
157750        break;
157751      case 208: /* expr ::= expr in_op LP exprlist RP */
157752{
157753    if( yymsp[-1].minor.yy242==0 ){
157754      /* Expressions of the form
157755      **
157756      **      expr1 IN ()
157757      **      expr1 NOT IN ()
157758      **
157759      ** simplify to constants 0 (false) and 1 (true), respectively,
157760      ** regardless of the value of expr1.
157761      */
157762      sqlite3ExprUnmapAndDelete(pParse, yymsp[-4].minor.yy202);
157763      yymsp[-4].minor.yy202 = sqlite3Expr(pParse->db, TK_INTEGER, yymsp[-3].minor.yy192 ? "1" : "0");
157764    }else if( yymsp[-1].minor.yy242->nExpr==1 && sqlite3ExprIsConstant(yymsp[-1].minor.yy242->a[0].pExpr) ){
157765      Expr *pRHS = yymsp[-1].minor.yy242->a[0].pExpr;
157766      yymsp[-1].minor.yy242->a[0].pExpr = 0;
157767      sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy242);
157768      pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0);
157769      yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_EQ, yymsp[-4].minor.yy202, pRHS);
157770      if( yymsp[-3].minor.yy192 ) yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy202, 0);
157771    }else{
157772      yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy202, 0);
157773      if( yymsp[-4].minor.yy202 ){
157774        yymsp[-4].minor.yy202->x.pList = yymsp[-1].minor.yy242;
157775        sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy202);
157776      }else{
157777        sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy242);
157778      }
157779      if( yymsp[-3].minor.yy192 ) yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy202, 0);
157780    }
157781  }
157782        break;
157783      case 209: /* expr ::= LP select RP */
157784{
157785    yymsp[-2].minor.yy202 = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
157786    sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy202, yymsp[-1].minor.yy539);
157787  }
157788        break;
157789      case 210: /* expr ::= expr in_op LP select RP */
157790{
157791    yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy202, 0);
157792    sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy202, yymsp[-1].minor.yy539);
157793    if( yymsp[-3].minor.yy192 ) yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy202, 0);
157794  }
157795        break;
157796      case 211: /* expr ::= expr in_op nm dbnm paren_exprlist */
157797{
157798    SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
157799    Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
157800    if( yymsp[0].minor.yy242 )  sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, yymsp[0].minor.yy242);
157801    yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy202, 0);
157802    sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy202, pSelect);
157803    if( yymsp[-3].minor.yy192 ) yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy202, 0);
157804  }
157805        break;
157806      case 212: /* expr ::= EXISTS LP select RP */
157807{
157808    Expr *p;
157809    p = yymsp[-3].minor.yy202 = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
157810    sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy539);
157811  }
157812        break;
157813      case 213: /* expr ::= CASE case_operand case_exprlist case_else END */
157814{
157815  yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy202, 0);
157816  if( yymsp[-4].minor.yy202 ){
157817    yymsp[-4].minor.yy202->x.pList = yymsp[-1].minor.yy202 ? sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy242,yymsp[-1].minor.yy202) : yymsp[-2].minor.yy242;
157818    sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy202);
157819  }else{
157820    sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy242);
157821    sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy202);
157822  }
157823}
157824        break;
157825      case 214: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
157826{
157827  yymsp[-4].minor.yy242 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy242, yymsp[-2].minor.yy202);
157828  yymsp[-4].minor.yy242 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy242, yymsp[0].minor.yy202);
157829}
157830        break;
157831      case 215: /* case_exprlist ::= WHEN expr THEN expr */
157832{
157833  yymsp[-3].minor.yy242 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy202);
157834  yymsp[-3].minor.yy242 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy242, yymsp[0].minor.yy202);
157835}
157836        break;
157837      case 218: /* case_operand ::= expr */
157838{yymsp[0].minor.yy202 = yymsp[0].minor.yy202; /*A-overwrites-X*/}
157839        break;
157840      case 221: /* nexprlist ::= nexprlist COMMA expr */
157841{yymsp[-2].minor.yy242 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy242,yymsp[0].minor.yy202);}
157842        break;
157843      case 222: /* nexprlist ::= expr */
157844{yymsp[0].minor.yy242 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy202); /*A-overwrites-Y*/}
157845        break;
157846      case 224: /* paren_exprlist ::= LP exprlist RP */
157847      case 229: /* eidlist_opt ::= LP eidlist RP */ yytestcase(yyruleno==229);
157848{yymsp[-2].minor.yy242 = yymsp[-1].minor.yy242;}
157849        break;
157850      case 225: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
157851{
157852  sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
157853                     sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy242, yymsp[-10].minor.yy192,
157854                      &yymsp[-11].minor.yy0, yymsp[0].minor.yy202, SQLITE_SO_ASC, yymsp[-8].minor.yy192, SQLITE_IDXTYPE_APPDEF);
157855  if( IN_RENAME_OBJECT && pParse->pNewIndex ){
157856    sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &yymsp[-4].minor.yy0);
157857  }
157858}
157859        break;
157860      case 226: /* uniqueflag ::= UNIQUE */
157861      case 268: /* raisetype ::= ABORT */ yytestcase(yyruleno==268);
157862{yymsp[0].minor.yy192 = OE_Abort;}
157863        break;
157864      case 227: /* uniqueflag ::= */
157865{yymsp[1].minor.yy192 = OE_None;}
157866        break;
157867      case 230: /* eidlist ::= eidlist COMMA nm collate sortorder */
157868{
157869  yymsp[-4].minor.yy242 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy242, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy192, yymsp[0].minor.yy192);
157870}
157871        break;
157872      case 231: /* eidlist ::= nm collate sortorder */
157873{
157874  yymsp[-2].minor.yy242 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy192, yymsp[0].minor.yy192); /*A-overwrites-Y*/
157875}
157876        break;
157877      case 234: /* cmd ::= DROP INDEX ifexists fullname */
157878{sqlite3DropIndex(pParse, yymsp[0].minor.yy47, yymsp[-1].minor.yy192);}
157879        break;
157880      case 235: /* cmd ::= VACUUM vinto */
157881{sqlite3Vacuum(pParse,0,yymsp[0].minor.yy202);}
157882        break;
157883      case 236: /* cmd ::= VACUUM nm vinto */
157884{sqlite3Vacuum(pParse,&yymsp[-1].minor.yy0,yymsp[0].minor.yy202);}
157885        break;
157886      case 239: /* cmd ::= PRAGMA nm dbnm */
157887{sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
157888        break;
157889      case 240: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
157890{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
157891        break;
157892      case 241: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
157893{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
157894        break;
157895      case 242: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
157896{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
157897        break;
157898      case 243: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
157899{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
157900        break;
157901      case 246: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
157902{
157903  Token all;
157904  all.z = yymsp[-3].minor.yy0.z;
157905  all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
157906  sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy447, &all);
157907}
157908        break;
157909      case 247: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
157910{
157911  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);
157912  yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-overwrites-T*/
157913}
157914        break;
157915      case 248: /* trigger_time ::= BEFORE|AFTER */
157916{ yymsp[0].minor.yy192 = yymsp[0].major; /*A-overwrites-X*/ }
157917        break;
157918      case 249: /* trigger_time ::= INSTEAD OF */
157919{ yymsp[-1].minor.yy192 = TK_INSTEAD;}
157920        break;
157921      case 250: /* trigger_time ::= */
157922{ yymsp[1].minor.yy192 = TK_BEFORE; }
157923        break;
157924      case 251: /* trigger_event ::= DELETE|INSERT */
157925      case 252: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==252);
157926{yymsp[0].minor.yy230.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy230.b = 0;}
157927        break;
157928      case 253: /* trigger_event ::= UPDATE OF idlist */
157929{yymsp[-2].minor.yy230.a = TK_UPDATE; yymsp[-2].minor.yy230.b = yymsp[0].minor.yy600;}
157930        break;
157931      case 254: /* when_clause ::= */
157932      case 273: /* key_opt ::= */ yytestcase(yyruleno==273);
157933{ yymsp[1].minor.yy202 = 0; }
157934        break;
157935      case 255: /* when_clause ::= WHEN expr */
157936      case 274: /* key_opt ::= KEY expr */ yytestcase(yyruleno==274);
157937{ yymsp[-1].minor.yy202 = yymsp[0].minor.yy202; }
157938        break;
157939      case 256: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
157940{
157941  assert( yymsp[-2].minor.yy447!=0 );
157942  yymsp[-2].minor.yy447->pLast->pNext = yymsp[-1].minor.yy447;
157943  yymsp[-2].minor.yy447->pLast = yymsp[-1].minor.yy447;
157944}
157945        break;
157946      case 257: /* trigger_cmd_list ::= trigger_cmd SEMI */
157947{
157948  assert( yymsp[-1].minor.yy447!=0 );
157949  yymsp[-1].minor.yy447->pLast = yymsp[-1].minor.yy447;
157950}
157951        break;
157952      case 258: /* trnm ::= nm DOT nm */
157953{
157954  yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
157955  sqlite3ErrorMsg(pParse,
157956        "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
157957        "statements within triggers");
157958}
157959        break;
157960      case 259: /* tridxby ::= INDEXED BY nm */
157961{
157962  sqlite3ErrorMsg(pParse,
157963        "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
157964        "within triggers");
157965}
157966        break;
157967      case 260: /* tridxby ::= NOT INDEXED */
157968{
157969  sqlite3ErrorMsg(pParse,
157970        "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
157971        "within triggers");
157972}
157973        break;
157974      case 261: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt */
157975{yylhsminor.yy447 = sqlite3TriggerUpdateStep(pParse, &yymsp[-5].minor.yy0, yymsp[-2].minor.yy242, yymsp[-1].minor.yy202, yymsp[-6].minor.yy192, yymsp[-7].minor.yy0.z, yymsp[0].minor.yy436);}
157976  yymsp[-7].minor.yy447 = yylhsminor.yy447;
157977        break;
157978      case 262: /* trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
157979{
157980   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*/
157981}
157982  yymsp[-7].minor.yy447 = yylhsminor.yy447;
157983        break;
157984      case 263: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
157985{yylhsminor.yy447 = sqlite3TriggerDeleteStep(pParse, &yymsp[-3].minor.yy0, yymsp[-1].minor.yy202, yymsp[-5].minor.yy0.z, yymsp[0].minor.yy436);}
157986  yymsp[-5].minor.yy447 = yylhsminor.yy447;
157987        break;
157988      case 264: /* trigger_cmd ::= scanpt select scanpt */
157989{yylhsminor.yy447 = sqlite3TriggerSelectStep(pParse->db, yymsp[-1].minor.yy539, yymsp[-2].minor.yy436, yymsp[0].minor.yy436); /*yylhsminor.yy447-overwrites-yymsp[-1].minor.yy539*/}
157990  yymsp[-2].minor.yy447 = yylhsminor.yy447;
157991        break;
157992      case 265: /* expr ::= RAISE LP IGNORE RP */
157993{
157994  yymsp[-3].minor.yy202 = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
157995  if( yymsp[-3].minor.yy202 ){
157996    yymsp[-3].minor.yy202->affExpr = OE_Ignore;
157997  }
157998}
157999        break;
158000      case 266: /* expr ::= RAISE LP raisetype COMMA nm RP */
158001{
158002  yymsp[-5].minor.yy202 = sqlite3ExprAlloc(pParse->db, TK_RAISE, &yymsp[-1].minor.yy0, 1);
158003  if( yymsp[-5].minor.yy202 ) {
158004    yymsp[-5].minor.yy202->affExpr = (char)yymsp[-3].minor.yy192;
158005  }
158006}
158007        break;
158008      case 267: /* raisetype ::= ROLLBACK */
158009{yymsp[0].minor.yy192 = OE_Rollback;}
158010        break;
158011      case 269: /* raisetype ::= FAIL */
158012{yymsp[0].minor.yy192 = OE_Fail;}
158013        break;
158014      case 270: /* cmd ::= DROP TRIGGER ifexists fullname */
158015{
158016  sqlite3DropTrigger(pParse,yymsp[0].minor.yy47,yymsp[-1].minor.yy192);
158017}
158018        break;
158019      case 271: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
158020{
158021  sqlite3Attach(pParse, yymsp[-3].minor.yy202, yymsp[-1].minor.yy202, yymsp[0].minor.yy202);
158022}
158023        break;
158024      case 272: /* cmd ::= DETACH database_kw_opt expr */
158025{
158026  sqlite3Detach(pParse, yymsp[0].minor.yy202);
158027}
158028        break;
158029      case 275: /* cmd ::= REINDEX */
158030{sqlite3Reindex(pParse, 0, 0);}
158031        break;
158032      case 276: /* cmd ::= REINDEX nm dbnm */
158033{sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
158034        break;
158035      case 277: /* cmd ::= ANALYZE */
158036{sqlite3Analyze(pParse, 0, 0);}
158037        break;
158038      case 278: /* cmd ::= ANALYZE nm dbnm */
158039{sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
158040        break;
158041      case 279: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
158042{
158043  sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy47,&yymsp[0].minor.yy0);
158044}
158045        break;
158046      case 280: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
158047{
158048  yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
158049  sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
158050}
158051        break;
158052      case 281: /* add_column_fullname ::= fullname */
158053{
158054  disableLookaside(pParse);
158055  sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy47);
158056}
158057        break;
158058      case 282: /* cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
158059{
158060  sqlite3AlterRenameColumn(pParse, yymsp[-5].minor.yy47, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
158061}
158062        break;
158063      case 283: /* cmd ::= create_vtab */
158064{sqlite3VtabFinishParse(pParse,0);}
158065        break;
158066      case 284: /* cmd ::= create_vtab LP vtabarglist RP */
158067{sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
158068        break;
158069      case 285: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
158070{
158071    sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy192);
158072}
158073        break;
158074      case 286: /* vtabarg ::= */
158075{sqlite3VtabArgInit(pParse);}
158076        break;
158077      case 287: /* vtabargtoken ::= ANY */
158078      case 288: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==288);
158079      case 289: /* lp ::= LP */ yytestcase(yyruleno==289);
158080{sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
158081        break;
158082      case 290: /* with ::= WITH wqlist */
158083      case 291: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==291);
158084{ sqlite3WithPush(pParse, yymsp[0].minor.yy131, 1); }
158085        break;
158086      case 292: /* wqlist ::= nm eidlist_opt AS LP select RP */
158087{
158088  yymsp[-5].minor.yy131 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy242, yymsp[-1].minor.yy539); /*A-overwrites-X*/
158089}
158090        break;
158091      case 293: /* wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
158092{
158093  yymsp[-7].minor.yy131 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy131, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy242, yymsp[-1].minor.yy539);
158094}
158095        break;
158096      case 294: /* windowdefn_list ::= windowdefn */
158097{ yylhsminor.yy303 = yymsp[0].minor.yy303; }
158098  yymsp[0].minor.yy303 = yylhsminor.yy303;
158099        break;
158100      case 295: /* windowdefn_list ::= windowdefn_list COMMA windowdefn */
158101{
158102  assert( yymsp[0].minor.yy303!=0 );
158103  sqlite3WindowChain(pParse, yymsp[0].minor.yy303, yymsp[-2].minor.yy303);
158104  yymsp[0].minor.yy303->pNextWin = yymsp[-2].minor.yy303;
158105  yylhsminor.yy303 = yymsp[0].minor.yy303;
158106}
158107  yymsp[-2].minor.yy303 = yylhsminor.yy303;
158108        break;
158109      case 296: /* windowdefn ::= nm AS LP window RP */
158110{
158111  if( ALWAYS(yymsp[-1].minor.yy303) ){
158112    yymsp[-1].minor.yy303->zName = sqlite3DbStrNDup(pParse->db, yymsp[-4].minor.yy0.z, yymsp[-4].minor.yy0.n);
158113  }
158114  yylhsminor.yy303 = yymsp[-1].minor.yy303;
158115}
158116  yymsp[-4].minor.yy303 = yylhsminor.yy303;
158117        break;
158118      case 297: /* window ::= PARTITION BY nexprlist orderby_opt frame_opt */
158119{
158120  yymsp[-4].minor.yy303 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy303, yymsp[-2].minor.yy242, yymsp[-1].minor.yy242, 0);
158121}
158122        break;
158123      case 298: /* window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
158124{
158125  yylhsminor.yy303 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy303, yymsp[-2].minor.yy242, yymsp[-1].minor.yy242, &yymsp[-5].minor.yy0);
158126}
158127  yymsp[-5].minor.yy303 = yylhsminor.yy303;
158128        break;
158129      case 299: /* window ::= ORDER BY sortlist frame_opt */
158130{
158131  yymsp[-3].minor.yy303 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy303, 0, yymsp[-1].minor.yy242, 0);
158132}
158133        break;
158134      case 300: /* window ::= nm ORDER BY sortlist frame_opt */
158135{
158136  yylhsminor.yy303 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy303, 0, yymsp[-1].minor.yy242, &yymsp[-4].minor.yy0);
158137}
158138  yymsp[-4].minor.yy303 = yylhsminor.yy303;
158139        break;
158140      case 301: /* window ::= frame_opt */
158141      case 320: /* filter_over ::= over_clause */ yytestcase(yyruleno==320);
158142{
158143  yylhsminor.yy303 = yymsp[0].minor.yy303;
158144}
158145  yymsp[0].minor.yy303 = yylhsminor.yy303;
158146        break;
158147      case 302: /* window ::= nm frame_opt */
158148{
158149  yylhsminor.yy303 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy303, 0, 0, &yymsp[-1].minor.yy0);
158150}
158151  yymsp[-1].minor.yy303 = yylhsminor.yy303;
158152        break;
158153      case 303: /* frame_opt ::= */
158154{
158155  yymsp[1].minor.yy303 = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0);
158156}
158157        break;
158158      case 304: /* frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
158159{
158160  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);
158161}
158162  yymsp[-2].minor.yy303 = yylhsminor.yy303;
158163        break;
158164      case 305: /* frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
158165{
158166  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);
158167}
158168  yymsp[-5].minor.yy303 = yylhsminor.yy303;
158169        break;
158170      case 307: /* frame_bound_s ::= frame_bound */
158171      case 309: /* frame_bound_e ::= frame_bound */ yytestcase(yyruleno==309);
158172{yylhsminor.yy77 = yymsp[0].minor.yy77;}
158173  yymsp[0].minor.yy77 = yylhsminor.yy77;
158174        break;
158175      case 308: /* frame_bound_s ::= UNBOUNDED PRECEDING */
158176      case 310: /* frame_bound_e ::= UNBOUNDED FOLLOWING */ yytestcase(yyruleno==310);
158177      case 312: /* frame_bound ::= CURRENT ROW */ yytestcase(yyruleno==312);
158178{yylhsminor.yy77.eType = yymsp[-1].major; yylhsminor.yy77.pExpr = 0;}
158179  yymsp[-1].minor.yy77 = yylhsminor.yy77;
158180        break;
158181      case 311: /* frame_bound ::= expr PRECEDING|FOLLOWING */
158182{yylhsminor.yy77.eType = yymsp[0].major; yylhsminor.yy77.pExpr = yymsp[-1].minor.yy202;}
158183  yymsp[-1].minor.yy77 = yylhsminor.yy77;
158184        break;
158185      case 313: /* frame_exclude_opt ::= */
158186{yymsp[1].minor.yy58 = 0;}
158187        break;
158188      case 314: /* frame_exclude_opt ::= EXCLUDE frame_exclude */
158189{yymsp[-1].minor.yy58 = yymsp[0].minor.yy58;}
158190        break;
158191      case 315: /* frame_exclude ::= NO OTHERS */
158192      case 316: /* frame_exclude ::= CURRENT ROW */ yytestcase(yyruleno==316);
158193{yymsp[-1].minor.yy58 = yymsp[-1].major; /*A-overwrites-X*/}
158194        break;
158195      case 317: /* frame_exclude ::= GROUP|TIES */
158196{yymsp[0].minor.yy58 = yymsp[0].major; /*A-overwrites-X*/}
158197        break;
158198      case 318: /* window_clause ::= WINDOW windowdefn_list */
158199{ yymsp[-1].minor.yy303 = yymsp[0].minor.yy303; }
158200        break;
158201      case 319: /* filter_over ::= filter_clause over_clause */
158202{
158203  yymsp[0].minor.yy303->pFilter = yymsp[-1].minor.yy202;
158204  yylhsminor.yy303 = yymsp[0].minor.yy303;
158205}
158206  yymsp[-1].minor.yy303 = yylhsminor.yy303;
158207        break;
158208      case 321: /* filter_over ::= filter_clause */
158209{
158210  yylhsminor.yy303 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
158211  if( yylhsminor.yy303 ){
158212    yylhsminor.yy303->eFrmType = TK_FILTER;
158213    yylhsminor.yy303->pFilter = yymsp[0].minor.yy202;
158214  }else{
158215    sqlite3ExprDelete(pParse->db, yymsp[0].minor.yy202);
158216  }
158217}
158218  yymsp[0].minor.yy303 = yylhsminor.yy303;
158219        break;
158220      case 322: /* over_clause ::= OVER LP window RP */
158221{
158222  yymsp[-3].minor.yy303 = yymsp[-1].minor.yy303;
158223  assert( yymsp[-3].minor.yy303!=0 );
158224}
158225        break;
158226      case 323: /* over_clause ::= OVER nm */
158227{
158228  yymsp[-1].minor.yy303 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
158229  if( yymsp[-1].minor.yy303 ){
158230    yymsp[-1].minor.yy303->zName = sqlite3DbStrNDup(pParse->db, yymsp[0].minor.yy0.z, yymsp[0].minor.yy0.n);
158231  }
158232}
158233        break;
158234      case 324: /* filter_clause ::= FILTER LP WHERE expr RP */
158235{ yymsp[-4].minor.yy202 = yymsp[-1].minor.yy202; }
158236        break;
158237      default:
158238      /* (325) input ::= cmdlist */ yytestcase(yyruleno==325);
158239      /* (326) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==326);
158240      /* (327) cmdlist ::= ecmd (OPTIMIZED OUT) */ assert(yyruleno!=327);
158241      /* (328) ecmd ::= SEMI */ yytestcase(yyruleno==328);
158242      /* (329) ecmd ::= cmdx SEMI */ yytestcase(yyruleno==329);
158243      /* (330) ecmd ::= explain cmdx SEMI (NEVER REDUCES) */ assert(yyruleno!=330);
158244      /* (331) trans_opt ::= */ yytestcase(yyruleno==331);
158245      /* (332) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==332);
158246      /* (333) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==333);
158247      /* (334) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==334);
158248      /* (335) savepoint_opt ::= */ yytestcase(yyruleno==335);
158249      /* (336) cmd ::= create_table create_table_args */ yytestcase(yyruleno==336);
158250      /* (337) columnlist ::= columnlist COMMA columnname carglist */ yytestcase(yyruleno==337);
158251      /* (338) columnlist ::= columnname carglist */ yytestcase(yyruleno==338);
158252      /* (339) nm ::= ID|INDEXED */ yytestcase(yyruleno==339);
158253      /* (340) nm ::= STRING */ yytestcase(yyruleno==340);
158254      /* (341) nm ::= JOIN_KW */ yytestcase(yyruleno==341);
158255      /* (342) typetoken ::= typename */ yytestcase(yyruleno==342);
158256      /* (343) typename ::= ID|STRING */ yytestcase(yyruleno==343);
158257      /* (344) signed ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=344);
158258      /* (345) signed ::= minus_num (OPTIMIZED OUT) */ assert(yyruleno!=345);
158259      /* (346) carglist ::= carglist ccons */ yytestcase(yyruleno==346);
158260      /* (347) carglist ::= */ yytestcase(yyruleno==347);
158261      /* (348) ccons ::= NULL onconf */ yytestcase(yyruleno==348);
158262      /* (349) ccons ::= GENERATED ALWAYS AS generated */ yytestcase(yyruleno==349);
158263      /* (350) ccons ::= AS generated */ yytestcase(yyruleno==350);
158264      /* (351) conslist_opt ::= COMMA conslist */ yytestcase(yyruleno==351);
158265      /* (352) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==352);
158266      /* (353) conslist ::= tcons (OPTIMIZED OUT) */ assert(yyruleno!=353);
158267      /* (354) tconscomma ::= */ yytestcase(yyruleno==354);
158268      /* (355) defer_subclause_opt ::= defer_subclause (OPTIMIZED OUT) */ assert(yyruleno!=355);
158269      /* (356) resolvetype ::= raisetype (OPTIMIZED OUT) */ assert(yyruleno!=356);
158270      /* (357) selectnowith ::= oneselect (OPTIMIZED OUT) */ assert(yyruleno!=357);
158271      /* (358) oneselect ::= values */ yytestcase(yyruleno==358);
158272      /* (359) sclp ::= selcollist COMMA */ yytestcase(yyruleno==359);
158273      /* (360) as ::= ID|STRING */ yytestcase(yyruleno==360);
158274      /* (361) expr ::= term (OPTIMIZED OUT) */ assert(yyruleno!=361);
158275      /* (362) likeop ::= LIKE_KW|MATCH */ yytestcase(yyruleno==362);
158276      /* (363) exprlist ::= nexprlist */ yytestcase(yyruleno==363);
158277      /* (364) nmnum ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=364);
158278      /* (365) nmnum ::= nm (OPTIMIZED OUT) */ assert(yyruleno!=365);
158279      /* (366) nmnum ::= ON */ yytestcase(yyruleno==366);
158280      /* (367) nmnum ::= DELETE */ yytestcase(yyruleno==367);
158281      /* (368) nmnum ::= DEFAULT */ yytestcase(yyruleno==368);
158282      /* (369) plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==369);
158283      /* (370) foreach_clause ::= */ yytestcase(yyruleno==370);
158284      /* (371) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==371);
158285      /* (372) trnm ::= nm */ yytestcase(yyruleno==372);
158286      /* (373) tridxby ::= */ yytestcase(yyruleno==373);
158287      /* (374) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==374);
158288      /* (375) database_kw_opt ::= */ yytestcase(yyruleno==375);
158289      /* (376) kwcolumn_opt ::= */ yytestcase(yyruleno==376);
158290      /* (377) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==377);
158291      /* (378) vtabarglist ::= vtabarg */ yytestcase(yyruleno==378);
158292      /* (379) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==379);
158293      /* (380) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==380);
158294      /* (381) anylist ::= */ yytestcase(yyruleno==381);
158295      /* (382) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==382);
158296      /* (383) anylist ::= anylist ANY */ yytestcase(yyruleno==383);
158297      /* (384) with ::= */ yytestcase(yyruleno==384);
158298        break;
158299/********** End reduce actions ************************************************/
158300  };
158301  assert( yyruleno<sizeof(yyRuleInfoLhs)/sizeof(yyRuleInfoLhs[0]) );
158302  yygoto = yyRuleInfoLhs[yyruleno];
158303  yysize = yyRuleInfoNRhs[yyruleno];
158304  yyact = yy_find_reduce_action(yymsp[yysize].stateno,(YYCODETYPE)yygoto);
158305
158306  /* There are no SHIFTREDUCE actions on nonterminals because the table
158307  ** generator has simplified them to pure REDUCE actions. */
158308  assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) );
158309
158310  /* It is not possible for a REDUCE to be followed by an error */
158311  assert( yyact!=YY_ERROR_ACTION );
158312
158313  yymsp += yysize+1;
158314  yypParser->yytos = yymsp;
158315  yymsp->stateno = (YYACTIONTYPE)yyact;
158316  yymsp->major = (YYCODETYPE)yygoto;
158317  yyTraceShift(yypParser, yyact, "... then shift");
158318  return yyact;
158319}
158320
158321/*
158322** The following code executes when the parse fails
158323*/
158324#ifndef YYNOERRORRECOVERY
158325static void yy_parse_failed(
158326  yyParser *yypParser           /* The parser */
158327){
158328  sqlite3ParserARG_FETCH
158329  sqlite3ParserCTX_FETCH
158330#ifndef NDEBUG
158331  if( yyTraceFILE ){
158332    fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
158333  }
158334#endif
158335  while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
158336  /* Here code is inserted which will be executed whenever the
158337  ** parser fails */
158338/************ Begin %parse_failure code ***************************************/
158339/************ End %parse_failure code *****************************************/
158340  sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
158341  sqlite3ParserCTX_STORE
158342}
158343#endif /* YYNOERRORRECOVERY */
158344
158345/*
158346** The following code executes when a syntax error first occurs.
158347*/
158348static void yy_syntax_error(
158349  yyParser *yypParser,           /* The parser */
158350  int yymajor,                   /* The major type of the error token */
158351  sqlite3ParserTOKENTYPE yyminor         /* The minor type of the error token */
158352){
158353  sqlite3ParserARG_FETCH
158354  sqlite3ParserCTX_FETCH
158355#define TOKEN yyminor
158356/************ Begin %syntax_error code ****************************************/
158357
158358  UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
158359  if( TOKEN.z[0] ){
158360    sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
158361  }else{
158362    sqlite3ErrorMsg(pParse, "incomplete input");
158363  }
158364/************ End %syntax_error code ******************************************/
158365  sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
158366  sqlite3ParserCTX_STORE
158367}
158368
158369/*
158370** The following is executed when the parser accepts
158371*/
158372static void yy_accept(
158373  yyParser *yypParser           /* The parser */
158374){
158375  sqlite3ParserARG_FETCH
158376  sqlite3ParserCTX_FETCH
158377#ifndef NDEBUG
158378  if( yyTraceFILE ){
158379    fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
158380  }
158381#endif
158382#ifndef YYNOERRORRECOVERY
158383  yypParser->yyerrcnt = -1;
158384#endif
158385  assert( yypParser->yytos==yypParser->yystack );
158386  /* Here code is inserted which will be executed whenever the
158387  ** parser accepts */
158388/*********** Begin %parse_accept code *****************************************/
158389/*********** End %parse_accept code *******************************************/
158390  sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
158391  sqlite3ParserCTX_STORE
158392}
158393
158394/* The main parser program.
158395** The first argument is a pointer to a structure obtained from
158396** "sqlite3ParserAlloc" which describes the current state of the parser.
158397** The second argument is the major token number.  The third is
158398** the minor token.  The fourth optional argument is whatever the
158399** user wants (and specified in the grammar) and is available for
158400** use by the action routines.
158401**
158402** Inputs:
158403** <ul>
158404** <li> A pointer to the parser (an opaque structure.)
158405** <li> The major token number.
158406** <li> The minor token number.
158407** <li> An option argument of a grammar-specified type.
158408** </ul>
158409**
158410** Outputs:
158411** None.
158412*/
158413SQLITE_PRIVATE void sqlite3Parser(
158414  void *yyp,                   /* The parser */
158415  int yymajor,                 /* The major token code number */
158416  sqlite3ParserTOKENTYPE yyminor       /* The value for the token */
158417  sqlite3ParserARG_PDECL               /* Optional %extra_argument parameter */
158418){
158419  YYMINORTYPE yyminorunion;
158420  YYACTIONTYPE yyact;   /* The parser action. */
158421#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
158422  int yyendofinput;     /* True if we are at the end of input */
158423#endif
158424#ifdef YYERRORSYMBOL
158425  int yyerrorhit = 0;   /* True if yymajor has invoked an error */
158426#endif
158427  yyParser *yypParser = (yyParser*)yyp;  /* The parser */
158428  sqlite3ParserCTX_FETCH
158429  sqlite3ParserARG_STORE
158430
158431  assert( yypParser->yytos!=0 );
158432#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
158433  yyendofinput = (yymajor==0);
158434#endif
158435
158436  yyact = yypParser->yytos->stateno;
158437#ifndef NDEBUG
158438  if( yyTraceFILE ){
158439    if( yyact < YY_MIN_REDUCE ){
158440      fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
158441              yyTracePrompt,yyTokenName[yymajor],yyact);
158442    }else{
158443      fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
158444              yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
158445    }
158446  }
158447#endif
158448
158449  do{
158450    assert( yyact==yypParser->yytos->stateno );
158451    yyact = yy_find_shift_action((YYCODETYPE)yymajor,yyact);
158452    if( yyact >= YY_MIN_REDUCE ){
158453      yyact = yy_reduce(yypParser,yyact-YY_MIN_REDUCE,yymajor,
158454                        yyminor sqlite3ParserCTX_PARAM);
158455    }else if( yyact <= YY_MAX_SHIFTREDUCE ){
158456      yy_shift(yypParser,yyact,(YYCODETYPE)yymajor,yyminor);
158457#ifndef YYNOERRORRECOVERY
158458      yypParser->yyerrcnt--;
158459#endif
158460      break;
158461    }else if( yyact==YY_ACCEPT_ACTION ){
158462      yypParser->yytos--;
158463      yy_accept(yypParser);
158464      return;
158465    }else{
158466      assert( yyact == YY_ERROR_ACTION );
158467      yyminorunion.yy0 = yyminor;
158468#ifdef YYERRORSYMBOL
158469      int yymx;
158470#endif
158471#ifndef NDEBUG
158472      if( yyTraceFILE ){
158473        fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
158474      }
158475#endif
158476#ifdef YYERRORSYMBOL
158477      /* A syntax error has occurred.
158478      ** The response to an error depends upon whether or not the
158479      ** grammar defines an error token "ERROR".
158480      **
158481      ** This is what we do if the grammar does define ERROR:
158482      **
158483      **  * Call the %syntax_error function.
158484      **
158485      **  * Begin popping the stack until we enter a state where
158486      **    it is legal to shift the error symbol, then shift
158487      **    the error symbol.
158488      **
158489      **  * Set the error count to three.
158490      **
158491      **  * Begin accepting and shifting new tokens.  No new error
158492      **    processing will occur until three tokens have been
158493      **    shifted successfully.
158494      **
158495      */
158496      if( yypParser->yyerrcnt<0 ){
158497        yy_syntax_error(yypParser,yymajor,yyminor);
158498      }
158499      yymx = yypParser->yytos->major;
158500      if( yymx==YYERRORSYMBOL || yyerrorhit ){
158501#ifndef NDEBUG
158502        if( yyTraceFILE ){
158503          fprintf(yyTraceFILE,"%sDiscard input token %s\n",
158504             yyTracePrompt,yyTokenName[yymajor]);
158505        }
158506#endif
158507        yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion);
158508        yymajor = YYNOCODE;
158509      }else{
158510        while( yypParser->yytos >= yypParser->yystack
158511            && (yyact = yy_find_reduce_action(
158512                        yypParser->yytos->stateno,
158513                        YYERRORSYMBOL)) > YY_MAX_SHIFTREDUCE
158514        ){
158515          yy_pop_parser_stack(yypParser);
158516        }
158517        if( yypParser->yytos < yypParser->yystack || yymajor==0 ){
158518          yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
158519          yy_parse_failed(yypParser);
158520#ifndef YYNOERRORRECOVERY
158521          yypParser->yyerrcnt = -1;
158522#endif
158523          yymajor = YYNOCODE;
158524        }else if( yymx!=YYERRORSYMBOL ){
158525          yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
158526        }
158527      }
158528      yypParser->yyerrcnt = 3;
158529      yyerrorhit = 1;
158530      if( yymajor==YYNOCODE ) break;
158531      yyact = yypParser->yytos->stateno;
158532#elif defined(YYNOERRORRECOVERY)
158533      /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
158534      ** do any kind of error recovery.  Instead, simply invoke the syntax
158535      ** error routine and continue going as if nothing had happened.
158536      **
158537      ** Applications can set this macro (for example inside %include) if
158538      ** they intend to abandon the parse upon the first syntax error seen.
158539      */
158540      yy_syntax_error(yypParser,yymajor, yyminor);
158541      yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
158542      break;
158543#else  /* YYERRORSYMBOL is not defined */
158544      /* This is what we do if the grammar does not define ERROR:
158545      **
158546      **  * Report an error message, and throw away the input token.
158547      **
158548      **  * If the input token is $, then fail the parse.
158549      **
158550      ** As before, subsequent error messages are suppressed until
158551      ** three input tokens have been successfully shifted.
158552      */
158553      if( yypParser->yyerrcnt<=0 ){
158554        yy_syntax_error(yypParser,yymajor, yyminor);
158555      }
158556      yypParser->yyerrcnt = 3;
158557      yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
158558      if( yyendofinput ){
158559        yy_parse_failed(yypParser);
158560#ifndef YYNOERRORRECOVERY
158561        yypParser->yyerrcnt = -1;
158562#endif
158563      }
158564      break;
158565#endif
158566    }
158567  }while( yypParser->yytos>yypParser->yystack );
158568#ifndef NDEBUG
158569  if( yyTraceFILE ){
158570    yyStackEntry *i;
158571    char cDiv = '[';
158572    fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
158573    for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
158574      fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
158575      cDiv = ' ';
158576    }
158577    fprintf(yyTraceFILE,"]\n");
158578  }
158579#endif
158580  return;
158581}
158582
158583/*
158584** Return the fallback token corresponding to canonical token iToken, or
158585** 0 if iToken has no fallback.
158586*/
158587SQLITE_PRIVATE int sqlite3ParserFallback(int iToken){
158588#ifdef YYFALLBACK
158589  assert( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) );
158590  return yyFallback[iToken];
158591#else
158592  (void)iToken;
158593  return 0;
158594#endif
158595}
158596
158597/************** End of parse.c ***********************************************/
158598/************** Begin file tokenize.c ****************************************/
158599/*
158600** 2001 September 15
158601**
158602** The author disclaims copyright to this source code.  In place of
158603** a legal notice, here is a blessing:
158604**
158605**    May you do good and not evil.
158606**    May you find forgiveness for yourself and forgive others.
158607**    May you share freely, never taking more than you give.
158608**
158609*************************************************************************
158610** An tokenizer for SQL
158611**
158612** This file contains C code that splits an SQL input string up into
158613** individual tokens and sends those tokens one-by-one over to the
158614** parser for analysis.
158615*/
158616/* #include "sqliteInt.h" */
158617/* #include <stdlib.h> */
158618
158619/* Character classes for tokenizing
158620**
158621** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
158622** using a lookup table, whereas a switch() directly on c uses a binary search.
158623** The lookup table is much faster.  To maximize speed, and to ensure that
158624** a lookup table is used, all of the classes need to be small integers and
158625** all of them need to be used within the switch.
158626*/
158627#define CC_X          0    /* The letter 'x', or start of BLOB literal */
158628#define CC_KYWD       1    /* Alphabetics or '_'.  Usable in a keyword */
158629#define CC_ID         2    /* unicode characters usable in IDs */
158630#define CC_DIGIT      3    /* Digits */
158631#define CC_DOLLAR     4    /* '$' */
158632#define CC_VARALPHA   5    /* '@', '#', ':'.  Alphabetic SQL variables */
158633#define CC_VARNUM     6    /* '?'.  Numeric SQL variables */
158634#define CC_SPACE      7    /* Space characters */
158635#define CC_QUOTE      8    /* '"', '\'', or '`'.  String literals, quoted ids */
158636#define CC_QUOTE2     9    /* '['.   [...] style quoted ids */
158637#define CC_PIPE      10    /* '|'.   Bitwise OR or concatenate */
158638#define CC_MINUS     11    /* '-'.  Minus or SQL-style comment */
158639#define CC_LT        12    /* '<'.  Part of < or <= or <> */
158640#define CC_GT        13    /* '>'.  Part of > or >= */
158641#define CC_EQ        14    /* '='.  Part of = or == */
158642#define CC_BANG      15    /* '!'.  Part of != */
158643#define CC_SLASH     16    /* '/'.  / or c-style comment */
158644#define CC_LP        17    /* '(' */
158645#define CC_RP        18    /* ')' */
158646#define CC_SEMI      19    /* ';' */
158647#define CC_PLUS      20    /* '+' */
158648#define CC_STAR      21    /* '*' */
158649#define CC_PERCENT   22    /* '%' */
158650#define CC_COMMA     23    /* ',' */
158651#define CC_AND       24    /* '&' */
158652#define CC_TILDA     25    /* '~' */
158653#define CC_DOT       26    /* '.' */
158654#define CC_ILLEGAL   27    /* Illegal character */
158655#define CC_NUL       28    /* 0x00 */
158656
158657static const unsigned char aiClass[] = {
158658#ifdef SQLITE_ASCII
158659/*         x0  x1  x2  x3  x4  x5  x6  x7  x8  x9  xa  xb  xc  xd  xe  xf */
158660/* 0x */   28, 27, 27, 27, 27, 27, 27, 27, 27,  7,  7, 27,  7,  7, 27, 27,
158661/* 1x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
158662/* 2x */    7, 15,  8,  5,  4, 22, 24,  8, 17, 18, 21, 20, 23, 11, 26, 16,
158663/* 3x */    3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  5, 19, 12, 14, 13,  6,
158664/* 4x */    5,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
158665/* 5x */    1,  1,  1,  1,  1,  1,  1,  1,  0,  1,  1,  9, 27, 27, 27,  1,
158666/* 6x */    8,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
158667/* 7x */    1,  1,  1,  1,  1,  1,  1,  1,  0,  1,  1, 27, 10, 27, 25, 27,
158668/* 8x */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
158669/* 9x */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
158670/* Ax */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
158671/* Bx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
158672/* Cx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
158673/* Dx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
158674/* Ex */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
158675/* Fx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2
158676#endif
158677#ifdef SQLITE_EBCDIC
158678/*         x0  x1  x2  x3  x4  x5  x6  x7  x8  x9  xa  xb  xc  xd  xe  xf */
158679/* 0x */   27, 27, 27, 27, 27,  7, 27, 27, 27, 27, 27, 27,  7,  7, 27, 27,
158680/* 1x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
158681/* 2x */   27, 27, 27, 27, 27,  7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
158682/* 3x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
158683/* 4x */    7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 26, 12, 17, 20, 10,
158684/* 5x */   24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15,  4, 21, 18, 19, 27,
158685/* 6x */   11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22,  1, 13,  6,
158686/* 7x */   27, 27, 27, 27, 27, 27, 27, 27, 27,  8,  5,  5,  5,  8, 14,  8,
158687/* 8x */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
158688/* 9x */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
158689/* Ax */   27, 25,  1,  1,  1,  1,  1,  0,  1,  1, 27, 27, 27, 27, 27, 27,
158690/* Bx */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27,  9, 27, 27, 27, 27, 27,
158691/* Cx */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
158692/* Dx */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
158693/* Ex */   27, 27,  1,  1,  1,  1,  1,  0,  1,  1, 27, 27, 27, 27, 27, 27,
158694/* Fx */    3,  3,  3,  3,  3,  3,  3,  3,  3,  3, 27, 27, 27, 27, 27, 27,
158695#endif
158696};
158697
158698/*
158699** The charMap() macro maps alphabetic characters (only) into their
158700** lower-case ASCII equivalent.  On ASCII machines, this is just
158701** an upper-to-lower case map.  On EBCDIC machines we also need
158702** to adjust the encoding.  The mapping is only valid for alphabetics
158703** which are the only characters for which this feature is used.
158704**
158705** Used by keywordhash.h
158706*/
158707#ifdef SQLITE_ASCII
158708# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
158709#endif
158710#ifdef SQLITE_EBCDIC
158711# define charMap(X) ebcdicToAscii[(unsigned char)X]
158712const unsigned char ebcdicToAscii[] = {
158713/* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
158714   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
158715   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
158716   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
158717   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
158718   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
158719   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
158720   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
158721   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
158722   0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
158723   0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
158724   0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
158725   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
158726   0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
158727   0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
158728   0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
158729   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
158730};
158731#endif
158732
158733/*
158734** The sqlite3KeywordCode function looks up an identifier to determine if
158735** it is a keyword.  If it is a keyword, the token code of that keyword is
158736** returned.  If the input is not a keyword, TK_ID is returned.
158737**
158738** The implementation of this routine was generated by a program,
158739** mkkeywordhash.c, located in the tool subdirectory of the distribution.
158740** The output of the mkkeywordhash.c program is written into a file
158741** named keywordhash.h and then included into this source file by
158742** the #include below.
158743*/
158744/************** Include keywordhash.h in the middle of tokenize.c ************/
158745/************** Begin file keywordhash.h *************************************/
158746/***** This file contains automatically generated code ******
158747**
158748** The code in this file has been automatically generated by
158749**
158750**   sqlite/tool/mkkeywordhash.c
158751**
158752** The code in this file implements a function that determines whether
158753** or not a given identifier is really an SQL keyword.  The same thing
158754** might be implemented more directly using a hand-written hash table.
158755** But by using this automatically generated code, the size of the code
158756** is substantially reduced.  This is important for embedded applications
158757** on platforms with limited memory.
158758*/
158759/* Hash score: 227 */
158760/* zKWText[] encodes 984 bytes of keyword text in 648 bytes */
158761/*   REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT       */
158762/*   ABLEFTHENDEFERRABLELSEXCLUDELETEMPORARYISNULLSAVEPOINTERSECT       */
158763/*   IESNOTNULLIKEXCEPTRANSACTIONATURALTERAISEXCLUSIVEXISTS             */
158764/*   CONSTRAINTOFFSETRIGGERANGENERATEDETACHAVINGLOBEGINNEREFERENCES     */
158765/*   UNIQUERYWITHOUTERELEASEATTACHBETWEENOTHINGROUPSCASCADEFAULT        */
158766/*   CASECOLLATECREATECURRENT_DATEIMMEDIATEJOINSERTMATCHPLANALYZE       */
158767/*   PRAGMABORTUPDATEVALUESVIRTUALWAYSWHENWHERECURSIVEAFTERENAMEAND     */
158768/*   EFERREDISTINCTAUTOINCREMENTCASTCOLUMNCOMMITCONFLICTCROSS           */
158769/*   CURRENT_TIMESTAMPARTITIONDROPRECEDINGFAILASTFILTEREPLACEFIRST      */
158770/*   FOLLOWINGFROMFULLIMITIFORDERESTRICTOTHERSOVERIGHTROLLBACKROWS      */
158771/*   UNBOUNDEDUNIONUSINGVACUUMVIEWINDOWBYINITIALLYPRIMARY               */
158772static const char zKWText[647] = {
158773  'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
158774  'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
158775  'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
158776  'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
158777  'E','R','R','A','B','L','E','L','S','E','X','C','L','U','D','E','L','E',
158778  'T','E','M','P','O','R','A','R','Y','I','S','N','U','L','L','S','A','V',
158779  'E','P','O','I','N','T','E','R','S','E','C','T','I','E','S','N','O','T',
158780  'N','U','L','L','I','K','E','X','C','E','P','T','R','A','N','S','A','C',
158781  'T','I','O','N','A','T','U','R','A','L','T','E','R','A','I','S','E','X',
158782  'C','L','U','S','I','V','E','X','I','S','T','S','C','O','N','S','T','R',
158783  'A','I','N','T','O','F','F','S','E','T','R','I','G','G','E','R','A','N',
158784  'G','E','N','E','R','A','T','E','D','E','T','A','C','H','A','V','I','N',
158785  'G','L','O','B','E','G','I','N','N','E','R','E','F','E','R','E','N','C',
158786  'E','S','U','N','I','Q','U','E','R','Y','W','I','T','H','O','U','T','E',
158787  'R','E','L','E','A','S','E','A','T','T','A','C','H','B','E','T','W','E',
158788  'E','N','O','T','H','I','N','G','R','O','U','P','S','C','A','S','C','A',
158789  'D','E','F','A','U','L','T','C','A','S','E','C','O','L','L','A','T','E',
158790  'C','R','E','A','T','E','C','U','R','R','E','N','T','_','D','A','T','E',
158791  'I','M','M','E','D','I','A','T','E','J','O','I','N','S','E','R','T','M',
158792  'A','T','C','H','P','L','A','N','A','L','Y','Z','E','P','R','A','G','M',
158793  'A','B','O','R','T','U','P','D','A','T','E','V','A','L','U','E','S','V',
158794  'I','R','T','U','A','L','W','A','Y','S','W','H','E','N','W','H','E','R',
158795  'E','C','U','R','S','I','V','E','A','F','T','E','R','E','N','A','M','E',
158796  'A','N','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T','A',
158797  'U','T','O','I','N','C','R','E','M','E','N','T','C','A','S','T','C','O',
158798  'L','U','M','N','C','O','M','M','I','T','C','O','N','F','L','I','C','T',
158799  'C','R','O','S','S','C','U','R','R','E','N','T','_','T','I','M','E','S',
158800  'T','A','M','P','A','R','T','I','T','I','O','N','D','R','O','P','R','E',
158801  'C','E','D','I','N','G','F','A','I','L','A','S','T','F','I','L','T','E',
158802  'R','E','P','L','A','C','E','F','I','R','S','T','F','O','L','L','O','W',
158803  'I','N','G','F','R','O','M','F','U','L','L','I','M','I','T','I','F','O',
158804  'R','D','E','R','E','S','T','R','I','C','T','O','T','H','E','R','S','O',
158805  'V','E','R','I','G','H','T','R','O','L','L','B','A','C','K','R','O','W',
158806  'S','U','N','B','O','U','N','D','E','D','U','N','I','O','N','U','S','I',
158807  'N','G','V','A','C','U','U','M','V','I','E','W','I','N','D','O','W','B',
158808  'Y','I','N','I','T','I','A','L','L','Y','P','R','I','M','A','R','Y',
158809};
158810/* aKWHash[i] is the hash value for the i-th keyword */
158811static const unsigned char aKWHash[127] = {
158812    84, 102, 132,  82, 114,  29,   0,   0,  91,   0,  85,  72,   0,
158813    53,  35,  86,  15,   0,  42,  94,  54, 126, 133,  19,   0,   0,
158814   138,   0,  40, 128,   0,  22, 104,   0,   9,   0,   0, 122,  80,
158815     0,  78,   6,   0,  65,  99, 145,   0, 134, 112,   0,   0,  48,
158816     0, 100,  24,   0,  17,   0,  27,  70,  23,  26,   5,  60, 140,
158817   107, 121,   0,  73, 101,  71, 143,  61, 119,  74,   0,  49,   0,
158818    11,  41,   0, 110,   0,   0,   0, 106,  10, 108, 113, 124,  14,
158819    50, 123,   0,  89,   0,  18, 120, 142,  56, 129, 137,  88,  83,
158820    37,  30, 125,   0,   0, 105,  51, 130, 127,   0,  34,   0,   0,
158821    44,   0,  95,  38,  39,   0,  20,  45, 116,  90,
158822};
158823/* aKWNext[] forms the hash collision chain.  If aKWHash[i]==0
158824** then the i-th keyword has no more hash collisions.  Otherwise,
158825** the next keyword with the same hash is aKWHash[i]-1. */
158826static const unsigned char aKWNext[145] = {
158827     0,   0,   0,   0,   4,   0,  43,   0,   0, 103, 111,   0,   0,
158828     0,   2,   0,   0, 141,   0,   0,   0,  13,   0,   0,   0,   0,
158829   139,   0,   0, 118,  52,   0,   0, 135,  12,   0,   0,  62,   0,
158830   136,   0, 131,   0,   0,  36,   0,   0,  28,  77,   0,   0,   0,
158831     0,  59,   0,  47,   0,   0,   0,   0,   0,   0,   0,   0,   0,
158832     0,  69,   0,   0,   0,   0,   0, 144,   3,   0,  58,   0,   1,
158833    75,   0,   0,   0,  31,   0,   0,   0,   0,   0,   0,  64,  66,
158834    63,   0,   0,   0,   0,  46,   0,  16,   0, 115,   0,   0,   0,
158835     0,   0,   0,   0,   0,   0,   0,  81,  97,   0,   8,   0, 109,
158836    21,   7,  67,   0,  79,  93, 117,   0,   0,  68,   0,   0,  96,
158837     0,  55,   0,  76,   0,  92,  32,  33,  57,  25,   0,  98,   0,
158838     0,  87,
158839};
158840/* aKWLen[i] is the length (in bytes) of the i-th keyword */
158841static const unsigned char aKWLen[145] = {
158842     7,   7,   5,   4,   6,   4,   5,   3,   6,   7,   3,   6,   6,
158843     7,   7,   3,   8,   2,   6,   5,   4,   4,   3,  10,   4,   7,
158844     6,   9,   4,   2,   6,   5,   9,   9,   4,   7,   3,   2,   4,
158845     4,   6,  11,   6,   2,   7,   5,   5,   9,   6,  10,   4,   6,
158846     2,   3,   7,   5,   9,   6,   6,   4,   5,   5,  10,   6,   5,
158847     7,   4,   5,   7,   6,   7,   7,   6,   5,   7,   3,   7,   4,
158848     7,   6,  12,   9,   4,   6,   5,   4,   7,   6,   5,   6,   6,
158849     7,   6,   4,   5,   9,   5,   6,   3,   8,   8,   2,  13,   2,
158850     2,   4,   6,   6,   8,   5,  17,  12,   7,   9,   4,   9,   4,
158851     4,   6,   7,   5,   9,   4,   4,   5,   2,   5,   8,   6,   4,
158852     5,   8,   4,   3,   9,   5,   5,   6,   4,   6,   2,   2,   9,
158853     3,   7,
158854};
158855/* aKWOffset[i] is the index into zKWText[] of the start of
158856** the text for the i-th keyword. */
158857static const unsigned short int aKWOffset[145] = {
158858     0,   2,   2,   8,   9,  14,  16,  20,  23,  25,  25,  29,  33,
158859    36,  41,  46,  48,  53,  54,  59,  62,  65,  67,  69,  78,  81,
158860    86,  90,  90,  94,  99, 101, 105, 111, 119, 123, 123, 123, 126,
158861   129, 132, 137, 142, 146, 147, 152, 156, 160, 168, 174, 181, 184,
158862   184, 187, 189, 195, 198, 206, 211, 216, 219, 222, 226, 236, 239,
158863   244, 244, 248, 252, 259, 265, 271, 277, 277, 283, 284, 288, 295,
158864   299, 306, 312, 324, 333, 335, 341, 346, 348, 355, 360, 365, 371,
158865   377, 382, 388, 392, 395, 404, 408, 414, 416, 423, 424, 431, 433,
158866   435, 444, 448, 454, 460, 468, 473, 473, 473, 489, 498, 501, 510,
158867   513, 517, 522, 529, 534, 543, 547, 550, 555, 557, 561, 569, 575,
158868   578, 583, 591, 591, 595, 604, 609, 614, 620, 623, 626, 629, 631,
158869   636, 640,
158870};
158871/* aKWCode[i] is the parser symbol code for the i-th keyword */
158872static const unsigned char aKWCode[145] = {
158873  TK_REINDEX,    TK_INDEXED,    TK_INDEX,      TK_DESC,       TK_ESCAPE,
158874  TK_EACH,       TK_CHECK,      TK_KEY,        TK_BEFORE,     TK_FOREIGN,
158875  TK_FOR,        TK_IGNORE,     TK_LIKE_KW,    TK_EXPLAIN,    TK_INSTEAD,
158876  TK_ADD,        TK_DATABASE,   TK_AS,         TK_SELECT,     TK_TABLE,
158877  TK_JOIN_KW,    TK_THEN,       TK_END,        TK_DEFERRABLE, TK_ELSE,
158878  TK_EXCLUDE,    TK_DELETE,     TK_TEMP,       TK_TEMP,       TK_OR,
158879  TK_ISNULL,     TK_NULLS,      TK_SAVEPOINT,  TK_INTERSECT,  TK_TIES,
158880  TK_NOTNULL,    TK_NOT,        TK_NO,         TK_NULL,       TK_LIKE_KW,
158881  TK_EXCEPT,     TK_TRANSACTION,TK_ACTION,     TK_ON,         TK_JOIN_KW,
158882  TK_ALTER,      TK_RAISE,      TK_EXCLUSIVE,  TK_EXISTS,     TK_CONSTRAINT,
158883  TK_INTO,       TK_OFFSET,     TK_OF,         TK_SET,        TK_TRIGGER,
158884  TK_RANGE,      TK_GENERATED,  TK_DETACH,     TK_HAVING,     TK_LIKE_KW,
158885  TK_BEGIN,      TK_JOIN_KW,    TK_REFERENCES, TK_UNIQUE,     TK_QUERY,
158886  TK_WITHOUT,    TK_WITH,       TK_JOIN_KW,    TK_RELEASE,    TK_ATTACH,
158887  TK_BETWEEN,    TK_NOTHING,    TK_GROUPS,     TK_GROUP,      TK_CASCADE,
158888  TK_ASC,        TK_DEFAULT,    TK_CASE,       TK_COLLATE,    TK_CREATE,
158889  TK_CTIME_KW,   TK_IMMEDIATE,  TK_JOIN,       TK_INSERT,     TK_MATCH,
158890  TK_PLAN,       TK_ANALYZE,    TK_PRAGMA,     TK_ABORT,      TK_UPDATE,
158891  TK_VALUES,     TK_VIRTUAL,    TK_ALWAYS,     TK_WHEN,       TK_WHERE,
158892  TK_RECURSIVE,  TK_AFTER,      TK_RENAME,     TK_AND,        TK_DEFERRED,
158893  TK_DISTINCT,   TK_IS,         TK_AUTOINCR,   TK_TO,         TK_IN,
158894  TK_CAST,       TK_COLUMNKW,   TK_COMMIT,     TK_CONFLICT,   TK_JOIN_KW,
158895  TK_CTIME_KW,   TK_CTIME_KW,   TK_CURRENT,    TK_PARTITION,  TK_DROP,
158896  TK_PRECEDING,  TK_FAIL,       TK_LAST,       TK_FILTER,     TK_REPLACE,
158897  TK_FIRST,      TK_FOLLOWING,  TK_FROM,       TK_JOIN_KW,    TK_LIMIT,
158898  TK_IF,         TK_ORDER,      TK_RESTRICT,   TK_OTHERS,     TK_OVER,
158899  TK_JOIN_KW,    TK_ROLLBACK,   TK_ROWS,       TK_ROW,        TK_UNBOUNDED,
158900  TK_UNION,      TK_USING,      TK_VACUUM,     TK_VIEW,       TK_WINDOW,
158901  TK_DO,         TK_BY,         TK_INITIALLY,  TK_ALL,        TK_PRIMARY,
158902};
158903/* Hash table decoded:
158904**   0: INSERT
158905**   1: IS
158906**   2: ROLLBACK TRIGGER
158907**   3: IMMEDIATE
158908**   4: PARTITION
158909**   5: TEMP
158910**   6:
158911**   7:
158912**   8: VALUES WITHOUT
158913**   9:
158914**  10: MATCH
158915**  11: NOTHING
158916**  12:
158917**  13: OF
158918**  14: TIES IGNORE
158919**  15: PLAN
158920**  16: INSTEAD INDEXED
158921**  17:
158922**  18: TRANSACTION RIGHT
158923**  19: WHEN
158924**  20: SET HAVING
158925**  21: IF
158926**  22: ROWS
158927**  23: SELECT
158928**  24:
158929**  25:
158930**  26: VACUUM SAVEPOINT
158931**  27:
158932**  28: LIKE UNION VIRTUAL REFERENCES
158933**  29: RESTRICT
158934**  30:
158935**  31: THEN REGEXP
158936**  32: TO
158937**  33:
158938**  34: BEFORE
158939**  35:
158940**  36:
158941**  37: FOLLOWING COLLATE CASCADE
158942**  38: CREATE
158943**  39:
158944**  40: CASE REINDEX
158945**  41: EACH
158946**  42:
158947**  43: QUERY
158948**  44: AND ADD
158949**  45: PRIMARY ANALYZE
158950**  46:
158951**  47: ROW ASC DETACH
158952**  48: CURRENT_TIME CURRENT_DATE
158953**  49:
158954**  50:
158955**  51: EXCLUSIVE TEMPORARY
158956**  52:
158957**  53: DEFERRED
158958**  54: DEFERRABLE
158959**  55:
158960**  56: DATABASE
158961**  57:
158962**  58: DELETE VIEW GENERATED
158963**  59: ATTACH
158964**  60: END
158965**  61: EXCLUDE
158966**  62: ESCAPE DESC
158967**  63: GLOB
158968**  64: WINDOW ELSE
158969**  65: COLUMN
158970**  66: FIRST
158971**  67:
158972**  68: GROUPS ALL
158973**  69: DISTINCT DROP KEY
158974**  70: BETWEEN
158975**  71: INITIALLY
158976**  72: BEGIN
158977**  73: FILTER CHECK ACTION
158978**  74: GROUP INDEX
158979**  75:
158980**  76: EXISTS DEFAULT
158981**  77:
158982**  78: FOR CURRENT_TIMESTAMP
158983**  79: EXCEPT
158984**  80:
158985**  81: CROSS
158986**  82:
158987**  83:
158988**  84:
158989**  85: CAST
158990**  86: FOREIGN AUTOINCREMENT
158991**  87: COMMIT
158992**  88: CURRENT AFTER ALTER
158993**  89: FULL FAIL CONFLICT
158994**  90: EXPLAIN
158995**  91: CONSTRAINT
158996**  92: FROM ALWAYS
158997**  93:
158998**  94: ABORT
158999**  95:
159000**  96: AS DO
159001**  97: REPLACE WITH RELEASE
159002**  98: BY RENAME
159003**  99: RANGE RAISE
159004** 100: OTHERS
159005** 101: USING NULLS
159006** 102: PRAGMA
159007** 103: JOIN ISNULL OFFSET
159008** 104: NOT
159009** 105: OR LAST LEFT
159010** 106: LIMIT
159011** 107:
159012** 108:
159013** 109: IN
159014** 110: INTO
159015** 111: OVER RECURSIVE
159016** 112: ORDER OUTER
159017** 113:
159018** 114: INTERSECT UNBOUNDED
159019** 115:
159020** 116:
159021** 117: ON
159022** 118:
159023** 119: WHERE
159024** 120: NO INNER
159025** 121: NULL
159026** 122:
159027** 123: TABLE
159028** 124: NATURAL NOTNULL
159029** 125: PRECEDING
159030** 126: UPDATE UNIQUE
159031*/
159032/* Check to see if z[0..n-1] is a keyword. If it is, write the
159033** parser symbol code for that keyword into *pType.  Always
159034** return the integer n (the length of the token). */
159035static int keywordCode(const char *z, int n, int *pType){
159036  int i, j;
159037  const char *zKW;
159038  if( n>=2 ){
159039    i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) % 127;
159040    for(i=((int)aKWHash[i])-1; i>=0; i=((int)aKWNext[i])-1){
159041      if( aKWLen[i]!=n ) continue;
159042      zKW = &zKWText[aKWOffset[i]];
159043#ifdef SQLITE_ASCII
159044      if( (z[0]&~0x20)!=zKW[0] ) continue;
159045      if( (z[1]&~0x20)!=zKW[1] ) continue;
159046      j = 2;
159047      while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; }
159048#endif
159049#ifdef SQLITE_EBCDIC
159050      if( toupper(z[0])!=zKW[0] ) continue;
159051      if( toupper(z[1])!=zKW[1] ) continue;
159052      j = 2;
159053      while( j<n && toupper(z[j])==zKW[j] ){ j++; }
159054#endif
159055      if( j<n ) continue;
159056      testcase( i==0 ); /* REINDEX */
159057      testcase( i==1 ); /* INDEXED */
159058      testcase( i==2 ); /* INDEX */
159059      testcase( i==3 ); /* DESC */
159060      testcase( i==4 ); /* ESCAPE */
159061      testcase( i==5 ); /* EACH */
159062      testcase( i==6 ); /* CHECK */
159063      testcase( i==7 ); /* KEY */
159064      testcase( i==8 ); /* BEFORE */
159065      testcase( i==9 ); /* FOREIGN */
159066      testcase( i==10 ); /* FOR */
159067      testcase( i==11 ); /* IGNORE */
159068      testcase( i==12 ); /* REGEXP */
159069      testcase( i==13 ); /* EXPLAIN */
159070      testcase( i==14 ); /* INSTEAD */
159071      testcase( i==15 ); /* ADD */
159072      testcase( i==16 ); /* DATABASE */
159073      testcase( i==17 ); /* AS */
159074      testcase( i==18 ); /* SELECT */
159075      testcase( i==19 ); /* TABLE */
159076      testcase( i==20 ); /* LEFT */
159077      testcase( i==21 ); /* THEN */
159078      testcase( i==22 ); /* END */
159079      testcase( i==23 ); /* DEFERRABLE */
159080      testcase( i==24 ); /* ELSE */
159081      testcase( i==25 ); /* EXCLUDE */
159082      testcase( i==26 ); /* DELETE */
159083      testcase( i==27 ); /* TEMPORARY */
159084      testcase( i==28 ); /* TEMP */
159085      testcase( i==29 ); /* OR */
159086      testcase( i==30 ); /* ISNULL */
159087      testcase( i==31 ); /* NULLS */
159088      testcase( i==32 ); /* SAVEPOINT */
159089      testcase( i==33 ); /* INTERSECT */
159090      testcase( i==34 ); /* TIES */
159091      testcase( i==35 ); /* NOTNULL */
159092      testcase( i==36 ); /* NOT */
159093      testcase( i==37 ); /* NO */
159094      testcase( i==38 ); /* NULL */
159095      testcase( i==39 ); /* LIKE */
159096      testcase( i==40 ); /* EXCEPT */
159097      testcase( i==41 ); /* TRANSACTION */
159098      testcase( i==42 ); /* ACTION */
159099      testcase( i==43 ); /* ON */
159100      testcase( i==44 ); /* NATURAL */
159101      testcase( i==45 ); /* ALTER */
159102      testcase( i==46 ); /* RAISE */
159103      testcase( i==47 ); /* EXCLUSIVE */
159104      testcase( i==48 ); /* EXISTS */
159105      testcase( i==49 ); /* CONSTRAINT */
159106      testcase( i==50 ); /* INTO */
159107      testcase( i==51 ); /* OFFSET */
159108      testcase( i==52 ); /* OF */
159109      testcase( i==53 ); /* SET */
159110      testcase( i==54 ); /* TRIGGER */
159111      testcase( i==55 ); /* RANGE */
159112      testcase( i==56 ); /* GENERATED */
159113      testcase( i==57 ); /* DETACH */
159114      testcase( i==58 ); /* HAVING */
159115      testcase( i==59 ); /* GLOB */
159116      testcase( i==60 ); /* BEGIN */
159117      testcase( i==61 ); /* INNER */
159118      testcase( i==62 ); /* REFERENCES */
159119      testcase( i==63 ); /* UNIQUE */
159120      testcase( i==64 ); /* QUERY */
159121      testcase( i==65 ); /* WITHOUT */
159122      testcase( i==66 ); /* WITH */
159123      testcase( i==67 ); /* OUTER */
159124      testcase( i==68 ); /* RELEASE */
159125      testcase( i==69 ); /* ATTACH */
159126      testcase( i==70 ); /* BETWEEN */
159127      testcase( i==71 ); /* NOTHING */
159128      testcase( i==72 ); /* GROUPS */
159129      testcase( i==73 ); /* GROUP */
159130      testcase( i==74 ); /* CASCADE */
159131      testcase( i==75 ); /* ASC */
159132      testcase( i==76 ); /* DEFAULT */
159133      testcase( i==77 ); /* CASE */
159134      testcase( i==78 ); /* COLLATE */
159135      testcase( i==79 ); /* CREATE */
159136      testcase( i==80 ); /* CURRENT_DATE */
159137      testcase( i==81 ); /* IMMEDIATE */
159138      testcase( i==82 ); /* JOIN */
159139      testcase( i==83 ); /* INSERT */
159140      testcase( i==84 ); /* MATCH */
159141      testcase( i==85 ); /* PLAN */
159142      testcase( i==86 ); /* ANALYZE */
159143      testcase( i==87 ); /* PRAGMA */
159144      testcase( i==88 ); /* ABORT */
159145      testcase( i==89 ); /* UPDATE */
159146      testcase( i==90 ); /* VALUES */
159147      testcase( i==91 ); /* VIRTUAL */
159148      testcase( i==92 ); /* ALWAYS */
159149      testcase( i==93 ); /* WHEN */
159150      testcase( i==94 ); /* WHERE */
159151      testcase( i==95 ); /* RECURSIVE */
159152      testcase( i==96 ); /* AFTER */
159153      testcase( i==97 ); /* RENAME */
159154      testcase( i==98 ); /* AND */
159155      testcase( i==99 ); /* DEFERRED */
159156      testcase( i==100 ); /* DISTINCT */
159157      testcase( i==101 ); /* IS */
159158      testcase( i==102 ); /* AUTOINCREMENT */
159159      testcase( i==103 ); /* TO */
159160      testcase( i==104 ); /* IN */
159161      testcase( i==105 ); /* CAST */
159162      testcase( i==106 ); /* COLUMN */
159163      testcase( i==107 ); /* COMMIT */
159164      testcase( i==108 ); /* CONFLICT */
159165      testcase( i==109 ); /* CROSS */
159166      testcase( i==110 ); /* CURRENT_TIMESTAMP */
159167      testcase( i==111 ); /* CURRENT_TIME */
159168      testcase( i==112 ); /* CURRENT */
159169      testcase( i==113 ); /* PARTITION */
159170      testcase( i==114 ); /* DROP */
159171      testcase( i==115 ); /* PRECEDING */
159172      testcase( i==116 ); /* FAIL */
159173      testcase( i==117 ); /* LAST */
159174      testcase( i==118 ); /* FILTER */
159175      testcase( i==119 ); /* REPLACE */
159176      testcase( i==120 ); /* FIRST */
159177      testcase( i==121 ); /* FOLLOWING */
159178      testcase( i==122 ); /* FROM */
159179      testcase( i==123 ); /* FULL */
159180      testcase( i==124 ); /* LIMIT */
159181      testcase( i==125 ); /* IF */
159182      testcase( i==126 ); /* ORDER */
159183      testcase( i==127 ); /* RESTRICT */
159184      testcase( i==128 ); /* OTHERS */
159185      testcase( i==129 ); /* OVER */
159186      testcase( i==130 ); /* RIGHT */
159187      testcase( i==131 ); /* ROLLBACK */
159188      testcase( i==132 ); /* ROWS */
159189      testcase( i==133 ); /* ROW */
159190      testcase( i==134 ); /* UNBOUNDED */
159191      testcase( i==135 ); /* UNION */
159192      testcase( i==136 ); /* USING */
159193      testcase( i==137 ); /* VACUUM */
159194      testcase( i==138 ); /* VIEW */
159195      testcase( i==139 ); /* WINDOW */
159196      testcase( i==140 ); /* DO */
159197      testcase( i==141 ); /* BY */
159198      testcase( i==142 ); /* INITIALLY */
159199      testcase( i==143 ); /* ALL */
159200      testcase( i==144 ); /* PRIMARY */
159201      *pType = aKWCode[i];
159202      break;
159203    }
159204  }
159205  return n;
159206}
159207SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
159208  int id = TK_ID;
159209  keywordCode((char*)z, n, &id);
159210  return id;
159211}
159212#define SQLITE_N_KEYWORD 145
159213SQLITE_API int sqlite3_keyword_name(int i,const char **pzName,int *pnName){
159214  if( i<0 || i>=SQLITE_N_KEYWORD ) return SQLITE_ERROR;
159215  *pzName = zKWText + aKWOffset[i];
159216  *pnName = aKWLen[i];
159217  return SQLITE_OK;
159218}
159219SQLITE_API int sqlite3_keyword_count(void){ return SQLITE_N_KEYWORD; }
159220SQLITE_API int sqlite3_keyword_check(const char *zName, int nName){
159221  return TK_ID!=sqlite3KeywordCode((const u8*)zName, nName);
159222}
159223
159224/************** End of keywordhash.h *****************************************/
159225/************** Continuing where we left off in tokenize.c *******************/
159226
159227
159228/*
159229** If X is a character that can be used in an identifier then
159230** IdChar(X) will be true.  Otherwise it is false.
159231**
159232** For ASCII, any character with the high-order bit set is
159233** allowed in an identifier.  For 7-bit characters,
159234** sqlite3IsIdChar[X] must be 1.
159235**
159236** For EBCDIC, the rules are more complex but have the same
159237** end result.
159238**
159239** Ticket #1066.  the SQL standard does not allow '$' in the
159240** middle of identifiers.  But many SQL implementations do.
159241** SQLite will allow '$' in identifiers for compatibility.
159242** But the feature is undocumented.
159243*/
159244#ifdef SQLITE_ASCII
159245#define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
159246#endif
159247#ifdef SQLITE_EBCDIC
159248SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
159249/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
159250    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
159251    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
159252    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
159253    0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
159254    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
159255    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
159256    1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
159257    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
159258    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
159259    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
159260    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
159261    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
159262};
159263#define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
159264#endif
159265
159266/* Make the IdChar function accessible from ctime.c and alter.c */
159267SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
159268
159269#ifndef SQLITE_OMIT_WINDOWFUNC
159270/*
159271** Return the id of the next token in string (*pz). Before returning, set
159272** (*pz) to point to the byte following the parsed token.
159273*/
159274static int getToken(const unsigned char **pz){
159275  const unsigned char *z = *pz;
159276  int t;                          /* Token type to return */
159277  do {
159278    z += sqlite3GetToken(z, &t);
159279  }while( t==TK_SPACE );
159280  if( t==TK_ID
159281   || t==TK_STRING
159282   || t==TK_JOIN_KW
159283   || t==TK_WINDOW
159284   || t==TK_OVER
159285   || sqlite3ParserFallback(t)==TK_ID
159286  ){
159287    t = TK_ID;
159288  }
159289  *pz = z;
159290  return t;
159291}
159292
159293/*
159294** The following three functions are called immediately after the tokenizer
159295** reads the keywords WINDOW, OVER and FILTER, respectively, to determine
159296** whether the token should be treated as a keyword or an SQL identifier.
159297** This cannot be handled by the usual lemon %fallback method, due to
159298** the ambiguity in some constructions. e.g.
159299**
159300**   SELECT sum(x) OVER ...
159301**
159302** In the above, "OVER" might be a keyword, or it might be an alias for the
159303** sum(x) expression. If a "%fallback ID OVER" directive were added to
159304** grammar, then SQLite would always treat "OVER" as an alias, making it
159305** impossible to call a window-function without a FILTER clause.
159306**
159307** WINDOW is treated as a keyword if:
159308**
159309**   * the following token is an identifier, or a keyword that can fallback
159310**     to being an identifier, and
159311**   * the token after than one is TK_AS.
159312**
159313** OVER is a keyword if:
159314**
159315**   * the previous token was TK_RP, and
159316**   * the next token is either TK_LP or an identifier.
159317**
159318** FILTER is a keyword if:
159319**
159320**   * the previous token was TK_RP, and
159321**   * the next token is TK_LP.
159322*/
159323static int analyzeWindowKeyword(const unsigned char *z){
159324  int t;
159325  t = getToken(&z);
159326  if( t!=TK_ID ) return TK_ID;
159327  t = getToken(&z);
159328  if( t!=TK_AS ) return TK_ID;
159329  return TK_WINDOW;
159330}
159331static int analyzeOverKeyword(const unsigned char *z, int lastToken){
159332  if( lastToken==TK_RP ){
159333    int t = getToken(&z);
159334    if( t==TK_LP || t==TK_ID ) return TK_OVER;
159335  }
159336  return TK_ID;
159337}
159338static int analyzeFilterKeyword(const unsigned char *z, int lastToken){
159339  if( lastToken==TK_RP && getToken(&z)==TK_LP ){
159340    return TK_FILTER;
159341  }
159342  return TK_ID;
159343}
159344#endif /* SQLITE_OMIT_WINDOWFUNC */
159345
159346/*
159347** Return the length (in bytes) of the token that begins at z[0].
159348** Store the token type in *tokenType before returning.
159349*/
159350SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
159351  int i, c;
159352  switch( aiClass[*z] ){  /* Switch on the character-class of the first byte
159353                          ** of the token. See the comment on the CC_ defines
159354                          ** above. */
159355    case CC_SPACE: {
159356      testcase( z[0]==' ' );
159357      testcase( z[0]=='\t' );
159358      testcase( z[0]=='\n' );
159359      testcase( z[0]=='\f' );
159360      testcase( z[0]=='\r' );
159361      for(i=1; sqlite3Isspace(z[i]); i++){}
159362      *tokenType = TK_SPACE;
159363      return i;
159364    }
159365    case CC_MINUS: {
159366      if( z[1]=='-' ){
159367        for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
159368        *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
159369        return i;
159370      }
159371      *tokenType = TK_MINUS;
159372      return 1;
159373    }
159374    case CC_LP: {
159375      *tokenType = TK_LP;
159376      return 1;
159377    }
159378    case CC_RP: {
159379      *tokenType = TK_RP;
159380      return 1;
159381    }
159382    case CC_SEMI: {
159383      *tokenType = TK_SEMI;
159384      return 1;
159385    }
159386    case CC_PLUS: {
159387      *tokenType = TK_PLUS;
159388      return 1;
159389    }
159390    case CC_STAR: {
159391      *tokenType = TK_STAR;
159392      return 1;
159393    }
159394    case CC_SLASH: {
159395      if( z[1]!='*' || z[2]==0 ){
159396        *tokenType = TK_SLASH;
159397        return 1;
159398      }
159399      for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
159400      if( c ) i++;
159401      *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
159402      return i;
159403    }
159404    case CC_PERCENT: {
159405      *tokenType = TK_REM;
159406      return 1;
159407    }
159408    case CC_EQ: {
159409      *tokenType = TK_EQ;
159410      return 1 + (z[1]=='=');
159411    }
159412    case CC_LT: {
159413      if( (c=z[1])=='=' ){
159414        *tokenType = TK_LE;
159415        return 2;
159416      }else if( c=='>' ){
159417        *tokenType = TK_NE;
159418        return 2;
159419      }else if( c=='<' ){
159420        *tokenType = TK_LSHIFT;
159421        return 2;
159422      }else{
159423        *tokenType = TK_LT;
159424        return 1;
159425      }
159426    }
159427    case CC_GT: {
159428      if( (c=z[1])=='=' ){
159429        *tokenType = TK_GE;
159430        return 2;
159431      }else if( c=='>' ){
159432        *tokenType = TK_RSHIFT;
159433        return 2;
159434      }else{
159435        *tokenType = TK_GT;
159436        return 1;
159437      }
159438    }
159439    case CC_BANG: {
159440      if( z[1]!='=' ){
159441        *tokenType = TK_ILLEGAL;
159442        return 1;
159443      }else{
159444        *tokenType = TK_NE;
159445        return 2;
159446      }
159447    }
159448    case CC_PIPE: {
159449      if( z[1]!='|' ){
159450        *tokenType = TK_BITOR;
159451        return 1;
159452      }else{
159453        *tokenType = TK_CONCAT;
159454        return 2;
159455      }
159456    }
159457    case CC_COMMA: {
159458      *tokenType = TK_COMMA;
159459      return 1;
159460    }
159461    case CC_AND: {
159462      *tokenType = TK_BITAND;
159463      return 1;
159464    }
159465    case CC_TILDA: {
159466      *tokenType = TK_BITNOT;
159467      return 1;
159468    }
159469    case CC_QUOTE: {
159470      int delim = z[0];
159471      testcase( delim=='`' );
159472      testcase( delim=='\'' );
159473      testcase( delim=='"' );
159474      for(i=1; (c=z[i])!=0; i++){
159475        if( c==delim ){
159476          if( z[i+1]==delim ){
159477            i++;
159478          }else{
159479            break;
159480          }
159481        }
159482      }
159483      if( c=='\'' ){
159484        *tokenType = TK_STRING;
159485        return i+1;
159486      }else if( c!=0 ){
159487        *tokenType = TK_ID;
159488        return i+1;
159489      }else{
159490        *tokenType = TK_ILLEGAL;
159491        return i;
159492      }
159493    }
159494    case CC_DOT: {
159495#ifndef SQLITE_OMIT_FLOATING_POINT
159496      if( !sqlite3Isdigit(z[1]) )
159497#endif
159498      {
159499        *tokenType = TK_DOT;
159500        return 1;
159501      }
159502      /* If the next character is a digit, this is a floating point
159503      ** number that begins with ".".  Fall thru into the next case */
159504    }
159505    case CC_DIGIT: {
159506      testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
159507      testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
159508      testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
159509      testcase( z[0]=='9' );
159510      *tokenType = TK_INTEGER;
159511#ifndef SQLITE_OMIT_HEX_INTEGER
159512      if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
159513        for(i=3; sqlite3Isxdigit(z[i]); i++){}
159514        return i;
159515      }
159516#endif
159517      for(i=0; sqlite3Isdigit(z[i]); i++){}
159518#ifndef SQLITE_OMIT_FLOATING_POINT
159519      if( z[i]=='.' ){
159520        i++;
159521        while( sqlite3Isdigit(z[i]) ){ i++; }
159522        *tokenType = TK_FLOAT;
159523      }
159524      if( (z[i]=='e' || z[i]=='E') &&
159525           ( sqlite3Isdigit(z[i+1])
159526            || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
159527           )
159528      ){
159529        i += 2;
159530        while( sqlite3Isdigit(z[i]) ){ i++; }
159531        *tokenType = TK_FLOAT;
159532      }
159533#endif
159534      while( IdChar(z[i]) ){
159535        *tokenType = TK_ILLEGAL;
159536        i++;
159537      }
159538      return i;
159539    }
159540    case CC_QUOTE2: {
159541      for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
159542      *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
159543      return i;
159544    }
159545    case CC_VARNUM: {
159546      *tokenType = TK_VARIABLE;
159547      for(i=1; sqlite3Isdigit(z[i]); i++){}
159548      return i;
159549    }
159550    case CC_DOLLAR:
159551    case CC_VARALPHA: {
159552      int n = 0;
159553      testcase( z[0]=='$' );  testcase( z[0]=='@' );
159554      testcase( z[0]==':' );  testcase( z[0]=='#' );
159555      *tokenType = TK_VARIABLE;
159556      for(i=1; (c=z[i])!=0; i++){
159557        if( IdChar(c) ){
159558          n++;
159559#ifndef SQLITE_OMIT_TCL_VARIABLE
159560        }else if( c=='(' && n>0 ){
159561          do{
159562            i++;
159563          }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
159564          if( c==')' ){
159565            i++;
159566          }else{
159567            *tokenType = TK_ILLEGAL;
159568          }
159569          break;
159570        }else if( c==':' && z[i+1]==':' ){
159571          i++;
159572#endif
159573        }else{
159574          break;
159575        }
159576      }
159577      if( n==0 ) *tokenType = TK_ILLEGAL;
159578      return i;
159579    }
159580    case CC_KYWD: {
159581      for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
159582      if( IdChar(z[i]) ){
159583        /* This token started out using characters that can appear in keywords,
159584        ** but z[i] is a character not allowed within keywords, so this must
159585        ** be an identifier instead */
159586        i++;
159587        break;
159588      }
159589      *tokenType = TK_ID;
159590      return keywordCode((char*)z, i, tokenType);
159591    }
159592    case CC_X: {
159593#ifndef SQLITE_OMIT_BLOB_LITERAL
159594      testcase( z[0]=='x' ); testcase( z[0]=='X' );
159595      if( z[1]=='\'' ){
159596        *tokenType = TK_BLOB;
159597        for(i=2; sqlite3Isxdigit(z[i]); i++){}
159598        if( z[i]!='\'' || i%2 ){
159599          *tokenType = TK_ILLEGAL;
159600          while( z[i] && z[i]!='\'' ){ i++; }
159601        }
159602        if( z[i] ) i++;
159603        return i;
159604      }
159605#endif
159606      /* If it is not a BLOB literal, then it must be an ID, since no
159607      ** SQL keywords start with the letter 'x'.  Fall through */
159608    }
159609    case CC_ID: {
159610      i = 1;
159611      break;
159612    }
159613    case CC_NUL: {
159614      *tokenType = TK_ILLEGAL;
159615      return 0;
159616    }
159617    default: {
159618      *tokenType = TK_ILLEGAL;
159619      return 1;
159620    }
159621  }
159622  while( IdChar(z[i]) ){ i++; }
159623  *tokenType = TK_ID;
159624  return i;
159625}
159626
159627/*
159628** Run the parser on the given SQL string.  The parser structure is
159629** passed in.  An SQLITE_ status code is returned.  If an error occurs
159630** then an and attempt is made to write an error message into
159631** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
159632** error message.
159633*/
159634SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
159635  int nErr = 0;                   /* Number of errors encountered */
159636  void *pEngine;                  /* The LEMON-generated LALR(1) parser */
159637  int n = 0;                      /* Length of the next token token */
159638  int tokenType;                  /* type of the next token */
159639  int lastTokenParsed = -1;       /* type of the previous token */
159640  sqlite3 *db = pParse->db;       /* The database connection */
159641  int mxSqlLen;                   /* Max length of an SQL string */
159642#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
159643  yyParser sEngine;    /* Space to hold the Lemon-generated Parser object */
159644#endif
159645  VVA_ONLY( u8 startedWithOom = db->mallocFailed );
159646
159647  assert( zSql!=0 );
159648  mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
159649  if( db->nVdbeActive==0 ){
159650    AtomicStore(&db->u1.isInterrupted, 0);
159651  }
159652  pParse->rc = SQLITE_OK;
159653  pParse->zTail = zSql;
159654  assert( pzErrMsg!=0 );
159655#ifdef SQLITE_DEBUG
159656  if( db->flags & SQLITE_ParserTrace ){
159657    printf("parser: [[[%s]]]\n", zSql);
159658    sqlite3ParserTrace(stdout, "parser: ");
159659  }else{
159660    sqlite3ParserTrace(0, 0);
159661  }
159662#endif
159663#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
159664  pEngine = &sEngine;
159665  sqlite3ParserInit(pEngine, pParse);
159666#else
159667  pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse);
159668  if( pEngine==0 ){
159669    sqlite3OomFault(db);
159670    return SQLITE_NOMEM_BKPT;
159671  }
159672#endif
159673  assert( pParse->pNewTable==0 );
159674  assert( pParse->pNewTrigger==0 );
159675  assert( pParse->nVar==0 );
159676  assert( pParse->pVList==0 );
159677  pParse->pParentParse = db->pParse;
159678  db->pParse = pParse;
159679  while( 1 ){
159680    n = sqlite3GetToken((u8*)zSql, &tokenType);
159681    mxSqlLen -= n;
159682    if( mxSqlLen<0 ){
159683      pParse->rc = SQLITE_TOOBIG;
159684      break;
159685    }
159686#ifndef SQLITE_OMIT_WINDOWFUNC
159687    if( tokenType>=TK_WINDOW ){
159688      assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER
159689           || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW
159690      );
159691#else
159692    if( tokenType>=TK_SPACE ){
159693      assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
159694#endif /* SQLITE_OMIT_WINDOWFUNC */
159695      if( AtomicLoad(&db->u1.isInterrupted) ){
159696        pParse->rc = SQLITE_INTERRUPT;
159697        break;
159698      }
159699      if( tokenType==TK_SPACE ){
159700        zSql += n;
159701        continue;
159702      }
159703      if( zSql[0]==0 ){
159704        /* Upon reaching the end of input, call the parser two more times
159705        ** with tokens TK_SEMI and 0, in that order. */
159706        if( lastTokenParsed==TK_SEMI ){
159707          tokenType = 0;
159708        }else if( lastTokenParsed==0 ){
159709          break;
159710        }else{
159711          tokenType = TK_SEMI;
159712        }
159713        n = 0;
159714#ifndef SQLITE_OMIT_WINDOWFUNC
159715      }else if( tokenType==TK_WINDOW ){
159716        assert( n==6 );
159717        tokenType = analyzeWindowKeyword((const u8*)&zSql[6]);
159718      }else if( tokenType==TK_OVER ){
159719        assert( n==4 );
159720        tokenType = analyzeOverKeyword((const u8*)&zSql[4], lastTokenParsed);
159721      }else if( tokenType==TK_FILTER ){
159722        assert( n==6 );
159723        tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed);
159724#endif /* SQLITE_OMIT_WINDOWFUNC */
159725      }else{
159726        sqlite3ErrorMsg(pParse, "unrecognized token: \"%.*s\"", n, zSql);
159727        break;
159728      }
159729    }
159730    pParse->sLastToken.z = zSql;
159731    pParse->sLastToken.n = n;
159732    sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
159733    lastTokenParsed = tokenType;
159734    zSql += n;
159735    assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom );
159736    if( pParse->rc!=SQLITE_OK ) break;
159737  }
159738  assert( nErr==0 );
159739#ifdef YYTRACKMAXSTACKDEPTH
159740  sqlite3_mutex_enter(sqlite3MallocMutex());
159741  sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
159742      sqlite3ParserStackPeak(pEngine)
159743  );
159744  sqlite3_mutex_leave(sqlite3MallocMutex());
159745#endif /* YYDEBUG */
159746#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
159747  sqlite3ParserFinalize(pEngine);
159748#else
159749  sqlite3ParserFree(pEngine, sqlite3_free);
159750#endif
159751  if( db->mallocFailed ){
159752    pParse->rc = SQLITE_NOMEM_BKPT;
159753  }
159754  if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
159755    pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
159756  }
159757  assert( pzErrMsg!=0 );
159758  if( pParse->zErrMsg ){
159759    *pzErrMsg = pParse->zErrMsg;
159760    sqlite3_log(pParse->rc, "%s in \"%s\"",
159761                *pzErrMsg, pParse->zTail);
159762    pParse->zErrMsg = 0;
159763    nErr++;
159764  }
159765  pParse->zTail = zSql;
159766  if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
159767    sqlite3VdbeDelete(pParse->pVdbe);
159768    pParse->pVdbe = 0;
159769  }
159770#ifndef SQLITE_OMIT_SHARED_CACHE
159771  if( pParse->nested==0 ){
159772    sqlite3DbFree(db, pParse->aTableLock);
159773    pParse->aTableLock = 0;
159774    pParse->nTableLock = 0;
159775  }
159776#endif
159777#ifndef SQLITE_OMIT_VIRTUALTABLE
159778  sqlite3_free(pParse->apVtabLock);
159779#endif
159780
159781  if( !IN_SPECIAL_PARSE ){
159782    /* If the pParse->declareVtab flag is set, do not delete any table
159783    ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
159784    ** will take responsibility for freeing the Table structure.
159785    */
159786    sqlite3DeleteTable(db, pParse->pNewTable);
159787  }
159788  if( !IN_RENAME_OBJECT ){
159789    sqlite3DeleteTrigger(db, pParse->pNewTrigger);
159790  }
159791
159792  if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
159793  sqlite3DbFree(db, pParse->pVList);
159794  while( pParse->pAinc ){
159795    AutoincInfo *p = pParse->pAinc;
159796    pParse->pAinc = p->pNext;
159797    sqlite3DbFreeNN(db, p);
159798  }
159799  while( pParse->pZombieTab ){
159800    Table *p = pParse->pZombieTab;
159801    pParse->pZombieTab = p->pNextZombie;
159802    sqlite3DeleteTable(db, p);
159803  }
159804  db->pParse = pParse->pParentParse;
159805  pParse->pParentParse = 0;
159806  assert( nErr==0 || pParse->rc!=SQLITE_OK );
159807  return nErr;
159808}
159809
159810
159811#ifdef SQLITE_ENABLE_NORMALIZE
159812/*
159813** Insert a single space character into pStr if the current string
159814** ends with an identifier
159815*/
159816static void addSpaceSeparator(sqlite3_str *pStr){
159817  if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
159818    sqlite3_str_append(pStr, " ", 1);
159819  }
159820}
159821
159822/*
159823** Compute a normalization of the SQL given by zSql[0..nSql-1].  Return
159824** the normalization in space obtained from sqlite3DbMalloc().  Or return
159825** NULL if anything goes wrong or if zSql is NULL.
159826*/
159827SQLITE_PRIVATE char *sqlite3Normalize(
159828  Vdbe *pVdbe,       /* VM being reprepared */
159829  const char *zSql   /* The original SQL string */
159830){
159831  sqlite3 *db;       /* The database connection */
159832  int i;             /* Next unread byte of zSql[] */
159833  int n;             /* length of current token */
159834  int tokenType;     /* type of current token */
159835  int prevType = 0;  /* Previous non-whitespace token */
159836  int nParen;        /* Number of nested levels of parentheses */
159837  int iStartIN;      /* Start of RHS of IN operator in z[] */
159838  int nParenAtIN;    /* Value of nParent at start of RHS of IN operator */
159839  u32 j;             /* Bytes of normalized SQL generated so far */
159840  sqlite3_str *pStr; /* The normalized SQL string under construction */
159841
159842  db = sqlite3VdbeDb(pVdbe);
159843  tokenType = -1;
159844  nParen = iStartIN = nParenAtIN = 0;
159845  pStr = sqlite3_str_new(db);
159846  assert( pStr!=0 );  /* sqlite3_str_new() never returns NULL */
159847  for(i=0; zSql[i] && pStr->accError==0; i+=n){
159848    if( tokenType!=TK_SPACE ){
159849      prevType = tokenType;
159850    }
159851    n = sqlite3GetToken((unsigned char*)zSql+i, &tokenType);
159852    if( NEVER(n<=0) ) break;
159853    switch( tokenType ){
159854      case TK_SPACE: {
159855        break;
159856      }
159857      case TK_NULL: {
159858        if( prevType==TK_IS || prevType==TK_NOT ){
159859          sqlite3_str_append(pStr, " NULL", 5);
159860          break;
159861        }
159862        /* Fall through */
159863      }
159864      case TK_STRING:
159865      case TK_INTEGER:
159866      case TK_FLOAT:
159867      case TK_VARIABLE:
159868      case TK_BLOB: {
159869        sqlite3_str_append(pStr, "?", 1);
159870        break;
159871      }
159872      case TK_LP: {
159873        nParen++;
159874        if( prevType==TK_IN ){
159875          iStartIN = pStr->nChar;
159876          nParenAtIN = nParen;
159877        }
159878        sqlite3_str_append(pStr, "(", 1);
159879        break;
159880      }
159881      case TK_RP: {
159882        if( iStartIN>0 && nParen==nParenAtIN ){
159883          assert( pStr->nChar>=(u32)iStartIN );
159884          pStr->nChar = iStartIN+1;
159885          sqlite3_str_append(pStr, "?,?,?", 5);
159886          iStartIN = 0;
159887        }
159888        nParen--;
159889        sqlite3_str_append(pStr, ")", 1);
159890        break;
159891      }
159892      case TK_ID: {
159893        iStartIN = 0;
159894        j = pStr->nChar;
159895        if( sqlite3Isquote(zSql[i]) ){
159896          char *zId = sqlite3DbStrNDup(db, zSql+i, n);
159897          int nId;
159898          int eType = 0;
159899          if( zId==0 ) break;
159900          sqlite3Dequote(zId);
159901          if( zSql[i]=='"' && sqlite3VdbeUsesDoubleQuotedString(pVdbe, zId) ){
159902            sqlite3_str_append(pStr, "?", 1);
159903            sqlite3DbFree(db, zId);
159904            break;
159905          }
159906          nId = sqlite3Strlen30(zId);
159907          if( sqlite3GetToken((u8*)zId, &eType)==nId && eType==TK_ID ){
159908            addSpaceSeparator(pStr);
159909            sqlite3_str_append(pStr, zId, nId);
159910          }else{
159911            sqlite3_str_appendf(pStr, "\"%w\"", zId);
159912          }
159913          sqlite3DbFree(db, zId);
159914        }else{
159915          addSpaceSeparator(pStr);
159916          sqlite3_str_append(pStr, zSql+i, n);
159917        }
159918        while( j<pStr->nChar ){
159919          pStr->zText[j] = sqlite3Tolower(pStr->zText[j]);
159920          j++;
159921        }
159922        break;
159923      }
159924      case TK_SELECT: {
159925        iStartIN = 0;
159926        /* fall through */
159927      }
159928      default: {
159929        if( sqlite3IsIdChar(zSql[i]) ) addSpaceSeparator(pStr);
159930        j = pStr->nChar;
159931        sqlite3_str_append(pStr, zSql+i, n);
159932        while( j<pStr->nChar ){
159933          pStr->zText[j] = sqlite3Toupper(pStr->zText[j]);
159934          j++;
159935        }
159936        break;
159937      }
159938    }
159939  }
159940  if( tokenType!=TK_SEMI ) sqlite3_str_append(pStr, ";", 1);
159941  return sqlite3_str_finish(pStr);
159942}
159943#endif /* SQLITE_ENABLE_NORMALIZE */
159944
159945/************** End of tokenize.c ********************************************/
159946/************** Begin file complete.c ****************************************/
159947/*
159948** 2001 September 15
159949**
159950** The author disclaims copyright to this source code.  In place of
159951** a legal notice, here is a blessing:
159952**
159953**    May you do good and not evil.
159954**    May you find forgiveness for yourself and forgive others.
159955**    May you share freely, never taking more than you give.
159956**
159957*************************************************************************
159958** An tokenizer for SQL
159959**
159960** This file contains C code that implements the sqlite3_complete() API.
159961** This code used to be part of the tokenizer.c source file.  But by
159962** separating it out, the code will be automatically omitted from
159963** static links that do not use it.
159964*/
159965/* #include "sqliteInt.h" */
159966#ifndef SQLITE_OMIT_COMPLETE
159967
159968/*
159969** This is defined in tokenize.c.  We just have to import the definition.
159970*/
159971#ifndef SQLITE_AMALGAMATION
159972#ifdef SQLITE_ASCII
159973#define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
159974#endif
159975#ifdef SQLITE_EBCDIC
159976SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
159977#define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
159978#endif
159979#endif /* SQLITE_AMALGAMATION */
159980
159981
159982/*
159983** Token types used by the sqlite3_complete() routine.  See the header
159984** comments on that procedure for additional information.
159985*/
159986#define tkSEMI    0
159987#define tkWS      1
159988#define tkOTHER   2
159989#ifndef SQLITE_OMIT_TRIGGER
159990#define tkEXPLAIN 3
159991#define tkCREATE  4
159992#define tkTEMP    5
159993#define tkTRIGGER 6
159994#define tkEND     7
159995#endif
159996
159997/*
159998** Return TRUE if the given SQL string ends in a semicolon.
159999**
160000** Special handling is require for CREATE TRIGGER statements.
160001** Whenever the CREATE TRIGGER keywords are seen, the statement
160002** must end with ";END;".
160003**
160004** This implementation uses a state machine with 8 states:
160005**
160006**   (0) INVALID   We have not yet seen a non-whitespace character.
160007**
160008**   (1) START     At the beginning or end of an SQL statement.  This routine
160009**                 returns 1 if it ends in the START state and 0 if it ends
160010**                 in any other state.
160011**
160012**   (2) NORMAL    We are in the middle of statement which ends with a single
160013**                 semicolon.
160014**
160015**   (3) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of
160016**                 a statement.
160017**
160018**   (4) CREATE    The keyword CREATE has been seen at the beginning of a
160019**                 statement, possibly preceded by EXPLAIN and/or followed by
160020**                 TEMP or TEMPORARY
160021**
160022**   (5) TRIGGER   We are in the middle of a trigger definition that must be
160023**                 ended by a semicolon, the keyword END, and another semicolon.
160024**
160025**   (6) SEMI      We've seen the first semicolon in the ";END;" that occurs at
160026**                 the end of a trigger definition.
160027**
160028**   (7) END       We've seen the ";END" of the ";END;" that occurs at the end
160029**                 of a trigger definition.
160030**
160031** Transitions between states above are determined by tokens extracted
160032** from the input.  The following tokens are significant:
160033**
160034**   (0) tkSEMI      A semicolon.
160035**   (1) tkWS        Whitespace.
160036**   (2) tkOTHER     Any other SQL token.
160037**   (3) tkEXPLAIN   The "explain" keyword.
160038**   (4) tkCREATE    The "create" keyword.
160039**   (5) tkTEMP      The "temp" or "temporary" keyword.
160040**   (6) tkTRIGGER   The "trigger" keyword.
160041**   (7) tkEND       The "end" keyword.
160042**
160043** Whitespace never causes a state transition and is always ignored.
160044** This means that a SQL string of all whitespace is invalid.
160045**
160046** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
160047** to recognize the end of a trigger can be omitted.  All we have to do
160048** is look for a semicolon that is not part of an string or comment.
160049*/
160050SQLITE_API int sqlite3_complete(const char *zSql){
160051  u8 state = 0;   /* Current state, using numbers defined in header comment */
160052  u8 token;       /* Value of the next token */
160053
160054#ifndef SQLITE_OMIT_TRIGGER
160055  /* A complex statement machine used to detect the end of a CREATE TRIGGER
160056  ** statement.  This is the normal case.
160057  */
160058  static const u8 trans[8][8] = {
160059                     /* Token:                                                */
160060     /* State:       **  SEMI  WS  OTHER  EXPLAIN  CREATE  TEMP  TRIGGER  END */
160061     /* 0 INVALID: */ {    1,  0,     2,       3,      4,    2,       2,   2, },
160062     /* 1   START: */ {    1,  1,     2,       3,      4,    2,       2,   2, },
160063     /* 2  NORMAL: */ {    1,  2,     2,       2,      2,    2,       2,   2, },
160064     /* 3 EXPLAIN: */ {    1,  3,     3,       2,      4,    2,       2,   2, },
160065     /* 4  CREATE: */ {    1,  4,     2,       2,      2,    4,       5,   2, },
160066     /* 5 TRIGGER: */ {    6,  5,     5,       5,      5,    5,       5,   5, },
160067     /* 6    SEMI: */ {    6,  6,     5,       5,      5,    5,       5,   7, },
160068     /* 7     END: */ {    1,  7,     5,       5,      5,    5,       5,   5, },
160069  };
160070#else
160071  /* If triggers are not supported by this compile then the statement machine
160072  ** used to detect the end of a statement is much simpler
160073  */
160074  static const u8 trans[3][3] = {
160075                     /* Token:           */
160076     /* State:       **  SEMI  WS  OTHER */
160077     /* 0 INVALID: */ {    1,  0,     2, },
160078     /* 1   START: */ {    1,  1,     2, },
160079     /* 2  NORMAL: */ {    1,  2,     2, },
160080  };
160081#endif /* SQLITE_OMIT_TRIGGER */
160082
160083#ifdef SQLITE_ENABLE_API_ARMOR
160084  if( zSql==0 ){
160085    (void)SQLITE_MISUSE_BKPT;
160086    return 0;
160087  }
160088#endif
160089
160090  while( *zSql ){
160091    switch( *zSql ){
160092      case ';': {  /* A semicolon */
160093        token = tkSEMI;
160094        break;
160095      }
160096      case ' ':
160097      case '\r':
160098      case '\t':
160099      case '\n':
160100      case '\f': {  /* White space is ignored */
160101        token = tkWS;
160102        break;
160103      }
160104      case '/': {   /* C-style comments */
160105        if( zSql[1]!='*' ){
160106          token = tkOTHER;
160107          break;
160108        }
160109        zSql += 2;
160110        while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
160111        if( zSql[0]==0 ) return 0;
160112        zSql++;
160113        token = tkWS;
160114        break;
160115      }
160116      case '-': {   /* SQL-style comments from "--" to end of line */
160117        if( zSql[1]!='-' ){
160118          token = tkOTHER;
160119          break;
160120        }
160121        while( *zSql && *zSql!='\n' ){ zSql++; }
160122        if( *zSql==0 ) return state==1;
160123        token = tkWS;
160124        break;
160125      }
160126      case '[': {   /* Microsoft-style identifiers in [...] */
160127        zSql++;
160128        while( *zSql && *zSql!=']' ){ zSql++; }
160129        if( *zSql==0 ) return 0;
160130        token = tkOTHER;
160131        break;
160132      }
160133      case '`':     /* Grave-accent quoted symbols used by MySQL */
160134      case '"':     /* single- and double-quoted strings */
160135      case '\'': {
160136        int c = *zSql;
160137        zSql++;
160138        while( *zSql && *zSql!=c ){ zSql++; }
160139        if( *zSql==0 ) return 0;
160140        token = tkOTHER;
160141        break;
160142      }
160143      default: {
160144#ifdef SQLITE_EBCDIC
160145        unsigned char c;
160146#endif
160147        if( IdChar((u8)*zSql) ){
160148          /* Keywords and unquoted identifiers */
160149          int nId;
160150          for(nId=1; IdChar(zSql[nId]); nId++){}
160151#ifdef SQLITE_OMIT_TRIGGER
160152          token = tkOTHER;
160153#else
160154          switch( *zSql ){
160155            case 'c': case 'C': {
160156              if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
160157                token = tkCREATE;
160158              }else{
160159                token = tkOTHER;
160160              }
160161              break;
160162            }
160163            case 't': case 'T': {
160164              if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
160165                token = tkTRIGGER;
160166              }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
160167                token = tkTEMP;
160168              }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
160169                token = tkTEMP;
160170              }else{
160171                token = tkOTHER;
160172              }
160173              break;
160174            }
160175            case 'e':  case 'E': {
160176              if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
160177                token = tkEND;
160178              }else
160179#ifndef SQLITE_OMIT_EXPLAIN
160180              if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
160181                token = tkEXPLAIN;
160182              }else
160183#endif
160184              {
160185                token = tkOTHER;
160186              }
160187              break;
160188            }
160189            default: {
160190              token = tkOTHER;
160191              break;
160192            }
160193          }
160194#endif /* SQLITE_OMIT_TRIGGER */
160195          zSql += nId-1;
160196        }else{
160197          /* Operators and special symbols */
160198          token = tkOTHER;
160199        }
160200        break;
160201      }
160202    }
160203    state = trans[state][token];
160204    zSql++;
160205  }
160206  return state==1;
160207}
160208
160209#ifndef SQLITE_OMIT_UTF16
160210/*
160211** This routine is the same as the sqlite3_complete() routine described
160212** above, except that the parameter is required to be UTF-16 encoded, not
160213** UTF-8.
160214*/
160215SQLITE_API int sqlite3_complete16(const void *zSql){
160216  sqlite3_value *pVal;
160217  char const *zSql8;
160218  int rc;
160219
160220#ifndef SQLITE_OMIT_AUTOINIT
160221  rc = sqlite3_initialize();
160222  if( rc ) return rc;
160223#endif
160224  pVal = sqlite3ValueNew(0);
160225  sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
160226  zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
160227  if( zSql8 ){
160228    rc = sqlite3_complete(zSql8);
160229  }else{
160230    rc = SQLITE_NOMEM_BKPT;
160231  }
160232  sqlite3ValueFree(pVal);
160233  return rc & 0xff;
160234}
160235#endif /* SQLITE_OMIT_UTF16 */
160236#endif /* SQLITE_OMIT_COMPLETE */
160237
160238/************** End of complete.c ********************************************/
160239/************** Begin file main.c ********************************************/
160240/*
160241** 2001 September 15
160242**
160243** The author disclaims copyright to this source code.  In place of
160244** a legal notice, here is a blessing:
160245**
160246**    May you do good and not evil.
160247**    May you find forgiveness for yourself and forgive others.
160248**    May you share freely, never taking more than you give.
160249**
160250*************************************************************************
160251** Main file for the SQLite library.  The routines in this file
160252** implement the programmer interface to the library.  Routines in
160253** other files are for internal use by SQLite and should not be
160254** accessed by users of the library.
160255*/
160256/* #include "sqliteInt.h" */
160257
160258#ifdef SQLITE_ENABLE_FTS3
160259/************** Include fts3.h in the middle of main.c ***********************/
160260/************** Begin file fts3.h ********************************************/
160261/*
160262** 2006 Oct 10
160263**
160264** The author disclaims copyright to this source code.  In place of
160265** a legal notice, here is a blessing:
160266**
160267**    May you do good and not evil.
160268**    May you find forgiveness for yourself and forgive others.
160269**    May you share freely, never taking more than you give.
160270**
160271******************************************************************************
160272**
160273** This header file is used by programs that want to link against the
160274** FTS3 library.  All it does is declare the sqlite3Fts3Init() interface.
160275*/
160276/* #include "sqlite3.h" */
160277
160278#if 0
160279extern "C" {
160280#endif  /* __cplusplus */
160281
160282SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
160283
160284#if 0
160285}  /* extern "C" */
160286#endif  /* __cplusplus */
160287
160288/************** End of fts3.h ************************************************/
160289/************** Continuing where we left off in main.c ***********************/
160290#endif
160291#ifdef SQLITE_ENABLE_RTREE
160292/************** Include rtree.h in the middle of main.c **********************/
160293/************** Begin file rtree.h *******************************************/
160294/*
160295** 2008 May 26
160296**
160297** The author disclaims copyright to this source code.  In place of
160298** a legal notice, here is a blessing:
160299**
160300**    May you do good and not evil.
160301**    May you find forgiveness for yourself and forgive others.
160302**    May you share freely, never taking more than you give.
160303**
160304******************************************************************************
160305**
160306** This header file is used by programs that want to link against the
160307** RTREE library.  All it does is declare the sqlite3RtreeInit() interface.
160308*/
160309/* #include "sqlite3.h" */
160310
160311#ifdef SQLITE_OMIT_VIRTUALTABLE
160312# undef SQLITE_ENABLE_RTREE
160313#endif
160314
160315#if 0
160316extern "C" {
160317#endif  /* __cplusplus */
160318
160319SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
160320
160321#if 0
160322}  /* extern "C" */
160323#endif  /* __cplusplus */
160324
160325/************** End of rtree.h ***********************************************/
160326/************** Continuing where we left off in main.c ***********************/
160327#endif
160328#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
160329/************** Include sqliteicu.h in the middle of main.c ******************/
160330/************** Begin file sqliteicu.h ***************************************/
160331/*
160332** 2008 May 26
160333**
160334** The author disclaims copyright to this source code.  In place of
160335** a legal notice, here is a blessing:
160336**
160337**    May you do good and not evil.
160338**    May you find forgiveness for yourself and forgive others.
160339**    May you share freely, never taking more than you give.
160340**
160341******************************************************************************
160342**
160343** This header file is used by programs that want to link against the
160344** ICU extension.  All it does is declare the sqlite3IcuInit() interface.
160345*/
160346/* #include "sqlite3.h" */
160347
160348#if 0
160349extern "C" {
160350#endif  /* __cplusplus */
160351
160352SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
160353
160354#if 0
160355}  /* extern "C" */
160356#endif  /* __cplusplus */
160357
160358
160359/************** End of sqliteicu.h *******************************************/
160360/************** Continuing where we left off in main.c ***********************/
160361#endif
160362
160363/*
160364** This is an extension initializer that is a no-op and always
160365** succeeds, except that it fails if the fault-simulation is set
160366** to 500.
160367*/
160368static int sqlite3TestExtInit(sqlite3 *db){
160369  (void)db;
160370  return sqlite3FaultSim(500);
160371}
160372
160373
160374/*
160375** Forward declarations of external module initializer functions
160376** for modules that need them.
160377*/
160378#ifdef SQLITE_ENABLE_FTS1
160379SQLITE_PRIVATE int sqlite3Fts1Init(sqlite3*);
160380#endif
160381#ifdef SQLITE_ENABLE_FTS2
160382SQLITE_PRIVATE int sqlite3Fts2Init(sqlite3*);
160383#endif
160384#ifdef SQLITE_ENABLE_FTS5
160385SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3*);
160386#endif
160387#ifdef SQLITE_ENABLE_JSON1
160388SQLITE_PRIVATE int sqlite3Json1Init(sqlite3*);
160389#endif
160390#ifdef SQLITE_ENABLE_STMTVTAB
160391SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3*);
160392#endif
160393
160394/*
160395** An array of pointers to extension initializer functions for
160396** built-in extensions.
160397*/
160398static int (*const sqlite3BuiltinExtensions[])(sqlite3*) = {
160399#ifdef SQLITE_ENABLE_FTS1
160400  sqlite3Fts1Init,
160401#endif
160402#ifdef SQLITE_ENABLE_FTS2
160403  sqlite3Fts2Init,
160404#endif
160405#ifdef SQLITE_ENABLE_FTS3
160406  sqlite3Fts3Init,
160407#endif
160408#ifdef SQLITE_ENABLE_FTS5
160409  sqlite3Fts5Init,
160410#endif
160411#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
160412  sqlite3IcuInit,
160413#endif
160414#ifdef SQLITE_ENABLE_RTREE
160415  sqlite3RtreeInit,
160416#endif
160417#ifdef SQLITE_ENABLE_DBPAGE_VTAB
160418  sqlite3DbpageRegister,
160419#endif
160420#ifdef SQLITE_ENABLE_DBSTAT_VTAB
160421  sqlite3DbstatRegister,
160422#endif
160423  sqlite3TestExtInit,
160424#ifdef SQLITE_ENABLE_JSON1
160425  sqlite3Json1Init,
160426#endif
160427#ifdef SQLITE_ENABLE_STMTVTAB
160428  sqlite3StmtVtabInit,
160429#endif
160430#ifdef SQLITE_ENABLE_BYTECODE_VTAB
160431  sqlite3VdbeBytecodeVtabInit,
160432#endif
160433};
160434
160435#ifndef SQLITE_AMALGAMATION
160436/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
160437** contains the text of SQLITE_VERSION macro.
160438*/
160439SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
160440#endif
160441
160442/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
160443** a pointer to the to the sqlite3_version[] string constant.
160444*/
160445SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
160446
160447/* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
160448** pointer to a string constant whose value is the same as the
160449** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using
160450** an edited copy of the amalgamation, then the last four characters of
160451** the hash might be different from SQLITE_SOURCE_ID.
160452*/
160453/* SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } */
160454
160455/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
160456** returns an integer equal to SQLITE_VERSION_NUMBER.
160457*/
160458SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
160459
160460/* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
160461** zero if and only if SQLite was compiled with mutexing code omitted due to
160462** the SQLITE_THREADSAFE compile-time option being set to 0.
160463*/
160464SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
160465
160466/*
160467** When compiling the test fixture or with debugging enabled (on Win32),
160468** this variable being set to non-zero will cause OSTRACE macros to emit
160469** extra diagnostic information.
160470*/
160471#ifdef SQLITE_HAVE_OS_TRACE
160472# ifndef SQLITE_DEBUG_OS_TRACE
160473#   define SQLITE_DEBUG_OS_TRACE 0
160474# endif
160475  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
160476#endif
160477
160478#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
160479/*
160480** If the following function pointer is not NULL and if
160481** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
160482** I/O active are written using this function.  These messages
160483** are intended for debugging activity only.
160484*/
160485SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
160486#endif
160487
160488/*
160489** If the following global variable points to a string which is the
160490** name of a directory, then that directory will be used to store
160491** temporary files.
160492**
160493** See also the "PRAGMA temp_store_directory" SQL command.
160494*/
160495SQLITE_API char *sqlite3_temp_directory = 0;
160496
160497/*
160498** If the following global variable points to a string which is the
160499** name of a directory, then that directory will be used to store
160500** all database files specified with a relative pathname.
160501**
160502** See also the "PRAGMA data_store_directory" SQL command.
160503*/
160504SQLITE_API char *sqlite3_data_directory = 0;
160505
160506/*
160507** Initialize SQLite.
160508**
160509** This routine must be called to initialize the memory allocation,
160510** VFS, and mutex subsystems prior to doing any serious work with
160511** SQLite.  But as long as you do not compile with SQLITE_OMIT_AUTOINIT
160512** this routine will be called automatically by key routines such as
160513** sqlite3_open().
160514**
160515** This routine is a no-op except on its very first call for the process,
160516** or for the first call after a call to sqlite3_shutdown.
160517**
160518** The first thread to call this routine runs the initialization to
160519** completion.  If subsequent threads call this routine before the first
160520** thread has finished the initialization process, then the subsequent
160521** threads must block until the first thread finishes with the initialization.
160522**
160523** The first thread might call this routine recursively.  Recursive
160524** calls to this routine should not block, of course.  Otherwise the
160525** initialization process would never complete.
160526**
160527** Let X be the first thread to enter this routine.  Let Y be some other
160528** thread.  Then while the initial invocation of this routine by X is
160529** incomplete, it is required that:
160530**
160531**    *  Calls to this routine from Y must block until the outer-most
160532**       call by X completes.
160533**
160534**    *  Recursive calls to this routine from thread X return immediately
160535**       without blocking.
160536*/
160537SQLITE_API int sqlite3_initialize(void){
160538  MUTEX_LOGIC( sqlite3_mutex *pMaster; )       /* The main static mutex */
160539  int rc;                                      /* Result code */
160540#ifdef SQLITE_EXTRA_INIT
160541  int bRunExtraInit = 0;                       /* Extra initialization needed */
160542#endif
160543
160544#ifdef SQLITE_OMIT_WSD
160545  rc = sqlite3_wsd_init(4096, 24);
160546  if( rc!=SQLITE_OK ){
160547    return rc;
160548  }
160549#endif
160550
160551  /* If the following assert() fails on some obscure processor/compiler
160552  ** combination, the work-around is to set the correct pointer
160553  ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
160554  assert( SQLITE_PTRSIZE==sizeof(char*) );
160555
160556  /* If SQLite is already completely initialized, then this call
160557  ** to sqlite3_initialize() should be a no-op.  But the initialization
160558  ** must be complete.  So isInit must not be set until the very end
160559  ** of this routine.
160560  */
160561  if( sqlite3GlobalConfig.isInit ){
160562    sqlite3MemoryBarrier();
160563    return SQLITE_OK;
160564  }
160565
160566  /* Make sure the mutex subsystem is initialized.  If unable to
160567  ** initialize the mutex subsystem, return early with the error.
160568  ** If the system is so sick that we are unable to allocate a mutex,
160569  ** there is not much SQLite is going to be able to do.
160570  **
160571  ** The mutex subsystem must take care of serializing its own
160572  ** initialization.
160573  */
160574  rc = sqlite3MutexInit();
160575  if( rc ) return rc;
160576
160577  /* Initialize the malloc() system and the recursive pInitMutex mutex.
160578  ** This operation is protected by the STATIC_MASTER mutex.  Note that
160579  ** MutexAlloc() is called for a static mutex prior to initializing the
160580  ** malloc subsystem - this implies that the allocation of a static
160581  ** mutex must not require support from the malloc subsystem.
160582  */
160583  MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
160584  sqlite3_mutex_enter(pMaster);
160585  sqlite3GlobalConfig.isMutexInit = 1;
160586  if( !sqlite3GlobalConfig.isMallocInit ){
160587    rc = sqlite3MallocInit();
160588  }
160589  if( rc==SQLITE_OK ){
160590    sqlite3GlobalConfig.isMallocInit = 1;
160591    if( !sqlite3GlobalConfig.pInitMutex ){
160592      sqlite3GlobalConfig.pInitMutex =
160593           sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
160594      if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
160595        rc = SQLITE_NOMEM_BKPT;
160596      }
160597    }
160598  }
160599  if( rc==SQLITE_OK ){
160600    sqlite3GlobalConfig.nRefInitMutex++;
160601  }
160602  sqlite3_mutex_leave(pMaster);
160603
160604  /* If rc is not SQLITE_OK at this point, then either the malloc
160605  ** subsystem could not be initialized or the system failed to allocate
160606  ** the pInitMutex mutex. Return an error in either case.  */
160607  if( rc!=SQLITE_OK ){
160608    return rc;
160609  }
160610
160611  /* Do the rest of the initialization under the recursive mutex so
160612  ** that we will be able to handle recursive calls into
160613  ** sqlite3_initialize().  The recursive calls normally come through
160614  ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
160615  ** recursive calls might also be possible.
160616  **
160617  ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
160618  ** to the xInit method, so the xInit method need not be threadsafe.
160619  **
160620  ** The following mutex is what serializes access to the appdef pcache xInit
160621  ** methods.  The sqlite3_pcache_methods.xInit() all is embedded in the
160622  ** call to sqlite3PcacheInitialize().
160623  */
160624  sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
160625  if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
160626    sqlite3GlobalConfig.inProgress = 1;
160627#ifdef SQLITE_ENABLE_SQLLOG
160628    {
160629      extern void sqlite3_init_sqllog(void);
160630      sqlite3_init_sqllog();
160631    }
160632#endif
160633    memset(&sqlite3BuiltinFunctions, 0, sizeof(sqlite3BuiltinFunctions));
160634    sqlite3RegisterBuiltinFunctions();
160635    if( sqlite3GlobalConfig.isPCacheInit==0 ){
160636      rc = sqlite3PcacheInitialize();
160637    }
160638    if( rc==SQLITE_OK ){
160639      sqlite3GlobalConfig.isPCacheInit = 1;
160640      rc = sqlite3OsInit();
160641    }
160642#ifdef SQLITE_ENABLE_DESERIALIZE
160643    if( rc==SQLITE_OK ){
160644      rc = sqlite3MemdbInit();
160645    }
160646#endif
160647    if( rc==SQLITE_OK ){
160648      sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
160649          sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
160650      sqlite3MemoryBarrier();
160651      sqlite3GlobalConfig.isInit = 1;
160652#ifdef SQLITE_EXTRA_INIT
160653      bRunExtraInit = 1;
160654#endif
160655    }
160656    sqlite3GlobalConfig.inProgress = 0;
160657  }
160658  sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
160659
160660  /* Go back under the static mutex and clean up the recursive
160661  ** mutex to prevent a resource leak.
160662  */
160663  sqlite3_mutex_enter(pMaster);
160664  sqlite3GlobalConfig.nRefInitMutex--;
160665  if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
160666    assert( sqlite3GlobalConfig.nRefInitMutex==0 );
160667    sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
160668    sqlite3GlobalConfig.pInitMutex = 0;
160669  }
160670  sqlite3_mutex_leave(pMaster);
160671
160672  /* The following is just a sanity check to make sure SQLite has
160673  ** been compiled correctly.  It is important to run this code, but
160674  ** we don't want to run it too often and soak up CPU cycles for no
160675  ** reason.  So we run it once during initialization.
160676  */
160677#ifndef NDEBUG
160678#ifndef SQLITE_OMIT_FLOATING_POINT
160679  /* This section of code's only "output" is via assert() statements. */
160680  if( rc==SQLITE_OK ){
160681    u64 x = (((u64)1)<<63)-1;
160682    double y;
160683    assert(sizeof(x)==8);
160684    assert(sizeof(x)==sizeof(y));
160685    memcpy(&y, &x, 8);
160686    assert( sqlite3IsNaN(y) );
160687  }
160688#endif
160689#endif
160690
160691  /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
160692  ** compile-time option.
160693  */
160694#ifdef SQLITE_EXTRA_INIT
160695  if( bRunExtraInit ){
160696    int SQLITE_EXTRA_INIT(const char*);
160697    rc = SQLITE_EXTRA_INIT(0);
160698  }
160699#endif
160700
160701  return rc;
160702}
160703
160704/*
160705** Undo the effects of sqlite3_initialize().  Must not be called while
160706** there are outstanding database connections or memory allocations or
160707** while any part of SQLite is otherwise in use in any thread.  This
160708** routine is not threadsafe.  But it is safe to invoke this routine
160709** on when SQLite is already shut down.  If SQLite is already shut down
160710** when this routine is invoked, then this routine is a harmless no-op.
160711*/
160712SQLITE_API int sqlite3_shutdown(void){
160713#ifdef SQLITE_OMIT_WSD
160714  int rc = sqlite3_wsd_init(4096, 24);
160715  if( rc!=SQLITE_OK ){
160716    return rc;
160717  }
160718#endif
160719
160720  if( sqlite3GlobalConfig.isInit ){
160721#ifdef SQLITE_EXTRA_SHUTDOWN
160722    void SQLITE_EXTRA_SHUTDOWN(void);
160723    SQLITE_EXTRA_SHUTDOWN();
160724#endif
160725    sqlite3_os_end();
160726    sqlite3_reset_auto_extension();
160727    sqlite3GlobalConfig.isInit = 0;
160728  }
160729  if( sqlite3GlobalConfig.isPCacheInit ){
160730    sqlite3PcacheShutdown();
160731    sqlite3GlobalConfig.isPCacheInit = 0;
160732  }
160733  if( sqlite3GlobalConfig.isMallocInit ){
160734    sqlite3MallocEnd();
160735    sqlite3GlobalConfig.isMallocInit = 0;
160736
160737#ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
160738    /* The heap subsystem has now been shutdown and these values are supposed
160739    ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
160740    ** which would rely on that heap subsystem; therefore, make sure these
160741    ** values cannot refer to heap memory that was just invalidated when the
160742    ** heap subsystem was shutdown.  This is only done if the current call to
160743    ** this function resulted in the heap subsystem actually being shutdown.
160744    */
160745    sqlite3_data_directory = 0;
160746    sqlite3_temp_directory = 0;
160747#endif
160748  }
160749  if( sqlite3GlobalConfig.isMutexInit ){
160750    sqlite3MutexEnd();
160751    sqlite3GlobalConfig.isMutexInit = 0;
160752  }
160753
160754  return SQLITE_OK;
160755}
160756
160757/*
160758** This API allows applications to modify the global configuration of
160759** the SQLite library at run-time.
160760**
160761** This routine should only be called when there are no outstanding
160762** database connections or memory allocations.  This routine is not
160763** threadsafe.  Failure to heed these warnings can lead to unpredictable
160764** behavior.
160765*/
160766SQLITE_API int sqlite3_config(int op, ...){
160767  va_list ap;
160768  int rc = SQLITE_OK;
160769
160770  /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
160771  ** the SQLite library is in use. */
160772  if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
160773
160774  va_start(ap, op);
160775  switch( op ){
160776
160777    /* Mutex configuration options are only available in a threadsafe
160778    ** compile.
160779    */
160780#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0  /* IMP: R-54466-46756 */
160781    case SQLITE_CONFIG_SINGLETHREAD: {
160782      /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
160783      ** Single-thread. */
160784      sqlite3GlobalConfig.bCoreMutex = 0;  /* Disable mutex on core */
160785      sqlite3GlobalConfig.bFullMutex = 0;  /* Disable mutex on connections */
160786      break;
160787    }
160788#endif
160789#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
160790    case SQLITE_CONFIG_MULTITHREAD: {
160791      /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
160792      ** Multi-thread. */
160793      sqlite3GlobalConfig.bCoreMutex = 1;  /* Enable mutex on core */
160794      sqlite3GlobalConfig.bFullMutex = 0;  /* Disable mutex on connections */
160795      break;
160796    }
160797#endif
160798#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
160799    case SQLITE_CONFIG_SERIALIZED: {
160800      /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
160801      ** Serialized. */
160802      sqlite3GlobalConfig.bCoreMutex = 1;  /* Enable mutex on core */
160803      sqlite3GlobalConfig.bFullMutex = 1;  /* Enable mutex on connections */
160804      break;
160805    }
160806#endif
160807#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
160808    case SQLITE_CONFIG_MUTEX: {
160809      /* Specify an alternative mutex implementation */
160810      sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
160811      break;
160812    }
160813#endif
160814#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
160815    case SQLITE_CONFIG_GETMUTEX: {
160816      /* Retrieve the current mutex implementation */
160817      *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
160818      break;
160819    }
160820#endif
160821
160822    case SQLITE_CONFIG_MALLOC: {
160823      /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
160824      ** single argument which is a pointer to an instance of the
160825      ** sqlite3_mem_methods structure. The argument specifies alternative
160826      ** low-level memory allocation routines to be used in place of the memory
160827      ** allocation routines built into SQLite. */
160828      sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
160829      break;
160830    }
160831    case SQLITE_CONFIG_GETMALLOC: {
160832      /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
160833      ** single argument which is a pointer to an instance of the
160834      ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
160835      ** filled with the currently defined memory allocation routines. */
160836      if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
160837      *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
160838      break;
160839    }
160840    case SQLITE_CONFIG_MEMSTATUS: {
160841      /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
160842      ** single argument of type int, interpreted as a boolean, which enables
160843      ** or disables the collection of memory allocation statistics. */
160844      sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
160845      break;
160846    }
160847    case SQLITE_CONFIG_SMALL_MALLOC: {
160848      sqlite3GlobalConfig.bSmallMalloc = va_arg(ap, int);
160849      break;
160850    }
160851    case SQLITE_CONFIG_PAGECACHE: {
160852      /* EVIDENCE-OF: R-18761-36601 There are three arguments to
160853      ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
160854      ** the size of each page cache line (sz), and the number of cache lines
160855      ** (N). */
160856      sqlite3GlobalConfig.pPage = va_arg(ap, void*);
160857      sqlite3GlobalConfig.szPage = va_arg(ap, int);
160858      sqlite3GlobalConfig.nPage = va_arg(ap, int);
160859      break;
160860    }
160861    case SQLITE_CONFIG_PCACHE_HDRSZ: {
160862      /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
160863      ** a single parameter which is a pointer to an integer and writes into
160864      ** that integer the number of extra bytes per page required for each page
160865      ** in SQLITE_CONFIG_PAGECACHE. */
160866      *va_arg(ap, int*) =
160867          sqlite3HeaderSizeBtree() +
160868          sqlite3HeaderSizePcache() +
160869          sqlite3HeaderSizePcache1();
160870      break;
160871    }
160872
160873    case SQLITE_CONFIG_PCACHE: {
160874      /* no-op */
160875      break;
160876    }
160877    case SQLITE_CONFIG_GETPCACHE: {
160878      /* now an error */
160879      rc = SQLITE_ERROR;
160880      break;
160881    }
160882
160883    case SQLITE_CONFIG_PCACHE2: {
160884      /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
160885      ** single argument which is a pointer to an sqlite3_pcache_methods2
160886      ** object. This object specifies the interface to a custom page cache
160887      ** implementation. */
160888      sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
160889      break;
160890    }
160891    case SQLITE_CONFIG_GETPCACHE2: {
160892      /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
160893      ** single argument which is a pointer to an sqlite3_pcache_methods2
160894      ** object. SQLite copies of the current page cache implementation into
160895      ** that object. */
160896      if( sqlite3GlobalConfig.pcache2.xInit==0 ){
160897        sqlite3PCacheSetDefault();
160898      }
160899      *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
160900      break;
160901    }
160902
160903/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
160904** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
160905** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
160906#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
160907    case SQLITE_CONFIG_HEAP: {
160908      /* EVIDENCE-OF: R-19854-42126 There are three arguments to
160909      ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
160910      ** number of bytes in the memory buffer, and the minimum allocation size.
160911      */
160912      sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
160913      sqlite3GlobalConfig.nHeap = va_arg(ap, int);
160914      sqlite3GlobalConfig.mnReq = va_arg(ap, int);
160915
160916      if( sqlite3GlobalConfig.mnReq<1 ){
160917        sqlite3GlobalConfig.mnReq = 1;
160918      }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
160919        /* cap min request size at 2^12 */
160920        sqlite3GlobalConfig.mnReq = (1<<12);
160921      }
160922
160923      if( sqlite3GlobalConfig.pHeap==0 ){
160924        /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
160925        ** is NULL, then SQLite reverts to using its default memory allocator
160926        ** (the system malloc() implementation), undoing any prior invocation of
160927        ** SQLITE_CONFIG_MALLOC.
160928        **
160929        ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
160930        ** revert to its default implementation when sqlite3_initialize() is run
160931        */
160932        memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
160933      }else{
160934        /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
160935        ** alternative memory allocator is engaged to handle all of SQLites
160936        ** memory allocation needs. */
160937#ifdef SQLITE_ENABLE_MEMSYS3
160938        sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
160939#endif
160940#ifdef SQLITE_ENABLE_MEMSYS5
160941        sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
160942#endif
160943      }
160944      break;
160945    }
160946#endif
160947
160948    case SQLITE_CONFIG_LOOKASIDE: {
160949      sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
160950      sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
160951      break;
160952    }
160953
160954    /* Record a pointer to the logger function and its first argument.
160955    ** The default is NULL.  Logging is disabled if the function pointer is
160956    ** NULL.
160957    */
160958    case SQLITE_CONFIG_LOG: {
160959      /* MSVC is picky about pulling func ptrs from va lists.
160960      ** http://support.microsoft.com/kb/47961
160961      ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
160962      */
160963      typedef void(*LOGFUNC_t)(void*,int,const char*);
160964      sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
160965      sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
160966      break;
160967    }
160968
160969    /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
160970    ** can be changed at start-time using the
160971    ** sqlite3_config(SQLITE_CONFIG_URI,1) or
160972    ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
160973    */
160974    case SQLITE_CONFIG_URI: {
160975      /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
160976      ** argument of type int. If non-zero, then URI handling is globally
160977      ** enabled. If the parameter is zero, then URI handling is globally
160978      ** disabled. */
160979      sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
160980      break;
160981    }
160982
160983    case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
160984      /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
160985      ** option takes a single integer argument which is interpreted as a
160986      ** boolean in order to enable or disable the use of covering indices for
160987      ** full table scans in the query optimizer. */
160988      sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
160989      break;
160990    }
160991
160992#ifdef SQLITE_ENABLE_SQLLOG
160993    case SQLITE_CONFIG_SQLLOG: {
160994      typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
160995      sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
160996      sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
160997      break;
160998    }
160999#endif
161000
161001    case SQLITE_CONFIG_MMAP_SIZE: {
161002      /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
161003      ** integer (sqlite3_int64) values that are the default mmap size limit
161004      ** (the default setting for PRAGMA mmap_size) and the maximum allowed
161005      ** mmap size limit. */
161006      sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
161007      sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
161008      /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
161009      ** negative, then that argument is changed to its compile-time default.
161010      **
161011      ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
161012      ** silently truncated if necessary so that it does not exceed the
161013      ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
161014      ** compile-time option.
161015      */
161016      if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
161017        mxMmap = SQLITE_MAX_MMAP_SIZE;
161018      }
161019      if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
161020      if( szMmap>mxMmap) szMmap = mxMmap;
161021      sqlite3GlobalConfig.mxMmap = mxMmap;
161022      sqlite3GlobalConfig.szMmap = szMmap;
161023      break;
161024    }
161025
161026#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
161027    case SQLITE_CONFIG_WIN32_HEAPSIZE: {
161028      /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
161029      ** unsigned integer value that specifies the maximum size of the created
161030      ** heap. */
161031      sqlite3GlobalConfig.nHeap = va_arg(ap, int);
161032      break;
161033    }
161034#endif
161035
161036    case SQLITE_CONFIG_PMASZ: {
161037      sqlite3GlobalConfig.szPma = va_arg(ap, unsigned int);
161038      break;
161039    }
161040
161041    case SQLITE_CONFIG_STMTJRNL_SPILL: {
161042      sqlite3GlobalConfig.nStmtSpill = va_arg(ap, int);
161043      break;
161044    }
161045
161046#ifdef SQLITE_ENABLE_SORTER_REFERENCES
161047    case SQLITE_CONFIG_SORTERREF_SIZE: {
161048      int iVal = va_arg(ap, int);
161049      if( iVal<0 ){
161050        iVal = SQLITE_DEFAULT_SORTERREF_SIZE;
161051      }
161052      sqlite3GlobalConfig.szSorterRef = (u32)iVal;
161053      break;
161054    }
161055#endif /* SQLITE_ENABLE_SORTER_REFERENCES */
161056
161057#ifdef SQLITE_ENABLE_DESERIALIZE
161058    case SQLITE_CONFIG_MEMDB_MAXSIZE: {
161059      sqlite3GlobalConfig.mxMemdbSize = va_arg(ap, sqlite3_int64);
161060      break;
161061    }
161062#endif /* SQLITE_ENABLE_DESERIALIZE */
161063
161064    default: {
161065      rc = SQLITE_ERROR;
161066      break;
161067    }
161068  }
161069  va_end(ap);
161070  return rc;
161071}
161072
161073/*
161074** Set up the lookaside buffers for a database connection.
161075** Return SQLITE_OK on success.
161076** If lookaside is already active, return SQLITE_BUSY.
161077**
161078** The sz parameter is the number of bytes in each lookaside slot.
161079** The cnt parameter is the number of slots.  If pStart is NULL the
161080** space for the lookaside memory is obtained from sqlite3_malloc().
161081** If pStart is not NULL then it is sz*cnt bytes of memory to use for
161082** the lookaside memory.
161083*/
161084static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
161085#ifndef SQLITE_OMIT_LOOKASIDE
161086  void *pStart;
161087  sqlite3_int64 szAlloc = sz*(sqlite3_int64)cnt;
161088  int nBig;   /* Number of full-size slots */
161089  int nSm;    /* Number smaller LOOKASIDE_SMALL-byte slots */
161090
161091  if( sqlite3LookasideUsed(db,0)>0 ){
161092    return SQLITE_BUSY;
161093  }
161094  /* Free any existing lookaside buffer for this handle before
161095  ** allocating a new one so we don't have to have space for
161096  ** both at the same time.
161097  */
161098  if( db->lookaside.bMalloced ){
161099    sqlite3_free(db->lookaside.pStart);
161100  }
161101  /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
161102  ** than a pointer to be useful.
161103  */
161104  sz = ROUNDDOWN8(sz);  /* IMP: R-33038-09382 */
161105  if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
161106  if( cnt<0 ) cnt = 0;
161107  if( sz==0 || cnt==0 ){
161108    sz = 0;
161109    pStart = 0;
161110  }else if( pBuf==0 ){
161111    sqlite3BeginBenignMalloc();
161112    pStart = sqlite3Malloc( szAlloc );  /* IMP: R-61949-35727 */
161113    sqlite3EndBenignMalloc();
161114    if( pStart ) szAlloc = sqlite3MallocSize(pStart);
161115  }else{
161116    pStart = pBuf;
161117  }
161118#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
161119  if( sz>=LOOKASIDE_SMALL*3 ){
161120    nBig = szAlloc/(3*LOOKASIDE_SMALL+sz);
161121    nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
161122  }else if( sz>=LOOKASIDE_SMALL*2 ){
161123    nBig = szAlloc/(LOOKASIDE_SMALL+sz);
161124    nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
161125  }else
161126#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
161127  if( sz>0 ){
161128    nBig = szAlloc/sz;
161129    nSm = 0;
161130  }else{
161131    nBig = nSm = 0;
161132  }
161133  db->lookaside.pStart = pStart;
161134  db->lookaside.pInit = 0;
161135  db->lookaside.pFree = 0;
161136  db->lookaside.sz = (u16)sz;
161137  db->lookaside.szTrue = (u16)sz;
161138  if( pStart ){
161139    int i;
161140    LookasideSlot *p;
161141    assert( sz > (int)sizeof(LookasideSlot*) );
161142    p = (LookasideSlot*)pStart;
161143    for(i=0; i<nBig; i++){
161144      p->pNext = db->lookaside.pInit;
161145      db->lookaside.pInit = p;
161146      p = (LookasideSlot*)&((u8*)p)[sz];
161147    }
161148#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
161149    db->lookaside.pSmallInit = 0;
161150    db->lookaside.pSmallFree = 0;
161151    db->lookaside.pMiddle = p;
161152    for(i=0; i<nSm; i++){
161153      p->pNext = db->lookaside.pSmallInit;
161154      db->lookaside.pSmallInit = p;
161155      p = (LookasideSlot*)&((u8*)p)[LOOKASIDE_SMALL];
161156    }
161157#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
161158    assert( ((uptr)p)<=szAlloc + (uptr)pStart );
161159    db->lookaside.pEnd = p;
161160    db->lookaside.bDisable = 0;
161161    db->lookaside.bMalloced = pBuf==0 ?1:0;
161162    db->lookaside.nSlot = nBig+nSm;
161163  }else{
161164    db->lookaside.pStart = db;
161165#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
161166    db->lookaside.pSmallInit = 0;
161167    db->lookaside.pSmallFree = 0;
161168    db->lookaside.pMiddle = db;
161169#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
161170    db->lookaside.pEnd = db;
161171    db->lookaside.bDisable = 1;
161172    db->lookaside.sz = 0;
161173    db->lookaside.bMalloced = 0;
161174    db->lookaside.nSlot = 0;
161175  }
161176  assert( sqlite3LookasideUsed(db,0)==0 );
161177#endif /* SQLITE_OMIT_LOOKASIDE */
161178  return SQLITE_OK;
161179}
161180
161181/*
161182** Return the mutex associated with a database connection.
161183*/
161184SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
161185#ifdef SQLITE_ENABLE_API_ARMOR
161186  if( !sqlite3SafetyCheckOk(db) ){
161187    (void)SQLITE_MISUSE_BKPT;
161188    return 0;
161189  }
161190#endif
161191  return db->mutex;
161192}
161193
161194/*
161195** Free up as much memory as we can from the given database
161196** connection.
161197*/
161198SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
161199  int i;
161200
161201#ifdef SQLITE_ENABLE_API_ARMOR
161202  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
161203#endif
161204  sqlite3_mutex_enter(db->mutex);
161205  sqlite3BtreeEnterAll(db);
161206  for(i=0; i<db->nDb; i++){
161207    Btree *pBt = db->aDb[i].pBt;
161208    if( pBt ){
161209      Pager *pPager = sqlite3BtreePager(pBt);
161210      sqlite3PagerShrink(pPager);
161211    }
161212  }
161213  sqlite3BtreeLeaveAll(db);
161214  sqlite3_mutex_leave(db->mutex);
161215  return SQLITE_OK;
161216}
161217
161218/*
161219** Flush any dirty pages in the pager-cache for any attached database
161220** to disk.
161221*/
161222SQLITE_API int sqlite3_db_cacheflush(sqlite3 *db){
161223  int i;
161224  int rc = SQLITE_OK;
161225  int bSeenBusy = 0;
161226
161227#ifdef SQLITE_ENABLE_API_ARMOR
161228  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
161229#endif
161230  sqlite3_mutex_enter(db->mutex);
161231  sqlite3BtreeEnterAll(db);
161232  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
161233    Btree *pBt = db->aDb[i].pBt;
161234    if( pBt && sqlite3BtreeIsInTrans(pBt) ){
161235      Pager *pPager = sqlite3BtreePager(pBt);
161236      rc = sqlite3PagerFlush(pPager);
161237      if( rc==SQLITE_BUSY ){
161238        bSeenBusy = 1;
161239        rc = SQLITE_OK;
161240      }
161241    }
161242  }
161243  sqlite3BtreeLeaveAll(db);
161244  sqlite3_mutex_leave(db->mutex);
161245  return ((rc==SQLITE_OK && bSeenBusy) ? SQLITE_BUSY : rc);
161246}
161247
161248/*
161249** Configuration settings for an individual database connection
161250*/
161251SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
161252  va_list ap;
161253  int rc;
161254  va_start(ap, op);
161255  switch( op ){
161256    case SQLITE_DBCONFIG_MAINDBNAME: {
161257      /* IMP: R-06824-28531 */
161258      /* IMP: R-36257-52125 */
161259      db->aDb[0].zDbSName = va_arg(ap,char*);
161260      rc = SQLITE_OK;
161261      break;
161262    }
161263    case SQLITE_DBCONFIG_LOOKASIDE: {
161264      void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
161265      int sz = va_arg(ap, int);       /* IMP: R-47871-25994 */
161266      int cnt = va_arg(ap, int);      /* IMP: R-04460-53386 */
161267      rc = setupLookaside(db, pBuf, sz, cnt);
161268      break;
161269    }
161270    default: {
161271      static const struct {
161272        int op;      /* The opcode */
161273        u32 mask;    /* Mask of the bit in sqlite3.flags to set/clear */
161274      } aFlagOp[] = {
161275        { SQLITE_DBCONFIG_ENABLE_FKEY,           SQLITE_ForeignKeys    },
161276        { SQLITE_DBCONFIG_ENABLE_TRIGGER,        SQLITE_EnableTrigger  },
161277        { SQLITE_DBCONFIG_ENABLE_VIEW,           SQLITE_EnableView     },
161278        { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer  },
161279        { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension  },
161280        { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE,      SQLITE_NoCkptOnClose  },
161281        { SQLITE_DBCONFIG_ENABLE_QPSG,           SQLITE_EnableQPSG     },
161282        { SQLITE_DBCONFIG_TRIGGER_EQP,           SQLITE_TriggerEQP     },
161283        { SQLITE_DBCONFIG_RESET_DATABASE,        SQLITE_ResetDatabase  },
161284        { SQLITE_DBCONFIG_DEFENSIVE,             SQLITE_Defensive      },
161285        { SQLITE_DBCONFIG_WRITABLE_SCHEMA,       SQLITE_WriteSchema|
161286                                                 SQLITE_NoSchemaError  },
161287        { SQLITE_DBCONFIG_LEGACY_ALTER_TABLE,    SQLITE_LegacyAlter    },
161288        { SQLITE_DBCONFIG_DQS_DDL,               SQLITE_DqsDDL         },
161289        { SQLITE_DBCONFIG_DQS_DML,               SQLITE_DqsDML         },
161290        { SQLITE_DBCONFIG_LEGACY_FILE_FORMAT,    SQLITE_LegacyFileFmt  },
161291        { SQLITE_DBCONFIG_TRUSTED_SCHEMA,        SQLITE_TrustedSchema  },
161292      };
161293      unsigned int i;
161294      rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
161295      for(i=0; i<ArraySize(aFlagOp); i++){
161296        if( aFlagOp[i].op==op ){
161297          int onoff = va_arg(ap, int);
161298          int *pRes = va_arg(ap, int*);
161299          u64 oldFlags = db->flags;
161300          if( onoff>0 ){
161301            db->flags |= aFlagOp[i].mask;
161302          }else if( onoff==0 ){
161303            db->flags &= ~(u64)aFlagOp[i].mask;
161304          }
161305          if( oldFlags!=db->flags ){
161306            sqlite3ExpirePreparedStatements(db, 0);
161307          }
161308          if( pRes ){
161309            *pRes = (db->flags & aFlagOp[i].mask)!=0;
161310          }
161311          rc = SQLITE_OK;
161312          break;
161313        }
161314      }
161315      break;
161316    }
161317  }
161318  va_end(ap);
161319  return rc;
161320}
161321
161322/*
161323** This is the default collating function named "BINARY" which is always
161324** available.
161325*/
161326static int binCollFunc(
161327  void *NotUsed,
161328  int nKey1, const void *pKey1,
161329  int nKey2, const void *pKey2
161330){
161331  int rc, n;
161332  UNUSED_PARAMETER(NotUsed);
161333  n = nKey1<nKey2 ? nKey1 : nKey2;
161334  /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
161335  ** strings byte by byte using the memcmp() function from the standard C
161336  ** library. */
161337  assert( pKey1 && pKey2 );
161338  rc = memcmp(pKey1, pKey2, n);
161339  if( rc==0 ){
161340    rc = nKey1 - nKey2;
161341  }
161342  return rc;
161343}
161344
161345/*
161346** This is the collating function named "RTRIM" which is always
161347** available.  Ignore trailing spaces.
161348*/
161349static int rtrimCollFunc(
161350  void *pUser,
161351  int nKey1, const void *pKey1,
161352  int nKey2, const void *pKey2
161353){
161354  const u8 *pK1 = (const u8*)pKey1;
161355  const u8 *pK2 = (const u8*)pKey2;
161356  while( nKey1 && pK1[nKey1-1]==' ' ) nKey1--;
161357  while( nKey2 && pK2[nKey2-1]==' ' ) nKey2--;
161358  return binCollFunc(pUser, nKey1, pKey1, nKey2, pKey2);
161359}
161360
161361/*
161362** Return true if CollSeq is the default built-in BINARY.
161363*/
161364SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq *p){
161365  assert( p==0 || p->xCmp!=binCollFunc || strcmp(p->zName,"BINARY")==0 );
161366  return p==0 || p->xCmp==binCollFunc;
161367}
161368
161369/*
161370** Another built-in collating sequence: NOCASE.
161371**
161372** This collating sequence is intended to be used for "case independent
161373** comparison". SQLite's knowledge of upper and lower case equivalents
161374** extends only to the 26 characters used in the English language.
161375**
161376** At the moment there is only a UTF-8 implementation.
161377*/
161378static int nocaseCollatingFunc(
161379  void *NotUsed,
161380  int nKey1, const void *pKey1,
161381  int nKey2, const void *pKey2
161382){
161383  int r = sqlite3StrNICmp(
161384      (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
161385  UNUSED_PARAMETER(NotUsed);
161386  if( 0==r ){
161387    r = nKey1-nKey2;
161388  }
161389  return r;
161390}
161391
161392/*
161393** Return the ROWID of the most recent insert
161394*/
161395SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
161396#ifdef SQLITE_ENABLE_API_ARMOR
161397  if( !sqlite3SafetyCheckOk(db) ){
161398    (void)SQLITE_MISUSE_BKPT;
161399    return 0;
161400  }
161401#endif
161402  return db->lastRowid;
161403}
161404
161405/*
161406** Set the value returned by the sqlite3_last_insert_rowid() API function.
161407*/
161408SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){
161409#ifdef SQLITE_ENABLE_API_ARMOR
161410  if( !sqlite3SafetyCheckOk(db) ){
161411    (void)SQLITE_MISUSE_BKPT;
161412    return;
161413  }
161414#endif
161415  sqlite3_mutex_enter(db->mutex);
161416  db->lastRowid = iRowid;
161417  sqlite3_mutex_leave(db->mutex);
161418}
161419
161420/*
161421** Return the number of changes in the most recent call to sqlite3_exec().
161422*/
161423SQLITE_API int sqlite3_changes(sqlite3 *db){
161424#ifdef SQLITE_ENABLE_API_ARMOR
161425  if( !sqlite3SafetyCheckOk(db) ){
161426    (void)SQLITE_MISUSE_BKPT;
161427    return 0;
161428  }
161429#endif
161430  return db->nChange;
161431}
161432
161433/*
161434** Return the number of changes since the database handle was opened.
161435*/
161436SQLITE_API int sqlite3_total_changes(sqlite3 *db){
161437#ifdef SQLITE_ENABLE_API_ARMOR
161438  if( !sqlite3SafetyCheckOk(db) ){
161439    (void)SQLITE_MISUSE_BKPT;
161440    return 0;
161441  }
161442#endif
161443  return db->nTotalChange;
161444}
161445
161446/*
161447** Close all open savepoints. This function only manipulates fields of the
161448** database handle object, it does not close any savepoints that may be open
161449** at the b-tree/pager level.
161450*/
161451SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
161452  while( db->pSavepoint ){
161453    Savepoint *pTmp = db->pSavepoint;
161454    db->pSavepoint = pTmp->pNext;
161455    sqlite3DbFree(db, pTmp);
161456  }
161457  db->nSavepoint = 0;
161458  db->nStatement = 0;
161459  db->isTransactionSavepoint = 0;
161460}
161461
161462/*
161463** Invoke the destructor function associated with FuncDef p, if any. Except,
161464** if this is not the last copy of the function, do not invoke it. Multiple
161465** copies of a single function are created when create_function() is called
161466** with SQLITE_ANY as the encoding.
161467*/
161468static void functionDestroy(sqlite3 *db, FuncDef *p){
161469  FuncDestructor *pDestructor = p->u.pDestructor;
161470  if( pDestructor ){
161471    pDestructor->nRef--;
161472    if( pDestructor->nRef==0 ){
161473      pDestructor->xDestroy(pDestructor->pUserData);
161474      sqlite3DbFree(db, pDestructor);
161475    }
161476  }
161477}
161478
161479/*
161480** Disconnect all sqlite3_vtab objects that belong to database connection
161481** db. This is called when db is being closed.
161482*/
161483static void disconnectAllVtab(sqlite3 *db){
161484#ifndef SQLITE_OMIT_VIRTUALTABLE
161485  int i;
161486  HashElem *p;
161487  sqlite3BtreeEnterAll(db);
161488  for(i=0; i<db->nDb; i++){
161489    Schema *pSchema = db->aDb[i].pSchema;
161490    if( pSchema ){
161491      for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
161492        Table *pTab = (Table *)sqliteHashData(p);
161493        if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab);
161494      }
161495    }
161496  }
161497  for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
161498    Module *pMod = (Module *)sqliteHashData(p);
161499    if( pMod->pEpoTab ){
161500      sqlite3VtabDisconnect(db, pMod->pEpoTab);
161501    }
161502  }
161503  sqlite3VtabUnlockList(db);
161504  sqlite3BtreeLeaveAll(db);
161505#else
161506  UNUSED_PARAMETER(db);
161507#endif
161508}
161509
161510/*
161511** Return TRUE if database connection db has unfinalized prepared
161512** statements or unfinished sqlite3_backup objects.
161513*/
161514static int connectionIsBusy(sqlite3 *db){
161515  int j;
161516  assert( sqlite3_mutex_held(db->mutex) );
161517  if( db->pVdbe ) return 1;
161518  for(j=0; j<db->nDb; j++){
161519    Btree *pBt = db->aDb[j].pBt;
161520    if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
161521  }
161522  return 0;
161523}
161524
161525/*
161526** Close an existing SQLite database
161527*/
161528static int sqlite3Close(sqlite3 *db, int forceZombie){
161529  if( !db ){
161530    /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
161531    ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
161532    return SQLITE_OK;
161533  }
161534  if( !sqlite3SafetyCheckSickOrOk(db) ){
161535    return SQLITE_MISUSE_BKPT;
161536  }
161537  sqlite3_mutex_enter(db->mutex);
161538  if( db->mTrace & SQLITE_TRACE_CLOSE ){
161539    db->xTrace(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
161540  }
161541
161542  /* Force xDisconnect calls on all virtual tables */
161543  disconnectAllVtab(db);
161544
161545  /* If a transaction is open, the disconnectAllVtab() call above
161546  ** will not have called the xDisconnect() method on any virtual
161547  ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
161548  ** call will do so. We need to do this before the check for active
161549  ** SQL statements below, as the v-table implementation may be storing
161550  ** some prepared statements internally.
161551  */
161552  sqlite3VtabRollback(db);
161553
161554  /* Legacy behavior (sqlite3_close() behavior) is to return
161555  ** SQLITE_BUSY if the connection can not be closed immediately.
161556  */
161557  if( !forceZombie && connectionIsBusy(db) ){
161558    sqlite3ErrorWithMsg(db, SQLITE_BUSY, "unable to close due to unfinalized "
161559       "statements or unfinished backups");
161560    sqlite3_mutex_leave(db->mutex);
161561    return SQLITE_BUSY;
161562  }
161563
161564#ifdef SQLITE_ENABLE_SQLLOG
161565  if( sqlite3GlobalConfig.xSqllog ){
161566    /* Closing the handle. Fourth parameter is passed the value 2. */
161567    sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
161568  }
161569#endif
161570
161571  /* Convert the connection into a zombie and then close it.
161572  */
161573  db->magic = SQLITE_MAGIC_ZOMBIE;
161574  sqlite3LeaveMutexAndCloseZombie(db);
161575  return SQLITE_OK;
161576}
161577
161578/*
161579** Two variations on the public interface for closing a database
161580** connection. The sqlite3_close() version returns SQLITE_BUSY and
161581** leaves the connection option if there are unfinalized prepared
161582** statements or unfinished sqlite3_backups.  The sqlite3_close_v2()
161583** version forces the connection to become a zombie if there are
161584** unclosed resources, and arranges for deallocation when the last
161585** prepare statement or sqlite3_backup closes.
161586*/
161587SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
161588SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
161589
161590
161591/*
161592** Close the mutex on database connection db.
161593**
161594** Furthermore, if database connection db is a zombie (meaning that there
161595** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
161596** every sqlite3_stmt has now been finalized and every sqlite3_backup has
161597** finished, then free all resources.
161598*/
161599SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
161600  HashElem *i;                    /* Hash table iterator */
161601  int j;
161602
161603  /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
161604  ** or if the connection has not yet been closed by sqlite3_close_v2(),
161605  ** then just leave the mutex and return.
161606  */
161607  if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
161608    sqlite3_mutex_leave(db->mutex);
161609    return;
161610  }
161611
161612  /* If we reach this point, it means that the database connection has
161613  ** closed all sqlite3_stmt and sqlite3_backup objects and has been
161614  ** passed to sqlite3_close (meaning that it is a zombie).  Therefore,
161615  ** go ahead and free all resources.
161616  */
161617
161618  /* If a transaction is open, roll it back. This also ensures that if
161619  ** any database schemas have been modified by an uncommitted transaction
161620  ** they are reset. And that the required b-tree mutex is held to make
161621  ** the pager rollback and schema reset an atomic operation. */
161622  sqlite3RollbackAll(db, SQLITE_OK);
161623
161624  /* Free any outstanding Savepoint structures. */
161625  sqlite3CloseSavepoints(db);
161626
161627  /* Close all database connections */
161628  for(j=0; j<db->nDb; j++){
161629    struct Db *pDb = &db->aDb[j];
161630    if( pDb->pBt ){
161631      sqlite3BtreeClose(pDb->pBt);
161632      pDb->pBt = 0;
161633      if( j!=1 ){
161634        pDb->pSchema = 0;
161635      }
161636    }
161637  }
161638  /* Clear the TEMP schema separately and last */
161639  if( db->aDb[1].pSchema ){
161640    sqlite3SchemaClear(db->aDb[1].pSchema);
161641  }
161642  sqlite3VtabUnlockList(db);
161643
161644  /* Free up the array of auxiliary databases */
161645  sqlite3CollapseDatabaseArray(db);
161646  assert( db->nDb<=2 );
161647  assert( db->aDb==db->aDbStatic );
161648
161649  /* Tell the code in notify.c that the connection no longer holds any
161650  ** locks and does not require any further unlock-notify callbacks.
161651  */
161652  sqlite3ConnectionClosed(db);
161653
161654  for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
161655    FuncDef *pNext, *p;
161656    p = sqliteHashData(i);
161657    do{
161658      functionDestroy(db, p);
161659      pNext = p->pNext;
161660      sqlite3DbFree(db, p);
161661      p = pNext;
161662    }while( p );
161663  }
161664  sqlite3HashClear(&db->aFunc);
161665  for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
161666    CollSeq *pColl = (CollSeq *)sqliteHashData(i);
161667    /* Invoke any destructors registered for collation sequence user data. */
161668    for(j=0; j<3; j++){
161669      if( pColl[j].xDel ){
161670        pColl[j].xDel(pColl[j].pUser);
161671      }
161672    }
161673    sqlite3DbFree(db, pColl);
161674  }
161675  sqlite3HashClear(&db->aCollSeq);
161676#ifndef SQLITE_OMIT_VIRTUALTABLE
161677  for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
161678    Module *pMod = (Module *)sqliteHashData(i);
161679    sqlite3VtabEponymousTableClear(db, pMod);
161680    sqlite3VtabModuleUnref(db, pMod);
161681  }
161682  sqlite3HashClear(&db->aModule);
161683#endif
161684
161685  sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
161686  sqlite3ValueFree(db->pErr);
161687  sqlite3CloseExtensions(db);
161688#if SQLITE_USER_AUTHENTICATION
161689  sqlite3_free(db->auth.zAuthUser);
161690  sqlite3_free(db->auth.zAuthPW);
161691#endif
161692
161693  db->magic = SQLITE_MAGIC_ERROR;
161694
161695  /* The temp-database schema is allocated differently from the other schema
161696  ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
161697  ** So it needs to be freed here. Todo: Why not roll the temp schema into
161698  ** the same sqliteMalloc() as the one that allocates the database
161699  ** structure?
161700  */
161701  sqlite3DbFree(db, db->aDb[1].pSchema);
161702  sqlite3_mutex_leave(db->mutex);
161703  db->magic = SQLITE_MAGIC_CLOSED;
161704  sqlite3_mutex_free(db->mutex);
161705  assert( sqlite3LookasideUsed(db,0)==0 );
161706  if( db->lookaside.bMalloced ){
161707    sqlite3_free(db->lookaside.pStart);
161708  }
161709  sqlite3_free(db);
161710}
161711
161712/*
161713** Rollback all database files.  If tripCode is not SQLITE_OK, then
161714** any write cursors are invalidated ("tripped" - as in "tripping a circuit
161715** breaker") and made to return tripCode if there are any further
161716** attempts to use that cursor.  Read cursors remain open and valid
161717** but are "saved" in case the table pages are moved around.
161718*/
161719SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
161720  int i;
161721  int inTrans = 0;
161722  int schemaChange;
161723  assert( sqlite3_mutex_held(db->mutex) );
161724  sqlite3BeginBenignMalloc();
161725
161726  /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
161727  ** This is important in case the transaction being rolled back has
161728  ** modified the database schema. If the b-tree mutexes are not taken
161729  ** here, then another shared-cache connection might sneak in between
161730  ** the database rollback and schema reset, which can cause false
161731  ** corruption reports in some cases.  */
161732  sqlite3BtreeEnterAll(db);
161733  schemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0 && db->init.busy==0;
161734
161735  for(i=0; i<db->nDb; i++){
161736    Btree *p = db->aDb[i].pBt;
161737    if( p ){
161738      if( sqlite3BtreeIsInTrans(p) ){
161739        inTrans = 1;
161740      }
161741      sqlite3BtreeRollback(p, tripCode, !schemaChange);
161742    }
161743  }
161744  sqlite3VtabRollback(db);
161745  sqlite3EndBenignMalloc();
161746
161747  if( schemaChange ){
161748    sqlite3ExpirePreparedStatements(db, 0);
161749    sqlite3ResetAllSchemasOfConnection(db);
161750  }
161751  sqlite3BtreeLeaveAll(db);
161752
161753  /* Any deferred constraint violations have now been resolved. */
161754  db->nDeferredCons = 0;
161755  db->nDeferredImmCons = 0;
161756  db->flags &= ~(u64)SQLITE_DeferFKs;
161757
161758  /* If one has been configured, invoke the rollback-hook callback */
161759  if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
161760    db->xRollbackCallback(db->pRollbackArg);
161761  }
161762}
161763
161764/*
161765** Return a static string containing the name corresponding to the error code
161766** specified in the argument.
161767*/
161768#if defined(SQLITE_NEED_ERR_NAME)
161769SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
161770  const char *zName = 0;
161771  int i, origRc = rc;
161772  for(i=0; i<2 && zName==0; i++, rc &= 0xff){
161773    switch( rc ){
161774      case SQLITE_OK:                 zName = "SQLITE_OK";                break;
161775      case SQLITE_ERROR:              zName = "SQLITE_ERROR";             break;
161776      case SQLITE_ERROR_SNAPSHOT:     zName = "SQLITE_ERROR_SNAPSHOT";    break;
161777      case SQLITE_INTERNAL:           zName = "SQLITE_INTERNAL";          break;
161778      case SQLITE_PERM:               zName = "SQLITE_PERM";              break;
161779      case SQLITE_ABORT:              zName = "SQLITE_ABORT";             break;
161780      case SQLITE_ABORT_ROLLBACK:     zName = "SQLITE_ABORT_ROLLBACK";    break;
161781      case SQLITE_BUSY:               zName = "SQLITE_BUSY";              break;
161782      case SQLITE_BUSY_RECOVERY:      zName = "SQLITE_BUSY_RECOVERY";     break;
161783      case SQLITE_BUSY_SNAPSHOT:      zName = "SQLITE_BUSY_SNAPSHOT";     break;
161784      case SQLITE_LOCKED:             zName = "SQLITE_LOCKED";            break;
161785      case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
161786      case SQLITE_NOMEM:              zName = "SQLITE_NOMEM";             break;
161787      case SQLITE_READONLY:           zName = "SQLITE_READONLY";          break;
161788      case SQLITE_READONLY_RECOVERY:  zName = "SQLITE_READONLY_RECOVERY"; break;
161789      case SQLITE_READONLY_CANTINIT:  zName = "SQLITE_READONLY_CANTINIT"; break;
161790      case SQLITE_READONLY_ROLLBACK:  zName = "SQLITE_READONLY_ROLLBACK"; break;
161791      case SQLITE_READONLY_DBMOVED:   zName = "SQLITE_READONLY_DBMOVED";  break;
161792      case SQLITE_READONLY_DIRECTORY: zName = "SQLITE_READONLY_DIRECTORY";break;
161793      case SQLITE_INTERRUPT:          zName = "SQLITE_INTERRUPT";         break;
161794      case SQLITE_IOERR:              zName = "SQLITE_IOERR";             break;
161795      case SQLITE_IOERR_READ:         zName = "SQLITE_IOERR_READ";        break;
161796      case SQLITE_IOERR_SHORT_READ:   zName = "SQLITE_IOERR_SHORT_READ";  break;
161797      case SQLITE_IOERR_WRITE:        zName = "SQLITE_IOERR_WRITE";       break;
161798      case SQLITE_IOERR_FSYNC:        zName = "SQLITE_IOERR_FSYNC";       break;
161799      case SQLITE_IOERR_DIR_FSYNC:    zName = "SQLITE_IOERR_DIR_FSYNC";   break;
161800      case SQLITE_IOERR_TRUNCATE:     zName = "SQLITE_IOERR_TRUNCATE";    break;
161801      case SQLITE_IOERR_FSTAT:        zName = "SQLITE_IOERR_FSTAT";       break;
161802      case SQLITE_IOERR_UNLOCK:       zName = "SQLITE_IOERR_UNLOCK";      break;
161803      case SQLITE_IOERR_RDLOCK:       zName = "SQLITE_IOERR_RDLOCK";      break;
161804      case SQLITE_IOERR_DELETE:       zName = "SQLITE_IOERR_DELETE";      break;
161805      case SQLITE_IOERR_NOMEM:        zName = "SQLITE_IOERR_NOMEM";       break;
161806      case SQLITE_IOERR_ACCESS:       zName = "SQLITE_IOERR_ACCESS";      break;
161807      case SQLITE_IOERR_CHECKRESERVEDLOCK:
161808                                zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
161809      case SQLITE_IOERR_LOCK:         zName = "SQLITE_IOERR_LOCK";        break;
161810      case SQLITE_IOERR_CLOSE:        zName = "SQLITE_IOERR_CLOSE";       break;
161811      case SQLITE_IOERR_DIR_CLOSE:    zName = "SQLITE_IOERR_DIR_CLOSE";   break;
161812      case SQLITE_IOERR_SHMOPEN:      zName = "SQLITE_IOERR_SHMOPEN";     break;
161813      case SQLITE_IOERR_SHMSIZE:      zName = "SQLITE_IOERR_SHMSIZE";     break;
161814      case SQLITE_IOERR_SHMLOCK:      zName = "SQLITE_IOERR_SHMLOCK";     break;
161815      case SQLITE_IOERR_SHMMAP:       zName = "SQLITE_IOERR_SHMMAP";      break;
161816      case SQLITE_IOERR_SEEK:         zName = "SQLITE_IOERR_SEEK";        break;
161817      case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
161818      case SQLITE_IOERR_MMAP:         zName = "SQLITE_IOERR_MMAP";        break;
161819      case SQLITE_IOERR_GETTEMPPATH:  zName = "SQLITE_IOERR_GETTEMPPATH"; break;
161820      case SQLITE_IOERR_CONVPATH:     zName = "SQLITE_IOERR_CONVPATH";    break;
161821      case SQLITE_CORRUPT:            zName = "SQLITE_CORRUPT";           break;
161822      case SQLITE_CORRUPT_VTAB:       zName = "SQLITE_CORRUPT_VTAB";      break;
161823      case SQLITE_NOTFOUND:           zName = "SQLITE_NOTFOUND";          break;
161824      case SQLITE_FULL:               zName = "SQLITE_FULL";              break;
161825      case SQLITE_CANTOPEN:           zName = "SQLITE_CANTOPEN";          break;
161826      case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
161827      case SQLITE_CANTOPEN_ISDIR:     zName = "SQLITE_CANTOPEN_ISDIR";    break;
161828      case SQLITE_CANTOPEN_FULLPATH:  zName = "SQLITE_CANTOPEN_FULLPATH"; break;
161829      case SQLITE_CANTOPEN_CONVPATH:  zName = "SQLITE_CANTOPEN_CONVPATH"; break;
161830      case SQLITE_CANTOPEN_SYMLINK:   zName = "SQLITE_CANTOPEN_SYMLINK";  break;
161831      case SQLITE_PROTOCOL:           zName = "SQLITE_PROTOCOL";          break;
161832      case SQLITE_EMPTY:              zName = "SQLITE_EMPTY";             break;
161833      case SQLITE_SCHEMA:             zName = "SQLITE_SCHEMA";            break;
161834      case SQLITE_TOOBIG:             zName = "SQLITE_TOOBIG";            break;
161835      case SQLITE_CONSTRAINT:         zName = "SQLITE_CONSTRAINT";        break;
161836      case SQLITE_CONSTRAINT_UNIQUE:  zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
161837      case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break;
161838      case SQLITE_CONSTRAINT_FOREIGNKEY:
161839                                zName = "SQLITE_CONSTRAINT_FOREIGNKEY";   break;
161840      case SQLITE_CONSTRAINT_CHECK:   zName = "SQLITE_CONSTRAINT_CHECK";  break;
161841      case SQLITE_CONSTRAINT_PRIMARYKEY:
161842                                zName = "SQLITE_CONSTRAINT_PRIMARYKEY";   break;
161843      case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break;
161844      case SQLITE_CONSTRAINT_COMMITHOOK:
161845                                zName = "SQLITE_CONSTRAINT_COMMITHOOK";   break;
161846      case SQLITE_CONSTRAINT_VTAB:    zName = "SQLITE_CONSTRAINT_VTAB";   break;
161847      case SQLITE_CONSTRAINT_FUNCTION:
161848                                zName = "SQLITE_CONSTRAINT_FUNCTION";     break;
161849      case SQLITE_CONSTRAINT_ROWID:   zName = "SQLITE_CONSTRAINT_ROWID";  break;
161850      case SQLITE_MISMATCH:           zName = "SQLITE_MISMATCH";          break;
161851      case SQLITE_MISUSE:             zName = "SQLITE_MISUSE";            break;
161852      case SQLITE_NOLFS:              zName = "SQLITE_NOLFS";             break;
161853      case SQLITE_AUTH:               zName = "SQLITE_AUTH";              break;
161854      case SQLITE_FORMAT:             zName = "SQLITE_FORMAT";            break;
161855      case SQLITE_RANGE:              zName = "SQLITE_RANGE";             break;
161856      case SQLITE_NOTADB:             zName = "SQLITE_NOTADB";            break;
161857      case SQLITE_ROW:                zName = "SQLITE_ROW";               break;
161858      case SQLITE_NOTICE:             zName = "SQLITE_NOTICE";            break;
161859      case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break;
161860      case SQLITE_NOTICE_RECOVER_ROLLBACK:
161861                                zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
161862      case SQLITE_WARNING:            zName = "SQLITE_WARNING";           break;
161863      case SQLITE_WARNING_AUTOINDEX:  zName = "SQLITE_WARNING_AUTOINDEX"; break;
161864      case SQLITE_DONE:               zName = "SQLITE_DONE";              break;
161865    }
161866  }
161867  if( zName==0 ){
161868    static char zBuf[50];
161869    sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
161870    zName = zBuf;
161871  }
161872  return zName;
161873}
161874#endif
161875
161876/*
161877** Return a static string that describes the kind of error specified in the
161878** argument.
161879*/
161880SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
161881  static const char* const aMsg[] = {
161882    /* SQLITE_OK          */ "not an error",
161883    /* SQLITE_ERROR       */ "SQL logic error",
161884    /* SQLITE_INTERNAL    */ 0,
161885    /* SQLITE_PERM        */ "access permission denied",
161886    /* SQLITE_ABORT       */ "query aborted",
161887    /* SQLITE_BUSY        */ "database is locked",
161888    /* SQLITE_LOCKED      */ "database table is locked",
161889    /* SQLITE_NOMEM       */ "out of memory",
161890    /* SQLITE_READONLY    */ "attempt to write a readonly database",
161891    /* SQLITE_INTERRUPT   */ "interrupted",
161892    /* SQLITE_IOERR       */ "disk I/O error",
161893    /* SQLITE_CORRUPT     */ "database disk image is malformed",
161894    /* SQLITE_NOTFOUND    */ "unknown operation",
161895    /* SQLITE_FULL        */ "database or disk is full",
161896    /* SQLITE_CANTOPEN    */ "unable to open database file",
161897    /* SQLITE_PROTOCOL    */ "locking protocol",
161898    /* SQLITE_EMPTY       */ 0,
161899    /* SQLITE_SCHEMA      */ "database schema has changed",
161900    /* SQLITE_TOOBIG      */ "string or blob too big",
161901    /* SQLITE_CONSTRAINT  */ "constraint failed",
161902    /* SQLITE_MISMATCH    */ "datatype mismatch",
161903    /* SQLITE_MISUSE      */ "bad parameter or other API misuse",
161904#ifdef SQLITE_DISABLE_LFS
161905    /* SQLITE_NOLFS       */ "large file support is disabled",
161906#else
161907    /* SQLITE_NOLFS       */ 0,
161908#endif
161909    /* SQLITE_AUTH        */ "authorization denied",
161910    /* SQLITE_FORMAT      */ 0,
161911    /* SQLITE_RANGE       */ "column index out of range",
161912    /* SQLITE_NOTADB      */ "file is not a database",
161913    /* SQLITE_NOTICE      */ "notification message",
161914    /* SQLITE_WARNING     */ "warning message",
161915  };
161916  const char *zErr = "unknown error";
161917  switch( rc ){
161918    case SQLITE_ABORT_ROLLBACK: {
161919      zErr = "abort due to ROLLBACK";
161920      break;
161921    }
161922    case SQLITE_ROW: {
161923      zErr = "another row available";
161924      break;
161925    }
161926    case SQLITE_DONE: {
161927      zErr = "no more rows available";
161928      break;
161929    }
161930    default: {
161931      rc &= 0xff;
161932      if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
161933        zErr = aMsg[rc];
161934      }
161935      break;
161936    }
161937  }
161938  return zErr;
161939}
161940
161941/*
161942** This routine implements a busy callback that sleeps and tries
161943** again until a timeout value is reached.  The timeout value is
161944** an integer number of milliseconds passed in as the first
161945** argument.
161946**
161947** Return non-zero to retry the lock.  Return zero to stop trying
161948** and cause SQLite to return SQLITE_BUSY.
161949*/
161950static int sqliteDefaultBusyCallback(
161951  void *ptr,               /* Database connection */
161952  int count                /* Number of times table has been busy */
161953){
161954#if SQLITE_OS_WIN || HAVE_USLEEP
161955  /* This case is for systems that have support for sleeping for fractions of
161956  ** a second.  Examples:  All windows systems, unix systems with usleep() */
161957  static const u8 delays[] =
161958     { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
161959  static const u8 totals[] =
161960     { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
161961# define NDELAY ArraySize(delays)
161962  sqlite3 *db = (sqlite3 *)ptr;
161963  int tmout = db->busyTimeout;
161964  int delay, prior;
161965
161966  assert( count>=0 );
161967  if( count < NDELAY ){
161968    delay = delays[count];
161969    prior = totals[count];
161970  }else{
161971    delay = delays[NDELAY-1];
161972    prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
161973  }
161974  if( prior + delay > tmout ){
161975    delay = tmout - prior;
161976    if( delay<=0 ) return 0;
161977  }
161978  sqlite3OsSleep(db->pVfs, delay*1000);
161979  return 1;
161980#else
161981  /* This case for unix systems that lack usleep() support.  Sleeping
161982  ** must be done in increments of whole seconds */
161983  sqlite3 *db = (sqlite3 *)ptr;
161984  int tmout = ((sqlite3 *)ptr)->busyTimeout;
161985  if( (count+1)*1000 > tmout ){
161986    return 0;
161987  }
161988  sqlite3OsSleep(db->pVfs, 1000000);
161989  return 1;
161990#endif
161991}
161992
161993/*
161994** Invoke the given busy handler.
161995**
161996** This routine is called when an operation failed to acquire a
161997** lock on VFS file pFile.
161998**
161999** If this routine returns non-zero, the lock is retried.  If it
162000** returns 0, the operation aborts with an SQLITE_BUSY error.
162001*/
162002SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
162003  int rc;
162004  if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
162005  rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
162006  if( rc==0 ){
162007    p->nBusy = -1;
162008  }else{
162009    p->nBusy++;
162010  }
162011  return rc;
162012}
162013
162014/*
162015** This routine sets the busy callback for an Sqlite database to the
162016** given callback function with the given argument.
162017*/
162018SQLITE_API int sqlite3_busy_handler(
162019  sqlite3 *db,
162020  int (*xBusy)(void*,int),
162021  void *pArg
162022){
162023#ifdef SQLITE_ENABLE_API_ARMOR
162024  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
162025#endif
162026  sqlite3_mutex_enter(db->mutex);
162027  db->busyHandler.xBusyHandler = xBusy;
162028  db->busyHandler.pBusyArg = pArg;
162029  db->busyHandler.nBusy = 0;
162030  db->busyTimeout = 0;
162031  sqlite3_mutex_leave(db->mutex);
162032  return SQLITE_OK;
162033}
162034
162035#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
162036/*
162037** This routine sets the progress callback for an Sqlite database to the
162038** given callback function with the given argument. The progress callback will
162039** be invoked every nOps opcodes.
162040*/
162041SQLITE_API void sqlite3_progress_handler(
162042  sqlite3 *db,
162043  int nOps,
162044  int (*xProgress)(void*),
162045  void *pArg
162046){
162047#ifdef SQLITE_ENABLE_API_ARMOR
162048  if( !sqlite3SafetyCheckOk(db) ){
162049    (void)SQLITE_MISUSE_BKPT;
162050    return;
162051  }
162052#endif
162053  sqlite3_mutex_enter(db->mutex);
162054  if( nOps>0 ){
162055    db->xProgress = xProgress;
162056    db->nProgressOps = (unsigned)nOps;
162057    db->pProgressArg = pArg;
162058  }else{
162059    db->xProgress = 0;
162060    db->nProgressOps = 0;
162061    db->pProgressArg = 0;
162062  }
162063  sqlite3_mutex_leave(db->mutex);
162064}
162065#endif
162066
162067
162068/*
162069** This routine installs a default busy handler that waits for the
162070** specified number of milliseconds before returning 0.
162071*/
162072SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
162073#ifdef SQLITE_ENABLE_API_ARMOR
162074  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
162075#endif
162076  if( ms>0 ){
162077    sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
162078                             (void*)db);
162079    db->busyTimeout = ms;
162080  }else{
162081    sqlite3_busy_handler(db, 0, 0);
162082  }
162083  return SQLITE_OK;
162084}
162085
162086/*
162087** Cause any pending operation to stop at its earliest opportunity.
162088*/
162089SQLITE_API void sqlite3_interrupt(sqlite3 *db){
162090#ifdef SQLITE_ENABLE_API_ARMOR
162091  if( !sqlite3SafetyCheckOk(db) && (db==0 || db->magic!=SQLITE_MAGIC_ZOMBIE) ){
162092    (void)SQLITE_MISUSE_BKPT;
162093    return;
162094  }
162095#endif
162096  AtomicStore(&db->u1.isInterrupted, 1);
162097}
162098
162099
162100/*
162101** This function is exactly the same as sqlite3_create_function(), except
162102** that it is designed to be called by internal code. The difference is
162103** that if a malloc() fails in sqlite3_create_function(), an error code
162104** is returned and the mallocFailed flag cleared.
162105*/
162106SQLITE_PRIVATE int sqlite3CreateFunc(
162107  sqlite3 *db,
162108  const char *zFunctionName,
162109  int nArg,
162110  int enc,
162111  void *pUserData,
162112  void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
162113  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
162114  void (*xFinal)(sqlite3_context*),
162115  void (*xValue)(sqlite3_context*),
162116  void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
162117  FuncDestructor *pDestructor
162118){
162119  FuncDef *p;
162120  int nName;
162121  int extraFlags;
162122
162123  assert( sqlite3_mutex_held(db->mutex) );
162124  assert( xValue==0 || xSFunc==0 );
162125  if( zFunctionName==0                /* Must have a valid name */
162126   || (xSFunc!=0 && xFinal!=0)        /* Not both xSFunc and xFinal */
162127   || ((xFinal==0)!=(xStep==0))       /* Both or neither of xFinal and xStep */
162128   || ((xValue==0)!=(xInverse==0))    /* Both or neither of xValue, xInverse */
162129   || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG)
162130   || (255<(nName = sqlite3Strlen30( zFunctionName)))
162131  ){
162132    return SQLITE_MISUSE_BKPT;
162133  }
162134
162135  assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
162136  assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY );
162137  extraFlags = enc &  (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY|
162138                       SQLITE_SUBTYPE|SQLITE_INNOCUOUS);
162139  enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
162140
162141  /* The SQLITE_INNOCUOUS flag is the same bit as SQLITE_FUNC_UNSAFE.  But
162142  ** the meaning is inverted.  So flip the bit. */
162143  assert( SQLITE_FUNC_UNSAFE==SQLITE_INNOCUOUS );
162144  extraFlags ^= SQLITE_FUNC_UNSAFE;
162145
162146
162147#ifndef SQLITE_OMIT_UTF16
162148  /* If SQLITE_UTF16 is specified as the encoding type, transform this
162149  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
162150  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
162151  **
162152  ** If SQLITE_ANY is specified, add three versions of the function
162153  ** to the hash table.
162154  */
162155  if( enc==SQLITE_UTF16 ){
162156    enc = SQLITE_UTF16NATIVE;
162157  }else if( enc==SQLITE_ANY ){
162158    int rc;
162159    rc = sqlite3CreateFunc(db, zFunctionName, nArg,
162160         (SQLITE_UTF8|extraFlags)^SQLITE_FUNC_UNSAFE,
162161         pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
162162    if( rc==SQLITE_OK ){
162163      rc = sqlite3CreateFunc(db, zFunctionName, nArg,
162164           (SQLITE_UTF16LE|extraFlags)^SQLITE_FUNC_UNSAFE,
162165           pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
162166    }
162167    if( rc!=SQLITE_OK ){
162168      return rc;
162169    }
162170    enc = SQLITE_UTF16BE;
162171  }
162172#else
162173  enc = SQLITE_UTF8;
162174#endif
162175
162176  /* Check if an existing function is being overridden or deleted. If so,
162177  ** and there are active VMs, then return SQLITE_BUSY. If a function
162178  ** is being overridden/deleted but there are no active VMs, allow the
162179  ** operation to continue but invalidate all precompiled statements.
162180  */
162181  p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 0);
162182  if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==(u32)enc && p->nArg==nArg ){
162183    if( db->nVdbeActive ){
162184      sqlite3ErrorWithMsg(db, SQLITE_BUSY,
162185        "unable to delete/modify user-function due to active statements");
162186      assert( !db->mallocFailed );
162187      return SQLITE_BUSY;
162188    }else{
162189      sqlite3ExpirePreparedStatements(db, 0);
162190    }
162191  }
162192
162193  p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 1);
162194  assert(p || db->mallocFailed);
162195  if( !p ){
162196    return SQLITE_NOMEM_BKPT;
162197  }
162198
162199  /* If an older version of the function with a configured destructor is
162200  ** being replaced invoke the destructor function here. */
162201  functionDestroy(db, p);
162202
162203  if( pDestructor ){
162204    pDestructor->nRef++;
162205  }
162206  p->u.pDestructor = pDestructor;
162207  p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
162208  testcase( p->funcFlags & SQLITE_DETERMINISTIC );
162209  testcase( p->funcFlags & SQLITE_DIRECTONLY );
162210  p->xSFunc = xSFunc ? xSFunc : xStep;
162211  p->xFinalize = xFinal;
162212  p->xValue = xValue;
162213  p->xInverse = xInverse;
162214  p->pUserData = pUserData;
162215  p->nArg = (u16)nArg;
162216  return SQLITE_OK;
162217}
162218
162219/*
162220** Worker function used by utf-8 APIs that create new functions:
162221**
162222**    sqlite3_create_function()
162223**    sqlite3_create_function_v2()
162224**    sqlite3_create_window_function()
162225*/
162226static int createFunctionApi(
162227  sqlite3 *db,
162228  const char *zFunc,
162229  int nArg,
162230  int enc,
162231  void *p,
162232  void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
162233  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
162234  void (*xFinal)(sqlite3_context*),
162235  void (*xValue)(sqlite3_context*),
162236  void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
162237  void(*xDestroy)(void*)
162238){
162239  int rc = SQLITE_ERROR;
162240  FuncDestructor *pArg = 0;
162241
162242#ifdef SQLITE_ENABLE_API_ARMOR
162243  if( !sqlite3SafetyCheckOk(db) ){
162244    return SQLITE_MISUSE_BKPT;
162245  }
162246#endif
162247  sqlite3_mutex_enter(db->mutex);
162248  if( xDestroy ){
162249    pArg = (FuncDestructor *)sqlite3Malloc(sizeof(FuncDestructor));
162250    if( !pArg ){
162251      sqlite3OomFault(db);
162252      xDestroy(p);
162253      goto out;
162254    }
162255    pArg->nRef = 0;
162256    pArg->xDestroy = xDestroy;
162257    pArg->pUserData = p;
162258  }
162259  rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p,
162260      xSFunc, xStep, xFinal, xValue, xInverse, pArg
162261  );
162262  if( pArg && pArg->nRef==0 ){
162263    assert( rc!=SQLITE_OK );
162264    xDestroy(p);
162265    sqlite3_free(pArg);
162266  }
162267
162268 out:
162269  rc = sqlite3ApiExit(db, rc);
162270  sqlite3_mutex_leave(db->mutex);
162271  return rc;
162272}
162273
162274/*
162275** Create new user functions.
162276*/
162277SQLITE_API int sqlite3_create_function(
162278  sqlite3 *db,
162279  const char *zFunc,
162280  int nArg,
162281  int enc,
162282  void *p,
162283  void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
162284  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
162285  void (*xFinal)(sqlite3_context*)
162286){
162287  return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
162288                                    xFinal, 0, 0, 0);
162289}
162290SQLITE_API int sqlite3_create_function_v2(
162291  sqlite3 *db,
162292  const char *zFunc,
162293  int nArg,
162294  int enc,
162295  void *p,
162296  void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
162297  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
162298  void (*xFinal)(sqlite3_context*),
162299  void (*xDestroy)(void *)
162300){
162301  return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
162302                                    xFinal, 0, 0, xDestroy);
162303}
162304SQLITE_API int sqlite3_create_window_function(
162305  sqlite3 *db,
162306  const char *zFunc,
162307  int nArg,
162308  int enc,
162309  void *p,
162310  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
162311  void (*xFinal)(sqlite3_context*),
162312  void (*xValue)(sqlite3_context*),
162313  void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
162314  void (*xDestroy)(void *)
162315){
162316  return createFunctionApi(db, zFunc, nArg, enc, p, 0, xStep,
162317                                    xFinal, xValue, xInverse, xDestroy);
162318}
162319
162320#ifndef SQLITE_OMIT_UTF16
162321SQLITE_API int sqlite3_create_function16(
162322  sqlite3 *db,
162323  const void *zFunctionName,
162324  int nArg,
162325  int eTextRep,
162326  void *p,
162327  void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
162328  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
162329  void (*xFinal)(sqlite3_context*)
162330){
162331  int rc;
162332  char *zFunc8;
162333
162334#ifdef SQLITE_ENABLE_API_ARMOR
162335  if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT;
162336#endif
162337  sqlite3_mutex_enter(db->mutex);
162338  assert( !db->mallocFailed );
162339  zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
162340  rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xSFunc,xStep,xFinal,0,0,0);
162341  sqlite3DbFree(db, zFunc8);
162342  rc = sqlite3ApiExit(db, rc);
162343  sqlite3_mutex_leave(db->mutex);
162344  return rc;
162345}
162346#endif
162347
162348
162349/*
162350** The following is the implementation of an SQL function that always
162351** fails with an error message stating that the function is used in the
162352** wrong context.  The sqlite3_overload_function() API might construct
162353** SQL function that use this routine so that the functions will exist
162354** for name resolution but are actually overloaded by the xFindFunction
162355** method of virtual tables.
162356*/
162357static void sqlite3InvalidFunction(
162358  sqlite3_context *context,  /* The function calling context */
162359  int NotUsed,               /* Number of arguments to the function */
162360  sqlite3_value **NotUsed2   /* Value of each argument */
162361){
162362  const char *zName = (const char*)sqlite3_user_data(context);
162363  char *zErr;
162364  UNUSED_PARAMETER2(NotUsed, NotUsed2);
162365  zErr = sqlite3_mprintf(
162366      "unable to use function %s in the requested context", zName);
162367  sqlite3_result_error(context, zErr, -1);
162368  sqlite3_free(zErr);
162369}
162370
162371/*
162372** Declare that a function has been overloaded by a virtual table.
162373**
162374** If the function already exists as a regular global function, then
162375** this routine is a no-op.  If the function does not exist, then create
162376** a new one that always throws a run-time error.
162377**
162378** When virtual tables intend to provide an overloaded function, they
162379** should call this routine to make sure the global function exists.
162380** A global function must exist in order for name resolution to work
162381** properly.
162382*/
162383SQLITE_API int sqlite3_overload_function(
162384  sqlite3 *db,
162385  const char *zName,
162386  int nArg
162387){
162388  int rc;
162389  char *zCopy;
162390
162391#ifdef SQLITE_ENABLE_API_ARMOR
162392  if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
162393    return SQLITE_MISUSE_BKPT;
162394  }
162395#endif
162396  sqlite3_mutex_enter(db->mutex);
162397  rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)!=0;
162398  sqlite3_mutex_leave(db->mutex);
162399  if( rc ) return SQLITE_OK;
162400  zCopy = sqlite3_mprintf(zName);
162401  if( zCopy==0 ) return SQLITE_NOMEM;
162402  return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF8,
162403                           zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free);
162404}
162405
162406#ifndef SQLITE_OMIT_TRACE
162407/*
162408** Register a trace function.  The pArg from the previously registered trace
162409** is returned.
162410**
162411** A NULL trace function means that no tracing is executes.  A non-NULL
162412** trace is a pointer to a function that is invoked at the start of each
162413** SQL statement.
162414*/
162415#ifndef SQLITE_OMIT_DEPRECATED
162416SQLITE_API void *sqlite3_trace(sqlite3 *db, void(*xTrace)(void*,const char*), void *pArg){
162417  void *pOld;
162418
162419#ifdef SQLITE_ENABLE_API_ARMOR
162420  if( !sqlite3SafetyCheckOk(db) ){
162421    (void)SQLITE_MISUSE_BKPT;
162422    return 0;
162423  }
162424#endif
162425  sqlite3_mutex_enter(db->mutex);
162426  pOld = db->pTraceArg;
162427  db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0;
162428  db->xTrace = (int(*)(u32,void*,void*,void*))xTrace;
162429  db->pTraceArg = pArg;
162430  sqlite3_mutex_leave(db->mutex);
162431  return pOld;
162432}
162433#endif /* SQLITE_OMIT_DEPRECATED */
162434
162435/* Register a trace callback using the version-2 interface.
162436*/
162437SQLITE_API int sqlite3_trace_v2(
162438  sqlite3 *db,                               /* Trace this connection */
162439  unsigned mTrace,                           /* Mask of events to be traced */
162440  int(*xTrace)(unsigned,void*,void*,void*),  /* Callback to invoke */
162441  void *pArg                                 /* Context */
162442){
162443#ifdef SQLITE_ENABLE_API_ARMOR
162444  if( !sqlite3SafetyCheckOk(db) ){
162445    return SQLITE_MISUSE_BKPT;
162446  }
162447#endif
162448  sqlite3_mutex_enter(db->mutex);
162449  if( mTrace==0 ) xTrace = 0;
162450  if( xTrace==0 ) mTrace = 0;
162451  db->mTrace = mTrace;
162452  db->xTrace = xTrace;
162453  db->pTraceArg = pArg;
162454  sqlite3_mutex_leave(db->mutex);
162455  return SQLITE_OK;
162456}
162457
162458#ifndef SQLITE_OMIT_DEPRECATED
162459/*
162460** Register a profile function.  The pArg from the previously registered
162461** profile function is returned.
162462**
162463** A NULL profile function means that no profiling is executes.  A non-NULL
162464** profile is a pointer to a function that is invoked at the conclusion of
162465** each SQL statement that is run.
162466*/
162467SQLITE_API void *sqlite3_profile(
162468  sqlite3 *db,
162469  void (*xProfile)(void*,const char*,sqlite_uint64),
162470  void *pArg
162471){
162472  void *pOld;
162473
162474#ifdef SQLITE_ENABLE_API_ARMOR
162475  if( !sqlite3SafetyCheckOk(db) ){
162476    (void)SQLITE_MISUSE_BKPT;
162477    return 0;
162478  }
162479#endif
162480  sqlite3_mutex_enter(db->mutex);
162481  pOld = db->pProfileArg;
162482  db->xProfile = xProfile;
162483  db->pProfileArg = pArg;
162484  db->mTrace &= SQLITE_TRACE_NONLEGACY_MASK;
162485  if( db->xProfile ) db->mTrace |= SQLITE_TRACE_XPROFILE;
162486  sqlite3_mutex_leave(db->mutex);
162487  return pOld;
162488}
162489#endif /* SQLITE_OMIT_DEPRECATED */
162490#endif /* SQLITE_OMIT_TRACE */
162491
162492/*
162493** Register a function to be invoked when a transaction commits.
162494** If the invoked function returns non-zero, then the commit becomes a
162495** rollback.
162496*/
162497SQLITE_API void *sqlite3_commit_hook(
162498  sqlite3 *db,              /* Attach the hook to this database */
162499  int (*xCallback)(void*),  /* Function to invoke on each commit */
162500  void *pArg                /* Argument to the function */
162501){
162502  void *pOld;
162503
162504#ifdef SQLITE_ENABLE_API_ARMOR
162505  if( !sqlite3SafetyCheckOk(db) ){
162506    (void)SQLITE_MISUSE_BKPT;
162507    return 0;
162508  }
162509#endif
162510  sqlite3_mutex_enter(db->mutex);
162511  pOld = db->pCommitArg;
162512  db->xCommitCallback = xCallback;
162513  db->pCommitArg = pArg;
162514  sqlite3_mutex_leave(db->mutex);
162515  return pOld;
162516}
162517
162518/*
162519** Register a callback to be invoked each time a row is updated,
162520** inserted or deleted using this database connection.
162521*/
162522SQLITE_API void *sqlite3_update_hook(
162523  sqlite3 *db,              /* Attach the hook to this database */
162524  void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
162525  void *pArg                /* Argument to the function */
162526){
162527  void *pRet;
162528
162529#ifdef SQLITE_ENABLE_API_ARMOR
162530  if( !sqlite3SafetyCheckOk(db) ){
162531    (void)SQLITE_MISUSE_BKPT;
162532    return 0;
162533  }
162534#endif
162535  sqlite3_mutex_enter(db->mutex);
162536  pRet = db->pUpdateArg;
162537  db->xUpdateCallback = xCallback;
162538  db->pUpdateArg = pArg;
162539  sqlite3_mutex_leave(db->mutex);
162540  return pRet;
162541}
162542
162543/*
162544** Register a callback to be invoked each time a transaction is rolled
162545** back by this database connection.
162546*/
162547SQLITE_API void *sqlite3_rollback_hook(
162548  sqlite3 *db,              /* Attach the hook to this database */
162549  void (*xCallback)(void*), /* Callback function */
162550  void *pArg                /* Argument to the function */
162551){
162552  void *pRet;
162553
162554#ifdef SQLITE_ENABLE_API_ARMOR
162555  if( !sqlite3SafetyCheckOk(db) ){
162556    (void)SQLITE_MISUSE_BKPT;
162557    return 0;
162558  }
162559#endif
162560  sqlite3_mutex_enter(db->mutex);
162561  pRet = db->pRollbackArg;
162562  db->xRollbackCallback = xCallback;
162563  db->pRollbackArg = pArg;
162564  sqlite3_mutex_leave(db->mutex);
162565  return pRet;
162566}
162567
162568#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
162569/*
162570** Register a callback to be invoked each time a row is updated,
162571** inserted or deleted using this database connection.
162572*/
162573SQLITE_API void *sqlite3_preupdate_hook(
162574  sqlite3 *db,              /* Attach the hook to this database */
162575  void(*xCallback)(         /* Callback function */
162576    void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64),
162577  void *pArg                /* First callback argument */
162578){
162579  void *pRet;
162580  sqlite3_mutex_enter(db->mutex);
162581  pRet = db->pPreUpdateArg;
162582  db->xPreUpdateCallback = xCallback;
162583  db->pPreUpdateArg = pArg;
162584  sqlite3_mutex_leave(db->mutex);
162585  return pRet;
162586}
162587#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
162588
162589#ifndef SQLITE_OMIT_WAL
162590/*
162591** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
162592** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
162593** is greater than sqlite3.pWalArg cast to an integer (the value configured by
162594** wal_autocheckpoint()).
162595*/
162596SQLITE_PRIVATE int sqlite3WalDefaultHook(
162597  void *pClientData,     /* Argument */
162598  sqlite3 *db,           /* Connection */
162599  const char *zDb,       /* Database */
162600  int nFrame             /* Size of WAL */
162601){
162602  if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
162603    sqlite3BeginBenignMalloc();
162604    sqlite3_wal_checkpoint(db, zDb);
162605    sqlite3EndBenignMalloc();
162606  }
162607  return SQLITE_OK;
162608}
162609#endif /* SQLITE_OMIT_WAL */
162610
162611/*
162612** Configure an sqlite3_wal_hook() callback to automatically checkpoint
162613** a database after committing a transaction if there are nFrame or
162614** more frames in the log file. Passing zero or a negative value as the
162615** nFrame parameter disables automatic checkpoints entirely.
162616**
162617** The callback registered by this function replaces any existing callback
162618** registered using sqlite3_wal_hook(). Likewise, registering a callback
162619** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
162620** configured by this function.
162621*/
162622SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
162623#ifdef SQLITE_OMIT_WAL
162624  UNUSED_PARAMETER(db);
162625  UNUSED_PARAMETER(nFrame);
162626#else
162627#ifdef SQLITE_ENABLE_API_ARMOR
162628  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
162629#endif
162630  if( nFrame>0 ){
162631    sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
162632  }else{
162633    sqlite3_wal_hook(db, 0, 0);
162634  }
162635#endif
162636  return SQLITE_OK;
162637}
162638
162639/*
162640** Register a callback to be invoked each time a transaction is written
162641** into the write-ahead-log by this database connection.
162642*/
162643SQLITE_API void *sqlite3_wal_hook(
162644  sqlite3 *db,                    /* Attach the hook to this db handle */
162645  int(*xCallback)(void *, sqlite3*, const char*, int),
162646  void *pArg                      /* First argument passed to xCallback() */
162647){
162648#ifndef SQLITE_OMIT_WAL
162649  void *pRet;
162650#ifdef SQLITE_ENABLE_API_ARMOR
162651  if( !sqlite3SafetyCheckOk(db) ){
162652    (void)SQLITE_MISUSE_BKPT;
162653    return 0;
162654  }
162655#endif
162656  sqlite3_mutex_enter(db->mutex);
162657  pRet = db->pWalArg;
162658  db->xWalCallback = xCallback;
162659  db->pWalArg = pArg;
162660  sqlite3_mutex_leave(db->mutex);
162661  return pRet;
162662#else
162663  return 0;
162664#endif
162665}
162666
162667/*
162668** Checkpoint database zDb.
162669*/
162670SQLITE_API int sqlite3_wal_checkpoint_v2(
162671  sqlite3 *db,                    /* Database handle */
162672  const char *zDb,                /* Name of attached database (or NULL) */
162673  int eMode,                      /* SQLITE_CHECKPOINT_* value */
162674  int *pnLog,                     /* OUT: Size of WAL log in frames */
162675  int *pnCkpt                     /* OUT: Total number of frames checkpointed */
162676){
162677#ifdef SQLITE_OMIT_WAL
162678  return SQLITE_OK;
162679#else
162680  int rc;                         /* Return code */
162681  int iDb = SQLITE_MAX_ATTACHED;  /* sqlite3.aDb[] index of db to checkpoint */
162682
162683#ifdef SQLITE_ENABLE_API_ARMOR
162684  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
162685#endif
162686
162687  /* Initialize the output variables to -1 in case an error occurs. */
162688  if( pnLog ) *pnLog = -1;
162689  if( pnCkpt ) *pnCkpt = -1;
162690
162691  assert( SQLITE_CHECKPOINT_PASSIVE==0 );
162692  assert( SQLITE_CHECKPOINT_FULL==1 );
162693  assert( SQLITE_CHECKPOINT_RESTART==2 );
162694  assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
162695  if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_TRUNCATE ){
162696    /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
162697    ** mode: */
162698    return SQLITE_MISUSE;
162699  }
162700
162701  sqlite3_mutex_enter(db->mutex);
162702  if( zDb && zDb[0] ){
162703    iDb = sqlite3FindDbName(db, zDb);
162704  }
162705  if( iDb<0 ){
162706    rc = SQLITE_ERROR;
162707    sqlite3ErrorWithMsg(db, SQLITE_ERROR, "unknown database: %s", zDb);
162708  }else{
162709    db->busyHandler.nBusy = 0;
162710    rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
162711    sqlite3Error(db, rc);
162712  }
162713  rc = sqlite3ApiExit(db, rc);
162714
162715  /* If there are no active statements, clear the interrupt flag at this
162716  ** point.  */
162717  if( db->nVdbeActive==0 ){
162718    AtomicStore(&db->u1.isInterrupted, 0);
162719  }
162720
162721  sqlite3_mutex_leave(db->mutex);
162722  return rc;
162723#endif
162724}
162725
162726
162727/*
162728** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
162729** to contains a zero-length string, all attached databases are
162730** checkpointed.
162731*/
162732SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
162733  /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
162734  ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
162735  return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,0,0);
162736}
162737
162738#ifndef SQLITE_OMIT_WAL
162739/*
162740** Run a checkpoint on database iDb. This is a no-op if database iDb is
162741** not currently open in WAL mode.
162742**
162743** If a transaction is open on the database being checkpointed, this
162744** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
162745** an error occurs while running the checkpoint, an SQLite error code is
162746** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
162747**
162748** The mutex on database handle db should be held by the caller. The mutex
162749** associated with the specific b-tree being checkpointed is taken by
162750** this function while the checkpoint is running.
162751**
162752** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
162753** checkpointed. If an error is encountered it is returned immediately -
162754** no attempt is made to checkpoint any remaining databases.
162755**
162756** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL, RESTART
162757** or TRUNCATE.
162758*/
162759SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
162760  int rc = SQLITE_OK;             /* Return code */
162761  int i;                          /* Used to iterate through attached dbs */
162762  int bBusy = 0;                  /* True if SQLITE_BUSY has been encountered */
162763
162764  assert( sqlite3_mutex_held(db->mutex) );
162765  assert( !pnLog || *pnLog==-1 );
162766  assert( !pnCkpt || *pnCkpt==-1 );
162767
162768  for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
162769    if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
162770      rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
162771      pnLog = 0;
162772      pnCkpt = 0;
162773      if( rc==SQLITE_BUSY ){
162774        bBusy = 1;
162775        rc = SQLITE_OK;
162776      }
162777    }
162778  }
162779
162780  return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
162781}
162782#endif /* SQLITE_OMIT_WAL */
162783
162784/*
162785** This function returns true if main-memory should be used instead of
162786** a temporary file for transient pager files and statement journals.
162787** The value returned depends on the value of db->temp_store (runtime
162788** parameter) and the compile time value of SQLITE_TEMP_STORE. The
162789** following table describes the relationship between these two values
162790** and this functions return value.
162791**
162792**   SQLITE_TEMP_STORE     db->temp_store     Location of temporary database
162793**   -----------------     --------------     ------------------------------
162794**   0                     any                file      (return 0)
162795**   1                     1                  file      (return 0)
162796**   1                     2                  memory    (return 1)
162797**   1                     0                  file      (return 0)
162798**   2                     1                  file      (return 0)
162799**   2                     2                  memory    (return 1)
162800**   2                     0                  memory    (return 1)
162801**   3                     any                memory    (return 1)
162802*/
162803SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
162804#if SQLITE_TEMP_STORE==1
162805  return ( db->temp_store==2 );
162806#endif
162807#if SQLITE_TEMP_STORE==2
162808  return ( db->temp_store!=1 );
162809#endif
162810#if SQLITE_TEMP_STORE==3
162811  UNUSED_PARAMETER(db);
162812  return 1;
162813#endif
162814#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
162815  UNUSED_PARAMETER(db);
162816  return 0;
162817#endif
162818}
162819
162820/*
162821** Return UTF-8 encoded English language explanation of the most recent
162822** error.
162823*/
162824SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
162825  const char *z;
162826  if( !db ){
162827    return sqlite3ErrStr(SQLITE_NOMEM_BKPT);
162828  }
162829  if( !sqlite3SafetyCheckSickOrOk(db) ){
162830    return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
162831  }
162832  sqlite3_mutex_enter(db->mutex);
162833  if( db->mallocFailed ){
162834    z = sqlite3ErrStr(SQLITE_NOMEM_BKPT);
162835  }else{
162836    testcase( db->pErr==0 );
162837    z = db->errCode ? (char*)sqlite3_value_text(db->pErr) : 0;
162838    assert( !db->mallocFailed );
162839    if( z==0 ){
162840      z = sqlite3ErrStr(db->errCode);
162841    }
162842  }
162843  sqlite3_mutex_leave(db->mutex);
162844  return z;
162845}
162846
162847#ifndef SQLITE_OMIT_UTF16
162848/*
162849** Return UTF-16 encoded English language explanation of the most recent
162850** error.
162851*/
162852SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
162853  static const u16 outOfMem[] = {
162854    'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
162855  };
162856  static const u16 misuse[] = {
162857    'b', 'a', 'd', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', ' ',
162858    'o', 'r', ' ', 'o', 't', 'h', 'e', 'r', ' ', 'A', 'P', 'I', ' ',
162859    'm', 'i', 's', 'u', 's', 'e', 0
162860  };
162861
162862  const void *z;
162863  if( !db ){
162864    return (void *)outOfMem;
162865  }
162866  if( !sqlite3SafetyCheckSickOrOk(db) ){
162867    return (void *)misuse;
162868  }
162869  sqlite3_mutex_enter(db->mutex);
162870  if( db->mallocFailed ){
162871    z = (void *)outOfMem;
162872  }else{
162873    z = sqlite3_value_text16(db->pErr);
162874    if( z==0 ){
162875      sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
162876      z = sqlite3_value_text16(db->pErr);
162877    }
162878    /* A malloc() may have failed within the call to sqlite3_value_text16()
162879    ** above. If this is the case, then the db->mallocFailed flag needs to
162880    ** be cleared before returning. Do this directly, instead of via
162881    ** sqlite3ApiExit(), to avoid setting the database handle error message.
162882    */
162883    sqlite3OomClear(db);
162884  }
162885  sqlite3_mutex_leave(db->mutex);
162886  return z;
162887}
162888#endif /* SQLITE_OMIT_UTF16 */
162889
162890/*
162891** Return the most recent error code generated by an SQLite routine. If NULL is
162892** passed to this function, we assume a malloc() failed during sqlite3_open().
162893*/
162894SQLITE_API int sqlite3_errcode(sqlite3 *db){
162895  if( db && !sqlite3SafetyCheckSickOrOk(db) ){
162896    return SQLITE_MISUSE_BKPT;
162897  }
162898  if( !db || db->mallocFailed ){
162899    return SQLITE_NOMEM_BKPT;
162900  }
162901  return db->errCode & db->errMask;
162902}
162903SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
162904  if( db && !sqlite3SafetyCheckSickOrOk(db) ){
162905    return SQLITE_MISUSE_BKPT;
162906  }
162907  if( !db || db->mallocFailed ){
162908    return SQLITE_NOMEM_BKPT;
162909  }
162910  return db->errCode;
162911}
162912SQLITE_API int sqlite3_system_errno(sqlite3 *db){
162913  return db ? db->iSysErrno : 0;
162914}
162915
162916/*
162917** Return a string that describes the kind of error specified in the
162918** argument.  For now, this simply calls the internal sqlite3ErrStr()
162919** function.
162920*/
162921SQLITE_API const char *sqlite3_errstr(int rc){
162922  return sqlite3ErrStr(rc);
162923}
162924
162925/*
162926** Create a new collating function for database "db".  The name is zName
162927** and the encoding is enc.
162928*/
162929static int createCollation(
162930  sqlite3* db,
162931  const char *zName,
162932  u8 enc,
162933  void* pCtx,
162934  int(*xCompare)(void*,int,const void*,int,const void*),
162935  void(*xDel)(void*)
162936){
162937  CollSeq *pColl;
162938  int enc2;
162939
162940  assert( sqlite3_mutex_held(db->mutex) );
162941
162942  /* If SQLITE_UTF16 is specified as the encoding type, transform this
162943  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
162944  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
162945  */
162946  enc2 = enc;
162947  testcase( enc2==SQLITE_UTF16 );
162948  testcase( enc2==SQLITE_UTF16_ALIGNED );
162949  if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
162950    enc2 = SQLITE_UTF16NATIVE;
162951  }
162952  if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
162953    return SQLITE_MISUSE_BKPT;
162954  }
162955
162956  /* Check if this call is removing or replacing an existing collation
162957  ** sequence. If so, and there are active VMs, return busy. If there
162958  ** are no active VMs, invalidate any pre-compiled statements.
162959  */
162960  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
162961  if( pColl && pColl->xCmp ){
162962    if( db->nVdbeActive ){
162963      sqlite3ErrorWithMsg(db, SQLITE_BUSY,
162964        "unable to delete/modify collation sequence due to active statements");
162965      return SQLITE_BUSY;
162966    }
162967    sqlite3ExpirePreparedStatements(db, 0);
162968
162969    /* If collation sequence pColl was created directly by a call to
162970    ** sqlite3_create_collation, and not generated by synthCollSeq(),
162971    ** then any copies made by synthCollSeq() need to be invalidated.
162972    ** Also, collation destructor - CollSeq.xDel() - function may need
162973    ** to be called.
162974    */
162975    if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
162976      CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
162977      int j;
162978      for(j=0; j<3; j++){
162979        CollSeq *p = &aColl[j];
162980        if( p->enc==pColl->enc ){
162981          if( p->xDel ){
162982            p->xDel(p->pUser);
162983          }
162984          p->xCmp = 0;
162985        }
162986      }
162987    }
162988  }
162989
162990  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
162991  if( pColl==0 ) return SQLITE_NOMEM_BKPT;
162992  pColl->xCmp = xCompare;
162993  pColl->pUser = pCtx;
162994  pColl->xDel = xDel;
162995  pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
162996  sqlite3Error(db, SQLITE_OK);
162997  return SQLITE_OK;
162998}
162999
163000
163001/*
163002** This array defines hard upper bounds on limit values.  The
163003** initializer must be kept in sync with the SQLITE_LIMIT_*
163004** #defines in sqlite3.h.
163005*/
163006static const int aHardLimit[] = {
163007  SQLITE_MAX_LENGTH,
163008  SQLITE_MAX_SQL_LENGTH,
163009  SQLITE_MAX_COLUMN,
163010  SQLITE_MAX_EXPR_DEPTH,
163011  SQLITE_MAX_COMPOUND_SELECT,
163012  SQLITE_MAX_VDBE_OP,
163013  SQLITE_MAX_FUNCTION_ARG,
163014  SQLITE_MAX_ATTACHED,
163015  SQLITE_MAX_LIKE_PATTERN_LENGTH,
163016  SQLITE_MAX_VARIABLE_NUMBER,      /* IMP: R-38091-32352 */
163017  SQLITE_MAX_TRIGGER_DEPTH,
163018  SQLITE_MAX_WORKER_THREADS,
163019};
163020
163021/*
163022** Make sure the hard limits are set to reasonable values
163023*/
163024#if SQLITE_MAX_LENGTH<100
163025# error SQLITE_MAX_LENGTH must be at least 100
163026#endif
163027#if SQLITE_MAX_SQL_LENGTH<100
163028# error SQLITE_MAX_SQL_LENGTH must be at least 100
163029#endif
163030#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
163031# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
163032#endif
163033#if SQLITE_MAX_COMPOUND_SELECT<2
163034# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
163035#endif
163036#if SQLITE_MAX_VDBE_OP<40
163037# error SQLITE_MAX_VDBE_OP must be at least 40
163038#endif
163039#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>127
163040# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 127
163041#endif
163042#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125
163043# error SQLITE_MAX_ATTACHED must be between 0 and 125
163044#endif
163045#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
163046# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
163047#endif
163048#if SQLITE_MAX_COLUMN>32767
163049# error SQLITE_MAX_COLUMN must not exceed 32767
163050#endif
163051#if SQLITE_MAX_TRIGGER_DEPTH<1
163052# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
163053#endif
163054#if SQLITE_MAX_WORKER_THREADS<0 || SQLITE_MAX_WORKER_THREADS>50
163055# error SQLITE_MAX_WORKER_THREADS must be between 0 and 50
163056#endif
163057
163058
163059/*
163060** Change the value of a limit.  Report the old value.
163061** If an invalid limit index is supplied, report -1.
163062** Make no changes but still report the old value if the
163063** new limit is negative.
163064**
163065** A new lower limit does not shrink existing constructs.
163066** It merely prevents new constructs that exceed the limit
163067** from forming.
163068*/
163069SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
163070  int oldLimit;
163071
163072#ifdef SQLITE_ENABLE_API_ARMOR
163073  if( !sqlite3SafetyCheckOk(db) ){
163074    (void)SQLITE_MISUSE_BKPT;
163075    return -1;
163076  }
163077#endif
163078
163079  /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
163080  ** there is a hard upper bound set at compile-time by a C preprocessor
163081  ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
163082  ** "_MAX_".)
163083  */
163084  assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
163085  assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
163086  assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
163087  assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
163088  assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
163089  assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
163090  assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
163091  assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
163092  assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
163093                                               SQLITE_MAX_LIKE_PATTERN_LENGTH );
163094  assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
163095  assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
163096  assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS );
163097  assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) );
163098
163099
163100  if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
163101    return -1;
163102  }
163103  oldLimit = db->aLimit[limitId];
163104  if( newLimit>=0 ){                   /* IMP: R-52476-28732 */
163105    if( newLimit>aHardLimit[limitId] ){
163106      newLimit = aHardLimit[limitId];  /* IMP: R-51463-25634 */
163107    }
163108    db->aLimit[limitId] = newLimit;
163109  }
163110  return oldLimit;                     /* IMP: R-53341-35419 */
163111}
163112
163113/*
163114** This function is used to parse both URIs and non-URI filenames passed by the
163115** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
163116** URIs specified as part of ATTACH statements.
163117**
163118** The first argument to this function is the name of the VFS to use (or
163119** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
163120** query parameter. The second argument contains the URI (or non-URI filename)
163121** itself. When this function is called the *pFlags variable should contain
163122** the default flags to open the database handle with. The value stored in
163123** *pFlags may be updated before returning if the URI filename contains
163124** "cache=xxx" or "mode=xxx" query parameters.
163125**
163126** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
163127** the VFS that should be used to open the database file. *pzFile is set to
163128** point to a buffer containing the name of the file to open.  The value
163129** stored in *pzFile is a database name acceptable to sqlite3_uri_parameter()
163130** and is in the same format as names created using sqlite3_create_filename().
163131** The caller must invoke sqlite3_free_filename() (not sqlite3_free()!) on
163132** the value returned in *pzFile to avoid a memory leak.
163133**
163134** If an error occurs, then an SQLite error code is returned and *pzErrMsg
163135** may be set to point to a buffer containing an English language error
163136** message. It is the responsibility of the caller to eventually release
163137** this buffer by calling sqlite3_free().
163138*/
163139SQLITE_PRIVATE int sqlite3ParseUri(
163140  const char *zDefaultVfs,        /* VFS to use if no "vfs=xxx" query option */
163141  const char *zUri,               /* Nul-terminated URI to parse */
163142  unsigned int *pFlags,           /* IN/OUT: SQLITE_OPEN_XXX flags */
163143  sqlite3_vfs **ppVfs,            /* OUT: VFS to use */
163144  char **pzFile,                  /* OUT: Filename component of URI */
163145  char **pzErrMsg                 /* OUT: Error message (if rc!=SQLITE_OK) */
163146){
163147  int rc = SQLITE_OK;
163148  unsigned int flags = *pFlags;
163149  const char *zVfs = zDefaultVfs;
163150  char *zFile;
163151  char c;
163152  int nUri = sqlite3Strlen30(zUri);
163153
163154  assert( *pzErrMsg==0 );
163155
163156  if( ((flags & SQLITE_OPEN_URI)             /* IMP: R-48725-32206 */
163157            || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
163158   && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
163159  ){
163160    char *zOpt;
163161    int eState;                   /* Parser state when parsing URI */
163162    int iIn;                      /* Input character index */
163163    int iOut = 0;                 /* Output character index */
163164    u64 nByte = nUri+8;           /* Bytes of space to allocate */
163165
163166    /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
163167    ** method that there may be extra parameters following the file-name.  */
163168    flags |= SQLITE_OPEN_URI;
163169
163170    for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
163171    zFile = sqlite3_malloc64(nByte);
163172    if( !zFile ) return SQLITE_NOMEM_BKPT;
163173
163174    memset(zFile, 0, 4);  /* 4-byte of 0x00 is the start of DB name marker */
163175    zFile += 4;
163176
163177    iIn = 5;
163178#ifdef SQLITE_ALLOW_URI_AUTHORITY
163179    if( strncmp(zUri+5, "///", 3)==0 ){
163180      iIn = 7;
163181      /* The following condition causes URIs with five leading / characters
163182      ** like file://///host/path to be converted into UNCs like //host/path.
163183      ** The correct URI for that UNC has only two or four leading / characters
163184      ** file://host/path or file:////host/path.  But 5 leading slashes is a
163185      ** common error, we are told, so we handle it as a special case. */
163186      if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; }
163187    }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){
163188      iIn = 16;
163189    }
163190#else
163191    /* Discard the scheme and authority segments of the URI. */
163192    if( zUri[5]=='/' && zUri[6]=='/' ){
163193      iIn = 7;
163194      while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
163195      if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
163196        *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
163197            iIn-7, &zUri[7]);
163198        rc = SQLITE_ERROR;
163199        goto parse_uri_out;
163200      }
163201    }
163202#endif
163203
163204    /* Copy the filename and any query parameters into the zFile buffer.
163205    ** Decode %HH escape codes along the way.
163206    **
163207    ** Within this loop, variable eState may be set to 0, 1 or 2, depending
163208    ** on the parsing context. As follows:
163209    **
163210    **   0: Parsing file-name.
163211    **   1: Parsing name section of a name=value query parameter.
163212    **   2: Parsing value section of a name=value query parameter.
163213    */
163214    eState = 0;
163215    while( (c = zUri[iIn])!=0 && c!='#' ){
163216      iIn++;
163217      if( c=='%'
163218       && sqlite3Isxdigit(zUri[iIn])
163219       && sqlite3Isxdigit(zUri[iIn+1])
163220      ){
163221        int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
163222        octet += sqlite3HexToInt(zUri[iIn++]);
163223
163224        assert( octet>=0 && octet<256 );
163225        if( octet==0 ){
163226#ifndef SQLITE_ENABLE_URI_00_ERROR
163227          /* This branch is taken when "%00" appears within the URI. In this
163228          ** case we ignore all text in the remainder of the path, name or
163229          ** value currently being parsed. So ignore the current character
163230          ** and skip to the next "?", "=" or "&", as appropriate. */
163231          while( (c = zUri[iIn])!=0 && c!='#'
163232              && (eState!=0 || c!='?')
163233              && (eState!=1 || (c!='=' && c!='&'))
163234              && (eState!=2 || c!='&')
163235          ){
163236            iIn++;
163237          }
163238          continue;
163239#else
163240          /* If ENABLE_URI_00_ERROR is defined, "%00" in a URI is an error. */
163241          *pzErrMsg = sqlite3_mprintf("unexpected %%00 in uri");
163242          rc = SQLITE_ERROR;
163243          goto parse_uri_out;
163244#endif
163245        }
163246        c = octet;
163247      }else if( eState==1 && (c=='&' || c=='=') ){
163248        if( zFile[iOut-1]==0 ){
163249          /* An empty option name. Ignore this option altogether. */
163250          while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
163251          continue;
163252        }
163253        if( c=='&' ){
163254          zFile[iOut++] = '\0';
163255        }else{
163256          eState = 2;
163257        }
163258        c = 0;
163259      }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
163260        c = 0;
163261        eState = 1;
163262      }
163263      zFile[iOut++] = c;
163264    }
163265    if( eState==1 ) zFile[iOut++] = '\0';
163266    memset(zFile+iOut, 0, 4); /* end-of-options + empty journal filenames */
163267
163268    /* Check if there were any options specified that should be interpreted
163269    ** here. Options that are interpreted here include "vfs" and those that
163270    ** correspond to flags that may be passed to the sqlite3_open_v2()
163271    ** method. */
163272    zOpt = &zFile[sqlite3Strlen30(zFile)+1];
163273    while( zOpt[0] ){
163274      int nOpt = sqlite3Strlen30(zOpt);
163275      char *zVal = &zOpt[nOpt+1];
163276      int nVal = sqlite3Strlen30(zVal);
163277
163278      if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
163279        zVfs = zVal;
163280      }else{
163281        struct OpenMode {
163282          const char *z;
163283          int mode;
163284        } *aMode = 0;
163285        char *zModeType = 0;
163286        int mask = 0;
163287        int limit = 0;
163288
163289        if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
163290          static struct OpenMode aCacheMode[] = {
163291            { "shared",  SQLITE_OPEN_SHAREDCACHE },
163292            { "private", SQLITE_OPEN_PRIVATECACHE },
163293            { 0, 0 }
163294          };
163295
163296          mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
163297          aMode = aCacheMode;
163298          limit = mask;
163299          zModeType = "cache";
163300        }
163301        if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
163302          static struct OpenMode aOpenMode[] = {
163303            { "ro",  SQLITE_OPEN_READONLY },
163304            { "rw",  SQLITE_OPEN_READWRITE },
163305            { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
163306            { "memory", SQLITE_OPEN_MEMORY },
163307            { 0, 0 }
163308          };
163309
163310          mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
163311                   | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
163312          aMode = aOpenMode;
163313          limit = mask & flags;
163314          zModeType = "access";
163315        }
163316
163317        if( aMode ){
163318          int i;
163319          int mode = 0;
163320          for(i=0; aMode[i].z; i++){
163321            const char *z = aMode[i].z;
163322            if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
163323              mode = aMode[i].mode;
163324              break;
163325            }
163326          }
163327          if( mode==0 ){
163328            *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
163329            rc = SQLITE_ERROR;
163330            goto parse_uri_out;
163331          }
163332          if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){
163333            *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
163334                                        zModeType, zVal);
163335            rc = SQLITE_PERM;
163336            goto parse_uri_out;
163337          }
163338          flags = (flags & ~mask) | mode;
163339        }
163340      }
163341
163342      zOpt = &zVal[nVal+1];
163343    }
163344
163345  }else{
163346    zFile = sqlite3_malloc64(nUri+8);
163347    if( !zFile ) return SQLITE_NOMEM_BKPT;
163348    memset(zFile, 0, 4);
163349    zFile += 4;
163350    if( nUri ){
163351      memcpy(zFile, zUri, nUri);
163352    }
163353    memset(zFile+nUri, 0, 4);
163354    flags &= ~SQLITE_OPEN_URI;
163355  }
163356
163357  *ppVfs = sqlite3_vfs_find(zVfs);
163358  if( *ppVfs==0 ){
163359    *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
163360    rc = SQLITE_ERROR;
163361  }
163362 parse_uri_out:
163363  if( rc!=SQLITE_OK ){
163364    sqlite3_free_filename(zFile);
163365    zFile = 0;
163366  }
163367  *pFlags = flags;
163368  *pzFile = zFile;
163369  return rc;
163370}
163371
163372/*
163373** This routine does the core work of extracting URI parameters from a
163374** database filename for the sqlite3_uri_parameter() interface.
163375*/
163376static const char *uriParameter(const char *zFilename, const char *zParam){
163377  zFilename += sqlite3Strlen30(zFilename) + 1;
163378  while( zFilename[0] ){
163379    int x = strcmp(zFilename, zParam);
163380    zFilename += sqlite3Strlen30(zFilename) + 1;
163381    if( x==0 ) return zFilename;
163382    zFilename += sqlite3Strlen30(zFilename) + 1;
163383  }
163384  return 0;
163385}
163386
163387
163388
163389/*
163390** This routine does the work of opening a database on behalf of
163391** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
163392** is UTF-8 encoded.
163393*/
163394static int openDatabase(
163395  const char *zFilename, /* Database filename UTF-8 encoded */
163396  sqlite3 **ppDb,        /* OUT: Returned database handle */
163397  unsigned int flags,    /* Operational flags */
163398  const char *zVfs       /* Name of the VFS to use */
163399){
163400  sqlite3 *db;                    /* Store allocated handle here */
163401  int rc;                         /* Return code */
163402  int isThreadsafe;               /* True for threadsafe connections */
163403  char *zOpen = 0;                /* Filename argument to pass to BtreeOpen() */
163404  char *zErrMsg = 0;              /* Error message from sqlite3ParseUri() */
163405  int i;                          /* Loop counter */
163406
163407#ifdef SQLITE_ENABLE_API_ARMOR
163408  if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
163409#endif
163410  *ppDb = 0;
163411#ifndef SQLITE_OMIT_AUTOINIT
163412  rc = sqlite3_initialize();
163413  if( rc ) return rc;
163414#endif
163415
163416  if( sqlite3GlobalConfig.bCoreMutex==0 ){
163417    isThreadsafe = 0;
163418  }else if( flags & SQLITE_OPEN_NOMUTEX ){
163419    isThreadsafe = 0;
163420  }else if( flags & SQLITE_OPEN_FULLMUTEX ){
163421    isThreadsafe = 1;
163422  }else{
163423    isThreadsafe = sqlite3GlobalConfig.bFullMutex;
163424  }
163425
163426  if( flags & SQLITE_OPEN_PRIVATECACHE ){
163427    flags &= ~SQLITE_OPEN_SHAREDCACHE;
163428  }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
163429    flags |= SQLITE_OPEN_SHAREDCACHE;
163430  }
163431
163432  /* Remove harmful bits from the flags parameter
163433  **
163434  ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
163435  ** dealt with in the previous code block.  Besides these, the only
163436  ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
163437  ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
163438  ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits.  Silently mask
163439  ** off all other flags.
163440  */
163441  flags &=  ~( SQLITE_OPEN_DELETEONCLOSE |
163442               SQLITE_OPEN_EXCLUSIVE |
163443               SQLITE_OPEN_MAIN_DB |
163444               SQLITE_OPEN_TEMP_DB |
163445               SQLITE_OPEN_TRANSIENT_DB |
163446               SQLITE_OPEN_MAIN_JOURNAL |
163447               SQLITE_OPEN_TEMP_JOURNAL |
163448               SQLITE_OPEN_SUBJOURNAL |
163449               SQLITE_OPEN_MASTER_JOURNAL |
163450               SQLITE_OPEN_NOMUTEX |
163451               SQLITE_OPEN_FULLMUTEX |
163452               SQLITE_OPEN_WAL
163453             );
163454
163455  /* Allocate the sqlite data structure */
163456  db = sqlite3MallocZero( sizeof(sqlite3) );
163457  if( db==0 ) goto opendb_out;
163458  if( isThreadsafe
163459#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
163460   || sqlite3GlobalConfig.bCoreMutex
163461#endif
163462  ){
163463    db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
163464    if( db->mutex==0 ){
163465      sqlite3_free(db);
163466      db = 0;
163467      goto opendb_out;
163468    }
163469    if( isThreadsafe==0 ){
163470      sqlite3MutexWarnOnContention(db->mutex);
163471    }
163472  }
163473  sqlite3_mutex_enter(db->mutex);
163474  db->errMask = 0xff;
163475  db->nDb = 2;
163476  db->magic = SQLITE_MAGIC_BUSY;
163477  db->aDb = db->aDbStatic;
163478  db->lookaside.bDisable = 1;
163479  db->lookaside.sz = 0;
163480
163481  assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
163482  memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
163483  db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
163484  db->autoCommit = 1;
163485  db->nextAutovac = -1;
163486  db->szMmap = sqlite3GlobalConfig.szMmap;
163487  db->nextPagesize = 0;
163488  db->nMaxSorterMmap = 0x7FFFFFFF;
163489  db->flags |= SQLITE_ShortColNames
163490                 | SQLITE_EnableTrigger
163491                 | SQLITE_EnableView
163492                 | SQLITE_CacheSpill
163493#if !defined(SQLITE_TRUSTED_SCHEMA) || SQLITE_TRUSTED_SCHEMA+0!=0
163494                 | SQLITE_TrustedSchema
163495#endif
163496/* The SQLITE_DQS compile-time option determines the default settings
163497** for SQLITE_DBCONFIG_DQS_DDL and SQLITE_DBCONFIG_DQS_DML.
163498**
163499**    SQLITE_DQS     SQLITE_DBCONFIG_DQS_DDL    SQLITE_DBCONFIG_DQS_DML
163500**    ----------     -----------------------    -----------------------
163501**     undefined               on                          on
163502**         3                   on                          on
163503**         2                   on                         off
163504**         1                  off                          on
163505**         0                  off                         off
163506**
163507** Legacy behavior is 3 (double-quoted string literals are allowed anywhere)
163508** and so that is the default.  But developers are encouranged to use
163509** -DSQLITE_DQS=0 (best) or -DSQLITE_DQS=1 (second choice) if possible.
163510*/
163511#if !defined(SQLITE_DQS)
163512# define SQLITE_DQS 3
163513#endif
163514#if (SQLITE_DQS&1)==1
163515                 | SQLITE_DqsDML
163516#endif
163517#if (SQLITE_DQS&2)==2
163518                 | SQLITE_DqsDDL
163519#endif
163520
163521#if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
163522                 | SQLITE_AutoIndex
163523#endif
163524#if SQLITE_DEFAULT_CKPTFULLFSYNC
163525                 | SQLITE_CkptFullFSync
163526#endif
163527#if SQLITE_DEFAULT_FILE_FORMAT<4
163528                 | SQLITE_LegacyFileFmt
163529#endif
163530#ifdef SQLITE_ENABLE_LOAD_EXTENSION
163531                 | SQLITE_LoadExtension
163532#endif
163533#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
163534                 | SQLITE_RecTriggers
163535#endif
163536#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
163537                 | SQLITE_ForeignKeys
163538#endif
163539#if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
163540                 | SQLITE_ReverseOrder
163541#endif
163542#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
163543                 | SQLITE_CellSizeCk
163544#endif
163545#if defined(SQLITE_ENABLE_FTS3_TOKENIZER)
163546                 | SQLITE_Fts3Tokenizer
163547#endif
163548#if defined(SQLITE_ENABLE_QPSG)
163549                 | SQLITE_EnableQPSG
163550#endif
163551#if defined(SQLITE_DEFAULT_DEFENSIVE)
163552                 | SQLITE_Defensive
163553#endif
163554#if defined(SQLITE_DEFAULT_LEGACY_ALTER_TABLE)
163555                 | SQLITE_LegacyAlter
163556#endif
163557      ;
163558  sqlite3HashInit(&db->aCollSeq);
163559#ifndef SQLITE_OMIT_VIRTUALTABLE
163560  sqlite3HashInit(&db->aModule);
163561#endif
163562
163563  /* Add the default collation sequence BINARY. BINARY works for both UTF-8
163564  ** and UTF-16, so add a version for each to avoid any unnecessary
163565  ** conversions. The only error that can occur here is a malloc() failure.
163566  **
163567  ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
163568  ** functions:
163569  */
163570  createCollation(db, sqlite3StrBINARY, SQLITE_UTF8, 0, binCollFunc, 0);
163571  createCollation(db, sqlite3StrBINARY, SQLITE_UTF16BE, 0, binCollFunc, 0);
163572  createCollation(db, sqlite3StrBINARY, SQLITE_UTF16LE, 0, binCollFunc, 0);
163573  createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
163574  createCollation(db, "RTRIM", SQLITE_UTF8, 0, rtrimCollFunc, 0);
163575  if( db->mallocFailed ){
163576    goto opendb_out;
163577  }
163578
163579  /* Parse the filename/URI argument
163580  **
163581  ** Only allow sensible combinations of bits in the flags argument.
163582  ** Throw an error if any non-sense combination is used.  If we
163583  ** do not block illegal combinations here, it could trigger
163584  ** assert() statements in deeper layers.  Sensible combinations
163585  ** are:
163586  **
163587  **  1:  SQLITE_OPEN_READONLY
163588  **  2:  SQLITE_OPEN_READWRITE
163589  **  6:  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
163590  */
163591  db->openFlags = flags;
163592  assert( SQLITE_OPEN_READONLY  == 0x01 );
163593  assert( SQLITE_OPEN_READWRITE == 0x02 );
163594  assert( SQLITE_OPEN_CREATE    == 0x04 );
163595  testcase( (1<<(flags&7))==0x02 ); /* READONLY */
163596  testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
163597  testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
163598  if( ((1<<(flags&7)) & 0x46)==0 ){
163599    rc = SQLITE_MISUSE_BKPT;  /* IMP: R-18321-05872 */
163600  }else{
163601    rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
163602  }
163603  if( rc!=SQLITE_OK ){
163604    if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
163605    sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
163606    sqlite3_free(zErrMsg);
163607    goto opendb_out;
163608  }
163609
163610  /* Open the backend database driver */
163611  rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
163612                        flags | SQLITE_OPEN_MAIN_DB);
163613  if( rc!=SQLITE_OK ){
163614    if( rc==SQLITE_IOERR_NOMEM ){
163615      rc = SQLITE_NOMEM_BKPT;
163616    }
163617    sqlite3Error(db, rc);
163618    goto opendb_out;
163619  }
163620  sqlite3BtreeEnter(db->aDb[0].pBt);
163621  db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
163622  if( !db->mallocFailed ){
163623    sqlite3SetTextEncoding(db, SCHEMA_ENC(db));
163624  }
163625  sqlite3BtreeLeave(db->aDb[0].pBt);
163626  db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
163627
163628  /* The default safety_level for the main database is FULL; for the temp
163629  ** database it is OFF. This matches the pager layer defaults.
163630  */
163631  db->aDb[0].zDbSName = "main";
163632  db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
163633  db->aDb[1].zDbSName = "temp";
163634  db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF;
163635
163636  db->magic = SQLITE_MAGIC_OPEN;
163637  if( db->mallocFailed ){
163638    goto opendb_out;
163639  }
163640
163641  /* Register all built-in functions, but do not attempt to read the
163642  ** database schema yet. This is delayed until the first time the database
163643  ** is accessed.
163644  */
163645  sqlite3Error(db, SQLITE_OK);
163646  sqlite3RegisterPerConnectionBuiltinFunctions(db);
163647  rc = sqlite3_errcode(db);
163648
163649
163650  /* Load compiled-in extensions */
163651  for(i=0; rc==SQLITE_OK && i<ArraySize(sqlite3BuiltinExtensions); i++){
163652    rc = sqlite3BuiltinExtensions[i](db);
163653  }
163654
163655  /* Load automatic extensions - extensions that have been registered
163656  ** using the sqlite3_automatic_extension() API.
163657  */
163658  if( rc==SQLITE_OK ){
163659    sqlite3AutoLoadExtensions(db);
163660    rc = sqlite3_errcode(db);
163661    if( rc!=SQLITE_OK ){
163662      goto opendb_out;
163663    }
163664  }
163665
163666#ifdef SQLITE_ENABLE_INTERNAL_FUNCTIONS
163667  /* Testing use only!!! The -DSQLITE_ENABLE_INTERNAL_FUNCTIONS=1 compile-time
163668  ** option gives access to internal functions by default.
163669  ** Testing use only!!! */
163670  db->mDbFlags |= DBFLAG_InternalFunc;
163671#endif
163672
163673  /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
163674  ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
163675  ** mode.  Doing nothing at all also makes NORMAL the default.
163676  */
163677#ifdef SQLITE_DEFAULT_LOCKING_MODE
163678  db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
163679  sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
163680                          SQLITE_DEFAULT_LOCKING_MODE);
163681#endif
163682
163683  if( rc ) sqlite3Error(db, rc);
163684
163685  /* Enable the lookaside-malloc subsystem */
163686  setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
163687                        sqlite3GlobalConfig.nLookaside);
163688
163689  sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
163690
163691opendb_out:
163692  if( db ){
163693    assert( db->mutex!=0 || isThreadsafe==0
163694           || sqlite3GlobalConfig.bFullMutex==0 );
163695    sqlite3_mutex_leave(db->mutex);
163696  }
163697  rc = sqlite3_errcode(db);
163698  assert( db!=0 || rc==SQLITE_NOMEM );
163699  if( rc==SQLITE_NOMEM ){
163700    sqlite3_close(db);
163701    db = 0;
163702  }else if( rc!=SQLITE_OK ){
163703    db->magic = SQLITE_MAGIC_SICK;
163704  }
163705  *ppDb = db;
163706#ifdef SQLITE_ENABLE_SQLLOG
163707  if( sqlite3GlobalConfig.xSqllog ){
163708    /* Opening a db handle. Fourth parameter is passed 0. */
163709    void *pArg = sqlite3GlobalConfig.pSqllogArg;
163710    sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
163711  }
163712#endif
163713  sqlite3_free_filename(zOpen);
163714  return rc & 0xff;
163715}
163716
163717
163718/*
163719** Open a new database handle.
163720*/
163721SQLITE_API int sqlite3_open(
163722  const char *zFilename,
163723  sqlite3 **ppDb
163724){
163725  return openDatabase(zFilename, ppDb,
163726                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
163727}
163728SQLITE_API int sqlite3_open_v2(
163729  const char *filename,   /* Database filename (UTF-8) */
163730  sqlite3 **ppDb,         /* OUT: SQLite db handle */
163731  int flags,              /* Flags */
163732  const char *zVfs        /* Name of VFS module to use */
163733){
163734  return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
163735}
163736
163737#ifndef SQLITE_OMIT_UTF16
163738/*
163739** Open a new database handle.
163740*/
163741SQLITE_API int sqlite3_open16(
163742  const void *zFilename,
163743  sqlite3 **ppDb
163744){
163745  char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
163746  sqlite3_value *pVal;
163747  int rc;
163748
163749#ifdef SQLITE_ENABLE_API_ARMOR
163750  if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
163751#endif
163752  *ppDb = 0;
163753#ifndef SQLITE_OMIT_AUTOINIT
163754  rc = sqlite3_initialize();
163755  if( rc ) return rc;
163756#endif
163757  if( zFilename==0 ) zFilename = "\000\000";
163758  pVal = sqlite3ValueNew(0);
163759  sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
163760  zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
163761  if( zFilename8 ){
163762    rc = openDatabase(zFilename8, ppDb,
163763                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
163764    assert( *ppDb || rc==SQLITE_NOMEM );
163765    if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
163766      SCHEMA_ENC(*ppDb) = ENC(*ppDb) = SQLITE_UTF16NATIVE;
163767    }
163768  }else{
163769    rc = SQLITE_NOMEM_BKPT;
163770  }
163771  sqlite3ValueFree(pVal);
163772
163773  return rc & 0xff;
163774}
163775#endif /* SQLITE_OMIT_UTF16 */
163776
163777/*
163778** Register a new collation sequence with the database handle db.
163779*/
163780SQLITE_API int sqlite3_create_collation(
163781  sqlite3* db,
163782  const char *zName,
163783  int enc,
163784  void* pCtx,
163785  int(*xCompare)(void*,int,const void*,int,const void*)
163786){
163787  return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0);
163788}
163789
163790/*
163791** Register a new collation sequence with the database handle db.
163792*/
163793SQLITE_API int sqlite3_create_collation_v2(
163794  sqlite3* db,
163795  const char *zName,
163796  int enc,
163797  void* pCtx,
163798  int(*xCompare)(void*,int,const void*,int,const void*),
163799  void(*xDel)(void*)
163800){
163801  int rc;
163802
163803#ifdef SQLITE_ENABLE_API_ARMOR
163804  if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
163805#endif
163806  sqlite3_mutex_enter(db->mutex);
163807  assert( !db->mallocFailed );
163808  rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
163809  rc = sqlite3ApiExit(db, rc);
163810  sqlite3_mutex_leave(db->mutex);
163811  return rc;
163812}
163813
163814#ifndef SQLITE_OMIT_UTF16
163815/*
163816** Register a new collation sequence with the database handle db.
163817*/
163818SQLITE_API int sqlite3_create_collation16(
163819  sqlite3* db,
163820  const void *zName,
163821  int enc,
163822  void* pCtx,
163823  int(*xCompare)(void*,int,const void*,int,const void*)
163824){
163825  int rc = SQLITE_OK;
163826  char *zName8;
163827
163828#ifdef SQLITE_ENABLE_API_ARMOR
163829  if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
163830#endif
163831  sqlite3_mutex_enter(db->mutex);
163832  assert( !db->mallocFailed );
163833  zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
163834  if( zName8 ){
163835    rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
163836    sqlite3DbFree(db, zName8);
163837  }
163838  rc = sqlite3ApiExit(db, rc);
163839  sqlite3_mutex_leave(db->mutex);
163840  return rc;
163841}
163842#endif /* SQLITE_OMIT_UTF16 */
163843
163844/*
163845** Register a collation sequence factory callback with the database handle
163846** db. Replace any previously installed collation sequence factory.
163847*/
163848SQLITE_API int sqlite3_collation_needed(
163849  sqlite3 *db,
163850  void *pCollNeededArg,
163851  void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
163852){
163853#ifdef SQLITE_ENABLE_API_ARMOR
163854  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
163855#endif
163856  sqlite3_mutex_enter(db->mutex);
163857  db->xCollNeeded = xCollNeeded;
163858  db->xCollNeeded16 = 0;
163859  db->pCollNeededArg = pCollNeededArg;
163860  sqlite3_mutex_leave(db->mutex);
163861  return SQLITE_OK;
163862}
163863
163864#ifndef SQLITE_OMIT_UTF16
163865/*
163866** Register a collation sequence factory callback with the database handle
163867** db. Replace any previously installed collation sequence factory.
163868*/
163869SQLITE_API int sqlite3_collation_needed16(
163870  sqlite3 *db,
163871  void *pCollNeededArg,
163872  void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
163873){
163874#ifdef SQLITE_ENABLE_API_ARMOR
163875  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
163876#endif
163877  sqlite3_mutex_enter(db->mutex);
163878  db->xCollNeeded = 0;
163879  db->xCollNeeded16 = xCollNeeded16;
163880  db->pCollNeededArg = pCollNeededArg;
163881  sqlite3_mutex_leave(db->mutex);
163882  return SQLITE_OK;
163883}
163884#endif /* SQLITE_OMIT_UTF16 */
163885
163886#ifndef SQLITE_OMIT_DEPRECATED
163887/*
163888** This function is now an anachronism. It used to be used to recover from a
163889** malloc() failure, but SQLite now does this automatically.
163890*/
163891SQLITE_API int sqlite3_global_recover(void){
163892  return SQLITE_OK;
163893}
163894#endif
163895
163896/*
163897** Test to see whether or not the database connection is in autocommit
163898** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on
163899** by default.  Autocommit is disabled by a BEGIN statement and reenabled
163900** by the next COMMIT or ROLLBACK.
163901*/
163902SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
163903#ifdef SQLITE_ENABLE_API_ARMOR
163904  if( !sqlite3SafetyCheckOk(db) ){
163905    (void)SQLITE_MISUSE_BKPT;
163906    return 0;
163907  }
163908#endif
163909  return db->autoCommit;
163910}
163911
163912/*
163913** The following routines are substitutes for constants SQLITE_CORRUPT,
163914** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_NOMEM and possibly other error
163915** constants.  They serve two purposes:
163916**
163917**   1.  Serve as a convenient place to set a breakpoint in a debugger
163918**       to detect when version error conditions occurs.
163919**
163920**   2.  Invoke sqlite3_log() to provide the source code location where
163921**       a low-level error is first detected.
163922*/
163923SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType){
163924  sqlite3_log(iErr, "%s at line %d of [%.10s]",
163925              zType, lineno, 20+sqlite3_sourceid());
163926  return iErr;
163927}
163928SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
163929  testcase( sqlite3GlobalConfig.xLog!=0 );
163930  return sqlite3ReportError(SQLITE_CORRUPT, lineno, "database corruption");
163931}
163932SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
163933  testcase( sqlite3GlobalConfig.xLog!=0 );
163934  return sqlite3ReportError(SQLITE_MISUSE, lineno, "misuse");
163935}
163936SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
163937  testcase( sqlite3GlobalConfig.xLog!=0 );
163938  return sqlite3ReportError(SQLITE_CANTOPEN, lineno, "cannot open file");
163939}
163940#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
163941SQLITE_PRIVATE int sqlite3CorruptPgnoError(int lineno, Pgno pgno){
163942  char zMsg[100];
163943  sqlite3_snprintf(sizeof(zMsg), zMsg, "database corruption page %d", pgno);
163944  testcase( sqlite3GlobalConfig.xLog!=0 );
163945  return sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
163946}
163947#endif
163948#ifdef SQLITE_DEBUG
163949SQLITE_PRIVATE int sqlite3NomemError(int lineno){
163950  testcase( sqlite3GlobalConfig.xLog!=0 );
163951  return sqlite3ReportError(SQLITE_NOMEM, lineno, "OOM");
163952}
163953SQLITE_PRIVATE int sqlite3IoerrnomemError(int lineno){
163954  testcase( sqlite3GlobalConfig.xLog!=0 );
163955  return sqlite3ReportError(SQLITE_IOERR_NOMEM, lineno, "I/O OOM error");
163956}
163957#endif
163958
163959#ifndef SQLITE_OMIT_DEPRECATED
163960/*
163961** This is a convenience routine that makes sure that all thread-specific
163962** data for this thread has been deallocated.
163963**
163964** SQLite no longer uses thread-specific data so this routine is now a
163965** no-op.  It is retained for historical compatibility.
163966*/
163967SQLITE_API void sqlite3_thread_cleanup(void){
163968}
163969#endif
163970
163971/*
163972** Return meta information about a specific column of a database table.
163973** See comment in sqlite3.h (sqlite.h.in) for details.
163974*/
163975SQLITE_API int sqlite3_table_column_metadata(
163976  sqlite3 *db,                /* Connection handle */
163977  const char *zDbName,        /* Database name or NULL */
163978  const char *zTableName,     /* Table name */
163979  const char *zColumnName,    /* Column name */
163980  char const **pzDataType,    /* OUTPUT: Declared data type */
163981  char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
163982  int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
163983  int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
163984  int *pAutoinc               /* OUTPUT: True if column is auto-increment */
163985){
163986  int rc;
163987  char *zErrMsg = 0;
163988  Table *pTab = 0;
163989  Column *pCol = 0;
163990  int iCol = 0;
163991  char const *zDataType = 0;
163992  char const *zCollSeq = 0;
163993  int notnull = 0;
163994  int primarykey = 0;
163995  int autoinc = 0;
163996
163997
163998#ifdef SQLITE_ENABLE_API_ARMOR
163999  if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){
164000    return SQLITE_MISUSE_BKPT;
164001  }
164002#endif
164003
164004  /* Ensure the database schema has been loaded */
164005  sqlite3_mutex_enter(db->mutex);
164006  sqlite3BtreeEnterAll(db);
164007  rc = sqlite3Init(db, &zErrMsg);
164008  if( SQLITE_OK!=rc ){
164009    goto error_out;
164010  }
164011
164012  /* Locate the table in question */
164013  pTab = sqlite3FindTable(db, zTableName, zDbName);
164014  if( !pTab || pTab->pSelect ){
164015    pTab = 0;
164016    goto error_out;
164017  }
164018
164019  /* Find the column for which info is requested */
164020  if( zColumnName==0 ){
164021    /* Query for existance of table only */
164022  }else{
164023    for(iCol=0; iCol<pTab->nCol; iCol++){
164024      pCol = &pTab->aCol[iCol];
164025      if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
164026        break;
164027      }
164028    }
164029    if( iCol==pTab->nCol ){
164030      if( HasRowid(pTab) && sqlite3IsRowid(zColumnName) ){
164031        iCol = pTab->iPKey;
164032        pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
164033      }else{
164034        pTab = 0;
164035        goto error_out;
164036      }
164037    }
164038  }
164039
164040  /* The following block stores the meta information that will be returned
164041  ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
164042  ** and autoinc. At this point there are two possibilities:
164043  **
164044  **     1. The specified column name was rowid", "oid" or "_rowid_"
164045  **        and there is no explicitly declared IPK column.
164046  **
164047  **     2. The table is not a view and the column name identified an
164048  **        explicitly declared column. Copy meta information from *pCol.
164049  */
164050  if( pCol ){
164051    zDataType = sqlite3ColumnType(pCol,0);
164052    zCollSeq = pCol->zColl;
164053    notnull = pCol->notNull!=0;
164054    primarykey  = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
164055    autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
164056  }else{
164057    zDataType = "INTEGER";
164058    primarykey = 1;
164059  }
164060  if( !zCollSeq ){
164061    zCollSeq = sqlite3StrBINARY;
164062  }
164063
164064error_out:
164065  sqlite3BtreeLeaveAll(db);
164066
164067  /* Whether the function call succeeded or failed, set the output parameters
164068  ** to whatever their local counterparts contain. If an error did occur,
164069  ** this has the effect of zeroing all output parameters.
164070  */
164071  if( pzDataType ) *pzDataType = zDataType;
164072  if( pzCollSeq ) *pzCollSeq = zCollSeq;
164073  if( pNotNull ) *pNotNull = notnull;
164074  if( pPrimaryKey ) *pPrimaryKey = primarykey;
164075  if( pAutoinc ) *pAutoinc = autoinc;
164076
164077  if( SQLITE_OK==rc && !pTab ){
164078    sqlite3DbFree(db, zErrMsg);
164079    zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
164080        zColumnName);
164081    rc = SQLITE_ERROR;
164082  }
164083  sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg);
164084  sqlite3DbFree(db, zErrMsg);
164085  rc = sqlite3ApiExit(db, rc);
164086  sqlite3_mutex_leave(db->mutex);
164087  return rc;
164088}
164089
164090/*
164091** Sleep for a little while.  Return the amount of time slept.
164092*/
164093SQLITE_API int sqlite3_sleep(int ms){
164094  sqlite3_vfs *pVfs;
164095  int rc;
164096  pVfs = sqlite3_vfs_find(0);
164097  if( pVfs==0 ) return 0;
164098
164099  /* This function works in milliseconds, but the underlying OsSleep()
164100  ** API uses microseconds. Hence the 1000's.
164101  */
164102  rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
164103  return rc;
164104}
164105
164106/*
164107** Enable or disable the extended result codes.
164108*/
164109SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
164110#ifdef SQLITE_ENABLE_API_ARMOR
164111  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
164112#endif
164113  sqlite3_mutex_enter(db->mutex);
164114  db->errMask = onoff ? 0xffffffff : 0xff;
164115  sqlite3_mutex_leave(db->mutex);
164116  return SQLITE_OK;
164117}
164118
164119/*
164120** Invoke the xFileControl method on a particular database.
164121*/
164122SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
164123  int rc = SQLITE_ERROR;
164124  Btree *pBtree;
164125
164126#ifdef SQLITE_ENABLE_API_ARMOR
164127  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
164128#endif
164129  sqlite3_mutex_enter(db->mutex);
164130  pBtree = sqlite3DbNameToBtree(db, zDbName);
164131  if( pBtree ){
164132    Pager *pPager;
164133    sqlite3_file *fd;
164134    sqlite3BtreeEnter(pBtree);
164135    pPager = sqlite3BtreePager(pBtree);
164136    assert( pPager!=0 );
164137    fd = sqlite3PagerFile(pPager);
164138    assert( fd!=0 );
164139    if( op==SQLITE_FCNTL_FILE_POINTER ){
164140      *(sqlite3_file**)pArg = fd;
164141      rc = SQLITE_OK;
164142    }else if( op==SQLITE_FCNTL_VFS_POINTER ){
164143      *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager);
164144      rc = SQLITE_OK;
164145    }else if( op==SQLITE_FCNTL_JOURNAL_POINTER ){
164146      *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager);
164147      rc = SQLITE_OK;
164148    }else if( op==SQLITE_FCNTL_DATA_VERSION ){
164149      *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager);
164150      rc = SQLITE_OK;
164151    }else if( op==SQLITE_FCNTL_RESERVE_BYTES ){
164152      int iNew = *(int*)pArg;
164153      *(int*)pArg = sqlite3BtreeGetRequestedReserve(pBtree);
164154      if( iNew>=0 && iNew<=255 ){
164155        sqlite3BtreeSetPageSize(pBtree, 0, iNew, 0);
164156      }
164157      rc = SQLITE_OK;
164158    }else{
164159      rc = sqlite3OsFileControl(fd, op, pArg);
164160    }
164161    sqlite3BtreeLeave(pBtree);
164162  }
164163  sqlite3_mutex_leave(db->mutex);
164164  return rc;
164165}
164166
164167/*
164168** Interface to the testing logic.
164169*/
164170SQLITE_API int sqlite3_test_control(int op, ...){
164171  int rc = 0;
164172#ifdef SQLITE_UNTESTABLE
164173  UNUSED_PARAMETER(op);
164174#else
164175  va_list ap;
164176  va_start(ap, op);
164177  switch( op ){
164178
164179    /*
164180    ** Save the current state of the PRNG.
164181    */
164182    case SQLITE_TESTCTRL_PRNG_SAVE: {
164183      sqlite3PrngSaveState();
164184      break;
164185    }
164186
164187    /*
164188    ** Restore the state of the PRNG to the last state saved using
164189    ** PRNG_SAVE.  If PRNG_SAVE has never before been called, then
164190    ** this verb acts like PRNG_RESET.
164191    */
164192    case SQLITE_TESTCTRL_PRNG_RESTORE: {
164193      sqlite3PrngRestoreState();
164194      break;
164195    }
164196
164197    /*  sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, int x, sqlite3 *db);
164198    **
164199    ** Control the seed for the pseudo-random number generator (PRNG) that
164200    ** is built into SQLite.  Cases:
164201    **
164202    **    x!=0 && db!=0       Seed the PRNG to the current value of the
164203    **                        schema cookie in the main database for db, or
164204    **                        x if the schema cookie is zero.  This case
164205    **                        is convenient to use with database fuzzers
164206    **                        as it allows the fuzzer some control over the
164207    **                        the PRNG seed.
164208    **
164209    **    x!=0 && db==0       Seed the PRNG to the value of x.
164210    **
164211    **    x==0 && db==0       Revert to default behavior of using the
164212    **                        xRandomness method on the primary VFS.
164213    **
164214    ** This test-control also resets the PRNG so that the new seed will
164215    ** be used for the next call to sqlite3_randomness().
164216    */
164217#ifndef SQLITE_OMIT_WSD
164218    case SQLITE_TESTCTRL_PRNG_SEED: {
164219      int x = va_arg(ap, int);
164220      int y;
164221      sqlite3 *db = va_arg(ap, sqlite3*);
164222      assert( db==0 || db->aDb[0].pSchema!=0 );
164223      if( db && (y = db->aDb[0].pSchema->schema_cookie)!=0 ){ x = y; }
164224      sqlite3Config.iPrngSeed = x;
164225      sqlite3_randomness(0,0);
164226      break;
164227    }
164228#endif
164229
164230    /*
164231    **  sqlite3_test_control(BITVEC_TEST, size, program)
164232    **
164233    ** Run a test against a Bitvec object of size.  The program argument
164234    ** is an array of integers that defines the test.  Return -1 on a
164235    ** memory allocation error, 0 on success, or non-zero for an error.
164236    ** See the sqlite3BitvecBuiltinTest() for additional information.
164237    */
164238    case SQLITE_TESTCTRL_BITVEC_TEST: {
164239      int sz = va_arg(ap, int);
164240      int *aProg = va_arg(ap, int*);
164241      rc = sqlite3BitvecBuiltinTest(sz, aProg);
164242      break;
164243    }
164244
164245    /*
164246    **  sqlite3_test_control(FAULT_INSTALL, xCallback)
164247    **
164248    ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
164249    ** if xCallback is not NULL.
164250    **
164251    ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
164252    ** is called immediately after installing the new callback and the return
164253    ** value from sqlite3FaultSim(0) becomes the return from
164254    ** sqlite3_test_control().
164255    */
164256    case SQLITE_TESTCTRL_FAULT_INSTALL: {
164257      /* MSVC is picky about pulling func ptrs from va lists.
164258      ** http://support.microsoft.com/kb/47961
164259      ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int));
164260      */
164261      typedef int(*TESTCALLBACKFUNC_t)(int);
164262      sqlite3GlobalConfig.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t);
164263      rc = sqlite3FaultSim(0);
164264      break;
164265    }
164266
164267    /*
164268    **  sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
164269    **
164270    ** Register hooks to call to indicate which malloc() failures
164271    ** are benign.
164272    */
164273    case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
164274      typedef void (*void_function)(void);
164275      void_function xBenignBegin;
164276      void_function xBenignEnd;
164277      xBenignBegin = va_arg(ap, void_function);
164278      xBenignEnd = va_arg(ap, void_function);
164279      sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
164280      break;
164281    }
164282
164283    /*
164284    **  sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
164285    **
164286    ** Set the PENDING byte to the value in the argument, if X>0.
164287    ** Make no changes if X==0.  Return the value of the pending byte
164288    ** as it existing before this routine was called.
164289    **
164290    ** IMPORTANT:  Changing the PENDING byte from 0x40000000 results in
164291    ** an incompatible database file format.  Changing the PENDING byte
164292    ** while any database connection is open results in undefined and
164293    ** deleterious behavior.
164294    */
164295    case SQLITE_TESTCTRL_PENDING_BYTE: {
164296      rc = PENDING_BYTE;
164297#ifndef SQLITE_OMIT_WSD
164298      {
164299        unsigned int newVal = va_arg(ap, unsigned int);
164300        if( newVal ) sqlite3PendingByte = newVal;
164301      }
164302#endif
164303      break;
164304    }
164305
164306    /*
164307    **  sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
164308    **
164309    ** This action provides a run-time test to see whether or not
164310    ** assert() was enabled at compile-time.  If X is true and assert()
164311    ** is enabled, then the return value is true.  If X is true and
164312    ** assert() is disabled, then the return value is zero.  If X is
164313    ** false and assert() is enabled, then the assertion fires and the
164314    ** process aborts.  If X is false and assert() is disabled, then the
164315    ** return value is zero.
164316    */
164317    case SQLITE_TESTCTRL_ASSERT: {
164318      volatile int x = 0;
164319      assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 );
164320      rc = x;
164321      break;
164322    }
164323
164324
164325    /*
164326    **  sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
164327    **
164328    ** This action provides a run-time test to see how the ALWAYS and
164329    ** NEVER macros were defined at compile-time.
164330    **
164331    ** The return value is ALWAYS(X) if X is true, or 0 if X is false.
164332    **
164333    ** The recommended test is X==2.  If the return value is 2, that means
164334    ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
164335    ** default setting.  If the return value is 1, then ALWAYS() is either
164336    ** hard-coded to true or else it asserts if its argument is false.
164337    ** The first behavior (hard-coded to true) is the case if
164338    ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
164339    ** behavior (assert if the argument to ALWAYS() is false) is the case if
164340    ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
164341    **
164342    ** The run-time test procedure might look something like this:
164343    **
164344    **    if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
164345    **      // ALWAYS() and NEVER() are no-op pass-through macros
164346    **    }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
164347    **      // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
164348    **    }else{
164349    **      // ALWAYS(x) is a constant 1.  NEVER(x) is a constant 0.
164350    **    }
164351    */
164352    case SQLITE_TESTCTRL_ALWAYS: {
164353      int x = va_arg(ap,int);
164354      rc = x ? ALWAYS(x) : 0;
164355      break;
164356    }
164357
164358    /*
164359    **   sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
164360    **
164361    ** The integer returned reveals the byte-order of the computer on which
164362    ** SQLite is running:
164363    **
164364    **       1     big-endian,    determined at run-time
164365    **      10     little-endian, determined at run-time
164366    **  432101     big-endian,    determined at compile-time
164367    **  123410     little-endian, determined at compile-time
164368    */
164369    case SQLITE_TESTCTRL_BYTEORDER: {
164370      rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN;
164371      break;
164372    }
164373
164374    /*  sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
164375    **
164376    ** Enable or disable various optimizations for testing purposes.  The
164377    ** argument N is a bitmask of optimizations to be disabled.  For normal
164378    ** operation N should be 0.  The idea is that a test program (like the
164379    ** SQL Logic Test or SLT test module) can run the same SQL multiple times
164380    ** with various optimizations disabled to verify that the same answer
164381    ** is obtained in every case.
164382    */
164383    case SQLITE_TESTCTRL_OPTIMIZATIONS: {
164384      sqlite3 *db = va_arg(ap, sqlite3*);
164385      db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
164386      break;
164387    }
164388
164389    /*   sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
164390    **
164391    ** If parameter onoff is non-zero, subsequent calls to localtime()
164392    ** and its variants fail. If onoff is zero, undo this setting.
164393    */
164394    case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
164395      sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
164396      break;
164397    }
164398
164399    /*   sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, sqlite3*);
164400    **
164401    ** Toggle the ability to use internal functions on or off for
164402    ** the database connection given in the argument.
164403    */
164404    case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: {
164405      sqlite3 *db = va_arg(ap, sqlite3*);
164406      db->mDbFlags ^= DBFLAG_InternalFunc;
164407      break;
164408    }
164409
164410    /*   sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
164411    **
164412    ** Set or clear a flag that indicates that the database file is always well-
164413    ** formed and never corrupt.  This flag is clear by default, indicating that
164414    ** database files might have arbitrary corruption.  Setting the flag during
164415    ** testing causes certain assert() statements in the code to be activated
164416    ** that demonstrat invariants on well-formed database files.
164417    */
164418    case SQLITE_TESTCTRL_NEVER_CORRUPT: {
164419      sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int);
164420      break;
164421    }
164422
164423    /*   sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, int);
164424    **
164425    ** Set or clear a flag that causes SQLite to verify that type, name,
164426    ** and tbl_name fields of the sqlite_master table.  This is normally
164427    ** on, but it is sometimes useful to turn it off for testing.
164428    */
164429    case SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS: {
164430      sqlite3GlobalConfig.bExtraSchemaChecks = va_arg(ap, int);
164431      break;
164432    }
164433
164434    /* Set the threshold at which OP_Once counters reset back to zero.
164435    ** By default this is 0x7ffffffe (over 2 billion), but that value is
164436    ** too big to test in a reasonable amount of time, so this control is
164437    ** provided to set a small and easily reachable reset value.
164438    */
164439    case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD: {
164440      sqlite3GlobalConfig.iOnceResetThreshold = va_arg(ap, int);
164441      break;
164442    }
164443
164444    /*   sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
164445    **
164446    ** Set the VDBE coverage callback function to xCallback with context
164447    ** pointer ptr.
164448    */
164449    case SQLITE_TESTCTRL_VDBE_COVERAGE: {
164450#ifdef SQLITE_VDBE_COVERAGE
164451      typedef void (*branch_callback)(void*,unsigned int,
164452                                      unsigned char,unsigned char);
164453      sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback);
164454      sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
164455#endif
164456      break;
164457    }
164458
164459    /*   sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */
164460    case SQLITE_TESTCTRL_SORTER_MMAP: {
164461      sqlite3 *db = va_arg(ap, sqlite3*);
164462      db->nMaxSorterMmap = va_arg(ap, int);
164463      break;
164464    }
164465
164466    /*   sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
164467    **
164468    ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
164469    ** not.
164470    */
164471    case SQLITE_TESTCTRL_ISINIT: {
164472      if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR;
164473      break;
164474    }
164475
164476    /*  sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum);
164477    **
164478    ** This test control is used to create imposter tables.  "db" is a pointer
164479    ** to the database connection.  dbName is the database name (ex: "main" or
164480    ** "temp") which will receive the imposter.  "onOff" turns imposter mode on
164481    ** or off.  "tnum" is the root page of the b-tree to which the imposter
164482    ** table should connect.
164483    **
164484    ** Enable imposter mode only when the schema has already been parsed.  Then
164485    ** run a single CREATE TABLE statement to construct the imposter table in
164486    ** the parsed schema.  Then turn imposter mode back off again.
164487    **
164488    ** If onOff==0 and tnum>0 then reset the schema for all databases, causing
164489    ** the schema to be reparsed the next time it is needed.  This has the
164490    ** effect of erasing all imposter tables.
164491    */
164492    case SQLITE_TESTCTRL_IMPOSTER: {
164493      sqlite3 *db = va_arg(ap, sqlite3*);
164494      sqlite3_mutex_enter(db->mutex);
164495      db->init.iDb = sqlite3FindDbName(db, va_arg(ap,const char*));
164496      db->init.busy = db->init.imposterTable = va_arg(ap,int);
164497      db->init.newTnum = va_arg(ap,int);
164498      if( db->init.busy==0 && db->init.newTnum>0 ){
164499        sqlite3ResetAllSchemasOfConnection(db);
164500      }
164501      sqlite3_mutex_leave(db->mutex);
164502      break;
164503    }
164504
164505#if defined(YYCOVERAGE)
164506    /*  sqlite3_test_control(SQLITE_TESTCTRL_PARSER_COVERAGE, FILE *out)
164507    **
164508    ** This test control (only available when SQLite is compiled with
164509    ** -DYYCOVERAGE) writes a report onto "out" that shows all
164510    ** state/lookahead combinations in the parser state machine
164511    ** which are never exercised.  If any state is missed, make the
164512    ** return code SQLITE_ERROR.
164513    */
164514    case SQLITE_TESTCTRL_PARSER_COVERAGE: {
164515      FILE *out = va_arg(ap, FILE*);
164516      if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR;
164517      break;
164518    }
164519#endif /* defined(YYCOVERAGE) */
164520
164521    /*  sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, sqlite3_context*);
164522    **
164523    ** This test-control causes the most recent sqlite3_result_int64() value
164524    ** to be interpreted as a MEM_IntReal instead of as an MEM_Int.  Normally,
164525    ** MEM_IntReal values only arise during an INSERT operation of integer
164526    ** values into a REAL column, so they can be challenging to test.  This
164527    ** test-control enables us to write an intreal() SQL function that can
164528    ** inject an intreal() value at arbitrary places in an SQL statement,
164529    ** for testing purposes.
164530    */
164531    case SQLITE_TESTCTRL_RESULT_INTREAL: {
164532      sqlite3_context *pCtx = va_arg(ap, sqlite3_context*);
164533      sqlite3ResultIntReal(pCtx);
164534      break;
164535    }
164536  }
164537  va_end(ap);
164538#endif /* SQLITE_UNTESTABLE */
164539  return rc;
164540}
164541
164542/*
164543** The Pager stores the Database filename, Journal filename, and WAL filename
164544** consecutively in memory, in that order.  The database filename is prefixed
164545** by four zero bytes.  Locate the start of the database filename by searching
164546** backwards for the first byte following four consecutive zero bytes.
164547**
164548** This only works if the filename passed in was obtained from the Pager.
164549*/
164550static const char *databaseName(const char *zName){
164551  while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
164552    zName--;
164553  }
164554  return zName;
164555}
164556
164557/*
164558** Append text z[] to the end of p[].  Return a pointer to the first
164559** character after then zero terminator on the new text in p[].
164560*/
164561static char *appendText(char *p, const char *z){
164562  size_t n = strlen(z);
164563  memcpy(p, z, n+1);
164564  return p+n+1;
164565}
164566
164567/*
164568** Allocate memory to hold names for a database, journal file, WAL file,
164569** and query parameters.  The pointer returned is valid for use by
164570** sqlite3_filename_database() and sqlite3_uri_parameter() and related
164571** functions.
164572**
164573** Memory layout must be compatible with that generated by the pager
164574** and expected by sqlite3_uri_parameter() and databaseName().
164575*/
164576SQLITE_API char *sqlite3_create_filename(
164577  const char *zDatabase,
164578  const char *zJournal,
164579  const char *zWal,
164580  int nParam,
164581  const char **azParam
164582){
164583  sqlite3_int64 nByte;
164584  int i;
164585  char *pResult, *p;
164586  nByte = strlen(zDatabase) + strlen(zJournal) + strlen(zWal) + 10;
164587  for(i=0; i<nParam*2; i++){
164588    nByte += strlen(azParam[i])+1;
164589  }
164590  pResult = p = sqlite3_malloc64( nByte );
164591  if( p==0 ) return 0;
164592  memset(p, 0, 4);
164593  p += 4;
164594  p = appendText(p, zDatabase);
164595  for(i=0; i<nParam*2; i++){
164596    p = appendText(p, azParam[i]);
164597  }
164598  *(p++) = 0;
164599  p = appendText(p, zJournal);
164600  p = appendText(p, zWal);
164601  *(p++) = 0;
164602  *(p++) = 0;
164603  assert( (sqlite3_int64)(p - pResult)==nByte );
164604  return pResult + 4;
164605}
164606
164607/*
164608** Free memory obtained from sqlite3_create_filename().  It is a severe
164609** error to call this routine with any parameter other than a pointer
164610** previously obtained from sqlite3_create_filename() or a NULL pointer.
164611*/
164612SQLITE_API void sqlite3_free_filename(char *p){
164613  if( p==0 ) return;
164614  p = (char*)databaseName(p);
164615  sqlite3_free(p - 4);
164616}
164617
164618
164619/*
164620** This is a utility routine, useful to VFS implementations, that checks
164621** to see if a database file was a URI that contained a specific query
164622** parameter, and if so obtains the value of the query parameter.
164623**
164624** The zFilename argument is the filename pointer passed into the xOpen()
164625** method of a VFS implementation.  The zParam argument is the name of the
164626** query parameter we seek.  This routine returns the value of the zParam
164627** parameter if it exists.  If the parameter does not exist, this routine
164628** returns a NULL pointer.
164629*/
164630SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
164631  if( zFilename==0 || zParam==0 ) return 0;
164632  zFilename = databaseName(zFilename);
164633  return uriParameter(zFilename, zParam);
164634}
164635
164636/*
164637** Return a pointer to the name of Nth query parameter of the filename.
164638*/
164639SQLITE_API const char *sqlite3_uri_key(const char *zFilename, int N){
164640  if( zFilename==0 || N<0 ) return 0;
164641  zFilename = databaseName(zFilename);
164642  zFilename += sqlite3Strlen30(zFilename) + 1;
164643  while( zFilename[0] && (N--)>0 ){
164644    zFilename += sqlite3Strlen30(zFilename) + 1;
164645    zFilename += sqlite3Strlen30(zFilename) + 1;
164646  }
164647  return zFilename[0] ? zFilename : 0;
164648}
164649
164650/*
164651** Return a boolean value for a query parameter.
164652*/
164653SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
164654  const char *z = sqlite3_uri_parameter(zFilename, zParam);
164655  bDflt = bDflt!=0;
164656  return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
164657}
164658
164659/*
164660** Return a 64-bit integer value for a query parameter.
164661*/
164662SQLITE_API sqlite3_int64 sqlite3_uri_int64(
164663  const char *zFilename,    /* Filename as passed to xOpen */
164664  const char *zParam,       /* URI parameter sought */
164665  sqlite3_int64 bDflt       /* return if parameter is missing */
164666){
164667  const char *z = sqlite3_uri_parameter(zFilename, zParam);
164668  sqlite3_int64 v;
164669  if( z && sqlite3DecOrHexToI64(z, &v)==0 ){
164670    bDflt = v;
164671  }
164672  return bDflt;
164673}
164674
164675/*
164676** Translate a filename that was handed to a VFS routine into the corresponding
164677** database, journal, or WAL file.
164678**
164679** It is an error to pass this routine a filename string that was not
164680** passed into the VFS from the SQLite core.  Doing so is similar to
164681** passing free() a pointer that was not obtained from malloc() - it is
164682** an error that we cannot easily detect but that will likely cause memory
164683** corruption.
164684*/
164685SQLITE_API const char *sqlite3_filename_database(const char *zFilename){
164686  return databaseName(zFilename);
164687}
164688SQLITE_API const char *sqlite3_filename_journal(const char *zFilename){
164689  zFilename = databaseName(zFilename);
164690  zFilename += sqlite3Strlen30(zFilename) + 1;
164691  while( zFilename[0] ){
164692    zFilename += sqlite3Strlen30(zFilename) + 1;
164693    zFilename += sqlite3Strlen30(zFilename) + 1;
164694  }
164695  return zFilename + 1;
164696}
164697SQLITE_API const char *sqlite3_filename_wal(const char *zFilename){
164698#ifdef SQLITE_OMIT_WAL
164699  return 0;
164700#else
164701  zFilename = sqlite3_filename_journal(zFilename);
164702  zFilename += sqlite3Strlen30(zFilename) + 1;
164703  return zFilename;
164704#endif
164705}
164706
164707/*
164708** Return the Btree pointer identified by zDbName.  Return NULL if not found.
164709*/
164710SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
164711  int iDb = zDbName ? sqlite3FindDbName(db, zDbName) : 0;
164712  return iDb<0 ? 0 : db->aDb[iDb].pBt;
164713}
164714
164715/*
164716** Return the filename of the database associated with a database
164717** connection.
164718*/
164719SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
164720  Btree *pBt;
164721#ifdef SQLITE_ENABLE_API_ARMOR
164722  if( !sqlite3SafetyCheckOk(db) ){
164723    (void)SQLITE_MISUSE_BKPT;
164724    return 0;
164725  }
164726#endif
164727  pBt = sqlite3DbNameToBtree(db, zDbName);
164728  return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
164729}
164730
164731/*
164732** Return 1 if database is read-only or 0 if read/write.  Return -1 if
164733** no such database exists.
164734*/
164735SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
164736  Btree *pBt;
164737#ifdef SQLITE_ENABLE_API_ARMOR
164738  if( !sqlite3SafetyCheckOk(db) ){
164739    (void)SQLITE_MISUSE_BKPT;
164740    return -1;
164741  }
164742#endif
164743  pBt = sqlite3DbNameToBtree(db, zDbName);
164744  return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
164745}
164746
164747#ifdef SQLITE_ENABLE_SNAPSHOT
164748/*
164749** Obtain a snapshot handle for the snapshot of database zDb currently
164750** being read by handle db.
164751*/
164752SQLITE_API int sqlite3_snapshot_get(
164753  sqlite3 *db,
164754  const char *zDb,
164755  sqlite3_snapshot **ppSnapshot
164756){
164757  int rc = SQLITE_ERROR;
164758#ifndef SQLITE_OMIT_WAL
164759
164760#ifdef SQLITE_ENABLE_API_ARMOR
164761  if( !sqlite3SafetyCheckOk(db) ){
164762    return SQLITE_MISUSE_BKPT;
164763  }
164764#endif
164765  sqlite3_mutex_enter(db->mutex);
164766
164767  if( db->autoCommit==0 ){
164768    int iDb = sqlite3FindDbName(db, zDb);
164769    if( iDb==0 || iDb>1 ){
164770      Btree *pBt = db->aDb[iDb].pBt;
164771      if( 0==sqlite3BtreeIsInTrans(pBt) ){
164772        rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
164773        if( rc==SQLITE_OK ){
164774          rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot);
164775        }
164776      }
164777    }
164778  }
164779
164780  sqlite3_mutex_leave(db->mutex);
164781#endif   /* SQLITE_OMIT_WAL */
164782  return rc;
164783}
164784
164785/*
164786** Open a read-transaction on the snapshot idendified by pSnapshot.
164787*/
164788SQLITE_API int sqlite3_snapshot_open(
164789  sqlite3 *db,
164790  const char *zDb,
164791  sqlite3_snapshot *pSnapshot
164792){
164793  int rc = SQLITE_ERROR;
164794#ifndef SQLITE_OMIT_WAL
164795
164796#ifdef SQLITE_ENABLE_API_ARMOR
164797  if( !sqlite3SafetyCheckOk(db) ){
164798    return SQLITE_MISUSE_BKPT;
164799  }
164800#endif
164801  sqlite3_mutex_enter(db->mutex);
164802  if( db->autoCommit==0 ){
164803    int iDb;
164804    iDb = sqlite3FindDbName(db, zDb);
164805    if( iDb==0 || iDb>1 ){
164806      Btree *pBt = db->aDb[iDb].pBt;
164807      if( sqlite3BtreeIsInTrans(pBt)==0 ){
164808        Pager *pPager = sqlite3BtreePager(pBt);
164809        int bUnlock = 0;
164810        if( sqlite3BtreeIsInReadTrans(pBt) ){
164811          if( db->nVdbeActive==0 ){
164812            rc = sqlite3PagerSnapshotCheck(pPager, pSnapshot);
164813            if( rc==SQLITE_OK ){
164814              bUnlock = 1;
164815              rc = sqlite3BtreeCommit(pBt);
164816            }
164817          }
164818        }else{
164819          rc = SQLITE_OK;
164820        }
164821        if( rc==SQLITE_OK ){
164822          rc = sqlite3PagerSnapshotOpen(pPager, pSnapshot);
164823        }
164824        if( rc==SQLITE_OK ){
164825          rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
164826          sqlite3PagerSnapshotOpen(pPager, 0);
164827        }
164828        if( bUnlock ){
164829          sqlite3PagerSnapshotUnlock(pPager);
164830        }
164831      }
164832    }
164833  }
164834
164835  sqlite3_mutex_leave(db->mutex);
164836#endif   /* SQLITE_OMIT_WAL */
164837  return rc;
164838}
164839
164840/*
164841** Recover as many snapshots as possible from the wal file associated with
164842** schema zDb of database db.
164843*/
164844SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
164845  int rc = SQLITE_ERROR;
164846  int iDb;
164847#ifndef SQLITE_OMIT_WAL
164848
164849#ifdef SQLITE_ENABLE_API_ARMOR
164850  if( !sqlite3SafetyCheckOk(db) ){
164851    return SQLITE_MISUSE_BKPT;
164852  }
164853#endif
164854
164855  sqlite3_mutex_enter(db->mutex);
164856  iDb = sqlite3FindDbName(db, zDb);
164857  if( iDb==0 || iDb>1 ){
164858    Btree *pBt = db->aDb[iDb].pBt;
164859    if( 0==sqlite3BtreeIsInReadTrans(pBt) ){
164860      rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
164861      if( rc==SQLITE_OK ){
164862        rc = sqlite3PagerSnapshotRecover(sqlite3BtreePager(pBt));
164863        sqlite3BtreeCommit(pBt);
164864      }
164865    }
164866  }
164867  sqlite3_mutex_leave(db->mutex);
164868#endif   /* SQLITE_OMIT_WAL */
164869  return rc;
164870}
164871
164872/*
164873** Free a snapshot handle obtained from sqlite3_snapshot_get().
164874*/
164875SQLITE_API void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){
164876  sqlite3_free(pSnapshot);
164877}
164878#endif /* SQLITE_ENABLE_SNAPSHOT */
164879
164880#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
164881/*
164882** Given the name of a compile-time option, return true if that option
164883** was used and false if not.
164884**
164885** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
164886** is not required for a match.
164887*/
164888SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
164889  int i, n;
164890  int nOpt;
164891  const char **azCompileOpt;
164892
164893#if SQLITE_ENABLE_API_ARMOR
164894  if( zOptName==0 ){
164895    (void)SQLITE_MISUSE_BKPT;
164896    return 0;
164897  }
164898#endif
164899
164900  azCompileOpt = sqlite3CompileOptions(&nOpt);
164901
164902  if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
164903  n = sqlite3Strlen30(zOptName);
164904
164905  /* Since nOpt is normally in single digits, a linear search is
164906  ** adequate. No need for a binary search. */
164907  for(i=0; i<nOpt; i++){
164908    if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
164909     && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0
164910    ){
164911      return 1;
164912    }
164913  }
164914  return 0;
164915}
164916
164917/*
164918** Return the N-th compile-time option string.  If N is out of range,
164919** return a NULL pointer.
164920*/
164921SQLITE_API const char *sqlite3_compileoption_get(int N){
164922  int nOpt;
164923  const char **azCompileOpt;
164924  azCompileOpt = sqlite3CompileOptions(&nOpt);
164925  if( N>=0 && N<nOpt ){
164926    return azCompileOpt[N];
164927  }
164928  return 0;
164929}
164930#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
164931
164932/************** End of main.c ************************************************/
164933/************** Begin file notify.c ******************************************/
164934/*
164935** 2009 March 3
164936**
164937** The author disclaims copyright to this source code.  In place of
164938** a legal notice, here is a blessing:
164939**
164940**    May you do good and not evil.
164941**    May you find forgiveness for yourself and forgive others.
164942**    May you share freely, never taking more than you give.
164943**
164944*************************************************************************
164945**
164946** This file contains the implementation of the sqlite3_unlock_notify()
164947** API method and its associated functionality.
164948*/
164949/* #include "sqliteInt.h" */
164950/* #include "btreeInt.h" */
164951
164952/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
164953#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
164954
164955/*
164956** Public interfaces:
164957**
164958**   sqlite3ConnectionBlocked()
164959**   sqlite3ConnectionUnlocked()
164960**   sqlite3ConnectionClosed()
164961**   sqlite3_unlock_notify()
164962*/
164963
164964#define assertMutexHeld() \
164965  assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )
164966
164967/*
164968** Head of a linked list of all sqlite3 objects created by this process
164969** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
164970** is not NULL. This variable may only accessed while the STATIC_MASTER
164971** mutex is held.
164972*/
164973static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
164974
164975#ifndef NDEBUG
164976/*
164977** This function is a complex assert() that verifies the following
164978** properties of the blocked connections list:
164979**
164980**   1) Each entry in the list has a non-NULL value for either
164981**      pUnlockConnection or pBlockingConnection, or both.
164982**
164983**   2) All entries in the list that share a common value for
164984**      xUnlockNotify are grouped together.
164985**
164986**   3) If the argument db is not NULL, then none of the entries in the
164987**      blocked connections list have pUnlockConnection or pBlockingConnection
164988**      set to db. This is used when closing connection db.
164989*/
164990static void checkListProperties(sqlite3 *db){
164991  sqlite3 *p;
164992  for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
164993    int seen = 0;
164994    sqlite3 *p2;
164995
164996    /* Verify property (1) */
164997    assert( p->pUnlockConnection || p->pBlockingConnection );
164998
164999    /* Verify property (2) */
165000    for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
165001      if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
165002      assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
165003      assert( db==0 || p->pUnlockConnection!=db );
165004      assert( db==0 || p->pBlockingConnection!=db );
165005    }
165006  }
165007}
165008#else
165009# define checkListProperties(x)
165010#endif
165011
165012/*
165013** Remove connection db from the blocked connections list. If connection
165014** db is not currently a part of the list, this function is a no-op.
165015*/
165016static void removeFromBlockedList(sqlite3 *db){
165017  sqlite3 **pp;
165018  assertMutexHeld();
165019  for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
165020    if( *pp==db ){
165021      *pp = (*pp)->pNextBlocked;
165022      break;
165023    }
165024  }
165025}
165026
165027/*
165028** Add connection db to the blocked connections list. It is assumed
165029** that it is not already a part of the list.
165030*/
165031static void addToBlockedList(sqlite3 *db){
165032  sqlite3 **pp;
165033  assertMutexHeld();
165034  for(
165035    pp=&sqlite3BlockedList;
165036    *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
165037    pp=&(*pp)->pNextBlocked
165038  );
165039  db->pNextBlocked = *pp;
165040  *pp = db;
165041}
165042
165043/*
165044** Obtain the STATIC_MASTER mutex.
165045*/
165046static void enterMutex(void){
165047  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
165048  checkListProperties(0);
165049}
165050
165051/*
165052** Release the STATIC_MASTER mutex.
165053*/
165054static void leaveMutex(void){
165055  assertMutexHeld();
165056  checkListProperties(0);
165057  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
165058}
165059
165060/*
165061** Register an unlock-notify callback.
165062**
165063** This is called after connection "db" has attempted some operation
165064** but has received an SQLITE_LOCKED error because another connection
165065** (call it pOther) in the same process was busy using the same shared
165066** cache.  pOther is found by looking at db->pBlockingConnection.
165067**
165068** If there is no blocking connection, the callback is invoked immediately,
165069** before this routine returns.
165070**
165071** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
165072** a deadlock.
165073**
165074** Otherwise, make arrangements to invoke xNotify when pOther drops
165075** its locks.
165076**
165077** Each call to this routine overrides any prior callbacks registered
165078** on the same "db".  If xNotify==0 then any prior callbacks are immediately
165079** cancelled.
165080*/
165081SQLITE_API int sqlite3_unlock_notify(
165082  sqlite3 *db,
165083  void (*xNotify)(void **, int),
165084  void *pArg
165085){
165086  int rc = SQLITE_OK;
165087
165088  sqlite3_mutex_enter(db->mutex);
165089  enterMutex();
165090
165091  if( xNotify==0 ){
165092    removeFromBlockedList(db);
165093    db->pBlockingConnection = 0;
165094    db->pUnlockConnection = 0;
165095    db->xUnlockNotify = 0;
165096    db->pUnlockArg = 0;
165097  }else if( 0==db->pBlockingConnection ){
165098    /* The blocking transaction has been concluded. Or there never was a
165099    ** blocking transaction. In either case, invoke the notify callback
165100    ** immediately.
165101    */
165102    xNotify(&pArg, 1);
165103  }else{
165104    sqlite3 *p;
165105
165106    for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
165107    if( p ){
165108      rc = SQLITE_LOCKED;              /* Deadlock detected. */
165109    }else{
165110      db->pUnlockConnection = db->pBlockingConnection;
165111      db->xUnlockNotify = xNotify;
165112      db->pUnlockArg = pArg;
165113      removeFromBlockedList(db);
165114      addToBlockedList(db);
165115    }
165116  }
165117
165118  leaveMutex();
165119  assert( !db->mallocFailed );
165120  sqlite3ErrorWithMsg(db, rc, (rc?"database is deadlocked":0));
165121  sqlite3_mutex_leave(db->mutex);
165122  return rc;
165123}
165124
165125/*
165126** This function is called while stepping or preparing a statement
165127** associated with connection db. The operation will return SQLITE_LOCKED
165128** to the user because it requires a lock that will not be available
165129** until connection pBlocker concludes its current transaction.
165130*/
165131SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
165132  enterMutex();
165133  if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
165134    addToBlockedList(db);
165135  }
165136  db->pBlockingConnection = pBlocker;
165137  leaveMutex();
165138}
165139
165140/*
165141** This function is called when
165142** the transaction opened by database db has just finished. Locks held
165143** by database connection db have been released.
165144**
165145** This function loops through each entry in the blocked connections
165146** list and does the following:
165147**
165148**   1) If the sqlite3.pBlockingConnection member of a list entry is
165149**      set to db, then set pBlockingConnection=0.
165150**
165151**   2) If the sqlite3.pUnlockConnection member of a list entry is
165152**      set to db, then invoke the configured unlock-notify callback and
165153**      set pUnlockConnection=0.
165154**
165155**   3) If the two steps above mean that pBlockingConnection==0 and
165156**      pUnlockConnection==0, remove the entry from the blocked connections
165157**      list.
165158*/
165159SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
165160  void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
165161  int nArg = 0;                            /* Number of entries in aArg[] */
165162  sqlite3 **pp;                            /* Iterator variable */
165163  void **aArg;               /* Arguments to the unlock callback */
165164  void **aDyn = 0;           /* Dynamically allocated space for aArg[] */
165165  void *aStatic[16];         /* Starter space for aArg[].  No malloc required */
165166
165167  aArg = aStatic;
165168  enterMutex();         /* Enter STATIC_MASTER mutex */
165169
165170  /* This loop runs once for each entry in the blocked-connections list. */
165171  for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
165172    sqlite3 *p = *pp;
165173
165174    /* Step 1. */
165175    if( p->pBlockingConnection==db ){
165176      p->pBlockingConnection = 0;
165177    }
165178
165179    /* Step 2. */
165180    if( p->pUnlockConnection==db ){
165181      assert( p->xUnlockNotify );
165182      if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
165183        xUnlockNotify(aArg, nArg);
165184        nArg = 0;
165185      }
165186
165187      sqlite3BeginBenignMalloc();
165188      assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
165189      assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
165190      if( (!aDyn && nArg==(int)ArraySize(aStatic))
165191       || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
165192      ){
165193        /* The aArg[] array needs to grow. */
165194        void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
165195        if( pNew ){
165196          memcpy(pNew, aArg, nArg*sizeof(void *));
165197          sqlite3_free(aDyn);
165198          aDyn = aArg = pNew;
165199        }else{
165200          /* This occurs when the array of context pointers that need to
165201          ** be passed to the unlock-notify callback is larger than the
165202          ** aStatic[] array allocated on the stack and the attempt to
165203          ** allocate a larger array from the heap has failed.
165204          **
165205          ** This is a difficult situation to handle. Returning an error
165206          ** code to the caller is insufficient, as even if an error code
165207          ** is returned the transaction on connection db will still be
165208          ** closed and the unlock-notify callbacks on blocked connections
165209          ** will go unissued. This might cause the application to wait
165210          ** indefinitely for an unlock-notify callback that will never
165211          ** arrive.
165212          **
165213          ** Instead, invoke the unlock-notify callback with the context
165214          ** array already accumulated. We can then clear the array and
165215          ** begin accumulating any further context pointers without
165216          ** requiring any dynamic allocation. This is sub-optimal because
165217          ** it means that instead of one callback with a large array of
165218          ** context pointers the application will receive two or more
165219          ** callbacks with smaller arrays of context pointers, which will
165220          ** reduce the applications ability to prioritize multiple
165221          ** connections. But it is the best that can be done under the
165222          ** circumstances.
165223          */
165224          xUnlockNotify(aArg, nArg);
165225          nArg = 0;
165226        }
165227      }
165228      sqlite3EndBenignMalloc();
165229
165230      aArg[nArg++] = p->pUnlockArg;
165231      xUnlockNotify = p->xUnlockNotify;
165232      p->pUnlockConnection = 0;
165233      p->xUnlockNotify = 0;
165234      p->pUnlockArg = 0;
165235    }
165236
165237    /* Step 3. */
165238    if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
165239      /* Remove connection p from the blocked connections list. */
165240      *pp = p->pNextBlocked;
165241      p->pNextBlocked = 0;
165242    }else{
165243      pp = &p->pNextBlocked;
165244    }
165245  }
165246
165247  if( nArg!=0 ){
165248    xUnlockNotify(aArg, nArg);
165249  }
165250  sqlite3_free(aDyn);
165251  leaveMutex();         /* Leave STATIC_MASTER mutex */
165252}
165253
165254/*
165255** This is called when the database connection passed as an argument is
165256** being closed. The connection is removed from the blocked list.
165257*/
165258SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
165259  sqlite3ConnectionUnlocked(db);
165260  enterMutex();
165261  removeFromBlockedList(db);
165262  checkListProperties(db);
165263  leaveMutex();
165264}
165265#endif
165266
165267/************** End of notify.c **********************************************/
165268/************** Begin file fts3.c ********************************************/
165269/*
165270** 2006 Oct 10
165271**
165272** The author disclaims copyright to this source code.  In place of
165273** a legal notice, here is a blessing:
165274**
165275**    May you do good and not evil.
165276**    May you find forgiveness for yourself and forgive others.
165277**    May you share freely, never taking more than you give.
165278**
165279******************************************************************************
165280**
165281** This is an SQLite module implementing full-text search.
165282*/
165283
165284/*
165285** The code in this file is only compiled if:
165286**
165287**     * The FTS3 module is being built as an extension
165288**       (in which case SQLITE_CORE is not defined), or
165289**
165290**     * The FTS3 module is being built into the core of
165291**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
165292*/
165293
165294/* The full-text index is stored in a series of b+tree (-like)
165295** structures called segments which map terms to doclists.  The
165296** structures are like b+trees in layout, but are constructed from the
165297** bottom up in optimal fashion and are not updatable.  Since trees
165298** are built from the bottom up, things will be described from the
165299** bottom up.
165300**
165301**
165302**** Varints ****
165303** The basic unit of encoding is a variable-length integer called a
165304** varint.  We encode variable-length integers in little-endian order
165305** using seven bits * per byte as follows:
165306**
165307** KEY:
165308**         A = 0xxxxxxx    7 bits of data and one flag bit
165309**         B = 1xxxxxxx    7 bits of data and one flag bit
165310**
165311**  7 bits - A
165312** 14 bits - BA
165313** 21 bits - BBA
165314** and so on.
165315**
165316** This is similar in concept to how sqlite encodes "varints" but
165317** the encoding is not the same.  SQLite varints are big-endian
165318** are are limited to 9 bytes in length whereas FTS3 varints are
165319** little-endian and can be up to 10 bytes in length (in theory).
165320**
165321** Example encodings:
165322**
165323**     1:    0x01
165324**   127:    0x7f
165325**   128:    0x81 0x00
165326**
165327**
165328**** Document lists ****
165329** A doclist (document list) holds a docid-sorted list of hits for a
165330** given term.  Doclists hold docids and associated token positions.
165331** A docid is the unique integer identifier for a single document.
165332** A position is the index of a word within the document.  The first
165333** word of the document has a position of 0.
165334**
165335** FTS3 used to optionally store character offsets using a compile-time
165336** option.  But that functionality is no longer supported.
165337**
165338** A doclist is stored like this:
165339**
165340** array {
165341**   varint docid;          (delta from previous doclist)
165342**   array {                (position list for column 0)
165343**     varint position;     (2 more than the delta from previous position)
165344**   }
165345**   array {
165346**     varint POS_COLUMN;   (marks start of position list for new column)
165347**     varint column;       (index of new column)
165348**     array {
165349**       varint position;   (2 more than the delta from previous position)
165350**     }
165351**   }
165352**   varint POS_END;        (marks end of positions for this document.
165353** }
165354**
165355** Here, array { X } means zero or more occurrences of X, adjacent in
165356** memory.  A "position" is an index of a token in the token stream
165357** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
165358** in the same logical place as the position element, and act as sentinals
165359** ending a position list array.  POS_END is 0.  POS_COLUMN is 1.
165360** The positions numbers are not stored literally but rather as two more
165361** than the difference from the prior position, or the just the position plus
165362** 2 for the first position.  Example:
165363**
165364**   label:       A B C D E  F  G H   I  J K
165365**   value:     123 5 9 1 1 14 35 0 234 72 0
165366**
165367** The 123 value is the first docid.  For column zero in this document
165368** there are two matches at positions 3 and 10 (5-2 and 9-2+3).  The 1
165369** at D signals the start of a new column; the 1 at E indicates that the
165370** new column is column number 1.  There are two positions at 12 and 45
165371** (14-2 and 35-2+12).  The 0 at H indicate the end-of-document.  The
165372** 234 at I is the delta to next docid (357).  It has one position 70
165373** (72-2) and then terminates with the 0 at K.
165374**
165375** A "position-list" is the list of positions for multiple columns for
165376** a single docid.  A "column-list" is the set of positions for a single
165377** column.  Hence, a position-list consists of one or more column-lists,
165378** a document record consists of a docid followed by a position-list and
165379** a doclist consists of one or more document records.
165380**
165381** A bare doclist omits the position information, becoming an
165382** array of varint-encoded docids.
165383**
165384**** Segment leaf nodes ****
165385** Segment leaf nodes store terms and doclists, ordered by term.  Leaf
165386** nodes are written using LeafWriter, and read using LeafReader (to
165387** iterate through a single leaf node's data) and LeavesReader (to
165388** iterate through a segment's entire leaf layer).  Leaf nodes have
165389** the format:
165390**
165391** varint iHeight;             (height from leaf level, always 0)
165392** varint nTerm;               (length of first term)
165393** char pTerm[nTerm];          (content of first term)
165394** varint nDoclist;            (length of term's associated doclist)
165395** char pDoclist[nDoclist];    (content of doclist)
165396** array {
165397**                             (further terms are delta-encoded)
165398**   varint nPrefix;           (length of prefix shared with previous term)
165399**   varint nSuffix;           (length of unshared suffix)
165400**   char pTermSuffix[nSuffix];(unshared suffix of next term)
165401**   varint nDoclist;          (length of term's associated doclist)
165402**   char pDoclist[nDoclist];  (content of doclist)
165403** }
165404**
165405** Here, array { X } means zero or more occurrences of X, adjacent in
165406** memory.
165407**
165408** Leaf nodes are broken into blocks which are stored contiguously in
165409** the %_segments table in sorted order.  This means that when the end
165410** of a node is reached, the next term is in the node with the next
165411** greater node id.
165412**
165413** New data is spilled to a new leaf node when the current node
165414** exceeds LEAF_MAX bytes (default 2048).  New data which itself is
165415** larger than STANDALONE_MIN (default 1024) is placed in a standalone
165416** node (a leaf node with a single term and doclist).  The goal of
165417** these settings is to pack together groups of small doclists while
165418** making it efficient to directly access large doclists.  The
165419** assumption is that large doclists represent terms which are more
165420** likely to be query targets.
165421**
165422** TODO(shess) It may be useful for blocking decisions to be more
165423** dynamic.  For instance, it may make more sense to have a 2.5k leaf
165424** node rather than splitting into 2k and .5k nodes.  My intuition is
165425** that this might extend through 2x or 4x the pagesize.
165426**
165427**
165428**** Segment interior nodes ****
165429** Segment interior nodes store blockids for subtree nodes and terms
165430** to describe what data is stored by the each subtree.  Interior
165431** nodes are written using InteriorWriter, and read using
165432** InteriorReader.  InteriorWriters are created as needed when
165433** SegmentWriter creates new leaf nodes, or when an interior node
165434** itself grows too big and must be split.  The format of interior
165435** nodes:
165436**
165437** varint iHeight;           (height from leaf level, always >0)
165438** varint iBlockid;          (block id of node's leftmost subtree)
165439** optional {
165440**   varint nTerm;           (length of first term)
165441**   char pTerm[nTerm];      (content of first term)
165442**   array {
165443**                                (further terms are delta-encoded)
165444**     varint nPrefix;            (length of shared prefix with previous term)
165445**     varint nSuffix;            (length of unshared suffix)
165446**     char pTermSuffix[nSuffix]; (unshared suffix of next term)
165447**   }
165448** }
165449**
165450** Here, optional { X } means an optional element, while array { X }
165451** means zero or more occurrences of X, adjacent in memory.
165452**
165453** An interior node encodes n terms separating n+1 subtrees.  The
165454** subtree blocks are contiguous, so only the first subtree's blockid
165455** is encoded.  The subtree at iBlockid will contain all terms less
165456** than the first term encoded (or all terms if no term is encoded).
165457** Otherwise, for terms greater than or equal to pTerm[i] but less
165458** than pTerm[i+1], the subtree for that term will be rooted at
165459** iBlockid+i.  Interior nodes only store enough term data to
165460** distinguish adjacent children (if the rightmost term of the left
165461** child is "something", and the leftmost term of the right child is
165462** "wicked", only "w" is stored).
165463**
165464** New data is spilled to a new interior node at the same height when
165465** the current node exceeds INTERIOR_MAX bytes (default 2048).
165466** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
165467** interior nodes and making the tree too skinny.  The interior nodes
165468** at a given height are naturally tracked by interior nodes at
165469** height+1, and so on.
165470**
165471**
165472**** Segment directory ****
165473** The segment directory in table %_segdir stores meta-information for
165474** merging and deleting segments, and also the root node of the
165475** segment's tree.
165476**
165477** The root node is the top node of the segment's tree after encoding
165478** the entire segment, restricted to ROOT_MAX bytes (default 1024).
165479** This could be either a leaf node or an interior node.  If the top
165480** node requires more than ROOT_MAX bytes, it is flushed to %_segments
165481** and a new root interior node is generated (which should always fit
165482** within ROOT_MAX because it only needs space for 2 varints, the
165483** height and the blockid of the previous root).
165484**
165485** The meta-information in the segment directory is:
165486**   level               - segment level (see below)
165487**   idx                 - index within level
165488**                       - (level,idx uniquely identify a segment)
165489**   start_block         - first leaf node
165490**   leaves_end_block    - last leaf node
165491**   end_block           - last block (including interior nodes)
165492**   root                - contents of root node
165493**
165494** If the root node is a leaf node, then start_block,
165495** leaves_end_block, and end_block are all 0.
165496**
165497**
165498**** Segment merging ****
165499** To amortize update costs, segments are grouped into levels and
165500** merged in batches.  Each increase in level represents exponentially
165501** more documents.
165502**
165503** New documents (actually, document updates) are tokenized and
165504** written individually (using LeafWriter) to a level 0 segment, with
165505** incrementing idx.  When idx reaches MERGE_COUNT (default 16), all
165506** level 0 segments are merged into a single level 1 segment.  Level 1
165507** is populated like level 0, and eventually MERGE_COUNT level 1
165508** segments are merged to a single level 2 segment (representing
165509** MERGE_COUNT^2 updates), and so on.
165510**
165511** A segment merge traverses all segments at a given level in
165512** parallel, performing a straightforward sorted merge.  Since segment
165513** leaf nodes are written in to the %_segments table in order, this
165514** merge traverses the underlying sqlite disk structures efficiently.
165515** After the merge, all segment blocks from the merged level are
165516** deleted.
165517**
165518** MERGE_COUNT controls how often we merge segments.  16 seems to be
165519** somewhat of a sweet spot for insertion performance.  32 and 64 show
165520** very similar performance numbers to 16 on insertion, though they're
165521** a tiny bit slower (perhaps due to more overhead in merge-time
165522** sorting).  8 is about 20% slower than 16, 4 about 50% slower than
165523** 16, 2 about 66% slower than 16.
165524**
165525** At query time, high MERGE_COUNT increases the number of segments
165526** which need to be scanned and merged.  For instance, with 100k docs
165527** inserted:
165528**
165529**    MERGE_COUNT   segments
165530**       16           25
165531**        8           12
165532**        4           10
165533**        2            6
165534**
165535** This appears to have only a moderate impact on queries for very
165536** frequent terms (which are somewhat dominated by segment merge
165537** costs), and infrequent and non-existent terms still seem to be fast
165538** even with many segments.
165539**
165540** TODO(shess) That said, it would be nice to have a better query-side
165541** argument for MERGE_COUNT of 16.  Also, it is possible/likely that
165542** optimizations to things like doclist merging will swing the sweet
165543** spot around.
165544**
165545**
165546**
165547**** Handling of deletions and updates ****
165548** Since we're using a segmented structure, with no docid-oriented
165549** index into the term index, we clearly cannot simply update the term
165550** index when a document is deleted or updated.  For deletions, we
165551** write an empty doclist (varint(docid) varint(POS_END)), for updates
165552** we simply write the new doclist.  Segment merges overwrite older
165553** data for a particular docid with newer data, so deletes or updates
165554** will eventually overtake the earlier data and knock it out.  The
165555** query logic likewise merges doclists so that newer data knocks out
165556** older data.
165557*/
165558
165559/************** Include fts3Int.h in the middle of fts3.c ********************/
165560/************** Begin file fts3Int.h *****************************************/
165561/*
165562** 2009 Nov 12
165563**
165564** The author disclaims copyright to this source code.  In place of
165565** a legal notice, here is a blessing:
165566**
165567**    May you do good and not evil.
165568**    May you find forgiveness for yourself and forgive others.
165569**    May you share freely, never taking more than you give.
165570**
165571******************************************************************************
165572**
165573*/
165574#ifndef _FTSINT_H
165575#define _FTSINT_H
165576
165577#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
165578# define NDEBUG 1
165579#endif
165580
165581/* FTS3/FTS4 require virtual tables */
165582#ifdef SQLITE_OMIT_VIRTUALTABLE
165583# undef SQLITE_ENABLE_FTS3
165584# undef SQLITE_ENABLE_FTS4
165585#endif
165586
165587/*
165588** FTS4 is really an extension for FTS3.  It is enabled using the
165589** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
165590** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
165591*/
165592#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
165593# define SQLITE_ENABLE_FTS3
165594#endif
165595
165596#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
165597
165598/* If not building as part of the core, include sqlite3ext.h. */
165599#ifndef SQLITE_CORE
165600/* # include "sqlite3ext.h"  */
165601SQLITE_EXTENSION_INIT3
165602#endif
165603
165604/* #include "sqlite3.h" */
165605/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
165606/************** Begin file fts3_tokenizer.h **********************************/
165607/*
165608** 2006 July 10
165609**
165610** The author disclaims copyright to this source code.
165611**
165612*************************************************************************
165613** Defines the interface to tokenizers used by fulltext-search.  There
165614** are three basic components:
165615**
165616** sqlite3_tokenizer_module is a singleton defining the tokenizer
165617** interface functions.  This is essentially the class structure for
165618** tokenizers.
165619**
165620** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
165621** including customization information defined at creation time.
165622**
165623** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
165624** tokens from a particular input.
165625*/
165626#ifndef _FTS3_TOKENIZER_H_
165627#define _FTS3_TOKENIZER_H_
165628
165629/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
165630** If tokenizers are to be allowed to call sqlite3_*() functions, then
165631** we will need a way to register the API consistently.
165632*/
165633/* #include "sqlite3.h" */
165634
165635/*
165636** Structures used by the tokenizer interface. When a new tokenizer
165637** implementation is registered, the caller provides a pointer to
165638** an sqlite3_tokenizer_module containing pointers to the callback
165639** functions that make up an implementation.
165640**
165641** When an fts3 table is created, it passes any arguments passed to
165642** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
165643** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
165644** implementation. The xCreate() function in turn returns an
165645** sqlite3_tokenizer structure representing the specific tokenizer to
165646** be used for the fts3 table (customized by the tokenizer clause arguments).
165647**
165648** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
165649** method is called. It returns an sqlite3_tokenizer_cursor object
165650** that may be used to tokenize a specific input buffer based on
165651** the tokenization rules supplied by a specific sqlite3_tokenizer
165652** object.
165653*/
165654typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
165655typedef struct sqlite3_tokenizer sqlite3_tokenizer;
165656typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
165657
165658struct sqlite3_tokenizer_module {
165659
165660  /*
165661  ** Structure version. Should always be set to 0 or 1.
165662  */
165663  int iVersion;
165664
165665  /*
165666  ** Create a new tokenizer. The values in the argv[] array are the
165667  ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
165668  ** TABLE statement that created the fts3 table. For example, if
165669  ** the following SQL is executed:
165670  **
165671  **   CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
165672  **
165673  ** then argc is set to 2, and the argv[] array contains pointers
165674  ** to the strings "arg1" and "arg2".
165675  **
165676  ** This method should return either SQLITE_OK (0), or an SQLite error
165677  ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
165678  ** to point at the newly created tokenizer structure. The generic
165679  ** sqlite3_tokenizer.pModule variable should not be initialized by
165680  ** this callback. The caller will do so.
165681  */
165682  int (*xCreate)(
165683    int argc,                           /* Size of argv array */
165684    const char *const*argv,             /* Tokenizer argument strings */
165685    sqlite3_tokenizer **ppTokenizer     /* OUT: Created tokenizer */
165686  );
165687
165688  /*
165689  ** Destroy an existing tokenizer. The fts3 module calls this method
165690  ** exactly once for each successful call to xCreate().
165691  */
165692  int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
165693
165694  /*
165695  ** Create a tokenizer cursor to tokenize an input buffer. The caller
165696  ** is responsible for ensuring that the input buffer remains valid
165697  ** until the cursor is closed (using the xClose() method).
165698  */
165699  int (*xOpen)(
165700    sqlite3_tokenizer *pTokenizer,       /* Tokenizer object */
165701    const char *pInput, int nBytes,      /* Input buffer */
165702    sqlite3_tokenizer_cursor **ppCursor  /* OUT: Created tokenizer cursor */
165703  );
165704
165705  /*
165706  ** Destroy an existing tokenizer cursor. The fts3 module calls this
165707  ** method exactly once for each successful call to xOpen().
165708  */
165709  int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
165710
165711  /*
165712  ** Retrieve the next token from the tokenizer cursor pCursor. This
165713  ** method should either return SQLITE_OK and set the values of the
165714  ** "OUT" variables identified below, or SQLITE_DONE to indicate that
165715  ** the end of the buffer has been reached, or an SQLite error code.
165716  **
165717  ** *ppToken should be set to point at a buffer containing the
165718  ** normalized version of the token (i.e. after any case-folding and/or
165719  ** stemming has been performed). *pnBytes should be set to the length
165720  ** of this buffer in bytes. The input text that generated the token is
165721  ** identified by the byte offsets returned in *piStartOffset and
165722  ** *piEndOffset. *piStartOffset should be set to the index of the first
165723  ** byte of the token in the input buffer. *piEndOffset should be set
165724  ** to the index of the first byte just past the end of the token in
165725  ** the input buffer.
165726  **
165727  ** The buffer *ppToken is set to point at is managed by the tokenizer
165728  ** implementation. It is only required to be valid until the next call
165729  ** to xNext() or xClose().
165730  */
165731  /* TODO(shess) current implementation requires pInput to be
165732  ** nul-terminated.  This should either be fixed, or pInput/nBytes
165733  ** should be converted to zInput.
165734  */
165735  int (*xNext)(
165736    sqlite3_tokenizer_cursor *pCursor,   /* Tokenizer cursor */
165737    const char **ppToken, int *pnBytes,  /* OUT: Normalized text for token */
165738    int *piStartOffset,  /* OUT: Byte offset of token in input buffer */
165739    int *piEndOffset,    /* OUT: Byte offset of end of token in input buffer */
165740    int *piPosition      /* OUT: Number of tokens returned before this one */
165741  );
165742
165743  /***********************************************************************
165744  ** Methods below this point are only available if iVersion>=1.
165745  */
165746
165747  /*
165748  ** Configure the language id of a tokenizer cursor.
165749  */
165750  int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
165751};
165752
165753struct sqlite3_tokenizer {
165754  const sqlite3_tokenizer_module *pModule;  /* The module for this tokenizer */
165755  /* Tokenizer implementations will typically add additional fields */
165756};
165757
165758struct sqlite3_tokenizer_cursor {
165759  sqlite3_tokenizer *pTokenizer;       /* Tokenizer for this cursor. */
165760  /* Tokenizer implementations will typically add additional fields */
165761};
165762
165763int fts3_global_term_cnt(int iTerm, int iCol);
165764int fts3_term_cnt(int iTerm, int iCol);
165765
165766
165767#endif /* _FTS3_TOKENIZER_H_ */
165768
165769/************** End of fts3_tokenizer.h **************************************/
165770/************** Continuing where we left off in fts3Int.h ********************/
165771/************** Include fts3_hash.h in the middle of fts3Int.h ***************/
165772/************** Begin file fts3_hash.h ***************************************/
165773/*
165774** 2001 September 22
165775**
165776** The author disclaims copyright to this source code.  In place of
165777** a legal notice, here is a blessing:
165778**
165779**    May you do good and not evil.
165780**    May you find forgiveness for yourself and forgive others.
165781**    May you share freely, never taking more than you give.
165782**
165783*************************************************************************
165784** This is the header file for the generic hash-table implementation
165785** used in SQLite.  We've modified it slightly to serve as a standalone
165786** hash table implementation for the full-text indexing module.
165787**
165788*/
165789#ifndef _FTS3_HASH_H_
165790#define _FTS3_HASH_H_
165791
165792/* Forward declarations of structures. */
165793typedef struct Fts3Hash Fts3Hash;
165794typedef struct Fts3HashElem Fts3HashElem;
165795
165796/* A complete hash table is an instance of the following structure.
165797** The internals of this structure are intended to be opaque -- client
165798** code should not attempt to access or modify the fields of this structure
165799** directly.  Change this structure only by using the routines below.
165800** However, many of the "procedures" and "functions" for modifying and
165801** accessing this structure are really macros, so we can't really make
165802** this structure opaque.
165803*/
165804struct Fts3Hash {
165805  char keyClass;          /* HASH_INT, _POINTER, _STRING, _BINARY */
165806  char copyKey;           /* True if copy of key made on insert */
165807  int count;              /* Number of entries in this table */
165808  Fts3HashElem *first;    /* The first element of the array */
165809  int htsize;             /* Number of buckets in the hash table */
165810  struct _fts3ht {        /* the hash table */
165811    int count;               /* Number of entries with this hash */
165812    Fts3HashElem *chain;     /* Pointer to first entry with this hash */
165813  } *ht;
165814};
165815
165816/* Each element in the hash table is an instance of the following
165817** structure.  All elements are stored on a single doubly-linked list.
165818**
165819** Again, this structure is intended to be opaque, but it can't really
165820** be opaque because it is used by macros.
165821*/
165822struct Fts3HashElem {
165823  Fts3HashElem *next, *prev; /* Next and previous elements in the table */
165824  void *data;                /* Data associated with this element */
165825  void *pKey; int nKey;      /* Key associated with this element */
165826};
165827
165828/*
165829** There are 2 different modes of operation for a hash table:
165830**
165831**   FTS3_HASH_STRING        pKey points to a string that is nKey bytes long
165832**                           (including the null-terminator, if any).  Case
165833**                           is respected in comparisons.
165834**
165835**   FTS3_HASH_BINARY        pKey points to binary data nKey bytes long.
165836**                           memcmp() is used to compare keys.
165837**
165838** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.
165839*/
165840#define FTS3_HASH_STRING    1
165841#define FTS3_HASH_BINARY    2
165842
165843/*
165844** Access routines.  To delete, insert a NULL pointer.
165845*/
165846SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
165847SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
165848SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
165849SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
165850SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
165851
165852/*
165853** Shorthand for the functions above
165854*/
165855#define fts3HashInit     sqlite3Fts3HashInit
165856#define fts3HashInsert   sqlite3Fts3HashInsert
165857#define fts3HashFind     sqlite3Fts3HashFind
165858#define fts3HashClear    sqlite3Fts3HashClear
165859#define fts3HashFindElem sqlite3Fts3HashFindElem
165860
165861/*
165862** Macros for looping over all elements of a hash table.  The idiom is
165863** like this:
165864**
165865**   Fts3Hash h;
165866**   Fts3HashElem *p;
165867**   ...
165868**   for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
165869**     SomeStructure *pData = fts3HashData(p);
165870**     // do something with pData
165871**   }
165872*/
165873#define fts3HashFirst(H)  ((H)->first)
165874#define fts3HashNext(E)   ((E)->next)
165875#define fts3HashData(E)   ((E)->data)
165876#define fts3HashKey(E)    ((E)->pKey)
165877#define fts3HashKeysize(E) ((E)->nKey)
165878
165879/*
165880** Number of entries in a hash table
165881*/
165882#define fts3HashCount(H)  ((H)->count)
165883
165884#endif /* _FTS3_HASH_H_ */
165885
165886/************** End of fts3_hash.h *******************************************/
165887/************** Continuing where we left off in fts3Int.h ********************/
165888
165889/*
165890** This constant determines the maximum depth of an FTS expression tree
165891** that the library will create and use. FTS uses recursion to perform
165892** various operations on the query tree, so the disadvantage of a large
165893** limit is that it may allow very large queries to use large amounts
165894** of stack space (perhaps causing a stack overflow).
165895*/
165896#ifndef SQLITE_FTS3_MAX_EXPR_DEPTH
165897# define SQLITE_FTS3_MAX_EXPR_DEPTH 12
165898#endif
165899
165900
165901/*
165902** This constant controls how often segments are merged. Once there are
165903** FTS3_MERGE_COUNT segments of level N, they are merged into a single
165904** segment of level N+1.
165905*/
165906#define FTS3_MERGE_COUNT 16
165907
165908/*
165909** This is the maximum amount of data (in bytes) to store in the
165910** Fts3Table.pendingTerms hash table. Normally, the hash table is
165911** populated as documents are inserted/updated/deleted in a transaction
165912** and used to create a new segment when the transaction is committed.
165913** However if this limit is reached midway through a transaction, a new
165914** segment is created and the hash table cleared immediately.
165915*/
165916#define FTS3_MAX_PENDING_DATA (1*1024*1024)
165917
165918/*
165919** Macro to return the number of elements in an array. SQLite has a
165920** similar macro called ArraySize(). Use a different name to avoid
165921** a collision when building an amalgamation with built-in FTS3.
165922*/
165923#define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
165924
165925
165926#ifndef MIN
165927# define MIN(x,y) ((x)<(y)?(x):(y))
165928#endif
165929#ifndef MAX
165930# define MAX(x,y) ((x)>(y)?(x):(y))
165931#endif
165932
165933/*
165934** Maximum length of a varint encoded integer. The varint format is different
165935** from that used by SQLite, so the maximum length is 10, not 9.
165936*/
165937#define FTS3_VARINT_MAX 10
165938
165939#define FTS3_BUFFER_PADDING 8
165940
165941/*
165942** FTS4 virtual tables may maintain multiple indexes - one index of all terms
165943** in the document set and zero or more prefix indexes. All indexes are stored
165944** as one or more b+-trees in the %_segments and %_segdir tables.
165945**
165946** It is possible to determine which index a b+-tree belongs to based on the
165947** value stored in the "%_segdir.level" column. Given this value L, the index
165948** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
165949** level values between 0 and 1023 (inclusive) belong to index 0, all levels
165950** between 1024 and 2047 to index 1, and so on.
165951**
165952** It is considered impossible for an index to use more than 1024 levels. In
165953** theory though this may happen, but only after at least
165954** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
165955*/
165956#define FTS3_SEGDIR_MAXLEVEL      1024
165957#define FTS3_SEGDIR_MAXLEVEL_STR "1024"
165958
165959/*
165960** The testcase() macro is only used by the amalgamation.  If undefined,
165961** make it a no-op.
165962*/
165963#ifndef testcase
165964# define testcase(X)
165965#endif
165966
165967/*
165968** Terminator values for position-lists and column-lists.
165969*/
165970#define POS_COLUMN  (1)     /* Column-list terminator */
165971#define POS_END     (0)     /* Position-list terminator */
165972
165973/*
165974** The assert_fts3_nc() macro is similar to the assert() macro, except that it
165975** is used for assert() conditions that are true only if it can be
165976** guranteed that the database is not corrupt.
165977*/
165978#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
165979SQLITE_API extern int sqlite3_fts3_may_be_corrupt;
165980# define assert_fts3_nc(x) assert(sqlite3_fts3_may_be_corrupt || (x))
165981#else
165982# define assert_fts3_nc(x) assert(x)
165983#endif
165984
165985/*
165986** This section provides definitions to allow the
165987** FTS3 extension to be compiled outside of the
165988** amalgamation.
165989*/
165990#ifndef SQLITE_AMALGAMATION
165991/*
165992** Macros indicating that conditional expressions are always true or
165993** false.
165994*/
165995#ifdef SQLITE_COVERAGE_TEST
165996# define ALWAYS(x) (1)
165997# define NEVER(X)  (0)
165998#elif defined(SQLITE_DEBUG)
165999# define ALWAYS(x) sqlite3Fts3Always((x)!=0)
166000# define NEVER(x) sqlite3Fts3Never((x)!=0)
166001SQLITE_PRIVATE int sqlite3Fts3Always(int b);
166002SQLITE_PRIVATE int sqlite3Fts3Never(int b);
166003#else
166004# define ALWAYS(x) (x)
166005# define NEVER(x)  (x)
166006#endif
166007
166008/*
166009** Internal types used by SQLite.
166010*/
166011typedef unsigned char u8;         /* 1-byte (or larger) unsigned integer */
166012typedef short int i16;            /* 2-byte (or larger) signed integer */
166013typedef unsigned int u32;         /* 4-byte unsigned integer */
166014typedef sqlite3_uint64 u64;       /* 8-byte unsigned integer */
166015typedef sqlite3_int64 i64;        /* 8-byte signed integer */
166016
166017/*
166018** Macro used to suppress compiler warnings for unused parameters.
166019*/
166020#define UNUSED_PARAMETER(x) (void)(x)
166021
166022/*
166023** Activate assert() only if SQLITE_TEST is enabled.
166024*/
166025#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
166026# define NDEBUG 1
166027#endif
166028
166029/*
166030** The TESTONLY macro is used to enclose variable declarations or
166031** other bits of code that are needed to support the arguments
166032** within testcase() and assert() macros.
166033*/
166034#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
166035# define TESTONLY(X)  X
166036#else
166037# define TESTONLY(X)
166038#endif
166039
166040#define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
166041#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
166042
166043#endif /* SQLITE_AMALGAMATION */
166044
166045#ifdef SQLITE_DEBUG
166046SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
166047# define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
166048#else
166049# define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
166050#endif
166051
166052typedef struct Fts3Table Fts3Table;
166053typedef struct Fts3Cursor Fts3Cursor;
166054typedef struct Fts3Expr Fts3Expr;
166055typedef struct Fts3Phrase Fts3Phrase;
166056typedef struct Fts3PhraseToken Fts3PhraseToken;
166057
166058typedef struct Fts3Doclist Fts3Doclist;
166059typedef struct Fts3SegFilter Fts3SegFilter;
166060typedef struct Fts3DeferredToken Fts3DeferredToken;
166061typedef struct Fts3SegReader Fts3SegReader;
166062typedef struct Fts3MultiSegReader Fts3MultiSegReader;
166063
166064typedef struct MatchinfoBuffer MatchinfoBuffer;
166065
166066/*
166067** A connection to a fulltext index is an instance of the following
166068** structure. The xCreate and xConnect methods create an instance
166069** of this structure and xDestroy and xDisconnect free that instance.
166070** All other methods receive a pointer to the structure as one of their
166071** arguments.
166072*/
166073struct Fts3Table {
166074  sqlite3_vtab base;              /* Base class used by SQLite core */
166075  sqlite3 *db;                    /* The database connection */
166076  const char *zDb;                /* logical database name */
166077  const char *zName;              /* virtual table name */
166078  int nColumn;                    /* number of named columns in virtual table */
166079  char **azColumn;                /* column names.  malloced */
166080  u8 *abNotindexed;               /* True for 'notindexed' columns */
166081  sqlite3_tokenizer *pTokenizer;  /* tokenizer for inserts and queries */
166082  char *zContentTbl;              /* content=xxx option, or NULL */
166083  char *zLanguageid;              /* languageid=xxx option, or NULL */
166084  int nAutoincrmerge;             /* Value configured by 'automerge' */
166085  u32 nLeafAdd;                   /* Number of leaf blocks added this trans */
166086  int bLock;                      /* Used to prevent recursive content= tbls */
166087
166088  /* Precompiled statements used by the implementation. Each of these
166089  ** statements is run and reset within a single virtual table API call.
166090  */
166091  sqlite3_stmt *aStmt[40];
166092  sqlite3_stmt *pSeekStmt;        /* Cache for fts3CursorSeekStmt() */
166093
166094  char *zReadExprlist;
166095  char *zWriteExprlist;
166096
166097  int nNodeSize;                  /* Soft limit for node size */
166098  u8 bFts4;                       /* True for FTS4, false for FTS3 */
166099  u8 bHasStat;                    /* True if %_stat table exists (2==unknown) */
166100  u8 bHasDocsize;                 /* True if %_docsize table exists */
166101  u8 bDescIdx;                    /* True if doclists are in reverse order */
166102  u8 bIgnoreSavepoint;            /* True to ignore xSavepoint invocations */
166103  int nPgsz;                      /* Page size for host database */
166104  char *zSegmentsTbl;             /* Name of %_segments table */
166105  sqlite3_blob *pSegments;        /* Blob handle open on %_segments table */
166106
166107  /*
166108  ** The following array of hash tables is used to buffer pending index
166109  ** updates during transactions. All pending updates buffered at any one
166110  ** time must share a common language-id (see the FTS4 langid= feature).
166111  ** The current language id is stored in variable iPrevLangid.
166112  **
166113  ** A single FTS4 table may have multiple full-text indexes. For each index
166114  ** there is an entry in the aIndex[] array. Index 0 is an index of all the
166115  ** terms that appear in the document set. Each subsequent index in aIndex[]
166116  ** is an index of prefixes of a specific length.
166117  **
166118  ** Variable nPendingData contains an estimate the memory consumed by the
166119  ** pending data structures, including hash table overhead, but not including
166120  ** malloc overhead.  When nPendingData exceeds nMaxPendingData, all hash
166121  ** tables are flushed to disk. Variable iPrevDocid is the docid of the most
166122  ** recently inserted record.
166123  */
166124  int nIndex;                     /* Size of aIndex[] */
166125  struct Fts3Index {
166126    int nPrefix;                  /* Prefix length (0 for main terms index) */
166127    Fts3Hash hPending;            /* Pending terms table for this index */
166128  } *aIndex;
166129  int nMaxPendingData;            /* Max pending data before flush to disk */
166130  int nPendingData;               /* Current bytes of pending data */
166131  sqlite_int64 iPrevDocid;        /* Docid of most recently inserted document */
166132  int iPrevLangid;                /* Langid of recently inserted document */
166133  int bPrevDelete;                /* True if last operation was a delete */
166134
166135#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
166136  /* State variables used for validating that the transaction control
166137  ** methods of the virtual table are called at appropriate times.  These
166138  ** values do not contribute to FTS functionality; they are used for
166139  ** verifying the operation of the SQLite core.
166140  */
166141  int inTransaction;     /* True after xBegin but before xCommit/xRollback */
166142  int mxSavepoint;       /* Largest valid xSavepoint integer */
166143#endif
166144
166145#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
166146  /* True to disable the incremental doclist optimization. This is controled
166147  ** by special insert command 'test-no-incr-doclist'.  */
166148  int bNoIncrDoclist;
166149
166150  /* Number of segments in a level */
166151  int nMergeCount;
166152#endif
166153};
166154
166155/* Macro to find the number of segments to merge */
166156#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
166157# define MergeCount(P) ((P)->nMergeCount)
166158#else
166159# define MergeCount(P) FTS3_MERGE_COUNT
166160#endif
166161
166162/*
166163** When the core wants to read from the virtual table, it creates a
166164** virtual table cursor (an instance of the following structure) using
166165** the xOpen method. Cursors are destroyed using the xClose method.
166166*/
166167struct Fts3Cursor {
166168  sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
166169  i16 eSearch;                    /* Search strategy (see below) */
166170  u8 isEof;                       /* True if at End Of Results */
166171  u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
166172  u8 bSeekStmt;                   /* True if pStmt is a seek */
166173  sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
166174  Fts3Expr *pExpr;                /* Parsed MATCH query string */
166175  int iLangid;                    /* Language being queried for */
166176  int nPhrase;                    /* Number of matchable phrases in query */
166177  Fts3DeferredToken *pDeferred;   /* Deferred search tokens, if any */
166178  sqlite3_int64 iPrevId;          /* Previous id read from aDoclist */
166179  char *pNextId;                  /* Pointer into the body of aDoclist */
166180  char *aDoclist;                 /* List of docids for full-text queries */
166181  int nDoclist;                   /* Size of buffer at aDoclist */
166182  u8 bDesc;                       /* True to sort in descending order */
166183  int eEvalmode;                  /* An FTS3_EVAL_XX constant */
166184  int nRowAvg;                    /* Average size of database rows, in pages */
166185  sqlite3_int64 nDoc;             /* Documents in table */
166186  i64 iMinDocid;                  /* Minimum docid to return */
166187  i64 iMaxDocid;                  /* Maximum docid to return */
166188  int isMatchinfoNeeded;          /* True when aMatchinfo[] needs filling in */
166189  MatchinfoBuffer *pMIBuffer;     /* Buffer for matchinfo data */
166190};
166191
166192#define FTS3_EVAL_FILTER    0
166193#define FTS3_EVAL_NEXT      1
166194#define FTS3_EVAL_MATCHINFO 2
166195
166196/*
166197** The Fts3Cursor.eSearch member is always set to one of the following.
166198** Actualy, Fts3Cursor.eSearch can be greater than or equal to
166199** FTS3_FULLTEXT_SEARCH.  If so, then Fts3Cursor.eSearch - 2 is the index
166200** of the column to be searched.  For example, in
166201**
166202**     CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
166203**     SELECT docid FROM ex1 WHERE b MATCH 'one two three';
166204**
166205** Because the LHS of the MATCH operator is 2nd column "b",
166206** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1.  (+0 for a,
166207** +1 for b, +2 for c, +3 for d.)  If the LHS of MATCH were "ex1"
166208** indicating that all columns should be searched,
166209** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
166210*/
166211#define FTS3_FULLSCAN_SEARCH 0    /* Linear scan of %_content table */
166212#define FTS3_DOCID_SEARCH    1    /* Lookup by rowid on %_content table */
166213#define FTS3_FULLTEXT_SEARCH 2    /* Full-text index search */
166214
166215/*
166216** The lower 16-bits of the sqlite3_index_info.idxNum value set by
166217** the xBestIndex() method contains the Fts3Cursor.eSearch value described
166218** above. The upper 16-bits contain a combination of the following
166219** bits, used to describe extra constraints on full-text searches.
166220*/
166221#define FTS3_HAVE_LANGID    0x00010000      /* languageid=? */
166222#define FTS3_HAVE_DOCID_GE  0x00020000      /* docid>=? */
166223#define FTS3_HAVE_DOCID_LE  0x00040000      /* docid<=? */
166224
166225struct Fts3Doclist {
166226  char *aAll;                    /* Array containing doclist (or NULL) */
166227  int nAll;                      /* Size of a[] in bytes */
166228  char *pNextDocid;              /* Pointer to next docid */
166229
166230  sqlite3_int64 iDocid;          /* Current docid (if pList!=0) */
166231  int bFreeList;                 /* True if pList should be sqlite3_free()d */
166232  char *pList;                   /* Pointer to position list following iDocid */
166233  int nList;                     /* Length of position list */
166234};
166235
166236/*
166237** A "phrase" is a sequence of one or more tokens that must match in
166238** sequence.  A single token is the base case and the most common case.
166239** For a sequence of tokens contained in double-quotes (i.e. "one two three")
166240** nToken will be the number of tokens in the string.
166241*/
166242struct Fts3PhraseToken {
166243  char *z;                        /* Text of the token */
166244  int n;                          /* Number of bytes in buffer z */
166245  int isPrefix;                   /* True if token ends with a "*" character */
166246  int bFirst;                     /* True if token must appear at position 0 */
166247
166248  /* Variables above this point are populated when the expression is
166249  ** parsed (by code in fts3_expr.c). Below this point the variables are
166250  ** used when evaluating the expression. */
166251  Fts3DeferredToken *pDeferred;   /* Deferred token object for this token */
166252  Fts3MultiSegReader *pSegcsr;    /* Segment-reader for this token */
166253};
166254
166255struct Fts3Phrase {
166256  /* Cache of doclist for this phrase. */
166257  Fts3Doclist doclist;
166258  int bIncr;                 /* True if doclist is loaded incrementally */
166259  int iDoclistToken;
166260
166261  /* Used by sqlite3Fts3EvalPhrasePoslist() if this is a descendent of an
166262  ** OR condition.  */
166263  char *pOrPoslist;
166264  i64 iOrDocid;
166265
166266  /* Variables below this point are populated by fts3_expr.c when parsing
166267  ** a MATCH expression. Everything above is part of the evaluation phase.
166268  */
166269  int nToken;                /* Number of tokens in the phrase */
166270  int iColumn;               /* Index of column this phrase must match */
166271  Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
166272};
166273
166274/*
166275** A tree of these objects forms the RHS of a MATCH operator.
166276**
166277** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
166278** points to a malloced buffer, size nDoclist bytes, containing the results
166279** of this phrase query in FTS3 doclist format. As usual, the initial
166280** "Length" field found in doclists stored on disk is omitted from this
166281** buffer.
166282**
166283** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
166284** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
166285** where nCol is the number of columns in the queried FTS table. The array
166286** is populated as follows:
166287**
166288**   aMI[iCol*3 + 0] = Undefined
166289**   aMI[iCol*3 + 1] = Number of occurrences
166290**   aMI[iCol*3 + 2] = Number of rows containing at least one instance
166291**
166292** The aMI array is allocated using sqlite3_malloc(). It should be freed
166293** when the expression node is.
166294*/
166295struct Fts3Expr {
166296  int eType;                 /* One of the FTSQUERY_XXX values defined below */
166297  int nNear;                 /* Valid if eType==FTSQUERY_NEAR */
166298  Fts3Expr *pParent;         /* pParent->pLeft==this or pParent->pRight==this */
166299  Fts3Expr *pLeft;           /* Left operand */
166300  Fts3Expr *pRight;          /* Right operand */
166301  Fts3Phrase *pPhrase;       /* Valid if eType==FTSQUERY_PHRASE */
166302
166303  /* The following are used by the fts3_eval.c module. */
166304  sqlite3_int64 iDocid;      /* Current docid */
166305  u8 bEof;                   /* True this expression is at EOF already */
166306  u8 bStart;                 /* True if iDocid is valid */
166307  u8 bDeferred;              /* True if this expression is entirely deferred */
166308
166309  /* The following are used by the fts3_snippet.c module. */
166310  int iPhrase;               /* Index of this phrase in matchinfo() results */
166311  u32 *aMI;                  /* See above */
166312};
166313
166314/*
166315** Candidate values for Fts3Query.eType. Note that the order of the first
166316** four values is in order of precedence when parsing expressions. For
166317** example, the following:
166318**
166319**   "a OR b AND c NOT d NEAR e"
166320**
166321** is equivalent to:
166322**
166323**   "a OR (b AND (c NOT (d NEAR e)))"
166324*/
166325#define FTSQUERY_NEAR   1
166326#define FTSQUERY_NOT    2
166327#define FTSQUERY_AND    3
166328#define FTSQUERY_OR     4
166329#define FTSQUERY_PHRASE 5
166330
166331
166332/* fts3_write.c */
166333SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
166334SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
166335SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
166336SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
166337SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
166338  sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
166339SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
166340  Fts3Table*,int,const char*,int,int,Fts3SegReader**);
166341SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
166342SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
166343SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
166344
166345SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
166346SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
166347
166348#ifndef SQLITE_DISABLE_FTS4_DEFERRED
166349SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
166350SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
166351SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
166352SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
166353SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
166354#else
166355# define sqlite3Fts3FreeDeferredTokens(x)
166356# define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK
166357# define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK
166358# define sqlite3Fts3FreeDeferredDoclists(x)
166359# define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK
166360#endif
166361
166362SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
166363SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *, int *);
166364
166365/* Special values interpreted by sqlite3SegReaderCursor() */
166366#define FTS3_SEGCURSOR_PENDING        -1
166367#define FTS3_SEGCURSOR_ALL            -2
166368
166369SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
166370SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
166371SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
166372
166373SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(Fts3Table *,
166374    int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
166375
166376/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
166377#define FTS3_SEGMENT_REQUIRE_POS   0x00000001
166378#define FTS3_SEGMENT_IGNORE_EMPTY  0x00000002
166379#define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
166380#define FTS3_SEGMENT_PREFIX        0x00000008
166381#define FTS3_SEGMENT_SCAN          0x00000010
166382#define FTS3_SEGMENT_FIRST         0x00000020
166383
166384/* Type passed as 4th argument to SegmentReaderIterate() */
166385struct Fts3SegFilter {
166386  const char *zTerm;
166387  int nTerm;
166388  int iCol;
166389  int flags;
166390};
166391
166392struct Fts3MultiSegReader {
166393  /* Used internally by sqlite3Fts3SegReaderXXX() calls */
166394  Fts3SegReader **apSegment;      /* Array of Fts3SegReader objects */
166395  int nSegment;                   /* Size of apSegment array */
166396  int nAdvance;                   /* How many seg-readers to advance */
166397  Fts3SegFilter *pFilter;         /* Pointer to filter object */
166398  char *aBuffer;                  /* Buffer to merge doclists in */
166399  int nBuffer;                    /* Allocated size of aBuffer[] in bytes */
166400
166401  int iColFilter;                 /* If >=0, filter for this column */
166402  int bRestart;
166403
166404  /* Used by fts3.c only. */
166405  int nCost;                      /* Cost of running iterator */
166406  int bLookup;                    /* True if a lookup of a single entry. */
166407
166408  /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
166409  char *zTerm;                    /* Pointer to term buffer */
166410  int nTerm;                      /* Size of zTerm in bytes */
166411  char *aDoclist;                 /* Pointer to doclist buffer */
166412  int nDoclist;                   /* Size of aDoclist[] in bytes */
166413};
166414
166415SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
166416
166417#define fts3GetVarint32(p, piVal) (                                           \
166418  (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
166419)
166420
166421/* fts3.c */
166422SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char**,const char*,...);
166423SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
166424SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
166425SQLITE_PRIVATE int sqlite3Fts3GetVarintU(const char *, sqlite_uint64 *);
166426SQLITE_PRIVATE int sqlite3Fts3GetVarintBounded(const char*,const char*,sqlite3_int64*);
166427SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
166428SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
166429SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
166430SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
166431SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
166432SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
166433SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
166434SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc);
166435SQLITE_PRIVATE int sqlite3Fts3ReadInt(const char *z, int *pnOut);
166436
166437/* fts3_tokenizer.c */
166438SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
166439SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
166440SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
166441    sqlite3_tokenizer **, char **
166442);
166443SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
166444
166445/* fts3_snippet.c */
166446SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
166447SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
166448  const char *, const char *, int, int
166449);
166450SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
166451SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p);
166452
166453/* fts3_expr.c */
166454SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
166455  char **, int, int, int, const char *, int, Fts3Expr **, char **
166456);
166457SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
166458#ifdef SQLITE_TEST
166459SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash*);
166460SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
166461#endif
166462
166463SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
166464  sqlite3_tokenizer_cursor **
166465);
166466
166467/* fts3_aux.c */
166468SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
166469
166470SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
166471
166472SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
166473    Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
166474SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
166475    Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
166476SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
166477SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
166478SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
166479
166480/* fts3_tokenize_vtab.c */
166481SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
166482
166483/* fts3_unicode2.c (functions generated by parsing unicode text files) */
166484#ifndef SQLITE_DISABLE_FTS3_UNICODE
166485SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
166486SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
166487SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
166488#endif
166489
166490#endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
166491#endif /* _FTSINT_H */
166492
166493/************** End of fts3Int.h *********************************************/
166494/************** Continuing where we left off in fts3.c ***********************/
166495#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
166496
166497#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
166498# define SQLITE_CORE 1
166499#endif
166500
166501/* #include <assert.h> */
166502/* #include <stdlib.h> */
166503/* #include <stddef.h> */
166504/* #include <stdio.h> */
166505/* #include <string.h> */
166506/* #include <stdarg.h> */
166507
166508/* #include "fts3.h" */
166509#ifndef SQLITE_CORE
166510/* # include "sqlite3ext.h" */
166511  SQLITE_EXTENSION_INIT1
166512#endif
166513
166514static int fts3EvalNext(Fts3Cursor *pCsr);
166515static int fts3EvalStart(Fts3Cursor *pCsr);
166516static int fts3TermSegReaderCursor(
166517    Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
166518
166519#ifndef SQLITE_AMALGAMATION
166520# if defined(SQLITE_DEBUG)
166521SQLITE_PRIVATE int sqlite3Fts3Always(int b) { assert( b ); return b; }
166522SQLITE_PRIVATE int sqlite3Fts3Never(int b)  { assert( !b ); return b; }
166523# endif
166524#endif
166525
166526/*
166527** This variable is set to false when running tests for which the on disk
166528** structures should not be corrupt. Otherwise, true. If it is false, extra
166529** assert() conditions in the fts3 code are activated - conditions that are
166530** only true if it is guaranteed that the fts3 database is not corrupt.
166531*/
166532SQLITE_API int sqlite3_fts3_may_be_corrupt = 1;
166533
166534/*
166535** Write a 64-bit variable-length integer to memory starting at p[0].
166536** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
166537** The number of bytes written is returned.
166538*/
166539SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
166540  unsigned char *q = (unsigned char *) p;
166541  sqlite_uint64 vu = v;
166542  do{
166543    *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
166544    vu >>= 7;
166545  }while( vu!=0 );
166546  q[-1] &= 0x7f;  /* turn off high bit in final byte */
166547  assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
166548  return (int) (q - (unsigned char *)p);
166549}
166550
166551#define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
166552  v = (v & mask1) | ( (*(const unsigned char*)(ptr++)) << shift );  \
166553  if( (v & mask2)==0 ){ var = v; return ret; }
166554#define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \
166555  v = (*ptr++);                                               \
166556  if( (v & mask2)==0 ){ var = v; return ret; }
166557
166558SQLITE_PRIVATE int sqlite3Fts3GetVarintU(const char *pBuf, sqlite_uint64 *v){
166559  const unsigned char *p = (const unsigned char*)pBuf;
166560  const unsigned char *pStart = p;
166561  u32 a;
166562  u64 b;
166563  int shift;
166564
166565  GETVARINT_INIT(a, p, 0,  0x00,     0x80, *v, 1);
166566  GETVARINT_STEP(a, p, 7,  0x7F,     0x4000, *v, 2);
166567  GETVARINT_STEP(a, p, 14, 0x3FFF,   0x200000, *v, 3);
166568  GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4);
166569  b = (a & 0x0FFFFFFF );
166570
166571  for(shift=28; shift<=63; shift+=7){
166572    u64 c = *p++;
166573    b += (c&0x7F) << shift;
166574    if( (c & 0x80)==0 ) break;
166575  }
166576  *v = b;
166577  return (int)(p - pStart);
166578}
166579
166580/*
166581** Read a 64-bit variable-length integer from memory starting at p[0].
166582** Return the number of bytes read, or 0 on error.
166583** The value is stored in *v.
166584*/
166585SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *pBuf, sqlite_int64 *v){
166586  return sqlite3Fts3GetVarintU(pBuf, (sqlite3_uint64*)v);
166587}
166588
166589/*
166590** Read a 64-bit variable-length integer from memory starting at p[0] and
166591** not extending past pEnd[-1].
166592** Return the number of bytes read, or 0 on error.
166593** The value is stored in *v.
166594*/
166595SQLITE_PRIVATE int sqlite3Fts3GetVarintBounded(
166596  const char *pBuf,
166597  const char *pEnd,
166598  sqlite_int64 *v
166599){
166600  const unsigned char *p = (const unsigned char*)pBuf;
166601  const unsigned char *pStart = p;
166602  const unsigned char *pX = (const unsigned char*)pEnd;
166603  u64 b = 0;
166604  int shift;
166605  for(shift=0; shift<=63; shift+=7){
166606    u64 c = p<pX ? *p : 0;
166607    p++;
166608    b += (c&0x7F) << shift;
166609    if( (c & 0x80)==0 ) break;
166610  }
166611  *v = b;
166612  return (int)(p - pStart);
166613}
166614
166615/*
166616** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to
166617** a non-negative 32-bit integer before it is returned.
166618*/
166619SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
166620  const unsigned char *ptr = (const unsigned char*)p;
166621  u32 a;
166622
166623#ifndef fts3GetVarint32
166624  GETVARINT_INIT(a, ptr, 0,  0x00,     0x80, *pi, 1);
166625#else
166626  a = (*ptr++);
166627  assert( a & 0x80 );
166628#endif
166629
166630  GETVARINT_STEP(a, ptr, 7,  0x7F,     0x4000, *pi, 2);
166631  GETVARINT_STEP(a, ptr, 14, 0x3FFF,   0x200000, *pi, 3);
166632  GETVARINT_STEP(a, ptr, 21, 0x1FFFFF, 0x10000000, *pi, 4);
166633  a = (a & 0x0FFFFFFF );
166634  *pi = (int)(a | ((u32)(*ptr & 0x07) << 28));
166635  assert( 0==(a & 0x80000000) );
166636  assert( *pi>=0 );
166637  return 5;
166638}
166639
166640/*
166641** Return the number of bytes required to encode v as a varint
166642*/
166643SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
166644  int i = 0;
166645  do{
166646    i++;
166647    v >>= 7;
166648  }while( v!=0 );
166649  return i;
166650}
166651
166652/*
166653** Convert an SQL-style quoted string into a normal string by removing
166654** the quote characters.  The conversion is done in-place.  If the
166655** input does not begin with a quote character, then this routine
166656** is a no-op.
166657**
166658** Examples:
166659**
166660**     "abc"   becomes   abc
166661**     'xyz'   becomes   xyz
166662**     [pqr]   becomes   pqr
166663**     `mno`   becomes   mno
166664**
166665*/
166666SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
166667  char quote;                     /* Quote character (if any ) */
166668
166669  quote = z[0];
166670  if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
166671    int iIn = 1;                  /* Index of next byte to read from input */
166672    int iOut = 0;                 /* Index of next byte to write to output */
166673
166674    /* If the first byte was a '[', then the close-quote character is a ']' */
166675    if( quote=='[' ) quote = ']';
166676
166677    while( z[iIn] ){
166678      if( z[iIn]==quote ){
166679        if( z[iIn+1]!=quote ) break;
166680        z[iOut++] = quote;
166681        iIn += 2;
166682      }else{
166683        z[iOut++] = z[iIn++];
166684      }
166685    }
166686    z[iOut] = '\0';
166687  }
166688}
166689
166690/*
166691** Read a single varint from the doclist at *pp and advance *pp to point
166692** to the first byte past the end of the varint.  Add the value of the varint
166693** to *pVal.
166694*/
166695static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
166696  sqlite3_int64 iVal;
166697  *pp += sqlite3Fts3GetVarint(*pp, &iVal);
166698  *pVal += iVal;
166699}
166700
166701/*
166702** When this function is called, *pp points to the first byte following a
166703** varint that is part of a doclist (or position-list, or any other list
166704** of varints). This function moves *pp to point to the start of that varint,
166705** and sets *pVal by the varint value.
166706**
166707** Argument pStart points to the first byte of the doclist that the
166708** varint is part of.
166709*/
166710static void fts3GetReverseVarint(
166711  char **pp,
166712  char *pStart,
166713  sqlite3_int64 *pVal
166714){
166715  sqlite3_int64 iVal;
166716  char *p;
166717
166718  /* Pointer p now points at the first byte past the varint we are
166719  ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
166720  ** clear on character p[-1]. */
166721  for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
166722  p++;
166723  *pp = p;
166724
166725  sqlite3Fts3GetVarint(p, &iVal);
166726  *pVal = iVal;
166727}
166728
166729/*
166730** The xDisconnect() virtual table method.
166731*/
166732static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
166733  Fts3Table *p = (Fts3Table *)pVtab;
166734  int i;
166735
166736  assert( p->nPendingData==0 );
166737  assert( p->pSegments==0 );
166738
166739  /* Free any prepared statements held */
166740  sqlite3_finalize(p->pSeekStmt);
166741  for(i=0; i<SizeofArray(p->aStmt); i++){
166742    sqlite3_finalize(p->aStmt[i]);
166743  }
166744  sqlite3_free(p->zSegmentsTbl);
166745  sqlite3_free(p->zReadExprlist);
166746  sqlite3_free(p->zWriteExprlist);
166747  sqlite3_free(p->zContentTbl);
166748  sqlite3_free(p->zLanguageid);
166749
166750  /* Invoke the tokenizer destructor to free the tokenizer. */
166751  p->pTokenizer->pModule->xDestroy(p->pTokenizer);
166752
166753  sqlite3_free(p);
166754  return SQLITE_OK;
166755}
166756
166757/*
166758** Write an error message into *pzErr
166759*/
166760SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char **pzErr, const char *zFormat, ...){
166761  va_list ap;
166762  sqlite3_free(*pzErr);
166763  va_start(ap, zFormat);
166764  *pzErr = sqlite3_vmprintf(zFormat, ap);
166765  va_end(ap);
166766}
166767
166768/*
166769** Construct one or more SQL statements from the format string given
166770** and then evaluate those statements. The success code is written
166771** into *pRc.
166772**
166773** If *pRc is initially non-zero then this routine is a no-op.
166774*/
166775static void fts3DbExec(
166776  int *pRc,              /* Success code */
166777  sqlite3 *db,           /* Database in which to run SQL */
166778  const char *zFormat,   /* Format string for SQL */
166779  ...                    /* Arguments to the format string */
166780){
166781  va_list ap;
166782  char *zSql;
166783  if( *pRc ) return;
166784  va_start(ap, zFormat);
166785  zSql = sqlite3_vmprintf(zFormat, ap);
166786  va_end(ap);
166787  if( zSql==0 ){
166788    *pRc = SQLITE_NOMEM;
166789  }else{
166790    *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
166791    sqlite3_free(zSql);
166792  }
166793}
166794
166795/*
166796** The xDestroy() virtual table method.
166797*/
166798static int fts3DestroyMethod(sqlite3_vtab *pVtab){
166799  Fts3Table *p = (Fts3Table *)pVtab;
166800  int rc = SQLITE_OK;              /* Return code */
166801  const char *zDb = p->zDb;        /* Name of database (e.g. "main", "temp") */
166802  sqlite3 *db = p->db;             /* Database handle */
166803
166804  /* Drop the shadow tables */
166805  fts3DbExec(&rc, db,
166806    "DROP TABLE IF EXISTS %Q.'%q_segments';"
166807    "DROP TABLE IF EXISTS %Q.'%q_segdir';"
166808    "DROP TABLE IF EXISTS %Q.'%q_docsize';"
166809    "DROP TABLE IF EXISTS %Q.'%q_stat';"
166810    "%s DROP TABLE IF EXISTS %Q.'%q_content';",
166811    zDb, p->zName,
166812    zDb, p->zName,
166813    zDb, p->zName,
166814    zDb, p->zName,
166815    (p->zContentTbl ? "--" : ""), zDb,p->zName
166816  );
166817
166818  /* If everything has worked, invoke fts3DisconnectMethod() to free the
166819  ** memory associated with the Fts3Table structure and return SQLITE_OK.
166820  ** Otherwise, return an SQLite error code.
166821  */
166822  return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
166823}
166824
166825
166826/*
166827** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
166828** passed as the first argument. This is done as part of the xConnect()
166829** and xCreate() methods.
166830**
166831** If *pRc is non-zero when this function is called, it is a no-op.
166832** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
166833** before returning.
166834*/
166835static void fts3DeclareVtab(int *pRc, Fts3Table *p){
166836  if( *pRc==SQLITE_OK ){
166837    int i;                        /* Iterator variable */
166838    int rc;                       /* Return code */
166839    char *zSql;                   /* SQL statement passed to declare_vtab() */
166840    char *zCols;                  /* List of user defined columns */
166841    const char *zLanguageid;
166842
166843    zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
166844    sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
166845
166846    /* Create a list of user columns for the virtual table */
166847    zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
166848    for(i=1; zCols && i<p->nColumn; i++){
166849      zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
166850    }
166851
166852    /* Create the whole "CREATE TABLE" statement to pass to SQLite */
166853    zSql = sqlite3_mprintf(
166854        "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)",
166855        zCols, p->zName, zLanguageid
166856    );
166857    if( !zCols || !zSql ){
166858      rc = SQLITE_NOMEM;
166859    }else{
166860      rc = sqlite3_declare_vtab(p->db, zSql);
166861    }
166862
166863    sqlite3_free(zSql);
166864    sqlite3_free(zCols);
166865    *pRc = rc;
166866  }
166867}
166868
166869/*
166870** Create the %_stat table if it does not already exist.
166871*/
166872SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
166873  fts3DbExec(pRc, p->db,
166874      "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
166875          "(id INTEGER PRIMARY KEY, value BLOB);",
166876      p->zDb, p->zName
166877  );
166878  if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
166879}
166880
166881/*
166882** Create the backing store tables (%_content, %_segments and %_segdir)
166883** required by the FTS3 table passed as the only argument. This is done
166884** as part of the vtab xCreate() method.
166885**
166886** If the p->bHasDocsize boolean is true (indicating that this is an
166887** FTS4 table, not an FTS3 table) then also create the %_docsize and
166888** %_stat tables required by FTS4.
166889*/
166890static int fts3CreateTables(Fts3Table *p){
166891  int rc = SQLITE_OK;             /* Return code */
166892  int i;                          /* Iterator variable */
166893  sqlite3 *db = p->db;            /* The database connection */
166894
166895  if( p->zContentTbl==0 ){
166896    const char *zLanguageid = p->zLanguageid;
166897    char *zContentCols;           /* Columns of %_content table */
166898
166899    /* Create a list of user columns for the content table */
166900    zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
166901    for(i=0; zContentCols && i<p->nColumn; i++){
166902      char *z = p->azColumn[i];
166903      zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
166904    }
166905    if( zLanguageid && zContentCols ){
166906      zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid);
166907    }
166908    if( zContentCols==0 ) rc = SQLITE_NOMEM;
166909
166910    /* Create the content table */
166911    fts3DbExec(&rc, db,
166912       "CREATE TABLE %Q.'%q_content'(%s)",
166913       p->zDb, p->zName, zContentCols
166914    );
166915    sqlite3_free(zContentCols);
166916  }
166917
166918  /* Create other tables */
166919  fts3DbExec(&rc, db,
166920      "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
166921      p->zDb, p->zName
166922  );
166923  fts3DbExec(&rc, db,
166924      "CREATE TABLE %Q.'%q_segdir'("
166925        "level INTEGER,"
166926        "idx INTEGER,"
166927        "start_block INTEGER,"
166928        "leaves_end_block INTEGER,"
166929        "end_block INTEGER,"
166930        "root BLOB,"
166931        "PRIMARY KEY(level, idx)"
166932      ");",
166933      p->zDb, p->zName
166934  );
166935  if( p->bHasDocsize ){
166936    fts3DbExec(&rc, db,
166937        "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
166938        p->zDb, p->zName
166939    );
166940  }
166941  assert( p->bHasStat==p->bFts4 );
166942  if( p->bHasStat ){
166943    sqlite3Fts3CreateStatTable(&rc, p);
166944  }
166945  return rc;
166946}
166947
166948/*
166949** Store the current database page-size in bytes in p->nPgsz.
166950**
166951** If *pRc is non-zero when this function is called, it is a no-op.
166952** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
166953** before returning.
166954*/
166955static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
166956  if( *pRc==SQLITE_OK ){
166957    int rc;                       /* Return code */
166958    char *zSql;                   /* SQL text "PRAGMA %Q.page_size" */
166959    sqlite3_stmt *pStmt;          /* Compiled "PRAGMA %Q.page_size" statement */
166960
166961    zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
166962    if( !zSql ){
166963      rc = SQLITE_NOMEM;
166964    }else{
166965      rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
166966      if( rc==SQLITE_OK ){
166967        sqlite3_step(pStmt);
166968        p->nPgsz = sqlite3_column_int(pStmt, 0);
166969        rc = sqlite3_finalize(pStmt);
166970      }else if( rc==SQLITE_AUTH ){
166971        p->nPgsz = 1024;
166972        rc = SQLITE_OK;
166973      }
166974    }
166975    assert( p->nPgsz>0 || rc!=SQLITE_OK );
166976    sqlite3_free(zSql);
166977    *pRc = rc;
166978  }
166979}
166980
166981/*
166982** "Special" FTS4 arguments are column specifications of the following form:
166983**
166984**   <key> = <value>
166985**
166986** There may not be whitespace surrounding the "=" character. The <value>
166987** term may be quoted, but the <key> may not.
166988*/
166989static int fts3IsSpecialColumn(
166990  const char *z,
166991  int *pnKey,
166992  char **pzValue
166993){
166994  char *zValue;
166995  const char *zCsr = z;
166996
166997  while( *zCsr!='=' ){
166998    if( *zCsr=='\0' ) return 0;
166999    zCsr++;
167000  }
167001
167002  *pnKey = (int)(zCsr-z);
167003  zValue = sqlite3_mprintf("%s", &zCsr[1]);
167004  if( zValue ){
167005    sqlite3Fts3Dequote(zValue);
167006  }
167007  *pzValue = zValue;
167008  return 1;
167009}
167010
167011/*
167012** Append the output of a printf() style formatting to an existing string.
167013*/
167014static void fts3Appendf(
167015  int *pRc,                       /* IN/OUT: Error code */
167016  char **pz,                      /* IN/OUT: Pointer to string buffer */
167017  const char *zFormat,            /* Printf format string to append */
167018  ...                             /* Arguments for printf format string */
167019){
167020  if( *pRc==SQLITE_OK ){
167021    va_list ap;
167022    char *z;
167023    va_start(ap, zFormat);
167024    z = sqlite3_vmprintf(zFormat, ap);
167025    va_end(ap);
167026    if( z && *pz ){
167027      char *z2 = sqlite3_mprintf("%s%s", *pz, z);
167028      sqlite3_free(z);
167029      z = z2;
167030    }
167031    if( z==0 ) *pRc = SQLITE_NOMEM;
167032    sqlite3_free(*pz);
167033    *pz = z;
167034  }
167035}
167036
167037/*
167038** Return a copy of input string zInput enclosed in double-quotes (") and
167039** with all double quote characters escaped. For example:
167040**
167041**     fts3QuoteId("un \"zip\"")   ->    "un \"\"zip\"\""
167042**
167043** The pointer returned points to memory obtained from sqlite3_malloc(). It
167044** is the callers responsibility to call sqlite3_free() to release this
167045** memory.
167046*/
167047static char *fts3QuoteId(char const *zInput){
167048  sqlite3_int64 nRet;
167049  char *zRet;
167050  nRet = 2 + (int)strlen(zInput)*2 + 1;
167051  zRet = sqlite3_malloc64(nRet);
167052  if( zRet ){
167053    int i;
167054    char *z = zRet;
167055    *(z++) = '"';
167056    for(i=0; zInput[i]; i++){
167057      if( zInput[i]=='"' ) *(z++) = '"';
167058      *(z++) = zInput[i];
167059    }
167060    *(z++) = '"';
167061    *(z++) = '\0';
167062  }
167063  return zRet;
167064}
167065
167066/*
167067** Return a list of comma separated SQL expressions and a FROM clause that
167068** could be used in a SELECT statement such as the following:
167069**
167070**     SELECT <list of expressions> FROM %_content AS x ...
167071**
167072** to return the docid, followed by each column of text data in order
167073** from left to write. If parameter zFunc is not NULL, then instead of
167074** being returned directly each column of text data is passed to an SQL
167075** function named zFunc first. For example, if zFunc is "unzip" and the
167076** table has the three user-defined columns "a", "b", and "c", the following
167077** string is returned:
167078**
167079**     "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
167080**
167081** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
167082** is the responsibility of the caller to eventually free it.
167083**
167084** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
167085** a NULL pointer is returned). Otherwise, if an OOM error is encountered
167086** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
167087** no error occurs, *pRc is left unmodified.
167088*/
167089static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
167090  char *zRet = 0;
167091  char *zFree = 0;
167092  char *zFunction;
167093  int i;
167094
167095  if( p->zContentTbl==0 ){
167096    if( !zFunc ){
167097      zFunction = "";
167098    }else{
167099      zFree = zFunction = fts3QuoteId(zFunc);
167100    }
167101    fts3Appendf(pRc, &zRet, "docid");
167102    for(i=0; i<p->nColumn; i++){
167103      fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
167104    }
167105    if( p->zLanguageid ){
167106      fts3Appendf(pRc, &zRet, ", x.%Q", "langid");
167107    }
167108    sqlite3_free(zFree);
167109  }else{
167110    fts3Appendf(pRc, &zRet, "rowid");
167111    for(i=0; i<p->nColumn; i++){
167112      fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
167113    }
167114    if( p->zLanguageid ){
167115      fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
167116    }
167117  }
167118  fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x",
167119      p->zDb,
167120      (p->zContentTbl ? p->zContentTbl : p->zName),
167121      (p->zContentTbl ? "" : "_content")
167122  );
167123  return zRet;
167124}
167125
167126/*
167127** Return a list of N comma separated question marks, where N is the number
167128** of columns in the %_content table (one for the docid plus one for each
167129** user-defined text column).
167130**
167131** If argument zFunc is not NULL, then all but the first question mark
167132** is preceded by zFunc and an open bracket, and followed by a closed
167133** bracket. For example, if zFunc is "zip" and the FTS3 table has three
167134** user-defined text columns, the following string is returned:
167135**
167136**     "?, zip(?), zip(?), zip(?)"
167137**
167138** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
167139** is the responsibility of the caller to eventually free it.
167140**
167141** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
167142** a NULL pointer is returned). Otherwise, if an OOM error is encountered
167143** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
167144** no error occurs, *pRc is left unmodified.
167145*/
167146static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
167147  char *zRet = 0;
167148  char *zFree = 0;
167149  char *zFunction;
167150  int i;
167151
167152  if( !zFunc ){
167153    zFunction = "";
167154  }else{
167155    zFree = zFunction = fts3QuoteId(zFunc);
167156  }
167157  fts3Appendf(pRc, &zRet, "?");
167158  for(i=0; i<p->nColumn; i++){
167159    fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
167160  }
167161  if( p->zLanguageid ){
167162    fts3Appendf(pRc, &zRet, ", ?");
167163  }
167164  sqlite3_free(zFree);
167165  return zRet;
167166}
167167
167168/*
167169** Buffer z contains a positive integer value encoded as utf-8 text.
167170** Decode this value and store it in *pnOut, returning the number of bytes
167171** consumed. If an overflow error occurs return a negative value.
167172*/
167173SQLITE_PRIVATE int sqlite3Fts3ReadInt(const char *z, int *pnOut){
167174  u64 iVal = 0;
167175  int i;
167176  for(i=0; z[i]>='0' && z[i]<='9'; i++){
167177    iVal = iVal*10 + (z[i] - '0');
167178    if( iVal>0x7FFFFFFF ) return -1;
167179  }
167180  *pnOut = (int)iVal;
167181  return i;
167182}
167183
167184/*
167185** This function interprets the string at (*pp) as a non-negative integer
167186** value. It reads the integer and sets *pnOut to the value read, then
167187** sets *pp to point to the byte immediately following the last byte of
167188** the integer value.
167189**
167190** Only decimal digits ('0'..'9') may be part of an integer value.
167191**
167192** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
167193** the output value undefined. Otherwise SQLITE_OK is returned.
167194**
167195** This function is used when parsing the "prefix=" FTS4 parameter.
167196*/
167197static int fts3GobbleInt(const char **pp, int *pnOut){
167198  const int MAX_NPREFIX = 10000000;
167199  int nInt = 0;                   /* Output value */
167200  int nByte;
167201  nByte = sqlite3Fts3ReadInt(*pp, &nInt);
167202  if( nInt>MAX_NPREFIX ){
167203    nInt = 0;
167204  }
167205  if( nByte==0 ){
167206    return SQLITE_ERROR;
167207  }
167208  *pnOut = nInt;
167209  *pp += nByte;
167210  return SQLITE_OK;
167211}
167212
167213/*
167214** This function is called to allocate an array of Fts3Index structures
167215** representing the indexes maintained by the current FTS table. FTS tables
167216** always maintain the main "terms" index, but may also maintain one or
167217** more "prefix" indexes, depending on the value of the "prefix=" parameter
167218** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
167219**
167220** Argument zParam is passed the value of the "prefix=" option if one was
167221** specified, or NULL otherwise.
167222**
167223** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
167224** the allocated array. *pnIndex is set to the number of elements in the
167225** array. If an error does occur, an SQLite error code is returned.
167226**
167227** Regardless of whether or not an error is returned, it is the responsibility
167228** of the caller to call sqlite3_free() on the output array to free it.
167229*/
167230static int fts3PrefixParameter(
167231  const char *zParam,             /* ABC in prefix=ABC parameter to parse */
167232  int *pnIndex,                   /* OUT: size of *apIndex[] array */
167233  struct Fts3Index **apIndex      /* OUT: Array of indexes for this table */
167234){
167235  struct Fts3Index *aIndex;       /* Allocated array */
167236  int nIndex = 1;                 /* Number of entries in array */
167237
167238  if( zParam && zParam[0] ){
167239    const char *p;
167240    nIndex++;
167241    for(p=zParam; *p; p++){
167242      if( *p==',' ) nIndex++;
167243    }
167244  }
167245
167246  aIndex = sqlite3_malloc64(sizeof(struct Fts3Index) * nIndex);
167247  *apIndex = aIndex;
167248  if( !aIndex ){
167249    return SQLITE_NOMEM;
167250  }
167251
167252  memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
167253  if( zParam ){
167254    const char *p = zParam;
167255    int i;
167256    for(i=1; i<nIndex; i++){
167257      int nPrefix = 0;
167258      if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
167259      assert( nPrefix>=0 );
167260      if( nPrefix==0 ){
167261        nIndex--;
167262        i--;
167263      }else{
167264        aIndex[i].nPrefix = nPrefix;
167265      }
167266      p++;
167267    }
167268  }
167269
167270  *pnIndex = nIndex;
167271  return SQLITE_OK;
167272}
167273
167274/*
167275** This function is called when initializing an FTS4 table that uses the
167276** content=xxx option. It determines the number of and names of the columns
167277** of the new FTS4 table.
167278**
167279** The third argument passed to this function is the value passed to the
167280** config=xxx option (i.e. "xxx"). This function queries the database for
167281** a table of that name. If found, the output variables are populated
167282** as follows:
167283**
167284**   *pnCol:   Set to the number of columns table xxx has,
167285**
167286**   *pnStr:   Set to the total amount of space required to store a copy
167287**             of each columns name, including the nul-terminator.
167288**
167289**   *pazCol:  Set to point to an array of *pnCol strings. Each string is
167290**             the name of the corresponding column in table xxx. The array
167291**             and its contents are allocated using a single allocation. It
167292**             is the responsibility of the caller to free this allocation
167293**             by eventually passing the *pazCol value to sqlite3_free().
167294**
167295** If the table cannot be found, an error code is returned and the output
167296** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
167297** returned (and the output variables are undefined).
167298*/
167299static int fts3ContentColumns(
167300  sqlite3 *db,                    /* Database handle */
167301  const char *zDb,                /* Name of db (i.e. "main", "temp" etc.) */
167302  const char *zTbl,               /* Name of content table */
167303  const char ***pazCol,           /* OUT: Malloc'd array of column names */
167304  int *pnCol,                     /* OUT: Size of array *pazCol */
167305  int *pnStr,                     /* OUT: Bytes of string content */
167306  char **pzErr                    /* OUT: error message */
167307){
167308  int rc = SQLITE_OK;             /* Return code */
167309  char *zSql;                     /* "SELECT *" statement on zTbl */
167310  sqlite3_stmt *pStmt = 0;        /* Compiled version of zSql */
167311
167312  zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
167313  if( !zSql ){
167314    rc = SQLITE_NOMEM;
167315  }else{
167316    rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
167317    if( rc!=SQLITE_OK ){
167318      sqlite3Fts3ErrMsg(pzErr, "%s", sqlite3_errmsg(db));
167319    }
167320  }
167321  sqlite3_free(zSql);
167322
167323  if( rc==SQLITE_OK ){
167324    const char **azCol;           /* Output array */
167325    sqlite3_int64 nStr = 0;       /* Size of all column names (incl. 0x00) */
167326    int nCol;                     /* Number of table columns */
167327    int i;                        /* Used to iterate through columns */
167328
167329    /* Loop through the returned columns. Set nStr to the number of bytes of
167330    ** space required to store a copy of each column name, including the
167331    ** nul-terminator byte.  */
167332    nCol = sqlite3_column_count(pStmt);
167333    for(i=0; i<nCol; i++){
167334      const char *zCol = sqlite3_column_name(pStmt, i);
167335      nStr += strlen(zCol) + 1;
167336    }
167337
167338    /* Allocate and populate the array to return. */
167339    azCol = (const char **)sqlite3_malloc64(sizeof(char *) * nCol + nStr);
167340    if( azCol==0 ){
167341      rc = SQLITE_NOMEM;
167342    }else{
167343      char *p = (char *)&azCol[nCol];
167344      for(i=0; i<nCol; i++){
167345        const char *zCol = sqlite3_column_name(pStmt, i);
167346        int n = (int)strlen(zCol)+1;
167347        memcpy(p, zCol, n);
167348        azCol[i] = p;
167349        p += n;
167350      }
167351    }
167352    sqlite3_finalize(pStmt);
167353
167354    /* Set the output variables. */
167355    *pnCol = nCol;
167356    *pnStr = nStr;
167357    *pazCol = azCol;
167358  }
167359
167360  return rc;
167361}
167362
167363/*
167364** This function is the implementation of both the xConnect and xCreate
167365** methods of the FTS3 virtual table.
167366**
167367** The argv[] array contains the following:
167368**
167369**   argv[0]   -> module name  ("fts3" or "fts4")
167370**   argv[1]   -> database name
167371**   argv[2]   -> table name
167372**   argv[...] -> "column name" and other module argument fields.
167373*/
167374static int fts3InitVtab(
167375  int isCreate,                   /* True for xCreate, false for xConnect */
167376  sqlite3 *db,                    /* The SQLite database connection */
167377  void *pAux,                     /* Hash table containing tokenizers */
167378  int argc,                       /* Number of elements in argv array */
167379  const char * const *argv,       /* xCreate/xConnect argument array */
167380  sqlite3_vtab **ppVTab,          /* Write the resulting vtab structure here */
167381  char **pzErr                    /* Write any error message here */
167382){
167383  Fts3Hash *pHash = (Fts3Hash *)pAux;
167384  Fts3Table *p = 0;               /* Pointer to allocated vtab */
167385  int rc = SQLITE_OK;             /* Return code */
167386  int i;                          /* Iterator variable */
167387  sqlite3_int64 nByte;            /* Size of allocation used for *p */
167388  int iCol;                       /* Column index */
167389  int nString = 0;                /* Bytes required to hold all column names */
167390  int nCol = 0;                   /* Number of columns in the FTS table */
167391  char *zCsr;                     /* Space for holding column names */
167392  int nDb;                        /* Bytes required to hold database name */
167393  int nName;                      /* Bytes required to hold table name */
167394  int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
167395  const char **aCol;              /* Array of column names */
167396  sqlite3_tokenizer *pTokenizer = 0;        /* Tokenizer for this table */
167397
167398  int nIndex = 0;                 /* Size of aIndex[] array */
167399  struct Fts3Index *aIndex = 0;   /* Array of indexes for this table */
167400
167401  /* The results of parsing supported FTS4 key=value options: */
167402  int bNoDocsize = 0;             /* True to omit %_docsize table */
167403  int bDescIdx = 0;               /* True to store descending indexes */
167404  char *zPrefix = 0;              /* Prefix parameter value (or NULL) */
167405  char *zCompress = 0;            /* compress=? parameter (or NULL) */
167406  char *zUncompress = 0;          /* uncompress=? parameter (or NULL) */
167407  char *zContent = 0;             /* content=? parameter (or NULL) */
167408  char *zLanguageid = 0;          /* languageid=? parameter (or NULL) */
167409  char **azNotindexed = 0;        /* The set of notindexed= columns */
167410  int nNotindexed = 0;            /* Size of azNotindexed[] array */
167411
167412  assert( strlen(argv[0])==4 );
167413  assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
167414       || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
167415  );
167416
167417  nDb = (int)strlen(argv[1]) + 1;
167418  nName = (int)strlen(argv[2]) + 1;
167419
167420  nByte = sizeof(const char *) * (argc-2);
167421  aCol = (const char **)sqlite3_malloc64(nByte);
167422  if( aCol ){
167423    memset((void*)aCol, 0, nByte);
167424    azNotindexed = (char **)sqlite3_malloc64(nByte);
167425  }
167426  if( azNotindexed ){
167427    memset(azNotindexed, 0, nByte);
167428  }
167429  if( !aCol || !azNotindexed ){
167430    rc = SQLITE_NOMEM;
167431    goto fts3_init_out;
167432  }
167433
167434  /* Loop through all of the arguments passed by the user to the FTS3/4
167435  ** module (i.e. all the column names and special arguments). This loop
167436  ** does the following:
167437  **
167438  **   + Figures out the number of columns the FTSX table will have, and
167439  **     the number of bytes of space that must be allocated to store copies
167440  **     of the column names.
167441  **
167442  **   + If there is a tokenizer specification included in the arguments,
167443  **     initializes the tokenizer pTokenizer.
167444  */
167445  for(i=3; rc==SQLITE_OK && i<argc; i++){
167446    char const *z = argv[i];
167447    int nKey;
167448    char *zVal;
167449
167450    /* Check if this is a tokenizer specification */
167451    if( !pTokenizer
167452     && strlen(z)>8
167453     && 0==sqlite3_strnicmp(z, "tokenize", 8)
167454     && 0==sqlite3Fts3IsIdChar(z[8])
167455    ){
167456      rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
167457    }
167458
167459    /* Check if it is an FTS4 special argument. */
167460    else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
167461      struct Fts4Option {
167462        const char *zOpt;
167463        int nOpt;
167464      } aFts4Opt[] = {
167465        { "matchinfo",   9 },     /* 0 -> MATCHINFO */
167466        { "prefix",      6 },     /* 1 -> PREFIX */
167467        { "compress",    8 },     /* 2 -> COMPRESS */
167468        { "uncompress", 10 },     /* 3 -> UNCOMPRESS */
167469        { "order",       5 },     /* 4 -> ORDER */
167470        { "content",     7 },     /* 5 -> CONTENT */
167471        { "languageid", 10 },     /* 6 -> LANGUAGEID */
167472        { "notindexed", 10 }      /* 7 -> NOTINDEXED */
167473      };
167474
167475      int iOpt;
167476      if( !zVal ){
167477        rc = SQLITE_NOMEM;
167478      }else{
167479        for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
167480          struct Fts4Option *pOp = &aFts4Opt[iOpt];
167481          if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
167482            break;
167483          }
167484        }
167485        switch( iOpt ){
167486          case 0:               /* MATCHINFO */
167487            if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
167488              sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal);
167489              rc = SQLITE_ERROR;
167490            }
167491            bNoDocsize = 1;
167492            break;
167493
167494          case 1:               /* PREFIX */
167495            sqlite3_free(zPrefix);
167496            zPrefix = zVal;
167497            zVal = 0;
167498            break;
167499
167500          case 2:               /* COMPRESS */
167501            sqlite3_free(zCompress);
167502            zCompress = zVal;
167503            zVal = 0;
167504            break;
167505
167506          case 3:               /* UNCOMPRESS */
167507            sqlite3_free(zUncompress);
167508            zUncompress = zVal;
167509            zVal = 0;
167510            break;
167511
167512          case 4:               /* ORDER */
167513            if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3))
167514             && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4))
167515            ){
167516              sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal);
167517              rc = SQLITE_ERROR;
167518            }
167519            bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
167520            break;
167521
167522          case 5:              /* CONTENT */
167523            sqlite3_free(zContent);
167524            zContent = zVal;
167525            zVal = 0;
167526            break;
167527
167528          case 6:              /* LANGUAGEID */
167529            assert( iOpt==6 );
167530            sqlite3_free(zLanguageid);
167531            zLanguageid = zVal;
167532            zVal = 0;
167533            break;
167534
167535          case 7:              /* NOTINDEXED */
167536            azNotindexed[nNotindexed++] = zVal;
167537            zVal = 0;
167538            break;
167539
167540          default:
167541            assert( iOpt==SizeofArray(aFts4Opt) );
167542            sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z);
167543            rc = SQLITE_ERROR;
167544            break;
167545        }
167546        sqlite3_free(zVal);
167547      }
167548    }
167549
167550    /* Otherwise, the argument is a column name. */
167551    else {
167552      nString += (int)(strlen(z) + 1);
167553      aCol[nCol++] = z;
167554    }
167555  }
167556
167557  /* If a content=xxx option was specified, the following:
167558  **
167559  **   1. Ignore any compress= and uncompress= options.
167560  **
167561  **   2. If no column names were specified as part of the CREATE VIRTUAL
167562  **      TABLE statement, use all columns from the content table.
167563  */
167564  if( rc==SQLITE_OK && zContent ){
167565    sqlite3_free(zCompress);
167566    sqlite3_free(zUncompress);
167567    zCompress = 0;
167568    zUncompress = 0;
167569    if( nCol==0 ){
167570      sqlite3_free((void*)aCol);
167571      aCol = 0;
167572      rc = fts3ContentColumns(db, argv[1], zContent,&aCol,&nCol,&nString,pzErr);
167573
167574      /* If a languageid= option was specified, remove the language id
167575      ** column from the aCol[] array. */
167576      if( rc==SQLITE_OK && zLanguageid ){
167577        int j;
167578        for(j=0; j<nCol; j++){
167579          if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
167580            int k;
167581            for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
167582            nCol--;
167583            break;
167584          }
167585        }
167586      }
167587    }
167588  }
167589  if( rc!=SQLITE_OK ) goto fts3_init_out;
167590
167591  if( nCol==0 ){
167592    assert( nString==0 );
167593    aCol[0] = "content";
167594    nString = 8;
167595    nCol = 1;
167596  }
167597
167598  if( pTokenizer==0 ){
167599    rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
167600    if( rc!=SQLITE_OK ) goto fts3_init_out;
167601  }
167602  assert( pTokenizer );
167603
167604  rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
167605  if( rc==SQLITE_ERROR ){
167606    assert( zPrefix );
167607    sqlite3Fts3ErrMsg(pzErr, "error parsing prefix parameter: %s", zPrefix);
167608  }
167609  if( rc!=SQLITE_OK ) goto fts3_init_out;
167610
167611  /* Allocate and populate the Fts3Table structure. */
167612  nByte = sizeof(Fts3Table) +                  /* Fts3Table */
167613          nCol * sizeof(char *) +              /* azColumn */
167614          nIndex * sizeof(struct Fts3Index) +  /* aIndex */
167615          nCol * sizeof(u8) +                  /* abNotindexed */
167616          nName +                              /* zName */
167617          nDb +                                /* zDb */
167618          nString;                             /* Space for azColumn strings */
167619  p = (Fts3Table*)sqlite3_malloc64(nByte);
167620  if( p==0 ){
167621    rc = SQLITE_NOMEM;
167622    goto fts3_init_out;
167623  }
167624  memset(p, 0, nByte);
167625  p->db = db;
167626  p->nColumn = nCol;
167627  p->nPendingData = 0;
167628  p->azColumn = (char **)&p[1];
167629  p->pTokenizer = pTokenizer;
167630  p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
167631  p->bHasDocsize = (isFts4 && bNoDocsize==0);
167632  p->bHasStat = (u8)isFts4;
167633  p->bFts4 = (u8)isFts4;
167634  p->bDescIdx = (u8)bDescIdx;
167635  p->nAutoincrmerge = 0xff;   /* 0xff means setting unknown */
167636  p->zContentTbl = zContent;
167637  p->zLanguageid = zLanguageid;
167638  zContent = 0;
167639  zLanguageid = 0;
167640  TESTONLY( p->inTransaction = -1 );
167641  TESTONLY( p->mxSavepoint = -1 );
167642
167643  p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
167644  memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
167645  p->nIndex = nIndex;
167646  for(i=0; i<nIndex; i++){
167647    fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
167648  }
167649  p->abNotindexed = (u8 *)&p->aIndex[nIndex];
167650
167651  /* Fill in the zName and zDb fields of the vtab structure. */
167652  zCsr = (char *)&p->abNotindexed[nCol];
167653  p->zName = zCsr;
167654  memcpy(zCsr, argv[2], nName);
167655  zCsr += nName;
167656  p->zDb = zCsr;
167657  memcpy(zCsr, argv[1], nDb);
167658  zCsr += nDb;
167659
167660  /* Fill in the azColumn array */
167661  for(iCol=0; iCol<nCol; iCol++){
167662    char *z;
167663    int n = 0;
167664    z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
167665    if( n>0 ){
167666      memcpy(zCsr, z, n);
167667    }
167668    zCsr[n] = '\0';
167669    sqlite3Fts3Dequote(zCsr);
167670    p->azColumn[iCol] = zCsr;
167671    zCsr += n+1;
167672    assert( zCsr <= &((char *)p)[nByte] );
167673  }
167674
167675  /* Fill in the abNotindexed array */
167676  for(iCol=0; iCol<nCol; iCol++){
167677    int n = (int)strlen(p->azColumn[iCol]);
167678    for(i=0; i<nNotindexed; i++){
167679      char *zNot = azNotindexed[i];
167680      if( zNot && n==(int)strlen(zNot)
167681       && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n)
167682      ){
167683        p->abNotindexed[iCol] = 1;
167684        sqlite3_free(zNot);
167685        azNotindexed[i] = 0;
167686      }
167687    }
167688  }
167689  for(i=0; i<nNotindexed; i++){
167690    if( azNotindexed[i] ){
167691      sqlite3Fts3ErrMsg(pzErr, "no such column: %s", azNotindexed[i]);
167692      rc = SQLITE_ERROR;
167693    }
167694  }
167695
167696  if( rc==SQLITE_OK && (zCompress==0)!=(zUncompress==0) ){
167697    char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
167698    rc = SQLITE_ERROR;
167699    sqlite3Fts3ErrMsg(pzErr, "missing %s parameter in fts4 constructor", zMiss);
167700  }
167701  p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
167702  p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
167703  if( rc!=SQLITE_OK ) goto fts3_init_out;
167704
167705  /* If this is an xCreate call, create the underlying tables in the
167706  ** database. TODO: For xConnect(), it could verify that said tables exist.
167707  */
167708  if( isCreate ){
167709    rc = fts3CreateTables(p);
167710  }
167711
167712  /* Check to see if a legacy fts3 table has been "upgraded" by the
167713  ** addition of a %_stat table so that it can use incremental merge.
167714  */
167715  if( !isFts4 && !isCreate ){
167716    p->bHasStat = 2;
167717  }
167718
167719  /* Figure out the page-size for the database. This is required in order to
167720  ** estimate the cost of loading large doclists from the database.  */
167721  fts3DatabasePageSize(&rc, p);
167722  p->nNodeSize = p->nPgsz-35;
167723
167724#if defined(SQLITE_DEBUG)||defined(SQLITE_TEST)
167725  p->nMergeCount = FTS3_MERGE_COUNT;
167726#endif
167727
167728  /* Declare the table schema to SQLite. */
167729  fts3DeclareVtab(&rc, p);
167730
167731fts3_init_out:
167732  sqlite3_free(zPrefix);
167733  sqlite3_free(aIndex);
167734  sqlite3_free(zCompress);
167735  sqlite3_free(zUncompress);
167736  sqlite3_free(zContent);
167737  sqlite3_free(zLanguageid);
167738  for(i=0; i<nNotindexed; i++) sqlite3_free(azNotindexed[i]);
167739  sqlite3_free((void *)aCol);
167740  sqlite3_free((void *)azNotindexed);
167741  if( rc!=SQLITE_OK ){
167742    if( p ){
167743      fts3DisconnectMethod((sqlite3_vtab *)p);
167744    }else if( pTokenizer ){
167745      pTokenizer->pModule->xDestroy(pTokenizer);
167746    }
167747  }else{
167748    assert( p->pSegments==0 );
167749    *ppVTab = &p->base;
167750  }
167751  return rc;
167752}
167753
167754/*
167755** The xConnect() and xCreate() methods for the virtual table. All the
167756** work is done in function fts3InitVtab().
167757*/
167758static int fts3ConnectMethod(
167759  sqlite3 *db,                    /* Database connection */
167760  void *pAux,                     /* Pointer to tokenizer hash table */
167761  int argc,                       /* Number of elements in argv array */
167762  const char * const *argv,       /* xCreate/xConnect argument array */
167763  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
167764  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
167765){
167766  return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
167767}
167768static int fts3CreateMethod(
167769  sqlite3 *db,                    /* Database connection */
167770  void *pAux,                     /* Pointer to tokenizer hash table */
167771  int argc,                       /* Number of elements in argv array */
167772  const char * const *argv,       /* xCreate/xConnect argument array */
167773  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
167774  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
167775){
167776  return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
167777}
167778
167779/*
167780** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
167781** extension is currently being used by a version of SQLite too old to
167782** support estimatedRows. In that case this function is a no-op.
167783*/
167784static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
167785#if SQLITE_VERSION_NUMBER>=3008002
167786  if( sqlite3_libversion_number()>=3008002 ){
167787    pIdxInfo->estimatedRows = nRow;
167788  }
167789#endif
167790}
167791
167792/*
167793** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
167794** extension is currently being used by a version of SQLite too old to
167795** support index-info flags. In that case this function is a no-op.
167796*/
167797static void fts3SetUniqueFlag(sqlite3_index_info *pIdxInfo){
167798#if SQLITE_VERSION_NUMBER>=3008012
167799  if( sqlite3_libversion_number()>=3008012 ){
167800    pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
167801  }
167802#endif
167803}
167804
167805/*
167806** Implementation of the xBestIndex method for FTS3 tables. There
167807** are three possible strategies, in order of preference:
167808**
167809**   1. Direct lookup by rowid or docid.
167810**   2. Full-text search using a MATCH operator on a non-docid column.
167811**   3. Linear scan of %_content table.
167812*/
167813static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
167814  Fts3Table *p = (Fts3Table *)pVTab;
167815  int i;                          /* Iterator variable */
167816  int iCons = -1;                 /* Index of constraint to use */
167817
167818  int iLangidCons = -1;           /* Index of langid=x constraint, if present */
167819  int iDocidGe = -1;              /* Index of docid>=x constraint, if present */
167820  int iDocidLe = -1;              /* Index of docid<=x constraint, if present */
167821  int iIdx;
167822
167823  if( p->bLock ){
167824    return SQLITE_ERROR;
167825  }
167826
167827  /* By default use a full table scan. This is an expensive option,
167828  ** so search through the constraints to see if a more efficient
167829  ** strategy is possible.
167830  */
167831  pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
167832  pInfo->estimatedCost = 5000000;
167833  for(i=0; i<pInfo->nConstraint; i++){
167834    int bDocid;                 /* True if this constraint is on docid */
167835    struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
167836    if( pCons->usable==0 ){
167837      if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
167838        /* There exists an unusable MATCH constraint. This means that if
167839        ** the planner does elect to use the results of this call as part
167840        ** of the overall query plan the user will see an "unable to use
167841        ** function MATCH in the requested context" error. To discourage
167842        ** this, return a very high cost here.  */
167843        pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
167844        pInfo->estimatedCost = 1e50;
167845        fts3SetEstimatedRows(pInfo, ((sqlite3_int64)1) << 50);
167846        return SQLITE_OK;
167847      }
167848      continue;
167849    }
167850
167851    bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
167852
167853    /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
167854    if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){
167855      pInfo->idxNum = FTS3_DOCID_SEARCH;
167856      pInfo->estimatedCost = 1.0;
167857      iCons = i;
167858    }
167859
167860    /* A MATCH constraint. Use a full-text search.
167861    **
167862    ** If there is more than one MATCH constraint available, use the first
167863    ** one encountered. If there is both a MATCH constraint and a direct
167864    ** rowid/docid lookup, prefer the MATCH strategy. This is done even
167865    ** though the rowid/docid lookup is faster than a MATCH query, selecting
167866    ** it would lead to an "unable to use function MATCH in the requested
167867    ** context" error.
167868    */
167869    if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
167870     && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
167871    ){
167872      pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
167873      pInfo->estimatedCost = 2.0;
167874      iCons = i;
167875    }
167876
167877    /* Equality constraint on the langid column */
167878    if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
167879     && pCons->iColumn==p->nColumn + 2
167880    ){
167881      iLangidCons = i;
167882    }
167883
167884    if( bDocid ){
167885      switch( pCons->op ){
167886        case SQLITE_INDEX_CONSTRAINT_GE:
167887        case SQLITE_INDEX_CONSTRAINT_GT:
167888          iDocidGe = i;
167889          break;
167890
167891        case SQLITE_INDEX_CONSTRAINT_LE:
167892        case SQLITE_INDEX_CONSTRAINT_LT:
167893          iDocidLe = i;
167894          break;
167895      }
167896    }
167897  }
167898
167899  /* If using a docid=? or rowid=? strategy, set the UNIQUE flag. */
167900  if( pInfo->idxNum==FTS3_DOCID_SEARCH ) fts3SetUniqueFlag(pInfo);
167901
167902  iIdx = 1;
167903  if( iCons>=0 ){
167904    pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
167905    pInfo->aConstraintUsage[iCons].omit = 1;
167906  }
167907  if( iLangidCons>=0 ){
167908    pInfo->idxNum |= FTS3_HAVE_LANGID;
167909    pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
167910  }
167911  if( iDocidGe>=0 ){
167912    pInfo->idxNum |= FTS3_HAVE_DOCID_GE;
167913    pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
167914  }
167915  if( iDocidLe>=0 ){
167916    pInfo->idxNum |= FTS3_HAVE_DOCID_LE;
167917    pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
167918  }
167919
167920  /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
167921  ** docid) order. Both ascending and descending are possible.
167922  */
167923  if( pInfo->nOrderBy==1 ){
167924    struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
167925    if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
167926      if( pOrder->desc ){
167927        pInfo->idxStr = "DESC";
167928      }else{
167929        pInfo->idxStr = "ASC";
167930      }
167931      pInfo->orderByConsumed = 1;
167932    }
167933  }
167934
167935  assert( p->pSegments==0 );
167936  return SQLITE_OK;
167937}
167938
167939/*
167940** Implementation of xOpen method.
167941*/
167942static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
167943  sqlite3_vtab_cursor *pCsr;               /* Allocated cursor */
167944
167945  UNUSED_PARAMETER(pVTab);
167946
167947  /* Allocate a buffer large enough for an Fts3Cursor structure. If the
167948  ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
167949  ** if the allocation fails, return SQLITE_NOMEM.
167950  */
167951  *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
167952  if( !pCsr ){
167953    return SQLITE_NOMEM;
167954  }
167955  memset(pCsr, 0, sizeof(Fts3Cursor));
167956  return SQLITE_OK;
167957}
167958
167959/*
167960** Finalize the statement handle at pCsr->pStmt.
167961**
167962** Or, if that statement handle is one created by fts3CursorSeekStmt(),
167963** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
167964** pointer there instead of finalizing it.
167965*/
167966static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
167967  if( pCsr->bSeekStmt ){
167968    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
167969    if( p->pSeekStmt==0 ){
167970      p->pSeekStmt = pCsr->pStmt;
167971      sqlite3_reset(pCsr->pStmt);
167972      pCsr->pStmt = 0;
167973    }
167974    pCsr->bSeekStmt = 0;
167975  }
167976  sqlite3_finalize(pCsr->pStmt);
167977}
167978
167979/*
167980** Free all resources currently held by the cursor passed as the only
167981** argument.
167982*/
167983static void fts3ClearCursor(Fts3Cursor *pCsr){
167984  fts3CursorFinalizeStmt(pCsr);
167985  sqlite3Fts3FreeDeferredTokens(pCsr);
167986  sqlite3_free(pCsr->aDoclist);
167987  sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
167988  sqlite3Fts3ExprFree(pCsr->pExpr);
167989  memset(&(&pCsr->base)[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
167990}
167991
167992/*
167993** Close the cursor.  For additional information see the documentation
167994** on the xClose method of the virtual table interface.
167995*/
167996static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
167997  Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
167998  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
167999  fts3ClearCursor(pCsr);
168000  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
168001  sqlite3_free(pCsr);
168002  return SQLITE_OK;
168003}
168004
168005/*
168006** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
168007** compose and prepare an SQL statement of the form:
168008**
168009**    "SELECT <columns> FROM %_content WHERE rowid = ?"
168010**
168011** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
168012** it. If an error occurs, return an SQLite error code.
168013*/
168014static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
168015  int rc = SQLITE_OK;
168016  if( pCsr->pStmt==0 ){
168017    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
168018    char *zSql;
168019    if( p->pSeekStmt ){
168020      pCsr->pStmt = p->pSeekStmt;
168021      p->pSeekStmt = 0;
168022    }else{
168023      zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
168024      if( !zSql ) return SQLITE_NOMEM;
168025      p->bLock++;
168026      rc = sqlite3_prepare_v3(
168027          p->db, zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0
168028      );
168029      p->bLock--;
168030      sqlite3_free(zSql);
168031    }
168032    if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
168033  }
168034  return rc;
168035}
168036
168037/*
168038** Position the pCsr->pStmt statement so that it is on the row
168039** of the %_content table that contains the last match.  Return
168040** SQLITE_OK on success.
168041*/
168042static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
168043  int rc = SQLITE_OK;
168044  if( pCsr->isRequireSeek ){
168045    rc = fts3CursorSeekStmt(pCsr);
168046    if( rc==SQLITE_OK ){
168047      Fts3Table *pTab = (Fts3Table*)pCsr->base.pVtab;
168048      pTab->bLock++;
168049      sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
168050      pCsr->isRequireSeek = 0;
168051      if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
168052        pTab->bLock--;
168053        return SQLITE_OK;
168054      }else{
168055        pTab->bLock--;
168056        rc = sqlite3_reset(pCsr->pStmt);
168057        if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
168058          /* If no row was found and no error has occurred, then the %_content
168059          ** table is missing a row that is present in the full-text index.
168060          ** The data structures are corrupt.  */
168061          rc = FTS_CORRUPT_VTAB;
168062          pCsr->isEof = 1;
168063        }
168064      }
168065    }
168066  }
168067
168068  if( rc!=SQLITE_OK && pContext ){
168069    sqlite3_result_error_code(pContext, rc);
168070  }
168071  return rc;
168072}
168073
168074/*
168075** This function is used to process a single interior node when searching
168076** a b-tree for a term or term prefix. The node data is passed to this
168077** function via the zNode/nNode parameters. The term to search for is
168078** passed in zTerm/nTerm.
168079**
168080** If piFirst is not NULL, then this function sets *piFirst to the blockid
168081** of the child node that heads the sub-tree that may contain the term.
168082**
168083** If piLast is not NULL, then *piLast is set to the right-most child node
168084** that heads a sub-tree that may contain a term for which zTerm/nTerm is
168085** a prefix.
168086**
168087** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
168088*/
168089static int fts3ScanInteriorNode(
168090  const char *zTerm,              /* Term to select leaves for */
168091  int nTerm,                      /* Size of term zTerm in bytes */
168092  const char *zNode,              /* Buffer containing segment interior node */
168093  int nNode,                      /* Size of buffer at zNode */
168094  sqlite3_int64 *piFirst,         /* OUT: Selected child node */
168095  sqlite3_int64 *piLast           /* OUT: Selected child node */
168096){
168097  int rc = SQLITE_OK;             /* Return code */
168098  const char *zCsr = zNode;       /* Cursor to iterate through node */
168099  const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
168100  char *zBuffer = 0;              /* Buffer to load terms into */
168101  i64 nAlloc = 0;                 /* Size of allocated buffer */
168102  int isFirstTerm = 1;            /* True when processing first term on page */
168103  sqlite3_int64 iChild;           /* Block id of child node to descend to */
168104  int nBuffer = 0;                /* Total term size */
168105
168106  /* Skip over the 'height' varint that occurs at the start of every
168107  ** interior node. Then load the blockid of the left-child of the b-tree
168108  ** node into variable iChild.
168109  **
168110  ** Even if the data structure on disk is corrupted, this (reading two
168111  ** varints from the buffer) does not risk an overread. If zNode is a
168112  ** root node, then the buffer comes from a SELECT statement. SQLite does
168113  ** not make this guarantee explicitly, but in practice there are always
168114  ** either more than 20 bytes of allocated space following the nNode bytes of
168115  ** contents, or two zero bytes. Or, if the node is read from the %_segments
168116  ** table, then there are always 20 bytes of zeroed padding following the
168117  ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
168118  */
168119  zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
168120  zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
168121  if( zCsr>zEnd ){
168122    return FTS_CORRUPT_VTAB;
168123  }
168124
168125  while( zCsr<zEnd && (piFirst || piLast) ){
168126    int cmp;                      /* memcmp() result */
168127    int nSuffix;                  /* Size of term suffix */
168128    int nPrefix = 0;              /* Size of term prefix */
168129
168130    /* Load the next term on the node into zBuffer. Use realloc() to expand
168131    ** the size of zBuffer if required.  */
168132    if( !isFirstTerm ){
168133      zCsr += fts3GetVarint32(zCsr, &nPrefix);
168134      if( nPrefix>nBuffer ){
168135        rc = FTS_CORRUPT_VTAB;
168136        goto finish_scan;
168137      }
168138    }
168139    isFirstTerm = 0;
168140    zCsr += fts3GetVarint32(zCsr, &nSuffix);
168141
168142    assert( nPrefix>=0 && nSuffix>=0 );
168143    if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr || nSuffix==0 ){
168144      rc = FTS_CORRUPT_VTAB;
168145      goto finish_scan;
168146    }
168147    if( (i64)nPrefix+nSuffix>nAlloc ){
168148      char *zNew;
168149      nAlloc = ((i64)nPrefix+nSuffix) * 2;
168150      zNew = (char *)sqlite3_realloc64(zBuffer, nAlloc);
168151      if( !zNew ){
168152        rc = SQLITE_NOMEM;
168153        goto finish_scan;
168154      }
168155      zBuffer = zNew;
168156    }
168157    assert( zBuffer );
168158    memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
168159    nBuffer = nPrefix + nSuffix;
168160    zCsr += nSuffix;
168161
168162    /* Compare the term we are searching for with the term just loaded from
168163    ** the interior node. If the specified term is greater than or equal
168164    ** to the term from the interior node, then all terms on the sub-tree
168165    ** headed by node iChild are smaller than zTerm. No need to search
168166    ** iChild.
168167    **
168168    ** If the interior node term is larger than the specified term, then
168169    ** the tree headed by iChild may contain the specified term.
168170    */
168171    cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
168172    if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
168173      *piFirst = iChild;
168174      piFirst = 0;
168175    }
168176
168177    if( piLast && cmp<0 ){
168178      *piLast = iChild;
168179      piLast = 0;
168180    }
168181
168182    iChild++;
168183  };
168184
168185  if( piFirst ) *piFirst = iChild;
168186  if( piLast ) *piLast = iChild;
168187
168188 finish_scan:
168189  sqlite3_free(zBuffer);
168190  return rc;
168191}
168192
168193
168194/*
168195** The buffer pointed to by argument zNode (size nNode bytes) contains an
168196** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
168197** contains a term. This function searches the sub-tree headed by the zNode
168198** node for the range of leaf nodes that may contain the specified term
168199** or terms for which the specified term is a prefix.
168200**
168201** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
168202** left-most leaf node in the tree that may contain the specified term.
168203** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
168204** right-most leaf node that may contain a term for which the specified
168205** term is a prefix.
168206**
168207** It is possible that the range of returned leaf nodes does not contain
168208** the specified term or any terms for which it is a prefix. However, if the
168209** segment does contain any such terms, they are stored within the identified
168210** range. Because this function only inspects interior segment nodes (and
168211** never loads leaf nodes into memory), it is not possible to be sure.
168212**
168213** If an error occurs, an error code other than SQLITE_OK is returned.
168214*/
168215static int fts3SelectLeaf(
168216  Fts3Table *p,                   /* Virtual table handle */
168217  const char *zTerm,              /* Term to select leaves for */
168218  int nTerm,                      /* Size of term zTerm in bytes */
168219  const char *zNode,              /* Buffer containing segment interior node */
168220  int nNode,                      /* Size of buffer at zNode */
168221  sqlite3_int64 *piLeaf,          /* Selected leaf node */
168222  sqlite3_int64 *piLeaf2          /* Selected leaf node */
168223){
168224  int rc = SQLITE_OK;             /* Return code */
168225  int iHeight;                    /* Height of this node in tree */
168226
168227  assert( piLeaf || piLeaf2 );
168228
168229  fts3GetVarint32(zNode, &iHeight);
168230  rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
168231  assert_fts3_nc( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
168232
168233  if( rc==SQLITE_OK && iHeight>1 ){
168234    char *zBlob = 0;              /* Blob read from %_segments table */
168235    int nBlob = 0;                /* Size of zBlob in bytes */
168236
168237    if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
168238      rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
168239      if( rc==SQLITE_OK ){
168240        rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
168241      }
168242      sqlite3_free(zBlob);
168243      piLeaf = 0;
168244      zBlob = 0;
168245    }
168246
168247    if( rc==SQLITE_OK ){
168248      rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
168249    }
168250    if( rc==SQLITE_OK ){
168251      int iNewHeight = 0;
168252      fts3GetVarint32(zBlob, &iNewHeight);
168253      if( iNewHeight>=iHeight ){
168254        rc = FTS_CORRUPT_VTAB;
168255      }else{
168256        rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
168257      }
168258    }
168259    sqlite3_free(zBlob);
168260  }
168261
168262  return rc;
168263}
168264
168265/*
168266** This function is used to create delta-encoded serialized lists of FTS3
168267** varints. Each call to this function appends a single varint to a list.
168268*/
168269static void fts3PutDeltaVarint(
168270  char **pp,                      /* IN/OUT: Output pointer */
168271  sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
168272  sqlite3_int64 iVal              /* Write this value to the list */
168273){
168274  assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
168275  *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
168276  *piPrev = iVal;
168277}
168278
168279/*
168280** When this function is called, *ppPoslist is assumed to point to the
168281** start of a position-list. After it returns, *ppPoslist points to the
168282** first byte after the position-list.
168283**
168284** A position list is list of positions (delta encoded) and columns for
168285** a single document record of a doclist.  So, in other words, this
168286** routine advances *ppPoslist so that it points to the next docid in
168287** the doclist, or to the first byte past the end of the doclist.
168288**
168289** If pp is not NULL, then the contents of the position list are copied
168290** to *pp. *pp is set to point to the first byte past the last byte copied
168291** before this function returns.
168292*/
168293static void fts3PoslistCopy(char **pp, char **ppPoslist){
168294  char *pEnd = *ppPoslist;
168295  char c = 0;
168296
168297  /* The end of a position list is marked by a zero encoded as an FTS3
168298  ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
168299  ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
168300  ** of some other, multi-byte, value.
168301  **
168302  ** The following while-loop moves pEnd to point to the first byte that is not
168303  ** immediately preceded by a byte with the 0x80 bit set. Then increments
168304  ** pEnd once more so that it points to the byte immediately following the
168305  ** last byte in the position-list.
168306  */
168307  while( *pEnd | c ){
168308    c = *pEnd++ & 0x80;
168309    testcase( c!=0 && (*pEnd)==0 );
168310  }
168311  pEnd++;  /* Advance past the POS_END terminator byte */
168312
168313  if( pp ){
168314    int n = (int)(pEnd - *ppPoslist);
168315    char *p = *pp;
168316    memcpy(p, *ppPoslist, n);
168317    p += n;
168318    *pp = p;
168319  }
168320  *ppPoslist = pEnd;
168321}
168322
168323/*
168324** When this function is called, *ppPoslist is assumed to point to the
168325** start of a column-list. After it returns, *ppPoslist points to the
168326** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
168327**
168328** A column-list is list of delta-encoded positions for a single column
168329** within a single document within a doclist.
168330**
168331** The column-list is terminated either by a POS_COLUMN varint (1) or
168332** a POS_END varint (0).  This routine leaves *ppPoslist pointing to
168333** the POS_COLUMN or POS_END that terminates the column-list.
168334**
168335** If pp is not NULL, then the contents of the column-list are copied
168336** to *pp. *pp is set to point to the first byte past the last byte copied
168337** before this function returns.  The POS_COLUMN or POS_END terminator
168338** is not copied into *pp.
168339*/
168340static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
168341  char *pEnd = *ppPoslist;
168342  char c = 0;
168343
168344  /* A column-list is terminated by either a 0x01 or 0x00 byte that is
168345  ** not part of a multi-byte varint.
168346  */
168347  while( 0xFE & (*pEnd | c) ){
168348    c = *pEnd++ & 0x80;
168349    testcase( c!=0 && ((*pEnd)&0xfe)==0 );
168350  }
168351  if( pp ){
168352    int n = (int)(pEnd - *ppPoslist);
168353    char *p = *pp;
168354    memcpy(p, *ppPoslist, n);
168355    p += n;
168356    *pp = p;
168357  }
168358  *ppPoslist = pEnd;
168359}
168360
168361/*
168362** Value used to signify the end of an position-list. This must be
168363** as large or larger than any value that might appear on the
168364** position-list, even a position list that has been corrupted.
168365*/
168366#define POSITION_LIST_END LARGEST_INT64
168367
168368/*
168369** This function is used to help parse position-lists. When this function is
168370** called, *pp may point to the start of the next varint in the position-list
168371** being parsed, or it may point to 1 byte past the end of the position-list
168372** (in which case **pp will be a terminator bytes POS_END (0) or
168373** (1)).
168374**
168375** If *pp points past the end of the current position-list, set *pi to
168376** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
168377** increment the current value of *pi by the value read, and set *pp to
168378** point to the next value before returning.
168379**
168380** Before calling this routine *pi must be initialized to the value of
168381** the previous position, or zero if we are reading the first position
168382** in the position-list.  Because positions are delta-encoded, the value
168383** of the previous position is needed in order to compute the value of
168384** the next position.
168385*/
168386static void fts3ReadNextPos(
168387  char **pp,                    /* IN/OUT: Pointer into position-list buffer */
168388  sqlite3_int64 *pi             /* IN/OUT: Value read from position-list */
168389){
168390  if( (**pp)&0xFE ){
168391    int iVal;
168392    *pp += fts3GetVarint32((*pp), &iVal);
168393    *pi += iVal;
168394    *pi -= 2;
168395  }else{
168396    *pi = POSITION_LIST_END;
168397  }
168398}
168399
168400/*
168401** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
168402** the value of iCol encoded as a varint to *pp.   This will start a new
168403** column list.
168404**
168405** Set *pp to point to the byte just after the last byte written before
168406** returning (do not modify it if iCol==0). Return the total number of bytes
168407** written (0 if iCol==0).
168408*/
168409static int fts3PutColNumber(char **pp, int iCol){
168410  int n = 0;                      /* Number of bytes written */
168411  if( iCol ){
168412    char *p = *pp;                /* Output pointer */
168413    n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
168414    *p = 0x01;
168415    *pp = &p[n];
168416  }
168417  return n;
168418}
168419
168420/*
168421** Compute the union of two position lists.  The output written
168422** into *pp contains all positions of both *pp1 and *pp2 in sorted
168423** order and with any duplicates removed.  All pointers are
168424** updated appropriately.   The caller is responsible for insuring
168425** that there is enough space in *pp to hold the complete output.
168426*/
168427static int fts3PoslistMerge(
168428  char **pp,                      /* Output buffer */
168429  char **pp1,                     /* Left input list */
168430  char **pp2                      /* Right input list */
168431){
168432  char *p = *pp;
168433  char *p1 = *pp1;
168434  char *p2 = *pp2;
168435
168436  while( *p1 || *p2 ){
168437    int iCol1;         /* The current column index in pp1 */
168438    int iCol2;         /* The current column index in pp2 */
168439
168440    if( *p1==POS_COLUMN ){
168441      fts3GetVarint32(&p1[1], &iCol1);
168442      if( iCol1==0 ) return FTS_CORRUPT_VTAB;
168443    }
168444    else if( *p1==POS_END ) iCol1 = 0x7fffffff;
168445    else iCol1 = 0;
168446
168447    if( *p2==POS_COLUMN ){
168448      fts3GetVarint32(&p2[1], &iCol2);
168449      if( iCol2==0 ) return FTS_CORRUPT_VTAB;
168450    }
168451    else if( *p2==POS_END ) iCol2 = 0x7fffffff;
168452    else iCol2 = 0;
168453
168454    if( iCol1==iCol2 ){
168455      sqlite3_int64 i1 = 0;       /* Last position from pp1 */
168456      sqlite3_int64 i2 = 0;       /* Last position from pp2 */
168457      sqlite3_int64 iPrev = 0;
168458      int n = fts3PutColNumber(&p, iCol1);
168459      p1 += n;
168460      p2 += n;
168461
168462      /* At this point, both p1 and p2 point to the start of column-lists
168463      ** for the same column (the column with index iCol1 and iCol2).
168464      ** A column-list is a list of non-negative delta-encoded varints, each
168465      ** incremented by 2 before being stored. Each list is terminated by a
168466      ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
168467      ** and writes the results to buffer p. p is left pointing to the byte
168468      ** after the list written. No terminator (POS_END or POS_COLUMN) is
168469      ** written to the output.
168470      */
168471      fts3GetDeltaVarint(&p1, &i1);
168472      fts3GetDeltaVarint(&p2, &i2);
168473      if( i1<2 || i2<2 ){
168474        break;
168475      }
168476      do {
168477        fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2);
168478        iPrev -= 2;
168479        if( i1==i2 ){
168480          fts3ReadNextPos(&p1, &i1);
168481          fts3ReadNextPos(&p2, &i2);
168482        }else if( i1<i2 ){
168483          fts3ReadNextPos(&p1, &i1);
168484        }else{
168485          fts3ReadNextPos(&p2, &i2);
168486        }
168487      }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
168488    }else if( iCol1<iCol2 ){
168489      p1 += fts3PutColNumber(&p, iCol1);
168490      fts3ColumnlistCopy(&p, &p1);
168491    }else{
168492      p2 += fts3PutColNumber(&p, iCol2);
168493      fts3ColumnlistCopy(&p, &p2);
168494    }
168495  }
168496
168497  *p++ = POS_END;
168498  *pp = p;
168499  *pp1 = p1 + 1;
168500  *pp2 = p2 + 1;
168501  return SQLITE_OK;
168502}
168503
168504/*
168505** This function is used to merge two position lists into one. When it is
168506** called, *pp1 and *pp2 must both point to position lists. A position-list is
168507** the part of a doclist that follows each document id. For example, if a row
168508** contains:
168509**
168510**     'a b c'|'x y z'|'a b b a'
168511**
168512** Then the position list for this row for token 'b' would consist of:
168513**
168514**     0x02 0x01 0x02 0x03 0x03 0x00
168515**
168516** When this function returns, both *pp1 and *pp2 are left pointing to the
168517** byte following the 0x00 terminator of their respective position lists.
168518**
168519** If isSaveLeft is 0, an entry is added to the output position list for
168520** each position in *pp2 for which there exists one or more positions in
168521** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
168522** when the *pp1 token appears before the *pp2 token, but not more than nToken
168523** slots before it.
168524**
168525** e.g. nToken==1 searches for adjacent positions.
168526*/
168527static int fts3PoslistPhraseMerge(
168528  char **pp,                      /* IN/OUT: Preallocated output buffer */
168529  int nToken,                     /* Maximum difference in token positions */
168530  int isSaveLeft,                 /* Save the left position */
168531  int isExact,                    /* If *pp1 is exactly nTokens before *pp2 */
168532  char **pp1,                     /* IN/OUT: Left input list */
168533  char **pp2                      /* IN/OUT: Right input list */
168534){
168535  char *p = *pp;
168536  char *p1 = *pp1;
168537  char *p2 = *pp2;
168538  int iCol1 = 0;
168539  int iCol2 = 0;
168540
168541  /* Never set both isSaveLeft and isExact for the same invocation. */
168542  assert( isSaveLeft==0 || isExact==0 );
168543
168544  assert_fts3_nc( p!=0 && *p1!=0 && *p2!=0 );
168545  if( *p1==POS_COLUMN ){
168546    p1++;
168547    p1 += fts3GetVarint32(p1, &iCol1);
168548  }
168549  if( *p2==POS_COLUMN ){
168550    p2++;
168551    p2 += fts3GetVarint32(p2, &iCol2);
168552  }
168553
168554  while( 1 ){
168555    if( iCol1==iCol2 ){
168556      char *pSave = p;
168557      sqlite3_int64 iPrev = 0;
168558      sqlite3_int64 iPos1 = 0;
168559      sqlite3_int64 iPos2 = 0;
168560
168561      if( iCol1 ){
168562        *p++ = POS_COLUMN;
168563        p += sqlite3Fts3PutVarint(p, iCol1);
168564      }
168565
168566      fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
168567      fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
168568      if( iPos1<0 || iPos2<0 ) break;
168569
168570      while( 1 ){
168571        if( iPos2==iPos1+nToken
168572         || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
168573        ){
168574          sqlite3_int64 iSave;
168575          iSave = isSaveLeft ? iPos1 : iPos2;
168576          fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
168577          pSave = 0;
168578          assert( p );
168579        }
168580        if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
168581          if( (*p2&0xFE)==0 ) break;
168582          fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
168583        }else{
168584          if( (*p1&0xFE)==0 ) break;
168585          fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
168586        }
168587      }
168588
168589      if( pSave ){
168590        assert( pp && p );
168591        p = pSave;
168592      }
168593
168594      fts3ColumnlistCopy(0, &p1);
168595      fts3ColumnlistCopy(0, &p2);
168596      assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
168597      if( 0==*p1 || 0==*p2 ) break;
168598
168599      p1++;
168600      p1 += fts3GetVarint32(p1, &iCol1);
168601      p2++;
168602      p2 += fts3GetVarint32(p2, &iCol2);
168603    }
168604
168605    /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
168606    ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
168607    ** end of the position list, or the 0x01 that precedes the next
168608    ** column-number in the position list.
168609    */
168610    else if( iCol1<iCol2 ){
168611      fts3ColumnlistCopy(0, &p1);
168612      if( 0==*p1 ) break;
168613      p1++;
168614      p1 += fts3GetVarint32(p1, &iCol1);
168615    }else{
168616      fts3ColumnlistCopy(0, &p2);
168617      if( 0==*p2 ) break;
168618      p2++;
168619      p2 += fts3GetVarint32(p2, &iCol2);
168620    }
168621  }
168622
168623  fts3PoslistCopy(0, &p2);
168624  fts3PoslistCopy(0, &p1);
168625  *pp1 = p1;
168626  *pp2 = p2;
168627  if( *pp==p ){
168628    return 0;
168629  }
168630  *p++ = 0x00;
168631  *pp = p;
168632  return 1;
168633}
168634
168635/*
168636** Merge two position-lists as required by the NEAR operator. The argument
168637** position lists correspond to the left and right phrases of an expression
168638** like:
168639**
168640**     "phrase 1" NEAR "phrase number 2"
168641**
168642** Position list *pp1 corresponds to the left-hand side of the NEAR
168643** expression and *pp2 to the right. As usual, the indexes in the position
168644** lists are the offsets of the last token in each phrase (tokens "1" and "2"
168645** in the example above).
168646**
168647** The output position list - written to *pp - is a copy of *pp2 with those
168648** entries that are not sufficiently NEAR entries in *pp1 removed.
168649*/
168650static int fts3PoslistNearMerge(
168651  char **pp,                      /* Output buffer */
168652  char *aTmp,                     /* Temporary buffer space */
168653  int nRight,                     /* Maximum difference in token positions */
168654  int nLeft,                      /* Maximum difference in token positions */
168655  char **pp1,                     /* IN/OUT: Left input list */
168656  char **pp2                      /* IN/OUT: Right input list */
168657){
168658  char *p1 = *pp1;
168659  char *p2 = *pp2;
168660
168661  char *pTmp1 = aTmp;
168662  char *pTmp2;
168663  char *aTmp2;
168664  int res = 1;
168665
168666  fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
168667  aTmp2 = pTmp2 = pTmp1;
168668  *pp1 = p1;
168669  *pp2 = p2;
168670  fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
168671  if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
168672    fts3PoslistMerge(pp, &aTmp, &aTmp2);
168673  }else if( pTmp1!=aTmp ){
168674    fts3PoslistCopy(pp, &aTmp);
168675  }else if( pTmp2!=aTmp2 ){
168676    fts3PoslistCopy(pp, &aTmp2);
168677  }else{
168678    res = 0;
168679  }
168680
168681  return res;
168682}
168683
168684/*
168685** An instance of this function is used to merge together the (potentially
168686** large number of) doclists for each term that matches a prefix query.
168687** See function fts3TermSelectMerge() for details.
168688*/
168689typedef struct TermSelect TermSelect;
168690struct TermSelect {
168691  char *aaOutput[16];             /* Malloc'd output buffers */
168692  int anOutput[16];               /* Size each output buffer in bytes */
168693};
168694
168695/*
168696** This function is used to read a single varint from a buffer. Parameter
168697** pEnd points 1 byte past the end of the buffer. When this function is
168698** called, if *pp points to pEnd or greater, then the end of the buffer
168699** has been reached. In this case *pp is set to 0 and the function returns.
168700**
168701** If *pp does not point to or past pEnd, then a single varint is read
168702** from *pp. *pp is then set to point 1 byte past the end of the read varint.
168703**
168704** If bDescIdx is false, the value read is added to *pVal before returning.
168705** If it is true, the value read is subtracted from *pVal before this
168706** function returns.
168707*/
168708static void fts3GetDeltaVarint3(
168709  char **pp,                      /* IN/OUT: Point to read varint from */
168710  char *pEnd,                     /* End of buffer */
168711  int bDescIdx,                   /* True if docids are descending */
168712  sqlite3_int64 *pVal             /* IN/OUT: Integer value */
168713){
168714  if( *pp>=pEnd ){
168715    *pp = 0;
168716  }else{
168717    u64 iVal;
168718    *pp += sqlite3Fts3GetVarintU(*pp, &iVal);
168719    if( bDescIdx ){
168720      *pVal = (i64)((u64)*pVal - iVal);
168721    }else{
168722      *pVal = (i64)((u64)*pVal + iVal);
168723    }
168724  }
168725}
168726
168727/*
168728** This function is used to write a single varint to a buffer. The varint
168729** is written to *pp. Before returning, *pp is set to point 1 byte past the
168730** end of the value written.
168731**
168732** If *pbFirst is zero when this function is called, the value written to
168733** the buffer is that of parameter iVal.
168734**
168735** If *pbFirst is non-zero when this function is called, then the value
168736** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
168737** (if bDescIdx is non-zero).
168738**
168739** Before returning, this function always sets *pbFirst to 1 and *piPrev
168740** to the value of parameter iVal.
168741*/
168742static void fts3PutDeltaVarint3(
168743  char **pp,                      /* IN/OUT: Output pointer */
168744  int bDescIdx,                   /* True for descending docids */
168745  sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
168746  int *pbFirst,                   /* IN/OUT: True after first int written */
168747  sqlite3_int64 iVal              /* Write this value to the list */
168748){
168749  sqlite3_uint64 iWrite;
168750  if( bDescIdx==0 || *pbFirst==0 ){
168751    assert_fts3_nc( *pbFirst==0 || iVal>=*piPrev );
168752    iWrite = (u64)iVal - (u64)*piPrev;
168753  }else{
168754    assert_fts3_nc( *piPrev>=iVal );
168755    iWrite = (u64)*piPrev - (u64)iVal;
168756  }
168757  assert( *pbFirst || *piPrev==0 );
168758  assert_fts3_nc( *pbFirst==0 || iWrite>0 );
168759  *pp += sqlite3Fts3PutVarint(*pp, iWrite);
168760  *piPrev = iVal;
168761  *pbFirst = 1;
168762}
168763
168764
168765/*
168766** This macro is used by various functions that merge doclists. The two
168767** arguments are 64-bit docid values. If the value of the stack variable
168768** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
168769** Otherwise, (i2-i1).
168770**
168771** Using this makes it easier to write code that can merge doclists that are
168772** sorted in either ascending or descending order.
168773*/
168774/* #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i64)((u64)i1-i2)) */
168775#define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1>i2?1:((i1==i2)?0:-1)))
168776
168777/*
168778** This function does an "OR" merge of two doclists (output contains all
168779** positions contained in either argument doclist). If the docids in the
168780** input doclists are sorted in ascending order, parameter bDescDoclist
168781** should be false. If they are sorted in ascending order, it should be
168782** passed a non-zero value.
168783**
168784** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
168785** containing the output doclist and SQLITE_OK is returned. In this case
168786** *pnOut is set to the number of bytes in the output doclist.
168787**
168788** If an error occurs, an SQLite error code is returned. The output values
168789** are undefined in this case.
168790*/
168791static int fts3DoclistOrMerge(
168792  int bDescDoclist,               /* True if arguments are desc */
168793  char *a1, int n1,               /* First doclist */
168794  char *a2, int n2,               /* Second doclist */
168795  char **paOut, int *pnOut        /* OUT: Malloc'd doclist */
168796){
168797  int rc = SQLITE_OK;
168798  sqlite3_int64 i1 = 0;
168799  sqlite3_int64 i2 = 0;
168800  sqlite3_int64 iPrev = 0;
168801  char *pEnd1 = &a1[n1];
168802  char *pEnd2 = &a2[n2];
168803  char *p1 = a1;
168804  char *p2 = a2;
168805  char *p;
168806  char *aOut;
168807  int bFirstOut = 0;
168808
168809  *paOut = 0;
168810  *pnOut = 0;
168811
168812  /* Allocate space for the output. Both the input and output doclists
168813  ** are delta encoded. If they are in ascending order (bDescDoclist==0),
168814  ** then the first docid in each list is simply encoded as a varint. For
168815  ** each subsequent docid, the varint stored is the difference between the
168816  ** current and previous docid (a positive number - since the list is in
168817  ** ascending order).
168818  **
168819  ** The first docid written to the output is therefore encoded using the
168820  ** same number of bytes as it is in whichever of the input lists it is
168821  ** read from. And each subsequent docid read from the same input list
168822  ** consumes either the same or less bytes as it did in the input (since
168823  ** the difference between it and the previous value in the output must
168824  ** be a positive value less than or equal to the delta value read from
168825  ** the input list). The same argument applies to all but the first docid
168826  ** read from the 'other' list. And to the contents of all position lists
168827  ** that will be copied and merged from the input to the output.
168828  **
168829  ** However, if the first docid copied to the output is a negative number,
168830  ** then the encoding of the first docid from the 'other' input list may
168831  ** be larger in the output than it was in the input (since the delta value
168832  ** may be a larger positive integer than the actual docid).
168833  **
168834  ** The space required to store the output is therefore the sum of the
168835  ** sizes of the two inputs, plus enough space for exactly one of the input
168836  ** docids to grow.
168837  **
168838  ** A symetric argument may be made if the doclists are in descending
168839  ** order.
168840  */
168841  aOut = sqlite3_malloc64((i64)n1+n2+FTS3_VARINT_MAX-1+FTS3_BUFFER_PADDING);
168842  if( !aOut ) return SQLITE_NOMEM;
168843
168844  p = aOut;
168845  fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
168846  fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
168847  while( p1 || p2 ){
168848    sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
168849
168850    if( p2 && p1 && iDiff==0 ){
168851      fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
168852      rc = fts3PoslistMerge(&p, &p1, &p2);
168853      if( rc ) break;
168854      fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
168855      fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
168856    }else if( !p2 || (p1 && iDiff<0) ){
168857      fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
168858      fts3PoslistCopy(&p, &p1);
168859      fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
168860    }else{
168861      fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
168862      fts3PoslistCopy(&p, &p2);
168863      fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
168864    }
168865
168866    assert( (p-aOut)<=((p1?(p1-a1):n1)+(p2?(p2-a2):n2)+FTS3_VARINT_MAX-1) );
168867  }
168868
168869  if( rc!=SQLITE_OK ){
168870    sqlite3_free(aOut);
168871    p = aOut = 0;
168872  }else{
168873    assert( (p-aOut)<=n1+n2+FTS3_VARINT_MAX-1 );
168874    memset(&aOut[(p-aOut)], 0, FTS3_BUFFER_PADDING);
168875  }
168876  *paOut = aOut;
168877  *pnOut = (int)(p-aOut);
168878  return rc;
168879}
168880
168881/*
168882** This function does a "phrase" merge of two doclists. In a phrase merge,
168883** the output contains a copy of each position from the right-hand input
168884** doclist for which there is a position in the left-hand input doclist
168885** exactly nDist tokens before it.
168886**
168887** If the docids in the input doclists are sorted in ascending order,
168888** parameter bDescDoclist should be false. If they are sorted in ascending
168889** order, it should be passed a non-zero value.
168890**
168891** The right-hand input doclist is overwritten by this function.
168892*/
168893static int fts3DoclistPhraseMerge(
168894  int bDescDoclist,               /* True if arguments are desc */
168895  int nDist,                      /* Distance from left to right (1=adjacent) */
168896  char *aLeft, int nLeft,         /* Left doclist */
168897  char **paRight, int *pnRight    /* IN/OUT: Right/output doclist */
168898){
168899  sqlite3_int64 i1 = 0;
168900  sqlite3_int64 i2 = 0;
168901  sqlite3_int64 iPrev = 0;
168902  char *aRight = *paRight;
168903  char *pEnd1 = &aLeft[nLeft];
168904  char *pEnd2 = &aRight[*pnRight];
168905  char *p1 = aLeft;
168906  char *p2 = aRight;
168907  char *p;
168908  int bFirstOut = 0;
168909  char *aOut;
168910
168911  assert( nDist>0 );
168912  if( bDescDoclist ){
168913    aOut = sqlite3_malloc64((sqlite3_int64)*pnRight + FTS3_VARINT_MAX);
168914    if( aOut==0 ) return SQLITE_NOMEM;
168915  }else{
168916    aOut = aRight;
168917  }
168918  p = aOut;
168919
168920  fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
168921  fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
168922
168923  while( p1 && p2 ){
168924    sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
168925    if( iDiff==0 ){
168926      char *pSave = p;
168927      sqlite3_int64 iPrevSave = iPrev;
168928      int bFirstOutSave = bFirstOut;
168929
168930      fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
168931      if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
168932        p = pSave;
168933        iPrev = iPrevSave;
168934        bFirstOut = bFirstOutSave;
168935      }
168936      fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
168937      fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
168938    }else if( iDiff<0 ){
168939      fts3PoslistCopy(0, &p1);
168940      fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
168941    }else{
168942      fts3PoslistCopy(0, &p2);
168943      fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
168944    }
168945  }
168946
168947  *pnRight = (int)(p - aOut);
168948  if( bDescDoclist ){
168949    sqlite3_free(aRight);
168950    *paRight = aOut;
168951  }
168952
168953  return SQLITE_OK;
168954}
168955
168956/*
168957** Argument pList points to a position list nList bytes in size. This
168958** function checks to see if the position list contains any entries for
168959** a token in position 0 (of any column). If so, it writes argument iDelta
168960** to the output buffer pOut, followed by a position list consisting only
168961** of the entries from pList at position 0, and terminated by an 0x00 byte.
168962** The value returned is the number of bytes written to pOut (if any).
168963*/
168964SQLITE_PRIVATE int sqlite3Fts3FirstFilter(
168965  sqlite3_int64 iDelta,           /* Varint that may be written to pOut */
168966  char *pList,                    /* Position list (no 0x00 term) */
168967  int nList,                      /* Size of pList in bytes */
168968  char *pOut                      /* Write output here */
168969){
168970  int nOut = 0;
168971  int bWritten = 0;               /* True once iDelta has been written */
168972  char *p = pList;
168973  char *pEnd = &pList[nList];
168974
168975  if( *p!=0x01 ){
168976    if( *p==0x02 ){
168977      nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
168978      pOut[nOut++] = 0x02;
168979      bWritten = 1;
168980    }
168981    fts3ColumnlistCopy(0, &p);
168982  }
168983
168984  while( p<pEnd ){
168985    sqlite3_int64 iCol;
168986    p++;
168987    p += sqlite3Fts3GetVarint(p, &iCol);
168988    if( *p==0x02 ){
168989      if( bWritten==0 ){
168990        nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
168991        bWritten = 1;
168992      }
168993      pOut[nOut++] = 0x01;
168994      nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
168995      pOut[nOut++] = 0x02;
168996    }
168997    fts3ColumnlistCopy(0, &p);
168998  }
168999  if( bWritten ){
169000    pOut[nOut++] = 0x00;
169001  }
169002
169003  return nOut;
169004}
169005
169006
169007/*
169008** Merge all doclists in the TermSelect.aaOutput[] array into a single
169009** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
169010** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
169011**
169012** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
169013** the responsibility of the caller to free any doclists left in the
169014** TermSelect.aaOutput[] array.
169015*/
169016static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
169017  char *aOut = 0;
169018  int nOut = 0;
169019  int i;
169020
169021  /* Loop through the doclists in the aaOutput[] array. Merge them all
169022  ** into a single doclist.
169023  */
169024  for(i=0; i<SizeofArray(pTS->aaOutput); i++){
169025    if( pTS->aaOutput[i] ){
169026      if( !aOut ){
169027        aOut = pTS->aaOutput[i];
169028        nOut = pTS->anOutput[i];
169029        pTS->aaOutput[i] = 0;
169030      }else{
169031        int nNew;
169032        char *aNew;
169033
169034        int rc = fts3DoclistOrMerge(p->bDescIdx,
169035            pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
169036        );
169037        if( rc!=SQLITE_OK ){
169038          sqlite3_free(aOut);
169039          return rc;
169040        }
169041
169042        sqlite3_free(pTS->aaOutput[i]);
169043        sqlite3_free(aOut);
169044        pTS->aaOutput[i] = 0;
169045        aOut = aNew;
169046        nOut = nNew;
169047      }
169048    }
169049  }
169050
169051  pTS->aaOutput[0] = aOut;
169052  pTS->anOutput[0] = nOut;
169053  return SQLITE_OK;
169054}
169055
169056/*
169057** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
169058** as the first argument. The merge is an "OR" merge (see function
169059** fts3DoclistOrMerge() for details).
169060**
169061** This function is called with the doclist for each term that matches
169062** a queried prefix. It merges all these doclists into one, the doclist
169063** for the specified prefix. Since there can be a very large number of
169064** doclists to merge, the merging is done pair-wise using the TermSelect
169065** object.
169066**
169067** This function returns SQLITE_OK if the merge is successful, or an
169068** SQLite error code (SQLITE_NOMEM) if an error occurs.
169069*/
169070static int fts3TermSelectMerge(
169071  Fts3Table *p,                   /* FTS table handle */
169072  TermSelect *pTS,                /* TermSelect object to merge into */
169073  char *aDoclist,                 /* Pointer to doclist */
169074  int nDoclist                    /* Size of aDoclist in bytes */
169075){
169076  if( pTS->aaOutput[0]==0 ){
169077    /* If this is the first term selected, copy the doclist to the output
169078    ** buffer using memcpy().
169079    **
169080    ** Add FTS3_VARINT_MAX bytes of unused space to the end of the
169081    ** allocation. This is so as to ensure that the buffer is big enough
169082    ** to hold the current doclist AND'd with any other doclist. If the
169083    ** doclists are stored in order=ASC order, this padding would not be
169084    ** required (since the size of [doclistA AND doclistB] is always less
169085    ** than or equal to the size of [doclistA] in that case). But this is
169086    ** not true for order=DESC. For example, a doclist containing (1, -1)
169087    ** may be smaller than (-1), as in the first example the -1 may be stored
169088    ** as a single-byte delta, whereas in the second it must be stored as a
169089    ** FTS3_VARINT_MAX byte varint.
169090    **
169091    ** Similar padding is added in the fts3DoclistOrMerge() function.
169092    */
169093    pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1);
169094    pTS->anOutput[0] = nDoclist;
169095    if( pTS->aaOutput[0] ){
169096      memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
169097      memset(&pTS->aaOutput[0][nDoclist], 0, FTS3_VARINT_MAX);
169098    }else{
169099      return SQLITE_NOMEM;
169100    }
169101  }else{
169102    char *aMerge = aDoclist;
169103    int nMerge = nDoclist;
169104    int iOut;
169105
169106    for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
169107      if( pTS->aaOutput[iOut]==0 ){
169108        assert( iOut>0 );
169109        pTS->aaOutput[iOut] = aMerge;
169110        pTS->anOutput[iOut] = nMerge;
169111        break;
169112      }else{
169113        char *aNew;
169114        int nNew;
169115
169116        int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
169117            pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
169118        );
169119        if( rc!=SQLITE_OK ){
169120          if( aMerge!=aDoclist ) sqlite3_free(aMerge);
169121          return rc;
169122        }
169123
169124        if( aMerge!=aDoclist ) sqlite3_free(aMerge);
169125        sqlite3_free(pTS->aaOutput[iOut]);
169126        pTS->aaOutput[iOut] = 0;
169127
169128        aMerge = aNew;
169129        nMerge = nNew;
169130        if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
169131          pTS->aaOutput[iOut] = aMerge;
169132          pTS->anOutput[iOut] = nMerge;
169133        }
169134      }
169135    }
169136  }
169137  return SQLITE_OK;
169138}
169139
169140/*
169141** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
169142*/
169143static int fts3SegReaderCursorAppend(
169144  Fts3MultiSegReader *pCsr,
169145  Fts3SegReader *pNew
169146){
169147  if( (pCsr->nSegment%16)==0 ){
169148    Fts3SegReader **apNew;
169149    sqlite3_int64 nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
169150    apNew = (Fts3SegReader **)sqlite3_realloc64(pCsr->apSegment, nByte);
169151    if( !apNew ){
169152      sqlite3Fts3SegReaderFree(pNew);
169153      return SQLITE_NOMEM;
169154    }
169155    pCsr->apSegment = apNew;
169156  }
169157  pCsr->apSegment[pCsr->nSegment++] = pNew;
169158  return SQLITE_OK;
169159}
169160
169161/*
169162** Add seg-reader objects to the Fts3MultiSegReader object passed as the
169163** 8th argument.
169164**
169165** This function returns SQLITE_OK if successful, or an SQLite error code
169166** otherwise.
169167*/
169168static int fts3SegReaderCursor(
169169  Fts3Table *p,                   /* FTS3 table handle */
169170  int iLangid,                    /* Language id */
169171  int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
169172  int iLevel,                     /* Level of segments to scan */
169173  const char *zTerm,              /* Term to query for */
169174  int nTerm,                      /* Size of zTerm in bytes */
169175  int isPrefix,                   /* True for a prefix search */
169176  int isScan,                     /* True to scan from zTerm to EOF */
169177  Fts3MultiSegReader *pCsr        /* Cursor object to populate */
169178){
169179  int rc = SQLITE_OK;             /* Error code */
169180  sqlite3_stmt *pStmt = 0;        /* Statement to iterate through segments */
169181  int rc2;                        /* Result of sqlite3_reset() */
169182
169183  /* If iLevel is less than 0 and this is not a scan, include a seg-reader
169184  ** for the pending-terms. If this is a scan, then this call must be being
169185  ** made by an fts4aux module, not an FTS table. In this case calling
169186  ** Fts3SegReaderPending might segfault, as the data structures used by
169187  ** fts4aux are not completely populated. So it's easiest to filter these
169188  ** calls out here.  */
169189  if( iLevel<0 && p->aIndex && p->iPrevLangid==iLangid ){
169190    Fts3SegReader *pSeg = 0;
169191    rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix||isScan, &pSeg);
169192    if( rc==SQLITE_OK && pSeg ){
169193      rc = fts3SegReaderCursorAppend(pCsr, pSeg);
169194    }
169195  }
169196
169197  if( iLevel!=FTS3_SEGCURSOR_PENDING ){
169198    if( rc==SQLITE_OK ){
169199      rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
169200    }
169201
169202    while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
169203      Fts3SegReader *pSeg = 0;
169204
169205      /* Read the values returned by the SELECT into local variables. */
169206      sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
169207      sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
169208      sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
169209      int nRoot = sqlite3_column_bytes(pStmt, 4);
169210      char const *zRoot = sqlite3_column_blob(pStmt, 4);
169211
169212      /* If zTerm is not NULL, and this segment is not stored entirely on its
169213      ** root node, the range of leaves scanned can be reduced. Do this. */
169214      if( iStartBlock && zTerm && zRoot ){
169215        sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
169216        rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
169217        if( rc!=SQLITE_OK ) goto finished;
169218        if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
169219      }
169220
169221      rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
169222          (isPrefix==0 && isScan==0),
169223          iStartBlock, iLeavesEndBlock,
169224          iEndBlock, zRoot, nRoot, &pSeg
169225      );
169226      if( rc!=SQLITE_OK ) goto finished;
169227      rc = fts3SegReaderCursorAppend(pCsr, pSeg);
169228    }
169229  }
169230
169231 finished:
169232  rc2 = sqlite3_reset(pStmt);
169233  if( rc==SQLITE_DONE ) rc = rc2;
169234
169235  return rc;
169236}
169237
169238/*
169239** Set up a cursor object for iterating through a full-text index or a
169240** single level therein.
169241*/
169242SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
169243  Fts3Table *p,                   /* FTS3 table handle */
169244  int iLangid,                    /* Language-id to search */
169245  int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
169246  int iLevel,                     /* Level of segments to scan */
169247  const char *zTerm,              /* Term to query for */
169248  int nTerm,                      /* Size of zTerm in bytes */
169249  int isPrefix,                   /* True for a prefix search */
169250  int isScan,                     /* True to scan from zTerm to EOF */
169251  Fts3MultiSegReader *pCsr       /* Cursor object to populate */
169252){
169253  assert( iIndex>=0 && iIndex<p->nIndex );
169254  assert( iLevel==FTS3_SEGCURSOR_ALL
169255      ||  iLevel==FTS3_SEGCURSOR_PENDING
169256      ||  iLevel>=0
169257  );
169258  assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
169259  assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
169260  assert( isPrefix==0 || isScan==0 );
169261
169262  memset(pCsr, 0, sizeof(Fts3MultiSegReader));
169263  return fts3SegReaderCursor(
169264      p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
169265  );
169266}
169267
169268/*
169269** In addition to its current configuration, have the Fts3MultiSegReader
169270** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
169271**
169272** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
169273*/
169274static int fts3SegReaderCursorAddZero(
169275  Fts3Table *p,                   /* FTS virtual table handle */
169276  int iLangid,
169277  const char *zTerm,              /* Term to scan doclist of */
169278  int nTerm,                      /* Number of bytes in zTerm */
169279  Fts3MultiSegReader *pCsr        /* Fts3MultiSegReader to modify */
169280){
169281  return fts3SegReaderCursor(p,
169282      iLangid, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr
169283  );
169284}
169285
169286/*
169287** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
169288** if isPrefix is true, to scan the doclist for all terms for which
169289** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
169290** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
169291** an SQLite error code.
169292**
169293** It is the responsibility of the caller to free this object by eventually
169294** passing it to fts3SegReaderCursorFree()
169295**
169296** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
169297** Output parameter *ppSegcsr is set to 0 if an error occurs.
169298*/
169299static int fts3TermSegReaderCursor(
169300  Fts3Cursor *pCsr,               /* Virtual table cursor handle */
169301  const char *zTerm,              /* Term to query for */
169302  int nTerm,                      /* Size of zTerm in bytes */
169303  int isPrefix,                   /* True for a prefix search */
169304  Fts3MultiSegReader **ppSegcsr   /* OUT: Allocated seg-reader cursor */
169305){
169306  Fts3MultiSegReader *pSegcsr;    /* Object to allocate and return */
169307  int rc = SQLITE_NOMEM;          /* Return code */
169308
169309  pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
169310  if( pSegcsr ){
169311    int i;
169312    int bFound = 0;               /* True once an index has been found */
169313    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
169314
169315    if( isPrefix ){
169316      for(i=1; bFound==0 && i<p->nIndex; i++){
169317        if( p->aIndex[i].nPrefix==nTerm ){
169318          bFound = 1;
169319          rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
169320              i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr
169321          );
169322          pSegcsr->bLookup = 1;
169323        }
169324      }
169325
169326      for(i=1; bFound==0 && i<p->nIndex; i++){
169327        if( p->aIndex[i].nPrefix==nTerm+1 ){
169328          bFound = 1;
169329          rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
169330              i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr
169331          );
169332          if( rc==SQLITE_OK ){
169333            rc = fts3SegReaderCursorAddZero(
169334                p, pCsr->iLangid, zTerm, nTerm, pSegcsr
169335            );
169336          }
169337        }
169338      }
169339    }
169340
169341    if( bFound==0 ){
169342      rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
169343          0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr
169344      );
169345      pSegcsr->bLookup = !isPrefix;
169346    }
169347  }
169348
169349  *ppSegcsr = pSegcsr;
169350  return rc;
169351}
169352
169353/*
169354** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
169355*/
169356static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
169357  sqlite3Fts3SegReaderFinish(pSegcsr);
169358  sqlite3_free(pSegcsr);
169359}
169360
169361/*
169362** This function retrieves the doclist for the specified term (or term
169363** prefix) from the database.
169364*/
169365static int fts3TermSelect(
169366  Fts3Table *p,                   /* Virtual table handle */
169367  Fts3PhraseToken *pTok,          /* Token to query for */
169368  int iColumn,                    /* Column to query (or -ve for all columns) */
169369  int *pnOut,                     /* OUT: Size of buffer at *ppOut */
169370  char **ppOut                    /* OUT: Malloced result buffer */
169371){
169372  int rc;                         /* Return code */
169373  Fts3MultiSegReader *pSegcsr;    /* Seg-reader cursor for this term */
169374  TermSelect tsc;                 /* Object for pair-wise doclist merging */
169375  Fts3SegFilter filter;           /* Segment term filter configuration */
169376
169377  pSegcsr = pTok->pSegcsr;
169378  memset(&tsc, 0, sizeof(TermSelect));
169379
169380  filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
169381        | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
169382        | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
169383        | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
169384  filter.iCol = iColumn;
169385  filter.zTerm = pTok->z;
169386  filter.nTerm = pTok->n;
169387
169388  rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
169389  while( SQLITE_OK==rc
169390      && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
169391  ){
169392    rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
169393  }
169394
169395  if( rc==SQLITE_OK ){
169396    rc = fts3TermSelectFinishMerge(p, &tsc);
169397  }
169398  if( rc==SQLITE_OK ){
169399    *ppOut = tsc.aaOutput[0];
169400    *pnOut = tsc.anOutput[0];
169401  }else{
169402    int i;
169403    for(i=0; i<SizeofArray(tsc.aaOutput); i++){
169404      sqlite3_free(tsc.aaOutput[i]);
169405    }
169406  }
169407
169408  fts3SegReaderCursorFree(pSegcsr);
169409  pTok->pSegcsr = 0;
169410  return rc;
169411}
169412
169413/*
169414** This function counts the total number of docids in the doclist stored
169415** in buffer aList[], size nList bytes.
169416**
169417** If the isPoslist argument is true, then it is assumed that the doclist
169418** contains a position-list following each docid. Otherwise, it is assumed
169419** that the doclist is simply a list of docids stored as delta encoded
169420** varints.
169421*/
169422static int fts3DoclistCountDocids(char *aList, int nList){
169423  int nDoc = 0;                   /* Return value */
169424  if( aList ){
169425    char *aEnd = &aList[nList];   /* Pointer to one byte after EOF */
169426    char *p = aList;              /* Cursor */
169427    while( p<aEnd ){
169428      nDoc++;
169429      while( (*p++)&0x80 );     /* Skip docid varint */
169430      fts3PoslistCopy(0, &p);   /* Skip over position list */
169431    }
169432  }
169433
169434  return nDoc;
169435}
169436
169437/*
169438** Advance the cursor to the next row in the %_content table that
169439** matches the search criteria.  For a MATCH search, this will be
169440** the next row that matches. For a full-table scan, this will be
169441** simply the next row in the %_content table.  For a docid lookup,
169442** this routine simply sets the EOF flag.
169443**
169444** Return SQLITE_OK if nothing goes wrong.  SQLITE_OK is returned
169445** even if we reach end-of-file.  The fts3EofMethod() will be called
169446** subsequently to determine whether or not an EOF was hit.
169447*/
169448static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
169449  int rc;
169450  Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
169451  if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
169452    Fts3Table *pTab = (Fts3Table*)pCursor->pVtab;
169453    pTab->bLock++;
169454    if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
169455      pCsr->isEof = 1;
169456      rc = sqlite3_reset(pCsr->pStmt);
169457    }else{
169458      pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
169459      rc = SQLITE_OK;
169460    }
169461    pTab->bLock--;
169462  }else{
169463    rc = fts3EvalNext((Fts3Cursor *)pCursor);
169464  }
169465  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
169466  return rc;
169467}
169468
169469/*
169470** If the numeric type of argument pVal is "integer", then return it
169471** converted to a 64-bit signed integer. Otherwise, return a copy of
169472** the second parameter, iDefault.
169473*/
169474static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){
169475  if( pVal ){
169476    int eType = sqlite3_value_numeric_type(pVal);
169477    if( eType==SQLITE_INTEGER ){
169478      return sqlite3_value_int64(pVal);
169479    }
169480  }
169481  return iDefault;
169482}
169483
169484/*
169485** This is the xFilter interface for the virtual table.  See
169486** the virtual table xFilter method documentation for additional
169487** information.
169488**
169489** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
169490** the %_content table.
169491**
169492** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
169493** in the %_content table.
169494**
169495** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index.  The
169496** column on the left-hand side of the MATCH operator is column
169497** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed.  argv[0] is the right-hand
169498** side of the MATCH operator.
169499*/
169500static int fts3FilterMethod(
169501  sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
169502  int idxNum,                     /* Strategy index */
169503  const char *idxStr,             /* Unused */
169504  int nVal,                       /* Number of elements in apVal */
169505  sqlite3_value **apVal           /* Arguments for the indexing scheme */
169506){
169507  int rc = SQLITE_OK;
169508  char *zSql;                     /* SQL statement used to access %_content */
169509  int eSearch;
169510  Fts3Table *p = (Fts3Table *)pCursor->pVtab;
169511  Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
169512
169513  sqlite3_value *pCons = 0;       /* The MATCH or rowid constraint, if any */
169514  sqlite3_value *pLangid = 0;     /* The "langid = ?" constraint, if any */
169515  sqlite3_value *pDocidGe = 0;    /* The "docid >= ?" constraint, if any */
169516  sqlite3_value *pDocidLe = 0;    /* The "docid <= ?" constraint, if any */
169517  int iIdx;
169518
169519  UNUSED_PARAMETER(idxStr);
169520  UNUSED_PARAMETER(nVal);
169521
169522  if( p->bLock ){
169523    return SQLITE_ERROR;
169524  }
169525
169526  eSearch = (idxNum & 0x0000FFFF);
169527  assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
169528  assert( p->pSegments==0 );
169529
169530  /* Collect arguments into local variables */
169531  iIdx = 0;
169532  if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++];
169533  if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++];
169534  if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
169535  if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
169536  assert( iIdx==nVal );
169537
169538  /* In case the cursor has been used before, clear it now. */
169539  fts3ClearCursor(pCsr);
169540
169541  /* Set the lower and upper bounds on docids to return */
169542  pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
169543  pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64);
169544
169545  if( idxStr ){
169546    pCsr->bDesc = (idxStr[0]=='D');
169547  }else{
169548    pCsr->bDesc = p->bDescIdx;
169549  }
169550  pCsr->eSearch = (i16)eSearch;
169551
169552  if( eSearch!=FTS3_DOCID_SEARCH && eSearch!=FTS3_FULLSCAN_SEARCH ){
169553    int iCol = eSearch-FTS3_FULLTEXT_SEARCH;
169554    const char *zQuery = (const char *)sqlite3_value_text(pCons);
169555
169556    if( zQuery==0 && sqlite3_value_type(pCons)!=SQLITE_NULL ){
169557      return SQLITE_NOMEM;
169558    }
169559
169560    pCsr->iLangid = 0;
169561    if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid);
169562
169563    assert( p->base.zErrMsg==0 );
169564    rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
169565        p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
169566        &p->base.zErrMsg
169567    );
169568    if( rc!=SQLITE_OK ){
169569      return rc;
169570    }
169571
169572    rc = fts3EvalStart(pCsr);
169573    sqlite3Fts3SegmentsClose(p);
169574    if( rc!=SQLITE_OK ) return rc;
169575    pCsr->pNextId = pCsr->aDoclist;
169576    pCsr->iPrevId = 0;
169577  }
169578
169579  /* Compile a SELECT statement for this cursor. For a full-table-scan, the
169580  ** statement loops through all rows of the %_content table. For a
169581  ** full-text query or docid lookup, the statement retrieves a single
169582  ** row by docid.
169583  */
169584  if( eSearch==FTS3_FULLSCAN_SEARCH ){
169585    if( pDocidGe || pDocidLe ){
169586      zSql = sqlite3_mprintf(
169587          "SELECT %s WHERE rowid BETWEEN %lld AND %lld ORDER BY rowid %s",
169588          p->zReadExprlist, pCsr->iMinDocid, pCsr->iMaxDocid,
169589          (pCsr->bDesc ? "DESC" : "ASC")
169590      );
169591    }else{
169592      zSql = sqlite3_mprintf("SELECT %s ORDER BY rowid %s",
169593          p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
169594      );
169595    }
169596    if( zSql ){
169597      p->bLock++;
169598      rc = sqlite3_prepare_v3(
169599          p->db,zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0
169600      );
169601      p->bLock--;
169602      sqlite3_free(zSql);
169603    }else{
169604      rc = SQLITE_NOMEM;
169605    }
169606  }else if( eSearch==FTS3_DOCID_SEARCH ){
169607    rc = fts3CursorSeekStmt(pCsr);
169608    if( rc==SQLITE_OK ){
169609      rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
169610    }
169611  }
169612  if( rc!=SQLITE_OK ) return rc;
169613
169614  return fts3NextMethod(pCursor);
169615}
169616
169617/*
169618** This is the xEof method of the virtual table. SQLite calls this
169619** routine to find out if it has reached the end of a result set.
169620*/
169621static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
169622  Fts3Cursor *pCsr = (Fts3Cursor*)pCursor;
169623  if( pCsr->isEof ){
169624    fts3ClearCursor(pCsr);
169625    pCsr->isEof = 1;
169626  }
169627  return pCsr->isEof;
169628}
169629
169630/*
169631** This is the xRowid method. The SQLite core calls this routine to
169632** retrieve the rowid for the current row of the result set. fts3
169633** exposes %_content.docid as the rowid for the virtual table. The
169634** rowid should be written to *pRowid.
169635*/
169636static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
169637  Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
169638  *pRowid = pCsr->iPrevId;
169639  return SQLITE_OK;
169640}
169641
169642/*
169643** This is the xColumn method, called by SQLite to request a value from
169644** the row that the supplied cursor currently points to.
169645**
169646** If:
169647**
169648**   (iCol <  p->nColumn)   -> The value of the iCol'th user column.
169649**   (iCol == p->nColumn)   -> Magic column with the same name as the table.
169650**   (iCol == p->nColumn+1) -> Docid column
169651**   (iCol == p->nColumn+2) -> Langid column
169652*/
169653static int fts3ColumnMethod(
169654  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
169655  sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
169656  int iCol                        /* Index of column to read value from */
169657){
169658  int rc = SQLITE_OK;             /* Return Code */
169659  Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
169660  Fts3Table *p = (Fts3Table *)pCursor->pVtab;
169661
169662  /* The column value supplied by SQLite must be in range. */
169663  assert( iCol>=0 && iCol<=p->nColumn+2 );
169664
169665  switch( iCol-p->nColumn ){
169666    case 0:
169667      /* The special 'table-name' column */
169668      sqlite3_result_pointer(pCtx, pCsr, "fts3cursor", 0);
169669      break;
169670
169671    case 1:
169672      /* The docid column */
169673      sqlite3_result_int64(pCtx, pCsr->iPrevId);
169674      break;
169675
169676    case 2:
169677      if( pCsr->pExpr ){
169678        sqlite3_result_int64(pCtx, pCsr->iLangid);
169679        break;
169680      }else if( p->zLanguageid==0 ){
169681        sqlite3_result_int(pCtx, 0);
169682        break;
169683      }else{
169684        iCol = p->nColumn;
169685        /* fall-through */
169686      }
169687
169688    default:
169689      /* A user column. Or, if this is a full-table scan, possibly the
169690      ** language-id column. Seek the cursor. */
169691      rc = fts3CursorSeek(0, pCsr);
169692      if( rc==SQLITE_OK && sqlite3_data_count(pCsr->pStmt)-1>iCol ){
169693        sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
169694      }
169695      break;
169696  }
169697
169698  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
169699  return rc;
169700}
169701
169702/*
169703** This function is the implementation of the xUpdate callback used by
169704** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
169705** inserted, updated or deleted.
169706*/
169707static int fts3UpdateMethod(
169708  sqlite3_vtab *pVtab,            /* Virtual table handle */
169709  int nArg,                       /* Size of argument array */
169710  sqlite3_value **apVal,          /* Array of arguments */
169711  sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
169712){
169713  return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
169714}
169715
169716/*
169717** Implementation of xSync() method. Flush the contents of the pending-terms
169718** hash-table to the database.
169719*/
169720static int fts3SyncMethod(sqlite3_vtab *pVtab){
169721
169722  /* Following an incremental-merge operation, assuming that the input
169723  ** segments are not completely consumed (the usual case), they are updated
169724  ** in place to remove the entries that have already been merged. This
169725  ** involves updating the leaf block that contains the smallest unmerged
169726  ** entry and each block (if any) between the leaf and the root node. So
169727  ** if the height of the input segment b-trees is N, and input segments
169728  ** are merged eight at a time, updating the input segments at the end
169729  ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
169730  ** small - often between 0 and 2. So the overhead of the incremental
169731  ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
169732  ** dwarfing the actual productive work accomplished, the incremental merge
169733  ** is only attempted if it will write at least 64 leaf blocks. Hence
169734  ** nMinMerge.
169735  **
169736  ** Of course, updating the input segments also involves deleting a bunch
169737  ** of blocks from the segments table. But this is not considered overhead
169738  ** as it would also be required by a crisis-merge that used the same input
169739  ** segments.
169740  */
169741  const u32 nMinMerge = 64;       /* Minimum amount of incr-merge work to do */
169742
169743  Fts3Table *p = (Fts3Table*)pVtab;
169744  int rc;
169745  i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
169746
169747  rc = sqlite3Fts3PendingTermsFlush(p);
169748  if( rc==SQLITE_OK
169749   && p->nLeafAdd>(nMinMerge/16)
169750   && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
169751  ){
169752    int mxLevel = 0;              /* Maximum relative level value in db */
169753    int A;                        /* Incr-merge parameter A */
169754
169755    rc = sqlite3Fts3MaxLevel(p, &mxLevel);
169756    assert( rc==SQLITE_OK || mxLevel==0 );
169757    A = p->nLeafAdd * mxLevel;
169758    A += (A/2);
169759    if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
169760  }
169761  sqlite3Fts3SegmentsClose(p);
169762  sqlite3_set_last_insert_rowid(p->db, iLastRowid);
169763  return rc;
169764}
169765
169766/*
169767** If it is currently unknown whether or not the FTS table has an %_stat
169768** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
169769** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code
169770** if an error occurs.
169771*/
169772static int fts3SetHasStat(Fts3Table *p){
169773  int rc = SQLITE_OK;
169774  if( p->bHasStat==2 ){
169775    char *zTbl = sqlite3_mprintf("%s_stat", p->zName);
169776    if( zTbl ){
169777      int res = sqlite3_table_column_metadata(p->db, p->zDb, zTbl, 0,0,0,0,0,0);
169778      sqlite3_free(zTbl);
169779      p->bHasStat = (res==SQLITE_OK);
169780    }else{
169781      rc = SQLITE_NOMEM;
169782    }
169783  }
169784  return rc;
169785}
169786
169787/*
169788** Implementation of xBegin() method.
169789*/
169790static int fts3BeginMethod(sqlite3_vtab *pVtab){
169791  Fts3Table *p = (Fts3Table*)pVtab;
169792  UNUSED_PARAMETER(pVtab);
169793  assert( p->pSegments==0 );
169794  assert( p->nPendingData==0 );
169795  assert( p->inTransaction!=1 );
169796  TESTONLY( p->inTransaction = 1 );
169797  TESTONLY( p->mxSavepoint = -1; );
169798  p->nLeafAdd = 0;
169799  return fts3SetHasStat(p);
169800}
169801
169802/*
169803** Implementation of xCommit() method. This is a no-op. The contents of
169804** the pending-terms hash-table have already been flushed into the database
169805** by fts3SyncMethod().
169806*/
169807static int fts3CommitMethod(sqlite3_vtab *pVtab){
169808  TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
169809  UNUSED_PARAMETER(pVtab);
169810  assert( p->nPendingData==0 );
169811  assert( p->inTransaction!=0 );
169812  assert( p->pSegments==0 );
169813  TESTONLY( p->inTransaction = 0 );
169814  TESTONLY( p->mxSavepoint = -1; );
169815  return SQLITE_OK;
169816}
169817
169818/*
169819** Implementation of xRollback(). Discard the contents of the pending-terms
169820** hash-table. Any changes made to the database are reverted by SQLite.
169821*/
169822static int fts3RollbackMethod(sqlite3_vtab *pVtab){
169823  Fts3Table *p = (Fts3Table*)pVtab;
169824  sqlite3Fts3PendingTermsClear(p);
169825  assert( p->inTransaction!=0 );
169826  TESTONLY( p->inTransaction = 0 );
169827  TESTONLY( p->mxSavepoint = -1; );
169828  return SQLITE_OK;
169829}
169830
169831/*
169832** When called, *ppPoslist must point to the byte immediately following the
169833** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
169834** moves *ppPoslist so that it instead points to the first byte of the
169835** same position list.
169836*/
169837static void fts3ReversePoslist(char *pStart, char **ppPoslist){
169838  char *p = &(*ppPoslist)[-2];
169839  char c = 0;
169840
169841  /* Skip backwards passed any trailing 0x00 bytes added by NearTrim() */
169842  while( p>pStart && (c=*p--)==0 );
169843
169844  /* Search backwards for a varint with value zero (the end of the previous
169845  ** poslist). This is an 0x00 byte preceded by some byte that does not
169846  ** have the 0x80 bit set.  */
169847  while( p>pStart && (*p & 0x80) | c ){
169848    c = *p--;
169849  }
169850  assert( p==pStart || c==0 );
169851
169852  /* At this point p points to that preceding byte without the 0x80 bit
169853  ** set. So to find the start of the poslist, skip forward 2 bytes then
169854  ** over a varint.
169855  **
169856  ** Normally. The other case is that p==pStart and the poslist to return
169857  ** is the first in the doclist. In this case do not skip forward 2 bytes.
169858  ** The second part of the if condition (c==0 && *ppPoslist>&p[2])
169859  ** is required for cases where the first byte of a doclist and the
169860  ** doclist is empty. For example, if the first docid is 10, a doclist
169861  ** that begins with:
169862  **
169863  **   0x0A 0x00 <next docid delta varint>
169864  */
169865  if( p>pStart || (c==0 && *ppPoslist>&p[2]) ){ p = &p[2]; }
169866  while( *p++&0x80 );
169867  *ppPoslist = p;
169868}
169869
169870/*
169871** Helper function used by the implementation of the overloaded snippet(),
169872** offsets() and optimize() SQL functions.
169873**
169874** If the value passed as the third argument is a blob of size
169875** sizeof(Fts3Cursor*), then the blob contents are copied to the
169876** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
169877** message is written to context pContext and SQLITE_ERROR returned. The
169878** string passed via zFunc is used as part of the error message.
169879*/
169880static int fts3FunctionArg(
169881  sqlite3_context *pContext,      /* SQL function call context */
169882  const char *zFunc,              /* Function name */
169883  sqlite3_value *pVal,            /* argv[0] passed to function */
169884  Fts3Cursor **ppCsr              /* OUT: Store cursor handle here */
169885){
169886  int rc;
169887  *ppCsr = (Fts3Cursor*)sqlite3_value_pointer(pVal, "fts3cursor");
169888  if( (*ppCsr)!=0 ){
169889    rc = SQLITE_OK;
169890  }else{
169891    char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
169892    sqlite3_result_error(pContext, zErr, -1);
169893    sqlite3_free(zErr);
169894    rc = SQLITE_ERROR;
169895  }
169896  return rc;
169897}
169898
169899/*
169900** Implementation of the snippet() function for FTS3
169901*/
169902static void fts3SnippetFunc(
169903  sqlite3_context *pContext,      /* SQLite function call context */
169904  int nVal,                       /* Size of apVal[] array */
169905  sqlite3_value **apVal           /* Array of arguments */
169906){
169907  Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
169908  const char *zStart = "<b>";
169909  const char *zEnd = "</b>";
169910  const char *zEllipsis = "<b>...</b>";
169911  int iCol = -1;
169912  int nToken = 15;                /* Default number of tokens in snippet */
169913
169914  /* There must be at least one argument passed to this function (otherwise
169915  ** the non-overloaded version would have been called instead of this one).
169916  */
169917  assert( nVal>=1 );
169918
169919  if( nVal>6 ){
169920    sqlite3_result_error(pContext,
169921        "wrong number of arguments to function snippet()", -1);
169922    return;
169923  }
169924  if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
169925
169926  switch( nVal ){
169927    case 6: nToken = sqlite3_value_int(apVal[5]);
169928    case 5: iCol = sqlite3_value_int(apVal[4]);
169929    case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
169930    case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
169931    case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
169932  }
169933  if( !zEllipsis || !zEnd || !zStart ){
169934    sqlite3_result_error_nomem(pContext);
169935  }else if( nToken==0 ){
169936    sqlite3_result_text(pContext, "", -1, SQLITE_STATIC);
169937  }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
169938    sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
169939  }
169940}
169941
169942/*
169943** Implementation of the offsets() function for FTS3
169944*/
169945static void fts3OffsetsFunc(
169946  sqlite3_context *pContext,      /* SQLite function call context */
169947  int nVal,                       /* Size of argument array */
169948  sqlite3_value **apVal           /* Array of arguments */
169949){
169950  Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
169951
169952  UNUSED_PARAMETER(nVal);
169953
169954  assert( nVal==1 );
169955  if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
169956  assert( pCsr );
169957  if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
169958    sqlite3Fts3Offsets(pContext, pCsr);
169959  }
169960}
169961
169962/*
169963** Implementation of the special optimize() function for FTS3. This
169964** function merges all segments in the database to a single segment.
169965** Example usage is:
169966**
169967**   SELECT optimize(t) FROM t LIMIT 1;
169968**
169969** where 't' is the name of an FTS3 table.
169970*/
169971static void fts3OptimizeFunc(
169972  sqlite3_context *pContext,      /* SQLite function call context */
169973  int nVal,                       /* Size of argument array */
169974  sqlite3_value **apVal           /* Array of arguments */
169975){
169976  int rc;                         /* Return code */
169977  Fts3Table *p;                   /* Virtual table handle */
169978  Fts3Cursor *pCursor;            /* Cursor handle passed through apVal[0] */
169979
169980  UNUSED_PARAMETER(nVal);
169981
169982  assert( nVal==1 );
169983  if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
169984  p = (Fts3Table *)pCursor->base.pVtab;
169985  assert( p );
169986
169987  rc = sqlite3Fts3Optimize(p);
169988
169989  switch( rc ){
169990    case SQLITE_OK:
169991      sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
169992      break;
169993    case SQLITE_DONE:
169994      sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
169995      break;
169996    default:
169997      sqlite3_result_error_code(pContext, rc);
169998      break;
169999  }
170000}
170001
170002/*
170003** Implementation of the matchinfo() function for FTS3
170004*/
170005static void fts3MatchinfoFunc(
170006  sqlite3_context *pContext,      /* SQLite function call context */
170007  int nVal,                       /* Size of argument array */
170008  sqlite3_value **apVal           /* Array of arguments */
170009){
170010  Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
170011  assert( nVal==1 || nVal==2 );
170012  if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
170013    const char *zArg = 0;
170014    if( nVal>1 ){
170015      zArg = (const char *)sqlite3_value_text(apVal[1]);
170016    }
170017    sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
170018  }
170019}
170020
170021/*
170022** This routine implements the xFindFunction method for the FTS3
170023** virtual table.
170024*/
170025static int fts3FindFunctionMethod(
170026  sqlite3_vtab *pVtab,            /* Virtual table handle */
170027  int nArg,                       /* Number of SQL function arguments */
170028  const char *zName,              /* Name of SQL function */
170029  void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
170030  void **ppArg                    /* Unused */
170031){
170032  struct Overloaded {
170033    const char *zName;
170034    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
170035  } aOverload[] = {
170036    { "snippet", fts3SnippetFunc },
170037    { "offsets", fts3OffsetsFunc },
170038    { "optimize", fts3OptimizeFunc },
170039    { "matchinfo", fts3MatchinfoFunc },
170040  };
170041  int i;                          /* Iterator variable */
170042
170043  UNUSED_PARAMETER(pVtab);
170044  UNUSED_PARAMETER(nArg);
170045  UNUSED_PARAMETER(ppArg);
170046
170047  for(i=0; i<SizeofArray(aOverload); i++){
170048    if( strcmp(zName, aOverload[i].zName)==0 ){
170049      *pxFunc = aOverload[i].xFunc;
170050      return 1;
170051    }
170052  }
170053
170054  /* No function of the specified name was found. Return 0. */
170055  return 0;
170056}
170057
170058/*
170059** Implementation of FTS3 xRename method. Rename an fts3 table.
170060*/
170061static int fts3RenameMethod(
170062  sqlite3_vtab *pVtab,            /* Virtual table handle */
170063  const char *zName               /* New name of table */
170064){
170065  Fts3Table *p = (Fts3Table *)pVtab;
170066  sqlite3 *db = p->db;            /* Database connection */
170067  int rc;                         /* Return Code */
170068
170069  /* At this point it must be known if the %_stat table exists or not.
170070  ** So bHasStat may not be 2.  */
170071  rc = fts3SetHasStat(p);
170072
170073  /* As it happens, the pending terms table is always empty here. This is
170074  ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction
170075  ** always opens a savepoint transaction. And the xSavepoint() method
170076  ** flushes the pending terms table. But leave the (no-op) call to
170077  ** PendingTermsFlush() in in case that changes.
170078  */
170079  assert( p->nPendingData==0 );
170080  if( rc==SQLITE_OK ){
170081    rc = sqlite3Fts3PendingTermsFlush(p);
170082  }
170083
170084  if( p->zContentTbl==0 ){
170085    fts3DbExec(&rc, db,
170086      "ALTER TABLE %Q.'%q_content'  RENAME TO '%q_content';",
170087      p->zDb, p->zName, zName
170088    );
170089  }
170090
170091  if( p->bHasDocsize ){
170092    fts3DbExec(&rc, db,
170093      "ALTER TABLE %Q.'%q_docsize'  RENAME TO '%q_docsize';",
170094      p->zDb, p->zName, zName
170095    );
170096  }
170097  if( p->bHasStat ){
170098    fts3DbExec(&rc, db,
170099      "ALTER TABLE %Q.'%q_stat'  RENAME TO '%q_stat';",
170100      p->zDb, p->zName, zName
170101    );
170102  }
170103  fts3DbExec(&rc, db,
170104    "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
170105    p->zDb, p->zName, zName
170106  );
170107  fts3DbExec(&rc, db,
170108    "ALTER TABLE %Q.'%q_segdir'   RENAME TO '%q_segdir';",
170109    p->zDb, p->zName, zName
170110  );
170111  return rc;
170112}
170113
170114/*
170115** The xSavepoint() method.
170116**
170117** Flush the contents of the pending-terms table to disk.
170118*/
170119static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
170120  int rc = SQLITE_OK;
170121  UNUSED_PARAMETER(iSavepoint);
170122  assert( ((Fts3Table *)pVtab)->inTransaction );
170123  assert( ((Fts3Table *)pVtab)->mxSavepoint <= iSavepoint );
170124  TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
170125  if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
170126    rc = fts3SyncMethod(pVtab);
170127  }
170128  return rc;
170129}
170130
170131/*
170132** The xRelease() method.
170133**
170134** This is a no-op.
170135*/
170136static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
170137  TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
170138  UNUSED_PARAMETER(iSavepoint);
170139  UNUSED_PARAMETER(pVtab);
170140  assert( p->inTransaction );
170141  assert( p->mxSavepoint >= iSavepoint );
170142  TESTONLY( p->mxSavepoint = iSavepoint-1 );
170143  return SQLITE_OK;
170144}
170145
170146/*
170147** The xRollbackTo() method.
170148**
170149** Discard the contents of the pending terms table.
170150*/
170151static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
170152  Fts3Table *p = (Fts3Table*)pVtab;
170153  UNUSED_PARAMETER(iSavepoint);
170154  assert( p->inTransaction );
170155  TESTONLY( p->mxSavepoint = iSavepoint );
170156  sqlite3Fts3PendingTermsClear(p);
170157  return SQLITE_OK;
170158}
170159
170160/*
170161** Return true if zName is the extension on one of the shadow tables used
170162** by this module.
170163*/
170164static int fts3ShadowName(const char *zName){
170165  static const char *azName[] = {
170166    "content", "docsize", "segdir", "segments", "stat",
170167  };
170168  unsigned int i;
170169  for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
170170    if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
170171  }
170172  return 0;
170173}
170174
170175static const sqlite3_module fts3Module = {
170176  /* iVersion      */ 3,
170177  /* xCreate       */ fts3CreateMethod,
170178  /* xConnect      */ fts3ConnectMethod,
170179  /* xBestIndex    */ fts3BestIndexMethod,
170180  /* xDisconnect   */ fts3DisconnectMethod,
170181  /* xDestroy      */ fts3DestroyMethod,
170182  /* xOpen         */ fts3OpenMethod,
170183  /* xClose        */ fts3CloseMethod,
170184  /* xFilter       */ fts3FilterMethod,
170185  /* xNext         */ fts3NextMethod,
170186  /* xEof          */ fts3EofMethod,
170187  /* xColumn       */ fts3ColumnMethod,
170188  /* xRowid        */ fts3RowidMethod,
170189  /* xUpdate       */ fts3UpdateMethod,
170190  /* xBegin        */ fts3BeginMethod,
170191  /* xSync         */ fts3SyncMethod,
170192  /* xCommit       */ fts3CommitMethod,
170193  /* xRollback     */ fts3RollbackMethod,
170194  /* xFindFunction */ fts3FindFunctionMethod,
170195  /* xRename */       fts3RenameMethod,
170196  /* xSavepoint    */ fts3SavepointMethod,
170197  /* xRelease      */ fts3ReleaseMethod,
170198  /* xRollbackTo   */ fts3RollbackToMethod,
170199  /* xShadowName   */ fts3ShadowName,
170200};
170201
170202/*
170203** This function is registered as the module destructor (called when an
170204** FTS3 enabled database connection is closed). It frees the memory
170205** allocated for the tokenizer hash table.
170206*/
170207static void hashDestroy(void *p){
170208  Fts3Hash *pHash = (Fts3Hash *)p;
170209  sqlite3Fts3HashClear(pHash);
170210  sqlite3_free(pHash);
170211}
170212
170213/*
170214** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
170215** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
170216** respectively. The following three forward declarations are for functions
170217** declared in these files used to retrieve the respective implementations.
170218**
170219** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
170220** to by the argument to point to the "simple" tokenizer implementation.
170221** And so on.
170222*/
170223SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
170224SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
170225#ifndef SQLITE_DISABLE_FTS3_UNICODE
170226SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
170227#endif
170228#ifdef SQLITE_ENABLE_ICU
170229SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
170230#endif
170231
170232/*
170233** Initialize the fts3 extension. If this extension is built as part
170234** of the sqlite library, then this function is called directly by
170235** SQLite. If fts3 is built as a dynamically loadable extension, this
170236** function is called by the sqlite3_extension_init() entry point.
170237*/
170238SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
170239  int rc = SQLITE_OK;
170240  Fts3Hash *pHash = 0;
170241  const sqlite3_tokenizer_module *pSimple = 0;
170242  const sqlite3_tokenizer_module *pPorter = 0;
170243#ifndef SQLITE_DISABLE_FTS3_UNICODE
170244  const sqlite3_tokenizer_module *pUnicode = 0;
170245#endif
170246
170247#ifdef SQLITE_ENABLE_ICU
170248  const sqlite3_tokenizer_module *pIcu = 0;
170249  sqlite3Fts3IcuTokenizerModule(&pIcu);
170250#endif
170251
170252#ifndef SQLITE_DISABLE_FTS3_UNICODE
170253  sqlite3Fts3UnicodeTokenizer(&pUnicode);
170254#endif
170255
170256#ifdef SQLITE_TEST
170257  rc = sqlite3Fts3InitTerm(db);
170258  if( rc!=SQLITE_OK ) return rc;
170259#endif
170260
170261  rc = sqlite3Fts3InitAux(db);
170262  if( rc!=SQLITE_OK ) return rc;
170263
170264  sqlite3Fts3SimpleTokenizerModule(&pSimple);
170265  sqlite3Fts3PorterTokenizerModule(&pPorter);
170266
170267  /* Allocate and initialize the hash-table used to store tokenizers. */
170268  pHash = sqlite3_malloc(sizeof(Fts3Hash));
170269  if( !pHash ){
170270    rc = SQLITE_NOMEM;
170271  }else{
170272    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
170273  }
170274
170275  /* Load the built-in tokenizers into the hash table */
170276  if( rc==SQLITE_OK ){
170277    if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
170278     || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
170279
170280#ifndef SQLITE_DISABLE_FTS3_UNICODE
170281     || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode)
170282#endif
170283#ifdef SQLITE_ENABLE_ICU
170284     || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
170285#endif
170286    ){
170287      rc = SQLITE_NOMEM;
170288    }
170289  }
170290
170291#ifdef SQLITE_TEST
170292  if( rc==SQLITE_OK ){
170293    rc = sqlite3Fts3ExprInitTestInterface(db, pHash);
170294  }
170295#endif
170296
170297  /* Create the virtual table wrapper around the hash-table and overload
170298  ** the four scalar functions. If this is successful, register the
170299  ** module with sqlite.
170300  */
170301  if( SQLITE_OK==rc
170302   && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
170303   && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
170304   && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
170305   && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
170306   && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
170307   && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
170308  ){
170309    rc = sqlite3_create_module_v2(
170310        db, "fts3", &fts3Module, (void *)pHash, hashDestroy
170311    );
170312    if( rc==SQLITE_OK ){
170313      rc = sqlite3_create_module_v2(
170314          db, "fts4", &fts3Module, (void *)pHash, 0
170315      );
170316    }
170317    if( rc==SQLITE_OK ){
170318      rc = sqlite3Fts3InitTok(db, (void *)pHash);
170319    }
170320    return rc;
170321  }
170322
170323
170324  /* An error has occurred. Delete the hash table and return the error code. */
170325  assert( rc!=SQLITE_OK );
170326  if( pHash ){
170327    sqlite3Fts3HashClear(pHash);
170328    sqlite3_free(pHash);
170329  }
170330  return rc;
170331}
170332
170333/*
170334** Allocate an Fts3MultiSegReader for each token in the expression headed
170335** by pExpr.
170336**
170337** An Fts3SegReader object is a cursor that can seek or scan a range of
170338** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
170339** Fts3SegReader objects internally to provide an interface to seek or scan
170340** within the union of all segments of a b-tree. Hence the name.
170341**
170342** If the allocated Fts3MultiSegReader just seeks to a single entry in a
170343** segment b-tree (if the term is not a prefix or it is a prefix for which
170344** there exists prefix b-tree of the right length) then it may be traversed
170345** and merged incrementally. Otherwise, it has to be merged into an in-memory
170346** doclist and then traversed.
170347*/
170348static void fts3EvalAllocateReaders(
170349  Fts3Cursor *pCsr,               /* FTS cursor handle */
170350  Fts3Expr *pExpr,                /* Allocate readers for this expression */
170351  int *pnToken,                   /* OUT: Total number of tokens in phrase. */
170352  int *pnOr,                      /* OUT: Total number of OR nodes in expr. */
170353  int *pRc                        /* IN/OUT: Error code */
170354){
170355  if( pExpr && SQLITE_OK==*pRc ){
170356    if( pExpr->eType==FTSQUERY_PHRASE ){
170357      int i;
170358      int nToken = pExpr->pPhrase->nToken;
170359      *pnToken += nToken;
170360      for(i=0; i<nToken; i++){
170361        Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
170362        int rc = fts3TermSegReaderCursor(pCsr,
170363            pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
170364        );
170365        if( rc!=SQLITE_OK ){
170366          *pRc = rc;
170367          return;
170368        }
170369      }
170370      assert( pExpr->pPhrase->iDoclistToken==0 );
170371      pExpr->pPhrase->iDoclistToken = -1;
170372    }else{
170373      *pnOr += (pExpr->eType==FTSQUERY_OR);
170374      fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
170375      fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
170376    }
170377  }
170378}
170379
170380/*
170381** Arguments pList/nList contain the doclist for token iToken of phrase p.
170382** It is merged into the main doclist stored in p->doclist.aAll/nAll.
170383**
170384** This function assumes that pList points to a buffer allocated using
170385** sqlite3_malloc(). This function takes responsibility for eventually
170386** freeing the buffer.
170387**
170388** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs.
170389*/
170390static int fts3EvalPhraseMergeToken(
170391  Fts3Table *pTab,                /* FTS Table pointer */
170392  Fts3Phrase *p,                  /* Phrase to merge pList/nList into */
170393  int iToken,                     /* Token pList/nList corresponds to */
170394  char *pList,                    /* Pointer to doclist */
170395  int nList                       /* Number of bytes in pList */
170396){
170397  int rc = SQLITE_OK;
170398  assert( iToken!=p->iDoclistToken );
170399
170400  if( pList==0 ){
170401    sqlite3_free(p->doclist.aAll);
170402    p->doclist.aAll = 0;
170403    p->doclist.nAll = 0;
170404  }
170405
170406  else if( p->iDoclistToken<0 ){
170407    p->doclist.aAll = pList;
170408    p->doclist.nAll = nList;
170409  }
170410
170411  else if( p->doclist.aAll==0 ){
170412    sqlite3_free(pList);
170413  }
170414
170415  else {
170416    char *pLeft;
170417    char *pRight;
170418    int nLeft;
170419    int nRight;
170420    int nDiff;
170421
170422    if( p->iDoclistToken<iToken ){
170423      pLeft = p->doclist.aAll;
170424      nLeft = p->doclist.nAll;
170425      pRight = pList;
170426      nRight = nList;
170427      nDiff = iToken - p->iDoclistToken;
170428    }else{
170429      pRight = p->doclist.aAll;
170430      nRight = p->doclist.nAll;
170431      pLeft = pList;
170432      nLeft = nList;
170433      nDiff = p->iDoclistToken - iToken;
170434    }
170435
170436    rc = fts3DoclistPhraseMerge(
170437        pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
170438    );
170439    sqlite3_free(pLeft);
170440    p->doclist.aAll = pRight;
170441    p->doclist.nAll = nRight;
170442  }
170443
170444  if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
170445  return rc;
170446}
170447
170448/*
170449** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
170450** does not take deferred tokens into account.
170451**
170452** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
170453*/
170454static int fts3EvalPhraseLoad(
170455  Fts3Cursor *pCsr,               /* FTS Cursor handle */
170456  Fts3Phrase *p                   /* Phrase object */
170457){
170458  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
170459  int iToken;
170460  int rc = SQLITE_OK;
170461
170462  for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
170463    Fts3PhraseToken *pToken = &p->aToken[iToken];
170464    assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
170465
170466    if( pToken->pSegcsr ){
170467      int nThis = 0;
170468      char *pThis = 0;
170469      rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
170470      if( rc==SQLITE_OK ){
170471        rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
170472      }
170473    }
170474    assert( pToken->pSegcsr==0 );
170475  }
170476
170477  return rc;
170478}
170479
170480#ifndef SQLITE_DISABLE_FTS4_DEFERRED
170481/*
170482** This function is called on each phrase after the position lists for
170483** any deferred tokens have been loaded into memory. It updates the phrases
170484** current position list to include only those positions that are really
170485** instances of the phrase (after considering deferred tokens). If this
170486** means that the phrase does not appear in the current row, doclist.pList
170487** and doclist.nList are both zeroed.
170488**
170489** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
170490*/
170491static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
170492  int iToken;                     /* Used to iterate through phrase tokens */
170493  char *aPoslist = 0;             /* Position list for deferred tokens */
170494  int nPoslist = 0;               /* Number of bytes in aPoslist */
170495  int iPrev = -1;                 /* Token number of previous deferred token */
170496
170497  assert( pPhrase->doclist.bFreeList==0 );
170498
170499  for(iToken=0; iToken<pPhrase->nToken; iToken++){
170500    Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
170501    Fts3DeferredToken *pDeferred = pToken->pDeferred;
170502
170503    if( pDeferred ){
170504      char *pList;
170505      int nList;
170506      int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
170507      if( rc!=SQLITE_OK ) return rc;
170508
170509      if( pList==0 ){
170510        sqlite3_free(aPoslist);
170511        pPhrase->doclist.pList = 0;
170512        pPhrase->doclist.nList = 0;
170513        return SQLITE_OK;
170514
170515      }else if( aPoslist==0 ){
170516        aPoslist = pList;
170517        nPoslist = nList;
170518
170519      }else{
170520        char *aOut = pList;
170521        char *p1 = aPoslist;
170522        char *p2 = aOut;
170523
170524        assert( iPrev>=0 );
170525        fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
170526        sqlite3_free(aPoslist);
170527        aPoslist = pList;
170528        nPoslist = (int)(aOut - aPoslist);
170529        if( nPoslist==0 ){
170530          sqlite3_free(aPoslist);
170531          pPhrase->doclist.pList = 0;
170532          pPhrase->doclist.nList = 0;
170533          return SQLITE_OK;
170534        }
170535      }
170536      iPrev = iToken;
170537    }
170538  }
170539
170540  if( iPrev>=0 ){
170541    int nMaxUndeferred = pPhrase->iDoclistToken;
170542    if( nMaxUndeferred<0 ){
170543      pPhrase->doclist.pList = aPoslist;
170544      pPhrase->doclist.nList = nPoslist;
170545      pPhrase->doclist.iDocid = pCsr->iPrevId;
170546      pPhrase->doclist.bFreeList = 1;
170547    }else{
170548      int nDistance;
170549      char *p1;
170550      char *p2;
170551      char *aOut;
170552
170553      if( nMaxUndeferred>iPrev ){
170554        p1 = aPoslist;
170555        p2 = pPhrase->doclist.pList;
170556        nDistance = nMaxUndeferred - iPrev;
170557      }else{
170558        p1 = pPhrase->doclist.pList;
170559        p2 = aPoslist;
170560        nDistance = iPrev - nMaxUndeferred;
170561      }
170562
170563      aOut = (char *)sqlite3_malloc(nPoslist+8);
170564      if( !aOut ){
170565        sqlite3_free(aPoslist);
170566        return SQLITE_NOMEM;
170567      }
170568
170569      pPhrase->doclist.pList = aOut;
170570      if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
170571        pPhrase->doclist.bFreeList = 1;
170572        pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
170573      }else{
170574        sqlite3_free(aOut);
170575        pPhrase->doclist.pList = 0;
170576        pPhrase->doclist.nList = 0;
170577      }
170578      sqlite3_free(aPoslist);
170579    }
170580  }
170581
170582  return SQLITE_OK;
170583}
170584#endif /* SQLITE_DISABLE_FTS4_DEFERRED */
170585
170586/*
170587** Maximum number of tokens a phrase may have to be considered for the
170588** incremental doclists strategy.
170589*/
170590#define MAX_INCR_PHRASE_TOKENS 4
170591
170592/*
170593** This function is called for each Fts3Phrase in a full-text query
170594** expression to initialize the mechanism for returning rows. Once this
170595** function has been called successfully on an Fts3Phrase, it may be
170596** used with fts3EvalPhraseNext() to iterate through the matching docids.
170597**
170598** If parameter bOptOk is true, then the phrase may (or may not) use the
170599** incremental loading strategy. Otherwise, the entire doclist is loaded into
170600** memory within this call.
170601**
170602** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
170603*/
170604static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
170605  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
170606  int rc = SQLITE_OK;             /* Error code */
170607  int i;
170608
170609  /* Determine if doclists may be loaded from disk incrementally. This is
170610  ** possible if the bOptOk argument is true, the FTS doclists will be
170611  ** scanned in forward order, and the phrase consists of
170612  ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first"
170613  ** tokens or prefix tokens that cannot use a prefix-index.  */
170614  int bHaveIncr = 0;
170615  int bIncrOk = (bOptOk
170616   && pCsr->bDesc==pTab->bDescIdx
170617   && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
170618#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
170619   && pTab->bNoIncrDoclist==0
170620#endif
170621  );
170622  for(i=0; bIncrOk==1 && i<p->nToken; i++){
170623    Fts3PhraseToken *pToken = &p->aToken[i];
170624    if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
170625      bIncrOk = 0;
170626    }
170627    if( pToken->pSegcsr ) bHaveIncr = 1;
170628  }
170629
170630  if( bIncrOk && bHaveIncr ){
170631    /* Use the incremental approach. */
170632    int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
170633    for(i=0; rc==SQLITE_OK && i<p->nToken; i++){
170634      Fts3PhraseToken *pToken = &p->aToken[i];
170635      Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
170636      if( pSegcsr ){
170637        rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
170638      }
170639    }
170640    p->bIncr = 1;
170641  }else{
170642    /* Load the full doclist for the phrase into memory. */
170643    rc = fts3EvalPhraseLoad(pCsr, p);
170644    p->bIncr = 0;
170645  }
170646
170647  assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
170648  return rc;
170649}
170650
170651/*
170652** This function is used to iterate backwards (from the end to start)
170653** through doclists. It is used by this module to iterate through phrase
170654** doclists in reverse and by the fts3_write.c module to iterate through
170655** pending-terms lists when writing to databases with "order=desc".
170656**
170657** The doclist may be sorted in ascending (parameter bDescIdx==0) or
170658** descending (parameter bDescIdx==1) order of docid. Regardless, this
170659** function iterates from the end of the doclist to the beginning.
170660*/
170661SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
170662  int bDescIdx,                   /* True if the doclist is desc */
170663  char *aDoclist,                 /* Pointer to entire doclist */
170664  int nDoclist,                   /* Length of aDoclist in bytes */
170665  char **ppIter,                  /* IN/OUT: Iterator pointer */
170666  sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
170667  int *pnList,                    /* OUT: List length pointer */
170668  u8 *pbEof                       /* OUT: End-of-file flag */
170669){
170670  char *p = *ppIter;
170671
170672  assert( nDoclist>0 );
170673  assert( *pbEof==0 );
170674  assert( p || *piDocid==0 );
170675  assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
170676
170677  if( p==0 ){
170678    sqlite3_int64 iDocid = 0;
170679    char *pNext = 0;
170680    char *pDocid = aDoclist;
170681    char *pEnd = &aDoclist[nDoclist];
170682    int iMul = 1;
170683
170684    while( pDocid<pEnd ){
170685      sqlite3_int64 iDelta;
170686      pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
170687      iDocid += (iMul * iDelta);
170688      pNext = pDocid;
170689      fts3PoslistCopy(0, &pDocid);
170690      while( pDocid<pEnd && *pDocid==0 ) pDocid++;
170691      iMul = (bDescIdx ? -1 : 1);
170692    }
170693
170694    *pnList = (int)(pEnd - pNext);
170695    *ppIter = pNext;
170696    *piDocid = iDocid;
170697  }else{
170698    int iMul = (bDescIdx ? -1 : 1);
170699    sqlite3_int64 iDelta;
170700    fts3GetReverseVarint(&p, aDoclist, &iDelta);
170701    *piDocid -= (iMul * iDelta);
170702
170703    if( p==aDoclist ){
170704      *pbEof = 1;
170705    }else{
170706      char *pSave = p;
170707      fts3ReversePoslist(aDoclist, &p);
170708      *pnList = (int)(pSave - p);
170709    }
170710    *ppIter = p;
170711  }
170712}
170713
170714/*
170715** Iterate forwards through a doclist.
170716*/
170717SQLITE_PRIVATE void sqlite3Fts3DoclistNext(
170718  int bDescIdx,                   /* True if the doclist is desc */
170719  char *aDoclist,                 /* Pointer to entire doclist */
170720  int nDoclist,                   /* Length of aDoclist in bytes */
170721  char **ppIter,                  /* IN/OUT: Iterator pointer */
170722  sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
170723  u8 *pbEof                       /* OUT: End-of-file flag */
170724){
170725  char *p = *ppIter;
170726
170727  assert( nDoclist>0 );
170728  assert( *pbEof==0 );
170729  assert_fts3_nc( p || *piDocid==0 );
170730  assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) );
170731
170732  if( p==0 ){
170733    p = aDoclist;
170734    p += sqlite3Fts3GetVarint(p, piDocid);
170735  }else{
170736    fts3PoslistCopy(0, &p);
170737    while( p<&aDoclist[nDoclist] && *p==0 ) p++;
170738    if( p>=&aDoclist[nDoclist] ){
170739      *pbEof = 1;
170740    }else{
170741      sqlite3_int64 iVar;
170742      p += sqlite3Fts3GetVarint(p, &iVar);
170743      *piDocid += ((bDescIdx ? -1 : 1) * iVar);
170744    }
170745  }
170746
170747  *ppIter = p;
170748}
170749
170750/*
170751** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
170752** to true if EOF is reached.
170753*/
170754static void fts3EvalDlPhraseNext(
170755  Fts3Table *pTab,
170756  Fts3Doclist *pDL,
170757  u8 *pbEof
170758){
170759  char *pIter;                            /* Used to iterate through aAll */
170760  char *pEnd;                             /* 1 byte past end of aAll */
170761
170762  if( pDL->pNextDocid ){
170763    pIter = pDL->pNextDocid;
170764    assert( pDL->aAll!=0 || pIter==0 );
170765  }else{
170766    pIter = pDL->aAll;
170767  }
170768
170769  if( pIter==0 || pIter>=(pEnd = pDL->aAll + pDL->nAll) ){
170770    /* We have already reached the end of this doclist. EOF. */
170771    *pbEof = 1;
170772  }else{
170773    sqlite3_int64 iDelta;
170774    pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
170775    if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
170776      pDL->iDocid += iDelta;
170777    }else{
170778      pDL->iDocid -= iDelta;
170779    }
170780    pDL->pList = pIter;
170781    fts3PoslistCopy(0, &pIter);
170782    pDL->nList = (int)(pIter - pDL->pList);
170783
170784    /* pIter now points just past the 0x00 that terminates the position-
170785    ** list for document pDL->iDocid. However, if this position-list was
170786    ** edited in place by fts3EvalNearTrim(), then pIter may not actually
170787    ** point to the start of the next docid value. The following line deals
170788    ** with this case by advancing pIter past the zero-padding added by
170789    ** fts3EvalNearTrim().  */
170790    while( pIter<pEnd && *pIter==0 ) pIter++;
170791
170792    pDL->pNextDocid = pIter;
170793    assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
170794    *pbEof = 0;
170795  }
170796}
170797
170798/*
170799** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext().
170800*/
170801typedef struct TokenDoclist TokenDoclist;
170802struct TokenDoclist {
170803  int bIgnore;
170804  sqlite3_int64 iDocid;
170805  char *pList;
170806  int nList;
170807};
170808
170809/*
170810** Token pToken is an incrementally loaded token that is part of a
170811** multi-token phrase. Advance it to the next matching document in the
170812** database and populate output variable *p with the details of the new
170813** entry. Or, if the iterator has reached EOF, set *pbEof to true.
170814**
170815** If an error occurs, return an SQLite error code. Otherwise, return
170816** SQLITE_OK.
170817*/
170818static int incrPhraseTokenNext(
170819  Fts3Table *pTab,                /* Virtual table handle */
170820  Fts3Phrase *pPhrase,            /* Phrase to advance token of */
170821  int iToken,                     /* Specific token to advance */
170822  TokenDoclist *p,                /* OUT: Docid and doclist for new entry */
170823  u8 *pbEof                       /* OUT: True if iterator is at EOF */
170824){
170825  int rc = SQLITE_OK;
170826
170827  if( pPhrase->iDoclistToken==iToken ){
170828    assert( p->bIgnore==0 );
170829    assert( pPhrase->aToken[iToken].pSegcsr==0 );
170830    fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof);
170831    p->pList = pPhrase->doclist.pList;
170832    p->nList = pPhrase->doclist.nList;
170833    p->iDocid = pPhrase->doclist.iDocid;
170834  }else{
170835    Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
170836    assert( pToken->pDeferred==0 );
170837    assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 );
170838    if( pToken->pSegcsr ){
170839      assert( p->bIgnore==0 );
170840      rc = sqlite3Fts3MsrIncrNext(
170841          pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
170842      );
170843      if( p->pList==0 ) *pbEof = 1;
170844    }else{
170845      p->bIgnore = 1;
170846    }
170847  }
170848
170849  return rc;
170850}
170851
170852
170853/*
170854** The phrase iterator passed as the second argument:
170855**
170856**   * features at least one token that uses an incremental doclist, and
170857**
170858**   * does not contain any deferred tokens.
170859**
170860** Advance it to the next matching documnent in the database and populate
170861** the Fts3Doclist.pList and nList fields.
170862**
170863** If there is no "next" entry and no error occurs, then *pbEof is set to
170864** 1 before returning. Otherwise, if no error occurs and the iterator is
170865** successfully advanced, *pbEof is set to 0.
170866**
170867** If an error occurs, return an SQLite error code. Otherwise, return
170868** SQLITE_OK.
170869*/
170870static int fts3EvalIncrPhraseNext(
170871  Fts3Cursor *pCsr,               /* FTS Cursor handle */
170872  Fts3Phrase *p,                  /* Phrase object to advance to next docid */
170873  u8 *pbEof                       /* OUT: Set to 1 if EOF */
170874){
170875  int rc = SQLITE_OK;
170876  Fts3Doclist *pDL = &p->doclist;
170877  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
170878  u8 bEof = 0;
170879
170880  /* This is only called if it is guaranteed that the phrase has at least
170881  ** one incremental token. In which case the bIncr flag is set. */
170882  assert( p->bIncr==1 );
170883
170884  if( p->nToken==1 ){
170885    rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
170886        &pDL->iDocid, &pDL->pList, &pDL->nList
170887    );
170888    if( pDL->pList==0 ) bEof = 1;
170889  }else{
170890    int bDescDoclist = pCsr->bDesc;
170891    struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS];
170892
170893    memset(a, 0, sizeof(a));
170894    assert( p->nToken<=MAX_INCR_PHRASE_TOKENS );
170895    assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS );
170896
170897    while( bEof==0 ){
170898      int bMaxSet = 0;
170899      sqlite3_int64 iMax = 0;     /* Largest docid for all iterators */
170900      int i;                      /* Used to iterate through tokens */
170901
170902      /* Advance the iterator for each token in the phrase once. */
170903      for(i=0; rc==SQLITE_OK && i<p->nToken && bEof==0; i++){
170904        rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
170905        if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){
170906          iMax = a[i].iDocid;
170907          bMaxSet = 1;
170908        }
170909      }
170910      assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) );
170911      assert( rc!=SQLITE_OK || bMaxSet );
170912
170913      /* Keep advancing iterators until they all point to the same document */
170914      for(i=0; i<p->nToken; i++){
170915        while( rc==SQLITE_OK && bEof==0
170916            && a[i].bIgnore==0 && DOCID_CMP(a[i].iDocid, iMax)<0
170917        ){
170918          rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
170919          if( DOCID_CMP(a[i].iDocid, iMax)>0 ){
170920            iMax = a[i].iDocid;
170921            i = 0;
170922          }
170923        }
170924      }
170925
170926      /* Check if the current entries really are a phrase match */
170927      if( bEof==0 ){
170928        int nList = 0;
170929        int nByte = a[p->nToken-1].nList;
170930        char *aDoclist = sqlite3_malloc(nByte+FTS3_BUFFER_PADDING);
170931        if( !aDoclist ) return SQLITE_NOMEM;
170932        memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
170933        memset(&aDoclist[nByte], 0, FTS3_BUFFER_PADDING);
170934
170935        for(i=0; i<(p->nToken-1); i++){
170936          if( a[i].bIgnore==0 ){
170937            char *pL = a[i].pList;
170938            char *pR = aDoclist;
170939            char *pOut = aDoclist;
170940            int nDist = p->nToken-1-i;
170941            int res = fts3PoslistPhraseMerge(&pOut, nDist, 0, 1, &pL, &pR);
170942            if( res==0 ) break;
170943            nList = (int)(pOut - aDoclist);
170944          }
170945        }
170946        if( i==(p->nToken-1) ){
170947          pDL->iDocid = iMax;
170948          pDL->pList = aDoclist;
170949          pDL->nList = nList;
170950          pDL->bFreeList = 1;
170951          break;
170952        }
170953        sqlite3_free(aDoclist);
170954      }
170955    }
170956  }
170957
170958  *pbEof = bEof;
170959  return rc;
170960}
170961
170962/*
170963** Attempt to move the phrase iterator to point to the next matching docid.
170964** If an error occurs, return an SQLite error code. Otherwise, return
170965** SQLITE_OK.
170966**
170967** If there is no "next" entry and no error occurs, then *pbEof is set to
170968** 1 before returning. Otherwise, if no error occurs and the iterator is
170969** successfully advanced, *pbEof is set to 0.
170970*/
170971static int fts3EvalPhraseNext(
170972  Fts3Cursor *pCsr,               /* FTS Cursor handle */
170973  Fts3Phrase *p,                  /* Phrase object to advance to next docid */
170974  u8 *pbEof                       /* OUT: Set to 1 if EOF */
170975){
170976  int rc = SQLITE_OK;
170977  Fts3Doclist *pDL = &p->doclist;
170978  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
170979
170980  if( p->bIncr ){
170981    rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof);
170982  }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
170983    sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
170984        &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
170985    );
170986    pDL->pList = pDL->pNextDocid;
170987  }else{
170988    fts3EvalDlPhraseNext(pTab, pDL, pbEof);
170989  }
170990
170991  return rc;
170992}
170993
170994/*
170995**
170996** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
170997** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
170998** expression. Also the Fts3Expr.bDeferred variable is set to true for any
170999** expressions for which all descendent tokens are deferred.
171000**
171001** If parameter bOptOk is zero, then it is guaranteed that the
171002** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
171003** each phrase in the expression (subject to deferred token processing).
171004** Or, if bOptOk is non-zero, then one or more tokens within the expression
171005** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
171006**
171007** If an error occurs within this function, *pRc is set to an SQLite error
171008** code before returning.
171009*/
171010static void fts3EvalStartReaders(
171011  Fts3Cursor *pCsr,               /* FTS Cursor handle */
171012  Fts3Expr *pExpr,                /* Expression to initialize phrases in */
171013  int *pRc                        /* IN/OUT: Error code */
171014){
171015  if( pExpr && SQLITE_OK==*pRc ){
171016    if( pExpr->eType==FTSQUERY_PHRASE ){
171017      int nToken = pExpr->pPhrase->nToken;
171018      if( nToken ){
171019        int i;
171020        for(i=0; i<nToken; i++){
171021          if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
171022        }
171023        pExpr->bDeferred = (i==nToken);
171024      }
171025      *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
171026    }else{
171027      fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
171028      fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
171029      pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
171030    }
171031  }
171032}
171033
171034/*
171035** An array of the following structures is assembled as part of the process
171036** of selecting tokens to defer before the query starts executing (as part
171037** of the xFilter() method). There is one element in the array for each
171038** token in the FTS expression.
171039**
171040** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
171041** to phrases that are connected only by AND and NEAR operators (not OR or
171042** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
171043** separately. The root of a tokens AND/NEAR cluster is stored in
171044** Fts3TokenAndCost.pRoot.
171045*/
171046typedef struct Fts3TokenAndCost Fts3TokenAndCost;
171047struct Fts3TokenAndCost {
171048  Fts3Phrase *pPhrase;            /* The phrase the token belongs to */
171049  int iToken;                     /* Position of token in phrase */
171050  Fts3PhraseToken *pToken;        /* The token itself */
171051  Fts3Expr *pRoot;                /* Root of NEAR/AND cluster */
171052  int nOvfl;                      /* Number of overflow pages to load doclist */
171053  int iCol;                       /* The column the token must match */
171054};
171055
171056/*
171057** This function is used to populate an allocated Fts3TokenAndCost array.
171058**
171059** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
171060** Otherwise, if an error occurs during execution, *pRc is set to an
171061** SQLite error code.
171062*/
171063static void fts3EvalTokenCosts(
171064  Fts3Cursor *pCsr,               /* FTS Cursor handle */
171065  Fts3Expr *pRoot,                /* Root of current AND/NEAR cluster */
171066  Fts3Expr *pExpr,                /* Expression to consider */
171067  Fts3TokenAndCost **ppTC,        /* Write new entries to *(*ppTC)++ */
171068  Fts3Expr ***ppOr,               /* Write new OR root to *(*ppOr)++ */
171069  int *pRc                        /* IN/OUT: Error code */
171070){
171071  if( *pRc==SQLITE_OK ){
171072    if( pExpr->eType==FTSQUERY_PHRASE ){
171073      Fts3Phrase *pPhrase = pExpr->pPhrase;
171074      int i;
171075      for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
171076        Fts3TokenAndCost *pTC = (*ppTC)++;
171077        pTC->pPhrase = pPhrase;
171078        pTC->iToken = i;
171079        pTC->pRoot = pRoot;
171080        pTC->pToken = &pPhrase->aToken[i];
171081        pTC->iCol = pPhrase->iColumn;
171082        *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
171083      }
171084    }else if( pExpr->eType!=FTSQUERY_NOT ){
171085      assert( pExpr->eType==FTSQUERY_OR
171086           || pExpr->eType==FTSQUERY_AND
171087           || pExpr->eType==FTSQUERY_NEAR
171088      );
171089      assert( pExpr->pLeft && pExpr->pRight );
171090      if( pExpr->eType==FTSQUERY_OR ){
171091        pRoot = pExpr->pLeft;
171092        **ppOr = pRoot;
171093        (*ppOr)++;
171094      }
171095      fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
171096      if( pExpr->eType==FTSQUERY_OR ){
171097        pRoot = pExpr->pRight;
171098        **ppOr = pRoot;
171099        (*ppOr)++;
171100      }
171101      fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
171102    }
171103  }
171104}
171105
171106/*
171107** Determine the average document (row) size in pages. If successful,
171108** write this value to *pnPage and return SQLITE_OK. Otherwise, return
171109** an SQLite error code.
171110**
171111** The average document size in pages is calculated by first calculating
171112** determining the average size in bytes, B. If B is less than the amount
171113** of data that will fit on a single leaf page of an intkey table in
171114** this database, then the average docsize is 1. Otherwise, it is 1 plus
171115** the number of overflow pages consumed by a record B bytes in size.
171116*/
171117static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
171118  int rc = SQLITE_OK;
171119  if( pCsr->nRowAvg==0 ){
171120    /* The average document size, which is required to calculate the cost
171121    ** of each doclist, has not yet been determined. Read the required
171122    ** data from the %_stat table to calculate it.
171123    **
171124    ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
171125    ** varints, where nCol is the number of columns in the FTS3 table.
171126    ** The first varint is the number of documents currently stored in
171127    ** the table. The following nCol varints contain the total amount of
171128    ** data stored in all rows of each column of the table, from left
171129    ** to right.
171130    */
171131    Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
171132    sqlite3_stmt *pStmt;
171133    sqlite3_int64 nDoc = 0;
171134    sqlite3_int64 nByte = 0;
171135    const char *pEnd;
171136    const char *a;
171137
171138    rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
171139    if( rc!=SQLITE_OK ) return rc;
171140    a = sqlite3_column_blob(pStmt, 0);
171141    testcase( a==0 );  /* If %_stat.value set to X'' */
171142    if( a ){
171143      pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
171144      a += sqlite3Fts3GetVarintBounded(a, pEnd, &nDoc);
171145      while( a<pEnd ){
171146        a += sqlite3Fts3GetVarintBounded(a, pEnd, &nByte);
171147      }
171148    }
171149    if( nDoc==0 || nByte==0 ){
171150      sqlite3_reset(pStmt);
171151      return FTS_CORRUPT_VTAB;
171152    }
171153
171154    pCsr->nDoc = nDoc;
171155    pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
171156    assert( pCsr->nRowAvg>0 );
171157    rc = sqlite3_reset(pStmt);
171158  }
171159
171160  *pnPage = pCsr->nRowAvg;
171161  return rc;
171162}
171163
171164/*
171165** This function is called to select the tokens (if any) that will be
171166** deferred. The array aTC[] has already been populated when this is
171167** called.
171168**
171169** This function is called once for each AND/NEAR cluster in the
171170** expression. Each invocation determines which tokens to defer within
171171** the cluster with root node pRoot. See comments above the definition
171172** of struct Fts3TokenAndCost for more details.
171173**
171174** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
171175** called on each token to defer. Otherwise, an SQLite error code is
171176** returned.
171177*/
171178static int fts3EvalSelectDeferred(
171179  Fts3Cursor *pCsr,               /* FTS Cursor handle */
171180  Fts3Expr *pRoot,                /* Consider tokens with this root node */
171181  Fts3TokenAndCost *aTC,          /* Array of expression tokens and costs */
171182  int nTC                         /* Number of entries in aTC[] */
171183){
171184  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
171185  int nDocSize = 0;               /* Number of pages per doc loaded */
171186  int rc = SQLITE_OK;             /* Return code */
171187  int ii;                         /* Iterator variable for various purposes */
171188  int nOvfl = 0;                  /* Total overflow pages used by doclists */
171189  int nToken = 0;                 /* Total number of tokens in cluster */
171190
171191  int nMinEst = 0;                /* The minimum count for any phrase so far. */
171192  int nLoad4 = 1;                 /* (Phrases that will be loaded)^4. */
171193
171194  /* Tokens are never deferred for FTS tables created using the content=xxx
171195  ** option. The reason being that it is not guaranteed that the content
171196  ** table actually contains the same data as the index. To prevent this from
171197  ** causing any problems, the deferred token optimization is completely
171198  ** disabled for content=xxx tables. */
171199  if( pTab->zContentTbl ){
171200    return SQLITE_OK;
171201  }
171202
171203  /* Count the tokens in this AND/NEAR cluster. If none of the doclists
171204  ** associated with the tokens spill onto overflow pages, or if there is
171205  ** only 1 token, exit early. No tokens to defer in this case. */
171206  for(ii=0; ii<nTC; ii++){
171207    if( aTC[ii].pRoot==pRoot ){
171208      nOvfl += aTC[ii].nOvfl;
171209      nToken++;
171210    }
171211  }
171212  if( nOvfl==0 || nToken<2 ) return SQLITE_OK;
171213
171214  /* Obtain the average docsize (in pages). */
171215  rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
171216  assert( rc!=SQLITE_OK || nDocSize>0 );
171217
171218
171219  /* Iterate through all tokens in this AND/NEAR cluster, in ascending order
171220  ** of the number of overflow pages that will be loaded by the pager layer
171221  ** to retrieve the entire doclist for the token from the full-text index.
171222  ** Load the doclists for tokens that are either:
171223  **
171224  **   a. The cheapest token in the entire query (i.e. the one visited by the
171225  **      first iteration of this loop), or
171226  **
171227  **   b. Part of a multi-token phrase.
171228  **
171229  ** After each token doclist is loaded, merge it with the others from the
171230  ** same phrase and count the number of documents that the merged doclist
171231  ** contains. Set variable "nMinEst" to the smallest number of documents in
171232  ** any phrase doclist for which 1 or more token doclists have been loaded.
171233  ** Let nOther be the number of other phrases for which it is certain that
171234  ** one or more tokens will not be deferred.
171235  **
171236  ** Then, for each token, defer it if loading the doclist would result in
171237  ** loading N or more overflow pages into memory, where N is computed as:
171238  **
171239  **    (nMinEst + 4^nOther - 1) / (4^nOther)
171240  */
171241  for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){
171242    int iTC;                      /* Used to iterate through aTC[] array. */
171243    Fts3TokenAndCost *pTC = 0;    /* Set to cheapest remaining token. */
171244
171245    /* Set pTC to point to the cheapest remaining token. */
171246    for(iTC=0; iTC<nTC; iTC++){
171247      if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot
171248       && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
171249      ){
171250        pTC = &aTC[iTC];
171251      }
171252    }
171253    assert( pTC );
171254
171255    if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
171256      /* The number of overflow pages to load for this (and therefore all
171257      ** subsequent) tokens is greater than the estimated number of pages
171258      ** that will be loaded if all subsequent tokens are deferred.
171259      */
171260      Fts3PhraseToken *pToken = pTC->pToken;
171261      rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
171262      fts3SegReaderCursorFree(pToken->pSegcsr);
171263      pToken->pSegcsr = 0;
171264    }else{
171265      /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
171266      ** for-loop. Except, limit the value to 2^24 to prevent it from
171267      ** overflowing the 32-bit integer it is stored in. */
171268      if( ii<12 ) nLoad4 = nLoad4*4;
171269
171270      if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
171271        /* Either this is the cheapest token in the entire query, or it is
171272        ** part of a multi-token phrase. Either way, the entire doclist will
171273        ** (eventually) be loaded into memory. It may as well be now. */
171274        Fts3PhraseToken *pToken = pTC->pToken;
171275        int nList = 0;
171276        char *pList = 0;
171277        rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
171278        assert( rc==SQLITE_OK || pList==0 );
171279        if( rc==SQLITE_OK ){
171280          rc = fts3EvalPhraseMergeToken(
171281              pTab, pTC->pPhrase, pTC->iToken,pList,nList
171282          );
171283        }
171284        if( rc==SQLITE_OK ){
171285          int nCount;
171286          nCount = fts3DoclistCountDocids(
171287              pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
171288          );
171289          if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
171290        }
171291      }
171292    }
171293    pTC->pToken = 0;
171294  }
171295
171296  return rc;
171297}
171298
171299/*
171300** This function is called from within the xFilter method. It initializes
171301** the full-text query currently stored in pCsr->pExpr. To iterate through
171302** the results of a query, the caller does:
171303**
171304**    fts3EvalStart(pCsr);
171305**    while( 1 ){
171306**      fts3EvalNext(pCsr);
171307**      if( pCsr->bEof ) break;
171308**      ... return row pCsr->iPrevId to the caller ...
171309**    }
171310*/
171311static int fts3EvalStart(Fts3Cursor *pCsr){
171312  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
171313  int rc = SQLITE_OK;
171314  int nToken = 0;
171315  int nOr = 0;
171316
171317  /* Allocate a MultiSegReader for each token in the expression. */
171318  fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
171319
171320  /* Determine which, if any, tokens in the expression should be deferred. */
171321#ifndef SQLITE_DISABLE_FTS4_DEFERRED
171322  if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
171323    Fts3TokenAndCost *aTC;
171324    Fts3Expr **apOr;
171325    aTC = (Fts3TokenAndCost *)sqlite3_malloc64(
171326        sizeof(Fts3TokenAndCost) * nToken
171327      + sizeof(Fts3Expr *) * nOr * 2
171328    );
171329    apOr = (Fts3Expr **)&aTC[nToken];
171330
171331    if( !aTC ){
171332      rc = SQLITE_NOMEM;
171333    }else{
171334      int ii;
171335      Fts3TokenAndCost *pTC = aTC;
171336      Fts3Expr **ppOr = apOr;
171337
171338      fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
171339      nToken = (int)(pTC-aTC);
171340      nOr = (int)(ppOr-apOr);
171341
171342      if( rc==SQLITE_OK ){
171343        rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
171344        for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
171345          rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
171346        }
171347      }
171348
171349      sqlite3_free(aTC);
171350    }
171351  }
171352#endif
171353
171354  fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc);
171355  return rc;
171356}
171357
171358/*
171359** Invalidate the current position list for phrase pPhrase.
171360*/
171361static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
171362  if( pPhrase->doclist.bFreeList ){
171363    sqlite3_free(pPhrase->doclist.pList);
171364  }
171365  pPhrase->doclist.pList = 0;
171366  pPhrase->doclist.nList = 0;
171367  pPhrase->doclist.bFreeList = 0;
171368}
171369
171370/*
171371** This function is called to edit the position list associated with
171372** the phrase object passed as the fifth argument according to a NEAR
171373** condition. For example:
171374**
171375**     abc NEAR/5 "def ghi"
171376**
171377** Parameter nNear is passed the NEAR distance of the expression (5 in
171378** the example above). When this function is called, *paPoslist points to
171379** the position list, and *pnToken is the number of phrase tokens in the
171380** phrase on the other side of the NEAR operator to pPhrase. For example,
171381** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
171382** the position list associated with phrase "abc".
171383**
171384** All positions in the pPhrase position list that are not sufficiently
171385** close to a position in the *paPoslist position list are removed. If this
171386** leaves 0 positions, zero is returned. Otherwise, non-zero.
171387**
171388** Before returning, *paPoslist is set to point to the position lsit
171389** associated with pPhrase. And *pnToken is set to the number of tokens in
171390** pPhrase.
171391*/
171392static int fts3EvalNearTrim(
171393  int nNear,                      /* NEAR distance. As in "NEAR/nNear". */
171394  char *aTmp,                     /* Temporary space to use */
171395  char **paPoslist,               /* IN/OUT: Position list */
171396  int *pnToken,                   /* IN/OUT: Tokens in phrase of *paPoslist */
171397  Fts3Phrase *pPhrase             /* The phrase object to trim the doclist of */
171398){
171399  int nParam1 = nNear + pPhrase->nToken;
171400  int nParam2 = nNear + *pnToken;
171401  int nNew;
171402  char *p2;
171403  char *pOut;
171404  int res;
171405
171406  assert( pPhrase->doclist.pList );
171407
171408  p2 = pOut = pPhrase->doclist.pList;
171409  res = fts3PoslistNearMerge(
171410    &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
171411  );
171412  if( res ){
171413    nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
171414    assert( pPhrase->doclist.pList[nNew]=='\0' );
171415    assert( nNew<=pPhrase->doclist.nList && nNew>0 );
171416    memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
171417    pPhrase->doclist.nList = nNew;
171418    *paPoslist = pPhrase->doclist.pList;
171419    *pnToken = pPhrase->nToken;
171420  }
171421
171422  return res;
171423}
171424
171425/*
171426** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
171427** Otherwise, it advances the expression passed as the second argument to
171428** point to the next matching row in the database. Expressions iterate through
171429** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
171430** or descending if it is non-zero.
171431**
171432** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
171433** successful, the following variables in pExpr are set:
171434**
171435**   Fts3Expr.bEof                (non-zero if EOF - there is no next row)
171436**   Fts3Expr.iDocid              (valid if bEof==0. The docid of the next row)
171437**
171438** If the expression is of type FTSQUERY_PHRASE, and the expression is not
171439** at EOF, then the following variables are populated with the position list
171440** for the phrase for the visited row:
171441**
171442**   FTs3Expr.pPhrase->doclist.nList        (length of pList in bytes)
171443**   FTs3Expr.pPhrase->doclist.pList        (pointer to position list)
171444**
171445** It says above that this function advances the expression to the next
171446** matching row. This is usually true, but there are the following exceptions:
171447**
171448**   1. Deferred tokens are not taken into account. If a phrase consists
171449**      entirely of deferred tokens, it is assumed to match every row in
171450**      the db. In this case the position-list is not populated at all.
171451**
171452**      Or, if a phrase contains one or more deferred tokens and one or
171453**      more non-deferred tokens, then the expression is advanced to the
171454**      next possible match, considering only non-deferred tokens. In other
171455**      words, if the phrase is "A B C", and "B" is deferred, the expression
171456**      is advanced to the next row that contains an instance of "A * C",
171457**      where "*" may match any single token. The position list in this case
171458**      is populated as for "A * C" before returning.
171459**
171460**   2. NEAR is treated as AND. If the expression is "x NEAR y", it is
171461**      advanced to point to the next row that matches "x AND y".
171462**
171463** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is
171464** really a match, taking into account deferred tokens and NEAR operators.
171465*/
171466static void fts3EvalNextRow(
171467  Fts3Cursor *pCsr,               /* FTS Cursor handle */
171468  Fts3Expr *pExpr,                /* Expr. to advance to next matching row */
171469  int *pRc                        /* IN/OUT: Error code */
171470){
171471  if( *pRc==SQLITE_OK ){
171472    int bDescDoclist = pCsr->bDesc;         /* Used by DOCID_CMP() macro */
171473    assert( pExpr->bEof==0 );
171474    pExpr->bStart = 1;
171475
171476    switch( pExpr->eType ){
171477      case FTSQUERY_NEAR:
171478      case FTSQUERY_AND: {
171479        Fts3Expr *pLeft = pExpr->pLeft;
171480        Fts3Expr *pRight = pExpr->pRight;
171481        assert( !pLeft->bDeferred || !pRight->bDeferred );
171482
171483        if( pLeft->bDeferred ){
171484          /* LHS is entirely deferred. So we assume it matches every row.
171485          ** Advance the RHS iterator to find the next row visited. */
171486          fts3EvalNextRow(pCsr, pRight, pRc);
171487          pExpr->iDocid = pRight->iDocid;
171488          pExpr->bEof = pRight->bEof;
171489        }else if( pRight->bDeferred ){
171490          /* RHS is entirely deferred. So we assume it matches every row.
171491          ** Advance the LHS iterator to find the next row visited. */
171492          fts3EvalNextRow(pCsr, pLeft, pRc);
171493          pExpr->iDocid = pLeft->iDocid;
171494          pExpr->bEof = pLeft->bEof;
171495        }else{
171496          /* Neither the RHS or LHS are deferred. */
171497          fts3EvalNextRow(pCsr, pLeft, pRc);
171498          fts3EvalNextRow(pCsr, pRight, pRc);
171499          while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
171500            sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
171501            if( iDiff==0 ) break;
171502            if( iDiff<0 ){
171503              fts3EvalNextRow(pCsr, pLeft, pRc);
171504            }else{
171505              fts3EvalNextRow(pCsr, pRight, pRc);
171506            }
171507          }
171508          pExpr->iDocid = pLeft->iDocid;
171509          pExpr->bEof = (pLeft->bEof || pRight->bEof);
171510          if( pExpr->eType==FTSQUERY_NEAR && pExpr->bEof ){
171511            assert( pRight->eType==FTSQUERY_PHRASE );
171512            if( pRight->pPhrase->doclist.aAll ){
171513              Fts3Doclist *pDl = &pRight->pPhrase->doclist;
171514              while( *pRc==SQLITE_OK && pRight->bEof==0 ){
171515                memset(pDl->pList, 0, pDl->nList);
171516                fts3EvalNextRow(pCsr, pRight, pRc);
171517              }
171518            }
171519            if( pLeft->pPhrase && pLeft->pPhrase->doclist.aAll ){
171520              Fts3Doclist *pDl = &pLeft->pPhrase->doclist;
171521              while( *pRc==SQLITE_OK && pLeft->bEof==0 ){
171522                memset(pDl->pList, 0, pDl->nList);
171523                fts3EvalNextRow(pCsr, pLeft, pRc);
171524              }
171525            }
171526            pRight->bEof = pLeft->bEof = 1;
171527          }
171528        }
171529        break;
171530      }
171531
171532      case FTSQUERY_OR: {
171533        Fts3Expr *pLeft = pExpr->pLeft;
171534        Fts3Expr *pRight = pExpr->pRight;
171535        sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
171536
171537        assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
171538        assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
171539
171540        if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
171541          fts3EvalNextRow(pCsr, pLeft, pRc);
171542        }else if( pLeft->bEof || iCmp>0 ){
171543          fts3EvalNextRow(pCsr, pRight, pRc);
171544        }else{
171545          fts3EvalNextRow(pCsr, pLeft, pRc);
171546          fts3EvalNextRow(pCsr, pRight, pRc);
171547        }
171548
171549        pExpr->bEof = (pLeft->bEof && pRight->bEof);
171550        iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
171551        if( pRight->bEof || (pLeft->bEof==0 &&  iCmp<0) ){
171552          pExpr->iDocid = pLeft->iDocid;
171553        }else{
171554          pExpr->iDocid = pRight->iDocid;
171555        }
171556
171557        break;
171558      }
171559
171560      case FTSQUERY_NOT: {
171561        Fts3Expr *pLeft = pExpr->pLeft;
171562        Fts3Expr *pRight = pExpr->pRight;
171563
171564        if( pRight->bStart==0 ){
171565          fts3EvalNextRow(pCsr, pRight, pRc);
171566          assert( *pRc!=SQLITE_OK || pRight->bStart );
171567        }
171568
171569        fts3EvalNextRow(pCsr, pLeft, pRc);
171570        if( pLeft->bEof==0 ){
171571          while( !*pRc
171572              && !pRight->bEof
171573              && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
171574          ){
171575            fts3EvalNextRow(pCsr, pRight, pRc);
171576          }
171577        }
171578        pExpr->iDocid = pLeft->iDocid;
171579        pExpr->bEof = pLeft->bEof;
171580        break;
171581      }
171582
171583      default: {
171584        Fts3Phrase *pPhrase = pExpr->pPhrase;
171585        fts3EvalInvalidatePoslist(pPhrase);
171586        *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
171587        pExpr->iDocid = pPhrase->doclist.iDocid;
171588        break;
171589      }
171590    }
171591  }
171592}
171593
171594/*
171595** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
171596** cluster, then this function returns 1 immediately.
171597**
171598** Otherwise, it checks if the current row really does match the NEAR
171599** expression, using the data currently stored in the position lists
171600** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
171601**
171602** If the current row is a match, the position list associated with each
171603** phrase in the NEAR expression is edited in place to contain only those
171604** phrase instances sufficiently close to their peers to satisfy all NEAR
171605** constraints. In this case it returns 1. If the NEAR expression does not
171606** match the current row, 0 is returned. The position lists may or may not
171607** be edited if 0 is returned.
171608*/
171609static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
171610  int res = 1;
171611
171612  /* The following block runs if pExpr is the root of a NEAR query.
171613  ** For example, the query:
171614  **
171615  **         "w" NEAR "x" NEAR "y" NEAR "z"
171616  **
171617  ** which is represented in tree form as:
171618  **
171619  **                               |
171620  **                          +--NEAR--+      <-- root of NEAR query
171621  **                          |        |
171622  **                     +--NEAR--+   "z"
171623  **                     |        |
171624  **                +--NEAR--+   "y"
171625  **                |        |
171626  **               "w"      "x"
171627  **
171628  ** The right-hand child of a NEAR node is always a phrase. The
171629  ** left-hand child may be either a phrase or a NEAR node. There are
171630  ** no exceptions to this - it's the way the parser in fts3_expr.c works.
171631  */
171632  if( *pRc==SQLITE_OK
171633   && pExpr->eType==FTSQUERY_NEAR
171634   && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
171635  ){
171636    Fts3Expr *p;
171637    sqlite3_int64 nTmp = 0;       /* Bytes of temp space */
171638    char *aTmp;                   /* Temp space for PoslistNearMerge() */
171639
171640    /* Allocate temporary working space. */
171641    for(p=pExpr; p->pLeft; p=p->pLeft){
171642      assert( p->pRight->pPhrase->doclist.nList>0 );
171643      nTmp += p->pRight->pPhrase->doclist.nList;
171644    }
171645    nTmp += p->pPhrase->doclist.nList;
171646    aTmp = sqlite3_malloc64(nTmp*2);
171647    if( !aTmp ){
171648      *pRc = SQLITE_NOMEM;
171649      res = 0;
171650    }else{
171651      char *aPoslist = p->pPhrase->doclist.pList;
171652      int nToken = p->pPhrase->nToken;
171653
171654      for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
171655        Fts3Phrase *pPhrase = p->pRight->pPhrase;
171656        int nNear = p->nNear;
171657        res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
171658      }
171659
171660      aPoslist = pExpr->pRight->pPhrase->doclist.pList;
171661      nToken = pExpr->pRight->pPhrase->nToken;
171662      for(p=pExpr->pLeft; p && res; p=p->pLeft){
171663        int nNear;
171664        Fts3Phrase *pPhrase;
171665        assert( p->pParent && p->pParent->pLeft==p );
171666        nNear = p->pParent->nNear;
171667        pPhrase = (
171668            p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
171669        );
171670        res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
171671      }
171672    }
171673
171674    sqlite3_free(aTmp);
171675  }
171676
171677  return res;
171678}
171679
171680/*
171681** This function is a helper function for sqlite3Fts3EvalTestDeferred().
171682** Assuming no error occurs or has occurred, It returns non-zero if the
171683** expression passed as the second argument matches the row that pCsr
171684** currently points to, or zero if it does not.
171685**
171686** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
171687** If an error occurs during execution of this function, *pRc is set to
171688** the appropriate SQLite error code. In this case the returned value is
171689** undefined.
171690*/
171691static int fts3EvalTestExpr(
171692  Fts3Cursor *pCsr,               /* FTS cursor handle */
171693  Fts3Expr *pExpr,                /* Expr to test. May or may not be root. */
171694  int *pRc                        /* IN/OUT: Error code */
171695){
171696  int bHit = 1;                   /* Return value */
171697  if( *pRc==SQLITE_OK ){
171698    switch( pExpr->eType ){
171699      case FTSQUERY_NEAR:
171700      case FTSQUERY_AND:
171701        bHit = (
171702            fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
171703         && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
171704         && fts3EvalNearTest(pExpr, pRc)
171705        );
171706
171707        /* If the NEAR expression does not match any rows, zero the doclist for
171708        ** all phrases involved in the NEAR. This is because the snippet(),
171709        ** offsets() and matchinfo() functions are not supposed to recognize
171710        ** any instances of phrases that are part of unmatched NEAR queries.
171711        ** For example if this expression:
171712        **
171713        **    ... MATCH 'a OR (b NEAR c)'
171714        **
171715        ** is matched against a row containing:
171716        **
171717        **        'a b d e'
171718        **
171719        ** then any snippet() should ony highlight the "a" term, not the "b"
171720        ** (as "b" is part of a non-matching NEAR clause).
171721        */
171722        if( bHit==0
171723         && pExpr->eType==FTSQUERY_NEAR
171724         && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
171725        ){
171726          Fts3Expr *p;
171727          for(p=pExpr; p->pPhrase==0; p=p->pLeft){
171728            if( p->pRight->iDocid==pCsr->iPrevId ){
171729              fts3EvalInvalidatePoslist(p->pRight->pPhrase);
171730            }
171731          }
171732          if( p->iDocid==pCsr->iPrevId ){
171733            fts3EvalInvalidatePoslist(p->pPhrase);
171734          }
171735        }
171736
171737        break;
171738
171739      case FTSQUERY_OR: {
171740        int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
171741        int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
171742        bHit = bHit1 || bHit2;
171743        break;
171744      }
171745
171746      case FTSQUERY_NOT:
171747        bHit = (
171748            fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
171749         && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
171750        );
171751        break;
171752
171753      default: {
171754#ifndef SQLITE_DISABLE_FTS4_DEFERRED
171755        if( pCsr->pDeferred
171756         && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
171757        ){
171758          Fts3Phrase *pPhrase = pExpr->pPhrase;
171759          assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
171760          if( pExpr->bDeferred ){
171761            fts3EvalInvalidatePoslist(pPhrase);
171762          }
171763          *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
171764          bHit = (pPhrase->doclist.pList!=0);
171765          pExpr->iDocid = pCsr->iPrevId;
171766        }else
171767#endif
171768        {
171769          bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
171770        }
171771        break;
171772      }
171773    }
171774  }
171775  return bHit;
171776}
171777
171778/*
171779** This function is called as the second part of each xNext operation when
171780** iterating through the results of a full-text query. At this point the
171781** cursor points to a row that matches the query expression, with the
171782** following caveats:
171783**
171784**   * Up until this point, "NEAR" operators in the expression have been
171785**     treated as "AND".
171786**
171787**   * Deferred tokens have not yet been considered.
171788**
171789** If *pRc is not SQLITE_OK when this function is called, it immediately
171790** returns 0. Otherwise, it tests whether or not after considering NEAR
171791** operators and deferred tokens the current row is still a match for the
171792** expression. It returns 1 if both of the following are true:
171793**
171794**   1. *pRc is SQLITE_OK when this function returns, and
171795**
171796**   2. After scanning the current FTS table row for the deferred tokens,
171797**      it is determined that the row does *not* match the query.
171798**
171799** Or, if no error occurs and it seems the current row does match the FTS
171800** query, return 0.
171801*/
171802SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){
171803  int rc = *pRc;
171804  int bMiss = 0;
171805  if( rc==SQLITE_OK ){
171806
171807    /* If there are one or more deferred tokens, load the current row into
171808    ** memory and scan it to determine the position list for each deferred
171809    ** token. Then, see if this row is really a match, considering deferred
171810    ** tokens and NEAR operators (neither of which were taken into account
171811    ** earlier, by fts3EvalNextRow()).
171812    */
171813    if( pCsr->pDeferred ){
171814      rc = fts3CursorSeek(0, pCsr);
171815      if( rc==SQLITE_OK ){
171816        rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
171817      }
171818    }
171819    bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
171820
171821    /* Free the position-lists accumulated for each deferred token above. */
171822    sqlite3Fts3FreeDeferredDoclists(pCsr);
171823    *pRc = rc;
171824  }
171825  return (rc==SQLITE_OK && bMiss);
171826}
171827
171828/*
171829** Advance to the next document that matches the FTS expression in
171830** Fts3Cursor.pExpr.
171831*/
171832static int fts3EvalNext(Fts3Cursor *pCsr){
171833  int rc = SQLITE_OK;             /* Return Code */
171834  Fts3Expr *pExpr = pCsr->pExpr;
171835  assert( pCsr->isEof==0 );
171836  if( pExpr==0 ){
171837    pCsr->isEof = 1;
171838  }else{
171839    do {
171840      if( pCsr->isRequireSeek==0 ){
171841        sqlite3_reset(pCsr->pStmt);
171842      }
171843      assert( sqlite3_data_count(pCsr->pStmt)==0 );
171844      fts3EvalNextRow(pCsr, pExpr, &rc);
171845      pCsr->isEof = pExpr->bEof;
171846      pCsr->isRequireSeek = 1;
171847      pCsr->isMatchinfoNeeded = 1;
171848      pCsr->iPrevId = pExpr->iDocid;
171849    }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) );
171850  }
171851
171852  /* Check if the cursor is past the end of the docid range specified
171853  ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag.  */
171854  if( rc==SQLITE_OK && (
171855        (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
171856     || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
171857  )){
171858    pCsr->isEof = 1;
171859  }
171860
171861  return rc;
171862}
171863
171864/*
171865** Restart interation for expression pExpr so that the next call to
171866** fts3EvalNext() visits the first row. Do not allow incremental
171867** loading or merging of phrase doclists for this iteration.
171868**
171869** If *pRc is other than SQLITE_OK when this function is called, it is
171870** a no-op. If an error occurs within this function, *pRc is set to an
171871** SQLite error code before returning.
171872*/
171873static void fts3EvalRestart(
171874  Fts3Cursor *pCsr,
171875  Fts3Expr *pExpr,
171876  int *pRc
171877){
171878  if( pExpr && *pRc==SQLITE_OK ){
171879    Fts3Phrase *pPhrase = pExpr->pPhrase;
171880
171881    if( pPhrase ){
171882      fts3EvalInvalidatePoslist(pPhrase);
171883      if( pPhrase->bIncr ){
171884        int i;
171885        for(i=0; i<pPhrase->nToken; i++){
171886          Fts3PhraseToken *pToken = &pPhrase->aToken[i];
171887          assert( pToken->pDeferred==0 );
171888          if( pToken->pSegcsr ){
171889            sqlite3Fts3MsrIncrRestart(pToken->pSegcsr);
171890          }
171891        }
171892        *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
171893      }
171894      pPhrase->doclist.pNextDocid = 0;
171895      pPhrase->doclist.iDocid = 0;
171896      pPhrase->pOrPoslist = 0;
171897    }
171898
171899    pExpr->iDocid = 0;
171900    pExpr->bEof = 0;
171901    pExpr->bStart = 0;
171902
171903    fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
171904    fts3EvalRestart(pCsr, pExpr->pRight, pRc);
171905  }
171906}
171907
171908/*
171909** After allocating the Fts3Expr.aMI[] array for each phrase in the
171910** expression rooted at pExpr, the cursor iterates through all rows matched
171911** by pExpr, calling this function for each row. This function increments
171912** the values in Fts3Expr.aMI[] according to the position-list currently
171913** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
171914** expression nodes.
171915*/
171916static void fts3EvalUpdateCounts(Fts3Expr *pExpr, int nCol){
171917  if( pExpr ){
171918    Fts3Phrase *pPhrase = pExpr->pPhrase;
171919    if( pPhrase && pPhrase->doclist.pList ){
171920      int iCol = 0;
171921      char *p = pPhrase->doclist.pList;
171922
171923      do{
171924        u8 c = 0;
171925        int iCnt = 0;
171926        while( 0xFE & (*p | c) ){
171927          if( (c&0x80)==0 ) iCnt++;
171928          c = *p++ & 0x80;
171929        }
171930
171931        /* aMI[iCol*3 + 1] = Number of occurrences
171932        ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
171933        */
171934        pExpr->aMI[iCol*3 + 1] += iCnt;
171935        pExpr->aMI[iCol*3 + 2] += (iCnt>0);
171936        if( *p==0x00 ) break;
171937        p++;
171938        p += fts3GetVarint32(p, &iCol);
171939      }while( iCol<nCol );
171940    }
171941
171942    fts3EvalUpdateCounts(pExpr->pLeft, nCol);
171943    fts3EvalUpdateCounts(pExpr->pRight, nCol);
171944  }
171945}
171946
171947/*
171948** Expression pExpr must be of type FTSQUERY_PHRASE.
171949**
171950** If it is not already allocated and populated, this function allocates and
171951** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
171952** of a NEAR expression, then it also allocates and populates the same array
171953** for all other phrases that are part of the NEAR expression.
171954**
171955** SQLITE_OK is returned if the aMI[] array is successfully allocated and
171956** populated. Otherwise, if an error occurs, an SQLite error code is returned.
171957*/
171958static int fts3EvalGatherStats(
171959  Fts3Cursor *pCsr,               /* Cursor object */
171960  Fts3Expr *pExpr                 /* FTSQUERY_PHRASE expression */
171961){
171962  int rc = SQLITE_OK;             /* Return code */
171963
171964  assert( pExpr->eType==FTSQUERY_PHRASE );
171965  if( pExpr->aMI==0 ){
171966    Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
171967    Fts3Expr *pRoot;                /* Root of NEAR expression */
171968    Fts3Expr *p;                    /* Iterator used for several purposes */
171969
171970    sqlite3_int64 iPrevId = pCsr->iPrevId;
171971    sqlite3_int64 iDocid;
171972    u8 bEof;
171973
171974    /* Find the root of the NEAR expression */
171975    pRoot = pExpr;
171976    while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
171977      pRoot = pRoot->pParent;
171978    }
171979    iDocid = pRoot->iDocid;
171980    bEof = pRoot->bEof;
171981    assert( pRoot->bStart );
171982
171983    /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
171984    for(p=pRoot; p; p=p->pLeft){
171985      Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
171986      assert( pE->aMI==0 );
171987      pE->aMI = (u32 *)sqlite3_malloc64(pTab->nColumn * 3 * sizeof(u32));
171988      if( !pE->aMI ) return SQLITE_NOMEM;
171989      memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
171990    }
171991
171992    fts3EvalRestart(pCsr, pRoot, &rc);
171993
171994    while( pCsr->isEof==0 && rc==SQLITE_OK ){
171995
171996      do {
171997        /* Ensure the %_content statement is reset. */
171998        if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
171999        assert( sqlite3_data_count(pCsr->pStmt)==0 );
172000
172001        /* Advance to the next document */
172002        fts3EvalNextRow(pCsr, pRoot, &rc);
172003        pCsr->isEof = pRoot->bEof;
172004        pCsr->isRequireSeek = 1;
172005        pCsr->isMatchinfoNeeded = 1;
172006        pCsr->iPrevId = pRoot->iDocid;
172007      }while( pCsr->isEof==0
172008           && pRoot->eType==FTSQUERY_NEAR
172009           && sqlite3Fts3EvalTestDeferred(pCsr, &rc)
172010      );
172011
172012      if( rc==SQLITE_OK && pCsr->isEof==0 ){
172013        fts3EvalUpdateCounts(pRoot, pTab->nColumn);
172014      }
172015    }
172016
172017    pCsr->isEof = 0;
172018    pCsr->iPrevId = iPrevId;
172019
172020    if( bEof ){
172021      pRoot->bEof = bEof;
172022    }else{
172023      /* Caution: pRoot may iterate through docids in ascending or descending
172024      ** order. For this reason, even though it seems more defensive, the
172025      ** do loop can not be written:
172026      **
172027      **   do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
172028      */
172029      fts3EvalRestart(pCsr, pRoot, &rc);
172030      do {
172031        fts3EvalNextRow(pCsr, pRoot, &rc);
172032        assert( pRoot->bEof==0 );
172033      }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
172034    }
172035  }
172036  return rc;
172037}
172038
172039/*
172040** This function is used by the matchinfo() module to query a phrase
172041** expression node for the following information:
172042**
172043**   1. The total number of occurrences of the phrase in each column of
172044**      the FTS table (considering all rows), and
172045**
172046**   2. For each column, the number of rows in the table for which the
172047**      column contains at least one instance of the phrase.
172048**
172049** If no error occurs, SQLITE_OK is returned and the values for each column
172050** written into the array aiOut as follows:
172051**
172052**   aiOut[iCol*3 + 1] = Number of occurrences
172053**   aiOut[iCol*3 + 2] = Number of rows containing at least one instance
172054**
172055** Caveats:
172056**
172057**   * If a phrase consists entirely of deferred tokens, then all output
172058**     values are set to the number of documents in the table. In other
172059**     words we assume that very common tokens occur exactly once in each
172060**     column of each row of the table.
172061**
172062**   * If a phrase contains some deferred tokens (and some non-deferred
172063**     tokens), count the potential occurrence identified by considering
172064**     the non-deferred tokens instead of actual phrase occurrences.
172065**
172066**   * If the phrase is part of a NEAR expression, then only phrase instances
172067**     that meet the NEAR constraint are included in the counts.
172068*/
172069SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(
172070  Fts3Cursor *pCsr,               /* FTS cursor handle */
172071  Fts3Expr *pExpr,                /* Phrase expression */
172072  u32 *aiOut                      /* Array to write results into (see above) */
172073){
172074  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
172075  int rc = SQLITE_OK;
172076  int iCol;
172077
172078  if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
172079    assert( pCsr->nDoc>0 );
172080    for(iCol=0; iCol<pTab->nColumn; iCol++){
172081      aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
172082      aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
172083    }
172084  }else{
172085    rc = fts3EvalGatherStats(pCsr, pExpr);
172086    if( rc==SQLITE_OK ){
172087      assert( pExpr->aMI );
172088      for(iCol=0; iCol<pTab->nColumn; iCol++){
172089        aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
172090        aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
172091      }
172092    }
172093  }
172094
172095  return rc;
172096}
172097
172098/*
172099** The expression pExpr passed as the second argument to this function
172100** must be of type FTSQUERY_PHRASE.
172101**
172102** The returned value is either NULL or a pointer to a buffer containing
172103** a position-list indicating the occurrences of the phrase in column iCol
172104** of the current row.
172105**
172106** More specifically, the returned buffer contains 1 varint for each
172107** occurrence of the phrase in the column, stored using the normal (delta+2)
172108** compression and is terminated by either an 0x01 or 0x00 byte. For example,
172109** if the requested column contains "a b X c d X X" and the position-list
172110** for 'X' is requested, the buffer returned may contain:
172111**
172112**     0x04 0x05 0x03 0x01   or   0x04 0x05 0x03 0x00
172113**
172114** This function works regardless of whether or not the phrase is deferred,
172115** incremental, or neither.
172116*/
172117SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(
172118  Fts3Cursor *pCsr,               /* FTS3 cursor object */
172119  Fts3Expr *pExpr,                /* Phrase to return doclist for */
172120  int iCol,                       /* Column to return position list for */
172121  char **ppOut                    /* OUT: Pointer to position list */
172122){
172123  Fts3Phrase *pPhrase = pExpr->pPhrase;
172124  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
172125  char *pIter;
172126  int iThis;
172127  sqlite3_int64 iDocid;
172128
172129  /* If this phrase is applies specifically to some column other than
172130  ** column iCol, return a NULL pointer.  */
172131  *ppOut = 0;
172132  assert( iCol>=0 && iCol<pTab->nColumn );
172133  if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
172134    return SQLITE_OK;
172135  }
172136
172137  iDocid = pExpr->iDocid;
172138  pIter = pPhrase->doclist.pList;
172139  if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
172140    int rc = SQLITE_OK;
172141    int bDescDoclist = pTab->bDescIdx;      /* For DOCID_CMP macro */
172142    int bOr = 0;
172143    u8 bTreeEof = 0;
172144    Fts3Expr *p;                  /* Used to iterate from pExpr to root */
172145    Fts3Expr *pNear;              /* Most senior NEAR ancestor (or pExpr) */
172146    int bMatch;
172147
172148    /* Check if this phrase descends from an OR expression node. If not,
172149    ** return NULL. Otherwise, the entry that corresponds to docid
172150    ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
172151    ** tree that the node is part of has been marked as EOF, but the node
172152    ** itself is not EOF, then it may point to an earlier entry. */
172153    pNear = pExpr;
172154    for(p=pExpr->pParent; p; p=p->pParent){
172155      if( p->eType==FTSQUERY_OR ) bOr = 1;
172156      if( p->eType==FTSQUERY_NEAR ) pNear = p;
172157      if( p->bEof ) bTreeEof = 1;
172158    }
172159    if( bOr==0 ) return SQLITE_OK;
172160
172161    /* This is the descendent of an OR node. In this case we cannot use
172162    ** an incremental phrase. Load the entire doclist for the phrase
172163    ** into memory in this case.  */
172164    if( pPhrase->bIncr ){
172165      int bEofSave = pNear->bEof;
172166      fts3EvalRestart(pCsr, pNear, &rc);
172167      while( rc==SQLITE_OK && !pNear->bEof ){
172168        fts3EvalNextRow(pCsr, pNear, &rc);
172169        if( bEofSave==0 && pNear->iDocid==iDocid ) break;
172170      }
172171      assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
172172    }
172173    if( bTreeEof ){
172174      while( rc==SQLITE_OK && !pNear->bEof ){
172175        fts3EvalNextRow(pCsr, pNear, &rc);
172176      }
172177    }
172178    if( rc!=SQLITE_OK ) return rc;
172179
172180    bMatch = 1;
172181    for(p=pNear; p; p=p->pLeft){
172182      u8 bEof = 0;
172183      Fts3Expr *pTest = p;
172184      Fts3Phrase *pPh;
172185      assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE );
172186      if( pTest->eType==FTSQUERY_NEAR ) pTest = pTest->pRight;
172187      assert( pTest->eType==FTSQUERY_PHRASE );
172188      pPh = pTest->pPhrase;
172189
172190      pIter = pPh->pOrPoslist;
172191      iDocid = pPh->iOrDocid;
172192      if( pCsr->bDesc==bDescDoclist ){
172193        bEof = !pPh->doclist.nAll ||
172194          (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
172195        while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
172196          sqlite3Fts3DoclistNext(
172197              bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
172198              &pIter, &iDocid, &bEof
172199          );
172200        }
172201      }else{
172202        bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
172203        while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
172204          int dummy;
172205          sqlite3Fts3DoclistPrev(
172206              bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
172207              &pIter, &iDocid, &dummy, &bEof
172208              );
172209        }
172210      }
172211      pPh->pOrPoslist = pIter;
172212      pPh->iOrDocid = iDocid;
172213      if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
172214    }
172215
172216    if( bMatch ){
172217      pIter = pPhrase->pOrPoslist;
172218    }else{
172219      pIter = 0;
172220    }
172221  }
172222  if( pIter==0 ) return SQLITE_OK;
172223
172224  if( *pIter==0x01 ){
172225    pIter++;
172226    pIter += fts3GetVarint32(pIter, &iThis);
172227  }else{
172228    iThis = 0;
172229  }
172230  while( iThis<iCol ){
172231    fts3ColumnlistCopy(0, &pIter);
172232    if( *pIter==0x00 ) return SQLITE_OK;
172233    pIter++;
172234    pIter += fts3GetVarint32(pIter, &iThis);
172235  }
172236  if( *pIter==0x00 ){
172237    pIter = 0;
172238  }
172239
172240  *ppOut = ((iCol==iThis)?pIter:0);
172241  return SQLITE_OK;
172242}
172243
172244/*
172245** Free all components of the Fts3Phrase structure that were allocated by
172246** the eval module. Specifically, this means to free:
172247**
172248**   * the contents of pPhrase->doclist, and
172249**   * any Fts3MultiSegReader objects held by phrase tokens.
172250*/
172251SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
172252  if( pPhrase ){
172253    int i;
172254    sqlite3_free(pPhrase->doclist.aAll);
172255    fts3EvalInvalidatePoslist(pPhrase);
172256    memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
172257    for(i=0; i<pPhrase->nToken; i++){
172258      fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
172259      pPhrase->aToken[i].pSegcsr = 0;
172260    }
172261  }
172262}
172263
172264
172265/*
172266** Return SQLITE_CORRUPT_VTAB.
172267*/
172268#ifdef SQLITE_DEBUG
172269SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
172270  return SQLITE_CORRUPT_VTAB;
172271}
172272#endif
172273
172274#if !SQLITE_CORE
172275/*
172276** Initialize API pointer table, if required.
172277*/
172278#ifdef _WIN32
172279__declspec(dllexport)
172280#endif
172281SQLITE_API int sqlite3_fts3_init(
172282  sqlite3 *db,
172283  char **pzErrMsg,
172284  const sqlite3_api_routines *pApi
172285){
172286  SQLITE_EXTENSION_INIT2(pApi)
172287  return sqlite3Fts3Init(db);
172288}
172289#endif
172290
172291#endif
172292
172293/************** End of fts3.c ************************************************/
172294/************** Begin file fts3_aux.c ****************************************/
172295/*
172296** 2011 Jan 27
172297**
172298** The author disclaims copyright to this source code.  In place of
172299** a legal notice, here is a blessing:
172300**
172301**    May you do good and not evil.
172302**    May you find forgiveness for yourself and forgive others.
172303**    May you share freely, never taking more than you give.
172304**
172305******************************************************************************
172306**
172307*/
172308/* #include "fts3Int.h" */
172309#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
172310
172311/* #include <string.h> */
172312/* #include <assert.h> */
172313
172314typedef struct Fts3auxTable Fts3auxTable;
172315typedef struct Fts3auxCursor Fts3auxCursor;
172316
172317struct Fts3auxTable {
172318  sqlite3_vtab base;              /* Base class used by SQLite core */
172319  Fts3Table *pFts3Tab;
172320};
172321
172322struct Fts3auxCursor {
172323  sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
172324  Fts3MultiSegReader csr;        /* Must be right after "base" */
172325  Fts3SegFilter filter;
172326  char *zStop;
172327  int nStop;                      /* Byte-length of string zStop */
172328  int iLangid;                    /* Language id to query */
172329  int isEof;                      /* True if cursor is at EOF */
172330  sqlite3_int64 iRowid;           /* Current rowid */
172331
172332  int iCol;                       /* Current value of 'col' column */
172333  int nStat;                      /* Size of aStat[] array */
172334  struct Fts3auxColstats {
172335    sqlite3_int64 nDoc;           /* 'documents' values for current csr row */
172336    sqlite3_int64 nOcc;           /* 'occurrences' values for current csr row */
172337  } *aStat;
172338};
172339
172340/*
172341** Schema of the terms table.
172342*/
172343#define FTS3_AUX_SCHEMA \
172344  "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)"
172345
172346/*
172347** This function does all the work for both the xConnect and xCreate methods.
172348** These tables have no persistent representation of their own, so xConnect
172349** and xCreate are identical operations.
172350*/
172351static int fts3auxConnectMethod(
172352  sqlite3 *db,                    /* Database connection */
172353  void *pUnused,                  /* Unused */
172354  int argc,                       /* Number of elements in argv array */
172355  const char * const *argv,       /* xCreate/xConnect argument array */
172356  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
172357  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
172358){
172359  char const *zDb;                /* Name of database (e.g. "main") */
172360  char const *zFts3;              /* Name of fts3 table */
172361  int nDb;                        /* Result of strlen(zDb) */
172362  int nFts3;                      /* Result of strlen(zFts3) */
172363  sqlite3_int64 nByte;            /* Bytes of space to allocate here */
172364  int rc;                         /* value returned by declare_vtab() */
172365  Fts3auxTable *p;                /* Virtual table object to return */
172366
172367  UNUSED_PARAMETER(pUnused);
172368
172369  /* The user should invoke this in one of two forms:
172370  **
172371  **     CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
172372  **     CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
172373  */
172374  if( argc!=4 && argc!=5 ) goto bad_args;
172375
172376  zDb = argv[1];
172377  nDb = (int)strlen(zDb);
172378  if( argc==5 ){
172379    if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){
172380      zDb = argv[3];
172381      nDb = (int)strlen(zDb);
172382      zFts3 = argv[4];
172383    }else{
172384      goto bad_args;
172385    }
172386  }else{
172387    zFts3 = argv[3];
172388  }
172389  nFts3 = (int)strlen(zFts3);
172390
172391  rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA);
172392  if( rc!=SQLITE_OK ) return rc;
172393
172394  nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
172395  p = (Fts3auxTable *)sqlite3_malloc64(nByte);
172396  if( !p ) return SQLITE_NOMEM;
172397  memset(p, 0, nByte);
172398
172399  p->pFts3Tab = (Fts3Table *)&p[1];
172400  p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
172401  p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
172402  p->pFts3Tab->db = db;
172403  p->pFts3Tab->nIndex = 1;
172404
172405  memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
172406  memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
172407  sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
172408
172409  *ppVtab = (sqlite3_vtab *)p;
172410  return SQLITE_OK;
172411
172412 bad_args:
172413  sqlite3Fts3ErrMsg(pzErr, "invalid arguments to fts4aux constructor");
172414  return SQLITE_ERROR;
172415}
172416
172417/*
172418** This function does the work for both the xDisconnect and xDestroy methods.
172419** These tables have no persistent representation of their own, so xDisconnect
172420** and xDestroy are identical operations.
172421*/
172422static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
172423  Fts3auxTable *p = (Fts3auxTable *)pVtab;
172424  Fts3Table *pFts3 = p->pFts3Tab;
172425  int i;
172426
172427  /* Free any prepared statements held */
172428  for(i=0; i<SizeofArray(pFts3->aStmt); i++){
172429    sqlite3_finalize(pFts3->aStmt[i]);
172430  }
172431  sqlite3_free(pFts3->zSegmentsTbl);
172432  sqlite3_free(p);
172433  return SQLITE_OK;
172434}
172435
172436#define FTS4AUX_EQ_CONSTRAINT 1
172437#define FTS4AUX_GE_CONSTRAINT 2
172438#define FTS4AUX_LE_CONSTRAINT 4
172439
172440/*
172441** xBestIndex - Analyze a WHERE and ORDER BY clause.
172442*/
172443static int fts3auxBestIndexMethod(
172444  sqlite3_vtab *pVTab,
172445  sqlite3_index_info *pInfo
172446){
172447  int i;
172448  int iEq = -1;
172449  int iGe = -1;
172450  int iLe = -1;
172451  int iLangid = -1;
172452  int iNext = 1;                  /* Next free argvIndex value */
172453
172454  UNUSED_PARAMETER(pVTab);
172455
172456  /* This vtab delivers always results in "ORDER BY term ASC" order. */
172457  if( pInfo->nOrderBy==1
172458   && pInfo->aOrderBy[0].iColumn==0
172459   && pInfo->aOrderBy[0].desc==0
172460  ){
172461    pInfo->orderByConsumed = 1;
172462  }
172463
172464  /* Search for equality and range constraints on the "term" column.
172465  ** And equality constraints on the hidden "languageid" column. */
172466  for(i=0; i<pInfo->nConstraint; i++){
172467    if( pInfo->aConstraint[i].usable ){
172468      int op = pInfo->aConstraint[i].op;
172469      int iCol = pInfo->aConstraint[i].iColumn;
172470
172471      if( iCol==0 ){
172472        if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
172473        if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
172474        if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
172475        if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
172476        if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
172477      }
172478      if( iCol==4 ){
172479        if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iLangid = i;
172480      }
172481    }
172482  }
172483
172484  if( iEq>=0 ){
172485    pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
172486    pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
172487    pInfo->estimatedCost = 5;
172488  }else{
172489    pInfo->idxNum = 0;
172490    pInfo->estimatedCost = 20000;
172491    if( iGe>=0 ){
172492      pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
172493      pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
172494      pInfo->estimatedCost /= 2;
172495    }
172496    if( iLe>=0 ){
172497      pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
172498      pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
172499      pInfo->estimatedCost /= 2;
172500    }
172501  }
172502  if( iLangid>=0 ){
172503    pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
172504    pInfo->estimatedCost--;
172505  }
172506
172507  return SQLITE_OK;
172508}
172509
172510/*
172511** xOpen - Open a cursor.
172512*/
172513static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
172514  Fts3auxCursor *pCsr;            /* Pointer to cursor object to return */
172515
172516  UNUSED_PARAMETER(pVTab);
172517
172518  pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
172519  if( !pCsr ) return SQLITE_NOMEM;
172520  memset(pCsr, 0, sizeof(Fts3auxCursor));
172521
172522  *ppCsr = (sqlite3_vtab_cursor *)pCsr;
172523  return SQLITE_OK;
172524}
172525
172526/*
172527** xClose - Close a cursor.
172528*/
172529static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
172530  Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
172531  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
172532
172533  sqlite3Fts3SegmentsClose(pFts3);
172534  sqlite3Fts3SegReaderFinish(&pCsr->csr);
172535  sqlite3_free((void *)pCsr->filter.zTerm);
172536  sqlite3_free(pCsr->zStop);
172537  sqlite3_free(pCsr->aStat);
172538  sqlite3_free(pCsr);
172539  return SQLITE_OK;
172540}
172541
172542static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
172543  if( nSize>pCsr->nStat ){
172544    struct Fts3auxColstats *aNew;
172545    aNew = (struct Fts3auxColstats *)sqlite3_realloc64(pCsr->aStat,
172546        sizeof(struct Fts3auxColstats) * nSize
172547    );
172548    if( aNew==0 ) return SQLITE_NOMEM;
172549    memset(&aNew[pCsr->nStat], 0,
172550        sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
172551    );
172552    pCsr->aStat = aNew;
172553    pCsr->nStat = nSize;
172554  }
172555  return SQLITE_OK;
172556}
172557
172558/*
172559** xNext - Advance the cursor to the next row, if any.
172560*/
172561static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
172562  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
172563  Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
172564  int rc;
172565
172566  /* Increment our pretend rowid value. */
172567  pCsr->iRowid++;
172568
172569  for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
172570    if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
172571  }
172572
172573  rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
172574  if( rc==SQLITE_ROW ){
172575    int i = 0;
172576    int nDoclist = pCsr->csr.nDoclist;
172577    char *aDoclist = pCsr->csr.aDoclist;
172578    int iCol;
172579
172580    int eState = 0;
172581
172582    if( pCsr->zStop ){
172583      int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
172584      int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
172585      if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
172586        pCsr->isEof = 1;
172587        return SQLITE_OK;
172588      }
172589    }
172590
172591    if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
172592    memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
172593    iCol = 0;
172594
172595    while( i<nDoclist ){
172596      sqlite3_int64 v = 0;
172597
172598      i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
172599      switch( eState ){
172600        /* State 0. In this state the integer just read was a docid. */
172601        case 0:
172602          pCsr->aStat[0].nDoc++;
172603          eState = 1;
172604          iCol = 0;
172605          break;
172606
172607        /* State 1. In this state we are expecting either a 1, indicating
172608        ** that the following integer will be a column number, or the
172609        ** start of a position list for column 0.
172610        **
172611        ** The only difference between state 1 and state 2 is that if the
172612        ** integer encountered in state 1 is not 0 or 1, then we need to
172613        ** increment the column 0 "nDoc" count for this term.
172614        */
172615        case 1:
172616          assert( iCol==0 );
172617          if( v>1 ){
172618            pCsr->aStat[1].nDoc++;
172619          }
172620          eState = 2;
172621          /* fall through */
172622
172623        case 2:
172624          if( v==0 ){       /* 0x00. Next integer will be a docid. */
172625            eState = 0;
172626          }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
172627            eState = 3;
172628          }else{            /* 2 or greater. A position. */
172629            pCsr->aStat[iCol+1].nOcc++;
172630            pCsr->aStat[0].nOcc++;
172631          }
172632          break;
172633
172634        /* State 3. The integer just read is a column number. */
172635        default: assert( eState==3 );
172636          iCol = (int)v;
172637          if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
172638          pCsr->aStat[iCol+1].nDoc++;
172639          eState = 2;
172640          break;
172641      }
172642    }
172643
172644    pCsr->iCol = 0;
172645    rc = SQLITE_OK;
172646  }else{
172647    pCsr->isEof = 1;
172648  }
172649  return rc;
172650}
172651
172652/*
172653** xFilter - Initialize a cursor to point at the start of its data.
172654*/
172655static int fts3auxFilterMethod(
172656  sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
172657  int idxNum,                     /* Strategy index */
172658  const char *idxStr,             /* Unused */
172659  int nVal,                       /* Number of elements in apVal */
172660  sqlite3_value **apVal           /* Arguments for the indexing scheme */
172661){
172662  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
172663  Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
172664  int rc;
172665  int isScan = 0;
172666  int iLangVal = 0;               /* Language id to query */
172667
172668  int iEq = -1;                   /* Index of term=? value in apVal */
172669  int iGe = -1;                   /* Index of term>=? value in apVal */
172670  int iLe = -1;                   /* Index of term<=? value in apVal */
172671  int iLangid = -1;               /* Index of languageid=? value in apVal */
172672  int iNext = 0;
172673
172674  UNUSED_PARAMETER(nVal);
172675  UNUSED_PARAMETER(idxStr);
172676
172677  assert( idxStr==0 );
172678  assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
172679       || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
172680       || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
172681  );
172682
172683  if( idxNum==FTS4AUX_EQ_CONSTRAINT ){
172684    iEq = iNext++;
172685  }else{
172686    isScan = 1;
172687    if( idxNum & FTS4AUX_GE_CONSTRAINT ){
172688      iGe = iNext++;
172689    }
172690    if( idxNum & FTS4AUX_LE_CONSTRAINT ){
172691      iLe = iNext++;
172692    }
172693  }
172694  if( iNext<nVal ){
172695    iLangid = iNext++;
172696  }
172697
172698  /* In case this cursor is being reused, close and zero it. */
172699  testcase(pCsr->filter.zTerm);
172700  sqlite3Fts3SegReaderFinish(&pCsr->csr);
172701  sqlite3_free((void *)pCsr->filter.zTerm);
172702  sqlite3_free(pCsr->aStat);
172703  memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
172704
172705  pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
172706  if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
172707
172708  if( iEq>=0 || iGe>=0 ){
172709    const unsigned char *zStr = sqlite3_value_text(apVal[0]);
172710    assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) );
172711    if( zStr ){
172712      pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
172713      if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
172714      pCsr->filter.nTerm = (int)strlen(pCsr->filter.zTerm);
172715    }
172716  }
172717
172718  if( iLe>=0 ){
172719    pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
172720    if( pCsr->zStop==0 ) return SQLITE_NOMEM;
172721    pCsr->nStop = (int)strlen(pCsr->zStop);
172722  }
172723
172724  if( iLangid>=0 ){
172725    iLangVal = sqlite3_value_int(apVal[iLangid]);
172726
172727    /* If the user specified a negative value for the languageid, use zero
172728    ** instead. This works, as the "languageid=?" constraint will also
172729    ** be tested by the VDBE layer. The test will always be false (since
172730    ** this module will not return a row with a negative languageid), and
172731    ** so the overall query will return zero rows.  */
172732    if( iLangVal<0 ) iLangVal = 0;
172733  }
172734  pCsr->iLangid = iLangVal;
172735
172736  rc = sqlite3Fts3SegReaderCursor(pFts3, iLangVal, 0, FTS3_SEGCURSOR_ALL,
172737      pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
172738  );
172739  if( rc==SQLITE_OK ){
172740    rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
172741  }
172742
172743  if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
172744  return rc;
172745}
172746
172747/*
172748** xEof - Return true if the cursor is at EOF, or false otherwise.
172749*/
172750static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
172751  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
172752  return pCsr->isEof;
172753}
172754
172755/*
172756** xColumn - Return a column value.
172757*/
172758static int fts3auxColumnMethod(
172759  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
172760  sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
172761  int iCol                        /* Index of column to read value from */
172762){
172763  Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
172764
172765  assert( p->isEof==0 );
172766  switch( iCol ){
172767    case 0: /* term */
172768      sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
172769      break;
172770
172771    case 1: /* col */
172772      if( p->iCol ){
172773        sqlite3_result_int(pCtx, p->iCol-1);
172774      }else{
172775        sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC);
172776      }
172777      break;
172778
172779    case 2: /* documents */
172780      sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc);
172781      break;
172782
172783    case 3: /* occurrences */
172784      sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc);
172785      break;
172786
172787    default: /* languageid */
172788      assert( iCol==4 );
172789      sqlite3_result_int(pCtx, p->iLangid);
172790      break;
172791  }
172792
172793  return SQLITE_OK;
172794}
172795
172796/*
172797** xRowid - Return the current rowid for the cursor.
172798*/
172799static int fts3auxRowidMethod(
172800  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
172801  sqlite_int64 *pRowid            /* OUT: Rowid value */
172802){
172803  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
172804  *pRowid = pCsr->iRowid;
172805  return SQLITE_OK;
172806}
172807
172808/*
172809** Register the fts3aux module with database connection db. Return SQLITE_OK
172810** if successful or an error code if sqlite3_create_module() fails.
172811*/
172812SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
172813  static const sqlite3_module fts3aux_module = {
172814     0,                           /* iVersion      */
172815     fts3auxConnectMethod,        /* xCreate       */
172816     fts3auxConnectMethod,        /* xConnect      */
172817     fts3auxBestIndexMethod,      /* xBestIndex    */
172818     fts3auxDisconnectMethod,     /* xDisconnect   */
172819     fts3auxDisconnectMethod,     /* xDestroy      */
172820     fts3auxOpenMethod,           /* xOpen         */
172821     fts3auxCloseMethod,          /* xClose        */
172822     fts3auxFilterMethod,         /* xFilter       */
172823     fts3auxNextMethod,           /* xNext         */
172824     fts3auxEofMethod,            /* xEof          */
172825     fts3auxColumnMethod,         /* xColumn       */
172826     fts3auxRowidMethod,          /* xRowid        */
172827     0,                           /* xUpdate       */
172828     0,                           /* xBegin        */
172829     0,                           /* xSync         */
172830     0,                           /* xCommit       */
172831     0,                           /* xRollback     */
172832     0,                           /* xFindFunction */
172833     0,                           /* xRename       */
172834     0,                           /* xSavepoint    */
172835     0,                           /* xRelease      */
172836     0,                           /* xRollbackTo   */
172837     0                            /* xShadowName   */
172838  };
172839  int rc;                         /* Return code */
172840
172841  rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
172842  return rc;
172843}
172844
172845#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
172846
172847/************** End of fts3_aux.c ********************************************/
172848/************** Begin file fts3_expr.c ***************************************/
172849/*
172850** 2008 Nov 28
172851**
172852** The author disclaims copyright to this source code.  In place of
172853** a legal notice, here is a blessing:
172854**
172855**    May you do good and not evil.
172856**    May you find forgiveness for yourself and forgive others.
172857**    May you share freely, never taking more than you give.
172858**
172859******************************************************************************
172860**
172861** This module contains code that implements a parser for fts3 query strings
172862** (the right-hand argument to the MATCH operator). Because the supported
172863** syntax is relatively simple, the whole tokenizer/parser system is
172864** hand-coded.
172865*/
172866/* #include "fts3Int.h" */
172867#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
172868
172869/*
172870** By default, this module parses the legacy syntax that has been
172871** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
172872** is defined, then it uses the new syntax. The differences between
172873** the new and the old syntaxes are:
172874**
172875**  a) The new syntax supports parenthesis. The old does not.
172876**
172877**  b) The new syntax supports the AND and NOT operators. The old does not.
172878**
172879**  c) The old syntax supports the "-" token qualifier. This is not
172880**     supported by the new syntax (it is replaced by the NOT operator).
172881**
172882**  d) When using the old syntax, the OR operator has a greater precedence
172883**     than an implicit AND. When using the new, both implicity and explicit
172884**     AND operators have a higher precedence than OR.
172885**
172886** If compiled with SQLITE_TEST defined, then this module exports the
172887** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
172888** to zero causes the module to use the old syntax. If it is set to
172889** non-zero the new syntax is activated. This is so both syntaxes can
172890** be tested using a single build of testfixture.
172891**
172892** The following describes the syntax supported by the fts3 MATCH
172893** operator in a similar format to that used by the lemon parser
172894** generator. This module does not use actually lemon, it uses a
172895** custom parser.
172896**
172897**   query ::= andexpr (OR andexpr)*.
172898**
172899**   andexpr ::= notexpr (AND? notexpr)*.
172900**
172901**   notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
172902**   notexpr ::= LP query RP.
172903**
172904**   nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
172905**
172906**   distance_opt ::= .
172907**   distance_opt ::= / INTEGER.
172908**
172909**   phrase ::= TOKEN.
172910**   phrase ::= COLUMN:TOKEN.
172911**   phrase ::= "TOKEN TOKEN TOKEN...".
172912*/
172913
172914#ifdef SQLITE_TEST
172915SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
172916#else
172917# ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
172918#  define sqlite3_fts3_enable_parentheses 1
172919# else
172920#  define sqlite3_fts3_enable_parentheses 0
172921# endif
172922#endif
172923
172924/*
172925** Default span for NEAR operators.
172926*/
172927#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
172928
172929/* #include <string.h> */
172930/* #include <assert.h> */
172931
172932/*
172933** isNot:
172934**   This variable is used by function getNextNode(). When getNextNode() is
172935**   called, it sets ParseContext.isNot to true if the 'next node' is a
172936**   FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
172937**   FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
172938**   zero.
172939*/
172940typedef struct ParseContext ParseContext;
172941struct ParseContext {
172942  sqlite3_tokenizer *pTokenizer;      /* Tokenizer module */
172943  int iLangid;                        /* Language id used with tokenizer */
172944  const char **azCol;                 /* Array of column names for fts3 table */
172945  int bFts4;                          /* True to allow FTS4-only syntax */
172946  int nCol;                           /* Number of entries in azCol[] */
172947  int iDefaultCol;                    /* Default column to query */
172948  int isNot;                          /* True if getNextNode() sees a unary - */
172949  sqlite3_context *pCtx;              /* Write error message here */
172950  int nNest;                          /* Number of nested brackets */
172951};
172952
172953/*
172954** This function is equivalent to the standard isspace() function.
172955**
172956** The standard isspace() can be awkward to use safely, because although it
172957** is defined to accept an argument of type int, its behavior when passed
172958** an integer that falls outside of the range of the unsigned char type
172959** is undefined (and sometimes, "undefined" means segfault). This wrapper
172960** is defined to accept an argument of type char, and always returns 0 for
172961** any values that fall outside of the range of the unsigned char type (i.e.
172962** negative values).
172963*/
172964static int fts3isspace(char c){
172965  return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
172966}
172967
172968/*
172969** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
172970** zero the memory before returning a pointer to it. If unsuccessful,
172971** return NULL.
172972*/
172973static void *fts3MallocZero(sqlite3_int64 nByte){
172974  void *pRet = sqlite3_malloc64(nByte);
172975  if( pRet ) memset(pRet, 0, nByte);
172976  return pRet;
172977}
172978
172979SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(
172980  sqlite3_tokenizer *pTokenizer,
172981  int iLangid,
172982  const char *z,
172983  int n,
172984  sqlite3_tokenizer_cursor **ppCsr
172985){
172986  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
172987  sqlite3_tokenizer_cursor *pCsr = 0;
172988  int rc;
172989
172990  rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
172991  assert( rc==SQLITE_OK || pCsr==0 );
172992  if( rc==SQLITE_OK ){
172993    pCsr->pTokenizer = pTokenizer;
172994    if( pModule->iVersion>=1 ){
172995      rc = pModule->xLanguageid(pCsr, iLangid);
172996      if( rc!=SQLITE_OK ){
172997        pModule->xClose(pCsr);
172998        pCsr = 0;
172999      }
173000    }
173001  }
173002  *ppCsr = pCsr;
173003  return rc;
173004}
173005
173006/*
173007** Function getNextNode(), which is called by fts3ExprParse(), may itself
173008** call fts3ExprParse(). So this forward declaration is required.
173009*/
173010static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
173011
173012/*
173013** Extract the next token from buffer z (length n) using the tokenizer
173014** and other information (column names etc.) in pParse. Create an Fts3Expr
173015** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
173016** single token and set *ppExpr to point to it. If the end of the buffer is
173017** reached before a token is found, set *ppExpr to zero. It is the
173018** responsibility of the caller to eventually deallocate the allocated
173019** Fts3Expr structure (if any) by passing it to sqlite3_free().
173020**
173021** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
173022** fails.
173023*/
173024static int getNextToken(
173025  ParseContext *pParse,                   /* fts3 query parse context */
173026  int iCol,                               /* Value for Fts3Phrase.iColumn */
173027  const char *z, int n,                   /* Input string */
173028  Fts3Expr **ppExpr,                      /* OUT: expression */
173029  int *pnConsumed                         /* OUT: Number of bytes consumed */
173030){
173031  sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
173032  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
173033  int rc;
173034  sqlite3_tokenizer_cursor *pCursor;
173035  Fts3Expr *pRet = 0;
173036  int i = 0;
173037
173038  /* Set variable i to the maximum number of bytes of input to tokenize. */
173039  for(i=0; i<n; i++){
173040    if( sqlite3_fts3_enable_parentheses && (z[i]=='(' || z[i]==')') ) break;
173041    if( z[i]=='"' ) break;
173042  }
173043
173044  *pnConsumed = i;
173045  rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, i, &pCursor);
173046  if( rc==SQLITE_OK ){
173047    const char *zToken;
173048    int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
173049    sqlite3_int64 nByte;                    /* total space to allocate */
173050
173051    rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
173052    if( rc==SQLITE_OK ){
173053      nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
173054      pRet = (Fts3Expr *)fts3MallocZero(nByte);
173055      if( !pRet ){
173056        rc = SQLITE_NOMEM;
173057      }else{
173058        pRet->eType = FTSQUERY_PHRASE;
173059        pRet->pPhrase = (Fts3Phrase *)&pRet[1];
173060        pRet->pPhrase->nToken = 1;
173061        pRet->pPhrase->iColumn = iCol;
173062        pRet->pPhrase->aToken[0].n = nToken;
173063        pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
173064        memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
173065
173066        if( iEnd<n && z[iEnd]=='*' ){
173067          pRet->pPhrase->aToken[0].isPrefix = 1;
173068          iEnd++;
173069        }
173070
173071        while( 1 ){
173072          if( !sqlite3_fts3_enable_parentheses
173073           && iStart>0 && z[iStart-1]=='-'
173074          ){
173075            pParse->isNot = 1;
173076            iStart--;
173077          }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
173078            pRet->pPhrase->aToken[0].bFirst = 1;
173079            iStart--;
173080          }else{
173081            break;
173082          }
173083        }
173084
173085      }
173086      *pnConsumed = iEnd;
173087    }else if( i && rc==SQLITE_DONE ){
173088      rc = SQLITE_OK;
173089    }
173090
173091    pModule->xClose(pCursor);
173092  }
173093
173094  *ppExpr = pRet;
173095  return rc;
173096}
173097
173098
173099/*
173100** Enlarge a memory allocation.  If an out-of-memory allocation occurs,
173101** then free the old allocation.
173102*/
173103static void *fts3ReallocOrFree(void *pOrig, sqlite3_int64 nNew){
173104  void *pRet = sqlite3_realloc64(pOrig, nNew);
173105  if( !pRet ){
173106    sqlite3_free(pOrig);
173107  }
173108  return pRet;
173109}
173110
173111/*
173112** Buffer zInput, length nInput, contains the contents of a quoted string
173113** that appeared as part of an fts3 query expression. Neither quote character
173114** is included in the buffer. This function attempts to tokenize the entire
173115** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE
173116** containing the results.
173117**
173118** If successful, SQLITE_OK is returned and *ppExpr set to point at the
173119** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
173120** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
173121** to 0.
173122*/
173123static int getNextString(
173124  ParseContext *pParse,                   /* fts3 query parse context */
173125  const char *zInput, int nInput,         /* Input string */
173126  Fts3Expr **ppExpr                       /* OUT: expression */
173127){
173128  sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
173129  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
173130  int rc;
173131  Fts3Expr *p = 0;
173132  sqlite3_tokenizer_cursor *pCursor = 0;
173133  char *zTemp = 0;
173134  int nTemp = 0;
173135
173136  const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
173137  int nToken = 0;
173138
173139  /* The final Fts3Expr data structure, including the Fts3Phrase,
173140  ** Fts3PhraseToken structures token buffers are all stored as a single
173141  ** allocation so that the expression can be freed with a single call to
173142  ** sqlite3_free(). Setting this up requires a two pass approach.
173143  **
173144  ** The first pass, in the block below, uses a tokenizer cursor to iterate
173145  ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
173146  ** to assemble data in two dynamic buffers:
173147  **
173148  **   Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
173149  **             structure, followed by the array of Fts3PhraseToken
173150  **             structures. This pass only populates the Fts3PhraseToken array.
173151  **
173152  **   Buffer zTemp: Contains copies of all tokens.
173153  **
173154  ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
173155  ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
173156  ** structures.
173157  */
173158  rc = sqlite3Fts3OpenTokenizer(
173159      pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
173160  if( rc==SQLITE_OK ){
173161    int ii;
173162    for(ii=0; rc==SQLITE_OK; ii++){
173163      const char *zByte;
173164      int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
173165      rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
173166      if( rc==SQLITE_OK ){
173167        Fts3PhraseToken *pToken;
173168
173169        p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
173170        if( !p ) goto no_mem;
173171
173172        zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
173173        if( !zTemp ) goto no_mem;
173174
173175        assert( nToken==ii );
173176        pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
173177        memset(pToken, 0, sizeof(Fts3PhraseToken));
173178
173179        memcpy(&zTemp[nTemp], zByte, nByte);
173180        nTemp += nByte;
173181
173182        pToken->n = nByte;
173183        pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
173184        pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
173185        nToken = ii+1;
173186      }
173187    }
173188
173189    pModule->xClose(pCursor);
173190    pCursor = 0;
173191  }
173192
173193  if( rc==SQLITE_DONE ){
173194    int jj;
173195    char *zBuf = 0;
173196
173197    p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
173198    if( !p ) goto no_mem;
173199    memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
173200    p->eType = FTSQUERY_PHRASE;
173201    p->pPhrase = (Fts3Phrase *)&p[1];
173202    p->pPhrase->iColumn = pParse->iDefaultCol;
173203    p->pPhrase->nToken = nToken;
173204
173205    zBuf = (char *)&p->pPhrase->aToken[nToken];
173206    if( zTemp ){
173207      memcpy(zBuf, zTemp, nTemp);
173208      sqlite3_free(zTemp);
173209    }else{
173210      assert( nTemp==0 );
173211    }
173212
173213    for(jj=0; jj<p->pPhrase->nToken; jj++){
173214      p->pPhrase->aToken[jj].z = zBuf;
173215      zBuf += p->pPhrase->aToken[jj].n;
173216    }
173217    rc = SQLITE_OK;
173218  }
173219
173220  *ppExpr = p;
173221  return rc;
173222no_mem:
173223
173224  if( pCursor ){
173225    pModule->xClose(pCursor);
173226  }
173227  sqlite3_free(zTemp);
173228  sqlite3_free(p);
173229  *ppExpr = 0;
173230  return SQLITE_NOMEM;
173231}
173232
173233/*
173234** The output variable *ppExpr is populated with an allocated Fts3Expr
173235** structure, or set to 0 if the end of the input buffer is reached.
173236**
173237** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
173238** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
173239** If SQLITE_ERROR is returned, pContext is populated with an error message.
173240*/
173241static int getNextNode(
173242  ParseContext *pParse,                   /* fts3 query parse context */
173243  const char *z, int n,                   /* Input string */
173244  Fts3Expr **ppExpr,                      /* OUT: expression */
173245  int *pnConsumed                         /* OUT: Number of bytes consumed */
173246){
173247  static const struct Fts3Keyword {
173248    char *z;                              /* Keyword text */
173249    unsigned char n;                      /* Length of the keyword */
173250    unsigned char parenOnly;              /* Only valid in paren mode */
173251    unsigned char eType;                  /* Keyword code */
173252  } aKeyword[] = {
173253    { "OR" ,  2, 0, FTSQUERY_OR   },
173254    { "AND",  3, 1, FTSQUERY_AND  },
173255    { "NOT",  3, 1, FTSQUERY_NOT  },
173256    { "NEAR", 4, 0, FTSQUERY_NEAR }
173257  };
173258  int ii;
173259  int iCol;
173260  int iColLen;
173261  int rc;
173262  Fts3Expr *pRet = 0;
173263
173264  const char *zInput = z;
173265  int nInput = n;
173266
173267  pParse->isNot = 0;
173268
173269  /* Skip over any whitespace before checking for a keyword, an open or
173270  ** close bracket, or a quoted string.
173271  */
173272  while( nInput>0 && fts3isspace(*zInput) ){
173273    nInput--;
173274    zInput++;
173275  }
173276  if( nInput==0 ){
173277    return SQLITE_DONE;
173278  }
173279
173280  /* See if we are dealing with a keyword. */
173281  for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
173282    const struct Fts3Keyword *pKey = &aKeyword[ii];
173283
173284    if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
173285      continue;
173286    }
173287
173288    if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
173289      int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
173290      int nKey = pKey->n;
173291      char cNext;
173292
173293      /* If this is a "NEAR" keyword, check for an explicit nearness. */
173294      if( pKey->eType==FTSQUERY_NEAR ){
173295        assert( nKey==4 );
173296        if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
173297          nKey += 1+sqlite3Fts3ReadInt(&zInput[nKey+1], &nNear);
173298        }
173299      }
173300
173301      /* At this point this is probably a keyword. But for that to be true,
173302      ** the next byte must contain either whitespace, an open or close
173303      ** parenthesis, a quote character, or EOF.
173304      */
173305      cNext = zInput[nKey];
173306      if( fts3isspace(cNext)
173307       || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
173308      ){
173309        pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr));
173310        if( !pRet ){
173311          return SQLITE_NOMEM;
173312        }
173313        pRet->eType = pKey->eType;
173314        pRet->nNear = nNear;
173315        *ppExpr = pRet;
173316        *pnConsumed = (int)((zInput - z) + nKey);
173317        return SQLITE_OK;
173318      }
173319
173320      /* Turns out that wasn't a keyword after all. This happens if the
173321      ** user has supplied a token such as "ORacle". Continue.
173322      */
173323    }
173324  }
173325
173326  /* See if we are dealing with a quoted phrase. If this is the case, then
173327  ** search for the closing quote and pass the whole string to getNextString()
173328  ** for processing. This is easy to do, as fts3 has no syntax for escaping
173329  ** a quote character embedded in a string.
173330  */
173331  if( *zInput=='"' ){
173332    for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
173333    *pnConsumed = (int)((zInput - z) + ii + 1);
173334    if( ii==nInput ){
173335      return SQLITE_ERROR;
173336    }
173337    return getNextString(pParse, &zInput[1], ii-1, ppExpr);
173338  }
173339
173340  if( sqlite3_fts3_enable_parentheses ){
173341    if( *zInput=='(' ){
173342      int nConsumed = 0;
173343      pParse->nNest++;
173344      rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
173345      *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
173346      return rc;
173347    }else if( *zInput==')' ){
173348      pParse->nNest--;
173349      *pnConsumed = (int)((zInput - z) + 1);
173350      *ppExpr = 0;
173351      return SQLITE_DONE;
173352    }
173353  }
173354
173355  /* If control flows to this point, this must be a regular token, or
173356  ** the end of the input. Read a regular token using the sqlite3_tokenizer
173357  ** interface. Before doing so, figure out if there is an explicit
173358  ** column specifier for the token.
173359  **
173360  ** TODO: Strangely, it is not possible to associate a column specifier
173361  ** with a quoted phrase, only with a single token. Not sure if this was
173362  ** an implementation artifact or an intentional decision when fts3 was
173363  ** first implemented. Whichever it was, this module duplicates the
173364  ** limitation.
173365  */
173366  iCol = pParse->iDefaultCol;
173367  iColLen = 0;
173368  for(ii=0; ii<pParse->nCol; ii++){
173369    const char *zStr = pParse->azCol[ii];
173370    int nStr = (int)strlen(zStr);
173371    if( nInput>nStr && zInput[nStr]==':'
173372     && sqlite3_strnicmp(zStr, zInput, nStr)==0
173373    ){
173374      iCol = ii;
173375      iColLen = (int)((zInput - z) + nStr + 1);
173376      break;
173377    }
173378  }
173379  rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
173380  *pnConsumed += iColLen;
173381  return rc;
173382}
173383
173384/*
173385** The argument is an Fts3Expr structure for a binary operator (any type
173386** except an FTSQUERY_PHRASE). Return an integer value representing the
173387** precedence of the operator. Lower values have a higher precedence (i.e.
173388** group more tightly). For example, in the C language, the == operator
173389** groups more tightly than ||, and would therefore have a higher precedence.
173390**
173391** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
173392** is defined), the order of the operators in precedence from highest to
173393** lowest is:
173394**
173395**   NEAR
173396**   NOT
173397**   AND (including implicit ANDs)
173398**   OR
173399**
173400** Note that when using the old query syntax, the OR operator has a higher
173401** precedence than the AND operator.
173402*/
173403static int opPrecedence(Fts3Expr *p){
173404  assert( p->eType!=FTSQUERY_PHRASE );
173405  if( sqlite3_fts3_enable_parentheses ){
173406    return p->eType;
173407  }else if( p->eType==FTSQUERY_NEAR ){
173408    return 1;
173409  }else if( p->eType==FTSQUERY_OR ){
173410    return 2;
173411  }
173412  assert( p->eType==FTSQUERY_AND );
173413  return 3;
173414}
173415
173416/*
173417** Argument ppHead contains a pointer to the current head of a query
173418** expression tree being parsed. pPrev is the expression node most recently
173419** inserted into the tree. This function adds pNew, which is always a binary
173420** operator node, into the expression tree based on the relative precedence
173421** of pNew and the existing nodes of the tree. This may result in the head
173422** of the tree changing, in which case *ppHead is set to the new root node.
173423*/
173424static void insertBinaryOperator(
173425  Fts3Expr **ppHead,       /* Pointer to the root node of a tree */
173426  Fts3Expr *pPrev,         /* Node most recently inserted into the tree */
173427  Fts3Expr *pNew           /* New binary node to insert into expression tree */
173428){
173429  Fts3Expr *pSplit = pPrev;
173430  while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
173431    pSplit = pSplit->pParent;
173432  }
173433
173434  if( pSplit->pParent ){
173435    assert( pSplit->pParent->pRight==pSplit );
173436    pSplit->pParent->pRight = pNew;
173437    pNew->pParent = pSplit->pParent;
173438  }else{
173439    *ppHead = pNew;
173440  }
173441  pNew->pLeft = pSplit;
173442  pSplit->pParent = pNew;
173443}
173444
173445/*
173446** Parse the fts3 query expression found in buffer z, length n. This function
173447** returns either when the end of the buffer is reached or an unmatched
173448** closing bracket - ')' - is encountered.
173449**
173450** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
173451** parsed form of the expression and *pnConsumed is set to the number of
173452** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
173453** (out of memory error) or SQLITE_ERROR (parse error) is returned.
173454*/
173455static int fts3ExprParse(
173456  ParseContext *pParse,                   /* fts3 query parse context */
173457  const char *z, int n,                   /* Text of MATCH query */
173458  Fts3Expr **ppExpr,                      /* OUT: Parsed query structure */
173459  int *pnConsumed                         /* OUT: Number of bytes consumed */
173460){
173461  Fts3Expr *pRet = 0;
173462  Fts3Expr *pPrev = 0;
173463  Fts3Expr *pNotBranch = 0;               /* Only used in legacy parse mode */
173464  int nIn = n;
173465  const char *zIn = z;
173466  int rc = SQLITE_OK;
173467  int isRequirePhrase = 1;
173468
173469  while( rc==SQLITE_OK ){
173470    Fts3Expr *p = 0;
173471    int nByte = 0;
173472
173473    rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
173474    assert( nByte>0 || (rc!=SQLITE_OK && p==0) );
173475    if( rc==SQLITE_OK ){
173476      if( p ){
173477        int isPhrase;
173478
173479        if( !sqlite3_fts3_enable_parentheses
173480            && p->eType==FTSQUERY_PHRASE && pParse->isNot
173481        ){
173482          /* Create an implicit NOT operator. */
173483          Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
173484          if( !pNot ){
173485            sqlite3Fts3ExprFree(p);
173486            rc = SQLITE_NOMEM;
173487            goto exprparse_out;
173488          }
173489          pNot->eType = FTSQUERY_NOT;
173490          pNot->pRight = p;
173491          p->pParent = pNot;
173492          if( pNotBranch ){
173493            pNot->pLeft = pNotBranch;
173494            pNotBranch->pParent = pNot;
173495          }
173496          pNotBranch = pNot;
173497          p = pPrev;
173498        }else{
173499          int eType = p->eType;
173500          isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
173501
173502          /* The isRequirePhrase variable is set to true if a phrase or
173503          ** an expression contained in parenthesis is required. If a
173504          ** binary operator (AND, OR, NOT or NEAR) is encounted when
173505          ** isRequirePhrase is set, this is a syntax error.
173506          */
173507          if( !isPhrase && isRequirePhrase ){
173508            sqlite3Fts3ExprFree(p);
173509            rc = SQLITE_ERROR;
173510            goto exprparse_out;
173511          }
173512
173513          if( isPhrase && !isRequirePhrase ){
173514            /* Insert an implicit AND operator. */
173515            Fts3Expr *pAnd;
173516            assert( pRet && pPrev );
173517            pAnd = fts3MallocZero(sizeof(Fts3Expr));
173518            if( !pAnd ){
173519              sqlite3Fts3ExprFree(p);
173520              rc = SQLITE_NOMEM;
173521              goto exprparse_out;
173522            }
173523            pAnd->eType = FTSQUERY_AND;
173524            insertBinaryOperator(&pRet, pPrev, pAnd);
173525            pPrev = pAnd;
173526          }
173527
173528          /* This test catches attempts to make either operand of a NEAR
173529           ** operator something other than a phrase. For example, either of
173530           ** the following:
173531           **
173532           **    (bracketed expression) NEAR phrase
173533           **    phrase NEAR (bracketed expression)
173534           **
173535           ** Return an error in either case.
173536           */
173537          if( pPrev && (
173538            (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
173539         || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
173540          )){
173541            sqlite3Fts3ExprFree(p);
173542            rc = SQLITE_ERROR;
173543            goto exprparse_out;
173544          }
173545
173546          if( isPhrase ){
173547            if( pRet ){
173548              assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
173549              pPrev->pRight = p;
173550              p->pParent = pPrev;
173551            }else{
173552              pRet = p;
173553            }
173554          }else{
173555            insertBinaryOperator(&pRet, pPrev, p);
173556          }
173557          isRequirePhrase = !isPhrase;
173558        }
173559        pPrev = p;
173560      }
173561      assert( nByte>0 );
173562    }
173563    assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
173564    nIn -= nByte;
173565    zIn += nByte;
173566  }
173567
173568  if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
173569    rc = SQLITE_ERROR;
173570  }
173571
173572  if( rc==SQLITE_DONE ){
173573    rc = SQLITE_OK;
173574    if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
173575      if( !pRet ){
173576        rc = SQLITE_ERROR;
173577      }else{
173578        Fts3Expr *pIter = pNotBranch;
173579        while( pIter->pLeft ){
173580          pIter = pIter->pLeft;
173581        }
173582        pIter->pLeft = pRet;
173583        pRet->pParent = pIter;
173584        pRet = pNotBranch;
173585      }
173586    }
173587  }
173588  *pnConsumed = n - nIn;
173589
173590exprparse_out:
173591  if( rc!=SQLITE_OK ){
173592    sqlite3Fts3ExprFree(pRet);
173593    sqlite3Fts3ExprFree(pNotBranch);
173594    pRet = 0;
173595  }
173596  *ppExpr = pRet;
173597  return rc;
173598}
173599
173600/*
173601** Return SQLITE_ERROR if the maximum depth of the expression tree passed
173602** as the only argument is more than nMaxDepth.
173603*/
173604static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){
173605  int rc = SQLITE_OK;
173606  if( p ){
173607    if( nMaxDepth<0 ){
173608      rc = SQLITE_TOOBIG;
173609    }else{
173610      rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1);
173611      if( rc==SQLITE_OK ){
173612        rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1);
173613      }
173614    }
173615  }
173616  return rc;
173617}
173618
173619/*
173620** This function attempts to transform the expression tree at (*pp) to
173621** an equivalent but more balanced form. The tree is modified in place.
173622** If successful, SQLITE_OK is returned and (*pp) set to point to the
173623** new root expression node.
173624**
173625** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
173626**
173627** Otherwise, if an error occurs, an SQLite error code is returned and
173628** expression (*pp) freed.
173629*/
173630static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){
173631  int rc = SQLITE_OK;             /* Return code */
173632  Fts3Expr *pRoot = *pp;          /* Initial root node */
173633  Fts3Expr *pFree = 0;            /* List of free nodes. Linked by pParent. */
173634  int eType = pRoot->eType;       /* Type of node in this tree */
173635
173636  if( nMaxDepth==0 ){
173637    rc = SQLITE_ERROR;
173638  }
173639
173640  if( rc==SQLITE_OK ){
173641    if( (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){
173642      Fts3Expr **apLeaf;
173643      apLeaf = (Fts3Expr **)sqlite3_malloc64(sizeof(Fts3Expr *) * nMaxDepth);
173644      if( 0==apLeaf ){
173645        rc = SQLITE_NOMEM;
173646      }else{
173647        memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth);
173648      }
173649
173650      if( rc==SQLITE_OK ){
173651        int i;
173652        Fts3Expr *p;
173653
173654        /* Set $p to point to the left-most leaf in the tree of eType nodes. */
173655        for(p=pRoot; p->eType==eType; p=p->pLeft){
173656          assert( p->pParent==0 || p->pParent->pLeft==p );
173657          assert( p->pLeft && p->pRight );
173658        }
173659
173660        /* This loop runs once for each leaf in the tree of eType nodes. */
173661        while( 1 ){
173662          int iLvl;
173663          Fts3Expr *pParent = p->pParent;     /* Current parent of p */
173664
173665          assert( pParent==0 || pParent->pLeft==p );
173666          p->pParent = 0;
173667          if( pParent ){
173668            pParent->pLeft = 0;
173669          }else{
173670            pRoot = 0;
173671          }
173672          rc = fts3ExprBalance(&p, nMaxDepth-1);
173673          if( rc!=SQLITE_OK ) break;
173674
173675          for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
173676            if( apLeaf[iLvl]==0 ){
173677              apLeaf[iLvl] = p;
173678              p = 0;
173679            }else{
173680              assert( pFree );
173681              pFree->pLeft = apLeaf[iLvl];
173682              pFree->pRight = p;
173683              pFree->pLeft->pParent = pFree;
173684              pFree->pRight->pParent = pFree;
173685
173686              p = pFree;
173687              pFree = pFree->pParent;
173688              p->pParent = 0;
173689              apLeaf[iLvl] = 0;
173690            }
173691          }
173692          if( p ){
173693            sqlite3Fts3ExprFree(p);
173694            rc = SQLITE_TOOBIG;
173695            break;
173696          }
173697
173698          /* If that was the last leaf node, break out of the loop */
173699          if( pParent==0 ) break;
173700
173701          /* Set $p to point to the next leaf in the tree of eType nodes */
173702          for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
173703
173704          /* Remove pParent from the original tree. */
173705          assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent );
173706          pParent->pRight->pParent = pParent->pParent;
173707          if( pParent->pParent ){
173708            pParent->pParent->pLeft = pParent->pRight;
173709          }else{
173710            assert( pParent==pRoot );
173711            pRoot = pParent->pRight;
173712          }
173713
173714          /* Link pParent into the free node list. It will be used as an
173715          ** internal node of the new tree.  */
173716          pParent->pParent = pFree;
173717          pFree = pParent;
173718        }
173719
173720        if( rc==SQLITE_OK ){
173721          p = 0;
173722          for(i=0; i<nMaxDepth; i++){
173723            if( apLeaf[i] ){
173724              if( p==0 ){
173725                p = apLeaf[i];
173726                p->pParent = 0;
173727              }else{
173728                assert( pFree!=0 );
173729                pFree->pRight = p;
173730                pFree->pLeft = apLeaf[i];
173731                pFree->pLeft->pParent = pFree;
173732                pFree->pRight->pParent = pFree;
173733
173734                p = pFree;
173735                pFree = pFree->pParent;
173736                p->pParent = 0;
173737              }
173738            }
173739          }
173740          pRoot = p;
173741        }else{
173742          /* An error occurred. Delete the contents of the apLeaf[] array
173743          ** and pFree list. Everything else is cleaned up by the call to
173744          ** sqlite3Fts3ExprFree(pRoot) below.  */
173745          Fts3Expr *pDel;
173746          for(i=0; i<nMaxDepth; i++){
173747            sqlite3Fts3ExprFree(apLeaf[i]);
173748          }
173749          while( (pDel=pFree)!=0 ){
173750            pFree = pDel->pParent;
173751            sqlite3_free(pDel);
173752          }
173753        }
173754
173755        assert( pFree==0 );
173756        sqlite3_free( apLeaf );
173757      }
173758    }else if( eType==FTSQUERY_NOT ){
173759      Fts3Expr *pLeft = pRoot->pLeft;
173760      Fts3Expr *pRight = pRoot->pRight;
173761
173762      pRoot->pLeft = 0;
173763      pRoot->pRight = 0;
173764      pLeft->pParent = 0;
173765      pRight->pParent = 0;
173766
173767      rc = fts3ExprBalance(&pLeft, nMaxDepth-1);
173768      if( rc==SQLITE_OK ){
173769        rc = fts3ExprBalance(&pRight, nMaxDepth-1);
173770      }
173771
173772      if( rc!=SQLITE_OK ){
173773        sqlite3Fts3ExprFree(pRight);
173774        sqlite3Fts3ExprFree(pLeft);
173775      }else{
173776        assert( pLeft && pRight );
173777        pRoot->pLeft = pLeft;
173778        pLeft->pParent = pRoot;
173779        pRoot->pRight = pRight;
173780        pRight->pParent = pRoot;
173781      }
173782    }
173783  }
173784
173785  if( rc!=SQLITE_OK ){
173786    sqlite3Fts3ExprFree(pRoot);
173787    pRoot = 0;
173788  }
173789  *pp = pRoot;
173790  return rc;
173791}
173792
173793/*
173794** This function is similar to sqlite3Fts3ExprParse(), with the following
173795** differences:
173796**
173797**   1. It does not do expression rebalancing.
173798**   2. It does not check that the expression does not exceed the
173799**      maximum allowable depth.
173800**   3. Even if it fails, *ppExpr may still be set to point to an
173801**      expression tree. It should be deleted using sqlite3Fts3ExprFree()
173802**      in this case.
173803*/
173804static int fts3ExprParseUnbalanced(
173805  sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
173806  int iLangid,                        /* Language id for tokenizer */
173807  char **azCol,                       /* Array of column names for fts3 table */
173808  int bFts4,                          /* True to allow FTS4-only syntax */
173809  int nCol,                           /* Number of entries in azCol[] */
173810  int iDefaultCol,                    /* Default column to query */
173811  const char *z, int n,               /* Text of MATCH query */
173812  Fts3Expr **ppExpr                   /* OUT: Parsed query structure */
173813){
173814  int nParsed;
173815  int rc;
173816  ParseContext sParse;
173817
173818  memset(&sParse, 0, sizeof(ParseContext));
173819  sParse.pTokenizer = pTokenizer;
173820  sParse.iLangid = iLangid;
173821  sParse.azCol = (const char **)azCol;
173822  sParse.nCol = nCol;
173823  sParse.iDefaultCol = iDefaultCol;
173824  sParse.bFts4 = bFts4;
173825  if( z==0 ){
173826    *ppExpr = 0;
173827    return SQLITE_OK;
173828  }
173829  if( n<0 ){
173830    n = (int)strlen(z);
173831  }
173832  rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
173833  assert( rc==SQLITE_OK || *ppExpr==0 );
173834
173835  /* Check for mismatched parenthesis */
173836  if( rc==SQLITE_OK && sParse.nNest ){
173837    rc = SQLITE_ERROR;
173838  }
173839
173840  return rc;
173841}
173842
173843/*
173844** Parameters z and n contain a pointer to and length of a buffer containing
173845** an fts3 query expression, respectively. This function attempts to parse the
173846** query expression and create a tree of Fts3Expr structures representing the
173847** parsed expression. If successful, *ppExpr is set to point to the head
173848** of the parsed expression tree and SQLITE_OK is returned. If an error
173849** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
173850** error) is returned and *ppExpr is set to 0.
173851**
173852** If parameter n is a negative number, then z is assumed to point to a
173853** nul-terminated string and the length is determined using strlen().
173854**
173855** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
173856** use to normalize query tokens while parsing the expression. The azCol[]
173857** array, which is assumed to contain nCol entries, should contain the names
173858** of each column in the target fts3 table, in order from left to right.
173859** Column names must be nul-terminated strings.
173860**
173861** The iDefaultCol parameter should be passed the index of the table column
173862** that appears on the left-hand-side of the MATCH operator (the default
173863** column to match against for tokens for which a column name is not explicitly
173864** specified as part of the query string), or -1 if tokens may by default
173865** match any table column.
173866*/
173867SQLITE_PRIVATE int sqlite3Fts3ExprParse(
173868  sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
173869  int iLangid,                        /* Language id for tokenizer */
173870  char **azCol,                       /* Array of column names for fts3 table */
173871  int bFts4,                          /* True to allow FTS4-only syntax */
173872  int nCol,                           /* Number of entries in azCol[] */
173873  int iDefaultCol,                    /* Default column to query */
173874  const char *z, int n,               /* Text of MATCH query */
173875  Fts3Expr **ppExpr,                  /* OUT: Parsed query structure */
173876  char **pzErr                        /* OUT: Error message (sqlite3_malloc) */
173877){
173878  int rc = fts3ExprParseUnbalanced(
173879      pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr
173880  );
173881
173882  /* Rebalance the expression. And check that its depth does not exceed
173883  ** SQLITE_FTS3_MAX_EXPR_DEPTH.  */
173884  if( rc==SQLITE_OK && *ppExpr ){
173885    rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
173886    if( rc==SQLITE_OK ){
173887      rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
173888    }
173889  }
173890
173891  if( rc!=SQLITE_OK ){
173892    sqlite3Fts3ExprFree(*ppExpr);
173893    *ppExpr = 0;
173894    if( rc==SQLITE_TOOBIG ){
173895      sqlite3Fts3ErrMsg(pzErr,
173896          "FTS expression tree is too large (maximum depth %d)",
173897          SQLITE_FTS3_MAX_EXPR_DEPTH
173898      );
173899      rc = SQLITE_ERROR;
173900    }else if( rc==SQLITE_ERROR ){
173901      sqlite3Fts3ErrMsg(pzErr, "malformed MATCH expression: [%s]", z);
173902    }
173903  }
173904
173905  return rc;
173906}
173907
173908/*
173909** Free a single node of an expression tree.
173910*/
173911static void fts3FreeExprNode(Fts3Expr *p){
173912  assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
173913  sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
173914  sqlite3_free(p->aMI);
173915  sqlite3_free(p);
173916}
173917
173918/*
173919** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
173920**
173921** This function would be simpler if it recursively called itself. But
173922** that would mean passing a sufficiently large expression to ExprParse()
173923** could cause a stack overflow.
173924*/
173925SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *pDel){
173926  Fts3Expr *p;
173927  assert( pDel==0 || pDel->pParent==0 );
173928  for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
173929    assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft );
173930  }
173931  while( p ){
173932    Fts3Expr *pParent = p->pParent;
173933    fts3FreeExprNode(p);
173934    if( pParent && p==pParent->pLeft && pParent->pRight ){
173935      p = pParent->pRight;
173936      while( p && (p->pLeft || p->pRight) ){
173937        assert( p==p->pParent->pRight || p==p->pParent->pLeft );
173938        p = (p->pLeft ? p->pLeft : p->pRight);
173939      }
173940    }else{
173941      p = pParent;
173942    }
173943  }
173944}
173945
173946/****************************************************************************
173947*****************************************************************************
173948** Everything after this point is just test code.
173949*/
173950
173951#ifdef SQLITE_TEST
173952
173953/* #include <stdio.h> */
173954
173955/*
173956** Return a pointer to a buffer containing a text representation of the
173957** expression passed as the first argument. The buffer is obtained from
173958** sqlite3_malloc(). It is the responsibility of the caller to use
173959** sqlite3_free() to release the memory. If an OOM condition is encountered,
173960** NULL is returned.
173961**
173962** If the second argument is not NULL, then its contents are prepended to
173963** the returned expression text and then freed using sqlite3_free().
173964*/
173965static char *exprToString(Fts3Expr *pExpr, char *zBuf){
173966  if( pExpr==0 ){
173967    return sqlite3_mprintf("");
173968  }
173969  switch( pExpr->eType ){
173970    case FTSQUERY_PHRASE: {
173971      Fts3Phrase *pPhrase = pExpr->pPhrase;
173972      int i;
173973      zBuf = sqlite3_mprintf(
173974          "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
173975      for(i=0; zBuf && i<pPhrase->nToken; i++){
173976        zBuf = sqlite3_mprintf("%z %.*s%s", zBuf,
173977            pPhrase->aToken[i].n, pPhrase->aToken[i].z,
173978            (pPhrase->aToken[i].isPrefix?"+":"")
173979        );
173980      }
173981      return zBuf;
173982    }
173983
173984    case FTSQUERY_NEAR:
173985      zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
173986      break;
173987    case FTSQUERY_NOT:
173988      zBuf = sqlite3_mprintf("%zNOT ", zBuf);
173989      break;
173990    case FTSQUERY_AND:
173991      zBuf = sqlite3_mprintf("%zAND ", zBuf);
173992      break;
173993    case FTSQUERY_OR:
173994      zBuf = sqlite3_mprintf("%zOR ", zBuf);
173995      break;
173996  }
173997
173998  if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
173999  if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
174000  if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
174001
174002  if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
174003  if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
174004
174005  return zBuf;
174006}
174007
174008/*
174009** This is the implementation of a scalar SQL function used to test the
174010** expression parser. It should be called as follows:
174011**
174012**   fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
174013**
174014** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
174015** to parse the query expression (see README.tokenizers). The second argument
174016** is the query expression to parse. Each subsequent argument is the name
174017** of a column of the fts3 table that the query expression may refer to.
174018** For example:
174019**
174020**   SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
174021*/
174022static void fts3ExprTestCommon(
174023  int bRebalance,
174024  sqlite3_context *context,
174025  int argc,
174026  sqlite3_value **argv
174027){
174028  sqlite3_tokenizer *pTokenizer = 0;
174029  int rc;
174030  char **azCol = 0;
174031  const char *zExpr;
174032  int nExpr;
174033  int nCol;
174034  int ii;
174035  Fts3Expr *pExpr;
174036  char *zBuf = 0;
174037  Fts3Hash *pHash = (Fts3Hash*)sqlite3_user_data(context);
174038  const char *zTokenizer = 0;
174039  char *zErr = 0;
174040
174041  if( argc<3 ){
174042    sqlite3_result_error(context,
174043        "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
174044    );
174045    return;
174046  }
174047
174048  zTokenizer = (const char*)sqlite3_value_text(argv[0]);
174049  rc = sqlite3Fts3InitTokenizer(pHash, zTokenizer, &pTokenizer, &zErr);
174050  if( rc!=SQLITE_OK ){
174051    if( rc==SQLITE_NOMEM ){
174052      sqlite3_result_error_nomem(context);
174053    }else{
174054      sqlite3_result_error(context, zErr, -1);
174055    }
174056    sqlite3_free(zErr);
174057    return;
174058  }
174059
174060  zExpr = (const char *)sqlite3_value_text(argv[1]);
174061  nExpr = sqlite3_value_bytes(argv[1]);
174062  nCol = argc-2;
174063  azCol = (char **)sqlite3_malloc64(nCol*sizeof(char *));
174064  if( !azCol ){
174065    sqlite3_result_error_nomem(context);
174066    goto exprtest_out;
174067  }
174068  for(ii=0; ii<nCol; ii++){
174069    azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
174070  }
174071
174072  if( bRebalance ){
174073    char *zDummy = 0;
174074    rc = sqlite3Fts3ExprParse(
174075        pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
174076    );
174077    assert( rc==SQLITE_OK || pExpr==0 );
174078    sqlite3_free(zDummy);
174079  }else{
174080    rc = fts3ExprParseUnbalanced(
174081        pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
174082    );
174083  }
174084
174085  if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
174086    sqlite3Fts3ExprFree(pExpr);
174087    sqlite3_result_error(context, "Error parsing expression", -1);
174088  }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
174089    sqlite3_result_error_nomem(context);
174090  }else{
174091    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
174092    sqlite3_free(zBuf);
174093  }
174094
174095  sqlite3Fts3ExprFree(pExpr);
174096
174097exprtest_out:
174098  if( pTokenizer ){
174099    rc = pTokenizer->pModule->xDestroy(pTokenizer);
174100  }
174101  sqlite3_free(azCol);
174102}
174103
174104static void fts3ExprTest(
174105  sqlite3_context *context,
174106  int argc,
174107  sqlite3_value **argv
174108){
174109  fts3ExprTestCommon(0, context, argc, argv);
174110}
174111static void fts3ExprTestRebalance(
174112  sqlite3_context *context,
174113  int argc,
174114  sqlite3_value **argv
174115){
174116  fts3ExprTestCommon(1, context, argc, argv);
174117}
174118
174119/*
174120** Register the query expression parser test function fts3_exprtest()
174121** with database connection db.
174122*/
174123SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash *pHash){
174124  int rc = sqlite3_create_function(
174125      db, "fts3_exprtest", -1, SQLITE_UTF8, (void*)pHash, fts3ExprTest, 0, 0
174126  );
174127  if( rc==SQLITE_OK ){
174128    rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
174129        -1, SQLITE_UTF8, (void*)pHash, fts3ExprTestRebalance, 0, 0
174130    );
174131  }
174132  return rc;
174133}
174134
174135#endif
174136#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
174137
174138/************** End of fts3_expr.c *******************************************/
174139/************** Begin file fts3_hash.c ***************************************/
174140/*
174141** 2001 September 22
174142**
174143** The author disclaims copyright to this source code.  In place of
174144** a legal notice, here is a blessing:
174145**
174146**    May you do good and not evil.
174147**    May you find forgiveness for yourself and forgive others.
174148**    May you share freely, never taking more than you give.
174149**
174150*************************************************************************
174151** This is the implementation of generic hash-tables used in SQLite.
174152** We've modified it slightly to serve as a standalone hash table
174153** implementation for the full-text indexing module.
174154*/
174155
174156/*
174157** The code in this file is only compiled if:
174158**
174159**     * The FTS3 module is being built as an extension
174160**       (in which case SQLITE_CORE is not defined), or
174161**
174162**     * The FTS3 module is being built into the core of
174163**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
174164*/
174165/* #include "fts3Int.h" */
174166#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
174167
174168/* #include <assert.h> */
174169/* #include <stdlib.h> */
174170/* #include <string.h> */
174171
174172/* #include "fts3_hash.h" */
174173
174174/*
174175** Malloc and Free functions
174176*/
174177static void *fts3HashMalloc(sqlite3_int64 n){
174178  void *p = sqlite3_malloc64(n);
174179  if( p ){
174180    memset(p, 0, n);
174181  }
174182  return p;
174183}
174184static void fts3HashFree(void *p){
174185  sqlite3_free(p);
174186}
174187
174188/* Turn bulk memory into a hash table object by initializing the
174189** fields of the Hash structure.
174190**
174191** "pNew" is a pointer to the hash table that is to be initialized.
174192** keyClass is one of the constants
174193** FTS3_HASH_BINARY or FTS3_HASH_STRING.  The value of keyClass
174194** determines what kind of key the hash table will use.  "copyKey" is
174195** true if the hash table should make its own private copy of keys and
174196** false if it should just use the supplied pointer.
174197*/
174198SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
174199  assert( pNew!=0 );
174200  assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
174201  pNew->keyClass = keyClass;
174202  pNew->copyKey = copyKey;
174203  pNew->first = 0;
174204  pNew->count = 0;
174205  pNew->htsize = 0;
174206  pNew->ht = 0;
174207}
174208
174209/* Remove all entries from a hash table.  Reclaim all memory.
174210** Call this routine to delete a hash table or to reset a hash table
174211** to the empty state.
174212*/
174213SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
174214  Fts3HashElem *elem;         /* For looping over all elements of the table */
174215
174216  assert( pH!=0 );
174217  elem = pH->first;
174218  pH->first = 0;
174219  fts3HashFree(pH->ht);
174220  pH->ht = 0;
174221  pH->htsize = 0;
174222  while( elem ){
174223    Fts3HashElem *next_elem = elem->next;
174224    if( pH->copyKey && elem->pKey ){
174225      fts3HashFree(elem->pKey);
174226    }
174227    fts3HashFree(elem);
174228    elem = next_elem;
174229  }
174230  pH->count = 0;
174231}
174232
174233/*
174234** Hash and comparison functions when the mode is FTS3_HASH_STRING
174235*/
174236static int fts3StrHash(const void *pKey, int nKey){
174237  const char *z = (const char *)pKey;
174238  unsigned h = 0;
174239  if( nKey<=0 ) nKey = (int) strlen(z);
174240  while( nKey > 0  ){
174241    h = (h<<3) ^ h ^ *z++;
174242    nKey--;
174243  }
174244  return (int)(h & 0x7fffffff);
174245}
174246static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
174247  if( n1!=n2 ) return 1;
174248  return strncmp((const char*)pKey1,(const char*)pKey2,n1);
174249}
174250
174251/*
174252** Hash and comparison functions when the mode is FTS3_HASH_BINARY
174253*/
174254static int fts3BinHash(const void *pKey, int nKey){
174255  int h = 0;
174256  const char *z = (const char *)pKey;
174257  while( nKey-- > 0 ){
174258    h = (h<<3) ^ h ^ *(z++);
174259  }
174260  return h & 0x7fffffff;
174261}
174262static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
174263  if( n1!=n2 ) return 1;
174264  return memcmp(pKey1,pKey2,n1);
174265}
174266
174267/*
174268** Return a pointer to the appropriate hash function given the key class.
174269**
174270** The C syntax in this function definition may be unfamilar to some
174271** programmers, so we provide the following additional explanation:
174272**
174273** The name of the function is "ftsHashFunction".  The function takes a
174274** single parameter "keyClass".  The return value of ftsHashFunction()
174275** is a pointer to another function.  Specifically, the return value
174276** of ftsHashFunction() is a pointer to a function that takes two parameters
174277** with types "const void*" and "int" and returns an "int".
174278*/
174279static int (*ftsHashFunction(int keyClass))(const void*,int){
174280  if( keyClass==FTS3_HASH_STRING ){
174281    return &fts3StrHash;
174282  }else{
174283    assert( keyClass==FTS3_HASH_BINARY );
174284    return &fts3BinHash;
174285  }
174286}
174287
174288/*
174289** Return a pointer to the appropriate hash function given the key class.
174290**
174291** For help in interpreted the obscure C code in the function definition,
174292** see the header comment on the previous function.
174293*/
174294static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
174295  if( keyClass==FTS3_HASH_STRING ){
174296    return &fts3StrCompare;
174297  }else{
174298    assert( keyClass==FTS3_HASH_BINARY );
174299    return &fts3BinCompare;
174300  }
174301}
174302
174303/* Link an element into the hash table
174304*/
174305static void fts3HashInsertElement(
174306  Fts3Hash *pH,            /* The complete hash table */
174307  struct _fts3ht *pEntry,  /* The entry into which pNew is inserted */
174308  Fts3HashElem *pNew       /* The element to be inserted */
174309){
174310  Fts3HashElem *pHead;     /* First element already in pEntry */
174311  pHead = pEntry->chain;
174312  if( pHead ){
174313    pNew->next = pHead;
174314    pNew->prev = pHead->prev;
174315    if( pHead->prev ){ pHead->prev->next = pNew; }
174316    else             { pH->first = pNew; }
174317    pHead->prev = pNew;
174318  }else{
174319    pNew->next = pH->first;
174320    if( pH->first ){ pH->first->prev = pNew; }
174321    pNew->prev = 0;
174322    pH->first = pNew;
174323  }
174324  pEntry->count++;
174325  pEntry->chain = pNew;
174326}
174327
174328
174329/* Resize the hash table so that it cantains "new_size" buckets.
174330** "new_size" must be a power of 2.  The hash table might fail
174331** to resize if sqliteMalloc() fails.
174332**
174333** Return non-zero if a memory allocation error occurs.
174334*/
174335static int fts3Rehash(Fts3Hash *pH, int new_size){
174336  struct _fts3ht *new_ht;          /* The new hash table */
174337  Fts3HashElem *elem, *next_elem;  /* For looping over existing elements */
174338  int (*xHash)(const void*,int);   /* The hash function */
174339
174340  assert( (new_size & (new_size-1))==0 );
174341  new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
174342  if( new_ht==0 ) return 1;
174343  fts3HashFree(pH->ht);
174344  pH->ht = new_ht;
174345  pH->htsize = new_size;
174346  xHash = ftsHashFunction(pH->keyClass);
174347  for(elem=pH->first, pH->first=0; elem; elem = next_elem){
174348    int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
174349    next_elem = elem->next;
174350    fts3HashInsertElement(pH, &new_ht[h], elem);
174351  }
174352  return 0;
174353}
174354
174355/* This function (for internal use only) locates an element in an
174356** hash table that matches the given key.  The hash for this key has
174357** already been computed and is passed as the 4th parameter.
174358*/
174359static Fts3HashElem *fts3FindElementByHash(
174360  const Fts3Hash *pH, /* The pH to be searched */
174361  const void *pKey,   /* The key we are searching for */
174362  int nKey,
174363  int h               /* The hash for this key. */
174364){
174365  Fts3HashElem *elem;            /* Used to loop thru the element list */
174366  int count;                     /* Number of elements left to test */
174367  int (*xCompare)(const void*,int,const void*,int);  /* comparison function */
174368
174369  if( pH->ht ){
174370    struct _fts3ht *pEntry = &pH->ht[h];
174371    elem = pEntry->chain;
174372    count = pEntry->count;
174373    xCompare = ftsCompareFunction(pH->keyClass);
174374    while( count-- && elem ){
174375      if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
174376        return elem;
174377      }
174378      elem = elem->next;
174379    }
174380  }
174381  return 0;
174382}
174383
174384/* Remove a single entry from the hash table given a pointer to that
174385** element and a hash on the element's key.
174386*/
174387static void fts3RemoveElementByHash(
174388  Fts3Hash *pH,         /* The pH containing "elem" */
174389  Fts3HashElem* elem,   /* The element to be removed from the pH */
174390  int h                 /* Hash value for the element */
174391){
174392  struct _fts3ht *pEntry;
174393  if( elem->prev ){
174394    elem->prev->next = elem->next;
174395  }else{
174396    pH->first = elem->next;
174397  }
174398  if( elem->next ){
174399    elem->next->prev = elem->prev;
174400  }
174401  pEntry = &pH->ht[h];
174402  if( pEntry->chain==elem ){
174403    pEntry->chain = elem->next;
174404  }
174405  pEntry->count--;
174406  if( pEntry->count<=0 ){
174407    pEntry->chain = 0;
174408  }
174409  if( pH->copyKey && elem->pKey ){
174410    fts3HashFree(elem->pKey);
174411  }
174412  fts3HashFree( elem );
174413  pH->count--;
174414  if( pH->count<=0 ){
174415    assert( pH->first==0 );
174416    assert( pH->count==0 );
174417    fts3HashClear(pH);
174418  }
174419}
174420
174421SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
174422  const Fts3Hash *pH,
174423  const void *pKey,
174424  int nKey
174425){
174426  int h;                          /* A hash on key */
174427  int (*xHash)(const void*,int);  /* The hash function */
174428
174429  if( pH==0 || pH->ht==0 ) return 0;
174430  xHash = ftsHashFunction(pH->keyClass);
174431  assert( xHash!=0 );
174432  h = (*xHash)(pKey,nKey);
174433  assert( (pH->htsize & (pH->htsize-1))==0 );
174434  return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
174435}
174436
174437/*
174438** Attempt to locate an element of the hash table pH with a key
174439** that matches pKey,nKey.  Return the data for this element if it is
174440** found, or NULL if there is no match.
174441*/
174442SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
174443  Fts3HashElem *pElem;            /* The element that matches key (if any) */
174444
174445  pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
174446  return pElem ? pElem->data : 0;
174447}
174448
174449/* Insert an element into the hash table pH.  The key is pKey,nKey
174450** and the data is "data".
174451**
174452** If no element exists with a matching key, then a new
174453** element is created.  A copy of the key is made if the copyKey
174454** flag is set.  NULL is returned.
174455**
174456** If another element already exists with the same key, then the
174457** new data replaces the old data and the old data is returned.
174458** The key is not copied in this instance.  If a malloc fails, then
174459** the new data is returned and the hash table is unchanged.
174460**
174461** If the "data" parameter to this function is NULL, then the
174462** element corresponding to "key" is removed from the hash table.
174463*/
174464SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
174465  Fts3Hash *pH,        /* The hash table to insert into */
174466  const void *pKey,    /* The key */
174467  int nKey,            /* Number of bytes in the key */
174468  void *data           /* The data */
174469){
174470  int hraw;                 /* Raw hash value of the key */
174471  int h;                    /* the hash of the key modulo hash table size */
174472  Fts3HashElem *elem;       /* Used to loop thru the element list */
174473  Fts3HashElem *new_elem;   /* New element added to the pH */
174474  int (*xHash)(const void*,int);  /* The hash function */
174475
174476  assert( pH!=0 );
174477  xHash = ftsHashFunction(pH->keyClass);
174478  assert( xHash!=0 );
174479  hraw = (*xHash)(pKey, nKey);
174480  assert( (pH->htsize & (pH->htsize-1))==0 );
174481  h = hraw & (pH->htsize-1);
174482  elem = fts3FindElementByHash(pH,pKey,nKey,h);
174483  if( elem ){
174484    void *old_data = elem->data;
174485    if( data==0 ){
174486      fts3RemoveElementByHash(pH,elem,h);
174487    }else{
174488      elem->data = data;
174489    }
174490    return old_data;
174491  }
174492  if( data==0 ) return 0;
174493  if( (pH->htsize==0 && fts3Rehash(pH,8))
174494   || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
174495  ){
174496    pH->count = 0;
174497    return data;
174498  }
174499  assert( pH->htsize>0 );
174500  new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
174501  if( new_elem==0 ) return data;
174502  if( pH->copyKey && pKey!=0 ){
174503    new_elem->pKey = fts3HashMalloc( nKey );
174504    if( new_elem->pKey==0 ){
174505      fts3HashFree(new_elem);
174506      return data;
174507    }
174508    memcpy((void*)new_elem->pKey, pKey, nKey);
174509  }else{
174510    new_elem->pKey = (void*)pKey;
174511  }
174512  new_elem->nKey = nKey;
174513  pH->count++;
174514  assert( pH->htsize>0 );
174515  assert( (pH->htsize & (pH->htsize-1))==0 );
174516  h = hraw & (pH->htsize-1);
174517  fts3HashInsertElement(pH, &pH->ht[h], new_elem);
174518  new_elem->data = data;
174519  return 0;
174520}
174521
174522#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
174523
174524/************** End of fts3_hash.c *******************************************/
174525/************** Begin file fts3_porter.c *************************************/
174526/*
174527** 2006 September 30
174528**
174529** The author disclaims copyright to this source code.  In place of
174530** a legal notice, here is a blessing:
174531**
174532**    May you do good and not evil.
174533**    May you find forgiveness for yourself and forgive others.
174534**    May you share freely, never taking more than you give.
174535**
174536*************************************************************************
174537** Implementation of the full-text-search tokenizer that implements
174538** a Porter stemmer.
174539*/
174540
174541/*
174542** The code in this file is only compiled if:
174543**
174544**     * The FTS3 module is being built as an extension
174545**       (in which case SQLITE_CORE is not defined), or
174546**
174547**     * The FTS3 module is being built into the core of
174548**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
174549*/
174550/* #include "fts3Int.h" */
174551#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
174552
174553/* #include <assert.h> */
174554/* #include <stdlib.h> */
174555/* #include <stdio.h> */
174556/* #include <string.h> */
174557
174558/* #include "fts3_tokenizer.h" */
174559
174560/*
174561** Class derived from sqlite3_tokenizer
174562*/
174563typedef struct porter_tokenizer {
174564  sqlite3_tokenizer base;      /* Base class */
174565} porter_tokenizer;
174566
174567/*
174568** Class derived from sqlite3_tokenizer_cursor
174569*/
174570typedef struct porter_tokenizer_cursor {
174571  sqlite3_tokenizer_cursor base;
174572  const char *zInput;          /* input we are tokenizing */
174573  int nInput;                  /* size of the input */
174574  int iOffset;                 /* current position in zInput */
174575  int iToken;                  /* index of next token to be returned */
174576  char *zToken;                /* storage for current token */
174577  int nAllocated;              /* space allocated to zToken buffer */
174578} porter_tokenizer_cursor;
174579
174580
174581/*
174582** Create a new tokenizer instance.
174583*/
174584static int porterCreate(
174585  int argc, const char * const *argv,
174586  sqlite3_tokenizer **ppTokenizer
174587){
174588  porter_tokenizer *t;
174589
174590  UNUSED_PARAMETER(argc);
174591  UNUSED_PARAMETER(argv);
174592
174593  t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
174594  if( t==NULL ) return SQLITE_NOMEM;
174595  memset(t, 0, sizeof(*t));
174596  *ppTokenizer = &t->base;
174597  return SQLITE_OK;
174598}
174599
174600/*
174601** Destroy a tokenizer
174602*/
174603static int porterDestroy(sqlite3_tokenizer *pTokenizer){
174604  sqlite3_free(pTokenizer);
174605  return SQLITE_OK;
174606}
174607
174608/*
174609** Prepare to begin tokenizing a particular string.  The input
174610** string to be tokenized is zInput[0..nInput-1].  A cursor
174611** used to incrementally tokenize this string is returned in
174612** *ppCursor.
174613*/
174614static int porterOpen(
174615  sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
174616  const char *zInput, int nInput,        /* String to be tokenized */
174617  sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
174618){
174619  porter_tokenizer_cursor *c;
174620
174621  UNUSED_PARAMETER(pTokenizer);
174622
174623  c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
174624  if( c==NULL ) return SQLITE_NOMEM;
174625
174626  c->zInput = zInput;
174627  if( zInput==0 ){
174628    c->nInput = 0;
174629  }else if( nInput<0 ){
174630    c->nInput = (int)strlen(zInput);
174631  }else{
174632    c->nInput = nInput;
174633  }
174634  c->iOffset = 0;                 /* start tokenizing at the beginning */
174635  c->iToken = 0;
174636  c->zToken = NULL;               /* no space allocated, yet. */
174637  c->nAllocated = 0;
174638
174639  *ppCursor = &c->base;
174640  return SQLITE_OK;
174641}
174642
174643/*
174644** Close a tokenization cursor previously opened by a call to
174645** porterOpen() above.
174646*/
174647static int porterClose(sqlite3_tokenizer_cursor *pCursor){
174648  porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
174649  sqlite3_free(c->zToken);
174650  sqlite3_free(c);
174651  return SQLITE_OK;
174652}
174653/*
174654** Vowel or consonant
174655*/
174656static const char cType[] = {
174657   0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
174658   1, 1, 1, 2, 1
174659};
174660
174661/*
174662** isConsonant() and isVowel() determine if their first character in
174663** the string they point to is a consonant or a vowel, according
174664** to Porter ruls.
174665**
174666** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
174667** 'Y' is a consonant unless it follows another consonant,
174668** in which case it is a vowel.
174669**
174670** In these routine, the letters are in reverse order.  So the 'y' rule
174671** is that 'y' is a consonant unless it is followed by another
174672** consonent.
174673*/
174674static int isVowel(const char*);
174675static int isConsonant(const char *z){
174676  int j;
174677  char x = *z;
174678  if( x==0 ) return 0;
174679  assert( x>='a' && x<='z' );
174680  j = cType[x-'a'];
174681  if( j<2 ) return j;
174682  return z[1]==0 || isVowel(z + 1);
174683}
174684static int isVowel(const char *z){
174685  int j;
174686  char x = *z;
174687  if( x==0 ) return 0;
174688  assert( x>='a' && x<='z' );
174689  j = cType[x-'a'];
174690  if( j<2 ) return 1-j;
174691  return isConsonant(z + 1);
174692}
174693
174694/*
174695** Let any sequence of one or more vowels be represented by V and let
174696** C be sequence of one or more consonants.  Then every word can be
174697** represented as:
174698**
174699**           [C] (VC){m} [V]
174700**
174701** In prose:  A word is an optional consonant followed by zero or
174702** vowel-consonant pairs followed by an optional vowel.  "m" is the
174703** number of vowel consonant pairs.  This routine computes the value
174704** of m for the first i bytes of a word.
174705**
174706** Return true if the m-value for z is 1 or more.  In other words,
174707** return true if z contains at least one vowel that is followed
174708** by a consonant.
174709**
174710** In this routine z[] is in reverse order.  So we are really looking
174711** for an instance of a consonant followed by a vowel.
174712*/
174713static int m_gt_0(const char *z){
174714  while( isVowel(z) ){ z++; }
174715  if( *z==0 ) return 0;
174716  while( isConsonant(z) ){ z++; }
174717  return *z!=0;
174718}
174719
174720/* Like mgt0 above except we are looking for a value of m which is
174721** exactly 1
174722*/
174723static int m_eq_1(const char *z){
174724  while( isVowel(z) ){ z++; }
174725  if( *z==0 ) return 0;
174726  while( isConsonant(z) ){ z++; }
174727  if( *z==0 ) return 0;
174728  while( isVowel(z) ){ z++; }
174729  if( *z==0 ) return 1;
174730  while( isConsonant(z) ){ z++; }
174731  return *z==0;
174732}
174733
174734/* Like mgt0 above except we are looking for a value of m>1 instead
174735** or m>0
174736*/
174737static int m_gt_1(const char *z){
174738  while( isVowel(z) ){ z++; }
174739  if( *z==0 ) return 0;
174740  while( isConsonant(z) ){ z++; }
174741  if( *z==0 ) return 0;
174742  while( isVowel(z) ){ z++; }
174743  if( *z==0 ) return 0;
174744  while( isConsonant(z) ){ z++; }
174745  return *z!=0;
174746}
174747
174748/*
174749** Return TRUE if there is a vowel anywhere within z[0..n-1]
174750*/
174751static int hasVowel(const char *z){
174752  while( isConsonant(z) ){ z++; }
174753  return *z!=0;
174754}
174755
174756/*
174757** Return TRUE if the word ends in a double consonant.
174758**
174759** The text is reversed here. So we are really looking at
174760** the first two characters of z[].
174761*/
174762static int doubleConsonant(const char *z){
174763  return isConsonant(z) && z[0]==z[1];
174764}
174765
174766/*
174767** Return TRUE if the word ends with three letters which
174768** are consonant-vowel-consonent and where the final consonant
174769** is not 'w', 'x', or 'y'.
174770**
174771** The word is reversed here.  So we are really checking the
174772** first three letters and the first one cannot be in [wxy].
174773*/
174774static int star_oh(const char *z){
174775  return
174776    isConsonant(z) &&
174777    z[0]!='w' && z[0]!='x' && z[0]!='y' &&
174778    isVowel(z+1) &&
174779    isConsonant(z+2);
174780}
174781
174782/*
174783** If the word ends with zFrom and xCond() is true for the stem
174784** of the word that preceeds the zFrom ending, then change the
174785** ending to zTo.
174786**
174787** The input word *pz and zFrom are both in reverse order.  zTo
174788** is in normal order.
174789**
174790** Return TRUE if zFrom matches.  Return FALSE if zFrom does not
174791** match.  Not that TRUE is returned even if xCond() fails and
174792** no substitution occurs.
174793*/
174794static int stem(
174795  char **pz,             /* The word being stemmed (Reversed) */
174796  const char *zFrom,     /* If the ending matches this... (Reversed) */
174797  const char *zTo,       /* ... change the ending to this (not reversed) */
174798  int (*xCond)(const char*)   /* Condition that must be true */
174799){
174800  char *z = *pz;
174801  while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
174802  if( *zFrom!=0 ) return 0;
174803  if( xCond && !xCond(z) ) return 1;
174804  while( *zTo ){
174805    *(--z) = *(zTo++);
174806  }
174807  *pz = z;
174808  return 1;
174809}
174810
174811/*
174812** This is the fallback stemmer used when the porter stemmer is
174813** inappropriate.  The input word is copied into the output with
174814** US-ASCII case folding.  If the input word is too long (more
174815** than 20 bytes if it contains no digits or more than 6 bytes if
174816** it contains digits) then word is truncated to 20 or 6 bytes
174817** by taking 10 or 3 bytes from the beginning and end.
174818*/
174819static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
174820  int i, mx, j;
174821  int hasDigit = 0;
174822  for(i=0; i<nIn; i++){
174823    char c = zIn[i];
174824    if( c>='A' && c<='Z' ){
174825      zOut[i] = c - 'A' + 'a';
174826    }else{
174827      if( c>='0' && c<='9' ) hasDigit = 1;
174828      zOut[i] = c;
174829    }
174830  }
174831  mx = hasDigit ? 3 : 10;
174832  if( nIn>mx*2 ){
174833    for(j=mx, i=nIn-mx; i<nIn; i++, j++){
174834      zOut[j] = zOut[i];
174835    }
174836    i = j;
174837  }
174838  zOut[i] = 0;
174839  *pnOut = i;
174840}
174841
174842
174843/*
174844** Stem the input word zIn[0..nIn-1].  Store the output in zOut.
174845** zOut is at least big enough to hold nIn bytes.  Write the actual
174846** size of the output word (exclusive of the '\0' terminator) into *pnOut.
174847**
174848** Any upper-case characters in the US-ASCII character set ([A-Z])
174849** are converted to lower case.  Upper-case UTF characters are
174850** unchanged.
174851**
174852** Words that are longer than about 20 bytes are stemmed by retaining
174853** a few bytes from the beginning and the end of the word.  If the
174854** word contains digits, 3 bytes are taken from the beginning and
174855** 3 bytes from the end.  For long words without digits, 10 bytes
174856** are taken from each end.  US-ASCII case folding still applies.
174857**
174858** If the input word contains not digits but does characters not
174859** in [a-zA-Z] then no stemming is attempted and this routine just
174860** copies the input into the input into the output with US-ASCII
174861** case folding.
174862**
174863** Stemming never increases the length of the word.  So there is
174864** no chance of overflowing the zOut buffer.
174865*/
174866static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
174867  int i, j;
174868  char zReverse[28];
174869  char *z, *z2;
174870  if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
174871    /* The word is too big or too small for the porter stemmer.
174872    ** Fallback to the copy stemmer */
174873    copy_stemmer(zIn, nIn, zOut, pnOut);
174874    return;
174875  }
174876  for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
174877    char c = zIn[i];
174878    if( c>='A' && c<='Z' ){
174879      zReverse[j] = c + 'a' - 'A';
174880    }else if( c>='a' && c<='z' ){
174881      zReverse[j] = c;
174882    }else{
174883      /* The use of a character not in [a-zA-Z] means that we fallback
174884      ** to the copy stemmer */
174885      copy_stemmer(zIn, nIn, zOut, pnOut);
174886      return;
174887    }
174888  }
174889  memset(&zReverse[sizeof(zReverse)-5], 0, 5);
174890  z = &zReverse[j+1];
174891
174892
174893  /* Step 1a */
174894  if( z[0]=='s' ){
174895    if(
174896     !stem(&z, "sess", "ss", 0) &&
174897     !stem(&z, "sei", "i", 0)  &&
174898     !stem(&z, "ss", "ss", 0)
174899    ){
174900      z++;
174901    }
174902  }
174903
174904  /* Step 1b */
174905  z2 = z;
174906  if( stem(&z, "dee", "ee", m_gt_0) ){
174907    /* Do nothing.  The work was all in the test */
174908  }else if(
174909     (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
174910      && z!=z2
174911  ){
174912     if( stem(&z, "ta", "ate", 0) ||
174913         stem(&z, "lb", "ble", 0) ||
174914         stem(&z, "zi", "ize", 0) ){
174915       /* Do nothing.  The work was all in the test */
174916     }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
174917       z++;
174918     }else if( m_eq_1(z) && star_oh(z) ){
174919       *(--z) = 'e';
174920     }
174921  }
174922
174923  /* Step 1c */
174924  if( z[0]=='y' && hasVowel(z+1) ){
174925    z[0] = 'i';
174926  }
174927
174928  /* Step 2 */
174929  switch( z[1] ){
174930   case 'a':
174931     if( !stem(&z, "lanoita", "ate", m_gt_0) ){
174932       stem(&z, "lanoit", "tion", m_gt_0);
174933     }
174934     break;
174935   case 'c':
174936     if( !stem(&z, "icne", "ence", m_gt_0) ){
174937       stem(&z, "icna", "ance", m_gt_0);
174938     }
174939     break;
174940   case 'e':
174941     stem(&z, "rezi", "ize", m_gt_0);
174942     break;
174943   case 'g':
174944     stem(&z, "igol", "log", m_gt_0);
174945     break;
174946   case 'l':
174947     if( !stem(&z, "ilb", "ble", m_gt_0)
174948      && !stem(&z, "illa", "al", m_gt_0)
174949      && !stem(&z, "iltne", "ent", m_gt_0)
174950      && !stem(&z, "ile", "e", m_gt_0)
174951     ){
174952       stem(&z, "ilsuo", "ous", m_gt_0);
174953     }
174954     break;
174955   case 'o':
174956     if( !stem(&z, "noitazi", "ize", m_gt_0)
174957      && !stem(&z, "noita", "ate", m_gt_0)
174958     ){
174959       stem(&z, "rota", "ate", m_gt_0);
174960     }
174961     break;
174962   case 's':
174963     if( !stem(&z, "msila", "al", m_gt_0)
174964      && !stem(&z, "ssenevi", "ive", m_gt_0)
174965      && !stem(&z, "ssenluf", "ful", m_gt_0)
174966     ){
174967       stem(&z, "ssensuo", "ous", m_gt_0);
174968     }
174969     break;
174970   case 't':
174971     if( !stem(&z, "itila", "al", m_gt_0)
174972      && !stem(&z, "itivi", "ive", m_gt_0)
174973     ){
174974       stem(&z, "itilib", "ble", m_gt_0);
174975     }
174976     break;
174977  }
174978
174979  /* Step 3 */
174980  switch( z[0] ){
174981   case 'e':
174982     if( !stem(&z, "etaci", "ic", m_gt_0)
174983      && !stem(&z, "evita", "", m_gt_0)
174984     ){
174985       stem(&z, "ezila", "al", m_gt_0);
174986     }
174987     break;
174988   case 'i':
174989     stem(&z, "itici", "ic", m_gt_0);
174990     break;
174991   case 'l':
174992     if( !stem(&z, "laci", "ic", m_gt_0) ){
174993       stem(&z, "luf", "", m_gt_0);
174994     }
174995     break;
174996   case 's':
174997     stem(&z, "ssen", "", m_gt_0);
174998     break;
174999  }
175000
175001  /* Step 4 */
175002  switch( z[1] ){
175003   case 'a':
175004     if( z[0]=='l' && m_gt_1(z+2) ){
175005       z += 2;
175006     }
175007     break;
175008   case 'c':
175009     if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e')  && m_gt_1(z+4)  ){
175010       z += 4;
175011     }
175012     break;
175013   case 'e':
175014     if( z[0]=='r' && m_gt_1(z+2) ){
175015       z += 2;
175016     }
175017     break;
175018   case 'i':
175019     if( z[0]=='c' && m_gt_1(z+2) ){
175020       z += 2;
175021     }
175022     break;
175023   case 'l':
175024     if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
175025       z += 4;
175026     }
175027     break;
175028   case 'n':
175029     if( z[0]=='t' ){
175030       if( z[2]=='a' ){
175031         if( m_gt_1(z+3) ){
175032           z += 3;
175033         }
175034       }else if( z[2]=='e' ){
175035         if( !stem(&z, "tneme", "", m_gt_1)
175036          && !stem(&z, "tnem", "", m_gt_1)
175037         ){
175038           stem(&z, "tne", "", m_gt_1);
175039         }
175040       }
175041     }
175042     break;
175043   case 'o':
175044     if( z[0]=='u' ){
175045       if( m_gt_1(z+2) ){
175046         z += 2;
175047       }
175048     }else if( z[3]=='s' || z[3]=='t' ){
175049       stem(&z, "noi", "", m_gt_1);
175050     }
175051     break;
175052   case 's':
175053     if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
175054       z += 3;
175055     }
175056     break;
175057   case 't':
175058     if( !stem(&z, "eta", "", m_gt_1) ){
175059       stem(&z, "iti", "", m_gt_1);
175060     }
175061     break;
175062   case 'u':
175063     if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
175064       z += 3;
175065     }
175066     break;
175067   case 'v':
175068   case 'z':
175069     if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
175070       z += 3;
175071     }
175072     break;
175073  }
175074
175075  /* Step 5a */
175076  if( z[0]=='e' ){
175077    if( m_gt_1(z+1) ){
175078      z++;
175079    }else if( m_eq_1(z+1) && !star_oh(z+1) ){
175080      z++;
175081    }
175082  }
175083
175084  /* Step 5b */
175085  if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
175086    z++;
175087  }
175088
175089  /* z[] is now the stemmed word in reverse order.  Flip it back
175090  ** around into forward order and return.
175091  */
175092  *pnOut = i = (int)strlen(z);
175093  zOut[i] = 0;
175094  while( *z ){
175095    zOut[--i] = *(z++);
175096  }
175097}
175098
175099/*
175100** Characters that can be part of a token.  We assume any character
175101** whose value is greater than 0x80 (any UTF character) can be
175102** part of a token.  In other words, delimiters all must have
175103** values of 0x7f or lower.
175104*/
175105static const char porterIdChar[] = {
175106/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
175107    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
175108    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
175109    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
175110    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
175111    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
175112};
175113#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
175114
175115/*
175116** Extract the next token from a tokenization cursor.  The cursor must
175117** have been opened by a prior call to porterOpen().
175118*/
175119static int porterNext(
175120  sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by porterOpen */
175121  const char **pzToken,               /* OUT: *pzToken is the token text */
175122  int *pnBytes,                       /* OUT: Number of bytes in token */
175123  int *piStartOffset,                 /* OUT: Starting offset of token */
175124  int *piEndOffset,                   /* OUT: Ending offset of token */
175125  int *piPosition                     /* OUT: Position integer of token */
175126){
175127  porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
175128  const char *z = c->zInput;
175129
175130  while( c->iOffset<c->nInput ){
175131    int iStartOffset, ch;
175132
175133    /* Scan past delimiter characters */
175134    while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
175135      c->iOffset++;
175136    }
175137
175138    /* Count non-delimiter characters. */
175139    iStartOffset = c->iOffset;
175140    while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
175141      c->iOffset++;
175142    }
175143
175144    if( c->iOffset>iStartOffset ){
175145      int n = c->iOffset-iStartOffset;
175146      if( n>c->nAllocated ){
175147        char *pNew;
175148        c->nAllocated = n+20;
175149        pNew = sqlite3_realloc(c->zToken, c->nAllocated);
175150        if( !pNew ) return SQLITE_NOMEM;
175151        c->zToken = pNew;
175152      }
175153      porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
175154      *pzToken = c->zToken;
175155      *piStartOffset = iStartOffset;
175156      *piEndOffset = c->iOffset;
175157      *piPosition = c->iToken++;
175158      return SQLITE_OK;
175159    }
175160  }
175161  return SQLITE_DONE;
175162}
175163
175164/*
175165** The set of routines that implement the porter-stemmer tokenizer
175166*/
175167static const sqlite3_tokenizer_module porterTokenizerModule = {
175168  0,
175169  porterCreate,
175170  porterDestroy,
175171  porterOpen,
175172  porterClose,
175173  porterNext,
175174  0
175175};
175176
175177/*
175178** Allocate a new porter tokenizer.  Return a pointer to the new
175179** tokenizer in *ppModule
175180*/
175181SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
175182  sqlite3_tokenizer_module const**ppModule
175183){
175184  *ppModule = &porterTokenizerModule;
175185}
175186
175187#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
175188
175189/************** End of fts3_porter.c *****************************************/
175190/************** Begin file fts3_tokenizer.c **********************************/
175191/*
175192** 2007 June 22
175193**
175194** The author disclaims copyright to this source code.  In place of
175195** a legal notice, here is a blessing:
175196**
175197**    May you do good and not evil.
175198**    May you find forgiveness for yourself and forgive others.
175199**    May you share freely, never taking more than you give.
175200**
175201******************************************************************************
175202**
175203** This is part of an SQLite module implementing full-text search.
175204** This particular file implements the generic tokenizer interface.
175205*/
175206
175207/*
175208** The code in this file is only compiled if:
175209**
175210**     * The FTS3 module is being built as an extension
175211**       (in which case SQLITE_CORE is not defined), or
175212**
175213**     * The FTS3 module is being built into the core of
175214**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
175215*/
175216/* #include "fts3Int.h" */
175217#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
175218
175219/* #include <assert.h> */
175220/* #include <string.h> */
175221
175222/*
175223** Return true if the two-argument version of fts3_tokenizer()
175224** has been activated via a prior call to sqlite3_db_config(db,
175225** SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
175226*/
175227static int fts3TokenizerEnabled(sqlite3_context *context){
175228  sqlite3 *db = sqlite3_context_db_handle(context);
175229  int isEnabled = 0;
175230  sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER,-1,&isEnabled);
175231  return isEnabled;
175232}
175233
175234/*
175235** Implementation of the SQL scalar function for accessing the underlying
175236** hash table. This function may be called as follows:
175237**
175238**   SELECT <function-name>(<key-name>);
175239**   SELECT <function-name>(<key-name>, <pointer>);
175240**
175241** where <function-name> is the name passed as the second argument
175242** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
175243**
175244** If the <pointer> argument is specified, it must be a blob value
175245** containing a pointer to be stored as the hash data corresponding
175246** to the string <key-name>. If <pointer> is not specified, then
175247** the string <key-name> must already exist in the has table. Otherwise,
175248** an error is returned.
175249**
175250** Whether or not the <pointer> argument is specified, the value returned
175251** is a blob containing the pointer stored as the hash data corresponding
175252** to string <key-name> (after the hash-table is updated, if applicable).
175253*/
175254static void fts3TokenizerFunc(
175255  sqlite3_context *context,
175256  int argc,
175257  sqlite3_value **argv
175258){
175259  Fts3Hash *pHash;
175260  void *pPtr = 0;
175261  const unsigned char *zName;
175262  int nName;
175263
175264  assert( argc==1 || argc==2 );
175265
175266  pHash = (Fts3Hash *)sqlite3_user_data(context);
175267
175268  zName = sqlite3_value_text(argv[0]);
175269  nName = sqlite3_value_bytes(argv[0])+1;
175270
175271  if( argc==2 ){
175272    if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[1]) ){
175273      void *pOld;
175274      int n = sqlite3_value_bytes(argv[1]);
175275      if( zName==0 || n!=sizeof(pPtr) ){
175276        sqlite3_result_error(context, "argument type mismatch", -1);
175277        return;
175278      }
175279      pPtr = *(void **)sqlite3_value_blob(argv[1]);
175280      pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
175281      if( pOld==pPtr ){
175282        sqlite3_result_error(context, "out of memory", -1);
175283      }
175284    }else{
175285      sqlite3_result_error(context, "fts3tokenize disabled", -1);
175286      return;
175287    }
175288  }else{
175289    if( zName ){
175290      pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
175291    }
175292    if( !pPtr ){
175293      char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
175294      sqlite3_result_error(context, zErr, -1);
175295      sqlite3_free(zErr);
175296      return;
175297    }
175298  }
175299  if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[0]) ){
175300    sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
175301  }
175302}
175303
175304SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
175305  static const char isFtsIdChar[] = {
175306      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x */
175307      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 1x */
175308      0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
175309      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
175310      0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
175311      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
175312      0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
175313      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
175314  };
175315  return (c&0x80 || isFtsIdChar[(int)(c)]);
175316}
175317
175318SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
175319  const char *z1;
175320  const char *z2 = 0;
175321
175322  /* Find the start of the next token. */
175323  z1 = zStr;
175324  while( z2==0 ){
175325    char c = *z1;
175326    switch( c ){
175327      case '\0': return 0;        /* No more tokens here */
175328      case '\'':
175329      case '"':
175330      case '`': {
175331        z2 = z1;
175332        while( *++z2 && (*z2!=c || *++z2==c) );
175333        break;
175334      }
175335      case '[':
175336        z2 = &z1[1];
175337        while( *z2 && z2[0]!=']' ) z2++;
175338        if( *z2 ) z2++;
175339        break;
175340
175341      default:
175342        if( sqlite3Fts3IsIdChar(*z1) ){
175343          z2 = &z1[1];
175344          while( sqlite3Fts3IsIdChar(*z2) ) z2++;
175345        }else{
175346          z1++;
175347        }
175348    }
175349  }
175350
175351  *pn = (int)(z2-z1);
175352  return z1;
175353}
175354
175355SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
175356  Fts3Hash *pHash,                /* Tokenizer hash table */
175357  const char *zArg,               /* Tokenizer name */
175358  sqlite3_tokenizer **ppTok,      /* OUT: Tokenizer (if applicable) */
175359  char **pzErr                    /* OUT: Set to malloced error message */
175360){
175361  int rc;
175362  char *z = (char *)zArg;
175363  int n = 0;
175364  char *zCopy;
175365  char *zEnd;                     /* Pointer to nul-term of zCopy */
175366  sqlite3_tokenizer_module *m;
175367
175368  zCopy = sqlite3_mprintf("%s", zArg);
175369  if( !zCopy ) return SQLITE_NOMEM;
175370  zEnd = &zCopy[strlen(zCopy)];
175371
175372  z = (char *)sqlite3Fts3NextToken(zCopy, &n);
175373  if( z==0 ){
175374    assert( n==0 );
175375    z = zCopy;
175376  }
175377  z[n] = '\0';
175378  sqlite3Fts3Dequote(z);
175379
175380  m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
175381  if( !m ){
175382    sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", z);
175383    rc = SQLITE_ERROR;
175384  }else{
175385    char const **aArg = 0;
175386    int iArg = 0;
175387    z = &z[n+1];
175388    while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
175389      sqlite3_int64 nNew = sizeof(char *)*(iArg+1);
175390      char const **aNew = (const char **)sqlite3_realloc64((void *)aArg, nNew);
175391      if( !aNew ){
175392        sqlite3_free(zCopy);
175393        sqlite3_free((void *)aArg);
175394        return SQLITE_NOMEM;
175395      }
175396      aArg = aNew;
175397      aArg[iArg++] = z;
175398      z[n] = '\0';
175399      sqlite3Fts3Dequote(z);
175400      z = &z[n+1];
175401    }
175402    rc = m->xCreate(iArg, aArg, ppTok);
175403    assert( rc!=SQLITE_OK || *ppTok );
175404    if( rc!=SQLITE_OK ){
175405      sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer");
175406    }else{
175407      (*ppTok)->pModule = m;
175408    }
175409    sqlite3_free((void *)aArg);
175410  }
175411
175412  sqlite3_free(zCopy);
175413  return rc;
175414}
175415
175416
175417#ifdef SQLITE_TEST
175418
175419#if defined(INCLUDE_SQLITE_TCL_H)
175420#  include "sqlite_tcl.h"
175421#else
175422#  include "tcl.h"
175423#endif
175424/* #include <string.h> */
175425
175426/*
175427** Implementation of a special SQL scalar function for testing tokenizers
175428** designed to be used in concert with the Tcl testing framework. This
175429** function must be called with two or more arguments:
175430**
175431**   SELECT <function-name>(<key-name>, ..., <input-string>);
175432**
175433** where <function-name> is the name passed as the second argument
175434** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
175435** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
175436**
175437** The return value is a string that may be interpreted as a Tcl
175438** list. For each token in the <input-string>, three elements are
175439** added to the returned list. The first is the token position, the
175440** second is the token text (folded, stemmed, etc.) and the third is the
175441** substring of <input-string> associated with the token. For example,
175442** using the built-in "simple" tokenizer:
175443**
175444**   SELECT fts_tokenizer_test('simple', 'I don't see how');
175445**
175446** will return the string:
175447**
175448**   "{0 i I 1 dont don't 2 see see 3 how how}"
175449**
175450*/
175451static void testFunc(
175452  sqlite3_context *context,
175453  int argc,
175454  sqlite3_value **argv
175455){
175456  Fts3Hash *pHash;
175457  sqlite3_tokenizer_module *p;
175458  sqlite3_tokenizer *pTokenizer = 0;
175459  sqlite3_tokenizer_cursor *pCsr = 0;
175460
175461  const char *zErr = 0;
175462
175463  const char *zName;
175464  int nName;
175465  const char *zInput;
175466  int nInput;
175467
175468  const char *azArg[64];
175469
175470  const char *zToken;
175471  int nToken = 0;
175472  int iStart = 0;
175473  int iEnd = 0;
175474  int iPos = 0;
175475  int i;
175476
175477  Tcl_Obj *pRet;
175478
175479  if( argc<2 ){
175480    sqlite3_result_error(context, "insufficient arguments", -1);
175481    return;
175482  }
175483
175484  nName = sqlite3_value_bytes(argv[0]);
175485  zName = (const char *)sqlite3_value_text(argv[0]);
175486  nInput = sqlite3_value_bytes(argv[argc-1]);
175487  zInput = (const char *)sqlite3_value_text(argv[argc-1]);
175488
175489  pHash = (Fts3Hash *)sqlite3_user_data(context);
175490  p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
175491
175492  if( !p ){
175493    char *zErr2 = sqlite3_mprintf("unknown tokenizer: %s", zName);
175494    sqlite3_result_error(context, zErr2, -1);
175495    sqlite3_free(zErr2);
175496    return;
175497  }
175498
175499  pRet = Tcl_NewObj();
175500  Tcl_IncrRefCount(pRet);
175501
175502  for(i=1; i<argc-1; i++){
175503    azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
175504  }
175505
175506  if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
175507    zErr = "error in xCreate()";
175508    goto finish;
175509  }
175510  pTokenizer->pModule = p;
175511  if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
175512    zErr = "error in xOpen()";
175513    goto finish;
175514  }
175515
175516  while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
175517    Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
175518    Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
175519    zToken = &zInput[iStart];
175520    nToken = iEnd-iStart;
175521    Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
175522  }
175523
175524  if( SQLITE_OK!=p->xClose(pCsr) ){
175525    zErr = "error in xClose()";
175526    goto finish;
175527  }
175528  if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
175529    zErr = "error in xDestroy()";
175530    goto finish;
175531  }
175532
175533finish:
175534  if( zErr ){
175535    sqlite3_result_error(context, zErr, -1);
175536  }else{
175537    sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
175538  }
175539  Tcl_DecrRefCount(pRet);
175540}
175541
175542static
175543int registerTokenizer(
175544  sqlite3 *db,
175545  char *zName,
175546  const sqlite3_tokenizer_module *p
175547){
175548  int rc;
175549  sqlite3_stmt *pStmt;
175550  const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
175551
175552  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
175553  if( rc!=SQLITE_OK ){
175554    return rc;
175555  }
175556
175557  sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
175558  sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
175559  sqlite3_step(pStmt);
175560
175561  return sqlite3_finalize(pStmt);
175562}
175563
175564
175565static
175566int queryTokenizer(
175567  sqlite3 *db,
175568  char *zName,
175569  const sqlite3_tokenizer_module **pp
175570){
175571  int rc;
175572  sqlite3_stmt *pStmt;
175573  const char zSql[] = "SELECT fts3_tokenizer(?)";
175574
175575  *pp = 0;
175576  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
175577  if( rc!=SQLITE_OK ){
175578    return rc;
175579  }
175580
175581  sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
175582  if( SQLITE_ROW==sqlite3_step(pStmt) ){
175583    if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB
175584     && sqlite3_column_bytes(pStmt, 0)==sizeof(*pp)
175585    ){
175586      memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
175587    }
175588  }
175589
175590  return sqlite3_finalize(pStmt);
175591}
175592
175593SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
175594
175595/*
175596** Implementation of the scalar function fts3_tokenizer_internal_test().
175597** This function is used for testing only, it is not included in the
175598** build unless SQLITE_TEST is defined.
175599**
175600** The purpose of this is to test that the fts3_tokenizer() function
175601** can be used as designed by the C-code in the queryTokenizer and
175602** registerTokenizer() functions above. These two functions are repeated
175603** in the README.tokenizer file as an example, so it is important to
175604** test them.
175605**
175606** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
175607** function with no arguments. An assert() will fail if a problem is
175608** detected. i.e.:
175609**
175610**     SELECT fts3_tokenizer_internal_test();
175611**
175612*/
175613static void intTestFunc(
175614  sqlite3_context *context,
175615  int argc,
175616  sqlite3_value **argv
175617){
175618  int rc;
175619  const sqlite3_tokenizer_module *p1;
175620  const sqlite3_tokenizer_module *p2;
175621  sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
175622
175623  UNUSED_PARAMETER(argc);
175624  UNUSED_PARAMETER(argv);
175625
175626  /* Test the query function */
175627  sqlite3Fts3SimpleTokenizerModule(&p1);
175628  rc = queryTokenizer(db, "simple", &p2);
175629  assert( rc==SQLITE_OK );
175630  assert( p1==p2 );
175631  rc = queryTokenizer(db, "nosuchtokenizer", &p2);
175632  assert( rc==SQLITE_ERROR );
175633  assert( p2==0 );
175634  assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
175635
175636  /* Test the storage function */
175637  if( fts3TokenizerEnabled(context) ){
175638    rc = registerTokenizer(db, "nosuchtokenizer", p1);
175639    assert( rc==SQLITE_OK );
175640    rc = queryTokenizer(db, "nosuchtokenizer", &p2);
175641    assert( rc==SQLITE_OK );
175642    assert( p2==p1 );
175643  }
175644
175645  sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
175646}
175647
175648#endif
175649
175650/*
175651** Set up SQL objects in database db used to access the contents of
175652** the hash table pointed to by argument pHash. The hash table must
175653** been initialized to use string keys, and to take a private copy
175654** of the key when a value is inserted. i.e. by a call similar to:
175655**
175656**    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
175657**
175658** This function adds a scalar function (see header comment above
175659** fts3TokenizerFunc() in this file for details) and, if ENABLE_TABLE is
175660** defined at compilation time, a temporary virtual table (see header
175661** comment above struct HashTableVtab) to the database schema. Both
175662** provide read/write access to the contents of *pHash.
175663**
175664** The third argument to this function, zName, is used as the name
175665** of both the scalar and, if created, the virtual table.
175666*/
175667SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
175668  sqlite3 *db,
175669  Fts3Hash *pHash,
175670  const char *zName
175671){
175672  int rc = SQLITE_OK;
175673  void *p = (void *)pHash;
175674  const int any = SQLITE_UTF8|SQLITE_DIRECTONLY;
175675
175676#ifdef SQLITE_TEST
175677  char *zTest = 0;
175678  char *zTest2 = 0;
175679  void *pdb = (void *)db;
175680  zTest = sqlite3_mprintf("%s_test", zName);
175681  zTest2 = sqlite3_mprintf("%s_internal_test", zName);
175682  if( !zTest || !zTest2 ){
175683    rc = SQLITE_NOMEM;
175684  }
175685#endif
175686
175687  if( SQLITE_OK==rc ){
175688    rc = sqlite3_create_function(db, zName, 1, any, p, fts3TokenizerFunc, 0, 0);
175689  }
175690  if( SQLITE_OK==rc ){
175691    rc = sqlite3_create_function(db, zName, 2, any, p, fts3TokenizerFunc, 0, 0);
175692  }
175693#ifdef SQLITE_TEST
175694  if( SQLITE_OK==rc ){
175695    rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
175696  }
175697  if( SQLITE_OK==rc ){
175698    rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
175699  }
175700#endif
175701
175702#ifdef SQLITE_TEST
175703  sqlite3_free(zTest);
175704  sqlite3_free(zTest2);
175705#endif
175706
175707  return rc;
175708}
175709
175710#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
175711
175712/************** End of fts3_tokenizer.c **************************************/
175713/************** Begin file fts3_tokenizer1.c *********************************/
175714/*
175715** 2006 Oct 10
175716**
175717** The author disclaims copyright to this source code.  In place of
175718** a legal notice, here is a blessing:
175719**
175720**    May you do good and not evil.
175721**    May you find forgiveness for yourself and forgive others.
175722**    May you share freely, never taking more than you give.
175723**
175724******************************************************************************
175725**
175726** Implementation of the "simple" full-text-search tokenizer.
175727*/
175728
175729/*
175730** The code in this file is only compiled if:
175731**
175732**     * The FTS3 module is being built as an extension
175733**       (in which case SQLITE_CORE is not defined), or
175734**
175735**     * The FTS3 module is being built into the core of
175736**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
175737*/
175738/* #include "fts3Int.h" */
175739#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
175740
175741/* #include <assert.h> */
175742/* #include <stdlib.h> */
175743/* #include <stdio.h> */
175744/* #include <string.h> */
175745
175746/* #include "fts3_tokenizer.h" */
175747
175748typedef struct simple_tokenizer {
175749  sqlite3_tokenizer base;
175750  char delim[128];             /* flag ASCII delimiters */
175751} simple_tokenizer;
175752
175753typedef struct simple_tokenizer_cursor {
175754  sqlite3_tokenizer_cursor base;
175755  const char *pInput;          /* input we are tokenizing */
175756  int nBytes;                  /* size of the input */
175757  int iOffset;                 /* current position in pInput */
175758  int iToken;                  /* index of next token to be returned */
175759  char *pToken;                /* storage for current token */
175760  int nTokenAllocated;         /* space allocated to zToken buffer */
175761} simple_tokenizer_cursor;
175762
175763
175764static int simpleDelim(simple_tokenizer *t, unsigned char c){
175765  return c<0x80 && t->delim[c];
175766}
175767static int fts3_isalnum(int x){
175768  return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
175769}
175770
175771/*
175772** Create a new tokenizer instance.
175773*/
175774static int simpleCreate(
175775  int argc, const char * const *argv,
175776  sqlite3_tokenizer **ppTokenizer
175777){
175778  simple_tokenizer *t;
175779
175780  t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
175781  if( t==NULL ) return SQLITE_NOMEM;
175782  memset(t, 0, sizeof(*t));
175783
175784  /* TODO(shess) Delimiters need to remain the same from run to run,
175785  ** else we need to reindex.  One solution would be a meta-table to
175786  ** track such information in the database, then we'd only want this
175787  ** information on the initial create.
175788  */
175789  if( argc>1 ){
175790    int i, n = (int)strlen(argv[1]);
175791    for(i=0; i<n; i++){
175792      unsigned char ch = argv[1][i];
175793      /* We explicitly don't support UTF-8 delimiters for now. */
175794      if( ch>=0x80 ){
175795        sqlite3_free(t);
175796        return SQLITE_ERROR;
175797      }
175798      t->delim[ch] = 1;
175799    }
175800  } else {
175801    /* Mark non-alphanumeric ASCII characters as delimiters */
175802    int i;
175803    for(i=1; i<0x80; i++){
175804      t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
175805    }
175806  }
175807
175808  *ppTokenizer = &t->base;
175809  return SQLITE_OK;
175810}
175811
175812/*
175813** Destroy a tokenizer
175814*/
175815static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
175816  sqlite3_free(pTokenizer);
175817  return SQLITE_OK;
175818}
175819
175820/*
175821** Prepare to begin tokenizing a particular string.  The input
175822** string to be tokenized is pInput[0..nBytes-1].  A cursor
175823** used to incrementally tokenize this string is returned in
175824** *ppCursor.
175825*/
175826static int simpleOpen(
175827  sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
175828  const char *pInput, int nBytes,        /* String to be tokenized */
175829  sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
175830){
175831  simple_tokenizer_cursor *c;
175832
175833  UNUSED_PARAMETER(pTokenizer);
175834
175835  c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
175836  if( c==NULL ) return SQLITE_NOMEM;
175837
175838  c->pInput = pInput;
175839  if( pInput==0 ){
175840    c->nBytes = 0;
175841  }else if( nBytes<0 ){
175842    c->nBytes = (int)strlen(pInput);
175843  }else{
175844    c->nBytes = nBytes;
175845  }
175846  c->iOffset = 0;                 /* start tokenizing at the beginning */
175847  c->iToken = 0;
175848  c->pToken = NULL;               /* no space allocated, yet. */
175849  c->nTokenAllocated = 0;
175850
175851  *ppCursor = &c->base;
175852  return SQLITE_OK;
175853}
175854
175855/*
175856** Close a tokenization cursor previously opened by a call to
175857** simpleOpen() above.
175858*/
175859static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
175860  simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
175861  sqlite3_free(c->pToken);
175862  sqlite3_free(c);
175863  return SQLITE_OK;
175864}
175865
175866/*
175867** Extract the next token from a tokenization cursor.  The cursor must
175868** have been opened by a prior call to simpleOpen().
175869*/
175870static int simpleNext(
175871  sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
175872  const char **ppToken,               /* OUT: *ppToken is the token text */
175873  int *pnBytes,                       /* OUT: Number of bytes in token */
175874  int *piStartOffset,                 /* OUT: Starting offset of token */
175875  int *piEndOffset,                   /* OUT: Ending offset of token */
175876  int *piPosition                     /* OUT: Position integer of token */
175877){
175878  simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
175879  simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
175880  unsigned char *p = (unsigned char *)c->pInput;
175881
175882  while( c->iOffset<c->nBytes ){
175883    int iStartOffset;
175884
175885    /* Scan past delimiter characters */
175886    while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
175887      c->iOffset++;
175888    }
175889
175890    /* Count non-delimiter characters. */
175891    iStartOffset = c->iOffset;
175892    while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
175893      c->iOffset++;
175894    }
175895
175896    if( c->iOffset>iStartOffset ){
175897      int i, n = c->iOffset-iStartOffset;
175898      if( n>c->nTokenAllocated ){
175899        char *pNew;
175900        c->nTokenAllocated = n+20;
175901        pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
175902        if( !pNew ) return SQLITE_NOMEM;
175903        c->pToken = pNew;
175904      }
175905      for(i=0; i<n; i++){
175906        /* TODO(shess) This needs expansion to handle UTF-8
175907        ** case-insensitivity.
175908        */
175909        unsigned char ch = p[iStartOffset+i];
175910        c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
175911      }
175912      *ppToken = c->pToken;
175913      *pnBytes = n;
175914      *piStartOffset = iStartOffset;
175915      *piEndOffset = c->iOffset;
175916      *piPosition = c->iToken++;
175917
175918      return SQLITE_OK;
175919    }
175920  }
175921  return SQLITE_DONE;
175922}
175923
175924/*
175925** The set of routines that implement the simple tokenizer
175926*/
175927static const sqlite3_tokenizer_module simpleTokenizerModule = {
175928  0,
175929  simpleCreate,
175930  simpleDestroy,
175931  simpleOpen,
175932  simpleClose,
175933  simpleNext,
175934  0,
175935};
175936
175937/*
175938** Allocate a new simple tokenizer.  Return a pointer to the new
175939** tokenizer in *ppModule
175940*/
175941SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
175942  sqlite3_tokenizer_module const**ppModule
175943){
175944  *ppModule = &simpleTokenizerModule;
175945}
175946
175947#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
175948
175949/************** End of fts3_tokenizer1.c *************************************/
175950/************** Begin file fts3_tokenize_vtab.c ******************************/
175951/*
175952** 2013 Apr 22
175953**
175954** The author disclaims copyright to this source code.  In place of
175955** a legal notice, here is a blessing:
175956**
175957**    May you do good and not evil.
175958**    May you find forgiveness for yourself and forgive others.
175959**    May you share freely, never taking more than you give.
175960**
175961******************************************************************************
175962**
175963** This file contains code for the "fts3tokenize" virtual table module.
175964** An fts3tokenize virtual table is created as follows:
175965**
175966**   CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
175967**       <tokenizer-name>, <arg-1>, ...
175968**   );
175969**
175970** The table created has the following schema:
175971**
175972**   CREATE TABLE <tbl>(input, token, start, end, position)
175973**
175974** When queried, the query must include a WHERE clause of type:
175975**
175976**   input = <string>
175977**
175978** The virtual table module tokenizes this <string>, using the FTS3
175979** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
175980** statement and returns one row for each token in the result. With
175981** fields set as follows:
175982**
175983**   input:   Always set to a copy of <string>
175984**   token:   A token from the input.
175985**   start:   Byte offset of the token within the input <string>.
175986**   end:     Byte offset of the byte immediately following the end of the
175987**            token within the input string.
175988**   pos:     Token offset of token within input.
175989**
175990*/
175991/* #include "fts3Int.h" */
175992#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
175993
175994/* #include <string.h> */
175995/* #include <assert.h> */
175996
175997typedef struct Fts3tokTable Fts3tokTable;
175998typedef struct Fts3tokCursor Fts3tokCursor;
175999
176000/*
176001** Virtual table structure.
176002*/
176003struct Fts3tokTable {
176004  sqlite3_vtab base;              /* Base class used by SQLite core */
176005  const sqlite3_tokenizer_module *pMod;
176006  sqlite3_tokenizer *pTok;
176007};
176008
176009/*
176010** Virtual table cursor structure.
176011*/
176012struct Fts3tokCursor {
176013  sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
176014  char *zInput;                   /* Input string */
176015  sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
176016  int iRowid;                     /* Current 'rowid' value */
176017  const char *zToken;             /* Current 'token' value */
176018  int nToken;                     /* Size of zToken in bytes */
176019  int iStart;                     /* Current 'start' value */
176020  int iEnd;                       /* Current 'end' value */
176021  int iPos;                       /* Current 'pos' value */
176022};
176023
176024/*
176025** Query FTS for the tokenizer implementation named zName.
176026*/
176027static int fts3tokQueryTokenizer(
176028  Fts3Hash *pHash,
176029  const char *zName,
176030  const sqlite3_tokenizer_module **pp,
176031  char **pzErr
176032){
176033  sqlite3_tokenizer_module *p;
176034  int nName = (int)strlen(zName);
176035
176036  p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
176037  if( !p ){
176038    sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", zName);
176039    return SQLITE_ERROR;
176040  }
176041
176042  *pp = p;
176043  return SQLITE_OK;
176044}
176045
176046/*
176047** The second argument, argv[], is an array of pointers to nul-terminated
176048** strings. This function makes a copy of the array and strings into a
176049** single block of memory. It then dequotes any of the strings that appear
176050** to be quoted.
176051**
176052** If successful, output parameter *pazDequote is set to point at the
176053** array of dequoted strings and SQLITE_OK is returned. The caller is
176054** responsible for eventually calling sqlite3_free() to free the array
176055** in this case. Or, if an error occurs, an SQLite error code is returned.
176056** The final value of *pazDequote is undefined in this case.
176057*/
176058static int fts3tokDequoteArray(
176059  int argc,                       /* Number of elements in argv[] */
176060  const char * const *argv,       /* Input array */
176061  char ***pazDequote              /* Output array */
176062){
176063  int rc = SQLITE_OK;             /* Return code */
176064  if( argc==0 ){
176065    *pazDequote = 0;
176066  }else{
176067    int i;
176068    int nByte = 0;
176069    char **azDequote;
176070
176071    for(i=0; i<argc; i++){
176072      nByte += (int)(strlen(argv[i]) + 1);
176073    }
176074
176075    *pazDequote = azDequote = sqlite3_malloc64(sizeof(char *)*argc + nByte);
176076    if( azDequote==0 ){
176077      rc = SQLITE_NOMEM;
176078    }else{
176079      char *pSpace = (char *)&azDequote[argc];
176080      for(i=0; i<argc; i++){
176081        int n = (int)strlen(argv[i]);
176082        azDequote[i] = pSpace;
176083        memcpy(pSpace, argv[i], n+1);
176084        sqlite3Fts3Dequote(pSpace);
176085        pSpace += (n+1);
176086      }
176087    }
176088  }
176089
176090  return rc;
176091}
176092
176093/*
176094** Schema of the tokenizer table.
176095*/
176096#define FTS3_TOK_SCHEMA "CREATE TABLE x(input, token, start, end, position)"
176097
176098/*
176099** This function does all the work for both the xConnect and xCreate methods.
176100** These tables have no persistent representation of their own, so xConnect
176101** and xCreate are identical operations.
176102**
176103**   argv[0]: module name
176104**   argv[1]: database name
176105**   argv[2]: table name
176106**   argv[3]: first argument (tokenizer name)
176107*/
176108static int fts3tokConnectMethod(
176109  sqlite3 *db,                    /* Database connection */
176110  void *pHash,                    /* Hash table of tokenizers */
176111  int argc,                       /* Number of elements in argv array */
176112  const char * const *argv,       /* xCreate/xConnect argument array */
176113  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
176114  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
176115){
176116  Fts3tokTable *pTab = 0;
176117  const sqlite3_tokenizer_module *pMod = 0;
176118  sqlite3_tokenizer *pTok = 0;
176119  int rc;
176120  char **azDequote = 0;
176121  int nDequote;
176122
176123  rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA);
176124  if( rc!=SQLITE_OK ) return rc;
176125
176126  nDequote = argc-3;
176127  rc = fts3tokDequoteArray(nDequote, &argv[3], &azDequote);
176128
176129  if( rc==SQLITE_OK ){
176130    const char *zModule;
176131    if( nDequote<1 ){
176132      zModule = "simple";
176133    }else{
176134      zModule = azDequote[0];
176135    }
176136    rc = fts3tokQueryTokenizer((Fts3Hash*)pHash, zModule, &pMod, pzErr);
176137  }
176138
176139  assert( (rc==SQLITE_OK)==(pMod!=0) );
176140  if( rc==SQLITE_OK ){
176141    const char * const *azArg = (const char * const *)&azDequote[1];
176142    rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
176143  }
176144
176145  if( rc==SQLITE_OK ){
176146    pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable));
176147    if( pTab==0 ){
176148      rc = SQLITE_NOMEM;
176149    }
176150  }
176151
176152  if( rc==SQLITE_OK ){
176153    memset(pTab, 0, sizeof(Fts3tokTable));
176154    pTab->pMod = pMod;
176155    pTab->pTok = pTok;
176156    *ppVtab = &pTab->base;
176157  }else{
176158    if( pTok ){
176159      pMod->xDestroy(pTok);
176160    }
176161  }
176162
176163  sqlite3_free(azDequote);
176164  return rc;
176165}
176166
176167/*
176168** This function does the work for both the xDisconnect and xDestroy methods.
176169** These tables have no persistent representation of their own, so xDisconnect
176170** and xDestroy are identical operations.
176171*/
176172static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
176173  Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
176174
176175  pTab->pMod->xDestroy(pTab->pTok);
176176  sqlite3_free(pTab);
176177  return SQLITE_OK;
176178}
176179
176180/*
176181** xBestIndex - Analyze a WHERE and ORDER BY clause.
176182*/
176183static int fts3tokBestIndexMethod(
176184  sqlite3_vtab *pVTab,
176185  sqlite3_index_info *pInfo
176186){
176187  int i;
176188  UNUSED_PARAMETER(pVTab);
176189
176190  for(i=0; i<pInfo->nConstraint; i++){
176191    if( pInfo->aConstraint[i].usable
176192     && pInfo->aConstraint[i].iColumn==0
176193     && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ
176194    ){
176195      pInfo->idxNum = 1;
176196      pInfo->aConstraintUsage[i].argvIndex = 1;
176197      pInfo->aConstraintUsage[i].omit = 1;
176198      pInfo->estimatedCost = 1;
176199      return SQLITE_OK;
176200    }
176201  }
176202
176203  pInfo->idxNum = 0;
176204  assert( pInfo->estimatedCost>1000000.0 );
176205
176206  return SQLITE_OK;
176207}
176208
176209/*
176210** xOpen - Open a cursor.
176211*/
176212static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
176213  Fts3tokCursor *pCsr;
176214  UNUSED_PARAMETER(pVTab);
176215
176216  pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor));
176217  if( pCsr==0 ){
176218    return SQLITE_NOMEM;
176219  }
176220  memset(pCsr, 0, sizeof(Fts3tokCursor));
176221
176222  *ppCsr = (sqlite3_vtab_cursor *)pCsr;
176223  return SQLITE_OK;
176224}
176225
176226/*
176227** Reset the tokenizer cursor passed as the only argument. As if it had
176228** just been returned by fts3tokOpenMethod().
176229*/
176230static void fts3tokResetCursor(Fts3tokCursor *pCsr){
176231  if( pCsr->pCsr ){
176232    Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
176233    pTab->pMod->xClose(pCsr->pCsr);
176234    pCsr->pCsr = 0;
176235  }
176236  sqlite3_free(pCsr->zInput);
176237  pCsr->zInput = 0;
176238  pCsr->zToken = 0;
176239  pCsr->nToken = 0;
176240  pCsr->iStart = 0;
176241  pCsr->iEnd = 0;
176242  pCsr->iPos = 0;
176243  pCsr->iRowid = 0;
176244}
176245
176246/*
176247** xClose - Close a cursor.
176248*/
176249static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
176250  Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
176251
176252  fts3tokResetCursor(pCsr);
176253  sqlite3_free(pCsr);
176254  return SQLITE_OK;
176255}
176256
176257/*
176258** xNext - Advance the cursor to the next row, if any.
176259*/
176260static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
176261  Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
176262  Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
176263  int rc;                         /* Return code */
176264
176265  pCsr->iRowid++;
176266  rc = pTab->pMod->xNext(pCsr->pCsr,
176267      &pCsr->zToken, &pCsr->nToken,
176268      &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
176269  );
176270
176271  if( rc!=SQLITE_OK ){
176272    fts3tokResetCursor(pCsr);
176273    if( rc==SQLITE_DONE ) rc = SQLITE_OK;
176274  }
176275
176276  return rc;
176277}
176278
176279/*
176280** xFilter - Initialize a cursor to point at the start of its data.
176281*/
176282static int fts3tokFilterMethod(
176283  sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
176284  int idxNum,                     /* Strategy index */
176285  const char *idxStr,             /* Unused */
176286  int nVal,                       /* Number of elements in apVal */
176287  sqlite3_value **apVal           /* Arguments for the indexing scheme */
176288){
176289  int rc = SQLITE_ERROR;
176290  Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
176291  Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
176292  UNUSED_PARAMETER(idxStr);
176293  UNUSED_PARAMETER(nVal);
176294
176295  fts3tokResetCursor(pCsr);
176296  if( idxNum==1 ){
176297    const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
176298    int nByte = sqlite3_value_bytes(apVal[0]);
176299    pCsr->zInput = sqlite3_malloc64(nByte+1);
176300    if( pCsr->zInput==0 ){
176301      rc = SQLITE_NOMEM;
176302    }else{
176303      if( nByte>0 ) memcpy(pCsr->zInput, zByte, nByte);
176304      pCsr->zInput[nByte] = 0;
176305      rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
176306      if( rc==SQLITE_OK ){
176307        pCsr->pCsr->pTokenizer = pTab->pTok;
176308      }
176309    }
176310  }
176311
176312  if( rc!=SQLITE_OK ) return rc;
176313  return fts3tokNextMethod(pCursor);
176314}
176315
176316/*
176317** xEof - Return true if the cursor is at EOF, or false otherwise.
176318*/
176319static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
176320  Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
176321  return (pCsr->zToken==0);
176322}
176323
176324/*
176325** xColumn - Return a column value.
176326*/
176327static int fts3tokColumnMethod(
176328  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
176329  sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
176330  int iCol                        /* Index of column to read value from */
176331){
176332  Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
176333
176334  /* CREATE TABLE x(input, token, start, end, position) */
176335  switch( iCol ){
176336    case 0:
176337      sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT);
176338      break;
176339    case 1:
176340      sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT);
176341      break;
176342    case 2:
176343      sqlite3_result_int(pCtx, pCsr->iStart);
176344      break;
176345    case 3:
176346      sqlite3_result_int(pCtx, pCsr->iEnd);
176347      break;
176348    default:
176349      assert( iCol==4 );
176350      sqlite3_result_int(pCtx, pCsr->iPos);
176351      break;
176352  }
176353  return SQLITE_OK;
176354}
176355
176356/*
176357** xRowid - Return the current rowid for the cursor.
176358*/
176359static int fts3tokRowidMethod(
176360  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
176361  sqlite_int64 *pRowid            /* OUT: Rowid value */
176362){
176363  Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
176364  *pRowid = (sqlite3_int64)pCsr->iRowid;
176365  return SQLITE_OK;
176366}
176367
176368/*
176369** Register the fts3tok module with database connection db. Return SQLITE_OK
176370** if successful or an error code if sqlite3_create_module() fails.
176371*/
176372SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){
176373  static const sqlite3_module fts3tok_module = {
176374     0,                           /* iVersion      */
176375     fts3tokConnectMethod,        /* xCreate       */
176376     fts3tokConnectMethod,        /* xConnect      */
176377     fts3tokBestIndexMethod,      /* xBestIndex    */
176378     fts3tokDisconnectMethod,     /* xDisconnect   */
176379     fts3tokDisconnectMethod,     /* xDestroy      */
176380     fts3tokOpenMethod,           /* xOpen         */
176381     fts3tokCloseMethod,          /* xClose        */
176382     fts3tokFilterMethod,         /* xFilter       */
176383     fts3tokNextMethod,           /* xNext         */
176384     fts3tokEofMethod,            /* xEof          */
176385     fts3tokColumnMethod,         /* xColumn       */
176386     fts3tokRowidMethod,          /* xRowid        */
176387     0,                           /* xUpdate       */
176388     0,                           /* xBegin        */
176389     0,                           /* xSync         */
176390     0,                           /* xCommit       */
176391     0,                           /* xRollback     */
176392     0,                           /* xFindFunction */
176393     0,                           /* xRename       */
176394     0,                           /* xSavepoint    */
176395     0,                           /* xRelease      */
176396     0,                           /* xRollbackTo   */
176397     0                            /* xShadowName   */
176398  };
176399  int rc;                         /* Return code */
176400
176401  rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash);
176402  return rc;
176403}
176404
176405#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
176406
176407/************** End of fts3_tokenize_vtab.c **********************************/
176408/************** Begin file fts3_write.c **************************************/
176409/*
176410** 2009 Oct 23
176411**
176412** The author disclaims copyright to this source code.  In place of
176413** a legal notice, here is a blessing:
176414**
176415**    May you do good and not evil.
176416**    May you find forgiveness for yourself and forgive others.
176417**    May you share freely, never taking more than you give.
176418**
176419******************************************************************************
176420**
176421** This file is part of the SQLite FTS3 extension module. Specifically,
176422** this file contains code to insert, update and delete rows from FTS3
176423** tables. It also contains code to merge FTS3 b-tree segments. Some
176424** of the sub-routines used to merge segments are also used by the query
176425** code in fts3.c.
176426*/
176427
176428/* #include "fts3Int.h" */
176429#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
176430
176431/* #include <string.h> */
176432/* #include <assert.h> */
176433/* #include <stdlib.h> */
176434/* #include <stdio.h> */
176435
176436#define FTS_MAX_APPENDABLE_HEIGHT 16
176437
176438/*
176439** When full-text index nodes are loaded from disk, the buffer that they
176440** are loaded into has the following number of bytes of padding at the end
176441** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
176442** of 920 bytes is allocated for it.
176443**
176444** This means that if we have a pointer into a buffer containing node data,
176445** it is always safe to read up to two varints from it without risking an
176446** overread, even if the node data is corrupted.
176447*/
176448#define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
176449
176450/*
176451** Under certain circumstances, b-tree nodes (doclists) can be loaded into
176452** memory incrementally instead of all at once. This can be a big performance
176453** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
176454** method before retrieving all query results (as may happen, for example,
176455** if a query has a LIMIT clause).
176456**
176457** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
176458** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
176459** The code is written so that the hard lower-limit for each of these values
176460** is 1. Clearly such small values would be inefficient, but can be useful
176461** for testing purposes.
176462**
176463** If this module is built with SQLITE_TEST defined, these constants may
176464** be overridden at runtime for testing purposes. File fts3_test.c contains
176465** a Tcl interface to read and write the values.
176466*/
176467#ifdef SQLITE_TEST
176468int test_fts3_node_chunksize = (4*1024);
176469int test_fts3_node_chunk_threshold = (4*1024)*4;
176470# define FTS3_NODE_CHUNKSIZE       test_fts3_node_chunksize
176471# define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
176472#else
176473# define FTS3_NODE_CHUNKSIZE (4*1024)
176474# define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
176475#endif
176476
176477/*
176478** The values that may be meaningfully bound to the :1 parameter in
176479** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
176480*/
176481#define FTS_STAT_DOCTOTAL      0
176482#define FTS_STAT_INCRMERGEHINT 1
176483#define FTS_STAT_AUTOINCRMERGE 2
176484
176485/*
176486** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
176487** and incremental merge operation that takes place. This is used for
176488** debugging FTS only, it should not usually be turned on in production
176489** systems.
176490*/
176491#ifdef FTS3_LOG_MERGES
176492static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
176493  sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
176494}
176495#else
176496#define fts3LogMerge(x, y)
176497#endif
176498
176499
176500typedef struct PendingList PendingList;
176501typedef struct SegmentNode SegmentNode;
176502typedef struct SegmentWriter SegmentWriter;
176503
176504/*
176505** An instance of the following data structure is used to build doclists
176506** incrementally. See function fts3PendingListAppend() for details.
176507*/
176508struct PendingList {
176509  int nData;
176510  char *aData;
176511  int nSpace;
176512  sqlite3_int64 iLastDocid;
176513  sqlite3_int64 iLastCol;
176514  sqlite3_int64 iLastPos;
176515};
176516
176517
176518/*
176519** Each cursor has a (possibly empty) linked list of the following objects.
176520*/
176521struct Fts3DeferredToken {
176522  Fts3PhraseToken *pToken;        /* Pointer to corresponding expr token */
176523  int iCol;                       /* Column token must occur in */
176524  Fts3DeferredToken *pNext;       /* Next in list of deferred tokens */
176525  PendingList *pList;             /* Doclist is assembled here */
176526};
176527
176528/*
176529** An instance of this structure is used to iterate through the terms on
176530** a contiguous set of segment b-tree leaf nodes. Although the details of
176531** this structure are only manipulated by code in this file, opaque handles
176532** of type Fts3SegReader* are also used by code in fts3.c to iterate through
176533** terms when querying the full-text index. See functions:
176534**
176535**   sqlite3Fts3SegReaderNew()
176536**   sqlite3Fts3SegReaderFree()
176537**   sqlite3Fts3SegReaderIterate()
176538**
176539** Methods used to manipulate Fts3SegReader structures:
176540**
176541**   fts3SegReaderNext()
176542**   fts3SegReaderFirstDocid()
176543**   fts3SegReaderNextDocid()
176544*/
176545struct Fts3SegReader {
176546  int iIdx;                       /* Index within level, or 0x7FFFFFFF for PT */
176547  u8 bLookup;                     /* True for a lookup only */
176548  u8 rootOnly;                    /* True for a root-only reader */
176549
176550  sqlite3_int64 iStartBlock;      /* Rowid of first leaf block to traverse */
176551  sqlite3_int64 iLeafEndBlock;    /* Rowid of final leaf block to traverse */
176552  sqlite3_int64 iEndBlock;        /* Rowid of final block in segment (or 0) */
176553  sqlite3_int64 iCurrentBlock;    /* Current leaf block (or 0) */
176554
176555  char *aNode;                    /* Pointer to node data (or NULL) */
176556  int nNode;                      /* Size of buffer at aNode (or 0) */
176557  int nPopulate;                  /* If >0, bytes of buffer aNode[] loaded */
176558  sqlite3_blob *pBlob;            /* If not NULL, blob handle to read node */
176559
176560  Fts3HashElem **ppNextElem;
176561
176562  /* Variables set by fts3SegReaderNext(). These may be read directly
176563  ** by the caller. They are valid from the time SegmentReaderNew() returns
176564  ** until SegmentReaderNext() returns something other than SQLITE_OK
176565  ** (i.e. SQLITE_DONE).
176566  */
176567  int nTerm;                      /* Number of bytes in current term */
176568  char *zTerm;                    /* Pointer to current term */
176569  int nTermAlloc;                 /* Allocated size of zTerm buffer */
176570  char *aDoclist;                 /* Pointer to doclist of current entry */
176571  int nDoclist;                   /* Size of doclist in current entry */
176572
176573  /* The following variables are used by fts3SegReaderNextDocid() to iterate
176574  ** through the current doclist (aDoclist/nDoclist).
176575  */
176576  char *pOffsetList;
176577  int nOffsetList;                /* For descending pending seg-readers only */
176578  sqlite3_int64 iDocid;
176579};
176580
176581#define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
176582#define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
176583
176584/*
176585** An instance of this structure is used to create a segment b-tree in the
176586** database. The internal details of this type are only accessed by the
176587** following functions:
176588**
176589**   fts3SegWriterAdd()
176590**   fts3SegWriterFlush()
176591**   fts3SegWriterFree()
176592*/
176593struct SegmentWriter {
176594  SegmentNode *pTree;             /* Pointer to interior tree structure */
176595  sqlite3_int64 iFirst;           /* First slot in %_segments written */
176596  sqlite3_int64 iFree;            /* Next free slot in %_segments */
176597  char *zTerm;                    /* Pointer to previous term buffer */
176598  int nTerm;                      /* Number of bytes in zTerm */
176599  int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
176600  char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
176601  int nSize;                      /* Size of allocation at aData */
176602  int nData;                      /* Bytes of data in aData */
176603  char *aData;                    /* Pointer to block from malloc() */
176604  i64 nLeafData;                  /* Number of bytes of leaf data written */
176605};
176606
176607/*
176608** Type SegmentNode is used by the following three functions to create
176609** the interior part of the segment b+-tree structures (everything except
176610** the leaf nodes). These functions and type are only ever used by code
176611** within the fts3SegWriterXXX() family of functions described above.
176612**
176613**   fts3NodeAddTerm()
176614**   fts3NodeWrite()
176615**   fts3NodeFree()
176616**
176617** When a b+tree is written to the database (either as a result of a merge
176618** or the pending-terms table being flushed), leaves are written into the
176619** database file as soon as they are completely populated. The interior of
176620** the tree is assembled in memory and written out only once all leaves have
176621** been populated and stored. This is Ok, as the b+-tree fanout is usually
176622** very large, meaning that the interior of the tree consumes relatively
176623** little memory.
176624*/
176625struct SegmentNode {
176626  SegmentNode *pParent;           /* Parent node (or NULL for root node) */
176627  SegmentNode *pRight;            /* Pointer to right-sibling */
176628  SegmentNode *pLeftmost;         /* Pointer to left-most node of this depth */
176629  int nEntry;                     /* Number of terms written to node so far */
176630  char *zTerm;                    /* Pointer to previous term buffer */
176631  int nTerm;                      /* Number of bytes in zTerm */
176632  int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
176633  char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
176634  int nData;                      /* Bytes of valid data so far */
176635  char *aData;                    /* Node data */
176636};
176637
176638/*
176639** Valid values for the second argument to fts3SqlStmt().
176640*/
176641#define SQL_DELETE_CONTENT             0
176642#define SQL_IS_EMPTY                   1
176643#define SQL_DELETE_ALL_CONTENT         2
176644#define SQL_DELETE_ALL_SEGMENTS        3
176645#define SQL_DELETE_ALL_SEGDIR          4
176646#define SQL_DELETE_ALL_DOCSIZE         5
176647#define SQL_DELETE_ALL_STAT            6
176648#define SQL_SELECT_CONTENT_BY_ROWID    7
176649#define SQL_NEXT_SEGMENT_INDEX         8
176650#define SQL_INSERT_SEGMENTS            9
176651#define SQL_NEXT_SEGMENTS_ID          10
176652#define SQL_INSERT_SEGDIR             11
176653#define SQL_SELECT_LEVEL              12
176654#define SQL_SELECT_LEVEL_RANGE        13
176655#define SQL_SELECT_LEVEL_COUNT        14
176656#define SQL_SELECT_SEGDIR_MAX_LEVEL   15
176657#define SQL_DELETE_SEGDIR_LEVEL       16
176658#define SQL_DELETE_SEGMENTS_RANGE     17
176659#define SQL_CONTENT_INSERT            18
176660#define SQL_DELETE_DOCSIZE            19
176661#define SQL_REPLACE_DOCSIZE           20
176662#define SQL_SELECT_DOCSIZE            21
176663#define SQL_SELECT_STAT               22
176664#define SQL_REPLACE_STAT              23
176665
176666#define SQL_SELECT_ALL_PREFIX_LEVEL   24
176667#define SQL_DELETE_ALL_TERMS_SEGDIR   25
176668#define SQL_DELETE_SEGDIR_RANGE       26
176669#define SQL_SELECT_ALL_LANGID         27
176670#define SQL_FIND_MERGE_LEVEL          28
176671#define SQL_MAX_LEAF_NODE_ESTIMATE    29
176672#define SQL_DELETE_SEGDIR_ENTRY       30
176673#define SQL_SHIFT_SEGDIR_ENTRY        31
176674#define SQL_SELECT_SEGDIR             32
176675#define SQL_CHOMP_SEGDIR              33
176676#define SQL_SEGMENT_IS_APPENDABLE     34
176677#define SQL_SELECT_INDEXES            35
176678#define SQL_SELECT_MXLEVEL            36
176679
176680#define SQL_SELECT_LEVEL_RANGE2       37
176681#define SQL_UPDATE_LEVEL_IDX          38
176682#define SQL_UPDATE_LEVEL              39
176683
176684/*
176685** This function is used to obtain an SQLite prepared statement handle
176686** for the statement identified by the second argument. If successful,
176687** *pp is set to the requested statement handle and SQLITE_OK returned.
176688** Otherwise, an SQLite error code is returned and *pp is set to 0.
176689**
176690** If argument apVal is not NULL, then it must point to an array with
176691** at least as many entries as the requested statement has bound
176692** parameters. The values are bound to the statements parameters before
176693** returning.
176694*/
176695static int fts3SqlStmt(
176696  Fts3Table *p,                   /* Virtual table handle */
176697  int eStmt,                      /* One of the SQL_XXX constants above */
176698  sqlite3_stmt **pp,              /* OUT: Statement handle */
176699  sqlite3_value **apVal           /* Values to bind to statement */
176700){
176701  const char *azSql[] = {
176702/* 0  */  "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
176703/* 1  */  "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
176704/* 2  */  "DELETE FROM %Q.'%q_content'",
176705/* 3  */  "DELETE FROM %Q.'%q_segments'",
176706/* 4  */  "DELETE FROM %Q.'%q_segdir'",
176707/* 5  */  "DELETE FROM %Q.'%q_docsize'",
176708/* 6  */  "DELETE FROM %Q.'%q_stat'",
176709/* 7  */  "SELECT %s WHERE rowid=?",
176710/* 8  */  "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
176711/* 9  */  "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
176712/* 10 */  "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
176713/* 11 */  "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
176714
176715          /* Return segments in order from oldest to newest.*/
176716/* 12 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
176717            "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
176718/* 13 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
176719            "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
176720            "ORDER BY level DESC, idx ASC",
176721
176722/* 14 */  "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
176723/* 15 */  "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
176724
176725/* 16 */  "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
176726/* 17 */  "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
176727/* 18 */  "INSERT INTO %Q.'%q_content' VALUES(%s)",
176728/* 19 */  "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
176729/* 20 */  "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
176730/* 21 */  "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
176731/* 22 */  "SELECT value FROM %Q.'%q_stat' WHERE id=?",
176732/* 23 */  "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
176733/* 24 */  "",
176734/* 25 */  "",
176735
176736/* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
176737/* 27 */ "SELECT ? UNION SELECT level / (1024 * ?) FROM %Q.'%q_segdir'",
176738
176739/* This statement is used to determine which level to read the input from
176740** when performing an incremental merge. It returns the absolute level number
176741** of the oldest level in the db that contains at least ? segments. Or,
176742** if no level in the FTS index contains more than ? segments, the statement
176743** returns zero rows.  */
176744/* 28 */ "SELECT level, count(*) AS cnt FROM %Q.'%q_segdir' "
176745         "  GROUP BY level HAVING cnt>=?"
176746         "  ORDER BY (level %% 1024) ASC, 2 DESC LIMIT 1",
176747
176748/* Estimate the upper limit on the number of leaf nodes in a new segment
176749** created by merging the oldest :2 segments from absolute level :1. See
176750** function sqlite3Fts3Incrmerge() for details.  */
176751/* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
176752         "  FROM %Q.'%q_segdir' WHERE level = ? AND idx < ?",
176753
176754/* SQL_DELETE_SEGDIR_ENTRY
176755**   Delete the %_segdir entry on absolute level :1 with index :2.  */
176756/* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
176757
176758/* SQL_SHIFT_SEGDIR_ENTRY
176759**   Modify the idx value for the segment with idx=:3 on absolute level :2
176760**   to :1.  */
176761/* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
176762
176763/* SQL_SELECT_SEGDIR
176764**   Read a single entry from the %_segdir table. The entry from absolute
176765**   level :1 with index value :2.  */
176766/* 32 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
176767            "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
176768
176769/* SQL_CHOMP_SEGDIR
176770**   Update the start_block (:1) and root (:2) fields of the %_segdir
176771**   entry located on absolute level :3 with index :4.  */
176772/* 33 */  "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
176773            "WHERE level = ? AND idx = ?",
176774
176775/* SQL_SEGMENT_IS_APPENDABLE
176776**   Return a single row if the segment with end_block=? is appendable. Or
176777**   no rows otherwise.  */
176778/* 34 */  "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
176779
176780/* SQL_SELECT_INDEXES
176781**   Return the list of valid segment indexes for absolute level ?  */
176782/* 35 */  "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
176783
176784/* SQL_SELECT_MXLEVEL
176785**   Return the largest relative level in the FTS index or indexes.  */
176786/* 36 */  "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'",
176787
176788          /* Return segments in order from oldest to newest.*/
176789/* 37 */  "SELECT level, idx, end_block "
176790            "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? "
176791            "ORDER BY level DESC, idx ASC",
176792
176793          /* Update statements used while promoting segments */
176794/* 38 */  "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
176795            "WHERE level=? AND idx=?",
176796/* 39 */  "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
176797
176798  };
176799  int rc = SQLITE_OK;
176800  sqlite3_stmt *pStmt;
176801
176802  assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
176803  assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
176804
176805  pStmt = p->aStmt[eStmt];
176806  if( !pStmt ){
176807    int f = SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB;
176808    char *zSql;
176809    if( eStmt==SQL_CONTENT_INSERT ){
176810      zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
176811    }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
176812      f &= ~SQLITE_PREPARE_NO_VTAB;
176813      zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
176814    }else{
176815      zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
176816    }
176817    if( !zSql ){
176818      rc = SQLITE_NOMEM;
176819    }else{
176820      rc = sqlite3_prepare_v3(p->db, zSql, -1, f, &pStmt, NULL);
176821      sqlite3_free(zSql);
176822      assert( rc==SQLITE_OK || pStmt==0 );
176823      p->aStmt[eStmt] = pStmt;
176824    }
176825  }
176826  if( apVal ){
176827    int i;
176828    int nParam = sqlite3_bind_parameter_count(pStmt);
176829    for(i=0; rc==SQLITE_OK && i<nParam; i++){
176830      rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
176831    }
176832  }
176833  *pp = pStmt;
176834  return rc;
176835}
176836
176837
176838static int fts3SelectDocsize(
176839  Fts3Table *pTab,                /* FTS3 table handle */
176840  sqlite3_int64 iDocid,           /* Docid to bind for SQL_SELECT_DOCSIZE */
176841  sqlite3_stmt **ppStmt           /* OUT: Statement handle */
176842){
176843  sqlite3_stmt *pStmt = 0;        /* Statement requested from fts3SqlStmt() */
176844  int rc;                         /* Return code */
176845
176846  rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE, &pStmt, 0);
176847  if( rc==SQLITE_OK ){
176848    sqlite3_bind_int64(pStmt, 1, iDocid);
176849    rc = sqlite3_step(pStmt);
176850    if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
176851      rc = sqlite3_reset(pStmt);
176852      if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
176853      pStmt = 0;
176854    }else{
176855      rc = SQLITE_OK;
176856    }
176857  }
176858
176859  *ppStmt = pStmt;
176860  return rc;
176861}
176862
176863SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
176864  Fts3Table *pTab,                /* Fts3 table handle */
176865  sqlite3_stmt **ppStmt           /* OUT: Statement handle */
176866){
176867  sqlite3_stmt *pStmt = 0;
176868  int rc;
176869  rc = fts3SqlStmt(pTab, SQL_SELECT_STAT, &pStmt, 0);
176870  if( rc==SQLITE_OK ){
176871    sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
176872    if( sqlite3_step(pStmt)!=SQLITE_ROW
176873     || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB
176874    ){
176875      rc = sqlite3_reset(pStmt);
176876      if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
176877      pStmt = 0;
176878    }
176879  }
176880  *ppStmt = pStmt;
176881  return rc;
176882}
176883
176884SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
176885  Fts3Table *pTab,                /* Fts3 table handle */
176886  sqlite3_int64 iDocid,           /* Docid to read size data for */
176887  sqlite3_stmt **ppStmt           /* OUT: Statement handle */
176888){
176889  return fts3SelectDocsize(pTab, iDocid, ppStmt);
176890}
176891
176892/*
176893** Similar to fts3SqlStmt(). Except, after binding the parameters in
176894** array apVal[] to the SQL statement identified by eStmt, the statement
176895** is executed.
176896**
176897** Returns SQLITE_OK if the statement is successfully executed, or an
176898** SQLite error code otherwise.
176899*/
176900static void fts3SqlExec(
176901  int *pRC,                /* Result code */
176902  Fts3Table *p,            /* The FTS3 table */
176903  int eStmt,               /* Index of statement to evaluate */
176904  sqlite3_value **apVal    /* Parameters to bind */
176905){
176906  sqlite3_stmt *pStmt;
176907  int rc;
176908  if( *pRC ) return;
176909  rc = fts3SqlStmt(p, eStmt, &pStmt, apVal);
176910  if( rc==SQLITE_OK ){
176911    sqlite3_step(pStmt);
176912    rc = sqlite3_reset(pStmt);
176913  }
176914  *pRC = rc;
176915}
176916
176917
176918/*
176919** This function ensures that the caller has obtained an exclusive
176920** shared-cache table-lock on the %_segdir table. This is required before
176921** writing data to the fts3 table. If this lock is not acquired first, then
176922** the caller may end up attempting to take this lock as part of committing
176923** a transaction, causing SQLite to return SQLITE_LOCKED or
176924** LOCKED_SHAREDCACHEto a COMMIT command.
176925**
176926** It is best to avoid this because if FTS3 returns any error when
176927** committing a transaction, the whole transaction will be rolled back.
176928** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE.
176929** It can still happen if the user locks the underlying tables directly
176930** instead of accessing them via FTS.
176931*/
176932static int fts3Writelock(Fts3Table *p){
176933  int rc = SQLITE_OK;
176934
176935  if( p->nPendingData==0 ){
176936    sqlite3_stmt *pStmt;
176937    rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pStmt, 0);
176938    if( rc==SQLITE_OK ){
176939      sqlite3_bind_null(pStmt, 1);
176940      sqlite3_step(pStmt);
176941      rc = sqlite3_reset(pStmt);
176942    }
176943  }
176944
176945  return rc;
176946}
176947
176948/*
176949** FTS maintains a separate indexes for each language-id (a 32-bit integer).
176950** Within each language id, a separate index is maintained to store the
176951** document terms, and each configured prefix size (configured the FTS
176952** "prefix=" option). And each index consists of multiple levels ("relative
176953** levels").
176954**
176955** All three of these values (the language id, the specific index and the
176956** level within the index) are encoded in 64-bit integer values stored
176957** in the %_segdir table on disk. This function is used to convert three
176958** separate component values into the single 64-bit integer value that
176959** can be used to query the %_segdir table.
176960**
176961** Specifically, each language-id/index combination is allocated 1024
176962** 64-bit integer level values ("absolute levels"). The main terms index
176963** for language-id 0 is allocate values 0-1023. The first prefix index
176964** (if any) for language-id 0 is allocated values 1024-2047. And so on.
176965** Language 1 indexes are allocated immediately following language 0.
176966**
176967** So, for a system with nPrefix prefix indexes configured, the block of
176968** absolute levels that corresponds to language-id iLangid and index
176969** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
176970*/
176971static sqlite3_int64 getAbsoluteLevel(
176972  Fts3Table *p,                   /* FTS3 table handle */
176973  int iLangid,                    /* Language id */
176974  int iIndex,                     /* Index in p->aIndex[] */
176975  int iLevel                      /* Level of segments */
176976){
176977  sqlite3_int64 iBase;            /* First absolute level for iLangid/iIndex */
176978  assert_fts3_nc( iLangid>=0 );
176979  assert( p->nIndex>0 );
176980  assert( iIndex>=0 && iIndex<p->nIndex );
176981
176982  iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
176983  return iBase + iLevel;
176984}
176985
176986/*
176987** Set *ppStmt to a statement handle that may be used to iterate through
176988** all rows in the %_segdir table, from oldest to newest. If successful,
176989** return SQLITE_OK. If an error occurs while preparing the statement,
176990** return an SQLite error code.
176991**
176992** There is only ever one instance of this SQL statement compiled for
176993** each FTS3 table.
176994**
176995** The statement returns the following columns from the %_segdir table:
176996**
176997**   0: idx
176998**   1: start_block
176999**   2: leaves_end_block
177000**   3: end_block
177001**   4: root
177002*/
177003SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(
177004  Fts3Table *p,                   /* FTS3 table */
177005  int iLangid,                    /* Language being queried */
177006  int iIndex,                     /* Index for p->aIndex[] */
177007  int iLevel,                     /* Level to select (relative level) */
177008  sqlite3_stmt **ppStmt           /* OUT: Compiled statement */
177009){
177010  int rc;
177011  sqlite3_stmt *pStmt = 0;
177012
177013  assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
177014  assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
177015  assert( iIndex>=0 && iIndex<p->nIndex );
177016
177017  if( iLevel<0 ){
177018    /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
177019    rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
177020    if( rc==SQLITE_OK ){
177021      sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
177022      sqlite3_bind_int64(pStmt, 2,
177023          getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
177024      );
177025    }
177026  }else{
177027    /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
177028    rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
177029    if( rc==SQLITE_OK ){
177030      sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel));
177031    }
177032  }
177033  *ppStmt = pStmt;
177034  return rc;
177035}
177036
177037
177038/*
177039** Append a single varint to a PendingList buffer. SQLITE_OK is returned
177040** if successful, or an SQLite error code otherwise.
177041**
177042** This function also serves to allocate the PendingList structure itself.
177043** For example, to create a new PendingList structure containing two
177044** varints:
177045**
177046**   PendingList *p = 0;
177047**   fts3PendingListAppendVarint(&p, 1);
177048**   fts3PendingListAppendVarint(&p, 2);
177049*/
177050static int fts3PendingListAppendVarint(
177051  PendingList **pp,               /* IN/OUT: Pointer to PendingList struct */
177052  sqlite3_int64 i                 /* Value to append to data */
177053){
177054  PendingList *p = *pp;
177055
177056  /* Allocate or grow the PendingList as required. */
177057  if( !p ){
177058    p = sqlite3_malloc(sizeof(*p) + 100);
177059    if( !p ){
177060      return SQLITE_NOMEM;
177061    }
177062    p->nSpace = 100;
177063    p->aData = (char *)&p[1];
177064    p->nData = 0;
177065  }
177066  else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
177067    int nNew = p->nSpace * 2;
177068    p = sqlite3_realloc(p, sizeof(*p) + nNew);
177069    if( !p ){
177070      sqlite3_free(*pp);
177071      *pp = 0;
177072      return SQLITE_NOMEM;
177073    }
177074    p->nSpace = nNew;
177075    p->aData = (char *)&p[1];
177076  }
177077
177078  /* Append the new serialized varint to the end of the list. */
177079  p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
177080  p->aData[p->nData] = '\0';
177081  *pp = p;
177082  return SQLITE_OK;
177083}
177084
177085/*
177086** Add a docid/column/position entry to a PendingList structure. Non-zero
177087** is returned if the structure is sqlite3_realloced as part of adding
177088** the entry. Otherwise, zero.
177089**
177090** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
177091** Zero is always returned in this case. Otherwise, if no OOM error occurs,
177092** it is set to SQLITE_OK.
177093*/
177094static int fts3PendingListAppend(
177095  PendingList **pp,               /* IN/OUT: PendingList structure */
177096  sqlite3_int64 iDocid,           /* Docid for entry to add */
177097  sqlite3_int64 iCol,             /* Column for entry to add */
177098  sqlite3_int64 iPos,             /* Position of term for entry to add */
177099  int *pRc                        /* OUT: Return code */
177100){
177101  PendingList *p = *pp;
177102  int rc = SQLITE_OK;
177103
177104  assert( !p || p->iLastDocid<=iDocid );
177105
177106  if( !p || p->iLastDocid!=iDocid ){
177107    u64 iDelta = (u64)iDocid - (u64)(p ? p->iLastDocid : 0);
177108    if( p ){
177109      assert( p->nData<p->nSpace );
177110      assert( p->aData[p->nData]==0 );
177111      p->nData++;
177112    }
177113    if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
177114      goto pendinglistappend_out;
177115    }
177116    p->iLastCol = -1;
177117    p->iLastPos = 0;
177118    p->iLastDocid = iDocid;
177119  }
177120  if( iCol>0 && p->iLastCol!=iCol ){
177121    if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
177122     || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
177123    ){
177124      goto pendinglistappend_out;
177125    }
177126    p->iLastCol = iCol;
177127    p->iLastPos = 0;
177128  }
177129  if( iCol>=0 ){
177130    assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
177131    rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
177132    if( rc==SQLITE_OK ){
177133      p->iLastPos = iPos;
177134    }
177135  }
177136
177137 pendinglistappend_out:
177138  *pRc = rc;
177139  if( p!=*pp ){
177140    *pp = p;
177141    return 1;
177142  }
177143  return 0;
177144}
177145
177146/*
177147** Free a PendingList object allocated by fts3PendingListAppend().
177148*/
177149static void fts3PendingListDelete(PendingList *pList){
177150  sqlite3_free(pList);
177151}
177152
177153/*
177154** Add an entry to one of the pending-terms hash tables.
177155*/
177156static int fts3PendingTermsAddOne(
177157  Fts3Table *p,
177158  int iCol,
177159  int iPos,
177160  Fts3Hash *pHash,                /* Pending terms hash table to add entry to */
177161  const char *zToken,
177162  int nToken
177163){
177164  PendingList *pList;
177165  int rc = SQLITE_OK;
177166
177167  pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
177168  if( pList ){
177169    p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
177170  }
177171  if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
177172    if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
177173      /* Malloc failed while inserting the new entry. This can only
177174      ** happen if there was no previous entry for this token.
177175      */
177176      assert( 0==fts3HashFind(pHash, zToken, nToken) );
177177      sqlite3_free(pList);
177178      rc = SQLITE_NOMEM;
177179    }
177180  }
177181  if( rc==SQLITE_OK ){
177182    p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
177183  }
177184  return rc;
177185}
177186
177187/*
177188** Tokenize the nul-terminated string zText and add all tokens to the
177189** pending-terms hash-table. The docid used is that currently stored in
177190** p->iPrevDocid, and the column is specified by argument iCol.
177191**
177192** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
177193*/
177194static int fts3PendingTermsAdd(
177195  Fts3Table *p,                   /* Table into which text will be inserted */
177196  int iLangid,                    /* Language id to use */
177197  const char *zText,              /* Text of document to be inserted */
177198  int iCol,                       /* Column into which text is being inserted */
177199  u32 *pnWord                     /* IN/OUT: Incr. by number tokens inserted */
177200){
177201  int rc;
177202  int iStart = 0;
177203  int iEnd = 0;
177204  int iPos = 0;
177205  int nWord = 0;
177206
177207  char const *zToken;
177208  int nToken = 0;
177209
177210  sqlite3_tokenizer *pTokenizer = p->pTokenizer;
177211  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
177212  sqlite3_tokenizer_cursor *pCsr;
177213  int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
177214      const char**,int*,int*,int*,int*);
177215
177216  assert( pTokenizer && pModule );
177217
177218  /* If the user has inserted a NULL value, this function may be called with
177219  ** zText==0. In this case, add zero token entries to the hash table and
177220  ** return early. */
177221  if( zText==0 ){
177222    *pnWord = 0;
177223    return SQLITE_OK;
177224  }
177225
177226  rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
177227  if( rc!=SQLITE_OK ){
177228    return rc;
177229  }
177230
177231  xNext = pModule->xNext;
177232  while( SQLITE_OK==rc
177233      && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
177234  ){
177235    int i;
177236    if( iPos>=nWord ) nWord = iPos+1;
177237
177238    /* Positions cannot be negative; we use -1 as a terminator internally.
177239    ** Tokens must have a non-zero length.
177240    */
177241    if( iPos<0 || !zToken || nToken<=0 ){
177242      rc = SQLITE_ERROR;
177243      break;
177244    }
177245
177246    /* Add the term to the terms index */
177247    rc = fts3PendingTermsAddOne(
177248        p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
177249    );
177250
177251    /* Add the term to each of the prefix indexes that it is not too
177252    ** short for. */
177253    for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
177254      struct Fts3Index *pIndex = &p->aIndex[i];
177255      if( nToken<pIndex->nPrefix ) continue;
177256      rc = fts3PendingTermsAddOne(
177257          p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
177258      );
177259    }
177260  }
177261
177262  pModule->xClose(pCsr);
177263  *pnWord += nWord;
177264  return (rc==SQLITE_DONE ? SQLITE_OK : rc);
177265}
177266
177267/*
177268** Calling this function indicates that subsequent calls to
177269** fts3PendingTermsAdd() are to add term/position-list pairs for the
177270** contents of the document with docid iDocid.
177271*/
177272static int fts3PendingTermsDocid(
177273  Fts3Table *p,                   /* Full-text table handle */
177274  int bDelete,                    /* True if this op is a delete */
177275  int iLangid,                    /* Language id of row being written */
177276  sqlite_int64 iDocid             /* Docid of row being written */
177277){
177278  assert( iLangid>=0 );
177279  assert( bDelete==1 || bDelete==0 );
177280
177281  /* TODO(shess) Explore whether partially flushing the buffer on
177282  ** forced-flush would provide better performance.  I suspect that if
177283  ** we ordered the doclists by size and flushed the largest until the
177284  ** buffer was half empty, that would let the less frequent terms
177285  ** generate longer doclists.
177286  */
177287  if( iDocid<p->iPrevDocid
177288   || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
177289   || p->iPrevLangid!=iLangid
177290   || p->nPendingData>p->nMaxPendingData
177291  ){
177292    int rc = sqlite3Fts3PendingTermsFlush(p);
177293    if( rc!=SQLITE_OK ) return rc;
177294  }
177295  p->iPrevDocid = iDocid;
177296  p->iPrevLangid = iLangid;
177297  p->bPrevDelete = bDelete;
177298  return SQLITE_OK;
177299}
177300
177301/*
177302** Discard the contents of the pending-terms hash tables.
177303*/
177304SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
177305  int i;
177306  for(i=0; i<p->nIndex; i++){
177307    Fts3HashElem *pElem;
177308    Fts3Hash *pHash = &p->aIndex[i].hPending;
177309    for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
177310      PendingList *pList = (PendingList *)fts3HashData(pElem);
177311      fts3PendingListDelete(pList);
177312    }
177313    fts3HashClear(pHash);
177314  }
177315  p->nPendingData = 0;
177316}
177317
177318/*
177319** This function is called by the xUpdate() method as part of an INSERT
177320** operation. It adds entries for each term in the new record to the
177321** pendingTerms hash table.
177322**
177323** Argument apVal is the same as the similarly named argument passed to
177324** fts3InsertData(). Parameter iDocid is the docid of the new row.
177325*/
177326static int fts3InsertTerms(
177327  Fts3Table *p,
177328  int iLangid,
177329  sqlite3_value **apVal,
177330  u32 *aSz
177331){
177332  int i;                          /* Iterator variable */
177333  for(i=2; i<p->nColumn+2; i++){
177334    int iCol = i-2;
177335    if( p->abNotindexed[iCol]==0 ){
177336      const char *zText = (const char *)sqlite3_value_text(apVal[i]);
177337      int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, &aSz[iCol]);
177338      if( rc!=SQLITE_OK ){
177339        return rc;
177340      }
177341      aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
177342    }
177343  }
177344  return SQLITE_OK;
177345}
177346
177347/*
177348** This function is called by the xUpdate() method for an INSERT operation.
177349** The apVal parameter is passed a copy of the apVal argument passed by
177350** SQLite to the xUpdate() method. i.e:
177351**
177352**   apVal[0]                Not used for INSERT.
177353**   apVal[1]                rowid
177354**   apVal[2]                Left-most user-defined column
177355**   ...
177356**   apVal[p->nColumn+1]     Right-most user-defined column
177357**   apVal[p->nColumn+2]     Hidden column with same name as table
177358**   apVal[p->nColumn+3]     Hidden "docid" column (alias for rowid)
177359**   apVal[p->nColumn+4]     Hidden languageid column
177360*/
177361static int fts3InsertData(
177362  Fts3Table *p,                   /* Full-text table */
177363  sqlite3_value **apVal,          /* Array of values to insert */
177364  sqlite3_int64 *piDocid          /* OUT: Docid for row just inserted */
177365){
177366  int rc;                         /* Return code */
177367  sqlite3_stmt *pContentInsert;   /* INSERT INTO %_content VALUES(...) */
177368
177369  if( p->zContentTbl ){
177370    sqlite3_value *pRowid = apVal[p->nColumn+3];
177371    if( sqlite3_value_type(pRowid)==SQLITE_NULL ){
177372      pRowid = apVal[1];
177373    }
177374    if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){
177375      return SQLITE_CONSTRAINT;
177376    }
177377    *piDocid = sqlite3_value_int64(pRowid);
177378    return SQLITE_OK;
177379  }
177380
177381  /* Locate the statement handle used to insert data into the %_content
177382  ** table. The SQL for this statement is:
177383  **
177384  **   INSERT INTO %_content VALUES(?, ?, ?, ...)
177385  **
177386  ** The statement features N '?' variables, where N is the number of user
177387  ** defined columns in the FTS3 table, plus one for the docid field.
177388  */
177389  rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
177390  if( rc==SQLITE_OK && p->zLanguageid ){
177391    rc = sqlite3_bind_int(
177392        pContentInsert, p->nColumn+2,
177393        sqlite3_value_int(apVal[p->nColumn+4])
177394    );
177395  }
177396  if( rc!=SQLITE_OK ) return rc;
177397
177398  /* There is a quirk here. The users INSERT statement may have specified
177399  ** a value for the "rowid" field, for the "docid" field, or for both.
177400  ** Which is a problem, since "rowid" and "docid" are aliases for the
177401  ** same value. For example:
177402  **
177403  **   INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
177404  **
177405  ** In FTS3, this is an error. It is an error to specify non-NULL values
177406  ** for both docid and some other rowid alias.
177407  */
177408  if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
177409    if( SQLITE_NULL==sqlite3_value_type(apVal[0])
177410     && SQLITE_NULL!=sqlite3_value_type(apVal[1])
177411    ){
177412      /* A rowid/docid conflict. */
177413      return SQLITE_ERROR;
177414    }
177415    rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
177416    if( rc!=SQLITE_OK ) return rc;
177417  }
177418
177419  /* Execute the statement to insert the record. Set *piDocid to the
177420  ** new docid value.
177421  */
177422  sqlite3_step(pContentInsert);
177423  rc = sqlite3_reset(pContentInsert);
177424
177425  *piDocid = sqlite3_last_insert_rowid(p->db);
177426  return rc;
177427}
177428
177429
177430
177431/*
177432** Remove all data from the FTS3 table. Clear the hash table containing
177433** pending terms.
177434*/
177435static int fts3DeleteAll(Fts3Table *p, int bContent){
177436  int rc = SQLITE_OK;             /* Return code */
177437
177438  /* Discard the contents of the pending-terms hash table. */
177439  sqlite3Fts3PendingTermsClear(p);
177440
177441  /* Delete everything from the shadow tables. Except, leave %_content as
177442  ** is if bContent is false.  */
177443  assert( p->zContentTbl==0 || bContent==0 );
177444  if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
177445  fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
177446  fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
177447  if( p->bHasDocsize ){
177448    fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
177449  }
177450  if( p->bHasStat ){
177451    fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
177452  }
177453  return rc;
177454}
177455
177456/*
177457**
177458*/
177459static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
177460  int iLangid = 0;
177461  if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
177462  return iLangid;
177463}
177464
177465/*
177466** The first element in the apVal[] array is assumed to contain the docid
177467** (an integer) of a row about to be deleted. Remove all terms from the
177468** full-text index.
177469*/
177470static void fts3DeleteTerms(
177471  int *pRC,               /* Result code */
177472  Fts3Table *p,           /* The FTS table to delete from */
177473  sqlite3_value *pRowid,  /* The docid to be deleted */
177474  u32 *aSz,               /* Sizes of deleted document written here */
177475  int *pbFound            /* OUT: Set to true if row really does exist */
177476){
177477  int rc;
177478  sqlite3_stmt *pSelect;
177479
177480  assert( *pbFound==0 );
177481  if( *pRC ) return;
177482  rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
177483  if( rc==SQLITE_OK ){
177484    if( SQLITE_ROW==sqlite3_step(pSelect) ){
177485      int i;
177486      int iLangid = langidFromSelect(p, pSelect);
177487      i64 iDocid = sqlite3_column_int64(pSelect, 0);
177488      rc = fts3PendingTermsDocid(p, 1, iLangid, iDocid);
177489      for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
177490        int iCol = i-1;
177491        if( p->abNotindexed[iCol]==0 ){
177492          const char *zText = (const char *)sqlite3_column_text(pSelect, i);
177493          rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
177494          aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
177495        }
177496      }
177497      if( rc!=SQLITE_OK ){
177498        sqlite3_reset(pSelect);
177499        *pRC = rc;
177500        return;
177501      }
177502      *pbFound = 1;
177503    }
177504    rc = sqlite3_reset(pSelect);
177505  }else{
177506    sqlite3_reset(pSelect);
177507  }
177508  *pRC = rc;
177509}
177510
177511/*
177512** Forward declaration to account for the circular dependency between
177513** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
177514*/
177515static int fts3SegmentMerge(Fts3Table *, int, int, int);
177516
177517/*
177518** This function allocates a new level iLevel index in the segdir table.
177519** Usually, indexes are allocated within a level sequentially starting
177520** with 0, so the allocated index is one greater than the value returned
177521** by:
177522**
177523**   SELECT max(idx) FROM %_segdir WHERE level = :iLevel
177524**
177525** However, if there are already FTS3_MERGE_COUNT indexes at the requested
177526** level, they are merged into a single level (iLevel+1) segment and the
177527** allocated index is 0.
177528**
177529** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
177530** returned. Otherwise, an SQLite error code is returned.
177531*/
177532static int fts3AllocateSegdirIdx(
177533  Fts3Table *p,
177534  int iLangid,                    /* Language id */
177535  int iIndex,                     /* Index for p->aIndex */
177536  int iLevel,
177537  int *piIdx
177538){
177539  int rc;                         /* Return Code */
177540  sqlite3_stmt *pNextIdx;         /* Query for next idx at level iLevel */
177541  int iNext = 0;                  /* Result of query pNextIdx */
177542
177543  assert( iLangid>=0 );
177544  assert( p->nIndex>=1 );
177545
177546  /* Set variable iNext to the next available segdir index at level iLevel. */
177547  rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
177548  if( rc==SQLITE_OK ){
177549    sqlite3_bind_int64(
177550        pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
177551    );
177552    if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
177553      iNext = sqlite3_column_int(pNextIdx, 0);
177554    }
177555    rc = sqlite3_reset(pNextIdx);
177556  }
177557
177558  if( rc==SQLITE_OK ){
177559    /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
177560    ** full, merge all segments in level iLevel into a single iLevel+1
177561    ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
177562    ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
177563    */
177564    if( iNext>=MergeCount(p) ){
177565      fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
177566      rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
177567      *piIdx = 0;
177568    }else{
177569      *piIdx = iNext;
177570    }
177571  }
177572
177573  return rc;
177574}
177575
177576/*
177577** The %_segments table is declared as follows:
177578**
177579**   CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
177580**
177581** This function reads data from a single row of the %_segments table. The
177582** specific row is identified by the iBlockid parameter. If paBlob is not
177583** NULL, then a buffer is allocated using sqlite3_malloc() and populated
177584** with the contents of the blob stored in the "block" column of the
177585** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
177586** to the size of the blob in bytes before returning.
177587**
177588** If an error occurs, or the table does not contain the specified row,
177589** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
177590** paBlob is non-NULL, then it is the responsibility of the caller to
177591** eventually free the returned buffer.
177592**
177593** This function may leave an open sqlite3_blob* handle in the
177594** Fts3Table.pSegments variable. This handle is reused by subsequent calls
177595** to this function. The handle may be closed by calling the
177596** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
177597** performance improvement, but the blob handle should always be closed
177598** before control is returned to the user (to prevent a lock being held
177599** on the database file for longer than necessary). Thus, any virtual table
177600** method (xFilter etc.) that may directly or indirectly call this function
177601** must call sqlite3Fts3SegmentsClose() before returning.
177602*/
177603SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
177604  Fts3Table *p,                   /* FTS3 table handle */
177605  sqlite3_int64 iBlockid,         /* Access the row with blockid=$iBlockid */
177606  char **paBlob,                  /* OUT: Blob data in malloc'd buffer */
177607  int *pnBlob,                    /* OUT: Size of blob data */
177608  int *pnLoad                     /* OUT: Bytes actually loaded */
177609){
177610  int rc;                         /* Return code */
177611
177612  /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
177613  assert( pnBlob );
177614
177615  if( p->pSegments ){
177616    rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
177617  }else{
177618    if( 0==p->zSegmentsTbl ){
177619      p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
177620      if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
177621    }
177622    rc = sqlite3_blob_open(
177623       p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
177624    );
177625  }
177626
177627  if( rc==SQLITE_OK ){
177628    int nByte = sqlite3_blob_bytes(p->pSegments);
177629    *pnBlob = nByte;
177630    if( paBlob ){
177631      char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
177632      if( !aByte ){
177633        rc = SQLITE_NOMEM;
177634      }else{
177635        if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
177636          nByte = FTS3_NODE_CHUNKSIZE;
177637          *pnLoad = nByte;
177638        }
177639        rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
177640        memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
177641        if( rc!=SQLITE_OK ){
177642          sqlite3_free(aByte);
177643          aByte = 0;
177644        }
177645      }
177646      *paBlob = aByte;
177647    }
177648  }else if( rc==SQLITE_ERROR ){
177649    rc = FTS_CORRUPT_VTAB;
177650  }
177651
177652  return rc;
177653}
177654
177655/*
177656** Close the blob handle at p->pSegments, if it is open. See comments above
177657** the sqlite3Fts3ReadBlock() function for details.
177658*/
177659SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
177660  sqlite3_blob_close(p->pSegments);
177661  p->pSegments = 0;
177662}
177663
177664static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
177665  int nRead;                      /* Number of bytes to read */
177666  int rc;                         /* Return code */
177667
177668  nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
177669  rc = sqlite3_blob_read(
177670      pReader->pBlob,
177671      &pReader->aNode[pReader->nPopulate],
177672      nRead,
177673      pReader->nPopulate
177674  );
177675
177676  if( rc==SQLITE_OK ){
177677    pReader->nPopulate += nRead;
177678    memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
177679    if( pReader->nPopulate==pReader->nNode ){
177680      sqlite3_blob_close(pReader->pBlob);
177681      pReader->pBlob = 0;
177682      pReader->nPopulate = 0;
177683    }
177684  }
177685  return rc;
177686}
177687
177688static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
177689  int rc = SQLITE_OK;
177690  assert( !pReader->pBlob
177691       || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
177692  );
177693  while( pReader->pBlob && rc==SQLITE_OK
177694     &&  (pFrom - pReader->aNode + nByte)>pReader->nPopulate
177695  ){
177696    rc = fts3SegReaderIncrRead(pReader);
177697  }
177698  return rc;
177699}
177700
177701/*
177702** Set an Fts3SegReader cursor to point at EOF.
177703*/
177704static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
177705  if( !fts3SegReaderIsRootOnly(pSeg) ){
177706    sqlite3_free(pSeg->aNode);
177707    sqlite3_blob_close(pSeg->pBlob);
177708    pSeg->pBlob = 0;
177709  }
177710  pSeg->aNode = 0;
177711}
177712
177713/*
177714** Move the iterator passed as the first argument to the next term in the
177715** segment. If successful, SQLITE_OK is returned. If there is no next term,
177716** SQLITE_DONE. Otherwise, an SQLite error code.
177717*/
177718static int fts3SegReaderNext(
177719  Fts3Table *p,
177720  Fts3SegReader *pReader,
177721  int bIncr
177722){
177723  int rc;                         /* Return code of various sub-routines */
177724  char *pNext;                    /* Cursor variable */
177725  int nPrefix;                    /* Number of bytes in term prefix */
177726  int nSuffix;                    /* Number of bytes in term suffix */
177727
177728  if( !pReader->aDoclist ){
177729    pNext = pReader->aNode;
177730  }else{
177731    pNext = &pReader->aDoclist[pReader->nDoclist];
177732  }
177733
177734  if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
177735
177736    if( fts3SegReaderIsPending(pReader) ){
177737      Fts3HashElem *pElem = *(pReader->ppNextElem);
177738      sqlite3_free(pReader->aNode);
177739      pReader->aNode = 0;
177740      if( pElem ){
177741        char *aCopy;
177742        PendingList *pList = (PendingList *)fts3HashData(pElem);
177743        int nCopy = pList->nData+1;
177744        pReader->zTerm = (char *)fts3HashKey(pElem);
177745        pReader->nTerm = fts3HashKeysize(pElem);
177746        aCopy = (char*)sqlite3_malloc(nCopy);
177747        if( !aCopy ) return SQLITE_NOMEM;
177748        memcpy(aCopy, pList->aData, nCopy);
177749        pReader->nNode = pReader->nDoclist = nCopy;
177750        pReader->aNode = pReader->aDoclist = aCopy;
177751        pReader->ppNextElem++;
177752        assert( pReader->aNode );
177753      }
177754      return SQLITE_OK;
177755    }
177756
177757    fts3SegReaderSetEof(pReader);
177758
177759    /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
177760    ** blocks have already been traversed.  */
177761#ifdef CORRUPT_DB
177762    assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock || CORRUPT_DB );
177763#endif
177764    if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
177765      return SQLITE_OK;
177766    }
177767
177768    rc = sqlite3Fts3ReadBlock(
177769        p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
177770        (bIncr ? &pReader->nPopulate : 0)
177771    );
177772    if( rc!=SQLITE_OK ) return rc;
177773    assert( pReader->pBlob==0 );
177774    if( bIncr && pReader->nPopulate<pReader->nNode ){
177775      pReader->pBlob = p->pSegments;
177776      p->pSegments = 0;
177777    }
177778    pNext = pReader->aNode;
177779  }
177780
177781  assert( !fts3SegReaderIsPending(pReader) );
177782
177783  rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
177784  if( rc!=SQLITE_OK ) return rc;
177785
177786  /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
177787  ** safe (no risk of overread) even if the node data is corrupted. */
177788  pNext += fts3GetVarint32(pNext, &nPrefix);
177789  pNext += fts3GetVarint32(pNext, &nSuffix);
177790  if( nSuffix<=0
177791   || (&pReader->aNode[pReader->nNode] - pNext)<nSuffix
177792   || nPrefix>pReader->nTerm
177793  ){
177794    return FTS_CORRUPT_VTAB;
177795  }
177796
177797  /* Both nPrefix and nSuffix were read by fts3GetVarint32() and so are
177798  ** between 0 and 0x7FFFFFFF. But the sum of the two may cause integer
177799  ** overflow - hence the (i64) casts.  */
177800  if( (i64)nPrefix+nSuffix>(i64)pReader->nTermAlloc ){
177801    i64 nNew = ((i64)nPrefix+nSuffix)*2;
177802    char *zNew = sqlite3_realloc64(pReader->zTerm, nNew);
177803    if( !zNew ){
177804      return SQLITE_NOMEM;
177805    }
177806    pReader->zTerm = zNew;
177807    pReader->nTermAlloc = nNew;
177808  }
177809
177810  rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
177811  if( rc!=SQLITE_OK ) return rc;
177812
177813  memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
177814  pReader->nTerm = nPrefix+nSuffix;
177815  pNext += nSuffix;
177816  pNext += fts3GetVarint32(pNext, &pReader->nDoclist);
177817  pReader->aDoclist = pNext;
177818  pReader->pOffsetList = 0;
177819
177820  /* Check that the doclist does not appear to extend past the end of the
177821  ** b-tree node. And that the final byte of the doclist is 0x00. If either
177822  ** of these statements is untrue, then the data structure is corrupt.
177823  */
177824  if( pReader->nDoclist > pReader->nNode-(pReader->aDoclist-pReader->aNode)
177825   || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
177826   || pReader->nDoclist==0
177827  ){
177828    return FTS_CORRUPT_VTAB;
177829  }
177830  return SQLITE_OK;
177831}
177832
177833/*
177834** Set the SegReader to point to the first docid in the doclist associated
177835** with the current term.
177836*/
177837static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
177838  int rc = SQLITE_OK;
177839  assert( pReader->aDoclist );
177840  assert( !pReader->pOffsetList );
177841  if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
177842    u8 bEof = 0;
177843    pReader->iDocid = 0;
177844    pReader->nOffsetList = 0;
177845    sqlite3Fts3DoclistPrev(0,
177846        pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
177847        &pReader->iDocid, &pReader->nOffsetList, &bEof
177848    );
177849  }else{
177850    rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
177851    if( rc==SQLITE_OK ){
177852      int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
177853      pReader->pOffsetList = &pReader->aDoclist[n];
177854    }
177855  }
177856  return rc;
177857}
177858
177859/*
177860** Advance the SegReader to point to the next docid in the doclist
177861** associated with the current term.
177862**
177863** If arguments ppOffsetList and pnOffsetList are not NULL, then
177864** *ppOffsetList is set to point to the first column-offset list
177865** in the doclist entry (i.e. immediately past the docid varint).
177866** *pnOffsetList is set to the length of the set of column-offset
177867** lists, not including the nul-terminator byte. For example:
177868*/
177869static int fts3SegReaderNextDocid(
177870  Fts3Table *pTab,
177871  Fts3SegReader *pReader,         /* Reader to advance to next docid */
177872  char **ppOffsetList,            /* OUT: Pointer to current position-list */
177873  int *pnOffsetList               /* OUT: Length of *ppOffsetList in bytes */
177874){
177875  int rc = SQLITE_OK;
177876  char *p = pReader->pOffsetList;
177877  char c = 0;
177878
177879  assert( p );
177880
177881  if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
177882    /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
177883    ** Pending-terms doclists are always built up in ascending order, so
177884    ** we have to iterate through them backwards here. */
177885    u8 bEof = 0;
177886    if( ppOffsetList ){
177887      *ppOffsetList = pReader->pOffsetList;
177888      *pnOffsetList = pReader->nOffsetList - 1;
177889    }
177890    sqlite3Fts3DoclistPrev(0,
177891        pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
177892        &pReader->nOffsetList, &bEof
177893    );
177894    if( bEof ){
177895      pReader->pOffsetList = 0;
177896    }else{
177897      pReader->pOffsetList = p;
177898    }
177899  }else{
177900    char *pEnd = &pReader->aDoclist[pReader->nDoclist];
177901
177902    /* Pointer p currently points at the first byte of an offset list. The
177903    ** following block advances it to point one byte past the end of
177904    ** the same offset list. */
177905    while( 1 ){
177906
177907      /* The following line of code (and the "p++" below the while() loop) is
177908      ** normally all that is required to move pointer p to the desired
177909      ** position. The exception is if this node is being loaded from disk
177910      ** incrementally and pointer "p" now points to the first byte past
177911      ** the populated part of pReader->aNode[].
177912      */
177913      while( *p | c ) c = *p++ & 0x80;
177914      assert( *p==0 );
177915
177916      if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
177917      rc = fts3SegReaderIncrRead(pReader);
177918      if( rc!=SQLITE_OK ) return rc;
177919    }
177920    p++;
177921
177922    /* If required, populate the output variables with a pointer to and the
177923    ** size of the previous offset-list.
177924    */
177925    if( ppOffsetList ){
177926      *ppOffsetList = pReader->pOffsetList;
177927      *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
177928    }
177929
177930    /* List may have been edited in place by fts3EvalNearTrim() */
177931    while( p<pEnd && *p==0 ) p++;
177932
177933    /* If there are no more entries in the doclist, set pOffsetList to
177934    ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
177935    ** Fts3SegReader.pOffsetList to point to the next offset list before
177936    ** returning.
177937    */
177938    if( p>=pEnd ){
177939      pReader->pOffsetList = 0;
177940    }else{
177941      rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
177942      if( rc==SQLITE_OK ){
177943        u64 iDelta;
177944        pReader->pOffsetList = p + sqlite3Fts3GetVarintU(p, &iDelta);
177945        if( pTab->bDescIdx ){
177946          pReader->iDocid = (i64)((u64)pReader->iDocid - iDelta);
177947        }else{
177948          pReader->iDocid = (i64)((u64)pReader->iDocid + iDelta);
177949        }
177950      }
177951    }
177952  }
177953
177954  return rc;
177955}
177956
177957
177958SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
177959  Fts3Cursor *pCsr,
177960  Fts3MultiSegReader *pMsr,
177961  int *pnOvfl
177962){
177963  Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
177964  int nOvfl = 0;
177965  int ii;
177966  int rc = SQLITE_OK;
177967  int pgsz = p->nPgsz;
177968
177969  assert( p->bFts4 );
177970  assert( pgsz>0 );
177971
177972  for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
177973    Fts3SegReader *pReader = pMsr->apSegment[ii];
177974    if( !fts3SegReaderIsPending(pReader)
177975     && !fts3SegReaderIsRootOnly(pReader)
177976    ){
177977      sqlite3_int64 jj;
177978      for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
177979        int nBlob;
177980        rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
177981        if( rc!=SQLITE_OK ) break;
177982        if( (nBlob+35)>pgsz ){
177983          nOvfl += (nBlob + 34)/pgsz;
177984        }
177985      }
177986    }
177987  }
177988  *pnOvfl = nOvfl;
177989  return rc;
177990}
177991
177992/*
177993** Free all allocations associated with the iterator passed as the
177994** second argument.
177995*/
177996SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
177997  if( pReader ){
177998    if( !fts3SegReaderIsPending(pReader) ){
177999      sqlite3_free(pReader->zTerm);
178000    }
178001    if( !fts3SegReaderIsRootOnly(pReader) ){
178002      sqlite3_free(pReader->aNode);
178003    }
178004    sqlite3_blob_close(pReader->pBlob);
178005  }
178006  sqlite3_free(pReader);
178007}
178008
178009/*
178010** Allocate a new SegReader object.
178011*/
178012SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
178013  int iAge,                       /* Segment "age". */
178014  int bLookup,                    /* True for a lookup only */
178015  sqlite3_int64 iStartLeaf,       /* First leaf to traverse */
178016  sqlite3_int64 iEndLeaf,         /* Final leaf to traverse */
178017  sqlite3_int64 iEndBlock,        /* Final block of segment */
178018  const char *zRoot,              /* Buffer containing root node */
178019  int nRoot,                      /* Size of buffer containing root node */
178020  Fts3SegReader **ppReader        /* OUT: Allocated Fts3SegReader */
178021){
178022  Fts3SegReader *pReader;         /* Newly allocated SegReader object */
178023  int nExtra = 0;                 /* Bytes to allocate segment root node */
178024
178025  assert( zRoot!=0 || nRoot==0 );
178026#ifdef CORRUPT_DB
178027  assert( zRoot!=0 || CORRUPT_DB );
178028#endif
178029
178030  if( iStartLeaf==0 ){
178031    if( iEndLeaf!=0 ) return FTS_CORRUPT_VTAB;
178032    nExtra = nRoot + FTS3_NODE_PADDING;
178033  }
178034
178035  pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
178036  if( !pReader ){
178037    return SQLITE_NOMEM;
178038  }
178039  memset(pReader, 0, sizeof(Fts3SegReader));
178040  pReader->iIdx = iAge;
178041  pReader->bLookup = bLookup!=0;
178042  pReader->iStartBlock = iStartLeaf;
178043  pReader->iLeafEndBlock = iEndLeaf;
178044  pReader->iEndBlock = iEndBlock;
178045
178046  if( nExtra ){
178047    /* The entire segment is stored in the root node. */
178048    pReader->aNode = (char *)&pReader[1];
178049    pReader->rootOnly = 1;
178050    pReader->nNode = nRoot;
178051    if( nRoot ) memcpy(pReader->aNode, zRoot, nRoot);
178052    memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
178053  }else{
178054    pReader->iCurrentBlock = iStartLeaf-1;
178055  }
178056  *ppReader = pReader;
178057  return SQLITE_OK;
178058}
178059
178060/*
178061** This is a comparison function used as a qsort() callback when sorting
178062** an array of pending terms by term. This occurs as part of flushing
178063** the contents of the pending-terms hash table to the database.
178064*/
178065static int SQLITE_CDECL fts3CompareElemByTerm(
178066  const void *lhs,
178067  const void *rhs
178068){
178069  char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
178070  char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
178071  int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
178072  int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
178073
178074  int n = (n1<n2 ? n1 : n2);
178075  int c = memcmp(z1, z2, n);
178076  if( c==0 ){
178077    c = n1 - n2;
178078  }
178079  return c;
178080}
178081
178082/*
178083** This function is used to allocate an Fts3SegReader that iterates through
178084** a subset of the terms stored in the Fts3Table.pendingTerms array.
178085**
178086** If the isPrefixIter parameter is zero, then the returned SegReader iterates
178087** through each term in the pending-terms table. Or, if isPrefixIter is
178088** non-zero, it iterates through each term and its prefixes. For example, if
178089** the pending terms hash table contains the terms "sqlite", "mysql" and
178090** "firebird", then the iterator visits the following 'terms' (in the order
178091** shown):
178092**
178093**   f fi fir fire fireb firebi firebir firebird
178094**   m my mys mysq mysql
178095**   s sq sql sqli sqlit sqlite
178096**
178097** Whereas if isPrefixIter is zero, the terms visited are:
178098**
178099**   firebird mysql sqlite
178100*/
178101SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
178102  Fts3Table *p,                   /* Virtual table handle */
178103  int iIndex,                     /* Index for p->aIndex */
178104  const char *zTerm,              /* Term to search for */
178105  int nTerm,                      /* Size of buffer zTerm */
178106  int bPrefix,                    /* True for a prefix iterator */
178107  Fts3SegReader **ppReader        /* OUT: SegReader for pending-terms */
178108){
178109  Fts3SegReader *pReader = 0;     /* Fts3SegReader object to return */
178110  Fts3HashElem *pE;               /* Iterator variable */
178111  Fts3HashElem **aElem = 0;       /* Array of term hash entries to scan */
178112  int nElem = 0;                  /* Size of array at aElem */
178113  int rc = SQLITE_OK;             /* Return Code */
178114  Fts3Hash *pHash;
178115
178116  pHash = &p->aIndex[iIndex].hPending;
178117  if( bPrefix ){
178118    int nAlloc = 0;               /* Size of allocated array at aElem */
178119
178120    for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
178121      char *zKey = (char *)fts3HashKey(pE);
178122      int nKey = fts3HashKeysize(pE);
178123      if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
178124        if( nElem==nAlloc ){
178125          Fts3HashElem **aElem2;
178126          nAlloc += 16;
178127          aElem2 = (Fts3HashElem **)sqlite3_realloc(
178128              aElem, nAlloc*sizeof(Fts3HashElem *)
178129          );
178130          if( !aElem2 ){
178131            rc = SQLITE_NOMEM;
178132            nElem = 0;
178133            break;
178134          }
178135          aElem = aElem2;
178136        }
178137
178138        aElem[nElem++] = pE;
178139      }
178140    }
178141
178142    /* If more than one term matches the prefix, sort the Fts3HashElem
178143    ** objects in term order using qsort(). This uses the same comparison
178144    ** callback as is used when flushing terms to disk.
178145    */
178146    if( nElem>1 ){
178147      qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
178148    }
178149
178150  }else{
178151    /* The query is a simple term lookup that matches at most one term in
178152    ** the index. All that is required is a straight hash-lookup.
178153    **
178154    ** Because the stack address of pE may be accessed via the aElem pointer
178155    ** below, the "Fts3HashElem *pE" must be declared so that it is valid
178156    ** within this entire function, not just this "else{...}" block.
178157    */
178158    pE = fts3HashFindElem(pHash, zTerm, nTerm);
178159    if( pE ){
178160      aElem = &pE;
178161      nElem = 1;
178162    }
178163  }
178164
178165  if( nElem>0 ){
178166    sqlite3_int64 nByte;
178167    nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
178168    pReader = (Fts3SegReader *)sqlite3_malloc64(nByte);
178169    if( !pReader ){
178170      rc = SQLITE_NOMEM;
178171    }else{
178172      memset(pReader, 0, nByte);
178173      pReader->iIdx = 0x7FFFFFFF;
178174      pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
178175      memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
178176    }
178177  }
178178
178179  if( bPrefix ){
178180    sqlite3_free(aElem);
178181  }
178182  *ppReader = pReader;
178183  return rc;
178184}
178185
178186/*
178187** Compare the entries pointed to by two Fts3SegReader structures.
178188** Comparison is as follows:
178189**
178190**   1) EOF is greater than not EOF.
178191**
178192**   2) The current terms (if any) are compared using memcmp(). If one
178193**      term is a prefix of another, the longer term is considered the
178194**      larger.
178195**
178196**   3) By segment age. An older segment is considered larger.
178197*/
178198static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
178199  int rc;
178200  if( pLhs->aNode && pRhs->aNode ){
178201    int rc2 = pLhs->nTerm - pRhs->nTerm;
178202    if( rc2<0 ){
178203      rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
178204    }else{
178205      rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
178206    }
178207    if( rc==0 ){
178208      rc = rc2;
178209    }
178210  }else{
178211    rc = (pLhs->aNode==0) - (pRhs->aNode==0);
178212  }
178213  if( rc==0 ){
178214    rc = pRhs->iIdx - pLhs->iIdx;
178215  }
178216  assert( rc!=0 );
178217  return rc;
178218}
178219
178220/*
178221** A different comparison function for SegReader structures. In this
178222** version, it is assumed that each SegReader points to an entry in
178223** a doclist for identical terms. Comparison is made as follows:
178224**
178225**   1) EOF (end of doclist in this case) is greater than not EOF.
178226**
178227**   2) By current docid.
178228**
178229**   3) By segment age. An older segment is considered larger.
178230*/
178231static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
178232  int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
178233  if( rc==0 ){
178234    if( pLhs->iDocid==pRhs->iDocid ){
178235      rc = pRhs->iIdx - pLhs->iIdx;
178236    }else{
178237      rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
178238    }
178239  }
178240  assert( pLhs->aNode && pRhs->aNode );
178241  return rc;
178242}
178243static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
178244  int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
178245  if( rc==0 ){
178246    if( pLhs->iDocid==pRhs->iDocid ){
178247      rc = pRhs->iIdx - pLhs->iIdx;
178248    }else{
178249      rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
178250    }
178251  }
178252  assert( pLhs->aNode && pRhs->aNode );
178253  return rc;
178254}
178255
178256/*
178257** Compare the term that the Fts3SegReader object passed as the first argument
178258** points to with the term specified by arguments zTerm and nTerm.
178259**
178260** If the pSeg iterator is already at EOF, return 0. Otherwise, return
178261** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
178262** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
178263*/
178264static int fts3SegReaderTermCmp(
178265  Fts3SegReader *pSeg,            /* Segment reader object */
178266  const char *zTerm,              /* Term to compare to */
178267  int nTerm                       /* Size of term zTerm in bytes */
178268){
178269  int res = 0;
178270  if( pSeg->aNode ){
178271    if( pSeg->nTerm>nTerm ){
178272      res = memcmp(pSeg->zTerm, zTerm, nTerm);
178273    }else{
178274      res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
178275    }
178276    if( res==0 ){
178277      res = pSeg->nTerm-nTerm;
178278    }
178279  }
178280  return res;
178281}
178282
178283/*
178284** Argument apSegment is an array of nSegment elements. It is known that
178285** the final (nSegment-nSuspect) members are already in sorted order
178286** (according to the comparison function provided). This function shuffles
178287** the array around until all entries are in sorted order.
178288*/
178289static void fts3SegReaderSort(
178290  Fts3SegReader **apSegment,                     /* Array to sort entries of */
178291  int nSegment,                                  /* Size of apSegment array */
178292  int nSuspect,                                  /* Unsorted entry count */
178293  int (*xCmp)(Fts3SegReader *, Fts3SegReader *)  /* Comparison function */
178294){
178295  int i;                          /* Iterator variable */
178296
178297  assert( nSuspect<=nSegment );
178298
178299  if( nSuspect==nSegment ) nSuspect--;
178300  for(i=nSuspect-1; i>=0; i--){
178301    int j;
178302    for(j=i; j<(nSegment-1); j++){
178303      Fts3SegReader *pTmp;
178304      if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
178305      pTmp = apSegment[j+1];
178306      apSegment[j+1] = apSegment[j];
178307      apSegment[j] = pTmp;
178308    }
178309  }
178310
178311#ifndef NDEBUG
178312  /* Check that the list really is sorted now. */
178313  for(i=0; i<(nSuspect-1); i++){
178314    assert( xCmp(apSegment[i], apSegment[i+1])<0 );
178315  }
178316#endif
178317}
178318
178319/*
178320** Insert a record into the %_segments table.
178321*/
178322static int fts3WriteSegment(
178323  Fts3Table *p,                   /* Virtual table handle */
178324  sqlite3_int64 iBlock,           /* Block id for new block */
178325  char *z,                        /* Pointer to buffer containing block data */
178326  int n                           /* Size of buffer z in bytes */
178327){
178328  sqlite3_stmt *pStmt;
178329  int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
178330  if( rc==SQLITE_OK ){
178331    sqlite3_bind_int64(pStmt, 1, iBlock);
178332    sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
178333    sqlite3_step(pStmt);
178334    rc = sqlite3_reset(pStmt);
178335    sqlite3_bind_null(pStmt, 2);
178336  }
178337  return rc;
178338}
178339
178340/*
178341** Find the largest relative level number in the table. If successful, set
178342** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
178343** set *pnMax to zero and return an SQLite error code.
178344*/
178345SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
178346  int rc;
178347  int mxLevel = 0;
178348  sqlite3_stmt *pStmt = 0;
178349
178350  rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, &pStmt, 0);
178351  if( rc==SQLITE_OK ){
178352    if( SQLITE_ROW==sqlite3_step(pStmt) ){
178353      mxLevel = sqlite3_column_int(pStmt, 0);
178354    }
178355    rc = sqlite3_reset(pStmt);
178356  }
178357  *pnMax = mxLevel;
178358  return rc;
178359}
178360
178361/*
178362** Insert a record into the %_segdir table.
178363*/
178364static int fts3WriteSegdir(
178365  Fts3Table *p,                   /* Virtual table handle */
178366  sqlite3_int64 iLevel,           /* Value for "level" field (absolute level) */
178367  int iIdx,                       /* Value for "idx" field */
178368  sqlite3_int64 iStartBlock,      /* Value for "start_block" field */
178369  sqlite3_int64 iLeafEndBlock,    /* Value for "leaves_end_block" field */
178370  sqlite3_int64 iEndBlock,        /* Value for "end_block" field */
178371  sqlite3_int64 nLeafData,        /* Bytes of leaf data in segment */
178372  char *zRoot,                    /* Blob value for "root" field */
178373  int nRoot                       /* Number of bytes in buffer zRoot */
178374){
178375  sqlite3_stmt *pStmt;
178376  int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
178377  if( rc==SQLITE_OK ){
178378    sqlite3_bind_int64(pStmt, 1, iLevel);
178379    sqlite3_bind_int(pStmt, 2, iIdx);
178380    sqlite3_bind_int64(pStmt, 3, iStartBlock);
178381    sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
178382    if( nLeafData==0 ){
178383      sqlite3_bind_int64(pStmt, 5, iEndBlock);
178384    }else{
178385      char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
178386      if( !zEnd ) return SQLITE_NOMEM;
178387      sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
178388    }
178389    sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
178390    sqlite3_step(pStmt);
178391    rc = sqlite3_reset(pStmt);
178392    sqlite3_bind_null(pStmt, 6);
178393  }
178394  return rc;
178395}
178396
178397/*
178398** Return the size of the common prefix (if any) shared by zPrev and
178399** zNext, in bytes. For example,
178400**
178401**   fts3PrefixCompress("abc", 3, "abcdef", 6)   // returns 3
178402**   fts3PrefixCompress("abX", 3, "abcdef", 6)   // returns 2
178403**   fts3PrefixCompress("abX", 3, "Xbcdef", 6)   // returns 0
178404*/
178405static int fts3PrefixCompress(
178406  const char *zPrev,              /* Buffer containing previous term */
178407  int nPrev,                      /* Size of buffer zPrev in bytes */
178408  const char *zNext,              /* Buffer containing next term */
178409  int nNext                       /* Size of buffer zNext in bytes */
178410){
178411  int n;
178412  UNUSED_PARAMETER(nNext);
178413  for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
178414  return n;
178415}
178416
178417/*
178418** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
178419** (according to memcmp) than the previous term.
178420*/
178421static int fts3NodeAddTerm(
178422  Fts3Table *p,                   /* Virtual table handle */
178423  SegmentNode **ppTree,           /* IN/OUT: SegmentNode handle */
178424  int isCopyTerm,                 /* True if zTerm/nTerm is transient */
178425  const char *zTerm,              /* Pointer to buffer containing term */
178426  int nTerm                       /* Size of term in bytes */
178427){
178428  SegmentNode *pTree = *ppTree;
178429  int rc;
178430  SegmentNode *pNew;
178431
178432  /* First try to append the term to the current node. Return early if
178433  ** this is possible.
178434  */
178435  if( pTree ){
178436    int nData = pTree->nData;     /* Current size of node in bytes */
178437    int nReq = nData;             /* Required space after adding zTerm */
178438    int nPrefix;                  /* Number of bytes of prefix compression */
178439    int nSuffix;                  /* Suffix length */
178440
178441    nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
178442    nSuffix = nTerm-nPrefix;
178443
178444    /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of
178445    ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
178446    ** compared with BINARY collation. This indicates corruption.  */
178447    if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
178448
178449    nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
178450    if( nReq<=p->nNodeSize || !pTree->zTerm ){
178451
178452      if( nReq>p->nNodeSize ){
178453        /* An unusual case: this is the first term to be added to the node
178454        ** and the static node buffer (p->nNodeSize bytes) is not large
178455        ** enough. Use a separately malloced buffer instead This wastes
178456        ** p->nNodeSize bytes, but since this scenario only comes about when
178457        ** the database contain two terms that share a prefix of almost 2KB,
178458        ** this is not expected to be a serious problem.
178459        */
178460        assert( pTree->aData==(char *)&pTree[1] );
178461        pTree->aData = (char *)sqlite3_malloc(nReq);
178462        if( !pTree->aData ){
178463          return SQLITE_NOMEM;
178464        }
178465      }
178466
178467      if( pTree->zTerm ){
178468        /* There is no prefix-length field for first term in a node */
178469        nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
178470      }
178471
178472      nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
178473      memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
178474      pTree->nData = nData + nSuffix;
178475      pTree->nEntry++;
178476
178477      if( isCopyTerm ){
178478        if( pTree->nMalloc<nTerm ){
178479          char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
178480          if( !zNew ){
178481            return SQLITE_NOMEM;
178482          }
178483          pTree->nMalloc = nTerm*2;
178484          pTree->zMalloc = zNew;
178485        }
178486        pTree->zTerm = pTree->zMalloc;
178487        memcpy(pTree->zTerm, zTerm, nTerm);
178488        pTree->nTerm = nTerm;
178489      }else{
178490        pTree->zTerm = (char *)zTerm;
178491        pTree->nTerm = nTerm;
178492      }
178493      return SQLITE_OK;
178494    }
178495  }
178496
178497  /* If control flows to here, it was not possible to append zTerm to the
178498  ** current node. Create a new node (a right-sibling of the current node).
178499  ** If this is the first node in the tree, the term is added to it.
178500  **
178501  ** Otherwise, the term is not added to the new node, it is left empty for
178502  ** now. Instead, the term is inserted into the parent of pTree. If pTree
178503  ** has no parent, one is created here.
178504  */
178505  pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
178506  if( !pNew ){
178507    return SQLITE_NOMEM;
178508  }
178509  memset(pNew, 0, sizeof(SegmentNode));
178510  pNew->nData = 1 + FTS3_VARINT_MAX;
178511  pNew->aData = (char *)&pNew[1];
178512
178513  if( pTree ){
178514    SegmentNode *pParent = pTree->pParent;
178515    rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
178516    if( pTree->pParent==0 ){
178517      pTree->pParent = pParent;
178518    }
178519    pTree->pRight = pNew;
178520    pNew->pLeftmost = pTree->pLeftmost;
178521    pNew->pParent = pParent;
178522    pNew->zMalloc = pTree->zMalloc;
178523    pNew->nMalloc = pTree->nMalloc;
178524    pTree->zMalloc = 0;
178525  }else{
178526    pNew->pLeftmost = pNew;
178527    rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm);
178528  }
178529
178530  *ppTree = pNew;
178531  return rc;
178532}
178533
178534/*
178535** Helper function for fts3NodeWrite().
178536*/
178537static int fts3TreeFinishNode(
178538  SegmentNode *pTree,
178539  int iHeight,
178540  sqlite3_int64 iLeftChild
178541){
178542  int nStart;
178543  assert( iHeight>=1 && iHeight<128 );
178544  nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
178545  pTree->aData[nStart] = (char)iHeight;
178546  sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
178547  return nStart;
178548}
178549
178550/*
178551** Write the buffer for the segment node pTree and all of its peers to the
178552** database. Then call this function recursively to write the parent of
178553** pTree and its peers to the database.
178554**
178555** Except, if pTree is a root node, do not write it to the database. Instead,
178556** set output variables *paRoot and *pnRoot to contain the root node.
178557**
178558** If successful, SQLITE_OK is returned and output variable *piLast is
178559** set to the largest blockid written to the database (or zero if no
178560** blocks were written to the db). Otherwise, an SQLite error code is
178561** returned.
178562*/
178563static int fts3NodeWrite(
178564  Fts3Table *p,                   /* Virtual table handle */
178565  SegmentNode *pTree,             /* SegmentNode handle */
178566  int iHeight,                    /* Height of this node in tree */
178567  sqlite3_int64 iLeaf,            /* Block id of first leaf node */
178568  sqlite3_int64 iFree,            /* Block id of next free slot in %_segments */
178569  sqlite3_int64 *piLast,          /* OUT: Block id of last entry written */
178570  char **paRoot,                  /* OUT: Data for root node */
178571  int *pnRoot                     /* OUT: Size of root node in bytes */
178572){
178573  int rc = SQLITE_OK;
178574
178575  if( !pTree->pParent ){
178576    /* Root node of the tree. */
178577    int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
178578    *piLast = iFree-1;
178579    *pnRoot = pTree->nData - nStart;
178580    *paRoot = &pTree->aData[nStart];
178581  }else{
178582    SegmentNode *pIter;
178583    sqlite3_int64 iNextFree = iFree;
178584    sqlite3_int64 iNextLeaf = iLeaf;
178585    for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
178586      int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
178587      int nWrite = pIter->nData - nStart;
178588
178589      rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
178590      iNextFree++;
178591      iNextLeaf += (pIter->nEntry+1);
178592    }
178593    if( rc==SQLITE_OK ){
178594      assert( iNextLeaf==iFree );
178595      rc = fts3NodeWrite(
178596          p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
178597      );
178598    }
178599  }
178600
178601  return rc;
178602}
178603
178604/*
178605** Free all memory allocations associated with the tree pTree.
178606*/
178607static void fts3NodeFree(SegmentNode *pTree){
178608  if( pTree ){
178609    SegmentNode *p = pTree->pLeftmost;
178610    fts3NodeFree(p->pParent);
178611    while( p ){
178612      SegmentNode *pRight = p->pRight;
178613      if( p->aData!=(char *)&p[1] ){
178614        sqlite3_free(p->aData);
178615      }
178616      assert( pRight==0 || p->zMalloc==0 );
178617      sqlite3_free(p->zMalloc);
178618      sqlite3_free(p);
178619      p = pRight;
178620    }
178621  }
178622}
178623
178624/*
178625** Add a term to the segment being constructed by the SegmentWriter object
178626** *ppWriter. When adding the first term to a segment, *ppWriter should
178627** be passed NULL. This function will allocate a new SegmentWriter object
178628** and return it via the input/output variable *ppWriter in this case.
178629**
178630** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
178631*/
178632static int fts3SegWriterAdd(
178633  Fts3Table *p,                   /* Virtual table handle */
178634  SegmentWriter **ppWriter,       /* IN/OUT: SegmentWriter handle */
178635  int isCopyTerm,                 /* True if buffer zTerm must be copied */
178636  const char *zTerm,              /* Pointer to buffer containing term */
178637  int nTerm,                      /* Size of term in bytes */
178638  const char *aDoclist,           /* Pointer to buffer containing doclist */
178639  int nDoclist                    /* Size of doclist in bytes */
178640){
178641  int nPrefix;                    /* Size of term prefix in bytes */
178642  int nSuffix;                    /* Size of term suffix in bytes */
178643  int nReq;                       /* Number of bytes required on leaf page */
178644  int nData;
178645  SegmentWriter *pWriter = *ppWriter;
178646
178647  if( !pWriter ){
178648    int rc;
178649    sqlite3_stmt *pStmt;
178650
178651    /* Allocate the SegmentWriter structure */
178652    pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
178653    if( !pWriter ) return SQLITE_NOMEM;
178654    memset(pWriter, 0, sizeof(SegmentWriter));
178655    *ppWriter = pWriter;
178656
178657    /* Allocate a buffer in which to accumulate data */
178658    pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
178659    if( !pWriter->aData ) return SQLITE_NOMEM;
178660    pWriter->nSize = p->nNodeSize;
178661
178662    /* Find the next free blockid in the %_segments table */
178663    rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
178664    if( rc!=SQLITE_OK ) return rc;
178665    if( SQLITE_ROW==sqlite3_step(pStmt) ){
178666      pWriter->iFree = sqlite3_column_int64(pStmt, 0);
178667      pWriter->iFirst = pWriter->iFree;
178668    }
178669    rc = sqlite3_reset(pStmt);
178670    if( rc!=SQLITE_OK ) return rc;
178671  }
178672  nData = pWriter->nData;
178673
178674  nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
178675  nSuffix = nTerm-nPrefix;
178676
178677  /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of
178678  ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
178679  ** compared with BINARY collation. This indicates corruption.  */
178680  if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
178681
178682  /* Figure out how many bytes are required by this new entry */
178683  nReq = sqlite3Fts3VarintLen(nPrefix) +    /* varint containing prefix size */
178684    sqlite3Fts3VarintLen(nSuffix) +         /* varint containing suffix size */
178685    nSuffix +                               /* Term suffix */
178686    sqlite3Fts3VarintLen(nDoclist) +        /* Size of doclist */
178687    nDoclist;                               /* Doclist data */
178688
178689  if( nData>0 && nData+nReq>p->nNodeSize ){
178690    int rc;
178691
178692    /* The current leaf node is full. Write it out to the database. */
178693    if( pWriter->iFree==LARGEST_INT64 ) return FTS_CORRUPT_VTAB;
178694    rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
178695    if( rc!=SQLITE_OK ) return rc;
178696    p->nLeafAdd++;
178697
178698    /* Add the current term to the interior node tree. The term added to
178699    ** the interior tree must:
178700    **
178701    **   a) be greater than the largest term on the leaf node just written
178702    **      to the database (still available in pWriter->zTerm), and
178703    **
178704    **   b) be less than or equal to the term about to be added to the new
178705    **      leaf node (zTerm/nTerm).
178706    **
178707    ** In other words, it must be the prefix of zTerm 1 byte longer than
178708    ** the common prefix (if any) of zTerm and pWriter->zTerm.
178709    */
178710    assert( nPrefix<nTerm );
178711    rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
178712    if( rc!=SQLITE_OK ) return rc;
178713
178714    nData = 0;
178715    pWriter->nTerm = 0;
178716
178717    nPrefix = 0;
178718    nSuffix = nTerm;
178719    nReq = 1 +                              /* varint containing prefix size */
178720      sqlite3Fts3VarintLen(nTerm) +         /* varint containing suffix size */
178721      nTerm +                               /* Term suffix */
178722      sqlite3Fts3VarintLen(nDoclist) +      /* Size of doclist */
178723      nDoclist;                             /* Doclist data */
178724  }
178725
178726  /* Increase the total number of bytes written to account for the new entry. */
178727  pWriter->nLeafData += nReq;
178728
178729  /* If the buffer currently allocated is too small for this entry, realloc
178730  ** the buffer to make it large enough.
178731  */
178732  if( nReq>pWriter->nSize ){
178733    char *aNew = sqlite3_realloc(pWriter->aData, nReq);
178734    if( !aNew ) return SQLITE_NOMEM;
178735    pWriter->aData = aNew;
178736    pWriter->nSize = nReq;
178737  }
178738  assert( nData+nReq<=pWriter->nSize );
178739
178740  /* Append the prefix-compressed term and doclist to the buffer. */
178741  nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
178742  nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
178743  assert( nSuffix>0 );
178744  memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
178745  nData += nSuffix;
178746  nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
178747  assert( nDoclist>0 );
178748  memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
178749  pWriter->nData = nData + nDoclist;
178750
178751  /* Save the current term so that it can be used to prefix-compress the next.
178752  ** If the isCopyTerm parameter is true, then the buffer pointed to by
178753  ** zTerm is transient, so take a copy of the term data. Otherwise, just
178754  ** store a copy of the pointer.
178755  */
178756  if( isCopyTerm ){
178757    if( nTerm>pWriter->nMalloc ){
178758      char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
178759      if( !zNew ){
178760        return SQLITE_NOMEM;
178761      }
178762      pWriter->nMalloc = nTerm*2;
178763      pWriter->zMalloc = zNew;
178764      pWriter->zTerm = zNew;
178765    }
178766    assert( pWriter->zTerm==pWriter->zMalloc );
178767    assert( nTerm>0 );
178768    memcpy(pWriter->zTerm, zTerm, nTerm);
178769  }else{
178770    pWriter->zTerm = (char *)zTerm;
178771  }
178772  pWriter->nTerm = nTerm;
178773
178774  return SQLITE_OK;
178775}
178776
178777/*
178778** Flush all data associated with the SegmentWriter object pWriter to the
178779** database. This function must be called after all terms have been added
178780** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
178781** returned. Otherwise, an SQLite error code.
178782*/
178783static int fts3SegWriterFlush(
178784  Fts3Table *p,                   /* Virtual table handle */
178785  SegmentWriter *pWriter,         /* SegmentWriter to flush to the db */
178786  sqlite3_int64 iLevel,           /* Value for 'level' column of %_segdir */
178787  int iIdx                        /* Value for 'idx' column of %_segdir */
178788){
178789  int rc;                         /* Return code */
178790  if( pWriter->pTree ){
178791    sqlite3_int64 iLast = 0;      /* Largest block id written to database */
178792    sqlite3_int64 iLastLeaf;      /* Largest leaf block id written to db */
178793    char *zRoot = NULL;           /* Pointer to buffer containing root node */
178794    int nRoot = 0;                /* Size of buffer zRoot */
178795
178796    iLastLeaf = pWriter->iFree;
178797    rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
178798    if( rc==SQLITE_OK ){
178799      rc = fts3NodeWrite(p, pWriter->pTree, 1,
178800          pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
178801    }
178802    if( rc==SQLITE_OK ){
178803      rc = fts3WriteSegdir(p, iLevel, iIdx,
178804          pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot);
178805    }
178806  }else{
178807    /* The entire tree fits on the root node. Write it to the segdir table. */
178808    rc = fts3WriteSegdir(p, iLevel, iIdx,
178809        0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData);
178810  }
178811  p->nLeafAdd++;
178812  return rc;
178813}
178814
178815/*
178816** Release all memory held by the SegmentWriter object passed as the
178817** first argument.
178818*/
178819static void fts3SegWriterFree(SegmentWriter *pWriter){
178820  if( pWriter ){
178821    sqlite3_free(pWriter->aData);
178822    sqlite3_free(pWriter->zMalloc);
178823    fts3NodeFree(pWriter->pTree);
178824    sqlite3_free(pWriter);
178825  }
178826}
178827
178828/*
178829** The first value in the apVal[] array is assumed to contain an integer.
178830** This function tests if there exist any documents with docid values that
178831** are different from that integer. i.e. if deleting the document with docid
178832** pRowid would mean the FTS3 table were empty.
178833**
178834** If successful, *pisEmpty is set to true if the table is empty except for
178835** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
178836** error occurs, an SQLite error code is returned.
178837*/
178838static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
178839  sqlite3_stmt *pStmt;
178840  int rc;
178841  if( p->zContentTbl ){
178842    /* If using the content=xxx option, assume the table is never empty */
178843    *pisEmpty = 0;
178844    rc = SQLITE_OK;
178845  }else{
178846    rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
178847    if( rc==SQLITE_OK ){
178848      if( SQLITE_ROW==sqlite3_step(pStmt) ){
178849        *pisEmpty = sqlite3_column_int(pStmt, 0);
178850      }
178851      rc = sqlite3_reset(pStmt);
178852    }
178853  }
178854  return rc;
178855}
178856
178857/*
178858** Set *pnMax to the largest segment level in the database for the index
178859** iIndex.
178860**
178861** Segment levels are stored in the 'level' column of the %_segdir table.
178862**
178863** Return SQLITE_OK if successful, or an SQLite error code if not.
178864*/
178865static int fts3SegmentMaxLevel(
178866  Fts3Table *p,
178867  int iLangid,
178868  int iIndex,
178869  sqlite3_int64 *pnMax
178870){
178871  sqlite3_stmt *pStmt;
178872  int rc;
178873  assert( iIndex>=0 && iIndex<p->nIndex );
178874
178875  /* Set pStmt to the compiled version of:
178876  **
178877  **   SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
178878  **
178879  ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
178880  */
178881  rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
178882  if( rc!=SQLITE_OK ) return rc;
178883  sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
178884  sqlite3_bind_int64(pStmt, 2,
178885      getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
178886  );
178887  if( SQLITE_ROW==sqlite3_step(pStmt) ){
178888    *pnMax = sqlite3_column_int64(pStmt, 0);
178889  }
178890  return sqlite3_reset(pStmt);
178891}
178892
178893/*
178894** iAbsLevel is an absolute level that may be assumed to exist within
178895** the database. This function checks if it is the largest level number
178896** within its index. Assuming no error occurs, *pbMax is set to 1 if
178897** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK
178898** is returned. If an error occurs, an error code is returned and the
178899** final value of *pbMax is undefined.
178900*/
178901static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){
178902
178903  /* Set pStmt to the compiled version of:
178904  **
178905  **   SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
178906  **
178907  ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
178908  */
178909  sqlite3_stmt *pStmt;
178910  int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
178911  if( rc!=SQLITE_OK ) return rc;
178912  sqlite3_bind_int64(pStmt, 1, iAbsLevel+1);
178913  sqlite3_bind_int64(pStmt, 2,
178914      (((u64)iAbsLevel/FTS3_SEGDIR_MAXLEVEL)+1) * FTS3_SEGDIR_MAXLEVEL
178915  );
178916
178917  *pbMax = 0;
178918  if( SQLITE_ROW==sqlite3_step(pStmt) ){
178919    *pbMax = sqlite3_column_type(pStmt, 0)==SQLITE_NULL;
178920  }
178921  return sqlite3_reset(pStmt);
178922}
178923
178924/*
178925** Delete all entries in the %_segments table associated with the segment
178926** opened with seg-reader pSeg. This function does not affect the contents
178927** of the %_segdir table.
178928*/
178929static int fts3DeleteSegment(
178930  Fts3Table *p,                   /* FTS table handle */
178931  Fts3SegReader *pSeg             /* Segment to delete */
178932){
178933  int rc = SQLITE_OK;             /* Return code */
178934  if( pSeg->iStartBlock ){
178935    sqlite3_stmt *pDelete;        /* SQL statement to delete rows */
178936    rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
178937    if( rc==SQLITE_OK ){
178938      sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
178939      sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
178940      sqlite3_step(pDelete);
178941      rc = sqlite3_reset(pDelete);
178942    }
178943  }
178944  return rc;
178945}
178946
178947/*
178948** This function is used after merging multiple segments into a single large
178949** segment to delete the old, now redundant, segment b-trees. Specifically,
178950** it:
178951**
178952**   1) Deletes all %_segments entries for the segments associated with
178953**      each of the SegReader objects in the array passed as the third
178954**      argument, and
178955**
178956**   2) deletes all %_segdir entries with level iLevel, or all %_segdir
178957**      entries regardless of level if (iLevel<0).
178958**
178959** SQLITE_OK is returned if successful, otherwise an SQLite error code.
178960*/
178961static int fts3DeleteSegdir(
178962  Fts3Table *p,                   /* Virtual table handle */
178963  int iLangid,                    /* Language id */
178964  int iIndex,                     /* Index for p->aIndex */
178965  int iLevel,                     /* Level of %_segdir entries to delete */
178966  Fts3SegReader **apSegment,      /* Array of SegReader objects */
178967  int nReader                     /* Size of array apSegment */
178968){
178969  int rc = SQLITE_OK;             /* Return Code */
178970  int i;                          /* Iterator variable */
178971  sqlite3_stmt *pDelete = 0;      /* SQL statement to delete rows */
178972
178973  for(i=0; rc==SQLITE_OK && i<nReader; i++){
178974    rc = fts3DeleteSegment(p, apSegment[i]);
178975  }
178976  if( rc!=SQLITE_OK ){
178977    return rc;
178978  }
178979
178980  assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
178981  if( iLevel==FTS3_SEGCURSOR_ALL ){
178982    rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
178983    if( rc==SQLITE_OK ){
178984      sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
178985      sqlite3_bind_int64(pDelete, 2,
178986          getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
178987      );
178988    }
178989  }else{
178990    rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
178991    if( rc==SQLITE_OK ){
178992      sqlite3_bind_int64(
178993          pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
178994      );
178995    }
178996  }
178997
178998  if( rc==SQLITE_OK ){
178999    sqlite3_step(pDelete);
179000    rc = sqlite3_reset(pDelete);
179001  }
179002
179003  return rc;
179004}
179005
179006/*
179007** When this function is called, buffer *ppList (size *pnList bytes) contains
179008** a position list that may (or may not) feature multiple columns. This
179009** function adjusts the pointer *ppList and the length *pnList so that they
179010** identify the subset of the position list that corresponds to column iCol.
179011**
179012** If there are no entries in the input position list for column iCol, then
179013** *pnList is set to zero before returning.
179014**
179015** If parameter bZero is non-zero, then any part of the input list following
179016** the end of the output list is zeroed before returning.
179017*/
179018static void fts3ColumnFilter(
179019  int iCol,                       /* Column to filter on */
179020  int bZero,                      /* Zero out anything following *ppList */
179021  char **ppList,                  /* IN/OUT: Pointer to position list */
179022  int *pnList                     /* IN/OUT: Size of buffer *ppList in bytes */
179023){
179024  char *pList = *ppList;
179025  int nList = *pnList;
179026  char *pEnd = &pList[nList];
179027  int iCurrent = 0;
179028  char *p = pList;
179029
179030  assert( iCol>=0 );
179031  while( 1 ){
179032    char c = 0;
179033    while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
179034
179035    if( iCol==iCurrent ){
179036      nList = (int)(p - pList);
179037      break;
179038    }
179039
179040    nList -= (int)(p - pList);
179041    pList = p;
179042    if( nList<=0 ){
179043      break;
179044    }
179045    p = &pList[1];
179046    p += fts3GetVarint32(p, &iCurrent);
179047  }
179048
179049  if( bZero && (pEnd - &pList[nList])>0){
179050    memset(&pList[nList], 0, pEnd - &pList[nList]);
179051  }
179052  *ppList = pList;
179053  *pnList = nList;
179054}
179055
179056/*
179057** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
179058** existing data). Grow the buffer if required.
179059**
179060** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
179061** trying to resize the buffer, return SQLITE_NOMEM.
179062*/
179063static int fts3MsrBufferData(
179064  Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
179065  char *pList,
179066  int nList
179067){
179068  if( nList>pMsr->nBuffer ){
179069    char *pNew;
179070    pMsr->nBuffer = nList*2;
179071    pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
179072    if( !pNew ) return SQLITE_NOMEM;
179073    pMsr->aBuffer = pNew;
179074  }
179075
179076  assert( nList>0 );
179077  memcpy(pMsr->aBuffer, pList, nList);
179078  return SQLITE_OK;
179079}
179080
179081SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
179082  Fts3Table *p,                   /* Virtual table handle */
179083  Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
179084  sqlite3_int64 *piDocid,         /* OUT: Docid value */
179085  char **paPoslist,               /* OUT: Pointer to position list */
179086  int *pnPoslist                  /* OUT: Size of position list in bytes */
179087){
179088  int nMerge = pMsr->nAdvance;
179089  Fts3SegReader **apSegment = pMsr->apSegment;
179090  int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
179091    p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
179092  );
179093
179094  if( nMerge==0 ){
179095    *paPoslist = 0;
179096    return SQLITE_OK;
179097  }
179098
179099  while( 1 ){
179100    Fts3SegReader *pSeg;
179101    pSeg = pMsr->apSegment[0];
179102
179103    if( pSeg->pOffsetList==0 ){
179104      *paPoslist = 0;
179105      break;
179106    }else{
179107      int rc;
179108      char *pList;
179109      int nList;
179110      int j;
179111      sqlite3_int64 iDocid = apSegment[0]->iDocid;
179112
179113      rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
179114      j = 1;
179115      while( rc==SQLITE_OK
179116        && j<nMerge
179117        && apSegment[j]->pOffsetList
179118        && apSegment[j]->iDocid==iDocid
179119      ){
179120        rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
179121        j++;
179122      }
179123      if( rc!=SQLITE_OK ) return rc;
179124      fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
179125
179126      if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){
179127        rc = fts3MsrBufferData(pMsr, pList, nList+1);
179128        if( rc!=SQLITE_OK ) return rc;
179129        assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
179130        pList = pMsr->aBuffer;
179131      }
179132
179133      if( pMsr->iColFilter>=0 ){
179134        fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
179135      }
179136
179137      if( nList>0 ){
179138        *paPoslist = pList;
179139        *piDocid = iDocid;
179140        *pnPoslist = nList;
179141        break;
179142      }
179143    }
179144  }
179145
179146  return SQLITE_OK;
179147}
179148
179149static int fts3SegReaderStart(
179150  Fts3Table *p,                   /* Virtual table handle */
179151  Fts3MultiSegReader *pCsr,       /* Cursor object */
179152  const char *zTerm,              /* Term searched for (or NULL) */
179153  int nTerm                       /* Length of zTerm in bytes */
179154){
179155  int i;
179156  int nSeg = pCsr->nSegment;
179157
179158  /* If the Fts3SegFilter defines a specific term (or term prefix) to search
179159  ** for, then advance each segment iterator until it points to a term of
179160  ** equal or greater value than the specified term. This prevents many
179161  ** unnecessary merge/sort operations for the case where single segment
179162  ** b-tree leaf nodes contain more than one term.
179163  */
179164  for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
179165    int res = 0;
179166    Fts3SegReader *pSeg = pCsr->apSegment[i];
179167    do {
179168      int rc = fts3SegReaderNext(p, pSeg, 0);
179169      if( rc!=SQLITE_OK ) return rc;
179170    }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
179171
179172    if( pSeg->bLookup && res!=0 ){
179173      fts3SegReaderSetEof(pSeg);
179174    }
179175  }
179176  fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
179177
179178  return SQLITE_OK;
179179}
179180
179181SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
179182  Fts3Table *p,                   /* Virtual table handle */
179183  Fts3MultiSegReader *pCsr,       /* Cursor object */
179184  Fts3SegFilter *pFilter          /* Restrictions on range of iteration */
179185){
179186  pCsr->pFilter = pFilter;
179187  return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
179188}
179189
179190SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
179191  Fts3Table *p,                   /* Virtual table handle */
179192  Fts3MultiSegReader *pCsr,       /* Cursor object */
179193  int iCol,                       /* Column to match on. */
179194  const char *zTerm,              /* Term to iterate through a doclist for */
179195  int nTerm                       /* Number of bytes in zTerm */
179196){
179197  int i;
179198  int rc;
179199  int nSegment = pCsr->nSegment;
179200  int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
179201    p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
179202  );
179203
179204  assert( pCsr->pFilter==0 );
179205  assert( zTerm && nTerm>0 );
179206
179207  /* Advance each segment iterator until it points to the term zTerm/nTerm. */
179208  rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
179209  if( rc!=SQLITE_OK ) return rc;
179210
179211  /* Determine how many of the segments actually point to zTerm/nTerm. */
179212  for(i=0; i<nSegment; i++){
179213    Fts3SegReader *pSeg = pCsr->apSegment[i];
179214    if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
179215      break;
179216    }
179217  }
179218  pCsr->nAdvance = i;
179219
179220  /* Advance each of the segments to point to the first docid. */
179221  for(i=0; i<pCsr->nAdvance; i++){
179222    rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
179223    if( rc!=SQLITE_OK ) return rc;
179224  }
179225  fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
179226
179227  assert( iCol<0 || iCol<p->nColumn );
179228  pCsr->iColFilter = iCol;
179229
179230  return SQLITE_OK;
179231}
179232
179233/*
179234** This function is called on a MultiSegReader that has been started using
179235** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
179236** have been made. Calling this function puts the MultiSegReader in such
179237** a state that if the next two calls are:
179238**
179239**   sqlite3Fts3SegReaderStart()
179240**   sqlite3Fts3SegReaderStep()
179241**
179242** then the entire doclist for the term is available in
179243** MultiSegReader.aDoclist/nDoclist.
179244*/
179245SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
179246  int i;                          /* Used to iterate through segment-readers */
179247
179248  assert( pCsr->zTerm==0 );
179249  assert( pCsr->nTerm==0 );
179250  assert( pCsr->aDoclist==0 );
179251  assert( pCsr->nDoclist==0 );
179252
179253  pCsr->nAdvance = 0;
179254  pCsr->bRestart = 1;
179255  for(i=0; i<pCsr->nSegment; i++){
179256    pCsr->apSegment[i]->pOffsetList = 0;
179257    pCsr->apSegment[i]->nOffsetList = 0;
179258    pCsr->apSegment[i]->iDocid = 0;
179259  }
179260
179261  return SQLITE_OK;
179262}
179263
179264
179265SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
179266  Fts3Table *p,                   /* Virtual table handle */
179267  Fts3MultiSegReader *pCsr        /* Cursor object */
179268){
179269  int rc = SQLITE_OK;
179270
179271  int isIgnoreEmpty =  (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
179272  int isRequirePos =   (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
179273  int isColFilter =    (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
179274  int isPrefix =       (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
179275  int isScan =         (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
179276  int isFirst =        (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
179277
179278  Fts3SegReader **apSegment = pCsr->apSegment;
179279  int nSegment = pCsr->nSegment;
179280  Fts3SegFilter *pFilter = pCsr->pFilter;
179281  int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
179282    p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
179283  );
179284
179285  if( pCsr->nSegment==0 ) return SQLITE_OK;
179286
179287  do {
179288    int nMerge;
179289    int i;
179290
179291    /* Advance the first pCsr->nAdvance entries in the apSegment[] array
179292    ** forward. Then sort the list in order of current term again.
179293    */
179294    for(i=0; i<pCsr->nAdvance; i++){
179295      Fts3SegReader *pSeg = apSegment[i];
179296      if( pSeg->bLookup ){
179297        fts3SegReaderSetEof(pSeg);
179298      }else{
179299        rc = fts3SegReaderNext(p, pSeg, 0);
179300      }
179301      if( rc!=SQLITE_OK ) return rc;
179302    }
179303    fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
179304    pCsr->nAdvance = 0;
179305
179306    /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
179307    assert( rc==SQLITE_OK );
179308    if( apSegment[0]->aNode==0 ) break;
179309
179310    pCsr->nTerm = apSegment[0]->nTerm;
179311    pCsr->zTerm = apSegment[0]->zTerm;
179312
179313    /* If this is a prefix-search, and if the term that apSegment[0] points
179314    ** to does not share a suffix with pFilter->zTerm/nTerm, then all
179315    ** required callbacks have been made. In this case exit early.
179316    **
179317    ** Similarly, if this is a search for an exact match, and the first term
179318    ** of segment apSegment[0] is not a match, exit early.
179319    */
179320    if( pFilter->zTerm && !isScan ){
179321      if( pCsr->nTerm<pFilter->nTerm
179322       || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
179323       || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
179324      ){
179325        break;
179326      }
179327    }
179328
179329    nMerge = 1;
179330    while( nMerge<nSegment
179331        && apSegment[nMerge]->aNode
179332        && apSegment[nMerge]->nTerm==pCsr->nTerm
179333        && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
179334    ){
179335      nMerge++;
179336    }
179337
179338    assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
179339    if( nMerge==1
179340     && !isIgnoreEmpty
179341     && !isFirst
179342     && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
179343    ){
179344      pCsr->nDoclist = apSegment[0]->nDoclist;
179345      if( fts3SegReaderIsPending(apSegment[0]) ){
179346        rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
179347        pCsr->aDoclist = pCsr->aBuffer;
179348      }else{
179349        pCsr->aDoclist = apSegment[0]->aDoclist;
179350      }
179351      if( rc==SQLITE_OK ) rc = SQLITE_ROW;
179352    }else{
179353      int nDoclist = 0;           /* Size of doclist */
179354      sqlite3_int64 iPrev = 0;    /* Previous docid stored in doclist */
179355
179356      /* The current term of the first nMerge entries in the array
179357      ** of Fts3SegReader objects is the same. The doclists must be merged
179358      ** and a single term returned with the merged doclist.
179359      */
179360      for(i=0; i<nMerge; i++){
179361        fts3SegReaderFirstDocid(p, apSegment[i]);
179362      }
179363      fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
179364      while( apSegment[0]->pOffsetList ){
179365        int j;                    /* Number of segments that share a docid */
179366        char *pList = 0;
179367        int nList = 0;
179368        int nByte;
179369        sqlite3_int64 iDocid = apSegment[0]->iDocid;
179370        fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
179371        j = 1;
179372        while( j<nMerge
179373            && apSegment[j]->pOffsetList
179374            && apSegment[j]->iDocid==iDocid
179375        ){
179376          fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
179377          j++;
179378        }
179379
179380        if( isColFilter ){
179381          fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
179382        }
179383
179384        if( !isIgnoreEmpty || nList>0 ){
179385
179386          /* Calculate the 'docid' delta value to write into the merged
179387          ** doclist. */
179388          sqlite3_int64 iDelta;
179389          if( p->bDescIdx && nDoclist>0 ){
179390            if( iPrev<=iDocid ) return FTS_CORRUPT_VTAB;
179391            iDelta = (i64)((u64)iPrev - (u64)iDocid);
179392          }else{
179393            if( nDoclist>0 && iPrev>=iDocid ) return FTS_CORRUPT_VTAB;
179394            iDelta = (i64)((u64)iDocid - (u64)iPrev);
179395          }
179396
179397          nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
179398          if( nDoclist+nByte>pCsr->nBuffer ){
179399            char *aNew;
179400            pCsr->nBuffer = (nDoclist+nByte)*2;
179401            aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
179402            if( !aNew ){
179403              return SQLITE_NOMEM;
179404            }
179405            pCsr->aBuffer = aNew;
179406          }
179407
179408          if( isFirst ){
179409            char *a = &pCsr->aBuffer[nDoclist];
179410            int nWrite;
179411
179412            nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
179413            if( nWrite ){
179414              iPrev = iDocid;
179415              nDoclist += nWrite;
179416            }
179417          }else{
179418            nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
179419            iPrev = iDocid;
179420            if( isRequirePos ){
179421              memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
179422              nDoclist += nList;
179423              pCsr->aBuffer[nDoclist++] = '\0';
179424            }
179425          }
179426        }
179427
179428        fts3SegReaderSort(apSegment, nMerge, j, xCmp);
179429      }
179430      if( nDoclist>0 ){
179431        pCsr->aDoclist = pCsr->aBuffer;
179432        pCsr->nDoclist = nDoclist;
179433        rc = SQLITE_ROW;
179434      }
179435    }
179436    pCsr->nAdvance = nMerge;
179437  }while( rc==SQLITE_OK );
179438
179439  return rc;
179440}
179441
179442
179443SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
179444  Fts3MultiSegReader *pCsr       /* Cursor object */
179445){
179446  if( pCsr ){
179447    int i;
179448    for(i=0; i<pCsr->nSegment; i++){
179449      sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
179450    }
179451    sqlite3_free(pCsr->apSegment);
179452    sqlite3_free(pCsr->aBuffer);
179453
179454    pCsr->nSegment = 0;
179455    pCsr->apSegment = 0;
179456    pCsr->aBuffer = 0;
179457  }
179458}
179459
179460/*
179461** Decode the "end_block" field, selected by column iCol of the SELECT
179462** statement passed as the first argument.
179463**
179464** The "end_block" field may contain either an integer, or a text field
179465** containing the text representation of two non-negative integers separated
179466** by one or more space (0x20) characters. In the first case, set *piEndBlock
179467** to the integer value and *pnByte to zero before returning. In the second,
179468** set *piEndBlock to the first value and *pnByte to the second.
179469*/
179470static void fts3ReadEndBlockField(
179471  sqlite3_stmt *pStmt,
179472  int iCol,
179473  i64 *piEndBlock,
179474  i64 *pnByte
179475){
179476  const unsigned char *zText = sqlite3_column_text(pStmt, iCol);
179477  if( zText ){
179478    int i;
179479    int iMul = 1;
179480    u64 iVal = 0;
179481    for(i=0; zText[i]>='0' && zText[i]<='9'; i++){
179482      iVal = iVal*10 + (zText[i] - '0');
179483    }
179484    *piEndBlock = (i64)iVal;
179485    while( zText[i]==' ' ) i++;
179486    iVal = 0;
179487    if( zText[i]=='-' ){
179488      i++;
179489      iMul = -1;
179490    }
179491    for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
179492      iVal = iVal*10 + (zText[i] - '0');
179493    }
179494    *pnByte = ((i64)iVal * (i64)iMul);
179495  }
179496}
179497
179498
179499/*
179500** A segment of size nByte bytes has just been written to absolute level
179501** iAbsLevel. Promote any segments that should be promoted as a result.
179502*/
179503static int fts3PromoteSegments(
179504  Fts3Table *p,                   /* FTS table handle */
179505  sqlite3_int64 iAbsLevel,        /* Absolute level just updated */
179506  sqlite3_int64 nByte             /* Size of new segment at iAbsLevel */
179507){
179508  int rc = SQLITE_OK;
179509  sqlite3_stmt *pRange;
179510
179511  rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE2, &pRange, 0);
179512
179513  if( rc==SQLITE_OK ){
179514    int bOk = 0;
179515    i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL + 1) * FTS3_SEGDIR_MAXLEVEL - 1;
179516    i64 nLimit = (nByte*3)/2;
179517
179518    /* Loop through all entries in the %_segdir table corresponding to
179519    ** segments in this index on levels greater than iAbsLevel. If there is
179520    ** at least one such segment, and it is possible to determine that all
179521    ** such segments are smaller than nLimit bytes in size, they will be
179522    ** promoted to level iAbsLevel.  */
179523    sqlite3_bind_int64(pRange, 1, iAbsLevel+1);
179524    sqlite3_bind_int64(pRange, 2, iLast);
179525    while( SQLITE_ROW==sqlite3_step(pRange) ){
179526      i64 nSize = 0, dummy;
179527      fts3ReadEndBlockField(pRange, 2, &dummy, &nSize);
179528      if( nSize<=0 || nSize>nLimit ){
179529        /* If nSize==0, then the %_segdir.end_block field does not not
179530        ** contain a size value. This happens if it was written by an
179531        ** old version of FTS. In this case it is not possible to determine
179532        ** the size of the segment, and so segment promotion does not
179533        ** take place.  */
179534        bOk = 0;
179535        break;
179536      }
179537      bOk = 1;
179538    }
179539    rc = sqlite3_reset(pRange);
179540
179541    if( bOk ){
179542      int iIdx = 0;
179543      sqlite3_stmt *pUpdate1 = 0;
179544      sqlite3_stmt *pUpdate2 = 0;
179545
179546      if( rc==SQLITE_OK ){
179547        rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX, &pUpdate1, 0);
179548      }
179549      if( rc==SQLITE_OK ){
179550        rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL, &pUpdate2, 0);
179551      }
179552
179553      if( rc==SQLITE_OK ){
179554
179555        /* Loop through all %_segdir entries for segments in this index with
179556        ** levels equal to or greater than iAbsLevel. As each entry is visited,
179557        ** updated it to set (level = -1) and (idx = N), where N is 0 for the
179558        ** oldest segment in the range, 1 for the next oldest, and so on.
179559        **
179560        ** In other words, move all segments being promoted to level -1,
179561        ** setting the "idx" fields as appropriate to keep them in the same
179562        ** order. The contents of level -1 (which is never used, except
179563        ** transiently here), will be moved back to level iAbsLevel below.  */
179564        sqlite3_bind_int64(pRange, 1, iAbsLevel);
179565        while( SQLITE_ROW==sqlite3_step(pRange) ){
179566          sqlite3_bind_int(pUpdate1, 1, iIdx++);
179567          sqlite3_bind_int(pUpdate1, 2, sqlite3_column_int(pRange, 0));
179568          sqlite3_bind_int(pUpdate1, 3, sqlite3_column_int(pRange, 1));
179569          sqlite3_step(pUpdate1);
179570          rc = sqlite3_reset(pUpdate1);
179571          if( rc!=SQLITE_OK ){
179572            sqlite3_reset(pRange);
179573            break;
179574          }
179575        }
179576      }
179577      if( rc==SQLITE_OK ){
179578        rc = sqlite3_reset(pRange);
179579      }
179580
179581      /* Move level -1 to level iAbsLevel */
179582      if( rc==SQLITE_OK ){
179583        sqlite3_bind_int64(pUpdate2, 1, iAbsLevel);
179584        sqlite3_step(pUpdate2);
179585        rc = sqlite3_reset(pUpdate2);
179586      }
179587    }
179588  }
179589
179590
179591  return rc;
179592}
179593
179594/*
179595** Merge all level iLevel segments in the database into a single
179596** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
179597** single segment with a level equal to the numerically largest level
179598** currently present in the database.
179599**
179600** If this function is called with iLevel<0, but there is only one
179601** segment in the database, SQLITE_DONE is returned immediately.
179602** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
179603** an SQLite error code is returned.
179604*/
179605static int fts3SegmentMerge(
179606  Fts3Table *p,
179607  int iLangid,                    /* Language id to merge */
179608  int iIndex,                     /* Index in p->aIndex[] to merge */
179609  int iLevel                      /* Level to merge */
179610){
179611  int rc;                         /* Return code */
179612  int iIdx = 0;                   /* Index of new segment */
179613  sqlite3_int64 iNewLevel = 0;    /* Level/index to create new segment at */
179614  SegmentWriter *pWriter = 0;     /* Used to write the new, merged, segment */
179615  Fts3SegFilter filter;           /* Segment term filter condition */
179616  Fts3MultiSegReader csr;         /* Cursor to iterate through level(s) */
179617  int bIgnoreEmpty = 0;           /* True to ignore empty segments */
179618  i64 iMaxLevel = 0;              /* Max level number for this index/langid */
179619
179620  assert( iLevel==FTS3_SEGCURSOR_ALL
179621       || iLevel==FTS3_SEGCURSOR_PENDING
179622       || iLevel>=0
179623  );
179624  assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
179625  assert( iIndex>=0 && iIndex<p->nIndex );
179626
179627  rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
179628  if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
179629
179630  if( iLevel!=FTS3_SEGCURSOR_PENDING ){
179631    rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iMaxLevel);
179632    if( rc!=SQLITE_OK ) goto finished;
179633  }
179634
179635  if( iLevel==FTS3_SEGCURSOR_ALL ){
179636    /* This call is to merge all segments in the database to a single
179637    ** segment. The level of the new segment is equal to the numerically
179638    ** greatest segment level currently present in the database for this
179639    ** index. The idx of the new segment is always 0.  */
179640    if( csr.nSegment==1 && 0==fts3SegReaderIsPending(csr.apSegment[0]) ){
179641      rc = SQLITE_DONE;
179642      goto finished;
179643    }
179644    iNewLevel = iMaxLevel;
179645    bIgnoreEmpty = 1;
179646
179647  }else{
179648    /* This call is to merge all segments at level iLevel. find the next
179649    ** available segment index at level iLevel+1. The call to
179650    ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
179651    ** a single iLevel+2 segment if necessary.  */
179652    assert( FTS3_SEGCURSOR_PENDING==-1 );
179653    iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
179654    rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
179655    bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING) && (iNewLevel>iMaxLevel);
179656  }
179657  if( rc!=SQLITE_OK ) goto finished;
179658
179659  assert( csr.nSegment>0 );
179660  assert_fts3_nc( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
179661  assert_fts3_nc(
179662    iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL)
179663  );
179664
179665  memset(&filter, 0, sizeof(Fts3SegFilter));
179666  filter.flags = FTS3_SEGMENT_REQUIRE_POS;
179667  filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
179668
179669  rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
179670  while( SQLITE_OK==rc ){
179671    rc = sqlite3Fts3SegReaderStep(p, &csr);
179672    if( rc!=SQLITE_ROW ) break;
179673    rc = fts3SegWriterAdd(p, &pWriter, 1,
179674        csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
179675  }
179676  if( rc!=SQLITE_OK ) goto finished;
179677  assert_fts3_nc( pWriter || bIgnoreEmpty );
179678
179679  if( iLevel!=FTS3_SEGCURSOR_PENDING ){
179680    rc = fts3DeleteSegdir(
179681        p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
179682    );
179683    if( rc!=SQLITE_OK ) goto finished;
179684  }
179685  if( pWriter ){
179686    rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
179687    if( rc==SQLITE_OK ){
179688      if( iLevel==FTS3_SEGCURSOR_PENDING || iNewLevel<iMaxLevel ){
179689        rc = fts3PromoteSegments(p, iNewLevel, pWriter->nLeafData);
179690      }
179691    }
179692  }
179693
179694 finished:
179695  fts3SegWriterFree(pWriter);
179696  sqlite3Fts3SegReaderFinish(&csr);
179697  return rc;
179698}
179699
179700
179701/*
179702** Flush the contents of pendingTerms to level 0 segments.
179703*/
179704SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
179705  int rc = SQLITE_OK;
179706  int i;
179707
179708  for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
179709    rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
179710    if( rc==SQLITE_DONE ) rc = SQLITE_OK;
179711  }
179712  sqlite3Fts3PendingTermsClear(p);
179713
179714  /* Determine the auto-incr-merge setting if unknown.  If enabled,
179715  ** estimate the number of leaf blocks of content to be written
179716  */
179717  if( rc==SQLITE_OK && p->bHasStat
179718   && p->nAutoincrmerge==0xff && p->nLeafAdd>0
179719  ){
179720    sqlite3_stmt *pStmt = 0;
179721    rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
179722    if( rc==SQLITE_OK ){
179723      sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
179724      rc = sqlite3_step(pStmt);
179725      if( rc==SQLITE_ROW ){
179726        p->nAutoincrmerge = sqlite3_column_int(pStmt, 0);
179727        if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
179728      }else if( rc==SQLITE_DONE ){
179729        p->nAutoincrmerge = 0;
179730      }
179731      rc = sqlite3_reset(pStmt);
179732    }
179733  }
179734  return rc;
179735}
179736
179737/*
179738** Encode N integers as varints into a blob.
179739*/
179740static void fts3EncodeIntArray(
179741  int N,             /* The number of integers to encode */
179742  u32 *a,            /* The integer values */
179743  char *zBuf,        /* Write the BLOB here */
179744  int *pNBuf         /* Write number of bytes if zBuf[] used here */
179745){
179746  int i, j;
179747  for(i=j=0; i<N; i++){
179748    j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
179749  }
179750  *pNBuf = j;
179751}
179752
179753/*
179754** Decode a blob of varints into N integers
179755*/
179756static void fts3DecodeIntArray(
179757  int N,             /* The number of integers to decode */
179758  u32 *a,            /* Write the integer values */
179759  const char *zBuf,  /* The BLOB containing the varints */
179760  int nBuf           /* size of the BLOB */
179761){
179762  int i = 0;
179763  if( nBuf && (zBuf[nBuf-1]&0x80)==0 ){
179764    int j;
179765    for(i=j=0; i<N && j<nBuf; i++){
179766      sqlite3_int64 x;
179767      j += sqlite3Fts3GetVarint(&zBuf[j], &x);
179768      a[i] = (u32)(x & 0xffffffff);
179769    }
179770  }
179771  while( i<N ) a[i++] = 0;
179772}
179773
179774/*
179775** Insert the sizes (in tokens) for each column of the document
179776** with docid equal to p->iPrevDocid.  The sizes are encoded as
179777** a blob of varints.
179778*/
179779static void fts3InsertDocsize(
179780  int *pRC,                       /* Result code */
179781  Fts3Table *p,                   /* Table into which to insert */
179782  u32 *aSz                        /* Sizes of each column, in tokens */
179783){
179784  char *pBlob;             /* The BLOB encoding of the document size */
179785  int nBlob;               /* Number of bytes in the BLOB */
179786  sqlite3_stmt *pStmt;     /* Statement used to insert the encoding */
179787  int rc;                  /* Result code from subfunctions */
179788
179789  if( *pRC ) return;
179790  pBlob = sqlite3_malloc64( 10*(sqlite3_int64)p->nColumn );
179791  if( pBlob==0 ){
179792    *pRC = SQLITE_NOMEM;
179793    return;
179794  }
179795  fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
179796  rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
179797  if( rc ){
179798    sqlite3_free(pBlob);
179799    *pRC = rc;
179800    return;
179801  }
179802  sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
179803  sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
179804  sqlite3_step(pStmt);
179805  *pRC = sqlite3_reset(pStmt);
179806}
179807
179808/*
179809** Record 0 of the %_stat table contains a blob consisting of N varints,
179810** where N is the number of user defined columns in the fts3 table plus
179811** two. If nCol is the number of user defined columns, then values of the
179812** varints are set as follows:
179813**
179814**   Varint 0:       Total number of rows in the table.
179815**
179816**   Varint 1..nCol: For each column, the total number of tokens stored in
179817**                   the column for all rows of the table.
179818**
179819**   Varint 1+nCol:  The total size, in bytes, of all text values in all
179820**                   columns of all rows of the table.
179821**
179822*/
179823static void fts3UpdateDocTotals(
179824  int *pRC,                       /* The result code */
179825  Fts3Table *p,                   /* Table being updated */
179826  u32 *aSzIns,                    /* Size increases */
179827  u32 *aSzDel,                    /* Size decreases */
179828  int nChng                       /* Change in the number of documents */
179829){
179830  char *pBlob;             /* Storage for BLOB written into %_stat */
179831  int nBlob;               /* Size of BLOB written into %_stat */
179832  u32 *a;                  /* Array of integers that becomes the BLOB */
179833  sqlite3_stmt *pStmt;     /* Statement for reading and writing */
179834  int i;                   /* Loop counter */
179835  int rc;                  /* Result code from subfunctions */
179836
179837  const int nStat = p->nColumn+2;
179838
179839  if( *pRC ) return;
179840  a = sqlite3_malloc64( (sizeof(u32)+10)*(sqlite3_int64)nStat );
179841  if( a==0 ){
179842    *pRC = SQLITE_NOMEM;
179843    return;
179844  }
179845  pBlob = (char*)&a[nStat];
179846  rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
179847  if( rc ){
179848    sqlite3_free(a);
179849    *pRC = rc;
179850    return;
179851  }
179852  sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
179853  if( sqlite3_step(pStmt)==SQLITE_ROW ){
179854    fts3DecodeIntArray(nStat, a,
179855         sqlite3_column_blob(pStmt, 0),
179856         sqlite3_column_bytes(pStmt, 0));
179857  }else{
179858    memset(a, 0, sizeof(u32)*(nStat) );
179859  }
179860  rc = sqlite3_reset(pStmt);
179861  if( rc!=SQLITE_OK ){
179862    sqlite3_free(a);
179863    *pRC = rc;
179864    return;
179865  }
179866  if( nChng<0 && a[0]<(u32)(-nChng) ){
179867    a[0] = 0;
179868  }else{
179869    a[0] += nChng;
179870  }
179871  for(i=0; i<p->nColumn+1; i++){
179872    u32 x = a[i+1];
179873    if( x+aSzIns[i] < aSzDel[i] ){
179874      x = 0;
179875    }else{
179876      x = x + aSzIns[i] - aSzDel[i];
179877    }
179878    a[i+1] = x;
179879  }
179880  fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
179881  rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
179882  if( rc ){
179883    sqlite3_free(a);
179884    *pRC = rc;
179885    return;
179886  }
179887  sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
179888  sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
179889  sqlite3_step(pStmt);
179890  *pRC = sqlite3_reset(pStmt);
179891  sqlite3_bind_null(pStmt, 2);
179892  sqlite3_free(a);
179893}
179894
179895/*
179896** Merge the entire database so that there is one segment for each
179897** iIndex/iLangid combination.
179898*/
179899static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
179900  int bSeenDone = 0;
179901  int rc;
179902  sqlite3_stmt *pAllLangid = 0;
179903
179904  rc = sqlite3Fts3PendingTermsFlush(p);
179905  if( rc==SQLITE_OK ){
179906    rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
179907  }
179908  if( rc==SQLITE_OK ){
179909    int rc2;
179910    sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
179911    sqlite3_bind_int(pAllLangid, 2, p->nIndex);
179912    while( sqlite3_step(pAllLangid)==SQLITE_ROW ){
179913      int i;
179914      int iLangid = sqlite3_column_int(pAllLangid, 0);
179915      for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
179916        rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL);
179917        if( rc==SQLITE_DONE ){
179918          bSeenDone = 1;
179919          rc = SQLITE_OK;
179920        }
179921      }
179922    }
179923    rc2 = sqlite3_reset(pAllLangid);
179924    if( rc==SQLITE_OK ) rc = rc2;
179925  }
179926
179927  sqlite3Fts3SegmentsClose(p);
179928
179929  return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
179930}
179931
179932/*
179933** This function is called when the user executes the following statement:
179934**
179935**     INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
179936**
179937** The entire FTS index is discarded and rebuilt. If the table is one
179938** created using the content=xxx option, then the new index is based on
179939** the current contents of the xxx table. Otherwise, it is rebuilt based
179940** on the contents of the %_content table.
179941*/
179942static int fts3DoRebuild(Fts3Table *p){
179943  int rc;                         /* Return Code */
179944
179945  rc = fts3DeleteAll(p, 0);
179946  if( rc==SQLITE_OK ){
179947    u32 *aSz = 0;
179948    u32 *aSzIns = 0;
179949    u32 *aSzDel = 0;
179950    sqlite3_stmt *pStmt = 0;
179951    int nEntry = 0;
179952
179953    /* Compose and prepare an SQL statement to loop through the content table */
179954    char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
179955    if( !zSql ){
179956      rc = SQLITE_NOMEM;
179957    }else{
179958      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
179959      sqlite3_free(zSql);
179960    }
179961
179962    if( rc==SQLITE_OK ){
179963      sqlite3_int64 nByte = sizeof(u32) * ((sqlite3_int64)p->nColumn+1)*3;
179964      aSz = (u32 *)sqlite3_malloc64(nByte);
179965      if( aSz==0 ){
179966        rc = SQLITE_NOMEM;
179967      }else{
179968        memset(aSz, 0, nByte);
179969        aSzIns = &aSz[p->nColumn+1];
179970        aSzDel = &aSzIns[p->nColumn+1];
179971      }
179972    }
179973
179974    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
179975      int iCol;
179976      int iLangid = langidFromSelect(p, pStmt);
179977      rc = fts3PendingTermsDocid(p, 0, iLangid, sqlite3_column_int64(pStmt, 0));
179978      memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
179979      for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
179980        if( p->abNotindexed[iCol]==0 ){
179981          const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
179982          rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
179983          aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
179984        }
179985      }
179986      if( p->bHasDocsize ){
179987        fts3InsertDocsize(&rc, p, aSz);
179988      }
179989      if( rc!=SQLITE_OK ){
179990        sqlite3_finalize(pStmt);
179991        pStmt = 0;
179992      }else{
179993        nEntry++;
179994        for(iCol=0; iCol<=p->nColumn; iCol++){
179995          aSzIns[iCol] += aSz[iCol];
179996        }
179997      }
179998    }
179999    if( p->bFts4 ){
180000      fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
180001    }
180002    sqlite3_free(aSz);
180003
180004    if( pStmt ){
180005      int rc2 = sqlite3_finalize(pStmt);
180006      if( rc==SQLITE_OK ){
180007        rc = rc2;
180008      }
180009    }
180010  }
180011
180012  return rc;
180013}
180014
180015
180016/*
180017** This function opens a cursor used to read the input data for an
180018** incremental merge operation. Specifically, it opens a cursor to scan
180019** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute
180020** level iAbsLevel.
180021*/
180022static int fts3IncrmergeCsr(
180023  Fts3Table *p,                   /* FTS3 table handle */
180024  sqlite3_int64 iAbsLevel,        /* Absolute level to open */
180025  int nSeg,                       /* Number of segments to merge */
180026  Fts3MultiSegReader *pCsr        /* Cursor object to populate */
180027){
180028  int rc;                         /* Return Code */
180029  sqlite3_stmt *pStmt = 0;        /* Statement used to read %_segdir entry */
180030  sqlite3_int64 nByte;            /* Bytes allocated at pCsr->apSegment[] */
180031
180032  /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
180033  memset(pCsr, 0, sizeof(*pCsr));
180034  nByte = sizeof(Fts3SegReader *) * nSeg;
180035  pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc64(nByte);
180036
180037  if( pCsr->apSegment==0 ){
180038    rc = SQLITE_NOMEM;
180039  }else{
180040    memset(pCsr->apSegment, 0, nByte);
180041    rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
180042  }
180043  if( rc==SQLITE_OK ){
180044    int i;
180045    int rc2;
180046    sqlite3_bind_int64(pStmt, 1, iAbsLevel);
180047    assert( pCsr->nSegment==0 );
180048    for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && i<nSeg; i++){
180049      rc = sqlite3Fts3SegReaderNew(i, 0,
180050          sqlite3_column_int64(pStmt, 1),        /* segdir.start_block */
180051          sqlite3_column_int64(pStmt, 2),        /* segdir.leaves_end_block */
180052          sqlite3_column_int64(pStmt, 3),        /* segdir.end_block */
180053          sqlite3_column_blob(pStmt, 4),         /* segdir.root */
180054          sqlite3_column_bytes(pStmt, 4),        /* segdir.root */
180055          &pCsr->apSegment[i]
180056      );
180057      pCsr->nSegment++;
180058    }
180059    rc2 = sqlite3_reset(pStmt);
180060    if( rc==SQLITE_OK ) rc = rc2;
180061  }
180062
180063  return rc;
180064}
180065
180066typedef struct IncrmergeWriter IncrmergeWriter;
180067typedef struct NodeWriter NodeWriter;
180068typedef struct Blob Blob;
180069typedef struct NodeReader NodeReader;
180070
180071/*
180072** An instance of the following structure is used as a dynamic buffer
180073** to build up nodes or other blobs of data in.
180074**
180075** The function blobGrowBuffer() is used to extend the allocation.
180076*/
180077struct Blob {
180078  char *a;                        /* Pointer to allocation */
180079  int n;                          /* Number of valid bytes of data in a[] */
180080  int nAlloc;                     /* Allocated size of a[] (nAlloc>=n) */
180081};
180082
180083/*
180084** This structure is used to build up buffers containing segment b-tree
180085** nodes (blocks).
180086*/
180087struct NodeWriter {
180088  sqlite3_int64 iBlock;           /* Current block id */
180089  Blob key;                       /* Last key written to the current block */
180090  Blob block;                     /* Current block image */
180091};
180092
180093/*
180094** An object of this type contains the state required to create or append
180095** to an appendable b-tree segment.
180096*/
180097struct IncrmergeWriter {
180098  int nLeafEst;                   /* Space allocated for leaf blocks */
180099  int nWork;                      /* Number of leaf pages flushed */
180100  sqlite3_int64 iAbsLevel;        /* Absolute level of input segments */
180101  int iIdx;                       /* Index of *output* segment in iAbsLevel+1 */
180102  sqlite3_int64 iStart;           /* Block number of first allocated block */
180103  sqlite3_int64 iEnd;             /* Block number of last allocated block */
180104  sqlite3_int64 nLeafData;        /* Bytes of leaf page data so far */
180105  u8 bNoLeafData;                 /* If true, store 0 for segment size */
180106  NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
180107};
180108
180109/*
180110** An object of the following type is used to read data from a single
180111** FTS segment node. See the following functions:
180112**
180113**     nodeReaderInit()
180114**     nodeReaderNext()
180115**     nodeReaderRelease()
180116*/
180117struct NodeReader {
180118  const char *aNode;
180119  int nNode;
180120  int iOff;                       /* Current offset within aNode[] */
180121
180122  /* Output variables. Containing the current node entry. */
180123  sqlite3_int64 iChild;           /* Pointer to child node */
180124  Blob term;                      /* Current term */
180125  const char *aDoclist;           /* Pointer to doclist */
180126  int nDoclist;                   /* Size of doclist in bytes */
180127};
180128
180129/*
180130** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
180131** Otherwise, if the allocation at pBlob->a is not already at least nMin
180132** bytes in size, extend (realloc) it to be so.
180133**
180134** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
180135** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
180136** to reflect the new size of the pBlob->a[] buffer.
180137*/
180138static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
180139  if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
180140    int nAlloc = nMin;
180141    char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
180142    if( a ){
180143      pBlob->nAlloc = nAlloc;
180144      pBlob->a = a;
180145    }else{
180146      *pRc = SQLITE_NOMEM;
180147    }
180148  }
180149}
180150
180151/*
180152** Attempt to advance the node-reader object passed as the first argument to
180153** the next entry on the node.
180154**
180155** Return an error code if an error occurs (SQLITE_NOMEM is possible).
180156** Otherwise return SQLITE_OK. If there is no next entry on the node
180157** (e.g. because the current entry is the last) set NodeReader->aNode to
180158** NULL to indicate EOF. Otherwise, populate the NodeReader structure output
180159** variables for the new entry.
180160*/
180161static int nodeReaderNext(NodeReader *p){
180162  int bFirst = (p->term.n==0);    /* True for first term on the node */
180163  int nPrefix = 0;                /* Bytes to copy from previous term */
180164  int nSuffix = 0;                /* Bytes to append to the prefix */
180165  int rc = SQLITE_OK;             /* Return code */
180166
180167  assert( p->aNode );
180168  if( p->iChild && bFirst==0 ) p->iChild++;
180169  if( p->iOff>=p->nNode ){
180170    /* EOF */
180171    p->aNode = 0;
180172  }else{
180173    if( bFirst==0 ){
180174      p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
180175    }
180176    p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
180177
180178    if( nPrefix>p->term.n || nSuffix>p->nNode-p->iOff || nSuffix==0 ){
180179      return FTS_CORRUPT_VTAB;
180180    }
180181    blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
180182    if( rc==SQLITE_OK ){
180183      memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
180184      p->term.n = nPrefix+nSuffix;
180185      p->iOff += nSuffix;
180186      if( p->iChild==0 ){
180187        p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
180188        if( (p->nNode-p->iOff)<p->nDoclist ){
180189          return FTS_CORRUPT_VTAB;
180190        }
180191        p->aDoclist = &p->aNode[p->iOff];
180192        p->iOff += p->nDoclist;
180193      }
180194    }
180195  }
180196
180197  assert_fts3_nc( p->iOff<=p->nNode );
180198  return rc;
180199}
180200
180201/*
180202** Release all dynamic resources held by node-reader object *p.
180203*/
180204static void nodeReaderRelease(NodeReader *p){
180205  sqlite3_free(p->term.a);
180206}
180207
180208/*
180209** Initialize a node-reader object to read the node in buffer aNode/nNode.
180210**
180211** If successful, SQLITE_OK is returned and the NodeReader object set to
180212** point to the first entry on the node (if any). Otherwise, an SQLite
180213** error code is returned.
180214*/
180215static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
180216  memset(p, 0, sizeof(NodeReader));
180217  p->aNode = aNode;
180218  p->nNode = nNode;
180219
180220  /* Figure out if this is a leaf or an internal node. */
180221  if( aNode && aNode[0] ){
180222    /* An internal node. */
180223    p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
180224  }else{
180225    p->iOff = 1;
180226  }
180227
180228  return aNode ? nodeReaderNext(p) : SQLITE_OK;
180229}
180230
180231/*
180232** This function is called while writing an FTS segment each time a leaf o
180233** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
180234** to be greater than the largest key on the node just written, but smaller
180235** than or equal to the first key that will be written to the next leaf
180236** node.
180237**
180238** The block id of the leaf node just written to disk may be found in
180239** (pWriter->aNodeWriter[0].iBlock) when this function is called.
180240*/
180241static int fts3IncrmergePush(
180242  Fts3Table *p,                   /* Fts3 table handle */
180243  IncrmergeWriter *pWriter,       /* Writer object */
180244  const char *zTerm,              /* Term to write to internal node */
180245  int nTerm                       /* Bytes at zTerm */
180246){
180247  sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
180248  int iLayer;
180249
180250  assert( nTerm>0 );
180251  for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
180252    sqlite3_int64 iNextPtr = 0;
180253    NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
180254    int rc = SQLITE_OK;
180255    int nPrefix;
180256    int nSuffix;
180257    int nSpace;
180258
180259    /* Figure out how much space the key will consume if it is written to
180260    ** the current node of layer iLayer. Due to the prefix compression,
180261    ** the space required changes depending on which node the key is to
180262    ** be added to.  */
180263    nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
180264    nSuffix = nTerm - nPrefix;
180265    if(nSuffix<=0 ) return FTS_CORRUPT_VTAB;
180266    nSpace  = sqlite3Fts3VarintLen(nPrefix);
180267    nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
180268
180269    if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){
180270      /* If the current node of layer iLayer contains zero keys, or if adding
180271      ** the key to it will not cause it to grow to larger than nNodeSize
180272      ** bytes in size, write the key here.  */
180273
180274      Blob *pBlk = &pNode->block;
180275      if( pBlk->n==0 ){
180276        blobGrowBuffer(pBlk, p->nNodeSize, &rc);
180277        if( rc==SQLITE_OK ){
180278          pBlk->a[0] = (char)iLayer;
180279          pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
180280        }
180281      }
180282      blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
180283      blobGrowBuffer(&pNode->key, nTerm, &rc);
180284
180285      if( rc==SQLITE_OK ){
180286        if( pNode->key.n ){
180287          pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
180288        }
180289        pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
180290        memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
180291        pBlk->n += nSuffix;
180292
180293        memcpy(pNode->key.a, zTerm, nTerm);
180294        pNode->key.n = nTerm;
180295      }
180296    }else{
180297      /* Otherwise, flush the current node of layer iLayer to disk.
180298      ** Then allocate a new, empty sibling node. The key will be written
180299      ** into the parent of this node. */
180300      rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
180301
180302      assert( pNode->block.nAlloc>=p->nNodeSize );
180303      pNode->block.a[0] = (char)iLayer;
180304      pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
180305
180306      iNextPtr = pNode->iBlock;
180307      pNode->iBlock++;
180308      pNode->key.n = 0;
180309    }
180310
180311    if( rc!=SQLITE_OK || iNextPtr==0 ) return rc;
180312    iPtr = iNextPtr;
180313  }
180314
180315  assert( 0 );
180316  return 0;
180317}
180318
180319/*
180320** Append a term and (optionally) doclist to the FTS segment node currently
180321** stored in blob *pNode. The node need not contain any terms, but the
180322** header must be written before this function is called.
180323**
180324** A node header is a single 0x00 byte for a leaf node, or a height varint
180325** followed by the left-hand-child varint for an internal node.
180326**
180327** The term to be appended is passed via arguments zTerm/nTerm. For a
180328** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
180329** node, both aDoclist and nDoclist must be passed 0.
180330**
180331** If the size of the value in blob pPrev is zero, then this is the first
180332** term written to the node. Otherwise, pPrev contains a copy of the
180333** previous term. Before this function returns, it is updated to contain a
180334** copy of zTerm/nTerm.
180335**
180336** It is assumed that the buffer associated with pNode is already large
180337** enough to accommodate the new entry. The buffer associated with pPrev
180338** is extended by this function if requrired.
180339**
180340** If an error (i.e. OOM condition) occurs, an SQLite error code is
180341** returned. Otherwise, SQLITE_OK.
180342*/
180343static int fts3AppendToNode(
180344  Blob *pNode,                    /* Current node image to append to */
180345  Blob *pPrev,                    /* Buffer containing previous term written */
180346  const char *zTerm,              /* New term to write */
180347  int nTerm,                      /* Size of zTerm in bytes */
180348  const char *aDoclist,           /* Doclist (or NULL) to write */
180349  int nDoclist                    /* Size of aDoclist in bytes */
180350){
180351  int rc = SQLITE_OK;             /* Return code */
180352  int bFirst = (pPrev->n==0);     /* True if this is the first term written */
180353  int nPrefix;                    /* Size of term prefix in bytes */
180354  int nSuffix;                    /* Size of term suffix in bytes */
180355
180356  /* Node must have already been started. There must be a doclist for a
180357  ** leaf node, and there must not be a doclist for an internal node.  */
180358  assert( pNode->n>0 );
180359  assert_fts3_nc( (pNode->a[0]=='\0')==(aDoclist!=0) );
180360
180361  blobGrowBuffer(pPrev, nTerm, &rc);
180362  if( rc!=SQLITE_OK ) return rc;
180363
180364  nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
180365  nSuffix = nTerm - nPrefix;
180366  if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
180367  memcpy(pPrev->a, zTerm, nTerm);
180368  pPrev->n = nTerm;
180369
180370  if( bFirst==0 ){
180371    pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
180372  }
180373  pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
180374  memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
180375  pNode->n += nSuffix;
180376
180377  if( aDoclist ){
180378    pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
180379    memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
180380    pNode->n += nDoclist;
180381  }
180382
180383  assert( pNode->n<=pNode->nAlloc );
180384
180385  return SQLITE_OK;
180386}
180387
180388/*
180389** Append the current term and doclist pointed to by cursor pCsr to the
180390** appendable b-tree segment opened for writing by pWriter.
180391**
180392** Return SQLITE_OK if successful, or an SQLite error code otherwise.
180393*/
180394static int fts3IncrmergeAppend(
180395  Fts3Table *p,                   /* Fts3 table handle */
180396  IncrmergeWriter *pWriter,       /* Writer object */
180397  Fts3MultiSegReader *pCsr        /* Cursor containing term and doclist */
180398){
180399  const char *zTerm = pCsr->zTerm;
180400  int nTerm = pCsr->nTerm;
180401  const char *aDoclist = pCsr->aDoclist;
180402  int nDoclist = pCsr->nDoclist;
180403  int rc = SQLITE_OK;           /* Return code */
180404  int nSpace;                   /* Total space in bytes required on leaf */
180405  int nPrefix;                  /* Size of prefix shared with previous term */
180406  int nSuffix;                  /* Size of suffix (nTerm - nPrefix) */
180407  NodeWriter *pLeaf;            /* Object used to write leaf nodes */
180408
180409  pLeaf = &pWriter->aNodeWriter[0];
180410  nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
180411  nSuffix = nTerm - nPrefix;
180412
180413  nSpace  = sqlite3Fts3VarintLen(nPrefix);
180414  nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
180415  nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
180416
180417  /* If the current block is not empty, and if adding this term/doclist
180418  ** to the current block would make it larger than Fts3Table.nNodeSize
180419  ** bytes, write this block out to the database. */
180420  if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
180421    rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
180422    pWriter->nWork++;
180423
180424    /* Add the current term to the parent node. The term added to the
180425    ** parent must:
180426    **
180427    **   a) be greater than the largest term on the leaf node just written
180428    **      to the database (still available in pLeaf->key), and
180429    **
180430    **   b) be less than or equal to the term about to be added to the new
180431    **      leaf node (zTerm/nTerm).
180432    **
180433    ** In other words, it must be the prefix of zTerm 1 byte longer than
180434    ** the common prefix (if any) of zTerm and pWriter->zTerm.
180435    */
180436    if( rc==SQLITE_OK ){
180437      rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
180438    }
180439
180440    /* Advance to the next output block */
180441    pLeaf->iBlock++;
180442    pLeaf->key.n = 0;
180443    pLeaf->block.n = 0;
180444
180445    nSuffix = nTerm;
180446    nSpace  = 1;
180447    nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
180448    nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
180449  }
180450
180451  pWriter->nLeafData += nSpace;
180452  blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
180453  if( rc==SQLITE_OK ){
180454    if( pLeaf->block.n==0 ){
180455      pLeaf->block.n = 1;
180456      pLeaf->block.a[0] = '\0';
180457    }
180458    rc = fts3AppendToNode(
180459        &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
180460    );
180461  }
180462
180463  return rc;
180464}
180465
180466/*
180467** This function is called to release all dynamic resources held by the
180468** merge-writer object pWriter, and if no error has occurred, to flush
180469** all outstanding node buffers held by pWriter to disk.
180470**
180471** If *pRc is not SQLITE_OK when this function is called, then no attempt
180472** is made to write any data to disk. Instead, this function serves only
180473** to release outstanding resources.
180474**
180475** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
180476** flushing buffers to disk, *pRc is set to an SQLite error code before
180477** returning.
180478*/
180479static void fts3IncrmergeRelease(
180480  Fts3Table *p,                   /* FTS3 table handle */
180481  IncrmergeWriter *pWriter,       /* Merge-writer object */
180482  int *pRc                        /* IN/OUT: Error code */
180483){
180484  int i;                          /* Used to iterate through non-root layers */
180485  int iRoot;                      /* Index of root in pWriter->aNodeWriter */
180486  NodeWriter *pRoot;              /* NodeWriter for root node */
180487  int rc = *pRc;                  /* Error code */
180488
180489  /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment
180490  ** root node. If the segment fits entirely on a single leaf node, iRoot
180491  ** will be set to 0. If the root node is the parent of the leaves, iRoot
180492  ** will be 1. And so on.  */
180493  for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
180494    NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
180495    if( pNode->block.n>0 ) break;
180496    assert( *pRc || pNode->block.nAlloc==0 );
180497    assert( *pRc || pNode->key.nAlloc==0 );
180498    sqlite3_free(pNode->block.a);
180499    sqlite3_free(pNode->key.a);
180500  }
180501
180502  /* Empty output segment. This is a no-op. */
180503  if( iRoot<0 ) return;
180504
180505  /* The entire output segment fits on a single node. Normally, this means
180506  ** the node would be stored as a blob in the "root" column of the %_segdir
180507  ** table. However, this is not permitted in this case. The problem is that
180508  ** space has already been reserved in the %_segments table, and so the
180509  ** start_block and end_block fields of the %_segdir table must be populated.
180510  ** And, by design or by accident, released versions of FTS cannot handle
180511  ** segments that fit entirely on the root node with start_block!=0.
180512  **
180513  ** Instead, create a synthetic root node that contains nothing but a
180514  ** pointer to the single content node. So that the segment consists of a
180515  ** single leaf and a single interior (root) node.
180516  **
180517  ** Todo: Better might be to defer allocating space in the %_segments
180518  ** table until we are sure it is needed.
180519  */
180520  if( iRoot==0 ){
180521    Blob *pBlock = &pWriter->aNodeWriter[1].block;
180522    blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX, &rc);
180523    if( rc==SQLITE_OK ){
180524      pBlock->a[0] = 0x01;
180525      pBlock->n = 1 + sqlite3Fts3PutVarint(
180526          &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
180527      );
180528    }
180529    iRoot = 1;
180530  }
180531  pRoot = &pWriter->aNodeWriter[iRoot];
180532
180533  /* Flush all currently outstanding nodes to disk. */
180534  for(i=0; i<iRoot; i++){
180535    NodeWriter *pNode = &pWriter->aNodeWriter[i];
180536    if( pNode->block.n>0 && rc==SQLITE_OK ){
180537      rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
180538    }
180539    sqlite3_free(pNode->block.a);
180540    sqlite3_free(pNode->key.a);
180541  }
180542
180543  /* Write the %_segdir record. */
180544  if( rc==SQLITE_OK ){
180545    rc = fts3WriteSegdir(p,
180546        pWriter->iAbsLevel+1,               /* level */
180547        pWriter->iIdx,                      /* idx */
180548        pWriter->iStart,                    /* start_block */
180549        pWriter->aNodeWriter[0].iBlock,     /* leaves_end_block */
180550        pWriter->iEnd,                      /* end_block */
180551        (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0),   /* end_block */
180552        pRoot->block.a, pRoot->block.n      /* root */
180553    );
180554  }
180555  sqlite3_free(pRoot->block.a);
180556  sqlite3_free(pRoot->key.a);
180557
180558  *pRc = rc;
180559}
180560
180561/*
180562** Compare the term in buffer zLhs (size in bytes nLhs) with that in
180563** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
180564** the other, it is considered to be smaller than the other.
180565**
180566** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
180567** if it is greater.
180568*/
180569static int fts3TermCmp(
180570  const char *zLhs, int nLhs,     /* LHS of comparison */
180571  const char *zRhs, int nRhs      /* RHS of comparison */
180572){
180573  int nCmp = MIN(nLhs, nRhs);
180574  int res;
180575
180576  res = (nCmp ? memcmp(zLhs, zRhs, nCmp) : 0);
180577  if( res==0 ) res = nLhs - nRhs;
180578
180579  return res;
180580}
180581
180582
180583/*
180584** Query to see if the entry in the %_segments table with blockid iEnd is
180585** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
180586** returning. Otherwise, set *pbRes to 0.
180587**
180588** Or, if an error occurs while querying the database, return an SQLite
180589** error code. The final value of *pbRes is undefined in this case.
180590**
180591** This is used to test if a segment is an "appendable" segment. If it
180592** is, then a NULL entry has been inserted into the %_segments table
180593** with blockid %_segdir.end_block.
180594*/
180595static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
180596  int bRes = 0;                   /* Result to set *pbRes to */
180597  sqlite3_stmt *pCheck = 0;       /* Statement to query database with */
180598  int rc;                         /* Return code */
180599
180600  rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, &pCheck, 0);
180601  if( rc==SQLITE_OK ){
180602    sqlite3_bind_int64(pCheck, 1, iEnd);
180603    if( SQLITE_ROW==sqlite3_step(pCheck) ) bRes = 1;
180604    rc = sqlite3_reset(pCheck);
180605  }
180606
180607  *pbRes = bRes;
180608  return rc;
180609}
180610
180611/*
180612** This function is called when initializing an incremental-merge operation.
180613** It checks if the existing segment with index value iIdx at absolute level
180614** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
180615** merge-writer object *pWriter is initialized to write to it.
180616**
180617** An existing segment can be appended to by an incremental merge if:
180618**
180619**   * It was initially created as an appendable segment (with all required
180620**     space pre-allocated), and
180621**
180622**   * The first key read from the input (arguments zKey and nKey) is
180623**     greater than the largest key currently stored in the potential
180624**     output segment.
180625*/
180626static int fts3IncrmergeLoad(
180627  Fts3Table *p,                   /* Fts3 table handle */
180628  sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
180629  int iIdx,                       /* Index of candidate output segment */
180630  const char *zKey,               /* First key to write */
180631  int nKey,                       /* Number of bytes in nKey */
180632  IncrmergeWriter *pWriter        /* Populate this object */
180633){
180634  int rc;                         /* Return code */
180635  sqlite3_stmt *pSelect = 0;      /* SELECT to read %_segdir entry */
180636
180637  rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pSelect, 0);
180638  if( rc==SQLITE_OK ){
180639    sqlite3_int64 iStart = 0;     /* Value of %_segdir.start_block */
180640    sqlite3_int64 iLeafEnd = 0;   /* Value of %_segdir.leaves_end_block */
180641    sqlite3_int64 iEnd = 0;       /* Value of %_segdir.end_block */
180642    const char *aRoot = 0;        /* Pointer to %_segdir.root buffer */
180643    int nRoot = 0;                /* Size of aRoot[] in bytes */
180644    int rc2;                      /* Return code from sqlite3_reset() */
180645    int bAppendable = 0;          /* Set to true if segment is appendable */
180646
180647    /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
180648    sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
180649    sqlite3_bind_int(pSelect, 2, iIdx);
180650    if( sqlite3_step(pSelect)==SQLITE_ROW ){
180651      iStart = sqlite3_column_int64(pSelect, 1);
180652      iLeafEnd = sqlite3_column_int64(pSelect, 2);
180653      fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData);
180654      if( pWriter->nLeafData<0 ){
180655        pWriter->nLeafData = pWriter->nLeafData * -1;
180656      }
180657      pWriter->bNoLeafData = (pWriter->nLeafData==0);
180658      nRoot = sqlite3_column_bytes(pSelect, 4);
180659      aRoot = sqlite3_column_blob(pSelect, 4);
180660      if( aRoot==0 ){
180661        sqlite3_reset(pSelect);
180662        return nRoot ? SQLITE_NOMEM : FTS_CORRUPT_VTAB;
180663      }
180664    }else{
180665      return sqlite3_reset(pSelect);
180666    }
180667
180668    /* Check for the zero-length marker in the %_segments table */
180669    rc = fts3IsAppendable(p, iEnd, &bAppendable);
180670
180671    /* Check that zKey/nKey is larger than the largest key the candidate */
180672    if( rc==SQLITE_OK && bAppendable ){
180673      char *aLeaf = 0;
180674      int nLeaf = 0;
180675
180676      rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
180677      if( rc==SQLITE_OK ){
180678        NodeReader reader;
180679        for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
180680            rc==SQLITE_OK && reader.aNode;
180681            rc = nodeReaderNext(&reader)
180682        ){
180683          assert( reader.aNode );
180684        }
180685        if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
180686          bAppendable = 0;
180687        }
180688        nodeReaderRelease(&reader);
180689      }
180690      sqlite3_free(aLeaf);
180691    }
180692
180693    if( rc==SQLITE_OK && bAppendable ){
180694      /* It is possible to append to this segment. Set up the IncrmergeWriter
180695      ** object to do so.  */
180696      int i;
180697      int nHeight = (int)aRoot[0];
180698      NodeWriter *pNode;
180699      if( nHeight<1 || nHeight>FTS_MAX_APPENDABLE_HEIGHT ){
180700        sqlite3_reset(pSelect);
180701        return FTS_CORRUPT_VTAB;
180702      }
180703
180704      pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
180705      pWriter->iStart = iStart;
180706      pWriter->iEnd = iEnd;
180707      pWriter->iAbsLevel = iAbsLevel;
180708      pWriter->iIdx = iIdx;
180709
180710      for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
180711        pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
180712      }
180713
180714      pNode = &pWriter->aNodeWriter[nHeight];
180715      pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
180716      blobGrowBuffer(&pNode->block,
180717          MAX(nRoot, p->nNodeSize)+FTS3_NODE_PADDING, &rc
180718      );
180719      if( rc==SQLITE_OK ){
180720        memcpy(pNode->block.a, aRoot, nRoot);
180721        pNode->block.n = nRoot;
180722        memset(&pNode->block.a[nRoot], 0, FTS3_NODE_PADDING);
180723      }
180724
180725      for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
180726        NodeReader reader;
180727        pNode = &pWriter->aNodeWriter[i];
180728
180729        if( pNode->block.a){
180730          rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
180731          while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader);
180732          blobGrowBuffer(&pNode->key, reader.term.n, &rc);
180733          if( rc==SQLITE_OK ){
180734            memcpy(pNode->key.a, reader.term.a, reader.term.n);
180735            pNode->key.n = reader.term.n;
180736            if( i>0 ){
180737              char *aBlock = 0;
180738              int nBlock = 0;
180739              pNode = &pWriter->aNodeWriter[i-1];
180740              pNode->iBlock = reader.iChild;
180741              rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0);
180742              blobGrowBuffer(&pNode->block,
180743                  MAX(nBlock, p->nNodeSize)+FTS3_NODE_PADDING, &rc
180744              );
180745              if( rc==SQLITE_OK ){
180746                memcpy(pNode->block.a, aBlock, nBlock);
180747                pNode->block.n = nBlock;
180748                memset(&pNode->block.a[nBlock], 0, FTS3_NODE_PADDING);
180749              }
180750              sqlite3_free(aBlock);
180751            }
180752          }
180753        }
180754        nodeReaderRelease(&reader);
180755      }
180756    }
180757
180758    rc2 = sqlite3_reset(pSelect);
180759    if( rc==SQLITE_OK ) rc = rc2;
180760  }
180761
180762  return rc;
180763}
180764
180765/*
180766** Determine the largest segment index value that exists within absolute
180767** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
180768** one before returning SQLITE_OK. Or, if there are no segments at all
180769** within level iAbsLevel, set *piIdx to zero.
180770**
180771** If an error occurs, return an SQLite error code. The final value of
180772** *piIdx is undefined in this case.
180773*/
180774static int fts3IncrmergeOutputIdx(
180775  Fts3Table *p,                   /* FTS Table handle */
180776  sqlite3_int64 iAbsLevel,        /* Absolute index of input segments */
180777  int *piIdx                      /* OUT: Next free index at iAbsLevel+1 */
180778){
180779  int rc;
180780  sqlite3_stmt *pOutputIdx = 0;   /* SQL used to find output index */
180781
180782  rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pOutputIdx, 0);
180783  if( rc==SQLITE_OK ){
180784    sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
180785    sqlite3_step(pOutputIdx);
180786    *piIdx = sqlite3_column_int(pOutputIdx, 0);
180787    rc = sqlite3_reset(pOutputIdx);
180788  }
180789
180790  return rc;
180791}
180792
180793/*
180794** Allocate an appendable output segment on absolute level iAbsLevel+1
180795** with idx value iIdx.
180796**
180797** In the %_segdir table, a segment is defined by the values in three
180798** columns:
180799**
180800**     start_block
180801**     leaves_end_block
180802**     end_block
180803**
180804** When an appendable segment is allocated, it is estimated that the
180805** maximum number of leaf blocks that may be required is the sum of the
180806** number of leaf blocks consumed by the input segments, plus the number
180807** of input segments, multiplied by two. This value is stored in stack
180808** variable nLeafEst.
180809**
180810** A total of 16*nLeafEst blocks are allocated when an appendable segment
180811** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
180812** array of leaf nodes starts at the first block allocated. The array
180813** of interior nodes that are parents of the leaf nodes start at block
180814** (start_block + (1 + end_block - start_block) / 16). And so on.
180815**
180816** In the actual code below, the value "16" is replaced with the
180817** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
180818*/
180819static int fts3IncrmergeWriter(
180820  Fts3Table *p,                   /* Fts3 table handle */
180821  sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
180822  int iIdx,                       /* Index of new output segment */
180823  Fts3MultiSegReader *pCsr,       /* Cursor that data will be read from */
180824  IncrmergeWriter *pWriter        /* Populate this object */
180825){
180826  int rc;                         /* Return Code */
180827  int i;                          /* Iterator variable */
180828  int nLeafEst = 0;               /* Blocks allocated for leaf nodes */
180829  sqlite3_stmt *pLeafEst = 0;     /* SQL used to determine nLeafEst */
180830  sqlite3_stmt *pFirstBlock = 0;  /* SQL used to determine first block */
180831
180832  /* Calculate nLeafEst. */
180833  rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, &pLeafEst, 0);
180834  if( rc==SQLITE_OK ){
180835    sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
180836    sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
180837    if( SQLITE_ROW==sqlite3_step(pLeafEst) ){
180838      nLeafEst = sqlite3_column_int(pLeafEst, 0);
180839    }
180840    rc = sqlite3_reset(pLeafEst);
180841  }
180842  if( rc!=SQLITE_OK ) return rc;
180843
180844  /* Calculate the first block to use in the output segment */
180845  rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pFirstBlock, 0);
180846  if( rc==SQLITE_OK ){
180847    if( SQLITE_ROW==sqlite3_step(pFirstBlock) ){
180848      pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
180849      pWriter->iEnd = pWriter->iStart - 1;
180850      pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
180851    }
180852    rc = sqlite3_reset(pFirstBlock);
180853  }
180854  if( rc!=SQLITE_OK ) return rc;
180855
180856  /* Insert the marker in the %_segments table to make sure nobody tries
180857  ** to steal the space just allocated. This is also used to identify
180858  ** appendable segments.  */
180859  rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
180860  if( rc!=SQLITE_OK ) return rc;
180861
180862  pWriter->iAbsLevel = iAbsLevel;
180863  pWriter->nLeafEst = nLeafEst;
180864  pWriter->iIdx = iIdx;
180865
180866  /* Set up the array of NodeWriter objects */
180867  for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
180868    pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
180869  }
180870  return SQLITE_OK;
180871}
180872
180873/*
180874** Remove an entry from the %_segdir table. This involves running the
180875** following two statements:
180876**
180877**   DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
180878**   UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
180879**
180880** The DELETE statement removes the specific %_segdir level. The UPDATE
180881** statement ensures that the remaining segments have contiguously allocated
180882** idx values.
180883*/
180884static int fts3RemoveSegdirEntry(
180885  Fts3Table *p,                   /* FTS3 table handle */
180886  sqlite3_int64 iAbsLevel,        /* Absolute level to delete from */
180887  int iIdx                        /* Index of %_segdir entry to delete */
180888){
180889  int rc;                         /* Return code */
180890  sqlite3_stmt *pDelete = 0;      /* DELETE statement */
180891
180892  rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, &pDelete, 0);
180893  if( rc==SQLITE_OK ){
180894    sqlite3_bind_int64(pDelete, 1, iAbsLevel);
180895    sqlite3_bind_int(pDelete, 2, iIdx);
180896    sqlite3_step(pDelete);
180897    rc = sqlite3_reset(pDelete);
180898  }
180899
180900  return rc;
180901}
180902
180903/*
180904** One or more segments have just been removed from absolute level iAbsLevel.
180905** Update the 'idx' values of the remaining segments in the level so that
180906** the idx values are a contiguous sequence starting from 0.
180907*/
180908static int fts3RepackSegdirLevel(
180909  Fts3Table *p,                   /* FTS3 table handle */
180910  sqlite3_int64 iAbsLevel         /* Absolute level to repack */
180911){
180912  int rc;                         /* Return code */
180913  int *aIdx = 0;                  /* Array of remaining idx values */
180914  int nIdx = 0;                   /* Valid entries in aIdx[] */
180915  int nAlloc = 0;                 /* Allocated size of aIdx[] */
180916  int i;                          /* Iterator variable */
180917  sqlite3_stmt *pSelect = 0;      /* Select statement to read idx values */
180918  sqlite3_stmt *pUpdate = 0;      /* Update statement to modify idx values */
180919
180920  rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, &pSelect, 0);
180921  if( rc==SQLITE_OK ){
180922    int rc2;
180923    sqlite3_bind_int64(pSelect, 1, iAbsLevel);
180924    while( SQLITE_ROW==sqlite3_step(pSelect) ){
180925      if( nIdx>=nAlloc ){
180926        int *aNew;
180927        nAlloc += 16;
180928        aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
180929        if( !aNew ){
180930          rc = SQLITE_NOMEM;
180931          break;
180932        }
180933        aIdx = aNew;
180934      }
180935      aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
180936    }
180937    rc2 = sqlite3_reset(pSelect);
180938    if( rc==SQLITE_OK ) rc = rc2;
180939  }
180940
180941  if( rc==SQLITE_OK ){
180942    rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, &pUpdate, 0);
180943  }
180944  if( rc==SQLITE_OK ){
180945    sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
180946  }
180947
180948  assert( p->bIgnoreSavepoint==0 );
180949  p->bIgnoreSavepoint = 1;
180950  for(i=0; rc==SQLITE_OK && i<nIdx; i++){
180951    if( aIdx[i]!=i ){
180952      sqlite3_bind_int(pUpdate, 3, aIdx[i]);
180953      sqlite3_bind_int(pUpdate, 1, i);
180954      sqlite3_step(pUpdate);
180955      rc = sqlite3_reset(pUpdate);
180956    }
180957  }
180958  p->bIgnoreSavepoint = 0;
180959
180960  sqlite3_free(aIdx);
180961  return rc;
180962}
180963
180964static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
180965  pNode->a[0] = (char)iHeight;
180966  if( iChild ){
180967    assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
180968    pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
180969  }else{
180970    assert( pNode->nAlloc>=1 );
180971    pNode->n = 1;
180972  }
180973}
180974
180975/*
180976** The first two arguments are a pointer to and the size of a segment b-tree
180977** node. The node may be a leaf or an internal node.
180978**
180979** This function creates a new node image in blob object *pNew by copying
180980** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
180981** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
180982*/
180983static int fts3TruncateNode(
180984  const char *aNode,              /* Current node image */
180985  int nNode,                      /* Size of aNode in bytes */
180986  Blob *pNew,                     /* OUT: Write new node image here */
180987  const char *zTerm,              /* Omit all terms smaller than this */
180988  int nTerm,                      /* Size of zTerm in bytes */
180989  sqlite3_int64 *piBlock          /* OUT: Block number in next layer down */
180990){
180991  NodeReader reader;              /* Reader object */
180992  Blob prev = {0, 0, 0};          /* Previous term written to new node */
180993  int rc = SQLITE_OK;             /* Return code */
180994  int bLeaf;                       /* True for a leaf node */
180995
180996  if( nNode<1 ) return FTS_CORRUPT_VTAB;
180997  bLeaf = aNode[0]=='\0';
180998
180999  /* Allocate required output space */
181000  blobGrowBuffer(pNew, nNode, &rc);
181001  if( rc!=SQLITE_OK ) return rc;
181002  pNew->n = 0;
181003
181004  /* Populate new node buffer */
181005  for(rc = nodeReaderInit(&reader, aNode, nNode);
181006      rc==SQLITE_OK && reader.aNode;
181007      rc = nodeReaderNext(&reader)
181008  ){
181009    if( pNew->n==0 ){
181010      int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
181011      if( res<0 || (bLeaf==0 && res==0) ) continue;
181012      fts3StartNode(pNew, (int)aNode[0], reader.iChild);
181013      *piBlock = reader.iChild;
181014    }
181015    rc = fts3AppendToNode(
181016        pNew, &prev, reader.term.a, reader.term.n,
181017        reader.aDoclist, reader.nDoclist
181018    );
181019    if( rc!=SQLITE_OK ) break;
181020  }
181021  if( pNew->n==0 ){
181022    fts3StartNode(pNew, (int)aNode[0], reader.iChild);
181023    *piBlock = reader.iChild;
181024  }
181025  assert( pNew->n<=pNew->nAlloc );
181026
181027  nodeReaderRelease(&reader);
181028  sqlite3_free(prev.a);
181029  return rc;
181030}
181031
181032/*
181033** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute
181034** level iAbsLevel. This may involve deleting entries from the %_segments
181035** table, and modifying existing entries in both the %_segments and %_segdir
181036** tables.
181037**
181038** SQLITE_OK is returned if the segment is updated successfully. Or an
181039** SQLite error code otherwise.
181040*/
181041static int fts3TruncateSegment(
181042  Fts3Table *p,                   /* FTS3 table handle */
181043  sqlite3_int64 iAbsLevel,        /* Absolute level of segment to modify */
181044  int iIdx,                       /* Index within level of segment to modify */
181045  const char *zTerm,              /* Remove terms smaller than this */
181046  int nTerm                      /* Number of bytes in buffer zTerm */
181047){
181048  int rc = SQLITE_OK;             /* Return code */
181049  Blob root = {0,0,0};            /* New root page image */
181050  Blob block = {0,0,0};           /* Buffer used for any other block */
181051  sqlite3_int64 iBlock = 0;       /* Block id */
181052  sqlite3_int64 iNewStart = 0;    /* New value for iStartBlock */
181053  sqlite3_int64 iOldStart = 0;    /* Old value for iStartBlock */
181054  sqlite3_stmt *pFetch = 0;       /* Statement used to fetch segdir */
181055
181056  rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pFetch, 0);
181057  if( rc==SQLITE_OK ){
181058    int rc2;                      /* sqlite3_reset() return code */
181059    sqlite3_bind_int64(pFetch, 1, iAbsLevel);
181060    sqlite3_bind_int(pFetch, 2, iIdx);
181061    if( SQLITE_ROW==sqlite3_step(pFetch) ){
181062      const char *aRoot = sqlite3_column_blob(pFetch, 4);
181063      int nRoot = sqlite3_column_bytes(pFetch, 4);
181064      iOldStart = sqlite3_column_int64(pFetch, 1);
181065      rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
181066    }
181067    rc2 = sqlite3_reset(pFetch);
181068    if( rc==SQLITE_OK ) rc = rc2;
181069  }
181070
181071  while( rc==SQLITE_OK && iBlock ){
181072    char *aBlock = 0;
181073    int nBlock = 0;
181074    iNewStart = iBlock;
181075
181076    rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
181077    if( rc==SQLITE_OK ){
181078      rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
181079    }
181080    if( rc==SQLITE_OK ){
181081      rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
181082    }
181083    sqlite3_free(aBlock);
181084  }
181085
181086  /* Variable iNewStart now contains the first valid leaf node. */
181087  if( rc==SQLITE_OK && iNewStart ){
181088    sqlite3_stmt *pDel = 0;
181089    rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDel, 0);
181090    if( rc==SQLITE_OK ){
181091      sqlite3_bind_int64(pDel, 1, iOldStart);
181092      sqlite3_bind_int64(pDel, 2, iNewStart-1);
181093      sqlite3_step(pDel);
181094      rc = sqlite3_reset(pDel);
181095    }
181096  }
181097
181098  if( rc==SQLITE_OK ){
181099    sqlite3_stmt *pChomp = 0;
181100    rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, &pChomp, 0);
181101    if( rc==SQLITE_OK ){
181102      sqlite3_bind_int64(pChomp, 1, iNewStart);
181103      sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
181104      sqlite3_bind_int64(pChomp, 3, iAbsLevel);
181105      sqlite3_bind_int(pChomp, 4, iIdx);
181106      sqlite3_step(pChomp);
181107      rc = sqlite3_reset(pChomp);
181108      sqlite3_bind_null(pChomp, 2);
181109    }
181110  }
181111
181112  sqlite3_free(root.a);
181113  sqlite3_free(block.a);
181114  return rc;
181115}
181116
181117/*
181118** This function is called after an incrmental-merge operation has run to
181119** merge (or partially merge) two or more segments from absolute level
181120** iAbsLevel.
181121**
181122** Each input segment is either removed from the db completely (if all of
181123** its data was copied to the output segment by the incrmerge operation)
181124** or modified in place so that it no longer contains those entries that
181125** have been duplicated in the output segment.
181126*/
181127static int fts3IncrmergeChomp(
181128  Fts3Table *p,                   /* FTS table handle */
181129  sqlite3_int64 iAbsLevel,        /* Absolute level containing segments */
181130  Fts3MultiSegReader *pCsr,       /* Chomp all segments opened by this cursor */
181131  int *pnRem                      /* Number of segments not deleted */
181132){
181133  int i;
181134  int nRem = 0;
181135  int rc = SQLITE_OK;
181136
181137  for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
181138    Fts3SegReader *pSeg = 0;
181139    int j;
181140
181141    /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
181142    ** somewhere in the pCsr->apSegment[] array.  */
181143    for(j=0; ALWAYS(j<pCsr->nSegment); j++){
181144      pSeg = pCsr->apSegment[j];
181145      if( pSeg->iIdx==i ) break;
181146    }
181147    assert( j<pCsr->nSegment && pSeg->iIdx==i );
181148
181149    if( pSeg->aNode==0 ){
181150      /* Seg-reader is at EOF. Remove the entire input segment. */
181151      rc = fts3DeleteSegment(p, pSeg);
181152      if( rc==SQLITE_OK ){
181153        rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
181154      }
181155      *pnRem = 0;
181156    }else{
181157      /* The incremental merge did not copy all the data from this
181158      ** segment to the upper level. The segment is modified in place
181159      ** so that it contains no keys smaller than zTerm/nTerm. */
181160      const char *zTerm = pSeg->zTerm;
181161      int nTerm = pSeg->nTerm;
181162      rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
181163      nRem++;
181164    }
181165  }
181166
181167  if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
181168    rc = fts3RepackSegdirLevel(p, iAbsLevel);
181169  }
181170
181171  *pnRem = nRem;
181172  return rc;
181173}
181174
181175/*
181176** Store an incr-merge hint in the database.
181177*/
181178static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
181179  sqlite3_stmt *pReplace = 0;
181180  int rc;                         /* Return code */
181181
181182  rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
181183  if( rc==SQLITE_OK ){
181184    sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
181185    sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
181186    sqlite3_step(pReplace);
181187    rc = sqlite3_reset(pReplace);
181188    sqlite3_bind_null(pReplace, 2);
181189  }
181190
181191  return rc;
181192}
181193
181194/*
181195** Load an incr-merge hint from the database. The incr-merge hint, if one
181196** exists, is stored in the rowid==1 row of the %_stat table.
181197**
181198** If successful, populate blob *pHint with the value read from the %_stat
181199** table and return SQLITE_OK. Otherwise, if an error occurs, return an
181200** SQLite error code.
181201*/
181202static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
181203  sqlite3_stmt *pSelect = 0;
181204  int rc;
181205
181206  pHint->n = 0;
181207  rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pSelect, 0);
181208  if( rc==SQLITE_OK ){
181209    int rc2;
181210    sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT);
181211    if( SQLITE_ROW==sqlite3_step(pSelect) ){
181212      const char *aHint = sqlite3_column_blob(pSelect, 0);
181213      int nHint = sqlite3_column_bytes(pSelect, 0);
181214      if( aHint ){
181215        blobGrowBuffer(pHint, nHint, &rc);
181216        if( rc==SQLITE_OK ){
181217          memcpy(pHint->a, aHint, nHint);
181218          pHint->n = nHint;
181219        }
181220      }
181221    }
181222    rc2 = sqlite3_reset(pSelect);
181223    if( rc==SQLITE_OK ) rc = rc2;
181224  }
181225
181226  return rc;
181227}
181228
181229/*
181230** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
181231** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
181232** consists of two varints, the absolute level number of the input segments
181233** and the number of input segments.
181234**
181235** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
181236** set *pRc to an SQLite error code before returning.
181237*/
181238static void fts3IncrmergeHintPush(
181239  Blob *pHint,                    /* Hint blob to append to */
181240  i64 iAbsLevel,                  /* First varint to store in hint */
181241  int nInput,                     /* Second varint to store in hint */
181242  int *pRc                        /* IN/OUT: Error code */
181243){
181244  blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
181245  if( *pRc==SQLITE_OK ){
181246    pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
181247    pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
181248  }
181249}
181250
181251/*
181252** Read the last entry (most recently pushed) from the hint blob *pHint
181253** and then remove the entry. Write the two values read to *piAbsLevel and
181254** *pnInput before returning.
181255**
181256** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
181257** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
181258*/
181259static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
181260  const int nHint = pHint->n;
181261  int i;
181262
181263  i = pHint->n-1;
181264  if( (pHint->a[i] & 0x80) ) return FTS_CORRUPT_VTAB;
181265  while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
181266  if( i==0 ) return FTS_CORRUPT_VTAB;
181267  i--;
181268  while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
181269
181270  pHint->n = i;
181271  i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
181272  i += fts3GetVarint32(&pHint->a[i], pnInput);
181273  assert( i<=nHint );
181274  if( i!=nHint ) return FTS_CORRUPT_VTAB;
181275
181276  return SQLITE_OK;
181277}
181278
181279
181280/*
181281** Attempt an incremental merge that writes nMerge leaf blocks.
181282**
181283** Incremental merges happen nMin segments at a time. The segments
181284** to be merged are the nMin oldest segments (the ones with the smallest
181285** values for the _segdir.idx field) in the highest level that contains
181286** at least nMin segments. Multiple merges might occur in an attempt to
181287** write the quota of nMerge leaf blocks.
181288*/
181289SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
181290  int rc;                         /* Return code */
181291  int nRem = nMerge;              /* Number of leaf pages yet to  be written */
181292  Fts3MultiSegReader *pCsr;       /* Cursor used to read input data */
181293  Fts3SegFilter *pFilter;         /* Filter used with cursor pCsr */
181294  IncrmergeWriter *pWriter;       /* Writer object */
181295  int nSeg = 0;                   /* Number of input segments */
181296  sqlite3_int64 iAbsLevel = 0;    /* Absolute level number to work on */
181297  Blob hint = {0, 0, 0};          /* Hint read from %_stat table */
181298  int bDirtyHint = 0;             /* True if blob 'hint' has been modified */
181299
181300  /* Allocate space for the cursor, filter and writer objects */
181301  const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
181302  pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
181303  if( !pWriter ) return SQLITE_NOMEM;
181304  pFilter = (Fts3SegFilter *)&pWriter[1];
181305  pCsr = (Fts3MultiSegReader *)&pFilter[1];
181306
181307  rc = fts3IncrmergeHintLoad(p, &hint);
181308  while( rc==SQLITE_OK && nRem>0 ){
181309    const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
181310    sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
181311    int bUseHint = 0;             /* True if attempting to append */
181312    int iIdx = 0;                 /* Largest idx in level (iAbsLevel+1) */
181313
181314    /* Search the %_segdir table for the absolute level with the smallest
181315    ** relative level number that contains at least nMin segments, if any.
181316    ** If one is found, set iAbsLevel to the absolute level number and
181317    ** nSeg to nMin. If no level with at least nMin segments can be found,
181318    ** set nSeg to -1.
181319    */
181320    rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0);
181321    sqlite3_bind_int(pFindLevel, 1, MAX(2, nMin));
181322    if( sqlite3_step(pFindLevel)==SQLITE_ROW ){
181323      iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
181324      nSeg = sqlite3_column_int(pFindLevel, 1);
181325      assert( nSeg>=2 );
181326    }else{
181327      nSeg = -1;
181328    }
181329    rc = sqlite3_reset(pFindLevel);
181330
181331    /* If the hint read from the %_stat table is not empty, check if the
181332    ** last entry in it specifies a relative level smaller than or equal
181333    ** to the level identified by the block above (if any). If so, this
181334    ** iteration of the loop will work on merging at the hinted level.
181335    */
181336    if( rc==SQLITE_OK && hint.n ){
181337      int nHint = hint.n;
181338      sqlite3_int64 iHintAbsLevel = 0;      /* Hint level */
181339      int nHintSeg = 0;                     /* Hint number of segments */
181340
181341      rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
181342      if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
181343        /* Based on the scan in the block above, it is known that there
181344        ** are no levels with a relative level smaller than that of
181345        ** iAbsLevel with more than nSeg segments, or if nSeg is -1,
181346        ** no levels with more than nMin segments. Use this to limit the
181347        ** value of nHintSeg to avoid a large memory allocation in case the
181348        ** merge-hint is corrupt*/
181349        iAbsLevel = iHintAbsLevel;
181350        nSeg = MIN(MAX(nMin,nSeg), nHintSeg);
181351        bUseHint = 1;
181352        bDirtyHint = 1;
181353      }else{
181354        /* This undoes the effect of the HintPop() above - so that no entry
181355        ** is removed from the hint blob.  */
181356        hint.n = nHint;
181357      }
181358    }
181359
181360    /* If nSeg is less that zero, then there is no level with at least
181361    ** nMin segments and no hint in the %_stat table. No work to do.
181362    ** Exit early in this case.  */
181363    if( nSeg<=0 ) break;
181364
181365    assert( nMod<=0x7FFFFFFF );
181366    if( iAbsLevel<0 || iAbsLevel>(nMod<<32) ){
181367      rc = FTS_CORRUPT_VTAB;
181368      break;
181369    }
181370
181371    /* Open a cursor to iterate through the contents of the oldest nSeg
181372    ** indexes of absolute level iAbsLevel. If this cursor is opened using
181373    ** the 'hint' parameters, it is possible that there are less than nSeg
181374    ** segments available in level iAbsLevel. In this case, no work is
181375    ** done on iAbsLevel - fall through to the next iteration of the loop
181376    ** to start work on some other level.  */
181377    memset(pWriter, 0, nAlloc);
181378    pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
181379
181380    if( rc==SQLITE_OK ){
181381      rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
181382      assert( bUseHint==1 || bUseHint==0 );
181383      if( iIdx==0 || (bUseHint && iIdx==1) ){
181384        int bIgnore = 0;
181385        rc = fts3SegmentIsMaxLevel(p, iAbsLevel+1, &bIgnore);
181386        if( bIgnore ){
181387          pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY;
181388        }
181389      }
181390    }
181391
181392    if( rc==SQLITE_OK ){
181393      rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
181394    }
181395    if( SQLITE_OK==rc && pCsr->nSegment==nSeg
181396     && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
181397    ){
181398      int bEmpty = 0;
181399      rc = sqlite3Fts3SegReaderStep(p, pCsr);
181400      if( rc==SQLITE_OK ){
181401        bEmpty = 1;
181402      }else if( rc!=SQLITE_ROW ){
181403        sqlite3Fts3SegReaderFinish(pCsr);
181404        break;
181405      }
181406      if( bUseHint && iIdx>0 ){
181407        const char *zKey = pCsr->zTerm;
181408        int nKey = pCsr->nTerm;
181409        rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
181410      }else{
181411        rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
181412      }
181413
181414      if( rc==SQLITE_OK && pWriter->nLeafEst ){
181415        fts3LogMerge(nSeg, iAbsLevel);
181416        if( bEmpty==0 ){
181417          do {
181418            rc = fts3IncrmergeAppend(p, pWriter, pCsr);
181419            if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
181420            if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
181421          }while( rc==SQLITE_ROW );
181422        }
181423
181424        /* Update or delete the input segments */
181425        if( rc==SQLITE_OK ){
181426          nRem -= (1 + pWriter->nWork);
181427          rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
181428          if( nSeg!=0 ){
181429            bDirtyHint = 1;
181430            fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
181431          }
181432        }
181433      }
181434
181435      if( nSeg!=0 ){
181436        pWriter->nLeafData = pWriter->nLeafData * -1;
181437      }
181438      fts3IncrmergeRelease(p, pWriter, &rc);
181439      if( nSeg==0 && pWriter->bNoLeafData==0 ){
181440        fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData);
181441      }
181442    }
181443
181444    sqlite3Fts3SegReaderFinish(pCsr);
181445  }
181446
181447  /* Write the hint values into the %_stat table for the next incr-merger */
181448  if( bDirtyHint && rc==SQLITE_OK ){
181449    rc = fts3IncrmergeHintStore(p, &hint);
181450  }
181451
181452  sqlite3_free(pWriter);
181453  sqlite3_free(hint.a);
181454  return rc;
181455}
181456
181457/*
181458** Convert the text beginning at *pz into an integer and return
181459** its value.  Advance *pz to point to the first character past
181460** the integer.
181461**
181462** This function used for parameters to merge= and incrmerge=
181463** commands.
181464*/
181465static int fts3Getint(const char **pz){
181466  const char *z = *pz;
181467  int i = 0;
181468  while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
181469  *pz = z;
181470  return i;
181471}
181472
181473/*
181474** Process statements of the form:
181475**
181476**    INSERT INTO table(table) VALUES('merge=A,B');
181477**
181478** A and B are integers that decode to be the number of leaf pages
181479** written for the merge, and the minimum number of segments on a level
181480** before it will be selected for a merge, respectively.
181481*/
181482static int fts3DoIncrmerge(
181483  Fts3Table *p,                   /* FTS3 table handle */
181484  const char *zParam              /* Nul-terminated string containing "A,B" */
181485){
181486  int rc;
181487  int nMin = (MergeCount(p) / 2);
181488  int nMerge = 0;
181489  const char *z = zParam;
181490
181491  /* Read the first integer value */
181492  nMerge = fts3Getint(&z);
181493
181494  /* If the first integer value is followed by a ',',  read the second
181495  ** integer value. */
181496  if( z[0]==',' && z[1]!='\0' ){
181497    z++;
181498    nMin = fts3Getint(&z);
181499  }
181500
181501  if( z[0]!='\0' || nMin<2 ){
181502    rc = SQLITE_ERROR;
181503  }else{
181504    rc = SQLITE_OK;
181505    if( !p->bHasStat ){
181506      assert( p->bFts4==0 );
181507      sqlite3Fts3CreateStatTable(&rc, p);
181508    }
181509    if( rc==SQLITE_OK ){
181510      rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
181511    }
181512    sqlite3Fts3SegmentsClose(p);
181513  }
181514  return rc;
181515}
181516
181517/*
181518** Process statements of the form:
181519**
181520**    INSERT INTO table(table) VALUES('automerge=X');
181521**
181522** where X is an integer.  X==0 means to turn automerge off.  X!=0 means
181523** turn it on.  The setting is persistent.
181524*/
181525static int fts3DoAutoincrmerge(
181526  Fts3Table *p,                   /* FTS3 table handle */
181527  const char *zParam              /* Nul-terminated string containing boolean */
181528){
181529  int rc = SQLITE_OK;
181530  sqlite3_stmt *pStmt = 0;
181531  p->nAutoincrmerge = fts3Getint(&zParam);
181532  if( p->nAutoincrmerge==1 || p->nAutoincrmerge>MergeCount(p) ){
181533    p->nAutoincrmerge = 8;
181534  }
181535  if( !p->bHasStat ){
181536    assert( p->bFts4==0 );
181537    sqlite3Fts3CreateStatTable(&rc, p);
181538    if( rc ) return rc;
181539  }
181540  rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
181541  if( rc ) return rc;
181542  sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
181543  sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge);
181544  sqlite3_step(pStmt);
181545  rc = sqlite3_reset(pStmt);
181546  return rc;
181547}
181548
181549/*
181550** Return a 64-bit checksum for the FTS index entry specified by the
181551** arguments to this function.
181552*/
181553static u64 fts3ChecksumEntry(
181554  const char *zTerm,              /* Pointer to buffer containing term */
181555  int nTerm,                      /* Size of zTerm in bytes */
181556  int iLangid,                    /* Language id for current row */
181557  int iIndex,                     /* Index (0..Fts3Table.nIndex-1) */
181558  i64 iDocid,                     /* Docid for current row. */
181559  int iCol,                       /* Column number */
181560  int iPos                        /* Position */
181561){
181562  int i;
181563  u64 ret = (u64)iDocid;
181564
181565  ret += (ret<<3) + iLangid;
181566  ret += (ret<<3) + iIndex;
181567  ret += (ret<<3) + iCol;
181568  ret += (ret<<3) + iPos;
181569  for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
181570
181571  return ret;
181572}
181573
181574/*
181575** Return a checksum of all entries in the FTS index that correspond to
181576** language id iLangid. The checksum is calculated by XORing the checksums
181577** of each individual entry (see fts3ChecksumEntry()) together.
181578**
181579** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
181580** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
181581** return value is undefined in this case.
181582*/
181583static u64 fts3ChecksumIndex(
181584  Fts3Table *p,                   /* FTS3 table handle */
181585  int iLangid,                    /* Language id to return cksum for */
181586  int iIndex,                     /* Index to cksum (0..p->nIndex-1) */
181587  int *pRc                        /* OUT: Return code */
181588){
181589  Fts3SegFilter filter;
181590  Fts3MultiSegReader csr;
181591  int rc;
181592  u64 cksum = 0;
181593
181594  assert( *pRc==SQLITE_OK );
181595
181596  memset(&filter, 0, sizeof(filter));
181597  memset(&csr, 0, sizeof(csr));
181598  filter.flags =  FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
181599  filter.flags |= FTS3_SEGMENT_SCAN;
181600
181601  rc = sqlite3Fts3SegReaderCursor(
181602      p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, 0, 0, 0, 1,&csr
181603  );
181604  if( rc==SQLITE_OK ){
181605    rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
181606  }
181607
181608  if( rc==SQLITE_OK ){
181609    while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
181610      char *pCsr = csr.aDoclist;
181611      char *pEnd = &pCsr[csr.nDoclist];
181612
181613      i64 iDocid = 0;
181614      i64 iCol = 0;
181615      u64 iPos = 0;
181616
181617      pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
181618      while( pCsr<pEnd ){
181619        u64 iVal = 0;
181620        pCsr += sqlite3Fts3GetVarintU(pCsr, &iVal);
181621        if( pCsr<pEnd ){
181622          if( iVal==0 || iVal==1 ){
181623            iCol = 0;
181624            iPos = 0;
181625            if( iVal ){
181626              pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
181627            }else{
181628              pCsr += sqlite3Fts3GetVarintU(pCsr, &iVal);
181629              if( p->bDescIdx ){
181630                iDocid = (i64)((u64)iDocid - iVal);
181631              }else{
181632                iDocid = (i64)((u64)iDocid + iVal);
181633              }
181634            }
181635          }else{
181636            iPos += (iVal - 2);
181637            cksum = cksum ^ fts3ChecksumEntry(
181638                csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
181639                (int)iCol, (int)iPos
181640            );
181641          }
181642        }
181643      }
181644    }
181645  }
181646  sqlite3Fts3SegReaderFinish(&csr);
181647
181648  *pRc = rc;
181649  return cksum;
181650}
181651
181652/*
181653** Check if the contents of the FTS index match the current contents of the
181654** content table. If no error occurs and the contents do match, set *pbOk
181655** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
181656** to false before returning.
181657**
181658** If an error occurs (e.g. an OOM or IO error), return an SQLite error
181659** code. The final value of *pbOk is undefined in this case.
181660*/
181661static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
181662  int rc = SQLITE_OK;             /* Return code */
181663  u64 cksum1 = 0;                 /* Checksum based on FTS index contents */
181664  u64 cksum2 = 0;                 /* Checksum based on %_content contents */
181665  sqlite3_stmt *pAllLangid = 0;   /* Statement to return all language-ids */
181666
181667  /* This block calculates the checksum according to the FTS index. */
181668  rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
181669  if( rc==SQLITE_OK ){
181670    int rc2;
181671    sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
181672    sqlite3_bind_int(pAllLangid, 2, p->nIndex);
181673    while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){
181674      int iLangid = sqlite3_column_int(pAllLangid, 0);
181675      int i;
181676      for(i=0; i<p->nIndex; i++){
181677        cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
181678      }
181679    }
181680    rc2 = sqlite3_reset(pAllLangid);
181681    if( rc==SQLITE_OK ) rc = rc2;
181682  }
181683
181684  /* This block calculates the checksum according to the %_content table */
181685  if( rc==SQLITE_OK ){
181686    sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
181687    sqlite3_stmt *pStmt = 0;
181688    char *zSql;
181689
181690    zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
181691    if( !zSql ){
181692      rc = SQLITE_NOMEM;
181693    }else{
181694      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
181695      sqlite3_free(zSql);
181696    }
181697
181698    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
181699      i64 iDocid = sqlite3_column_int64(pStmt, 0);
181700      int iLang = langidFromSelect(p, pStmt);
181701      int iCol;
181702
181703      for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
181704        if( p->abNotindexed[iCol]==0 ){
181705          const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
181706          sqlite3_tokenizer_cursor *pT = 0;
181707
181708          rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, -1, &pT);
181709          while( rc==SQLITE_OK ){
181710            char const *zToken;       /* Buffer containing token */
181711            int nToken = 0;           /* Number of bytes in token */
181712            int iDum1 = 0, iDum2 = 0; /* Dummy variables */
181713            int iPos = 0;             /* Position of token in zText */
181714
181715            rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
181716            if( rc==SQLITE_OK ){
181717              int i;
181718              cksum2 = cksum2 ^ fts3ChecksumEntry(
181719                  zToken, nToken, iLang, 0, iDocid, iCol, iPos
181720              );
181721              for(i=1; i<p->nIndex; i++){
181722                if( p->aIndex[i].nPrefix<=nToken ){
181723                  cksum2 = cksum2 ^ fts3ChecksumEntry(
181724                      zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
181725                  );
181726                }
181727              }
181728            }
181729          }
181730          if( pT ) pModule->xClose(pT);
181731          if( rc==SQLITE_DONE ) rc = SQLITE_OK;
181732        }
181733      }
181734    }
181735
181736    sqlite3_finalize(pStmt);
181737  }
181738
181739  *pbOk = (cksum1==cksum2);
181740  return rc;
181741}
181742
181743/*
181744** Run the integrity-check. If no error occurs and the current contents of
181745** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
181746** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
181747**
181748** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite
181749** error code.
181750**
181751** The integrity-check works as follows. For each token and indexed token
181752** prefix in the document set, a 64-bit checksum is calculated (by code
181753** in fts3ChecksumEntry()) based on the following:
181754**
181755**     + The index number (0 for the main index, 1 for the first prefix
181756**       index etc.),
181757**     + The token (or token prefix) text itself,
181758**     + The language-id of the row it appears in,
181759**     + The docid of the row it appears in,
181760**     + The column it appears in, and
181761**     + The tokens position within that column.
181762**
181763** The checksums for all entries in the index are XORed together to create
181764** a single checksum for the entire index.
181765**
181766** The integrity-check code calculates the same checksum in two ways:
181767**
181768**     1. By scanning the contents of the FTS index, and
181769**     2. By scanning and tokenizing the content table.
181770**
181771** If the two checksums are identical, the integrity-check is deemed to have
181772** passed.
181773*/
181774static int fts3DoIntegrityCheck(
181775  Fts3Table *p                    /* FTS3 table handle */
181776){
181777  int rc;
181778  int bOk = 0;
181779  rc = fts3IntegrityCheck(p, &bOk);
181780  if( rc==SQLITE_OK && bOk==0 ) rc = FTS_CORRUPT_VTAB;
181781  return rc;
181782}
181783
181784/*
181785** Handle a 'special' INSERT of the form:
181786**
181787**   "INSERT INTO tbl(tbl) VALUES(<expr>)"
181788**
181789** Argument pVal contains the result of <expr>. Currently the only
181790** meaningful value to insert is the text 'optimize'.
181791*/
181792static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
181793  int rc = SQLITE_ERROR;           /* Return Code */
181794  const char *zVal = (const char *)sqlite3_value_text(pVal);
181795  int nVal = sqlite3_value_bytes(pVal);
181796
181797  if( !zVal ){
181798    return SQLITE_NOMEM;
181799  }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
181800    rc = fts3DoOptimize(p, 0);
181801  }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
181802    rc = fts3DoRebuild(p);
181803  }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
181804    rc = fts3DoIntegrityCheck(p);
181805  }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
181806    rc = fts3DoIncrmerge(p, &zVal[6]);
181807  }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
181808    rc = fts3DoAutoincrmerge(p, &zVal[10]);
181809#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
181810  }else{
181811    int v;
181812    if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
181813      v = atoi(&zVal[9]);
181814      if( v>=24 && v<=p->nPgsz-35 ) p->nNodeSize = v;
181815      rc = SQLITE_OK;
181816    }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
181817      v = atoi(&zVal[11]);
181818      if( v>=64 && v<=FTS3_MAX_PENDING_DATA ) p->nMaxPendingData = v;
181819      rc = SQLITE_OK;
181820    }else if( nVal>21 && 0==sqlite3_strnicmp(zVal,"test-no-incr-doclist=",21) ){
181821      p->bNoIncrDoclist = atoi(&zVal[21]);
181822      rc = SQLITE_OK;
181823    }else if( nVal>11 && 0==sqlite3_strnicmp(zVal,"mergecount=",11) ){
181824      v = atoi(&zVal[11]);
181825      if( v>=4 && v<=FTS3_MERGE_COUNT && (v&1)==0 ) p->nMergeCount = v;
181826      rc = SQLITE_OK;
181827    }
181828#endif
181829  }
181830  return rc;
181831}
181832
181833#ifndef SQLITE_DISABLE_FTS4_DEFERRED
181834/*
181835** Delete all cached deferred doclists. Deferred doclists are cached
181836** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
181837*/
181838SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
181839  Fts3DeferredToken *pDef;
181840  for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
181841    fts3PendingListDelete(pDef->pList);
181842    pDef->pList = 0;
181843  }
181844}
181845
181846/*
181847** Free all entries in the pCsr->pDeffered list. Entries are added to
181848** this list using sqlite3Fts3DeferToken().
181849*/
181850SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
181851  Fts3DeferredToken *pDef;
181852  Fts3DeferredToken *pNext;
181853  for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
181854    pNext = pDef->pNext;
181855    fts3PendingListDelete(pDef->pList);
181856    sqlite3_free(pDef);
181857  }
181858  pCsr->pDeferred = 0;
181859}
181860
181861/*
181862** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
181863** based on the row that pCsr currently points to.
181864**
181865** A deferred-doclist is like any other doclist with position information
181866** included, except that it only contains entries for a single row of the
181867** table, not for all rows.
181868*/
181869SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
181870  int rc = SQLITE_OK;             /* Return code */
181871  if( pCsr->pDeferred ){
181872    int i;                        /* Used to iterate through table columns */
181873    sqlite3_int64 iDocid;         /* Docid of the row pCsr points to */
181874    Fts3DeferredToken *pDef;      /* Used to iterate through deferred tokens */
181875
181876    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
181877    sqlite3_tokenizer *pT = p->pTokenizer;
181878    sqlite3_tokenizer_module const *pModule = pT->pModule;
181879
181880    assert( pCsr->isRequireSeek==0 );
181881    iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
181882
181883    for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
181884      if( p->abNotindexed[i]==0 ){
181885        const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
181886        sqlite3_tokenizer_cursor *pTC = 0;
181887
181888        rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
181889        while( rc==SQLITE_OK ){
181890          char const *zToken;       /* Buffer containing token */
181891          int nToken = 0;           /* Number of bytes in token */
181892          int iDum1 = 0, iDum2 = 0; /* Dummy variables */
181893          int iPos = 0;             /* Position of token in zText */
181894
181895          rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
181896          for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
181897            Fts3PhraseToken *pPT = pDef->pToken;
181898            if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
181899                && (pPT->bFirst==0 || iPos==0)
181900                && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
181901                && (0==memcmp(zToken, pPT->z, pPT->n))
181902              ){
181903              fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
181904            }
181905          }
181906        }
181907        if( pTC ) pModule->xClose(pTC);
181908        if( rc==SQLITE_DONE ) rc = SQLITE_OK;
181909      }
181910    }
181911
181912    for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
181913      if( pDef->pList ){
181914        rc = fts3PendingListAppendVarint(&pDef->pList, 0);
181915      }
181916    }
181917  }
181918
181919  return rc;
181920}
181921
181922SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
181923  Fts3DeferredToken *p,
181924  char **ppData,
181925  int *pnData
181926){
181927  char *pRet;
181928  int nSkip;
181929  sqlite3_int64 dummy;
181930
181931  *ppData = 0;
181932  *pnData = 0;
181933
181934  if( p->pList==0 ){
181935    return SQLITE_OK;
181936  }
181937
181938  pRet = (char *)sqlite3_malloc(p->pList->nData);
181939  if( !pRet ) return SQLITE_NOMEM;
181940
181941  nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
181942  *pnData = p->pList->nData - nSkip;
181943  *ppData = pRet;
181944
181945  memcpy(pRet, &p->pList->aData[nSkip], *pnData);
181946  return SQLITE_OK;
181947}
181948
181949/*
181950** Add an entry for token pToken to the pCsr->pDeferred list.
181951*/
181952SQLITE_PRIVATE int sqlite3Fts3DeferToken(
181953  Fts3Cursor *pCsr,               /* Fts3 table cursor */
181954  Fts3PhraseToken *pToken,        /* Token to defer */
181955  int iCol                        /* Column that token must appear in (or -1) */
181956){
181957  Fts3DeferredToken *pDeferred;
181958  pDeferred = sqlite3_malloc(sizeof(*pDeferred));
181959  if( !pDeferred ){
181960    return SQLITE_NOMEM;
181961  }
181962  memset(pDeferred, 0, sizeof(*pDeferred));
181963  pDeferred->pToken = pToken;
181964  pDeferred->pNext = pCsr->pDeferred;
181965  pDeferred->iCol = iCol;
181966  pCsr->pDeferred = pDeferred;
181967
181968  assert( pToken->pDeferred==0 );
181969  pToken->pDeferred = pDeferred;
181970
181971  return SQLITE_OK;
181972}
181973#endif
181974
181975/*
181976** SQLite value pRowid contains the rowid of a row that may or may not be
181977** present in the FTS3 table. If it is, delete it and adjust the contents
181978** of subsiduary data structures accordingly.
181979*/
181980static int fts3DeleteByRowid(
181981  Fts3Table *p,
181982  sqlite3_value *pRowid,
181983  int *pnChng,                    /* IN/OUT: Decrement if row is deleted */
181984  u32 *aSzDel
181985){
181986  int rc = SQLITE_OK;             /* Return code */
181987  int bFound = 0;                 /* True if *pRowid really is in the table */
181988
181989  fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
181990  if( bFound && rc==SQLITE_OK ){
181991    int isEmpty = 0;              /* Deleting *pRowid leaves the table empty */
181992    rc = fts3IsEmpty(p, pRowid, &isEmpty);
181993    if( rc==SQLITE_OK ){
181994      if( isEmpty ){
181995        /* Deleting this row means the whole table is empty. In this case
181996        ** delete the contents of all three tables and throw away any
181997        ** data in the pendingTerms hash table.  */
181998        rc = fts3DeleteAll(p, 1);
181999        *pnChng = 0;
182000        memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
182001      }else{
182002        *pnChng = *pnChng - 1;
182003        if( p->zContentTbl==0 ){
182004          fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
182005        }
182006        if( p->bHasDocsize ){
182007          fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
182008        }
182009      }
182010    }
182011  }
182012
182013  return rc;
182014}
182015
182016/*
182017** This function does the work for the xUpdate method of FTS3 virtual
182018** tables. The schema of the virtual table being:
182019**
182020**     CREATE TABLE <table name>(
182021**       <user columns>,
182022**       <table name> HIDDEN,
182023**       docid HIDDEN,
182024**       <langid> HIDDEN
182025**     );
182026**
182027**
182028*/
182029SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
182030  sqlite3_vtab *pVtab,            /* FTS3 vtab object */
182031  int nArg,                       /* Size of argument array */
182032  sqlite3_value **apVal,          /* Array of arguments */
182033  sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
182034){
182035  Fts3Table *p = (Fts3Table *)pVtab;
182036  int rc = SQLITE_OK;             /* Return Code */
182037  u32 *aSzIns = 0;                /* Sizes of inserted documents */
182038  u32 *aSzDel = 0;                /* Sizes of deleted documents */
182039  int nChng = 0;                  /* Net change in number of documents */
182040  int bInsertDone = 0;
182041
182042  /* At this point it must be known if the %_stat table exists or not.
182043  ** So bHasStat may not be 2.  */
182044  assert( p->bHasStat==0 || p->bHasStat==1 );
182045
182046  assert( p->pSegments==0 );
182047  assert(
182048      nArg==1                     /* DELETE operations */
182049   || nArg==(2 + p->nColumn + 3)  /* INSERT or UPDATE operations */
182050  );
182051
182052  /* Check for a "special" INSERT operation. One of the form:
182053  **
182054  **   INSERT INTO xyz(xyz) VALUES('command');
182055  */
182056  if( nArg>1
182057   && sqlite3_value_type(apVal[0])==SQLITE_NULL
182058   && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL
182059  ){
182060    rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
182061    goto update_out;
182062  }
182063
182064  if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
182065    rc = SQLITE_CONSTRAINT;
182066    goto update_out;
182067  }
182068
182069  /* Allocate space to hold the change in document sizes */
182070  aSzDel = sqlite3_malloc64(sizeof(aSzDel[0])*((sqlite3_int64)p->nColumn+1)*2);
182071  if( aSzDel==0 ){
182072    rc = SQLITE_NOMEM;
182073    goto update_out;
182074  }
182075  aSzIns = &aSzDel[p->nColumn+1];
182076  memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
182077
182078  rc = fts3Writelock(p);
182079  if( rc!=SQLITE_OK ) goto update_out;
182080
182081  /* If this is an INSERT operation, or an UPDATE that modifies the rowid
182082  ** value, then this operation requires constraint handling.
182083  **
182084  ** If the on-conflict mode is REPLACE, this means that the existing row
182085  ** should be deleted from the database before inserting the new row. Or,
182086  ** if the on-conflict mode is other than REPLACE, then this method must
182087  ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
182088  ** modify the database file.
182089  */
182090  if( nArg>1 && p->zContentTbl==0 ){
182091    /* Find the value object that holds the new rowid value. */
182092    sqlite3_value *pNewRowid = apVal[3+p->nColumn];
182093    if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
182094      pNewRowid = apVal[1];
182095    }
182096
182097    if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && (
182098        sqlite3_value_type(apVal[0])==SQLITE_NULL
182099     || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
182100    )){
182101      /* The new rowid is not NULL (in this case the rowid will be
182102      ** automatically assigned and there is no chance of a conflict), and
182103      ** the statement is either an INSERT or an UPDATE that modifies the
182104      ** rowid column. So if the conflict mode is REPLACE, then delete any
182105      ** existing row with rowid=pNewRowid.
182106      **
182107      ** Or, if the conflict mode is not REPLACE, insert the new record into
182108      ** the %_content table. If we hit the duplicate rowid constraint (or any
182109      ** other error) while doing so, return immediately.
182110      **
182111      ** This branch may also run if pNewRowid contains a value that cannot
182112      ** be losslessly converted to an integer. In this case, the eventual
182113      ** call to fts3InsertData() (either just below or further on in this
182114      ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
182115      ** invoked, it will delete zero rows (since no row will have
182116      ** docid=$pNewRowid if $pNewRowid is not an integer value).
182117      */
182118      if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
182119        rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
182120      }else{
182121        rc = fts3InsertData(p, apVal, pRowid);
182122        bInsertDone = 1;
182123      }
182124    }
182125  }
182126  if( rc!=SQLITE_OK ){
182127    goto update_out;
182128  }
182129
182130  /* If this is a DELETE or UPDATE operation, remove the old record. */
182131  if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
182132    assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
182133    rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
182134  }
182135
182136  /* If this is an INSERT or UPDATE operation, insert the new record. */
182137  if( nArg>1 && rc==SQLITE_OK ){
182138    int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
182139    if( bInsertDone==0 ){
182140      rc = fts3InsertData(p, apVal, pRowid);
182141      if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
182142        rc = FTS_CORRUPT_VTAB;
182143      }
182144    }
182145    if( rc==SQLITE_OK ){
182146      rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
182147    }
182148    if( rc==SQLITE_OK ){
182149      assert( p->iPrevDocid==*pRowid );
182150      rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
182151    }
182152    if( p->bHasDocsize ){
182153      fts3InsertDocsize(&rc, p, aSzIns);
182154    }
182155    nChng++;
182156  }
182157
182158  if( p->bFts4 ){
182159    fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
182160  }
182161
182162 update_out:
182163  sqlite3_free(aSzDel);
182164  sqlite3Fts3SegmentsClose(p);
182165  return rc;
182166}
182167
182168/*
182169** Flush any data in the pending-terms hash table to disk. If successful,
182170** merge all segments in the database (including the new segment, if
182171** there was any data to flush) into a single segment.
182172*/
182173SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
182174  int rc;
182175  rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
182176  if( rc==SQLITE_OK ){
182177    rc = fts3DoOptimize(p, 1);
182178    if( rc==SQLITE_OK || rc==SQLITE_DONE ){
182179      int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
182180      if( rc2!=SQLITE_OK ) rc = rc2;
182181    }else{
182182      sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
182183      sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
182184    }
182185  }
182186  sqlite3Fts3SegmentsClose(p);
182187  return rc;
182188}
182189
182190#endif
182191
182192/************** End of fts3_write.c ******************************************/
182193/************** Begin file fts3_snippet.c ************************************/
182194/*
182195** 2009 Oct 23
182196**
182197** The author disclaims copyright to this source code.  In place of
182198** a legal notice, here is a blessing:
182199**
182200**    May you do good and not evil.
182201**    May you find forgiveness for yourself and forgive others.
182202**    May you share freely, never taking more than you give.
182203**
182204******************************************************************************
182205*/
182206
182207/* #include "fts3Int.h" */
182208#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
182209
182210/* #include <string.h> */
182211/* #include <assert.h> */
182212
182213/*
182214** Characters that may appear in the second argument to matchinfo().
182215*/
182216#define FTS3_MATCHINFO_NPHRASE   'p'        /* 1 value */
182217#define FTS3_MATCHINFO_NCOL      'c'        /* 1 value */
182218#define FTS3_MATCHINFO_NDOC      'n'        /* 1 value */
182219#define FTS3_MATCHINFO_AVGLENGTH 'a'        /* nCol values */
182220#define FTS3_MATCHINFO_LENGTH    'l'        /* nCol values */
182221#define FTS3_MATCHINFO_LCS       's'        /* nCol values */
182222#define FTS3_MATCHINFO_HITS      'x'        /* 3*nCol*nPhrase values */
182223#define FTS3_MATCHINFO_LHITS     'y'        /* nCol*nPhrase values */
182224#define FTS3_MATCHINFO_LHITS_BM  'b'        /* nCol*nPhrase values */
182225
182226/*
182227** The default value for the second argument to matchinfo().
182228*/
182229#define FTS3_MATCHINFO_DEFAULT   "pcx"
182230
182231
182232/*
182233** Used as an fts3ExprIterate() context when loading phrase doclists to
182234** Fts3Expr.aDoclist[]/nDoclist.
182235*/
182236typedef struct LoadDoclistCtx LoadDoclistCtx;
182237struct LoadDoclistCtx {
182238  Fts3Cursor *pCsr;               /* FTS3 Cursor */
182239  int nPhrase;                    /* Number of phrases seen so far */
182240  int nToken;                     /* Number of tokens seen so far */
182241};
182242
182243/*
182244** The following types are used as part of the implementation of the
182245** fts3BestSnippet() routine.
182246*/
182247typedef struct SnippetIter SnippetIter;
182248typedef struct SnippetPhrase SnippetPhrase;
182249typedef struct SnippetFragment SnippetFragment;
182250
182251struct SnippetIter {
182252  Fts3Cursor *pCsr;               /* Cursor snippet is being generated from */
182253  int iCol;                       /* Extract snippet from this column */
182254  int nSnippet;                   /* Requested snippet length (in tokens) */
182255  int nPhrase;                    /* Number of phrases in query */
182256  SnippetPhrase *aPhrase;         /* Array of size nPhrase */
182257  int iCurrent;                   /* First token of current snippet */
182258};
182259
182260struct SnippetPhrase {
182261  int nToken;                     /* Number of tokens in phrase */
182262  char *pList;                    /* Pointer to start of phrase position list */
182263  int iHead;                      /* Next value in position list */
182264  char *pHead;                    /* Position list data following iHead */
182265  int iTail;                      /* Next value in trailing position list */
182266  char *pTail;                    /* Position list data following iTail */
182267};
182268
182269struct SnippetFragment {
182270  int iCol;                       /* Column snippet is extracted from */
182271  int iPos;                       /* Index of first token in snippet */
182272  u64 covered;                    /* Mask of query phrases covered */
182273  u64 hlmask;                     /* Mask of snippet terms to highlight */
182274};
182275
182276/*
182277** This type is used as an fts3ExprIterate() context object while
182278** accumulating the data returned by the matchinfo() function.
182279*/
182280typedef struct MatchInfo MatchInfo;
182281struct MatchInfo {
182282  Fts3Cursor *pCursor;            /* FTS3 Cursor */
182283  int nCol;                       /* Number of columns in table */
182284  int nPhrase;                    /* Number of matchable phrases in query */
182285  sqlite3_int64 nDoc;             /* Number of docs in database */
182286  char flag;
182287  u32 *aMatchinfo;                /* Pre-allocated buffer */
182288};
182289
182290/*
182291** An instance of this structure is used to manage a pair of buffers, each
182292** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below
182293** for details.
182294*/
182295struct MatchinfoBuffer {
182296  u8 aRef[3];
182297  int nElem;
182298  int bGlobal;                    /* Set if global data is loaded */
182299  char *zMatchinfo;
182300  u32 aMatchinfo[1];
182301};
182302
182303
182304/*
182305** The snippet() and offsets() functions both return text values. An instance
182306** of the following structure is used to accumulate those values while the
182307** functions are running. See fts3StringAppend() for details.
182308*/
182309typedef struct StrBuffer StrBuffer;
182310struct StrBuffer {
182311  char *z;                        /* Pointer to buffer containing string */
182312  int n;                          /* Length of z in bytes (excl. nul-term) */
182313  int nAlloc;                     /* Allocated size of buffer z in bytes */
182314};
182315
182316
182317/*************************************************************************
182318** Start of MatchinfoBuffer code.
182319*/
182320
182321/*
182322** Allocate a two-slot MatchinfoBuffer object.
182323*/
182324static MatchinfoBuffer *fts3MIBufferNew(size_t nElem, const char *zMatchinfo){
182325  MatchinfoBuffer *pRet;
182326  sqlite3_int64 nByte = sizeof(u32) * (2*(sqlite3_int64)nElem + 1)
182327                           + sizeof(MatchinfoBuffer);
182328  sqlite3_int64 nStr = strlen(zMatchinfo);
182329
182330  pRet = sqlite3_malloc64(nByte + nStr+1);
182331  if( pRet ){
182332    memset(pRet, 0, nByte);
182333    pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet;
182334    pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0]
182335                                      + sizeof(u32)*((int)nElem+1);
182336    pRet->nElem = (int)nElem;
182337    pRet->zMatchinfo = ((char*)pRet) + nByte;
182338    memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1);
182339    pRet->aRef[0] = 1;
182340  }
182341
182342  return pRet;
182343}
182344
182345static void fts3MIBufferFree(void *p){
182346  MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
182347
182348  assert( (u32*)p==&pBuf->aMatchinfo[1]
182349       || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2]
182350  );
182351  if( (u32*)p==&pBuf->aMatchinfo[1] ){
182352    pBuf->aRef[1] = 0;
182353  }else{
182354    pBuf->aRef[2] = 0;
182355  }
182356
182357  if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
182358    sqlite3_free(pBuf);
182359  }
182360}
182361
182362static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){
182363  void (*xRet)(void*) = 0;
182364  u32 *aOut = 0;
182365
182366  if( p->aRef[1]==0 ){
182367    p->aRef[1] = 1;
182368    aOut = &p->aMatchinfo[1];
182369    xRet = fts3MIBufferFree;
182370  }
182371  else if( p->aRef[2]==0 ){
182372    p->aRef[2] = 1;
182373    aOut = &p->aMatchinfo[p->nElem+2];
182374    xRet = fts3MIBufferFree;
182375  }else{
182376    aOut = (u32*)sqlite3_malloc64(p->nElem * sizeof(u32));
182377    if( aOut ){
182378      xRet = sqlite3_free;
182379      if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32));
182380    }
182381  }
182382
182383  *paOut = aOut;
182384  return xRet;
182385}
182386
182387static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){
182388  p->bGlobal = 1;
182389  memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32));
182390}
182391
182392/*
182393** Free a MatchinfoBuffer object allocated using fts3MIBufferNew()
182394*/
182395SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){
182396  if( p ){
182397    assert( p->aRef[0]==1 );
182398    p->aRef[0] = 0;
182399    if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
182400      sqlite3_free(p);
182401    }
182402  }
182403}
182404
182405/*
182406** End of MatchinfoBuffer code.
182407*************************************************************************/
182408
182409
182410/*
182411** This function is used to help iterate through a position-list. A position
182412** list is a list of unique integers, sorted from smallest to largest. Each
182413** element of the list is represented by an FTS3 varint that takes the value
182414** of the difference between the current element and the previous one plus
182415** two. For example, to store the position-list:
182416**
182417**     4 9 113
182418**
182419** the three varints:
182420**
182421**     6 7 106
182422**
182423** are encoded.
182424**
182425** When this function is called, *pp points to the start of an element of
182426** the list. *piPos contains the value of the previous entry in the list.
182427** After it returns, *piPos contains the value of the next element of the
182428** list and *pp is advanced to the following varint.
182429*/
182430static void fts3GetDeltaPosition(char **pp, int *piPos){
182431  int iVal;
182432  *pp += fts3GetVarint32(*pp, &iVal);
182433  *piPos += (iVal-2);
182434}
182435
182436/*
182437** Helper function for fts3ExprIterate() (see below).
182438*/
182439static int fts3ExprIterate2(
182440  Fts3Expr *pExpr,                /* Expression to iterate phrases of */
182441  int *piPhrase,                  /* Pointer to phrase counter */
182442  int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
182443  void *pCtx                      /* Second argument to pass to callback */
182444){
182445  int rc;                         /* Return code */
182446  int eType = pExpr->eType;     /* Type of expression node pExpr */
182447
182448  if( eType!=FTSQUERY_PHRASE ){
182449    assert( pExpr->pLeft && pExpr->pRight );
182450    rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
182451    if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
182452      rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
182453    }
182454  }else{
182455    rc = x(pExpr, *piPhrase, pCtx);
182456    (*piPhrase)++;
182457  }
182458  return rc;
182459}
182460
182461/*
182462** Iterate through all phrase nodes in an FTS3 query, except those that
182463** are part of a sub-tree that is the right-hand-side of a NOT operator.
182464** For each phrase node found, the supplied callback function is invoked.
182465**
182466** If the callback function returns anything other than SQLITE_OK,
182467** the iteration is abandoned and the error code returned immediately.
182468** Otherwise, SQLITE_OK is returned after a callback has been made for
182469** all eligible phrase nodes.
182470*/
182471static int fts3ExprIterate(
182472  Fts3Expr *pExpr,                /* Expression to iterate phrases of */
182473  int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
182474  void *pCtx                      /* Second argument to pass to callback */
182475){
182476  int iPhrase = 0;                /* Variable used as the phrase counter */
182477  return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
182478}
182479
182480
182481/*
182482** This is an fts3ExprIterate() callback used while loading the doclists
182483** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
182484** fts3ExprLoadDoclists().
182485*/
182486static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
182487  int rc = SQLITE_OK;
182488  Fts3Phrase *pPhrase = pExpr->pPhrase;
182489  LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
182490
182491  UNUSED_PARAMETER(iPhrase);
182492
182493  p->nPhrase++;
182494  p->nToken += pPhrase->nToken;
182495
182496  return rc;
182497}
182498
182499/*
182500** Load the doclists for each phrase in the query associated with FTS3 cursor
182501** pCsr.
182502**
182503** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable
182504** phrases in the expression (all phrases except those directly or
182505** indirectly descended from the right-hand-side of a NOT operator). If
182506** pnToken is not NULL, then it is set to the number of tokens in all
182507** matchable phrases of the expression.
182508*/
182509static int fts3ExprLoadDoclists(
182510  Fts3Cursor *pCsr,               /* Fts3 cursor for current query */
182511  int *pnPhrase,                  /* OUT: Number of phrases in query */
182512  int *pnToken                    /* OUT: Number of tokens in query */
182513){
182514  int rc;                         /* Return Code */
182515  LoadDoclistCtx sCtx = {0,0,0};  /* Context for fts3ExprIterate() */
182516  sCtx.pCsr = pCsr;
182517  rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
182518  if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
182519  if( pnToken ) *pnToken = sCtx.nToken;
182520  return rc;
182521}
182522
182523static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
182524  (*(int *)ctx)++;
182525  pExpr->iPhrase = iPhrase;
182526  return SQLITE_OK;
182527}
182528static int fts3ExprPhraseCount(Fts3Expr *pExpr){
182529  int nPhrase = 0;
182530  (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
182531  return nPhrase;
182532}
182533
182534/*
182535** Advance the position list iterator specified by the first two
182536** arguments so that it points to the first element with a value greater
182537** than or equal to parameter iNext.
182538*/
182539static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){
182540  char *pIter = *ppIter;
182541  if( pIter ){
182542    int iIter = *piIter;
182543
182544    while( iIter<iNext ){
182545      if( 0==(*pIter & 0xFE) ){
182546        iIter = -1;
182547        pIter = 0;
182548        break;
182549      }
182550      fts3GetDeltaPosition(&pIter, &iIter);
182551    }
182552
182553    *piIter = iIter;
182554    *ppIter = pIter;
182555  }
182556}
182557
182558/*
182559** Advance the snippet iterator to the next candidate snippet.
182560*/
182561static int fts3SnippetNextCandidate(SnippetIter *pIter){
182562  int i;                          /* Loop counter */
182563
182564  if( pIter->iCurrent<0 ){
182565    /* The SnippetIter object has just been initialized. The first snippet
182566    ** candidate always starts at offset 0 (even if this candidate has a
182567    ** score of 0.0).
182568    */
182569    pIter->iCurrent = 0;
182570
182571    /* Advance the 'head' iterator of each phrase to the first offset that
182572    ** is greater than or equal to (iNext+nSnippet).
182573    */
182574    for(i=0; i<pIter->nPhrase; i++){
182575      SnippetPhrase *pPhrase = &pIter->aPhrase[i];
182576      fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
182577    }
182578  }else{
182579    int iStart;
182580    int iEnd = 0x7FFFFFFF;
182581
182582    for(i=0; i<pIter->nPhrase; i++){
182583      SnippetPhrase *pPhrase = &pIter->aPhrase[i];
182584      if( pPhrase->pHead && pPhrase->iHead<iEnd ){
182585        iEnd = pPhrase->iHead;
182586      }
182587    }
182588    if( iEnd==0x7FFFFFFF ){
182589      return 1;
182590    }
182591
182592    pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
182593    for(i=0; i<pIter->nPhrase; i++){
182594      SnippetPhrase *pPhrase = &pIter->aPhrase[i];
182595      fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
182596      fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
182597    }
182598  }
182599
182600  return 0;
182601}
182602
182603/*
182604** Retrieve information about the current candidate snippet of snippet
182605** iterator pIter.
182606*/
182607static void fts3SnippetDetails(
182608  SnippetIter *pIter,             /* Snippet iterator */
182609  u64 mCovered,                   /* Bitmask of phrases already covered */
182610  int *piToken,                   /* OUT: First token of proposed snippet */
182611  int *piScore,                   /* OUT: "Score" for this snippet */
182612  u64 *pmCover,                   /* OUT: Bitmask of phrases covered */
182613  u64 *pmHighlight                /* OUT: Bitmask of terms to highlight */
182614){
182615  int iStart = pIter->iCurrent;   /* First token of snippet */
182616  int iScore = 0;                 /* Score of this snippet */
182617  int i;                          /* Loop counter */
182618  u64 mCover = 0;                 /* Mask of phrases covered by this snippet */
182619  u64 mHighlight = 0;             /* Mask of tokens to highlight in snippet */
182620
182621  for(i=0; i<pIter->nPhrase; i++){
182622    SnippetPhrase *pPhrase = &pIter->aPhrase[i];
182623    if( pPhrase->pTail ){
182624      char *pCsr = pPhrase->pTail;
182625      int iCsr = pPhrase->iTail;
182626
182627      while( iCsr<(iStart+pIter->nSnippet) && iCsr>=iStart ){
182628        int j;
182629        u64 mPhrase = (u64)1 << (i%64);
182630        u64 mPos = (u64)1 << (iCsr - iStart);
182631        assert( iCsr>=iStart && (iCsr - iStart)<=64 );
182632        assert( i>=0 );
182633        if( (mCover|mCovered)&mPhrase ){
182634          iScore++;
182635        }else{
182636          iScore += 1000;
182637        }
182638        mCover |= mPhrase;
182639
182640        for(j=0; j<pPhrase->nToken; j++){
182641          mHighlight |= (mPos>>j);
182642        }
182643
182644        if( 0==(*pCsr & 0x0FE) ) break;
182645        fts3GetDeltaPosition(&pCsr, &iCsr);
182646      }
182647    }
182648  }
182649
182650  /* Set the output variables before returning. */
182651  *piToken = iStart;
182652  *piScore = iScore;
182653  *pmCover = mCover;
182654  *pmHighlight = mHighlight;
182655}
182656
182657/*
182658** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
182659** Each invocation populates an element of the SnippetIter.aPhrase[] array.
182660*/
182661static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
182662  SnippetIter *p = (SnippetIter *)ctx;
182663  SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
182664  char *pCsr;
182665  int rc;
182666
182667  pPhrase->nToken = pExpr->pPhrase->nToken;
182668  rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
182669  assert( rc==SQLITE_OK || pCsr==0 );
182670  if( pCsr ){
182671    int iFirst = 0;
182672    pPhrase->pList = pCsr;
182673    fts3GetDeltaPosition(&pCsr, &iFirst);
182674    if( iFirst<0 ){
182675      rc = FTS_CORRUPT_VTAB;
182676    }else{
182677      pPhrase->pHead = pCsr;
182678      pPhrase->pTail = pCsr;
182679      pPhrase->iHead = iFirst;
182680      pPhrase->iTail = iFirst;
182681    }
182682  }else{
182683    assert( rc!=SQLITE_OK || (
182684       pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0
182685    ));
182686  }
182687
182688  return rc;
182689}
182690
182691/*
182692** Select the fragment of text consisting of nFragment contiguous tokens
182693** from column iCol that represent the "best" snippet. The best snippet
182694** is the snippet with the highest score, where scores are calculated
182695** by adding:
182696**
182697**   (a) +1 point for each occurrence of a matchable phrase in the snippet.
182698**
182699**   (b) +1000 points for the first occurrence of each matchable phrase in
182700**       the snippet for which the corresponding mCovered bit is not set.
182701**
182702** The selected snippet parameters are stored in structure *pFragment before
182703** returning. The score of the selected snippet is stored in *piScore
182704** before returning.
182705*/
182706static int fts3BestSnippet(
182707  int nSnippet,                   /* Desired snippet length */
182708  Fts3Cursor *pCsr,               /* Cursor to create snippet for */
182709  int iCol,                       /* Index of column to create snippet from */
182710  u64 mCovered,                   /* Mask of phrases already covered */
182711  u64 *pmSeen,                    /* IN/OUT: Mask of phrases seen */
182712  SnippetFragment *pFragment,     /* OUT: Best snippet found */
182713  int *piScore                    /* OUT: Score of snippet pFragment */
182714){
182715  int rc;                         /* Return Code */
182716  int nList;                      /* Number of phrases in expression */
182717  SnippetIter sIter;              /* Iterates through snippet candidates */
182718  sqlite3_int64 nByte;            /* Number of bytes of space to allocate */
182719  int iBestScore = -1;            /* Best snippet score found so far */
182720  int i;                          /* Loop counter */
182721
182722  memset(&sIter, 0, sizeof(sIter));
182723
182724  /* Iterate through the phrases in the expression to count them. The same
182725  ** callback makes sure the doclists are loaded for each phrase.
182726  */
182727  rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
182728  if( rc!=SQLITE_OK ){
182729    return rc;
182730  }
182731
182732  /* Now that it is known how many phrases there are, allocate and zero
182733  ** the required space using malloc().
182734  */
182735  nByte = sizeof(SnippetPhrase) * nList;
182736  sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc64(nByte);
182737  if( !sIter.aPhrase ){
182738    return SQLITE_NOMEM;
182739  }
182740  memset(sIter.aPhrase, 0, nByte);
182741
182742  /* Initialize the contents of the SnippetIter object. Then iterate through
182743  ** the set of phrases in the expression to populate the aPhrase[] array.
182744  */
182745  sIter.pCsr = pCsr;
182746  sIter.iCol = iCol;
182747  sIter.nSnippet = nSnippet;
182748  sIter.nPhrase = nList;
182749  sIter.iCurrent = -1;
182750  rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter);
182751  if( rc==SQLITE_OK ){
182752
182753    /* Set the *pmSeen output variable. */
182754    for(i=0; i<nList; i++){
182755      if( sIter.aPhrase[i].pHead ){
182756        *pmSeen |= (u64)1 << (i%64);
182757      }
182758    }
182759
182760    /* Loop through all candidate snippets. Store the best snippet in
182761     ** *pFragment. Store its associated 'score' in iBestScore.
182762     */
182763    pFragment->iCol = iCol;
182764    while( !fts3SnippetNextCandidate(&sIter) ){
182765      int iPos;
182766      int iScore;
182767      u64 mCover;
182768      u64 mHighlite;
182769      fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover,&mHighlite);
182770      assert( iScore>=0 );
182771      if( iScore>iBestScore ){
182772        pFragment->iPos = iPos;
182773        pFragment->hlmask = mHighlite;
182774        pFragment->covered = mCover;
182775        iBestScore = iScore;
182776      }
182777    }
182778
182779    *piScore = iBestScore;
182780  }
182781  sqlite3_free(sIter.aPhrase);
182782  return rc;
182783}
182784
182785
182786/*
182787** Append a string to the string-buffer passed as the first argument.
182788**
182789** If nAppend is negative, then the length of the string zAppend is
182790** determined using strlen().
182791*/
182792static int fts3StringAppend(
182793  StrBuffer *pStr,                /* Buffer to append to */
182794  const char *zAppend,            /* Pointer to data to append to buffer */
182795  int nAppend                     /* Size of zAppend in bytes (or -1) */
182796){
182797  if( nAppend<0 ){
182798    nAppend = (int)strlen(zAppend);
182799  }
182800
182801  /* If there is insufficient space allocated at StrBuffer.z, use realloc()
182802  ** to grow the buffer until so that it is big enough to accomadate the
182803  ** appended data.
182804  */
182805  if( pStr->n+nAppend+1>=pStr->nAlloc ){
182806    sqlite3_int64 nAlloc = pStr->nAlloc+(sqlite3_int64)nAppend+100;
182807    char *zNew = sqlite3_realloc64(pStr->z, nAlloc);
182808    if( !zNew ){
182809      return SQLITE_NOMEM;
182810    }
182811    pStr->z = zNew;
182812    pStr->nAlloc = nAlloc;
182813  }
182814  assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) );
182815
182816  /* Append the data to the string buffer. */
182817  memcpy(&pStr->z[pStr->n], zAppend, nAppend);
182818  pStr->n += nAppend;
182819  pStr->z[pStr->n] = '\0';
182820
182821  return SQLITE_OK;
182822}
182823
182824/*
182825** The fts3BestSnippet() function often selects snippets that end with a
182826** query term. That is, the final term of the snippet is always a term
182827** that requires highlighting. For example, if 'X' is a highlighted term
182828** and '.' is a non-highlighted term, BestSnippet() may select:
182829**
182830**     ........X.....X
182831**
182832** This function "shifts" the beginning of the snippet forward in the
182833** document so that there are approximately the same number of
182834** non-highlighted terms to the right of the final highlighted term as there
182835** are to the left of the first highlighted term. For example, to this:
182836**
182837**     ....X.....X....
182838**
182839** This is done as part of extracting the snippet text, not when selecting
182840** the snippet. Snippet selection is done based on doclists only, so there
182841** is no way for fts3BestSnippet() to know whether or not the document
182842** actually contains terms that follow the final highlighted term.
182843*/
182844static int fts3SnippetShift(
182845  Fts3Table *pTab,                /* FTS3 table snippet comes from */
182846  int iLangid,                    /* Language id to use in tokenizing */
182847  int nSnippet,                   /* Number of tokens desired for snippet */
182848  const char *zDoc,               /* Document text to extract snippet from */
182849  int nDoc,                       /* Size of buffer zDoc in bytes */
182850  int *piPos,                     /* IN/OUT: First token of snippet */
182851  u64 *pHlmask                    /* IN/OUT: Mask of tokens to highlight */
182852){
182853  u64 hlmask = *pHlmask;          /* Local copy of initial highlight-mask */
182854
182855  if( hlmask ){
182856    int nLeft;                    /* Tokens to the left of first highlight */
182857    int nRight;                   /* Tokens to the right of last highlight */
182858    int nDesired;                 /* Ideal number of tokens to shift forward */
182859
182860    for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
182861    for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
182862    assert( (nSnippet-1-nRight)<=63 && (nSnippet-1-nRight)>=0 );
182863    nDesired = (nLeft-nRight)/2;
182864
182865    /* Ideally, the start of the snippet should be pushed forward in the
182866    ** document nDesired tokens. This block checks if there are actually
182867    ** nDesired tokens to the right of the snippet. If so, *piPos and
182868    ** *pHlMask are updated to shift the snippet nDesired tokens to the
182869    ** right. Otherwise, the snippet is shifted by the number of tokens
182870    ** available.
182871    */
182872    if( nDesired>0 ){
182873      int nShift;                 /* Number of tokens to shift snippet by */
182874      int iCurrent = 0;           /* Token counter */
182875      int rc;                     /* Return Code */
182876      sqlite3_tokenizer_module *pMod;
182877      sqlite3_tokenizer_cursor *pC;
182878      pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
182879
182880      /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
182881      ** or more tokens in zDoc/nDoc.
182882      */
182883      rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC);
182884      if( rc!=SQLITE_OK ){
182885        return rc;
182886      }
182887      while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
182888        const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
182889        rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
182890      }
182891      pMod->xClose(pC);
182892      if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
182893
182894      nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
182895      assert( nShift<=nDesired );
182896      if( nShift>0 ){
182897        *piPos += nShift;
182898        *pHlmask = hlmask >> nShift;
182899      }
182900    }
182901  }
182902  return SQLITE_OK;
182903}
182904
182905/*
182906** Extract the snippet text for fragment pFragment from cursor pCsr and
182907** append it to string buffer pOut.
182908*/
182909static int fts3SnippetText(
182910  Fts3Cursor *pCsr,               /* FTS3 Cursor */
182911  SnippetFragment *pFragment,     /* Snippet to extract */
182912  int iFragment,                  /* Fragment number */
182913  int isLast,                     /* True for final fragment in snippet */
182914  int nSnippet,                   /* Number of tokens in extracted snippet */
182915  const char *zOpen,              /* String inserted before highlighted term */
182916  const char *zClose,             /* String inserted after highlighted term */
182917  const char *zEllipsis,          /* String inserted between snippets */
182918  StrBuffer *pOut                 /* Write output here */
182919){
182920  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
182921  int rc;                         /* Return code */
182922  const char *zDoc;               /* Document text to extract snippet from */
182923  int nDoc;                       /* Size of zDoc in bytes */
182924  int iCurrent = 0;               /* Current token number of document */
182925  int iEnd = 0;                   /* Byte offset of end of current token */
182926  int isShiftDone = 0;            /* True after snippet is shifted */
182927  int iPos = pFragment->iPos;     /* First token of snippet */
182928  u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
182929  int iCol = pFragment->iCol+1;   /* Query column to extract text from */
182930  sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
182931  sqlite3_tokenizer_cursor *pC;   /* Tokenizer cursor open on zDoc/nDoc */
182932
182933  zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
182934  if( zDoc==0 ){
182935    if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
182936      return SQLITE_NOMEM;
182937    }
182938    return SQLITE_OK;
182939  }
182940  nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
182941
182942  /* Open a token cursor on the document. */
182943  pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
182944  rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC);
182945  if( rc!=SQLITE_OK ){
182946    return rc;
182947  }
182948
182949  while( rc==SQLITE_OK ){
182950    const char *ZDUMMY;           /* Dummy argument used with tokenizer */
182951    int DUMMY1 = -1;              /* Dummy argument used with tokenizer */
182952    int iBegin = 0;               /* Offset in zDoc of start of token */
182953    int iFin = 0;                 /* Offset in zDoc of end of token */
182954    int isHighlight = 0;          /* True for highlighted terms */
182955
182956    /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
182957    ** in the FTS code the variable that the third argument to xNext points to
182958    ** is initialized to zero before the first (*but not necessarily
182959    ** subsequent*) call to xNext(). This is done for a particular application
182960    ** that needs to know whether or not the tokenizer is being used for
182961    ** snippet generation or for some other purpose.
182962    **
182963    ** Extreme care is required when writing code to depend on this
182964    ** initialization. It is not a documented part of the tokenizer interface.
182965    ** If a tokenizer is used directly by any code outside of FTS, this
182966    ** convention might not be respected.  */
182967    rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
182968    if( rc!=SQLITE_OK ){
182969      if( rc==SQLITE_DONE ){
182970        /* Special case - the last token of the snippet is also the last token
182971        ** of the column. Append any punctuation that occurred between the end
182972        ** of the previous token and the end of the document to the output.
182973        ** Then break out of the loop. */
182974        rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
182975      }
182976      break;
182977    }
182978    if( iCurrent<iPos ){ continue; }
182979
182980    if( !isShiftDone ){
182981      int n = nDoc - iBegin;
182982      rc = fts3SnippetShift(
182983          pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask
182984      );
182985      isShiftDone = 1;
182986
182987      /* Now that the shift has been done, check if the initial "..." are
182988      ** required. They are required if (a) this is not the first fragment,
182989      ** or (b) this fragment does not begin at position 0 of its column.
182990      */
182991      if( rc==SQLITE_OK ){
182992        if( iPos>0 || iFragment>0 ){
182993          rc = fts3StringAppend(pOut, zEllipsis, -1);
182994        }else if( iBegin ){
182995          rc = fts3StringAppend(pOut, zDoc, iBegin);
182996        }
182997      }
182998      if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
182999    }
183000
183001    if( iCurrent>=(iPos+nSnippet) ){
183002      if( isLast ){
183003        rc = fts3StringAppend(pOut, zEllipsis, -1);
183004      }
183005      break;
183006    }
183007
183008    /* Set isHighlight to true if this term should be highlighted. */
183009    isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
183010
183011    if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
183012    if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
183013    if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
183014    if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
183015
183016    iEnd = iFin;
183017  }
183018
183019  pMod->xClose(pC);
183020  return rc;
183021}
183022
183023
183024/*
183025** This function is used to count the entries in a column-list (a
183026** delta-encoded list of term offsets within a single column of a single
183027** row). When this function is called, *ppCollist should point to the
183028** beginning of the first varint in the column-list (the varint that
183029** contains the position of the first matching term in the column data).
183030** Before returning, *ppCollist is set to point to the first byte after
183031** the last varint in the column-list (either the 0x00 signifying the end
183032** of the position-list, or the 0x01 that precedes the column number of
183033** the next column in the position-list).
183034**
183035** The number of elements in the column-list is returned.
183036*/
183037static int fts3ColumnlistCount(char **ppCollist){
183038  char *pEnd = *ppCollist;
183039  char c = 0;
183040  int nEntry = 0;
183041
183042  /* A column-list is terminated by either a 0x01 or 0x00. */
183043  while( 0xFE & (*pEnd | c) ){
183044    c = *pEnd++ & 0x80;
183045    if( !c ) nEntry++;
183046  }
183047
183048  *ppCollist = pEnd;
183049  return nEntry;
183050}
183051
183052/*
183053** This function gathers 'y' or 'b' data for a single phrase.
183054*/
183055static int fts3ExprLHits(
183056  Fts3Expr *pExpr,                /* Phrase expression node */
183057  MatchInfo *p                    /* Matchinfo context */
183058){
183059  Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
183060  int iStart;
183061  Fts3Phrase *pPhrase = pExpr->pPhrase;
183062  char *pIter = pPhrase->doclist.pList;
183063  int iCol = 0;
183064
183065  assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS );
183066  if( p->flag==FTS3_MATCHINFO_LHITS ){
183067    iStart = pExpr->iPhrase * p->nCol;
183068  }else{
183069    iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
183070  }
183071
183072  if( pIter ) while( 1 ){
183073    int nHit = fts3ColumnlistCount(&pIter);
183074    if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
183075      if( p->flag==FTS3_MATCHINFO_LHITS ){
183076        p->aMatchinfo[iStart + iCol] = (u32)nHit;
183077      }else if( nHit ){
183078        p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
183079      }
183080    }
183081    assert( *pIter==0x00 || *pIter==0x01 );
183082    if( *pIter!=0x01 ) break;
183083    pIter++;
183084    pIter += fts3GetVarint32(pIter, &iCol);
183085    if( iCol>=p->nCol ) return FTS_CORRUPT_VTAB;
183086  }
183087  return SQLITE_OK;
183088}
183089
183090/*
183091** Gather the results for matchinfo directives 'y' and 'b'.
183092*/
183093static int fts3ExprLHitGather(
183094  Fts3Expr *pExpr,
183095  MatchInfo *p
183096){
183097  int rc = SQLITE_OK;
183098  assert( (pExpr->pLeft==0)==(pExpr->pRight==0) );
183099  if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
183100    if( pExpr->pLeft ){
183101      rc = fts3ExprLHitGather(pExpr->pLeft, p);
183102      if( rc==SQLITE_OK ) rc = fts3ExprLHitGather(pExpr->pRight, p);
183103    }else{
183104      rc = fts3ExprLHits(pExpr, p);
183105    }
183106  }
183107  return rc;
183108}
183109
183110/*
183111** fts3ExprIterate() callback used to collect the "global" matchinfo stats
183112** for a single query.
183113**
183114** fts3ExprIterate() callback to load the 'global' elements of a
183115** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements
183116** of the matchinfo array that are constant for all rows returned by the
183117** current query.
183118**
183119** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
183120** function populates Matchinfo.aMatchinfo[] as follows:
183121**
183122**   for(iCol=0; iCol<nCol; iCol++){
183123**     aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
183124**     aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
183125**   }
183126**
183127** where X is the number of matches for phrase iPhrase is column iCol of all
183128** rows of the table. Y is the number of rows for which column iCol contains
183129** at least one instance of phrase iPhrase.
183130**
183131** If the phrase pExpr consists entirely of deferred tokens, then all X and
183132** Y values are set to nDoc, where nDoc is the number of documents in the
183133** file system. This is done because the full-text index doclist is required
183134** to calculate these values properly, and the full-text index doclist is
183135** not available for deferred tokens.
183136*/
183137static int fts3ExprGlobalHitsCb(
183138  Fts3Expr *pExpr,                /* Phrase expression node */
183139  int iPhrase,                    /* Phrase number (numbered from zero) */
183140  void *pCtx                      /* Pointer to MatchInfo structure */
183141){
183142  MatchInfo *p = (MatchInfo *)pCtx;
183143  return sqlite3Fts3EvalPhraseStats(
183144      p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
183145  );
183146}
183147
183148/*
183149** fts3ExprIterate() callback used to collect the "local" part of the
183150** FTS3_MATCHINFO_HITS array. The local stats are those elements of the
183151** array that are different for each row returned by the query.
183152*/
183153static int fts3ExprLocalHitsCb(
183154  Fts3Expr *pExpr,                /* Phrase expression node */
183155  int iPhrase,                    /* Phrase number */
183156  void *pCtx                      /* Pointer to MatchInfo structure */
183157){
183158  int rc = SQLITE_OK;
183159  MatchInfo *p = (MatchInfo *)pCtx;
183160  int iStart = iPhrase * p->nCol * 3;
183161  int i;
183162
183163  for(i=0; i<p->nCol && rc==SQLITE_OK; i++){
183164    char *pCsr;
183165    rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
183166    if( pCsr ){
183167      p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
183168    }else{
183169      p->aMatchinfo[iStart+i*3] = 0;
183170    }
183171  }
183172
183173  return rc;
183174}
183175
183176static int fts3MatchinfoCheck(
183177  Fts3Table *pTab,
183178  char cArg,
183179  char **pzErr
183180){
183181  if( (cArg==FTS3_MATCHINFO_NPHRASE)
183182   || (cArg==FTS3_MATCHINFO_NCOL)
183183   || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
183184   || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
183185   || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
183186   || (cArg==FTS3_MATCHINFO_LCS)
183187   || (cArg==FTS3_MATCHINFO_HITS)
183188   || (cArg==FTS3_MATCHINFO_LHITS)
183189   || (cArg==FTS3_MATCHINFO_LHITS_BM)
183190  ){
183191    return SQLITE_OK;
183192  }
183193  sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg);
183194  return SQLITE_ERROR;
183195}
183196
183197static size_t fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
183198  size_t nVal;                      /* Number of integers output by cArg */
183199
183200  switch( cArg ){
183201    case FTS3_MATCHINFO_NDOC:
183202    case FTS3_MATCHINFO_NPHRASE:
183203    case FTS3_MATCHINFO_NCOL:
183204      nVal = 1;
183205      break;
183206
183207    case FTS3_MATCHINFO_AVGLENGTH:
183208    case FTS3_MATCHINFO_LENGTH:
183209    case FTS3_MATCHINFO_LCS:
183210      nVal = pInfo->nCol;
183211      break;
183212
183213    case FTS3_MATCHINFO_LHITS:
183214      nVal = pInfo->nCol * pInfo->nPhrase;
183215      break;
183216
183217    case FTS3_MATCHINFO_LHITS_BM:
183218      nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
183219      break;
183220
183221    default:
183222      assert( cArg==FTS3_MATCHINFO_HITS );
183223      nVal = pInfo->nCol * pInfo->nPhrase * 3;
183224      break;
183225  }
183226
183227  return nVal;
183228}
183229
183230static int fts3MatchinfoSelectDoctotal(
183231  Fts3Table *pTab,
183232  sqlite3_stmt **ppStmt,
183233  sqlite3_int64 *pnDoc,
183234  const char **paLen,
183235  const char **ppEnd
183236){
183237  sqlite3_stmt *pStmt;
183238  const char *a;
183239  const char *pEnd;
183240  sqlite3_int64 nDoc;
183241  int n;
183242
183243
183244  if( !*ppStmt ){
183245    int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
183246    if( rc!=SQLITE_OK ) return rc;
183247  }
183248  pStmt = *ppStmt;
183249  assert( sqlite3_data_count(pStmt)==1 );
183250
183251  n = sqlite3_column_bytes(pStmt, 0);
183252  a = sqlite3_column_blob(pStmt, 0);
183253  if( a==0 ){
183254    return FTS_CORRUPT_VTAB;
183255  }
183256  pEnd = a + n;
183257  a += sqlite3Fts3GetVarintBounded(a, pEnd, &nDoc);
183258  if( nDoc<=0 || a>pEnd ){
183259    return FTS_CORRUPT_VTAB;
183260  }
183261  *pnDoc = nDoc;
183262
183263  if( paLen ) *paLen = a;
183264  if( ppEnd ) *ppEnd = pEnd;
183265  return SQLITE_OK;
183266}
183267
183268/*
183269** An instance of the following structure is used to store state while
183270** iterating through a multi-column position-list corresponding to the
183271** hits for a single phrase on a single row in order to calculate the
183272** values for a matchinfo() FTS3_MATCHINFO_LCS request.
183273*/
183274typedef struct LcsIterator LcsIterator;
183275struct LcsIterator {
183276  Fts3Expr *pExpr;                /* Pointer to phrase expression */
183277  int iPosOffset;                 /* Tokens count up to end of this phrase */
183278  char *pRead;                    /* Cursor used to iterate through aDoclist */
183279  int iPos;                       /* Current position */
183280};
183281
183282/*
183283** If LcsIterator.iCol is set to the following value, the iterator has
183284** finished iterating through all offsets for all columns.
183285*/
183286#define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
183287
183288static int fts3MatchinfoLcsCb(
183289  Fts3Expr *pExpr,                /* Phrase expression node */
183290  int iPhrase,                    /* Phrase number (numbered from zero) */
183291  void *pCtx                      /* Pointer to MatchInfo structure */
183292){
183293  LcsIterator *aIter = (LcsIterator *)pCtx;
183294  aIter[iPhrase].pExpr = pExpr;
183295  return SQLITE_OK;
183296}
183297
183298/*
183299** Advance the iterator passed as an argument to the next position. Return
183300** 1 if the iterator is at EOF or if it now points to the start of the
183301** position list for the next column.
183302*/
183303static int fts3LcsIteratorAdvance(LcsIterator *pIter){
183304  char *pRead = pIter->pRead;
183305  sqlite3_int64 iRead;
183306  int rc = 0;
183307
183308  pRead += sqlite3Fts3GetVarint(pRead, &iRead);
183309  if( iRead==0 || iRead==1 ){
183310    pRead = 0;
183311    rc = 1;
183312  }else{
183313    pIter->iPos += (int)(iRead-2);
183314  }
183315
183316  pIter->pRead = pRead;
183317  return rc;
183318}
183319
183320/*
183321** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag.
183322**
183323** If the call is successful, the longest-common-substring lengths for each
183324** column are written into the first nCol elements of the pInfo->aMatchinfo[]
183325** array before returning. SQLITE_OK is returned in this case.
183326**
183327** Otherwise, if an error occurs, an SQLite error code is returned and the
183328** data written to the first nCol elements of pInfo->aMatchinfo[] is
183329** undefined.
183330*/
183331static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
183332  LcsIterator *aIter;
183333  int i;
183334  int iCol;
183335  int nToken = 0;
183336  int rc = SQLITE_OK;
183337
183338  /* Allocate and populate the array of LcsIterator objects. The array
183339  ** contains one element for each matchable phrase in the query.
183340  **/
183341  aIter = sqlite3_malloc64(sizeof(LcsIterator) * pCsr->nPhrase);
183342  if( !aIter ) return SQLITE_NOMEM;
183343  memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
183344  (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
183345
183346  for(i=0; i<pInfo->nPhrase; i++){
183347    LcsIterator *pIter = &aIter[i];
183348    nToken -= pIter->pExpr->pPhrase->nToken;
183349    pIter->iPosOffset = nToken;
183350  }
183351
183352  for(iCol=0; iCol<pInfo->nCol; iCol++){
183353    int nLcs = 0;                 /* LCS value for this column */
183354    int nLive = 0;                /* Number of iterators in aIter not at EOF */
183355
183356    for(i=0; i<pInfo->nPhrase; i++){
183357      LcsIterator *pIt = &aIter[i];
183358      rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
183359      if( rc!=SQLITE_OK ) goto matchinfo_lcs_out;
183360      if( pIt->pRead ){
183361        pIt->iPos = pIt->iPosOffset;
183362        fts3LcsIteratorAdvance(pIt);
183363        if( pIt->pRead==0 ){
183364          rc = FTS_CORRUPT_VTAB;
183365          goto matchinfo_lcs_out;
183366        }
183367        nLive++;
183368      }
183369    }
183370
183371    while( nLive>0 ){
183372      LcsIterator *pAdv = 0;      /* The iterator to advance by one position */
183373      int nThisLcs = 0;           /* LCS for the current iterator positions */
183374
183375      for(i=0; i<pInfo->nPhrase; i++){
183376        LcsIterator *pIter = &aIter[i];
183377        if( pIter->pRead==0 ){
183378          /* This iterator is already at EOF for this column. */
183379          nThisLcs = 0;
183380        }else{
183381          if( pAdv==0 || pIter->iPos<pAdv->iPos ){
183382            pAdv = pIter;
183383          }
183384          if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
183385            nThisLcs++;
183386          }else{
183387            nThisLcs = 1;
183388          }
183389          if( nThisLcs>nLcs ) nLcs = nThisLcs;
183390        }
183391      }
183392      if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
183393    }
183394
183395    pInfo->aMatchinfo[iCol] = nLcs;
183396  }
183397
183398 matchinfo_lcs_out:
183399  sqlite3_free(aIter);
183400  return rc;
183401}
183402
183403/*
183404** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
183405** be returned by the matchinfo() function. Argument zArg contains the
183406** format string passed as the second argument to matchinfo (or the
183407** default value "pcx" if no second argument was specified). The format
183408** string has already been validated and the pInfo->aMatchinfo[] array
183409** is guaranteed to be large enough for the output.
183410**
183411** If bGlobal is true, then populate all fields of the matchinfo() output.
183412** If it is false, then assume that those fields that do not change between
183413** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
183414** have already been populated.
183415**
183416** Return SQLITE_OK if successful, or an SQLite error code if an error
183417** occurs. If a value other than SQLITE_OK is returned, the state the
183418** pInfo->aMatchinfo[] buffer is left in is undefined.
183419*/
183420static int fts3MatchinfoValues(
183421  Fts3Cursor *pCsr,               /* FTS3 cursor object */
183422  int bGlobal,                    /* True to grab the global stats */
183423  MatchInfo *pInfo,               /* Matchinfo context object */
183424  const char *zArg                /* Matchinfo format string */
183425){
183426  int rc = SQLITE_OK;
183427  int i;
183428  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
183429  sqlite3_stmt *pSelect = 0;
183430
183431  for(i=0; rc==SQLITE_OK && zArg[i]; i++){
183432    pInfo->flag = zArg[i];
183433    switch( zArg[i] ){
183434      case FTS3_MATCHINFO_NPHRASE:
183435        if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
183436        break;
183437
183438      case FTS3_MATCHINFO_NCOL:
183439        if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
183440        break;
183441
183442      case FTS3_MATCHINFO_NDOC:
183443        if( bGlobal ){
183444          sqlite3_int64 nDoc = 0;
183445          rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0, 0);
183446          pInfo->aMatchinfo[0] = (u32)nDoc;
183447        }
183448        break;
183449
183450      case FTS3_MATCHINFO_AVGLENGTH:
183451        if( bGlobal ){
183452          sqlite3_int64 nDoc;     /* Number of rows in table */
183453          const char *a;          /* Aggregate column length array */
183454          const char *pEnd;       /* First byte past end of length array */
183455
183456          rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a, &pEnd);
183457          if( rc==SQLITE_OK ){
183458            int iCol;
183459            for(iCol=0; iCol<pInfo->nCol; iCol++){
183460              u32 iVal;
183461              sqlite3_int64 nToken;
183462              a += sqlite3Fts3GetVarint(a, &nToken);
183463              if( a>pEnd ){
183464                rc = SQLITE_CORRUPT_VTAB;
183465                break;
183466              }
183467              iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
183468              pInfo->aMatchinfo[iCol] = iVal;
183469            }
183470          }
183471        }
183472        break;
183473
183474      case FTS3_MATCHINFO_LENGTH: {
183475        sqlite3_stmt *pSelectDocsize = 0;
183476        rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
183477        if( rc==SQLITE_OK ){
183478          int iCol;
183479          const char *a = sqlite3_column_blob(pSelectDocsize, 0);
183480          const char *pEnd = a + sqlite3_column_bytes(pSelectDocsize, 0);
183481          for(iCol=0; iCol<pInfo->nCol; iCol++){
183482            sqlite3_int64 nToken;
183483            a += sqlite3Fts3GetVarintBounded(a, pEnd, &nToken);
183484            if( a>pEnd ){
183485              rc = SQLITE_CORRUPT_VTAB;
183486              break;
183487            }
183488            pInfo->aMatchinfo[iCol] = (u32)nToken;
183489          }
183490        }
183491        sqlite3_reset(pSelectDocsize);
183492        break;
183493      }
183494
183495      case FTS3_MATCHINFO_LCS:
183496        rc = fts3ExprLoadDoclists(pCsr, 0, 0);
183497        if( rc==SQLITE_OK ){
183498          rc = fts3MatchinfoLcs(pCsr, pInfo);
183499        }
183500        break;
183501
183502      case FTS3_MATCHINFO_LHITS_BM:
183503      case FTS3_MATCHINFO_LHITS: {
183504        size_t nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32);
183505        memset(pInfo->aMatchinfo, 0, nZero);
183506        rc = fts3ExprLHitGather(pCsr->pExpr, pInfo);
183507        break;
183508      }
183509
183510      default: {
183511        Fts3Expr *pExpr;
183512        assert( zArg[i]==FTS3_MATCHINFO_HITS );
183513        pExpr = pCsr->pExpr;
183514        rc = fts3ExprLoadDoclists(pCsr, 0, 0);
183515        if( rc!=SQLITE_OK ) break;
183516        if( bGlobal ){
183517          if( pCsr->pDeferred ){
183518            rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc,0,0);
183519            if( rc!=SQLITE_OK ) break;
183520          }
183521          rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
183522          sqlite3Fts3EvalTestDeferred(pCsr, &rc);
183523          if( rc!=SQLITE_OK ) break;
183524        }
183525        (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
183526        break;
183527      }
183528    }
183529
183530    pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
183531  }
183532
183533  sqlite3_reset(pSelect);
183534  return rc;
183535}
183536
183537
183538/*
183539** Populate pCsr->aMatchinfo[] with data for the current row. The
183540** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
183541*/
183542static void fts3GetMatchinfo(
183543  sqlite3_context *pCtx,        /* Return results here */
183544  Fts3Cursor *pCsr,               /* FTS3 Cursor object */
183545  const char *zArg                /* Second argument to matchinfo() function */
183546){
183547  MatchInfo sInfo;
183548  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
183549  int rc = SQLITE_OK;
183550  int bGlobal = 0;                /* Collect 'global' stats as well as local */
183551
183552  u32 *aOut = 0;
183553  void (*xDestroyOut)(void*) = 0;
183554
183555  memset(&sInfo, 0, sizeof(MatchInfo));
183556  sInfo.pCursor = pCsr;
183557  sInfo.nCol = pTab->nColumn;
183558
183559  /* If there is cached matchinfo() data, but the format string for the
183560  ** cache does not match the format string for this request, discard
183561  ** the cached data. */
183562  if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){
183563    sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
183564    pCsr->pMIBuffer = 0;
183565  }
183566
183567  /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the
183568  ** matchinfo function has been called for this query. In this case
183569  ** allocate the array used to accumulate the matchinfo data and
183570  ** initialize those elements that are constant for every row.
183571  */
183572  if( pCsr->pMIBuffer==0 ){
183573    size_t nMatchinfo = 0;        /* Number of u32 elements in match-info */
183574    int i;                        /* Used to iterate through zArg */
183575
183576    /* Determine the number of phrases in the query */
183577    pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
183578    sInfo.nPhrase = pCsr->nPhrase;
183579
183580    /* Determine the number of integers in the buffer returned by this call. */
183581    for(i=0; zArg[i]; i++){
183582      char *zErr = 0;
183583      if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
183584        sqlite3_result_error(pCtx, zErr, -1);
183585        sqlite3_free(zErr);
183586        return;
183587      }
183588      nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
183589    }
183590
183591    /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
183592    pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg);
183593    if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM;
183594
183595    pCsr->isMatchinfoNeeded = 1;
183596    bGlobal = 1;
183597  }
183598
183599  if( rc==SQLITE_OK ){
183600    xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut);
183601    if( xDestroyOut==0 ){
183602      rc = SQLITE_NOMEM;
183603    }
183604  }
183605
183606  if( rc==SQLITE_OK ){
183607    sInfo.aMatchinfo = aOut;
183608    sInfo.nPhrase = pCsr->nPhrase;
183609    rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
183610    if( bGlobal ){
183611      fts3MIBufferSetGlobal(pCsr->pMIBuffer);
183612    }
183613  }
183614
183615  if( rc!=SQLITE_OK ){
183616    sqlite3_result_error_code(pCtx, rc);
183617    if( xDestroyOut ) xDestroyOut(aOut);
183618  }else{
183619    int n = pCsr->pMIBuffer->nElem * sizeof(u32);
183620    sqlite3_result_blob(pCtx, aOut, n, xDestroyOut);
183621  }
183622}
183623
183624/*
183625** Implementation of snippet() function.
183626*/
183627SQLITE_PRIVATE void sqlite3Fts3Snippet(
183628  sqlite3_context *pCtx,          /* SQLite function call context */
183629  Fts3Cursor *pCsr,               /* Cursor object */
183630  const char *zStart,             /* Snippet start text - "<b>" */
183631  const char *zEnd,               /* Snippet end text - "</b>" */
183632  const char *zEllipsis,          /* Snippet ellipsis text - "<b>...</b>" */
183633  int iCol,                       /* Extract snippet from this column */
183634  int nToken                      /* Approximate number of tokens in snippet */
183635){
183636  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
183637  int rc = SQLITE_OK;
183638  int i;
183639  StrBuffer res = {0, 0, 0};
183640
183641  /* The returned text includes up to four fragments of text extracted from
183642  ** the data in the current row. The first iteration of the for(...) loop
183643  ** below attempts to locate a single fragment of text nToken tokens in
183644  ** size that contains at least one instance of all phrases in the query
183645  ** expression that appear in the current row. If such a fragment of text
183646  ** cannot be found, the second iteration of the loop attempts to locate
183647  ** a pair of fragments, and so on.
183648  */
183649  int nSnippet = 0;               /* Number of fragments in this snippet */
183650  SnippetFragment aSnippet[4];    /* Maximum of 4 fragments per snippet */
183651  int nFToken = -1;               /* Number of tokens in each fragment */
183652
183653  if( !pCsr->pExpr ){
183654    sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
183655    return;
183656  }
183657
183658  /* Limit the snippet length to 64 tokens. */
183659  if( nToken<-64 ) nToken = -64;
183660  if( nToken>+64 ) nToken = +64;
183661
183662  for(nSnippet=1; 1; nSnippet++){
183663
183664    int iSnip;                    /* Loop counter 0..nSnippet-1 */
183665    u64 mCovered = 0;             /* Bitmask of phrases covered by snippet */
183666    u64 mSeen = 0;                /* Bitmask of phrases seen by BestSnippet() */
183667
183668    if( nToken>=0 ){
183669      nFToken = (nToken+nSnippet-1) / nSnippet;
183670    }else{
183671      nFToken = -1 * nToken;
183672    }
183673
183674    for(iSnip=0; iSnip<nSnippet; iSnip++){
183675      int iBestScore = -1;        /* Best score of columns checked so far */
183676      int iRead;                  /* Used to iterate through columns */
183677      SnippetFragment *pFragment = &aSnippet[iSnip];
183678
183679      memset(pFragment, 0, sizeof(*pFragment));
183680
183681      /* Loop through all columns of the table being considered for snippets.
183682      ** If the iCol argument to this function was negative, this means all
183683      ** columns of the FTS3 table. Otherwise, only column iCol is considered.
183684      */
183685      for(iRead=0; iRead<pTab->nColumn; iRead++){
183686        SnippetFragment sF = {0, 0, 0, 0};
183687        int iS = 0;
183688        if( iCol>=0 && iRead!=iCol ) continue;
183689
183690        /* Find the best snippet of nFToken tokens in column iRead. */
183691        rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
183692        if( rc!=SQLITE_OK ){
183693          goto snippet_out;
183694        }
183695        if( iS>iBestScore ){
183696          *pFragment = sF;
183697          iBestScore = iS;
183698        }
183699      }
183700
183701      mCovered |= pFragment->covered;
183702    }
183703
183704    /* If all query phrases seen by fts3BestSnippet() are present in at least
183705    ** one of the nSnippet snippet fragments, break out of the loop.
183706    */
183707    assert( (mCovered&mSeen)==mCovered );
183708    if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
183709  }
183710
183711  assert( nFToken>0 );
183712
183713  for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
183714    rc = fts3SnippetText(pCsr, &aSnippet[i],
183715        i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
183716    );
183717  }
183718
183719 snippet_out:
183720  sqlite3Fts3SegmentsClose(pTab);
183721  if( rc!=SQLITE_OK ){
183722    sqlite3_result_error_code(pCtx, rc);
183723    sqlite3_free(res.z);
183724  }else{
183725    sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
183726  }
183727}
183728
183729
183730typedef struct TermOffset TermOffset;
183731typedef struct TermOffsetCtx TermOffsetCtx;
183732
183733struct TermOffset {
183734  char *pList;                    /* Position-list */
183735  int iPos;                       /* Position just read from pList */
183736  int iOff;                       /* Offset of this term from read positions */
183737};
183738
183739struct TermOffsetCtx {
183740  Fts3Cursor *pCsr;
183741  int iCol;                       /* Column of table to populate aTerm for */
183742  int iTerm;
183743  sqlite3_int64 iDocid;
183744  TermOffset *aTerm;
183745};
183746
183747/*
183748** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
183749*/
183750static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
183751  TermOffsetCtx *p = (TermOffsetCtx *)ctx;
183752  int nTerm;                      /* Number of tokens in phrase */
183753  int iTerm;                      /* For looping through nTerm phrase terms */
183754  char *pList;                    /* Pointer to position list for phrase */
183755  int iPos = 0;                   /* First position in position-list */
183756  int rc;
183757
183758  UNUSED_PARAMETER(iPhrase);
183759  rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
183760  nTerm = pExpr->pPhrase->nToken;
183761  if( pList ){
183762    fts3GetDeltaPosition(&pList, &iPos);
183763    assert_fts3_nc( iPos>=0 );
183764  }
183765
183766  for(iTerm=0; iTerm<nTerm; iTerm++){
183767    TermOffset *pT = &p->aTerm[p->iTerm++];
183768    pT->iOff = nTerm-iTerm-1;
183769    pT->pList = pList;
183770    pT->iPos = iPos;
183771  }
183772
183773  return rc;
183774}
183775
183776/*
183777** Implementation of offsets() function.
183778*/
183779SQLITE_PRIVATE void sqlite3Fts3Offsets(
183780  sqlite3_context *pCtx,          /* SQLite function call context */
183781  Fts3Cursor *pCsr                /* Cursor object */
183782){
183783  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
183784  sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
183785  int rc;                         /* Return Code */
183786  int nToken;                     /* Number of tokens in query */
183787  int iCol;                       /* Column currently being processed */
183788  StrBuffer res = {0, 0, 0};      /* Result string */
183789  TermOffsetCtx sCtx;             /* Context for fts3ExprTermOffsetInit() */
183790
183791  if( !pCsr->pExpr ){
183792    sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
183793    return;
183794  }
183795
183796  memset(&sCtx, 0, sizeof(sCtx));
183797  assert( pCsr->isRequireSeek==0 );
183798
183799  /* Count the number of terms in the query */
183800  rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
183801  if( rc!=SQLITE_OK ) goto offsets_out;
183802
183803  /* Allocate the array of TermOffset iterators. */
183804  sCtx.aTerm = (TermOffset *)sqlite3_malloc64(sizeof(TermOffset)*nToken);
183805  if( 0==sCtx.aTerm ){
183806    rc = SQLITE_NOMEM;
183807    goto offsets_out;
183808  }
183809  sCtx.iDocid = pCsr->iPrevId;
183810  sCtx.pCsr = pCsr;
183811
183812  /* Loop through the table columns, appending offset information to
183813  ** string-buffer res for each column.
183814  */
183815  for(iCol=0; iCol<pTab->nColumn; iCol++){
183816    sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
183817    const char *ZDUMMY;           /* Dummy argument used with xNext() */
183818    int NDUMMY = 0;               /* Dummy argument used with xNext() */
183819    int iStart = 0;
183820    int iEnd = 0;
183821    int iCurrent = 0;
183822    const char *zDoc;
183823    int nDoc;
183824
183825    /* Initialize the contents of sCtx.aTerm[] for column iCol. There is
183826    ** no way that this operation can fail, so the return code from
183827    ** fts3ExprIterate() can be discarded.
183828    */
183829    sCtx.iCol = iCol;
183830    sCtx.iTerm = 0;
183831    (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx);
183832
183833    /* Retreive the text stored in column iCol. If an SQL NULL is stored
183834    ** in column iCol, jump immediately to the next iteration of the loop.
183835    ** If an OOM occurs while retrieving the data (this can happen if SQLite
183836    ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
183837    ** to the caller.
183838    */
183839    zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
183840    nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
183841    if( zDoc==0 ){
183842      if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
183843        continue;
183844      }
183845      rc = SQLITE_NOMEM;
183846      goto offsets_out;
183847    }
183848
183849    /* Initialize a tokenizer iterator to iterate through column iCol. */
183850    rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid,
183851        zDoc, nDoc, &pC
183852    );
183853    if( rc!=SQLITE_OK ) goto offsets_out;
183854
183855    rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
183856    while( rc==SQLITE_OK ){
183857      int i;                      /* Used to loop through terms */
183858      int iMinPos = 0x7FFFFFFF;   /* Position of next token */
183859      TermOffset *pTerm = 0;      /* TermOffset associated with next token */
183860
183861      for(i=0; i<nToken; i++){
183862        TermOffset *pT = &sCtx.aTerm[i];
183863        if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
183864          iMinPos = pT->iPos-pT->iOff;
183865          pTerm = pT;
183866        }
183867      }
183868
183869      if( !pTerm ){
183870        /* All offsets for this column have been gathered. */
183871        rc = SQLITE_DONE;
183872      }else{
183873        assert_fts3_nc( iCurrent<=iMinPos );
183874        if( 0==(0xFE&*pTerm->pList) ){
183875          pTerm->pList = 0;
183876        }else{
183877          fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
183878        }
183879        while( rc==SQLITE_OK && iCurrent<iMinPos ){
183880          rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
183881        }
183882        if( rc==SQLITE_OK ){
183883          char aBuffer[64];
183884          sqlite3_snprintf(sizeof(aBuffer), aBuffer,
183885              "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
183886          );
183887          rc = fts3StringAppend(&res, aBuffer, -1);
183888        }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
183889          rc = FTS_CORRUPT_VTAB;
183890        }
183891      }
183892    }
183893    if( rc==SQLITE_DONE ){
183894      rc = SQLITE_OK;
183895    }
183896
183897    pMod->xClose(pC);
183898    if( rc!=SQLITE_OK ) goto offsets_out;
183899  }
183900
183901 offsets_out:
183902  sqlite3_free(sCtx.aTerm);
183903  assert( rc!=SQLITE_DONE );
183904  sqlite3Fts3SegmentsClose(pTab);
183905  if( rc!=SQLITE_OK ){
183906    sqlite3_result_error_code(pCtx,  rc);
183907    sqlite3_free(res.z);
183908  }else{
183909    sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
183910  }
183911  return;
183912}
183913
183914/*
183915** Implementation of matchinfo() function.
183916*/
183917SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
183918  sqlite3_context *pContext,      /* Function call context */
183919  Fts3Cursor *pCsr,               /* FTS3 table cursor */
183920  const char *zArg                /* Second arg to matchinfo() function */
183921){
183922  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
183923  const char *zFormat;
183924
183925  if( zArg ){
183926    zFormat = zArg;
183927  }else{
183928    zFormat = FTS3_MATCHINFO_DEFAULT;
183929  }
183930
183931  if( !pCsr->pExpr ){
183932    sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
183933    return;
183934  }else{
183935    /* Retrieve matchinfo() data. */
183936    fts3GetMatchinfo(pContext, pCsr, zFormat);
183937    sqlite3Fts3SegmentsClose(pTab);
183938  }
183939}
183940
183941#endif
183942
183943/************** End of fts3_snippet.c ****************************************/
183944/************** Begin file fts3_unicode.c ************************************/
183945/*
183946** 2012 May 24
183947**
183948** The author disclaims copyright to this source code.  In place of
183949** a legal notice, here is a blessing:
183950**
183951**    May you do good and not evil.
183952**    May you find forgiveness for yourself and forgive others.
183953**    May you share freely, never taking more than you give.
183954**
183955******************************************************************************
183956**
183957** Implementation of the "unicode" full-text-search tokenizer.
183958*/
183959
183960#ifndef SQLITE_DISABLE_FTS3_UNICODE
183961
183962/* #include "fts3Int.h" */
183963#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
183964
183965/* #include <assert.h> */
183966/* #include <stdlib.h> */
183967/* #include <stdio.h> */
183968/* #include <string.h> */
183969
183970/* #include "fts3_tokenizer.h" */
183971
183972/*
183973** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
183974** from the sqlite3 source file utf.c. If this file is compiled as part
183975** of the amalgamation, they are not required.
183976*/
183977#ifndef SQLITE_AMALGAMATION
183978
183979static const unsigned char sqlite3Utf8Trans1[] = {
183980  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
183981  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
183982  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
183983  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
183984  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
183985  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
183986  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
183987  0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
183988};
183989
183990#define READ_UTF8(zIn, zTerm, c)                           \
183991  c = *(zIn++);                                            \
183992  if( c>=0xc0 ){                                           \
183993    c = sqlite3Utf8Trans1[c-0xc0];                         \
183994    while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
183995      c = (c<<6) + (0x3f & *(zIn++));                      \
183996    }                                                      \
183997    if( c<0x80                                             \
183998        || (c&0xFFFFF800)==0xD800                          \
183999        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
184000  }
184001
184002#define WRITE_UTF8(zOut, c) {                          \
184003  if( c<0x00080 ){                                     \
184004    *zOut++ = (u8)(c&0xFF);                            \
184005  }                                                    \
184006  else if( c<0x00800 ){                                \
184007    *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
184008    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
184009  }                                                    \
184010  else if( c<0x10000 ){                                \
184011    *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
184012    *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
184013    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
184014  }else{                                               \
184015    *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
184016    *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
184017    *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
184018    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
184019  }                                                    \
184020}
184021
184022#endif /* ifndef SQLITE_AMALGAMATION */
184023
184024typedef struct unicode_tokenizer unicode_tokenizer;
184025typedef struct unicode_cursor unicode_cursor;
184026
184027struct unicode_tokenizer {
184028  sqlite3_tokenizer base;
184029  int eRemoveDiacritic;
184030  int nException;
184031  int *aiException;
184032};
184033
184034struct unicode_cursor {
184035  sqlite3_tokenizer_cursor base;
184036  const unsigned char *aInput;    /* Input text being tokenized */
184037  int nInput;                     /* Size of aInput[] in bytes */
184038  int iOff;                       /* Current offset within aInput[] */
184039  int iToken;                     /* Index of next token to be returned */
184040  char *zToken;                   /* storage for current token */
184041  int nAlloc;                     /* space allocated at zToken */
184042};
184043
184044
184045/*
184046** Destroy a tokenizer allocated by unicodeCreate().
184047*/
184048static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
184049  if( pTokenizer ){
184050    unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
184051    sqlite3_free(p->aiException);
184052    sqlite3_free(p);
184053  }
184054  return SQLITE_OK;
184055}
184056
184057/*
184058** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
184059** statement has specified that the tokenizer for this table shall consider
184060** all characters in string zIn/nIn to be separators (if bAlnum==0) or
184061** token characters (if bAlnum==1).
184062**
184063** For each codepoint in the zIn/nIn string, this function checks if the
184064** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
184065** If so, no action is taken. Otherwise, the codepoint is added to the
184066** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
184067** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
184068** codepoints in the aiException[] array.
184069**
184070** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
184071** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
184072** It is not possible to change the behavior of the tokenizer with respect
184073** to these codepoints.
184074*/
184075static int unicodeAddExceptions(
184076  unicode_tokenizer *p,           /* Tokenizer to add exceptions to */
184077  int bAlnum,                     /* Replace Isalnum() return value with this */
184078  const char *zIn,                /* Array of characters to make exceptions */
184079  int nIn                         /* Length of z in bytes */
184080){
184081  const unsigned char *z = (const unsigned char *)zIn;
184082  const unsigned char *zTerm = &z[nIn];
184083  unsigned int iCode;
184084  int nEntry = 0;
184085
184086  assert( bAlnum==0 || bAlnum==1 );
184087
184088  while( z<zTerm ){
184089    READ_UTF8(z, zTerm, iCode);
184090    assert( (sqlite3FtsUnicodeIsalnum((int)iCode) & 0xFFFFFFFE)==0 );
184091    if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
184092     && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
184093    ){
184094      nEntry++;
184095    }
184096  }
184097
184098  if( nEntry ){
184099    int *aNew;                    /* New aiException[] array */
184100    int nNew;                     /* Number of valid entries in array aNew[] */
184101
184102    aNew = sqlite3_realloc64(p->aiException,(p->nException+nEntry)*sizeof(int));
184103    if( aNew==0 ) return SQLITE_NOMEM;
184104    nNew = p->nException;
184105
184106    z = (const unsigned char *)zIn;
184107    while( z<zTerm ){
184108      READ_UTF8(z, zTerm, iCode);
184109      if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
184110       && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
184111      ){
184112        int i, j;
184113        for(i=0; i<nNew && aNew[i]<(int)iCode; i++);
184114        for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
184115        aNew[i] = (int)iCode;
184116        nNew++;
184117      }
184118    }
184119    p->aiException = aNew;
184120    p->nException = nNew;
184121  }
184122
184123  return SQLITE_OK;
184124}
184125
184126/*
184127** Return true if the p->aiException[] array contains the value iCode.
184128*/
184129static int unicodeIsException(unicode_tokenizer *p, int iCode){
184130  if( p->nException>0 ){
184131    int *a = p->aiException;
184132    int iLo = 0;
184133    int iHi = p->nException-1;
184134
184135    while( iHi>=iLo ){
184136      int iTest = (iHi + iLo) / 2;
184137      if( iCode==a[iTest] ){
184138        return 1;
184139      }else if( iCode>a[iTest] ){
184140        iLo = iTest+1;
184141      }else{
184142        iHi = iTest-1;
184143      }
184144    }
184145  }
184146
184147  return 0;
184148}
184149
184150/*
184151** Return true if, for the purposes of tokenization, codepoint iCode is
184152** considered a token character (not a separator).
184153*/
184154static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
184155  assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
184156  return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
184157}
184158
184159/*
184160** Create a new tokenizer instance.
184161*/
184162static int unicodeCreate(
184163  int nArg,                       /* Size of array argv[] */
184164  const char * const *azArg,      /* Tokenizer creation arguments */
184165  sqlite3_tokenizer **pp          /* OUT: New tokenizer handle */
184166){
184167  unicode_tokenizer *pNew;        /* New tokenizer object */
184168  int i;
184169  int rc = SQLITE_OK;
184170
184171  pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
184172  if( pNew==NULL ) return SQLITE_NOMEM;
184173  memset(pNew, 0, sizeof(unicode_tokenizer));
184174  pNew->eRemoveDiacritic = 1;
184175
184176  for(i=0; rc==SQLITE_OK && i<nArg; i++){
184177    const char *z = azArg[i];
184178    int n = (int)strlen(z);
184179
184180    if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
184181      pNew->eRemoveDiacritic = 1;
184182    }
184183    else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
184184      pNew->eRemoveDiacritic = 0;
184185    }
184186    else if( n==19 && memcmp("remove_diacritics=2", z, 19)==0 ){
184187      pNew->eRemoveDiacritic = 2;
184188    }
184189    else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
184190      rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
184191    }
184192    else if( n>=11 && memcmp("separators=", z, 11)==0 ){
184193      rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
184194    }
184195    else{
184196      /* Unrecognized argument */
184197      rc  = SQLITE_ERROR;
184198    }
184199  }
184200
184201  if( rc!=SQLITE_OK ){
184202    unicodeDestroy((sqlite3_tokenizer *)pNew);
184203    pNew = 0;
184204  }
184205  *pp = (sqlite3_tokenizer *)pNew;
184206  return rc;
184207}
184208
184209/*
184210** Prepare to begin tokenizing a particular string.  The input
184211** string to be tokenized is pInput[0..nBytes-1].  A cursor
184212** used to incrementally tokenize this string is returned in
184213** *ppCursor.
184214*/
184215static int unicodeOpen(
184216  sqlite3_tokenizer *p,           /* The tokenizer */
184217  const char *aInput,             /* Input string */
184218  int nInput,                     /* Size of string aInput in bytes */
184219  sqlite3_tokenizer_cursor **pp   /* OUT: New cursor object */
184220){
184221  unicode_cursor *pCsr;
184222
184223  pCsr = (unicode_cursor *)sqlite3_malloc(sizeof(unicode_cursor));
184224  if( pCsr==0 ){
184225    return SQLITE_NOMEM;
184226  }
184227  memset(pCsr, 0, sizeof(unicode_cursor));
184228
184229  pCsr->aInput = (const unsigned char *)aInput;
184230  if( aInput==0 ){
184231    pCsr->nInput = 0;
184232  }else if( nInput<0 ){
184233    pCsr->nInput = (int)strlen(aInput);
184234  }else{
184235    pCsr->nInput = nInput;
184236  }
184237
184238  *pp = &pCsr->base;
184239  UNUSED_PARAMETER(p);
184240  return SQLITE_OK;
184241}
184242
184243/*
184244** Close a tokenization cursor previously opened by a call to
184245** simpleOpen() above.
184246*/
184247static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
184248  unicode_cursor *pCsr = (unicode_cursor *) pCursor;
184249  sqlite3_free(pCsr->zToken);
184250  sqlite3_free(pCsr);
184251  return SQLITE_OK;
184252}
184253
184254/*
184255** Extract the next token from a tokenization cursor.  The cursor must
184256** have been opened by a prior call to simpleOpen().
184257*/
184258static int unicodeNext(
184259  sqlite3_tokenizer_cursor *pC,   /* Cursor returned by simpleOpen */
184260  const char **paToken,           /* OUT: Token text */
184261  int *pnToken,                   /* OUT: Number of bytes at *paToken */
184262  int *piStart,                   /* OUT: Starting offset of token */
184263  int *piEnd,                     /* OUT: Ending offset of token */
184264  int *piPos                      /* OUT: Position integer of token */
184265){
184266  unicode_cursor *pCsr = (unicode_cursor *)pC;
184267  unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
184268  unsigned int iCode = 0;
184269  char *zOut;
184270  const unsigned char *z = &pCsr->aInput[pCsr->iOff];
184271  const unsigned char *zStart = z;
184272  const unsigned char *zEnd;
184273  const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
184274
184275  /* Scan past any delimiter characters before the start of the next token.
184276  ** Return SQLITE_DONE early if this takes us all the way to the end of
184277  ** the input.  */
184278  while( z<zTerm ){
184279    READ_UTF8(z, zTerm, iCode);
184280    if( unicodeIsAlnum(p, (int)iCode) ) break;
184281    zStart = z;
184282  }
184283  if( zStart>=zTerm ) return SQLITE_DONE;
184284
184285  zOut = pCsr->zToken;
184286  do {
184287    int iOut;
184288
184289    /* Grow the output buffer if required. */
184290    if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
184291      char *zNew = sqlite3_realloc64(pCsr->zToken, pCsr->nAlloc+64);
184292      if( !zNew ) return SQLITE_NOMEM;
184293      zOut = &zNew[zOut - pCsr->zToken];
184294      pCsr->zToken = zNew;
184295      pCsr->nAlloc += 64;
184296    }
184297
184298    /* Write the folded case of the last character read to the output */
184299    zEnd = z;
184300    iOut = sqlite3FtsUnicodeFold((int)iCode, p->eRemoveDiacritic);
184301    if( iOut ){
184302      WRITE_UTF8(zOut, iOut);
184303    }
184304
184305    /* If the cursor is not at EOF, read the next character */
184306    if( z>=zTerm ) break;
184307    READ_UTF8(z, zTerm, iCode);
184308  }while( unicodeIsAlnum(p, (int)iCode)
184309       || sqlite3FtsUnicodeIsdiacritic((int)iCode)
184310  );
184311
184312  /* Set the output variables and return. */
184313  pCsr->iOff = (int)(z - pCsr->aInput);
184314  *paToken = pCsr->zToken;
184315  *pnToken = (int)(zOut - pCsr->zToken);
184316  *piStart = (int)(zStart - pCsr->aInput);
184317  *piEnd = (int)(zEnd - pCsr->aInput);
184318  *piPos = pCsr->iToken++;
184319  return SQLITE_OK;
184320}
184321
184322/*
184323** Set *ppModule to a pointer to the sqlite3_tokenizer_module
184324** structure for the unicode tokenizer.
184325*/
184326SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
184327  static const sqlite3_tokenizer_module module = {
184328    0,
184329    unicodeCreate,
184330    unicodeDestroy,
184331    unicodeOpen,
184332    unicodeClose,
184333    unicodeNext,
184334    0,
184335  };
184336  *ppModule = &module;
184337}
184338
184339#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
184340#endif /* ifndef SQLITE_DISABLE_FTS3_UNICODE */
184341
184342/************** End of fts3_unicode.c ****************************************/
184343/************** Begin file fts3_unicode2.c ***********************************/
184344/*
184345** 2012-05-25
184346**
184347** The author disclaims copyright to this source code.  In place of
184348** a legal notice, here is a blessing:
184349**
184350**    May you do good and not evil.
184351**    May you find forgiveness for yourself and forgive others.
184352**    May you share freely, never taking more than you give.
184353**
184354******************************************************************************
184355*/
184356
184357/*
184358** DO NOT EDIT THIS MACHINE GENERATED FILE.
184359*/
184360
184361#ifndef SQLITE_DISABLE_FTS3_UNICODE
184362#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
184363
184364/* #include <assert.h> */
184365
184366/*
184367** Return true if the argument corresponds to a unicode codepoint
184368** classified as either a letter or a number. Otherwise false.
184369**
184370** The results are undefined if the value passed to this function
184371** is less than zero.
184372*/
184373SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int c){
184374  /* Each unsigned integer in the following array corresponds to a contiguous
184375  ** range of unicode codepoints that are not either letters or numbers (i.e.
184376  ** codepoints for which this function should return 0).
184377  **
184378  ** The most significant 22 bits in each 32-bit value contain the first
184379  ** codepoint in the range. The least significant 10 bits are used to store
184380  ** the size of the range (always at least 1). In other words, the value
184381  ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
184382  ** C. It is not possible to represent a range larger than 1023 codepoints
184383  ** using this format.
184384  */
184385  static const unsigned int aEntry[] = {
184386    0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
184387    0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
184388    0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
184389    0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
184390    0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
184391    0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
184392    0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
184393    0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
184394    0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
184395    0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
184396    0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
184397    0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
184398    0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
184399    0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
184400    0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
184401    0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
184402    0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
184403    0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
184404    0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
184405    0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
184406    0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
184407    0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
184408    0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
184409    0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
184410    0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
184411    0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
184412    0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
184413    0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
184414    0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
184415    0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
184416    0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
184417    0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
184418    0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
184419    0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
184420    0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
184421    0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
184422    0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
184423    0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
184424    0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
184425    0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
184426    0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
184427    0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
184428    0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
184429    0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
184430    0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
184431    0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
184432    0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
184433    0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
184434    0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
184435    0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
184436    0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
184437    0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
184438    0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
184439    0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
184440    0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
184441    0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
184442    0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
184443    0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
184444    0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
184445    0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
184446    0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
184447    0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802,
184448    0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013,
184449    0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06,
184450    0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003,
184451    0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01,
184452    0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403,
184453    0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009,
184454    0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003,
184455    0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003,
184456    0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E,
184457    0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046,
184458    0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401,
184459    0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401,
184460    0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F,
184461    0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C,
184462    0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002,
184463    0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025,
184464    0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6,
184465    0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46,
184466    0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
184467    0x380400F0,
184468  };
184469  static const unsigned int aAscii[4] = {
184470    0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
184471  };
184472
184473  if( (unsigned int)c<128 ){
184474    return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
184475  }else if( (unsigned int)c<(1<<22) ){
184476    unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
184477    int iRes = 0;
184478    int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
184479    int iLo = 0;
184480    while( iHi>=iLo ){
184481      int iTest = (iHi + iLo) / 2;
184482      if( key >= aEntry[iTest] ){
184483        iRes = iTest;
184484        iLo = iTest+1;
184485      }else{
184486        iHi = iTest-1;
184487      }
184488    }
184489    assert( aEntry[0]<key );
184490    assert( key>=aEntry[iRes] );
184491    return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
184492  }
184493  return 1;
184494}
184495
184496
184497/*
184498** If the argument is a codepoint corresponding to a lowercase letter
184499** in the ASCII range with a diacritic added, return the codepoint
184500** of the ASCII letter only. For example, if passed 235 - "LATIN
184501** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
184502** E"). The resuls of passing a codepoint that corresponds to an
184503** uppercase letter are undefined.
184504*/
184505static int remove_diacritic(int c, int bComplex){
184506  unsigned short aDia[] = {
184507        0,  1797,  1848,  1859,  1891,  1928,  1940,  1995,
184508     2024,  2040,  2060,  2110,  2168,  2206,  2264,  2286,
184509     2344,  2383,  2472,  2488,  2516,  2596,  2668,  2732,
184510     2782,  2842,  2894,  2954,  2984,  3000,  3028,  3336,
184511     3456,  3696,  3712,  3728,  3744,  3766,  3832,  3896,
184512     3912,  3928,  3944,  3968,  4008,  4040,  4056,  4106,
184513     4138,  4170,  4202,  4234,  4266,  4296,  4312,  4344,
184514     4408,  4424,  4442,  4472,  4488,  4504,  6148,  6198,
184515     6264,  6280,  6360,  6429,  6505,  6529, 61448, 61468,
184516    61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704,
184517    61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914,
184518    61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218,
184519    62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554,
184520    62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766,
184521    62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118,
184522    63182, 63242, 63274, 63310, 63368, 63390,
184523  };
184524#define HIBIT ((unsigned char)0x80)
184525  unsigned char aChar[] = {
184526    '\0',      'a',       'c',       'e',       'i',       'n',
184527    'o',       'u',       'y',       'y',       'a',       'c',
184528    'd',       'e',       'e',       'g',       'h',       'i',
184529    'j',       'k',       'l',       'n',       'o',       'r',
184530    's',       't',       'u',       'u',       'w',       'y',
184531    'z',       'o',       'u',       'a',       'i',       'o',
184532    'u',       'u'|HIBIT, 'a'|HIBIT, 'g',       'k',       'o',
184533    'o'|HIBIT, 'j',       'g',       'n',       'a'|HIBIT, 'a',
184534    'e',       'i',       'o',       'r',       'u',       's',
184535    't',       'h',       'a',       'e',       'o'|HIBIT, 'o',
184536    'o'|HIBIT, 'y',       '\0',      '\0',      '\0',      '\0',
184537    '\0',      '\0',      '\0',      '\0',      'a',       'b',
184538    'c'|HIBIT, 'd',       'd',       'e'|HIBIT, 'e',       'e'|HIBIT,
184539    'f',       'g',       'h',       'h',       'i',       'i'|HIBIT,
184540    'k',       'l',       'l'|HIBIT, 'l',       'm',       'n',
184541    'o'|HIBIT, 'p',       'r',       'r'|HIBIT, 'r',       's',
184542    's'|HIBIT, 't',       'u',       'u'|HIBIT, 'v',       'w',
184543    'w',       'x',       'y',       'z',       'h',       't',
184544    'w',       'y',       'a',       'a'|HIBIT, 'a'|HIBIT, 'a'|HIBIT,
184545    'e',       'e'|HIBIT, 'e'|HIBIT, 'i',       'o',       'o'|HIBIT,
184546    'o'|HIBIT, 'o'|HIBIT, 'u',       'u'|HIBIT, 'u'|HIBIT, 'y',
184547  };
184548
184549  unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
184550  int iRes = 0;
184551  int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
184552  int iLo = 0;
184553  while( iHi>=iLo ){
184554    int iTest = (iHi + iLo) / 2;
184555    if( key >= aDia[iTest] ){
184556      iRes = iTest;
184557      iLo = iTest+1;
184558    }else{
184559      iHi = iTest-1;
184560    }
184561  }
184562  assert( key>=aDia[iRes] );
184563  if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
184564  return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
184565}
184566
184567
184568/*
184569** Return true if the argument interpreted as a unicode codepoint
184570** is a diacritical modifier character.
184571*/
184572SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int c){
184573  unsigned int mask0 = 0x08029FDF;
184574  unsigned int mask1 = 0x000361F8;
184575  if( c<768 || c>817 ) return 0;
184576  return (c < 768+32) ?
184577      (mask0 & ((unsigned int)1 << (c-768))) :
184578      (mask1 & ((unsigned int)1 << (c-768-32)));
184579}
184580
184581
184582/*
184583** Interpret the argument as a unicode codepoint. If the codepoint
184584** is an upper case character that has a lower case equivalent,
184585** return the codepoint corresponding to the lower case version.
184586** Otherwise, return a copy of the argument.
184587**
184588** The results are undefined if the value passed to this function
184589** is less than zero.
184590*/
184591SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int eRemoveDiacritic){
184592  /* Each entry in the following array defines a rule for folding a range
184593  ** of codepoints to lower case. The rule applies to a range of nRange
184594  ** codepoints starting at codepoint iCode.
184595  **
184596  ** If the least significant bit in flags is clear, then the rule applies
184597  ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
184598  ** need to be folded). Or, if it is set, then the rule only applies to
184599  ** every second codepoint in the range, starting with codepoint C.
184600  **
184601  ** The 7 most significant bits in flags are an index into the aiOff[]
184602  ** array. If a specific codepoint C does require folding, then its lower
184603  ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
184604  **
184605  ** The contents of this array are generated by parsing the CaseFolding.txt
184606  ** file distributed as part of the "Unicode Character Database". See
184607  ** http://www.unicode.org for details.
184608  */
184609  static const struct TableEntry {
184610    unsigned short iCode;
184611    unsigned char flags;
184612    unsigned char nRange;
184613  } aEntry[] = {
184614    {65, 14, 26},          {181, 64, 1},          {192, 14, 23},
184615    {216, 14, 7},          {256, 1, 48},          {306, 1, 6},
184616    {313, 1, 16},          {330, 1, 46},          {376, 116, 1},
184617    {377, 1, 6},           {383, 104, 1},         {385, 50, 1},
184618    {386, 1, 4},           {390, 44, 1},          {391, 0, 1},
184619    {393, 42, 2},          {395, 0, 1},           {398, 32, 1},
184620    {399, 38, 1},          {400, 40, 1},          {401, 0, 1},
184621    {403, 42, 1},          {404, 46, 1},          {406, 52, 1},
184622    {407, 48, 1},          {408, 0, 1},           {412, 52, 1},
184623    {413, 54, 1},          {415, 56, 1},          {416, 1, 6},
184624    {422, 60, 1},          {423, 0, 1},           {425, 60, 1},
184625    {428, 0, 1},           {430, 60, 1},          {431, 0, 1},
184626    {433, 58, 2},          {435, 1, 4},           {439, 62, 1},
184627    {440, 0, 1},           {444, 0, 1},           {452, 2, 1},
184628    {453, 0, 1},           {455, 2, 1},           {456, 0, 1},
184629    {458, 2, 1},           {459, 1, 18},          {478, 1, 18},
184630    {497, 2, 1},           {498, 1, 4},           {502, 122, 1},
184631    {503, 134, 1},         {504, 1, 40},          {544, 110, 1},
184632    {546, 1, 18},          {570, 70, 1},          {571, 0, 1},
184633    {573, 108, 1},         {574, 68, 1},          {577, 0, 1},
184634    {579, 106, 1},         {580, 28, 1},          {581, 30, 1},
184635    {582, 1, 10},          {837, 36, 1},          {880, 1, 4},
184636    {886, 0, 1},           {902, 18, 1},          {904, 16, 3},
184637    {908, 26, 1},          {910, 24, 2},          {913, 14, 17},
184638    {931, 14, 9},          {962, 0, 1},           {975, 4, 1},
184639    {976, 140, 1},         {977, 142, 1},         {981, 146, 1},
184640    {982, 144, 1},         {984, 1, 24},          {1008, 136, 1},
184641    {1009, 138, 1},        {1012, 130, 1},        {1013, 128, 1},
184642    {1015, 0, 1},          {1017, 152, 1},        {1018, 0, 1},
184643    {1021, 110, 3},        {1024, 34, 16},        {1040, 14, 32},
184644    {1120, 1, 34},         {1162, 1, 54},         {1216, 6, 1},
184645    {1217, 1, 14},         {1232, 1, 88},         {1329, 22, 38},
184646    {4256, 66, 38},        {4295, 66, 1},         {4301, 66, 1},
184647    {7680, 1, 150},        {7835, 132, 1},        {7838, 96, 1},
184648    {7840, 1, 96},         {7944, 150, 8},        {7960, 150, 6},
184649    {7976, 150, 8},        {7992, 150, 8},        {8008, 150, 6},
184650    {8025, 151, 8},        {8040, 150, 8},        {8072, 150, 8},
184651    {8088, 150, 8},        {8104, 150, 8},        {8120, 150, 2},
184652    {8122, 126, 2},        {8124, 148, 1},        {8126, 100, 1},
184653    {8136, 124, 4},        {8140, 148, 1},        {8152, 150, 2},
184654    {8154, 120, 2},        {8168, 150, 2},        {8170, 118, 2},
184655    {8172, 152, 1},        {8184, 112, 2},        {8186, 114, 2},
184656    {8188, 148, 1},        {8486, 98, 1},         {8490, 92, 1},
184657    {8491, 94, 1},         {8498, 12, 1},         {8544, 8, 16},
184658    {8579, 0, 1},          {9398, 10, 26},        {11264, 22, 47},
184659    {11360, 0, 1},         {11362, 88, 1},        {11363, 102, 1},
184660    {11364, 90, 1},        {11367, 1, 6},         {11373, 84, 1},
184661    {11374, 86, 1},        {11375, 80, 1},        {11376, 82, 1},
184662    {11378, 0, 1},         {11381, 0, 1},         {11390, 78, 2},
184663    {11392, 1, 100},       {11499, 1, 4},         {11506, 0, 1},
184664    {42560, 1, 46},        {42624, 1, 24},        {42786, 1, 14},
184665    {42802, 1, 62},        {42873, 1, 4},         {42877, 76, 1},
184666    {42878, 1, 10},        {42891, 0, 1},         {42893, 74, 1},
184667    {42896, 1, 4},         {42912, 1, 10},        {42922, 72, 1},
184668    {65313, 14, 26},
184669  };
184670  static const unsigned short aiOff[] = {
184671   1,     2,     8,     15,    16,    26,    28,    32,
184672   37,    38,    40,    48,    63,    64,    69,    71,
184673   79,    80,    116,   202,   203,   205,   206,   207,
184674   209,   210,   211,   213,   214,   217,   218,   219,
184675   775,   7264,  10792, 10795, 23228, 23256, 30204, 54721,
184676   54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
184677   57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
184678   65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
184679   65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
184680   65514, 65521, 65527, 65528, 65529,
184681  };
184682
184683  int ret = c;
184684
184685  assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
184686
184687  if( c<128 ){
184688    if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
184689  }else if( c<65536 ){
184690    const struct TableEntry *p;
184691    int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
184692    int iLo = 0;
184693    int iRes = -1;
184694
184695    assert( c>aEntry[0].iCode );
184696    while( iHi>=iLo ){
184697      int iTest = (iHi + iLo) / 2;
184698      int cmp = (c - aEntry[iTest].iCode);
184699      if( cmp>=0 ){
184700        iRes = iTest;
184701        iLo = iTest+1;
184702      }else{
184703        iHi = iTest-1;
184704      }
184705    }
184706
184707    assert( iRes>=0 && c>=aEntry[iRes].iCode );
184708    p = &aEntry[iRes];
184709    if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
184710      ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
184711      assert( ret>0 );
184712    }
184713
184714    if( eRemoveDiacritic ){
184715      ret = remove_diacritic(ret, eRemoveDiacritic==2);
184716    }
184717  }
184718
184719  else if( c>=66560 && c<66600 ){
184720    ret = c + 40;
184721  }
184722
184723  return ret;
184724}
184725#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
184726#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */
184727
184728/************** End of fts3_unicode2.c ***************************************/
184729/************** Begin file json1.c *******************************************/
184730/*
184731** 2015-08-12
184732**
184733** The author disclaims copyright to this source code.  In place of
184734** a legal notice, here is a blessing:
184735**
184736**    May you do good and not evil.
184737**    May you find forgiveness for yourself and forgive others.
184738**    May you share freely, never taking more than you give.
184739**
184740******************************************************************************
184741**
184742** This SQLite extension implements JSON functions.  The interface is
184743** modeled after MySQL JSON functions:
184744**
184745**     https://dev.mysql.com/doc/refman/5.7/en/json.html
184746**
184747** For the time being, all JSON is stored as pure text.  (We might add
184748** a JSONB type in the future which stores a binary encoding of JSON in
184749** a BLOB, but there is no support for JSONB in the current implementation.
184750** This implementation parses JSON text at 250 MB/s, so it is hard to see
184751** how JSONB might improve on that.)
184752*/
184753#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
184754#if !defined(SQLITEINT_H)
184755/* #include "sqlite3ext.h" */
184756#endif
184757SQLITE_EXTENSION_INIT1
184758/* #include <assert.h> */
184759/* #include <string.h> */
184760/* #include <stdlib.h> */
184761/* #include <stdarg.h> */
184762
184763/* Mark a function parameter as unused, to suppress nuisance compiler
184764** warnings. */
184765#ifndef UNUSED_PARAM
184766# define UNUSED_PARAM(X)  (void)(X)
184767#endif
184768
184769#ifndef LARGEST_INT64
184770# define LARGEST_INT64  (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
184771# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
184772#endif
184773
184774/*
184775** Versions of isspace(), isalnum() and isdigit() to which it is safe
184776** to pass signed char values.
184777*/
184778#ifdef sqlite3Isdigit
184779   /* Use the SQLite core versions if this routine is part of the
184780   ** SQLite amalgamation */
184781#  define safe_isdigit(x)  sqlite3Isdigit(x)
184782#  define safe_isalnum(x)  sqlite3Isalnum(x)
184783#  define safe_isxdigit(x) sqlite3Isxdigit(x)
184784#else
184785   /* Use the standard library for separate compilation */
184786#include <ctype.h>  /* amalgamator: keep */
184787#  define safe_isdigit(x)  isdigit((unsigned char)(x))
184788#  define safe_isalnum(x)  isalnum((unsigned char)(x))
184789#  define safe_isxdigit(x) isxdigit((unsigned char)(x))
184790#endif
184791
184792/*
184793** Growing our own isspace() routine this way is twice as fast as
184794** the library isspace() function, resulting in a 7% overall performance
184795** increase for the parser.  (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
184796*/
184797static const char jsonIsSpace[] = {
184798  0, 0, 0, 0, 0, 0, 0, 0,     0, 1, 1, 0, 0, 1, 0, 0,
184799  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
184800  1, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
184801  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
184802  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
184803  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
184804  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
184805  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
184806  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
184807  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
184808  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
184809  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
184810  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
184811  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
184812  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
184813  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
184814};
184815#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
184816
184817#ifndef SQLITE_AMALGAMATION
184818  /* Unsigned integer types.  These are already defined in the sqliteInt.h,
184819  ** but the definitions need to be repeated for separate compilation. */
184820  typedef sqlite3_uint64 u64;
184821  typedef unsigned int u32;
184822  typedef unsigned short int u16;
184823  typedef unsigned char u8;
184824#endif
184825
184826/* Objects */
184827typedef struct JsonString JsonString;
184828typedef struct JsonNode JsonNode;
184829typedef struct JsonParse JsonParse;
184830
184831/* An instance of this object represents a JSON string
184832** under construction.  Really, this is a generic string accumulator
184833** that can be and is used to create strings other than JSON.
184834*/
184835struct JsonString {
184836  sqlite3_context *pCtx;   /* Function context - put error messages here */
184837  char *zBuf;              /* Append JSON content here */
184838  u64 nAlloc;              /* Bytes of storage available in zBuf[] */
184839  u64 nUsed;               /* Bytes of zBuf[] currently used */
184840  u8 bStatic;              /* True if zBuf is static space */
184841  u8 bErr;                 /* True if an error has been encountered */
184842  char zSpace[100];        /* Initial static space */
184843};
184844
184845/* JSON type values
184846*/
184847#define JSON_NULL     0
184848#define JSON_TRUE     1
184849#define JSON_FALSE    2
184850#define JSON_INT      3
184851#define JSON_REAL     4
184852#define JSON_STRING   5
184853#define JSON_ARRAY    6
184854#define JSON_OBJECT   7
184855
184856/* The "subtype" set for JSON values */
184857#define JSON_SUBTYPE  74    /* Ascii for "J" */
184858
184859/*
184860** Names of the various JSON types:
184861*/
184862static const char * const jsonType[] = {
184863  "null", "true", "false", "integer", "real", "text", "array", "object"
184864};
184865
184866/* Bit values for the JsonNode.jnFlag field
184867*/
184868#define JNODE_RAW     0x01         /* Content is raw, not JSON encoded */
184869#define JNODE_ESCAPE  0x02         /* Content is text with \ escapes */
184870#define JNODE_REMOVE  0x04         /* Do not output */
184871#define JNODE_REPLACE 0x08         /* Replace with JsonNode.u.iReplace */
184872#define JNODE_PATCH   0x10         /* Patch with JsonNode.u.pPatch */
184873#define JNODE_APPEND  0x20         /* More ARRAY/OBJECT entries at u.iAppend */
184874#define JNODE_LABEL   0x40         /* Is a label of an object */
184875
184876
184877/* A single node of parsed JSON
184878*/
184879struct JsonNode {
184880  u8 eType;              /* One of the JSON_ type values */
184881  u8 jnFlags;            /* JNODE flags */
184882  u32 n;                 /* Bytes of content, or number of sub-nodes */
184883  union {
184884    const char *zJContent; /* Content for INT, REAL, and STRING */
184885    u32 iAppend;           /* More terms for ARRAY and OBJECT */
184886    u32 iKey;              /* Key for ARRAY objects in json_tree() */
184887    u32 iReplace;          /* Replacement content for JNODE_REPLACE */
184888    JsonNode *pPatch;      /* Node chain of patch for JNODE_PATCH */
184889  } u;
184890};
184891
184892/* A completely parsed JSON string
184893*/
184894struct JsonParse {
184895  u32 nNode;         /* Number of slots of aNode[] used */
184896  u32 nAlloc;        /* Number of slots of aNode[] allocated */
184897  JsonNode *aNode;   /* Array of nodes containing the parse */
184898  const char *zJson; /* Original JSON string */
184899  u32 *aUp;          /* Index of parent of each node */
184900  u8 oom;            /* Set to true if out of memory */
184901  u8 nErr;           /* Number of errors seen */
184902  u16 iDepth;        /* Nesting depth */
184903  int nJson;         /* Length of the zJson string in bytes */
184904  u32 iHold;         /* Replace cache line with the lowest iHold value */
184905};
184906
184907/*
184908** Maximum nesting depth of JSON for this implementation.
184909**
184910** This limit is needed to avoid a stack overflow in the recursive
184911** descent parser.  A depth of 2000 is far deeper than any sane JSON
184912** should go.
184913*/
184914#define JSON_MAX_DEPTH  2000
184915
184916/**************************************************************************
184917** Utility routines for dealing with JsonString objects
184918**************************************************************************/
184919
184920/* Set the JsonString object to an empty string
184921*/
184922static void jsonZero(JsonString *p){
184923  p->zBuf = p->zSpace;
184924  p->nAlloc = sizeof(p->zSpace);
184925  p->nUsed = 0;
184926  p->bStatic = 1;
184927}
184928
184929/* Initialize the JsonString object
184930*/
184931static void jsonInit(JsonString *p, sqlite3_context *pCtx){
184932  p->pCtx = pCtx;
184933  p->bErr = 0;
184934  jsonZero(p);
184935}
184936
184937
184938/* Free all allocated memory and reset the JsonString object back to its
184939** initial state.
184940*/
184941static void jsonReset(JsonString *p){
184942  if( !p->bStatic ) sqlite3_free(p->zBuf);
184943  jsonZero(p);
184944}
184945
184946
184947/* Report an out-of-memory (OOM) condition
184948*/
184949static void jsonOom(JsonString *p){
184950  p->bErr = 1;
184951  sqlite3_result_error_nomem(p->pCtx);
184952  jsonReset(p);
184953}
184954
184955/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
184956** Return zero on success.  Return non-zero on an OOM error
184957*/
184958static int jsonGrow(JsonString *p, u32 N){
184959  u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
184960  char *zNew;
184961  if( p->bStatic ){
184962    if( p->bErr ) return 1;
184963    zNew = sqlite3_malloc64(nTotal);
184964    if( zNew==0 ){
184965      jsonOom(p);
184966      return SQLITE_NOMEM;
184967    }
184968    memcpy(zNew, p->zBuf, (size_t)p->nUsed);
184969    p->zBuf = zNew;
184970    p->bStatic = 0;
184971  }else{
184972    zNew = sqlite3_realloc64(p->zBuf, nTotal);
184973    if( zNew==0 ){
184974      jsonOom(p);
184975      return SQLITE_NOMEM;
184976    }
184977    p->zBuf = zNew;
184978  }
184979  p->nAlloc = nTotal;
184980  return SQLITE_OK;
184981}
184982
184983/* Append N bytes from zIn onto the end of the JsonString string.
184984*/
184985static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
184986  if( N==0 ) return;
184987  if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
184988  memcpy(p->zBuf+p->nUsed, zIn, N);
184989  p->nUsed += N;
184990}
184991
184992/* Append formatted text (not to exceed N bytes) to the JsonString.
184993*/
184994static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
184995  va_list ap;
184996  if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
184997  va_start(ap, zFormat);
184998  sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
184999  va_end(ap);
185000  p->nUsed += (int)strlen(p->zBuf+p->nUsed);
185001}
185002
185003/* Append a single character
185004*/
185005static void jsonAppendChar(JsonString *p, char c){
185006  if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
185007  p->zBuf[p->nUsed++] = c;
185008}
185009
185010/* Append a comma separator to the output buffer, if the previous
185011** character is not '[' or '{'.
185012*/
185013static void jsonAppendSeparator(JsonString *p){
185014  char c;
185015  if( p->nUsed==0 ) return;
185016  c = p->zBuf[p->nUsed-1];
185017  if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
185018}
185019
185020/* Append the N-byte string in zIn to the end of the JsonString string
185021** under construction.  Enclose the string in "..." and escape
185022** any double-quotes or backslash characters contained within the
185023** string.
185024*/
185025static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
185026  u32 i;
185027  if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
185028  p->zBuf[p->nUsed++] = '"';
185029  for(i=0; i<N; i++){
185030    unsigned char c = ((unsigned const char*)zIn)[i];
185031    if( c=='"' || c=='\\' ){
185032      json_simple_escape:
185033      if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
185034      p->zBuf[p->nUsed++] = '\\';
185035    }else if( c<=0x1f ){
185036      static const char aSpecial[] = {
185037         0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
185038         0, 0, 0, 0, 0, 0, 0, 0,   0,   0,   0, 0,   0,   0, 0, 0
185039      };
185040      assert( sizeof(aSpecial)==32 );
185041      assert( aSpecial['\b']=='b' );
185042      assert( aSpecial['\f']=='f' );
185043      assert( aSpecial['\n']=='n' );
185044      assert( aSpecial['\r']=='r' );
185045      assert( aSpecial['\t']=='t' );
185046      if( aSpecial[c] ){
185047        c = aSpecial[c];
185048        goto json_simple_escape;
185049      }
185050      if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
185051      p->zBuf[p->nUsed++] = '\\';
185052      p->zBuf[p->nUsed++] = 'u';
185053      p->zBuf[p->nUsed++] = '0';
185054      p->zBuf[p->nUsed++] = '0';
185055      p->zBuf[p->nUsed++] = '0' + (c>>4);
185056      c = "0123456789abcdef"[c&0xf];
185057    }
185058    p->zBuf[p->nUsed++] = c;
185059  }
185060  p->zBuf[p->nUsed++] = '"';
185061  assert( p->nUsed<p->nAlloc );
185062}
185063
185064/*
185065** Append a function parameter value to the JSON string under
185066** construction.
185067*/
185068static void jsonAppendValue(
185069  JsonString *p,                 /* Append to this JSON string */
185070  sqlite3_value *pValue          /* Value to append */
185071){
185072  switch( sqlite3_value_type(pValue) ){
185073    case SQLITE_NULL: {
185074      jsonAppendRaw(p, "null", 4);
185075      break;
185076    }
185077    case SQLITE_INTEGER:
185078    case SQLITE_FLOAT: {
185079      const char *z = (const char*)sqlite3_value_text(pValue);
185080      u32 n = (u32)sqlite3_value_bytes(pValue);
185081      jsonAppendRaw(p, z, n);
185082      break;
185083    }
185084    case SQLITE_TEXT: {
185085      const char *z = (const char*)sqlite3_value_text(pValue);
185086      u32 n = (u32)sqlite3_value_bytes(pValue);
185087      if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
185088        jsonAppendRaw(p, z, n);
185089      }else{
185090        jsonAppendString(p, z, n);
185091      }
185092      break;
185093    }
185094    default: {
185095      if( p->bErr==0 ){
185096        sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
185097        p->bErr = 2;
185098        jsonReset(p);
185099      }
185100      break;
185101    }
185102  }
185103}
185104
185105
185106/* Make the JSON in p the result of the SQL function.
185107*/
185108static void jsonResult(JsonString *p){
185109  if( p->bErr==0 ){
185110    sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
185111                          p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
185112                          SQLITE_UTF8);
185113    jsonZero(p);
185114  }
185115  assert( p->bStatic );
185116}
185117
185118/**************************************************************************
185119** Utility routines for dealing with JsonNode and JsonParse objects
185120**************************************************************************/
185121
185122/*
185123** Return the number of consecutive JsonNode slots need to represent
185124** the parsed JSON at pNode.  The minimum answer is 1.  For ARRAY and
185125** OBJECT types, the number might be larger.
185126**
185127** Appended elements are not counted.  The value returned is the number
185128** by which the JsonNode counter should increment in order to go to the
185129** next peer value.
185130*/
185131static u32 jsonNodeSize(JsonNode *pNode){
185132  return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
185133}
185134
185135/*
185136** Reclaim all memory allocated by a JsonParse object.  But do not
185137** delete the JsonParse object itself.
185138*/
185139static void jsonParseReset(JsonParse *pParse){
185140  sqlite3_free(pParse->aNode);
185141  pParse->aNode = 0;
185142  pParse->nNode = 0;
185143  pParse->nAlloc = 0;
185144  sqlite3_free(pParse->aUp);
185145  pParse->aUp = 0;
185146}
185147
185148/*
185149** Free a JsonParse object that was obtained from sqlite3_malloc().
185150*/
185151static void jsonParseFree(JsonParse *pParse){
185152  jsonParseReset(pParse);
185153  sqlite3_free(pParse);
185154}
185155
185156/*
185157** Convert the JsonNode pNode into a pure JSON string and
185158** append to pOut.  Subsubstructure is also included.  Return
185159** the number of JsonNode objects that are encoded.
185160*/
185161static void jsonRenderNode(
185162  JsonNode *pNode,               /* The node to render */
185163  JsonString *pOut,              /* Write JSON here */
185164  sqlite3_value **aReplace       /* Replacement values */
185165){
185166  if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
185167    if( pNode->jnFlags & JNODE_REPLACE ){
185168      jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
185169      return;
185170    }
185171    pNode = pNode->u.pPatch;
185172  }
185173  switch( pNode->eType ){
185174    default: {
185175      assert( pNode->eType==JSON_NULL );
185176      jsonAppendRaw(pOut, "null", 4);
185177      break;
185178    }
185179    case JSON_TRUE: {
185180      jsonAppendRaw(pOut, "true", 4);
185181      break;
185182    }
185183    case JSON_FALSE: {
185184      jsonAppendRaw(pOut, "false", 5);
185185      break;
185186    }
185187    case JSON_STRING: {
185188      if( pNode->jnFlags & JNODE_RAW ){
185189        jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
185190        break;
185191      }
185192      /* Fall through into the next case */
185193    }
185194    case JSON_REAL:
185195    case JSON_INT: {
185196      jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
185197      break;
185198    }
185199    case JSON_ARRAY: {
185200      u32 j = 1;
185201      jsonAppendChar(pOut, '[');
185202      for(;;){
185203        while( j<=pNode->n ){
185204          if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
185205            jsonAppendSeparator(pOut);
185206            jsonRenderNode(&pNode[j], pOut, aReplace);
185207          }
185208          j += jsonNodeSize(&pNode[j]);
185209        }
185210        if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
185211        pNode = &pNode[pNode->u.iAppend];
185212        j = 1;
185213      }
185214      jsonAppendChar(pOut, ']');
185215      break;
185216    }
185217    case JSON_OBJECT: {
185218      u32 j = 1;
185219      jsonAppendChar(pOut, '{');
185220      for(;;){
185221        while( j<=pNode->n ){
185222          if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
185223            jsonAppendSeparator(pOut);
185224            jsonRenderNode(&pNode[j], pOut, aReplace);
185225            jsonAppendChar(pOut, ':');
185226            jsonRenderNode(&pNode[j+1], pOut, aReplace);
185227          }
185228          j += 1 + jsonNodeSize(&pNode[j+1]);
185229        }
185230        if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
185231        pNode = &pNode[pNode->u.iAppend];
185232        j = 1;
185233      }
185234      jsonAppendChar(pOut, '}');
185235      break;
185236    }
185237  }
185238}
185239
185240/*
185241** Return a JsonNode and all its descendents as a JSON string.
185242*/
185243static void jsonReturnJson(
185244  JsonNode *pNode,            /* Node to return */
185245  sqlite3_context *pCtx,      /* Return value for this function */
185246  sqlite3_value **aReplace    /* Array of replacement values */
185247){
185248  JsonString s;
185249  jsonInit(&s, pCtx);
185250  jsonRenderNode(pNode, &s, aReplace);
185251  jsonResult(&s);
185252  sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
185253}
185254
185255/*
185256** Translate a single byte of Hex into an integer.
185257** This routine only works if h really is a valid hexadecimal
185258** character:  0..9a..fA..F
185259*/
185260static u8 jsonHexToInt(int h){
185261  assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
185262#ifdef SQLITE_EBCDIC
185263  h += 9*(1&~(h>>4));
185264#else
185265  h += 9*(1&(h>>6));
185266#endif
185267  return (u8)(h & 0xf);
185268}
185269
185270/*
185271** Convert a 4-byte hex string into an integer
185272*/
185273static u32 jsonHexToInt4(const char *z){
185274  u32 v;
185275  assert( safe_isxdigit(z[0]) );
185276  assert( safe_isxdigit(z[1]) );
185277  assert( safe_isxdigit(z[2]) );
185278  assert( safe_isxdigit(z[3]) );
185279  v = (jsonHexToInt(z[0])<<12)
185280    + (jsonHexToInt(z[1])<<8)
185281    + (jsonHexToInt(z[2])<<4)
185282    + jsonHexToInt(z[3]);
185283  return v;
185284}
185285
185286/*
185287** Make the JsonNode the return value of the function.
185288*/
185289static void jsonReturn(
185290  JsonNode *pNode,            /* Node to return */
185291  sqlite3_context *pCtx,      /* Return value for this function */
185292  sqlite3_value **aReplace    /* Array of replacement values */
185293){
185294  switch( pNode->eType ){
185295    default: {
185296      assert( pNode->eType==JSON_NULL );
185297      sqlite3_result_null(pCtx);
185298      break;
185299    }
185300    case JSON_TRUE: {
185301      sqlite3_result_int(pCtx, 1);
185302      break;
185303    }
185304    case JSON_FALSE: {
185305      sqlite3_result_int(pCtx, 0);
185306      break;
185307    }
185308    case JSON_INT: {
185309      sqlite3_int64 i = 0;
185310      const char *z = pNode->u.zJContent;
185311      if( z[0]=='-' ){ z++; }
185312      while( z[0]>='0' && z[0]<='9' ){
185313        unsigned v = *(z++) - '0';
185314        if( i>=LARGEST_INT64/10 ){
185315          if( i>LARGEST_INT64/10 ) goto int_as_real;
185316          if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
185317          if( v==9 ) goto int_as_real;
185318          if( v==8 ){
185319            if( pNode->u.zJContent[0]=='-' ){
185320              sqlite3_result_int64(pCtx, SMALLEST_INT64);
185321              goto int_done;
185322            }else{
185323              goto int_as_real;
185324            }
185325          }
185326        }
185327        i = i*10 + v;
185328      }
185329      if( pNode->u.zJContent[0]=='-' ){ i = -i; }
185330      sqlite3_result_int64(pCtx, i);
185331      int_done:
185332      break;
185333      int_as_real: /* fall through to real */;
185334    }
185335    case JSON_REAL: {
185336      double r;
185337#ifdef SQLITE_AMALGAMATION
185338      const char *z = pNode->u.zJContent;
185339      sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
185340#else
185341      r = strtod(pNode->u.zJContent, 0);
185342#endif
185343      sqlite3_result_double(pCtx, r);
185344      break;
185345    }
185346    case JSON_STRING: {
185347#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
185348      ** json_insert() and json_replace() and those routines do not
185349      ** call jsonReturn() */
185350      if( pNode->jnFlags & JNODE_RAW ){
185351        sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
185352                            SQLITE_TRANSIENT);
185353      }else
185354#endif
185355      assert( (pNode->jnFlags & JNODE_RAW)==0 );
185356      if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
185357        /* JSON formatted without any backslash-escapes */
185358        sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
185359                            SQLITE_TRANSIENT);
185360      }else{
185361        /* Translate JSON formatted string into raw text */
185362        u32 i;
185363        u32 n = pNode->n;
185364        const char *z = pNode->u.zJContent;
185365        char *zOut;
185366        u32 j;
185367        zOut = sqlite3_malloc( n+1 );
185368        if( zOut==0 ){
185369          sqlite3_result_error_nomem(pCtx);
185370          break;
185371        }
185372        for(i=1, j=0; i<n-1; i++){
185373          char c = z[i];
185374          if( c!='\\' ){
185375            zOut[j++] = c;
185376          }else{
185377            c = z[++i];
185378            if( c=='u' ){
185379              u32 v = jsonHexToInt4(z+i+1);
185380              i += 4;
185381              if( v==0 ) break;
185382              if( v<=0x7f ){
185383                zOut[j++] = (char)v;
185384              }else if( v<=0x7ff ){
185385                zOut[j++] = (char)(0xc0 | (v>>6));
185386                zOut[j++] = 0x80 | (v&0x3f);
185387              }else{
185388                u32 vlo;
185389                if( (v&0xfc00)==0xd800
185390                  && i<n-6
185391                  && z[i+1]=='\\'
185392                  && z[i+2]=='u'
185393                  && ((vlo = jsonHexToInt4(z+i+3))&0xfc00)==0xdc00
185394                ){
185395                  /* We have a surrogate pair */
185396                  v = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
185397                  i += 6;
185398                  zOut[j++] = 0xf0 | (v>>18);
185399                  zOut[j++] = 0x80 | ((v>>12)&0x3f);
185400                  zOut[j++] = 0x80 | ((v>>6)&0x3f);
185401                  zOut[j++] = 0x80 | (v&0x3f);
185402                }else{
185403                  zOut[j++] = 0xe0 | (v>>12);
185404                  zOut[j++] = 0x80 | ((v>>6)&0x3f);
185405                  zOut[j++] = 0x80 | (v&0x3f);
185406                }
185407              }
185408            }else{
185409              if( c=='b' ){
185410                c = '\b';
185411              }else if( c=='f' ){
185412                c = '\f';
185413              }else if( c=='n' ){
185414                c = '\n';
185415              }else if( c=='r' ){
185416                c = '\r';
185417              }else if( c=='t' ){
185418                c = '\t';
185419              }
185420              zOut[j++] = c;
185421            }
185422          }
185423        }
185424        zOut[j] = 0;
185425        sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
185426      }
185427      break;
185428    }
185429    case JSON_ARRAY:
185430    case JSON_OBJECT: {
185431      jsonReturnJson(pNode, pCtx, aReplace);
185432      break;
185433    }
185434  }
185435}
185436
185437/* Forward reference */
185438static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
185439
185440/*
185441** A macro to hint to the compiler that a function should not be
185442** inlined.
185443*/
185444#if defined(__GNUC__)
185445#  define JSON_NOINLINE  __attribute__((noinline))
185446#elif defined(_MSC_VER) && _MSC_VER>=1310
185447#  define JSON_NOINLINE  __declspec(noinline)
185448#else
185449#  define JSON_NOINLINE
185450#endif
185451
185452
185453static JSON_NOINLINE int jsonParseAddNodeExpand(
185454  JsonParse *pParse,        /* Append the node to this object */
185455  u32 eType,                /* Node type */
185456  u32 n,                    /* Content size or sub-node count */
185457  const char *zContent      /* Content */
185458){
185459  u32 nNew;
185460  JsonNode *pNew;
185461  assert( pParse->nNode>=pParse->nAlloc );
185462  if( pParse->oom ) return -1;
185463  nNew = pParse->nAlloc*2 + 10;
185464  pNew = sqlite3_realloc64(pParse->aNode, sizeof(JsonNode)*nNew);
185465  if( pNew==0 ){
185466    pParse->oom = 1;
185467    return -1;
185468  }
185469  pParse->nAlloc = nNew;
185470  pParse->aNode = pNew;
185471  assert( pParse->nNode<pParse->nAlloc );
185472  return jsonParseAddNode(pParse, eType, n, zContent);
185473}
185474
185475/*
185476** Create a new JsonNode instance based on the arguments and append that
185477** instance to the JsonParse.  Return the index in pParse->aNode[] of the
185478** new node, or -1 if a memory allocation fails.
185479*/
185480static int jsonParseAddNode(
185481  JsonParse *pParse,        /* Append the node to this object */
185482  u32 eType,                /* Node type */
185483  u32 n,                    /* Content size or sub-node count */
185484  const char *zContent      /* Content */
185485){
185486  JsonNode *p;
185487  if( pParse->nNode>=pParse->nAlloc ){
185488    return jsonParseAddNodeExpand(pParse, eType, n, zContent);
185489  }
185490  p = &pParse->aNode[pParse->nNode];
185491  p->eType = (u8)eType;
185492  p->jnFlags = 0;
185493  p->n = n;
185494  p->u.zJContent = zContent;
185495  return pParse->nNode++;
185496}
185497
185498/*
185499** Return true if z[] begins with 4 (or more) hexadecimal digits
185500*/
185501static int jsonIs4Hex(const char *z){
185502  int i;
185503  for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0;
185504  return 1;
185505}
185506
185507/*
185508** Parse a single JSON value which begins at pParse->zJson[i].  Return the
185509** index of the first character past the end of the value parsed.
185510**
185511** Return negative for a syntax error.  Special cases:  return -2 if the
185512** first non-whitespace character is '}' and return -3 if the first
185513** non-whitespace character is ']'.
185514*/
185515static int jsonParseValue(JsonParse *pParse, u32 i){
185516  char c;
185517  u32 j;
185518  int iThis;
185519  int x;
185520  JsonNode *pNode;
185521  const char *z = pParse->zJson;
185522  while( safe_isspace(z[i]) ){ i++; }
185523  if( (c = z[i])=='{' ){
185524    /* Parse object */
185525    iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
185526    if( iThis<0 ) return -1;
185527    for(j=i+1;;j++){
185528      while( safe_isspace(z[j]) ){ j++; }
185529      if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
185530      x = jsonParseValue(pParse, j);
185531      if( x<0 ){
185532        pParse->iDepth--;
185533        if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
185534        return -1;
185535      }
185536      if( pParse->oom ) return -1;
185537      pNode = &pParse->aNode[pParse->nNode-1];
185538      if( pNode->eType!=JSON_STRING ) return -1;
185539      pNode->jnFlags |= JNODE_LABEL;
185540      j = x;
185541      while( safe_isspace(z[j]) ){ j++; }
185542      if( z[j]!=':' ) return -1;
185543      j++;
185544      x = jsonParseValue(pParse, j);
185545      pParse->iDepth--;
185546      if( x<0 ) return -1;
185547      j = x;
185548      while( safe_isspace(z[j]) ){ j++; }
185549      c = z[j];
185550      if( c==',' ) continue;
185551      if( c!='}' ) return -1;
185552      break;
185553    }
185554    pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
185555    return j+1;
185556  }else if( c=='[' ){
185557    /* Parse array */
185558    iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
185559    if( iThis<0 ) return -1;
185560    for(j=i+1;;j++){
185561      while( safe_isspace(z[j]) ){ j++; }
185562      if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
185563      x = jsonParseValue(pParse, j);
185564      pParse->iDepth--;
185565      if( x<0 ){
185566        if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
185567        return -1;
185568      }
185569      j = x;
185570      while( safe_isspace(z[j]) ){ j++; }
185571      c = z[j];
185572      if( c==',' ) continue;
185573      if( c!=']' ) return -1;
185574      break;
185575    }
185576    pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
185577    return j+1;
185578  }else if( c=='"' ){
185579    /* Parse string */
185580    u8 jnFlags = 0;
185581    j = i+1;
185582    for(;;){
185583      c = z[j];
185584      if( (c & ~0x1f)==0 ){
185585        /* Control characters are not allowed in strings */
185586        return -1;
185587      }
185588      if( c=='\\' ){
185589        c = z[++j];
185590        if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
185591           || c=='n' || c=='r' || c=='t'
185592           || (c=='u' && jsonIs4Hex(z+j+1)) ){
185593          jnFlags = JNODE_ESCAPE;
185594        }else{
185595          return -1;
185596        }
185597      }else if( c=='"' ){
185598        break;
185599      }
185600      j++;
185601    }
185602    jsonParseAddNode(pParse, JSON_STRING, j+1-i, &z[i]);
185603    if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
185604    return j+1;
185605  }else if( c=='n'
185606         && strncmp(z+i,"null",4)==0
185607         && !safe_isalnum(z[i+4]) ){
185608    jsonParseAddNode(pParse, JSON_NULL, 0, 0);
185609    return i+4;
185610  }else if( c=='t'
185611         && strncmp(z+i,"true",4)==0
185612         && !safe_isalnum(z[i+4]) ){
185613    jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
185614    return i+4;
185615  }else if( c=='f'
185616         && strncmp(z+i,"false",5)==0
185617         && !safe_isalnum(z[i+5]) ){
185618    jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
185619    return i+5;
185620  }else if( c=='-' || (c>='0' && c<='9') ){
185621    /* Parse number */
185622    u8 seenDP = 0;
185623    u8 seenE = 0;
185624    assert( '-' < '0' );
185625    if( c<='0' ){
185626      j = c=='-' ? i+1 : i;
185627      if( z[j]=='0' && z[j+1]>='0' && z[j+1]<='9' ) return -1;
185628    }
185629    j = i+1;
185630    for(;; j++){
185631      c = z[j];
185632      if( c>='0' && c<='9' ) continue;
185633      if( c=='.' ){
185634        if( z[j-1]=='-' ) return -1;
185635        if( seenDP ) return -1;
185636        seenDP = 1;
185637        continue;
185638      }
185639      if( c=='e' || c=='E' ){
185640        if( z[j-1]<'0' ) return -1;
185641        if( seenE ) return -1;
185642        seenDP = seenE = 1;
185643        c = z[j+1];
185644        if( c=='+' || c=='-' ){
185645          j++;
185646          c = z[j+1];
185647        }
185648        if( c<'0' || c>'9' ) return -1;
185649        continue;
185650      }
185651      break;
185652    }
185653    if( z[j-1]<'0' ) return -1;
185654    jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
185655                        j - i, &z[i]);
185656    return j;
185657  }else if( c=='}' ){
185658    return -2;  /* End of {...} */
185659  }else if( c==']' ){
185660    return -3;  /* End of [...] */
185661  }else if( c==0 ){
185662    return 0;   /* End of file */
185663  }else{
185664    return -1;  /* Syntax error */
185665  }
185666}
185667
185668/*
185669** Parse a complete JSON string.  Return 0 on success or non-zero if there
185670** are any errors.  If an error occurs, free all memory associated with
185671** pParse.
185672**
185673** pParse is uninitialized when this routine is called.
185674*/
185675static int jsonParse(
185676  JsonParse *pParse,           /* Initialize and fill this JsonParse object */
185677  sqlite3_context *pCtx,       /* Report errors here */
185678  const char *zJson            /* Input JSON text to be parsed */
185679){
185680  int i;
185681  memset(pParse, 0, sizeof(*pParse));
185682  if( zJson==0 ) return 1;
185683  pParse->zJson = zJson;
185684  i = jsonParseValue(pParse, 0);
185685  if( pParse->oom ) i = -1;
185686  if( i>0 ){
185687    assert( pParse->iDepth==0 );
185688    while( safe_isspace(zJson[i]) ) i++;
185689    if( zJson[i] ) i = -1;
185690  }
185691  if( i<=0 ){
185692    if( pCtx!=0 ){
185693      if( pParse->oom ){
185694        sqlite3_result_error_nomem(pCtx);
185695      }else{
185696        sqlite3_result_error(pCtx, "malformed JSON", -1);
185697      }
185698    }
185699    jsonParseReset(pParse);
185700    return 1;
185701  }
185702  return 0;
185703}
185704
185705/* Mark node i of pParse as being a child of iParent.  Call recursively
185706** to fill in all the descendants of node i.
185707*/
185708static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
185709  JsonNode *pNode = &pParse->aNode[i];
185710  u32 j;
185711  pParse->aUp[i] = iParent;
185712  switch( pNode->eType ){
185713    case JSON_ARRAY: {
185714      for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
185715        jsonParseFillInParentage(pParse, i+j, i);
185716      }
185717      break;
185718    }
185719    case JSON_OBJECT: {
185720      for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
185721        pParse->aUp[i+j] = i;
185722        jsonParseFillInParentage(pParse, i+j+1, i);
185723      }
185724      break;
185725    }
185726    default: {
185727      break;
185728    }
185729  }
185730}
185731
185732/*
185733** Compute the parentage of all nodes in a completed parse.
185734*/
185735static int jsonParseFindParents(JsonParse *pParse){
185736  u32 *aUp;
185737  assert( pParse->aUp==0 );
185738  aUp = pParse->aUp = sqlite3_malloc64( sizeof(u32)*pParse->nNode );
185739  if( aUp==0 ){
185740    pParse->oom = 1;
185741    return SQLITE_NOMEM;
185742  }
185743  jsonParseFillInParentage(pParse, 0, 0);
185744  return SQLITE_OK;
185745}
185746
185747/*
185748** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
185749*/
185750#define JSON_CACHE_ID  (-429938)  /* First cache entry */
185751#define JSON_CACHE_SZ  4          /* Max number of cache entries */
185752
185753/*
185754** Obtain a complete parse of the JSON found in the first argument
185755** of the argv array.  Use the sqlite3_get_auxdata() cache for this
185756** parse if it is available.  If the cache is not available or if it
185757** is no longer valid, parse the JSON again and return the new parse,
185758** and also register the new parse so that it will be available for
185759** future sqlite3_get_auxdata() calls.
185760*/
185761static JsonParse *jsonParseCached(
185762  sqlite3_context *pCtx,
185763  sqlite3_value **argv,
185764  sqlite3_context *pErrCtx
185765){
185766  const char *zJson = (const char*)sqlite3_value_text(argv[0]);
185767  int nJson = sqlite3_value_bytes(argv[0]);
185768  JsonParse *p;
185769  JsonParse *pMatch = 0;
185770  int iKey;
185771  int iMinKey = 0;
185772  u32 iMinHold = 0xffffffff;
185773  u32 iMaxHold = 0;
185774  if( zJson==0 ) return 0;
185775  for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
185776    p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey);
185777    if( p==0 ){
185778      iMinKey = iKey;
185779      break;
185780    }
185781    if( pMatch==0
185782     && p->nJson==nJson
185783     && memcmp(p->zJson,zJson,nJson)==0
185784    ){
185785      p->nErr = 0;
185786      pMatch = p;
185787    }else if( p->iHold<iMinHold ){
185788      iMinHold = p->iHold;
185789      iMinKey = iKey;
185790    }
185791    if( p->iHold>iMaxHold ){
185792      iMaxHold = p->iHold;
185793    }
185794  }
185795  if( pMatch ){
185796    pMatch->nErr = 0;
185797    pMatch->iHold = iMaxHold+1;
185798    return pMatch;
185799  }
185800  p = sqlite3_malloc64( sizeof(*p) + nJson + 1 );
185801  if( p==0 ){
185802    sqlite3_result_error_nomem(pCtx);
185803    return 0;
185804  }
185805  memset(p, 0, sizeof(*p));
185806  p->zJson = (char*)&p[1];
185807  memcpy((char*)p->zJson, zJson, nJson+1);
185808  if( jsonParse(p, pErrCtx, p->zJson) ){
185809    sqlite3_free(p);
185810    return 0;
185811  }
185812  p->nJson = nJson;
185813  p->iHold = iMaxHold+1;
185814  sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, p,
185815                      (void(*)(void*))jsonParseFree);
185816  return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey);
185817}
185818
185819/*
185820** Compare the OBJECT label at pNode against zKey,nKey.  Return true on
185821** a match.
185822*/
185823static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
185824  if( pNode->jnFlags & JNODE_RAW ){
185825    if( pNode->n!=nKey ) return 0;
185826    return strncmp(pNode->u.zJContent, zKey, nKey)==0;
185827  }else{
185828    if( pNode->n!=nKey+2 ) return 0;
185829    return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
185830  }
185831}
185832
185833/* forward declaration */
185834static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
185835
185836/*
185837** Search along zPath to find the node specified.  Return a pointer
185838** to that node, or NULL if zPath is malformed or if there is no such
185839** node.
185840**
185841** If pApnd!=0, then try to append new nodes to complete zPath if it is
185842** possible to do so and if no existing node corresponds to zPath.  If
185843** new nodes are appended *pApnd is set to 1.
185844*/
185845static JsonNode *jsonLookupStep(
185846  JsonParse *pParse,      /* The JSON to search */
185847  u32 iRoot,              /* Begin the search at this node */
185848  const char *zPath,      /* The path to search */
185849  int *pApnd,             /* Append nodes to complete path if not NULL */
185850  const char **pzErr      /* Make *pzErr point to any syntax error in zPath */
185851){
185852  u32 i, j, nKey;
185853  const char *zKey;
185854  JsonNode *pRoot = &pParse->aNode[iRoot];
185855  if( zPath[0]==0 ) return pRoot;
185856  if( pRoot->jnFlags & JNODE_REPLACE ) return 0;
185857  if( zPath[0]=='.' ){
185858    if( pRoot->eType!=JSON_OBJECT ) return 0;
185859    zPath++;
185860    if( zPath[0]=='"' ){
185861      zKey = zPath + 1;
185862      for(i=1; zPath[i] && zPath[i]!='"'; i++){}
185863      nKey = i-1;
185864      if( zPath[i] ){
185865        i++;
185866      }else{
185867        *pzErr = zPath;
185868        return 0;
185869      }
185870    }else{
185871      zKey = zPath;
185872      for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
185873      nKey = i;
185874    }
185875    if( nKey==0 ){
185876      *pzErr = zPath;
185877      return 0;
185878    }
185879    j = 1;
185880    for(;;){
185881      while( j<=pRoot->n ){
185882        if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
185883          return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
185884        }
185885        j++;
185886        j += jsonNodeSize(&pRoot[j]);
185887      }
185888      if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
185889      iRoot += pRoot->u.iAppend;
185890      pRoot = &pParse->aNode[iRoot];
185891      j = 1;
185892    }
185893    if( pApnd ){
185894      u32 iStart, iLabel;
185895      JsonNode *pNode;
185896      iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
185897      iLabel = jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
185898      zPath += i;
185899      pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
185900      if( pParse->oom ) return 0;
185901      if( pNode ){
185902        pRoot = &pParse->aNode[iRoot];
185903        pRoot->u.iAppend = iStart - iRoot;
185904        pRoot->jnFlags |= JNODE_APPEND;
185905        pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
185906      }
185907      return pNode;
185908    }
185909  }else if( zPath[0]=='[' ){
185910    i = 0;
185911    j = 1;
185912    while( safe_isdigit(zPath[j]) ){
185913      i = i*10 + zPath[j] - '0';
185914      j++;
185915    }
185916    if( j<2 || zPath[j]!=']' ){
185917      if( zPath[1]=='#' ){
185918        JsonNode *pBase = pRoot;
185919        int iBase = iRoot;
185920        if( pRoot->eType!=JSON_ARRAY ) return 0;
185921        for(;;){
185922          while( j<=pBase->n ){
185923            if( (pBase[j].jnFlags & JNODE_REMOVE)==0 ) i++;
185924            j += jsonNodeSize(&pBase[j]);
185925          }
185926          if( (pBase->jnFlags & JNODE_APPEND)==0 ) break;
185927          iBase += pBase->u.iAppend;
185928          pBase = &pParse->aNode[iBase];
185929          j = 1;
185930        }
185931        j = 2;
185932        if( zPath[2]=='-' && safe_isdigit(zPath[3]) ){
185933          unsigned int x = 0;
185934          j = 3;
185935          do{
185936            x = x*10 + zPath[j] - '0';
185937            j++;
185938          }while( safe_isdigit(zPath[j]) );
185939          if( x>i ) return 0;
185940          i -= x;
185941        }
185942        if( zPath[j]!=']' ){
185943          *pzErr = zPath;
185944          return 0;
185945        }
185946      }else{
185947        *pzErr = zPath;
185948        return 0;
185949      }
185950    }
185951    if( pRoot->eType!=JSON_ARRAY ) return 0;
185952    zPath += j + 1;
185953    j = 1;
185954    for(;;){
185955      while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
185956        if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
185957        j += jsonNodeSize(&pRoot[j]);
185958      }
185959      if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
185960      iRoot += pRoot->u.iAppend;
185961      pRoot = &pParse->aNode[iRoot];
185962      j = 1;
185963    }
185964    if( j<=pRoot->n ){
185965      return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
185966    }
185967    if( i==0 && pApnd ){
185968      u32 iStart;
185969      JsonNode *pNode;
185970      iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
185971      pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
185972      if( pParse->oom ) return 0;
185973      if( pNode ){
185974        pRoot = &pParse->aNode[iRoot];
185975        pRoot->u.iAppend = iStart - iRoot;
185976        pRoot->jnFlags |= JNODE_APPEND;
185977      }
185978      return pNode;
185979    }
185980  }else{
185981    *pzErr = zPath;
185982  }
185983  return 0;
185984}
185985
185986/*
185987** Append content to pParse that will complete zPath.  Return a pointer
185988** to the inserted node, or return NULL if the append fails.
185989*/
185990static JsonNode *jsonLookupAppend(
185991  JsonParse *pParse,     /* Append content to the JSON parse */
185992  const char *zPath,     /* Description of content to append */
185993  int *pApnd,            /* Set this flag to 1 */
185994  const char **pzErr     /* Make this point to any syntax error */
185995){
185996  *pApnd = 1;
185997  if( zPath[0]==0 ){
185998    jsonParseAddNode(pParse, JSON_NULL, 0, 0);
185999    return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
186000  }
186001  if( zPath[0]=='.' ){
186002    jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
186003  }else if( strncmp(zPath,"[0]",3)==0 ){
186004    jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
186005  }else{
186006    return 0;
186007  }
186008  if( pParse->oom ) return 0;
186009  return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
186010}
186011
186012/*
186013** Return the text of a syntax error message on a JSON path.  Space is
186014** obtained from sqlite3_malloc().
186015*/
186016static char *jsonPathSyntaxError(const char *zErr){
186017  return sqlite3_mprintf("JSON path error near '%q'", zErr);
186018}
186019
186020/*
186021** Do a node lookup using zPath.  Return a pointer to the node on success.
186022** Return NULL if not found or if there is an error.
186023**
186024** On an error, write an error message into pCtx and increment the
186025** pParse->nErr counter.
186026**
186027** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
186028** nodes are appended.
186029*/
186030static JsonNode *jsonLookup(
186031  JsonParse *pParse,      /* The JSON to search */
186032  const char *zPath,      /* The path to search */
186033  int *pApnd,             /* Append nodes to complete path if not NULL */
186034  sqlite3_context *pCtx   /* Report errors here, if not NULL */
186035){
186036  const char *zErr = 0;
186037  JsonNode *pNode = 0;
186038  char *zMsg;
186039
186040  if( zPath==0 ) return 0;
186041  if( zPath[0]!='$' ){
186042    zErr = zPath;
186043    goto lookup_err;
186044  }
186045  zPath++;
186046  pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
186047  if( zErr==0 ) return pNode;
186048
186049lookup_err:
186050  pParse->nErr++;
186051  assert( zErr!=0 && pCtx!=0 );
186052  zMsg = jsonPathSyntaxError(zErr);
186053  if( zMsg ){
186054    sqlite3_result_error(pCtx, zMsg, -1);
186055    sqlite3_free(zMsg);
186056  }else{
186057    sqlite3_result_error_nomem(pCtx);
186058  }
186059  return 0;
186060}
186061
186062
186063/*
186064** Report the wrong number of arguments for json_insert(), json_replace()
186065** or json_set().
186066*/
186067static void jsonWrongNumArgs(
186068  sqlite3_context *pCtx,
186069  const char *zFuncName
186070){
186071  char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
186072                               zFuncName);
186073  sqlite3_result_error(pCtx, zMsg, -1);
186074  sqlite3_free(zMsg);
186075}
186076
186077/*
186078** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
186079*/
186080static void jsonRemoveAllNulls(JsonNode *pNode){
186081  int i, n;
186082  assert( pNode->eType==JSON_OBJECT );
186083  n = pNode->n;
186084  for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
186085    switch( pNode[i].eType ){
186086      case JSON_NULL:
186087        pNode[i].jnFlags |= JNODE_REMOVE;
186088        break;
186089      case JSON_OBJECT:
186090        jsonRemoveAllNulls(&pNode[i]);
186091        break;
186092    }
186093  }
186094}
186095
186096
186097/****************************************************************************
186098** SQL functions used for testing and debugging
186099****************************************************************************/
186100
186101#ifdef SQLITE_DEBUG
186102/*
186103** The json_parse(JSON) function returns a string which describes
186104** a parse of the JSON provided.  Or it returns NULL if JSON is not
186105** well-formed.
186106*/
186107static void jsonParseFunc(
186108  sqlite3_context *ctx,
186109  int argc,
186110  sqlite3_value **argv
186111){
186112  JsonString s;       /* Output string - not real JSON */
186113  JsonParse x;        /* The parse */
186114  u32 i;
186115
186116  assert( argc==1 );
186117  if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
186118  jsonParseFindParents(&x);
186119  jsonInit(&s, ctx);
186120  for(i=0; i<x.nNode; i++){
186121    const char *zType;
186122    if( x.aNode[i].jnFlags & JNODE_LABEL ){
186123      assert( x.aNode[i].eType==JSON_STRING );
186124      zType = "label";
186125    }else{
186126      zType = jsonType[x.aNode[i].eType];
186127    }
186128    jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
186129               i, zType, x.aNode[i].n, x.aUp[i]);
186130    if( x.aNode[i].u.zJContent!=0 ){
186131      jsonAppendRaw(&s, " ", 1);
186132      jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
186133    }
186134    jsonAppendRaw(&s, "\n", 1);
186135  }
186136  jsonParseReset(&x);
186137  jsonResult(&s);
186138}
186139
186140/*
186141** The json_test1(JSON) function return true (1) if the input is JSON
186142** text generated by another json function.  It returns (0) if the input
186143** is not known to be JSON.
186144*/
186145static void jsonTest1Func(
186146  sqlite3_context *ctx,
186147  int argc,
186148  sqlite3_value **argv
186149){
186150  UNUSED_PARAM(argc);
186151  sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
186152}
186153#endif /* SQLITE_DEBUG */
186154
186155/****************************************************************************
186156** Scalar SQL function implementations
186157****************************************************************************/
186158
186159/*
186160** Implementation of the json_QUOTE(VALUE) function.  Return a JSON value
186161** corresponding to the SQL value input.  Mostly this means putting
186162** double-quotes around strings and returning the unquoted string "null"
186163** when given a NULL input.
186164*/
186165static void jsonQuoteFunc(
186166  sqlite3_context *ctx,
186167  int argc,
186168  sqlite3_value **argv
186169){
186170  JsonString jx;
186171  UNUSED_PARAM(argc);
186172
186173  jsonInit(&jx, ctx);
186174  jsonAppendValue(&jx, argv[0]);
186175  jsonResult(&jx);
186176  sqlite3_result_subtype(ctx, JSON_SUBTYPE);
186177}
186178
186179/*
186180** Implementation of the json_array(VALUE,...) function.  Return a JSON
186181** array that contains all values given in arguments.  Or if any argument
186182** is a BLOB, throw an error.
186183*/
186184static void jsonArrayFunc(
186185  sqlite3_context *ctx,
186186  int argc,
186187  sqlite3_value **argv
186188){
186189  int i;
186190  JsonString jx;
186191
186192  jsonInit(&jx, ctx);
186193  jsonAppendChar(&jx, '[');
186194  for(i=0; i<argc; i++){
186195    jsonAppendSeparator(&jx);
186196    jsonAppendValue(&jx, argv[i]);
186197  }
186198  jsonAppendChar(&jx, ']');
186199  jsonResult(&jx);
186200  sqlite3_result_subtype(ctx, JSON_SUBTYPE);
186201}
186202
186203
186204/*
186205** json_array_length(JSON)
186206** json_array_length(JSON, PATH)
186207**
186208** Return the number of elements in the top-level JSON array.
186209** Return 0 if the input is not a well-formed JSON array.
186210*/
186211static void jsonArrayLengthFunc(
186212  sqlite3_context *ctx,
186213  int argc,
186214  sqlite3_value **argv
186215){
186216  JsonParse *p;          /* The parse */
186217  sqlite3_int64 n = 0;
186218  u32 i;
186219  JsonNode *pNode;
186220
186221  p = jsonParseCached(ctx, argv, ctx);
186222  if( p==0 ) return;
186223  assert( p->nNode );
186224  if( argc==2 ){
186225    const char *zPath = (const char*)sqlite3_value_text(argv[1]);
186226    pNode = jsonLookup(p, zPath, 0, ctx);
186227  }else{
186228    pNode = p->aNode;
186229  }
186230  if( pNode==0 ){
186231    return;
186232  }
186233  if( pNode->eType==JSON_ARRAY ){
186234    assert( (pNode->jnFlags & JNODE_APPEND)==0 );
186235    for(i=1; i<=pNode->n; n++){
186236      i += jsonNodeSize(&pNode[i]);
186237    }
186238  }
186239  sqlite3_result_int64(ctx, n);
186240}
186241
186242/*
186243** json_extract(JSON, PATH, ...)
186244**
186245** Return the element described by PATH.  Return NULL if there is no
186246** PATH element.  If there are multiple PATHs, then return a JSON array
186247** with the result from each path.  Throw an error if the JSON or any PATH
186248** is malformed.
186249*/
186250static void jsonExtractFunc(
186251  sqlite3_context *ctx,
186252  int argc,
186253  sqlite3_value **argv
186254){
186255  JsonParse *p;          /* The parse */
186256  JsonNode *pNode;
186257  const char *zPath;
186258  JsonString jx;
186259  int i;
186260
186261  if( argc<2 ) return;
186262  p = jsonParseCached(ctx, argv, ctx);
186263  if( p==0 ) return;
186264  jsonInit(&jx, ctx);
186265  jsonAppendChar(&jx, '[');
186266  for(i=1; i<argc; i++){
186267    zPath = (const char*)sqlite3_value_text(argv[i]);
186268    pNode = jsonLookup(p, zPath, 0, ctx);
186269    if( p->nErr ) break;
186270    if( argc>2 ){
186271      jsonAppendSeparator(&jx);
186272      if( pNode ){
186273        jsonRenderNode(pNode, &jx, 0);
186274      }else{
186275        jsonAppendRaw(&jx, "null", 4);
186276      }
186277    }else if( pNode ){
186278      jsonReturn(pNode, ctx, 0);
186279    }
186280  }
186281  if( argc>2 && i==argc ){
186282    jsonAppendChar(&jx, ']');
186283    jsonResult(&jx);
186284    sqlite3_result_subtype(ctx, JSON_SUBTYPE);
186285  }
186286  jsonReset(&jx);
186287}
186288
186289/* This is the RFC 7396 MergePatch algorithm.
186290*/
186291static JsonNode *jsonMergePatch(
186292  JsonParse *pParse,   /* The JSON parser that contains the TARGET */
186293  u32 iTarget,         /* Node of the TARGET in pParse */
186294  JsonNode *pPatch     /* The PATCH */
186295){
186296  u32 i, j;
186297  u32 iRoot;
186298  JsonNode *pTarget;
186299  if( pPatch->eType!=JSON_OBJECT ){
186300    return pPatch;
186301  }
186302  assert( iTarget>=0 && iTarget<pParse->nNode );
186303  pTarget = &pParse->aNode[iTarget];
186304  assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
186305  if( pTarget->eType!=JSON_OBJECT ){
186306    jsonRemoveAllNulls(pPatch);
186307    return pPatch;
186308  }
186309  iRoot = iTarget;
186310  for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
186311    u32 nKey;
186312    const char *zKey;
186313    assert( pPatch[i].eType==JSON_STRING );
186314    assert( pPatch[i].jnFlags & JNODE_LABEL );
186315    nKey = pPatch[i].n;
186316    zKey = pPatch[i].u.zJContent;
186317    assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
186318    for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
186319      assert( pTarget[j].eType==JSON_STRING );
186320      assert( pTarget[j].jnFlags & JNODE_LABEL );
186321      assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
186322      if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
186323        if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
186324        if( pPatch[i+1].eType==JSON_NULL ){
186325          pTarget[j+1].jnFlags |= JNODE_REMOVE;
186326        }else{
186327          JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
186328          if( pNew==0 ) return 0;
186329          pTarget = &pParse->aNode[iTarget];
186330          if( pNew!=&pTarget[j+1] ){
186331            pTarget[j+1].u.pPatch = pNew;
186332            pTarget[j+1].jnFlags |= JNODE_PATCH;
186333          }
186334        }
186335        break;
186336      }
186337    }
186338    if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
186339      int iStart, iPatch;
186340      iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
186341      jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
186342      iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
186343      if( pParse->oom ) return 0;
186344      jsonRemoveAllNulls(pPatch);
186345      pTarget = &pParse->aNode[iTarget];
186346      pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
186347      pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
186348      iRoot = iStart;
186349      pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
186350      pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
186351    }
186352  }
186353  return pTarget;
186354}
186355
186356/*
186357** Implementation of the json_mergepatch(JSON1,JSON2) function.  Return a JSON
186358** object that is the result of running the RFC 7396 MergePatch() algorithm
186359** on the two arguments.
186360*/
186361static void jsonPatchFunc(
186362  sqlite3_context *ctx,
186363  int argc,
186364  sqlite3_value **argv
186365){
186366  JsonParse x;     /* The JSON that is being patched */
186367  JsonParse y;     /* The patch */
186368  JsonNode *pResult;   /* The result of the merge */
186369
186370  UNUSED_PARAM(argc);
186371  if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
186372  if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
186373    jsonParseReset(&x);
186374    return;
186375  }
186376  pResult = jsonMergePatch(&x, 0, y.aNode);
186377  assert( pResult!=0 || x.oom );
186378  if( pResult ){
186379    jsonReturnJson(pResult, ctx, 0);
186380  }else{
186381    sqlite3_result_error_nomem(ctx);
186382  }
186383  jsonParseReset(&x);
186384  jsonParseReset(&y);
186385}
186386
186387
186388/*
186389** Implementation of the json_object(NAME,VALUE,...) function.  Return a JSON
186390** object that contains all name/value given in arguments.  Or if any name
186391** is not a string or if any value is a BLOB, throw an error.
186392*/
186393static void jsonObjectFunc(
186394  sqlite3_context *ctx,
186395  int argc,
186396  sqlite3_value **argv
186397){
186398  int i;
186399  JsonString jx;
186400  const char *z;
186401  u32 n;
186402
186403  if( argc&1 ){
186404    sqlite3_result_error(ctx, "json_object() requires an even number "
186405                                  "of arguments", -1);
186406    return;
186407  }
186408  jsonInit(&jx, ctx);
186409  jsonAppendChar(&jx, '{');
186410  for(i=0; i<argc; i+=2){
186411    if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
186412      sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
186413      jsonReset(&jx);
186414      return;
186415    }
186416    jsonAppendSeparator(&jx);
186417    z = (const char*)sqlite3_value_text(argv[i]);
186418    n = (u32)sqlite3_value_bytes(argv[i]);
186419    jsonAppendString(&jx, z, n);
186420    jsonAppendChar(&jx, ':');
186421    jsonAppendValue(&jx, argv[i+1]);
186422  }
186423  jsonAppendChar(&jx, '}');
186424  jsonResult(&jx);
186425  sqlite3_result_subtype(ctx, JSON_SUBTYPE);
186426}
186427
186428
186429/*
186430** json_remove(JSON, PATH, ...)
186431**
186432** Remove the named elements from JSON and return the result.  malformed
186433** JSON or PATH arguments result in an error.
186434*/
186435static void jsonRemoveFunc(
186436  sqlite3_context *ctx,
186437  int argc,
186438  sqlite3_value **argv
186439){
186440  JsonParse x;          /* The parse */
186441  JsonNode *pNode;
186442  const char *zPath;
186443  u32 i;
186444
186445  if( argc<1 ) return;
186446  if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
186447  assert( x.nNode );
186448  for(i=1; i<(u32)argc; i++){
186449    zPath = (const char*)sqlite3_value_text(argv[i]);
186450    if( zPath==0 ) goto remove_done;
186451    pNode = jsonLookup(&x, zPath, 0, ctx);
186452    if( x.nErr ) goto remove_done;
186453    if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
186454  }
186455  if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
186456    jsonReturnJson(x.aNode, ctx, 0);
186457  }
186458remove_done:
186459  jsonParseReset(&x);
186460}
186461
186462/*
186463** json_replace(JSON, PATH, VALUE, ...)
186464**
186465** Replace the value at PATH with VALUE.  If PATH does not already exist,
186466** this routine is a no-op.  If JSON or PATH is malformed, throw an error.
186467*/
186468static void jsonReplaceFunc(
186469  sqlite3_context *ctx,
186470  int argc,
186471  sqlite3_value **argv
186472){
186473  JsonParse x;          /* The parse */
186474  JsonNode *pNode;
186475  const char *zPath;
186476  u32 i;
186477
186478  if( argc<1 ) return;
186479  if( (argc&1)==0 ) {
186480    jsonWrongNumArgs(ctx, "replace");
186481    return;
186482  }
186483  if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
186484  assert( x.nNode );
186485  for(i=1; i<(u32)argc; i+=2){
186486    zPath = (const char*)sqlite3_value_text(argv[i]);
186487    pNode = jsonLookup(&x, zPath, 0, ctx);
186488    if( x.nErr ) goto replace_err;
186489    if( pNode ){
186490      pNode->jnFlags |= (u8)JNODE_REPLACE;
186491      pNode->u.iReplace = i + 1;
186492    }
186493  }
186494  if( x.aNode[0].jnFlags & JNODE_REPLACE ){
186495    sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
186496  }else{
186497    jsonReturnJson(x.aNode, ctx, argv);
186498  }
186499replace_err:
186500  jsonParseReset(&x);
186501}
186502
186503/*
186504** json_set(JSON, PATH, VALUE, ...)
186505**
186506** Set the value at PATH to VALUE.  Create the PATH if it does not already
186507** exist.  Overwrite existing values that do exist.
186508** If JSON or PATH is malformed, throw an error.
186509**
186510** json_insert(JSON, PATH, VALUE, ...)
186511**
186512** Create PATH and initialize it to VALUE.  If PATH already exists, this
186513** routine is a no-op.  If JSON or PATH is malformed, throw an error.
186514*/
186515static void jsonSetFunc(
186516  sqlite3_context *ctx,
186517  int argc,
186518  sqlite3_value **argv
186519){
186520  JsonParse x;          /* The parse */
186521  JsonNode *pNode;
186522  const char *zPath;
186523  u32 i;
186524  int bApnd;
186525  int bIsSet = *(int*)sqlite3_user_data(ctx);
186526
186527  if( argc<1 ) return;
186528  if( (argc&1)==0 ) {
186529    jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
186530    return;
186531  }
186532  if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
186533  assert( x.nNode );
186534  for(i=1; i<(u32)argc; i+=2){
186535    zPath = (const char*)sqlite3_value_text(argv[i]);
186536    bApnd = 0;
186537    pNode = jsonLookup(&x, zPath, &bApnd, ctx);
186538    if( x.oom ){
186539      sqlite3_result_error_nomem(ctx);
186540      goto jsonSetDone;
186541    }else if( x.nErr ){
186542      goto jsonSetDone;
186543    }else if( pNode && (bApnd || bIsSet) ){
186544      pNode->jnFlags |= (u8)JNODE_REPLACE;
186545      pNode->u.iReplace = i + 1;
186546    }
186547  }
186548  if( x.aNode[0].jnFlags & JNODE_REPLACE ){
186549    sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
186550  }else{
186551    jsonReturnJson(x.aNode, ctx, argv);
186552  }
186553jsonSetDone:
186554  jsonParseReset(&x);
186555}
186556
186557/*
186558** json_type(JSON)
186559** json_type(JSON, PATH)
186560**
186561** Return the top-level "type" of a JSON string.  Throw an error if
186562** either the JSON or PATH inputs are not well-formed.
186563*/
186564static void jsonTypeFunc(
186565  sqlite3_context *ctx,
186566  int argc,
186567  sqlite3_value **argv
186568){
186569  JsonParse *p;          /* The parse */
186570  const char *zPath;
186571  JsonNode *pNode;
186572
186573  p = jsonParseCached(ctx, argv, ctx);
186574  if( p==0 ) return;
186575  if( argc==2 ){
186576    zPath = (const char*)sqlite3_value_text(argv[1]);
186577    pNode = jsonLookup(p, zPath, 0, ctx);
186578  }else{
186579    pNode = p->aNode;
186580  }
186581  if( pNode ){
186582    sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
186583  }
186584}
186585
186586/*
186587** json_valid(JSON)
186588**
186589** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
186590** Return 0 otherwise.
186591*/
186592static void jsonValidFunc(
186593  sqlite3_context *ctx,
186594  int argc,
186595  sqlite3_value **argv
186596){
186597  JsonParse *p;          /* The parse */
186598  UNUSED_PARAM(argc);
186599  p = jsonParseCached(ctx, argv, 0);
186600  sqlite3_result_int(ctx, p!=0);
186601}
186602
186603
186604/****************************************************************************
186605** Aggregate SQL function implementations
186606****************************************************************************/
186607/*
186608** json_group_array(VALUE)
186609**
186610** Return a JSON array composed of all values in the aggregate.
186611*/
186612static void jsonArrayStep(
186613  sqlite3_context *ctx,
186614  int argc,
186615  sqlite3_value **argv
186616){
186617  JsonString *pStr;
186618  UNUSED_PARAM(argc);
186619  pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
186620  if( pStr ){
186621    if( pStr->zBuf==0 ){
186622      jsonInit(pStr, ctx);
186623      jsonAppendChar(pStr, '[');
186624    }else if( pStr->nUsed>1 ){
186625      jsonAppendChar(pStr, ',');
186626      pStr->pCtx = ctx;
186627    }
186628    jsonAppendValue(pStr, argv[0]);
186629  }
186630}
186631static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
186632  JsonString *pStr;
186633  pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
186634  if( pStr ){
186635    pStr->pCtx = ctx;
186636    jsonAppendChar(pStr, ']');
186637    if( pStr->bErr ){
186638      if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
186639      assert( pStr->bStatic );
186640    }else if( isFinal ){
186641      sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
186642                          pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
186643      pStr->bStatic = 1;
186644    }else{
186645      sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
186646      pStr->nUsed--;
186647    }
186648  }else{
186649    sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
186650  }
186651  sqlite3_result_subtype(ctx, JSON_SUBTYPE);
186652}
186653static void jsonArrayValue(sqlite3_context *ctx){
186654  jsonArrayCompute(ctx, 0);
186655}
186656static void jsonArrayFinal(sqlite3_context *ctx){
186657  jsonArrayCompute(ctx, 1);
186658}
186659
186660#ifndef SQLITE_OMIT_WINDOWFUNC
186661/*
186662** This method works for both json_group_array() and json_group_object().
186663** It works by removing the first element of the group by searching forward
186664** to the first comma (",") that is not within a string and deleting all
186665** text through that comma.
186666*/
186667static void jsonGroupInverse(
186668  sqlite3_context *ctx,
186669  int argc,
186670  sqlite3_value **argv
186671){
186672  unsigned int i;
186673  int inStr = 0;
186674  int nNest = 0;
186675  char *z;
186676  char c;
186677  JsonString *pStr;
186678  UNUSED_PARAM(argc);
186679  UNUSED_PARAM(argv);
186680  pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
186681#ifdef NEVER
186682  /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
186683  ** always have been called to initalize it */
186684  if( NEVER(!pStr) ) return;
186685#endif
186686  z = pStr->zBuf;
186687  for(i=1; (c = z[i])!=',' || inStr || nNest; i++){
186688    if( i>=pStr->nUsed ){
186689      pStr->nUsed = 1;
186690      return;
186691    }
186692    if( c=='"' ){
186693      inStr = !inStr;
186694    }else if( c=='\\' ){
186695      i++;
186696    }else if( !inStr ){
186697      if( c=='{' || c=='[' ) nNest++;
186698      if( c=='}' || c==']' ) nNest--;
186699    }
186700  }
186701  pStr->nUsed -= i;
186702  memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
186703}
186704#else
186705# define jsonGroupInverse 0
186706#endif
186707
186708
186709/*
186710** json_group_obj(NAME,VALUE)
186711**
186712** Return a JSON object composed of all names and values in the aggregate.
186713*/
186714static void jsonObjectStep(
186715  sqlite3_context *ctx,
186716  int argc,
186717  sqlite3_value **argv
186718){
186719  JsonString *pStr;
186720  const char *z;
186721  u32 n;
186722  UNUSED_PARAM(argc);
186723  pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
186724  if( pStr ){
186725    if( pStr->zBuf==0 ){
186726      jsonInit(pStr, ctx);
186727      jsonAppendChar(pStr, '{');
186728    }else if( pStr->nUsed>1 ){
186729      jsonAppendChar(pStr, ',');
186730      pStr->pCtx = ctx;
186731    }
186732    z = (const char*)sqlite3_value_text(argv[0]);
186733    n = (u32)sqlite3_value_bytes(argv[0]);
186734    jsonAppendString(pStr, z, n);
186735    jsonAppendChar(pStr, ':');
186736    jsonAppendValue(pStr, argv[1]);
186737  }
186738}
186739static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
186740  JsonString *pStr;
186741  pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
186742  if( pStr ){
186743    jsonAppendChar(pStr, '}');
186744    if( pStr->bErr ){
186745      if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
186746      assert( pStr->bStatic );
186747    }else if( isFinal ){
186748      sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
186749                          pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
186750      pStr->bStatic = 1;
186751    }else{
186752      sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
186753      pStr->nUsed--;
186754    }
186755  }else{
186756    sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
186757  }
186758  sqlite3_result_subtype(ctx, JSON_SUBTYPE);
186759}
186760static void jsonObjectValue(sqlite3_context *ctx){
186761  jsonObjectCompute(ctx, 0);
186762}
186763static void jsonObjectFinal(sqlite3_context *ctx){
186764  jsonObjectCompute(ctx, 1);
186765}
186766
186767
186768
186769#ifndef SQLITE_OMIT_VIRTUALTABLE
186770/****************************************************************************
186771** The json_each virtual table
186772****************************************************************************/
186773typedef struct JsonEachCursor JsonEachCursor;
186774struct JsonEachCursor {
186775  sqlite3_vtab_cursor base;  /* Base class - must be first */
186776  u32 iRowid;                /* The rowid */
186777  u32 iBegin;                /* The first node of the scan */
186778  u32 i;                     /* Index in sParse.aNode[] of current row */
186779  u32 iEnd;                  /* EOF when i equals or exceeds this value */
186780  u8 eType;                  /* Type of top-level element */
186781  u8 bRecursive;             /* True for json_tree().  False for json_each() */
186782  char *zJson;               /* Input JSON */
186783  char *zRoot;               /* Path by which to filter zJson */
186784  JsonParse sParse;          /* Parse of the input JSON */
186785};
186786
186787/* Constructor for the json_each virtual table */
186788static int jsonEachConnect(
186789  sqlite3 *db,
186790  void *pAux,
186791  int argc, const char *const*argv,
186792  sqlite3_vtab **ppVtab,
186793  char **pzErr
186794){
186795  sqlite3_vtab *pNew;
186796  int rc;
186797
186798/* Column numbers */
186799#define JEACH_KEY     0
186800#define JEACH_VALUE   1
186801#define JEACH_TYPE    2
186802#define JEACH_ATOM    3
186803#define JEACH_ID      4
186804#define JEACH_PARENT  5
186805#define JEACH_FULLKEY 6
186806#define JEACH_PATH    7
186807/* The xBestIndex method assumes that the JSON and ROOT columns are
186808** the last two columns in the table.  Should this ever changes, be
186809** sure to update the xBestIndex method. */
186810#define JEACH_JSON    8
186811#define JEACH_ROOT    9
186812
186813  UNUSED_PARAM(pzErr);
186814  UNUSED_PARAM(argv);
186815  UNUSED_PARAM(argc);
186816  UNUSED_PARAM(pAux);
186817  rc = sqlite3_declare_vtab(db,
186818     "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
186819                    "json HIDDEN,root HIDDEN)");
186820  if( rc==SQLITE_OK ){
186821    pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
186822    if( pNew==0 ) return SQLITE_NOMEM;
186823    memset(pNew, 0, sizeof(*pNew));
186824    sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
186825  }
186826  return rc;
186827}
186828
186829/* destructor for json_each virtual table */
186830static int jsonEachDisconnect(sqlite3_vtab *pVtab){
186831  sqlite3_free(pVtab);
186832  return SQLITE_OK;
186833}
186834
186835/* constructor for a JsonEachCursor object for json_each(). */
186836static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
186837  JsonEachCursor *pCur;
186838
186839  UNUSED_PARAM(p);
186840  pCur = sqlite3_malloc( sizeof(*pCur) );
186841  if( pCur==0 ) return SQLITE_NOMEM;
186842  memset(pCur, 0, sizeof(*pCur));
186843  *ppCursor = &pCur->base;
186844  return SQLITE_OK;
186845}
186846
186847/* constructor for a JsonEachCursor object for json_tree(). */
186848static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
186849  int rc = jsonEachOpenEach(p, ppCursor);
186850  if( rc==SQLITE_OK ){
186851    JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
186852    pCur->bRecursive = 1;
186853  }
186854  return rc;
186855}
186856
186857/* Reset a JsonEachCursor back to its original state.  Free any memory
186858** held. */
186859static void jsonEachCursorReset(JsonEachCursor *p){
186860  sqlite3_free(p->zJson);
186861  sqlite3_free(p->zRoot);
186862  jsonParseReset(&p->sParse);
186863  p->iRowid = 0;
186864  p->i = 0;
186865  p->iEnd = 0;
186866  p->eType = 0;
186867  p->zJson = 0;
186868  p->zRoot = 0;
186869}
186870
186871/* Destructor for a jsonEachCursor object */
186872static int jsonEachClose(sqlite3_vtab_cursor *cur){
186873  JsonEachCursor *p = (JsonEachCursor*)cur;
186874  jsonEachCursorReset(p);
186875  sqlite3_free(cur);
186876  return SQLITE_OK;
186877}
186878
186879/* Return TRUE if the jsonEachCursor object has been advanced off the end
186880** of the JSON object */
186881static int jsonEachEof(sqlite3_vtab_cursor *cur){
186882  JsonEachCursor *p = (JsonEachCursor*)cur;
186883  return p->i >= p->iEnd;
186884}
186885
186886/* Advance the cursor to the next element for json_tree() */
186887static int jsonEachNext(sqlite3_vtab_cursor *cur){
186888  JsonEachCursor *p = (JsonEachCursor*)cur;
186889  if( p->bRecursive ){
186890    if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
186891    p->i++;
186892    p->iRowid++;
186893    if( p->i<p->iEnd ){
186894      u32 iUp = p->sParse.aUp[p->i];
186895      JsonNode *pUp = &p->sParse.aNode[iUp];
186896      p->eType = pUp->eType;
186897      if( pUp->eType==JSON_ARRAY ){
186898        if( iUp==p->i-1 ){
186899          pUp->u.iKey = 0;
186900        }else{
186901          pUp->u.iKey++;
186902        }
186903      }
186904    }
186905  }else{
186906    switch( p->eType ){
186907      case JSON_ARRAY: {
186908        p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
186909        p->iRowid++;
186910        break;
186911      }
186912      case JSON_OBJECT: {
186913        p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
186914        p->iRowid++;
186915        break;
186916      }
186917      default: {
186918        p->i = p->iEnd;
186919        break;
186920      }
186921    }
186922  }
186923  return SQLITE_OK;
186924}
186925
186926/* Append the name of the path for element i to pStr
186927*/
186928static void jsonEachComputePath(
186929  JsonEachCursor *p,       /* The cursor */
186930  JsonString *pStr,        /* Write the path here */
186931  u32 i                    /* Path to this element */
186932){
186933  JsonNode *pNode, *pUp;
186934  u32 iUp;
186935  if( i==0 ){
186936    jsonAppendChar(pStr, '$');
186937    return;
186938  }
186939  iUp = p->sParse.aUp[i];
186940  jsonEachComputePath(p, pStr, iUp);
186941  pNode = &p->sParse.aNode[i];
186942  pUp = &p->sParse.aNode[iUp];
186943  if( pUp->eType==JSON_ARRAY ){
186944    jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
186945  }else{
186946    assert( pUp->eType==JSON_OBJECT );
186947    if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
186948    assert( pNode->eType==JSON_STRING );
186949    assert( pNode->jnFlags & JNODE_LABEL );
186950    jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
186951  }
186952}
186953
186954/* Return the value of a column */
186955static int jsonEachColumn(
186956  sqlite3_vtab_cursor *cur,   /* The cursor */
186957  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
186958  int i                       /* Which column to return */
186959){
186960  JsonEachCursor *p = (JsonEachCursor*)cur;
186961  JsonNode *pThis = &p->sParse.aNode[p->i];
186962  switch( i ){
186963    case JEACH_KEY: {
186964      if( p->i==0 ) break;
186965      if( p->eType==JSON_OBJECT ){
186966        jsonReturn(pThis, ctx, 0);
186967      }else if( p->eType==JSON_ARRAY ){
186968        u32 iKey;
186969        if( p->bRecursive ){
186970          if( p->iRowid==0 ) break;
186971          iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
186972        }else{
186973          iKey = p->iRowid;
186974        }
186975        sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
186976      }
186977      break;
186978    }
186979    case JEACH_VALUE: {
186980      if( pThis->jnFlags & JNODE_LABEL ) pThis++;
186981      jsonReturn(pThis, ctx, 0);
186982      break;
186983    }
186984    case JEACH_TYPE: {
186985      if( pThis->jnFlags & JNODE_LABEL ) pThis++;
186986      sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
186987      break;
186988    }
186989    case JEACH_ATOM: {
186990      if( pThis->jnFlags & JNODE_LABEL ) pThis++;
186991      if( pThis->eType>=JSON_ARRAY ) break;
186992      jsonReturn(pThis, ctx, 0);
186993      break;
186994    }
186995    case JEACH_ID: {
186996      sqlite3_result_int64(ctx,
186997         (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
186998      break;
186999    }
187000    case JEACH_PARENT: {
187001      if( p->i>p->iBegin && p->bRecursive ){
187002        sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
187003      }
187004      break;
187005    }
187006    case JEACH_FULLKEY: {
187007      JsonString x;
187008      jsonInit(&x, ctx);
187009      if( p->bRecursive ){
187010        jsonEachComputePath(p, &x, p->i);
187011      }else{
187012        if( p->zRoot ){
187013          jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
187014        }else{
187015          jsonAppendChar(&x, '$');
187016        }
187017        if( p->eType==JSON_ARRAY ){
187018          jsonPrintf(30, &x, "[%d]", p->iRowid);
187019        }else if( p->eType==JSON_OBJECT ){
187020          jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
187021        }
187022      }
187023      jsonResult(&x);
187024      break;
187025    }
187026    case JEACH_PATH: {
187027      if( p->bRecursive ){
187028        JsonString x;
187029        jsonInit(&x, ctx);
187030        jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
187031        jsonResult(&x);
187032        break;
187033      }
187034      /* For json_each() path and root are the same so fall through
187035      ** into the root case */
187036    }
187037    default: {
187038      const char *zRoot = p->zRoot;
187039      if( zRoot==0 ) zRoot = "$";
187040      sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
187041      break;
187042    }
187043    case JEACH_JSON: {
187044      assert( i==JEACH_JSON );
187045      sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
187046      break;
187047    }
187048  }
187049  return SQLITE_OK;
187050}
187051
187052/* Return the current rowid value */
187053static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
187054  JsonEachCursor *p = (JsonEachCursor*)cur;
187055  *pRowid = p->iRowid;
187056  return SQLITE_OK;
187057}
187058
187059/* The query strategy is to look for an equality constraint on the json
187060** column.  Without such a constraint, the table cannot operate.  idxNum is
187061** 1 if the constraint is found, 3 if the constraint and zRoot are found,
187062** and 0 otherwise.
187063*/
187064static int jsonEachBestIndex(
187065  sqlite3_vtab *tab,
187066  sqlite3_index_info *pIdxInfo
187067){
187068  int i;                     /* Loop counter or computed array index */
187069  int aIdx[2];               /* Index of constraints for JSON and ROOT */
187070  int unusableMask = 0;      /* Mask of unusable JSON and ROOT constraints */
187071  int idxMask = 0;           /* Mask of usable == constraints JSON and ROOT */
187072  const struct sqlite3_index_constraint *pConstraint;
187073
187074  /* This implementation assumes that JSON and ROOT are the last two
187075  ** columns in the table */
187076  assert( JEACH_ROOT == JEACH_JSON+1 );
187077  UNUSED_PARAM(tab);
187078  aIdx[0] = aIdx[1] = -1;
187079  pConstraint = pIdxInfo->aConstraint;
187080  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
187081    int iCol;
187082    int iMask;
187083    if( pConstraint->iColumn < JEACH_JSON ) continue;
187084    iCol = pConstraint->iColumn - JEACH_JSON;
187085    assert( iCol==0 || iCol==1 );
187086    iMask = 1 << iCol;
187087    if( pConstraint->usable==0 ){
187088      unusableMask |= iMask;
187089    }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
187090      aIdx[iCol] = i;
187091      idxMask |= iMask;
187092    }
187093  }
187094  if( (unusableMask & ~idxMask)!=0 ){
187095    /* If there are any unusable constraints on JSON or ROOT, then reject
187096    ** this entire plan */
187097    return SQLITE_CONSTRAINT;
187098  }
187099  if( aIdx[0]<0 ){
187100    /* No JSON input.  Leave estimatedCost at the huge value that it was
187101    ** initialized to to discourage the query planner from selecting this
187102    ** plan. */
187103    pIdxInfo->idxNum = 0;
187104  }else{
187105    pIdxInfo->estimatedCost = 1.0;
187106    i = aIdx[0];
187107    pIdxInfo->aConstraintUsage[i].argvIndex = 1;
187108    pIdxInfo->aConstraintUsage[i].omit = 1;
187109    if( aIdx[1]<0 ){
187110      pIdxInfo->idxNum = 1;  /* Only JSON supplied.  Plan 1 */
187111    }else{
187112      i = aIdx[1];
187113      pIdxInfo->aConstraintUsage[i].argvIndex = 2;
187114      pIdxInfo->aConstraintUsage[i].omit = 1;
187115      pIdxInfo->idxNum = 3;  /* Both JSON and ROOT are supplied.  Plan 3 */
187116    }
187117  }
187118  return SQLITE_OK;
187119}
187120
187121/* Start a search on a new JSON string */
187122static int jsonEachFilter(
187123  sqlite3_vtab_cursor *cur,
187124  int idxNum, const char *idxStr,
187125  int argc, sqlite3_value **argv
187126){
187127  JsonEachCursor *p = (JsonEachCursor*)cur;
187128  const char *z;
187129  const char *zRoot = 0;
187130  sqlite3_int64 n;
187131
187132  UNUSED_PARAM(idxStr);
187133  UNUSED_PARAM(argc);
187134  jsonEachCursorReset(p);
187135  if( idxNum==0 ) return SQLITE_OK;
187136  z = (const char*)sqlite3_value_text(argv[0]);
187137  if( z==0 ) return SQLITE_OK;
187138  n = sqlite3_value_bytes(argv[0]);
187139  p->zJson = sqlite3_malloc64( n+1 );
187140  if( p->zJson==0 ) return SQLITE_NOMEM;
187141  memcpy(p->zJson, z, (size_t)n+1);
187142  if( jsonParse(&p->sParse, 0, p->zJson) ){
187143    int rc = SQLITE_NOMEM;
187144    if( p->sParse.oom==0 ){
187145      sqlite3_free(cur->pVtab->zErrMsg);
187146      cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
187147      if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
187148    }
187149    jsonEachCursorReset(p);
187150    return rc;
187151  }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
187152    jsonEachCursorReset(p);
187153    return SQLITE_NOMEM;
187154  }else{
187155    JsonNode *pNode = 0;
187156    if( idxNum==3 ){
187157      const char *zErr = 0;
187158      zRoot = (const char*)sqlite3_value_text(argv[1]);
187159      if( zRoot==0 ) return SQLITE_OK;
187160      n = sqlite3_value_bytes(argv[1]);
187161      p->zRoot = sqlite3_malloc64( n+1 );
187162      if( p->zRoot==0 ) return SQLITE_NOMEM;
187163      memcpy(p->zRoot, zRoot, (size_t)n+1);
187164      if( zRoot[0]!='$' ){
187165        zErr = zRoot;
187166      }else{
187167        pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
187168      }
187169      if( zErr ){
187170        sqlite3_free(cur->pVtab->zErrMsg);
187171        cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
187172        jsonEachCursorReset(p);
187173        return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
187174      }else if( pNode==0 ){
187175        return SQLITE_OK;
187176      }
187177    }else{
187178      pNode = p->sParse.aNode;
187179    }
187180    p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
187181    p->eType = pNode->eType;
187182    if( p->eType>=JSON_ARRAY ){
187183      pNode->u.iKey = 0;
187184      p->iEnd = p->i + pNode->n + 1;
187185      if( p->bRecursive ){
187186        p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
187187        if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
187188          p->i--;
187189        }
187190      }else{
187191        p->i++;
187192      }
187193    }else{
187194      p->iEnd = p->i+1;
187195    }
187196  }
187197  return SQLITE_OK;
187198}
187199
187200/* The methods of the json_each virtual table */
187201static sqlite3_module jsonEachModule = {
187202  0,                         /* iVersion */
187203  0,                         /* xCreate */
187204  jsonEachConnect,           /* xConnect */
187205  jsonEachBestIndex,         /* xBestIndex */
187206  jsonEachDisconnect,        /* xDisconnect */
187207  0,                         /* xDestroy */
187208  jsonEachOpenEach,          /* xOpen - open a cursor */
187209  jsonEachClose,             /* xClose - close a cursor */
187210  jsonEachFilter,            /* xFilter - configure scan constraints */
187211  jsonEachNext,              /* xNext - advance a cursor */
187212  jsonEachEof,               /* xEof - check for end of scan */
187213  jsonEachColumn,            /* xColumn - read data */
187214  jsonEachRowid,             /* xRowid - read data */
187215  0,                         /* xUpdate */
187216  0,                         /* xBegin */
187217  0,                         /* xSync */
187218  0,                         /* xCommit */
187219  0,                         /* xRollback */
187220  0,                         /* xFindMethod */
187221  0,                         /* xRename */
187222  0,                         /* xSavepoint */
187223  0,                         /* xRelease */
187224  0,                         /* xRollbackTo */
187225  0                          /* xShadowName */
187226};
187227
187228/* The methods of the json_tree virtual table. */
187229static sqlite3_module jsonTreeModule = {
187230  0,                         /* iVersion */
187231  0,                         /* xCreate */
187232  jsonEachConnect,           /* xConnect */
187233  jsonEachBestIndex,         /* xBestIndex */
187234  jsonEachDisconnect,        /* xDisconnect */
187235  0,                         /* xDestroy */
187236  jsonEachOpenTree,          /* xOpen - open a cursor */
187237  jsonEachClose,             /* xClose - close a cursor */
187238  jsonEachFilter,            /* xFilter - configure scan constraints */
187239  jsonEachNext,              /* xNext - advance a cursor */
187240  jsonEachEof,               /* xEof - check for end of scan */
187241  jsonEachColumn,            /* xColumn - read data */
187242  jsonEachRowid,             /* xRowid - read data */
187243  0,                         /* xUpdate */
187244  0,                         /* xBegin */
187245  0,                         /* xSync */
187246  0,                         /* xCommit */
187247  0,                         /* xRollback */
187248  0,                         /* xFindMethod */
187249  0,                         /* xRename */
187250  0,                         /* xSavepoint */
187251  0,                         /* xRelease */
187252  0,                         /* xRollbackTo */
187253  0                          /* xShadowName */
187254};
187255#endif /* SQLITE_OMIT_VIRTUALTABLE */
187256
187257/****************************************************************************
187258** The following routines are the only publically visible identifiers in this
187259** file.  Call the following routines in order to register the various SQL
187260** functions and the virtual table implemented by this file.
187261****************************************************************************/
187262
187263SQLITE_PRIVATE int sqlite3Json1Init(sqlite3 *db){
187264  int rc = SQLITE_OK;
187265  unsigned int i;
187266  static const struct {
187267     const char *zName;
187268     int nArg;
187269     int flag;
187270     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
187271  } aFunc[] = {
187272    { "json",                 1, 0,   jsonRemoveFunc        },
187273    { "json_array",          -1, 0,   jsonArrayFunc         },
187274    { "json_array_length",    1, 0,   jsonArrayLengthFunc   },
187275    { "json_array_length",    2, 0,   jsonArrayLengthFunc   },
187276    { "json_extract",        -1, 0,   jsonExtractFunc       },
187277    { "json_insert",         -1, 0,   jsonSetFunc           },
187278    { "json_object",         -1, 0,   jsonObjectFunc        },
187279    { "json_patch",           2, 0,   jsonPatchFunc         },
187280    { "json_quote",           1, 0,   jsonQuoteFunc         },
187281    { "json_remove",         -1, 0,   jsonRemoveFunc        },
187282    { "json_replace",        -1, 0,   jsonReplaceFunc       },
187283    { "json_set",            -1, 1,   jsonSetFunc           },
187284    { "json_type",            1, 0,   jsonTypeFunc          },
187285    { "json_type",            2, 0,   jsonTypeFunc          },
187286    { "json_valid",           1, 0,   jsonValidFunc         },
187287
187288#if SQLITE_DEBUG
187289    /* DEBUG and TESTING functions */
187290    { "json_parse",           1, 0,   jsonParseFunc         },
187291    { "json_test1",           1, 0,   jsonTest1Func         },
187292#endif
187293  };
187294  static const struct {
187295     const char *zName;
187296     int nArg;
187297     void (*xStep)(sqlite3_context*,int,sqlite3_value**);
187298     void (*xFinal)(sqlite3_context*);
187299     void (*xValue)(sqlite3_context*);
187300  } aAgg[] = {
187301    { "json_group_array",     1,
187302      jsonArrayStep,   jsonArrayFinal,  jsonArrayValue  },
187303    { "json_group_object",    2,
187304      jsonObjectStep,  jsonObjectFinal, jsonObjectValue },
187305  };
187306#ifndef SQLITE_OMIT_VIRTUALTABLE
187307  static const struct {
187308     const char *zName;
187309     sqlite3_module *pModule;
187310  } aMod[] = {
187311    { "json_each",            &jsonEachModule               },
187312    { "json_tree",            &jsonTreeModule               },
187313  };
187314#endif
187315  static const int enc =
187316       SQLITE_UTF8 |
187317       SQLITE_DETERMINISTIC |
187318       SQLITE_INNOCUOUS;
187319  for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
187320    rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg, enc,
187321                                 (void*)&aFunc[i].flag,
187322                                 aFunc[i].xFunc, 0, 0);
187323  }
187324#ifndef SQLITE_OMIT_WINDOWFUNC
187325  for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
187326    rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
187327                                 SQLITE_SUBTYPE | enc, 0,
187328                                 aAgg[i].xStep, aAgg[i].xFinal,
187329                                 aAgg[i].xValue, jsonGroupInverse, 0);
187330  }
187331#endif
187332#ifndef SQLITE_OMIT_VIRTUALTABLE
187333  for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
187334    rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
187335  }
187336#endif
187337  return rc;
187338}
187339
187340
187341#ifndef SQLITE_CORE
187342#ifdef _WIN32
187343__declspec(dllexport)
187344#endif
187345SQLITE_API int sqlite3_json_init(
187346  sqlite3 *db,
187347  char **pzErrMsg,
187348  const sqlite3_api_routines *pApi
187349){
187350  SQLITE_EXTENSION_INIT2(pApi);
187351  (void)pzErrMsg;  /* Unused parameter */
187352  return sqlite3Json1Init(db);
187353}
187354#endif
187355#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */
187356
187357/************** End of json1.c ***********************************************/
187358/************** Begin file rtree.c *******************************************/
187359/*
187360** 2001 September 15
187361**
187362** The author disclaims copyright to this source code.  In place of
187363** a legal notice, here is a blessing:
187364**
187365**    May you do good and not evil.
187366**    May you find forgiveness for yourself and forgive others.
187367**    May you share freely, never taking more than you give.
187368**
187369*************************************************************************
187370** This file contains code for implementations of the r-tree and r*-tree
187371** algorithms packaged as an SQLite virtual table module.
187372*/
187373
187374/*
187375** Database Format of R-Tree Tables
187376** --------------------------------
187377**
187378** The data structure for a single virtual r-tree table is stored in three
187379** native SQLite tables declared as follows. In each case, the '%' character
187380** in the table name is replaced with the user-supplied name of the r-tree
187381** table.
187382**
187383**   CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
187384**   CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
187385**   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
187386**
187387** The data for each node of the r-tree structure is stored in the %_node
187388** table. For each node that is not the root node of the r-tree, there is
187389** an entry in the %_parent table associating the node with its parent.
187390** And for each row of data in the table, there is an entry in the %_rowid
187391** table that maps from the entries rowid to the id of the node that it
187392** is stored on.  If the r-tree contains auxiliary columns, those are stored
187393** on the end of the %_rowid table.
187394**
187395** The root node of an r-tree always exists, even if the r-tree table is
187396** empty. The nodeno of the root node is always 1. All other nodes in the
187397** table must be the same size as the root node. The content of each node
187398** is formatted as follows:
187399**
187400**   1. If the node is the root node (node 1), then the first 2 bytes
187401**      of the node contain the tree depth as a big-endian integer.
187402**      For non-root nodes, the first 2 bytes are left unused.
187403**
187404**   2. The next 2 bytes contain the number of entries currently
187405**      stored in the node.
187406**
187407**   3. The remainder of the node contains the node entries. Each entry
187408**      consists of a single 8-byte integer followed by an even number
187409**      of 4-byte coordinates. For leaf nodes the integer is the rowid
187410**      of a record. For internal nodes it is the node number of a
187411**      child page.
187412*/
187413
187414#if !defined(SQLITE_CORE) \
187415  || (defined(SQLITE_ENABLE_RTREE) && !defined(SQLITE_OMIT_VIRTUALTABLE))
187416
187417#ifndef SQLITE_CORE
187418/*   #include "sqlite3ext.h" */
187419  SQLITE_EXTENSION_INIT1
187420#else
187421/*   #include "sqlite3.h" */
187422#endif
187423SQLITE_PRIVATE int sqlite3GetToken(const unsigned char*,int*); /* In the SQLite core */
187424
187425#ifndef SQLITE_AMALGAMATION
187426#include "sqlite3rtree.h"
187427typedef sqlite3_int64 i64;
187428typedef sqlite3_uint64 u64;
187429typedef unsigned char u8;
187430typedef unsigned short u16;
187431typedef unsigned int u32;
187432#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
187433# define NDEBUG 1
187434#endif
187435#if defined(NDEBUG) && defined(SQLITE_DEBUG)
187436# undef NDEBUG
187437#endif
187438#endif
187439
187440/* #include <string.h> */
187441/* #include <stdio.h> */
187442/* #include <assert.h> */
187443
187444/*  The following macro is used to suppress compiler warnings.
187445*/
187446#ifndef UNUSED_PARAMETER
187447# define UNUSED_PARAMETER(x) (void)(x)
187448#endif
187449
187450typedef struct Rtree Rtree;
187451typedef struct RtreeCursor RtreeCursor;
187452typedef struct RtreeNode RtreeNode;
187453typedef struct RtreeCell RtreeCell;
187454typedef struct RtreeConstraint RtreeConstraint;
187455typedef struct RtreeMatchArg RtreeMatchArg;
187456typedef struct RtreeGeomCallback RtreeGeomCallback;
187457typedef union RtreeCoord RtreeCoord;
187458typedef struct RtreeSearchPoint RtreeSearchPoint;
187459
187460/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
187461#define RTREE_MAX_DIMENSIONS 5
187462
187463/* Maximum number of auxiliary columns */
187464#define RTREE_MAX_AUX_COLUMN 100
187465
187466/* Size of hash table Rtree.aHash. This hash table is not expected to
187467** ever contain very many entries, so a fixed number of buckets is
187468** used.
187469*/
187470#define HASHSIZE 97
187471
187472/* The xBestIndex method of this virtual table requires an estimate of
187473** the number of rows in the virtual table to calculate the costs of
187474** various strategies. If possible, this estimate is loaded from the
187475** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
187476** Otherwise, if no sqlite_stat1 entry is available, use
187477** RTREE_DEFAULT_ROWEST.
187478*/
187479#define RTREE_DEFAULT_ROWEST 1048576
187480#define RTREE_MIN_ROWEST         100
187481
187482/*
187483** An rtree virtual-table object.
187484*/
187485struct Rtree {
187486  sqlite3_vtab base;          /* Base class.  Must be first */
187487  sqlite3 *db;                /* Host database connection */
187488  int iNodeSize;              /* Size in bytes of each node in the node table */
187489  u8 nDim;                    /* Number of dimensions */
187490  u8 nDim2;                   /* Twice the number of dimensions */
187491  u8 eCoordType;              /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
187492  u8 nBytesPerCell;           /* Bytes consumed per cell */
187493  u8 inWrTrans;               /* True if inside write transaction */
187494  u8 nAux;                    /* # of auxiliary columns in %_rowid */
187495  u8 nAuxNotNull;             /* Number of initial not-null aux columns */
187496#ifdef SQLITE_DEBUG
187497  u8 bCorrupt;                /* Shadow table corruption detected */
187498#endif
187499  int iDepth;                 /* Current depth of the r-tree structure */
187500  char *zDb;                  /* Name of database containing r-tree table */
187501  char *zName;                /* Name of r-tree table */
187502  u32 nBusy;                  /* Current number of users of this structure */
187503  i64 nRowEst;                /* Estimated number of rows in this table */
187504  u32 nCursor;                /* Number of open cursors */
187505  u32 nNodeRef;               /* Number RtreeNodes with positive nRef */
187506  char *zReadAuxSql;          /* SQL for statement to read aux data */
187507
187508  /* List of nodes removed during a CondenseTree operation. List is
187509  ** linked together via the pointer normally used for hash chains -
187510  ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
187511  ** headed by the node (leaf nodes have RtreeNode.iNode==0).
187512  */
187513  RtreeNode *pDeleted;
187514  int iReinsertHeight;        /* Height of sub-trees Reinsert() has run on */
187515
187516  /* Blob I/O on xxx_node */
187517  sqlite3_blob *pNodeBlob;
187518
187519  /* Statements to read/write/delete a record from xxx_node */
187520  sqlite3_stmt *pWriteNode;
187521  sqlite3_stmt *pDeleteNode;
187522
187523  /* Statements to read/write/delete a record from xxx_rowid */
187524  sqlite3_stmt *pReadRowid;
187525  sqlite3_stmt *pWriteRowid;
187526  sqlite3_stmt *pDeleteRowid;
187527
187528  /* Statements to read/write/delete a record from xxx_parent */
187529  sqlite3_stmt *pReadParent;
187530  sqlite3_stmt *pWriteParent;
187531  sqlite3_stmt *pDeleteParent;
187532
187533  /* Statement for writing to the "aux:" fields, if there are any */
187534  sqlite3_stmt *pWriteAux;
187535
187536  RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
187537};
187538
187539/* Possible values for Rtree.eCoordType: */
187540#define RTREE_COORD_REAL32 0
187541#define RTREE_COORD_INT32  1
187542
187543/*
187544** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
187545** only deal with integer coordinates.  No floating point operations
187546** will be done.
187547*/
187548#ifdef SQLITE_RTREE_INT_ONLY
187549  typedef sqlite3_int64 RtreeDValue;       /* High accuracy coordinate */
187550  typedef int RtreeValue;                  /* Low accuracy coordinate */
187551# define RTREE_ZERO 0
187552#else
187553  typedef double RtreeDValue;              /* High accuracy coordinate */
187554  typedef float RtreeValue;                /* Low accuracy coordinate */
187555# define RTREE_ZERO 0.0
187556#endif
187557
187558/*
187559** Set the Rtree.bCorrupt flag
187560*/
187561#ifdef SQLITE_DEBUG
187562# define RTREE_IS_CORRUPT(X) ((X)->bCorrupt = 1)
187563#else
187564# define RTREE_IS_CORRUPT(X)
187565#endif
187566
187567/*
187568** When doing a search of an r-tree, instances of the following structure
187569** record intermediate results from the tree walk.
187570**
187571** The id is always a node-id.  For iLevel>=1 the id is the node-id of
187572** the node that the RtreeSearchPoint represents.  When iLevel==0, however,
187573** the id is of the parent node and the cell that RtreeSearchPoint
187574** represents is the iCell-th entry in the parent node.
187575*/
187576struct RtreeSearchPoint {
187577  RtreeDValue rScore;    /* The score for this node.  Smallest goes first. */
187578  sqlite3_int64 id;      /* Node ID */
187579  u8 iLevel;             /* 0=entries.  1=leaf node.  2+ for higher */
187580  u8 eWithin;            /* PARTLY_WITHIN or FULLY_WITHIN */
187581  u8 iCell;              /* Cell index within the node */
187582};
187583
187584/*
187585** The minimum number of cells allowed for a node is a third of the
187586** maximum. In Gutman's notation:
187587**
187588**     m = M/3
187589**
187590** If an R*-tree "Reinsert" operation is required, the same number of
187591** cells are removed from the overfull node and reinserted into the tree.
187592*/
187593#define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
187594#define RTREE_REINSERT(p) RTREE_MINCELLS(p)
187595#define RTREE_MAXCELLS 51
187596
187597/*
187598** The smallest possible node-size is (512-64)==448 bytes. And the largest
187599** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
187600** Therefore all non-root nodes must contain at least 3 entries. Since
187601** 3^40 is greater than 2^64, an r-tree structure always has a depth of
187602** 40 or less.
187603*/
187604#define RTREE_MAX_DEPTH 40
187605
187606
187607/*
187608** Number of entries in the cursor RtreeNode cache.  The first entry is
187609** used to cache the RtreeNode for RtreeCursor.sPoint.  The remaining
187610** entries cache the RtreeNode for the first elements of the priority queue.
187611*/
187612#define RTREE_CACHE_SZ  5
187613
187614/*
187615** An rtree cursor object.
187616*/
187617struct RtreeCursor {
187618  sqlite3_vtab_cursor base;         /* Base class.  Must be first */
187619  u8 atEOF;                         /* True if at end of search */
187620  u8 bPoint;                        /* True if sPoint is valid */
187621  u8 bAuxValid;                     /* True if pReadAux is valid */
187622  int iStrategy;                    /* Copy of idxNum search parameter */
187623  int nConstraint;                  /* Number of entries in aConstraint */
187624  RtreeConstraint *aConstraint;     /* Search constraints. */
187625  int nPointAlloc;                  /* Number of slots allocated for aPoint[] */
187626  int nPoint;                       /* Number of slots used in aPoint[] */
187627  int mxLevel;                      /* iLevel value for root of the tree */
187628  RtreeSearchPoint *aPoint;         /* Priority queue for search points */
187629  sqlite3_stmt *pReadAux;           /* Statement to read aux-data */
187630  RtreeSearchPoint sPoint;          /* Cached next search point */
187631  RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */
187632  u32 anQueue[RTREE_MAX_DEPTH+1];   /* Number of queued entries by iLevel */
187633};
187634
187635/* Return the Rtree of a RtreeCursor */
187636#define RTREE_OF_CURSOR(X)   ((Rtree*)((X)->base.pVtab))
187637
187638/*
187639** A coordinate can be either a floating point number or a integer.  All
187640** coordinates within a single R-Tree are always of the same time.
187641*/
187642union RtreeCoord {
187643  RtreeValue f;      /* Floating point value */
187644  int i;             /* Integer value */
187645  u32 u;             /* Unsigned for byte-order conversions */
187646};
187647
187648/*
187649** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
187650** formatted as a RtreeDValue (double or int64). This macro assumes that local
187651** variable pRtree points to the Rtree structure associated with the
187652** RtreeCoord.
187653*/
187654#ifdef SQLITE_RTREE_INT_ONLY
187655# define DCOORD(coord) ((RtreeDValue)coord.i)
187656#else
187657# define DCOORD(coord) (                           \
187658    (pRtree->eCoordType==RTREE_COORD_REAL32) ?      \
187659      ((double)coord.f) :                           \
187660      ((double)coord.i)                             \
187661  )
187662#endif
187663
187664/*
187665** A search constraint.
187666*/
187667struct RtreeConstraint {
187668  int iCoord;                     /* Index of constrained coordinate */
187669  int op;                         /* Constraining operation */
187670  union {
187671    RtreeDValue rValue;             /* Constraint value. */
187672    int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*);
187673    int (*xQueryFunc)(sqlite3_rtree_query_info*);
187674  } u;
187675  sqlite3_rtree_query_info *pInfo;  /* xGeom and xQueryFunc argument */
187676};
187677
187678/* Possible values for RtreeConstraint.op */
187679#define RTREE_EQ    0x41  /* A */
187680#define RTREE_LE    0x42  /* B */
187681#define RTREE_LT    0x43  /* C */
187682#define RTREE_GE    0x44  /* D */
187683#define RTREE_GT    0x45  /* E */
187684#define RTREE_MATCH 0x46  /* F: Old-style sqlite3_rtree_geometry_callback() */
187685#define RTREE_QUERY 0x47  /* G: New-style sqlite3_rtree_query_callback() */
187686
187687/* Special operators available only on cursors.  Needs to be consecutive
187688** with the normal values above, but must be less than RTREE_MATCH.  These
187689** are used in the cursor for contraints such as x=NULL (RTREE_FALSE) or
187690** x<'xyz' (RTREE_TRUE) */
187691#define RTREE_TRUE  0x3f  /* ? */
187692#define RTREE_FALSE 0x40  /* @ */
187693
187694/*
187695** An rtree structure node.
187696*/
187697struct RtreeNode {
187698  RtreeNode *pParent;         /* Parent node */
187699  i64 iNode;                  /* The node number */
187700  int nRef;                   /* Number of references to this node */
187701  int isDirty;                /* True if the node needs to be written to disk */
187702  u8 *zData;                  /* Content of the node, as should be on disk */
187703  RtreeNode *pNext;           /* Next node in this hash collision chain */
187704};
187705
187706/* Return the number of cells in a node  */
187707#define NCELL(pNode) readInt16(&(pNode)->zData[2])
187708
187709/*
187710** A single cell from a node, deserialized
187711*/
187712struct RtreeCell {
187713  i64 iRowid;                                 /* Node or entry ID */
187714  RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];  /* Bounding box coordinates */
187715};
187716
187717
187718/*
187719** This object becomes the sqlite3_user_data() for the SQL functions
187720** that are created by sqlite3_rtree_geometry_callback() and
187721** sqlite3_rtree_query_callback() and which appear on the right of MATCH
187722** operators in order to constrain a search.
187723**
187724** xGeom and xQueryFunc are the callback functions.  Exactly one of
187725** xGeom and xQueryFunc fields is non-NULL, depending on whether the
187726** SQL function was created using sqlite3_rtree_geometry_callback() or
187727** sqlite3_rtree_query_callback().
187728**
187729** This object is deleted automatically by the destructor mechanism in
187730** sqlite3_create_function_v2().
187731*/
187732struct RtreeGeomCallback {
187733  int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
187734  int (*xQueryFunc)(sqlite3_rtree_query_info*);
187735  void (*xDestructor)(void*);
187736  void *pContext;
187737};
187738
187739/*
187740** An instance of this structure (in the form of a BLOB) is returned by
187741** the SQL functions that sqlite3_rtree_geometry_callback() and
187742** sqlite3_rtree_query_callback() create, and is read as the right-hand
187743** operand to the MATCH operator of an R-Tree.
187744*/
187745struct RtreeMatchArg {
187746  u32 iSize;                  /* Size of this object */
187747  RtreeGeomCallback cb;       /* Info about the callback functions */
187748  int nParam;                 /* Number of parameters to the SQL function */
187749  sqlite3_value **apSqlParam; /* Original SQL parameter values */
187750  RtreeDValue aParam[1];      /* Values for parameters to the SQL function */
187751};
187752
187753#ifndef MAX
187754# define MAX(x,y) ((x) < (y) ? (y) : (x))
187755#endif
187756#ifndef MIN
187757# define MIN(x,y) ((x) > (y) ? (y) : (x))
187758#endif
187759
187760/* What version of GCC is being used.  0 means GCC is not being used .
187761** Note that the GCC_VERSION macro will also be set correctly when using
187762** clang, since clang works hard to be gcc compatible.  So the gcc
187763** optimizations will also work when compiling with clang.
187764*/
187765#ifndef GCC_VERSION
187766#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
187767# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
187768#else
187769# define GCC_VERSION 0
187770#endif
187771#endif
187772
187773/* The testcase() macro should already be defined in the amalgamation.  If
187774** it is not, make it a no-op.
187775*/
187776#ifndef SQLITE_AMALGAMATION
187777# define testcase(X)
187778#endif
187779
187780/*
187781** Macros to determine whether the machine is big or little endian,
187782** and whether or not that determination is run-time or compile-time.
187783**
187784** For best performance, an attempt is made to guess at the byte-order
187785** using C-preprocessor macros.  If that is unsuccessful, or if
187786** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
187787** at run-time.
187788*/
187789#ifndef SQLITE_BYTEORDER
187790#if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
187791    defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
187792    defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
187793    defined(__arm__)
187794# define SQLITE_BYTEORDER    1234
187795#elif defined(sparc)    || defined(__ppc__)
187796# define SQLITE_BYTEORDER    4321
187797#else
187798# define SQLITE_BYTEORDER    0     /* 0 means "unknown at compile-time" */
187799#endif
187800#endif
187801
187802
187803/* What version of MSVC is being used.  0 means MSVC is not being used */
187804#ifndef MSVC_VERSION
187805#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
187806# define MSVC_VERSION _MSC_VER
187807#else
187808# define MSVC_VERSION 0
187809#endif
187810#endif
187811
187812/*
187813** Functions to deserialize a 16 bit integer, 32 bit real number and
187814** 64 bit integer. The deserialized value is returned.
187815*/
187816static int readInt16(u8 *p){
187817  return (p[0]<<8) + p[1];
187818}
187819static void readCoord(u8 *p, RtreeCoord *pCoord){
187820  assert( ((((char*)p) - (char*)0)&3)==0 );  /* p is always 4-byte aligned */
187821#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
187822  pCoord->u = _byteswap_ulong(*(u32*)p);
187823#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
187824  pCoord->u = __builtin_bswap32(*(u32*)p);
187825#elif SQLITE_BYTEORDER==4321
187826  pCoord->u = *(u32*)p;
187827#else
187828  pCoord->u = (
187829    (((u32)p[0]) << 24) +
187830    (((u32)p[1]) << 16) +
187831    (((u32)p[2]) <<  8) +
187832    (((u32)p[3]) <<  0)
187833  );
187834#endif
187835}
187836static i64 readInt64(u8 *p){
187837#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
187838  u64 x;
187839  memcpy(&x, p, 8);
187840  return (i64)_byteswap_uint64(x);
187841#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
187842  u64 x;
187843  memcpy(&x, p, 8);
187844  return (i64)__builtin_bswap64(x);
187845#elif SQLITE_BYTEORDER==4321
187846  i64 x;
187847  memcpy(&x, p, 8);
187848  return x;
187849#else
187850  return (i64)(
187851    (((u64)p[0]) << 56) +
187852    (((u64)p[1]) << 48) +
187853    (((u64)p[2]) << 40) +
187854    (((u64)p[3]) << 32) +
187855    (((u64)p[4]) << 24) +
187856    (((u64)p[5]) << 16) +
187857    (((u64)p[6]) <<  8) +
187858    (((u64)p[7]) <<  0)
187859  );
187860#endif
187861}
187862
187863/*
187864** Functions to serialize a 16 bit integer, 32 bit real number and
187865** 64 bit integer. The value returned is the number of bytes written
187866** to the argument buffer (always 2, 4 and 8 respectively).
187867*/
187868static void writeInt16(u8 *p, int i){
187869  p[0] = (i>> 8)&0xFF;
187870  p[1] = (i>> 0)&0xFF;
187871}
187872static int writeCoord(u8 *p, RtreeCoord *pCoord){
187873  u32 i;
187874  assert( ((((char*)p) - (char*)0)&3)==0 );  /* p is always 4-byte aligned */
187875  assert( sizeof(RtreeCoord)==4 );
187876  assert( sizeof(u32)==4 );
187877#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
187878  i = __builtin_bswap32(pCoord->u);
187879  memcpy(p, &i, 4);
187880#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
187881  i = _byteswap_ulong(pCoord->u);
187882  memcpy(p, &i, 4);
187883#elif SQLITE_BYTEORDER==4321
187884  i = pCoord->u;
187885  memcpy(p, &i, 4);
187886#else
187887  i = pCoord->u;
187888  p[0] = (i>>24)&0xFF;
187889  p[1] = (i>>16)&0xFF;
187890  p[2] = (i>> 8)&0xFF;
187891  p[3] = (i>> 0)&0xFF;
187892#endif
187893  return 4;
187894}
187895static int writeInt64(u8 *p, i64 i){
187896#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
187897  i = (i64)__builtin_bswap64((u64)i);
187898  memcpy(p, &i, 8);
187899#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
187900  i = (i64)_byteswap_uint64((u64)i);
187901  memcpy(p, &i, 8);
187902#elif SQLITE_BYTEORDER==4321
187903  memcpy(p, &i, 8);
187904#else
187905  p[0] = (i>>56)&0xFF;
187906  p[1] = (i>>48)&0xFF;
187907  p[2] = (i>>40)&0xFF;
187908  p[3] = (i>>32)&0xFF;
187909  p[4] = (i>>24)&0xFF;
187910  p[5] = (i>>16)&0xFF;
187911  p[6] = (i>> 8)&0xFF;
187912  p[7] = (i>> 0)&0xFF;
187913#endif
187914  return 8;
187915}
187916
187917/*
187918** Increment the reference count of node p.
187919*/
187920static void nodeReference(RtreeNode *p){
187921  if( p ){
187922    assert( p->nRef>0 );
187923    p->nRef++;
187924  }
187925}
187926
187927/*
187928** Clear the content of node p (set all bytes to 0x00).
187929*/
187930static void nodeZero(Rtree *pRtree, RtreeNode *p){
187931  memset(&p->zData[2], 0, pRtree->iNodeSize-2);
187932  p->isDirty = 1;
187933}
187934
187935/*
187936** Given a node number iNode, return the corresponding key to use
187937** in the Rtree.aHash table.
187938*/
187939static unsigned int nodeHash(i64 iNode){
187940  return ((unsigned)iNode) % HASHSIZE;
187941}
187942
187943/*
187944** Search the node hash table for node iNode. If found, return a pointer
187945** to it. Otherwise, return 0.
187946*/
187947static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
187948  RtreeNode *p;
187949  for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
187950  return p;
187951}
187952
187953/*
187954** Add node pNode to the node hash table.
187955*/
187956static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
187957  int iHash;
187958  assert( pNode->pNext==0 );
187959  iHash = nodeHash(pNode->iNode);
187960  pNode->pNext = pRtree->aHash[iHash];
187961  pRtree->aHash[iHash] = pNode;
187962}
187963
187964/*
187965** Remove node pNode from the node hash table.
187966*/
187967static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
187968  RtreeNode **pp;
187969  if( pNode->iNode!=0 ){
187970    pp = &pRtree->aHash[nodeHash(pNode->iNode)];
187971    for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
187972    *pp = pNode->pNext;
187973    pNode->pNext = 0;
187974  }
187975}
187976
187977/*
187978** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
187979** indicating that node has not yet been assigned a node number. It is
187980** assigned a node number when nodeWrite() is called to write the
187981** node contents out to the database.
187982*/
187983static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
187984  RtreeNode *pNode;
187985  pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode) + pRtree->iNodeSize);
187986  if( pNode ){
187987    memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
187988    pNode->zData = (u8 *)&pNode[1];
187989    pNode->nRef = 1;
187990    pRtree->nNodeRef++;
187991    pNode->pParent = pParent;
187992    pNode->isDirty = 1;
187993    nodeReference(pParent);
187994  }
187995  return pNode;
187996}
187997
187998/*
187999** Clear the Rtree.pNodeBlob object
188000*/
188001static void nodeBlobReset(Rtree *pRtree){
188002  if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
188003    sqlite3_blob *pBlob = pRtree->pNodeBlob;
188004    pRtree->pNodeBlob = 0;
188005    sqlite3_blob_close(pBlob);
188006  }
188007}
188008
188009/*
188010** Check to see if pNode is the same as pParent or any of the parents
188011** of pParent.
188012*/
188013static int nodeInParentChain(const RtreeNode *pNode, const RtreeNode *pParent){
188014  do{
188015    if( pNode==pParent ) return 1;
188016    pParent = pParent->pParent;
188017  }while( pParent );
188018  return 0;
188019}
188020
188021/*
188022** Obtain a reference to an r-tree node.
188023*/
188024static int nodeAcquire(
188025  Rtree *pRtree,             /* R-tree structure */
188026  i64 iNode,                 /* Node number to load */
188027  RtreeNode *pParent,        /* Either the parent node or NULL */
188028  RtreeNode **ppNode         /* OUT: Acquired node */
188029){
188030  int rc = SQLITE_OK;
188031  RtreeNode *pNode = 0;
188032
188033  /* Check if the requested node is already in the hash table. If so,
188034  ** increase its reference count and return it.
188035  */
188036  if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){
188037    if( pParent && !pNode->pParent ){
188038      if( nodeInParentChain(pNode, pParent) ){
188039        RTREE_IS_CORRUPT(pRtree);
188040        return SQLITE_CORRUPT_VTAB;
188041      }
188042      pParent->nRef++;
188043      pNode->pParent = pParent;
188044    }else if( pParent && pNode->pParent && pParent!=pNode->pParent ){
188045      RTREE_IS_CORRUPT(pRtree);
188046      return SQLITE_CORRUPT_VTAB;
188047    }
188048    pNode->nRef++;
188049    *ppNode = pNode;
188050    return SQLITE_OK;
188051  }
188052
188053  if( pRtree->pNodeBlob ){
188054    sqlite3_blob *pBlob = pRtree->pNodeBlob;
188055    pRtree->pNodeBlob = 0;
188056    rc = sqlite3_blob_reopen(pBlob, iNode);
188057    pRtree->pNodeBlob = pBlob;
188058    if( rc ){
188059      nodeBlobReset(pRtree);
188060      if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
188061    }
188062  }
188063  if( pRtree->pNodeBlob==0 ){
188064    char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
188065    if( zTab==0 ) return SQLITE_NOMEM;
188066    rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
188067                           &pRtree->pNodeBlob);
188068    sqlite3_free(zTab);
188069  }
188070  if( rc ){
188071    nodeBlobReset(pRtree);
188072    *ppNode = 0;
188073    /* If unable to open an sqlite3_blob on the desired row, that can only
188074    ** be because the shadow tables hold erroneous data. */
188075    if( rc==SQLITE_ERROR ){
188076      rc = SQLITE_CORRUPT_VTAB;
188077      RTREE_IS_CORRUPT(pRtree);
188078    }
188079  }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
188080    pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode)+pRtree->iNodeSize);
188081    if( !pNode ){
188082      rc = SQLITE_NOMEM;
188083    }else{
188084      pNode->pParent = pParent;
188085      pNode->zData = (u8 *)&pNode[1];
188086      pNode->nRef = 1;
188087      pRtree->nNodeRef++;
188088      pNode->iNode = iNode;
188089      pNode->isDirty = 0;
188090      pNode->pNext = 0;
188091      rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
188092                             pRtree->iNodeSize, 0);
188093    }
188094  }
188095
188096  /* If the root node was just loaded, set pRtree->iDepth to the height
188097  ** of the r-tree structure. A height of zero means all data is stored on
188098  ** the root node. A height of one means the children of the root node
188099  ** are the leaves, and so on. If the depth as specified on the root node
188100  ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
188101  */
188102  if( pNode && iNode==1 ){
188103    pRtree->iDepth = readInt16(pNode->zData);
188104    if( pRtree->iDepth>RTREE_MAX_DEPTH ){
188105      rc = SQLITE_CORRUPT_VTAB;
188106      RTREE_IS_CORRUPT(pRtree);
188107    }
188108  }
188109
188110  /* If no error has occurred so far, check if the "number of entries"
188111  ** field on the node is too large. If so, set the return code to
188112  ** SQLITE_CORRUPT_VTAB.
188113  */
188114  if( pNode && rc==SQLITE_OK ){
188115    if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
188116      rc = SQLITE_CORRUPT_VTAB;
188117      RTREE_IS_CORRUPT(pRtree);
188118    }
188119  }
188120
188121  if( rc==SQLITE_OK ){
188122    if( pNode!=0 ){
188123      nodeReference(pParent);
188124      nodeHashInsert(pRtree, pNode);
188125    }else{
188126      rc = SQLITE_CORRUPT_VTAB;
188127      RTREE_IS_CORRUPT(pRtree);
188128    }
188129    *ppNode = pNode;
188130  }else{
188131    if( pNode ){
188132      pRtree->nNodeRef--;
188133      sqlite3_free(pNode);
188134    }
188135    *ppNode = 0;
188136  }
188137
188138  return rc;
188139}
188140
188141/*
188142** Overwrite cell iCell of node pNode with the contents of pCell.
188143*/
188144static void nodeOverwriteCell(
188145  Rtree *pRtree,             /* The overall R-Tree */
188146  RtreeNode *pNode,          /* The node into which the cell is to be written */
188147  RtreeCell *pCell,          /* The cell to write */
188148  int iCell                  /* Index into pNode into which pCell is written */
188149){
188150  int ii;
188151  u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
188152  p += writeInt64(p, pCell->iRowid);
188153  for(ii=0; ii<pRtree->nDim2; ii++){
188154    p += writeCoord(p, &pCell->aCoord[ii]);
188155  }
188156  pNode->isDirty = 1;
188157}
188158
188159/*
188160** Remove the cell with index iCell from node pNode.
188161*/
188162static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
188163  u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
188164  u8 *pSrc = &pDst[pRtree->nBytesPerCell];
188165  int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
188166  memmove(pDst, pSrc, nByte);
188167  writeInt16(&pNode->zData[2], NCELL(pNode)-1);
188168  pNode->isDirty = 1;
188169}
188170
188171/*
188172** Insert the contents of cell pCell into node pNode. If the insert
188173** is successful, return SQLITE_OK.
188174**
188175** If there is not enough free space in pNode, return SQLITE_FULL.
188176*/
188177static int nodeInsertCell(
188178  Rtree *pRtree,                /* The overall R-Tree */
188179  RtreeNode *pNode,             /* Write new cell into this node */
188180  RtreeCell *pCell              /* The cell to be inserted */
188181){
188182  int nCell;                    /* Current number of cells in pNode */
188183  int nMaxCell;                 /* Maximum number of cells for pNode */
188184
188185  nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
188186  nCell = NCELL(pNode);
188187
188188  assert( nCell<=nMaxCell );
188189  if( nCell<nMaxCell ){
188190    nodeOverwriteCell(pRtree, pNode, pCell, nCell);
188191    writeInt16(&pNode->zData[2], nCell+1);
188192    pNode->isDirty = 1;
188193  }
188194
188195  return (nCell==nMaxCell);
188196}
188197
188198/*
188199** If the node is dirty, write it out to the database.
188200*/
188201static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){
188202  int rc = SQLITE_OK;
188203  if( pNode->isDirty ){
188204    sqlite3_stmt *p = pRtree->pWriteNode;
188205    if( pNode->iNode ){
188206      sqlite3_bind_int64(p, 1, pNode->iNode);
188207    }else{
188208      sqlite3_bind_null(p, 1);
188209    }
188210    sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
188211    sqlite3_step(p);
188212    pNode->isDirty = 0;
188213    rc = sqlite3_reset(p);
188214    sqlite3_bind_null(p, 2);
188215    if( pNode->iNode==0 && rc==SQLITE_OK ){
188216      pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
188217      nodeHashInsert(pRtree, pNode);
188218    }
188219  }
188220  return rc;
188221}
188222
188223/*
188224** Release a reference to a node. If the node is dirty and the reference
188225** count drops to zero, the node data is written to the database.
188226*/
188227static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
188228  int rc = SQLITE_OK;
188229  if( pNode ){
188230    assert( pNode->nRef>0 );
188231    assert( pRtree->nNodeRef>0 );
188232    pNode->nRef--;
188233    if( pNode->nRef==0 ){
188234      pRtree->nNodeRef--;
188235      if( pNode->iNode==1 ){
188236        pRtree->iDepth = -1;
188237      }
188238      if( pNode->pParent ){
188239        rc = nodeRelease(pRtree, pNode->pParent);
188240      }
188241      if( rc==SQLITE_OK ){
188242        rc = nodeWrite(pRtree, pNode);
188243      }
188244      nodeHashDelete(pRtree, pNode);
188245      sqlite3_free(pNode);
188246    }
188247  }
188248  return rc;
188249}
188250
188251/*
188252** Return the 64-bit integer value associated with cell iCell of
188253** node pNode. If pNode is a leaf node, this is a rowid. If it is
188254** an internal node, then the 64-bit integer is a child page number.
188255*/
188256static i64 nodeGetRowid(
188257  Rtree *pRtree,       /* The overall R-Tree */
188258  RtreeNode *pNode,    /* The node from which to extract the ID */
188259  int iCell            /* The cell index from which to extract the ID */
188260){
188261  assert( iCell<NCELL(pNode) );
188262  return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
188263}
188264
188265/*
188266** Return coordinate iCoord from cell iCell in node pNode.
188267*/
188268static void nodeGetCoord(
188269  Rtree *pRtree,               /* The overall R-Tree */
188270  RtreeNode *pNode,            /* The node from which to extract a coordinate */
188271  int iCell,                   /* The index of the cell within the node */
188272  int iCoord,                  /* Which coordinate to extract */
188273  RtreeCoord *pCoord           /* OUT: Space to write result to */
188274){
188275  readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
188276}
188277
188278/*
188279** Deserialize cell iCell of node pNode. Populate the structure pointed
188280** to by pCell with the results.
188281*/
188282static void nodeGetCell(
188283  Rtree *pRtree,               /* The overall R-Tree */
188284  RtreeNode *pNode,            /* The node containing the cell to be read */
188285  int iCell,                   /* Index of the cell within the node */
188286  RtreeCell *pCell             /* OUT: Write the cell contents here */
188287){
188288  u8 *pData;
188289  RtreeCoord *pCoord;
188290  int ii = 0;
188291  pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
188292  pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
188293  pCoord = pCell->aCoord;
188294  do{
188295    readCoord(pData, &pCoord[ii]);
188296    readCoord(pData+4, &pCoord[ii+1]);
188297    pData += 8;
188298    ii += 2;
188299  }while( ii<pRtree->nDim2 );
188300}
188301
188302
188303/* Forward declaration for the function that does the work of
188304** the virtual table module xCreate() and xConnect() methods.
188305*/
188306static int rtreeInit(
188307  sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
188308);
188309
188310/*
188311** Rtree virtual table module xCreate method.
188312*/
188313static int rtreeCreate(
188314  sqlite3 *db,
188315  void *pAux,
188316  int argc, const char *const*argv,
188317  sqlite3_vtab **ppVtab,
188318  char **pzErr
188319){
188320  return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
188321}
188322
188323/*
188324** Rtree virtual table module xConnect method.
188325*/
188326static int rtreeConnect(
188327  sqlite3 *db,
188328  void *pAux,
188329  int argc, const char *const*argv,
188330  sqlite3_vtab **ppVtab,
188331  char **pzErr
188332){
188333  return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
188334}
188335
188336/*
188337** Increment the r-tree reference count.
188338*/
188339static void rtreeReference(Rtree *pRtree){
188340  pRtree->nBusy++;
188341}
188342
188343/*
188344** Decrement the r-tree reference count. When the reference count reaches
188345** zero the structure is deleted.
188346*/
188347static void rtreeRelease(Rtree *pRtree){
188348  pRtree->nBusy--;
188349  if( pRtree->nBusy==0 ){
188350    pRtree->inWrTrans = 0;
188351    assert( pRtree->nCursor==0 );
188352    nodeBlobReset(pRtree);
188353    assert( pRtree->nNodeRef==0 || pRtree->bCorrupt );
188354    sqlite3_finalize(pRtree->pWriteNode);
188355    sqlite3_finalize(pRtree->pDeleteNode);
188356    sqlite3_finalize(pRtree->pReadRowid);
188357    sqlite3_finalize(pRtree->pWriteRowid);
188358    sqlite3_finalize(pRtree->pDeleteRowid);
188359    sqlite3_finalize(pRtree->pReadParent);
188360    sqlite3_finalize(pRtree->pWriteParent);
188361    sqlite3_finalize(pRtree->pDeleteParent);
188362    sqlite3_finalize(pRtree->pWriteAux);
188363    sqlite3_free(pRtree->zReadAuxSql);
188364    sqlite3_free(pRtree);
188365  }
188366}
188367
188368/*
188369** Rtree virtual table module xDisconnect method.
188370*/
188371static int rtreeDisconnect(sqlite3_vtab *pVtab){
188372  rtreeRelease((Rtree *)pVtab);
188373  return SQLITE_OK;
188374}
188375
188376/*
188377** Rtree virtual table module xDestroy method.
188378*/
188379static int rtreeDestroy(sqlite3_vtab *pVtab){
188380  Rtree *pRtree = (Rtree *)pVtab;
188381  int rc;
188382  char *zCreate = sqlite3_mprintf(
188383    "DROP TABLE '%q'.'%q_node';"
188384    "DROP TABLE '%q'.'%q_rowid';"
188385    "DROP TABLE '%q'.'%q_parent';",
188386    pRtree->zDb, pRtree->zName,
188387    pRtree->zDb, pRtree->zName,
188388    pRtree->zDb, pRtree->zName
188389  );
188390  if( !zCreate ){
188391    rc = SQLITE_NOMEM;
188392  }else{
188393    nodeBlobReset(pRtree);
188394    rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
188395    sqlite3_free(zCreate);
188396  }
188397  if( rc==SQLITE_OK ){
188398    rtreeRelease(pRtree);
188399  }
188400
188401  return rc;
188402}
188403
188404/*
188405** Rtree virtual table module xOpen method.
188406*/
188407static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
188408  int rc = SQLITE_NOMEM;
188409  Rtree *pRtree = (Rtree *)pVTab;
188410  RtreeCursor *pCsr;
188411
188412  pCsr = (RtreeCursor *)sqlite3_malloc64(sizeof(RtreeCursor));
188413  if( pCsr ){
188414    memset(pCsr, 0, sizeof(RtreeCursor));
188415    pCsr->base.pVtab = pVTab;
188416    rc = SQLITE_OK;
188417    pRtree->nCursor++;
188418  }
188419  *ppCursor = (sqlite3_vtab_cursor *)pCsr;
188420
188421  return rc;
188422}
188423
188424
188425/*
188426** Reset a cursor back to its initial state.
188427*/
188428static void resetCursor(RtreeCursor *pCsr){
188429  Rtree *pRtree = (Rtree *)(pCsr->base.pVtab);
188430  int ii;
188431  sqlite3_stmt *pStmt;
188432  if( pCsr->aConstraint ){
188433    int i;                        /* Used to iterate through constraint array */
188434    for(i=0; i<pCsr->nConstraint; i++){
188435      sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
188436      if( pInfo ){
188437        if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
188438        sqlite3_free(pInfo);
188439      }
188440    }
188441    sqlite3_free(pCsr->aConstraint);
188442    pCsr->aConstraint = 0;
188443  }
188444  for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
188445  sqlite3_free(pCsr->aPoint);
188446  pStmt = pCsr->pReadAux;
188447  memset(pCsr, 0, sizeof(RtreeCursor));
188448  pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
188449  pCsr->pReadAux = pStmt;
188450
188451}
188452
188453/*
188454** Rtree virtual table module xClose method.
188455*/
188456static int rtreeClose(sqlite3_vtab_cursor *cur){
188457  Rtree *pRtree = (Rtree *)(cur->pVtab);
188458  RtreeCursor *pCsr = (RtreeCursor *)cur;
188459  assert( pRtree->nCursor>0 );
188460  resetCursor(pCsr);
188461  sqlite3_finalize(pCsr->pReadAux);
188462  sqlite3_free(pCsr);
188463  pRtree->nCursor--;
188464  nodeBlobReset(pRtree);
188465  return SQLITE_OK;
188466}
188467
188468/*
188469** Rtree virtual table module xEof method.
188470**
188471** Return non-zero if the cursor does not currently point to a valid
188472** record (i.e if the scan has finished), or zero otherwise.
188473*/
188474static int rtreeEof(sqlite3_vtab_cursor *cur){
188475  RtreeCursor *pCsr = (RtreeCursor *)cur;
188476  return pCsr->atEOF;
188477}
188478
188479/*
188480** Convert raw bits from the on-disk RTree record into a coordinate value.
188481** The on-disk format is big-endian and needs to be converted for little-
188482** endian platforms.  The on-disk record stores integer coordinates if
188483** eInt is true and it stores 32-bit floating point records if eInt is
188484** false.  a[] is the four bytes of the on-disk record to be decoded.
188485** Store the results in "r".
188486**
188487** There are five versions of this macro.  The last one is generic.  The
188488** other four are various architectures-specific optimizations.
188489*/
188490#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
188491#define RTREE_DECODE_COORD(eInt, a, r) {                        \
188492    RtreeCoord c;    /* Coordinate decoded */                   \
188493    c.u = _byteswap_ulong(*(u32*)a);                            \
188494    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
188495}
188496#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
188497#define RTREE_DECODE_COORD(eInt, a, r) {                        \
188498    RtreeCoord c;    /* Coordinate decoded */                   \
188499    c.u = __builtin_bswap32(*(u32*)a);                          \
188500    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
188501}
188502#elif SQLITE_BYTEORDER==1234
188503#define RTREE_DECODE_COORD(eInt, a, r) {                        \
188504    RtreeCoord c;    /* Coordinate decoded */                   \
188505    memcpy(&c.u,a,4);                                           \
188506    c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)|                   \
188507          ((c.u&0xff)<<24)|((c.u&0xff00)<<8);                   \
188508    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
188509}
188510#elif SQLITE_BYTEORDER==4321
188511#define RTREE_DECODE_COORD(eInt, a, r) {                        \
188512    RtreeCoord c;    /* Coordinate decoded */                   \
188513    memcpy(&c.u,a,4);                                           \
188514    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
188515}
188516#else
188517#define RTREE_DECODE_COORD(eInt, a, r) {                        \
188518    RtreeCoord c;    /* Coordinate decoded */                   \
188519    c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16)                     \
188520           +((u32)a[2]<<8) + a[3];                              \
188521    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
188522}
188523#endif
188524
188525/*
188526** Check the RTree node or entry given by pCellData and p against the MATCH
188527** constraint pConstraint.
188528*/
188529static int rtreeCallbackConstraint(
188530  RtreeConstraint *pConstraint,  /* The constraint to test */
188531  int eInt,                      /* True if RTree holding integer coordinates */
188532  u8 *pCellData,                 /* Raw cell content */
188533  RtreeSearchPoint *pSearch,     /* Container of this cell */
188534  sqlite3_rtree_dbl *prScore,    /* OUT: score for the cell */
188535  int *peWithin                  /* OUT: visibility of the cell */
188536){
188537  sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
188538  int nCoord = pInfo->nCoord;                           /* No. of coordinates */
188539  int rc;                                             /* Callback return code */
188540  RtreeCoord c;                                       /* Translator union */
188541  sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2];   /* Decoded coordinates */
188542
188543  assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
188544  assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
188545
188546  if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
188547    pInfo->iRowid = readInt64(pCellData);
188548  }
188549  pCellData += 8;
188550#ifndef SQLITE_RTREE_INT_ONLY
188551  if( eInt==0 ){
188552    switch( nCoord ){
188553      case 10:  readCoord(pCellData+36, &c); aCoord[9] = c.f;
188554                readCoord(pCellData+32, &c); aCoord[8] = c.f;
188555      case 8:   readCoord(pCellData+28, &c); aCoord[7] = c.f;
188556                readCoord(pCellData+24, &c); aCoord[6] = c.f;
188557      case 6:   readCoord(pCellData+20, &c); aCoord[5] = c.f;
188558                readCoord(pCellData+16, &c); aCoord[4] = c.f;
188559      case 4:   readCoord(pCellData+12, &c); aCoord[3] = c.f;
188560                readCoord(pCellData+8,  &c); aCoord[2] = c.f;
188561      default:  readCoord(pCellData+4,  &c); aCoord[1] = c.f;
188562                readCoord(pCellData,    &c); aCoord[0] = c.f;
188563    }
188564  }else
188565#endif
188566  {
188567    switch( nCoord ){
188568      case 10:  readCoord(pCellData+36, &c); aCoord[9] = c.i;
188569                readCoord(pCellData+32, &c); aCoord[8] = c.i;
188570      case 8:   readCoord(pCellData+28, &c); aCoord[7] = c.i;
188571                readCoord(pCellData+24, &c); aCoord[6] = c.i;
188572      case 6:   readCoord(pCellData+20, &c); aCoord[5] = c.i;
188573                readCoord(pCellData+16, &c); aCoord[4] = c.i;
188574      case 4:   readCoord(pCellData+12, &c); aCoord[3] = c.i;
188575                readCoord(pCellData+8,  &c); aCoord[2] = c.i;
188576      default:  readCoord(pCellData+4,  &c); aCoord[1] = c.i;
188577                readCoord(pCellData,    &c); aCoord[0] = c.i;
188578    }
188579  }
188580  if( pConstraint->op==RTREE_MATCH ){
188581    int eWithin = 0;
188582    rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
188583                              nCoord, aCoord, &eWithin);
188584    if( eWithin==0 ) *peWithin = NOT_WITHIN;
188585    *prScore = RTREE_ZERO;
188586  }else{
188587    pInfo->aCoord = aCoord;
188588    pInfo->iLevel = pSearch->iLevel - 1;
188589    pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
188590    pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
188591    rc = pConstraint->u.xQueryFunc(pInfo);
188592    if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
188593    if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO ){
188594      *prScore = pInfo->rScore;
188595    }
188596  }
188597  return rc;
188598}
188599
188600/*
188601** Check the internal RTree node given by pCellData against constraint p.
188602** If this constraint cannot be satisfied by any child within the node,
188603** set *peWithin to NOT_WITHIN.
188604*/
188605static void rtreeNonleafConstraint(
188606  RtreeConstraint *p,        /* The constraint to test */
188607  int eInt,                  /* True if RTree holds integer coordinates */
188608  u8 *pCellData,             /* Raw cell content as appears on disk */
188609  int *peWithin              /* Adjust downward, as appropriate */
188610){
188611  sqlite3_rtree_dbl val;     /* Coordinate value convert to a double */
188612
188613  /* p->iCoord might point to either a lower or upper bound coordinate
188614  ** in a coordinate pair.  But make pCellData point to the lower bound.
188615  */
188616  pCellData += 8 + 4*(p->iCoord&0xfe);
188617
188618  assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
188619      || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_TRUE
188620      || p->op==RTREE_FALSE );
188621  assert( ((((char*)pCellData) - (char*)0)&3)==0 );  /* 4-byte aligned */
188622  switch( p->op ){
188623    case RTREE_TRUE:  return;   /* Always satisfied */
188624    case RTREE_FALSE: break;    /* Never satisfied */
188625    case RTREE_LE:
188626    case RTREE_LT:
188627    case RTREE_EQ:
188628      RTREE_DECODE_COORD(eInt, pCellData, val);
188629      /* val now holds the lower bound of the coordinate pair */
188630      if( p->u.rValue>=val ) return;
188631      if( p->op!=RTREE_EQ ) break;  /* RTREE_LE and RTREE_LT end here */
188632      /* Fall through for the RTREE_EQ case */
188633
188634    default: /* RTREE_GT or RTREE_GE,  or fallthrough of RTREE_EQ */
188635      pCellData += 4;
188636      RTREE_DECODE_COORD(eInt, pCellData, val);
188637      /* val now holds the upper bound of the coordinate pair */
188638      if( p->u.rValue<=val ) return;
188639  }
188640  *peWithin = NOT_WITHIN;
188641}
188642
188643/*
188644** Check the leaf RTree cell given by pCellData against constraint p.
188645** If this constraint is not satisfied, set *peWithin to NOT_WITHIN.
188646** If the constraint is satisfied, leave *peWithin unchanged.
188647**
188648** The constraint is of the form:  xN op $val
188649**
188650** The op is given by p->op.  The xN is p->iCoord-th coordinate in
188651** pCellData.  $val is given by p->u.rValue.
188652*/
188653static void rtreeLeafConstraint(
188654  RtreeConstraint *p,        /* The constraint to test */
188655  int eInt,                  /* True if RTree holds integer coordinates */
188656  u8 *pCellData,             /* Raw cell content as appears on disk */
188657  int *peWithin              /* Adjust downward, as appropriate */
188658){
188659  RtreeDValue xN;      /* Coordinate value converted to a double */
188660
188661  assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
188662      || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_TRUE
188663      || p->op==RTREE_FALSE );
188664  pCellData += 8 + p->iCoord*4;
188665  assert( ((((char*)pCellData) - (char*)0)&3)==0 );  /* 4-byte aligned */
188666  RTREE_DECODE_COORD(eInt, pCellData, xN);
188667  switch( p->op ){
188668    case RTREE_TRUE:  return;   /* Always satisfied */
188669    case RTREE_FALSE: break;    /* Never satisfied */
188670    case RTREE_LE:    if( xN <= p->u.rValue ) return;  break;
188671    case RTREE_LT:    if( xN <  p->u.rValue ) return;  break;
188672    case RTREE_GE:    if( xN >= p->u.rValue ) return;  break;
188673    case RTREE_GT:    if( xN >  p->u.rValue ) return;  break;
188674    default:          if( xN == p->u.rValue ) return;  break;
188675  }
188676  *peWithin = NOT_WITHIN;
188677}
188678
188679/*
188680** One of the cells in node pNode is guaranteed to have a 64-bit
188681** integer value equal to iRowid. Return the index of this cell.
188682*/
188683static int nodeRowidIndex(
188684  Rtree *pRtree,
188685  RtreeNode *pNode,
188686  i64 iRowid,
188687  int *piIndex
188688){
188689  int ii;
188690  int nCell = NCELL(pNode);
188691  assert( nCell<200 );
188692  for(ii=0; ii<nCell; ii++){
188693    if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
188694      *piIndex = ii;
188695      return SQLITE_OK;
188696    }
188697  }
188698  RTREE_IS_CORRUPT(pRtree);
188699  return SQLITE_CORRUPT_VTAB;
188700}
188701
188702/*
188703** Return the index of the cell containing a pointer to node pNode
188704** in its parent. If pNode is the root node, return -1.
188705*/
188706static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
188707  RtreeNode *pParent = pNode->pParent;
188708  if( pParent ){
188709    return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
188710  }
188711  *piIndex = -1;
188712  return SQLITE_OK;
188713}
188714
188715/*
188716** Compare two search points.  Return negative, zero, or positive if the first
188717** is less than, equal to, or greater than the second.
188718**
188719** The rScore is the primary key.  Smaller rScore values come first.
188720** If the rScore is a tie, then use iLevel as the tie breaker with smaller
188721** iLevel values coming first.  In this way, if rScore is the same for all
188722** SearchPoints, then iLevel becomes the deciding factor and the result
188723** is a depth-first search, which is the desired default behavior.
188724*/
188725static int rtreeSearchPointCompare(
188726  const RtreeSearchPoint *pA,
188727  const RtreeSearchPoint *pB
188728){
188729  if( pA->rScore<pB->rScore ) return -1;
188730  if( pA->rScore>pB->rScore ) return +1;
188731  if( pA->iLevel<pB->iLevel ) return -1;
188732  if( pA->iLevel>pB->iLevel ) return +1;
188733  return 0;
188734}
188735
188736/*
188737** Interchange two search points in a cursor.
188738*/
188739static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
188740  RtreeSearchPoint t = p->aPoint[i];
188741  assert( i<j );
188742  p->aPoint[i] = p->aPoint[j];
188743  p->aPoint[j] = t;
188744  i++; j++;
188745  if( i<RTREE_CACHE_SZ ){
188746    if( j>=RTREE_CACHE_SZ ){
188747      nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
188748      p->aNode[i] = 0;
188749    }else{
188750      RtreeNode *pTemp = p->aNode[i];
188751      p->aNode[i] = p->aNode[j];
188752      p->aNode[j] = pTemp;
188753    }
188754  }
188755}
188756
188757/*
188758** Return the search point with the lowest current score.
188759*/
188760static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
188761  return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
188762}
188763
188764/*
188765** Get the RtreeNode for the search point with the lowest score.
188766*/
188767static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
188768  sqlite3_int64 id;
188769  int ii = 1 - pCur->bPoint;
188770  assert( ii==0 || ii==1 );
188771  assert( pCur->bPoint || pCur->nPoint );
188772  if( pCur->aNode[ii]==0 ){
188773    assert( pRC!=0 );
188774    id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
188775    *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]);
188776  }
188777  return pCur->aNode[ii];
188778}
188779
188780/*
188781** Push a new element onto the priority queue
188782*/
188783static RtreeSearchPoint *rtreeEnqueue(
188784  RtreeCursor *pCur,    /* The cursor */
188785  RtreeDValue rScore,   /* Score for the new search point */
188786  u8 iLevel             /* Level for the new search point */
188787){
188788  int i, j;
188789  RtreeSearchPoint *pNew;
188790  if( pCur->nPoint>=pCur->nPointAlloc ){
188791    int nNew = pCur->nPointAlloc*2 + 8;
188792    pNew = sqlite3_realloc64(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
188793    if( pNew==0 ) return 0;
188794    pCur->aPoint = pNew;
188795    pCur->nPointAlloc = nNew;
188796  }
188797  i = pCur->nPoint++;
188798  pNew = pCur->aPoint + i;
188799  pNew->rScore = rScore;
188800  pNew->iLevel = iLevel;
188801  assert( iLevel<=RTREE_MAX_DEPTH );
188802  while( i>0 ){
188803    RtreeSearchPoint *pParent;
188804    j = (i-1)/2;
188805    pParent = pCur->aPoint + j;
188806    if( rtreeSearchPointCompare(pNew, pParent)>=0 ) break;
188807    rtreeSearchPointSwap(pCur, j, i);
188808    i = j;
188809    pNew = pParent;
188810  }
188811  return pNew;
188812}
188813
188814/*
188815** Allocate a new RtreeSearchPoint and return a pointer to it.  Return
188816** NULL if malloc fails.
188817*/
188818static RtreeSearchPoint *rtreeSearchPointNew(
188819  RtreeCursor *pCur,    /* The cursor */
188820  RtreeDValue rScore,   /* Score for the new search point */
188821  u8 iLevel             /* Level for the new search point */
188822){
188823  RtreeSearchPoint *pNew, *pFirst;
188824  pFirst = rtreeSearchPointFirst(pCur);
188825  pCur->anQueue[iLevel]++;
188826  if( pFirst==0
188827   || pFirst->rScore>rScore
188828   || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
188829  ){
188830    if( pCur->bPoint ){
188831      int ii;
188832      pNew = rtreeEnqueue(pCur, rScore, iLevel);
188833      if( pNew==0 ) return 0;
188834      ii = (int)(pNew - pCur->aPoint) + 1;
188835      if( ii<RTREE_CACHE_SZ ){
188836        assert( pCur->aNode[ii]==0 );
188837        pCur->aNode[ii] = pCur->aNode[0];
188838      }else{
188839        nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
188840      }
188841      pCur->aNode[0] = 0;
188842      *pNew = pCur->sPoint;
188843    }
188844    pCur->sPoint.rScore = rScore;
188845    pCur->sPoint.iLevel = iLevel;
188846    pCur->bPoint = 1;
188847    return &pCur->sPoint;
188848  }else{
188849    return rtreeEnqueue(pCur, rScore, iLevel);
188850  }
188851}
188852
188853#if 0
188854/* Tracing routines for the RtreeSearchPoint queue */
188855static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
188856  if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); }
188857  printf(" %d.%05lld.%02d %g %d",
188858    p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
188859  );
188860  idx++;
188861  if( idx<RTREE_CACHE_SZ ){
188862    printf(" %p\n", pCur->aNode[idx]);
188863  }else{
188864    printf("\n");
188865  }
188866}
188867static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
188868  int ii;
188869  printf("=== %9s ", zPrefix);
188870  if( pCur->bPoint ){
188871    tracePoint(&pCur->sPoint, -1, pCur);
188872  }
188873  for(ii=0; ii<pCur->nPoint; ii++){
188874    if( ii>0 || pCur->bPoint ) printf("              ");
188875    tracePoint(&pCur->aPoint[ii], ii, pCur);
188876  }
188877}
188878# define RTREE_QUEUE_TRACE(A,B) traceQueue(A,B)
188879#else
188880# define RTREE_QUEUE_TRACE(A,B)   /* no-op */
188881#endif
188882
188883/* Remove the search point with the lowest current score.
188884*/
188885static void rtreeSearchPointPop(RtreeCursor *p){
188886  int i, j, k, n;
188887  i = 1 - p->bPoint;
188888  assert( i==0 || i==1 );
188889  if( p->aNode[i] ){
188890    nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
188891    p->aNode[i] = 0;
188892  }
188893  if( p->bPoint ){
188894    p->anQueue[p->sPoint.iLevel]--;
188895    p->bPoint = 0;
188896  }else if( p->nPoint ){
188897    p->anQueue[p->aPoint[0].iLevel]--;
188898    n = --p->nPoint;
188899    p->aPoint[0] = p->aPoint[n];
188900    if( n<RTREE_CACHE_SZ-1 ){
188901      p->aNode[1] = p->aNode[n+1];
188902      p->aNode[n+1] = 0;
188903    }
188904    i = 0;
188905    while( (j = i*2+1)<n ){
188906      k = j+1;
188907      if( k<n && rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[j])<0 ){
188908        if( rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[i])<0 ){
188909          rtreeSearchPointSwap(p, i, k);
188910          i = k;
188911        }else{
188912          break;
188913        }
188914      }else{
188915        if( rtreeSearchPointCompare(&p->aPoint[j], &p->aPoint[i])<0 ){
188916          rtreeSearchPointSwap(p, i, j);
188917          i = j;
188918        }else{
188919          break;
188920        }
188921      }
188922    }
188923  }
188924}
188925
188926
188927/*
188928** Continue the search on cursor pCur until the front of the queue
188929** contains an entry suitable for returning as a result-set row,
188930** or until the RtreeSearchPoint queue is empty, indicating that the
188931** query has completed.
188932*/
188933static int rtreeStepToLeaf(RtreeCursor *pCur){
188934  RtreeSearchPoint *p;
188935  Rtree *pRtree = RTREE_OF_CURSOR(pCur);
188936  RtreeNode *pNode;
188937  int eWithin;
188938  int rc = SQLITE_OK;
188939  int nCell;
188940  int nConstraint = pCur->nConstraint;
188941  int ii;
188942  int eInt;
188943  RtreeSearchPoint x;
188944
188945  eInt = pRtree->eCoordType==RTREE_COORD_INT32;
188946  while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
188947    u8 *pCellData;
188948    pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
188949    if( rc ) return rc;
188950    nCell = NCELL(pNode);
188951    assert( nCell<200 );
188952    pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
188953    while( p->iCell<nCell ){
188954      sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
188955      eWithin = FULLY_WITHIN;
188956      for(ii=0; ii<nConstraint; ii++){
188957        RtreeConstraint *pConstraint = pCur->aConstraint + ii;
188958        if( pConstraint->op>=RTREE_MATCH ){
188959          rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, p,
188960                                       &rScore, &eWithin);
188961          if( rc ) return rc;
188962        }else if( p->iLevel==1 ){
188963          rtreeLeafConstraint(pConstraint, eInt, pCellData, &eWithin);
188964        }else{
188965          rtreeNonleafConstraint(pConstraint, eInt, pCellData, &eWithin);
188966        }
188967        if( eWithin==NOT_WITHIN ){
188968          p->iCell++;
188969          pCellData += pRtree->nBytesPerCell;
188970          break;
188971        }
188972      }
188973      if( eWithin==NOT_WITHIN ) continue;
188974      p->iCell++;
188975      x.iLevel = p->iLevel - 1;
188976      if( x.iLevel ){
188977        x.id = readInt64(pCellData);
188978        for(ii=0; ii<pCur->nPoint; ii++){
188979          if( pCur->aPoint[ii].id==x.id ){
188980            RTREE_IS_CORRUPT(pRtree);
188981            return SQLITE_CORRUPT_VTAB;
188982          }
188983        }
188984        x.iCell = 0;
188985      }else{
188986        x.id = p->id;
188987        x.iCell = p->iCell - 1;
188988      }
188989      if( p->iCell>=nCell ){
188990        RTREE_QUEUE_TRACE(pCur, "POP-S:");
188991        rtreeSearchPointPop(pCur);
188992      }
188993      if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
188994      p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
188995      if( p==0 ) return SQLITE_NOMEM;
188996      p->eWithin = (u8)eWithin;
188997      p->id = x.id;
188998      p->iCell = x.iCell;
188999      RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
189000      break;
189001    }
189002    if( p->iCell>=nCell ){
189003      RTREE_QUEUE_TRACE(pCur, "POP-Se:");
189004      rtreeSearchPointPop(pCur);
189005    }
189006  }
189007  pCur->atEOF = p==0;
189008  return SQLITE_OK;
189009}
189010
189011/*
189012** Rtree virtual table module xNext method.
189013*/
189014static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
189015  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
189016  int rc = SQLITE_OK;
189017
189018  /* Move to the next entry that matches the configured constraints. */
189019  RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
189020  if( pCsr->bAuxValid ){
189021    pCsr->bAuxValid = 0;
189022    sqlite3_reset(pCsr->pReadAux);
189023  }
189024  rtreeSearchPointPop(pCsr);
189025  rc = rtreeStepToLeaf(pCsr);
189026  return rc;
189027}
189028
189029/*
189030** Rtree virtual table module xRowid method.
189031*/
189032static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
189033  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
189034  RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
189035  int rc = SQLITE_OK;
189036  RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
189037  if( rc==SQLITE_OK && p ){
189038    *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr), pNode, p->iCell);
189039  }
189040  return rc;
189041}
189042
189043/*
189044** Rtree virtual table module xColumn method.
189045*/
189046static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
189047  Rtree *pRtree = (Rtree *)cur->pVtab;
189048  RtreeCursor *pCsr = (RtreeCursor *)cur;
189049  RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
189050  RtreeCoord c;
189051  int rc = SQLITE_OK;
189052  RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
189053
189054  if( rc ) return rc;
189055  if( p==0 ) return SQLITE_OK;
189056  if( i==0 ){
189057    sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
189058  }else if( i<=pRtree->nDim2 ){
189059    nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
189060#ifndef SQLITE_RTREE_INT_ONLY
189061    if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
189062      sqlite3_result_double(ctx, c.f);
189063    }else
189064#endif
189065    {
189066      assert( pRtree->eCoordType==RTREE_COORD_INT32 );
189067      sqlite3_result_int(ctx, c.i);
189068    }
189069  }else{
189070    if( !pCsr->bAuxValid ){
189071      if( pCsr->pReadAux==0 ){
189072        rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
189073                                &pCsr->pReadAux, 0);
189074        if( rc ) return rc;
189075      }
189076      sqlite3_bind_int64(pCsr->pReadAux, 1,
189077          nodeGetRowid(pRtree, pNode, p->iCell));
189078      rc = sqlite3_step(pCsr->pReadAux);
189079      if( rc==SQLITE_ROW ){
189080        pCsr->bAuxValid = 1;
189081      }else{
189082        sqlite3_reset(pCsr->pReadAux);
189083        if( rc==SQLITE_DONE ) rc = SQLITE_OK;
189084        return rc;
189085      }
189086    }
189087    sqlite3_result_value(ctx,
189088         sqlite3_column_value(pCsr->pReadAux, i - pRtree->nDim2 + 1));
189089  }
189090  return SQLITE_OK;
189091}
189092
189093/*
189094** Use nodeAcquire() to obtain the leaf node containing the record with
189095** rowid iRowid. If successful, set *ppLeaf to point to the node and
189096** return SQLITE_OK. If there is no such record in the table, set
189097** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
189098** to zero and return an SQLite error code.
189099*/
189100static int findLeafNode(
189101  Rtree *pRtree,              /* RTree to search */
189102  i64 iRowid,                 /* The rowid searching for */
189103  RtreeNode **ppLeaf,         /* Write the node here */
189104  sqlite3_int64 *piNode       /* Write the node-id here */
189105){
189106  int rc;
189107  *ppLeaf = 0;
189108  sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
189109  if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
189110    i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
189111    if( piNode ) *piNode = iNode;
189112    rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
189113    sqlite3_reset(pRtree->pReadRowid);
189114  }else{
189115    rc = sqlite3_reset(pRtree->pReadRowid);
189116  }
189117  return rc;
189118}
189119
189120/*
189121** This function is called to configure the RtreeConstraint object passed
189122** as the second argument for a MATCH constraint. The value passed as the
189123** first argument to this function is the right-hand operand to the MATCH
189124** operator.
189125*/
189126static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
189127  RtreeMatchArg *pBlob, *pSrc;       /* BLOB returned by geometry function */
189128  sqlite3_rtree_query_info *pInfo;   /* Callback information */
189129
189130  pSrc = sqlite3_value_pointer(pValue, "RtreeMatchArg");
189131  if( pSrc==0 ) return SQLITE_ERROR;
189132  pInfo = (sqlite3_rtree_query_info*)
189133                sqlite3_malloc64( sizeof(*pInfo)+pSrc->iSize );
189134  if( !pInfo ) return SQLITE_NOMEM;
189135  memset(pInfo, 0, sizeof(*pInfo));
189136  pBlob = (RtreeMatchArg*)&pInfo[1];
189137  memcpy(pBlob, pSrc, pSrc->iSize);
189138  pInfo->pContext = pBlob->cb.pContext;
189139  pInfo->nParam = pBlob->nParam;
189140  pInfo->aParam = pBlob->aParam;
189141  pInfo->apSqlParam = pBlob->apSqlParam;
189142
189143  if( pBlob->cb.xGeom ){
189144    pCons->u.xGeom = pBlob->cb.xGeom;
189145  }else{
189146    pCons->op = RTREE_QUERY;
189147    pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
189148  }
189149  pCons->pInfo = pInfo;
189150  return SQLITE_OK;
189151}
189152
189153/*
189154** Rtree virtual table module xFilter method.
189155*/
189156static int rtreeFilter(
189157  sqlite3_vtab_cursor *pVtabCursor,
189158  int idxNum, const char *idxStr,
189159  int argc, sqlite3_value **argv
189160){
189161  Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
189162  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
189163  RtreeNode *pRoot = 0;
189164  int ii;
189165  int rc = SQLITE_OK;
189166  int iCell = 0;
189167
189168  rtreeReference(pRtree);
189169
189170  /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
189171  resetCursor(pCsr);
189172
189173  pCsr->iStrategy = idxNum;
189174  if( idxNum==1 ){
189175    /* Special case - lookup by rowid. */
189176    RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
189177    RtreeSearchPoint *p;     /* Search point for the leaf */
189178    i64 iRowid = sqlite3_value_int64(argv[0]);
189179    i64 iNode = 0;
189180    int eType = sqlite3_value_numeric_type(argv[0]);
189181    if( eType==SQLITE_INTEGER
189182     || (eType==SQLITE_FLOAT && sqlite3_value_double(argv[0])==iRowid)
189183    ){
189184      rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
189185    }else{
189186      rc = SQLITE_OK;
189187      pLeaf = 0;
189188    }
189189    if( rc==SQLITE_OK && pLeaf!=0 ){
189190      p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
189191      assert( p!=0 );  /* Always returns pCsr->sPoint */
189192      pCsr->aNode[0] = pLeaf;
189193      p->id = iNode;
189194      p->eWithin = PARTLY_WITHIN;
189195      rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
189196      p->iCell = (u8)iCell;
189197      RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
189198    }else{
189199      pCsr->atEOF = 1;
189200    }
189201  }else{
189202    /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
189203    ** with the configured constraints.
189204    */
189205    rc = nodeAcquire(pRtree, 1, 0, &pRoot);
189206    if( rc==SQLITE_OK && argc>0 ){
189207      pCsr->aConstraint = sqlite3_malloc64(sizeof(RtreeConstraint)*argc);
189208      pCsr->nConstraint = argc;
189209      if( !pCsr->aConstraint ){
189210        rc = SQLITE_NOMEM;
189211      }else{
189212        memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
189213        memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
189214        assert( (idxStr==0 && argc==0)
189215                || (idxStr && (int)strlen(idxStr)==argc*2) );
189216        for(ii=0; ii<argc; ii++){
189217          RtreeConstraint *p = &pCsr->aConstraint[ii];
189218          int eType = sqlite3_value_numeric_type(argv[ii]);
189219          p->op = idxStr[ii*2];
189220          p->iCoord = idxStr[ii*2+1]-'0';
189221          if( p->op>=RTREE_MATCH ){
189222            /* A MATCH operator. The right-hand-side must be a blob that
189223            ** can be cast into an RtreeMatchArg object. One created using
189224            ** an sqlite3_rtree_geometry_callback() SQL user function.
189225            */
189226            rc = deserializeGeometry(argv[ii], p);
189227            if( rc!=SQLITE_OK ){
189228              break;
189229            }
189230            p->pInfo->nCoord = pRtree->nDim2;
189231            p->pInfo->anQueue = pCsr->anQueue;
189232            p->pInfo->mxLevel = pRtree->iDepth + 1;
189233          }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
189234#ifdef SQLITE_RTREE_INT_ONLY
189235            p->u.rValue = sqlite3_value_int64(argv[ii]);
189236#else
189237            p->u.rValue = sqlite3_value_double(argv[ii]);
189238#endif
189239          }else{
189240            p->u.rValue = RTREE_ZERO;
189241            if( eType==SQLITE_NULL ){
189242              p->op = RTREE_FALSE;
189243            }else if( p->op==RTREE_LT || p->op==RTREE_LE ){
189244              p->op = RTREE_TRUE;
189245            }else{
189246              p->op = RTREE_FALSE;
189247            }
189248          }
189249        }
189250      }
189251    }
189252    if( rc==SQLITE_OK ){
189253      RtreeSearchPoint *pNew;
189254      pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
189255      if( pNew==0 ) return SQLITE_NOMEM;
189256      pNew->id = 1;
189257      pNew->iCell = 0;
189258      pNew->eWithin = PARTLY_WITHIN;
189259      assert( pCsr->bPoint==1 );
189260      pCsr->aNode[0] = pRoot;
189261      pRoot = 0;
189262      RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
189263      rc = rtreeStepToLeaf(pCsr);
189264    }
189265  }
189266
189267  nodeRelease(pRtree, pRoot);
189268  rtreeRelease(pRtree);
189269  return rc;
189270}
189271
189272/*
189273** Rtree virtual table module xBestIndex method. There are three
189274** table scan strategies to choose from (in order from most to
189275** least desirable):
189276**
189277**   idxNum     idxStr        Strategy
189278**   ------------------------------------------------
189279**     1        Unused        Direct lookup by rowid.
189280**     2        See below     R-tree query or full-table scan.
189281**   ------------------------------------------------
189282**
189283** If strategy 1 is used, then idxStr is not meaningful. If strategy
189284** 2 is used, idxStr is formatted to contain 2 bytes for each
189285** constraint used. The first two bytes of idxStr correspond to
189286** the constraint in sqlite3_index_info.aConstraintUsage[] with
189287** (argvIndex==1) etc.
189288**
189289** The first of each pair of bytes in idxStr identifies the constraint
189290** operator as follows:
189291**
189292**   Operator    Byte Value
189293**   ----------------------
189294**      =        0x41 ('A')
189295**     <=        0x42 ('B')
189296**      <        0x43 ('C')
189297**     >=        0x44 ('D')
189298**      >        0x45 ('E')
189299**   MATCH       0x46 ('F')
189300**   ----------------------
189301**
189302** The second of each pair of bytes identifies the coordinate column
189303** to which the constraint applies. The leftmost coordinate column
189304** is 'a', the second from the left 'b' etc.
189305*/
189306static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
189307  Rtree *pRtree = (Rtree*)tab;
189308  int rc = SQLITE_OK;
189309  int ii;
189310  int bMatch = 0;                 /* True if there exists a MATCH constraint */
189311  i64 nRow;                       /* Estimated rows returned by this scan */
189312
189313  int iIdx = 0;
189314  char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
189315  memset(zIdxStr, 0, sizeof(zIdxStr));
189316
189317  /* Check if there exists a MATCH constraint - even an unusable one. If there
189318  ** is, do not consider the lookup-by-rowid plan as using such a plan would
189319  ** require the VDBE to evaluate the MATCH constraint, which is not currently
189320  ** possible. */
189321  for(ii=0; ii<pIdxInfo->nConstraint; ii++){
189322    if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH ){
189323      bMatch = 1;
189324    }
189325  }
189326
189327  assert( pIdxInfo->idxStr==0 );
189328  for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
189329    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
189330
189331    if( bMatch==0 && p->usable
189332     && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ
189333    ){
189334      /* We have an equality constraint on the rowid. Use strategy 1. */
189335      int jj;
189336      for(jj=0; jj<ii; jj++){
189337        pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
189338        pIdxInfo->aConstraintUsage[jj].omit = 0;
189339      }
189340      pIdxInfo->idxNum = 1;
189341      pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
189342      pIdxInfo->aConstraintUsage[jj].omit = 1;
189343
189344      /* This strategy involves a two rowid lookups on an B-Tree structures
189345      ** and then a linear search of an R-Tree node. This should be
189346      ** considered almost as quick as a direct rowid lookup (for which
189347      ** sqlite uses an internal cost of 0.0). It is expected to return
189348      ** a single row.
189349      */
189350      pIdxInfo->estimatedCost = 30.0;
189351      pIdxInfo->estimatedRows = 1;
189352      pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
189353      return SQLITE_OK;
189354    }
189355
189356    if( p->usable
189357    && ((p->iColumn>0 && p->iColumn<=pRtree->nDim2)
189358        || p->op==SQLITE_INDEX_CONSTRAINT_MATCH)
189359    ){
189360      u8 op;
189361      switch( p->op ){
189362        case SQLITE_INDEX_CONSTRAINT_EQ:    op = RTREE_EQ;    break;
189363        case SQLITE_INDEX_CONSTRAINT_GT:    op = RTREE_GT;    break;
189364        case SQLITE_INDEX_CONSTRAINT_LE:    op = RTREE_LE;    break;
189365        case SQLITE_INDEX_CONSTRAINT_LT:    op = RTREE_LT;    break;
189366        case SQLITE_INDEX_CONSTRAINT_GE:    op = RTREE_GE;    break;
189367        case SQLITE_INDEX_CONSTRAINT_MATCH: op = RTREE_MATCH; break;
189368        default:                            op = 0;           break;
189369      }
189370      if( op ){
189371        zIdxStr[iIdx++] = op;
189372        zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
189373        pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
189374        pIdxInfo->aConstraintUsage[ii].omit = 1;
189375      }
189376    }
189377  }
189378
189379  pIdxInfo->idxNum = 2;
189380  pIdxInfo->needToFreeIdxStr = 1;
189381  if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
189382    return SQLITE_NOMEM;
189383  }
189384
189385  nRow = pRtree->nRowEst >> (iIdx/2);
189386  pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
189387  pIdxInfo->estimatedRows = nRow;
189388
189389  return rc;
189390}
189391
189392/*
189393** Return the N-dimensional volumn of the cell stored in *p.
189394*/
189395static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
189396  RtreeDValue area = (RtreeDValue)1;
189397  assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
189398#ifndef SQLITE_RTREE_INT_ONLY
189399  if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
189400    switch( pRtree->nDim ){
189401      case 5:  area  = p->aCoord[9].f - p->aCoord[8].f;
189402      case 4:  area *= p->aCoord[7].f - p->aCoord[6].f;
189403      case 3:  area *= p->aCoord[5].f - p->aCoord[4].f;
189404      case 2:  area *= p->aCoord[3].f - p->aCoord[2].f;
189405      default: area *= p->aCoord[1].f - p->aCoord[0].f;
189406    }
189407  }else
189408#endif
189409  {
189410    switch( pRtree->nDim ){
189411      case 5:  area  = (i64)p->aCoord[9].i - (i64)p->aCoord[8].i;
189412      case 4:  area *= (i64)p->aCoord[7].i - (i64)p->aCoord[6].i;
189413      case 3:  area *= (i64)p->aCoord[5].i - (i64)p->aCoord[4].i;
189414      case 2:  area *= (i64)p->aCoord[3].i - (i64)p->aCoord[2].i;
189415      default: area *= (i64)p->aCoord[1].i - (i64)p->aCoord[0].i;
189416    }
189417  }
189418  return area;
189419}
189420
189421/*
189422** Return the margin length of cell p. The margin length is the sum
189423** of the objects size in each dimension.
189424*/
189425static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
189426  RtreeDValue margin = 0;
189427  int ii = pRtree->nDim2 - 2;
189428  do{
189429    margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
189430    ii -= 2;
189431  }while( ii>=0 );
189432  return margin;
189433}
189434
189435/*
189436** Store the union of cells p1 and p2 in p1.
189437*/
189438static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
189439  int ii = 0;
189440  if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
189441    do{
189442      p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
189443      p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
189444      ii += 2;
189445    }while( ii<pRtree->nDim2 );
189446  }else{
189447    do{
189448      p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
189449      p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
189450      ii += 2;
189451    }while( ii<pRtree->nDim2 );
189452  }
189453}
189454
189455/*
189456** Return true if the area covered by p2 is a subset of the area covered
189457** by p1. False otherwise.
189458*/
189459static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
189460  int ii;
189461  int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
189462  for(ii=0; ii<pRtree->nDim2; ii+=2){
189463    RtreeCoord *a1 = &p1->aCoord[ii];
189464    RtreeCoord *a2 = &p2->aCoord[ii];
189465    if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
189466     || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
189467    ){
189468      return 0;
189469    }
189470  }
189471  return 1;
189472}
189473
189474/*
189475** Return the amount cell p would grow by if it were unioned with pCell.
189476*/
189477static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
189478  RtreeDValue area;
189479  RtreeCell cell;
189480  memcpy(&cell, p, sizeof(RtreeCell));
189481  area = cellArea(pRtree, &cell);
189482  cellUnion(pRtree, &cell, pCell);
189483  return (cellArea(pRtree, &cell)-area);
189484}
189485
189486static RtreeDValue cellOverlap(
189487  Rtree *pRtree,
189488  RtreeCell *p,
189489  RtreeCell *aCell,
189490  int nCell
189491){
189492  int ii;
189493  RtreeDValue overlap = RTREE_ZERO;
189494  for(ii=0; ii<nCell; ii++){
189495    int jj;
189496    RtreeDValue o = (RtreeDValue)1;
189497    for(jj=0; jj<pRtree->nDim2; jj+=2){
189498      RtreeDValue x1, x2;
189499      x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
189500      x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
189501      if( x2<x1 ){
189502        o = (RtreeDValue)0;
189503        break;
189504      }else{
189505        o = o * (x2-x1);
189506      }
189507    }
189508    overlap += o;
189509  }
189510  return overlap;
189511}
189512
189513
189514/*
189515** This function implements the ChooseLeaf algorithm from Gutman[84].
189516** ChooseSubTree in r*tree terminology.
189517*/
189518static int ChooseLeaf(
189519  Rtree *pRtree,               /* Rtree table */
189520  RtreeCell *pCell,            /* Cell to insert into rtree */
189521  int iHeight,                 /* Height of sub-tree rooted at pCell */
189522  RtreeNode **ppLeaf           /* OUT: Selected leaf page */
189523){
189524  int rc;
189525  int ii;
189526  RtreeNode *pNode = 0;
189527  rc = nodeAcquire(pRtree, 1, 0, &pNode);
189528
189529  for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
189530    int iCell;
189531    sqlite3_int64 iBest = 0;
189532
189533    RtreeDValue fMinGrowth = RTREE_ZERO;
189534    RtreeDValue fMinArea = RTREE_ZERO;
189535
189536    int nCell = NCELL(pNode);
189537    RtreeCell cell;
189538    RtreeNode *pChild;
189539
189540    RtreeCell *aCell = 0;
189541
189542    /* Select the child node which will be enlarged the least if pCell
189543    ** is inserted into it. Resolve ties by choosing the entry with
189544    ** the smallest area.
189545    */
189546    for(iCell=0; iCell<nCell; iCell++){
189547      int bBest = 0;
189548      RtreeDValue growth;
189549      RtreeDValue area;
189550      nodeGetCell(pRtree, pNode, iCell, &cell);
189551      growth = cellGrowth(pRtree, &cell, pCell);
189552      area = cellArea(pRtree, &cell);
189553      if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
189554        bBest = 1;
189555      }
189556      if( bBest ){
189557        fMinGrowth = growth;
189558        fMinArea = area;
189559        iBest = cell.iRowid;
189560      }
189561    }
189562
189563    sqlite3_free(aCell);
189564    rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
189565    nodeRelease(pRtree, pNode);
189566    pNode = pChild;
189567  }
189568
189569  *ppLeaf = pNode;
189570  return rc;
189571}
189572
189573/*
189574** A cell with the same content as pCell has just been inserted into
189575** the node pNode. This function updates the bounding box cells in
189576** all ancestor elements.
189577*/
189578static int AdjustTree(
189579  Rtree *pRtree,                    /* Rtree table */
189580  RtreeNode *pNode,                 /* Adjust ancestry of this node. */
189581  RtreeCell *pCell                  /* This cell was just inserted */
189582){
189583  RtreeNode *p = pNode;
189584  int cnt = 0;
189585  while( p->pParent ){
189586    RtreeNode *pParent = p->pParent;
189587    RtreeCell cell;
189588    int iCell;
189589
189590    if( (++cnt)>1000 || nodeParentIndex(pRtree, p, &iCell)  ){
189591      RTREE_IS_CORRUPT(pRtree);
189592      return SQLITE_CORRUPT_VTAB;
189593    }
189594
189595    nodeGetCell(pRtree, pParent, iCell, &cell);
189596    if( !cellContains(pRtree, &cell, pCell) ){
189597      cellUnion(pRtree, &cell, pCell);
189598      nodeOverwriteCell(pRtree, pParent, &cell, iCell);
189599    }
189600
189601    p = pParent;
189602  }
189603  return SQLITE_OK;
189604}
189605
189606/*
189607** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
189608*/
189609static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
189610  sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
189611  sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
189612  sqlite3_step(pRtree->pWriteRowid);
189613  return sqlite3_reset(pRtree->pWriteRowid);
189614}
189615
189616/*
189617** Write mapping (iNode->iPar) to the <rtree>_parent table.
189618*/
189619static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
189620  sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
189621  sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
189622  sqlite3_step(pRtree->pWriteParent);
189623  return sqlite3_reset(pRtree->pWriteParent);
189624}
189625
189626static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
189627
189628
189629/*
189630** Arguments aIdx, aDistance and aSpare all point to arrays of size
189631** nIdx. The aIdx array contains the set of integers from 0 to
189632** (nIdx-1) in no particular order. This function sorts the values
189633** in aIdx according to the indexed values in aDistance. For
189634** example, assuming the inputs:
189635**
189636**   aIdx      = { 0,   1,   2,   3 }
189637**   aDistance = { 5.0, 2.0, 7.0, 6.0 }
189638**
189639** this function sets the aIdx array to contain:
189640**
189641**   aIdx      = { 0,   1,   2,   3 }
189642**
189643** The aSpare array is used as temporary working space by the
189644** sorting algorithm.
189645*/
189646static void SortByDistance(
189647  int *aIdx,
189648  int nIdx,
189649  RtreeDValue *aDistance,
189650  int *aSpare
189651){
189652  if( nIdx>1 ){
189653    int iLeft = 0;
189654    int iRight = 0;
189655
189656    int nLeft = nIdx/2;
189657    int nRight = nIdx-nLeft;
189658    int *aLeft = aIdx;
189659    int *aRight = &aIdx[nLeft];
189660
189661    SortByDistance(aLeft, nLeft, aDistance, aSpare);
189662    SortByDistance(aRight, nRight, aDistance, aSpare);
189663
189664    memcpy(aSpare, aLeft, sizeof(int)*nLeft);
189665    aLeft = aSpare;
189666
189667    while( iLeft<nLeft || iRight<nRight ){
189668      if( iLeft==nLeft ){
189669        aIdx[iLeft+iRight] = aRight[iRight];
189670        iRight++;
189671      }else if( iRight==nRight ){
189672        aIdx[iLeft+iRight] = aLeft[iLeft];
189673        iLeft++;
189674      }else{
189675        RtreeDValue fLeft = aDistance[aLeft[iLeft]];
189676        RtreeDValue fRight = aDistance[aRight[iRight]];
189677        if( fLeft<fRight ){
189678          aIdx[iLeft+iRight] = aLeft[iLeft];
189679          iLeft++;
189680        }else{
189681          aIdx[iLeft+iRight] = aRight[iRight];
189682          iRight++;
189683        }
189684      }
189685    }
189686
189687#if 0
189688    /* Check that the sort worked */
189689    {
189690      int jj;
189691      for(jj=1; jj<nIdx; jj++){
189692        RtreeDValue left = aDistance[aIdx[jj-1]];
189693        RtreeDValue right = aDistance[aIdx[jj]];
189694        assert( left<=right );
189695      }
189696    }
189697#endif
189698  }
189699}
189700
189701/*
189702** Arguments aIdx, aCell and aSpare all point to arrays of size
189703** nIdx. The aIdx array contains the set of integers from 0 to
189704** (nIdx-1) in no particular order. This function sorts the values
189705** in aIdx according to dimension iDim of the cells in aCell. The
189706** minimum value of dimension iDim is considered first, the
189707** maximum used to break ties.
189708**
189709** The aSpare array is used as temporary working space by the
189710** sorting algorithm.
189711*/
189712static void SortByDimension(
189713  Rtree *pRtree,
189714  int *aIdx,
189715  int nIdx,
189716  int iDim,
189717  RtreeCell *aCell,
189718  int *aSpare
189719){
189720  if( nIdx>1 ){
189721
189722    int iLeft = 0;
189723    int iRight = 0;
189724
189725    int nLeft = nIdx/2;
189726    int nRight = nIdx-nLeft;
189727    int *aLeft = aIdx;
189728    int *aRight = &aIdx[nLeft];
189729
189730    SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
189731    SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
189732
189733    memcpy(aSpare, aLeft, sizeof(int)*nLeft);
189734    aLeft = aSpare;
189735    while( iLeft<nLeft || iRight<nRight ){
189736      RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
189737      RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
189738      RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
189739      RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
189740      if( (iLeft!=nLeft) && ((iRight==nRight)
189741       || (xleft1<xright1)
189742       || (xleft1==xright1 && xleft2<xright2)
189743      )){
189744        aIdx[iLeft+iRight] = aLeft[iLeft];
189745        iLeft++;
189746      }else{
189747        aIdx[iLeft+iRight] = aRight[iRight];
189748        iRight++;
189749      }
189750    }
189751
189752#if 0
189753    /* Check that the sort worked */
189754    {
189755      int jj;
189756      for(jj=1; jj<nIdx; jj++){
189757        RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
189758        RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
189759        RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
189760        RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
189761        assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
189762      }
189763    }
189764#endif
189765  }
189766}
189767
189768/*
189769** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
189770*/
189771static int splitNodeStartree(
189772  Rtree *pRtree,
189773  RtreeCell *aCell,
189774  int nCell,
189775  RtreeNode *pLeft,
189776  RtreeNode *pRight,
189777  RtreeCell *pBboxLeft,
189778  RtreeCell *pBboxRight
189779){
189780  int **aaSorted;
189781  int *aSpare;
189782  int ii;
189783
189784  int iBestDim = 0;
189785  int iBestSplit = 0;
189786  RtreeDValue fBestMargin = RTREE_ZERO;
189787
189788  sqlite3_int64 nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
189789
189790  aaSorted = (int **)sqlite3_malloc64(nByte);
189791  if( !aaSorted ){
189792    return SQLITE_NOMEM;
189793  }
189794
189795  aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
189796  memset(aaSorted, 0, nByte);
189797  for(ii=0; ii<pRtree->nDim; ii++){
189798    int jj;
189799    aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
189800    for(jj=0; jj<nCell; jj++){
189801      aaSorted[ii][jj] = jj;
189802    }
189803    SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
189804  }
189805
189806  for(ii=0; ii<pRtree->nDim; ii++){
189807    RtreeDValue margin = RTREE_ZERO;
189808    RtreeDValue fBestOverlap = RTREE_ZERO;
189809    RtreeDValue fBestArea = RTREE_ZERO;
189810    int iBestLeft = 0;
189811    int nLeft;
189812
189813    for(
189814      nLeft=RTREE_MINCELLS(pRtree);
189815      nLeft<=(nCell-RTREE_MINCELLS(pRtree));
189816      nLeft++
189817    ){
189818      RtreeCell left;
189819      RtreeCell right;
189820      int kk;
189821      RtreeDValue overlap;
189822      RtreeDValue area;
189823
189824      memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
189825      memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
189826      for(kk=1; kk<(nCell-1); kk++){
189827        if( kk<nLeft ){
189828          cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
189829        }else{
189830          cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
189831        }
189832      }
189833      margin += cellMargin(pRtree, &left);
189834      margin += cellMargin(pRtree, &right);
189835      overlap = cellOverlap(pRtree, &left, &right, 1);
189836      area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
189837      if( (nLeft==RTREE_MINCELLS(pRtree))
189838       || (overlap<fBestOverlap)
189839       || (overlap==fBestOverlap && area<fBestArea)
189840      ){
189841        iBestLeft = nLeft;
189842        fBestOverlap = overlap;
189843        fBestArea = area;
189844      }
189845    }
189846
189847    if( ii==0 || margin<fBestMargin ){
189848      iBestDim = ii;
189849      fBestMargin = margin;
189850      iBestSplit = iBestLeft;
189851    }
189852  }
189853
189854  memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
189855  memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
189856  for(ii=0; ii<nCell; ii++){
189857    RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
189858    RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
189859    RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
189860    nodeInsertCell(pRtree, pTarget, pCell);
189861    cellUnion(pRtree, pBbox, pCell);
189862  }
189863
189864  sqlite3_free(aaSorted);
189865  return SQLITE_OK;
189866}
189867
189868
189869static int updateMapping(
189870  Rtree *pRtree,
189871  i64 iRowid,
189872  RtreeNode *pNode,
189873  int iHeight
189874){
189875  int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
189876  xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
189877  if( iHeight>0 ){
189878    RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
189879    if( pChild ){
189880      nodeRelease(pRtree, pChild->pParent);
189881      nodeReference(pNode);
189882      pChild->pParent = pNode;
189883    }
189884  }
189885  return xSetMapping(pRtree, iRowid, pNode->iNode);
189886}
189887
189888static int SplitNode(
189889  Rtree *pRtree,
189890  RtreeNode *pNode,
189891  RtreeCell *pCell,
189892  int iHeight
189893){
189894  int i;
189895  int newCellIsRight = 0;
189896
189897  int rc = SQLITE_OK;
189898  int nCell = NCELL(pNode);
189899  RtreeCell *aCell;
189900  int *aiUsed;
189901
189902  RtreeNode *pLeft = 0;
189903  RtreeNode *pRight = 0;
189904
189905  RtreeCell leftbbox;
189906  RtreeCell rightbbox;
189907
189908  /* Allocate an array and populate it with a copy of pCell and
189909  ** all cells from node pLeft. Then zero the original node.
189910  */
189911  aCell = sqlite3_malloc64((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
189912  if( !aCell ){
189913    rc = SQLITE_NOMEM;
189914    goto splitnode_out;
189915  }
189916  aiUsed = (int *)&aCell[nCell+1];
189917  memset(aiUsed, 0, sizeof(int)*(nCell+1));
189918  for(i=0; i<nCell; i++){
189919    nodeGetCell(pRtree, pNode, i, &aCell[i]);
189920  }
189921  nodeZero(pRtree, pNode);
189922  memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
189923  nCell++;
189924
189925  if( pNode->iNode==1 ){
189926    pRight = nodeNew(pRtree, pNode);
189927    pLeft = nodeNew(pRtree, pNode);
189928    pRtree->iDepth++;
189929    pNode->isDirty = 1;
189930    writeInt16(pNode->zData, pRtree->iDepth);
189931  }else{
189932    pLeft = pNode;
189933    pRight = nodeNew(pRtree, pLeft->pParent);
189934    pLeft->nRef++;
189935  }
189936
189937  if( !pLeft || !pRight ){
189938    rc = SQLITE_NOMEM;
189939    goto splitnode_out;
189940  }
189941
189942  memset(pLeft->zData, 0, pRtree->iNodeSize);
189943  memset(pRight->zData, 0, pRtree->iNodeSize);
189944
189945  rc = splitNodeStartree(pRtree, aCell, nCell, pLeft, pRight,
189946                         &leftbbox, &rightbbox);
189947  if( rc!=SQLITE_OK ){
189948    goto splitnode_out;
189949  }
189950
189951  /* Ensure both child nodes have node numbers assigned to them by calling
189952  ** nodeWrite(). Node pRight always needs a node number, as it was created
189953  ** by nodeNew() above. But node pLeft sometimes already has a node number.
189954  ** In this case avoid the all to nodeWrite().
189955  */
189956  if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))
189957   || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
189958  ){
189959    goto splitnode_out;
189960  }
189961
189962  rightbbox.iRowid = pRight->iNode;
189963  leftbbox.iRowid = pLeft->iNode;
189964
189965  if( pNode->iNode==1 ){
189966    rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
189967    if( rc!=SQLITE_OK ){
189968      goto splitnode_out;
189969    }
189970  }else{
189971    RtreeNode *pParent = pLeft->pParent;
189972    int iCell;
189973    rc = nodeParentIndex(pRtree, pLeft, &iCell);
189974    if( rc==SQLITE_OK ){
189975      nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
189976      rc = AdjustTree(pRtree, pParent, &leftbbox);
189977    }
189978    if( rc!=SQLITE_OK ){
189979      goto splitnode_out;
189980    }
189981  }
189982  if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
189983    goto splitnode_out;
189984  }
189985
189986  for(i=0; i<NCELL(pRight); i++){
189987    i64 iRowid = nodeGetRowid(pRtree, pRight, i);
189988    rc = updateMapping(pRtree, iRowid, pRight, iHeight);
189989    if( iRowid==pCell->iRowid ){
189990      newCellIsRight = 1;
189991    }
189992    if( rc!=SQLITE_OK ){
189993      goto splitnode_out;
189994    }
189995  }
189996  if( pNode->iNode==1 ){
189997    for(i=0; i<NCELL(pLeft); i++){
189998      i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
189999      rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
190000      if( rc!=SQLITE_OK ){
190001        goto splitnode_out;
190002      }
190003    }
190004  }else if( newCellIsRight==0 ){
190005    rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
190006  }
190007
190008  if( rc==SQLITE_OK ){
190009    rc = nodeRelease(pRtree, pRight);
190010    pRight = 0;
190011  }
190012  if( rc==SQLITE_OK ){
190013    rc = nodeRelease(pRtree, pLeft);
190014    pLeft = 0;
190015  }
190016
190017splitnode_out:
190018  nodeRelease(pRtree, pRight);
190019  nodeRelease(pRtree, pLeft);
190020  sqlite3_free(aCell);
190021  return rc;
190022}
190023
190024/*
190025** If node pLeaf is not the root of the r-tree and its pParent pointer is
190026** still NULL, load all ancestor nodes of pLeaf into memory and populate
190027** the pLeaf->pParent chain all the way up to the root node.
190028**
190029** This operation is required when a row is deleted (or updated - an update
190030** is implemented as a delete followed by an insert). SQLite provides the
190031** rowid of the row to delete, which can be used to find the leaf on which
190032** the entry resides (argument pLeaf). Once the leaf is located, this
190033** function is called to determine its ancestry.
190034*/
190035static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
190036  int rc = SQLITE_OK;
190037  RtreeNode *pChild = pLeaf;
190038  while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
190039    int rc2 = SQLITE_OK;          /* sqlite3_reset() return code */
190040    sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
190041    rc = sqlite3_step(pRtree->pReadParent);
190042    if( rc==SQLITE_ROW ){
190043      RtreeNode *pTest;           /* Used to test for reference loops */
190044      i64 iNode;                  /* Node number of parent node */
190045
190046      /* Before setting pChild->pParent, test that we are not creating a
190047      ** loop of references (as we would if, say, pChild==pParent). We don't
190048      ** want to do this as it leads to a memory leak when trying to delete
190049      ** the referenced counted node structures.
190050      */
190051      iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
190052      for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
190053      if( !pTest ){
190054        rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
190055      }
190056    }
190057    rc = sqlite3_reset(pRtree->pReadParent);
190058    if( rc==SQLITE_OK ) rc = rc2;
190059    if( rc==SQLITE_OK && !pChild->pParent ){
190060      RTREE_IS_CORRUPT(pRtree);
190061      rc = SQLITE_CORRUPT_VTAB;
190062    }
190063    pChild = pChild->pParent;
190064  }
190065  return rc;
190066}
190067
190068static int deleteCell(Rtree *, RtreeNode *, int, int);
190069
190070static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
190071  int rc;
190072  int rc2;
190073  RtreeNode *pParent = 0;
190074  int iCell;
190075
190076  assert( pNode->nRef==1 );
190077
190078  /* Remove the entry in the parent cell. */
190079  rc = nodeParentIndex(pRtree, pNode, &iCell);
190080  if( rc==SQLITE_OK ){
190081    pParent = pNode->pParent;
190082    pNode->pParent = 0;
190083    rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
190084  }
190085  rc2 = nodeRelease(pRtree, pParent);
190086  if( rc==SQLITE_OK ){
190087    rc = rc2;
190088  }
190089  if( rc!=SQLITE_OK ){
190090    return rc;
190091  }
190092
190093  /* Remove the xxx_node entry. */
190094  sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
190095  sqlite3_step(pRtree->pDeleteNode);
190096  if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
190097    return rc;
190098  }
190099
190100  /* Remove the xxx_parent entry. */
190101  sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
190102  sqlite3_step(pRtree->pDeleteParent);
190103  if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
190104    return rc;
190105  }
190106
190107  /* Remove the node from the in-memory hash table and link it into
190108  ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
190109  */
190110  nodeHashDelete(pRtree, pNode);
190111  pNode->iNode = iHeight;
190112  pNode->pNext = pRtree->pDeleted;
190113  pNode->nRef++;
190114  pRtree->pDeleted = pNode;
190115
190116  return SQLITE_OK;
190117}
190118
190119static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
190120  RtreeNode *pParent = pNode->pParent;
190121  int rc = SQLITE_OK;
190122  if( pParent ){
190123    int ii;
190124    int nCell = NCELL(pNode);
190125    RtreeCell box;                            /* Bounding box for pNode */
190126    nodeGetCell(pRtree, pNode, 0, &box);
190127    for(ii=1; ii<nCell; ii++){
190128      RtreeCell cell;
190129      nodeGetCell(pRtree, pNode, ii, &cell);
190130      cellUnion(pRtree, &box, &cell);
190131    }
190132    box.iRowid = pNode->iNode;
190133    rc = nodeParentIndex(pRtree, pNode, &ii);
190134    if( rc==SQLITE_OK ){
190135      nodeOverwriteCell(pRtree, pParent, &box, ii);
190136      rc = fixBoundingBox(pRtree, pParent);
190137    }
190138  }
190139  return rc;
190140}
190141
190142/*
190143** Delete the cell at index iCell of node pNode. After removing the
190144** cell, adjust the r-tree data structure if required.
190145*/
190146static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
190147  RtreeNode *pParent;
190148  int rc;
190149
190150  if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
190151    return rc;
190152  }
190153
190154  /* Remove the cell from the node. This call just moves bytes around
190155  ** the in-memory node image, so it cannot fail.
190156  */
190157  nodeDeleteCell(pRtree, pNode, iCell);
190158
190159  /* If the node is not the tree root and now has less than the minimum
190160  ** number of cells, remove it from the tree. Otherwise, update the
190161  ** cell in the parent node so that it tightly contains the updated
190162  ** node.
190163  */
190164  pParent = pNode->pParent;
190165  assert( pParent || pNode->iNode==1 );
190166  if( pParent ){
190167    if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
190168      rc = removeNode(pRtree, pNode, iHeight);
190169    }else{
190170      rc = fixBoundingBox(pRtree, pNode);
190171    }
190172  }
190173
190174  return rc;
190175}
190176
190177static int Reinsert(
190178  Rtree *pRtree,
190179  RtreeNode *pNode,
190180  RtreeCell *pCell,
190181  int iHeight
190182){
190183  int *aOrder;
190184  int *aSpare;
190185  RtreeCell *aCell;
190186  RtreeDValue *aDistance;
190187  int nCell;
190188  RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
190189  int iDim;
190190  int ii;
190191  int rc = SQLITE_OK;
190192  int n;
190193
190194  memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
190195
190196  nCell = NCELL(pNode)+1;
190197  n = (nCell+1)&(~1);
190198
190199  /* Allocate the buffers used by this operation. The allocation is
190200  ** relinquished before this function returns.
190201  */
190202  aCell = (RtreeCell *)sqlite3_malloc64(n * (
190203    sizeof(RtreeCell)     +         /* aCell array */
190204    sizeof(int)           +         /* aOrder array */
190205    sizeof(int)           +         /* aSpare array */
190206    sizeof(RtreeDValue)             /* aDistance array */
190207  ));
190208  if( !aCell ){
190209    return SQLITE_NOMEM;
190210  }
190211  aOrder    = (int *)&aCell[n];
190212  aSpare    = (int *)&aOrder[n];
190213  aDistance = (RtreeDValue *)&aSpare[n];
190214
190215  for(ii=0; ii<nCell; ii++){
190216    if( ii==(nCell-1) ){
190217      memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
190218    }else{
190219      nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
190220    }
190221    aOrder[ii] = ii;
190222    for(iDim=0; iDim<pRtree->nDim; iDim++){
190223      aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
190224      aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
190225    }
190226  }
190227  for(iDim=0; iDim<pRtree->nDim; iDim++){
190228    aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
190229  }
190230
190231  for(ii=0; ii<nCell; ii++){
190232    aDistance[ii] = RTREE_ZERO;
190233    for(iDim=0; iDim<pRtree->nDim; iDim++){
190234      RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
190235                               DCOORD(aCell[ii].aCoord[iDim*2]));
190236      aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
190237    }
190238  }
190239
190240  SortByDistance(aOrder, nCell, aDistance, aSpare);
190241  nodeZero(pRtree, pNode);
190242
190243  for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
190244    RtreeCell *p = &aCell[aOrder[ii]];
190245    nodeInsertCell(pRtree, pNode, p);
190246    if( p->iRowid==pCell->iRowid ){
190247      if( iHeight==0 ){
190248        rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
190249      }else{
190250        rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
190251      }
190252    }
190253  }
190254  if( rc==SQLITE_OK ){
190255    rc = fixBoundingBox(pRtree, pNode);
190256  }
190257  for(; rc==SQLITE_OK && ii<nCell; ii++){
190258    /* Find a node to store this cell in. pNode->iNode currently contains
190259    ** the height of the sub-tree headed by the cell.
190260    */
190261    RtreeNode *pInsert;
190262    RtreeCell *p = &aCell[aOrder[ii]];
190263    rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
190264    if( rc==SQLITE_OK ){
190265      int rc2;
190266      rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
190267      rc2 = nodeRelease(pRtree, pInsert);
190268      if( rc==SQLITE_OK ){
190269        rc = rc2;
190270      }
190271    }
190272  }
190273
190274  sqlite3_free(aCell);
190275  return rc;
190276}
190277
190278/*
190279** Insert cell pCell into node pNode. Node pNode is the head of a
190280** subtree iHeight high (leaf nodes have iHeight==0).
190281*/
190282static int rtreeInsertCell(
190283  Rtree *pRtree,
190284  RtreeNode *pNode,
190285  RtreeCell *pCell,
190286  int iHeight
190287){
190288  int rc = SQLITE_OK;
190289  if( iHeight>0 ){
190290    RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
190291    if( pChild ){
190292      nodeRelease(pRtree, pChild->pParent);
190293      nodeReference(pNode);
190294      pChild->pParent = pNode;
190295    }
190296  }
190297  if( nodeInsertCell(pRtree, pNode, pCell) ){
190298    if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
190299      rc = SplitNode(pRtree, pNode, pCell, iHeight);
190300    }else{
190301      pRtree->iReinsertHeight = iHeight;
190302      rc = Reinsert(pRtree, pNode, pCell, iHeight);
190303    }
190304  }else{
190305    rc = AdjustTree(pRtree, pNode, pCell);
190306    if( rc==SQLITE_OK ){
190307      if( iHeight==0 ){
190308        rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
190309      }else{
190310        rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
190311      }
190312    }
190313  }
190314  return rc;
190315}
190316
190317static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
190318  int ii;
190319  int rc = SQLITE_OK;
190320  int nCell = NCELL(pNode);
190321
190322  for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
190323    RtreeNode *pInsert;
190324    RtreeCell cell;
190325    nodeGetCell(pRtree, pNode, ii, &cell);
190326
190327    /* Find a node to store this cell in. pNode->iNode currently contains
190328    ** the height of the sub-tree headed by the cell.
190329    */
190330    rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
190331    if( rc==SQLITE_OK ){
190332      int rc2;
190333      rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
190334      rc2 = nodeRelease(pRtree, pInsert);
190335      if( rc==SQLITE_OK ){
190336        rc = rc2;
190337      }
190338    }
190339  }
190340  return rc;
190341}
190342
190343/*
190344** Select a currently unused rowid for a new r-tree record.
190345*/
190346static int rtreeNewRowid(Rtree *pRtree, i64 *piRowid){
190347  int rc;
190348  sqlite3_bind_null(pRtree->pWriteRowid, 1);
190349  sqlite3_bind_null(pRtree->pWriteRowid, 2);
190350  sqlite3_step(pRtree->pWriteRowid);
190351  rc = sqlite3_reset(pRtree->pWriteRowid);
190352  *piRowid = sqlite3_last_insert_rowid(pRtree->db);
190353  return rc;
190354}
190355
190356/*
190357** Remove the entry with rowid=iDelete from the r-tree structure.
190358*/
190359static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
190360  int rc;                         /* Return code */
190361  RtreeNode *pLeaf = 0;           /* Leaf node containing record iDelete */
190362  int iCell;                      /* Index of iDelete cell in pLeaf */
190363  RtreeNode *pRoot = 0;           /* Root node of rtree structure */
190364
190365
190366  /* Obtain a reference to the root node to initialize Rtree.iDepth */
190367  rc = nodeAcquire(pRtree, 1, 0, &pRoot);
190368
190369  /* Obtain a reference to the leaf node that contains the entry
190370  ** about to be deleted.
190371  */
190372  if( rc==SQLITE_OK ){
190373    rc = findLeafNode(pRtree, iDelete, &pLeaf, 0);
190374  }
190375
190376#ifdef CORRUPT_DB
190377  assert( pLeaf!=0 || rc!=SQLITE_OK || CORRUPT_DB );
190378#endif
190379
190380  /* Delete the cell in question from the leaf node. */
190381  if( rc==SQLITE_OK && pLeaf ){
190382    int rc2;
190383    rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
190384    if( rc==SQLITE_OK ){
190385      rc = deleteCell(pRtree, pLeaf, iCell, 0);
190386    }
190387    rc2 = nodeRelease(pRtree, pLeaf);
190388    if( rc==SQLITE_OK ){
190389      rc = rc2;
190390    }
190391  }
190392
190393  /* Delete the corresponding entry in the <rtree>_rowid table. */
190394  if( rc==SQLITE_OK ){
190395    sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
190396    sqlite3_step(pRtree->pDeleteRowid);
190397    rc = sqlite3_reset(pRtree->pDeleteRowid);
190398  }
190399
190400  /* Check if the root node now has exactly one child. If so, remove
190401  ** it, schedule the contents of the child for reinsertion and
190402  ** reduce the tree height by one.
190403  **
190404  ** This is equivalent to copying the contents of the child into
190405  ** the root node (the operation that Gutman's paper says to perform
190406  ** in this scenario).
190407  */
190408  if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
190409    int rc2;
190410    RtreeNode *pChild = 0;
190411    i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
190412    rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
190413    if( rc==SQLITE_OK ){
190414      rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
190415    }
190416    rc2 = nodeRelease(pRtree, pChild);
190417    if( rc==SQLITE_OK ) rc = rc2;
190418    if( rc==SQLITE_OK ){
190419      pRtree->iDepth--;
190420      writeInt16(pRoot->zData, pRtree->iDepth);
190421      pRoot->isDirty = 1;
190422    }
190423  }
190424
190425  /* Re-insert the contents of any underfull nodes removed from the tree. */
190426  for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
190427    if( rc==SQLITE_OK ){
190428      rc = reinsertNodeContent(pRtree, pLeaf);
190429    }
190430    pRtree->pDeleted = pLeaf->pNext;
190431    pRtree->nNodeRef--;
190432    sqlite3_free(pLeaf);
190433  }
190434
190435  /* Release the reference to the root node. */
190436  if( rc==SQLITE_OK ){
190437    rc = nodeRelease(pRtree, pRoot);
190438  }else{
190439    nodeRelease(pRtree, pRoot);
190440  }
190441
190442  return rc;
190443}
190444
190445/*
190446** Rounding constants for float->double conversion.
190447*/
190448#define RNDTOWARDS  (1.0 - 1.0/8388608.0)  /* Round towards zero */
190449#define RNDAWAY     (1.0 + 1.0/8388608.0)  /* Round away from zero */
190450
190451#if !defined(SQLITE_RTREE_INT_ONLY)
190452/*
190453** Convert an sqlite3_value into an RtreeValue (presumably a float)
190454** while taking care to round toward negative or positive, respectively.
190455*/
190456static RtreeValue rtreeValueDown(sqlite3_value *v){
190457  double d = sqlite3_value_double(v);
190458  float f = (float)d;
190459  if( f>d ){
190460    f = (float)(d*(d<0 ? RNDAWAY : RNDTOWARDS));
190461  }
190462  return f;
190463}
190464static RtreeValue rtreeValueUp(sqlite3_value *v){
190465  double d = sqlite3_value_double(v);
190466  float f = (float)d;
190467  if( f<d ){
190468    f = (float)(d*(d<0 ? RNDTOWARDS : RNDAWAY));
190469  }
190470  return f;
190471}
190472#endif /* !defined(SQLITE_RTREE_INT_ONLY) */
190473
190474/*
190475** A constraint has failed while inserting a row into an rtree table.
190476** Assuming no OOM error occurs, this function sets the error message
190477** (at pRtree->base.zErrMsg) to an appropriate value and returns
190478** SQLITE_CONSTRAINT.
190479**
190480** Parameter iCol is the index of the leftmost column involved in the
190481** constraint failure. If it is 0, then the constraint that failed is
190482** the unique constraint on the id column. Otherwise, it is the rtree
190483** (c1<=c2) constraint on columns iCol and iCol+1 that has failed.
190484**
190485** If an OOM occurs, SQLITE_NOMEM is returned instead of SQLITE_CONSTRAINT.
190486*/
190487static int rtreeConstraintError(Rtree *pRtree, int iCol){
190488  sqlite3_stmt *pStmt = 0;
190489  char *zSql;
190490  int rc;
190491
190492  assert( iCol==0 || iCol%2 );
190493  zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", pRtree->zDb, pRtree->zName);
190494  if( zSql ){
190495    rc = sqlite3_prepare_v2(pRtree->db, zSql, -1, &pStmt, 0);
190496  }else{
190497    rc = SQLITE_NOMEM;
190498  }
190499  sqlite3_free(zSql);
190500
190501  if( rc==SQLITE_OK ){
190502    if( iCol==0 ){
190503      const char *zCol = sqlite3_column_name(pStmt, 0);
190504      pRtree->base.zErrMsg = sqlite3_mprintf(
190505          "UNIQUE constraint failed: %s.%s", pRtree->zName, zCol
190506      );
190507    }else{
190508      const char *zCol1 = sqlite3_column_name(pStmt, iCol);
190509      const char *zCol2 = sqlite3_column_name(pStmt, iCol+1);
190510      pRtree->base.zErrMsg = sqlite3_mprintf(
190511          "rtree constraint failed: %s.(%s<=%s)", pRtree->zName, zCol1, zCol2
190512      );
190513    }
190514  }
190515
190516  sqlite3_finalize(pStmt);
190517  return (rc==SQLITE_OK ? SQLITE_CONSTRAINT : rc);
190518}
190519
190520
190521
190522/*
190523** The xUpdate method for rtree module virtual tables.
190524*/
190525static int rtreeUpdate(
190526  sqlite3_vtab *pVtab,
190527  int nData,
190528  sqlite3_value **aData,
190529  sqlite_int64 *pRowid
190530){
190531  Rtree *pRtree = (Rtree *)pVtab;
190532  int rc = SQLITE_OK;
190533  RtreeCell cell;                 /* New cell to insert if nData>1 */
190534  int bHaveRowid = 0;             /* Set to 1 after new rowid is determined */
190535
190536  if( pRtree->nNodeRef ){
190537    /* Unable to write to the btree while another cursor is reading from it,
190538    ** since the write might do a rebalance which would disrupt the read
190539    ** cursor. */
190540    return SQLITE_LOCKED_VTAB;
190541  }
190542  rtreeReference(pRtree);
190543  assert(nData>=1);
190544
190545  cell.iRowid = 0;  /* Used only to suppress a compiler warning */
190546
190547  /* Constraint handling. A write operation on an r-tree table may return
190548  ** SQLITE_CONSTRAINT for two reasons:
190549  **
190550  **   1. A duplicate rowid value, or
190551  **   2. The supplied data violates the "x2>=x1" constraint.
190552  **
190553  ** In the first case, if the conflict-handling mode is REPLACE, then
190554  ** the conflicting row can be removed before proceeding. In the second
190555  ** case, SQLITE_CONSTRAINT must be returned regardless of the
190556  ** conflict-handling mode specified by the user.
190557  */
190558  if( nData>1 ){
190559    int ii;
190560    int nn = nData - 4;
190561
190562    if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
190563    /* Populate the cell.aCoord[] array. The first coordinate is aData[3].
190564    **
190565    ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
190566    ** with "column" that are interpreted as table constraints.
190567    ** Example:  CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
190568    ** This problem was discovered after years of use, so we silently ignore
190569    ** these kinds of misdeclared tables to avoid breaking any legacy.
190570    */
190571
190572#ifndef SQLITE_RTREE_INT_ONLY
190573    if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
190574      for(ii=0; ii<nn; ii+=2){
190575        cell.aCoord[ii].f = rtreeValueDown(aData[ii+3]);
190576        cell.aCoord[ii+1].f = rtreeValueUp(aData[ii+4]);
190577        if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
190578          rc = rtreeConstraintError(pRtree, ii+1);
190579          goto constraint;
190580        }
190581      }
190582    }else
190583#endif
190584    {
190585      for(ii=0; ii<nn; ii+=2){
190586        cell.aCoord[ii].i = sqlite3_value_int(aData[ii+3]);
190587        cell.aCoord[ii+1].i = sqlite3_value_int(aData[ii+4]);
190588        if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
190589          rc = rtreeConstraintError(pRtree, ii+1);
190590          goto constraint;
190591        }
190592      }
190593    }
190594
190595    /* If a rowid value was supplied, check if it is already present in
190596    ** the table. If so, the constraint has failed. */
190597    if( sqlite3_value_type(aData[2])!=SQLITE_NULL ){
190598      cell.iRowid = sqlite3_value_int64(aData[2]);
190599      if( sqlite3_value_type(aData[0])==SQLITE_NULL
190600       || sqlite3_value_int64(aData[0])!=cell.iRowid
190601      ){
190602        int steprc;
190603        sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
190604        steprc = sqlite3_step(pRtree->pReadRowid);
190605        rc = sqlite3_reset(pRtree->pReadRowid);
190606        if( SQLITE_ROW==steprc ){
190607          if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
190608            rc = rtreeDeleteRowid(pRtree, cell.iRowid);
190609          }else{
190610            rc = rtreeConstraintError(pRtree, 0);
190611            goto constraint;
190612          }
190613        }
190614      }
190615      bHaveRowid = 1;
190616    }
190617  }
190618
190619  /* If aData[0] is not an SQL NULL value, it is the rowid of a
190620  ** record to delete from the r-tree table. The following block does
190621  ** just that.
190622  */
190623  if( sqlite3_value_type(aData[0])!=SQLITE_NULL ){
190624    rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(aData[0]));
190625  }
190626
190627  /* If the aData[] array contains more than one element, elements
190628  ** (aData[2]..aData[argc-1]) contain a new record to insert into
190629  ** the r-tree structure.
190630  */
190631  if( rc==SQLITE_OK && nData>1 ){
190632    /* Insert the new record into the r-tree */
190633    RtreeNode *pLeaf = 0;
190634
190635    /* Figure out the rowid of the new row. */
190636    if( bHaveRowid==0 ){
190637      rc = rtreeNewRowid(pRtree, &cell.iRowid);
190638    }
190639    *pRowid = cell.iRowid;
190640
190641    if( rc==SQLITE_OK ){
190642      rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
190643    }
190644    if( rc==SQLITE_OK ){
190645      int rc2;
190646      pRtree->iReinsertHeight = -1;
190647      rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
190648      rc2 = nodeRelease(pRtree, pLeaf);
190649      if( rc==SQLITE_OK ){
190650        rc = rc2;
190651      }
190652    }
190653    if( rc==SQLITE_OK && pRtree->nAux ){
190654      sqlite3_stmt *pUp = pRtree->pWriteAux;
190655      int jj;
190656      sqlite3_bind_int64(pUp, 1, *pRowid);
190657      for(jj=0; jj<pRtree->nAux; jj++){
190658        sqlite3_bind_value(pUp, jj+2, aData[pRtree->nDim2+3+jj]);
190659      }
190660      sqlite3_step(pUp);
190661      rc = sqlite3_reset(pUp);
190662    }
190663  }
190664
190665constraint:
190666  rtreeRelease(pRtree);
190667  return rc;
190668}
190669
190670/*
190671** Called when a transaction starts.
190672*/
190673static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
190674  Rtree *pRtree = (Rtree *)pVtab;
190675  assert( pRtree->inWrTrans==0 );
190676  pRtree->inWrTrans++;
190677  return SQLITE_OK;
190678}
190679
190680/*
190681** Called when a transaction completes (either by COMMIT or ROLLBACK).
190682** The sqlite3_blob object should be released at this point.
190683*/
190684static int rtreeEndTransaction(sqlite3_vtab *pVtab){
190685  Rtree *pRtree = (Rtree *)pVtab;
190686  pRtree->inWrTrans = 0;
190687  nodeBlobReset(pRtree);
190688  return SQLITE_OK;
190689}
190690
190691/*
190692** The xRename method for rtree module virtual tables.
190693*/
190694static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
190695  Rtree *pRtree = (Rtree *)pVtab;
190696  int rc = SQLITE_NOMEM;
190697  char *zSql = sqlite3_mprintf(
190698    "ALTER TABLE %Q.'%q_node'   RENAME TO \"%w_node\";"
190699    "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
190700    "ALTER TABLE %Q.'%q_rowid'  RENAME TO \"%w_rowid\";"
190701    , pRtree->zDb, pRtree->zName, zNewName
190702    , pRtree->zDb, pRtree->zName, zNewName
190703    , pRtree->zDb, pRtree->zName, zNewName
190704  );
190705  if( zSql ){
190706    nodeBlobReset(pRtree);
190707    rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
190708    sqlite3_free(zSql);
190709  }
190710  return rc;
190711}
190712
190713/*
190714** The xSavepoint method.
190715**
190716** This module does not need to do anything to support savepoints. However,
190717** it uses this hook to close any open blob handle. This is done because a
190718** DROP TABLE command - which fortunately always opens a savepoint - cannot
190719** succeed if there are any open blob handles. i.e. if the blob handle were
190720** not closed here, the following would fail:
190721**
190722**   BEGIN;
190723**     INSERT INTO rtree...
190724**     DROP TABLE <tablename>;    -- Would fail with SQLITE_LOCKED
190725**   COMMIT;
190726*/
190727static int rtreeSavepoint(sqlite3_vtab *pVtab, int iSavepoint){
190728  Rtree *pRtree = (Rtree *)pVtab;
190729  u8 iwt = pRtree->inWrTrans;
190730  UNUSED_PARAMETER(iSavepoint);
190731  pRtree->inWrTrans = 0;
190732  nodeBlobReset(pRtree);
190733  pRtree->inWrTrans = iwt;
190734  return SQLITE_OK;
190735}
190736
190737/*
190738** This function populates the pRtree->nRowEst variable with an estimate
190739** of the number of rows in the virtual table. If possible, this is based
190740** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
190741*/
190742static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
190743  const char *zFmt = "SELECT stat FROM %Q.sqlite_stat1 WHERE tbl = '%q_rowid'";
190744  char *zSql;
190745  sqlite3_stmt *p;
190746  int rc;
190747  i64 nRow = 0;
190748
190749  rc = sqlite3_table_column_metadata(
190750      db, pRtree->zDb, "sqlite_stat1",0,0,0,0,0,0
190751  );
190752  if( rc!=SQLITE_OK ){
190753    pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
190754    return rc==SQLITE_ERROR ? SQLITE_OK : rc;
190755  }
190756  zSql = sqlite3_mprintf(zFmt, pRtree->zDb, pRtree->zName);
190757  if( zSql==0 ){
190758    rc = SQLITE_NOMEM;
190759  }else{
190760    rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
190761    if( rc==SQLITE_OK ){
190762      if( sqlite3_step(p)==SQLITE_ROW ) nRow = sqlite3_column_int64(p, 0);
190763      rc = sqlite3_finalize(p);
190764    }else if( rc!=SQLITE_NOMEM ){
190765      rc = SQLITE_OK;
190766    }
190767
190768    if( rc==SQLITE_OK ){
190769      if( nRow==0 ){
190770        pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
190771      }else{
190772        pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST);
190773      }
190774    }
190775    sqlite3_free(zSql);
190776  }
190777
190778  return rc;
190779}
190780
190781
190782/*
190783** Return true if zName is the extension on one of the shadow tables used
190784** by this module.
190785*/
190786static int rtreeShadowName(const char *zName){
190787  static const char *azName[] = {
190788    "node", "parent", "rowid"
190789  };
190790  unsigned int i;
190791  for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
190792    if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
190793  }
190794  return 0;
190795}
190796
190797static sqlite3_module rtreeModule = {
190798  3,                          /* iVersion */
190799  rtreeCreate,                /* xCreate - create a table */
190800  rtreeConnect,               /* xConnect - connect to an existing table */
190801  rtreeBestIndex,             /* xBestIndex - Determine search strategy */
190802  rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
190803  rtreeDestroy,               /* xDestroy - Drop a table */
190804  rtreeOpen,                  /* xOpen - open a cursor */
190805  rtreeClose,                 /* xClose - close a cursor */
190806  rtreeFilter,                /* xFilter - configure scan constraints */
190807  rtreeNext,                  /* xNext - advance a cursor */
190808  rtreeEof,                   /* xEof */
190809  rtreeColumn,                /* xColumn - read data */
190810  rtreeRowid,                 /* xRowid - read data */
190811  rtreeUpdate,                /* xUpdate - write data */
190812  rtreeBeginTransaction,      /* xBegin - begin transaction */
190813  rtreeEndTransaction,        /* xSync - sync transaction */
190814  rtreeEndTransaction,        /* xCommit - commit transaction */
190815  rtreeEndTransaction,        /* xRollback - rollback transaction */
190816  0,                          /* xFindFunction - function overloading */
190817  rtreeRename,                /* xRename - rename the table */
190818  rtreeSavepoint,             /* xSavepoint */
190819  0,                          /* xRelease */
190820  0,                          /* xRollbackTo */
190821  rtreeShadowName             /* xShadowName */
190822};
190823
190824static int rtreeSqlInit(
190825  Rtree *pRtree,
190826  sqlite3 *db,
190827  const char *zDb,
190828  const char *zPrefix,
190829  int isCreate
190830){
190831  int rc = SQLITE_OK;
190832
190833  #define N_STATEMENT 8
190834  static const char *azSql[N_STATEMENT] = {
190835    /* Write the xxx_node table */
190836    "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(?1, ?2)",
190837    "DELETE FROM '%q'.'%q_node' WHERE nodeno = ?1",
190838
190839    /* Read and write the xxx_rowid table */
190840    "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = ?1",
190841    "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(?1, ?2)",
190842    "DELETE FROM '%q'.'%q_rowid' WHERE rowid = ?1",
190843
190844    /* Read and write the xxx_parent table */
190845    "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = ?1",
190846    "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(?1, ?2)",
190847    "DELETE FROM '%q'.'%q_parent' WHERE nodeno = ?1"
190848  };
190849  sqlite3_stmt **appStmt[N_STATEMENT];
190850  int i;
190851  const int f = SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB;
190852
190853  pRtree->db = db;
190854
190855  if( isCreate ){
190856    char *zCreate;
190857    sqlite3_str *p = sqlite3_str_new(db);
190858    int ii;
190859    sqlite3_str_appendf(p,
190860       "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY,nodeno",
190861       zDb, zPrefix);
190862    for(ii=0; ii<pRtree->nAux; ii++){
190863      sqlite3_str_appendf(p,",a%d",ii);
190864    }
190865    sqlite3_str_appendf(p,
190866      ");CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY,data);",
190867      zDb, zPrefix);
190868    sqlite3_str_appendf(p,
190869    "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,parentnode);",
190870      zDb, zPrefix);
190871    sqlite3_str_appendf(p,
190872       "INSERT INTO \"%w\".\"%w_node\"VALUES(1,zeroblob(%d))",
190873       zDb, zPrefix, pRtree->iNodeSize);
190874    zCreate = sqlite3_str_finish(p);
190875    if( !zCreate ){
190876      return SQLITE_NOMEM;
190877    }
190878    rc = sqlite3_exec(db, zCreate, 0, 0, 0);
190879    sqlite3_free(zCreate);
190880    if( rc!=SQLITE_OK ){
190881      return rc;
190882    }
190883  }
190884
190885  appStmt[0] = &pRtree->pWriteNode;
190886  appStmt[1] = &pRtree->pDeleteNode;
190887  appStmt[2] = &pRtree->pReadRowid;
190888  appStmt[3] = &pRtree->pWriteRowid;
190889  appStmt[4] = &pRtree->pDeleteRowid;
190890  appStmt[5] = &pRtree->pReadParent;
190891  appStmt[6] = &pRtree->pWriteParent;
190892  appStmt[7] = &pRtree->pDeleteParent;
190893
190894  rc = rtreeQueryStat1(db, pRtree);
190895  for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
190896    char *zSql;
190897    const char *zFormat;
190898    if( i!=3 || pRtree->nAux==0 ){
190899       zFormat = azSql[i];
190900    }else {
190901       /* An UPSERT is very slightly slower than REPLACE, but it is needed
190902       ** if there are auxiliary columns */
190903       zFormat = "INSERT INTO\"%w\".\"%w_rowid\"(rowid,nodeno)VALUES(?1,?2)"
190904                  "ON CONFLICT(rowid)DO UPDATE SET nodeno=excluded.nodeno";
190905    }
190906    zSql = sqlite3_mprintf(zFormat, zDb, zPrefix);
190907    if( zSql ){
190908      rc = sqlite3_prepare_v3(db, zSql, -1, f, appStmt[i], 0);
190909    }else{
190910      rc = SQLITE_NOMEM;
190911    }
190912    sqlite3_free(zSql);
190913  }
190914  if( pRtree->nAux ){
190915    pRtree->zReadAuxSql = sqlite3_mprintf(
190916       "SELECT * FROM \"%w\".\"%w_rowid\" WHERE rowid=?1",
190917       zDb, zPrefix);
190918    if( pRtree->zReadAuxSql==0 ){
190919      rc = SQLITE_NOMEM;
190920    }else{
190921      sqlite3_str *p = sqlite3_str_new(db);
190922      int ii;
190923      char *zSql;
190924      sqlite3_str_appendf(p, "UPDATE \"%w\".\"%w_rowid\"SET ", zDb, zPrefix);
190925      for(ii=0; ii<pRtree->nAux; ii++){
190926        if( ii ) sqlite3_str_append(p, ",", 1);
190927        if( ii<pRtree->nAuxNotNull ){
190928          sqlite3_str_appendf(p,"a%d=coalesce(?%d,a%d)",ii,ii+2,ii);
190929        }else{
190930          sqlite3_str_appendf(p,"a%d=?%d",ii,ii+2);
190931        }
190932      }
190933      sqlite3_str_appendf(p, " WHERE rowid=?1");
190934      zSql = sqlite3_str_finish(p);
190935      if( zSql==0 ){
190936        rc = SQLITE_NOMEM;
190937      }else{
190938        rc = sqlite3_prepare_v3(db, zSql, -1, f, &pRtree->pWriteAux, 0);
190939        sqlite3_free(zSql);
190940      }
190941    }
190942  }
190943
190944  return rc;
190945}
190946
190947/*
190948** The second argument to this function contains the text of an SQL statement
190949** that returns a single integer value. The statement is compiled and executed
190950** using database connection db. If successful, the integer value returned
190951** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
190952** code is returned and the value of *piVal after returning is not defined.
190953*/
190954static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
190955  int rc = SQLITE_NOMEM;
190956  if( zSql ){
190957    sqlite3_stmt *pStmt = 0;
190958    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
190959    if( rc==SQLITE_OK ){
190960      if( SQLITE_ROW==sqlite3_step(pStmt) ){
190961        *piVal = sqlite3_column_int(pStmt, 0);
190962      }
190963      rc = sqlite3_finalize(pStmt);
190964    }
190965  }
190966  return rc;
190967}
190968
190969/*
190970** This function is called from within the xConnect() or xCreate() method to
190971** determine the node-size used by the rtree table being created or connected
190972** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
190973** Otherwise, an SQLite error code is returned.
190974**
190975** If this function is being called as part of an xConnect(), then the rtree
190976** table already exists. In this case the node-size is determined by inspecting
190977** the root node of the tree.
190978**
190979** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
190980** This ensures that each node is stored on a single database page. If the
190981** database page-size is so large that more than RTREE_MAXCELLS entries
190982** would fit in a single node, use a smaller node-size.
190983*/
190984static int getNodeSize(
190985  sqlite3 *db,                    /* Database handle */
190986  Rtree *pRtree,                  /* Rtree handle */
190987  int isCreate,                   /* True for xCreate, false for xConnect */
190988  char **pzErr                    /* OUT: Error message, if any */
190989){
190990  int rc;
190991  char *zSql;
190992  if( isCreate ){
190993    int iPageSize = 0;
190994    zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
190995    rc = getIntFromStmt(db, zSql, &iPageSize);
190996    if( rc==SQLITE_OK ){
190997      pRtree->iNodeSize = iPageSize-64;
190998      if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
190999        pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
191000      }
191001    }else{
191002      *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
191003    }
191004  }else{
191005    zSql = sqlite3_mprintf(
191006        "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
191007        pRtree->zDb, pRtree->zName
191008    );
191009    rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
191010    if( rc!=SQLITE_OK ){
191011      *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
191012    }else if( pRtree->iNodeSize<(512-64) ){
191013      rc = SQLITE_CORRUPT_VTAB;
191014      RTREE_IS_CORRUPT(pRtree);
191015      *pzErr = sqlite3_mprintf("undersize RTree blobs in \"%q_node\"",
191016                               pRtree->zName);
191017    }
191018  }
191019
191020  sqlite3_free(zSql);
191021  return rc;
191022}
191023
191024/*
191025** Return the length of a token
191026*/
191027static int rtreeTokenLength(const char *z){
191028  int dummy = 0;
191029  return sqlite3GetToken((const unsigned char*)z,&dummy);
191030}
191031
191032/*
191033** This function is the implementation of both the xConnect and xCreate
191034** methods of the r-tree virtual table.
191035**
191036**   argv[0]   -> module name
191037**   argv[1]   -> database name
191038**   argv[2]   -> table name
191039**   argv[...] -> column names...
191040*/
191041static int rtreeInit(
191042  sqlite3 *db,                        /* Database connection */
191043  void *pAux,                         /* One of the RTREE_COORD_* constants */
191044  int argc, const char *const*argv,   /* Parameters to CREATE TABLE statement */
191045  sqlite3_vtab **ppVtab,              /* OUT: New virtual table */
191046  char **pzErr,                       /* OUT: Error message, if any */
191047  int isCreate                        /* True for xCreate, false for xConnect */
191048){
191049  int rc = SQLITE_OK;
191050  Rtree *pRtree;
191051  int nDb;              /* Length of string argv[1] */
191052  int nName;            /* Length of string argv[2] */
191053  int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
191054  sqlite3_str *pSql;
191055  char *zSql;
191056  int ii = 4;
191057  int iErr;
191058
191059  const char *aErrMsg[] = {
191060    0,                                                    /* 0 */
191061    "Wrong number of columns for an rtree table",         /* 1 */
191062    "Too few columns for an rtree table",                 /* 2 */
191063    "Too many columns for an rtree table",                /* 3 */
191064    "Auxiliary rtree columns must be last"                /* 4 */
191065  };
191066
191067  assert( RTREE_MAX_AUX_COLUMN<256 ); /* Aux columns counted by a u8 */
191068  if( argc<6 || argc>RTREE_MAX_AUX_COLUMN+3 ){
191069    *pzErr = sqlite3_mprintf("%s", aErrMsg[2 + (argc>=6)]);
191070    return SQLITE_ERROR;
191071  }
191072
191073  sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
191074
191075  /* Allocate the sqlite3_vtab structure */
191076  nDb = (int)strlen(argv[1]);
191077  nName = (int)strlen(argv[2]);
191078  pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName+2);
191079  if( !pRtree ){
191080    return SQLITE_NOMEM;
191081  }
191082  memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
191083  pRtree->nBusy = 1;
191084  pRtree->base.pModule = &rtreeModule;
191085  pRtree->zDb = (char *)&pRtree[1];
191086  pRtree->zName = &pRtree->zDb[nDb+1];
191087  pRtree->eCoordType = (u8)eCoordType;
191088  memcpy(pRtree->zDb, argv[1], nDb);
191089  memcpy(pRtree->zName, argv[2], nName);
191090
191091
191092  /* Create/Connect to the underlying relational database schema. If
191093  ** that is successful, call sqlite3_declare_vtab() to configure
191094  ** the r-tree table schema.
191095  */
191096  pSql = sqlite3_str_new(db);
191097  sqlite3_str_appendf(pSql, "CREATE TABLE x(%.*s INT",
191098                      rtreeTokenLength(argv[3]), argv[3]);
191099  for(ii=4; ii<argc; ii++){
191100    const char *zArg = argv[ii];
191101    if( zArg[0]=='+' ){
191102      pRtree->nAux++;
191103      sqlite3_str_appendf(pSql, ",%.*s", rtreeTokenLength(zArg+1), zArg+1);
191104    }else if( pRtree->nAux>0 ){
191105      break;
191106    }else{
191107      static const char *azFormat[] = {",%.*s REAL", ",%.*s INT"};
191108      pRtree->nDim2++;
191109      sqlite3_str_appendf(pSql, azFormat[eCoordType],
191110                          rtreeTokenLength(zArg), zArg);
191111    }
191112  }
191113  sqlite3_str_appendf(pSql, ");");
191114  zSql = sqlite3_str_finish(pSql);
191115  if( !zSql ){
191116    rc = SQLITE_NOMEM;
191117  }else if( ii<argc ){
191118    *pzErr = sqlite3_mprintf("%s", aErrMsg[4]);
191119    rc = SQLITE_ERROR;
191120  }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
191121    *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
191122  }
191123  sqlite3_free(zSql);
191124  if( rc ) goto rtreeInit_fail;
191125  pRtree->nDim = pRtree->nDim2/2;
191126  if( pRtree->nDim<1 ){
191127    iErr = 2;
191128  }else if( pRtree->nDim2>RTREE_MAX_DIMENSIONS*2 ){
191129    iErr = 3;
191130  }else if( pRtree->nDim2 % 2 ){
191131    iErr = 1;
191132  }else{
191133    iErr = 0;
191134  }
191135  if( iErr ){
191136    *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
191137    goto rtreeInit_fail;
191138  }
191139  pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
191140
191141  /* Figure out the node size to use. */
191142  rc = getNodeSize(db, pRtree, isCreate, pzErr);
191143  if( rc ) goto rtreeInit_fail;
191144  rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
191145  if( rc ){
191146    *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
191147    goto rtreeInit_fail;
191148  }
191149
191150  *ppVtab = (sqlite3_vtab *)pRtree;
191151  return SQLITE_OK;
191152
191153rtreeInit_fail:
191154  if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
191155  assert( *ppVtab==0 );
191156  assert( pRtree->nBusy==1 );
191157  rtreeRelease(pRtree);
191158  return rc;
191159}
191160
191161
191162/*
191163** Implementation of a scalar function that decodes r-tree nodes to
191164** human readable strings. This can be used for debugging and analysis.
191165**
191166** The scalar function takes two arguments: (1) the number of dimensions
191167** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing
191168** an r-tree node.  For a two-dimensional r-tree structure called "rt", to
191169** deserialize all nodes, a statement like:
191170**
191171**   SELECT rtreenode(2, data) FROM rt_node;
191172**
191173** The human readable string takes the form of a Tcl list with one
191174** entry for each cell in the r-tree node. Each entry is itself a
191175** list, containing the 8-byte rowid/pageno followed by the
191176** <num-dimension>*2 coordinates.
191177*/
191178static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
191179  RtreeNode node;
191180  Rtree tree;
191181  int ii;
191182  int nData;
191183  int errCode;
191184  sqlite3_str *pOut;
191185
191186  UNUSED_PARAMETER(nArg);
191187  memset(&node, 0, sizeof(RtreeNode));
191188  memset(&tree, 0, sizeof(Rtree));
191189  tree.nDim = (u8)sqlite3_value_int(apArg[0]);
191190  if( tree.nDim<1 || tree.nDim>5 ) return;
191191  tree.nDim2 = tree.nDim*2;
191192  tree.nBytesPerCell = 8 + 8 * tree.nDim;
191193  node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
191194  nData = sqlite3_value_bytes(apArg[1]);
191195  if( nData<4 ) return;
191196  if( nData<NCELL(&node)*tree.nBytesPerCell ) return;
191197
191198  pOut = sqlite3_str_new(0);
191199  for(ii=0; ii<NCELL(&node); ii++){
191200    RtreeCell cell;
191201    int jj;
191202
191203    nodeGetCell(&tree, &node, ii, &cell);
191204    if( ii>0 ) sqlite3_str_append(pOut, " ", 1);
191205    sqlite3_str_appendf(pOut, "{%lld", cell.iRowid);
191206    for(jj=0; jj<tree.nDim2; jj++){
191207#ifndef SQLITE_RTREE_INT_ONLY
191208      sqlite3_str_appendf(pOut, " %g", (double)cell.aCoord[jj].f);
191209#else
191210      sqlite3_str_appendf(pOut, " %d", cell.aCoord[jj].i);
191211#endif
191212    }
191213    sqlite3_str_append(pOut, "}", 1);
191214  }
191215  errCode = sqlite3_str_errcode(pOut);
191216  sqlite3_result_text(ctx, sqlite3_str_finish(pOut), -1, sqlite3_free);
191217  sqlite3_result_error_code(ctx, errCode);
191218}
191219
191220/* This routine implements an SQL function that returns the "depth" parameter
191221** from the front of a blob that is an r-tree node.  For example:
191222**
191223**     SELECT rtreedepth(data) FROM rt_node WHERE nodeno=1;
191224**
191225** The depth value is 0 for all nodes other than the root node, and the root
191226** node always has nodeno=1, so the example above is the primary use for this
191227** routine.  This routine is intended for testing and analysis only.
191228*/
191229static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
191230  UNUSED_PARAMETER(nArg);
191231  if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB
191232   || sqlite3_value_bytes(apArg[0])<2
191233  ){
191234    sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
191235  }else{
191236    u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
191237    sqlite3_result_int(ctx, readInt16(zBlob));
191238  }
191239}
191240
191241/*
191242** Context object passed between the various routines that make up the
191243** implementation of integrity-check function rtreecheck().
191244*/
191245typedef struct RtreeCheck RtreeCheck;
191246struct RtreeCheck {
191247  sqlite3 *db;                    /* Database handle */
191248  const char *zDb;                /* Database containing rtree table */
191249  const char *zTab;               /* Name of rtree table */
191250  int bInt;                       /* True for rtree_i32 table */
191251  int nDim;                       /* Number of dimensions for this rtree tbl */
191252  sqlite3_stmt *pGetNode;         /* Statement used to retrieve nodes */
191253  sqlite3_stmt *aCheckMapping[2]; /* Statements to query %_parent/%_rowid */
191254  int nLeaf;                      /* Number of leaf cells in table */
191255  int nNonLeaf;                   /* Number of non-leaf cells in table */
191256  int rc;                         /* Return code */
191257  char *zReport;                  /* Message to report */
191258  int nErr;                       /* Number of lines in zReport */
191259};
191260
191261#define RTREE_CHECK_MAX_ERROR 100
191262
191263/*
191264** Reset SQL statement pStmt. If the sqlite3_reset() call returns an error,
191265** and RtreeCheck.rc==SQLITE_OK, set RtreeCheck.rc to the error code.
191266*/
191267static void rtreeCheckReset(RtreeCheck *pCheck, sqlite3_stmt *pStmt){
191268  int rc = sqlite3_reset(pStmt);
191269  if( pCheck->rc==SQLITE_OK ) pCheck->rc = rc;
191270}
191271
191272/*
191273** The second and subsequent arguments to this function are a format string
191274** and printf style arguments. This function formats the string and attempts
191275** to compile it as an SQL statement.
191276**
191277** If successful, a pointer to the new SQL statement is returned. Otherwise,
191278** NULL is returned and an error code left in RtreeCheck.rc.
191279*/
191280static sqlite3_stmt *rtreeCheckPrepare(
191281  RtreeCheck *pCheck,             /* RtreeCheck object */
191282  const char *zFmt, ...           /* Format string and trailing args */
191283){
191284  va_list ap;
191285  char *z;
191286  sqlite3_stmt *pRet = 0;
191287
191288  va_start(ap, zFmt);
191289  z = sqlite3_vmprintf(zFmt, ap);
191290
191291  if( pCheck->rc==SQLITE_OK ){
191292    if( z==0 ){
191293      pCheck->rc = SQLITE_NOMEM;
191294    }else{
191295      pCheck->rc = sqlite3_prepare_v2(pCheck->db, z, -1, &pRet, 0);
191296    }
191297  }
191298
191299  sqlite3_free(z);
191300  va_end(ap);
191301  return pRet;
191302}
191303
191304/*
191305** The second and subsequent arguments to this function are a printf()
191306** style format string and arguments. This function formats the string and
191307** appends it to the report being accumuated in pCheck.
191308*/
191309static void rtreeCheckAppendMsg(RtreeCheck *pCheck, const char *zFmt, ...){
191310  va_list ap;
191311  va_start(ap, zFmt);
191312  if( pCheck->rc==SQLITE_OK && pCheck->nErr<RTREE_CHECK_MAX_ERROR ){
191313    char *z = sqlite3_vmprintf(zFmt, ap);
191314    if( z==0 ){
191315      pCheck->rc = SQLITE_NOMEM;
191316    }else{
191317      pCheck->zReport = sqlite3_mprintf("%z%s%z",
191318          pCheck->zReport, (pCheck->zReport ? "\n" : ""), z
191319      );
191320      if( pCheck->zReport==0 ){
191321        pCheck->rc = SQLITE_NOMEM;
191322      }
191323    }
191324    pCheck->nErr++;
191325  }
191326  va_end(ap);
191327}
191328
191329/*
191330** This function is a no-op if there is already an error code stored
191331** in the RtreeCheck object indicated by the first argument. NULL is
191332** returned in this case.
191333**
191334** Otherwise, the contents of rtree table node iNode are loaded from
191335** the database and copied into a buffer obtained from sqlite3_malloc().
191336** If no error occurs, a pointer to the buffer is returned and (*pnNode)
191337** is set to the size of the buffer in bytes.
191338**
191339** Or, if an error does occur, NULL is returned and an error code left
191340** in the RtreeCheck object. The final value of *pnNode is undefined in
191341** this case.
191342*/
191343static u8 *rtreeCheckGetNode(RtreeCheck *pCheck, i64 iNode, int *pnNode){
191344  u8 *pRet = 0;                   /* Return value */
191345
191346  if( pCheck->rc==SQLITE_OK && pCheck->pGetNode==0 ){
191347    pCheck->pGetNode = rtreeCheckPrepare(pCheck,
191348        "SELECT data FROM %Q.'%q_node' WHERE nodeno=?",
191349        pCheck->zDb, pCheck->zTab
191350    );
191351  }
191352
191353  if( pCheck->rc==SQLITE_OK ){
191354    sqlite3_bind_int64(pCheck->pGetNode, 1, iNode);
191355    if( sqlite3_step(pCheck->pGetNode)==SQLITE_ROW ){
191356      int nNode = sqlite3_column_bytes(pCheck->pGetNode, 0);
191357      const u8 *pNode = (const u8*)sqlite3_column_blob(pCheck->pGetNode, 0);
191358      pRet = sqlite3_malloc64(nNode);
191359      if( pRet==0 ){
191360        pCheck->rc = SQLITE_NOMEM;
191361      }else{
191362        memcpy(pRet, pNode, nNode);
191363        *pnNode = nNode;
191364      }
191365    }
191366    rtreeCheckReset(pCheck, pCheck->pGetNode);
191367    if( pCheck->rc==SQLITE_OK && pRet==0 ){
191368      rtreeCheckAppendMsg(pCheck, "Node %lld missing from database", iNode);
191369    }
191370  }
191371
191372  return pRet;
191373}
191374
191375/*
191376** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
191377** (if bLeaf==1) table contains a specified entry. The schemas of the
191378** two tables are:
191379**
191380**   CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
191381**   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
191382**
191383** In both cases, this function checks that there exists an entry with
191384** IPK value iKey and the second column set to iVal.
191385**
191386*/
191387static void rtreeCheckMapping(
191388  RtreeCheck *pCheck,             /* RtreeCheck object */
191389  int bLeaf,                      /* True for a leaf cell, false for interior */
191390  i64 iKey,                       /* Key for mapping */
191391  i64 iVal                        /* Expected value for mapping */
191392){
191393  int rc;
191394  sqlite3_stmt *pStmt;
191395  const char *azSql[2] = {
191396    "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?1",
191397    "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?1"
191398  };
191399
191400  assert( bLeaf==0 || bLeaf==1 );
191401  if( pCheck->aCheckMapping[bLeaf]==0 ){
191402    pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
191403        azSql[bLeaf], pCheck->zDb, pCheck->zTab
191404    );
191405  }
191406  if( pCheck->rc!=SQLITE_OK ) return;
191407
191408  pStmt = pCheck->aCheckMapping[bLeaf];
191409  sqlite3_bind_int64(pStmt, 1, iKey);
191410  rc = sqlite3_step(pStmt);
191411  if( rc==SQLITE_DONE ){
191412    rtreeCheckAppendMsg(pCheck, "Mapping (%lld -> %lld) missing from %s table",
191413        iKey, iVal, (bLeaf ? "%_rowid" : "%_parent")
191414    );
191415  }else if( rc==SQLITE_ROW ){
191416    i64 ii = sqlite3_column_int64(pStmt, 0);
191417    if( ii!=iVal ){
191418      rtreeCheckAppendMsg(pCheck,
191419          "Found (%lld -> %lld) in %s table, expected (%lld -> %lld)",
191420          iKey, ii, (bLeaf ? "%_rowid" : "%_parent"), iKey, iVal
191421      );
191422    }
191423  }
191424  rtreeCheckReset(pCheck, pStmt);
191425}
191426
191427/*
191428** Argument pCell points to an array of coordinates stored on an rtree page.
191429** This function checks that the coordinates are internally consistent (no
191430** x1>x2 conditions) and adds an error message to the RtreeCheck object
191431** if they are not.
191432**
191433** Additionally, if pParent is not NULL, then it is assumed to point to
191434** the array of coordinates on the parent page that bound the page
191435** containing pCell. In this case it is also verified that the two
191436** sets of coordinates are mutually consistent and an error message added
191437** to the RtreeCheck object if they are not.
191438*/
191439static void rtreeCheckCellCoord(
191440  RtreeCheck *pCheck,
191441  i64 iNode,                      /* Node id to use in error messages */
191442  int iCell,                      /* Cell number to use in error messages */
191443  u8 *pCell,                      /* Pointer to cell coordinates */
191444  u8 *pParent                     /* Pointer to parent coordinates */
191445){
191446  RtreeCoord c1, c2;
191447  RtreeCoord p1, p2;
191448  int i;
191449
191450  for(i=0; i<pCheck->nDim; i++){
191451    readCoord(&pCell[4*2*i], &c1);
191452    readCoord(&pCell[4*(2*i + 1)], &c2);
191453
191454    /* printf("%e, %e\n", c1.u.f, c2.u.f); */
191455    if( pCheck->bInt ? c1.i>c2.i : c1.f>c2.f ){
191456      rtreeCheckAppendMsg(pCheck,
191457          "Dimension %d of cell %d on node %lld is corrupt", i, iCell, iNode
191458      );
191459    }
191460
191461    if( pParent ){
191462      readCoord(&pParent[4*2*i], &p1);
191463      readCoord(&pParent[4*(2*i + 1)], &p2);
191464
191465      if( (pCheck->bInt ? c1.i<p1.i : c1.f<p1.f)
191466       || (pCheck->bInt ? c2.i>p2.i : c2.f>p2.f)
191467      ){
191468        rtreeCheckAppendMsg(pCheck,
191469            "Dimension %d of cell %d on node %lld is corrupt relative to parent"
191470            , i, iCell, iNode
191471        );
191472      }
191473    }
191474  }
191475}
191476
191477/*
191478** Run rtreecheck() checks on node iNode, which is at depth iDepth within
191479** the r-tree structure. Argument aParent points to the array of coordinates
191480** that bound node iNode on the parent node.
191481**
191482** If any problems are discovered, an error message is appended to the
191483** report accumulated in the RtreeCheck object.
191484*/
191485static void rtreeCheckNode(
191486  RtreeCheck *pCheck,
191487  int iDepth,                     /* Depth of iNode (0==leaf) */
191488  u8 *aParent,                    /* Buffer containing parent coords */
191489  i64 iNode                       /* Node to check */
191490){
191491  u8 *aNode = 0;
191492  int nNode = 0;
191493
191494  assert( iNode==1 || aParent!=0 );
191495  assert( pCheck->nDim>0 );
191496
191497  aNode = rtreeCheckGetNode(pCheck, iNode, &nNode);
191498  if( aNode ){
191499    if( nNode<4 ){
191500      rtreeCheckAppendMsg(pCheck,
191501          "Node %lld is too small (%d bytes)", iNode, nNode
191502      );
191503    }else{
191504      int nCell;                  /* Number of cells on page */
191505      int i;                      /* Used to iterate through cells */
191506      if( aParent==0 ){
191507        iDepth = readInt16(aNode);
191508        if( iDepth>RTREE_MAX_DEPTH ){
191509          rtreeCheckAppendMsg(pCheck, "Rtree depth out of range (%d)", iDepth);
191510          sqlite3_free(aNode);
191511          return;
191512        }
191513      }
191514      nCell = readInt16(&aNode[2]);
191515      if( (4 + nCell*(8 + pCheck->nDim*2*4))>nNode ){
191516        rtreeCheckAppendMsg(pCheck,
191517            "Node %lld is too small for cell count of %d (%d bytes)",
191518            iNode, nCell, nNode
191519        );
191520      }else{
191521        for(i=0; i<nCell; i++){
191522          u8 *pCell = &aNode[4 + i*(8 + pCheck->nDim*2*4)];
191523          i64 iVal = readInt64(pCell);
191524          rtreeCheckCellCoord(pCheck, iNode, i, &pCell[8], aParent);
191525
191526          if( iDepth>0 ){
191527            rtreeCheckMapping(pCheck, 0, iVal, iNode);
191528            rtreeCheckNode(pCheck, iDepth-1, &pCell[8], iVal);
191529            pCheck->nNonLeaf++;
191530          }else{
191531            rtreeCheckMapping(pCheck, 1, iVal, iNode);
191532            pCheck->nLeaf++;
191533          }
191534        }
191535      }
191536    }
191537    sqlite3_free(aNode);
191538  }
191539}
191540
191541/*
191542** The second argument to this function must be either "_rowid" or
191543** "_parent". This function checks that the number of entries in the
191544** %_rowid or %_parent table is exactly nExpect. If not, it adds
191545** an error message to the report in the RtreeCheck object indicated
191546** by the first argument.
191547*/
191548static void rtreeCheckCount(RtreeCheck *pCheck, const char *zTbl, i64 nExpect){
191549  if( pCheck->rc==SQLITE_OK ){
191550    sqlite3_stmt *pCount;
191551    pCount = rtreeCheckPrepare(pCheck, "SELECT count(*) FROM %Q.'%q%s'",
191552        pCheck->zDb, pCheck->zTab, zTbl
191553    );
191554    if( pCount ){
191555      if( sqlite3_step(pCount)==SQLITE_ROW ){
191556        i64 nActual = sqlite3_column_int64(pCount, 0);
191557        if( nActual!=nExpect ){
191558          rtreeCheckAppendMsg(pCheck, "Wrong number of entries in %%%s table"
191559              " - expected %lld, actual %lld" , zTbl, nExpect, nActual
191560          );
191561        }
191562      }
191563      pCheck->rc = sqlite3_finalize(pCount);
191564    }
191565  }
191566}
191567
191568/*
191569** This function does the bulk of the work for the rtree integrity-check.
191570** It is called by rtreecheck(), which is the SQL function implementation.
191571*/
191572static int rtreeCheckTable(
191573  sqlite3 *db,                    /* Database handle to access db through */
191574  const char *zDb,                /* Name of db ("main", "temp" etc.) */
191575  const char *zTab,               /* Name of rtree table to check */
191576  char **pzReport                 /* OUT: sqlite3_malloc'd report text */
191577){
191578  RtreeCheck check;               /* Common context for various routines */
191579  sqlite3_stmt *pStmt = 0;        /* Used to find column count of rtree table */
191580  int bEnd = 0;                   /* True if transaction should be closed */
191581  int nAux = 0;                   /* Number of extra columns. */
191582
191583  /* Initialize the context object */
191584  memset(&check, 0, sizeof(check));
191585  check.db = db;
191586  check.zDb = zDb;
191587  check.zTab = zTab;
191588
191589  /* If there is not already an open transaction, open one now. This is
191590  ** to ensure that the queries run as part of this integrity-check operate
191591  ** on a consistent snapshot.  */
191592  if( sqlite3_get_autocommit(db) ){
191593    check.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
191594    bEnd = 1;
191595  }
191596
191597  /* Find the number of auxiliary columns */
191598  if( check.rc==SQLITE_OK ){
191599    pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.'%q_rowid'", zDb, zTab);
191600    if( pStmt ){
191601      nAux = sqlite3_column_count(pStmt) - 2;
191602      sqlite3_finalize(pStmt);
191603    }
191604    check.rc = SQLITE_OK;
191605  }
191606
191607  /* Find number of dimensions in the rtree table. */
191608  pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab);
191609  if( pStmt ){
191610    int rc;
191611    check.nDim = (sqlite3_column_count(pStmt) - 1 - nAux) / 2;
191612    if( check.nDim<1 ){
191613      rtreeCheckAppendMsg(&check, "Schema corrupt or not an rtree");
191614    }else if( SQLITE_ROW==sqlite3_step(pStmt) ){
191615      check.bInt = (sqlite3_column_type(pStmt, 1)==SQLITE_INTEGER);
191616    }
191617    rc = sqlite3_finalize(pStmt);
191618    if( rc!=SQLITE_CORRUPT ) check.rc = rc;
191619  }
191620
191621  /* Do the actual integrity-check */
191622  if( check.nDim>=1 ){
191623    if( check.rc==SQLITE_OK ){
191624      rtreeCheckNode(&check, 0, 0, 1);
191625    }
191626    rtreeCheckCount(&check, "_rowid", check.nLeaf);
191627    rtreeCheckCount(&check, "_parent", check.nNonLeaf);
191628  }
191629
191630  /* Finalize SQL statements used by the integrity-check */
191631  sqlite3_finalize(check.pGetNode);
191632  sqlite3_finalize(check.aCheckMapping[0]);
191633  sqlite3_finalize(check.aCheckMapping[1]);
191634
191635  /* If one was opened, close the transaction */
191636  if( bEnd ){
191637    int rc = sqlite3_exec(db, "END", 0, 0, 0);
191638    if( check.rc==SQLITE_OK ) check.rc = rc;
191639  }
191640  *pzReport = check.zReport;
191641  return check.rc;
191642}
191643
191644/*
191645** Usage:
191646**
191647**   rtreecheck(<rtree-table>);
191648**   rtreecheck(<database>, <rtree-table>);
191649**
191650** Invoking this SQL function runs an integrity-check on the named rtree
191651** table. The integrity-check verifies the following:
191652**
191653**   1. For each cell in the r-tree structure (%_node table), that:
191654**
191655**       a) for each dimension, (coord1 <= coord2).
191656**
191657**       b) unless the cell is on the root node, that the cell is bounded
191658**          by the parent cell on the parent node.
191659**
191660**       c) for leaf nodes, that there is an entry in the %_rowid
191661**          table corresponding to the cell's rowid value that
191662**          points to the correct node.
191663**
191664**       d) for cells on non-leaf nodes, that there is an entry in the
191665**          %_parent table mapping from the cell's child node to the
191666**          node that it resides on.
191667**
191668**   2. That there are the same number of entries in the %_rowid table
191669**      as there are leaf cells in the r-tree structure, and that there
191670**      is a leaf cell that corresponds to each entry in the %_rowid table.
191671**
191672**   3. That there are the same number of entries in the %_parent table
191673**      as there are non-leaf cells in the r-tree structure, and that
191674**      there is a non-leaf cell that corresponds to each entry in the
191675**      %_parent table.
191676*/
191677static void rtreecheck(
191678  sqlite3_context *ctx,
191679  int nArg,
191680  sqlite3_value **apArg
191681){
191682  if( nArg!=1 && nArg!=2 ){
191683    sqlite3_result_error(ctx,
191684        "wrong number of arguments to function rtreecheck()", -1
191685    );
191686  }else{
191687    int rc;
191688    char *zReport = 0;
191689    const char *zDb = (const char*)sqlite3_value_text(apArg[0]);
191690    const char *zTab;
191691    if( nArg==1 ){
191692      zTab = zDb;
191693      zDb = "main";
191694    }else{
191695      zTab = (const char*)sqlite3_value_text(apArg[1]);
191696    }
191697    rc = rtreeCheckTable(sqlite3_context_db_handle(ctx), zDb, zTab, &zReport);
191698    if( rc==SQLITE_OK ){
191699      sqlite3_result_text(ctx, zReport ? zReport : "ok", -1, SQLITE_TRANSIENT);
191700    }else{
191701      sqlite3_result_error_code(ctx, rc);
191702    }
191703    sqlite3_free(zReport);
191704  }
191705}
191706
191707/* Conditionally include the geopoly code */
191708#ifdef SQLITE_ENABLE_GEOPOLY
191709/************** Include geopoly.c in the middle of rtree.c *******************/
191710/************** Begin file geopoly.c *****************************************/
191711/*
191712** 2018-05-25
191713**
191714** The author disclaims copyright to this source code.  In place of
191715** a legal notice, here is a blessing:
191716**
191717**    May you do good and not evil.
191718**    May you find forgiveness for yourself and forgive others.
191719**    May you share freely, never taking more than you give.
191720**
191721******************************************************************************
191722**
191723** This file implements an alternative R-Tree virtual table that
191724** uses polygons to express the boundaries of 2-dimensional objects.
191725**
191726** This file is #include-ed onto the end of "rtree.c" so that it has
191727** access to all of the R-Tree internals.
191728*/
191729/* #include <stdlib.h> */
191730
191731/* Enable -DGEOPOLY_ENABLE_DEBUG for debugging facilities */
191732#ifdef GEOPOLY_ENABLE_DEBUG
191733  static int geo_debug = 0;
191734# define GEODEBUG(X) if(geo_debug)printf X
191735#else
191736# define GEODEBUG(X)
191737#endif
191738
191739#ifndef JSON_NULL   /* The following stuff repeats things found in json1 */
191740/*
191741** Versions of isspace(), isalnum() and isdigit() to which it is safe
191742** to pass signed char values.
191743*/
191744#ifdef sqlite3Isdigit
191745   /* Use the SQLite core versions if this routine is part of the
191746   ** SQLite amalgamation */
191747#  define safe_isdigit(x)  sqlite3Isdigit(x)
191748#  define safe_isalnum(x)  sqlite3Isalnum(x)
191749#  define safe_isxdigit(x) sqlite3Isxdigit(x)
191750#else
191751   /* Use the standard library for separate compilation */
191752#include <ctype.h>  /* amalgamator: keep */
191753#  define safe_isdigit(x)  isdigit((unsigned char)(x))
191754#  define safe_isalnum(x)  isalnum((unsigned char)(x))
191755#  define safe_isxdigit(x) isxdigit((unsigned char)(x))
191756#endif
191757
191758/*
191759** Growing our own isspace() routine this way is twice as fast as
191760** the library isspace() function.
191761*/
191762static const char geopolyIsSpace[] = {
191763  0, 0, 0, 0, 0, 0, 0, 0,     0, 1, 1, 0, 0, 1, 0, 0,
191764  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
191765  1, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
191766  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
191767  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
191768  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
191769  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
191770  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
191771  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
191772  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
191773  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
191774  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
191775  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
191776  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
191777  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
191778  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
191779};
191780#define safe_isspace(x) (geopolyIsSpace[(unsigned char)x])
191781#endif /* JSON NULL - back to original code */
191782
191783/* Compiler and version */
191784#ifndef GCC_VERSION
191785#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
191786# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
191787#else
191788# define GCC_VERSION 0
191789#endif
191790#endif
191791#ifndef MSVC_VERSION
191792#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
191793# define MSVC_VERSION _MSC_VER
191794#else
191795# define MSVC_VERSION 0
191796#endif
191797#endif
191798
191799/* Datatype for coordinates
191800*/
191801typedef float GeoCoord;
191802
191803/*
191804** Internal representation of a polygon.
191805**
191806** The polygon consists of a sequence of vertexes.  There is a line
191807** segment between each pair of vertexes, and one final segment from
191808** the last vertex back to the first.  (This differs from the GeoJSON
191809** standard in which the final vertex is a repeat of the first.)
191810**
191811** The polygon follows the right-hand rule.  The area to the right of
191812** each segment is "outside" and the area to the left is "inside".
191813**
191814** The on-disk representation consists of a 4-byte header followed by
191815** the values.  The 4-byte header is:
191816**
191817**      encoding    (1 byte)   0=big-endian, 1=little-endian
191818**      nvertex     (3 bytes)  Number of vertexes as a big-endian integer
191819**
191820** Enough space is allocated for 4 coordinates, to work around over-zealous
191821** warnings coming from some compiler (notably, clang). In reality, the size
191822** of each GeoPoly memory allocate is adjusted as necessary so that the
191823** GeoPoly.a[] array at the end is the appropriate size.
191824*/
191825typedef struct GeoPoly GeoPoly;
191826struct GeoPoly {
191827  int nVertex;          /* Number of vertexes */
191828  unsigned char hdr[4]; /* Header for on-disk representation */
191829  GeoCoord a[8];        /* 2*nVertex values. X (longitude) first, then Y */
191830};
191831
191832/* The size of a memory allocation needed for a GeoPoly object sufficient
191833** to hold N coordinate pairs.
191834*/
191835#define GEOPOLY_SZ(N)  (sizeof(GeoPoly) + sizeof(GeoCoord)*2*((N)-4))
191836
191837/* Macros to access coordinates of a GeoPoly.
191838** We have to use these macros, rather than just say p->a[i] in order
191839** to silence (incorrect) UBSAN warnings if the array index is too large.
191840*/
191841#define GeoX(P,I)  (((GeoCoord*)(P)->a)[(I)*2])
191842#define GeoY(P,I)  (((GeoCoord*)(P)->a)[(I)*2+1])
191843
191844
191845/*
191846** State of a parse of a GeoJSON input.
191847*/
191848typedef struct GeoParse GeoParse;
191849struct GeoParse {
191850  const unsigned char *z;   /* Unparsed input */
191851  int nVertex;              /* Number of vertexes in a[] */
191852  int nAlloc;               /* Space allocated to a[] */
191853  int nErr;                 /* Number of errors encountered */
191854  GeoCoord *a;          /* Array of vertexes.  From sqlite3_malloc64() */
191855};
191856
191857/* Do a 4-byte byte swap */
191858static void geopolySwab32(unsigned char *a){
191859  unsigned char t = a[0];
191860  a[0] = a[3];
191861  a[3] = t;
191862  t = a[1];
191863  a[1] = a[2];
191864  a[2] = t;
191865}
191866
191867/* Skip whitespace.  Return the next non-whitespace character. */
191868static char geopolySkipSpace(GeoParse *p){
191869  while( safe_isspace(p->z[0]) ) p->z++;
191870  return p->z[0];
191871}
191872
191873/* Parse out a number.  Write the value into *pVal if pVal!=0.
191874** return non-zero on success and zero if the next token is not a number.
191875*/
191876static int geopolyParseNumber(GeoParse *p, GeoCoord *pVal){
191877  char c = geopolySkipSpace(p);
191878  const unsigned char *z = p->z;
191879  int j = 0;
191880  int seenDP = 0;
191881  int seenE = 0;
191882  if( c=='-' ){
191883    j = 1;
191884    c = z[j];
191885  }
191886  if( c=='0' && z[j+1]>='0' && z[j+1]<='9' ) return 0;
191887  for(;; j++){
191888    c = z[j];
191889    if( safe_isdigit(c) ) continue;
191890    if( c=='.' ){
191891      if( z[j-1]=='-' ) return 0;
191892      if( seenDP ) return 0;
191893      seenDP = 1;
191894      continue;
191895    }
191896    if( c=='e' || c=='E' ){
191897      if( z[j-1]<'0' ) return 0;
191898      if( seenE ) return -1;
191899      seenDP = seenE = 1;
191900      c = z[j+1];
191901      if( c=='+' || c=='-' ){
191902        j++;
191903        c = z[j+1];
191904      }
191905      if( c<'0' || c>'9' ) return 0;
191906      continue;
191907    }
191908    break;
191909  }
191910  if( z[j-1]<'0' ) return 0;
191911  if( pVal ){
191912#ifdef SQLITE_AMALGAMATION
191913     /* The sqlite3AtoF() routine is much much faster than atof(), if it
191914     ** is available */
191915     double r;
191916     (void)sqlite3AtoF((const char*)p->z, &r, j, SQLITE_UTF8);
191917     *pVal = r;
191918#else
191919     *pVal = (GeoCoord)atof((const char*)p->z);
191920#endif
191921  }
191922  p->z += j;
191923  return 1;
191924}
191925
191926/*
191927** If the input is a well-formed JSON array of coordinates with at least
191928** four coordinates and where each coordinate is itself a two-value array,
191929** then convert the JSON into a GeoPoly object and return a pointer to
191930** that object.
191931**
191932** If any error occurs, return NULL.
191933*/
191934static GeoPoly *geopolyParseJson(const unsigned char *z, int *pRc){
191935  GeoParse s;
191936  int rc = SQLITE_OK;
191937  memset(&s, 0, sizeof(s));
191938  s.z = z;
191939  if( geopolySkipSpace(&s)=='[' ){
191940    s.z++;
191941    while( geopolySkipSpace(&s)=='[' ){
191942      int ii = 0;
191943      char c;
191944      s.z++;
191945      if( s.nVertex>=s.nAlloc ){
191946        GeoCoord *aNew;
191947        s.nAlloc = s.nAlloc*2 + 16;
191948        aNew = sqlite3_realloc64(s.a, s.nAlloc*sizeof(GeoCoord)*2 );
191949        if( aNew==0 ){
191950          rc = SQLITE_NOMEM;
191951          s.nErr++;
191952          break;
191953        }
191954        s.a = aNew;
191955      }
191956      while( geopolyParseNumber(&s, ii<=1 ? &s.a[s.nVertex*2+ii] : 0) ){
191957        ii++;
191958        if( ii==2 ) s.nVertex++;
191959        c = geopolySkipSpace(&s);
191960        s.z++;
191961        if( c==',' ) continue;
191962        if( c==']' && ii>=2 ) break;
191963        s.nErr++;
191964        rc = SQLITE_ERROR;
191965        goto parse_json_err;
191966      }
191967      if( geopolySkipSpace(&s)==',' ){
191968        s.z++;
191969        continue;
191970      }
191971      break;
191972    }
191973    if( geopolySkipSpace(&s)==']'
191974     && s.nVertex>=4
191975     && s.a[0]==s.a[s.nVertex*2-2]
191976     && s.a[1]==s.a[s.nVertex*2-1]
191977     && (s.z++, geopolySkipSpace(&s)==0)
191978    ){
191979      GeoPoly *pOut;
191980      int x = 1;
191981      s.nVertex--;  /* Remove the redundant vertex at the end */
191982      pOut = sqlite3_malloc64( GEOPOLY_SZ((sqlite3_int64)s.nVertex) );
191983      x = 1;
191984      if( pOut==0 ) goto parse_json_err;
191985      pOut->nVertex = s.nVertex;
191986      memcpy(pOut->a, s.a, s.nVertex*2*sizeof(GeoCoord));
191987      pOut->hdr[0] = *(unsigned char*)&x;
191988      pOut->hdr[1] = (s.nVertex>>16)&0xff;
191989      pOut->hdr[2] = (s.nVertex>>8)&0xff;
191990      pOut->hdr[3] = s.nVertex&0xff;
191991      sqlite3_free(s.a);
191992      if( pRc ) *pRc = SQLITE_OK;
191993      return pOut;
191994    }else{
191995      s.nErr++;
191996      rc = SQLITE_ERROR;
191997    }
191998  }
191999parse_json_err:
192000  if( pRc ) *pRc = rc;
192001  sqlite3_free(s.a);
192002  return 0;
192003}
192004
192005/*
192006** Given a function parameter, try to interpret it as a polygon, either
192007** in the binary format or JSON text.  Compute a GeoPoly object and
192008** return a pointer to that object.  Or if the input is not a well-formed
192009** polygon, put an error message in sqlite3_context and return NULL.
192010*/
192011static GeoPoly *geopolyFuncParam(
192012  sqlite3_context *pCtx,      /* Context for error messages */
192013  sqlite3_value *pVal,        /* The value to decode */
192014  int *pRc                    /* Write error here */
192015){
192016  GeoPoly *p = 0;
192017  int nByte;
192018  if( sqlite3_value_type(pVal)==SQLITE_BLOB
192019   && (nByte = sqlite3_value_bytes(pVal))>=(4+6*sizeof(GeoCoord))
192020  ){
192021    const unsigned char *a = sqlite3_value_blob(pVal);
192022    int nVertex;
192023    nVertex = (a[1]<<16) + (a[2]<<8) + a[3];
192024    if( (a[0]==0 || a[0]==1)
192025     && (nVertex*2*sizeof(GeoCoord) + 4)==(unsigned int)nByte
192026    ){
192027      p = sqlite3_malloc64( sizeof(*p) + (nVertex-1)*2*sizeof(GeoCoord) );
192028      if( p==0 ){
192029        if( pRc ) *pRc = SQLITE_NOMEM;
192030        if( pCtx ) sqlite3_result_error_nomem(pCtx);
192031      }else{
192032        int x = 1;
192033        p->nVertex = nVertex;
192034        memcpy(p->hdr, a, nByte);
192035        if( a[0] != *(unsigned char*)&x ){
192036          int ii;
192037          for(ii=0; ii<nVertex; ii++){
192038            geopolySwab32((unsigned char*)&GeoX(p,ii));
192039            geopolySwab32((unsigned char*)&GeoY(p,ii));
192040          }
192041          p->hdr[0] ^= 1;
192042        }
192043      }
192044    }
192045    if( pRc ) *pRc = SQLITE_OK;
192046    return p;
192047  }else if( sqlite3_value_type(pVal)==SQLITE_TEXT ){
192048    const unsigned char *zJson = sqlite3_value_text(pVal);
192049    if( zJson==0 ){
192050      if( pRc ) *pRc = SQLITE_NOMEM;
192051      return 0;
192052    }
192053    return geopolyParseJson(zJson, pRc);
192054  }else{
192055    if( pRc ) *pRc = SQLITE_ERROR;
192056    return 0;
192057  }
192058}
192059
192060/*
192061** Implementation of the geopoly_blob(X) function.
192062**
192063** If the input is a well-formed Geopoly BLOB or JSON string
192064** then return the BLOB representation of the polygon.  Otherwise
192065** return NULL.
192066*/
192067static void geopolyBlobFunc(
192068  sqlite3_context *context,
192069  int argc,
192070  sqlite3_value **argv
192071){
192072  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
192073  if( p ){
192074    sqlite3_result_blob(context, p->hdr,
192075       4+8*p->nVertex, SQLITE_TRANSIENT);
192076    sqlite3_free(p);
192077  }
192078}
192079
192080/*
192081** SQL function:     geopoly_json(X)
192082**
192083** Interpret X as a polygon and render it as a JSON array
192084** of coordinates.  Or, if X is not a valid polygon, return NULL.
192085*/
192086static void geopolyJsonFunc(
192087  sqlite3_context *context,
192088  int argc,
192089  sqlite3_value **argv
192090){
192091  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
192092  if( p ){
192093    sqlite3 *db = sqlite3_context_db_handle(context);
192094    sqlite3_str *x = sqlite3_str_new(db);
192095    int i;
192096    sqlite3_str_append(x, "[", 1);
192097    for(i=0; i<p->nVertex; i++){
192098      sqlite3_str_appendf(x, "[%!g,%!g],", GeoX(p,i), GeoY(p,i));
192099    }
192100    sqlite3_str_appendf(x, "[%!g,%!g]]", GeoX(p,0), GeoY(p,0));
192101    sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
192102    sqlite3_free(p);
192103  }
192104}
192105
192106/*
192107** SQL function:     geopoly_svg(X, ....)
192108**
192109** Interpret X as a polygon and render it as a SVG <polyline>.
192110** Additional arguments are added as attributes to the <polyline>.
192111*/
192112static void geopolySvgFunc(
192113  sqlite3_context *context,
192114  int argc,
192115  sqlite3_value **argv
192116){
192117  GeoPoly *p;
192118  if( argc<1 ) return;
192119  p = geopolyFuncParam(context, argv[0], 0);
192120  if( p ){
192121    sqlite3 *db = sqlite3_context_db_handle(context);
192122    sqlite3_str *x = sqlite3_str_new(db);
192123    int i;
192124    char cSep = '\'';
192125    sqlite3_str_appendf(x, "<polyline points=");
192126    for(i=0; i<p->nVertex; i++){
192127      sqlite3_str_appendf(x, "%c%g,%g", cSep, GeoX(p,i), GeoY(p,i));
192128      cSep = ' ';
192129    }
192130    sqlite3_str_appendf(x, " %g,%g'", GeoX(p,0), GeoY(p,0));
192131    for(i=1; i<argc; i++){
192132      const char *z = (const char*)sqlite3_value_text(argv[i]);
192133      if( z && z[0] ){
192134        sqlite3_str_appendf(x, " %s", z);
192135      }
192136    }
192137    sqlite3_str_appendf(x, "></polyline>");
192138    sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
192139    sqlite3_free(p);
192140  }
192141}
192142
192143/*
192144** SQL Function:      geopoly_xform(poly, A, B, C, D, E, F)
192145**
192146** Transform and/or translate a polygon as follows:
192147**
192148**      x1 = A*x0 + B*y0 + E
192149**      y1 = C*x0 + D*y0 + F
192150**
192151** For a translation:
192152**
192153**      geopoly_xform(poly, 1, 0, 0, 1, x-offset, y-offset)
192154**
192155** Rotate by R around the point (0,0):
192156**
192157**      geopoly_xform(poly, cos(R), sin(R), -sin(R), cos(R), 0, 0)
192158*/
192159static void geopolyXformFunc(
192160  sqlite3_context *context,
192161  int argc,
192162  sqlite3_value **argv
192163){
192164  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
192165  double A = sqlite3_value_double(argv[1]);
192166  double B = sqlite3_value_double(argv[2]);
192167  double C = sqlite3_value_double(argv[3]);
192168  double D = sqlite3_value_double(argv[4]);
192169  double E = sqlite3_value_double(argv[5]);
192170  double F = sqlite3_value_double(argv[6]);
192171  GeoCoord x1, y1, x0, y0;
192172  int ii;
192173  if( p ){
192174    for(ii=0; ii<p->nVertex; ii++){
192175      x0 = GeoX(p,ii);
192176      y0 = GeoY(p,ii);
192177      x1 = (GeoCoord)(A*x0 + B*y0 + E);
192178      y1 = (GeoCoord)(C*x0 + D*y0 + F);
192179      GeoX(p,ii) = x1;
192180      GeoY(p,ii) = y1;
192181    }
192182    sqlite3_result_blob(context, p->hdr,
192183       4+8*p->nVertex, SQLITE_TRANSIENT);
192184    sqlite3_free(p);
192185  }
192186}
192187
192188/*
192189** Compute the area enclosed by the polygon.
192190**
192191** This routine can also be used to detect polygons that rotate in
192192** the wrong direction.  Polygons are suppose to be counter-clockwise (CCW).
192193** This routine returns a negative value for clockwise (CW) polygons.
192194*/
192195static double geopolyArea(GeoPoly *p){
192196  double rArea = 0.0;
192197  int ii;
192198  for(ii=0; ii<p->nVertex-1; ii++){
192199    rArea += (GeoX(p,ii) - GeoX(p,ii+1))           /* (x0 - x1) */
192200              * (GeoY(p,ii) + GeoY(p,ii+1))        /* (y0 + y1) */
192201              * 0.5;
192202  }
192203  rArea += (GeoX(p,ii) - GeoX(p,0))                /* (xN - x0) */
192204           * (GeoY(p,ii) + GeoY(p,0))              /* (yN + y0) */
192205           * 0.5;
192206  return rArea;
192207}
192208
192209/*
192210** Implementation of the geopoly_area(X) function.
192211**
192212** If the input is a well-formed Geopoly BLOB then return the area
192213** enclosed by the polygon.  If the polygon circulates clockwise instead
192214** of counterclockwise (as it should) then return the negative of the
192215** enclosed area.  Otherwise return NULL.
192216*/
192217static void geopolyAreaFunc(
192218  sqlite3_context *context,
192219  int argc,
192220  sqlite3_value **argv
192221){
192222  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
192223  if( p ){
192224    sqlite3_result_double(context, geopolyArea(p));
192225    sqlite3_free(p);
192226  }
192227}
192228
192229/*
192230** Implementation of the geopoly_ccw(X) function.
192231**
192232** If the rotation of polygon X is clockwise (incorrect) instead of
192233** counter-clockwise (the correct winding order according to RFC7946)
192234** then reverse the order of the vertexes in polygon X.
192235**
192236** In other words, this routine returns a CCW polygon regardless of the
192237** winding order of its input.
192238**
192239** Use this routine to sanitize historical inputs that that sometimes
192240** contain polygons that wind in the wrong direction.
192241*/
192242static void geopolyCcwFunc(
192243  sqlite3_context *context,
192244  int argc,
192245  sqlite3_value **argv
192246){
192247  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
192248  if( p ){
192249    if( geopolyArea(p)<0.0 ){
192250      int ii, jj;
192251      for(ii=1, jj=p->nVertex-1; ii<jj; ii++, jj--){
192252        GeoCoord t = GeoX(p,ii);
192253        GeoX(p,ii) = GeoX(p,jj);
192254        GeoX(p,jj) = t;
192255        t = GeoY(p,ii);
192256        GeoY(p,ii) = GeoY(p,jj);
192257        GeoY(p,jj) = t;
192258      }
192259    }
192260    sqlite3_result_blob(context, p->hdr,
192261       4+8*p->nVertex, SQLITE_TRANSIENT);
192262    sqlite3_free(p);
192263  }
192264}
192265
192266#define GEOPOLY_PI 3.1415926535897932385
192267
192268/* Fast approximation for sine(X) for X between -0.5*pi and 2*pi
192269*/
192270static double geopolySine(double r){
192271  assert( r>=-0.5*GEOPOLY_PI && r<=2.0*GEOPOLY_PI );
192272  if( r>=1.5*GEOPOLY_PI ){
192273    r -= 2.0*GEOPOLY_PI;
192274  }
192275  if( r>=0.5*GEOPOLY_PI ){
192276    return -geopolySine(r-GEOPOLY_PI);
192277  }else{
192278    double r2 = r*r;
192279    double r3 = r2*r;
192280    double r5 = r3*r2;
192281    return 0.9996949*r - 0.1656700*r3 + 0.0075134*r5;
192282  }
192283}
192284
192285/*
192286** Function:   geopoly_regular(X,Y,R,N)
192287**
192288** Construct a simple, convex, regular polygon centered at X, Y
192289** with circumradius R and with N sides.
192290*/
192291static void geopolyRegularFunc(
192292  sqlite3_context *context,
192293  int argc,
192294  sqlite3_value **argv
192295){
192296  double x = sqlite3_value_double(argv[0]);
192297  double y = sqlite3_value_double(argv[1]);
192298  double r = sqlite3_value_double(argv[2]);
192299  int n = sqlite3_value_int(argv[3]);
192300  int i;
192301  GeoPoly *p;
192302
192303  if( n<3 || r<=0.0 ) return;
192304  if( n>1000 ) n = 1000;
192305  p = sqlite3_malloc64( sizeof(*p) + (n-1)*2*sizeof(GeoCoord) );
192306  if( p==0 ){
192307    sqlite3_result_error_nomem(context);
192308    return;
192309  }
192310  i = 1;
192311  p->hdr[0] = *(unsigned char*)&i;
192312  p->hdr[1] = 0;
192313  p->hdr[2] = (n>>8)&0xff;
192314  p->hdr[3] = n&0xff;
192315  for(i=0; i<n; i++){
192316    double rAngle = 2.0*GEOPOLY_PI*i/n;
192317    GeoX(p,i) = x - r*geopolySine(rAngle-0.5*GEOPOLY_PI);
192318    GeoY(p,i) = y + r*geopolySine(rAngle);
192319  }
192320  sqlite3_result_blob(context, p->hdr, 4+8*n, SQLITE_TRANSIENT);
192321  sqlite3_free(p);
192322}
192323
192324/*
192325** If pPoly is a polygon, compute its bounding box. Then:
192326**
192327**    (1) if aCoord!=0 store the bounding box in aCoord, returning NULL
192328**    (2) otherwise, compute a GeoPoly for the bounding box and return the
192329**        new GeoPoly
192330**
192331** If pPoly is NULL but aCoord is not NULL, then compute a new GeoPoly from
192332** the bounding box in aCoord and return a pointer to that GeoPoly.
192333*/
192334static GeoPoly *geopolyBBox(
192335  sqlite3_context *context,   /* For recording the error */
192336  sqlite3_value *pPoly,       /* The polygon */
192337  RtreeCoord *aCoord,         /* Results here */
192338  int *pRc                    /* Error code here */
192339){
192340  GeoPoly *pOut = 0;
192341  GeoPoly *p;
192342  float mnX, mxX, mnY, mxY;
192343  if( pPoly==0 && aCoord!=0 ){
192344    p = 0;
192345    mnX = aCoord[0].f;
192346    mxX = aCoord[1].f;
192347    mnY = aCoord[2].f;
192348    mxY = aCoord[3].f;
192349    goto geopolyBboxFill;
192350  }else{
192351    p = geopolyFuncParam(context, pPoly, pRc);
192352  }
192353  if( p ){
192354    int ii;
192355    mnX = mxX = GeoX(p,0);
192356    mnY = mxY = GeoY(p,0);
192357    for(ii=1; ii<p->nVertex; ii++){
192358      double r = GeoX(p,ii);
192359      if( r<mnX ) mnX = (float)r;
192360      else if( r>mxX ) mxX = (float)r;
192361      r = GeoY(p,ii);
192362      if( r<mnY ) mnY = (float)r;
192363      else if( r>mxY ) mxY = (float)r;
192364    }
192365    if( pRc ) *pRc = SQLITE_OK;
192366    if( aCoord==0 ){
192367      geopolyBboxFill:
192368      pOut = sqlite3_realloc64(p, GEOPOLY_SZ(4));
192369      if( pOut==0 ){
192370        sqlite3_free(p);
192371        if( context ) sqlite3_result_error_nomem(context);
192372        if( pRc ) *pRc = SQLITE_NOMEM;
192373        return 0;
192374      }
192375      pOut->nVertex = 4;
192376      ii = 1;
192377      pOut->hdr[0] = *(unsigned char*)&ii;
192378      pOut->hdr[1] = 0;
192379      pOut->hdr[2] = 0;
192380      pOut->hdr[3] = 4;
192381      GeoX(pOut,0) = mnX;
192382      GeoY(pOut,0) = mnY;
192383      GeoX(pOut,1) = mxX;
192384      GeoY(pOut,1) = mnY;
192385      GeoX(pOut,2) = mxX;
192386      GeoY(pOut,2) = mxY;
192387      GeoX(pOut,3) = mnX;
192388      GeoY(pOut,3) = mxY;
192389    }else{
192390      sqlite3_free(p);
192391      aCoord[0].f = mnX;
192392      aCoord[1].f = mxX;
192393      aCoord[2].f = mnY;
192394      aCoord[3].f = mxY;
192395    }
192396  }
192397  return pOut;
192398}
192399
192400/*
192401** Implementation of the geopoly_bbox(X) SQL function.
192402*/
192403static void geopolyBBoxFunc(
192404  sqlite3_context *context,
192405  int argc,
192406  sqlite3_value **argv
192407){
192408  GeoPoly *p = geopolyBBox(context, argv[0], 0, 0);
192409  if( p ){
192410    sqlite3_result_blob(context, p->hdr,
192411       4+8*p->nVertex, SQLITE_TRANSIENT);
192412    sqlite3_free(p);
192413  }
192414}
192415
192416/*
192417** State vector for the geopoly_group_bbox() aggregate function.
192418*/
192419typedef struct GeoBBox GeoBBox;
192420struct GeoBBox {
192421  int isInit;
192422  RtreeCoord a[4];
192423};
192424
192425
192426/*
192427** Implementation of the geopoly_group_bbox(X) aggregate SQL function.
192428*/
192429static void geopolyBBoxStep(
192430  sqlite3_context *context,
192431  int argc,
192432  sqlite3_value **argv
192433){
192434  RtreeCoord a[4];
192435  int rc = SQLITE_OK;
192436  (void)geopolyBBox(context, argv[0], a, &rc);
192437  if( rc==SQLITE_OK ){
192438    GeoBBox *pBBox;
192439    pBBox = (GeoBBox*)sqlite3_aggregate_context(context, sizeof(*pBBox));
192440    if( pBBox==0 ) return;
192441    if( pBBox->isInit==0 ){
192442      pBBox->isInit = 1;
192443      memcpy(pBBox->a, a, sizeof(RtreeCoord)*4);
192444    }else{
192445      if( a[0].f < pBBox->a[0].f ) pBBox->a[0] = a[0];
192446      if( a[1].f > pBBox->a[1].f ) pBBox->a[1] = a[1];
192447      if( a[2].f < pBBox->a[2].f ) pBBox->a[2] = a[2];
192448      if( a[3].f > pBBox->a[3].f ) pBBox->a[3] = a[3];
192449    }
192450  }
192451}
192452static void geopolyBBoxFinal(
192453  sqlite3_context *context
192454){
192455  GeoPoly *p;
192456  GeoBBox *pBBox;
192457  pBBox = (GeoBBox*)sqlite3_aggregate_context(context, 0);
192458  if( pBBox==0 ) return;
192459  p = geopolyBBox(context, 0, pBBox->a, 0);
192460  if( p ){
192461    sqlite3_result_blob(context, p->hdr,
192462       4+8*p->nVertex, SQLITE_TRANSIENT);
192463    sqlite3_free(p);
192464  }
192465}
192466
192467
192468/*
192469** Determine if point (x0,y0) is beneath line segment (x1,y1)->(x2,y2).
192470** Returns:
192471**
192472**    +2  x0,y0 is on the line segement
192473**
192474**    +1  x0,y0 is beneath line segment
192475**
192476**    0   x0,y0 is not on or beneath the line segment or the line segment
192477**        is vertical and x0,y0 is not on the line segment
192478**
192479** The left-most coordinate min(x1,x2) is not considered to be part of
192480** the line segment for the purposes of this analysis.
192481*/
192482static int pointBeneathLine(
192483  double x0, double y0,
192484  double x1, double y1,
192485  double x2, double y2
192486){
192487  double y;
192488  if( x0==x1 && y0==y1 ) return 2;
192489  if( x1<x2 ){
192490    if( x0<=x1 || x0>x2 ) return 0;
192491  }else if( x1>x2 ){
192492    if( x0<=x2 || x0>x1 ) return 0;
192493  }else{
192494    /* Vertical line segment */
192495    if( x0!=x1 ) return 0;
192496    if( y0<y1 && y0<y2 ) return 0;
192497    if( y0>y1 && y0>y2 ) return 0;
192498    return 2;
192499  }
192500  y = y1 + (y2-y1)*(x0-x1)/(x2-x1);
192501  if( y0==y ) return 2;
192502  if( y0<y ) return 1;
192503  return 0;
192504}
192505
192506/*
192507** SQL function:    geopoly_contains_point(P,X,Y)
192508**
192509** Return +2 if point X,Y is within polygon P.
192510** Return +1 if point X,Y is on the polygon boundary.
192511** Return 0 if point X,Y is outside the polygon
192512*/
192513static void geopolyContainsPointFunc(
192514  sqlite3_context *context,
192515  int argc,
192516  sqlite3_value **argv
192517){
192518  GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
192519  double x0 = sqlite3_value_double(argv[1]);
192520  double y0 = sqlite3_value_double(argv[2]);
192521  int v = 0;
192522  int cnt = 0;
192523  int ii;
192524  if( p1==0 ) return;
192525  for(ii=0; ii<p1->nVertex-1; ii++){
192526    v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
192527                               GeoX(p1,ii+1),GeoY(p1,ii+1));
192528    if( v==2 ) break;
192529    cnt += v;
192530  }
192531  if( v!=2 ){
192532    v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
192533                               GeoX(p1,0),  GeoY(p1,0));
192534  }
192535  if( v==2 ){
192536    sqlite3_result_int(context, 1);
192537  }else if( ((v+cnt)&1)==0 ){
192538    sqlite3_result_int(context, 0);
192539  }else{
192540    sqlite3_result_int(context, 2);
192541  }
192542  sqlite3_free(p1);
192543}
192544
192545/* Forward declaration */
192546static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2);
192547
192548/*
192549** SQL function:    geopoly_within(P1,P2)
192550**
192551** Return +2 if P1 and P2 are the same polygon
192552** Return +1 if P2 is contained within P1
192553** Return 0 if any part of P2 is on the outside of P1
192554**
192555*/
192556static void geopolyWithinFunc(
192557  sqlite3_context *context,
192558  int argc,
192559  sqlite3_value **argv
192560){
192561  GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
192562  GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
192563  if( p1 && p2 ){
192564    int x = geopolyOverlap(p1, p2);
192565    if( x<0 ){
192566      sqlite3_result_error_nomem(context);
192567    }else{
192568      sqlite3_result_int(context, x==2 ? 1 : x==4 ? 2 : 0);
192569    }
192570  }
192571  sqlite3_free(p1);
192572  sqlite3_free(p2);
192573}
192574
192575/* Objects used by the overlap algorihm. */
192576typedef struct GeoEvent GeoEvent;
192577typedef struct GeoSegment GeoSegment;
192578typedef struct GeoOverlap GeoOverlap;
192579struct GeoEvent {
192580  double x;              /* X coordinate at which event occurs */
192581  int eType;             /* 0 for ADD, 1 for REMOVE */
192582  GeoSegment *pSeg;      /* The segment to be added or removed */
192583  GeoEvent *pNext;       /* Next event in the sorted list */
192584};
192585struct GeoSegment {
192586  double C, B;           /* y = C*x + B */
192587  double y;              /* Current y value */
192588  float y0;              /* Initial y value */
192589  unsigned char side;    /* 1 for p1, 2 for p2 */
192590  unsigned int idx;      /* Which segment within the side */
192591  GeoSegment *pNext;     /* Next segment in a list sorted by y */
192592};
192593struct GeoOverlap {
192594  GeoEvent *aEvent;          /* Array of all events */
192595  GeoSegment *aSegment;      /* Array of all segments */
192596  int nEvent;                /* Number of events */
192597  int nSegment;              /* Number of segments */
192598};
192599
192600/*
192601** Add a single segment and its associated events.
192602*/
192603static void geopolyAddOneSegment(
192604  GeoOverlap *p,
192605  GeoCoord x0,
192606  GeoCoord y0,
192607  GeoCoord x1,
192608  GeoCoord y1,
192609  unsigned char side,
192610  unsigned int idx
192611){
192612  GeoSegment *pSeg;
192613  GeoEvent *pEvent;
192614  if( x0==x1 ) return;  /* Ignore vertical segments */
192615  if( x0>x1 ){
192616    GeoCoord t = x0;
192617    x0 = x1;
192618    x1 = t;
192619    t = y0;
192620    y0 = y1;
192621    y1 = t;
192622  }
192623  pSeg = p->aSegment + p->nSegment;
192624  p->nSegment++;
192625  pSeg->C = (y1-y0)/(x1-x0);
192626  pSeg->B = y1 - x1*pSeg->C;
192627  pSeg->y0 = y0;
192628  pSeg->side = side;
192629  pSeg->idx = idx;
192630  pEvent = p->aEvent + p->nEvent;
192631  p->nEvent++;
192632  pEvent->x = x0;
192633  pEvent->eType = 0;
192634  pEvent->pSeg = pSeg;
192635  pEvent = p->aEvent + p->nEvent;
192636  p->nEvent++;
192637  pEvent->x = x1;
192638  pEvent->eType = 1;
192639  pEvent->pSeg = pSeg;
192640}
192641
192642
192643
192644/*
192645** Insert all segments and events for polygon pPoly.
192646*/
192647static void geopolyAddSegments(
192648  GeoOverlap *p,          /* Add segments to this Overlap object */
192649  GeoPoly *pPoly,         /* Take all segments from this polygon */
192650  unsigned char side      /* The side of pPoly */
192651){
192652  unsigned int i;
192653  GeoCoord *x;
192654  for(i=0; i<(unsigned)pPoly->nVertex-1; i++){
192655    x = &GeoX(pPoly,i);
192656    geopolyAddOneSegment(p, x[0], x[1], x[2], x[3], side, i);
192657  }
192658  x = &GeoX(pPoly,i);
192659  geopolyAddOneSegment(p, x[0], x[1], pPoly->a[0], pPoly->a[1], side, i);
192660}
192661
192662/*
192663** Merge two lists of sorted events by X coordinate
192664*/
192665static GeoEvent *geopolyEventMerge(GeoEvent *pLeft, GeoEvent *pRight){
192666  GeoEvent head, *pLast;
192667  head.pNext = 0;
192668  pLast = &head;
192669  while( pRight && pLeft ){
192670    if( pRight->x <= pLeft->x ){
192671      pLast->pNext = pRight;
192672      pLast = pRight;
192673      pRight = pRight->pNext;
192674    }else{
192675      pLast->pNext = pLeft;
192676      pLast = pLeft;
192677      pLeft = pLeft->pNext;
192678    }
192679  }
192680  pLast->pNext = pRight ? pRight : pLeft;
192681  return head.pNext;
192682}
192683
192684/*
192685** Sort an array of nEvent event objects into a list.
192686*/
192687static GeoEvent *geopolySortEventsByX(GeoEvent *aEvent, int nEvent){
192688  int mx = 0;
192689  int i, j;
192690  GeoEvent *p;
192691  GeoEvent *a[50];
192692  for(i=0; i<nEvent; i++){
192693    p = &aEvent[i];
192694    p->pNext = 0;
192695    for(j=0; j<mx && a[j]; j++){
192696      p = geopolyEventMerge(a[j], p);
192697      a[j] = 0;
192698    }
192699    a[j] = p;
192700    if( j>=mx ) mx = j+1;
192701  }
192702  p = 0;
192703  for(i=0; i<mx; i++){
192704    p = geopolyEventMerge(a[i], p);
192705  }
192706  return p;
192707}
192708
192709/*
192710** Merge two lists of sorted segments by Y, and then by C.
192711*/
192712static GeoSegment *geopolySegmentMerge(GeoSegment *pLeft, GeoSegment *pRight){
192713  GeoSegment head, *pLast;
192714  head.pNext = 0;
192715  pLast = &head;
192716  while( pRight && pLeft ){
192717    double r = pRight->y - pLeft->y;
192718    if( r==0.0 ) r = pRight->C - pLeft->C;
192719    if( r<0.0 ){
192720      pLast->pNext = pRight;
192721      pLast = pRight;
192722      pRight = pRight->pNext;
192723    }else{
192724      pLast->pNext = pLeft;
192725      pLast = pLeft;
192726      pLeft = pLeft->pNext;
192727    }
192728  }
192729  pLast->pNext = pRight ? pRight : pLeft;
192730  return head.pNext;
192731}
192732
192733/*
192734** Sort a list of GeoSegments in order of increasing Y and in the event of
192735** a tie, increasing C (slope).
192736*/
192737static GeoSegment *geopolySortSegmentsByYAndC(GeoSegment *pList){
192738  int mx = 0;
192739  int i;
192740  GeoSegment *p;
192741  GeoSegment *a[50];
192742  while( pList ){
192743    p = pList;
192744    pList = pList->pNext;
192745    p->pNext = 0;
192746    for(i=0; i<mx && a[i]; i++){
192747      p = geopolySegmentMerge(a[i], p);
192748      a[i] = 0;
192749    }
192750    a[i] = p;
192751    if( i>=mx ) mx = i+1;
192752  }
192753  p = 0;
192754  for(i=0; i<mx; i++){
192755    p = geopolySegmentMerge(a[i], p);
192756  }
192757  return p;
192758}
192759
192760/*
192761** Determine the overlap between two polygons
192762*/
192763static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2){
192764  sqlite3_int64 nVertex = p1->nVertex + p2->nVertex + 2;
192765  GeoOverlap *p;
192766  sqlite3_int64 nByte;
192767  GeoEvent *pThisEvent;
192768  double rX;
192769  int rc = 0;
192770  int needSort = 0;
192771  GeoSegment *pActive = 0;
192772  GeoSegment *pSeg;
192773  unsigned char aOverlap[4];
192774
192775  nByte = sizeof(GeoEvent)*nVertex*2
192776           + sizeof(GeoSegment)*nVertex
192777           + sizeof(GeoOverlap);
192778  p = sqlite3_malloc64( nByte );
192779  if( p==0 ) return -1;
192780  p->aEvent = (GeoEvent*)&p[1];
192781  p->aSegment = (GeoSegment*)&p->aEvent[nVertex*2];
192782  p->nEvent = p->nSegment = 0;
192783  geopolyAddSegments(p, p1, 1);
192784  geopolyAddSegments(p, p2, 2);
192785  pThisEvent = geopolySortEventsByX(p->aEvent, p->nEvent);
192786  rX = pThisEvent->x==0.0 ? -1.0 : 0.0;
192787  memset(aOverlap, 0, sizeof(aOverlap));
192788  while( pThisEvent ){
192789    if( pThisEvent->x!=rX ){
192790      GeoSegment *pPrev = 0;
192791      int iMask = 0;
192792      GEODEBUG(("Distinct X: %g\n", pThisEvent->x));
192793      rX = pThisEvent->x;
192794      if( needSort ){
192795        GEODEBUG(("SORT\n"));
192796        pActive = geopolySortSegmentsByYAndC(pActive);
192797        needSort = 0;
192798      }
192799      for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
192800        if( pPrev ){
192801          if( pPrev->y!=pSeg->y ){
192802            GEODEBUG(("MASK: %d\n", iMask));
192803            aOverlap[iMask] = 1;
192804          }
192805        }
192806        iMask ^= pSeg->side;
192807        pPrev = pSeg;
192808      }
192809      pPrev = 0;
192810      for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
192811        double y = pSeg->C*rX + pSeg->B;
192812        GEODEBUG(("Segment %d.%d %g->%g\n", pSeg->side, pSeg->idx, pSeg->y, y));
192813        pSeg->y = y;
192814        if( pPrev ){
192815          if( pPrev->y>pSeg->y && pPrev->side!=pSeg->side ){
192816            rc = 1;
192817            GEODEBUG(("Crossing: %d.%d and %d.%d\n",
192818                    pPrev->side, pPrev->idx,
192819                    pSeg->side, pSeg->idx));
192820            goto geopolyOverlapDone;
192821          }else if( pPrev->y!=pSeg->y ){
192822            GEODEBUG(("MASK: %d\n", iMask));
192823            aOverlap[iMask] = 1;
192824          }
192825        }
192826        iMask ^= pSeg->side;
192827        pPrev = pSeg;
192828      }
192829    }
192830    GEODEBUG(("%s %d.%d C=%g B=%g\n",
192831      pThisEvent->eType ? "RM " : "ADD",
192832      pThisEvent->pSeg->side, pThisEvent->pSeg->idx,
192833      pThisEvent->pSeg->C,
192834      pThisEvent->pSeg->B));
192835    if( pThisEvent->eType==0 ){
192836      /* Add a segment */
192837      pSeg = pThisEvent->pSeg;
192838      pSeg->y = pSeg->y0;
192839      pSeg->pNext = pActive;
192840      pActive = pSeg;
192841      needSort = 1;
192842    }else{
192843      /* Remove a segment */
192844      if( pActive==pThisEvent->pSeg ){
192845        pActive = pActive->pNext;
192846      }else{
192847        for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
192848          if( pSeg->pNext==pThisEvent->pSeg ){
192849            pSeg->pNext = pSeg->pNext->pNext;
192850            break;
192851          }
192852        }
192853      }
192854    }
192855    pThisEvent = pThisEvent->pNext;
192856  }
192857  if( aOverlap[3]==0 ){
192858    rc = 0;
192859  }else if( aOverlap[1]!=0 && aOverlap[2]==0 ){
192860    rc = 3;
192861  }else if( aOverlap[1]==0 && aOverlap[2]!=0 ){
192862    rc = 2;
192863  }else if( aOverlap[1]==0 && aOverlap[2]==0 ){
192864    rc = 4;
192865  }else{
192866    rc = 1;
192867  }
192868
192869geopolyOverlapDone:
192870  sqlite3_free(p);
192871  return rc;
192872}
192873
192874/*
192875** SQL function:    geopoly_overlap(P1,P2)
192876**
192877** Determine whether or not P1 and P2 overlap. Return value:
192878**
192879**   0     The two polygons are disjoint
192880**   1     They overlap
192881**   2     P1 is completely contained within P2
192882**   3     P2 is completely contained within P1
192883**   4     P1 and P2 are the same polygon
192884**   NULL  Either P1 or P2 or both are not valid polygons
192885*/
192886static void geopolyOverlapFunc(
192887  sqlite3_context *context,
192888  int argc,
192889  sqlite3_value **argv
192890){
192891  GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
192892  GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
192893  if( p1 && p2 ){
192894    int x = geopolyOverlap(p1, p2);
192895    if( x<0 ){
192896      sqlite3_result_error_nomem(context);
192897    }else{
192898      sqlite3_result_int(context, x);
192899    }
192900  }
192901  sqlite3_free(p1);
192902  sqlite3_free(p2);
192903}
192904
192905/*
192906** Enable or disable debugging output
192907*/
192908static void geopolyDebugFunc(
192909  sqlite3_context *context,
192910  int argc,
192911  sqlite3_value **argv
192912){
192913#ifdef GEOPOLY_ENABLE_DEBUG
192914  geo_debug = sqlite3_value_int(argv[0]);
192915#endif
192916}
192917
192918/*
192919** This function is the implementation of both the xConnect and xCreate
192920** methods of the geopoly virtual table.
192921**
192922**   argv[0]   -> module name
192923**   argv[1]   -> database name
192924**   argv[2]   -> table name
192925**   argv[...] -> column names...
192926*/
192927static int geopolyInit(
192928  sqlite3 *db,                        /* Database connection */
192929  void *pAux,                         /* One of the RTREE_COORD_* constants */
192930  int argc, const char *const*argv,   /* Parameters to CREATE TABLE statement */
192931  sqlite3_vtab **ppVtab,              /* OUT: New virtual table */
192932  char **pzErr,                       /* OUT: Error message, if any */
192933  int isCreate                        /* True for xCreate, false for xConnect */
192934){
192935  int rc = SQLITE_OK;
192936  Rtree *pRtree;
192937  sqlite3_int64 nDb;              /* Length of string argv[1] */
192938  sqlite3_int64 nName;            /* Length of string argv[2] */
192939  sqlite3_str *pSql;
192940  char *zSql;
192941  int ii;
192942
192943  sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
192944
192945  /* Allocate the sqlite3_vtab structure */
192946  nDb = strlen(argv[1]);
192947  nName = strlen(argv[2]);
192948  pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName+2);
192949  if( !pRtree ){
192950    return SQLITE_NOMEM;
192951  }
192952  memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
192953  pRtree->nBusy = 1;
192954  pRtree->base.pModule = &rtreeModule;
192955  pRtree->zDb = (char *)&pRtree[1];
192956  pRtree->zName = &pRtree->zDb[nDb+1];
192957  pRtree->eCoordType = RTREE_COORD_REAL32;
192958  pRtree->nDim = 2;
192959  pRtree->nDim2 = 4;
192960  memcpy(pRtree->zDb, argv[1], nDb);
192961  memcpy(pRtree->zName, argv[2], nName);
192962
192963
192964  /* Create/Connect to the underlying relational database schema. If
192965  ** that is successful, call sqlite3_declare_vtab() to configure
192966  ** the r-tree table schema.
192967  */
192968  pSql = sqlite3_str_new(db);
192969  sqlite3_str_appendf(pSql, "CREATE TABLE x(_shape");
192970  pRtree->nAux = 1;         /* Add one for _shape */
192971  pRtree->nAuxNotNull = 1;  /* The _shape column is always not-null */
192972  for(ii=3; ii<argc; ii++){
192973    pRtree->nAux++;
192974    sqlite3_str_appendf(pSql, ",%s", argv[ii]);
192975  }
192976  sqlite3_str_appendf(pSql, ");");
192977  zSql = sqlite3_str_finish(pSql);
192978  if( !zSql ){
192979    rc = SQLITE_NOMEM;
192980  }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
192981    *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
192982  }
192983  sqlite3_free(zSql);
192984  if( rc ) goto geopolyInit_fail;
192985  pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
192986
192987  /* Figure out the node size to use. */
192988  rc = getNodeSize(db, pRtree, isCreate, pzErr);
192989  if( rc ) goto geopolyInit_fail;
192990  rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
192991  if( rc ){
192992    *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
192993    goto geopolyInit_fail;
192994  }
192995
192996  *ppVtab = (sqlite3_vtab *)pRtree;
192997  return SQLITE_OK;
192998
192999geopolyInit_fail:
193000  if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
193001  assert( *ppVtab==0 );
193002  assert( pRtree->nBusy==1 );
193003  rtreeRelease(pRtree);
193004  return rc;
193005}
193006
193007
193008/*
193009** GEOPOLY virtual table module xCreate method.
193010*/
193011static int geopolyCreate(
193012  sqlite3 *db,
193013  void *pAux,
193014  int argc, const char *const*argv,
193015  sqlite3_vtab **ppVtab,
193016  char **pzErr
193017){
193018  return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
193019}
193020
193021/*
193022** GEOPOLY virtual table module xConnect method.
193023*/
193024static int geopolyConnect(
193025  sqlite3 *db,
193026  void *pAux,
193027  int argc, const char *const*argv,
193028  sqlite3_vtab **ppVtab,
193029  char **pzErr
193030){
193031  return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
193032}
193033
193034
193035/*
193036** GEOPOLY virtual table module xFilter method.
193037**
193038** Query plans:
193039**
193040**      1         rowid lookup
193041**      2         search for objects overlapping the same bounding box
193042**                that contains polygon argv[0]
193043**      3         search for objects overlapping the same bounding box
193044**                that contains polygon argv[0]
193045**      4         full table scan
193046*/
193047static int geopolyFilter(
193048  sqlite3_vtab_cursor *pVtabCursor,     /* The cursor to initialize */
193049  int idxNum,                           /* Query plan */
193050  const char *idxStr,                   /* Not Used */
193051  int argc, sqlite3_value **argv        /* Parameters to the query plan */
193052){
193053  Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
193054  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
193055  RtreeNode *pRoot = 0;
193056  int rc = SQLITE_OK;
193057  int iCell = 0;
193058
193059  rtreeReference(pRtree);
193060
193061  /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
193062  resetCursor(pCsr);
193063
193064  pCsr->iStrategy = idxNum;
193065  if( idxNum==1 ){
193066    /* Special case - lookup by rowid. */
193067    RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
193068    RtreeSearchPoint *p;     /* Search point for the leaf */
193069    i64 iRowid = sqlite3_value_int64(argv[0]);
193070    i64 iNode = 0;
193071    rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
193072    if( rc==SQLITE_OK && pLeaf!=0 ){
193073      p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
193074      assert( p!=0 );  /* Always returns pCsr->sPoint */
193075      pCsr->aNode[0] = pLeaf;
193076      p->id = iNode;
193077      p->eWithin = PARTLY_WITHIN;
193078      rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
193079      p->iCell = (u8)iCell;
193080      RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
193081    }else{
193082      pCsr->atEOF = 1;
193083    }
193084  }else{
193085    /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
193086    ** with the configured constraints.
193087    */
193088    rc = nodeAcquire(pRtree, 1, 0, &pRoot);
193089    if( rc==SQLITE_OK && idxNum<=3 ){
193090      RtreeCoord bbox[4];
193091      RtreeConstraint *p;
193092      assert( argc==1 );
193093      geopolyBBox(0, argv[0], bbox, &rc);
193094      if( rc ){
193095        goto geopoly_filter_end;
193096      }
193097      pCsr->aConstraint = p = sqlite3_malloc(sizeof(RtreeConstraint)*4);
193098      pCsr->nConstraint = 4;
193099      if( p==0 ){
193100        rc = SQLITE_NOMEM;
193101      }else{
193102        memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*4);
193103        memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
193104        if( idxNum==2 ){
193105          /* Overlap query */
193106          p->op = 'B';
193107          p->iCoord = 0;
193108          p->u.rValue = bbox[1].f;
193109          p++;
193110          p->op = 'D';
193111          p->iCoord = 1;
193112          p->u.rValue = bbox[0].f;
193113          p++;
193114          p->op = 'B';
193115          p->iCoord = 2;
193116          p->u.rValue = bbox[3].f;
193117          p++;
193118          p->op = 'D';
193119          p->iCoord = 3;
193120          p->u.rValue = bbox[2].f;
193121        }else{
193122          /* Within query */
193123          p->op = 'D';
193124          p->iCoord = 0;
193125          p->u.rValue = bbox[0].f;
193126          p++;
193127          p->op = 'B';
193128          p->iCoord = 1;
193129          p->u.rValue = bbox[1].f;
193130          p++;
193131          p->op = 'D';
193132          p->iCoord = 2;
193133          p->u.rValue = bbox[2].f;
193134          p++;
193135          p->op = 'B';
193136          p->iCoord = 3;
193137          p->u.rValue = bbox[3].f;
193138        }
193139      }
193140    }
193141    if( rc==SQLITE_OK ){
193142      RtreeSearchPoint *pNew;
193143      pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
193144      if( pNew==0 ){
193145        rc = SQLITE_NOMEM;
193146        goto geopoly_filter_end;
193147      }
193148      pNew->id = 1;
193149      pNew->iCell = 0;
193150      pNew->eWithin = PARTLY_WITHIN;
193151      assert( pCsr->bPoint==1 );
193152      pCsr->aNode[0] = pRoot;
193153      pRoot = 0;
193154      RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
193155      rc = rtreeStepToLeaf(pCsr);
193156    }
193157  }
193158
193159geopoly_filter_end:
193160  nodeRelease(pRtree, pRoot);
193161  rtreeRelease(pRtree);
193162  return rc;
193163}
193164
193165/*
193166** Rtree virtual table module xBestIndex method. There are three
193167** table scan strategies to choose from (in order from most to
193168** least desirable):
193169**
193170**   idxNum     idxStr        Strategy
193171**   ------------------------------------------------
193172**     1        "rowid"       Direct lookup by rowid.
193173**     2        "rtree"       R-tree overlap query using geopoly_overlap()
193174**     3        "rtree"       R-tree within query using geopoly_within()
193175**     4        "fullscan"    full-table scan.
193176**   ------------------------------------------------
193177*/
193178static int geopolyBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
193179  int ii;
193180  int iRowidTerm = -1;
193181  int iFuncTerm = -1;
193182  int idxNum = 0;
193183
193184  for(ii=0; ii<pIdxInfo->nConstraint; ii++){
193185    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
193186    if( !p->usable ) continue;
193187    if( p->iColumn<0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ  ){
193188      iRowidTerm = ii;
193189      break;
193190    }
193191    if( p->iColumn==0 && p->op>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
193192      /* p->op==SQLITE_INDEX_CONSTRAINT_FUNCTION for geopoly_overlap()
193193      ** p->op==(SQLITE_INDEX_CONTRAINT_FUNCTION+1) for geopoly_within().
193194      ** See geopolyFindFunction() */
193195      iFuncTerm = ii;
193196      idxNum = p->op - SQLITE_INDEX_CONSTRAINT_FUNCTION + 2;
193197    }
193198  }
193199
193200  if( iRowidTerm>=0 ){
193201    pIdxInfo->idxNum = 1;
193202    pIdxInfo->idxStr = "rowid";
193203    pIdxInfo->aConstraintUsage[iRowidTerm].argvIndex = 1;
193204    pIdxInfo->aConstraintUsage[iRowidTerm].omit = 1;
193205    pIdxInfo->estimatedCost = 30.0;
193206    pIdxInfo->estimatedRows = 1;
193207    pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
193208    return SQLITE_OK;
193209  }
193210  if( iFuncTerm>=0 ){
193211    pIdxInfo->idxNum = idxNum;
193212    pIdxInfo->idxStr = "rtree";
193213    pIdxInfo->aConstraintUsage[iFuncTerm].argvIndex = 1;
193214    pIdxInfo->aConstraintUsage[iFuncTerm].omit = 0;
193215    pIdxInfo->estimatedCost = 300.0;
193216    pIdxInfo->estimatedRows = 10;
193217    return SQLITE_OK;
193218  }
193219  pIdxInfo->idxNum = 4;
193220  pIdxInfo->idxStr = "fullscan";
193221  pIdxInfo->estimatedCost = 3000000.0;
193222  pIdxInfo->estimatedRows = 100000;
193223  return SQLITE_OK;
193224}
193225
193226
193227/*
193228** GEOPOLY virtual table module xColumn method.
193229*/
193230static int geopolyColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
193231  Rtree *pRtree = (Rtree *)cur->pVtab;
193232  RtreeCursor *pCsr = (RtreeCursor *)cur;
193233  RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
193234  int rc = SQLITE_OK;
193235  RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
193236
193237  if( rc ) return rc;
193238  if( p==0 ) return SQLITE_OK;
193239  if( i==0 && sqlite3_vtab_nochange(ctx) ) return SQLITE_OK;
193240  if( i<=pRtree->nAux ){
193241    if( !pCsr->bAuxValid ){
193242      if( pCsr->pReadAux==0 ){
193243        rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
193244                                &pCsr->pReadAux, 0);
193245        if( rc ) return rc;
193246      }
193247      sqlite3_bind_int64(pCsr->pReadAux, 1,
193248          nodeGetRowid(pRtree, pNode, p->iCell));
193249      rc = sqlite3_step(pCsr->pReadAux);
193250      if( rc==SQLITE_ROW ){
193251        pCsr->bAuxValid = 1;
193252      }else{
193253        sqlite3_reset(pCsr->pReadAux);
193254        if( rc==SQLITE_DONE ) rc = SQLITE_OK;
193255        return rc;
193256      }
193257    }
193258    sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pReadAux, i+2));
193259  }
193260  return SQLITE_OK;
193261}
193262
193263
193264/*
193265** The xUpdate method for GEOPOLY module virtual tables.
193266**
193267** For DELETE:
193268**
193269**     argv[0] = the rowid to be deleted
193270**
193271** For INSERT:
193272**
193273**     argv[0] = SQL NULL
193274**     argv[1] = rowid to insert, or an SQL NULL to select automatically
193275**     argv[2] = _shape column
193276**     argv[3] = first application-defined column....
193277**
193278** For UPDATE:
193279**
193280**     argv[0] = rowid to modify.  Never NULL
193281**     argv[1] = rowid after the change.  Never NULL
193282**     argv[2] = new value for _shape
193283**     argv[3] = new value for first application-defined column....
193284*/
193285static int geopolyUpdate(
193286  sqlite3_vtab *pVtab,
193287  int nData,
193288  sqlite3_value **aData,
193289  sqlite_int64 *pRowid
193290){
193291  Rtree *pRtree = (Rtree *)pVtab;
193292  int rc = SQLITE_OK;
193293  RtreeCell cell;                 /* New cell to insert if nData>1 */
193294  i64 oldRowid;                   /* The old rowid */
193295  int oldRowidValid;              /* True if oldRowid is valid */
193296  i64 newRowid;                   /* The new rowid */
193297  int newRowidValid;              /* True if newRowid is valid */
193298  int coordChange = 0;            /* Change in coordinates */
193299
193300  if( pRtree->nNodeRef ){
193301    /* Unable to write to the btree while another cursor is reading from it,
193302    ** since the write might do a rebalance which would disrupt the read
193303    ** cursor. */
193304    return SQLITE_LOCKED_VTAB;
193305  }
193306  rtreeReference(pRtree);
193307  assert(nData>=1);
193308
193309  oldRowidValid = sqlite3_value_type(aData[0])!=SQLITE_NULL;;
193310  oldRowid = oldRowidValid ? sqlite3_value_int64(aData[0]) : 0;
193311  newRowidValid = nData>1 && sqlite3_value_type(aData[1])!=SQLITE_NULL;
193312  newRowid = newRowidValid ? sqlite3_value_int64(aData[1]) : 0;
193313  cell.iRowid = newRowid;
193314
193315  if( nData>1                                 /* not a DELETE */
193316   && (!oldRowidValid                         /* INSERT */
193317        || !sqlite3_value_nochange(aData[2])  /* UPDATE _shape */
193318        || oldRowid!=newRowid)                /* Rowid change */
193319  ){
193320    geopolyBBox(0, aData[2], cell.aCoord, &rc);
193321    if( rc ){
193322      if( rc==SQLITE_ERROR ){
193323        pVtab->zErrMsg =
193324          sqlite3_mprintf("_shape does not contain a valid polygon");
193325      }
193326      goto geopoly_update_end;
193327    }
193328    coordChange = 1;
193329
193330    /* If a rowid value was supplied, check if it is already present in
193331    ** the table. If so, the constraint has failed. */
193332    if( newRowidValid && (!oldRowidValid || oldRowid!=newRowid) ){
193333      int steprc;
193334      sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
193335      steprc = sqlite3_step(pRtree->pReadRowid);
193336      rc = sqlite3_reset(pRtree->pReadRowid);
193337      if( SQLITE_ROW==steprc ){
193338        if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
193339          rc = rtreeDeleteRowid(pRtree, cell.iRowid);
193340        }else{
193341          rc = rtreeConstraintError(pRtree, 0);
193342        }
193343      }
193344    }
193345  }
193346
193347  /* If aData[0] is not an SQL NULL value, it is the rowid of a
193348  ** record to delete from the r-tree table. The following block does
193349  ** just that.
193350  */
193351  if( rc==SQLITE_OK && (nData==1 || (coordChange && oldRowidValid)) ){
193352    rc = rtreeDeleteRowid(pRtree, oldRowid);
193353  }
193354
193355  /* If the aData[] array contains more than one element, elements
193356  ** (aData[2]..aData[argc-1]) contain a new record to insert into
193357  ** the r-tree structure.
193358  */
193359  if( rc==SQLITE_OK && nData>1 && coordChange ){
193360    /* Insert the new record into the r-tree */
193361    RtreeNode *pLeaf = 0;
193362    if( !newRowidValid ){
193363      rc = rtreeNewRowid(pRtree, &cell.iRowid);
193364    }
193365    *pRowid = cell.iRowid;
193366    if( rc==SQLITE_OK ){
193367      rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
193368    }
193369    if( rc==SQLITE_OK ){
193370      int rc2;
193371      pRtree->iReinsertHeight = -1;
193372      rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
193373      rc2 = nodeRelease(pRtree, pLeaf);
193374      if( rc==SQLITE_OK ){
193375        rc = rc2;
193376      }
193377    }
193378  }
193379
193380  /* Change the data */
193381  if( rc==SQLITE_OK && nData>1 ){
193382    sqlite3_stmt *pUp = pRtree->pWriteAux;
193383    int jj;
193384    int nChange = 0;
193385    sqlite3_bind_int64(pUp, 1, cell.iRowid);
193386    assert( pRtree->nAux>=1 );
193387    if( sqlite3_value_nochange(aData[2]) ){
193388      sqlite3_bind_null(pUp, 2);
193389    }else{
193390      GeoPoly *p = 0;
193391      if( sqlite3_value_type(aData[2])==SQLITE_TEXT
193392       && (p = geopolyFuncParam(0, aData[2], &rc))!=0
193393       && rc==SQLITE_OK
193394      ){
193395        sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT);
193396      }else{
193397        sqlite3_bind_value(pUp, 2, aData[2]);
193398      }
193399      sqlite3_free(p);
193400      nChange = 1;
193401    }
193402    for(jj=1; jj<pRtree->nAux; jj++){
193403      nChange++;
193404      sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
193405    }
193406    if( nChange ){
193407      sqlite3_step(pUp);
193408      rc = sqlite3_reset(pUp);
193409    }
193410  }
193411
193412geopoly_update_end:
193413  rtreeRelease(pRtree);
193414  return rc;
193415}
193416
193417/*
193418** Report that geopoly_overlap() is an overloaded function suitable
193419** for use in xBestIndex.
193420*/
193421static int geopolyFindFunction(
193422  sqlite3_vtab *pVtab,
193423  int nArg,
193424  const char *zName,
193425  void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
193426  void **ppArg
193427){
193428  if( sqlite3_stricmp(zName, "geopoly_overlap")==0 ){
193429    *pxFunc = geopolyOverlapFunc;
193430    *ppArg = 0;
193431    return SQLITE_INDEX_CONSTRAINT_FUNCTION;
193432  }
193433  if( sqlite3_stricmp(zName, "geopoly_within")==0 ){
193434    *pxFunc = geopolyWithinFunc;
193435    *ppArg = 0;
193436    return SQLITE_INDEX_CONSTRAINT_FUNCTION+1;
193437  }
193438  return 0;
193439}
193440
193441
193442static sqlite3_module geopolyModule = {
193443  3,                          /* iVersion */
193444  geopolyCreate,              /* xCreate - create a table */
193445  geopolyConnect,             /* xConnect - connect to an existing table */
193446  geopolyBestIndex,           /* xBestIndex - Determine search strategy */
193447  rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
193448  rtreeDestroy,               /* xDestroy - Drop a table */
193449  rtreeOpen,                  /* xOpen - open a cursor */
193450  rtreeClose,                 /* xClose - close a cursor */
193451  geopolyFilter,              /* xFilter - configure scan constraints */
193452  rtreeNext,                  /* xNext - advance a cursor */
193453  rtreeEof,                   /* xEof */
193454  geopolyColumn,              /* xColumn - read data */
193455  rtreeRowid,                 /* xRowid - read data */
193456  geopolyUpdate,              /* xUpdate - write data */
193457  rtreeBeginTransaction,      /* xBegin - begin transaction */
193458  rtreeEndTransaction,        /* xSync - sync transaction */
193459  rtreeEndTransaction,        /* xCommit - commit transaction */
193460  rtreeEndTransaction,        /* xRollback - rollback transaction */
193461  geopolyFindFunction,        /* xFindFunction - function overloading */
193462  rtreeRename,                /* xRename - rename the table */
193463  rtreeSavepoint,             /* xSavepoint */
193464  0,                          /* xRelease */
193465  0,                          /* xRollbackTo */
193466  rtreeShadowName             /* xShadowName */
193467};
193468
193469static int sqlite3_geopoly_init(sqlite3 *db){
193470  int rc = SQLITE_OK;
193471  static const struct {
193472    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
193473    signed char nArg;
193474    unsigned char bPure;
193475    const char *zName;
193476  } aFunc[] = {
193477     { geopolyAreaFunc,          1, 1,    "geopoly_area"             },
193478     { geopolyBlobFunc,          1, 1,    "geopoly_blob"             },
193479     { geopolyJsonFunc,          1, 1,    "geopoly_json"             },
193480     { geopolySvgFunc,          -1, 1,    "geopoly_svg"              },
193481     { geopolyWithinFunc,        2, 1,    "geopoly_within"           },
193482     { geopolyContainsPointFunc, 3, 1,    "geopoly_contains_point"   },
193483     { geopolyOverlapFunc,       2, 1,    "geopoly_overlap"          },
193484     { geopolyDebugFunc,         1, 0,    "geopoly_debug"            },
193485     { geopolyBBoxFunc,          1, 1,    "geopoly_bbox"             },
193486     { geopolyXformFunc,         7, 1,    "geopoly_xform"            },
193487     { geopolyRegularFunc,       4, 1,    "geopoly_regular"          },
193488     { geopolyCcwFunc,           1, 1,    "geopoly_ccw"              },
193489  };
193490  static const struct {
193491    void (*xStep)(sqlite3_context*,int,sqlite3_value**);
193492    void (*xFinal)(sqlite3_context*);
193493    const char *zName;
193494  } aAgg[] = {
193495     { geopolyBBoxStep, geopolyBBoxFinal, "geopoly_group_bbox"    },
193496  };
193497  int i;
193498  for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
193499    int enc;
193500    if( aFunc[i].bPure ){
193501      enc = SQLITE_UTF8|SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS;
193502    }else{
193503      enc = SQLITE_UTF8|SQLITE_DIRECTONLY;
193504    }
193505    rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
193506                                 enc, 0,
193507                                 aFunc[i].xFunc, 0, 0);
193508  }
193509  for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
193510    rc = sqlite3_create_function(db, aAgg[i].zName, 1,
193511              SQLITE_UTF8|SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS, 0,
193512              0, aAgg[i].xStep, aAgg[i].xFinal);
193513  }
193514  if( rc==SQLITE_OK ){
193515    rc = sqlite3_create_module_v2(db, "geopoly", &geopolyModule, 0, 0);
193516  }
193517  return rc;
193518}
193519
193520/************** End of geopoly.c *********************************************/
193521/************** Continuing where we left off in rtree.c **********************/
193522#endif
193523
193524/*
193525** Register the r-tree module with database handle db. This creates the
193526** virtual table module "rtree" and the debugging/analysis scalar
193527** function "rtreenode".
193528*/
193529SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
193530  const int utf8 = SQLITE_UTF8;
193531  int rc;
193532
193533  rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
193534  if( rc==SQLITE_OK ){
193535    rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
193536  }
193537  if( rc==SQLITE_OK ){
193538    rc = sqlite3_create_function(db, "rtreecheck", -1, utf8, 0,rtreecheck, 0,0);
193539  }
193540  if( rc==SQLITE_OK ){
193541#ifdef SQLITE_RTREE_INT_ONLY
193542    void *c = (void *)RTREE_COORD_INT32;
193543#else
193544    void *c = (void *)RTREE_COORD_REAL32;
193545#endif
193546    rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
193547  }
193548  if( rc==SQLITE_OK ){
193549    void *c = (void *)RTREE_COORD_INT32;
193550    rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
193551  }
193552#ifdef SQLITE_ENABLE_GEOPOLY
193553  if( rc==SQLITE_OK ){
193554    rc = sqlite3_geopoly_init(db);
193555  }
193556#endif
193557
193558  return rc;
193559}
193560
193561/*
193562** This routine deletes the RtreeGeomCallback object that was attached
193563** one of the SQL functions create by sqlite3_rtree_geometry_callback()
193564** or sqlite3_rtree_query_callback().  In other words, this routine is the
193565** destructor for an RtreeGeomCallback objecct.  This routine is called when
193566** the corresponding SQL function is deleted.
193567*/
193568static void rtreeFreeCallback(void *p){
193569  RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
193570  if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
193571  sqlite3_free(p);
193572}
193573
193574/*
193575** This routine frees the BLOB that is returned by geomCallback().
193576*/
193577static void rtreeMatchArgFree(void *pArg){
193578  int i;
193579  RtreeMatchArg *p = (RtreeMatchArg*)pArg;
193580  for(i=0; i<p->nParam; i++){
193581    sqlite3_value_free(p->apSqlParam[i]);
193582  }
193583  sqlite3_free(p);
193584}
193585
193586/*
193587** Each call to sqlite3_rtree_geometry_callback() or
193588** sqlite3_rtree_query_callback() creates an ordinary SQLite
193589** scalar function that is implemented by this routine.
193590**
193591** All this function does is construct an RtreeMatchArg object that
193592** contains the geometry-checking callback routines and a list of
193593** parameters to this function, then return that RtreeMatchArg object
193594** as a BLOB.
193595**
193596** The R-Tree MATCH operator will read the returned BLOB, deserialize
193597** the RtreeMatchArg object, and use the RtreeMatchArg object to figure
193598** out which elements of the R-Tree should be returned by the query.
193599*/
193600static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
193601  RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
193602  RtreeMatchArg *pBlob;
193603  sqlite3_int64 nBlob;
193604  int memErr = 0;
193605
193606  nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
193607           + nArg*sizeof(sqlite3_value*);
193608  pBlob = (RtreeMatchArg *)sqlite3_malloc64(nBlob);
193609  if( !pBlob ){
193610    sqlite3_result_error_nomem(ctx);
193611  }else{
193612    int i;
193613    pBlob->iSize = nBlob;
193614    pBlob->cb = pGeomCtx[0];
193615    pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
193616    pBlob->nParam = nArg;
193617    for(i=0; i<nArg; i++){
193618      pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
193619      if( pBlob->apSqlParam[i]==0 ) memErr = 1;
193620#ifdef SQLITE_RTREE_INT_ONLY
193621      pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
193622#else
193623      pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
193624#endif
193625    }
193626    if( memErr ){
193627      sqlite3_result_error_nomem(ctx);
193628      rtreeMatchArgFree(pBlob);
193629    }else{
193630      sqlite3_result_pointer(ctx, pBlob, "RtreeMatchArg", rtreeMatchArgFree);
193631    }
193632  }
193633}
193634
193635/*
193636** Register a new geometry function for use with the r-tree MATCH operator.
193637*/
193638SQLITE_API int sqlite3_rtree_geometry_callback(
193639  sqlite3 *db,                  /* Register SQL function on this connection */
193640  const char *zGeom,            /* Name of the new SQL function */
193641  int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */
193642  void *pContext                /* Extra data associated with the callback */
193643){
193644  RtreeGeomCallback *pGeomCtx;      /* Context object for new user-function */
193645
193646  /* Allocate and populate the context object. */
193647  pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
193648  if( !pGeomCtx ) return SQLITE_NOMEM;
193649  pGeomCtx->xGeom = xGeom;
193650  pGeomCtx->xQueryFunc = 0;
193651  pGeomCtx->xDestructor = 0;
193652  pGeomCtx->pContext = pContext;
193653  return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY,
193654      (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
193655  );
193656}
193657
193658/*
193659** Register a new 2nd-generation geometry function for use with the
193660** r-tree MATCH operator.
193661*/
193662SQLITE_API int sqlite3_rtree_query_callback(
193663  sqlite3 *db,                 /* Register SQL function on this connection */
193664  const char *zQueryFunc,      /* Name of new SQL function */
193665  int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */
193666  void *pContext,              /* Extra data passed into the callback */
193667  void (*xDestructor)(void*)   /* Destructor for the extra data */
193668){
193669  RtreeGeomCallback *pGeomCtx;      /* Context object for new user-function */
193670
193671  /* Allocate and populate the context object. */
193672  pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
193673  if( !pGeomCtx ) return SQLITE_NOMEM;
193674  pGeomCtx->xGeom = 0;
193675  pGeomCtx->xQueryFunc = xQueryFunc;
193676  pGeomCtx->xDestructor = xDestructor;
193677  pGeomCtx->pContext = pContext;
193678  return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY,
193679      (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
193680  );
193681}
193682
193683#if !SQLITE_CORE
193684#ifdef _WIN32
193685__declspec(dllexport)
193686#endif
193687SQLITE_API int sqlite3_rtree_init(
193688  sqlite3 *db,
193689  char **pzErrMsg,
193690  const sqlite3_api_routines *pApi
193691){
193692  SQLITE_EXTENSION_INIT2(pApi)
193693  return sqlite3RtreeInit(db);
193694}
193695#endif
193696
193697#endif
193698
193699/************** End of rtree.c ***********************************************/
193700/************** Begin file icu.c *********************************************/
193701/*
193702** 2007 May 6
193703**
193704** The author disclaims copyright to this source code.  In place of
193705** a legal notice, here is a blessing:
193706**
193707**    May you do good and not evil.
193708**    May you find forgiveness for yourself and forgive others.
193709**    May you share freely, never taking more than you give.
193710**
193711*************************************************************************
193712** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
193713**
193714** This file implements an integration between the ICU library
193715** ("International Components for Unicode", an open-source library
193716** for handling unicode data) and SQLite. The integration uses
193717** ICU to provide the following to SQLite:
193718**
193719**   * An implementation of the SQL regexp() function (and hence REGEXP
193720**     operator) using the ICU uregex_XX() APIs.
193721**
193722**   * Implementations of the SQL scalar upper() and lower() functions
193723**     for case mapping.
193724**
193725**   * Integration of ICU and SQLite collation sequences.
193726**
193727**   * An implementation of the LIKE operator that uses ICU to
193728**     provide case-independent matching.
193729*/
193730
193731#if !defined(SQLITE_CORE)                  \
193732 || defined(SQLITE_ENABLE_ICU)             \
193733 || defined(SQLITE_ENABLE_ICU_COLLATIONS)
193734
193735/* Include ICU headers */
193736#include <unicode/utypes.h>
193737#include <unicode/uregex.h>
193738#include <unicode/ustring.h>
193739#include <unicode/ucol.h>
193740
193741/* #include <assert.h> */
193742
193743#ifndef SQLITE_CORE
193744/*   #include "sqlite3ext.h" */
193745  SQLITE_EXTENSION_INIT1
193746#else
193747/*   #include "sqlite3.h" */
193748#endif
193749
193750/*
193751** This function is called when an ICU function called from within
193752** the implementation of an SQL scalar function returns an error.
193753**
193754** The scalar function context passed as the first argument is
193755** loaded with an error message based on the following two args.
193756*/
193757static void icuFunctionError(
193758  sqlite3_context *pCtx,       /* SQLite scalar function context */
193759  const char *zName,           /* Name of ICU function that failed */
193760  UErrorCode e                 /* Error code returned by ICU function */
193761){
193762  char zBuf[128];
193763  sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
193764  zBuf[127] = '\0';
193765  sqlite3_result_error(pCtx, zBuf, -1);
193766}
193767
193768#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
193769
193770/*
193771** Maximum length (in bytes) of the pattern in a LIKE or GLOB
193772** operator.
193773*/
193774#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
193775# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
193776#endif
193777
193778/*
193779** Version of sqlite3_free() that is always a function, never a macro.
193780*/
193781static void xFree(void *p){
193782  sqlite3_free(p);
193783}
193784
193785/*
193786** This lookup table is used to help decode the first byte of
193787** a multi-byte UTF8 character. It is copied here from SQLite source
193788** code file utf8.c.
193789*/
193790static const unsigned char icuUtf8Trans1[] = {
193791  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
193792  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
193793  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
193794  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
193795  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
193796  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
193797  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
193798  0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
193799};
193800
193801#define SQLITE_ICU_READ_UTF8(zIn, c)                       \
193802  c = *(zIn++);                                            \
193803  if( c>=0xc0 ){                                           \
193804    c = icuUtf8Trans1[c-0xc0];                             \
193805    while( (*zIn & 0xc0)==0x80 ){                          \
193806      c = (c<<6) + (0x3f & *(zIn++));                      \
193807    }                                                      \
193808  }
193809
193810#define SQLITE_ICU_SKIP_UTF8(zIn)                          \
193811  assert( *zIn );                                          \
193812  if( *(zIn++)>=0xc0 ){                                    \
193813    while( (*zIn & 0xc0)==0x80 ){zIn++;}                   \
193814  }
193815
193816
193817/*
193818** Compare two UTF-8 strings for equality where the first string is
193819** a "LIKE" expression. Return true (1) if they are the same and
193820** false (0) if they are different.
193821*/
193822static int icuLikeCompare(
193823  const uint8_t *zPattern,   /* LIKE pattern */
193824  const uint8_t *zString,    /* The UTF-8 string to compare against */
193825  const UChar32 uEsc         /* The escape character */
193826){
193827  static const uint32_t MATCH_ONE = (uint32_t)'_';
193828  static const uint32_t MATCH_ALL = (uint32_t)'%';
193829
193830  int prevEscape = 0;     /* True if the previous character was uEsc */
193831
193832  while( 1 ){
193833
193834    /* Read (and consume) the next character from the input pattern. */
193835    uint32_t uPattern;
193836    SQLITE_ICU_READ_UTF8(zPattern, uPattern);
193837    if( uPattern==0 ) break;
193838
193839    /* There are now 4 possibilities:
193840    **
193841    **     1. uPattern is an unescaped match-all character "%",
193842    **     2. uPattern is an unescaped match-one character "_",
193843    **     3. uPattern is an unescaped escape character, or
193844    **     4. uPattern is to be handled as an ordinary character
193845    */
193846    if( uPattern==MATCH_ALL && !prevEscape && uPattern!=(uint32_t)uEsc ){
193847      /* Case 1. */
193848      uint8_t c;
193849
193850      /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
193851      ** MATCH_ALL. For each MATCH_ONE, skip one character in the
193852      ** test string.
193853      */
193854      while( (c=*zPattern) == MATCH_ALL || c == MATCH_ONE ){
193855        if( c==MATCH_ONE ){
193856          if( *zString==0 ) return 0;
193857          SQLITE_ICU_SKIP_UTF8(zString);
193858        }
193859        zPattern++;
193860      }
193861
193862      if( *zPattern==0 ) return 1;
193863
193864      while( *zString ){
193865        if( icuLikeCompare(zPattern, zString, uEsc) ){
193866          return 1;
193867        }
193868        SQLITE_ICU_SKIP_UTF8(zString);
193869      }
193870      return 0;
193871
193872    }else if( uPattern==MATCH_ONE && !prevEscape && uPattern!=(uint32_t)uEsc ){
193873      /* Case 2. */
193874      if( *zString==0 ) return 0;
193875      SQLITE_ICU_SKIP_UTF8(zString);
193876
193877    }else if( uPattern==(uint32_t)uEsc && !prevEscape ){
193878      /* Case 3. */
193879      prevEscape = 1;
193880
193881    }else{
193882      /* Case 4. */
193883      uint32_t uString;
193884      SQLITE_ICU_READ_UTF8(zString, uString);
193885      uString = (uint32_t)u_foldCase((UChar32)uString, U_FOLD_CASE_DEFAULT);
193886      uPattern = (uint32_t)u_foldCase((UChar32)uPattern, U_FOLD_CASE_DEFAULT);
193887      if( uString!=uPattern ){
193888        return 0;
193889      }
193890      prevEscape = 0;
193891    }
193892  }
193893
193894  return *zString==0;
193895}
193896
193897/*
193898** Implementation of the like() SQL function.  This function implements
193899** the build-in LIKE operator.  The first argument to the function is the
193900** pattern and the second argument is the string.  So, the SQL statements:
193901**
193902**       A LIKE B
193903**
193904** is implemented as like(B, A). If there is an escape character E,
193905**
193906**       A LIKE B ESCAPE E
193907**
193908** is mapped to like(B, A, E).
193909*/
193910static void icuLikeFunc(
193911  sqlite3_context *context,
193912  int argc,
193913  sqlite3_value **argv
193914){
193915  const unsigned char *zA = sqlite3_value_text(argv[0]);
193916  const unsigned char *zB = sqlite3_value_text(argv[1]);
193917  UChar32 uEsc = 0;
193918
193919  /* Limit the length of the LIKE or GLOB pattern to avoid problems
193920  ** of deep recursion and N*N behavior in patternCompare().
193921  */
193922  if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
193923    sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
193924    return;
193925  }
193926
193927
193928  if( argc==3 ){
193929    /* The escape character string must consist of a single UTF-8 character.
193930    ** Otherwise, return an error.
193931    */
193932    int nE= sqlite3_value_bytes(argv[2]);
193933    const unsigned char *zE = sqlite3_value_text(argv[2]);
193934    int i = 0;
193935    if( zE==0 ) return;
193936    U8_NEXT(zE, i, nE, uEsc);
193937    if( i!=nE){
193938      sqlite3_result_error(context,
193939          "ESCAPE expression must be a single character", -1);
193940      return;
193941    }
193942  }
193943
193944  if( zA && zB ){
193945    sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
193946  }
193947}
193948
193949/*
193950** Function to delete compiled regexp objects. Registered as
193951** a destructor function with sqlite3_set_auxdata().
193952*/
193953static void icuRegexpDelete(void *p){
193954  URegularExpression *pExpr = (URegularExpression *)p;
193955  uregex_close(pExpr);
193956}
193957
193958/*
193959** Implementation of SQLite REGEXP operator. This scalar function takes
193960** two arguments. The first is a regular expression pattern to compile
193961** the second is a string to match against that pattern. If either
193962** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
193963** is 1 if the string matches the pattern, or 0 otherwise.
193964**
193965** SQLite maps the regexp() function to the regexp() operator such
193966** that the following two are equivalent:
193967**
193968**     zString REGEXP zPattern
193969**     regexp(zPattern, zString)
193970**
193971** Uses the following ICU regexp APIs:
193972**
193973**     uregex_open()
193974**     uregex_matches()
193975**     uregex_close()
193976*/
193977static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
193978  UErrorCode status = U_ZERO_ERROR;
193979  URegularExpression *pExpr;
193980  UBool res;
193981  const UChar *zString = sqlite3_value_text16(apArg[1]);
193982
193983  (void)nArg;  /* Unused parameter */
193984
193985  /* If the left hand side of the regexp operator is NULL,
193986  ** then the result is also NULL.
193987  */
193988  if( !zString ){
193989    return;
193990  }
193991
193992  pExpr = sqlite3_get_auxdata(p, 0);
193993  if( !pExpr ){
193994    const UChar *zPattern = sqlite3_value_text16(apArg[0]);
193995    if( !zPattern ){
193996      return;
193997    }
193998    pExpr = uregex_open(zPattern, -1, 0, 0, &status);
193999
194000    if( U_SUCCESS(status) ){
194001      sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
194002    }else{
194003      assert(!pExpr);
194004      icuFunctionError(p, "uregex_open", status);
194005      return;
194006    }
194007  }
194008
194009  /* Configure the text that the regular expression operates on. */
194010  uregex_setText(pExpr, zString, -1, &status);
194011  if( !U_SUCCESS(status) ){
194012    icuFunctionError(p, "uregex_setText", status);
194013    return;
194014  }
194015
194016  /* Attempt the match */
194017  res = uregex_matches(pExpr, 0, &status);
194018  if( !U_SUCCESS(status) ){
194019    icuFunctionError(p, "uregex_matches", status);
194020    return;
194021  }
194022
194023  /* Set the text that the regular expression operates on to a NULL
194024  ** pointer. This is not really necessary, but it is tidier than
194025  ** leaving the regular expression object configured with an invalid
194026  ** pointer after this function returns.
194027  */
194028  uregex_setText(pExpr, 0, 0, &status);
194029
194030  /* Return 1 or 0. */
194031  sqlite3_result_int(p, res ? 1 : 0);
194032}
194033
194034/*
194035** Implementations of scalar functions for case mapping - upper() and
194036** lower(). Function upper() converts its input to upper-case (ABC).
194037** Function lower() converts to lower-case (abc).
194038**
194039** ICU provides two types of case mapping, "general" case mapping and
194040** "language specific". Refer to ICU documentation for the differences
194041** between the two.
194042**
194043** To utilise "general" case mapping, the upper() or lower() scalar
194044** functions are invoked with one argument:
194045**
194046**     upper('ABC') -> 'abc'
194047**     lower('abc') -> 'ABC'
194048**
194049** To access ICU "language specific" case mapping, upper() or lower()
194050** should be invoked with two arguments. The second argument is the name
194051** of the locale to use. Passing an empty string ("") or SQL NULL value
194052** as the second argument is the same as invoking the 1 argument version
194053** of upper() or lower().
194054**
194055**     lower('I', 'en_us') -> 'i'
194056**     lower('I', 'tr_tr') -> '\u131' (small dotless i)
194057**
194058** http://www.icu-project.org/userguide/posix.html#case_mappings
194059*/
194060static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
194061  const UChar *zInput;            /* Pointer to input string */
194062  UChar *zOutput = 0;             /* Pointer to output buffer */
194063  int nInput;                     /* Size of utf-16 input string in bytes */
194064  int nOut;                       /* Size of output buffer in bytes */
194065  int cnt;
194066  int bToUpper;                   /* True for toupper(), false for tolower() */
194067  UErrorCode status;
194068  const char *zLocale = 0;
194069
194070  assert(nArg==1 || nArg==2);
194071  bToUpper = (sqlite3_user_data(p)!=0);
194072  if( nArg==2 ){
194073    zLocale = (const char *)sqlite3_value_text(apArg[1]);
194074  }
194075
194076  zInput = sqlite3_value_text16(apArg[0]);
194077  if( !zInput ){
194078    return;
194079  }
194080  nOut = nInput = sqlite3_value_bytes16(apArg[0]);
194081  if( nOut==0 ){
194082    sqlite3_result_text16(p, "", 0, SQLITE_STATIC);
194083    return;
194084  }
194085
194086  for(cnt=0; cnt<2; cnt++){
194087    UChar *zNew = sqlite3_realloc(zOutput, nOut);
194088    if( zNew==0 ){
194089      sqlite3_free(zOutput);
194090      sqlite3_result_error_nomem(p);
194091      return;
194092    }
194093    zOutput = zNew;
194094    status = U_ZERO_ERROR;
194095    if( bToUpper ){
194096      nOut = 2*u_strToUpper(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
194097    }else{
194098      nOut = 2*u_strToLower(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
194099    }
194100
194101    if( U_SUCCESS(status) ){
194102      sqlite3_result_text16(p, zOutput, nOut, xFree);
194103    }else if( status==U_BUFFER_OVERFLOW_ERROR ){
194104      assert( cnt==0 );
194105      continue;
194106    }else{
194107      icuFunctionError(p, bToUpper ? "u_strToUpper" : "u_strToLower", status);
194108    }
194109    return;
194110  }
194111  assert( 0 );     /* Unreachable */
194112}
194113
194114#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
194115
194116/*
194117** Collation sequence destructor function. The pCtx argument points to
194118** a UCollator structure previously allocated using ucol_open().
194119*/
194120static void icuCollationDel(void *pCtx){
194121  UCollator *p = (UCollator *)pCtx;
194122  ucol_close(p);
194123}
194124
194125/*
194126** Collation sequence comparison function. The pCtx argument points to
194127** a UCollator structure previously allocated using ucol_open().
194128*/
194129static int icuCollationColl(
194130  void *pCtx,
194131  int nLeft,
194132  const void *zLeft,
194133  int nRight,
194134  const void *zRight
194135){
194136  UCollationResult res;
194137  UCollator *p = (UCollator *)pCtx;
194138  res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
194139  switch( res ){
194140    case UCOL_LESS:    return -1;
194141    case UCOL_GREATER: return +1;
194142    case UCOL_EQUAL:   return 0;
194143  }
194144  assert(!"Unexpected return value from ucol_strcoll()");
194145  return 0;
194146}
194147
194148/*
194149** Implementation of the scalar function icu_load_collation().
194150**
194151** This scalar function is used to add ICU collation based collation
194152** types to an SQLite database connection. It is intended to be called
194153** as follows:
194154**
194155**     SELECT icu_load_collation(<locale>, <collation-name>);
194156**
194157** Where <locale> is a string containing an ICU locale identifier (i.e.
194158** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
194159** collation sequence to create.
194160*/
194161static void icuLoadCollation(
194162  sqlite3_context *p,
194163  int nArg,
194164  sqlite3_value **apArg
194165){
194166  sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
194167  UErrorCode status = U_ZERO_ERROR;
194168  const char *zLocale;      /* Locale identifier - (eg. "jp_JP") */
194169  const char *zName;        /* SQL Collation sequence name (eg. "japanese") */
194170  UCollator *pUCollator;    /* ICU library collation object */
194171  int rc;                   /* Return code from sqlite3_create_collation_x() */
194172
194173  assert(nArg==2);
194174  (void)nArg; /* Unused parameter */
194175  zLocale = (const char *)sqlite3_value_text(apArg[0]);
194176  zName = (const char *)sqlite3_value_text(apArg[1]);
194177
194178  if( !zLocale || !zName ){
194179    return;
194180  }
194181
194182  pUCollator = ucol_open(zLocale, &status);
194183  if( !U_SUCCESS(status) ){
194184    icuFunctionError(p, "ucol_open", status);
194185    return;
194186  }
194187  assert(p);
194188
194189  rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator,
194190      icuCollationColl, icuCollationDel
194191  );
194192  if( rc!=SQLITE_OK ){
194193    ucol_close(pUCollator);
194194    sqlite3_result_error(p, "Error registering collation function", -1);
194195  }
194196}
194197
194198/*
194199** Register the ICU extension functions with database db.
194200*/
194201SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
194202# define SQLITEICU_EXTRAFLAGS (SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS)
194203  static const struct IcuScalar {
194204    const char *zName;                        /* Function name */
194205    unsigned char nArg;                       /* Number of arguments */
194206    unsigned int enc;                         /* Optimal text encoding */
194207    unsigned char iContext;                   /* sqlite3_user_data() context */
194208    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
194209  } scalars[] = {
194210    {"icu_load_collation",2,SQLITE_UTF8|SQLITE_DIRECTONLY,1, icuLoadCollation},
194211#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
194212    {"regexp", 2, SQLITE_ANY|SQLITEICU_EXTRAFLAGS,         0, icuRegexpFunc},
194213    {"lower",  1, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS,       0, icuCaseFunc16},
194214    {"lower",  2, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS,       0, icuCaseFunc16},
194215    {"upper",  1, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS,       1, icuCaseFunc16},
194216    {"upper",  2, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS,       1, icuCaseFunc16},
194217    {"lower",  1, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS,        0, icuCaseFunc16},
194218    {"lower",  2, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS,        0, icuCaseFunc16},
194219    {"upper",  1, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS,        1, icuCaseFunc16},
194220    {"upper",  2, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS,        1, icuCaseFunc16},
194221    {"like",   2, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS,        0, icuLikeFunc},
194222    {"like",   3, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS,        0, icuLikeFunc},
194223#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
194224  };
194225  int rc = SQLITE_OK;
194226  int i;
194227
194228  for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
194229    const struct IcuScalar *p = &scalars[i];
194230    rc = sqlite3_create_function(
194231        db, p->zName, p->nArg, p->enc,
194232        p->iContext ? (void*)db : (void*)0,
194233        p->xFunc, 0, 0
194234    );
194235  }
194236
194237  return rc;
194238}
194239
194240#if !SQLITE_CORE
194241#ifdef _WIN32
194242__declspec(dllexport)
194243#endif
194244SQLITE_API int sqlite3_icu_init(
194245  sqlite3 *db,
194246  char **pzErrMsg,
194247  const sqlite3_api_routines *pApi
194248){
194249  SQLITE_EXTENSION_INIT2(pApi)
194250  return sqlite3IcuInit(db);
194251}
194252#endif
194253
194254#endif
194255
194256/************** End of icu.c *************************************************/
194257/************** Begin file fts3_icu.c ****************************************/
194258/*
194259** 2007 June 22
194260**
194261** The author disclaims copyright to this source code.  In place of
194262** a legal notice, here is a blessing:
194263**
194264**    May you do good and not evil.
194265**    May you find forgiveness for yourself and forgive others.
194266**    May you share freely, never taking more than you give.
194267**
194268*************************************************************************
194269** This file implements a tokenizer for fts3 based on the ICU library.
194270*/
194271/* #include "fts3Int.h" */
194272#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
194273#ifdef SQLITE_ENABLE_ICU
194274
194275/* #include <assert.h> */
194276/* #include <string.h> */
194277/* #include "fts3_tokenizer.h" */
194278
194279#include <unicode/ubrk.h>
194280/* #include <unicode/ucol.h> */
194281/* #include <unicode/ustring.h> */
194282#include <unicode/utf16.h>
194283
194284typedef struct IcuTokenizer IcuTokenizer;
194285typedef struct IcuCursor IcuCursor;
194286
194287struct IcuTokenizer {
194288  sqlite3_tokenizer base;
194289  char *zLocale;
194290};
194291
194292struct IcuCursor {
194293  sqlite3_tokenizer_cursor base;
194294
194295  UBreakIterator *pIter;      /* ICU break-iterator object */
194296  int nChar;                  /* Number of UChar elements in pInput */
194297  UChar *aChar;               /* Copy of input using utf-16 encoding */
194298  int *aOffset;               /* Offsets of each character in utf-8 input */
194299
194300  int nBuffer;
194301  char *zBuffer;
194302
194303  int iToken;
194304};
194305
194306/*
194307** Create a new tokenizer instance.
194308*/
194309static int icuCreate(
194310  int argc,                            /* Number of entries in argv[] */
194311  const char * const *argv,            /* Tokenizer creation arguments */
194312  sqlite3_tokenizer **ppTokenizer      /* OUT: Created tokenizer */
194313){
194314  IcuTokenizer *p;
194315  int n = 0;
194316
194317  if( argc>0 ){
194318    n = strlen(argv[0])+1;
194319  }
194320  p = (IcuTokenizer *)sqlite3_malloc64(sizeof(IcuTokenizer)+n);
194321  if( !p ){
194322    return SQLITE_NOMEM;
194323  }
194324  memset(p, 0, sizeof(IcuTokenizer));
194325
194326  if( n ){
194327    p->zLocale = (char *)&p[1];
194328    memcpy(p->zLocale, argv[0], n);
194329  }
194330
194331  *ppTokenizer = (sqlite3_tokenizer *)p;
194332
194333  return SQLITE_OK;
194334}
194335
194336/*
194337** Destroy a tokenizer
194338*/
194339static int icuDestroy(sqlite3_tokenizer *pTokenizer){
194340  IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
194341  sqlite3_free(p);
194342  return SQLITE_OK;
194343}
194344
194345/*
194346** Prepare to begin tokenizing a particular string.  The input
194347** string to be tokenized is pInput[0..nBytes-1].  A cursor
194348** used to incrementally tokenize this string is returned in
194349** *ppCursor.
194350*/
194351static int icuOpen(
194352  sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
194353  const char *zInput,                    /* Input string */
194354  int nInput,                            /* Length of zInput in bytes */
194355  sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
194356){
194357  IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
194358  IcuCursor *pCsr;
194359
194360  const int32_t opt = U_FOLD_CASE_DEFAULT;
194361  UErrorCode status = U_ZERO_ERROR;
194362  int nChar;
194363
194364  UChar32 c;
194365  int iInput = 0;
194366  int iOut = 0;
194367
194368  *ppCursor = 0;
194369
194370  if( zInput==0 ){
194371    nInput = 0;
194372    zInput = "";
194373  }else if( nInput<0 ){
194374    nInput = strlen(zInput);
194375  }
194376  nChar = nInput+1;
194377  pCsr = (IcuCursor *)sqlite3_malloc64(
194378      sizeof(IcuCursor) +                /* IcuCursor */
194379      ((nChar+3)&~3) * sizeof(UChar) +   /* IcuCursor.aChar[] */
194380      (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */
194381  );
194382  if( !pCsr ){
194383    return SQLITE_NOMEM;
194384  }
194385  memset(pCsr, 0, sizeof(IcuCursor));
194386  pCsr->aChar = (UChar *)&pCsr[1];
194387  pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
194388
194389  pCsr->aOffset[iOut] = iInput;
194390  U8_NEXT(zInput, iInput, nInput, c);
194391  while( c>0 ){
194392    int isError = 0;
194393    c = u_foldCase(c, opt);
194394    U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
194395    if( isError ){
194396      sqlite3_free(pCsr);
194397      return SQLITE_ERROR;
194398    }
194399    pCsr->aOffset[iOut] = iInput;
194400
194401    if( iInput<nInput ){
194402      U8_NEXT(zInput, iInput, nInput, c);
194403    }else{
194404      c = 0;
194405    }
194406  }
194407
194408  pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
194409  if( !U_SUCCESS(status) ){
194410    sqlite3_free(pCsr);
194411    return SQLITE_ERROR;
194412  }
194413  pCsr->nChar = iOut;
194414
194415  ubrk_first(pCsr->pIter);
194416  *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
194417  return SQLITE_OK;
194418}
194419
194420/*
194421** Close a tokenization cursor previously opened by a call to icuOpen().
194422*/
194423static int icuClose(sqlite3_tokenizer_cursor *pCursor){
194424  IcuCursor *pCsr = (IcuCursor *)pCursor;
194425  ubrk_close(pCsr->pIter);
194426  sqlite3_free(pCsr->zBuffer);
194427  sqlite3_free(pCsr);
194428  return SQLITE_OK;
194429}
194430
194431/*
194432** Extract the next token from a tokenization cursor.
194433*/
194434static int icuNext(
194435  sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
194436  const char **ppToken,               /* OUT: *ppToken is the token text */
194437  int *pnBytes,                       /* OUT: Number of bytes in token */
194438  int *piStartOffset,                 /* OUT: Starting offset of token */
194439  int *piEndOffset,                   /* OUT: Ending offset of token */
194440  int *piPosition                     /* OUT: Position integer of token */
194441){
194442  IcuCursor *pCsr = (IcuCursor *)pCursor;
194443
194444  int iStart = 0;
194445  int iEnd = 0;
194446  int nByte = 0;
194447
194448  while( iStart==iEnd ){
194449    UChar32 c;
194450
194451    iStart = ubrk_current(pCsr->pIter);
194452    iEnd = ubrk_next(pCsr->pIter);
194453    if( iEnd==UBRK_DONE ){
194454      return SQLITE_DONE;
194455    }
194456
194457    while( iStart<iEnd ){
194458      int iWhite = iStart;
194459      U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
194460      if( u_isspace(c) ){
194461        iStart = iWhite;
194462      }else{
194463        break;
194464      }
194465    }
194466    assert(iStart<=iEnd);
194467  }
194468
194469  do {
194470    UErrorCode status = U_ZERO_ERROR;
194471    if( nByte ){
194472      char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
194473      if( !zNew ){
194474        return SQLITE_NOMEM;
194475      }
194476      pCsr->zBuffer = zNew;
194477      pCsr->nBuffer = nByte;
194478    }
194479
194480    u_strToUTF8(
194481        pCsr->zBuffer, pCsr->nBuffer, &nByte,    /* Output vars */
194482        &pCsr->aChar[iStart], iEnd-iStart,       /* Input vars */
194483        &status                                  /* Output success/failure */
194484    );
194485  } while( nByte>pCsr->nBuffer );
194486
194487  *ppToken = pCsr->zBuffer;
194488  *pnBytes = nByte;
194489  *piStartOffset = pCsr->aOffset[iStart];
194490  *piEndOffset = pCsr->aOffset[iEnd];
194491  *piPosition = pCsr->iToken++;
194492
194493  return SQLITE_OK;
194494}
194495
194496/*
194497** The set of routines that implement the simple tokenizer
194498*/
194499static const sqlite3_tokenizer_module icuTokenizerModule = {
194500  0,                           /* iVersion    */
194501  icuCreate,                   /* xCreate     */
194502  icuDestroy,                  /* xCreate     */
194503  icuOpen,                     /* xOpen       */
194504  icuClose,                    /* xClose      */
194505  icuNext,                     /* xNext       */
194506  0,                           /* xLanguageid */
194507};
194508
194509/*
194510** Set *ppModule to point at the implementation of the ICU tokenizer.
194511*/
194512SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
194513  sqlite3_tokenizer_module const**ppModule
194514){
194515  *ppModule = &icuTokenizerModule;
194516}
194517
194518#endif /* defined(SQLITE_ENABLE_ICU) */
194519#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
194520
194521/************** End of fts3_icu.c ********************************************/
194522/************** Begin file sqlite3rbu.c **************************************/
194523/*
194524** 2014 August 30
194525**
194526** The author disclaims copyright to this source code.  In place of
194527** a legal notice, here is a blessing:
194528**
194529**    May you do good and not evil.
194530**    May you find forgiveness for yourself and forgive others.
194531**    May you share freely, never taking more than you give.
194532**
194533*************************************************************************
194534**
194535**
194536** OVERVIEW
194537**
194538**  The RBU extension requires that the RBU update be packaged as an
194539**  SQLite database. The tables it expects to find are described in
194540**  sqlite3rbu.h.  Essentially, for each table xyz in the target database
194541**  that the user wishes to write to, a corresponding data_xyz table is
194542**  created in the RBU database and populated with one row for each row to
194543**  update, insert or delete from the target table.
194544**
194545**  The update proceeds in three stages:
194546**
194547**  1) The database is updated. The modified database pages are written
194548**     to a *-oal file. A *-oal file is just like a *-wal file, except
194549**     that it is named "<database>-oal" instead of "<database>-wal".
194550**     Because regular SQLite clients do not look for file named
194551**     "<database>-oal", they go on using the original database in
194552**     rollback mode while the *-oal file is being generated.
194553**
194554**     During this stage RBU does not update the database by writing
194555**     directly to the target tables. Instead it creates "imposter"
194556**     tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
194557**     to update each b-tree individually. All updates required by each
194558**     b-tree are completed before moving on to the next, and all
194559**     updates are done in sorted key order.
194560**
194561**  2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
194562**     location using a call to rename(2). Before doing this the RBU
194563**     module takes an EXCLUSIVE lock on the database file, ensuring
194564**     that there are no other active readers.
194565**
194566**     Once the EXCLUSIVE lock is released, any other database readers
194567**     detect the new *-wal file and read the database in wal mode. At
194568**     this point they see the new version of the database - including
194569**     the updates made as part of the RBU update.
194570**
194571**  3) The new *-wal file is checkpointed. This proceeds in the same way
194572**     as a regular database checkpoint, except that a single frame is
194573**     checkpointed each time sqlite3rbu_step() is called. If the RBU
194574**     handle is closed before the entire *-wal file is checkpointed,
194575**     the checkpoint progress is saved in the RBU database and the
194576**     checkpoint can be resumed by another RBU client at some point in
194577**     the future.
194578**
194579** POTENTIAL PROBLEMS
194580**
194581**  The rename() call might not be portable. And RBU is not currently
194582**  syncing the directory after renaming the file.
194583**
194584**  When state is saved, any commit to the *-oal file and the commit to
194585**  the RBU update database are not atomic. So if the power fails at the
194586**  wrong moment they might get out of sync. As the main database will be
194587**  committed before the RBU update database this will likely either just
194588**  pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
194589**  constraint violations).
194590**
194591**  If some client does modify the target database mid RBU update, or some
194592**  other error occurs, the RBU extension will keep throwing errors. It's
194593**  not really clear how to get out of this state. The system could just
194594**  by delete the RBU update database and *-oal file and have the device
194595**  download the update again and start over.
194596**
194597**  At present, for an UPDATE, both the new.* and old.* records are
194598**  collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
194599**  fields are collected.  This means we're probably writing a lot more
194600**  data to disk when saving the state of an ongoing update to the RBU
194601**  update database than is strictly necessary.
194602**
194603*/
194604
194605/* #include <assert.h> */
194606/* #include <string.h> */
194607/* #include <stdio.h> */
194608
194609/* #include "sqlite3.h" */
194610
194611#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
194612/************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
194613/************** Begin file sqlite3rbu.h **************************************/
194614/*
194615** 2014 August 30
194616**
194617** The author disclaims copyright to this source code.  In place of
194618** a legal notice, here is a blessing:
194619**
194620**    May you do good and not evil.
194621**    May you find forgiveness for yourself and forgive others.
194622**    May you share freely, never taking more than you give.
194623**
194624*************************************************************************
194625**
194626** This file contains the public interface for the RBU extension.
194627*/
194628
194629/*
194630** SUMMARY
194631**
194632** Writing a transaction containing a large number of operations on
194633** b-tree indexes that are collectively larger than the available cache
194634** memory can be very inefficient.
194635**
194636** The problem is that in order to update a b-tree, the leaf page (at least)
194637** containing the entry being inserted or deleted must be modified. If the
194638** working set of leaves is larger than the available cache memory, then a
194639** single leaf that is modified more than once as part of the transaction
194640** may be loaded from or written to the persistent media multiple times.
194641** Additionally, because the index updates are likely to be applied in
194642** random order, access to pages within the database is also likely to be in
194643** random order, which is itself quite inefficient.
194644**
194645** One way to improve the situation is to sort the operations on each index
194646** by index key before applying them to the b-tree. This leads to an IO
194647** pattern that resembles a single linear scan through the index b-tree,
194648** and all but guarantees each modified leaf page is loaded and stored
194649** exactly once. SQLite uses this trick to improve the performance of
194650** CREATE INDEX commands. This extension allows it to be used to improve
194651** the performance of large transactions on existing databases.
194652**
194653** Additionally, this extension allows the work involved in writing the
194654** large transaction to be broken down into sub-transactions performed
194655** sequentially by separate processes. This is useful if the system cannot
194656** guarantee that a single update process will run for long enough to apply
194657** the entire update, for example because the update is being applied on a
194658** mobile device that is frequently rebooted. Even after the writer process
194659** has committed one or more sub-transactions, other database clients continue
194660** to read from the original database snapshot. In other words, partially
194661** applied transactions are not visible to other clients.
194662**
194663** "RBU" stands for "Resumable Bulk Update". As in a large database update
194664** transmitted via a wireless network to a mobile device. A transaction
194665** applied using this extension is hence refered to as an "RBU update".
194666**
194667**
194668** LIMITATIONS
194669**
194670** An "RBU update" transaction is subject to the following limitations:
194671**
194672**   * The transaction must consist of INSERT, UPDATE and DELETE operations
194673**     only.
194674**
194675**   * INSERT statements may not use any default values.
194676**
194677**   * UPDATE and DELETE statements must identify their target rows by
194678**     non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
194679**     KEY fields may not be updated or deleted. If the table being written
194680**     has no PRIMARY KEY, affected rows must be identified by rowid.
194681**
194682**   * UPDATE statements may not modify PRIMARY KEY columns.
194683**
194684**   * No triggers will be fired.
194685**
194686**   * No foreign key violations are detected or reported.
194687**
194688**   * CHECK constraints are not enforced.
194689**
194690**   * No constraint handling mode except for "OR ROLLBACK" is supported.
194691**
194692**
194693** PREPARATION
194694**
194695** An "RBU update" is stored as a separate SQLite database. A database
194696** containing an RBU update is an "RBU database". For each table in the
194697** target database to be updated, the RBU database should contain a table
194698** named "data_<target name>" containing the same set of columns as the
194699** target table, and one more - "rbu_control". The data_% table should
194700** have no PRIMARY KEY or UNIQUE constraints, but each column should have
194701** the same type as the corresponding column in the target database.
194702** The "rbu_control" column should have no type at all. For example, if
194703** the target database contains:
194704**
194705**   CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
194706**
194707** Then the RBU database should contain:
194708**
194709**   CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
194710**
194711** The order of the columns in the data_% table does not matter.
194712**
194713** Instead of a regular table, the RBU database may also contain virtual
194714** tables or view named using the data_<target> naming scheme.
194715**
194716** Instead of the plain data_<target> naming scheme, RBU database tables
194717** may also be named data<integer>_<target>, where <integer> is any sequence
194718** of zero or more numeric characters (0-9). This can be significant because
194719** tables within the RBU database are always processed in order sorted by
194720** name. By judicious selection of the <integer> portion of the names
194721** of the RBU tables the user can therefore control the order in which they
194722** are processed. This can be useful, for example, to ensure that "external
194723** content" FTS4 tables are updated before their underlying content tables.
194724**
194725** If the target database table is a virtual table or a table that has no
194726** PRIMARY KEY declaration, the data_% table must also contain a column
194727** named "rbu_rowid". This column is mapped to the tables implicit primary
194728** key column - "rowid". Virtual tables for which the "rowid" column does
194729** not function like a primary key value cannot be updated using RBU. For
194730** example, if the target db contains either of the following:
194731**
194732**   CREATE VIRTUAL TABLE x1 USING fts3(a, b);
194733**   CREATE TABLE x1(a, b)
194734**
194735** then the RBU database should contain:
194736**
194737**   CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
194738**
194739** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
194740** target table must be present in the input table. For virtual tables,
194741** hidden columns are optional - they are updated by RBU if present in
194742** the input table, or not otherwise. For example, to write to an fts4
194743** table with a hidden languageid column such as:
194744**
194745**   CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
194746**
194747** Either of the following input table schemas may be used:
194748**
194749**   CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
194750**   CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
194751**
194752** For each row to INSERT into the target database as part of the RBU
194753** update, the corresponding data_% table should contain a single record
194754** with the "rbu_control" column set to contain integer value 0. The
194755** other columns should be set to the values that make up the new record
194756** to insert.
194757**
194758** If the target database table has an INTEGER PRIMARY KEY, it is not
194759** possible to insert a NULL value into the IPK column. Attempting to
194760** do so results in an SQLITE_MISMATCH error.
194761**
194762** For each row to DELETE from the target database as part of the RBU
194763** update, the corresponding data_% table should contain a single record
194764** with the "rbu_control" column set to contain integer value 1. The
194765** real primary key values of the row to delete should be stored in the
194766** corresponding columns of the data_% table. The values stored in the
194767** other columns are not used.
194768**
194769** For each row to UPDATE from the target database as part of the RBU
194770** update, the corresponding data_% table should contain a single record
194771** with the "rbu_control" column set to contain a value of type text.
194772** The real primary key values identifying the row to update should be
194773** stored in the corresponding columns of the data_% table row, as should
194774** the new values of all columns being update. The text value in the
194775** "rbu_control" column must contain the same number of characters as
194776** there are columns in the target database table, and must consist entirely
194777** of 'x' and '.' characters (or in some special cases 'd' - see below). For
194778** each column that is being updated, the corresponding character is set to
194779** 'x'. For those that remain as they are, the corresponding character of the
194780** rbu_control value should be set to '.'. For example, given the tables
194781** above, the update statement:
194782**
194783**   UPDATE t1 SET c = 'usa' WHERE a = 4;
194784**
194785** is represented by the data_t1 row created by:
194786**
194787**   INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
194788**
194789** Instead of an 'x' character, characters of the rbu_control value specified
194790** for UPDATEs may also be set to 'd'. In this case, instead of updating the
194791** target table with the value stored in the corresponding data_% column, the
194792** user-defined SQL function "rbu_delta()" is invoked and the result stored in
194793** the target table column. rbu_delta() is invoked with two arguments - the
194794** original value currently stored in the target table column and the
194795** value specified in the data_xxx table.
194796**
194797** For example, this row:
194798**
194799**   INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
194800**
194801** is similar to an UPDATE statement such as:
194802**
194803**   UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
194804**
194805** Finally, if an 'f' character appears in place of a 'd' or 's' in an
194806** ota_control string, the contents of the data_xxx table column is assumed
194807** to be a "fossil delta" - a patch to be applied to a blob value in the
194808** format used by the fossil source-code management system. In this case
194809** the existing value within the target database table must be of type BLOB.
194810** It is replaced by the result of applying the specified fossil delta to
194811** itself.
194812**
194813** If the target database table is a virtual table or a table with no PRIMARY
194814** KEY, the rbu_control value should not include a character corresponding
194815** to the rbu_rowid value. For example, this:
194816**
194817**   INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
194818**       VALUES(NULL, 'usa', 12, '.x');
194819**
194820** causes a result similar to:
194821**
194822**   UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
194823**
194824** The data_xxx tables themselves should have no PRIMARY KEY declarations.
194825** However, RBU is more efficient if reading the rows in from each data_xxx
194826** table in "rowid" order is roughly the same as reading them sorted by
194827** the PRIMARY KEY of the corresponding target database table. In other
194828** words, rows should be sorted using the destination table PRIMARY KEY
194829** fields before they are inserted into the data_xxx tables.
194830**
194831** USAGE
194832**
194833** The API declared below allows an application to apply an RBU update
194834** stored on disk to an existing target database. Essentially, the
194835** application:
194836**
194837**     1) Opens an RBU handle using the sqlite3rbu_open() function.
194838**
194839**     2) Registers any required virtual table modules with the database
194840**        handle returned by sqlite3rbu_db(). Also, if required, register
194841**        the rbu_delta() implementation.
194842**
194843**     3) Calls the sqlite3rbu_step() function one or more times on
194844**        the new handle. Each call to sqlite3rbu_step() performs a single
194845**        b-tree operation, so thousands of calls may be required to apply
194846**        a complete update.
194847**
194848**     4) Calls sqlite3rbu_close() to close the RBU update handle. If
194849**        sqlite3rbu_step() has been called enough times to completely
194850**        apply the update to the target database, then the RBU database
194851**        is marked as fully applied. Otherwise, the state of the RBU
194852**        update application is saved in the RBU database for later
194853**        resumption.
194854**
194855** See comments below for more detail on APIs.
194856**
194857** If an update is only partially applied to the target database by the
194858** time sqlite3rbu_close() is called, various state information is saved
194859** within the RBU database. This allows subsequent processes to automatically
194860** resume the RBU update from where it left off.
194861**
194862** To remove all RBU extension state information, returning an RBU database
194863** to its original contents, it is sufficient to drop all tables that begin
194864** with the prefix "rbu_"
194865**
194866** DATABASE LOCKING
194867**
194868** An RBU update may not be applied to a database in WAL mode. Attempting
194869** to do so is an error (SQLITE_ERROR).
194870**
194871** While an RBU handle is open, a SHARED lock may be held on the target
194872** database file. This means it is possible for other clients to read the
194873** database, but not to write it.
194874**
194875** If an RBU update is started and then suspended before it is completed,
194876** then an external client writes to the database, then attempting to resume
194877** the suspended RBU update is also an error (SQLITE_BUSY).
194878*/
194879
194880#ifndef _SQLITE3RBU_H
194881#define _SQLITE3RBU_H
194882
194883/* #include "sqlite3.h"              ** Required for error code definitions ** */
194884
194885#if 0
194886extern "C" {
194887#endif
194888
194889typedef struct sqlite3rbu sqlite3rbu;
194890
194891/*
194892** Open an RBU handle.
194893**
194894** Argument zTarget is the path to the target database. Argument zRbu is
194895** the path to the RBU database. Each call to this function must be matched
194896** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
194897** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
194898** or zRbu begin with "file:", it will be interpreted as an SQLite
194899** database URI, not a regular file name.
194900**
194901** If the zState argument is passed a NULL value, the RBU extension stores
194902** the current state of the update (how many rows have been updated, which
194903** indexes are yet to be updated etc.) within the RBU database itself. This
194904** can be convenient, as it means that the RBU application does not need to
194905** organize removing a separate state file after the update is concluded.
194906** Or, if zState is non-NULL, it must be a path to a database file in which
194907** the RBU extension can store the state of the update.
194908**
194909** When resuming an RBU update, the zState argument must be passed the same
194910** value as when the RBU update was started.
194911**
194912** Once the RBU update is finished, the RBU extension does not
194913** automatically remove any zState database file, even if it created it.
194914**
194915** By default, RBU uses the default VFS to access the files on disk. To
194916** use a VFS other than the default, an SQLite "file:" URI containing a
194917** "vfs=..." option may be passed as the zTarget option.
194918**
194919** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
194920** SQLite's built-in VFSs, including the multiplexor VFS. However it does
194921** not work out of the box with zipvfs. Refer to the comment describing
194922** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
194923*/
194924SQLITE_API sqlite3rbu *sqlite3rbu_open(
194925  const char *zTarget,
194926  const char *zRbu,
194927  const char *zState
194928);
194929
194930/*
194931** Open an RBU handle to perform an RBU vacuum on database file zTarget.
194932** An RBU vacuum is similar to SQLite's built-in VACUUM command, except
194933** that it can be suspended and resumed like an RBU update.
194934**
194935** The second argument to this function identifies a database in which
194936** to store the state of the RBU vacuum operation if it is suspended. The
194937** first time sqlite3rbu_vacuum() is called, to start an RBU vacuum
194938** operation, the state database should either not exist or be empty
194939** (contain no tables). If an RBU vacuum is suspended by calling
194940** sqlite3rbu_close() on the RBU handle before sqlite3rbu_step() has
194941** returned SQLITE_DONE, the vacuum state is stored in the state database.
194942** The vacuum can be resumed by calling this function to open a new RBU
194943** handle specifying the same target and state databases.
194944**
194945** If the second argument passed to this function is NULL, then the
194946** name of the state database is "<database>-vacuum", where <database>
194947** is the name of the target database file. In this case, on UNIX, if the
194948** state database is not already present in the file-system, it is created
194949** with the same permissions as the target db is made.
194950**
194951** With an RBU vacuum, it is an SQLITE_MISUSE error if the name of the
194952** state database ends with "-vactmp". This name is reserved for internal
194953** use.
194954**
194955** This function does not delete the state database after an RBU vacuum
194956** is completed, even if it created it. However, if the call to
194957** sqlite3rbu_close() returns any value other than SQLITE_OK, the contents
194958** of the state tables within the state database are zeroed. This way,
194959** the next call to sqlite3rbu_vacuum() opens a handle that starts a
194960** new RBU vacuum operation.
194961**
194962** As with sqlite3rbu_open(), Zipvfs users should rever to the comment
194963** describing the sqlite3rbu_create_vfs() API function below for
194964** a description of the complications associated with using RBU with
194965** zipvfs databases.
194966*/
194967SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
194968  const char *zTarget,
194969  const char *zState
194970);
194971
194972/*
194973** Configure a limit for the amount of temp space that may be used by
194974** the RBU handle passed as the first argument. The new limit is specified
194975** in bytes by the second parameter. If it is positive, the limit is updated.
194976** If the second parameter to this function is passed zero, then the limit
194977** is removed entirely. If the second parameter is negative, the limit is
194978** not modified (this is useful for querying the current limit).
194979**
194980** In all cases the returned value is the current limit in bytes (zero
194981** indicates unlimited).
194982**
194983** If the temp space limit is exceeded during operation, an SQLITE_FULL
194984** error is returned.
194985*/
194986SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu*, sqlite3_int64);
194987
194988/*
194989** Return the current amount of temp file space, in bytes, currently used by
194990** the RBU handle passed as the only argument.
194991*/
194992SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu*);
194993
194994/*
194995** Internally, each RBU connection uses a separate SQLite database
194996** connection to access the target and rbu update databases. This
194997** API allows the application direct access to these database handles.
194998**
194999** The first argument passed to this function must be a valid, open, RBU
195000** handle. The second argument should be passed zero to access the target
195001** database handle, or non-zero to access the rbu update database handle.
195002** Accessing the underlying database handles may be useful in the
195003** following scenarios:
195004**
195005**   * If any target tables are virtual tables, it may be necessary to
195006**     call sqlite3_create_module() on the target database handle to
195007**     register the required virtual table implementations.
195008**
195009**   * If the data_xxx tables in the RBU source database are virtual
195010**     tables, the application may need to call sqlite3_create_module() on
195011**     the rbu update db handle to any required virtual table
195012**     implementations.
195013**
195014**   * If the application uses the "rbu_delta()" feature described above,
195015**     it must use sqlite3_create_function() or similar to register the
195016**     rbu_delta() implementation with the target database handle.
195017**
195018** If an error has occurred, either while opening or stepping the RBU object,
195019** this function may return NULL. The error code and message may be collected
195020** when sqlite3rbu_close() is called.
195021**
195022** Database handles returned by this function remain valid until the next
195023** call to any sqlite3rbu_xxx() function other than sqlite3rbu_db().
195024*/
195025SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu*, int bRbu);
195026
195027/*
195028** Do some work towards applying the RBU update to the target db.
195029**
195030** Return SQLITE_DONE if the update has been completely applied, or
195031** SQLITE_OK if no error occurs but there remains work to do to apply
195032** the RBU update. If an error does occur, some other error code is
195033** returned.
195034**
195035** Once a call to sqlite3rbu_step() has returned a value other than
195036** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
195037** that immediately return the same value.
195038*/
195039SQLITE_API int sqlite3rbu_step(sqlite3rbu *pRbu);
195040
195041/*
195042** Force RBU to save its state to disk.
195043**
195044** If a power failure or application crash occurs during an update, following
195045** system recovery RBU may resume the update from the point at which the state
195046** was last saved. In other words, from the most recent successful call to
195047** sqlite3rbu_close() or this function.
195048**
195049** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
195050*/
195051SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *pRbu);
195052
195053/*
195054** Close an RBU handle.
195055**
195056** If the RBU update has been completely applied, mark the RBU database
195057** as fully applied. Otherwise, assuming no error has occurred, save the
195058** current state of the RBU update appliation to the RBU database.
195059**
195060** If an error has already occurred as part of an sqlite3rbu_step()
195061** or sqlite3rbu_open() call, or if one occurs within this function, an
195062** SQLite error code is returned. Additionally, if pzErrmsg is not NULL,
195063** *pzErrmsg may be set to point to a buffer containing a utf-8 formatted
195064** English language error message. It is the responsibility of the caller to
195065** eventually free any such buffer using sqlite3_free().
195066**
195067** Otherwise, if no error occurs, this function returns SQLITE_OK if the
195068** update has been partially applied, or SQLITE_DONE if it has been
195069** completely applied.
195070*/
195071SQLITE_API int sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
195072
195073/*
195074** Return the total number of key-value operations (inserts, deletes or
195075** updates) that have been performed on the target database since the
195076** current RBU update was started.
195077*/
195078SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu);
195079
195080/*
195081** Obtain permyriadage (permyriadage is to 10000 as percentage is to 100)
195082** progress indications for the two stages of an RBU update. This API may
195083** be useful for driving GUI progress indicators and similar.
195084**
195085** An RBU update is divided into two stages:
195086**
195087**   * Stage 1, in which changes are accumulated in an oal/wal file, and
195088**   * Stage 2, in which the contents of the wal file are copied into the
195089**     main database.
195090**
195091** The update is visible to non-RBU clients during stage 2. During stage 1
195092** non-RBU reader clients may see the original database.
195093**
195094** If this API is called during stage 2 of the update, output variable
195095** (*pnOne) is set to 10000 to indicate that stage 1 has finished and (*pnTwo)
195096** to a value between 0 and 10000 to indicate the permyriadage progress of
195097** stage 2. A value of 5000 indicates that stage 2 is half finished,
195098** 9000 indicates that it is 90% finished, and so on.
195099**
195100** If this API is called during stage 1 of the update, output variable
195101** (*pnTwo) is set to 0 to indicate that stage 2 has not yet started. The
195102** value to which (*pnOne) is set depends on whether or not the RBU
195103** database contains an "rbu_count" table. The rbu_count table, if it
195104** exists, must contain the same columns as the following:
195105**
195106**   CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
195107**
195108** There must be one row in the table for each source (data_xxx) table within
195109** the RBU database. The 'tbl' column should contain the name of the source
195110** table. The 'cnt' column should contain the number of rows within the
195111** source table.
195112**
195113** If the rbu_count table is present and populated correctly and this
195114** API is called during stage 1, the *pnOne output variable is set to the
195115** permyriadage progress of the same stage. If the rbu_count table does
195116** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count
195117** table exists but is not correctly populated, the value of the *pnOne
195118** output variable during stage 1 is undefined.
195119*/
195120SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *pRbu, int *pnOne, int*pnTwo);
195121
195122/*
195123** Obtain an indication as to the current stage of an RBU update or vacuum.
195124** This function always returns one of the SQLITE_RBU_STATE_XXX constants
195125** defined in this file. Return values should be interpreted as follows:
195126**
195127** SQLITE_RBU_STATE_OAL:
195128**   RBU is currently building a *-oal file. The next call to sqlite3rbu_step()
195129**   may either add further data to the *-oal file, or compute data that will
195130**   be added by a subsequent call.
195131**
195132** SQLITE_RBU_STATE_MOVE:
195133**   RBU has finished building the *-oal file. The next call to sqlite3rbu_step()
195134**   will move the *-oal file to the equivalent *-wal path. If the current
195135**   operation is an RBU update, then the updated version of the database
195136**   file will become visible to ordinary SQLite clients following the next
195137**   call to sqlite3rbu_step().
195138**
195139** SQLITE_RBU_STATE_CHECKPOINT:
195140**   RBU is currently performing an incremental checkpoint. The next call to
195141**   sqlite3rbu_step() will copy a page of data from the *-wal file into
195142**   the target database file.
195143**
195144** SQLITE_RBU_STATE_DONE:
195145**   The RBU operation has finished. Any subsequent calls to sqlite3rbu_step()
195146**   will immediately return SQLITE_DONE.
195147**
195148** SQLITE_RBU_STATE_ERROR:
195149**   An error has occurred. Any subsequent calls to sqlite3rbu_step() will
195150**   immediately return the SQLite error code associated with the error.
195151*/
195152#define SQLITE_RBU_STATE_OAL        1
195153#define SQLITE_RBU_STATE_MOVE       2
195154#define SQLITE_RBU_STATE_CHECKPOINT 3
195155#define SQLITE_RBU_STATE_DONE       4
195156#define SQLITE_RBU_STATE_ERROR      5
195157
195158SQLITE_API int sqlite3rbu_state(sqlite3rbu *pRbu);
195159
195160/*
195161** Create an RBU VFS named zName that accesses the underlying file-system
195162** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
195163** then the new RBU VFS uses the default system VFS to access the file-system.
195164** The new object is registered as a non-default VFS with SQLite before
195165** returning.
195166**
195167** Part of the RBU implementation uses a custom VFS object. Usually, this
195168** object is created and deleted automatically by RBU.
195169**
195170** The exception is for applications that also use zipvfs. In this case,
195171** the custom VFS must be explicitly created by the user before the RBU
195172** handle is opened. The RBU VFS should be installed so that the zipvfs
195173** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
195174** (for example multiplexor) to access the file-system. For example,
195175** to assemble an RBU enabled VFS stack that uses both zipvfs and
195176** multiplexor (error checking omitted):
195177**
195178**     // Create a VFS named "multiplex" (not the default).
195179**     sqlite3_multiplex_initialize(0, 0);
195180**
195181**     // Create an rbu VFS named "rbu" that uses multiplexor. If the
195182**     // second argument were replaced with NULL, the "rbu" VFS would
195183**     // access the file-system via the system default VFS, bypassing the
195184**     // multiplexor.
195185**     sqlite3rbu_create_vfs("rbu", "multiplex");
195186**
195187**     // Create a zipvfs VFS named "zipvfs" that uses rbu.
195188**     zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
195189**
195190**     // Make zipvfs the default VFS.
195191**     sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
195192**
195193** Because the default VFS created above includes a RBU functionality, it
195194** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
195195** that does not include the RBU layer results in an error.
195196**
195197** The overhead of adding the "rbu" VFS to the system is negligible for
195198** non-RBU users. There is no harm in an application accessing the
195199** file-system via "rbu" all the time, even if it only uses RBU functionality
195200** occasionally.
195201*/
195202SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent);
195203
195204/*
195205** Deregister and destroy an RBU vfs created by an earlier call to
195206** sqlite3rbu_create_vfs().
195207**
195208** VFS objects are not reference counted. If a VFS object is destroyed
195209** before all database handles that use it have been closed, the results
195210** are undefined.
195211*/
195212SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName);
195213
195214#if 0
195215}  /* end of the 'extern "C"' block */
195216#endif
195217
195218#endif /* _SQLITE3RBU_H */
195219
195220/************** End of sqlite3rbu.h ******************************************/
195221/************** Continuing where we left off in sqlite3rbu.c *****************/
195222
195223#if defined(_WIN32_WCE)
195224/* #include "windows.h" */
195225#endif
195226
195227/* Maximum number of prepared UPDATE statements held by this module */
195228#define SQLITE_RBU_UPDATE_CACHESIZE 16
195229
195230/* Delta checksums disabled by default.  Compile with -DRBU_ENABLE_DELTA_CKSUM
195231** to enable checksum verification.
195232*/
195233#ifndef RBU_ENABLE_DELTA_CKSUM
195234# define RBU_ENABLE_DELTA_CKSUM 0
195235#endif
195236
195237/*
195238** Swap two objects of type TYPE.
195239*/
195240#if !defined(SQLITE_AMALGAMATION)
195241# define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
195242#endif
195243
195244/*
195245** The rbu_state table is used to save the state of a partially applied
195246** update so that it can be resumed later. The table consists of integer
195247** keys mapped to values as follows:
195248**
195249** RBU_STATE_STAGE:
195250**   May be set to integer values 1, 2, 4 or 5. As follows:
195251**       1: the *-rbu file is currently under construction.
195252**       2: the *-rbu file has been constructed, but not yet moved
195253**          to the *-wal path.
195254**       4: the checkpoint is underway.
195255**       5: the rbu update has been checkpointed.
195256**
195257** RBU_STATE_TBL:
195258**   Only valid if STAGE==1. The target database name of the table
195259**   currently being written.
195260**
195261** RBU_STATE_IDX:
195262**   Only valid if STAGE==1. The target database name of the index
195263**   currently being written, or NULL if the main table is currently being
195264**   updated.
195265**
195266** RBU_STATE_ROW:
195267**   Only valid if STAGE==1. Number of rows already processed for the current
195268**   table/index.
195269**
195270** RBU_STATE_PROGRESS:
195271**   Trbul number of sqlite3rbu_step() calls made so far as part of this
195272**   rbu update.
195273**
195274** RBU_STATE_CKPT:
195275**   Valid if STAGE==4. The 64-bit checksum associated with the wal-index
195276**   header created by recovering the *-wal file. This is used to detect
195277**   cases when another client appends frames to the *-wal file in the
195278**   middle of an incremental checkpoint (an incremental checkpoint cannot
195279**   be continued if this happens).
195280**
195281** RBU_STATE_COOKIE:
195282**   Valid if STAGE==1. The current change-counter cookie value in the
195283**   target db file.
195284**
195285** RBU_STATE_OALSZ:
195286**   Valid if STAGE==1. The size in bytes of the *-oal file.
195287**
195288** RBU_STATE_DATATBL:
195289**   Only valid if STAGE==1. The RBU database name of the table
195290**   currently being read.
195291*/
195292#define RBU_STATE_STAGE        1
195293#define RBU_STATE_TBL          2
195294#define RBU_STATE_IDX          3
195295#define RBU_STATE_ROW          4
195296#define RBU_STATE_PROGRESS     5
195297#define RBU_STATE_CKPT         6
195298#define RBU_STATE_COOKIE       7
195299#define RBU_STATE_OALSZ        8
195300#define RBU_STATE_PHASEONESTEP 9
195301#define RBU_STATE_DATATBL     10
195302
195303#define RBU_STAGE_OAL         1
195304#define RBU_STAGE_MOVE        2
195305#define RBU_STAGE_CAPTURE     3
195306#define RBU_STAGE_CKPT        4
195307#define RBU_STAGE_DONE        5
195308
195309
195310#define RBU_CREATE_STATE \
195311  "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
195312
195313typedef struct RbuFrame RbuFrame;
195314typedef struct RbuObjIter RbuObjIter;
195315typedef struct RbuState RbuState;
195316typedef struct RbuSpan RbuSpan;
195317typedef struct rbu_vfs rbu_vfs;
195318typedef struct rbu_file rbu_file;
195319typedef struct RbuUpdateStmt RbuUpdateStmt;
195320
195321#if !defined(SQLITE_AMALGAMATION)
195322typedef unsigned int u32;
195323typedef unsigned short u16;
195324typedef unsigned char u8;
195325typedef sqlite3_int64 i64;
195326#endif
195327
195328/*
195329** These values must match the values defined in wal.c for the equivalent
195330** locks. These are not magic numbers as they are part of the SQLite file
195331** format.
195332*/
195333#define WAL_LOCK_WRITE  0
195334#define WAL_LOCK_CKPT   1
195335#define WAL_LOCK_READ0  3
195336
195337#define SQLITE_FCNTL_RBUCNT    5149216
195338
195339/*
195340** A structure to store values read from the rbu_state table in memory.
195341*/
195342struct RbuState {
195343  int eStage;
195344  char *zTbl;
195345  char *zDataTbl;
195346  char *zIdx;
195347  i64 iWalCksum;
195348  int nRow;
195349  i64 nProgress;
195350  u32 iCookie;
195351  i64 iOalSz;
195352  i64 nPhaseOneStep;
195353};
195354
195355struct RbuUpdateStmt {
195356  char *zMask;                    /* Copy of update mask used with pUpdate */
195357  sqlite3_stmt *pUpdate;          /* Last update statement (or NULL) */
195358  RbuUpdateStmt *pNext;
195359};
195360
195361struct RbuSpan {
195362  const char *zSpan;
195363  int nSpan;
195364};
195365
195366/*
195367** An iterator of this type is used to iterate through all objects in
195368** the target database that require updating. For each such table, the
195369** iterator visits, in order:
195370**
195371**     * the table itself,
195372**     * each index of the table (zero or more points to visit), and
195373**     * a special "cleanup table" state.
195374**
195375** abIndexed:
195376**   If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
195377**   it points to an array of flags nTblCol elements in size. The flag is
195378**   set for each column that is either a part of the PK or a part of an
195379**   index. Or clear otherwise.
195380**
195381**   If there are one or more partial indexes on the table, all fields of
195382**   this array set set to 1. This is because in that case, the module has
195383**   no way to tell which fields will be required to add and remove entries
195384**   from the partial indexes.
195385**
195386*/
195387struct RbuObjIter {
195388  sqlite3_stmt *pTblIter;         /* Iterate through tables */
195389  sqlite3_stmt *pIdxIter;         /* Index iterator */
195390  int nTblCol;                    /* Size of azTblCol[] array */
195391  char **azTblCol;                /* Array of unquoted target column names */
195392  char **azTblType;               /* Array of target column types */
195393  int *aiSrcOrder;                /* src table col -> target table col */
195394  u8 *abTblPk;                    /* Array of flags, set on target PK columns */
195395  u8 *abNotNull;                  /* Array of flags, set on NOT NULL columns */
195396  u8 *abIndexed;                  /* Array of flags, set on indexed & PK cols */
195397  int eType;                      /* Table type - an RBU_PK_XXX value */
195398
195399  /* Output variables. zTbl==0 implies EOF. */
195400  int bCleanup;                   /* True in "cleanup" state */
195401  const char *zTbl;               /* Name of target db table */
195402  const char *zDataTbl;           /* Name of rbu db table (or null) */
195403  const char *zIdx;               /* Name of target db index (or null) */
195404  int iTnum;                      /* Root page of current object */
195405  int iPkTnum;                    /* If eType==EXTERNAL, root of PK index */
195406  int bUnique;                    /* Current index is unique */
195407  int nIndex;                     /* Number of aux. indexes on table zTbl */
195408
195409  /* Statements created by rbuObjIterPrepareAll() */
195410  int nCol;                       /* Number of columns in current object */
195411  sqlite3_stmt *pSelect;          /* Source data */
195412  sqlite3_stmt *pInsert;          /* Statement for INSERT operations */
195413  sqlite3_stmt *pDelete;          /* Statement for DELETE ops */
195414  sqlite3_stmt *pTmpInsert;       /* Insert into rbu_tmp_$zDataTbl */
195415  int nIdxCol;
195416  RbuSpan *aIdxCol;
195417  char *zIdxSql;
195418
195419  /* Last UPDATE used (for PK b-tree updates only), or NULL. */
195420  RbuUpdateStmt *pRbuUpdate;
195421};
195422
195423/*
195424** Values for RbuObjIter.eType
195425**
195426**     0: Table does not exist (error)
195427**     1: Table has an implicit rowid.
195428**     2: Table has an explicit IPK column.
195429**     3: Table has an external PK index.
195430**     4: Table is WITHOUT ROWID.
195431**     5: Table is a virtual table.
195432*/
195433#define RBU_PK_NOTABLE        0
195434#define RBU_PK_NONE           1
195435#define RBU_PK_IPK            2
195436#define RBU_PK_EXTERNAL       3
195437#define RBU_PK_WITHOUT_ROWID  4
195438#define RBU_PK_VTAB           5
195439
195440
195441/*
195442** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
195443** one of the following operations.
195444*/
195445#define RBU_INSERT     1          /* Insert on a main table b-tree */
195446#define RBU_DELETE     2          /* Delete a row from a main table b-tree */
195447#define RBU_REPLACE    3          /* Delete and then insert a row */
195448#define RBU_IDX_DELETE 4          /* Delete a row from an aux. index b-tree */
195449#define RBU_IDX_INSERT 5          /* Insert on an aux. index b-tree */
195450
195451#define RBU_UPDATE     6          /* Update a row in a main table b-tree */
195452
195453/*
195454** A single step of an incremental checkpoint - frame iWalFrame of the wal
195455** file should be copied to page iDbPage of the database file.
195456*/
195457struct RbuFrame {
195458  u32 iDbPage;
195459  u32 iWalFrame;
195460};
195461
195462/*
195463** RBU handle.
195464**
195465** nPhaseOneStep:
195466**   If the RBU database contains an rbu_count table, this value is set to
195467**   a running estimate of the number of b-tree operations required to
195468**   finish populating the *-oal file. This allows the sqlite3_bp_progress()
195469**   API to calculate the permyriadage progress of populating the *-oal file
195470**   using the formula:
195471**
195472**     permyriadage = (10000 * nProgress) / nPhaseOneStep
195473**
195474**   nPhaseOneStep is initialized to the sum of:
195475**
195476**     nRow * (nIndex + 1)
195477**
195478**   for all source tables in the RBU database, where nRow is the number
195479**   of rows in the source table and nIndex the number of indexes on the
195480**   corresponding target database table.
195481**
195482**   This estimate is accurate if the RBU update consists entirely of
195483**   INSERT operations. However, it is inaccurate if:
195484**
195485**     * the RBU update contains any UPDATE operations. If the PK specified
195486**       for an UPDATE operation does not exist in the target table, then
195487**       no b-tree operations are required on index b-trees. Or if the
195488**       specified PK does exist, then (nIndex*2) such operations are
195489**       required (one delete and one insert on each index b-tree).
195490**
195491**     * the RBU update contains any DELETE operations for which the specified
195492**       PK does not exist. In this case no operations are required on index
195493**       b-trees.
195494**
195495**     * the RBU update contains REPLACE operations. These are similar to
195496**       UPDATE operations.
195497**
195498**   nPhaseOneStep is updated to account for the conditions above during the
195499**   first pass of each source table. The updated nPhaseOneStep value is
195500**   stored in the rbu_state table if the RBU update is suspended.
195501*/
195502struct sqlite3rbu {
195503  int eStage;                     /* Value of RBU_STATE_STAGE field */
195504  sqlite3 *dbMain;                /* target database handle */
195505  sqlite3 *dbRbu;                 /* rbu database handle */
195506  char *zTarget;                  /* Path to target db */
195507  char *zRbu;                     /* Path to rbu db */
195508  char *zState;                   /* Path to state db (or NULL if zRbu) */
195509  char zStateDb[5];               /* Db name for state ("stat" or "main") */
195510  int rc;                         /* Value returned by last rbu_step() call */
195511  char *zErrmsg;                  /* Error message if rc!=SQLITE_OK */
195512  int nStep;                      /* Rows processed for current object */
195513  int nProgress;                  /* Rows processed for all objects */
195514  RbuObjIter objiter;             /* Iterator for skipping through tbl/idx */
195515  const char *zVfsName;           /* Name of automatically created rbu vfs */
195516  rbu_file *pTargetFd;            /* File handle open on target db */
195517  int nPagePerSector;             /* Pages per sector for pTargetFd */
195518  i64 iOalSz;
195519  i64 nPhaseOneStep;
195520
195521  /* The following state variables are used as part of the incremental
195522  ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
195523  ** function rbuSetupCheckpoint() for details.  */
195524  u32 iMaxFrame;                  /* Largest iWalFrame value in aFrame[] */
195525  u32 mLock;
195526  int nFrame;                     /* Entries in aFrame[] array */
195527  int nFrameAlloc;                /* Allocated size of aFrame[] array */
195528  RbuFrame *aFrame;
195529  int pgsz;
195530  u8 *aBuf;
195531  i64 iWalCksum;
195532  i64 szTemp;                     /* Current size of all temp files in use */
195533  i64 szTempLimit;                /* Total size limit for temp files */
195534
195535  /* Used in RBU vacuum mode only */
195536  int nRbu;                       /* Number of RBU VFS in the stack */
195537  rbu_file *pRbuFd;               /* Fd for main db of dbRbu */
195538};
195539
195540/*
195541** An rbu VFS is implemented using an instance of this structure.
195542**
195543** Variable pRbu is only non-NULL for automatically created RBU VFS objects.
195544** It is NULL for RBU VFS objects created explicitly using
195545** sqlite3rbu_create_vfs(). It is used to track the total amount of temp
195546** space used by the RBU handle.
195547*/
195548struct rbu_vfs {
195549  sqlite3_vfs base;               /* rbu VFS shim methods */
195550  sqlite3_vfs *pRealVfs;          /* Underlying VFS */
195551  sqlite3_mutex *mutex;           /* Mutex to protect pMain */
195552  sqlite3rbu *pRbu;               /* Owner RBU object */
195553  rbu_file *pMain;                /* List of main db files */
195554  rbu_file *pMainRbu;             /* List of main db files with pRbu!=0 */
195555};
195556
195557/*
195558** Each file opened by an rbu VFS is represented by an instance of
195559** the following structure.
195560**
195561** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
195562** "sz" is set to the current size of the database file.
195563*/
195564struct rbu_file {
195565  sqlite3_file base;              /* sqlite3_file methods */
195566  sqlite3_file *pReal;            /* Underlying file handle */
195567  rbu_vfs *pRbuVfs;               /* Pointer to the rbu_vfs object */
195568  sqlite3rbu *pRbu;               /* Pointer to rbu object (rbu target only) */
195569  i64 sz;                         /* Size of file in bytes (temp only) */
195570
195571  int openFlags;                  /* Flags this file was opened with */
195572  u32 iCookie;                    /* Cookie value for main db files */
195573  u8 iWriteVer;                   /* "write-version" value for main db files */
195574  u8 bNolock;                     /* True to fail EXCLUSIVE locks */
195575
195576  int nShm;                       /* Number of entries in apShm[] array */
195577  char **apShm;                   /* Array of mmap'd *-shm regions */
195578  char *zDel;                     /* Delete this when closing file */
195579
195580  const char *zWal;               /* Wal filename for this main db file */
195581  rbu_file *pWalFd;               /* Wal file descriptor for this main db */
195582  rbu_file *pMainNext;            /* Next MAIN_DB file */
195583  rbu_file *pMainRbuNext;         /* Next MAIN_DB file with pRbu!=0 */
195584};
195585
195586/*
195587** True for an RBU vacuum handle, or false otherwise.
195588*/
195589#define rbuIsVacuum(p) ((p)->zTarget==0)
195590
195591
195592/*************************************************************************
195593** The following three functions, found below:
195594**
195595**   rbuDeltaGetInt()
195596**   rbuDeltaChecksum()
195597**   rbuDeltaApply()
195598**
195599** are lifted from the fossil source code (http://fossil-scm.org). They
195600** are used to implement the scalar SQL function rbu_fossil_delta().
195601*/
195602
195603/*
195604** Read bytes from *pz and convert them into a positive integer.  When
195605** finished, leave *pz pointing to the first character past the end of
195606** the integer.  The *pLen parameter holds the length of the string
195607** in *pz and is decremented once for each character in the integer.
195608*/
195609static unsigned int rbuDeltaGetInt(const char **pz, int *pLen){
195610  static const signed char zValue[] = {
195611    -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
195612    -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
195613    -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
195614     0,  1,  2,  3,  4,  5,  6,  7,    8,  9, -1, -1, -1, -1, -1, -1,
195615    -1, 10, 11, 12, 13, 14, 15, 16,   17, 18, 19, 20, 21, 22, 23, 24,
195616    25, 26, 27, 28, 29, 30, 31, 32,   33, 34, 35, -1, -1, -1, -1, 36,
195617    -1, 37, 38, 39, 40, 41, 42, 43,   44, 45, 46, 47, 48, 49, 50, 51,
195618    52, 53, 54, 55, 56, 57, 58, 59,   60, 61, 62, -1, -1, -1, 63, -1,
195619  };
195620  unsigned int v = 0;
195621  int c;
195622  unsigned char *z = (unsigned char*)*pz;
195623  unsigned char *zStart = z;
195624  while( (c = zValue[0x7f&*(z++)])>=0 ){
195625     v = (v<<6) + c;
195626  }
195627  z--;
195628  *pLen -= z - zStart;
195629  *pz = (char*)z;
195630  return v;
195631}
195632
195633#if RBU_ENABLE_DELTA_CKSUM
195634/*
195635** Compute a 32-bit checksum on the N-byte buffer.  Return the result.
195636*/
195637static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){
195638  const unsigned char *z = (const unsigned char *)zIn;
195639  unsigned sum0 = 0;
195640  unsigned sum1 = 0;
195641  unsigned sum2 = 0;
195642  unsigned sum3 = 0;
195643  while(N >= 16){
195644    sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
195645    sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
195646    sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
195647    sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
195648    z += 16;
195649    N -= 16;
195650  }
195651  while(N >= 4){
195652    sum0 += z[0];
195653    sum1 += z[1];
195654    sum2 += z[2];
195655    sum3 += z[3];
195656    z += 4;
195657    N -= 4;
195658  }
195659  sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
195660  switch(N){
195661    case 3:   sum3 += (z[2] << 8);
195662    case 2:   sum3 += (z[1] << 16);
195663    case 1:   sum3 += (z[0] << 24);
195664    default:  ;
195665  }
195666  return sum3;
195667}
195668#endif
195669
195670/*
195671** Apply a delta.
195672**
195673** The output buffer should be big enough to hold the whole output
195674** file and a NUL terminator at the end.  The delta_output_size()
195675** routine will determine this size for you.
195676**
195677** The delta string should be null-terminated.  But the delta string
195678** may contain embedded NUL characters (if the input and output are
195679** binary files) so we also have to pass in the length of the delta in
195680** the lenDelta parameter.
195681**
195682** This function returns the size of the output file in bytes (excluding
195683** the final NUL terminator character).  Except, if the delta string is
195684** malformed or intended for use with a source file other than zSrc,
195685** then this routine returns -1.
195686**
195687** Refer to the delta_create() documentation above for a description
195688** of the delta file format.
195689*/
195690static int rbuDeltaApply(
195691  const char *zSrc,      /* The source or pattern file */
195692  int lenSrc,            /* Length of the source file */
195693  const char *zDelta,    /* Delta to apply to the pattern */
195694  int lenDelta,          /* Length of the delta */
195695  char *zOut             /* Write the output into this preallocated buffer */
195696){
195697  unsigned int limit;
195698  unsigned int total = 0;
195699#if RBU_ENABLE_DELTA_CKSUM
195700  char *zOrigOut = zOut;
195701#endif
195702
195703  limit = rbuDeltaGetInt(&zDelta, &lenDelta);
195704  if( *zDelta!='\n' ){
195705    /* ERROR: size integer not terminated by "\n" */
195706    return -1;
195707  }
195708  zDelta++; lenDelta--;
195709  while( *zDelta && lenDelta>0 ){
195710    unsigned int cnt, ofst;
195711    cnt = rbuDeltaGetInt(&zDelta, &lenDelta);
195712    switch( zDelta[0] ){
195713      case '@': {
195714        zDelta++; lenDelta--;
195715        ofst = rbuDeltaGetInt(&zDelta, &lenDelta);
195716        if( lenDelta>0 && zDelta[0]!=',' ){
195717          /* ERROR: copy command not terminated by ',' */
195718          return -1;
195719        }
195720        zDelta++; lenDelta--;
195721        total += cnt;
195722        if( total>limit ){
195723          /* ERROR: copy exceeds output file size */
195724          return -1;
195725        }
195726        if( (int)(ofst+cnt) > lenSrc ){
195727          /* ERROR: copy extends past end of input */
195728          return -1;
195729        }
195730        memcpy(zOut, &zSrc[ofst], cnt);
195731        zOut += cnt;
195732        break;
195733      }
195734      case ':': {
195735        zDelta++; lenDelta--;
195736        total += cnt;
195737        if( total>limit ){
195738          /* ERROR:  insert command gives an output larger than predicted */
195739          return -1;
195740        }
195741        if( (int)cnt>lenDelta ){
195742          /* ERROR: insert count exceeds size of delta */
195743          return -1;
195744        }
195745        memcpy(zOut, zDelta, cnt);
195746        zOut += cnt;
195747        zDelta += cnt;
195748        lenDelta -= cnt;
195749        break;
195750      }
195751      case ';': {
195752        zDelta++; lenDelta--;
195753        zOut[0] = 0;
195754#if RBU_ENABLE_DELTA_CKSUM
195755        if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){
195756          /* ERROR:  bad checksum */
195757          return -1;
195758        }
195759#endif
195760        if( total!=limit ){
195761          /* ERROR: generated size does not match predicted size */
195762          return -1;
195763        }
195764        return total;
195765      }
195766      default: {
195767        /* ERROR: unknown delta operator */
195768        return -1;
195769      }
195770    }
195771  }
195772  /* ERROR: unterminated delta */
195773  return -1;
195774}
195775
195776static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){
195777  int size;
195778  size = rbuDeltaGetInt(&zDelta, &lenDelta);
195779  if( *zDelta!='\n' ){
195780    /* ERROR: size integer not terminated by "\n" */
195781    return -1;
195782  }
195783  return size;
195784}
195785
195786/*
195787** End of code taken from fossil.
195788*************************************************************************/
195789
195790/*
195791** Implementation of SQL scalar function rbu_fossil_delta().
195792**
195793** This function applies a fossil delta patch to a blob. Exactly two
195794** arguments must be passed to this function. The first is the blob to
195795** patch and the second the patch to apply. If no error occurs, this
195796** function returns the patched blob.
195797*/
195798static void rbuFossilDeltaFunc(
195799  sqlite3_context *context,
195800  int argc,
195801  sqlite3_value **argv
195802){
195803  const char *aDelta;
195804  int nDelta;
195805  const char *aOrig;
195806  int nOrig;
195807
195808  int nOut;
195809  int nOut2;
195810  char *aOut;
195811
195812  assert( argc==2 );
195813
195814  nOrig = sqlite3_value_bytes(argv[0]);
195815  aOrig = (const char*)sqlite3_value_blob(argv[0]);
195816  nDelta = sqlite3_value_bytes(argv[1]);
195817  aDelta = (const char*)sqlite3_value_blob(argv[1]);
195818
195819  /* Figure out the size of the output */
195820  nOut = rbuDeltaOutputSize(aDelta, nDelta);
195821  if( nOut<0 ){
195822    sqlite3_result_error(context, "corrupt fossil delta", -1);
195823    return;
195824  }
195825
195826  aOut = sqlite3_malloc(nOut+1);
195827  if( aOut==0 ){
195828    sqlite3_result_error_nomem(context);
195829  }else{
195830    nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut);
195831    if( nOut2!=nOut ){
195832      sqlite3_free(aOut);
195833      sqlite3_result_error(context, "corrupt fossil delta", -1);
195834    }else{
195835      sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
195836    }
195837  }
195838}
195839
195840
195841/*
195842** Prepare the SQL statement in buffer zSql against database handle db.
195843** If successful, set *ppStmt to point to the new statement and return
195844** SQLITE_OK.
195845**
195846** Otherwise, if an error does occur, set *ppStmt to NULL and return
195847** an SQLite error code. Additionally, set output variable *pzErrmsg to
195848** point to a buffer containing an error message. It is the responsibility
195849** of the caller to (eventually) free this buffer using sqlite3_free().
195850*/
195851static int prepareAndCollectError(
195852  sqlite3 *db,
195853  sqlite3_stmt **ppStmt,
195854  char **pzErrmsg,
195855  const char *zSql
195856){
195857  int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
195858  if( rc!=SQLITE_OK ){
195859    *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
195860    *ppStmt = 0;
195861  }
195862  return rc;
195863}
195864
195865/*
195866** Reset the SQL statement passed as the first argument. Return a copy
195867** of the value returned by sqlite3_reset().
195868**
195869** If an error has occurred, then set *pzErrmsg to point to a buffer
195870** containing an error message. It is the responsibility of the caller
195871** to eventually free this buffer using sqlite3_free().
195872*/
195873static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
195874  int rc = sqlite3_reset(pStmt);
195875  if( rc!=SQLITE_OK ){
195876    *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
195877  }
195878  return rc;
195879}
195880
195881/*
195882** Unless it is NULL, argument zSql points to a buffer allocated using
195883** sqlite3_malloc containing an SQL statement. This function prepares the SQL
195884** statement against database db and frees the buffer. If statement
195885** compilation is successful, *ppStmt is set to point to the new statement
195886** handle and SQLITE_OK is returned.
195887**
195888** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
195889** returned. In this case, *pzErrmsg may also be set to point to an error
195890** message. It is the responsibility of the caller to free this error message
195891** buffer using sqlite3_free().
195892**
195893** If argument zSql is NULL, this function assumes that an OOM has occurred.
195894** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
195895*/
195896static int prepareFreeAndCollectError(
195897  sqlite3 *db,
195898  sqlite3_stmt **ppStmt,
195899  char **pzErrmsg,
195900  char *zSql
195901){
195902  int rc;
195903  assert( *pzErrmsg==0 );
195904  if( zSql==0 ){
195905    rc = SQLITE_NOMEM;
195906    *ppStmt = 0;
195907  }else{
195908    rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
195909    sqlite3_free(zSql);
195910  }
195911  return rc;
195912}
195913
195914/*
195915** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
195916** by an earlier call to rbuObjIterCacheTableInfo().
195917*/
195918static void rbuObjIterFreeCols(RbuObjIter *pIter){
195919  int i;
195920  for(i=0; i<pIter->nTblCol; i++){
195921    sqlite3_free(pIter->azTblCol[i]);
195922    sqlite3_free(pIter->azTblType[i]);
195923  }
195924  sqlite3_free(pIter->azTblCol);
195925  pIter->azTblCol = 0;
195926  pIter->azTblType = 0;
195927  pIter->aiSrcOrder = 0;
195928  pIter->abTblPk = 0;
195929  pIter->abNotNull = 0;
195930  pIter->nTblCol = 0;
195931  pIter->eType = 0;               /* Invalid value */
195932}
195933
195934/*
195935** Finalize all statements and free all allocations that are specific to
195936** the current object (table/index pair).
195937*/
195938static void rbuObjIterClearStatements(RbuObjIter *pIter){
195939  RbuUpdateStmt *pUp;
195940
195941  sqlite3_finalize(pIter->pSelect);
195942  sqlite3_finalize(pIter->pInsert);
195943  sqlite3_finalize(pIter->pDelete);
195944  sqlite3_finalize(pIter->pTmpInsert);
195945  pUp = pIter->pRbuUpdate;
195946  while( pUp ){
195947    RbuUpdateStmt *pTmp = pUp->pNext;
195948    sqlite3_finalize(pUp->pUpdate);
195949    sqlite3_free(pUp);
195950    pUp = pTmp;
195951  }
195952  sqlite3_free(pIter->aIdxCol);
195953  sqlite3_free(pIter->zIdxSql);
195954
195955  pIter->pSelect = 0;
195956  pIter->pInsert = 0;
195957  pIter->pDelete = 0;
195958  pIter->pRbuUpdate = 0;
195959  pIter->pTmpInsert = 0;
195960  pIter->nCol = 0;
195961  pIter->nIdxCol = 0;
195962  pIter->aIdxCol = 0;
195963  pIter->zIdxSql = 0;
195964}
195965
195966/*
195967** Clean up any resources allocated as part of the iterator object passed
195968** as the only argument.
195969*/
195970static void rbuObjIterFinalize(RbuObjIter *pIter){
195971  rbuObjIterClearStatements(pIter);
195972  sqlite3_finalize(pIter->pTblIter);
195973  sqlite3_finalize(pIter->pIdxIter);
195974  rbuObjIterFreeCols(pIter);
195975  memset(pIter, 0, sizeof(RbuObjIter));
195976}
195977
195978/*
195979** Advance the iterator to the next position.
195980**
195981** If no error occurs, SQLITE_OK is returned and the iterator is left
195982** pointing to the next entry. Otherwise, an error code and message is
195983** left in the RBU handle passed as the first argument. A copy of the
195984** error code is returned.
195985*/
195986static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
195987  int rc = p->rc;
195988  if( rc==SQLITE_OK ){
195989
195990    /* Free any SQLite statements used while processing the previous object */
195991    rbuObjIterClearStatements(pIter);
195992    if( pIter->zIdx==0 ){
195993      rc = sqlite3_exec(p->dbMain,
195994          "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
195995          "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
195996          "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
195997          "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
195998          , 0, 0, &p->zErrmsg
195999      );
196000    }
196001
196002    if( rc==SQLITE_OK ){
196003      if( pIter->bCleanup ){
196004        rbuObjIterFreeCols(pIter);
196005        pIter->bCleanup = 0;
196006        rc = sqlite3_step(pIter->pTblIter);
196007        if( rc!=SQLITE_ROW ){
196008          rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
196009          pIter->zTbl = 0;
196010        }else{
196011          pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
196012          pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
196013          rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK : SQLITE_NOMEM;
196014        }
196015      }else{
196016        if( pIter->zIdx==0 ){
196017          sqlite3_stmt *pIdx = pIter->pIdxIter;
196018          rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
196019        }
196020        if( rc==SQLITE_OK ){
196021          rc = sqlite3_step(pIter->pIdxIter);
196022          if( rc!=SQLITE_ROW ){
196023            rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
196024            pIter->bCleanup = 1;
196025            pIter->zIdx = 0;
196026          }else{
196027            pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
196028            pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
196029            pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
196030            rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
196031          }
196032        }
196033      }
196034    }
196035  }
196036
196037  if( rc!=SQLITE_OK ){
196038    rbuObjIterFinalize(pIter);
196039    p->rc = rc;
196040  }
196041  return rc;
196042}
196043
196044
196045/*
196046** The implementation of the rbu_target_name() SQL function. This function
196047** accepts one or two arguments. The first argument is the name of a table -
196048** the name of a table in the RBU database.  The second, if it is present, is 1
196049** for a view or 0 for a table.
196050**
196051** For a non-vacuum RBU handle, if the table name matches the pattern:
196052**
196053**     data[0-9]_<name>
196054**
196055** where <name> is any sequence of 1 or more characters, <name> is returned.
196056** Otherwise, if the only argument does not match the above pattern, an SQL
196057** NULL is returned.
196058**
196059**     "data_t1"     -> "t1"
196060**     "data0123_t2" -> "t2"
196061**     "dataAB_t3"   -> NULL
196062**
196063** For an rbu vacuum handle, a copy of the first argument is returned if
196064** the second argument is either missing or 0 (not a view).
196065*/
196066static void rbuTargetNameFunc(
196067  sqlite3_context *pCtx,
196068  int argc,
196069  sqlite3_value **argv
196070){
196071  sqlite3rbu *p = sqlite3_user_data(pCtx);
196072  const char *zIn;
196073  assert( argc==1 || argc==2 );
196074
196075  zIn = (const char*)sqlite3_value_text(argv[0]);
196076  if( zIn ){
196077    if( rbuIsVacuum(p) ){
196078      assert( argc==2 || argc==1 );
196079      if( argc==1 || 0==sqlite3_value_int(argv[1]) ){
196080        sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC);
196081      }
196082    }else{
196083      if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
196084        int i;
196085        for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
196086        if( zIn[i]=='_' && zIn[i+1] ){
196087          sqlite3_result_text(pCtx, &zIn[i+1], -1, SQLITE_STATIC);
196088        }
196089      }
196090    }
196091  }
196092}
196093
196094/*
196095** Initialize the iterator structure passed as the second argument.
196096**
196097** If no error occurs, SQLITE_OK is returned and the iterator is left
196098** pointing to the first entry. Otherwise, an error code and message is
196099** left in the RBU handle passed as the first argument. A copy of the
196100** error code is returned.
196101*/
196102static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
196103  int rc;
196104  memset(pIter, 0, sizeof(RbuObjIter));
196105
196106  rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
196107    sqlite3_mprintf(
196108      "SELECT rbu_target_name(name, type='view') AS target, name "
196109      "FROM sqlite_master "
196110      "WHERE type IN ('table', 'view') AND target IS NOT NULL "
196111      " %s "
196112      "ORDER BY name"
196113  , rbuIsVacuum(p) ? "AND rootpage!=0 AND rootpage IS NOT NULL" : ""));
196114
196115  if( rc==SQLITE_OK ){
196116    rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
196117        "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
196118        "  FROM main.sqlite_master "
196119        "  WHERE type='index' AND tbl_name = ?"
196120    );
196121  }
196122
196123  pIter->bCleanup = 1;
196124  p->rc = rc;
196125  return rbuObjIterNext(p, pIter);
196126}
196127
196128/*
196129** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
196130** an error code is stored in the RBU handle passed as the first argument.
196131**
196132** If an error has already occurred (p->rc is already set to something other
196133** than SQLITE_OK), then this function returns NULL without modifying the
196134** stored error code. In this case it still calls sqlite3_free() on any
196135** printf() parameters associated with %z conversions.
196136*/
196137static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
196138  char *zSql = 0;
196139  va_list ap;
196140  va_start(ap, zFmt);
196141  zSql = sqlite3_vmprintf(zFmt, ap);
196142  if( p->rc==SQLITE_OK ){
196143    if( zSql==0 ) p->rc = SQLITE_NOMEM;
196144  }else{
196145    sqlite3_free(zSql);
196146    zSql = 0;
196147  }
196148  va_end(ap);
196149  return zSql;
196150}
196151
196152/*
196153** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
196154** arguments are the usual subsitution values. This function performs
196155** the printf() style substitutions and executes the result as an SQL
196156** statement on the RBU handles database.
196157**
196158** If an error occurs, an error code and error message is stored in the
196159** RBU handle. If an error has already occurred when this function is
196160** called, it is a no-op.
196161*/
196162static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
196163  va_list ap;
196164  char *zSql;
196165  va_start(ap, zFmt);
196166  zSql = sqlite3_vmprintf(zFmt, ap);
196167  if( p->rc==SQLITE_OK ){
196168    if( zSql==0 ){
196169      p->rc = SQLITE_NOMEM;
196170    }else{
196171      p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
196172    }
196173  }
196174  sqlite3_free(zSql);
196175  va_end(ap);
196176  return p->rc;
196177}
196178
196179/*
196180** Attempt to allocate and return a pointer to a zeroed block of nByte
196181** bytes.
196182**
196183** If an error (i.e. an OOM condition) occurs, return NULL and leave an
196184** error code in the rbu handle passed as the first argument. Or, if an
196185** error has already occurred when this function is called, return NULL
196186** immediately without attempting the allocation or modifying the stored
196187** error code.
196188*/
196189static void *rbuMalloc(sqlite3rbu *p, sqlite3_int64 nByte){
196190  void *pRet = 0;
196191  if( p->rc==SQLITE_OK ){
196192    assert( nByte>0 );
196193    pRet = sqlite3_malloc64(nByte);
196194    if( pRet==0 ){
196195      p->rc = SQLITE_NOMEM;
196196    }else{
196197      memset(pRet, 0, nByte);
196198    }
196199  }
196200  return pRet;
196201}
196202
196203
196204/*
196205** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
196206** there is room for at least nCol elements. If an OOM occurs, store an
196207** error code in the RBU handle passed as the first argument.
196208*/
196209static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
196210  sqlite3_int64 nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
196211  char **azNew;
196212
196213  azNew = (char**)rbuMalloc(p, nByte);
196214  if( azNew ){
196215    pIter->azTblCol = azNew;
196216    pIter->azTblType = &azNew[nCol];
196217    pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
196218    pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
196219    pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
196220    pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
196221  }
196222}
196223
196224/*
196225** The first argument must be a nul-terminated string. This function
196226** returns a copy of the string in memory obtained from sqlite3_malloc().
196227** It is the responsibility of the caller to eventually free this memory
196228** using sqlite3_free().
196229**
196230** If an OOM condition is encountered when attempting to allocate memory,
196231** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
196232** if the allocation succeeds, (*pRc) is left unchanged.
196233*/
196234static char *rbuStrndup(const char *zStr, int *pRc){
196235  char *zRet = 0;
196236
196237  if( *pRc==SQLITE_OK ){
196238    if( zStr ){
196239      size_t nCopy = strlen(zStr) + 1;
196240      zRet = (char*)sqlite3_malloc64(nCopy);
196241      if( zRet ){
196242        memcpy(zRet, zStr, nCopy);
196243      }else{
196244        *pRc = SQLITE_NOMEM;
196245      }
196246    }
196247  }
196248
196249  return zRet;
196250}
196251
196252/*
196253** Finalize the statement passed as the second argument.
196254**
196255** If the sqlite3_finalize() call indicates that an error occurs, and the
196256** rbu handle error code is not already set, set the error code and error
196257** message accordingly.
196258*/
196259static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
196260  sqlite3 *db = sqlite3_db_handle(pStmt);
196261  int rc = sqlite3_finalize(pStmt);
196262  if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
196263    p->rc = rc;
196264    p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
196265  }
196266}
196267
196268/* Determine the type of a table.
196269**
196270**   peType is of type (int*), a pointer to an output parameter of type
196271**   (int). This call sets the output parameter as follows, depending
196272**   on the type of the table specified by parameters dbName and zTbl.
196273**
196274**     RBU_PK_NOTABLE:       No such table.
196275**     RBU_PK_NONE:          Table has an implicit rowid.
196276**     RBU_PK_IPK:           Table has an explicit IPK column.
196277**     RBU_PK_EXTERNAL:      Table has an external PK index.
196278**     RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
196279**     RBU_PK_VTAB:          Table is a virtual table.
196280**
196281**   Argument *piPk is also of type (int*), and also points to an output
196282**   parameter. Unless the table has an external primary key index
196283**   (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
196284**   if the table does have an external primary key index, then *piPk
196285**   is set to the root page number of the primary key index before
196286**   returning.
196287**
196288** ALGORITHM:
196289**
196290**   if( no entry exists in sqlite_master ){
196291**     return RBU_PK_NOTABLE
196292**   }else if( sql for the entry starts with "CREATE VIRTUAL" ){
196293**     return RBU_PK_VTAB
196294**   }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
196295**     if( the index that is the pk exists in sqlite_master ){
196296**       *piPK = rootpage of that index.
196297**       return RBU_PK_EXTERNAL
196298**     }else{
196299**       return RBU_PK_WITHOUT_ROWID
196300**     }
196301**   }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
196302**     return RBU_PK_IPK
196303**   }else{
196304**     return RBU_PK_NONE
196305**   }
196306*/
196307static void rbuTableType(
196308  sqlite3rbu *p,
196309  const char *zTab,
196310  int *peType,
196311  int *piTnum,
196312  int *piPk
196313){
196314  /*
196315  ** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q)
196316  ** 1) PRAGMA index_list = ?
196317  ** 2) SELECT count(*) FROM sqlite_master where name=%Q
196318  ** 3) PRAGMA table_info = ?
196319  */
196320  sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
196321
196322  *peType = RBU_PK_NOTABLE;
196323  *piPk = 0;
196324
196325  assert( p->rc==SQLITE_OK );
196326  p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
196327    sqlite3_mprintf(
196328          "SELECT (sql LIKE 'create virtual%%'), rootpage"
196329          "  FROM sqlite_master"
196330          " WHERE name=%Q", zTab
196331  ));
196332  if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
196333    /* Either an error, or no such table. */
196334    goto rbuTableType_end;
196335  }
196336  if( sqlite3_column_int(aStmt[0], 0) ){
196337    *peType = RBU_PK_VTAB;                     /* virtual table */
196338    goto rbuTableType_end;
196339  }
196340  *piTnum = sqlite3_column_int(aStmt[0], 1);
196341
196342  p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
196343    sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
196344  );
196345  if( p->rc ) goto rbuTableType_end;
196346  while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
196347    const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
196348    const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
196349    if( zOrig && zIdx && zOrig[0]=='p' ){
196350      p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
196351          sqlite3_mprintf(
196352            "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
196353      ));
196354      if( p->rc==SQLITE_OK ){
196355        if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
196356          *piPk = sqlite3_column_int(aStmt[2], 0);
196357          *peType = RBU_PK_EXTERNAL;
196358        }else{
196359          *peType = RBU_PK_WITHOUT_ROWID;
196360        }
196361      }
196362      goto rbuTableType_end;
196363    }
196364  }
196365
196366  p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
196367    sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
196368  );
196369  if( p->rc==SQLITE_OK ){
196370    while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
196371      if( sqlite3_column_int(aStmt[3],5)>0 ){
196372        *peType = RBU_PK_IPK;                /* explicit IPK column */
196373        goto rbuTableType_end;
196374      }
196375    }
196376    *peType = RBU_PK_NONE;
196377  }
196378
196379rbuTableType_end: {
196380    unsigned int i;
196381    for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
196382      rbuFinalize(p, aStmt[i]);
196383    }
196384  }
196385}
196386
196387/*
196388** This is a helper function for rbuObjIterCacheTableInfo(). It populates
196389** the pIter->abIndexed[] array.
196390*/
196391static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
196392  sqlite3_stmt *pList = 0;
196393  int bIndex = 0;
196394
196395  if( p->rc==SQLITE_OK ){
196396    memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
196397    p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
196398        sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
196399    );
196400  }
196401
196402  pIter->nIndex = 0;
196403  while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
196404    const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
196405    int bPartial = sqlite3_column_int(pList, 4);
196406    sqlite3_stmt *pXInfo = 0;
196407    if( zIdx==0 ) break;
196408    if( bPartial ){
196409      memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
196410    }
196411    p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
196412        sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
196413    );
196414    while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
196415      int iCid = sqlite3_column_int(pXInfo, 1);
196416      if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
196417      if( iCid==-2 ){
196418        memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
196419      }
196420    }
196421    rbuFinalize(p, pXInfo);
196422    bIndex = 1;
196423    pIter->nIndex++;
196424  }
196425
196426  if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
196427    /* "PRAGMA index_list" includes the main PK b-tree */
196428    pIter->nIndex--;
196429  }
196430
196431  rbuFinalize(p, pList);
196432  if( bIndex==0 ) pIter->abIndexed = 0;
196433}
196434
196435
196436/*
196437** If they are not already populated, populate the pIter->azTblCol[],
196438** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
196439** the table (not index) that the iterator currently points to.
196440**
196441** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
196442** an error does occur, an error code and error message are also left in
196443** the RBU handle.
196444*/
196445static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
196446  if( pIter->azTblCol==0 ){
196447    sqlite3_stmt *pStmt = 0;
196448    int nCol = 0;
196449    int i;                        /* for() loop iterator variable */
196450    int bRbuRowid = 0;            /* If input table has column "rbu_rowid" */
196451    int iOrder = 0;
196452    int iTnum = 0;
196453
196454    /* Figure out the type of table this step will deal with. */
196455    assert( pIter->eType==0 );
196456    rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
196457    if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
196458      p->rc = SQLITE_ERROR;
196459      p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
196460    }
196461    if( p->rc ) return p->rc;
196462    if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
196463
196464    assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
196465         || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
196466         || pIter->eType==RBU_PK_VTAB
196467    );
196468
196469    /* Populate the azTblCol[] and nTblCol variables based on the columns
196470    ** of the input table. Ignore any input table columns that begin with
196471    ** "rbu_".  */
196472    p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
196473        sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
196474    );
196475    if( p->rc==SQLITE_OK ){
196476      nCol = sqlite3_column_count(pStmt);
196477      rbuAllocateIterArrays(p, pIter, nCol);
196478    }
196479    for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
196480      const char *zName = (const char*)sqlite3_column_name(pStmt, i);
196481      if( sqlite3_strnicmp("rbu_", zName, 4) ){
196482        char *zCopy = rbuStrndup(zName, &p->rc);
196483        pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
196484        pIter->azTblCol[pIter->nTblCol++] = zCopy;
196485      }
196486      else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
196487        bRbuRowid = 1;
196488      }
196489    }
196490    sqlite3_finalize(pStmt);
196491    pStmt = 0;
196492
196493    if( p->rc==SQLITE_OK
196494     && rbuIsVacuum(p)==0
196495     && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
196496    ){
196497      p->rc = SQLITE_ERROR;
196498      p->zErrmsg = sqlite3_mprintf(
196499          "table %q %s rbu_rowid column", pIter->zDataTbl,
196500          (bRbuRowid ? "may not have" : "requires")
196501      );
196502    }
196503
196504    /* Check that all non-HIDDEN columns in the destination table are also
196505    ** present in the input table. Populate the abTblPk[], azTblType[] and
196506    ** aiTblOrder[] arrays at the same time.  */
196507    if( p->rc==SQLITE_OK ){
196508      p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
196509          sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
196510      );
196511    }
196512    while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
196513      const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
196514      if( zName==0 ) break;  /* An OOM - finalize() below returns S_NOMEM */
196515      for(i=iOrder; i<pIter->nTblCol; i++){
196516        if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
196517      }
196518      if( i==pIter->nTblCol ){
196519        p->rc = SQLITE_ERROR;
196520        p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
196521            pIter->zDataTbl, zName
196522        );
196523      }else{
196524        int iPk = sqlite3_column_int(pStmt, 5);
196525        int bNotNull = sqlite3_column_int(pStmt, 3);
196526        const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
196527
196528        if( i!=iOrder ){
196529          SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
196530          SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
196531        }
196532
196533        pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
196534        assert( iPk>=0 );
196535        pIter->abTblPk[iOrder] = (u8)iPk;
196536        pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
196537        iOrder++;
196538      }
196539    }
196540
196541    rbuFinalize(p, pStmt);
196542    rbuObjIterCacheIndexedCols(p, pIter);
196543    assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
196544    assert( pIter->eType!=RBU_PK_VTAB || pIter->nIndex==0 );
196545  }
196546
196547  return p->rc;
196548}
196549
196550/*
196551** This function constructs and returns a pointer to a nul-terminated
196552** string containing some SQL clause or list based on one or more of the
196553** column names currently stored in the pIter->azTblCol[] array.
196554*/
196555static char *rbuObjIterGetCollist(
196556  sqlite3rbu *p,                  /* RBU object */
196557  RbuObjIter *pIter               /* Object iterator for column names */
196558){
196559  char *zList = 0;
196560  const char *zSep = "";
196561  int i;
196562  for(i=0; i<pIter->nTblCol; i++){
196563    const char *z = pIter->azTblCol[i];
196564    zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
196565    zSep = ", ";
196566  }
196567  return zList;
196568}
196569
196570/*
196571** Return a comma separated list of the quoted PRIMARY KEY column names,
196572** in order, for the current table. Before each column name, add the text
196573** zPre. After each column name, add the zPost text. Use zSeparator as
196574** the separator text (usually ", ").
196575*/
196576static char *rbuObjIterGetPkList(
196577  sqlite3rbu *p,                  /* RBU object */
196578  RbuObjIter *pIter,              /* Object iterator for column names */
196579  const char *zPre,               /* Before each quoted column name */
196580  const char *zSeparator,         /* Separator to use between columns */
196581  const char *zPost               /* After each quoted column name */
196582){
196583  int iPk = 1;
196584  char *zRet = 0;
196585  const char *zSep = "";
196586  while( 1 ){
196587    int i;
196588    for(i=0; i<pIter->nTblCol; i++){
196589      if( (int)pIter->abTblPk[i]==iPk ){
196590        const char *zCol = pIter->azTblCol[i];
196591        zRet = rbuMPrintf(p, "%z%s%s\"%w\"%s", zRet, zSep, zPre, zCol, zPost);
196592        zSep = zSeparator;
196593        break;
196594      }
196595    }
196596    if( i==pIter->nTblCol ) break;
196597    iPk++;
196598  }
196599  return zRet;
196600}
196601
196602/*
196603** This function is called as part of restarting an RBU vacuum within
196604** stage 1 of the process (while the *-oal file is being built) while
196605** updating a table (not an index). The table may be a rowid table or
196606** a WITHOUT ROWID table. It queries the target database to find the
196607** largest key that has already been written to the target table and
196608** constructs a WHERE clause that can be used to extract the remaining
196609** rows from the source table. For a rowid table, the WHERE clause
196610** is of the form:
196611**
196612**     "WHERE _rowid_ > ?"
196613**
196614** and for WITHOUT ROWID tables:
196615**
196616**     "WHERE (key1, key2) > (?, ?)"
196617**
196618** Instead of "?" placeholders, the actual WHERE clauses created by
196619** this function contain literal SQL values.
196620*/
196621static char *rbuVacuumTableStart(
196622  sqlite3rbu *p,                  /* RBU handle */
196623  RbuObjIter *pIter,              /* RBU iterator object */
196624  int bRowid,                     /* True for a rowid table */
196625  const char *zWrite              /* Target table name prefix */
196626){
196627  sqlite3_stmt *pMax = 0;
196628  char *zRet = 0;
196629  if( bRowid ){
196630    p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
196631        sqlite3_mprintf(
196632          "SELECT max(_rowid_) FROM \"%s%w\"", zWrite, pIter->zTbl
196633        )
196634    );
196635    if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
196636      sqlite3_int64 iMax = sqlite3_column_int64(pMax, 0);
196637      zRet = rbuMPrintf(p, " WHERE _rowid_ > %lld ", iMax);
196638    }
196639    rbuFinalize(p, pMax);
196640  }else{
196641    char *zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", " DESC");
196642    char *zSelect = rbuObjIterGetPkList(p, pIter, "quote(", "||','||", ")");
196643    char *zList = rbuObjIterGetPkList(p, pIter, "", ", ", "");
196644
196645    if( p->rc==SQLITE_OK ){
196646      p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
196647          sqlite3_mprintf(
196648            "SELECT %s FROM \"%s%w\" ORDER BY %s LIMIT 1",
196649                zSelect, zWrite, pIter->zTbl, zOrder
196650          )
196651      );
196652      if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
196653        const char *zVal = (const char*)sqlite3_column_text(pMax, 0);
196654        zRet = rbuMPrintf(p, " WHERE (%s) > (%s) ", zList, zVal);
196655      }
196656      rbuFinalize(p, pMax);
196657    }
196658
196659    sqlite3_free(zOrder);
196660    sqlite3_free(zSelect);
196661    sqlite3_free(zList);
196662  }
196663  return zRet;
196664}
196665
196666/*
196667** This function is called as part of restating an RBU vacuum when the
196668** current operation is writing content to an index. If possible, it
196669** queries the target index b-tree for the largest key already written to
196670** it, then composes and returns an expression that can be used in a WHERE
196671** clause to select the remaining required rows from the source table.
196672** It is only possible to return such an expression if:
196673**
196674**   * The index contains no DESC columns, and
196675**   * The last key written to the index before the operation was
196676**     suspended does not contain any NULL values.
196677**
196678** The expression is of the form:
196679**
196680**   (index-field1, index-field2, ...) > (?, ?, ...)
196681**
196682** except that the "?" placeholders are replaced with literal values.
196683**
196684** If the expression cannot be created, NULL is returned. In this case,
196685** the caller has to use an OFFSET clause to extract only the required
196686** rows from the sourct table, just as it does for an RBU update operation.
196687*/
196688char *rbuVacuumIndexStart(
196689  sqlite3rbu *p,                  /* RBU handle */
196690  RbuObjIter *pIter               /* RBU iterator object */
196691){
196692  char *zOrder = 0;
196693  char *zLhs = 0;
196694  char *zSelect = 0;
196695  char *zVector = 0;
196696  char *zRet = 0;
196697  int bFailed = 0;
196698  const char *zSep = "";
196699  int iCol = 0;
196700  sqlite3_stmt *pXInfo = 0;
196701
196702  p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
196703      sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
196704  );
196705  while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
196706    int iCid = sqlite3_column_int(pXInfo, 1);
196707    const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
196708    const char *zCol;
196709    if( sqlite3_column_int(pXInfo, 3) ){
196710      bFailed = 1;
196711      break;
196712    }
196713
196714    if( iCid<0 ){
196715      if( pIter->eType==RBU_PK_IPK ){
196716        int i;
196717        for(i=0; pIter->abTblPk[i]==0; i++);
196718        assert( i<pIter->nTblCol );
196719        zCol = pIter->azTblCol[i];
196720      }else{
196721        zCol = "_rowid_";
196722      }
196723    }else{
196724      zCol = pIter->azTblCol[iCid];
196725    }
196726
196727    zLhs = rbuMPrintf(p, "%z%s \"%w\" COLLATE %Q",
196728        zLhs, zSep, zCol, zCollate
196729        );
196730    zOrder = rbuMPrintf(p, "%z%s \"rbu_imp_%d%w\" COLLATE %Q DESC",
196731        zOrder, zSep, iCol, zCol, zCollate
196732        );
196733    zSelect = rbuMPrintf(p, "%z%s quote(\"rbu_imp_%d%w\")",
196734        zSelect, zSep, iCol, zCol
196735        );
196736    zSep = ", ";
196737    iCol++;
196738  }
196739  rbuFinalize(p, pXInfo);
196740  if( bFailed ) goto index_start_out;
196741
196742  if( p->rc==SQLITE_OK ){
196743    sqlite3_stmt *pSel = 0;
196744
196745    p->rc = prepareFreeAndCollectError(p->dbMain, &pSel, &p->zErrmsg,
196746        sqlite3_mprintf("SELECT %s FROM \"rbu_imp_%w\" ORDER BY %s LIMIT 1",
196747          zSelect, pIter->zTbl, zOrder
196748        )
196749    );
196750    if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSel) ){
196751      zSep = "";
196752      for(iCol=0; iCol<pIter->nCol; iCol++){
196753        const char *zQuoted = (const char*)sqlite3_column_text(pSel, iCol);
196754        if( zQuoted[0]=='N' ){
196755          bFailed = 1;
196756          break;
196757        }
196758        zVector = rbuMPrintf(p, "%z%s%s", zVector, zSep, zQuoted);
196759        zSep = ", ";
196760      }
196761
196762      if( !bFailed ){
196763        zRet = rbuMPrintf(p, "(%s) > (%s)", zLhs, zVector);
196764      }
196765    }
196766    rbuFinalize(p, pSel);
196767  }
196768
196769 index_start_out:
196770  sqlite3_free(zOrder);
196771  sqlite3_free(zSelect);
196772  sqlite3_free(zVector);
196773  sqlite3_free(zLhs);
196774  return zRet;
196775}
196776
196777/*
196778** This function is used to create a SELECT list (the list of SQL
196779** expressions that follows a SELECT keyword) for a SELECT statement
196780** used to read from an data_xxx or rbu_tmp_xxx table while updating the
196781** index object currently indicated by the iterator object passed as the
196782** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
196783** to obtain the required information.
196784**
196785** If the index is of the following form:
196786**
196787**   CREATE INDEX i1 ON t1(c, b COLLATE nocase);
196788**
196789** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
196790** "ipk", the returned string is:
196791**
196792**   "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
196793**
196794** As well as the returned string, three other malloc'd strings are
196795** returned via output parameters. As follows:
196796**
196797**   pzImposterCols: ...
196798**   pzImposterPk: ...
196799**   pzWhere: ...
196800*/
196801static char *rbuObjIterGetIndexCols(
196802  sqlite3rbu *p,                  /* RBU object */
196803  RbuObjIter *pIter,              /* Object iterator for column names */
196804  char **pzImposterCols,          /* OUT: Columns for imposter table */
196805  char **pzImposterPk,            /* OUT: Imposter PK clause */
196806  char **pzWhere,                 /* OUT: WHERE clause */
196807  int *pnBind                     /* OUT: Trbul number of columns */
196808){
196809  int rc = p->rc;                 /* Error code */
196810  int rc2;                        /* sqlite3_finalize() return code */
196811  char *zRet = 0;                 /* String to return */
196812  char *zImpCols = 0;             /* String to return via *pzImposterCols */
196813  char *zImpPK = 0;               /* String to return via *pzImposterPK */
196814  char *zWhere = 0;               /* String to return via *pzWhere */
196815  int nBind = 0;                  /* Value to return via *pnBind */
196816  const char *zCom = "";          /* Set to ", " later on */
196817  const char *zAnd = "";          /* Set to " AND " later on */
196818  sqlite3_stmt *pXInfo = 0;       /* PRAGMA index_xinfo = ? */
196819
196820  if( rc==SQLITE_OK ){
196821    assert( p->zErrmsg==0 );
196822    rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
196823        sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
196824    );
196825  }
196826
196827  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
196828    int iCid = sqlite3_column_int(pXInfo, 1);
196829    int bDesc = sqlite3_column_int(pXInfo, 3);
196830    const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
196831    const char *zCol = 0;
196832    const char *zType;
196833
196834    if( iCid==-2 ){
196835      int iSeq = sqlite3_column_int(pXInfo, 0);
196836      zRet = sqlite3_mprintf("%z%s(%.*s) COLLATE %Q", zRet, zCom,
196837          pIter->aIdxCol[iSeq].nSpan, pIter->aIdxCol[iSeq].zSpan, zCollate
196838      );
196839      zType = "";
196840    }else {
196841      if( iCid<0 ){
196842        /* An integer primary key. If the table has an explicit IPK, use
196843        ** its name. Otherwise, use "rbu_rowid".  */
196844        if( pIter->eType==RBU_PK_IPK ){
196845          int i;
196846          for(i=0; pIter->abTblPk[i]==0; i++);
196847          assert( i<pIter->nTblCol );
196848          zCol = pIter->azTblCol[i];
196849        }else if( rbuIsVacuum(p) ){
196850          zCol = "_rowid_";
196851        }else{
196852          zCol = "rbu_rowid";
196853        }
196854        zType = "INTEGER";
196855      }else{
196856        zCol = pIter->azTblCol[iCid];
196857        zType = pIter->azTblType[iCid];
196858      }
196859      zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom,zCol,zCollate);
196860    }
196861
196862    if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
196863      const char *zOrder = (bDesc ? " DESC" : "");
196864      zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
196865          zImpPK, zCom, nBind, zCol, zOrder
196866      );
196867    }
196868    zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
196869        zImpCols, zCom, nBind, zCol, zType, zCollate
196870    );
196871    zWhere = sqlite3_mprintf(
196872        "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
196873    );
196874    if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
196875    zCom = ", ";
196876    zAnd = " AND ";
196877    nBind++;
196878  }
196879
196880  rc2 = sqlite3_finalize(pXInfo);
196881  if( rc==SQLITE_OK ) rc = rc2;
196882
196883  if( rc!=SQLITE_OK ){
196884    sqlite3_free(zRet);
196885    sqlite3_free(zImpCols);
196886    sqlite3_free(zImpPK);
196887    sqlite3_free(zWhere);
196888    zRet = 0;
196889    zImpCols = 0;
196890    zImpPK = 0;
196891    zWhere = 0;
196892    p->rc = rc;
196893  }
196894
196895  *pzImposterCols = zImpCols;
196896  *pzImposterPk = zImpPK;
196897  *pzWhere = zWhere;
196898  *pnBind = nBind;
196899  return zRet;
196900}
196901
196902/*
196903** Assuming the current table columns are "a", "b" and "c", and the zObj
196904** paramter is passed "old", return a string of the form:
196905**
196906**     "old.a, old.b, old.b"
196907**
196908** With the column names escaped.
196909**
196910** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
196911** the text ", old._rowid_" to the returned value.
196912*/
196913static char *rbuObjIterGetOldlist(
196914  sqlite3rbu *p,
196915  RbuObjIter *pIter,
196916  const char *zObj
196917){
196918  char *zList = 0;
196919  if( p->rc==SQLITE_OK && pIter->abIndexed ){
196920    const char *zS = "";
196921    int i;
196922    for(i=0; i<pIter->nTblCol; i++){
196923      if( pIter->abIndexed[i] ){
196924        const char *zCol = pIter->azTblCol[i];
196925        zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
196926      }else{
196927        zList = sqlite3_mprintf("%z%sNULL", zList, zS);
196928      }
196929      zS = ", ";
196930      if( zList==0 ){
196931        p->rc = SQLITE_NOMEM;
196932        break;
196933      }
196934    }
196935
196936    /* For a table with implicit rowids, append "old._rowid_" to the list. */
196937    if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
196938      zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
196939    }
196940  }
196941  return zList;
196942}
196943
196944/*
196945** Return an expression that can be used in a WHERE clause to match the
196946** primary key of the current table. For example, if the table is:
196947**
196948**   CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
196949**
196950** Return the string:
196951**
196952**   "b = ?1 AND c = ?2"
196953*/
196954static char *rbuObjIterGetWhere(
196955  sqlite3rbu *p,
196956  RbuObjIter *pIter
196957){
196958  char *zList = 0;
196959  if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
196960    zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
196961  }else if( pIter->eType==RBU_PK_EXTERNAL ){
196962    const char *zSep = "";
196963    int i;
196964    for(i=0; i<pIter->nTblCol; i++){
196965      if( pIter->abTblPk[i] ){
196966        zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
196967        zSep = " AND ";
196968      }
196969    }
196970    zList = rbuMPrintf(p,
196971        "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
196972    );
196973
196974  }else{
196975    const char *zSep = "";
196976    int i;
196977    for(i=0; i<pIter->nTblCol; i++){
196978      if( pIter->abTblPk[i] ){
196979        const char *zCol = pIter->azTblCol[i];
196980        zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
196981        zSep = " AND ";
196982      }
196983    }
196984  }
196985  return zList;
196986}
196987
196988/*
196989** The SELECT statement iterating through the keys for the current object
196990** (p->objiter.pSelect) currently points to a valid row. However, there
196991** is something wrong with the rbu_control value in the rbu_control value
196992** stored in the (p->nCol+1)'th column. Set the error code and error message
196993** of the RBU handle to something reflecting this.
196994*/
196995static void rbuBadControlError(sqlite3rbu *p){
196996  p->rc = SQLITE_ERROR;
196997  p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
196998}
196999
197000
197001/*
197002** Return a nul-terminated string containing the comma separated list of
197003** assignments that should be included following the "SET" keyword of
197004** an UPDATE statement used to update the table object that the iterator
197005** passed as the second argument currently points to if the rbu_control
197006** column of the data_xxx table entry is set to zMask.
197007**
197008** The memory for the returned string is obtained from sqlite3_malloc().
197009** It is the responsibility of the caller to eventually free it using
197010** sqlite3_free().
197011**
197012** If an OOM error is encountered when allocating space for the new
197013** string, an error code is left in the rbu handle passed as the first
197014** argument and NULL is returned. Or, if an error has already occurred
197015** when this function is called, NULL is returned immediately, without
197016** attempting the allocation or modifying the stored error code.
197017*/
197018static char *rbuObjIterGetSetlist(
197019  sqlite3rbu *p,
197020  RbuObjIter *pIter,
197021  const char *zMask
197022){
197023  char *zList = 0;
197024  if( p->rc==SQLITE_OK ){
197025    int i;
197026
197027    if( (int)strlen(zMask)!=pIter->nTblCol ){
197028      rbuBadControlError(p);
197029    }else{
197030      const char *zSep = "";
197031      for(i=0; i<pIter->nTblCol; i++){
197032        char c = zMask[pIter->aiSrcOrder[i]];
197033        if( c=='x' ){
197034          zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
197035              zList, zSep, pIter->azTblCol[i], i+1
197036          );
197037          zSep = ", ";
197038        }
197039        else if( c=='d' ){
197040          zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
197041              zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
197042          );
197043          zSep = ", ";
197044        }
197045        else if( c=='f' ){
197046          zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)",
197047              zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
197048          );
197049          zSep = ", ";
197050        }
197051      }
197052    }
197053  }
197054  return zList;
197055}
197056
197057/*
197058** Return a nul-terminated string consisting of nByte comma separated
197059** "?" expressions. For example, if nByte is 3, return a pointer to
197060** a buffer containing the string "?,?,?".
197061**
197062** The memory for the returned string is obtained from sqlite3_malloc().
197063** It is the responsibility of the caller to eventually free it using
197064** sqlite3_free().
197065**
197066** If an OOM error is encountered when allocating space for the new
197067** string, an error code is left in the rbu handle passed as the first
197068** argument and NULL is returned. Or, if an error has already occurred
197069** when this function is called, NULL is returned immediately, without
197070** attempting the allocation or modifying the stored error code.
197071*/
197072static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
197073  char *zRet = 0;
197074  sqlite3_int64 nByte = 2*(sqlite3_int64)nBind + 1;
197075
197076  zRet = (char*)rbuMalloc(p, nByte);
197077  if( zRet ){
197078    int i;
197079    for(i=0; i<nBind; i++){
197080      zRet[i*2] = '?';
197081      zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
197082    }
197083  }
197084  return zRet;
197085}
197086
197087/*
197088** The iterator currently points to a table (not index) of type
197089** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
197090** declaration for the corresponding imposter table. For example,
197091** if the iterator points to a table created as:
197092**
197093**   CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
197094**
197095** this function returns:
197096**
197097**   PRIMARY KEY("b", "a" DESC)
197098*/
197099static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
197100  char *z = 0;
197101  assert( pIter->zIdx==0 );
197102  if( p->rc==SQLITE_OK ){
197103    const char *zSep = "PRIMARY KEY(";
197104    sqlite3_stmt *pXList = 0;     /* PRAGMA index_list = (pIter->zTbl) */
197105    sqlite3_stmt *pXInfo = 0;     /* PRAGMA index_xinfo = <pk-index> */
197106
197107    p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
197108        sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
197109    );
197110    while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
197111      const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
197112      if( zOrig && strcmp(zOrig, "pk")==0 ){
197113        const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
197114        if( zIdx ){
197115          p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
197116              sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
197117          );
197118        }
197119        break;
197120      }
197121    }
197122    rbuFinalize(p, pXList);
197123
197124    while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
197125      if( sqlite3_column_int(pXInfo, 5) ){
197126        /* int iCid = sqlite3_column_int(pXInfo, 0); */
197127        const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
197128        const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
197129        z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
197130        zSep = ", ";
197131      }
197132    }
197133    z = rbuMPrintf(p, "%z)", z);
197134    rbuFinalize(p, pXInfo);
197135  }
197136  return z;
197137}
197138
197139/*
197140** This function creates the second imposter table used when writing to
197141** a table b-tree where the table has an external primary key. If the
197142** iterator passed as the second argument does not currently point to
197143** a table (not index) with an external primary key, this function is a
197144** no-op.
197145**
197146** Assuming the iterator does point to a table with an external PK, this
197147** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
197148** used to access that PK index. For example, if the target table is
197149** declared as follows:
197150**
197151**   CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
197152**
197153** then the imposter table schema is:
197154**
197155**   CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
197156**
197157*/
197158static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
197159  if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
197160    int tnum = pIter->iPkTnum;    /* Root page of PK index */
197161    sqlite3_stmt *pQuery = 0;     /* SELECT name ... WHERE rootpage = $tnum */
197162    const char *zIdx = 0;         /* Name of PK index */
197163    sqlite3_stmt *pXInfo = 0;     /* PRAGMA main.index_xinfo = $zIdx */
197164    const char *zComma = "";
197165    char *zCols = 0;              /* Used to build up list of table cols */
197166    char *zPk = 0;                /* Used to build up table PK declaration */
197167
197168    /* Figure out the name of the primary key index for the current table.
197169    ** This is needed for the argument to "PRAGMA index_xinfo". Set
197170    ** zIdx to point to a nul-terminated string containing this name. */
197171    p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
197172        "SELECT name FROM sqlite_master WHERE rootpage = ?"
197173    );
197174    if( p->rc==SQLITE_OK ){
197175      sqlite3_bind_int(pQuery, 1, tnum);
197176      if( SQLITE_ROW==sqlite3_step(pQuery) ){
197177        zIdx = (const char*)sqlite3_column_text(pQuery, 0);
197178      }
197179    }
197180    if( zIdx ){
197181      p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
197182          sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
197183      );
197184    }
197185    rbuFinalize(p, pQuery);
197186
197187    while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
197188      int bKey = sqlite3_column_int(pXInfo, 5);
197189      if( bKey ){
197190        int iCid = sqlite3_column_int(pXInfo, 1);
197191        int bDesc = sqlite3_column_int(pXInfo, 3);
197192        const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
197193        zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %Q", zCols, zComma,
197194            iCid, pIter->azTblType[iCid], zCollate
197195        );
197196        zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
197197        zComma = ", ";
197198      }
197199    }
197200    zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
197201    rbuFinalize(p, pXInfo);
197202
197203    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
197204    rbuMPrintfExec(p, p->dbMain,
197205        "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
197206        zCols, zPk
197207    );
197208    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
197209  }
197210}
197211
197212/*
197213** If an error has already occurred when this function is called, it
197214** immediately returns zero (without doing any work). Or, if an error
197215** occurs during the execution of this function, it sets the error code
197216** in the sqlite3rbu object indicated by the first argument and returns
197217** zero.
197218**
197219** The iterator passed as the second argument is guaranteed to point to
197220** a table (not an index) when this function is called. This function
197221** attempts to create any imposter table required to write to the main
197222** table b-tree of the table before returning. Non-zero is returned if
197223** an imposter table are created, or zero otherwise.
197224**
197225** An imposter table is required in all cases except RBU_PK_VTAB. Only
197226** virtual tables are written to directly. The imposter table has the
197227** same schema as the actual target table (less any UNIQUE constraints).
197228** More precisely, the "same schema" means the same columns, types,
197229** collation sequences. For tables that do not have an external PRIMARY
197230** KEY, it also means the same PRIMARY KEY declaration.
197231*/
197232static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
197233  if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
197234    int tnum = pIter->iTnum;
197235    const char *zComma = "";
197236    char *zSql = 0;
197237    int iCol;
197238    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
197239
197240    for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
197241      const char *zPk = "";
197242      const char *zCol = pIter->azTblCol[iCol];
197243      const char *zColl = 0;
197244
197245      p->rc = sqlite3_table_column_metadata(
197246          p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
197247      );
197248
197249      if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
197250        /* If the target table column is an "INTEGER PRIMARY KEY", add
197251        ** "PRIMARY KEY" to the imposter table column declaration. */
197252        zPk = "PRIMARY KEY ";
197253      }
197254      zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s",
197255          zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
197256          (pIter->abNotNull[iCol] ? " NOT NULL" : "")
197257      );
197258      zComma = ", ";
197259    }
197260
197261    if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
197262      char *zPk = rbuWithoutRowidPK(p, pIter);
197263      if( zPk ){
197264        zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
197265      }
197266    }
197267
197268    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
197269    rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
197270        pIter->zTbl, zSql,
197271        (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
197272    );
197273    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
197274  }
197275}
197276
197277/*
197278** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
197279** Specifically a statement of the form:
197280**
197281**     INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
197282**
197283** The number of bound variables is equal to the number of columns in
197284** the target table, plus one (for the rbu_control column), plus one more
197285** (for the rbu_rowid column) if the target table is an implicit IPK or
197286** virtual table.
197287*/
197288static void rbuObjIterPrepareTmpInsert(
197289  sqlite3rbu *p,
197290  RbuObjIter *pIter,
197291  const char *zCollist,
197292  const char *zRbuRowid
197293){
197294  int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
197295  char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
197296  if( zBind ){
197297    assert( pIter->pTmpInsert==0 );
197298    p->rc = prepareFreeAndCollectError(
197299        p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
197300          "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
197301          p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
197302    ));
197303  }
197304}
197305
197306static void rbuTmpInsertFunc(
197307  sqlite3_context *pCtx,
197308  int nVal,
197309  sqlite3_value **apVal
197310){
197311  sqlite3rbu *p = sqlite3_user_data(pCtx);
197312  int rc = SQLITE_OK;
197313  int i;
197314
197315  assert( sqlite3_value_int(apVal[0])!=0
197316      || p->objiter.eType==RBU_PK_EXTERNAL
197317      || p->objiter.eType==RBU_PK_NONE
197318  );
197319  if( sqlite3_value_int(apVal[0])!=0 ){
197320    p->nPhaseOneStep += p->objiter.nIndex;
197321  }
197322
197323  for(i=0; rc==SQLITE_OK && i<nVal; i++){
197324    rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
197325  }
197326  if( rc==SQLITE_OK ){
197327    sqlite3_step(p->objiter.pTmpInsert);
197328    rc = sqlite3_reset(p->objiter.pTmpInsert);
197329  }
197330
197331  if( rc!=SQLITE_OK ){
197332    sqlite3_result_error_code(pCtx, rc);
197333  }
197334}
197335
197336static char *rbuObjIterGetIndexWhere(sqlite3rbu *p, RbuObjIter *pIter){
197337  sqlite3_stmt *pStmt = 0;
197338  int rc = p->rc;
197339  char *zRet = 0;
197340
197341  assert( pIter->zIdxSql==0 && pIter->nIdxCol==0 && pIter->aIdxCol==0 );
197342
197343  if( rc==SQLITE_OK ){
197344    rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
197345        "SELECT trim(sql) FROM sqlite_master WHERE type='index' AND name=?"
197346    );
197347  }
197348  if( rc==SQLITE_OK ){
197349    int rc2;
197350    rc = sqlite3_bind_text(pStmt, 1, pIter->zIdx, -1, SQLITE_STATIC);
197351    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
197352      char *zSql = (char*)sqlite3_column_text(pStmt, 0);
197353      if( zSql ){
197354        pIter->zIdxSql = zSql = rbuStrndup(zSql, &rc);
197355      }
197356      if( zSql ){
197357        int nParen = 0;           /* Number of open parenthesis */
197358        int i;
197359        int iIdxCol = 0;
197360        int nIdxAlloc = 0;
197361        for(i=0; zSql[i]; i++){
197362          char c = zSql[i];
197363
197364          /* If necessary, grow the pIter->aIdxCol[] array */
197365          if( iIdxCol==nIdxAlloc ){
197366            RbuSpan *aIdxCol = (RbuSpan*)sqlite3_realloc(
197367                pIter->aIdxCol, (nIdxAlloc+16)*sizeof(RbuSpan)
197368            );
197369            if( aIdxCol==0 ){
197370              rc = SQLITE_NOMEM;
197371              break;
197372            }
197373            pIter->aIdxCol = aIdxCol;
197374            nIdxAlloc += 16;
197375          }
197376
197377          if( c=='(' ){
197378            if( nParen==0 ){
197379              assert( iIdxCol==0 );
197380              pIter->aIdxCol[0].zSpan = &zSql[i+1];
197381            }
197382            nParen++;
197383          }
197384          else if( c==')' ){
197385            nParen--;
197386            if( nParen==0 ){
197387              int nSpan = &zSql[i] - pIter->aIdxCol[iIdxCol].zSpan;
197388              pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
197389              i++;
197390              break;
197391            }
197392          }else if( c==',' && nParen==1 ){
197393            int nSpan = &zSql[i] - pIter->aIdxCol[iIdxCol].zSpan;
197394            pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
197395            pIter->aIdxCol[iIdxCol].zSpan = &zSql[i+1];
197396          }else if( c=='"' || c=='\'' || c=='`' ){
197397            for(i++; 1; i++){
197398              if( zSql[i]==c ){
197399                if( zSql[i+1]!=c ) break;
197400                i++;
197401              }
197402            }
197403          }else if( c=='[' ){
197404            for(i++; 1; i++){
197405              if( zSql[i]==']' ) break;
197406            }
197407          }else if( c=='-' && zSql[i+1]=='-' ){
197408            for(i=i+2; zSql[i] && zSql[i]!='\n'; i++);
197409            if( zSql[i]=='\0' ) break;
197410          }else if( c=='/' && zSql[i+1]=='*' ){
197411            for(i=i+2; zSql[i] && (zSql[i]!='*' || zSql[i+1]!='/'); i++);
197412            if( zSql[i]=='\0' ) break;
197413            i++;
197414          }
197415        }
197416        if( zSql[i] ){
197417          zRet = rbuStrndup(&zSql[i], &rc);
197418        }
197419        pIter->nIdxCol = iIdxCol;
197420      }
197421    }
197422
197423    rc2 = sqlite3_finalize(pStmt);
197424    if( rc==SQLITE_OK ) rc = rc2;
197425  }
197426
197427  p->rc = rc;
197428  return zRet;
197429}
197430
197431/*
197432** Ensure that the SQLite statement handles required to update the
197433** target database object currently indicated by the iterator passed
197434** as the second argument are available.
197435*/
197436static int rbuObjIterPrepareAll(
197437  sqlite3rbu *p,
197438  RbuObjIter *pIter,
197439  int nOffset                     /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
197440){
197441  assert( pIter->bCleanup==0 );
197442  if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
197443    const int tnum = pIter->iTnum;
197444    char *zCollist = 0;           /* List of indexed columns */
197445    char **pz = &p->zErrmsg;
197446    const char *zIdx = pIter->zIdx;
197447    char *zLimit = 0;
197448
197449    if( nOffset ){
197450      zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
197451      if( !zLimit ) p->rc = SQLITE_NOMEM;
197452    }
197453
197454    if( zIdx ){
197455      const char *zTbl = pIter->zTbl;
197456      char *zImposterCols = 0;    /* Columns for imposter table */
197457      char *zImposterPK = 0;      /* Primary key declaration for imposter */
197458      char *zWhere = 0;           /* WHERE clause on PK columns */
197459      char *zBind = 0;
197460      char *zPart = 0;
197461      int nBind = 0;
197462
197463      assert( pIter->eType!=RBU_PK_VTAB );
197464      zPart = rbuObjIterGetIndexWhere(p, pIter);
197465      zCollist = rbuObjIterGetIndexCols(
197466          p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
197467      );
197468      zBind = rbuObjIterGetBindlist(p, nBind);
197469
197470      /* Create the imposter table used to write to this index. */
197471      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
197472      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
197473      rbuMPrintfExec(p, p->dbMain,
197474          "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
197475          zTbl, zImposterCols, zImposterPK
197476      );
197477      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
197478
197479      /* Create the statement to insert index entries */
197480      pIter->nCol = nBind;
197481      if( p->rc==SQLITE_OK ){
197482        p->rc = prepareFreeAndCollectError(
197483            p->dbMain, &pIter->pInsert, &p->zErrmsg,
197484          sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
197485        );
197486      }
197487
197488      /* And to delete index entries */
197489      if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
197490        p->rc = prepareFreeAndCollectError(
197491            p->dbMain, &pIter->pDelete, &p->zErrmsg,
197492          sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
197493        );
197494      }
197495
197496      /* Create the SELECT statement to read keys in sorted order */
197497      if( p->rc==SQLITE_OK ){
197498        char *zSql;
197499        if( rbuIsVacuum(p) ){
197500          char *zStart = 0;
197501          if( nOffset ){
197502            zStart = rbuVacuumIndexStart(p, pIter);
197503            if( zStart ){
197504              sqlite3_free(zLimit);
197505              zLimit = 0;
197506            }
197507          }
197508
197509          zSql = sqlite3_mprintf(
197510              "SELECT %s, 0 AS rbu_control FROM '%q' %s %s %s ORDER BY %s%s",
197511              zCollist,
197512              pIter->zDataTbl,
197513              zPart,
197514              (zStart ? (zPart ? "AND" : "WHERE") : ""), zStart,
197515              zCollist, zLimit
197516          );
197517          sqlite3_free(zStart);
197518        }else
197519
197520        if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
197521          zSql = sqlite3_mprintf(
197522              "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s ORDER BY %s%s",
197523              zCollist, p->zStateDb, pIter->zDataTbl,
197524              zPart, zCollist, zLimit
197525          );
197526        }else{
197527          zSql = sqlite3_mprintf(
197528              "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s "
197529              "UNION ALL "
197530              "SELECT %s, rbu_control FROM '%q' "
197531              "%s %s typeof(rbu_control)='integer' AND rbu_control!=1 "
197532              "ORDER BY %s%s",
197533              zCollist, p->zStateDb, pIter->zDataTbl, zPart,
197534              zCollist, pIter->zDataTbl,
197535              zPart,
197536              (zPart ? "AND" : "WHERE"),
197537              zCollist, zLimit
197538          );
197539        }
197540        if( p->rc==SQLITE_OK ){
197541          p->rc = prepareFreeAndCollectError(p->dbRbu,&pIter->pSelect,pz,zSql);
197542        }else{
197543          sqlite3_free(zSql);
197544        }
197545      }
197546
197547      sqlite3_free(zImposterCols);
197548      sqlite3_free(zImposterPK);
197549      sqlite3_free(zWhere);
197550      sqlite3_free(zBind);
197551      sqlite3_free(zPart);
197552    }else{
197553      int bRbuRowid = (pIter->eType==RBU_PK_VTAB)
197554                    ||(pIter->eType==RBU_PK_NONE)
197555                    ||(pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p));
197556      const char *zTbl = pIter->zTbl;       /* Table this step applies to */
197557      const char *zWrite;                   /* Imposter table name */
197558
197559      char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
197560      char *zWhere = rbuObjIterGetWhere(p, pIter);
197561      char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
197562      char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
197563
197564      zCollist = rbuObjIterGetCollist(p, pIter);
197565      pIter->nCol = pIter->nTblCol;
197566
197567      /* Create the imposter table or tables (if required). */
197568      rbuCreateImposterTable(p, pIter);
197569      rbuCreateImposterTable2(p, pIter);
197570      zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
197571
197572      /* Create the INSERT statement to write to the target PK b-tree */
197573      if( p->rc==SQLITE_OK ){
197574        p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
197575            sqlite3_mprintf(
197576              "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
197577              zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
197578            )
197579        );
197580      }
197581
197582      /* Create the DELETE statement to write to the target PK b-tree.
197583      ** Because it only performs INSERT operations, this is not required for
197584      ** an rbu vacuum handle.  */
197585      if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
197586        p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
197587            sqlite3_mprintf(
197588              "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
197589            )
197590        );
197591      }
197592
197593      if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
197594        const char *zRbuRowid = "";
197595        if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
197596          zRbuRowid = ", rbu_rowid";
197597        }
197598
197599        /* Create the rbu_tmp_xxx table and the triggers to populate it. */
197600        rbuMPrintfExec(p, p->dbRbu,
197601            "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
197602            "SELECT *%s FROM '%q' WHERE 0;"
197603            , p->zStateDb, pIter->zDataTbl
197604            , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
197605            , pIter->zDataTbl
197606        );
197607
197608        rbuMPrintfExec(p, p->dbMain,
197609            "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
197610            "BEGIN "
197611            "  SELECT rbu_tmp_insert(3, %s);"
197612            "END;"
197613
197614            "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
197615            "BEGIN "
197616            "  SELECT rbu_tmp_insert(3, %s);"
197617            "END;"
197618
197619            "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
197620            "BEGIN "
197621            "  SELECT rbu_tmp_insert(4, %s);"
197622            "END;",
197623            zWrite, zTbl, zOldlist,
197624            zWrite, zTbl, zOldlist,
197625            zWrite, zTbl, zNewlist
197626        );
197627
197628        if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
197629          rbuMPrintfExec(p, p->dbMain,
197630              "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
197631              "BEGIN "
197632              "  SELECT rbu_tmp_insert(0, %s);"
197633              "END;",
197634              zWrite, zTbl, zNewlist
197635          );
197636        }
197637
197638        rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
197639      }
197640
197641      /* Create the SELECT statement to read keys from data_xxx */
197642      if( p->rc==SQLITE_OK ){
197643        const char *zRbuRowid = "";
197644        char *zStart = 0;
197645        char *zOrder = 0;
197646        if( bRbuRowid ){
197647          zRbuRowid = rbuIsVacuum(p) ? ",_rowid_ " : ",rbu_rowid";
197648        }
197649
197650        if( rbuIsVacuum(p) ){
197651          if( nOffset ){
197652            zStart = rbuVacuumTableStart(p, pIter, bRbuRowid, zWrite);
197653            if( zStart ){
197654              sqlite3_free(zLimit);
197655              zLimit = 0;
197656            }
197657          }
197658          if( bRbuRowid ){
197659            zOrder = rbuMPrintf(p, "_rowid_");
197660          }else{
197661            zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", "");
197662          }
197663        }
197664
197665        if( p->rc==SQLITE_OK ){
197666          p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
197667              sqlite3_mprintf(
197668                "SELECT %s,%s rbu_control%s FROM '%q'%s %s %s %s",
197669                zCollist,
197670                (rbuIsVacuum(p) ? "0 AS " : ""),
197671                zRbuRowid,
197672                pIter->zDataTbl, (zStart ? zStart : ""),
197673                (zOrder ? "ORDER BY" : ""), zOrder,
197674                zLimit
197675              )
197676          );
197677        }
197678        sqlite3_free(zStart);
197679        sqlite3_free(zOrder);
197680      }
197681
197682      sqlite3_free(zWhere);
197683      sqlite3_free(zOldlist);
197684      sqlite3_free(zNewlist);
197685      sqlite3_free(zBindings);
197686    }
197687    sqlite3_free(zCollist);
197688    sqlite3_free(zLimit);
197689  }
197690
197691  return p->rc;
197692}
197693
197694/*
197695** Set output variable *ppStmt to point to an UPDATE statement that may
197696** be used to update the imposter table for the main table b-tree of the
197697** table object that pIter currently points to, assuming that the
197698** rbu_control column of the data_xyz table contains zMask.
197699**
197700** If the zMask string does not specify any columns to update, then this
197701** is not an error. Output variable *ppStmt is set to NULL in this case.
197702*/
197703static int rbuGetUpdateStmt(
197704  sqlite3rbu *p,                  /* RBU handle */
197705  RbuObjIter *pIter,              /* Object iterator */
197706  const char *zMask,              /* rbu_control value ('x.x.') */
197707  sqlite3_stmt **ppStmt           /* OUT: UPDATE statement handle */
197708){
197709  RbuUpdateStmt **pp;
197710  RbuUpdateStmt *pUp = 0;
197711  int nUp = 0;
197712
197713  /* In case an error occurs */
197714  *ppStmt = 0;
197715
197716  /* Search for an existing statement. If one is found, shift it to the front
197717  ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
197718  ** to the number of statements currently in the cache and pUp to the
197719  ** last object in the list.  */
197720  for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
197721    pUp = *pp;
197722    if( strcmp(pUp->zMask, zMask)==0 ){
197723      *pp = pUp->pNext;
197724      pUp->pNext = pIter->pRbuUpdate;
197725      pIter->pRbuUpdate = pUp;
197726      *ppStmt = pUp->pUpdate;
197727      return SQLITE_OK;
197728    }
197729    nUp++;
197730  }
197731  assert( pUp==0 || pUp->pNext==0 );
197732
197733  if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
197734    for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
197735    *pp = 0;
197736    sqlite3_finalize(pUp->pUpdate);
197737    pUp->pUpdate = 0;
197738  }else{
197739    pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
197740  }
197741
197742  if( pUp ){
197743    char *zWhere = rbuObjIterGetWhere(p, pIter);
197744    char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
197745    char *zUpdate = 0;
197746
197747    pUp->zMask = (char*)&pUp[1];
197748    memcpy(pUp->zMask, zMask, pIter->nTblCol);
197749    pUp->pNext = pIter->pRbuUpdate;
197750    pIter->pRbuUpdate = pUp;
197751
197752    if( zSet ){
197753      const char *zPrefix = "";
197754
197755      if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
197756      zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
197757          zPrefix, pIter->zTbl, zSet, zWhere
197758      );
197759      p->rc = prepareFreeAndCollectError(
197760          p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
197761      );
197762      *ppStmt = pUp->pUpdate;
197763    }
197764    sqlite3_free(zWhere);
197765    sqlite3_free(zSet);
197766  }
197767
197768  return p->rc;
197769}
197770
197771static sqlite3 *rbuOpenDbhandle(
197772  sqlite3rbu *p,
197773  const char *zName,
197774  int bUseVfs
197775){
197776  sqlite3 *db = 0;
197777  if( p->rc==SQLITE_OK ){
197778    const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
197779    p->rc = sqlite3_open_v2(zName, &db, flags, bUseVfs ? p->zVfsName : 0);
197780    if( p->rc ){
197781      p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
197782      sqlite3_close(db);
197783      db = 0;
197784    }
197785  }
197786  return db;
197787}
197788
197789/*
197790** Free an RbuState object allocated by rbuLoadState().
197791*/
197792static void rbuFreeState(RbuState *p){
197793  if( p ){
197794    sqlite3_free(p->zTbl);
197795    sqlite3_free(p->zDataTbl);
197796    sqlite3_free(p->zIdx);
197797    sqlite3_free(p);
197798  }
197799}
197800
197801/*
197802** Allocate an RbuState object and load the contents of the rbu_state
197803** table into it. Return a pointer to the new object. It is the
197804** responsibility of the caller to eventually free the object using
197805** sqlite3_free().
197806**
197807** If an error occurs, leave an error code and message in the rbu handle
197808** and return NULL.
197809*/
197810static RbuState *rbuLoadState(sqlite3rbu *p){
197811  RbuState *pRet = 0;
197812  sqlite3_stmt *pStmt = 0;
197813  int rc;
197814  int rc2;
197815
197816  pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
197817  if( pRet==0 ) return 0;
197818
197819  rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
197820      sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
197821  );
197822  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
197823    switch( sqlite3_column_int(pStmt, 0) ){
197824      case RBU_STATE_STAGE:
197825        pRet->eStage = sqlite3_column_int(pStmt, 1);
197826        if( pRet->eStage!=RBU_STAGE_OAL
197827         && pRet->eStage!=RBU_STAGE_MOVE
197828         && pRet->eStage!=RBU_STAGE_CKPT
197829        ){
197830          p->rc = SQLITE_CORRUPT;
197831        }
197832        break;
197833
197834      case RBU_STATE_TBL:
197835        pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
197836        break;
197837
197838      case RBU_STATE_IDX:
197839        pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
197840        break;
197841
197842      case RBU_STATE_ROW:
197843        pRet->nRow = sqlite3_column_int(pStmt, 1);
197844        break;
197845
197846      case RBU_STATE_PROGRESS:
197847        pRet->nProgress = sqlite3_column_int64(pStmt, 1);
197848        break;
197849
197850      case RBU_STATE_CKPT:
197851        pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
197852        break;
197853
197854      case RBU_STATE_COOKIE:
197855        pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
197856        break;
197857
197858      case RBU_STATE_OALSZ:
197859        pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
197860        break;
197861
197862      case RBU_STATE_PHASEONESTEP:
197863        pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
197864        break;
197865
197866      case RBU_STATE_DATATBL:
197867        pRet->zDataTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
197868        break;
197869
197870      default:
197871        rc = SQLITE_CORRUPT;
197872        break;
197873    }
197874  }
197875  rc2 = sqlite3_finalize(pStmt);
197876  if( rc==SQLITE_OK ) rc = rc2;
197877
197878  p->rc = rc;
197879  return pRet;
197880}
197881
197882
197883/*
197884** Open the database handle and attach the RBU database as "rbu". If an
197885** error occurs, leave an error code and message in the RBU handle.
197886*/
197887static void rbuOpenDatabase(sqlite3rbu *p, int *pbRetry){
197888  assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
197889  assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
197890
197891  /* Open the RBU database */
197892  p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
197893
197894  if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
197895    sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
197896    if( p->zState==0 ){
197897      const char *zFile = sqlite3_db_filename(p->dbRbu, "main");
197898      p->zState = rbuMPrintf(p, "file://%s-vacuum?modeof=%s", zFile, zFile);
197899    }
197900  }
197901
197902  /* If using separate RBU and state databases, attach the state database to
197903  ** the RBU db handle now.  */
197904  if( p->zState ){
197905    rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
197906    memcpy(p->zStateDb, "stat", 4);
197907  }else{
197908    memcpy(p->zStateDb, "main", 4);
197909  }
197910
197911#if 0
197912  if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
197913    p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, 0);
197914  }
197915#endif
197916
197917  /* If it has not already been created, create the rbu_state table */
197918  rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
197919
197920#if 0
197921  if( rbuIsVacuum(p) ){
197922    if( p->rc==SQLITE_OK ){
197923      int rc2;
197924      int bOk = 0;
197925      sqlite3_stmt *pCnt = 0;
197926      p->rc = prepareAndCollectError(p->dbRbu, &pCnt, &p->zErrmsg,
197927          "SELECT count(*) FROM stat.sqlite_master"
197928      );
197929      if( p->rc==SQLITE_OK
197930       && sqlite3_step(pCnt)==SQLITE_ROW
197931       && 1==sqlite3_column_int(pCnt, 0)
197932      ){
197933        bOk = 1;
197934      }
197935      rc2 = sqlite3_finalize(pCnt);
197936      if( p->rc==SQLITE_OK ) p->rc = rc2;
197937
197938      if( p->rc==SQLITE_OK && bOk==0 ){
197939        p->rc = SQLITE_ERROR;
197940        p->zErrmsg = sqlite3_mprintf("invalid state database");
197941      }
197942
197943      if( p->rc==SQLITE_OK ){
197944        p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
197945      }
197946    }
197947  }
197948#endif
197949
197950  if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
197951    int bOpen = 0;
197952    int rc;
197953    p->nRbu = 0;
197954    p->pRbuFd = 0;
197955    rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
197956    if( rc!=SQLITE_NOTFOUND ) p->rc = rc;
197957    if( p->eStage>=RBU_STAGE_MOVE ){
197958      bOpen = 1;
197959    }else{
197960      RbuState *pState = rbuLoadState(p);
197961      if( pState ){
197962        bOpen = (pState->eStage>=RBU_STAGE_MOVE);
197963        rbuFreeState(pState);
197964      }
197965    }
197966    if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
197967  }
197968
197969  p->eStage = 0;
197970  if( p->rc==SQLITE_OK && p->dbMain==0 ){
197971    if( !rbuIsVacuum(p) ){
197972      p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
197973    }else if( p->pRbuFd->pWalFd ){
197974      if( pbRetry ){
197975        p->pRbuFd->bNolock = 0;
197976        sqlite3_close(p->dbRbu);
197977        sqlite3_close(p->dbMain);
197978        p->dbMain = 0;
197979        p->dbRbu = 0;
197980        *pbRetry = 1;
197981        return;
197982      }
197983      p->rc = SQLITE_ERROR;
197984      p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
197985    }else{
197986      char *zTarget;
197987      char *zExtra = 0;
197988      if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
197989        zExtra = &p->zRbu[5];
197990        while( *zExtra ){
197991          if( *zExtra++=='?' ) break;
197992        }
197993        if( *zExtra=='\0' ) zExtra = 0;
197994      }
197995
197996      zTarget = sqlite3_mprintf("file:%s-vactmp?rbu_memory=1%s%s",
197997          sqlite3_db_filename(p->dbRbu, "main"),
197998          (zExtra==0 ? "" : "&"), (zExtra==0 ? "" : zExtra)
197999      );
198000
198001      if( zTarget==0 ){
198002        p->rc = SQLITE_NOMEM;
198003        return;
198004      }
198005      p->dbMain = rbuOpenDbhandle(p, zTarget, p->nRbu<=1);
198006      sqlite3_free(zTarget);
198007    }
198008  }
198009
198010  if( p->rc==SQLITE_OK ){
198011    p->rc = sqlite3_create_function(p->dbMain,
198012        "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
198013    );
198014  }
198015
198016  if( p->rc==SQLITE_OK ){
198017    p->rc = sqlite3_create_function(p->dbMain,
198018        "rbu_fossil_delta", 2, SQLITE_UTF8, 0, rbuFossilDeltaFunc, 0, 0
198019    );
198020  }
198021
198022  if( p->rc==SQLITE_OK ){
198023    p->rc = sqlite3_create_function(p->dbRbu,
198024        "rbu_target_name", -1, SQLITE_UTF8, (void*)p, rbuTargetNameFunc, 0, 0
198025    );
198026  }
198027
198028  if( p->rc==SQLITE_OK ){
198029    p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
198030  }
198031  rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
198032
198033  /* Mark the database file just opened as an RBU target database. If
198034  ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
198035  ** This is an error.  */
198036  if( p->rc==SQLITE_OK ){
198037    p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
198038  }
198039
198040  if( p->rc==SQLITE_NOTFOUND ){
198041    p->rc = SQLITE_ERROR;
198042    p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
198043  }
198044}
198045
198046/*
198047** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
198048** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
198049**
198050** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
198051** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
198052** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
198053** three characters, then shorten the suffix on z[] to be the last three
198054** characters of the original suffix.
198055**
198056** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
198057** do the suffix shortening regardless of URI parameter.
198058**
198059** Examples:
198060**
198061**     test.db-journal    =>   test.nal
198062**     test.db-wal        =>   test.wal
198063**     test.db-shm        =>   test.shm
198064**     test.db-mj7f3319fa =>   test.9fa
198065*/
198066static void rbuFileSuffix3(const char *zBase, char *z){
198067#ifdef SQLITE_ENABLE_8_3_NAMES
198068#if SQLITE_ENABLE_8_3_NAMES<2
198069  if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
198070#endif
198071  {
198072    int i, sz;
198073    sz = (int)strlen(z)&0xffffff;
198074    for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
198075    if( z[i]=='.' && sz>i+4 ) memmove(&z[i+1], &z[sz-3], 4);
198076  }
198077#endif
198078}
198079
198080/*
198081** Return the current wal-index header checksum for the target database
198082** as a 64-bit integer.
198083**
198084** The checksum is store in the first page of xShmMap memory as an 8-byte
198085** blob starting at byte offset 40.
198086*/
198087static i64 rbuShmChecksum(sqlite3rbu *p){
198088  i64 iRet = 0;
198089  if( p->rc==SQLITE_OK ){
198090    sqlite3_file *pDb = p->pTargetFd->pReal;
198091    u32 volatile *ptr;
198092    p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
198093    if( p->rc==SQLITE_OK ){
198094      iRet = ((i64)ptr[10] << 32) + ptr[11];
198095    }
198096  }
198097  return iRet;
198098}
198099
198100/*
198101** This function is called as part of initializing or reinitializing an
198102** incremental checkpoint.
198103**
198104** It populates the sqlite3rbu.aFrame[] array with the set of
198105** (wal frame -> db page) copy operations required to checkpoint the
198106** current wal file, and obtains the set of shm locks required to safely
198107** perform the copy operations directly on the file-system.
198108**
198109** If argument pState is not NULL, then the incremental checkpoint is
198110** being resumed. In this case, if the checksum of the wal-index-header
198111** following recovery is not the same as the checksum saved in the RbuState
198112** object, then the rbu handle is set to DONE state. This occurs if some
198113** other client appends a transaction to the wal file in the middle of
198114** an incremental checkpoint.
198115*/
198116static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
198117
198118  /* If pState is NULL, then the wal file may not have been opened and
198119  ** recovered. Running a read-statement here to ensure that doing so
198120  ** does not interfere with the "capture" process below.  */
198121  if( pState==0 ){
198122    p->eStage = 0;
198123    if( p->rc==SQLITE_OK ){
198124      p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
198125    }
198126  }
198127
198128  /* Assuming no error has occurred, run a "restart" checkpoint with the
198129  ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
198130  ** special behaviour in the rbu VFS:
198131  **
198132  **   * If the exclusive shm WRITER or READ0 lock cannot be obtained,
198133  **     the checkpoint fails with SQLITE_BUSY (normally SQLite would
198134  **     proceed with running a passive checkpoint instead of failing).
198135  **
198136  **   * Attempts to read from the *-wal file or write to the database file
198137  **     do not perform any IO. Instead, the frame/page combinations that
198138  **     would be read/written are recorded in the sqlite3rbu.aFrame[]
198139  **     array.
198140  **
198141  **   * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
198142  **     READ0 and CHECKPOINT locks taken as part of the checkpoint are
198143  **     no-ops. These locks will not be released until the connection
198144  **     is closed.
198145  **
198146  **   * Attempting to xSync() the database file causes an SQLITE_INTERNAL
198147  **     error.
198148  **
198149  ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
198150  ** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
198151  ** array populated with a set of (frame -> page) mappings. Because the
198152  ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
198153  ** data from the wal file into the database file according to the
198154  ** contents of aFrame[].
198155  */
198156  if( p->rc==SQLITE_OK ){
198157    int rc2;
198158    p->eStage = RBU_STAGE_CAPTURE;
198159    rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
198160    if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
198161  }
198162
198163  if( p->rc==SQLITE_OK && p->nFrame>0 ){
198164    p->eStage = RBU_STAGE_CKPT;
198165    p->nStep = (pState ? pState->nRow : 0);
198166    p->aBuf = rbuMalloc(p, p->pgsz);
198167    p->iWalCksum = rbuShmChecksum(p);
198168  }
198169
198170  if( p->rc==SQLITE_OK ){
198171    if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
198172      p->rc = SQLITE_DONE;
198173      p->eStage = RBU_STAGE_DONE;
198174    }else{
198175      int nSectorSize;
198176      sqlite3_file *pDb = p->pTargetFd->pReal;
198177      sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
198178      assert( p->nPagePerSector==0 );
198179      nSectorSize = pDb->pMethods->xSectorSize(pDb);
198180      if( nSectorSize>p->pgsz ){
198181        p->nPagePerSector = nSectorSize / p->pgsz;
198182      }else{
198183        p->nPagePerSector = 1;
198184      }
198185
198186      /* Call xSync() on the wal file. This causes SQLite to sync the
198187      ** directory in which the target database and the wal file reside, in
198188      ** case it has not been synced since the rename() call in
198189      ** rbuMoveOalFile(). */
198190      p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL);
198191    }
198192  }
198193}
198194
198195/*
198196** Called when iAmt bytes are read from offset iOff of the wal file while
198197** the rbu object is in capture mode. Record the frame number of the frame
198198** being read in the aFrame[] array.
198199*/
198200static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
198201  const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
198202  u32 iFrame;
198203
198204  if( pRbu->mLock!=mReq ){
198205    pRbu->rc = SQLITE_BUSY;
198206    return SQLITE_INTERNAL;
198207  }
198208
198209  pRbu->pgsz = iAmt;
198210  if( pRbu->nFrame==pRbu->nFrameAlloc ){
198211    int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
198212    RbuFrame *aNew;
198213    aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
198214    if( aNew==0 ) return SQLITE_NOMEM;
198215    pRbu->aFrame = aNew;
198216    pRbu->nFrameAlloc = nNew;
198217  }
198218
198219  iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
198220  if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
198221  pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
198222  pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
198223  pRbu->nFrame++;
198224  return SQLITE_OK;
198225}
198226
198227/*
198228** Called when a page of data is written to offset iOff of the database
198229** file while the rbu handle is in capture mode. Record the page number
198230** of the page being written in the aFrame[] array.
198231*/
198232static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
198233  pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
198234  return SQLITE_OK;
198235}
198236
198237/*
198238** This is called as part of an incremental checkpoint operation. Copy
198239** a single frame of data from the wal file into the database file, as
198240** indicated by the RbuFrame object.
198241*/
198242static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
198243  sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
198244  sqlite3_file *pDb = p->pTargetFd->pReal;
198245  i64 iOff;
198246
198247  assert( p->rc==SQLITE_OK );
198248  iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
198249  p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
198250  if( p->rc ) return;
198251
198252  iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
198253  p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
198254}
198255
198256
198257/*
198258** Take an EXCLUSIVE lock on the database file.
198259*/
198260static void rbuLockDatabase(sqlite3rbu *p){
198261  sqlite3_file *pReal = p->pTargetFd->pReal;
198262  assert( p->rc==SQLITE_OK );
198263  p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
198264  if( p->rc==SQLITE_OK ){
198265    p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
198266  }
198267}
198268
198269#if defined(_WIN32_WCE)
198270static LPWSTR rbuWinUtf8ToUnicode(const char *zFilename){
198271  int nChar;
198272  LPWSTR zWideFilename;
198273
198274  nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
198275  if( nChar==0 ){
198276    return 0;
198277  }
198278  zWideFilename = sqlite3_malloc64( nChar*sizeof(zWideFilename[0]) );
198279  if( zWideFilename==0 ){
198280    return 0;
198281  }
198282  memset(zWideFilename, 0, nChar*sizeof(zWideFilename[0]));
198283  nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
198284                                nChar);
198285  if( nChar==0 ){
198286    sqlite3_free(zWideFilename);
198287    zWideFilename = 0;
198288  }
198289  return zWideFilename;
198290}
198291#endif
198292
198293/*
198294** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
198295** on the database file. This proc moves the *-oal file to the *-wal path,
198296** then reopens the database file (this time in vanilla, non-oal, WAL mode).
198297** If an error occurs, leave an error code and error message in the rbu
198298** handle.
198299*/
198300static void rbuMoveOalFile(sqlite3rbu *p){
198301  const char *zBase = sqlite3_db_filename(p->dbMain, "main");
198302  const char *zMove = zBase;
198303  char *zOal;
198304  char *zWal;
198305
198306  if( rbuIsVacuum(p) ){
198307    zMove = sqlite3_db_filename(p->dbRbu, "main");
198308  }
198309  zOal = sqlite3_mprintf("%s-oal", zMove);
198310  zWal = sqlite3_mprintf("%s-wal", zMove);
198311
198312  assert( p->eStage==RBU_STAGE_MOVE );
198313  assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
198314  if( zWal==0 || zOal==0 ){
198315    p->rc = SQLITE_NOMEM;
198316  }else{
198317    /* Move the *-oal file to *-wal. At this point connection p->db is
198318    ** holding a SHARED lock on the target database file (because it is
198319    ** in WAL mode). So no other connection may be writing the db.
198320    **
198321    ** In order to ensure that there are no database readers, an EXCLUSIVE
198322    ** lock is obtained here before the *-oal is moved to *-wal.
198323    */
198324    rbuLockDatabase(p);
198325    if( p->rc==SQLITE_OK ){
198326      rbuFileSuffix3(zBase, zWal);
198327      rbuFileSuffix3(zBase, zOal);
198328
198329      /* Re-open the databases. */
198330      rbuObjIterFinalize(&p->objiter);
198331      sqlite3_close(p->dbRbu);
198332      sqlite3_close(p->dbMain);
198333      p->dbMain = 0;
198334      p->dbRbu = 0;
198335
198336#if defined(_WIN32_WCE)
198337      {
198338        LPWSTR zWideOal;
198339        LPWSTR zWideWal;
198340
198341        zWideOal = rbuWinUtf8ToUnicode(zOal);
198342        if( zWideOal ){
198343          zWideWal = rbuWinUtf8ToUnicode(zWal);
198344          if( zWideWal ){
198345            if( MoveFileW(zWideOal, zWideWal) ){
198346              p->rc = SQLITE_OK;
198347            }else{
198348              p->rc = SQLITE_IOERR;
198349            }
198350            sqlite3_free(zWideWal);
198351          }else{
198352            p->rc = SQLITE_IOERR_NOMEM;
198353          }
198354          sqlite3_free(zWideOal);
198355        }else{
198356          p->rc = SQLITE_IOERR_NOMEM;
198357        }
198358      }
198359#else
198360      p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
198361#endif
198362
198363      if( p->rc==SQLITE_OK ){
198364        rbuOpenDatabase(p, 0);
198365        rbuSetupCheckpoint(p, 0);
198366      }
198367    }
198368  }
198369
198370  sqlite3_free(zWal);
198371  sqlite3_free(zOal);
198372}
198373
198374/*
198375** The SELECT statement iterating through the keys for the current object
198376** (p->objiter.pSelect) currently points to a valid row. This function
198377** determines the type of operation requested by this row and returns
198378** one of the following values to indicate the result:
198379**
198380**     * RBU_INSERT
198381**     * RBU_DELETE
198382**     * RBU_IDX_DELETE
198383**     * RBU_UPDATE
198384**
198385** If RBU_UPDATE is returned, then output variable *pzMask is set to
198386** point to the text value indicating the columns to update.
198387**
198388** If the rbu_control field contains an invalid value, an error code and
198389** message are left in the RBU handle and zero returned.
198390*/
198391static int rbuStepType(sqlite3rbu *p, const char **pzMask){
198392  int iCol = p->objiter.nCol;     /* Index of rbu_control column */
198393  int res = 0;                    /* Return value */
198394
198395  switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
198396    case SQLITE_INTEGER: {
198397      int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
198398      switch( iVal ){
198399        case 0: res = RBU_INSERT;     break;
198400        case 1: res = RBU_DELETE;     break;
198401        case 2: res = RBU_REPLACE;    break;
198402        case 3: res = RBU_IDX_DELETE; break;
198403        case 4: res = RBU_IDX_INSERT; break;
198404      }
198405      break;
198406    }
198407
198408    case SQLITE_TEXT: {
198409      const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
198410      if( z==0 ){
198411        p->rc = SQLITE_NOMEM;
198412      }else{
198413        *pzMask = (const char*)z;
198414      }
198415      res = RBU_UPDATE;
198416
198417      break;
198418    }
198419
198420    default:
198421      break;
198422  }
198423
198424  if( res==0 ){
198425    rbuBadControlError(p);
198426  }
198427  return res;
198428}
198429
198430#ifdef SQLITE_DEBUG
198431/*
198432** Assert that column iCol of statement pStmt is named zName.
198433*/
198434static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
198435  const char *zCol = sqlite3_column_name(pStmt, iCol);
198436  assert( 0==sqlite3_stricmp(zName, zCol) );
198437}
198438#else
198439# define assertColumnName(x,y,z)
198440#endif
198441
198442/*
198443** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or
198444** RBU_IDX_DELETE. This function performs the work of a single
198445** sqlite3rbu_step() call for the type of operation specified by eType.
198446*/
198447static void rbuStepOneOp(sqlite3rbu *p, int eType){
198448  RbuObjIter *pIter = &p->objiter;
198449  sqlite3_value *pVal;
198450  sqlite3_stmt *pWriter;
198451  int i;
198452
198453  assert( p->rc==SQLITE_OK );
198454  assert( eType!=RBU_DELETE || pIter->zIdx==0 );
198455  assert( eType==RBU_DELETE || eType==RBU_IDX_DELETE
198456       || eType==RBU_INSERT || eType==RBU_IDX_INSERT
198457  );
198458
198459  /* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE
198460  ** statement below does actually delete a row, nPhaseOneStep will be
198461  ** incremented by the same amount when SQL function rbu_tmp_insert()
198462  ** is invoked by the trigger.  */
198463  if( eType==RBU_DELETE ){
198464    p->nPhaseOneStep -= p->objiter.nIndex;
198465  }
198466
198467  if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
198468    pWriter = pIter->pDelete;
198469  }else{
198470    pWriter = pIter->pInsert;
198471  }
198472
198473  for(i=0; i<pIter->nCol; i++){
198474    /* If this is an INSERT into a table b-tree and the table has an
198475    ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
198476    ** to write a NULL into the IPK column. That is not permitted.  */
198477    if( eType==RBU_INSERT
198478     && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
198479     && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
198480    ){
198481      p->rc = SQLITE_MISMATCH;
198482      p->zErrmsg = sqlite3_mprintf("datatype mismatch");
198483      return;
198484    }
198485
198486    if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
198487      continue;
198488    }
198489
198490    pVal = sqlite3_column_value(pIter->pSelect, i);
198491    p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
198492    if( p->rc ) return;
198493  }
198494  if( pIter->zIdx==0 ){
198495    if( pIter->eType==RBU_PK_VTAB
198496     || pIter->eType==RBU_PK_NONE
198497     || (pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p))
198498    ){
198499      /* For a virtual table, or a table with no primary key, the
198500      ** SELECT statement is:
198501      **
198502      **   SELECT <cols>, rbu_control, rbu_rowid FROM ....
198503      **
198504      ** Hence column_value(pIter->nCol+1).
198505      */
198506      assertColumnName(pIter->pSelect, pIter->nCol+1,
198507          rbuIsVacuum(p) ? "rowid" : "rbu_rowid"
198508      );
198509      pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
198510      p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
198511    }
198512  }
198513  if( p->rc==SQLITE_OK ){
198514    sqlite3_step(pWriter);
198515    p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
198516  }
198517}
198518
198519/*
198520** This function does the work for an sqlite3rbu_step() call.
198521**
198522** The object-iterator (p->objiter) currently points to a valid object,
198523** and the input cursor (p->objiter.pSelect) currently points to a valid
198524** input row. Perform whatever processing is required and return.
198525**
198526** If no  error occurs, SQLITE_OK is returned. Otherwise, an error code
198527** and message is left in the RBU handle and a copy of the error code
198528** returned.
198529*/
198530static int rbuStep(sqlite3rbu *p){
198531  RbuObjIter *pIter = &p->objiter;
198532  const char *zMask = 0;
198533  int eType = rbuStepType(p, &zMask);
198534
198535  if( eType ){
198536    assert( eType==RBU_INSERT     || eType==RBU_DELETE
198537         || eType==RBU_REPLACE    || eType==RBU_IDX_DELETE
198538         || eType==RBU_IDX_INSERT || eType==RBU_UPDATE
198539    );
198540    assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
198541
198542    if( pIter->zIdx==0 && (eType==RBU_IDX_DELETE || eType==RBU_IDX_INSERT) ){
198543      rbuBadControlError(p);
198544    }
198545    else if( eType==RBU_REPLACE ){
198546      if( pIter->zIdx==0 ){
198547        p->nPhaseOneStep += p->objiter.nIndex;
198548        rbuStepOneOp(p, RBU_DELETE);
198549      }
198550      if( p->rc==SQLITE_OK ) rbuStepOneOp(p, RBU_INSERT);
198551    }
198552    else if( eType!=RBU_UPDATE ){
198553      rbuStepOneOp(p, eType);
198554    }
198555    else{
198556      sqlite3_value *pVal;
198557      sqlite3_stmt *pUpdate = 0;
198558      assert( eType==RBU_UPDATE );
198559      p->nPhaseOneStep -= p->objiter.nIndex;
198560      rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
198561      if( pUpdate ){
198562        int i;
198563        for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
198564          char c = zMask[pIter->aiSrcOrder[i]];
198565          pVal = sqlite3_column_value(pIter->pSelect, i);
198566          if( pIter->abTblPk[i] || c!='.' ){
198567            p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
198568          }
198569        }
198570        if( p->rc==SQLITE_OK
198571         && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
198572        ){
198573          /* Bind the rbu_rowid value to column _rowid_ */
198574          assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
198575          pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
198576          p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
198577        }
198578        if( p->rc==SQLITE_OK ){
198579          sqlite3_step(pUpdate);
198580          p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
198581        }
198582      }
198583    }
198584  }
198585  return p->rc;
198586}
198587
198588/*
198589** Increment the schema cookie of the main database opened by p->dbMain.
198590**
198591** Or, if this is an RBU vacuum, set the schema cookie of the main db
198592** opened by p->dbMain to one more than the schema cookie of the main
198593** db opened by p->dbRbu.
198594*/
198595static void rbuIncrSchemaCookie(sqlite3rbu *p){
198596  if( p->rc==SQLITE_OK ){
198597    sqlite3 *dbread = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
198598    int iCookie = 1000000;
198599    sqlite3_stmt *pStmt;
198600
198601    p->rc = prepareAndCollectError(dbread, &pStmt, &p->zErrmsg,
198602        "PRAGMA schema_version"
198603    );
198604    if( p->rc==SQLITE_OK ){
198605      /* Coverage: it may be that this sqlite3_step() cannot fail. There
198606      ** is already a transaction open, so the prepared statement cannot
198607      ** throw an SQLITE_SCHEMA exception. The only database page the
198608      ** statement reads is page 1, which is guaranteed to be in the cache.
198609      ** And no memory allocations are required.  */
198610      if( SQLITE_ROW==sqlite3_step(pStmt) ){
198611        iCookie = sqlite3_column_int(pStmt, 0);
198612      }
198613      rbuFinalize(p, pStmt);
198614    }
198615    if( p->rc==SQLITE_OK ){
198616      rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
198617    }
198618  }
198619}
198620
198621/*
198622** Update the contents of the rbu_state table within the rbu database. The
198623** value stored in the RBU_STATE_STAGE column is eStage. All other values
198624** are determined by inspecting the rbu handle passed as the first argument.
198625*/
198626static void rbuSaveState(sqlite3rbu *p, int eStage){
198627  if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
198628    sqlite3_stmt *pInsert = 0;
198629    rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
198630    int rc;
198631
198632    assert( p->zErrmsg==0 );
198633    rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
198634        sqlite3_mprintf(
198635          "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
198636          "(%d, %d), "
198637          "(%d, %Q), "
198638          "(%d, %Q), "
198639          "(%d, %d), "
198640          "(%d, %d), "
198641          "(%d, %lld), "
198642          "(%d, %lld), "
198643          "(%d, %lld), "
198644          "(%d, %lld), "
198645          "(%d, %Q)  ",
198646          p->zStateDb,
198647          RBU_STATE_STAGE, eStage,
198648          RBU_STATE_TBL, p->objiter.zTbl,
198649          RBU_STATE_IDX, p->objiter.zIdx,
198650          RBU_STATE_ROW, p->nStep,
198651          RBU_STATE_PROGRESS, p->nProgress,
198652          RBU_STATE_CKPT, p->iWalCksum,
198653          RBU_STATE_COOKIE, (i64)pFd->iCookie,
198654          RBU_STATE_OALSZ, p->iOalSz,
198655          RBU_STATE_PHASEONESTEP, p->nPhaseOneStep,
198656          RBU_STATE_DATATBL, p->objiter.zDataTbl
198657      )
198658    );
198659    assert( pInsert==0 || rc==SQLITE_OK );
198660
198661    if( rc==SQLITE_OK ){
198662      sqlite3_step(pInsert);
198663      rc = sqlite3_finalize(pInsert);
198664    }
198665    if( rc!=SQLITE_OK ) p->rc = rc;
198666  }
198667}
198668
198669
198670/*
198671** The second argument passed to this function is the name of a PRAGMA
198672** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
198673** This function executes the following on sqlite3rbu.dbRbu:
198674**
198675**   "PRAGMA main.$zPragma"
198676**
198677** where $zPragma is the string passed as the second argument, then
198678** on sqlite3rbu.dbMain:
198679**
198680**   "PRAGMA main.$zPragma = $val"
198681**
198682** where $val is the value returned by the first PRAGMA invocation.
198683**
198684** In short, it copies the value  of the specified PRAGMA setting from
198685** dbRbu to dbMain.
198686*/
198687static void rbuCopyPragma(sqlite3rbu *p, const char *zPragma){
198688  if( p->rc==SQLITE_OK ){
198689    sqlite3_stmt *pPragma = 0;
198690    p->rc = prepareFreeAndCollectError(p->dbRbu, &pPragma, &p->zErrmsg,
198691        sqlite3_mprintf("PRAGMA main.%s", zPragma)
198692    );
198693    if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPragma) ){
198694      p->rc = rbuMPrintfExec(p, p->dbMain, "PRAGMA main.%s = %d",
198695          zPragma, sqlite3_column_int(pPragma, 0)
198696      );
198697    }
198698    rbuFinalize(p, pPragma);
198699  }
198700}
198701
198702/*
198703** The RBU handle passed as the only argument has just been opened and
198704** the state database is empty. If this RBU handle was opened for an
198705** RBU vacuum operation, create the schema in the target db.
198706*/
198707static void rbuCreateTargetSchema(sqlite3rbu *p){
198708  sqlite3_stmt *pSql = 0;
198709  sqlite3_stmt *pInsert = 0;
198710
198711  assert( rbuIsVacuum(p) );
198712  p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=1", 0,0, &p->zErrmsg);
198713  if( p->rc==SQLITE_OK ){
198714    p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
198715      "SELECT sql FROM sqlite_master WHERE sql!='' AND rootpage!=0"
198716      " AND name!='sqlite_sequence' "
198717      " ORDER BY type DESC"
198718    );
198719  }
198720
198721  while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
198722    const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
198723    p->rc = sqlite3_exec(p->dbMain, zSql, 0, 0, &p->zErrmsg);
198724  }
198725  rbuFinalize(p, pSql);
198726  if( p->rc!=SQLITE_OK ) return;
198727
198728  if( p->rc==SQLITE_OK ){
198729    p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
198730        "SELECT * FROM sqlite_master WHERE rootpage=0 OR rootpage IS NULL"
198731    );
198732  }
198733
198734  if( p->rc==SQLITE_OK ){
198735    p->rc = prepareAndCollectError(p->dbMain, &pInsert, &p->zErrmsg,
198736        "INSERT INTO sqlite_master VALUES(?,?,?,?,?)"
198737    );
198738  }
198739
198740  while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
198741    int i;
198742    for(i=0; i<5; i++){
198743      sqlite3_bind_value(pInsert, i+1, sqlite3_column_value(pSql, i));
198744    }
198745    sqlite3_step(pInsert);
198746    p->rc = sqlite3_reset(pInsert);
198747  }
198748  if( p->rc==SQLITE_OK ){
198749    p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=0",0,0,&p->zErrmsg);
198750  }
198751
198752  rbuFinalize(p, pSql);
198753  rbuFinalize(p, pInsert);
198754}
198755
198756/*
198757** Step the RBU object.
198758*/
198759SQLITE_API int sqlite3rbu_step(sqlite3rbu *p){
198760  if( p ){
198761    switch( p->eStage ){
198762      case RBU_STAGE_OAL: {
198763        RbuObjIter *pIter = &p->objiter;
198764
198765        /* If this is an RBU vacuum operation and the state table was empty
198766        ** when this handle was opened, create the target database schema. */
198767        if( rbuIsVacuum(p) && p->nProgress==0 && p->rc==SQLITE_OK ){
198768          rbuCreateTargetSchema(p);
198769          rbuCopyPragma(p, "user_version");
198770          rbuCopyPragma(p, "application_id");
198771        }
198772
198773        while( p->rc==SQLITE_OK && pIter->zTbl ){
198774
198775          if( pIter->bCleanup ){
198776            /* Clean up the rbu_tmp_xxx table for the previous table. It
198777            ** cannot be dropped as there are currently active SQL statements.
198778            ** But the contents can be deleted.  */
198779            if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
198780              rbuMPrintfExec(p, p->dbRbu,
198781                  "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
198782              );
198783            }
198784          }else{
198785            rbuObjIterPrepareAll(p, pIter, 0);
198786
198787            /* Advance to the next row to process. */
198788            if( p->rc==SQLITE_OK ){
198789              int rc = sqlite3_step(pIter->pSelect);
198790              if( rc==SQLITE_ROW ){
198791                p->nProgress++;
198792                p->nStep++;
198793                return rbuStep(p);
198794              }
198795              p->rc = sqlite3_reset(pIter->pSelect);
198796              p->nStep = 0;
198797            }
198798          }
198799
198800          rbuObjIterNext(p, pIter);
198801        }
198802
198803        if( p->rc==SQLITE_OK ){
198804          assert( pIter->zTbl==0 );
198805          rbuSaveState(p, RBU_STAGE_MOVE);
198806          rbuIncrSchemaCookie(p);
198807          if( p->rc==SQLITE_OK ){
198808            p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
198809          }
198810          if( p->rc==SQLITE_OK ){
198811            p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
198812          }
198813          p->eStage = RBU_STAGE_MOVE;
198814        }
198815        break;
198816      }
198817
198818      case RBU_STAGE_MOVE: {
198819        if( p->rc==SQLITE_OK ){
198820          rbuMoveOalFile(p);
198821          p->nProgress++;
198822        }
198823        break;
198824      }
198825
198826      case RBU_STAGE_CKPT: {
198827        if( p->rc==SQLITE_OK ){
198828          if( p->nStep>=p->nFrame ){
198829            sqlite3_file *pDb = p->pTargetFd->pReal;
198830
198831            /* Sync the db file */
198832            p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
198833
198834            /* Update nBackfill */
198835            if( p->rc==SQLITE_OK ){
198836              void volatile *ptr;
198837              p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
198838              if( p->rc==SQLITE_OK ){
198839                ((u32 volatile*)ptr)[24] = p->iMaxFrame;
198840              }
198841            }
198842
198843            if( p->rc==SQLITE_OK ){
198844              p->eStage = RBU_STAGE_DONE;
198845              p->rc = SQLITE_DONE;
198846            }
198847          }else{
198848            /* At one point the following block copied a single frame from the
198849            ** wal file to the database file. So that one call to sqlite3rbu_step()
198850            ** checkpointed a single frame.
198851            **
198852            ** However, if the sector-size is larger than the page-size, and the
198853            ** application calls sqlite3rbu_savestate() or close() immediately
198854            ** after this step, then rbu_step() again, then a power failure occurs,
198855            ** then the database page written here may be damaged. Work around
198856            ** this by checkpointing frames until the next page in the aFrame[]
198857            ** lies on a different disk sector to the current one. */
198858            u32 iSector;
198859            do{
198860              RbuFrame *pFrame = &p->aFrame[p->nStep];
198861              iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
198862              rbuCheckpointFrame(p, pFrame);
198863              p->nStep++;
198864            }while( p->nStep<p->nFrame
198865                 && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
198866                 && p->rc==SQLITE_OK
198867            );
198868          }
198869          p->nProgress++;
198870        }
198871        break;
198872      }
198873
198874      default:
198875        break;
198876    }
198877    return p->rc;
198878  }else{
198879    return SQLITE_NOMEM;
198880  }
198881}
198882
198883/*
198884** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
198885** otherwise. Either or both argument may be NULL. Two NULL values are
198886** considered equal, and NULL is considered distinct from all other values.
198887*/
198888static int rbuStrCompare(const char *z1, const char *z2){
198889  if( z1==0 && z2==0 ) return 0;
198890  if( z1==0 || z2==0 ) return 1;
198891  return (sqlite3_stricmp(z1, z2)!=0);
198892}
198893
198894/*
198895** This function is called as part of sqlite3rbu_open() when initializing
198896** an rbu handle in OAL stage. If the rbu update has not started (i.e.
198897** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
198898** things so that the next call to sqlite3rbu_step() continues on from
198899** where the previous rbu handle left off.
198900**
198901** If an error occurs, an error code and error message are left in the
198902** rbu handle passed as the first argument.
198903*/
198904static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
198905  assert( p->rc==SQLITE_OK );
198906  if( pState->zTbl ){
198907    RbuObjIter *pIter = &p->objiter;
198908    int rc = SQLITE_OK;
198909
198910    while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
198911       || rbuStrCompare(pIter->zIdx, pState->zIdx)
198912       || (pState->zDataTbl==0 && rbuStrCompare(pIter->zTbl, pState->zTbl))
198913       || (pState->zDataTbl && rbuStrCompare(pIter->zDataTbl, pState->zDataTbl))
198914    )){
198915      rc = rbuObjIterNext(p, pIter);
198916    }
198917
198918    if( rc==SQLITE_OK && !pIter->zTbl ){
198919      rc = SQLITE_ERROR;
198920      p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
198921    }
198922
198923    if( rc==SQLITE_OK ){
198924      p->nStep = pState->nRow;
198925      rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
198926    }
198927
198928    p->rc = rc;
198929  }
198930}
198931
198932/*
198933** If there is a "*-oal" file in the file-system corresponding to the
198934** target database in the file-system, delete it. If an error occurs,
198935** leave an error code and error message in the rbu handle.
198936*/
198937static void rbuDeleteOalFile(sqlite3rbu *p){
198938  char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
198939  if( zOal ){
198940    sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
198941    assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 );
198942    pVfs->xDelete(pVfs, zOal, 0);
198943    sqlite3_free(zOal);
198944  }
198945}
198946
198947/*
198948** Allocate a private rbu VFS for the rbu handle passed as the only
198949** argument. This VFS will be used unless the call to sqlite3rbu_open()
198950** specified a URI with a vfs=? option in place of a target database
198951** file name.
198952*/
198953static void rbuCreateVfs(sqlite3rbu *p){
198954  int rnd;
198955  char zRnd[64];
198956
198957  assert( p->rc==SQLITE_OK );
198958  sqlite3_randomness(sizeof(int), (void*)&rnd);
198959  sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
198960  p->rc = sqlite3rbu_create_vfs(zRnd, 0);
198961  if( p->rc==SQLITE_OK ){
198962    sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
198963    assert( pVfs );
198964    p->zVfsName = pVfs->zName;
198965    ((rbu_vfs*)pVfs)->pRbu = p;
198966  }
198967}
198968
198969/*
198970** Destroy the private VFS created for the rbu handle passed as the only
198971** argument by an earlier call to rbuCreateVfs().
198972*/
198973static void rbuDeleteVfs(sqlite3rbu *p){
198974  if( p->zVfsName ){
198975    sqlite3rbu_destroy_vfs(p->zVfsName);
198976    p->zVfsName = 0;
198977  }
198978}
198979
198980/*
198981** This user-defined SQL function is invoked with a single argument - the
198982** name of a table expected to appear in the target database. It returns
198983** the number of auxilliary indexes on the table.
198984*/
198985static void rbuIndexCntFunc(
198986  sqlite3_context *pCtx,
198987  int nVal,
198988  sqlite3_value **apVal
198989){
198990  sqlite3rbu *p = (sqlite3rbu*)sqlite3_user_data(pCtx);
198991  sqlite3_stmt *pStmt = 0;
198992  char *zErrmsg = 0;
198993  int rc;
198994  sqlite3 *db = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
198995
198996  assert( nVal==1 );
198997
198998  rc = prepareFreeAndCollectError(db, &pStmt, &zErrmsg,
198999      sqlite3_mprintf("SELECT count(*) FROM sqlite_master "
199000        "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal[0]))
199001  );
199002  if( rc!=SQLITE_OK ){
199003    sqlite3_result_error(pCtx, zErrmsg, -1);
199004  }else{
199005    int nIndex = 0;
199006    if( SQLITE_ROW==sqlite3_step(pStmt) ){
199007      nIndex = sqlite3_column_int(pStmt, 0);
199008    }
199009    rc = sqlite3_finalize(pStmt);
199010    if( rc==SQLITE_OK ){
199011      sqlite3_result_int(pCtx, nIndex);
199012    }else{
199013      sqlite3_result_error(pCtx, sqlite3_errmsg(db), -1);
199014    }
199015  }
199016
199017  sqlite3_free(zErrmsg);
199018}
199019
199020/*
199021** If the RBU database contains the rbu_count table, use it to initialize
199022** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table
199023** is assumed to contain the same columns as:
199024**
199025**   CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
199026**
199027** There should be one row in the table for each data_xxx table in the
199028** database. The 'tbl' column should contain the name of a data_xxx table,
199029** and the cnt column the number of rows it contains.
199030**
199031** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt
199032** for all rows in the rbu_count table, where nIndex is the number of
199033** indexes on the corresponding target database table.
199034*/
199035static void rbuInitPhaseOneSteps(sqlite3rbu *p){
199036  if( p->rc==SQLITE_OK ){
199037    sqlite3_stmt *pStmt = 0;
199038    int bExists = 0;                /* True if rbu_count exists */
199039
199040    p->nPhaseOneStep = -1;
199041
199042    p->rc = sqlite3_create_function(p->dbRbu,
199043        "rbu_index_cnt", 1, SQLITE_UTF8, (void*)p, rbuIndexCntFunc, 0, 0
199044    );
199045
199046    /* Check for the rbu_count table. If it does not exist, or if an error
199047    ** occurs, nPhaseOneStep will be left set to -1. */
199048    if( p->rc==SQLITE_OK ){
199049      p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
199050          "SELECT 1 FROM sqlite_master WHERE tbl_name = 'rbu_count'"
199051      );
199052    }
199053    if( p->rc==SQLITE_OK ){
199054      if( SQLITE_ROW==sqlite3_step(pStmt) ){
199055        bExists = 1;
199056      }
199057      p->rc = sqlite3_finalize(pStmt);
199058    }
199059
199060    if( p->rc==SQLITE_OK && bExists ){
199061      p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
199062          "SELECT sum(cnt * (1 + rbu_index_cnt(rbu_target_name(tbl))))"
199063          "FROM rbu_count"
199064      );
199065      if( p->rc==SQLITE_OK ){
199066        if( SQLITE_ROW==sqlite3_step(pStmt) ){
199067          p->nPhaseOneStep = sqlite3_column_int64(pStmt, 0);
199068        }
199069        p->rc = sqlite3_finalize(pStmt);
199070      }
199071    }
199072  }
199073}
199074
199075
199076static sqlite3rbu *openRbuHandle(
199077  const char *zTarget,
199078  const char *zRbu,
199079  const char *zState
199080){
199081  sqlite3rbu *p;
199082  size_t nTarget = zTarget ? strlen(zTarget) : 0;
199083  size_t nRbu = strlen(zRbu);
199084  size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1;
199085
199086  p = (sqlite3rbu*)sqlite3_malloc64(nByte);
199087  if( p ){
199088    RbuState *pState = 0;
199089
199090    /* Create the custom VFS. */
199091    memset(p, 0, sizeof(sqlite3rbu));
199092    rbuCreateVfs(p);
199093
199094    /* Open the target, RBU and state databases */
199095    if( p->rc==SQLITE_OK ){
199096      char *pCsr = (char*)&p[1];
199097      int bRetry = 0;
199098      if( zTarget ){
199099        p->zTarget = pCsr;
199100        memcpy(p->zTarget, zTarget, nTarget+1);
199101        pCsr += nTarget+1;
199102      }
199103      p->zRbu = pCsr;
199104      memcpy(p->zRbu, zRbu, nRbu+1);
199105      pCsr += nRbu+1;
199106      if( zState ){
199107        p->zState = rbuMPrintf(p, "%s", zState);
199108      }
199109
199110      /* If the first attempt to open the database file fails and the bRetry
199111      ** flag it set, this means that the db was not opened because it seemed
199112      ** to be a wal-mode db. But, this may have happened due to an earlier
199113      ** RBU vacuum operation leaving an old wal file in the directory.
199114      ** If this is the case, it will have been checkpointed and deleted
199115      ** when the handle was closed and a second attempt to open the
199116      ** database may succeed.  */
199117      rbuOpenDatabase(p, &bRetry);
199118      if( bRetry ){
199119        rbuOpenDatabase(p, 0);
199120      }
199121    }
199122
199123    if( p->rc==SQLITE_OK ){
199124      pState = rbuLoadState(p);
199125      assert( pState || p->rc!=SQLITE_OK );
199126      if( p->rc==SQLITE_OK ){
199127
199128        if( pState->eStage==0 ){
199129          rbuDeleteOalFile(p);
199130          rbuInitPhaseOneSteps(p);
199131          p->eStage = RBU_STAGE_OAL;
199132        }else{
199133          p->eStage = pState->eStage;
199134          p->nPhaseOneStep = pState->nPhaseOneStep;
199135        }
199136        p->nProgress = pState->nProgress;
199137        p->iOalSz = pState->iOalSz;
199138      }
199139    }
199140    assert( p->rc!=SQLITE_OK || p->eStage!=0 );
199141
199142    if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
199143      if( p->eStage==RBU_STAGE_OAL ){
199144        p->rc = SQLITE_ERROR;
199145        p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
199146      }else if( p->eStage==RBU_STAGE_MOVE ){
199147        p->eStage = RBU_STAGE_CKPT;
199148        p->nStep = 0;
199149      }
199150    }
199151
199152    if( p->rc==SQLITE_OK
199153     && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
199154     && pState->eStage!=0
199155    ){
199156      rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
199157      if( pFd->iCookie!=pState->iCookie ){
199158        /* At this point (pTargetFd->iCookie) contains the value of the
199159        ** change-counter cookie (the thing that gets incremented when a
199160        ** transaction is committed in rollback mode) currently stored on
199161        ** page 1 of the database file. */
199162        p->rc = SQLITE_BUSY;
199163        p->zErrmsg = sqlite3_mprintf("database modified during rbu %s",
199164            (rbuIsVacuum(p) ? "vacuum" : "update")
199165        );
199166      }
199167    }
199168
199169    if( p->rc==SQLITE_OK ){
199170      if( p->eStage==RBU_STAGE_OAL ){
199171        sqlite3 *db = p->dbMain;
199172        p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
199173
199174        /* Point the object iterator at the first object */
199175        if( p->rc==SQLITE_OK ){
199176          p->rc = rbuObjIterFirst(p, &p->objiter);
199177        }
199178
199179        /* If the RBU database contains no data_xxx tables, declare the RBU
199180        ** update finished.  */
199181        if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
199182          p->rc = SQLITE_DONE;
199183          p->eStage = RBU_STAGE_DONE;
199184        }else{
199185          if( p->rc==SQLITE_OK && pState->eStage==0 && rbuIsVacuum(p) ){
199186            rbuCopyPragma(p, "page_size");
199187            rbuCopyPragma(p, "auto_vacuum");
199188          }
199189
199190          /* Open transactions both databases. The *-oal file is opened or
199191          ** created at this point. */
199192          if( p->rc==SQLITE_OK ){
199193            p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
199194          }
199195
199196          /* Check if the main database is a zipvfs db. If it is, set the upper
199197          ** level pager to use "journal_mode=off". This prevents it from
199198          ** generating a large journal using a temp file.  */
199199          if( p->rc==SQLITE_OK ){
199200            int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS, 0);
199201            if( frc==SQLITE_OK ){
199202              p->rc = sqlite3_exec(
199203                db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
199204            }
199205          }
199206
199207          if( p->rc==SQLITE_OK ){
199208            rbuSetupOal(p, pState);
199209          }
199210        }
199211      }else if( p->eStage==RBU_STAGE_MOVE ){
199212        /* no-op */
199213      }else if( p->eStage==RBU_STAGE_CKPT ){
199214        rbuSetupCheckpoint(p, pState);
199215      }else if( p->eStage==RBU_STAGE_DONE ){
199216        p->rc = SQLITE_DONE;
199217      }else{
199218        p->rc = SQLITE_CORRUPT;
199219      }
199220    }
199221
199222    rbuFreeState(pState);
199223  }
199224
199225  return p;
199226}
199227
199228/*
199229** Allocate and return an RBU handle with all fields zeroed except for the
199230** error code, which is set to SQLITE_MISUSE.
199231*/
199232static sqlite3rbu *rbuMisuseError(void){
199233  sqlite3rbu *pRet;
199234  pRet = sqlite3_malloc64(sizeof(sqlite3rbu));
199235  if( pRet ){
199236    memset(pRet, 0, sizeof(sqlite3rbu));
199237    pRet->rc = SQLITE_MISUSE;
199238  }
199239  return pRet;
199240}
199241
199242/*
199243** Open and return a new RBU handle.
199244*/
199245SQLITE_API sqlite3rbu *sqlite3rbu_open(
199246  const char *zTarget,
199247  const char *zRbu,
199248  const char *zState
199249){
199250  if( zTarget==0 || zRbu==0 ){ return rbuMisuseError(); }
199251  /* TODO: Check that zTarget and zRbu are non-NULL */
199252  return openRbuHandle(zTarget, zRbu, zState);
199253}
199254
199255/*
199256** Open a handle to begin or resume an RBU VACUUM operation.
199257*/
199258SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
199259  const char *zTarget,
199260  const char *zState
199261){
199262  if( zTarget==0 ){ return rbuMisuseError(); }
199263  if( zState ){
199264    int n = strlen(zState);
199265    if( n>=7 && 0==memcmp("-vactmp", &zState[n-7], 7) ){
199266      return rbuMisuseError();
199267    }
199268  }
199269  /* TODO: Check that both arguments are non-NULL */
199270  return openRbuHandle(0, zTarget, zState);
199271}
199272
199273/*
199274** Return the database handle used by pRbu.
199275*/
199276SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
199277  sqlite3 *db = 0;
199278  if( pRbu ){
199279    db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
199280  }
199281  return db;
199282}
199283
199284
199285/*
199286** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
199287** then edit any error message string so as to remove all occurrences of
199288** the pattern "rbu_imp_[0-9]*".
199289*/
199290static void rbuEditErrmsg(sqlite3rbu *p){
199291  if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
199292    unsigned int i;
199293    size_t nErrmsg = strlen(p->zErrmsg);
199294    for(i=0; i<(nErrmsg-8); i++){
199295      if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
199296        int nDel = 8;
199297        while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
199298        memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
199299        nErrmsg -= nDel;
199300      }
199301    }
199302  }
199303}
199304
199305/*
199306** Close the RBU handle.
199307*/
199308SQLITE_API int sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
199309  int rc;
199310  if( p ){
199311
199312    /* Commit the transaction to the *-oal file. */
199313    if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
199314      p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
199315    }
199316
199317    /* Sync the db file if currently doing an incremental checkpoint */
199318    if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
199319      sqlite3_file *pDb = p->pTargetFd->pReal;
199320      p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
199321    }
199322
199323    rbuSaveState(p, p->eStage);
199324
199325    if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
199326      p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
199327    }
199328
199329    /* Close any open statement handles. */
199330    rbuObjIterFinalize(&p->objiter);
199331
199332    /* If this is an RBU vacuum handle and the vacuum has either finished
199333    ** successfully or encountered an error, delete the contents of the
199334    ** state table. This causes the next call to sqlite3rbu_vacuum()
199335    ** specifying the current target and state databases to start a new
199336    ** vacuum from scratch.  */
199337    if( rbuIsVacuum(p) && p->rc!=SQLITE_OK && p->dbRbu ){
199338      int rc2 = sqlite3_exec(p->dbRbu, "DELETE FROM stat.rbu_state", 0, 0, 0);
199339      if( p->rc==SQLITE_DONE && rc2!=SQLITE_OK ) p->rc = rc2;
199340    }
199341
199342    /* Close the open database handle and VFS object. */
199343    sqlite3_close(p->dbRbu);
199344    sqlite3_close(p->dbMain);
199345    assert( p->szTemp==0 );
199346    rbuDeleteVfs(p);
199347    sqlite3_free(p->aBuf);
199348    sqlite3_free(p->aFrame);
199349
199350    rbuEditErrmsg(p);
199351    rc = p->rc;
199352    if( pzErrmsg ){
199353      *pzErrmsg = p->zErrmsg;
199354    }else{
199355      sqlite3_free(p->zErrmsg);
199356    }
199357    sqlite3_free(p->zState);
199358    sqlite3_free(p);
199359  }else{
199360    rc = SQLITE_NOMEM;
199361    *pzErrmsg = 0;
199362  }
199363  return rc;
199364}
199365
199366/*
199367** Return the total number of key-value operations (inserts, deletes or
199368** updates) that have been performed on the target database since the
199369** current RBU update was started.
199370*/
199371SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu){
199372  return pRbu->nProgress;
199373}
199374
199375/*
199376** Return permyriadage progress indications for the two main stages of
199377** an RBU update.
199378*/
199379SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *p, int *pnOne, int *pnTwo){
199380  const int MAX_PROGRESS = 10000;
199381  switch( p->eStage ){
199382    case RBU_STAGE_OAL:
199383      if( p->nPhaseOneStep>0 ){
199384        *pnOne = (int)(MAX_PROGRESS * (i64)p->nProgress/(i64)p->nPhaseOneStep);
199385      }else{
199386        *pnOne = -1;
199387      }
199388      *pnTwo = 0;
199389      break;
199390
199391    case RBU_STAGE_MOVE:
199392      *pnOne = MAX_PROGRESS;
199393      *pnTwo = 0;
199394      break;
199395
199396    case RBU_STAGE_CKPT:
199397      *pnOne = MAX_PROGRESS;
199398      *pnTwo = (int)(MAX_PROGRESS * (i64)p->nStep / (i64)p->nFrame);
199399      break;
199400
199401    case RBU_STAGE_DONE:
199402      *pnOne = MAX_PROGRESS;
199403      *pnTwo = MAX_PROGRESS;
199404      break;
199405
199406    default:
199407      assert( 0 );
199408  }
199409}
199410
199411/*
199412** Return the current state of the RBU vacuum or update operation.
199413*/
199414SQLITE_API int sqlite3rbu_state(sqlite3rbu *p){
199415  int aRes[] = {
199416    0, SQLITE_RBU_STATE_OAL, SQLITE_RBU_STATE_MOVE,
199417    0, SQLITE_RBU_STATE_CHECKPOINT, SQLITE_RBU_STATE_DONE
199418  };
199419
199420  assert( RBU_STAGE_OAL==1 );
199421  assert( RBU_STAGE_MOVE==2 );
199422  assert( RBU_STAGE_CKPT==4 );
199423  assert( RBU_STAGE_DONE==5 );
199424  assert( aRes[RBU_STAGE_OAL]==SQLITE_RBU_STATE_OAL );
199425  assert( aRes[RBU_STAGE_MOVE]==SQLITE_RBU_STATE_MOVE );
199426  assert( aRes[RBU_STAGE_CKPT]==SQLITE_RBU_STATE_CHECKPOINT );
199427  assert( aRes[RBU_STAGE_DONE]==SQLITE_RBU_STATE_DONE );
199428
199429  if( p->rc!=SQLITE_OK && p->rc!=SQLITE_DONE ){
199430    return SQLITE_RBU_STATE_ERROR;
199431  }else{
199432    assert( p->rc!=SQLITE_DONE || p->eStage==RBU_STAGE_DONE );
199433    assert( p->eStage==RBU_STAGE_OAL
199434         || p->eStage==RBU_STAGE_MOVE
199435         || p->eStage==RBU_STAGE_CKPT
199436         || p->eStage==RBU_STAGE_DONE
199437    );
199438    return aRes[p->eStage];
199439  }
199440}
199441
199442SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *p){
199443  int rc = p->rc;
199444  if( rc==SQLITE_DONE ) return SQLITE_OK;
199445
199446  assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
199447  if( p->eStage==RBU_STAGE_OAL ){
199448    assert( rc!=SQLITE_DONE );
199449    if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
199450  }
199451
199452  /* Sync the db file */
199453  if( rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
199454    sqlite3_file *pDb = p->pTargetFd->pReal;
199455    rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
199456  }
199457
199458  p->rc = rc;
199459  rbuSaveState(p, p->eStage);
199460  rc = p->rc;
199461
199462  if( p->eStage==RBU_STAGE_OAL ){
199463    assert( rc!=SQLITE_DONE );
199464    if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
199465    if( rc==SQLITE_OK ){
199466      const char *zBegin = rbuIsVacuum(p) ? "BEGIN" : "BEGIN IMMEDIATE";
199467      rc = sqlite3_exec(p->dbRbu, zBegin, 0, 0, 0);
199468    }
199469    if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
199470  }
199471
199472  p->rc = rc;
199473  return rc;
199474}
199475
199476/**************************************************************************
199477** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
199478** of a standard VFS in the following ways:
199479**
199480** 1. Whenever the first page of a main database file is read or
199481**    written, the value of the change-counter cookie is stored in
199482**    rbu_file.iCookie. Similarly, the value of the "write-version"
199483**    database header field is stored in rbu_file.iWriteVer. This ensures
199484**    that the values are always trustworthy within an open transaction.
199485**
199486** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
199487**    member variable of the associated database file descriptor is set
199488**    to point to the new file. A mutex protected linked list of all main
199489**    db fds opened using a particular RBU VFS is maintained at
199490**    rbu_vfs.pMain to facilitate this.
199491**
199492** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
199493**    object can be marked as the target database of an RBU update. This
199494**    turns on the following extra special behaviour:
199495**
199496** 3a. If xAccess() is called to check if there exists a *-wal file
199497**     associated with an RBU target database currently in RBU_STAGE_OAL
199498**     stage (preparing the *-oal file), the following special handling
199499**     applies:
199500**
199501**      * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
199502**        target database may not be in wal mode already.
199503**
199504**      * if the *-wal file does not exist, set the output parameter to
199505**        non-zero (to tell SQLite that it does exist) anyway.
199506**
199507**     Then, when xOpen() is called to open the *-wal file associated with
199508**     the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
199509**     file, the rbu vfs opens the corresponding *-oal file instead.
199510**
199511** 3b. The *-shm pages returned by xShmMap() for a target db file in
199512**     RBU_STAGE_OAL mode are actually stored in heap memory. This is to
199513**     avoid creating a *-shm file on disk. Additionally, xShmLock() calls
199514**     are no-ops on target database files in RBU_STAGE_OAL mode. This is
199515**     because assert() statements in some VFS implementations fail if
199516**     xShmLock() is called before xShmMap().
199517**
199518** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
199519**     mode except RBU_STAGE_DONE (all work completed and checkpointed), it
199520**     fails with an SQLITE_BUSY error. This is to stop RBU connections
199521**     from automatically checkpointing a *-wal (or *-oal) file from within
199522**     sqlite3_close().
199523**
199524** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
199525**     all xWrite() calls on the target database file perform no IO.
199526**     Instead the frame and page numbers that would be read and written
199527**     are recorded. Additionally, successful attempts to obtain exclusive
199528**     xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
199529**     database file are recorded. xShmLock() calls to unlock the same
199530**     locks are no-ops (so that once obtained, these locks are never
199531**     relinquished). Finally, calls to xSync() on the target database
199532**     file fail with SQLITE_INTERNAL errors.
199533*/
199534
199535static void rbuUnlockShm(rbu_file *p){
199536  assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
199537  if( p->pRbu ){
199538    int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
199539    int i;
199540    for(i=0; i<SQLITE_SHM_NLOCK;i++){
199541      if( (1<<i) & p->pRbu->mLock ){
199542        xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
199543      }
199544    }
199545    p->pRbu->mLock = 0;
199546  }
199547}
199548
199549/*
199550*/
199551static int rbuUpdateTempSize(rbu_file *pFd, sqlite3_int64 nNew){
199552  sqlite3rbu *pRbu = pFd->pRbu;
199553  i64 nDiff = nNew - pFd->sz;
199554  pRbu->szTemp += nDiff;
199555  pFd->sz = nNew;
199556  assert( pRbu->szTemp>=0 );
199557  if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL;
199558  return SQLITE_OK;
199559}
199560
199561/*
199562** Add an item to the main-db lists, if it is not already present.
199563**
199564** There are two main-db lists. One for all file descriptors, and one
199565** for all file descriptors with rbu_file.pDb!=0. If the argument has
199566** rbu_file.pDb!=0, then it is assumed to already be present on the
199567** main list and is only added to the pDb!=0 list.
199568*/
199569static void rbuMainlistAdd(rbu_file *p){
199570  rbu_vfs *pRbuVfs = p->pRbuVfs;
199571  rbu_file *pIter;
199572  assert( (p->openFlags & SQLITE_OPEN_MAIN_DB) );
199573  sqlite3_mutex_enter(pRbuVfs->mutex);
199574  if( p->pRbu==0 ){
199575    for(pIter=pRbuVfs->pMain; pIter; pIter=pIter->pMainNext);
199576    p->pMainNext = pRbuVfs->pMain;
199577    pRbuVfs->pMain = p;
199578  }else{
199579    for(pIter=pRbuVfs->pMainRbu; pIter && pIter!=p; pIter=pIter->pMainRbuNext){}
199580    if( pIter==0 ){
199581      p->pMainRbuNext = pRbuVfs->pMainRbu;
199582      pRbuVfs->pMainRbu = p;
199583    }
199584  }
199585  sqlite3_mutex_leave(pRbuVfs->mutex);
199586}
199587
199588/*
199589** Remove an item from the main-db lists.
199590*/
199591static void rbuMainlistRemove(rbu_file *p){
199592  rbu_file **pp;
199593  sqlite3_mutex_enter(p->pRbuVfs->mutex);
199594  for(pp=&p->pRbuVfs->pMain; *pp && *pp!=p; pp=&((*pp)->pMainNext)){}
199595  if( *pp ) *pp = p->pMainNext;
199596  p->pMainNext = 0;
199597  for(pp=&p->pRbuVfs->pMainRbu; *pp && *pp!=p; pp=&((*pp)->pMainRbuNext)){}
199598  if( *pp ) *pp = p->pMainRbuNext;
199599  p->pMainRbuNext = 0;
199600  sqlite3_mutex_leave(p->pRbuVfs->mutex);
199601}
199602
199603/*
199604** Given that zWal points to a buffer containing a wal file name passed to
199605** either the xOpen() or xAccess() VFS method, search the main-db list for
199606** a file-handle opened by the same database connection on the corresponding
199607** database file.
199608**
199609** If parameter bRbu is true, only search for file-descriptors with
199610** rbu_file.pDb!=0.
199611*/
199612static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal, int bRbu){
199613  rbu_file *pDb;
199614  sqlite3_mutex_enter(pRbuVfs->mutex);
199615  if( bRbu ){
199616    for(pDb=pRbuVfs->pMainRbu; pDb && pDb->zWal!=zWal; pDb=pDb->pMainRbuNext){}
199617  }else{
199618    for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
199619  }
199620  sqlite3_mutex_leave(pRbuVfs->mutex);
199621  return pDb;
199622}
199623
199624/*
199625** Close an rbu file.
199626*/
199627static int rbuVfsClose(sqlite3_file *pFile){
199628  rbu_file *p = (rbu_file*)pFile;
199629  int rc;
199630  int i;
199631
199632  /* Free the contents of the apShm[] array. And the array itself. */
199633  for(i=0; i<p->nShm; i++){
199634    sqlite3_free(p->apShm[i]);
199635  }
199636  sqlite3_free(p->apShm);
199637  p->apShm = 0;
199638  sqlite3_free(p->zDel);
199639
199640  if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
199641    rbuMainlistRemove(p);
199642    rbuUnlockShm(p);
199643    p->pReal->pMethods->xShmUnmap(p->pReal, 0);
199644  }
199645  else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
199646    rbuUpdateTempSize(p, 0);
199647  }
199648  assert( p->pMainNext==0 && p->pRbuVfs->pMain!=p );
199649
199650  /* Close the underlying file handle */
199651  rc = p->pReal->pMethods->xClose(p->pReal);
199652  return rc;
199653}
199654
199655
199656/*
199657** Read and return an unsigned 32-bit big-endian integer from the buffer
199658** passed as the only argument.
199659*/
199660static u32 rbuGetU32(u8 *aBuf){
199661  return ((u32)aBuf[0] << 24)
199662       + ((u32)aBuf[1] << 16)
199663       + ((u32)aBuf[2] <<  8)
199664       + ((u32)aBuf[3]);
199665}
199666
199667/*
199668** Write an unsigned 32-bit value in big-endian format to the supplied
199669** buffer.
199670*/
199671static void rbuPutU32(u8 *aBuf, u32 iVal){
199672  aBuf[0] = (iVal >> 24) & 0xFF;
199673  aBuf[1] = (iVal >> 16) & 0xFF;
199674  aBuf[2] = (iVal >>  8) & 0xFF;
199675  aBuf[3] = (iVal >>  0) & 0xFF;
199676}
199677
199678static void rbuPutU16(u8 *aBuf, u16 iVal){
199679  aBuf[0] = (iVal >>  8) & 0xFF;
199680  aBuf[1] = (iVal >>  0) & 0xFF;
199681}
199682
199683/*
199684** Read data from an rbuVfs-file.
199685*/
199686static int rbuVfsRead(
199687  sqlite3_file *pFile,
199688  void *zBuf,
199689  int iAmt,
199690  sqlite_int64 iOfst
199691){
199692  rbu_file *p = (rbu_file*)pFile;
199693  sqlite3rbu *pRbu = p->pRbu;
199694  int rc;
199695
199696  if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
199697    assert( p->openFlags & SQLITE_OPEN_WAL );
199698    rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
199699  }else{
199700    if( pRbu && pRbu->eStage==RBU_STAGE_OAL
199701     && (p->openFlags & SQLITE_OPEN_WAL)
199702     && iOfst>=pRbu->iOalSz
199703    ){
199704      rc = SQLITE_OK;
199705      memset(zBuf, 0, iAmt);
199706    }else{
199707      rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
199708#if 1
199709      /* If this is being called to read the first page of the target
199710      ** database as part of an rbu vacuum operation, synthesize the
199711      ** contents of the first page if it does not yet exist. Otherwise,
199712      ** SQLite will not check for a *-wal file.  */
199713      if( pRbu && rbuIsVacuum(pRbu)
199714          && rc==SQLITE_IOERR_SHORT_READ && iOfst==0
199715          && (p->openFlags & SQLITE_OPEN_MAIN_DB)
199716          && pRbu->rc==SQLITE_OK
199717      ){
199718        sqlite3_file *pFd = (sqlite3_file*)pRbu->pRbuFd;
199719        rc = pFd->pMethods->xRead(pFd, zBuf, iAmt, iOfst);
199720        if( rc==SQLITE_OK ){
199721          u8 *aBuf = (u8*)zBuf;
199722          u32 iRoot = rbuGetU32(&aBuf[52]) ? 1 : 0;
199723          rbuPutU32(&aBuf[52], iRoot);      /* largest root page number */
199724          rbuPutU32(&aBuf[36], 0);          /* number of free pages */
199725          rbuPutU32(&aBuf[32], 0);          /* first page on free list trunk */
199726          rbuPutU32(&aBuf[28], 1);          /* size of db file in pages */
199727          rbuPutU32(&aBuf[24], pRbu->pRbuFd->iCookie+1);  /* Change counter */
199728
199729          if( iAmt>100 ){
199730            memset(&aBuf[100], 0, iAmt-100);
199731            rbuPutU16(&aBuf[105], iAmt & 0xFFFF);
199732            aBuf[100] = 0x0D;
199733          }
199734        }
199735      }
199736#endif
199737    }
199738    if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
199739      /* These look like magic numbers. But they are stable, as they are part
199740       ** of the definition of the SQLite file format, which may not change. */
199741      u8 *pBuf = (u8*)zBuf;
199742      p->iCookie = rbuGetU32(&pBuf[24]);
199743      p->iWriteVer = pBuf[19];
199744    }
199745  }
199746  return rc;
199747}
199748
199749/*
199750** Write data to an rbuVfs-file.
199751*/
199752static int rbuVfsWrite(
199753  sqlite3_file *pFile,
199754  const void *zBuf,
199755  int iAmt,
199756  sqlite_int64 iOfst
199757){
199758  rbu_file *p = (rbu_file*)pFile;
199759  sqlite3rbu *pRbu = p->pRbu;
199760  int rc;
199761
199762  if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
199763    assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
199764    rc = rbuCaptureDbWrite(p->pRbu, iOfst);
199765  }else{
199766    if( pRbu ){
199767      if( pRbu->eStage==RBU_STAGE_OAL
199768       && (p->openFlags & SQLITE_OPEN_WAL)
199769       && iOfst>=pRbu->iOalSz
199770      ){
199771        pRbu->iOalSz = iAmt + iOfst;
199772      }else if( p->openFlags & SQLITE_OPEN_DELETEONCLOSE ){
199773        i64 szNew = iAmt+iOfst;
199774        if( szNew>p->sz ){
199775          rc = rbuUpdateTempSize(p, szNew);
199776          if( rc!=SQLITE_OK ) return rc;
199777        }
199778      }
199779    }
199780    rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
199781    if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
199782      /* These look like magic numbers. But they are stable, as they are part
199783      ** of the definition of the SQLite file format, which may not change. */
199784      u8 *pBuf = (u8*)zBuf;
199785      p->iCookie = rbuGetU32(&pBuf[24]);
199786      p->iWriteVer = pBuf[19];
199787    }
199788  }
199789  return rc;
199790}
199791
199792/*
199793** Truncate an rbuVfs-file.
199794*/
199795static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
199796  rbu_file *p = (rbu_file*)pFile;
199797  if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
199798    int rc = rbuUpdateTempSize(p, size);
199799    if( rc!=SQLITE_OK ) return rc;
199800  }
199801  return p->pReal->pMethods->xTruncate(p->pReal, size);
199802}
199803
199804/*
199805** Sync an rbuVfs-file.
199806*/
199807static int rbuVfsSync(sqlite3_file *pFile, int flags){
199808  rbu_file *p = (rbu_file *)pFile;
199809  if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
199810    if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
199811      return SQLITE_INTERNAL;
199812    }
199813    return SQLITE_OK;
199814  }
199815  return p->pReal->pMethods->xSync(p->pReal, flags);
199816}
199817
199818/*
199819** Return the current file-size of an rbuVfs-file.
199820*/
199821static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
199822  rbu_file *p = (rbu_file *)pFile;
199823  int rc;
199824  rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
199825
199826  /* If this is an RBU vacuum operation and this is the target database,
199827  ** pretend that it has at least one page. Otherwise, SQLite will not
199828  ** check for the existance of a *-wal file. rbuVfsRead() contains
199829  ** similar logic.  */
199830  if( rc==SQLITE_OK && *pSize==0
199831   && p->pRbu && rbuIsVacuum(p->pRbu)
199832   && (p->openFlags & SQLITE_OPEN_MAIN_DB)
199833  ){
199834    *pSize = 1024;
199835  }
199836  return rc;
199837}
199838
199839/*
199840** Lock an rbuVfs-file.
199841*/
199842static int rbuVfsLock(sqlite3_file *pFile, int eLock){
199843  rbu_file *p = (rbu_file*)pFile;
199844  sqlite3rbu *pRbu = p->pRbu;
199845  int rc = SQLITE_OK;
199846
199847  assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
199848  if( eLock==SQLITE_LOCK_EXCLUSIVE
199849   && (p->bNolock || (pRbu && pRbu->eStage!=RBU_STAGE_DONE))
199850  ){
199851    /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
199852    ** prevents it from checkpointing the database from sqlite3_close(). */
199853    rc = SQLITE_BUSY;
199854  }else{
199855    rc = p->pReal->pMethods->xLock(p->pReal, eLock);
199856  }
199857
199858  return rc;
199859}
199860
199861/*
199862** Unlock an rbuVfs-file.
199863*/
199864static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
199865  rbu_file *p = (rbu_file *)pFile;
199866  return p->pReal->pMethods->xUnlock(p->pReal, eLock);
199867}
199868
199869/*
199870** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
199871*/
199872static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
199873  rbu_file *p = (rbu_file *)pFile;
199874  return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
199875}
199876
199877/*
199878** File control method. For custom operations on an rbuVfs-file.
199879*/
199880static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
199881  rbu_file *p = (rbu_file *)pFile;
199882  int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
199883  int rc;
199884
199885  assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
199886       || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
199887  );
199888  if( op==SQLITE_FCNTL_RBU ){
199889    sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
199890
199891    /* First try to find another RBU vfs lower down in the vfs stack. If
199892    ** one is found, this vfs will operate in pass-through mode. The lower
199893    ** level vfs will do the special RBU handling.  */
199894    rc = xControl(p->pReal, op, pArg);
199895
199896    if( rc==SQLITE_NOTFOUND ){
199897      /* Now search for a zipvfs instance lower down in the VFS stack. If
199898      ** one is found, this is an error.  */
199899      void *dummy = 0;
199900      rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
199901      if( rc==SQLITE_OK ){
199902        rc = SQLITE_ERROR;
199903        pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
199904      }else if( rc==SQLITE_NOTFOUND ){
199905        pRbu->pTargetFd = p;
199906        p->pRbu = pRbu;
199907        rbuMainlistAdd(p);
199908        if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
199909        rc = SQLITE_OK;
199910      }
199911    }
199912    return rc;
199913  }
199914  else if( op==SQLITE_FCNTL_RBUCNT ){
199915    sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
199916    pRbu->nRbu++;
199917    pRbu->pRbuFd = p;
199918    p->bNolock = 1;
199919  }
199920
199921  rc = xControl(p->pReal, op, pArg);
199922  if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
199923    rbu_vfs *pRbuVfs = p->pRbuVfs;
199924    char *zIn = *(char**)pArg;
199925    char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
199926    *(char**)pArg = zOut;
199927    if( zOut==0 ) rc = SQLITE_NOMEM;
199928  }
199929
199930  return rc;
199931}
199932
199933/*
199934** Return the sector-size in bytes for an rbuVfs-file.
199935*/
199936static int rbuVfsSectorSize(sqlite3_file *pFile){
199937  rbu_file *p = (rbu_file *)pFile;
199938  return p->pReal->pMethods->xSectorSize(p->pReal);
199939}
199940
199941/*
199942** Return the device characteristic flags supported by an rbuVfs-file.
199943*/
199944static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
199945  rbu_file *p = (rbu_file *)pFile;
199946  return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
199947}
199948
199949/*
199950** Take or release a shared-memory lock.
199951*/
199952static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
199953  rbu_file *p = (rbu_file*)pFile;
199954  sqlite3rbu *pRbu = p->pRbu;
199955  int rc = SQLITE_OK;
199956
199957#ifdef SQLITE_AMALGAMATION
199958    assert( WAL_CKPT_LOCK==1 );
199959#endif
199960
199961  assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
199962  if( pRbu && (pRbu->eStage==RBU_STAGE_OAL || pRbu->eStage==RBU_STAGE_MOVE) ){
199963    /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
199964    ** taking this lock also prevents any checkpoints from occurring.
199965    ** todo: really, it's not clear why this might occur, as
199966    ** wal_autocheckpoint ought to be turned off.  */
199967    if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
199968  }else{
199969    int bCapture = 0;
199970    if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
199971      bCapture = 1;
199972    }
199973
199974    if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
199975      rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
199976      if( bCapture && rc==SQLITE_OK ){
199977        pRbu->mLock |= (1 << ofst);
199978      }
199979    }
199980  }
199981
199982  return rc;
199983}
199984
199985/*
199986** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
199987*/
199988static int rbuVfsShmMap(
199989  sqlite3_file *pFile,
199990  int iRegion,
199991  int szRegion,
199992  int isWrite,
199993  void volatile **pp
199994){
199995  rbu_file *p = (rbu_file*)pFile;
199996  int rc = SQLITE_OK;
199997  int eStage = (p->pRbu ? p->pRbu->eStage : 0);
199998
199999  /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
200000  ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
200001  ** instead of a file on disk.  */
200002  assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
200003  if( eStage==RBU_STAGE_OAL ){
200004    sqlite3_int64 nByte = (iRegion+1) * sizeof(char*);
200005    char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
200006
200007    /* This is an RBU connection that uses its own heap memory for the
200008    ** pages of the *-shm file. Since no other process can have run
200009    ** recovery, the connection must request *-shm pages in order
200010    ** from start to finish.  */
200011    assert( iRegion==p->nShm );
200012    if( apNew==0 ){
200013      rc = SQLITE_NOMEM;
200014    }else{
200015      memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
200016      p->apShm = apNew;
200017      p->nShm = iRegion+1;
200018    }
200019
200020    if( rc==SQLITE_OK ){
200021      char *pNew = (char*)sqlite3_malloc64(szRegion);
200022      if( pNew==0 ){
200023        rc = SQLITE_NOMEM;
200024      }else{
200025        memset(pNew, 0, szRegion);
200026        p->apShm[iRegion] = pNew;
200027      }
200028    }
200029
200030    if( rc==SQLITE_OK ){
200031      *pp = p->apShm[iRegion];
200032    }else{
200033      *pp = 0;
200034    }
200035  }else{
200036    assert( p->apShm==0 );
200037    rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
200038  }
200039
200040  return rc;
200041}
200042
200043/*
200044** Memory barrier.
200045*/
200046static void rbuVfsShmBarrier(sqlite3_file *pFile){
200047  rbu_file *p = (rbu_file *)pFile;
200048  p->pReal->pMethods->xShmBarrier(p->pReal);
200049}
200050
200051/*
200052** The xShmUnmap method.
200053*/
200054static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
200055  rbu_file *p = (rbu_file*)pFile;
200056  int rc = SQLITE_OK;
200057  int eStage = (p->pRbu ? p->pRbu->eStage : 0);
200058
200059  assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
200060  if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
200061    /* no-op */
200062  }else{
200063    /* Release the checkpointer and writer locks */
200064    rbuUnlockShm(p);
200065    rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
200066  }
200067  return rc;
200068}
200069
200070/*
200071** Open an rbu file handle.
200072*/
200073static int rbuVfsOpen(
200074  sqlite3_vfs *pVfs,
200075  const char *zName,
200076  sqlite3_file *pFile,
200077  int flags,
200078  int *pOutFlags
200079){
200080  static sqlite3_io_methods rbuvfs_io_methods = {
200081    2,                            /* iVersion */
200082    rbuVfsClose,                  /* xClose */
200083    rbuVfsRead,                   /* xRead */
200084    rbuVfsWrite,                  /* xWrite */
200085    rbuVfsTruncate,               /* xTruncate */
200086    rbuVfsSync,                   /* xSync */
200087    rbuVfsFileSize,               /* xFileSize */
200088    rbuVfsLock,                   /* xLock */
200089    rbuVfsUnlock,                 /* xUnlock */
200090    rbuVfsCheckReservedLock,      /* xCheckReservedLock */
200091    rbuVfsFileControl,            /* xFileControl */
200092    rbuVfsSectorSize,             /* xSectorSize */
200093    rbuVfsDeviceCharacteristics,  /* xDeviceCharacteristics */
200094    rbuVfsShmMap,                 /* xShmMap */
200095    rbuVfsShmLock,                /* xShmLock */
200096    rbuVfsShmBarrier,             /* xShmBarrier */
200097    rbuVfsShmUnmap,               /* xShmUnmap */
200098    0, 0                          /* xFetch, xUnfetch */
200099  };
200100  rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
200101  sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
200102  rbu_file *pFd = (rbu_file *)pFile;
200103  int rc = SQLITE_OK;
200104  const char *zOpen = zName;
200105  int oflags = flags;
200106
200107  memset(pFd, 0, sizeof(rbu_file));
200108  pFd->pReal = (sqlite3_file*)&pFd[1];
200109  pFd->pRbuVfs = pRbuVfs;
200110  pFd->openFlags = flags;
200111  if( zName ){
200112    if( flags & SQLITE_OPEN_MAIN_DB ){
200113      /* A main database has just been opened. The following block sets
200114      ** (pFd->zWal) to point to a buffer owned by SQLite that contains
200115      ** the name of the *-wal file this db connection will use. SQLite
200116      ** happens to pass a pointer to this buffer when using xAccess()
200117      ** or xOpen() to operate on the *-wal file.  */
200118      pFd->zWal = sqlite3_filename_wal(zName);
200119    }
200120    else if( flags & SQLITE_OPEN_WAL ){
200121      rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName, 0);
200122      if( pDb ){
200123        if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
200124          /* This call is to open a *-wal file. Intead, open the *-oal. This
200125          ** code ensures that the string passed to xOpen() is terminated by a
200126          ** pair of '\0' bytes in case the VFS attempts to extract a URI
200127          ** parameter from it.  */
200128          const char *zBase = zName;
200129          size_t nCopy;
200130          char *zCopy;
200131          if( rbuIsVacuum(pDb->pRbu) ){
200132            zBase = sqlite3_db_filename(pDb->pRbu->dbRbu, "main");
200133            zBase = sqlite3_filename_wal(zBase);
200134          }
200135          nCopy = strlen(zBase);
200136          zCopy = sqlite3_malloc64(nCopy+2);
200137          if( zCopy ){
200138            memcpy(zCopy, zBase, nCopy);
200139            zCopy[nCopy-3] = 'o';
200140            zCopy[nCopy] = '\0';
200141            zCopy[nCopy+1] = '\0';
200142            zOpen = (const char*)(pFd->zDel = zCopy);
200143          }else{
200144            rc = SQLITE_NOMEM;
200145          }
200146          pFd->pRbu = pDb->pRbu;
200147        }
200148        pDb->pWalFd = pFd;
200149      }
200150    }
200151  }else{
200152    pFd->pRbu = pRbuVfs->pRbu;
200153  }
200154
200155  if( oflags & SQLITE_OPEN_MAIN_DB
200156   && sqlite3_uri_boolean(zName, "rbu_memory", 0)
200157  ){
200158    assert( oflags & SQLITE_OPEN_MAIN_DB );
200159    oflags =  SQLITE_OPEN_TEMP_DB | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
200160              SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
200161    zOpen = 0;
200162  }
200163
200164  if( rc==SQLITE_OK ){
200165    rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, oflags, pOutFlags);
200166  }
200167  if( pFd->pReal->pMethods ){
200168    /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
200169    ** pointer and, if the file is a main database file, link it into the
200170    ** mutex protected linked list of all such files.  */
200171    pFile->pMethods = &rbuvfs_io_methods;
200172    if( flags & SQLITE_OPEN_MAIN_DB ){
200173      rbuMainlistAdd(pFd);
200174    }
200175  }else{
200176    sqlite3_free(pFd->zDel);
200177  }
200178
200179  return rc;
200180}
200181
200182/*
200183** Delete the file located at zPath.
200184*/
200185static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
200186  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
200187  return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
200188}
200189
200190/*
200191** Test for access permissions. Return true if the requested permission
200192** is available, or false otherwise.
200193*/
200194static int rbuVfsAccess(
200195  sqlite3_vfs *pVfs,
200196  const char *zPath,
200197  int flags,
200198  int *pResOut
200199){
200200  rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
200201  sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
200202  int rc;
200203
200204  rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
200205
200206  /* If this call is to check if a *-wal file associated with an RBU target
200207  ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
200208  ** the following special handling is activated:
200209  **
200210  **   a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
200211  **      ensures that the RBU extension never tries to update a database
200212  **      in wal mode, even if the first page of the database file has
200213  **      been damaged.
200214  **
200215  **   b) if the *-wal file does not exist, claim that it does anyway,
200216  **      causing SQLite to call xOpen() to open it. This call will also
200217  **      be intercepted (see the rbuVfsOpen() function) and the *-oal
200218  **      file opened instead.
200219  */
200220  if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
200221    rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1);
200222    if( pDb && pDb->pRbu->eStage==RBU_STAGE_OAL ){
200223      assert( pDb->pRbu );
200224      if( *pResOut ){
200225        rc = SQLITE_CANTOPEN;
200226      }else{
200227        sqlite3_int64 sz = 0;
200228        rc = rbuVfsFileSize(&pDb->base, &sz);
200229        *pResOut = (sz>0);
200230      }
200231    }
200232  }
200233
200234  return rc;
200235}
200236
200237/*
200238** Populate buffer zOut with the full canonical pathname corresponding
200239** to the pathname in zPath. zOut is guaranteed to point to a buffer
200240** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
200241*/
200242static int rbuVfsFullPathname(
200243  sqlite3_vfs *pVfs,
200244  const char *zPath,
200245  int nOut,
200246  char *zOut
200247){
200248  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
200249  return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
200250}
200251
200252#ifndef SQLITE_OMIT_LOAD_EXTENSION
200253/*
200254** Open the dynamic library located at zPath and return a handle.
200255*/
200256static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
200257  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
200258  return pRealVfs->xDlOpen(pRealVfs, zPath);
200259}
200260
200261/*
200262** Populate the buffer zErrMsg (size nByte bytes) with a human readable
200263** utf-8 string describing the most recent error encountered associated
200264** with dynamic libraries.
200265*/
200266static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
200267  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
200268  pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
200269}
200270
200271/*
200272** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
200273*/
200274static void (*rbuVfsDlSym(
200275  sqlite3_vfs *pVfs,
200276  void *pArg,
200277  const char *zSym
200278))(void){
200279  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
200280  return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
200281}
200282
200283/*
200284** Close the dynamic library handle pHandle.
200285*/
200286static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
200287  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
200288  pRealVfs->xDlClose(pRealVfs, pHandle);
200289}
200290#endif /* SQLITE_OMIT_LOAD_EXTENSION */
200291
200292/*
200293** Populate the buffer pointed to by zBufOut with nByte bytes of
200294** random data.
200295*/
200296static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
200297  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
200298  return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
200299}
200300
200301/*
200302** Sleep for nMicro microseconds. Return the number of microseconds
200303** actually slept.
200304*/
200305static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
200306  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
200307  return pRealVfs->xSleep(pRealVfs, nMicro);
200308}
200309
200310/*
200311** Return the current time as a Julian Day number in *pTimeOut.
200312*/
200313static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
200314  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
200315  return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
200316}
200317
200318/*
200319** No-op.
200320*/
200321static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
200322  return 0;
200323}
200324
200325/*
200326** Deregister and destroy an RBU vfs created by an earlier call to
200327** sqlite3rbu_create_vfs().
200328*/
200329SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName){
200330  sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
200331  if( pVfs && pVfs->xOpen==rbuVfsOpen ){
200332    sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
200333    sqlite3_vfs_unregister(pVfs);
200334    sqlite3_free(pVfs);
200335  }
200336}
200337
200338/*
200339** Create an RBU VFS named zName that accesses the underlying file-system
200340** via existing VFS zParent. The new object is registered as a non-default
200341** VFS with SQLite before returning.
200342*/
200343SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent){
200344
200345  /* Template for VFS */
200346  static sqlite3_vfs vfs_template = {
200347    1,                            /* iVersion */
200348    0,                            /* szOsFile */
200349    0,                            /* mxPathname */
200350    0,                            /* pNext */
200351    0,                            /* zName */
200352    0,                            /* pAppData */
200353    rbuVfsOpen,                   /* xOpen */
200354    rbuVfsDelete,                 /* xDelete */
200355    rbuVfsAccess,                 /* xAccess */
200356    rbuVfsFullPathname,           /* xFullPathname */
200357
200358#ifndef SQLITE_OMIT_LOAD_EXTENSION
200359    rbuVfsDlOpen,                 /* xDlOpen */
200360    rbuVfsDlError,                /* xDlError */
200361    rbuVfsDlSym,                  /* xDlSym */
200362    rbuVfsDlClose,                /* xDlClose */
200363#else
200364    0, 0, 0, 0,
200365#endif
200366
200367    rbuVfsRandomness,             /* xRandomness */
200368    rbuVfsSleep,                  /* xSleep */
200369    rbuVfsCurrentTime,            /* xCurrentTime */
200370    rbuVfsGetLastError,           /* xGetLastError */
200371    0,                            /* xCurrentTimeInt64 (version 2) */
200372    0, 0, 0                       /* Unimplemented version 3 methods */
200373  };
200374
200375  rbu_vfs *pNew = 0;              /* Newly allocated VFS */
200376  int rc = SQLITE_OK;
200377  size_t nName;
200378  size_t nByte;
200379
200380  nName = strlen(zName);
200381  nByte = sizeof(rbu_vfs) + nName + 1;
200382  pNew = (rbu_vfs*)sqlite3_malloc64(nByte);
200383  if( pNew==0 ){
200384    rc = SQLITE_NOMEM;
200385  }else{
200386    sqlite3_vfs *pParent;           /* Parent VFS */
200387    memset(pNew, 0, nByte);
200388    pParent = sqlite3_vfs_find(zParent);
200389    if( pParent==0 ){
200390      rc = SQLITE_NOTFOUND;
200391    }else{
200392      char *zSpace;
200393      memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
200394      pNew->base.mxPathname = pParent->mxPathname;
200395      pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
200396      pNew->pRealVfs = pParent;
200397      pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
200398      memcpy(zSpace, zName, nName);
200399
200400      /* Allocate the mutex and register the new VFS (not as the default) */
200401      pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
200402      if( pNew->mutex==0 ){
200403        rc = SQLITE_NOMEM;
200404      }else{
200405        rc = sqlite3_vfs_register(&pNew->base, 0);
200406      }
200407    }
200408
200409    if( rc!=SQLITE_OK ){
200410      sqlite3_mutex_free(pNew->mutex);
200411      sqlite3_free(pNew);
200412    }
200413  }
200414
200415  return rc;
200416}
200417
200418/*
200419** Configure the aggregate temp file size limit for this RBU handle.
200420*/
200421SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu *pRbu, sqlite3_int64 n){
200422  if( n>=0 ){
200423    pRbu->szTempLimit = n;
200424  }
200425  return pRbu->szTempLimit;
200426}
200427
200428SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu){
200429  return pRbu->szTemp;
200430}
200431
200432
200433/**************************************************************************/
200434
200435#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
200436
200437/************** End of sqlite3rbu.c ******************************************/
200438/************** Begin file dbstat.c ******************************************/
200439/*
200440** 2010 July 12
200441**
200442** The author disclaims copyright to this source code.  In place of
200443** a legal notice, here is a blessing:
200444**
200445**    May you do good and not evil.
200446**    May you find forgiveness for yourself and forgive others.
200447**    May you share freely, never taking more than you give.
200448**
200449******************************************************************************
200450**
200451** This file contains an implementation of the "dbstat" virtual table.
200452**
200453** The dbstat virtual table is used to extract low-level storage
200454** information from an SQLite database in order to implement the
200455** "sqlite3_analyzer" utility.  See the ../tool/spaceanal.tcl script
200456** for an example implementation.
200457**
200458** Additional information is available on the "dbstat.html" page of the
200459** official SQLite documentation.
200460*/
200461
200462/* #include "sqliteInt.h"   ** Requires access to internal data structures ** */
200463#if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
200464    && !defined(SQLITE_OMIT_VIRTUALTABLE)
200465
200466/*
200467** Page paths:
200468**
200469**   The value of the 'path' column describes the path taken from the
200470**   root-node of the b-tree structure to each page. The value of the
200471**   root-node path is '/'.
200472**
200473**   The value of the path for the left-most child page of the root of
200474**   a b-tree is '/000/'. (Btrees store content ordered from left to right
200475**   so the pages to the left have smaller keys than the pages to the right.)
200476**   The next to left-most child of the root page is
200477**   '/001', and so on, each sibling page identified by a 3-digit hex
200478**   value. The children of the 451st left-most sibling have paths such
200479**   as '/1c2/000/, '/1c2/001/' etc.
200480**
200481**   Overflow pages are specified by appending a '+' character and a
200482**   six-digit hexadecimal value to the path to the cell they are linked
200483**   from. For example, the three overflow pages in a chain linked from
200484**   the left-most cell of the 450th child of the root page are identified
200485**   by the paths:
200486**
200487**      '/1c2/000+000000'         // First page in overflow chain
200488**      '/1c2/000+000001'         // Second page in overflow chain
200489**      '/1c2/000+000002'         // Third page in overflow chain
200490**
200491**   If the paths are sorted using the BINARY collation sequence, then
200492**   the overflow pages associated with a cell will appear earlier in the
200493**   sort-order than its child page:
200494**
200495**      '/1c2/000/'               // Left-most child of 451st child of root
200496*/
200497static const char zDbstatSchema[] =
200498  "CREATE TABLE x("
200499  " name       TEXT,"          /*  0 Name of table or index */
200500  " path       TEXT,"          /*  1 Path to page from root (NULL for agg) */
200501  " pageno     INTEGER,"       /*  2 Page number (page count for aggregates) */
200502  " pagetype   TEXT,"          /*  3 'internal', 'leaf', 'overflow', or NULL */
200503  " ncell      INTEGER,"       /*  4 Cells on page (0 for overflow) */
200504  " payload    INTEGER,"       /*  5 Bytes of payload on this page */
200505  " unused     INTEGER,"       /*  6 Bytes of unused space on this page */
200506  " mx_payload INTEGER,"       /*  7 Largest payload size of all cells */
200507  " pgoffset   INTEGER,"       /*  8 Offset of page in file (NULL for agg) */
200508  " pgsize     INTEGER,"       /*  9 Size of the page (sum for aggregate) */
200509  " schema     TEXT HIDDEN,"   /* 10 Database schema being analyzed */
200510  " aggregate  BOOLEAN HIDDEN" /* 11 aggregate info for each table */
200511  ")"
200512;
200513
200514/* Forward reference to data structured used in this module */
200515typedef struct StatTable StatTable;
200516typedef struct StatCursor StatCursor;
200517typedef struct StatPage StatPage;
200518typedef struct StatCell StatCell;
200519
200520/* Size information for a single cell within a btree page */
200521struct StatCell {
200522  int nLocal;                     /* Bytes of local payload */
200523  u32 iChildPg;                   /* Child node (or 0 if this is a leaf) */
200524  int nOvfl;                      /* Entries in aOvfl[] */
200525  u32 *aOvfl;                     /* Array of overflow page numbers */
200526  int nLastOvfl;                  /* Bytes of payload on final overflow page */
200527  int iOvfl;                      /* Iterates through aOvfl[] */
200528};
200529
200530/* Size information for a single btree page */
200531struct StatPage {
200532  u32 iPgno;                      /* Page number */
200533  DbPage *pPg;                    /* Page content */
200534  int iCell;                      /* Current cell */
200535
200536  char *zPath;                    /* Path to this page */
200537
200538  /* Variables populated by statDecodePage(): */
200539  u8 flags;                       /* Copy of flags byte */
200540  int nCell;                      /* Number of cells on page */
200541  int nUnused;                    /* Number of unused bytes on page */
200542  StatCell *aCell;                /* Array of parsed cells */
200543  u32 iRightChildPg;              /* Right-child page number (or 0) */
200544  int nMxPayload;                 /* Largest payload of any cell on the page */
200545};
200546
200547/* The cursor for scanning the dbstat virtual table */
200548struct StatCursor {
200549  sqlite3_vtab_cursor base;       /* base class.  MUST BE FIRST! */
200550  sqlite3_stmt *pStmt;            /* Iterates through set of root pages */
200551  u8 isEof;                       /* After pStmt has returned SQLITE_DONE */
200552  u8 isAgg;                       /* Aggregate results for each table */
200553  int iDb;                        /* Schema used for this query */
200554
200555  StatPage aPage[32];             /* Pages in path to current page */
200556  int iPage;                      /* Current entry in aPage[] */
200557
200558  /* Values to return. */
200559  u32 iPageno;                    /* Value of 'pageno' column */
200560  char *zName;                    /* Value of 'name' column */
200561  char *zPath;                    /* Value of 'path' column */
200562  char *zPagetype;                /* Value of 'pagetype' column */
200563  int nPage;                      /* Number of pages in current btree */
200564  int nCell;                      /* Value of 'ncell' column */
200565  int nMxPayload;                 /* Value of 'mx_payload' column */
200566  i64 nUnused;                    /* Value of 'unused' column */
200567  i64 nPayload;                   /* Value of 'payload' column */
200568  i64 iOffset;                    /* Value of 'pgOffset' column */
200569  i64 szPage;                     /* Value of 'pgSize' column */
200570};
200571
200572/* An instance of the DBSTAT virtual table */
200573struct StatTable {
200574  sqlite3_vtab base;              /* base class.  MUST BE FIRST! */
200575  sqlite3 *db;                    /* Database connection that owns this vtab */
200576  int iDb;                        /* Index of database to analyze */
200577};
200578
200579#ifndef get2byte
200580# define get2byte(x)   ((x)[0]<<8 | (x)[1])
200581#endif
200582
200583/*
200584** Connect to or create a new DBSTAT virtual table.
200585*/
200586static int statConnect(
200587  sqlite3 *db,
200588  void *pAux,
200589  int argc, const char *const*argv,
200590  sqlite3_vtab **ppVtab,
200591  char **pzErr
200592){
200593  StatTable *pTab = 0;
200594  int rc = SQLITE_OK;
200595  int iDb;
200596
200597  if( argc>=4 ){
200598    Token nm;
200599    sqlite3TokenInit(&nm, (char*)argv[3]);
200600    iDb = sqlite3FindDb(db, &nm);
200601    if( iDb<0 ){
200602      *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
200603      return SQLITE_ERROR;
200604    }
200605  }else{
200606    iDb = 0;
200607  }
200608  sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
200609  rc = sqlite3_declare_vtab(db, zDbstatSchema);
200610  if( rc==SQLITE_OK ){
200611    pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
200612    if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
200613  }
200614
200615  assert( rc==SQLITE_OK || pTab==0 );
200616  if( rc==SQLITE_OK ){
200617    memset(pTab, 0, sizeof(StatTable));
200618    pTab->db = db;
200619    pTab->iDb = iDb;
200620  }
200621
200622  *ppVtab = (sqlite3_vtab*)pTab;
200623  return rc;
200624}
200625
200626/*
200627** Disconnect from or destroy the DBSTAT virtual table.
200628*/
200629static int statDisconnect(sqlite3_vtab *pVtab){
200630  sqlite3_free(pVtab);
200631  return SQLITE_OK;
200632}
200633
200634/*
200635** Compute the best query strategy and return the result in idxNum.
200636**
200637**   idxNum-Bit        Meaning
200638**   ----------        ----------------------------------------------
200639**      0x01           There is a schema=? term in the WHERE clause
200640**      0x02           There is a name=? term in the WHERE clause
200641**      0x04           There is an aggregate=? term in the WHERE clause
200642**      0x08           Output should be ordered by name and path
200643*/
200644static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
200645  int i;
200646  int iSchema = -1;
200647  int iName = -1;
200648  int iAgg = -1;
200649
200650  /* Look for a valid schema=? constraint.  If found, change the idxNum to
200651  ** 1 and request the value of that constraint be sent to xFilter.  And
200652  ** lower the cost estimate to encourage the constrained version to be
200653  ** used.
200654  */
200655  for(i=0; i<pIdxInfo->nConstraint; i++){
200656    if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
200657    if( pIdxInfo->aConstraint[i].usable==0 ){
200658      /* Force DBSTAT table should always be the right-most table in a join */
200659      return SQLITE_CONSTRAINT;
200660    }
200661    switch( pIdxInfo->aConstraint[i].iColumn ){
200662      case 0: {    /* name */
200663        iName = i;
200664        break;
200665      }
200666      case 10: {   /* schema */
200667        iSchema = i;
200668        break;
200669      }
200670      case 11: {   /* aggregate */
200671        iAgg = i;
200672        break;
200673      }
200674    }
200675  }
200676  i = 0;
200677  if( iSchema>=0 ){
200678    pIdxInfo->aConstraintUsage[iSchema].argvIndex = ++i;
200679    pIdxInfo->aConstraintUsage[iSchema].omit = 1;
200680    pIdxInfo->idxNum |= 0x01;
200681  }
200682  if( iName>=0 ){
200683    pIdxInfo->aConstraintUsage[iName].argvIndex = ++i;
200684    pIdxInfo->idxNum |= 0x02;
200685  }
200686  if( iAgg>=0 ){
200687    pIdxInfo->aConstraintUsage[iAgg].argvIndex = ++i;
200688    pIdxInfo->idxNum |= 0x04;
200689  }
200690  pIdxInfo->estimatedCost = 1.0;
200691
200692  /* Records are always returned in ascending order of (name, path).
200693  ** If this will satisfy the client, set the orderByConsumed flag so that
200694  ** SQLite does not do an external sort.
200695  */
200696  if( ( pIdxInfo->nOrderBy==1
200697     && pIdxInfo->aOrderBy[0].iColumn==0
200698     && pIdxInfo->aOrderBy[0].desc==0
200699     ) ||
200700      ( pIdxInfo->nOrderBy==2
200701     && pIdxInfo->aOrderBy[0].iColumn==0
200702     && pIdxInfo->aOrderBy[0].desc==0
200703     && pIdxInfo->aOrderBy[1].iColumn==1
200704     && pIdxInfo->aOrderBy[1].desc==0
200705     )
200706  ){
200707    pIdxInfo->orderByConsumed = 1;
200708    pIdxInfo->idxNum |= 0x08;
200709  }
200710
200711  return SQLITE_OK;
200712}
200713
200714/*
200715** Open a new DBSTAT cursor.
200716*/
200717static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
200718  StatTable *pTab = (StatTable *)pVTab;
200719  StatCursor *pCsr;
200720
200721  pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
200722  if( pCsr==0 ){
200723    return SQLITE_NOMEM_BKPT;
200724  }else{
200725    memset(pCsr, 0, sizeof(StatCursor));
200726    pCsr->base.pVtab = pVTab;
200727    pCsr->iDb = pTab->iDb;
200728  }
200729
200730  *ppCursor = (sqlite3_vtab_cursor *)pCsr;
200731  return SQLITE_OK;
200732}
200733
200734static void statClearCells(StatPage *p){
200735  int i;
200736  if( p->aCell ){
200737    for(i=0; i<p->nCell; i++){
200738      sqlite3_free(p->aCell[i].aOvfl);
200739    }
200740    sqlite3_free(p->aCell);
200741  }
200742  p->nCell = 0;
200743  p->aCell = 0;
200744}
200745
200746static void statClearPage(StatPage *p){
200747  statClearCells(p);
200748  sqlite3PagerUnref(p->pPg);
200749  sqlite3_free(p->zPath);
200750  memset(p, 0, sizeof(StatPage));
200751}
200752
200753static void statResetCsr(StatCursor *pCsr){
200754  int i;
200755  sqlite3_reset(pCsr->pStmt);
200756  for(i=0; i<ArraySize(pCsr->aPage); i++){
200757    statClearPage(&pCsr->aPage[i]);
200758  }
200759  pCsr->iPage = 0;
200760  sqlite3_free(pCsr->zPath);
200761  pCsr->zPath = 0;
200762  pCsr->isEof = 0;
200763}
200764
200765/* Resize the space-used counters inside of the cursor */
200766static void statResetCounts(StatCursor *pCsr){
200767  pCsr->nCell = 0;
200768  pCsr->nMxPayload = 0;
200769  pCsr->nUnused = 0;
200770  pCsr->nPayload = 0;
200771  pCsr->szPage = 0;
200772  pCsr->nPage = 0;
200773}
200774
200775/*
200776** Close a DBSTAT cursor.
200777*/
200778static int statClose(sqlite3_vtab_cursor *pCursor){
200779  StatCursor *pCsr = (StatCursor *)pCursor;
200780  statResetCsr(pCsr);
200781  sqlite3_finalize(pCsr->pStmt);
200782  sqlite3_free(pCsr);
200783  return SQLITE_OK;
200784}
200785
200786/*
200787** For a single cell on a btree page, compute the number of bytes of
200788** content (payload) stored on that page.  That is to say, compute the
200789** number of bytes of content not found on overflow pages.
200790*/
200791static int getLocalPayload(
200792  int nUsable,                    /* Usable bytes per page */
200793  u8 flags,                       /* Page flags */
200794  int nTotal                      /* Total record (payload) size */
200795){
200796  int nLocal;
200797  int nMinLocal;
200798  int nMaxLocal;
200799
200800  if( flags==0x0D ){              /* Table leaf node */
200801    nMinLocal = (nUsable - 12) * 32 / 255 - 23;
200802    nMaxLocal = nUsable - 35;
200803  }else{                          /* Index interior and leaf nodes */
200804    nMinLocal = (nUsable - 12) * 32 / 255 - 23;
200805    nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
200806  }
200807
200808  nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
200809  if( nLocal>nMaxLocal ) nLocal = nMinLocal;
200810  return nLocal;
200811}
200812
200813/* Populate the StatPage object with information about the all
200814** cells found on the page currently under analysis.
200815*/
200816static int statDecodePage(Btree *pBt, StatPage *p){
200817  int nUnused;
200818  int iOff;
200819  int nHdr;
200820  int isLeaf;
200821  int szPage;
200822
200823  u8 *aData = sqlite3PagerGetData(p->pPg);
200824  u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
200825
200826  p->flags = aHdr[0];
200827  if( p->flags==0x0A || p->flags==0x0D ){
200828    isLeaf = 1;
200829    nHdr = 8;
200830  }else if( p->flags==0x05 || p->flags==0x02 ){
200831    isLeaf = 0;
200832    nHdr = 12;
200833  }else{
200834    goto statPageIsCorrupt;
200835  }
200836  if( p->iPgno==1 ) nHdr += 100;
200837  p->nCell = get2byte(&aHdr[3]);
200838  p->nMxPayload = 0;
200839  szPage = sqlite3BtreeGetPageSize(pBt);
200840
200841  nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
200842  nUnused += (int)aHdr[7];
200843  iOff = get2byte(&aHdr[1]);
200844  while( iOff ){
200845    int iNext;
200846    if( iOff>=szPage ) goto statPageIsCorrupt;
200847    nUnused += get2byte(&aData[iOff+2]);
200848    iNext = get2byte(&aData[iOff]);
200849    if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt;
200850    iOff = iNext;
200851  }
200852  p->nUnused = nUnused;
200853  p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
200854
200855  if( p->nCell ){
200856    int i;                        /* Used to iterate through cells */
200857    int nUsable;                  /* Usable bytes per page */
200858
200859    sqlite3BtreeEnter(pBt);
200860    nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
200861    sqlite3BtreeLeave(pBt);
200862    p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
200863    if( p->aCell==0 ) return SQLITE_NOMEM_BKPT;
200864    memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
200865
200866    for(i=0; i<p->nCell; i++){
200867      StatCell *pCell = &p->aCell[i];
200868
200869      iOff = get2byte(&aData[nHdr+i*2]);
200870      if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt;
200871      if( !isLeaf ){
200872        pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
200873        iOff += 4;
200874      }
200875      if( p->flags==0x05 ){
200876        /* A table interior node. nPayload==0. */
200877      }else{
200878        u32 nPayload;             /* Bytes of payload total (local+overflow) */
200879        int nLocal;               /* Bytes of payload stored locally */
200880        iOff += getVarint32(&aData[iOff], nPayload);
200881        if( p->flags==0x0D ){
200882          u64 dummy;
200883          iOff += sqlite3GetVarint(&aData[iOff], &dummy);
200884        }
200885        if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
200886        nLocal = getLocalPayload(nUsable, p->flags, nPayload);
200887        if( nLocal<0 ) goto statPageIsCorrupt;
200888        pCell->nLocal = nLocal;
200889        assert( nPayload>=(u32)nLocal );
200890        assert( nLocal<=(nUsable-35) );
200891        if( nPayload>(u32)nLocal ){
200892          int j;
200893          int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
200894          if( iOff+nLocal>nUsable || nPayload>0x7fffffff ){
200895            goto statPageIsCorrupt;
200896          }
200897          pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
200898          pCell->nOvfl = nOvfl;
200899          pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
200900          if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
200901          pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
200902          for(j=1; j<nOvfl; j++){
200903            int rc;
200904            u32 iPrev = pCell->aOvfl[j-1];
200905            DbPage *pPg = 0;
200906            rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0);
200907            if( rc!=SQLITE_OK ){
200908              assert( pPg==0 );
200909              return rc;
200910            }
200911            pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
200912            sqlite3PagerUnref(pPg);
200913          }
200914        }
200915      }
200916    }
200917  }
200918
200919  return SQLITE_OK;
200920
200921statPageIsCorrupt:
200922  p->flags = 0;
200923  statClearCells(p);
200924  return SQLITE_OK;
200925}
200926
200927/*
200928** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
200929** the current value of pCsr->iPageno.
200930*/
200931static void statSizeAndOffset(StatCursor *pCsr){
200932  StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
200933  Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
200934  Pager *pPager = sqlite3BtreePager(pBt);
200935  sqlite3_file *fd;
200936  sqlite3_int64 x[2];
200937
200938  /* If connected to a ZIPVFS backend, find the page size and
200939  ** offset from ZIPVFS.
200940  */
200941  fd = sqlite3PagerFile(pPager);
200942  x[0] = pCsr->iPageno;
200943  if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
200944    pCsr->iOffset = x[0];
200945    pCsr->szPage += x[1];
200946  }else{
200947    /* Not ZIPVFS: The default page size and offset */
200948    pCsr->szPage += sqlite3BtreeGetPageSize(pBt);
200949    pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
200950  }
200951}
200952
200953/*
200954** Move a DBSTAT cursor to the next entry.  Normally, the next
200955** entry will be the next page, but in aggregated mode (pCsr->isAgg!=0),
200956** the next entry is the next btree.
200957*/
200958static int statNext(sqlite3_vtab_cursor *pCursor){
200959  int rc;
200960  int nPayload;
200961  char *z;
200962  StatCursor *pCsr = (StatCursor *)pCursor;
200963  StatTable *pTab = (StatTable *)pCursor->pVtab;
200964  Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
200965  Pager *pPager = sqlite3BtreePager(pBt);
200966
200967  sqlite3_free(pCsr->zPath);
200968  pCsr->zPath = 0;
200969
200970statNextRestart:
200971  if( pCsr->aPage[0].pPg==0 ){
200972    /* Start measuring space on the next btree */
200973    statResetCounts(pCsr);
200974    rc = sqlite3_step(pCsr->pStmt);
200975    if( rc==SQLITE_ROW ){
200976      int nPage;
200977      u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
200978      sqlite3PagerPagecount(pPager, &nPage);
200979      if( nPage==0 ){
200980        pCsr->isEof = 1;
200981        return sqlite3_reset(pCsr->pStmt);
200982      }
200983      rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg, 0);
200984      pCsr->aPage[0].iPgno = iRoot;
200985      pCsr->aPage[0].iCell = 0;
200986      if( !pCsr->isAgg ){
200987        pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
200988        if( z==0 ) rc = SQLITE_NOMEM_BKPT;
200989      }
200990      pCsr->iPage = 0;
200991      pCsr->nPage = 1;
200992    }else{
200993      pCsr->isEof = 1;
200994      return sqlite3_reset(pCsr->pStmt);
200995    }
200996  }else{
200997    /* Continue analyzing the btree previously started */
200998    StatPage *p = &pCsr->aPage[pCsr->iPage];
200999    if( !pCsr->isAgg ) statResetCounts(pCsr);
201000    while( p->iCell<p->nCell ){
201001      StatCell *pCell = &p->aCell[p->iCell];
201002      while( pCell->iOvfl<pCell->nOvfl ){
201003        int nUsable, iOvfl;
201004        sqlite3BtreeEnter(pBt);
201005        nUsable = sqlite3BtreeGetPageSize(pBt) -
201006                        sqlite3BtreeGetReserveNoMutex(pBt);
201007        sqlite3BtreeLeave(pBt);
201008        pCsr->nPage++;
201009        statSizeAndOffset(pCsr);
201010        if( pCell->iOvfl<pCell->nOvfl-1 ){
201011          pCsr->nPayload += nUsable - 4;
201012        }else{
201013          pCsr->nPayload += pCell->nLastOvfl;
201014          pCsr->nUnused += nUsable - 4 - pCell->nLastOvfl;
201015        }
201016        iOvfl = pCell->iOvfl;
201017        pCell->iOvfl++;
201018        if( !pCsr->isAgg ){
201019          pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
201020          pCsr->iPageno = pCell->aOvfl[iOvfl];
201021          pCsr->zPagetype = "overflow";
201022          pCsr->zPath = z = sqlite3_mprintf(
201023              "%s%.3x+%.6x", p->zPath, p->iCell, iOvfl
201024          );
201025          return z==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
201026        }
201027      }
201028      if( p->iRightChildPg ) break;
201029      p->iCell++;
201030    }
201031
201032    if( !p->iRightChildPg || p->iCell>p->nCell ){
201033      statClearPage(p);
201034      if( pCsr->iPage>0 ){
201035        pCsr->iPage--;
201036      }else if( pCsr->isAgg ){
201037        /* label-statNext-done:  When computing aggregate space usage over
201038        ** an entire btree, this is the exit point from this function */
201039        return SQLITE_OK;
201040      }
201041      goto statNextRestart; /* Tail recursion */
201042    }
201043    pCsr->iPage++;
201044    if( pCsr->iPage>=ArraySize(pCsr->aPage) ){
201045      statResetCsr(pCsr);
201046      return SQLITE_CORRUPT_BKPT;
201047    }
201048    assert( p==&pCsr->aPage[pCsr->iPage-1] );
201049
201050    if( p->iCell==p->nCell ){
201051      p[1].iPgno = p->iRightChildPg;
201052    }else{
201053      p[1].iPgno = p->aCell[p->iCell].iChildPg;
201054    }
201055    rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg, 0);
201056    pCsr->nPage++;
201057    p[1].iCell = 0;
201058    if( !pCsr->isAgg ){
201059      p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
201060      if( z==0 ) rc = SQLITE_NOMEM_BKPT;
201061    }
201062    p->iCell++;
201063  }
201064
201065
201066  /* Populate the StatCursor fields with the values to be returned
201067  ** by the xColumn() and xRowid() methods.
201068  */
201069  if( rc==SQLITE_OK ){
201070    int i;
201071    StatPage *p = &pCsr->aPage[pCsr->iPage];
201072    pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
201073    pCsr->iPageno = p->iPgno;
201074
201075    rc = statDecodePage(pBt, p);
201076    if( rc==SQLITE_OK ){
201077      statSizeAndOffset(pCsr);
201078
201079      switch( p->flags ){
201080        case 0x05:             /* table internal */
201081        case 0x02:             /* index internal */
201082          pCsr->zPagetype = "internal";
201083          break;
201084        case 0x0D:             /* table leaf */
201085        case 0x0A:             /* index leaf */
201086          pCsr->zPagetype = "leaf";
201087          break;
201088        default:
201089          pCsr->zPagetype = "corrupted";
201090          break;
201091      }
201092      pCsr->nCell += p->nCell;
201093      pCsr->nUnused += p->nUnused;
201094      if( p->nMxPayload>pCsr->nMxPayload ) pCsr->nMxPayload = p->nMxPayload;
201095      if( !pCsr->isAgg ){
201096        pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
201097        if( z==0 ) rc = SQLITE_NOMEM_BKPT;
201098      }
201099      nPayload = 0;
201100      for(i=0; i<p->nCell; i++){
201101        nPayload += p->aCell[i].nLocal;
201102      }
201103      pCsr->nPayload += nPayload;
201104
201105      /* If computing aggregate space usage by btree, continue with the
201106      ** next page.  The loop will exit via the return at label-statNext-done
201107      */
201108      if( pCsr->isAgg ) goto statNextRestart;
201109    }
201110  }
201111
201112  return rc;
201113}
201114
201115static int statEof(sqlite3_vtab_cursor *pCursor){
201116  StatCursor *pCsr = (StatCursor *)pCursor;
201117  return pCsr->isEof;
201118}
201119
201120/* Initialize a cursor according to the query plan idxNum using the
201121** arguments in argv[0].  See statBestIndex() for a description of the
201122** meaning of the bits in idxNum.
201123*/
201124static int statFilter(
201125  sqlite3_vtab_cursor *pCursor,
201126  int idxNum, const char *idxStr,
201127  int argc, sqlite3_value **argv
201128){
201129  StatCursor *pCsr = (StatCursor *)pCursor;
201130  StatTable *pTab = (StatTable*)(pCursor->pVtab);
201131  sqlite3_str *pSql;      /* Query of btrees to analyze */
201132  char *zSql;             /* String value of pSql */
201133  int iArg = 0;           /* Count of argv[] parameters used so far */
201134  int rc = SQLITE_OK;     /* Result of this operation */
201135  const char *zName = 0;  /* Only provide analysis of this table */
201136
201137  statResetCsr(pCsr);
201138  sqlite3_finalize(pCsr->pStmt);
201139  pCsr->pStmt = 0;
201140  if( idxNum & 0x01 ){
201141    /* schema=? constraint is present.  Get its value */
201142    const char *zDbase = (const char*)sqlite3_value_text(argv[iArg++]);
201143    pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
201144    if( pCsr->iDb<0 ){
201145      pCsr->iDb = 0;
201146      pCsr->isEof = 1;
201147      return SQLITE_OK;
201148    }
201149  }else{
201150    pCsr->iDb = pTab->iDb;
201151  }
201152  if( idxNum & 0x02 ){
201153    /* name=? constraint is present */
201154    zName = (const char*)sqlite3_value_text(argv[iArg++]);
201155  }
201156  if( idxNum & 0x04 ){
201157    /* aggregate=? constraint is present */
201158    pCsr->isAgg = sqlite3_value_double(argv[iArg++])!=0.0;
201159  }else{
201160    pCsr->isAgg = 0;
201161  }
201162  pSql = sqlite3_str_new(pTab->db);
201163  sqlite3_str_appendf(pSql,
201164      "SELECT * FROM ("
201165        "SELECT 'sqlite_master' AS name,1 AS rootpage,'table' AS type"
201166        " UNION ALL "
201167        "SELECT name,rootpage,type"
201168        " FROM \"%w\".sqlite_master WHERE rootpage!=0)",
201169      pTab->db->aDb[pCsr->iDb].zDbSName);
201170  if( zName ){
201171    sqlite3_str_appendf(pSql, "WHERE name=%Q", zName);
201172  }
201173  if( idxNum & 0x08 ){
201174    sqlite3_str_appendf(pSql, " ORDER BY name");
201175  }
201176  zSql = sqlite3_str_finish(pSql);
201177  if( zSql==0 ){
201178    return SQLITE_NOMEM_BKPT;
201179  }else{
201180    rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
201181    sqlite3_free(zSql);
201182  }
201183
201184  if( rc==SQLITE_OK ){
201185    rc = statNext(pCursor);
201186  }
201187  return rc;
201188}
201189
201190static int statColumn(
201191  sqlite3_vtab_cursor *pCursor,
201192  sqlite3_context *ctx,
201193  int i
201194){
201195  StatCursor *pCsr = (StatCursor *)pCursor;
201196  switch( i ){
201197    case 0:            /* name */
201198      sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT);
201199      break;
201200    case 1:            /* path */
201201      if( !pCsr->isAgg ){
201202        sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
201203      }
201204      break;
201205    case 2:            /* pageno */
201206      if( pCsr->isAgg ){
201207        sqlite3_result_int64(ctx, pCsr->nPage);
201208      }else{
201209        sqlite3_result_int64(ctx, pCsr->iPageno);
201210      }
201211      break;
201212    case 3:            /* pagetype */
201213      if( !pCsr->isAgg ){
201214        sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
201215      }
201216      break;
201217    case 4:            /* ncell */
201218      sqlite3_result_int(ctx, pCsr->nCell);
201219      break;
201220    case 5:            /* payload */
201221      sqlite3_result_int(ctx, pCsr->nPayload);
201222      break;
201223    case 6:            /* unused */
201224      sqlite3_result_int(ctx, pCsr->nUnused);
201225      break;
201226    case 7:            /* mx_payload */
201227      sqlite3_result_int(ctx, pCsr->nMxPayload);
201228      break;
201229    case 8:            /* pgoffset */
201230      if( !pCsr->isAgg ){
201231        sqlite3_result_int64(ctx, pCsr->iOffset);
201232      }
201233      break;
201234    case 9:            /* pgsize */
201235      sqlite3_result_int(ctx, pCsr->szPage);
201236      break;
201237    case 10: {         /* schema */
201238      sqlite3 *db = sqlite3_context_db_handle(ctx);
201239      int iDb = pCsr->iDb;
201240      sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC);
201241      break;
201242    }
201243    default: {         /* aggregate */
201244      sqlite3_result_int(ctx, pCsr->isAgg);
201245      break;
201246    }
201247  }
201248  return SQLITE_OK;
201249}
201250
201251static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
201252  StatCursor *pCsr = (StatCursor *)pCursor;
201253  *pRowid = pCsr->iPageno;
201254  return SQLITE_OK;
201255}
201256
201257/*
201258** Invoke this routine to register the "dbstat" virtual table module
201259*/
201260SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){
201261  static sqlite3_module dbstat_module = {
201262    0,                            /* iVersion */
201263    statConnect,                  /* xCreate */
201264    statConnect,                  /* xConnect */
201265    statBestIndex,                /* xBestIndex */
201266    statDisconnect,               /* xDisconnect */
201267    statDisconnect,               /* xDestroy */
201268    statOpen,                     /* xOpen - open a cursor */
201269    statClose,                    /* xClose - close a cursor */
201270    statFilter,                   /* xFilter - configure scan constraints */
201271    statNext,                     /* xNext - advance a cursor */
201272    statEof,                      /* xEof - check for end of scan */
201273    statColumn,                   /* xColumn - read data */
201274    statRowid,                    /* xRowid - read data */
201275    0,                            /* xUpdate */
201276    0,                            /* xBegin */
201277    0,                            /* xSync */
201278    0,                            /* xCommit */
201279    0,                            /* xRollback */
201280    0,                            /* xFindMethod */
201281    0,                            /* xRename */
201282    0,                            /* xSavepoint */
201283    0,                            /* xRelease */
201284    0,                            /* xRollbackTo */
201285    0                             /* xShadowName */
201286  };
201287  return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
201288}
201289#elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
201290SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
201291#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
201292
201293/************** End of dbstat.c **********************************************/
201294/************** Begin file dbpage.c ******************************************/
201295/*
201296** 2017-10-11
201297**
201298** The author disclaims copyright to this source code.  In place of
201299** a legal notice, here is a blessing:
201300**
201301**    May you do good and not evil.
201302**    May you find forgiveness for yourself and forgive others.
201303**    May you share freely, never taking more than you give.
201304**
201305******************************************************************************
201306**
201307** This file contains an implementation of the "sqlite_dbpage" virtual table.
201308**
201309** The sqlite_dbpage virtual table is used to read or write whole raw
201310** pages of the database file.  The pager interface is used so that
201311** uncommitted changes and changes recorded in the WAL file are correctly
201312** retrieved.
201313**
201314** Usage example:
201315**
201316**    SELECT data FROM sqlite_dbpage('aux1') WHERE pgno=123;
201317**
201318** This is an eponymous virtual table so it does not need to be created before
201319** use.  The optional argument to the sqlite_dbpage() table name is the
201320** schema for the database file that is to be read.  The default schema is
201321** "main".
201322**
201323** The data field of sqlite_dbpage table can be updated.  The new
201324** value must be a BLOB which is the correct page size, otherwise the
201325** update fails.  Rows may not be deleted or inserted.
201326*/
201327
201328/* #include "sqliteInt.h"   ** Requires access to internal data structures ** */
201329#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
201330    && !defined(SQLITE_OMIT_VIRTUALTABLE)
201331
201332typedef struct DbpageTable DbpageTable;
201333typedef struct DbpageCursor DbpageCursor;
201334
201335struct DbpageCursor {
201336  sqlite3_vtab_cursor base;       /* Base class.  Must be first */
201337  int pgno;                       /* Current page number */
201338  int mxPgno;                     /* Last page to visit on this scan */
201339  Pager *pPager;                  /* Pager being read/written */
201340  DbPage *pPage1;                 /* Page 1 of the database */
201341  int iDb;                        /* Index of database to analyze */
201342  int szPage;                     /* Size of each page in bytes */
201343};
201344
201345struct DbpageTable {
201346  sqlite3_vtab base;              /* Base class.  Must be first */
201347  sqlite3 *db;                    /* The database */
201348};
201349
201350/* Columns */
201351#define DBPAGE_COLUMN_PGNO    0
201352#define DBPAGE_COLUMN_DATA    1
201353#define DBPAGE_COLUMN_SCHEMA  2
201354
201355
201356
201357/*
201358** Connect to or create a dbpagevfs virtual table.
201359*/
201360static int dbpageConnect(
201361  sqlite3 *db,
201362  void *pAux,
201363  int argc, const char *const*argv,
201364  sqlite3_vtab **ppVtab,
201365  char **pzErr
201366){
201367  DbpageTable *pTab = 0;
201368  int rc = SQLITE_OK;
201369
201370  sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
201371  rc = sqlite3_declare_vtab(db,
201372          "CREATE TABLE x(pgno INTEGER PRIMARY KEY, data BLOB, schema HIDDEN)");
201373  if( rc==SQLITE_OK ){
201374    pTab = (DbpageTable *)sqlite3_malloc64(sizeof(DbpageTable));
201375    if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
201376  }
201377
201378  assert( rc==SQLITE_OK || pTab==0 );
201379  if( rc==SQLITE_OK ){
201380    memset(pTab, 0, sizeof(DbpageTable));
201381    pTab->db = db;
201382  }
201383
201384  *ppVtab = (sqlite3_vtab*)pTab;
201385  return rc;
201386}
201387
201388/*
201389** Disconnect from or destroy a dbpagevfs virtual table.
201390*/
201391static int dbpageDisconnect(sqlite3_vtab *pVtab){
201392  sqlite3_free(pVtab);
201393  return SQLITE_OK;
201394}
201395
201396/*
201397** idxNum:
201398**
201399**     0     schema=main, full table scan
201400**     1     schema=main, pgno=?1
201401**     2     schema=?1, full table scan
201402**     3     schema=?1, pgno=?2
201403*/
201404static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
201405  int i;
201406  int iPlan = 0;
201407
201408  /* If there is a schema= constraint, it must be honored.  Report a
201409  ** ridiculously large estimated cost if the schema= constraint is
201410  ** unavailable
201411  */
201412  for(i=0; i<pIdxInfo->nConstraint; i++){
201413    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
201414    if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue;
201415    if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
201416    if( !p->usable ){
201417      /* No solution. */
201418      return SQLITE_CONSTRAINT;
201419    }
201420    iPlan = 2;
201421    pIdxInfo->aConstraintUsage[i].argvIndex = 1;
201422    pIdxInfo->aConstraintUsage[i].omit = 1;
201423    break;
201424  }
201425
201426  /* If we reach this point, it means that either there is no schema=
201427  ** constraint (in which case we use the "main" schema) or else the
201428  ** schema constraint was accepted.  Lower the estimated cost accordingly
201429  */
201430  pIdxInfo->estimatedCost = 1.0e6;
201431
201432  /* Check for constraints against pgno */
201433  for(i=0; i<pIdxInfo->nConstraint; i++){
201434    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
201435    if( p->usable && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
201436      pIdxInfo->estimatedRows = 1;
201437      pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
201438      pIdxInfo->estimatedCost = 1.0;
201439      pIdxInfo->aConstraintUsage[i].argvIndex = iPlan ? 2 : 1;
201440      pIdxInfo->aConstraintUsage[i].omit = 1;
201441      iPlan |= 1;
201442      break;
201443    }
201444  }
201445  pIdxInfo->idxNum = iPlan;
201446
201447  if( pIdxInfo->nOrderBy>=1
201448   && pIdxInfo->aOrderBy[0].iColumn<=0
201449   && pIdxInfo->aOrderBy[0].desc==0
201450  ){
201451    pIdxInfo->orderByConsumed = 1;
201452  }
201453  return SQLITE_OK;
201454}
201455
201456/*
201457** Open a new dbpagevfs cursor.
201458*/
201459static int dbpageOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
201460  DbpageCursor *pCsr;
201461
201462  pCsr = (DbpageCursor *)sqlite3_malloc64(sizeof(DbpageCursor));
201463  if( pCsr==0 ){
201464    return SQLITE_NOMEM_BKPT;
201465  }else{
201466    memset(pCsr, 0, sizeof(DbpageCursor));
201467    pCsr->base.pVtab = pVTab;
201468    pCsr->pgno = -1;
201469  }
201470
201471  *ppCursor = (sqlite3_vtab_cursor *)pCsr;
201472  return SQLITE_OK;
201473}
201474
201475/*
201476** Close a dbpagevfs cursor.
201477*/
201478static int dbpageClose(sqlite3_vtab_cursor *pCursor){
201479  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
201480  if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
201481  sqlite3_free(pCsr);
201482  return SQLITE_OK;
201483}
201484
201485/*
201486** Move a dbpagevfs cursor to the next entry in the file.
201487*/
201488static int dbpageNext(sqlite3_vtab_cursor *pCursor){
201489  int rc = SQLITE_OK;
201490  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
201491  pCsr->pgno++;
201492  return rc;
201493}
201494
201495static int dbpageEof(sqlite3_vtab_cursor *pCursor){
201496  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
201497  return pCsr->pgno > pCsr->mxPgno;
201498}
201499
201500/*
201501** idxNum:
201502**
201503**     0     schema=main, full table scan
201504**     1     schema=main, pgno=?1
201505**     2     schema=?1, full table scan
201506**     3     schema=?1, pgno=?2
201507**
201508** idxStr is not used
201509*/
201510static int dbpageFilter(
201511  sqlite3_vtab_cursor *pCursor,
201512  int idxNum, const char *idxStr,
201513  int argc, sqlite3_value **argv
201514){
201515  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
201516  DbpageTable *pTab = (DbpageTable *)pCursor->pVtab;
201517  int rc;
201518  sqlite3 *db = pTab->db;
201519  Btree *pBt;
201520
201521  /* Default setting is no rows of result */
201522  pCsr->pgno = 1;
201523  pCsr->mxPgno = 0;
201524
201525  if( idxNum & 2 ){
201526    const char *zSchema;
201527    assert( argc>=1 );
201528    zSchema = (const char*)sqlite3_value_text(argv[0]);
201529    pCsr->iDb = sqlite3FindDbName(db, zSchema);
201530    if( pCsr->iDb<0 ) return SQLITE_OK;
201531  }else{
201532    pCsr->iDb = 0;
201533  }
201534  pBt = db->aDb[pCsr->iDb].pBt;
201535  if( pBt==0 ) return SQLITE_OK;
201536  pCsr->pPager = sqlite3BtreePager(pBt);
201537  pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
201538  pCsr->mxPgno = sqlite3BtreeLastPage(pBt);
201539  if( idxNum & 1 ){
201540    assert( argc>(idxNum>>1) );
201541    pCsr->pgno = sqlite3_value_int(argv[idxNum>>1]);
201542    if( pCsr->pgno<1 || pCsr->pgno>pCsr->mxPgno ){
201543      pCsr->pgno = 1;
201544      pCsr->mxPgno = 0;
201545    }else{
201546      pCsr->mxPgno = pCsr->pgno;
201547    }
201548  }else{
201549    assert( pCsr->pgno==1 );
201550  }
201551  if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
201552  rc = sqlite3PagerGet(pCsr->pPager, 1, &pCsr->pPage1, 0);
201553  return rc;
201554}
201555
201556static int dbpageColumn(
201557  sqlite3_vtab_cursor *pCursor,
201558  sqlite3_context *ctx,
201559  int i
201560){
201561  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
201562  int rc = SQLITE_OK;
201563  switch( i ){
201564    case 0: {           /* pgno */
201565      sqlite3_result_int(ctx, pCsr->pgno);
201566      break;
201567    }
201568    case 1: {           /* data */
201569      DbPage *pDbPage = 0;
201570      rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
201571      if( rc==SQLITE_OK ){
201572        sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
201573                            SQLITE_TRANSIENT);
201574      }
201575      sqlite3PagerUnref(pDbPage);
201576      break;
201577    }
201578    default: {          /* schema */
201579      sqlite3 *db = sqlite3_context_db_handle(ctx);
201580      sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC);
201581      break;
201582    }
201583  }
201584  return SQLITE_OK;
201585}
201586
201587static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
201588  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
201589  *pRowid = pCsr->pgno;
201590  return SQLITE_OK;
201591}
201592
201593static int dbpageUpdate(
201594  sqlite3_vtab *pVtab,
201595  int argc,
201596  sqlite3_value **argv,
201597  sqlite_int64 *pRowid
201598){
201599  DbpageTable *pTab = (DbpageTable *)pVtab;
201600  Pgno pgno;
201601  DbPage *pDbPage = 0;
201602  int rc = SQLITE_OK;
201603  char *zErr = 0;
201604  const char *zSchema;
201605  int iDb;
201606  Btree *pBt;
201607  Pager *pPager;
201608  int szPage;
201609
201610  if( pTab->db->flags & SQLITE_Defensive ){
201611    zErr = "read-only";
201612    goto update_fail;
201613  }
201614  if( argc==1 ){
201615    zErr = "cannot delete";
201616    goto update_fail;
201617  }
201618  pgno = sqlite3_value_int(argv[0]);
201619  if( (Pgno)sqlite3_value_int(argv[1])!=pgno ){
201620    zErr = "cannot insert";
201621    goto update_fail;
201622  }
201623  zSchema = (const char*)sqlite3_value_text(argv[4]);
201624  iDb = zSchema ? sqlite3FindDbName(pTab->db, zSchema) : -1;
201625  if( iDb<0 ){
201626    zErr = "no such schema";
201627    goto update_fail;
201628  }
201629  pBt = pTab->db->aDb[iDb].pBt;
201630  if( pgno<1 || pBt==0 || pgno>(int)sqlite3BtreeLastPage(pBt) ){
201631    zErr = "bad page number";
201632    goto update_fail;
201633  }
201634  szPage = sqlite3BtreeGetPageSize(pBt);
201635  if( sqlite3_value_type(argv[3])!=SQLITE_BLOB
201636   || sqlite3_value_bytes(argv[3])!=szPage
201637  ){
201638    zErr = "bad page value";
201639    goto update_fail;
201640  }
201641  pPager = sqlite3BtreePager(pBt);
201642  rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
201643  if( rc==SQLITE_OK ){
201644    rc = sqlite3PagerWrite(pDbPage);
201645    if( rc==SQLITE_OK ){
201646      memcpy(sqlite3PagerGetData(pDbPage),
201647             sqlite3_value_blob(argv[3]),
201648             szPage);
201649    }
201650  }
201651  sqlite3PagerUnref(pDbPage);
201652  return rc;
201653
201654update_fail:
201655  sqlite3_free(pVtab->zErrMsg);
201656  pVtab->zErrMsg = sqlite3_mprintf("%s", zErr);
201657  return SQLITE_ERROR;
201658}
201659
201660/* Since we do not know in advance which database files will be
201661** written by the sqlite_dbpage virtual table, start a write transaction
201662** on them all.
201663*/
201664static int dbpageBegin(sqlite3_vtab *pVtab){
201665  DbpageTable *pTab = (DbpageTable *)pVtab;
201666  sqlite3 *db = pTab->db;
201667  int i;
201668  for(i=0; i<db->nDb; i++){
201669    Btree *pBt = db->aDb[i].pBt;
201670    if( pBt ) sqlite3BtreeBeginTrans(pBt, 1, 0);
201671  }
201672  return SQLITE_OK;
201673}
201674
201675
201676/*
201677** Invoke this routine to register the "dbpage" virtual table module
201678*/
201679SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){
201680  static sqlite3_module dbpage_module = {
201681    0,                            /* iVersion */
201682    dbpageConnect,                /* xCreate */
201683    dbpageConnect,                /* xConnect */
201684    dbpageBestIndex,              /* xBestIndex */
201685    dbpageDisconnect,             /* xDisconnect */
201686    dbpageDisconnect,             /* xDestroy */
201687    dbpageOpen,                   /* xOpen - open a cursor */
201688    dbpageClose,                  /* xClose - close a cursor */
201689    dbpageFilter,                 /* xFilter - configure scan constraints */
201690    dbpageNext,                   /* xNext - advance a cursor */
201691    dbpageEof,                    /* xEof - check for end of scan */
201692    dbpageColumn,                 /* xColumn - read data */
201693    dbpageRowid,                  /* xRowid - read data */
201694    dbpageUpdate,                 /* xUpdate */
201695    dbpageBegin,                  /* xBegin */
201696    0,                            /* xSync */
201697    0,                            /* xCommit */
201698    0,                            /* xRollback */
201699    0,                            /* xFindMethod */
201700    0,                            /* xRename */
201701    0,                            /* xSavepoint */
201702    0,                            /* xRelease */
201703    0,                            /* xRollbackTo */
201704    0                             /* xShadowName */
201705  };
201706  return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0);
201707}
201708#elif defined(SQLITE_ENABLE_DBPAGE_VTAB)
201709SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK; }
201710#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
201711
201712/************** End of dbpage.c **********************************************/
201713/************** Begin file sqlite3session.c **********************************/
201714
201715#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
201716/* #include "sqlite3session.h" */
201717/* #include <assert.h> */
201718/* #include <string.h> */
201719
201720#ifndef SQLITE_AMALGAMATION
201721/* # include "sqliteInt.h" */
201722/* # include "vdbeInt.h" */
201723#endif
201724
201725typedef struct SessionTable SessionTable;
201726typedef struct SessionChange SessionChange;
201727typedef struct SessionBuffer SessionBuffer;
201728typedef struct SessionInput SessionInput;
201729
201730/*
201731** Minimum chunk size used by streaming versions of functions.
201732*/
201733#ifndef SESSIONS_STRM_CHUNK_SIZE
201734# ifdef SQLITE_TEST
201735#   define SESSIONS_STRM_CHUNK_SIZE 64
201736# else
201737#   define SESSIONS_STRM_CHUNK_SIZE 1024
201738# endif
201739#endif
201740
201741static int sessions_strm_chunk_size = SESSIONS_STRM_CHUNK_SIZE;
201742
201743typedef struct SessionHook SessionHook;
201744struct SessionHook {
201745  void *pCtx;
201746  int (*xOld)(void*,int,sqlite3_value**);
201747  int (*xNew)(void*,int,sqlite3_value**);
201748  int (*xCount)(void*);
201749  int (*xDepth)(void*);
201750};
201751
201752/*
201753** Session handle structure.
201754*/
201755struct sqlite3_session {
201756  sqlite3 *db;                    /* Database handle session is attached to */
201757  char *zDb;                      /* Name of database session is attached to */
201758  int bEnable;                    /* True if currently recording */
201759  int bIndirect;                  /* True if all changes are indirect */
201760  int bAutoAttach;                /* True to auto-attach tables */
201761  int rc;                         /* Non-zero if an error has occurred */
201762  void *pFilterCtx;               /* First argument to pass to xTableFilter */
201763  int (*xTableFilter)(void *pCtx, const char *zTab);
201764  sqlite3_value *pZeroBlob;       /* Value containing X'' */
201765  sqlite3_session *pNext;         /* Next session object on same db. */
201766  SessionTable *pTable;           /* List of attached tables */
201767  SessionHook hook;               /* APIs to grab new and old data with */
201768};
201769
201770/*
201771** Instances of this structure are used to build strings or binary records.
201772*/
201773struct SessionBuffer {
201774  u8 *aBuf;                       /* Pointer to changeset buffer */
201775  int nBuf;                       /* Size of buffer aBuf */
201776  int nAlloc;                     /* Size of allocation containing aBuf */
201777};
201778
201779/*
201780** An object of this type is used internally as an abstraction for
201781** input data. Input data may be supplied either as a single large buffer
201782** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
201783**  sqlite3changeset_start_strm()).
201784*/
201785struct SessionInput {
201786  int bNoDiscard;                 /* If true, do not discard in InputBuffer() */
201787  int iCurrent;                   /* Offset in aData[] of current change */
201788  int iNext;                      /* Offset in aData[] of next change */
201789  u8 *aData;                      /* Pointer to buffer containing changeset */
201790  int nData;                      /* Number of bytes in aData */
201791
201792  SessionBuffer buf;              /* Current read buffer */
201793  int (*xInput)(void*, void*, int*);        /* Input stream call (or NULL) */
201794  void *pIn;                                /* First argument to xInput */
201795  int bEof;                       /* Set to true after xInput finished */
201796};
201797
201798/*
201799** Structure for changeset iterators.
201800*/
201801struct sqlite3_changeset_iter {
201802  SessionInput in;                /* Input buffer or stream */
201803  SessionBuffer tblhdr;           /* Buffer to hold apValue/zTab/abPK/ */
201804  int bPatchset;                  /* True if this is a patchset */
201805  int bInvert;                    /* True to invert changeset */
201806  int rc;                         /* Iterator error code */
201807  sqlite3_stmt *pConflict;        /* Points to conflicting row, if any */
201808  char *zTab;                     /* Current table */
201809  int nCol;                       /* Number of columns in zTab */
201810  int op;                         /* Current operation */
201811  int bIndirect;                  /* True if current change was indirect */
201812  u8 *abPK;                       /* Primary key array */
201813  sqlite3_value **apValue;        /* old.* and new.* values */
201814};
201815
201816/*
201817** Each session object maintains a set of the following structures, one
201818** for each table the session object is monitoring. The structures are
201819** stored in a linked list starting at sqlite3_session.pTable.
201820**
201821** The keys of the SessionTable.aChange[] hash table are all rows that have
201822** been modified in any way since the session object was attached to the
201823** table.
201824**
201825** The data associated with each hash-table entry is a structure containing
201826** a subset of the initial values that the modified row contained at the
201827** start of the session. Or no initial values if the row was inserted.
201828*/
201829struct SessionTable {
201830  SessionTable *pNext;
201831  char *zName;                    /* Local name of table */
201832  int nCol;                       /* Number of columns in table zName */
201833  int bStat1;                     /* True if this is sqlite_stat1 */
201834  const char **azCol;             /* Column names */
201835  u8 *abPK;                       /* Array of primary key flags */
201836  int nEntry;                     /* Total number of entries in hash table */
201837  int nChange;                    /* Size of apChange[] array */
201838  SessionChange **apChange;       /* Hash table buckets */
201839};
201840
201841/*
201842** RECORD FORMAT:
201843**
201844** The following record format is similar to (but not compatible with) that
201845** used in SQLite database files. This format is used as part of the
201846** change-set binary format, and so must be architecture independent.
201847**
201848** Unlike the SQLite database record format, each field is self-contained -
201849** there is no separation of header and data. Each field begins with a
201850** single byte describing its type, as follows:
201851**
201852**       0x00: Undefined value.
201853**       0x01: Integer value.
201854**       0x02: Real value.
201855**       0x03: Text value.
201856**       0x04: Blob value.
201857**       0x05: SQL NULL value.
201858**
201859** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT
201860** and so on in sqlite3.h. For undefined and NULL values, the field consists
201861** only of the single type byte. For other types of values, the type byte
201862** is followed by:
201863**
201864**   Text values:
201865**     A varint containing the number of bytes in the value (encoded using
201866**     UTF-8). Followed by a buffer containing the UTF-8 representation
201867**     of the text value. There is no nul terminator.
201868**
201869**   Blob values:
201870**     A varint containing the number of bytes in the value, followed by
201871**     a buffer containing the value itself.
201872**
201873**   Integer values:
201874**     An 8-byte big-endian integer value.
201875**
201876**   Real values:
201877**     An 8-byte big-endian IEEE 754-2008 real value.
201878**
201879** Varint values are encoded in the same way as varints in the SQLite
201880** record format.
201881**
201882** CHANGESET FORMAT:
201883**
201884** A changeset is a collection of DELETE, UPDATE and INSERT operations on
201885** one or more tables. Operations on a single table are grouped together,
201886** but may occur in any order (i.e. deletes, updates and inserts are all
201887** mixed together).
201888**
201889** Each group of changes begins with a table header:
201890**
201891**   1 byte: Constant 0x54 (capital 'T')
201892**   Varint: Number of columns in the table.
201893**   nCol bytes: 0x01 for PK columns, 0x00 otherwise.
201894**   N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
201895**
201896** Followed by one or more changes to the table.
201897**
201898**   1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
201899**   1 byte: The "indirect-change" flag.
201900**   old.* record: (delete and update only)
201901**   new.* record: (insert and update only)
201902**
201903** The "old.*" and "new.*" records, if present, are N field records in the
201904** format described above under "RECORD FORMAT", where N is the number of
201905** columns in the table. The i'th field of each record is associated with
201906** the i'th column of the table, counting from left to right in the order
201907** in which columns were declared in the CREATE TABLE statement.
201908**
201909** The new.* record that is part of each INSERT change contains the values
201910** that make up the new row. Similarly, the old.* record that is part of each
201911** DELETE change contains the values that made up the row that was deleted
201912** from the database. In the changeset format, the records that are part
201913** of INSERT or DELETE changes never contain any undefined (type byte 0x00)
201914** fields.
201915**
201916** Within the old.* record associated with an UPDATE change, all fields
201917** associated with table columns that are not PRIMARY KEY columns and are
201918** not modified by the UPDATE change are set to "undefined". Other fields
201919** are set to the values that made up the row before the UPDATE that the
201920** change records took place. Within the new.* record, fields associated
201921** with table columns modified by the UPDATE change contain the new
201922** values. Fields associated with table columns that are not modified
201923** are set to "undefined".
201924**
201925** PATCHSET FORMAT:
201926**
201927** A patchset is also a collection of changes. It is similar to a changeset,
201928** but leaves undefined those fields that are not useful if no conflict
201929** resolution is required when applying the changeset.
201930**
201931** Each group of changes begins with a table header:
201932**
201933**   1 byte: Constant 0x50 (capital 'P')
201934**   Varint: Number of columns in the table.
201935**   nCol bytes: 0x01 for PK columns, 0x00 otherwise.
201936**   N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
201937**
201938** Followed by one or more changes to the table.
201939**
201940**   1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
201941**   1 byte: The "indirect-change" flag.
201942**   single record: (PK fields for DELETE, PK and modified fields for UPDATE,
201943**                   full record for INSERT).
201944**
201945** As in the changeset format, each field of the single record that is part
201946** of a patchset change is associated with the correspondingly positioned
201947** table column, counting from left to right within the CREATE TABLE
201948** statement.
201949**
201950** For a DELETE change, all fields within the record except those associated
201951** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
201952** values identifying the row to delete.
201953**
201954** For an UPDATE change, all fields except those associated with PRIMARY KEY
201955** columns and columns that are modified by the UPDATE are set to "undefined".
201956** PRIMARY KEY fields contain the values identifying the table row to update,
201957** and fields associated with modified columns contain the new column values.
201958**
201959** The records associated with INSERT changes are in the same format as for
201960** changesets. It is not possible for a record associated with an INSERT
201961** change to contain a field set to "undefined".
201962**
201963** REBASE BLOB FORMAT:
201964**
201965** A rebase blob may be output by sqlite3changeset_apply_v2() and its
201966** streaming equivalent for use with the sqlite3_rebaser APIs to rebase
201967** existing changesets. A rebase blob contains one entry for each conflict
201968** resolved using either the OMIT or REPLACE strategies within the apply_v2()
201969** call.
201970**
201971** The format used for a rebase blob is very similar to that used for
201972** changesets. All entries related to a single table are grouped together.
201973**
201974** Each group of entries begins with a table header in changeset format:
201975**
201976**   1 byte: Constant 0x54 (capital 'T')
201977**   Varint: Number of columns in the table.
201978**   nCol bytes: 0x01 for PK columns, 0x00 otherwise.
201979**   N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
201980**
201981** Followed by one or more entries associated with the table.
201982**
201983**   1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09).
201984**   1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT.
201985**   record: (in the record format defined above).
201986**
201987** In a rebase blob, the first field is set to SQLITE_INSERT if the change
201988** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if
201989** it was a DELETE. The second field is set to 0x01 if the conflict
201990** resolution strategy was REPLACE, or 0x00 if it was OMIT.
201991**
201992** If the change that caused the conflict was a DELETE, then the single
201993** record is a copy of the old.* record from the original changeset. If it
201994** was an INSERT, then the single record is a copy of the new.* record. If
201995** the conflicting change was an UPDATE, then the single record is a copy
201996** of the new.* record with the PK fields filled in based on the original
201997** old.* record.
201998*/
201999
202000/*
202001** For each row modified during a session, there exists a single instance of
202002** this structure stored in a SessionTable.aChange[] hash table.
202003*/
202004struct SessionChange {
202005  int op;                         /* One of UPDATE, DELETE, INSERT */
202006  int bIndirect;                  /* True if this change is "indirect" */
202007  int nRecord;                    /* Number of bytes in buffer aRecord[] */
202008  u8 *aRecord;                    /* Buffer containing old.* record */
202009  SessionChange *pNext;           /* For hash-table collisions */
202010};
202011
202012/*
202013** Write a varint with value iVal into the buffer at aBuf. Return the
202014** number of bytes written.
202015*/
202016static int sessionVarintPut(u8 *aBuf, int iVal){
202017  return putVarint32(aBuf, iVal);
202018}
202019
202020/*
202021** Return the number of bytes required to store value iVal as a varint.
202022*/
202023static int sessionVarintLen(int iVal){
202024  return sqlite3VarintLen(iVal);
202025}
202026
202027/*
202028** Read a varint value from aBuf[] into *piVal. Return the number of
202029** bytes read.
202030*/
202031static int sessionVarintGet(u8 *aBuf, int *piVal){
202032  return getVarint32(aBuf, *piVal);
202033}
202034
202035/* Load an unaligned and unsigned 32-bit integer */
202036#define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
202037
202038/*
202039** Read a 64-bit big-endian integer value from buffer aRec[]. Return
202040** the value read.
202041*/
202042static sqlite3_int64 sessionGetI64(u8 *aRec){
202043  u64 x = SESSION_UINT32(aRec);
202044  u32 y = SESSION_UINT32(aRec+4);
202045  x = (x<<32) + y;
202046  return (sqlite3_int64)x;
202047}
202048
202049/*
202050** Write a 64-bit big-endian integer value to the buffer aBuf[].
202051*/
202052static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
202053  aBuf[0] = (i>>56) & 0xFF;
202054  aBuf[1] = (i>>48) & 0xFF;
202055  aBuf[2] = (i>>40) & 0xFF;
202056  aBuf[3] = (i>>32) & 0xFF;
202057  aBuf[4] = (i>>24) & 0xFF;
202058  aBuf[5] = (i>>16) & 0xFF;
202059  aBuf[6] = (i>> 8) & 0xFF;
202060  aBuf[7] = (i>> 0) & 0xFF;
202061}
202062
202063/*
202064** This function is used to serialize the contents of value pValue (see
202065** comment titled "RECORD FORMAT" above).
202066**
202067** If it is non-NULL, the serialized form of the value is written to
202068** buffer aBuf. *pnWrite is set to the number of bytes written before
202069** returning. Or, if aBuf is NULL, the only thing this function does is
202070** set *pnWrite.
202071**
202072** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
202073** within a call to sqlite3_value_text() (may fail if the db is utf-16))
202074** SQLITE_NOMEM is returned.
202075*/
202076static int sessionSerializeValue(
202077  u8 *aBuf,                       /* If non-NULL, write serialized value here */
202078  sqlite3_value *pValue,          /* Value to serialize */
202079  sqlite3_int64 *pnWrite          /* IN/OUT: Increment by bytes written */
202080){
202081  int nByte;                      /* Size of serialized value in bytes */
202082
202083  if( pValue ){
202084    int eType;                    /* Value type (SQLITE_NULL, TEXT etc.) */
202085
202086    eType = sqlite3_value_type(pValue);
202087    if( aBuf ) aBuf[0] = eType;
202088
202089    switch( eType ){
202090      case SQLITE_NULL:
202091        nByte = 1;
202092        break;
202093
202094      case SQLITE_INTEGER:
202095      case SQLITE_FLOAT:
202096        if( aBuf ){
202097          /* TODO: SQLite does something special to deal with mixed-endian
202098          ** floating point values (e.g. ARM7). This code probably should
202099          ** too.  */
202100          u64 i;
202101          if( eType==SQLITE_INTEGER ){
202102            i = (u64)sqlite3_value_int64(pValue);
202103          }else{
202104            double r;
202105            assert( sizeof(double)==8 && sizeof(u64)==8 );
202106            r = sqlite3_value_double(pValue);
202107            memcpy(&i, &r, 8);
202108          }
202109          sessionPutI64(&aBuf[1], i);
202110        }
202111        nByte = 9;
202112        break;
202113
202114      default: {
202115        u8 *z;
202116        int n;
202117        int nVarint;
202118
202119        assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
202120        if( eType==SQLITE_TEXT ){
202121          z = (u8 *)sqlite3_value_text(pValue);
202122        }else{
202123          z = (u8 *)sqlite3_value_blob(pValue);
202124        }
202125        n = sqlite3_value_bytes(pValue);
202126        if( z==0 && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
202127        nVarint = sessionVarintLen(n);
202128
202129        if( aBuf ){
202130          sessionVarintPut(&aBuf[1], n);
202131          if( n ) memcpy(&aBuf[nVarint + 1], z, n);
202132        }
202133
202134        nByte = 1 + nVarint + n;
202135        break;
202136      }
202137    }
202138  }else{
202139    nByte = 1;
202140    if( aBuf ) aBuf[0] = '\0';
202141  }
202142
202143  if( pnWrite ) *pnWrite += nByte;
202144  return SQLITE_OK;
202145}
202146
202147
202148/*
202149** This macro is used to calculate hash key values for data structures. In
202150** order to use this macro, the entire data structure must be represented
202151** as a series of unsigned integers. In order to calculate a hash-key value
202152** for a data structure represented as three such integers, the macro may
202153** then be used as follows:
202154**
202155**    int hash_key_value;
202156**    hash_key_value = HASH_APPEND(0, <value 1>);
202157**    hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
202158**    hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
202159**
202160** In practice, the data structures this macro is used for are the primary
202161** key values of modified rows.
202162*/
202163#define HASH_APPEND(hash, add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
202164
202165/*
202166** Append the hash of the 64-bit integer passed as the second argument to the
202167** hash-key value passed as the first. Return the new hash-key value.
202168*/
202169static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
202170  h = HASH_APPEND(h, i & 0xFFFFFFFF);
202171  return HASH_APPEND(h, (i>>32)&0xFFFFFFFF);
202172}
202173
202174/*
202175** Append the hash of the blob passed via the second and third arguments to
202176** the hash-key value passed as the first. Return the new hash-key value.
202177*/
202178static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
202179  int i;
202180  for(i=0; i<n; i++) h = HASH_APPEND(h, z[i]);
202181  return h;
202182}
202183
202184/*
202185** Append the hash of the data type passed as the second argument to the
202186** hash-key value passed as the first. Return the new hash-key value.
202187*/
202188static unsigned int sessionHashAppendType(unsigned int h, int eType){
202189  return HASH_APPEND(h, eType);
202190}
202191
202192/*
202193** This function may only be called from within a pre-update callback.
202194** It calculates a hash based on the primary key values of the old.* or
202195** new.* row currently available and, assuming no error occurs, writes it to
202196** *piHash before returning. If the primary key contains one or more NULL
202197** values, *pbNullPK is set to true before returning.
202198**
202199** If an error occurs, an SQLite error code is returned and the final values
202200** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
202201** and the output variables are set as described above.
202202*/
202203static int sessionPreupdateHash(
202204  sqlite3_session *pSession,      /* Session object that owns pTab */
202205  SessionTable *pTab,             /* Session table handle */
202206  int bNew,                       /* True to hash the new.* PK */
202207  int *piHash,                    /* OUT: Hash value */
202208  int *pbNullPK                   /* OUT: True if there are NULL values in PK */
202209){
202210  unsigned int h = 0;             /* Hash value to return */
202211  int i;                          /* Used to iterate through columns */
202212
202213  assert( *pbNullPK==0 );
202214  assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) );
202215  for(i=0; i<pTab->nCol; i++){
202216    if( pTab->abPK[i] ){
202217      int rc;
202218      int eType;
202219      sqlite3_value *pVal;
202220
202221      if( bNew ){
202222        rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
202223      }else{
202224        rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
202225      }
202226      if( rc!=SQLITE_OK ) return rc;
202227
202228      eType = sqlite3_value_type(pVal);
202229      h = sessionHashAppendType(h, eType);
202230      if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
202231        i64 iVal;
202232        if( eType==SQLITE_INTEGER ){
202233          iVal = sqlite3_value_int64(pVal);
202234        }else{
202235          double rVal = sqlite3_value_double(pVal);
202236          assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
202237          memcpy(&iVal, &rVal, 8);
202238        }
202239        h = sessionHashAppendI64(h, iVal);
202240      }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
202241        const u8 *z;
202242        int n;
202243        if( eType==SQLITE_TEXT ){
202244          z = (const u8 *)sqlite3_value_text(pVal);
202245        }else{
202246          z = (const u8 *)sqlite3_value_blob(pVal);
202247        }
202248        n = sqlite3_value_bytes(pVal);
202249        if( !z && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
202250        h = sessionHashAppendBlob(h, n, z);
202251      }else{
202252        assert( eType==SQLITE_NULL );
202253        assert( pTab->bStat1==0 || i!=1 );
202254        *pbNullPK = 1;
202255      }
202256    }
202257  }
202258
202259  *piHash = (h % pTab->nChange);
202260  return SQLITE_OK;
202261}
202262
202263/*
202264** The buffer that the argument points to contains a serialized SQL value.
202265** Return the number of bytes of space occupied by the value (including
202266** the type byte).
202267*/
202268static int sessionSerialLen(u8 *a){
202269  int e = *a;
202270  int n;
202271  if( e==0 || e==0xFF ) return 1;
202272  if( e==SQLITE_NULL ) return 1;
202273  if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
202274  return sessionVarintGet(&a[1], &n) + 1 + n;
202275}
202276
202277/*
202278** Based on the primary key values stored in change aRecord, calculate a
202279** hash key. Assume the has table has nBucket buckets. The hash keys
202280** calculated by this function are compatible with those calculated by
202281** sessionPreupdateHash().
202282**
202283** The bPkOnly argument is non-zero if the record at aRecord[] is from
202284** a patchset DELETE. In this case the non-PK fields are omitted entirely.
202285*/
202286static unsigned int sessionChangeHash(
202287  SessionTable *pTab,             /* Table handle */
202288  int bPkOnly,                    /* Record consists of PK fields only */
202289  u8 *aRecord,                    /* Change record */
202290  int nBucket                     /* Assume this many buckets in hash table */
202291){
202292  unsigned int h = 0;             /* Value to return */
202293  int i;                          /* Used to iterate through columns */
202294  u8 *a = aRecord;                /* Used to iterate through change record */
202295
202296  for(i=0; i<pTab->nCol; i++){
202297    int eType = *a;
202298    int isPK = pTab->abPK[i];
202299    if( bPkOnly && isPK==0 ) continue;
202300
202301    /* It is not possible for eType to be SQLITE_NULL here. The session
202302    ** module does not record changes for rows with NULL values stored in
202303    ** primary key columns. */
202304    assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
202305         || eType==SQLITE_TEXT || eType==SQLITE_BLOB
202306         || eType==SQLITE_NULL || eType==0
202307    );
202308    assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) );
202309
202310    if( isPK ){
202311      a++;
202312      h = sessionHashAppendType(h, eType);
202313      if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
202314        h = sessionHashAppendI64(h, sessionGetI64(a));
202315        a += 8;
202316      }else{
202317        int n;
202318        a += sessionVarintGet(a, &n);
202319        h = sessionHashAppendBlob(h, n, a);
202320        a += n;
202321      }
202322    }else{
202323      a += sessionSerialLen(a);
202324    }
202325  }
202326  return (h % nBucket);
202327}
202328
202329/*
202330** Arguments aLeft and aRight are pointers to change records for table pTab.
202331** This function returns true if the two records apply to the same row (i.e.
202332** have the same values stored in the primary key columns), or false
202333** otherwise.
202334*/
202335static int sessionChangeEqual(
202336  SessionTable *pTab,             /* Table used for PK definition */
202337  int bLeftPkOnly,                /* True if aLeft[] contains PK fields only */
202338  u8 *aLeft,                      /* Change record */
202339  int bRightPkOnly,               /* True if aRight[] contains PK fields only */
202340  u8 *aRight                      /* Change record */
202341){
202342  u8 *a1 = aLeft;                 /* Cursor to iterate through aLeft */
202343  u8 *a2 = aRight;                /* Cursor to iterate through aRight */
202344  int iCol;                       /* Used to iterate through table columns */
202345
202346  for(iCol=0; iCol<pTab->nCol; iCol++){
202347    if( pTab->abPK[iCol] ){
202348      int n1 = sessionSerialLen(a1);
202349      int n2 = sessionSerialLen(a2);
202350
202351      if( n1!=n2 || memcmp(a1, a2, n1) ){
202352        return 0;
202353      }
202354      a1 += n1;
202355      a2 += n2;
202356    }else{
202357      if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
202358      if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
202359    }
202360  }
202361
202362  return 1;
202363}
202364
202365/*
202366** Arguments aLeft and aRight both point to buffers containing change
202367** records with nCol columns. This function "merges" the two records into
202368** a single records which is written to the buffer at *paOut. *paOut is
202369** then set to point to one byte after the last byte written before
202370** returning.
202371**
202372** The merging of records is done as follows: For each column, if the
202373** aRight record contains a value for the column, copy the value from
202374** their. Otherwise, if aLeft contains a value, copy it. If neither
202375** record contains a value for a given column, then neither does the
202376** output record.
202377*/
202378static void sessionMergeRecord(
202379  u8 **paOut,
202380  int nCol,
202381  u8 *aLeft,
202382  u8 *aRight
202383){
202384  u8 *a1 = aLeft;                 /* Cursor used to iterate through aLeft */
202385  u8 *a2 = aRight;                /* Cursor used to iterate through aRight */
202386  u8 *aOut = *paOut;              /* Output cursor */
202387  int iCol;                       /* Used to iterate from 0 to nCol */
202388
202389  for(iCol=0; iCol<nCol; iCol++){
202390    int n1 = sessionSerialLen(a1);
202391    int n2 = sessionSerialLen(a2);
202392    if( *a2 ){
202393      memcpy(aOut, a2, n2);
202394      aOut += n2;
202395    }else{
202396      memcpy(aOut, a1, n1);
202397      aOut += n1;
202398    }
202399    a1 += n1;
202400    a2 += n2;
202401  }
202402
202403  *paOut = aOut;
202404}
202405
202406/*
202407** This is a helper function used by sessionMergeUpdate().
202408**
202409** When this function is called, both *paOne and *paTwo point to a value
202410** within a change record. Before it returns, both have been advanced so
202411** as to point to the next value in the record.
202412**
202413** If, when this function is called, *paTwo points to a valid value (i.e.
202414** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
202415** pointer is returned and *pnVal is set to the number of bytes in the
202416** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
202417** set to the number of bytes in the value at *paOne. If *paOne points
202418** to the "no value" placeholder, *pnVal is set to 1. In other words:
202419**
202420**   if( *paTwo is valid ) return *paTwo;
202421**   return *paOne;
202422**
202423*/
202424static u8 *sessionMergeValue(
202425  u8 **paOne,                     /* IN/OUT: Left-hand buffer pointer */
202426  u8 **paTwo,                     /* IN/OUT: Right-hand buffer pointer */
202427  int *pnVal                      /* OUT: Bytes in returned value */
202428){
202429  u8 *a1 = *paOne;
202430  u8 *a2 = *paTwo;
202431  u8 *pRet = 0;
202432  int n1;
202433
202434  assert( a1 );
202435  if( a2 ){
202436    int n2 = sessionSerialLen(a2);
202437    if( *a2 ){
202438      *pnVal = n2;
202439      pRet = a2;
202440    }
202441    *paTwo = &a2[n2];
202442  }
202443
202444  n1 = sessionSerialLen(a1);
202445  if( pRet==0 ){
202446    *pnVal = n1;
202447    pRet = a1;
202448  }
202449  *paOne = &a1[n1];
202450
202451  return pRet;
202452}
202453
202454/*
202455** This function is used by changeset_concat() to merge two UPDATE changes
202456** on the same row.
202457*/
202458static int sessionMergeUpdate(
202459  u8 **paOut,                     /* IN/OUT: Pointer to output buffer */
202460  SessionTable *pTab,             /* Table change pertains to */
202461  int bPatchset,                  /* True if records are patchset records */
202462  u8 *aOldRecord1,                /* old.* record for first change */
202463  u8 *aOldRecord2,                /* old.* record for second change */
202464  u8 *aNewRecord1,                /* new.* record for first change */
202465  u8 *aNewRecord2                 /* new.* record for second change */
202466){
202467  u8 *aOld1 = aOldRecord1;
202468  u8 *aOld2 = aOldRecord2;
202469  u8 *aNew1 = aNewRecord1;
202470  u8 *aNew2 = aNewRecord2;
202471
202472  u8 *aOut = *paOut;
202473  int i;
202474
202475  if( bPatchset==0 ){
202476    int bRequired = 0;
202477
202478    assert( aOldRecord1 && aNewRecord1 );
202479
202480    /* Write the old.* vector first. */
202481    for(i=0; i<pTab->nCol; i++){
202482      int nOld;
202483      u8 *aOld;
202484      int nNew;
202485      u8 *aNew;
202486
202487      aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
202488      aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
202489      if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
202490        if( pTab->abPK[i]==0 ) bRequired = 1;
202491        memcpy(aOut, aOld, nOld);
202492        aOut += nOld;
202493      }else{
202494        *(aOut++) = '\0';
202495      }
202496    }
202497
202498    if( !bRequired ) return 0;
202499  }
202500
202501  /* Write the new.* vector */
202502  aOld1 = aOldRecord1;
202503  aOld2 = aOldRecord2;
202504  aNew1 = aNewRecord1;
202505  aNew2 = aNewRecord2;
202506  for(i=0; i<pTab->nCol; i++){
202507    int nOld;
202508    u8 *aOld;
202509    int nNew;
202510    u8 *aNew;
202511
202512    aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
202513    aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
202514    if( bPatchset==0
202515     && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
202516    ){
202517      *(aOut++) = '\0';
202518    }else{
202519      memcpy(aOut, aNew, nNew);
202520      aOut += nNew;
202521    }
202522  }
202523
202524  *paOut = aOut;
202525  return 1;
202526}
202527
202528/*
202529** This function is only called from within a pre-update-hook callback.
202530** It determines if the current pre-update-hook change affects the same row
202531** as the change stored in argument pChange. If so, it returns true. Otherwise
202532** if the pre-update-hook does not affect the same row as pChange, it returns
202533** false.
202534*/
202535static int sessionPreupdateEqual(
202536  sqlite3_session *pSession,      /* Session object that owns SessionTable */
202537  SessionTable *pTab,             /* Table associated with change */
202538  SessionChange *pChange,         /* Change to compare to */
202539  int op                          /* Current pre-update operation */
202540){
202541  int iCol;                       /* Used to iterate through columns */
202542  u8 *a = pChange->aRecord;       /* Cursor used to scan change record */
202543
202544  assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
202545  for(iCol=0; iCol<pTab->nCol; iCol++){
202546    if( !pTab->abPK[iCol] ){
202547      a += sessionSerialLen(a);
202548    }else{
202549      sqlite3_value *pVal;        /* Value returned by preupdate_new/old */
202550      int rc;                     /* Error code from preupdate_new/old */
202551      int eType = *a++;           /* Type of value from change record */
202552
202553      /* The following calls to preupdate_new() and preupdate_old() can not
202554      ** fail. This is because they cache their return values, and by the
202555      ** time control flows to here they have already been called once from
202556      ** within sessionPreupdateHash(). The first two asserts below verify
202557      ** this (that the method has already been called). */
202558      if( op==SQLITE_INSERT ){
202559        /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
202560        rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
202561      }else{
202562        /* assert( db->pPreUpdate->pUnpacked ); */
202563        rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
202564      }
202565      assert( rc==SQLITE_OK );
202566      if( sqlite3_value_type(pVal)!=eType ) return 0;
202567
202568      /* A SessionChange object never has a NULL value in a PK column */
202569      assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
202570           || eType==SQLITE_BLOB    || eType==SQLITE_TEXT
202571      );
202572
202573      if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
202574        i64 iVal = sessionGetI64(a);
202575        a += 8;
202576        if( eType==SQLITE_INTEGER ){
202577          if( sqlite3_value_int64(pVal)!=iVal ) return 0;
202578        }else{
202579          double rVal;
202580          assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
202581          memcpy(&rVal, &iVal, 8);
202582          if( sqlite3_value_double(pVal)!=rVal ) return 0;
202583        }
202584      }else{
202585        int n;
202586        const u8 *z;
202587        a += sessionVarintGet(a, &n);
202588        if( sqlite3_value_bytes(pVal)!=n ) return 0;
202589        if( eType==SQLITE_TEXT ){
202590          z = sqlite3_value_text(pVal);
202591        }else{
202592          z = sqlite3_value_blob(pVal);
202593        }
202594        if( n>0 && memcmp(a, z, n) ) return 0;
202595        a += n;
202596      }
202597    }
202598  }
202599
202600  return 1;
202601}
202602
202603/*
202604** If required, grow the hash table used to store changes on table pTab
202605** (part of the session pSession). If a fatal OOM error occurs, set the
202606** session object to failed and return SQLITE_ERROR. Otherwise, return
202607** SQLITE_OK.
202608**
202609** It is possible that a non-fatal OOM error occurs in this function. In
202610** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
202611** Growing the hash table in this case is a performance optimization only,
202612** it is not required for correct operation.
202613*/
202614static int sessionGrowHash(int bPatchset, SessionTable *pTab){
202615  if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
202616    int i;
202617    SessionChange **apNew;
202618    sqlite3_int64 nNew = 2*(sqlite3_int64)(pTab->nChange ? pTab->nChange : 128);
202619
202620    apNew = (SessionChange **)sqlite3_malloc64(sizeof(SessionChange *) * nNew);
202621    if( apNew==0 ){
202622      if( pTab->nChange==0 ){
202623        return SQLITE_ERROR;
202624      }
202625      return SQLITE_OK;
202626    }
202627    memset(apNew, 0, sizeof(SessionChange *) * nNew);
202628
202629    for(i=0; i<pTab->nChange; i++){
202630      SessionChange *p;
202631      SessionChange *pNext;
202632      for(p=pTab->apChange[i]; p; p=pNext){
202633        int bPkOnly = (p->op==SQLITE_DELETE && bPatchset);
202634        int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
202635        pNext = p->pNext;
202636        p->pNext = apNew[iHash];
202637        apNew[iHash] = p;
202638      }
202639    }
202640
202641    sqlite3_free(pTab->apChange);
202642    pTab->nChange = nNew;
202643    pTab->apChange = apNew;
202644  }
202645
202646  return SQLITE_OK;
202647}
202648
202649/*
202650** This function queries the database for the names of the columns of table
202651** zThis, in schema zDb.
202652**
202653** Otherwise, if they are not NULL, variable *pnCol is set to the number
202654** of columns in the database table and variable *pzTab is set to point to a
202655** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
202656** point to an array of pointers to column names. And *pabPK (again, if not
202657** NULL) is set to point to an array of booleans - true if the corresponding
202658** column is part of the primary key.
202659**
202660** For example, if the table is declared as:
202661**
202662**     CREATE TABLE tbl1(w, x, y, z, PRIMARY KEY(w, z));
202663**
202664** Then the four output variables are populated as follows:
202665**
202666**     *pnCol  = 4
202667**     *pzTab  = "tbl1"
202668**     *pazCol = {"w", "x", "y", "z"}
202669**     *pabPK  = {1, 0, 0, 1}
202670**
202671** All returned buffers are part of the same single allocation, which must
202672** be freed using sqlite3_free() by the caller
202673*/
202674static int sessionTableInfo(
202675  sqlite3 *db,                    /* Database connection */
202676  const char *zDb,                /* Name of attached database (e.g. "main") */
202677  const char *zThis,              /* Table name */
202678  int *pnCol,                     /* OUT: number of columns */
202679  const char **pzTab,             /* OUT: Copy of zThis */
202680  const char ***pazCol,           /* OUT: Array of column names for table */
202681  u8 **pabPK                      /* OUT: Array of booleans - true for PK col */
202682){
202683  char *zPragma;
202684  sqlite3_stmt *pStmt;
202685  int rc;
202686  sqlite3_int64 nByte;
202687  int nDbCol = 0;
202688  int nThis;
202689  int i;
202690  u8 *pAlloc = 0;
202691  char **azCol = 0;
202692  u8 *abPK = 0;
202693
202694  assert( pazCol && pabPK );
202695
202696  nThis = sqlite3Strlen30(zThis);
202697  if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){
202698    rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0);
202699    if( rc==SQLITE_OK ){
202700      /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */
202701      zPragma = sqlite3_mprintf(
202702          "SELECT 0, 'tbl',  '', 0, '', 1     UNION ALL "
202703          "SELECT 1, 'idx',  '', 0, '', 2     UNION ALL "
202704          "SELECT 2, 'stat', '', 0, '', 0"
202705      );
202706    }else if( rc==SQLITE_ERROR ){
202707      zPragma = sqlite3_mprintf("");
202708    }else{
202709      return rc;
202710    }
202711  }else{
202712    zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
202713  }
202714  if( !zPragma ) return SQLITE_NOMEM;
202715
202716  rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
202717  sqlite3_free(zPragma);
202718  if( rc!=SQLITE_OK ) return rc;
202719
202720  nByte = nThis + 1;
202721  while( SQLITE_ROW==sqlite3_step(pStmt) ){
202722    nByte += sqlite3_column_bytes(pStmt, 1);
202723    nDbCol++;
202724  }
202725  rc = sqlite3_reset(pStmt);
202726
202727  if( rc==SQLITE_OK ){
202728    nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
202729    pAlloc = sqlite3_malloc64(nByte);
202730    if( pAlloc==0 ){
202731      rc = SQLITE_NOMEM;
202732    }
202733  }
202734  if( rc==SQLITE_OK ){
202735    azCol = (char **)pAlloc;
202736    pAlloc = (u8 *)&azCol[nDbCol];
202737    abPK = (u8 *)pAlloc;
202738    pAlloc = &abPK[nDbCol];
202739    if( pzTab ){
202740      memcpy(pAlloc, zThis, nThis+1);
202741      *pzTab = (char *)pAlloc;
202742      pAlloc += nThis+1;
202743    }
202744
202745    i = 0;
202746    while( SQLITE_ROW==sqlite3_step(pStmt) ){
202747      int nName = sqlite3_column_bytes(pStmt, 1);
202748      const unsigned char *zName = sqlite3_column_text(pStmt, 1);
202749      if( zName==0 ) break;
202750      memcpy(pAlloc, zName, nName+1);
202751      azCol[i] = (char *)pAlloc;
202752      pAlloc += nName+1;
202753      abPK[i] = sqlite3_column_int(pStmt, 5);
202754      i++;
202755    }
202756    rc = sqlite3_reset(pStmt);
202757
202758  }
202759
202760  /* If successful, populate the output variables. Otherwise, zero them and
202761  ** free any allocation made. An error code will be returned in this case.
202762  */
202763  if( rc==SQLITE_OK ){
202764    *pazCol = (const char **)azCol;
202765    *pabPK = abPK;
202766    *pnCol = nDbCol;
202767  }else{
202768    *pazCol = 0;
202769    *pabPK = 0;
202770    *pnCol = 0;
202771    if( pzTab ) *pzTab = 0;
202772    sqlite3_free(azCol);
202773  }
202774  sqlite3_finalize(pStmt);
202775  return rc;
202776}
202777
202778/*
202779** This function is only called from within a pre-update handler for a
202780** write to table pTab, part of session pSession. If this is the first
202781** write to this table, initalize the SessionTable.nCol, azCol[] and
202782** abPK[] arrays accordingly.
202783**
202784** If an error occurs, an error code is stored in sqlite3_session.rc and
202785** non-zero returned. Or, if no error occurs but the table has no primary
202786** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
202787** indicate that updates on this table should be ignored. SessionTable.abPK
202788** is set to NULL in this case.
202789*/
202790static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
202791  if( pTab->nCol==0 ){
202792    u8 *abPK;
202793    assert( pTab->azCol==0 || pTab->abPK==0 );
202794    pSession->rc = sessionTableInfo(pSession->db, pSession->zDb,
202795        pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
202796    );
202797    if( pSession->rc==SQLITE_OK ){
202798      int i;
202799      for(i=0; i<pTab->nCol; i++){
202800        if( abPK[i] ){
202801          pTab->abPK = abPK;
202802          break;
202803        }
202804      }
202805      if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){
202806        pTab->bStat1 = 1;
202807      }
202808    }
202809  }
202810  return (pSession->rc || pTab->abPK==0);
202811}
202812
202813/*
202814** Versions of the four methods in object SessionHook for use with the
202815** sqlite_stat1 table. The purpose of this is to substitute a zero-length
202816** blob each time a NULL value is read from the "idx" column of the
202817** sqlite_stat1 table.
202818*/
202819typedef struct SessionStat1Ctx SessionStat1Ctx;
202820struct SessionStat1Ctx {
202821  SessionHook hook;
202822  sqlite3_session *pSession;
202823};
202824static int sessionStat1Old(void *pCtx, int iCol, sqlite3_value **ppVal){
202825  SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
202826  sqlite3_value *pVal = 0;
202827  int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal);
202828  if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
202829    pVal = p->pSession->pZeroBlob;
202830  }
202831  *ppVal = pVal;
202832  return rc;
202833}
202834static int sessionStat1New(void *pCtx, int iCol, sqlite3_value **ppVal){
202835  SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
202836  sqlite3_value *pVal = 0;
202837  int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal);
202838  if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
202839    pVal = p->pSession->pZeroBlob;
202840  }
202841  *ppVal = pVal;
202842  return rc;
202843}
202844static int sessionStat1Count(void *pCtx){
202845  SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
202846  return p->hook.xCount(p->hook.pCtx);
202847}
202848static int sessionStat1Depth(void *pCtx){
202849  SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
202850  return p->hook.xDepth(p->hook.pCtx);
202851}
202852
202853
202854/*
202855** This function is only called from with a pre-update-hook reporting a
202856** change on table pTab (attached to session pSession). The type of change
202857** (UPDATE, INSERT, DELETE) is specified by the first argument.
202858**
202859** Unless one is already present or an error occurs, an entry is added
202860** to the changed-rows hash table associated with table pTab.
202861*/
202862static void sessionPreupdateOneChange(
202863  int op,                         /* One of SQLITE_UPDATE, INSERT, DELETE */
202864  sqlite3_session *pSession,      /* Session object pTab is attached to */
202865  SessionTable *pTab              /* Table that change applies to */
202866){
202867  int iHash;
202868  int bNull = 0;
202869  int rc = SQLITE_OK;
202870  SessionStat1Ctx stat1 = {{0,0,0,0,0},0};
202871
202872  if( pSession->rc ) return;
202873
202874  /* Load table details if required */
202875  if( sessionInitTable(pSession, pTab) ) return;
202876
202877  /* Check the number of columns in this xPreUpdate call matches the
202878  ** number of columns in the table.  */
202879  if( pTab->nCol!=pSession->hook.xCount(pSession->hook.pCtx) ){
202880    pSession->rc = SQLITE_SCHEMA;
202881    return;
202882  }
202883
202884  /* Grow the hash table if required */
202885  if( sessionGrowHash(0, pTab) ){
202886    pSession->rc = SQLITE_NOMEM;
202887    return;
202888  }
202889
202890  if( pTab->bStat1 ){
202891    stat1.hook = pSession->hook;
202892    stat1.pSession = pSession;
202893    pSession->hook.pCtx = (void*)&stat1;
202894    pSession->hook.xNew = sessionStat1New;
202895    pSession->hook.xOld = sessionStat1Old;
202896    pSession->hook.xCount = sessionStat1Count;
202897    pSession->hook.xDepth = sessionStat1Depth;
202898    if( pSession->pZeroBlob==0 ){
202899      sqlite3_value *p = sqlite3ValueNew(0);
202900      if( p==0 ){
202901        rc = SQLITE_NOMEM;
202902        goto error_out;
202903      }
202904      sqlite3ValueSetStr(p, 0, "", 0, SQLITE_STATIC);
202905      pSession->pZeroBlob = p;
202906    }
202907  }
202908
202909  /* Calculate the hash-key for this change. If the primary key of the row
202910  ** includes a NULL value, exit early. Such changes are ignored by the
202911  ** session module. */
202912  rc = sessionPreupdateHash(pSession, pTab, op==SQLITE_INSERT, &iHash, &bNull);
202913  if( rc!=SQLITE_OK ) goto error_out;
202914
202915  if( bNull==0 ){
202916    /* Search the hash table for an existing record for this row. */
202917    SessionChange *pC;
202918    for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
202919      if( sessionPreupdateEqual(pSession, pTab, pC, op) ) break;
202920    }
202921
202922    if( pC==0 ){
202923      /* Create a new change object containing all the old values (if
202924      ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
202925      ** values (if this is an INSERT). */
202926      SessionChange *pChange; /* New change object */
202927      sqlite3_int64 nByte;    /* Number of bytes to allocate */
202928      int i;                  /* Used to iterate through columns */
202929
202930      assert( rc==SQLITE_OK );
202931      pTab->nEntry++;
202932
202933      /* Figure out how large an allocation is required */
202934      nByte = sizeof(SessionChange);
202935      for(i=0; i<pTab->nCol; i++){
202936        sqlite3_value *p = 0;
202937        if( op!=SQLITE_INSERT ){
202938          TESTONLY(int trc = ) pSession->hook.xOld(pSession->hook.pCtx, i, &p);
202939          assert( trc==SQLITE_OK );
202940        }else if( pTab->abPK[i] ){
202941          TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
202942          assert( trc==SQLITE_OK );
202943        }
202944
202945        /* This may fail if SQLite value p contains a utf-16 string that must
202946        ** be converted to utf-8 and an OOM error occurs while doing so. */
202947        rc = sessionSerializeValue(0, p, &nByte);
202948        if( rc!=SQLITE_OK ) goto error_out;
202949      }
202950
202951      /* Allocate the change object */
202952      pChange = (SessionChange *)sqlite3_malloc64(nByte);
202953      if( !pChange ){
202954        rc = SQLITE_NOMEM;
202955        goto error_out;
202956      }else{
202957        memset(pChange, 0, sizeof(SessionChange));
202958        pChange->aRecord = (u8 *)&pChange[1];
202959      }
202960
202961      /* Populate the change object. None of the preupdate_old(),
202962      ** preupdate_new() or SerializeValue() calls below may fail as all
202963      ** required values and encodings have already been cached in memory.
202964      ** It is not possible for an OOM to occur in this block. */
202965      nByte = 0;
202966      for(i=0; i<pTab->nCol; i++){
202967        sqlite3_value *p = 0;
202968        if( op!=SQLITE_INSERT ){
202969          pSession->hook.xOld(pSession->hook.pCtx, i, &p);
202970        }else if( pTab->abPK[i] ){
202971          pSession->hook.xNew(pSession->hook.pCtx, i, &p);
202972        }
202973        sessionSerializeValue(&pChange->aRecord[nByte], p, &nByte);
202974      }
202975
202976      /* Add the change to the hash-table */
202977      if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
202978        pChange->bIndirect = 1;
202979      }
202980      pChange->nRecord = nByte;
202981      pChange->op = op;
202982      pChange->pNext = pTab->apChange[iHash];
202983      pTab->apChange[iHash] = pChange;
202984
202985    }else if( pC->bIndirect ){
202986      /* If the existing change is considered "indirect", but this current
202987      ** change is "direct", mark the change object as direct. */
202988      if( pSession->hook.xDepth(pSession->hook.pCtx)==0
202989       && pSession->bIndirect==0
202990      ){
202991        pC->bIndirect = 0;
202992      }
202993    }
202994  }
202995
202996  /* If an error has occurred, mark the session object as failed. */
202997 error_out:
202998  if( pTab->bStat1 ){
202999    pSession->hook = stat1.hook;
203000  }
203001  if( rc!=SQLITE_OK ){
203002    pSession->rc = rc;
203003  }
203004}
203005
203006static int sessionFindTable(
203007  sqlite3_session *pSession,
203008  const char *zName,
203009  SessionTable **ppTab
203010){
203011  int rc = SQLITE_OK;
203012  int nName = sqlite3Strlen30(zName);
203013  SessionTable *pRet;
203014
203015  /* Search for an existing table */
203016  for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
203017    if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
203018  }
203019
203020  if( pRet==0 && pSession->bAutoAttach ){
203021    /* If there is a table-filter configured, invoke it. If it returns 0,
203022    ** do not automatically add the new table. */
203023    if( pSession->xTableFilter==0
203024     || pSession->xTableFilter(pSession->pFilterCtx, zName)
203025    ){
203026      rc = sqlite3session_attach(pSession, zName);
203027      if( rc==SQLITE_OK ){
203028        for(pRet=pSession->pTable; pRet->pNext; pRet=pRet->pNext);
203029        assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) );
203030      }
203031    }
203032  }
203033
203034  assert( rc==SQLITE_OK || pRet==0 );
203035  *ppTab = pRet;
203036  return rc;
203037}
203038
203039/*
203040** The 'pre-update' hook registered by this module with SQLite databases.
203041*/
203042static void xPreUpdate(
203043  void *pCtx,                     /* Copy of third arg to preupdate_hook() */
203044  sqlite3 *db,                    /* Database handle */
203045  int op,                         /* SQLITE_UPDATE, DELETE or INSERT */
203046  char const *zDb,                /* Database name */
203047  char const *zName,              /* Table name */
203048  sqlite3_int64 iKey1,            /* Rowid of row about to be deleted/updated */
203049  sqlite3_int64 iKey2             /* New rowid value (for a rowid UPDATE) */
203050){
203051  sqlite3_session *pSession;
203052  int nDb = sqlite3Strlen30(zDb);
203053
203054  assert( sqlite3_mutex_held(db->mutex) );
203055
203056  for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
203057    SessionTable *pTab;
203058
203059    /* If this session is attached to a different database ("main", "temp"
203060    ** etc.), or if it is not currently enabled, there is nothing to do. Skip
203061    ** to the next session object attached to this database. */
203062    if( pSession->bEnable==0 ) continue;
203063    if( pSession->rc ) continue;
203064    if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
203065
203066    pSession->rc = sessionFindTable(pSession, zName, &pTab);
203067    if( pTab ){
203068      assert( pSession->rc==SQLITE_OK );
203069      sessionPreupdateOneChange(op, pSession, pTab);
203070      if( op==SQLITE_UPDATE ){
203071        sessionPreupdateOneChange(SQLITE_INSERT, pSession, pTab);
203072      }
203073    }
203074  }
203075}
203076
203077/*
203078** The pre-update hook implementations.
203079*/
203080static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
203081  return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
203082}
203083static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
203084  return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
203085}
203086static int sessionPreupdateCount(void *pCtx){
203087  return sqlite3_preupdate_count((sqlite3*)pCtx);
203088}
203089static int sessionPreupdateDepth(void *pCtx){
203090  return sqlite3_preupdate_depth((sqlite3*)pCtx);
203091}
203092
203093/*
203094** Install the pre-update hooks on the session object passed as the only
203095** argument.
203096*/
203097static void sessionPreupdateHooks(
203098  sqlite3_session *pSession
203099){
203100  pSession->hook.pCtx = (void*)pSession->db;
203101  pSession->hook.xOld = sessionPreupdateOld;
203102  pSession->hook.xNew = sessionPreupdateNew;
203103  pSession->hook.xCount = sessionPreupdateCount;
203104  pSession->hook.xDepth = sessionPreupdateDepth;
203105}
203106
203107typedef struct SessionDiffCtx SessionDiffCtx;
203108struct SessionDiffCtx {
203109  sqlite3_stmt *pStmt;
203110  int nOldOff;
203111};
203112
203113/*
203114** The diff hook implementations.
203115*/
203116static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
203117  SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
203118  *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff);
203119  return SQLITE_OK;
203120}
203121static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
203122  SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
203123  *ppVal = sqlite3_column_value(p->pStmt, iVal);
203124   return SQLITE_OK;
203125}
203126static int sessionDiffCount(void *pCtx){
203127  SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
203128  return p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt);
203129}
203130static int sessionDiffDepth(void *pCtx){
203131  return 0;
203132}
203133
203134/*
203135** Install the diff hooks on the session object passed as the only
203136** argument.
203137*/
203138static void sessionDiffHooks(
203139  sqlite3_session *pSession,
203140  SessionDiffCtx *pDiffCtx
203141){
203142  pSession->hook.pCtx = (void*)pDiffCtx;
203143  pSession->hook.xOld = sessionDiffOld;
203144  pSession->hook.xNew = sessionDiffNew;
203145  pSession->hook.xCount = sessionDiffCount;
203146  pSession->hook.xDepth = sessionDiffDepth;
203147}
203148
203149static char *sessionExprComparePK(
203150  int nCol,
203151  const char *zDb1, const char *zDb2,
203152  const char *zTab,
203153  const char **azCol, u8 *abPK
203154){
203155  int i;
203156  const char *zSep = "";
203157  char *zRet = 0;
203158
203159  for(i=0; i<nCol; i++){
203160    if( abPK[i] ){
203161      zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
203162          zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
203163      );
203164      zSep = " AND ";
203165      if( zRet==0 ) break;
203166    }
203167  }
203168
203169  return zRet;
203170}
203171
203172static char *sessionExprCompareOther(
203173  int nCol,
203174  const char *zDb1, const char *zDb2,
203175  const char *zTab,
203176  const char **azCol, u8 *abPK
203177){
203178  int i;
203179  const char *zSep = "";
203180  char *zRet = 0;
203181  int bHave = 0;
203182
203183  for(i=0; i<nCol; i++){
203184    if( abPK[i]==0 ){
203185      bHave = 1;
203186      zRet = sqlite3_mprintf(
203187          "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
203188          zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
203189      );
203190      zSep = " OR ";
203191      if( zRet==0 ) break;
203192    }
203193  }
203194
203195  if( bHave==0 ){
203196    assert( zRet==0 );
203197    zRet = sqlite3_mprintf("0");
203198  }
203199
203200  return zRet;
203201}
203202
203203static char *sessionSelectFindNew(
203204  int nCol,
203205  const char *zDb1,      /* Pick rows in this db only */
203206  const char *zDb2,      /* But not in this one */
203207  const char *zTbl,      /* Table name */
203208  const char *zExpr
203209){
203210  char *zRet = sqlite3_mprintf(
203211      "SELECT * FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
203212      "  SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
203213      ")",
203214      zDb1, zTbl, zDb2, zTbl, zExpr
203215  );
203216  return zRet;
203217}
203218
203219static int sessionDiffFindNew(
203220  int op,
203221  sqlite3_session *pSession,
203222  SessionTable *pTab,
203223  const char *zDb1,
203224  const char *zDb2,
203225  char *zExpr
203226){
203227  int rc = SQLITE_OK;
203228  char *zStmt = sessionSelectFindNew(pTab->nCol, zDb1, zDb2, pTab->zName,zExpr);
203229
203230  if( zStmt==0 ){
203231    rc = SQLITE_NOMEM;
203232  }else{
203233    sqlite3_stmt *pStmt;
203234    rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
203235    if( rc==SQLITE_OK ){
203236      SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
203237      pDiffCtx->pStmt = pStmt;
203238      pDiffCtx->nOldOff = 0;
203239      while( SQLITE_ROW==sqlite3_step(pStmt) ){
203240        sessionPreupdateOneChange(op, pSession, pTab);
203241      }
203242      rc = sqlite3_finalize(pStmt);
203243    }
203244    sqlite3_free(zStmt);
203245  }
203246
203247  return rc;
203248}
203249
203250static int sessionDiffFindModified(
203251  sqlite3_session *pSession,
203252  SessionTable *pTab,
203253  const char *zFrom,
203254  const char *zExpr
203255){
203256  int rc = SQLITE_OK;
203257
203258  char *zExpr2 = sessionExprCompareOther(pTab->nCol,
203259      pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
203260  );
203261  if( zExpr2==0 ){
203262    rc = SQLITE_NOMEM;
203263  }else{
203264    char *zStmt = sqlite3_mprintf(
203265        "SELECT * FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
203266        pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
203267    );
203268    if( zStmt==0 ){
203269      rc = SQLITE_NOMEM;
203270    }else{
203271      sqlite3_stmt *pStmt;
203272      rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
203273
203274      if( rc==SQLITE_OK ){
203275        SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
203276        pDiffCtx->pStmt = pStmt;
203277        pDiffCtx->nOldOff = pTab->nCol;
203278        while( SQLITE_ROW==sqlite3_step(pStmt) ){
203279          sessionPreupdateOneChange(SQLITE_UPDATE, pSession, pTab);
203280        }
203281        rc = sqlite3_finalize(pStmt);
203282      }
203283      sqlite3_free(zStmt);
203284    }
203285  }
203286
203287  return rc;
203288}
203289
203290SQLITE_API int sqlite3session_diff(
203291  sqlite3_session *pSession,
203292  const char *zFrom,
203293  const char *zTbl,
203294  char **pzErrMsg
203295){
203296  const char *zDb = pSession->zDb;
203297  int rc = pSession->rc;
203298  SessionDiffCtx d;
203299
203300  memset(&d, 0, sizeof(d));
203301  sessionDiffHooks(pSession, &d);
203302
203303  sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
203304  if( pzErrMsg ) *pzErrMsg = 0;
203305  if( rc==SQLITE_OK ){
203306    char *zExpr = 0;
203307    sqlite3 *db = pSession->db;
203308    SessionTable *pTo;            /* Table zTbl */
203309
203310    /* Locate and if necessary initialize the target table object */
203311    rc = sessionFindTable(pSession, zTbl, &pTo);
203312    if( pTo==0 ) goto diff_out;
203313    if( sessionInitTable(pSession, pTo) ){
203314      rc = pSession->rc;
203315      goto diff_out;
203316    }
203317
203318    /* Check the table schemas match */
203319    if( rc==SQLITE_OK ){
203320      int bHasPk = 0;
203321      int bMismatch = 0;
203322      int nCol;                   /* Columns in zFrom.zTbl */
203323      u8 *abPK;
203324      const char **azCol = 0;
203325      rc = sessionTableInfo(db, zFrom, zTbl, &nCol, 0, &azCol, &abPK);
203326      if( rc==SQLITE_OK ){
203327        if( pTo->nCol!=nCol ){
203328          bMismatch = 1;
203329        }else{
203330          int i;
203331          for(i=0; i<nCol; i++){
203332            if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
203333            if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
203334            if( abPK[i] ) bHasPk = 1;
203335          }
203336        }
203337      }
203338      sqlite3_free((char*)azCol);
203339      if( bMismatch ){
203340        if( pzErrMsg ){
203341          *pzErrMsg = sqlite3_mprintf("table schemas do not match");
203342        }
203343        rc = SQLITE_SCHEMA;
203344      }
203345      if( bHasPk==0 ){
203346        /* Ignore tables with no primary keys */
203347        goto diff_out;
203348      }
203349    }
203350
203351    if( rc==SQLITE_OK ){
203352      zExpr = sessionExprComparePK(pTo->nCol,
203353          zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
203354      );
203355    }
203356
203357    /* Find new rows */
203358    if( rc==SQLITE_OK ){
203359      rc = sessionDiffFindNew(SQLITE_INSERT, pSession, pTo, zDb, zFrom, zExpr);
203360    }
203361
203362    /* Find old rows */
203363    if( rc==SQLITE_OK ){
203364      rc = sessionDiffFindNew(SQLITE_DELETE, pSession, pTo, zFrom, zDb, zExpr);
203365    }
203366
203367    /* Find modified rows */
203368    if( rc==SQLITE_OK ){
203369      rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
203370    }
203371
203372    sqlite3_free(zExpr);
203373  }
203374
203375 diff_out:
203376  sessionPreupdateHooks(pSession);
203377  sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
203378  return rc;
203379}
203380
203381/*
203382** Create a session object. This session object will record changes to
203383** database zDb attached to connection db.
203384*/
203385SQLITE_API int sqlite3session_create(
203386  sqlite3 *db,                    /* Database handle */
203387  const char *zDb,                /* Name of db (e.g. "main") */
203388  sqlite3_session **ppSession     /* OUT: New session object */
203389){
203390  sqlite3_session *pNew;          /* Newly allocated session object */
203391  sqlite3_session *pOld;          /* Session object already attached to db */
203392  int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
203393
203394  /* Zero the output value in case an error occurs. */
203395  *ppSession = 0;
203396
203397  /* Allocate and populate the new session object. */
203398  pNew = (sqlite3_session *)sqlite3_malloc64(sizeof(sqlite3_session) + nDb + 1);
203399  if( !pNew ) return SQLITE_NOMEM;
203400  memset(pNew, 0, sizeof(sqlite3_session));
203401  pNew->db = db;
203402  pNew->zDb = (char *)&pNew[1];
203403  pNew->bEnable = 1;
203404  memcpy(pNew->zDb, zDb, nDb+1);
203405  sessionPreupdateHooks(pNew);
203406
203407  /* Add the new session object to the linked list of session objects
203408  ** attached to database handle $db. Do this under the cover of the db
203409  ** handle mutex.  */
203410  sqlite3_mutex_enter(sqlite3_db_mutex(db));
203411  pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
203412  pNew->pNext = pOld;
203413  sqlite3_mutex_leave(sqlite3_db_mutex(db));
203414
203415  *ppSession = pNew;
203416  return SQLITE_OK;
203417}
203418
203419/*
203420** Free the list of table objects passed as the first argument. The contents
203421** of the changed-rows hash tables are also deleted.
203422*/
203423static void sessionDeleteTable(SessionTable *pList){
203424  SessionTable *pNext;
203425  SessionTable *pTab;
203426
203427  for(pTab=pList; pTab; pTab=pNext){
203428    int i;
203429    pNext = pTab->pNext;
203430    for(i=0; i<pTab->nChange; i++){
203431      SessionChange *p;
203432      SessionChange *pNextChange;
203433      for(p=pTab->apChange[i]; p; p=pNextChange){
203434        pNextChange = p->pNext;
203435        sqlite3_free(p);
203436      }
203437    }
203438    sqlite3_free((char*)pTab->azCol);  /* cast works around VC++ bug */
203439    sqlite3_free(pTab->apChange);
203440    sqlite3_free(pTab);
203441  }
203442}
203443
203444/*
203445** Delete a session object previously allocated using sqlite3session_create().
203446*/
203447SQLITE_API void sqlite3session_delete(sqlite3_session *pSession){
203448  sqlite3 *db = pSession->db;
203449  sqlite3_session *pHead;
203450  sqlite3_session **pp;
203451
203452  /* Unlink the session from the linked list of sessions attached to the
203453  ** database handle. Hold the db mutex while doing so.  */
203454  sqlite3_mutex_enter(sqlite3_db_mutex(db));
203455  pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
203456  for(pp=&pHead; ALWAYS((*pp)!=0); pp=&((*pp)->pNext)){
203457    if( (*pp)==pSession ){
203458      *pp = (*pp)->pNext;
203459      if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
203460      break;
203461    }
203462  }
203463  sqlite3_mutex_leave(sqlite3_db_mutex(db));
203464  sqlite3ValueFree(pSession->pZeroBlob);
203465
203466  /* Delete all attached table objects. And the contents of their
203467  ** associated hash-tables. */
203468  sessionDeleteTable(pSession->pTable);
203469
203470  /* Free the session object itself. */
203471  sqlite3_free(pSession);
203472}
203473
203474/*
203475** Set a table filter on a Session Object.
203476*/
203477SQLITE_API void sqlite3session_table_filter(
203478  sqlite3_session *pSession,
203479  int(*xFilter)(void*, const char*),
203480  void *pCtx                      /* First argument passed to xFilter */
203481){
203482  pSession->bAutoAttach = 1;
203483  pSession->pFilterCtx = pCtx;
203484  pSession->xTableFilter = xFilter;
203485}
203486
203487/*
203488** Attach a table to a session. All subsequent changes made to the table
203489** while the session object is enabled will be recorded.
203490**
203491** Only tables that have a PRIMARY KEY defined may be attached. It does
203492** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
203493** or not.
203494*/
203495SQLITE_API int sqlite3session_attach(
203496  sqlite3_session *pSession,      /* Session object */
203497  const char *zName               /* Table name */
203498){
203499  int rc = SQLITE_OK;
203500  sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
203501
203502  if( !zName ){
203503    pSession->bAutoAttach = 1;
203504  }else{
203505    SessionTable *pTab;           /* New table object (if required) */
203506    int nName;                    /* Number of bytes in string zName */
203507
203508    /* First search for an existing entry. If one is found, this call is
203509    ** a no-op. Return early. */
203510    nName = sqlite3Strlen30(zName);
203511    for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
203512      if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
203513    }
203514
203515    if( !pTab ){
203516      /* Allocate new SessionTable object. */
203517      pTab = (SessionTable *)sqlite3_malloc64(sizeof(SessionTable) + nName + 1);
203518      if( !pTab ){
203519        rc = SQLITE_NOMEM;
203520      }else{
203521        /* Populate the new SessionTable object and link it into the list.
203522        ** The new object must be linked onto the end of the list, not
203523        ** simply added to the start of it in order to ensure that tables
203524        ** appear in the correct order when a changeset or patchset is
203525        ** eventually generated. */
203526        SessionTable **ppTab;
203527        memset(pTab, 0, sizeof(SessionTable));
203528        pTab->zName = (char *)&pTab[1];
203529        memcpy(pTab->zName, zName, nName+1);
203530        for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
203531        *ppTab = pTab;
203532      }
203533    }
203534  }
203535
203536  sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
203537  return rc;
203538}
203539
203540/*
203541** Ensure that there is room in the buffer to append nByte bytes of data.
203542** If not, use sqlite3_realloc() to grow the buffer so that there is.
203543**
203544** If successful, return zero. Otherwise, if an OOM condition is encountered,
203545** set *pRc to SQLITE_NOMEM and return non-zero.
203546*/
203547static int sessionBufferGrow(SessionBuffer *p, size_t nByte, int *pRc){
203548  if( *pRc==SQLITE_OK && (size_t)(p->nAlloc-p->nBuf)<nByte ){
203549    u8 *aNew;
203550    i64 nNew = p->nAlloc ? p->nAlloc : 128;
203551    do {
203552      nNew = nNew*2;
203553    }while( (size_t)(nNew-p->nBuf)<nByte );
203554
203555    aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
203556    if( 0==aNew ){
203557      *pRc = SQLITE_NOMEM;
203558    }else{
203559      p->aBuf = aNew;
203560      p->nAlloc = nNew;
203561    }
203562  }
203563  return (*pRc!=SQLITE_OK);
203564}
203565
203566/*
203567** Append the value passed as the second argument to the buffer passed
203568** as the first.
203569**
203570** This function is a no-op if *pRc is non-zero when it is called.
203571** Otherwise, if an error occurs, *pRc is set to an SQLite error code
203572** before returning.
203573*/
203574static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
203575  int rc = *pRc;
203576  if( rc==SQLITE_OK ){
203577    sqlite3_int64 nByte = 0;
203578    rc = sessionSerializeValue(0, pVal, &nByte);
203579    sessionBufferGrow(p, nByte, &rc);
203580    if( rc==SQLITE_OK ){
203581      rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
203582      p->nBuf += nByte;
203583    }else{
203584      *pRc = rc;
203585    }
203586  }
203587}
203588
203589/*
203590** This function is a no-op if *pRc is other than SQLITE_OK when it is
203591** called. Otherwise, append a single byte to the buffer.
203592**
203593** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
203594** returning.
203595*/
203596static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
203597  if( 0==sessionBufferGrow(p, 1, pRc) ){
203598    p->aBuf[p->nBuf++] = v;
203599  }
203600}
203601
203602/*
203603** This function is a no-op if *pRc is other than SQLITE_OK when it is
203604** called. Otherwise, append a single varint to the buffer.
203605**
203606** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
203607** returning.
203608*/
203609static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
203610  if( 0==sessionBufferGrow(p, 9, pRc) ){
203611    p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
203612  }
203613}
203614
203615/*
203616** This function is a no-op if *pRc is other than SQLITE_OK when it is
203617** called. Otherwise, append a blob of data to the buffer.
203618**
203619** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
203620** returning.
203621*/
203622static void sessionAppendBlob(
203623  SessionBuffer *p,
203624  const u8 *aBlob,
203625  int nBlob,
203626  int *pRc
203627){
203628  if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){
203629    memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
203630    p->nBuf += nBlob;
203631  }
203632}
203633
203634/*
203635** This function is a no-op if *pRc is other than SQLITE_OK when it is
203636** called. Otherwise, append a string to the buffer. All bytes in the string
203637** up to (but not including) the nul-terminator are written to the buffer.
203638**
203639** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
203640** returning.
203641*/
203642static void sessionAppendStr(
203643  SessionBuffer *p,
203644  const char *zStr,
203645  int *pRc
203646){
203647  int nStr = sqlite3Strlen30(zStr);
203648  if( 0==sessionBufferGrow(p, nStr, pRc) ){
203649    memcpy(&p->aBuf[p->nBuf], zStr, nStr);
203650    p->nBuf += nStr;
203651  }
203652}
203653
203654/*
203655** This function is a no-op if *pRc is other than SQLITE_OK when it is
203656** called. Otherwise, append the string representation of integer iVal
203657** to the buffer. No nul-terminator is written.
203658**
203659** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
203660** returning.
203661*/
203662static void sessionAppendInteger(
203663  SessionBuffer *p,               /* Buffer to append to */
203664  int iVal,                       /* Value to write the string rep. of */
203665  int *pRc                        /* IN/OUT: Error code */
203666){
203667  char aBuf[24];
203668  sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
203669  sessionAppendStr(p, aBuf, pRc);
203670}
203671
203672/*
203673** This function is a no-op if *pRc is other than SQLITE_OK when it is
203674** called. Otherwise, append the string zStr enclosed in quotes (") and
203675** with any embedded quote characters escaped to the buffer. No
203676** nul-terminator byte is written.
203677**
203678** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
203679** returning.
203680*/
203681static void sessionAppendIdent(
203682  SessionBuffer *p,               /* Buffer to a append to */
203683  const char *zStr,               /* String to quote, escape and append */
203684  int *pRc                        /* IN/OUT: Error code */
203685){
203686  int nStr = sqlite3Strlen30(zStr)*2 + 2 + 1;
203687  if( 0==sessionBufferGrow(p, nStr, pRc) ){
203688    char *zOut = (char *)&p->aBuf[p->nBuf];
203689    const char *zIn = zStr;
203690    *zOut++ = '"';
203691    while( *zIn ){
203692      if( *zIn=='"' ) *zOut++ = '"';
203693      *zOut++ = *(zIn++);
203694    }
203695    *zOut++ = '"';
203696    p->nBuf = (int)((u8 *)zOut - p->aBuf);
203697  }
203698}
203699
203700/*
203701** This function is a no-op if *pRc is other than SQLITE_OK when it is
203702** called. Otherwse, it appends the serialized version of the value stored
203703** in column iCol of the row that SQL statement pStmt currently points
203704** to to the buffer.
203705*/
203706static void sessionAppendCol(
203707  SessionBuffer *p,               /* Buffer to append to */
203708  sqlite3_stmt *pStmt,            /* Handle pointing to row containing value */
203709  int iCol,                       /* Column to read value from */
203710  int *pRc                        /* IN/OUT: Error code */
203711){
203712  if( *pRc==SQLITE_OK ){
203713    int eType = sqlite3_column_type(pStmt, iCol);
203714    sessionAppendByte(p, (u8)eType, pRc);
203715    if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
203716      sqlite3_int64 i;
203717      u8 aBuf[8];
203718      if( eType==SQLITE_INTEGER ){
203719        i = sqlite3_column_int64(pStmt, iCol);
203720      }else{
203721        double r = sqlite3_column_double(pStmt, iCol);
203722        memcpy(&i, &r, 8);
203723      }
203724      sessionPutI64(aBuf, i);
203725      sessionAppendBlob(p, aBuf, 8, pRc);
203726    }
203727    if( eType==SQLITE_BLOB || eType==SQLITE_TEXT ){
203728      u8 *z;
203729      int nByte;
203730      if( eType==SQLITE_BLOB ){
203731        z = (u8 *)sqlite3_column_blob(pStmt, iCol);
203732      }else{
203733        z = (u8 *)sqlite3_column_text(pStmt, iCol);
203734      }
203735      nByte = sqlite3_column_bytes(pStmt, iCol);
203736      if( z || (eType==SQLITE_BLOB && nByte==0) ){
203737        sessionAppendVarint(p, nByte, pRc);
203738        sessionAppendBlob(p, z, nByte, pRc);
203739      }else{
203740        *pRc = SQLITE_NOMEM;
203741      }
203742    }
203743  }
203744}
203745
203746/*
203747**
203748** This function appends an update change to the buffer (see the comments
203749** under "CHANGESET FORMAT" at the top of the file). An update change
203750** consists of:
203751**
203752**   1 byte:  SQLITE_UPDATE (0x17)
203753**   n bytes: old.* record (see RECORD FORMAT)
203754**   m bytes: new.* record (see RECORD FORMAT)
203755**
203756** The SessionChange object passed as the third argument contains the
203757** values that were stored in the row when the session began (the old.*
203758** values). The statement handle passed as the second argument points
203759** at the current version of the row (the new.* values).
203760**
203761** If all of the old.* values are equal to their corresponding new.* value
203762** (i.e. nothing has changed), then no data at all is appended to the buffer.
203763**
203764** Otherwise, the old.* record contains all primary key values and the
203765** original values of any fields that have been modified. The new.* record
203766** contains the new values of only those fields that have been modified.
203767*/
203768static int sessionAppendUpdate(
203769  SessionBuffer *pBuf,            /* Buffer to append to */
203770  int bPatchset,                  /* True for "patchset", 0 for "changeset" */
203771  sqlite3_stmt *pStmt,            /* Statement handle pointing at new row */
203772  SessionChange *p,               /* Object containing old values */
203773  u8 *abPK                        /* Boolean array - true for PK columns */
203774){
203775  int rc = SQLITE_OK;
203776  SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
203777  int bNoop = 1;                /* Set to zero if any values are modified */
203778  int nRewind = pBuf->nBuf;     /* Set to zero if any values are modified */
203779  int i;                        /* Used to iterate through columns */
203780  u8 *pCsr = p->aRecord;        /* Used to iterate through old.* values */
203781
203782  sessionAppendByte(pBuf, SQLITE_UPDATE, &rc);
203783  sessionAppendByte(pBuf, p->bIndirect, &rc);
203784  for(i=0; i<sqlite3_column_count(pStmt); i++){
203785    int bChanged = 0;
203786    int nAdvance;
203787    int eType = *pCsr;
203788    switch( eType ){
203789      case SQLITE_NULL:
203790        nAdvance = 1;
203791        if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
203792          bChanged = 1;
203793        }
203794        break;
203795
203796      case SQLITE_FLOAT:
203797      case SQLITE_INTEGER: {
203798        nAdvance = 9;
203799        if( eType==sqlite3_column_type(pStmt, i) ){
203800          sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
203801          if( eType==SQLITE_INTEGER ){
203802            if( iVal==sqlite3_column_int64(pStmt, i) ) break;
203803          }else{
203804            double dVal;
203805            memcpy(&dVal, &iVal, 8);
203806            if( dVal==sqlite3_column_double(pStmt, i) ) break;
203807          }
203808        }
203809        bChanged = 1;
203810        break;
203811      }
203812
203813      default: {
203814        int n;
203815        int nHdr = 1 + sessionVarintGet(&pCsr[1], &n);
203816        assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
203817        nAdvance = nHdr + n;
203818        if( eType==sqlite3_column_type(pStmt, i)
203819         && n==sqlite3_column_bytes(pStmt, i)
203820         && (n==0 || 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), n))
203821        ){
203822          break;
203823        }
203824        bChanged = 1;
203825      }
203826    }
203827
203828    /* If at least one field has been modified, this is not a no-op. */
203829    if( bChanged ) bNoop = 0;
203830
203831    /* Add a field to the old.* record. This is omitted if this modules is
203832    ** currently generating a patchset. */
203833    if( bPatchset==0 ){
203834      if( bChanged || abPK[i] ){
203835        sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
203836      }else{
203837        sessionAppendByte(pBuf, 0, &rc);
203838      }
203839    }
203840
203841    /* Add a field to the new.* record. Or the only record if currently
203842    ** generating a patchset.  */
203843    if( bChanged || (bPatchset && abPK[i]) ){
203844      sessionAppendCol(&buf2, pStmt, i, &rc);
203845    }else{
203846      sessionAppendByte(&buf2, 0, &rc);
203847    }
203848
203849    pCsr += nAdvance;
203850  }
203851
203852  if( bNoop ){
203853    pBuf->nBuf = nRewind;
203854  }else{
203855    sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
203856  }
203857  sqlite3_free(buf2.aBuf);
203858
203859  return rc;
203860}
203861
203862/*
203863** Append a DELETE change to the buffer passed as the first argument. Use
203864** the changeset format if argument bPatchset is zero, or the patchset
203865** format otherwise.
203866*/
203867static int sessionAppendDelete(
203868  SessionBuffer *pBuf,            /* Buffer to append to */
203869  int bPatchset,                  /* True for "patchset", 0 for "changeset" */
203870  SessionChange *p,               /* Object containing old values */
203871  int nCol,                       /* Number of columns in table */
203872  u8 *abPK                        /* Boolean array - true for PK columns */
203873){
203874  int rc = SQLITE_OK;
203875
203876  sessionAppendByte(pBuf, SQLITE_DELETE, &rc);
203877  sessionAppendByte(pBuf, p->bIndirect, &rc);
203878
203879  if( bPatchset==0 ){
203880    sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
203881  }else{
203882    int i;
203883    u8 *a = p->aRecord;
203884    for(i=0; i<nCol; i++){
203885      u8 *pStart = a;
203886      int eType = *a++;
203887
203888      switch( eType ){
203889        case 0:
203890        case SQLITE_NULL:
203891          assert( abPK[i]==0 );
203892          break;
203893
203894        case SQLITE_FLOAT:
203895        case SQLITE_INTEGER:
203896          a += 8;
203897          break;
203898
203899        default: {
203900          int n;
203901          a += sessionVarintGet(a, &n);
203902          a += n;
203903          break;
203904        }
203905      }
203906      if( abPK[i] ){
203907        sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
203908      }
203909    }
203910    assert( (a - p->aRecord)==p->nRecord );
203911  }
203912
203913  return rc;
203914}
203915
203916/*
203917** Formulate and prepare a SELECT statement to retrieve a row from table
203918** zTab in database zDb based on its primary key. i.e.
203919**
203920**   SELECT * FROM zDb.zTab WHERE pk1 = ? AND pk2 = ? AND ...
203921*/
203922static int sessionSelectStmt(
203923  sqlite3 *db,                    /* Database handle */
203924  const char *zDb,                /* Database name */
203925  const char *zTab,               /* Table name */
203926  int nCol,                       /* Number of columns in table */
203927  const char **azCol,             /* Names of table columns */
203928  u8 *abPK,                       /* PRIMARY KEY  array */
203929  sqlite3_stmt **ppStmt           /* OUT: Prepared SELECT statement */
203930){
203931  int rc = SQLITE_OK;
203932  char *zSql = 0;
203933  int nSql = -1;
203934
203935  if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
203936    zSql = sqlite3_mprintf(
203937        "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
203938        "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
203939    );
203940    if( zSql==0 ) rc = SQLITE_NOMEM;
203941  }else{
203942    int i;
203943    const char *zSep = "";
203944    SessionBuffer buf = {0, 0, 0};
203945
203946    sessionAppendStr(&buf, "SELECT * FROM ", &rc);
203947    sessionAppendIdent(&buf, zDb, &rc);
203948    sessionAppendStr(&buf, ".", &rc);
203949    sessionAppendIdent(&buf, zTab, &rc);
203950    sessionAppendStr(&buf, " WHERE ", &rc);
203951    for(i=0; i<nCol; i++){
203952      if( abPK[i] ){
203953        sessionAppendStr(&buf, zSep, &rc);
203954        sessionAppendIdent(&buf, azCol[i], &rc);
203955        sessionAppendStr(&buf, " IS ?", &rc);
203956        sessionAppendInteger(&buf, i+1, &rc);
203957        zSep = " AND ";
203958      }
203959    }
203960    zSql = (char*)buf.aBuf;
203961    nSql = buf.nBuf;
203962  }
203963
203964  if( rc==SQLITE_OK ){
203965    rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, 0);
203966  }
203967  sqlite3_free(zSql);
203968  return rc;
203969}
203970
203971/*
203972** Bind the PRIMARY KEY values from the change passed in argument pChange
203973** to the SELECT statement passed as the first argument. The SELECT statement
203974** is as prepared by function sessionSelectStmt().
203975**
203976** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
203977** error code (e.g. SQLITE_NOMEM) otherwise.
203978*/
203979static int sessionSelectBind(
203980  sqlite3_stmt *pSelect,          /* SELECT from sessionSelectStmt() */
203981  int nCol,                       /* Number of columns in table */
203982  u8 *abPK,                       /* PRIMARY KEY array */
203983  SessionChange *pChange          /* Change structure */
203984){
203985  int i;
203986  int rc = SQLITE_OK;
203987  u8 *a = pChange->aRecord;
203988
203989  for(i=0; i<nCol && rc==SQLITE_OK; i++){
203990    int eType = *a++;
203991
203992    switch( eType ){
203993      case 0:
203994      case SQLITE_NULL:
203995        assert( abPK[i]==0 );
203996        break;
203997
203998      case SQLITE_INTEGER: {
203999        if( abPK[i] ){
204000          i64 iVal = sessionGetI64(a);
204001          rc = sqlite3_bind_int64(pSelect, i+1, iVal);
204002        }
204003        a += 8;
204004        break;
204005      }
204006
204007      case SQLITE_FLOAT: {
204008        if( abPK[i] ){
204009          double rVal;
204010          i64 iVal = sessionGetI64(a);
204011          memcpy(&rVal, &iVal, 8);
204012          rc = sqlite3_bind_double(pSelect, i+1, rVal);
204013        }
204014        a += 8;
204015        break;
204016      }
204017
204018      case SQLITE_TEXT: {
204019        int n;
204020        a += sessionVarintGet(a, &n);
204021        if( abPK[i] ){
204022          rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT);
204023        }
204024        a += n;
204025        break;
204026      }
204027
204028      default: {
204029        int n;
204030        assert( eType==SQLITE_BLOB );
204031        a += sessionVarintGet(a, &n);
204032        if( abPK[i] ){
204033          rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT);
204034        }
204035        a += n;
204036        break;
204037      }
204038    }
204039  }
204040
204041  return rc;
204042}
204043
204044/*
204045** This function is a no-op if *pRc is set to other than SQLITE_OK when it
204046** is called. Otherwise, append a serialized table header (part of the binary
204047** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
204048** SQLite error code before returning.
204049*/
204050static void sessionAppendTableHdr(
204051  SessionBuffer *pBuf,            /* Append header to this buffer */
204052  int bPatchset,                  /* Use the patchset format if true */
204053  SessionTable *pTab,             /* Table object to append header for */
204054  int *pRc                        /* IN/OUT: Error code */
204055){
204056  /* Write a table header */
204057  sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
204058  sessionAppendVarint(pBuf, pTab->nCol, pRc);
204059  sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
204060  sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
204061}
204062
204063/*
204064** Generate either a changeset (if argument bPatchset is zero) or a patchset
204065** (if it is non-zero) based on the current contents of the session object
204066** passed as the first argument.
204067**
204068** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
204069** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
204070** occurs, an SQLite error code is returned and both output variables set
204071** to 0.
204072*/
204073static int sessionGenerateChangeset(
204074  sqlite3_session *pSession,      /* Session object */
204075  int bPatchset,                  /* True for patchset, false for changeset */
204076  int (*xOutput)(void *pOut, const void *pData, int nData),
204077  void *pOut,                     /* First argument for xOutput */
204078  int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
204079  void **ppChangeset              /* OUT: Buffer containing changeset */
204080){
204081  sqlite3 *db = pSession->db;     /* Source database handle */
204082  SessionTable *pTab;             /* Used to iterate through attached tables */
204083  SessionBuffer buf = {0,0,0};    /* Buffer in which to accumlate changeset */
204084  int rc;                         /* Return code */
204085
204086  assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0 ) );
204087
204088  /* Zero the output variables in case an error occurs. If this session
204089  ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
204090  ** this call will be a no-op.  */
204091  if( xOutput==0 ){
204092    *pnChangeset = 0;
204093    *ppChangeset = 0;
204094  }
204095
204096  if( pSession->rc ) return pSession->rc;
204097  rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
204098  if( rc!=SQLITE_OK ) return rc;
204099
204100  sqlite3_mutex_enter(sqlite3_db_mutex(db));
204101
204102  for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
204103    if( pTab->nEntry ){
204104      const char *zName = pTab->zName;
204105      int nCol;                   /* Number of columns in table */
204106      u8 *abPK;                   /* Primary key array */
204107      const char **azCol = 0;     /* Table columns */
204108      int i;                      /* Used to iterate through hash buckets */
204109      sqlite3_stmt *pSel = 0;     /* SELECT statement to query table pTab */
204110      int nRewind = buf.nBuf;     /* Initial size of write buffer */
204111      int nNoop;                  /* Size of buffer after writing tbl header */
204112
204113      /* Check the table schema is still Ok. */
204114      rc = sessionTableInfo(db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK);
204115      if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
204116        rc = SQLITE_SCHEMA;
204117      }
204118
204119      /* Write a table header */
204120      sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
204121
204122      /* Build and compile a statement to execute: */
204123      if( rc==SQLITE_OK ){
204124        rc = sessionSelectStmt(
204125            db, pSession->zDb, zName, nCol, azCol, abPK, &pSel);
204126      }
204127
204128      nNoop = buf.nBuf;
204129      for(i=0; i<pTab->nChange && rc==SQLITE_OK; i++){
204130        SessionChange *p;         /* Used to iterate through changes */
204131
204132        for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){
204133          rc = sessionSelectBind(pSel, nCol, abPK, p);
204134          if( rc!=SQLITE_OK ) continue;
204135          if( sqlite3_step(pSel)==SQLITE_ROW ){
204136            if( p->op==SQLITE_INSERT ){
204137              int iCol;
204138              sessionAppendByte(&buf, SQLITE_INSERT, &rc);
204139              sessionAppendByte(&buf, p->bIndirect, &rc);
204140              for(iCol=0; iCol<nCol; iCol++){
204141                sessionAppendCol(&buf, pSel, iCol, &rc);
204142              }
204143            }else{
204144              rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, abPK);
204145            }
204146          }else if( p->op!=SQLITE_INSERT ){
204147            rc = sessionAppendDelete(&buf, bPatchset, p, nCol, abPK);
204148          }
204149          if( rc==SQLITE_OK ){
204150            rc = sqlite3_reset(pSel);
204151          }
204152
204153          /* If the buffer is now larger than sessions_strm_chunk_size, pass
204154          ** its contents to the xOutput() callback. */
204155          if( xOutput
204156           && rc==SQLITE_OK
204157           && buf.nBuf>nNoop
204158           && buf.nBuf>sessions_strm_chunk_size
204159          ){
204160            rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
204161            nNoop = -1;
204162            buf.nBuf = 0;
204163          }
204164
204165        }
204166      }
204167
204168      sqlite3_finalize(pSel);
204169      if( buf.nBuf==nNoop ){
204170        buf.nBuf = nRewind;
204171      }
204172      sqlite3_free((char*)azCol);  /* cast works around VC++ bug */
204173    }
204174  }
204175
204176  if( rc==SQLITE_OK ){
204177    if( xOutput==0 ){
204178      *pnChangeset = buf.nBuf;
204179      *ppChangeset = buf.aBuf;
204180      buf.aBuf = 0;
204181    }else if( buf.nBuf>0 ){
204182      rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
204183    }
204184  }
204185
204186  sqlite3_free(buf.aBuf);
204187  sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
204188  sqlite3_mutex_leave(sqlite3_db_mutex(db));
204189  return rc;
204190}
204191
204192/*
204193** Obtain a changeset object containing all changes recorded by the
204194** session object passed as the first argument.
204195**
204196** It is the responsibility of the caller to eventually free the buffer
204197** using sqlite3_free().
204198*/
204199SQLITE_API int sqlite3session_changeset(
204200  sqlite3_session *pSession,      /* Session object */
204201  int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
204202  void **ppChangeset              /* OUT: Buffer containing changeset */
204203){
204204  return sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset, ppChangeset);
204205}
204206
204207/*
204208** Streaming version of sqlite3session_changeset().
204209*/
204210SQLITE_API int sqlite3session_changeset_strm(
204211  sqlite3_session *pSession,
204212  int (*xOutput)(void *pOut, const void *pData, int nData),
204213  void *pOut
204214){
204215  return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
204216}
204217
204218/*
204219** Streaming version of sqlite3session_patchset().
204220*/
204221SQLITE_API int sqlite3session_patchset_strm(
204222  sqlite3_session *pSession,
204223  int (*xOutput)(void *pOut, const void *pData, int nData),
204224  void *pOut
204225){
204226  return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
204227}
204228
204229/*
204230** Obtain a patchset object containing all changes recorded by the
204231** session object passed as the first argument.
204232**
204233** It is the responsibility of the caller to eventually free the buffer
204234** using sqlite3_free().
204235*/
204236SQLITE_API int sqlite3session_patchset(
204237  sqlite3_session *pSession,      /* Session object */
204238  int *pnPatchset,                /* OUT: Size of buffer at *ppChangeset */
204239  void **ppPatchset               /* OUT: Buffer containing changeset */
204240){
204241  return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
204242}
204243
204244/*
204245** Enable or disable the session object passed as the first argument.
204246*/
204247SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
204248  int ret;
204249  sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
204250  if( bEnable>=0 ){
204251    pSession->bEnable = bEnable;
204252  }
204253  ret = pSession->bEnable;
204254  sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
204255  return ret;
204256}
204257
204258/*
204259** Enable or disable the session object passed as the first argument.
204260*/
204261SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){
204262  int ret;
204263  sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
204264  if( bIndirect>=0 ){
204265    pSession->bIndirect = bIndirect;
204266  }
204267  ret = pSession->bIndirect;
204268  sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
204269  return ret;
204270}
204271
204272/*
204273** Return true if there have been no changes to monitored tables recorded
204274** by the session object passed as the only argument.
204275*/
204276SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession){
204277  int ret = 0;
204278  SessionTable *pTab;
204279
204280  sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
204281  for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
204282    ret = (pTab->nEntry>0);
204283  }
204284  sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
204285
204286  return (ret==0);
204287}
204288
204289/*
204290** Do the work for either sqlite3changeset_start() or start_strm().
204291*/
204292static int sessionChangesetStart(
204293  sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
204294  int (*xInput)(void *pIn, void *pData, int *pnData),
204295  void *pIn,
204296  int nChangeset,                 /* Size of buffer pChangeset in bytes */
204297  void *pChangeset,               /* Pointer to buffer containing changeset */
204298  int bInvert                     /* True to invert changeset */
204299){
204300  sqlite3_changeset_iter *pRet;   /* Iterator to return */
204301  int nByte;                      /* Number of bytes to allocate for iterator */
204302
204303  assert( xInput==0 || (pChangeset==0 && nChangeset==0) );
204304
204305  /* Zero the output variable in case an error occurs. */
204306  *pp = 0;
204307
204308  /* Allocate and initialize the iterator structure. */
204309  nByte = sizeof(sqlite3_changeset_iter);
204310  pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
204311  if( !pRet ) return SQLITE_NOMEM;
204312  memset(pRet, 0, sizeof(sqlite3_changeset_iter));
204313  pRet->in.aData = (u8 *)pChangeset;
204314  pRet->in.nData = nChangeset;
204315  pRet->in.xInput = xInput;
204316  pRet->in.pIn = pIn;
204317  pRet->in.bEof = (xInput ? 0 : 1);
204318  pRet->bInvert = bInvert;
204319
204320  /* Populate the output variable and return success. */
204321  *pp = pRet;
204322  return SQLITE_OK;
204323}
204324
204325/*
204326** Create an iterator used to iterate through the contents of a changeset.
204327*/
204328SQLITE_API int sqlite3changeset_start(
204329  sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
204330  int nChangeset,                 /* Size of buffer pChangeset in bytes */
204331  void *pChangeset                /* Pointer to buffer containing changeset */
204332){
204333  return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0);
204334}
204335SQLITE_API int sqlite3changeset_start_v2(
204336  sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
204337  int nChangeset,                 /* Size of buffer pChangeset in bytes */
204338  void *pChangeset,               /* Pointer to buffer containing changeset */
204339  int flags
204340){
204341  int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
204342  return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert);
204343}
204344
204345/*
204346** Streaming version of sqlite3changeset_start().
204347*/
204348SQLITE_API int sqlite3changeset_start_strm(
204349  sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
204350  int (*xInput)(void *pIn, void *pData, int *pnData),
204351  void *pIn
204352){
204353  return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0);
204354}
204355SQLITE_API int sqlite3changeset_start_v2_strm(
204356  sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
204357  int (*xInput)(void *pIn, void *pData, int *pnData),
204358  void *pIn,
204359  int flags
204360){
204361  int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
204362  return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert);
204363}
204364
204365/*
204366** If the SessionInput object passed as the only argument is a streaming
204367** object and the buffer is full, discard some data to free up space.
204368*/
204369static void sessionDiscardData(SessionInput *pIn){
204370  if( pIn->xInput && pIn->iNext>=sessions_strm_chunk_size ){
204371    int nMove = pIn->buf.nBuf - pIn->iNext;
204372    assert( nMove>=0 );
204373    if( nMove>0 ){
204374      memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
204375    }
204376    pIn->buf.nBuf -= pIn->iNext;
204377    pIn->iNext = 0;
204378    pIn->nData = pIn->buf.nBuf;
204379  }
204380}
204381
204382/*
204383** Ensure that there are at least nByte bytes available in the buffer. Or,
204384** if there are not nByte bytes remaining in the input, that all available
204385** data is in the buffer.
204386**
204387** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
204388*/
204389static int sessionInputBuffer(SessionInput *pIn, int nByte){
204390  int rc = SQLITE_OK;
204391  if( pIn->xInput ){
204392    while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
204393      int nNew = sessions_strm_chunk_size;
204394
204395      if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
204396      if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
204397        rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
204398        if( nNew==0 ){
204399          pIn->bEof = 1;
204400        }else{
204401          pIn->buf.nBuf += nNew;
204402        }
204403      }
204404
204405      pIn->aData = pIn->buf.aBuf;
204406      pIn->nData = pIn->buf.nBuf;
204407    }
204408  }
204409  return rc;
204410}
204411
204412/*
204413** When this function is called, *ppRec points to the start of a record
204414** that contains nCol values. This function advances the pointer *ppRec
204415** until it points to the byte immediately following that record.
204416*/
204417static void sessionSkipRecord(
204418  u8 **ppRec,                     /* IN/OUT: Record pointer */
204419  int nCol                        /* Number of values in record */
204420){
204421  u8 *aRec = *ppRec;
204422  int i;
204423  for(i=0; i<nCol; i++){
204424    int eType = *aRec++;
204425    if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
204426      int nByte;
204427      aRec += sessionVarintGet((u8*)aRec, &nByte);
204428      aRec += nByte;
204429    }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
204430      aRec += 8;
204431    }
204432  }
204433
204434  *ppRec = aRec;
204435}
204436
204437/*
204438** This function sets the value of the sqlite3_value object passed as the
204439** first argument to a copy of the string or blob held in the aData[]
204440** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
204441** error occurs.
204442*/
204443static int sessionValueSetStr(
204444  sqlite3_value *pVal,            /* Set the value of this object */
204445  u8 *aData,                      /* Buffer containing string or blob data */
204446  int nData,                      /* Size of buffer aData[] in bytes */
204447  u8 enc                          /* String encoding (0 for blobs) */
204448){
204449  /* In theory this code could just pass SQLITE_TRANSIENT as the final
204450  ** argument to sqlite3ValueSetStr() and have the copy created
204451  ** automatically. But doing so makes it difficult to detect any OOM
204452  ** error. Hence the code to create the copy externally. */
204453  u8 *aCopy = sqlite3_malloc64((sqlite3_int64)nData+1);
204454  if( aCopy==0 ) return SQLITE_NOMEM;
204455  memcpy(aCopy, aData, nData);
204456  sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
204457  return SQLITE_OK;
204458}
204459
204460/*
204461** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
204462** for details.
204463**
204464** When this function is called, *paChange points to the start of the record
204465** to deserialize. Assuming no error occurs, *paChange is set to point to
204466** one byte after the end of the same record before this function returns.
204467** If the argument abPK is NULL, then the record contains nCol values. Or,
204468** if abPK is other than NULL, then the record contains only the PK fields
204469** (in other words, it is a patchset DELETE record).
204470**
204471** If successful, each element of the apOut[] array (allocated by the caller)
204472** is set to point to an sqlite3_value object containing the value read
204473** from the corresponding position in the record. If that value is not
204474** included in the record (i.e. because the record is part of an UPDATE change
204475** and the field was not modified), the corresponding element of apOut[] is
204476** set to NULL.
204477**
204478** It is the responsibility of the caller to free all sqlite_value structures
204479** using sqlite3_free().
204480**
204481** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
204482** The apOut[] array may have been partially populated in this case.
204483*/
204484static int sessionReadRecord(
204485  SessionInput *pIn,              /* Input data */
204486  int nCol,                       /* Number of values in record */
204487  u8 *abPK,                       /* Array of primary key flags, or NULL */
204488  sqlite3_value **apOut           /* Write values to this array */
204489){
204490  int i;                          /* Used to iterate through columns */
204491  int rc = SQLITE_OK;
204492
204493  for(i=0; i<nCol && rc==SQLITE_OK; i++){
204494    int eType = 0;                /* Type of value (SQLITE_NULL, TEXT etc.) */
204495    if( abPK && abPK[i]==0 ) continue;
204496    rc = sessionInputBuffer(pIn, 9);
204497    if( rc==SQLITE_OK ){
204498      if( pIn->iNext>=pIn->nData ){
204499        rc = SQLITE_CORRUPT_BKPT;
204500      }else{
204501        eType = pIn->aData[pIn->iNext++];
204502        assert( apOut[i]==0 );
204503        if( eType ){
204504          apOut[i] = sqlite3ValueNew(0);
204505          if( !apOut[i] ) rc = SQLITE_NOMEM;
204506        }
204507      }
204508    }
204509
204510    if( rc==SQLITE_OK ){
204511      u8 *aVal = &pIn->aData[pIn->iNext];
204512      if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
204513        int nByte;
204514        pIn->iNext += sessionVarintGet(aVal, &nByte);
204515        rc = sessionInputBuffer(pIn, nByte);
204516        if( rc==SQLITE_OK ){
204517          if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
204518            rc = SQLITE_CORRUPT_BKPT;
204519          }else{
204520            u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
204521            rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
204522            pIn->iNext += nByte;
204523          }
204524        }
204525      }
204526      if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
204527        sqlite3_int64 v = sessionGetI64(aVal);
204528        if( eType==SQLITE_INTEGER ){
204529          sqlite3VdbeMemSetInt64(apOut[i], v);
204530        }else{
204531          double d;
204532          memcpy(&d, &v, 8);
204533          sqlite3VdbeMemSetDouble(apOut[i], d);
204534        }
204535        pIn->iNext += 8;
204536      }
204537    }
204538  }
204539
204540  return rc;
204541}
204542
204543/*
204544** The input pointer currently points to the second byte of a table-header.
204545** Specifically, to the following:
204546**
204547**   + number of columns in table (varint)
204548**   + array of PK flags (1 byte per column),
204549**   + table name (nul terminated).
204550**
204551** This function ensures that all of the above is present in the input
204552** buffer (i.e. that it can be accessed without any calls to xInput()).
204553** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
204554** The input pointer is not moved.
204555*/
204556static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
204557  int rc = SQLITE_OK;
204558  int nCol = 0;
204559  int nRead = 0;
204560
204561  rc = sessionInputBuffer(pIn, 9);
204562  if( rc==SQLITE_OK ){
204563    nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
204564    /* The hard upper limit for the number of columns in an SQLite
204565    ** database table is, according to sqliteLimit.h, 32676. So
204566    ** consider any table-header that purports to have more than 65536
204567    ** columns to be corrupt. This is convenient because otherwise,
204568    ** if the (nCol>65536) condition below were omitted, a sufficiently
204569    ** large value for nCol may cause nRead to wrap around and become
204570    ** negative. Leading to a crash. */
204571    if( nCol<0 || nCol>65536 ){
204572      rc = SQLITE_CORRUPT_BKPT;
204573    }else{
204574      rc = sessionInputBuffer(pIn, nRead+nCol+100);
204575      nRead += nCol;
204576    }
204577  }
204578
204579  while( rc==SQLITE_OK ){
204580    while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
204581      nRead++;
204582    }
204583    if( (pIn->iNext + nRead)<pIn->nData ) break;
204584    rc = sessionInputBuffer(pIn, nRead + 100);
204585  }
204586  *pnByte = nRead+1;
204587  return rc;
204588}
204589
204590/*
204591** The input pointer currently points to the first byte of the first field
204592** of a record consisting of nCol columns. This function ensures the entire
204593** record is buffered. It does not move the input pointer.
204594**
204595** If successful, SQLITE_OK is returned and *pnByte is set to the size of
204596** the record in bytes. Otherwise, an SQLite error code is returned. The
204597** final value of *pnByte is undefined in this case.
204598*/
204599static int sessionChangesetBufferRecord(
204600  SessionInput *pIn,              /* Input data */
204601  int nCol,                       /* Number of columns in record */
204602  int *pnByte                     /* OUT: Size of record in bytes */
204603){
204604  int rc = SQLITE_OK;
204605  int nByte = 0;
204606  int i;
204607  for(i=0; rc==SQLITE_OK && i<nCol; i++){
204608    int eType;
204609    rc = sessionInputBuffer(pIn, nByte + 10);
204610    if( rc==SQLITE_OK ){
204611      eType = pIn->aData[pIn->iNext + nByte++];
204612      if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
204613        int n;
204614        nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
204615        nByte += n;
204616        rc = sessionInputBuffer(pIn, nByte);
204617      }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
204618        nByte += 8;
204619      }
204620    }
204621  }
204622  *pnByte = nByte;
204623  return rc;
204624}
204625
204626/*
204627** The input pointer currently points to the second byte of a table-header.
204628** Specifically, to the following:
204629**
204630**   + number of columns in table (varint)
204631**   + array of PK flags (1 byte per column),
204632**   + table name (nul terminated).
204633**
204634** This function decodes the table-header and populates the p->nCol,
204635** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
204636** also allocated or resized according to the new value of p->nCol. The
204637** input pointer is left pointing to the byte following the table header.
204638**
204639** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
204640** is returned and the final values of the various fields enumerated above
204641** are undefined.
204642*/
204643static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
204644  int rc;
204645  int nCopy;
204646  assert( p->rc==SQLITE_OK );
204647
204648  rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
204649  if( rc==SQLITE_OK ){
204650    int nByte;
204651    int nVarint;
204652    nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
204653    if( p->nCol>0 ){
204654      nCopy -= nVarint;
204655      p->in.iNext += nVarint;
204656      nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
204657      p->tblhdr.nBuf = 0;
204658      sessionBufferGrow(&p->tblhdr, nByte, &rc);
204659    }else{
204660      rc = SQLITE_CORRUPT_BKPT;
204661    }
204662  }
204663
204664  if( rc==SQLITE_OK ){
204665    size_t iPK = sizeof(sqlite3_value*)*p->nCol*2;
204666    memset(p->tblhdr.aBuf, 0, iPK);
204667    memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
204668    p->in.iNext += nCopy;
204669  }
204670
204671  p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
204672  p->abPK = (u8*)&p->apValue[p->nCol*2];
204673  p->zTab = (char*)&p->abPK[p->nCol];
204674  return (p->rc = rc);
204675}
204676
204677/*
204678** Advance the changeset iterator to the next change.
204679**
204680** If both paRec and pnRec are NULL, then this function works like the public
204681** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
204682** sqlite3changeset_new() and old() APIs may be used to query for values.
204683**
204684** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
204685** record is written to *paRec before returning and the number of bytes in
204686** the record to *pnRec.
204687**
204688** Either way, this function returns SQLITE_ROW if the iterator is
204689** successfully advanced to the next change in the changeset, an SQLite
204690** error code if an error occurs, or SQLITE_DONE if there are no further
204691** changes in the changeset.
204692*/
204693static int sessionChangesetNext(
204694  sqlite3_changeset_iter *p,      /* Changeset iterator */
204695  u8 **paRec,                     /* If non-NULL, store record pointer here */
204696  int *pnRec,                     /* If non-NULL, store size of record here */
204697  int *pbNew                      /* If non-NULL, true if new table */
204698){
204699  int i;
204700  u8 op;
204701
204702  assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
204703
204704  /* If the iterator is in the error-state, return immediately. */
204705  if( p->rc!=SQLITE_OK ) return p->rc;
204706
204707  /* Free the current contents of p->apValue[], if any. */
204708  if( p->apValue ){
204709    for(i=0; i<p->nCol*2; i++){
204710      sqlite3ValueFree(p->apValue[i]);
204711    }
204712    memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
204713  }
204714
204715  /* Make sure the buffer contains at least 10 bytes of input data, or all
204716  ** remaining data if there are less than 10 bytes available. This is
204717  ** sufficient either for the 'T' or 'P' byte and the varint that follows
204718  ** it, or for the two single byte values otherwise. */
204719  p->rc = sessionInputBuffer(&p->in, 2);
204720  if( p->rc!=SQLITE_OK ) return p->rc;
204721
204722  /* If the iterator is already at the end of the changeset, return DONE. */
204723  if( p->in.iNext>=p->in.nData ){
204724    return SQLITE_DONE;
204725  }
204726
204727  sessionDiscardData(&p->in);
204728  p->in.iCurrent = p->in.iNext;
204729
204730  op = p->in.aData[p->in.iNext++];
204731  while( op=='T' || op=='P' ){
204732    if( pbNew ) *pbNew = 1;
204733    p->bPatchset = (op=='P');
204734    if( sessionChangesetReadTblhdr(p) ) return p->rc;
204735    if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
204736    p->in.iCurrent = p->in.iNext;
204737    if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
204738    op = p->in.aData[p->in.iNext++];
204739  }
204740
204741  if( p->zTab==0 || (p->bPatchset && p->bInvert) ){
204742    /* The first record in the changeset is not a table header. Must be a
204743    ** corrupt changeset. */
204744    assert( p->in.iNext==1 || p->zTab );
204745    return (p->rc = SQLITE_CORRUPT_BKPT);
204746  }
204747
204748  p->op = op;
204749  p->bIndirect = p->in.aData[p->in.iNext++];
204750  if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
204751    return (p->rc = SQLITE_CORRUPT_BKPT);
204752  }
204753
204754  if( paRec ){
204755    int nVal;                     /* Number of values to buffer */
204756    if( p->bPatchset==0 && op==SQLITE_UPDATE ){
204757      nVal = p->nCol * 2;
204758    }else if( p->bPatchset && op==SQLITE_DELETE ){
204759      nVal = 0;
204760      for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
204761    }else{
204762      nVal = p->nCol;
204763    }
204764    p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
204765    if( p->rc!=SQLITE_OK ) return p->rc;
204766    *paRec = &p->in.aData[p->in.iNext];
204767    p->in.iNext += *pnRec;
204768  }else{
204769    sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue);
204770    sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]);
204771
204772    /* If this is an UPDATE or DELETE, read the old.* record. */
204773    if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
204774      u8 *abPK = p->bPatchset ? p->abPK : 0;
204775      p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld);
204776      if( p->rc!=SQLITE_OK ) return p->rc;
204777    }
204778
204779    /* If this is an INSERT or UPDATE, read the new.* record. */
204780    if( p->op!=SQLITE_DELETE ){
204781      p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew);
204782      if( p->rc!=SQLITE_OK ) return p->rc;
204783    }
204784
204785    if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE ){
204786      /* If this is an UPDATE that is part of a patchset, then all PK and
204787      ** modified fields are present in the new.* record. The old.* record
204788      ** is currently completely empty. This block shifts the PK fields from
204789      ** new.* to old.*, to accommodate the code that reads these arrays.  */
204790      for(i=0; i<p->nCol; i++){
204791        assert( p->bPatchset==0 || p->apValue[i]==0 );
204792        if( p->abPK[i] ){
204793          assert( p->apValue[i]==0 );
204794          p->apValue[i] = p->apValue[i+p->nCol];
204795          if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
204796          p->apValue[i+p->nCol] = 0;
204797        }
204798      }
204799    }else if( p->bInvert ){
204800      if( p->op==SQLITE_INSERT ) p->op = SQLITE_DELETE;
204801      else if( p->op==SQLITE_DELETE ) p->op = SQLITE_INSERT;
204802    }
204803  }
204804
204805  return SQLITE_ROW;
204806}
204807
204808/*
204809** Advance an iterator created by sqlite3changeset_start() to the next
204810** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
204811** or SQLITE_CORRUPT.
204812**
204813** This function may not be called on iterators passed to a conflict handler
204814** callback by changeset_apply().
204815*/
204816SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
204817  return sessionChangesetNext(p, 0, 0, 0);
204818}
204819
204820/*
204821** The following function extracts information on the current change
204822** from a changeset iterator. It may only be called after changeset_next()
204823** has returned SQLITE_ROW.
204824*/
204825SQLITE_API int sqlite3changeset_op(
204826  sqlite3_changeset_iter *pIter,  /* Iterator handle */
204827  const char **pzTab,             /* OUT: Pointer to table name */
204828  int *pnCol,                     /* OUT: Number of columns in table */
204829  int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
204830  int *pbIndirect                 /* OUT: True if change is indirect */
204831){
204832  *pOp = pIter->op;
204833  *pnCol = pIter->nCol;
204834  *pzTab = pIter->zTab;
204835  if( pbIndirect ) *pbIndirect = pIter->bIndirect;
204836  return SQLITE_OK;
204837}
204838
204839/*
204840** Return information regarding the PRIMARY KEY and number of columns in
204841** the database table affected by the change that pIter currently points
204842** to. This function may only be called after changeset_next() returns
204843** SQLITE_ROW.
204844*/
204845SQLITE_API int sqlite3changeset_pk(
204846  sqlite3_changeset_iter *pIter,  /* Iterator object */
204847  unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
204848  int *pnCol                      /* OUT: Number of entries in output array */
204849){
204850  *pabPK = pIter->abPK;
204851  if( pnCol ) *pnCol = pIter->nCol;
204852  return SQLITE_OK;
204853}
204854
204855/*
204856** This function may only be called while the iterator is pointing to an
204857** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
204858** Otherwise, SQLITE_MISUSE is returned.
204859**
204860** It sets *ppValue to point to an sqlite3_value structure containing the
204861** iVal'th value in the old.* record. Or, if that particular value is not
204862** included in the record (because the change is an UPDATE and the field
204863** was not modified and is not a PK column), set *ppValue to NULL.
204864**
204865** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
204866** not modified. Otherwise, SQLITE_OK.
204867*/
204868SQLITE_API int sqlite3changeset_old(
204869  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
204870  int iVal,                       /* Index of old.* value to retrieve */
204871  sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
204872){
204873  if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_DELETE ){
204874    return SQLITE_MISUSE;
204875  }
204876  if( iVal<0 || iVal>=pIter->nCol ){
204877    return SQLITE_RANGE;
204878  }
204879  *ppValue = pIter->apValue[iVal];
204880  return SQLITE_OK;
204881}
204882
204883/*
204884** This function may only be called while the iterator is pointing to an
204885** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
204886** Otherwise, SQLITE_MISUSE is returned.
204887**
204888** It sets *ppValue to point to an sqlite3_value structure containing the
204889** iVal'th value in the new.* record. Or, if that particular value is not
204890** included in the record (because the change is an UPDATE and the field
204891** was not modified), set *ppValue to NULL.
204892**
204893** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
204894** not modified. Otherwise, SQLITE_OK.
204895*/
204896SQLITE_API int sqlite3changeset_new(
204897  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
204898  int iVal,                       /* Index of new.* value to retrieve */
204899  sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
204900){
204901  if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_INSERT ){
204902    return SQLITE_MISUSE;
204903  }
204904  if( iVal<0 || iVal>=pIter->nCol ){
204905    return SQLITE_RANGE;
204906  }
204907  *ppValue = pIter->apValue[pIter->nCol+iVal];
204908  return SQLITE_OK;
204909}
204910
204911/*
204912** The following two macros are used internally. They are similar to the
204913** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
204914** they omit all error checking and return a pointer to the requested value.
204915*/
204916#define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)]
204917#define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)]
204918
204919/*
204920** This function may only be called with a changeset iterator that has been
204921** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT
204922** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
204923**
204924** If successful, *ppValue is set to point to an sqlite3_value structure
204925** containing the iVal'th value of the conflicting record.
204926**
204927** If value iVal is out-of-range or some other error occurs, an SQLite error
204928** code is returned. Otherwise, SQLITE_OK.
204929*/
204930SQLITE_API int sqlite3changeset_conflict(
204931  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
204932  int iVal,                       /* Index of conflict record value to fetch */
204933  sqlite3_value **ppValue         /* OUT: Value from conflicting row */
204934){
204935  if( !pIter->pConflict ){
204936    return SQLITE_MISUSE;
204937  }
204938  if( iVal<0 || iVal>=pIter->nCol ){
204939    return SQLITE_RANGE;
204940  }
204941  *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
204942  return SQLITE_OK;
204943}
204944
204945/*
204946** This function may only be called with an iterator passed to an
204947** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
204948** it sets the output variable to the total number of known foreign key
204949** violations in the destination database and returns SQLITE_OK.
204950**
204951** In all other cases this function returns SQLITE_MISUSE.
204952*/
204953SQLITE_API int sqlite3changeset_fk_conflicts(
204954  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
204955  int *pnOut                      /* OUT: Number of FK violations */
204956){
204957  if( pIter->pConflict || pIter->apValue ){
204958    return SQLITE_MISUSE;
204959  }
204960  *pnOut = pIter->nCol;
204961  return SQLITE_OK;
204962}
204963
204964
204965/*
204966** Finalize an iterator allocated with sqlite3changeset_start().
204967**
204968** This function may not be called on iterators passed to a conflict handler
204969** callback by changeset_apply().
204970*/
204971SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
204972  int rc = SQLITE_OK;
204973  if( p ){
204974    int i;                        /* Used to iterate through p->apValue[] */
204975    rc = p->rc;
204976    if( p->apValue ){
204977      for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
204978    }
204979    sqlite3_free(p->tblhdr.aBuf);
204980    sqlite3_free(p->in.buf.aBuf);
204981    sqlite3_free(p);
204982  }
204983  return rc;
204984}
204985
204986static int sessionChangesetInvert(
204987  SessionInput *pInput,           /* Input changeset */
204988  int (*xOutput)(void *pOut, const void *pData, int nData),
204989  void *pOut,
204990  int *pnInverted,                /* OUT: Number of bytes in output changeset */
204991  void **ppInverted               /* OUT: Inverse of pChangeset */
204992){
204993  int rc = SQLITE_OK;             /* Return value */
204994  SessionBuffer sOut;             /* Output buffer */
204995  int nCol = 0;                   /* Number of cols in current table */
204996  u8 *abPK = 0;                   /* PK array for current table */
204997  sqlite3_value **apVal = 0;      /* Space for values for UPDATE inversion */
204998  SessionBuffer sPK = {0, 0, 0};  /* PK array for current table */
204999
205000  /* Initialize the output buffer */
205001  memset(&sOut, 0, sizeof(SessionBuffer));
205002
205003  /* Zero the output variables in case an error occurs. */
205004  if( ppInverted ){
205005    *ppInverted = 0;
205006    *pnInverted = 0;
205007  }
205008
205009  while( 1 ){
205010    u8 eType;
205011
205012    /* Test for EOF. */
205013    if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
205014    if( pInput->iNext>=pInput->nData ) break;
205015    eType = pInput->aData[pInput->iNext];
205016
205017    switch( eType ){
205018      case 'T': {
205019        /* A 'table' record consists of:
205020        **
205021        **   * A constant 'T' character,
205022        **   * Number of columns in said table (a varint),
205023        **   * An array of nCol bytes (sPK),
205024        **   * A nul-terminated table name.
205025        */
205026        int nByte;
205027        int nVar;
205028        pInput->iNext++;
205029        if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
205030          goto finished_invert;
205031        }
205032        nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
205033        sPK.nBuf = 0;
205034        sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
205035        sessionAppendByte(&sOut, eType, &rc);
205036        sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
205037        if( rc ) goto finished_invert;
205038
205039        pInput->iNext += nByte;
205040        sqlite3_free(apVal);
205041        apVal = 0;
205042        abPK = sPK.aBuf;
205043        break;
205044      }
205045
205046      case SQLITE_INSERT:
205047      case SQLITE_DELETE: {
205048        int nByte;
205049        int bIndirect = pInput->aData[pInput->iNext+1];
205050        int eType2 = (eType==SQLITE_DELETE ? SQLITE_INSERT : SQLITE_DELETE);
205051        pInput->iNext += 2;
205052        assert( rc==SQLITE_OK );
205053        rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
205054        sessionAppendByte(&sOut, eType2, &rc);
205055        sessionAppendByte(&sOut, bIndirect, &rc);
205056        sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
205057        pInput->iNext += nByte;
205058        if( rc ) goto finished_invert;
205059        break;
205060      }
205061
205062      case SQLITE_UPDATE: {
205063        int iCol;
205064
205065        if( 0==apVal ){
205066          apVal = (sqlite3_value **)sqlite3_malloc64(sizeof(apVal[0])*nCol*2);
205067          if( 0==apVal ){
205068            rc = SQLITE_NOMEM;
205069            goto finished_invert;
205070          }
205071          memset(apVal, 0, sizeof(apVal[0])*nCol*2);
205072        }
205073
205074        /* Write the header for the new UPDATE change. Same as the original. */
205075        sessionAppendByte(&sOut, eType, &rc);
205076        sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
205077
205078        /* Read the old.* and new.* records for the update change. */
205079        pInput->iNext += 2;
205080        rc = sessionReadRecord(pInput, nCol, 0, &apVal[0]);
205081        if( rc==SQLITE_OK ){
205082          rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol]);
205083        }
205084
205085        /* Write the new old.* record. Consists of the PK columns from the
205086        ** original old.* record, and the other values from the original
205087        ** new.* record. */
205088        for(iCol=0; iCol<nCol; iCol++){
205089          sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
205090          sessionAppendValue(&sOut, pVal, &rc);
205091        }
205092
205093        /* Write the new new.* record. Consists of a copy of all values
205094        ** from the original old.* record, except for the PK columns, which
205095        ** are set to "undefined". */
205096        for(iCol=0; iCol<nCol; iCol++){
205097          sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
205098          sessionAppendValue(&sOut, pVal, &rc);
205099        }
205100
205101        for(iCol=0; iCol<nCol*2; iCol++){
205102          sqlite3ValueFree(apVal[iCol]);
205103        }
205104        memset(apVal, 0, sizeof(apVal[0])*nCol*2);
205105        if( rc!=SQLITE_OK ){
205106          goto finished_invert;
205107        }
205108
205109        break;
205110      }
205111
205112      default:
205113        rc = SQLITE_CORRUPT_BKPT;
205114        goto finished_invert;
205115    }
205116
205117    assert( rc==SQLITE_OK );
205118    if( xOutput && sOut.nBuf>=sessions_strm_chunk_size ){
205119      rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
205120      sOut.nBuf = 0;
205121      if( rc!=SQLITE_OK ) goto finished_invert;
205122    }
205123  }
205124
205125  assert( rc==SQLITE_OK );
205126  if( pnInverted ){
205127    *pnInverted = sOut.nBuf;
205128    *ppInverted = sOut.aBuf;
205129    sOut.aBuf = 0;
205130  }else if( sOut.nBuf>0 ){
205131    rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
205132  }
205133
205134 finished_invert:
205135  sqlite3_free(sOut.aBuf);
205136  sqlite3_free(apVal);
205137  sqlite3_free(sPK.aBuf);
205138  return rc;
205139}
205140
205141
205142/*
205143** Invert a changeset object.
205144*/
205145SQLITE_API int sqlite3changeset_invert(
205146  int nChangeset,                 /* Number of bytes in input */
205147  const void *pChangeset,         /* Input changeset */
205148  int *pnInverted,                /* OUT: Number of bytes in output changeset */
205149  void **ppInverted               /* OUT: Inverse of pChangeset */
205150){
205151  SessionInput sInput;
205152
205153  /* Set up the input stream */
205154  memset(&sInput, 0, sizeof(SessionInput));
205155  sInput.nData = nChangeset;
205156  sInput.aData = (u8*)pChangeset;
205157
205158  return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
205159}
205160
205161/*
205162** Streaming version of sqlite3changeset_invert().
205163*/
205164SQLITE_API int sqlite3changeset_invert_strm(
205165  int (*xInput)(void *pIn, void *pData, int *pnData),
205166  void *pIn,
205167  int (*xOutput)(void *pOut, const void *pData, int nData),
205168  void *pOut
205169){
205170  SessionInput sInput;
205171  int rc;
205172
205173  /* Set up the input stream */
205174  memset(&sInput, 0, sizeof(SessionInput));
205175  sInput.xInput = xInput;
205176  sInput.pIn = pIn;
205177
205178  rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
205179  sqlite3_free(sInput.buf.aBuf);
205180  return rc;
205181}
205182
205183typedef struct SessionApplyCtx SessionApplyCtx;
205184struct SessionApplyCtx {
205185  sqlite3 *db;
205186  sqlite3_stmt *pDelete;          /* DELETE statement */
205187  sqlite3_stmt *pUpdate;          /* UPDATE statement */
205188  sqlite3_stmt *pInsert;          /* INSERT statement */
205189  sqlite3_stmt *pSelect;          /* SELECT statement */
205190  int nCol;                       /* Size of azCol[] and abPK[] arrays */
205191  const char **azCol;             /* Array of column names */
205192  u8 *abPK;                       /* Boolean array - true if column is in PK */
205193  int bStat1;                     /* True if table is sqlite_stat1 */
205194  int bDeferConstraints;          /* True to defer constraints */
205195  SessionBuffer constraints;      /* Deferred constraints are stored here */
205196  SessionBuffer rebase;           /* Rebase information (if any) here */
205197  u8 bRebaseStarted;              /* If table header is already in rebase */
205198  u8 bRebase;                     /* True to collect rebase information */
205199};
205200
205201/*
205202** Formulate a statement to DELETE a row from database db. Assuming a table
205203** structure like this:
205204**
205205**     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
205206**
205207** The DELETE statement looks like this:
205208**
205209**     DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
205210**
205211** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
205212** matching b and d values, or 1 otherwise. The second case comes up if the
205213** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
205214**
205215** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
205216** pointing to the prepared version of the SQL statement.
205217*/
205218static int sessionDeleteRow(
205219  sqlite3 *db,                    /* Database handle */
205220  const char *zTab,               /* Table name */
205221  SessionApplyCtx *p              /* Session changeset-apply context */
205222){
205223  int i;
205224  const char *zSep = "";
205225  int rc = SQLITE_OK;
205226  SessionBuffer buf = {0, 0, 0};
205227  int nPk = 0;
205228
205229  sessionAppendStr(&buf, "DELETE FROM main.", &rc);
205230  sessionAppendIdent(&buf, zTab, &rc);
205231  sessionAppendStr(&buf, " WHERE ", &rc);
205232
205233  for(i=0; i<p->nCol; i++){
205234    if( p->abPK[i] ){
205235      nPk++;
205236      sessionAppendStr(&buf, zSep, &rc);
205237      sessionAppendIdent(&buf, p->azCol[i], &rc);
205238      sessionAppendStr(&buf, " = ?", &rc);
205239      sessionAppendInteger(&buf, i+1, &rc);
205240      zSep = " AND ";
205241    }
205242  }
205243
205244  if( nPk<p->nCol ){
205245    sessionAppendStr(&buf, " AND (?", &rc);
205246    sessionAppendInteger(&buf, p->nCol+1, &rc);
205247    sessionAppendStr(&buf, " OR ", &rc);
205248
205249    zSep = "";
205250    for(i=0; i<p->nCol; i++){
205251      if( !p->abPK[i] ){
205252        sessionAppendStr(&buf, zSep, &rc);
205253        sessionAppendIdent(&buf, p->azCol[i], &rc);
205254        sessionAppendStr(&buf, " IS ?", &rc);
205255        sessionAppendInteger(&buf, i+1, &rc);
205256        zSep = "AND ";
205257      }
205258    }
205259    sessionAppendStr(&buf, ")", &rc);
205260  }
205261
205262  if( rc==SQLITE_OK ){
205263    rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
205264  }
205265  sqlite3_free(buf.aBuf);
205266
205267  return rc;
205268}
205269
205270/*
205271** Formulate and prepare a statement to UPDATE a row from database db.
205272** Assuming a table structure like this:
205273**
205274**     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
205275**
205276** The UPDATE statement looks like this:
205277**
205278**     UPDATE x SET
205279**     a = CASE WHEN ?2  THEN ?3  ELSE a END,
205280**     b = CASE WHEN ?5  THEN ?6  ELSE b END,
205281**     c = CASE WHEN ?8  THEN ?9  ELSE c END,
205282**     d = CASE WHEN ?11 THEN ?12 ELSE d END
205283**     WHERE a = ?1 AND c = ?7 AND (?13 OR
205284**       (?5==0 OR b IS ?4) AND (?11==0 OR d IS ?10) AND
205285**     )
205286**
205287** For each column in the table, there are three variables to bind:
205288**
205289**     ?(i*3+1)    The old.* value of the column, if any.
205290**     ?(i*3+2)    A boolean flag indicating that the value is being modified.
205291**     ?(i*3+3)    The new.* value of the column, if any.
205292**
205293** Also, a boolean flag that, if set to true, causes the statement to update
205294** a row even if the non-PK values do not match. This is required if the
205295** conflict-handler is invoked with CHANGESET_DATA and returns
205296** CHANGESET_REPLACE. This is variable "?(nCol*3+1)".
205297**
205298** If successful, SQLITE_OK is returned and SessionApplyCtx.pUpdate is left
205299** pointing to the prepared version of the SQL statement.
205300*/
205301static int sessionUpdateRow(
205302  sqlite3 *db,                    /* Database handle */
205303  const char *zTab,               /* Table name */
205304  SessionApplyCtx *p              /* Session changeset-apply context */
205305){
205306  int rc = SQLITE_OK;
205307  int i;
205308  const char *zSep = "";
205309  SessionBuffer buf = {0, 0, 0};
205310
205311  /* Append "UPDATE tbl SET " */
205312  sessionAppendStr(&buf, "UPDATE main.", &rc);
205313  sessionAppendIdent(&buf, zTab, &rc);
205314  sessionAppendStr(&buf, " SET ", &rc);
205315
205316  /* Append the assignments */
205317  for(i=0; i<p->nCol; i++){
205318    sessionAppendStr(&buf, zSep, &rc);
205319    sessionAppendIdent(&buf, p->azCol[i], &rc);
205320    sessionAppendStr(&buf, " = CASE WHEN ?", &rc);
205321    sessionAppendInteger(&buf, i*3+2, &rc);
205322    sessionAppendStr(&buf, " THEN ?", &rc);
205323    sessionAppendInteger(&buf, i*3+3, &rc);
205324    sessionAppendStr(&buf, " ELSE ", &rc);
205325    sessionAppendIdent(&buf, p->azCol[i], &rc);
205326    sessionAppendStr(&buf, " END", &rc);
205327    zSep = ", ";
205328  }
205329
205330  /* Append the PK part of the WHERE clause */
205331  sessionAppendStr(&buf, " WHERE ", &rc);
205332  for(i=0; i<p->nCol; i++){
205333    if( p->abPK[i] ){
205334      sessionAppendIdent(&buf, p->azCol[i], &rc);
205335      sessionAppendStr(&buf, " = ?", &rc);
205336      sessionAppendInteger(&buf, i*3+1, &rc);
205337      sessionAppendStr(&buf, " AND ", &rc);
205338    }
205339  }
205340
205341  /* Append the non-PK part of the WHERE clause */
205342  sessionAppendStr(&buf, " (?", &rc);
205343  sessionAppendInteger(&buf, p->nCol*3+1, &rc);
205344  sessionAppendStr(&buf, " OR 1", &rc);
205345  for(i=0; i<p->nCol; i++){
205346    if( !p->abPK[i] ){
205347      sessionAppendStr(&buf, " AND (?", &rc);
205348      sessionAppendInteger(&buf, i*3+2, &rc);
205349      sessionAppendStr(&buf, "=0 OR ", &rc);
205350      sessionAppendIdent(&buf, p->azCol[i], &rc);
205351      sessionAppendStr(&buf, " IS ?", &rc);
205352      sessionAppendInteger(&buf, i*3+1, &rc);
205353      sessionAppendStr(&buf, ")", &rc);
205354    }
205355  }
205356  sessionAppendStr(&buf, ")", &rc);
205357
205358  if( rc==SQLITE_OK ){
205359    rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pUpdate, 0);
205360  }
205361  sqlite3_free(buf.aBuf);
205362
205363  return rc;
205364}
205365
205366
205367/*
205368** Formulate and prepare an SQL statement to query table zTab by primary
205369** key. Assuming the following table structure:
205370**
205371**     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
205372**
205373** The SELECT statement looks like this:
205374**
205375**     SELECT * FROM x WHERE a = ?1 AND c = ?3
205376**
205377** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
205378** pointing to the prepared version of the SQL statement.
205379*/
205380static int sessionSelectRow(
205381  sqlite3 *db,                    /* Database handle */
205382  const char *zTab,               /* Table name */
205383  SessionApplyCtx *p              /* Session changeset-apply context */
205384){
205385  return sessionSelectStmt(
205386      db, "main", zTab, p->nCol, p->azCol, p->abPK, &p->pSelect);
205387}
205388
205389/*
205390** Formulate and prepare an INSERT statement to add a record to table zTab.
205391** For example:
205392**
205393**     INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
205394**
205395** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
205396** pointing to the prepared version of the SQL statement.
205397*/
205398static int sessionInsertRow(
205399  sqlite3 *db,                    /* Database handle */
205400  const char *zTab,               /* Table name */
205401  SessionApplyCtx *p              /* Session changeset-apply context */
205402){
205403  int rc = SQLITE_OK;
205404  int i;
205405  SessionBuffer buf = {0, 0, 0};
205406
205407  sessionAppendStr(&buf, "INSERT INTO main.", &rc);
205408  sessionAppendIdent(&buf, zTab, &rc);
205409  sessionAppendStr(&buf, "(", &rc);
205410  for(i=0; i<p->nCol; i++){
205411    if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
205412    sessionAppendIdent(&buf, p->azCol[i], &rc);
205413  }
205414
205415  sessionAppendStr(&buf, ") VALUES(?", &rc);
205416  for(i=1; i<p->nCol; i++){
205417    sessionAppendStr(&buf, ", ?", &rc);
205418  }
205419  sessionAppendStr(&buf, ")", &rc);
205420
205421  if( rc==SQLITE_OK ){
205422    rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
205423  }
205424  sqlite3_free(buf.aBuf);
205425  return rc;
205426}
205427
205428static int sessionPrepare(sqlite3 *db, sqlite3_stmt **pp, const char *zSql){
205429  return sqlite3_prepare_v2(db, zSql, -1, pp, 0);
205430}
205431
205432/*
205433** Prepare statements for applying changes to the sqlite_stat1 table.
205434** These are similar to those created by sessionSelectRow(),
205435** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for
205436** other tables.
205437*/
205438static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){
205439  int rc = sessionSelectRow(db, "sqlite_stat1", p);
205440  if( rc==SQLITE_OK ){
205441    rc = sessionPrepare(db, &p->pInsert,
205442        "INSERT INTO main.sqlite_stat1 VALUES(?1, "
205443        "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, "
205444        "?3)"
205445    );
205446  }
205447  if( rc==SQLITE_OK ){
205448    rc = sessionPrepare(db, &p->pUpdate,
205449        "UPDATE main.sqlite_stat1 SET "
205450        "tbl = CASE WHEN ?2 THEN ?3 ELSE tbl END, "
205451        "idx = CASE WHEN ?5 THEN ?6 ELSE idx END, "
205452        "stat = CASE WHEN ?8 THEN ?9 ELSE stat END  "
205453        "WHERE tbl=?1 AND idx IS "
205454        "CASE WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL ELSE ?4 END "
205455        "AND (?10 OR ?8=0 OR stat IS ?7)"
205456    );
205457  }
205458  if( rc==SQLITE_OK ){
205459    rc = sessionPrepare(db, &p->pDelete,
205460        "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
205461        "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
205462        "AND (?4 OR stat IS ?3)"
205463    );
205464  }
205465  return rc;
205466}
205467
205468/*
205469** A wrapper around sqlite3_bind_value() that detects an extra problem.
205470** See comments in the body of this function for details.
205471*/
205472static int sessionBindValue(
205473  sqlite3_stmt *pStmt,            /* Statement to bind value to */
205474  int i,                          /* Parameter number to bind to */
205475  sqlite3_value *pVal             /* Value to bind */
205476){
205477  int eType = sqlite3_value_type(pVal);
205478  /* COVERAGE: The (pVal->z==0) branch is never true using current versions
205479  ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
205480  ** the (pVal->z) variable remains as it was or the type of the value is
205481  ** set to SQLITE_NULL.  */
205482  if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){
205483    /* This condition occurs when an earlier OOM in a call to
205484    ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
205485    ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
205486    return SQLITE_NOMEM;
205487  }
205488  return sqlite3_bind_value(pStmt, i, pVal);
205489}
205490
205491/*
205492** Iterator pIter must point to an SQLITE_INSERT entry. This function
205493** transfers new.* values from the current iterator entry to statement
205494** pStmt. The table being inserted into has nCol columns.
205495**
205496** New.* value $i from the iterator is bound to variable ($i+1) of
205497** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
205498** are transfered to the statement. Otherwise, if abPK is not NULL, it points
205499** to an array nCol elements in size. In this case only those values for
205500** which abPK[$i] is true are read from the iterator and bound to the
205501** statement.
205502**
205503** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
205504*/
205505static int sessionBindRow(
205506  sqlite3_changeset_iter *pIter,  /* Iterator to read values from */
205507  int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
205508  int nCol,                       /* Number of columns */
205509  u8 *abPK,                       /* If not NULL, bind only if true */
205510  sqlite3_stmt *pStmt             /* Bind values to this statement */
205511){
205512  int i;
205513  int rc = SQLITE_OK;
205514
205515  /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
205516  ** argument iterator points to a suitable entry. Make sure that xValue
205517  ** is one of these to guarantee that it is safe to ignore the return
205518  ** in the code below. */
205519  assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
205520
205521  for(i=0; rc==SQLITE_OK && i<nCol; i++){
205522    if( !abPK || abPK[i] ){
205523      sqlite3_value *pVal;
205524      (void)xValue(pIter, i, &pVal);
205525      if( pVal==0 ){
205526        /* The value in the changeset was "undefined". This indicates a
205527        ** corrupt changeset blob.  */
205528        rc = SQLITE_CORRUPT_BKPT;
205529      }else{
205530        rc = sessionBindValue(pStmt, i+1, pVal);
205531      }
205532    }
205533  }
205534  return rc;
205535}
205536
205537/*
205538** SQL statement pSelect is as generated by the sessionSelectRow() function.
205539** This function binds the primary key values from the change that changeset
205540** iterator pIter points to to the SELECT and attempts to seek to the table
205541** entry. If a row is found, the SELECT statement left pointing at the row
205542** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
205543** has occured, the statement is reset and SQLITE_OK is returned. If an
205544** error occurs, the statement is reset and an SQLite error code is returned.
205545**
205546** If this function returns SQLITE_ROW, the caller must eventually reset()
205547** statement pSelect. If any other value is returned, the statement does
205548** not require a reset().
205549**
205550** If the iterator currently points to an INSERT record, bind values from the
205551** new.* record to the SELECT statement. Or, if it points to a DELETE or
205552** UPDATE, bind values from the old.* record.
205553*/
205554static int sessionSeekToRow(
205555  sqlite3 *db,                    /* Database handle */
205556  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
205557  u8 *abPK,                       /* Primary key flags array */
205558  sqlite3_stmt *pSelect           /* SELECT statement from sessionSelectRow() */
205559){
205560  int rc;                         /* Return code */
205561  int nCol;                       /* Number of columns in table */
205562  int op;                         /* Changset operation (SQLITE_UPDATE etc.) */
205563  const char *zDummy;             /* Unused */
205564
205565  sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
205566  rc = sessionBindRow(pIter,
205567      op==SQLITE_INSERT ? sqlite3changeset_new : sqlite3changeset_old,
205568      nCol, abPK, pSelect
205569  );
205570
205571  if( rc==SQLITE_OK ){
205572    rc = sqlite3_step(pSelect);
205573    if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
205574  }
205575
205576  return rc;
205577}
205578
205579/*
205580** This function is called from within sqlite3changeset_apply_v2() when
205581** a conflict is encountered and resolved using conflict resolution
205582** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
205583** It adds a conflict resolution record to the buffer in
205584** SessionApplyCtx.rebase, which will eventually be returned to the caller
205585** of apply_v2() as the "rebase" buffer.
205586**
205587** Return SQLITE_OK if successful, or an SQLite error code otherwise.
205588*/
205589static int sessionRebaseAdd(
205590  SessionApplyCtx *p,             /* Apply context */
205591  int eType,                      /* Conflict resolution (OMIT or REPLACE) */
205592  sqlite3_changeset_iter *pIter   /* Iterator pointing at current change */
205593){
205594  int rc = SQLITE_OK;
205595  if( p->bRebase ){
205596    int i;
205597    int eOp = pIter->op;
205598    if( p->bRebaseStarted==0 ){
205599      /* Append a table-header to the rebase buffer */
205600      const char *zTab = pIter->zTab;
205601      sessionAppendByte(&p->rebase, 'T', &rc);
205602      sessionAppendVarint(&p->rebase, p->nCol, &rc);
205603      sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
205604      sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
205605      p->bRebaseStarted = 1;
205606    }
205607
205608    assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
205609    assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
205610
205611    sessionAppendByte(&p->rebase,
205612        (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
205613        );
205614    sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
205615    for(i=0; i<p->nCol; i++){
205616      sqlite3_value *pVal = 0;
205617      if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
205618        sqlite3changeset_old(pIter, i, &pVal);
205619      }else{
205620        sqlite3changeset_new(pIter, i, &pVal);
205621      }
205622      sessionAppendValue(&p->rebase, pVal, &rc);
205623    }
205624  }
205625  return rc;
205626}
205627
205628/*
205629** Invoke the conflict handler for the change that the changeset iterator
205630** currently points to.
205631**
205632** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
205633** If argument pbReplace is NULL, then the type of conflict handler invoked
205634** depends solely on eType, as follows:
205635**
205636**    eType value                 Value passed to xConflict
205637**    -------------------------------------------------
205638**    CHANGESET_DATA              CHANGESET_NOTFOUND
205639**    CHANGESET_CONFLICT          CHANGESET_CONSTRAINT
205640**
205641** Or, if pbReplace is not NULL, then an attempt is made to find an existing
205642** record with the same primary key as the record about to be deleted, updated
205643** or inserted. If such a record can be found, it is available to the conflict
205644** handler as the "conflicting" record. In this case the type of conflict
205645** handler invoked is as follows:
205646**
205647**    eType value         PK Record found?   Value passed to xConflict
205648**    ----------------------------------------------------------------
205649**    CHANGESET_DATA      Yes                CHANGESET_DATA
205650**    CHANGESET_DATA      No                 CHANGESET_NOTFOUND
205651**    CHANGESET_CONFLICT  Yes                CHANGESET_CONFLICT
205652**    CHANGESET_CONFLICT  No                 CHANGESET_CONSTRAINT
205653**
205654** If pbReplace is not NULL, and a record with a matching PK is found, and
205655** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
205656** is set to non-zero before returning SQLITE_OK.
205657**
205658** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
205659** returned. Or, if the conflict handler returns an invalid value,
205660** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
205661** this function returns SQLITE_OK.
205662*/
205663static int sessionConflictHandler(
205664  int eType,                      /* Either CHANGESET_DATA or CONFLICT */
205665  SessionApplyCtx *p,             /* changeset_apply() context */
205666  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
205667  int(*xConflict)(void *, int, sqlite3_changeset_iter*),
205668  void *pCtx,                     /* First argument for conflict handler */
205669  int *pbReplace                  /* OUT: Set to true if PK row is found */
205670){
205671  int res = 0;                    /* Value returned by conflict handler */
205672  int rc;
205673  int nCol;
205674  int op;
205675  const char *zDummy;
205676
205677  sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
205678
205679  assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA );
205680  assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT );
205681  assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND );
205682
205683  /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
205684  if( pbReplace ){
205685    rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
205686  }else{
205687    rc = SQLITE_OK;
205688  }
205689
205690  if( rc==SQLITE_ROW ){
205691    /* There exists another row with the new.* primary key. */
205692    pIter->pConflict = p->pSelect;
205693    res = xConflict(pCtx, eType, pIter);
205694    pIter->pConflict = 0;
205695    rc = sqlite3_reset(p->pSelect);
205696  }else if( rc==SQLITE_OK ){
205697    if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
205698      /* Instead of invoking the conflict handler, append the change blob
205699      ** to the SessionApplyCtx.constraints buffer. */
205700      u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
205701      int nBlob = pIter->in.iNext - pIter->in.iCurrent;
205702      sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
205703      return SQLITE_OK;
205704    }else{
205705      /* No other row with the new.* primary key. */
205706      res = xConflict(pCtx, eType+1, pIter);
205707      if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
205708    }
205709  }
205710
205711  if( rc==SQLITE_OK ){
205712    switch( res ){
205713      case SQLITE_CHANGESET_REPLACE:
205714        assert( pbReplace );
205715        *pbReplace = 1;
205716        break;
205717
205718      case SQLITE_CHANGESET_OMIT:
205719        break;
205720
205721      case SQLITE_CHANGESET_ABORT:
205722        rc = SQLITE_ABORT;
205723        break;
205724
205725      default:
205726        rc = SQLITE_MISUSE;
205727        break;
205728    }
205729    if( rc==SQLITE_OK ){
205730      rc = sessionRebaseAdd(p, res, pIter);
205731    }
205732  }
205733
205734  return rc;
205735}
205736
205737/*
205738** Attempt to apply the change that the iterator passed as the first argument
205739** currently points to to the database. If a conflict is encountered, invoke
205740** the conflict handler callback.
205741**
205742** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
205743** one is encountered, update or delete the row with the matching primary key
205744** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
205745** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
205746** to true before returning. In this case the caller will invoke this function
205747** again, this time with pbRetry set to NULL.
205748**
205749** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is
205750** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
205751** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
205752** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
205753** before retrying. In this case the caller attempts to remove the conflicting
205754** row before invoking this function again, this time with pbReplace set
205755** to NULL.
205756**
205757** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
205758** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is
205759** returned.
205760*/
205761static int sessionApplyOneOp(
205762  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
205763  SessionApplyCtx *p,             /* changeset_apply() context */
205764  int(*xConflict)(void *, int, sqlite3_changeset_iter *),
205765  void *pCtx,                     /* First argument for the conflict handler */
205766  int *pbReplace,                 /* OUT: True to remove PK row and retry */
205767  int *pbRetry                    /* OUT: True to retry. */
205768){
205769  const char *zDummy;
205770  int op;
205771  int nCol;
205772  int rc = SQLITE_OK;
205773
205774  assert( p->pDelete && p->pUpdate && p->pInsert && p->pSelect );
205775  assert( p->azCol && p->abPK );
205776  assert( !pbReplace || *pbReplace==0 );
205777
205778  sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
205779
205780  if( op==SQLITE_DELETE ){
205781
205782    /* Bind values to the DELETE statement. If conflict handling is required,
205783    ** bind values for all columns and set bound variable (nCol+1) to true.
205784    ** Or, if conflict handling is not required, bind just the PK column
205785    ** values and, if it exists, set (nCol+1) to false. Conflict handling
205786    ** is not required if:
205787    **
205788    **   * this is a patchset, or
205789    **   * (pbRetry==0), or
205790    **   * all columns of the table are PK columns (in this case there is
205791    **     no (nCol+1) variable to bind to).
205792    */
205793    u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
205794    rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
205795    if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
205796      rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
205797    }
205798    if( rc!=SQLITE_OK ) return rc;
205799
205800    sqlite3_step(p->pDelete);
205801    rc = sqlite3_reset(p->pDelete);
205802    if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
205803      rc = sessionConflictHandler(
205804          SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
205805      );
205806    }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
205807      rc = sessionConflictHandler(
205808          SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
205809      );
205810    }
205811
205812  }else if( op==SQLITE_UPDATE ){
205813    int i;
205814
205815    /* Bind values to the UPDATE statement. */
205816    for(i=0; rc==SQLITE_OK && i<nCol; i++){
205817      sqlite3_value *pOld = sessionChangesetOld(pIter, i);
205818      sqlite3_value *pNew = sessionChangesetNew(pIter, i);
205819
205820      sqlite3_bind_int(p->pUpdate, i*3+2, !!pNew);
205821      if( pOld ){
205822        rc = sessionBindValue(p->pUpdate, i*3+1, pOld);
205823      }
205824      if( rc==SQLITE_OK && pNew ){
205825        rc = sessionBindValue(p->pUpdate, i*3+3, pNew);
205826      }
205827    }
205828    if( rc==SQLITE_OK ){
205829      sqlite3_bind_int(p->pUpdate, nCol*3+1, pbRetry==0 || pIter->bPatchset);
205830    }
205831    if( rc!=SQLITE_OK ) return rc;
205832
205833    /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
205834    ** the result will be SQLITE_OK with 0 rows modified. */
205835    sqlite3_step(p->pUpdate);
205836    rc = sqlite3_reset(p->pUpdate);
205837
205838    if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
205839      /* A NOTFOUND or DATA error. Search the table to see if it contains
205840      ** a row with a matching primary key. If so, this is a DATA conflict.
205841      ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
205842
205843      rc = sessionConflictHandler(
205844          SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
205845      );
205846
205847    }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
205848      /* This is always a CONSTRAINT conflict. */
205849      rc = sessionConflictHandler(
205850          SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
205851      );
205852    }
205853
205854  }else{
205855    assert( op==SQLITE_INSERT );
205856    if( p->bStat1 ){
205857      /* Check if there is a conflicting row. For sqlite_stat1, this needs
205858      ** to be done using a SELECT, as there is no PRIMARY KEY in the
205859      ** database schema to throw an exception if a duplicate is inserted.  */
205860      rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
205861      if( rc==SQLITE_ROW ){
205862        rc = SQLITE_CONSTRAINT;
205863        sqlite3_reset(p->pSelect);
205864      }
205865    }
205866
205867    if( rc==SQLITE_OK ){
205868      rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
205869      if( rc!=SQLITE_OK ) return rc;
205870
205871      sqlite3_step(p->pInsert);
205872      rc = sqlite3_reset(p->pInsert);
205873    }
205874
205875    if( (rc&0xff)==SQLITE_CONSTRAINT ){
205876      rc = sessionConflictHandler(
205877          SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace
205878      );
205879    }
205880  }
205881
205882  return rc;
205883}
205884
205885/*
205886** Attempt to apply the change that the iterator passed as the first argument
205887** currently points to to the database. If a conflict is encountered, invoke
205888** the conflict handler callback.
205889**
205890** The difference between this function and sessionApplyOne() is that this
205891** function handles the case where the conflict-handler is invoked and
205892** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
205893** retried in some manner.
205894*/
205895static int sessionApplyOneWithRetry(
205896  sqlite3 *db,                    /* Apply change to "main" db of this handle */
205897  sqlite3_changeset_iter *pIter,  /* Changeset iterator to read change from */
205898  SessionApplyCtx *pApply,        /* Apply context */
205899  int(*xConflict)(void*, int, sqlite3_changeset_iter*),
205900  void *pCtx                      /* First argument passed to xConflict */
205901){
205902  int bReplace = 0;
205903  int bRetry = 0;
205904  int rc;
205905
205906  rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
205907  if( rc==SQLITE_OK ){
205908    /* If the bRetry flag is set, the change has not been applied due to an
205909    ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
205910    ** a row with the correct PK is present in the db, but one or more other
205911    ** fields do not contain the expected values) and the conflict handler
205912    ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
205913    ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
205914    ** the SQLITE_CHANGESET_DATA problem.  */
205915    if( bRetry ){
205916      assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
205917      rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
205918    }
205919
205920    /* If the bReplace flag is set, the change is an INSERT that has not
205921    ** been performed because the database already contains a row with the
205922    ** specified primary key and the conflict handler returned
205923    ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
205924    ** before reattempting the INSERT.  */
205925    else if( bReplace ){
205926      assert( pIter->op==SQLITE_INSERT );
205927      rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
205928      if( rc==SQLITE_OK ){
205929        rc = sessionBindRow(pIter,
205930            sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
205931        sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
205932      }
205933      if( rc==SQLITE_OK ){
205934        sqlite3_step(pApply->pDelete);
205935        rc = sqlite3_reset(pApply->pDelete);
205936      }
205937      if( rc==SQLITE_OK ){
205938        rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
205939      }
205940      if( rc==SQLITE_OK ){
205941        rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
205942      }
205943    }
205944  }
205945
205946  return rc;
205947}
205948
205949/*
205950** Retry the changes accumulated in the pApply->constraints buffer.
205951*/
205952static int sessionRetryConstraints(
205953  sqlite3 *db,
205954  int bPatchset,
205955  const char *zTab,
205956  SessionApplyCtx *pApply,
205957  int(*xConflict)(void*, int, sqlite3_changeset_iter*),
205958  void *pCtx                      /* First argument passed to xConflict */
205959){
205960  int rc = SQLITE_OK;
205961
205962  while( pApply->constraints.nBuf ){
205963    sqlite3_changeset_iter *pIter2 = 0;
205964    SessionBuffer cons = pApply->constraints;
205965    memset(&pApply->constraints, 0, sizeof(SessionBuffer));
205966
205967    rc = sessionChangesetStart(&pIter2, 0, 0, cons.nBuf, cons.aBuf, 0);
205968    if( rc==SQLITE_OK ){
205969      size_t nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
205970      int rc2;
205971      pIter2->bPatchset = bPatchset;
205972      pIter2->zTab = (char*)zTab;
205973      pIter2->nCol = pApply->nCol;
205974      pIter2->abPK = pApply->abPK;
205975      sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
205976      pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
205977      if( rc==SQLITE_OK ) memset(pIter2->apValue, 0, nByte);
205978
205979      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter2) ){
205980        rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx);
205981      }
205982
205983      rc2 = sqlite3changeset_finalize(pIter2);
205984      if( rc==SQLITE_OK ) rc = rc2;
205985    }
205986    assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 );
205987
205988    sqlite3_free(cons.aBuf);
205989    if( rc!=SQLITE_OK ) break;
205990    if( pApply->constraints.nBuf>=cons.nBuf ){
205991      /* No progress was made on the last round. */
205992      pApply->bDeferConstraints = 0;
205993    }
205994  }
205995
205996  return rc;
205997}
205998
205999/*
206000** Argument pIter is a changeset iterator that has been initialized, but
206001** not yet passed to sqlite3changeset_next(). This function applies the
206002** changeset to the main database attached to handle "db". The supplied
206003** conflict handler callback is invoked to resolve any conflicts encountered
206004** while applying the change.
206005*/
206006static int sessionChangesetApply(
206007  sqlite3 *db,                    /* Apply change to "main" db of this handle */
206008  sqlite3_changeset_iter *pIter,  /* Changeset to apply */
206009  int(*xFilter)(
206010    void *pCtx,                   /* Copy of sixth arg to _apply() */
206011    const char *zTab              /* Table name */
206012  ),
206013  int(*xConflict)(
206014    void *pCtx,                   /* Copy of fifth arg to _apply() */
206015    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
206016    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
206017  ),
206018  void *pCtx,                     /* First argument passed to xConflict */
206019  void **ppRebase, int *pnRebase, /* OUT: Rebase information */
206020  int flags                       /* SESSION_APPLY_XXX flags */
206021){
206022  int schemaMismatch = 0;
206023  int rc = SQLITE_OK;             /* Return code */
206024  const char *zTab = 0;           /* Name of current table */
206025  int nTab = 0;                   /* Result of sqlite3Strlen30(zTab) */
206026  SessionApplyCtx sApply;         /* changeset_apply() context object */
206027  int bPatchset;
206028
206029  assert( xConflict!=0 );
206030
206031  pIter->in.bNoDiscard = 1;
206032  memset(&sApply, 0, sizeof(sApply));
206033  sApply.bRebase = (ppRebase && pnRebase);
206034  sqlite3_mutex_enter(sqlite3_db_mutex(db));
206035  if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
206036    rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
206037  }
206038  if( rc==SQLITE_OK ){
206039    rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
206040  }
206041  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){
206042    int nCol;
206043    int op;
206044    const char *zNew;
206045
206046    sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
206047
206048    if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
206049      u8 *abPK;
206050
206051      rc = sessionRetryConstraints(
206052          db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
206053      );
206054      if( rc!=SQLITE_OK ) break;
206055
206056      sqlite3_free((char*)sApply.azCol);  /* cast works around VC++ bug */
206057      sqlite3_finalize(sApply.pDelete);
206058      sqlite3_finalize(sApply.pUpdate);
206059      sqlite3_finalize(sApply.pInsert);
206060      sqlite3_finalize(sApply.pSelect);
206061      sApply.db = db;
206062      sApply.pDelete = 0;
206063      sApply.pUpdate = 0;
206064      sApply.pInsert = 0;
206065      sApply.pSelect = 0;
206066      sApply.nCol = 0;
206067      sApply.azCol = 0;
206068      sApply.abPK = 0;
206069      sApply.bStat1 = 0;
206070      sApply.bDeferConstraints = 1;
206071      sApply.bRebaseStarted = 0;
206072      memset(&sApply.constraints, 0, sizeof(SessionBuffer));
206073
206074      /* If an xFilter() callback was specified, invoke it now. If the
206075      ** xFilter callback returns zero, skip this table. If it returns
206076      ** non-zero, proceed. */
206077      schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
206078      if( schemaMismatch ){
206079        zTab = sqlite3_mprintf("%s", zNew);
206080        if( zTab==0 ){
206081          rc = SQLITE_NOMEM;
206082          break;
206083        }
206084        nTab = (int)strlen(zTab);
206085        sApply.azCol = (const char **)zTab;
206086      }else{
206087        int nMinCol = 0;
206088        int i;
206089
206090        sqlite3changeset_pk(pIter, &abPK, 0);
206091        rc = sessionTableInfo(
206092            db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
206093        );
206094        if( rc!=SQLITE_OK ) break;
206095        for(i=0; i<sApply.nCol; i++){
206096          if( sApply.abPK[i] ) nMinCol = i+1;
206097        }
206098
206099        if( sApply.nCol==0 ){
206100          schemaMismatch = 1;
206101          sqlite3_log(SQLITE_SCHEMA,
206102              "sqlite3changeset_apply(): no such table: %s", zTab
206103          );
206104        }
206105        else if( sApply.nCol<nCol ){
206106          schemaMismatch = 1;
206107          sqlite3_log(SQLITE_SCHEMA,
206108              "sqlite3changeset_apply(): table %s has %d columns, "
206109              "expected %d or more",
206110              zTab, sApply.nCol, nCol
206111          );
206112        }
206113        else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
206114          schemaMismatch = 1;
206115          sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
206116              "primary key mismatch for table %s", zTab
206117          );
206118        }
206119        else{
206120          sApply.nCol = nCol;
206121          if( 0==sqlite3_stricmp(zTab, "sqlite_stat1") ){
206122            if( (rc = sessionStat1Sql(db, &sApply) ) ){
206123              break;
206124            }
206125            sApply.bStat1 = 1;
206126          }else{
206127            if((rc = sessionSelectRow(db, zTab, &sApply))
206128                || (rc = sessionUpdateRow(db, zTab, &sApply))
206129                || (rc = sessionDeleteRow(db, zTab, &sApply))
206130                || (rc = sessionInsertRow(db, zTab, &sApply))
206131              ){
206132              break;
206133            }
206134            sApply.bStat1 = 0;
206135          }
206136        }
206137        nTab = sqlite3Strlen30(zTab);
206138      }
206139    }
206140
206141    /* If there is a schema mismatch on the current table, proceed to the
206142    ** next change. A log message has already been issued. */
206143    if( schemaMismatch ) continue;
206144
206145    rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
206146  }
206147
206148  bPatchset = pIter->bPatchset;
206149  if( rc==SQLITE_OK ){
206150    rc = sqlite3changeset_finalize(pIter);
206151  }else{
206152    sqlite3changeset_finalize(pIter);
206153  }
206154
206155  if( rc==SQLITE_OK ){
206156    rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx);
206157  }
206158
206159  if( rc==SQLITE_OK ){
206160    int nFk, notUsed;
206161    sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS, &nFk, &notUsed, 0);
206162    if( nFk!=0 ){
206163      int res = SQLITE_CHANGESET_ABORT;
206164      sqlite3_changeset_iter sIter;
206165      memset(&sIter, 0, sizeof(sIter));
206166      sIter.nCol = nFk;
206167      res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY, &sIter);
206168      if( res!=SQLITE_CHANGESET_OMIT ){
206169        rc = SQLITE_CONSTRAINT;
206170      }
206171    }
206172  }
206173  sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
206174
206175  if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
206176    if( rc==SQLITE_OK ){
206177      rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
206178    }else{
206179      sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
206180      sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
206181    }
206182  }
206183
206184  assert( sApply.bRebase || sApply.rebase.nBuf==0 );
206185  if( rc==SQLITE_OK && bPatchset==0 && sApply.bRebase ){
206186    *ppRebase = (void*)sApply.rebase.aBuf;
206187    *pnRebase = sApply.rebase.nBuf;
206188    sApply.rebase.aBuf = 0;
206189  }
206190  sqlite3_finalize(sApply.pInsert);
206191  sqlite3_finalize(sApply.pDelete);
206192  sqlite3_finalize(sApply.pUpdate);
206193  sqlite3_finalize(sApply.pSelect);
206194  sqlite3_free((char*)sApply.azCol);  /* cast works around VC++ bug */
206195  sqlite3_free((char*)sApply.constraints.aBuf);
206196  sqlite3_free((char*)sApply.rebase.aBuf);
206197  sqlite3_mutex_leave(sqlite3_db_mutex(db));
206198  return rc;
206199}
206200
206201/*
206202** Apply the changeset passed via pChangeset/nChangeset to the main
206203** database attached to handle "db".
206204*/
206205SQLITE_API int sqlite3changeset_apply_v2(
206206  sqlite3 *db,                    /* Apply change to "main" db of this handle */
206207  int nChangeset,                 /* Size of changeset in bytes */
206208  void *pChangeset,               /* Changeset blob */
206209  int(*xFilter)(
206210    void *pCtx,                   /* Copy of sixth arg to _apply() */
206211    const char *zTab              /* Table name */
206212  ),
206213  int(*xConflict)(
206214    void *pCtx,                   /* Copy of sixth arg to _apply() */
206215    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
206216    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
206217  ),
206218  void *pCtx,                     /* First argument passed to xConflict */
206219  void **ppRebase, int *pnRebase,
206220  int flags
206221){
206222  sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */
206223  int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
206224  int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset,bInverse);
206225  if( rc==SQLITE_OK ){
206226    rc = sessionChangesetApply(
206227        db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
206228    );
206229  }
206230  return rc;
206231}
206232
206233/*
206234** Apply the changeset passed via pChangeset/nChangeset to the main database
206235** attached to handle "db". Invoke the supplied conflict handler callback
206236** to resolve any conflicts encountered while applying the change.
206237*/
206238SQLITE_API int sqlite3changeset_apply(
206239  sqlite3 *db,                    /* Apply change to "main" db of this handle */
206240  int nChangeset,                 /* Size of changeset in bytes */
206241  void *pChangeset,               /* Changeset blob */
206242  int(*xFilter)(
206243    void *pCtx,                   /* Copy of sixth arg to _apply() */
206244    const char *zTab              /* Table name */
206245  ),
206246  int(*xConflict)(
206247    void *pCtx,                   /* Copy of fifth arg to _apply() */
206248    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
206249    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
206250  ),
206251  void *pCtx                      /* First argument passed to xConflict */
206252){
206253  return sqlite3changeset_apply_v2(
206254      db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0, 0
206255  );
206256}
206257
206258/*
206259** Apply the changeset passed via xInput/pIn to the main database
206260** attached to handle "db". Invoke the supplied conflict handler callback
206261** to resolve any conflicts encountered while applying the change.
206262*/
206263SQLITE_API int sqlite3changeset_apply_v2_strm(
206264  sqlite3 *db,                    /* Apply change to "main" db of this handle */
206265  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
206266  void *pIn,                                          /* First arg for xInput */
206267  int(*xFilter)(
206268    void *pCtx,                   /* Copy of sixth arg to _apply() */
206269    const char *zTab              /* Table name */
206270  ),
206271  int(*xConflict)(
206272    void *pCtx,                   /* Copy of sixth arg to _apply() */
206273    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
206274    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
206275  ),
206276  void *pCtx,                     /* First argument passed to xConflict */
206277  void **ppRebase, int *pnRebase,
206278  int flags
206279){
206280  sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */
206281  int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
206282  int rc = sessionChangesetStart(&pIter, xInput, pIn, 0, 0, bInverse);
206283  if( rc==SQLITE_OK ){
206284    rc = sessionChangesetApply(
206285        db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
206286    );
206287  }
206288  return rc;
206289}
206290SQLITE_API int sqlite3changeset_apply_strm(
206291  sqlite3 *db,                    /* Apply change to "main" db of this handle */
206292  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
206293  void *pIn,                                          /* First arg for xInput */
206294  int(*xFilter)(
206295    void *pCtx,                   /* Copy of sixth arg to _apply() */
206296    const char *zTab              /* Table name */
206297  ),
206298  int(*xConflict)(
206299    void *pCtx,                   /* Copy of sixth arg to _apply() */
206300    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
206301    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
206302  ),
206303  void *pCtx                      /* First argument passed to xConflict */
206304){
206305  return sqlite3changeset_apply_v2_strm(
206306      db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0, 0
206307  );
206308}
206309
206310/*
206311** sqlite3_changegroup handle.
206312*/
206313struct sqlite3_changegroup {
206314  int rc;                         /* Error code */
206315  int bPatch;                     /* True to accumulate patchsets */
206316  SessionTable *pList;            /* List of tables in current patch */
206317};
206318
206319/*
206320** This function is called to merge two changes to the same row together as
206321** part of an sqlite3changeset_concat() operation. A new change object is
206322** allocated and a pointer to it stored in *ppNew.
206323*/
206324static int sessionChangeMerge(
206325  SessionTable *pTab,             /* Table structure */
206326  int bRebase,                    /* True for a rebase hash-table */
206327  int bPatchset,                  /* True for patchsets */
206328  SessionChange *pExist,          /* Existing change */
206329  int op2,                        /* Second change operation */
206330  int bIndirect,                  /* True if second change is indirect */
206331  u8 *aRec,                       /* Second change record */
206332  int nRec,                       /* Number of bytes in aRec */
206333  SessionChange **ppNew           /* OUT: Merged change */
206334){
206335  SessionChange *pNew = 0;
206336  int rc = SQLITE_OK;
206337
206338  if( !pExist ){
206339    pNew = (SessionChange *)sqlite3_malloc64(sizeof(SessionChange) + nRec);
206340    if( !pNew ){
206341      return SQLITE_NOMEM;
206342    }
206343    memset(pNew, 0, sizeof(SessionChange));
206344    pNew->op = op2;
206345    pNew->bIndirect = bIndirect;
206346    pNew->aRecord = (u8*)&pNew[1];
206347    if( bIndirect==0 || bRebase==0 ){
206348      pNew->nRecord = nRec;
206349      memcpy(pNew->aRecord, aRec, nRec);
206350    }else{
206351      int i;
206352      u8 *pIn = aRec;
206353      u8 *pOut = pNew->aRecord;
206354      for(i=0; i<pTab->nCol; i++){
206355        int nIn = sessionSerialLen(pIn);
206356        if( *pIn==0 ){
206357          *pOut++ = 0;
206358        }else if( pTab->abPK[i]==0 ){
206359          *pOut++ = 0xFF;
206360        }else{
206361          memcpy(pOut, pIn, nIn);
206362          pOut += nIn;
206363        }
206364        pIn += nIn;
206365      }
206366      pNew->nRecord = pOut - pNew->aRecord;
206367    }
206368  }else if( bRebase ){
206369    if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
206370      *ppNew = pExist;
206371    }else{
206372      sqlite3_int64 nByte = nRec + pExist->nRecord + sizeof(SessionChange);
206373      pNew = (SessionChange*)sqlite3_malloc64(nByte);
206374      if( pNew==0 ){
206375        rc = SQLITE_NOMEM;
206376      }else{
206377        int i;
206378        u8 *a1 = pExist->aRecord;
206379        u8 *a2 = aRec;
206380        u8 *pOut;
206381
206382        memset(pNew, 0, nByte);
206383        pNew->bIndirect = bIndirect || pExist->bIndirect;
206384        pNew->op = op2;
206385        pOut = pNew->aRecord = (u8*)&pNew[1];
206386
206387        for(i=0; i<pTab->nCol; i++){
206388          int n1 = sessionSerialLen(a1);
206389          int n2 = sessionSerialLen(a2);
206390          if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
206391            *pOut++ = 0xFF;
206392          }else if( *a2==0 ){
206393            memcpy(pOut, a1, n1);
206394            pOut += n1;
206395          }else{
206396            memcpy(pOut, a2, n2);
206397            pOut += n2;
206398          }
206399          a1 += n1;
206400          a2 += n2;
206401        }
206402        pNew->nRecord = pOut - pNew->aRecord;
206403      }
206404      sqlite3_free(pExist);
206405    }
206406  }else{
206407    int op1 = pExist->op;
206408
206409    /*
206410    **   op1=INSERT, op2=INSERT      ->      Unsupported. Discard op2.
206411    **   op1=INSERT, op2=UPDATE      ->      INSERT.
206412    **   op1=INSERT, op2=DELETE      ->      (none)
206413    **
206414    **   op1=UPDATE, op2=INSERT      ->      Unsupported. Discard op2.
206415    **   op1=UPDATE, op2=UPDATE      ->      UPDATE.
206416    **   op1=UPDATE, op2=DELETE      ->      DELETE.
206417    **
206418    **   op1=DELETE, op2=INSERT      ->      UPDATE.
206419    **   op1=DELETE, op2=UPDATE      ->      Unsupported. Discard op2.
206420    **   op1=DELETE, op2=DELETE      ->      Unsupported. Discard op2.
206421    */
206422    if( (op1==SQLITE_INSERT && op2==SQLITE_INSERT)
206423     || (op1==SQLITE_UPDATE && op2==SQLITE_INSERT)
206424     || (op1==SQLITE_DELETE && op2==SQLITE_UPDATE)
206425     || (op1==SQLITE_DELETE && op2==SQLITE_DELETE)
206426    ){
206427      pNew = pExist;
206428    }else if( op1==SQLITE_INSERT && op2==SQLITE_DELETE ){
206429      sqlite3_free(pExist);
206430      assert( pNew==0 );
206431    }else{
206432      u8 *aExist = pExist->aRecord;
206433      sqlite3_int64 nByte;
206434      u8 *aCsr;
206435
206436      /* Allocate a new SessionChange object. Ensure that the aRecord[]
206437      ** buffer of the new object is large enough to hold any record that
206438      ** may be generated by combining the input records.  */
206439      nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
206440      pNew = (SessionChange *)sqlite3_malloc64(nByte);
206441      if( !pNew ){
206442        sqlite3_free(pExist);
206443        return SQLITE_NOMEM;
206444      }
206445      memset(pNew, 0, sizeof(SessionChange));
206446      pNew->bIndirect = (bIndirect && pExist->bIndirect);
206447      aCsr = pNew->aRecord = (u8 *)&pNew[1];
206448
206449      if( op1==SQLITE_INSERT ){             /* INSERT + UPDATE */
206450        u8 *a1 = aRec;
206451        assert( op2==SQLITE_UPDATE );
206452        pNew->op = SQLITE_INSERT;
206453        if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
206454        sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
206455      }else if( op1==SQLITE_DELETE ){       /* DELETE + INSERT */
206456        assert( op2==SQLITE_INSERT );
206457        pNew->op = SQLITE_UPDATE;
206458        if( bPatchset ){
206459          memcpy(aCsr, aRec, nRec);
206460          aCsr += nRec;
206461        }else{
206462          if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){
206463            sqlite3_free(pNew);
206464            pNew = 0;
206465          }
206466        }
206467      }else if( op2==SQLITE_UPDATE ){       /* UPDATE + UPDATE */
206468        u8 *a1 = aExist;
206469        u8 *a2 = aRec;
206470        assert( op1==SQLITE_UPDATE );
206471        if( bPatchset==0 ){
206472          sessionSkipRecord(&a1, pTab->nCol);
206473          sessionSkipRecord(&a2, pTab->nCol);
206474        }
206475        pNew->op = SQLITE_UPDATE;
206476        if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
206477          sqlite3_free(pNew);
206478          pNew = 0;
206479        }
206480      }else{                                /* UPDATE + DELETE */
206481        assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE );
206482        pNew->op = SQLITE_DELETE;
206483        if( bPatchset ){
206484          memcpy(aCsr, aRec, nRec);
206485          aCsr += nRec;
206486        }else{
206487          sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
206488        }
206489      }
206490
206491      if( pNew ){
206492        pNew->nRecord = (int)(aCsr - pNew->aRecord);
206493      }
206494      sqlite3_free(pExist);
206495    }
206496  }
206497
206498  *ppNew = pNew;
206499  return rc;
206500}
206501
206502/*
206503** Add all changes in the changeset traversed by the iterator passed as
206504** the first argument to the changegroup hash tables.
206505*/
206506static int sessionChangesetToHash(
206507  sqlite3_changeset_iter *pIter,   /* Iterator to read from */
206508  sqlite3_changegroup *pGrp,       /* Changegroup object to add changeset to */
206509  int bRebase                      /* True if hash table is for rebasing */
206510){
206511  u8 *aRec;
206512  int nRec;
206513  int rc = SQLITE_OK;
206514  SessionTable *pTab = 0;
206515
206516  while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
206517    const char *zNew;
206518    int nCol;
206519    int op;
206520    int iHash;
206521    int bIndirect;
206522    SessionChange *pChange;
206523    SessionChange *pExist = 0;
206524    SessionChange **pp;
206525
206526    if( pGrp->pList==0 ){
206527      pGrp->bPatch = pIter->bPatchset;
206528    }else if( pIter->bPatchset!=pGrp->bPatch ){
206529      rc = SQLITE_ERROR;
206530      break;
206531    }
206532
206533    sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect);
206534    if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
206535      /* Search the list for a matching table */
206536      int nNew = (int)strlen(zNew);
206537      u8 *abPK;
206538
206539      sqlite3changeset_pk(pIter, &abPK, 0);
206540      for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
206541        if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
206542      }
206543      if( !pTab ){
206544        SessionTable **ppTab;
206545
206546        pTab = sqlite3_malloc64(sizeof(SessionTable) + nCol + nNew+1);
206547        if( !pTab ){
206548          rc = SQLITE_NOMEM;
206549          break;
206550        }
206551        memset(pTab, 0, sizeof(SessionTable));
206552        pTab->nCol = nCol;
206553        pTab->abPK = (u8*)&pTab[1];
206554        memcpy(pTab->abPK, abPK, nCol);
206555        pTab->zName = (char*)&pTab->abPK[nCol];
206556        memcpy(pTab->zName, zNew, nNew+1);
206557
206558        /* The new object must be linked on to the end of the list, not
206559        ** simply added to the start of it. This is to ensure that the
206560        ** tables within the output of sqlite3changegroup_output() are in
206561        ** the right order.  */
206562        for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
206563        *ppTab = pTab;
206564      }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
206565        rc = SQLITE_SCHEMA;
206566        break;
206567      }
206568    }
206569
206570    if( sessionGrowHash(pIter->bPatchset, pTab) ){
206571      rc = SQLITE_NOMEM;
206572      break;
206573    }
206574    iHash = sessionChangeHash(
206575        pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
206576    );
206577
206578    /* Search for existing entry. If found, remove it from the hash table.
206579    ** Code below may link it back in.
206580    */
206581    for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
206582      int bPkOnly1 = 0;
206583      int bPkOnly2 = 0;
206584      if( pIter->bPatchset ){
206585        bPkOnly1 = (*pp)->op==SQLITE_DELETE;
206586        bPkOnly2 = op==SQLITE_DELETE;
206587      }
206588      if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
206589        pExist = *pp;
206590        *pp = (*pp)->pNext;
206591        pTab->nEntry--;
206592        break;
206593      }
206594    }
206595
206596    rc = sessionChangeMerge(pTab, bRebase,
206597        pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
206598    );
206599    if( rc ) break;
206600    if( pChange ){
206601      pChange->pNext = pTab->apChange[iHash];
206602      pTab->apChange[iHash] = pChange;
206603      pTab->nEntry++;
206604    }
206605  }
206606
206607  if( rc==SQLITE_OK ) rc = pIter->rc;
206608  return rc;
206609}
206610
206611/*
206612** Serialize a changeset (or patchset) based on all changesets (or patchsets)
206613** added to the changegroup object passed as the first argument.
206614**
206615** If xOutput is not NULL, then the changeset/patchset is returned to the
206616** user via one or more calls to xOutput, as with the other streaming
206617** interfaces.
206618**
206619** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
206620** buffer containing the output changeset before this function returns. In
206621** this case (*pnOut) is set to the size of the output buffer in bytes. It
206622** is the responsibility of the caller to free the output buffer using
206623** sqlite3_free() when it is no longer required.
206624**
206625** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
206626** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
206627** are both set to 0 before returning.
206628*/
206629static int sessionChangegroupOutput(
206630  sqlite3_changegroup *pGrp,
206631  int (*xOutput)(void *pOut, const void *pData, int nData),
206632  void *pOut,
206633  int *pnOut,
206634  void **ppOut
206635){
206636  int rc = SQLITE_OK;
206637  SessionBuffer buf = {0, 0, 0};
206638  SessionTable *pTab;
206639  assert( xOutput==0 || (ppOut==0 && pnOut==0) );
206640
206641  /* Create the serialized output changeset based on the contents of the
206642  ** hash tables attached to the SessionTable objects in list p->pList.
206643  */
206644  for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
206645    int i;
206646    if( pTab->nEntry==0 ) continue;
206647
206648    sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
206649    for(i=0; i<pTab->nChange; i++){
206650      SessionChange *p;
206651      for(p=pTab->apChange[i]; p; p=p->pNext){
206652        sessionAppendByte(&buf, p->op, &rc);
206653        sessionAppendByte(&buf, p->bIndirect, &rc);
206654        sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
206655        if( rc==SQLITE_OK && xOutput && buf.nBuf>=sessions_strm_chunk_size ){
206656          rc = xOutput(pOut, buf.aBuf, buf.nBuf);
206657          buf.nBuf = 0;
206658        }
206659      }
206660    }
206661  }
206662
206663  if( rc==SQLITE_OK ){
206664    if( xOutput ){
206665      if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
206666    }else{
206667      *ppOut = buf.aBuf;
206668      *pnOut = buf.nBuf;
206669      buf.aBuf = 0;
206670    }
206671  }
206672  sqlite3_free(buf.aBuf);
206673
206674  return rc;
206675}
206676
206677/*
206678** Allocate a new, empty, sqlite3_changegroup.
206679*/
206680SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp){
206681  int rc = SQLITE_OK;             /* Return code */
206682  sqlite3_changegroup *p;         /* New object */
206683  p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
206684  if( p==0 ){
206685    rc = SQLITE_NOMEM;
206686  }else{
206687    memset(p, 0, sizeof(sqlite3_changegroup));
206688  }
206689  *pp = p;
206690  return rc;
206691}
206692
206693/*
206694** Add the changeset currently stored in buffer pData, size nData bytes,
206695** to changeset-group p.
206696*/
206697SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
206698  sqlite3_changeset_iter *pIter;  /* Iterator opened on pData/nData */
206699  int rc;                         /* Return code */
206700
206701  rc = sqlite3changeset_start(&pIter, nData, pData);
206702  if( rc==SQLITE_OK ){
206703    rc = sessionChangesetToHash(pIter, pGrp, 0);
206704  }
206705  sqlite3changeset_finalize(pIter);
206706  return rc;
206707}
206708
206709/*
206710** Obtain a buffer containing a changeset representing the concatenation
206711** of all changesets added to the group so far.
206712*/
206713SQLITE_API int sqlite3changegroup_output(
206714    sqlite3_changegroup *pGrp,
206715    int *pnData,
206716    void **ppData
206717){
206718  return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
206719}
206720
206721/*
206722** Streaming versions of changegroup_add().
206723*/
206724SQLITE_API int sqlite3changegroup_add_strm(
206725  sqlite3_changegroup *pGrp,
206726  int (*xInput)(void *pIn, void *pData, int *pnData),
206727  void *pIn
206728){
206729  sqlite3_changeset_iter *pIter;  /* Iterator opened on pData/nData */
206730  int rc;                         /* Return code */
206731
206732  rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
206733  if( rc==SQLITE_OK ){
206734    rc = sessionChangesetToHash(pIter, pGrp, 0);
206735  }
206736  sqlite3changeset_finalize(pIter);
206737  return rc;
206738}
206739
206740/*
206741** Streaming versions of changegroup_output().
206742*/
206743SQLITE_API int sqlite3changegroup_output_strm(
206744  sqlite3_changegroup *pGrp,
206745  int (*xOutput)(void *pOut, const void *pData, int nData),
206746  void *pOut
206747){
206748  return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
206749}
206750
206751/*
206752** Delete a changegroup object.
206753*/
206754SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
206755  if( pGrp ){
206756    sessionDeleteTable(pGrp->pList);
206757    sqlite3_free(pGrp);
206758  }
206759}
206760
206761/*
206762** Combine two changesets together.
206763*/
206764SQLITE_API int sqlite3changeset_concat(
206765  int nLeft,                      /* Number of bytes in lhs input */
206766  void *pLeft,                    /* Lhs input changeset */
206767  int nRight                      /* Number of bytes in rhs input */,
206768  void *pRight,                   /* Rhs input changeset */
206769  int *pnOut,                     /* OUT: Number of bytes in output changeset */
206770  void **ppOut                    /* OUT: changeset (left <concat> right) */
206771){
206772  sqlite3_changegroup *pGrp;
206773  int rc;
206774
206775  rc = sqlite3changegroup_new(&pGrp);
206776  if( rc==SQLITE_OK ){
206777    rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
206778  }
206779  if( rc==SQLITE_OK ){
206780    rc = sqlite3changegroup_add(pGrp, nRight, pRight);
206781  }
206782  if( rc==SQLITE_OK ){
206783    rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
206784  }
206785  sqlite3changegroup_delete(pGrp);
206786
206787  return rc;
206788}
206789
206790/*
206791** Streaming version of sqlite3changeset_concat().
206792*/
206793SQLITE_API int sqlite3changeset_concat_strm(
206794  int (*xInputA)(void *pIn, void *pData, int *pnData),
206795  void *pInA,
206796  int (*xInputB)(void *pIn, void *pData, int *pnData),
206797  void *pInB,
206798  int (*xOutput)(void *pOut, const void *pData, int nData),
206799  void *pOut
206800){
206801  sqlite3_changegroup *pGrp;
206802  int rc;
206803
206804  rc = sqlite3changegroup_new(&pGrp);
206805  if( rc==SQLITE_OK ){
206806    rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
206807  }
206808  if( rc==SQLITE_OK ){
206809    rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
206810  }
206811  if( rc==SQLITE_OK ){
206812    rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
206813  }
206814  sqlite3changegroup_delete(pGrp);
206815
206816  return rc;
206817}
206818
206819/*
206820** Changeset rebaser handle.
206821*/
206822struct sqlite3_rebaser {
206823  sqlite3_changegroup grp;        /* Hash table */
206824};
206825
206826/*
206827** Buffers a1 and a2 must both contain a sessions module record nCol
206828** fields in size. This function appends an nCol sessions module
206829** record to buffer pBuf that is a copy of a1, except that for
206830** each field that is undefined in a1[], swap in the field from a2[].
206831*/
206832static void sessionAppendRecordMerge(
206833  SessionBuffer *pBuf,            /* Buffer to append to */
206834  int nCol,                       /* Number of columns in each record */
206835  u8 *a1, int n1,                 /* Record 1 */
206836  u8 *a2, int n2,                 /* Record 2 */
206837  int *pRc                        /* IN/OUT: error code */
206838){
206839  sessionBufferGrow(pBuf, n1+n2, pRc);
206840  if( *pRc==SQLITE_OK ){
206841    int i;
206842    u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
206843    for(i=0; i<nCol; i++){
206844      int nn1 = sessionSerialLen(a1);
206845      int nn2 = sessionSerialLen(a2);
206846      if( *a1==0 || *a1==0xFF ){
206847        memcpy(pOut, a2, nn2);
206848        pOut += nn2;
206849      }else{
206850        memcpy(pOut, a1, nn1);
206851        pOut += nn1;
206852      }
206853      a1 += nn1;
206854      a2 += nn2;
206855    }
206856
206857    pBuf->nBuf = pOut-pBuf->aBuf;
206858    assert( pBuf->nBuf<=pBuf->nAlloc );
206859  }
206860}
206861
206862/*
206863** This function is called when rebasing a local UPDATE change against one
206864** or more remote UPDATE changes. The aRec/nRec buffer contains the current
206865** old.* and new.* records for the change. The rebase buffer (a single
206866** record) is in aChange/nChange. The rebased change is appended to buffer
206867** pBuf.
206868**
206869** Rebasing the UPDATE involves:
206870**
206871**   * Removing any changes to fields for which the corresponding field
206872**     in the rebase buffer is set to "replaced" (type 0xFF). If this
206873**     means the UPDATE change updates no fields, nothing is appended
206874**     to the output buffer.
206875**
206876**   * For each field modified by the local change for which the
206877**     corresponding field in the rebase buffer is not "undefined" (0x00)
206878**     or "replaced" (0xFF), the old.* value is replaced by the value
206879**     in the rebase buffer.
206880*/
206881static void sessionAppendPartialUpdate(
206882  SessionBuffer *pBuf,            /* Append record here */
206883  sqlite3_changeset_iter *pIter,  /* Iterator pointed at local change */
206884  u8 *aRec, int nRec,             /* Local change */
206885  u8 *aChange, int nChange,       /* Record to rebase against */
206886  int *pRc                        /* IN/OUT: Return Code */
206887){
206888  sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
206889  if( *pRc==SQLITE_OK ){
206890    int bData = 0;
206891    u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
206892    int i;
206893    u8 *a1 = aRec;
206894    u8 *a2 = aChange;
206895
206896    *pOut++ = SQLITE_UPDATE;
206897    *pOut++ = pIter->bIndirect;
206898    for(i=0; i<pIter->nCol; i++){
206899      int n1 = sessionSerialLen(a1);
206900      int n2 = sessionSerialLen(a2);
206901      if( pIter->abPK[i] || a2[0]==0 ){
206902        if( !pIter->abPK[i] ) bData = 1;
206903        memcpy(pOut, a1, n1);
206904        pOut += n1;
206905      }else if( a2[0]!=0xFF ){
206906        bData = 1;
206907        memcpy(pOut, a2, n2);
206908        pOut += n2;
206909      }else{
206910        *pOut++ = '\0';
206911      }
206912      a1 += n1;
206913      a2 += n2;
206914    }
206915    if( bData ){
206916      a2 = aChange;
206917      for(i=0; i<pIter->nCol; i++){
206918        int n1 = sessionSerialLen(a1);
206919        int n2 = sessionSerialLen(a2);
206920        if( pIter->abPK[i] || a2[0]!=0xFF ){
206921          memcpy(pOut, a1, n1);
206922          pOut += n1;
206923        }else{
206924          *pOut++ = '\0';
206925        }
206926        a1 += n1;
206927        a2 += n2;
206928      }
206929      pBuf->nBuf = (pOut - pBuf->aBuf);
206930    }
206931  }
206932}
206933
206934/*
206935** pIter is configured to iterate through a changeset. This function rebases
206936** that changeset according to the current configuration of the rebaser
206937** object passed as the first argument. If no error occurs and argument xOutput
206938** is not NULL, then the changeset is returned to the caller by invoking
206939** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
206940** then (*ppOut) is set to point to a buffer containing the rebased changeset
206941** before this function returns. In this case (*pnOut) is set to the size of
206942** the buffer in bytes.  It is the responsibility of the caller to eventually
206943** free the (*ppOut) buffer using sqlite3_free().
206944**
206945** If an error occurs, an SQLite error code is returned. If ppOut and
206946** pnOut are not NULL, then the two output parameters are set to 0 before
206947** returning.
206948*/
206949static int sessionRebase(
206950  sqlite3_rebaser *p,             /* Rebaser hash table */
206951  sqlite3_changeset_iter *pIter,  /* Input data */
206952  int (*xOutput)(void *pOut, const void *pData, int nData),
206953  void *pOut,                     /* Context for xOutput callback */
206954  int *pnOut,                     /* OUT: Number of bytes in output changeset */
206955  void **ppOut                    /* OUT: Inverse of pChangeset */
206956){
206957  int rc = SQLITE_OK;
206958  u8 *aRec = 0;
206959  int nRec = 0;
206960  int bNew = 0;
206961  SessionTable *pTab = 0;
206962  SessionBuffer sOut = {0,0,0};
206963
206964  while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
206965    SessionChange *pChange = 0;
206966    int bDone = 0;
206967
206968    if( bNew ){
206969      const char *zTab = pIter->zTab;
206970      for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
206971        if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
206972      }
206973      bNew = 0;
206974
206975      /* A patchset may not be rebased */
206976      if( pIter->bPatchset ){
206977        rc = SQLITE_ERROR;
206978      }
206979
206980      /* Append a table header to the output for this new table */
206981      sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
206982      sessionAppendVarint(&sOut, pIter->nCol, &rc);
206983      sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
206984      sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
206985    }
206986
206987    if( pTab && rc==SQLITE_OK ){
206988      int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
206989
206990      for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
206991        if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
206992          break;
206993        }
206994      }
206995    }
206996
206997    if( pChange ){
206998      assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT );
206999      switch( pIter->op ){
207000        case SQLITE_INSERT:
207001          if( pChange->op==SQLITE_INSERT ){
207002            bDone = 1;
207003            if( pChange->bIndirect==0 ){
207004              sessionAppendByte(&sOut, SQLITE_UPDATE, &rc);
207005              sessionAppendByte(&sOut, pIter->bIndirect, &rc);
207006              sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
207007              sessionAppendBlob(&sOut, aRec, nRec, &rc);
207008            }
207009          }
207010          break;
207011
207012        case SQLITE_UPDATE:
207013          bDone = 1;
207014          if( pChange->op==SQLITE_DELETE ){
207015            if( pChange->bIndirect==0 ){
207016              u8 *pCsr = aRec;
207017              sessionSkipRecord(&pCsr, pIter->nCol);
207018              sessionAppendByte(&sOut, SQLITE_INSERT, &rc);
207019              sessionAppendByte(&sOut, pIter->bIndirect, &rc);
207020              sessionAppendRecordMerge(&sOut, pIter->nCol,
207021                  pCsr, nRec-(pCsr-aRec),
207022                  pChange->aRecord, pChange->nRecord, &rc
207023              );
207024            }
207025          }else{
207026            sessionAppendPartialUpdate(&sOut, pIter,
207027                aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
207028            );
207029          }
207030          break;
207031
207032        default:
207033          assert( pIter->op==SQLITE_DELETE );
207034          bDone = 1;
207035          if( pChange->op==SQLITE_INSERT ){
207036            sessionAppendByte(&sOut, SQLITE_DELETE, &rc);
207037            sessionAppendByte(&sOut, pIter->bIndirect, &rc);
207038            sessionAppendRecordMerge(&sOut, pIter->nCol,
207039                pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
207040            );
207041          }
207042          break;
207043      }
207044    }
207045
207046    if( bDone==0 ){
207047      sessionAppendByte(&sOut, pIter->op, &rc);
207048      sessionAppendByte(&sOut, pIter->bIndirect, &rc);
207049      sessionAppendBlob(&sOut, aRec, nRec, &rc);
207050    }
207051    if( rc==SQLITE_OK && xOutput && sOut.nBuf>sessions_strm_chunk_size ){
207052      rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
207053      sOut.nBuf = 0;
207054    }
207055    if( rc ) break;
207056  }
207057
207058  if( rc!=SQLITE_OK ){
207059    sqlite3_free(sOut.aBuf);
207060    memset(&sOut, 0, sizeof(sOut));
207061  }
207062
207063  if( rc==SQLITE_OK ){
207064    if( xOutput ){
207065      if( sOut.nBuf>0 ){
207066        rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
207067      }
207068    }else{
207069      *ppOut = (void*)sOut.aBuf;
207070      *pnOut = sOut.nBuf;
207071      sOut.aBuf = 0;
207072    }
207073  }
207074  sqlite3_free(sOut.aBuf);
207075  return rc;
207076}
207077
207078/*
207079** Create a new rebaser object.
207080*/
207081SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
207082  int rc = SQLITE_OK;
207083  sqlite3_rebaser *pNew;
207084
207085  pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
207086  if( pNew==0 ){
207087    rc = SQLITE_NOMEM;
207088  }else{
207089    memset(pNew, 0, sizeof(sqlite3_rebaser));
207090  }
207091  *ppNew = pNew;
207092  return rc;
207093}
207094
207095/*
207096** Call this one or more times to configure a rebaser.
207097*/
207098SQLITE_API int sqlite3rebaser_configure(
207099  sqlite3_rebaser *p,
207100  int nRebase, const void *pRebase
207101){
207102  sqlite3_changeset_iter *pIter = 0;   /* Iterator opened on pData/nData */
207103  int rc;                              /* Return code */
207104  rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
207105  if( rc==SQLITE_OK ){
207106    rc = sessionChangesetToHash(pIter, &p->grp, 1);
207107  }
207108  sqlite3changeset_finalize(pIter);
207109  return rc;
207110}
207111
207112/*
207113** Rebase a changeset according to current rebaser configuration
207114*/
207115SQLITE_API int sqlite3rebaser_rebase(
207116  sqlite3_rebaser *p,
207117  int nIn, const void *pIn,
207118  int *pnOut, void **ppOut
207119){
207120  sqlite3_changeset_iter *pIter = 0;   /* Iterator to skip through input */
207121  int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
207122
207123  if( rc==SQLITE_OK ){
207124    rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
207125    sqlite3changeset_finalize(pIter);
207126  }
207127
207128  return rc;
207129}
207130
207131/*
207132** Rebase a changeset according to current rebaser configuration
207133*/
207134SQLITE_API int sqlite3rebaser_rebase_strm(
207135  sqlite3_rebaser *p,
207136  int (*xInput)(void *pIn, void *pData, int *pnData),
207137  void *pIn,
207138  int (*xOutput)(void *pOut, const void *pData, int nData),
207139  void *pOut
207140){
207141  sqlite3_changeset_iter *pIter = 0;   /* Iterator to skip through input */
207142  int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
207143
207144  if( rc==SQLITE_OK ){
207145    rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
207146    sqlite3changeset_finalize(pIter);
207147  }
207148
207149  return rc;
207150}
207151
207152/*
207153** Destroy a rebaser object
207154*/
207155SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
207156  if( p ){
207157    sessionDeleteTable(p->grp.pList);
207158    sqlite3_free(p);
207159  }
207160}
207161
207162/*
207163** Global configuration
207164*/
207165SQLITE_API int sqlite3session_config(int op, void *pArg){
207166  int rc = SQLITE_OK;
207167  switch( op ){
207168    case SQLITE_SESSION_CONFIG_STRMSIZE: {
207169      int *pInt = (int*)pArg;
207170      if( *pInt>0 ){
207171        sessions_strm_chunk_size = *pInt;
207172      }
207173      *pInt = sessions_strm_chunk_size;
207174      break;
207175    }
207176    default:
207177      rc = SQLITE_MISUSE;
207178      break;
207179  }
207180  return rc;
207181}
207182
207183#endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
207184
207185/************** End of sqlite3session.c **************************************/
207186/************** Begin file fts5.c ********************************************/
207187
207188
207189#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5)
207190
207191#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
207192# define NDEBUG 1
207193#endif
207194#if defined(NDEBUG) && defined(SQLITE_DEBUG)
207195# undef NDEBUG
207196#endif
207197
207198/*
207199** 2014 May 31
207200**
207201** The author disclaims copyright to this source code.  In place of
207202** a legal notice, here is a blessing:
207203**
207204**    May you do good and not evil.
207205**    May you find forgiveness for yourself and forgive others.
207206**    May you share freely, never taking more than you give.
207207**
207208******************************************************************************
207209**
207210** Interfaces to extend FTS5. Using the interfaces defined in this file,
207211** FTS5 may be extended with:
207212**
207213**     * custom tokenizers, and
207214**     * custom auxiliary functions.
207215*/
207216
207217
207218#ifndef _FTS5_H
207219#define _FTS5_H
207220
207221/* #include "sqlite3.h" */
207222
207223#if 0
207224extern "C" {
207225#endif
207226
207227/*************************************************************************
207228** CUSTOM AUXILIARY FUNCTIONS
207229**
207230** Virtual table implementations may overload SQL functions by implementing
207231** the sqlite3_module.xFindFunction() method.
207232*/
207233
207234typedef struct Fts5ExtensionApi Fts5ExtensionApi;
207235typedef struct Fts5Context Fts5Context;
207236typedef struct Fts5PhraseIter Fts5PhraseIter;
207237
207238typedef void (*fts5_extension_function)(
207239  const Fts5ExtensionApi *pApi,   /* API offered by current FTS version */
207240  Fts5Context *pFts,              /* First arg to pass to pApi functions */
207241  sqlite3_context *pCtx,          /* Context for returning result/error */
207242  int nVal,                       /* Number of values in apVal[] array */
207243  sqlite3_value **apVal           /* Array of trailing arguments */
207244);
207245
207246struct Fts5PhraseIter {
207247  const unsigned char *a;
207248  const unsigned char *b;
207249};
207250
207251/*
207252** EXTENSION API FUNCTIONS
207253**
207254** xUserData(pFts):
207255**   Return a copy of the context pointer the extension function was
207256**   registered with.
207257**
207258** xColumnTotalSize(pFts, iCol, pnToken):
207259**   If parameter iCol is less than zero, set output variable *pnToken
207260**   to the total number of tokens in the FTS5 table. Or, if iCol is
207261**   non-negative but less than the number of columns in the table, return
207262**   the total number of tokens in column iCol, considering all rows in
207263**   the FTS5 table.
207264**
207265**   If parameter iCol is greater than or equal to the number of columns
207266**   in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
207267**   an OOM condition or IO error), an appropriate SQLite error code is
207268**   returned.
207269**
207270** xColumnCount(pFts):
207271**   Return the number of columns in the table.
207272**
207273** xColumnSize(pFts, iCol, pnToken):
207274**   If parameter iCol is less than zero, set output variable *pnToken
207275**   to the total number of tokens in the current row. Or, if iCol is
207276**   non-negative but less than the number of columns in the table, set
207277**   *pnToken to the number of tokens in column iCol of the current row.
207278**
207279**   If parameter iCol is greater than or equal to the number of columns
207280**   in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
207281**   an OOM condition or IO error), an appropriate SQLite error code is
207282**   returned.
207283**
207284**   This function may be quite inefficient if used with an FTS5 table
207285**   created with the "columnsize=0" option.
207286**
207287** xColumnText:
207288**   This function attempts to retrieve the text of column iCol of the
207289**   current document. If successful, (*pz) is set to point to a buffer
207290**   containing the text in utf-8 encoding, (*pn) is set to the size in bytes
207291**   (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
207292**   if an error occurs, an SQLite error code is returned and the final values
207293**   of (*pz) and (*pn) are undefined.
207294**
207295** xPhraseCount:
207296**   Returns the number of phrases in the current query expression.
207297**
207298** xPhraseSize:
207299**   Returns the number of tokens in phrase iPhrase of the query. Phrases
207300**   are numbered starting from zero.
207301**
207302** xInstCount:
207303**   Set *pnInst to the total number of occurrences of all phrases within
207304**   the query within the current row. Return SQLITE_OK if successful, or
207305**   an error code (i.e. SQLITE_NOMEM) if an error occurs.
207306**
207307**   This API can be quite slow if used with an FTS5 table created with the
207308**   "detail=none" or "detail=column" option. If the FTS5 table is created
207309**   with either "detail=none" or "detail=column" and "content=" option
207310**   (i.e. if it is a contentless table), then this API always returns 0.
207311**
207312** xInst:
207313**   Query for the details of phrase match iIdx within the current row.
207314**   Phrase matches are numbered starting from zero, so the iIdx argument
207315**   should be greater than or equal to zero and smaller than the value
207316**   output by xInstCount().
207317**
207318**   Usually, output parameter *piPhrase is set to the phrase number, *piCol
207319**   to the column in which it occurs and *piOff the token offset of the
207320**   first token of the phrase. Returns SQLITE_OK if successful, or an error
207321**   code (i.e. SQLITE_NOMEM) if an error occurs.
207322**
207323**   This API can be quite slow if used with an FTS5 table created with the
207324**   "detail=none" or "detail=column" option.
207325**
207326** xRowid:
207327**   Returns the rowid of the current row.
207328**
207329** xTokenize:
207330**   Tokenize text using the tokenizer belonging to the FTS5 table.
207331**
207332** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
207333**   This API function is used to query the FTS table for phrase iPhrase
207334**   of the current query. Specifically, a query equivalent to:
207335**
207336**       ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
207337**
207338**   with $p set to a phrase equivalent to the phrase iPhrase of the
207339**   current query is executed. Any column filter that applies to
207340**   phrase iPhrase of the current query is included in $p. For each
207341**   row visited, the callback function passed as the fourth argument
207342**   is invoked. The context and API objects passed to the callback
207343**   function may be used to access the properties of each matched row.
207344**   Invoking Api.xUserData() returns a copy of the pointer passed as
207345**   the third argument to pUserData.
207346**
207347**   If the callback function returns any value other than SQLITE_OK, the
207348**   query is abandoned and the xQueryPhrase function returns immediately.
207349**   If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
207350**   Otherwise, the error code is propagated upwards.
207351**
207352**   If the query runs to completion without incident, SQLITE_OK is returned.
207353**   Or, if some error occurs before the query completes or is aborted by
207354**   the callback, an SQLite error code is returned.
207355**
207356**
207357** xSetAuxdata(pFts5, pAux, xDelete)
207358**
207359**   Save the pointer passed as the second argument as the extension function's
207360**   "auxiliary data". The pointer may then be retrieved by the current or any
207361**   future invocation of the same fts5 extension function made as part of
207362**   the same MATCH query using the xGetAuxdata() API.
207363**
207364**   Each extension function is allocated a single auxiliary data slot for
207365**   each FTS query (MATCH expression). If the extension function is invoked
207366**   more than once for a single FTS query, then all invocations share a
207367**   single auxiliary data context.
207368**
207369**   If there is already an auxiliary data pointer when this function is
207370**   invoked, then it is replaced by the new pointer. If an xDelete callback
207371**   was specified along with the original pointer, it is invoked at this
207372**   point.
207373**
207374**   The xDelete callback, if one is specified, is also invoked on the
207375**   auxiliary data pointer after the FTS5 query has finished.
207376**
207377**   If an error (e.g. an OOM condition) occurs within this function,
207378**   the auxiliary data is set to NULL and an error code returned. If the
207379**   xDelete parameter was not NULL, it is invoked on the auxiliary data
207380**   pointer before returning.
207381**
207382**
207383** xGetAuxdata(pFts5, bClear)
207384**
207385**   Returns the current auxiliary data pointer for the fts5 extension
207386**   function. See the xSetAuxdata() method for details.
207387**
207388**   If the bClear argument is non-zero, then the auxiliary data is cleared
207389**   (set to NULL) before this function returns. In this case the xDelete,
207390**   if any, is not invoked.
207391**
207392**
207393** xRowCount(pFts5, pnRow)
207394**
207395**   This function is used to retrieve the total number of rows in the table.
207396**   In other words, the same value that would be returned by:
207397**
207398**        SELECT count(*) FROM ftstable;
207399**
207400** xPhraseFirst()
207401**   This function is used, along with type Fts5PhraseIter and the xPhraseNext
207402**   method, to iterate through all instances of a single query phrase within
207403**   the current row. This is the same information as is accessible via the
207404**   xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
207405**   to use, this API may be faster under some circumstances. To iterate
207406**   through instances of phrase iPhrase, use the following code:
207407**
207408**       Fts5PhraseIter iter;
207409**       int iCol, iOff;
207410**       for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
207411**           iCol>=0;
207412**           pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
207413**       ){
207414**         // An instance of phrase iPhrase at offset iOff of column iCol
207415**       }
207416**
207417**   The Fts5PhraseIter structure is defined above. Applications should not
207418**   modify this structure directly - it should only be used as shown above
207419**   with the xPhraseFirst() and xPhraseNext() API methods (and by
207420**   xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
207421**
207422**   This API can be quite slow if used with an FTS5 table created with the
207423**   "detail=none" or "detail=column" option. If the FTS5 table is created
207424**   with either "detail=none" or "detail=column" and "content=" option
207425**   (i.e. if it is a contentless table), then this API always iterates
207426**   through an empty set (all calls to xPhraseFirst() set iCol to -1).
207427**
207428** xPhraseNext()
207429**   See xPhraseFirst above.
207430**
207431** xPhraseFirstColumn()
207432**   This function and xPhraseNextColumn() are similar to the xPhraseFirst()
207433**   and xPhraseNext() APIs described above. The difference is that instead
207434**   of iterating through all instances of a phrase in the current row, these
207435**   APIs are used to iterate through the set of columns in the current row
207436**   that contain one or more instances of a specified phrase. For example:
207437**
207438**       Fts5PhraseIter iter;
207439**       int iCol;
207440**       for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
207441**           iCol>=0;
207442**           pApi->xPhraseNextColumn(pFts, &iter, &iCol)
207443**       ){
207444**         // Column iCol contains at least one instance of phrase iPhrase
207445**       }
207446**
207447**   This API can be quite slow if used with an FTS5 table created with the
207448**   "detail=none" option. If the FTS5 table is created with either
207449**   "detail=none" "content=" option (i.e. if it is a contentless table),
207450**   then this API always iterates through an empty set (all calls to
207451**   xPhraseFirstColumn() set iCol to -1).
207452**
207453**   The information accessed using this API and its companion
207454**   xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
207455**   (or xInst/xInstCount). The chief advantage of this API is that it is
207456**   significantly more efficient than those alternatives when used with
207457**   "detail=column" tables.
207458**
207459** xPhraseNextColumn()
207460**   See xPhraseFirstColumn above.
207461*/
207462struct Fts5ExtensionApi {
207463  int iVersion;                   /* Currently always set to 3 */
207464
207465  void *(*xUserData)(Fts5Context*);
207466
207467  int (*xColumnCount)(Fts5Context*);
207468  int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
207469  int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
207470
207471  int (*xTokenize)(Fts5Context*,
207472    const char *pText, int nText, /* Text to tokenize */
207473    void *pCtx,                   /* Context passed to xToken() */
207474    int (*xToken)(void*, int, const char*, int, int, int)       /* Callback */
207475  );
207476
207477  int (*xPhraseCount)(Fts5Context*);
207478  int (*xPhraseSize)(Fts5Context*, int iPhrase);
207479
207480  int (*xInstCount)(Fts5Context*, int *pnInst);
207481  int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
207482
207483  sqlite3_int64 (*xRowid)(Fts5Context*);
207484  int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
207485  int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
207486
207487  int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
207488    int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
207489  );
207490  int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
207491  void *(*xGetAuxdata)(Fts5Context*, int bClear);
207492
207493  int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
207494  void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
207495
207496  int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
207497  void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
207498};
207499
207500/*
207501** CUSTOM AUXILIARY FUNCTIONS
207502*************************************************************************/
207503
207504/*************************************************************************
207505** CUSTOM TOKENIZERS
207506**
207507** Applications may also register custom tokenizer types. A tokenizer
207508** is registered by providing fts5 with a populated instance of the
207509** following structure. All structure methods must be defined, setting
207510** any member of the fts5_tokenizer struct to NULL leads to undefined
207511** behaviour. The structure methods are expected to function as follows:
207512**
207513** xCreate:
207514**   This function is used to allocate and initialize a tokenizer instance.
207515**   A tokenizer instance is required to actually tokenize text.
207516**
207517**   The first argument passed to this function is a copy of the (void*)
207518**   pointer provided by the application when the fts5_tokenizer object
207519**   was registered with FTS5 (the third argument to xCreateTokenizer()).
207520**   The second and third arguments are an array of nul-terminated strings
207521**   containing the tokenizer arguments, if any, specified following the
207522**   tokenizer name as part of the CREATE VIRTUAL TABLE statement used
207523**   to create the FTS5 table.
207524**
207525**   The final argument is an output variable. If successful, (*ppOut)
207526**   should be set to point to the new tokenizer handle and SQLITE_OK
207527**   returned. If an error occurs, some value other than SQLITE_OK should
207528**   be returned. In this case, fts5 assumes that the final value of *ppOut
207529**   is undefined.
207530**
207531** xDelete:
207532**   This function is invoked to delete a tokenizer handle previously
207533**   allocated using xCreate(). Fts5 guarantees that this function will
207534**   be invoked exactly once for each successful call to xCreate().
207535**
207536** xTokenize:
207537**   This function is expected to tokenize the nText byte string indicated
207538**   by argument pText. pText may or may not be nul-terminated. The first
207539**   argument passed to this function is a pointer to an Fts5Tokenizer object
207540**   returned by an earlier call to xCreate().
207541**
207542**   The second argument indicates the reason that FTS5 is requesting
207543**   tokenization of the supplied text. This is always one of the following
207544**   four values:
207545**
207546**   <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
207547**            or removed from the FTS table. The tokenizer is being invoked to
207548**            determine the set of tokens to add to (or delete from) the
207549**            FTS index.
207550**
207551**       <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
207552**            against the FTS index. The tokenizer is being called to tokenize
207553**            a bareword or quoted string specified as part of the query.
207554**
207555**       <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
207556**            FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
207557**            followed by a "*" character, indicating that the last token
207558**            returned by the tokenizer will be treated as a token prefix.
207559**
207560**       <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
207561**            satisfy an fts5_api.xTokenize() request made by an auxiliary
207562**            function. Or an fts5_api.xColumnSize() request made by the same
207563**            on a columnsize=0 database.
207564**   </ul>
207565**
207566**   For each token in the input string, the supplied callback xToken() must
207567**   be invoked. The first argument to it should be a copy of the pointer
207568**   passed as the second argument to xTokenize(). The third and fourth
207569**   arguments are a pointer to a buffer containing the token text, and the
207570**   size of the token in bytes. The 4th and 5th arguments are the byte offsets
207571**   of the first byte of and first byte immediately following the text from
207572**   which the token is derived within the input.
207573**
207574**   The second argument passed to the xToken() callback ("tflags") should
207575**   normally be set to 0. The exception is if the tokenizer supports
207576**   synonyms. In this case see the discussion below for details.
207577**
207578**   FTS5 assumes the xToken() callback is invoked for each token in the
207579**   order that they occur within the input text.
207580**
207581**   If an xToken() callback returns any value other than SQLITE_OK, then
207582**   the tokenization should be abandoned and the xTokenize() method should
207583**   immediately return a copy of the xToken() return value. Or, if the
207584**   input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
207585**   if an error occurs with the xTokenize() implementation itself, it
207586**   may abandon the tokenization and return any error code other than
207587**   SQLITE_OK or SQLITE_DONE.
207588**
207589** SYNONYM SUPPORT
207590**
207591**   Custom tokenizers may also support synonyms. Consider a case in which a
207592**   user wishes to query for a phrase such as "first place". Using the
207593**   built-in tokenizers, the FTS5 query 'first + place' will match instances
207594**   of "first place" within the document set, but not alternative forms
207595**   such as "1st place". In some applications, it would be better to match
207596**   all instances of "first place" or "1st place" regardless of which form
207597**   the user specified in the MATCH query text.
207598**
207599**   There are several ways to approach this in FTS5:
207600**
207601**   <ol><li> By mapping all synonyms to a single token. In this case, using
207602**            the above example, this means that the tokenizer returns the
207603**            same token for inputs "first" and "1st". Say that token is in
207604**            fact "first", so that when the user inserts the document "I won
207605**            1st place" entries are added to the index for tokens "i", "won",
207606**            "first" and "place". If the user then queries for '1st + place',
207607**            the tokenizer substitutes "first" for "1st" and the query works
207608**            as expected.
207609**
207610**       <li> By querying the index for all synonyms of each query term
207611**            separately. In this case, when tokenizing query text, the
207612**            tokenizer may provide multiple synonyms for a single term
207613**            within the document. FTS5 then queries the index for each
207614**            synonym individually. For example, faced with the query:
207615**
207616**   <codeblock>
207617**     ... MATCH 'first place'</codeblock>
207618**
207619**            the tokenizer offers both "1st" and "first" as synonyms for the
207620**            first token in the MATCH query and FTS5 effectively runs a query
207621**            similar to:
207622**
207623**   <codeblock>
207624**     ... MATCH '(first OR 1st) place'</codeblock>
207625**
207626**            except that, for the purposes of auxiliary functions, the query
207627**            still appears to contain just two phrases - "(first OR 1st)"
207628**            being treated as a single phrase.
207629**
207630**       <li> By adding multiple synonyms for a single term to the FTS index.
207631**            Using this method, when tokenizing document text, the tokenizer
207632**            provides multiple synonyms for each token. So that when a
207633**            document such as "I won first place" is tokenized, entries are
207634**            added to the FTS index for "i", "won", "first", "1st" and
207635**            "place".
207636**
207637**            This way, even if the tokenizer does not provide synonyms
207638**            when tokenizing query text (it should not - to do so would be
207639**            inefficient), it doesn't matter if the user queries for
207640**            'first + place' or '1st + place', as there are entries in the
207641**            FTS index corresponding to both forms of the first token.
207642**   </ol>
207643**
207644**   Whether it is parsing document or query text, any call to xToken that
207645**   specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
207646**   is considered to supply a synonym for the previous token. For example,
207647**   when parsing the document "I won first place", a tokenizer that supports
207648**   synonyms would call xToken() 5 times, as follows:
207649**
207650**   <codeblock>
207651**       xToken(pCtx, 0, "i",                      1,  0,  1);
207652**       xToken(pCtx, 0, "won",                    3,  2,  5);
207653**       xToken(pCtx, 0, "first",                  5,  6, 11);
207654**       xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3,  6, 11);
207655**       xToken(pCtx, 0, "place",                  5, 12, 17);
207656**</codeblock>
207657**
207658**   It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
207659**   xToken() is called. Multiple synonyms may be specified for a single token
207660**   by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
207661**   There is no limit to the number of synonyms that may be provided for a
207662**   single token.
207663**
207664**   In many cases, method (1) above is the best approach. It does not add
207665**   extra data to the FTS index or require FTS5 to query for multiple terms,
207666**   so it is efficient in terms of disk space and query speed. However, it
207667**   does not support prefix queries very well. If, as suggested above, the
207668**   token "first" is substituted for "1st" by the tokenizer, then the query:
207669**
207670**   <codeblock>
207671**     ... MATCH '1s*'</codeblock>
207672**
207673**   will not match documents that contain the token "1st" (as the tokenizer
207674**   will probably not map "1s" to any prefix of "first").
207675**
207676**   For full prefix support, method (3) may be preferred. In this case,
207677**   because the index contains entries for both "first" and "1st", prefix
207678**   queries such as 'fi*' or '1s*' will match correctly. However, because
207679**   extra entries are added to the FTS index, this method uses more space
207680**   within the database.
207681**
207682**   Method (2) offers a midpoint between (1) and (3). Using this method,
207683**   a query such as '1s*' will match documents that contain the literal
207684**   token "1st", but not "first" (assuming the tokenizer is not able to
207685**   provide synonyms for prefixes). However, a non-prefix query like '1st'
207686**   will match against "1st" and "first". This method does not require
207687**   extra disk space, as no extra entries are added to the FTS index.
207688**   On the other hand, it may require more CPU cycles to run MATCH queries,
207689**   as separate queries of the FTS index are required for each synonym.
207690**
207691**   When using methods (2) or (3), it is important that the tokenizer only
207692**   provide synonyms when tokenizing document text (method (2)) or query
207693**   text (method (3)), not both. Doing so will not cause any errors, but is
207694**   inefficient.
207695*/
207696typedef struct Fts5Tokenizer Fts5Tokenizer;
207697typedef struct fts5_tokenizer fts5_tokenizer;
207698struct fts5_tokenizer {
207699  int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
207700  void (*xDelete)(Fts5Tokenizer*);
207701  int (*xTokenize)(Fts5Tokenizer*,
207702      void *pCtx,
207703      int flags,            /* Mask of FTS5_TOKENIZE_* flags */
207704      const char *pText, int nText,
207705      int (*xToken)(
207706        void *pCtx,         /* Copy of 2nd argument to xTokenize() */
207707        int tflags,         /* Mask of FTS5_TOKEN_* flags */
207708        const char *pToken, /* Pointer to buffer containing token */
207709        int nToken,         /* Size of token in bytes */
207710        int iStart,         /* Byte offset of token within input text */
207711        int iEnd            /* Byte offset of end of token within input text */
207712      )
207713  );
207714};
207715
207716/* Flags that may be passed as the third argument to xTokenize() */
207717#define FTS5_TOKENIZE_QUERY     0x0001
207718#define FTS5_TOKENIZE_PREFIX    0x0002
207719#define FTS5_TOKENIZE_DOCUMENT  0x0004
207720#define FTS5_TOKENIZE_AUX       0x0008
207721
207722/* Flags that may be passed by the tokenizer implementation back to FTS5
207723** as the third argument to the supplied xToken callback. */
207724#define FTS5_TOKEN_COLOCATED    0x0001      /* Same position as prev. token */
207725
207726/*
207727** END OF CUSTOM TOKENIZERS
207728*************************************************************************/
207729
207730/*************************************************************************
207731** FTS5 EXTENSION REGISTRATION API
207732*/
207733typedef struct fts5_api fts5_api;
207734struct fts5_api {
207735  int iVersion;                   /* Currently always set to 2 */
207736
207737  /* Create a new tokenizer */
207738  int (*xCreateTokenizer)(
207739    fts5_api *pApi,
207740    const char *zName,
207741    void *pContext,
207742    fts5_tokenizer *pTokenizer,
207743    void (*xDestroy)(void*)
207744  );
207745
207746  /* Find an existing tokenizer */
207747  int (*xFindTokenizer)(
207748    fts5_api *pApi,
207749    const char *zName,
207750    void **ppContext,
207751    fts5_tokenizer *pTokenizer
207752  );
207753
207754  /* Create a new auxiliary function */
207755  int (*xCreateFunction)(
207756    fts5_api *pApi,
207757    const char *zName,
207758    void *pContext,
207759    fts5_extension_function xFunction,
207760    void (*xDestroy)(void*)
207761  );
207762};
207763
207764/*
207765** END OF REGISTRATION API
207766*************************************************************************/
207767
207768#if 0
207769}  /* end of the 'extern "C"' block */
207770#endif
207771
207772#endif /* _FTS5_H */
207773
207774/*
207775** 2014 May 31
207776**
207777** The author disclaims copyright to this source code.  In place of
207778** a legal notice, here is a blessing:
207779**
207780**    May you do good and not evil.
207781**    May you find forgiveness for yourself and forgive others.
207782**    May you share freely, never taking more than you give.
207783**
207784******************************************************************************
207785**
207786*/
207787#ifndef _FTS5INT_H
207788#define _FTS5INT_H
207789
207790/* #include "fts5.h" */
207791/* #include "sqlite3ext.h" */
207792SQLITE_EXTENSION_INIT1
207793
207794/* #include <string.h> */
207795/* #include <assert.h> */
207796
207797#ifndef SQLITE_AMALGAMATION
207798
207799typedef unsigned char  u8;
207800typedef unsigned int   u32;
207801typedef unsigned short u16;
207802typedef short i16;
207803typedef sqlite3_int64 i64;
207804typedef sqlite3_uint64 u64;
207805
207806#ifndef ArraySize
207807# define ArraySize(x) ((int)(sizeof(x) / sizeof(x[0])))
207808#endif
207809
207810#define testcase(x)
207811#define ALWAYS(x) 1
207812#define NEVER(x) 0
207813
207814#define MIN(x,y) (((x) < (y)) ? (x) : (y))
207815#define MAX(x,y) (((x) > (y)) ? (x) : (y))
207816
207817/*
207818** Constants for the largest and smallest possible 64-bit signed integers.
207819*/
207820# define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
207821# define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
207822
207823#endif
207824
207825/* Truncate very long tokens to this many bytes. Hard limit is
207826** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
207827** field that occurs at the start of each leaf page (see fts5_index.c). */
207828#define FTS5_MAX_TOKEN_SIZE 32768
207829
207830/*
207831** Maximum number of prefix indexes on single FTS5 table. This must be
207832** less than 32. If it is set to anything large than that, an #error
207833** directive in fts5_index.c will cause the build to fail.
207834*/
207835#define FTS5_MAX_PREFIX_INDEXES 31
207836
207837/*
207838** Maximum segments permitted in a single index
207839*/
207840#define FTS5_MAX_SEGMENT 2000
207841
207842#define FTS5_DEFAULT_NEARDIST 10
207843#define FTS5_DEFAULT_RANK     "bm25"
207844
207845/* Name of rank and rowid columns */
207846#define FTS5_RANK_NAME "rank"
207847#define FTS5_ROWID_NAME "rowid"
207848
207849#ifdef SQLITE_DEBUG
207850# define FTS5_CORRUPT sqlite3Fts5Corrupt()
207851static int sqlite3Fts5Corrupt(void);
207852#else
207853# define FTS5_CORRUPT SQLITE_CORRUPT_VTAB
207854#endif
207855
207856/*
207857** The assert_nc() macro is similar to the assert() macro, except that it
207858** is used for assert() conditions that are true only if it can be
207859** guranteed that the database is not corrupt.
207860*/
207861#ifdef SQLITE_DEBUG
207862SQLITE_API extern int sqlite3_fts5_may_be_corrupt;
207863# define assert_nc(x) assert(sqlite3_fts5_may_be_corrupt || (x))
207864#else
207865# define assert_nc(x) assert(x)
207866#endif
207867
207868/*
207869** A version of memcmp() that does not cause asan errors if one of the pointer
207870** parameters is NULL and the number of bytes to compare is zero.
207871*/
207872#define fts5Memcmp(s1, s2, n) ((n)==0 ? 0 : memcmp((s1), (s2), (n)))
207873
207874/* Mark a function parameter as unused, to suppress nuisance compiler
207875** warnings. */
207876#ifndef UNUSED_PARAM
207877# define UNUSED_PARAM(X)  (void)(X)
207878#endif
207879
207880#ifndef UNUSED_PARAM2
207881# define UNUSED_PARAM2(X, Y)  (void)(X), (void)(Y)
207882#endif
207883
207884typedef struct Fts5Global Fts5Global;
207885typedef struct Fts5Colset Fts5Colset;
207886
207887/* If a NEAR() clump or phrase may only match a specific set of columns,
207888** then an object of the following type is used to record the set of columns.
207889** Each entry in the aiCol[] array is a column that may be matched.
207890**
207891** This object is used by fts5_expr.c and fts5_index.c.
207892*/
207893struct Fts5Colset {
207894  int nCol;
207895  int aiCol[1];
207896};
207897
207898
207899
207900/**************************************************************************
207901** Interface to code in fts5_config.c. fts5_config.c contains contains code
207902** to parse the arguments passed to the CREATE VIRTUAL TABLE statement.
207903*/
207904
207905typedef struct Fts5Config Fts5Config;
207906
207907/*
207908** An instance of the following structure encodes all information that can
207909** be gleaned from the CREATE VIRTUAL TABLE statement.
207910**
207911** And all information loaded from the %_config table.
207912**
207913** nAutomerge:
207914**   The minimum number of segments that an auto-merge operation should
207915**   attempt to merge together. A value of 1 sets the object to use the
207916**   compile time default. Zero disables auto-merge altogether.
207917**
207918** zContent:
207919**
207920** zContentRowid:
207921**   The value of the content_rowid= option, if one was specified. Or
207922**   the string "rowid" otherwise. This text is not quoted - if it is
207923**   used as part of an SQL statement it needs to be quoted appropriately.
207924**
207925** zContentExprlist:
207926**
207927** pzErrmsg:
207928**   This exists in order to allow the fts5_index.c module to return a
207929**   decent error message if it encounters a file-format version it does
207930**   not understand.
207931**
207932** bColumnsize:
207933**   True if the %_docsize table is created.
207934**
207935** bPrefixIndex:
207936**   This is only used for debugging. If set to false, any prefix indexes
207937**   are ignored. This value is configured using:
207938**
207939**       INSERT INTO tbl(tbl, rank) VALUES('prefix-index', $bPrefixIndex);
207940**
207941*/
207942struct Fts5Config {
207943  sqlite3 *db;                    /* Database handle */
207944  char *zDb;                      /* Database holding FTS index (e.g. "main") */
207945  char *zName;                    /* Name of FTS index */
207946  int nCol;                       /* Number of columns */
207947  char **azCol;                   /* Column names */
207948  u8 *abUnindexed;                /* True for unindexed columns */
207949  int nPrefix;                    /* Number of prefix indexes */
207950  int *aPrefix;                   /* Sizes in bytes of nPrefix prefix indexes */
207951  int eContent;                   /* An FTS5_CONTENT value */
207952  char *zContent;                 /* content table */
207953  char *zContentRowid;            /* "content_rowid=" option value */
207954  int bColumnsize;                /* "columnsize=" option value (dflt==1) */
207955  int eDetail;                    /* FTS5_DETAIL_XXX value */
207956  char *zContentExprlist;
207957  Fts5Tokenizer *pTok;
207958  fts5_tokenizer *pTokApi;
207959  int bLock;                      /* True when table is preparing statement */
207960
207961  /* Values loaded from the %_config table */
207962  int iCookie;                    /* Incremented when %_config is modified */
207963  int pgsz;                       /* Approximate page size used in %_data */
207964  int nAutomerge;                 /* 'automerge' setting */
207965  int nCrisisMerge;               /* Maximum allowed segments per level */
207966  int nUsermerge;                 /* 'usermerge' setting */
207967  int nHashSize;                  /* Bytes of memory for in-memory hash */
207968  char *zRank;                    /* Name of rank function */
207969  char *zRankArgs;                /* Arguments to rank function */
207970
207971  /* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */
207972  char **pzErrmsg;
207973
207974#ifdef SQLITE_DEBUG
207975  int bPrefixIndex;               /* True to use prefix-indexes */
207976#endif
207977};
207978
207979/* Current expected value of %_config table 'version' field */
207980#define FTS5_CURRENT_VERSION 4
207981
207982#define FTS5_CONTENT_NORMAL   0
207983#define FTS5_CONTENT_NONE     1
207984#define FTS5_CONTENT_EXTERNAL 2
207985
207986#define FTS5_DETAIL_FULL    0
207987#define FTS5_DETAIL_NONE    1
207988#define FTS5_DETAIL_COLUMNS 2
207989
207990
207991
207992static int sqlite3Fts5ConfigParse(
207993    Fts5Global*, sqlite3*, int, const char **, Fts5Config**, char**
207994);
207995static void sqlite3Fts5ConfigFree(Fts5Config*);
207996
207997static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig);
207998
207999static int sqlite3Fts5Tokenize(
208000  Fts5Config *pConfig,            /* FTS5 Configuration object */
208001  int flags,                      /* FTS5_TOKENIZE_* flags */
208002  const char *pText, int nText,   /* Text to tokenize */
208003  void *pCtx,                     /* Context passed to xToken() */
208004  int (*xToken)(void*, int, const char*, int, int, int)    /* Callback */
208005);
208006
208007static void sqlite3Fts5Dequote(char *z);
208008
208009/* Load the contents of the %_config table */
208010static int sqlite3Fts5ConfigLoad(Fts5Config*, int);
208011
208012/* Set the value of a single config attribute */
208013static int sqlite3Fts5ConfigSetValue(Fts5Config*, const char*, sqlite3_value*, int*);
208014
208015static int sqlite3Fts5ConfigParseRank(const char*, char**, char**);
208016
208017/*
208018** End of interface to code in fts5_config.c.
208019**************************************************************************/
208020
208021/**************************************************************************
208022** Interface to code in fts5_buffer.c.
208023*/
208024
208025/*
208026** Buffer object for the incremental building of string data.
208027*/
208028typedef struct Fts5Buffer Fts5Buffer;
208029struct Fts5Buffer {
208030  u8 *p;
208031  int n;
208032  int nSpace;
208033};
208034
208035static int sqlite3Fts5BufferSize(int*, Fts5Buffer*, u32);
208036static void sqlite3Fts5BufferAppendVarint(int*, Fts5Buffer*, i64);
208037static void sqlite3Fts5BufferAppendBlob(int*, Fts5Buffer*, u32, const u8*);
208038static void sqlite3Fts5BufferAppendString(int *, Fts5Buffer*, const char*);
208039static void sqlite3Fts5BufferFree(Fts5Buffer*);
208040static void sqlite3Fts5BufferZero(Fts5Buffer*);
208041static void sqlite3Fts5BufferSet(int*, Fts5Buffer*, int, const u8*);
208042static void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...);
208043
208044static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...);
208045
208046#define fts5BufferZero(x)             sqlite3Fts5BufferZero(x)
208047#define fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,c)
208048#define fts5BufferFree(a)             sqlite3Fts5BufferFree(a)
208049#define fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
208050#define fts5BufferSet(a,b,c,d)        sqlite3Fts5BufferSet(a,b,c,d)
208051
208052#define fts5BufferGrow(pRc,pBuf,nn) ( \
208053  (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace) ? 0 : \
208054    sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n) \
208055)
208056
208057/* Write and decode big-endian 32-bit integer values */
208058static void sqlite3Fts5Put32(u8*, int);
208059static int sqlite3Fts5Get32(const u8*);
208060
208061#define FTS5_POS2COLUMN(iPos) (int)(iPos >> 32)
208062#define FTS5_POS2OFFSET(iPos) (int)(iPos & 0x7FFFFFFF)
208063
208064typedef struct Fts5PoslistReader Fts5PoslistReader;
208065struct Fts5PoslistReader {
208066  /* Variables used only by sqlite3Fts5PoslistIterXXX() functions. */
208067  const u8 *a;                    /* Position list to iterate through */
208068  int n;                          /* Size of buffer at a[] in bytes */
208069  int i;                          /* Current offset in a[] */
208070
208071  u8 bFlag;                       /* For client use (any custom purpose) */
208072
208073  /* Output variables */
208074  u8 bEof;                        /* Set to true at EOF */
208075  i64 iPos;                       /* (iCol<<32) + iPos */
208076};
208077static int sqlite3Fts5PoslistReaderInit(
208078  const u8 *a, int n,             /* Poslist buffer to iterate through */
208079  Fts5PoslistReader *pIter        /* Iterator object to initialize */
208080);
208081static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader*);
208082
208083typedef struct Fts5PoslistWriter Fts5PoslistWriter;
208084struct Fts5PoslistWriter {
208085  i64 iPrev;
208086};
208087static int sqlite3Fts5PoslistWriterAppend(Fts5Buffer*, Fts5PoslistWriter*, i64);
208088static void sqlite3Fts5PoslistSafeAppend(Fts5Buffer*, i64*, i64);
208089
208090static int sqlite3Fts5PoslistNext64(
208091  const u8 *a, int n,             /* Buffer containing poslist */
208092  int *pi,                        /* IN/OUT: Offset within a[] */
208093  i64 *piOff                      /* IN/OUT: Current offset */
208094);
208095
208096/* Malloc utility */
208097static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte);
208098static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn);
208099
208100/* Character set tests (like isspace(), isalpha() etc.) */
208101static int sqlite3Fts5IsBareword(char t);
208102
208103
208104/* Bucket of terms object used by the integrity-check in offsets=0 mode. */
208105typedef struct Fts5Termset Fts5Termset;
208106static int sqlite3Fts5TermsetNew(Fts5Termset**);
208107static int sqlite3Fts5TermsetAdd(Fts5Termset*, int, const char*, int, int *pbPresent);
208108static void sqlite3Fts5TermsetFree(Fts5Termset*);
208109
208110/*
208111** End of interface to code in fts5_buffer.c.
208112**************************************************************************/
208113
208114/**************************************************************************
208115** Interface to code in fts5_index.c. fts5_index.c contains contains code
208116** to access the data stored in the %_data table.
208117*/
208118
208119typedef struct Fts5Index Fts5Index;
208120typedef struct Fts5IndexIter Fts5IndexIter;
208121
208122struct Fts5IndexIter {
208123  i64 iRowid;
208124  const u8 *pData;
208125  int nData;
208126  u8 bEof;
208127};
208128
208129#define sqlite3Fts5IterEof(x) ((x)->bEof)
208130
208131/*
208132** Values used as part of the flags argument passed to IndexQuery().
208133*/
208134#define FTS5INDEX_QUERY_PREFIX     0x0001   /* Prefix query */
208135#define FTS5INDEX_QUERY_DESC       0x0002   /* Docs in descending rowid order */
208136#define FTS5INDEX_QUERY_TEST_NOIDX 0x0004   /* Do not use prefix index */
208137#define FTS5INDEX_QUERY_SCAN       0x0008   /* Scan query (fts5vocab) */
208138
208139/* The following are used internally by the fts5_index.c module. They are
208140** defined here only to make it easier to avoid clashes with the flags
208141** above. */
208142#define FTS5INDEX_QUERY_SKIPEMPTY  0x0010
208143#define FTS5INDEX_QUERY_NOOUTPUT   0x0020
208144
208145/*
208146** Create/destroy an Fts5Index object.
208147*/
208148static int sqlite3Fts5IndexOpen(Fts5Config *pConfig, int bCreate, Fts5Index**, char**);
208149static int sqlite3Fts5IndexClose(Fts5Index *p);
208150
208151/*
208152** Return a simple checksum value based on the arguments.
208153*/
208154static u64 sqlite3Fts5IndexEntryCksum(
208155  i64 iRowid,
208156  int iCol,
208157  int iPos,
208158  int iIdx,
208159  const char *pTerm,
208160  int nTerm
208161);
208162
208163/*
208164** Argument p points to a buffer containing utf-8 text that is n bytes in
208165** size. Return the number of bytes in the nChar character prefix of the
208166** buffer, or 0 if there are less than nChar characters in total.
208167*/
208168static int sqlite3Fts5IndexCharlenToBytelen(
208169  const char *p,
208170  int nByte,
208171  int nChar
208172);
208173
208174/*
208175** Open a new iterator to iterate though all rowids that match the
208176** specified token or token prefix.
208177*/
208178static int sqlite3Fts5IndexQuery(
208179  Fts5Index *p,                   /* FTS index to query */
208180  const char *pToken, int nToken, /* Token (or prefix) to query for */
208181  int flags,                      /* Mask of FTS5INDEX_QUERY_X flags */
208182  Fts5Colset *pColset,            /* Match these columns only */
208183  Fts5IndexIter **ppIter          /* OUT: New iterator object */
208184);
208185
208186/*
208187** The various operations on open token or token prefix iterators opened
208188** using sqlite3Fts5IndexQuery().
208189*/
208190static int sqlite3Fts5IterNext(Fts5IndexIter*);
208191static int sqlite3Fts5IterNextFrom(Fts5IndexIter*, i64 iMatch);
208192
208193/*
208194** Close an iterator opened by sqlite3Fts5IndexQuery().
208195*/
208196static void sqlite3Fts5IterClose(Fts5IndexIter*);
208197
208198/*
208199** Close the reader blob handle, if it is open.
208200*/
208201static void sqlite3Fts5IndexCloseReader(Fts5Index*);
208202
208203/*
208204** This interface is used by the fts5vocab module.
208205*/
208206static const char *sqlite3Fts5IterTerm(Fts5IndexIter*, int*);
208207static int sqlite3Fts5IterNextScan(Fts5IndexIter*);
208208
208209
208210/*
208211** Insert or remove data to or from the index. Each time a document is
208212** added to or removed from the index, this function is called one or more
208213** times.
208214**
208215** For an insert, it must be called once for each token in the new document.
208216** If the operation is a delete, it must be called (at least) once for each
208217** unique token in the document with an iCol value less than zero. The iPos
208218** argument is ignored for a delete.
208219*/
208220static int sqlite3Fts5IndexWrite(
208221  Fts5Index *p,                   /* Index to write to */
208222  int iCol,                       /* Column token appears in (-ve -> delete) */
208223  int iPos,                       /* Position of token within column */
208224  const char *pToken, int nToken  /* Token to add or remove to or from index */
208225);
208226
208227/*
208228** Indicate that subsequent calls to sqlite3Fts5IndexWrite() pertain to
208229** document iDocid.
208230*/
208231static int sqlite3Fts5IndexBeginWrite(
208232  Fts5Index *p,                   /* Index to write to */
208233  int bDelete,                    /* True if current operation is a delete */
208234  i64 iDocid                      /* Docid to add or remove data from */
208235);
208236
208237/*
208238** Flush any data stored in the in-memory hash tables to the database.
208239** Also close any open blob handles.
208240*/
208241static int sqlite3Fts5IndexSync(Fts5Index *p);
208242
208243/*
208244** Discard any data stored in the in-memory hash tables. Do not write it
208245** to the database. Additionally, assume that the contents of the %_data
208246** table may have changed on disk. So any in-memory caches of %_data
208247** records must be invalidated.
208248*/
208249static int sqlite3Fts5IndexRollback(Fts5Index *p);
208250
208251/*
208252** Get or set the "averages" values.
208253*/
208254static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize);
208255static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8*, int);
208256
208257/*
208258** Functions called by the storage module as part of integrity-check.
208259*/
208260static int sqlite3Fts5IndexIntegrityCheck(Fts5Index*, u64 cksum);
208261
208262/*
208263** Called during virtual module initialization to register UDF
208264** fts5_decode() with SQLite
208265*/
208266static int sqlite3Fts5IndexInit(sqlite3*);
208267
208268static int sqlite3Fts5IndexSetCookie(Fts5Index*, int);
208269
208270/*
208271** Return the total number of entries read from the %_data table by
208272** this connection since it was created.
208273*/
208274static int sqlite3Fts5IndexReads(Fts5Index *p);
208275
208276static int sqlite3Fts5IndexReinit(Fts5Index *p);
208277static int sqlite3Fts5IndexOptimize(Fts5Index *p);
208278static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge);
208279static int sqlite3Fts5IndexReset(Fts5Index *p);
208280
208281static int sqlite3Fts5IndexLoadConfig(Fts5Index *p);
208282
208283/*
208284** End of interface to code in fts5_index.c.
208285**************************************************************************/
208286
208287/**************************************************************************
208288** Interface to code in fts5_varint.c.
208289*/
208290static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v);
208291static int sqlite3Fts5GetVarintLen(u32 iVal);
208292static u8 sqlite3Fts5GetVarint(const unsigned char*, u64*);
208293static int sqlite3Fts5PutVarint(unsigned char *p, u64 v);
208294
208295#define fts5GetVarint32(a,b) sqlite3Fts5GetVarint32(a,(u32*)&b)
208296#define fts5GetVarint    sqlite3Fts5GetVarint
208297
208298#define fts5FastGetVarint32(a, iOff, nVal) {      \
208299  nVal = (a)[iOff++];                             \
208300  if( nVal & 0x80 ){                              \
208301    iOff--;                                       \
208302    iOff += fts5GetVarint32(&(a)[iOff], nVal);    \
208303  }                                               \
208304}
208305
208306
208307/*
208308** End of interface to code in fts5_varint.c.
208309**************************************************************************/
208310
208311
208312/**************************************************************************
208313** Interface to code in fts5_main.c.
208314*/
208315
208316/*
208317** Virtual-table object.
208318*/
208319typedef struct Fts5Table Fts5Table;
208320struct Fts5Table {
208321  sqlite3_vtab base;              /* Base class used by SQLite core */
208322  Fts5Config *pConfig;            /* Virtual table configuration */
208323  Fts5Index *pIndex;              /* Full-text index */
208324};
208325
208326static int sqlite3Fts5GetTokenizer(
208327  Fts5Global*,
208328  const char **azArg,
208329  int nArg,
208330  Fts5Tokenizer**,
208331  fts5_tokenizer**,
208332  char **pzErr
208333);
208334
208335static Fts5Table *sqlite3Fts5TableFromCsrid(Fts5Global*, i64);
208336
208337static int sqlite3Fts5FlushToDisk(Fts5Table*);
208338
208339/*
208340** End of interface to code in fts5.c.
208341**************************************************************************/
208342
208343/**************************************************************************
208344** Interface to code in fts5_hash.c.
208345*/
208346typedef struct Fts5Hash Fts5Hash;
208347
208348/*
208349** Create a hash table, free a hash table.
208350*/
208351static int sqlite3Fts5HashNew(Fts5Config*, Fts5Hash**, int *pnSize);
208352static void sqlite3Fts5HashFree(Fts5Hash*);
208353
208354static int sqlite3Fts5HashWrite(
208355  Fts5Hash*,
208356  i64 iRowid,                     /* Rowid for this entry */
208357  int iCol,                       /* Column token appears in (-ve -> delete) */
208358  int iPos,                       /* Position of token within column */
208359  char bByte,
208360  const char *pToken, int nToken  /* Token to add or remove to or from index */
208361);
208362
208363/*
208364** Empty (but do not delete) a hash table.
208365*/
208366static void sqlite3Fts5HashClear(Fts5Hash*);
208367
208368static int sqlite3Fts5HashQuery(
208369  Fts5Hash*,                      /* Hash table to query */
208370  int nPre,
208371  const char *pTerm, int nTerm,   /* Query term */
208372  void **ppObj,                   /* OUT: Pointer to doclist for pTerm */
208373  int *pnDoclist                  /* OUT: Size of doclist in bytes */
208374);
208375
208376static int sqlite3Fts5HashScanInit(
208377  Fts5Hash*,                      /* Hash table to query */
208378  const char *pTerm, int nTerm    /* Query prefix */
208379);
208380static void sqlite3Fts5HashScanNext(Fts5Hash*);
208381static int sqlite3Fts5HashScanEof(Fts5Hash*);
208382static void sqlite3Fts5HashScanEntry(Fts5Hash *,
208383  const char **pzTerm,            /* OUT: term (nul-terminated) */
208384  const u8 **ppDoclist,           /* OUT: pointer to doclist */
208385  int *pnDoclist                  /* OUT: size of doclist in bytes */
208386);
208387
208388
208389/*
208390** End of interface to code in fts5_hash.c.
208391**************************************************************************/
208392
208393/**************************************************************************
208394** Interface to code in fts5_storage.c. fts5_storage.c contains contains
208395** code to access the data stored in the %_content and %_docsize tables.
208396*/
208397
208398#define FTS5_STMT_SCAN_ASC  0     /* SELECT rowid, * FROM ... ORDER BY 1 ASC */
208399#define FTS5_STMT_SCAN_DESC 1     /* SELECT rowid, * FROM ... ORDER BY 1 DESC */
208400#define FTS5_STMT_LOOKUP    2     /* SELECT rowid, * FROM ... WHERE rowid=? */
208401
208402typedef struct Fts5Storage Fts5Storage;
208403
208404static int sqlite3Fts5StorageOpen(Fts5Config*, Fts5Index*, int, Fts5Storage**, char**);
208405static int sqlite3Fts5StorageClose(Fts5Storage *p);
208406static int sqlite3Fts5StorageRename(Fts5Storage*, const char *zName);
208407
208408static int sqlite3Fts5DropAll(Fts5Config*);
208409static int sqlite3Fts5CreateTable(Fts5Config*, const char*, const char*, int, char **);
208410
208411static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64, sqlite3_value**);
208412static int sqlite3Fts5StorageContentInsert(Fts5Storage *p, sqlite3_value**, i64*);
208413static int sqlite3Fts5StorageIndexInsert(Fts5Storage *p, sqlite3_value**, i64);
208414
208415static int sqlite3Fts5StorageIntegrity(Fts5Storage *p);
208416
208417static int sqlite3Fts5StorageStmt(Fts5Storage *p, int eStmt, sqlite3_stmt**, char**);
208418static void sqlite3Fts5StorageStmtRelease(Fts5Storage *p, int eStmt, sqlite3_stmt*);
208419
208420static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol);
208421static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnAvg);
208422static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow);
208423
208424static int sqlite3Fts5StorageSync(Fts5Storage *p);
208425static int sqlite3Fts5StorageRollback(Fts5Storage *p);
208426
208427static int sqlite3Fts5StorageConfigValue(
208428    Fts5Storage *p, const char*, sqlite3_value*, int
208429);
208430
208431static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p);
208432static int sqlite3Fts5StorageRebuild(Fts5Storage *p);
208433static int sqlite3Fts5StorageOptimize(Fts5Storage *p);
208434static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge);
208435static int sqlite3Fts5StorageReset(Fts5Storage *p);
208436
208437/*
208438** End of interface to code in fts5_storage.c.
208439**************************************************************************/
208440
208441
208442/**************************************************************************
208443** Interface to code in fts5_expr.c.
208444*/
208445typedef struct Fts5Expr Fts5Expr;
208446typedef struct Fts5ExprNode Fts5ExprNode;
208447typedef struct Fts5Parse Fts5Parse;
208448typedef struct Fts5Token Fts5Token;
208449typedef struct Fts5ExprPhrase Fts5ExprPhrase;
208450typedef struct Fts5ExprNearset Fts5ExprNearset;
208451
208452struct Fts5Token {
208453  const char *p;                  /* Token text (not NULL terminated) */
208454  int n;                          /* Size of buffer p in bytes */
208455};
208456
208457/* Parse a MATCH expression. */
208458static int sqlite3Fts5ExprNew(
208459  Fts5Config *pConfig,
208460  int iCol,                       /* Column on LHS of MATCH operator */
208461  const char *zExpr,
208462  Fts5Expr **ppNew,
208463  char **pzErr
208464);
208465
208466/*
208467** for(rc = sqlite3Fts5ExprFirst(pExpr, pIdx, bDesc);
208468**     rc==SQLITE_OK && 0==sqlite3Fts5ExprEof(pExpr);
208469**     rc = sqlite3Fts5ExprNext(pExpr)
208470** ){
208471**   // The document with rowid iRowid matches the expression!
208472**   i64 iRowid = sqlite3Fts5ExprRowid(pExpr);
208473** }
208474*/
208475static int sqlite3Fts5ExprFirst(Fts5Expr*, Fts5Index *pIdx, i64 iMin, int bDesc);
208476static int sqlite3Fts5ExprNext(Fts5Expr*, i64 iMax);
208477static int sqlite3Fts5ExprEof(Fts5Expr*);
208478static i64 sqlite3Fts5ExprRowid(Fts5Expr*);
208479
208480static void sqlite3Fts5ExprFree(Fts5Expr*);
208481static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2);
208482
208483/* Called during startup to register a UDF with SQLite */
208484static int sqlite3Fts5ExprInit(Fts5Global*, sqlite3*);
208485
208486static int sqlite3Fts5ExprPhraseCount(Fts5Expr*);
208487static int sqlite3Fts5ExprPhraseSize(Fts5Expr*, int iPhrase);
208488static int sqlite3Fts5ExprPoslist(Fts5Expr*, int, const u8 **);
208489
208490typedef struct Fts5PoslistPopulator Fts5PoslistPopulator;
208491static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr*, int);
208492static int sqlite3Fts5ExprPopulatePoslists(
208493    Fts5Config*, Fts5Expr*, Fts5PoslistPopulator*, int, const char*, int
208494);
208495static void sqlite3Fts5ExprCheckPoslists(Fts5Expr*, i64);
208496
208497static int sqlite3Fts5ExprClonePhrase(Fts5Expr*, int, Fts5Expr**);
208498
208499static int sqlite3Fts5ExprPhraseCollist(Fts5Expr *, int, const u8 **, int *);
208500
208501/*******************************************
208502** The fts5_expr.c API above this point is used by the other hand-written
208503** C code in this module. The interfaces below this point are called by
208504** the parser code in fts5parse.y.  */
208505
208506static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...);
208507
208508static Fts5ExprNode *sqlite3Fts5ParseNode(
208509  Fts5Parse *pParse,
208510  int eType,
208511  Fts5ExprNode *pLeft,
208512  Fts5ExprNode *pRight,
208513  Fts5ExprNearset *pNear
208514);
208515
208516static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
208517  Fts5Parse *pParse,
208518  Fts5ExprNode *pLeft,
208519  Fts5ExprNode *pRight
208520);
208521
208522static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
208523  Fts5Parse *pParse,
208524  Fts5ExprPhrase *pPhrase,
208525  Fts5Token *pToken,
208526  int bPrefix
208527);
208528
208529static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase*);
208530
208531static Fts5ExprNearset *sqlite3Fts5ParseNearset(
208532  Fts5Parse*,
208533  Fts5ExprNearset*,
208534  Fts5ExprPhrase*
208535);
208536
208537static Fts5Colset *sqlite3Fts5ParseColset(
208538  Fts5Parse*,
208539  Fts5Colset*,
208540  Fts5Token *
208541);
208542
208543static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase*);
208544static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset*);
208545static void sqlite3Fts5ParseNodeFree(Fts5ExprNode*);
208546
208547static void sqlite3Fts5ParseSetDistance(Fts5Parse*, Fts5ExprNearset*, Fts5Token*);
208548static void sqlite3Fts5ParseSetColset(Fts5Parse*, Fts5ExprNode*, Fts5Colset*);
208549static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse*, Fts5Colset*);
208550static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p);
208551static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token*);
208552
208553/*
208554** End of interface to code in fts5_expr.c.
208555**************************************************************************/
208556
208557
208558
208559/**************************************************************************
208560** Interface to code in fts5_aux.c.
208561*/
208562
208563static int sqlite3Fts5AuxInit(fts5_api*);
208564/*
208565** End of interface to code in fts5_aux.c.
208566**************************************************************************/
208567
208568/**************************************************************************
208569** Interface to code in fts5_tokenizer.c.
208570*/
208571
208572static int sqlite3Fts5TokenizerInit(fts5_api*);
208573/*
208574** End of interface to code in fts5_tokenizer.c.
208575**************************************************************************/
208576
208577/**************************************************************************
208578** Interface to code in fts5_vocab.c.
208579*/
208580
208581static int sqlite3Fts5VocabInit(Fts5Global*, sqlite3*);
208582
208583/*
208584** End of interface to code in fts5_vocab.c.
208585**************************************************************************/
208586
208587
208588/**************************************************************************
208589** Interface to automatically generated code in fts5_unicode2.c.
208590*/
208591static int sqlite3Fts5UnicodeIsdiacritic(int c);
208592static int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic);
208593
208594static int sqlite3Fts5UnicodeCatParse(const char*, u8*);
208595static int sqlite3Fts5UnicodeCategory(u32 iCode);
208596static void sqlite3Fts5UnicodeAscii(u8*, u8*);
208597/*
208598** End of interface to code in fts5_unicode2.c.
208599**************************************************************************/
208600
208601#endif
208602
208603#define FTS5_OR                               1
208604#define FTS5_AND                              2
208605#define FTS5_NOT                              3
208606#define FTS5_TERM                             4
208607#define FTS5_COLON                            5
208608#define FTS5_MINUS                            6
208609#define FTS5_LCP                              7
208610#define FTS5_RCP                              8
208611#define FTS5_STRING                           9
208612#define FTS5_LP                              10
208613#define FTS5_RP                              11
208614#define FTS5_CARET                           12
208615#define FTS5_COMMA                           13
208616#define FTS5_PLUS                            14
208617#define FTS5_STAR                            15
208618
208619/*
208620** 2000-05-29
208621**
208622** The author disclaims copyright to this source code.  In place of
208623** a legal notice, here is a blessing:
208624**
208625**    May you do good and not evil.
208626**    May you find forgiveness for yourself and forgive others.
208627**    May you share freely, never taking more than you give.
208628**
208629*************************************************************************
208630** Driver template for the LEMON parser generator.
208631**
208632** The "lemon" program processes an LALR(1) input grammar file, then uses
208633** this template to construct a parser.  The "lemon" program inserts text
208634** at each "%%" line.  Also, any "P-a-r-s-e" identifer prefix (without the
208635** interstitial "-" characters) contained in this template is changed into
208636** the value of the %name directive from the grammar.  Otherwise, the content
208637** of this template is copied straight through into the generate parser
208638** source file.
208639**
208640** The following is the concatenation of all %include directives from the
208641** input grammar file:
208642*/
208643/* #include <stdio.h> */
208644/* #include <assert.h> */
208645/************ Begin %include sections from the grammar ************************/
208646
208647/* #include "fts5Int.h" */
208648/* #include "fts5parse.h" */
208649
208650/*
208651** Disable all error recovery processing in the parser push-down
208652** automaton.
208653*/
208654#define fts5YYNOERRORRECOVERY 1
208655
208656/*
208657** Make fts5yytestcase() the same as testcase()
208658*/
208659#define fts5yytestcase(X) testcase(X)
208660
208661/*
208662** Indicate that sqlite3ParserFree() will never be called with a null
208663** pointer.
208664*/
208665#define fts5YYPARSEFREENOTNULL 1
208666
208667/*
208668** Alternative datatype for the argument to the malloc() routine passed
208669** into sqlite3ParserAlloc().  The default is size_t.
208670*/
208671#define fts5YYMALLOCARGTYPE  u64
208672
208673/**************** End of %include directives **********************************/
208674/* These constants specify the various numeric values for terminal symbols
208675** in a format understandable to "makeheaders".  This section is blank unless
208676** "lemon" is run with the "-m" command-line option.
208677***************** Begin makeheaders token definitions *************************/
208678/**************** End makeheaders token definitions ***************************/
208679
208680/* The next sections is a series of control #defines.
208681** various aspects of the generated parser.
208682**    fts5YYCODETYPE         is the data type used to store the integer codes
208683**                       that represent terminal and non-terminal symbols.
208684**                       "unsigned char" is used if there are fewer than
208685**                       256 symbols.  Larger types otherwise.
208686**    fts5YYNOCODE           is a number of type fts5YYCODETYPE that is not used for
208687**                       any terminal or nonterminal symbol.
208688**    fts5YYFALLBACK         If defined, this indicates that one or more tokens
208689**                       (also known as: "terminal symbols") have fall-back
208690**                       values which should be used if the original symbol
208691**                       would not parse.  This permits keywords to sometimes
208692**                       be used as identifiers, for example.
208693**    fts5YYACTIONTYPE       is the data type used for "action codes" - numbers
208694**                       that indicate what to do in response to the next
208695**                       token.
208696**    sqlite3Fts5ParserFTS5TOKENTYPE     is the data type used for minor type for terminal
208697**                       symbols.  Background: A "minor type" is a semantic
208698**                       value associated with a terminal or non-terminal
208699**                       symbols.  For example, for an "ID" terminal symbol,
208700**                       the minor type might be the name of the identifier.
208701**                       Each non-terminal can have a different minor type.
208702**                       Terminal symbols all have the same minor type, though.
208703**                       This macros defines the minor type for terminal
208704**                       symbols.
208705**    fts5YYMINORTYPE        is the data type used for all minor types.
208706**                       This is typically a union of many types, one of
208707**                       which is sqlite3Fts5ParserFTS5TOKENTYPE.  The entry in the union
208708**                       for terminal symbols is called "fts5yy0".
208709**    fts5YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
208710**                       zero the stack is dynamically sized using realloc()
208711**    sqlite3Fts5ParserARG_SDECL     A static variable declaration for the %extra_argument
208712**    sqlite3Fts5ParserARG_PDECL     A parameter declaration for the %extra_argument
208713**    sqlite3Fts5ParserARG_PARAM     Code to pass %extra_argument as a subroutine parameter
208714**    sqlite3Fts5ParserARG_STORE     Code to store %extra_argument into fts5yypParser
208715**    sqlite3Fts5ParserARG_FETCH     Code to extract %extra_argument from fts5yypParser
208716**    sqlite3Fts5ParserCTX_*         As sqlite3Fts5ParserARG_ except for %extra_context
208717**    fts5YYERRORSYMBOL      is the code number of the error symbol.  If not
208718**                       defined, then do no error processing.
208719**    fts5YYNSTATE           the combined number of states.
208720**    fts5YYNRULE            the number of rules in the grammar
208721**    fts5YYNFTS5TOKEN           Number of terminal symbols
208722**    fts5YY_MAX_SHIFT       Maximum value for shift actions
208723**    fts5YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
208724**    fts5YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
208725**    fts5YY_ERROR_ACTION    The fts5yy_action[] code for syntax error
208726**    fts5YY_ACCEPT_ACTION   The fts5yy_action[] code for accept
208727**    fts5YY_NO_ACTION       The fts5yy_action[] code for no-op
208728**    fts5YY_MIN_REDUCE      Minimum value for reduce actions
208729**    fts5YY_MAX_REDUCE      Maximum value for reduce actions
208730*/
208731#ifndef INTERFACE
208732# define INTERFACE 1
208733#endif
208734/************* Begin control #defines *****************************************/
208735#define fts5YYCODETYPE unsigned char
208736#define fts5YYNOCODE 27
208737#define fts5YYACTIONTYPE unsigned char
208738#define sqlite3Fts5ParserFTS5TOKENTYPE Fts5Token
208739typedef union {
208740  int fts5yyinit;
208741  sqlite3Fts5ParserFTS5TOKENTYPE fts5yy0;
208742  int fts5yy4;
208743  Fts5Colset* fts5yy11;
208744  Fts5ExprNode* fts5yy24;
208745  Fts5ExprNearset* fts5yy46;
208746  Fts5ExprPhrase* fts5yy53;
208747} fts5YYMINORTYPE;
208748#ifndef fts5YYSTACKDEPTH
208749#define fts5YYSTACKDEPTH 100
208750#endif
208751#define sqlite3Fts5ParserARG_SDECL Fts5Parse *pParse;
208752#define sqlite3Fts5ParserARG_PDECL ,Fts5Parse *pParse
208753#define sqlite3Fts5ParserARG_PARAM ,pParse
208754#define sqlite3Fts5ParserARG_FETCH Fts5Parse *pParse=fts5yypParser->pParse;
208755#define sqlite3Fts5ParserARG_STORE fts5yypParser->pParse=pParse;
208756#define sqlite3Fts5ParserCTX_SDECL
208757#define sqlite3Fts5ParserCTX_PDECL
208758#define sqlite3Fts5ParserCTX_PARAM
208759#define sqlite3Fts5ParserCTX_FETCH
208760#define sqlite3Fts5ParserCTX_STORE
208761#define fts5YYNSTATE             35
208762#define fts5YYNRULE              28
208763#define fts5YYNRULE_WITH_ACTION  28
208764#define fts5YYNFTS5TOKEN             16
208765#define fts5YY_MAX_SHIFT         34
208766#define fts5YY_MIN_SHIFTREDUCE   52
208767#define fts5YY_MAX_SHIFTREDUCE   79
208768#define fts5YY_ERROR_ACTION      80
208769#define fts5YY_ACCEPT_ACTION     81
208770#define fts5YY_NO_ACTION         82
208771#define fts5YY_MIN_REDUCE        83
208772#define fts5YY_MAX_REDUCE        110
208773/************* End control #defines *******************************************/
208774#define fts5YY_NLOOKAHEAD ((int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])))
208775
208776/* Define the fts5yytestcase() macro to be a no-op if is not already defined
208777** otherwise.
208778**
208779** Applications can choose to define fts5yytestcase() in the %include section
208780** to a macro that can assist in verifying code coverage.  For production
208781** code the fts5yytestcase() macro should be turned off.  But it is useful
208782** for testing.
208783*/
208784#ifndef fts5yytestcase
208785# define fts5yytestcase(X)
208786#endif
208787
208788
208789/* Next are the tables used to determine what action to take based on the
208790** current state and lookahead token.  These tables are used to implement
208791** functions that take a state number and lookahead value and return an
208792** action integer.
208793**
208794** Suppose the action integer is N.  Then the action is determined as
208795** follows
208796**
208797**   0 <= N <= fts5YY_MAX_SHIFT             Shift N.  That is, push the lookahead
208798**                                      token onto the stack and goto state N.
208799**
208800**   N between fts5YY_MIN_SHIFTREDUCE       Shift to an arbitrary state then
208801**     and fts5YY_MAX_SHIFTREDUCE           reduce by rule N-fts5YY_MIN_SHIFTREDUCE.
208802**
208803**   N == fts5YY_ERROR_ACTION               A syntax error has occurred.
208804**
208805**   N == fts5YY_ACCEPT_ACTION              The parser accepts its input.
208806**
208807**   N == fts5YY_NO_ACTION                  No such action.  Denotes unused
208808**                                      slots in the fts5yy_action[] table.
208809**
208810**   N between fts5YY_MIN_REDUCE            Reduce by rule N-fts5YY_MIN_REDUCE
208811**     and fts5YY_MAX_REDUCE
208812**
208813** The action table is constructed as a single large table named fts5yy_action[].
208814** Given state S and lookahead X, the action is computed as either:
208815**
208816**    (A)   N = fts5yy_action[ fts5yy_shift_ofst[S] + X ]
208817**    (B)   N = fts5yy_default[S]
208818**
208819** The (A) formula is preferred.  The B formula is used instead if
208820** fts5yy_lookahead[fts5yy_shift_ofst[S]+X] is not equal to X.
208821**
208822** The formulas above are for computing the action when the lookahead is
208823** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
208824** a reduce action) then the fts5yy_reduce_ofst[] array is used in place of
208825** the fts5yy_shift_ofst[] array.
208826**
208827** The following are the tables generated in this section:
208828**
208829**  fts5yy_action[]        A single table containing all actions.
208830**  fts5yy_lookahead[]     A table containing the lookahead for each entry in
208831**                     fts5yy_action.  Used to detect hash collisions.
208832**  fts5yy_shift_ofst[]    For each state, the offset into fts5yy_action for
208833**                     shifting terminals.
208834**  fts5yy_reduce_ofst[]   For each state, the offset into fts5yy_action for
208835**                     shifting non-terminals after a reduce.
208836**  fts5yy_default[]       Default action for each state.
208837**
208838*********** Begin parsing tables **********************************************/
208839#define fts5YY_ACTTAB_COUNT (105)
208840static const fts5YYACTIONTYPE fts5yy_action[] = {
208841 /*     0 */    81,   20,   96,    6,   28,   99,   98,   26,   26,   18,
208842 /*    10 */    96,    6,   28,   17,   98,   56,   26,   19,   96,    6,
208843 /*    20 */    28,   14,   98,   14,   26,   31,   92,   96,    6,   28,
208844 /*    30 */   108,   98,   25,   26,   21,   96,    6,   28,   78,   98,
208845 /*    40 */    58,   26,   29,   96,    6,   28,  107,   98,   22,   26,
208846 /*    50 */    24,   16,   12,   11,    1,   13,   13,   24,   16,   23,
208847 /*    60 */    11,   33,   34,   13,   97,    8,   27,   32,   98,    7,
208848 /*    70 */    26,    3,    4,    5,    3,    4,    5,    3,   83,    4,
208849 /*    80 */     5,    3,   63,    5,    3,   62,   12,    2,   86,   13,
208850 /*    90 */     9,   30,   10,   10,   54,   57,   75,   78,   78,   53,
208851 /*   100 */    57,   15,   82,   82,   71,
208852};
208853static const fts5YYCODETYPE fts5yy_lookahead[] = {
208854 /*     0 */    16,   17,   18,   19,   20,   22,   22,   24,   24,   17,
208855 /*    10 */    18,   19,   20,    7,   22,    9,   24,   17,   18,   19,
208856 /*    20 */    20,    9,   22,    9,   24,   13,   17,   18,   19,   20,
208857 /*    30 */    26,   22,   24,   24,   17,   18,   19,   20,   15,   22,
208858 /*    40 */     9,   24,   17,   18,   19,   20,   26,   22,   21,   24,
208859 /*    50 */     6,    7,    9,    9,   10,   12,   12,    6,    7,   21,
208860 /*    60 */     9,   24,   25,   12,   18,    5,   20,   14,   22,    5,
208861 /*    70 */    24,    3,    1,    2,    3,    1,    2,    3,    0,    1,
208862 /*    80 */     2,    3,   11,    2,    3,   11,    9,   10,    5,   12,
208863 /*    90 */    23,   24,   10,   10,    8,    9,    9,   15,   15,    8,
208864 /*   100 */     9,    9,   27,   27,   11,   27,   27,   27,   27,   27,
208865 /*   110 */    27,   27,   27,   27,   27,   27,   27,   27,   27,   27,
208866 /*   120 */    27,
208867};
208868#define fts5YY_SHIFT_COUNT    (34)
208869#define fts5YY_SHIFT_MIN      (0)
208870#define fts5YY_SHIFT_MAX      (93)
208871static const unsigned char fts5yy_shift_ofst[] = {
208872 /*     0 */    44,   44,   44,   44,   44,   44,   51,   77,   43,   12,
208873 /*    10 */    14,   83,   82,   14,   23,   23,   31,   31,   71,   74,
208874 /*    20 */    78,   81,   86,   91,    6,   53,   53,   60,   64,   68,
208875 /*    30 */    53,   87,   92,   53,   93,
208876};
208877#define fts5YY_REDUCE_COUNT (17)
208878#define fts5YY_REDUCE_MIN   (-17)
208879#define fts5YY_REDUCE_MAX   (67)
208880static const signed char fts5yy_reduce_ofst[] = {
208881 /*     0 */   -16,   -8,    0,    9,   17,   25,   46,  -17,  -17,   37,
208882 /*    10 */    67,    4,    4,    8,    4,   20,   27,   38,
208883};
208884static const fts5YYACTIONTYPE fts5yy_default[] = {
208885 /*     0 */    80,   80,   80,   80,   80,   80,   95,   80,   80,  105,
208886 /*    10 */    80,  110,  110,   80,  110,  110,   80,   80,   80,   80,
208887 /*    20 */    80,   91,   80,   80,   80,  101,  100,   80,   80,   90,
208888 /*    30 */   103,   80,   80,  104,   80,
208889};
208890/********** End of lemon-generated parsing tables *****************************/
208891
208892/* The next table maps tokens (terminal symbols) into fallback tokens.
208893** If a construct like the following:
208894**
208895**      %fallback ID X Y Z.
208896**
208897** appears in the grammar, then ID becomes a fallback token for X, Y,
208898** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
208899** but it does not parse, the type of the token is changed to ID and
208900** the parse is retried before an error is thrown.
208901**
208902** This feature can be used, for example, to cause some keywords in a language
208903** to revert to identifiers if they keyword does not apply in the context where
208904** it appears.
208905*/
208906#ifdef fts5YYFALLBACK
208907static const fts5YYCODETYPE fts5yyFallback[] = {
208908};
208909#endif /* fts5YYFALLBACK */
208910
208911/* The following structure represents a single element of the
208912** parser's stack.  Information stored includes:
208913**
208914**   +  The state number for the parser at this level of the stack.
208915**
208916**   +  The value of the token stored at this level of the stack.
208917**      (In other words, the "major" token.)
208918**
208919**   +  The semantic value stored at this level of the stack.  This is
208920**      the information used by the action routines in the grammar.
208921**      It is sometimes called the "minor" token.
208922**
208923** After the "shift" half of a SHIFTREDUCE action, the stateno field
208924** actually contains the reduce action for the second half of the
208925** SHIFTREDUCE.
208926*/
208927struct fts5yyStackEntry {
208928  fts5YYACTIONTYPE stateno;  /* The state-number, or reduce action in SHIFTREDUCE */
208929  fts5YYCODETYPE major;      /* The major token value.  This is the code
208930                         ** number for the token at this stack level */
208931  fts5YYMINORTYPE minor;     /* The user-supplied minor token value.  This
208932                         ** is the value of the token  */
208933};
208934typedef struct fts5yyStackEntry fts5yyStackEntry;
208935
208936/* The state of the parser is completely contained in an instance of
208937** the following structure */
208938struct fts5yyParser {
208939  fts5yyStackEntry *fts5yytos;          /* Pointer to top element of the stack */
208940#ifdef fts5YYTRACKMAXSTACKDEPTH
208941  int fts5yyhwm;                    /* High-water mark of the stack */
208942#endif
208943#ifndef fts5YYNOERRORRECOVERY
208944  int fts5yyerrcnt;                 /* Shifts left before out of the error */
208945#endif
208946  sqlite3Fts5ParserARG_SDECL                /* A place to hold %extra_argument */
208947  sqlite3Fts5ParserCTX_SDECL                /* A place to hold %extra_context */
208948#if fts5YYSTACKDEPTH<=0
208949  int fts5yystksz;                  /* Current side of the stack */
208950  fts5yyStackEntry *fts5yystack;        /* The parser's stack */
208951  fts5yyStackEntry fts5yystk0;          /* First stack entry */
208952#else
208953  fts5yyStackEntry fts5yystack[fts5YYSTACKDEPTH];  /* The parser's stack */
208954  fts5yyStackEntry *fts5yystackEnd;            /* Last entry in the stack */
208955#endif
208956};
208957typedef struct fts5yyParser fts5yyParser;
208958
208959#ifndef NDEBUG
208960/* #include <stdio.h> */
208961static FILE *fts5yyTraceFILE = 0;
208962static char *fts5yyTracePrompt = 0;
208963#endif /* NDEBUG */
208964
208965#ifndef NDEBUG
208966/*
208967** Turn parser tracing on by giving a stream to which to write the trace
208968** and a prompt to preface each trace message.  Tracing is turned off
208969** by making either argument NULL
208970**
208971** Inputs:
208972** <ul>
208973** <li> A FILE* to which trace output should be written.
208974**      If NULL, then tracing is turned off.
208975** <li> A prefix string written at the beginning of every
208976**      line of trace output.  If NULL, then tracing is
208977**      turned off.
208978** </ul>
208979**
208980** Outputs:
208981** None.
208982*/
208983static void sqlite3Fts5ParserTrace(FILE *TraceFILE, char *zTracePrompt){
208984  fts5yyTraceFILE = TraceFILE;
208985  fts5yyTracePrompt = zTracePrompt;
208986  if( fts5yyTraceFILE==0 ) fts5yyTracePrompt = 0;
208987  else if( fts5yyTracePrompt==0 ) fts5yyTraceFILE = 0;
208988}
208989#endif /* NDEBUG */
208990
208991#if defined(fts5YYCOVERAGE) || !defined(NDEBUG)
208992/* For tracing shifts, the names of all terminals and nonterminals
208993** are required.  The following table supplies these names */
208994static const char *const fts5yyTokenName[] = {
208995  /*    0 */ "$",
208996  /*    1 */ "OR",
208997  /*    2 */ "AND",
208998  /*    3 */ "NOT",
208999  /*    4 */ "TERM",
209000  /*    5 */ "COLON",
209001  /*    6 */ "MINUS",
209002  /*    7 */ "LCP",
209003  /*    8 */ "RCP",
209004  /*    9 */ "STRING",
209005  /*   10 */ "LP",
209006  /*   11 */ "RP",
209007  /*   12 */ "CARET",
209008  /*   13 */ "COMMA",
209009  /*   14 */ "PLUS",
209010  /*   15 */ "STAR",
209011  /*   16 */ "input",
209012  /*   17 */ "expr",
209013  /*   18 */ "cnearset",
209014  /*   19 */ "exprlist",
209015  /*   20 */ "colset",
209016  /*   21 */ "colsetlist",
209017  /*   22 */ "nearset",
209018  /*   23 */ "nearphrases",
209019  /*   24 */ "phrase",
209020  /*   25 */ "neardist_opt",
209021  /*   26 */ "star_opt",
209022};
209023#endif /* defined(fts5YYCOVERAGE) || !defined(NDEBUG) */
209024
209025#ifndef NDEBUG
209026/* For tracing reduce actions, the names of all rules are required.
209027*/
209028static const char *const fts5yyRuleName[] = {
209029 /*   0 */ "input ::= expr",
209030 /*   1 */ "colset ::= MINUS LCP colsetlist RCP",
209031 /*   2 */ "colset ::= LCP colsetlist RCP",
209032 /*   3 */ "colset ::= STRING",
209033 /*   4 */ "colset ::= MINUS STRING",
209034 /*   5 */ "colsetlist ::= colsetlist STRING",
209035 /*   6 */ "colsetlist ::= STRING",
209036 /*   7 */ "expr ::= expr AND expr",
209037 /*   8 */ "expr ::= expr OR expr",
209038 /*   9 */ "expr ::= expr NOT expr",
209039 /*  10 */ "expr ::= colset COLON LP expr RP",
209040 /*  11 */ "expr ::= LP expr RP",
209041 /*  12 */ "expr ::= exprlist",
209042 /*  13 */ "exprlist ::= cnearset",
209043 /*  14 */ "exprlist ::= exprlist cnearset",
209044 /*  15 */ "cnearset ::= nearset",
209045 /*  16 */ "cnearset ::= colset COLON nearset",
209046 /*  17 */ "nearset ::= phrase",
209047 /*  18 */ "nearset ::= CARET phrase",
209048 /*  19 */ "nearset ::= STRING LP nearphrases neardist_opt RP",
209049 /*  20 */ "nearphrases ::= phrase",
209050 /*  21 */ "nearphrases ::= nearphrases phrase",
209051 /*  22 */ "neardist_opt ::=",
209052 /*  23 */ "neardist_opt ::= COMMA STRING",
209053 /*  24 */ "phrase ::= phrase PLUS STRING star_opt",
209054 /*  25 */ "phrase ::= STRING star_opt",
209055 /*  26 */ "star_opt ::= STAR",
209056 /*  27 */ "star_opt ::=",
209057};
209058#endif /* NDEBUG */
209059
209060
209061#if fts5YYSTACKDEPTH<=0
209062/*
209063** Try to increase the size of the parser stack.  Return the number
209064** of errors.  Return 0 on success.
209065*/
209066static int fts5yyGrowStack(fts5yyParser *p){
209067  int newSize;
209068  int idx;
209069  fts5yyStackEntry *pNew;
209070
209071  newSize = p->fts5yystksz*2 + 100;
209072  idx = p->fts5yytos ? (int)(p->fts5yytos - p->fts5yystack) : 0;
209073  if( p->fts5yystack==&p->fts5yystk0 ){
209074    pNew = malloc(newSize*sizeof(pNew[0]));
209075    if( pNew ) pNew[0] = p->fts5yystk0;
209076  }else{
209077    pNew = realloc(p->fts5yystack, newSize*sizeof(pNew[0]));
209078  }
209079  if( pNew ){
209080    p->fts5yystack = pNew;
209081    p->fts5yytos = &p->fts5yystack[idx];
209082#ifndef NDEBUG
209083    if( fts5yyTraceFILE ){
209084      fprintf(fts5yyTraceFILE,"%sStack grows from %d to %d entries.\n",
209085              fts5yyTracePrompt, p->fts5yystksz, newSize);
209086    }
209087#endif
209088    p->fts5yystksz = newSize;
209089  }
209090  return pNew==0;
209091}
209092#endif
209093
209094/* Datatype of the argument to the memory allocated passed as the
209095** second argument to sqlite3Fts5ParserAlloc() below.  This can be changed by
209096** putting an appropriate #define in the %include section of the input
209097** grammar.
209098*/
209099#ifndef fts5YYMALLOCARGTYPE
209100# define fts5YYMALLOCARGTYPE size_t
209101#endif
209102
209103/* Initialize a new parser that has already been allocated.
209104*/
209105static void sqlite3Fts5ParserInit(void *fts5yypRawParser sqlite3Fts5ParserCTX_PDECL){
209106  fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yypRawParser;
209107  sqlite3Fts5ParserCTX_STORE
209108#ifdef fts5YYTRACKMAXSTACKDEPTH
209109  fts5yypParser->fts5yyhwm = 0;
209110#endif
209111#if fts5YYSTACKDEPTH<=0
209112  fts5yypParser->fts5yytos = NULL;
209113  fts5yypParser->fts5yystack = NULL;
209114  fts5yypParser->fts5yystksz = 0;
209115  if( fts5yyGrowStack(fts5yypParser) ){
209116    fts5yypParser->fts5yystack = &fts5yypParser->fts5yystk0;
209117    fts5yypParser->fts5yystksz = 1;
209118  }
209119#endif
209120#ifndef fts5YYNOERRORRECOVERY
209121  fts5yypParser->fts5yyerrcnt = -1;
209122#endif
209123  fts5yypParser->fts5yytos = fts5yypParser->fts5yystack;
209124  fts5yypParser->fts5yystack[0].stateno = 0;
209125  fts5yypParser->fts5yystack[0].major = 0;
209126#if fts5YYSTACKDEPTH>0
209127  fts5yypParser->fts5yystackEnd = &fts5yypParser->fts5yystack[fts5YYSTACKDEPTH-1];
209128#endif
209129}
209130
209131#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
209132/*
209133** This function allocates a new parser.
209134** The only argument is a pointer to a function which works like
209135** malloc.
209136**
209137** Inputs:
209138** A pointer to the function used to allocate memory.
209139**
209140** Outputs:
209141** A pointer to a parser.  This pointer is used in subsequent calls
209142** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
209143*/
209144static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE) sqlite3Fts5ParserCTX_PDECL){
209145  fts5yyParser *fts5yypParser;
209146  fts5yypParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPE)sizeof(fts5yyParser) );
209147  if( fts5yypParser ){
209148    sqlite3Fts5ParserCTX_STORE
209149    sqlite3Fts5ParserInit(fts5yypParser sqlite3Fts5ParserCTX_PARAM);
209150  }
209151  return (void*)fts5yypParser;
209152}
209153#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
209154
209155
209156/* The following function deletes the "minor type" or semantic value
209157** associated with a symbol.  The symbol can be either a terminal
209158** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
209159** a pointer to the value to be deleted.  The code used to do the
209160** deletions is derived from the %destructor and/or %token_destructor
209161** directives of the input grammar.
209162*/
209163static void fts5yy_destructor(
209164  fts5yyParser *fts5yypParser,    /* The parser */
209165  fts5YYCODETYPE fts5yymajor,     /* Type code for object to destroy */
209166  fts5YYMINORTYPE *fts5yypminor   /* The object to be destroyed */
209167){
209168  sqlite3Fts5ParserARG_FETCH
209169  sqlite3Fts5ParserCTX_FETCH
209170  switch( fts5yymajor ){
209171    /* Here is inserted the actions which take place when a
209172    ** terminal or non-terminal is destroyed.  This can happen
209173    ** when the symbol is popped from the stack during a
209174    ** reduce or during error processing or when a parser is
209175    ** being destroyed before it is finished parsing.
209176    **
209177    ** Note: during a reduce, the only symbols destroyed are those
209178    ** which appear on the RHS of the rule, but which are *not* used
209179    ** inside the C code.
209180    */
209181/********* Begin destructor definitions ***************************************/
209182    case 16: /* input */
209183{
209184 (void)pParse;
209185}
209186      break;
209187    case 17: /* expr */
209188    case 18: /* cnearset */
209189    case 19: /* exprlist */
209190{
209191 sqlite3Fts5ParseNodeFree((fts5yypminor->fts5yy24));
209192}
209193      break;
209194    case 20: /* colset */
209195    case 21: /* colsetlist */
209196{
209197 sqlite3_free((fts5yypminor->fts5yy11));
209198}
209199      break;
209200    case 22: /* nearset */
209201    case 23: /* nearphrases */
209202{
209203 sqlite3Fts5ParseNearsetFree((fts5yypminor->fts5yy46));
209204}
209205      break;
209206    case 24: /* phrase */
209207{
209208 sqlite3Fts5ParsePhraseFree((fts5yypminor->fts5yy53));
209209}
209210      break;
209211/********* End destructor definitions *****************************************/
209212    default:  break;   /* If no destructor action specified: do nothing */
209213  }
209214}
209215
209216/*
209217** Pop the parser's stack once.
209218**
209219** If there is a destructor routine associated with the token which
209220** is popped from the stack, then call it.
209221*/
209222static void fts5yy_pop_parser_stack(fts5yyParser *pParser){
209223  fts5yyStackEntry *fts5yytos;
209224  assert( pParser->fts5yytos!=0 );
209225  assert( pParser->fts5yytos > pParser->fts5yystack );
209226  fts5yytos = pParser->fts5yytos--;
209227#ifndef NDEBUG
209228  if( fts5yyTraceFILE ){
209229    fprintf(fts5yyTraceFILE,"%sPopping %s\n",
209230      fts5yyTracePrompt,
209231      fts5yyTokenName[fts5yytos->major]);
209232  }
209233#endif
209234  fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
209235}
209236
209237/*
209238** Clear all secondary memory allocations from the parser
209239*/
209240static void sqlite3Fts5ParserFinalize(void *p){
209241  fts5yyParser *pParser = (fts5yyParser*)p;
209242  while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
209243#if fts5YYSTACKDEPTH<=0
209244  if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
209245#endif
209246}
209247
209248#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
209249/*
209250** Deallocate and destroy a parser.  Destructors are called for
209251** all stack elements before shutting the parser down.
209252**
209253** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
209254** is defined in a %include section of the input grammar) then it is
209255** assumed that the input pointer is never NULL.
209256*/
209257static void sqlite3Fts5ParserFree(
209258  void *p,                    /* The parser to be deleted */
209259  void (*freeProc)(void*)     /* Function used to reclaim memory */
209260){
209261#ifndef fts5YYPARSEFREENEVERNULL
209262  if( p==0 ) return;
209263#endif
209264  sqlite3Fts5ParserFinalize(p);
209265  (*freeProc)(p);
209266}
209267#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
209268
209269/*
209270** Return the peak depth of the stack for a parser.
209271*/
209272#ifdef fts5YYTRACKMAXSTACKDEPTH
209273static int sqlite3Fts5ParserStackPeak(void *p){
209274  fts5yyParser *pParser = (fts5yyParser*)p;
209275  return pParser->fts5yyhwm;
209276}
209277#endif
209278
209279/* This array of booleans keeps track of the parser statement
209280** coverage.  The element fts5yycoverage[X][Y] is set when the parser
209281** is in state X and has a lookahead token Y.  In a well-tested
209282** systems, every element of this matrix should end up being set.
209283*/
209284#if defined(fts5YYCOVERAGE)
209285static unsigned char fts5yycoverage[fts5YYNSTATE][fts5YYNFTS5TOKEN];
209286#endif
209287
209288/*
209289** Write into out a description of every state/lookahead combination that
209290**
209291**   (1)  has not been used by the parser, and
209292**   (2)  is not a syntax error.
209293**
209294** Return the number of missed state/lookahead combinations.
209295*/
209296#if defined(fts5YYCOVERAGE)
209297static int sqlite3Fts5ParserCoverage(FILE *out){
209298  int stateno, iLookAhead, i;
209299  int nMissed = 0;
209300  for(stateno=0; stateno<fts5YYNSTATE; stateno++){
209301    i = fts5yy_shift_ofst[stateno];
209302    for(iLookAhead=0; iLookAhead<fts5YYNFTS5TOKEN; iLookAhead++){
209303      if( fts5yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
209304      if( fts5yycoverage[stateno][iLookAhead]==0 ) nMissed++;
209305      if( out ){
209306        fprintf(out,"State %d lookahead %s %s\n", stateno,
209307                fts5yyTokenName[iLookAhead],
209308                fts5yycoverage[stateno][iLookAhead] ? "ok" : "missed");
209309      }
209310    }
209311  }
209312  return nMissed;
209313}
209314#endif
209315
209316/*
209317** Find the appropriate action for a parser given the terminal
209318** look-ahead token iLookAhead.
209319*/
209320static fts5YYACTIONTYPE fts5yy_find_shift_action(
209321  fts5YYCODETYPE iLookAhead,    /* The look-ahead token */
209322  fts5YYACTIONTYPE stateno      /* Current state number */
209323){
209324  int i;
209325
209326  if( stateno>fts5YY_MAX_SHIFT ) return stateno;
209327  assert( stateno <= fts5YY_SHIFT_COUNT );
209328#if defined(fts5YYCOVERAGE)
209329  fts5yycoverage[stateno][iLookAhead] = 1;
209330#endif
209331  do{
209332    i = fts5yy_shift_ofst[stateno];
209333    assert( i>=0 );
209334    assert( i<=fts5YY_ACTTAB_COUNT );
209335    assert( i+fts5YYNFTS5TOKEN<=(int)fts5YY_NLOOKAHEAD );
209336    assert( iLookAhead!=fts5YYNOCODE );
209337    assert( iLookAhead < fts5YYNFTS5TOKEN );
209338    i += iLookAhead;
209339    assert( i<(int)fts5YY_NLOOKAHEAD );
209340    if( fts5yy_lookahead[i]!=iLookAhead ){
209341#ifdef fts5YYFALLBACK
209342      fts5YYCODETYPE iFallback;            /* Fallback token */
209343      assert( iLookAhead<sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0]) );
209344      iFallback = fts5yyFallback[iLookAhead];
209345      if( iFallback!=0 ){
209346#ifndef NDEBUG
209347        if( fts5yyTraceFILE ){
209348          fprintf(fts5yyTraceFILE, "%sFALLBACK %s => %s\n",
209349             fts5yyTracePrompt, fts5yyTokenName[iLookAhead], fts5yyTokenName[iFallback]);
209350        }
209351#endif
209352        assert( fts5yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
209353        iLookAhead = iFallback;
209354        continue;
209355      }
209356#endif
209357#ifdef fts5YYWILDCARD
209358      {
209359        int j = i - iLookAhead + fts5YYWILDCARD;
209360        assert( j<(int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])) );
209361        if( fts5yy_lookahead[j]==fts5YYWILDCARD && iLookAhead>0 ){
209362#ifndef NDEBUG
209363          if( fts5yyTraceFILE ){
209364            fprintf(fts5yyTraceFILE, "%sWILDCARD %s => %s\n",
209365               fts5yyTracePrompt, fts5yyTokenName[iLookAhead],
209366               fts5yyTokenName[fts5YYWILDCARD]);
209367          }
209368#endif /* NDEBUG */
209369          return fts5yy_action[j];
209370        }
209371      }
209372#endif /* fts5YYWILDCARD */
209373      return fts5yy_default[stateno];
209374    }else{
209375      assert( i>=0 && i<sizeof(fts5yy_action)/sizeof(fts5yy_action[0]) );
209376      return fts5yy_action[i];
209377    }
209378  }while(1);
209379}
209380
209381/*
209382** Find the appropriate action for a parser given the non-terminal
209383** look-ahead token iLookAhead.
209384*/
209385static fts5YYACTIONTYPE fts5yy_find_reduce_action(
209386  fts5YYACTIONTYPE stateno,     /* Current state number */
209387  fts5YYCODETYPE iLookAhead     /* The look-ahead token */
209388){
209389  int i;
209390#ifdef fts5YYERRORSYMBOL
209391  if( stateno>fts5YY_REDUCE_COUNT ){
209392    return fts5yy_default[stateno];
209393  }
209394#else
209395  assert( stateno<=fts5YY_REDUCE_COUNT );
209396#endif
209397  i = fts5yy_reduce_ofst[stateno];
209398  assert( iLookAhead!=fts5YYNOCODE );
209399  i += iLookAhead;
209400#ifdef fts5YYERRORSYMBOL
209401  if( i<0 || i>=fts5YY_ACTTAB_COUNT || fts5yy_lookahead[i]!=iLookAhead ){
209402    return fts5yy_default[stateno];
209403  }
209404#else
209405  assert( i>=0 && i<fts5YY_ACTTAB_COUNT );
209406  assert( fts5yy_lookahead[i]==iLookAhead );
209407#endif
209408  return fts5yy_action[i];
209409}
209410
209411/*
209412** The following routine is called if the stack overflows.
209413*/
209414static void fts5yyStackOverflow(fts5yyParser *fts5yypParser){
209415   sqlite3Fts5ParserARG_FETCH
209416   sqlite3Fts5ParserCTX_FETCH
209417#ifndef NDEBUG
209418   if( fts5yyTraceFILE ){
209419     fprintf(fts5yyTraceFILE,"%sStack Overflow!\n",fts5yyTracePrompt);
209420   }
209421#endif
209422   while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
209423   /* Here code is inserted which will execute if the parser
209424   ** stack every overflows */
209425/******** Begin %stack_overflow code ******************************************/
209426
209427  sqlite3Fts5ParseError(pParse, "fts5: parser stack overflow");
209428/******** End %stack_overflow code ********************************************/
209429   sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument var */
209430   sqlite3Fts5ParserCTX_STORE
209431}
209432
209433/*
209434** Print tracing information for a SHIFT action
209435*/
209436#ifndef NDEBUG
209437static void fts5yyTraceShift(fts5yyParser *fts5yypParser, int fts5yyNewState, const char *zTag){
209438  if( fts5yyTraceFILE ){
209439    if( fts5yyNewState<fts5YYNSTATE ){
209440      fprintf(fts5yyTraceFILE,"%s%s '%s', go to state %d\n",
209441         fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
209442         fts5yyNewState);
209443    }else{
209444      fprintf(fts5yyTraceFILE,"%s%s '%s', pending reduce %d\n",
209445         fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
209446         fts5yyNewState - fts5YY_MIN_REDUCE);
209447    }
209448  }
209449}
209450#else
209451# define fts5yyTraceShift(X,Y,Z)
209452#endif
209453
209454/*
209455** Perform a shift action.
209456*/
209457static void fts5yy_shift(
209458  fts5yyParser *fts5yypParser,          /* The parser to be shifted */
209459  fts5YYACTIONTYPE fts5yyNewState,      /* The new state to shift in */
209460  fts5YYCODETYPE fts5yyMajor,           /* The major token to shift in */
209461  sqlite3Fts5ParserFTS5TOKENTYPE fts5yyMinor        /* The minor token to shift in */
209462){
209463  fts5yyStackEntry *fts5yytos;
209464  fts5yypParser->fts5yytos++;
209465#ifdef fts5YYTRACKMAXSTACKDEPTH
209466  if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
209467    fts5yypParser->fts5yyhwm++;
209468    assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack) );
209469  }
209470#endif
209471#if fts5YYSTACKDEPTH>0
209472  if( fts5yypParser->fts5yytos>fts5yypParser->fts5yystackEnd ){
209473    fts5yypParser->fts5yytos--;
209474    fts5yyStackOverflow(fts5yypParser);
209475    return;
209476  }
209477#else
209478  if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz] ){
209479    if( fts5yyGrowStack(fts5yypParser) ){
209480      fts5yypParser->fts5yytos--;
209481      fts5yyStackOverflow(fts5yypParser);
209482      return;
209483    }
209484  }
209485#endif
209486  if( fts5yyNewState > fts5YY_MAX_SHIFT ){
209487    fts5yyNewState += fts5YY_MIN_REDUCE - fts5YY_MIN_SHIFTREDUCE;
209488  }
209489  fts5yytos = fts5yypParser->fts5yytos;
209490  fts5yytos->stateno = fts5yyNewState;
209491  fts5yytos->major = fts5yyMajor;
209492  fts5yytos->minor.fts5yy0 = fts5yyMinor;
209493  fts5yyTraceShift(fts5yypParser, fts5yyNewState, "Shift");
209494}
209495
209496/* For rule J, fts5yyRuleInfoLhs[J] contains the symbol on the left-hand side
209497** of that rule */
209498static const fts5YYCODETYPE fts5yyRuleInfoLhs[] = {
209499    16,  /* (0) input ::= expr */
209500    20,  /* (1) colset ::= MINUS LCP colsetlist RCP */
209501    20,  /* (2) colset ::= LCP colsetlist RCP */
209502    20,  /* (3) colset ::= STRING */
209503    20,  /* (4) colset ::= MINUS STRING */
209504    21,  /* (5) colsetlist ::= colsetlist STRING */
209505    21,  /* (6) colsetlist ::= STRING */
209506    17,  /* (7) expr ::= expr AND expr */
209507    17,  /* (8) expr ::= expr OR expr */
209508    17,  /* (9) expr ::= expr NOT expr */
209509    17,  /* (10) expr ::= colset COLON LP expr RP */
209510    17,  /* (11) expr ::= LP expr RP */
209511    17,  /* (12) expr ::= exprlist */
209512    19,  /* (13) exprlist ::= cnearset */
209513    19,  /* (14) exprlist ::= exprlist cnearset */
209514    18,  /* (15) cnearset ::= nearset */
209515    18,  /* (16) cnearset ::= colset COLON nearset */
209516    22,  /* (17) nearset ::= phrase */
209517    22,  /* (18) nearset ::= CARET phrase */
209518    22,  /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
209519    23,  /* (20) nearphrases ::= phrase */
209520    23,  /* (21) nearphrases ::= nearphrases phrase */
209521    25,  /* (22) neardist_opt ::= */
209522    25,  /* (23) neardist_opt ::= COMMA STRING */
209523    24,  /* (24) phrase ::= phrase PLUS STRING star_opt */
209524    24,  /* (25) phrase ::= STRING star_opt */
209525    26,  /* (26) star_opt ::= STAR */
209526    26,  /* (27) star_opt ::= */
209527};
209528
209529/* For rule J, fts5yyRuleInfoNRhs[J] contains the negative of the number
209530** of symbols on the right-hand side of that rule. */
209531static const signed char fts5yyRuleInfoNRhs[] = {
209532   -1,  /* (0) input ::= expr */
209533   -4,  /* (1) colset ::= MINUS LCP colsetlist RCP */
209534   -3,  /* (2) colset ::= LCP colsetlist RCP */
209535   -1,  /* (3) colset ::= STRING */
209536   -2,  /* (4) colset ::= MINUS STRING */
209537   -2,  /* (5) colsetlist ::= colsetlist STRING */
209538   -1,  /* (6) colsetlist ::= STRING */
209539   -3,  /* (7) expr ::= expr AND expr */
209540   -3,  /* (8) expr ::= expr OR expr */
209541   -3,  /* (9) expr ::= expr NOT expr */
209542   -5,  /* (10) expr ::= colset COLON LP expr RP */
209543   -3,  /* (11) expr ::= LP expr RP */
209544   -1,  /* (12) expr ::= exprlist */
209545   -1,  /* (13) exprlist ::= cnearset */
209546   -2,  /* (14) exprlist ::= exprlist cnearset */
209547   -1,  /* (15) cnearset ::= nearset */
209548   -3,  /* (16) cnearset ::= colset COLON nearset */
209549   -1,  /* (17) nearset ::= phrase */
209550   -2,  /* (18) nearset ::= CARET phrase */
209551   -5,  /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
209552   -1,  /* (20) nearphrases ::= phrase */
209553   -2,  /* (21) nearphrases ::= nearphrases phrase */
209554    0,  /* (22) neardist_opt ::= */
209555   -2,  /* (23) neardist_opt ::= COMMA STRING */
209556   -4,  /* (24) phrase ::= phrase PLUS STRING star_opt */
209557   -2,  /* (25) phrase ::= STRING star_opt */
209558   -1,  /* (26) star_opt ::= STAR */
209559    0,  /* (27) star_opt ::= */
209560};
209561
209562static void fts5yy_accept(fts5yyParser*);  /* Forward Declaration */
209563
209564/*
209565** Perform a reduce action and the shift that must immediately
209566** follow the reduce.
209567**
209568** The fts5yyLookahead and fts5yyLookaheadToken parameters provide reduce actions
209569** access to the lookahead token (if any).  The fts5yyLookahead will be fts5YYNOCODE
209570** if the lookahead token has already been consumed.  As this procedure is
209571** only called from one place, optimizing compilers will in-line it, which
209572** means that the extra parameters have no performance impact.
209573*/
209574static fts5YYACTIONTYPE fts5yy_reduce(
209575  fts5yyParser *fts5yypParser,         /* The parser */
209576  unsigned int fts5yyruleno,       /* Number of the rule by which to reduce */
209577  int fts5yyLookahead,             /* Lookahead token, or fts5YYNOCODE if none */
209578  sqlite3Fts5ParserFTS5TOKENTYPE fts5yyLookaheadToken  /* Value of the lookahead token */
209579  sqlite3Fts5ParserCTX_PDECL                   /* %extra_context */
209580){
209581  int fts5yygoto;                     /* The next state */
209582  fts5YYACTIONTYPE fts5yyact;             /* The next action */
209583  fts5yyStackEntry *fts5yymsp;            /* The top of the parser's stack */
209584  int fts5yysize;                     /* Amount to pop the stack */
209585  sqlite3Fts5ParserARG_FETCH
209586  (void)fts5yyLookahead;
209587  (void)fts5yyLookaheadToken;
209588  fts5yymsp = fts5yypParser->fts5yytos;
209589#ifndef NDEBUG
209590  if( fts5yyTraceFILE && fts5yyruleno<(int)(sizeof(fts5yyRuleName)/sizeof(fts5yyRuleName[0])) ){
209591    fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
209592    if( fts5yysize ){
209593      fprintf(fts5yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
209594        fts5yyTracePrompt,
209595        fts5yyruleno, fts5yyRuleName[fts5yyruleno],
209596        fts5yyruleno<fts5YYNRULE_WITH_ACTION ? "" : " without external action",
209597        fts5yymsp[fts5yysize].stateno);
209598    }else{
209599      fprintf(fts5yyTraceFILE, "%sReduce %d [%s]%s.\n",
209600        fts5yyTracePrompt, fts5yyruleno, fts5yyRuleName[fts5yyruleno],
209601        fts5yyruleno<fts5YYNRULE_WITH_ACTION ? "" : " without external action");
209602    }
209603  }
209604#endif /* NDEBUG */
209605
209606  /* Check that the stack is large enough to grow by a single entry
209607  ** if the RHS of the rule is empty.  This ensures that there is room
209608  ** enough on the stack to push the LHS value */
209609  if( fts5yyRuleInfoNRhs[fts5yyruleno]==0 ){
209610#ifdef fts5YYTRACKMAXSTACKDEPTH
209611    if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
209612      fts5yypParser->fts5yyhwm++;
209613      assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack));
209614    }
209615#endif
209616#if fts5YYSTACKDEPTH>0
209617    if( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystackEnd ){
209618      fts5yyStackOverflow(fts5yypParser);
209619      /* The call to fts5yyStackOverflow() above pops the stack until it is
209620      ** empty, causing the main parser loop to exit.  So the return value
209621      ** is never used and does not matter. */
209622      return 0;
209623    }
209624#else
209625    if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz-1] ){
209626      if( fts5yyGrowStack(fts5yypParser) ){
209627        fts5yyStackOverflow(fts5yypParser);
209628        /* The call to fts5yyStackOverflow() above pops the stack until it is
209629        ** empty, causing the main parser loop to exit.  So the return value
209630        ** is never used and does not matter. */
209631        return 0;
209632      }
209633      fts5yymsp = fts5yypParser->fts5yytos;
209634    }
209635#endif
209636  }
209637
209638  switch( fts5yyruleno ){
209639  /* Beginning here are the reduction cases.  A typical example
209640  ** follows:
209641  **   case 0:
209642  **  #line <lineno> <grammarfile>
209643  **     { ... }           // User supplied code
209644  **  #line <lineno> <thisfile>
209645  **     break;
209646  */
209647/********** Begin reduce actions **********************************************/
209648        fts5YYMINORTYPE fts5yylhsminor;
209649      case 0: /* input ::= expr */
209650{ sqlite3Fts5ParseFinished(pParse, fts5yymsp[0].minor.fts5yy24); }
209651        break;
209652      case 1: /* colset ::= MINUS LCP colsetlist RCP */
209653{
209654    fts5yymsp[-3].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
209655}
209656        break;
209657      case 2: /* colset ::= LCP colsetlist RCP */
209658{ fts5yymsp[-2].minor.fts5yy11 = fts5yymsp[-1].minor.fts5yy11; }
209659        break;
209660      case 3: /* colset ::= STRING */
209661{
209662  fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
209663}
209664  fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
209665        break;
209666      case 4: /* colset ::= MINUS STRING */
209667{
209668  fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
209669  fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
209670}
209671        break;
209672      case 5: /* colsetlist ::= colsetlist STRING */
209673{
209674  fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, fts5yymsp[-1].minor.fts5yy11, &fts5yymsp[0].minor.fts5yy0); }
209675  fts5yymsp[-1].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
209676        break;
209677      case 6: /* colsetlist ::= STRING */
209678{
209679  fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
209680}
209681  fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
209682        break;
209683      case 7: /* expr ::= expr AND expr */
209684{
209685  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_AND, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
209686}
209687  fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
209688        break;
209689      case 8: /* expr ::= expr OR expr */
209690{
209691  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_OR, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
209692}
209693  fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
209694        break;
209695      case 9: /* expr ::= expr NOT expr */
209696{
209697  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_NOT, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
209698}
209699  fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
209700        break;
209701      case 10: /* expr ::= colset COLON LP expr RP */
209702{
209703  sqlite3Fts5ParseSetColset(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[-4].minor.fts5yy11);
209704  fts5yylhsminor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;
209705}
209706  fts5yymsp[-4].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
209707        break;
209708      case 11: /* expr ::= LP expr RP */
209709{fts5yymsp[-2].minor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;}
209710        break;
209711      case 12: /* expr ::= exprlist */
209712      case 13: /* exprlist ::= cnearset */ fts5yytestcase(fts5yyruleno==13);
209713{fts5yylhsminor.fts5yy24 = fts5yymsp[0].minor.fts5yy24;}
209714  fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
209715        break;
209716      case 14: /* exprlist ::= exprlist cnearset */
209717{
209718  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseImplicitAnd(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24);
209719}
209720  fts5yymsp[-1].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
209721        break;
209722      case 15: /* cnearset ::= nearset */
209723{
209724  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy46);
209725}
209726  fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
209727        break;
209728      case 16: /* cnearset ::= colset COLON nearset */
209729{
209730  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy46);
209731  sqlite3Fts5ParseSetColset(pParse, fts5yylhsminor.fts5yy24, fts5yymsp[-2].minor.fts5yy11);
209732}
209733  fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
209734        break;
209735      case 17: /* nearset ::= phrase */
209736{ fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); }
209737  fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
209738        break;
209739      case 18: /* nearset ::= CARET phrase */
209740{
209741  sqlite3Fts5ParseSetCaret(fts5yymsp[0].minor.fts5yy53);
209742  fts5yymsp[-1].minor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
209743}
209744        break;
209745      case 19: /* nearset ::= STRING LP nearphrases neardist_opt RP */
209746{
209747  sqlite3Fts5ParseNear(pParse, &fts5yymsp[-4].minor.fts5yy0);
209748  sqlite3Fts5ParseSetDistance(pParse, fts5yymsp[-2].minor.fts5yy46, &fts5yymsp[-1].minor.fts5yy0);
209749  fts5yylhsminor.fts5yy46 = fts5yymsp[-2].minor.fts5yy46;
209750}
209751  fts5yymsp[-4].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
209752        break;
209753      case 20: /* nearphrases ::= phrase */
209754{
209755  fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
209756}
209757  fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
209758        break;
209759      case 21: /* nearphrases ::= nearphrases phrase */
209760{
209761  fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, fts5yymsp[-1].minor.fts5yy46, fts5yymsp[0].minor.fts5yy53);
209762}
209763  fts5yymsp[-1].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
209764        break;
209765      case 22: /* neardist_opt ::= */
209766{ fts5yymsp[1].minor.fts5yy0.p = 0; fts5yymsp[1].minor.fts5yy0.n = 0; }
209767        break;
209768      case 23: /* neardist_opt ::= COMMA STRING */
209769{ fts5yymsp[-1].minor.fts5yy0 = fts5yymsp[0].minor.fts5yy0; }
209770        break;
209771      case 24: /* phrase ::= phrase PLUS STRING star_opt */
209772{
209773  fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, fts5yymsp[-3].minor.fts5yy53, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
209774}
209775  fts5yymsp[-3].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
209776        break;
209777      case 25: /* phrase ::= STRING star_opt */
209778{
209779  fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, 0, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
209780}
209781  fts5yymsp[-1].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
209782        break;
209783      case 26: /* star_opt ::= STAR */
209784{ fts5yymsp[0].minor.fts5yy4 = 1; }
209785        break;
209786      case 27: /* star_opt ::= */
209787{ fts5yymsp[1].minor.fts5yy4 = 0; }
209788        break;
209789      default:
209790        break;
209791/********** End reduce actions ************************************************/
209792  };
209793  assert( fts5yyruleno<sizeof(fts5yyRuleInfoLhs)/sizeof(fts5yyRuleInfoLhs[0]) );
209794  fts5yygoto = fts5yyRuleInfoLhs[fts5yyruleno];
209795  fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
209796  fts5yyact = fts5yy_find_reduce_action(fts5yymsp[fts5yysize].stateno,(fts5YYCODETYPE)fts5yygoto);
209797
209798  /* There are no SHIFTREDUCE actions on nonterminals because the table
209799  ** generator has simplified them to pure REDUCE actions. */
209800  assert( !(fts5yyact>fts5YY_MAX_SHIFT && fts5yyact<=fts5YY_MAX_SHIFTREDUCE) );
209801
209802  /* It is not possible for a REDUCE to be followed by an error */
209803  assert( fts5yyact!=fts5YY_ERROR_ACTION );
209804
209805  fts5yymsp += fts5yysize+1;
209806  fts5yypParser->fts5yytos = fts5yymsp;
209807  fts5yymsp->stateno = (fts5YYACTIONTYPE)fts5yyact;
209808  fts5yymsp->major = (fts5YYCODETYPE)fts5yygoto;
209809  fts5yyTraceShift(fts5yypParser, fts5yyact, "... then shift");
209810  return fts5yyact;
209811}
209812
209813/*
209814** The following code executes when the parse fails
209815*/
209816#ifndef fts5YYNOERRORRECOVERY
209817static void fts5yy_parse_failed(
209818  fts5yyParser *fts5yypParser           /* The parser */
209819){
209820  sqlite3Fts5ParserARG_FETCH
209821  sqlite3Fts5ParserCTX_FETCH
209822#ifndef NDEBUG
209823  if( fts5yyTraceFILE ){
209824    fprintf(fts5yyTraceFILE,"%sFail!\n",fts5yyTracePrompt);
209825  }
209826#endif
209827  while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
209828  /* Here code is inserted which will be executed whenever the
209829  ** parser fails */
209830/************ Begin %parse_failure code ***************************************/
209831/************ End %parse_failure code *****************************************/
209832  sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
209833  sqlite3Fts5ParserCTX_STORE
209834}
209835#endif /* fts5YYNOERRORRECOVERY */
209836
209837/*
209838** The following code executes when a syntax error first occurs.
209839*/
209840static void fts5yy_syntax_error(
209841  fts5yyParser *fts5yypParser,           /* The parser */
209842  int fts5yymajor,                   /* The major type of the error token */
209843  sqlite3Fts5ParserFTS5TOKENTYPE fts5yyminor         /* The minor type of the error token */
209844){
209845  sqlite3Fts5ParserARG_FETCH
209846  sqlite3Fts5ParserCTX_FETCH
209847#define FTS5TOKEN fts5yyminor
209848/************ Begin %syntax_error code ****************************************/
209849
209850  UNUSED_PARAM(fts5yymajor); /* Silence a compiler warning */
209851  sqlite3Fts5ParseError(
209852    pParse, "fts5: syntax error near \"%.*s\"",FTS5TOKEN.n,FTS5TOKEN.p
209853  );
209854/************ End %syntax_error code ******************************************/
209855  sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
209856  sqlite3Fts5ParserCTX_STORE
209857}
209858
209859/*
209860** The following is executed when the parser accepts
209861*/
209862static void fts5yy_accept(
209863  fts5yyParser *fts5yypParser           /* The parser */
209864){
209865  sqlite3Fts5ParserARG_FETCH
209866  sqlite3Fts5ParserCTX_FETCH
209867#ifndef NDEBUG
209868  if( fts5yyTraceFILE ){
209869    fprintf(fts5yyTraceFILE,"%sAccept!\n",fts5yyTracePrompt);
209870  }
209871#endif
209872#ifndef fts5YYNOERRORRECOVERY
209873  fts5yypParser->fts5yyerrcnt = -1;
209874#endif
209875  assert( fts5yypParser->fts5yytos==fts5yypParser->fts5yystack );
209876  /* Here code is inserted which will be executed whenever the
209877  ** parser accepts */
209878/*********** Begin %parse_accept code *****************************************/
209879/*********** End %parse_accept code *******************************************/
209880  sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
209881  sqlite3Fts5ParserCTX_STORE
209882}
209883
209884/* The main parser program.
209885** The first argument is a pointer to a structure obtained from
209886** "sqlite3Fts5ParserAlloc" which describes the current state of the parser.
209887** The second argument is the major token number.  The third is
209888** the minor token.  The fourth optional argument is whatever the
209889** user wants (and specified in the grammar) and is available for
209890** use by the action routines.
209891**
209892** Inputs:
209893** <ul>
209894** <li> A pointer to the parser (an opaque structure.)
209895** <li> The major token number.
209896** <li> The minor token number.
209897** <li> An option argument of a grammar-specified type.
209898** </ul>
209899**
209900** Outputs:
209901** None.
209902*/
209903static void sqlite3Fts5Parser(
209904  void *fts5yyp,                   /* The parser */
209905  int fts5yymajor,                 /* The major token code number */
209906  sqlite3Fts5ParserFTS5TOKENTYPE fts5yyminor       /* The value for the token */
209907  sqlite3Fts5ParserARG_PDECL               /* Optional %extra_argument parameter */
209908){
209909  fts5YYMINORTYPE fts5yyminorunion;
209910  fts5YYACTIONTYPE fts5yyact;   /* The parser action. */
209911#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY)
209912  int fts5yyendofinput;     /* True if we are at the end of input */
209913#endif
209914#ifdef fts5YYERRORSYMBOL
209915  int fts5yyerrorhit = 0;   /* True if fts5yymajor has invoked an error */
209916#endif
209917  fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yyp;  /* The parser */
209918  sqlite3Fts5ParserCTX_FETCH
209919  sqlite3Fts5ParserARG_STORE
209920
209921  assert( fts5yypParser->fts5yytos!=0 );
209922#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY)
209923  fts5yyendofinput = (fts5yymajor==0);
209924#endif
209925
209926  fts5yyact = fts5yypParser->fts5yytos->stateno;
209927#ifndef NDEBUG
209928  if( fts5yyTraceFILE ){
209929    if( fts5yyact < fts5YY_MIN_REDUCE ){
209930      fprintf(fts5yyTraceFILE,"%sInput '%s' in state %d\n",
209931              fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact);
209932    }else{
209933      fprintf(fts5yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
209934              fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact-fts5YY_MIN_REDUCE);
209935    }
209936  }
209937#endif
209938
209939  do{
209940    assert( fts5yyact==fts5yypParser->fts5yytos->stateno );
209941    fts5yyact = fts5yy_find_shift_action((fts5YYCODETYPE)fts5yymajor,fts5yyact);
209942    if( fts5yyact >= fts5YY_MIN_REDUCE ){
209943      fts5yyact = fts5yy_reduce(fts5yypParser,fts5yyact-fts5YY_MIN_REDUCE,fts5yymajor,
209944                        fts5yyminor sqlite3Fts5ParserCTX_PARAM);
209945    }else if( fts5yyact <= fts5YY_MAX_SHIFTREDUCE ){
209946      fts5yy_shift(fts5yypParser,fts5yyact,(fts5YYCODETYPE)fts5yymajor,fts5yyminor);
209947#ifndef fts5YYNOERRORRECOVERY
209948      fts5yypParser->fts5yyerrcnt--;
209949#endif
209950      break;
209951    }else if( fts5yyact==fts5YY_ACCEPT_ACTION ){
209952      fts5yypParser->fts5yytos--;
209953      fts5yy_accept(fts5yypParser);
209954      return;
209955    }else{
209956      assert( fts5yyact == fts5YY_ERROR_ACTION );
209957      fts5yyminorunion.fts5yy0 = fts5yyminor;
209958#ifdef fts5YYERRORSYMBOL
209959      int fts5yymx;
209960#endif
209961#ifndef NDEBUG
209962      if( fts5yyTraceFILE ){
209963        fprintf(fts5yyTraceFILE,"%sSyntax Error!\n",fts5yyTracePrompt);
209964      }
209965#endif
209966#ifdef fts5YYERRORSYMBOL
209967      /* A syntax error has occurred.
209968      ** The response to an error depends upon whether or not the
209969      ** grammar defines an error token "ERROR".
209970      **
209971      ** This is what we do if the grammar does define ERROR:
209972      **
209973      **  * Call the %syntax_error function.
209974      **
209975      **  * Begin popping the stack until we enter a state where
209976      **    it is legal to shift the error symbol, then shift
209977      **    the error symbol.
209978      **
209979      **  * Set the error count to three.
209980      **
209981      **  * Begin accepting and shifting new tokens.  No new error
209982      **    processing will occur until three tokens have been
209983      **    shifted successfully.
209984      **
209985      */
209986      if( fts5yypParser->fts5yyerrcnt<0 ){
209987        fts5yy_syntax_error(fts5yypParser,fts5yymajor,fts5yyminor);
209988      }
209989      fts5yymx = fts5yypParser->fts5yytos->major;
209990      if( fts5yymx==fts5YYERRORSYMBOL || fts5yyerrorhit ){
209991#ifndef NDEBUG
209992        if( fts5yyTraceFILE ){
209993          fprintf(fts5yyTraceFILE,"%sDiscard input token %s\n",
209994             fts5yyTracePrompt,fts5yyTokenName[fts5yymajor]);
209995        }
209996#endif
209997        fts5yy_destructor(fts5yypParser, (fts5YYCODETYPE)fts5yymajor, &fts5yyminorunion);
209998        fts5yymajor = fts5YYNOCODE;
209999      }else{
210000        while( fts5yypParser->fts5yytos >= fts5yypParser->fts5yystack
210001            && (fts5yyact = fts5yy_find_reduce_action(
210002                        fts5yypParser->fts5yytos->stateno,
210003                        fts5YYERRORSYMBOL)) > fts5YY_MAX_SHIFTREDUCE
210004        ){
210005          fts5yy_pop_parser_stack(fts5yypParser);
210006        }
210007        if( fts5yypParser->fts5yytos < fts5yypParser->fts5yystack || fts5yymajor==0 ){
210008          fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
210009          fts5yy_parse_failed(fts5yypParser);
210010#ifndef fts5YYNOERRORRECOVERY
210011          fts5yypParser->fts5yyerrcnt = -1;
210012#endif
210013          fts5yymajor = fts5YYNOCODE;
210014        }else if( fts5yymx!=fts5YYERRORSYMBOL ){
210015          fts5yy_shift(fts5yypParser,fts5yyact,fts5YYERRORSYMBOL,fts5yyminor);
210016        }
210017      }
210018      fts5yypParser->fts5yyerrcnt = 3;
210019      fts5yyerrorhit = 1;
210020      if( fts5yymajor==fts5YYNOCODE ) break;
210021      fts5yyact = fts5yypParser->fts5yytos->stateno;
210022#elif defined(fts5YYNOERRORRECOVERY)
210023      /* If the fts5YYNOERRORRECOVERY macro is defined, then do not attempt to
210024      ** do any kind of error recovery.  Instead, simply invoke the syntax
210025      ** error routine and continue going as if nothing had happened.
210026      **
210027      ** Applications can set this macro (for example inside %include) if
210028      ** they intend to abandon the parse upon the first syntax error seen.
210029      */
210030      fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
210031      fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
210032      break;
210033#else  /* fts5YYERRORSYMBOL is not defined */
210034      /* This is what we do if the grammar does not define ERROR:
210035      **
210036      **  * Report an error message, and throw away the input token.
210037      **
210038      **  * If the input token is $, then fail the parse.
210039      **
210040      ** As before, subsequent error messages are suppressed until
210041      ** three input tokens have been successfully shifted.
210042      */
210043      if( fts5yypParser->fts5yyerrcnt<=0 ){
210044        fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
210045      }
210046      fts5yypParser->fts5yyerrcnt = 3;
210047      fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
210048      if( fts5yyendofinput ){
210049        fts5yy_parse_failed(fts5yypParser);
210050#ifndef fts5YYNOERRORRECOVERY
210051        fts5yypParser->fts5yyerrcnt = -1;
210052#endif
210053      }
210054      break;
210055#endif
210056    }
210057  }while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack );
210058#ifndef NDEBUG
210059  if( fts5yyTraceFILE ){
210060    fts5yyStackEntry *i;
210061    char cDiv = '[';
210062    fprintf(fts5yyTraceFILE,"%sReturn. Stack=",fts5yyTracePrompt);
210063    for(i=&fts5yypParser->fts5yystack[1]; i<=fts5yypParser->fts5yytos; i++){
210064      fprintf(fts5yyTraceFILE,"%c%s", cDiv, fts5yyTokenName[i->major]);
210065      cDiv = ' ';
210066    }
210067    fprintf(fts5yyTraceFILE,"]\n");
210068  }
210069#endif
210070  return;
210071}
210072
210073/*
210074** Return the fallback token corresponding to canonical token iToken, or
210075** 0 if iToken has no fallback.
210076*/
210077static int sqlite3Fts5ParserFallback(int iToken){
210078#ifdef fts5YYFALLBACK
210079  assert( iToken<(int)(sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])) );
210080  return fts5yyFallback[iToken];
210081#else
210082  (void)iToken;
210083  return 0;
210084#endif
210085}
210086
210087/*
210088** 2014 May 31
210089**
210090** The author disclaims copyright to this source code.  In place of
210091** a legal notice, here is a blessing:
210092**
210093**    May you do good and not evil.
210094**    May you find forgiveness for yourself and forgive others.
210095**    May you share freely, never taking more than you give.
210096**
210097******************************************************************************
210098*/
210099
210100
210101/* #include "fts5Int.h" */
210102#include <math.h>                 /* amalgamator: keep */
210103
210104/*
210105** Object used to iterate through all "coalesced phrase instances" in
210106** a single column of the current row. If the phrase instances in the
210107** column being considered do not overlap, this object simply iterates
210108** through them. Or, if they do overlap (share one or more tokens in
210109** common), each set of overlapping instances is treated as a single
210110** match. See documentation for the highlight() auxiliary function for
210111** details.
210112**
210113** Usage is:
210114**
210115**   for(rc = fts5CInstIterNext(pApi, pFts, iCol, &iter);
210116**      (rc==SQLITE_OK && 0==fts5CInstIterEof(&iter);
210117**      rc = fts5CInstIterNext(&iter)
210118**   ){
210119**     printf("instance starts at %d, ends at %d\n", iter.iStart, iter.iEnd);
210120**   }
210121**
210122*/
210123typedef struct CInstIter CInstIter;
210124struct CInstIter {
210125  const Fts5ExtensionApi *pApi;   /* API offered by current FTS version */
210126  Fts5Context *pFts;              /* First arg to pass to pApi functions */
210127  int iCol;                       /* Column to search */
210128  int iInst;                      /* Next phrase instance index */
210129  int nInst;                      /* Total number of phrase instances */
210130
210131  /* Output variables */
210132  int iStart;                     /* First token in coalesced phrase instance */
210133  int iEnd;                       /* Last token in coalesced phrase instance */
210134};
210135
210136/*
210137** Advance the iterator to the next coalesced phrase instance. Return
210138** an SQLite error code if an error occurs, or SQLITE_OK otherwise.
210139*/
210140static int fts5CInstIterNext(CInstIter *pIter){
210141  int rc = SQLITE_OK;
210142  pIter->iStart = -1;
210143  pIter->iEnd = -1;
210144
210145  while( rc==SQLITE_OK && pIter->iInst<pIter->nInst ){
210146    int ip; int ic; int io;
210147    rc = pIter->pApi->xInst(pIter->pFts, pIter->iInst, &ip, &ic, &io);
210148    if( rc==SQLITE_OK ){
210149      if( ic==pIter->iCol ){
210150        int iEnd = io - 1 + pIter->pApi->xPhraseSize(pIter->pFts, ip);
210151        if( pIter->iStart<0 ){
210152          pIter->iStart = io;
210153          pIter->iEnd = iEnd;
210154        }else if( io<=pIter->iEnd ){
210155          if( iEnd>pIter->iEnd ) pIter->iEnd = iEnd;
210156        }else{
210157          break;
210158        }
210159      }
210160      pIter->iInst++;
210161    }
210162  }
210163
210164  return rc;
210165}
210166
210167/*
210168** Initialize the iterator object indicated by the final parameter to
210169** iterate through coalesced phrase instances in column iCol.
210170*/
210171static int fts5CInstIterInit(
210172  const Fts5ExtensionApi *pApi,
210173  Fts5Context *pFts,
210174  int iCol,
210175  CInstIter *pIter
210176){
210177  int rc;
210178
210179  memset(pIter, 0, sizeof(CInstIter));
210180  pIter->pApi = pApi;
210181  pIter->pFts = pFts;
210182  pIter->iCol = iCol;
210183  rc = pApi->xInstCount(pFts, &pIter->nInst);
210184
210185  if( rc==SQLITE_OK ){
210186    rc = fts5CInstIterNext(pIter);
210187  }
210188
210189  return rc;
210190}
210191
210192
210193
210194/*************************************************************************
210195** Start of highlight() implementation.
210196*/
210197typedef struct HighlightContext HighlightContext;
210198struct HighlightContext {
210199  CInstIter iter;                 /* Coalesced Instance Iterator */
210200  int iPos;                       /* Current token offset in zIn[] */
210201  int iRangeStart;                /* First token to include */
210202  int iRangeEnd;                  /* If non-zero, last token to include */
210203  const char *zOpen;              /* Opening highlight */
210204  const char *zClose;             /* Closing highlight */
210205  const char *zIn;                /* Input text */
210206  int nIn;                        /* Size of input text in bytes */
210207  int iOff;                       /* Current offset within zIn[] */
210208  char *zOut;                     /* Output value */
210209};
210210
210211/*
210212** Append text to the HighlightContext output string - p->zOut. Argument
210213** z points to a buffer containing n bytes of text to append. If n is
210214** negative, everything up until the first '\0' is appended to the output.
210215**
210216** If *pRc is set to any value other than SQLITE_OK when this function is
210217** called, it is a no-op. If an error (i.e. an OOM condition) is encountered,
210218** *pRc is set to an error code before returning.
210219*/
210220static void fts5HighlightAppend(
210221  int *pRc,
210222  HighlightContext *p,
210223  const char *z, int n
210224){
210225  if( *pRc==SQLITE_OK && z ){
210226    if( n<0 ) n = (int)strlen(z);
210227    p->zOut = sqlite3_mprintf("%z%.*s", p->zOut, n, z);
210228    if( p->zOut==0 ) *pRc = SQLITE_NOMEM;
210229  }
210230}
210231
210232/*
210233** Tokenizer callback used by implementation of highlight() function.
210234*/
210235static int fts5HighlightCb(
210236  void *pContext,                 /* Pointer to HighlightContext object */
210237  int tflags,                     /* Mask of FTS5_TOKEN_* flags */
210238  const char *pToken,             /* Buffer containing token */
210239  int nToken,                     /* Size of token in bytes */
210240  int iStartOff,                  /* Start offset of token */
210241  int iEndOff                     /* End offset of token */
210242){
210243  HighlightContext *p = (HighlightContext*)pContext;
210244  int rc = SQLITE_OK;
210245  int iPos;
210246
210247  UNUSED_PARAM2(pToken, nToken);
210248
210249  if( tflags & FTS5_TOKEN_COLOCATED ) return SQLITE_OK;
210250  iPos = p->iPos++;
210251
210252  if( p->iRangeEnd>0 ){
210253    if( iPos<p->iRangeStart || iPos>p->iRangeEnd ) return SQLITE_OK;
210254    if( p->iRangeStart && iPos==p->iRangeStart ) p->iOff = iStartOff;
210255  }
210256
210257  if( iPos==p->iter.iStart ){
210258    fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iStartOff - p->iOff);
210259    fts5HighlightAppend(&rc, p, p->zOpen, -1);
210260    p->iOff = iStartOff;
210261  }
210262
210263  if( iPos==p->iter.iEnd ){
210264    if( p->iRangeEnd && p->iter.iStart<p->iRangeStart ){
210265      fts5HighlightAppend(&rc, p, p->zOpen, -1);
210266    }
210267    fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
210268    fts5HighlightAppend(&rc, p, p->zClose, -1);
210269    p->iOff = iEndOff;
210270    if( rc==SQLITE_OK ){
210271      rc = fts5CInstIterNext(&p->iter);
210272    }
210273  }
210274
210275  if( p->iRangeEnd>0 && iPos==p->iRangeEnd ){
210276    fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
210277    p->iOff = iEndOff;
210278    if( iPos>=p->iter.iStart && iPos<p->iter.iEnd ){
210279      fts5HighlightAppend(&rc, p, p->zClose, -1);
210280    }
210281  }
210282
210283  return rc;
210284}
210285
210286/*
210287** Implementation of highlight() function.
210288*/
210289static void fts5HighlightFunction(
210290  const Fts5ExtensionApi *pApi,   /* API offered by current FTS version */
210291  Fts5Context *pFts,              /* First arg to pass to pApi functions */
210292  sqlite3_context *pCtx,          /* Context for returning result/error */
210293  int nVal,                       /* Number of values in apVal[] array */
210294  sqlite3_value **apVal           /* Array of trailing arguments */
210295){
210296  HighlightContext ctx;
210297  int rc;
210298  int iCol;
210299
210300  if( nVal!=3 ){
210301    const char *zErr = "wrong number of arguments to function highlight()";
210302    sqlite3_result_error(pCtx, zErr, -1);
210303    return;
210304  }
210305
210306  iCol = sqlite3_value_int(apVal[0]);
210307  memset(&ctx, 0, sizeof(HighlightContext));
210308  ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]);
210309  ctx.zClose = (const char*)sqlite3_value_text(apVal[2]);
210310  rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);
210311
210312  if( ctx.zIn ){
210313    if( rc==SQLITE_OK ){
210314      rc = fts5CInstIterInit(pApi, pFts, iCol, &ctx.iter);
210315    }
210316
210317    if( rc==SQLITE_OK ){
210318      rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
210319    }
210320    fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
210321
210322    if( rc==SQLITE_OK ){
210323      sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT);
210324    }
210325    sqlite3_free(ctx.zOut);
210326  }
210327  if( rc!=SQLITE_OK ){
210328    sqlite3_result_error_code(pCtx, rc);
210329  }
210330}
210331/*
210332** End of highlight() implementation.
210333**************************************************************************/
210334
210335/*
210336** Context object passed to the fts5SentenceFinderCb() function.
210337*/
210338typedef struct Fts5SFinder Fts5SFinder;
210339struct Fts5SFinder {
210340  int iPos;                       /* Current token position */
210341  int nFirstAlloc;                /* Allocated size of aFirst[] */
210342  int nFirst;                     /* Number of entries in aFirst[] */
210343  int *aFirst;                    /* Array of first token in each sentence */
210344  const char *zDoc;               /* Document being tokenized */
210345};
210346
210347/*
210348** Add an entry to the Fts5SFinder.aFirst[] array. Grow the array if
210349** necessary. Return SQLITE_OK if successful, or SQLITE_NOMEM if an
210350** error occurs.
210351*/
210352static int fts5SentenceFinderAdd(Fts5SFinder *p, int iAdd){
210353  if( p->nFirstAlloc==p->nFirst ){
210354    int nNew = p->nFirstAlloc ? p->nFirstAlloc*2 : 64;
210355    int *aNew;
210356
210357    aNew = (int*)sqlite3_realloc64(p->aFirst, nNew*sizeof(int));
210358    if( aNew==0 ) return SQLITE_NOMEM;
210359    p->aFirst = aNew;
210360    p->nFirstAlloc = nNew;
210361  }
210362  p->aFirst[p->nFirst++] = iAdd;
210363  return SQLITE_OK;
210364}
210365
210366/*
210367** This function is an xTokenize() callback used by the auxiliary snippet()
210368** function. Its job is to identify tokens that are the first in a sentence.
210369** For each such token, an entry is added to the SFinder.aFirst[] array.
210370*/
210371static int fts5SentenceFinderCb(
210372  void *pContext,                 /* Pointer to HighlightContext object */
210373  int tflags,                     /* Mask of FTS5_TOKEN_* flags */
210374  const char *pToken,             /* Buffer containing token */
210375  int nToken,                     /* Size of token in bytes */
210376  int iStartOff,                  /* Start offset of token */
210377  int iEndOff                     /* End offset of token */
210378){
210379  int rc = SQLITE_OK;
210380
210381  UNUSED_PARAM2(pToken, nToken);
210382  UNUSED_PARAM(iEndOff);
210383
210384  if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
210385    Fts5SFinder *p = (Fts5SFinder*)pContext;
210386    if( p->iPos>0 ){
210387      int i;
210388      char c = 0;
210389      for(i=iStartOff-1; i>=0; i--){
210390        c = p->zDoc[i];
210391        if( c!=' ' && c!='\t' && c!='\n' && c!='\r' ) break;
210392      }
210393      if( i!=iStartOff-1 && (c=='.' || c==':') ){
210394        rc = fts5SentenceFinderAdd(p, p->iPos);
210395      }
210396    }else{
210397      rc = fts5SentenceFinderAdd(p, 0);
210398    }
210399    p->iPos++;
210400  }
210401  return rc;
210402}
210403
210404static int fts5SnippetScore(
210405  const Fts5ExtensionApi *pApi,   /* API offered by current FTS version */
210406  Fts5Context *pFts,              /* First arg to pass to pApi functions */
210407  int nDocsize,                   /* Size of column in tokens */
210408  unsigned char *aSeen,           /* Array with one element per query phrase */
210409  int iCol,                       /* Column to score */
210410  int iPos,                       /* Starting offset to score */
210411  int nToken,                     /* Max tokens per snippet */
210412  int *pnScore,                   /* OUT: Score */
210413  int *piPos                      /* OUT: Adjusted offset */
210414){
210415  int rc;
210416  int i;
210417  int ip = 0;
210418  int ic = 0;
210419  int iOff = 0;
210420  int iFirst = -1;
210421  int nInst;
210422  int nScore = 0;
210423  int iLast = 0;
210424  sqlite3_int64 iEnd = (sqlite3_int64)iPos + nToken;
210425
210426  rc = pApi->xInstCount(pFts, &nInst);
210427  for(i=0; i<nInst && rc==SQLITE_OK; i++){
210428    rc = pApi->xInst(pFts, i, &ip, &ic, &iOff);
210429    if( rc==SQLITE_OK && ic==iCol && iOff>=iPos && iOff<iEnd ){
210430      nScore += (aSeen[ip] ? 1 : 1000);
210431      aSeen[ip] = 1;
210432      if( iFirst<0 ) iFirst = iOff;
210433      iLast = iOff + pApi->xPhraseSize(pFts, ip);
210434    }
210435  }
210436
210437  *pnScore = nScore;
210438  if( piPos ){
210439    sqlite3_int64 iAdj = iFirst - (nToken - (iLast-iFirst)) / 2;
210440    if( (iAdj+nToken)>nDocsize ) iAdj = nDocsize - nToken;
210441    if( iAdj<0 ) iAdj = 0;
210442    *piPos = (int)iAdj;
210443  }
210444
210445  return rc;
210446}
210447
210448/*
210449** Return the value in pVal interpreted as utf-8 text. Except, if pVal
210450** contains a NULL value, return a pointer to a static string zero
210451** bytes in length instead of a NULL pointer.
210452*/
210453static const char *fts5ValueToText(sqlite3_value *pVal){
210454  const char *zRet = (const char*)sqlite3_value_text(pVal);
210455  return zRet ? zRet : "";
210456}
210457
210458/*
210459** Implementation of snippet() function.
210460*/
210461static void fts5SnippetFunction(
210462  const Fts5ExtensionApi *pApi,   /* API offered by current FTS version */
210463  Fts5Context *pFts,              /* First arg to pass to pApi functions */
210464  sqlite3_context *pCtx,          /* Context for returning result/error */
210465  int nVal,                       /* Number of values in apVal[] array */
210466  sqlite3_value **apVal           /* Array of trailing arguments */
210467){
210468  HighlightContext ctx;
210469  int rc = SQLITE_OK;             /* Return code */
210470  int iCol;                       /* 1st argument to snippet() */
210471  const char *zEllips;            /* 4th argument to snippet() */
210472  int nToken;                     /* 5th argument to snippet() */
210473  int nInst = 0;                  /* Number of instance matches this row */
210474  int i;                          /* Used to iterate through instances */
210475  int nPhrase;                    /* Number of phrases in query */
210476  unsigned char *aSeen;           /* Array of "seen instance" flags */
210477  int iBestCol;                   /* Column containing best snippet */
210478  int iBestStart = 0;             /* First token of best snippet */
210479  int nBestScore = 0;             /* Score of best snippet */
210480  int nColSize = 0;               /* Total size of iBestCol in tokens */
210481  Fts5SFinder sFinder;            /* Used to find the beginnings of sentences */
210482  int nCol;
210483
210484  if( nVal!=5 ){
210485    const char *zErr = "wrong number of arguments to function snippet()";
210486    sqlite3_result_error(pCtx, zErr, -1);
210487    return;
210488  }
210489
210490  nCol = pApi->xColumnCount(pFts);
210491  memset(&ctx, 0, sizeof(HighlightContext));
210492  iCol = sqlite3_value_int(apVal[0]);
210493  ctx.zOpen = fts5ValueToText(apVal[1]);
210494  ctx.zClose = fts5ValueToText(apVal[2]);
210495  zEllips = fts5ValueToText(apVal[3]);
210496  nToken = sqlite3_value_int(apVal[4]);
210497
210498  iBestCol = (iCol>=0 ? iCol : 0);
210499  nPhrase = pApi->xPhraseCount(pFts);
210500  aSeen = sqlite3_malloc(nPhrase);
210501  if( aSeen==0 ){
210502    rc = SQLITE_NOMEM;
210503  }
210504  if( rc==SQLITE_OK ){
210505    rc = pApi->xInstCount(pFts, &nInst);
210506  }
210507
210508  memset(&sFinder, 0, sizeof(Fts5SFinder));
210509  for(i=0; i<nCol; i++){
210510    if( iCol<0 || iCol==i ){
210511      int nDoc;
210512      int nDocsize;
210513      int ii;
210514      sFinder.iPos = 0;
210515      sFinder.nFirst = 0;
210516      rc = pApi->xColumnText(pFts, i, &sFinder.zDoc, &nDoc);
210517      if( rc!=SQLITE_OK ) break;
210518      rc = pApi->xTokenize(pFts,
210519          sFinder.zDoc, nDoc, (void*)&sFinder,fts5SentenceFinderCb
210520      );
210521      if( rc!=SQLITE_OK ) break;
210522      rc = pApi->xColumnSize(pFts, i, &nDocsize);
210523      if( rc!=SQLITE_OK ) break;
210524
210525      for(ii=0; rc==SQLITE_OK && ii<nInst; ii++){
210526        int ip, ic, io;
210527        int iAdj;
210528        int nScore;
210529        int jj;
210530
210531        rc = pApi->xInst(pFts, ii, &ip, &ic, &io);
210532        if( ic!=i ) continue;
210533        if( io>nDocsize ) rc = FTS5_CORRUPT;
210534        if( rc!=SQLITE_OK ) continue;
210535        memset(aSeen, 0, nPhrase);
210536        rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
210537            io, nToken, &nScore, &iAdj
210538        );
210539        if( rc==SQLITE_OK && nScore>nBestScore ){
210540          nBestScore = nScore;
210541          iBestCol = i;
210542          iBestStart = iAdj;
210543          nColSize = nDocsize;
210544        }
210545
210546        if( rc==SQLITE_OK && sFinder.nFirst && nDocsize>nToken ){
210547          for(jj=0; jj<(sFinder.nFirst-1); jj++){
210548            if( sFinder.aFirst[jj+1]>io ) break;
210549          }
210550
210551          if( sFinder.aFirst[jj]<io ){
210552            memset(aSeen, 0, nPhrase);
210553            rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
210554              sFinder.aFirst[jj], nToken, &nScore, 0
210555            );
210556
210557            nScore += (sFinder.aFirst[jj]==0 ? 120 : 100);
210558            if( rc==SQLITE_OK && nScore>nBestScore ){
210559              nBestScore = nScore;
210560              iBestCol = i;
210561              iBestStart = sFinder.aFirst[jj];
210562              nColSize = nDocsize;
210563            }
210564          }
210565        }
210566      }
210567    }
210568  }
210569
210570  if( rc==SQLITE_OK ){
210571    rc = pApi->xColumnText(pFts, iBestCol, &ctx.zIn, &ctx.nIn);
210572  }
210573  if( rc==SQLITE_OK && nColSize==0 ){
210574    rc = pApi->xColumnSize(pFts, iBestCol, &nColSize);
210575  }
210576  if( ctx.zIn ){
210577    if( rc==SQLITE_OK ){
210578      rc = fts5CInstIterInit(pApi, pFts, iBestCol, &ctx.iter);
210579    }
210580
210581    ctx.iRangeStart = iBestStart;
210582    ctx.iRangeEnd = iBestStart + nToken - 1;
210583
210584    if( iBestStart>0 ){
210585      fts5HighlightAppend(&rc, &ctx, zEllips, -1);
210586    }
210587
210588    /* Advance iterator ctx.iter so that it points to the first coalesced
210589    ** phrase instance at or following position iBestStart. */
210590    while( ctx.iter.iStart>=0 && ctx.iter.iStart<iBestStart && rc==SQLITE_OK ){
210591      rc = fts5CInstIterNext(&ctx.iter);
210592    }
210593
210594    if( rc==SQLITE_OK ){
210595      rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
210596    }
210597    if( ctx.iRangeEnd>=(nColSize-1) ){
210598      fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
210599    }else{
210600      fts5HighlightAppend(&rc, &ctx, zEllips, -1);
210601    }
210602  }
210603  if( rc==SQLITE_OK ){
210604    sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT);
210605  }else{
210606    sqlite3_result_error_code(pCtx, rc);
210607  }
210608  sqlite3_free(ctx.zOut);
210609  sqlite3_free(aSeen);
210610  sqlite3_free(sFinder.aFirst);
210611}
210612
210613/************************************************************************/
210614
210615/*
210616** The first time the bm25() function is called for a query, an instance
210617** of the following structure is allocated and populated.
210618*/
210619typedef struct Fts5Bm25Data Fts5Bm25Data;
210620struct Fts5Bm25Data {
210621  int nPhrase;                    /* Number of phrases in query */
210622  double avgdl;                   /* Average number of tokens in each row */
210623  double *aIDF;                   /* IDF for each phrase */
210624  double *aFreq;                  /* Array used to calculate phrase freq. */
210625};
210626
210627/*
210628** Callback used by fts5Bm25GetData() to count the number of rows in the
210629** table matched by each individual phrase within the query.
210630*/
210631static int fts5CountCb(
210632  const Fts5ExtensionApi *pApi,
210633  Fts5Context *pFts,
210634  void *pUserData                 /* Pointer to sqlite3_int64 variable */
210635){
210636  sqlite3_int64 *pn = (sqlite3_int64*)pUserData;
210637  UNUSED_PARAM2(pApi, pFts);
210638  (*pn)++;
210639  return SQLITE_OK;
210640}
210641
210642/*
210643** Set *ppData to point to the Fts5Bm25Data object for the current query.
210644** If the object has not already been allocated, allocate and populate it
210645** now.
210646*/
210647static int fts5Bm25GetData(
210648  const Fts5ExtensionApi *pApi,
210649  Fts5Context *pFts,
210650  Fts5Bm25Data **ppData           /* OUT: bm25-data object for this query */
210651){
210652  int rc = SQLITE_OK;             /* Return code */
210653  Fts5Bm25Data *p;                /* Object to return */
210654
210655  p = pApi->xGetAuxdata(pFts, 0);
210656  if( p==0 ){
210657    int nPhrase;                  /* Number of phrases in query */
210658    sqlite3_int64 nRow = 0;       /* Number of rows in table */
210659    sqlite3_int64 nToken = 0;     /* Number of tokens in table */
210660    sqlite3_int64 nByte;          /* Bytes of space to allocate */
210661    int i;
210662
210663    /* Allocate the Fts5Bm25Data object */
210664    nPhrase = pApi->xPhraseCount(pFts);
210665    nByte = sizeof(Fts5Bm25Data) + nPhrase*2*sizeof(double);
210666    p = (Fts5Bm25Data*)sqlite3_malloc64(nByte);
210667    if( p==0 ){
210668      rc = SQLITE_NOMEM;
210669    }else{
210670      memset(p, 0, (size_t)nByte);
210671      p->nPhrase = nPhrase;
210672      p->aIDF = (double*)&p[1];
210673      p->aFreq = &p->aIDF[nPhrase];
210674    }
210675
210676    /* Calculate the average document length for this FTS5 table */
210677    if( rc==SQLITE_OK ) rc = pApi->xRowCount(pFts, &nRow);
210678    assert( rc!=SQLITE_OK || nRow>0 );
210679    if( rc==SQLITE_OK ) rc = pApi->xColumnTotalSize(pFts, -1, &nToken);
210680    if( rc==SQLITE_OK ) p->avgdl = (double)nToken  / (double)nRow;
210681
210682    /* Calculate an IDF for each phrase in the query */
210683    for(i=0; rc==SQLITE_OK && i<nPhrase; i++){
210684      sqlite3_int64 nHit = 0;
210685      rc = pApi->xQueryPhrase(pFts, i, (void*)&nHit, fts5CountCb);
210686      if( rc==SQLITE_OK ){
210687        /* Calculate the IDF (Inverse Document Frequency) for phrase i.
210688        ** This is done using the standard BM25 formula as found on wikipedia:
210689        **
210690        **   IDF = log( (N - nHit + 0.5) / (nHit + 0.5) )
210691        **
210692        ** where "N" is the total number of documents in the set and nHit
210693        ** is the number that contain at least one instance of the phrase
210694        ** under consideration.
210695        **
210696        ** The problem with this is that if (N < 2*nHit), the IDF is
210697        ** negative. Which is undesirable. So the mimimum allowable IDF is
210698        ** (1e-6) - roughly the same as a term that appears in just over
210699        ** half of set of 5,000,000 documents.  */
210700        double idf = log( (nRow - nHit + 0.5) / (nHit + 0.5) );
210701        if( idf<=0.0 ) idf = 1e-6;
210702        p->aIDF[i] = idf;
210703      }
210704    }
210705
210706    if( rc!=SQLITE_OK ){
210707      sqlite3_free(p);
210708    }else{
210709      rc = pApi->xSetAuxdata(pFts, p, sqlite3_free);
210710    }
210711    if( rc!=SQLITE_OK ) p = 0;
210712  }
210713  *ppData = p;
210714  return rc;
210715}
210716
210717/*
210718** Implementation of bm25() function.
210719*/
210720static void fts5Bm25Function(
210721  const Fts5ExtensionApi *pApi,   /* API offered by current FTS version */
210722  Fts5Context *pFts,              /* First arg to pass to pApi functions */
210723  sqlite3_context *pCtx,          /* Context for returning result/error */
210724  int nVal,                       /* Number of values in apVal[] array */
210725  sqlite3_value **apVal           /* Array of trailing arguments */
210726){
210727  const double k1 = 1.2;          /* Constant "k1" from BM25 formula */
210728  const double b = 0.75;          /* Constant "b" from BM25 formula */
210729  int rc = SQLITE_OK;             /* Error code */
210730  double score = 0.0;             /* SQL function return value */
210731  Fts5Bm25Data *pData;            /* Values allocated/calculated once only */
210732  int i;                          /* Iterator variable */
210733  int nInst = 0;                  /* Value returned by xInstCount() */
210734  double D = 0.0;                 /* Total number of tokens in row */
210735  double *aFreq = 0;              /* Array of phrase freq. for current row */
210736
210737  /* Calculate the phrase frequency (symbol "f(qi,D)" in the documentation)
210738  ** for each phrase in the query for the current row. */
210739  rc = fts5Bm25GetData(pApi, pFts, &pData);
210740  if( rc==SQLITE_OK ){
210741    aFreq = pData->aFreq;
210742    memset(aFreq, 0, sizeof(double) * pData->nPhrase);
210743    rc = pApi->xInstCount(pFts, &nInst);
210744  }
210745  for(i=0; rc==SQLITE_OK && i<nInst; i++){
210746    int ip; int ic; int io;
210747    rc = pApi->xInst(pFts, i, &ip, &ic, &io);
210748    if( rc==SQLITE_OK ){
210749      double w = (nVal > ic) ? sqlite3_value_double(apVal[ic]) : 1.0;
210750      aFreq[ip] += w;
210751    }
210752  }
210753
210754  /* Figure out the total size of the current row in tokens. */
210755  if( rc==SQLITE_OK ){
210756    int nTok;
210757    rc = pApi->xColumnSize(pFts, -1, &nTok);
210758    D = (double)nTok;
210759  }
210760
210761  /* Determine the BM25 score for the current row. */
210762  for(i=0; rc==SQLITE_OK && i<pData->nPhrase; i++){
210763    score += pData->aIDF[i] * (
210764      ( aFreq[i] * (k1 + 1.0) ) /
210765      ( aFreq[i] + k1 * (1 - b + b * D / pData->avgdl) )
210766    );
210767  }
210768
210769  /* If no error has occurred, return the calculated score. Otherwise,
210770  ** throw an SQL exception.  */
210771  if( rc==SQLITE_OK ){
210772    sqlite3_result_double(pCtx, -1.0 * score);
210773  }else{
210774    sqlite3_result_error_code(pCtx, rc);
210775  }
210776}
210777
210778static int sqlite3Fts5AuxInit(fts5_api *pApi){
210779  struct Builtin {
210780    const char *zFunc;            /* Function name (nul-terminated) */
210781    void *pUserData;              /* User-data pointer */
210782    fts5_extension_function xFunc;/* Callback function */
210783    void (*xDestroy)(void*);      /* Destructor function */
210784  } aBuiltin [] = {
210785    { "snippet",   0, fts5SnippetFunction, 0 },
210786    { "highlight", 0, fts5HighlightFunction, 0 },
210787    { "bm25",      0, fts5Bm25Function,    0 },
210788  };
210789  int rc = SQLITE_OK;             /* Return code */
210790  int i;                          /* To iterate through builtin functions */
210791
210792  for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
210793    rc = pApi->xCreateFunction(pApi,
210794        aBuiltin[i].zFunc,
210795        aBuiltin[i].pUserData,
210796        aBuiltin[i].xFunc,
210797        aBuiltin[i].xDestroy
210798    );
210799  }
210800
210801  return rc;
210802}
210803
210804/*
210805** 2014 May 31
210806**
210807** The author disclaims copyright to this source code.  In place of
210808** a legal notice, here is a blessing:
210809**
210810**    May you do good and not evil.
210811**    May you find forgiveness for yourself and forgive others.
210812**    May you share freely, never taking more than you give.
210813**
210814******************************************************************************
210815*/
210816
210817
210818
210819/* #include "fts5Int.h" */
210820
210821static int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){
210822  if( (u32)pBuf->nSpace<nByte ){
210823    u64 nNew = pBuf->nSpace ? pBuf->nSpace : 64;
210824    u8 *pNew;
210825    while( nNew<nByte ){
210826      nNew = nNew * 2;
210827    }
210828    pNew = sqlite3_realloc64(pBuf->p, nNew);
210829    if( pNew==0 ){
210830      *pRc = SQLITE_NOMEM;
210831      return 1;
210832    }else{
210833      pBuf->nSpace = (int)nNew;
210834      pBuf->p = pNew;
210835    }
210836  }
210837  return 0;
210838}
210839
210840
210841/*
210842** Encode value iVal as an SQLite varint and append it to the buffer object
210843** pBuf. If an OOM error occurs, set the error code in p.
210844*/
210845static void sqlite3Fts5BufferAppendVarint(int *pRc, Fts5Buffer *pBuf, i64 iVal){
210846  if( fts5BufferGrow(pRc, pBuf, 9) ) return;
210847  pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iVal);
210848}
210849
210850static void sqlite3Fts5Put32(u8 *aBuf, int iVal){
210851  aBuf[0] = (iVal>>24) & 0x00FF;
210852  aBuf[1] = (iVal>>16) & 0x00FF;
210853  aBuf[2] = (iVal>> 8) & 0x00FF;
210854  aBuf[3] = (iVal>> 0) & 0x00FF;
210855}
210856
210857static int sqlite3Fts5Get32(const u8 *aBuf){
210858  return (int)((((u32)aBuf[0])<<24) + (aBuf[1]<<16) + (aBuf[2]<<8) + aBuf[3]);
210859}
210860
210861/*
210862** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set
210863** the error code in p. If an error has already occurred when this function
210864** is called, it is a no-op.
210865*/
210866static void sqlite3Fts5BufferAppendBlob(
210867  int *pRc,
210868  Fts5Buffer *pBuf,
210869  u32 nData,
210870  const u8 *pData
210871){
210872  assert_nc( *pRc || nData>=0 );
210873  if( nData ){
210874    if( fts5BufferGrow(pRc, pBuf, nData) ) return;
210875    memcpy(&pBuf->p[pBuf->n], pData, nData);
210876    pBuf->n += nData;
210877  }
210878}
210879
210880/*
210881** Append the nul-terminated string zStr to the buffer pBuf. This function
210882** ensures that the byte following the buffer data is set to 0x00, even
210883** though this byte is not included in the pBuf->n count.
210884*/
210885static void sqlite3Fts5BufferAppendString(
210886  int *pRc,
210887  Fts5Buffer *pBuf,
210888  const char *zStr
210889){
210890  int nStr = (int)strlen(zStr);
210891  sqlite3Fts5BufferAppendBlob(pRc, pBuf, nStr+1, (const u8*)zStr);
210892  pBuf->n--;
210893}
210894
210895/*
210896** Argument zFmt is a printf() style format string. This function performs
210897** the printf() style processing, then appends the results to buffer pBuf.
210898**
210899** Like sqlite3Fts5BufferAppendString(), this function ensures that the byte
210900** following the buffer data is set to 0x00, even though this byte is not
210901** included in the pBuf->n count.
210902*/
210903static void sqlite3Fts5BufferAppendPrintf(
210904  int *pRc,
210905  Fts5Buffer *pBuf,
210906  char *zFmt, ...
210907){
210908  if( *pRc==SQLITE_OK ){
210909    char *zTmp;
210910    va_list ap;
210911    va_start(ap, zFmt);
210912    zTmp = sqlite3_vmprintf(zFmt, ap);
210913    va_end(ap);
210914
210915    if( zTmp==0 ){
210916      *pRc = SQLITE_NOMEM;
210917    }else{
210918      sqlite3Fts5BufferAppendString(pRc, pBuf, zTmp);
210919      sqlite3_free(zTmp);
210920    }
210921  }
210922}
210923
210924static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...){
210925  char *zRet = 0;
210926  if( *pRc==SQLITE_OK ){
210927    va_list ap;
210928    va_start(ap, zFmt);
210929    zRet = sqlite3_vmprintf(zFmt, ap);
210930    va_end(ap);
210931    if( zRet==0 ){
210932      *pRc = SQLITE_NOMEM;
210933    }
210934  }
210935  return zRet;
210936}
210937
210938
210939/*
210940** Free any buffer allocated by pBuf. Zero the structure before returning.
210941*/
210942static void sqlite3Fts5BufferFree(Fts5Buffer *pBuf){
210943  sqlite3_free(pBuf->p);
210944  memset(pBuf, 0, sizeof(Fts5Buffer));
210945}
210946
210947/*
210948** Zero the contents of the buffer object. But do not free the associated
210949** memory allocation.
210950*/
210951static void sqlite3Fts5BufferZero(Fts5Buffer *pBuf){
210952  pBuf->n = 0;
210953}
210954
210955/*
210956** Set the buffer to contain nData/pData. If an OOM error occurs, leave an
210957** the error code in p. If an error has already occurred when this function
210958** is called, it is a no-op.
210959*/
210960static void sqlite3Fts5BufferSet(
210961  int *pRc,
210962  Fts5Buffer *pBuf,
210963  int nData,
210964  const u8 *pData
210965){
210966  pBuf->n = 0;
210967  sqlite3Fts5BufferAppendBlob(pRc, pBuf, nData, pData);
210968}
210969
210970static int sqlite3Fts5PoslistNext64(
210971  const u8 *a, int n,             /* Buffer containing poslist */
210972  int *pi,                        /* IN/OUT: Offset within a[] */
210973  i64 *piOff                      /* IN/OUT: Current offset */
210974){
210975  int i = *pi;
210976  if( i>=n ){
210977    /* EOF */
210978    *piOff = -1;
210979    return 1;
210980  }else{
210981    i64 iOff = *piOff;
210982    int iVal;
210983    fts5FastGetVarint32(a, i, iVal);
210984    if( iVal<=1 ){
210985      if( iVal==0 ){
210986        *pi = i;
210987        return 0;
210988      }
210989      fts5FastGetVarint32(a, i, iVal);
210990      iOff = ((i64)iVal) << 32;
210991      fts5FastGetVarint32(a, i, iVal);
210992      if( iVal<2 ){
210993        /* This is a corrupt record. So stop parsing it here. */
210994        *piOff = -1;
210995        return 1;
210996      }
210997    }
210998    *piOff = iOff + ((iVal-2) & 0x7FFFFFFF);
210999    *pi = i;
211000    return 0;
211001  }
211002}
211003
211004
211005/*
211006** Advance the iterator object passed as the only argument. Return true
211007** if the iterator reaches EOF, or false otherwise.
211008*/
211009static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader *pIter){
211010  if( sqlite3Fts5PoslistNext64(pIter->a, pIter->n, &pIter->i, &pIter->iPos) ){
211011    pIter->bEof = 1;
211012  }
211013  return pIter->bEof;
211014}
211015
211016static int sqlite3Fts5PoslistReaderInit(
211017  const u8 *a, int n,             /* Poslist buffer to iterate through */
211018  Fts5PoslistReader *pIter        /* Iterator object to initialize */
211019){
211020  memset(pIter, 0, sizeof(*pIter));
211021  pIter->a = a;
211022  pIter->n = n;
211023  sqlite3Fts5PoslistReaderNext(pIter);
211024  return pIter->bEof;
211025}
211026
211027/*
211028** Append position iPos to the position list being accumulated in buffer
211029** pBuf, which must be already be large enough to hold the new data.
211030** The previous position written to this list is *piPrev. *piPrev is set
211031** to iPos before returning.
211032*/
211033static void sqlite3Fts5PoslistSafeAppend(
211034  Fts5Buffer *pBuf,
211035  i64 *piPrev,
211036  i64 iPos
211037){
211038  static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32;
211039  if( (iPos & colmask) != (*piPrev & colmask) ){
211040    pBuf->p[pBuf->n++] = 1;
211041    pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32));
211042    *piPrev = (iPos & colmask);
211043  }
211044  pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-*piPrev)+2);
211045  *piPrev = iPos;
211046}
211047
211048static int sqlite3Fts5PoslistWriterAppend(
211049  Fts5Buffer *pBuf,
211050  Fts5PoslistWriter *pWriter,
211051  i64 iPos
211052){
211053  int rc = 0;   /* Initialized only to suppress erroneous warning from Clang */
211054  if( fts5BufferGrow(&rc, pBuf, 5+5+5) ) return rc;
211055  sqlite3Fts5PoslistSafeAppend(pBuf, &pWriter->iPrev, iPos);
211056  return SQLITE_OK;
211057}
211058
211059static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte){
211060  void *pRet = 0;
211061  if( *pRc==SQLITE_OK ){
211062    pRet = sqlite3_malloc64(nByte);
211063    if( pRet==0 ){
211064      if( nByte>0 ) *pRc = SQLITE_NOMEM;
211065    }else{
211066      memset(pRet, 0, (size_t)nByte);
211067    }
211068  }
211069  return pRet;
211070}
211071
211072/*
211073** Return a nul-terminated copy of the string indicated by pIn. If nIn
211074** is non-negative, then it is the length of the string in bytes. Otherwise,
211075** the length of the string is determined using strlen().
211076**
211077** It is the responsibility of the caller to eventually free the returned
211078** buffer using sqlite3_free(). If an OOM error occurs, NULL is returned.
211079*/
211080static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn){
211081  char *zRet = 0;
211082  if( *pRc==SQLITE_OK ){
211083    if( nIn<0 ){
211084      nIn = (int)strlen(pIn);
211085    }
211086    zRet = (char*)sqlite3_malloc(nIn+1);
211087    if( zRet ){
211088      memcpy(zRet, pIn, nIn);
211089      zRet[nIn] = '\0';
211090    }else{
211091      *pRc = SQLITE_NOMEM;
211092    }
211093  }
211094  return zRet;
211095}
211096
211097
211098/*
211099** Return true if character 't' may be part of an FTS5 bareword, or false
211100** otherwise. Characters that may be part of barewords:
211101**
211102**   * All non-ASCII characters,
211103**   * The 52 upper and lower case ASCII characters, and
211104**   * The 10 integer ASCII characters.
211105**   * The underscore character "_" (0x5F).
211106**   * The unicode "subsitute" character (0x1A).
211107*/
211108static int sqlite3Fts5IsBareword(char t){
211109  u8 aBareword[128] = {
211110    0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 0, 0, 0, 0, 0,   /* 0x00 .. 0x0F */
211111    0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 1, 0, 0, 0, 0, 0,   /* 0x10 .. 0x1F */
211112    0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 0, 0, 0, 0, 0,   /* 0x20 .. 0x2F */
211113    1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 0, 0, 0, 0, 0, 0,   /* 0x30 .. 0x3F */
211114    0, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 1, 1, 1, 1, 1,   /* 0x40 .. 0x4F */
211115    1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 0, 0, 0, 0, 1,   /* 0x50 .. 0x5F */
211116    0, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 1, 1, 1, 1, 1,   /* 0x60 .. 0x6F */
211117    1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 0, 0, 0, 0, 0    /* 0x70 .. 0x7F */
211118  };
211119
211120  return (t & 0x80) || aBareword[(int)t];
211121}
211122
211123
211124/*************************************************************************
211125*/
211126typedef struct Fts5TermsetEntry Fts5TermsetEntry;
211127struct Fts5TermsetEntry {
211128  char *pTerm;
211129  int nTerm;
211130  int iIdx;                       /* Index (main or aPrefix[] entry) */
211131  Fts5TermsetEntry *pNext;
211132};
211133
211134struct Fts5Termset {
211135  Fts5TermsetEntry *apHash[512];
211136};
211137
211138static int sqlite3Fts5TermsetNew(Fts5Termset **pp){
211139  int rc = SQLITE_OK;
211140  *pp = sqlite3Fts5MallocZero(&rc, sizeof(Fts5Termset));
211141  return rc;
211142}
211143
211144static int sqlite3Fts5TermsetAdd(
211145  Fts5Termset *p,
211146  int iIdx,
211147  const char *pTerm, int nTerm,
211148  int *pbPresent
211149){
211150  int rc = SQLITE_OK;
211151  *pbPresent = 0;
211152  if( p ){
211153    int i;
211154    u32 hash = 13;
211155    Fts5TermsetEntry *pEntry;
211156
211157    /* Calculate a hash value for this term. This is the same hash checksum
211158    ** used by the fts5_hash.c module. This is not important for correct
211159    ** operation of the module, but is necessary to ensure that some tests
211160    ** designed to produce hash table collisions really do work.  */
211161    for(i=nTerm-1; i>=0; i--){
211162      hash = (hash << 3) ^ hash ^ pTerm[i];
211163    }
211164    hash = (hash << 3) ^ hash ^ iIdx;
211165    hash = hash % ArraySize(p->apHash);
211166
211167    for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){
211168      if( pEntry->iIdx==iIdx
211169          && pEntry->nTerm==nTerm
211170          && memcmp(pEntry->pTerm, pTerm, nTerm)==0
211171      ){
211172        *pbPresent = 1;
211173        break;
211174      }
211175    }
211176
211177    if( pEntry==0 ){
211178      pEntry = sqlite3Fts5MallocZero(&rc, sizeof(Fts5TermsetEntry) + nTerm);
211179      if( pEntry ){
211180        pEntry->pTerm = (char*)&pEntry[1];
211181        pEntry->nTerm = nTerm;
211182        pEntry->iIdx = iIdx;
211183        memcpy(pEntry->pTerm, pTerm, nTerm);
211184        pEntry->pNext = p->apHash[hash];
211185        p->apHash[hash] = pEntry;
211186      }
211187    }
211188  }
211189
211190  return rc;
211191}
211192
211193static void sqlite3Fts5TermsetFree(Fts5Termset *p){
211194  if( p ){
211195    u32 i;
211196    for(i=0; i<ArraySize(p->apHash); i++){
211197      Fts5TermsetEntry *pEntry = p->apHash[i];
211198      while( pEntry ){
211199        Fts5TermsetEntry *pDel = pEntry;
211200        pEntry = pEntry->pNext;
211201        sqlite3_free(pDel);
211202      }
211203    }
211204    sqlite3_free(p);
211205  }
211206}
211207
211208/*
211209** 2014 Jun 09
211210**
211211** The author disclaims copyright to this source code.  In place of
211212** a legal notice, here is a blessing:
211213**
211214**    May you do good and not evil.
211215**    May you find forgiveness for yourself and forgive others.
211216**    May you share freely, never taking more than you give.
211217**
211218******************************************************************************
211219**
211220** This is an SQLite module implementing full-text search.
211221*/
211222
211223
211224/* #include "fts5Int.h" */
211225
211226#define FTS5_DEFAULT_PAGE_SIZE   4050
211227#define FTS5_DEFAULT_AUTOMERGE      4
211228#define FTS5_DEFAULT_USERMERGE      4
211229#define FTS5_DEFAULT_CRISISMERGE   16
211230#define FTS5_DEFAULT_HASHSIZE    (1024*1024)
211231
211232/* Maximum allowed page size */
211233#define FTS5_MAX_PAGE_SIZE (64*1024)
211234
211235static int fts5_iswhitespace(char x){
211236  return (x==' ');
211237}
211238
211239static int fts5_isopenquote(char x){
211240  return (x=='"' || x=='\'' || x=='[' || x=='`');
211241}
211242
211243/*
211244** Argument pIn points to a character that is part of a nul-terminated
211245** string. Return a pointer to the first character following *pIn in
211246** the string that is not a white-space character.
211247*/
211248static const char *fts5ConfigSkipWhitespace(const char *pIn){
211249  const char *p = pIn;
211250  if( p ){
211251    while( fts5_iswhitespace(*p) ){ p++; }
211252  }
211253  return p;
211254}
211255
211256/*
211257** Argument pIn points to a character that is part of a nul-terminated
211258** string. Return a pointer to the first character following *pIn in
211259** the string that is not a "bareword" character.
211260*/
211261static const char *fts5ConfigSkipBareword(const char *pIn){
211262  const char *p = pIn;
211263  while ( sqlite3Fts5IsBareword(*p) ) p++;
211264  if( p==pIn ) p = 0;
211265  return p;
211266}
211267
211268static int fts5_isdigit(char a){
211269  return (a>='0' && a<='9');
211270}
211271
211272
211273
211274static const char *fts5ConfigSkipLiteral(const char *pIn){
211275  const char *p = pIn;
211276  switch( *p ){
211277    case 'n': case 'N':
211278      if( sqlite3_strnicmp("null", p, 4)==0 ){
211279        p = &p[4];
211280      }else{
211281        p = 0;
211282      }
211283      break;
211284
211285    case 'x': case 'X':
211286      p++;
211287      if( *p=='\'' ){
211288        p++;
211289        while( (*p>='a' && *p<='f')
211290            || (*p>='A' && *p<='F')
211291            || (*p>='0' && *p<='9')
211292            ){
211293          p++;
211294        }
211295        if( *p=='\'' && 0==((p-pIn)%2) ){
211296          p++;
211297        }else{
211298          p = 0;
211299        }
211300      }else{
211301        p = 0;
211302      }
211303      break;
211304
211305    case '\'':
211306      p++;
211307      while( p ){
211308        if( *p=='\'' ){
211309          p++;
211310          if( *p!='\'' ) break;
211311        }
211312        p++;
211313        if( *p==0 ) p = 0;
211314      }
211315      break;
211316
211317    default:
211318      /* maybe a number */
211319      if( *p=='+' || *p=='-' ) p++;
211320      while( fts5_isdigit(*p) ) p++;
211321
211322      /* At this point, if the literal was an integer, the parse is
211323      ** finished. Or, if it is a floating point value, it may continue
211324      ** with either a decimal point or an 'E' character. */
211325      if( *p=='.' && fts5_isdigit(p[1]) ){
211326        p += 2;
211327        while( fts5_isdigit(*p) ) p++;
211328      }
211329      if( p==pIn ) p = 0;
211330
211331      break;
211332  }
211333
211334  return p;
211335}
211336
211337/*
211338** The first character of the string pointed to by argument z is guaranteed
211339** to be an open-quote character (see function fts5_isopenquote()).
211340**
211341** This function searches for the corresponding close-quote character within
211342** the string and, if found, dequotes the string in place and adds a new
211343** nul-terminator byte.
211344**
211345** If the close-quote is found, the value returned is the byte offset of
211346** the character immediately following it. Or, if the close-quote is not
211347** found, -1 is returned. If -1 is returned, the buffer is left in an
211348** undefined state.
211349*/
211350static int fts5Dequote(char *z){
211351  char q;
211352  int iIn = 1;
211353  int iOut = 0;
211354  q = z[0];
211355
211356  /* Set stack variable q to the close-quote character */
211357  assert( q=='[' || q=='\'' || q=='"' || q=='`' );
211358  if( q=='[' ) q = ']';
211359
211360  while( z[iIn] ){
211361    if( z[iIn]==q ){
211362      if( z[iIn+1]!=q ){
211363        /* Character iIn was the close quote. */
211364        iIn++;
211365        break;
211366      }else{
211367        /* Character iIn and iIn+1 form an escaped quote character. Skip
211368        ** the input cursor past both and copy a single quote character
211369        ** to the output buffer. */
211370        iIn += 2;
211371        z[iOut++] = q;
211372      }
211373    }else{
211374      z[iOut++] = z[iIn++];
211375    }
211376  }
211377
211378  z[iOut] = '\0';
211379  return iIn;
211380}
211381
211382/*
211383** Convert an SQL-style quoted string into a normal string by removing
211384** the quote characters.  The conversion is done in-place.  If the
211385** input does not begin with a quote character, then this routine
211386** is a no-op.
211387**
211388** Examples:
211389**
211390**     "abc"   becomes   abc
211391**     'xyz'   becomes   xyz
211392**     [pqr]   becomes   pqr
211393**     `mno`   becomes   mno
211394*/
211395static void sqlite3Fts5Dequote(char *z){
211396  char quote;                     /* Quote character (if any ) */
211397
211398  assert( 0==fts5_iswhitespace(z[0]) );
211399  quote = z[0];
211400  if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
211401    fts5Dequote(z);
211402  }
211403}
211404
211405
211406struct Fts5Enum {
211407  const char *zName;
211408  int eVal;
211409};
211410typedef struct Fts5Enum Fts5Enum;
211411
211412static int fts5ConfigSetEnum(
211413  const Fts5Enum *aEnum,
211414  const char *zEnum,
211415  int *peVal
211416){
211417  int nEnum = (int)strlen(zEnum);
211418  int i;
211419  int iVal = -1;
211420
211421  for(i=0; aEnum[i].zName; i++){
211422    if( sqlite3_strnicmp(aEnum[i].zName, zEnum, nEnum)==0 ){
211423      if( iVal>=0 ) return SQLITE_ERROR;
211424      iVal = aEnum[i].eVal;
211425    }
211426  }
211427
211428  *peVal = iVal;
211429  return iVal<0 ? SQLITE_ERROR : SQLITE_OK;
211430}
211431
211432/*
211433** Parse a "special" CREATE VIRTUAL TABLE directive and update
211434** configuration object pConfig as appropriate.
211435**
211436** If successful, object pConfig is updated and SQLITE_OK returned. If
211437** an error occurs, an SQLite error code is returned and an error message
211438** may be left in *pzErr. It is the responsibility of the caller to
211439** eventually free any such error message using sqlite3_free().
211440*/
211441static int fts5ConfigParseSpecial(
211442  Fts5Global *pGlobal,
211443  Fts5Config *pConfig,            /* Configuration object to update */
211444  const char *zCmd,               /* Special command to parse */
211445  const char *zArg,               /* Argument to parse */
211446  char **pzErr                    /* OUT: Error message */
211447){
211448  int rc = SQLITE_OK;
211449  int nCmd = (int)strlen(zCmd);
211450  if( sqlite3_strnicmp("prefix", zCmd, nCmd)==0 ){
211451    const int nByte = sizeof(int) * FTS5_MAX_PREFIX_INDEXES;
211452    const char *p;
211453    int bFirst = 1;
211454    if( pConfig->aPrefix==0 ){
211455      pConfig->aPrefix = sqlite3Fts5MallocZero(&rc, nByte);
211456      if( rc ) return rc;
211457    }
211458
211459    p = zArg;
211460    while( 1 ){
211461      int nPre = 0;
211462
211463      while( p[0]==' ' ) p++;
211464      if( bFirst==0 && p[0]==',' ){
211465        p++;
211466        while( p[0]==' ' ) p++;
211467      }else if( p[0]=='\0' ){
211468        break;
211469      }
211470      if( p[0]<'0' || p[0]>'9' ){
211471        *pzErr = sqlite3_mprintf("malformed prefix=... directive");
211472        rc = SQLITE_ERROR;
211473        break;
211474      }
211475
211476      if( pConfig->nPrefix==FTS5_MAX_PREFIX_INDEXES ){
211477        *pzErr = sqlite3_mprintf(
211478            "too many prefix indexes (max %d)", FTS5_MAX_PREFIX_INDEXES
211479        );
211480        rc = SQLITE_ERROR;
211481        break;
211482      }
211483
211484      while( p[0]>='0' && p[0]<='9' && nPre<1000 ){
211485        nPre = nPre*10 + (p[0] - '0');
211486        p++;
211487      }
211488
211489      if( nPre<=0 || nPre>=1000 ){
211490        *pzErr = sqlite3_mprintf("prefix length out of range (max 999)");
211491        rc = SQLITE_ERROR;
211492        break;
211493      }
211494
211495      pConfig->aPrefix[pConfig->nPrefix] = nPre;
211496      pConfig->nPrefix++;
211497      bFirst = 0;
211498    }
211499    assert( pConfig->nPrefix<=FTS5_MAX_PREFIX_INDEXES );
211500    return rc;
211501  }
211502
211503  if( sqlite3_strnicmp("tokenize", zCmd, nCmd)==0 ){
211504    const char *p = (const char*)zArg;
211505    sqlite3_int64 nArg = strlen(zArg) + 1;
211506    char **azArg = sqlite3Fts5MallocZero(&rc, sizeof(char*) * nArg);
211507    char *pDel = sqlite3Fts5MallocZero(&rc, nArg * 2);
211508    char *pSpace = pDel;
211509
211510    if( azArg && pSpace ){
211511      if( pConfig->pTok ){
211512        *pzErr = sqlite3_mprintf("multiple tokenize=... directives");
211513        rc = SQLITE_ERROR;
211514      }else{
211515        for(nArg=0; p && *p; nArg++){
211516          const char *p2 = fts5ConfigSkipWhitespace(p);
211517          if( *p2=='\'' ){
211518            p = fts5ConfigSkipLiteral(p2);
211519          }else{
211520            p = fts5ConfigSkipBareword(p2);
211521          }
211522          if( p ){
211523            memcpy(pSpace, p2, p-p2);
211524            azArg[nArg] = pSpace;
211525            sqlite3Fts5Dequote(pSpace);
211526            pSpace += (p - p2) + 1;
211527            p = fts5ConfigSkipWhitespace(p);
211528          }
211529        }
211530        if( p==0 ){
211531          *pzErr = sqlite3_mprintf("parse error in tokenize directive");
211532          rc = SQLITE_ERROR;
211533        }else{
211534          rc = sqlite3Fts5GetTokenizer(pGlobal,
211535              (const char**)azArg, (int)nArg, &pConfig->pTok, &pConfig->pTokApi,
211536              pzErr
211537          );
211538        }
211539      }
211540    }
211541
211542    sqlite3_free(azArg);
211543    sqlite3_free(pDel);
211544    return rc;
211545  }
211546
211547  if( sqlite3_strnicmp("content", zCmd, nCmd)==0 ){
211548    if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
211549      *pzErr = sqlite3_mprintf("multiple content=... directives");
211550      rc = SQLITE_ERROR;
211551    }else{
211552      if( zArg[0] ){
211553        pConfig->eContent = FTS5_CONTENT_EXTERNAL;
211554        pConfig->zContent = sqlite3Fts5Mprintf(&rc, "%Q.%Q", pConfig->zDb,zArg);
211555      }else{
211556        pConfig->eContent = FTS5_CONTENT_NONE;
211557      }
211558    }
211559    return rc;
211560  }
211561
211562  if( sqlite3_strnicmp("content_rowid", zCmd, nCmd)==0 ){
211563    if( pConfig->zContentRowid ){
211564      *pzErr = sqlite3_mprintf("multiple content_rowid=... directives");
211565      rc = SQLITE_ERROR;
211566    }else{
211567      pConfig->zContentRowid = sqlite3Fts5Strndup(&rc, zArg, -1);
211568    }
211569    return rc;
211570  }
211571
211572  if( sqlite3_strnicmp("columnsize", zCmd, nCmd)==0 ){
211573    if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
211574      *pzErr = sqlite3_mprintf("malformed columnsize=... directive");
211575      rc = SQLITE_ERROR;
211576    }else{
211577      pConfig->bColumnsize = (zArg[0]=='1');
211578    }
211579    return rc;
211580  }
211581
211582  if( sqlite3_strnicmp("detail", zCmd, nCmd)==0 ){
211583    const Fts5Enum aDetail[] = {
211584      { "none", FTS5_DETAIL_NONE },
211585      { "full", FTS5_DETAIL_FULL },
211586      { "columns", FTS5_DETAIL_COLUMNS },
211587      { 0, 0 }
211588    };
211589
211590    if( (rc = fts5ConfigSetEnum(aDetail, zArg, &pConfig->eDetail)) ){
211591      *pzErr = sqlite3_mprintf("malformed detail=... directive");
211592    }
211593    return rc;
211594  }
211595
211596  *pzErr = sqlite3_mprintf("unrecognized option: \"%.*s\"", nCmd, zCmd);
211597  return SQLITE_ERROR;
211598}
211599
211600/*
211601** Allocate an instance of the default tokenizer ("simple") at
211602** Fts5Config.pTokenizer. Return SQLITE_OK if successful, or an SQLite error
211603** code if an error occurs.
211604*/
211605static int fts5ConfigDefaultTokenizer(Fts5Global *pGlobal, Fts5Config *pConfig){
211606  assert( pConfig->pTok==0 && pConfig->pTokApi==0 );
211607  return sqlite3Fts5GetTokenizer(
211608      pGlobal, 0, 0, &pConfig->pTok, &pConfig->pTokApi, 0
211609  );
211610}
211611
211612/*
211613** Gobble up the first bareword or quoted word from the input buffer zIn.
211614** Return a pointer to the character immediately following the last in
211615** the gobbled word if successful, or a NULL pointer otherwise (failed
211616** to find close-quote character).
211617**
211618** Before returning, set pzOut to point to a new buffer containing a
211619** nul-terminated, dequoted copy of the gobbled word. If the word was
211620** quoted, *pbQuoted is also set to 1 before returning.
211621**
211622** If *pRc is other than SQLITE_OK when this function is called, it is
211623** a no-op (NULL is returned). Otherwise, if an OOM occurs within this
211624** function, *pRc is set to SQLITE_NOMEM before returning. *pRc is *not*
211625** set if a parse error (failed to find close quote) occurs.
211626*/
211627static const char *fts5ConfigGobbleWord(
211628  int *pRc,                       /* IN/OUT: Error code */
211629  const char *zIn,                /* Buffer to gobble string/bareword from */
211630  char **pzOut,                   /* OUT: malloc'd buffer containing str/bw */
211631  int *pbQuoted                   /* OUT: Set to true if dequoting required */
211632){
211633  const char *zRet = 0;
211634
211635  sqlite3_int64 nIn = strlen(zIn);
211636  char *zOut = sqlite3_malloc64(nIn+1);
211637
211638  assert( *pRc==SQLITE_OK );
211639  *pbQuoted = 0;
211640  *pzOut = 0;
211641
211642  if( zOut==0 ){
211643    *pRc = SQLITE_NOMEM;
211644  }else{
211645    memcpy(zOut, zIn, (size_t)(nIn+1));
211646    if( fts5_isopenquote(zOut[0]) ){
211647      int ii = fts5Dequote(zOut);
211648      zRet = &zIn[ii];
211649      *pbQuoted = 1;
211650    }else{
211651      zRet = fts5ConfigSkipBareword(zIn);
211652      if( zRet ){
211653        zOut[zRet-zIn] = '\0';
211654      }
211655    }
211656  }
211657
211658  if( zRet==0 ){
211659    sqlite3_free(zOut);
211660  }else{
211661    *pzOut = zOut;
211662  }
211663
211664  return zRet;
211665}
211666
211667static int fts5ConfigParseColumn(
211668  Fts5Config *p,
211669  char *zCol,
211670  char *zArg,
211671  char **pzErr
211672){
211673  int rc = SQLITE_OK;
211674  if( 0==sqlite3_stricmp(zCol, FTS5_RANK_NAME)
211675   || 0==sqlite3_stricmp(zCol, FTS5_ROWID_NAME)
211676  ){
211677    *pzErr = sqlite3_mprintf("reserved fts5 column name: %s", zCol);
211678    rc = SQLITE_ERROR;
211679  }else if( zArg ){
211680    if( 0==sqlite3_stricmp(zArg, "unindexed") ){
211681      p->abUnindexed[p->nCol] = 1;
211682    }else{
211683      *pzErr = sqlite3_mprintf("unrecognized column option: %s", zArg);
211684      rc = SQLITE_ERROR;
211685    }
211686  }
211687
211688  p->azCol[p->nCol++] = zCol;
211689  return rc;
211690}
211691
211692/*
211693** Populate the Fts5Config.zContentExprlist string.
211694*/
211695static int fts5ConfigMakeExprlist(Fts5Config *p){
211696  int i;
211697  int rc = SQLITE_OK;
211698  Fts5Buffer buf = {0, 0, 0};
211699
211700  sqlite3Fts5BufferAppendPrintf(&rc, &buf, "T.%Q", p->zContentRowid);
211701  if( p->eContent!=FTS5_CONTENT_NONE ){
211702    for(i=0; i<p->nCol; i++){
211703      if( p->eContent==FTS5_CONTENT_EXTERNAL ){
211704        sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.%Q", p->azCol[i]);
211705      }else{
211706        sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.c%d", i);
211707      }
211708    }
211709  }
211710
211711  assert( p->zContentExprlist==0 );
211712  p->zContentExprlist = (char*)buf.p;
211713  return rc;
211714}
211715
211716/*
211717** Arguments nArg/azArg contain the string arguments passed to the xCreate
211718** or xConnect method of the virtual table. This function attempts to
211719** allocate an instance of Fts5Config containing the results of parsing
211720** those arguments.
211721**
211722** If successful, SQLITE_OK is returned and *ppOut is set to point to the
211723** new Fts5Config object. If an error occurs, an SQLite error code is
211724** returned, *ppOut is set to NULL and an error message may be left in
211725** *pzErr. It is the responsibility of the caller to eventually free any
211726** such error message using sqlite3_free().
211727*/
211728static int sqlite3Fts5ConfigParse(
211729  Fts5Global *pGlobal,
211730  sqlite3 *db,
211731  int nArg,                       /* Number of arguments */
211732  const char **azArg,             /* Array of nArg CREATE VIRTUAL TABLE args */
211733  Fts5Config **ppOut,             /* OUT: Results of parse */
211734  char **pzErr                    /* OUT: Error message */
211735){
211736  int rc = SQLITE_OK;             /* Return code */
211737  Fts5Config *pRet;               /* New object to return */
211738  int i;
211739  sqlite3_int64 nByte;
211740
211741  *ppOut = pRet = (Fts5Config*)sqlite3_malloc(sizeof(Fts5Config));
211742  if( pRet==0 ) return SQLITE_NOMEM;
211743  memset(pRet, 0, sizeof(Fts5Config));
211744  pRet->db = db;
211745  pRet->iCookie = -1;
211746
211747  nByte = nArg * (sizeof(char*) + sizeof(u8));
211748  pRet->azCol = (char**)sqlite3Fts5MallocZero(&rc, nByte);
211749  pRet->abUnindexed = (u8*)&pRet->azCol[nArg];
211750  pRet->zDb = sqlite3Fts5Strndup(&rc, azArg[1], -1);
211751  pRet->zName = sqlite3Fts5Strndup(&rc, azArg[2], -1);
211752  pRet->bColumnsize = 1;
211753  pRet->eDetail = FTS5_DETAIL_FULL;
211754#ifdef SQLITE_DEBUG
211755  pRet->bPrefixIndex = 1;
211756#endif
211757  if( rc==SQLITE_OK && sqlite3_stricmp(pRet->zName, FTS5_RANK_NAME)==0 ){
211758    *pzErr = sqlite3_mprintf("reserved fts5 table name: %s", pRet->zName);
211759    rc = SQLITE_ERROR;
211760  }
211761
211762  for(i=3; rc==SQLITE_OK && i<nArg; i++){
211763    const char *zOrig = azArg[i];
211764    const char *z;
211765    char *zOne = 0;
211766    char *zTwo = 0;
211767    int bOption = 0;
211768    int bMustBeCol = 0;
211769
211770    z = fts5ConfigGobbleWord(&rc, zOrig, &zOne, &bMustBeCol);
211771    z = fts5ConfigSkipWhitespace(z);
211772    if( z && *z=='=' ){
211773      bOption = 1;
211774      z++;
211775      if( bMustBeCol ) z = 0;
211776    }
211777    z = fts5ConfigSkipWhitespace(z);
211778    if( z && z[0] ){
211779      int bDummy;
211780      z = fts5ConfigGobbleWord(&rc, z, &zTwo, &bDummy);
211781      if( z && z[0] ) z = 0;
211782    }
211783
211784    if( rc==SQLITE_OK ){
211785      if( z==0 ){
211786        *pzErr = sqlite3_mprintf("parse error in \"%s\"", zOrig);
211787        rc = SQLITE_ERROR;
211788      }else{
211789        if( bOption ){
211790          rc = fts5ConfigParseSpecial(pGlobal, pRet, zOne, zTwo?zTwo:"", pzErr);
211791        }else{
211792          rc = fts5ConfigParseColumn(pRet, zOne, zTwo, pzErr);
211793          zOne = 0;
211794        }
211795      }
211796    }
211797
211798    sqlite3_free(zOne);
211799    sqlite3_free(zTwo);
211800  }
211801
211802  /* If a tokenizer= option was successfully parsed, the tokenizer has
211803  ** already been allocated. Otherwise, allocate an instance of the default
211804  ** tokenizer (unicode61) now.  */
211805  if( rc==SQLITE_OK && pRet->pTok==0 ){
211806    rc = fts5ConfigDefaultTokenizer(pGlobal, pRet);
211807  }
211808
211809  /* If no zContent option was specified, fill in the default values. */
211810  if( rc==SQLITE_OK && pRet->zContent==0 ){
211811    const char *zTail = 0;
211812    assert( pRet->eContent==FTS5_CONTENT_NORMAL
211813         || pRet->eContent==FTS5_CONTENT_NONE
211814    );
211815    if( pRet->eContent==FTS5_CONTENT_NORMAL ){
211816      zTail = "content";
211817    }else if( pRet->bColumnsize ){
211818      zTail = "docsize";
211819    }
211820
211821    if( zTail ){
211822      pRet->zContent = sqlite3Fts5Mprintf(
211823          &rc, "%Q.'%q_%s'", pRet->zDb, pRet->zName, zTail
211824      );
211825    }
211826  }
211827
211828  if( rc==SQLITE_OK && pRet->zContentRowid==0 ){
211829    pRet->zContentRowid = sqlite3Fts5Strndup(&rc, "rowid", -1);
211830  }
211831
211832  /* Formulate the zContentExprlist text */
211833  if( rc==SQLITE_OK ){
211834    rc = fts5ConfigMakeExprlist(pRet);
211835  }
211836
211837  if( rc!=SQLITE_OK ){
211838    sqlite3Fts5ConfigFree(pRet);
211839    *ppOut = 0;
211840  }
211841  return rc;
211842}
211843
211844/*
211845** Free the configuration object passed as the only argument.
211846*/
211847static void sqlite3Fts5ConfigFree(Fts5Config *pConfig){
211848  if( pConfig ){
211849    int i;
211850    if( pConfig->pTok ){
211851      pConfig->pTokApi->xDelete(pConfig->pTok);
211852    }
211853    sqlite3_free(pConfig->zDb);
211854    sqlite3_free(pConfig->zName);
211855    for(i=0; i<pConfig->nCol; i++){
211856      sqlite3_free(pConfig->azCol[i]);
211857    }
211858    sqlite3_free(pConfig->azCol);
211859    sqlite3_free(pConfig->aPrefix);
211860    sqlite3_free(pConfig->zRank);
211861    sqlite3_free(pConfig->zRankArgs);
211862    sqlite3_free(pConfig->zContent);
211863    sqlite3_free(pConfig->zContentRowid);
211864    sqlite3_free(pConfig->zContentExprlist);
211865    sqlite3_free(pConfig);
211866  }
211867}
211868
211869/*
211870** Call sqlite3_declare_vtab() based on the contents of the configuration
211871** object passed as the only argument. Return SQLITE_OK if successful, or
211872** an SQLite error code if an error occurs.
211873*/
211874static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig){
211875  int i;
211876  int rc = SQLITE_OK;
211877  char *zSql;
211878
211879  zSql = sqlite3Fts5Mprintf(&rc, "CREATE TABLE x(");
211880  for(i=0; zSql && i<pConfig->nCol; i++){
211881    const char *zSep = (i==0?"":", ");
211882    zSql = sqlite3Fts5Mprintf(&rc, "%z%s%Q", zSql, zSep, pConfig->azCol[i]);
211883  }
211884  zSql = sqlite3Fts5Mprintf(&rc, "%z, %Q HIDDEN, %s HIDDEN)",
211885      zSql, pConfig->zName, FTS5_RANK_NAME
211886  );
211887
211888  assert( zSql || rc==SQLITE_NOMEM );
211889  if( zSql ){
211890    rc = sqlite3_declare_vtab(pConfig->db, zSql);
211891    sqlite3_free(zSql);
211892  }
211893
211894  return rc;
211895}
211896
211897/*
211898** Tokenize the text passed via the second and third arguments.
211899**
211900** The callback is invoked once for each token in the input text. The
211901** arguments passed to it are, in order:
211902**
211903**     void *pCtx          // Copy of 4th argument to sqlite3Fts5Tokenize()
211904**     const char *pToken  // Pointer to buffer containing token
211905**     int nToken          // Size of token in bytes
211906**     int iStart          // Byte offset of start of token within input text
211907**     int iEnd            // Byte offset of end of token within input text
211908**     int iPos            // Position of token in input (first token is 0)
211909**
211910** If the callback returns a non-zero value the tokenization is abandoned
211911** and no further callbacks are issued.
211912**
211913** This function returns SQLITE_OK if successful or an SQLite error code
211914** if an error occurs. If the tokenization was abandoned early because
211915** the callback returned SQLITE_DONE, this is not an error and this function
211916** still returns SQLITE_OK. Or, if the tokenization was abandoned early
211917** because the callback returned another non-zero value, it is assumed
211918** to be an SQLite error code and returned to the caller.
211919*/
211920static int sqlite3Fts5Tokenize(
211921  Fts5Config *pConfig,            /* FTS5 Configuration object */
211922  int flags,                      /* FTS5_TOKENIZE_* flags */
211923  const char *pText, int nText,   /* Text to tokenize */
211924  void *pCtx,                     /* Context passed to xToken() */
211925  int (*xToken)(void*, int, const char*, int, int, int)    /* Callback */
211926){
211927  if( pText==0 ) return SQLITE_OK;
211928  return pConfig->pTokApi->xTokenize(
211929      pConfig->pTok, pCtx, flags, pText, nText, xToken
211930  );
211931}
211932
211933/*
211934** Argument pIn points to the first character in what is expected to be
211935** a comma-separated list of SQL literals followed by a ')' character.
211936** If it actually is this, return a pointer to the ')'. Otherwise, return
211937** NULL to indicate a parse error.
211938*/
211939static const char *fts5ConfigSkipArgs(const char *pIn){
211940  const char *p = pIn;
211941
211942  while( 1 ){
211943    p = fts5ConfigSkipWhitespace(p);
211944    p = fts5ConfigSkipLiteral(p);
211945    p = fts5ConfigSkipWhitespace(p);
211946    if( p==0 || *p==')' ) break;
211947    if( *p!=',' ){
211948      p = 0;
211949      break;
211950    }
211951    p++;
211952  }
211953
211954  return p;
211955}
211956
211957/*
211958** Parameter zIn contains a rank() function specification. The format of
211959** this is:
211960**
211961**   + Bareword (function name)
211962**   + Open parenthesis - "("
211963**   + Zero or more SQL literals in a comma separated list
211964**   + Close parenthesis - ")"
211965*/
211966static int sqlite3Fts5ConfigParseRank(
211967  const char *zIn,                /* Input string */
211968  char **pzRank,                  /* OUT: Rank function name */
211969  char **pzRankArgs               /* OUT: Rank function arguments */
211970){
211971  const char *p = zIn;
211972  const char *pRank;
211973  char *zRank = 0;
211974  char *zRankArgs = 0;
211975  int rc = SQLITE_OK;
211976
211977  *pzRank = 0;
211978  *pzRankArgs = 0;
211979
211980  if( p==0 ){
211981    rc = SQLITE_ERROR;
211982  }else{
211983    p = fts5ConfigSkipWhitespace(p);
211984    pRank = p;
211985    p = fts5ConfigSkipBareword(p);
211986
211987    if( p ){
211988      zRank = sqlite3Fts5MallocZero(&rc, 1 + p - pRank);
211989      if( zRank ) memcpy(zRank, pRank, p-pRank);
211990    }else{
211991      rc = SQLITE_ERROR;
211992    }
211993
211994    if( rc==SQLITE_OK ){
211995      p = fts5ConfigSkipWhitespace(p);
211996      if( *p!='(' ) rc = SQLITE_ERROR;
211997      p++;
211998    }
211999    if( rc==SQLITE_OK ){
212000      const char *pArgs;
212001      p = fts5ConfigSkipWhitespace(p);
212002      pArgs = p;
212003      if( *p!=')' ){
212004        p = fts5ConfigSkipArgs(p);
212005        if( p==0 ){
212006          rc = SQLITE_ERROR;
212007        }else{
212008          zRankArgs = sqlite3Fts5MallocZero(&rc, 1 + p - pArgs);
212009          if( zRankArgs ) memcpy(zRankArgs, pArgs, p-pArgs);
212010        }
212011      }
212012    }
212013  }
212014
212015  if( rc!=SQLITE_OK ){
212016    sqlite3_free(zRank);
212017    assert( zRankArgs==0 );
212018  }else{
212019    *pzRank = zRank;
212020    *pzRankArgs = zRankArgs;
212021  }
212022  return rc;
212023}
212024
212025static int sqlite3Fts5ConfigSetValue(
212026  Fts5Config *pConfig,
212027  const char *zKey,
212028  sqlite3_value *pVal,
212029  int *pbBadkey
212030){
212031  int rc = SQLITE_OK;
212032
212033  if( 0==sqlite3_stricmp(zKey, "pgsz") ){
212034    int pgsz = 0;
212035    if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
212036      pgsz = sqlite3_value_int(pVal);
212037    }
212038    if( pgsz<32 || pgsz>FTS5_MAX_PAGE_SIZE ){
212039      *pbBadkey = 1;
212040    }else{
212041      pConfig->pgsz = pgsz;
212042    }
212043  }
212044
212045  else if( 0==sqlite3_stricmp(zKey, "hashsize") ){
212046    int nHashSize = -1;
212047    if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
212048      nHashSize = sqlite3_value_int(pVal);
212049    }
212050    if( nHashSize<=0 ){
212051      *pbBadkey = 1;
212052    }else{
212053      pConfig->nHashSize = nHashSize;
212054    }
212055  }
212056
212057  else if( 0==sqlite3_stricmp(zKey, "automerge") ){
212058    int nAutomerge = -1;
212059    if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
212060      nAutomerge = sqlite3_value_int(pVal);
212061    }
212062    if( nAutomerge<0 || nAutomerge>64 ){
212063      *pbBadkey = 1;
212064    }else{
212065      if( nAutomerge==1 ) nAutomerge = FTS5_DEFAULT_AUTOMERGE;
212066      pConfig->nAutomerge = nAutomerge;
212067    }
212068  }
212069
212070  else if( 0==sqlite3_stricmp(zKey, "usermerge") ){
212071    int nUsermerge = -1;
212072    if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
212073      nUsermerge = sqlite3_value_int(pVal);
212074    }
212075    if( nUsermerge<2 || nUsermerge>16 ){
212076      *pbBadkey = 1;
212077    }else{
212078      pConfig->nUsermerge = nUsermerge;
212079    }
212080  }
212081
212082  else if( 0==sqlite3_stricmp(zKey, "crisismerge") ){
212083    int nCrisisMerge = -1;
212084    if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
212085      nCrisisMerge = sqlite3_value_int(pVal);
212086    }
212087    if( nCrisisMerge<0 ){
212088      *pbBadkey = 1;
212089    }else{
212090      if( nCrisisMerge<=1 ) nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
212091      if( nCrisisMerge>=FTS5_MAX_SEGMENT ) nCrisisMerge = FTS5_MAX_SEGMENT-1;
212092      pConfig->nCrisisMerge = nCrisisMerge;
212093    }
212094  }
212095
212096  else if( 0==sqlite3_stricmp(zKey, "rank") ){
212097    const char *zIn = (const char*)sqlite3_value_text(pVal);
212098    char *zRank;
212099    char *zRankArgs;
212100    rc = sqlite3Fts5ConfigParseRank(zIn, &zRank, &zRankArgs);
212101    if( rc==SQLITE_OK ){
212102      sqlite3_free(pConfig->zRank);
212103      sqlite3_free(pConfig->zRankArgs);
212104      pConfig->zRank = zRank;
212105      pConfig->zRankArgs = zRankArgs;
212106    }else if( rc==SQLITE_ERROR ){
212107      rc = SQLITE_OK;
212108      *pbBadkey = 1;
212109    }
212110  }else{
212111    *pbBadkey = 1;
212112  }
212113  return rc;
212114}
212115
212116/*
212117** Load the contents of the %_config table into memory.
212118*/
212119static int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){
212120  const char *zSelect = "SELECT k, v FROM %Q.'%q_config'";
212121  char *zSql;
212122  sqlite3_stmt *p = 0;
212123  int rc = SQLITE_OK;
212124  int iVersion = 0;
212125
212126  /* Set default values */
212127  pConfig->pgsz = FTS5_DEFAULT_PAGE_SIZE;
212128  pConfig->nAutomerge = FTS5_DEFAULT_AUTOMERGE;
212129  pConfig->nUsermerge = FTS5_DEFAULT_USERMERGE;
212130  pConfig->nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
212131  pConfig->nHashSize = FTS5_DEFAULT_HASHSIZE;
212132
212133  zSql = sqlite3Fts5Mprintf(&rc, zSelect, pConfig->zDb, pConfig->zName);
212134  if( zSql ){
212135    rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &p, 0);
212136    sqlite3_free(zSql);
212137  }
212138
212139  assert( rc==SQLITE_OK || p==0 );
212140  if( rc==SQLITE_OK ){
212141    while( SQLITE_ROW==sqlite3_step(p) ){
212142      const char *zK = (const char*)sqlite3_column_text(p, 0);
212143      sqlite3_value *pVal = sqlite3_column_value(p, 1);
212144      if( 0==sqlite3_stricmp(zK, "version") ){
212145        iVersion = sqlite3_value_int(pVal);
212146      }else{
212147        int bDummy = 0;
212148        sqlite3Fts5ConfigSetValue(pConfig, zK, pVal, &bDummy);
212149      }
212150    }
212151    rc = sqlite3_finalize(p);
212152  }
212153
212154  if( rc==SQLITE_OK && iVersion!=FTS5_CURRENT_VERSION ){
212155    rc = SQLITE_ERROR;
212156    if( pConfig->pzErrmsg ){
212157      assert( 0==*pConfig->pzErrmsg );
212158      *pConfig->pzErrmsg = sqlite3_mprintf(
212159          "invalid fts5 file format (found %d, expected %d) - run 'rebuild'",
212160          iVersion, FTS5_CURRENT_VERSION
212161      );
212162    }
212163  }
212164
212165  if( rc==SQLITE_OK ){
212166    pConfig->iCookie = iCookie;
212167  }
212168  return rc;
212169}
212170
212171/*
212172** 2014 May 31
212173**
212174** The author disclaims copyright to this source code.  In place of
212175** a legal notice, here is a blessing:
212176**
212177**    May you do good and not evil.
212178**    May you find forgiveness for yourself and forgive others.
212179**    May you share freely, never taking more than you give.
212180**
212181******************************************************************************
212182**
212183*/
212184
212185
212186
212187/* #include "fts5Int.h" */
212188/* #include "fts5parse.h" */
212189
212190/*
212191** All token types in the generated fts5parse.h file are greater than 0.
212192*/
212193#define FTS5_EOF 0
212194
212195#define FTS5_LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
212196
212197typedef struct Fts5ExprTerm Fts5ExprTerm;
212198
212199/*
212200** Functions generated by lemon from fts5parse.y.
212201*/
212202static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64));
212203static void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*));
212204static void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*);
212205#ifndef NDEBUG
212206/* #include <stdio.h> */
212207static void sqlite3Fts5ParserTrace(FILE*, char*);
212208#endif
212209static int sqlite3Fts5ParserFallback(int);
212210
212211
212212struct Fts5Expr {
212213  Fts5Index *pIndex;
212214  Fts5Config *pConfig;
212215  Fts5ExprNode *pRoot;
212216  int bDesc;                      /* Iterate in descending rowid order */
212217  int nPhrase;                    /* Number of phrases in expression */
212218  Fts5ExprPhrase **apExprPhrase;  /* Pointers to phrase objects */
212219};
212220
212221/*
212222** eType:
212223**   Expression node type. Always one of:
212224**
212225**       FTS5_AND                 (nChild, apChild valid)
212226**       FTS5_OR                  (nChild, apChild valid)
212227**       FTS5_NOT                 (nChild, apChild valid)
212228**       FTS5_STRING              (pNear valid)
212229**       FTS5_TERM                (pNear valid)
212230*/
212231struct Fts5ExprNode {
212232  int eType;                      /* Node type */
212233  int bEof;                       /* True at EOF */
212234  int bNomatch;                   /* True if entry is not a match */
212235
212236  /* Next method for this node. */
212237  int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64);
212238
212239  i64 iRowid;                     /* Current rowid */
212240  Fts5ExprNearset *pNear;         /* For FTS5_STRING - cluster of phrases */
212241
212242  /* Child nodes. For a NOT node, this array always contains 2 entries. For
212243  ** AND or OR nodes, it contains 2 or more entries.  */
212244  int nChild;                     /* Number of child nodes */
212245  Fts5ExprNode *apChild[1];       /* Array of child nodes */
212246};
212247
212248#define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING)
212249
212250/*
212251** Invoke the xNext method of an Fts5ExprNode object. This macro should be
212252** used as if it has the same signature as the xNext() methods themselves.
212253*/
212254#define fts5ExprNodeNext(a,b,c,d) (b)->xNext((a), (b), (c), (d))
212255
212256/*
212257** An instance of the following structure represents a single search term
212258** or term prefix.
212259*/
212260struct Fts5ExprTerm {
212261  u8 bPrefix;                     /* True for a prefix term */
212262  u8 bFirst;                      /* True if token must be first in column */
212263  char *zTerm;                    /* nul-terminated term */
212264  Fts5IndexIter *pIter;           /* Iterator for this term */
212265  Fts5ExprTerm *pSynonym;         /* Pointer to first in list of synonyms */
212266};
212267
212268/*
212269** A phrase. One or more terms that must appear in a contiguous sequence
212270** within a document for it to match.
212271*/
212272struct Fts5ExprPhrase {
212273  Fts5ExprNode *pNode;            /* FTS5_STRING node this phrase is part of */
212274  Fts5Buffer poslist;             /* Current position list */
212275  int nTerm;                      /* Number of entries in aTerm[] */
212276  Fts5ExprTerm aTerm[1];          /* Terms that make up this phrase */
212277};
212278
212279/*
212280** One or more phrases that must appear within a certain token distance of
212281** each other within each matching document.
212282*/
212283struct Fts5ExprNearset {
212284  int nNear;                      /* NEAR parameter */
212285  Fts5Colset *pColset;            /* Columns to search (NULL -> all columns) */
212286  int nPhrase;                    /* Number of entries in aPhrase[] array */
212287  Fts5ExprPhrase *apPhrase[1];    /* Array of phrase pointers */
212288};
212289
212290
212291/*
212292** Parse context.
212293*/
212294struct Fts5Parse {
212295  Fts5Config *pConfig;
212296  char *zErr;
212297  int rc;
212298  int nPhrase;                    /* Size of apPhrase array */
212299  Fts5ExprPhrase **apPhrase;      /* Array of all phrases */
212300  Fts5ExprNode *pExpr;            /* Result of a successful parse */
212301};
212302
212303static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){
212304  va_list ap;
212305  va_start(ap, zFmt);
212306  if( pParse->rc==SQLITE_OK ){
212307    pParse->zErr = sqlite3_vmprintf(zFmt, ap);
212308    pParse->rc = SQLITE_ERROR;
212309  }
212310  va_end(ap);
212311}
212312
212313static int fts5ExprIsspace(char t){
212314  return t==' ' || t=='\t' || t=='\n' || t=='\r';
212315}
212316
212317/*
212318** Read the first token from the nul-terminated string at *pz.
212319*/
212320static int fts5ExprGetToken(
212321  Fts5Parse *pParse,
212322  const char **pz,                /* IN/OUT: Pointer into buffer */
212323  Fts5Token *pToken
212324){
212325  const char *z = *pz;
212326  int tok;
212327
212328  /* Skip past any whitespace */
212329  while( fts5ExprIsspace(*z) ) z++;
212330
212331  pToken->p = z;
212332  pToken->n = 1;
212333  switch( *z ){
212334    case '(':  tok = FTS5_LP;    break;
212335    case ')':  tok = FTS5_RP;    break;
212336    case '{':  tok = FTS5_LCP;   break;
212337    case '}':  tok = FTS5_RCP;   break;
212338    case ':':  tok = FTS5_COLON; break;
212339    case ',':  tok = FTS5_COMMA; break;
212340    case '+':  tok = FTS5_PLUS;  break;
212341    case '*':  tok = FTS5_STAR;  break;
212342    case '-':  tok = FTS5_MINUS; break;
212343    case '^':  tok = FTS5_CARET; break;
212344    case '\0': tok = FTS5_EOF;   break;
212345
212346    case '"': {
212347      const char *z2;
212348      tok = FTS5_STRING;
212349
212350      for(z2=&z[1]; 1; z2++){
212351        if( z2[0]=='"' ){
212352          z2++;
212353          if( z2[0]!='"' ) break;
212354        }
212355        if( z2[0]=='\0' ){
212356          sqlite3Fts5ParseError(pParse, "unterminated string");
212357          return FTS5_EOF;
212358        }
212359      }
212360      pToken->n = (z2 - z);
212361      break;
212362    }
212363
212364    default: {
212365      const char *z2;
212366      if( sqlite3Fts5IsBareword(z[0])==0 ){
212367        sqlite3Fts5ParseError(pParse, "fts5: syntax error near \"%.1s\"", z);
212368        return FTS5_EOF;
212369      }
212370      tok = FTS5_STRING;
212371      for(z2=&z[1]; sqlite3Fts5IsBareword(*z2); z2++);
212372      pToken->n = (z2 - z);
212373      if( pToken->n==2 && memcmp(pToken->p, "OR", 2)==0 )  tok = FTS5_OR;
212374      if( pToken->n==3 && memcmp(pToken->p, "NOT", 3)==0 ) tok = FTS5_NOT;
212375      if( pToken->n==3 && memcmp(pToken->p, "AND", 3)==0 ) tok = FTS5_AND;
212376      break;
212377    }
212378  }
212379
212380  *pz = &pToken->p[pToken->n];
212381  return tok;
212382}
212383
212384static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc64((sqlite3_int64)t);}
212385static void fts5ParseFree(void *p){ sqlite3_free(p); }
212386
212387static int sqlite3Fts5ExprNew(
212388  Fts5Config *pConfig,            /* FTS5 Configuration */
212389  int iCol,
212390  const char *zExpr,              /* Expression text */
212391  Fts5Expr **ppNew,
212392  char **pzErr
212393){
212394  Fts5Parse sParse;
212395  Fts5Token token;
212396  const char *z = zExpr;
212397  int t;                          /* Next token type */
212398  void *pEngine;
212399  Fts5Expr *pNew;
212400
212401  *ppNew = 0;
212402  *pzErr = 0;
212403  memset(&sParse, 0, sizeof(sParse));
212404  pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc);
212405  if( pEngine==0 ){ return SQLITE_NOMEM; }
212406  sParse.pConfig = pConfig;
212407
212408  do {
212409    t = fts5ExprGetToken(&sParse, &z, &token);
212410    sqlite3Fts5Parser(pEngine, t, token, &sParse);
212411  }while( sParse.rc==SQLITE_OK && t!=FTS5_EOF );
212412  sqlite3Fts5ParserFree(pEngine, fts5ParseFree);
212413
212414  /* If the LHS of the MATCH expression was a user column, apply the
212415  ** implicit column-filter.  */
212416  if( iCol<pConfig->nCol && sParse.pExpr && sParse.rc==SQLITE_OK ){
212417    int n = sizeof(Fts5Colset);
212418    Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&sParse.rc, n);
212419    if( pColset ){
212420      pColset->nCol = 1;
212421      pColset->aiCol[0] = iCol;
212422      sqlite3Fts5ParseSetColset(&sParse, sParse.pExpr, pColset);
212423    }
212424  }
212425
212426  assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 );
212427  if( sParse.rc==SQLITE_OK ){
212428    *ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr));
212429    if( pNew==0 ){
212430      sParse.rc = SQLITE_NOMEM;
212431      sqlite3Fts5ParseNodeFree(sParse.pExpr);
212432    }else{
212433      if( !sParse.pExpr ){
212434        const int nByte = sizeof(Fts5ExprNode);
212435        pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&sParse.rc, nByte);
212436        if( pNew->pRoot ){
212437          pNew->pRoot->bEof = 1;
212438        }
212439      }else{
212440        pNew->pRoot = sParse.pExpr;
212441      }
212442      pNew->pIndex = 0;
212443      pNew->pConfig = pConfig;
212444      pNew->apExprPhrase = sParse.apPhrase;
212445      pNew->nPhrase = sParse.nPhrase;
212446      sParse.apPhrase = 0;
212447    }
212448  }else{
212449    sqlite3Fts5ParseNodeFree(sParse.pExpr);
212450  }
212451
212452  sqlite3_free(sParse.apPhrase);
212453  *pzErr = sParse.zErr;
212454  return sParse.rc;
212455}
212456
212457/*
212458** Free the expression node object passed as the only argument.
212459*/
212460static void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){
212461  if( p ){
212462    int i;
212463    for(i=0; i<p->nChild; i++){
212464      sqlite3Fts5ParseNodeFree(p->apChild[i]);
212465    }
212466    sqlite3Fts5ParseNearsetFree(p->pNear);
212467    sqlite3_free(p);
212468  }
212469}
212470
212471/*
212472** Free the expression object passed as the only argument.
212473*/
212474static void sqlite3Fts5ExprFree(Fts5Expr *p){
212475  if( p ){
212476    sqlite3Fts5ParseNodeFree(p->pRoot);
212477    sqlite3_free(p->apExprPhrase);
212478    sqlite3_free(p);
212479  }
212480}
212481
212482static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2){
212483  Fts5Parse sParse;
212484  memset(&sParse, 0, sizeof(sParse));
212485
212486  if( *pp1 ){
212487    Fts5Expr *p1 = *pp1;
212488    int nPhrase = p1->nPhrase + p2->nPhrase;
212489
212490    p1->pRoot = sqlite3Fts5ParseNode(&sParse, FTS5_AND, p1->pRoot, p2->pRoot,0);
212491    p2->pRoot = 0;
212492
212493    if( sParse.rc==SQLITE_OK ){
212494      Fts5ExprPhrase **ap = (Fts5ExprPhrase**)sqlite3_realloc(
212495          p1->apExprPhrase, nPhrase * sizeof(Fts5ExprPhrase*)
212496      );
212497      if( ap==0 ){
212498        sParse.rc = SQLITE_NOMEM;
212499      }else{
212500        int i;
212501        memmove(&ap[p2->nPhrase], ap, p1->nPhrase*sizeof(Fts5ExprPhrase*));
212502        for(i=0; i<p2->nPhrase; i++){
212503          ap[i] = p2->apExprPhrase[i];
212504        }
212505        p1->nPhrase = nPhrase;
212506        p1->apExprPhrase = ap;
212507      }
212508    }
212509    sqlite3_free(p2->apExprPhrase);
212510    sqlite3_free(p2);
212511  }else{
212512    *pp1 = p2;
212513  }
212514
212515  return sParse.rc;
212516}
212517
212518/*
212519** Argument pTerm must be a synonym iterator. Return the current rowid
212520** that it points to.
212521*/
212522static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){
212523  i64 iRet = 0;
212524  int bRetValid = 0;
212525  Fts5ExprTerm *p;
212526
212527  assert( pTerm->pSynonym );
212528  assert( bDesc==0 || bDesc==1 );
212529  for(p=pTerm; p; p=p->pSynonym){
212530    if( 0==sqlite3Fts5IterEof(p->pIter) ){
212531      i64 iRowid = p->pIter->iRowid;
212532      if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
212533        iRet = iRowid;
212534        bRetValid = 1;
212535      }
212536    }
212537  }
212538
212539  if( pbEof && bRetValid==0 ) *pbEof = 1;
212540  return iRet;
212541}
212542
212543/*
212544** Argument pTerm must be a synonym iterator.
212545*/
212546static int fts5ExprSynonymList(
212547  Fts5ExprTerm *pTerm,
212548  i64 iRowid,
212549  Fts5Buffer *pBuf,               /* Use this buffer for space if required */
212550  u8 **pa, int *pn
212551){
212552  Fts5PoslistReader aStatic[4];
212553  Fts5PoslistReader *aIter = aStatic;
212554  int nIter = 0;
212555  int nAlloc = 4;
212556  int rc = SQLITE_OK;
212557  Fts5ExprTerm *p;
212558
212559  assert( pTerm->pSynonym );
212560  for(p=pTerm; p; p=p->pSynonym){
212561    Fts5IndexIter *pIter = p->pIter;
212562    if( sqlite3Fts5IterEof(pIter)==0 && pIter->iRowid==iRowid ){
212563      if( pIter->nData==0 ) continue;
212564      if( nIter==nAlloc ){
212565        sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
212566        Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
212567        if( aNew==0 ){
212568          rc = SQLITE_NOMEM;
212569          goto synonym_poslist_out;
212570        }
212571        memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter);
212572        nAlloc = nAlloc*2;
212573        if( aIter!=aStatic ) sqlite3_free(aIter);
212574        aIter = aNew;
212575      }
212576      sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &aIter[nIter]);
212577      assert( aIter[nIter].bEof==0 );
212578      nIter++;
212579    }
212580  }
212581
212582  if( nIter==1 ){
212583    *pa = (u8*)aIter[0].a;
212584    *pn = aIter[0].n;
212585  }else{
212586    Fts5PoslistWriter writer = {0};
212587    i64 iPrev = -1;
212588    fts5BufferZero(pBuf);
212589    while( 1 ){
212590      int i;
212591      i64 iMin = FTS5_LARGEST_INT64;
212592      for(i=0; i<nIter; i++){
212593        if( aIter[i].bEof==0 ){
212594          if( aIter[i].iPos==iPrev ){
212595            if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) continue;
212596          }
212597          if( aIter[i].iPos<iMin ){
212598            iMin = aIter[i].iPos;
212599          }
212600        }
212601      }
212602      if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break;
212603      rc = sqlite3Fts5PoslistWriterAppend(pBuf, &writer, iMin);
212604      iPrev = iMin;
212605    }
212606    if( rc==SQLITE_OK ){
212607      *pa = pBuf->p;
212608      *pn = pBuf->n;
212609    }
212610  }
212611
212612 synonym_poslist_out:
212613  if( aIter!=aStatic ) sqlite3_free(aIter);
212614  return rc;
212615}
212616
212617
212618/*
212619** All individual term iterators in pPhrase are guaranteed to be valid and
212620** pointing to the same rowid when this function is called. This function
212621** checks if the current rowid really is a match, and if so populates
212622** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch
212623** is set to true if this is really a match, or false otherwise.
212624**
212625** SQLITE_OK is returned if an error occurs, or an SQLite error code
212626** otherwise. It is not considered an error code if the current rowid is
212627** not a match.
212628*/
212629static int fts5ExprPhraseIsMatch(
212630  Fts5ExprNode *pNode,            /* Node pPhrase belongs to */
212631  Fts5ExprPhrase *pPhrase,        /* Phrase object to initialize */
212632  int *pbMatch                    /* OUT: Set to true if really a match */
212633){
212634  Fts5PoslistWriter writer = {0};
212635  Fts5PoslistReader aStatic[4];
212636  Fts5PoslistReader *aIter = aStatic;
212637  int i;
212638  int rc = SQLITE_OK;
212639  int bFirst = pPhrase->aTerm[0].bFirst;
212640
212641  fts5BufferZero(&pPhrase->poslist);
212642
212643  /* If the aStatic[] array is not large enough, allocate a large array
212644  ** using sqlite3_malloc(). This approach could be improved upon. */
212645  if( pPhrase->nTerm>ArraySize(aStatic) ){
212646    sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
212647    aIter = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
212648    if( !aIter ) return SQLITE_NOMEM;
212649  }
212650  memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm);
212651
212652  /* Initialize a term iterator for each term in the phrase */
212653  for(i=0; i<pPhrase->nTerm; i++){
212654    Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
212655    int n = 0;
212656    int bFlag = 0;
212657    u8 *a = 0;
212658    if( pTerm->pSynonym ){
212659      Fts5Buffer buf = {0, 0, 0};
212660      rc = fts5ExprSynonymList(pTerm, pNode->iRowid, &buf, &a, &n);
212661      if( rc ){
212662        sqlite3_free(a);
212663        goto ismatch_out;
212664      }
212665      if( a==buf.p ) bFlag = 1;
212666    }else{
212667      a = (u8*)pTerm->pIter->pData;
212668      n = pTerm->pIter->nData;
212669    }
212670    sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
212671    aIter[i].bFlag = (u8)bFlag;
212672    if( aIter[i].bEof ) goto ismatch_out;
212673  }
212674
212675  while( 1 ){
212676    int bMatch;
212677    i64 iPos = aIter[0].iPos;
212678    do {
212679      bMatch = 1;
212680      for(i=0; i<pPhrase->nTerm; i++){
212681        Fts5PoslistReader *pPos = &aIter[i];
212682        i64 iAdj = iPos + i;
212683        if( pPos->iPos!=iAdj ){
212684          bMatch = 0;
212685          while( pPos->iPos<iAdj ){
212686            if( sqlite3Fts5PoslistReaderNext(pPos) ) goto ismatch_out;
212687          }
212688          if( pPos->iPos>iAdj ) iPos = pPos->iPos-i;
212689        }
212690      }
212691    }while( bMatch==0 );
212692
212693    /* Append position iPos to the output */
212694    if( bFirst==0 || FTS5_POS2OFFSET(iPos)==0 ){
212695      rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos);
212696      if( rc!=SQLITE_OK ) goto ismatch_out;
212697    }
212698
212699    for(i=0; i<pPhrase->nTerm; i++){
212700      if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out;
212701    }
212702  }
212703
212704 ismatch_out:
212705  *pbMatch = (pPhrase->poslist.n>0);
212706  for(i=0; i<pPhrase->nTerm; i++){
212707    if( aIter[i].bFlag ) sqlite3_free((u8*)aIter[i].a);
212708  }
212709  if( aIter!=aStatic ) sqlite3_free(aIter);
212710  return rc;
212711}
212712
212713typedef struct Fts5LookaheadReader Fts5LookaheadReader;
212714struct Fts5LookaheadReader {
212715  const u8 *a;                    /* Buffer containing position list */
212716  int n;                          /* Size of buffer a[] in bytes */
212717  int i;                          /* Current offset in position list */
212718  i64 iPos;                       /* Current position */
212719  i64 iLookahead;                 /* Next position */
212720};
212721
212722#define FTS5_LOOKAHEAD_EOF (((i64)1) << 62)
212723
212724static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){
212725  p->iPos = p->iLookahead;
212726  if( sqlite3Fts5PoslistNext64(p->a, p->n, &p->i, &p->iLookahead) ){
212727    p->iLookahead = FTS5_LOOKAHEAD_EOF;
212728  }
212729  return (p->iPos==FTS5_LOOKAHEAD_EOF);
212730}
212731
212732static int fts5LookaheadReaderInit(
212733  const u8 *a, int n,             /* Buffer to read position list from */
212734  Fts5LookaheadReader *p          /* Iterator object to initialize */
212735){
212736  memset(p, 0, sizeof(Fts5LookaheadReader));
212737  p->a = a;
212738  p->n = n;
212739  fts5LookaheadReaderNext(p);
212740  return fts5LookaheadReaderNext(p);
212741}
212742
212743typedef struct Fts5NearTrimmer Fts5NearTrimmer;
212744struct Fts5NearTrimmer {
212745  Fts5LookaheadReader reader;     /* Input iterator */
212746  Fts5PoslistWriter writer;       /* Writer context */
212747  Fts5Buffer *pOut;               /* Output poslist */
212748};
212749
212750/*
212751** The near-set object passed as the first argument contains more than
212752** one phrase. All phrases currently point to the same row. The
212753** Fts5ExprPhrase.poslist buffers are populated accordingly. This function
212754** tests if the current row contains instances of each phrase sufficiently
212755** close together to meet the NEAR constraint. Non-zero is returned if it
212756** does, or zero otherwise.
212757**
212758** If in/out parameter (*pRc) is set to other than SQLITE_OK when this
212759** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM)
212760** occurs within this function (*pRc) is set accordingly before returning.
212761** The return value is undefined in both these cases.
212762**
212763** If no error occurs and non-zero (a match) is returned, the position-list
212764** of each phrase object is edited to contain only those entries that
212765** meet the constraint before returning.
212766*/
212767static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){
212768  Fts5NearTrimmer aStatic[4];
212769  Fts5NearTrimmer *a = aStatic;
212770  Fts5ExprPhrase **apPhrase = pNear->apPhrase;
212771
212772  int i;
212773  int rc = *pRc;
212774  int bMatch;
212775
212776  assert( pNear->nPhrase>1 );
212777
212778  /* If the aStatic[] array is not large enough, allocate a large array
212779  ** using sqlite3_malloc(). This approach could be improved upon. */
212780  if( pNear->nPhrase>ArraySize(aStatic) ){
212781    sqlite3_int64 nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
212782    a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte);
212783  }else{
212784    memset(aStatic, 0, sizeof(aStatic));
212785  }
212786  if( rc!=SQLITE_OK ){
212787    *pRc = rc;
212788    return 0;
212789  }
212790
212791  /* Initialize a lookahead iterator for each phrase. After passing the
212792  ** buffer and buffer size to the lookaside-reader init function, zero
212793  ** the phrase poslist buffer. The new poslist for the phrase (containing
212794  ** the same entries as the original with some entries removed on account
212795  ** of the NEAR constraint) is written over the original even as it is
212796  ** being read. This is safe as the entries for the new poslist are a
212797  ** subset of the old, so it is not possible for data yet to be read to
212798  ** be overwritten.  */
212799  for(i=0; i<pNear->nPhrase; i++){
212800    Fts5Buffer *pPoslist = &apPhrase[i]->poslist;
212801    fts5LookaheadReaderInit(pPoslist->p, pPoslist->n, &a[i].reader);
212802    pPoslist->n = 0;
212803    a[i].pOut = pPoslist;
212804  }
212805
212806  while( 1 ){
212807    int iAdv;
212808    i64 iMin;
212809    i64 iMax;
212810
212811    /* This block advances the phrase iterators until they point to a set of
212812    ** entries that together comprise a match.  */
212813    iMax = a[0].reader.iPos;
212814    do {
212815      bMatch = 1;
212816      for(i=0; i<pNear->nPhrase; i++){
212817        Fts5LookaheadReader *pPos = &a[i].reader;
212818        iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear;
212819        if( pPos->iPos<iMin || pPos->iPos>iMax ){
212820          bMatch = 0;
212821          while( pPos->iPos<iMin ){
212822            if( fts5LookaheadReaderNext(pPos) ) goto ismatch_out;
212823          }
212824          if( pPos->iPos>iMax ) iMax = pPos->iPos;
212825        }
212826      }
212827    }while( bMatch==0 );
212828
212829    /* Add an entry to each output position list */
212830    for(i=0; i<pNear->nPhrase; i++){
212831      i64 iPos = a[i].reader.iPos;
212832      Fts5PoslistWriter *pWriter = &a[i].writer;
212833      if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){
212834        sqlite3Fts5PoslistWriterAppend(a[i].pOut, pWriter, iPos);
212835      }
212836    }
212837
212838    iAdv = 0;
212839    iMin = a[0].reader.iLookahead;
212840    for(i=0; i<pNear->nPhrase; i++){
212841      if( a[i].reader.iLookahead < iMin ){
212842        iMin = a[i].reader.iLookahead;
212843        iAdv = i;
212844      }
212845    }
212846    if( fts5LookaheadReaderNext(&a[iAdv].reader) ) goto ismatch_out;
212847  }
212848
212849  ismatch_out: {
212850    int bRet = a[0].pOut->n>0;
212851    *pRc = rc;
212852    if( a!=aStatic ) sqlite3_free(a);
212853    return bRet;
212854  }
212855}
212856
212857/*
212858** Advance iterator pIter until it points to a value equal to or laster
212859** than the initial value of *piLast. If this means the iterator points
212860** to a value laster than *piLast, update *piLast to the new lastest value.
212861**
212862** If the iterator reaches EOF, set *pbEof to true before returning. If
212863** an error occurs, set *pRc to an error code. If either *pbEof or *pRc
212864** are set, return a non-zero value. Otherwise, return zero.
212865*/
212866static int fts5ExprAdvanceto(
212867  Fts5IndexIter *pIter,           /* Iterator to advance */
212868  int bDesc,                      /* True if iterator is "rowid DESC" */
212869  i64 *piLast,                    /* IN/OUT: Lastest rowid seen so far */
212870  int *pRc,                       /* OUT: Error code */
212871  int *pbEof                      /* OUT: Set to true if EOF */
212872){
212873  i64 iLast = *piLast;
212874  i64 iRowid;
212875
212876  iRowid = pIter->iRowid;
212877  if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
212878    int rc = sqlite3Fts5IterNextFrom(pIter, iLast);
212879    if( rc || sqlite3Fts5IterEof(pIter) ){
212880      *pRc = rc;
212881      *pbEof = 1;
212882      return 1;
212883    }
212884    iRowid = pIter->iRowid;
212885    assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) );
212886  }
212887  *piLast = iRowid;
212888
212889  return 0;
212890}
212891
212892static int fts5ExprSynonymAdvanceto(
212893  Fts5ExprTerm *pTerm,            /* Term iterator to advance */
212894  int bDesc,                      /* True if iterator is "rowid DESC" */
212895  i64 *piLast,                    /* IN/OUT: Lastest rowid seen so far */
212896  int *pRc                        /* OUT: Error code */
212897){
212898  int rc = SQLITE_OK;
212899  i64 iLast = *piLast;
212900  Fts5ExprTerm *p;
212901  int bEof = 0;
212902
212903  for(p=pTerm; rc==SQLITE_OK && p; p=p->pSynonym){
212904    if( sqlite3Fts5IterEof(p->pIter)==0 ){
212905      i64 iRowid = p->pIter->iRowid;
212906      if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
212907        rc = sqlite3Fts5IterNextFrom(p->pIter, iLast);
212908      }
212909    }
212910  }
212911
212912  if( rc!=SQLITE_OK ){
212913    *pRc = rc;
212914    bEof = 1;
212915  }else{
212916    *piLast = fts5ExprSynonymRowid(pTerm, bDesc, &bEof);
212917  }
212918  return bEof;
212919}
212920
212921
212922static int fts5ExprNearTest(
212923  int *pRc,
212924  Fts5Expr *pExpr,                /* Expression that pNear is a part of */
212925  Fts5ExprNode *pNode             /* The "NEAR" node (FTS5_STRING) */
212926){
212927  Fts5ExprNearset *pNear = pNode->pNear;
212928  int rc = *pRc;
212929
212930  if( pExpr->pConfig->eDetail!=FTS5_DETAIL_FULL ){
212931    Fts5ExprTerm *pTerm;
212932    Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
212933    pPhrase->poslist.n = 0;
212934    for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
212935      Fts5IndexIter *pIter = pTerm->pIter;
212936      if( sqlite3Fts5IterEof(pIter)==0 ){
212937        if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){
212938          pPhrase->poslist.n = 1;
212939        }
212940      }
212941    }
212942    return pPhrase->poslist.n;
212943  }else{
212944    int i;
212945
212946    /* Check that each phrase in the nearset matches the current row.
212947    ** Populate the pPhrase->poslist buffers at the same time. If any
212948    ** phrase is not a match, break out of the loop early.  */
212949    for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
212950      Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
212951      if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym
212952       || pNear->pColset || pPhrase->aTerm[0].bFirst
212953      ){
212954        int bMatch = 0;
212955        rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch);
212956        if( bMatch==0 ) break;
212957      }else{
212958        Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
212959        fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData);
212960      }
212961    }
212962
212963    *pRc = rc;
212964    if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
212965      return 1;
212966    }
212967    return 0;
212968  }
212969}
212970
212971
212972/*
212973** Initialize all term iterators in the pNear object. If any term is found
212974** to match no documents at all, return immediately without initializing any
212975** further iterators.
212976**
212977** If an error occurs, return an SQLite error code. Otherwise, return
212978** SQLITE_OK. It is not considered an error if some term matches zero
212979** documents.
212980*/
212981static int fts5ExprNearInitAll(
212982  Fts5Expr *pExpr,
212983  Fts5ExprNode *pNode
212984){
212985  Fts5ExprNearset *pNear = pNode->pNear;
212986  int i;
212987
212988  assert( pNode->bNomatch==0 );
212989  for(i=0; i<pNear->nPhrase; i++){
212990    Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
212991    if( pPhrase->nTerm==0 ){
212992      pNode->bEof = 1;
212993      return SQLITE_OK;
212994    }else{
212995      int j;
212996      for(j=0; j<pPhrase->nTerm; j++){
212997        Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
212998        Fts5ExprTerm *p;
212999        int bHit = 0;
213000
213001        for(p=pTerm; p; p=p->pSynonym){
213002          int rc;
213003          if( p->pIter ){
213004            sqlite3Fts5IterClose(p->pIter);
213005            p->pIter = 0;
213006          }
213007          rc = sqlite3Fts5IndexQuery(
213008              pExpr->pIndex, p->zTerm, (int)strlen(p->zTerm),
213009              (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) |
213010              (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0),
213011              pNear->pColset,
213012              &p->pIter
213013          );
213014          assert( (rc==SQLITE_OK)==(p->pIter!=0) );
213015          if( rc!=SQLITE_OK ) return rc;
213016          if( 0==sqlite3Fts5IterEof(p->pIter) ){
213017            bHit = 1;
213018          }
213019        }
213020
213021        if( bHit==0 ){
213022          pNode->bEof = 1;
213023          return SQLITE_OK;
213024        }
213025      }
213026    }
213027  }
213028
213029  pNode->bEof = 0;
213030  return SQLITE_OK;
213031}
213032
213033/*
213034** If pExpr is an ASC iterator, this function returns a value with the
213035** same sign as:
213036**
213037**   (iLhs - iRhs)
213038**
213039** Otherwise, if this is a DESC iterator, the opposite is returned:
213040**
213041**   (iRhs - iLhs)
213042*/
213043static int fts5RowidCmp(
213044  Fts5Expr *pExpr,
213045  i64 iLhs,
213046  i64 iRhs
213047){
213048  assert( pExpr->bDesc==0 || pExpr->bDesc==1 );
213049  if( pExpr->bDesc==0 ){
213050    if( iLhs<iRhs ) return -1;
213051    return (iLhs > iRhs);
213052  }else{
213053    if( iLhs>iRhs ) return -1;
213054    return (iLhs < iRhs);
213055  }
213056}
213057
213058static void fts5ExprSetEof(Fts5ExprNode *pNode){
213059  int i;
213060  pNode->bEof = 1;
213061  pNode->bNomatch = 0;
213062  for(i=0; i<pNode->nChild; i++){
213063    fts5ExprSetEof(pNode->apChild[i]);
213064  }
213065}
213066
213067static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){
213068  if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
213069    Fts5ExprNearset *pNear = pNode->pNear;
213070    int i;
213071    for(i=0; i<pNear->nPhrase; i++){
213072      Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
213073      pPhrase->poslist.n = 0;
213074    }
213075  }else{
213076    int i;
213077    for(i=0; i<pNode->nChild; i++){
213078      fts5ExprNodeZeroPoslist(pNode->apChild[i]);
213079    }
213080  }
213081}
213082
213083
213084
213085/*
213086** Compare the values currently indicated by the two nodes as follows:
213087**
213088**    res = (*p1) - (*p2)
213089**
213090** Nodes that point to values that come later in the iteration order are
213091** considered to be larger. Nodes at EOF are the largest of all.
213092**
213093** This means that if the iteration order is ASC, then numerically larger
213094** rowids are considered larger. Or if it is the default DESC, numerically
213095** smaller rowids are larger.
213096*/
213097static int fts5NodeCompare(
213098  Fts5Expr *pExpr,
213099  Fts5ExprNode *p1,
213100  Fts5ExprNode *p2
213101){
213102  if( p2->bEof ) return -1;
213103  if( p1->bEof ) return +1;
213104  return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid);
213105}
213106
213107/*
213108** All individual term iterators in pNear are guaranteed to be valid when
213109** this function is called. This function checks if all term iterators
213110** point to the same rowid, and if not, advances them until they do.
213111** If an EOF is reached before this happens, *pbEof is set to true before
213112** returning.
213113**
213114** SQLITE_OK is returned if an error occurs, or an SQLite error code
213115** otherwise. It is not considered an error code if an iterator reaches
213116** EOF.
213117*/
213118static int fts5ExprNodeTest_STRING(
213119  Fts5Expr *pExpr,                /* Expression pPhrase belongs to */
213120  Fts5ExprNode *pNode
213121){
213122  Fts5ExprNearset *pNear = pNode->pNear;
213123  Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
213124  int rc = SQLITE_OK;
213125  i64 iLast;                      /* Lastest rowid any iterator points to */
213126  int i, j;                       /* Phrase and token index, respectively */
213127  int bMatch;                     /* True if all terms are at the same rowid */
213128  const int bDesc = pExpr->bDesc;
213129
213130  /* Check that this node should not be FTS5_TERM */
213131  assert( pNear->nPhrase>1
213132       || pNear->apPhrase[0]->nTerm>1
213133       || pNear->apPhrase[0]->aTerm[0].pSynonym
213134       || pNear->apPhrase[0]->aTerm[0].bFirst
213135  );
213136
213137  /* Initialize iLast, the "lastest" rowid any iterator points to. If the
213138  ** iterator skips through rowids in the default ascending order, this means
213139  ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
213140  ** means the minimum rowid.  */
213141  if( pLeft->aTerm[0].pSynonym ){
213142    iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0);
213143  }else{
213144    iLast = pLeft->aTerm[0].pIter->iRowid;
213145  }
213146
213147  do {
213148    bMatch = 1;
213149    for(i=0; i<pNear->nPhrase; i++){
213150      Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
213151      for(j=0; j<pPhrase->nTerm; j++){
213152        Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
213153        if( pTerm->pSynonym ){
213154          i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0);
213155          if( iRowid==iLast ) continue;
213156          bMatch = 0;
213157          if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){
213158            pNode->bNomatch = 0;
213159            pNode->bEof = 1;
213160            return rc;
213161          }
213162        }else{
213163          Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
213164          if( pIter->iRowid==iLast || pIter->bEof ) continue;
213165          bMatch = 0;
213166          if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){
213167            return rc;
213168          }
213169        }
213170      }
213171    }
213172  }while( bMatch==0 );
213173
213174  pNode->iRowid = iLast;
213175  pNode->bNomatch = ((0==fts5ExprNearTest(&rc, pExpr, pNode)) && rc==SQLITE_OK);
213176  assert( pNode->bEof==0 || pNode->bNomatch==0 );
213177
213178  return rc;
213179}
213180
213181/*
213182** Advance the first term iterator in the first phrase of pNear. Set output
213183** variable *pbEof to true if it reaches EOF or if an error occurs.
213184**
213185** Return SQLITE_OK if successful, or an SQLite error code if an error
213186** occurs.
213187*/
213188static int fts5ExprNodeNext_STRING(
213189  Fts5Expr *pExpr,                /* Expression pPhrase belongs to */
213190  Fts5ExprNode *pNode,            /* FTS5_STRING or FTS5_TERM node */
213191  int bFromValid,
213192  i64 iFrom
213193){
213194  Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
213195  int rc = SQLITE_OK;
213196
213197  pNode->bNomatch = 0;
213198  if( pTerm->pSynonym ){
213199    int bEof = 1;
213200    Fts5ExprTerm *p;
213201
213202    /* Find the firstest rowid any synonym points to. */
213203    i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0);
213204
213205    /* Advance each iterator that currently points to iRowid. Or, if iFrom
213206    ** is valid - each iterator that points to a rowid before iFrom.  */
213207    for(p=pTerm; p; p=p->pSynonym){
213208      if( sqlite3Fts5IterEof(p->pIter)==0 ){
213209        i64 ii = p->pIter->iRowid;
213210        if( ii==iRowid
213211         || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
213212        ){
213213          if( bFromValid ){
213214            rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom);
213215          }else{
213216            rc = sqlite3Fts5IterNext(p->pIter);
213217          }
213218          if( rc!=SQLITE_OK ) break;
213219          if( sqlite3Fts5IterEof(p->pIter)==0 ){
213220            bEof = 0;
213221          }
213222        }else{
213223          bEof = 0;
213224        }
213225      }
213226    }
213227
213228    /* Set the EOF flag if either all synonym iterators are at EOF or an
213229    ** error has occurred.  */
213230    pNode->bEof = (rc || bEof);
213231  }else{
213232    Fts5IndexIter *pIter = pTerm->pIter;
213233
213234    assert( Fts5NodeIsString(pNode) );
213235    if( bFromValid ){
213236      rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
213237    }else{
213238      rc = sqlite3Fts5IterNext(pIter);
213239    }
213240
213241    pNode->bEof = (rc || sqlite3Fts5IterEof(pIter));
213242  }
213243
213244  if( pNode->bEof==0 ){
213245    assert( rc==SQLITE_OK );
213246    rc = fts5ExprNodeTest_STRING(pExpr, pNode);
213247  }
213248
213249  return rc;
213250}
213251
213252
213253static int fts5ExprNodeTest_TERM(
213254  Fts5Expr *pExpr,                /* Expression that pNear is a part of */
213255  Fts5ExprNode *pNode             /* The "NEAR" node (FTS5_TERM) */
213256){
213257  /* As this "NEAR" object is actually a single phrase that consists
213258  ** of a single term only, grab pointers into the poslist managed by the
213259  ** fts5_index.c iterator object. This is much faster than synthesizing
213260  ** a new poslist the way we have to for more complicated phrase or NEAR
213261  ** expressions.  */
213262  Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
213263  Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
213264
213265  assert( pNode->eType==FTS5_TERM );
213266  assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 );
213267  assert( pPhrase->aTerm[0].pSynonym==0 );
213268
213269  pPhrase->poslist.n = pIter->nData;
213270  if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL ){
213271    pPhrase->poslist.p = (u8*)pIter->pData;
213272  }
213273  pNode->iRowid = pIter->iRowid;
213274  pNode->bNomatch = (pPhrase->poslist.n==0);
213275  return SQLITE_OK;
213276}
213277
213278/*
213279** xNext() method for a node of type FTS5_TERM.
213280*/
213281static int fts5ExprNodeNext_TERM(
213282  Fts5Expr *pExpr,
213283  Fts5ExprNode *pNode,
213284  int bFromValid,
213285  i64 iFrom
213286){
213287  int rc;
213288  Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
213289
213290  assert( pNode->bEof==0 );
213291  if( bFromValid ){
213292    rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
213293  }else{
213294    rc = sqlite3Fts5IterNext(pIter);
213295  }
213296  if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){
213297    rc = fts5ExprNodeTest_TERM(pExpr, pNode);
213298  }else{
213299    pNode->bEof = 1;
213300    pNode->bNomatch = 0;
213301  }
213302  return rc;
213303}
213304
213305static void fts5ExprNodeTest_OR(
213306  Fts5Expr *pExpr,                /* Expression of which pNode is a part */
213307  Fts5ExprNode *pNode             /* Expression node to test */
213308){
213309  Fts5ExprNode *pNext = pNode->apChild[0];
213310  int i;
213311
213312  for(i=1; i<pNode->nChild; i++){
213313    Fts5ExprNode *pChild = pNode->apChild[i];
213314    int cmp = fts5NodeCompare(pExpr, pNext, pChild);
213315    if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
213316      pNext = pChild;
213317    }
213318  }
213319  pNode->iRowid = pNext->iRowid;
213320  pNode->bEof = pNext->bEof;
213321  pNode->bNomatch = pNext->bNomatch;
213322}
213323
213324static int fts5ExprNodeNext_OR(
213325  Fts5Expr *pExpr,
213326  Fts5ExprNode *pNode,
213327  int bFromValid,
213328  i64 iFrom
213329){
213330  int i;
213331  i64 iLast = pNode->iRowid;
213332
213333  for(i=0; i<pNode->nChild; i++){
213334    Fts5ExprNode *p1 = pNode->apChild[i];
213335    assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 );
213336    if( p1->bEof==0 ){
213337      if( (p1->iRowid==iLast)
213338       || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
213339      ){
213340        int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
213341        if( rc!=SQLITE_OK ){
213342          pNode->bNomatch = 0;
213343          return rc;
213344        }
213345      }
213346    }
213347  }
213348
213349  fts5ExprNodeTest_OR(pExpr, pNode);
213350  return SQLITE_OK;
213351}
213352
213353/*
213354** Argument pNode is an FTS5_AND node.
213355*/
213356static int fts5ExprNodeTest_AND(
213357  Fts5Expr *pExpr,                /* Expression pPhrase belongs to */
213358  Fts5ExprNode *pAnd              /* FTS5_AND node to advance */
213359){
213360  int iChild;
213361  i64 iLast = pAnd->iRowid;
213362  int rc = SQLITE_OK;
213363  int bMatch;
213364
213365  assert( pAnd->bEof==0 );
213366  do {
213367    pAnd->bNomatch = 0;
213368    bMatch = 1;
213369    for(iChild=0; iChild<pAnd->nChild; iChild++){
213370      Fts5ExprNode *pChild = pAnd->apChild[iChild];
213371      int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
213372      if( cmp>0 ){
213373        /* Advance pChild until it points to iLast or laster */
213374        rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
213375        if( rc!=SQLITE_OK ){
213376          pAnd->bNomatch = 0;
213377          return rc;
213378        }
213379      }
213380
213381      /* If the child node is now at EOF, so is the parent AND node. Otherwise,
213382      ** the child node is guaranteed to have advanced at least as far as
213383      ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
213384      ** new lastest rowid seen so far.  */
213385      assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 );
213386      if( pChild->bEof ){
213387        fts5ExprSetEof(pAnd);
213388        bMatch = 1;
213389        break;
213390      }else if( iLast!=pChild->iRowid ){
213391        bMatch = 0;
213392        iLast = pChild->iRowid;
213393      }
213394
213395      if( pChild->bNomatch ){
213396        pAnd->bNomatch = 1;
213397      }
213398    }
213399  }while( bMatch==0 );
213400
213401  if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
213402    fts5ExprNodeZeroPoslist(pAnd);
213403  }
213404  pAnd->iRowid = iLast;
213405  return SQLITE_OK;
213406}
213407
213408static int fts5ExprNodeNext_AND(
213409  Fts5Expr *pExpr,
213410  Fts5ExprNode *pNode,
213411  int bFromValid,
213412  i64 iFrom
213413){
213414  int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
213415  if( rc==SQLITE_OK ){
213416    rc = fts5ExprNodeTest_AND(pExpr, pNode);
213417  }else{
213418    pNode->bNomatch = 0;
213419  }
213420  return rc;
213421}
213422
213423static int fts5ExprNodeTest_NOT(
213424  Fts5Expr *pExpr,                /* Expression pPhrase belongs to */
213425  Fts5ExprNode *pNode             /* FTS5_NOT node to advance */
213426){
213427  int rc = SQLITE_OK;
213428  Fts5ExprNode *p1 = pNode->apChild[0];
213429  Fts5ExprNode *p2 = pNode->apChild[1];
213430  assert( pNode->nChild==2 );
213431
213432  while( rc==SQLITE_OK && p1->bEof==0 ){
213433    int cmp = fts5NodeCompare(pExpr, p1, p2);
213434    if( cmp>0 ){
213435      rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid);
213436      cmp = fts5NodeCompare(pExpr, p1, p2);
213437    }
213438    assert( rc!=SQLITE_OK || cmp<=0 );
213439    if( cmp || p2->bNomatch ) break;
213440    rc = fts5ExprNodeNext(pExpr, p1, 0, 0);
213441  }
213442  pNode->bEof = p1->bEof;
213443  pNode->bNomatch = p1->bNomatch;
213444  pNode->iRowid = p1->iRowid;
213445  if( p1->bEof ){
213446    fts5ExprNodeZeroPoslist(p2);
213447  }
213448  return rc;
213449}
213450
213451static int fts5ExprNodeNext_NOT(
213452  Fts5Expr *pExpr,
213453  Fts5ExprNode *pNode,
213454  int bFromValid,
213455  i64 iFrom
213456){
213457  int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
213458  if( rc==SQLITE_OK ){
213459    rc = fts5ExprNodeTest_NOT(pExpr, pNode);
213460  }
213461  if( rc!=SQLITE_OK ){
213462    pNode->bNomatch = 0;
213463  }
213464  return rc;
213465}
213466
213467/*
213468** If pNode currently points to a match, this function returns SQLITE_OK
213469** without modifying it. Otherwise, pNode is advanced until it does point
213470** to a match or EOF is reached.
213471*/
213472static int fts5ExprNodeTest(
213473  Fts5Expr *pExpr,                /* Expression of which pNode is a part */
213474  Fts5ExprNode *pNode             /* Expression node to test */
213475){
213476  int rc = SQLITE_OK;
213477  if( pNode->bEof==0 ){
213478    switch( pNode->eType ){
213479
213480      case FTS5_STRING: {
213481        rc = fts5ExprNodeTest_STRING(pExpr, pNode);
213482        break;
213483      }
213484
213485      case FTS5_TERM: {
213486        rc = fts5ExprNodeTest_TERM(pExpr, pNode);
213487        break;
213488      }
213489
213490      case FTS5_AND: {
213491        rc = fts5ExprNodeTest_AND(pExpr, pNode);
213492        break;
213493      }
213494
213495      case FTS5_OR: {
213496        fts5ExprNodeTest_OR(pExpr, pNode);
213497        break;
213498      }
213499
213500      default: assert( pNode->eType==FTS5_NOT ); {
213501        rc = fts5ExprNodeTest_NOT(pExpr, pNode);
213502        break;
213503      }
213504    }
213505  }
213506  return rc;
213507}
213508
213509
213510/*
213511** Set node pNode, which is part of expression pExpr, to point to the first
213512** match. If there are no matches, set the Node.bEof flag to indicate EOF.
213513**
213514** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
213515** It is not an error if there are no matches.
213516*/
213517static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
213518  int rc = SQLITE_OK;
213519  pNode->bEof = 0;
213520  pNode->bNomatch = 0;
213521
213522  if( Fts5NodeIsString(pNode) ){
213523    /* Initialize all term iterators in the NEAR object. */
213524    rc = fts5ExprNearInitAll(pExpr, pNode);
213525  }else if( pNode->xNext==0 ){
213526    pNode->bEof = 1;
213527  }else{
213528    int i;
213529    int nEof = 0;
213530    for(i=0; i<pNode->nChild && rc==SQLITE_OK; i++){
213531      Fts5ExprNode *pChild = pNode->apChild[i];
213532      rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]);
213533      assert( pChild->bEof==0 || pChild->bEof==1 );
213534      nEof += pChild->bEof;
213535    }
213536    pNode->iRowid = pNode->apChild[0]->iRowid;
213537
213538    switch( pNode->eType ){
213539      case FTS5_AND:
213540        if( nEof>0 ) fts5ExprSetEof(pNode);
213541        break;
213542
213543      case FTS5_OR:
213544        if( pNode->nChild==nEof ) fts5ExprSetEof(pNode);
213545        break;
213546
213547      default:
213548        assert( pNode->eType==FTS5_NOT );
213549        pNode->bEof = pNode->apChild[0]->bEof;
213550        break;
213551    }
213552  }
213553
213554  if( rc==SQLITE_OK ){
213555    rc = fts5ExprNodeTest(pExpr, pNode);
213556  }
213557  return rc;
213558}
213559
213560
213561/*
213562** Begin iterating through the set of documents in index pIdx matched by
213563** the MATCH expression passed as the first argument. If the "bDesc"
213564** parameter is passed a non-zero value, iteration is in descending rowid
213565** order. Or, if it is zero, in ascending order.
213566**
213567** If iterating in ascending rowid order (bDesc==0), the first document
213568** visited is that with the smallest rowid that is larger than or equal
213569** to parameter iFirst. Or, if iterating in ascending order (bDesc==1),
213570** then the first document visited must have a rowid smaller than or
213571** equal to iFirst.
213572**
213573** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
213574** is not considered an error if the query does not match any documents.
213575*/
213576static int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
213577  Fts5ExprNode *pRoot = p->pRoot;
213578  int rc;                         /* Return code */
213579
213580  p->pIndex = pIdx;
213581  p->bDesc = bDesc;
213582  rc = fts5ExprNodeFirst(p, pRoot);
213583
213584  /* If not at EOF but the current rowid occurs earlier than iFirst in
213585  ** the iteration order, move to document iFirst or later. */
213586  if( rc==SQLITE_OK
213587   && 0==pRoot->bEof
213588   && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0
213589  ){
213590    rc = fts5ExprNodeNext(p, pRoot, 1, iFirst);
213591  }
213592
213593  /* If the iterator is not at a real match, skip forward until it is. */
213594  while( pRoot->bNomatch ){
213595    assert( pRoot->bEof==0 && rc==SQLITE_OK );
213596    rc = fts5ExprNodeNext(p, pRoot, 0, 0);
213597  }
213598  return rc;
213599}
213600
213601/*
213602** Move to the next document
213603**
213604** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
213605** is not considered an error if the query does not match any documents.
213606*/
213607static int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
213608  int rc;
213609  Fts5ExprNode *pRoot = p->pRoot;
213610  assert( pRoot->bEof==0 && pRoot->bNomatch==0 );
213611  do {
213612    rc = fts5ExprNodeNext(p, pRoot, 0, 0);
213613    assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) );
213614  }while( pRoot->bNomatch );
213615  if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){
213616    pRoot->bEof = 1;
213617  }
213618  return rc;
213619}
213620
213621static int sqlite3Fts5ExprEof(Fts5Expr *p){
213622  return p->pRoot->bEof;
213623}
213624
213625static i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
213626  return p->pRoot->iRowid;
213627}
213628
213629static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){
213630  int rc = SQLITE_OK;
213631  *pz = sqlite3Fts5Strndup(&rc, pToken->p, pToken->n);
213632  return rc;
213633}
213634
213635/*
213636** Free the phrase object passed as the only argument.
213637*/
213638static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){
213639  if( pPhrase ){
213640    int i;
213641    for(i=0; i<pPhrase->nTerm; i++){
213642      Fts5ExprTerm *pSyn;
213643      Fts5ExprTerm *pNext;
213644      Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
213645      sqlite3_free(pTerm->zTerm);
213646      sqlite3Fts5IterClose(pTerm->pIter);
213647      for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
213648        pNext = pSyn->pSynonym;
213649        sqlite3Fts5IterClose(pSyn->pIter);
213650        fts5BufferFree((Fts5Buffer*)&pSyn[1]);
213651        sqlite3_free(pSyn);
213652      }
213653    }
213654    if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist);
213655    sqlite3_free(pPhrase);
213656  }
213657}
213658
213659/*
213660** Set the "bFirst" flag on the first token of the phrase passed as the
213661** only argument.
213662*/
213663static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){
213664  if( pPhrase && pPhrase->nTerm ){
213665    pPhrase->aTerm[0].bFirst = 1;
213666  }
213667}
213668
213669/*
213670** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
213671** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
213672** appended to it and the results returned.
213673**
213674** If an OOM error occurs, both the pNear and pPhrase objects are freed and
213675** NULL returned.
213676*/
213677static Fts5ExprNearset *sqlite3Fts5ParseNearset(
213678  Fts5Parse *pParse,              /* Parse context */
213679  Fts5ExprNearset *pNear,         /* Existing nearset, or NULL */
213680  Fts5ExprPhrase *pPhrase         /* Recently parsed phrase */
213681){
213682  const int SZALLOC = 8;
213683  Fts5ExprNearset *pRet = 0;
213684
213685  if( pParse->rc==SQLITE_OK ){
213686    if( pPhrase==0 ){
213687      return pNear;
213688    }
213689    if( pNear==0 ){
213690      sqlite3_int64 nByte;
213691      nByte = sizeof(Fts5ExprNearset) + SZALLOC * sizeof(Fts5ExprPhrase*);
213692      pRet = sqlite3_malloc64(nByte);
213693      if( pRet==0 ){
213694        pParse->rc = SQLITE_NOMEM;
213695      }else{
213696        memset(pRet, 0, (size_t)nByte);
213697      }
213698    }else if( (pNear->nPhrase % SZALLOC)==0 ){
213699      int nNew = pNear->nPhrase + SZALLOC;
213700      sqlite3_int64 nByte;
213701
213702      nByte = sizeof(Fts5ExprNearset) + nNew * sizeof(Fts5ExprPhrase*);
213703      pRet = (Fts5ExprNearset*)sqlite3_realloc64(pNear, nByte);
213704      if( pRet==0 ){
213705        pParse->rc = SQLITE_NOMEM;
213706      }
213707    }else{
213708      pRet = pNear;
213709    }
213710  }
213711
213712  if( pRet==0 ){
213713    assert( pParse->rc!=SQLITE_OK );
213714    sqlite3Fts5ParseNearsetFree(pNear);
213715    sqlite3Fts5ParsePhraseFree(pPhrase);
213716  }else{
213717    if( pRet->nPhrase>0 ){
213718      Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1];
213719      assert( pLast==pParse->apPhrase[pParse->nPhrase-2] );
213720      if( pPhrase->nTerm==0 ){
213721        fts5ExprPhraseFree(pPhrase);
213722        pRet->nPhrase--;
213723        pParse->nPhrase--;
213724        pPhrase = pLast;
213725      }else if( pLast->nTerm==0 ){
213726        fts5ExprPhraseFree(pLast);
213727        pParse->apPhrase[pParse->nPhrase-2] = pPhrase;
213728        pParse->nPhrase--;
213729        pRet->nPhrase--;
213730      }
213731    }
213732    pRet->apPhrase[pRet->nPhrase++] = pPhrase;
213733  }
213734  return pRet;
213735}
213736
213737typedef struct TokenCtx TokenCtx;
213738struct TokenCtx {
213739  Fts5ExprPhrase *pPhrase;
213740  int rc;
213741};
213742
213743/*
213744** Callback for tokenizing terms used by ParseTerm().
213745*/
213746static int fts5ParseTokenize(
213747  void *pContext,                 /* Pointer to Fts5InsertCtx object */
213748  int tflags,                     /* Mask of FTS5_TOKEN_* flags */
213749  const char *pToken,             /* Buffer containing token */
213750  int nToken,                     /* Size of token in bytes */
213751  int iUnused1,                   /* Start offset of token */
213752  int iUnused2                    /* End offset of token */
213753){
213754  int rc = SQLITE_OK;
213755  const int SZALLOC = 8;
213756  TokenCtx *pCtx = (TokenCtx*)pContext;
213757  Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
213758
213759  UNUSED_PARAM2(iUnused1, iUnused2);
213760
213761  /* If an error has already occurred, this is a no-op */
213762  if( pCtx->rc!=SQLITE_OK ) return pCtx->rc;
213763  if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
213764
213765  if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED) ){
213766    Fts5ExprTerm *pSyn;
213767    sqlite3_int64 nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1;
213768    pSyn = (Fts5ExprTerm*)sqlite3_malloc64(nByte);
213769    if( pSyn==0 ){
213770      rc = SQLITE_NOMEM;
213771    }else{
213772      memset(pSyn, 0, (size_t)nByte);
213773      pSyn->zTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer);
213774      memcpy(pSyn->zTerm, pToken, nToken);
213775      pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
213776      pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
213777    }
213778  }else{
213779    Fts5ExprTerm *pTerm;
213780    if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){
213781      Fts5ExprPhrase *pNew;
213782      int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0);
213783
213784      pNew = (Fts5ExprPhrase*)sqlite3_realloc64(pPhrase,
213785          sizeof(Fts5ExprPhrase) + sizeof(Fts5ExprTerm) * nNew
213786      );
213787      if( pNew==0 ){
213788        rc = SQLITE_NOMEM;
213789      }else{
213790        if( pPhrase==0 ) memset(pNew, 0, sizeof(Fts5ExprPhrase));
213791        pCtx->pPhrase = pPhrase = pNew;
213792        pNew->nTerm = nNew - SZALLOC;
213793      }
213794    }
213795
213796    if( rc==SQLITE_OK ){
213797      pTerm = &pPhrase->aTerm[pPhrase->nTerm++];
213798      memset(pTerm, 0, sizeof(Fts5ExprTerm));
213799      pTerm->zTerm = sqlite3Fts5Strndup(&rc, pToken, nToken);
213800    }
213801  }
213802
213803  pCtx->rc = rc;
213804  return rc;
213805}
213806
213807
213808/*
213809** Free the phrase object passed as the only argument.
213810*/
213811static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){
213812  fts5ExprPhraseFree(pPhrase);
213813}
213814
213815/*
213816** Free the phrase object passed as the second argument.
213817*/
213818static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){
213819  if( pNear ){
213820    int i;
213821    for(i=0; i<pNear->nPhrase; i++){
213822      fts5ExprPhraseFree(pNear->apPhrase[i]);
213823    }
213824    sqlite3_free(pNear->pColset);
213825    sqlite3_free(pNear);
213826  }
213827}
213828
213829static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){
213830  assert( pParse->pExpr==0 );
213831  pParse->pExpr = p;
213832}
213833
213834/*
213835** This function is called by the parser to process a string token. The
213836** string may or may not be quoted. In any case it is tokenized and a
213837** phrase object consisting of all tokens returned.
213838*/
213839static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
213840  Fts5Parse *pParse,              /* Parse context */
213841  Fts5ExprPhrase *pAppend,        /* Phrase to append to */
213842  Fts5Token *pToken,              /* String to tokenize */
213843  int bPrefix                     /* True if there is a trailing "*" */
213844){
213845  Fts5Config *pConfig = pParse->pConfig;
213846  TokenCtx sCtx;                  /* Context object passed to callback */
213847  int rc;                         /* Tokenize return code */
213848  char *z = 0;
213849
213850  memset(&sCtx, 0, sizeof(TokenCtx));
213851  sCtx.pPhrase = pAppend;
213852
213853  rc = fts5ParseStringFromToken(pToken, &z);
213854  if( rc==SQLITE_OK ){
213855    int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
213856    int n;
213857    sqlite3Fts5Dequote(z);
213858    n = (int)strlen(z);
213859    rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
213860  }
213861  sqlite3_free(z);
213862  if( rc || (rc = sCtx.rc) ){
213863    pParse->rc = rc;
213864    fts5ExprPhraseFree(sCtx.pPhrase);
213865    sCtx.pPhrase = 0;
213866  }else{
213867
213868    if( pAppend==0 ){
213869      if( (pParse->nPhrase % 8)==0 ){
213870        sqlite3_int64 nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8);
213871        Fts5ExprPhrase **apNew;
213872        apNew = (Fts5ExprPhrase**)sqlite3_realloc64(pParse->apPhrase, nByte);
213873        if( apNew==0 ){
213874          pParse->rc = SQLITE_NOMEM;
213875          fts5ExprPhraseFree(sCtx.pPhrase);
213876          return 0;
213877        }
213878        pParse->apPhrase = apNew;
213879      }
213880      pParse->nPhrase++;
213881    }
213882
213883    if( sCtx.pPhrase==0 ){
213884      /* This happens when parsing a token or quoted phrase that contains
213885      ** no token characters at all. (e.g ... MATCH '""'). */
213886      sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
213887    }else if( sCtx.pPhrase->nTerm ){
213888      sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
213889    }
213890    pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
213891  }
213892
213893  return sCtx.pPhrase;
213894}
213895
213896/*
213897** Create a new FTS5 expression by cloning phrase iPhrase of the
213898** expression passed as the second argument.
213899*/
213900static int sqlite3Fts5ExprClonePhrase(
213901  Fts5Expr *pExpr,
213902  int iPhrase,
213903  Fts5Expr **ppNew
213904){
213905  int rc = SQLITE_OK;             /* Return code */
213906  Fts5ExprPhrase *pOrig;          /* The phrase extracted from pExpr */
213907  Fts5Expr *pNew = 0;             /* Expression to return via *ppNew */
213908  TokenCtx sCtx = {0,0};          /* Context object for fts5ParseTokenize */
213909
213910  pOrig = pExpr->apExprPhrase[iPhrase];
213911  pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
213912  if( rc==SQLITE_OK ){
213913    pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
213914        sizeof(Fts5ExprPhrase*));
213915  }
213916  if( rc==SQLITE_OK ){
213917    pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc,
213918        sizeof(Fts5ExprNode));
213919  }
213920  if( rc==SQLITE_OK ){
213921    pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc,
213922        sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*));
213923  }
213924  if( rc==SQLITE_OK ){
213925    Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
213926    if( pColsetOrig ){
213927      sqlite3_int64 nByte;
213928      Fts5Colset *pColset;
213929      nByte = sizeof(Fts5Colset) + (pColsetOrig->nCol-1) * sizeof(int);
213930      pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&rc, nByte);
213931      if( pColset ){
213932        memcpy(pColset, pColsetOrig, (size_t)nByte);
213933      }
213934      pNew->pRoot->pNear->pColset = pColset;
213935    }
213936  }
213937
213938  if( pOrig->nTerm ){
213939    int i;                          /* Used to iterate through phrase terms */
213940    for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){
213941      int tflags = 0;
213942      Fts5ExprTerm *p;
213943      for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){
213944        const char *zTerm = p->zTerm;
213945        rc = fts5ParseTokenize((void*)&sCtx, tflags, zTerm, (int)strlen(zTerm),
213946            0, 0);
213947        tflags = FTS5_TOKEN_COLOCATED;
213948      }
213949      if( rc==SQLITE_OK ){
213950        sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
213951        sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
213952      }
213953    }
213954  }else{
213955    /* This happens when parsing a token or quoted phrase that contains
213956    ** no token characters at all. (e.g ... MATCH '""'). */
213957    sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase));
213958  }
213959
213960  if( rc==SQLITE_OK ){
213961    /* All the allocations succeeded. Put the expression object together. */
213962    pNew->pIndex = pExpr->pIndex;
213963    pNew->pConfig = pExpr->pConfig;
213964    pNew->nPhrase = 1;
213965    pNew->apExprPhrase[0] = sCtx.pPhrase;
213966    pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase;
213967    pNew->pRoot->pNear->nPhrase = 1;
213968    sCtx.pPhrase->pNode = pNew->pRoot;
213969
213970    if( pOrig->nTerm==1
213971     && pOrig->aTerm[0].pSynonym==0
213972     && pOrig->aTerm[0].bFirst==0
213973    ){
213974      pNew->pRoot->eType = FTS5_TERM;
213975      pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
213976    }else{
213977      pNew->pRoot->eType = FTS5_STRING;
213978      pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
213979    }
213980  }else{
213981    sqlite3Fts5ExprFree(pNew);
213982    fts5ExprPhraseFree(sCtx.pPhrase);
213983    pNew = 0;
213984  }
213985
213986  *ppNew = pNew;
213987  return rc;
213988}
213989
213990
213991/*
213992** Token pTok has appeared in a MATCH expression where the NEAR operator
213993** is expected. If token pTok does not contain "NEAR", store an error
213994** in the pParse object.
213995*/
213996static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){
213997  if( pTok->n!=4 || memcmp("NEAR", pTok->p, 4) ){
213998    sqlite3Fts5ParseError(
213999        pParse, "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p
214000    );
214001  }
214002}
214003
214004static void sqlite3Fts5ParseSetDistance(
214005  Fts5Parse *pParse,
214006  Fts5ExprNearset *pNear,
214007  Fts5Token *p
214008){
214009  if( pNear ){
214010    int nNear = 0;
214011    int i;
214012    if( p->n ){
214013      for(i=0; i<p->n; i++){
214014        char c = (char)p->p[i];
214015        if( c<'0' || c>'9' ){
214016          sqlite3Fts5ParseError(
214017              pParse, "expected integer, got \"%.*s\"", p->n, p->p
214018              );
214019          return;
214020        }
214021        nNear = nNear * 10 + (p->p[i] - '0');
214022      }
214023    }else{
214024      nNear = FTS5_DEFAULT_NEARDIST;
214025    }
214026    pNear->nNear = nNear;
214027  }
214028}
214029
214030/*
214031** The second argument passed to this function may be NULL, or it may be
214032** an existing Fts5Colset object. This function returns a pointer to
214033** a new colset object containing the contents of (p) with new value column
214034** number iCol appended.
214035**
214036** If an OOM error occurs, store an error code in pParse and return NULL.
214037** The old colset object (if any) is not freed in this case.
214038*/
214039static Fts5Colset *fts5ParseColset(
214040  Fts5Parse *pParse,              /* Store SQLITE_NOMEM here if required */
214041  Fts5Colset *p,                  /* Existing colset object */
214042  int iCol                        /* New column to add to colset object */
214043){
214044  int nCol = p ? p->nCol : 0;     /* Num. columns already in colset object */
214045  Fts5Colset *pNew;               /* New colset object to return */
214046
214047  assert( pParse->rc==SQLITE_OK );
214048  assert( iCol>=0 && iCol<pParse->pConfig->nCol );
214049
214050  pNew = sqlite3_realloc64(p, sizeof(Fts5Colset) + sizeof(int)*nCol);
214051  if( pNew==0 ){
214052    pParse->rc = SQLITE_NOMEM;
214053  }else{
214054    int *aiCol = pNew->aiCol;
214055    int i, j;
214056    for(i=0; i<nCol; i++){
214057      if( aiCol[i]==iCol ) return pNew;
214058      if( aiCol[i]>iCol ) break;
214059    }
214060    for(j=nCol; j>i; j--){
214061      aiCol[j] = aiCol[j-1];
214062    }
214063    aiCol[i] = iCol;
214064    pNew->nCol = nCol+1;
214065
214066#ifndef NDEBUG
214067    /* Check that the array is in order and contains no duplicate entries. */
214068    for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] );
214069#endif
214070  }
214071
214072  return pNew;
214073}
214074
214075/*
214076** Allocate and return an Fts5Colset object specifying the inverse of
214077** the colset passed as the second argument. Free the colset passed
214078** as the second argument before returning.
214079*/
214080static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){
214081  Fts5Colset *pRet;
214082  int nCol = pParse->pConfig->nCol;
214083
214084  pRet = (Fts5Colset*)sqlite3Fts5MallocZero(&pParse->rc,
214085      sizeof(Fts5Colset) + sizeof(int)*nCol
214086  );
214087  if( pRet ){
214088    int i;
214089    int iOld = 0;
214090    for(i=0; i<nCol; i++){
214091      if( iOld>=p->nCol || p->aiCol[iOld]!=i ){
214092        pRet->aiCol[pRet->nCol++] = i;
214093      }else{
214094        iOld++;
214095      }
214096    }
214097  }
214098
214099  sqlite3_free(p);
214100  return pRet;
214101}
214102
214103static Fts5Colset *sqlite3Fts5ParseColset(
214104  Fts5Parse *pParse,              /* Store SQLITE_NOMEM here if required */
214105  Fts5Colset *pColset,            /* Existing colset object */
214106  Fts5Token *p
214107){
214108  Fts5Colset *pRet = 0;
214109  int iCol;
214110  char *z;                        /* Dequoted copy of token p */
214111
214112  z = sqlite3Fts5Strndup(&pParse->rc, p->p, p->n);
214113  if( pParse->rc==SQLITE_OK ){
214114    Fts5Config *pConfig = pParse->pConfig;
214115    sqlite3Fts5Dequote(z);
214116    for(iCol=0; iCol<pConfig->nCol; iCol++){
214117      if( 0==sqlite3_stricmp(pConfig->azCol[iCol], z) ) break;
214118    }
214119    if( iCol==pConfig->nCol ){
214120      sqlite3Fts5ParseError(pParse, "no such column: %s", z);
214121    }else{
214122      pRet = fts5ParseColset(pParse, pColset, iCol);
214123    }
214124    sqlite3_free(z);
214125  }
214126
214127  if( pRet==0 ){
214128    assert( pParse->rc!=SQLITE_OK );
214129    sqlite3_free(pColset);
214130  }
214131
214132  return pRet;
214133}
214134
214135/*
214136** If argument pOrig is NULL, or if (*pRc) is set to anything other than
214137** SQLITE_OK when this function is called, NULL is returned.
214138**
214139** Otherwise, a copy of (*pOrig) is made into memory obtained from
214140** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation
214141** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned.
214142*/
214143static Fts5Colset *fts5CloneColset(int *pRc, Fts5Colset *pOrig){
214144  Fts5Colset *pRet;
214145  if( pOrig ){
214146    sqlite3_int64 nByte = sizeof(Fts5Colset) + (pOrig->nCol-1) * sizeof(int);
214147    pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc, nByte);
214148    if( pRet ){
214149      memcpy(pRet, pOrig, (size_t)nByte);
214150    }
214151  }else{
214152    pRet = 0;
214153  }
214154  return pRet;
214155}
214156
214157/*
214158** Remove from colset pColset any columns that are not also in colset pMerge.
214159*/
214160static void fts5MergeColset(Fts5Colset *pColset, Fts5Colset *pMerge){
214161  int iIn = 0;          /* Next input in pColset */
214162  int iMerge = 0;       /* Next input in pMerge */
214163  int iOut = 0;         /* Next output slot in pColset */
214164
214165  while( iIn<pColset->nCol && iMerge<pMerge->nCol ){
214166    int iDiff = pColset->aiCol[iIn] - pMerge->aiCol[iMerge];
214167    if( iDiff==0 ){
214168      pColset->aiCol[iOut++] = pMerge->aiCol[iMerge];
214169      iMerge++;
214170      iIn++;
214171    }else if( iDiff>0 ){
214172      iMerge++;
214173    }else{
214174      iIn++;
214175    }
214176  }
214177  pColset->nCol = iOut;
214178}
214179
214180/*
214181** Recursively apply colset pColset to expression node pNode and all of
214182** its decendents. If (*ppFree) is not NULL, it contains a spare copy
214183** of pColset. This function may use the spare copy and set (*ppFree) to
214184** zero, or it may create copies of pColset using fts5CloneColset().
214185*/
214186static void fts5ParseSetColset(
214187  Fts5Parse *pParse,
214188  Fts5ExprNode *pNode,
214189  Fts5Colset *pColset,
214190  Fts5Colset **ppFree
214191){
214192  if( pParse->rc==SQLITE_OK ){
214193    assert( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING
214194         || pNode->eType==FTS5_AND  || pNode->eType==FTS5_OR
214195         || pNode->eType==FTS5_NOT  || pNode->eType==FTS5_EOF
214196    );
214197    if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
214198      Fts5ExprNearset *pNear = pNode->pNear;
214199      if( pNear->pColset ){
214200        fts5MergeColset(pNear->pColset, pColset);
214201        if( pNear->pColset->nCol==0 ){
214202          pNode->eType = FTS5_EOF;
214203          pNode->xNext = 0;
214204        }
214205      }else if( *ppFree ){
214206        pNear->pColset = pColset;
214207        *ppFree = 0;
214208      }else{
214209        pNear->pColset = fts5CloneColset(&pParse->rc, pColset);
214210      }
214211    }else{
214212      int i;
214213      assert( pNode->eType!=FTS5_EOF || pNode->nChild==0 );
214214      for(i=0; i<pNode->nChild; i++){
214215        fts5ParseSetColset(pParse, pNode->apChild[i], pColset, ppFree);
214216      }
214217    }
214218  }
214219}
214220
214221/*
214222** Apply colset pColset to expression node pExpr and all of its descendents.
214223*/
214224static void sqlite3Fts5ParseSetColset(
214225  Fts5Parse *pParse,
214226  Fts5ExprNode *pExpr,
214227  Fts5Colset *pColset
214228){
214229  Fts5Colset *pFree = pColset;
214230  if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE ){
214231    pParse->rc = SQLITE_ERROR;
214232    pParse->zErr = sqlite3_mprintf(
214233      "fts5: column queries are not supported (detail=none)"
214234    );
214235  }else{
214236    fts5ParseSetColset(pParse, pExpr, pColset, &pFree);
214237  }
214238  sqlite3_free(pFree);
214239}
214240
214241static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
214242  switch( pNode->eType ){
214243    case FTS5_STRING: {
214244      Fts5ExprNearset *pNear = pNode->pNear;
214245      if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1
214246       && pNear->apPhrase[0]->aTerm[0].pSynonym==0
214247       && pNear->apPhrase[0]->aTerm[0].bFirst==0
214248      ){
214249        pNode->eType = FTS5_TERM;
214250        pNode->xNext = fts5ExprNodeNext_TERM;
214251      }else{
214252        pNode->xNext = fts5ExprNodeNext_STRING;
214253      }
214254      break;
214255    };
214256
214257    case FTS5_OR: {
214258      pNode->xNext = fts5ExprNodeNext_OR;
214259      break;
214260    };
214261
214262    case FTS5_AND: {
214263      pNode->xNext = fts5ExprNodeNext_AND;
214264      break;
214265    };
214266
214267    default: assert( pNode->eType==FTS5_NOT ); {
214268      pNode->xNext = fts5ExprNodeNext_NOT;
214269      break;
214270    };
214271  }
214272}
214273
214274static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
214275  if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){
214276    int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
214277    memcpy(&p->apChild[p->nChild], pSub->apChild, nByte);
214278    p->nChild += pSub->nChild;
214279    sqlite3_free(pSub);
214280  }else{
214281    p->apChild[p->nChild++] = pSub;
214282  }
214283}
214284
214285/*
214286** Allocate and return a new expression object. If anything goes wrong (i.e.
214287** OOM error), leave an error code in pParse and return NULL.
214288*/
214289static Fts5ExprNode *sqlite3Fts5ParseNode(
214290  Fts5Parse *pParse,              /* Parse context */
214291  int eType,                      /* FTS5_STRING, AND, OR or NOT */
214292  Fts5ExprNode *pLeft,            /* Left hand child expression */
214293  Fts5ExprNode *pRight,           /* Right hand child expression */
214294  Fts5ExprNearset *pNear          /* For STRING expressions, the near cluster */
214295){
214296  Fts5ExprNode *pRet = 0;
214297
214298  if( pParse->rc==SQLITE_OK ){
214299    int nChild = 0;               /* Number of children of returned node */
214300    sqlite3_int64 nByte;          /* Bytes of space to allocate for this node */
214301
214302    assert( (eType!=FTS5_STRING && !pNear)
214303         || (eType==FTS5_STRING && !pLeft && !pRight)
214304    );
214305    if( eType==FTS5_STRING && pNear==0 ) return 0;
214306    if( eType!=FTS5_STRING && pLeft==0 ) return pRight;
214307    if( eType!=FTS5_STRING && pRight==0 ) return pLeft;
214308
214309    if( eType==FTS5_NOT ){
214310      nChild = 2;
214311    }else if( eType==FTS5_AND || eType==FTS5_OR ){
214312      nChild = 2;
214313      if( pLeft->eType==eType ) nChild += pLeft->nChild-1;
214314      if( pRight->eType==eType ) nChild += pRight->nChild-1;
214315    }
214316
214317    nByte = sizeof(Fts5ExprNode) + sizeof(Fts5ExprNode*)*(nChild-1);
214318    pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
214319
214320    if( pRet ){
214321      pRet->eType = eType;
214322      pRet->pNear = pNear;
214323      fts5ExprAssignXNext(pRet);
214324      if( eType==FTS5_STRING ){
214325        int iPhrase;
214326        for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
214327          pNear->apPhrase[iPhrase]->pNode = pRet;
214328          if( pNear->apPhrase[iPhrase]->nTerm==0 ){
214329            pRet->xNext = 0;
214330            pRet->eType = FTS5_EOF;
214331          }
214332        }
214333
214334        if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL ){
214335          Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
214336          if( pNear->nPhrase!=1
214337           || pPhrase->nTerm>1
214338           || (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst)
214339          ){
214340            assert( pParse->rc==SQLITE_OK );
214341            pParse->rc = SQLITE_ERROR;
214342            assert( pParse->zErr==0 );
214343            pParse->zErr = sqlite3_mprintf(
214344                "fts5: %s queries are not supported (detail!=full)",
214345                pNear->nPhrase==1 ? "phrase": "NEAR"
214346                );
214347            sqlite3_free(pRet);
214348            pRet = 0;
214349          }
214350        }
214351      }else{
214352        fts5ExprAddChildren(pRet, pLeft);
214353        fts5ExprAddChildren(pRet, pRight);
214354      }
214355    }
214356  }
214357
214358  if( pRet==0 ){
214359    assert( pParse->rc!=SQLITE_OK );
214360    sqlite3Fts5ParseNodeFree(pLeft);
214361    sqlite3Fts5ParseNodeFree(pRight);
214362    sqlite3Fts5ParseNearsetFree(pNear);
214363  }
214364  return pRet;
214365}
214366
214367static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
214368  Fts5Parse *pParse,              /* Parse context */
214369  Fts5ExprNode *pLeft,            /* Left hand child expression */
214370  Fts5ExprNode *pRight            /* Right hand child expression */
214371){
214372  Fts5ExprNode *pRet = 0;
214373  Fts5ExprNode *pPrev;
214374
214375  if( pParse->rc ){
214376    sqlite3Fts5ParseNodeFree(pLeft);
214377    sqlite3Fts5ParseNodeFree(pRight);
214378  }else{
214379
214380    assert( pLeft->eType==FTS5_STRING
214381        || pLeft->eType==FTS5_TERM
214382        || pLeft->eType==FTS5_EOF
214383        || pLeft->eType==FTS5_AND
214384    );
214385    assert( pRight->eType==FTS5_STRING
214386        || pRight->eType==FTS5_TERM
214387        || pRight->eType==FTS5_EOF
214388    );
214389
214390    if( pLeft->eType==FTS5_AND ){
214391      pPrev = pLeft->apChild[pLeft->nChild-1];
214392    }else{
214393      pPrev = pLeft;
214394    }
214395    assert( pPrev->eType==FTS5_STRING
214396        || pPrev->eType==FTS5_TERM
214397        || pPrev->eType==FTS5_EOF
214398        );
214399
214400    if( pRight->eType==FTS5_EOF ){
214401      assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] );
214402      sqlite3Fts5ParseNodeFree(pRight);
214403      pRet = pLeft;
214404      pParse->nPhrase--;
214405    }
214406    else if( pPrev->eType==FTS5_EOF ){
214407      Fts5ExprPhrase **ap;
214408
214409      if( pPrev==pLeft ){
214410        pRet = pRight;
214411      }else{
214412        pLeft->apChild[pLeft->nChild-1] = pRight;
214413        pRet = pLeft;
214414      }
214415
214416      ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase];
214417      assert( ap[0]==pPrev->pNear->apPhrase[0] );
214418      memmove(ap, &ap[1], sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase);
214419      pParse->nPhrase--;
214420
214421      sqlite3Fts5ParseNodeFree(pPrev);
214422    }
214423    else{
214424      pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND, pLeft, pRight, 0);
214425    }
214426  }
214427
214428  return pRet;
214429}
214430
214431static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){
214432  sqlite3_int64 nByte = 0;
214433  Fts5ExprTerm *p;
214434  char *zQuoted;
214435
214436  /* Determine the maximum amount of space required. */
214437  for(p=pTerm; p; p=p->pSynonym){
214438    nByte += (int)strlen(pTerm->zTerm) * 2 + 3 + 2;
214439  }
214440  zQuoted = sqlite3_malloc64(nByte);
214441
214442  if( zQuoted ){
214443    int i = 0;
214444    for(p=pTerm; p; p=p->pSynonym){
214445      char *zIn = p->zTerm;
214446      zQuoted[i++] = '"';
214447      while( *zIn ){
214448        if( *zIn=='"' ) zQuoted[i++] = '"';
214449        zQuoted[i++] = *zIn++;
214450      }
214451      zQuoted[i++] = '"';
214452      if( p->pSynonym ) zQuoted[i++] = '|';
214453    }
214454    if( pTerm->bPrefix ){
214455      zQuoted[i++] = ' ';
214456      zQuoted[i++] = '*';
214457    }
214458    zQuoted[i++] = '\0';
214459  }
214460  return zQuoted;
214461}
214462
214463static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){
214464  char *zNew;
214465  va_list ap;
214466  va_start(ap, zFmt);
214467  zNew = sqlite3_vmprintf(zFmt, ap);
214468  va_end(ap);
214469  if( zApp && zNew ){
214470    char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew);
214471    sqlite3_free(zNew);
214472    zNew = zNew2;
214473  }
214474  sqlite3_free(zApp);
214475  return zNew;
214476}
214477
214478/*
214479** Compose a tcl-readable representation of expression pExpr. Return a
214480** pointer to a buffer containing that representation. It is the
214481** responsibility of the caller to at some point free the buffer using
214482** sqlite3_free().
214483*/
214484static char *fts5ExprPrintTcl(
214485  Fts5Config *pConfig,
214486  const char *zNearsetCmd,
214487  Fts5ExprNode *pExpr
214488){
214489  char *zRet = 0;
214490  if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
214491    Fts5ExprNearset *pNear = pExpr->pNear;
214492    int i;
214493    int iTerm;
214494
214495    zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd);
214496    if( zRet==0 ) return 0;
214497    if( pNear->pColset ){
214498      int *aiCol = pNear->pColset->aiCol;
214499      int nCol = pNear->pColset->nCol;
214500      if( nCol==1 ){
214501        zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]);
214502      }else{
214503        zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]);
214504        for(i=1; i<pNear->pColset->nCol; i++){
214505          zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]);
214506        }
214507        zRet = fts5PrintfAppend(zRet, "} ");
214508      }
214509      if( zRet==0 ) return 0;
214510    }
214511
214512    if( pNear->nPhrase>1 ){
214513      zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear);
214514      if( zRet==0 ) return 0;
214515    }
214516
214517    zRet = fts5PrintfAppend(zRet, "--");
214518    if( zRet==0 ) return 0;
214519
214520    for(i=0; i<pNear->nPhrase; i++){
214521      Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
214522
214523      zRet = fts5PrintfAppend(zRet, " {");
214524      for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){
214525        char *zTerm = pPhrase->aTerm[iTerm].zTerm;
214526        zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" ", zTerm);
214527        if( pPhrase->aTerm[iTerm].bPrefix ){
214528          zRet = fts5PrintfAppend(zRet, "*");
214529        }
214530      }
214531
214532      if( zRet ) zRet = fts5PrintfAppend(zRet, "}");
214533      if( zRet==0 ) return 0;
214534    }
214535
214536  }else{
214537    char const *zOp = 0;
214538    int i;
214539    switch( pExpr->eType ){
214540      case FTS5_AND: zOp = "AND"; break;
214541      case FTS5_NOT: zOp = "NOT"; break;
214542      default:
214543        assert( pExpr->eType==FTS5_OR );
214544        zOp = "OR";
214545        break;
214546    }
214547
214548    zRet = sqlite3_mprintf("%s", zOp);
214549    for(i=0; zRet && i<pExpr->nChild; i++){
214550      char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]);
214551      if( !z ){
214552        sqlite3_free(zRet);
214553        zRet = 0;
214554      }else{
214555        zRet = fts5PrintfAppend(zRet, " [%z]", z);
214556      }
214557    }
214558  }
214559
214560  return zRet;
214561}
214562
214563static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){
214564  char *zRet = 0;
214565  if( pExpr->eType==0 ){
214566    return sqlite3_mprintf("\"\"");
214567  }else
214568  if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
214569    Fts5ExprNearset *pNear = pExpr->pNear;
214570    int i;
214571    int iTerm;
214572
214573    if( pNear->pColset ){
214574      int iCol = pNear->pColset->aiCol[0];
214575      zRet = fts5PrintfAppend(zRet, "%s : ", pConfig->azCol[iCol]);
214576      if( zRet==0 ) return 0;
214577    }
214578
214579    if( pNear->nPhrase>1 ){
214580      zRet = fts5PrintfAppend(zRet, "NEAR(");
214581      if( zRet==0 ) return 0;
214582    }
214583
214584    for(i=0; i<pNear->nPhrase; i++){
214585      Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
214586      if( i!=0 ){
214587        zRet = fts5PrintfAppend(zRet, " ");
214588        if( zRet==0 ) return 0;
214589      }
214590      for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){
214591        char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]);
214592        if( zTerm ){
214593          zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm);
214594          sqlite3_free(zTerm);
214595        }
214596        if( zTerm==0 || zRet==0 ){
214597          sqlite3_free(zRet);
214598          return 0;
214599        }
214600      }
214601    }
214602
214603    if( pNear->nPhrase>1 ){
214604      zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear);
214605      if( zRet==0 ) return 0;
214606    }
214607
214608  }else{
214609    char const *zOp = 0;
214610    int i;
214611
214612    switch( pExpr->eType ){
214613      case FTS5_AND: zOp = " AND "; break;
214614      case FTS5_NOT: zOp = " NOT "; break;
214615      default:
214616        assert( pExpr->eType==FTS5_OR );
214617        zOp = " OR ";
214618        break;
214619    }
214620
214621    for(i=0; i<pExpr->nChild; i++){
214622      char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]);
214623      if( z==0 ){
214624        sqlite3_free(zRet);
214625        zRet = 0;
214626      }else{
214627        int e = pExpr->apChild[i]->eType;
214628        int b = (e!=FTS5_STRING && e!=FTS5_TERM && e!=FTS5_EOF);
214629        zRet = fts5PrintfAppend(zRet, "%s%s%z%s",
214630            (i==0 ? "" : zOp),
214631            (b?"(":""), z, (b?")":"")
214632        );
214633      }
214634      if( zRet==0 ) break;
214635    }
214636  }
214637
214638  return zRet;
214639}
214640
214641/*
214642** The implementation of user-defined scalar functions fts5_expr() (bTcl==0)
214643** and fts5_expr_tcl() (bTcl!=0).
214644*/
214645static void fts5ExprFunction(
214646  sqlite3_context *pCtx,          /* Function call context */
214647  int nArg,                       /* Number of args */
214648  sqlite3_value **apVal,          /* Function arguments */
214649  int bTcl
214650){
214651  Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
214652  sqlite3 *db = sqlite3_context_db_handle(pCtx);
214653  const char *zExpr = 0;
214654  char *zErr = 0;
214655  Fts5Expr *pExpr = 0;
214656  int rc;
214657  int i;
214658
214659  const char **azConfig;          /* Array of arguments for Fts5Config */
214660  const char *zNearsetCmd = "nearset";
214661  int nConfig;                    /* Size of azConfig[] */
214662  Fts5Config *pConfig = 0;
214663  int iArg = 1;
214664
214665  if( nArg<1 ){
214666    zErr = sqlite3_mprintf("wrong number of arguments to function %s",
214667        bTcl ? "fts5_expr_tcl" : "fts5_expr"
214668    );
214669    sqlite3_result_error(pCtx, zErr, -1);
214670    sqlite3_free(zErr);
214671    return;
214672  }
214673
214674  if( bTcl && nArg>1 ){
214675    zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]);
214676    iArg = 2;
214677  }
214678
214679  nConfig = 3 + (nArg-iArg);
214680  azConfig = (const char**)sqlite3_malloc64(sizeof(char*) * nConfig);
214681  if( azConfig==0 ){
214682    sqlite3_result_error_nomem(pCtx);
214683    return;
214684  }
214685  azConfig[0] = 0;
214686  azConfig[1] = "main";
214687  azConfig[2] = "tbl";
214688  for(i=3; iArg<nArg; iArg++){
214689    const char *z = (const char*)sqlite3_value_text(apVal[iArg]);
214690    azConfig[i++] = (z ? z : "");
214691  }
214692
214693  zExpr = (const char*)sqlite3_value_text(apVal[0]);
214694  if( zExpr==0 ) zExpr = "";
214695
214696  rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr);
214697  if( rc==SQLITE_OK ){
214698    rc = sqlite3Fts5ExprNew(pConfig, pConfig->nCol, zExpr, &pExpr, &zErr);
214699  }
214700  if( rc==SQLITE_OK ){
214701    char *zText;
214702    if( pExpr->pRoot->xNext==0 ){
214703      zText = sqlite3_mprintf("");
214704    }else if( bTcl ){
214705      zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
214706    }else{
214707      zText = fts5ExprPrint(pConfig, pExpr->pRoot);
214708    }
214709    if( zText==0 ){
214710      rc = SQLITE_NOMEM;
214711    }else{
214712      sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT);
214713      sqlite3_free(zText);
214714    }
214715  }
214716
214717  if( rc!=SQLITE_OK ){
214718    if( zErr ){
214719      sqlite3_result_error(pCtx, zErr, -1);
214720      sqlite3_free(zErr);
214721    }else{
214722      sqlite3_result_error_code(pCtx, rc);
214723    }
214724  }
214725  sqlite3_free((void *)azConfig);
214726  sqlite3Fts5ConfigFree(pConfig);
214727  sqlite3Fts5ExprFree(pExpr);
214728}
214729
214730static void fts5ExprFunctionHr(
214731  sqlite3_context *pCtx,          /* Function call context */
214732  int nArg,                       /* Number of args */
214733  sqlite3_value **apVal           /* Function arguments */
214734){
214735  fts5ExprFunction(pCtx, nArg, apVal, 0);
214736}
214737static void fts5ExprFunctionTcl(
214738  sqlite3_context *pCtx,          /* Function call context */
214739  int nArg,                       /* Number of args */
214740  sqlite3_value **apVal           /* Function arguments */
214741){
214742  fts5ExprFunction(pCtx, nArg, apVal, 1);
214743}
214744
214745/*
214746** The implementation of an SQLite user-defined-function that accepts a
214747** single integer as an argument. If the integer is an alpha-numeric
214748** unicode code point, 1 is returned. Otherwise 0.
214749*/
214750static void fts5ExprIsAlnum(
214751  sqlite3_context *pCtx,          /* Function call context */
214752  int nArg,                       /* Number of args */
214753  sqlite3_value **apVal           /* Function arguments */
214754){
214755  int iCode;
214756  u8 aArr[32];
214757  if( nArg!=1 ){
214758    sqlite3_result_error(pCtx,
214759        "wrong number of arguments to function fts5_isalnum", -1
214760    );
214761    return;
214762  }
214763  memset(aArr, 0, sizeof(aArr));
214764  sqlite3Fts5UnicodeCatParse("L*", aArr);
214765  sqlite3Fts5UnicodeCatParse("N*", aArr);
214766  sqlite3Fts5UnicodeCatParse("Co", aArr);
214767  iCode = sqlite3_value_int(apVal[0]);
214768  sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory((u32)iCode)]);
214769}
214770
214771static void fts5ExprFold(
214772  sqlite3_context *pCtx,          /* Function call context */
214773  int nArg,                       /* Number of args */
214774  sqlite3_value **apVal           /* Function arguments */
214775){
214776  if( nArg!=1 && nArg!=2 ){
214777    sqlite3_result_error(pCtx,
214778        "wrong number of arguments to function fts5_fold", -1
214779    );
214780  }else{
214781    int iCode;
214782    int bRemoveDiacritics = 0;
214783    iCode = sqlite3_value_int(apVal[0]);
214784    if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]);
214785    sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics));
214786  }
214787}
214788
214789/*
214790** This is called during initialization to register the fts5_expr() scalar
214791** UDF with the SQLite handle passed as the only argument.
214792*/
214793static int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){
214794  struct Fts5ExprFunc {
214795    const char *z;
214796    void (*x)(sqlite3_context*,int,sqlite3_value**);
214797  } aFunc[] = {
214798    { "fts5_expr",     fts5ExprFunctionHr },
214799    { "fts5_expr_tcl", fts5ExprFunctionTcl },
214800    { "fts5_isalnum",  fts5ExprIsAlnum },
214801    { "fts5_fold",     fts5ExprFold },
214802  };
214803  int i;
214804  int rc = SQLITE_OK;
214805  void *pCtx = (void*)pGlobal;
214806
214807  for(i=0; rc==SQLITE_OK && i<ArraySize(aFunc); i++){
214808    struct Fts5ExprFunc *p = &aFunc[i];
214809    rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0);
214810  }
214811
214812  /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and
214813  ** sqlite3Fts5ParserFallback() are unused */
214814#ifndef NDEBUG
214815  (void)sqlite3Fts5ParserTrace;
214816#endif
214817  (void)sqlite3Fts5ParserFallback;
214818
214819  return rc;
214820}
214821
214822/*
214823** Return the number of phrases in expression pExpr.
214824*/
214825static int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){
214826  return (pExpr ? pExpr->nPhrase : 0);
214827}
214828
214829/*
214830** Return the number of terms in the iPhrase'th phrase in pExpr.
214831*/
214832static int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){
214833  if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0;
214834  return pExpr->apExprPhrase[iPhrase]->nTerm;
214835}
214836
214837/*
214838** This function is used to access the current position list for phrase
214839** iPhrase.
214840*/
214841static int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){
214842  int nRet;
214843  Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
214844  Fts5ExprNode *pNode = pPhrase->pNode;
214845  if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){
214846    *pa = pPhrase->poslist.p;
214847    nRet = pPhrase->poslist.n;
214848  }else{
214849    *pa = 0;
214850    nRet = 0;
214851  }
214852  return nRet;
214853}
214854
214855struct Fts5PoslistPopulator {
214856  Fts5PoslistWriter writer;
214857  int bOk;                        /* True if ok to populate */
214858  int bMiss;
214859};
214860
214861static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){
214862  Fts5PoslistPopulator *pRet;
214863  pRet = sqlite3_malloc64(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
214864  if( pRet ){
214865    int i;
214866    memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
214867    for(i=0; i<pExpr->nPhrase; i++){
214868      Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist;
214869      Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
214870      assert( pExpr->apExprPhrase[i]->nTerm==1 );
214871      if( bLive &&
214872          (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof)
214873      ){
214874        pRet[i].bMiss = 1;
214875      }else{
214876        pBuf->n = 0;
214877      }
214878    }
214879  }
214880  return pRet;
214881}
214882
214883struct Fts5ExprCtx {
214884  Fts5Expr *pExpr;
214885  Fts5PoslistPopulator *aPopulator;
214886  i64 iOff;
214887};
214888typedef struct Fts5ExprCtx Fts5ExprCtx;
214889
214890/*
214891** TODO: Make this more efficient!
214892*/
214893static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){
214894  int i;
214895  for(i=0; i<pColset->nCol; i++){
214896    if( pColset->aiCol[i]==iCol ) return 1;
214897  }
214898  return 0;
214899}
214900
214901static int fts5ExprPopulatePoslistsCb(
214902  void *pCtx,                /* Copy of 2nd argument to xTokenize() */
214903  int tflags,                /* Mask of FTS5_TOKEN_* flags */
214904  const char *pToken,        /* Pointer to buffer containing token */
214905  int nToken,                /* Size of token in bytes */
214906  int iUnused1,              /* Byte offset of token within input text */
214907  int iUnused2               /* Byte offset of end of token within input text */
214908){
214909  Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
214910  Fts5Expr *pExpr = p->pExpr;
214911  int i;
214912
214913  UNUSED_PARAM2(iUnused1, iUnused2);
214914
214915  if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
214916  if( (tflags & FTS5_TOKEN_COLOCATED)==0 ) p->iOff++;
214917  for(i=0; i<pExpr->nPhrase; i++){
214918    Fts5ExprTerm *pTerm;
214919    if( p->aPopulator[i].bOk==0 ) continue;
214920    for(pTerm=&pExpr->apExprPhrase[i]->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
214921      int nTerm = (int)strlen(pTerm->zTerm);
214922      if( (nTerm==nToken || (nTerm<nToken && pTerm->bPrefix))
214923       && memcmp(pTerm->zTerm, pToken, nTerm)==0
214924      ){
214925        int rc = sqlite3Fts5PoslistWriterAppend(
214926            &pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff
214927        );
214928        if( rc ) return rc;
214929        break;
214930      }
214931    }
214932  }
214933  return SQLITE_OK;
214934}
214935
214936static int sqlite3Fts5ExprPopulatePoslists(
214937  Fts5Config *pConfig,
214938  Fts5Expr *pExpr,
214939  Fts5PoslistPopulator *aPopulator,
214940  int iCol,
214941  const char *z, int n
214942){
214943  int i;
214944  Fts5ExprCtx sCtx;
214945  sCtx.pExpr = pExpr;
214946  sCtx.aPopulator = aPopulator;
214947  sCtx.iOff = (((i64)iCol) << 32) - 1;
214948
214949  for(i=0; i<pExpr->nPhrase; i++){
214950    Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
214951    Fts5Colset *pColset = pNode->pNear->pColset;
214952    if( (pColset && 0==fts5ExprColsetTest(pColset, iCol))
214953     || aPopulator[i].bMiss
214954    ){
214955      aPopulator[i].bOk = 0;
214956    }else{
214957      aPopulator[i].bOk = 1;
214958    }
214959  }
214960
214961  return sqlite3Fts5Tokenize(pConfig,
214962      FTS5_TOKENIZE_DOCUMENT, z, n, (void*)&sCtx, fts5ExprPopulatePoslistsCb
214963  );
214964}
214965
214966static void fts5ExprClearPoslists(Fts5ExprNode *pNode){
214967  if( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING ){
214968    pNode->pNear->apPhrase[0]->poslist.n = 0;
214969  }else{
214970    int i;
214971    for(i=0; i<pNode->nChild; i++){
214972      fts5ExprClearPoslists(pNode->apChild[i]);
214973    }
214974  }
214975}
214976
214977static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){
214978  pNode->iRowid = iRowid;
214979  pNode->bEof = 0;
214980  switch( pNode->eType ){
214981    case FTS5_TERM:
214982    case FTS5_STRING:
214983      return (pNode->pNear->apPhrase[0]->poslist.n>0);
214984
214985    case FTS5_AND: {
214986      int i;
214987      for(i=0; i<pNode->nChild; i++){
214988        if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid)==0 ){
214989          fts5ExprClearPoslists(pNode);
214990          return 0;
214991        }
214992      }
214993      break;
214994    }
214995
214996    case FTS5_OR: {
214997      int i;
214998      int bRet = 0;
214999      for(i=0; i<pNode->nChild; i++){
215000        if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid) ){
215001          bRet = 1;
215002        }
215003      }
215004      return bRet;
215005    }
215006
215007    default: {
215008      assert( pNode->eType==FTS5_NOT );
215009      if( 0==fts5ExprCheckPoslists(pNode->apChild[0], iRowid)
215010          || 0!=fts5ExprCheckPoslists(pNode->apChild[1], iRowid)
215011        ){
215012        fts5ExprClearPoslists(pNode);
215013        return 0;
215014      }
215015      break;
215016    }
215017  }
215018  return 1;
215019}
215020
215021static void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){
215022  fts5ExprCheckPoslists(pExpr->pRoot, iRowid);
215023}
215024
215025/*
215026** This function is only called for detail=columns tables.
215027*/
215028static int sqlite3Fts5ExprPhraseCollist(
215029  Fts5Expr *pExpr,
215030  int iPhrase,
215031  const u8 **ppCollist,
215032  int *pnCollist
215033){
215034  Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
215035  Fts5ExprNode *pNode = pPhrase->pNode;
215036  int rc = SQLITE_OK;
215037
215038  assert( iPhrase>=0 && iPhrase<pExpr->nPhrase );
215039  assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
215040
215041  if( pNode->bEof==0
215042   && pNode->iRowid==pExpr->pRoot->iRowid
215043   && pPhrase->poslist.n>0
215044  ){
215045    Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
215046    if( pTerm->pSynonym ){
215047      Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1];
215048      rc = fts5ExprSynonymList(
215049          pTerm, pNode->iRowid, pBuf, (u8**)ppCollist, pnCollist
215050      );
215051    }else{
215052      *ppCollist = pPhrase->aTerm[0].pIter->pData;
215053      *pnCollist = pPhrase->aTerm[0].pIter->nData;
215054    }
215055  }else{
215056    *ppCollist = 0;
215057    *pnCollist = 0;
215058  }
215059
215060  return rc;
215061}
215062
215063/*
215064** 2014 August 11
215065**
215066** The author disclaims copyright to this source code.  In place of
215067** a legal notice, here is a blessing:
215068**
215069**    May you do good and not evil.
215070**    May you find forgiveness for yourself and forgive others.
215071**    May you share freely, never taking more than you give.
215072**
215073******************************************************************************
215074**
215075*/
215076
215077
215078
215079/* #include "fts5Int.h" */
215080
215081typedef struct Fts5HashEntry Fts5HashEntry;
215082
215083/*
215084** This file contains the implementation of an in-memory hash table used
215085** to accumuluate "term -> doclist" content before it is flused to a level-0
215086** segment.
215087*/
215088
215089
215090struct Fts5Hash {
215091  int eDetail;                    /* Copy of Fts5Config.eDetail */
215092  int *pnByte;                    /* Pointer to bytes counter */
215093  int nEntry;                     /* Number of entries currently in hash */
215094  int nSlot;                      /* Size of aSlot[] array */
215095  Fts5HashEntry *pScan;           /* Current ordered scan item */
215096  Fts5HashEntry **aSlot;          /* Array of hash slots */
215097};
215098
215099/*
215100** Each entry in the hash table is represented by an object of the
215101** following type. Each object, its key (a nul-terminated string) and
215102** its current data are stored in a single memory allocation. The
215103** key immediately follows the object in memory. The position list
215104** data immediately follows the key data in memory.
215105**
215106** The data that follows the key is in a similar, but not identical format
215107** to the doclist data stored in the database. It is:
215108**
215109**   * Rowid, as a varint
215110**   * Position list, without 0x00 terminator.
215111**   * Size of previous position list and rowid, as a 4 byte
215112**     big-endian integer.
215113**
215114** iRowidOff:
215115**   Offset of last rowid written to data area. Relative to first byte of
215116**   structure.
215117**
215118** nData:
215119**   Bytes of data written since iRowidOff.
215120*/
215121struct Fts5HashEntry {
215122  Fts5HashEntry *pHashNext;       /* Next hash entry with same hash-key */
215123  Fts5HashEntry *pScanNext;       /* Next entry in sorted order */
215124
215125  int nAlloc;                     /* Total size of allocation */
215126  int iSzPoslist;                 /* Offset of space for 4-byte poslist size */
215127  int nData;                      /* Total bytes of data (incl. structure) */
215128  int nKey;                       /* Length of key in bytes */
215129  u8 bDel;                        /* Set delete-flag @ iSzPoslist */
215130  u8 bContent;                    /* Set content-flag (detail=none mode) */
215131  i16 iCol;                       /* Column of last value written */
215132  int iPos;                       /* Position of last value written */
215133  i64 iRowid;                     /* Rowid of last value written */
215134};
215135
215136/*
215137** Eqivalent to:
215138**
215139**   char *fts5EntryKey(Fts5HashEntry *pEntry){ return zKey; }
215140*/
215141#define fts5EntryKey(p) ( ((char *)(&(p)[1])) )
215142
215143
215144/*
215145** Allocate a new hash table.
215146*/
215147static int sqlite3Fts5HashNew(Fts5Config *pConfig, Fts5Hash **ppNew, int *pnByte){
215148  int rc = SQLITE_OK;
215149  Fts5Hash *pNew;
215150
215151  *ppNew = pNew = (Fts5Hash*)sqlite3_malloc(sizeof(Fts5Hash));
215152  if( pNew==0 ){
215153    rc = SQLITE_NOMEM;
215154  }else{
215155    sqlite3_int64 nByte;
215156    memset(pNew, 0, sizeof(Fts5Hash));
215157    pNew->pnByte = pnByte;
215158    pNew->eDetail = pConfig->eDetail;
215159
215160    pNew->nSlot = 1024;
215161    nByte = sizeof(Fts5HashEntry*) * pNew->nSlot;
215162    pNew->aSlot = (Fts5HashEntry**)sqlite3_malloc64(nByte);
215163    if( pNew->aSlot==0 ){
215164      sqlite3_free(pNew);
215165      *ppNew = 0;
215166      rc = SQLITE_NOMEM;
215167    }else{
215168      memset(pNew->aSlot, 0, (size_t)nByte);
215169    }
215170  }
215171  return rc;
215172}
215173
215174/*
215175** Free a hash table object.
215176*/
215177static void sqlite3Fts5HashFree(Fts5Hash *pHash){
215178  if( pHash ){
215179    sqlite3Fts5HashClear(pHash);
215180    sqlite3_free(pHash->aSlot);
215181    sqlite3_free(pHash);
215182  }
215183}
215184
215185/*
215186** Empty (but do not delete) a hash table.
215187*/
215188static void sqlite3Fts5HashClear(Fts5Hash *pHash){
215189  int i;
215190  for(i=0; i<pHash->nSlot; i++){
215191    Fts5HashEntry *pNext;
215192    Fts5HashEntry *pSlot;
215193    for(pSlot=pHash->aSlot[i]; pSlot; pSlot=pNext){
215194      pNext = pSlot->pHashNext;
215195      sqlite3_free(pSlot);
215196    }
215197  }
215198  memset(pHash->aSlot, 0, pHash->nSlot * sizeof(Fts5HashEntry*));
215199  pHash->nEntry = 0;
215200}
215201
215202static unsigned int fts5HashKey(int nSlot, const u8 *p, int n){
215203  int i;
215204  unsigned int h = 13;
215205  for(i=n-1; i>=0; i--){
215206    h = (h << 3) ^ h ^ p[i];
215207  }
215208  return (h % nSlot);
215209}
215210
215211static unsigned int fts5HashKey2(int nSlot, u8 b, const u8 *p, int n){
215212  int i;
215213  unsigned int h = 13;
215214  for(i=n-1; i>=0; i--){
215215    h = (h << 3) ^ h ^ p[i];
215216  }
215217  h = (h << 3) ^ h ^ b;
215218  return (h % nSlot);
215219}
215220
215221/*
215222** Resize the hash table by doubling the number of slots.
215223*/
215224static int fts5HashResize(Fts5Hash *pHash){
215225  int nNew = pHash->nSlot*2;
215226  int i;
215227  Fts5HashEntry **apNew;
215228  Fts5HashEntry **apOld = pHash->aSlot;
215229
215230  apNew = (Fts5HashEntry**)sqlite3_malloc64(nNew*sizeof(Fts5HashEntry*));
215231  if( !apNew ) return SQLITE_NOMEM;
215232  memset(apNew, 0, nNew*sizeof(Fts5HashEntry*));
215233
215234  for(i=0; i<pHash->nSlot; i++){
215235    while( apOld[i] ){
215236      unsigned int iHash;
215237      Fts5HashEntry *p = apOld[i];
215238      apOld[i] = p->pHashNext;
215239      iHash = fts5HashKey(nNew, (u8*)fts5EntryKey(p),
215240                          (int)strlen(fts5EntryKey(p)));
215241      p->pHashNext = apNew[iHash];
215242      apNew[iHash] = p;
215243    }
215244  }
215245
215246  sqlite3_free(apOld);
215247  pHash->nSlot = nNew;
215248  pHash->aSlot = apNew;
215249  return SQLITE_OK;
215250}
215251
215252static int fts5HashAddPoslistSize(
215253  Fts5Hash *pHash,
215254  Fts5HashEntry *p,
215255  Fts5HashEntry *p2
215256){
215257  int nRet = 0;
215258  if( p->iSzPoslist ){
215259    u8 *pPtr = p2 ? (u8*)p2 : (u8*)p;
215260    int nData = p->nData;
215261    if( pHash->eDetail==FTS5_DETAIL_NONE ){
215262      assert( nData==p->iSzPoslist );
215263      if( p->bDel ){
215264        pPtr[nData++] = 0x00;
215265        if( p->bContent ){
215266          pPtr[nData++] = 0x00;
215267        }
215268      }
215269    }else{
215270      int nSz = (nData - p->iSzPoslist - 1);       /* Size in bytes */
215271      int nPos = nSz*2 + p->bDel;                     /* Value of nPos field */
215272
215273      assert( p->bDel==0 || p->bDel==1 );
215274      if( nPos<=127 ){
215275        pPtr[p->iSzPoslist] = (u8)nPos;
215276      }else{
215277        int nByte = sqlite3Fts5GetVarintLen((u32)nPos);
215278        memmove(&pPtr[p->iSzPoslist + nByte], &pPtr[p->iSzPoslist + 1], nSz);
215279        sqlite3Fts5PutVarint(&pPtr[p->iSzPoslist], nPos);
215280        nData += (nByte-1);
215281      }
215282    }
215283
215284    nRet = nData - p->nData;
215285    if( p2==0 ){
215286      p->iSzPoslist = 0;
215287      p->bDel = 0;
215288      p->bContent = 0;
215289      p->nData = nData;
215290    }
215291  }
215292  return nRet;
215293}
215294
215295/*
215296** Add an entry to the in-memory hash table. The key is the concatenation
215297** of bByte and (pToken/nToken). The value is (iRowid/iCol/iPos).
215298**
215299**     (bByte || pToken) -> (iRowid,iCol,iPos)
215300**
215301** Or, if iCol is negative, then the value is a delete marker.
215302*/
215303static int sqlite3Fts5HashWrite(
215304  Fts5Hash *pHash,
215305  i64 iRowid,                     /* Rowid for this entry */
215306  int iCol,                       /* Column token appears in (-ve -> delete) */
215307  int iPos,                       /* Position of token within column */
215308  char bByte,                     /* First byte of token */
215309  const char *pToken, int nToken  /* Token to add or remove to or from index */
215310){
215311  unsigned int iHash;
215312  Fts5HashEntry *p;
215313  u8 *pPtr;
215314  int nIncr = 0;                  /* Amount to increment (*pHash->pnByte) by */
215315  int bNew;                       /* If non-delete entry should be written */
215316
215317  bNew = (pHash->eDetail==FTS5_DETAIL_FULL);
215318
215319  /* Attempt to locate an existing hash entry */
215320  iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
215321  for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
215322    char *zKey = fts5EntryKey(p);
215323    if( zKey[0]==bByte
215324     && p->nKey==nToken
215325     && memcmp(&zKey[1], pToken, nToken)==0
215326    ){
215327      break;
215328    }
215329  }
215330
215331  /* If an existing hash entry cannot be found, create a new one. */
215332  if( p==0 ){
215333    /* Figure out how much space to allocate */
215334    char *zKey;
215335    sqlite3_int64 nByte = sizeof(Fts5HashEntry) + (nToken+1) + 1 + 64;
215336    if( nByte<128 ) nByte = 128;
215337
215338    /* Grow the Fts5Hash.aSlot[] array if necessary. */
215339    if( (pHash->nEntry*2)>=pHash->nSlot ){
215340      int rc = fts5HashResize(pHash);
215341      if( rc!=SQLITE_OK ) return rc;
215342      iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
215343    }
215344
215345    /* Allocate new Fts5HashEntry and add it to the hash table. */
215346    p = (Fts5HashEntry*)sqlite3_malloc64(nByte);
215347    if( !p ) return SQLITE_NOMEM;
215348    memset(p, 0, sizeof(Fts5HashEntry));
215349    p->nAlloc = (int)nByte;
215350    zKey = fts5EntryKey(p);
215351    zKey[0] = bByte;
215352    memcpy(&zKey[1], pToken, nToken);
215353    assert( iHash==fts5HashKey(pHash->nSlot, (u8*)zKey, nToken+1) );
215354    p->nKey = nToken;
215355    zKey[nToken+1] = '\0';
215356    p->nData = nToken+1 + 1 + sizeof(Fts5HashEntry);
215357    p->pHashNext = pHash->aSlot[iHash];
215358    pHash->aSlot[iHash] = p;
215359    pHash->nEntry++;
215360
215361    /* Add the first rowid field to the hash-entry */
215362    p->nData += sqlite3Fts5PutVarint(&((u8*)p)[p->nData], iRowid);
215363    p->iRowid = iRowid;
215364
215365    p->iSzPoslist = p->nData;
215366    if( pHash->eDetail!=FTS5_DETAIL_NONE ){
215367      p->nData += 1;
215368      p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1);
215369    }
215370
215371    nIncr += p->nData;
215372  }else{
215373
215374    /* Appending to an existing hash-entry. Check that there is enough
215375    ** space to append the largest possible new entry. Worst case scenario
215376    ** is:
215377    **
215378    **     + 9 bytes for a new rowid,
215379    **     + 4 byte reserved for the "poslist size" varint.
215380    **     + 1 byte for a "new column" byte,
215381    **     + 3 bytes for a new column number (16-bit max) as a varint,
215382    **     + 5 bytes for the new position offset (32-bit max).
215383    */
215384    if( (p->nAlloc - p->nData) < (9 + 4 + 1 + 3 + 5) ){
215385      sqlite3_int64 nNew = p->nAlloc * 2;
215386      Fts5HashEntry *pNew;
215387      Fts5HashEntry **pp;
215388      pNew = (Fts5HashEntry*)sqlite3_realloc64(p, nNew);
215389      if( pNew==0 ) return SQLITE_NOMEM;
215390      pNew->nAlloc = (int)nNew;
215391      for(pp=&pHash->aSlot[iHash]; *pp!=p; pp=&(*pp)->pHashNext);
215392      *pp = pNew;
215393      p = pNew;
215394    }
215395    nIncr -= p->nData;
215396  }
215397  assert( (p->nAlloc - p->nData) >= (9 + 4 + 1 + 3 + 5) );
215398
215399  pPtr = (u8*)p;
215400
215401  /* If this is a new rowid, append the 4-byte size field for the previous
215402  ** entry, and the new rowid for this entry.  */
215403  if( iRowid!=p->iRowid ){
215404    fts5HashAddPoslistSize(pHash, p, 0);
215405    p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iRowid - p->iRowid);
215406    p->iRowid = iRowid;
215407    bNew = 1;
215408    p->iSzPoslist = p->nData;
215409    if( pHash->eDetail!=FTS5_DETAIL_NONE ){
215410      p->nData += 1;
215411      p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1);
215412      p->iPos = 0;
215413    }
215414  }
215415
215416  if( iCol>=0 ){
215417    if( pHash->eDetail==FTS5_DETAIL_NONE ){
215418      p->bContent = 1;
215419    }else{
215420      /* Append a new column value, if necessary */
215421      assert( iCol>=p->iCol );
215422      if( iCol!=p->iCol ){
215423        if( pHash->eDetail==FTS5_DETAIL_FULL ){
215424          pPtr[p->nData++] = 0x01;
215425          p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iCol);
215426          p->iCol = (i16)iCol;
215427          p->iPos = 0;
215428        }else{
215429          bNew = 1;
215430          p->iCol = (i16)(iPos = iCol);
215431        }
215432      }
215433
215434      /* Append the new position offset, if necessary */
215435      if( bNew ){
215436        p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iPos - p->iPos + 2);
215437        p->iPos = iPos;
215438      }
215439    }
215440  }else{
215441    /* This is a delete. Set the delete flag. */
215442    p->bDel = 1;
215443  }
215444
215445  nIncr += p->nData;
215446  *pHash->pnByte += nIncr;
215447  return SQLITE_OK;
215448}
215449
215450
215451/*
215452** Arguments pLeft and pRight point to linked-lists of hash-entry objects,
215453** each sorted in key order. This function merges the two lists into a
215454** single list and returns a pointer to its first element.
215455*/
215456static Fts5HashEntry *fts5HashEntryMerge(
215457  Fts5HashEntry *pLeft,
215458  Fts5HashEntry *pRight
215459){
215460  Fts5HashEntry *p1 = pLeft;
215461  Fts5HashEntry *p2 = pRight;
215462  Fts5HashEntry *pRet = 0;
215463  Fts5HashEntry **ppOut = &pRet;
215464
215465  while( p1 || p2 ){
215466    if( p1==0 ){
215467      *ppOut = p2;
215468      p2 = 0;
215469    }else if( p2==0 ){
215470      *ppOut = p1;
215471      p1 = 0;
215472    }else{
215473      int i = 0;
215474      char *zKey1 = fts5EntryKey(p1);
215475      char *zKey2 = fts5EntryKey(p2);
215476      while( zKey1[i]==zKey2[i] ) i++;
215477
215478      if( ((u8)zKey1[i])>((u8)zKey2[i]) ){
215479        /* p2 is smaller */
215480        *ppOut = p2;
215481        ppOut = &p2->pScanNext;
215482        p2 = p2->pScanNext;
215483      }else{
215484        /* p1 is smaller */
215485        *ppOut = p1;
215486        ppOut = &p1->pScanNext;
215487        p1 = p1->pScanNext;
215488      }
215489      *ppOut = 0;
215490    }
215491  }
215492
215493  return pRet;
215494}
215495
215496/*
215497** Extract all tokens from hash table iHash and link them into a list
215498** in sorted order. The hash table is cleared before returning. It is
215499** the responsibility of the caller to free the elements of the returned
215500** list.
215501*/
215502static int fts5HashEntrySort(
215503  Fts5Hash *pHash,
215504  const char *pTerm, int nTerm,   /* Query prefix, if any */
215505  Fts5HashEntry **ppSorted
215506){
215507  const int nMergeSlot = 32;
215508  Fts5HashEntry **ap;
215509  Fts5HashEntry *pList;
215510  int iSlot;
215511  int i;
215512
215513  *ppSorted = 0;
215514  ap = sqlite3_malloc64(sizeof(Fts5HashEntry*) * nMergeSlot);
215515  if( !ap ) return SQLITE_NOMEM;
215516  memset(ap, 0, sizeof(Fts5HashEntry*) * nMergeSlot);
215517
215518  for(iSlot=0; iSlot<pHash->nSlot; iSlot++){
215519    Fts5HashEntry *pIter;
215520    for(pIter=pHash->aSlot[iSlot]; pIter; pIter=pIter->pHashNext){
215521      if( pTerm==0
215522       || (pIter->nKey+1>=nTerm && 0==memcmp(fts5EntryKey(pIter), pTerm, nTerm))
215523      ){
215524        Fts5HashEntry *pEntry = pIter;
215525        pEntry->pScanNext = 0;
215526        for(i=0; ap[i]; i++){
215527          pEntry = fts5HashEntryMerge(pEntry, ap[i]);
215528          ap[i] = 0;
215529        }
215530        ap[i] = pEntry;
215531      }
215532    }
215533  }
215534
215535  pList = 0;
215536  for(i=0; i<nMergeSlot; i++){
215537    pList = fts5HashEntryMerge(pList, ap[i]);
215538  }
215539
215540  pHash->nEntry = 0;
215541  sqlite3_free(ap);
215542  *ppSorted = pList;
215543  return SQLITE_OK;
215544}
215545
215546/*
215547** Query the hash table for a doclist associated with term pTerm/nTerm.
215548*/
215549static int sqlite3Fts5HashQuery(
215550  Fts5Hash *pHash,                /* Hash table to query */
215551  int nPre,
215552  const char *pTerm, int nTerm,   /* Query term */
215553  void **ppOut,                   /* OUT: Pointer to new object */
215554  int *pnDoclist                  /* OUT: Size of doclist in bytes */
215555){
215556  unsigned int iHash = fts5HashKey(pHash->nSlot, (const u8*)pTerm, nTerm);
215557  char *zKey = 0;
215558  Fts5HashEntry *p;
215559
215560  for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
215561    zKey = fts5EntryKey(p);
215562    assert( p->nKey+1==(int)strlen(zKey) );
215563    if( nTerm==p->nKey+1 && memcmp(zKey, pTerm, nTerm)==0 ) break;
215564  }
215565
215566  if( p ){
215567    int nHashPre = sizeof(Fts5HashEntry) + nTerm + 1;
215568    int nList = p->nData - nHashPre;
215569    u8 *pRet = (u8*)(*ppOut = sqlite3_malloc64(nPre + nList + 10));
215570    if( pRet ){
215571      Fts5HashEntry *pFaux = (Fts5HashEntry*)&pRet[nPre-nHashPre];
215572      memcpy(&pRet[nPre], &((u8*)p)[nHashPre], nList);
215573      nList += fts5HashAddPoslistSize(pHash, p, pFaux);
215574      *pnDoclist = nList;
215575    }else{
215576      *pnDoclist = 0;
215577      return SQLITE_NOMEM;
215578    }
215579  }else{
215580    *ppOut = 0;
215581    *pnDoclist = 0;
215582  }
215583
215584  return SQLITE_OK;
215585}
215586
215587static int sqlite3Fts5HashScanInit(
215588  Fts5Hash *p,                    /* Hash table to query */
215589  const char *pTerm, int nTerm    /* Query prefix */
215590){
215591  return fts5HashEntrySort(p, pTerm, nTerm, &p->pScan);
215592}
215593
215594static void sqlite3Fts5HashScanNext(Fts5Hash *p){
215595  assert( !sqlite3Fts5HashScanEof(p) );
215596  p->pScan = p->pScan->pScanNext;
215597}
215598
215599static int sqlite3Fts5HashScanEof(Fts5Hash *p){
215600  return (p->pScan==0);
215601}
215602
215603static void sqlite3Fts5HashScanEntry(
215604  Fts5Hash *pHash,
215605  const char **pzTerm,            /* OUT: term (nul-terminated) */
215606  const u8 **ppDoclist,           /* OUT: pointer to doclist */
215607  int *pnDoclist                  /* OUT: size of doclist in bytes */
215608){
215609  Fts5HashEntry *p;
215610  if( (p = pHash->pScan) ){
215611    char *zKey = fts5EntryKey(p);
215612    int nTerm = (int)strlen(zKey);
215613    fts5HashAddPoslistSize(pHash, p, 0);
215614    *pzTerm = zKey;
215615    *ppDoclist = (const u8*)&zKey[nTerm+1];
215616    *pnDoclist = p->nData - (sizeof(Fts5HashEntry) + nTerm + 1);
215617  }else{
215618    *pzTerm = 0;
215619    *ppDoclist = 0;
215620    *pnDoclist = 0;
215621  }
215622}
215623
215624/*
215625** 2014 May 31
215626**
215627** The author disclaims copyright to this source code.  In place of
215628** a legal notice, here is a blessing:
215629**
215630**    May you do good and not evil.
215631**    May you find forgiveness for yourself and forgive others.
215632**    May you share freely, never taking more than you give.
215633**
215634******************************************************************************
215635**
215636** Low level access to the FTS index stored in the database file. The
215637** routines in this file file implement all read and write access to the
215638** %_data table. Other parts of the system access this functionality via
215639** the interface defined in fts5Int.h.
215640*/
215641
215642
215643/* #include "fts5Int.h" */
215644
215645/*
215646** Overview:
215647**
215648** The %_data table contains all the FTS indexes for an FTS5 virtual table.
215649** As well as the main term index, there may be up to 31 prefix indexes.
215650** The format is similar to FTS3/4, except that:
215651**
215652**   * all segment b-tree leaf data is stored in fixed size page records
215653**     (e.g. 1000 bytes). A single doclist may span multiple pages. Care is
215654**     taken to ensure it is possible to iterate in either direction through
215655**     the entries in a doclist, or to seek to a specific entry within a
215656**     doclist, without loading it into memory.
215657**
215658**   * large doclists that span many pages have associated "doclist index"
215659**     records that contain a copy of the first rowid on each page spanned by
215660**     the doclist. This is used to speed up seek operations, and merges of
215661**     large doclists with very small doclists.
215662**
215663**   * extra fields in the "structure record" record the state of ongoing
215664**     incremental merge operations.
215665**
215666*/
215667
215668
215669#define FTS5_OPT_WORK_UNIT  1000  /* Number of leaf pages per optimize step */
215670#define FTS5_WORK_UNIT      64    /* Number of leaf pages in unit of work */
215671
215672#define FTS5_MIN_DLIDX_SIZE 4     /* Add dlidx if this many empty pages */
215673
215674#define FTS5_MAIN_PREFIX '0'
215675
215676#if FTS5_MAX_PREFIX_INDEXES > 31
215677# error "FTS5_MAX_PREFIX_INDEXES is too large"
215678#endif
215679
215680/*
215681** Details:
215682**
215683** The %_data table managed by this module,
215684**
215685**     CREATE TABLE %_data(id INTEGER PRIMARY KEY, block BLOB);
215686**
215687** , contains the following 5 types of records. See the comments surrounding
215688** the FTS5_*_ROWID macros below for a description of how %_data rowids are
215689** assigned to each fo them.
215690**
215691** 1. Structure Records:
215692**
215693**   The set of segments that make up an index - the index structure - are
215694**   recorded in a single record within the %_data table. The record consists
215695**   of a single 32-bit configuration cookie value followed by a list of
215696**   SQLite varints. If the FTS table features more than one index (because
215697**   there are one or more prefix indexes), it is guaranteed that all share
215698**   the same cookie value.
215699**
215700**   Immediately following the configuration cookie, the record begins with
215701**   three varints:
215702**
215703**     + number of levels,
215704**     + total number of segments on all levels,
215705**     + value of write counter.
215706**
215707**   Then, for each level from 0 to nMax:
215708**
215709**     + number of input segments in ongoing merge.
215710**     + total number of segments in level.
215711**     + for each segment from oldest to newest:
215712**         + segment id (always > 0)
215713**         + first leaf page number (often 1, always greater than 0)
215714**         + final leaf page number
215715**
215716** 2. The Averages Record:
215717**
215718**   A single record within the %_data table. The data is a list of varints.
215719**   The first value is the number of rows in the index. Then, for each column
215720**   from left to right, the total number of tokens in the column for all
215721**   rows of the table.
215722**
215723** 3. Segment leaves:
215724**
215725**   TERM/DOCLIST FORMAT:
215726**
215727**     Most of each segment leaf is taken up by term/doclist data. The
215728**     general format of term/doclist, starting with the first term
215729**     on the leaf page, is:
215730**
215731**         varint : size of first term
215732**         blob:    first term data
215733**         doclist: first doclist
215734**         zero-or-more {
215735**           varint:  number of bytes in common with previous term
215736**           varint:  number of bytes of new term data (nNew)
215737**           blob:    nNew bytes of new term data
215738**           doclist: next doclist
215739**         }
215740**
215741**     doclist format:
215742**
215743**         varint:  first rowid
215744**         poslist: first poslist
215745**         zero-or-more {
215746**           varint:  rowid delta (always > 0)
215747**           poslist: next poslist
215748**         }
215749**
215750**     poslist format:
215751**
215752**         varint: size of poslist in bytes multiplied by 2, not including
215753**                 this field. Plus 1 if this entry carries the "delete" flag.
215754**         collist: collist for column 0
215755**         zero-or-more {
215756**           0x01 byte
215757**           varint: column number (I)
215758**           collist: collist for column I
215759**         }
215760**
215761**     collist format:
215762**
215763**         varint: first offset + 2
215764**         zero-or-more {
215765**           varint: offset delta + 2
215766**         }
215767**
215768**   PAGE FORMAT
215769**
215770**     Each leaf page begins with a 4-byte header containing 2 16-bit
215771**     unsigned integer fields in big-endian format. They are:
215772**
215773**       * The byte offset of the first rowid on the page, if it exists
215774**         and occurs before the first term (otherwise 0).
215775**
215776**       * The byte offset of the start of the page footer. If the page
215777**         footer is 0 bytes in size, then this field is the same as the
215778**         size of the leaf page in bytes.
215779**
215780**     The page footer consists of a single varint for each term located
215781**     on the page. Each varint is the byte offset of the current term
215782**     within the page, delta-compressed against the previous value. In
215783**     other words, the first varint in the footer is the byte offset of
215784**     the first term, the second is the byte offset of the second less that
215785**     of the first, and so on.
215786**
215787**     The term/doclist format described above is accurate if the entire
215788**     term/doclist data fits on a single leaf page. If this is not the case,
215789**     the format is changed in two ways:
215790**
215791**       + if the first rowid on a page occurs before the first term, it
215792**         is stored as a literal value:
215793**
215794**             varint:  first rowid
215795**
215796**       + the first term on each page is stored in the same way as the
215797**         very first term of the segment:
215798**
215799**             varint : size of first term
215800**             blob:    first term data
215801**
215802** 5. Segment doclist indexes:
215803**
215804**   Doclist indexes are themselves b-trees, however they usually consist of
215805**   a single leaf record only. The format of each doclist index leaf page
215806**   is:
215807**
215808**     * Flags byte. Bits are:
215809**         0x01: Clear if leaf is also the root page, otherwise set.
215810**
215811**     * Page number of fts index leaf page. As a varint.
215812**
215813**     * First rowid on page indicated by previous field. As a varint.
215814**
215815**     * A list of varints, one for each subsequent termless page. A
215816**       positive delta if the termless page contains at least one rowid,
215817**       or an 0x00 byte otherwise.
215818**
215819**   Internal doclist index nodes are:
215820**
215821**     * Flags byte. Bits are:
215822**         0x01: Clear for root page, otherwise set.
215823**
215824**     * Page number of first child page. As a varint.
215825**
215826**     * Copy of first rowid on page indicated by previous field. As a varint.
215827**
215828**     * A list of delta-encoded varints - the first rowid on each subsequent
215829**       child page.
215830**
215831*/
215832
215833/*
215834** Rowids for the averages and structure records in the %_data table.
215835*/
215836#define FTS5_AVERAGES_ROWID     1    /* Rowid used for the averages record */
215837#define FTS5_STRUCTURE_ROWID   10    /* The structure record */
215838
215839/*
215840** Macros determining the rowids used by segment leaves and dlidx leaves
215841** and nodes. All nodes and leaves are stored in the %_data table with large
215842** positive rowids.
215843**
215844** Each segment has a unique non-zero 16-bit id.
215845**
215846** The rowid for each segment leaf is found by passing the segment id and
215847** the leaf page number to the FTS5_SEGMENT_ROWID macro. Leaves are numbered
215848** sequentially starting from 1.
215849*/
215850#define FTS5_DATA_ID_B     16     /* Max seg id number 65535 */
215851#define FTS5_DATA_DLI_B     1     /* Doclist-index flag (1 bit) */
215852#define FTS5_DATA_HEIGHT_B  5     /* Max dlidx tree height of 32 */
215853#define FTS5_DATA_PAGE_B   31     /* Max page number of 2147483648 */
215854
215855#define fts5_dri(segid, dlidx, height, pgno) (                                 \
215856 ((i64)(segid)  << (FTS5_DATA_PAGE_B+FTS5_DATA_HEIGHT_B+FTS5_DATA_DLI_B)) +    \
215857 ((i64)(dlidx)  << (FTS5_DATA_PAGE_B + FTS5_DATA_HEIGHT_B)) +                  \
215858 ((i64)(height) << (FTS5_DATA_PAGE_B)) +                                       \
215859 ((i64)(pgno))                                                                 \
215860)
215861
215862#define FTS5_SEGMENT_ROWID(segid, pgno)       fts5_dri(segid, 0, 0, pgno)
215863#define FTS5_DLIDX_ROWID(segid, height, pgno) fts5_dri(segid, 1, height, pgno)
215864
215865#ifdef SQLITE_DEBUG
215866static int sqlite3Fts5Corrupt() { return SQLITE_CORRUPT_VTAB; }
215867#endif
215868
215869
215870/*
215871** Each time a blob is read from the %_data table, it is padded with this
215872** many zero bytes. This makes it easier to decode the various record formats
215873** without overreading if the records are corrupt.
215874*/
215875#define FTS5_DATA_ZERO_PADDING 8
215876#define FTS5_DATA_PADDING 20
215877
215878typedef struct Fts5Data Fts5Data;
215879typedef struct Fts5DlidxIter Fts5DlidxIter;
215880typedef struct Fts5DlidxLvl Fts5DlidxLvl;
215881typedef struct Fts5DlidxWriter Fts5DlidxWriter;
215882typedef struct Fts5Iter Fts5Iter;
215883typedef struct Fts5PageWriter Fts5PageWriter;
215884typedef struct Fts5SegIter Fts5SegIter;
215885typedef struct Fts5DoclistIter Fts5DoclistIter;
215886typedef struct Fts5SegWriter Fts5SegWriter;
215887typedef struct Fts5Structure Fts5Structure;
215888typedef struct Fts5StructureLevel Fts5StructureLevel;
215889typedef struct Fts5StructureSegment Fts5StructureSegment;
215890
215891struct Fts5Data {
215892  u8 *p;                          /* Pointer to buffer containing record */
215893  int nn;                         /* Size of record in bytes */
215894  int szLeaf;                     /* Size of leaf without page-index */
215895};
215896
215897/*
215898** One object per %_data table.
215899*/
215900struct Fts5Index {
215901  Fts5Config *pConfig;            /* Virtual table configuration */
215902  char *zDataTbl;                 /* Name of %_data table */
215903  int nWorkUnit;                  /* Leaf pages in a "unit" of work */
215904
215905  /*
215906  ** Variables related to the accumulation of tokens and doclists within the
215907  ** in-memory hash tables before they are flushed to disk.
215908  */
215909  Fts5Hash *pHash;                /* Hash table for in-memory data */
215910  int nPendingData;               /* Current bytes of pending data */
215911  i64 iWriteRowid;                /* Rowid for current doc being written */
215912  int bDelete;                    /* Current write is a delete */
215913
215914  /* Error state. */
215915  int rc;                         /* Current error code */
215916
215917  /* State used by the fts5DataXXX() functions. */
215918  sqlite3_blob *pReader;          /* RO incr-blob open on %_data table */
215919  sqlite3_stmt *pWriter;          /* "INSERT ... %_data VALUES(?,?)" */
215920  sqlite3_stmt *pDeleter;         /* "DELETE FROM %_data ... id>=? AND id<=?" */
215921  sqlite3_stmt *pIdxWriter;       /* "INSERT ... %_idx VALUES(?,?,?,?)" */
215922  sqlite3_stmt *pIdxDeleter;      /* "DELETE FROM %_idx WHERE segid=? */
215923  sqlite3_stmt *pIdxSelect;
215924  int nRead;                      /* Total number of blocks read */
215925
215926  sqlite3_stmt *pDataVersion;
215927  i64 iStructVersion;             /* data_version when pStruct read */
215928  Fts5Structure *pStruct;         /* Current db structure (or NULL) */
215929};
215930
215931struct Fts5DoclistIter {
215932  u8 *aEof;                       /* Pointer to 1 byte past end of doclist */
215933
215934  /* Output variables. aPoslist==0 at EOF */
215935  i64 iRowid;
215936  u8 *aPoslist;
215937  int nPoslist;
215938  int nSize;
215939};
215940
215941/*
215942** The contents of the "structure" record for each index are represented
215943** using an Fts5Structure record in memory. Which uses instances of the
215944** other Fts5StructureXXX types as components.
215945*/
215946struct Fts5StructureSegment {
215947  int iSegid;                     /* Segment id */
215948  int pgnoFirst;                  /* First leaf page number in segment */
215949  int pgnoLast;                   /* Last leaf page number in segment */
215950};
215951struct Fts5StructureLevel {
215952  int nMerge;                     /* Number of segments in incr-merge */
215953  int nSeg;                       /* Total number of segments on level */
215954  Fts5StructureSegment *aSeg;     /* Array of segments. aSeg[0] is oldest. */
215955};
215956struct Fts5Structure {
215957  int nRef;                       /* Object reference count */
215958  u64 nWriteCounter;              /* Total leaves written to level 0 */
215959  int nSegment;                   /* Total segments in this structure */
215960  int nLevel;                     /* Number of levels in this index */
215961  Fts5StructureLevel aLevel[1];   /* Array of nLevel level objects */
215962};
215963
215964/*
215965** An object of type Fts5SegWriter is used to write to segments.
215966*/
215967struct Fts5PageWriter {
215968  int pgno;                       /* Page number for this page */
215969  int iPrevPgidx;                 /* Previous value written into pgidx */
215970  Fts5Buffer buf;                 /* Buffer containing leaf data */
215971  Fts5Buffer pgidx;               /* Buffer containing page-index */
215972  Fts5Buffer term;                /* Buffer containing previous term on page */
215973};
215974struct Fts5DlidxWriter {
215975  int pgno;                       /* Page number for this page */
215976  int bPrevValid;                 /* True if iPrev is valid */
215977  i64 iPrev;                      /* Previous rowid value written to page */
215978  Fts5Buffer buf;                 /* Buffer containing page data */
215979};
215980struct Fts5SegWriter {
215981  int iSegid;                     /* Segid to write to */
215982  Fts5PageWriter writer;          /* PageWriter object */
215983  i64 iPrevRowid;                 /* Previous rowid written to current leaf */
215984  u8 bFirstRowidInDoclist;        /* True if next rowid is first in doclist */
215985  u8 bFirstRowidInPage;           /* True if next rowid is first in page */
215986  /* TODO1: Can use (writer.pgidx.n==0) instead of bFirstTermInPage */
215987  u8 bFirstTermInPage;            /* True if next term will be first in leaf */
215988  int nLeafWritten;               /* Number of leaf pages written */
215989  int nEmpty;                     /* Number of contiguous term-less nodes */
215990
215991  int nDlidx;                     /* Allocated size of aDlidx[] array */
215992  Fts5DlidxWriter *aDlidx;        /* Array of Fts5DlidxWriter objects */
215993
215994  /* Values to insert into the %_idx table */
215995  Fts5Buffer btterm;              /* Next term to insert into %_idx table */
215996  int iBtPage;                    /* Page number corresponding to btterm */
215997};
215998
215999typedef struct Fts5CResult Fts5CResult;
216000struct Fts5CResult {
216001  u16 iFirst;                     /* aSeg[] index of firstest iterator */
216002  u8 bTermEq;                     /* True if the terms are equal */
216003};
216004
216005/*
216006** Object for iterating through a single segment, visiting each term/rowid
216007** pair in the segment.
216008**
216009** pSeg:
216010**   The segment to iterate through.
216011**
216012** iLeafPgno:
216013**   Current leaf page number within segment.
216014**
216015** iLeafOffset:
216016**   Byte offset within the current leaf that is the first byte of the
216017**   position list data (one byte passed the position-list size field).
216018**   rowid field of the current entry. Usually this is the size field of the
216019**   position list data. The exception is if the rowid for the current entry
216020**   is the last thing on the leaf page.
216021**
216022** pLeaf:
216023**   Buffer containing current leaf page data. Set to NULL at EOF.
216024**
216025** iTermLeafPgno, iTermLeafOffset:
216026**   Leaf page number containing the last term read from the segment. And
216027**   the offset immediately following the term data.
216028**
216029** flags:
216030**   Mask of FTS5_SEGITER_XXX values. Interpreted as follows:
216031**
216032**   FTS5_SEGITER_ONETERM:
216033**     If set, set the iterator to point to EOF after the current doclist
216034**     has been exhausted. Do not proceed to the next term in the segment.
216035**
216036**   FTS5_SEGITER_REVERSE:
216037**     This flag is only ever set if FTS5_SEGITER_ONETERM is also set. If
216038**     it is set, iterate through rowid in descending order instead of the
216039**     default ascending order.
216040**
216041** iRowidOffset/nRowidOffset/aRowidOffset:
216042**     These are used if the FTS5_SEGITER_REVERSE flag is set.
216043**
216044**     For each rowid on the page corresponding to the current term, the
216045**     corresponding aRowidOffset[] entry is set to the byte offset of the
216046**     start of the "position-list-size" field within the page.
216047**
216048** iTermIdx:
216049**     Index of current term on iTermLeafPgno.
216050*/
216051struct Fts5SegIter {
216052  Fts5StructureSegment *pSeg;     /* Segment to iterate through */
216053  int flags;                      /* Mask of configuration flags */
216054  int iLeafPgno;                  /* Current leaf page number */
216055  Fts5Data *pLeaf;                /* Current leaf data */
216056  Fts5Data *pNextLeaf;            /* Leaf page (iLeafPgno+1) */
216057  int iLeafOffset;                /* Byte offset within current leaf */
216058
216059  /* Next method */
216060  void (*xNext)(Fts5Index*, Fts5SegIter*, int*);
216061
216062  /* The page and offset from which the current term was read. The offset
216063  ** is the offset of the first rowid in the current doclist.  */
216064  int iTermLeafPgno;
216065  int iTermLeafOffset;
216066
216067  int iPgidxOff;                  /* Next offset in pgidx */
216068  int iEndofDoclist;
216069
216070  /* The following are only used if the FTS5_SEGITER_REVERSE flag is set. */
216071  int iRowidOffset;               /* Current entry in aRowidOffset[] */
216072  int nRowidOffset;               /* Allocated size of aRowidOffset[] array */
216073  int *aRowidOffset;              /* Array of offset to rowid fields */
216074
216075  Fts5DlidxIter *pDlidx;          /* If there is a doclist-index */
216076
216077  /* Variables populated based on current entry. */
216078  Fts5Buffer term;                /* Current term */
216079  i64 iRowid;                     /* Current rowid */
216080  int nPos;                       /* Number of bytes in current position list */
216081  u8 bDel;                        /* True if the delete flag is set */
216082};
216083
216084/*
216085** Argument is a pointer to an Fts5Data structure that contains a
216086** leaf page.
216087*/
216088#define ASSERT_SZLEAF_OK(x) assert( \
216089    (x)->szLeaf==(x)->nn || (x)->szLeaf==fts5GetU16(&(x)->p[2]) \
216090)
216091
216092#define FTS5_SEGITER_ONETERM 0x01
216093#define FTS5_SEGITER_REVERSE 0x02
216094
216095/*
216096** Argument is a pointer to an Fts5Data structure that contains a leaf
216097** page. This macro evaluates to true if the leaf contains no terms, or
216098** false if it contains at least one term.
216099*/
216100#define fts5LeafIsTermless(x) ((x)->szLeaf >= (x)->nn)
216101
216102#define fts5LeafTermOff(x, i) (fts5GetU16(&(x)->p[(x)->szLeaf + (i)*2]))
216103
216104#define fts5LeafFirstRowidOff(x) (fts5GetU16((x)->p))
216105
216106/*
216107** Object for iterating through the merged results of one or more segments,
216108** visiting each term/rowid pair in the merged data.
216109**
216110** nSeg is always a power of two greater than or equal to the number of
216111** segments that this object is merging data from. Both the aSeg[] and
216112** aFirst[] arrays are sized at nSeg entries. The aSeg[] array is padded
216113** with zeroed objects - these are handled as if they were iterators opened
216114** on empty segments.
216115**
216116** The results of comparing segments aSeg[N] and aSeg[N+1], where N is an
216117** even number, is stored in aFirst[(nSeg+N)/2]. The "result" of the
216118** comparison in this context is the index of the iterator that currently
216119** points to the smaller term/rowid combination. Iterators at EOF are
216120** considered to be greater than all other iterators.
216121**
216122** aFirst[1] contains the index in aSeg[] of the iterator that points to
216123** the smallest key overall. aFirst[0] is unused.
216124**
216125** poslist:
216126**   Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered.
216127**   There is no way to tell if this is populated or not.
216128*/
216129struct Fts5Iter {
216130  Fts5IndexIter base;             /* Base class containing output vars */
216131
216132  Fts5Index *pIndex;              /* Index that owns this iterator */
216133  Fts5Buffer poslist;             /* Buffer containing current poslist */
216134  Fts5Colset *pColset;            /* Restrict matches to these columns */
216135
216136  /* Invoked to set output variables. */
216137  void (*xSetOutputs)(Fts5Iter*, Fts5SegIter*);
216138
216139  int nSeg;                       /* Size of aSeg[] array */
216140  int bRev;                       /* True to iterate in reverse order */
216141  u8 bSkipEmpty;                  /* True to skip deleted entries */
216142
216143  i64 iSwitchRowid;               /* Firstest rowid of other than aFirst[1] */
216144  Fts5CResult *aFirst;            /* Current merge state (see above) */
216145  Fts5SegIter aSeg[1];            /* Array of segment iterators */
216146};
216147
216148
216149/*
216150** An instance of the following type is used to iterate through the contents
216151** of a doclist-index record.
216152**
216153** pData:
216154**   Record containing the doclist-index data.
216155**
216156** bEof:
216157**   Set to true once iterator has reached EOF.
216158**
216159** iOff:
216160**   Set to the current offset within record pData.
216161*/
216162struct Fts5DlidxLvl {
216163  Fts5Data *pData;              /* Data for current page of this level */
216164  int iOff;                     /* Current offset into pData */
216165  int bEof;                     /* At EOF already */
216166  int iFirstOff;                /* Used by reverse iterators */
216167
216168  /* Output variables */
216169  int iLeafPgno;                /* Page number of current leaf page */
216170  i64 iRowid;                   /* First rowid on leaf iLeafPgno */
216171};
216172struct Fts5DlidxIter {
216173  int nLvl;
216174  int iSegid;
216175  Fts5DlidxLvl aLvl[1];
216176};
216177
216178static void fts5PutU16(u8 *aOut, u16 iVal){
216179  aOut[0] = (iVal>>8);
216180  aOut[1] = (iVal&0xFF);
216181}
216182
216183static u16 fts5GetU16(const u8 *aIn){
216184  return ((u16)aIn[0] << 8) + aIn[1];
216185}
216186
216187/*
216188** Allocate and return a buffer at least nByte bytes in size.
216189**
216190** If an OOM error is encountered, return NULL and set the error code in
216191** the Fts5Index handle passed as the first argument.
216192*/
216193static void *fts5IdxMalloc(Fts5Index *p, sqlite3_int64 nByte){
216194  return sqlite3Fts5MallocZero(&p->rc, nByte);
216195}
216196
216197/*
216198** Compare the contents of the pLeft buffer with the pRight/nRight blob.
216199**
216200** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
216201** +ve if pRight is smaller than pLeft. In other words:
216202**
216203**     res = *pLeft - *pRight
216204*/
216205#ifdef SQLITE_DEBUG
216206static int fts5BufferCompareBlob(
216207  Fts5Buffer *pLeft,              /* Left hand side of comparison */
216208  const u8 *pRight, int nRight    /* Right hand side of comparison */
216209){
216210  int nCmp = MIN(pLeft->n, nRight);
216211  int res = memcmp(pLeft->p, pRight, nCmp);
216212  return (res==0 ? (pLeft->n - nRight) : res);
216213}
216214#endif
216215
216216/*
216217** Compare the contents of the two buffers using memcmp(). If one buffer
216218** is a prefix of the other, it is considered the lesser.
216219**
216220** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
216221** +ve if pRight is smaller than pLeft. In other words:
216222**
216223**     res = *pLeft - *pRight
216224*/
216225static int fts5BufferCompare(Fts5Buffer *pLeft, Fts5Buffer *pRight){
216226  int nCmp = MIN(pLeft->n, pRight->n);
216227  int res = fts5Memcmp(pLeft->p, pRight->p, nCmp);
216228  return (res==0 ? (pLeft->n - pRight->n) : res);
216229}
216230
216231static int fts5LeafFirstTermOff(Fts5Data *pLeaf){
216232  int ret;
216233  fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf], ret);
216234  return ret;
216235}
216236
216237/*
216238** Close the read-only blob handle, if it is open.
216239*/
216240static void sqlite3Fts5IndexCloseReader(Fts5Index *p){
216241  if( p->pReader ){
216242    sqlite3_blob *pReader = p->pReader;
216243    p->pReader = 0;
216244    sqlite3_blob_close(pReader);
216245  }
216246}
216247
216248/*
216249** Retrieve a record from the %_data table.
216250**
216251** If an error occurs, NULL is returned and an error left in the
216252** Fts5Index object.
216253*/
216254static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){
216255  Fts5Data *pRet = 0;
216256  if( p->rc==SQLITE_OK ){
216257    int rc = SQLITE_OK;
216258
216259    if( p->pReader ){
216260      /* This call may return SQLITE_ABORT if there has been a savepoint
216261      ** rollback since it was last used. In this case a new blob handle
216262      ** is required.  */
216263      sqlite3_blob *pBlob = p->pReader;
216264      p->pReader = 0;
216265      rc = sqlite3_blob_reopen(pBlob, iRowid);
216266      assert( p->pReader==0 );
216267      p->pReader = pBlob;
216268      if( rc!=SQLITE_OK ){
216269        sqlite3Fts5IndexCloseReader(p);
216270      }
216271      if( rc==SQLITE_ABORT ) rc = SQLITE_OK;
216272    }
216273
216274    /* If the blob handle is not open at this point, open it and seek
216275    ** to the requested entry.  */
216276    if( p->pReader==0 && rc==SQLITE_OK ){
216277      Fts5Config *pConfig = p->pConfig;
216278      rc = sqlite3_blob_open(pConfig->db,
216279          pConfig->zDb, p->zDataTbl, "block", iRowid, 0, &p->pReader
216280      );
216281    }
216282
216283    /* If either of the sqlite3_blob_open() or sqlite3_blob_reopen() calls
216284    ** above returned SQLITE_ERROR, return SQLITE_CORRUPT_VTAB instead.
216285    ** All the reasons those functions might return SQLITE_ERROR - missing
216286    ** table, missing row, non-blob/text in block column - indicate
216287    ** backing store corruption.  */
216288    if( rc==SQLITE_ERROR ) rc = FTS5_CORRUPT;
216289
216290    if( rc==SQLITE_OK ){
216291      u8 *aOut = 0;               /* Read blob data into this buffer */
216292      int nByte = sqlite3_blob_bytes(p->pReader);
216293      sqlite3_int64 nAlloc = sizeof(Fts5Data) + nByte + FTS5_DATA_PADDING;
216294      pRet = (Fts5Data*)sqlite3_malloc64(nAlloc);
216295      if( pRet ){
216296        pRet->nn = nByte;
216297        aOut = pRet->p = (u8*)&pRet[1];
216298      }else{
216299        rc = SQLITE_NOMEM;
216300      }
216301
216302      if( rc==SQLITE_OK ){
216303        rc = sqlite3_blob_read(p->pReader, aOut, nByte, 0);
216304      }
216305      if( rc!=SQLITE_OK ){
216306        sqlite3_free(pRet);
216307        pRet = 0;
216308      }else{
216309        /* TODO1: Fix this */
216310        pRet->p[nByte] = 0x00;
216311        pRet->p[nByte+1] = 0x00;
216312        pRet->szLeaf = fts5GetU16(&pRet->p[2]);
216313      }
216314    }
216315    p->rc = rc;
216316    p->nRead++;
216317  }
216318
216319  assert( (pRet==0)==(p->rc!=SQLITE_OK) );
216320  return pRet;
216321}
216322
216323/*
216324** Release a reference to data record returned by an earlier call to
216325** fts5DataRead().
216326*/
216327static void fts5DataRelease(Fts5Data *pData){
216328  sqlite3_free(pData);
216329}
216330
216331static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){
216332  Fts5Data *pRet = fts5DataRead(p, iRowid);
216333  if( pRet ){
216334    if( pRet->nn<4 || pRet->szLeaf>pRet->nn ){
216335      p->rc = FTS5_CORRUPT;
216336      fts5DataRelease(pRet);
216337      pRet = 0;
216338    }
216339  }
216340  return pRet;
216341}
216342
216343static int fts5IndexPrepareStmt(
216344  Fts5Index *p,
216345  sqlite3_stmt **ppStmt,
216346  char *zSql
216347){
216348  if( p->rc==SQLITE_OK ){
216349    if( zSql ){
216350      p->rc = sqlite3_prepare_v3(p->pConfig->db, zSql, -1,
216351          SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB,
216352          ppStmt, 0);
216353    }else{
216354      p->rc = SQLITE_NOMEM;
216355    }
216356  }
216357  sqlite3_free(zSql);
216358  return p->rc;
216359}
216360
216361
216362/*
216363** INSERT OR REPLACE a record into the %_data table.
216364*/
216365static void fts5DataWrite(Fts5Index *p, i64 iRowid, const u8 *pData, int nData){
216366  if( p->rc!=SQLITE_OK ) return;
216367
216368  if( p->pWriter==0 ){
216369    Fts5Config *pConfig = p->pConfig;
216370    fts5IndexPrepareStmt(p, &p->pWriter, sqlite3_mprintf(
216371          "REPLACE INTO '%q'.'%q_data'(id, block) VALUES(?,?)",
216372          pConfig->zDb, pConfig->zName
216373    ));
216374    if( p->rc ) return;
216375  }
216376
216377  sqlite3_bind_int64(p->pWriter, 1, iRowid);
216378  sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC);
216379  sqlite3_step(p->pWriter);
216380  p->rc = sqlite3_reset(p->pWriter);
216381  sqlite3_bind_null(p->pWriter, 2);
216382}
216383
216384/*
216385** Execute the following SQL:
216386**
216387**     DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
216388*/
216389static void fts5DataDelete(Fts5Index *p, i64 iFirst, i64 iLast){
216390  if( p->rc!=SQLITE_OK ) return;
216391
216392  if( p->pDeleter==0 ){
216393    Fts5Config *pConfig = p->pConfig;
216394    char *zSql = sqlite3_mprintf(
216395        "DELETE FROM '%q'.'%q_data' WHERE id>=? AND id<=?",
216396          pConfig->zDb, pConfig->zName
216397    );
216398    if( fts5IndexPrepareStmt(p, &p->pDeleter, zSql) ) return;
216399  }
216400
216401  sqlite3_bind_int64(p->pDeleter, 1, iFirst);
216402  sqlite3_bind_int64(p->pDeleter, 2, iLast);
216403  sqlite3_step(p->pDeleter);
216404  p->rc = sqlite3_reset(p->pDeleter);
216405}
216406
216407/*
216408** Remove all records associated with segment iSegid.
216409*/
216410static void fts5DataRemoveSegment(Fts5Index *p, int iSegid){
216411  i64 iFirst = FTS5_SEGMENT_ROWID(iSegid, 0);
216412  i64 iLast = FTS5_SEGMENT_ROWID(iSegid+1, 0)-1;
216413  fts5DataDelete(p, iFirst, iLast);
216414  if( p->pIdxDeleter==0 ){
216415    Fts5Config *pConfig = p->pConfig;
216416    fts5IndexPrepareStmt(p, &p->pIdxDeleter, sqlite3_mprintf(
216417          "DELETE FROM '%q'.'%q_idx' WHERE segid=?",
216418          pConfig->zDb, pConfig->zName
216419    ));
216420  }
216421  if( p->rc==SQLITE_OK ){
216422    sqlite3_bind_int(p->pIdxDeleter, 1, iSegid);
216423    sqlite3_step(p->pIdxDeleter);
216424    p->rc = sqlite3_reset(p->pIdxDeleter);
216425  }
216426}
216427
216428/*
216429** Release a reference to an Fts5Structure object returned by an earlier
216430** call to fts5StructureRead() or fts5StructureDecode().
216431*/
216432static void fts5StructureRelease(Fts5Structure *pStruct){
216433  if( pStruct && 0>=(--pStruct->nRef) ){
216434    int i;
216435    assert( pStruct->nRef==0 );
216436    for(i=0; i<pStruct->nLevel; i++){
216437      sqlite3_free(pStruct->aLevel[i].aSeg);
216438    }
216439    sqlite3_free(pStruct);
216440  }
216441}
216442
216443static void fts5StructureRef(Fts5Structure *pStruct){
216444  pStruct->nRef++;
216445}
216446
216447/*
216448** Deserialize and return the structure record currently stored in serialized
216449** form within buffer pData/nData.
216450**
216451** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
216452** are over-allocated by one slot. This allows the structure contents
216453** to be more easily edited.
216454**
216455** If an error occurs, *ppOut is set to NULL and an SQLite error code
216456** returned. Otherwise, *ppOut is set to point to the new object and
216457** SQLITE_OK returned.
216458*/
216459static int fts5StructureDecode(
216460  const u8 *pData,                /* Buffer containing serialized structure */
216461  int nData,                      /* Size of buffer pData in bytes */
216462  int *piCookie,                  /* Configuration cookie value */
216463  Fts5Structure **ppOut           /* OUT: Deserialized object */
216464){
216465  int rc = SQLITE_OK;
216466  int i = 0;
216467  int iLvl;
216468  int nLevel = 0;
216469  int nSegment = 0;
216470  sqlite3_int64 nByte;            /* Bytes of space to allocate at pRet */
216471  Fts5Structure *pRet = 0;        /* Structure object to return */
216472
216473  /* Grab the cookie value */
216474  if( piCookie ) *piCookie = sqlite3Fts5Get32(pData);
216475  i = 4;
216476
216477  /* Read the total number of levels and segments from the start of the
216478  ** structure record.  */
216479  i += fts5GetVarint32(&pData[i], nLevel);
216480  i += fts5GetVarint32(&pData[i], nSegment);
216481  if( nLevel>FTS5_MAX_SEGMENT   || nLevel<0
216482   || nSegment>FTS5_MAX_SEGMENT || nSegment<0
216483  ){
216484    return FTS5_CORRUPT;
216485  }
216486  nByte = (
216487      sizeof(Fts5Structure) +                    /* Main structure */
216488      sizeof(Fts5StructureLevel) * (nLevel-1)    /* aLevel[] array */
216489  );
216490  pRet = (Fts5Structure*)sqlite3Fts5MallocZero(&rc, nByte);
216491
216492  if( pRet ){
216493    pRet->nRef = 1;
216494    pRet->nLevel = nLevel;
216495    pRet->nSegment = nSegment;
216496    i += sqlite3Fts5GetVarint(&pData[i], &pRet->nWriteCounter);
216497
216498    for(iLvl=0; rc==SQLITE_OK && iLvl<nLevel; iLvl++){
216499      Fts5StructureLevel *pLvl = &pRet->aLevel[iLvl];
216500      int nTotal = 0;
216501      int iSeg;
216502
216503      if( i>=nData ){
216504        rc = FTS5_CORRUPT;
216505      }else{
216506        i += fts5GetVarint32(&pData[i], pLvl->nMerge);
216507        i += fts5GetVarint32(&pData[i], nTotal);
216508        if( nTotal<pLvl->nMerge ) rc = FTS5_CORRUPT;
216509        pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&rc,
216510            nTotal * sizeof(Fts5StructureSegment)
216511        );
216512        nSegment -= nTotal;
216513      }
216514
216515      if( rc==SQLITE_OK ){
216516        pLvl->nSeg = nTotal;
216517        for(iSeg=0; iSeg<nTotal; iSeg++){
216518          Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
216519          if( i>=nData ){
216520            rc = FTS5_CORRUPT;
216521            break;
216522          }
216523          i += fts5GetVarint32(&pData[i], pSeg->iSegid);
216524          i += fts5GetVarint32(&pData[i], pSeg->pgnoFirst);
216525          i += fts5GetVarint32(&pData[i], pSeg->pgnoLast);
216526          if( pSeg->pgnoLast<pSeg->pgnoFirst ){
216527            rc = FTS5_CORRUPT;
216528            break;
216529          }
216530        }
216531        if( iLvl>0 && pLvl[-1].nMerge && nTotal==0 ) rc = FTS5_CORRUPT;
216532        if( iLvl==nLevel-1 && pLvl->nMerge ) rc = FTS5_CORRUPT;
216533      }
216534    }
216535    if( nSegment!=0 && rc==SQLITE_OK ) rc = FTS5_CORRUPT;
216536
216537    if( rc!=SQLITE_OK ){
216538      fts5StructureRelease(pRet);
216539      pRet = 0;
216540    }
216541  }
216542
216543  *ppOut = pRet;
216544  return rc;
216545}
216546
216547/*
216548**
216549*/
216550static void fts5StructureAddLevel(int *pRc, Fts5Structure **ppStruct){
216551  if( *pRc==SQLITE_OK ){
216552    Fts5Structure *pStruct = *ppStruct;
216553    int nLevel = pStruct->nLevel;
216554    sqlite3_int64 nByte = (
216555        sizeof(Fts5Structure) +                  /* Main structure */
216556        sizeof(Fts5StructureLevel) * (nLevel+1)  /* aLevel[] array */
216557    );
216558
216559    pStruct = sqlite3_realloc64(pStruct, nByte);
216560    if( pStruct ){
216561      memset(&pStruct->aLevel[nLevel], 0, sizeof(Fts5StructureLevel));
216562      pStruct->nLevel++;
216563      *ppStruct = pStruct;
216564    }else{
216565      *pRc = SQLITE_NOMEM;
216566    }
216567  }
216568}
216569
216570/*
216571** Extend level iLvl so that there is room for at least nExtra more
216572** segments.
216573*/
216574static void fts5StructureExtendLevel(
216575  int *pRc,
216576  Fts5Structure *pStruct,
216577  int iLvl,
216578  int nExtra,
216579  int bInsert
216580){
216581  if( *pRc==SQLITE_OK ){
216582    Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
216583    Fts5StructureSegment *aNew;
216584    sqlite3_int64 nByte;
216585
216586    nByte = (pLvl->nSeg + nExtra) * sizeof(Fts5StructureSegment);
216587    aNew = sqlite3_realloc64(pLvl->aSeg, nByte);
216588    if( aNew ){
216589      if( bInsert==0 ){
216590        memset(&aNew[pLvl->nSeg], 0, sizeof(Fts5StructureSegment) * nExtra);
216591      }else{
216592        int nMove = pLvl->nSeg * sizeof(Fts5StructureSegment);
216593        memmove(&aNew[nExtra], aNew, nMove);
216594        memset(aNew, 0, sizeof(Fts5StructureSegment) * nExtra);
216595      }
216596      pLvl->aSeg = aNew;
216597    }else{
216598      *pRc = SQLITE_NOMEM;
216599    }
216600  }
216601}
216602
216603static Fts5Structure *fts5StructureReadUncached(Fts5Index *p){
216604  Fts5Structure *pRet = 0;
216605  Fts5Config *pConfig = p->pConfig;
216606  int iCookie;                    /* Configuration cookie */
216607  Fts5Data *pData;
216608
216609  pData = fts5DataRead(p, FTS5_STRUCTURE_ROWID);
216610  if( p->rc==SQLITE_OK ){
216611    /* TODO: Do we need this if the leaf-index is appended? Probably... */
216612    memset(&pData->p[pData->nn], 0, FTS5_DATA_PADDING);
216613    p->rc = fts5StructureDecode(pData->p, pData->nn, &iCookie, &pRet);
216614    if( p->rc==SQLITE_OK && (pConfig->pgsz==0 || pConfig->iCookie!=iCookie) ){
216615      p->rc = sqlite3Fts5ConfigLoad(pConfig, iCookie);
216616    }
216617    fts5DataRelease(pData);
216618    if( p->rc!=SQLITE_OK ){
216619      fts5StructureRelease(pRet);
216620      pRet = 0;
216621    }
216622  }
216623
216624  return pRet;
216625}
216626
216627static i64 fts5IndexDataVersion(Fts5Index *p){
216628  i64 iVersion = 0;
216629
216630  if( p->rc==SQLITE_OK ){
216631    if( p->pDataVersion==0 ){
216632      p->rc = fts5IndexPrepareStmt(p, &p->pDataVersion,
216633          sqlite3_mprintf("PRAGMA %Q.data_version", p->pConfig->zDb)
216634          );
216635      if( p->rc ) return 0;
216636    }
216637
216638    if( SQLITE_ROW==sqlite3_step(p->pDataVersion) ){
216639      iVersion = sqlite3_column_int64(p->pDataVersion, 0);
216640    }
216641    p->rc = sqlite3_reset(p->pDataVersion);
216642  }
216643
216644  return iVersion;
216645}
216646
216647/*
216648** Read, deserialize and return the structure record.
216649**
216650** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
216651** are over-allocated as described for function fts5StructureDecode()
216652** above.
216653**
216654** If an error occurs, NULL is returned and an error code left in the
216655** Fts5Index handle. If an error has already occurred when this function
216656** is called, it is a no-op.
216657*/
216658static Fts5Structure *fts5StructureRead(Fts5Index *p){
216659
216660  if( p->pStruct==0 ){
216661    p->iStructVersion = fts5IndexDataVersion(p);
216662    if( p->rc==SQLITE_OK ){
216663      p->pStruct = fts5StructureReadUncached(p);
216664    }
216665  }
216666
216667#if 0
216668  else{
216669    Fts5Structure *pTest = fts5StructureReadUncached(p);
216670    if( pTest ){
216671      int i, j;
216672      assert_nc( p->pStruct->nSegment==pTest->nSegment );
216673      assert_nc( p->pStruct->nLevel==pTest->nLevel );
216674      for(i=0; i<pTest->nLevel; i++){
216675        assert_nc( p->pStruct->aLevel[i].nMerge==pTest->aLevel[i].nMerge );
216676        assert_nc( p->pStruct->aLevel[i].nSeg==pTest->aLevel[i].nSeg );
216677        for(j=0; j<pTest->aLevel[i].nSeg; j++){
216678          Fts5StructureSegment *p1 = &pTest->aLevel[i].aSeg[j];
216679          Fts5StructureSegment *p2 = &p->pStruct->aLevel[i].aSeg[j];
216680          assert_nc( p1->iSegid==p2->iSegid );
216681          assert_nc( p1->pgnoFirst==p2->pgnoFirst );
216682          assert_nc( p1->pgnoLast==p2->pgnoLast );
216683        }
216684      }
216685      fts5StructureRelease(pTest);
216686    }
216687  }
216688#endif
216689
216690  if( p->rc!=SQLITE_OK ) return 0;
216691  assert( p->iStructVersion!=0 );
216692  assert( p->pStruct!=0 );
216693  fts5StructureRef(p->pStruct);
216694  return p->pStruct;
216695}
216696
216697static void fts5StructureInvalidate(Fts5Index *p){
216698  if( p->pStruct ){
216699    fts5StructureRelease(p->pStruct);
216700    p->pStruct = 0;
216701  }
216702}
216703
216704/*
216705** Return the total number of segments in index structure pStruct. This
216706** function is only ever used as part of assert() conditions.
216707*/
216708#ifdef SQLITE_DEBUG
216709static int fts5StructureCountSegments(Fts5Structure *pStruct){
216710  int nSegment = 0;               /* Total number of segments */
216711  if( pStruct ){
216712    int iLvl;                     /* Used to iterate through levels */
216713    for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
216714      nSegment += pStruct->aLevel[iLvl].nSeg;
216715    }
216716  }
216717
216718  return nSegment;
216719}
216720#endif
216721
216722#define fts5BufferSafeAppendBlob(pBuf, pBlob, nBlob) {     \
216723  assert( (pBuf)->nSpace>=((pBuf)->n+nBlob) );             \
216724  memcpy(&(pBuf)->p[(pBuf)->n], pBlob, nBlob);             \
216725  (pBuf)->n += nBlob;                                      \
216726}
216727
216728#define fts5BufferSafeAppendVarint(pBuf, iVal) {                \
216729  (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf)->n], (iVal));  \
216730  assert( (pBuf)->nSpace>=(pBuf)->n );                          \
216731}
216732
216733
216734/*
216735** Serialize and store the "structure" record.
216736**
216737** If an error occurs, leave an error code in the Fts5Index object. If an
216738** error has already occurred, this function is a no-op.
216739*/
216740static void fts5StructureWrite(Fts5Index *p, Fts5Structure *pStruct){
216741  if( p->rc==SQLITE_OK ){
216742    Fts5Buffer buf;               /* Buffer to serialize record into */
216743    int iLvl;                     /* Used to iterate through levels */
216744    int iCookie;                  /* Cookie value to store */
216745
216746    assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
216747    memset(&buf, 0, sizeof(Fts5Buffer));
216748
216749    /* Append the current configuration cookie */
216750    iCookie = p->pConfig->iCookie;
216751    if( iCookie<0 ) iCookie = 0;
216752
216753    if( 0==sqlite3Fts5BufferSize(&p->rc, &buf, 4+9+9+9) ){
216754      sqlite3Fts5Put32(buf.p, iCookie);
216755      buf.n = 4;
216756      fts5BufferSafeAppendVarint(&buf, pStruct->nLevel);
216757      fts5BufferSafeAppendVarint(&buf, pStruct->nSegment);
216758      fts5BufferSafeAppendVarint(&buf, (i64)pStruct->nWriteCounter);
216759    }
216760
216761    for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
216762      int iSeg;                     /* Used to iterate through segments */
216763      Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
216764      fts5BufferAppendVarint(&p->rc, &buf, pLvl->nMerge);
216765      fts5BufferAppendVarint(&p->rc, &buf, pLvl->nSeg);
216766      assert( pLvl->nMerge<=pLvl->nSeg );
216767
216768      for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
216769        fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].iSegid);
216770        fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoFirst);
216771        fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoLast);
216772      }
216773    }
216774
216775    fts5DataWrite(p, FTS5_STRUCTURE_ROWID, buf.p, buf.n);
216776    fts5BufferFree(&buf);
216777  }
216778}
216779
216780#if 0
216781static void fts5DebugStructure(int*,Fts5Buffer*,Fts5Structure*);
216782static void fts5PrintStructure(const char *zCaption, Fts5Structure *pStruct){
216783  int rc = SQLITE_OK;
216784  Fts5Buffer buf;
216785  memset(&buf, 0, sizeof(buf));
216786  fts5DebugStructure(&rc, &buf, pStruct);
216787  fprintf(stdout, "%s: %s\n", zCaption, buf.p);
216788  fflush(stdout);
216789  fts5BufferFree(&buf);
216790}
216791#else
216792# define fts5PrintStructure(x,y)
216793#endif
216794
216795static int fts5SegmentSize(Fts5StructureSegment *pSeg){
216796  return 1 + pSeg->pgnoLast - pSeg->pgnoFirst;
216797}
216798
216799/*
216800** Return a copy of index structure pStruct. Except, promote as many
216801** segments as possible to level iPromote. If an OOM occurs, NULL is
216802** returned.
216803*/
216804static void fts5StructurePromoteTo(
216805  Fts5Index *p,
216806  int iPromote,
216807  int szPromote,
216808  Fts5Structure *pStruct
216809){
216810  int il, is;
216811  Fts5StructureLevel *pOut = &pStruct->aLevel[iPromote];
216812
216813  if( pOut->nMerge==0 ){
216814    for(il=iPromote+1; il<pStruct->nLevel; il++){
216815      Fts5StructureLevel *pLvl = &pStruct->aLevel[il];
216816      if( pLvl->nMerge ) return;
216817      for(is=pLvl->nSeg-1; is>=0; is--){
216818        int sz = fts5SegmentSize(&pLvl->aSeg[is]);
216819        if( sz>szPromote ) return;
216820        fts5StructureExtendLevel(&p->rc, pStruct, iPromote, 1, 1);
216821        if( p->rc ) return;
216822        memcpy(pOut->aSeg, &pLvl->aSeg[is], sizeof(Fts5StructureSegment));
216823        pOut->nSeg++;
216824        pLvl->nSeg--;
216825      }
216826    }
216827  }
216828}
216829
216830/*
216831** A new segment has just been written to level iLvl of index structure
216832** pStruct. This function determines if any segments should be promoted
216833** as a result. Segments are promoted in two scenarios:
216834**
216835**   a) If the segment just written is smaller than one or more segments
216836**      within the previous populated level, it is promoted to the previous
216837**      populated level.
216838**
216839**   b) If the segment just written is larger than the newest segment on
216840**      the next populated level, then that segment, and any other adjacent
216841**      segments that are also smaller than the one just written, are
216842**      promoted.
216843**
216844** If one or more segments are promoted, the structure object is updated
216845** to reflect this.
216846*/
216847static void fts5StructurePromote(
216848  Fts5Index *p,                   /* FTS5 backend object */
216849  int iLvl,                       /* Index level just updated */
216850  Fts5Structure *pStruct          /* Index structure */
216851){
216852  if( p->rc==SQLITE_OK ){
216853    int iTst;
216854    int iPromote = -1;
216855    int szPromote = 0;            /* Promote anything this size or smaller */
216856    Fts5StructureSegment *pSeg;   /* Segment just written */
216857    int szSeg;                    /* Size of segment just written */
216858    int nSeg = pStruct->aLevel[iLvl].nSeg;
216859
216860    if( nSeg==0 ) return;
216861    pSeg = &pStruct->aLevel[iLvl].aSeg[pStruct->aLevel[iLvl].nSeg-1];
216862    szSeg = (1 + pSeg->pgnoLast - pSeg->pgnoFirst);
216863
216864    /* Check for condition (a) */
216865    for(iTst=iLvl-1; iTst>=0 && pStruct->aLevel[iTst].nSeg==0; iTst--);
216866    if( iTst>=0 ){
216867      int i;
216868      int szMax = 0;
216869      Fts5StructureLevel *pTst = &pStruct->aLevel[iTst];
216870      assert( pTst->nMerge==0 );
216871      for(i=0; i<pTst->nSeg; i++){
216872        int sz = pTst->aSeg[i].pgnoLast - pTst->aSeg[i].pgnoFirst + 1;
216873        if( sz>szMax ) szMax = sz;
216874      }
216875      if( szMax>=szSeg ){
216876        /* Condition (a) is true. Promote the newest segment on level
216877        ** iLvl to level iTst.  */
216878        iPromote = iTst;
216879        szPromote = szMax;
216880      }
216881    }
216882
216883    /* If condition (a) is not met, assume (b) is true. StructurePromoteTo()
216884    ** is a no-op if it is not.  */
216885    if( iPromote<0 ){
216886      iPromote = iLvl;
216887      szPromote = szSeg;
216888    }
216889    fts5StructurePromoteTo(p, iPromote, szPromote, pStruct);
216890  }
216891}
216892
216893
216894/*
216895** Advance the iterator passed as the only argument. If the end of the
216896** doclist-index page is reached, return non-zero.
216897*/
216898static int fts5DlidxLvlNext(Fts5DlidxLvl *pLvl){
216899  Fts5Data *pData = pLvl->pData;
216900
216901  if( pLvl->iOff==0 ){
216902    assert( pLvl->bEof==0 );
216903    pLvl->iOff = 1;
216904    pLvl->iOff += fts5GetVarint32(&pData->p[1], pLvl->iLeafPgno);
216905    pLvl->iOff += fts5GetVarint(&pData->p[pLvl->iOff], (u64*)&pLvl->iRowid);
216906    pLvl->iFirstOff = pLvl->iOff;
216907  }else{
216908    int iOff;
216909    for(iOff=pLvl->iOff; iOff<pData->nn; iOff++){
216910      if( pData->p[iOff] ) break;
216911    }
216912
216913    if( iOff<pData->nn ){
216914      i64 iVal;
216915      pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1;
216916      iOff += fts5GetVarint(&pData->p[iOff], (u64*)&iVal);
216917      pLvl->iRowid += iVal;
216918      pLvl->iOff = iOff;
216919    }else{
216920      pLvl->bEof = 1;
216921    }
216922  }
216923
216924  return pLvl->bEof;
216925}
216926
216927/*
216928** Advance the iterator passed as the only argument.
216929*/
216930static int fts5DlidxIterNextR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
216931  Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
216932
216933  assert( iLvl<pIter->nLvl );
216934  if( fts5DlidxLvlNext(pLvl) ){
216935    if( (iLvl+1) < pIter->nLvl ){
216936      fts5DlidxIterNextR(p, pIter, iLvl+1);
216937      if( pLvl[1].bEof==0 ){
216938        fts5DataRelease(pLvl->pData);
216939        memset(pLvl, 0, sizeof(Fts5DlidxLvl));
216940        pLvl->pData = fts5DataRead(p,
216941            FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
216942        );
216943        if( pLvl->pData ) fts5DlidxLvlNext(pLvl);
216944      }
216945    }
216946  }
216947
216948  return pIter->aLvl[0].bEof;
216949}
216950static int fts5DlidxIterNext(Fts5Index *p, Fts5DlidxIter *pIter){
216951  return fts5DlidxIterNextR(p, pIter, 0);
216952}
216953
216954/*
216955** The iterator passed as the first argument has the following fields set
216956** as follows. This function sets up the rest of the iterator so that it
216957** points to the first rowid in the doclist-index.
216958**
216959**   pData:
216960**     pointer to doclist-index record,
216961**
216962** When this function is called pIter->iLeafPgno is the page number the
216963** doclist is associated with (the one featuring the term).
216964*/
216965static int fts5DlidxIterFirst(Fts5DlidxIter *pIter){
216966  int i;
216967  for(i=0; i<pIter->nLvl; i++){
216968    fts5DlidxLvlNext(&pIter->aLvl[i]);
216969  }
216970  return pIter->aLvl[0].bEof;
216971}
216972
216973
216974static int fts5DlidxIterEof(Fts5Index *p, Fts5DlidxIter *pIter){
216975  return p->rc!=SQLITE_OK || pIter->aLvl[0].bEof;
216976}
216977
216978static void fts5DlidxIterLast(Fts5Index *p, Fts5DlidxIter *pIter){
216979  int i;
216980
216981  /* Advance each level to the last entry on the last page */
216982  for(i=pIter->nLvl-1; p->rc==SQLITE_OK && i>=0; i--){
216983    Fts5DlidxLvl *pLvl = &pIter->aLvl[i];
216984    while( fts5DlidxLvlNext(pLvl)==0 );
216985    pLvl->bEof = 0;
216986
216987    if( i>0 ){
216988      Fts5DlidxLvl *pChild = &pLvl[-1];
216989      fts5DataRelease(pChild->pData);
216990      memset(pChild, 0, sizeof(Fts5DlidxLvl));
216991      pChild->pData = fts5DataRead(p,
216992          FTS5_DLIDX_ROWID(pIter->iSegid, i-1, pLvl->iLeafPgno)
216993      );
216994    }
216995  }
216996}
216997
216998/*
216999** Move the iterator passed as the only argument to the previous entry.
217000*/
217001static int fts5DlidxLvlPrev(Fts5DlidxLvl *pLvl){
217002  int iOff = pLvl->iOff;
217003
217004  assert( pLvl->bEof==0 );
217005  if( iOff<=pLvl->iFirstOff ){
217006    pLvl->bEof = 1;
217007  }else{
217008    u8 *a = pLvl->pData->p;
217009    i64 iVal;
217010    int iLimit;
217011    int ii;
217012    int nZero = 0;
217013
217014    /* Currently iOff points to the first byte of a varint. This block
217015    ** decrements iOff until it points to the first byte of the previous
217016    ** varint. Taking care not to read any memory locations that occur
217017    ** before the buffer in memory.  */
217018    iLimit = (iOff>9 ? iOff-9 : 0);
217019    for(iOff--; iOff>iLimit; iOff--){
217020      if( (a[iOff-1] & 0x80)==0 ) break;
217021    }
217022
217023    fts5GetVarint(&a[iOff], (u64*)&iVal);
217024    pLvl->iRowid -= iVal;
217025    pLvl->iLeafPgno--;
217026
217027    /* Skip backwards past any 0x00 varints. */
217028    for(ii=iOff-1; ii>=pLvl->iFirstOff && a[ii]==0x00; ii--){
217029      nZero++;
217030    }
217031    if( ii>=pLvl->iFirstOff && (a[ii] & 0x80) ){
217032      /* The byte immediately before the last 0x00 byte has the 0x80 bit
217033      ** set. So the last 0x00 is only a varint 0 if there are 8 more 0x80
217034      ** bytes before a[ii]. */
217035      int bZero = 0;              /* True if last 0x00 counts */
217036      if( (ii-8)>=pLvl->iFirstOff ){
217037        int j;
217038        for(j=1; j<=8 && (a[ii-j] & 0x80); j++);
217039        bZero = (j>8);
217040      }
217041      if( bZero==0 ) nZero--;
217042    }
217043    pLvl->iLeafPgno -= nZero;
217044    pLvl->iOff = iOff - nZero;
217045  }
217046
217047  return pLvl->bEof;
217048}
217049
217050static int fts5DlidxIterPrevR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
217051  Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
217052
217053  assert( iLvl<pIter->nLvl );
217054  if( fts5DlidxLvlPrev(pLvl) ){
217055    if( (iLvl+1) < pIter->nLvl ){
217056      fts5DlidxIterPrevR(p, pIter, iLvl+1);
217057      if( pLvl[1].bEof==0 ){
217058        fts5DataRelease(pLvl->pData);
217059        memset(pLvl, 0, sizeof(Fts5DlidxLvl));
217060        pLvl->pData = fts5DataRead(p,
217061            FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
217062        );
217063        if( pLvl->pData ){
217064          while( fts5DlidxLvlNext(pLvl)==0 );
217065          pLvl->bEof = 0;
217066        }
217067      }
217068    }
217069  }
217070
217071  return pIter->aLvl[0].bEof;
217072}
217073static int fts5DlidxIterPrev(Fts5Index *p, Fts5DlidxIter *pIter){
217074  return fts5DlidxIterPrevR(p, pIter, 0);
217075}
217076
217077/*
217078** Free a doclist-index iterator object allocated by fts5DlidxIterInit().
217079*/
217080static void fts5DlidxIterFree(Fts5DlidxIter *pIter){
217081  if( pIter ){
217082    int i;
217083    for(i=0; i<pIter->nLvl; i++){
217084      fts5DataRelease(pIter->aLvl[i].pData);
217085    }
217086    sqlite3_free(pIter);
217087  }
217088}
217089
217090static Fts5DlidxIter *fts5DlidxIterInit(
217091  Fts5Index *p,                   /* Fts5 Backend to iterate within */
217092  int bRev,                       /* True for ORDER BY ASC */
217093  int iSegid,                     /* Segment id */
217094  int iLeafPg                     /* Leaf page number to load dlidx for */
217095){
217096  Fts5DlidxIter *pIter = 0;
217097  int i;
217098  int bDone = 0;
217099
217100  for(i=0; p->rc==SQLITE_OK && bDone==0; i++){
217101    sqlite3_int64 nByte = sizeof(Fts5DlidxIter) + i * sizeof(Fts5DlidxLvl);
217102    Fts5DlidxIter *pNew;
217103
217104    pNew = (Fts5DlidxIter*)sqlite3_realloc64(pIter, nByte);
217105    if( pNew==0 ){
217106      p->rc = SQLITE_NOMEM;
217107    }else{
217108      i64 iRowid = FTS5_DLIDX_ROWID(iSegid, i, iLeafPg);
217109      Fts5DlidxLvl *pLvl = &pNew->aLvl[i];
217110      pIter = pNew;
217111      memset(pLvl, 0, sizeof(Fts5DlidxLvl));
217112      pLvl->pData = fts5DataRead(p, iRowid);
217113      if( pLvl->pData && (pLvl->pData->p[0] & 0x0001)==0 ){
217114        bDone = 1;
217115      }
217116      pIter->nLvl = i+1;
217117    }
217118  }
217119
217120  if( p->rc==SQLITE_OK ){
217121    pIter->iSegid = iSegid;
217122    if( bRev==0 ){
217123      fts5DlidxIterFirst(pIter);
217124    }else{
217125      fts5DlidxIterLast(p, pIter);
217126    }
217127  }
217128
217129  if( p->rc!=SQLITE_OK ){
217130    fts5DlidxIterFree(pIter);
217131    pIter = 0;
217132  }
217133
217134  return pIter;
217135}
217136
217137static i64 fts5DlidxIterRowid(Fts5DlidxIter *pIter){
217138  return pIter->aLvl[0].iRowid;
217139}
217140static int fts5DlidxIterPgno(Fts5DlidxIter *pIter){
217141  return pIter->aLvl[0].iLeafPgno;
217142}
217143
217144/*
217145** Load the next leaf page into the segment iterator.
217146*/
217147static void fts5SegIterNextPage(
217148  Fts5Index *p,                   /* FTS5 backend object */
217149  Fts5SegIter *pIter              /* Iterator to advance to next page */
217150){
217151  Fts5Data *pLeaf;
217152  Fts5StructureSegment *pSeg = pIter->pSeg;
217153  fts5DataRelease(pIter->pLeaf);
217154  pIter->iLeafPgno++;
217155  if( pIter->pNextLeaf ){
217156    pIter->pLeaf = pIter->pNextLeaf;
217157    pIter->pNextLeaf = 0;
217158  }else if( pIter->iLeafPgno<=pSeg->pgnoLast ){
217159    pIter->pLeaf = fts5LeafRead(p,
217160        FTS5_SEGMENT_ROWID(pSeg->iSegid, pIter->iLeafPgno)
217161    );
217162  }else{
217163    pIter->pLeaf = 0;
217164  }
217165  pLeaf = pIter->pLeaf;
217166
217167  if( pLeaf ){
217168    pIter->iPgidxOff = pLeaf->szLeaf;
217169    if( fts5LeafIsTermless(pLeaf) ){
217170      pIter->iEndofDoclist = pLeaf->nn+1;
217171    }else{
217172      pIter->iPgidxOff += fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
217173          pIter->iEndofDoclist
217174      );
217175    }
217176  }
217177}
217178
217179/*
217180** Argument p points to a buffer containing a varint to be interpreted as a
217181** position list size field. Read the varint and return the number of bytes
217182** read. Before returning, set *pnSz to the number of bytes in the position
217183** list, and *pbDel to true if the delete flag is set, or false otherwise.
217184*/
217185static int fts5GetPoslistSize(const u8 *p, int *pnSz, int *pbDel){
217186  int nSz;
217187  int n = 0;
217188  fts5FastGetVarint32(p, n, nSz);
217189  assert_nc( nSz>=0 );
217190  *pnSz = nSz/2;
217191  *pbDel = nSz & 0x0001;
217192  return n;
217193}
217194
217195/*
217196** Fts5SegIter.iLeafOffset currently points to the first byte of a
217197** position-list size field. Read the value of the field and store it
217198** in the following variables:
217199**
217200**   Fts5SegIter.nPos
217201**   Fts5SegIter.bDel
217202**
217203** Leave Fts5SegIter.iLeafOffset pointing to the first byte of the
217204** position list content (if any).
217205*/
217206static void fts5SegIterLoadNPos(Fts5Index *p, Fts5SegIter *pIter){
217207  if( p->rc==SQLITE_OK ){
217208    int iOff = pIter->iLeafOffset;  /* Offset to read at */
217209    ASSERT_SZLEAF_OK(pIter->pLeaf);
217210    if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
217211      int iEod = MIN(pIter->iEndofDoclist, pIter->pLeaf->szLeaf);
217212      pIter->bDel = 0;
217213      pIter->nPos = 1;
217214      if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
217215        pIter->bDel = 1;
217216        iOff++;
217217        if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
217218          pIter->nPos = 1;
217219          iOff++;
217220        }else{
217221          pIter->nPos = 0;
217222        }
217223      }
217224    }else{
217225      int nSz;
217226      fts5FastGetVarint32(pIter->pLeaf->p, iOff, nSz);
217227      pIter->bDel = (nSz & 0x0001);
217228      pIter->nPos = nSz>>1;
217229      assert_nc( pIter->nPos>=0 );
217230    }
217231    pIter->iLeafOffset = iOff;
217232  }
217233}
217234
217235static void fts5SegIterLoadRowid(Fts5Index *p, Fts5SegIter *pIter){
217236  u8 *a = pIter->pLeaf->p;        /* Buffer to read data from */
217237  int iOff = pIter->iLeafOffset;
217238
217239  ASSERT_SZLEAF_OK(pIter->pLeaf);
217240  if( iOff>=pIter->pLeaf->szLeaf ){
217241    fts5SegIterNextPage(p, pIter);
217242    if( pIter->pLeaf==0 ){
217243      if( p->rc==SQLITE_OK ) p->rc = FTS5_CORRUPT;
217244      return;
217245    }
217246    iOff = 4;
217247    a = pIter->pLeaf->p;
217248  }
217249  iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
217250  pIter->iLeafOffset = iOff;
217251}
217252
217253/*
217254** Fts5SegIter.iLeafOffset currently points to the first byte of the
217255** "nSuffix" field of a term. Function parameter nKeep contains the value
217256** of the "nPrefix" field (if there was one - it is passed 0 if this is
217257** the first term in the segment).
217258**
217259** This function populates:
217260**
217261**   Fts5SegIter.term
217262**   Fts5SegIter.rowid
217263**
217264** accordingly and leaves (Fts5SegIter.iLeafOffset) set to the content of
217265** the first position list. The position list belonging to document
217266** (Fts5SegIter.iRowid).
217267*/
217268static void fts5SegIterLoadTerm(Fts5Index *p, Fts5SegIter *pIter, int nKeep){
217269  u8 *a = pIter->pLeaf->p;        /* Buffer to read data from */
217270  int iOff = pIter->iLeafOffset;  /* Offset to read at */
217271  int nNew;                       /* Bytes of new data */
217272
217273  iOff += fts5GetVarint32(&a[iOff], nNew);
217274  if( iOff+nNew>pIter->pLeaf->szLeaf || nKeep>pIter->term.n || nNew==0 ){
217275    p->rc = FTS5_CORRUPT;
217276    return;
217277  }
217278  pIter->term.n = nKeep;
217279  fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
217280  assert( pIter->term.n<=pIter->term.nSpace );
217281  iOff += nNew;
217282  pIter->iTermLeafOffset = iOff;
217283  pIter->iTermLeafPgno = pIter->iLeafPgno;
217284  pIter->iLeafOffset = iOff;
217285
217286  if( pIter->iPgidxOff>=pIter->pLeaf->nn ){
217287    pIter->iEndofDoclist = pIter->pLeaf->nn+1;
217288  }else{
217289    int nExtra;
217290    pIter->iPgidxOff += fts5GetVarint32(&a[pIter->iPgidxOff], nExtra);
217291    pIter->iEndofDoclist += nExtra;
217292  }
217293
217294  fts5SegIterLoadRowid(p, pIter);
217295}
217296
217297static void fts5SegIterNext(Fts5Index*, Fts5SegIter*, int*);
217298static void fts5SegIterNext_Reverse(Fts5Index*, Fts5SegIter*, int*);
217299static void fts5SegIterNext_None(Fts5Index*, Fts5SegIter*, int*);
217300
217301static void fts5SegIterSetNext(Fts5Index *p, Fts5SegIter *pIter){
217302  if( pIter->flags & FTS5_SEGITER_REVERSE ){
217303    pIter->xNext = fts5SegIterNext_Reverse;
217304  }else if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
217305    pIter->xNext = fts5SegIterNext_None;
217306  }else{
217307    pIter->xNext = fts5SegIterNext;
217308  }
217309}
217310
217311/*
217312** Initialize the iterator object pIter to iterate through the entries in
217313** segment pSeg. The iterator is left pointing to the first entry when
217314** this function returns.
217315**
217316** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
217317** an error has already occurred when this function is called, it is a no-op.
217318*/
217319static void fts5SegIterInit(
217320  Fts5Index *p,                   /* FTS index object */
217321  Fts5StructureSegment *pSeg,     /* Description of segment */
217322  Fts5SegIter *pIter              /* Object to populate */
217323){
217324  if( pSeg->pgnoFirst==0 ){
217325    /* This happens if the segment is being used as an input to an incremental
217326    ** merge and all data has already been "trimmed". See function
217327    ** fts5TrimSegments() for details. In this case leave the iterator empty.
217328    ** The caller will see the (pIter->pLeaf==0) and assume the iterator is
217329    ** at EOF already. */
217330    assert( pIter->pLeaf==0 );
217331    return;
217332  }
217333
217334  if( p->rc==SQLITE_OK ){
217335    memset(pIter, 0, sizeof(*pIter));
217336    fts5SegIterSetNext(p, pIter);
217337    pIter->pSeg = pSeg;
217338    pIter->iLeafPgno = pSeg->pgnoFirst-1;
217339    fts5SegIterNextPage(p, pIter);
217340  }
217341
217342  if( p->rc==SQLITE_OK ){
217343    pIter->iLeafOffset = 4;
217344    assert_nc( pIter->pLeaf->nn>4 );
217345    assert_nc( fts5LeafFirstTermOff(pIter->pLeaf)==4 );
217346    pIter->iPgidxOff = pIter->pLeaf->szLeaf+1;
217347    fts5SegIterLoadTerm(p, pIter, 0);
217348    fts5SegIterLoadNPos(p, pIter);
217349  }
217350}
217351
217352/*
217353** This function is only ever called on iterators created by calls to
217354** Fts5IndexQuery() with the FTS5INDEX_QUERY_DESC flag set.
217355**
217356** The iterator is in an unusual state when this function is called: the
217357** Fts5SegIter.iLeafOffset variable is set to the offset of the start of
217358** the position-list size field for the first relevant rowid on the page.
217359** Fts5SegIter.rowid is set, but nPos and bDel are not.
217360**
217361** This function advances the iterator so that it points to the last
217362** relevant rowid on the page and, if necessary, initializes the
217363** aRowidOffset[] and iRowidOffset variables. At this point the iterator
217364** is in its regular state - Fts5SegIter.iLeafOffset points to the first
217365** byte of the position list content associated with said rowid.
217366*/
217367static void fts5SegIterReverseInitPage(Fts5Index *p, Fts5SegIter *pIter){
217368  int eDetail = p->pConfig->eDetail;
217369  int n = pIter->pLeaf->szLeaf;
217370  int i = pIter->iLeafOffset;
217371  u8 *a = pIter->pLeaf->p;
217372  int iRowidOffset = 0;
217373
217374  if( n>pIter->iEndofDoclist ){
217375    n = pIter->iEndofDoclist;
217376  }
217377
217378  ASSERT_SZLEAF_OK(pIter->pLeaf);
217379  while( 1 ){
217380    i64 iDelta = 0;
217381
217382    if( eDetail==FTS5_DETAIL_NONE ){
217383      /* todo */
217384      if( i<n && a[i]==0 ){
217385        i++;
217386        if( i<n && a[i]==0 ) i++;
217387      }
217388    }else{
217389      int nPos;
217390      int bDummy;
217391      i += fts5GetPoslistSize(&a[i], &nPos, &bDummy);
217392      i += nPos;
217393    }
217394    if( i>=n ) break;
217395    i += fts5GetVarint(&a[i], (u64*)&iDelta);
217396    pIter->iRowid += iDelta;
217397
217398    /* If necessary, grow the pIter->aRowidOffset[] array. */
217399    if( iRowidOffset>=pIter->nRowidOffset ){
217400      int nNew = pIter->nRowidOffset + 8;
217401      int *aNew = (int*)sqlite3_realloc64(pIter->aRowidOffset,nNew*sizeof(int));
217402      if( aNew==0 ){
217403        p->rc = SQLITE_NOMEM;
217404        break;
217405      }
217406      pIter->aRowidOffset = aNew;
217407      pIter->nRowidOffset = nNew;
217408    }
217409
217410    pIter->aRowidOffset[iRowidOffset++] = pIter->iLeafOffset;
217411    pIter->iLeafOffset = i;
217412  }
217413  pIter->iRowidOffset = iRowidOffset;
217414  fts5SegIterLoadNPos(p, pIter);
217415}
217416
217417/*
217418**
217419*/
217420static void fts5SegIterReverseNewPage(Fts5Index *p, Fts5SegIter *pIter){
217421  assert( pIter->flags & FTS5_SEGITER_REVERSE );
217422  assert( pIter->flags & FTS5_SEGITER_ONETERM );
217423
217424  fts5DataRelease(pIter->pLeaf);
217425  pIter->pLeaf = 0;
217426  while( p->rc==SQLITE_OK && pIter->iLeafPgno>pIter->iTermLeafPgno ){
217427    Fts5Data *pNew;
217428    pIter->iLeafPgno--;
217429    pNew = fts5DataRead(p, FTS5_SEGMENT_ROWID(
217430          pIter->pSeg->iSegid, pIter->iLeafPgno
217431    ));
217432    if( pNew ){
217433      /* iTermLeafOffset may be equal to szLeaf if the term is the last
217434      ** thing on the page - i.e. the first rowid is on the following page.
217435      ** In this case leave pIter->pLeaf==0, this iterator is at EOF. */
217436      if( pIter->iLeafPgno==pIter->iTermLeafPgno ){
217437        assert( pIter->pLeaf==0 );
217438        if( pIter->iTermLeafOffset<pNew->szLeaf ){
217439          pIter->pLeaf = pNew;
217440          pIter->iLeafOffset = pIter->iTermLeafOffset;
217441        }
217442      }else{
217443        int iRowidOff;
217444        iRowidOff = fts5LeafFirstRowidOff(pNew);
217445        if( iRowidOff ){
217446          pIter->pLeaf = pNew;
217447          pIter->iLeafOffset = iRowidOff;
217448        }
217449      }
217450
217451      if( pIter->pLeaf ){
217452        u8 *a = &pIter->pLeaf->p[pIter->iLeafOffset];
217453        pIter->iLeafOffset += fts5GetVarint(a, (u64*)&pIter->iRowid);
217454        break;
217455      }else{
217456        fts5DataRelease(pNew);
217457      }
217458    }
217459  }
217460
217461  if( pIter->pLeaf ){
217462    pIter->iEndofDoclist = pIter->pLeaf->nn+1;
217463    fts5SegIterReverseInitPage(p, pIter);
217464  }
217465}
217466
217467/*
217468** Return true if the iterator passed as the second argument currently
217469** points to a delete marker. A delete marker is an entry with a 0 byte
217470** position-list.
217471*/
217472static int fts5MultiIterIsEmpty(Fts5Index *p, Fts5Iter *pIter){
217473  Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
217474  return (p->rc==SQLITE_OK && pSeg->pLeaf && pSeg->nPos==0);
217475}
217476
217477/*
217478** Advance iterator pIter to the next entry.
217479**
217480** This version of fts5SegIterNext() is only used by reverse iterators.
217481*/
217482static void fts5SegIterNext_Reverse(
217483  Fts5Index *p,                   /* FTS5 backend object */
217484  Fts5SegIter *pIter,             /* Iterator to advance */
217485  int *pbUnused                   /* Unused */
217486){
217487  assert( pIter->flags & FTS5_SEGITER_REVERSE );
217488  assert( pIter->pNextLeaf==0 );
217489  UNUSED_PARAM(pbUnused);
217490
217491  if( pIter->iRowidOffset>0 ){
217492    u8 *a = pIter->pLeaf->p;
217493    int iOff;
217494    i64 iDelta;
217495
217496    pIter->iRowidOffset--;
217497    pIter->iLeafOffset = pIter->aRowidOffset[pIter->iRowidOffset];
217498    fts5SegIterLoadNPos(p, pIter);
217499    iOff = pIter->iLeafOffset;
217500    if( p->pConfig->eDetail!=FTS5_DETAIL_NONE ){
217501      iOff += pIter->nPos;
217502    }
217503    fts5GetVarint(&a[iOff], (u64*)&iDelta);
217504    pIter->iRowid -= iDelta;
217505  }else{
217506    fts5SegIterReverseNewPage(p, pIter);
217507  }
217508}
217509
217510/*
217511** Advance iterator pIter to the next entry.
217512**
217513** This version of fts5SegIterNext() is only used if detail=none and the
217514** iterator is not a reverse direction iterator.
217515*/
217516static void fts5SegIterNext_None(
217517  Fts5Index *p,                   /* FTS5 backend object */
217518  Fts5SegIter *pIter,             /* Iterator to advance */
217519  int *pbNewTerm                  /* OUT: Set for new term */
217520){
217521  int iOff;
217522
217523  assert( p->rc==SQLITE_OK );
217524  assert( (pIter->flags & FTS5_SEGITER_REVERSE)==0 );
217525  assert( p->pConfig->eDetail==FTS5_DETAIL_NONE );
217526
217527  ASSERT_SZLEAF_OK(pIter->pLeaf);
217528  iOff = pIter->iLeafOffset;
217529
217530  /* Next entry is on the next page */
217531  if( pIter->pSeg && iOff>=pIter->pLeaf->szLeaf ){
217532    fts5SegIterNextPage(p, pIter);
217533    if( p->rc || pIter->pLeaf==0 ) return;
217534    pIter->iRowid = 0;
217535    iOff = 4;
217536  }
217537
217538  if( iOff<pIter->iEndofDoclist ){
217539    /* Next entry is on the current page */
217540    i64 iDelta;
217541    iOff += sqlite3Fts5GetVarint(&pIter->pLeaf->p[iOff], (u64*)&iDelta);
217542    pIter->iLeafOffset = iOff;
217543    pIter->iRowid += iDelta;
217544  }else if( (pIter->flags & FTS5_SEGITER_ONETERM)==0 ){
217545    if( pIter->pSeg ){
217546      int nKeep = 0;
217547      if( iOff!=fts5LeafFirstTermOff(pIter->pLeaf) ){
217548        iOff += fts5GetVarint32(&pIter->pLeaf->p[iOff], nKeep);
217549      }
217550      pIter->iLeafOffset = iOff;
217551      fts5SegIterLoadTerm(p, pIter, nKeep);
217552    }else{
217553      const u8 *pList = 0;
217554      const char *zTerm = 0;
217555      int nList;
217556      sqlite3Fts5HashScanNext(p->pHash);
217557      sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &pList, &nList);
217558      if( pList==0 ) goto next_none_eof;
217559      pIter->pLeaf->p = (u8*)pList;
217560      pIter->pLeaf->nn = nList;
217561      pIter->pLeaf->szLeaf = nList;
217562      pIter->iEndofDoclist = nList;
217563      sqlite3Fts5BufferSet(&p->rc,&pIter->term, (int)strlen(zTerm), (u8*)zTerm);
217564      pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
217565    }
217566
217567    if( pbNewTerm ) *pbNewTerm = 1;
217568  }else{
217569    goto next_none_eof;
217570  }
217571
217572  fts5SegIterLoadNPos(p, pIter);
217573
217574  return;
217575 next_none_eof:
217576  fts5DataRelease(pIter->pLeaf);
217577  pIter->pLeaf = 0;
217578}
217579
217580
217581/*
217582** Advance iterator pIter to the next entry.
217583**
217584** If an error occurs, Fts5Index.rc is set to an appropriate error code. It
217585** is not considered an error if the iterator reaches EOF. If an error has
217586** already occurred when this function is called, it is a no-op.
217587*/
217588static void fts5SegIterNext(
217589  Fts5Index *p,                   /* FTS5 backend object */
217590  Fts5SegIter *pIter,             /* Iterator to advance */
217591  int *pbNewTerm                  /* OUT: Set for new term */
217592){
217593  Fts5Data *pLeaf = pIter->pLeaf;
217594  int iOff;
217595  int bNewTerm = 0;
217596  int nKeep = 0;
217597  u8 *a;
217598  int n;
217599
217600  assert( pbNewTerm==0 || *pbNewTerm==0 );
217601  assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
217602
217603  /* Search for the end of the position list within the current page. */
217604  a = pLeaf->p;
217605  n = pLeaf->szLeaf;
217606
217607  ASSERT_SZLEAF_OK(pLeaf);
217608  iOff = pIter->iLeafOffset + pIter->nPos;
217609
217610  if( iOff<n ){
217611    /* The next entry is on the current page. */
217612    assert_nc( iOff<=pIter->iEndofDoclist );
217613    if( iOff>=pIter->iEndofDoclist ){
217614      bNewTerm = 1;
217615      if( iOff!=fts5LeafFirstTermOff(pLeaf) ){
217616        iOff += fts5GetVarint32(&a[iOff], nKeep);
217617      }
217618    }else{
217619      u64 iDelta;
217620      iOff += sqlite3Fts5GetVarint(&a[iOff], &iDelta);
217621      pIter->iRowid += iDelta;
217622      assert_nc( iDelta>0 );
217623    }
217624    pIter->iLeafOffset = iOff;
217625
217626  }else if( pIter->pSeg==0 ){
217627    const u8 *pList = 0;
217628    const char *zTerm = 0;
217629    int nList = 0;
217630    assert( (pIter->flags & FTS5_SEGITER_ONETERM) || pbNewTerm );
217631    if( 0==(pIter->flags & FTS5_SEGITER_ONETERM) ){
217632      sqlite3Fts5HashScanNext(p->pHash);
217633      sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &pList, &nList);
217634    }
217635    if( pList==0 ){
217636      fts5DataRelease(pIter->pLeaf);
217637      pIter->pLeaf = 0;
217638    }else{
217639      pIter->pLeaf->p = (u8*)pList;
217640      pIter->pLeaf->nn = nList;
217641      pIter->pLeaf->szLeaf = nList;
217642      pIter->iEndofDoclist = nList+1;
217643      sqlite3Fts5BufferSet(&p->rc, &pIter->term, (int)strlen(zTerm),
217644          (u8*)zTerm);
217645      pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
217646      *pbNewTerm = 1;
217647    }
217648  }else{
217649    iOff = 0;
217650    /* Next entry is not on the current page */
217651    while( iOff==0 ){
217652      fts5SegIterNextPage(p, pIter);
217653      pLeaf = pIter->pLeaf;
217654      if( pLeaf==0 ) break;
217655      ASSERT_SZLEAF_OK(pLeaf);
217656      if( (iOff = fts5LeafFirstRowidOff(pLeaf)) && iOff<pLeaf->szLeaf ){
217657        iOff += sqlite3Fts5GetVarint(&pLeaf->p[iOff], (u64*)&pIter->iRowid);
217658        pIter->iLeafOffset = iOff;
217659
217660        if( pLeaf->nn>pLeaf->szLeaf ){
217661          pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
217662              &pLeaf->p[pLeaf->szLeaf], pIter->iEndofDoclist
217663          );
217664        }
217665      }
217666      else if( pLeaf->nn>pLeaf->szLeaf ){
217667        pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
217668            &pLeaf->p[pLeaf->szLeaf], iOff
217669        );
217670        pIter->iLeafOffset = iOff;
217671        pIter->iEndofDoclist = iOff;
217672        bNewTerm = 1;
217673      }
217674      assert_nc( iOff<pLeaf->szLeaf );
217675      if( iOff>pLeaf->szLeaf ){
217676        p->rc = FTS5_CORRUPT;
217677        return;
217678      }
217679    }
217680  }
217681
217682  /* Check if the iterator is now at EOF. If so, return early. */
217683  if( pIter->pLeaf ){
217684    if( bNewTerm ){
217685      if( pIter->flags & FTS5_SEGITER_ONETERM ){
217686        fts5DataRelease(pIter->pLeaf);
217687        pIter->pLeaf = 0;
217688      }else{
217689        fts5SegIterLoadTerm(p, pIter, nKeep);
217690        fts5SegIterLoadNPos(p, pIter);
217691        if( pbNewTerm ) *pbNewTerm = 1;
217692      }
217693    }else{
217694      /* The following could be done by calling fts5SegIterLoadNPos(). But
217695      ** this block is particularly performance critical, so equivalent
217696      ** code is inlined.
217697      **
217698      ** Later: Switched back to fts5SegIterLoadNPos() because it supports
217699      ** detail=none mode. Not ideal.
217700      */
217701      int nSz;
217702      assert( p->rc==SQLITE_OK );
217703      assert( pIter->iLeafOffset<=pIter->pLeaf->nn );
217704      fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz);
217705      pIter->bDel = (nSz & 0x0001);
217706      pIter->nPos = nSz>>1;
217707      assert_nc( pIter->nPos>=0 );
217708    }
217709  }
217710}
217711
217712#define SWAPVAL(T, a, b) { T tmp; tmp=a; a=b; b=tmp; }
217713
217714#define fts5IndexSkipVarint(a, iOff) {            \
217715  int iEnd = iOff+9;                              \
217716  while( (a[iOff++] & 0x80) && iOff<iEnd );       \
217717}
217718
217719/*
217720** Iterator pIter currently points to the first rowid in a doclist. This
217721** function sets the iterator up so that iterates in reverse order through
217722** the doclist.
217723*/
217724static void fts5SegIterReverse(Fts5Index *p, Fts5SegIter *pIter){
217725  Fts5DlidxIter *pDlidx = pIter->pDlidx;
217726  Fts5Data *pLast = 0;
217727  int pgnoLast = 0;
217728
217729  if( pDlidx ){
217730    int iSegid = pIter->pSeg->iSegid;
217731    pgnoLast = fts5DlidxIterPgno(pDlidx);
217732    pLast = fts5DataRead(p, FTS5_SEGMENT_ROWID(iSegid, pgnoLast));
217733  }else{
217734    Fts5Data *pLeaf = pIter->pLeaf;         /* Current leaf data */
217735
217736    /* Currently, Fts5SegIter.iLeafOffset points to the first byte of
217737    ** position-list content for the current rowid. Back it up so that it
217738    ** points to the start of the position-list size field. */
217739    int iPoslist;
217740    if( pIter->iTermLeafPgno==pIter->iLeafPgno ){
217741      iPoslist = pIter->iTermLeafOffset;
217742    }else{
217743      iPoslist = 4;
217744    }
217745    fts5IndexSkipVarint(pLeaf->p, iPoslist);
217746    pIter->iLeafOffset = iPoslist;
217747
217748    /* If this condition is true then the largest rowid for the current
217749    ** term may not be stored on the current page. So search forward to
217750    ** see where said rowid really is.  */
217751    if( pIter->iEndofDoclist>=pLeaf->szLeaf ){
217752      int pgno;
217753      Fts5StructureSegment *pSeg = pIter->pSeg;
217754
217755      /* The last rowid in the doclist may not be on the current page. Search
217756      ** forward to find the page containing the last rowid.  */
217757      for(pgno=pIter->iLeafPgno+1; !p->rc && pgno<=pSeg->pgnoLast; pgno++){
217758        i64 iAbs = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno);
217759        Fts5Data *pNew = fts5DataRead(p, iAbs);
217760        if( pNew ){
217761          int iRowid, bTermless;
217762          iRowid = fts5LeafFirstRowidOff(pNew);
217763          bTermless = fts5LeafIsTermless(pNew);
217764          if( iRowid ){
217765            SWAPVAL(Fts5Data*, pNew, pLast);
217766            pgnoLast = pgno;
217767          }
217768          fts5DataRelease(pNew);
217769          if( bTermless==0 ) break;
217770        }
217771      }
217772    }
217773  }
217774
217775  /* If pLast is NULL at this point, then the last rowid for this doclist
217776  ** lies on the page currently indicated by the iterator. In this case
217777  ** pIter->iLeafOffset is already set to point to the position-list size
217778  ** field associated with the first relevant rowid on the page.
217779  **
217780  ** Or, if pLast is non-NULL, then it is the page that contains the last
217781  ** rowid. In this case configure the iterator so that it points to the
217782  ** first rowid on this page.
217783  */
217784  if( pLast ){
217785    int iOff;
217786    fts5DataRelease(pIter->pLeaf);
217787    pIter->pLeaf = pLast;
217788    pIter->iLeafPgno = pgnoLast;
217789    iOff = fts5LeafFirstRowidOff(pLast);
217790    iOff += fts5GetVarint(&pLast->p[iOff], (u64*)&pIter->iRowid);
217791    pIter->iLeafOffset = iOff;
217792
217793    if( fts5LeafIsTermless(pLast) ){
217794      pIter->iEndofDoclist = pLast->nn+1;
217795    }else{
217796      pIter->iEndofDoclist = fts5LeafFirstTermOff(pLast);
217797    }
217798
217799  }
217800
217801  fts5SegIterReverseInitPage(p, pIter);
217802}
217803
217804/*
217805** Iterator pIter currently points to the first rowid of a doclist.
217806** There is a doclist-index associated with the final term on the current
217807** page. If the current term is the last term on the page, load the
217808** doclist-index from disk and initialize an iterator at (pIter->pDlidx).
217809*/
217810static void fts5SegIterLoadDlidx(Fts5Index *p, Fts5SegIter *pIter){
217811  int iSeg = pIter->pSeg->iSegid;
217812  int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
217813  Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
217814
217815  assert( pIter->flags & FTS5_SEGITER_ONETERM );
217816  assert( pIter->pDlidx==0 );
217817
217818  /* Check if the current doclist ends on this page. If it does, return
217819  ** early without loading the doclist-index (as it belongs to a different
217820  ** term. */
217821  if( pIter->iTermLeafPgno==pIter->iLeafPgno
217822   && pIter->iEndofDoclist<pLeaf->szLeaf
217823  ){
217824    return;
217825  }
217826
217827  pIter->pDlidx = fts5DlidxIterInit(p, bRev, iSeg, pIter->iTermLeafPgno);
217828}
217829
217830/*
217831** The iterator object passed as the second argument currently contains
217832** no valid values except for the Fts5SegIter.pLeaf member variable. This
217833** function searches the leaf page for a term matching (pTerm/nTerm).
217834**
217835** If the specified term is found on the page, then the iterator is left
217836** pointing to it. If argument bGe is zero and the term is not found,
217837** the iterator is left pointing at EOF.
217838**
217839** If bGe is non-zero and the specified term is not found, then the
217840** iterator is left pointing to the smallest term in the segment that
217841** is larger than the specified term, even if this term is not on the
217842** current page.
217843*/
217844static void fts5LeafSeek(
217845  Fts5Index *p,                   /* Leave any error code here */
217846  int bGe,                        /* True for a >= search */
217847  Fts5SegIter *pIter,             /* Iterator to seek */
217848  const u8 *pTerm, int nTerm      /* Term to search for */
217849){
217850  int iOff;
217851  const u8 *a = pIter->pLeaf->p;
217852  int szLeaf = pIter->pLeaf->szLeaf;
217853  int n = pIter->pLeaf->nn;
217854
217855  u32 nMatch = 0;
217856  u32 nKeep = 0;
217857  u32 nNew = 0;
217858  u32 iTermOff;
217859  int iPgidx;                     /* Current offset in pgidx */
217860  int bEndOfPage = 0;
217861
217862  assert( p->rc==SQLITE_OK );
217863
217864  iPgidx = szLeaf;
217865  iPgidx += fts5GetVarint32(&a[iPgidx], iTermOff);
217866  iOff = iTermOff;
217867  if( iOff>n ){
217868    p->rc = FTS5_CORRUPT;
217869    return;
217870  }
217871
217872  while( 1 ){
217873
217874    /* Figure out how many new bytes are in this term */
217875    fts5FastGetVarint32(a, iOff, nNew);
217876    if( nKeep<nMatch ){
217877      goto search_failed;
217878    }
217879
217880    assert( nKeep>=nMatch );
217881    if( nKeep==nMatch ){
217882      u32 nCmp;
217883      u32 i;
217884      nCmp = (u32)MIN(nNew, nTerm-nMatch);
217885      for(i=0; i<nCmp; i++){
217886        if( a[iOff+i]!=pTerm[nMatch+i] ) break;
217887      }
217888      nMatch += i;
217889
217890      if( (u32)nTerm==nMatch ){
217891        if( i==nNew ){
217892          goto search_success;
217893        }else{
217894          goto search_failed;
217895        }
217896      }else if( i<nNew && a[iOff+i]>pTerm[nMatch] ){
217897        goto search_failed;
217898      }
217899    }
217900
217901    if( iPgidx>=n ){
217902      bEndOfPage = 1;
217903      break;
217904    }
217905
217906    iPgidx += fts5GetVarint32(&a[iPgidx], nKeep);
217907    iTermOff += nKeep;
217908    iOff = iTermOff;
217909
217910    if( iOff>=n ){
217911      p->rc = FTS5_CORRUPT;
217912      return;
217913    }
217914
217915    /* Read the nKeep field of the next term. */
217916    fts5FastGetVarint32(a, iOff, nKeep);
217917  }
217918
217919 search_failed:
217920  if( bGe==0 ){
217921    fts5DataRelease(pIter->pLeaf);
217922    pIter->pLeaf = 0;
217923    return;
217924  }else if( bEndOfPage ){
217925    do {
217926      fts5SegIterNextPage(p, pIter);
217927      if( pIter->pLeaf==0 ) return;
217928      a = pIter->pLeaf->p;
217929      if( fts5LeafIsTermless(pIter->pLeaf)==0 ){
217930        iPgidx = pIter->pLeaf->szLeaf;
217931        iPgidx += fts5GetVarint32(&pIter->pLeaf->p[iPgidx], iOff);
217932        if( iOff<4 || iOff>=pIter->pLeaf->szLeaf ){
217933          p->rc = FTS5_CORRUPT;
217934          return;
217935        }else{
217936          nKeep = 0;
217937          iTermOff = iOff;
217938          n = pIter->pLeaf->nn;
217939          iOff += fts5GetVarint32(&a[iOff], nNew);
217940          break;
217941        }
217942      }
217943    }while( 1 );
217944  }
217945
217946 search_success:
217947  pIter->iLeafOffset = iOff + nNew;
217948  if( pIter->iLeafOffset>n || nNew<1 ){
217949    p->rc = FTS5_CORRUPT;
217950    return;
217951  }
217952  pIter->iTermLeafOffset = pIter->iLeafOffset;
217953  pIter->iTermLeafPgno = pIter->iLeafPgno;
217954
217955  fts5BufferSet(&p->rc, &pIter->term, nKeep, pTerm);
217956  fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
217957
217958  if( iPgidx>=n ){
217959    pIter->iEndofDoclist = pIter->pLeaf->nn+1;
217960  }else{
217961    int nExtra;
217962    iPgidx += fts5GetVarint32(&a[iPgidx], nExtra);
217963    pIter->iEndofDoclist = iTermOff + nExtra;
217964  }
217965  pIter->iPgidxOff = iPgidx;
217966
217967  fts5SegIterLoadRowid(p, pIter);
217968  fts5SegIterLoadNPos(p, pIter);
217969}
217970
217971static sqlite3_stmt *fts5IdxSelectStmt(Fts5Index *p){
217972  if( p->pIdxSelect==0 ){
217973    Fts5Config *pConfig = p->pConfig;
217974    fts5IndexPrepareStmt(p, &p->pIdxSelect, sqlite3_mprintf(
217975          "SELECT pgno FROM '%q'.'%q_idx' WHERE "
217976          "segid=? AND term<=? ORDER BY term DESC LIMIT 1",
217977          pConfig->zDb, pConfig->zName
217978    ));
217979  }
217980  return p->pIdxSelect;
217981}
217982
217983/*
217984** Initialize the object pIter to point to term pTerm/nTerm within segment
217985** pSeg. If there is no such term in the index, the iterator is set to EOF.
217986**
217987** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
217988** an error has already occurred when this function is called, it is a no-op.
217989*/
217990static void fts5SegIterSeekInit(
217991  Fts5Index *p,                   /* FTS5 backend */
217992  const u8 *pTerm, int nTerm,     /* Term to seek to */
217993  int flags,                      /* Mask of FTS5INDEX_XXX flags */
217994  Fts5StructureSegment *pSeg,     /* Description of segment */
217995  Fts5SegIter *pIter              /* Object to populate */
217996){
217997  int iPg = 1;
217998  int bGe = (flags & FTS5INDEX_QUERY_SCAN);
217999  int bDlidx = 0;                 /* True if there is a doclist-index */
218000  sqlite3_stmt *pIdxSelect = 0;
218001
218002  assert( bGe==0 || (flags & FTS5INDEX_QUERY_DESC)==0 );
218003  assert( pTerm && nTerm );
218004  memset(pIter, 0, sizeof(*pIter));
218005  pIter->pSeg = pSeg;
218006
218007  /* This block sets stack variable iPg to the leaf page number that may
218008  ** contain term (pTerm/nTerm), if it is present in the segment. */
218009  pIdxSelect = fts5IdxSelectStmt(p);
218010  if( p->rc ) return;
218011  sqlite3_bind_int(pIdxSelect, 1, pSeg->iSegid);
218012  sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC);
218013  if( SQLITE_ROW==sqlite3_step(pIdxSelect) ){
218014    i64 val = sqlite3_column_int(pIdxSelect, 0);
218015    iPg = (int)(val>>1);
218016    bDlidx = (val & 0x0001);
218017  }
218018  p->rc = sqlite3_reset(pIdxSelect);
218019  sqlite3_bind_null(pIdxSelect, 2);
218020
218021  if( iPg<pSeg->pgnoFirst ){
218022    iPg = pSeg->pgnoFirst;
218023    bDlidx = 0;
218024  }
218025
218026  pIter->iLeafPgno = iPg - 1;
218027  fts5SegIterNextPage(p, pIter);
218028
218029  if( pIter->pLeaf ){
218030    fts5LeafSeek(p, bGe, pIter, pTerm, nTerm);
218031  }
218032
218033  if( p->rc==SQLITE_OK && bGe==0 ){
218034    pIter->flags |= FTS5_SEGITER_ONETERM;
218035    if( pIter->pLeaf ){
218036      if( flags & FTS5INDEX_QUERY_DESC ){
218037        pIter->flags |= FTS5_SEGITER_REVERSE;
218038      }
218039      if( bDlidx ){
218040        fts5SegIterLoadDlidx(p, pIter);
218041      }
218042      if( flags & FTS5INDEX_QUERY_DESC ){
218043        fts5SegIterReverse(p, pIter);
218044      }
218045    }
218046  }
218047
218048  fts5SegIterSetNext(p, pIter);
218049
218050  /* Either:
218051  **
218052  **   1) an error has occurred, or
218053  **   2) the iterator points to EOF, or
218054  **   3) the iterator points to an entry with term (pTerm/nTerm), or
218055  **   4) the FTS5INDEX_QUERY_SCAN flag was set and the iterator points
218056  **      to an entry with a term greater than or equal to (pTerm/nTerm).
218057  */
218058  assert_nc( p->rc!=SQLITE_OK                                       /* 1 */
218059   || pIter->pLeaf==0                                               /* 2 */
218060   || fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)==0          /* 3 */
218061   || (bGe && fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)>0)  /* 4 */
218062  );
218063}
218064
218065/*
218066** Initialize the object pIter to point to term pTerm/nTerm within the
218067** in-memory hash table. If there is no such term in the hash-table, the
218068** iterator is set to EOF.
218069**
218070** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
218071** an error has already occurred when this function is called, it is a no-op.
218072*/
218073static void fts5SegIterHashInit(
218074  Fts5Index *p,                   /* FTS5 backend */
218075  const u8 *pTerm, int nTerm,     /* Term to seek to */
218076  int flags,                      /* Mask of FTS5INDEX_XXX flags */
218077  Fts5SegIter *pIter              /* Object to populate */
218078){
218079  int nList = 0;
218080  const u8 *z = 0;
218081  int n = 0;
218082  Fts5Data *pLeaf = 0;
218083
218084  assert( p->pHash );
218085  assert( p->rc==SQLITE_OK );
218086
218087  if( pTerm==0 || (flags & FTS5INDEX_QUERY_SCAN) ){
218088    const u8 *pList = 0;
218089
218090    p->rc = sqlite3Fts5HashScanInit(p->pHash, (const char*)pTerm, nTerm);
218091    sqlite3Fts5HashScanEntry(p->pHash, (const char**)&z, &pList, &nList);
218092    n = (z ? (int)strlen((const char*)z) : 0);
218093    if( pList ){
218094      pLeaf = fts5IdxMalloc(p, sizeof(Fts5Data));
218095      if( pLeaf ){
218096        pLeaf->p = (u8*)pList;
218097      }
218098    }
218099  }else{
218100    p->rc = sqlite3Fts5HashQuery(p->pHash, sizeof(Fts5Data),
218101        (const char*)pTerm, nTerm, (void**)&pLeaf, &nList
218102    );
218103    if( pLeaf ){
218104      pLeaf->p = (u8*)&pLeaf[1];
218105    }
218106    z = pTerm;
218107    n = nTerm;
218108    pIter->flags |= FTS5_SEGITER_ONETERM;
218109  }
218110
218111  if( pLeaf ){
218112    sqlite3Fts5BufferSet(&p->rc, &pIter->term, n, z);
218113    pLeaf->nn = pLeaf->szLeaf = nList;
218114    pIter->pLeaf = pLeaf;
218115    pIter->iLeafOffset = fts5GetVarint(pLeaf->p, (u64*)&pIter->iRowid);
218116    pIter->iEndofDoclist = pLeaf->nn;
218117
218118    if( flags & FTS5INDEX_QUERY_DESC ){
218119      pIter->flags |= FTS5_SEGITER_REVERSE;
218120      fts5SegIterReverseInitPage(p, pIter);
218121    }else{
218122      fts5SegIterLoadNPos(p, pIter);
218123    }
218124  }
218125
218126  fts5SegIterSetNext(p, pIter);
218127}
218128
218129/*
218130** Zero the iterator passed as the only argument.
218131*/
218132static void fts5SegIterClear(Fts5SegIter *pIter){
218133  fts5BufferFree(&pIter->term);
218134  fts5DataRelease(pIter->pLeaf);
218135  fts5DataRelease(pIter->pNextLeaf);
218136  fts5DlidxIterFree(pIter->pDlidx);
218137  sqlite3_free(pIter->aRowidOffset);
218138  memset(pIter, 0, sizeof(Fts5SegIter));
218139}
218140
218141#ifdef SQLITE_DEBUG
218142
218143/*
218144** This function is used as part of the big assert() procedure implemented by
218145** fts5AssertMultiIterSetup(). It ensures that the result currently stored
218146** in *pRes is the correct result of comparing the current positions of the
218147** two iterators.
218148*/
218149static void fts5AssertComparisonResult(
218150  Fts5Iter *pIter,
218151  Fts5SegIter *p1,
218152  Fts5SegIter *p2,
218153  Fts5CResult *pRes
218154){
218155  int i1 = p1 - pIter->aSeg;
218156  int i2 = p2 - pIter->aSeg;
218157
218158  if( p1->pLeaf || p2->pLeaf ){
218159    if( p1->pLeaf==0 ){
218160      assert( pRes->iFirst==i2 );
218161    }else if( p2->pLeaf==0 ){
218162      assert( pRes->iFirst==i1 );
218163    }else{
218164      int nMin = MIN(p1->term.n, p2->term.n);
218165      int res = fts5Memcmp(p1->term.p, p2->term.p, nMin);
218166      if( res==0 ) res = p1->term.n - p2->term.n;
218167
218168      if( res==0 ){
218169        assert( pRes->bTermEq==1 );
218170        assert( p1->iRowid!=p2->iRowid );
218171        res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : 1;
218172      }else{
218173        assert( pRes->bTermEq==0 );
218174      }
218175
218176      if( res<0 ){
218177        assert( pRes->iFirst==i1 );
218178      }else{
218179        assert( pRes->iFirst==i2 );
218180      }
218181    }
218182  }
218183}
218184
218185/*
218186** This function is a no-op unless SQLITE_DEBUG is defined when this module
218187** is compiled. In that case, this function is essentially an assert()
218188** statement used to verify that the contents of the pIter->aFirst[] array
218189** are correct.
218190*/
218191static void fts5AssertMultiIterSetup(Fts5Index *p, Fts5Iter *pIter){
218192  if( p->rc==SQLITE_OK ){
218193    Fts5SegIter *pFirst = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
218194    int i;
218195
218196    assert( (pFirst->pLeaf==0)==pIter->base.bEof );
218197
218198    /* Check that pIter->iSwitchRowid is set correctly. */
218199    for(i=0; i<pIter->nSeg; i++){
218200      Fts5SegIter *p1 = &pIter->aSeg[i];
218201      assert( p1==pFirst
218202           || p1->pLeaf==0
218203           || fts5BufferCompare(&pFirst->term, &p1->term)
218204           || p1->iRowid==pIter->iSwitchRowid
218205           || (p1->iRowid<pIter->iSwitchRowid)==pIter->bRev
218206      );
218207    }
218208
218209    for(i=0; i<pIter->nSeg; i+=2){
218210      Fts5SegIter *p1 = &pIter->aSeg[i];
218211      Fts5SegIter *p2 = &pIter->aSeg[i+1];
218212      Fts5CResult *pRes = &pIter->aFirst[(pIter->nSeg + i) / 2];
218213      fts5AssertComparisonResult(pIter, p1, p2, pRes);
218214    }
218215
218216    for(i=1; i<(pIter->nSeg / 2); i+=2){
218217      Fts5SegIter *p1 = &pIter->aSeg[ pIter->aFirst[i*2].iFirst ];
218218      Fts5SegIter *p2 = &pIter->aSeg[ pIter->aFirst[i*2+1].iFirst ];
218219      Fts5CResult *pRes = &pIter->aFirst[i];
218220      fts5AssertComparisonResult(pIter, p1, p2, pRes);
218221    }
218222  }
218223}
218224#else
218225# define fts5AssertMultiIterSetup(x,y)
218226#endif
218227
218228/*
218229** Do the comparison necessary to populate pIter->aFirst[iOut].
218230**
218231** If the returned value is non-zero, then it is the index of an entry
218232** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing
218233** to a key that is a duplicate of another, higher priority,
218234** segment-iterator in the pSeg->aSeg[] array.
218235*/
218236static int fts5MultiIterDoCompare(Fts5Iter *pIter, int iOut){
218237  int i1;                         /* Index of left-hand Fts5SegIter */
218238  int i2;                         /* Index of right-hand Fts5SegIter */
218239  int iRes;
218240  Fts5SegIter *p1;                /* Left-hand Fts5SegIter */
218241  Fts5SegIter *p2;                /* Right-hand Fts5SegIter */
218242  Fts5CResult *pRes = &pIter->aFirst[iOut];
218243
218244  assert( iOut<pIter->nSeg && iOut>0 );
218245  assert( pIter->bRev==0 || pIter->bRev==1 );
218246
218247  if( iOut>=(pIter->nSeg/2) ){
218248    i1 = (iOut - pIter->nSeg/2) * 2;
218249    i2 = i1 + 1;
218250  }else{
218251    i1 = pIter->aFirst[iOut*2].iFirst;
218252    i2 = pIter->aFirst[iOut*2+1].iFirst;
218253  }
218254  p1 = &pIter->aSeg[i1];
218255  p2 = &pIter->aSeg[i2];
218256
218257  pRes->bTermEq = 0;
218258  if( p1->pLeaf==0 ){           /* If p1 is at EOF */
218259    iRes = i2;
218260  }else if( p2->pLeaf==0 ){     /* If p2 is at EOF */
218261    iRes = i1;
218262  }else{
218263    int res = fts5BufferCompare(&p1->term, &p2->term);
218264    if( res==0 ){
218265      assert_nc( i2>i1 );
218266      assert_nc( i2!=0 );
218267      pRes->bTermEq = 1;
218268      if( p1->iRowid==p2->iRowid ){
218269        p1->bDel = p2->bDel;
218270        return i2;
218271      }
218272      res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : +1;
218273    }
218274    assert( res!=0 );
218275    if( res<0 ){
218276      iRes = i1;
218277    }else{
218278      iRes = i2;
218279    }
218280  }
218281
218282  pRes->iFirst = (u16)iRes;
218283  return 0;
218284}
218285
218286/*
218287** Move the seg-iter so that it points to the first rowid on page iLeafPgno.
218288** It is an error if leaf iLeafPgno does not exist or contains no rowids.
218289*/
218290static void fts5SegIterGotoPage(
218291  Fts5Index *p,                   /* FTS5 backend object */
218292  Fts5SegIter *pIter,             /* Iterator to advance */
218293  int iLeafPgno
218294){
218295  assert( iLeafPgno>pIter->iLeafPgno );
218296
218297  if( iLeafPgno>pIter->pSeg->pgnoLast ){
218298    p->rc = FTS5_CORRUPT;
218299  }else{
218300    fts5DataRelease(pIter->pNextLeaf);
218301    pIter->pNextLeaf = 0;
218302    pIter->iLeafPgno = iLeafPgno-1;
218303    fts5SegIterNextPage(p, pIter);
218304    assert( p->rc!=SQLITE_OK || pIter->iLeafPgno==iLeafPgno );
218305
218306    if( p->rc==SQLITE_OK ){
218307      int iOff;
218308      u8 *a = pIter->pLeaf->p;
218309      int n = pIter->pLeaf->szLeaf;
218310
218311      iOff = fts5LeafFirstRowidOff(pIter->pLeaf);
218312      if( iOff<4 || iOff>=n ){
218313        p->rc = FTS5_CORRUPT;
218314      }else{
218315        iOff += fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
218316        pIter->iLeafOffset = iOff;
218317        fts5SegIterLoadNPos(p, pIter);
218318      }
218319    }
218320  }
218321}
218322
218323/*
218324** Advance the iterator passed as the second argument until it is at or
218325** past rowid iFrom. Regardless of the value of iFrom, the iterator is
218326** always advanced at least once.
218327*/
218328static void fts5SegIterNextFrom(
218329  Fts5Index *p,                   /* FTS5 backend object */
218330  Fts5SegIter *pIter,             /* Iterator to advance */
218331  i64 iMatch                      /* Advance iterator at least this far */
218332){
218333  int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
218334  Fts5DlidxIter *pDlidx = pIter->pDlidx;
218335  int iLeafPgno = pIter->iLeafPgno;
218336  int bMove = 1;
218337
218338  assert( pIter->flags & FTS5_SEGITER_ONETERM );
218339  assert( pIter->pDlidx );
218340  assert( pIter->pLeaf );
218341
218342  if( bRev==0 ){
218343    while( !fts5DlidxIterEof(p, pDlidx) && iMatch>fts5DlidxIterRowid(pDlidx) ){
218344      iLeafPgno = fts5DlidxIterPgno(pDlidx);
218345      fts5DlidxIterNext(p, pDlidx);
218346    }
218347    assert_nc( iLeafPgno>=pIter->iLeafPgno || p->rc );
218348    if( iLeafPgno>pIter->iLeafPgno ){
218349      fts5SegIterGotoPage(p, pIter, iLeafPgno);
218350      bMove = 0;
218351    }
218352  }else{
218353    assert( pIter->pNextLeaf==0 );
218354    assert( iMatch<pIter->iRowid );
218355    while( !fts5DlidxIterEof(p, pDlidx) && iMatch<fts5DlidxIterRowid(pDlidx) ){
218356      fts5DlidxIterPrev(p, pDlidx);
218357    }
218358    iLeafPgno = fts5DlidxIterPgno(pDlidx);
218359
218360    assert( fts5DlidxIterEof(p, pDlidx) || iLeafPgno<=pIter->iLeafPgno );
218361
218362    if( iLeafPgno<pIter->iLeafPgno ){
218363      pIter->iLeafPgno = iLeafPgno+1;
218364      fts5SegIterReverseNewPage(p, pIter);
218365      bMove = 0;
218366    }
218367  }
218368
218369  do{
218370    if( bMove && p->rc==SQLITE_OK ) pIter->xNext(p, pIter, 0);
218371    if( pIter->pLeaf==0 ) break;
218372    if( bRev==0 && pIter->iRowid>=iMatch ) break;
218373    if( bRev!=0 && pIter->iRowid<=iMatch ) break;
218374    bMove = 1;
218375  }while( p->rc==SQLITE_OK );
218376}
218377
218378
218379/*
218380** Free the iterator object passed as the second argument.
218381*/
218382static void fts5MultiIterFree(Fts5Iter *pIter){
218383  if( pIter ){
218384    int i;
218385    for(i=0; i<pIter->nSeg; i++){
218386      fts5SegIterClear(&pIter->aSeg[i]);
218387    }
218388    fts5BufferFree(&pIter->poslist);
218389    sqlite3_free(pIter);
218390  }
218391}
218392
218393static void fts5MultiIterAdvanced(
218394  Fts5Index *p,                   /* FTS5 backend to iterate within */
218395  Fts5Iter *pIter,                /* Iterator to update aFirst[] array for */
218396  int iChanged,                   /* Index of sub-iterator just advanced */
218397  int iMinset                     /* Minimum entry in aFirst[] to set */
218398){
218399  int i;
218400  for(i=(pIter->nSeg+iChanged)/2; i>=iMinset && p->rc==SQLITE_OK; i=i/2){
218401    int iEq;
218402    if( (iEq = fts5MultiIterDoCompare(pIter, i)) ){
218403      Fts5SegIter *pSeg = &pIter->aSeg[iEq];
218404      assert( p->rc==SQLITE_OK );
218405      pSeg->xNext(p, pSeg, 0);
218406      i = pIter->nSeg + iEq;
218407    }
218408  }
218409}
218410
218411/*
218412** Sub-iterator iChanged of iterator pIter has just been advanced. It still
218413** points to the same term though - just a different rowid. This function
218414** attempts to update the contents of the pIter->aFirst[] accordingly.
218415** If it does so successfully, 0 is returned. Otherwise 1.
218416**
218417** If non-zero is returned, the caller should call fts5MultiIterAdvanced()
218418** on the iterator instead. That function does the same as this one, except
218419** that it deals with more complicated cases as well.
218420*/
218421static int fts5MultiIterAdvanceRowid(
218422  Fts5Iter *pIter,                /* Iterator to update aFirst[] array for */
218423  int iChanged,                   /* Index of sub-iterator just advanced */
218424  Fts5SegIter **ppFirst
218425){
218426  Fts5SegIter *pNew = &pIter->aSeg[iChanged];
218427
218428  if( pNew->iRowid==pIter->iSwitchRowid
218429   || (pNew->iRowid<pIter->iSwitchRowid)==pIter->bRev
218430  ){
218431    int i;
218432    Fts5SegIter *pOther = &pIter->aSeg[iChanged ^ 0x0001];
218433    pIter->iSwitchRowid = pIter->bRev ? SMALLEST_INT64 : LARGEST_INT64;
218434    for(i=(pIter->nSeg+iChanged)/2; 1; i=i/2){
218435      Fts5CResult *pRes = &pIter->aFirst[i];
218436
218437      assert( pNew->pLeaf );
218438      assert( pRes->bTermEq==0 || pOther->pLeaf );
218439
218440      if( pRes->bTermEq ){
218441        if( pNew->iRowid==pOther->iRowid ){
218442          return 1;
218443        }else if( (pOther->iRowid>pNew->iRowid)==pIter->bRev ){
218444          pIter->iSwitchRowid = pOther->iRowid;
218445          pNew = pOther;
218446        }else if( (pOther->iRowid>pIter->iSwitchRowid)==pIter->bRev ){
218447          pIter->iSwitchRowid = pOther->iRowid;
218448        }
218449      }
218450      pRes->iFirst = (u16)(pNew - pIter->aSeg);
218451      if( i==1 ) break;
218452
218453      pOther = &pIter->aSeg[ pIter->aFirst[i ^ 0x0001].iFirst ];
218454    }
218455  }
218456
218457  *ppFirst = pNew;
218458  return 0;
218459}
218460
218461/*
218462** Set the pIter->bEof variable based on the state of the sub-iterators.
218463*/
218464static void fts5MultiIterSetEof(Fts5Iter *pIter){
218465  Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
218466  pIter->base.bEof = pSeg->pLeaf==0;
218467  pIter->iSwitchRowid = pSeg->iRowid;
218468}
218469
218470/*
218471** Move the iterator to the next entry.
218472**
218473** If an error occurs, an error code is left in Fts5Index.rc. It is not
218474** considered an error if the iterator reaches EOF, or if it is already at
218475** EOF when this function is called.
218476*/
218477static void fts5MultiIterNext(
218478  Fts5Index *p,
218479  Fts5Iter *pIter,
218480  int bFrom,                      /* True if argument iFrom is valid */
218481  i64 iFrom                       /* Advance at least as far as this */
218482){
218483  int bUseFrom = bFrom;
218484  assert( pIter->base.bEof==0 );
218485  while( p->rc==SQLITE_OK ){
218486    int iFirst = pIter->aFirst[1].iFirst;
218487    int bNewTerm = 0;
218488    Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
218489    assert( p->rc==SQLITE_OK );
218490    if( bUseFrom && pSeg->pDlidx ){
218491      fts5SegIterNextFrom(p, pSeg, iFrom);
218492    }else{
218493      pSeg->xNext(p, pSeg, &bNewTerm);
218494    }
218495
218496    if( pSeg->pLeaf==0 || bNewTerm
218497     || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
218498    ){
218499      fts5MultiIterAdvanced(p, pIter, iFirst, 1);
218500      fts5MultiIterSetEof(pIter);
218501      pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
218502      if( pSeg->pLeaf==0 ) return;
218503    }
218504
218505    fts5AssertMultiIterSetup(p, pIter);
218506    assert( pSeg==&pIter->aSeg[pIter->aFirst[1].iFirst] && pSeg->pLeaf );
218507    if( pIter->bSkipEmpty==0 || pSeg->nPos ){
218508      pIter->xSetOutputs(pIter, pSeg);
218509      return;
218510    }
218511    bUseFrom = 0;
218512  }
218513}
218514
218515static void fts5MultiIterNext2(
218516  Fts5Index *p,
218517  Fts5Iter *pIter,
218518  int *pbNewTerm                  /* OUT: True if *might* be new term */
218519){
218520  assert( pIter->bSkipEmpty );
218521  if( p->rc==SQLITE_OK ){
218522    *pbNewTerm = 0;
218523    do{
218524      int iFirst = pIter->aFirst[1].iFirst;
218525      Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
218526      int bNewTerm = 0;
218527
218528      assert( p->rc==SQLITE_OK );
218529      pSeg->xNext(p, pSeg, &bNewTerm);
218530      if( pSeg->pLeaf==0 || bNewTerm
218531       || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
218532      ){
218533        fts5MultiIterAdvanced(p, pIter, iFirst, 1);
218534        fts5MultiIterSetEof(pIter);
218535        *pbNewTerm = 1;
218536      }
218537      fts5AssertMultiIterSetup(p, pIter);
218538
218539    }while( fts5MultiIterIsEmpty(p, pIter) );
218540  }
218541}
218542
218543static void fts5IterSetOutputs_Noop(Fts5Iter *pUnused1, Fts5SegIter *pUnused2){
218544  UNUSED_PARAM2(pUnused1, pUnused2);
218545}
218546
218547static Fts5Iter *fts5MultiIterAlloc(
218548  Fts5Index *p,                   /* FTS5 backend to iterate within */
218549  int nSeg
218550){
218551  Fts5Iter *pNew;
218552  int nSlot;                      /* Power of two >= nSeg */
218553
218554  for(nSlot=2; nSlot<nSeg; nSlot=nSlot*2);
218555  pNew = fts5IdxMalloc(p,
218556      sizeof(Fts5Iter) +                  /* pNew */
218557      sizeof(Fts5SegIter) * (nSlot-1) +   /* pNew->aSeg[] */
218558      sizeof(Fts5CResult) * nSlot         /* pNew->aFirst[] */
218559  );
218560  if( pNew ){
218561    pNew->nSeg = nSlot;
218562    pNew->aFirst = (Fts5CResult*)&pNew->aSeg[nSlot];
218563    pNew->pIndex = p;
218564    pNew->xSetOutputs = fts5IterSetOutputs_Noop;
218565  }
218566  return pNew;
218567}
218568
218569static void fts5PoslistCallback(
218570  Fts5Index *pUnused,
218571  void *pContext,
218572  const u8 *pChunk, int nChunk
218573){
218574  UNUSED_PARAM(pUnused);
218575  assert_nc( nChunk>=0 );
218576  if( nChunk>0 ){
218577    fts5BufferSafeAppendBlob((Fts5Buffer*)pContext, pChunk, nChunk);
218578  }
218579}
218580
218581typedef struct PoslistCallbackCtx PoslistCallbackCtx;
218582struct PoslistCallbackCtx {
218583  Fts5Buffer *pBuf;               /* Append to this buffer */
218584  Fts5Colset *pColset;            /* Restrict matches to this column */
218585  int eState;                     /* See above */
218586};
218587
218588typedef struct PoslistOffsetsCtx PoslistOffsetsCtx;
218589struct PoslistOffsetsCtx {
218590  Fts5Buffer *pBuf;               /* Append to this buffer */
218591  Fts5Colset *pColset;            /* Restrict matches to this column */
218592  int iRead;
218593  int iWrite;
218594};
218595
218596/*
218597** TODO: Make this more efficient!
218598*/
218599static int fts5IndexColsetTest(Fts5Colset *pColset, int iCol){
218600  int i;
218601  for(i=0; i<pColset->nCol; i++){
218602    if( pColset->aiCol[i]==iCol ) return 1;
218603  }
218604  return 0;
218605}
218606
218607static void fts5PoslistOffsetsCallback(
218608  Fts5Index *pUnused,
218609  void *pContext,
218610  const u8 *pChunk, int nChunk
218611){
218612  PoslistOffsetsCtx *pCtx = (PoslistOffsetsCtx*)pContext;
218613  UNUSED_PARAM(pUnused);
218614  assert_nc( nChunk>=0 );
218615  if( nChunk>0 ){
218616    int i = 0;
218617    while( i<nChunk ){
218618      int iVal;
218619      i += fts5GetVarint32(&pChunk[i], iVal);
218620      iVal += pCtx->iRead - 2;
218621      pCtx->iRead = iVal;
218622      if( fts5IndexColsetTest(pCtx->pColset, iVal) ){
218623        fts5BufferSafeAppendVarint(pCtx->pBuf, iVal + 2 - pCtx->iWrite);
218624        pCtx->iWrite = iVal;
218625      }
218626    }
218627  }
218628}
218629
218630static void fts5PoslistFilterCallback(
218631  Fts5Index *pUnused,
218632  void *pContext,
218633  const u8 *pChunk, int nChunk
218634){
218635  PoslistCallbackCtx *pCtx = (PoslistCallbackCtx*)pContext;
218636  UNUSED_PARAM(pUnused);
218637  assert_nc( nChunk>=0 );
218638  if( nChunk>0 ){
218639    /* Search through to find the first varint with value 1. This is the
218640    ** start of the next columns hits. */
218641    int i = 0;
218642    int iStart = 0;
218643
218644    if( pCtx->eState==2 ){
218645      int iCol;
218646      fts5FastGetVarint32(pChunk, i, iCol);
218647      if( fts5IndexColsetTest(pCtx->pColset, iCol) ){
218648        pCtx->eState = 1;
218649        fts5BufferSafeAppendVarint(pCtx->pBuf, 1);
218650      }else{
218651        pCtx->eState = 0;
218652      }
218653    }
218654
218655    do {
218656      while( i<nChunk && pChunk[i]!=0x01 ){
218657        while( pChunk[i] & 0x80 ) i++;
218658        i++;
218659      }
218660      if( pCtx->eState ){
218661        fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
218662      }
218663      if( i<nChunk ){
218664        int iCol;
218665        iStart = i;
218666        i++;
218667        if( i>=nChunk ){
218668          pCtx->eState = 2;
218669        }else{
218670          fts5FastGetVarint32(pChunk, i, iCol);
218671          pCtx->eState = fts5IndexColsetTest(pCtx->pColset, iCol);
218672          if( pCtx->eState ){
218673            fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
218674            iStart = i;
218675          }
218676        }
218677      }
218678    }while( i<nChunk );
218679  }
218680}
218681
218682static void fts5ChunkIterate(
218683  Fts5Index *p,                   /* Index object */
218684  Fts5SegIter *pSeg,              /* Poslist of this iterator */
218685  void *pCtx,                     /* Context pointer for xChunk callback */
218686  void (*xChunk)(Fts5Index*, void*, const u8*, int)
218687){
218688  int nRem = pSeg->nPos;          /* Number of bytes still to come */
218689  Fts5Data *pData = 0;
218690  u8 *pChunk = &pSeg->pLeaf->p[pSeg->iLeafOffset];
218691  int nChunk = MIN(nRem, pSeg->pLeaf->szLeaf - pSeg->iLeafOffset);
218692  int pgno = pSeg->iLeafPgno;
218693  int pgnoSave = 0;
218694
218695  /* This function does notmwork with detail=none databases. */
218696  assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
218697
218698  if( (pSeg->flags & FTS5_SEGITER_REVERSE)==0 ){
218699    pgnoSave = pgno+1;
218700  }
218701
218702  while( 1 ){
218703    xChunk(p, pCtx, pChunk, nChunk);
218704    nRem -= nChunk;
218705    fts5DataRelease(pData);
218706    if( nRem<=0 ){
218707      break;
218708    }else{
218709      pgno++;
218710      pData = fts5LeafRead(p, FTS5_SEGMENT_ROWID(pSeg->pSeg->iSegid, pgno));
218711      if( pData==0 ) break;
218712      pChunk = &pData->p[4];
218713      nChunk = MIN(nRem, pData->szLeaf - 4);
218714      if( pgno==pgnoSave ){
218715        assert( pSeg->pNextLeaf==0 );
218716        pSeg->pNextLeaf = pData;
218717        pData = 0;
218718      }
218719    }
218720  }
218721}
218722
218723/*
218724** Iterator pIter currently points to a valid entry (not EOF). This
218725** function appends the position list data for the current entry to
218726** buffer pBuf. It does not make a copy of the position-list size
218727** field.
218728*/
218729static void fts5SegiterPoslist(
218730  Fts5Index *p,
218731  Fts5SegIter *pSeg,
218732  Fts5Colset *pColset,
218733  Fts5Buffer *pBuf
218734){
218735  if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos+FTS5_DATA_ZERO_PADDING) ){
218736    memset(&pBuf->p[pBuf->n+pSeg->nPos], 0, FTS5_DATA_ZERO_PADDING);
218737    if( pColset==0 ){
218738      fts5ChunkIterate(p, pSeg, (void*)pBuf, fts5PoslistCallback);
218739    }else{
218740      if( p->pConfig->eDetail==FTS5_DETAIL_FULL ){
218741        PoslistCallbackCtx sCtx;
218742        sCtx.pBuf = pBuf;
218743        sCtx.pColset = pColset;
218744        sCtx.eState = fts5IndexColsetTest(pColset, 0);
218745        assert( sCtx.eState==0 || sCtx.eState==1 );
218746        fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistFilterCallback);
218747      }else{
218748        PoslistOffsetsCtx sCtx;
218749        memset(&sCtx, 0, sizeof(sCtx));
218750        sCtx.pBuf = pBuf;
218751        sCtx.pColset = pColset;
218752        fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistOffsetsCallback);
218753      }
218754    }
218755  }
218756}
218757
218758/*
218759** IN/OUT parameter (*pa) points to a position list n bytes in size. If
218760** the position list contains entries for column iCol, then (*pa) is set
218761** to point to the sub-position-list for that column and the number of
218762** bytes in it returned. Or, if the argument position list does not
218763** contain any entries for column iCol, return 0.
218764*/
218765static int fts5IndexExtractCol(
218766  const u8 **pa,                  /* IN/OUT: Pointer to poslist */
218767  int n,                          /* IN: Size of poslist in bytes */
218768  int iCol                        /* Column to extract from poslist */
218769){
218770  int iCurrent = 0;               /* Anything before the first 0x01 is col 0 */
218771  const u8 *p = *pa;
218772  const u8 *pEnd = &p[n];         /* One byte past end of position list */
218773
218774  while( iCol>iCurrent ){
218775    /* Advance pointer p until it points to pEnd or an 0x01 byte that is
218776    ** not part of a varint. Note that it is not possible for a negative
218777    ** or extremely large varint to occur within an uncorrupted position
218778    ** list. So the last byte of each varint may be assumed to have a clear
218779    ** 0x80 bit.  */
218780    while( *p!=0x01 ){
218781      while( *p++ & 0x80 );
218782      if( p>=pEnd ) return 0;
218783    }
218784    *pa = p++;
218785    iCurrent = *p++;
218786    if( iCurrent & 0x80 ){
218787      p--;
218788      p += fts5GetVarint32(p, iCurrent);
218789    }
218790  }
218791  if( iCol!=iCurrent ) return 0;
218792
218793  /* Advance pointer p until it points to pEnd or an 0x01 byte that is
218794  ** not part of a varint */
218795  while( p<pEnd && *p!=0x01 ){
218796    while( *p++ & 0x80 );
218797  }
218798
218799  return p - (*pa);
218800}
218801
218802static void fts5IndexExtractColset(
218803  int *pRc,
218804  Fts5Colset *pColset,            /* Colset to filter on */
218805  const u8 *pPos, int nPos,       /* Position list */
218806  Fts5Buffer *pBuf                /* Output buffer */
218807){
218808  if( *pRc==SQLITE_OK ){
218809    int i;
218810    fts5BufferZero(pBuf);
218811    for(i=0; i<pColset->nCol; i++){
218812      const u8 *pSub = pPos;
218813      int nSub = fts5IndexExtractCol(&pSub, nPos, pColset->aiCol[i]);
218814      if( nSub ){
218815        fts5BufferAppendBlob(pRc, pBuf, nSub, pSub);
218816      }
218817    }
218818  }
218819}
218820
218821/*
218822** xSetOutputs callback used by detail=none tables.
218823*/
218824static void fts5IterSetOutputs_None(Fts5Iter *pIter, Fts5SegIter *pSeg){
218825  assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_NONE );
218826  pIter->base.iRowid = pSeg->iRowid;
218827  pIter->base.nData = pSeg->nPos;
218828}
218829
218830/*
218831** xSetOutputs callback used by detail=full and detail=col tables when no
218832** column filters are specified.
218833*/
218834static void fts5IterSetOutputs_Nocolset(Fts5Iter *pIter, Fts5SegIter *pSeg){
218835  pIter->base.iRowid = pSeg->iRowid;
218836  pIter->base.nData = pSeg->nPos;
218837
218838  assert( pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_NONE );
218839  assert( pIter->pColset==0 );
218840
218841  if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
218842    /* All data is stored on the current page. Populate the output
218843    ** variables to point into the body of the page object. */
218844    pIter->base.pData = &pSeg->pLeaf->p[pSeg->iLeafOffset];
218845  }else{
218846    /* The data is distributed over two or more pages. Copy it into the
218847    ** Fts5Iter.poslist buffer and then set the output pointer to point
218848    ** to this buffer.  */
218849    fts5BufferZero(&pIter->poslist);
218850    fts5SegiterPoslist(pIter->pIndex, pSeg, 0, &pIter->poslist);
218851    pIter->base.pData = pIter->poslist.p;
218852  }
218853}
218854
218855/*
218856** xSetOutputs callback used when the Fts5Colset object has nCol==0 (match
218857** against no columns at all).
218858*/
218859static void fts5IterSetOutputs_ZeroColset(Fts5Iter *pIter, Fts5SegIter *pSeg){
218860  UNUSED_PARAM(pSeg);
218861  pIter->base.nData = 0;
218862}
218863
218864/*
218865** xSetOutputs callback used by detail=col when there is a column filter
218866** and there are 100 or more columns. Also called as a fallback from
218867** fts5IterSetOutputs_Col100 if the column-list spans more than one page.
218868*/
218869static void fts5IterSetOutputs_Col(Fts5Iter *pIter, Fts5SegIter *pSeg){
218870  fts5BufferZero(&pIter->poslist);
218871  fts5SegiterPoslist(pIter->pIndex, pSeg, pIter->pColset, &pIter->poslist);
218872  pIter->base.iRowid = pSeg->iRowid;
218873  pIter->base.pData = pIter->poslist.p;
218874  pIter->base.nData = pIter->poslist.n;
218875}
218876
218877/*
218878** xSetOutputs callback used when:
218879**
218880**   * detail=col,
218881**   * there is a column filter, and
218882**   * the table contains 100 or fewer columns.
218883**
218884** The last point is to ensure all column numbers are stored as
218885** single-byte varints.
218886*/
218887static void fts5IterSetOutputs_Col100(Fts5Iter *pIter, Fts5SegIter *pSeg){
218888
218889  assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
218890  assert( pIter->pColset );
218891
218892  if( pSeg->iLeafOffset+pSeg->nPos>pSeg->pLeaf->szLeaf ){
218893    fts5IterSetOutputs_Col(pIter, pSeg);
218894  }else{
218895    u8 *a = (u8*)&pSeg->pLeaf->p[pSeg->iLeafOffset];
218896    u8 *pEnd = (u8*)&a[pSeg->nPos];
218897    int iPrev = 0;
218898    int *aiCol = pIter->pColset->aiCol;
218899    int *aiColEnd = &aiCol[pIter->pColset->nCol];
218900
218901    u8 *aOut = pIter->poslist.p;
218902    int iPrevOut = 0;
218903
218904    pIter->base.iRowid = pSeg->iRowid;
218905
218906    while( a<pEnd ){
218907      iPrev += (int)a++[0] - 2;
218908      while( *aiCol<iPrev ){
218909        aiCol++;
218910        if( aiCol==aiColEnd ) goto setoutputs_col_out;
218911      }
218912      if( *aiCol==iPrev ){
218913        *aOut++ = (u8)((iPrev - iPrevOut) + 2);
218914        iPrevOut = iPrev;
218915      }
218916    }
218917
218918setoutputs_col_out:
218919    pIter->base.pData = pIter->poslist.p;
218920    pIter->base.nData = aOut - pIter->poslist.p;
218921  }
218922}
218923
218924/*
218925** xSetOutputs callback used by detail=full when there is a column filter.
218926*/
218927static void fts5IterSetOutputs_Full(Fts5Iter *pIter, Fts5SegIter *pSeg){
218928  Fts5Colset *pColset = pIter->pColset;
218929  pIter->base.iRowid = pSeg->iRowid;
218930
218931  assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_FULL );
218932  assert( pColset );
218933
218934  if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
218935    /* All data is stored on the current page. Populate the output
218936    ** variables to point into the body of the page object. */
218937    const u8 *a = &pSeg->pLeaf->p[pSeg->iLeafOffset];
218938    if( pColset->nCol==1 ){
218939      pIter->base.nData = fts5IndexExtractCol(&a, pSeg->nPos,pColset->aiCol[0]);
218940      pIter->base.pData = a;
218941    }else{
218942      int *pRc = &pIter->pIndex->rc;
218943      fts5BufferZero(&pIter->poslist);
218944      fts5IndexExtractColset(pRc, pColset, a, pSeg->nPos, &pIter->poslist);
218945      pIter->base.pData = pIter->poslist.p;
218946      pIter->base.nData = pIter->poslist.n;
218947    }
218948  }else{
218949    /* The data is distributed over two or more pages. Copy it into the
218950    ** Fts5Iter.poslist buffer and then set the output pointer to point
218951    ** to this buffer.  */
218952    fts5BufferZero(&pIter->poslist);
218953    fts5SegiterPoslist(pIter->pIndex, pSeg, pColset, &pIter->poslist);
218954    pIter->base.pData = pIter->poslist.p;
218955    pIter->base.nData = pIter->poslist.n;
218956  }
218957}
218958
218959static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){
218960  if( *pRc==SQLITE_OK ){
218961    Fts5Config *pConfig = pIter->pIndex->pConfig;
218962    if( pConfig->eDetail==FTS5_DETAIL_NONE ){
218963      pIter->xSetOutputs = fts5IterSetOutputs_None;
218964    }
218965
218966    else if( pIter->pColset==0 ){
218967      pIter->xSetOutputs = fts5IterSetOutputs_Nocolset;
218968    }
218969
218970    else if( pIter->pColset->nCol==0 ){
218971      pIter->xSetOutputs = fts5IterSetOutputs_ZeroColset;
218972    }
218973
218974    else if( pConfig->eDetail==FTS5_DETAIL_FULL ){
218975      pIter->xSetOutputs = fts5IterSetOutputs_Full;
218976    }
218977
218978    else{
218979      assert( pConfig->eDetail==FTS5_DETAIL_COLUMNS );
218980      if( pConfig->nCol<=100 ){
218981        pIter->xSetOutputs = fts5IterSetOutputs_Col100;
218982        sqlite3Fts5BufferSize(pRc, &pIter->poslist, pConfig->nCol);
218983      }else{
218984        pIter->xSetOutputs = fts5IterSetOutputs_Col;
218985      }
218986    }
218987  }
218988}
218989
218990
218991/*
218992** Allocate a new Fts5Iter object.
218993**
218994** The new object will be used to iterate through data in structure pStruct.
218995** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel
218996** is zero or greater, data from the first nSegment segments on level iLevel
218997** is merged.
218998**
218999** The iterator initially points to the first term/rowid entry in the
219000** iterated data.
219001*/
219002static void fts5MultiIterNew(
219003  Fts5Index *p,                   /* FTS5 backend to iterate within */
219004  Fts5Structure *pStruct,         /* Structure of specific index */
219005  int flags,                      /* FTS5INDEX_QUERY_XXX flags */
219006  Fts5Colset *pColset,            /* Colset to filter on (or NULL) */
219007  const u8 *pTerm, int nTerm,     /* Term to seek to (or NULL/0) */
219008  int iLevel,                     /* Level to iterate (-1 for all) */
219009  int nSegment,                   /* Number of segments to merge (iLevel>=0) */
219010  Fts5Iter **ppOut                /* New object */
219011){
219012  int nSeg = 0;                   /* Number of segment-iters in use */
219013  int iIter = 0;                  /* */
219014  int iSeg;                       /* Used to iterate through segments */
219015  Fts5StructureLevel *pLvl;
219016  Fts5Iter *pNew;
219017
219018  assert( (pTerm==0 && nTerm==0) || iLevel<0 );
219019
219020  /* Allocate space for the new multi-seg-iterator. */
219021  if( p->rc==SQLITE_OK ){
219022    if( iLevel<0 ){
219023      assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
219024      nSeg = pStruct->nSegment;
219025      nSeg += (p->pHash ? 1 : 0);
219026    }else{
219027      nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment);
219028    }
219029  }
219030  *ppOut = pNew = fts5MultiIterAlloc(p, nSeg);
219031  if( pNew==0 ) return;
219032  pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC));
219033  pNew->bSkipEmpty = (0!=(flags & FTS5INDEX_QUERY_SKIPEMPTY));
219034  pNew->pColset = pColset;
219035  if( (flags & FTS5INDEX_QUERY_NOOUTPUT)==0 ){
219036    fts5IterSetOutputCb(&p->rc, pNew);
219037  }
219038
219039  /* Initialize each of the component segment iterators. */
219040  if( p->rc==SQLITE_OK ){
219041    if( iLevel<0 ){
219042      Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel];
219043      if( p->pHash ){
219044        /* Add a segment iterator for the current contents of the hash table. */
219045        Fts5SegIter *pIter = &pNew->aSeg[iIter++];
219046        fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter);
219047      }
219048      for(pLvl=&pStruct->aLevel[0]; pLvl<pEnd; pLvl++){
219049        for(iSeg=pLvl->nSeg-1; iSeg>=0; iSeg--){
219050          Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
219051          Fts5SegIter *pIter = &pNew->aSeg[iIter++];
219052          if( pTerm==0 ){
219053            fts5SegIterInit(p, pSeg, pIter);
219054          }else{
219055            fts5SegIterSeekInit(p, pTerm, nTerm, flags, pSeg, pIter);
219056          }
219057        }
219058      }
219059    }else{
219060      pLvl = &pStruct->aLevel[iLevel];
219061      for(iSeg=nSeg-1; iSeg>=0; iSeg--){
219062        fts5SegIterInit(p, &pLvl->aSeg[iSeg], &pNew->aSeg[iIter++]);
219063      }
219064    }
219065    assert( iIter==nSeg );
219066  }
219067
219068  /* If the above was successful, each component iterators now points
219069  ** to the first entry in its segment. In this case initialize the
219070  ** aFirst[] array. Or, if an error has occurred, free the iterator
219071  ** object and set the output variable to NULL.  */
219072  if( p->rc==SQLITE_OK ){
219073    for(iIter=pNew->nSeg-1; iIter>0; iIter--){
219074      int iEq;
219075      if( (iEq = fts5MultiIterDoCompare(pNew, iIter)) ){
219076        Fts5SegIter *pSeg = &pNew->aSeg[iEq];
219077        if( p->rc==SQLITE_OK ) pSeg->xNext(p, pSeg, 0);
219078        fts5MultiIterAdvanced(p, pNew, iEq, iIter);
219079      }
219080    }
219081    fts5MultiIterSetEof(pNew);
219082    fts5AssertMultiIterSetup(p, pNew);
219083
219084    if( pNew->bSkipEmpty && fts5MultiIterIsEmpty(p, pNew) ){
219085      fts5MultiIterNext(p, pNew, 0, 0);
219086    }else if( pNew->base.bEof==0 ){
219087      Fts5SegIter *pSeg = &pNew->aSeg[pNew->aFirst[1].iFirst];
219088      pNew->xSetOutputs(pNew, pSeg);
219089    }
219090
219091  }else{
219092    fts5MultiIterFree(pNew);
219093    *ppOut = 0;
219094  }
219095}
219096
219097/*
219098** Create an Fts5Iter that iterates through the doclist provided
219099** as the second argument.
219100*/
219101static void fts5MultiIterNew2(
219102  Fts5Index *p,                   /* FTS5 backend to iterate within */
219103  Fts5Data *pData,                /* Doclist to iterate through */
219104  int bDesc,                      /* True for descending rowid order */
219105  Fts5Iter **ppOut                /* New object */
219106){
219107  Fts5Iter *pNew;
219108  pNew = fts5MultiIterAlloc(p, 2);
219109  if( pNew ){
219110    Fts5SegIter *pIter = &pNew->aSeg[1];
219111
219112    pIter->flags = FTS5_SEGITER_ONETERM;
219113    if( pData->szLeaf>0 ){
219114      pIter->pLeaf = pData;
219115      pIter->iLeafOffset = fts5GetVarint(pData->p, (u64*)&pIter->iRowid);
219116      pIter->iEndofDoclist = pData->nn;
219117      pNew->aFirst[1].iFirst = 1;
219118      if( bDesc ){
219119        pNew->bRev = 1;
219120        pIter->flags |= FTS5_SEGITER_REVERSE;
219121        fts5SegIterReverseInitPage(p, pIter);
219122      }else{
219123        fts5SegIterLoadNPos(p, pIter);
219124      }
219125      pData = 0;
219126    }else{
219127      pNew->base.bEof = 1;
219128    }
219129    fts5SegIterSetNext(p, pIter);
219130
219131    *ppOut = pNew;
219132  }
219133
219134  fts5DataRelease(pData);
219135}
219136
219137/*
219138** Return true if the iterator is at EOF or if an error has occurred.
219139** False otherwise.
219140*/
219141static int fts5MultiIterEof(Fts5Index *p, Fts5Iter *pIter){
219142  assert( p->rc
219143      || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->base.bEof
219144  );
219145  return (p->rc || pIter->base.bEof);
219146}
219147
219148/*
219149** Return the rowid of the entry that the iterator currently points
219150** to. If the iterator points to EOF when this function is called the
219151** results are undefined.
219152*/
219153static i64 fts5MultiIterRowid(Fts5Iter *pIter){
219154  assert( pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf );
219155  return pIter->aSeg[ pIter->aFirst[1].iFirst ].iRowid;
219156}
219157
219158/*
219159** Move the iterator to the next entry at or following iMatch.
219160*/
219161static void fts5MultiIterNextFrom(
219162  Fts5Index *p,
219163  Fts5Iter *pIter,
219164  i64 iMatch
219165){
219166  while( 1 ){
219167    i64 iRowid;
219168    fts5MultiIterNext(p, pIter, 1, iMatch);
219169    if( fts5MultiIterEof(p, pIter) ) break;
219170    iRowid = fts5MultiIterRowid(pIter);
219171    if( pIter->bRev==0 && iRowid>=iMatch ) break;
219172    if( pIter->bRev!=0 && iRowid<=iMatch ) break;
219173  }
219174}
219175
219176/*
219177** Return a pointer to a buffer containing the term associated with the
219178** entry that the iterator currently points to.
219179*/
219180static const u8 *fts5MultiIterTerm(Fts5Iter *pIter, int *pn){
219181  Fts5SegIter *p = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
219182  *pn = p->term.n;
219183  return p->term.p;
219184}
219185
219186/*
219187** Allocate a new segment-id for the structure pStruct. The new segment
219188** id must be between 1 and 65335 inclusive, and must not be used by
219189** any currently existing segment. If a free segment id cannot be found,
219190** SQLITE_FULL is returned.
219191**
219192** If an error has already occurred, this function is a no-op. 0 is
219193** returned in this case.
219194*/
219195static int fts5AllocateSegid(Fts5Index *p, Fts5Structure *pStruct){
219196  int iSegid = 0;
219197
219198  if( p->rc==SQLITE_OK ){
219199    if( pStruct->nSegment>=FTS5_MAX_SEGMENT ){
219200      p->rc = SQLITE_FULL;
219201    }else{
219202      /* FTS5_MAX_SEGMENT is currently defined as 2000. So the following
219203      ** array is 63 elements, or 252 bytes, in size.  */
219204      u32 aUsed[(FTS5_MAX_SEGMENT+31) / 32];
219205      int iLvl, iSeg;
219206      int i;
219207      u32 mask;
219208      memset(aUsed, 0, sizeof(aUsed));
219209      for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
219210        for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
219211          int iId = pStruct->aLevel[iLvl].aSeg[iSeg].iSegid;
219212          if( iId<=FTS5_MAX_SEGMENT && iId>0 ){
219213            aUsed[(iId-1) / 32] |= (u32)1 << ((iId-1) % 32);
219214          }
219215        }
219216      }
219217
219218      for(i=0; aUsed[i]==0xFFFFFFFF; i++);
219219      mask = aUsed[i];
219220      for(iSegid=0; mask & ((u32)1 << iSegid); iSegid++);
219221      iSegid += 1 + i*32;
219222
219223#ifdef SQLITE_DEBUG
219224      for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
219225        for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
219226          assert_nc( iSegid!=pStruct->aLevel[iLvl].aSeg[iSeg].iSegid );
219227        }
219228      }
219229      assert_nc( iSegid>0 && iSegid<=FTS5_MAX_SEGMENT );
219230
219231      {
219232        sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p);
219233        if( p->rc==SQLITE_OK ){
219234          u8 aBlob[2] = {0xff, 0xff};
219235          sqlite3_bind_int(pIdxSelect, 1, iSegid);
219236          sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC);
219237          assert_nc( sqlite3_step(pIdxSelect)!=SQLITE_ROW );
219238          p->rc = sqlite3_reset(pIdxSelect);
219239          sqlite3_bind_null(pIdxSelect, 2);
219240        }
219241      }
219242#endif
219243    }
219244  }
219245
219246  return iSegid;
219247}
219248
219249/*
219250** Discard all data currently cached in the hash-tables.
219251*/
219252static void fts5IndexDiscardData(Fts5Index *p){
219253  assert( p->pHash || p->nPendingData==0 );
219254  if( p->pHash ){
219255    sqlite3Fts5HashClear(p->pHash);
219256    p->nPendingData = 0;
219257  }
219258}
219259
219260/*
219261** Return the size of the prefix, in bytes, that buffer
219262** (pNew/<length-unknown>) shares with buffer (pOld/nOld).
219263**
219264** Buffer (pNew/<length-unknown>) is guaranteed to be greater
219265** than buffer (pOld/nOld).
219266*/
219267static int fts5PrefixCompress(int nOld, const u8 *pOld, const u8 *pNew){
219268  int i;
219269  for(i=0; i<nOld; i++){
219270    if( pOld[i]!=pNew[i] ) break;
219271  }
219272  return i;
219273}
219274
219275static void fts5WriteDlidxClear(
219276  Fts5Index *p,
219277  Fts5SegWriter *pWriter,
219278  int bFlush                      /* If true, write dlidx to disk */
219279){
219280  int i;
219281  assert( bFlush==0 || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n>0) );
219282  for(i=0; i<pWriter->nDlidx; i++){
219283    Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
219284    if( pDlidx->buf.n==0 ) break;
219285    if( bFlush ){
219286      assert( pDlidx->pgno!=0 );
219287      fts5DataWrite(p,
219288          FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
219289          pDlidx->buf.p, pDlidx->buf.n
219290      );
219291    }
219292    sqlite3Fts5BufferZero(&pDlidx->buf);
219293    pDlidx->bPrevValid = 0;
219294  }
219295}
219296
219297/*
219298** Grow the pWriter->aDlidx[] array to at least nLvl elements in size.
219299** Any new array elements are zeroed before returning.
219300*/
219301static int fts5WriteDlidxGrow(
219302  Fts5Index *p,
219303  Fts5SegWriter *pWriter,
219304  int nLvl
219305){
219306  if( p->rc==SQLITE_OK && nLvl>=pWriter->nDlidx ){
219307    Fts5DlidxWriter *aDlidx = (Fts5DlidxWriter*)sqlite3_realloc64(
219308        pWriter->aDlidx, sizeof(Fts5DlidxWriter) * nLvl
219309    );
219310    if( aDlidx==0 ){
219311      p->rc = SQLITE_NOMEM;
219312    }else{
219313      size_t nByte = sizeof(Fts5DlidxWriter) * (nLvl - pWriter->nDlidx);
219314      memset(&aDlidx[pWriter->nDlidx], 0, nByte);
219315      pWriter->aDlidx = aDlidx;
219316      pWriter->nDlidx = nLvl;
219317    }
219318  }
219319  return p->rc;
219320}
219321
219322/*
219323** If the current doclist-index accumulating in pWriter->aDlidx[] is large
219324** enough, flush it to disk and return 1. Otherwise discard it and return
219325** zero.
219326*/
219327static int fts5WriteFlushDlidx(Fts5Index *p, Fts5SegWriter *pWriter){
219328  int bFlag = 0;
219329
219330  /* If there were FTS5_MIN_DLIDX_SIZE or more empty leaf pages written
219331  ** to the database, also write the doclist-index to disk.  */
219332  if( pWriter->aDlidx[0].buf.n>0 && pWriter->nEmpty>=FTS5_MIN_DLIDX_SIZE ){
219333    bFlag = 1;
219334  }
219335  fts5WriteDlidxClear(p, pWriter, bFlag);
219336  pWriter->nEmpty = 0;
219337  return bFlag;
219338}
219339
219340/*
219341** This function is called whenever processing of the doclist for the
219342** last term on leaf page (pWriter->iBtPage) is completed.
219343**
219344** The doclist-index for that term is currently stored in-memory within the
219345** Fts5SegWriter.aDlidx[] array. If it is large enough, this function
219346** writes it out to disk. Or, if it is too small to bother with, discards
219347** it.
219348**
219349** Fts5SegWriter.btterm currently contains the first term on page iBtPage.
219350*/
219351static void fts5WriteFlushBtree(Fts5Index *p, Fts5SegWriter *pWriter){
219352  int bFlag;
219353
219354  assert( pWriter->iBtPage || pWriter->nEmpty==0 );
219355  if( pWriter->iBtPage==0 ) return;
219356  bFlag = fts5WriteFlushDlidx(p, pWriter);
219357
219358  if( p->rc==SQLITE_OK ){
219359    const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:"");
219360    /* The following was already done in fts5WriteInit(): */
219361    /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
219362    sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC);
219363    sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
219364    sqlite3_step(p->pIdxWriter);
219365    p->rc = sqlite3_reset(p->pIdxWriter);
219366    sqlite3_bind_null(p->pIdxWriter, 2);
219367  }
219368  pWriter->iBtPage = 0;
219369}
219370
219371/*
219372** This is called once for each leaf page except the first that contains
219373** at least one term. Argument (nTerm/pTerm) is the split-key - a term that
219374** is larger than all terms written to earlier leaves, and equal to or
219375** smaller than the first term on the new leaf.
219376**
219377** If an error occurs, an error code is left in Fts5Index.rc. If an error
219378** has already occurred when this function is called, it is a no-op.
219379*/
219380static void fts5WriteBtreeTerm(
219381  Fts5Index *p,                   /* FTS5 backend object */
219382  Fts5SegWriter *pWriter,         /* Writer object */
219383  int nTerm, const u8 *pTerm      /* First term on new page */
219384){
219385  fts5WriteFlushBtree(p, pWriter);
219386  if( p->rc==SQLITE_OK ){
219387    fts5BufferSet(&p->rc, &pWriter->btterm, nTerm, pTerm);
219388    pWriter->iBtPage = pWriter->writer.pgno;
219389  }
219390}
219391
219392/*
219393** This function is called when flushing a leaf page that contains no
219394** terms at all to disk.
219395*/
219396static void fts5WriteBtreeNoTerm(
219397  Fts5Index *p,                   /* FTS5 backend object */
219398  Fts5SegWriter *pWriter          /* Writer object */
219399){
219400  /* If there were no rowids on the leaf page either and the doclist-index
219401  ** has already been started, append an 0x00 byte to it.  */
219402  if( pWriter->bFirstRowidInPage && pWriter->aDlidx[0].buf.n>0 ){
219403    Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[0];
219404    assert( pDlidx->bPrevValid );
219405    sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, 0);
219406  }
219407
219408  /* Increment the "number of sequential leaves without a term" counter. */
219409  pWriter->nEmpty++;
219410}
219411
219412static i64 fts5DlidxExtractFirstRowid(Fts5Buffer *pBuf){
219413  i64 iRowid;
219414  int iOff;
219415
219416  iOff = 1 + fts5GetVarint(&pBuf->p[1], (u64*)&iRowid);
219417  fts5GetVarint(&pBuf->p[iOff], (u64*)&iRowid);
219418  return iRowid;
219419}
219420
219421/*
219422** Rowid iRowid has just been appended to the current leaf page. It is the
219423** first on the page. This function appends an appropriate entry to the current
219424** doclist-index.
219425*/
219426static void fts5WriteDlidxAppend(
219427  Fts5Index *p,
219428  Fts5SegWriter *pWriter,
219429  i64 iRowid
219430){
219431  int i;
219432  int bDone = 0;
219433
219434  for(i=0; p->rc==SQLITE_OK && bDone==0; i++){
219435    i64 iVal;
219436    Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
219437
219438    if( pDlidx->buf.n>=p->pConfig->pgsz ){
219439      /* The current doclist-index page is full. Write it to disk and push
219440      ** a copy of iRowid (which will become the first rowid on the next
219441      ** doclist-index leaf page) up into the next level of the b-tree
219442      ** hierarchy. If the node being flushed is currently the root node,
219443      ** also push its first rowid upwards. */
219444      pDlidx->buf.p[0] = 0x01;    /* Not the root node */
219445      fts5DataWrite(p,
219446          FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
219447          pDlidx->buf.p, pDlidx->buf.n
219448      );
219449      fts5WriteDlidxGrow(p, pWriter, i+2);
219450      pDlidx = &pWriter->aDlidx[i];
219451      if( p->rc==SQLITE_OK && pDlidx[1].buf.n==0 ){
219452        i64 iFirst = fts5DlidxExtractFirstRowid(&pDlidx->buf);
219453
219454        /* This was the root node. Push its first rowid up to the new root. */
219455        pDlidx[1].pgno = pDlidx->pgno;
219456        sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, 0);
219457        sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, pDlidx->pgno);
219458        sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, iFirst);
219459        pDlidx[1].bPrevValid = 1;
219460        pDlidx[1].iPrev = iFirst;
219461      }
219462
219463      sqlite3Fts5BufferZero(&pDlidx->buf);
219464      pDlidx->bPrevValid = 0;
219465      pDlidx->pgno++;
219466    }else{
219467      bDone = 1;
219468    }
219469
219470    if( pDlidx->bPrevValid ){
219471      iVal = iRowid - pDlidx->iPrev;
219472    }else{
219473      i64 iPgno = (i==0 ? pWriter->writer.pgno : pDlidx[-1].pgno);
219474      assert( pDlidx->buf.n==0 );
219475      sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, !bDone);
219476      sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iPgno);
219477      iVal = iRowid;
219478    }
219479
219480    sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iVal);
219481    pDlidx->bPrevValid = 1;
219482    pDlidx->iPrev = iRowid;
219483  }
219484}
219485
219486static void fts5WriteFlushLeaf(Fts5Index *p, Fts5SegWriter *pWriter){
219487  static const u8 zero[] = { 0x00, 0x00, 0x00, 0x00 };
219488  Fts5PageWriter *pPage = &pWriter->writer;
219489  i64 iRowid;
219490
219491  assert( (pPage->pgidx.n==0)==(pWriter->bFirstTermInPage) );
219492
219493  /* Set the szLeaf header field. */
219494  assert( 0==fts5GetU16(&pPage->buf.p[2]) );
219495  fts5PutU16(&pPage->buf.p[2], (u16)pPage->buf.n);
219496
219497  if( pWriter->bFirstTermInPage ){
219498    /* No term was written to this page. */
219499    assert( pPage->pgidx.n==0 );
219500    fts5WriteBtreeNoTerm(p, pWriter);
219501  }else{
219502    /* Append the pgidx to the page buffer. Set the szLeaf header field. */
219503    fts5BufferAppendBlob(&p->rc, &pPage->buf, pPage->pgidx.n, pPage->pgidx.p);
219504  }
219505
219506  /* Write the page out to disk */
219507  iRowid = FTS5_SEGMENT_ROWID(pWriter->iSegid, pPage->pgno);
219508  fts5DataWrite(p, iRowid, pPage->buf.p, pPage->buf.n);
219509
219510  /* Initialize the next page. */
219511  fts5BufferZero(&pPage->buf);
219512  fts5BufferZero(&pPage->pgidx);
219513  fts5BufferAppendBlob(&p->rc, &pPage->buf, 4, zero);
219514  pPage->iPrevPgidx = 0;
219515  pPage->pgno++;
219516
219517  /* Increase the leaves written counter */
219518  pWriter->nLeafWritten++;
219519
219520  /* The new leaf holds no terms or rowids */
219521  pWriter->bFirstTermInPage = 1;
219522  pWriter->bFirstRowidInPage = 1;
219523}
219524
219525/*
219526** Append term pTerm/nTerm to the segment being written by the writer passed
219527** as the second argument.
219528**
219529** If an error occurs, set the Fts5Index.rc error code. If an error has
219530** already occurred, this function is a no-op.
219531*/
219532static void fts5WriteAppendTerm(
219533  Fts5Index *p,
219534  Fts5SegWriter *pWriter,
219535  int nTerm, const u8 *pTerm
219536){
219537  int nPrefix;                    /* Bytes of prefix compression for term */
219538  Fts5PageWriter *pPage = &pWriter->writer;
219539  Fts5Buffer *pPgidx = &pWriter->writer.pgidx;
219540  int nMin = MIN(pPage->term.n, nTerm);
219541
219542  assert( p->rc==SQLITE_OK );
219543  assert( pPage->buf.n>=4 );
219544  assert( pPage->buf.n>4 || pWriter->bFirstTermInPage );
219545
219546  /* If the current leaf page is full, flush it to disk. */
219547  if( (pPage->buf.n + pPgidx->n + nTerm + 2)>=p->pConfig->pgsz ){
219548    if( pPage->buf.n>4 ){
219549      fts5WriteFlushLeaf(p, pWriter);
219550      if( p->rc!=SQLITE_OK ) return;
219551    }
219552    fts5BufferGrow(&p->rc, &pPage->buf, nTerm+FTS5_DATA_PADDING);
219553  }
219554
219555  /* TODO1: Updating pgidx here. */
219556  pPgidx->n += sqlite3Fts5PutVarint(
219557      &pPgidx->p[pPgidx->n], pPage->buf.n - pPage->iPrevPgidx
219558  );
219559  pPage->iPrevPgidx = pPage->buf.n;
219560#if 0
219561  fts5PutU16(&pPgidx->p[pPgidx->n], pPage->buf.n);
219562  pPgidx->n += 2;
219563#endif
219564
219565  if( pWriter->bFirstTermInPage ){
219566    nPrefix = 0;
219567    if( pPage->pgno!=1 ){
219568      /* This is the first term on a leaf that is not the leftmost leaf in
219569      ** the segment b-tree. In this case it is necessary to add a term to
219570      ** the b-tree hierarchy that is (a) larger than the largest term
219571      ** already written to the segment and (b) smaller than or equal to
219572      ** this term. In other words, a prefix of (pTerm/nTerm) that is one
219573      ** byte longer than the longest prefix (pTerm/nTerm) shares with the
219574      ** previous term.
219575      **
219576      ** Usually, the previous term is available in pPage->term. The exception
219577      ** is if this is the first term written in an incremental-merge step.
219578      ** In this case the previous term is not available, so just write a
219579      ** copy of (pTerm/nTerm) into the parent node. This is slightly
219580      ** inefficient, but still correct.  */
219581      int n = nTerm;
219582      if( pPage->term.n ){
219583        n = 1 + fts5PrefixCompress(nMin, pPage->term.p, pTerm);
219584      }
219585      fts5WriteBtreeTerm(p, pWriter, n, pTerm);
219586      if( p->rc!=SQLITE_OK ) return;
219587      pPage = &pWriter->writer;
219588    }
219589  }else{
219590    nPrefix = fts5PrefixCompress(nMin, pPage->term.p, pTerm);
219591    fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix);
219592  }
219593
219594  /* Append the number of bytes of new data, then the term data itself
219595  ** to the page. */
219596  fts5BufferAppendVarint(&p->rc, &pPage->buf, nTerm - nPrefix);
219597  fts5BufferAppendBlob(&p->rc, &pPage->buf, nTerm - nPrefix, &pTerm[nPrefix]);
219598
219599  /* Update the Fts5PageWriter.term field. */
219600  fts5BufferSet(&p->rc, &pPage->term, nTerm, pTerm);
219601  pWriter->bFirstTermInPage = 0;
219602
219603  pWriter->bFirstRowidInPage = 0;
219604  pWriter->bFirstRowidInDoclist = 1;
219605
219606  assert( p->rc || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n==0) );
219607  pWriter->aDlidx[0].pgno = pPage->pgno;
219608}
219609
219610/*
219611** Append a rowid and position-list size field to the writers output.
219612*/
219613static void fts5WriteAppendRowid(
219614  Fts5Index *p,
219615  Fts5SegWriter *pWriter,
219616  i64 iRowid
219617){
219618  if( p->rc==SQLITE_OK ){
219619    Fts5PageWriter *pPage = &pWriter->writer;
219620
219621    if( (pPage->buf.n + pPage->pgidx.n)>=p->pConfig->pgsz ){
219622      fts5WriteFlushLeaf(p, pWriter);
219623    }
219624
219625    /* If this is to be the first rowid written to the page, set the
219626    ** rowid-pointer in the page-header. Also append a value to the dlidx
219627    ** buffer, in case a doclist-index is required.  */
219628    if( pWriter->bFirstRowidInPage ){
219629      fts5PutU16(pPage->buf.p, (u16)pPage->buf.n);
219630      fts5WriteDlidxAppend(p, pWriter, iRowid);
219631    }
219632
219633    /* Write the rowid. */
219634    if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){
219635      fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid);
219636    }else{
219637      assert_nc( p->rc || iRowid>pWriter->iPrevRowid );
219638      fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid - pWriter->iPrevRowid);
219639    }
219640    pWriter->iPrevRowid = iRowid;
219641    pWriter->bFirstRowidInDoclist = 0;
219642    pWriter->bFirstRowidInPage = 0;
219643  }
219644}
219645
219646static void fts5WriteAppendPoslistData(
219647  Fts5Index *p,
219648  Fts5SegWriter *pWriter,
219649  const u8 *aData,
219650  int nData
219651){
219652  Fts5PageWriter *pPage = &pWriter->writer;
219653  const u8 *a = aData;
219654  int n = nData;
219655
219656  assert( p->pConfig->pgsz>0 );
219657  while( p->rc==SQLITE_OK
219658     && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
219659  ){
219660    int nReq = p->pConfig->pgsz - pPage->buf.n - pPage->pgidx.n;
219661    int nCopy = 0;
219662    while( nCopy<nReq ){
219663      i64 dummy;
219664      nCopy += fts5GetVarint(&a[nCopy], (u64*)&dummy);
219665    }
219666    fts5BufferAppendBlob(&p->rc, &pPage->buf, nCopy, a);
219667    a += nCopy;
219668    n -= nCopy;
219669    fts5WriteFlushLeaf(p, pWriter);
219670  }
219671  if( n>0 ){
219672    fts5BufferAppendBlob(&p->rc, &pPage->buf, n, a);
219673  }
219674}
219675
219676/*
219677** Flush any data cached by the writer object to the database. Free any
219678** allocations associated with the writer.
219679*/
219680static void fts5WriteFinish(
219681  Fts5Index *p,
219682  Fts5SegWriter *pWriter,         /* Writer object */
219683  int *pnLeaf                     /* OUT: Number of leaf pages in b-tree */
219684){
219685  int i;
219686  Fts5PageWriter *pLeaf = &pWriter->writer;
219687  if( p->rc==SQLITE_OK ){
219688    assert( pLeaf->pgno>=1 );
219689    if( pLeaf->buf.n>4 ){
219690      fts5WriteFlushLeaf(p, pWriter);
219691    }
219692    *pnLeaf = pLeaf->pgno-1;
219693    if( pLeaf->pgno>1 ){
219694      fts5WriteFlushBtree(p, pWriter);
219695    }
219696  }
219697  fts5BufferFree(&pLeaf->term);
219698  fts5BufferFree(&pLeaf->buf);
219699  fts5BufferFree(&pLeaf->pgidx);
219700  fts5BufferFree(&pWriter->btterm);
219701
219702  for(i=0; i<pWriter->nDlidx; i++){
219703    sqlite3Fts5BufferFree(&pWriter->aDlidx[i].buf);
219704  }
219705  sqlite3_free(pWriter->aDlidx);
219706}
219707
219708static void fts5WriteInit(
219709  Fts5Index *p,
219710  Fts5SegWriter *pWriter,
219711  int iSegid
219712){
219713  const int nBuffer = p->pConfig->pgsz + FTS5_DATA_PADDING;
219714
219715  memset(pWriter, 0, sizeof(Fts5SegWriter));
219716  pWriter->iSegid = iSegid;
219717
219718  fts5WriteDlidxGrow(p, pWriter, 1);
219719  pWriter->writer.pgno = 1;
219720  pWriter->bFirstTermInPage = 1;
219721  pWriter->iBtPage = 1;
219722
219723  assert( pWriter->writer.buf.n==0 );
219724  assert( pWriter->writer.pgidx.n==0 );
219725
219726  /* Grow the two buffers to pgsz + padding bytes in size. */
219727  sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.pgidx, nBuffer);
219728  sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.buf, nBuffer);
219729
219730  if( p->pIdxWriter==0 ){
219731    Fts5Config *pConfig = p->pConfig;
219732    fts5IndexPrepareStmt(p, &p->pIdxWriter, sqlite3_mprintf(
219733          "INSERT INTO '%q'.'%q_idx'(segid,term,pgno) VALUES(?,?,?)",
219734          pConfig->zDb, pConfig->zName
219735    ));
219736  }
219737
219738  if( p->rc==SQLITE_OK ){
219739    /* Initialize the 4-byte leaf-page header to 0x00. */
219740    memset(pWriter->writer.buf.p, 0, 4);
219741    pWriter->writer.buf.n = 4;
219742
219743    /* Bind the current output segment id to the index-writer. This is an
219744    ** optimization over binding the same value over and over as rows are
219745    ** inserted into %_idx by the current writer.  */
219746    sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid);
219747  }
219748}
219749
219750/*
219751** Iterator pIter was used to iterate through the input segments of on an
219752** incremental merge operation. This function is called if the incremental
219753** merge step has finished but the input has not been completely exhausted.
219754*/
219755static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){
219756  int i;
219757  Fts5Buffer buf;
219758  memset(&buf, 0, sizeof(Fts5Buffer));
219759  for(i=0; i<pIter->nSeg && p->rc==SQLITE_OK; i++){
219760    Fts5SegIter *pSeg = &pIter->aSeg[i];
219761    if( pSeg->pSeg==0 ){
219762      /* no-op */
219763    }else if( pSeg->pLeaf==0 ){
219764      /* All keys from this input segment have been transfered to the output.
219765      ** Set both the first and last page-numbers to 0 to indicate that the
219766      ** segment is now empty. */
219767      pSeg->pSeg->pgnoLast = 0;
219768      pSeg->pSeg->pgnoFirst = 0;
219769    }else{
219770      int iOff = pSeg->iTermLeafOffset;     /* Offset on new first leaf page */
219771      i64 iLeafRowid;
219772      Fts5Data *pData;
219773      int iId = pSeg->pSeg->iSegid;
219774      u8 aHdr[4] = {0x00, 0x00, 0x00, 0x00};
219775
219776      iLeafRowid = FTS5_SEGMENT_ROWID(iId, pSeg->iTermLeafPgno);
219777      pData = fts5LeafRead(p, iLeafRowid);
219778      if( pData ){
219779        if( iOff>pData->szLeaf ){
219780          /* This can occur if the pages that the segments occupy overlap - if
219781          ** a single page has been assigned to more than one segment. In
219782          ** this case a prior iteration of this loop may have corrupted the
219783          ** segment currently being trimmed.  */
219784          p->rc = FTS5_CORRUPT;
219785        }else{
219786          fts5BufferZero(&buf);
219787          fts5BufferGrow(&p->rc, &buf, pData->nn);
219788          fts5BufferAppendBlob(&p->rc, &buf, sizeof(aHdr), aHdr);
219789          fts5BufferAppendVarint(&p->rc, &buf, pSeg->term.n);
219790          fts5BufferAppendBlob(&p->rc, &buf, pSeg->term.n, pSeg->term.p);
219791          fts5BufferAppendBlob(&p->rc, &buf, pData->szLeaf-iOff,&pData->p[iOff]);
219792          if( p->rc==SQLITE_OK ){
219793            /* Set the szLeaf field */
219794            fts5PutU16(&buf.p[2], (u16)buf.n);
219795          }
219796
219797          /* Set up the new page-index array */
219798          fts5BufferAppendVarint(&p->rc, &buf, 4);
219799          if( pSeg->iLeafPgno==pSeg->iTermLeafPgno
219800           && pSeg->iEndofDoclist<pData->szLeaf
219801           && pSeg->iPgidxOff<=pData->nn
219802          ){
219803            int nDiff = pData->szLeaf - pSeg->iEndofDoclist;
219804            fts5BufferAppendVarint(&p->rc, &buf, buf.n - 1 - nDiff - 4);
219805            fts5BufferAppendBlob(&p->rc, &buf,
219806                pData->nn - pSeg->iPgidxOff, &pData->p[pSeg->iPgidxOff]
219807            );
219808          }
219809
219810          pSeg->pSeg->pgnoFirst = pSeg->iTermLeafPgno;
219811          fts5DataDelete(p, FTS5_SEGMENT_ROWID(iId, 1), iLeafRowid);
219812          fts5DataWrite(p, iLeafRowid, buf.p, buf.n);
219813        }
219814        fts5DataRelease(pData);
219815      }
219816    }
219817  }
219818  fts5BufferFree(&buf);
219819}
219820
219821static void fts5MergeChunkCallback(
219822  Fts5Index *p,
219823  void *pCtx,
219824  const u8 *pChunk, int nChunk
219825){
219826  Fts5SegWriter *pWriter = (Fts5SegWriter*)pCtx;
219827  fts5WriteAppendPoslistData(p, pWriter, pChunk, nChunk);
219828}
219829
219830/*
219831**
219832*/
219833static void fts5IndexMergeLevel(
219834  Fts5Index *p,                   /* FTS5 backend object */
219835  Fts5Structure **ppStruct,       /* IN/OUT: Stucture of index */
219836  int iLvl,                       /* Level to read input from */
219837  int *pnRem                      /* Write up to this many output leaves */
219838){
219839  Fts5Structure *pStruct = *ppStruct;
219840  Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
219841  Fts5StructureLevel *pLvlOut;
219842  Fts5Iter *pIter = 0;       /* Iterator to read input data */
219843  int nRem = pnRem ? *pnRem : 0;  /* Output leaf pages left to write */
219844  int nInput;                     /* Number of input segments */
219845  Fts5SegWriter writer;           /* Writer object */
219846  Fts5StructureSegment *pSeg;     /* Output segment */
219847  Fts5Buffer term;
219848  int bOldest;                    /* True if the output segment is the oldest */
219849  int eDetail = p->pConfig->eDetail;
219850  const int flags = FTS5INDEX_QUERY_NOOUTPUT;
219851  int bTermWritten = 0;           /* True if current term already output */
219852
219853  assert( iLvl<pStruct->nLevel );
219854  assert( pLvl->nMerge<=pLvl->nSeg );
219855
219856  memset(&writer, 0, sizeof(Fts5SegWriter));
219857  memset(&term, 0, sizeof(Fts5Buffer));
219858  if( pLvl->nMerge ){
219859    pLvlOut = &pStruct->aLevel[iLvl+1];
219860    assert( pLvlOut->nSeg>0 );
219861    nInput = pLvl->nMerge;
219862    pSeg = &pLvlOut->aSeg[pLvlOut->nSeg-1];
219863
219864    fts5WriteInit(p, &writer, pSeg->iSegid);
219865    writer.writer.pgno = pSeg->pgnoLast+1;
219866    writer.iBtPage = 0;
219867  }else{
219868    int iSegid = fts5AllocateSegid(p, pStruct);
219869
219870    /* Extend the Fts5Structure object as required to ensure the output
219871    ** segment exists. */
219872    if( iLvl==pStruct->nLevel-1 ){
219873      fts5StructureAddLevel(&p->rc, ppStruct);
219874      pStruct = *ppStruct;
219875    }
219876    fts5StructureExtendLevel(&p->rc, pStruct, iLvl+1, 1, 0);
219877    if( p->rc ) return;
219878    pLvl = &pStruct->aLevel[iLvl];
219879    pLvlOut = &pStruct->aLevel[iLvl+1];
219880
219881    fts5WriteInit(p, &writer, iSegid);
219882
219883    /* Add the new segment to the output level */
219884    pSeg = &pLvlOut->aSeg[pLvlOut->nSeg];
219885    pLvlOut->nSeg++;
219886    pSeg->pgnoFirst = 1;
219887    pSeg->iSegid = iSegid;
219888    pStruct->nSegment++;
219889
219890    /* Read input from all segments in the input level */
219891    nInput = pLvl->nSeg;
219892  }
219893  bOldest = (pLvlOut->nSeg==1 && pStruct->nLevel==iLvl+2);
219894
219895  assert( iLvl>=0 );
219896  for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, iLvl, nInput, &pIter);
219897      fts5MultiIterEof(p, pIter)==0;
219898      fts5MultiIterNext(p, pIter, 0, 0)
219899  ){
219900    Fts5SegIter *pSegIter = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
219901    int nPos;                     /* position-list size field value */
219902    int nTerm;
219903    const u8 *pTerm;
219904
219905    pTerm = fts5MultiIterTerm(pIter, &nTerm);
219906    if( nTerm!=term.n || fts5Memcmp(pTerm, term.p, nTerm) ){
219907      if( pnRem && writer.nLeafWritten>nRem ){
219908        break;
219909      }
219910      fts5BufferSet(&p->rc, &term, nTerm, pTerm);
219911      bTermWritten =0;
219912    }
219913
219914    /* Check for key annihilation. */
219915    if( pSegIter->nPos==0 && (bOldest || pSegIter->bDel==0) ) continue;
219916
219917    if( p->rc==SQLITE_OK && bTermWritten==0 ){
219918      /* This is a new term. Append a term to the output segment. */
219919      fts5WriteAppendTerm(p, &writer, nTerm, pTerm);
219920      bTermWritten = 1;
219921    }
219922
219923    /* Append the rowid to the output */
219924    /* WRITEPOSLISTSIZE */
219925    fts5WriteAppendRowid(p, &writer, fts5MultiIterRowid(pIter));
219926
219927    if( eDetail==FTS5_DETAIL_NONE ){
219928      if( pSegIter->bDel ){
219929        fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
219930        if( pSegIter->nPos>0 ){
219931          fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
219932        }
219933      }
219934    }else{
219935      /* Append the position-list data to the output */
219936      nPos = pSegIter->nPos*2 + pSegIter->bDel;
219937      fts5BufferAppendVarint(&p->rc, &writer.writer.buf, nPos);
219938      fts5ChunkIterate(p, pSegIter, (void*)&writer, fts5MergeChunkCallback);
219939    }
219940  }
219941
219942  /* Flush the last leaf page to disk. Set the output segment b-tree height
219943  ** and last leaf page number at the same time.  */
219944  fts5WriteFinish(p, &writer, &pSeg->pgnoLast);
219945
219946  if( fts5MultiIterEof(p, pIter) ){
219947    int i;
219948
219949    /* Remove the redundant segments from the %_data table */
219950    for(i=0; i<nInput; i++){
219951      fts5DataRemoveSegment(p, pLvl->aSeg[i].iSegid);
219952    }
219953
219954    /* Remove the redundant segments from the input level */
219955    if( pLvl->nSeg!=nInput ){
219956      int nMove = (pLvl->nSeg - nInput) * sizeof(Fts5StructureSegment);
219957      memmove(pLvl->aSeg, &pLvl->aSeg[nInput], nMove);
219958    }
219959    pStruct->nSegment -= nInput;
219960    pLvl->nSeg -= nInput;
219961    pLvl->nMerge = 0;
219962    if( pSeg->pgnoLast==0 ){
219963      pLvlOut->nSeg--;
219964      pStruct->nSegment--;
219965    }
219966  }else{
219967    assert( pSeg->pgnoLast>0 );
219968    fts5TrimSegments(p, pIter);
219969    pLvl->nMerge = nInput;
219970  }
219971
219972  fts5MultiIterFree(pIter);
219973  fts5BufferFree(&term);
219974  if( pnRem ) *pnRem -= writer.nLeafWritten;
219975}
219976
219977/*
219978** Do up to nPg pages of automerge work on the index.
219979**
219980** Return true if any changes were actually made, or false otherwise.
219981*/
219982static int fts5IndexMerge(
219983  Fts5Index *p,                   /* FTS5 backend object */
219984  Fts5Structure **ppStruct,       /* IN/OUT: Current structure of index */
219985  int nPg,                        /* Pages of work to do */
219986  int nMin                        /* Minimum number of segments to merge */
219987){
219988  int nRem = nPg;
219989  int bRet = 0;
219990  Fts5Structure *pStruct = *ppStruct;
219991  while( nRem>0 && p->rc==SQLITE_OK ){
219992    int iLvl;                   /* To iterate through levels */
219993    int iBestLvl = 0;           /* Level offering the most input segments */
219994    int nBest = 0;              /* Number of input segments on best level */
219995
219996    /* Set iBestLvl to the level to read input segments from. */
219997    assert( pStruct->nLevel>0 );
219998    for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
219999      Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
220000      if( pLvl->nMerge ){
220001        if( pLvl->nMerge>nBest ){
220002          iBestLvl = iLvl;
220003          nBest = pLvl->nMerge;
220004        }
220005        break;
220006      }
220007      if( pLvl->nSeg>nBest ){
220008        nBest = pLvl->nSeg;
220009        iBestLvl = iLvl;
220010      }
220011    }
220012
220013    /* If nBest is still 0, then the index must be empty. */
220014#ifdef SQLITE_DEBUG
220015    for(iLvl=0; nBest==0 && iLvl<pStruct->nLevel; iLvl++){
220016      assert( pStruct->aLevel[iLvl].nSeg==0 );
220017    }
220018#endif
220019
220020    if( nBest<nMin && pStruct->aLevel[iBestLvl].nMerge==0 ){
220021      break;
220022    }
220023    bRet = 1;
220024    fts5IndexMergeLevel(p, &pStruct, iBestLvl, &nRem);
220025    if( p->rc==SQLITE_OK && pStruct->aLevel[iBestLvl].nMerge==0 ){
220026      fts5StructurePromote(p, iBestLvl+1, pStruct);
220027    }
220028  }
220029  *ppStruct = pStruct;
220030  return bRet;
220031}
220032
220033/*
220034** A total of nLeaf leaf pages of data has just been flushed to a level-0
220035** segment. This function updates the write-counter accordingly and, if
220036** necessary, performs incremental merge work.
220037**
220038** If an error occurs, set the Fts5Index.rc error code. If an error has
220039** already occurred, this function is a no-op.
220040*/
220041static void fts5IndexAutomerge(
220042  Fts5Index *p,                   /* FTS5 backend object */
220043  Fts5Structure **ppStruct,       /* IN/OUT: Current structure of index */
220044  int nLeaf                       /* Number of output leaves just written */
220045){
220046  if( p->rc==SQLITE_OK && p->pConfig->nAutomerge>0 ){
220047    Fts5Structure *pStruct = *ppStruct;
220048    u64 nWrite;                   /* Initial value of write-counter */
220049    int nWork;                    /* Number of work-quanta to perform */
220050    int nRem;                     /* Number of leaf pages left to write */
220051
220052    /* Update the write-counter. While doing so, set nWork. */
220053    nWrite = pStruct->nWriteCounter;
220054    nWork = (int)(((nWrite + nLeaf) / p->nWorkUnit) - (nWrite / p->nWorkUnit));
220055    pStruct->nWriteCounter += nLeaf;
220056    nRem = (int)(p->nWorkUnit * nWork * pStruct->nLevel);
220057
220058    fts5IndexMerge(p, ppStruct, nRem, p->pConfig->nAutomerge);
220059  }
220060}
220061
220062static void fts5IndexCrisismerge(
220063  Fts5Index *p,                   /* FTS5 backend object */
220064  Fts5Structure **ppStruct        /* IN/OUT: Current structure of index */
220065){
220066  const int nCrisis = p->pConfig->nCrisisMerge;
220067  Fts5Structure *pStruct = *ppStruct;
220068  int iLvl = 0;
220069
220070  assert( p->rc!=SQLITE_OK || pStruct->nLevel>0 );
220071  while( p->rc==SQLITE_OK && pStruct->aLevel[iLvl].nSeg>=nCrisis ){
220072    fts5IndexMergeLevel(p, &pStruct, iLvl, 0);
220073    assert( p->rc!=SQLITE_OK || pStruct->nLevel>(iLvl+1) );
220074    fts5StructurePromote(p, iLvl+1, pStruct);
220075    iLvl++;
220076  }
220077  *ppStruct = pStruct;
220078}
220079
220080static int fts5IndexReturn(Fts5Index *p){
220081  int rc = p->rc;
220082  p->rc = SQLITE_OK;
220083  return rc;
220084}
220085
220086typedef struct Fts5FlushCtx Fts5FlushCtx;
220087struct Fts5FlushCtx {
220088  Fts5Index *pIdx;
220089  Fts5SegWriter writer;
220090};
220091
220092/*
220093** Buffer aBuf[] contains a list of varints, all small enough to fit
220094** in a 32-bit integer. Return the size of the largest prefix of this
220095** list nMax bytes or less in size.
220096*/
220097static int fts5PoslistPrefix(const u8 *aBuf, int nMax){
220098  int ret;
220099  u32 dummy;
220100  ret = fts5GetVarint32(aBuf, dummy);
220101  if( ret<nMax ){
220102    while( 1 ){
220103      int i = fts5GetVarint32(&aBuf[ret], dummy);
220104      if( (ret + i) > nMax ) break;
220105      ret += i;
220106    }
220107  }
220108  return ret;
220109}
220110
220111/*
220112** Flush the contents of in-memory hash table iHash to a new level-0
220113** segment on disk. Also update the corresponding structure record.
220114**
220115** If an error occurs, set the Fts5Index.rc error code. If an error has
220116** already occurred, this function is a no-op.
220117*/
220118static void fts5FlushOneHash(Fts5Index *p){
220119  Fts5Hash *pHash = p->pHash;
220120  Fts5Structure *pStruct;
220121  int iSegid;
220122  int pgnoLast = 0;                 /* Last leaf page number in segment */
220123
220124  /* Obtain a reference to the index structure and allocate a new segment-id
220125  ** for the new level-0 segment.  */
220126  pStruct = fts5StructureRead(p);
220127  iSegid = fts5AllocateSegid(p, pStruct);
220128  fts5StructureInvalidate(p);
220129
220130  if( iSegid ){
220131    const int pgsz = p->pConfig->pgsz;
220132    int eDetail = p->pConfig->eDetail;
220133    Fts5StructureSegment *pSeg;   /* New segment within pStruct */
220134    Fts5Buffer *pBuf;             /* Buffer in which to assemble leaf page */
220135    Fts5Buffer *pPgidx;           /* Buffer in which to assemble pgidx */
220136
220137    Fts5SegWriter writer;
220138    fts5WriteInit(p, &writer, iSegid);
220139
220140    pBuf = &writer.writer.buf;
220141    pPgidx = &writer.writer.pgidx;
220142
220143    /* fts5WriteInit() should have initialized the buffers to (most likely)
220144    ** the maximum space required. */
220145    assert( p->rc || pBuf->nSpace>=(pgsz + FTS5_DATA_PADDING) );
220146    assert( p->rc || pPgidx->nSpace>=(pgsz + FTS5_DATA_PADDING) );
220147
220148    /* Begin scanning through hash table entries. This loop runs once for each
220149    ** term/doclist currently stored within the hash table. */
220150    if( p->rc==SQLITE_OK ){
220151      p->rc = sqlite3Fts5HashScanInit(pHash, 0, 0);
220152    }
220153    while( p->rc==SQLITE_OK && 0==sqlite3Fts5HashScanEof(pHash) ){
220154      const char *zTerm;          /* Buffer containing term */
220155      const u8 *pDoclist;         /* Pointer to doclist for this term */
220156      int nDoclist;               /* Size of doclist in bytes */
220157
220158      /* Write the term for this entry to disk. */
220159      sqlite3Fts5HashScanEntry(pHash, &zTerm, &pDoclist, &nDoclist);
220160      fts5WriteAppendTerm(p, &writer, (int)strlen(zTerm), (const u8*)zTerm);
220161      if( p->rc!=SQLITE_OK ) break;
220162
220163      assert( writer.bFirstRowidInPage==0 );
220164      if( pgsz>=(pBuf->n + pPgidx->n + nDoclist + 1) ){
220165        /* The entire doclist will fit on the current leaf. */
220166        fts5BufferSafeAppendBlob(pBuf, pDoclist, nDoclist);
220167      }else{
220168        i64 iRowid = 0;
220169        i64 iDelta = 0;
220170        int iOff = 0;
220171
220172        /* The entire doclist will not fit on this leaf. The following
220173        ** loop iterates through the poslists that make up the current
220174        ** doclist.  */
220175        while( p->rc==SQLITE_OK && iOff<nDoclist ){
220176          iOff += fts5GetVarint(&pDoclist[iOff], (u64*)&iDelta);
220177          iRowid += iDelta;
220178
220179          if( writer.bFirstRowidInPage ){
220180            fts5PutU16(&pBuf->p[0], (u16)pBuf->n);   /* first rowid on page */
220181            pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowid);
220182            writer.bFirstRowidInPage = 0;
220183            fts5WriteDlidxAppend(p, &writer, iRowid);
220184            if( p->rc!=SQLITE_OK ) break;
220185          }else{
220186            pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iDelta);
220187          }
220188          assert( pBuf->n<=pBuf->nSpace );
220189
220190          if( eDetail==FTS5_DETAIL_NONE ){
220191            if( iOff<nDoclist && pDoclist[iOff]==0 ){
220192              pBuf->p[pBuf->n++] = 0;
220193              iOff++;
220194              if( iOff<nDoclist && pDoclist[iOff]==0 ){
220195                pBuf->p[pBuf->n++] = 0;
220196                iOff++;
220197              }
220198            }
220199            if( (pBuf->n + pPgidx->n)>=pgsz ){
220200              fts5WriteFlushLeaf(p, &writer);
220201            }
220202          }else{
220203            int bDummy;
220204            int nPos;
220205            int nCopy = fts5GetPoslistSize(&pDoclist[iOff], &nPos, &bDummy);
220206            nCopy += nPos;
220207            if( (pBuf->n + pPgidx->n + nCopy) <= pgsz ){
220208              /* The entire poslist will fit on the current leaf. So copy
220209              ** it in one go. */
220210              fts5BufferSafeAppendBlob(pBuf, &pDoclist[iOff], nCopy);
220211            }else{
220212              /* The entire poslist will not fit on this leaf. So it needs
220213              ** to be broken into sections. The only qualification being
220214              ** that each varint must be stored contiguously.  */
220215              const u8 *pPoslist = &pDoclist[iOff];
220216              int iPos = 0;
220217              while( p->rc==SQLITE_OK ){
220218                int nSpace = pgsz - pBuf->n - pPgidx->n;
220219                int n = 0;
220220                if( (nCopy - iPos)<=nSpace ){
220221                  n = nCopy - iPos;
220222                }else{
220223                  n = fts5PoslistPrefix(&pPoslist[iPos], nSpace);
220224                }
220225                assert( n>0 );
220226                fts5BufferSafeAppendBlob(pBuf, &pPoslist[iPos], n);
220227                iPos += n;
220228                if( (pBuf->n + pPgidx->n)>=pgsz ){
220229                  fts5WriteFlushLeaf(p, &writer);
220230                }
220231                if( iPos>=nCopy ) break;
220232              }
220233            }
220234            iOff += nCopy;
220235          }
220236        }
220237      }
220238
220239      /* TODO2: Doclist terminator written here. */
220240      /* pBuf->p[pBuf->n++] = '\0'; */
220241      assert( pBuf->n<=pBuf->nSpace );
220242      if( p->rc==SQLITE_OK ) sqlite3Fts5HashScanNext(pHash);
220243    }
220244    sqlite3Fts5HashClear(pHash);
220245    fts5WriteFinish(p, &writer, &pgnoLast);
220246
220247    /* Update the Fts5Structure. It is written back to the database by the
220248    ** fts5StructureRelease() call below.  */
220249    if( pStruct->nLevel==0 ){
220250      fts5StructureAddLevel(&p->rc, &pStruct);
220251    }
220252    fts5StructureExtendLevel(&p->rc, pStruct, 0, 1, 0);
220253    if( p->rc==SQLITE_OK ){
220254      pSeg = &pStruct->aLevel[0].aSeg[ pStruct->aLevel[0].nSeg++ ];
220255      pSeg->iSegid = iSegid;
220256      pSeg->pgnoFirst = 1;
220257      pSeg->pgnoLast = pgnoLast;
220258      pStruct->nSegment++;
220259    }
220260    fts5StructurePromote(p, 0, pStruct);
220261  }
220262
220263  fts5IndexAutomerge(p, &pStruct, pgnoLast);
220264  fts5IndexCrisismerge(p, &pStruct);
220265  fts5StructureWrite(p, pStruct);
220266  fts5StructureRelease(pStruct);
220267}
220268
220269/*
220270** Flush any data stored in the in-memory hash tables to the database.
220271*/
220272static void fts5IndexFlush(Fts5Index *p){
220273  /* Unless it is empty, flush the hash table to disk */
220274  if( p->nPendingData ){
220275    assert( p->pHash );
220276    p->nPendingData = 0;
220277    fts5FlushOneHash(p);
220278  }
220279}
220280
220281static Fts5Structure *fts5IndexOptimizeStruct(
220282  Fts5Index *p,
220283  Fts5Structure *pStruct
220284){
220285  Fts5Structure *pNew = 0;
220286  sqlite3_int64 nByte = sizeof(Fts5Structure);
220287  int nSeg = pStruct->nSegment;
220288  int i;
220289
220290  /* Figure out if this structure requires optimization. A structure does
220291  ** not require optimization if either:
220292  **
220293  **  + it consists of fewer than two segments, or
220294  **  + all segments are on the same level, or
220295  **  + all segments except one are currently inputs to a merge operation.
220296  **
220297  ** In the first case, return NULL. In the second, increment the ref-count
220298  ** on *pStruct and return a copy of the pointer to it.
220299  */
220300  if( nSeg<2 ) return 0;
220301  for(i=0; i<pStruct->nLevel; i++){
220302    int nThis = pStruct->aLevel[i].nSeg;
220303    if( nThis==nSeg || (nThis==nSeg-1 && pStruct->aLevel[i].nMerge==nThis) ){
220304      fts5StructureRef(pStruct);
220305      return pStruct;
220306    }
220307    assert( pStruct->aLevel[i].nMerge<=nThis );
220308  }
220309
220310  nByte += (pStruct->nLevel+1) * sizeof(Fts5StructureLevel);
220311  pNew = (Fts5Structure*)sqlite3Fts5MallocZero(&p->rc, nByte);
220312
220313  if( pNew ){
220314    Fts5StructureLevel *pLvl;
220315    nByte = nSeg * sizeof(Fts5StructureSegment);
220316    pNew->nLevel = pStruct->nLevel+1;
220317    pNew->nRef = 1;
220318    pNew->nWriteCounter = pStruct->nWriteCounter;
220319    pLvl = &pNew->aLevel[pStruct->nLevel];
220320    pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&p->rc, nByte);
220321    if( pLvl->aSeg ){
220322      int iLvl, iSeg;
220323      int iSegOut = 0;
220324      /* Iterate through all segments, from oldest to newest. Add them to
220325      ** the new Fts5Level object so that pLvl->aSeg[0] is the oldest
220326      ** segment in the data structure.  */
220327      for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){
220328        for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
220329          pLvl->aSeg[iSegOut] = pStruct->aLevel[iLvl].aSeg[iSeg];
220330          iSegOut++;
220331        }
220332      }
220333      pNew->nSegment = pLvl->nSeg = nSeg;
220334    }else{
220335      sqlite3_free(pNew);
220336      pNew = 0;
220337    }
220338  }
220339
220340  return pNew;
220341}
220342
220343static int sqlite3Fts5IndexOptimize(Fts5Index *p){
220344  Fts5Structure *pStruct;
220345  Fts5Structure *pNew = 0;
220346
220347  assert( p->rc==SQLITE_OK );
220348  fts5IndexFlush(p);
220349  pStruct = fts5StructureRead(p);
220350  fts5StructureInvalidate(p);
220351
220352  if( pStruct ){
220353    pNew = fts5IndexOptimizeStruct(p, pStruct);
220354  }
220355  fts5StructureRelease(pStruct);
220356
220357  assert( pNew==0 || pNew->nSegment>0 );
220358  if( pNew ){
220359    int iLvl;
220360    for(iLvl=0; pNew->aLevel[iLvl].nSeg==0; iLvl++){}
220361    while( p->rc==SQLITE_OK && pNew->aLevel[iLvl].nSeg>0 ){
220362      int nRem = FTS5_OPT_WORK_UNIT;
220363      fts5IndexMergeLevel(p, &pNew, iLvl, &nRem);
220364    }
220365
220366    fts5StructureWrite(p, pNew);
220367    fts5StructureRelease(pNew);
220368  }
220369
220370  return fts5IndexReturn(p);
220371}
220372
220373/*
220374** This is called to implement the special "VALUES('merge', $nMerge)"
220375** INSERT command.
220376*/
220377static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge){
220378  Fts5Structure *pStruct = fts5StructureRead(p);
220379  if( pStruct ){
220380    int nMin = p->pConfig->nUsermerge;
220381    fts5StructureInvalidate(p);
220382    if( nMerge<0 ){
220383      Fts5Structure *pNew = fts5IndexOptimizeStruct(p, pStruct);
220384      fts5StructureRelease(pStruct);
220385      pStruct = pNew;
220386      nMin = 2;
220387      nMerge = nMerge*-1;
220388    }
220389    if( pStruct && pStruct->nLevel ){
220390      if( fts5IndexMerge(p, &pStruct, nMerge, nMin) ){
220391        fts5StructureWrite(p, pStruct);
220392      }
220393    }
220394    fts5StructureRelease(pStruct);
220395  }
220396  return fts5IndexReturn(p);
220397}
220398
220399static void fts5AppendRowid(
220400  Fts5Index *p,
220401  i64 iDelta,
220402  Fts5Iter *pUnused,
220403  Fts5Buffer *pBuf
220404){
220405  UNUSED_PARAM(pUnused);
220406  fts5BufferAppendVarint(&p->rc, pBuf, iDelta);
220407}
220408
220409static void fts5AppendPoslist(
220410  Fts5Index *p,
220411  i64 iDelta,
220412  Fts5Iter *pMulti,
220413  Fts5Buffer *pBuf
220414){
220415  int nData = pMulti->base.nData;
220416  int nByte = nData + 9 + 9 + FTS5_DATA_ZERO_PADDING;
220417  assert( nData>0 );
220418  if( p->rc==SQLITE_OK && 0==fts5BufferGrow(&p->rc, pBuf, nByte) ){
220419    fts5BufferSafeAppendVarint(pBuf, iDelta);
220420    fts5BufferSafeAppendVarint(pBuf, nData*2);
220421    fts5BufferSafeAppendBlob(pBuf, pMulti->base.pData, nData);
220422    memset(&pBuf->p[pBuf->n], 0, FTS5_DATA_ZERO_PADDING);
220423  }
220424}
220425
220426
220427static void fts5DoclistIterNext(Fts5DoclistIter *pIter){
220428  u8 *p = pIter->aPoslist + pIter->nSize + pIter->nPoslist;
220429
220430  assert( pIter->aPoslist );
220431  if( p>=pIter->aEof ){
220432    pIter->aPoslist = 0;
220433  }else{
220434    i64 iDelta;
220435
220436    p += fts5GetVarint(p, (u64*)&iDelta);
220437    pIter->iRowid += iDelta;
220438
220439    /* Read position list size */
220440    if( p[0] & 0x80 ){
220441      int nPos;
220442      pIter->nSize = fts5GetVarint32(p, nPos);
220443      pIter->nPoslist = (nPos>>1);
220444    }else{
220445      pIter->nPoslist = ((int)(p[0])) >> 1;
220446      pIter->nSize = 1;
220447    }
220448
220449    pIter->aPoslist = p;
220450  }
220451}
220452
220453static void fts5DoclistIterInit(
220454  Fts5Buffer *pBuf,
220455  Fts5DoclistIter *pIter
220456){
220457  memset(pIter, 0, sizeof(*pIter));
220458  pIter->aPoslist = pBuf->p;
220459  pIter->aEof = &pBuf->p[pBuf->n];
220460  fts5DoclistIterNext(pIter);
220461}
220462
220463#if 0
220464/*
220465** Append a doclist to buffer pBuf.
220466**
220467** This function assumes that space within the buffer has already been
220468** allocated.
220469*/
220470static void fts5MergeAppendDocid(
220471  Fts5Buffer *pBuf,               /* Buffer to write to */
220472  i64 *piLastRowid,               /* IN/OUT: Previous rowid written (if any) */
220473  i64 iRowid                      /* Rowid to append */
220474){
220475  assert( pBuf->n!=0 || (*piLastRowid)==0 );
220476  fts5BufferSafeAppendVarint(pBuf, iRowid - *piLastRowid);
220477  *piLastRowid = iRowid;
220478}
220479#endif
220480
220481#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) {       \
220482  assert( (pBuf)->n!=0 || (iLastRowid)==0 );                   \
220483  fts5BufferSafeAppendVarint((pBuf), (iRowid) - (iLastRowid)); \
220484  (iLastRowid) = (iRowid);                                     \
220485}
220486
220487/*
220488** Swap the contents of buffer *p1 with that of *p2.
220489*/
220490static void fts5BufferSwap(Fts5Buffer *p1, Fts5Buffer *p2){
220491  Fts5Buffer tmp = *p1;
220492  *p1 = *p2;
220493  *p2 = tmp;
220494}
220495
220496static void fts5NextRowid(Fts5Buffer *pBuf, int *piOff, i64 *piRowid){
220497  int i = *piOff;
220498  if( i>=pBuf->n ){
220499    *piOff = -1;
220500  }else{
220501    u64 iVal;
220502    *piOff = i + sqlite3Fts5GetVarint(&pBuf->p[i], &iVal);
220503    *piRowid += iVal;
220504  }
220505}
220506
220507/*
220508** This is the equivalent of fts5MergePrefixLists() for detail=none mode.
220509** In this case the buffers consist of a delta-encoded list of rowids only.
220510*/
220511static void fts5MergeRowidLists(
220512  Fts5Index *p,                   /* FTS5 backend object */
220513  Fts5Buffer *p1,                 /* First list to merge */
220514  Fts5Buffer *p2                  /* Second list to merge */
220515){
220516  int i1 = 0;
220517  int i2 = 0;
220518  i64 iRowid1 = 0;
220519  i64 iRowid2 = 0;
220520  i64 iOut = 0;
220521
220522  Fts5Buffer out;
220523  memset(&out, 0, sizeof(out));
220524  sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n);
220525  if( p->rc ) return;
220526
220527  fts5NextRowid(p1, &i1, &iRowid1);
220528  fts5NextRowid(p2, &i2, &iRowid2);
220529  while( i1>=0 || i2>=0 ){
220530    if( i1>=0 && (i2<0 || iRowid1<iRowid2) ){
220531      assert( iOut==0 || iRowid1>iOut );
220532      fts5BufferSafeAppendVarint(&out, iRowid1 - iOut);
220533      iOut = iRowid1;
220534      fts5NextRowid(p1, &i1, &iRowid1);
220535    }else{
220536      assert( iOut==0 || iRowid2>iOut );
220537      fts5BufferSafeAppendVarint(&out, iRowid2 - iOut);
220538      iOut = iRowid2;
220539      if( i1>=0 && iRowid1==iRowid2 ){
220540        fts5NextRowid(p1, &i1, &iRowid1);
220541      }
220542      fts5NextRowid(p2, &i2, &iRowid2);
220543    }
220544  }
220545
220546  fts5BufferSwap(&out, p1);
220547  fts5BufferFree(&out);
220548}
220549
220550/*
220551** Buffers p1 and p2 contain doclists. This function merges the content
220552** of the two doclists together and sets buffer p1 to the result before
220553** returning.
220554**
220555** If an error occurs, an error code is left in p->rc. If an error has
220556** already occurred, this function is a no-op.
220557*/
220558static void fts5MergePrefixLists(
220559  Fts5Index *p,                   /* FTS5 backend object */
220560  Fts5Buffer *p1,                 /* First list to merge */
220561  Fts5Buffer *p2                  /* Second list to merge */
220562){
220563  if( p2->n ){
220564    i64 iLastRowid = 0;
220565    Fts5DoclistIter i1;
220566    Fts5DoclistIter i2;
220567    Fts5Buffer out = {0, 0, 0};
220568    Fts5Buffer tmp = {0, 0, 0};
220569
220570    /* The maximum size of the output is equal to the sum of the two
220571    ** input sizes + 1 varint (9 bytes). The extra varint is because if the
220572    ** first rowid in one input is a large negative number, and the first in
220573    ** the other a non-negative number, the delta for the non-negative
220574    ** number will be larger on disk than the literal integer value
220575    ** was.
220576    **
220577    ** Or, if the input position-lists are corrupt, then the output might
220578    ** include up to 2 extra 10-byte positions created by interpreting -1
220579    ** (the value PoslistNext64() uses for EOF) as a position and appending
220580    ** it to the output. This can happen at most once for each input
220581    ** position-list, hence two 10 byte paddings.  */
220582    if( sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n + 9+10+10) ) return;
220583    fts5DoclistIterInit(p1, &i1);
220584    fts5DoclistIterInit(p2, &i2);
220585
220586    while( 1 ){
220587      if( i1.iRowid<i2.iRowid ){
220588        /* Copy entry from i1 */
220589        fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
220590        fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.nPoslist+i1.nSize);
220591        fts5DoclistIterNext(&i1);
220592        if( i1.aPoslist==0 ) break;
220593        assert( out.n<=((i1.aPoslist-p1->p) + (i2.aPoslist-p2->p)+9+10+10) );
220594      }
220595      else if( i2.iRowid!=i1.iRowid ){
220596        /* Copy entry from i2 */
220597        fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
220598        fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.nPoslist+i2.nSize);
220599        fts5DoclistIterNext(&i2);
220600        if( i2.aPoslist==0 ) break;
220601        assert( out.n<=((i1.aPoslist-p1->p) + (i2.aPoslist-p2->p)+9+10+10) );
220602      }
220603      else{
220604        /* Merge the two position lists. */
220605        i64 iPos1 = 0;
220606        i64 iPos2 = 0;
220607        int iOff1 = 0;
220608        int iOff2 = 0;
220609        u8 *a1 = &i1.aPoslist[i1.nSize];
220610        u8 *a2 = &i2.aPoslist[i2.nSize];
220611        int nCopy;
220612        u8 *aCopy;
220613
220614        i64 iPrev = 0;
220615        Fts5PoslistWriter writer;
220616        memset(&writer, 0, sizeof(writer));
220617
220618        /* See the earlier comment in this function for an explanation of why
220619        ** corrupt input position lists might cause the output to consume
220620        ** at most 20 bytes of unexpected space. */
220621        fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
220622        fts5BufferZero(&tmp);
220623        sqlite3Fts5BufferSize(&p->rc, &tmp, i1.nPoslist + i2.nPoslist + 10 + 10);
220624        if( p->rc ) break;
220625
220626        sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
220627        sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
220628        assert_nc( iPos1>=0 && iPos2>=0 );
220629
220630        if( iPos1<iPos2 ){
220631          sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
220632          sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
220633        }else{
220634          sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
220635          sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
220636        }
220637        if( iPos1>=0 && iPos2>=0 ){
220638          while( 1 ){
220639            if( iPos1<iPos2 ){
220640              if( iPos1!=iPrev ){
220641                sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
220642              }
220643              sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
220644              if( iPos1<0 ) break;
220645            }else{
220646              assert_nc( iPos2!=iPrev );
220647              sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
220648              sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
220649              if( iPos2<0 ) break;
220650            }
220651          }
220652        }
220653
220654        if( iPos1>=0 ){
220655          if( iPos1!=iPrev ){
220656            sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
220657          }
220658          aCopy = &a1[iOff1];
220659          nCopy = i1.nPoslist - iOff1;
220660        }else{
220661          assert_nc( iPos2>=0 && iPos2!=iPrev );
220662          sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
220663          aCopy = &a2[iOff2];
220664          nCopy = i2.nPoslist - iOff2;
220665        }
220666        if( nCopy>0 ){
220667          fts5BufferSafeAppendBlob(&tmp, aCopy, nCopy);
220668        }
220669
220670        /* WRITEPOSLISTSIZE */
220671        assert_nc( tmp.n<=i1.nPoslist+i2.nPoslist );
220672        assert( tmp.n<=i1.nPoslist+i2.nPoslist+10+10 );
220673        if( tmp.n>i1.nPoslist+i2.nPoslist ){
220674          if( p->rc==SQLITE_OK ) p->rc = FTS5_CORRUPT;
220675          break;
220676        }
220677        fts5BufferSafeAppendVarint(&out, tmp.n * 2);
220678        fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n);
220679        fts5DoclistIterNext(&i1);
220680        fts5DoclistIterNext(&i2);
220681        assert_nc( out.n<=(p1->n+p2->n+9) );
220682        if( i1.aPoslist==0 || i2.aPoslist==0 ) break;
220683        assert( out.n<=((i1.aPoslist-p1->p) + (i2.aPoslist-p2->p)+9+10+10) );
220684      }
220685    }
220686
220687    if( i1.aPoslist ){
220688      fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
220689      fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.aEof - i1.aPoslist);
220690    }
220691    else if( i2.aPoslist ){
220692      fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
220693      fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.aEof - i2.aPoslist);
220694    }
220695    assert_nc( out.n<=(p1->n+p2->n+9) );
220696
220697    fts5BufferSet(&p->rc, p1, out.n, out.p);
220698    fts5BufferFree(&tmp);
220699    fts5BufferFree(&out);
220700  }
220701}
220702
220703static void fts5SetupPrefixIter(
220704  Fts5Index *p,                   /* Index to read from */
220705  int bDesc,                      /* True for "ORDER BY rowid DESC" */
220706  const u8 *pToken,               /* Buffer containing prefix to match */
220707  int nToken,                     /* Size of buffer pToken in bytes */
220708  Fts5Colset *pColset,            /* Restrict matches to these columns */
220709  Fts5Iter **ppIter          /* OUT: New iterator */
220710){
220711  Fts5Structure *pStruct;
220712  Fts5Buffer *aBuf;
220713  const int nBuf = 32;
220714
220715  void (*xMerge)(Fts5Index*, Fts5Buffer*, Fts5Buffer*);
220716  void (*xAppend)(Fts5Index*, i64, Fts5Iter*, Fts5Buffer*);
220717  if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
220718    xMerge = fts5MergeRowidLists;
220719    xAppend = fts5AppendRowid;
220720  }else{
220721    xMerge = fts5MergePrefixLists;
220722    xAppend = fts5AppendPoslist;
220723  }
220724
220725  aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*nBuf);
220726  pStruct = fts5StructureRead(p);
220727
220728  if( aBuf && pStruct ){
220729    const int flags = FTS5INDEX_QUERY_SCAN
220730                    | FTS5INDEX_QUERY_SKIPEMPTY
220731                    | FTS5INDEX_QUERY_NOOUTPUT;
220732    int i;
220733    i64 iLastRowid = 0;
220734    Fts5Iter *p1 = 0;     /* Iterator used to gather data from index */
220735    Fts5Data *pData;
220736    Fts5Buffer doclist;
220737    int bNewTerm = 1;
220738
220739    memset(&doclist, 0, sizeof(doclist));
220740    fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1);
220741    fts5IterSetOutputCb(&p->rc, p1);
220742    for( /* no-op */ ;
220743        fts5MultiIterEof(p, p1)==0;
220744        fts5MultiIterNext2(p, p1, &bNewTerm)
220745    ){
220746      Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
220747      int nTerm = pSeg->term.n;
220748      const u8 *pTerm = pSeg->term.p;
220749      p1->xSetOutputs(p1, pSeg);
220750
220751      assert_nc( memcmp(pToken, pTerm, MIN(nToken, nTerm))<=0 );
220752      if( bNewTerm ){
220753        if( nTerm<nToken || memcmp(pToken, pTerm, nToken) ) break;
220754      }
220755
220756      if( p1->base.nData==0 ) continue;
220757
220758      if( p1->base.iRowid<=iLastRowid && doclist.n>0 ){
220759        for(i=0; p->rc==SQLITE_OK && doclist.n; i++){
220760          assert( i<nBuf );
220761          if( aBuf[i].n==0 ){
220762            fts5BufferSwap(&doclist, &aBuf[i]);
220763            fts5BufferZero(&doclist);
220764          }else{
220765            xMerge(p, &doclist, &aBuf[i]);
220766            fts5BufferZero(&aBuf[i]);
220767          }
220768        }
220769        iLastRowid = 0;
220770      }
220771
220772      xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist);
220773      iLastRowid = p1->base.iRowid;
220774    }
220775
220776    for(i=0; i<nBuf; i++){
220777      if( p->rc==SQLITE_OK ){
220778        xMerge(p, &doclist, &aBuf[i]);
220779      }
220780      fts5BufferFree(&aBuf[i]);
220781    }
220782    fts5MultiIterFree(p1);
220783
220784    pData = fts5IdxMalloc(p, sizeof(Fts5Data)+doclist.n+FTS5_DATA_ZERO_PADDING);
220785    if( pData ){
220786      pData->p = (u8*)&pData[1];
220787      pData->nn = pData->szLeaf = doclist.n;
220788      if( doclist.n ) memcpy(pData->p, doclist.p, doclist.n);
220789      fts5MultiIterNew2(p, pData, bDesc, ppIter);
220790    }
220791    fts5BufferFree(&doclist);
220792  }
220793
220794  fts5StructureRelease(pStruct);
220795  sqlite3_free(aBuf);
220796}
220797
220798
220799/*
220800** Indicate that all subsequent calls to sqlite3Fts5IndexWrite() pertain
220801** to the document with rowid iRowid.
220802*/
220803static int sqlite3Fts5IndexBeginWrite(Fts5Index *p, int bDelete, i64 iRowid){
220804  assert( p->rc==SQLITE_OK );
220805
220806  /* Allocate the hash table if it has not already been allocated */
220807  if( p->pHash==0 ){
220808    p->rc = sqlite3Fts5HashNew(p->pConfig, &p->pHash, &p->nPendingData);
220809  }
220810
220811  /* Flush the hash table to disk if required */
220812  if( iRowid<p->iWriteRowid
220813   || (iRowid==p->iWriteRowid && p->bDelete==0)
220814   || (p->nPendingData > p->pConfig->nHashSize)
220815  ){
220816    fts5IndexFlush(p);
220817  }
220818
220819  p->iWriteRowid = iRowid;
220820  p->bDelete = bDelete;
220821  return fts5IndexReturn(p);
220822}
220823
220824/*
220825** Commit data to disk.
220826*/
220827static int sqlite3Fts5IndexSync(Fts5Index *p){
220828  assert( p->rc==SQLITE_OK );
220829  fts5IndexFlush(p);
220830  sqlite3Fts5IndexCloseReader(p);
220831  return fts5IndexReturn(p);
220832}
220833
220834/*
220835** Discard any data stored in the in-memory hash tables. Do not write it
220836** to the database. Additionally, assume that the contents of the %_data
220837** table may have changed on disk. So any in-memory caches of %_data
220838** records must be invalidated.
220839*/
220840static int sqlite3Fts5IndexRollback(Fts5Index *p){
220841  sqlite3Fts5IndexCloseReader(p);
220842  fts5IndexDiscardData(p);
220843  fts5StructureInvalidate(p);
220844  /* assert( p->rc==SQLITE_OK ); */
220845  return SQLITE_OK;
220846}
220847
220848/*
220849** The %_data table is completely empty when this function is called. This
220850** function populates it with the initial structure objects for each index,
220851** and the initial version of the "averages" record (a zero-byte blob).
220852*/
220853static int sqlite3Fts5IndexReinit(Fts5Index *p){
220854  Fts5Structure s;
220855  fts5StructureInvalidate(p);
220856  fts5IndexDiscardData(p);
220857  memset(&s, 0, sizeof(Fts5Structure));
220858  fts5DataWrite(p, FTS5_AVERAGES_ROWID, (const u8*)"", 0);
220859  fts5StructureWrite(p, &s);
220860  return fts5IndexReturn(p);
220861}
220862
220863/*
220864** Open a new Fts5Index handle. If the bCreate argument is true, create
220865** and initialize the underlying %_data table.
220866**
220867** If successful, set *pp to point to the new object and return SQLITE_OK.
220868** Otherwise, set *pp to NULL and return an SQLite error code.
220869*/
220870static int sqlite3Fts5IndexOpen(
220871  Fts5Config *pConfig,
220872  int bCreate,
220873  Fts5Index **pp,
220874  char **pzErr
220875){
220876  int rc = SQLITE_OK;
220877  Fts5Index *p;                   /* New object */
220878
220879  *pp = p = (Fts5Index*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Index));
220880  if( rc==SQLITE_OK ){
220881    p->pConfig = pConfig;
220882    p->nWorkUnit = FTS5_WORK_UNIT;
220883    p->zDataTbl = sqlite3Fts5Mprintf(&rc, "%s_data", pConfig->zName);
220884    if( p->zDataTbl && bCreate ){
220885      rc = sqlite3Fts5CreateTable(
220886          pConfig, "data", "id INTEGER PRIMARY KEY, block BLOB", 0, pzErr
220887      );
220888      if( rc==SQLITE_OK ){
220889        rc = sqlite3Fts5CreateTable(pConfig, "idx",
220890            "segid, term, pgno, PRIMARY KEY(segid, term)",
220891            1, pzErr
220892        );
220893      }
220894      if( rc==SQLITE_OK ){
220895        rc = sqlite3Fts5IndexReinit(p);
220896      }
220897    }
220898  }
220899
220900  assert( rc!=SQLITE_OK || p->rc==SQLITE_OK );
220901  if( rc ){
220902    sqlite3Fts5IndexClose(p);
220903    *pp = 0;
220904  }
220905  return rc;
220906}
220907
220908/*
220909** Close a handle opened by an earlier call to sqlite3Fts5IndexOpen().
220910*/
220911static int sqlite3Fts5IndexClose(Fts5Index *p){
220912  int rc = SQLITE_OK;
220913  if( p ){
220914    assert( p->pReader==0 );
220915    fts5StructureInvalidate(p);
220916    sqlite3_finalize(p->pWriter);
220917    sqlite3_finalize(p->pDeleter);
220918    sqlite3_finalize(p->pIdxWriter);
220919    sqlite3_finalize(p->pIdxDeleter);
220920    sqlite3_finalize(p->pIdxSelect);
220921    sqlite3_finalize(p->pDataVersion);
220922    sqlite3Fts5HashFree(p->pHash);
220923    sqlite3_free(p->zDataTbl);
220924    sqlite3_free(p);
220925  }
220926  return rc;
220927}
220928
220929/*
220930** Argument p points to a buffer containing utf-8 text that is n bytes in
220931** size. Return the number of bytes in the nChar character prefix of the
220932** buffer, or 0 if there are less than nChar characters in total.
220933*/
220934static int sqlite3Fts5IndexCharlenToBytelen(
220935  const char *p,
220936  int nByte,
220937  int nChar
220938){
220939  int n = 0;
220940  int i;
220941  for(i=0; i<nChar; i++){
220942    if( n>=nByte ) return 0;      /* Input contains fewer than nChar chars */
220943    if( (unsigned char)p[n++]>=0xc0 ){
220944      if( n>=nByte ) return 0;
220945      while( (p[n] & 0xc0)==0x80 ){
220946        n++;
220947        if( n>=nByte ){
220948          if( i+1==nChar ) break;
220949          return 0;
220950        }
220951      }
220952    }
220953  }
220954  return n;
220955}
220956
220957/*
220958** pIn is a UTF-8 encoded string, nIn bytes in size. Return the number of
220959** unicode characters in the string.
220960*/
220961static int fts5IndexCharlen(const char *pIn, int nIn){
220962  int nChar = 0;
220963  int i = 0;
220964  while( i<nIn ){
220965    if( (unsigned char)pIn[i++]>=0xc0 ){
220966      while( i<nIn && (pIn[i] & 0xc0)==0x80 ) i++;
220967    }
220968    nChar++;
220969  }
220970  return nChar;
220971}
220972
220973/*
220974** Insert or remove data to or from the index. Each time a document is
220975** added to or removed from the index, this function is called one or more
220976** times.
220977**
220978** For an insert, it must be called once for each token in the new document.
220979** If the operation is a delete, it must be called (at least) once for each
220980** unique token in the document with an iCol value less than zero. The iPos
220981** argument is ignored for a delete.
220982*/
220983static int sqlite3Fts5IndexWrite(
220984  Fts5Index *p,                   /* Index to write to */
220985  int iCol,                       /* Column token appears in (-ve -> delete) */
220986  int iPos,                       /* Position of token within column */
220987  const char *pToken, int nToken  /* Token to add or remove to or from index */
220988){
220989  int i;                          /* Used to iterate through indexes */
220990  int rc = SQLITE_OK;             /* Return code */
220991  Fts5Config *pConfig = p->pConfig;
220992
220993  assert( p->rc==SQLITE_OK );
220994  assert( (iCol<0)==p->bDelete );
220995
220996  /* Add the entry to the main terms index. */
220997  rc = sqlite3Fts5HashWrite(
220998      p->pHash, p->iWriteRowid, iCol, iPos, FTS5_MAIN_PREFIX, pToken, nToken
220999  );
221000
221001  for(i=0; i<pConfig->nPrefix && rc==SQLITE_OK; i++){
221002    const int nChar = pConfig->aPrefix[i];
221003    int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
221004    if( nByte ){
221005      rc = sqlite3Fts5HashWrite(p->pHash,
221006          p->iWriteRowid, iCol, iPos, (char)(FTS5_MAIN_PREFIX+i+1), pToken,
221007          nByte
221008      );
221009    }
221010  }
221011
221012  return rc;
221013}
221014
221015/*
221016** Open a new iterator to iterate though all rowid that match the
221017** specified token or token prefix.
221018*/
221019static int sqlite3Fts5IndexQuery(
221020  Fts5Index *p,                   /* FTS index to query */
221021  const char *pToken, int nToken, /* Token (or prefix) to query for */
221022  int flags,                      /* Mask of FTS5INDEX_QUERY_X flags */
221023  Fts5Colset *pColset,            /* Match these columns only */
221024  Fts5IndexIter **ppIter          /* OUT: New iterator object */
221025){
221026  Fts5Config *pConfig = p->pConfig;
221027  Fts5Iter *pRet = 0;
221028  Fts5Buffer buf = {0, 0, 0};
221029
221030  /* If the QUERY_SCAN flag is set, all other flags must be clear. */
221031  assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN );
221032
221033  if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){
221034    int iIdx = 0;                 /* Index to search */
221035    if( nToken ) memcpy(&buf.p[1], pToken, nToken);
221036
221037    /* Figure out which index to search and set iIdx accordingly. If this
221038    ** is a prefix query for which there is no prefix index, set iIdx to
221039    ** greater than pConfig->nPrefix to indicate that the query will be
221040    ** satisfied by scanning multiple terms in the main index.
221041    **
221042    ** If the QUERY_TEST_NOIDX flag was specified, then this must be a
221043    ** prefix-query. Instead of using a prefix-index (if one exists),
221044    ** evaluate the prefix query using the main FTS index. This is used
221045    ** for internal sanity checking by the integrity-check in debug
221046    ** mode only.  */
221047#ifdef SQLITE_DEBUG
221048    if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX) ){
221049      assert( flags & FTS5INDEX_QUERY_PREFIX );
221050      iIdx = 1+pConfig->nPrefix;
221051    }else
221052#endif
221053    if( flags & FTS5INDEX_QUERY_PREFIX ){
221054      int nChar = fts5IndexCharlen(pToken, nToken);
221055      for(iIdx=1; iIdx<=pConfig->nPrefix; iIdx++){
221056        if( pConfig->aPrefix[iIdx-1]==nChar ) break;
221057      }
221058    }
221059
221060    if( iIdx<=pConfig->nPrefix ){
221061      /* Straight index lookup */
221062      Fts5Structure *pStruct = fts5StructureRead(p);
221063      buf.p[0] = (u8)(FTS5_MAIN_PREFIX + iIdx);
221064      if( pStruct ){
221065        fts5MultiIterNew(p, pStruct, flags | FTS5INDEX_QUERY_SKIPEMPTY,
221066            pColset, buf.p, nToken+1, -1, 0, &pRet
221067        );
221068        fts5StructureRelease(pStruct);
221069      }
221070    }else{
221071      /* Scan multiple terms in the main index */
221072      int bDesc = (flags & FTS5INDEX_QUERY_DESC)!=0;
221073      buf.p[0] = FTS5_MAIN_PREFIX;
221074      fts5SetupPrefixIter(p, bDesc, buf.p, nToken+1, pColset, &pRet);
221075      assert( p->rc!=SQLITE_OK || pRet->pColset==0 );
221076      fts5IterSetOutputCb(&p->rc, pRet);
221077      if( p->rc==SQLITE_OK ){
221078        Fts5SegIter *pSeg = &pRet->aSeg[pRet->aFirst[1].iFirst];
221079        if( pSeg->pLeaf ) pRet->xSetOutputs(pRet, pSeg);
221080      }
221081    }
221082
221083    if( p->rc ){
221084      sqlite3Fts5IterClose((Fts5IndexIter*)pRet);
221085      pRet = 0;
221086      sqlite3Fts5IndexCloseReader(p);
221087    }
221088
221089    *ppIter = (Fts5IndexIter*)pRet;
221090    sqlite3Fts5BufferFree(&buf);
221091  }
221092  return fts5IndexReturn(p);
221093}
221094
221095/*
221096** Return true if the iterator passed as the only argument is at EOF.
221097*/
221098/*
221099** Move to the next matching rowid.
221100*/
221101static int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){
221102  Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
221103  assert( pIter->pIndex->rc==SQLITE_OK );
221104  fts5MultiIterNext(pIter->pIndex, pIter, 0, 0);
221105  return fts5IndexReturn(pIter->pIndex);
221106}
221107
221108/*
221109** Move to the next matching term/rowid. Used by the fts5vocab module.
221110*/
221111static int sqlite3Fts5IterNextScan(Fts5IndexIter *pIndexIter){
221112  Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
221113  Fts5Index *p = pIter->pIndex;
221114
221115  assert( pIter->pIndex->rc==SQLITE_OK );
221116
221117  fts5MultiIterNext(p, pIter, 0, 0);
221118  if( p->rc==SQLITE_OK ){
221119    Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
221120    if( pSeg->pLeaf && pSeg->term.p[0]!=FTS5_MAIN_PREFIX ){
221121      fts5DataRelease(pSeg->pLeaf);
221122      pSeg->pLeaf = 0;
221123      pIter->base.bEof = 1;
221124    }
221125  }
221126
221127  return fts5IndexReturn(pIter->pIndex);
221128}
221129
221130/*
221131** Move to the next matching rowid that occurs at or after iMatch. The
221132** definition of "at or after" depends on whether this iterator iterates
221133** in ascending or descending rowid order.
221134*/
221135static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){
221136  Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
221137  fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch);
221138  return fts5IndexReturn(pIter->pIndex);
221139}
221140
221141/*
221142** Return the current term.
221143*/
221144static const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIndexIter, int *pn){
221145  int n;
221146  const char *z = (const char*)fts5MultiIterTerm((Fts5Iter*)pIndexIter, &n);
221147  *pn = n-1;
221148  return &z[1];
221149}
221150
221151/*
221152** Close an iterator opened by an earlier call to sqlite3Fts5IndexQuery().
221153*/
221154static void sqlite3Fts5IterClose(Fts5IndexIter *pIndexIter){
221155  if( pIndexIter ){
221156    Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
221157    Fts5Index *pIndex = pIter->pIndex;
221158    fts5MultiIterFree(pIter);
221159    sqlite3Fts5IndexCloseReader(pIndex);
221160  }
221161}
221162
221163/*
221164** Read and decode the "averages" record from the database.
221165**
221166** Parameter anSize must point to an array of size nCol, where nCol is
221167** the number of user defined columns in the FTS table.
221168*/
221169static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize){
221170  int nCol = p->pConfig->nCol;
221171  Fts5Data *pData;
221172
221173  *pnRow = 0;
221174  memset(anSize, 0, sizeof(i64) * nCol);
221175  pData = fts5DataRead(p, FTS5_AVERAGES_ROWID);
221176  if( p->rc==SQLITE_OK && pData->nn ){
221177    int i = 0;
221178    int iCol;
221179    i += fts5GetVarint(&pData->p[i], (u64*)pnRow);
221180    for(iCol=0; i<pData->nn && iCol<nCol; iCol++){
221181      i += fts5GetVarint(&pData->p[i], (u64*)&anSize[iCol]);
221182    }
221183  }
221184
221185  fts5DataRelease(pData);
221186  return fts5IndexReturn(p);
221187}
221188
221189/*
221190** Replace the current "averages" record with the contents of the buffer
221191** supplied as the second argument.
221192*/
221193static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8 *pData, int nData){
221194  assert( p->rc==SQLITE_OK );
221195  fts5DataWrite(p, FTS5_AVERAGES_ROWID, pData, nData);
221196  return fts5IndexReturn(p);
221197}
221198
221199/*
221200** Return the total number of blocks this module has read from the %_data
221201** table since it was created.
221202*/
221203static int sqlite3Fts5IndexReads(Fts5Index *p){
221204  return p->nRead;
221205}
221206
221207/*
221208** Set the 32-bit cookie value stored at the start of all structure
221209** records to the value passed as the second argument.
221210**
221211** Return SQLITE_OK if successful, or an SQLite error code if an error
221212** occurs.
221213*/
221214static int sqlite3Fts5IndexSetCookie(Fts5Index *p, int iNew){
221215  int rc;                              /* Return code */
221216  Fts5Config *pConfig = p->pConfig;    /* Configuration object */
221217  u8 aCookie[4];                       /* Binary representation of iNew */
221218  sqlite3_blob *pBlob = 0;
221219
221220  assert( p->rc==SQLITE_OK );
221221  sqlite3Fts5Put32(aCookie, iNew);
221222
221223  rc = sqlite3_blob_open(pConfig->db, pConfig->zDb, p->zDataTbl,
221224      "block", FTS5_STRUCTURE_ROWID, 1, &pBlob
221225  );
221226  if( rc==SQLITE_OK ){
221227    sqlite3_blob_write(pBlob, aCookie, 4, 0);
221228    rc = sqlite3_blob_close(pBlob);
221229  }
221230
221231  return rc;
221232}
221233
221234static int sqlite3Fts5IndexLoadConfig(Fts5Index *p){
221235  Fts5Structure *pStruct;
221236  pStruct = fts5StructureRead(p);
221237  fts5StructureRelease(pStruct);
221238  return fts5IndexReturn(p);
221239}
221240
221241
221242/*************************************************************************
221243**************************************************************************
221244** Below this point is the implementation of the integrity-check
221245** functionality.
221246*/
221247
221248/*
221249** Return a simple checksum value based on the arguments.
221250*/
221251static u64 sqlite3Fts5IndexEntryCksum(
221252  i64 iRowid,
221253  int iCol,
221254  int iPos,
221255  int iIdx,
221256  const char *pTerm,
221257  int nTerm
221258){
221259  int i;
221260  u64 ret = iRowid;
221261  ret += (ret<<3) + iCol;
221262  ret += (ret<<3) + iPos;
221263  if( iIdx>=0 ) ret += (ret<<3) + (FTS5_MAIN_PREFIX + iIdx);
221264  for(i=0; i<nTerm; i++) ret += (ret<<3) + pTerm[i];
221265  return ret;
221266}
221267
221268#ifdef SQLITE_DEBUG
221269/*
221270** This function is purely an internal test. It does not contribute to
221271** FTS functionality, or even the integrity-check, in any way.
221272**
221273** Instead, it tests that the same set of pgno/rowid combinations are
221274** visited regardless of whether the doclist-index identified by parameters
221275** iSegid/iLeaf is iterated in forwards or reverse order.
221276*/
221277static void fts5TestDlidxReverse(
221278  Fts5Index *p,
221279  int iSegid,                     /* Segment id to load from */
221280  int iLeaf                       /* Load doclist-index for this leaf */
221281){
221282  Fts5DlidxIter *pDlidx = 0;
221283  u64 cksum1 = 13;
221284  u64 cksum2 = 13;
221285
221286  for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iLeaf);
221287      fts5DlidxIterEof(p, pDlidx)==0;
221288      fts5DlidxIterNext(p, pDlidx)
221289  ){
221290    i64 iRowid = fts5DlidxIterRowid(pDlidx);
221291    int pgno = fts5DlidxIterPgno(pDlidx);
221292    assert( pgno>iLeaf );
221293    cksum1 += iRowid + ((i64)pgno<<32);
221294  }
221295  fts5DlidxIterFree(pDlidx);
221296  pDlidx = 0;
221297
221298  for(pDlidx=fts5DlidxIterInit(p, 1, iSegid, iLeaf);
221299      fts5DlidxIterEof(p, pDlidx)==0;
221300      fts5DlidxIterPrev(p, pDlidx)
221301  ){
221302    i64 iRowid = fts5DlidxIterRowid(pDlidx);
221303    int pgno = fts5DlidxIterPgno(pDlidx);
221304    assert( fts5DlidxIterPgno(pDlidx)>iLeaf );
221305    cksum2 += iRowid + ((i64)pgno<<32);
221306  }
221307  fts5DlidxIterFree(pDlidx);
221308  pDlidx = 0;
221309
221310  if( p->rc==SQLITE_OK && cksum1!=cksum2 ) p->rc = FTS5_CORRUPT;
221311}
221312
221313static int fts5QueryCksum(
221314  Fts5Index *p,                   /* Fts5 index object */
221315  int iIdx,
221316  const char *z,                  /* Index key to query for */
221317  int n,                          /* Size of index key in bytes */
221318  int flags,                      /* Flags for Fts5IndexQuery */
221319  u64 *pCksum                     /* IN/OUT: Checksum value */
221320){
221321  int eDetail = p->pConfig->eDetail;
221322  u64 cksum = *pCksum;
221323  Fts5IndexIter *pIter = 0;
221324  int rc = sqlite3Fts5IndexQuery(p, z, n, flags, 0, &pIter);
221325
221326  while( rc==SQLITE_OK && 0==sqlite3Fts5IterEof(pIter) ){
221327    i64 rowid = pIter->iRowid;
221328
221329    if( eDetail==FTS5_DETAIL_NONE ){
221330      cksum ^= sqlite3Fts5IndexEntryCksum(rowid, 0, 0, iIdx, z, n);
221331    }else{
221332      Fts5PoslistReader sReader;
221333      for(sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &sReader);
221334          sReader.bEof==0;
221335          sqlite3Fts5PoslistReaderNext(&sReader)
221336      ){
221337        int iCol = FTS5_POS2COLUMN(sReader.iPos);
221338        int iOff = FTS5_POS2OFFSET(sReader.iPos);
221339        cksum ^= sqlite3Fts5IndexEntryCksum(rowid, iCol, iOff, iIdx, z, n);
221340      }
221341    }
221342    if( rc==SQLITE_OK ){
221343      rc = sqlite3Fts5IterNext(pIter);
221344    }
221345  }
221346  sqlite3Fts5IterClose(pIter);
221347
221348  *pCksum = cksum;
221349  return rc;
221350}
221351
221352/*
221353** Check if buffer z[], size n bytes, contains as series of valid utf-8
221354** encoded codepoints. If so, return 0. Otherwise, if the buffer does not
221355** contain valid utf-8, return non-zero.
221356*/
221357static int fts5TestUtf8(const char *z, int n){
221358  int i = 0;
221359  assert_nc( n>0 );
221360  while( i<n ){
221361    if( (z[i] & 0x80)==0x00 ){
221362      i++;
221363    }else
221364    if( (z[i] & 0xE0)==0xC0 ){
221365      if( i+1>=n || (z[i+1] & 0xC0)!=0x80 ) return 1;
221366      i += 2;
221367    }else
221368    if( (z[i] & 0xF0)==0xE0 ){
221369      if( i+2>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1;
221370      i += 3;
221371    }else
221372    if( (z[i] & 0xF8)==0xF0 ){
221373      if( i+3>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1;
221374      if( (z[i+2] & 0xC0)!=0x80 ) return 1;
221375      i += 3;
221376    }else{
221377      return 1;
221378    }
221379  }
221380
221381  return 0;
221382}
221383
221384/*
221385** This function is also purely an internal test. It does not contribute to
221386** FTS functionality, or even the integrity-check, in any way.
221387*/
221388static void fts5TestTerm(
221389  Fts5Index *p,
221390  Fts5Buffer *pPrev,              /* Previous term */
221391  const char *z, int n,           /* Possibly new term to test */
221392  u64 expected,
221393  u64 *pCksum
221394){
221395  int rc = p->rc;
221396  if( pPrev->n==0 ){
221397    fts5BufferSet(&rc, pPrev, n, (const u8*)z);
221398  }else
221399  if( rc==SQLITE_OK && (pPrev->n!=n || memcmp(pPrev->p, z, n)) ){
221400    u64 cksum3 = *pCksum;
221401    const char *zTerm = (const char*)&pPrev->p[1];  /* term sans prefix-byte */
221402    int nTerm = pPrev->n-1;            /* Size of zTerm in bytes */
221403    int iIdx = (pPrev->p[0] - FTS5_MAIN_PREFIX);
221404    int flags = (iIdx==0 ? 0 : FTS5INDEX_QUERY_PREFIX);
221405    u64 ck1 = 0;
221406    u64 ck2 = 0;
221407
221408    /* Check that the results returned for ASC and DESC queries are
221409    ** the same. If not, call this corruption.  */
221410    rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, flags, &ck1);
221411    if( rc==SQLITE_OK ){
221412      int f = flags|FTS5INDEX_QUERY_DESC;
221413      rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
221414    }
221415    if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
221416
221417    /* If this is a prefix query, check that the results returned if the
221418    ** the index is disabled are the same. In both ASC and DESC order.
221419    **
221420    ** This check may only be performed if the hash table is empty. This
221421    ** is because the hash table only supports a single scan query at
221422    ** a time, and the multi-iter loop from which this function is called
221423    ** is already performing such a scan.
221424    **
221425    ** Also only do this if buffer zTerm contains nTerm bytes of valid
221426    ** utf-8. Otherwise, the last part of the buffer contents might contain
221427    ** a non-utf-8 sequence that happens to be a prefix of a valid utf-8
221428    ** character stored in the main fts index, which will cause the
221429    ** test to fail.  */
221430    if( p->nPendingData==0 && 0==fts5TestUtf8(zTerm, nTerm) ){
221431      if( iIdx>0 && rc==SQLITE_OK ){
221432        int f = flags|FTS5INDEX_QUERY_TEST_NOIDX;
221433        ck2 = 0;
221434        rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
221435        if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
221436      }
221437      if( iIdx>0 && rc==SQLITE_OK ){
221438        int f = flags|FTS5INDEX_QUERY_TEST_NOIDX|FTS5INDEX_QUERY_DESC;
221439        ck2 = 0;
221440        rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
221441        if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
221442      }
221443    }
221444
221445    cksum3 ^= ck1;
221446    fts5BufferSet(&rc, pPrev, n, (const u8*)z);
221447
221448    if( rc==SQLITE_OK && cksum3!=expected ){
221449      rc = FTS5_CORRUPT;
221450    }
221451    *pCksum = cksum3;
221452  }
221453  p->rc = rc;
221454}
221455
221456#else
221457# define fts5TestDlidxReverse(x,y,z)
221458# define fts5TestTerm(u,v,w,x,y,z)
221459#endif
221460
221461/*
221462** Check that:
221463**
221464**   1) All leaves of pSeg between iFirst and iLast (inclusive) exist and
221465**      contain zero terms.
221466**   2) All leaves of pSeg between iNoRowid and iLast (inclusive) exist and
221467**      contain zero rowids.
221468*/
221469static void fts5IndexIntegrityCheckEmpty(
221470  Fts5Index *p,
221471  Fts5StructureSegment *pSeg,     /* Segment to check internal consistency */
221472  int iFirst,
221473  int iNoRowid,
221474  int iLast
221475){
221476  int i;
221477
221478  /* Now check that the iter.nEmpty leaves following the current leaf
221479  ** (a) exist and (b) contain no terms. */
221480  for(i=iFirst; p->rc==SQLITE_OK && i<=iLast; i++){
221481    Fts5Data *pLeaf = fts5DataRead(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, i));
221482    if( pLeaf ){
221483      if( !fts5LeafIsTermless(pLeaf) ) p->rc = FTS5_CORRUPT;
221484      if( i>=iNoRowid && 0!=fts5LeafFirstRowidOff(pLeaf) ) p->rc = FTS5_CORRUPT;
221485    }
221486    fts5DataRelease(pLeaf);
221487  }
221488}
221489
221490static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){
221491  int iTermOff = 0;
221492  int ii;
221493
221494  Fts5Buffer buf1 = {0,0,0};
221495  Fts5Buffer buf2 = {0,0,0};
221496
221497  ii = pLeaf->szLeaf;
221498  while( ii<pLeaf->nn && p->rc==SQLITE_OK ){
221499    int res;
221500    int iOff;
221501    int nIncr;
221502
221503    ii += fts5GetVarint32(&pLeaf->p[ii], nIncr);
221504    iTermOff += nIncr;
221505    iOff = iTermOff;
221506
221507    if( iOff>=pLeaf->szLeaf ){
221508      p->rc = FTS5_CORRUPT;
221509    }else if( iTermOff==nIncr ){
221510      int nByte;
221511      iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
221512      if( (iOff+nByte)>pLeaf->szLeaf ){
221513        p->rc = FTS5_CORRUPT;
221514      }else{
221515        fts5BufferSet(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
221516      }
221517    }else{
221518      int nKeep, nByte;
221519      iOff += fts5GetVarint32(&pLeaf->p[iOff], nKeep);
221520      iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
221521      if( nKeep>buf1.n || (iOff+nByte)>pLeaf->szLeaf ){
221522        p->rc = FTS5_CORRUPT;
221523      }else{
221524        buf1.n = nKeep;
221525        fts5BufferAppendBlob(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
221526      }
221527
221528      if( p->rc==SQLITE_OK ){
221529        res = fts5BufferCompare(&buf1, &buf2);
221530        if( res<=0 ) p->rc = FTS5_CORRUPT;
221531      }
221532    }
221533    fts5BufferSet(&p->rc, &buf2, buf1.n, buf1.p);
221534  }
221535
221536  fts5BufferFree(&buf1);
221537  fts5BufferFree(&buf2);
221538}
221539
221540static void fts5IndexIntegrityCheckSegment(
221541  Fts5Index *p,                   /* FTS5 backend object */
221542  Fts5StructureSegment *pSeg      /* Segment to check internal consistency */
221543){
221544  Fts5Config *pConfig = p->pConfig;
221545  sqlite3_stmt *pStmt = 0;
221546  int rc2;
221547  int iIdxPrevLeaf = pSeg->pgnoFirst-1;
221548  int iDlidxPrevLeaf = pSeg->pgnoLast;
221549
221550  if( pSeg->pgnoFirst==0 ) return;
221551
221552  fts5IndexPrepareStmt(p, &pStmt, sqlite3_mprintf(
221553      "SELECT segid, term, (pgno>>1), (pgno&1) FROM %Q.'%q_idx' WHERE segid=%d "
221554      "ORDER BY 1, 2",
221555      pConfig->zDb, pConfig->zName, pSeg->iSegid
221556  ));
221557
221558  /* Iterate through the b-tree hierarchy.  */
221559  while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
221560    i64 iRow;                     /* Rowid for this leaf */
221561    Fts5Data *pLeaf;              /* Data for this leaf */
221562
221563    const char *zIdxTerm = (const char*)sqlite3_column_blob(pStmt, 1);
221564    int nIdxTerm = sqlite3_column_bytes(pStmt, 1);
221565    int iIdxLeaf = sqlite3_column_int(pStmt, 2);
221566    int bIdxDlidx = sqlite3_column_int(pStmt, 3);
221567
221568    /* If the leaf in question has already been trimmed from the segment,
221569    ** ignore this b-tree entry. Otherwise, load it into memory. */
221570    if( iIdxLeaf<pSeg->pgnoFirst ) continue;
221571    iRow = FTS5_SEGMENT_ROWID(pSeg->iSegid, iIdxLeaf);
221572    pLeaf = fts5LeafRead(p, iRow);
221573    if( pLeaf==0 ) break;
221574
221575    /* Check that the leaf contains at least one term, and that it is equal
221576    ** to or larger than the split-key in zIdxTerm.  Also check that if there
221577    ** is also a rowid pointer within the leaf page header, it points to a
221578    ** location before the term.  */
221579    if( pLeaf->nn<=pLeaf->szLeaf ){
221580      p->rc = FTS5_CORRUPT;
221581    }else{
221582      int iOff;                   /* Offset of first term on leaf */
221583      int iRowidOff;              /* Offset of first rowid on leaf */
221584      int nTerm;                  /* Size of term on leaf in bytes */
221585      int res;                    /* Comparison of term and split-key */
221586
221587      iOff = fts5LeafFirstTermOff(pLeaf);
221588      iRowidOff = fts5LeafFirstRowidOff(pLeaf);
221589      if( iRowidOff>=iOff || iOff>=pLeaf->szLeaf ){
221590        p->rc = FTS5_CORRUPT;
221591      }else{
221592        iOff += fts5GetVarint32(&pLeaf->p[iOff], nTerm);
221593        res = fts5Memcmp(&pLeaf->p[iOff], zIdxTerm, MIN(nTerm, nIdxTerm));
221594        if( res==0 ) res = nTerm - nIdxTerm;
221595        if( res<0 ) p->rc = FTS5_CORRUPT;
221596      }
221597
221598      fts5IntegrityCheckPgidx(p, pLeaf);
221599    }
221600    fts5DataRelease(pLeaf);
221601    if( p->rc ) break;
221602
221603    /* Now check that the iter.nEmpty leaves following the current leaf
221604    ** (a) exist and (b) contain no terms. */
221605    fts5IndexIntegrityCheckEmpty(
221606        p, pSeg, iIdxPrevLeaf+1, iDlidxPrevLeaf+1, iIdxLeaf-1
221607    );
221608    if( p->rc ) break;
221609
221610    /* If there is a doclist-index, check that it looks right. */
221611    if( bIdxDlidx ){
221612      Fts5DlidxIter *pDlidx = 0;  /* For iterating through doclist index */
221613      int iPrevLeaf = iIdxLeaf;
221614      int iSegid = pSeg->iSegid;
221615      int iPg = 0;
221616      i64 iKey;
221617
221618      for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iIdxLeaf);
221619          fts5DlidxIterEof(p, pDlidx)==0;
221620          fts5DlidxIterNext(p, pDlidx)
221621      ){
221622
221623        /* Check any rowid-less pages that occur before the current leaf. */
221624        for(iPg=iPrevLeaf+1; iPg<fts5DlidxIterPgno(pDlidx); iPg++){
221625          iKey = FTS5_SEGMENT_ROWID(iSegid, iPg);
221626          pLeaf = fts5DataRead(p, iKey);
221627          if( pLeaf ){
221628            if( fts5LeafFirstRowidOff(pLeaf)!=0 ) p->rc = FTS5_CORRUPT;
221629            fts5DataRelease(pLeaf);
221630          }
221631        }
221632        iPrevLeaf = fts5DlidxIterPgno(pDlidx);
221633
221634        /* Check that the leaf page indicated by the iterator really does
221635        ** contain the rowid suggested by the same. */
221636        iKey = FTS5_SEGMENT_ROWID(iSegid, iPrevLeaf);
221637        pLeaf = fts5DataRead(p, iKey);
221638        if( pLeaf ){
221639          i64 iRowid;
221640          int iRowidOff = fts5LeafFirstRowidOff(pLeaf);
221641          ASSERT_SZLEAF_OK(pLeaf);
221642          if( iRowidOff>=pLeaf->szLeaf ){
221643            p->rc = FTS5_CORRUPT;
221644          }else{
221645            fts5GetVarint(&pLeaf->p[iRowidOff], (u64*)&iRowid);
221646            if( iRowid!=fts5DlidxIterRowid(pDlidx) ) p->rc = FTS5_CORRUPT;
221647          }
221648          fts5DataRelease(pLeaf);
221649        }
221650      }
221651
221652      iDlidxPrevLeaf = iPg;
221653      fts5DlidxIterFree(pDlidx);
221654      fts5TestDlidxReverse(p, iSegid, iIdxLeaf);
221655    }else{
221656      iDlidxPrevLeaf = pSeg->pgnoLast;
221657      /* TODO: Check there is no doclist index */
221658    }
221659
221660    iIdxPrevLeaf = iIdxLeaf;
221661  }
221662
221663  rc2 = sqlite3_finalize(pStmt);
221664  if( p->rc==SQLITE_OK ) p->rc = rc2;
221665
221666  /* Page iter.iLeaf must now be the rightmost leaf-page in the segment */
221667#if 0
221668  if( p->rc==SQLITE_OK && iter.iLeaf!=pSeg->pgnoLast ){
221669    p->rc = FTS5_CORRUPT;
221670  }
221671#endif
221672}
221673
221674
221675/*
221676** Run internal checks to ensure that the FTS index (a) is internally
221677** consistent and (b) contains entries for which the XOR of the checksums
221678** as calculated by sqlite3Fts5IndexEntryCksum() is cksum.
221679**
221680** Return SQLITE_CORRUPT if any of the internal checks fail, or if the
221681** checksum does not match. Return SQLITE_OK if all checks pass without
221682** error, or some other SQLite error code if another error (e.g. OOM)
221683** occurs.
221684*/
221685static int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum){
221686  int eDetail = p->pConfig->eDetail;
221687  u64 cksum2 = 0;                 /* Checksum based on contents of indexes */
221688  Fts5Buffer poslist = {0,0,0};   /* Buffer used to hold a poslist */
221689  Fts5Iter *pIter;                /* Used to iterate through entire index */
221690  Fts5Structure *pStruct;         /* Index structure */
221691
221692#ifdef SQLITE_DEBUG
221693  /* Used by extra internal tests only run if NDEBUG is not defined */
221694  u64 cksum3 = 0;                 /* Checksum based on contents of indexes */
221695  Fts5Buffer term = {0,0,0};      /* Buffer used to hold most recent term */
221696#endif
221697  const int flags = FTS5INDEX_QUERY_NOOUTPUT;
221698
221699  /* Load the FTS index structure */
221700  pStruct = fts5StructureRead(p);
221701
221702  /* Check that the internal nodes of each segment match the leaves */
221703  if( pStruct ){
221704    int iLvl, iSeg;
221705    for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
221706      for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
221707        Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
221708        fts5IndexIntegrityCheckSegment(p, pSeg);
221709      }
221710    }
221711  }
221712
221713  /* The cksum argument passed to this function is a checksum calculated
221714  ** based on all expected entries in the FTS index (including prefix index
221715  ** entries). This block checks that a checksum calculated based on the
221716  ** actual contents of FTS index is identical.
221717  **
221718  ** Two versions of the same checksum are calculated. The first (stack
221719  ** variable cksum2) based on entries extracted from the full-text index
221720  ** while doing a linear scan of each individual index in turn.
221721  **
221722  ** As each term visited by the linear scans, a separate query for the
221723  ** same term is performed. cksum3 is calculated based on the entries
221724  ** extracted by these queries.
221725  */
221726  for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, -1, 0, &pIter);
221727      fts5MultiIterEof(p, pIter)==0;
221728      fts5MultiIterNext(p, pIter, 0, 0)
221729  ){
221730    int n;                      /* Size of term in bytes */
221731    i64 iPos = 0;               /* Position read from poslist */
221732    int iOff = 0;               /* Offset within poslist */
221733    i64 iRowid = fts5MultiIterRowid(pIter);
221734    char *z = (char*)fts5MultiIterTerm(pIter, &n);
221735
221736    /* If this is a new term, query for it. Update cksum3 with the results. */
221737    fts5TestTerm(p, &term, z, n, cksum2, &cksum3);
221738
221739    if( eDetail==FTS5_DETAIL_NONE ){
221740      if( 0==fts5MultiIterIsEmpty(p, pIter) ){
221741        cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, 0, 0, -1, z, n);
221742      }
221743    }else{
221744      poslist.n = 0;
221745      fts5SegiterPoslist(p, &pIter->aSeg[pIter->aFirst[1].iFirst], 0, &poslist);
221746      while( 0==sqlite3Fts5PoslistNext64(poslist.p, poslist.n, &iOff, &iPos) ){
221747        int iCol = FTS5_POS2COLUMN(iPos);
221748        int iTokOff = FTS5_POS2OFFSET(iPos);
221749        cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, iCol, iTokOff, -1, z, n);
221750      }
221751    }
221752  }
221753  fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3);
221754
221755  fts5MultiIterFree(pIter);
221756  if( p->rc==SQLITE_OK && cksum!=cksum2 ) p->rc = FTS5_CORRUPT;
221757
221758  fts5StructureRelease(pStruct);
221759#ifdef SQLITE_DEBUG
221760  fts5BufferFree(&term);
221761#endif
221762  fts5BufferFree(&poslist);
221763  return fts5IndexReturn(p);
221764}
221765
221766/*************************************************************************
221767**************************************************************************
221768** Below this point is the implementation of the fts5_decode() scalar
221769** function only.
221770*/
221771
221772/*
221773** Decode a segment-data rowid from the %_data table. This function is
221774** the opposite of macro FTS5_SEGMENT_ROWID().
221775*/
221776static void fts5DecodeRowid(
221777  i64 iRowid,                     /* Rowid from %_data table */
221778  int *piSegid,                   /* OUT: Segment id */
221779  int *pbDlidx,                   /* OUT: Dlidx flag */
221780  int *piHeight,                  /* OUT: Height */
221781  int *piPgno                     /* OUT: Page number */
221782){
221783  *piPgno = (int)(iRowid & (((i64)1 << FTS5_DATA_PAGE_B) - 1));
221784  iRowid >>= FTS5_DATA_PAGE_B;
221785
221786  *piHeight = (int)(iRowid & (((i64)1 << FTS5_DATA_HEIGHT_B) - 1));
221787  iRowid >>= FTS5_DATA_HEIGHT_B;
221788
221789  *pbDlidx = (int)(iRowid & 0x0001);
221790  iRowid >>= FTS5_DATA_DLI_B;
221791
221792  *piSegid = (int)(iRowid & (((i64)1 << FTS5_DATA_ID_B) - 1));
221793}
221794
221795static void fts5DebugRowid(int *pRc, Fts5Buffer *pBuf, i64 iKey){
221796  int iSegid, iHeight, iPgno, bDlidx;       /* Rowid compenents */
221797  fts5DecodeRowid(iKey, &iSegid, &bDlidx, &iHeight, &iPgno);
221798
221799  if( iSegid==0 ){
221800    if( iKey==FTS5_AVERAGES_ROWID ){
221801      sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{averages} ");
221802    }else{
221803      sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{structure}");
221804    }
221805  }
221806  else{
221807    sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{%ssegid=%d h=%d pgno=%d}",
221808        bDlidx ? "dlidx " : "", iSegid, iHeight, iPgno
221809    );
221810  }
221811}
221812
221813static void fts5DebugStructure(
221814  int *pRc,                       /* IN/OUT: error code */
221815  Fts5Buffer *pBuf,
221816  Fts5Structure *p
221817){
221818  int iLvl, iSeg;                 /* Iterate through levels, segments */
221819
221820  for(iLvl=0; iLvl<p->nLevel; iLvl++){
221821    Fts5StructureLevel *pLvl = &p->aLevel[iLvl];
221822    sqlite3Fts5BufferAppendPrintf(pRc, pBuf,
221823        " {lvl=%d nMerge=%d nSeg=%d", iLvl, pLvl->nMerge, pLvl->nSeg
221824    );
221825    for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
221826      Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
221827      sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " {id=%d leaves=%d..%d}",
221828          pSeg->iSegid, pSeg->pgnoFirst, pSeg->pgnoLast
221829      );
221830    }
221831    sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}");
221832  }
221833}
221834
221835/*
221836** This is part of the fts5_decode() debugging aid.
221837**
221838** Arguments pBlob/nBlob contain a serialized Fts5Structure object. This
221839** function appends a human-readable representation of the same object
221840** to the buffer passed as the second argument.
221841*/
221842static void fts5DecodeStructure(
221843  int *pRc,                       /* IN/OUT: error code */
221844  Fts5Buffer *pBuf,
221845  const u8 *pBlob, int nBlob
221846){
221847  int rc;                         /* Return code */
221848  Fts5Structure *p = 0;           /* Decoded structure object */
221849
221850  rc = fts5StructureDecode(pBlob, nBlob, 0, &p);
221851  if( rc!=SQLITE_OK ){
221852    *pRc = rc;
221853    return;
221854  }
221855
221856  fts5DebugStructure(pRc, pBuf, p);
221857  fts5StructureRelease(p);
221858}
221859
221860/*
221861** This is part of the fts5_decode() debugging aid.
221862**
221863** Arguments pBlob/nBlob contain an "averages" record. This function
221864** appends a human-readable representation of record to the buffer passed
221865** as the second argument.
221866*/
221867static void fts5DecodeAverages(
221868  int *pRc,                       /* IN/OUT: error code */
221869  Fts5Buffer *pBuf,
221870  const u8 *pBlob, int nBlob
221871){
221872  int i = 0;
221873  const char *zSpace = "";
221874
221875  while( i<nBlob ){
221876    u64 iVal;
221877    i += sqlite3Fts5GetVarint(&pBlob[i], &iVal);
221878    sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "%s%d", zSpace, (int)iVal);
221879    zSpace = " ";
221880  }
221881}
221882
221883/*
221884** Buffer (a/n) is assumed to contain a list of serialized varints. Read
221885** each varint and append its string representation to buffer pBuf. Return
221886** after either the input buffer is exhausted or a 0 value is read.
221887**
221888** The return value is the number of bytes read from the input buffer.
221889*/
221890static int fts5DecodePoslist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
221891  int iOff = 0;
221892  while( iOff<n ){
221893    int iVal;
221894    iOff += fts5GetVarint32(&a[iOff], iVal);
221895    sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %d", iVal);
221896  }
221897  return iOff;
221898}
221899
221900/*
221901** The start of buffer (a/n) contains the start of a doclist. The doclist
221902** may or may not finish within the buffer. This function appends a text
221903** representation of the part of the doclist that is present to buffer
221904** pBuf.
221905**
221906** The return value is the number of bytes read from the input buffer.
221907*/
221908static int fts5DecodeDoclist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
221909  i64 iDocid = 0;
221910  int iOff = 0;
221911
221912  if( n>0 ){
221913    iOff = sqlite3Fts5GetVarint(a, (u64*)&iDocid);
221914    sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
221915  }
221916  while( iOff<n ){
221917    int nPos;
221918    int bDel;
221919    iOff += fts5GetPoslistSize(&a[iOff], &nPos, &bDel);
221920    sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " nPos=%d%s", nPos, bDel?"*":"");
221921    iOff += fts5DecodePoslist(pRc, pBuf, &a[iOff], MIN(n-iOff, nPos));
221922    if( iOff<n ){
221923      i64 iDelta;
221924      iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&iDelta);
221925      iDocid += iDelta;
221926      sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
221927    }
221928  }
221929
221930  return iOff;
221931}
221932
221933/*
221934** This function is part of the fts5_decode() debugging function. It is
221935** only ever used with detail=none tables.
221936**
221937** Buffer (pData/nData) contains a doclist in the format used by detail=none
221938** tables. This function appends a human-readable version of that list to
221939** buffer pBuf.
221940**
221941** If *pRc is other than SQLITE_OK when this function is called, it is a
221942** no-op. If an OOM or other error occurs within this function, *pRc is
221943** set to an SQLite error code before returning. The final state of buffer
221944** pBuf is undefined in this case.
221945*/
221946static void fts5DecodeRowidList(
221947  int *pRc,                       /* IN/OUT: Error code */
221948  Fts5Buffer *pBuf,               /* Buffer to append text to */
221949  const u8 *pData, int nData      /* Data to decode list-of-rowids from */
221950){
221951  int i = 0;
221952  i64 iRowid = 0;
221953
221954  while( i<nData ){
221955    const char *zApp = "";
221956    u64 iVal;
221957    i += sqlite3Fts5GetVarint(&pData[i], &iVal);
221958    iRowid += iVal;
221959
221960    if( i<nData && pData[i]==0x00 ){
221961      i++;
221962      if( i<nData && pData[i]==0x00 ){
221963        i++;
221964        zApp = "+";
221965      }else{
221966        zApp = "*";
221967      }
221968    }
221969
221970    sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %lld%s", iRowid, zApp);
221971  }
221972}
221973
221974/*
221975** The implementation of user-defined scalar function fts5_decode().
221976*/
221977static void fts5DecodeFunction(
221978  sqlite3_context *pCtx,          /* Function call context */
221979  int nArg,                       /* Number of args (always 2) */
221980  sqlite3_value **apVal           /* Function arguments */
221981){
221982  i64 iRowid;                     /* Rowid for record being decoded */
221983  int iSegid,iHeight,iPgno,bDlidx;/* Rowid components */
221984  const u8 *aBlob; int n;         /* Record to decode */
221985  u8 *a = 0;
221986  Fts5Buffer s;                   /* Build up text to return here */
221987  int rc = SQLITE_OK;             /* Return code */
221988  sqlite3_int64 nSpace = 0;
221989  int eDetailNone = (sqlite3_user_data(pCtx)!=0);
221990
221991  assert( nArg==2 );
221992  UNUSED_PARAM(nArg);
221993  memset(&s, 0, sizeof(Fts5Buffer));
221994  iRowid = sqlite3_value_int64(apVal[0]);
221995
221996  /* Make a copy of the second argument (a blob) in aBlob[]. The aBlob[]
221997  ** copy is followed by FTS5_DATA_ZERO_PADDING 0x00 bytes, which prevents
221998  ** buffer overreads even if the record is corrupt.  */
221999  n = sqlite3_value_bytes(apVal[1]);
222000  aBlob = sqlite3_value_blob(apVal[1]);
222001  nSpace = n + FTS5_DATA_ZERO_PADDING;
222002  a = (u8*)sqlite3Fts5MallocZero(&rc, nSpace);
222003  if( a==0 ) goto decode_out;
222004  if( n>0 ) memcpy(a, aBlob, n);
222005
222006  fts5DecodeRowid(iRowid, &iSegid, &bDlidx, &iHeight, &iPgno);
222007
222008  fts5DebugRowid(&rc, &s, iRowid);
222009  if( bDlidx ){
222010    Fts5Data dlidx;
222011    Fts5DlidxLvl lvl;
222012
222013    dlidx.p = a;
222014    dlidx.nn = n;
222015
222016    memset(&lvl, 0, sizeof(Fts5DlidxLvl));
222017    lvl.pData = &dlidx;
222018    lvl.iLeafPgno = iPgno;
222019
222020    for(fts5DlidxLvlNext(&lvl); lvl.bEof==0; fts5DlidxLvlNext(&lvl)){
222021      sqlite3Fts5BufferAppendPrintf(&rc, &s,
222022          " %d(%lld)", lvl.iLeafPgno, lvl.iRowid
222023      );
222024    }
222025  }else if( iSegid==0 ){
222026    if( iRowid==FTS5_AVERAGES_ROWID ){
222027      fts5DecodeAverages(&rc, &s, a, n);
222028    }else{
222029      fts5DecodeStructure(&rc, &s, a, n);
222030    }
222031  }else if( eDetailNone ){
222032    Fts5Buffer term;              /* Current term read from page */
222033    int szLeaf;
222034    int iPgidxOff = szLeaf = fts5GetU16(&a[2]);
222035    int iTermOff;
222036    int nKeep = 0;
222037    int iOff;
222038
222039    memset(&term, 0, sizeof(Fts5Buffer));
222040
222041    /* Decode any entries that occur before the first term. */
222042    if( szLeaf<n ){
222043      iPgidxOff += fts5GetVarint32(&a[iPgidxOff], iTermOff);
222044    }else{
222045      iTermOff = szLeaf;
222046    }
222047    fts5DecodeRowidList(&rc, &s, &a[4], iTermOff-4);
222048
222049    iOff = iTermOff;
222050    while( iOff<szLeaf ){
222051      int nAppend;
222052
222053      /* Read the term data for the next term*/
222054      iOff += fts5GetVarint32(&a[iOff], nAppend);
222055      term.n = nKeep;
222056      fts5BufferAppendBlob(&rc, &term, nAppend, &a[iOff]);
222057      sqlite3Fts5BufferAppendPrintf(
222058          &rc, &s, " term=%.*s", term.n, (const char*)term.p
222059      );
222060      iOff += nAppend;
222061
222062      /* Figure out where the doclist for this term ends */
222063      if( iPgidxOff<n ){
222064        int nIncr;
222065        iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nIncr);
222066        iTermOff += nIncr;
222067      }else{
222068        iTermOff = szLeaf;
222069      }
222070
222071      fts5DecodeRowidList(&rc, &s, &a[iOff], iTermOff-iOff);
222072      iOff = iTermOff;
222073      if( iOff<szLeaf ){
222074        iOff += fts5GetVarint32(&a[iOff], nKeep);
222075      }
222076    }
222077
222078    fts5BufferFree(&term);
222079  }else{
222080    Fts5Buffer term;              /* Current term read from page */
222081    int szLeaf;                   /* Offset of pgidx in a[] */
222082    int iPgidxOff;
222083    int iPgidxPrev = 0;           /* Previous value read from pgidx */
222084    int iTermOff = 0;
222085    int iRowidOff = 0;
222086    int iOff;
222087    int nDoclist;
222088
222089    memset(&term, 0, sizeof(Fts5Buffer));
222090
222091    if( n<4 ){
222092      sqlite3Fts5BufferSet(&rc, &s, 7, (const u8*)"corrupt");
222093      goto decode_out;
222094    }else{
222095      iRowidOff = fts5GetU16(&a[0]);
222096      iPgidxOff = szLeaf = fts5GetU16(&a[2]);
222097      if( iPgidxOff<n ){
222098        fts5GetVarint32(&a[iPgidxOff], iTermOff);
222099      }else if( iPgidxOff>n ){
222100        rc = FTS5_CORRUPT;
222101        goto decode_out;
222102      }
222103    }
222104
222105    /* Decode the position list tail at the start of the page */
222106    if( iRowidOff!=0 ){
222107      iOff = iRowidOff;
222108    }else if( iTermOff!=0 ){
222109      iOff = iTermOff;
222110    }else{
222111      iOff = szLeaf;
222112    }
222113    if( iOff>n ){
222114      rc = FTS5_CORRUPT;
222115      goto decode_out;
222116    }
222117    fts5DecodePoslist(&rc, &s, &a[4], iOff-4);
222118
222119    /* Decode any more doclist data that appears on the page before the
222120    ** first term. */
222121    nDoclist = (iTermOff ? iTermOff : szLeaf) - iOff;
222122    if( nDoclist+iOff>n ){
222123      rc = FTS5_CORRUPT;
222124      goto decode_out;
222125    }
222126    fts5DecodeDoclist(&rc, &s, &a[iOff], nDoclist);
222127
222128    while( iPgidxOff<n && rc==SQLITE_OK ){
222129      int bFirst = (iPgidxOff==szLeaf);     /* True for first term on page */
222130      int nByte;                            /* Bytes of data */
222131      int iEnd;
222132
222133      iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nByte);
222134      iPgidxPrev += nByte;
222135      iOff = iPgidxPrev;
222136
222137      if( iPgidxOff<n ){
222138        fts5GetVarint32(&a[iPgidxOff], nByte);
222139        iEnd = iPgidxPrev + nByte;
222140      }else{
222141        iEnd = szLeaf;
222142      }
222143      if( iEnd>szLeaf ){
222144        rc = FTS5_CORRUPT;
222145        break;
222146      }
222147
222148      if( bFirst==0 ){
222149        iOff += fts5GetVarint32(&a[iOff], nByte);
222150        if( nByte>term.n ){
222151          rc = FTS5_CORRUPT;
222152          break;
222153        }
222154        term.n = nByte;
222155      }
222156      iOff += fts5GetVarint32(&a[iOff], nByte);
222157      if( iOff+nByte>n ){
222158        rc = FTS5_CORRUPT;
222159        break;
222160      }
222161      fts5BufferAppendBlob(&rc, &term, nByte, &a[iOff]);
222162      iOff += nByte;
222163
222164      sqlite3Fts5BufferAppendPrintf(
222165          &rc, &s, " term=%.*s", term.n, (const char*)term.p
222166      );
222167      iOff += fts5DecodeDoclist(&rc, &s, &a[iOff], iEnd-iOff);
222168    }
222169
222170    fts5BufferFree(&term);
222171  }
222172
222173 decode_out:
222174  sqlite3_free(a);
222175  if( rc==SQLITE_OK ){
222176    sqlite3_result_text(pCtx, (const char*)s.p, s.n, SQLITE_TRANSIENT);
222177  }else{
222178    sqlite3_result_error_code(pCtx, rc);
222179  }
222180  fts5BufferFree(&s);
222181}
222182
222183/*
222184** The implementation of user-defined scalar function fts5_rowid().
222185*/
222186static void fts5RowidFunction(
222187  sqlite3_context *pCtx,          /* Function call context */
222188  int nArg,                       /* Number of args (always 2) */
222189  sqlite3_value **apVal           /* Function arguments */
222190){
222191  const char *zArg;
222192  if( nArg==0 ){
222193    sqlite3_result_error(pCtx, "should be: fts5_rowid(subject, ....)", -1);
222194  }else{
222195    zArg = (const char*)sqlite3_value_text(apVal[0]);
222196    if( 0==sqlite3_stricmp(zArg, "segment") ){
222197      i64 iRowid;
222198      int segid, pgno;
222199      if( nArg!=3 ){
222200        sqlite3_result_error(pCtx,
222201            "should be: fts5_rowid('segment', segid, pgno))", -1
222202        );
222203      }else{
222204        segid = sqlite3_value_int(apVal[1]);
222205        pgno = sqlite3_value_int(apVal[2]);
222206        iRowid = FTS5_SEGMENT_ROWID(segid, pgno);
222207        sqlite3_result_int64(pCtx, iRowid);
222208      }
222209    }else{
222210      sqlite3_result_error(pCtx,
222211        "first arg to fts5_rowid() must be 'segment'" , -1
222212      );
222213    }
222214  }
222215}
222216
222217/*
222218** This is called as part of registering the FTS5 module with database
222219** connection db. It registers several user-defined scalar functions useful
222220** with FTS5.
222221**
222222** If successful, SQLITE_OK is returned. If an error occurs, some other
222223** SQLite error code is returned instead.
222224*/
222225static int sqlite3Fts5IndexInit(sqlite3 *db){
222226  int rc = sqlite3_create_function(
222227      db, "fts5_decode", 2, SQLITE_UTF8, 0, fts5DecodeFunction, 0, 0
222228  );
222229
222230  if( rc==SQLITE_OK ){
222231    rc = sqlite3_create_function(
222232        db, "fts5_decode_none", 2,
222233        SQLITE_UTF8, (void*)db, fts5DecodeFunction, 0, 0
222234    );
222235  }
222236
222237  if( rc==SQLITE_OK ){
222238    rc = sqlite3_create_function(
222239        db, "fts5_rowid", -1, SQLITE_UTF8, 0, fts5RowidFunction, 0, 0
222240    );
222241  }
222242  return rc;
222243}
222244
222245
222246static int sqlite3Fts5IndexReset(Fts5Index *p){
222247  assert( p->pStruct==0 || p->iStructVersion!=0 );
222248  if( fts5IndexDataVersion(p)!=p->iStructVersion ){
222249    fts5StructureInvalidate(p);
222250  }
222251  return fts5IndexReturn(p);
222252}
222253
222254/*
222255** 2014 Jun 09
222256**
222257** The author disclaims copyright to this source code.  In place of
222258** a legal notice, here is a blessing:
222259**
222260**    May you do good and not evil.
222261**    May you find forgiveness for yourself and forgive others.
222262**    May you share freely, never taking more than you give.
222263**
222264******************************************************************************
222265**
222266** This is an SQLite module implementing full-text search.
222267*/
222268
222269
222270/* #include "fts5Int.h" */
222271
222272/*
222273** This variable is set to false when running tests for which the on disk
222274** structures should not be corrupt. Otherwise, true. If it is false, extra
222275** assert() conditions in the fts5 code are activated - conditions that are
222276** only true if it is guaranteed that the fts5 database is not corrupt.
222277*/
222278SQLITE_API int sqlite3_fts5_may_be_corrupt = 1;
222279
222280
222281typedef struct Fts5Auxdata Fts5Auxdata;
222282typedef struct Fts5Auxiliary Fts5Auxiliary;
222283typedef struct Fts5Cursor Fts5Cursor;
222284typedef struct Fts5FullTable Fts5FullTable;
222285typedef struct Fts5Sorter Fts5Sorter;
222286typedef struct Fts5TokenizerModule Fts5TokenizerModule;
222287
222288/*
222289** NOTES ON TRANSACTIONS:
222290**
222291** SQLite invokes the following virtual table methods as transactions are
222292** opened and closed by the user:
222293**
222294**     xBegin():    Start of a new transaction.
222295**     xSync():     Initial part of two-phase commit.
222296**     xCommit():   Final part of two-phase commit.
222297**     xRollback(): Rollback the transaction.
222298**
222299** Anything that is required as part of a commit that may fail is performed
222300** in the xSync() callback. Current versions of SQLite ignore any errors
222301** returned by xCommit().
222302**
222303** And as sub-transactions are opened/closed:
222304**
222305**     xSavepoint(int S):  Open savepoint S.
222306**     xRelease(int S):    Commit and close savepoint S.
222307**     xRollbackTo(int S): Rollback to start of savepoint S.
222308**
222309** During a write-transaction the fts5_index.c module may cache some data
222310** in-memory. It is flushed to disk whenever xSync(), xRelease() or
222311** xSavepoint() is called. And discarded whenever xRollback() or xRollbackTo()
222312** is called.
222313**
222314** Additionally, if SQLITE_DEBUG is defined, an instance of the following
222315** structure is used to record the current transaction state. This information
222316** is not required, but it is used in the assert() statements executed by
222317** function fts5CheckTransactionState() (see below).
222318*/
222319struct Fts5TransactionState {
222320  int eState;                     /* 0==closed, 1==open, 2==synced */
222321  int iSavepoint;                 /* Number of open savepoints (0 -> none) */
222322};
222323
222324/*
222325** A single object of this type is allocated when the FTS5 module is
222326** registered with a database handle. It is used to store pointers to
222327** all registered FTS5 extensions - tokenizers and auxiliary functions.
222328*/
222329struct Fts5Global {
222330  fts5_api api;                   /* User visible part of object (see fts5.h) */
222331  sqlite3 *db;                    /* Associated database connection */
222332  i64 iNextId;                    /* Used to allocate unique cursor ids */
222333  Fts5Auxiliary *pAux;            /* First in list of all aux. functions */
222334  Fts5TokenizerModule *pTok;      /* First in list of all tokenizer modules */
222335  Fts5TokenizerModule *pDfltTok;  /* Default tokenizer module */
222336  Fts5Cursor *pCsr;               /* First in list of all open cursors */
222337};
222338
222339/*
222340** Each auxiliary function registered with the FTS5 module is represented
222341** by an object of the following type. All such objects are stored as part
222342** of the Fts5Global.pAux list.
222343*/
222344struct Fts5Auxiliary {
222345  Fts5Global *pGlobal;            /* Global context for this function */
222346  char *zFunc;                    /* Function name (nul-terminated) */
222347  void *pUserData;                /* User-data pointer */
222348  fts5_extension_function xFunc;  /* Callback function */
222349  void (*xDestroy)(void*);        /* Destructor function */
222350  Fts5Auxiliary *pNext;           /* Next registered auxiliary function */
222351};
222352
222353/*
222354** Each tokenizer module registered with the FTS5 module is represented
222355** by an object of the following type. All such objects are stored as part
222356** of the Fts5Global.pTok list.
222357*/
222358struct Fts5TokenizerModule {
222359  char *zName;                    /* Name of tokenizer */
222360  void *pUserData;                /* User pointer passed to xCreate() */
222361  fts5_tokenizer x;               /* Tokenizer functions */
222362  void (*xDestroy)(void*);        /* Destructor function */
222363  Fts5TokenizerModule *pNext;     /* Next registered tokenizer module */
222364};
222365
222366struct Fts5FullTable {
222367  Fts5Table p;                    /* Public class members from fts5Int.h */
222368  Fts5Storage *pStorage;          /* Document store */
222369  Fts5Global *pGlobal;            /* Global (connection wide) data */
222370  Fts5Cursor *pSortCsr;           /* Sort data from this cursor */
222371#ifdef SQLITE_DEBUG
222372  struct Fts5TransactionState ts;
222373#endif
222374};
222375
222376struct Fts5MatchPhrase {
222377  Fts5Buffer *pPoslist;           /* Pointer to current poslist */
222378  int nTerm;                      /* Size of phrase in terms */
222379};
222380
222381/*
222382** pStmt:
222383**   SELECT rowid, <fts> FROM <fts> ORDER BY +rank;
222384**
222385** aIdx[]:
222386**   There is one entry in the aIdx[] array for each phrase in the query,
222387**   the value of which is the offset within aPoslist[] following the last
222388**   byte of the position list for the corresponding phrase.
222389*/
222390struct Fts5Sorter {
222391  sqlite3_stmt *pStmt;
222392  i64 iRowid;                     /* Current rowid */
222393  const u8 *aPoslist;             /* Position lists for current row */
222394  int nIdx;                       /* Number of entries in aIdx[] */
222395  int aIdx[1];                    /* Offsets into aPoslist for current row */
222396};
222397
222398
222399/*
222400** Virtual-table cursor object.
222401**
222402** iSpecial:
222403**   If this is a 'special' query (refer to function fts5SpecialMatch()),
222404**   then this variable contains the result of the query.
222405**
222406** iFirstRowid, iLastRowid:
222407**   These variables are only used for FTS5_PLAN_MATCH cursors. Assuming the
222408**   cursor iterates in ascending order of rowids, iFirstRowid is the lower
222409**   limit of rowids to return, and iLastRowid the upper. In other words, the
222410**   WHERE clause in the user's query might have been:
222411**
222412**       <tbl> MATCH <expr> AND rowid BETWEEN $iFirstRowid AND $iLastRowid
222413**
222414**   If the cursor iterates in descending order of rowid, iFirstRowid
222415**   is the upper limit (i.e. the "first" rowid visited) and iLastRowid
222416**   the lower.
222417*/
222418struct Fts5Cursor {
222419  sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
222420  Fts5Cursor *pNext;              /* Next cursor in Fts5Cursor.pCsr list */
222421  int *aColumnSize;               /* Values for xColumnSize() */
222422  i64 iCsrId;                     /* Cursor id */
222423
222424  /* Zero from this point onwards on cursor reset */
222425  int ePlan;                      /* FTS5_PLAN_XXX value */
222426  int bDesc;                      /* True for "ORDER BY rowid DESC" queries */
222427  i64 iFirstRowid;                /* Return no rowids earlier than this */
222428  i64 iLastRowid;                 /* Return no rowids later than this */
222429  sqlite3_stmt *pStmt;            /* Statement used to read %_content */
222430  Fts5Expr *pExpr;                /* Expression for MATCH queries */
222431  Fts5Sorter *pSorter;            /* Sorter for "ORDER BY rank" queries */
222432  int csrflags;                   /* Mask of cursor flags (see below) */
222433  i64 iSpecial;                   /* Result of special query */
222434
222435  /* "rank" function. Populated on demand from vtab.xColumn(). */
222436  char *zRank;                    /* Custom rank function */
222437  char *zRankArgs;                /* Custom rank function args */
222438  Fts5Auxiliary *pRank;           /* Rank callback (or NULL) */
222439  int nRankArg;                   /* Number of trailing arguments for rank() */
222440  sqlite3_value **apRankArg;      /* Array of trailing arguments */
222441  sqlite3_stmt *pRankArgStmt;     /* Origin of objects in apRankArg[] */
222442
222443  /* Auxiliary data storage */
222444  Fts5Auxiliary *pAux;            /* Currently executing extension function */
222445  Fts5Auxdata *pAuxdata;          /* First in linked list of saved aux-data */
222446
222447  /* Cache used by auxiliary functions xInst() and xInstCount() */
222448  Fts5PoslistReader *aInstIter;   /* One for each phrase */
222449  int nInstAlloc;                 /* Size of aInst[] array (entries / 3) */
222450  int nInstCount;                 /* Number of phrase instances */
222451  int *aInst;                     /* 3 integers per phrase instance */
222452};
222453
222454/*
222455** Bits that make up the "idxNum" parameter passed indirectly by
222456** xBestIndex() to xFilter().
222457*/
222458#define FTS5_BI_MATCH        0x0001         /* <tbl> MATCH ? */
222459#define FTS5_BI_RANK         0x0002         /* rank MATCH ? */
222460#define FTS5_BI_ROWID_EQ     0x0004         /* rowid == ? */
222461#define FTS5_BI_ROWID_LE     0x0008         /* rowid <= ? */
222462#define FTS5_BI_ROWID_GE     0x0010         /* rowid >= ? */
222463
222464#define FTS5_BI_ORDER_RANK   0x0020
222465#define FTS5_BI_ORDER_ROWID  0x0040
222466#define FTS5_BI_ORDER_DESC   0x0080
222467
222468/*
222469** Values for Fts5Cursor.csrflags
222470*/
222471#define FTS5CSR_EOF               0x01
222472#define FTS5CSR_REQUIRE_CONTENT   0x02
222473#define FTS5CSR_REQUIRE_DOCSIZE   0x04
222474#define FTS5CSR_REQUIRE_INST      0x08
222475#define FTS5CSR_FREE_ZRANK        0x10
222476#define FTS5CSR_REQUIRE_RESEEK    0x20
222477#define FTS5CSR_REQUIRE_POSLIST   0x40
222478
222479#define BitFlagAllTest(x,y) (((x) & (y))==(y))
222480#define BitFlagTest(x,y)    (((x) & (y))!=0)
222481
222482
222483/*
222484** Macros to Set(), Clear() and Test() cursor flags.
222485*/
222486#define CsrFlagSet(pCsr, flag)   ((pCsr)->csrflags |= (flag))
222487#define CsrFlagClear(pCsr, flag) ((pCsr)->csrflags &= ~(flag))
222488#define CsrFlagTest(pCsr, flag)  ((pCsr)->csrflags & (flag))
222489
222490struct Fts5Auxdata {
222491  Fts5Auxiliary *pAux;            /* Extension to which this belongs */
222492  void *pPtr;                     /* Pointer value */
222493  void(*xDelete)(void*);          /* Destructor */
222494  Fts5Auxdata *pNext;             /* Next object in linked list */
222495};
222496
222497#ifdef SQLITE_DEBUG
222498#define FTS5_BEGIN      1
222499#define FTS5_SYNC       2
222500#define FTS5_COMMIT     3
222501#define FTS5_ROLLBACK   4
222502#define FTS5_SAVEPOINT  5
222503#define FTS5_RELEASE    6
222504#define FTS5_ROLLBACKTO 7
222505static void fts5CheckTransactionState(Fts5FullTable *p, int op, int iSavepoint){
222506  switch( op ){
222507    case FTS5_BEGIN:
222508      assert( p->ts.eState==0 );
222509      p->ts.eState = 1;
222510      p->ts.iSavepoint = -1;
222511      break;
222512
222513    case FTS5_SYNC:
222514      assert( p->ts.eState==1 );
222515      p->ts.eState = 2;
222516      break;
222517
222518    case FTS5_COMMIT:
222519      assert( p->ts.eState==2 );
222520      p->ts.eState = 0;
222521      break;
222522
222523    case FTS5_ROLLBACK:
222524      assert( p->ts.eState==1 || p->ts.eState==2 || p->ts.eState==0 );
222525      p->ts.eState = 0;
222526      break;
222527
222528    case FTS5_SAVEPOINT:
222529      assert( p->ts.eState==1 );
222530      assert( iSavepoint>=0 );
222531      assert( iSavepoint>=p->ts.iSavepoint );
222532      p->ts.iSavepoint = iSavepoint;
222533      break;
222534
222535    case FTS5_RELEASE:
222536      assert( p->ts.eState==1 );
222537      assert( iSavepoint>=0 );
222538      assert( iSavepoint<=p->ts.iSavepoint );
222539      p->ts.iSavepoint = iSavepoint-1;
222540      break;
222541
222542    case FTS5_ROLLBACKTO:
222543      assert( p->ts.eState==1 );
222544      assert( iSavepoint>=-1 );
222545      /* The following assert() can fail if another vtab strikes an error
222546      ** within an xSavepoint() call then SQLite calls xRollbackTo() - without
222547      ** having called xSavepoint() on this vtab.  */
222548      /* assert( iSavepoint<=p->ts.iSavepoint ); */
222549      p->ts.iSavepoint = iSavepoint;
222550      break;
222551  }
222552}
222553#else
222554# define fts5CheckTransactionState(x,y,z)
222555#endif
222556
222557/*
222558** Return true if pTab is a contentless table.
222559*/
222560static int fts5IsContentless(Fts5FullTable *pTab){
222561  return pTab->p.pConfig->eContent==FTS5_CONTENT_NONE;
222562}
222563
222564/*
222565** Delete a virtual table handle allocated by fts5InitVtab().
222566*/
222567static void fts5FreeVtab(Fts5FullTable *pTab){
222568  if( pTab ){
222569    sqlite3Fts5IndexClose(pTab->p.pIndex);
222570    sqlite3Fts5StorageClose(pTab->pStorage);
222571    sqlite3Fts5ConfigFree(pTab->p.pConfig);
222572    sqlite3_free(pTab);
222573  }
222574}
222575
222576/*
222577** The xDisconnect() virtual table method.
222578*/
222579static int fts5DisconnectMethod(sqlite3_vtab *pVtab){
222580  fts5FreeVtab((Fts5FullTable*)pVtab);
222581  return SQLITE_OK;
222582}
222583
222584/*
222585** The xDestroy() virtual table method.
222586*/
222587static int fts5DestroyMethod(sqlite3_vtab *pVtab){
222588  Fts5Table *pTab = (Fts5Table*)pVtab;
222589  int rc = sqlite3Fts5DropAll(pTab->pConfig);
222590  if( rc==SQLITE_OK ){
222591    fts5FreeVtab((Fts5FullTable*)pVtab);
222592  }
222593  return rc;
222594}
222595
222596/*
222597** This function is the implementation of both the xConnect and xCreate
222598** methods of the FTS3 virtual table.
222599**
222600** The argv[] array contains the following:
222601**
222602**   argv[0]   -> module name  ("fts5")
222603**   argv[1]   -> database name
222604**   argv[2]   -> table name
222605**   argv[...] -> "column name" and other module argument fields.
222606*/
222607static int fts5InitVtab(
222608  int bCreate,                    /* True for xCreate, false for xConnect */
222609  sqlite3 *db,                    /* The SQLite database connection */
222610  void *pAux,                     /* Hash table containing tokenizers */
222611  int argc,                       /* Number of elements in argv array */
222612  const char * const *argv,       /* xCreate/xConnect argument array */
222613  sqlite3_vtab **ppVTab,          /* Write the resulting vtab structure here */
222614  char **pzErr                    /* Write any error message here */
222615){
222616  Fts5Global *pGlobal = (Fts5Global*)pAux;
222617  const char **azConfig = (const char**)argv;
222618  int rc = SQLITE_OK;             /* Return code */
222619  Fts5Config *pConfig = 0;        /* Results of parsing argc/argv */
222620  Fts5FullTable *pTab = 0;        /* New virtual table object */
222621
222622  /* Allocate the new vtab object and parse the configuration */
222623  pTab = (Fts5FullTable*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5FullTable));
222624  if( rc==SQLITE_OK ){
222625    rc = sqlite3Fts5ConfigParse(pGlobal, db, argc, azConfig, &pConfig, pzErr);
222626    assert( (rc==SQLITE_OK && *pzErr==0) || pConfig==0 );
222627  }
222628  if( rc==SQLITE_OK ){
222629    pTab->p.pConfig = pConfig;
222630    pTab->pGlobal = pGlobal;
222631  }
222632
222633  /* Open the index sub-system */
222634  if( rc==SQLITE_OK ){
222635    rc = sqlite3Fts5IndexOpen(pConfig, bCreate, &pTab->p.pIndex, pzErr);
222636  }
222637
222638  /* Open the storage sub-system */
222639  if( rc==SQLITE_OK ){
222640    rc = sqlite3Fts5StorageOpen(
222641        pConfig, pTab->p.pIndex, bCreate, &pTab->pStorage, pzErr
222642    );
222643  }
222644
222645  /* Call sqlite3_declare_vtab() */
222646  if( rc==SQLITE_OK ){
222647    rc = sqlite3Fts5ConfigDeclareVtab(pConfig);
222648  }
222649
222650  /* Load the initial configuration */
222651  if( rc==SQLITE_OK ){
222652    assert( pConfig->pzErrmsg==0 );
222653    pConfig->pzErrmsg = pzErr;
222654    rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
222655    sqlite3Fts5IndexRollback(pTab->p.pIndex);
222656    pConfig->pzErrmsg = 0;
222657  }
222658
222659  if( rc!=SQLITE_OK ){
222660    fts5FreeVtab(pTab);
222661    pTab = 0;
222662  }else if( bCreate ){
222663    fts5CheckTransactionState(pTab, FTS5_BEGIN, 0);
222664  }
222665  *ppVTab = (sqlite3_vtab*)pTab;
222666  return rc;
222667}
222668
222669/*
222670** The xConnect() and xCreate() methods for the virtual table. All the
222671** work is done in function fts5InitVtab().
222672*/
222673static int fts5ConnectMethod(
222674  sqlite3 *db,                    /* Database connection */
222675  void *pAux,                     /* Pointer to tokenizer hash table */
222676  int argc,                       /* Number of elements in argv array */
222677  const char * const *argv,       /* xCreate/xConnect argument array */
222678  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
222679  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
222680){
222681  return fts5InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
222682}
222683static int fts5CreateMethod(
222684  sqlite3 *db,                    /* Database connection */
222685  void *pAux,                     /* Pointer to tokenizer hash table */
222686  int argc,                       /* Number of elements in argv array */
222687  const char * const *argv,       /* xCreate/xConnect argument array */
222688  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
222689  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
222690){
222691  return fts5InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
222692}
222693
222694/*
222695** The different query plans.
222696*/
222697#define FTS5_PLAN_MATCH          1       /* (<tbl> MATCH ?) */
222698#define FTS5_PLAN_SOURCE         2       /* A source cursor for SORTED_MATCH */
222699#define FTS5_PLAN_SPECIAL        3       /* An internal query */
222700#define FTS5_PLAN_SORTED_MATCH   4       /* (<tbl> MATCH ? ORDER BY rank) */
222701#define FTS5_PLAN_SCAN           5       /* No usable constraint */
222702#define FTS5_PLAN_ROWID          6       /* (rowid = ?) */
222703
222704/*
222705** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
222706** extension is currently being used by a version of SQLite too old to
222707** support index-info flags. In that case this function is a no-op.
222708*/
222709static void fts5SetUniqueFlag(sqlite3_index_info *pIdxInfo){
222710#if SQLITE_VERSION_NUMBER>=3008012
222711#ifndef SQLITE_CORE
222712  if( sqlite3_libversion_number()>=3008012 )
222713#endif
222714  {
222715    pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
222716  }
222717#endif
222718}
222719
222720/*
222721** Implementation of the xBestIndex method for FTS5 tables. Within the
222722** WHERE constraint, it searches for the following:
222723**
222724**   1. A MATCH constraint against the table column.
222725**   2. A MATCH constraint against the "rank" column.
222726**   3. A MATCH constraint against some other column.
222727**   4. An == constraint against the rowid column.
222728**   5. A < or <= constraint against the rowid column.
222729**   6. A > or >= constraint against the rowid column.
222730**
222731** Within the ORDER BY, the following are supported:
222732**
222733**   5. ORDER BY rank [ASC|DESC]
222734**   6. ORDER BY rowid [ASC|DESC]
222735**
222736** Information for the xFilter call is passed via both the idxNum and
222737** idxStr variables. Specifically, idxNum is a bitmask of the following
222738** flags used to encode the ORDER BY clause:
222739**
222740**     FTS5_BI_ORDER_RANK
222741**     FTS5_BI_ORDER_ROWID
222742**     FTS5_BI_ORDER_DESC
222743**
222744** idxStr is used to encode data from the WHERE clause. For each argument
222745** passed to the xFilter method, the following is appended to idxStr:
222746**
222747**   Match against table column:            "m"
222748**   Match against rank column:             "r"
222749**   Match against other column:            "<column-number>"
222750**   Equality constraint against the rowid: "="
222751**   A < or <= against the rowid:           "<"
222752**   A > or >= against the rowid:           ">"
222753**
222754** This function ensures that there is at most one "r" or "=". And that if
222755** there exists an "=" then there is no "<" or ">".
222756**
222757** Costs are assigned as follows:
222758**
222759**  a) If an unusable MATCH operator is present in the WHERE clause, the
222760**     cost is unconditionally set to 1e50 (a really big number).
222761**
222762**  a) If a MATCH operator is present, the cost depends on the other
222763**     constraints also present. As follows:
222764**
222765**       * No other constraints:         cost=1000.0
222766**       * One rowid range constraint:   cost=750.0
222767**       * Both rowid range constraints: cost=500.0
222768**       * An == rowid constraint:       cost=100.0
222769**
222770**  b) Otherwise, if there is no MATCH:
222771**
222772**       * No other constraints:         cost=1000000.0
222773**       * One rowid range constraint:   cost=750000.0
222774**       * Both rowid range constraints: cost=250000.0
222775**       * An == rowid constraint:       cost=10.0
222776**
222777** Costs are not modified by the ORDER BY clause.
222778*/
222779static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
222780  Fts5Table *pTab = (Fts5Table*)pVTab;
222781  Fts5Config *pConfig = pTab->pConfig;
222782  const int nCol = pConfig->nCol;
222783  int idxFlags = 0;               /* Parameter passed through to xFilter() */
222784  int i;
222785
222786  char *idxStr;
222787  int iIdxStr = 0;
222788  int iCons = 0;
222789
222790  int bSeenEq = 0;
222791  int bSeenGt = 0;
222792  int bSeenLt = 0;
222793  int bSeenMatch = 0;
222794  int bSeenRank = 0;
222795
222796
222797  assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH );
222798  assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH );
222799  assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
222800  assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH );
222801  assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
222802
222803  if( pConfig->bLock ){
222804    pTab->base.zErrMsg = sqlite3_mprintf(
222805        "recursively defined fts5 content table"
222806    );
222807    return SQLITE_ERROR;
222808  }
222809
222810  idxStr = (char*)sqlite3_malloc(pInfo->nConstraint * 6 + 1);
222811  if( idxStr==0 ) return SQLITE_NOMEM;
222812  pInfo->idxStr = idxStr;
222813  pInfo->needToFreeIdxStr = 1;
222814
222815  for(i=0; i<pInfo->nConstraint; i++){
222816    struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
222817    int iCol = p->iColumn;
222818    if( p->op==SQLITE_INDEX_CONSTRAINT_MATCH
222819     || (p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol>=nCol)
222820    ){
222821      /* A MATCH operator or equivalent */
222822      if( p->usable==0 || iCol<0 ){
222823        /* As there exists an unusable MATCH constraint this is an
222824        ** unusable plan. Set a prohibitively high cost. */
222825        pInfo->estimatedCost = 1e50;
222826        assert( iIdxStr < pInfo->nConstraint*6 + 1 );
222827        idxStr[iIdxStr] = 0;
222828        return SQLITE_OK;
222829      }else{
222830        if( iCol==nCol+1 ){
222831          if( bSeenRank ) continue;
222832          idxStr[iIdxStr++] = 'r';
222833          bSeenRank = 1;
222834        }else{
222835          bSeenMatch = 1;
222836          idxStr[iIdxStr++] = 'm';
222837          if( iCol<nCol ){
222838            sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol);
222839            idxStr += strlen(&idxStr[iIdxStr]);
222840            assert( idxStr[iIdxStr]=='\0' );
222841          }
222842        }
222843        pInfo->aConstraintUsage[i].argvIndex = ++iCons;
222844        pInfo->aConstraintUsage[i].omit = 1;
222845      }
222846    }
222847    else if( p->usable && bSeenEq==0
222848      && p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol<0
222849    ){
222850      idxStr[iIdxStr++] = '=';
222851      bSeenEq = 1;
222852      pInfo->aConstraintUsage[i].argvIndex = ++iCons;
222853    }
222854  }
222855
222856  if( bSeenEq==0 ){
222857    for(i=0; i<pInfo->nConstraint; i++){
222858      struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
222859      if( p->iColumn<0 && p->usable ){
222860        int op = p->op;
222861        if( op==SQLITE_INDEX_CONSTRAINT_LT || op==SQLITE_INDEX_CONSTRAINT_LE ){
222862          if( bSeenLt ) continue;
222863          idxStr[iIdxStr++] = '<';
222864          pInfo->aConstraintUsage[i].argvIndex = ++iCons;
222865          bSeenLt = 1;
222866        }else
222867        if( op==SQLITE_INDEX_CONSTRAINT_GT || op==SQLITE_INDEX_CONSTRAINT_GE ){
222868          if( bSeenGt ) continue;
222869          idxStr[iIdxStr++] = '>';
222870          pInfo->aConstraintUsage[i].argvIndex = ++iCons;
222871          bSeenGt = 1;
222872        }
222873      }
222874    }
222875  }
222876  idxStr[iIdxStr] = '\0';
222877
222878  /* Set idxFlags flags for the ORDER BY clause */
222879  if( pInfo->nOrderBy==1 ){
222880    int iSort = pInfo->aOrderBy[0].iColumn;
222881    if( iSort==(pConfig->nCol+1) && bSeenMatch ){
222882      idxFlags |= FTS5_BI_ORDER_RANK;
222883    }else if( iSort==-1 ){
222884      idxFlags |= FTS5_BI_ORDER_ROWID;
222885    }
222886    if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID) ){
222887      pInfo->orderByConsumed = 1;
222888      if( pInfo->aOrderBy[0].desc ){
222889        idxFlags |= FTS5_BI_ORDER_DESC;
222890      }
222891    }
222892  }
222893
222894  /* Calculate the estimated cost based on the flags set in idxFlags. */
222895  if( bSeenEq ){
222896    pInfo->estimatedCost = bSeenMatch ? 100.0 : 10.0;
222897    if( bSeenMatch==0 ) fts5SetUniqueFlag(pInfo);
222898  }else if( bSeenLt && bSeenGt ){
222899    pInfo->estimatedCost = bSeenMatch ? 500.0 : 250000.0;
222900  }else if( bSeenLt || bSeenGt ){
222901    pInfo->estimatedCost = bSeenMatch ? 750.0 : 750000.0;
222902  }else{
222903    pInfo->estimatedCost = bSeenMatch ? 1000.0 : 1000000.0;
222904  }
222905
222906  pInfo->idxNum = idxFlags;
222907  return SQLITE_OK;
222908}
222909
222910static int fts5NewTransaction(Fts5FullTable *pTab){
222911  Fts5Cursor *pCsr;
222912  for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
222913    if( pCsr->base.pVtab==(sqlite3_vtab*)pTab ) return SQLITE_OK;
222914  }
222915  return sqlite3Fts5StorageReset(pTab->pStorage);
222916}
222917
222918/*
222919** Implementation of xOpen method.
222920*/
222921static int fts5OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
222922  Fts5FullTable *pTab = (Fts5FullTable*)pVTab;
222923  Fts5Config *pConfig = pTab->p.pConfig;
222924  Fts5Cursor *pCsr = 0;           /* New cursor object */
222925  sqlite3_int64 nByte;            /* Bytes of space to allocate */
222926  int rc;                         /* Return code */
222927
222928  rc = fts5NewTransaction(pTab);
222929  if( rc==SQLITE_OK ){
222930    nByte = sizeof(Fts5Cursor) + pConfig->nCol * sizeof(int);
222931    pCsr = (Fts5Cursor*)sqlite3_malloc64(nByte);
222932    if( pCsr ){
222933      Fts5Global *pGlobal = pTab->pGlobal;
222934      memset(pCsr, 0, (size_t)nByte);
222935      pCsr->aColumnSize = (int*)&pCsr[1];
222936      pCsr->pNext = pGlobal->pCsr;
222937      pGlobal->pCsr = pCsr;
222938      pCsr->iCsrId = ++pGlobal->iNextId;
222939    }else{
222940      rc = SQLITE_NOMEM;
222941    }
222942  }
222943  *ppCsr = (sqlite3_vtab_cursor*)pCsr;
222944  return rc;
222945}
222946
222947static int fts5StmtType(Fts5Cursor *pCsr){
222948  if( pCsr->ePlan==FTS5_PLAN_SCAN ){
222949    return (pCsr->bDesc) ? FTS5_STMT_SCAN_DESC : FTS5_STMT_SCAN_ASC;
222950  }
222951  return FTS5_STMT_LOOKUP;
222952}
222953
222954/*
222955** This function is called after the cursor passed as the only argument
222956** is moved to point at a different row. It clears all cached data
222957** specific to the previous row stored by the cursor object.
222958*/
222959static void fts5CsrNewrow(Fts5Cursor *pCsr){
222960  CsrFlagSet(pCsr,
222961      FTS5CSR_REQUIRE_CONTENT
222962    | FTS5CSR_REQUIRE_DOCSIZE
222963    | FTS5CSR_REQUIRE_INST
222964    | FTS5CSR_REQUIRE_POSLIST
222965  );
222966}
222967
222968static void fts5FreeCursorComponents(Fts5Cursor *pCsr){
222969  Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
222970  Fts5Auxdata *pData;
222971  Fts5Auxdata *pNext;
222972
222973  sqlite3_free(pCsr->aInstIter);
222974  sqlite3_free(pCsr->aInst);
222975  if( pCsr->pStmt ){
222976    int eStmt = fts5StmtType(pCsr);
222977    sqlite3Fts5StorageStmtRelease(pTab->pStorage, eStmt, pCsr->pStmt);
222978  }
222979  if( pCsr->pSorter ){
222980    Fts5Sorter *pSorter = pCsr->pSorter;
222981    sqlite3_finalize(pSorter->pStmt);
222982    sqlite3_free(pSorter);
222983  }
222984
222985  if( pCsr->ePlan!=FTS5_PLAN_SOURCE ){
222986    sqlite3Fts5ExprFree(pCsr->pExpr);
222987  }
222988
222989  for(pData=pCsr->pAuxdata; pData; pData=pNext){
222990    pNext = pData->pNext;
222991    if( pData->xDelete ) pData->xDelete(pData->pPtr);
222992    sqlite3_free(pData);
222993  }
222994
222995  sqlite3_finalize(pCsr->pRankArgStmt);
222996  sqlite3_free(pCsr->apRankArg);
222997
222998  if( CsrFlagTest(pCsr, FTS5CSR_FREE_ZRANK) ){
222999    sqlite3_free(pCsr->zRank);
223000    sqlite3_free(pCsr->zRankArgs);
223001  }
223002
223003  sqlite3Fts5IndexCloseReader(pTab->p.pIndex);
223004  memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan - (u8*)pCsr));
223005}
223006
223007
223008/*
223009** Close the cursor.  For additional information see the documentation
223010** on the xClose method of the virtual table interface.
223011*/
223012static int fts5CloseMethod(sqlite3_vtab_cursor *pCursor){
223013  if( pCursor ){
223014    Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
223015    Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
223016    Fts5Cursor **pp;
223017
223018    fts5FreeCursorComponents(pCsr);
223019    /* Remove the cursor from the Fts5Global.pCsr list */
223020    for(pp=&pTab->pGlobal->pCsr; (*pp)!=pCsr; pp=&(*pp)->pNext);
223021    *pp = pCsr->pNext;
223022
223023    sqlite3_free(pCsr);
223024  }
223025  return SQLITE_OK;
223026}
223027
223028static int fts5SorterNext(Fts5Cursor *pCsr){
223029  Fts5Sorter *pSorter = pCsr->pSorter;
223030  int rc;
223031
223032  rc = sqlite3_step(pSorter->pStmt);
223033  if( rc==SQLITE_DONE ){
223034    rc = SQLITE_OK;
223035    CsrFlagSet(pCsr, FTS5CSR_EOF);
223036  }else if( rc==SQLITE_ROW ){
223037    const u8 *a;
223038    const u8 *aBlob;
223039    int nBlob;
223040    int i;
223041    int iOff = 0;
223042    rc = SQLITE_OK;
223043
223044    pSorter->iRowid = sqlite3_column_int64(pSorter->pStmt, 0);
223045    nBlob = sqlite3_column_bytes(pSorter->pStmt, 1);
223046    aBlob = a = sqlite3_column_blob(pSorter->pStmt, 1);
223047
223048    /* nBlob==0 in detail=none mode. */
223049    if( nBlob>0 ){
223050      for(i=0; i<(pSorter->nIdx-1); i++){
223051        int iVal;
223052        a += fts5GetVarint32(a, iVal);
223053        iOff += iVal;
223054        pSorter->aIdx[i] = iOff;
223055      }
223056      pSorter->aIdx[i] = &aBlob[nBlob] - a;
223057      pSorter->aPoslist = a;
223058    }
223059
223060    fts5CsrNewrow(pCsr);
223061  }
223062
223063  return rc;
223064}
223065
223066
223067/*
223068** Set the FTS5CSR_REQUIRE_RESEEK flag on all FTS5_PLAN_MATCH cursors
223069** open on table pTab.
223070*/
223071static void fts5TripCursors(Fts5FullTable *pTab){
223072  Fts5Cursor *pCsr;
223073  for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
223074    if( pCsr->ePlan==FTS5_PLAN_MATCH
223075     && pCsr->base.pVtab==(sqlite3_vtab*)pTab
223076    ){
223077      CsrFlagSet(pCsr, FTS5CSR_REQUIRE_RESEEK);
223078    }
223079  }
223080}
223081
223082/*
223083** If the REQUIRE_RESEEK flag is set on the cursor passed as the first
223084** argument, close and reopen all Fts5IndexIter iterators that the cursor
223085** is using. Then attempt to move the cursor to a rowid equal to or laster
223086** (in the cursors sort order - ASC or DESC) than the current rowid.
223087**
223088** If the new rowid is not equal to the old, set output parameter *pbSkip
223089** to 1 before returning. Otherwise, leave it unchanged.
223090**
223091** Return SQLITE_OK if successful or if no reseek was required, or an
223092** error code if an error occurred.
223093*/
223094static int fts5CursorReseek(Fts5Cursor *pCsr, int *pbSkip){
223095  int rc = SQLITE_OK;
223096  assert( *pbSkip==0 );
223097  if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_RESEEK) ){
223098    Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
223099    int bDesc = pCsr->bDesc;
223100    i64 iRowid = sqlite3Fts5ExprRowid(pCsr->pExpr);
223101
223102    rc = sqlite3Fts5ExprFirst(pCsr->pExpr, pTab->p.pIndex, iRowid, bDesc);
223103    if( rc==SQLITE_OK &&  iRowid!=sqlite3Fts5ExprRowid(pCsr->pExpr) ){
223104      *pbSkip = 1;
223105    }
223106
223107    CsrFlagClear(pCsr, FTS5CSR_REQUIRE_RESEEK);
223108    fts5CsrNewrow(pCsr);
223109    if( sqlite3Fts5ExprEof(pCsr->pExpr) ){
223110      CsrFlagSet(pCsr, FTS5CSR_EOF);
223111      *pbSkip = 1;
223112    }
223113  }
223114  return rc;
223115}
223116
223117
223118/*
223119** Advance the cursor to the next row in the table that matches the
223120** search criteria.
223121**
223122** Return SQLITE_OK if nothing goes wrong.  SQLITE_OK is returned
223123** even if we reach end-of-file.  The fts5EofMethod() will be called
223124** subsequently to determine whether or not an EOF was hit.
223125*/
223126static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){
223127  Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
223128  int rc;
223129
223130  assert( (pCsr->ePlan<3)==
223131          (pCsr->ePlan==FTS5_PLAN_MATCH || pCsr->ePlan==FTS5_PLAN_SOURCE)
223132  );
223133  assert( !CsrFlagTest(pCsr, FTS5CSR_EOF) );
223134
223135  if( pCsr->ePlan<3 ){
223136    int bSkip = 0;
223137    if( (rc = fts5CursorReseek(pCsr, &bSkip)) || bSkip ) return rc;
223138    rc = sqlite3Fts5ExprNext(pCsr->pExpr, pCsr->iLastRowid);
223139    CsrFlagSet(pCsr, sqlite3Fts5ExprEof(pCsr->pExpr));
223140    fts5CsrNewrow(pCsr);
223141  }else{
223142    switch( pCsr->ePlan ){
223143      case FTS5_PLAN_SPECIAL: {
223144        CsrFlagSet(pCsr, FTS5CSR_EOF);
223145        rc = SQLITE_OK;
223146        break;
223147      }
223148
223149      case FTS5_PLAN_SORTED_MATCH: {
223150        rc = fts5SorterNext(pCsr);
223151        break;
223152      }
223153
223154      default: {
223155        Fts5Config *pConfig = ((Fts5Table*)pCursor->pVtab)->pConfig;
223156        pConfig->bLock++;
223157        rc = sqlite3_step(pCsr->pStmt);
223158        pConfig->bLock--;
223159        if( rc!=SQLITE_ROW ){
223160          CsrFlagSet(pCsr, FTS5CSR_EOF);
223161          rc = sqlite3_reset(pCsr->pStmt);
223162          if( rc!=SQLITE_OK ){
223163            pCursor->pVtab->zErrMsg = sqlite3_mprintf(
223164                "%s", sqlite3_errmsg(pConfig->db)
223165            );
223166          }
223167        }else{
223168          rc = SQLITE_OK;
223169        }
223170        break;
223171      }
223172    }
223173  }
223174
223175  return rc;
223176}
223177
223178
223179static int fts5PrepareStatement(
223180  sqlite3_stmt **ppStmt,
223181  Fts5Config *pConfig,
223182  const char *zFmt,
223183  ...
223184){
223185  sqlite3_stmt *pRet = 0;
223186  int rc;
223187  char *zSql;
223188  va_list ap;
223189
223190  va_start(ap, zFmt);
223191  zSql = sqlite3_vmprintf(zFmt, ap);
223192  if( zSql==0 ){
223193    rc = SQLITE_NOMEM;
223194  }else{
223195    rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
223196                            SQLITE_PREPARE_PERSISTENT, &pRet, 0);
223197    if( rc!=SQLITE_OK ){
223198      *pConfig->pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(pConfig->db));
223199    }
223200    sqlite3_free(zSql);
223201  }
223202
223203  va_end(ap);
223204  *ppStmt = pRet;
223205  return rc;
223206}
223207
223208static int fts5CursorFirstSorted(
223209  Fts5FullTable *pTab,
223210  Fts5Cursor *pCsr,
223211  int bDesc
223212){
223213  Fts5Config *pConfig = pTab->p.pConfig;
223214  Fts5Sorter *pSorter;
223215  int nPhrase;
223216  sqlite3_int64 nByte;
223217  int rc;
223218  const char *zRank = pCsr->zRank;
223219  const char *zRankArgs = pCsr->zRankArgs;
223220
223221  nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
223222  nByte = sizeof(Fts5Sorter) + sizeof(int) * (nPhrase-1);
223223  pSorter = (Fts5Sorter*)sqlite3_malloc64(nByte);
223224  if( pSorter==0 ) return SQLITE_NOMEM;
223225  memset(pSorter, 0, (size_t)nByte);
223226  pSorter->nIdx = nPhrase;
223227
223228  /* TODO: It would be better to have some system for reusing statement
223229  ** handles here, rather than preparing a new one for each query. But that
223230  ** is not possible as SQLite reference counts the virtual table objects.
223231  ** And since the statement required here reads from this very virtual
223232  ** table, saving it creates a circular reference.
223233  **
223234  ** If SQLite a built-in statement cache, this wouldn't be a problem. */
223235  rc = fts5PrepareStatement(&pSorter->pStmt, pConfig,
223236      "SELECT rowid, rank FROM %Q.%Q ORDER BY %s(\"%w\"%s%s) %s",
223237      pConfig->zDb, pConfig->zName, zRank, pConfig->zName,
223238      (zRankArgs ? ", " : ""),
223239      (zRankArgs ? zRankArgs : ""),
223240      bDesc ? "DESC" : "ASC"
223241  );
223242
223243  pCsr->pSorter = pSorter;
223244  if( rc==SQLITE_OK ){
223245    assert( pTab->pSortCsr==0 );
223246    pTab->pSortCsr = pCsr;
223247    rc = fts5SorterNext(pCsr);
223248    pTab->pSortCsr = 0;
223249  }
223250
223251  if( rc!=SQLITE_OK ){
223252    sqlite3_finalize(pSorter->pStmt);
223253    sqlite3_free(pSorter);
223254    pCsr->pSorter = 0;
223255  }
223256
223257  return rc;
223258}
223259
223260static int fts5CursorFirst(Fts5FullTable *pTab, Fts5Cursor *pCsr, int bDesc){
223261  int rc;
223262  Fts5Expr *pExpr = pCsr->pExpr;
223263  rc = sqlite3Fts5ExprFirst(pExpr, pTab->p.pIndex, pCsr->iFirstRowid, bDesc);
223264  if( sqlite3Fts5ExprEof(pExpr) ){
223265    CsrFlagSet(pCsr, FTS5CSR_EOF);
223266  }
223267  fts5CsrNewrow(pCsr);
223268  return rc;
223269}
223270
223271/*
223272** Process a "special" query. A special query is identified as one with a
223273** MATCH expression that begins with a '*' character. The remainder of
223274** the text passed to the MATCH operator are used as  the special query
223275** parameters.
223276*/
223277static int fts5SpecialMatch(
223278  Fts5FullTable *pTab,
223279  Fts5Cursor *pCsr,
223280  const char *zQuery
223281){
223282  int rc = SQLITE_OK;             /* Return code */
223283  const char *z = zQuery;         /* Special query text */
223284  int n;                          /* Number of bytes in text at z */
223285
223286  while( z[0]==' ' ) z++;
223287  for(n=0; z[n] && z[n]!=' '; n++);
223288
223289  assert( pTab->p.base.zErrMsg==0 );
223290  pCsr->ePlan = FTS5_PLAN_SPECIAL;
223291
223292  if( n==5 && 0==sqlite3_strnicmp("reads", z, n) ){
223293    pCsr->iSpecial = sqlite3Fts5IndexReads(pTab->p.pIndex);
223294  }
223295  else if( n==2 && 0==sqlite3_strnicmp("id", z, n) ){
223296    pCsr->iSpecial = pCsr->iCsrId;
223297  }
223298  else{
223299    /* An unrecognized directive. Return an error message. */
223300    pTab->p.base.zErrMsg = sqlite3_mprintf("unknown special query: %.*s", n, z);
223301    rc = SQLITE_ERROR;
223302  }
223303
223304  return rc;
223305}
223306
223307/*
223308** Search for an auxiliary function named zName that can be used with table
223309** pTab. If one is found, return a pointer to the corresponding Fts5Auxiliary
223310** structure. Otherwise, if no such function exists, return NULL.
223311*/
223312static Fts5Auxiliary *fts5FindAuxiliary(Fts5FullTable *pTab, const char *zName){
223313  Fts5Auxiliary *pAux;
223314
223315  for(pAux=pTab->pGlobal->pAux; pAux; pAux=pAux->pNext){
223316    if( sqlite3_stricmp(zName, pAux->zFunc)==0 ) return pAux;
223317  }
223318
223319  /* No function of the specified name was found. Return 0. */
223320  return 0;
223321}
223322
223323
223324static int fts5FindRankFunction(Fts5Cursor *pCsr){
223325  Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
223326  Fts5Config *pConfig = pTab->p.pConfig;
223327  int rc = SQLITE_OK;
223328  Fts5Auxiliary *pAux = 0;
223329  const char *zRank = pCsr->zRank;
223330  const char *zRankArgs = pCsr->zRankArgs;
223331
223332  if( zRankArgs ){
223333    char *zSql = sqlite3Fts5Mprintf(&rc, "SELECT %s", zRankArgs);
223334    if( zSql ){
223335      sqlite3_stmt *pStmt = 0;
223336      rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
223337                              SQLITE_PREPARE_PERSISTENT, &pStmt, 0);
223338      sqlite3_free(zSql);
223339      assert( rc==SQLITE_OK || pCsr->pRankArgStmt==0 );
223340      if( rc==SQLITE_OK ){
223341        if( SQLITE_ROW==sqlite3_step(pStmt) ){
223342          sqlite3_int64 nByte;
223343          pCsr->nRankArg = sqlite3_column_count(pStmt);
223344          nByte = sizeof(sqlite3_value*)*pCsr->nRankArg;
223345          pCsr->apRankArg = (sqlite3_value**)sqlite3Fts5MallocZero(&rc, nByte);
223346          if( rc==SQLITE_OK ){
223347            int i;
223348            for(i=0; i<pCsr->nRankArg; i++){
223349              pCsr->apRankArg[i] = sqlite3_column_value(pStmt, i);
223350            }
223351          }
223352          pCsr->pRankArgStmt = pStmt;
223353        }else{
223354          rc = sqlite3_finalize(pStmt);
223355          assert( rc!=SQLITE_OK );
223356        }
223357      }
223358    }
223359  }
223360
223361  if( rc==SQLITE_OK ){
223362    pAux = fts5FindAuxiliary(pTab, zRank);
223363    if( pAux==0 ){
223364      assert( pTab->p.base.zErrMsg==0 );
223365      pTab->p.base.zErrMsg = sqlite3_mprintf("no such function: %s", zRank);
223366      rc = SQLITE_ERROR;
223367    }
223368  }
223369
223370  pCsr->pRank = pAux;
223371  return rc;
223372}
223373
223374
223375static int fts5CursorParseRank(
223376  Fts5Config *pConfig,
223377  Fts5Cursor *pCsr,
223378  sqlite3_value *pRank
223379){
223380  int rc = SQLITE_OK;
223381  if( pRank ){
223382    const char *z = (const char*)sqlite3_value_text(pRank);
223383    char *zRank = 0;
223384    char *zRankArgs = 0;
223385
223386    if( z==0 ){
223387      if( sqlite3_value_type(pRank)==SQLITE_NULL ) rc = SQLITE_ERROR;
223388    }else{
223389      rc = sqlite3Fts5ConfigParseRank(z, &zRank, &zRankArgs);
223390    }
223391    if( rc==SQLITE_OK ){
223392      pCsr->zRank = zRank;
223393      pCsr->zRankArgs = zRankArgs;
223394      CsrFlagSet(pCsr, FTS5CSR_FREE_ZRANK);
223395    }else if( rc==SQLITE_ERROR ){
223396      pCsr->base.pVtab->zErrMsg = sqlite3_mprintf(
223397          "parse error in rank function: %s", z
223398      );
223399    }
223400  }else{
223401    if( pConfig->zRank ){
223402      pCsr->zRank = (char*)pConfig->zRank;
223403      pCsr->zRankArgs = (char*)pConfig->zRankArgs;
223404    }else{
223405      pCsr->zRank = (char*)FTS5_DEFAULT_RANK;
223406      pCsr->zRankArgs = 0;
223407    }
223408  }
223409  return rc;
223410}
223411
223412static i64 fts5GetRowidLimit(sqlite3_value *pVal, i64 iDefault){
223413  if( pVal ){
223414    int eType = sqlite3_value_numeric_type(pVal);
223415    if( eType==SQLITE_INTEGER ){
223416      return sqlite3_value_int64(pVal);
223417    }
223418  }
223419  return iDefault;
223420}
223421
223422/*
223423** This is the xFilter interface for the virtual table.  See
223424** the virtual table xFilter method documentation for additional
223425** information.
223426**
223427** There are three possible query strategies:
223428**
223429**   1. Full-text search using a MATCH operator.
223430**   2. A by-rowid lookup.
223431**   3. A full-table scan.
223432*/
223433static int fts5FilterMethod(
223434  sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
223435  int idxNum,                     /* Strategy index */
223436  const char *idxStr,             /* Unused */
223437  int nVal,                       /* Number of elements in apVal */
223438  sqlite3_value **apVal           /* Arguments for the indexing scheme */
223439){
223440  Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
223441  Fts5Config *pConfig = pTab->p.pConfig;
223442  Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
223443  int rc = SQLITE_OK;             /* Error code */
223444  int bDesc;                      /* True if ORDER BY [rank|rowid] DESC */
223445  int bOrderByRank;               /* True if ORDER BY rank */
223446  sqlite3_value *pRank = 0;       /* rank MATCH ? expression (or NULL) */
223447  sqlite3_value *pRowidEq = 0;    /* rowid = ? expression (or NULL) */
223448  sqlite3_value *pRowidLe = 0;    /* rowid <= ? expression (or NULL) */
223449  sqlite3_value *pRowidGe = 0;    /* rowid >= ? expression (or NULL) */
223450  int iCol;                       /* Column on LHS of MATCH operator */
223451  char **pzErrmsg = pConfig->pzErrmsg;
223452  int i;
223453  int iIdxStr = 0;
223454  Fts5Expr *pExpr = 0;
223455
223456  if( pConfig->bLock ){
223457    pTab->p.base.zErrMsg = sqlite3_mprintf(
223458        "recursively defined fts5 content table"
223459    );
223460    return SQLITE_ERROR;
223461  }
223462
223463  if( pCsr->ePlan ){
223464    fts5FreeCursorComponents(pCsr);
223465    memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
223466  }
223467
223468  assert( pCsr->pStmt==0 );
223469  assert( pCsr->pExpr==0 );
223470  assert( pCsr->csrflags==0 );
223471  assert( pCsr->pRank==0 );
223472  assert( pCsr->zRank==0 );
223473  assert( pCsr->zRankArgs==0 );
223474  assert( pTab->pSortCsr==0 || nVal==0 );
223475
223476  assert( pzErrmsg==0 || pzErrmsg==&pTab->p.base.zErrMsg );
223477  pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
223478
223479  /* Decode the arguments passed through to this function. */
223480  for(i=0; i<nVal; i++){
223481    switch( idxStr[iIdxStr++] ){
223482      case 'r':
223483        pRank = apVal[i];
223484        break;
223485      case 'm': {
223486        const char *zText = (const char*)sqlite3_value_text(apVal[i]);
223487        if( zText==0 ) zText = "";
223488
223489        if( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' ){
223490          iCol = 0;
223491          do{
223492            iCol = iCol*10 + (idxStr[iIdxStr]-'0');
223493            iIdxStr++;
223494          }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' );
223495        }else{
223496          iCol = pConfig->nCol;
223497        }
223498
223499        if( zText[0]=='*' ){
223500          /* The user has issued a query of the form "MATCH '*...'". This
223501          ** indicates that the MATCH expression is not a full text query,
223502          ** but a request for an internal parameter.  */
223503          rc = fts5SpecialMatch(pTab, pCsr, &zText[1]);
223504          goto filter_out;
223505        }else{
223506          char **pzErr = &pTab->p.base.zErrMsg;
223507          rc = sqlite3Fts5ExprNew(pConfig, iCol, zText, &pExpr, pzErr);
223508          if( rc==SQLITE_OK ){
223509            rc = sqlite3Fts5ExprAnd(&pCsr->pExpr, pExpr);
223510            pExpr = 0;
223511          }
223512          if( rc!=SQLITE_OK ) goto filter_out;
223513        }
223514
223515        break;
223516      }
223517      case '=':
223518        pRowidEq = apVal[i];
223519        break;
223520      case '<':
223521        pRowidLe = apVal[i];
223522        break;
223523      default: assert( idxStr[iIdxStr-1]=='>' );
223524        pRowidGe = apVal[i];
223525        break;
223526    }
223527  }
223528  bOrderByRank = ((idxNum & FTS5_BI_ORDER_RANK) ? 1 : 0);
223529  pCsr->bDesc = bDesc = ((idxNum & FTS5_BI_ORDER_DESC) ? 1 : 0);
223530
223531  /* Set the cursor upper and lower rowid limits. Only some strategies
223532  ** actually use them. This is ok, as the xBestIndex() method leaves the
223533  ** sqlite3_index_constraint.omit flag clear for range constraints
223534  ** on the rowid field.  */
223535  if( pRowidEq ){
223536    pRowidLe = pRowidGe = pRowidEq;
223537  }
223538  if( bDesc ){
223539    pCsr->iFirstRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64);
223540    pCsr->iLastRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64);
223541  }else{
223542    pCsr->iLastRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64);
223543    pCsr->iFirstRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64);
223544  }
223545
223546  if( pTab->pSortCsr ){
223547    /* If pSortCsr is non-NULL, then this call is being made as part of
223548    ** processing for a "... MATCH <expr> ORDER BY rank" query (ePlan is
223549    ** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will
223550    ** return results to the user for this query. The current cursor
223551    ** (pCursor) is used to execute the query issued by function
223552    ** fts5CursorFirstSorted() above.  */
223553    assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 );
223554    assert( nVal==0 && bOrderByRank==0 && bDesc==0 );
223555    assert( pCsr->iLastRowid==LARGEST_INT64 );
223556    assert( pCsr->iFirstRowid==SMALLEST_INT64 );
223557    if( pTab->pSortCsr->bDesc ){
223558      pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid;
223559      pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid;
223560    }else{
223561      pCsr->iLastRowid = pTab->pSortCsr->iLastRowid;
223562      pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid;
223563    }
223564    pCsr->ePlan = FTS5_PLAN_SOURCE;
223565    pCsr->pExpr = pTab->pSortCsr->pExpr;
223566    rc = fts5CursorFirst(pTab, pCsr, bDesc);
223567  }else if( pCsr->pExpr ){
223568    rc = fts5CursorParseRank(pConfig, pCsr, pRank);
223569    if( rc==SQLITE_OK ){
223570      if( bOrderByRank ){
223571        pCsr->ePlan = FTS5_PLAN_SORTED_MATCH;
223572        rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
223573      }else{
223574        pCsr->ePlan = FTS5_PLAN_MATCH;
223575        rc = fts5CursorFirst(pTab, pCsr, bDesc);
223576      }
223577    }
223578  }else if( pConfig->zContent==0 ){
223579    *pConfig->pzErrmsg = sqlite3_mprintf(
223580        "%s: table does not support scanning", pConfig->zName
223581    );
223582    rc = SQLITE_ERROR;
223583  }else{
223584    /* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup
223585    ** by rowid (ePlan==FTS5_PLAN_ROWID).  */
223586    pCsr->ePlan = (pRowidEq ? FTS5_PLAN_ROWID : FTS5_PLAN_SCAN);
223587    rc = sqlite3Fts5StorageStmt(
223588        pTab->pStorage, fts5StmtType(pCsr), &pCsr->pStmt, &pTab->p.base.zErrMsg
223589    );
223590    if( rc==SQLITE_OK ){
223591      if( pCsr->ePlan==FTS5_PLAN_ROWID ){
223592        sqlite3_bind_value(pCsr->pStmt, 1, pRowidEq);
223593      }else{
223594        sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iFirstRowid);
223595        sqlite3_bind_int64(pCsr->pStmt, 2, pCsr->iLastRowid);
223596      }
223597      rc = fts5NextMethod(pCursor);
223598    }
223599  }
223600
223601 filter_out:
223602  sqlite3Fts5ExprFree(pExpr);
223603  pConfig->pzErrmsg = pzErrmsg;
223604  return rc;
223605}
223606
223607/*
223608** This is the xEof method of the virtual table. SQLite calls this
223609** routine to find out if it has reached the end of a result set.
223610*/
223611static int fts5EofMethod(sqlite3_vtab_cursor *pCursor){
223612  Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
223613  return (CsrFlagTest(pCsr, FTS5CSR_EOF) ? 1 : 0);
223614}
223615
223616/*
223617** Return the rowid that the cursor currently points to.
223618*/
223619static i64 fts5CursorRowid(Fts5Cursor *pCsr){
223620  assert( pCsr->ePlan==FTS5_PLAN_MATCH
223621       || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH
223622       || pCsr->ePlan==FTS5_PLAN_SOURCE
223623  );
223624  if( pCsr->pSorter ){
223625    return pCsr->pSorter->iRowid;
223626  }else{
223627    return sqlite3Fts5ExprRowid(pCsr->pExpr);
223628  }
223629}
223630
223631/*
223632** This is the xRowid method. The SQLite core calls this routine to
223633** retrieve the rowid for the current row of the result set. fts5
223634** exposes %_content.rowid as the rowid for the virtual table. The
223635** rowid should be written to *pRowid.
223636*/
223637static int fts5RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
223638  Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
223639  int ePlan = pCsr->ePlan;
223640
223641  assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 );
223642  switch( ePlan ){
223643    case FTS5_PLAN_SPECIAL:
223644      *pRowid = 0;
223645      break;
223646
223647    case FTS5_PLAN_SOURCE:
223648    case FTS5_PLAN_MATCH:
223649    case FTS5_PLAN_SORTED_MATCH:
223650      *pRowid = fts5CursorRowid(pCsr);
223651      break;
223652
223653    default:
223654      *pRowid = sqlite3_column_int64(pCsr->pStmt, 0);
223655      break;
223656  }
223657
223658  return SQLITE_OK;
223659}
223660
223661/*
223662** If the cursor requires seeking (bSeekRequired flag is set), seek it.
223663** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise.
223664**
223665** If argument bErrormsg is true and an error occurs, an error message may
223666** be left in sqlite3_vtab.zErrMsg.
223667*/
223668static int fts5SeekCursor(Fts5Cursor *pCsr, int bErrormsg){
223669  int rc = SQLITE_OK;
223670
223671  /* If the cursor does not yet have a statement handle, obtain one now. */
223672  if( pCsr->pStmt==0 ){
223673    Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
223674    int eStmt = fts5StmtType(pCsr);
223675    rc = sqlite3Fts5StorageStmt(
223676        pTab->pStorage, eStmt, &pCsr->pStmt, (bErrormsg?&pTab->p.base.zErrMsg:0)
223677    );
223678    assert( rc!=SQLITE_OK || pTab->p.base.zErrMsg==0 );
223679    assert( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) );
223680  }
223681
223682  if( rc==SQLITE_OK && CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) ){
223683    Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
223684    assert( pCsr->pExpr );
223685    sqlite3_reset(pCsr->pStmt);
223686    sqlite3_bind_int64(pCsr->pStmt, 1, fts5CursorRowid(pCsr));
223687    pTab->pConfig->bLock++;
223688    rc = sqlite3_step(pCsr->pStmt);
223689    pTab->pConfig->bLock--;
223690    if( rc==SQLITE_ROW ){
223691      rc = SQLITE_OK;
223692      CsrFlagClear(pCsr, FTS5CSR_REQUIRE_CONTENT);
223693    }else{
223694      rc = sqlite3_reset(pCsr->pStmt);
223695      if( rc==SQLITE_OK ){
223696        rc = FTS5_CORRUPT;
223697      }else if( pTab->pConfig->pzErrmsg ){
223698        *pTab->pConfig->pzErrmsg = sqlite3_mprintf(
223699            "%s", sqlite3_errmsg(pTab->pConfig->db)
223700        );
223701      }
223702    }
223703  }
223704  return rc;
223705}
223706
223707static void fts5SetVtabError(Fts5FullTable *p, const char *zFormat, ...){
223708  va_list ap;                     /* ... printf arguments */
223709  va_start(ap, zFormat);
223710  assert( p->p.base.zErrMsg==0 );
223711  p->p.base.zErrMsg = sqlite3_vmprintf(zFormat, ap);
223712  va_end(ap);
223713}
223714
223715/*
223716** This function is called to handle an FTS INSERT command. In other words,
223717** an INSERT statement of the form:
223718**
223719**     INSERT INTO fts(fts) VALUES($pCmd)
223720**     INSERT INTO fts(fts, rank) VALUES($pCmd, $pVal)
223721**
223722** Argument pVal is the value assigned to column "fts" by the INSERT
223723** statement. This function returns SQLITE_OK if successful, or an SQLite
223724** error code if an error occurs.
223725**
223726** The commands implemented by this function are documented in the "Special
223727** INSERT Directives" section of the documentation. It should be updated if
223728** more commands are added to this function.
223729*/
223730static int fts5SpecialInsert(
223731  Fts5FullTable *pTab,            /* Fts5 table object */
223732  const char *zCmd,               /* Text inserted into table-name column */
223733  sqlite3_value *pVal             /* Value inserted into rank column */
223734){
223735  Fts5Config *pConfig = pTab->p.pConfig;
223736  int rc = SQLITE_OK;
223737  int bError = 0;
223738
223739  if( 0==sqlite3_stricmp("delete-all", zCmd) ){
223740    if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
223741      fts5SetVtabError(pTab,
223742          "'delete-all' may only be used with a "
223743          "contentless or external content fts5 table"
223744      );
223745      rc = SQLITE_ERROR;
223746    }else{
223747      rc = sqlite3Fts5StorageDeleteAll(pTab->pStorage);
223748    }
223749  }else if( 0==sqlite3_stricmp("rebuild", zCmd) ){
223750    if( pConfig->eContent==FTS5_CONTENT_NONE ){
223751      fts5SetVtabError(pTab,
223752          "'rebuild' may not be used with a contentless fts5 table"
223753      );
223754      rc = SQLITE_ERROR;
223755    }else{
223756      rc = sqlite3Fts5StorageRebuild(pTab->pStorage);
223757    }
223758  }else if( 0==sqlite3_stricmp("optimize", zCmd) ){
223759    rc = sqlite3Fts5StorageOptimize(pTab->pStorage);
223760  }else if( 0==sqlite3_stricmp("merge", zCmd) ){
223761    int nMerge = sqlite3_value_int(pVal);
223762    rc = sqlite3Fts5StorageMerge(pTab->pStorage, nMerge);
223763  }else if( 0==sqlite3_stricmp("integrity-check", zCmd) ){
223764    rc = sqlite3Fts5StorageIntegrity(pTab->pStorage);
223765#ifdef SQLITE_DEBUG
223766  }else if( 0==sqlite3_stricmp("prefix-index", zCmd) ){
223767    pConfig->bPrefixIndex = sqlite3_value_int(pVal);
223768#endif
223769  }else{
223770    rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
223771    if( rc==SQLITE_OK ){
223772      rc = sqlite3Fts5ConfigSetValue(pTab->p.pConfig, zCmd, pVal, &bError);
223773    }
223774    if( rc==SQLITE_OK ){
223775      if( bError ){
223776        rc = SQLITE_ERROR;
223777      }else{
223778        rc = sqlite3Fts5StorageConfigValue(pTab->pStorage, zCmd, pVal, 0);
223779      }
223780    }
223781  }
223782  return rc;
223783}
223784
223785static int fts5SpecialDelete(
223786  Fts5FullTable *pTab,
223787  sqlite3_value **apVal
223788){
223789  int rc = SQLITE_OK;
223790  int eType1 = sqlite3_value_type(apVal[1]);
223791  if( eType1==SQLITE_INTEGER ){
223792    sqlite3_int64 iDel = sqlite3_value_int64(apVal[1]);
223793    rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, &apVal[2]);
223794  }
223795  return rc;
223796}
223797
223798static void fts5StorageInsert(
223799  int *pRc,
223800  Fts5FullTable *pTab,
223801  sqlite3_value **apVal,
223802  i64 *piRowid
223803){
223804  int rc = *pRc;
223805  if( rc==SQLITE_OK ){
223806    rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, piRowid);
223807  }
223808  if( rc==SQLITE_OK ){
223809    rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal, *piRowid);
223810  }
223811  *pRc = rc;
223812}
223813
223814/*
223815** This function is the implementation of the xUpdate callback used by
223816** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
223817** inserted, updated or deleted.
223818**
223819** A delete specifies a single argument - the rowid of the row to remove.
223820**
223821** Update and insert operations pass:
223822**
223823**   1. The "old" rowid, or NULL.
223824**   2. The "new" rowid.
223825**   3. Values for each of the nCol matchable columns.
223826**   4. Values for the two hidden columns (<tablename> and "rank").
223827*/
223828static int fts5UpdateMethod(
223829  sqlite3_vtab *pVtab,            /* Virtual table handle */
223830  int nArg,                       /* Size of argument array */
223831  sqlite3_value **apVal,          /* Array of arguments */
223832  sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
223833){
223834  Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
223835  Fts5Config *pConfig = pTab->p.pConfig;
223836  int eType0;                     /* value_type() of apVal[0] */
223837  int rc = SQLITE_OK;             /* Return code */
223838
223839  /* A transaction must be open when this is called. */
223840  assert( pTab->ts.eState==1 );
223841
223842  assert( pVtab->zErrMsg==0 );
223843  assert( nArg==1 || nArg==(2+pConfig->nCol+2) );
223844  assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER
223845       || sqlite3_value_type(apVal[0])==SQLITE_NULL
223846  );
223847  assert( pTab->p.pConfig->pzErrmsg==0 );
223848  pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
223849
223850  /* Put any active cursors into REQUIRE_SEEK state. */
223851  fts5TripCursors(pTab);
223852
223853  eType0 = sqlite3_value_type(apVal[0]);
223854  if( eType0==SQLITE_NULL
223855   && sqlite3_value_type(apVal[2+pConfig->nCol])!=SQLITE_NULL
223856  ){
223857    /* A "special" INSERT op. These are handled separately. */
223858    const char *z = (const char*)sqlite3_value_text(apVal[2+pConfig->nCol]);
223859    if( pConfig->eContent!=FTS5_CONTENT_NORMAL
223860      && 0==sqlite3_stricmp("delete", z)
223861    ){
223862      rc = fts5SpecialDelete(pTab, apVal);
223863    }else{
223864      rc = fts5SpecialInsert(pTab, z, apVal[2 + pConfig->nCol + 1]);
223865    }
223866  }else{
223867    /* A regular INSERT, UPDATE or DELETE statement. The trick here is that
223868    ** any conflict on the rowid value must be detected before any
223869    ** modifications are made to the database file. There are 4 cases:
223870    **
223871    **   1) DELETE
223872    **   2) UPDATE (rowid not modified)
223873    **   3) UPDATE (rowid modified)
223874    **   4) INSERT
223875    **
223876    ** Cases 3 and 4 may violate the rowid constraint.
223877    */
223878    int eConflict = SQLITE_ABORT;
223879    if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
223880      eConflict = sqlite3_vtab_on_conflict(pConfig->db);
223881    }
223882
223883    assert( eType0==SQLITE_INTEGER || eType0==SQLITE_NULL );
223884    assert( nArg!=1 || eType0==SQLITE_INTEGER );
223885
223886    /* Filter out attempts to run UPDATE or DELETE on contentless tables.
223887    ** This is not suported.  */
223888    if( eType0==SQLITE_INTEGER && fts5IsContentless(pTab) ){
223889      pTab->p.base.zErrMsg = sqlite3_mprintf(
223890          "cannot %s contentless fts5 table: %s",
223891          (nArg>1 ? "UPDATE" : "DELETE from"), pConfig->zName
223892      );
223893      rc = SQLITE_ERROR;
223894    }
223895
223896    /* DELETE */
223897    else if( nArg==1 ){
223898      i64 iDel = sqlite3_value_int64(apVal[0]);  /* Rowid to delete */
223899      rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, 0);
223900    }
223901
223902    /* INSERT or UPDATE */
223903    else{
223904      int eType1 = sqlite3_value_numeric_type(apVal[1]);
223905
223906      if( eType1!=SQLITE_INTEGER && eType1!=SQLITE_NULL ){
223907        rc = SQLITE_MISMATCH;
223908      }
223909
223910      else if( eType0!=SQLITE_INTEGER ){
223911        /* If this is a REPLACE, first remove the current entry (if any) */
223912        if( eConflict==SQLITE_REPLACE && eType1==SQLITE_INTEGER ){
223913          i64 iNew = sqlite3_value_int64(apVal[1]);  /* Rowid to delete */
223914          rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0);
223915        }
223916        fts5StorageInsert(&rc, pTab, apVal, pRowid);
223917      }
223918
223919      /* UPDATE */
223920      else{
223921        i64 iOld = sqlite3_value_int64(apVal[0]);  /* Old rowid */
223922        i64 iNew = sqlite3_value_int64(apVal[1]);  /* New rowid */
223923        if( eType1==SQLITE_INTEGER && iOld!=iNew ){
223924          if( eConflict==SQLITE_REPLACE ){
223925            rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
223926            if( rc==SQLITE_OK ){
223927              rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0);
223928            }
223929            fts5StorageInsert(&rc, pTab, apVal, pRowid);
223930          }else{
223931            rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, pRowid);
223932            if( rc==SQLITE_OK ){
223933              rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
223934            }
223935            if( rc==SQLITE_OK ){
223936              rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal,*pRowid);
223937            }
223938          }
223939        }else{
223940          rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
223941          fts5StorageInsert(&rc, pTab, apVal, pRowid);
223942        }
223943      }
223944    }
223945  }
223946
223947  pTab->p.pConfig->pzErrmsg = 0;
223948  return rc;
223949}
223950
223951/*
223952** Implementation of xSync() method.
223953*/
223954static int fts5SyncMethod(sqlite3_vtab *pVtab){
223955  int rc;
223956  Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
223957  fts5CheckTransactionState(pTab, FTS5_SYNC, 0);
223958  pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
223959  fts5TripCursors(pTab);
223960  rc = sqlite3Fts5StorageSync(pTab->pStorage);
223961  pTab->p.pConfig->pzErrmsg = 0;
223962  return rc;
223963}
223964
223965/*
223966** Implementation of xBegin() method.
223967*/
223968static int fts5BeginMethod(sqlite3_vtab *pVtab){
223969  fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_BEGIN, 0);
223970  fts5NewTransaction((Fts5FullTable*)pVtab);
223971  return SQLITE_OK;
223972}
223973
223974/*
223975** Implementation of xCommit() method. This is a no-op. The contents of
223976** the pending-terms hash-table have already been flushed into the database
223977** by fts5SyncMethod().
223978*/
223979static int fts5CommitMethod(sqlite3_vtab *pVtab){
223980  UNUSED_PARAM(pVtab);  /* Call below is a no-op for NDEBUG builds */
223981  fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_COMMIT, 0);
223982  return SQLITE_OK;
223983}
223984
223985/*
223986** Implementation of xRollback(). Discard the contents of the pending-terms
223987** hash-table. Any changes made to the database are reverted by SQLite.
223988*/
223989static int fts5RollbackMethod(sqlite3_vtab *pVtab){
223990  int rc;
223991  Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
223992  fts5CheckTransactionState(pTab, FTS5_ROLLBACK, 0);
223993  rc = sqlite3Fts5StorageRollback(pTab->pStorage);
223994  return rc;
223995}
223996
223997static int fts5CsrPoslist(Fts5Cursor*, int, const u8**, int*);
223998
223999static void *fts5ApiUserData(Fts5Context *pCtx){
224000  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224001  return pCsr->pAux->pUserData;
224002}
224003
224004static int fts5ApiColumnCount(Fts5Context *pCtx){
224005  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224006  return ((Fts5Table*)(pCsr->base.pVtab))->pConfig->nCol;
224007}
224008
224009static int fts5ApiColumnTotalSize(
224010  Fts5Context *pCtx,
224011  int iCol,
224012  sqlite3_int64 *pnToken
224013){
224014  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224015  Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
224016  return sqlite3Fts5StorageSize(pTab->pStorage, iCol, pnToken);
224017}
224018
224019static int fts5ApiRowCount(Fts5Context *pCtx, i64 *pnRow){
224020  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224021  Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
224022  return sqlite3Fts5StorageRowCount(pTab->pStorage, pnRow);
224023}
224024
224025static int fts5ApiTokenize(
224026  Fts5Context *pCtx,
224027  const char *pText, int nText,
224028  void *pUserData,
224029  int (*xToken)(void*, int, const char*, int, int, int)
224030){
224031  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224032  Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
224033  return sqlite3Fts5Tokenize(
224034      pTab->pConfig, FTS5_TOKENIZE_AUX, pText, nText, pUserData, xToken
224035  );
224036}
224037
224038static int fts5ApiPhraseCount(Fts5Context *pCtx){
224039  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224040  return sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
224041}
224042
224043static int fts5ApiPhraseSize(Fts5Context *pCtx, int iPhrase){
224044  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224045  return sqlite3Fts5ExprPhraseSize(pCsr->pExpr, iPhrase);
224046}
224047
224048static int fts5ApiColumnText(
224049  Fts5Context *pCtx,
224050  int iCol,
224051  const char **pz,
224052  int *pn
224053){
224054  int rc = SQLITE_OK;
224055  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224056  if( fts5IsContentless((Fts5FullTable*)(pCsr->base.pVtab))
224057   || pCsr->ePlan==FTS5_PLAN_SPECIAL
224058  ){
224059    *pz = 0;
224060    *pn = 0;
224061  }else{
224062    rc = fts5SeekCursor(pCsr, 0);
224063    if( rc==SQLITE_OK ){
224064      *pz = (const char*)sqlite3_column_text(pCsr->pStmt, iCol+1);
224065      *pn = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
224066    }
224067  }
224068  return rc;
224069}
224070
224071static int fts5CsrPoslist(
224072  Fts5Cursor *pCsr,
224073  int iPhrase,
224074  const u8 **pa,
224075  int *pn
224076){
224077  Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
224078  int rc = SQLITE_OK;
224079  int bLive = (pCsr->pSorter==0);
224080
224081  if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST) ){
224082
224083    if( pConfig->eDetail!=FTS5_DETAIL_FULL ){
224084      Fts5PoslistPopulator *aPopulator;
224085      int i;
224086      aPopulator = sqlite3Fts5ExprClearPoslists(pCsr->pExpr, bLive);
224087      if( aPopulator==0 ) rc = SQLITE_NOMEM;
224088      for(i=0; i<pConfig->nCol && rc==SQLITE_OK; i++){
224089        int n; const char *z;
224090        rc = fts5ApiColumnText((Fts5Context*)pCsr, i, &z, &n);
224091        if( rc==SQLITE_OK ){
224092          rc = sqlite3Fts5ExprPopulatePoslists(
224093              pConfig, pCsr->pExpr, aPopulator, i, z, n
224094          );
224095        }
224096      }
224097      sqlite3_free(aPopulator);
224098
224099      if( pCsr->pSorter ){
224100        sqlite3Fts5ExprCheckPoslists(pCsr->pExpr, pCsr->pSorter->iRowid);
224101      }
224102    }
224103    CsrFlagClear(pCsr, FTS5CSR_REQUIRE_POSLIST);
224104  }
224105
224106  if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL ){
224107    Fts5Sorter *pSorter = pCsr->pSorter;
224108    int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
224109    *pn = pSorter->aIdx[iPhrase] - i1;
224110    *pa = &pSorter->aPoslist[i1];
224111  }else{
224112    *pn = sqlite3Fts5ExprPoslist(pCsr->pExpr, iPhrase, pa);
224113  }
224114
224115  return rc;
224116}
224117
224118/*
224119** Ensure that the Fts5Cursor.nInstCount and aInst[] variables are populated
224120** correctly for the current view. Return SQLITE_OK if successful, or an
224121** SQLite error code otherwise.
224122*/
224123static int fts5CacheInstArray(Fts5Cursor *pCsr){
224124  int rc = SQLITE_OK;
224125  Fts5PoslistReader *aIter;       /* One iterator for each phrase */
224126  int nIter;                      /* Number of iterators/phrases */
224127  int nCol = ((Fts5Table*)pCsr->base.pVtab)->pConfig->nCol;
224128
224129  nIter = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
224130  if( pCsr->aInstIter==0 ){
224131    sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nIter;
224132    pCsr->aInstIter = (Fts5PoslistReader*)sqlite3Fts5MallocZero(&rc, nByte);
224133  }
224134  aIter = pCsr->aInstIter;
224135
224136  if( aIter ){
224137    int nInst = 0;                /* Number instances seen so far */
224138    int i;
224139
224140    /* Initialize all iterators */
224141    for(i=0; i<nIter && rc==SQLITE_OK; i++){
224142      const u8 *a;
224143      int n;
224144      rc = fts5CsrPoslist(pCsr, i, &a, &n);
224145      if( rc==SQLITE_OK ){
224146        sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
224147      }
224148    }
224149
224150    if( rc==SQLITE_OK ){
224151      while( 1 ){
224152        int *aInst;
224153        int iBest = -1;
224154        for(i=0; i<nIter; i++){
224155          if( (aIter[i].bEof==0)
224156              && (iBest<0 || aIter[i].iPos<aIter[iBest].iPos)
224157            ){
224158            iBest = i;
224159          }
224160        }
224161        if( iBest<0 ) break;
224162
224163        nInst++;
224164        if( nInst>=pCsr->nInstAlloc ){
224165          pCsr->nInstAlloc = pCsr->nInstAlloc ? pCsr->nInstAlloc*2 : 32;
224166          aInst = (int*)sqlite3_realloc64(
224167              pCsr->aInst, pCsr->nInstAlloc*sizeof(int)*3
224168              );
224169          if( aInst ){
224170            pCsr->aInst = aInst;
224171          }else{
224172            rc = SQLITE_NOMEM;
224173            break;
224174          }
224175        }
224176
224177        aInst = &pCsr->aInst[3 * (nInst-1)];
224178        aInst[0] = iBest;
224179        aInst[1] = FTS5_POS2COLUMN(aIter[iBest].iPos);
224180        aInst[2] = FTS5_POS2OFFSET(aIter[iBest].iPos);
224181        if( aInst[1]<0 || aInst[1]>=nCol ){
224182          rc = FTS5_CORRUPT;
224183          break;
224184        }
224185        sqlite3Fts5PoslistReaderNext(&aIter[iBest]);
224186      }
224187    }
224188
224189    pCsr->nInstCount = nInst;
224190    CsrFlagClear(pCsr, FTS5CSR_REQUIRE_INST);
224191  }
224192  return rc;
224193}
224194
224195static int fts5ApiInstCount(Fts5Context *pCtx, int *pnInst){
224196  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224197  int rc = SQLITE_OK;
224198  if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0
224199   || SQLITE_OK==(rc = fts5CacheInstArray(pCsr)) ){
224200    *pnInst = pCsr->nInstCount;
224201  }
224202  return rc;
224203}
224204
224205static int fts5ApiInst(
224206  Fts5Context *pCtx,
224207  int iIdx,
224208  int *piPhrase,
224209  int *piCol,
224210  int *piOff
224211){
224212  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224213  int rc = SQLITE_OK;
224214  if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0
224215   || SQLITE_OK==(rc = fts5CacheInstArray(pCsr))
224216  ){
224217    if( iIdx<0 || iIdx>=pCsr->nInstCount ){
224218      rc = SQLITE_RANGE;
224219#if 0
224220    }else if( fts5IsOffsetless((Fts5Table*)pCsr->base.pVtab) ){
224221      *piPhrase = pCsr->aInst[iIdx*3];
224222      *piCol = pCsr->aInst[iIdx*3 + 2];
224223      *piOff = -1;
224224#endif
224225    }else{
224226      *piPhrase = pCsr->aInst[iIdx*3];
224227      *piCol = pCsr->aInst[iIdx*3 + 1];
224228      *piOff = pCsr->aInst[iIdx*3 + 2];
224229    }
224230  }
224231  return rc;
224232}
224233
224234static sqlite3_int64 fts5ApiRowid(Fts5Context *pCtx){
224235  return fts5CursorRowid((Fts5Cursor*)pCtx);
224236}
224237
224238static int fts5ColumnSizeCb(
224239  void *pContext,                 /* Pointer to int */
224240  int tflags,
224241  const char *pUnused,            /* Buffer containing token */
224242  int nUnused,                    /* Size of token in bytes */
224243  int iUnused1,                   /* Start offset of token */
224244  int iUnused2                    /* End offset of token */
224245){
224246  int *pCnt = (int*)pContext;
224247  UNUSED_PARAM2(pUnused, nUnused);
224248  UNUSED_PARAM2(iUnused1, iUnused2);
224249  if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
224250    (*pCnt)++;
224251  }
224252  return SQLITE_OK;
224253}
224254
224255static int fts5ApiColumnSize(Fts5Context *pCtx, int iCol, int *pnToken){
224256  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224257  Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
224258  Fts5Config *pConfig = pTab->p.pConfig;
224259  int rc = SQLITE_OK;
224260
224261  if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_DOCSIZE) ){
224262    if( pConfig->bColumnsize ){
224263      i64 iRowid = fts5CursorRowid(pCsr);
224264      rc = sqlite3Fts5StorageDocsize(pTab->pStorage, iRowid, pCsr->aColumnSize);
224265    }else if( pConfig->zContent==0 ){
224266      int i;
224267      for(i=0; i<pConfig->nCol; i++){
224268        if( pConfig->abUnindexed[i]==0 ){
224269          pCsr->aColumnSize[i] = -1;
224270        }
224271      }
224272    }else{
224273      int i;
224274      for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
224275        if( pConfig->abUnindexed[i]==0 ){
224276          const char *z; int n;
224277          void *p = (void*)(&pCsr->aColumnSize[i]);
224278          pCsr->aColumnSize[i] = 0;
224279          rc = fts5ApiColumnText(pCtx, i, &z, &n);
224280          if( rc==SQLITE_OK ){
224281            rc = sqlite3Fts5Tokenize(
224282                pConfig, FTS5_TOKENIZE_AUX, z, n, p, fts5ColumnSizeCb
224283            );
224284          }
224285        }
224286      }
224287    }
224288    CsrFlagClear(pCsr, FTS5CSR_REQUIRE_DOCSIZE);
224289  }
224290  if( iCol<0 ){
224291    int i;
224292    *pnToken = 0;
224293    for(i=0; i<pConfig->nCol; i++){
224294      *pnToken += pCsr->aColumnSize[i];
224295    }
224296  }else if( iCol<pConfig->nCol ){
224297    *pnToken = pCsr->aColumnSize[iCol];
224298  }else{
224299    *pnToken = 0;
224300    rc = SQLITE_RANGE;
224301  }
224302  return rc;
224303}
224304
224305/*
224306** Implementation of the xSetAuxdata() method.
224307*/
224308static int fts5ApiSetAuxdata(
224309  Fts5Context *pCtx,              /* Fts5 context */
224310  void *pPtr,                     /* Pointer to save as auxdata */
224311  void(*xDelete)(void*)           /* Destructor for pPtr (or NULL) */
224312){
224313  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224314  Fts5Auxdata *pData;
224315
224316  /* Search through the cursors list of Fts5Auxdata objects for one that
224317  ** corresponds to the currently executing auxiliary function.  */
224318  for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
224319    if( pData->pAux==pCsr->pAux ) break;
224320  }
224321
224322  if( pData ){
224323    if( pData->xDelete ){
224324      pData->xDelete(pData->pPtr);
224325    }
224326  }else{
224327    int rc = SQLITE_OK;
224328    pData = (Fts5Auxdata*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Auxdata));
224329    if( pData==0 ){
224330      if( xDelete ) xDelete(pPtr);
224331      return rc;
224332    }
224333    pData->pAux = pCsr->pAux;
224334    pData->pNext = pCsr->pAuxdata;
224335    pCsr->pAuxdata = pData;
224336  }
224337
224338  pData->xDelete = xDelete;
224339  pData->pPtr = pPtr;
224340  return SQLITE_OK;
224341}
224342
224343static void *fts5ApiGetAuxdata(Fts5Context *pCtx, int bClear){
224344  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224345  Fts5Auxdata *pData;
224346  void *pRet = 0;
224347
224348  for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
224349    if( pData->pAux==pCsr->pAux ) break;
224350  }
224351
224352  if( pData ){
224353    pRet = pData->pPtr;
224354    if( bClear ){
224355      pData->pPtr = 0;
224356      pData->xDelete = 0;
224357    }
224358  }
224359
224360  return pRet;
224361}
224362
224363static void fts5ApiPhraseNext(
224364  Fts5Context *pUnused,
224365  Fts5PhraseIter *pIter,
224366  int *piCol, int *piOff
224367){
224368  UNUSED_PARAM(pUnused);
224369  if( pIter->a>=pIter->b ){
224370    *piCol = -1;
224371    *piOff = -1;
224372  }else{
224373    int iVal;
224374    pIter->a += fts5GetVarint32(pIter->a, iVal);
224375    if( iVal==1 ){
224376      pIter->a += fts5GetVarint32(pIter->a, iVal);
224377      *piCol = iVal;
224378      *piOff = 0;
224379      pIter->a += fts5GetVarint32(pIter->a, iVal);
224380    }
224381    *piOff += (iVal-2);
224382  }
224383}
224384
224385static int fts5ApiPhraseFirst(
224386  Fts5Context *pCtx,
224387  int iPhrase,
224388  Fts5PhraseIter *pIter,
224389  int *piCol, int *piOff
224390){
224391  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224392  int n;
224393  int rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
224394  if( rc==SQLITE_OK ){
224395    pIter->b = &pIter->a[n];
224396    *piCol = 0;
224397    *piOff = 0;
224398    fts5ApiPhraseNext(pCtx, pIter, piCol, piOff);
224399  }
224400  return rc;
224401}
224402
224403static void fts5ApiPhraseNextColumn(
224404  Fts5Context *pCtx,
224405  Fts5PhraseIter *pIter,
224406  int *piCol
224407){
224408  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224409  Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
224410
224411  if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
224412    if( pIter->a>=pIter->b ){
224413      *piCol = -1;
224414    }else{
224415      int iIncr;
224416      pIter->a += fts5GetVarint32(&pIter->a[0], iIncr);
224417      *piCol += (iIncr-2);
224418    }
224419  }else{
224420    while( 1 ){
224421      int dummy;
224422      if( pIter->a>=pIter->b ){
224423        *piCol = -1;
224424        return;
224425      }
224426      if( pIter->a[0]==0x01 ) break;
224427      pIter->a += fts5GetVarint32(pIter->a, dummy);
224428    }
224429    pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol);
224430  }
224431}
224432
224433static int fts5ApiPhraseFirstColumn(
224434  Fts5Context *pCtx,
224435  int iPhrase,
224436  Fts5PhraseIter *pIter,
224437  int *piCol
224438){
224439  int rc = SQLITE_OK;
224440  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224441  Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
224442
224443  if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
224444    Fts5Sorter *pSorter = pCsr->pSorter;
224445    int n;
224446    if( pSorter ){
224447      int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
224448      n = pSorter->aIdx[iPhrase] - i1;
224449      pIter->a = &pSorter->aPoslist[i1];
224450    }else{
224451      rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, iPhrase, &pIter->a, &n);
224452    }
224453    if( rc==SQLITE_OK ){
224454      pIter->b = &pIter->a[n];
224455      *piCol = 0;
224456      fts5ApiPhraseNextColumn(pCtx, pIter, piCol);
224457    }
224458  }else{
224459    int n;
224460    rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
224461    if( rc==SQLITE_OK ){
224462      pIter->b = &pIter->a[n];
224463      if( n<=0 ){
224464        *piCol = -1;
224465      }else if( pIter->a[0]==0x01 ){
224466        pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol);
224467      }else{
224468        *piCol = 0;
224469      }
224470    }
224471  }
224472
224473  return rc;
224474}
224475
224476
224477static int fts5ApiQueryPhrase(Fts5Context*, int, void*,
224478    int(*)(const Fts5ExtensionApi*, Fts5Context*, void*)
224479);
224480
224481static const Fts5ExtensionApi sFts5Api = {
224482  2,                            /* iVersion */
224483  fts5ApiUserData,
224484  fts5ApiColumnCount,
224485  fts5ApiRowCount,
224486  fts5ApiColumnTotalSize,
224487  fts5ApiTokenize,
224488  fts5ApiPhraseCount,
224489  fts5ApiPhraseSize,
224490  fts5ApiInstCount,
224491  fts5ApiInst,
224492  fts5ApiRowid,
224493  fts5ApiColumnText,
224494  fts5ApiColumnSize,
224495  fts5ApiQueryPhrase,
224496  fts5ApiSetAuxdata,
224497  fts5ApiGetAuxdata,
224498  fts5ApiPhraseFirst,
224499  fts5ApiPhraseNext,
224500  fts5ApiPhraseFirstColumn,
224501  fts5ApiPhraseNextColumn,
224502};
224503
224504/*
224505** Implementation of API function xQueryPhrase().
224506*/
224507static int fts5ApiQueryPhrase(
224508  Fts5Context *pCtx,
224509  int iPhrase,
224510  void *pUserData,
224511  int(*xCallback)(const Fts5ExtensionApi*, Fts5Context*, void*)
224512){
224513  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224514  Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
224515  int rc;
224516  Fts5Cursor *pNew = 0;
224517
224518  rc = fts5OpenMethod(pCsr->base.pVtab, (sqlite3_vtab_cursor**)&pNew);
224519  if( rc==SQLITE_OK ){
224520    pNew->ePlan = FTS5_PLAN_MATCH;
224521    pNew->iFirstRowid = SMALLEST_INT64;
224522    pNew->iLastRowid = LARGEST_INT64;
224523    pNew->base.pVtab = (sqlite3_vtab*)pTab;
224524    rc = sqlite3Fts5ExprClonePhrase(pCsr->pExpr, iPhrase, &pNew->pExpr);
224525  }
224526
224527  if( rc==SQLITE_OK ){
224528    for(rc = fts5CursorFirst(pTab, pNew, 0);
224529        rc==SQLITE_OK && CsrFlagTest(pNew, FTS5CSR_EOF)==0;
224530        rc = fts5NextMethod((sqlite3_vtab_cursor*)pNew)
224531    ){
224532      rc = xCallback(&sFts5Api, (Fts5Context*)pNew, pUserData);
224533      if( rc!=SQLITE_OK ){
224534        if( rc==SQLITE_DONE ) rc = SQLITE_OK;
224535        break;
224536      }
224537    }
224538  }
224539
224540  fts5CloseMethod((sqlite3_vtab_cursor*)pNew);
224541  return rc;
224542}
224543
224544static void fts5ApiInvoke(
224545  Fts5Auxiliary *pAux,
224546  Fts5Cursor *pCsr,
224547  sqlite3_context *context,
224548  int argc,
224549  sqlite3_value **argv
224550){
224551  assert( pCsr->pAux==0 );
224552  pCsr->pAux = pAux;
224553  pAux->xFunc(&sFts5Api, (Fts5Context*)pCsr, context, argc, argv);
224554  pCsr->pAux = 0;
224555}
224556
224557static Fts5Cursor *fts5CursorFromCsrid(Fts5Global *pGlobal, i64 iCsrId){
224558  Fts5Cursor *pCsr;
224559  for(pCsr=pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
224560    if( pCsr->iCsrId==iCsrId ) break;
224561  }
224562  return pCsr;
224563}
224564
224565static void fts5ApiCallback(
224566  sqlite3_context *context,
224567  int argc,
224568  sqlite3_value **argv
224569){
224570
224571  Fts5Auxiliary *pAux;
224572  Fts5Cursor *pCsr;
224573  i64 iCsrId;
224574
224575  assert( argc>=1 );
224576  pAux = (Fts5Auxiliary*)sqlite3_user_data(context);
224577  iCsrId = sqlite3_value_int64(argv[0]);
224578
224579  pCsr = fts5CursorFromCsrid(pAux->pGlobal, iCsrId);
224580  if( pCsr==0 || pCsr->ePlan==0 ){
224581    char *zErr = sqlite3_mprintf("no such cursor: %lld", iCsrId);
224582    sqlite3_result_error(context, zErr, -1);
224583    sqlite3_free(zErr);
224584  }else{
224585    fts5ApiInvoke(pAux, pCsr, context, argc-1, &argv[1]);
224586  }
224587}
224588
224589
224590/*
224591** Given cursor id iId, return a pointer to the corresponding Fts5Table
224592** object. Or NULL If the cursor id does not exist.
224593*/
224594static Fts5Table *sqlite3Fts5TableFromCsrid(
224595  Fts5Global *pGlobal,            /* FTS5 global context for db handle */
224596  i64 iCsrId                      /* Id of cursor to find */
224597){
224598  Fts5Cursor *pCsr;
224599  pCsr = fts5CursorFromCsrid(pGlobal, iCsrId);
224600  if( pCsr ){
224601    return (Fts5Table*)pCsr->base.pVtab;
224602  }
224603  return 0;
224604}
224605
224606/*
224607** Return a "position-list blob" corresponding to the current position of
224608** cursor pCsr via sqlite3_result_blob(). A position-list blob contains
224609** the current position-list for each phrase in the query associated with
224610** cursor pCsr.
224611**
224612** A position-list blob begins with (nPhrase-1) varints, where nPhrase is
224613** the number of phrases in the query. Following the varints are the
224614** concatenated position lists for each phrase, in order.
224615**
224616** The first varint (if it exists) contains the size of the position list
224617** for phrase 0. The second (same disclaimer) contains the size of position
224618** list 1. And so on. There is no size field for the final position list,
224619** as it can be derived from the total size of the blob.
224620*/
224621static int fts5PoslistBlob(sqlite3_context *pCtx, Fts5Cursor *pCsr){
224622  int i;
224623  int rc = SQLITE_OK;
224624  int nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
224625  Fts5Buffer val;
224626
224627  memset(&val, 0, sizeof(Fts5Buffer));
224628  switch( ((Fts5Table*)(pCsr->base.pVtab))->pConfig->eDetail ){
224629    case FTS5_DETAIL_FULL:
224630
224631      /* Append the varints */
224632      for(i=0; i<(nPhrase-1); i++){
224633        const u8 *dummy;
224634        int nByte = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &dummy);
224635        sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
224636      }
224637
224638      /* Append the position lists */
224639      for(i=0; i<nPhrase; i++){
224640        const u8 *pPoslist;
224641        int nPoslist;
224642        nPoslist = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &pPoslist);
224643        sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
224644      }
224645      break;
224646
224647    case FTS5_DETAIL_COLUMNS:
224648
224649      /* Append the varints */
224650      for(i=0; rc==SQLITE_OK && i<(nPhrase-1); i++){
224651        const u8 *dummy;
224652        int nByte;
224653        rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &dummy, &nByte);
224654        sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
224655      }
224656
224657      /* Append the position lists */
224658      for(i=0; rc==SQLITE_OK && i<nPhrase; i++){
224659        const u8 *pPoslist;
224660        int nPoslist;
224661        rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &pPoslist, &nPoslist);
224662        sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
224663      }
224664      break;
224665
224666    default:
224667      break;
224668  }
224669
224670  sqlite3_result_blob(pCtx, val.p, val.n, sqlite3_free);
224671  return rc;
224672}
224673
224674/*
224675** This is the xColumn method, called by SQLite to request a value from
224676** the row that the supplied cursor currently points to.
224677*/
224678static int fts5ColumnMethod(
224679  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
224680  sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
224681  int iCol                        /* Index of column to read value from */
224682){
224683  Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
224684  Fts5Config *pConfig = pTab->p.pConfig;
224685  Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
224686  int rc = SQLITE_OK;
224687
224688  assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 );
224689
224690  if( pCsr->ePlan==FTS5_PLAN_SPECIAL ){
224691    if( iCol==pConfig->nCol ){
224692      sqlite3_result_int64(pCtx, pCsr->iSpecial);
224693    }
224694  }else
224695
224696  if( iCol==pConfig->nCol ){
224697    /* User is requesting the value of the special column with the same name
224698    ** as the table. Return the cursor integer id number. This value is only
224699    ** useful in that it may be passed as the first argument to an FTS5
224700    ** auxiliary function.  */
224701    sqlite3_result_int64(pCtx, pCsr->iCsrId);
224702  }else if( iCol==pConfig->nCol+1 ){
224703
224704    /* The value of the "rank" column. */
224705    if( pCsr->ePlan==FTS5_PLAN_SOURCE ){
224706      fts5PoslistBlob(pCtx, pCsr);
224707    }else if(
224708        pCsr->ePlan==FTS5_PLAN_MATCH
224709     || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH
224710    ){
224711      if( pCsr->pRank || SQLITE_OK==(rc = fts5FindRankFunction(pCsr)) ){
224712        fts5ApiInvoke(pCsr->pRank, pCsr, pCtx, pCsr->nRankArg, pCsr->apRankArg);
224713      }
224714    }
224715  }else if( !fts5IsContentless(pTab) ){
224716    pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
224717    rc = fts5SeekCursor(pCsr, 1);
224718    if( rc==SQLITE_OK ){
224719      sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
224720    }
224721    pConfig->pzErrmsg = 0;
224722  }
224723  return rc;
224724}
224725
224726
224727/*
224728** This routine implements the xFindFunction method for the FTS3
224729** virtual table.
224730*/
224731static int fts5FindFunctionMethod(
224732  sqlite3_vtab *pVtab,            /* Virtual table handle */
224733  int nUnused,                    /* Number of SQL function arguments */
224734  const char *zName,              /* Name of SQL function */
224735  void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
224736  void **ppArg                    /* OUT: User data for *pxFunc */
224737){
224738  Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
224739  Fts5Auxiliary *pAux;
224740
224741  UNUSED_PARAM(nUnused);
224742  pAux = fts5FindAuxiliary(pTab, zName);
224743  if( pAux ){
224744    *pxFunc = fts5ApiCallback;
224745    *ppArg = (void*)pAux;
224746    return 1;
224747  }
224748
224749  /* No function of the specified name was found. Return 0. */
224750  return 0;
224751}
224752
224753/*
224754** Implementation of FTS5 xRename method. Rename an fts5 table.
224755*/
224756static int fts5RenameMethod(
224757  sqlite3_vtab *pVtab,            /* Virtual table handle */
224758  const char *zName               /* New name of table */
224759){
224760  Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
224761  return sqlite3Fts5StorageRename(pTab->pStorage, zName);
224762}
224763
224764static int sqlite3Fts5FlushToDisk(Fts5Table *pTab){
224765  fts5TripCursors((Fts5FullTable*)pTab);
224766  return sqlite3Fts5StorageSync(((Fts5FullTable*)pTab)->pStorage);
224767}
224768
224769/*
224770** The xSavepoint() method.
224771**
224772** Flush the contents of the pending-terms table to disk.
224773*/
224774static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
224775  UNUSED_PARAM(iSavepoint);  /* Call below is a no-op for NDEBUG builds */
224776  fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_SAVEPOINT, iSavepoint);
224777  return sqlite3Fts5FlushToDisk((Fts5Table*)pVtab);
224778}
224779
224780/*
224781** The xRelease() method.
224782**
224783** This is a no-op.
224784*/
224785static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
224786  UNUSED_PARAM(iSavepoint);  /* Call below is a no-op for NDEBUG builds */
224787  fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_RELEASE, iSavepoint);
224788  return sqlite3Fts5FlushToDisk((Fts5Table*)pVtab);
224789}
224790
224791/*
224792** The xRollbackTo() method.
224793**
224794** Discard the contents of the pending terms table.
224795*/
224796static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
224797  Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
224798  UNUSED_PARAM(iSavepoint);  /* Call below is a no-op for NDEBUG builds */
224799  fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint);
224800  fts5TripCursors(pTab);
224801  return sqlite3Fts5StorageRollback(pTab->pStorage);
224802}
224803
224804/*
224805** Register a new auxiliary function with global context pGlobal.
224806*/
224807static int fts5CreateAux(
224808  fts5_api *pApi,                 /* Global context (one per db handle) */
224809  const char *zName,              /* Name of new function */
224810  void *pUserData,                /* User data for aux. function */
224811  fts5_extension_function xFunc,  /* Aux. function implementation */
224812  void(*xDestroy)(void*)          /* Destructor for pUserData */
224813){
224814  Fts5Global *pGlobal = (Fts5Global*)pApi;
224815  int rc = sqlite3_overload_function(pGlobal->db, zName, -1);
224816  if( rc==SQLITE_OK ){
224817    Fts5Auxiliary *pAux;
224818    sqlite3_int64 nName;            /* Size of zName in bytes, including \0 */
224819    sqlite3_int64 nByte;            /* Bytes of space to allocate */
224820
224821    nName = strlen(zName) + 1;
224822    nByte = sizeof(Fts5Auxiliary) + nName;
224823    pAux = (Fts5Auxiliary*)sqlite3_malloc64(nByte);
224824    if( pAux ){
224825      memset(pAux, 0, (size_t)nByte);
224826      pAux->zFunc = (char*)&pAux[1];
224827      memcpy(pAux->zFunc, zName, nName);
224828      pAux->pGlobal = pGlobal;
224829      pAux->pUserData = pUserData;
224830      pAux->xFunc = xFunc;
224831      pAux->xDestroy = xDestroy;
224832      pAux->pNext = pGlobal->pAux;
224833      pGlobal->pAux = pAux;
224834    }else{
224835      rc = SQLITE_NOMEM;
224836    }
224837  }
224838
224839  return rc;
224840}
224841
224842/*
224843** Register a new tokenizer. This is the implementation of the
224844** fts5_api.xCreateTokenizer() method.
224845*/
224846static int fts5CreateTokenizer(
224847  fts5_api *pApi,                 /* Global context (one per db handle) */
224848  const char *zName,              /* Name of new function */
224849  void *pUserData,                /* User data for aux. function */
224850  fts5_tokenizer *pTokenizer,     /* Tokenizer implementation */
224851  void(*xDestroy)(void*)          /* Destructor for pUserData */
224852){
224853  Fts5Global *pGlobal = (Fts5Global*)pApi;
224854  Fts5TokenizerModule *pNew;
224855  sqlite3_int64 nName;            /* Size of zName and its \0 terminator */
224856  sqlite3_int64 nByte;            /* Bytes of space to allocate */
224857  int rc = SQLITE_OK;
224858
224859  nName = strlen(zName) + 1;
224860  nByte = sizeof(Fts5TokenizerModule) + nName;
224861  pNew = (Fts5TokenizerModule*)sqlite3_malloc64(nByte);
224862  if( pNew ){
224863    memset(pNew, 0, (size_t)nByte);
224864    pNew->zName = (char*)&pNew[1];
224865    memcpy(pNew->zName, zName, nName);
224866    pNew->pUserData = pUserData;
224867    pNew->x = *pTokenizer;
224868    pNew->xDestroy = xDestroy;
224869    pNew->pNext = pGlobal->pTok;
224870    pGlobal->pTok = pNew;
224871    if( pNew->pNext==0 ){
224872      pGlobal->pDfltTok = pNew;
224873    }
224874  }else{
224875    rc = SQLITE_NOMEM;
224876  }
224877
224878  return rc;
224879}
224880
224881static Fts5TokenizerModule *fts5LocateTokenizer(
224882  Fts5Global *pGlobal,
224883  const char *zName
224884){
224885  Fts5TokenizerModule *pMod = 0;
224886
224887  if( zName==0 ){
224888    pMod = pGlobal->pDfltTok;
224889  }else{
224890    for(pMod=pGlobal->pTok; pMod; pMod=pMod->pNext){
224891      if( sqlite3_stricmp(zName, pMod->zName)==0 ) break;
224892    }
224893  }
224894
224895  return pMod;
224896}
224897
224898/*
224899** Find a tokenizer. This is the implementation of the
224900** fts5_api.xFindTokenizer() method.
224901*/
224902static int fts5FindTokenizer(
224903  fts5_api *pApi,                 /* Global context (one per db handle) */
224904  const char *zName,              /* Name of new function */
224905  void **ppUserData,
224906  fts5_tokenizer *pTokenizer      /* Populate this object */
224907){
224908  int rc = SQLITE_OK;
224909  Fts5TokenizerModule *pMod;
224910
224911  pMod = fts5LocateTokenizer((Fts5Global*)pApi, zName);
224912  if( pMod ){
224913    *pTokenizer = pMod->x;
224914    *ppUserData = pMod->pUserData;
224915  }else{
224916    memset(pTokenizer, 0, sizeof(fts5_tokenizer));
224917    rc = SQLITE_ERROR;
224918  }
224919
224920  return rc;
224921}
224922
224923static int sqlite3Fts5GetTokenizer(
224924  Fts5Global *pGlobal,
224925  const char **azArg,
224926  int nArg,
224927  Fts5Tokenizer **ppTok,
224928  fts5_tokenizer **ppTokApi,
224929  char **pzErr
224930){
224931  Fts5TokenizerModule *pMod;
224932  int rc = SQLITE_OK;
224933
224934  pMod = fts5LocateTokenizer(pGlobal, nArg==0 ? 0 : azArg[0]);
224935  if( pMod==0 ){
224936    assert( nArg>0 );
224937    rc = SQLITE_ERROR;
224938    *pzErr = sqlite3_mprintf("no such tokenizer: %s", azArg[0]);
224939  }else{
224940    rc = pMod->x.xCreate(pMod->pUserData, &azArg[1], (nArg?nArg-1:0), ppTok);
224941    *ppTokApi = &pMod->x;
224942    if( rc!=SQLITE_OK && pzErr ){
224943      *pzErr = sqlite3_mprintf("error in tokenizer constructor");
224944    }
224945  }
224946
224947  if( rc!=SQLITE_OK ){
224948    *ppTokApi = 0;
224949    *ppTok = 0;
224950  }
224951
224952  return rc;
224953}
224954
224955static void fts5ModuleDestroy(void *pCtx){
224956  Fts5TokenizerModule *pTok, *pNextTok;
224957  Fts5Auxiliary *pAux, *pNextAux;
224958  Fts5Global *pGlobal = (Fts5Global*)pCtx;
224959
224960  for(pAux=pGlobal->pAux; pAux; pAux=pNextAux){
224961    pNextAux = pAux->pNext;
224962    if( pAux->xDestroy ) pAux->xDestroy(pAux->pUserData);
224963    sqlite3_free(pAux);
224964  }
224965
224966  for(pTok=pGlobal->pTok; pTok; pTok=pNextTok){
224967    pNextTok = pTok->pNext;
224968    if( pTok->xDestroy ) pTok->xDestroy(pTok->pUserData);
224969    sqlite3_free(pTok);
224970  }
224971
224972  sqlite3_free(pGlobal);
224973}
224974
224975static void fts5Fts5Func(
224976  sqlite3_context *pCtx,          /* Function call context */
224977  int nArg,                       /* Number of args */
224978  sqlite3_value **apArg           /* Function arguments */
224979){
224980  Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
224981  fts5_api **ppApi;
224982  UNUSED_PARAM(nArg);
224983  assert( nArg==1 );
224984  ppApi = (fts5_api**)sqlite3_value_pointer(apArg[0], "fts5_api_ptr");
224985  if( ppApi ) *ppApi = &pGlobal->api;
224986}
224987
224988/*
224989** Implementation of fts5_source_id() function.
224990*/
224991static void fts5SourceIdFunc(
224992  sqlite3_context *pCtx,          /* Function call context */
224993  int nArg,                       /* Number of args */
224994  sqlite3_value **apUnused        /* Function arguments */
224995){
224996  assert( nArg==0 );
224997  UNUSED_PARAM2(nArg, apUnused);
224998  sqlite3_result_text(pCtx, "fts5: 2020-06-18 14:00:33 7ebdfa80be8e8e73324b8d66b3460222eb74c7e9dfd655b48d6ca7e1933cc8fd", -1, SQLITE_TRANSIENT);
224999}
225000
225001/*
225002** Return true if zName is the extension on one of the shadow tables used
225003** by this module.
225004*/
225005static int fts5ShadowName(const char *zName){
225006  static const char *azName[] = {
225007    "config", "content", "data", "docsize", "idx"
225008  };
225009  unsigned int i;
225010  for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
225011    if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
225012  }
225013  return 0;
225014}
225015
225016static int fts5Init(sqlite3 *db){
225017  static const sqlite3_module fts5Mod = {
225018    /* iVersion      */ 3,
225019    /* xCreate       */ fts5CreateMethod,
225020    /* xConnect      */ fts5ConnectMethod,
225021    /* xBestIndex    */ fts5BestIndexMethod,
225022    /* xDisconnect   */ fts5DisconnectMethod,
225023    /* xDestroy      */ fts5DestroyMethod,
225024    /* xOpen         */ fts5OpenMethod,
225025    /* xClose        */ fts5CloseMethod,
225026    /* xFilter       */ fts5FilterMethod,
225027    /* xNext         */ fts5NextMethod,
225028    /* xEof          */ fts5EofMethod,
225029    /* xColumn       */ fts5ColumnMethod,
225030    /* xRowid        */ fts5RowidMethod,
225031    /* xUpdate       */ fts5UpdateMethod,
225032    /* xBegin        */ fts5BeginMethod,
225033    /* xSync         */ fts5SyncMethod,
225034    /* xCommit       */ fts5CommitMethod,
225035    /* xRollback     */ fts5RollbackMethod,
225036    /* xFindFunction */ fts5FindFunctionMethod,
225037    /* xRename       */ fts5RenameMethod,
225038    /* xSavepoint    */ fts5SavepointMethod,
225039    /* xRelease      */ fts5ReleaseMethod,
225040    /* xRollbackTo   */ fts5RollbackToMethod,
225041    /* xShadowName   */ fts5ShadowName
225042  };
225043
225044  int rc;
225045  Fts5Global *pGlobal = 0;
225046
225047  pGlobal = (Fts5Global*)sqlite3_malloc(sizeof(Fts5Global));
225048  if( pGlobal==0 ){
225049    rc = SQLITE_NOMEM;
225050  }else{
225051    void *p = (void*)pGlobal;
225052    memset(pGlobal, 0, sizeof(Fts5Global));
225053    pGlobal->db = db;
225054    pGlobal->api.iVersion = 2;
225055    pGlobal->api.xCreateFunction = fts5CreateAux;
225056    pGlobal->api.xCreateTokenizer = fts5CreateTokenizer;
225057    pGlobal->api.xFindTokenizer = fts5FindTokenizer;
225058    rc = sqlite3_create_module_v2(db, "fts5", &fts5Mod, p, fts5ModuleDestroy);
225059    if( rc==SQLITE_OK ) rc = sqlite3Fts5IndexInit(db);
225060    if( rc==SQLITE_OK ) rc = sqlite3Fts5ExprInit(pGlobal, db);
225061    if( rc==SQLITE_OK ) rc = sqlite3Fts5AuxInit(&pGlobal->api);
225062    if( rc==SQLITE_OK ) rc = sqlite3Fts5TokenizerInit(&pGlobal->api);
225063    if( rc==SQLITE_OK ) rc = sqlite3Fts5VocabInit(pGlobal, db);
225064    if( rc==SQLITE_OK ){
225065      rc = sqlite3_create_function(
225066          db, "fts5", 1, SQLITE_UTF8, p, fts5Fts5Func, 0, 0
225067      );
225068    }
225069    if( rc==SQLITE_OK ){
225070      rc = sqlite3_create_function(
225071          db, "fts5_source_id", 0, SQLITE_UTF8, p, fts5SourceIdFunc, 0, 0
225072      );
225073    }
225074  }
225075
225076  /* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file
225077  ** fts5_test_mi.c is compiled and linked into the executable. And call
225078  ** its entry point to enable the matchinfo() demo.  */
225079#ifdef SQLITE_FTS5_ENABLE_TEST_MI
225080  if( rc==SQLITE_OK ){
225081    extern int sqlite3Fts5TestRegisterMatchinfo(sqlite3*);
225082    rc = sqlite3Fts5TestRegisterMatchinfo(db);
225083  }
225084#endif
225085
225086  return rc;
225087}
225088
225089/*
225090** The following functions are used to register the module with SQLite. If
225091** this module is being built as part of the SQLite core (SQLITE_CORE is
225092** defined), then sqlite3_open() will call sqlite3Fts5Init() directly.
225093**
225094** Or, if this module is being built as a loadable extension,
225095** sqlite3Fts5Init() is omitted and the two standard entry points
225096** sqlite3_fts_init() and sqlite3_fts5_init() defined instead.
225097*/
225098#ifndef SQLITE_CORE
225099#ifdef _WIN32
225100__declspec(dllexport)
225101#endif
225102SQLITE_API int sqlite3_fts_init(
225103  sqlite3 *db,
225104  char **pzErrMsg,
225105  const sqlite3_api_routines *pApi
225106){
225107  SQLITE_EXTENSION_INIT2(pApi);
225108  (void)pzErrMsg;  /* Unused parameter */
225109  return fts5Init(db);
225110}
225111
225112#ifdef _WIN32
225113__declspec(dllexport)
225114#endif
225115SQLITE_API int sqlite3_fts5_init(
225116  sqlite3 *db,
225117  char **pzErrMsg,
225118  const sqlite3_api_routines *pApi
225119){
225120  SQLITE_EXTENSION_INIT2(pApi);
225121  (void)pzErrMsg;  /* Unused parameter */
225122  return fts5Init(db);
225123}
225124#else
225125SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3 *db){
225126  return fts5Init(db);
225127}
225128#endif
225129
225130/*
225131** 2014 May 31
225132**
225133** The author disclaims copyright to this source code.  In place of
225134** a legal notice, here is a blessing:
225135**
225136**    May you do good and not evil.
225137**    May you find forgiveness for yourself and forgive others.
225138**    May you share freely, never taking more than you give.
225139**
225140******************************************************************************
225141**
225142*/
225143
225144
225145
225146/* #include "fts5Int.h" */
225147
225148struct Fts5Storage {
225149  Fts5Config *pConfig;
225150  Fts5Index *pIndex;
225151  int bTotalsValid;               /* True if nTotalRow/aTotalSize[] are valid */
225152  i64 nTotalRow;                  /* Total number of rows in FTS table */
225153  i64 *aTotalSize;                /* Total sizes of each column */
225154  sqlite3_stmt *aStmt[11];
225155};
225156
225157
225158#if FTS5_STMT_SCAN_ASC!=0
225159# error "FTS5_STMT_SCAN_ASC mismatch"
225160#endif
225161#if FTS5_STMT_SCAN_DESC!=1
225162# error "FTS5_STMT_SCAN_DESC mismatch"
225163#endif
225164#if FTS5_STMT_LOOKUP!=2
225165# error "FTS5_STMT_LOOKUP mismatch"
225166#endif
225167
225168#define FTS5_STMT_INSERT_CONTENT  3
225169#define FTS5_STMT_REPLACE_CONTENT 4
225170#define FTS5_STMT_DELETE_CONTENT  5
225171#define FTS5_STMT_REPLACE_DOCSIZE  6
225172#define FTS5_STMT_DELETE_DOCSIZE  7
225173#define FTS5_STMT_LOOKUP_DOCSIZE  8
225174#define FTS5_STMT_REPLACE_CONFIG 9
225175#define FTS5_STMT_SCAN 10
225176
225177/*
225178** Prepare the two insert statements - Fts5Storage.pInsertContent and
225179** Fts5Storage.pInsertDocsize - if they have not already been prepared.
225180** Return SQLITE_OK if successful, or an SQLite error code if an error
225181** occurs.
225182*/
225183static int fts5StorageGetStmt(
225184  Fts5Storage *p,                 /* Storage handle */
225185  int eStmt,                      /* FTS5_STMT_XXX constant */
225186  sqlite3_stmt **ppStmt,          /* OUT: Prepared statement handle */
225187  char **pzErrMsg                 /* OUT: Error message (if any) */
225188){
225189  int rc = SQLITE_OK;
225190
225191  /* If there is no %_docsize table, there should be no requests for
225192  ** statements to operate on it.  */
225193  assert( p->pConfig->bColumnsize || (
225194        eStmt!=FTS5_STMT_REPLACE_DOCSIZE
225195     && eStmt!=FTS5_STMT_DELETE_DOCSIZE
225196     && eStmt!=FTS5_STMT_LOOKUP_DOCSIZE
225197  ));
225198
225199  assert( eStmt>=0 && eStmt<ArraySize(p->aStmt) );
225200  if( p->aStmt[eStmt]==0 ){
225201    const char *azStmt[] = {
225202      "SELECT %s FROM %s T WHERE T.%Q >= ? AND T.%Q <= ? ORDER BY T.%Q ASC",
225203      "SELECT %s FROM %s T WHERE T.%Q <= ? AND T.%Q >= ? ORDER BY T.%Q DESC",
225204      "SELECT %s FROM %s T WHERE T.%Q=?",               /* LOOKUP  */
225205
225206      "INSERT INTO %Q.'%q_content' VALUES(%s)",         /* INSERT_CONTENT  */
225207      "REPLACE INTO %Q.'%q_content' VALUES(%s)",        /* REPLACE_CONTENT */
225208      "DELETE FROM %Q.'%q_content' WHERE id=?",         /* DELETE_CONTENT  */
225209      "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",       /* REPLACE_DOCSIZE  */
225210      "DELETE FROM %Q.'%q_docsize' WHERE id=?",         /* DELETE_DOCSIZE  */
225211
225212      "SELECT sz FROM %Q.'%q_docsize' WHERE id=?",      /* LOOKUP_DOCSIZE  */
225213
225214      "REPLACE INTO %Q.'%q_config' VALUES(?,?)",        /* REPLACE_CONFIG */
225215      "SELECT %s FROM %s AS T",                         /* SCAN */
225216    };
225217    Fts5Config *pC = p->pConfig;
225218    char *zSql = 0;
225219
225220    switch( eStmt ){
225221      case FTS5_STMT_SCAN:
225222        zSql = sqlite3_mprintf(azStmt[eStmt],
225223            pC->zContentExprlist, pC->zContent
225224        );
225225        break;
225226
225227      case FTS5_STMT_SCAN_ASC:
225228      case FTS5_STMT_SCAN_DESC:
225229        zSql = sqlite3_mprintf(azStmt[eStmt], pC->zContentExprlist,
225230            pC->zContent, pC->zContentRowid, pC->zContentRowid,
225231            pC->zContentRowid
225232        );
225233        break;
225234
225235      case FTS5_STMT_LOOKUP:
225236        zSql = sqlite3_mprintf(azStmt[eStmt],
225237            pC->zContentExprlist, pC->zContent, pC->zContentRowid
225238        );
225239        break;
225240
225241      case FTS5_STMT_INSERT_CONTENT:
225242      case FTS5_STMT_REPLACE_CONTENT: {
225243        int nCol = pC->nCol + 1;
225244        char *zBind;
225245        int i;
225246
225247        zBind = sqlite3_malloc64(1 + nCol*2);
225248        if( zBind ){
225249          for(i=0; i<nCol; i++){
225250            zBind[i*2] = '?';
225251            zBind[i*2 + 1] = ',';
225252          }
225253          zBind[i*2-1] = '\0';
225254          zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName, zBind);
225255          sqlite3_free(zBind);
225256        }
225257        break;
225258      }
225259
225260      default:
225261        zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName);
225262        break;
225263    }
225264
225265    if( zSql==0 ){
225266      rc = SQLITE_NOMEM;
225267    }else{
225268      int f = SQLITE_PREPARE_PERSISTENT;
225269      if( eStmt>FTS5_STMT_LOOKUP ) f |= SQLITE_PREPARE_NO_VTAB;
225270      p->pConfig->bLock++;
225271      rc = sqlite3_prepare_v3(pC->db, zSql, -1, f, &p->aStmt[eStmt], 0);
225272      p->pConfig->bLock--;
225273      sqlite3_free(zSql);
225274      if( rc!=SQLITE_OK && pzErrMsg ){
225275        *pzErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pC->db));
225276      }
225277    }
225278  }
225279
225280  *ppStmt = p->aStmt[eStmt];
225281  sqlite3_reset(*ppStmt);
225282  return rc;
225283}
225284
225285
225286static int fts5ExecPrintf(
225287  sqlite3 *db,
225288  char **pzErr,
225289  const char *zFormat,
225290  ...
225291){
225292  int rc;
225293  va_list ap;                     /* ... printf arguments */
225294  char *zSql;
225295
225296  va_start(ap, zFormat);
225297  zSql = sqlite3_vmprintf(zFormat, ap);
225298
225299  if( zSql==0 ){
225300    rc = SQLITE_NOMEM;
225301  }else{
225302    rc = sqlite3_exec(db, zSql, 0, 0, pzErr);
225303    sqlite3_free(zSql);
225304  }
225305
225306  va_end(ap);
225307  return rc;
225308}
225309
225310/*
225311** Drop all shadow tables. Return SQLITE_OK if successful or an SQLite error
225312** code otherwise.
225313*/
225314static int sqlite3Fts5DropAll(Fts5Config *pConfig){
225315  int rc = fts5ExecPrintf(pConfig->db, 0,
225316      "DROP TABLE IF EXISTS %Q.'%q_data';"
225317      "DROP TABLE IF EXISTS %Q.'%q_idx';"
225318      "DROP TABLE IF EXISTS %Q.'%q_config';",
225319      pConfig->zDb, pConfig->zName,
225320      pConfig->zDb, pConfig->zName,
225321      pConfig->zDb, pConfig->zName
225322  );
225323  if( rc==SQLITE_OK && pConfig->bColumnsize ){
225324    rc = fts5ExecPrintf(pConfig->db, 0,
225325        "DROP TABLE IF EXISTS %Q.'%q_docsize';",
225326        pConfig->zDb, pConfig->zName
225327    );
225328  }
225329  if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
225330    rc = fts5ExecPrintf(pConfig->db, 0,
225331        "DROP TABLE IF EXISTS %Q.'%q_content';",
225332        pConfig->zDb, pConfig->zName
225333    );
225334  }
225335  return rc;
225336}
225337
225338static void fts5StorageRenameOne(
225339  Fts5Config *pConfig,            /* Current FTS5 configuration */
225340  int *pRc,                       /* IN/OUT: Error code */
225341  const char *zTail,              /* Tail of table name e.g. "data", "config" */
225342  const char *zName               /* New name of FTS5 table */
225343){
225344  if( *pRc==SQLITE_OK ){
225345    *pRc = fts5ExecPrintf(pConfig->db, 0,
225346        "ALTER TABLE %Q.'%q_%s' RENAME TO '%q_%s';",
225347        pConfig->zDb, pConfig->zName, zTail, zName, zTail
225348    );
225349  }
225350}
225351
225352static int sqlite3Fts5StorageRename(Fts5Storage *pStorage, const char *zName){
225353  Fts5Config *pConfig = pStorage->pConfig;
225354  int rc = sqlite3Fts5StorageSync(pStorage);
225355
225356  fts5StorageRenameOne(pConfig, &rc, "data", zName);
225357  fts5StorageRenameOne(pConfig, &rc, "idx", zName);
225358  fts5StorageRenameOne(pConfig, &rc, "config", zName);
225359  if( pConfig->bColumnsize ){
225360    fts5StorageRenameOne(pConfig, &rc, "docsize", zName);
225361  }
225362  if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
225363    fts5StorageRenameOne(pConfig, &rc, "content", zName);
225364  }
225365  return rc;
225366}
225367
225368/*
225369** Create the shadow table named zPost, with definition zDefn. Return
225370** SQLITE_OK if successful, or an SQLite error code otherwise.
225371*/
225372static int sqlite3Fts5CreateTable(
225373  Fts5Config *pConfig,            /* FTS5 configuration */
225374  const char *zPost,              /* Shadow table to create (e.g. "content") */
225375  const char *zDefn,              /* Columns etc. for shadow table */
225376  int bWithout,                   /* True for without rowid */
225377  char **pzErr                    /* OUT: Error message */
225378){
225379  int rc;
225380  char *zErr = 0;
225381
225382  rc = fts5ExecPrintf(pConfig->db, &zErr, "CREATE TABLE %Q.'%q_%q'(%s)%s",
225383      pConfig->zDb, pConfig->zName, zPost, zDefn,
225384#ifndef SQLITE_FTS5_NO_WITHOUT_ROWID
225385      bWithout?" WITHOUT ROWID":
225386#endif
225387      ""
225388  );
225389  if( zErr ){
225390    *pzErr = sqlite3_mprintf(
225391        "fts5: error creating shadow table %q_%s: %s",
225392        pConfig->zName, zPost, zErr
225393    );
225394    sqlite3_free(zErr);
225395  }
225396
225397  return rc;
225398}
225399
225400/*
225401** Open a new Fts5Index handle. If the bCreate argument is true, create
225402** and initialize the underlying tables
225403**
225404** If successful, set *pp to point to the new object and return SQLITE_OK.
225405** Otherwise, set *pp to NULL and return an SQLite error code.
225406*/
225407static int sqlite3Fts5StorageOpen(
225408  Fts5Config *pConfig,
225409  Fts5Index *pIndex,
225410  int bCreate,
225411  Fts5Storage **pp,
225412  char **pzErr                    /* OUT: Error message */
225413){
225414  int rc = SQLITE_OK;
225415  Fts5Storage *p;                 /* New object */
225416  sqlite3_int64 nByte;            /* Bytes of space to allocate */
225417
225418  nByte = sizeof(Fts5Storage)               /* Fts5Storage object */
225419        + pConfig->nCol * sizeof(i64);      /* Fts5Storage.aTotalSize[] */
225420  *pp = p = (Fts5Storage*)sqlite3_malloc64(nByte);
225421  if( !p ) return SQLITE_NOMEM;
225422
225423  memset(p, 0, (size_t)nByte);
225424  p->aTotalSize = (i64*)&p[1];
225425  p->pConfig = pConfig;
225426  p->pIndex = pIndex;
225427
225428  if( bCreate ){
225429    if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
225430      int nDefn = 32 + pConfig->nCol*10;
225431      char *zDefn = sqlite3_malloc64(32 + (sqlite3_int64)pConfig->nCol * 10);
225432      if( zDefn==0 ){
225433        rc = SQLITE_NOMEM;
225434      }else{
225435        int i;
225436        int iOff;
225437        sqlite3_snprintf(nDefn, zDefn, "id INTEGER PRIMARY KEY");
225438        iOff = (int)strlen(zDefn);
225439        for(i=0; i<pConfig->nCol; i++){
225440          sqlite3_snprintf(nDefn-iOff, &zDefn[iOff], ", c%d", i);
225441          iOff += (int)strlen(&zDefn[iOff]);
225442        }
225443        rc = sqlite3Fts5CreateTable(pConfig, "content", zDefn, 0, pzErr);
225444      }
225445      sqlite3_free(zDefn);
225446    }
225447
225448    if( rc==SQLITE_OK && pConfig->bColumnsize ){
225449      rc = sqlite3Fts5CreateTable(
225450          pConfig, "docsize", "id INTEGER PRIMARY KEY, sz BLOB", 0, pzErr
225451      );
225452    }
225453    if( rc==SQLITE_OK ){
225454      rc = sqlite3Fts5CreateTable(
225455          pConfig, "config", "k PRIMARY KEY, v", 1, pzErr
225456      );
225457    }
225458    if( rc==SQLITE_OK ){
225459      rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
225460    }
225461  }
225462
225463  if( rc ){
225464    sqlite3Fts5StorageClose(p);
225465    *pp = 0;
225466  }
225467  return rc;
225468}
225469
225470/*
225471** Close a handle opened by an earlier call to sqlite3Fts5StorageOpen().
225472*/
225473static int sqlite3Fts5StorageClose(Fts5Storage *p){
225474  int rc = SQLITE_OK;
225475  if( p ){
225476    int i;
225477
225478    /* Finalize all SQL statements */
225479    for(i=0; i<ArraySize(p->aStmt); i++){
225480      sqlite3_finalize(p->aStmt[i]);
225481    }
225482
225483    sqlite3_free(p);
225484  }
225485  return rc;
225486}
225487
225488typedef struct Fts5InsertCtx Fts5InsertCtx;
225489struct Fts5InsertCtx {
225490  Fts5Storage *pStorage;
225491  int iCol;
225492  int szCol;                      /* Size of column value in tokens */
225493};
225494
225495/*
225496** Tokenization callback used when inserting tokens into the FTS index.
225497*/
225498static int fts5StorageInsertCallback(
225499  void *pContext,                 /* Pointer to Fts5InsertCtx object */
225500  int tflags,
225501  const char *pToken,             /* Buffer containing token */
225502  int nToken,                     /* Size of token in bytes */
225503  int iUnused1,                   /* Start offset of token */
225504  int iUnused2                    /* End offset of token */
225505){
225506  Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext;
225507  Fts5Index *pIdx = pCtx->pStorage->pIndex;
225508  UNUSED_PARAM2(iUnused1, iUnused2);
225509  if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
225510  if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
225511    pCtx->szCol++;
225512  }
225513  return sqlite3Fts5IndexWrite(pIdx, pCtx->iCol, pCtx->szCol-1, pToken, nToken);
225514}
225515
225516/*
225517** If a row with rowid iDel is present in the %_content table, add the
225518** delete-markers to the FTS index necessary to delete it. Do not actually
225519** remove the %_content row at this time though.
225520*/
225521static int fts5StorageDeleteFromIndex(
225522  Fts5Storage *p,
225523  i64 iDel,
225524  sqlite3_value **apVal
225525){
225526  Fts5Config *pConfig = p->pConfig;
225527  sqlite3_stmt *pSeek = 0;        /* SELECT to read row iDel from %_data */
225528  int rc;                         /* Return code */
225529  int rc2;                        /* sqlite3_reset() return code */
225530  int iCol;
225531  Fts5InsertCtx ctx;
225532
225533  if( apVal==0 ){
225534    rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP, &pSeek, 0);
225535    if( rc!=SQLITE_OK ) return rc;
225536    sqlite3_bind_int64(pSeek, 1, iDel);
225537    if( sqlite3_step(pSeek)!=SQLITE_ROW ){
225538      return sqlite3_reset(pSeek);
225539    }
225540  }
225541
225542  ctx.pStorage = p;
225543  ctx.iCol = -1;
225544  rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 1, iDel);
225545  for(iCol=1; rc==SQLITE_OK && iCol<=pConfig->nCol; iCol++){
225546    if( pConfig->abUnindexed[iCol-1]==0 ){
225547      const char *zText;
225548      int nText;
225549      if( pSeek ){
225550        zText = (const char*)sqlite3_column_text(pSeek, iCol);
225551        nText = sqlite3_column_bytes(pSeek, iCol);
225552      }else{
225553        zText = (const char*)sqlite3_value_text(apVal[iCol-1]);
225554        nText = sqlite3_value_bytes(apVal[iCol-1]);
225555      }
225556      ctx.szCol = 0;
225557      rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT,
225558          zText, nText, (void*)&ctx, fts5StorageInsertCallback
225559      );
225560      p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
225561    }
225562  }
225563  p->nTotalRow--;
225564
225565  rc2 = sqlite3_reset(pSeek);
225566  if( rc==SQLITE_OK ) rc = rc2;
225567  return rc;
225568}
225569
225570
225571/*
225572** Insert a record into the %_docsize table. Specifically, do:
225573**
225574**   INSERT OR REPLACE INTO %_docsize(id, sz) VALUES(iRowid, pBuf);
225575**
225576** If there is no %_docsize table (as happens if the columnsize=0 option
225577** is specified when the FTS5 table is created), this function is a no-op.
225578*/
225579static int fts5StorageInsertDocsize(
225580  Fts5Storage *p,                 /* Storage module to write to */
225581  i64 iRowid,                     /* id value */
225582  Fts5Buffer *pBuf                /* sz value */
225583){
225584  int rc = SQLITE_OK;
225585  if( p->pConfig->bColumnsize ){
225586    sqlite3_stmt *pReplace = 0;
225587    rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
225588    if( rc==SQLITE_OK ){
225589      sqlite3_bind_int64(pReplace, 1, iRowid);
225590      sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC);
225591      sqlite3_step(pReplace);
225592      rc = sqlite3_reset(pReplace);
225593      sqlite3_bind_null(pReplace, 2);
225594    }
225595  }
225596  return rc;
225597}
225598
225599/*
225600** Load the contents of the "averages" record from disk into the
225601** p->nTotalRow and p->aTotalSize[] variables. If successful, and if
225602** argument bCache is true, set the p->bTotalsValid flag to indicate
225603** that the contents of aTotalSize[] and nTotalRow are valid until
225604** further notice.
225605**
225606** Return SQLITE_OK if successful, or an SQLite error code if an error
225607** occurs.
225608*/
225609static int fts5StorageLoadTotals(Fts5Storage *p, int bCache){
225610  int rc = SQLITE_OK;
225611  if( p->bTotalsValid==0 ){
225612    rc = sqlite3Fts5IndexGetAverages(p->pIndex, &p->nTotalRow, p->aTotalSize);
225613    p->bTotalsValid = bCache;
225614  }
225615  return rc;
225616}
225617
225618/*
225619** Store the current contents of the p->nTotalRow and p->aTotalSize[]
225620** variables in the "averages" record on disk.
225621**
225622** Return SQLITE_OK if successful, or an SQLite error code if an error
225623** occurs.
225624*/
225625static int fts5StorageSaveTotals(Fts5Storage *p){
225626  int nCol = p->pConfig->nCol;
225627  int i;
225628  Fts5Buffer buf;
225629  int rc = SQLITE_OK;
225630  memset(&buf, 0, sizeof(buf));
225631
225632  sqlite3Fts5BufferAppendVarint(&rc, &buf, p->nTotalRow);
225633  for(i=0; i<nCol; i++){
225634    sqlite3Fts5BufferAppendVarint(&rc, &buf, p->aTotalSize[i]);
225635  }
225636  if( rc==SQLITE_OK ){
225637    rc = sqlite3Fts5IndexSetAverages(p->pIndex, buf.p, buf.n);
225638  }
225639  sqlite3_free(buf.p);
225640
225641  return rc;
225642}
225643
225644/*
225645** Remove a row from the FTS table.
225646*/
225647static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64 iDel, sqlite3_value **apVal){
225648  Fts5Config *pConfig = p->pConfig;
225649  int rc;
225650  sqlite3_stmt *pDel = 0;
225651
225652  assert( pConfig->eContent!=FTS5_CONTENT_NORMAL || apVal==0 );
225653  rc = fts5StorageLoadTotals(p, 1);
225654
225655  /* Delete the index records */
225656  if( rc==SQLITE_OK ){
225657    rc = fts5StorageDeleteFromIndex(p, iDel, apVal);
225658  }
225659
225660  /* Delete the %_docsize record */
225661  if( rc==SQLITE_OK && pConfig->bColumnsize ){
225662    rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_DOCSIZE, &pDel, 0);
225663    if( rc==SQLITE_OK ){
225664      sqlite3_bind_int64(pDel, 1, iDel);
225665      sqlite3_step(pDel);
225666      rc = sqlite3_reset(pDel);
225667    }
225668  }
225669
225670  /* Delete the %_content record */
225671  if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
225672    if( rc==SQLITE_OK ){
225673      rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_CONTENT, &pDel, 0);
225674    }
225675    if( rc==SQLITE_OK ){
225676      sqlite3_bind_int64(pDel, 1, iDel);
225677      sqlite3_step(pDel);
225678      rc = sqlite3_reset(pDel);
225679    }
225680  }
225681
225682  return rc;
225683}
225684
225685/*
225686** Delete all entries in the FTS5 index.
225687*/
225688static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){
225689  Fts5Config *pConfig = p->pConfig;
225690  int rc;
225691
225692  p->bTotalsValid = 0;
225693
225694  /* Delete the contents of the %_data and %_docsize tables. */
225695  rc = fts5ExecPrintf(pConfig->db, 0,
225696      "DELETE FROM %Q.'%q_data';"
225697      "DELETE FROM %Q.'%q_idx';",
225698      pConfig->zDb, pConfig->zName,
225699      pConfig->zDb, pConfig->zName
225700  );
225701  if( rc==SQLITE_OK && pConfig->bColumnsize ){
225702    rc = fts5ExecPrintf(pConfig->db, 0,
225703        "DELETE FROM %Q.'%q_docsize';",
225704        pConfig->zDb, pConfig->zName
225705    );
225706  }
225707
225708  /* Reinitialize the %_data table. This call creates the initial structure
225709  ** and averages records.  */
225710  if( rc==SQLITE_OK ){
225711    rc = sqlite3Fts5IndexReinit(p->pIndex);
225712  }
225713  if( rc==SQLITE_OK ){
225714    rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
225715  }
225716  return rc;
225717}
225718
225719static int sqlite3Fts5StorageRebuild(Fts5Storage *p){
225720  Fts5Buffer buf = {0,0,0};
225721  Fts5Config *pConfig = p->pConfig;
225722  sqlite3_stmt *pScan = 0;
225723  Fts5InsertCtx ctx;
225724  int rc, rc2;
225725
225726  memset(&ctx, 0, sizeof(Fts5InsertCtx));
225727  ctx.pStorage = p;
225728  rc = sqlite3Fts5StorageDeleteAll(p);
225729  if( rc==SQLITE_OK ){
225730    rc = fts5StorageLoadTotals(p, 1);
225731  }
225732
225733  if( rc==SQLITE_OK ){
225734    rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0);
225735  }
225736
225737  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pScan) ){
225738    i64 iRowid = sqlite3_column_int64(pScan, 0);
225739
225740    sqlite3Fts5BufferZero(&buf);
225741    rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
225742    for(ctx.iCol=0; rc==SQLITE_OK && ctx.iCol<pConfig->nCol; ctx.iCol++){
225743      ctx.szCol = 0;
225744      if( pConfig->abUnindexed[ctx.iCol]==0 ){
225745        const char *zText = (const char*)sqlite3_column_text(pScan, ctx.iCol+1);
225746        int nText = sqlite3_column_bytes(pScan, ctx.iCol+1);
225747        rc = sqlite3Fts5Tokenize(pConfig,
225748            FTS5_TOKENIZE_DOCUMENT,
225749            zText, nText,
225750            (void*)&ctx,
225751            fts5StorageInsertCallback
225752        );
225753      }
225754      sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
225755      p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
225756    }
225757    p->nTotalRow++;
225758
225759    if( rc==SQLITE_OK ){
225760      rc = fts5StorageInsertDocsize(p, iRowid, &buf);
225761    }
225762  }
225763  sqlite3_free(buf.p);
225764  rc2 = sqlite3_reset(pScan);
225765  if( rc==SQLITE_OK ) rc = rc2;
225766
225767  /* Write the averages record */
225768  if( rc==SQLITE_OK ){
225769    rc = fts5StorageSaveTotals(p);
225770  }
225771  return rc;
225772}
225773
225774static int sqlite3Fts5StorageOptimize(Fts5Storage *p){
225775  return sqlite3Fts5IndexOptimize(p->pIndex);
225776}
225777
225778static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge){
225779  return sqlite3Fts5IndexMerge(p->pIndex, nMerge);
225780}
225781
225782static int sqlite3Fts5StorageReset(Fts5Storage *p){
225783  return sqlite3Fts5IndexReset(p->pIndex);
225784}
225785
225786/*
225787** Allocate a new rowid. This is used for "external content" tables when
225788** a NULL value is inserted into the rowid column. The new rowid is allocated
225789** by inserting a dummy row into the %_docsize table. The dummy will be
225790** overwritten later.
225791**
225792** If the %_docsize table does not exist, SQLITE_MISMATCH is returned. In
225793** this case the user is required to provide a rowid explicitly.
225794*/
225795static int fts5StorageNewRowid(Fts5Storage *p, i64 *piRowid){
225796  int rc = SQLITE_MISMATCH;
225797  if( p->pConfig->bColumnsize ){
225798    sqlite3_stmt *pReplace = 0;
225799    rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
225800    if( rc==SQLITE_OK ){
225801      sqlite3_bind_null(pReplace, 1);
225802      sqlite3_bind_null(pReplace, 2);
225803      sqlite3_step(pReplace);
225804      rc = sqlite3_reset(pReplace);
225805    }
225806    if( rc==SQLITE_OK ){
225807      *piRowid = sqlite3_last_insert_rowid(p->pConfig->db);
225808    }
225809  }
225810  return rc;
225811}
225812
225813/*
225814** Insert a new row into the FTS content table.
225815*/
225816static int sqlite3Fts5StorageContentInsert(
225817  Fts5Storage *p,
225818  sqlite3_value **apVal,
225819  i64 *piRowid
225820){
225821  Fts5Config *pConfig = p->pConfig;
225822  int rc = SQLITE_OK;
225823
225824  /* Insert the new row into the %_content table. */
225825  if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
225826    if( sqlite3_value_type(apVal[1])==SQLITE_INTEGER ){
225827      *piRowid = sqlite3_value_int64(apVal[1]);
225828    }else{
225829      rc = fts5StorageNewRowid(p, piRowid);
225830    }
225831  }else{
225832    sqlite3_stmt *pInsert = 0;    /* Statement to write %_content table */
225833    int i;                        /* Counter variable */
225834    rc = fts5StorageGetStmt(p, FTS5_STMT_INSERT_CONTENT, &pInsert, 0);
225835    for(i=1; rc==SQLITE_OK && i<=pConfig->nCol+1; i++){
225836      rc = sqlite3_bind_value(pInsert, i, apVal[i]);
225837    }
225838    if( rc==SQLITE_OK ){
225839      sqlite3_step(pInsert);
225840      rc = sqlite3_reset(pInsert);
225841    }
225842    *piRowid = sqlite3_last_insert_rowid(pConfig->db);
225843  }
225844
225845  return rc;
225846}
225847
225848/*
225849** Insert new entries into the FTS index and %_docsize table.
225850*/
225851static int sqlite3Fts5StorageIndexInsert(
225852  Fts5Storage *p,
225853  sqlite3_value **apVal,
225854  i64 iRowid
225855){
225856  Fts5Config *pConfig = p->pConfig;
225857  int rc = SQLITE_OK;             /* Return code */
225858  Fts5InsertCtx ctx;              /* Tokenization callback context object */
225859  Fts5Buffer buf;                 /* Buffer used to build up %_docsize blob */
225860
225861  memset(&buf, 0, sizeof(Fts5Buffer));
225862  ctx.pStorage = p;
225863  rc = fts5StorageLoadTotals(p, 1);
225864
225865  if( rc==SQLITE_OK ){
225866    rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
225867  }
225868  for(ctx.iCol=0; rc==SQLITE_OK && ctx.iCol<pConfig->nCol; ctx.iCol++){
225869    ctx.szCol = 0;
225870    if( pConfig->abUnindexed[ctx.iCol]==0 ){
225871      const char *zText = (const char*)sqlite3_value_text(apVal[ctx.iCol+2]);
225872      int nText = sqlite3_value_bytes(apVal[ctx.iCol+2]);
225873      rc = sqlite3Fts5Tokenize(pConfig,
225874          FTS5_TOKENIZE_DOCUMENT,
225875          zText, nText,
225876          (void*)&ctx,
225877          fts5StorageInsertCallback
225878      );
225879    }
225880    sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
225881    p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
225882  }
225883  p->nTotalRow++;
225884
225885  /* Write the %_docsize record */
225886  if( rc==SQLITE_OK ){
225887    rc = fts5StorageInsertDocsize(p, iRowid, &buf);
225888  }
225889  sqlite3_free(buf.p);
225890
225891  return rc;
225892}
225893
225894static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
225895  Fts5Config *pConfig = p->pConfig;
225896  char *zSql;
225897  int rc;
225898
225899  zSql = sqlite3_mprintf("SELECT count(*) FROM %Q.'%q_%s'",
225900      pConfig->zDb, pConfig->zName, zSuffix
225901  );
225902  if( zSql==0 ){
225903    rc = SQLITE_NOMEM;
225904  }else{
225905    sqlite3_stmt *pCnt = 0;
225906    rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pCnt, 0);
225907    if( rc==SQLITE_OK ){
225908      if( SQLITE_ROW==sqlite3_step(pCnt) ){
225909        *pnRow = sqlite3_column_int64(pCnt, 0);
225910      }
225911      rc = sqlite3_finalize(pCnt);
225912    }
225913  }
225914
225915  sqlite3_free(zSql);
225916  return rc;
225917}
225918
225919/*
225920** Context object used by sqlite3Fts5StorageIntegrity().
225921*/
225922typedef struct Fts5IntegrityCtx Fts5IntegrityCtx;
225923struct Fts5IntegrityCtx {
225924  i64 iRowid;
225925  int iCol;
225926  int szCol;
225927  u64 cksum;
225928  Fts5Termset *pTermset;
225929  Fts5Config *pConfig;
225930};
225931
225932
225933/*
225934** Tokenization callback used by integrity check.
225935*/
225936static int fts5StorageIntegrityCallback(
225937  void *pContext,                 /* Pointer to Fts5IntegrityCtx object */
225938  int tflags,
225939  const char *pToken,             /* Buffer containing token */
225940  int nToken,                     /* Size of token in bytes */
225941  int iUnused1,                   /* Start offset of token */
225942  int iUnused2                    /* End offset of token */
225943){
225944  Fts5IntegrityCtx *pCtx = (Fts5IntegrityCtx*)pContext;
225945  Fts5Termset *pTermset = pCtx->pTermset;
225946  int bPresent;
225947  int ii;
225948  int rc = SQLITE_OK;
225949  int iPos;
225950  int iCol;
225951
225952  UNUSED_PARAM2(iUnused1, iUnused2);
225953  if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
225954
225955  if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
225956    pCtx->szCol++;
225957  }
225958
225959  switch( pCtx->pConfig->eDetail ){
225960    case FTS5_DETAIL_FULL:
225961      iPos = pCtx->szCol-1;
225962      iCol = pCtx->iCol;
225963      break;
225964
225965    case FTS5_DETAIL_COLUMNS:
225966      iPos = pCtx->iCol;
225967      iCol = 0;
225968      break;
225969
225970    default:
225971      assert( pCtx->pConfig->eDetail==FTS5_DETAIL_NONE );
225972      iPos = 0;
225973      iCol = 0;
225974      break;
225975  }
225976
225977  rc = sqlite3Fts5TermsetAdd(pTermset, 0, pToken, nToken, &bPresent);
225978  if( rc==SQLITE_OK && bPresent==0 ){
225979    pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
225980        pCtx->iRowid, iCol, iPos, 0, pToken, nToken
225981    );
225982  }
225983
225984  for(ii=0; rc==SQLITE_OK && ii<pCtx->pConfig->nPrefix; ii++){
225985    const int nChar = pCtx->pConfig->aPrefix[ii];
225986    int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
225987    if( nByte ){
225988      rc = sqlite3Fts5TermsetAdd(pTermset, ii+1, pToken, nByte, &bPresent);
225989      if( bPresent==0 ){
225990        pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
225991            pCtx->iRowid, iCol, iPos, ii+1, pToken, nByte
225992        );
225993      }
225994    }
225995  }
225996
225997  return rc;
225998}
225999
226000/*
226001** Check that the contents of the FTS index match that of the %_content
226002** table. Return SQLITE_OK if they do, or SQLITE_CORRUPT if not. Return
226003** some other SQLite error code if an error occurs while attempting to
226004** determine this.
226005*/
226006static int sqlite3Fts5StorageIntegrity(Fts5Storage *p){
226007  Fts5Config *pConfig = p->pConfig;
226008  int rc;                         /* Return code */
226009  int *aColSize;                  /* Array of size pConfig->nCol */
226010  i64 *aTotalSize;                /* Array of size pConfig->nCol */
226011  Fts5IntegrityCtx ctx;
226012  sqlite3_stmt *pScan;
226013
226014  memset(&ctx, 0, sizeof(Fts5IntegrityCtx));
226015  ctx.pConfig = p->pConfig;
226016  aTotalSize = (i64*)sqlite3_malloc64(pConfig->nCol*(sizeof(int)+sizeof(i64)));
226017  if( !aTotalSize ) return SQLITE_NOMEM;
226018  aColSize = (int*)&aTotalSize[pConfig->nCol];
226019  memset(aTotalSize, 0, sizeof(i64) * pConfig->nCol);
226020
226021  /* Generate the expected index checksum based on the contents of the
226022  ** %_content table. This block stores the checksum in ctx.cksum. */
226023  rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0);
226024  if( rc==SQLITE_OK ){
226025    int rc2;
226026    while( SQLITE_ROW==sqlite3_step(pScan) ){
226027      int i;
226028      ctx.iRowid = sqlite3_column_int64(pScan, 0);
226029      ctx.szCol = 0;
226030      if( pConfig->bColumnsize ){
226031        rc = sqlite3Fts5StorageDocsize(p, ctx.iRowid, aColSize);
226032      }
226033      if( rc==SQLITE_OK && pConfig->eDetail==FTS5_DETAIL_NONE ){
226034        rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
226035      }
226036      for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
226037        if( pConfig->abUnindexed[i] ) continue;
226038        ctx.iCol = i;
226039        ctx.szCol = 0;
226040        if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
226041          rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
226042        }
226043        if( rc==SQLITE_OK ){
226044          const char *zText = (const char*)sqlite3_column_text(pScan, i+1);
226045          int nText = sqlite3_column_bytes(pScan, i+1);
226046          rc = sqlite3Fts5Tokenize(pConfig,
226047              FTS5_TOKENIZE_DOCUMENT,
226048              zText, nText,
226049              (void*)&ctx,
226050              fts5StorageIntegrityCallback
226051          );
226052        }
226053        if( rc==SQLITE_OK && pConfig->bColumnsize && ctx.szCol!=aColSize[i] ){
226054          rc = FTS5_CORRUPT;
226055        }
226056        aTotalSize[i] += ctx.szCol;
226057        if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
226058          sqlite3Fts5TermsetFree(ctx.pTermset);
226059          ctx.pTermset = 0;
226060        }
226061      }
226062      sqlite3Fts5TermsetFree(ctx.pTermset);
226063      ctx.pTermset = 0;
226064
226065      if( rc!=SQLITE_OK ) break;
226066    }
226067    rc2 = sqlite3_reset(pScan);
226068    if( rc==SQLITE_OK ) rc = rc2;
226069  }
226070
226071  /* Test that the "totals" (sometimes called "averages") record looks Ok */
226072  if( rc==SQLITE_OK ){
226073    int i;
226074    rc = fts5StorageLoadTotals(p, 0);
226075    for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
226076      if( p->aTotalSize[i]!=aTotalSize[i] ) rc = FTS5_CORRUPT;
226077    }
226078  }
226079
226080  /* Check that the %_docsize and %_content tables contain the expected
226081  ** number of rows.  */
226082  if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
226083    i64 nRow = 0;
226084    rc = fts5StorageCount(p, "content", &nRow);
226085    if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
226086  }
226087  if( rc==SQLITE_OK && pConfig->bColumnsize ){
226088    i64 nRow = 0;
226089    rc = fts5StorageCount(p, "docsize", &nRow);
226090    if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
226091  }
226092
226093  /* Pass the expected checksum down to the FTS index module. It will
226094  ** verify, amongst other things, that it matches the checksum generated by
226095  ** inspecting the index itself.  */
226096  if( rc==SQLITE_OK ){
226097    rc = sqlite3Fts5IndexIntegrityCheck(p->pIndex, ctx.cksum);
226098  }
226099
226100  sqlite3_free(aTotalSize);
226101  return rc;
226102}
226103
226104/*
226105** Obtain an SQLite statement handle that may be used to read data from the
226106** %_content table.
226107*/
226108static int sqlite3Fts5StorageStmt(
226109  Fts5Storage *p,
226110  int eStmt,
226111  sqlite3_stmt **pp,
226112  char **pzErrMsg
226113){
226114  int rc;
226115  assert( eStmt==FTS5_STMT_SCAN_ASC
226116       || eStmt==FTS5_STMT_SCAN_DESC
226117       || eStmt==FTS5_STMT_LOOKUP
226118  );
226119  rc = fts5StorageGetStmt(p, eStmt, pp, pzErrMsg);
226120  if( rc==SQLITE_OK ){
226121    assert( p->aStmt[eStmt]==*pp );
226122    p->aStmt[eStmt] = 0;
226123  }
226124  return rc;
226125}
226126
226127/*
226128** Release an SQLite statement handle obtained via an earlier call to
226129** sqlite3Fts5StorageStmt(). The eStmt parameter passed to this function
226130** must match that passed to the sqlite3Fts5StorageStmt() call.
226131*/
226132static void sqlite3Fts5StorageStmtRelease(
226133  Fts5Storage *p,
226134  int eStmt,
226135  sqlite3_stmt *pStmt
226136){
226137  assert( eStmt==FTS5_STMT_SCAN_ASC
226138       || eStmt==FTS5_STMT_SCAN_DESC
226139       || eStmt==FTS5_STMT_LOOKUP
226140  );
226141  if( p->aStmt[eStmt]==0 ){
226142    sqlite3_reset(pStmt);
226143    p->aStmt[eStmt] = pStmt;
226144  }else{
226145    sqlite3_finalize(pStmt);
226146  }
226147}
226148
226149static int fts5StorageDecodeSizeArray(
226150  int *aCol, int nCol,            /* Array to populate */
226151  const u8 *aBlob, int nBlob      /* Record to read varints from */
226152){
226153  int i;
226154  int iOff = 0;
226155  for(i=0; i<nCol; i++){
226156    if( iOff>=nBlob ) return 1;
226157    iOff += fts5GetVarint32(&aBlob[iOff], aCol[i]);
226158  }
226159  return (iOff!=nBlob);
226160}
226161
226162/*
226163** Argument aCol points to an array of integers containing one entry for
226164** each table column. This function reads the %_docsize record for the
226165** specified rowid and populates aCol[] with the results.
226166**
226167** An SQLite error code is returned if an error occurs, or SQLITE_OK
226168** otherwise.
226169*/
226170static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol){
226171  int nCol = p->pConfig->nCol;    /* Number of user columns in table */
226172  sqlite3_stmt *pLookup = 0;      /* Statement to query %_docsize */
226173  int rc;                         /* Return Code */
226174
226175  assert( p->pConfig->bColumnsize );
226176  rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE, &pLookup, 0);
226177  if( rc==SQLITE_OK ){
226178    int bCorrupt = 1;
226179    sqlite3_bind_int64(pLookup, 1, iRowid);
226180    if( SQLITE_ROW==sqlite3_step(pLookup) ){
226181      const u8 *aBlob = sqlite3_column_blob(pLookup, 0);
226182      int nBlob = sqlite3_column_bytes(pLookup, 0);
226183      if( 0==fts5StorageDecodeSizeArray(aCol, nCol, aBlob, nBlob) ){
226184        bCorrupt = 0;
226185      }
226186    }
226187    rc = sqlite3_reset(pLookup);
226188    if( bCorrupt && rc==SQLITE_OK ){
226189      rc = FTS5_CORRUPT;
226190    }
226191  }
226192
226193  return rc;
226194}
226195
226196static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnToken){
226197  int rc = fts5StorageLoadTotals(p, 0);
226198  if( rc==SQLITE_OK ){
226199    *pnToken = 0;
226200    if( iCol<0 ){
226201      int i;
226202      for(i=0; i<p->pConfig->nCol; i++){
226203        *pnToken += p->aTotalSize[i];
226204      }
226205    }else if( iCol<p->pConfig->nCol ){
226206      *pnToken = p->aTotalSize[iCol];
226207    }else{
226208      rc = SQLITE_RANGE;
226209    }
226210  }
226211  return rc;
226212}
226213
226214static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow){
226215  int rc = fts5StorageLoadTotals(p, 0);
226216  if( rc==SQLITE_OK ){
226217    /* nTotalRow being zero does not necessarily indicate a corrupt
226218    ** database - it might be that the FTS5 table really does contain zero
226219    ** rows. However this function is only called from the xRowCount() API,
226220    ** and there is no way for that API to be invoked if the table contains
226221    ** no rows. Hence the FTS5_CORRUPT return.  */
226222    *pnRow = p->nTotalRow;
226223    if( p->nTotalRow<=0 ) rc = FTS5_CORRUPT;
226224  }
226225  return rc;
226226}
226227
226228/*
226229** Flush any data currently held in-memory to disk.
226230*/
226231static int sqlite3Fts5StorageSync(Fts5Storage *p){
226232  int rc = SQLITE_OK;
226233  i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db);
226234  if( p->bTotalsValid ){
226235    rc = fts5StorageSaveTotals(p);
226236    p->bTotalsValid = 0;
226237  }
226238  if( rc==SQLITE_OK ){
226239    rc = sqlite3Fts5IndexSync(p->pIndex);
226240  }
226241  sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid);
226242  return rc;
226243}
226244
226245static int sqlite3Fts5StorageRollback(Fts5Storage *p){
226246  p->bTotalsValid = 0;
226247  return sqlite3Fts5IndexRollback(p->pIndex);
226248}
226249
226250static int sqlite3Fts5StorageConfigValue(
226251  Fts5Storage *p,
226252  const char *z,
226253  sqlite3_value *pVal,
226254  int iVal
226255){
226256  sqlite3_stmt *pReplace = 0;
226257  int rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_CONFIG, &pReplace, 0);
226258  if( rc==SQLITE_OK ){
226259    sqlite3_bind_text(pReplace, 1, z, -1, SQLITE_STATIC);
226260    if( pVal ){
226261      sqlite3_bind_value(pReplace, 2, pVal);
226262    }else{
226263      sqlite3_bind_int(pReplace, 2, iVal);
226264    }
226265    sqlite3_step(pReplace);
226266    rc = sqlite3_reset(pReplace);
226267    sqlite3_bind_null(pReplace, 1);
226268  }
226269  if( rc==SQLITE_OK && pVal ){
226270    int iNew = p->pConfig->iCookie + 1;
226271    rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
226272    if( rc==SQLITE_OK ){
226273      p->pConfig->iCookie = iNew;
226274    }
226275  }
226276  return rc;
226277}
226278
226279/*
226280** 2014 May 31
226281**
226282** The author disclaims copyright to this source code.  In place of
226283** a legal notice, here is a blessing:
226284**
226285**    May you do good and not evil.
226286**    May you find forgiveness for yourself and forgive others.
226287**    May you share freely, never taking more than you give.
226288**
226289******************************************************************************
226290*/
226291
226292
226293/* #include "fts5Int.h" */
226294
226295/**************************************************************************
226296** Start of ascii tokenizer implementation.
226297*/
226298
226299/*
226300** For tokenizers with no "unicode" modifier, the set of token characters
226301** is the same as the set of ASCII range alphanumeric characters.
226302*/
226303static unsigned char aAsciiTokenChar[128] = {
226304  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,   /* 0x00..0x0F */
226305  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,   /* 0x10..0x1F */
226306  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,   /* 0x20..0x2F */
226307  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 0, 0, 0, 0, 0, 0,   /* 0x30..0x3F */
226308  0, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,   /* 0x40..0x4F */
226309  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 0, 0, 0, 0, 0,   /* 0x50..0x5F */
226310  0, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,   /* 0x60..0x6F */
226311  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 0, 0, 0, 0, 0,   /* 0x70..0x7F */
226312};
226313
226314typedef struct AsciiTokenizer AsciiTokenizer;
226315struct AsciiTokenizer {
226316  unsigned char aTokenChar[128];
226317};
226318
226319static void fts5AsciiAddExceptions(
226320  AsciiTokenizer *p,
226321  const char *zArg,
226322  int bTokenChars
226323){
226324  int i;
226325  for(i=0; zArg[i]; i++){
226326    if( (zArg[i] & 0x80)==0 ){
226327      p->aTokenChar[(int)zArg[i]] = (unsigned char)bTokenChars;
226328    }
226329  }
226330}
226331
226332/*
226333** Delete a "ascii" tokenizer.
226334*/
226335static void fts5AsciiDelete(Fts5Tokenizer *p){
226336  sqlite3_free(p);
226337}
226338
226339/*
226340** Create an "ascii" tokenizer.
226341*/
226342static int fts5AsciiCreate(
226343  void *pUnused,
226344  const char **azArg, int nArg,
226345  Fts5Tokenizer **ppOut
226346){
226347  int rc = SQLITE_OK;
226348  AsciiTokenizer *p = 0;
226349  UNUSED_PARAM(pUnused);
226350  if( nArg%2 ){
226351    rc = SQLITE_ERROR;
226352  }else{
226353    p = sqlite3_malloc(sizeof(AsciiTokenizer));
226354    if( p==0 ){
226355      rc = SQLITE_NOMEM;
226356    }else{
226357      int i;
226358      memset(p, 0, sizeof(AsciiTokenizer));
226359      memcpy(p->aTokenChar, aAsciiTokenChar, sizeof(aAsciiTokenChar));
226360      for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
226361        const char *zArg = azArg[i+1];
226362        if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
226363          fts5AsciiAddExceptions(p, zArg, 1);
226364        }else
226365        if( 0==sqlite3_stricmp(azArg[i], "separators") ){
226366          fts5AsciiAddExceptions(p, zArg, 0);
226367        }else{
226368          rc = SQLITE_ERROR;
226369        }
226370      }
226371      if( rc!=SQLITE_OK ){
226372        fts5AsciiDelete((Fts5Tokenizer*)p);
226373        p = 0;
226374      }
226375    }
226376  }
226377
226378  *ppOut = (Fts5Tokenizer*)p;
226379  return rc;
226380}
226381
226382
226383static void asciiFold(char *aOut, const char *aIn, int nByte){
226384  int i;
226385  for(i=0; i<nByte; i++){
226386    char c = aIn[i];
226387    if( c>='A' && c<='Z' ) c += 32;
226388    aOut[i] = c;
226389  }
226390}
226391
226392/*
226393** Tokenize some text using the ascii tokenizer.
226394*/
226395static int fts5AsciiTokenize(
226396  Fts5Tokenizer *pTokenizer,
226397  void *pCtx,
226398  int iUnused,
226399  const char *pText, int nText,
226400  int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
226401){
226402  AsciiTokenizer *p = (AsciiTokenizer*)pTokenizer;
226403  int rc = SQLITE_OK;
226404  int ie;
226405  int is = 0;
226406
226407  char aFold[64];
226408  int nFold = sizeof(aFold);
226409  char *pFold = aFold;
226410  unsigned char *a = p->aTokenChar;
226411
226412  UNUSED_PARAM(iUnused);
226413
226414  while( is<nText && rc==SQLITE_OK ){
226415    int nByte;
226416
226417    /* Skip any leading divider characters. */
226418    while( is<nText && ((pText[is]&0x80)==0 && a[(int)pText[is]]==0) ){
226419      is++;
226420    }
226421    if( is==nText ) break;
226422
226423    /* Count the token characters */
226424    ie = is+1;
226425    while( ie<nText && ((pText[ie]&0x80) || a[(int)pText[ie]] ) ){
226426      ie++;
226427    }
226428
226429    /* Fold to lower case */
226430    nByte = ie-is;
226431    if( nByte>nFold ){
226432      if( pFold!=aFold ) sqlite3_free(pFold);
226433      pFold = sqlite3_malloc64((sqlite3_int64)nByte*2);
226434      if( pFold==0 ){
226435        rc = SQLITE_NOMEM;
226436        break;
226437      }
226438      nFold = nByte*2;
226439    }
226440    asciiFold(pFold, &pText[is], nByte);
226441
226442    /* Invoke the token callback */
226443    rc = xToken(pCtx, 0, pFold, nByte, is, ie);
226444    is = ie+1;
226445  }
226446
226447  if( pFold!=aFold ) sqlite3_free(pFold);
226448  if( rc==SQLITE_DONE ) rc = SQLITE_OK;
226449  return rc;
226450}
226451
226452/**************************************************************************
226453** Start of unicode61 tokenizer implementation.
226454*/
226455
226456
226457/*
226458** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
226459** from the sqlite3 source file utf.c. If this file is compiled as part
226460** of the amalgamation, they are not required.
226461*/
226462#ifndef SQLITE_AMALGAMATION
226463
226464static const unsigned char sqlite3Utf8Trans1[] = {
226465  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
226466  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
226467  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
226468  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
226469  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
226470  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
226471  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
226472  0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
226473};
226474
226475#define READ_UTF8(zIn, zTerm, c)                           \
226476  c = *(zIn++);                                            \
226477  if( c>=0xc0 ){                                           \
226478    c = sqlite3Utf8Trans1[c-0xc0];                         \
226479    while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
226480      c = (c<<6) + (0x3f & *(zIn++));                      \
226481    }                                                      \
226482    if( c<0x80                                             \
226483        || (c&0xFFFFF800)==0xD800                          \
226484        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
226485  }
226486
226487
226488#define WRITE_UTF8(zOut, c) {                          \
226489  if( c<0x00080 ){                                     \
226490    *zOut++ = (unsigned char)(c&0xFF);                 \
226491  }                                                    \
226492  else if( c<0x00800 ){                                \
226493    *zOut++ = 0xC0 + (unsigned char)((c>>6)&0x1F);     \
226494    *zOut++ = 0x80 + (unsigned char)(c & 0x3F);        \
226495  }                                                    \
226496  else if( c<0x10000 ){                                \
226497    *zOut++ = 0xE0 + (unsigned char)((c>>12)&0x0F);    \
226498    *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F);   \
226499    *zOut++ = 0x80 + (unsigned char)(c & 0x3F);        \
226500  }else{                                               \
226501    *zOut++ = 0xF0 + (unsigned char)((c>>18) & 0x07);  \
226502    *zOut++ = 0x80 + (unsigned char)((c>>12) & 0x3F);  \
226503    *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F);   \
226504    *zOut++ = 0x80 + (unsigned char)(c & 0x3F);        \
226505  }                                                    \
226506}
226507
226508#endif /* ifndef SQLITE_AMALGAMATION */
226509
226510typedef struct Unicode61Tokenizer Unicode61Tokenizer;
226511struct Unicode61Tokenizer {
226512  unsigned char aTokenChar[128];  /* ASCII range token characters */
226513  char *aFold;                    /* Buffer to fold text into */
226514  int nFold;                      /* Size of aFold[] in bytes */
226515  int eRemoveDiacritic;           /* True if remove_diacritics=1 is set */
226516  int nException;
226517  int *aiException;
226518
226519  unsigned char aCategory[32];    /* True for token char categories */
226520};
226521
226522/* Values for eRemoveDiacritic (must match internals of fts5_unicode2.c) */
226523#define FTS5_REMOVE_DIACRITICS_NONE    0
226524#define FTS5_REMOVE_DIACRITICS_SIMPLE  1
226525#define FTS5_REMOVE_DIACRITICS_COMPLEX 2
226526
226527static int fts5UnicodeAddExceptions(
226528  Unicode61Tokenizer *p,          /* Tokenizer object */
226529  const char *z,                  /* Characters to treat as exceptions */
226530  int bTokenChars                 /* 1 for 'tokenchars', 0 for 'separators' */
226531){
226532  int rc = SQLITE_OK;
226533  int n = (int)strlen(z);
226534  int *aNew;
226535
226536  if( n>0 ){
226537    aNew = (int*)sqlite3_realloc64(p->aiException,
226538                                   (n+p->nException)*sizeof(int));
226539    if( aNew ){
226540      int nNew = p->nException;
226541      const unsigned char *zCsr = (const unsigned char*)z;
226542      const unsigned char *zTerm = (const unsigned char*)&z[n];
226543      while( zCsr<zTerm ){
226544        u32 iCode;
226545        int bToken;
226546        READ_UTF8(zCsr, zTerm, iCode);
226547        if( iCode<128 ){
226548          p->aTokenChar[iCode] = (unsigned char)bTokenChars;
226549        }else{
226550          bToken = p->aCategory[sqlite3Fts5UnicodeCategory(iCode)];
226551          assert( (bToken==0 || bToken==1) );
226552          assert( (bTokenChars==0 || bTokenChars==1) );
226553          if( bToken!=bTokenChars && sqlite3Fts5UnicodeIsdiacritic(iCode)==0 ){
226554            int i;
226555            for(i=0; i<nNew; i++){
226556              if( (u32)aNew[i]>iCode ) break;
226557            }
226558            memmove(&aNew[i+1], &aNew[i], (nNew-i)*sizeof(int));
226559            aNew[i] = iCode;
226560            nNew++;
226561          }
226562        }
226563      }
226564      p->aiException = aNew;
226565      p->nException = nNew;
226566    }else{
226567      rc = SQLITE_NOMEM;
226568    }
226569  }
226570
226571  return rc;
226572}
226573
226574/*
226575** Return true if the p->aiException[] array contains the value iCode.
226576*/
226577static int fts5UnicodeIsException(Unicode61Tokenizer *p, int iCode){
226578  if( p->nException>0 ){
226579    int *a = p->aiException;
226580    int iLo = 0;
226581    int iHi = p->nException-1;
226582
226583    while( iHi>=iLo ){
226584      int iTest = (iHi + iLo) / 2;
226585      if( iCode==a[iTest] ){
226586        return 1;
226587      }else if( iCode>a[iTest] ){
226588        iLo = iTest+1;
226589      }else{
226590        iHi = iTest-1;
226591      }
226592    }
226593  }
226594
226595  return 0;
226596}
226597
226598/*
226599** Delete a "unicode61" tokenizer.
226600*/
226601static void fts5UnicodeDelete(Fts5Tokenizer *pTok){
226602  if( pTok ){
226603    Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTok;
226604    sqlite3_free(p->aiException);
226605    sqlite3_free(p->aFold);
226606    sqlite3_free(p);
226607  }
226608  return;
226609}
226610
226611static int unicodeSetCategories(Unicode61Tokenizer *p, const char *zCat){
226612  const char *z = zCat;
226613
226614  while( *z ){
226615    while( *z==' ' || *z=='\t' ) z++;
226616    if( *z && sqlite3Fts5UnicodeCatParse(z, p->aCategory) ){
226617      return SQLITE_ERROR;
226618    }
226619    while( *z!=' ' && *z!='\t' && *z!='\0' ) z++;
226620  }
226621
226622  sqlite3Fts5UnicodeAscii(p->aCategory, p->aTokenChar);
226623  return SQLITE_OK;
226624}
226625
226626/*
226627** Create a "unicode61" tokenizer.
226628*/
226629static int fts5UnicodeCreate(
226630  void *pUnused,
226631  const char **azArg, int nArg,
226632  Fts5Tokenizer **ppOut
226633){
226634  int rc = SQLITE_OK;             /* Return code */
226635  Unicode61Tokenizer *p = 0;      /* New tokenizer object */
226636
226637  UNUSED_PARAM(pUnused);
226638
226639  if( nArg%2 ){
226640    rc = SQLITE_ERROR;
226641  }else{
226642    p = (Unicode61Tokenizer*)sqlite3_malloc(sizeof(Unicode61Tokenizer));
226643    if( p ){
226644      const char *zCat = "L* N* Co";
226645      int i;
226646      memset(p, 0, sizeof(Unicode61Tokenizer));
226647
226648      p->eRemoveDiacritic = FTS5_REMOVE_DIACRITICS_SIMPLE;
226649      p->nFold = 64;
226650      p->aFold = sqlite3_malloc64(p->nFold * sizeof(char));
226651      if( p->aFold==0 ){
226652        rc = SQLITE_NOMEM;
226653      }
226654
226655      /* Search for a "categories" argument */
226656      for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
226657        if( 0==sqlite3_stricmp(azArg[i], "categories") ){
226658          zCat = azArg[i+1];
226659        }
226660      }
226661
226662      if( rc==SQLITE_OK ){
226663        rc = unicodeSetCategories(p, zCat);
226664      }
226665
226666      for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
226667        const char *zArg = azArg[i+1];
226668        if( 0==sqlite3_stricmp(azArg[i], "remove_diacritics") ){
226669          if( (zArg[0]!='0' && zArg[0]!='1' && zArg[0]!='2') || zArg[1] ){
226670            rc = SQLITE_ERROR;
226671          }else{
226672            p->eRemoveDiacritic = (zArg[0] - '0');
226673            assert( p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_NONE
226674                 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_SIMPLE
226675                 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_COMPLEX
226676            );
226677          }
226678        }else
226679        if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
226680          rc = fts5UnicodeAddExceptions(p, zArg, 1);
226681        }else
226682        if( 0==sqlite3_stricmp(azArg[i], "separators") ){
226683          rc = fts5UnicodeAddExceptions(p, zArg, 0);
226684        }else
226685        if( 0==sqlite3_stricmp(azArg[i], "categories") ){
226686          /* no-op */
226687        }else{
226688          rc = SQLITE_ERROR;
226689        }
226690      }
226691
226692    }else{
226693      rc = SQLITE_NOMEM;
226694    }
226695    if( rc!=SQLITE_OK ){
226696      fts5UnicodeDelete((Fts5Tokenizer*)p);
226697      p = 0;
226698    }
226699    *ppOut = (Fts5Tokenizer*)p;
226700  }
226701  return rc;
226702}
226703
226704/*
226705** Return true if, for the purposes of tokenizing with the tokenizer
226706** passed as the first argument, codepoint iCode is considered a token
226707** character (not a separator).
226708*/
226709static int fts5UnicodeIsAlnum(Unicode61Tokenizer *p, int iCode){
226710  return (
226711    p->aCategory[sqlite3Fts5UnicodeCategory((u32)iCode)]
226712    ^ fts5UnicodeIsException(p, iCode)
226713  );
226714}
226715
226716static int fts5UnicodeTokenize(
226717  Fts5Tokenizer *pTokenizer,
226718  void *pCtx,
226719  int iUnused,
226720  const char *pText, int nText,
226721  int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
226722){
226723  Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTokenizer;
226724  int rc = SQLITE_OK;
226725  unsigned char *a = p->aTokenChar;
226726
226727  unsigned char *zTerm = (unsigned char*)&pText[nText];
226728  unsigned char *zCsr = (unsigned char *)pText;
226729
226730  /* Output buffer */
226731  char *aFold = p->aFold;
226732  int nFold = p->nFold;
226733  const char *pEnd = &aFold[nFold-6];
226734
226735  UNUSED_PARAM(iUnused);
226736
226737  /* Each iteration of this loop gobbles up a contiguous run of separators,
226738  ** then the next token.  */
226739  while( rc==SQLITE_OK ){
226740    u32 iCode;                    /* non-ASCII codepoint read from input */
226741    char *zOut = aFold;
226742    int is;
226743    int ie;
226744
226745    /* Skip any separator characters. */
226746    while( 1 ){
226747      if( zCsr>=zTerm ) goto tokenize_done;
226748      if( *zCsr & 0x80 ) {
226749        /* A character outside of the ascii range. Skip past it if it is
226750        ** a separator character. Or break out of the loop if it is not. */
226751        is = zCsr - (unsigned char*)pText;
226752        READ_UTF8(zCsr, zTerm, iCode);
226753        if( fts5UnicodeIsAlnum(p, iCode) ){
226754          goto non_ascii_tokenchar;
226755        }
226756      }else{
226757        if( a[*zCsr] ){
226758          is = zCsr - (unsigned char*)pText;
226759          goto ascii_tokenchar;
226760        }
226761        zCsr++;
226762      }
226763    }
226764
226765    /* Run through the tokenchars. Fold them into the output buffer along
226766    ** the way.  */
226767    while( zCsr<zTerm ){
226768
226769      /* Grow the output buffer so that there is sufficient space to fit the
226770      ** largest possible utf-8 character.  */
226771      if( zOut>pEnd ){
226772        aFold = sqlite3_malloc64((sqlite3_int64)nFold*2);
226773        if( aFold==0 ){
226774          rc = SQLITE_NOMEM;
226775          goto tokenize_done;
226776        }
226777        zOut = &aFold[zOut - p->aFold];
226778        memcpy(aFold, p->aFold, nFold);
226779        sqlite3_free(p->aFold);
226780        p->aFold = aFold;
226781        p->nFold = nFold = nFold*2;
226782        pEnd = &aFold[nFold-6];
226783      }
226784
226785      if( *zCsr & 0x80 ){
226786        /* An non-ascii-range character. Fold it into the output buffer if
226787        ** it is a token character, or break out of the loop if it is not. */
226788        READ_UTF8(zCsr, zTerm, iCode);
226789        if( fts5UnicodeIsAlnum(p,iCode)||sqlite3Fts5UnicodeIsdiacritic(iCode) ){
226790 non_ascii_tokenchar:
226791          iCode = sqlite3Fts5UnicodeFold(iCode, p->eRemoveDiacritic);
226792          if( iCode ) WRITE_UTF8(zOut, iCode);
226793        }else{
226794          break;
226795        }
226796      }else if( a[*zCsr]==0 ){
226797        /* An ascii-range separator character. End of token. */
226798        break;
226799      }else{
226800 ascii_tokenchar:
226801        if( *zCsr>='A' && *zCsr<='Z' ){
226802          *zOut++ = *zCsr + 32;
226803        }else{
226804          *zOut++ = *zCsr;
226805        }
226806        zCsr++;
226807      }
226808      ie = zCsr - (unsigned char*)pText;
226809    }
226810
226811    /* Invoke the token callback */
226812    rc = xToken(pCtx, 0, aFold, zOut-aFold, is, ie);
226813  }
226814
226815 tokenize_done:
226816  if( rc==SQLITE_DONE ) rc = SQLITE_OK;
226817  return rc;
226818}
226819
226820/**************************************************************************
226821** Start of porter stemmer implementation.
226822*/
226823
226824/* Any tokens larger than this (in bytes) are passed through without
226825** stemming. */
226826#define FTS5_PORTER_MAX_TOKEN 64
226827
226828typedef struct PorterTokenizer PorterTokenizer;
226829struct PorterTokenizer {
226830  fts5_tokenizer tokenizer;       /* Parent tokenizer module */
226831  Fts5Tokenizer *pTokenizer;      /* Parent tokenizer instance */
226832  char aBuf[FTS5_PORTER_MAX_TOKEN + 64];
226833};
226834
226835/*
226836** Delete a "porter" tokenizer.
226837*/
226838static void fts5PorterDelete(Fts5Tokenizer *pTok){
226839  if( pTok ){
226840    PorterTokenizer *p = (PorterTokenizer*)pTok;
226841    if( p->pTokenizer ){
226842      p->tokenizer.xDelete(p->pTokenizer);
226843    }
226844    sqlite3_free(p);
226845  }
226846}
226847
226848/*
226849** Create a "porter" tokenizer.
226850*/
226851static int fts5PorterCreate(
226852  void *pCtx,
226853  const char **azArg, int nArg,
226854  Fts5Tokenizer **ppOut
226855){
226856  fts5_api *pApi = (fts5_api*)pCtx;
226857  int rc = SQLITE_OK;
226858  PorterTokenizer *pRet;
226859  void *pUserdata = 0;
226860  const char *zBase = "unicode61";
226861
226862  if( nArg>0 ){
226863    zBase = azArg[0];
226864  }
226865
226866  pRet = (PorterTokenizer*)sqlite3_malloc(sizeof(PorterTokenizer));
226867  if( pRet ){
226868    memset(pRet, 0, sizeof(PorterTokenizer));
226869    rc = pApi->xFindTokenizer(pApi, zBase, &pUserdata, &pRet->tokenizer);
226870  }else{
226871    rc = SQLITE_NOMEM;
226872  }
226873  if( rc==SQLITE_OK ){
226874    int nArg2 = (nArg>0 ? nArg-1 : 0);
226875    const char **azArg2 = (nArg2 ? &azArg[1] : 0);
226876    rc = pRet->tokenizer.xCreate(pUserdata, azArg2, nArg2, &pRet->pTokenizer);
226877  }
226878
226879  if( rc!=SQLITE_OK ){
226880    fts5PorterDelete((Fts5Tokenizer*)pRet);
226881    pRet = 0;
226882  }
226883  *ppOut = (Fts5Tokenizer*)pRet;
226884  return rc;
226885}
226886
226887typedef struct PorterContext PorterContext;
226888struct PorterContext {
226889  void *pCtx;
226890  int (*xToken)(void*, int, const char*, int, int, int);
226891  char *aBuf;
226892};
226893
226894typedef struct PorterRule PorterRule;
226895struct PorterRule {
226896  const char *zSuffix;
226897  int nSuffix;
226898  int (*xCond)(char *zStem, int nStem);
226899  const char *zOutput;
226900  int nOutput;
226901};
226902
226903#if 0
226904static int fts5PorterApply(char *aBuf, int *pnBuf, PorterRule *aRule){
226905  int ret = -1;
226906  int nBuf = *pnBuf;
226907  PorterRule *p;
226908
226909  for(p=aRule; p->zSuffix; p++){
226910    assert( strlen(p->zSuffix)==p->nSuffix );
226911    assert( strlen(p->zOutput)==p->nOutput );
226912    if( nBuf<p->nSuffix ) continue;
226913    if( 0==memcmp(&aBuf[nBuf - p->nSuffix], p->zSuffix, p->nSuffix) ) break;
226914  }
226915
226916  if( p->zSuffix ){
226917    int nStem = nBuf - p->nSuffix;
226918    if( p->xCond==0 || p->xCond(aBuf, nStem) ){
226919      memcpy(&aBuf[nStem], p->zOutput, p->nOutput);
226920      *pnBuf = nStem + p->nOutput;
226921      ret = p - aRule;
226922    }
226923  }
226924
226925  return ret;
226926}
226927#endif
226928
226929static int fts5PorterIsVowel(char c, int bYIsVowel){
226930  return (
226931      c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || (bYIsVowel && c=='y')
226932  );
226933}
226934
226935static int fts5PorterGobbleVC(char *zStem, int nStem, int bPrevCons){
226936  int i;
226937  int bCons = bPrevCons;
226938
226939  /* Scan for a vowel */
226940  for(i=0; i<nStem; i++){
226941    if( 0==(bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) break;
226942  }
226943
226944  /* Scan for a consonent */
226945  for(i++; i<nStem; i++){
226946    if( (bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) return i+1;
226947  }
226948  return 0;
226949}
226950
226951/* porter rule condition: (m > 0) */
226952static int fts5Porter_MGt0(char *zStem, int nStem){
226953  return !!fts5PorterGobbleVC(zStem, nStem, 0);
226954}
226955
226956/* porter rule condition: (m > 1) */
226957static int fts5Porter_MGt1(char *zStem, int nStem){
226958  int n;
226959  n = fts5PorterGobbleVC(zStem, nStem, 0);
226960  if( n && fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
226961    return 1;
226962  }
226963  return 0;
226964}
226965
226966/* porter rule condition: (m = 1) */
226967static int fts5Porter_MEq1(char *zStem, int nStem){
226968  int n;
226969  n = fts5PorterGobbleVC(zStem, nStem, 0);
226970  if( n && 0==fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
226971    return 1;
226972  }
226973  return 0;
226974}
226975
226976/* porter rule condition: (*o) */
226977static int fts5Porter_Ostar(char *zStem, int nStem){
226978  if( zStem[nStem-1]=='w' || zStem[nStem-1]=='x' || zStem[nStem-1]=='y' ){
226979    return 0;
226980  }else{
226981    int i;
226982    int mask = 0;
226983    int bCons = 0;
226984    for(i=0; i<nStem; i++){
226985      bCons = !fts5PorterIsVowel(zStem[i], bCons);
226986      assert( bCons==0 || bCons==1 );
226987      mask = (mask << 1) + bCons;
226988    }
226989    return ((mask & 0x0007)==0x0005);
226990  }
226991}
226992
226993/* porter rule condition: (m > 1 and (*S or *T)) */
226994static int fts5Porter_MGt1_and_S_or_T(char *zStem, int nStem){
226995  assert( nStem>0 );
226996  return (zStem[nStem-1]=='s' || zStem[nStem-1]=='t')
226997      && fts5Porter_MGt1(zStem, nStem);
226998}
226999
227000/* porter rule condition: (*v*) */
227001static int fts5Porter_Vowel(char *zStem, int nStem){
227002  int i;
227003  for(i=0; i<nStem; i++){
227004    if( fts5PorterIsVowel(zStem[i], i>0) ){
227005      return 1;
227006    }
227007  }
227008  return 0;
227009}
227010
227011
227012/**************************************************************************
227013***************************************************************************
227014** GENERATED CODE STARTS HERE (mkportersteps.tcl)
227015*/
227016
227017static int fts5PorterStep4(char *aBuf, int *pnBuf){
227018  int ret = 0;
227019  int nBuf = *pnBuf;
227020  switch( aBuf[nBuf-2] ){
227021
227022    case 'a':
227023      if( nBuf>2 && 0==memcmp("al", &aBuf[nBuf-2], 2) ){
227024        if( fts5Porter_MGt1(aBuf, nBuf-2) ){
227025          *pnBuf = nBuf - 2;
227026        }
227027      }
227028      break;
227029
227030    case 'c':
227031      if( nBuf>4 && 0==memcmp("ance", &aBuf[nBuf-4], 4) ){
227032        if( fts5Porter_MGt1(aBuf, nBuf-4) ){
227033          *pnBuf = nBuf - 4;
227034        }
227035      }else if( nBuf>4 && 0==memcmp("ence", &aBuf[nBuf-4], 4) ){
227036        if( fts5Porter_MGt1(aBuf, nBuf-4) ){
227037          *pnBuf = nBuf - 4;
227038        }
227039      }
227040      break;
227041
227042    case 'e':
227043      if( nBuf>2 && 0==memcmp("er", &aBuf[nBuf-2], 2) ){
227044        if( fts5Porter_MGt1(aBuf, nBuf-2) ){
227045          *pnBuf = nBuf - 2;
227046        }
227047      }
227048      break;
227049
227050    case 'i':
227051      if( nBuf>2 && 0==memcmp("ic", &aBuf[nBuf-2], 2) ){
227052        if( fts5Porter_MGt1(aBuf, nBuf-2) ){
227053          *pnBuf = nBuf - 2;
227054        }
227055      }
227056      break;
227057
227058    case 'l':
227059      if( nBuf>4 && 0==memcmp("able", &aBuf[nBuf-4], 4) ){
227060        if( fts5Porter_MGt1(aBuf, nBuf-4) ){
227061          *pnBuf = nBuf - 4;
227062        }
227063      }else if( nBuf>4 && 0==memcmp("ible", &aBuf[nBuf-4], 4) ){
227064        if( fts5Porter_MGt1(aBuf, nBuf-4) ){
227065          *pnBuf = nBuf - 4;
227066        }
227067      }
227068      break;
227069
227070    case 'n':
227071      if( nBuf>3 && 0==memcmp("ant", &aBuf[nBuf-3], 3) ){
227072        if( fts5Porter_MGt1(aBuf, nBuf-3) ){
227073          *pnBuf = nBuf - 3;
227074        }
227075      }else if( nBuf>5 && 0==memcmp("ement", &aBuf[nBuf-5], 5) ){
227076        if( fts5Porter_MGt1(aBuf, nBuf-5) ){
227077          *pnBuf = nBuf - 5;
227078        }
227079      }else if( nBuf>4 && 0==memcmp("ment", &aBuf[nBuf-4], 4) ){
227080        if( fts5Porter_MGt1(aBuf, nBuf-4) ){
227081          *pnBuf = nBuf - 4;
227082        }
227083      }else if( nBuf>3 && 0==memcmp("ent", &aBuf[nBuf-3], 3) ){
227084        if( fts5Porter_MGt1(aBuf, nBuf-3) ){
227085          *pnBuf = nBuf - 3;
227086        }
227087      }
227088      break;
227089
227090    case 'o':
227091      if( nBuf>3 && 0==memcmp("ion", &aBuf[nBuf-3], 3) ){
227092        if( fts5Porter_MGt1_and_S_or_T(aBuf, nBuf-3) ){
227093          *pnBuf = nBuf - 3;
227094        }
227095      }else if( nBuf>2 && 0==memcmp("ou", &aBuf[nBuf-2], 2) ){
227096        if( fts5Porter_MGt1(aBuf, nBuf-2) ){
227097          *pnBuf = nBuf - 2;
227098        }
227099      }
227100      break;
227101
227102    case 's':
227103      if( nBuf>3 && 0==memcmp("ism", &aBuf[nBuf-3], 3) ){
227104        if( fts5Porter_MGt1(aBuf, nBuf-3) ){
227105          *pnBuf = nBuf - 3;
227106        }
227107      }
227108      break;
227109
227110    case 't':
227111      if( nBuf>3 && 0==memcmp("ate", &aBuf[nBuf-3], 3) ){
227112        if( fts5Porter_MGt1(aBuf, nBuf-3) ){
227113          *pnBuf = nBuf - 3;
227114        }
227115      }else if( nBuf>3 && 0==memcmp("iti", &aBuf[nBuf-3], 3) ){
227116        if( fts5Porter_MGt1(aBuf, nBuf-3) ){
227117          *pnBuf = nBuf - 3;
227118        }
227119      }
227120      break;
227121
227122    case 'u':
227123      if( nBuf>3 && 0==memcmp("ous", &aBuf[nBuf-3], 3) ){
227124        if( fts5Porter_MGt1(aBuf, nBuf-3) ){
227125          *pnBuf = nBuf - 3;
227126        }
227127      }
227128      break;
227129
227130    case 'v':
227131      if( nBuf>3 && 0==memcmp("ive", &aBuf[nBuf-3], 3) ){
227132        if( fts5Porter_MGt1(aBuf, nBuf-3) ){
227133          *pnBuf = nBuf - 3;
227134        }
227135      }
227136      break;
227137
227138    case 'z':
227139      if( nBuf>3 && 0==memcmp("ize", &aBuf[nBuf-3], 3) ){
227140        if( fts5Porter_MGt1(aBuf, nBuf-3) ){
227141          *pnBuf = nBuf - 3;
227142        }
227143      }
227144      break;
227145
227146  }
227147  return ret;
227148}
227149
227150
227151static int fts5PorterStep1B2(char *aBuf, int *pnBuf){
227152  int ret = 0;
227153  int nBuf = *pnBuf;
227154  switch( aBuf[nBuf-2] ){
227155
227156    case 'a':
227157      if( nBuf>2 && 0==memcmp("at", &aBuf[nBuf-2], 2) ){
227158        memcpy(&aBuf[nBuf-2], "ate", 3);
227159        *pnBuf = nBuf - 2 + 3;
227160        ret = 1;
227161      }
227162      break;
227163
227164    case 'b':
227165      if( nBuf>2 && 0==memcmp("bl", &aBuf[nBuf-2], 2) ){
227166        memcpy(&aBuf[nBuf-2], "ble", 3);
227167        *pnBuf = nBuf - 2 + 3;
227168        ret = 1;
227169      }
227170      break;
227171
227172    case 'i':
227173      if( nBuf>2 && 0==memcmp("iz", &aBuf[nBuf-2], 2) ){
227174        memcpy(&aBuf[nBuf-2], "ize", 3);
227175        *pnBuf = nBuf - 2 + 3;
227176        ret = 1;
227177      }
227178      break;
227179
227180  }
227181  return ret;
227182}
227183
227184
227185static int fts5PorterStep2(char *aBuf, int *pnBuf){
227186  int ret = 0;
227187  int nBuf = *pnBuf;
227188  switch( aBuf[nBuf-2] ){
227189
227190    case 'a':
227191      if( nBuf>7 && 0==memcmp("ational", &aBuf[nBuf-7], 7) ){
227192        if( fts5Porter_MGt0(aBuf, nBuf-7) ){
227193          memcpy(&aBuf[nBuf-7], "ate", 3);
227194          *pnBuf = nBuf - 7 + 3;
227195        }
227196      }else if( nBuf>6 && 0==memcmp("tional", &aBuf[nBuf-6], 6) ){
227197        if( fts5Porter_MGt0(aBuf, nBuf-6) ){
227198          memcpy(&aBuf[nBuf-6], "tion", 4);
227199          *pnBuf = nBuf - 6 + 4;
227200        }
227201      }
227202      break;
227203
227204    case 'c':
227205      if( nBuf>4 && 0==memcmp("enci", &aBuf[nBuf-4], 4) ){
227206        if( fts5Porter_MGt0(aBuf, nBuf-4) ){
227207          memcpy(&aBuf[nBuf-4], "ence", 4);
227208          *pnBuf = nBuf - 4 + 4;
227209        }
227210      }else if( nBuf>4 && 0==memcmp("anci", &aBuf[nBuf-4], 4) ){
227211        if( fts5Porter_MGt0(aBuf, nBuf-4) ){
227212          memcpy(&aBuf[nBuf-4], "ance", 4);
227213          *pnBuf = nBuf - 4 + 4;
227214        }
227215      }
227216      break;
227217
227218    case 'e':
227219      if( nBuf>4 && 0==memcmp("izer", &aBuf[nBuf-4], 4) ){
227220        if( fts5Porter_MGt0(aBuf, nBuf-4) ){
227221          memcpy(&aBuf[nBuf-4], "ize", 3);
227222          *pnBuf = nBuf - 4 + 3;
227223        }
227224      }
227225      break;
227226
227227    case 'g':
227228      if( nBuf>4 && 0==memcmp("logi", &aBuf[nBuf-4], 4) ){
227229        if( fts5Porter_MGt0(aBuf, nBuf-4) ){
227230          memcpy(&aBuf[nBuf-4], "log", 3);
227231          *pnBuf = nBuf - 4 + 3;
227232        }
227233      }
227234      break;
227235
227236    case 'l':
227237      if( nBuf>3 && 0==memcmp("bli", &aBuf[nBuf-3], 3) ){
227238        if( fts5Porter_MGt0(aBuf, nBuf-3) ){
227239          memcpy(&aBuf[nBuf-3], "ble", 3);
227240          *pnBuf = nBuf - 3 + 3;
227241        }
227242      }else if( nBuf>4 && 0==memcmp("alli", &aBuf[nBuf-4], 4) ){
227243        if( fts5Porter_MGt0(aBuf, nBuf-4) ){
227244          memcpy(&aBuf[nBuf-4], "al", 2);
227245          *pnBuf = nBuf - 4 + 2;
227246        }
227247      }else if( nBuf>5 && 0==memcmp("entli", &aBuf[nBuf-5], 5) ){
227248        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
227249          memcpy(&aBuf[nBuf-5], "ent", 3);
227250          *pnBuf = nBuf - 5 + 3;
227251        }
227252      }else if( nBuf>3 && 0==memcmp("eli", &aBuf[nBuf-3], 3) ){
227253        if( fts5Porter_MGt0(aBuf, nBuf-3) ){
227254          memcpy(&aBuf[nBuf-3], "e", 1);
227255          *pnBuf = nBuf - 3 + 1;
227256        }
227257      }else if( nBuf>5 && 0==memcmp("ousli", &aBuf[nBuf-5], 5) ){
227258        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
227259          memcpy(&aBuf[nBuf-5], "ous", 3);
227260          *pnBuf = nBuf - 5 + 3;
227261        }
227262      }
227263      break;
227264
227265    case 'o':
227266      if( nBuf>7 && 0==memcmp("ization", &aBuf[nBuf-7], 7) ){
227267        if( fts5Porter_MGt0(aBuf, nBuf-7) ){
227268          memcpy(&aBuf[nBuf-7], "ize", 3);
227269          *pnBuf = nBuf - 7 + 3;
227270        }
227271      }else if( nBuf>5 && 0==memcmp("ation", &aBuf[nBuf-5], 5) ){
227272        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
227273          memcpy(&aBuf[nBuf-5], "ate", 3);
227274          *pnBuf = nBuf - 5 + 3;
227275        }
227276      }else if( nBuf>4 && 0==memcmp("ator", &aBuf[nBuf-4], 4) ){
227277        if( fts5Porter_MGt0(aBuf, nBuf-4) ){
227278          memcpy(&aBuf[nBuf-4], "ate", 3);
227279          *pnBuf = nBuf - 4 + 3;
227280        }
227281      }
227282      break;
227283
227284    case 's':
227285      if( nBuf>5 && 0==memcmp("alism", &aBuf[nBuf-5], 5) ){
227286        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
227287          memcpy(&aBuf[nBuf-5], "al", 2);
227288          *pnBuf = nBuf - 5 + 2;
227289        }
227290      }else if( nBuf>7 && 0==memcmp("iveness", &aBuf[nBuf-7], 7) ){
227291        if( fts5Porter_MGt0(aBuf, nBuf-7) ){
227292          memcpy(&aBuf[nBuf-7], "ive", 3);
227293          *pnBuf = nBuf - 7 + 3;
227294        }
227295      }else if( nBuf>7 && 0==memcmp("fulness", &aBuf[nBuf-7], 7) ){
227296        if( fts5Porter_MGt0(aBuf, nBuf-7) ){
227297          memcpy(&aBuf[nBuf-7], "ful", 3);
227298          *pnBuf = nBuf - 7 + 3;
227299        }
227300      }else if( nBuf>7 && 0==memcmp("ousness", &aBuf[nBuf-7], 7) ){
227301        if( fts5Porter_MGt0(aBuf, nBuf-7) ){
227302          memcpy(&aBuf[nBuf-7], "ous", 3);
227303          *pnBuf = nBuf - 7 + 3;
227304        }
227305      }
227306      break;
227307
227308    case 't':
227309      if( nBuf>5 && 0==memcmp("aliti", &aBuf[nBuf-5], 5) ){
227310        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
227311          memcpy(&aBuf[nBuf-5], "al", 2);
227312          *pnBuf = nBuf - 5 + 2;
227313        }
227314      }else if( nBuf>5 && 0==memcmp("iviti", &aBuf[nBuf-5], 5) ){
227315        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
227316          memcpy(&aBuf[nBuf-5], "ive", 3);
227317          *pnBuf = nBuf - 5 + 3;
227318        }
227319      }else if( nBuf>6 && 0==memcmp("biliti", &aBuf[nBuf-6], 6) ){
227320        if( fts5Porter_MGt0(aBuf, nBuf-6) ){
227321          memcpy(&aBuf[nBuf-6], "ble", 3);
227322          *pnBuf = nBuf - 6 + 3;
227323        }
227324      }
227325      break;
227326
227327  }
227328  return ret;
227329}
227330
227331
227332static int fts5PorterStep3(char *aBuf, int *pnBuf){
227333  int ret = 0;
227334  int nBuf = *pnBuf;
227335  switch( aBuf[nBuf-2] ){
227336
227337    case 'a':
227338      if( nBuf>4 && 0==memcmp("ical", &aBuf[nBuf-4], 4) ){
227339        if( fts5Porter_MGt0(aBuf, nBuf-4) ){
227340          memcpy(&aBuf[nBuf-4], "ic", 2);
227341          *pnBuf = nBuf - 4 + 2;
227342        }
227343      }
227344      break;
227345
227346    case 's':
227347      if( nBuf>4 && 0==memcmp("ness", &aBuf[nBuf-4], 4) ){
227348        if( fts5Porter_MGt0(aBuf, nBuf-4) ){
227349          *pnBuf = nBuf - 4;
227350        }
227351      }
227352      break;
227353
227354    case 't':
227355      if( nBuf>5 && 0==memcmp("icate", &aBuf[nBuf-5], 5) ){
227356        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
227357          memcpy(&aBuf[nBuf-5], "ic", 2);
227358          *pnBuf = nBuf - 5 + 2;
227359        }
227360      }else if( nBuf>5 && 0==memcmp("iciti", &aBuf[nBuf-5], 5) ){
227361        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
227362          memcpy(&aBuf[nBuf-5], "ic", 2);
227363          *pnBuf = nBuf - 5 + 2;
227364        }
227365      }
227366      break;
227367
227368    case 'u':
227369      if( nBuf>3 && 0==memcmp("ful", &aBuf[nBuf-3], 3) ){
227370        if( fts5Porter_MGt0(aBuf, nBuf-3) ){
227371          *pnBuf = nBuf - 3;
227372        }
227373      }
227374      break;
227375
227376    case 'v':
227377      if( nBuf>5 && 0==memcmp("ative", &aBuf[nBuf-5], 5) ){
227378        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
227379          *pnBuf = nBuf - 5;
227380        }
227381      }
227382      break;
227383
227384    case 'z':
227385      if( nBuf>5 && 0==memcmp("alize", &aBuf[nBuf-5], 5) ){
227386        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
227387          memcpy(&aBuf[nBuf-5], "al", 2);
227388          *pnBuf = nBuf - 5 + 2;
227389        }
227390      }
227391      break;
227392
227393  }
227394  return ret;
227395}
227396
227397
227398static int fts5PorterStep1B(char *aBuf, int *pnBuf){
227399  int ret = 0;
227400  int nBuf = *pnBuf;
227401  switch( aBuf[nBuf-2] ){
227402
227403    case 'e':
227404      if( nBuf>3 && 0==memcmp("eed", &aBuf[nBuf-3], 3) ){
227405        if( fts5Porter_MGt0(aBuf, nBuf-3) ){
227406          memcpy(&aBuf[nBuf-3], "ee", 2);
227407          *pnBuf = nBuf - 3 + 2;
227408        }
227409      }else if( nBuf>2 && 0==memcmp("ed", &aBuf[nBuf-2], 2) ){
227410        if( fts5Porter_Vowel(aBuf, nBuf-2) ){
227411          *pnBuf = nBuf - 2;
227412          ret = 1;
227413        }
227414      }
227415      break;
227416
227417    case 'n':
227418      if( nBuf>3 && 0==memcmp("ing", &aBuf[nBuf-3], 3) ){
227419        if( fts5Porter_Vowel(aBuf, nBuf-3) ){
227420          *pnBuf = nBuf - 3;
227421          ret = 1;
227422        }
227423      }
227424      break;
227425
227426  }
227427  return ret;
227428}
227429
227430/*
227431** GENERATED CODE ENDS HERE (mkportersteps.tcl)
227432***************************************************************************
227433**************************************************************************/
227434
227435static void fts5PorterStep1A(char *aBuf, int *pnBuf){
227436  int nBuf = *pnBuf;
227437  if( aBuf[nBuf-1]=='s' ){
227438    if( aBuf[nBuf-2]=='e' ){
227439      if( (nBuf>4 && aBuf[nBuf-4]=='s' && aBuf[nBuf-3]=='s')
227440       || (nBuf>3 && aBuf[nBuf-3]=='i' )
227441      ){
227442        *pnBuf = nBuf-2;
227443      }else{
227444        *pnBuf = nBuf-1;
227445      }
227446    }
227447    else if( aBuf[nBuf-2]!='s' ){
227448      *pnBuf = nBuf-1;
227449    }
227450  }
227451}
227452
227453static int fts5PorterCb(
227454  void *pCtx,
227455  int tflags,
227456  const char *pToken,
227457  int nToken,
227458  int iStart,
227459  int iEnd
227460){
227461  PorterContext *p = (PorterContext*)pCtx;
227462
227463  char *aBuf;
227464  int nBuf;
227465
227466  if( nToken>FTS5_PORTER_MAX_TOKEN || nToken<3 ) goto pass_through;
227467  aBuf = p->aBuf;
227468  nBuf = nToken;
227469  memcpy(aBuf, pToken, nBuf);
227470
227471  /* Step 1. */
227472  fts5PorterStep1A(aBuf, &nBuf);
227473  if( fts5PorterStep1B(aBuf, &nBuf) ){
227474    if( fts5PorterStep1B2(aBuf, &nBuf)==0 ){
227475      char c = aBuf[nBuf-1];
227476      if( fts5PorterIsVowel(c, 0)==0
227477       && c!='l' && c!='s' && c!='z' && c==aBuf[nBuf-2]
227478      ){
227479        nBuf--;
227480      }else if( fts5Porter_MEq1(aBuf, nBuf) && fts5Porter_Ostar(aBuf, nBuf) ){
227481        aBuf[nBuf++] = 'e';
227482      }
227483    }
227484  }
227485
227486  /* Step 1C. */
227487  if( aBuf[nBuf-1]=='y' && fts5Porter_Vowel(aBuf, nBuf-1) ){
227488    aBuf[nBuf-1] = 'i';
227489  }
227490
227491  /* Steps 2 through 4. */
227492  fts5PorterStep2(aBuf, &nBuf);
227493  fts5PorterStep3(aBuf, &nBuf);
227494  fts5PorterStep4(aBuf, &nBuf);
227495
227496  /* Step 5a. */
227497  assert( nBuf>0 );
227498  if( aBuf[nBuf-1]=='e' ){
227499    if( fts5Porter_MGt1(aBuf, nBuf-1)
227500     || (fts5Porter_MEq1(aBuf, nBuf-1) && !fts5Porter_Ostar(aBuf, nBuf-1))
227501    ){
227502      nBuf--;
227503    }
227504  }
227505
227506  /* Step 5b. */
227507  if( nBuf>1 && aBuf[nBuf-1]=='l'
227508   && aBuf[nBuf-2]=='l' && fts5Porter_MGt1(aBuf, nBuf-1)
227509  ){
227510    nBuf--;
227511  }
227512
227513  return p->xToken(p->pCtx, tflags, aBuf, nBuf, iStart, iEnd);
227514
227515 pass_through:
227516  return p->xToken(p->pCtx, tflags, pToken, nToken, iStart, iEnd);
227517}
227518
227519/*
227520** Tokenize using the porter tokenizer.
227521*/
227522static int fts5PorterTokenize(
227523  Fts5Tokenizer *pTokenizer,
227524  void *pCtx,
227525  int flags,
227526  const char *pText, int nText,
227527  int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
227528){
227529  PorterTokenizer *p = (PorterTokenizer*)pTokenizer;
227530  PorterContext sCtx;
227531  sCtx.xToken = xToken;
227532  sCtx.pCtx = pCtx;
227533  sCtx.aBuf = p->aBuf;
227534  return p->tokenizer.xTokenize(
227535      p->pTokenizer, (void*)&sCtx, flags, pText, nText, fts5PorterCb
227536  );
227537}
227538
227539/*
227540** Register all built-in tokenizers with FTS5.
227541*/
227542static int sqlite3Fts5TokenizerInit(fts5_api *pApi){
227543  struct BuiltinTokenizer {
227544    const char *zName;
227545    fts5_tokenizer x;
227546  } aBuiltin[] = {
227547    { "unicode61", {fts5UnicodeCreate, fts5UnicodeDelete, fts5UnicodeTokenize}},
227548    { "ascii",     {fts5AsciiCreate, fts5AsciiDelete, fts5AsciiTokenize }},
227549    { "porter",    {fts5PorterCreate, fts5PorterDelete, fts5PorterTokenize }},
227550  };
227551
227552  int rc = SQLITE_OK;             /* Return code */
227553  int i;                          /* To iterate through builtin functions */
227554
227555  for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
227556    rc = pApi->xCreateTokenizer(pApi,
227557        aBuiltin[i].zName,
227558        (void*)pApi,
227559        &aBuiltin[i].x,
227560        0
227561    );
227562  }
227563
227564  return rc;
227565}
227566
227567/*
227568** 2012-05-25
227569**
227570** The author disclaims copyright to this source code.  In place of
227571** a legal notice, here is a blessing:
227572**
227573**    May you do good and not evil.
227574**    May you find forgiveness for yourself and forgive others.
227575**    May you share freely, never taking more than you give.
227576**
227577******************************************************************************
227578*/
227579
227580/*
227581** DO NOT EDIT THIS MACHINE GENERATED FILE.
227582*/
227583
227584
227585/* #include <assert.h> */
227586
227587
227588
227589/*
227590** If the argument is a codepoint corresponding to a lowercase letter
227591** in the ASCII range with a diacritic added, return the codepoint
227592** of the ASCII letter only. For example, if passed 235 - "LATIN
227593** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
227594** E"). The resuls of passing a codepoint that corresponds to an
227595** uppercase letter are undefined.
227596*/
227597static int fts5_remove_diacritic(int c, int bComplex){
227598  unsigned short aDia[] = {
227599        0,  1797,  1848,  1859,  1891,  1928,  1940,  1995,
227600     2024,  2040,  2060,  2110,  2168,  2206,  2264,  2286,
227601     2344,  2383,  2472,  2488,  2516,  2596,  2668,  2732,
227602     2782,  2842,  2894,  2954,  2984,  3000,  3028,  3336,
227603     3456,  3696,  3712,  3728,  3744,  3766,  3832,  3896,
227604     3912,  3928,  3944,  3968,  4008,  4040,  4056,  4106,
227605     4138,  4170,  4202,  4234,  4266,  4296,  4312,  4344,
227606     4408,  4424,  4442,  4472,  4488,  4504,  6148,  6198,
227607     6264,  6280,  6360,  6429,  6505,  6529, 61448, 61468,
227608    61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704,
227609    61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914,
227610    61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218,
227611    62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554,
227612    62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766,
227613    62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118,
227614    63182, 63242, 63274, 63310, 63368, 63390,
227615  };
227616#define HIBIT ((unsigned char)0x80)
227617  unsigned char aChar[] = {
227618    '\0',      'a',       'c',       'e',       'i',       'n',
227619    'o',       'u',       'y',       'y',       'a',       'c',
227620    'd',       'e',       'e',       'g',       'h',       'i',
227621    'j',       'k',       'l',       'n',       'o',       'r',
227622    's',       't',       'u',       'u',       'w',       'y',
227623    'z',       'o',       'u',       'a',       'i',       'o',
227624    'u',       'u'|HIBIT, 'a'|HIBIT, 'g',       'k',       'o',
227625    'o'|HIBIT, 'j',       'g',       'n',       'a'|HIBIT, 'a',
227626    'e',       'i',       'o',       'r',       'u',       's',
227627    't',       'h',       'a',       'e',       'o'|HIBIT, 'o',
227628    'o'|HIBIT, 'y',       '\0',      '\0',      '\0',      '\0',
227629    '\0',      '\0',      '\0',      '\0',      'a',       'b',
227630    'c'|HIBIT, 'd',       'd',       'e'|HIBIT, 'e',       'e'|HIBIT,
227631    'f',       'g',       'h',       'h',       'i',       'i'|HIBIT,
227632    'k',       'l',       'l'|HIBIT, 'l',       'm',       'n',
227633    'o'|HIBIT, 'p',       'r',       'r'|HIBIT, 'r',       's',
227634    's'|HIBIT, 't',       'u',       'u'|HIBIT, 'v',       'w',
227635    'w',       'x',       'y',       'z',       'h',       't',
227636    'w',       'y',       'a',       'a'|HIBIT, 'a'|HIBIT, 'a'|HIBIT,
227637    'e',       'e'|HIBIT, 'e'|HIBIT, 'i',       'o',       'o'|HIBIT,
227638    'o'|HIBIT, 'o'|HIBIT, 'u',       'u'|HIBIT, 'u'|HIBIT, 'y',
227639  };
227640
227641  unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
227642  int iRes = 0;
227643  int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
227644  int iLo = 0;
227645  while( iHi>=iLo ){
227646    int iTest = (iHi + iLo) / 2;
227647    if( key >= aDia[iTest] ){
227648      iRes = iTest;
227649      iLo = iTest+1;
227650    }else{
227651      iHi = iTest-1;
227652    }
227653  }
227654  assert( key>=aDia[iRes] );
227655  if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
227656  return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
227657}
227658
227659
227660/*
227661** Return true if the argument interpreted as a unicode codepoint
227662** is a diacritical modifier character.
227663*/
227664static int sqlite3Fts5UnicodeIsdiacritic(int c){
227665  unsigned int mask0 = 0x08029FDF;
227666  unsigned int mask1 = 0x000361F8;
227667  if( c<768 || c>817 ) return 0;
227668  return (c < 768+32) ?
227669      (mask0 & ((unsigned int)1 << (c-768))) :
227670      (mask1 & ((unsigned int)1 << (c-768-32)));
227671}
227672
227673
227674/*
227675** Interpret the argument as a unicode codepoint. If the codepoint
227676** is an upper case character that has a lower case equivalent,
227677** return the codepoint corresponding to the lower case version.
227678** Otherwise, return a copy of the argument.
227679**
227680** The results are undefined if the value passed to this function
227681** is less than zero.
227682*/
227683static int sqlite3Fts5UnicodeFold(int c, int eRemoveDiacritic){
227684  /* Each entry in the following array defines a rule for folding a range
227685  ** of codepoints to lower case. The rule applies to a range of nRange
227686  ** codepoints starting at codepoint iCode.
227687  **
227688  ** If the least significant bit in flags is clear, then the rule applies
227689  ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
227690  ** need to be folded). Or, if it is set, then the rule only applies to
227691  ** every second codepoint in the range, starting with codepoint C.
227692  **
227693  ** The 7 most significant bits in flags are an index into the aiOff[]
227694  ** array. If a specific codepoint C does require folding, then its lower
227695  ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
227696  **
227697  ** The contents of this array are generated by parsing the CaseFolding.txt
227698  ** file distributed as part of the "Unicode Character Database". See
227699  ** http://www.unicode.org for details.
227700  */
227701  static const struct TableEntry {
227702    unsigned short iCode;
227703    unsigned char flags;
227704    unsigned char nRange;
227705  } aEntry[] = {
227706    {65, 14, 26},          {181, 64, 1},          {192, 14, 23},
227707    {216, 14, 7},          {256, 1, 48},          {306, 1, 6},
227708    {313, 1, 16},          {330, 1, 46},          {376, 116, 1},
227709    {377, 1, 6},           {383, 104, 1},         {385, 50, 1},
227710    {386, 1, 4},           {390, 44, 1},          {391, 0, 1},
227711    {393, 42, 2},          {395, 0, 1},           {398, 32, 1},
227712    {399, 38, 1},          {400, 40, 1},          {401, 0, 1},
227713    {403, 42, 1},          {404, 46, 1},          {406, 52, 1},
227714    {407, 48, 1},          {408, 0, 1},           {412, 52, 1},
227715    {413, 54, 1},          {415, 56, 1},          {416, 1, 6},
227716    {422, 60, 1},          {423, 0, 1},           {425, 60, 1},
227717    {428, 0, 1},           {430, 60, 1},          {431, 0, 1},
227718    {433, 58, 2},          {435, 1, 4},           {439, 62, 1},
227719    {440, 0, 1},           {444, 0, 1},           {452, 2, 1},
227720    {453, 0, 1},           {455, 2, 1},           {456, 0, 1},
227721    {458, 2, 1},           {459, 1, 18},          {478, 1, 18},
227722    {497, 2, 1},           {498, 1, 4},           {502, 122, 1},
227723    {503, 134, 1},         {504, 1, 40},          {544, 110, 1},
227724    {546, 1, 18},          {570, 70, 1},          {571, 0, 1},
227725    {573, 108, 1},         {574, 68, 1},          {577, 0, 1},
227726    {579, 106, 1},         {580, 28, 1},          {581, 30, 1},
227727    {582, 1, 10},          {837, 36, 1},          {880, 1, 4},
227728    {886, 0, 1},           {902, 18, 1},          {904, 16, 3},
227729    {908, 26, 1},          {910, 24, 2},          {913, 14, 17},
227730    {931, 14, 9},          {962, 0, 1},           {975, 4, 1},
227731    {976, 140, 1},         {977, 142, 1},         {981, 146, 1},
227732    {982, 144, 1},         {984, 1, 24},          {1008, 136, 1},
227733    {1009, 138, 1},        {1012, 130, 1},        {1013, 128, 1},
227734    {1015, 0, 1},          {1017, 152, 1},        {1018, 0, 1},
227735    {1021, 110, 3},        {1024, 34, 16},        {1040, 14, 32},
227736    {1120, 1, 34},         {1162, 1, 54},         {1216, 6, 1},
227737    {1217, 1, 14},         {1232, 1, 88},         {1329, 22, 38},
227738    {4256, 66, 38},        {4295, 66, 1},         {4301, 66, 1},
227739    {7680, 1, 150},        {7835, 132, 1},        {7838, 96, 1},
227740    {7840, 1, 96},         {7944, 150, 8},        {7960, 150, 6},
227741    {7976, 150, 8},        {7992, 150, 8},        {8008, 150, 6},
227742    {8025, 151, 8},        {8040, 150, 8},        {8072, 150, 8},
227743    {8088, 150, 8},        {8104, 150, 8},        {8120, 150, 2},
227744    {8122, 126, 2},        {8124, 148, 1},        {8126, 100, 1},
227745    {8136, 124, 4},        {8140, 148, 1},        {8152, 150, 2},
227746    {8154, 120, 2},        {8168, 150, 2},        {8170, 118, 2},
227747    {8172, 152, 1},        {8184, 112, 2},        {8186, 114, 2},
227748    {8188, 148, 1},        {8486, 98, 1},         {8490, 92, 1},
227749    {8491, 94, 1},         {8498, 12, 1},         {8544, 8, 16},
227750    {8579, 0, 1},          {9398, 10, 26},        {11264, 22, 47},
227751    {11360, 0, 1},         {11362, 88, 1},        {11363, 102, 1},
227752    {11364, 90, 1},        {11367, 1, 6},         {11373, 84, 1},
227753    {11374, 86, 1},        {11375, 80, 1},        {11376, 82, 1},
227754    {11378, 0, 1},         {11381, 0, 1},         {11390, 78, 2},
227755    {11392, 1, 100},       {11499, 1, 4},         {11506, 0, 1},
227756    {42560, 1, 46},        {42624, 1, 24},        {42786, 1, 14},
227757    {42802, 1, 62},        {42873, 1, 4},         {42877, 76, 1},
227758    {42878, 1, 10},        {42891, 0, 1},         {42893, 74, 1},
227759    {42896, 1, 4},         {42912, 1, 10},        {42922, 72, 1},
227760    {65313, 14, 26},
227761  };
227762  static const unsigned short aiOff[] = {
227763   1,     2,     8,     15,    16,    26,    28,    32,
227764   37,    38,    40,    48,    63,    64,    69,    71,
227765   79,    80,    116,   202,   203,   205,   206,   207,
227766   209,   210,   211,   213,   214,   217,   218,   219,
227767   775,   7264,  10792, 10795, 23228, 23256, 30204, 54721,
227768   54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
227769   57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
227770   65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
227771   65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
227772   65514, 65521, 65527, 65528, 65529,
227773  };
227774
227775  int ret = c;
227776
227777  assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
227778
227779  if( c<128 ){
227780    if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
227781  }else if( c<65536 ){
227782    const struct TableEntry *p;
227783    int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
227784    int iLo = 0;
227785    int iRes = -1;
227786
227787    assert( c>aEntry[0].iCode );
227788    while( iHi>=iLo ){
227789      int iTest = (iHi + iLo) / 2;
227790      int cmp = (c - aEntry[iTest].iCode);
227791      if( cmp>=0 ){
227792        iRes = iTest;
227793        iLo = iTest+1;
227794      }else{
227795        iHi = iTest-1;
227796      }
227797    }
227798
227799    assert( iRes>=0 && c>=aEntry[iRes].iCode );
227800    p = &aEntry[iRes];
227801    if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
227802      ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
227803      assert( ret>0 );
227804    }
227805
227806    if( eRemoveDiacritic ){
227807      ret = fts5_remove_diacritic(ret, eRemoveDiacritic==2);
227808    }
227809  }
227810
227811  else if( c>=66560 && c<66600 ){
227812    ret = c + 40;
227813  }
227814
227815  return ret;
227816}
227817
227818
227819static int sqlite3Fts5UnicodeCatParse(const char *zCat, u8 *aArray){
227820  aArray[0] = 1;
227821  switch( zCat[0] ){
227822    case 'C':
227823          switch( zCat[1] ){
227824            case 'c': aArray[1] = 1; break;
227825            case 'f': aArray[2] = 1; break;
227826            case 'n': aArray[3] = 1; break;
227827            case 's': aArray[4] = 1; break;
227828            case 'o': aArray[31] = 1; break;
227829            case '*':
227830              aArray[1] = 1;
227831              aArray[2] = 1;
227832              aArray[3] = 1;
227833              aArray[4] = 1;
227834              aArray[31] = 1;
227835              break;
227836            default: return 1;          }
227837          break;
227838
227839    case 'L':
227840          switch( zCat[1] ){
227841            case 'l': aArray[5] = 1; break;
227842            case 'm': aArray[6] = 1; break;
227843            case 'o': aArray[7] = 1; break;
227844            case 't': aArray[8] = 1; break;
227845            case 'u': aArray[9] = 1; break;
227846            case 'C': aArray[30] = 1; break;
227847            case '*':
227848              aArray[5] = 1;
227849              aArray[6] = 1;
227850              aArray[7] = 1;
227851              aArray[8] = 1;
227852              aArray[9] = 1;
227853              aArray[30] = 1;
227854              break;
227855            default: return 1;          }
227856          break;
227857
227858    case 'M':
227859          switch( zCat[1] ){
227860            case 'c': aArray[10] = 1; break;
227861            case 'e': aArray[11] = 1; break;
227862            case 'n': aArray[12] = 1; break;
227863            case '*':
227864              aArray[10] = 1;
227865              aArray[11] = 1;
227866              aArray[12] = 1;
227867              break;
227868            default: return 1;          }
227869          break;
227870
227871    case 'N':
227872          switch( zCat[1] ){
227873            case 'd': aArray[13] = 1; break;
227874            case 'l': aArray[14] = 1; break;
227875            case 'o': aArray[15] = 1; break;
227876            case '*':
227877              aArray[13] = 1;
227878              aArray[14] = 1;
227879              aArray[15] = 1;
227880              break;
227881            default: return 1;          }
227882          break;
227883
227884    case 'P':
227885          switch( zCat[1] ){
227886            case 'c': aArray[16] = 1; break;
227887            case 'd': aArray[17] = 1; break;
227888            case 'e': aArray[18] = 1; break;
227889            case 'f': aArray[19] = 1; break;
227890            case 'i': aArray[20] = 1; break;
227891            case 'o': aArray[21] = 1; break;
227892            case 's': aArray[22] = 1; break;
227893            case '*':
227894              aArray[16] = 1;
227895              aArray[17] = 1;
227896              aArray[18] = 1;
227897              aArray[19] = 1;
227898              aArray[20] = 1;
227899              aArray[21] = 1;
227900              aArray[22] = 1;
227901              break;
227902            default: return 1;          }
227903          break;
227904
227905    case 'S':
227906          switch( zCat[1] ){
227907            case 'c': aArray[23] = 1; break;
227908            case 'k': aArray[24] = 1; break;
227909            case 'm': aArray[25] = 1; break;
227910            case 'o': aArray[26] = 1; break;
227911            case '*':
227912              aArray[23] = 1;
227913              aArray[24] = 1;
227914              aArray[25] = 1;
227915              aArray[26] = 1;
227916              break;
227917            default: return 1;          }
227918          break;
227919
227920    case 'Z':
227921          switch( zCat[1] ){
227922            case 'l': aArray[27] = 1; break;
227923            case 'p': aArray[28] = 1; break;
227924            case 's': aArray[29] = 1; break;
227925            case '*':
227926              aArray[27] = 1;
227927              aArray[28] = 1;
227928              aArray[29] = 1;
227929              break;
227930            default: return 1;          }
227931          break;
227932
227933  }
227934  return 0;
227935}
227936
227937static u16 aFts5UnicodeBlock[] = {
227938    0,     1471,  1753,  1760,  1760,  1760,  1760,  1760,  1760,  1760,
227939    1760,  1760,  1760,  1760,  1760,  1763,  1765,
227940  };
227941static u16 aFts5UnicodeMap[] = {
227942    0,     32,    33,    36,    37,    40,    41,    42,    43,    44,
227943    45,    46,    48,    58,    60,    63,    65,    91,    92,    93,
227944    94,    95,    96,    97,    123,   124,   125,   126,   127,   160,
227945    161,   162,   166,   167,   168,   169,   170,   171,   172,   173,
227946    174,   175,   176,   177,   178,   180,   181,   182,   184,   185,
227947    186,   187,   188,   191,   192,   215,   216,   223,   247,   248,
227948    256,   312,   313,   329,   330,   377,   383,   385,   387,   388,
227949    391,   394,   396,   398,   402,   403,   405,   406,   409,   412,
227950    414,   415,   417,   418,   423,   427,   428,   431,   434,   436,
227951    437,   440,   442,   443,   444,   446,   448,   452,   453,   454,
227952    455,   456,   457,   458,   459,   460,   461,   477,   478,   496,
227953    497,   498,   499,   500,   503,   505,   506,   564,   570,   572,
227954    573,   575,   577,   580,   583,   584,   592,   660,   661,   688,
227955    706,   710,   722,   736,   741,   748,   749,   750,   751,   768,
227956    880,   884,   885,   886,   890,   891,   894,   900,   902,   903,
227957    904,   908,   910,   912,   913,   931,   940,   975,   977,   978,
227958    981,   984,   1008,  1012,  1014,  1015,  1018,  1020,  1021,  1072,
227959    1120,  1154,  1155,  1160,  1162,  1217,  1231,  1232,  1329,  1369,
227960    1370,  1377,  1417,  1418,  1423,  1425,  1470,  1471,  1472,  1473,
227961    1475,  1476,  1478,  1479,  1488,  1520,  1523,  1536,  1542,  1545,
227962    1547,  1548,  1550,  1552,  1563,  1566,  1568,  1600,  1601,  1611,
227963    1632,  1642,  1646,  1648,  1649,  1748,  1749,  1750,  1757,  1758,
227964    1759,  1765,  1767,  1769,  1770,  1774,  1776,  1786,  1789,  1791,
227965    1792,  1807,  1808,  1809,  1810,  1840,  1869,  1958,  1969,  1984,
227966    1994,  2027,  2036,  2038,  2039,  2042,  2048,  2070,  2074,  2075,
227967    2084,  2085,  2088,  2089,  2096,  2112,  2137,  2142,  2208,  2210,
227968    2276,  2304,  2307,  2308,  2362,  2363,  2364,  2365,  2366,  2369,
227969    2377,  2381,  2382,  2384,  2385,  2392,  2402,  2404,  2406,  2416,
227970    2417,  2418,  2425,  2433,  2434,  2437,  2447,  2451,  2474,  2482,
227971    2486,  2492,  2493,  2494,  2497,  2503,  2507,  2509,  2510,  2519,
227972    2524,  2527,  2530,  2534,  2544,  2546,  2548,  2554,  2555,  2561,
227973    2563,  2565,  2575,  2579,  2602,  2610,  2613,  2616,  2620,  2622,
227974    2625,  2631,  2635,  2641,  2649,  2654,  2662,  2672,  2674,  2677,
227975    2689,  2691,  2693,  2703,  2707,  2730,  2738,  2741,  2748,  2749,
227976    2750,  2753,  2759,  2761,  2763,  2765,  2768,  2784,  2786,  2790,
227977    2800,  2801,  2817,  2818,  2821,  2831,  2835,  2858,  2866,  2869,
227978    2876,  2877,  2878,  2879,  2880,  2881,  2887,  2891,  2893,  2902,
227979    2903,  2908,  2911,  2914,  2918,  2928,  2929,  2930,  2946,  2947,
227980    2949,  2958,  2962,  2969,  2972,  2974,  2979,  2984,  2990,  3006,
227981    3008,  3009,  3014,  3018,  3021,  3024,  3031,  3046,  3056,  3059,
227982    3065,  3066,  3073,  3077,  3086,  3090,  3114,  3125,  3133,  3134,
227983    3137,  3142,  3146,  3157,  3160,  3168,  3170,  3174,  3192,  3199,
227984    3202,  3205,  3214,  3218,  3242,  3253,  3260,  3261,  3262,  3263,
227985    3264,  3270,  3271,  3274,  3276,  3285,  3294,  3296,  3298,  3302,
227986    3313,  3330,  3333,  3342,  3346,  3389,  3390,  3393,  3398,  3402,
227987    3405,  3406,  3415,  3424,  3426,  3430,  3440,  3449,  3450,  3458,
227988    3461,  3482,  3507,  3517,  3520,  3530,  3535,  3538,  3542,  3544,
227989    3570,  3572,  3585,  3633,  3634,  3636,  3647,  3648,  3654,  3655,
227990    3663,  3664,  3674,  3713,  3716,  3719,  3722,  3725,  3732,  3737,
227991    3745,  3749,  3751,  3754,  3757,  3761,  3762,  3764,  3771,  3773,
227992    3776,  3782,  3784,  3792,  3804,  3840,  3841,  3844,  3859,  3860,
227993    3861,  3864,  3866,  3872,  3882,  3892,  3893,  3894,  3895,  3896,
227994    3897,  3898,  3899,  3900,  3901,  3902,  3904,  3913,  3953,  3967,
227995    3968,  3973,  3974,  3976,  3981,  3993,  4030,  4038,  4039,  4046,
227996    4048,  4053,  4057,  4096,  4139,  4141,  4145,  4146,  4152,  4153,
227997    4155,  4157,  4159,  4160,  4170,  4176,  4182,  4184,  4186,  4190,
227998    4193,  4194,  4197,  4199,  4206,  4209,  4213,  4226,  4227,  4229,
227999    4231,  4237,  4238,  4239,  4240,  4250,  4253,  4254,  4256,  4295,
228000    4301,  4304,  4347,  4348,  4349,  4682,  4688,  4696,  4698,  4704,
228001    4746,  4752,  4786,  4792,  4800,  4802,  4808,  4824,  4882,  4888,
228002    4957,  4960,  4969,  4992,  5008,  5024,  5120,  5121,  5741,  5743,
228003    5760,  5761,  5787,  5788,  5792,  5867,  5870,  5888,  5902,  5906,
228004    5920,  5938,  5941,  5952,  5970,  5984,  5998,  6002,  6016,  6068,
228005    6070,  6071,  6078,  6086,  6087,  6089,  6100,  6103,  6104,  6107,
228006    6108,  6109,  6112,  6128,  6144,  6150,  6151,  6155,  6158,  6160,
228007    6176,  6211,  6212,  6272,  6313,  6314,  6320,  6400,  6432,  6435,
228008    6439,  6441,  6448,  6450,  6451,  6457,  6464,  6468,  6470,  6480,
228009    6512,  6528,  6576,  6593,  6600,  6608,  6618,  6622,  6656,  6679,
228010    6681,  6686,  6688,  6741,  6742,  6743,  6744,  6752,  6753,  6754,
228011    6755,  6757,  6765,  6771,  6783,  6784,  6800,  6816,  6823,  6824,
228012    6912,  6916,  6917,  6964,  6965,  6966,  6971,  6972,  6973,  6978,
228013    6979,  6981,  6992,  7002,  7009,  7019,  7028,  7040,  7042,  7043,
228014    7073,  7074,  7078,  7080,  7082,  7083,  7084,  7086,  7088,  7098,
228015    7142,  7143,  7144,  7146,  7149,  7150,  7151,  7154,  7164,  7168,
228016    7204,  7212,  7220,  7222,  7227,  7232,  7245,  7248,  7258,  7288,
228017    7294,  7360,  7376,  7379,  7380,  7393,  7394,  7401,  7405,  7406,
228018    7410,  7412,  7413,  7424,  7468,  7531,  7544,  7545,  7579,  7616,
228019    7676,  7680,  7830,  7838,  7936,  7944,  7952,  7960,  7968,  7976,
228020    7984,  7992,  8000,  8008,  8016,  8025,  8027,  8029,  8031,  8033,
228021    8040,  8048,  8064,  8072,  8080,  8088,  8096,  8104,  8112,  8118,
228022    8120,  8124,  8125,  8126,  8127,  8130,  8134,  8136,  8140,  8141,
228023    8144,  8150,  8152,  8157,  8160,  8168,  8173,  8178,  8182,  8184,
228024    8188,  8189,  8192,  8203,  8208,  8214,  8216,  8217,  8218,  8219,
228025    8221,  8222,  8223,  8224,  8232,  8233,  8234,  8239,  8240,  8249,
228026    8250,  8251,  8255,  8257,  8260,  8261,  8262,  8263,  8274,  8275,
228027    8276,  8277,  8287,  8288,  8298,  8304,  8305,  8308,  8314,  8317,
228028    8318,  8319,  8320,  8330,  8333,  8334,  8336,  8352,  8400,  8413,
228029    8417,  8418,  8421,  8448,  8450,  8451,  8455,  8456,  8458,  8459,
228030    8462,  8464,  8467,  8468,  8469,  8470,  8472,  8473,  8478,  8484,
228031    8485,  8486,  8487,  8488,  8489,  8490,  8494,  8495,  8496,  8500,
228032    8501,  8505,  8506,  8508,  8510,  8512,  8517,  8519,  8522,  8523,
228033    8524,  8526,  8527,  8528,  8544,  8579,  8581,  8585,  8592,  8597,
228034    8602,  8604,  8608,  8609,  8611,  8612,  8614,  8615,  8622,  8623,
228035    8654,  8656,  8658,  8659,  8660,  8661,  8692,  8960,  8968,  8972,
228036    8992,  8994,  9001,  9002,  9003,  9084,  9085,  9115,  9140,  9180,
228037    9186,  9216,  9280,  9312,  9372,  9450,  9472,  9655,  9656,  9665,
228038    9666,  9720,  9728,  9839,  9840,  9985,  10088, 10089, 10090, 10091,
228039    10092, 10093, 10094, 10095, 10096, 10097, 10098, 10099, 10100, 10101,
228040    10102, 10132, 10176, 10181, 10182, 10183, 10214, 10215, 10216, 10217,
228041    10218, 10219, 10220, 10221, 10222, 10223, 10224, 10240, 10496, 10627,
228042    10628, 10629, 10630, 10631, 10632, 10633, 10634, 10635, 10636, 10637,
228043    10638, 10639, 10640, 10641, 10642, 10643, 10644, 10645, 10646, 10647,
228044    10648, 10649, 10712, 10713, 10714, 10715, 10716, 10748, 10749, 10750,
228045    11008, 11056, 11077, 11079, 11088, 11264, 11312, 11360, 11363, 11365,
228046    11367, 11374, 11377, 11378, 11380, 11381, 11383, 11388, 11390, 11393,
228047    11394, 11492, 11493, 11499, 11503, 11506, 11513, 11517, 11518, 11520,
228048    11559, 11565, 11568, 11631, 11632, 11647, 11648, 11680, 11688, 11696,
228049    11704, 11712, 11720, 11728, 11736, 11744, 11776, 11778, 11779, 11780,
228050    11781, 11782, 11785, 11786, 11787, 11788, 11789, 11790, 11799, 11800,
228051    11802, 11803, 11804, 11805, 11806, 11808, 11809, 11810, 11811, 11812,
228052    11813, 11814, 11815, 11816, 11817, 11818, 11823, 11824, 11834, 11904,
228053    11931, 12032, 12272, 12288, 12289, 12292, 12293, 12294, 12295, 12296,
228054    12297, 12298, 12299, 12300, 12301, 12302, 12303, 12304, 12305, 12306,
228055    12308, 12309, 12310, 12311, 12312, 12313, 12314, 12315, 12316, 12317,
228056    12318, 12320, 12321, 12330, 12334, 12336, 12337, 12342, 12344, 12347,
228057    12348, 12349, 12350, 12353, 12441, 12443, 12445, 12447, 12448, 12449,
228058    12539, 12540, 12543, 12549, 12593, 12688, 12690, 12694, 12704, 12736,
228059    12784, 12800, 12832, 12842, 12872, 12880, 12881, 12896, 12928, 12938,
228060    12977, 12992, 13056, 13312, 19893, 19904, 19968, 40908, 40960, 40981,
228061    40982, 42128, 42192, 42232, 42238, 42240, 42508, 42509, 42512, 42528,
228062    42538, 42560, 42606, 42607, 42608, 42611, 42612, 42622, 42623, 42624,
228063    42655, 42656, 42726, 42736, 42738, 42752, 42775, 42784, 42786, 42800,
228064    42802, 42864, 42865, 42873, 42878, 42888, 42889, 42891, 42896, 42912,
228065    43000, 43002, 43003, 43010, 43011, 43014, 43015, 43019, 43020, 43043,
228066    43045, 43047, 43048, 43056, 43062, 43064, 43065, 43072, 43124, 43136,
228067    43138, 43188, 43204, 43214, 43216, 43232, 43250, 43256, 43259, 43264,
228068    43274, 43302, 43310, 43312, 43335, 43346, 43359, 43360, 43392, 43395,
228069    43396, 43443, 43444, 43446, 43450, 43452, 43453, 43457, 43471, 43472,
228070    43486, 43520, 43561, 43567, 43569, 43571, 43573, 43584, 43587, 43588,
228071    43596, 43597, 43600, 43612, 43616, 43632, 43633, 43639, 43642, 43643,
228072    43648, 43696, 43697, 43698, 43701, 43703, 43705, 43710, 43712, 43713,
228073    43714, 43739, 43741, 43742, 43744, 43755, 43756, 43758, 43760, 43762,
228074    43763, 43765, 43766, 43777, 43785, 43793, 43808, 43816, 43968, 44003,
228075    44005, 44006, 44008, 44009, 44011, 44012, 44013, 44016, 44032, 55203,
228076    55216, 55243, 55296, 56191, 56319, 57343, 57344, 63743, 63744, 64112,
228077    64256, 64275, 64285, 64286, 64287, 64297, 64298, 64312, 64318, 64320,
228078    64323, 64326, 64434, 64467, 64830, 64831, 64848, 64914, 65008, 65020,
228079    65021, 65024, 65040, 65047, 65048, 65049, 65056, 65072, 65073, 65075,
228080    65077, 65078, 65079, 65080, 65081, 65082, 65083, 65084, 65085, 65086,
228081    65087, 65088, 65089, 65090, 65091, 65092, 65093, 65095, 65096, 65097,
228082    65101, 65104, 65108, 65112, 65113, 65114, 65115, 65116, 65117, 65118,
228083    65119, 65122, 65123, 65124, 65128, 65129, 65130, 65136, 65142, 65279,
228084    65281, 65284, 65285, 65288, 65289, 65290, 65291, 65292, 65293, 65294,
228085    65296, 65306, 65308, 65311, 65313, 65339, 65340, 65341, 65342, 65343,
228086    65344, 65345, 65371, 65372, 65373, 65374, 65375, 65376, 65377, 65378,
228087    65379, 65380, 65382, 65392, 65393, 65438, 65440, 65474, 65482, 65490,
228088    65498, 65504, 65506, 65507, 65508, 65509, 65512, 65513, 65517, 65529,
228089    65532, 0,     13,    40,    60,    63,    80,    128,   256,   263,
228090    311,   320,   373,   377,   394,   400,   464,   509,   640,   672,
228091    768,   800,   816,   833,   834,   842,   896,   927,   928,   968,
228092    976,   977,   1024,  1064,  1104,  1184,  2048,  2056,  2058,  2103,
228093    2108,  2111,  2135,  2136,  2304,  2326,  2335,  2336,  2367,  2432,
228094    2494,  2560,  2561,  2565,  2572,  2576,  2581,  2585,  2616,  2623,
228095    2624,  2640,  2656,  2685,  2687,  2816,  2873,  2880,  2904,  2912,
228096    2936,  3072,  3680,  4096,  4097,  4098,  4099,  4152,  4167,  4178,
228097    4198,  4224,  4226,  4227,  4272,  4275,  4279,  4281,  4283,  4285,
228098    4286,  4304,  4336,  4352,  4355,  4391,  4396,  4397,  4406,  4416,
228099    4480,  4482,  4483,  4531,  4534,  4543,  4545,  4549,  4560,  5760,
228100    5803,  5804,  5805,  5806,  5808,  5814,  5815,  5824,  8192,  9216,
228101    9328,  12288, 26624, 28416, 28496, 28497, 28559, 28563, 45056, 53248,
228102    53504, 53545, 53605, 53607, 53610, 53613, 53619, 53627, 53635, 53637,
228103    53644, 53674, 53678, 53760, 53826, 53829, 54016, 54112, 54272, 54298,
228104    54324, 54350, 54358, 54376, 54402, 54428, 54430, 54434, 54437, 54441,
228105    54446, 54454, 54459, 54461, 54469, 54480, 54506, 54532, 54535, 54541,
228106    54550, 54558, 54584, 54587, 54592, 54598, 54602, 54610, 54636, 54662,
228107    54688, 54714, 54740, 54766, 54792, 54818, 54844, 54870, 54896, 54922,
228108    54952, 54977, 54978, 55003, 55004, 55010, 55035, 55036, 55061, 55062,
228109    55068, 55093, 55094, 55119, 55120, 55126, 55151, 55152, 55177, 55178,
228110    55184, 55209, 55210, 55235, 55236, 55242, 55246, 60928, 60933, 60961,
228111    60964, 60967, 60969, 60980, 60985, 60987, 60994, 60999, 61001, 61003,
228112    61005, 61009, 61012, 61015, 61017, 61019, 61021, 61023, 61025, 61028,
228113    61031, 61036, 61044, 61049, 61054, 61056, 61067, 61089, 61093, 61099,
228114    61168, 61440, 61488, 61600, 61617, 61633, 61649, 61696, 61712, 61744,
228115    61808, 61926, 61968, 62016, 62032, 62208, 62256, 62263, 62336, 62368,
228116    62406, 62432, 62464, 62528, 62530, 62713, 62720, 62784, 62800, 62971,
228117    63045, 63104, 63232, 0,     42710, 42752, 46900, 46912, 47133, 63488,
228118    1,     32,    256,   0,     65533,
228119  };
228120static u16 aFts5UnicodeData[] = {
228121    1025,  61,    117,   55,    117,   54,    50,    53,    57,    53,
228122    49,    85,    333,   85,    121,   85,    841,   54,    53,    50,
228123    56,    48,    56,    837,   54,    57,    50,    57,    1057,  61,
228124    53,    151,   58,    53,    56,    58,    39,    52,    57,    34,
228125    58,    56,    58,    57,    79,    56,    37,    85,    56,    47,
228126    39,    51,    111,   53,    745,   57,    233,   773,   57,    261,
228127    1822,  37,    542,   37,    1534,  222,   69,    73,    37,    126,
228128    126,   73,    69,    137,   37,    73,    37,    105,   101,   73,
228129    37,    73,    37,    190,   158,   37,    126,   126,   73,    37,
228130    126,   94,    37,    39,    94,    69,    135,   41,    40,    37,
228131    41,    40,    37,    41,    40,    37,    542,   37,    606,   37,
228132    41,    40,    37,    126,   73,    37,    1886,  197,   73,    37,
228133    73,    69,    126,   105,   37,    286,   2181,  39,    869,   582,
228134    152,   390,   472,   166,   248,   38,    56,    38,    568,   3596,
228135    158,   38,    56,    94,    38,    101,   53,    88,    41,    53,
228136    105,   41,    73,    37,    553,   297,   1125,  94,    37,    105,
228137    101,   798,   133,   94,    57,    126,   94,    37,    1641,  1541,
228138    1118,  58,    172,   75,    1790,  478,   37,    2846,  1225,  38,
228139    213,   1253,  53,    49,    55,    1452,  49,    44,    53,    76,
228140    53,    76,    53,    44,    871,   103,   85,    162,   121,   85,
228141    55,    85,    90,    364,   53,    85,    1031,  38,    327,   684,
228142    333,   149,   71,    44,    3175,  53,    39,    236,   34,    58,
228143    204,   70,    76,    58,    140,   71,    333,   103,   90,    39,
228144    469,   34,    39,    44,    967,   876,   2855,  364,   39,    333,
228145    1063,  300,   70,    58,    117,   38,    711,   140,   38,    300,
228146    38,    108,   38,    172,   501,   807,   108,   53,    39,    359,
228147    876,   108,   42,    1735,  44,    42,    44,    39,    106,   268,
228148    138,   44,    74,    39,    236,   327,   76,    85,    333,   53,
228149    38,    199,   231,   44,    74,    263,   71,    711,   231,   39,
228150    135,   44,    39,    106,   140,   74,    74,    44,    39,    42,
228151    71,    103,   76,    333,   71,    87,    207,   58,    55,    76,
228152    42,    199,   71,    711,   231,   71,    71,    71,    44,    106,
228153    76,    76,    108,   44,    135,   39,    333,   76,    103,   44,
228154    76,    42,    295,   103,   711,   231,   71,    167,   44,    39,
228155    106,   172,   76,    42,    74,    44,    39,    71,    76,    333,
228156    53,    55,    44,    74,    263,   71,    711,   231,   71,    167,
228157    44,    39,    42,    44,    42,    140,   74,    74,    44,    44,
228158    42,    71,    103,   76,    333,   58,    39,    207,   44,    39,
228159    199,   103,   135,   71,    39,    71,    71,    103,   391,   74,
228160    44,    74,    106,   106,   44,    39,    42,    333,   111,   218,
228161    55,    58,    106,   263,   103,   743,   327,   167,   39,    108,
228162    138,   108,   140,   76,    71,    71,    76,    333,   239,   58,
228163    74,    263,   103,   743,   327,   167,   44,    39,    42,    44,
228164    170,   44,    74,    74,    76,    74,    39,    71,    76,    333,
228165    71,    74,    263,   103,   1319,  39,    106,   140,   106,   106,
228166    44,    39,    42,    71,    76,    333,   207,   58,    199,   74,
228167    583,   775,   295,   39,    231,   44,    106,   108,   44,    266,
228168    74,    53,    1543,  44,    71,    236,   55,    199,   38,    268,
228169    53,    333,   85,    71,    39,    71,    39,    39,    135,   231,
228170    103,   39,    39,    71,    135,   44,    71,    204,   76,    39,
228171    167,   38,    204,   333,   135,   39,    122,   501,   58,    53,
228172    122,   76,    218,   333,   335,   58,    44,    58,    44,    58,
228173    44,    54,    50,    54,    50,    74,    263,   1159,  460,   42,
228174    172,   53,    76,    167,   364,   1164,  282,   44,    218,   90,
228175    181,   154,   85,    1383,  74,    140,   42,    204,   42,    76,
228176    74,    76,    39,    333,   213,   199,   74,    76,    135,   108,
228177    39,    106,   71,    234,   103,   140,   423,   44,    74,    76,
228178    202,   44,    39,    42,    333,   106,   44,    90,    1225,  41,
228179    41,    1383,  53,    38,    10631, 135,   231,   39,    135,   1319,
228180    135,   1063,  135,   231,   39,    135,   487,   1831,  135,   2151,
228181    108,   309,   655,   519,   346,   2727,  49,    19847, 85,    551,
228182    61,    839,   54,    50,    2407,  117,   110,   423,   135,   108,
228183    583,   108,   85,    583,   76,    423,   103,   76,    1671,  76,
228184    42,    236,   266,   44,    74,    364,   117,   38,    117,   55,
228185    39,    44,    333,   335,   213,   49,    149,   108,   61,    333,
228186    1127,  38,    1671,  1319,  44,    39,    2247,  935,   108,   138,
228187    76,    106,   74,    44,    202,   108,   58,    85,    333,   967,
228188    167,   1415,  554,   231,   74,    333,   47,    1114,  743,   76,
228189    106,   85,    1703,  42,    44,    42,    236,   44,    42,    44,
228190    74,    268,   202,   332,   44,    333,   333,   245,   38,    213,
228191    140,   42,    1511,  44,    42,    172,   42,    44,    170,   44,
228192    74,    231,   333,   245,   346,   300,   314,   76,    42,    967,
228193    42,    140,   74,    76,    42,    44,    74,    71,    333,   1415,
228194    44,    42,    76,    106,   44,    42,    108,   74,    149,   1159,
228195    266,   268,   74,    76,    181,   333,   103,   333,   967,   198,
228196    85,    277,   108,   53,    428,   42,    236,   135,   44,    135,
228197    74,    44,    71,    1413,  2022,  421,   38,    1093,  1190,  1260,
228198    140,   4830,  261,   3166,  261,   265,   197,   201,   261,   265,
228199    261,   265,   197,   201,   261,   41,    41,    41,    94,    229,
228200    265,   453,   261,   264,   261,   264,   261,   264,   165,   69,
228201    137,   40,    56,    37,    120,   101,   69,    137,   40,    120,
228202    133,   69,    137,   120,   261,   169,   120,   101,   69,    137,
228203    40,    88,    381,   162,   209,   85,    52,    51,    54,    84,
228204    51,    54,    52,    277,   59,    60,    162,   61,    309,   52,
228205    51,    149,   80,    117,   57,    54,    50,    373,   57,    53,
228206    48,    341,   61,    162,   194,   47,    38,    207,   121,   54,
228207    50,    38,    335,   121,   54,    50,    422,   855,   428,   139,
228208    44,    107,   396,   90,    41,    154,   41,    90,    37,    105,
228209    69,    105,   37,    58,    41,    90,    57,    169,   218,   41,
228210    58,    41,    58,    41,    58,    137,   58,    37,    137,   37,
228211    135,   37,    90,    69,    73,    185,   94,    101,   58,    57,
228212    90,    37,    58,    527,   1134,  94,    142,   47,    185,   186,
228213    89,    154,   57,    90,    57,    90,    57,    250,   57,    1018,
228214    89,    90,    57,    58,    57,    1018,  8601,  282,   153,   666,
228215    89,    250,   54,    50,    2618,  57,    986,   825,   1306,  217,
228216    602,   1274,  378,   1935,  2522,  719,   5882,  57,    314,   57,
228217    1754,  281,   3578,  57,    4634,  3322,  54,    50,    54,    50,
228218    54,    50,    54,    50,    54,    50,    54,    50,    54,    50,
228219    975,   1434,  185,   54,    50,    1017,  54,    50,    54,    50,
228220    54,    50,    54,    50,    54,    50,    537,   8218,  4217,  54,
228221    50,    54,    50,    54,    50,    54,    50,    54,    50,    54,
228222    50,    54,    50,    54,    50,    54,    50,    54,    50,    54,
228223    50,    2041,  54,    50,    54,    50,    1049,  54,    50,    8281,
228224    1562,  697,   90,    217,   346,   1513,  1509,  126,   73,    69,
228225    254,   105,   37,    94,    37,    94,    165,   70,    105,   37,
228226    3166,  37,    218,   158,   108,   94,    149,   47,    85,    1221,
228227    37,    37,    1799,  38,    53,    44,    743,   231,   231,   231,
228228    231,   231,   231,   231,   231,   1036,  85,    52,    51,    52,
228229    51,    117,   52,    51,    53,    52,    51,    309,   49,    85,
228230    49,    53,    52,    51,    85,    52,    51,    54,    50,    54,
228231    50,    54,    50,    54,    50,    181,   38,    341,   81,    858,
228232    2874,  6874,  410,   61,    117,   58,    38,    39,    46,    54,
228233    50,    54,    50,    54,    50,    54,    50,    54,    50,    90,
228234    54,    50,    54,    50,    54,    50,    54,    50,    49,    54,
228235    82,    58,    302,   140,   74,    49,    166,   90,    110,   38,
228236    39,    53,    90,    2759,  76,    88,    70,    39,    49,    2887,
228237    53,    102,   39,    1319,  3015,  90,    143,   346,   871,   1178,
228238    519,   1018,  335,   986,   271,   58,    495,   1050,  335,   1274,
228239    495,   2042,  8218,  39,    39,    2074,  39,    39,    679,   38,
228240    36583, 1786,  1287,  198,   85,    8583,  38,    117,   519,   333,
228241    71,    1502,  39,    44,    107,   53,    332,   53,    38,    798,
228242    44,    2247,  334,   76,    213,   760,   294,   88,    478,   69,
228243    2014,  38,    261,   190,   350,   38,    88,    158,   158,   382,
228244    70,    37,    231,   44,    103,   44,    135,   44,    743,   74,
228245    76,    42,    154,   207,   90,    55,    58,    1671,  149,   74,
228246    1607,  522,   44,    85,    333,   588,   199,   117,   39,    333,
228247    903,   268,   85,    743,   364,   74,    53,    935,   108,   42,
228248    1511,  44,    74,    140,   74,    44,    138,   437,   38,    333,
228249    85,    1319,  204,   74,    76,    74,    76,    103,   44,    263,
228250    44,    42,    333,   149,   519,   38,    199,   122,   39,    42,
228251    1543,  44,    39,    108,   71,    76,    167,   76,    39,    44,
228252    39,    71,    38,    85,    359,   42,    76,    74,    85,    39,
228253    70,    42,    44,    199,   199,   199,   231,   231,   1127,  74,
228254    44,    74,    44,    74,    53,    42,    44,    333,   39,    39,
228255    743,   1575,  36,    68,    68,    36,    63,    63,    11719, 3399,
228256    229,   165,   39,    44,    327,   57,    423,   167,   39,    71,
228257    71,    3463,  536,   11623, 54,    50,    2055,  1735,  391,   55,
228258    58,    524,   245,   54,    50,    53,    236,   53,    81,    80,
228259    54,    50,    54,    50,    54,    50,    54,    50,    54,    50,
228260    54,    50,    54,    50,    54,    50,    85,    54,    50,    149,
228261    112,   117,   149,   49,    54,    50,    54,    50,    54,    50,
228262    117,   57,    49,    121,   53,    55,    85,    167,   4327,  34,
228263    117,   55,    117,   54,    50,    53,    57,    53,    49,    85,
228264    333,   85,    121,   85,    841,   54,    53,    50,    56,    48,
228265    56,    837,   54,    57,    50,    57,    54,    50,    53,    54,
228266    50,    85,    327,   38,    1447,  70,    999,   199,   199,   199,
228267    103,   87,    57,    56,    58,    87,    58,    153,   90,    98,
228268    90,    391,   839,   615,   71,    487,   455,   3943,  117,   1455,
228269    314,   1710,  143,   570,   47,    410,   1466,  44,    935,   1575,
228270    999,   143,   551,   46,    263,   46,    967,   53,    1159,  263,
228271    53,    174,   1289,  1285,  2503,  333,   199,   39,    1415,  71,
228272    39,    743,   53,    271,   711,   207,   53,    839,   53,    1799,
228273    71,    39,    108,   76,    140,   135,   103,   871,   108,   44,
228274    271,   309,   935,   79,    53,    1735,  245,   711,   271,   615,
228275    271,   2343,  1007,  42,    44,    42,    1703,  492,   245,   655,
228276    333,   76,    42,    1447,  106,   140,   74,    76,    85,    34,
228277    149,   807,   333,   108,   1159,  172,   42,    268,   333,   149,
228278    76,    42,    1543,  106,   300,   74,    135,   149,   333,   1383,
228279    44,    42,    44,    74,    204,   42,    44,    333,   28135, 3182,
228280    149,   34279, 18215, 2215,  39,    1482,  140,   422,   71,    7898,
228281    1274,  1946,  74,    108,   122,   202,   258,   268,   90,    236,
228282    986,   140,   1562,  2138,  108,   58,    2810,  591,   841,   837,
228283    841,   229,   581,   841,   837,   41,    73,    41,    73,    137,
228284    265,   133,   37,    229,   357,   841,   837,   73,    137,   265,
228285    233,   837,   73,    137,   169,   41,    233,   837,   841,   837,
228286    841,   837,   841,   837,   841,   837,   841,   837,   841,   901,
228287    809,   57,    805,   57,    197,   809,   57,    805,   57,    197,
228288    809,   57,    805,   57,    197,   809,   57,    805,   57,    197,
228289    809,   57,    805,   57,    197,   94,    1613,  135,   871,   71,
228290    39,    39,    327,   135,   39,    39,    39,    39,    39,    39,
228291    103,   71,    39,    39,    39,    39,    39,    39,    71,    39,
228292    135,   231,   135,   135,   39,    327,   551,   103,   167,   551,
228293    89,    1434,  3226,  506,   474,   506,   506,   367,   1018,  1946,
228294    1402,  954,   1402,  314,   90,    1082,  218,   2266,  666,   1210,
228295    186,   570,   2042,  58,    5850,  154,   2010,  154,   794,   2266,
228296    378,   2266,  3738,  39,    39,    39,    39,    39,    39,    17351,
228297    34,    3074,  7692,  63,    63,
228298  };
228299
228300static int sqlite3Fts5UnicodeCategory(u32 iCode) {
228301  int iRes = -1;
228302  int iHi;
228303  int iLo;
228304  int ret;
228305  u16 iKey;
228306
228307  if( iCode>=(1<<20) ){
228308    return 0;
228309  }
228310  iLo = aFts5UnicodeBlock[(iCode>>16)];
228311  iHi = aFts5UnicodeBlock[1+(iCode>>16)];
228312  iKey = (iCode & 0xFFFF);
228313  while( iHi>iLo ){
228314    int iTest = (iHi + iLo) / 2;
228315    assert( iTest>=iLo && iTest<iHi );
228316    if( iKey>=aFts5UnicodeMap[iTest] ){
228317      iRes = iTest;
228318      iLo = iTest+1;
228319    }else{
228320      iHi = iTest;
228321    }
228322  }
228323
228324  if( iRes<0 ) return 0;
228325  if( iKey>=(aFts5UnicodeMap[iRes]+(aFts5UnicodeData[iRes]>>5)) ) return 0;
228326  ret = aFts5UnicodeData[iRes] & 0x1F;
228327  if( ret!=30 ) return ret;
228328  return ((iKey - aFts5UnicodeMap[iRes]) & 0x01) ? 5 : 9;
228329}
228330
228331static void sqlite3Fts5UnicodeAscii(u8 *aArray, u8 *aAscii){
228332  int i = 0;
228333  int iTbl = 0;
228334  while( i<128 ){
228335    int bToken = aArray[ aFts5UnicodeData[iTbl] & 0x1F ];
228336    int n = (aFts5UnicodeData[iTbl] >> 5) + i;
228337    for(; i<128 && i<n; i++){
228338      aAscii[i] = (u8)bToken;
228339    }
228340    iTbl++;
228341  }
228342}
228343
228344/*
228345** 2015 May 30
228346**
228347** The author disclaims copyright to this source code.  In place of
228348** a legal notice, here is a blessing:
228349**
228350**    May you do good and not evil.
228351**    May you find forgiveness for yourself and forgive others.
228352**    May you share freely, never taking more than you give.
228353**
228354******************************************************************************
228355**
228356** Routines for varint serialization and deserialization.
228357*/
228358
228359
228360/* #include "fts5Int.h" */
228361
228362/*
228363** This is a copy of the sqlite3GetVarint32() routine from the SQLite core.
228364** Except, this version does handle the single byte case that the core
228365** version depends on being handled before its function is called.
228366*/
228367static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v){
228368  u32 a,b;
228369
228370  /* The 1-byte case. Overwhelmingly the most common. */
228371  a = *p;
228372  /* a: p0 (unmasked) */
228373  if (!(a&0x80))
228374  {
228375    /* Values between 0 and 127 */
228376    *v = a;
228377    return 1;
228378  }
228379
228380  /* The 2-byte case */
228381  p++;
228382  b = *p;
228383  /* b: p1 (unmasked) */
228384  if (!(b&0x80))
228385  {
228386    /* Values between 128 and 16383 */
228387    a &= 0x7f;
228388    a = a<<7;
228389    *v = a | b;
228390    return 2;
228391  }
228392
228393  /* The 3-byte case */
228394  p++;
228395  a = a<<14;
228396  a |= *p;
228397  /* a: p0<<14 | p2 (unmasked) */
228398  if (!(a&0x80))
228399  {
228400    /* Values between 16384 and 2097151 */
228401    a &= (0x7f<<14)|(0x7f);
228402    b &= 0x7f;
228403    b = b<<7;
228404    *v = a | b;
228405    return 3;
228406  }
228407
228408  /* A 32-bit varint is used to store size information in btrees.
228409  ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
228410  ** A 3-byte varint is sufficient, for example, to record the size
228411  ** of a 1048569-byte BLOB or string.
228412  **
228413  ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
228414  ** rare larger cases can be handled by the slower 64-bit varint
228415  ** routine.
228416  */
228417  {
228418    u64 v64;
228419    u8 n;
228420    p -= 2;
228421    n = sqlite3Fts5GetVarint(p, &v64);
228422    *v = ((u32)v64) & 0x7FFFFFFF;
228423    assert( n>3 && n<=9 );
228424    return n;
228425  }
228426}
228427
228428
228429/*
228430** Bitmasks used by sqlite3GetVarint().  These precomputed constants
228431** are defined here rather than simply putting the constant expressions
228432** inline in order to work around bugs in the RVT compiler.
228433**
228434** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
228435**
228436** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
228437*/
228438#define SLOT_2_0     0x001fc07f
228439#define SLOT_4_2_0   0xf01fc07f
228440
228441/*
228442** Read a 64-bit variable-length integer from memory starting at p[0].
228443** Return the number of bytes read.  The value is stored in *v.
228444*/
228445static u8 sqlite3Fts5GetVarint(const unsigned char *p, u64 *v){
228446  u32 a,b,s;
228447
228448  a = *p;
228449  /* a: p0 (unmasked) */
228450  if (!(a&0x80))
228451  {
228452    *v = a;
228453    return 1;
228454  }
228455
228456  p++;
228457  b = *p;
228458  /* b: p1 (unmasked) */
228459  if (!(b&0x80))
228460  {
228461    a &= 0x7f;
228462    a = a<<7;
228463    a |= b;
228464    *v = a;
228465    return 2;
228466  }
228467
228468  /* Verify that constants are precomputed correctly */
228469  assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
228470  assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
228471
228472  p++;
228473  a = a<<14;
228474  a |= *p;
228475  /* a: p0<<14 | p2 (unmasked) */
228476  if (!(a&0x80))
228477  {
228478    a &= SLOT_2_0;
228479    b &= 0x7f;
228480    b = b<<7;
228481    a |= b;
228482    *v = a;
228483    return 3;
228484  }
228485
228486  /* CSE1 from below */
228487  a &= SLOT_2_0;
228488  p++;
228489  b = b<<14;
228490  b |= *p;
228491  /* b: p1<<14 | p3 (unmasked) */
228492  if (!(b&0x80))
228493  {
228494    b &= SLOT_2_0;
228495    /* moved CSE1 up */
228496    /* a &= (0x7f<<14)|(0x7f); */
228497    a = a<<7;
228498    a |= b;
228499    *v = a;
228500    return 4;
228501  }
228502
228503  /* a: p0<<14 | p2 (masked) */
228504  /* b: p1<<14 | p3 (unmasked) */
228505  /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
228506  /* moved CSE1 up */
228507  /* a &= (0x7f<<14)|(0x7f); */
228508  b &= SLOT_2_0;
228509  s = a;
228510  /* s: p0<<14 | p2 (masked) */
228511
228512  p++;
228513  a = a<<14;
228514  a |= *p;
228515  /* a: p0<<28 | p2<<14 | p4 (unmasked) */
228516  if (!(a&0x80))
228517  {
228518    /* we can skip these cause they were (effectively) done above in calc'ing s */
228519    /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
228520    /* b &= (0x7f<<14)|(0x7f); */
228521    b = b<<7;
228522    a |= b;
228523    s = s>>18;
228524    *v = ((u64)s)<<32 | a;
228525    return 5;
228526  }
228527
228528  /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
228529  s = s<<7;
228530  s |= b;
228531  /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
228532
228533  p++;
228534  b = b<<14;
228535  b |= *p;
228536  /* b: p1<<28 | p3<<14 | p5 (unmasked) */
228537  if (!(b&0x80))
228538  {
228539    /* we can skip this cause it was (effectively) done above in calc'ing s */
228540    /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
228541    a &= SLOT_2_0;
228542    a = a<<7;
228543    a |= b;
228544    s = s>>18;
228545    *v = ((u64)s)<<32 | a;
228546    return 6;
228547  }
228548
228549  p++;
228550  a = a<<14;
228551  a |= *p;
228552  /* a: p2<<28 | p4<<14 | p6 (unmasked) */
228553  if (!(a&0x80))
228554  {
228555    a &= SLOT_4_2_0;
228556    b &= SLOT_2_0;
228557    b = b<<7;
228558    a |= b;
228559    s = s>>11;
228560    *v = ((u64)s)<<32 | a;
228561    return 7;
228562  }
228563
228564  /* CSE2 from below */
228565  a &= SLOT_2_0;
228566  p++;
228567  b = b<<14;
228568  b |= *p;
228569  /* b: p3<<28 | p5<<14 | p7 (unmasked) */
228570  if (!(b&0x80))
228571  {
228572    b &= SLOT_4_2_0;
228573    /* moved CSE2 up */
228574    /* a &= (0x7f<<14)|(0x7f); */
228575    a = a<<7;
228576    a |= b;
228577    s = s>>4;
228578    *v = ((u64)s)<<32 | a;
228579    return 8;
228580  }
228581
228582  p++;
228583  a = a<<15;
228584  a |= *p;
228585  /* a: p4<<29 | p6<<15 | p8 (unmasked) */
228586
228587  /* moved CSE2 up */
228588  /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
228589  b &= SLOT_2_0;
228590  b = b<<8;
228591  a |= b;
228592
228593  s = s<<4;
228594  b = p[-4];
228595  b &= 0x7f;
228596  b = b>>3;
228597  s |= b;
228598
228599  *v = ((u64)s)<<32 | a;
228600
228601  return 9;
228602}
228603
228604/*
228605** The variable-length integer encoding is as follows:
228606**
228607** KEY:
228608**         A = 0xxxxxxx    7 bits of data and one flag bit
228609**         B = 1xxxxxxx    7 bits of data and one flag bit
228610**         C = xxxxxxxx    8 bits of data
228611**
228612**  7 bits - A
228613** 14 bits - BA
228614** 21 bits - BBA
228615** 28 bits - BBBA
228616** 35 bits - BBBBA
228617** 42 bits - BBBBBA
228618** 49 bits - BBBBBBA
228619** 56 bits - BBBBBBBA
228620** 64 bits - BBBBBBBBC
228621*/
228622
228623#ifdef SQLITE_NOINLINE
228624# define FTS5_NOINLINE SQLITE_NOINLINE
228625#else
228626# define FTS5_NOINLINE
228627#endif
228628
228629/*
228630** Write a 64-bit variable-length integer to memory starting at p[0].
228631** The length of data write will be between 1 and 9 bytes.  The number
228632** of bytes written is returned.
228633**
228634** A variable-length integer consists of the lower 7 bits of each byte
228635** for all bytes that have the 8th bit set and one byte with the 8th
228636** bit clear.  Except, if we get to the 9th byte, it stores the full
228637** 8 bits and is the last byte.
228638*/
228639static int FTS5_NOINLINE fts5PutVarint64(unsigned char *p, u64 v){
228640  int i, j, n;
228641  u8 buf[10];
228642  if( v & (((u64)0xff000000)<<32) ){
228643    p[8] = (u8)v;
228644    v >>= 8;
228645    for(i=7; i>=0; i--){
228646      p[i] = (u8)((v & 0x7f) | 0x80);
228647      v >>= 7;
228648    }
228649    return 9;
228650  }
228651  n = 0;
228652  do{
228653    buf[n++] = (u8)((v & 0x7f) | 0x80);
228654    v >>= 7;
228655  }while( v!=0 );
228656  buf[0] &= 0x7f;
228657  assert( n<=9 );
228658  for(i=0, j=n-1; j>=0; j--, i++){
228659    p[i] = buf[j];
228660  }
228661  return n;
228662}
228663
228664static int sqlite3Fts5PutVarint(unsigned char *p, u64 v){
228665  if( v<=0x7f ){
228666    p[0] = v&0x7f;
228667    return 1;
228668  }
228669  if( v<=0x3fff ){
228670    p[0] = ((v>>7)&0x7f)|0x80;
228671    p[1] = v&0x7f;
228672    return 2;
228673  }
228674  return fts5PutVarint64(p,v);
228675}
228676
228677
228678static int sqlite3Fts5GetVarintLen(u32 iVal){
228679#if 0
228680  if( iVal<(1 << 7 ) ) return 1;
228681#endif
228682  assert( iVal>=(1 << 7) );
228683  if( iVal<(1 << 14) ) return 2;
228684  if( iVal<(1 << 21) ) return 3;
228685  if( iVal<(1 << 28) ) return 4;
228686  return 5;
228687}
228688
228689/*
228690** 2015 May 08
228691**
228692** The author disclaims copyright to this source code.  In place of
228693** a legal notice, here is a blessing:
228694**
228695**    May you do good and not evil.
228696**    May you find forgiveness for yourself and forgive others.
228697**    May you share freely, never taking more than you give.
228698**
228699******************************************************************************
228700**
228701** This is an SQLite virtual table module implementing direct access to an
228702** existing FTS5 index. The module may create several different types of
228703** tables:
228704**
228705** col:
228706**     CREATE TABLE vocab(term, col, doc, cnt, PRIMARY KEY(term, col));
228707**
228708**   One row for each term/column combination. The value of $doc is set to
228709**   the number of fts5 rows that contain at least one instance of term
228710**   $term within column $col. Field $cnt is set to the total number of
228711**   instances of term $term in column $col (in any row of the fts5 table).
228712**
228713** row:
228714**     CREATE TABLE vocab(term, doc, cnt, PRIMARY KEY(term));
228715**
228716**   One row for each term in the database. The value of $doc is set to
228717**   the number of fts5 rows that contain at least one instance of term
228718**   $term. Field $cnt is set to the total number of instances of term
228719**   $term in the database.
228720**
228721** instance:
228722**     CREATE TABLE vocab(term, doc, col, offset, PRIMARY KEY(<all-fields>));
228723**
228724**   One row for each term instance in the database.
228725*/
228726
228727
228728/* #include "fts5Int.h" */
228729
228730
228731typedef struct Fts5VocabTable Fts5VocabTable;
228732typedef struct Fts5VocabCursor Fts5VocabCursor;
228733
228734struct Fts5VocabTable {
228735  sqlite3_vtab base;
228736  char *zFts5Tbl;                 /* Name of fts5 table */
228737  char *zFts5Db;                  /* Db containing fts5 table */
228738  sqlite3 *db;                    /* Database handle */
228739  Fts5Global *pGlobal;            /* FTS5 global object for this database */
228740  int eType;                      /* FTS5_VOCAB_COL, ROW or INSTANCE */
228741  unsigned bBusy;                 /* True if busy */
228742};
228743
228744struct Fts5VocabCursor {
228745  sqlite3_vtab_cursor base;
228746  sqlite3_stmt *pStmt;            /* Statement holding lock on pIndex */
228747  Fts5Table *pFts5;               /* Associated FTS5 table */
228748
228749  int bEof;                       /* True if this cursor is at EOF */
228750  Fts5IndexIter *pIter;           /* Term/rowid iterator object */
228751
228752  int nLeTerm;                    /* Size of zLeTerm in bytes */
228753  char *zLeTerm;                  /* (term <= $zLeTerm) paramater, or NULL */
228754
228755  /* These are used by 'col' tables only */
228756  int iCol;
228757  i64 *aCnt;
228758  i64 *aDoc;
228759
228760  /* Output values used by all tables. */
228761  i64 rowid;                      /* This table's current rowid value */
228762  Fts5Buffer term;                /* Current value of 'term' column */
228763
228764  /* Output values Used by 'instance' tables only */
228765  i64 iInstPos;
228766  int iInstOff;
228767};
228768
228769#define FTS5_VOCAB_COL      0
228770#define FTS5_VOCAB_ROW      1
228771#define FTS5_VOCAB_INSTANCE 2
228772
228773#define FTS5_VOCAB_COL_SCHEMA  "term, col, doc, cnt"
228774#define FTS5_VOCAB_ROW_SCHEMA  "term, doc, cnt"
228775#define FTS5_VOCAB_INST_SCHEMA "term, doc, col, offset"
228776
228777/*
228778** Bits for the mask used as the idxNum value by xBestIndex/xFilter.
228779*/
228780#define FTS5_VOCAB_TERM_EQ 0x01
228781#define FTS5_VOCAB_TERM_GE 0x02
228782#define FTS5_VOCAB_TERM_LE 0x04
228783
228784
228785/*
228786** Translate a string containing an fts5vocab table type to an
228787** FTS5_VOCAB_XXX constant. If successful, set *peType to the output
228788** value and return SQLITE_OK. Otherwise, set *pzErr to an error message
228789** and return SQLITE_ERROR.
228790*/
228791static int fts5VocabTableType(const char *zType, char **pzErr, int *peType){
228792  int rc = SQLITE_OK;
228793  char *zCopy = sqlite3Fts5Strndup(&rc, zType, -1);
228794  if( rc==SQLITE_OK ){
228795    sqlite3Fts5Dequote(zCopy);
228796    if( sqlite3_stricmp(zCopy, "col")==0 ){
228797      *peType = FTS5_VOCAB_COL;
228798    }else
228799
228800    if( sqlite3_stricmp(zCopy, "row")==0 ){
228801      *peType = FTS5_VOCAB_ROW;
228802    }else
228803    if( sqlite3_stricmp(zCopy, "instance")==0 ){
228804      *peType = FTS5_VOCAB_INSTANCE;
228805    }else
228806    {
228807      *pzErr = sqlite3_mprintf("fts5vocab: unknown table type: %Q", zCopy);
228808      rc = SQLITE_ERROR;
228809    }
228810    sqlite3_free(zCopy);
228811  }
228812
228813  return rc;
228814}
228815
228816
228817/*
228818** The xDisconnect() virtual table method.
228819*/
228820static int fts5VocabDisconnectMethod(sqlite3_vtab *pVtab){
228821  Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
228822  sqlite3_free(pTab);
228823  return SQLITE_OK;
228824}
228825
228826/*
228827** The xDestroy() virtual table method.
228828*/
228829static int fts5VocabDestroyMethod(sqlite3_vtab *pVtab){
228830  Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
228831  sqlite3_free(pTab);
228832  return SQLITE_OK;
228833}
228834
228835/*
228836** This function is the implementation of both the xConnect and xCreate
228837** methods of the FTS3 virtual table.
228838**
228839** The argv[] array contains the following:
228840**
228841**   argv[0]   -> module name  ("fts5vocab")
228842**   argv[1]   -> database name
228843**   argv[2]   -> table name
228844**
228845** then:
228846**
228847**   argv[3]   -> name of fts5 table
228848**   argv[4]   -> type of fts5vocab table
228849**
228850** or, for tables in the TEMP schema only.
228851**
228852**   argv[3]   -> name of fts5 tables database
228853**   argv[4]   -> name of fts5 table
228854**   argv[5]   -> type of fts5vocab table
228855*/
228856static int fts5VocabInitVtab(
228857  sqlite3 *db,                    /* The SQLite database connection */
228858  void *pAux,                     /* Pointer to Fts5Global object */
228859  int argc,                       /* Number of elements in argv array */
228860  const char * const *argv,       /* xCreate/xConnect argument array */
228861  sqlite3_vtab **ppVTab,          /* Write the resulting vtab structure here */
228862  char **pzErr                    /* Write any error message here */
228863){
228864  const char *azSchema[] = {
228865    "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA  ")",
228866    "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA  ")",
228867    "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA ")"
228868  };
228869
228870  Fts5VocabTable *pRet = 0;
228871  int rc = SQLITE_OK;             /* Return code */
228872  int bDb;
228873
228874  bDb = (argc==6 && strlen(argv[1])==4 && memcmp("temp", argv[1], 4)==0);
228875
228876  if( argc!=5 && bDb==0 ){
228877    *pzErr = sqlite3_mprintf("wrong number of vtable arguments");
228878    rc = SQLITE_ERROR;
228879  }else{
228880    int nByte;                      /* Bytes of space to allocate */
228881    const char *zDb = bDb ? argv[3] : argv[1];
228882    const char *zTab = bDb ? argv[4] : argv[3];
228883    const char *zType = bDb ? argv[5] : argv[4];
228884    int nDb = (int)strlen(zDb)+1;
228885    int nTab = (int)strlen(zTab)+1;
228886    int eType = 0;
228887
228888    rc = fts5VocabTableType(zType, pzErr, &eType);
228889    if( rc==SQLITE_OK ){
228890      assert( eType>=0 && eType<ArraySize(azSchema) );
228891      rc = sqlite3_declare_vtab(db, azSchema[eType]);
228892    }
228893
228894    nByte = sizeof(Fts5VocabTable) + nDb + nTab;
228895    pRet = sqlite3Fts5MallocZero(&rc, nByte);
228896    if( pRet ){
228897      pRet->pGlobal = (Fts5Global*)pAux;
228898      pRet->eType = eType;
228899      pRet->db = db;
228900      pRet->zFts5Tbl = (char*)&pRet[1];
228901      pRet->zFts5Db = &pRet->zFts5Tbl[nTab];
228902      memcpy(pRet->zFts5Tbl, zTab, nTab);
228903      memcpy(pRet->zFts5Db, zDb, nDb);
228904      sqlite3Fts5Dequote(pRet->zFts5Tbl);
228905      sqlite3Fts5Dequote(pRet->zFts5Db);
228906    }
228907  }
228908
228909  *ppVTab = (sqlite3_vtab*)pRet;
228910  return rc;
228911}
228912
228913
228914/*
228915** The xConnect() and xCreate() methods for the virtual table. All the
228916** work is done in function fts5VocabInitVtab().
228917*/
228918static int fts5VocabConnectMethod(
228919  sqlite3 *db,                    /* Database connection */
228920  void *pAux,                     /* Pointer to tokenizer hash table */
228921  int argc,                       /* Number of elements in argv array */
228922  const char * const *argv,       /* xCreate/xConnect argument array */
228923  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
228924  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
228925){
228926  return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
228927}
228928static int fts5VocabCreateMethod(
228929  sqlite3 *db,                    /* Database connection */
228930  void *pAux,                     /* Pointer to tokenizer hash table */
228931  int argc,                       /* Number of elements in argv array */
228932  const char * const *argv,       /* xCreate/xConnect argument array */
228933  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
228934  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
228935){
228936  return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
228937}
228938
228939/*
228940** Implementation of the xBestIndex method.
228941**
228942** Only constraints of the form:
228943**
228944**     term <= ?
228945**     term == ?
228946**     term >= ?
228947**
228948** are interpreted. Less-than and less-than-or-equal are treated
228949** identically, as are greater-than and greater-than-or-equal.
228950*/
228951static int fts5VocabBestIndexMethod(
228952  sqlite3_vtab *pUnused,
228953  sqlite3_index_info *pInfo
228954){
228955  int i;
228956  int iTermEq = -1;
228957  int iTermGe = -1;
228958  int iTermLe = -1;
228959  int idxNum = 0;
228960  int nArg = 0;
228961
228962  UNUSED_PARAM(pUnused);
228963
228964  for(i=0; i<pInfo->nConstraint; i++){
228965    struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
228966    if( p->usable==0 ) continue;
228967    if( p->iColumn==0 ){          /* term column */
228968      if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ) iTermEq = i;
228969      if( p->op==SQLITE_INDEX_CONSTRAINT_LE ) iTermLe = i;
228970      if( p->op==SQLITE_INDEX_CONSTRAINT_LT ) iTermLe = i;
228971      if( p->op==SQLITE_INDEX_CONSTRAINT_GE ) iTermGe = i;
228972      if( p->op==SQLITE_INDEX_CONSTRAINT_GT ) iTermGe = i;
228973    }
228974  }
228975
228976  if( iTermEq>=0 ){
228977    idxNum |= FTS5_VOCAB_TERM_EQ;
228978    pInfo->aConstraintUsage[iTermEq].argvIndex = ++nArg;
228979    pInfo->estimatedCost = 100;
228980  }else{
228981    pInfo->estimatedCost = 1000000;
228982    if( iTermGe>=0 ){
228983      idxNum |= FTS5_VOCAB_TERM_GE;
228984      pInfo->aConstraintUsage[iTermGe].argvIndex = ++nArg;
228985      pInfo->estimatedCost = pInfo->estimatedCost / 2;
228986    }
228987    if( iTermLe>=0 ){
228988      idxNum |= FTS5_VOCAB_TERM_LE;
228989      pInfo->aConstraintUsage[iTermLe].argvIndex = ++nArg;
228990      pInfo->estimatedCost = pInfo->estimatedCost / 2;
228991    }
228992  }
228993
228994  /* This virtual table always delivers results in ascending order of
228995  ** the "term" column (column 0). So if the user has requested this
228996  ** specifically - "ORDER BY term" or "ORDER BY term ASC" - set the
228997  ** sqlite3_index_info.orderByConsumed flag to tell the core the results
228998  ** are already in sorted order.  */
228999  if( pInfo->nOrderBy==1
229000   && pInfo->aOrderBy[0].iColumn==0
229001   && pInfo->aOrderBy[0].desc==0
229002  ){
229003    pInfo->orderByConsumed = 1;
229004  }
229005
229006  pInfo->idxNum = idxNum;
229007  return SQLITE_OK;
229008}
229009
229010/*
229011** Implementation of xOpen method.
229012*/
229013static int fts5VocabOpenMethod(
229014  sqlite3_vtab *pVTab,
229015  sqlite3_vtab_cursor **ppCsr
229016){
229017  Fts5VocabTable *pTab = (Fts5VocabTable*)pVTab;
229018  Fts5Table *pFts5 = 0;
229019  Fts5VocabCursor *pCsr = 0;
229020  int rc = SQLITE_OK;
229021  sqlite3_stmt *pStmt = 0;
229022  char *zSql = 0;
229023
229024  if( pTab->bBusy ){
229025    pVTab->zErrMsg = sqlite3_mprintf(
229026       "recursive definition for %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
229027    );
229028    return SQLITE_ERROR;
229029  }
229030  zSql = sqlite3Fts5Mprintf(&rc,
229031      "SELECT t.%Q FROM %Q.%Q AS t WHERE t.%Q MATCH '*id'",
229032      pTab->zFts5Tbl, pTab->zFts5Db, pTab->zFts5Tbl, pTab->zFts5Tbl
229033  );
229034  if( zSql ){
229035    rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
229036  }
229037  sqlite3_free(zSql);
229038  assert( rc==SQLITE_OK || pStmt==0 );
229039  if( rc==SQLITE_ERROR ) rc = SQLITE_OK;
229040
229041  pTab->bBusy = 1;
229042  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
229043    i64 iId = sqlite3_column_int64(pStmt, 0);
229044    pFts5 = sqlite3Fts5TableFromCsrid(pTab->pGlobal, iId);
229045  }
229046  pTab->bBusy = 0;
229047
229048  if( rc==SQLITE_OK ){
229049    if( pFts5==0 ){
229050      rc = sqlite3_finalize(pStmt);
229051      pStmt = 0;
229052      if( rc==SQLITE_OK ){
229053        pVTab->zErrMsg = sqlite3_mprintf(
229054            "no such fts5 table: %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
229055            );
229056        rc = SQLITE_ERROR;
229057      }
229058    }else{
229059      rc = sqlite3Fts5FlushToDisk(pFts5);
229060    }
229061  }
229062
229063  if( rc==SQLITE_OK ){
229064    int nByte = pFts5->pConfig->nCol * sizeof(i64)*2 + sizeof(Fts5VocabCursor);
229065    pCsr = (Fts5VocabCursor*)sqlite3Fts5MallocZero(&rc, nByte);
229066  }
229067
229068  if( pCsr ){
229069    pCsr->pFts5 = pFts5;
229070    pCsr->pStmt = pStmt;
229071    pCsr->aCnt = (i64*)&pCsr[1];
229072    pCsr->aDoc = &pCsr->aCnt[pFts5->pConfig->nCol];
229073  }else{
229074    sqlite3_finalize(pStmt);
229075  }
229076
229077  *ppCsr = (sqlite3_vtab_cursor*)pCsr;
229078  return rc;
229079}
229080
229081static void fts5VocabResetCursor(Fts5VocabCursor *pCsr){
229082  pCsr->rowid = 0;
229083  sqlite3Fts5IterClose(pCsr->pIter);
229084  pCsr->pIter = 0;
229085  sqlite3_free(pCsr->zLeTerm);
229086  pCsr->nLeTerm = -1;
229087  pCsr->zLeTerm = 0;
229088  pCsr->bEof = 0;
229089}
229090
229091/*
229092** Close the cursor.  For additional information see the documentation
229093** on the xClose method of the virtual table interface.
229094*/
229095static int fts5VocabCloseMethod(sqlite3_vtab_cursor *pCursor){
229096  Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
229097  fts5VocabResetCursor(pCsr);
229098  sqlite3Fts5BufferFree(&pCsr->term);
229099  sqlite3_finalize(pCsr->pStmt);
229100  sqlite3_free(pCsr);
229101  return SQLITE_OK;
229102}
229103
229104static int fts5VocabInstanceNewTerm(Fts5VocabCursor *pCsr){
229105  int rc = SQLITE_OK;
229106
229107  if( sqlite3Fts5IterEof(pCsr->pIter) ){
229108    pCsr->bEof = 1;
229109  }else{
229110    const char *zTerm;
229111    int nTerm;
229112    zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
229113    if( pCsr->nLeTerm>=0 ){
229114      int nCmp = MIN(nTerm, pCsr->nLeTerm);
229115      int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
229116      if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
229117        pCsr->bEof = 1;
229118      }
229119    }
229120
229121    sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
229122  }
229123  return rc;
229124}
229125
229126static int fts5VocabInstanceNext(Fts5VocabCursor *pCsr){
229127  int eDetail = pCsr->pFts5->pConfig->eDetail;
229128  int rc = SQLITE_OK;
229129  Fts5IndexIter *pIter = pCsr->pIter;
229130  i64 *pp = &pCsr->iInstPos;
229131  int *po = &pCsr->iInstOff;
229132
229133  assert( sqlite3Fts5IterEof(pIter)==0 );
229134  assert( pCsr->bEof==0 );
229135  while( eDetail==FTS5_DETAIL_NONE
229136      || sqlite3Fts5PoslistNext64(pIter->pData, pIter->nData, po, pp)
229137  ){
229138    pCsr->iInstPos = 0;
229139    pCsr->iInstOff = 0;
229140
229141    rc = sqlite3Fts5IterNextScan(pCsr->pIter);
229142    if( rc==SQLITE_OK ){
229143      rc = fts5VocabInstanceNewTerm(pCsr);
229144      if( pCsr->bEof || eDetail==FTS5_DETAIL_NONE ) break;
229145    }
229146    if( rc ){
229147      pCsr->bEof = 1;
229148      break;
229149    }
229150  }
229151
229152  return rc;
229153}
229154
229155/*
229156** Advance the cursor to the next row in the table.
229157*/
229158static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){
229159  Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
229160  Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
229161  int rc = SQLITE_OK;
229162  int nCol = pCsr->pFts5->pConfig->nCol;
229163
229164  pCsr->rowid++;
229165
229166  if( pTab->eType==FTS5_VOCAB_INSTANCE ){
229167    return fts5VocabInstanceNext(pCsr);
229168  }
229169
229170  if( pTab->eType==FTS5_VOCAB_COL ){
229171    for(pCsr->iCol++; pCsr->iCol<nCol; pCsr->iCol++){
229172      if( pCsr->aDoc[pCsr->iCol] ) break;
229173    }
229174  }
229175
229176  if( pTab->eType!=FTS5_VOCAB_COL || pCsr->iCol>=nCol ){
229177    if( sqlite3Fts5IterEof(pCsr->pIter) ){
229178      pCsr->bEof = 1;
229179    }else{
229180      const char *zTerm;
229181      int nTerm;
229182
229183      zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
229184      assert( nTerm>=0 );
229185      if( pCsr->nLeTerm>=0 ){
229186        int nCmp = MIN(nTerm, pCsr->nLeTerm);
229187        int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
229188        if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
229189          pCsr->bEof = 1;
229190          return SQLITE_OK;
229191        }
229192      }
229193
229194      sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
229195      memset(pCsr->aCnt, 0, nCol * sizeof(i64));
229196      memset(pCsr->aDoc, 0, nCol * sizeof(i64));
229197      pCsr->iCol = 0;
229198
229199      assert( pTab->eType==FTS5_VOCAB_COL || pTab->eType==FTS5_VOCAB_ROW );
229200      while( rc==SQLITE_OK ){
229201        int eDetail = pCsr->pFts5->pConfig->eDetail;
229202        const u8 *pPos; int nPos;   /* Position list */
229203        i64 iPos = 0;               /* 64-bit position read from poslist */
229204        int iOff = 0;               /* Current offset within position list */
229205
229206        pPos = pCsr->pIter->pData;
229207        nPos = pCsr->pIter->nData;
229208
229209        switch( pTab->eType ){
229210          case FTS5_VOCAB_ROW:
229211            if( eDetail==FTS5_DETAIL_FULL ){
229212              while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
229213                pCsr->aCnt[0]++;
229214              }
229215            }
229216            pCsr->aDoc[0]++;
229217            break;
229218
229219          case FTS5_VOCAB_COL:
229220            if( eDetail==FTS5_DETAIL_FULL ){
229221              int iCol = -1;
229222              while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
229223                int ii = FTS5_POS2COLUMN(iPos);
229224                if( iCol!=ii ){
229225                  if( ii>=nCol ){
229226                    rc = FTS5_CORRUPT;
229227                    break;
229228                  }
229229                  pCsr->aDoc[ii]++;
229230                  iCol = ii;
229231                }
229232                pCsr->aCnt[ii]++;
229233              }
229234            }else if( eDetail==FTS5_DETAIL_COLUMNS ){
229235              while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff,&iPos) ){
229236                assert_nc( iPos>=0 && iPos<nCol );
229237                if( iPos>=nCol ){
229238                  rc = FTS5_CORRUPT;
229239                  break;
229240                }
229241                pCsr->aDoc[iPos]++;
229242              }
229243            }else{
229244              assert( eDetail==FTS5_DETAIL_NONE );
229245              pCsr->aDoc[0]++;
229246            }
229247            break;
229248
229249          default:
229250            assert( pTab->eType==FTS5_VOCAB_INSTANCE );
229251            break;
229252        }
229253
229254        if( rc==SQLITE_OK ){
229255          rc = sqlite3Fts5IterNextScan(pCsr->pIter);
229256        }
229257        if( pTab->eType==FTS5_VOCAB_INSTANCE ) break;
229258
229259        if( rc==SQLITE_OK ){
229260          zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
229261          if( nTerm!=pCsr->term.n
229262          || (nTerm>0 && memcmp(zTerm, pCsr->term.p, nTerm))
229263          ){
229264            break;
229265          }
229266          if( sqlite3Fts5IterEof(pCsr->pIter) ) break;
229267        }
229268      }
229269    }
229270  }
229271
229272  if( rc==SQLITE_OK && pCsr->bEof==0 && pTab->eType==FTS5_VOCAB_COL ){
229273    for(/* noop */; pCsr->iCol<nCol && pCsr->aDoc[pCsr->iCol]==0; pCsr->iCol++);
229274    if( pCsr->iCol==nCol ){
229275      rc = FTS5_CORRUPT;
229276    }
229277  }
229278  return rc;
229279}
229280
229281/*
229282** This is the xFilter implementation for the virtual table.
229283*/
229284static int fts5VocabFilterMethod(
229285  sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
229286  int idxNum,                     /* Strategy index */
229287  const char *zUnused,            /* Unused */
229288  int nUnused,                    /* Number of elements in apVal */
229289  sqlite3_value **apVal           /* Arguments for the indexing scheme */
229290){
229291  Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
229292  Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
229293  int eType = pTab->eType;
229294  int rc = SQLITE_OK;
229295
229296  int iVal = 0;
229297  int f = FTS5INDEX_QUERY_SCAN;
229298  const char *zTerm = 0;
229299  int nTerm = 0;
229300
229301  sqlite3_value *pEq = 0;
229302  sqlite3_value *pGe = 0;
229303  sqlite3_value *pLe = 0;
229304
229305  UNUSED_PARAM2(zUnused, nUnused);
229306
229307  fts5VocabResetCursor(pCsr);
229308  if( idxNum & FTS5_VOCAB_TERM_EQ ) pEq = apVal[iVal++];
229309  if( idxNum & FTS5_VOCAB_TERM_GE ) pGe = apVal[iVal++];
229310  if( idxNum & FTS5_VOCAB_TERM_LE ) pLe = apVal[iVal++];
229311
229312  if( pEq ){
229313    zTerm = (const char *)sqlite3_value_text(pEq);
229314    nTerm = sqlite3_value_bytes(pEq);
229315    f = 0;
229316  }else{
229317    if( pGe ){
229318      zTerm = (const char *)sqlite3_value_text(pGe);
229319      nTerm = sqlite3_value_bytes(pGe);
229320    }
229321    if( pLe ){
229322      const char *zCopy = (const char *)sqlite3_value_text(pLe);
229323      if( zCopy==0 ) zCopy = "";
229324      pCsr->nLeTerm = sqlite3_value_bytes(pLe);
229325      pCsr->zLeTerm = sqlite3_malloc(pCsr->nLeTerm+1);
229326      if( pCsr->zLeTerm==0 ){
229327        rc = SQLITE_NOMEM;
229328      }else{
229329        memcpy(pCsr->zLeTerm, zCopy, pCsr->nLeTerm+1);
229330      }
229331    }
229332  }
229333
229334  if( rc==SQLITE_OK ){
229335    Fts5Index *pIndex = pCsr->pFts5->pIndex;
229336    rc = sqlite3Fts5IndexQuery(pIndex, zTerm, nTerm, f, 0, &pCsr->pIter);
229337  }
229338  if( rc==SQLITE_OK && eType==FTS5_VOCAB_INSTANCE ){
229339    rc = fts5VocabInstanceNewTerm(pCsr);
229340  }
229341  if( rc==SQLITE_OK && !pCsr->bEof
229342   && (eType!=FTS5_VOCAB_INSTANCE
229343    || pCsr->pFts5->pConfig->eDetail!=FTS5_DETAIL_NONE)
229344  ){
229345    rc = fts5VocabNextMethod(pCursor);
229346  }
229347
229348  return rc;
229349}
229350
229351/*
229352** This is the xEof method of the virtual table. SQLite calls this
229353** routine to find out if it has reached the end of a result set.
229354*/
229355static int fts5VocabEofMethod(sqlite3_vtab_cursor *pCursor){
229356  Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
229357  return pCsr->bEof;
229358}
229359
229360static int fts5VocabColumnMethod(
229361  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
229362  sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
229363  int iCol                        /* Index of column to read value from */
229364){
229365  Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
229366  int eDetail = pCsr->pFts5->pConfig->eDetail;
229367  int eType = ((Fts5VocabTable*)(pCursor->pVtab))->eType;
229368  i64 iVal = 0;
229369
229370  if( iCol==0 ){
229371    sqlite3_result_text(
229372        pCtx, (const char*)pCsr->term.p, pCsr->term.n, SQLITE_TRANSIENT
229373    );
229374  }else if( eType==FTS5_VOCAB_COL ){
229375    assert( iCol==1 || iCol==2 || iCol==3 );
229376    if( iCol==1 ){
229377      if( eDetail!=FTS5_DETAIL_NONE ){
229378        const char *z = pCsr->pFts5->pConfig->azCol[pCsr->iCol];
229379        sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC);
229380      }
229381    }else if( iCol==2 ){
229382      iVal = pCsr->aDoc[pCsr->iCol];
229383    }else{
229384      iVal = pCsr->aCnt[pCsr->iCol];
229385    }
229386  }else if( eType==FTS5_VOCAB_ROW ){
229387    assert( iCol==1 || iCol==2 );
229388    if( iCol==1 ){
229389      iVal = pCsr->aDoc[0];
229390    }else{
229391      iVal = pCsr->aCnt[0];
229392    }
229393  }else{
229394    assert( eType==FTS5_VOCAB_INSTANCE );
229395    switch( iCol ){
229396      case 1:
229397        sqlite3_result_int64(pCtx, pCsr->pIter->iRowid);
229398        break;
229399      case 2: {
229400        int ii = -1;
229401        if( eDetail==FTS5_DETAIL_FULL ){
229402          ii = FTS5_POS2COLUMN(pCsr->iInstPos);
229403        }else if( eDetail==FTS5_DETAIL_COLUMNS ){
229404          ii = (int)pCsr->iInstPos;
229405        }
229406        if( ii>=0 && ii<pCsr->pFts5->pConfig->nCol ){
229407          const char *z = pCsr->pFts5->pConfig->azCol[ii];
229408          sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC);
229409        }
229410        break;
229411      }
229412      default: {
229413        assert( iCol==3 );
229414        if( eDetail==FTS5_DETAIL_FULL ){
229415          int ii = FTS5_POS2OFFSET(pCsr->iInstPos);
229416          sqlite3_result_int(pCtx, ii);
229417        }
229418        break;
229419      }
229420    }
229421  }
229422
229423  if( iVal>0 ) sqlite3_result_int64(pCtx, iVal);
229424  return SQLITE_OK;
229425}
229426
229427/*
229428** This is the xRowid method. The SQLite core calls this routine to
229429** retrieve the rowid for the current row of the result set. The
229430** rowid should be written to *pRowid.
229431*/
229432static int fts5VocabRowidMethod(
229433  sqlite3_vtab_cursor *pCursor,
229434  sqlite_int64 *pRowid
229435){
229436  Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
229437  *pRowid = pCsr->rowid;
229438  return SQLITE_OK;
229439}
229440
229441static int sqlite3Fts5VocabInit(Fts5Global *pGlobal, sqlite3 *db){
229442  static const sqlite3_module fts5Vocab = {
229443    /* iVersion      */ 2,
229444    /* xCreate       */ fts5VocabCreateMethod,
229445    /* xConnect      */ fts5VocabConnectMethod,
229446    /* xBestIndex    */ fts5VocabBestIndexMethod,
229447    /* xDisconnect   */ fts5VocabDisconnectMethod,
229448    /* xDestroy      */ fts5VocabDestroyMethod,
229449    /* xOpen         */ fts5VocabOpenMethod,
229450    /* xClose        */ fts5VocabCloseMethod,
229451    /* xFilter       */ fts5VocabFilterMethod,
229452    /* xNext         */ fts5VocabNextMethod,
229453    /* xEof          */ fts5VocabEofMethod,
229454    /* xColumn       */ fts5VocabColumnMethod,
229455    /* xRowid        */ fts5VocabRowidMethod,
229456    /* xUpdate       */ 0,
229457    /* xBegin        */ 0,
229458    /* xSync         */ 0,
229459    /* xCommit       */ 0,
229460    /* xRollback     */ 0,
229461    /* xFindFunction */ 0,
229462    /* xRename       */ 0,
229463    /* xSavepoint    */ 0,
229464    /* xRelease      */ 0,
229465    /* xRollbackTo   */ 0,
229466    /* xShadowName   */ 0
229467  };
229468  void *p = (void*)pGlobal;
229469
229470  return sqlite3_create_module_v2(db, "fts5vocab", &fts5Vocab, p, 0);
229471}
229472
229473
229474
229475#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */
229476
229477/************** End of fts5.c ************************************************/
229478/************** Begin file stmt.c ********************************************/
229479/*
229480** 2017-05-31
229481**
229482** The author disclaims copyright to this source code.  In place of
229483** a legal notice, here is a blessing:
229484**
229485**    May you do good and not evil.
229486**    May you find forgiveness for yourself and forgive others.
229487**    May you share freely, never taking more than you give.
229488**
229489*************************************************************************
229490**
229491** This file demonstrates an eponymous virtual table that returns information
229492** about all prepared statements for the database connection.
229493**
229494** Usage example:
229495**
229496**     .load ./stmt
229497**     .mode line
229498**     .header on
229499**     SELECT * FROM stmt;
229500*/
229501#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB)
229502#if !defined(SQLITEINT_H)
229503/* #include "sqlite3ext.h" */
229504#endif
229505SQLITE_EXTENSION_INIT1
229506/* #include <assert.h> */
229507/* #include <string.h> */
229508
229509#ifndef SQLITE_OMIT_VIRTUALTABLE
229510
229511/* stmt_vtab is a subclass of sqlite3_vtab which will
229512** serve as the underlying representation of a stmt virtual table
229513*/
229514typedef struct stmt_vtab stmt_vtab;
229515struct stmt_vtab {
229516  sqlite3_vtab base;  /* Base class - must be first */
229517  sqlite3 *db;        /* Database connection for this stmt vtab */
229518};
229519
229520/* stmt_cursor is a subclass of sqlite3_vtab_cursor which will
229521** serve as the underlying representation of a cursor that scans
229522** over rows of the result
229523*/
229524typedef struct stmt_cursor stmt_cursor;
229525struct stmt_cursor {
229526  sqlite3_vtab_cursor base;  /* Base class - must be first */
229527  sqlite3 *db;               /* Database connection for this cursor */
229528  sqlite3_stmt *pStmt;       /* Statement cursor is currently pointing at */
229529  sqlite3_int64 iRowid;      /* The rowid */
229530};
229531
229532/*
229533** The stmtConnect() method is invoked to create a new
229534** stmt_vtab that describes the stmt virtual table.
229535**
229536** Think of this routine as the constructor for stmt_vtab objects.
229537**
229538** All this routine needs to do is:
229539**
229540**    (1) Allocate the stmt_vtab object and initialize all fields.
229541**
229542**    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
229543**        result set of queries against stmt will look like.
229544*/
229545static int stmtConnect(
229546  sqlite3 *db,
229547  void *pAux,
229548  int argc, const char *const*argv,
229549  sqlite3_vtab **ppVtab,
229550  char **pzErr
229551){
229552  stmt_vtab *pNew;
229553  int rc;
229554
229555/* Column numbers */
229556#define STMT_COLUMN_SQL     0   /* SQL for the statement */
229557#define STMT_COLUMN_NCOL    1   /* Number of result columns */
229558#define STMT_COLUMN_RO      2   /* True if read-only */
229559#define STMT_COLUMN_BUSY    3   /* True if currently busy */
229560#define STMT_COLUMN_NSCAN   4   /* SQLITE_STMTSTATUS_FULLSCAN_STEP */
229561#define STMT_COLUMN_NSORT   5   /* SQLITE_STMTSTATUS_SORT */
229562#define STMT_COLUMN_NAIDX   6   /* SQLITE_STMTSTATUS_AUTOINDEX */
229563#define STMT_COLUMN_NSTEP   7   /* SQLITE_STMTSTATUS_VM_STEP */
229564#define STMT_COLUMN_REPREP  8   /* SQLITE_STMTSTATUS_REPREPARE */
229565#define STMT_COLUMN_RUN     9   /* SQLITE_STMTSTATUS_RUN */
229566#define STMT_COLUMN_MEM    10   /* SQLITE_STMTSTATUS_MEMUSED */
229567
229568
229569  rc = sqlite3_declare_vtab(db,
229570     "CREATE TABLE x(sql,ncol,ro,busy,nscan,nsort,naidx,nstep,"
229571                    "reprep,run,mem)");
229572  if( rc==SQLITE_OK ){
229573    pNew = sqlite3_malloc( sizeof(*pNew) );
229574    *ppVtab = (sqlite3_vtab*)pNew;
229575    if( pNew==0 ) return SQLITE_NOMEM;
229576    memset(pNew, 0, sizeof(*pNew));
229577    pNew->db = db;
229578  }
229579  return rc;
229580}
229581
229582/*
229583** This method is the destructor for stmt_cursor objects.
229584*/
229585static int stmtDisconnect(sqlite3_vtab *pVtab){
229586  sqlite3_free(pVtab);
229587  return SQLITE_OK;
229588}
229589
229590/*
229591** Constructor for a new stmt_cursor object.
229592*/
229593static int stmtOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
229594  stmt_cursor *pCur;
229595  pCur = sqlite3_malloc( sizeof(*pCur) );
229596  if( pCur==0 ) return SQLITE_NOMEM;
229597  memset(pCur, 0, sizeof(*pCur));
229598  pCur->db = ((stmt_vtab*)p)->db;
229599  *ppCursor = &pCur->base;
229600  return SQLITE_OK;
229601}
229602
229603/*
229604** Destructor for a stmt_cursor.
229605*/
229606static int stmtClose(sqlite3_vtab_cursor *cur){
229607  sqlite3_free(cur);
229608  return SQLITE_OK;
229609}
229610
229611
229612/*
229613** Advance a stmt_cursor to its next row of output.
229614*/
229615static int stmtNext(sqlite3_vtab_cursor *cur){
229616  stmt_cursor *pCur = (stmt_cursor*)cur;
229617  pCur->iRowid++;
229618  pCur->pStmt = sqlite3_next_stmt(pCur->db, pCur->pStmt);
229619  return SQLITE_OK;
229620}
229621
229622/*
229623** Return values of columns for the row at which the stmt_cursor
229624** is currently pointing.
229625*/
229626static int stmtColumn(
229627  sqlite3_vtab_cursor *cur,   /* The cursor */
229628  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
229629  int i                       /* Which column to return */
229630){
229631  stmt_cursor *pCur = (stmt_cursor*)cur;
229632  switch( i ){
229633    case STMT_COLUMN_SQL: {
229634      sqlite3_result_text(ctx, sqlite3_sql(pCur->pStmt), -1, SQLITE_TRANSIENT);
229635      break;
229636    }
229637    case STMT_COLUMN_NCOL: {
229638      sqlite3_result_int(ctx, sqlite3_column_count(pCur->pStmt));
229639      break;
229640    }
229641    case STMT_COLUMN_RO: {
229642      sqlite3_result_int(ctx, sqlite3_stmt_readonly(pCur->pStmt));
229643      break;
229644    }
229645    case STMT_COLUMN_BUSY: {
229646      sqlite3_result_int(ctx, sqlite3_stmt_busy(pCur->pStmt));
229647      break;
229648    }
229649    default: {
229650      assert( i==STMT_COLUMN_MEM );
229651      i = SQLITE_STMTSTATUS_MEMUSED +
229652            STMT_COLUMN_NSCAN - SQLITE_STMTSTATUS_FULLSCAN_STEP;
229653      /* Fall thru */
229654    }
229655    case STMT_COLUMN_NSCAN:
229656    case STMT_COLUMN_NSORT:
229657    case STMT_COLUMN_NAIDX:
229658    case STMT_COLUMN_NSTEP:
229659    case STMT_COLUMN_REPREP:
229660    case STMT_COLUMN_RUN: {
229661      sqlite3_result_int(ctx, sqlite3_stmt_status(pCur->pStmt,
229662                      i-STMT_COLUMN_NSCAN+SQLITE_STMTSTATUS_FULLSCAN_STEP, 0));
229663      break;
229664    }
229665  }
229666  return SQLITE_OK;
229667}
229668
229669/*
229670** Return the rowid for the current row.  In this implementation, the
229671** rowid is the same as the output value.
229672*/
229673static int stmtRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
229674  stmt_cursor *pCur = (stmt_cursor*)cur;
229675  *pRowid = pCur->iRowid;
229676  return SQLITE_OK;
229677}
229678
229679/*
229680** Return TRUE if the cursor has been moved off of the last
229681** row of output.
229682*/
229683static int stmtEof(sqlite3_vtab_cursor *cur){
229684  stmt_cursor *pCur = (stmt_cursor*)cur;
229685  return pCur->pStmt==0;
229686}
229687
229688/*
229689** This method is called to "rewind" the stmt_cursor object back
229690** to the first row of output.  This method is always called at least
229691** once prior to any call to stmtColumn() or stmtRowid() or
229692** stmtEof().
229693*/
229694static int stmtFilter(
229695  sqlite3_vtab_cursor *pVtabCursor,
229696  int idxNum, const char *idxStr,
229697  int argc, sqlite3_value **argv
229698){
229699  stmt_cursor *pCur = (stmt_cursor *)pVtabCursor;
229700  pCur->pStmt = 0;
229701  pCur->iRowid = 0;
229702  return stmtNext(pVtabCursor);
229703}
229704
229705/*
229706** SQLite will invoke this method one or more times while planning a query
229707** that uses the stmt virtual table.  This routine needs to create
229708** a query plan for each invocation and compute an estimated cost for that
229709** plan.
229710*/
229711static int stmtBestIndex(
229712  sqlite3_vtab *tab,
229713  sqlite3_index_info *pIdxInfo
229714){
229715  pIdxInfo->estimatedCost = (double)500;
229716  pIdxInfo->estimatedRows = 500;
229717  return SQLITE_OK;
229718}
229719
229720/*
229721** This following structure defines all the methods for the
229722** stmt virtual table.
229723*/
229724static sqlite3_module stmtModule = {
229725  0,                         /* iVersion */
229726  0,                         /* xCreate */
229727  stmtConnect,               /* xConnect */
229728  stmtBestIndex,             /* xBestIndex */
229729  stmtDisconnect,            /* xDisconnect */
229730  0,                         /* xDestroy */
229731  stmtOpen,                  /* xOpen - open a cursor */
229732  stmtClose,                 /* xClose - close a cursor */
229733  stmtFilter,                /* xFilter - configure scan constraints */
229734  stmtNext,                  /* xNext - advance a cursor */
229735  stmtEof,                   /* xEof - check for end of scan */
229736  stmtColumn,                /* xColumn - read data */
229737  stmtRowid,                 /* xRowid - read data */
229738  0,                         /* xUpdate */
229739  0,                         /* xBegin */
229740  0,                         /* xSync */
229741  0,                         /* xCommit */
229742  0,                         /* xRollback */
229743  0,                         /* xFindMethod */
229744  0,                         /* xRename */
229745  0,                         /* xSavepoint */
229746  0,                         /* xRelease */
229747  0,                         /* xRollbackTo */
229748  0,                         /* xShadowName */
229749};
229750
229751#endif /* SQLITE_OMIT_VIRTUALTABLE */
229752
229753SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3 *db){
229754  int rc = SQLITE_OK;
229755#ifndef SQLITE_OMIT_VIRTUALTABLE
229756  rc = sqlite3_create_module(db, "sqlite_stmt", &stmtModule, 0);
229757#endif
229758  return rc;
229759}
229760
229761#ifndef SQLITE_CORE
229762#ifdef _WIN32
229763__declspec(dllexport)
229764#endif
229765SQLITE_API int sqlite3_stmt_init(
229766  sqlite3 *db,
229767  char **pzErrMsg,
229768  const sqlite3_api_routines *pApi
229769){
229770  int rc = SQLITE_OK;
229771  SQLITE_EXTENSION_INIT2(pApi);
229772#ifndef SQLITE_OMIT_VIRTUALTABLE
229773  rc = sqlite3StmtVtabInit(db);
229774#endif
229775  return rc;
229776}
229777#endif /* SQLITE_CORE */
229778#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
229779
229780/************** End of stmt.c ************************************************/
229781#if __LINE__!=229781
229782#undef SQLITE_SOURCE_ID
229783#define SQLITE_SOURCE_ID      "2020-06-18 14:00:33 7ebdfa80be8e8e73324b8d66b3460222eb74c7e9dfd655b48d6ca7e1933calt2"
229784#endif
229785/* Return the source-id for this library */
229786SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
229787/************************** End of sqlite3.c ******************************/
229788